lib.c 67 KB

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