e1000_mac.c 48 KB

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