mac.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  1. /*******************************************************************************
  2. Intel PRO/1000 Linux driver
  3. Copyright(c) 1999 - 2012 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. Linux NICS <linux.nics@intel.com>
  18. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  19. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  20. *******************************************************************************/
  21. #include "e1000.h"
  22. /**
  23. * e1000e_get_bus_info_pcie - Get PCIe bus information
  24. * @hw: pointer to the HW structure
  25. *
  26. * Determines and stores the system bus information for a particular
  27. * network interface. The following bus information is determined and stored:
  28. * bus speed, bus width, type (PCIe), and PCIe function.
  29. **/
  30. s32 e1000e_get_bus_info_pcie(struct e1000_hw *hw)
  31. {
  32. struct e1000_mac_info *mac = &hw->mac;
  33. struct e1000_bus_info *bus = &hw->bus;
  34. struct e1000_adapter *adapter = hw->adapter;
  35. u16 pcie_link_status, cap_offset;
  36. cap_offset = adapter->pdev->pcie_cap;
  37. if (!cap_offset) {
  38. bus->width = e1000_bus_width_unknown;
  39. } else {
  40. pci_read_config_word(adapter->pdev,
  41. cap_offset + PCIE_LINK_STATUS,
  42. &pcie_link_status);
  43. bus->width = (enum e1000_bus_width)((pcie_link_status &
  44. PCIE_LINK_WIDTH_MASK) >>
  45. PCIE_LINK_WIDTH_SHIFT);
  46. }
  47. mac->ops.set_lan_id(hw);
  48. return 0;
  49. }
  50. /**
  51. * e1000_set_lan_id_multi_port_pcie - Set LAN id for PCIe multiple port devices
  52. *
  53. * @hw: pointer to the HW structure
  54. *
  55. * Determines the LAN function id by reading memory-mapped registers
  56. * and swaps the port value if requested.
  57. **/
  58. void e1000_set_lan_id_multi_port_pcie(struct e1000_hw *hw)
  59. {
  60. struct e1000_bus_info *bus = &hw->bus;
  61. u32 reg;
  62. /*
  63. * The status register reports the correct function number
  64. * for the device regardless of function swap state.
  65. */
  66. reg = er32(STATUS);
  67. bus->func = (reg & E1000_STATUS_FUNC_MASK) >> E1000_STATUS_FUNC_SHIFT;
  68. }
  69. /**
  70. * e1000_set_lan_id_single_port - Set LAN id for a single port device
  71. * @hw: pointer to the HW structure
  72. *
  73. * Sets the LAN function id to zero for a single port device.
  74. **/
  75. void e1000_set_lan_id_single_port(struct e1000_hw *hw)
  76. {
  77. struct e1000_bus_info *bus = &hw->bus;
  78. bus->func = 0;
  79. }
  80. /**
  81. * e1000_clear_vfta_generic - Clear VLAN filter table
  82. * @hw: pointer to the HW structure
  83. *
  84. * Clears the register array which contains the VLAN filter table by
  85. * setting all the values to 0.
  86. **/
  87. void e1000_clear_vfta_generic(struct e1000_hw *hw)
  88. {
  89. u32 offset;
  90. for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
  91. E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, offset, 0);
  92. e1e_flush();
  93. }
  94. }
  95. /**
  96. * e1000_write_vfta_generic - Write value to VLAN filter table
  97. * @hw: pointer to the HW structure
  98. * @offset: register offset in VLAN filter table
  99. * @value: register value written to VLAN filter table
  100. *
  101. * Writes value at the given offset in the register array which stores
  102. * the VLAN filter table.
  103. **/
  104. void e1000_write_vfta_generic(struct e1000_hw *hw, u32 offset, u32 value)
  105. {
  106. E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, offset, value);
  107. e1e_flush();
  108. }
  109. /**
  110. * e1000e_init_rx_addrs - Initialize receive address's
  111. * @hw: pointer to the HW structure
  112. * @rar_count: receive address registers
  113. *
  114. * Setup the receive address registers by setting the base receive address
  115. * register to the devices MAC address and clearing all the other receive
  116. * address registers to 0.
  117. **/
  118. void e1000e_init_rx_addrs(struct e1000_hw *hw, u16 rar_count)
  119. {
  120. u32 i;
  121. u8 mac_addr[ETH_ALEN] = { 0 };
  122. /* Setup the receive address */
  123. e_dbg("Programming MAC Address into RAR[0]\n");
  124. hw->mac.ops.rar_set(hw, hw->mac.addr, 0);
  125. /* Zero out the other (rar_entry_count - 1) receive addresses */
  126. e_dbg("Clearing RAR[1-%u]\n", rar_count - 1);
  127. for (i = 1; i < rar_count; i++)
  128. hw->mac.ops.rar_set(hw, mac_addr, i);
  129. }
  130. /**
  131. * e1000_check_alt_mac_addr_generic - Check for alternate MAC addr
  132. * @hw: pointer to the HW structure
  133. *
  134. * Checks the nvm for an alternate MAC address. An alternate MAC address
  135. * can be setup by pre-boot software and must be treated like a permanent
  136. * address and must override the actual permanent MAC address. If an
  137. * alternate MAC address is found it is programmed into RAR0, replacing
  138. * the permanent address that was installed into RAR0 by the Si on reset.
  139. * This function will return SUCCESS unless it encounters an error while
  140. * reading the EEPROM.
  141. **/
  142. s32 e1000_check_alt_mac_addr_generic(struct e1000_hw *hw)
  143. {
  144. u32 i;
  145. s32 ret_val = 0;
  146. u16 offset, nvm_alt_mac_addr_offset, nvm_data;
  147. u8 alt_mac_addr[ETH_ALEN];
  148. ret_val = e1000_read_nvm(hw, NVM_COMPAT, 1, &nvm_data);
  149. if (ret_val)
  150. return ret_val;
  151. /* not supported on 82573 */
  152. if (hw->mac.type == e1000_82573)
  153. return 0;
  154. ret_val = e1000_read_nvm(hw, NVM_ALT_MAC_ADDR_PTR, 1,
  155. &nvm_alt_mac_addr_offset);
  156. if (ret_val) {
  157. e_dbg("NVM Read Error\n");
  158. return ret_val;
  159. }
  160. if ((nvm_alt_mac_addr_offset == 0xFFFF) ||
  161. (nvm_alt_mac_addr_offset == 0x0000))
  162. /* There is no Alternate MAC Address */
  163. return 0;
  164. if (hw->bus.func == E1000_FUNC_1)
  165. nvm_alt_mac_addr_offset += E1000_ALT_MAC_ADDRESS_OFFSET_LAN1;
  166. for (i = 0; i < ETH_ALEN; i += 2) {
  167. offset = nvm_alt_mac_addr_offset + (i >> 1);
  168. ret_val = e1000_read_nvm(hw, offset, 1, &nvm_data);
  169. if (ret_val) {
  170. e_dbg("NVM Read Error\n");
  171. return ret_val;
  172. }
  173. alt_mac_addr[i] = (u8)(nvm_data & 0xFF);
  174. alt_mac_addr[i + 1] = (u8)(nvm_data >> 8);
  175. }
  176. /* if multicast bit is set, the alternate address will not be used */
  177. if (is_multicast_ether_addr(alt_mac_addr)) {
  178. e_dbg("Ignoring Alternate Mac Address with MC bit set\n");
  179. return 0;
  180. }
  181. /*
  182. * We have a valid alternate MAC address, and we want to treat it the
  183. * same as the normal permanent MAC address stored by the HW into the
  184. * RAR. Do this by mapping this address into RAR0.
  185. */
  186. hw->mac.ops.rar_set(hw, alt_mac_addr, 0);
  187. return 0;
  188. }
  189. /**
  190. * e1000e_rar_set_generic - Set receive address register
  191. * @hw: pointer to the HW structure
  192. * @addr: pointer to the receive address
  193. * @index: receive address array register
  194. *
  195. * Sets the receive address array register at index to the address passed
  196. * in by addr.
  197. **/
  198. void e1000e_rar_set_generic(struct e1000_hw *hw, u8 *addr, u32 index)
  199. {
  200. u32 rar_low, rar_high;
  201. /*
  202. * HW expects these in little endian so we reverse the byte order
  203. * from network order (big endian) to little endian
  204. */
  205. rar_low = ((u32)addr[0] | ((u32)addr[1] << 8) |
  206. ((u32)addr[2] << 16) | ((u32)addr[3] << 24));
  207. rar_high = ((u32)addr[4] | ((u32)addr[5] << 8));
  208. /* If MAC address zero, no need to set the AV bit */
  209. if (rar_low || rar_high)
  210. rar_high |= E1000_RAH_AV;
  211. /*
  212. * Some bridges will combine consecutive 32-bit writes into
  213. * a single burst write, which will malfunction on some parts.
  214. * The flushes avoid this.
  215. */
  216. ew32(RAL(index), rar_low);
  217. e1e_flush();
  218. ew32(RAH(index), rar_high);
  219. e1e_flush();
  220. }
  221. /**
  222. * e1000_hash_mc_addr - Generate a multicast hash value
  223. * @hw: pointer to the HW structure
  224. * @mc_addr: pointer to a multicast address
  225. *
  226. * Generates a multicast address hash value which is used to determine
  227. * the multicast filter table array address and new table value.
  228. **/
  229. static u32 e1000_hash_mc_addr(struct e1000_hw *hw, u8 *mc_addr)
  230. {
  231. u32 hash_value, hash_mask;
  232. u8 bit_shift = 0;
  233. /* Register count multiplied by bits per register */
  234. hash_mask = (hw->mac.mta_reg_count * 32) - 1;
  235. /*
  236. * For a mc_filter_type of 0, bit_shift is the number of left-shifts
  237. * where 0xFF would still fall within the hash mask.
  238. */
  239. while (hash_mask >> bit_shift != 0xFF)
  240. bit_shift++;
  241. /*
  242. * The portion of the address that is used for the hash table
  243. * is determined by the mc_filter_type setting.
  244. * The algorithm is such that there is a total of 8 bits of shifting.
  245. * The bit_shift for a mc_filter_type of 0 represents the number of
  246. * left-shifts where the MSB of mc_addr[5] would still fall within
  247. * the hash_mask. Case 0 does this exactly. Since there are a total
  248. * of 8 bits of shifting, then mc_addr[4] will shift right the
  249. * remaining number of bits. Thus 8 - bit_shift. The rest of the
  250. * cases are a variation of this algorithm...essentially raising the
  251. * number of bits to shift mc_addr[5] left, while still keeping the
  252. * 8-bit shifting total.
  253. *
  254. * For example, given the following Destination MAC Address and an
  255. * mta register count of 128 (thus a 4096-bit vector and 0xFFF mask),
  256. * we can see that the bit_shift for case 0 is 4. These are the hash
  257. * values resulting from each mc_filter_type...
  258. * [0] [1] [2] [3] [4] [5]
  259. * 01 AA 00 12 34 56
  260. * LSB MSB
  261. *
  262. * case 0: hash_value = ((0x34 >> 4) | (0x56 << 4)) & 0xFFF = 0x563
  263. * case 1: hash_value = ((0x34 >> 3) | (0x56 << 5)) & 0xFFF = 0xAC6
  264. * case 2: hash_value = ((0x34 >> 2) | (0x56 << 6)) & 0xFFF = 0x163
  265. * case 3: hash_value = ((0x34 >> 0) | (0x56 << 8)) & 0xFFF = 0x634
  266. */
  267. switch (hw->mac.mc_filter_type) {
  268. default:
  269. case 0:
  270. break;
  271. case 1:
  272. bit_shift += 1;
  273. break;
  274. case 2:
  275. bit_shift += 2;
  276. break;
  277. case 3:
  278. bit_shift += 4;
  279. break;
  280. }
  281. hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
  282. (((u16)mc_addr[5]) << bit_shift)));
  283. return hash_value;
  284. }
  285. /**
  286. * e1000e_update_mc_addr_list_generic - Update Multicast addresses
  287. * @hw: pointer to the HW structure
  288. * @mc_addr_list: array of multicast addresses to program
  289. * @mc_addr_count: number of multicast addresses to program
  290. *
  291. * Updates entire Multicast Table Array.
  292. * The caller must have a packed mc_addr_list of multicast addresses.
  293. **/
  294. void e1000e_update_mc_addr_list_generic(struct e1000_hw *hw,
  295. u8 *mc_addr_list, u32 mc_addr_count)
  296. {
  297. u32 hash_value, hash_bit, hash_reg;
  298. int i;
  299. /* clear mta_shadow */
  300. memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
  301. /* update mta_shadow from mc_addr_list */
  302. for (i = 0; (u32)i < mc_addr_count; i++) {
  303. hash_value = e1000_hash_mc_addr(hw, mc_addr_list);
  304. hash_reg = (hash_value >> 5) & (hw->mac.mta_reg_count - 1);
  305. hash_bit = hash_value & 0x1F;
  306. hw->mac.mta_shadow[hash_reg] |= (1 << hash_bit);
  307. mc_addr_list += (ETH_ALEN);
  308. }
  309. /* replace the entire MTA table */
  310. for (i = hw->mac.mta_reg_count - 1; i >= 0; i--)
  311. E1000_WRITE_REG_ARRAY(hw, E1000_MTA, i, hw->mac.mta_shadow[i]);
  312. e1e_flush();
  313. }
  314. /**
  315. * e1000e_clear_hw_cntrs_base - Clear base hardware counters
  316. * @hw: pointer to the HW structure
  317. *
  318. * Clears the base hardware counters by reading the counter registers.
  319. **/
  320. void e1000e_clear_hw_cntrs_base(struct e1000_hw *hw)
  321. {
  322. er32(CRCERRS);
  323. er32(SYMERRS);
  324. er32(MPC);
  325. er32(SCC);
  326. er32(ECOL);
  327. er32(MCC);
  328. er32(LATECOL);
  329. er32(COLC);
  330. er32(DC);
  331. er32(SEC);
  332. er32(RLEC);
  333. er32(XONRXC);
  334. er32(XONTXC);
  335. er32(XOFFRXC);
  336. er32(XOFFTXC);
  337. er32(FCRUC);
  338. er32(GPRC);
  339. er32(BPRC);
  340. er32(MPRC);
  341. er32(GPTC);
  342. er32(GORCL);
  343. er32(GORCH);
  344. er32(GOTCL);
  345. er32(GOTCH);
  346. er32(RNBC);
  347. er32(RUC);
  348. er32(RFC);
  349. er32(ROC);
  350. er32(RJC);
  351. er32(TORL);
  352. er32(TORH);
  353. er32(TOTL);
  354. er32(TOTH);
  355. er32(TPR);
  356. er32(TPT);
  357. er32(MPTC);
  358. er32(BPTC);
  359. }
  360. /**
  361. * e1000e_check_for_copper_link - Check for link (Copper)
  362. * @hw: pointer to the HW structure
  363. *
  364. * Checks to see of the link status of the hardware has changed. If a
  365. * change in link status has been detected, then we read the PHY registers
  366. * to get the current speed/duplex if link exists.
  367. **/
  368. s32 e1000e_check_for_copper_link(struct e1000_hw *hw)
  369. {
  370. struct e1000_mac_info *mac = &hw->mac;
  371. s32 ret_val;
  372. bool link;
  373. /*
  374. * We only want to go out to the PHY registers to see if Auto-Neg
  375. * has completed and/or if our link status has changed. The
  376. * get_link_status flag is set upon receiving a Link Status
  377. * Change or Rx Sequence Error interrupt.
  378. */
  379. if (!mac->get_link_status)
  380. return 0;
  381. /*
  382. * First we want to see if the MII Status Register reports
  383. * link. If so, then we want to get the current speed/duplex
  384. * of the PHY.
  385. */
  386. ret_val = e1000e_phy_has_link_generic(hw, 1, 0, &link);
  387. if (ret_val)
  388. return ret_val;
  389. if (!link)
  390. return 0; /* No link detected */
  391. mac->get_link_status = false;
  392. /*
  393. * Check if there was DownShift, must be checked
  394. * immediately after link-up
  395. */
  396. e1000e_check_downshift(hw);
  397. /*
  398. * If we are forcing speed/duplex, then we simply return since
  399. * we have already determined whether we have link or not.
  400. */
  401. if (!mac->autoneg)
  402. return -E1000_ERR_CONFIG;
  403. /*
  404. * Auto-Neg is enabled. Auto Speed Detection takes care
  405. * of MAC speed/duplex configuration. So we only need to
  406. * configure Collision Distance in the MAC.
  407. */
  408. mac->ops.config_collision_dist(hw);
  409. /*
  410. * Configure Flow Control now that Auto-Neg has completed.
  411. * First, we need to restore the desired flow control
  412. * settings because we may have had to re-autoneg with a
  413. * different link partner.
  414. */
  415. ret_val = e1000e_config_fc_after_link_up(hw);
  416. if (ret_val)
  417. e_dbg("Error configuring flow control\n");
  418. return ret_val;
  419. }
  420. /**
  421. * e1000e_check_for_fiber_link - Check for link (Fiber)
  422. * @hw: pointer to the HW structure
  423. *
  424. * Checks for link up on the hardware. If link is not up and we have
  425. * a signal, then we need to force link up.
  426. **/
  427. s32 e1000e_check_for_fiber_link(struct e1000_hw *hw)
  428. {
  429. struct e1000_mac_info *mac = &hw->mac;
  430. u32 rxcw;
  431. u32 ctrl;
  432. u32 status;
  433. s32 ret_val;
  434. ctrl = er32(CTRL);
  435. status = er32(STATUS);
  436. rxcw = er32(RXCW);
  437. /*
  438. * If we don't have link (auto-negotiation failed or link partner
  439. * cannot auto-negotiate), the cable is plugged in (we have signal),
  440. * and our link partner is not trying to auto-negotiate with us (we
  441. * are receiving idles or data), we need to force link up. We also
  442. * need to give auto-negotiation time to complete, in case the cable
  443. * was just plugged in. The autoneg_failed flag does this.
  444. */
  445. /* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
  446. if ((ctrl & E1000_CTRL_SWDPIN1) && !(status & E1000_STATUS_LU) &&
  447. !(rxcw & E1000_RXCW_C)) {
  448. if (!mac->autoneg_failed) {
  449. mac->autoneg_failed = true;
  450. return 0;
  451. }
  452. e_dbg("NOT Rx'ing /C/, disable AutoNeg and force link.\n");
  453. /* Disable auto-negotiation in the TXCW register */
  454. ew32(TXCW, (mac->txcw & ~E1000_TXCW_ANE));
  455. /* Force link-up and also force full-duplex. */
  456. ctrl = er32(CTRL);
  457. ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
  458. ew32(CTRL, ctrl);
  459. /* Configure Flow Control after forcing link up. */
  460. ret_val = e1000e_config_fc_after_link_up(hw);
  461. if (ret_val) {
  462. e_dbg("Error configuring flow control\n");
  463. return ret_val;
  464. }
  465. } else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
  466. /*
  467. * If we are forcing link and we are receiving /C/ ordered
  468. * sets, re-enable auto-negotiation in the TXCW register
  469. * and disable forced link in the Device Control register
  470. * in an attempt to auto-negotiate with our link partner.
  471. */
  472. e_dbg("Rx'ing /C/, enable AutoNeg and stop forcing link.\n");
  473. ew32(TXCW, mac->txcw);
  474. ew32(CTRL, (ctrl & ~E1000_CTRL_SLU));
  475. mac->serdes_has_link = true;
  476. }
  477. return 0;
  478. }
  479. /**
  480. * e1000e_check_for_serdes_link - Check for link (Serdes)
  481. * @hw: pointer to the HW structure
  482. *
  483. * Checks for link up on the hardware. If link is not up and we have
  484. * a signal, then we need to force link up.
  485. **/
  486. s32 e1000e_check_for_serdes_link(struct e1000_hw *hw)
  487. {
  488. struct e1000_mac_info *mac = &hw->mac;
  489. u32 rxcw;
  490. u32 ctrl;
  491. u32 status;
  492. s32 ret_val;
  493. ctrl = er32(CTRL);
  494. status = er32(STATUS);
  495. rxcw = er32(RXCW);
  496. /*
  497. * If we don't have link (auto-negotiation failed or link partner
  498. * cannot auto-negotiate), and our link partner is not trying to
  499. * auto-negotiate with us (we are receiving idles or data),
  500. * we need to force link up. We also need to give auto-negotiation
  501. * time to complete.
  502. */
  503. /* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
  504. if (!(status & E1000_STATUS_LU) && !(rxcw & E1000_RXCW_C)) {
  505. if (!mac->autoneg_failed) {
  506. mac->autoneg_failed = true;
  507. return 0;
  508. }
  509. e_dbg("NOT Rx'ing /C/, disable AutoNeg and force link.\n");
  510. /* Disable auto-negotiation in the TXCW register */
  511. ew32(TXCW, (mac->txcw & ~E1000_TXCW_ANE));
  512. /* Force link-up and also force full-duplex. */
  513. ctrl = er32(CTRL);
  514. ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
  515. ew32(CTRL, ctrl);
  516. /* Configure Flow Control after forcing link up. */
  517. ret_val = e1000e_config_fc_after_link_up(hw);
  518. if (ret_val) {
  519. e_dbg("Error configuring flow control\n");
  520. return ret_val;
  521. }
  522. } else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
  523. /*
  524. * If we are forcing link and we are receiving /C/ ordered
  525. * sets, re-enable auto-negotiation in the TXCW register
  526. * and disable forced link in the Device Control register
  527. * in an attempt to auto-negotiate with our link partner.
  528. */
  529. e_dbg("Rx'ing /C/, enable AutoNeg and stop forcing link.\n");
  530. ew32(TXCW, mac->txcw);
  531. ew32(CTRL, (ctrl & ~E1000_CTRL_SLU));
  532. mac->serdes_has_link = true;
  533. } else if (!(E1000_TXCW_ANE & er32(TXCW))) {
  534. /*
  535. * If we force link for non-auto-negotiation switch, check
  536. * link status based on MAC synchronization for internal
  537. * serdes media type.
  538. */
  539. /* SYNCH bit and IV bit are sticky. */
  540. udelay(10);
  541. rxcw = er32(RXCW);
  542. if (rxcw & E1000_RXCW_SYNCH) {
  543. if (!(rxcw & E1000_RXCW_IV)) {
  544. mac->serdes_has_link = true;
  545. e_dbg("SERDES: Link up - forced.\n");
  546. }
  547. } else {
  548. mac->serdes_has_link = false;
  549. e_dbg("SERDES: Link down - force failed.\n");
  550. }
  551. }
  552. if (E1000_TXCW_ANE & er32(TXCW)) {
  553. status = er32(STATUS);
  554. if (status & E1000_STATUS_LU) {
  555. /* SYNCH bit and IV bit are sticky, so reread rxcw. */
  556. udelay(10);
  557. rxcw = er32(RXCW);
  558. if (rxcw & E1000_RXCW_SYNCH) {
  559. if (!(rxcw & E1000_RXCW_IV)) {
  560. mac->serdes_has_link = true;
  561. e_dbg("SERDES: Link up - autoneg completed successfully.\n");
  562. } else {
  563. mac->serdes_has_link = false;
  564. e_dbg("SERDES: Link down - invalid codewords detected in autoneg.\n");
  565. }
  566. } else {
  567. mac->serdes_has_link = false;
  568. e_dbg("SERDES: Link down - no sync.\n");
  569. }
  570. } else {
  571. mac->serdes_has_link = false;
  572. e_dbg("SERDES: Link down - autoneg failed\n");
  573. }
  574. }
  575. return 0;
  576. }
  577. /**
  578. * e1000_set_default_fc_generic - Set flow control default values
  579. * @hw: pointer to the HW structure
  580. *
  581. * Read the EEPROM for the default values for flow control and store the
  582. * values.
  583. **/
  584. static s32 e1000_set_default_fc_generic(struct e1000_hw *hw)
  585. {
  586. s32 ret_val;
  587. u16 nvm_data;
  588. /*
  589. * Read and store word 0x0F of the EEPROM. This word contains bits
  590. * that determine the hardware's default PAUSE (flow control) mode,
  591. * a bit that determines whether the HW defaults to enabling or
  592. * disabling auto-negotiation, and the direction of the
  593. * SW defined pins. If there is no SW over-ride of the flow
  594. * control setting, then the variable hw->fc will
  595. * be initialized based on a value in the EEPROM.
  596. */
  597. ret_val = e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &nvm_data);
  598. if (ret_val) {
  599. e_dbg("NVM Read Error\n");
  600. return ret_val;
  601. }
  602. if (!(nvm_data & NVM_WORD0F_PAUSE_MASK))
  603. hw->fc.requested_mode = e1000_fc_none;
  604. else if ((nvm_data & NVM_WORD0F_PAUSE_MASK) == NVM_WORD0F_ASM_DIR)
  605. hw->fc.requested_mode = e1000_fc_tx_pause;
  606. else
  607. hw->fc.requested_mode = e1000_fc_full;
  608. return 0;
  609. }
  610. /**
  611. * e1000e_setup_link_generic - Setup flow control and link settings
  612. * @hw: pointer to the HW structure
  613. *
  614. * Determines which flow control settings to use, then configures flow
  615. * control. Calls the appropriate media-specific link configuration
  616. * function. Assuming the adapter has a valid link partner, a valid link
  617. * should be established. Assumes the hardware has previously been reset
  618. * and the transmitter and receiver are not enabled.
  619. **/
  620. s32 e1000e_setup_link_generic(struct e1000_hw *hw)
  621. {
  622. s32 ret_val;
  623. /*
  624. * In the case of the phy reset being blocked, we already have a link.
  625. * We do not need to set it up again.
  626. */
  627. if (hw->phy.ops.check_reset_block(hw))
  628. return 0;
  629. /*
  630. * If requested flow control is set to default, set flow control
  631. * based on the EEPROM flow control settings.
  632. */
  633. if (hw->fc.requested_mode == e1000_fc_default) {
  634. ret_val = e1000_set_default_fc_generic(hw);
  635. if (ret_val)
  636. return ret_val;
  637. }
  638. /*
  639. * Save off the requested flow control mode for use later. Depending
  640. * on the link partner's capabilities, we may or may not use this mode.
  641. */
  642. hw->fc.current_mode = hw->fc.requested_mode;
  643. e_dbg("After fix-ups FlowControl is now = %x\n", hw->fc.current_mode);
  644. /* Call the necessary media_type subroutine to configure the link. */
  645. ret_val = hw->mac.ops.setup_physical_interface(hw);
  646. if (ret_val)
  647. return ret_val;
  648. /*
  649. * Initialize the flow control address, type, and PAUSE timer
  650. * registers to their default values. This is done even if flow
  651. * control is disabled, because it does not hurt anything to
  652. * initialize these registers.
  653. */
  654. e_dbg("Initializing the Flow Control address, type and timer regs\n");
  655. ew32(FCT, FLOW_CONTROL_TYPE);
  656. ew32(FCAH, FLOW_CONTROL_ADDRESS_HIGH);
  657. ew32(FCAL, FLOW_CONTROL_ADDRESS_LOW);
  658. ew32(FCTTV, hw->fc.pause_time);
  659. return e1000e_set_fc_watermarks(hw);
  660. }
  661. /**
  662. * e1000_commit_fc_settings_generic - Configure flow control
  663. * @hw: pointer to the HW structure
  664. *
  665. * Write the flow control settings to the Transmit Config Word Register (TXCW)
  666. * base on the flow control settings in e1000_mac_info.
  667. **/
  668. static s32 e1000_commit_fc_settings_generic(struct e1000_hw *hw)
  669. {
  670. struct e1000_mac_info *mac = &hw->mac;
  671. u32 txcw;
  672. /*
  673. * Check for a software override of the flow control settings, and
  674. * setup the device accordingly. If auto-negotiation is enabled, then
  675. * software will have to set the "PAUSE" bits to the correct value in
  676. * the Transmit Config Word Register (TXCW) and re-start auto-
  677. * negotiation. However, if auto-negotiation is disabled, then
  678. * software will have to manually configure the two flow control enable
  679. * bits in the CTRL register.
  680. *
  681. * The possible values of the "fc" parameter are:
  682. * 0: Flow control is completely disabled
  683. * 1: Rx flow control is enabled (we can receive pause frames,
  684. * but not send pause frames).
  685. * 2: Tx flow control is enabled (we can send pause frames but we
  686. * do not support receiving pause frames).
  687. * 3: Both Rx and Tx flow control (symmetric) are enabled.
  688. */
  689. switch (hw->fc.current_mode) {
  690. case e1000_fc_none:
  691. /* Flow control completely disabled by a software over-ride. */
  692. txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
  693. break;
  694. case e1000_fc_rx_pause:
  695. /*
  696. * Rx Flow control is enabled and Tx Flow control is disabled
  697. * by a software over-ride. Since there really isn't a way to
  698. * advertise that we are capable of Rx Pause ONLY, we will
  699. * advertise that we support both symmetric and asymmetric Rx
  700. * PAUSE. Later, we will disable the adapter's ability to send
  701. * PAUSE frames.
  702. */
  703. txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
  704. break;
  705. case e1000_fc_tx_pause:
  706. /*
  707. * Tx Flow control is enabled, and Rx Flow control is disabled,
  708. * by a software over-ride.
  709. */
  710. txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
  711. break;
  712. case e1000_fc_full:
  713. /*
  714. * Flow control (both Rx and Tx) is enabled by a software
  715. * over-ride.
  716. */
  717. txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
  718. break;
  719. default:
  720. e_dbg("Flow control param set incorrectly\n");
  721. return -E1000_ERR_CONFIG;
  722. break;
  723. }
  724. ew32(TXCW, txcw);
  725. mac->txcw = txcw;
  726. return 0;
  727. }
  728. /**
  729. * e1000_poll_fiber_serdes_link_generic - Poll for link up
  730. * @hw: pointer to the HW structure
  731. *
  732. * Polls for link up by reading the status register, if link fails to come
  733. * up with auto-negotiation, then the link is forced if a signal is detected.
  734. **/
  735. static s32 e1000_poll_fiber_serdes_link_generic(struct e1000_hw *hw)
  736. {
  737. struct e1000_mac_info *mac = &hw->mac;
  738. u32 i, status;
  739. s32 ret_val;
  740. /*
  741. * If we have a signal (the cable is plugged in, or assumed true for
  742. * serdes media) then poll for a "Link-Up" indication in the Device
  743. * Status Register. Time-out if a link isn't seen in 500 milliseconds
  744. * seconds (Auto-negotiation should complete in less than 500
  745. * milliseconds even if the other end is doing it in SW).
  746. */
  747. for (i = 0; i < FIBER_LINK_UP_LIMIT; i++) {
  748. usleep_range(10000, 20000);
  749. status = er32(STATUS);
  750. if (status & E1000_STATUS_LU)
  751. break;
  752. }
  753. if (i == FIBER_LINK_UP_LIMIT) {
  754. e_dbg("Never got a valid link from auto-neg!!!\n");
  755. mac->autoneg_failed = true;
  756. /*
  757. * AutoNeg failed to achieve a link, so we'll call
  758. * mac->check_for_link. This routine will force the
  759. * link up if we detect a signal. This will allow us to
  760. * communicate with non-autonegotiating link partners.
  761. */
  762. ret_val = mac->ops.check_for_link(hw);
  763. if (ret_val) {
  764. e_dbg("Error while checking for link\n");
  765. return ret_val;
  766. }
  767. mac->autoneg_failed = false;
  768. } else {
  769. mac->autoneg_failed = false;
  770. e_dbg("Valid Link Found\n");
  771. }
  772. return 0;
  773. }
  774. /**
  775. * e1000e_setup_fiber_serdes_link - Setup link for fiber/serdes
  776. * @hw: pointer to the HW structure
  777. *
  778. * Configures collision distance and flow control for fiber and serdes
  779. * links. Upon successful setup, poll for link.
  780. **/
  781. s32 e1000e_setup_fiber_serdes_link(struct e1000_hw *hw)
  782. {
  783. u32 ctrl;
  784. s32 ret_val;
  785. ctrl = er32(CTRL);
  786. /* Take the link out of reset */
  787. ctrl &= ~E1000_CTRL_LRST;
  788. hw->mac.ops.config_collision_dist(hw);
  789. ret_val = e1000_commit_fc_settings_generic(hw);
  790. if (ret_val)
  791. return ret_val;
  792. /*
  793. * Since auto-negotiation is enabled, take the link out of reset (the
  794. * link will be in reset, because we previously reset the chip). This
  795. * will restart auto-negotiation. If auto-negotiation is successful
  796. * then the link-up status bit will be set and the flow control enable
  797. * bits (RFCE and TFCE) will be set according to their negotiated value.
  798. */
  799. e_dbg("Auto-negotiation enabled\n");
  800. ew32(CTRL, ctrl);
  801. e1e_flush();
  802. usleep_range(1000, 2000);
  803. /*
  804. * For these adapters, the SW definable pin 1 is set when the optics
  805. * detect a signal. If we have a signal, then poll for a "Link-Up"
  806. * indication.
  807. */
  808. if (hw->phy.media_type == e1000_media_type_internal_serdes ||
  809. (er32(CTRL) & E1000_CTRL_SWDPIN1)) {
  810. ret_val = e1000_poll_fiber_serdes_link_generic(hw);
  811. } else {
  812. e_dbg("No signal detected\n");
  813. }
  814. return ret_val;
  815. }
  816. /**
  817. * e1000e_config_collision_dist_generic - Configure collision distance
  818. * @hw: pointer to the HW structure
  819. *
  820. * Configures the collision distance to the default value and is used
  821. * during link setup.
  822. **/
  823. void e1000e_config_collision_dist_generic(struct e1000_hw *hw)
  824. {
  825. u32 tctl;
  826. tctl = er32(TCTL);
  827. tctl &= ~E1000_TCTL_COLD;
  828. tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
  829. ew32(TCTL, tctl);
  830. e1e_flush();
  831. }
  832. /**
  833. * e1000e_set_fc_watermarks - Set flow control high/low watermarks
  834. * @hw: pointer to the HW structure
  835. *
  836. * Sets the flow control high/low threshold (watermark) registers. If
  837. * flow control XON frame transmission is enabled, then set XON frame
  838. * transmission as well.
  839. **/
  840. s32 e1000e_set_fc_watermarks(struct e1000_hw *hw)
  841. {
  842. u32 fcrtl = 0, fcrth = 0;
  843. /*
  844. * Set the flow control receive threshold registers. Normally,
  845. * these registers will be set to a default threshold that may be
  846. * adjusted later by the driver's runtime code. However, if the
  847. * ability to transmit pause frames is not enabled, then these
  848. * registers will be set to 0.
  849. */
  850. if (hw->fc.current_mode & e1000_fc_tx_pause) {
  851. /*
  852. * We need to set up the Receive Threshold high and low water
  853. * marks as well as (optionally) enabling the transmission of
  854. * XON frames.
  855. */
  856. fcrtl = hw->fc.low_water;
  857. if (hw->fc.send_xon)
  858. fcrtl |= E1000_FCRTL_XONE;
  859. fcrth = hw->fc.high_water;
  860. }
  861. ew32(FCRTL, fcrtl);
  862. ew32(FCRTH, fcrth);
  863. return 0;
  864. }
  865. /**
  866. * e1000e_force_mac_fc - Force the MAC's flow control settings
  867. * @hw: pointer to the HW structure
  868. *
  869. * Force the MAC's flow control settings. Sets the TFCE and RFCE bits in the
  870. * device control register to reflect the adapter settings. TFCE and RFCE
  871. * need to be explicitly set by software when a copper PHY is used because
  872. * autonegotiation is managed by the PHY rather than the MAC. Software must
  873. * also configure these bits when link is forced on a fiber connection.
  874. **/
  875. s32 e1000e_force_mac_fc(struct e1000_hw *hw)
  876. {
  877. u32 ctrl;
  878. ctrl = er32(CTRL);
  879. /*
  880. * Because we didn't get link via the internal auto-negotiation
  881. * mechanism (we either forced link or we got link via PHY
  882. * auto-neg), we have to manually enable/disable transmit an
  883. * receive flow control.
  884. *
  885. * The "Case" statement below enables/disable flow control
  886. * according to the "hw->fc.current_mode" parameter.
  887. *
  888. * The possible values of the "fc" parameter are:
  889. * 0: Flow control is completely disabled
  890. * 1: Rx flow control is enabled (we can receive pause
  891. * frames but not send pause frames).
  892. * 2: Tx flow control is enabled (we can send pause frames
  893. * frames but we do not receive pause frames).
  894. * 3: Both Rx and Tx flow control (symmetric) is enabled.
  895. * other: No other values should be possible at this point.
  896. */
  897. e_dbg("hw->fc.current_mode = %u\n", hw->fc.current_mode);
  898. switch (hw->fc.current_mode) {
  899. case e1000_fc_none:
  900. ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
  901. break;
  902. case e1000_fc_rx_pause:
  903. ctrl &= (~E1000_CTRL_TFCE);
  904. ctrl |= E1000_CTRL_RFCE;
  905. break;
  906. case e1000_fc_tx_pause:
  907. ctrl &= (~E1000_CTRL_RFCE);
  908. ctrl |= E1000_CTRL_TFCE;
  909. break;
  910. case e1000_fc_full:
  911. ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
  912. break;
  913. default:
  914. e_dbg("Flow control param set incorrectly\n");
  915. return -E1000_ERR_CONFIG;
  916. }
  917. ew32(CTRL, ctrl);
  918. return 0;
  919. }
  920. /**
  921. * e1000e_config_fc_after_link_up - Configures flow control after link
  922. * @hw: pointer to the HW structure
  923. *
  924. * Checks the status of auto-negotiation after link up to ensure that the
  925. * speed and duplex were not forced. If the link needed to be forced, then
  926. * flow control needs to be forced also. If auto-negotiation is enabled
  927. * and did not fail, then we configure flow control based on our link
  928. * partner.
  929. **/
  930. s32 e1000e_config_fc_after_link_up(struct e1000_hw *hw)
  931. {
  932. struct e1000_mac_info *mac = &hw->mac;
  933. s32 ret_val = 0;
  934. u16 mii_status_reg, mii_nway_adv_reg, mii_nway_lp_ability_reg;
  935. u16 speed, duplex;
  936. /*
  937. * Check for the case where we have fiber media and auto-neg failed
  938. * so we had to force link. In this case, we need to force the
  939. * configuration of the MAC to match the "fc" parameter.
  940. */
  941. if (mac->autoneg_failed) {
  942. if (hw->phy.media_type == e1000_media_type_fiber ||
  943. hw->phy.media_type == e1000_media_type_internal_serdes)
  944. ret_val = e1000e_force_mac_fc(hw);
  945. } else {
  946. if (hw->phy.media_type == e1000_media_type_copper)
  947. ret_val = e1000e_force_mac_fc(hw);
  948. }
  949. if (ret_val) {
  950. e_dbg("Error forcing flow control settings\n");
  951. return ret_val;
  952. }
  953. /*
  954. * Check for the case where we have copper media and auto-neg is
  955. * enabled. In this case, we need to check and see if Auto-Neg
  956. * has completed, and if so, how the PHY and link partner has
  957. * flow control configured.
  958. */
  959. if ((hw->phy.media_type == e1000_media_type_copper) && mac->autoneg) {
  960. /*
  961. * Read the MII Status Register and check to see if AutoNeg
  962. * has completed. We read this twice because this reg has
  963. * some "sticky" (latched) bits.
  964. */
  965. ret_val = e1e_rphy(hw, PHY_STATUS, &mii_status_reg);
  966. if (ret_val)
  967. return ret_val;
  968. ret_val = e1e_rphy(hw, PHY_STATUS, &mii_status_reg);
  969. if (ret_val)
  970. return ret_val;
  971. if (!(mii_status_reg & MII_SR_AUTONEG_COMPLETE)) {
  972. e_dbg("Copper PHY and Auto Neg has not completed.\n");
  973. return ret_val;
  974. }
  975. /*
  976. * The AutoNeg process has completed, so we now need to
  977. * read both the Auto Negotiation Advertisement
  978. * Register (Address 4) and the Auto_Negotiation Base
  979. * Page Ability Register (Address 5) to determine how
  980. * flow control was negotiated.
  981. */
  982. ret_val = e1e_rphy(hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg);
  983. if (ret_val)
  984. return ret_val;
  985. ret_val =
  986. e1e_rphy(hw, PHY_LP_ABILITY, &mii_nway_lp_ability_reg);
  987. if (ret_val)
  988. return ret_val;
  989. /*
  990. * Two bits in the Auto Negotiation Advertisement Register
  991. * (Address 4) and two bits in the Auto Negotiation Base
  992. * Page Ability Register (Address 5) determine flow control
  993. * for both the PHY and the link partner. The following
  994. * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
  995. * 1999, describes these PAUSE resolution bits and how flow
  996. * control is determined based upon these settings.
  997. * NOTE: DC = Don't Care
  998. *
  999. * LOCAL DEVICE | LINK PARTNER
  1000. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
  1001. *-------|---------|-------|---------|--------------------
  1002. * 0 | 0 | DC | DC | e1000_fc_none
  1003. * 0 | 1 | 0 | DC | e1000_fc_none
  1004. * 0 | 1 | 1 | 0 | e1000_fc_none
  1005. * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
  1006. * 1 | 0 | 0 | DC | e1000_fc_none
  1007. * 1 | DC | 1 | DC | e1000_fc_full
  1008. * 1 | 1 | 0 | 0 | e1000_fc_none
  1009. * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
  1010. *
  1011. * Are both PAUSE bits set to 1? If so, this implies
  1012. * Symmetric Flow Control is enabled at both ends. The
  1013. * ASM_DIR bits are irrelevant per the spec.
  1014. *
  1015. * For Symmetric Flow Control:
  1016. *
  1017. * LOCAL DEVICE | LINK PARTNER
  1018. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  1019. *-------|---------|-------|---------|--------------------
  1020. * 1 | DC | 1 | DC | E1000_fc_full
  1021. *
  1022. */
  1023. if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
  1024. (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
  1025. /*
  1026. * Now we need to check if the user selected Rx ONLY
  1027. * of pause frames. In this case, we had to advertise
  1028. * FULL flow control because we could not advertise Rx
  1029. * ONLY. Hence, we must now check to see if we need to
  1030. * turn OFF the TRANSMISSION of PAUSE frames.
  1031. */
  1032. if (hw->fc.requested_mode == e1000_fc_full) {
  1033. hw->fc.current_mode = e1000_fc_full;
  1034. e_dbg("Flow Control = FULL.\n");
  1035. } else {
  1036. hw->fc.current_mode = e1000_fc_rx_pause;
  1037. e_dbg("Flow Control = Rx PAUSE frames only.\n");
  1038. }
  1039. }
  1040. /*
  1041. * For receiving PAUSE frames ONLY.
  1042. *
  1043. * LOCAL DEVICE | LINK PARTNER
  1044. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  1045. *-------|---------|-------|---------|--------------------
  1046. * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
  1047. */
  1048. else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
  1049. (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
  1050. (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
  1051. (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
  1052. hw->fc.current_mode = e1000_fc_tx_pause;
  1053. e_dbg("Flow Control = Tx PAUSE frames only.\n");
  1054. }
  1055. /*
  1056. * For transmitting PAUSE frames ONLY.
  1057. *
  1058. * LOCAL DEVICE | LINK PARTNER
  1059. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  1060. *-------|---------|-------|---------|--------------------
  1061. * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
  1062. */
  1063. else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
  1064. (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
  1065. !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
  1066. (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
  1067. hw->fc.current_mode = e1000_fc_rx_pause;
  1068. e_dbg("Flow Control = Rx PAUSE frames only.\n");
  1069. } else {
  1070. /*
  1071. * Per the IEEE spec, at this point flow control
  1072. * should be disabled.
  1073. */
  1074. hw->fc.current_mode = e1000_fc_none;
  1075. e_dbg("Flow Control = NONE.\n");
  1076. }
  1077. /*
  1078. * Now we need to do one last check... If we auto-
  1079. * negotiated to HALF DUPLEX, flow control should not be
  1080. * enabled per IEEE 802.3 spec.
  1081. */
  1082. ret_val = mac->ops.get_link_up_info(hw, &speed, &duplex);
  1083. if (ret_val) {
  1084. e_dbg("Error getting link speed and duplex\n");
  1085. return ret_val;
  1086. }
  1087. if (duplex == HALF_DUPLEX)
  1088. hw->fc.current_mode = e1000_fc_none;
  1089. /*
  1090. * Now we call a subroutine to actually force the MAC
  1091. * controller to use the correct flow control settings.
  1092. */
  1093. ret_val = e1000e_force_mac_fc(hw);
  1094. if (ret_val) {
  1095. e_dbg("Error forcing flow control settings\n");
  1096. return ret_val;
  1097. }
  1098. }
  1099. return 0;
  1100. }
  1101. /**
  1102. * e1000e_get_speed_and_duplex_copper - Retrieve current speed/duplex
  1103. * @hw: pointer to the HW structure
  1104. * @speed: stores the current speed
  1105. * @duplex: stores the current duplex
  1106. *
  1107. * Read the status register for the current speed/duplex and store the current
  1108. * speed and duplex for copper connections.
  1109. **/
  1110. s32 e1000e_get_speed_and_duplex_copper(struct e1000_hw *hw, u16 *speed,
  1111. u16 *duplex)
  1112. {
  1113. u32 status;
  1114. status = er32(STATUS);
  1115. if (status & E1000_STATUS_SPEED_1000)
  1116. *speed = SPEED_1000;
  1117. else if (status & E1000_STATUS_SPEED_100)
  1118. *speed = SPEED_100;
  1119. else
  1120. *speed = SPEED_10;
  1121. if (status & E1000_STATUS_FD)
  1122. *duplex = FULL_DUPLEX;
  1123. else
  1124. *duplex = HALF_DUPLEX;
  1125. e_dbg("%u Mbps, %s Duplex\n",
  1126. *speed == SPEED_1000 ? 1000 : *speed == SPEED_100 ? 100 : 10,
  1127. *duplex == FULL_DUPLEX ? "Full" : "Half");
  1128. return 0;
  1129. }
  1130. /**
  1131. * e1000e_get_speed_and_duplex_fiber_serdes - Retrieve current speed/duplex
  1132. * @hw: pointer to the HW structure
  1133. * @speed: stores the current speed
  1134. * @duplex: stores the current duplex
  1135. *
  1136. * Sets the speed and duplex to gigabit full duplex (the only possible option)
  1137. * for fiber/serdes links.
  1138. **/
  1139. s32 e1000e_get_speed_and_duplex_fiber_serdes(struct e1000_hw *hw, u16 *speed,
  1140. u16 *duplex)
  1141. {
  1142. *speed = SPEED_1000;
  1143. *duplex = FULL_DUPLEX;
  1144. return 0;
  1145. }
  1146. /**
  1147. * e1000e_get_hw_semaphore - Acquire hardware semaphore
  1148. * @hw: pointer to the HW structure
  1149. *
  1150. * Acquire the HW semaphore to access the PHY or NVM
  1151. **/
  1152. s32 e1000e_get_hw_semaphore(struct e1000_hw *hw)
  1153. {
  1154. u32 swsm;
  1155. s32 timeout = hw->nvm.word_size + 1;
  1156. s32 i = 0;
  1157. /* Get the SW semaphore */
  1158. while (i < timeout) {
  1159. swsm = er32(SWSM);
  1160. if (!(swsm & E1000_SWSM_SMBI))
  1161. break;
  1162. udelay(50);
  1163. i++;
  1164. }
  1165. if (i == timeout) {
  1166. e_dbg("Driver can't access device - SMBI bit is set.\n");
  1167. return -E1000_ERR_NVM;
  1168. }
  1169. /* Get the FW semaphore. */
  1170. for (i = 0; i < timeout; i++) {
  1171. swsm = er32(SWSM);
  1172. ew32(SWSM, swsm | E1000_SWSM_SWESMBI);
  1173. /* Semaphore acquired if bit latched */
  1174. if (er32(SWSM) & E1000_SWSM_SWESMBI)
  1175. break;
  1176. udelay(50);
  1177. }
  1178. if (i == timeout) {
  1179. /* Release semaphores */
  1180. e1000e_put_hw_semaphore(hw);
  1181. e_dbg("Driver can't access the NVM\n");
  1182. return -E1000_ERR_NVM;
  1183. }
  1184. return 0;
  1185. }
  1186. /**
  1187. * e1000e_put_hw_semaphore - Release hardware semaphore
  1188. * @hw: pointer to the HW structure
  1189. *
  1190. * Release hardware semaphore used to access the PHY or NVM
  1191. **/
  1192. void e1000e_put_hw_semaphore(struct e1000_hw *hw)
  1193. {
  1194. u32 swsm;
  1195. swsm = er32(SWSM);
  1196. swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
  1197. ew32(SWSM, swsm);
  1198. }
  1199. /**
  1200. * e1000e_get_auto_rd_done - Check for auto read completion
  1201. * @hw: pointer to the HW structure
  1202. *
  1203. * Check EEPROM for Auto Read done bit.
  1204. **/
  1205. s32 e1000e_get_auto_rd_done(struct e1000_hw *hw)
  1206. {
  1207. s32 i = 0;
  1208. while (i < AUTO_READ_DONE_TIMEOUT) {
  1209. if (er32(EECD) & E1000_EECD_AUTO_RD)
  1210. break;
  1211. usleep_range(1000, 2000);
  1212. i++;
  1213. }
  1214. if (i == AUTO_READ_DONE_TIMEOUT) {
  1215. e_dbg("Auto read by HW from NVM has not completed.\n");
  1216. return -E1000_ERR_RESET;
  1217. }
  1218. return 0;
  1219. }
  1220. /**
  1221. * e1000e_valid_led_default - Verify a valid default LED config
  1222. * @hw: pointer to the HW structure
  1223. * @data: pointer to the NVM (EEPROM)
  1224. *
  1225. * Read the EEPROM for the current default LED configuration. If the
  1226. * LED configuration is not valid, set to a valid LED configuration.
  1227. **/
  1228. s32 e1000e_valid_led_default(struct e1000_hw *hw, u16 *data)
  1229. {
  1230. s32 ret_val;
  1231. ret_val = e1000_read_nvm(hw, NVM_ID_LED_SETTINGS, 1, data);
  1232. if (ret_val) {
  1233. e_dbg("NVM Read Error\n");
  1234. return ret_val;
  1235. }
  1236. if (*data == ID_LED_RESERVED_0000 || *data == ID_LED_RESERVED_FFFF)
  1237. *data = ID_LED_DEFAULT;
  1238. return 0;
  1239. }
  1240. /**
  1241. * e1000e_id_led_init_generic -
  1242. * @hw: pointer to the HW structure
  1243. *
  1244. **/
  1245. s32 e1000e_id_led_init_generic(struct e1000_hw *hw)
  1246. {
  1247. struct e1000_mac_info *mac = &hw->mac;
  1248. s32 ret_val;
  1249. const u32 ledctl_mask = 0x000000FF;
  1250. const u32 ledctl_on = E1000_LEDCTL_MODE_LED_ON;
  1251. const u32 ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
  1252. u16 data, i, temp;
  1253. const u16 led_mask = 0x0F;
  1254. ret_val = hw->nvm.ops.valid_led_default(hw, &data);
  1255. if (ret_val)
  1256. return ret_val;
  1257. mac->ledctl_default = er32(LEDCTL);
  1258. mac->ledctl_mode1 = mac->ledctl_default;
  1259. mac->ledctl_mode2 = mac->ledctl_default;
  1260. for (i = 0; i < 4; i++) {
  1261. temp = (data >> (i << 2)) & led_mask;
  1262. switch (temp) {
  1263. case ID_LED_ON1_DEF2:
  1264. case ID_LED_ON1_ON2:
  1265. case ID_LED_ON1_OFF2:
  1266. mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
  1267. mac->ledctl_mode1 |= ledctl_on << (i << 3);
  1268. break;
  1269. case ID_LED_OFF1_DEF2:
  1270. case ID_LED_OFF1_ON2:
  1271. case ID_LED_OFF1_OFF2:
  1272. mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
  1273. mac->ledctl_mode1 |= ledctl_off << (i << 3);
  1274. break;
  1275. default:
  1276. /* Do nothing */
  1277. break;
  1278. }
  1279. switch (temp) {
  1280. case ID_LED_DEF1_ON2:
  1281. case ID_LED_ON1_ON2:
  1282. case ID_LED_OFF1_ON2:
  1283. mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
  1284. mac->ledctl_mode2 |= ledctl_on << (i << 3);
  1285. break;
  1286. case ID_LED_DEF1_OFF2:
  1287. case ID_LED_ON1_OFF2:
  1288. case ID_LED_OFF1_OFF2:
  1289. mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
  1290. mac->ledctl_mode2 |= ledctl_off << (i << 3);
  1291. break;
  1292. default:
  1293. /* Do nothing */
  1294. break;
  1295. }
  1296. }
  1297. return 0;
  1298. }
  1299. /**
  1300. * e1000e_setup_led_generic - Configures SW controllable LED
  1301. * @hw: pointer to the HW structure
  1302. *
  1303. * This prepares the SW controllable LED for use and saves the current state
  1304. * of the LED so it can be later restored.
  1305. **/
  1306. s32 e1000e_setup_led_generic(struct e1000_hw *hw)
  1307. {
  1308. u32 ledctl;
  1309. if (hw->mac.ops.setup_led != e1000e_setup_led_generic)
  1310. return -E1000_ERR_CONFIG;
  1311. if (hw->phy.media_type == e1000_media_type_fiber) {
  1312. ledctl = er32(LEDCTL);
  1313. hw->mac.ledctl_default = ledctl;
  1314. /* Turn off LED0 */
  1315. ledctl &= ~(E1000_LEDCTL_LED0_IVRT | E1000_LEDCTL_LED0_BLINK |
  1316. E1000_LEDCTL_LED0_MODE_MASK);
  1317. ledctl |= (E1000_LEDCTL_MODE_LED_OFF <<
  1318. E1000_LEDCTL_LED0_MODE_SHIFT);
  1319. ew32(LEDCTL, ledctl);
  1320. } else if (hw->phy.media_type == e1000_media_type_copper) {
  1321. ew32(LEDCTL, hw->mac.ledctl_mode1);
  1322. }
  1323. return 0;
  1324. }
  1325. /**
  1326. * e1000e_cleanup_led_generic - Set LED config to default operation
  1327. * @hw: pointer to the HW structure
  1328. *
  1329. * Remove the current LED configuration and set the LED configuration
  1330. * to the default value, saved from the EEPROM.
  1331. **/
  1332. s32 e1000e_cleanup_led_generic(struct e1000_hw *hw)
  1333. {
  1334. ew32(LEDCTL, hw->mac.ledctl_default);
  1335. return 0;
  1336. }
  1337. /**
  1338. * e1000e_blink_led_generic - Blink LED
  1339. * @hw: pointer to the HW structure
  1340. *
  1341. * Blink the LEDs which are set to be on.
  1342. **/
  1343. s32 e1000e_blink_led_generic(struct e1000_hw *hw)
  1344. {
  1345. u32 ledctl_blink = 0;
  1346. u32 i;
  1347. if (hw->phy.media_type == e1000_media_type_fiber) {
  1348. /* always blink LED0 for PCI-E fiber */
  1349. ledctl_blink = E1000_LEDCTL_LED0_BLINK |
  1350. (E1000_LEDCTL_MODE_LED_ON << E1000_LEDCTL_LED0_MODE_SHIFT);
  1351. } else {
  1352. /*
  1353. * set the blink bit for each LED that's "on" (0x0E)
  1354. * in ledctl_mode2
  1355. */
  1356. ledctl_blink = hw->mac.ledctl_mode2;
  1357. for (i = 0; i < 4; i++)
  1358. if (((hw->mac.ledctl_mode2 >> (i * 8)) & 0xFF) ==
  1359. E1000_LEDCTL_MODE_LED_ON)
  1360. ledctl_blink |= (E1000_LEDCTL_LED0_BLINK <<
  1361. (i * 8));
  1362. }
  1363. ew32(LEDCTL, ledctl_blink);
  1364. return 0;
  1365. }
  1366. /**
  1367. * e1000e_led_on_generic - Turn LED on
  1368. * @hw: pointer to the HW structure
  1369. *
  1370. * Turn LED on.
  1371. **/
  1372. s32 e1000e_led_on_generic(struct e1000_hw *hw)
  1373. {
  1374. u32 ctrl;
  1375. switch (hw->phy.media_type) {
  1376. case e1000_media_type_fiber:
  1377. ctrl = er32(CTRL);
  1378. ctrl &= ~E1000_CTRL_SWDPIN0;
  1379. ctrl |= E1000_CTRL_SWDPIO0;
  1380. ew32(CTRL, ctrl);
  1381. break;
  1382. case e1000_media_type_copper:
  1383. ew32(LEDCTL, hw->mac.ledctl_mode2);
  1384. break;
  1385. default:
  1386. break;
  1387. }
  1388. return 0;
  1389. }
  1390. /**
  1391. * e1000e_led_off_generic - Turn LED off
  1392. * @hw: pointer to the HW structure
  1393. *
  1394. * Turn LED off.
  1395. **/
  1396. s32 e1000e_led_off_generic(struct e1000_hw *hw)
  1397. {
  1398. u32 ctrl;
  1399. switch (hw->phy.media_type) {
  1400. case e1000_media_type_fiber:
  1401. ctrl = er32(CTRL);
  1402. ctrl |= E1000_CTRL_SWDPIN0;
  1403. ctrl |= E1000_CTRL_SWDPIO0;
  1404. ew32(CTRL, ctrl);
  1405. break;
  1406. case e1000_media_type_copper:
  1407. ew32(LEDCTL, hw->mac.ledctl_mode1);
  1408. break;
  1409. default:
  1410. break;
  1411. }
  1412. return 0;
  1413. }
  1414. /**
  1415. * e1000e_set_pcie_no_snoop - Set PCI-express capabilities
  1416. * @hw: pointer to the HW structure
  1417. * @no_snoop: bitmap of snoop events
  1418. *
  1419. * Set the PCI-express register to snoop for events enabled in 'no_snoop'.
  1420. **/
  1421. void e1000e_set_pcie_no_snoop(struct e1000_hw *hw, u32 no_snoop)
  1422. {
  1423. u32 gcr;
  1424. if (no_snoop) {
  1425. gcr = er32(GCR);
  1426. gcr &= ~(PCIE_NO_SNOOP_ALL);
  1427. gcr |= no_snoop;
  1428. ew32(GCR, gcr);
  1429. }
  1430. }
  1431. /**
  1432. * e1000e_disable_pcie_master - Disables PCI-express master access
  1433. * @hw: pointer to the HW structure
  1434. *
  1435. * Returns 0 if successful, else returns -10
  1436. * (-E1000_ERR_MASTER_REQUESTS_PENDING) if master disable bit has not caused
  1437. * the master requests to be disabled.
  1438. *
  1439. * Disables PCI-Express master access and verifies there are no pending
  1440. * requests.
  1441. **/
  1442. s32 e1000e_disable_pcie_master(struct e1000_hw *hw)
  1443. {
  1444. u32 ctrl;
  1445. s32 timeout = MASTER_DISABLE_TIMEOUT;
  1446. ctrl = er32(CTRL);
  1447. ctrl |= E1000_CTRL_GIO_MASTER_DISABLE;
  1448. ew32(CTRL, ctrl);
  1449. while (timeout) {
  1450. if (!(er32(STATUS) & E1000_STATUS_GIO_MASTER_ENABLE))
  1451. break;
  1452. udelay(100);
  1453. timeout--;
  1454. }
  1455. if (!timeout) {
  1456. e_dbg("Master requests are pending.\n");
  1457. return -E1000_ERR_MASTER_REQUESTS_PENDING;
  1458. }
  1459. return 0;
  1460. }
  1461. /**
  1462. * e1000e_reset_adaptive - Reset Adaptive Interframe Spacing
  1463. * @hw: pointer to the HW structure
  1464. *
  1465. * Reset the Adaptive Interframe Spacing throttle to default values.
  1466. **/
  1467. void e1000e_reset_adaptive(struct e1000_hw *hw)
  1468. {
  1469. struct e1000_mac_info *mac = &hw->mac;
  1470. if (!mac->adaptive_ifs) {
  1471. e_dbg("Not in Adaptive IFS mode!\n");
  1472. return;
  1473. }
  1474. mac->current_ifs_val = 0;
  1475. mac->ifs_min_val = IFS_MIN;
  1476. mac->ifs_max_val = IFS_MAX;
  1477. mac->ifs_step_size = IFS_STEP;
  1478. mac->ifs_ratio = IFS_RATIO;
  1479. mac->in_ifs_mode = false;
  1480. ew32(AIT, 0);
  1481. }
  1482. /**
  1483. * e1000e_update_adaptive - Update Adaptive Interframe Spacing
  1484. * @hw: pointer to the HW structure
  1485. *
  1486. * Update the Adaptive Interframe Spacing Throttle value based on the
  1487. * time between transmitted packets and time between collisions.
  1488. **/
  1489. void e1000e_update_adaptive(struct e1000_hw *hw)
  1490. {
  1491. struct e1000_mac_info *mac = &hw->mac;
  1492. if (!mac->adaptive_ifs) {
  1493. e_dbg("Not in Adaptive IFS mode!\n");
  1494. return;
  1495. }
  1496. if ((mac->collision_delta * mac->ifs_ratio) > mac->tx_packet_delta) {
  1497. if (mac->tx_packet_delta > MIN_NUM_XMITS) {
  1498. mac->in_ifs_mode = true;
  1499. if (mac->current_ifs_val < mac->ifs_max_val) {
  1500. if (!mac->current_ifs_val)
  1501. mac->current_ifs_val = mac->ifs_min_val;
  1502. else
  1503. mac->current_ifs_val +=
  1504. mac->ifs_step_size;
  1505. ew32(AIT, mac->current_ifs_val);
  1506. }
  1507. }
  1508. } else {
  1509. if (mac->in_ifs_mode &&
  1510. (mac->tx_packet_delta <= MIN_NUM_XMITS)) {
  1511. mac->current_ifs_val = 0;
  1512. mac->in_ifs_mode = false;
  1513. ew32(AIT, 0);
  1514. }
  1515. }
  1516. }