e1000_mac.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. /*******************************************************************************
  2. Intel(R) Gigabit Ethernet Linux driver
  3. Copyright(c) 2007-2013 Intel Corporation.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Contact Information:
  17. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. #include <linux/if_ether.h>
  21. #include <linux/delay.h>
  22. #include <linux/pci.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include "e1000_mac.h"
  26. #include "igb.h"
  27. static s32 igb_set_default_fc(struct e1000_hw *hw);
  28. static s32 igb_set_fc_watermarks(struct e1000_hw *hw);
  29. /**
  30. * igb_get_bus_info_pcie - Get PCIe bus information
  31. * @hw: pointer to the HW structure
  32. *
  33. * Determines and stores the system bus information for a particular
  34. * network interface. The following bus information is determined and stored:
  35. * bus speed, bus width, type (PCIe), and PCIe function.
  36. **/
  37. s32 igb_get_bus_info_pcie(struct e1000_hw *hw)
  38. {
  39. struct e1000_bus_info *bus = &hw->bus;
  40. s32 ret_val;
  41. u32 reg;
  42. u16 pcie_link_status;
  43. bus->type = e1000_bus_type_pci_express;
  44. ret_val = igb_read_pcie_cap_reg(hw,
  45. PCI_EXP_LNKSTA,
  46. &pcie_link_status);
  47. if (ret_val) {
  48. bus->width = e1000_bus_width_unknown;
  49. bus->speed = e1000_bus_speed_unknown;
  50. } else {
  51. switch (pcie_link_status & PCI_EXP_LNKSTA_CLS) {
  52. case PCI_EXP_LNKSTA_CLS_2_5GB:
  53. bus->speed = e1000_bus_speed_2500;
  54. break;
  55. case PCI_EXP_LNKSTA_CLS_5_0GB:
  56. bus->speed = e1000_bus_speed_5000;
  57. break;
  58. default:
  59. bus->speed = e1000_bus_speed_unknown;
  60. break;
  61. }
  62. bus->width = (enum e1000_bus_width)((pcie_link_status &
  63. PCI_EXP_LNKSTA_NLW) >>
  64. PCI_EXP_LNKSTA_NLW_SHIFT);
  65. }
  66. reg = rd32(E1000_STATUS);
  67. bus->func = (reg & E1000_STATUS_FUNC_MASK) >> E1000_STATUS_FUNC_SHIFT;
  68. return 0;
  69. }
  70. /**
  71. * igb_clear_vfta - Clear VLAN filter table
  72. * @hw: pointer to the HW structure
  73. *
  74. * Clears the register array which contains the VLAN filter table by
  75. * setting all the values to 0.
  76. **/
  77. void igb_clear_vfta(struct e1000_hw *hw)
  78. {
  79. u32 offset;
  80. for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
  81. array_wr32(E1000_VFTA, offset, 0);
  82. wrfl();
  83. }
  84. }
  85. /**
  86. * igb_write_vfta - Write value to VLAN filter table
  87. * @hw: pointer to the HW structure
  88. * @offset: register offset in VLAN filter table
  89. * @value: register value written to VLAN filter table
  90. *
  91. * Writes value at the given offset in the register array which stores
  92. * the VLAN filter table.
  93. **/
  94. static void igb_write_vfta(struct e1000_hw *hw, u32 offset, u32 value)
  95. {
  96. array_wr32(E1000_VFTA, offset, value);
  97. wrfl();
  98. }
  99. /* Due to a hw errata, if the host tries to configure the VFTA register
  100. * while performing queries from the BMC or DMA, then the VFTA in some
  101. * cases won't be written.
  102. */
  103. /**
  104. * igb_clear_vfta_i350 - Clear VLAN filter table
  105. * @hw: pointer to the HW structure
  106. *
  107. * Clears the register array which contains the VLAN filter table by
  108. * setting all the values to 0.
  109. **/
  110. void igb_clear_vfta_i350(struct e1000_hw *hw)
  111. {
  112. u32 offset;
  113. int i;
  114. for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
  115. for (i = 0; i < 10; i++)
  116. array_wr32(E1000_VFTA, offset, 0);
  117. wrfl();
  118. }
  119. }
  120. /**
  121. * igb_write_vfta_i350 - Write value to VLAN filter table
  122. * @hw: pointer to the HW structure
  123. * @offset: register offset in VLAN filter table
  124. * @value: register value written to VLAN filter table
  125. *
  126. * Writes value at the given offset in the register array which stores
  127. * the VLAN filter table.
  128. **/
  129. static void igb_write_vfta_i350(struct e1000_hw *hw, u32 offset, u32 value)
  130. {
  131. int i;
  132. for (i = 0; i < 10; i++)
  133. array_wr32(E1000_VFTA, offset, value);
  134. wrfl();
  135. }
  136. /**
  137. * igb_init_rx_addrs - Initialize receive address's
  138. * @hw: pointer to the HW structure
  139. * @rar_count: receive address registers
  140. *
  141. * Setups the receive address registers by setting the base receive address
  142. * register to the devices MAC address and clearing all the other receive
  143. * address registers to 0.
  144. **/
  145. void igb_init_rx_addrs(struct e1000_hw *hw, u16 rar_count)
  146. {
  147. u32 i;
  148. u8 mac_addr[ETH_ALEN] = {0};
  149. /* Setup the receive address */
  150. hw_dbg("Programming MAC Address into RAR[0]\n");
  151. hw->mac.ops.rar_set(hw, hw->mac.addr, 0);
  152. /* Zero out the other (rar_entry_count - 1) receive addresses */
  153. hw_dbg("Clearing RAR[1-%u]\n", rar_count-1);
  154. for (i = 1; i < rar_count; i++)
  155. hw->mac.ops.rar_set(hw, mac_addr, i);
  156. }
  157. /**
  158. * igb_vfta_set - enable or disable vlan in VLAN filter table
  159. * @hw: pointer to the HW structure
  160. * @vid: VLAN id to add or remove
  161. * @add: if true add filter, if false remove
  162. *
  163. * Sets or clears a bit in the VLAN filter table array based on VLAN id
  164. * and if we are adding or removing the filter
  165. **/
  166. s32 igb_vfta_set(struct e1000_hw *hw, u32 vid, bool add)
  167. {
  168. u32 index = (vid >> E1000_VFTA_ENTRY_SHIFT) & E1000_VFTA_ENTRY_MASK;
  169. u32 mask = 1 << (vid & E1000_VFTA_ENTRY_BIT_SHIFT_MASK);
  170. u32 vfta;
  171. struct igb_adapter *adapter = hw->back;
  172. s32 ret_val = 0;
  173. vfta = adapter->shadow_vfta[index];
  174. /* bit was set/cleared before we started */
  175. if ((!!(vfta & mask)) == add) {
  176. ret_val = -E1000_ERR_CONFIG;
  177. } else {
  178. if (add)
  179. vfta |= mask;
  180. else
  181. vfta &= ~mask;
  182. }
  183. if ((hw->mac.type == e1000_i350) || (hw->mac.type == e1000_i354))
  184. igb_write_vfta_i350(hw, index, vfta);
  185. else
  186. igb_write_vfta(hw, index, vfta);
  187. adapter->shadow_vfta[index] = vfta;
  188. return ret_val;
  189. }
  190. /**
  191. * igb_check_alt_mac_addr - Check for alternate MAC addr
  192. * @hw: pointer to the HW structure
  193. *
  194. * Checks the nvm for an alternate MAC address. An alternate MAC address
  195. * can be setup by pre-boot software and must be treated like a permanent
  196. * address and must override the actual permanent MAC address. If an
  197. * alternate MAC address is found it is saved in the hw struct and
  198. * programmed into RAR0 and the function returns success, otherwise the
  199. * function returns an error.
  200. **/
  201. s32 igb_check_alt_mac_addr(struct e1000_hw *hw)
  202. {
  203. u32 i;
  204. s32 ret_val = 0;
  205. u16 offset, nvm_alt_mac_addr_offset, nvm_data;
  206. u8 alt_mac_addr[ETH_ALEN];
  207. /* Alternate MAC address is handled by the option ROM for 82580
  208. * and newer. SW support not required.
  209. */
  210. if (hw->mac.type >= e1000_82580)
  211. goto out;
  212. ret_val = hw->nvm.ops.read(hw, NVM_ALT_MAC_ADDR_PTR, 1,
  213. &nvm_alt_mac_addr_offset);
  214. if (ret_val) {
  215. hw_dbg("NVM Read Error\n");
  216. goto out;
  217. }
  218. if ((nvm_alt_mac_addr_offset == 0xFFFF) ||
  219. (nvm_alt_mac_addr_offset == 0x0000))
  220. /* There is no Alternate MAC Address */
  221. goto out;
  222. if (hw->bus.func == E1000_FUNC_1)
  223. nvm_alt_mac_addr_offset += E1000_ALT_MAC_ADDRESS_OFFSET_LAN1;
  224. if (hw->bus.func == E1000_FUNC_2)
  225. nvm_alt_mac_addr_offset += E1000_ALT_MAC_ADDRESS_OFFSET_LAN2;
  226. if (hw->bus.func == E1000_FUNC_3)
  227. nvm_alt_mac_addr_offset += E1000_ALT_MAC_ADDRESS_OFFSET_LAN3;
  228. for (i = 0; i < ETH_ALEN; i += 2) {
  229. offset = nvm_alt_mac_addr_offset + (i >> 1);
  230. ret_val = hw->nvm.ops.read(hw, offset, 1, &nvm_data);
  231. if (ret_val) {
  232. hw_dbg("NVM Read Error\n");
  233. goto out;
  234. }
  235. alt_mac_addr[i] = (u8)(nvm_data & 0xFF);
  236. alt_mac_addr[i + 1] = (u8)(nvm_data >> 8);
  237. }
  238. /* if multicast bit is set, the alternate address will not be used */
  239. if (is_multicast_ether_addr(alt_mac_addr)) {
  240. hw_dbg("Ignoring Alternate Mac Address with MC bit set\n");
  241. goto out;
  242. }
  243. /* We have a valid alternate MAC address, and we want to treat it the
  244. * same as the normal permanent MAC address stored by the HW into the
  245. * RAR. Do this by mapping this address into RAR0.
  246. */
  247. hw->mac.ops.rar_set(hw, alt_mac_addr, 0);
  248. out:
  249. return ret_val;
  250. }
  251. /**
  252. * igb_rar_set - Set receive address register
  253. * @hw: pointer to the HW structure
  254. * @addr: pointer to the receive address
  255. * @index: receive address array register
  256. *
  257. * Sets the receive address array register at index to the address passed
  258. * in by addr.
  259. **/
  260. void igb_rar_set(struct e1000_hw *hw, u8 *addr, u32 index)
  261. {
  262. u32 rar_low, rar_high;
  263. /* HW expects these in little endian so we reverse the byte order
  264. * from network order (big endian) to little endian
  265. */
  266. rar_low = ((u32) addr[0] |
  267. ((u32) addr[1] << 8) |
  268. ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
  269. rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
  270. /* If MAC address zero, no need to set the AV bit */
  271. if (rar_low || rar_high)
  272. rar_high |= E1000_RAH_AV;
  273. /* Some bridges will combine consecutive 32-bit writes into
  274. * a single burst write, which will malfunction on some parts.
  275. * The flushes avoid this.
  276. */
  277. wr32(E1000_RAL(index), rar_low);
  278. wrfl();
  279. wr32(E1000_RAH(index), rar_high);
  280. wrfl();
  281. }
  282. /**
  283. * igb_mta_set - Set multicast filter table address
  284. * @hw: pointer to the HW structure
  285. * @hash_value: determines the MTA register and bit to set
  286. *
  287. * The multicast table address is a register array of 32-bit registers.
  288. * The hash_value is used to determine what register the bit is in, the
  289. * current value is read, the new bit is OR'd in and the new value is
  290. * written back into the register.
  291. **/
  292. void igb_mta_set(struct e1000_hw *hw, u32 hash_value)
  293. {
  294. u32 hash_bit, hash_reg, mta;
  295. /* The MTA is a register array of 32-bit registers. It is
  296. * treated like an array of (32*mta_reg_count) bits. We want to
  297. * set bit BitArray[hash_value]. So we figure out what register
  298. * the bit is in, read it, OR in the new bit, then write
  299. * back the new value. The (hw->mac.mta_reg_count - 1) serves as a
  300. * mask to bits 31:5 of the hash value which gives us the
  301. * register we're modifying. The hash bit within that register
  302. * is determined by the lower 5 bits of the hash value.
  303. */
  304. hash_reg = (hash_value >> 5) & (hw->mac.mta_reg_count - 1);
  305. hash_bit = hash_value & 0x1F;
  306. mta = array_rd32(E1000_MTA, hash_reg);
  307. mta |= (1 << hash_bit);
  308. array_wr32(E1000_MTA, hash_reg, mta);
  309. wrfl();
  310. }
  311. /**
  312. * igb_hash_mc_addr - Generate a multicast hash value
  313. * @hw: pointer to the HW structure
  314. * @mc_addr: pointer to a multicast address
  315. *
  316. * Generates a multicast address hash value which is used to determine
  317. * the multicast filter table array address and new table value. See
  318. * igb_mta_set()
  319. **/
  320. static u32 igb_hash_mc_addr(struct e1000_hw *hw, u8 *mc_addr)
  321. {
  322. u32 hash_value, hash_mask;
  323. u8 bit_shift = 0;
  324. /* Register count multiplied by bits per register */
  325. hash_mask = (hw->mac.mta_reg_count * 32) - 1;
  326. /* For a mc_filter_type of 0, bit_shift is the number of left-shifts
  327. * where 0xFF would still fall within the hash mask.
  328. */
  329. while (hash_mask >> bit_shift != 0xFF)
  330. bit_shift++;
  331. /* The portion of the address that is used for the hash table
  332. * is determined by the mc_filter_type setting.
  333. * The algorithm is such that there is a total of 8 bits of shifting.
  334. * The bit_shift for a mc_filter_type of 0 represents the number of
  335. * left-shifts where the MSB of mc_addr[5] would still fall within
  336. * the hash_mask. Case 0 does this exactly. Since there are a total
  337. * of 8 bits of shifting, then mc_addr[4] will shift right the
  338. * remaining number of bits. Thus 8 - bit_shift. The rest of the
  339. * cases are a variation of this algorithm...essentially raising the
  340. * number of bits to shift mc_addr[5] left, while still keeping the
  341. * 8-bit shifting total.
  342. *
  343. * For example, given the following Destination MAC Address and an
  344. * mta register count of 128 (thus a 4096-bit vector and 0xFFF mask),
  345. * we can see that the bit_shift for case 0 is 4. These are the hash
  346. * values resulting from each mc_filter_type...
  347. * [0] [1] [2] [3] [4] [5]
  348. * 01 AA 00 12 34 56
  349. * LSB MSB
  350. *
  351. * case 0: hash_value = ((0x34 >> 4) | (0x56 << 4)) & 0xFFF = 0x563
  352. * case 1: hash_value = ((0x34 >> 3) | (0x56 << 5)) & 0xFFF = 0xAC6
  353. * case 2: hash_value = ((0x34 >> 2) | (0x56 << 6)) & 0xFFF = 0x163
  354. * case 3: hash_value = ((0x34 >> 0) | (0x56 << 8)) & 0xFFF = 0x634
  355. */
  356. switch (hw->mac.mc_filter_type) {
  357. default:
  358. case 0:
  359. break;
  360. case 1:
  361. bit_shift += 1;
  362. break;
  363. case 2:
  364. bit_shift += 2;
  365. break;
  366. case 3:
  367. bit_shift += 4;
  368. break;
  369. }
  370. hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
  371. (((u16) mc_addr[5]) << bit_shift)));
  372. return hash_value;
  373. }
  374. /**
  375. * igb_update_mc_addr_list - Update Multicast addresses
  376. * @hw: pointer to the HW structure
  377. * @mc_addr_list: array of multicast addresses to program
  378. * @mc_addr_count: number of multicast addresses to program
  379. *
  380. * Updates entire Multicast Table Array.
  381. * The caller must have a packed mc_addr_list of multicast addresses.
  382. **/
  383. void igb_update_mc_addr_list(struct e1000_hw *hw,
  384. u8 *mc_addr_list, u32 mc_addr_count)
  385. {
  386. u32 hash_value, hash_bit, hash_reg;
  387. int i;
  388. /* clear mta_shadow */
  389. memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
  390. /* update mta_shadow from mc_addr_list */
  391. for (i = 0; (u32) i < mc_addr_count; i++) {
  392. hash_value = igb_hash_mc_addr(hw, mc_addr_list);
  393. hash_reg = (hash_value >> 5) & (hw->mac.mta_reg_count - 1);
  394. hash_bit = hash_value & 0x1F;
  395. hw->mac.mta_shadow[hash_reg] |= (1 << hash_bit);
  396. mc_addr_list += (ETH_ALEN);
  397. }
  398. /* replace the entire MTA table */
  399. for (i = hw->mac.mta_reg_count - 1; i >= 0; i--)
  400. array_wr32(E1000_MTA, i, hw->mac.mta_shadow[i]);
  401. wrfl();
  402. }
  403. /**
  404. * igb_clear_hw_cntrs_base - Clear base hardware counters
  405. * @hw: pointer to the HW structure
  406. *
  407. * Clears the base hardware counters by reading the counter registers.
  408. **/
  409. void igb_clear_hw_cntrs_base(struct e1000_hw *hw)
  410. {
  411. rd32(E1000_CRCERRS);
  412. rd32(E1000_SYMERRS);
  413. rd32(E1000_MPC);
  414. rd32(E1000_SCC);
  415. rd32(E1000_ECOL);
  416. rd32(E1000_MCC);
  417. rd32(E1000_LATECOL);
  418. rd32(E1000_COLC);
  419. rd32(E1000_DC);
  420. rd32(E1000_SEC);
  421. rd32(E1000_RLEC);
  422. rd32(E1000_XONRXC);
  423. rd32(E1000_XONTXC);
  424. rd32(E1000_XOFFRXC);
  425. rd32(E1000_XOFFTXC);
  426. rd32(E1000_FCRUC);
  427. rd32(E1000_GPRC);
  428. rd32(E1000_BPRC);
  429. rd32(E1000_MPRC);
  430. rd32(E1000_GPTC);
  431. rd32(E1000_GORCL);
  432. rd32(E1000_GORCH);
  433. rd32(E1000_GOTCL);
  434. rd32(E1000_GOTCH);
  435. rd32(E1000_RNBC);
  436. rd32(E1000_RUC);
  437. rd32(E1000_RFC);
  438. rd32(E1000_ROC);
  439. rd32(E1000_RJC);
  440. rd32(E1000_TORL);
  441. rd32(E1000_TORH);
  442. rd32(E1000_TOTL);
  443. rd32(E1000_TOTH);
  444. rd32(E1000_TPR);
  445. rd32(E1000_TPT);
  446. rd32(E1000_MPTC);
  447. rd32(E1000_BPTC);
  448. }
  449. /**
  450. * igb_check_for_copper_link - Check for link (Copper)
  451. * @hw: pointer to the HW structure
  452. *
  453. * Checks to see of the link status of the hardware has changed. If a
  454. * change in link status has been detected, then we read the PHY registers
  455. * to get the current speed/duplex if link exists.
  456. **/
  457. s32 igb_check_for_copper_link(struct e1000_hw *hw)
  458. {
  459. struct e1000_mac_info *mac = &hw->mac;
  460. s32 ret_val;
  461. bool link;
  462. /* We only want to go out to the PHY registers to see if Auto-Neg
  463. * has completed and/or if our link status has changed. The
  464. * get_link_status flag is set upon receiving a Link Status
  465. * Change or Rx Sequence Error interrupt.
  466. */
  467. if (!mac->get_link_status) {
  468. ret_val = 0;
  469. goto out;
  470. }
  471. /* First we want to see if the MII Status Register reports
  472. * link. If so, then we want to get the current speed/duplex
  473. * of the PHY.
  474. */
  475. ret_val = igb_phy_has_link(hw, 1, 0, &link);
  476. if (ret_val)
  477. goto out;
  478. if (!link)
  479. goto out; /* No link detected */
  480. mac->get_link_status = false;
  481. /* Check if there was DownShift, must be checked
  482. * immediately after link-up
  483. */
  484. igb_check_downshift(hw);
  485. /* If we are forcing speed/duplex, then we simply return since
  486. * we have already determined whether we have link or not.
  487. */
  488. if (!mac->autoneg) {
  489. ret_val = -E1000_ERR_CONFIG;
  490. goto out;
  491. }
  492. /* Auto-Neg is enabled. Auto Speed Detection takes care
  493. * of MAC speed/duplex configuration. So we only need to
  494. * configure Collision Distance in the MAC.
  495. */
  496. igb_config_collision_dist(hw);
  497. /* Configure Flow Control now that Auto-Neg has completed.
  498. * First, we need to restore the desired flow control
  499. * settings because we may have had to re-autoneg with a
  500. * different link partner.
  501. */
  502. ret_val = igb_config_fc_after_link_up(hw);
  503. if (ret_val)
  504. hw_dbg("Error configuring flow control\n");
  505. out:
  506. return ret_val;
  507. }
  508. /**
  509. * igb_setup_link - Setup flow control and link settings
  510. * @hw: pointer to the HW structure
  511. *
  512. * Determines which flow control settings to use, then configures flow
  513. * control. Calls the appropriate media-specific link configuration
  514. * function. Assuming the adapter has a valid link partner, a valid link
  515. * should be established. Assumes the hardware has previously been reset
  516. * and the transmitter and receiver are not enabled.
  517. **/
  518. s32 igb_setup_link(struct e1000_hw *hw)
  519. {
  520. s32 ret_val = 0;
  521. /* In the case of the phy reset being blocked, we already have a link.
  522. * We do not need to set it up again.
  523. */
  524. if (igb_check_reset_block(hw))
  525. goto out;
  526. /* If requested flow control is set to default, set flow control
  527. * based on the EEPROM flow control settings.
  528. */
  529. if (hw->fc.requested_mode == e1000_fc_default) {
  530. ret_val = igb_set_default_fc(hw);
  531. if (ret_val)
  532. goto out;
  533. }
  534. /* We want to save off the original Flow Control configuration just
  535. * in case we get disconnected and then reconnected into a different
  536. * hub or switch with different Flow Control capabilities.
  537. */
  538. hw->fc.current_mode = hw->fc.requested_mode;
  539. hw_dbg("After fix-ups FlowControl is now = %x\n", hw->fc.current_mode);
  540. /* Call the necessary media_type subroutine to configure the link. */
  541. ret_val = hw->mac.ops.setup_physical_interface(hw);
  542. if (ret_val)
  543. goto out;
  544. /* Initialize the flow control address, type, and PAUSE timer
  545. * registers to their default values. This is done even if flow
  546. * control is disabled, because it does not hurt anything to
  547. * initialize these registers.
  548. */
  549. hw_dbg("Initializing the Flow Control address, type and timer regs\n");
  550. wr32(E1000_FCT, FLOW_CONTROL_TYPE);
  551. wr32(E1000_FCAH, FLOW_CONTROL_ADDRESS_HIGH);
  552. wr32(E1000_FCAL, FLOW_CONTROL_ADDRESS_LOW);
  553. wr32(E1000_FCTTV, hw->fc.pause_time);
  554. ret_val = igb_set_fc_watermarks(hw);
  555. out:
  556. return ret_val;
  557. }
  558. /**
  559. * igb_config_collision_dist - Configure collision distance
  560. * @hw: pointer to the HW structure
  561. *
  562. * Configures the collision distance to the default value and is used
  563. * during link setup. Currently no func pointer exists and all
  564. * implementations are handled in the generic version of this function.
  565. **/
  566. void igb_config_collision_dist(struct e1000_hw *hw)
  567. {
  568. u32 tctl;
  569. tctl = rd32(E1000_TCTL);
  570. tctl &= ~E1000_TCTL_COLD;
  571. tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
  572. wr32(E1000_TCTL, tctl);
  573. wrfl();
  574. }
  575. /**
  576. * igb_set_fc_watermarks - Set flow control high/low watermarks
  577. * @hw: pointer to the HW structure
  578. *
  579. * Sets the flow control high/low threshold (watermark) registers. If
  580. * flow control XON frame transmission is enabled, then set XON frame
  581. * tansmission as well.
  582. **/
  583. static s32 igb_set_fc_watermarks(struct e1000_hw *hw)
  584. {
  585. s32 ret_val = 0;
  586. u32 fcrtl = 0, fcrth = 0;
  587. /* Set the flow control receive threshold registers. Normally,
  588. * these registers will be set to a default threshold that may be
  589. * adjusted later by the driver's runtime code. However, if the
  590. * ability to transmit pause frames is not enabled, then these
  591. * registers will be set to 0.
  592. */
  593. if (hw->fc.current_mode & e1000_fc_tx_pause) {
  594. /* We need to set up the Receive Threshold high and low water
  595. * marks as well as (optionally) enabling the transmission of
  596. * XON frames.
  597. */
  598. fcrtl = hw->fc.low_water;
  599. if (hw->fc.send_xon)
  600. fcrtl |= E1000_FCRTL_XONE;
  601. fcrth = hw->fc.high_water;
  602. }
  603. wr32(E1000_FCRTL, fcrtl);
  604. wr32(E1000_FCRTH, fcrth);
  605. return ret_val;
  606. }
  607. /**
  608. * igb_set_default_fc - Set flow control default values
  609. * @hw: pointer to the HW structure
  610. *
  611. * Read the EEPROM for the default values for flow control and store the
  612. * values.
  613. **/
  614. static s32 igb_set_default_fc(struct e1000_hw *hw)
  615. {
  616. s32 ret_val = 0;
  617. u16 lan_offset;
  618. u16 nvm_data;
  619. /* Read and store word 0x0F of the EEPROM. This word contains bits
  620. * that determine the hardware's default PAUSE (flow control) mode,
  621. * a bit that determines whether the HW defaults to enabling or
  622. * disabling auto-negotiation, and the direction of the
  623. * SW defined pins. If there is no SW over-ride of the flow
  624. * control setting, then the variable hw->fc will
  625. * be initialized based on a value in the EEPROM.
  626. */
  627. if (hw->mac.type == e1000_i350) {
  628. lan_offset = NVM_82580_LAN_FUNC_OFFSET(hw->bus.func);
  629. ret_val = hw->nvm.ops.read(hw, NVM_INIT_CONTROL2_REG
  630. + lan_offset, 1, &nvm_data);
  631. } else {
  632. ret_val = hw->nvm.ops.read(hw, NVM_INIT_CONTROL2_REG,
  633. 1, &nvm_data);
  634. }
  635. if (ret_val) {
  636. hw_dbg("NVM Read Error\n");
  637. goto out;
  638. }
  639. if ((nvm_data & NVM_WORD0F_PAUSE_MASK) == 0)
  640. hw->fc.requested_mode = e1000_fc_none;
  641. else if ((nvm_data & NVM_WORD0F_PAUSE_MASK) ==
  642. NVM_WORD0F_ASM_DIR)
  643. hw->fc.requested_mode = e1000_fc_tx_pause;
  644. else
  645. hw->fc.requested_mode = e1000_fc_full;
  646. out:
  647. return ret_val;
  648. }
  649. /**
  650. * igb_force_mac_fc - Force the MAC's flow control settings
  651. * @hw: pointer to the HW structure
  652. *
  653. * Force the MAC's flow control settings. Sets the TFCE and RFCE bits in the
  654. * device control register to reflect the adapter settings. TFCE and RFCE
  655. * need to be explicitly set by software when a copper PHY is used because
  656. * autonegotiation is managed by the PHY rather than the MAC. Software must
  657. * also configure these bits when link is forced on a fiber connection.
  658. **/
  659. s32 igb_force_mac_fc(struct e1000_hw *hw)
  660. {
  661. u32 ctrl;
  662. s32 ret_val = 0;
  663. ctrl = rd32(E1000_CTRL);
  664. /* Because we didn't get link via the internal auto-negotiation
  665. * mechanism (we either forced link or we got link via PHY
  666. * auto-neg), we have to manually enable/disable transmit an
  667. * receive flow control.
  668. *
  669. * The "Case" statement below enables/disable flow control
  670. * according to the "hw->fc.current_mode" parameter.
  671. *
  672. * The possible values of the "fc" parameter are:
  673. * 0: Flow control is completely disabled
  674. * 1: Rx flow control is enabled (we can receive pause
  675. * frames but not send pause frames).
  676. * 2: Tx flow control is enabled (we can send pause frames
  677. * frames but we do not receive pause frames).
  678. * 3: Both Rx and TX flow control (symmetric) is enabled.
  679. * other: No other values should be possible at this point.
  680. */
  681. hw_dbg("hw->fc.current_mode = %u\n", hw->fc.current_mode);
  682. switch (hw->fc.current_mode) {
  683. case e1000_fc_none:
  684. ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
  685. break;
  686. case e1000_fc_rx_pause:
  687. ctrl &= (~E1000_CTRL_TFCE);
  688. ctrl |= E1000_CTRL_RFCE;
  689. break;
  690. case e1000_fc_tx_pause:
  691. ctrl &= (~E1000_CTRL_RFCE);
  692. ctrl |= E1000_CTRL_TFCE;
  693. break;
  694. case e1000_fc_full:
  695. ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
  696. break;
  697. default:
  698. hw_dbg("Flow control param set incorrectly\n");
  699. ret_val = -E1000_ERR_CONFIG;
  700. goto out;
  701. }
  702. wr32(E1000_CTRL, ctrl);
  703. out:
  704. return ret_val;
  705. }
  706. /**
  707. * igb_config_fc_after_link_up - Configures flow control after link
  708. * @hw: pointer to the HW structure
  709. *
  710. * Checks the status of auto-negotiation after link up to ensure that the
  711. * speed and duplex were not forced. If the link needed to be forced, then
  712. * flow control needs to be forced also. If auto-negotiation is enabled
  713. * and did not fail, then we configure flow control based on our link
  714. * partner.
  715. **/
  716. s32 igb_config_fc_after_link_up(struct e1000_hw *hw)
  717. {
  718. struct e1000_mac_info *mac = &hw->mac;
  719. s32 ret_val = 0;
  720. u32 pcs_status_reg, pcs_adv_reg, pcs_lp_ability_reg, pcs_ctrl_reg;
  721. u16 mii_status_reg, mii_nway_adv_reg, mii_nway_lp_ability_reg;
  722. u16 speed, duplex;
  723. /* Check for the case where we have fiber media and auto-neg failed
  724. * so we had to force link. In this case, we need to force the
  725. * configuration of the MAC to match the "fc" parameter.
  726. */
  727. if (mac->autoneg_failed) {
  728. if (hw->phy.media_type == e1000_media_type_internal_serdes)
  729. ret_val = igb_force_mac_fc(hw);
  730. } else {
  731. if (hw->phy.media_type == e1000_media_type_copper)
  732. ret_val = igb_force_mac_fc(hw);
  733. }
  734. if (ret_val) {
  735. hw_dbg("Error forcing flow control settings\n");
  736. goto out;
  737. }
  738. /* Check for the case where we have copper media and auto-neg is
  739. * enabled. In this case, we need to check and see if Auto-Neg
  740. * has completed, and if so, how the PHY and link partner has
  741. * flow control configured.
  742. */
  743. if ((hw->phy.media_type == e1000_media_type_copper) && mac->autoneg) {
  744. /* Read the MII Status Register and check to see if AutoNeg
  745. * has completed. We read this twice because this reg has
  746. * some "sticky" (latched) bits.
  747. */
  748. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS,
  749. &mii_status_reg);
  750. if (ret_val)
  751. goto out;
  752. ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS,
  753. &mii_status_reg);
  754. if (ret_val)
  755. goto out;
  756. if (!(mii_status_reg & MII_SR_AUTONEG_COMPLETE)) {
  757. hw_dbg("Copper PHY and Auto Neg "
  758. "has not completed.\n");
  759. goto out;
  760. }
  761. /* The AutoNeg process has completed, so we now need to
  762. * read both the Auto Negotiation Advertisement
  763. * Register (Address 4) and the Auto_Negotiation Base
  764. * Page Ability Register (Address 5) to determine how
  765. * flow control was negotiated.
  766. */
  767. ret_val = hw->phy.ops.read_reg(hw, PHY_AUTONEG_ADV,
  768. &mii_nway_adv_reg);
  769. if (ret_val)
  770. goto out;
  771. ret_val = hw->phy.ops.read_reg(hw, PHY_LP_ABILITY,
  772. &mii_nway_lp_ability_reg);
  773. if (ret_val)
  774. goto out;
  775. /* Two bits in the Auto Negotiation Advertisement Register
  776. * (Address 4) and two bits in the Auto Negotiation Base
  777. * Page Ability Register (Address 5) determine flow control
  778. * for both the PHY and the link partner. The following
  779. * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
  780. * 1999, describes these PAUSE resolution bits and how flow
  781. * control is determined based upon these settings.
  782. * NOTE: DC = Don't Care
  783. *
  784. * LOCAL DEVICE | LINK PARTNER
  785. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
  786. *-------|---------|-------|---------|--------------------
  787. * 0 | 0 | DC | DC | e1000_fc_none
  788. * 0 | 1 | 0 | DC | e1000_fc_none
  789. * 0 | 1 | 1 | 0 | e1000_fc_none
  790. * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
  791. * 1 | 0 | 0 | DC | e1000_fc_none
  792. * 1 | DC | 1 | DC | e1000_fc_full
  793. * 1 | 1 | 0 | 0 | e1000_fc_none
  794. * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
  795. *
  796. * Are both PAUSE bits set to 1? If so, this implies
  797. * Symmetric Flow Control is enabled at both ends. The
  798. * ASM_DIR bits are irrelevant per the spec.
  799. *
  800. * For Symmetric Flow Control:
  801. *
  802. * LOCAL DEVICE | LINK PARTNER
  803. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  804. *-------|---------|-------|---------|--------------------
  805. * 1 | DC | 1 | DC | E1000_fc_full
  806. *
  807. */
  808. if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
  809. (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
  810. /* Now we need to check if the user selected RX ONLY
  811. * of pause frames. In this case, we had to advertise
  812. * FULL flow control because we could not advertise RX
  813. * ONLY. Hence, we must now check to see if we need to
  814. * turn OFF the TRANSMISSION of PAUSE frames.
  815. */
  816. if (hw->fc.requested_mode == e1000_fc_full) {
  817. hw->fc.current_mode = e1000_fc_full;
  818. hw_dbg("Flow Control = FULL.\r\n");
  819. } else {
  820. hw->fc.current_mode = e1000_fc_rx_pause;
  821. hw_dbg("Flow Control = "
  822. "RX PAUSE frames only.\r\n");
  823. }
  824. }
  825. /* For receiving PAUSE frames ONLY.
  826. *
  827. * LOCAL DEVICE | LINK PARTNER
  828. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  829. *-------|---------|-------|---------|--------------------
  830. * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
  831. */
  832. else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
  833. (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
  834. (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
  835. (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
  836. hw->fc.current_mode = e1000_fc_tx_pause;
  837. hw_dbg("Flow Control = TX PAUSE frames only.\r\n");
  838. }
  839. /* For transmitting PAUSE frames ONLY.
  840. *
  841. * LOCAL DEVICE | LINK PARTNER
  842. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  843. *-------|---------|-------|---------|--------------------
  844. * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
  845. */
  846. else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
  847. (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
  848. !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
  849. (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
  850. hw->fc.current_mode = e1000_fc_rx_pause;
  851. hw_dbg("Flow Control = RX PAUSE frames only.\r\n");
  852. }
  853. /* Per the IEEE spec, at this point flow control should be
  854. * disabled. However, we want to consider that we could
  855. * be connected to a legacy switch that doesn't advertise
  856. * desired flow control, but can be forced on the link
  857. * partner. So if we advertised no flow control, that is
  858. * what we will resolve to. If we advertised some kind of
  859. * receive capability (Rx Pause Only or Full Flow Control)
  860. * and the link partner advertised none, we will configure
  861. * ourselves to enable Rx Flow Control only. We can do
  862. * this safely for two reasons: If the link partner really
  863. * didn't want flow control enabled, and we enable Rx, no
  864. * harm done since we won't be receiving any PAUSE frames
  865. * anyway. If the intent on the link partner was to have
  866. * flow control enabled, then by us enabling RX only, we
  867. * can at least receive pause frames and process them.
  868. * This is a good idea because in most cases, since we are
  869. * predominantly a server NIC, more times than not we will
  870. * be asked to delay transmission of packets than asking
  871. * our link partner to pause transmission of frames.
  872. */
  873. else if ((hw->fc.requested_mode == e1000_fc_none) ||
  874. (hw->fc.requested_mode == e1000_fc_tx_pause) ||
  875. (hw->fc.strict_ieee)) {
  876. hw->fc.current_mode = e1000_fc_none;
  877. hw_dbg("Flow Control = NONE.\r\n");
  878. } else {
  879. hw->fc.current_mode = e1000_fc_rx_pause;
  880. hw_dbg("Flow Control = RX PAUSE frames only.\r\n");
  881. }
  882. /* Now we need to do one last check... If we auto-
  883. * negotiated to HALF DUPLEX, flow control should not be
  884. * enabled per IEEE 802.3 spec.
  885. */
  886. ret_val = hw->mac.ops.get_speed_and_duplex(hw, &speed, &duplex);
  887. if (ret_val) {
  888. hw_dbg("Error getting link speed and duplex\n");
  889. goto out;
  890. }
  891. if (duplex == HALF_DUPLEX)
  892. hw->fc.current_mode = e1000_fc_none;
  893. /* Now we call a subroutine to actually force the MAC
  894. * controller to use the correct flow control settings.
  895. */
  896. ret_val = igb_force_mac_fc(hw);
  897. if (ret_val) {
  898. hw_dbg("Error forcing flow control settings\n");
  899. goto out;
  900. }
  901. }
  902. /* Check for the case where we have SerDes media and auto-neg is
  903. * enabled. In this case, we need to check and see if Auto-Neg
  904. * has completed, and if so, how the PHY and link partner has
  905. * flow control configured.
  906. */
  907. if ((hw->phy.media_type == e1000_media_type_internal_serdes)
  908. && mac->autoneg) {
  909. /* Read the PCS_LSTS and check to see if AutoNeg
  910. * has completed.
  911. */
  912. pcs_status_reg = rd32(E1000_PCS_LSTAT);
  913. if (!(pcs_status_reg & E1000_PCS_LSTS_AN_COMPLETE)) {
  914. hw_dbg("PCS Auto Neg has not completed.\n");
  915. return ret_val;
  916. }
  917. /* The AutoNeg process has completed, so we now need to
  918. * read both the Auto Negotiation Advertisement
  919. * Register (PCS_ANADV) and the Auto_Negotiation Base
  920. * Page Ability Register (PCS_LPAB) to determine how
  921. * flow control was negotiated.
  922. */
  923. pcs_adv_reg = rd32(E1000_PCS_ANADV);
  924. pcs_lp_ability_reg = rd32(E1000_PCS_LPAB);
  925. /* Two bits in the Auto Negotiation Advertisement Register
  926. * (PCS_ANADV) and two bits in the Auto Negotiation Base
  927. * Page Ability Register (PCS_LPAB) determine flow control
  928. * for both the PHY and the link partner. The following
  929. * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
  930. * 1999, describes these PAUSE resolution bits and how flow
  931. * control is determined based upon these settings.
  932. * NOTE: DC = Don't Care
  933. *
  934. * LOCAL DEVICE | LINK PARTNER
  935. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
  936. *-------|---------|-------|---------|--------------------
  937. * 0 | 0 | DC | DC | e1000_fc_none
  938. * 0 | 1 | 0 | DC | e1000_fc_none
  939. * 0 | 1 | 1 | 0 | e1000_fc_none
  940. * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
  941. * 1 | 0 | 0 | DC | e1000_fc_none
  942. * 1 | DC | 1 | DC | e1000_fc_full
  943. * 1 | 1 | 0 | 0 | e1000_fc_none
  944. * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
  945. *
  946. * Are both PAUSE bits set to 1? If so, this implies
  947. * Symmetric Flow Control is enabled at both ends. The
  948. * ASM_DIR bits are irrelevant per the spec.
  949. *
  950. * For Symmetric Flow Control:
  951. *
  952. * LOCAL DEVICE | LINK PARTNER
  953. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  954. *-------|---------|-------|---------|--------------------
  955. * 1 | DC | 1 | DC | e1000_fc_full
  956. *
  957. */
  958. if ((pcs_adv_reg & E1000_TXCW_PAUSE) &&
  959. (pcs_lp_ability_reg & E1000_TXCW_PAUSE)) {
  960. /* Now we need to check if the user selected Rx ONLY
  961. * of pause frames. In this case, we had to advertise
  962. * FULL flow control because we could not advertise Rx
  963. * ONLY. Hence, we must now check to see if we need to
  964. * turn OFF the TRANSMISSION of PAUSE frames.
  965. */
  966. if (hw->fc.requested_mode == e1000_fc_full) {
  967. hw->fc.current_mode = e1000_fc_full;
  968. hw_dbg("Flow Control = FULL.\n");
  969. } else {
  970. hw->fc.current_mode = e1000_fc_rx_pause;
  971. hw_dbg("Flow Control = Rx PAUSE frames only.\n");
  972. }
  973. }
  974. /* For receiving PAUSE frames ONLY.
  975. *
  976. * LOCAL DEVICE | LINK PARTNER
  977. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  978. *-------|---------|-------|---------|--------------------
  979. * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
  980. */
  981. else if (!(pcs_adv_reg & E1000_TXCW_PAUSE) &&
  982. (pcs_adv_reg & E1000_TXCW_ASM_DIR) &&
  983. (pcs_lp_ability_reg & E1000_TXCW_PAUSE) &&
  984. (pcs_lp_ability_reg & E1000_TXCW_ASM_DIR)) {
  985. hw->fc.current_mode = e1000_fc_tx_pause;
  986. hw_dbg("Flow Control = Tx PAUSE frames only.\n");
  987. }
  988. /* For transmitting PAUSE frames ONLY.
  989. *
  990. * LOCAL DEVICE | LINK PARTNER
  991. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  992. *-------|---------|-------|---------|--------------------
  993. * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
  994. */
  995. else if ((pcs_adv_reg & E1000_TXCW_PAUSE) &&
  996. (pcs_adv_reg & E1000_TXCW_ASM_DIR) &&
  997. !(pcs_lp_ability_reg & E1000_TXCW_PAUSE) &&
  998. (pcs_lp_ability_reg & E1000_TXCW_ASM_DIR)) {
  999. hw->fc.current_mode = e1000_fc_rx_pause;
  1000. hw_dbg("Flow Control = Rx PAUSE frames only.\n");
  1001. } else {
  1002. /* Per the IEEE spec, at this point flow control
  1003. * should be disabled.
  1004. */
  1005. hw->fc.current_mode = e1000_fc_none;
  1006. hw_dbg("Flow Control = NONE.\n");
  1007. }
  1008. /* Now we call a subroutine to actually force the MAC
  1009. * controller to use the correct flow control settings.
  1010. */
  1011. pcs_ctrl_reg = rd32(E1000_PCS_LCTL);
  1012. pcs_ctrl_reg |= E1000_PCS_LCTL_FORCE_FCTRL;
  1013. wr32(E1000_PCS_LCTL, pcs_ctrl_reg);
  1014. ret_val = igb_force_mac_fc(hw);
  1015. if (ret_val) {
  1016. hw_dbg("Error forcing flow control settings\n");
  1017. return ret_val;
  1018. }
  1019. }
  1020. out:
  1021. return ret_val;
  1022. }
  1023. /**
  1024. * igb_get_speed_and_duplex_copper - Retrieve current speed/duplex
  1025. * @hw: pointer to the HW structure
  1026. * @speed: stores the current speed
  1027. * @duplex: stores the current duplex
  1028. *
  1029. * Read the status register for the current speed/duplex and store the current
  1030. * speed and duplex for copper connections.
  1031. **/
  1032. s32 igb_get_speed_and_duplex_copper(struct e1000_hw *hw, u16 *speed,
  1033. u16 *duplex)
  1034. {
  1035. u32 status;
  1036. status = rd32(E1000_STATUS);
  1037. if (status & E1000_STATUS_SPEED_1000) {
  1038. *speed = SPEED_1000;
  1039. hw_dbg("1000 Mbs, ");
  1040. } else if (status & E1000_STATUS_SPEED_100) {
  1041. *speed = SPEED_100;
  1042. hw_dbg("100 Mbs, ");
  1043. } else {
  1044. *speed = SPEED_10;
  1045. hw_dbg("10 Mbs, ");
  1046. }
  1047. if (status & E1000_STATUS_FD) {
  1048. *duplex = FULL_DUPLEX;
  1049. hw_dbg("Full Duplex\n");
  1050. } else {
  1051. *duplex = HALF_DUPLEX;
  1052. hw_dbg("Half Duplex\n");
  1053. }
  1054. return 0;
  1055. }
  1056. /**
  1057. * igb_get_hw_semaphore - Acquire hardware semaphore
  1058. * @hw: pointer to the HW structure
  1059. *
  1060. * Acquire the HW semaphore to access the PHY or NVM
  1061. **/
  1062. s32 igb_get_hw_semaphore(struct e1000_hw *hw)
  1063. {
  1064. u32 swsm;
  1065. s32 ret_val = 0;
  1066. s32 timeout = hw->nvm.word_size + 1;
  1067. s32 i = 0;
  1068. /* Get the SW semaphore */
  1069. while (i < timeout) {
  1070. swsm = rd32(E1000_SWSM);
  1071. if (!(swsm & E1000_SWSM_SMBI))
  1072. break;
  1073. udelay(50);
  1074. i++;
  1075. }
  1076. if (i == timeout) {
  1077. hw_dbg("Driver can't access device - SMBI bit is set.\n");
  1078. ret_val = -E1000_ERR_NVM;
  1079. goto out;
  1080. }
  1081. /* Get the FW semaphore. */
  1082. for (i = 0; i < timeout; i++) {
  1083. swsm = rd32(E1000_SWSM);
  1084. wr32(E1000_SWSM, swsm | E1000_SWSM_SWESMBI);
  1085. /* Semaphore acquired if bit latched */
  1086. if (rd32(E1000_SWSM) & E1000_SWSM_SWESMBI)
  1087. break;
  1088. udelay(50);
  1089. }
  1090. if (i == timeout) {
  1091. /* Release semaphores */
  1092. igb_put_hw_semaphore(hw);
  1093. hw_dbg("Driver can't access the NVM\n");
  1094. ret_val = -E1000_ERR_NVM;
  1095. goto out;
  1096. }
  1097. out:
  1098. return ret_val;
  1099. }
  1100. /**
  1101. * igb_put_hw_semaphore - Release hardware semaphore
  1102. * @hw: pointer to the HW structure
  1103. *
  1104. * Release hardware semaphore used to access the PHY or NVM
  1105. **/
  1106. void igb_put_hw_semaphore(struct e1000_hw *hw)
  1107. {
  1108. u32 swsm;
  1109. swsm = rd32(E1000_SWSM);
  1110. swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
  1111. wr32(E1000_SWSM, swsm);
  1112. }
  1113. /**
  1114. * igb_get_auto_rd_done - Check for auto read completion
  1115. * @hw: pointer to the HW structure
  1116. *
  1117. * Check EEPROM for Auto Read done bit.
  1118. **/
  1119. s32 igb_get_auto_rd_done(struct e1000_hw *hw)
  1120. {
  1121. s32 i = 0;
  1122. s32 ret_val = 0;
  1123. while (i < AUTO_READ_DONE_TIMEOUT) {
  1124. if (rd32(E1000_EECD) & E1000_EECD_AUTO_RD)
  1125. break;
  1126. msleep(1);
  1127. i++;
  1128. }
  1129. if (i == AUTO_READ_DONE_TIMEOUT) {
  1130. hw_dbg("Auto read by HW from NVM has not completed.\n");
  1131. ret_val = -E1000_ERR_RESET;
  1132. goto out;
  1133. }
  1134. out:
  1135. return ret_val;
  1136. }
  1137. /**
  1138. * igb_valid_led_default - Verify a valid default LED config
  1139. * @hw: pointer to the HW structure
  1140. * @data: pointer to the NVM (EEPROM)
  1141. *
  1142. * Read the EEPROM for the current default LED configuration. If the
  1143. * LED configuration is not valid, set to a valid LED configuration.
  1144. **/
  1145. static s32 igb_valid_led_default(struct e1000_hw *hw, u16 *data)
  1146. {
  1147. s32 ret_val;
  1148. ret_val = hw->nvm.ops.read(hw, NVM_ID_LED_SETTINGS, 1, data);
  1149. if (ret_val) {
  1150. hw_dbg("NVM Read Error\n");
  1151. goto out;
  1152. }
  1153. if (*data == ID_LED_RESERVED_0000 || *data == ID_LED_RESERVED_FFFF) {
  1154. switch(hw->phy.media_type) {
  1155. case e1000_media_type_internal_serdes:
  1156. *data = ID_LED_DEFAULT_82575_SERDES;
  1157. break;
  1158. case e1000_media_type_copper:
  1159. default:
  1160. *data = ID_LED_DEFAULT;
  1161. break;
  1162. }
  1163. }
  1164. out:
  1165. return ret_val;
  1166. }
  1167. /**
  1168. * igb_id_led_init -
  1169. * @hw: pointer to the HW structure
  1170. *
  1171. **/
  1172. s32 igb_id_led_init(struct e1000_hw *hw)
  1173. {
  1174. struct e1000_mac_info *mac = &hw->mac;
  1175. s32 ret_val;
  1176. const u32 ledctl_mask = 0x000000FF;
  1177. const u32 ledctl_on = E1000_LEDCTL_MODE_LED_ON;
  1178. const u32 ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
  1179. u16 data, i, temp;
  1180. const u16 led_mask = 0x0F;
  1181. /* i210 and i211 devices have different LED mechanism */
  1182. if ((hw->mac.type == e1000_i210) ||
  1183. (hw->mac.type == e1000_i211))
  1184. ret_val = igb_valid_led_default_i210(hw, &data);
  1185. else
  1186. ret_val = igb_valid_led_default(hw, &data);
  1187. if (ret_val)
  1188. goto out;
  1189. mac->ledctl_default = rd32(E1000_LEDCTL);
  1190. mac->ledctl_mode1 = mac->ledctl_default;
  1191. mac->ledctl_mode2 = mac->ledctl_default;
  1192. for (i = 0; i < 4; i++) {
  1193. temp = (data >> (i << 2)) & led_mask;
  1194. switch (temp) {
  1195. case ID_LED_ON1_DEF2:
  1196. case ID_LED_ON1_ON2:
  1197. case ID_LED_ON1_OFF2:
  1198. mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
  1199. mac->ledctl_mode1 |= ledctl_on << (i << 3);
  1200. break;
  1201. case ID_LED_OFF1_DEF2:
  1202. case ID_LED_OFF1_ON2:
  1203. case ID_LED_OFF1_OFF2:
  1204. mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
  1205. mac->ledctl_mode1 |= ledctl_off << (i << 3);
  1206. break;
  1207. default:
  1208. /* Do nothing */
  1209. break;
  1210. }
  1211. switch (temp) {
  1212. case ID_LED_DEF1_ON2:
  1213. case ID_LED_ON1_ON2:
  1214. case ID_LED_OFF1_ON2:
  1215. mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
  1216. mac->ledctl_mode2 |= ledctl_on << (i << 3);
  1217. break;
  1218. case ID_LED_DEF1_OFF2:
  1219. case ID_LED_ON1_OFF2:
  1220. case ID_LED_OFF1_OFF2:
  1221. mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
  1222. mac->ledctl_mode2 |= ledctl_off << (i << 3);
  1223. break;
  1224. default:
  1225. /* Do nothing */
  1226. break;
  1227. }
  1228. }
  1229. out:
  1230. return ret_val;
  1231. }
  1232. /**
  1233. * igb_cleanup_led - Set LED config to default operation
  1234. * @hw: pointer to the HW structure
  1235. *
  1236. * Remove the current LED configuration and set the LED configuration
  1237. * to the default value, saved from the EEPROM.
  1238. **/
  1239. s32 igb_cleanup_led(struct e1000_hw *hw)
  1240. {
  1241. wr32(E1000_LEDCTL, hw->mac.ledctl_default);
  1242. return 0;
  1243. }
  1244. /**
  1245. * igb_blink_led - Blink LED
  1246. * @hw: pointer to the HW structure
  1247. *
  1248. * Blink the led's which are set to be on.
  1249. **/
  1250. s32 igb_blink_led(struct e1000_hw *hw)
  1251. {
  1252. u32 ledctl_blink = 0;
  1253. u32 i;
  1254. if (hw->phy.media_type == e1000_media_type_fiber) {
  1255. /* always blink LED0 for PCI-E fiber */
  1256. ledctl_blink = E1000_LEDCTL_LED0_BLINK |
  1257. (E1000_LEDCTL_MODE_LED_ON << E1000_LEDCTL_LED0_MODE_SHIFT);
  1258. } else {
  1259. /* Set the blink bit for each LED that's "on" (0x0E)
  1260. * (or "off" if inverted) in ledctl_mode2. The blink
  1261. * logic in hardware only works when mode is set to "on"
  1262. * so it must be changed accordingly when the mode is
  1263. * "off" and inverted.
  1264. */
  1265. ledctl_blink = hw->mac.ledctl_mode2;
  1266. for (i = 0; i < 32; i += 8) {
  1267. u32 mode = (hw->mac.ledctl_mode2 >> i) &
  1268. E1000_LEDCTL_LED0_MODE_MASK;
  1269. u32 led_default = hw->mac.ledctl_default >> i;
  1270. if ((!(led_default & E1000_LEDCTL_LED0_IVRT) &&
  1271. (mode == E1000_LEDCTL_MODE_LED_ON)) ||
  1272. ((led_default & E1000_LEDCTL_LED0_IVRT) &&
  1273. (mode == E1000_LEDCTL_MODE_LED_OFF))) {
  1274. ledctl_blink &=
  1275. ~(E1000_LEDCTL_LED0_MODE_MASK << i);
  1276. ledctl_blink |= (E1000_LEDCTL_LED0_BLINK |
  1277. E1000_LEDCTL_MODE_LED_ON) << i;
  1278. }
  1279. }
  1280. }
  1281. wr32(E1000_LEDCTL, ledctl_blink);
  1282. return 0;
  1283. }
  1284. /**
  1285. * igb_led_off - Turn LED off
  1286. * @hw: pointer to the HW structure
  1287. *
  1288. * Turn LED off.
  1289. **/
  1290. s32 igb_led_off(struct e1000_hw *hw)
  1291. {
  1292. switch (hw->phy.media_type) {
  1293. case e1000_media_type_copper:
  1294. wr32(E1000_LEDCTL, hw->mac.ledctl_mode1);
  1295. break;
  1296. default:
  1297. break;
  1298. }
  1299. return 0;
  1300. }
  1301. /**
  1302. * igb_disable_pcie_master - Disables PCI-express master access
  1303. * @hw: pointer to the HW structure
  1304. *
  1305. * Returns 0 (0) if successful, else returns -10
  1306. * (-E1000_ERR_MASTER_REQUESTS_PENDING) if master disable bit has not caused
  1307. * the master requests to be disabled.
  1308. *
  1309. * Disables PCI-Express master access and verifies there are no pending
  1310. * requests.
  1311. **/
  1312. s32 igb_disable_pcie_master(struct e1000_hw *hw)
  1313. {
  1314. u32 ctrl;
  1315. s32 timeout = MASTER_DISABLE_TIMEOUT;
  1316. s32 ret_val = 0;
  1317. if (hw->bus.type != e1000_bus_type_pci_express)
  1318. goto out;
  1319. ctrl = rd32(E1000_CTRL);
  1320. ctrl |= E1000_CTRL_GIO_MASTER_DISABLE;
  1321. wr32(E1000_CTRL, ctrl);
  1322. while (timeout) {
  1323. if (!(rd32(E1000_STATUS) &
  1324. E1000_STATUS_GIO_MASTER_ENABLE))
  1325. break;
  1326. udelay(100);
  1327. timeout--;
  1328. }
  1329. if (!timeout) {
  1330. hw_dbg("Master requests are pending.\n");
  1331. ret_val = -E1000_ERR_MASTER_REQUESTS_PENDING;
  1332. goto out;
  1333. }
  1334. out:
  1335. return ret_val;
  1336. }
  1337. /**
  1338. * igb_validate_mdi_setting - Verify MDI/MDIx settings
  1339. * @hw: pointer to the HW structure
  1340. *
  1341. * Verify that when not using auto-negotitation that MDI/MDIx is correctly
  1342. * set, which is forced to MDI mode only.
  1343. **/
  1344. s32 igb_validate_mdi_setting(struct e1000_hw *hw)
  1345. {
  1346. s32 ret_val = 0;
  1347. /* All MDI settings are supported on 82580 and newer. */
  1348. if (hw->mac.type >= e1000_82580)
  1349. goto out;
  1350. if (!hw->mac.autoneg && (hw->phy.mdix == 0 || hw->phy.mdix == 3)) {
  1351. hw_dbg("Invalid MDI setting detected\n");
  1352. hw->phy.mdix = 1;
  1353. ret_val = -E1000_ERR_CONFIG;
  1354. goto out;
  1355. }
  1356. out:
  1357. return ret_val;
  1358. }
  1359. /**
  1360. * igb_write_8bit_ctrl_reg - Write a 8bit CTRL register
  1361. * @hw: pointer to the HW structure
  1362. * @reg: 32bit register offset such as E1000_SCTL
  1363. * @offset: register offset to write to
  1364. * @data: data to write at register offset
  1365. *
  1366. * Writes an address/data control type register. There are several of these
  1367. * and they all have the format address << 8 | data and bit 31 is polled for
  1368. * completion.
  1369. **/
  1370. s32 igb_write_8bit_ctrl_reg(struct e1000_hw *hw, u32 reg,
  1371. u32 offset, u8 data)
  1372. {
  1373. u32 i, regvalue = 0;
  1374. s32 ret_val = 0;
  1375. /* Set up the address and data */
  1376. regvalue = ((u32)data) | (offset << E1000_GEN_CTL_ADDRESS_SHIFT);
  1377. wr32(reg, regvalue);
  1378. /* Poll the ready bit to see if the MDI read completed */
  1379. for (i = 0; i < E1000_GEN_POLL_TIMEOUT; i++) {
  1380. udelay(5);
  1381. regvalue = rd32(reg);
  1382. if (regvalue & E1000_GEN_CTL_READY)
  1383. break;
  1384. }
  1385. if (!(regvalue & E1000_GEN_CTL_READY)) {
  1386. hw_dbg("Reg %08x did not indicate ready\n", reg);
  1387. ret_val = -E1000_ERR_PHY;
  1388. goto out;
  1389. }
  1390. out:
  1391. return ret_val;
  1392. }
  1393. /**
  1394. * igb_enable_mng_pass_thru - Enable processing of ARP's
  1395. * @hw: pointer to the HW structure
  1396. *
  1397. * Verifies the hardware needs to leave interface enabled so that frames can
  1398. * be directed to and from the management interface.
  1399. **/
  1400. bool igb_enable_mng_pass_thru(struct e1000_hw *hw)
  1401. {
  1402. u32 manc;
  1403. u32 fwsm, factps;
  1404. bool ret_val = false;
  1405. if (!hw->mac.asf_firmware_present)
  1406. goto out;
  1407. manc = rd32(E1000_MANC);
  1408. if (!(manc & E1000_MANC_RCV_TCO_EN))
  1409. goto out;
  1410. if (hw->mac.arc_subsystem_valid) {
  1411. fwsm = rd32(E1000_FWSM);
  1412. factps = rd32(E1000_FACTPS);
  1413. if (!(factps & E1000_FACTPS_MNGCG) &&
  1414. ((fwsm & E1000_FWSM_MODE_MASK) ==
  1415. (e1000_mng_mode_pt << E1000_FWSM_MODE_SHIFT))) {
  1416. ret_val = true;
  1417. goto out;
  1418. }
  1419. } else {
  1420. if ((manc & E1000_MANC_SMBUS_EN) &&
  1421. !(manc & E1000_MANC_ASF_EN)) {
  1422. ret_val = true;
  1423. goto out;
  1424. }
  1425. }
  1426. out:
  1427. return ret_val;
  1428. }