mac.c 47 KB

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