e1000_mac.c 45 KB

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