lib.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493
  1. /*******************************************************************************
  2. Intel PRO/1000 Linux driver
  3. Copyright(c) 1999 - 2007 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 <linux/netdevice.h>
  22. #include <linux/ethtool.h>
  23. #include <linux/delay.h>
  24. #include <linux/pci.h>
  25. #include "e1000.h"
  26. enum e1000_mng_mode {
  27. e1000_mng_mode_none = 0,
  28. e1000_mng_mode_asf,
  29. e1000_mng_mode_pt,
  30. e1000_mng_mode_ipmi,
  31. e1000_mng_mode_host_if_only
  32. };
  33. #define E1000_FACTPS_MNGCG 0x20000000
  34. #define E1000_IAMT_SIGNATURE 0x544D4149 /* Intel(R) Active Management
  35. * Technology signature */
  36. /**
  37. * e1000e_get_bus_info_pcie - Get PCIe bus information
  38. * @hw: pointer to the HW structure
  39. *
  40. * Determines and stores the system bus information for a particular
  41. * network interface. The following bus information is determined and stored:
  42. * bus speed, bus width, type (PCIe), and PCIe function.
  43. **/
  44. s32 e1000e_get_bus_info_pcie(struct e1000_hw *hw)
  45. {
  46. struct e1000_bus_info *bus = &hw->bus;
  47. struct e1000_adapter *adapter = hw->adapter;
  48. u32 status;
  49. u16 pcie_link_status, pci_header_type, cap_offset;
  50. cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
  51. if (!cap_offset) {
  52. bus->width = e1000_bus_width_unknown;
  53. } else {
  54. pci_read_config_word(adapter->pdev,
  55. cap_offset + PCIE_LINK_STATUS,
  56. &pcie_link_status);
  57. bus->width = (enum e1000_bus_width)((pcie_link_status &
  58. PCIE_LINK_WIDTH_MASK) >>
  59. PCIE_LINK_WIDTH_SHIFT);
  60. }
  61. pci_read_config_word(adapter->pdev, PCI_HEADER_TYPE_REGISTER,
  62. &pci_header_type);
  63. if (pci_header_type & PCI_HEADER_TYPE_MULTIFUNC) {
  64. status = er32(STATUS);
  65. bus->func = (status & E1000_STATUS_FUNC_MASK)
  66. >> E1000_STATUS_FUNC_SHIFT;
  67. } else {
  68. bus->func = 0;
  69. }
  70. return 0;
  71. }
  72. /**
  73. * e1000e_write_vfta - Write value to VLAN filter table
  74. * @hw: pointer to the HW structure
  75. * @offset: register offset in VLAN filter table
  76. * @value: register value written to VLAN filter table
  77. *
  78. * Writes value at the given offset in the register array which stores
  79. * the VLAN filter table.
  80. **/
  81. void e1000e_write_vfta(struct e1000_hw *hw, u32 offset, u32 value)
  82. {
  83. E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, offset, value);
  84. e1e_flush();
  85. }
  86. /**
  87. * e1000e_init_rx_addrs - Initialize receive address's
  88. * @hw: pointer to the HW structure
  89. * @rar_count: receive address registers
  90. *
  91. * Setups the receive address registers by setting the base receive address
  92. * register to the devices MAC address and clearing all the other receive
  93. * address registers to 0.
  94. **/
  95. void e1000e_init_rx_addrs(struct e1000_hw *hw, u16 rar_count)
  96. {
  97. u32 i;
  98. /* Setup the receive address */
  99. hw_dbg(hw, "Programming MAC Address into RAR[0]\n");
  100. e1000e_rar_set(hw, hw->mac.addr, 0);
  101. /* Zero out the other (rar_entry_count - 1) receive addresses */
  102. hw_dbg(hw, "Clearing RAR[1-%u]\n", rar_count-1);
  103. for (i = 1; i < rar_count; i++) {
  104. E1000_WRITE_REG_ARRAY(hw, E1000_RA, (i << 1), 0);
  105. e1e_flush();
  106. E1000_WRITE_REG_ARRAY(hw, E1000_RA, ((i << 1) + 1), 0);
  107. e1e_flush();
  108. }
  109. }
  110. /**
  111. * e1000e_rar_set - Set receive address register
  112. * @hw: pointer to the HW structure
  113. * @addr: pointer to the receive address
  114. * @index: receive address array register
  115. *
  116. * Sets the receive address array register at index to the address passed
  117. * in by addr.
  118. **/
  119. void e1000e_rar_set(struct e1000_hw *hw, u8 *addr, u32 index)
  120. {
  121. u32 rar_low, rar_high;
  122. /* HW expects these in little endian so we reverse the byte order
  123. * from network order (big endian) to little endian
  124. */
  125. rar_low = ((u32) addr[0] |
  126. ((u32) addr[1] << 8) |
  127. ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
  128. rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
  129. rar_high |= E1000_RAH_AV;
  130. E1000_WRITE_REG_ARRAY(hw, E1000_RA, (index << 1), rar_low);
  131. E1000_WRITE_REG_ARRAY(hw, E1000_RA, ((index << 1) + 1), rar_high);
  132. }
  133. /**
  134. * e1000_mta_set - Set multicast filter table address
  135. * @hw: pointer to the HW structure
  136. * @hash_value: determines the MTA register and bit to set
  137. *
  138. * The multicast table address is a register array of 32-bit registers.
  139. * The hash_value is used to determine what register the bit is in, the
  140. * current value is read, the new bit is OR'd in and the new value is
  141. * written back into the register.
  142. **/
  143. static void e1000_mta_set(struct e1000_hw *hw, u32 hash_value)
  144. {
  145. u32 hash_bit, hash_reg, mta;
  146. /* The MTA is a register array of 32-bit registers. It is
  147. * treated like an array of (32*mta_reg_count) bits. We want to
  148. * set bit BitArray[hash_value]. So we figure out what register
  149. * the bit is in, read it, OR in the new bit, then write
  150. * back the new value. The (hw->mac.mta_reg_count - 1) serves as a
  151. * mask to bits 31:5 of the hash value which gives us the
  152. * register we're modifying. The hash bit within that register
  153. * is determined by the lower 5 bits of the hash value.
  154. */
  155. hash_reg = (hash_value >> 5) & (hw->mac.mta_reg_count - 1);
  156. hash_bit = hash_value & 0x1F;
  157. mta = E1000_READ_REG_ARRAY(hw, E1000_MTA, hash_reg);
  158. mta |= (1 << hash_bit);
  159. E1000_WRITE_REG_ARRAY(hw, E1000_MTA, hash_reg, mta);
  160. e1e_flush();
  161. }
  162. /**
  163. * e1000_hash_mc_addr - Generate a multicast hash value
  164. * @hw: pointer to the HW structure
  165. * @mc_addr: pointer to a multicast address
  166. *
  167. * Generates a multicast address hash value which is used to determine
  168. * the multicast filter table array address and new table value. See
  169. * e1000_mta_set_generic()
  170. **/
  171. static u32 e1000_hash_mc_addr(struct e1000_hw *hw, u8 *mc_addr)
  172. {
  173. u32 hash_value, hash_mask;
  174. u8 bit_shift = 0;
  175. /* Register count multiplied by bits per register */
  176. hash_mask = (hw->mac.mta_reg_count * 32) - 1;
  177. /* For a mc_filter_type of 0, bit_shift is the number of left-shifts
  178. * where 0xFF would still fall within the hash mask. */
  179. while (hash_mask >> bit_shift != 0xFF)
  180. bit_shift++;
  181. /* The portion of the address that is used for the hash table
  182. * is determined by the mc_filter_type setting.
  183. * The algorithm is such that there is a total of 8 bits of shifting.
  184. * The bit_shift for a mc_filter_type of 0 represents the number of
  185. * left-shifts where the MSB of mc_addr[5] would still fall within
  186. * the hash_mask. Case 0 does this exactly. Since there are a total
  187. * of 8 bits of shifting, then mc_addr[4] will shift right the
  188. * remaining number of bits. Thus 8 - bit_shift. The rest of the
  189. * cases are a variation of this algorithm...essentially raising the
  190. * number of bits to shift mc_addr[5] left, while still keeping the
  191. * 8-bit shifting total.
  192. */
  193. /* For example, given the following Destination MAC Address and an
  194. * mta register count of 128 (thus a 4096-bit vector and 0xFFF mask),
  195. * we can see that the bit_shift for case 0 is 4. These are the hash
  196. * values resulting from each mc_filter_type...
  197. * [0] [1] [2] [3] [4] [5]
  198. * 01 AA 00 12 34 56
  199. * LSB MSB
  200. *
  201. * case 0: hash_value = ((0x34 >> 4) | (0x56 << 4)) & 0xFFF = 0x563
  202. * case 1: hash_value = ((0x34 >> 3) | (0x56 << 5)) & 0xFFF = 0xAC6
  203. * case 2: hash_value = ((0x34 >> 2) | (0x56 << 6)) & 0xFFF = 0x163
  204. * case 3: hash_value = ((0x34 >> 0) | (0x56 << 8)) & 0xFFF = 0x634
  205. */
  206. switch (hw->mac.mc_filter_type) {
  207. default:
  208. case 0:
  209. break;
  210. case 1:
  211. bit_shift += 1;
  212. break;
  213. case 2:
  214. bit_shift += 2;
  215. break;
  216. case 3:
  217. bit_shift += 4;
  218. break;
  219. }
  220. hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
  221. (((u16) mc_addr[5]) << bit_shift)));
  222. return hash_value;
  223. }
  224. /**
  225. * e1000e_mc_addr_list_update_generic - Update Multicast addresses
  226. * @hw: pointer to the HW structure
  227. * @mc_addr_list: array of multicast addresses to program
  228. * @mc_addr_count: number of multicast addresses to program
  229. * @rar_used_count: the first RAR register free to program
  230. * @rar_count: total number of supported Receive Address Registers
  231. *
  232. * Updates the Receive Address Registers and Multicast Table Array.
  233. * The caller must have a packed mc_addr_list of multicast addresses.
  234. * The parameter rar_count will usually be hw->mac.rar_entry_count
  235. * unless there are workarounds that change this.
  236. **/
  237. void e1000e_mc_addr_list_update_generic(struct e1000_hw *hw,
  238. u8 *mc_addr_list, u32 mc_addr_count,
  239. u32 rar_used_count, u32 rar_count)
  240. {
  241. u32 hash_value;
  242. u32 i;
  243. /* Load the first set of multicast addresses into the exact
  244. * filters (RAR). If there are not enough to fill the RAR
  245. * array, clear the filters.
  246. */
  247. for (i = rar_used_count; i < rar_count; i++) {
  248. if (mc_addr_count) {
  249. e1000e_rar_set(hw, mc_addr_list, i);
  250. mc_addr_count--;
  251. mc_addr_list += ETH_ALEN;
  252. } else {
  253. E1000_WRITE_REG_ARRAY(hw, E1000_RA, i << 1, 0);
  254. e1e_flush();
  255. E1000_WRITE_REG_ARRAY(hw, E1000_RA, (i << 1) + 1, 0);
  256. e1e_flush();
  257. }
  258. }
  259. /* Clear the old settings from the MTA */
  260. hw_dbg(hw, "Clearing MTA\n");
  261. for (i = 0; i < hw->mac.mta_reg_count; i++) {
  262. E1000_WRITE_REG_ARRAY(hw, E1000_MTA, i, 0);
  263. e1e_flush();
  264. }
  265. /* Load any remaining multicast addresses into the hash table. */
  266. for (; mc_addr_count > 0; mc_addr_count--) {
  267. hash_value = e1000_hash_mc_addr(hw, mc_addr_list);
  268. hw_dbg(hw, "Hash value = 0x%03X\n", hash_value);
  269. e1000_mta_set(hw, hash_value);
  270. mc_addr_list += ETH_ALEN;
  271. }
  272. }
  273. /**
  274. * e1000e_clear_hw_cntrs_base - Clear base hardware counters
  275. * @hw: pointer to the HW structure
  276. *
  277. * Clears the base hardware counters by reading the counter registers.
  278. **/
  279. void e1000e_clear_hw_cntrs_base(struct e1000_hw *hw)
  280. {
  281. u32 temp;
  282. temp = er32(CRCERRS);
  283. temp = er32(SYMERRS);
  284. temp = er32(MPC);
  285. temp = er32(SCC);
  286. temp = er32(ECOL);
  287. temp = er32(MCC);
  288. temp = er32(LATECOL);
  289. temp = er32(COLC);
  290. temp = er32(DC);
  291. temp = er32(SEC);
  292. temp = er32(RLEC);
  293. temp = er32(XONRXC);
  294. temp = er32(XONTXC);
  295. temp = er32(XOFFRXC);
  296. temp = er32(XOFFTXC);
  297. temp = er32(FCRUC);
  298. temp = er32(GPRC);
  299. temp = er32(BPRC);
  300. temp = er32(MPRC);
  301. temp = er32(GPTC);
  302. temp = er32(GORCL);
  303. temp = er32(GORCH);
  304. temp = er32(GOTCL);
  305. temp = er32(GOTCH);
  306. temp = er32(RNBC);
  307. temp = er32(RUC);
  308. temp = er32(RFC);
  309. temp = er32(ROC);
  310. temp = er32(RJC);
  311. temp = er32(TORL);
  312. temp = er32(TORH);
  313. temp = er32(TOTL);
  314. temp = er32(TOTH);
  315. temp = er32(TPR);
  316. temp = er32(TPT);
  317. temp = er32(MPTC);
  318. temp = er32(BPTC);
  319. }
  320. /**
  321. * e1000e_check_for_copper_link - Check for link (Copper)
  322. * @hw: pointer to the HW structure
  323. *
  324. * Checks to see of the link status of the hardware has changed. If a
  325. * change in link status has been detected, then we read the PHY registers
  326. * to get the current speed/duplex if link exists.
  327. **/
  328. s32 e1000e_check_for_copper_link(struct e1000_hw *hw)
  329. {
  330. struct e1000_mac_info *mac = &hw->mac;
  331. s32 ret_val;
  332. bool link;
  333. /* We only want to go out to the PHY registers to see if Auto-Neg
  334. * has completed and/or if our link status has changed. The
  335. * get_link_status flag is set upon receiving a Link Status
  336. * Change or Rx Sequence Error interrupt.
  337. */
  338. if (!mac->get_link_status)
  339. return 0;
  340. /* First we want to see if the MII Status Register reports
  341. * link. If so, then we want to get the current speed/duplex
  342. * of the PHY.
  343. */
  344. ret_val = e1000e_phy_has_link_generic(hw, 1, 0, &link);
  345. if (ret_val)
  346. return ret_val;
  347. if (!link)
  348. return ret_val; /* No link detected */
  349. mac->get_link_status = 0;
  350. /* Check if there was DownShift, must be checked
  351. * immediately after link-up */
  352. e1000e_check_downshift(hw);
  353. /* If we are forcing speed/duplex, then we simply return since
  354. * we have already determined whether we have link or not.
  355. */
  356. if (!mac->autoneg) {
  357. ret_val = -E1000_ERR_CONFIG;
  358. return ret_val;
  359. }
  360. /* Auto-Neg is enabled. Auto Speed Detection takes care
  361. * of MAC speed/duplex configuration. So we only need to
  362. * configure Collision Distance in the MAC.
  363. */
  364. e1000e_config_collision_dist(hw);
  365. /* Configure Flow Control now that Auto-Neg has completed.
  366. * First, we need to restore the desired flow control
  367. * settings because we may have had to re-autoneg with a
  368. * different link partner.
  369. */
  370. ret_val = e1000e_config_fc_after_link_up(hw);
  371. if (ret_val) {
  372. hw_dbg(hw, "Error configuring flow control\n");
  373. }
  374. return ret_val;
  375. }
  376. /**
  377. * e1000e_check_for_fiber_link - Check for link (Fiber)
  378. * @hw: pointer to the HW structure
  379. *
  380. * Checks for link up on the hardware. If link is not up and we have
  381. * a signal, then we need to force link up.
  382. **/
  383. s32 e1000e_check_for_fiber_link(struct e1000_hw *hw)
  384. {
  385. struct e1000_mac_info *mac = &hw->mac;
  386. u32 rxcw;
  387. u32 ctrl;
  388. u32 status;
  389. s32 ret_val;
  390. ctrl = er32(CTRL);
  391. status = er32(STATUS);
  392. rxcw = er32(RXCW);
  393. /* If we don't have link (auto-negotiation failed or link partner
  394. * cannot auto-negotiate), the cable is plugged in (we have signal),
  395. * and our link partner is not trying to auto-negotiate with us (we
  396. * are receiving idles or data), we need to force link up. We also
  397. * need to give auto-negotiation time to complete, in case the cable
  398. * was just plugged in. The autoneg_failed flag does this.
  399. */
  400. /* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
  401. if ((ctrl & E1000_CTRL_SWDPIN1) && (!(status & E1000_STATUS_LU)) &&
  402. (!(rxcw & E1000_RXCW_C))) {
  403. if (mac->autoneg_failed == 0) {
  404. mac->autoneg_failed = 1;
  405. return 0;
  406. }
  407. hw_dbg(hw, "NOT RXing /C/, disable AutoNeg and force link.\n");
  408. /* Disable auto-negotiation in the TXCW register */
  409. ew32(TXCW, (mac->txcw & ~E1000_TXCW_ANE));
  410. /* Force link-up and also force full-duplex. */
  411. ctrl = er32(CTRL);
  412. ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
  413. ew32(CTRL, ctrl);
  414. /* Configure Flow Control after forcing link up. */
  415. ret_val = e1000e_config_fc_after_link_up(hw);
  416. if (ret_val) {
  417. hw_dbg(hw, "Error configuring flow control\n");
  418. return ret_val;
  419. }
  420. } else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
  421. /* If we are forcing link and we are receiving /C/ ordered
  422. * sets, re-enable auto-negotiation in the TXCW register
  423. * and disable forced link in the Device Control register
  424. * in an attempt to auto-negotiate with our link partner.
  425. */
  426. hw_dbg(hw, "RXing /C/, enable AutoNeg and stop forcing link.\n");
  427. ew32(TXCW, mac->txcw);
  428. ew32(CTRL, (ctrl & ~E1000_CTRL_SLU));
  429. mac->serdes_has_link = 1;
  430. }
  431. return 0;
  432. }
  433. /**
  434. * e1000e_check_for_serdes_link - Check for link (Serdes)
  435. * @hw: pointer to the HW structure
  436. *
  437. * Checks for link up on the hardware. If link is not up and we have
  438. * a signal, then we need to force link up.
  439. **/
  440. s32 e1000e_check_for_serdes_link(struct e1000_hw *hw)
  441. {
  442. struct e1000_mac_info *mac = &hw->mac;
  443. u32 rxcw;
  444. u32 ctrl;
  445. u32 status;
  446. s32 ret_val;
  447. ctrl = er32(CTRL);
  448. status = er32(STATUS);
  449. rxcw = er32(RXCW);
  450. /* If we don't have link (auto-negotiation failed or link partner
  451. * cannot auto-negotiate), and our link partner is not trying to
  452. * auto-negotiate with us (we are receiving idles or data),
  453. * we need to force link up. We also need to give auto-negotiation
  454. * time to complete.
  455. */
  456. /* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
  457. if ((!(status & E1000_STATUS_LU)) && (!(rxcw & E1000_RXCW_C))) {
  458. if (mac->autoneg_failed == 0) {
  459. mac->autoneg_failed = 1;
  460. return 0;
  461. }
  462. hw_dbg(hw, "NOT RXing /C/, disable AutoNeg and force link.\n");
  463. /* Disable auto-negotiation in the TXCW register */
  464. ew32(TXCW, (mac->txcw & ~E1000_TXCW_ANE));
  465. /* Force link-up and also force full-duplex. */
  466. ctrl = er32(CTRL);
  467. ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
  468. ew32(CTRL, ctrl);
  469. /* Configure Flow Control after forcing link up. */
  470. ret_val = e1000e_config_fc_after_link_up(hw);
  471. if (ret_val) {
  472. hw_dbg(hw, "Error configuring flow control\n");
  473. return ret_val;
  474. }
  475. } else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
  476. /* If we are forcing link and we are receiving /C/ ordered
  477. * sets, re-enable auto-negotiation in the TXCW register
  478. * and disable forced link in the Device Control register
  479. * in an attempt to auto-negotiate with our link partner.
  480. */
  481. hw_dbg(hw, "RXing /C/, enable AutoNeg and stop forcing link.\n");
  482. ew32(TXCW, mac->txcw);
  483. ew32(CTRL, (ctrl & ~E1000_CTRL_SLU));
  484. mac->serdes_has_link = 1;
  485. } else if (!(E1000_TXCW_ANE & er32(TXCW))) {
  486. /* If we force link for non-auto-negotiation switch, check
  487. * link status based on MAC synchronization for internal
  488. * serdes media type.
  489. */
  490. /* SYNCH bit and IV bit are sticky. */
  491. udelay(10);
  492. if (E1000_RXCW_SYNCH & er32(RXCW)) {
  493. if (!(rxcw & E1000_RXCW_IV)) {
  494. mac->serdes_has_link = 1;
  495. hw_dbg(hw, "SERDES: Link is up.\n");
  496. }
  497. } else {
  498. mac->serdes_has_link = 0;
  499. hw_dbg(hw, "SERDES: Link is down.\n");
  500. }
  501. }
  502. if (E1000_TXCW_ANE & er32(TXCW)) {
  503. status = er32(STATUS);
  504. mac->serdes_has_link = (status & E1000_STATUS_LU);
  505. }
  506. return 0;
  507. }
  508. /**
  509. * e1000_set_default_fc_generic - Set flow control default values
  510. * @hw: pointer to the HW structure
  511. *
  512. * Read the EEPROM for the default values for flow control and store the
  513. * values.
  514. **/
  515. static s32 e1000_set_default_fc_generic(struct e1000_hw *hw)
  516. {
  517. struct e1000_mac_info *mac = &hw->mac;
  518. s32 ret_val;
  519. u16 nvm_data;
  520. if (mac->fc != e1000_fc_default)
  521. return 0;
  522. /* Read and store word 0x0F of the EEPROM. This word contains bits
  523. * that determine the hardware's default PAUSE (flow control) mode,
  524. * a bit that determines whether the HW defaults to enabling or
  525. * disabling auto-negotiation, and the direction of the
  526. * SW defined pins. If there is no SW over-ride of the flow
  527. * control setting, then the variable hw->fc will
  528. * be initialized based on a value in the EEPROM.
  529. */
  530. ret_val = e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &nvm_data);
  531. if (ret_val) {
  532. hw_dbg(hw, "NVM Read Error\n");
  533. return ret_val;
  534. }
  535. if ((nvm_data & NVM_WORD0F_PAUSE_MASK) == 0)
  536. mac->fc = e1000_fc_none;
  537. else if ((nvm_data & NVM_WORD0F_PAUSE_MASK) ==
  538. NVM_WORD0F_ASM_DIR)
  539. mac->fc = e1000_fc_tx_pause;
  540. else
  541. mac->fc = e1000_fc_full;
  542. return 0;
  543. }
  544. /**
  545. * e1000e_setup_link - Setup flow control and link settings
  546. * @hw: pointer to the HW structure
  547. *
  548. * Determines which flow control settings to use, then configures flow
  549. * control. Calls the appropriate media-specific link configuration
  550. * function. Assuming the adapter has a valid link partner, a valid link
  551. * should be established. Assumes the hardware has previously been reset
  552. * and the transmitter and receiver are not enabled.
  553. **/
  554. s32 e1000e_setup_link(struct e1000_hw *hw)
  555. {
  556. struct e1000_mac_info *mac = &hw->mac;
  557. s32 ret_val;
  558. /* In the case of the phy reset being blocked, we already have a link.
  559. * We do not need to set it up again.
  560. */
  561. if (e1000_check_reset_block(hw))
  562. return 0;
  563. /*
  564. * If flow control is set to default, set flow control based on
  565. * the EEPROM flow control settings.
  566. */
  567. if (mac->fc == e1000_fc_default) {
  568. ret_val = e1000_set_default_fc_generic(hw);
  569. if (ret_val)
  570. return ret_val;
  571. }
  572. /* We want to save off the original Flow Control configuration just
  573. * in case we get disconnected and then reconnected into a different
  574. * hub or switch with different Flow Control capabilities.
  575. */
  576. mac->original_fc = mac->fc;
  577. hw_dbg(hw, "After fix-ups FlowControl is now = %x\n", mac->fc);
  578. /* Call the necessary media_type subroutine to configure the link. */
  579. ret_val = mac->ops.setup_physical_interface(hw);
  580. if (ret_val)
  581. return ret_val;
  582. /* Initialize the flow control address, type, and PAUSE timer
  583. * registers to their default values. This is done even if flow
  584. * control is disabled, because it does not hurt anything to
  585. * initialize these registers.
  586. */
  587. hw_dbg(hw, "Initializing the Flow Control address, type and timer regs\n");
  588. ew32(FCT, FLOW_CONTROL_TYPE);
  589. ew32(FCAH, FLOW_CONTROL_ADDRESS_HIGH);
  590. ew32(FCAL, FLOW_CONTROL_ADDRESS_LOW);
  591. ew32(FCTTV, mac->fc_pause_time);
  592. return e1000e_set_fc_watermarks(hw);
  593. }
  594. /**
  595. * e1000_commit_fc_settings_generic - Configure flow control
  596. * @hw: pointer to the HW structure
  597. *
  598. * Write the flow control settings to the Transmit Config Word Register (TXCW)
  599. * base on the flow control settings in e1000_mac_info.
  600. **/
  601. static s32 e1000_commit_fc_settings_generic(struct e1000_hw *hw)
  602. {
  603. struct e1000_mac_info *mac = &hw->mac;
  604. u32 txcw;
  605. /* Check for a software override of the flow control settings, and
  606. * setup the device accordingly. If auto-negotiation is enabled, then
  607. * software will have to set the "PAUSE" bits to the correct value in
  608. * the Transmit Config Word Register (TXCW) and re-start auto-
  609. * negotiation. However, if auto-negotiation is disabled, then
  610. * software will have to manually configure the two flow control enable
  611. * bits in the CTRL register.
  612. *
  613. * The possible values of the "fc" parameter are:
  614. * 0: Flow control is completely disabled
  615. * 1: Rx flow control is enabled (we can receive pause frames,
  616. * but not send pause frames).
  617. * 2: Tx flow control is enabled (we can send pause frames but we
  618. * do not support receiving pause frames).
  619. * 3: Both Rx and TX flow control (symmetric) are enabled.
  620. */
  621. switch (mac->fc) {
  622. case e1000_fc_none:
  623. /* Flow control completely disabled by a software over-ride. */
  624. txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
  625. break;
  626. case e1000_fc_rx_pause:
  627. /* RX Flow control is enabled and TX Flow control is disabled
  628. * by a software over-ride. Since there really isn't a way to
  629. * advertise that we are capable of RX Pause ONLY, we will
  630. * advertise that we support both symmetric and asymmetric RX
  631. * PAUSE. Later, we will disable the adapter's ability to send
  632. * PAUSE frames.
  633. */
  634. txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
  635. break;
  636. case e1000_fc_tx_pause:
  637. /* TX Flow control is enabled, and RX Flow control is disabled,
  638. * by a software over-ride.
  639. */
  640. txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
  641. break;
  642. case e1000_fc_full:
  643. /* Flow control (both RX and TX) is enabled by a software
  644. * over-ride.
  645. */
  646. txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
  647. break;
  648. default:
  649. hw_dbg(hw, "Flow control param set incorrectly\n");
  650. return -E1000_ERR_CONFIG;
  651. break;
  652. }
  653. ew32(TXCW, txcw);
  654. mac->txcw = txcw;
  655. return 0;
  656. }
  657. /**
  658. * e1000_poll_fiber_serdes_link_generic - Poll for link up
  659. * @hw: pointer to the HW structure
  660. *
  661. * Polls for link up by reading the status register, if link fails to come
  662. * up with auto-negotiation, then the link is forced if a signal is detected.
  663. **/
  664. static s32 e1000_poll_fiber_serdes_link_generic(struct e1000_hw *hw)
  665. {
  666. struct e1000_mac_info *mac = &hw->mac;
  667. u32 i, status;
  668. s32 ret_val;
  669. /* If we have a signal (the cable is plugged in, or assumed true for
  670. * serdes media) then poll for a "Link-Up" indication in the Device
  671. * Status Register. Time-out if a link isn't seen in 500 milliseconds
  672. * seconds (Auto-negotiation should complete in less than 500
  673. * milliseconds even if the other end is doing it in SW).
  674. */
  675. for (i = 0; i < FIBER_LINK_UP_LIMIT; i++) {
  676. msleep(10);
  677. status = er32(STATUS);
  678. if (status & E1000_STATUS_LU)
  679. break;
  680. }
  681. if (i == FIBER_LINK_UP_LIMIT) {
  682. hw_dbg(hw, "Never got a valid link from auto-neg!!!\n");
  683. mac->autoneg_failed = 1;
  684. /* AutoNeg failed to achieve a link, so we'll call
  685. * mac->check_for_link. This routine will force the
  686. * link up if we detect a signal. This will allow us to
  687. * communicate with non-autonegotiating link partners.
  688. */
  689. ret_val = mac->ops.check_for_link(hw);
  690. if (ret_val) {
  691. hw_dbg(hw, "Error while checking for link\n");
  692. return ret_val;
  693. }
  694. mac->autoneg_failed = 0;
  695. } else {
  696. mac->autoneg_failed = 0;
  697. hw_dbg(hw, "Valid Link Found\n");
  698. }
  699. return 0;
  700. }
  701. /**
  702. * e1000e_setup_fiber_serdes_link - Setup link for fiber/serdes
  703. * @hw: pointer to the HW structure
  704. *
  705. * Configures collision distance and flow control for fiber and serdes
  706. * links. Upon successful setup, poll for link.
  707. **/
  708. s32 e1000e_setup_fiber_serdes_link(struct e1000_hw *hw)
  709. {
  710. u32 ctrl;
  711. s32 ret_val;
  712. ctrl = er32(CTRL);
  713. /* Take the link out of reset */
  714. ctrl &= ~E1000_CTRL_LRST;
  715. e1000e_config_collision_dist(hw);
  716. ret_val = e1000_commit_fc_settings_generic(hw);
  717. if (ret_val)
  718. return ret_val;
  719. /* Since auto-negotiation is enabled, take the link out of reset (the
  720. * link will be in reset, because we previously reset the chip). This
  721. * will restart auto-negotiation. If auto-negotiation is successful
  722. * then the link-up status bit will be set and the flow control enable
  723. * bits (RFCE and TFCE) will be set according to their negotiated value.
  724. */
  725. hw_dbg(hw, "Auto-negotiation enabled\n");
  726. ew32(CTRL, ctrl);
  727. e1e_flush();
  728. msleep(1);
  729. /* For these adapters, the SW defineable pin 1 is set when the optics
  730. * detect a signal. If we have a signal, then poll for a "Link-Up"
  731. * indication.
  732. */
  733. if (hw->media_type == e1000_media_type_internal_serdes ||
  734. (er32(CTRL) & E1000_CTRL_SWDPIN1)) {
  735. ret_val = e1000_poll_fiber_serdes_link_generic(hw);
  736. } else {
  737. hw_dbg(hw, "No signal detected\n");
  738. }
  739. return 0;
  740. }
  741. /**
  742. * e1000e_config_collision_dist - Configure collision distance
  743. * @hw: pointer to the HW structure
  744. *
  745. * Configures the collision distance to the default value and is used
  746. * during link setup. Currently no func pointer exists and all
  747. * implementations are handled in the generic version of this function.
  748. **/
  749. void e1000e_config_collision_dist(struct e1000_hw *hw)
  750. {
  751. u32 tctl;
  752. tctl = er32(TCTL);
  753. tctl &= ~E1000_TCTL_COLD;
  754. tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
  755. ew32(TCTL, tctl);
  756. e1e_flush();
  757. }
  758. /**
  759. * e1000e_set_fc_watermarks - Set flow control high/low watermarks
  760. * @hw: pointer to the HW structure
  761. *
  762. * Sets the flow control high/low threshold (watermark) registers. If
  763. * flow control XON frame transmission is enabled, then set XON frame
  764. * tansmission as well.
  765. **/
  766. s32 e1000e_set_fc_watermarks(struct e1000_hw *hw)
  767. {
  768. struct e1000_mac_info *mac = &hw->mac;
  769. u32 fcrtl = 0, fcrth = 0;
  770. /* Set the flow control receive threshold registers. Normally,
  771. * these registers will be set to a default threshold that may be
  772. * adjusted later by the driver's runtime code. However, if the
  773. * ability to transmit pause frames is not enabled, then these
  774. * registers will be set to 0.
  775. */
  776. if (mac->fc & e1000_fc_tx_pause) {
  777. /* We need to set up the Receive Threshold high and low water
  778. * marks as well as (optionally) enabling the transmission of
  779. * XON frames.
  780. */
  781. fcrtl = mac->fc_low_water;
  782. fcrtl |= E1000_FCRTL_XONE;
  783. fcrth = mac->fc_high_water;
  784. }
  785. ew32(FCRTL, fcrtl);
  786. ew32(FCRTH, fcrth);
  787. return 0;
  788. }
  789. /**
  790. * e1000e_force_mac_fc - Force the MAC's flow control settings
  791. * @hw: pointer to the HW structure
  792. *
  793. * Force the MAC's flow control settings. Sets the TFCE and RFCE bits in the
  794. * device control register to reflect the adapter settings. TFCE and RFCE
  795. * need to be explicitly set by software when a copper PHY is used because
  796. * autonegotiation is managed by the PHY rather than the MAC. Software must
  797. * also configure these bits when link is forced on a fiber connection.
  798. **/
  799. s32 e1000e_force_mac_fc(struct e1000_hw *hw)
  800. {
  801. struct e1000_mac_info *mac = &hw->mac;
  802. u32 ctrl;
  803. ctrl = er32(CTRL);
  804. /* Because we didn't get link via the internal auto-negotiation
  805. * mechanism (we either forced link or we got link via PHY
  806. * auto-neg), we have to manually enable/disable transmit an
  807. * receive flow control.
  808. *
  809. * The "Case" statement below enables/disable flow control
  810. * according to the "mac->fc" parameter.
  811. *
  812. * The possible values of the "fc" parameter are:
  813. * 0: Flow control is completely disabled
  814. * 1: Rx flow control is enabled (we can receive pause
  815. * frames but not send pause frames).
  816. * 2: Tx flow control is enabled (we can send pause frames
  817. * frames but we do not receive pause frames).
  818. * 3: Both Rx and TX flow control (symmetric) is enabled.
  819. * other: No other values should be possible at this point.
  820. */
  821. hw_dbg(hw, "mac->fc = %u\n", mac->fc);
  822. switch (mac->fc) {
  823. case e1000_fc_none:
  824. ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
  825. break;
  826. case e1000_fc_rx_pause:
  827. ctrl &= (~E1000_CTRL_TFCE);
  828. ctrl |= E1000_CTRL_RFCE;
  829. break;
  830. case e1000_fc_tx_pause:
  831. ctrl &= (~E1000_CTRL_RFCE);
  832. ctrl |= E1000_CTRL_TFCE;
  833. break;
  834. case e1000_fc_full:
  835. ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
  836. break;
  837. default:
  838. hw_dbg(hw, "Flow control param set incorrectly\n");
  839. return -E1000_ERR_CONFIG;
  840. }
  841. ew32(CTRL, ctrl);
  842. return 0;
  843. }
  844. /**
  845. * e1000e_config_fc_after_link_up - Configures flow control after link
  846. * @hw: pointer to the HW structure
  847. *
  848. * Checks the status of auto-negotiation after link up to ensure that the
  849. * speed and duplex were not forced. If the link needed to be forced, then
  850. * flow control needs to be forced also. If auto-negotiation is enabled
  851. * and did not fail, then we configure flow control based on our link
  852. * partner.
  853. **/
  854. s32 e1000e_config_fc_after_link_up(struct e1000_hw *hw)
  855. {
  856. struct e1000_mac_info *mac = &hw->mac;
  857. s32 ret_val = 0;
  858. u16 mii_status_reg, mii_nway_adv_reg, mii_nway_lp_ability_reg;
  859. u16 speed, duplex;
  860. /* Check for the case where we have fiber media and auto-neg failed
  861. * so we had to force link. In this case, we need to force the
  862. * configuration of the MAC to match the "fc" parameter.
  863. */
  864. if (mac->autoneg_failed) {
  865. if (hw->media_type == e1000_media_type_fiber ||
  866. hw->media_type == e1000_media_type_internal_serdes)
  867. ret_val = e1000e_force_mac_fc(hw);
  868. } else {
  869. if (hw->media_type == e1000_media_type_copper)
  870. ret_val = e1000e_force_mac_fc(hw);
  871. }
  872. if (ret_val) {
  873. hw_dbg(hw, "Error forcing flow control settings\n");
  874. return ret_val;
  875. }
  876. /* Check for the case where we have copper media and auto-neg is
  877. * enabled. In this case, we need to check and see if Auto-Neg
  878. * has completed, and if so, how the PHY and link partner has
  879. * flow control configured.
  880. */
  881. if ((hw->media_type == e1000_media_type_copper) && mac->autoneg) {
  882. /* Read the MII Status Register and check to see if AutoNeg
  883. * has completed. We read this twice because this reg has
  884. * some "sticky" (latched) bits.
  885. */
  886. ret_val = e1e_rphy(hw, PHY_STATUS, &mii_status_reg);
  887. if (ret_val)
  888. return ret_val;
  889. ret_val = e1e_rphy(hw, PHY_STATUS, &mii_status_reg);
  890. if (ret_val)
  891. return ret_val;
  892. if (!(mii_status_reg & MII_SR_AUTONEG_COMPLETE)) {
  893. hw_dbg(hw, "Copper PHY and Auto Neg "
  894. "has not completed.\n");
  895. return ret_val;
  896. }
  897. /* The AutoNeg process has completed, so we now need to
  898. * read both the Auto Negotiation Advertisement
  899. * Register (Address 4) and the Auto_Negotiation Base
  900. * Page Ability Register (Address 5) to determine how
  901. * flow control was negotiated.
  902. */
  903. ret_val = e1e_rphy(hw, PHY_AUTONEG_ADV, &mii_nway_adv_reg);
  904. if (ret_val)
  905. return ret_val;
  906. ret_val = e1e_rphy(hw, PHY_LP_ABILITY, &mii_nway_lp_ability_reg);
  907. if (ret_val)
  908. return ret_val;
  909. /* Two bits in the Auto Negotiation Advertisement Register
  910. * (Address 4) and two bits in the Auto Negotiation Base
  911. * Page Ability Register (Address 5) determine flow control
  912. * for both the PHY and the link partner. The following
  913. * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
  914. * 1999, describes these PAUSE resolution bits and how flow
  915. * control is determined based upon these settings.
  916. * NOTE: DC = Don't Care
  917. *
  918. * LOCAL DEVICE | LINK PARTNER
  919. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
  920. *-------|---------|-------|---------|--------------------
  921. * 0 | 0 | DC | DC | e1000_fc_none
  922. * 0 | 1 | 0 | DC | e1000_fc_none
  923. * 0 | 1 | 1 | 0 | e1000_fc_none
  924. * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
  925. * 1 | 0 | 0 | DC | e1000_fc_none
  926. * 1 | DC | 1 | DC | e1000_fc_full
  927. * 1 | 1 | 0 | 0 | e1000_fc_none
  928. * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
  929. *
  930. */
  931. /* Are both PAUSE bits set to 1? If so, this implies
  932. * Symmetric Flow Control is enabled at both ends. The
  933. * ASM_DIR bits are irrelevant per the spec.
  934. *
  935. * For Symmetric Flow Control:
  936. *
  937. * LOCAL DEVICE | LINK PARTNER
  938. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  939. *-------|---------|-------|---------|--------------------
  940. * 1 | DC | 1 | DC | E1000_fc_full
  941. *
  942. */
  943. if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
  944. (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
  945. /* Now we need to check if the user selected RX ONLY
  946. * of pause frames. In this case, we had to advertise
  947. * FULL flow control because we could not advertise RX
  948. * ONLY. Hence, we must now check to see if we need to
  949. * turn OFF the TRANSMISSION of PAUSE frames.
  950. */
  951. if (mac->original_fc == e1000_fc_full) {
  952. mac->fc = e1000_fc_full;
  953. hw_dbg(hw, "Flow Control = FULL.\r\n");
  954. } else {
  955. mac->fc = e1000_fc_rx_pause;
  956. hw_dbg(hw, "Flow Control = "
  957. "RX PAUSE frames only.\r\n");
  958. }
  959. }
  960. /* For receiving PAUSE frames ONLY.
  961. *
  962. * LOCAL DEVICE | LINK PARTNER
  963. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  964. *-------|---------|-------|---------|--------------------
  965. * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
  966. *
  967. */
  968. else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
  969. (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
  970. (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
  971. (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
  972. mac->fc = e1000_fc_tx_pause;
  973. hw_dbg(hw, "Flow Control = TX PAUSE frames only.\r\n");
  974. }
  975. /* For transmitting PAUSE frames ONLY.
  976. *
  977. * LOCAL DEVICE | LINK PARTNER
  978. * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
  979. *-------|---------|-------|---------|--------------------
  980. * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
  981. *
  982. */
  983. else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
  984. (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
  985. !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
  986. (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
  987. mac->fc = e1000_fc_rx_pause;
  988. hw_dbg(hw, "Flow Control = RX PAUSE frames only.\r\n");
  989. }
  990. /* Per the IEEE spec, at this point flow control should be
  991. * disabled. However, we want to consider that we could
  992. * be connected to a legacy switch that doesn't advertise
  993. * desired flow control, but can be forced on the link
  994. * partner. So if we advertised no flow control, that is
  995. * what we will resolve to. If we advertised some kind of
  996. * receive capability (Rx Pause Only or Full Flow Control)
  997. * and the link partner advertised none, we will configure
  998. * ourselves to enable Rx Flow Control only. We can do
  999. * this safely for two reasons: If the link partner really
  1000. * didn't want flow control enabled, and we enable Rx, no
  1001. * harm done since we won't be receiving any PAUSE frames
  1002. * anyway. If the intent on the link partner was to have
  1003. * flow control enabled, then by us enabling RX only, we
  1004. * can at least receive pause frames and process them.
  1005. * This is a good idea because in most cases, since we are
  1006. * predominantly a server NIC, more times than not we will
  1007. * be asked to delay transmission of packets than asking
  1008. * our link partner to pause transmission of frames.
  1009. */
  1010. else if ((mac->original_fc == e1000_fc_none) ||
  1011. (mac->original_fc == e1000_fc_tx_pause)) {
  1012. mac->fc = e1000_fc_none;
  1013. hw_dbg(hw, "Flow Control = NONE.\r\n");
  1014. } else {
  1015. mac->fc = e1000_fc_rx_pause;
  1016. hw_dbg(hw, "Flow Control = RX PAUSE frames only.\r\n");
  1017. }
  1018. /* Now we need to do one last check... If we auto-
  1019. * negotiated to HALF DUPLEX, flow control should not be
  1020. * enabled per IEEE 802.3 spec.
  1021. */
  1022. ret_val = mac->ops.get_link_up_info(hw, &speed, &duplex);
  1023. if (ret_val) {
  1024. hw_dbg(hw, "Error getting link speed and duplex\n");
  1025. return ret_val;
  1026. }
  1027. if (duplex == HALF_DUPLEX)
  1028. mac->fc = e1000_fc_none;
  1029. /* Now we call a subroutine to actually force the MAC
  1030. * controller to use the correct flow control settings.
  1031. */
  1032. ret_val = e1000e_force_mac_fc(hw);
  1033. if (ret_val) {
  1034. hw_dbg(hw, "Error forcing flow control settings\n");
  1035. return ret_val;
  1036. }
  1037. }
  1038. return 0;
  1039. }
  1040. /**
  1041. * e1000e_get_speed_and_duplex_copper - Retreive current speed/duplex
  1042. * @hw: pointer to the HW structure
  1043. * @speed: stores the current speed
  1044. * @duplex: stores the current duplex
  1045. *
  1046. * Read the status register for the current speed/duplex and store the current
  1047. * speed and duplex for copper connections.
  1048. **/
  1049. s32 e1000e_get_speed_and_duplex_copper(struct e1000_hw *hw, u16 *speed, u16 *duplex)
  1050. {
  1051. u32 status;
  1052. status = er32(STATUS);
  1053. if (status & E1000_STATUS_SPEED_1000) {
  1054. *speed = SPEED_1000;
  1055. hw_dbg(hw, "1000 Mbs, ");
  1056. } else if (status & E1000_STATUS_SPEED_100) {
  1057. *speed = SPEED_100;
  1058. hw_dbg(hw, "100 Mbs, ");
  1059. } else {
  1060. *speed = SPEED_10;
  1061. hw_dbg(hw, "10 Mbs, ");
  1062. }
  1063. if (status & E1000_STATUS_FD) {
  1064. *duplex = FULL_DUPLEX;
  1065. hw_dbg(hw, "Full Duplex\n");
  1066. } else {
  1067. *duplex = HALF_DUPLEX;
  1068. hw_dbg(hw, "Half Duplex\n");
  1069. }
  1070. return 0;
  1071. }
  1072. /**
  1073. * e1000e_get_speed_and_duplex_fiber_serdes - Retreive current speed/duplex
  1074. * @hw: pointer to the HW structure
  1075. * @speed: stores the current speed
  1076. * @duplex: stores the current duplex
  1077. *
  1078. * Sets the speed and duplex to gigabit full duplex (the only possible option)
  1079. * for fiber/serdes links.
  1080. **/
  1081. s32 e1000e_get_speed_and_duplex_fiber_serdes(struct e1000_hw *hw, u16 *speed, u16 *duplex)
  1082. {
  1083. *speed = SPEED_1000;
  1084. *duplex = FULL_DUPLEX;
  1085. return 0;
  1086. }
  1087. /**
  1088. * e1000e_get_hw_semaphore - Acquire hardware semaphore
  1089. * @hw: pointer to the HW structure
  1090. *
  1091. * Acquire the HW semaphore to access the PHY or NVM
  1092. **/
  1093. s32 e1000e_get_hw_semaphore(struct e1000_hw *hw)
  1094. {
  1095. u32 swsm;
  1096. s32 timeout = hw->nvm.word_size + 1;
  1097. s32 i = 0;
  1098. /* Get the SW semaphore */
  1099. while (i < timeout) {
  1100. swsm = er32(SWSM);
  1101. if (!(swsm & E1000_SWSM_SMBI))
  1102. break;
  1103. udelay(50);
  1104. i++;
  1105. }
  1106. if (i == timeout) {
  1107. hw_dbg(hw, "Driver can't access device - SMBI bit is set.\n");
  1108. return -E1000_ERR_NVM;
  1109. }
  1110. /* Get the FW semaphore. */
  1111. for (i = 0; i < timeout; i++) {
  1112. swsm = er32(SWSM);
  1113. ew32(SWSM, swsm | E1000_SWSM_SWESMBI);
  1114. /* Semaphore acquired if bit latched */
  1115. if (er32(SWSM) & E1000_SWSM_SWESMBI)
  1116. break;
  1117. udelay(50);
  1118. }
  1119. if (i == timeout) {
  1120. /* Release semaphores */
  1121. e1000e_put_hw_semaphore(hw);
  1122. hw_dbg(hw, "Driver can't access the NVM\n");
  1123. return -E1000_ERR_NVM;
  1124. }
  1125. return 0;
  1126. }
  1127. /**
  1128. * e1000e_put_hw_semaphore - Release hardware semaphore
  1129. * @hw: pointer to the HW structure
  1130. *
  1131. * Release hardware semaphore used to access the PHY or NVM
  1132. **/
  1133. void e1000e_put_hw_semaphore(struct e1000_hw *hw)
  1134. {
  1135. u32 swsm;
  1136. swsm = er32(SWSM);
  1137. swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
  1138. ew32(SWSM, swsm);
  1139. }
  1140. /**
  1141. * e1000e_get_auto_rd_done - Check for auto read completion
  1142. * @hw: pointer to the HW structure
  1143. *
  1144. * Check EEPROM for Auto Read done bit.
  1145. **/
  1146. s32 e1000e_get_auto_rd_done(struct e1000_hw *hw)
  1147. {
  1148. s32 i = 0;
  1149. while (i < AUTO_READ_DONE_TIMEOUT) {
  1150. if (er32(EECD) & E1000_EECD_AUTO_RD)
  1151. break;
  1152. msleep(1);
  1153. i++;
  1154. }
  1155. if (i == AUTO_READ_DONE_TIMEOUT) {
  1156. hw_dbg(hw, "Auto read by HW from NVM has not completed.\n");
  1157. return -E1000_ERR_RESET;
  1158. }
  1159. return 0;
  1160. }
  1161. /**
  1162. * e1000e_valid_led_default - Verify a valid default LED config
  1163. * @hw: pointer to the HW structure
  1164. * @data: pointer to the NVM (EEPROM)
  1165. *
  1166. * Read the EEPROM for the current default LED configuration. If the
  1167. * LED configuration is not valid, set to a valid LED configuration.
  1168. **/
  1169. s32 e1000e_valid_led_default(struct e1000_hw *hw, u16 *data)
  1170. {
  1171. s32 ret_val;
  1172. ret_val = e1000_read_nvm(hw, NVM_ID_LED_SETTINGS, 1, data);
  1173. if (ret_val) {
  1174. hw_dbg(hw, "NVM Read Error\n");
  1175. return ret_val;
  1176. }
  1177. if (*data == ID_LED_RESERVED_0000 || *data == ID_LED_RESERVED_FFFF)
  1178. *data = ID_LED_DEFAULT;
  1179. return 0;
  1180. }
  1181. /**
  1182. * e1000e_id_led_init -
  1183. * @hw: pointer to the HW structure
  1184. *
  1185. **/
  1186. s32 e1000e_id_led_init(struct e1000_hw *hw)
  1187. {
  1188. struct e1000_mac_info *mac = &hw->mac;
  1189. s32 ret_val;
  1190. const u32 ledctl_mask = 0x000000FF;
  1191. const u32 ledctl_on = E1000_LEDCTL_MODE_LED_ON;
  1192. const u32 ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
  1193. u16 data, i, temp;
  1194. const u16 led_mask = 0x0F;
  1195. ret_val = hw->nvm.ops.valid_led_default(hw, &data);
  1196. if (ret_val)
  1197. return ret_val;
  1198. mac->ledctl_default = er32(LEDCTL);
  1199. mac->ledctl_mode1 = mac->ledctl_default;
  1200. mac->ledctl_mode2 = mac->ledctl_default;
  1201. for (i = 0; i < 4; i++) {
  1202. temp = (data >> (i << 2)) & led_mask;
  1203. switch (temp) {
  1204. case ID_LED_ON1_DEF2:
  1205. case ID_LED_ON1_ON2:
  1206. case ID_LED_ON1_OFF2:
  1207. mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
  1208. mac->ledctl_mode1 |= ledctl_on << (i << 3);
  1209. break;
  1210. case ID_LED_OFF1_DEF2:
  1211. case ID_LED_OFF1_ON2:
  1212. case ID_LED_OFF1_OFF2:
  1213. mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
  1214. mac->ledctl_mode1 |= ledctl_off << (i << 3);
  1215. break;
  1216. default:
  1217. /* Do nothing */
  1218. break;
  1219. }
  1220. switch (temp) {
  1221. case ID_LED_DEF1_ON2:
  1222. case ID_LED_ON1_ON2:
  1223. case ID_LED_OFF1_ON2:
  1224. mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
  1225. mac->ledctl_mode2 |= ledctl_on << (i << 3);
  1226. break;
  1227. case ID_LED_DEF1_OFF2:
  1228. case ID_LED_ON1_OFF2:
  1229. case ID_LED_OFF1_OFF2:
  1230. mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
  1231. mac->ledctl_mode2 |= ledctl_off << (i << 3);
  1232. break;
  1233. default:
  1234. /* Do nothing */
  1235. break;
  1236. }
  1237. }
  1238. return 0;
  1239. }
  1240. /**
  1241. * e1000e_cleanup_led_generic - Set LED config to default operation
  1242. * @hw: pointer to the HW structure
  1243. *
  1244. * Remove the current LED configuration and set the LED configuration
  1245. * to the default value, saved from the EEPROM.
  1246. **/
  1247. s32 e1000e_cleanup_led_generic(struct e1000_hw *hw)
  1248. {
  1249. ew32(LEDCTL, hw->mac.ledctl_default);
  1250. return 0;
  1251. }
  1252. /**
  1253. * e1000e_blink_led - Blink LED
  1254. * @hw: pointer to the HW structure
  1255. *
  1256. * Blink the led's which are set to be on.
  1257. **/
  1258. s32 e1000e_blink_led(struct e1000_hw *hw)
  1259. {
  1260. u32 ledctl_blink = 0;
  1261. u32 i;
  1262. if (hw->media_type == e1000_media_type_fiber) {
  1263. /* always blink LED0 for PCI-E fiber */
  1264. ledctl_blink = E1000_LEDCTL_LED0_BLINK |
  1265. (E1000_LEDCTL_MODE_LED_ON << E1000_LEDCTL_LED0_MODE_SHIFT);
  1266. } else {
  1267. /* set the blink bit for each LED that's "on" (0x0E)
  1268. * in ledctl_mode2 */
  1269. ledctl_blink = hw->mac.ledctl_mode2;
  1270. for (i = 0; i < 4; i++)
  1271. if (((hw->mac.ledctl_mode2 >> (i * 8)) & 0xFF) ==
  1272. E1000_LEDCTL_MODE_LED_ON)
  1273. ledctl_blink |= (E1000_LEDCTL_LED0_BLINK <<
  1274. (i * 8));
  1275. }
  1276. ew32(LEDCTL, ledctl_blink);
  1277. return 0;
  1278. }
  1279. /**
  1280. * e1000e_led_on_generic - Turn LED on
  1281. * @hw: pointer to the HW structure
  1282. *
  1283. * Turn LED on.
  1284. **/
  1285. s32 e1000e_led_on_generic(struct e1000_hw *hw)
  1286. {
  1287. u32 ctrl;
  1288. switch (hw->media_type) {
  1289. case e1000_media_type_fiber:
  1290. ctrl = er32(CTRL);
  1291. ctrl &= ~E1000_CTRL_SWDPIN0;
  1292. ctrl |= E1000_CTRL_SWDPIO0;
  1293. ew32(CTRL, ctrl);
  1294. break;
  1295. case e1000_media_type_copper:
  1296. ew32(LEDCTL, hw->mac.ledctl_mode2);
  1297. break;
  1298. default:
  1299. break;
  1300. }
  1301. return 0;
  1302. }
  1303. /**
  1304. * e1000e_led_off_generic - Turn LED off
  1305. * @hw: pointer to the HW structure
  1306. *
  1307. * Turn LED off.
  1308. **/
  1309. s32 e1000e_led_off_generic(struct e1000_hw *hw)
  1310. {
  1311. u32 ctrl;
  1312. switch (hw->media_type) {
  1313. case e1000_media_type_fiber:
  1314. ctrl = er32(CTRL);
  1315. ctrl |= E1000_CTRL_SWDPIN0;
  1316. ctrl |= E1000_CTRL_SWDPIO0;
  1317. ew32(CTRL, ctrl);
  1318. break;
  1319. case e1000_media_type_copper:
  1320. ew32(LEDCTL, hw->mac.ledctl_mode1);
  1321. break;
  1322. default:
  1323. break;
  1324. }
  1325. return 0;
  1326. }
  1327. /**
  1328. * e1000e_set_pcie_no_snoop - Set PCI-express capabilities
  1329. * @hw: pointer to the HW structure
  1330. * @no_snoop: bitmap of snoop events
  1331. *
  1332. * Set the PCI-express register to snoop for events enabled in 'no_snoop'.
  1333. **/
  1334. void e1000e_set_pcie_no_snoop(struct e1000_hw *hw, u32 no_snoop)
  1335. {
  1336. u32 gcr;
  1337. if (no_snoop) {
  1338. gcr = er32(GCR);
  1339. gcr &= ~(PCIE_NO_SNOOP_ALL);
  1340. gcr |= no_snoop;
  1341. ew32(GCR, gcr);
  1342. }
  1343. }
  1344. /**
  1345. * e1000e_disable_pcie_master - Disables PCI-express master access
  1346. * @hw: pointer to the HW structure
  1347. *
  1348. * Returns 0 if successful, else returns -10
  1349. * (-E1000_ERR_MASTER_REQUESTS_PENDING) if master disable bit has not casued
  1350. * the master requests to be disabled.
  1351. *
  1352. * Disables PCI-Express master access and verifies there are no pending
  1353. * requests.
  1354. **/
  1355. s32 e1000e_disable_pcie_master(struct e1000_hw *hw)
  1356. {
  1357. u32 ctrl;
  1358. s32 timeout = MASTER_DISABLE_TIMEOUT;
  1359. ctrl = er32(CTRL);
  1360. ctrl |= E1000_CTRL_GIO_MASTER_DISABLE;
  1361. ew32(CTRL, ctrl);
  1362. while (timeout) {
  1363. if (!(er32(STATUS) &
  1364. E1000_STATUS_GIO_MASTER_ENABLE))
  1365. break;
  1366. udelay(100);
  1367. timeout--;
  1368. }
  1369. if (!timeout) {
  1370. hw_dbg(hw, "Master requests are pending.\n");
  1371. return -E1000_ERR_MASTER_REQUESTS_PENDING;
  1372. }
  1373. return 0;
  1374. }
  1375. /**
  1376. * e1000e_reset_adaptive - Reset Adaptive Interframe Spacing
  1377. * @hw: pointer to the HW structure
  1378. *
  1379. * Reset the Adaptive Interframe Spacing throttle to default values.
  1380. **/
  1381. void e1000e_reset_adaptive(struct e1000_hw *hw)
  1382. {
  1383. struct e1000_mac_info *mac = &hw->mac;
  1384. mac->current_ifs_val = 0;
  1385. mac->ifs_min_val = IFS_MIN;
  1386. mac->ifs_max_val = IFS_MAX;
  1387. mac->ifs_step_size = IFS_STEP;
  1388. mac->ifs_ratio = IFS_RATIO;
  1389. mac->in_ifs_mode = 0;
  1390. ew32(AIT, 0);
  1391. }
  1392. /**
  1393. * e1000e_update_adaptive - Update Adaptive Interframe Spacing
  1394. * @hw: pointer to the HW structure
  1395. *
  1396. * Update the Adaptive Interframe Spacing Throttle value based on the
  1397. * time between transmitted packets and time between collisions.
  1398. **/
  1399. void e1000e_update_adaptive(struct e1000_hw *hw)
  1400. {
  1401. struct e1000_mac_info *mac = &hw->mac;
  1402. if ((mac->collision_delta * mac->ifs_ratio) > mac->tx_packet_delta) {
  1403. if (mac->tx_packet_delta > MIN_NUM_XMITS) {
  1404. mac->in_ifs_mode = 1;
  1405. if (mac->current_ifs_val < mac->ifs_max_val) {
  1406. if (!mac->current_ifs_val)
  1407. mac->current_ifs_val = mac->ifs_min_val;
  1408. else
  1409. mac->current_ifs_val +=
  1410. mac->ifs_step_size;
  1411. ew32(AIT,
  1412. mac->current_ifs_val);
  1413. }
  1414. }
  1415. } else {
  1416. if (mac->in_ifs_mode &&
  1417. (mac->tx_packet_delta <= MIN_NUM_XMITS)) {
  1418. mac->current_ifs_val = 0;
  1419. mac->in_ifs_mode = 0;
  1420. ew32(AIT, 0);
  1421. }
  1422. }
  1423. }
  1424. /**
  1425. * e1000_raise_eec_clk - Raise EEPROM clock
  1426. * @hw: pointer to the HW structure
  1427. * @eecd: pointer to the EEPROM
  1428. *
  1429. * Enable/Raise the EEPROM clock bit.
  1430. **/
  1431. static void e1000_raise_eec_clk(struct e1000_hw *hw, u32 *eecd)
  1432. {
  1433. *eecd = *eecd | E1000_EECD_SK;
  1434. ew32(EECD, *eecd);
  1435. e1e_flush();
  1436. udelay(hw->nvm.delay_usec);
  1437. }
  1438. /**
  1439. * e1000_lower_eec_clk - Lower EEPROM clock
  1440. * @hw: pointer to the HW structure
  1441. * @eecd: pointer to the EEPROM
  1442. *
  1443. * Clear/Lower the EEPROM clock bit.
  1444. **/
  1445. static void e1000_lower_eec_clk(struct e1000_hw *hw, u32 *eecd)
  1446. {
  1447. *eecd = *eecd & ~E1000_EECD_SK;
  1448. ew32(EECD, *eecd);
  1449. e1e_flush();
  1450. udelay(hw->nvm.delay_usec);
  1451. }
  1452. /**
  1453. * e1000_shift_out_eec_bits - Shift data bits our to the EEPROM
  1454. * @hw: pointer to the HW structure
  1455. * @data: data to send to the EEPROM
  1456. * @count: number of bits to shift out
  1457. *
  1458. * We need to shift 'count' bits out to the EEPROM. So, the value in the
  1459. * "data" parameter will be shifted out to the EEPROM one bit at a time.
  1460. * In order to do this, "data" must be broken down into bits.
  1461. **/
  1462. static void e1000_shift_out_eec_bits(struct e1000_hw *hw, u16 data, u16 count)
  1463. {
  1464. struct e1000_nvm_info *nvm = &hw->nvm;
  1465. u32 eecd = er32(EECD);
  1466. u32 mask;
  1467. mask = 0x01 << (count - 1);
  1468. if (nvm->type == e1000_nvm_eeprom_spi)
  1469. eecd |= E1000_EECD_DO;
  1470. do {
  1471. eecd &= ~E1000_EECD_DI;
  1472. if (data & mask)
  1473. eecd |= E1000_EECD_DI;
  1474. ew32(EECD, eecd);
  1475. e1e_flush();
  1476. udelay(nvm->delay_usec);
  1477. e1000_raise_eec_clk(hw, &eecd);
  1478. e1000_lower_eec_clk(hw, &eecd);
  1479. mask >>= 1;
  1480. } while (mask);
  1481. eecd &= ~E1000_EECD_DI;
  1482. ew32(EECD, eecd);
  1483. }
  1484. /**
  1485. * e1000_shift_in_eec_bits - Shift data bits in from the EEPROM
  1486. * @hw: pointer to the HW structure
  1487. * @count: number of bits to shift in
  1488. *
  1489. * In order to read a register from the EEPROM, we need to shift 'count' bits
  1490. * in from the EEPROM. Bits are "shifted in" by raising the clock input to
  1491. * the EEPROM (setting the SK bit), and then reading the value of the data out
  1492. * "DO" bit. During this "shifting in" process the data in "DI" bit should
  1493. * always be clear.
  1494. **/
  1495. static u16 e1000_shift_in_eec_bits(struct e1000_hw *hw, u16 count)
  1496. {
  1497. u32 eecd;
  1498. u32 i;
  1499. u16 data;
  1500. eecd = er32(EECD);
  1501. eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
  1502. data = 0;
  1503. for (i = 0; i < count; i++) {
  1504. data <<= 1;
  1505. e1000_raise_eec_clk(hw, &eecd);
  1506. eecd = er32(EECD);
  1507. eecd &= ~E1000_EECD_DI;
  1508. if (eecd & E1000_EECD_DO)
  1509. data |= 1;
  1510. e1000_lower_eec_clk(hw, &eecd);
  1511. }
  1512. return data;
  1513. }
  1514. /**
  1515. * e1000e_poll_eerd_eewr_done - Poll for EEPROM read/write completion
  1516. * @hw: pointer to the HW structure
  1517. * @ee_reg: EEPROM flag for polling
  1518. *
  1519. * Polls the EEPROM status bit for either read or write completion based
  1520. * upon the value of 'ee_reg'.
  1521. **/
  1522. s32 e1000e_poll_eerd_eewr_done(struct e1000_hw *hw, int ee_reg)
  1523. {
  1524. u32 attempts = 100000;
  1525. u32 i, reg = 0;
  1526. for (i = 0; i < attempts; i++) {
  1527. if (ee_reg == E1000_NVM_POLL_READ)
  1528. reg = er32(EERD);
  1529. else
  1530. reg = er32(EEWR);
  1531. if (reg & E1000_NVM_RW_REG_DONE)
  1532. return 0;
  1533. udelay(5);
  1534. }
  1535. return -E1000_ERR_NVM;
  1536. }
  1537. /**
  1538. * e1000e_acquire_nvm - Generic request for access to EEPROM
  1539. * @hw: pointer to the HW structure
  1540. *
  1541. * Set the EEPROM access request bit and wait for EEPROM access grant bit.
  1542. * Return successful if access grant bit set, else clear the request for
  1543. * EEPROM access and return -E1000_ERR_NVM (-1).
  1544. **/
  1545. s32 e1000e_acquire_nvm(struct e1000_hw *hw)
  1546. {
  1547. u32 eecd = er32(EECD);
  1548. s32 timeout = E1000_NVM_GRANT_ATTEMPTS;
  1549. ew32(EECD, eecd | E1000_EECD_REQ);
  1550. eecd = er32(EECD);
  1551. while (timeout) {
  1552. if (eecd & E1000_EECD_GNT)
  1553. break;
  1554. udelay(5);
  1555. eecd = er32(EECD);
  1556. timeout--;
  1557. }
  1558. if (!timeout) {
  1559. eecd &= ~E1000_EECD_REQ;
  1560. ew32(EECD, eecd);
  1561. hw_dbg(hw, "Could not acquire NVM grant\n");
  1562. return -E1000_ERR_NVM;
  1563. }
  1564. return 0;
  1565. }
  1566. /**
  1567. * e1000_standby_nvm - Return EEPROM to standby state
  1568. * @hw: pointer to the HW structure
  1569. *
  1570. * Return the EEPROM to a standby state.
  1571. **/
  1572. static void e1000_standby_nvm(struct e1000_hw *hw)
  1573. {
  1574. struct e1000_nvm_info *nvm = &hw->nvm;
  1575. u32 eecd = er32(EECD);
  1576. if (nvm->type == e1000_nvm_eeprom_spi) {
  1577. /* Toggle CS to flush commands */
  1578. eecd |= E1000_EECD_CS;
  1579. ew32(EECD, eecd);
  1580. e1e_flush();
  1581. udelay(nvm->delay_usec);
  1582. eecd &= ~E1000_EECD_CS;
  1583. ew32(EECD, eecd);
  1584. e1e_flush();
  1585. udelay(nvm->delay_usec);
  1586. }
  1587. }
  1588. /**
  1589. * e1000_stop_nvm - Terminate EEPROM command
  1590. * @hw: pointer to the HW structure
  1591. *
  1592. * Terminates the current command by inverting the EEPROM's chip select pin.
  1593. **/
  1594. static void e1000_stop_nvm(struct e1000_hw *hw)
  1595. {
  1596. u32 eecd;
  1597. eecd = er32(EECD);
  1598. if (hw->nvm.type == e1000_nvm_eeprom_spi) {
  1599. /* Pull CS high */
  1600. eecd |= E1000_EECD_CS;
  1601. e1000_lower_eec_clk(hw, &eecd);
  1602. }
  1603. }
  1604. /**
  1605. * e1000e_release_nvm - Release exclusive access to EEPROM
  1606. * @hw: pointer to the HW structure
  1607. *
  1608. * Stop any current commands to the EEPROM and clear the EEPROM request bit.
  1609. **/
  1610. void e1000e_release_nvm(struct e1000_hw *hw)
  1611. {
  1612. u32 eecd;
  1613. e1000_stop_nvm(hw);
  1614. eecd = er32(EECD);
  1615. eecd &= ~E1000_EECD_REQ;
  1616. ew32(EECD, eecd);
  1617. }
  1618. /**
  1619. * e1000_ready_nvm_eeprom - Prepares EEPROM for read/write
  1620. * @hw: pointer to the HW structure
  1621. *
  1622. * Setups the EEPROM for reading and writing.
  1623. **/
  1624. static s32 e1000_ready_nvm_eeprom(struct e1000_hw *hw)
  1625. {
  1626. struct e1000_nvm_info *nvm = &hw->nvm;
  1627. u32 eecd = er32(EECD);
  1628. u16 timeout = 0;
  1629. u8 spi_stat_reg;
  1630. if (nvm->type == e1000_nvm_eeprom_spi) {
  1631. /* Clear SK and CS */
  1632. eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
  1633. ew32(EECD, eecd);
  1634. udelay(1);
  1635. timeout = NVM_MAX_RETRY_SPI;
  1636. /* Read "Status Register" repeatedly until the LSB is cleared.
  1637. * The EEPROM will signal that the command has been completed
  1638. * by clearing bit 0 of the internal status register. If it's
  1639. * not cleared within 'timeout', then error out. */
  1640. while (timeout) {
  1641. e1000_shift_out_eec_bits(hw, NVM_RDSR_OPCODE_SPI,
  1642. hw->nvm.opcode_bits);
  1643. spi_stat_reg = (u8)e1000_shift_in_eec_bits(hw, 8);
  1644. if (!(spi_stat_reg & NVM_STATUS_RDY_SPI))
  1645. break;
  1646. udelay(5);
  1647. e1000_standby_nvm(hw);
  1648. timeout--;
  1649. }
  1650. if (!timeout) {
  1651. hw_dbg(hw, "SPI NVM Status error\n");
  1652. return -E1000_ERR_NVM;
  1653. }
  1654. }
  1655. return 0;
  1656. }
  1657. /**
  1658. * e1000e_read_nvm_spi - Read EEPROM's using SPI
  1659. * @hw: pointer to the HW structure
  1660. * @offset: offset of word in the EEPROM to read
  1661. * @words: number of words to read
  1662. * @data: word read from the EEPROM
  1663. *
  1664. * Reads a 16 bit word from the EEPROM.
  1665. **/
  1666. s32 e1000e_read_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
  1667. {
  1668. struct e1000_nvm_info *nvm = &hw->nvm;
  1669. u32 i = 0;
  1670. s32 ret_val;
  1671. u16 word_in;
  1672. u8 read_opcode = NVM_READ_OPCODE_SPI;
  1673. /* A check for invalid values: offset too large, too many words,
  1674. * and not enough words. */
  1675. if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
  1676. (words == 0)) {
  1677. hw_dbg(hw, "nvm parameter(s) out of bounds\n");
  1678. return -E1000_ERR_NVM;
  1679. }
  1680. ret_val = nvm->ops.acquire_nvm(hw);
  1681. if (ret_val)
  1682. return ret_val;
  1683. ret_val = e1000_ready_nvm_eeprom(hw);
  1684. if (ret_val) {
  1685. nvm->ops.release_nvm(hw);
  1686. return ret_val;
  1687. }
  1688. e1000_standby_nvm(hw);
  1689. if ((nvm->address_bits == 8) && (offset >= 128))
  1690. read_opcode |= NVM_A8_OPCODE_SPI;
  1691. /* Send the READ command (opcode + addr) */
  1692. e1000_shift_out_eec_bits(hw, read_opcode, nvm->opcode_bits);
  1693. e1000_shift_out_eec_bits(hw, (u16)(offset*2), nvm->address_bits);
  1694. /* Read the data. SPI NVMs increment the address with each byte
  1695. * read and will roll over if reading beyond the end. This allows
  1696. * us to read the whole NVM from any offset */
  1697. for (i = 0; i < words; i++) {
  1698. word_in = e1000_shift_in_eec_bits(hw, 16);
  1699. data[i] = (word_in >> 8) | (word_in << 8);
  1700. }
  1701. nvm->ops.release_nvm(hw);
  1702. return 0;
  1703. }
  1704. /**
  1705. * e1000e_read_nvm_eerd - Reads EEPROM using EERD register
  1706. * @hw: pointer to the HW structure
  1707. * @offset: offset of word in the EEPROM to read
  1708. * @words: number of words to read
  1709. * @data: word read from the EEPROM
  1710. *
  1711. * Reads a 16 bit word from the EEPROM using the EERD register.
  1712. **/
  1713. s32 e1000e_read_nvm_eerd(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
  1714. {
  1715. struct e1000_nvm_info *nvm = &hw->nvm;
  1716. u32 i, eerd = 0;
  1717. s32 ret_val = 0;
  1718. /* A check for invalid values: offset too large, too many words,
  1719. * and not enough words. */
  1720. if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
  1721. (words == 0)) {
  1722. hw_dbg(hw, "nvm parameter(s) out of bounds\n");
  1723. return -E1000_ERR_NVM;
  1724. }
  1725. for (i = 0; i < words; i++) {
  1726. eerd = ((offset+i) << E1000_NVM_RW_ADDR_SHIFT) +
  1727. E1000_NVM_RW_REG_START;
  1728. ew32(EERD, eerd);
  1729. ret_val = e1000e_poll_eerd_eewr_done(hw, E1000_NVM_POLL_READ);
  1730. if (ret_val)
  1731. break;
  1732. data[i] = (er32(EERD) >>
  1733. E1000_NVM_RW_REG_DATA);
  1734. }
  1735. return ret_val;
  1736. }
  1737. /**
  1738. * e1000e_write_nvm_spi - Write to EEPROM using SPI
  1739. * @hw: pointer to the HW structure
  1740. * @offset: offset within the EEPROM to be written to
  1741. * @words: number of words to write
  1742. * @data: 16 bit word(s) to be written to the EEPROM
  1743. *
  1744. * Writes data to EEPROM at offset using SPI interface.
  1745. *
  1746. * If e1000e_update_nvm_checksum is not called after this function , the
  1747. * EEPROM will most likley contain an invalid checksum.
  1748. **/
  1749. s32 e1000e_write_nvm_spi(struct e1000_hw *hw, u16 offset, u16 words, u16 *data)
  1750. {
  1751. struct e1000_nvm_info *nvm = &hw->nvm;
  1752. s32 ret_val;
  1753. u16 widx = 0;
  1754. /* A check for invalid values: offset too large, too many words,
  1755. * and not enough words. */
  1756. if ((offset >= nvm->word_size) || (words > (nvm->word_size - offset)) ||
  1757. (words == 0)) {
  1758. hw_dbg(hw, "nvm parameter(s) out of bounds\n");
  1759. return -E1000_ERR_NVM;
  1760. }
  1761. ret_val = nvm->ops.acquire_nvm(hw);
  1762. if (ret_val)
  1763. return ret_val;
  1764. msleep(10);
  1765. while (widx < words) {
  1766. u8 write_opcode = NVM_WRITE_OPCODE_SPI;
  1767. ret_val = e1000_ready_nvm_eeprom(hw);
  1768. if (ret_val) {
  1769. nvm->ops.release_nvm(hw);
  1770. return ret_val;
  1771. }
  1772. e1000_standby_nvm(hw);
  1773. /* Send the WRITE ENABLE command (8 bit opcode) */
  1774. e1000_shift_out_eec_bits(hw, NVM_WREN_OPCODE_SPI,
  1775. nvm->opcode_bits);
  1776. e1000_standby_nvm(hw);
  1777. /* Some SPI eeproms use the 8th address bit embedded in the
  1778. * opcode */
  1779. if ((nvm->address_bits == 8) && (offset >= 128))
  1780. write_opcode |= NVM_A8_OPCODE_SPI;
  1781. /* Send the Write command (8-bit opcode + addr) */
  1782. e1000_shift_out_eec_bits(hw, write_opcode, nvm->opcode_bits);
  1783. e1000_shift_out_eec_bits(hw, (u16)((offset + widx) * 2),
  1784. nvm->address_bits);
  1785. /* Loop to allow for up to whole page write of eeprom */
  1786. while (widx < words) {
  1787. u16 word_out = data[widx];
  1788. word_out = (word_out >> 8) | (word_out << 8);
  1789. e1000_shift_out_eec_bits(hw, word_out, 16);
  1790. widx++;
  1791. if ((((offset + widx) * 2) % nvm->page_size) == 0) {
  1792. e1000_standby_nvm(hw);
  1793. break;
  1794. }
  1795. }
  1796. }
  1797. msleep(10);
  1798. return 0;
  1799. }
  1800. /**
  1801. * e1000e_read_mac_addr - Read device MAC address
  1802. * @hw: pointer to the HW structure
  1803. *
  1804. * Reads the device MAC address from the EEPROM and stores the value.
  1805. * Since devices with two ports use the same EEPROM, we increment the
  1806. * last bit in the MAC address for the second port.
  1807. **/
  1808. s32 e1000e_read_mac_addr(struct e1000_hw *hw)
  1809. {
  1810. s32 ret_val;
  1811. u16 offset, nvm_data, i;
  1812. for (i = 0; i < ETH_ALEN; i += 2) {
  1813. offset = i >> 1;
  1814. ret_val = e1000_read_nvm(hw, offset, 1, &nvm_data);
  1815. if (ret_val) {
  1816. hw_dbg(hw, "NVM Read Error\n");
  1817. return ret_val;
  1818. }
  1819. hw->mac.perm_addr[i] = (u8)(nvm_data & 0xFF);
  1820. hw->mac.perm_addr[i+1] = (u8)(nvm_data >> 8);
  1821. }
  1822. /* Flip last bit of mac address if we're on second port */
  1823. if (hw->bus.func == E1000_FUNC_1)
  1824. hw->mac.perm_addr[5] ^= 1;
  1825. for (i = 0; i < ETH_ALEN; i++)
  1826. hw->mac.addr[i] = hw->mac.perm_addr[i];
  1827. return 0;
  1828. }
  1829. /**
  1830. * e1000e_validate_nvm_checksum_generic - Validate EEPROM checksum
  1831. * @hw: pointer to the HW structure
  1832. *
  1833. * Calculates the EEPROM checksum by reading/adding each word of the EEPROM
  1834. * and then verifies that the sum of the EEPROM is equal to 0xBABA.
  1835. **/
  1836. s32 e1000e_validate_nvm_checksum_generic(struct e1000_hw *hw)
  1837. {
  1838. s32 ret_val;
  1839. u16 checksum = 0;
  1840. u16 i, nvm_data;
  1841. for (i = 0; i < (NVM_CHECKSUM_REG + 1); i++) {
  1842. ret_val = e1000_read_nvm(hw, i, 1, &nvm_data);
  1843. if (ret_val) {
  1844. hw_dbg(hw, "NVM Read Error\n");
  1845. return ret_val;
  1846. }
  1847. checksum += nvm_data;
  1848. }
  1849. if (checksum != (u16) NVM_SUM) {
  1850. hw_dbg(hw, "NVM Checksum Invalid\n");
  1851. return -E1000_ERR_NVM;
  1852. }
  1853. return 0;
  1854. }
  1855. /**
  1856. * e1000e_update_nvm_checksum_generic - Update EEPROM checksum
  1857. * @hw: pointer to the HW structure
  1858. *
  1859. * Updates the EEPROM checksum by reading/adding each word of the EEPROM
  1860. * up to the checksum. Then calculates the EEPROM checksum and writes the
  1861. * value to the EEPROM.
  1862. **/
  1863. s32 e1000e_update_nvm_checksum_generic(struct e1000_hw *hw)
  1864. {
  1865. s32 ret_val;
  1866. u16 checksum = 0;
  1867. u16 i, nvm_data;
  1868. for (i = 0; i < NVM_CHECKSUM_REG; i++) {
  1869. ret_val = e1000_read_nvm(hw, i, 1, &nvm_data);
  1870. if (ret_val) {
  1871. hw_dbg(hw, "NVM Read Error while updating checksum.\n");
  1872. return ret_val;
  1873. }
  1874. checksum += nvm_data;
  1875. }
  1876. checksum = (u16) NVM_SUM - checksum;
  1877. ret_val = e1000_write_nvm(hw, NVM_CHECKSUM_REG, 1, &checksum);
  1878. if (ret_val)
  1879. hw_dbg(hw, "NVM Write Error while updating checksum.\n");
  1880. return ret_val;
  1881. }
  1882. /**
  1883. * e1000e_reload_nvm - Reloads EEPROM
  1884. * @hw: pointer to the HW structure
  1885. *
  1886. * Reloads the EEPROM by setting the "Reinitialize from EEPROM" bit in the
  1887. * extended control register.
  1888. **/
  1889. void e1000e_reload_nvm(struct e1000_hw *hw)
  1890. {
  1891. u32 ctrl_ext;
  1892. udelay(10);
  1893. ctrl_ext = er32(CTRL_EXT);
  1894. ctrl_ext |= E1000_CTRL_EXT_EE_RST;
  1895. ew32(CTRL_EXT, ctrl_ext);
  1896. e1e_flush();
  1897. }
  1898. /**
  1899. * e1000_calculate_checksum - Calculate checksum for buffer
  1900. * @buffer: pointer to EEPROM
  1901. * @length: size of EEPROM to calculate a checksum for
  1902. *
  1903. * Calculates the checksum for some buffer on a specified length. The
  1904. * checksum calculated is returned.
  1905. **/
  1906. static u8 e1000_calculate_checksum(u8 *buffer, u32 length)
  1907. {
  1908. u32 i;
  1909. u8 sum = 0;
  1910. if (!buffer)
  1911. return 0;
  1912. for (i = 0; i < length; i++)
  1913. sum += buffer[i];
  1914. return (u8) (0 - sum);
  1915. }
  1916. /**
  1917. * e1000_mng_enable_host_if - Checks host interface is enabled
  1918. * @hw: pointer to the HW structure
  1919. *
  1920. * Returns E1000_success upon success, else E1000_ERR_HOST_INTERFACE_COMMAND
  1921. *
  1922. * This function checks whether the HOST IF is enabled for command operaton
  1923. * and also checks whether the previous command is completed. It busy waits
  1924. * in case of previous command is not completed.
  1925. **/
  1926. static s32 e1000_mng_enable_host_if(struct e1000_hw *hw)
  1927. {
  1928. u32 hicr;
  1929. u8 i;
  1930. /* Check that the host interface is enabled. */
  1931. hicr = er32(HICR);
  1932. if ((hicr & E1000_HICR_EN) == 0) {
  1933. hw_dbg(hw, "E1000_HOST_EN bit disabled.\n");
  1934. return -E1000_ERR_HOST_INTERFACE_COMMAND;
  1935. }
  1936. /* check the previous command is completed */
  1937. for (i = 0; i < E1000_MNG_DHCP_COMMAND_TIMEOUT; i++) {
  1938. hicr = er32(HICR);
  1939. if (!(hicr & E1000_HICR_C))
  1940. break;
  1941. mdelay(1);
  1942. }
  1943. if (i == E1000_MNG_DHCP_COMMAND_TIMEOUT) {
  1944. hw_dbg(hw, "Previous command timeout failed .\n");
  1945. return -E1000_ERR_HOST_INTERFACE_COMMAND;
  1946. }
  1947. return 0;
  1948. }
  1949. /**
  1950. * e1000e_check_mng_mode - check managament mode
  1951. * @hw: pointer to the HW structure
  1952. *
  1953. * Reads the firmware semaphore register and returns true (>0) if
  1954. * manageability is enabled, else false (0).
  1955. **/
  1956. bool e1000e_check_mng_mode(struct e1000_hw *hw)
  1957. {
  1958. u32 fwsm = er32(FWSM);
  1959. return (fwsm & E1000_FWSM_MODE_MASK) == hw->mac.ops.mng_mode_enab;
  1960. }
  1961. /**
  1962. * e1000e_enable_tx_pkt_filtering - Enable packet filtering on TX
  1963. * @hw: pointer to the HW structure
  1964. *
  1965. * Enables packet filtering on transmit packets if manageability is enabled
  1966. * and host interface is enabled.
  1967. **/
  1968. bool e1000e_enable_tx_pkt_filtering(struct e1000_hw *hw)
  1969. {
  1970. struct e1000_host_mng_dhcp_cookie *hdr = &hw->mng_cookie;
  1971. u32 *buffer = (u32 *)&hw->mng_cookie;
  1972. u32 offset;
  1973. s32 ret_val, hdr_csum, csum;
  1974. u8 i, len;
  1975. /* No manageability, no filtering */
  1976. if (!e1000e_check_mng_mode(hw)) {
  1977. hw->mac.tx_pkt_filtering = 0;
  1978. return 0;
  1979. }
  1980. /* If we can't read from the host interface for whatever
  1981. * reason, disable filtering.
  1982. */
  1983. ret_val = e1000_mng_enable_host_if(hw);
  1984. if (ret_val != 0) {
  1985. hw->mac.tx_pkt_filtering = 0;
  1986. return ret_val;
  1987. }
  1988. /* Read in the header. Length and offset are in dwords. */
  1989. len = E1000_MNG_DHCP_COOKIE_LENGTH >> 2;
  1990. offset = E1000_MNG_DHCP_COOKIE_OFFSET >> 2;
  1991. for (i = 0; i < len; i++)
  1992. *(buffer + i) = E1000_READ_REG_ARRAY(hw, E1000_HOST_IF, offset + i);
  1993. hdr_csum = hdr->checksum;
  1994. hdr->checksum = 0;
  1995. csum = e1000_calculate_checksum((u8 *)hdr,
  1996. E1000_MNG_DHCP_COOKIE_LENGTH);
  1997. /* If either the checksums or signature don't match, then
  1998. * the cookie area isn't considered valid, in which case we
  1999. * take the safe route of assuming Tx filtering is enabled.
  2000. */
  2001. if ((hdr_csum != csum) || (hdr->signature != E1000_IAMT_SIGNATURE)) {
  2002. hw->mac.tx_pkt_filtering = 1;
  2003. return 1;
  2004. }
  2005. /* Cookie area is valid, make the final check for filtering. */
  2006. if (!(hdr->status & E1000_MNG_DHCP_COOKIE_STATUS_PARSING)) {
  2007. hw->mac.tx_pkt_filtering = 0;
  2008. return 0;
  2009. }
  2010. hw->mac.tx_pkt_filtering = 1;
  2011. return 1;
  2012. }
  2013. /**
  2014. * e1000_mng_write_cmd_header - Writes manageability command header
  2015. * @hw: pointer to the HW structure
  2016. * @hdr: pointer to the host interface command header
  2017. *
  2018. * Writes the command header after does the checksum calculation.
  2019. **/
  2020. static s32 e1000_mng_write_cmd_header(struct e1000_hw *hw,
  2021. struct e1000_host_mng_command_header *hdr)
  2022. {
  2023. u16 i, length = sizeof(struct e1000_host_mng_command_header);
  2024. /* Write the whole command header structure with new checksum. */
  2025. hdr->checksum = e1000_calculate_checksum((u8 *)hdr, length);
  2026. length >>= 2;
  2027. /* Write the relevant command block into the ram area. */
  2028. for (i = 0; i < length; i++) {
  2029. E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, i,
  2030. *((u32 *) hdr + i));
  2031. e1e_flush();
  2032. }
  2033. return 0;
  2034. }
  2035. /**
  2036. * e1000_mng_host_if_write - Writes to the manageability host interface
  2037. * @hw: pointer to the HW structure
  2038. * @buffer: pointer to the host interface buffer
  2039. * @length: size of the buffer
  2040. * @offset: location in the buffer to write to
  2041. * @sum: sum of the data (not checksum)
  2042. *
  2043. * This function writes the buffer content at the offset given on the host if.
  2044. * It also does alignment considerations to do the writes in most efficient
  2045. * way. Also fills up the sum of the buffer in *buffer parameter.
  2046. **/
  2047. static s32 e1000_mng_host_if_write(struct e1000_hw *hw, u8 *buffer,
  2048. u16 length, u16 offset, u8 *sum)
  2049. {
  2050. u8 *tmp;
  2051. u8 *bufptr = buffer;
  2052. u32 data = 0;
  2053. u16 remaining, i, j, prev_bytes;
  2054. /* sum = only sum of the data and it is not checksum */
  2055. if (length == 0 || offset + length > E1000_HI_MAX_MNG_DATA_LENGTH)
  2056. return -E1000_ERR_PARAM;
  2057. tmp = (u8 *)&data;
  2058. prev_bytes = offset & 0x3;
  2059. offset >>= 2;
  2060. if (prev_bytes) {
  2061. data = E1000_READ_REG_ARRAY(hw, E1000_HOST_IF, offset);
  2062. for (j = prev_bytes; j < sizeof(u32); j++) {
  2063. *(tmp + j) = *bufptr++;
  2064. *sum += *(tmp + j);
  2065. }
  2066. E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset, data);
  2067. length -= j - prev_bytes;
  2068. offset++;
  2069. }
  2070. remaining = length & 0x3;
  2071. length -= remaining;
  2072. /* Calculate length in DWORDs */
  2073. length >>= 2;
  2074. /* The device driver writes the relevant command block into the
  2075. * ram area. */
  2076. for (i = 0; i < length; i++) {
  2077. for (j = 0; j < sizeof(u32); j++) {
  2078. *(tmp + j) = *bufptr++;
  2079. *sum += *(tmp + j);
  2080. }
  2081. E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset + i, data);
  2082. }
  2083. if (remaining) {
  2084. for (j = 0; j < sizeof(u32); j++) {
  2085. if (j < remaining)
  2086. *(tmp + j) = *bufptr++;
  2087. else
  2088. *(tmp + j) = 0;
  2089. *sum += *(tmp + j);
  2090. }
  2091. E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset + i, data);
  2092. }
  2093. return 0;
  2094. }
  2095. /**
  2096. * e1000e_mng_write_dhcp_info - Writes DHCP info to host interface
  2097. * @hw: pointer to the HW structure
  2098. * @buffer: pointer to the host interface
  2099. * @length: size of the buffer
  2100. *
  2101. * Writes the DHCP information to the host interface.
  2102. **/
  2103. s32 e1000e_mng_write_dhcp_info(struct e1000_hw *hw, u8 *buffer, u16 length)
  2104. {
  2105. struct e1000_host_mng_command_header hdr;
  2106. s32 ret_val;
  2107. u32 hicr;
  2108. hdr.command_id = E1000_MNG_DHCP_TX_PAYLOAD_CMD;
  2109. hdr.command_length = length;
  2110. hdr.reserved1 = 0;
  2111. hdr.reserved2 = 0;
  2112. hdr.checksum = 0;
  2113. /* Enable the host interface */
  2114. ret_val = e1000_mng_enable_host_if(hw);
  2115. if (ret_val)
  2116. return ret_val;
  2117. /* Populate the host interface with the contents of "buffer". */
  2118. ret_val = e1000_mng_host_if_write(hw, buffer, length,
  2119. sizeof(hdr), &(hdr.checksum));
  2120. if (ret_val)
  2121. return ret_val;
  2122. /* Write the manageability command header */
  2123. ret_val = e1000_mng_write_cmd_header(hw, &hdr);
  2124. if (ret_val)
  2125. return ret_val;
  2126. /* Tell the ARC a new command is pending. */
  2127. hicr = er32(HICR);
  2128. ew32(HICR, hicr | E1000_HICR_C);
  2129. return 0;
  2130. }
  2131. /**
  2132. * e1000e_enable_mng_pass_thru - Enable processing of ARP's
  2133. * @hw: pointer to the HW structure
  2134. *
  2135. * Verifies the hardware needs to allow ARPs to be processed by the host.
  2136. **/
  2137. bool e1000e_enable_mng_pass_thru(struct e1000_hw *hw)
  2138. {
  2139. u32 manc;
  2140. u32 fwsm, factps;
  2141. bool ret_val = 0;
  2142. manc = er32(MANC);
  2143. if (!(manc & E1000_MANC_RCV_TCO_EN) ||
  2144. !(manc & E1000_MANC_EN_MAC_ADDR_FILTER))
  2145. return ret_val;
  2146. if (hw->mac.arc_subsystem_valid) {
  2147. fwsm = er32(FWSM);
  2148. factps = er32(FACTPS);
  2149. if (!(factps & E1000_FACTPS_MNGCG) &&
  2150. ((fwsm & E1000_FWSM_MODE_MASK) ==
  2151. (e1000_mng_mode_pt << E1000_FWSM_MODE_SHIFT))) {
  2152. ret_val = 1;
  2153. return ret_val;
  2154. }
  2155. } else {
  2156. if ((manc & E1000_MANC_SMBUS_EN) &&
  2157. !(manc & E1000_MANC_ASF_EN)) {
  2158. ret_val = 1;
  2159. return ret_val;
  2160. }
  2161. }
  2162. return ret_val;
  2163. }
  2164. s32 e1000e_read_part_num(struct e1000_hw *hw, u32 *part_num)
  2165. {
  2166. s32 ret_val;
  2167. u16 nvm_data;
  2168. ret_val = e1000_read_nvm(hw, NVM_PBA_OFFSET_0, 1, &nvm_data);
  2169. if (ret_val) {
  2170. hw_dbg(hw, "NVM Read Error\n");
  2171. return ret_val;
  2172. }
  2173. *part_num = (u32)(nvm_data << 16);
  2174. ret_val = e1000_read_nvm(hw, NVM_PBA_OFFSET_1, 1, &nvm_data);
  2175. if (ret_val) {
  2176. hw_dbg(hw, "NVM Read Error\n");
  2177. return ret_val;
  2178. }
  2179. *part_num |= nvm_data;
  2180. return 0;
  2181. }