e1000_mac.c 46 KB

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