mac.c 47 KB

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