ixgbe_common.c 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143
  1. /*******************************************************************************
  2. Intel 10 Gigabit PCI Express Linux driver
  3. Copyright(c) 1999 - 2016 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/pci.h>
  22. #include <linux/delay.h>
  23. #include <linux/sched.h>
  24. #include <linux/netdevice.h>
  25. #include "ixgbe.h"
  26. #include "ixgbe_common.h"
  27. #include "ixgbe_phy.h"
  28. static s32 ixgbe_acquire_eeprom(struct ixgbe_hw *hw);
  29. static s32 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw);
  30. static void ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw);
  31. static s32 ixgbe_ready_eeprom(struct ixgbe_hw *hw);
  32. static void ixgbe_standby_eeprom(struct ixgbe_hw *hw);
  33. static void ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
  34. u16 count);
  35. static u16 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count);
  36. static void ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec);
  37. static void ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec);
  38. static void ixgbe_release_eeprom(struct ixgbe_hw *hw);
  39. static s32 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr);
  40. static s32 ixgbe_poll_eerd_eewr_done(struct ixgbe_hw *hw, u32 ee_reg);
  41. static s32 ixgbe_read_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
  42. u16 words, u16 *data);
  43. static s32 ixgbe_write_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
  44. u16 words, u16 *data);
  45. static s32 ixgbe_detect_eeprom_page_size_generic(struct ixgbe_hw *hw,
  46. u16 offset);
  47. static s32 ixgbe_disable_pcie_master(struct ixgbe_hw *hw);
  48. /* Base table for registers values that change by MAC */
  49. const u32 ixgbe_mvals_8259X[IXGBE_MVALS_IDX_LIMIT] = {
  50. IXGBE_MVALS_INIT(8259X)
  51. };
  52. /**
  53. * ixgbe_device_supports_autoneg_fc - Check if phy supports autoneg flow
  54. * control
  55. * @hw: pointer to hardware structure
  56. *
  57. * There are several phys that do not support autoneg flow control. This
  58. * function check the device id to see if the associated phy supports
  59. * autoneg flow control.
  60. **/
  61. bool ixgbe_device_supports_autoneg_fc(struct ixgbe_hw *hw)
  62. {
  63. bool supported = false;
  64. ixgbe_link_speed speed;
  65. bool link_up;
  66. switch (hw->phy.media_type) {
  67. case ixgbe_media_type_fiber:
  68. hw->mac.ops.check_link(hw, &speed, &link_up, false);
  69. /* if link is down, assume supported */
  70. if (link_up)
  71. supported = speed == IXGBE_LINK_SPEED_1GB_FULL ?
  72. true : false;
  73. else
  74. supported = true;
  75. break;
  76. case ixgbe_media_type_backplane:
  77. supported = true;
  78. break;
  79. case ixgbe_media_type_copper:
  80. /* only some copper devices support flow control autoneg */
  81. switch (hw->device_id) {
  82. case IXGBE_DEV_ID_82599_T3_LOM:
  83. case IXGBE_DEV_ID_X540T:
  84. case IXGBE_DEV_ID_X540T1:
  85. case IXGBE_DEV_ID_X550T:
  86. case IXGBE_DEV_ID_X550T1:
  87. case IXGBE_DEV_ID_X550EM_X_10G_T:
  88. supported = true;
  89. break;
  90. default:
  91. break;
  92. }
  93. default:
  94. break;
  95. }
  96. return supported;
  97. }
  98. /**
  99. * ixgbe_setup_fc_generic - Set up flow control
  100. * @hw: pointer to hardware structure
  101. *
  102. * Called at init time to set up flow control.
  103. **/
  104. s32 ixgbe_setup_fc_generic(struct ixgbe_hw *hw)
  105. {
  106. s32 ret_val = 0;
  107. u32 reg = 0, reg_bp = 0;
  108. u16 reg_cu = 0;
  109. bool locked = false;
  110. /*
  111. * Validate the requested mode. Strict IEEE mode does not allow
  112. * ixgbe_fc_rx_pause because it will cause us to fail at UNH.
  113. */
  114. if (hw->fc.strict_ieee && hw->fc.requested_mode == ixgbe_fc_rx_pause) {
  115. hw_dbg(hw, "ixgbe_fc_rx_pause not valid in strict IEEE mode\n");
  116. return IXGBE_ERR_INVALID_LINK_SETTINGS;
  117. }
  118. /*
  119. * 10gig parts do not have a word in the EEPROM to determine the
  120. * default flow control setting, so we explicitly set it to full.
  121. */
  122. if (hw->fc.requested_mode == ixgbe_fc_default)
  123. hw->fc.requested_mode = ixgbe_fc_full;
  124. /*
  125. * Set up the 1G and 10G flow control advertisement registers so the
  126. * HW will be able to do fc autoneg once the cable is plugged in. If
  127. * we link at 10G, the 1G advertisement is harmless and vice versa.
  128. */
  129. switch (hw->phy.media_type) {
  130. case ixgbe_media_type_backplane:
  131. /* some MAC's need RMW protection on AUTOC */
  132. ret_val = hw->mac.ops.prot_autoc_read(hw, &locked, &reg_bp);
  133. if (ret_val)
  134. return ret_val;
  135. /* only backplane uses autoc so fall though */
  136. case ixgbe_media_type_fiber:
  137. reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANA);
  138. break;
  139. case ixgbe_media_type_copper:
  140. hw->phy.ops.read_reg(hw, MDIO_AN_ADVERTISE,
  141. MDIO_MMD_AN, &reg_cu);
  142. break;
  143. default:
  144. break;
  145. }
  146. /*
  147. * The possible values of fc.requested_mode are:
  148. * 0: Flow control is completely disabled
  149. * 1: Rx flow control is enabled (we can receive pause frames,
  150. * but not send pause frames).
  151. * 2: Tx flow control is enabled (we can send pause frames but
  152. * we do not support receiving pause frames).
  153. * 3: Both Rx and Tx flow control (symmetric) are enabled.
  154. * other: Invalid.
  155. */
  156. switch (hw->fc.requested_mode) {
  157. case ixgbe_fc_none:
  158. /* Flow control completely disabled by software override. */
  159. reg &= ~(IXGBE_PCS1GANA_SYM_PAUSE | IXGBE_PCS1GANA_ASM_PAUSE);
  160. if (hw->phy.media_type == ixgbe_media_type_backplane)
  161. reg_bp &= ~(IXGBE_AUTOC_SYM_PAUSE |
  162. IXGBE_AUTOC_ASM_PAUSE);
  163. else if (hw->phy.media_type == ixgbe_media_type_copper)
  164. reg_cu &= ~(IXGBE_TAF_SYM_PAUSE | IXGBE_TAF_ASM_PAUSE);
  165. break;
  166. case ixgbe_fc_tx_pause:
  167. /*
  168. * Tx Flow control is enabled, and Rx Flow control is
  169. * disabled by software override.
  170. */
  171. reg |= IXGBE_PCS1GANA_ASM_PAUSE;
  172. reg &= ~IXGBE_PCS1GANA_SYM_PAUSE;
  173. if (hw->phy.media_type == ixgbe_media_type_backplane) {
  174. reg_bp |= IXGBE_AUTOC_ASM_PAUSE;
  175. reg_bp &= ~IXGBE_AUTOC_SYM_PAUSE;
  176. } else if (hw->phy.media_type == ixgbe_media_type_copper) {
  177. reg_cu |= IXGBE_TAF_ASM_PAUSE;
  178. reg_cu &= ~IXGBE_TAF_SYM_PAUSE;
  179. }
  180. break;
  181. case ixgbe_fc_rx_pause:
  182. /*
  183. * Rx Flow control is enabled and Tx Flow control is
  184. * disabled by software override. Since there really
  185. * isn't a way to advertise that we are capable of RX
  186. * Pause ONLY, we will advertise that we support both
  187. * symmetric and asymmetric Rx PAUSE, as such we fall
  188. * through to the fc_full statement. Later, we will
  189. * disable the adapter's ability to send PAUSE frames.
  190. */
  191. case ixgbe_fc_full:
  192. /* Flow control (both Rx and Tx) is enabled by SW override. */
  193. reg |= IXGBE_PCS1GANA_SYM_PAUSE | IXGBE_PCS1GANA_ASM_PAUSE;
  194. if (hw->phy.media_type == ixgbe_media_type_backplane)
  195. reg_bp |= IXGBE_AUTOC_SYM_PAUSE |
  196. IXGBE_AUTOC_ASM_PAUSE;
  197. else if (hw->phy.media_type == ixgbe_media_type_copper)
  198. reg_cu |= IXGBE_TAF_SYM_PAUSE | IXGBE_TAF_ASM_PAUSE;
  199. break;
  200. default:
  201. hw_dbg(hw, "Flow control param set incorrectly\n");
  202. return IXGBE_ERR_CONFIG;
  203. }
  204. if (hw->mac.type != ixgbe_mac_X540) {
  205. /*
  206. * Enable auto-negotiation between the MAC & PHY;
  207. * the MAC will advertise clause 37 flow control.
  208. */
  209. IXGBE_WRITE_REG(hw, IXGBE_PCS1GANA, reg);
  210. reg = IXGBE_READ_REG(hw, IXGBE_PCS1GLCTL);
  211. /* Disable AN timeout */
  212. if (hw->fc.strict_ieee)
  213. reg &= ~IXGBE_PCS1GLCTL_AN_1G_TIMEOUT_EN;
  214. IXGBE_WRITE_REG(hw, IXGBE_PCS1GLCTL, reg);
  215. hw_dbg(hw, "Set up FC; PCS1GLCTL = 0x%08X\n", reg);
  216. }
  217. /*
  218. * AUTOC restart handles negotiation of 1G and 10G on backplane
  219. * and copper. There is no need to set the PCS1GCTL register.
  220. *
  221. */
  222. if (hw->phy.media_type == ixgbe_media_type_backplane) {
  223. /* Need the SW/FW semaphore around AUTOC writes if 82599 and
  224. * LESM is on, likewise reset_pipeline requries the lock as
  225. * it also writes AUTOC.
  226. */
  227. ret_val = hw->mac.ops.prot_autoc_write(hw, reg_bp, locked);
  228. if (ret_val)
  229. return ret_val;
  230. } else if ((hw->phy.media_type == ixgbe_media_type_copper) &&
  231. ixgbe_device_supports_autoneg_fc(hw)) {
  232. hw->phy.ops.write_reg(hw, MDIO_AN_ADVERTISE,
  233. MDIO_MMD_AN, reg_cu);
  234. }
  235. hw_dbg(hw, "Set up FC; IXGBE_AUTOC = 0x%08X\n", reg);
  236. return ret_val;
  237. }
  238. /**
  239. * ixgbe_start_hw_generic - Prepare hardware for Tx/Rx
  240. * @hw: pointer to hardware structure
  241. *
  242. * Starts the hardware by filling the bus info structure and media type, clears
  243. * all on chip counters, initializes receive address registers, multicast
  244. * table, VLAN filter table, calls routine to set up link and flow control
  245. * settings, and leaves transmit and receive units disabled and uninitialized
  246. **/
  247. s32 ixgbe_start_hw_generic(struct ixgbe_hw *hw)
  248. {
  249. s32 ret_val;
  250. u32 ctrl_ext;
  251. /* Set the media type */
  252. hw->phy.media_type = hw->mac.ops.get_media_type(hw);
  253. /* Identify the PHY */
  254. hw->phy.ops.identify(hw);
  255. /* Clear the VLAN filter table */
  256. hw->mac.ops.clear_vfta(hw);
  257. /* Clear statistics registers */
  258. hw->mac.ops.clear_hw_cntrs(hw);
  259. /* Set No Snoop Disable */
  260. ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
  261. ctrl_ext |= IXGBE_CTRL_EXT_NS_DIS;
  262. IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
  263. IXGBE_WRITE_FLUSH(hw);
  264. /* Setup flow control */
  265. ret_val = hw->mac.ops.setup_fc(hw);
  266. if (ret_val)
  267. return ret_val;
  268. /* Clear adapter stopped flag */
  269. hw->adapter_stopped = false;
  270. return 0;
  271. }
  272. /**
  273. * ixgbe_start_hw_gen2 - Init sequence for common device family
  274. * @hw: pointer to hw structure
  275. *
  276. * Performs the init sequence common to the second generation
  277. * of 10 GbE devices.
  278. * Devices in the second generation:
  279. * 82599
  280. * X540
  281. **/
  282. s32 ixgbe_start_hw_gen2(struct ixgbe_hw *hw)
  283. {
  284. u32 i;
  285. /* Clear the rate limiters */
  286. for (i = 0; i < hw->mac.max_tx_queues; i++) {
  287. IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, i);
  288. IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, 0);
  289. }
  290. IXGBE_WRITE_FLUSH(hw);
  291. #ifndef CONFIG_SPARC
  292. /* Disable relaxed ordering */
  293. for (i = 0; i < hw->mac.max_tx_queues; i++) {
  294. u32 regval;
  295. regval = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(i));
  296. regval &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
  297. IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(i), regval);
  298. }
  299. for (i = 0; i < hw->mac.max_rx_queues; i++) {
  300. u32 regval;
  301. regval = IXGBE_READ_REG(hw, IXGBE_DCA_RXCTRL(i));
  302. regval &= ~(IXGBE_DCA_RXCTRL_DATA_WRO_EN |
  303. IXGBE_DCA_RXCTRL_HEAD_WRO_EN);
  304. IXGBE_WRITE_REG(hw, IXGBE_DCA_RXCTRL(i), regval);
  305. }
  306. #endif
  307. return 0;
  308. }
  309. /**
  310. * ixgbe_init_hw_generic - Generic hardware initialization
  311. * @hw: pointer to hardware structure
  312. *
  313. * Initialize the hardware by resetting the hardware, filling the bus info
  314. * structure and media type, clears all on chip counters, initializes receive
  315. * address registers, multicast table, VLAN filter table, calls routine to set
  316. * up link and flow control settings, and leaves transmit and receive units
  317. * disabled and uninitialized
  318. **/
  319. s32 ixgbe_init_hw_generic(struct ixgbe_hw *hw)
  320. {
  321. s32 status;
  322. /* Reset the hardware */
  323. status = hw->mac.ops.reset_hw(hw);
  324. if (status == 0) {
  325. /* Start the HW */
  326. status = hw->mac.ops.start_hw(hw);
  327. }
  328. return status;
  329. }
  330. /**
  331. * ixgbe_clear_hw_cntrs_generic - Generic clear hardware counters
  332. * @hw: pointer to hardware structure
  333. *
  334. * Clears all hardware statistics counters by reading them from the hardware
  335. * Statistics counters are clear on read.
  336. **/
  337. s32 ixgbe_clear_hw_cntrs_generic(struct ixgbe_hw *hw)
  338. {
  339. u16 i = 0;
  340. IXGBE_READ_REG(hw, IXGBE_CRCERRS);
  341. IXGBE_READ_REG(hw, IXGBE_ILLERRC);
  342. IXGBE_READ_REG(hw, IXGBE_ERRBC);
  343. IXGBE_READ_REG(hw, IXGBE_MSPDC);
  344. for (i = 0; i < 8; i++)
  345. IXGBE_READ_REG(hw, IXGBE_MPC(i));
  346. IXGBE_READ_REG(hw, IXGBE_MLFC);
  347. IXGBE_READ_REG(hw, IXGBE_MRFC);
  348. IXGBE_READ_REG(hw, IXGBE_RLEC);
  349. IXGBE_READ_REG(hw, IXGBE_LXONTXC);
  350. IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
  351. if (hw->mac.type >= ixgbe_mac_82599EB) {
  352. IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
  353. IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
  354. } else {
  355. IXGBE_READ_REG(hw, IXGBE_LXONRXC);
  356. IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
  357. }
  358. for (i = 0; i < 8; i++) {
  359. IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
  360. IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
  361. if (hw->mac.type >= ixgbe_mac_82599EB) {
  362. IXGBE_READ_REG(hw, IXGBE_PXONRXCNT(i));
  363. IXGBE_READ_REG(hw, IXGBE_PXOFFRXCNT(i));
  364. } else {
  365. IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
  366. IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
  367. }
  368. }
  369. if (hw->mac.type >= ixgbe_mac_82599EB)
  370. for (i = 0; i < 8; i++)
  371. IXGBE_READ_REG(hw, IXGBE_PXON2OFFCNT(i));
  372. IXGBE_READ_REG(hw, IXGBE_PRC64);
  373. IXGBE_READ_REG(hw, IXGBE_PRC127);
  374. IXGBE_READ_REG(hw, IXGBE_PRC255);
  375. IXGBE_READ_REG(hw, IXGBE_PRC511);
  376. IXGBE_READ_REG(hw, IXGBE_PRC1023);
  377. IXGBE_READ_REG(hw, IXGBE_PRC1522);
  378. IXGBE_READ_REG(hw, IXGBE_GPRC);
  379. IXGBE_READ_REG(hw, IXGBE_BPRC);
  380. IXGBE_READ_REG(hw, IXGBE_MPRC);
  381. IXGBE_READ_REG(hw, IXGBE_GPTC);
  382. IXGBE_READ_REG(hw, IXGBE_GORCL);
  383. IXGBE_READ_REG(hw, IXGBE_GORCH);
  384. IXGBE_READ_REG(hw, IXGBE_GOTCL);
  385. IXGBE_READ_REG(hw, IXGBE_GOTCH);
  386. if (hw->mac.type == ixgbe_mac_82598EB)
  387. for (i = 0; i < 8; i++)
  388. IXGBE_READ_REG(hw, IXGBE_RNBC(i));
  389. IXGBE_READ_REG(hw, IXGBE_RUC);
  390. IXGBE_READ_REG(hw, IXGBE_RFC);
  391. IXGBE_READ_REG(hw, IXGBE_ROC);
  392. IXGBE_READ_REG(hw, IXGBE_RJC);
  393. IXGBE_READ_REG(hw, IXGBE_MNGPRC);
  394. IXGBE_READ_REG(hw, IXGBE_MNGPDC);
  395. IXGBE_READ_REG(hw, IXGBE_MNGPTC);
  396. IXGBE_READ_REG(hw, IXGBE_TORL);
  397. IXGBE_READ_REG(hw, IXGBE_TORH);
  398. IXGBE_READ_REG(hw, IXGBE_TPR);
  399. IXGBE_READ_REG(hw, IXGBE_TPT);
  400. IXGBE_READ_REG(hw, IXGBE_PTC64);
  401. IXGBE_READ_REG(hw, IXGBE_PTC127);
  402. IXGBE_READ_REG(hw, IXGBE_PTC255);
  403. IXGBE_READ_REG(hw, IXGBE_PTC511);
  404. IXGBE_READ_REG(hw, IXGBE_PTC1023);
  405. IXGBE_READ_REG(hw, IXGBE_PTC1522);
  406. IXGBE_READ_REG(hw, IXGBE_MPTC);
  407. IXGBE_READ_REG(hw, IXGBE_BPTC);
  408. for (i = 0; i < 16; i++) {
  409. IXGBE_READ_REG(hw, IXGBE_QPRC(i));
  410. IXGBE_READ_REG(hw, IXGBE_QPTC(i));
  411. if (hw->mac.type >= ixgbe_mac_82599EB) {
  412. IXGBE_READ_REG(hw, IXGBE_QBRC_L(i));
  413. IXGBE_READ_REG(hw, IXGBE_QBRC_H(i));
  414. IXGBE_READ_REG(hw, IXGBE_QBTC_L(i));
  415. IXGBE_READ_REG(hw, IXGBE_QBTC_H(i));
  416. IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
  417. } else {
  418. IXGBE_READ_REG(hw, IXGBE_QBRC(i));
  419. IXGBE_READ_REG(hw, IXGBE_QBTC(i));
  420. }
  421. }
  422. if (hw->mac.type == ixgbe_mac_X550 || hw->mac.type == ixgbe_mac_X540) {
  423. if (hw->phy.id == 0)
  424. hw->phy.ops.identify(hw);
  425. hw->phy.ops.read_reg(hw, IXGBE_PCRC8ECL, MDIO_MMD_PCS, &i);
  426. hw->phy.ops.read_reg(hw, IXGBE_PCRC8ECH, MDIO_MMD_PCS, &i);
  427. hw->phy.ops.read_reg(hw, IXGBE_LDPCECL, MDIO_MMD_PCS, &i);
  428. hw->phy.ops.read_reg(hw, IXGBE_LDPCECH, MDIO_MMD_PCS, &i);
  429. }
  430. return 0;
  431. }
  432. /**
  433. * ixgbe_read_pba_string_generic - Reads part number string from EEPROM
  434. * @hw: pointer to hardware structure
  435. * @pba_num: stores the part number string from the EEPROM
  436. * @pba_num_size: part number string buffer length
  437. *
  438. * Reads the part number string from the EEPROM.
  439. **/
  440. s32 ixgbe_read_pba_string_generic(struct ixgbe_hw *hw, u8 *pba_num,
  441. u32 pba_num_size)
  442. {
  443. s32 ret_val;
  444. u16 data;
  445. u16 pba_ptr;
  446. u16 offset;
  447. u16 length;
  448. if (pba_num == NULL) {
  449. hw_dbg(hw, "PBA string buffer was null\n");
  450. return IXGBE_ERR_INVALID_ARGUMENT;
  451. }
  452. ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM0_PTR, &data);
  453. if (ret_val) {
  454. hw_dbg(hw, "NVM Read Error\n");
  455. return ret_val;
  456. }
  457. ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM1_PTR, &pba_ptr);
  458. if (ret_val) {
  459. hw_dbg(hw, "NVM Read Error\n");
  460. return ret_val;
  461. }
  462. /*
  463. * if data is not ptr guard the PBA must be in legacy format which
  464. * means pba_ptr is actually our second data word for the PBA number
  465. * and we can decode it into an ascii string
  466. */
  467. if (data != IXGBE_PBANUM_PTR_GUARD) {
  468. hw_dbg(hw, "NVM PBA number is not stored as string\n");
  469. /* we will need 11 characters to store the PBA */
  470. if (pba_num_size < 11) {
  471. hw_dbg(hw, "PBA string buffer too small\n");
  472. return IXGBE_ERR_NO_SPACE;
  473. }
  474. /* extract hex string from data and pba_ptr */
  475. pba_num[0] = (data >> 12) & 0xF;
  476. pba_num[1] = (data >> 8) & 0xF;
  477. pba_num[2] = (data >> 4) & 0xF;
  478. pba_num[3] = data & 0xF;
  479. pba_num[4] = (pba_ptr >> 12) & 0xF;
  480. pba_num[5] = (pba_ptr >> 8) & 0xF;
  481. pba_num[6] = '-';
  482. pba_num[7] = 0;
  483. pba_num[8] = (pba_ptr >> 4) & 0xF;
  484. pba_num[9] = pba_ptr & 0xF;
  485. /* put a null character on the end of our string */
  486. pba_num[10] = '\0';
  487. /* switch all the data but the '-' to hex char */
  488. for (offset = 0; offset < 10; offset++) {
  489. if (pba_num[offset] < 0xA)
  490. pba_num[offset] += '0';
  491. else if (pba_num[offset] < 0x10)
  492. pba_num[offset] += 'A' - 0xA;
  493. }
  494. return 0;
  495. }
  496. ret_val = hw->eeprom.ops.read(hw, pba_ptr, &length);
  497. if (ret_val) {
  498. hw_dbg(hw, "NVM Read Error\n");
  499. return ret_val;
  500. }
  501. if (length == 0xFFFF || length == 0) {
  502. hw_dbg(hw, "NVM PBA number section invalid length\n");
  503. return IXGBE_ERR_PBA_SECTION;
  504. }
  505. /* check if pba_num buffer is big enough */
  506. if (pba_num_size < (((u32)length * 2) - 1)) {
  507. hw_dbg(hw, "PBA string buffer too small\n");
  508. return IXGBE_ERR_NO_SPACE;
  509. }
  510. /* trim pba length from start of string */
  511. pba_ptr++;
  512. length--;
  513. for (offset = 0; offset < length; offset++) {
  514. ret_val = hw->eeprom.ops.read(hw, pba_ptr + offset, &data);
  515. if (ret_val) {
  516. hw_dbg(hw, "NVM Read Error\n");
  517. return ret_val;
  518. }
  519. pba_num[offset * 2] = (u8)(data >> 8);
  520. pba_num[(offset * 2) + 1] = (u8)(data & 0xFF);
  521. }
  522. pba_num[offset * 2] = '\0';
  523. return 0;
  524. }
  525. /**
  526. * ixgbe_get_mac_addr_generic - Generic get MAC address
  527. * @hw: pointer to hardware structure
  528. * @mac_addr: Adapter MAC address
  529. *
  530. * Reads the adapter's MAC address from first Receive Address Register (RAR0)
  531. * A reset of the adapter must be performed prior to calling this function
  532. * in order for the MAC address to have been loaded from the EEPROM into RAR0
  533. **/
  534. s32 ixgbe_get_mac_addr_generic(struct ixgbe_hw *hw, u8 *mac_addr)
  535. {
  536. u32 rar_high;
  537. u32 rar_low;
  538. u16 i;
  539. rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(0));
  540. rar_low = IXGBE_READ_REG(hw, IXGBE_RAL(0));
  541. for (i = 0; i < 4; i++)
  542. mac_addr[i] = (u8)(rar_low >> (i*8));
  543. for (i = 0; i < 2; i++)
  544. mac_addr[i+4] = (u8)(rar_high >> (i*8));
  545. return 0;
  546. }
  547. enum ixgbe_bus_width ixgbe_convert_bus_width(u16 link_status)
  548. {
  549. switch (link_status & IXGBE_PCI_LINK_WIDTH) {
  550. case IXGBE_PCI_LINK_WIDTH_1:
  551. return ixgbe_bus_width_pcie_x1;
  552. case IXGBE_PCI_LINK_WIDTH_2:
  553. return ixgbe_bus_width_pcie_x2;
  554. case IXGBE_PCI_LINK_WIDTH_4:
  555. return ixgbe_bus_width_pcie_x4;
  556. case IXGBE_PCI_LINK_WIDTH_8:
  557. return ixgbe_bus_width_pcie_x8;
  558. default:
  559. return ixgbe_bus_width_unknown;
  560. }
  561. }
  562. enum ixgbe_bus_speed ixgbe_convert_bus_speed(u16 link_status)
  563. {
  564. switch (link_status & IXGBE_PCI_LINK_SPEED) {
  565. case IXGBE_PCI_LINK_SPEED_2500:
  566. return ixgbe_bus_speed_2500;
  567. case IXGBE_PCI_LINK_SPEED_5000:
  568. return ixgbe_bus_speed_5000;
  569. case IXGBE_PCI_LINK_SPEED_8000:
  570. return ixgbe_bus_speed_8000;
  571. default:
  572. return ixgbe_bus_speed_unknown;
  573. }
  574. }
  575. /**
  576. * ixgbe_get_bus_info_generic - Generic set PCI bus info
  577. * @hw: pointer to hardware structure
  578. *
  579. * Sets the PCI bus info (speed, width, type) within the ixgbe_hw structure
  580. **/
  581. s32 ixgbe_get_bus_info_generic(struct ixgbe_hw *hw)
  582. {
  583. u16 link_status;
  584. hw->bus.type = ixgbe_bus_type_pci_express;
  585. /* Get the negotiated link width and speed from PCI config space */
  586. link_status = ixgbe_read_pci_cfg_word(hw, IXGBE_PCI_LINK_STATUS);
  587. hw->bus.width = ixgbe_convert_bus_width(link_status);
  588. hw->bus.speed = ixgbe_convert_bus_speed(link_status);
  589. hw->mac.ops.set_lan_id(hw);
  590. return 0;
  591. }
  592. /**
  593. * ixgbe_set_lan_id_multi_port_pcie - Set LAN id for PCIe multiple port devices
  594. * @hw: pointer to the HW structure
  595. *
  596. * Determines the LAN function id by reading memory-mapped registers
  597. * and swaps the port value if requested.
  598. **/
  599. void ixgbe_set_lan_id_multi_port_pcie(struct ixgbe_hw *hw)
  600. {
  601. struct ixgbe_bus_info *bus = &hw->bus;
  602. u16 ee_ctrl_4;
  603. u32 reg;
  604. reg = IXGBE_READ_REG(hw, IXGBE_STATUS);
  605. bus->func = (reg & IXGBE_STATUS_LAN_ID) >> IXGBE_STATUS_LAN_ID_SHIFT;
  606. bus->lan_id = bus->func;
  607. /* check for a port swap */
  608. reg = IXGBE_READ_REG(hw, IXGBE_FACTPS(hw));
  609. if (reg & IXGBE_FACTPS_LFS)
  610. bus->func ^= 0x1;
  611. /* Get MAC instance from EEPROM for configuring CS4227 */
  612. if (hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP) {
  613. hw->eeprom.ops.read(hw, IXGBE_EEPROM_CTRL_4, &ee_ctrl_4);
  614. bus->instance_id = (ee_ctrl_4 & IXGBE_EE_CTRL_4_INST_ID) >>
  615. IXGBE_EE_CTRL_4_INST_ID_SHIFT;
  616. }
  617. }
  618. /**
  619. * ixgbe_stop_adapter_generic - Generic stop Tx/Rx units
  620. * @hw: pointer to hardware structure
  621. *
  622. * Sets the adapter_stopped flag within ixgbe_hw struct. Clears interrupts,
  623. * disables transmit and receive units. The adapter_stopped flag is used by
  624. * the shared code and drivers to determine if the adapter is in a stopped
  625. * state and should not touch the hardware.
  626. **/
  627. s32 ixgbe_stop_adapter_generic(struct ixgbe_hw *hw)
  628. {
  629. u32 reg_val;
  630. u16 i;
  631. /*
  632. * Set the adapter_stopped flag so other driver functions stop touching
  633. * the hardware
  634. */
  635. hw->adapter_stopped = true;
  636. /* Disable the receive unit */
  637. hw->mac.ops.disable_rx(hw);
  638. /* Clear interrupt mask to stop interrupts from being generated */
  639. IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_IRQ_CLEAR_MASK);
  640. /* Clear any pending interrupts, flush previous writes */
  641. IXGBE_READ_REG(hw, IXGBE_EICR);
  642. /* Disable the transmit unit. Each queue must be disabled. */
  643. for (i = 0; i < hw->mac.max_tx_queues; i++)
  644. IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(i), IXGBE_TXDCTL_SWFLSH);
  645. /* Disable the receive unit by stopping each queue */
  646. for (i = 0; i < hw->mac.max_rx_queues; i++) {
  647. reg_val = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
  648. reg_val &= ~IXGBE_RXDCTL_ENABLE;
  649. reg_val |= IXGBE_RXDCTL_SWFLSH;
  650. IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), reg_val);
  651. }
  652. /* flush all queues disables */
  653. IXGBE_WRITE_FLUSH(hw);
  654. usleep_range(1000, 2000);
  655. /*
  656. * Prevent the PCI-E bus from from hanging by disabling PCI-E master
  657. * access and verify no pending requests
  658. */
  659. return ixgbe_disable_pcie_master(hw);
  660. }
  661. /**
  662. * ixgbe_led_on_generic - Turns on the software controllable LEDs.
  663. * @hw: pointer to hardware structure
  664. * @index: led number to turn on
  665. **/
  666. s32 ixgbe_led_on_generic(struct ixgbe_hw *hw, u32 index)
  667. {
  668. u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
  669. /* To turn on the LED, set mode to ON. */
  670. led_reg &= ~IXGBE_LED_MODE_MASK(index);
  671. led_reg |= IXGBE_LED_ON << IXGBE_LED_MODE_SHIFT(index);
  672. IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
  673. IXGBE_WRITE_FLUSH(hw);
  674. return 0;
  675. }
  676. /**
  677. * ixgbe_led_off_generic - Turns off the software controllable LEDs.
  678. * @hw: pointer to hardware structure
  679. * @index: led number to turn off
  680. **/
  681. s32 ixgbe_led_off_generic(struct ixgbe_hw *hw, u32 index)
  682. {
  683. u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
  684. /* To turn off the LED, set mode to OFF. */
  685. led_reg &= ~IXGBE_LED_MODE_MASK(index);
  686. led_reg |= IXGBE_LED_OFF << IXGBE_LED_MODE_SHIFT(index);
  687. IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
  688. IXGBE_WRITE_FLUSH(hw);
  689. return 0;
  690. }
  691. /**
  692. * ixgbe_init_eeprom_params_generic - Initialize EEPROM params
  693. * @hw: pointer to hardware structure
  694. *
  695. * Initializes the EEPROM parameters ixgbe_eeprom_info within the
  696. * ixgbe_hw struct in order to set up EEPROM access.
  697. **/
  698. s32 ixgbe_init_eeprom_params_generic(struct ixgbe_hw *hw)
  699. {
  700. struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
  701. u32 eec;
  702. u16 eeprom_size;
  703. if (eeprom->type == ixgbe_eeprom_uninitialized) {
  704. eeprom->type = ixgbe_eeprom_none;
  705. /* Set default semaphore delay to 10ms which is a well
  706. * tested value */
  707. eeprom->semaphore_delay = 10;
  708. /* Clear EEPROM page size, it will be initialized as needed */
  709. eeprom->word_page_size = 0;
  710. /*
  711. * Check for EEPROM present first.
  712. * If not present leave as none
  713. */
  714. eec = IXGBE_READ_REG(hw, IXGBE_EEC(hw));
  715. if (eec & IXGBE_EEC_PRES) {
  716. eeprom->type = ixgbe_eeprom_spi;
  717. /*
  718. * SPI EEPROM is assumed here. This code would need to
  719. * change if a future EEPROM is not SPI.
  720. */
  721. eeprom_size = (u16)((eec & IXGBE_EEC_SIZE) >>
  722. IXGBE_EEC_SIZE_SHIFT);
  723. eeprom->word_size = 1 << (eeprom_size +
  724. IXGBE_EEPROM_WORD_SIZE_SHIFT);
  725. }
  726. if (eec & IXGBE_EEC_ADDR_SIZE)
  727. eeprom->address_bits = 16;
  728. else
  729. eeprom->address_bits = 8;
  730. hw_dbg(hw, "Eeprom params: type = %d, size = %d, address bits: %d\n",
  731. eeprom->type, eeprom->word_size, eeprom->address_bits);
  732. }
  733. return 0;
  734. }
  735. /**
  736. * ixgbe_write_eeprom_buffer_bit_bang_generic - Write EEPROM using bit-bang
  737. * @hw: pointer to hardware structure
  738. * @offset: offset within the EEPROM to write
  739. * @words: number of words
  740. * @data: 16 bit word(s) to write to EEPROM
  741. *
  742. * Reads 16 bit word(s) from EEPROM through bit-bang method
  743. **/
  744. s32 ixgbe_write_eeprom_buffer_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
  745. u16 words, u16 *data)
  746. {
  747. s32 status;
  748. u16 i, count;
  749. hw->eeprom.ops.init_params(hw);
  750. if (words == 0)
  751. return IXGBE_ERR_INVALID_ARGUMENT;
  752. if (offset + words > hw->eeprom.word_size)
  753. return IXGBE_ERR_EEPROM;
  754. /*
  755. * The EEPROM page size cannot be queried from the chip. We do lazy
  756. * initialization. It is worth to do that when we write large buffer.
  757. */
  758. if ((hw->eeprom.word_page_size == 0) &&
  759. (words > IXGBE_EEPROM_PAGE_SIZE_MAX))
  760. ixgbe_detect_eeprom_page_size_generic(hw, offset);
  761. /*
  762. * We cannot hold synchronization semaphores for too long
  763. * to avoid other entity starvation. However it is more efficient
  764. * to read in bursts than synchronizing access for each word.
  765. */
  766. for (i = 0; i < words; i += IXGBE_EEPROM_RD_BUFFER_MAX_COUNT) {
  767. count = (words - i) / IXGBE_EEPROM_RD_BUFFER_MAX_COUNT > 0 ?
  768. IXGBE_EEPROM_RD_BUFFER_MAX_COUNT : (words - i);
  769. status = ixgbe_write_eeprom_buffer_bit_bang(hw, offset + i,
  770. count, &data[i]);
  771. if (status != 0)
  772. break;
  773. }
  774. return status;
  775. }
  776. /**
  777. * ixgbe_write_eeprom_buffer_bit_bang - Writes 16 bit word(s) to EEPROM
  778. * @hw: pointer to hardware structure
  779. * @offset: offset within the EEPROM to be written to
  780. * @words: number of word(s)
  781. * @data: 16 bit word(s) to be written to the EEPROM
  782. *
  783. * If ixgbe_eeprom_update_checksum is not called after this function, the
  784. * EEPROM will most likely contain an invalid checksum.
  785. **/
  786. static s32 ixgbe_write_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
  787. u16 words, u16 *data)
  788. {
  789. s32 status;
  790. u16 word;
  791. u16 page_size;
  792. u16 i;
  793. u8 write_opcode = IXGBE_EEPROM_WRITE_OPCODE_SPI;
  794. /* Prepare the EEPROM for writing */
  795. status = ixgbe_acquire_eeprom(hw);
  796. if (status)
  797. return status;
  798. if (ixgbe_ready_eeprom(hw) != 0) {
  799. ixgbe_release_eeprom(hw);
  800. return IXGBE_ERR_EEPROM;
  801. }
  802. for (i = 0; i < words; i++) {
  803. ixgbe_standby_eeprom(hw);
  804. /* Send the WRITE ENABLE command (8 bit opcode) */
  805. ixgbe_shift_out_eeprom_bits(hw,
  806. IXGBE_EEPROM_WREN_OPCODE_SPI,
  807. IXGBE_EEPROM_OPCODE_BITS);
  808. ixgbe_standby_eeprom(hw);
  809. /* Some SPI eeproms use the 8th address bit embedded
  810. * in the opcode
  811. */
  812. if ((hw->eeprom.address_bits == 8) &&
  813. ((offset + i) >= 128))
  814. write_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
  815. /* Send the Write command (8-bit opcode + addr) */
  816. ixgbe_shift_out_eeprom_bits(hw, write_opcode,
  817. IXGBE_EEPROM_OPCODE_BITS);
  818. ixgbe_shift_out_eeprom_bits(hw, (u16)((offset + i) * 2),
  819. hw->eeprom.address_bits);
  820. page_size = hw->eeprom.word_page_size;
  821. /* Send the data in burst via SPI */
  822. do {
  823. word = data[i];
  824. word = (word >> 8) | (word << 8);
  825. ixgbe_shift_out_eeprom_bits(hw, word, 16);
  826. if (page_size == 0)
  827. break;
  828. /* do not wrap around page */
  829. if (((offset + i) & (page_size - 1)) ==
  830. (page_size - 1))
  831. break;
  832. } while (++i < words);
  833. ixgbe_standby_eeprom(hw);
  834. usleep_range(10000, 20000);
  835. }
  836. /* Done with writing - release the EEPROM */
  837. ixgbe_release_eeprom(hw);
  838. return 0;
  839. }
  840. /**
  841. * ixgbe_write_eeprom_generic - Writes 16 bit value to EEPROM
  842. * @hw: pointer to hardware structure
  843. * @offset: offset within the EEPROM to be written to
  844. * @data: 16 bit word to be written to the EEPROM
  845. *
  846. * If ixgbe_eeprom_update_checksum is not called after this function, the
  847. * EEPROM will most likely contain an invalid checksum.
  848. **/
  849. s32 ixgbe_write_eeprom_generic(struct ixgbe_hw *hw, u16 offset, u16 data)
  850. {
  851. hw->eeprom.ops.init_params(hw);
  852. if (offset >= hw->eeprom.word_size)
  853. return IXGBE_ERR_EEPROM;
  854. return ixgbe_write_eeprom_buffer_bit_bang(hw, offset, 1, &data);
  855. }
  856. /**
  857. * ixgbe_read_eeprom_buffer_bit_bang_generic - Read EEPROM using bit-bang
  858. * @hw: pointer to hardware structure
  859. * @offset: offset within the EEPROM to be read
  860. * @words: number of word(s)
  861. * @data: read 16 bit words(s) from EEPROM
  862. *
  863. * Reads 16 bit word(s) from EEPROM through bit-bang method
  864. **/
  865. s32 ixgbe_read_eeprom_buffer_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
  866. u16 words, u16 *data)
  867. {
  868. s32 status;
  869. u16 i, count;
  870. hw->eeprom.ops.init_params(hw);
  871. if (words == 0)
  872. return IXGBE_ERR_INVALID_ARGUMENT;
  873. if (offset + words > hw->eeprom.word_size)
  874. return IXGBE_ERR_EEPROM;
  875. /*
  876. * We cannot hold synchronization semaphores for too long
  877. * to avoid other entity starvation. However it is more efficient
  878. * to read in bursts than synchronizing access for each word.
  879. */
  880. for (i = 0; i < words; i += IXGBE_EEPROM_RD_BUFFER_MAX_COUNT) {
  881. count = (words - i) / IXGBE_EEPROM_RD_BUFFER_MAX_COUNT > 0 ?
  882. IXGBE_EEPROM_RD_BUFFER_MAX_COUNT : (words - i);
  883. status = ixgbe_read_eeprom_buffer_bit_bang(hw, offset + i,
  884. count, &data[i]);
  885. if (status)
  886. return status;
  887. }
  888. return 0;
  889. }
  890. /**
  891. * ixgbe_read_eeprom_buffer_bit_bang - Read EEPROM using bit-bang
  892. * @hw: pointer to hardware structure
  893. * @offset: offset within the EEPROM to be read
  894. * @words: number of word(s)
  895. * @data: read 16 bit word(s) from EEPROM
  896. *
  897. * Reads 16 bit word(s) from EEPROM through bit-bang method
  898. **/
  899. static s32 ixgbe_read_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
  900. u16 words, u16 *data)
  901. {
  902. s32 status;
  903. u16 word_in;
  904. u8 read_opcode = IXGBE_EEPROM_READ_OPCODE_SPI;
  905. u16 i;
  906. /* Prepare the EEPROM for reading */
  907. status = ixgbe_acquire_eeprom(hw);
  908. if (status)
  909. return status;
  910. if (ixgbe_ready_eeprom(hw) != 0) {
  911. ixgbe_release_eeprom(hw);
  912. return IXGBE_ERR_EEPROM;
  913. }
  914. for (i = 0; i < words; i++) {
  915. ixgbe_standby_eeprom(hw);
  916. /* Some SPI eeproms use the 8th address bit embedded
  917. * in the opcode
  918. */
  919. if ((hw->eeprom.address_bits == 8) &&
  920. ((offset + i) >= 128))
  921. read_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
  922. /* Send the READ command (opcode + addr) */
  923. ixgbe_shift_out_eeprom_bits(hw, read_opcode,
  924. IXGBE_EEPROM_OPCODE_BITS);
  925. ixgbe_shift_out_eeprom_bits(hw, (u16)((offset + i) * 2),
  926. hw->eeprom.address_bits);
  927. /* Read the data. */
  928. word_in = ixgbe_shift_in_eeprom_bits(hw, 16);
  929. data[i] = (word_in >> 8) | (word_in << 8);
  930. }
  931. /* End this read operation */
  932. ixgbe_release_eeprom(hw);
  933. return 0;
  934. }
  935. /**
  936. * ixgbe_read_eeprom_bit_bang_generic - Read EEPROM word using bit-bang
  937. * @hw: pointer to hardware structure
  938. * @offset: offset within the EEPROM to be read
  939. * @data: read 16 bit value from EEPROM
  940. *
  941. * Reads 16 bit value from EEPROM through bit-bang method
  942. **/
  943. s32 ixgbe_read_eeprom_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
  944. u16 *data)
  945. {
  946. hw->eeprom.ops.init_params(hw);
  947. if (offset >= hw->eeprom.word_size)
  948. return IXGBE_ERR_EEPROM;
  949. return ixgbe_read_eeprom_buffer_bit_bang(hw, offset, 1, data);
  950. }
  951. /**
  952. * ixgbe_read_eerd_buffer_generic - Read EEPROM word(s) using EERD
  953. * @hw: pointer to hardware structure
  954. * @offset: offset of word in the EEPROM to read
  955. * @words: number of word(s)
  956. * @data: 16 bit word(s) from the EEPROM
  957. *
  958. * Reads a 16 bit word(s) from the EEPROM using the EERD register.
  959. **/
  960. s32 ixgbe_read_eerd_buffer_generic(struct ixgbe_hw *hw, u16 offset,
  961. u16 words, u16 *data)
  962. {
  963. u32 eerd;
  964. s32 status;
  965. u32 i;
  966. hw->eeprom.ops.init_params(hw);
  967. if (words == 0)
  968. return IXGBE_ERR_INVALID_ARGUMENT;
  969. if (offset >= hw->eeprom.word_size)
  970. return IXGBE_ERR_EEPROM;
  971. for (i = 0; i < words; i++) {
  972. eerd = ((offset + i) << IXGBE_EEPROM_RW_ADDR_SHIFT) |
  973. IXGBE_EEPROM_RW_REG_START;
  974. IXGBE_WRITE_REG(hw, IXGBE_EERD, eerd);
  975. status = ixgbe_poll_eerd_eewr_done(hw, IXGBE_NVM_POLL_READ);
  976. if (status == 0) {
  977. data[i] = (IXGBE_READ_REG(hw, IXGBE_EERD) >>
  978. IXGBE_EEPROM_RW_REG_DATA);
  979. } else {
  980. hw_dbg(hw, "Eeprom read timed out\n");
  981. return status;
  982. }
  983. }
  984. return 0;
  985. }
  986. /**
  987. * ixgbe_detect_eeprom_page_size_generic - Detect EEPROM page size
  988. * @hw: pointer to hardware structure
  989. * @offset: offset within the EEPROM to be used as a scratch pad
  990. *
  991. * Discover EEPROM page size by writing marching data at given offset.
  992. * This function is called only when we are writing a new large buffer
  993. * at given offset so the data would be overwritten anyway.
  994. **/
  995. static s32 ixgbe_detect_eeprom_page_size_generic(struct ixgbe_hw *hw,
  996. u16 offset)
  997. {
  998. u16 data[IXGBE_EEPROM_PAGE_SIZE_MAX];
  999. s32 status;
  1000. u16 i;
  1001. for (i = 0; i < IXGBE_EEPROM_PAGE_SIZE_MAX; i++)
  1002. data[i] = i;
  1003. hw->eeprom.word_page_size = IXGBE_EEPROM_PAGE_SIZE_MAX;
  1004. status = ixgbe_write_eeprom_buffer_bit_bang(hw, offset,
  1005. IXGBE_EEPROM_PAGE_SIZE_MAX, data);
  1006. hw->eeprom.word_page_size = 0;
  1007. if (status)
  1008. return status;
  1009. status = ixgbe_read_eeprom_buffer_bit_bang(hw, offset, 1, data);
  1010. if (status)
  1011. return status;
  1012. /*
  1013. * When writing in burst more than the actual page size
  1014. * EEPROM address wraps around current page.
  1015. */
  1016. hw->eeprom.word_page_size = IXGBE_EEPROM_PAGE_SIZE_MAX - data[0];
  1017. hw_dbg(hw, "Detected EEPROM page size = %d words.\n",
  1018. hw->eeprom.word_page_size);
  1019. return 0;
  1020. }
  1021. /**
  1022. * ixgbe_read_eerd_generic - Read EEPROM word using EERD
  1023. * @hw: pointer to hardware structure
  1024. * @offset: offset of word in the EEPROM to read
  1025. * @data: word read from the EEPROM
  1026. *
  1027. * Reads a 16 bit word from the EEPROM using the EERD register.
  1028. **/
  1029. s32 ixgbe_read_eerd_generic(struct ixgbe_hw *hw, u16 offset, u16 *data)
  1030. {
  1031. return ixgbe_read_eerd_buffer_generic(hw, offset, 1, data);
  1032. }
  1033. /**
  1034. * ixgbe_write_eewr_buffer_generic - Write EEPROM word(s) using EEWR
  1035. * @hw: pointer to hardware structure
  1036. * @offset: offset of word in the EEPROM to write
  1037. * @words: number of words
  1038. * @data: word(s) write to the EEPROM
  1039. *
  1040. * Write a 16 bit word(s) to the EEPROM using the EEWR register.
  1041. **/
  1042. s32 ixgbe_write_eewr_buffer_generic(struct ixgbe_hw *hw, u16 offset,
  1043. u16 words, u16 *data)
  1044. {
  1045. u32 eewr;
  1046. s32 status;
  1047. u16 i;
  1048. hw->eeprom.ops.init_params(hw);
  1049. if (words == 0)
  1050. return IXGBE_ERR_INVALID_ARGUMENT;
  1051. if (offset >= hw->eeprom.word_size)
  1052. return IXGBE_ERR_EEPROM;
  1053. for (i = 0; i < words; i++) {
  1054. eewr = ((offset + i) << IXGBE_EEPROM_RW_ADDR_SHIFT) |
  1055. (data[i] << IXGBE_EEPROM_RW_REG_DATA) |
  1056. IXGBE_EEPROM_RW_REG_START;
  1057. status = ixgbe_poll_eerd_eewr_done(hw, IXGBE_NVM_POLL_WRITE);
  1058. if (status) {
  1059. hw_dbg(hw, "Eeprom write EEWR timed out\n");
  1060. return status;
  1061. }
  1062. IXGBE_WRITE_REG(hw, IXGBE_EEWR, eewr);
  1063. status = ixgbe_poll_eerd_eewr_done(hw, IXGBE_NVM_POLL_WRITE);
  1064. if (status) {
  1065. hw_dbg(hw, "Eeprom write EEWR timed out\n");
  1066. return status;
  1067. }
  1068. }
  1069. return 0;
  1070. }
  1071. /**
  1072. * ixgbe_write_eewr_generic - Write EEPROM word using EEWR
  1073. * @hw: pointer to hardware structure
  1074. * @offset: offset of word in the EEPROM to write
  1075. * @data: word write to the EEPROM
  1076. *
  1077. * Write a 16 bit word to the EEPROM using the EEWR register.
  1078. **/
  1079. s32 ixgbe_write_eewr_generic(struct ixgbe_hw *hw, u16 offset, u16 data)
  1080. {
  1081. return ixgbe_write_eewr_buffer_generic(hw, offset, 1, &data);
  1082. }
  1083. /**
  1084. * ixgbe_poll_eerd_eewr_done - Poll EERD read or EEWR write status
  1085. * @hw: pointer to hardware structure
  1086. * @ee_reg: EEPROM flag for polling
  1087. *
  1088. * Polls the status bit (bit 1) of the EERD or EEWR to determine when the
  1089. * read or write is done respectively.
  1090. **/
  1091. static s32 ixgbe_poll_eerd_eewr_done(struct ixgbe_hw *hw, u32 ee_reg)
  1092. {
  1093. u32 i;
  1094. u32 reg;
  1095. for (i = 0; i < IXGBE_EERD_EEWR_ATTEMPTS; i++) {
  1096. if (ee_reg == IXGBE_NVM_POLL_READ)
  1097. reg = IXGBE_READ_REG(hw, IXGBE_EERD);
  1098. else
  1099. reg = IXGBE_READ_REG(hw, IXGBE_EEWR);
  1100. if (reg & IXGBE_EEPROM_RW_REG_DONE) {
  1101. return 0;
  1102. }
  1103. udelay(5);
  1104. }
  1105. return IXGBE_ERR_EEPROM;
  1106. }
  1107. /**
  1108. * ixgbe_acquire_eeprom - Acquire EEPROM using bit-bang
  1109. * @hw: pointer to hardware structure
  1110. *
  1111. * Prepares EEPROM for access using bit-bang method. This function should
  1112. * be called before issuing a command to the EEPROM.
  1113. **/
  1114. static s32 ixgbe_acquire_eeprom(struct ixgbe_hw *hw)
  1115. {
  1116. u32 eec;
  1117. u32 i;
  1118. if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM) != 0)
  1119. return IXGBE_ERR_SWFW_SYNC;
  1120. eec = IXGBE_READ_REG(hw, IXGBE_EEC(hw));
  1121. /* Request EEPROM Access */
  1122. eec |= IXGBE_EEC_REQ;
  1123. IXGBE_WRITE_REG(hw, IXGBE_EEC(hw), eec);
  1124. for (i = 0; i < IXGBE_EEPROM_GRANT_ATTEMPTS; i++) {
  1125. eec = IXGBE_READ_REG(hw, IXGBE_EEC(hw));
  1126. if (eec & IXGBE_EEC_GNT)
  1127. break;
  1128. udelay(5);
  1129. }
  1130. /* Release if grant not acquired */
  1131. if (!(eec & IXGBE_EEC_GNT)) {
  1132. eec &= ~IXGBE_EEC_REQ;
  1133. IXGBE_WRITE_REG(hw, IXGBE_EEC(hw), eec);
  1134. hw_dbg(hw, "Could not acquire EEPROM grant\n");
  1135. hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
  1136. return IXGBE_ERR_EEPROM;
  1137. }
  1138. /* Setup EEPROM for Read/Write */
  1139. /* Clear CS and SK */
  1140. eec &= ~(IXGBE_EEC_CS | IXGBE_EEC_SK);
  1141. IXGBE_WRITE_REG(hw, IXGBE_EEC(hw), eec);
  1142. IXGBE_WRITE_FLUSH(hw);
  1143. udelay(1);
  1144. return 0;
  1145. }
  1146. /**
  1147. * ixgbe_get_eeprom_semaphore - Get hardware semaphore
  1148. * @hw: pointer to hardware structure
  1149. *
  1150. * Sets the hardware semaphores so EEPROM access can occur for bit-bang method
  1151. **/
  1152. static s32 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw)
  1153. {
  1154. u32 timeout = 2000;
  1155. u32 i;
  1156. u32 swsm;
  1157. /* Get SMBI software semaphore between device drivers first */
  1158. for (i = 0; i < timeout; i++) {
  1159. /*
  1160. * If the SMBI bit is 0 when we read it, then the bit will be
  1161. * set and we have the semaphore
  1162. */
  1163. swsm = IXGBE_READ_REG(hw, IXGBE_SWSM(hw));
  1164. if (!(swsm & IXGBE_SWSM_SMBI))
  1165. break;
  1166. usleep_range(50, 100);
  1167. }
  1168. if (i == timeout) {
  1169. hw_dbg(hw, "Driver can't access the Eeprom - SMBI Semaphore not granted.\n");
  1170. /* this release is particularly important because our attempts
  1171. * above to get the semaphore may have succeeded, and if there
  1172. * was a timeout, we should unconditionally clear the semaphore
  1173. * bits to free the driver to make progress
  1174. */
  1175. ixgbe_release_eeprom_semaphore(hw);
  1176. usleep_range(50, 100);
  1177. /* one last try
  1178. * If the SMBI bit is 0 when we read it, then the bit will be
  1179. * set and we have the semaphore
  1180. */
  1181. swsm = IXGBE_READ_REG(hw, IXGBE_SWSM(hw));
  1182. if (swsm & IXGBE_SWSM_SMBI) {
  1183. hw_dbg(hw, "Software semaphore SMBI between device drivers not granted.\n");
  1184. return IXGBE_ERR_EEPROM;
  1185. }
  1186. }
  1187. /* Now get the semaphore between SW/FW through the SWESMBI bit */
  1188. for (i = 0; i < timeout; i++) {
  1189. swsm = IXGBE_READ_REG(hw, IXGBE_SWSM(hw));
  1190. /* Set the SW EEPROM semaphore bit to request access */
  1191. swsm |= IXGBE_SWSM_SWESMBI;
  1192. IXGBE_WRITE_REG(hw, IXGBE_SWSM(hw), swsm);
  1193. /* If we set the bit successfully then we got the
  1194. * semaphore.
  1195. */
  1196. swsm = IXGBE_READ_REG(hw, IXGBE_SWSM(hw));
  1197. if (swsm & IXGBE_SWSM_SWESMBI)
  1198. break;
  1199. usleep_range(50, 100);
  1200. }
  1201. /* Release semaphores and return error if SW EEPROM semaphore
  1202. * was not granted because we don't have access to the EEPROM
  1203. */
  1204. if (i >= timeout) {
  1205. hw_dbg(hw, "SWESMBI Software EEPROM semaphore not granted.\n");
  1206. ixgbe_release_eeprom_semaphore(hw);
  1207. return IXGBE_ERR_EEPROM;
  1208. }
  1209. return 0;
  1210. }
  1211. /**
  1212. * ixgbe_release_eeprom_semaphore - Release hardware semaphore
  1213. * @hw: pointer to hardware structure
  1214. *
  1215. * This function clears hardware semaphore bits.
  1216. **/
  1217. static void ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw)
  1218. {
  1219. u32 swsm;
  1220. swsm = IXGBE_READ_REG(hw, IXGBE_SWSM(hw));
  1221. /* Release both semaphores by writing 0 to the bits SWESMBI and SMBI */
  1222. swsm &= ~(IXGBE_SWSM_SWESMBI | IXGBE_SWSM_SMBI);
  1223. IXGBE_WRITE_REG(hw, IXGBE_SWSM(hw), swsm);
  1224. IXGBE_WRITE_FLUSH(hw);
  1225. }
  1226. /**
  1227. * ixgbe_ready_eeprom - Polls for EEPROM ready
  1228. * @hw: pointer to hardware structure
  1229. **/
  1230. static s32 ixgbe_ready_eeprom(struct ixgbe_hw *hw)
  1231. {
  1232. u16 i;
  1233. u8 spi_stat_reg;
  1234. /*
  1235. * Read "Status Register" repeatedly until the LSB is cleared. The
  1236. * EEPROM will signal that the command has been completed by clearing
  1237. * bit 0 of the internal status register. If it's not cleared within
  1238. * 5 milliseconds, then error out.
  1239. */
  1240. for (i = 0; i < IXGBE_EEPROM_MAX_RETRY_SPI; i += 5) {
  1241. ixgbe_shift_out_eeprom_bits(hw, IXGBE_EEPROM_RDSR_OPCODE_SPI,
  1242. IXGBE_EEPROM_OPCODE_BITS);
  1243. spi_stat_reg = (u8)ixgbe_shift_in_eeprom_bits(hw, 8);
  1244. if (!(spi_stat_reg & IXGBE_EEPROM_STATUS_RDY_SPI))
  1245. break;
  1246. udelay(5);
  1247. ixgbe_standby_eeprom(hw);
  1248. }
  1249. /*
  1250. * On some parts, SPI write time could vary from 0-20mSec on 3.3V
  1251. * devices (and only 0-5mSec on 5V devices)
  1252. */
  1253. if (i >= IXGBE_EEPROM_MAX_RETRY_SPI) {
  1254. hw_dbg(hw, "SPI EEPROM Status error\n");
  1255. return IXGBE_ERR_EEPROM;
  1256. }
  1257. return 0;
  1258. }
  1259. /**
  1260. * ixgbe_standby_eeprom - Returns EEPROM to a "standby" state
  1261. * @hw: pointer to hardware structure
  1262. **/
  1263. static void ixgbe_standby_eeprom(struct ixgbe_hw *hw)
  1264. {
  1265. u32 eec;
  1266. eec = IXGBE_READ_REG(hw, IXGBE_EEC(hw));
  1267. /* Toggle CS to flush commands */
  1268. eec |= IXGBE_EEC_CS;
  1269. IXGBE_WRITE_REG(hw, IXGBE_EEC(hw), eec);
  1270. IXGBE_WRITE_FLUSH(hw);
  1271. udelay(1);
  1272. eec &= ~IXGBE_EEC_CS;
  1273. IXGBE_WRITE_REG(hw, IXGBE_EEC(hw), eec);
  1274. IXGBE_WRITE_FLUSH(hw);
  1275. udelay(1);
  1276. }
  1277. /**
  1278. * ixgbe_shift_out_eeprom_bits - Shift data bits out to the EEPROM.
  1279. * @hw: pointer to hardware structure
  1280. * @data: data to send to the EEPROM
  1281. * @count: number of bits to shift out
  1282. **/
  1283. static void ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
  1284. u16 count)
  1285. {
  1286. u32 eec;
  1287. u32 mask;
  1288. u32 i;
  1289. eec = IXGBE_READ_REG(hw, IXGBE_EEC(hw));
  1290. /*
  1291. * Mask is used to shift "count" bits of "data" out to the EEPROM
  1292. * one bit at a time. Determine the starting bit based on count
  1293. */
  1294. mask = 0x01 << (count - 1);
  1295. for (i = 0; i < count; i++) {
  1296. /*
  1297. * A "1" is shifted out to the EEPROM by setting bit "DI" to a
  1298. * "1", and then raising and then lowering the clock (the SK
  1299. * bit controls the clock input to the EEPROM). A "0" is
  1300. * shifted out to the EEPROM by setting "DI" to "0" and then
  1301. * raising and then lowering the clock.
  1302. */
  1303. if (data & mask)
  1304. eec |= IXGBE_EEC_DI;
  1305. else
  1306. eec &= ~IXGBE_EEC_DI;
  1307. IXGBE_WRITE_REG(hw, IXGBE_EEC(hw), eec);
  1308. IXGBE_WRITE_FLUSH(hw);
  1309. udelay(1);
  1310. ixgbe_raise_eeprom_clk(hw, &eec);
  1311. ixgbe_lower_eeprom_clk(hw, &eec);
  1312. /*
  1313. * Shift mask to signify next bit of data to shift in to the
  1314. * EEPROM
  1315. */
  1316. mask = mask >> 1;
  1317. }
  1318. /* We leave the "DI" bit set to "0" when we leave this routine. */
  1319. eec &= ~IXGBE_EEC_DI;
  1320. IXGBE_WRITE_REG(hw, IXGBE_EEC(hw), eec);
  1321. IXGBE_WRITE_FLUSH(hw);
  1322. }
  1323. /**
  1324. * ixgbe_shift_in_eeprom_bits - Shift data bits in from the EEPROM
  1325. * @hw: pointer to hardware structure
  1326. **/
  1327. static u16 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count)
  1328. {
  1329. u32 eec;
  1330. u32 i;
  1331. u16 data = 0;
  1332. /*
  1333. * In order to read a register from the EEPROM, we need to shift
  1334. * 'count' bits in from the EEPROM. Bits are "shifted in" by raising
  1335. * the clock input to the EEPROM (setting the SK bit), and then reading
  1336. * the value of the "DO" bit. During this "shifting in" process the
  1337. * "DI" bit should always be clear.
  1338. */
  1339. eec = IXGBE_READ_REG(hw, IXGBE_EEC(hw));
  1340. eec &= ~(IXGBE_EEC_DO | IXGBE_EEC_DI);
  1341. for (i = 0; i < count; i++) {
  1342. data = data << 1;
  1343. ixgbe_raise_eeprom_clk(hw, &eec);
  1344. eec = IXGBE_READ_REG(hw, IXGBE_EEC(hw));
  1345. eec &= ~(IXGBE_EEC_DI);
  1346. if (eec & IXGBE_EEC_DO)
  1347. data |= 1;
  1348. ixgbe_lower_eeprom_clk(hw, &eec);
  1349. }
  1350. return data;
  1351. }
  1352. /**
  1353. * ixgbe_raise_eeprom_clk - Raises the EEPROM's clock input.
  1354. * @hw: pointer to hardware structure
  1355. * @eec: EEC register's current value
  1356. **/
  1357. static void ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
  1358. {
  1359. /*
  1360. * Raise the clock input to the EEPROM
  1361. * (setting the SK bit), then delay
  1362. */
  1363. *eec = *eec | IXGBE_EEC_SK;
  1364. IXGBE_WRITE_REG(hw, IXGBE_EEC(hw), *eec);
  1365. IXGBE_WRITE_FLUSH(hw);
  1366. udelay(1);
  1367. }
  1368. /**
  1369. * ixgbe_lower_eeprom_clk - Lowers the EEPROM's clock input.
  1370. * @hw: pointer to hardware structure
  1371. * @eecd: EECD's current value
  1372. **/
  1373. static void ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
  1374. {
  1375. /*
  1376. * Lower the clock input to the EEPROM (clearing the SK bit), then
  1377. * delay
  1378. */
  1379. *eec = *eec & ~IXGBE_EEC_SK;
  1380. IXGBE_WRITE_REG(hw, IXGBE_EEC(hw), *eec);
  1381. IXGBE_WRITE_FLUSH(hw);
  1382. udelay(1);
  1383. }
  1384. /**
  1385. * ixgbe_release_eeprom - Release EEPROM, release semaphores
  1386. * @hw: pointer to hardware structure
  1387. **/
  1388. static void ixgbe_release_eeprom(struct ixgbe_hw *hw)
  1389. {
  1390. u32 eec;
  1391. eec = IXGBE_READ_REG(hw, IXGBE_EEC(hw));
  1392. eec |= IXGBE_EEC_CS; /* Pull CS high */
  1393. eec &= ~IXGBE_EEC_SK; /* Lower SCK */
  1394. IXGBE_WRITE_REG(hw, IXGBE_EEC(hw), eec);
  1395. IXGBE_WRITE_FLUSH(hw);
  1396. udelay(1);
  1397. /* Stop requesting EEPROM access */
  1398. eec &= ~IXGBE_EEC_REQ;
  1399. IXGBE_WRITE_REG(hw, IXGBE_EEC(hw), eec);
  1400. hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
  1401. /*
  1402. * Delay before attempt to obtain semaphore again to allow FW
  1403. * access. semaphore_delay is in ms we need us for usleep_range
  1404. */
  1405. usleep_range(hw->eeprom.semaphore_delay * 1000,
  1406. hw->eeprom.semaphore_delay * 2000);
  1407. }
  1408. /**
  1409. * ixgbe_calc_eeprom_checksum_generic - Calculates and returns the checksum
  1410. * @hw: pointer to hardware structure
  1411. **/
  1412. s32 ixgbe_calc_eeprom_checksum_generic(struct ixgbe_hw *hw)
  1413. {
  1414. u16 i;
  1415. u16 j;
  1416. u16 checksum = 0;
  1417. u16 length = 0;
  1418. u16 pointer = 0;
  1419. u16 word = 0;
  1420. /* Include 0x0-0x3F in the checksum */
  1421. for (i = 0; i < IXGBE_EEPROM_CHECKSUM; i++) {
  1422. if (hw->eeprom.ops.read(hw, i, &word)) {
  1423. hw_dbg(hw, "EEPROM read failed\n");
  1424. break;
  1425. }
  1426. checksum += word;
  1427. }
  1428. /* Include all data from pointers except for the fw pointer */
  1429. for (i = IXGBE_PCIE_ANALOG_PTR; i < IXGBE_FW_PTR; i++) {
  1430. if (hw->eeprom.ops.read(hw, i, &pointer)) {
  1431. hw_dbg(hw, "EEPROM read failed\n");
  1432. return IXGBE_ERR_EEPROM;
  1433. }
  1434. /* If the pointer seems invalid */
  1435. if (pointer == 0xFFFF || pointer == 0)
  1436. continue;
  1437. if (hw->eeprom.ops.read(hw, pointer, &length)) {
  1438. hw_dbg(hw, "EEPROM read failed\n");
  1439. return IXGBE_ERR_EEPROM;
  1440. }
  1441. if (length == 0xFFFF || length == 0)
  1442. continue;
  1443. for (j = pointer + 1; j <= pointer + length; j++) {
  1444. if (hw->eeprom.ops.read(hw, j, &word)) {
  1445. hw_dbg(hw, "EEPROM read failed\n");
  1446. return IXGBE_ERR_EEPROM;
  1447. }
  1448. checksum += word;
  1449. }
  1450. }
  1451. checksum = (u16)IXGBE_EEPROM_SUM - checksum;
  1452. return (s32)checksum;
  1453. }
  1454. /**
  1455. * ixgbe_validate_eeprom_checksum_generic - Validate EEPROM checksum
  1456. * @hw: pointer to hardware structure
  1457. * @checksum_val: calculated checksum
  1458. *
  1459. * Performs checksum calculation and validates the EEPROM checksum. If the
  1460. * caller does not need checksum_val, the value can be NULL.
  1461. **/
  1462. s32 ixgbe_validate_eeprom_checksum_generic(struct ixgbe_hw *hw,
  1463. u16 *checksum_val)
  1464. {
  1465. s32 status;
  1466. u16 checksum;
  1467. u16 read_checksum = 0;
  1468. /*
  1469. * Read the first word from the EEPROM. If this times out or fails, do
  1470. * not continue or we could be in for a very long wait while every
  1471. * EEPROM read fails
  1472. */
  1473. status = hw->eeprom.ops.read(hw, 0, &checksum);
  1474. if (status) {
  1475. hw_dbg(hw, "EEPROM read failed\n");
  1476. return status;
  1477. }
  1478. status = hw->eeprom.ops.calc_checksum(hw);
  1479. if (status < 0)
  1480. return status;
  1481. checksum = (u16)(status & 0xffff);
  1482. status = hw->eeprom.ops.read(hw, IXGBE_EEPROM_CHECKSUM, &read_checksum);
  1483. if (status) {
  1484. hw_dbg(hw, "EEPROM read failed\n");
  1485. return status;
  1486. }
  1487. /* Verify read checksum from EEPROM is the same as
  1488. * calculated checksum
  1489. */
  1490. if (read_checksum != checksum)
  1491. status = IXGBE_ERR_EEPROM_CHECKSUM;
  1492. /* If the user cares, return the calculated checksum */
  1493. if (checksum_val)
  1494. *checksum_val = checksum;
  1495. return status;
  1496. }
  1497. /**
  1498. * ixgbe_update_eeprom_checksum_generic - Updates the EEPROM checksum
  1499. * @hw: pointer to hardware structure
  1500. **/
  1501. s32 ixgbe_update_eeprom_checksum_generic(struct ixgbe_hw *hw)
  1502. {
  1503. s32 status;
  1504. u16 checksum;
  1505. /*
  1506. * Read the first word from the EEPROM. If this times out or fails, do
  1507. * not continue or we could be in for a very long wait while every
  1508. * EEPROM read fails
  1509. */
  1510. status = hw->eeprom.ops.read(hw, 0, &checksum);
  1511. if (status) {
  1512. hw_dbg(hw, "EEPROM read failed\n");
  1513. return status;
  1514. }
  1515. status = hw->eeprom.ops.calc_checksum(hw);
  1516. if (status < 0)
  1517. return status;
  1518. checksum = (u16)(status & 0xffff);
  1519. status = hw->eeprom.ops.write(hw, IXGBE_EEPROM_CHECKSUM, checksum);
  1520. return status;
  1521. }
  1522. /**
  1523. * ixgbe_set_rar_generic - Set Rx address register
  1524. * @hw: pointer to hardware structure
  1525. * @index: Receive address register to write
  1526. * @addr: Address to put into receive address register
  1527. * @vmdq: VMDq "set" or "pool" index
  1528. * @enable_addr: set flag that address is active
  1529. *
  1530. * Puts an ethernet address into a receive address register.
  1531. **/
  1532. s32 ixgbe_set_rar_generic(struct ixgbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
  1533. u32 enable_addr)
  1534. {
  1535. u32 rar_low, rar_high;
  1536. u32 rar_entries = hw->mac.num_rar_entries;
  1537. /* Make sure we are using a valid rar index range */
  1538. if (index >= rar_entries) {
  1539. hw_dbg(hw, "RAR index %d is out of range.\n", index);
  1540. return IXGBE_ERR_INVALID_ARGUMENT;
  1541. }
  1542. /* setup VMDq pool selection before this RAR gets enabled */
  1543. hw->mac.ops.set_vmdq(hw, index, vmdq);
  1544. /*
  1545. * HW expects these in little endian so we reverse the byte
  1546. * order from network order (big endian) to little endian
  1547. */
  1548. rar_low = ((u32)addr[0] |
  1549. ((u32)addr[1] << 8) |
  1550. ((u32)addr[2] << 16) |
  1551. ((u32)addr[3] << 24));
  1552. /*
  1553. * Some parts put the VMDq setting in the extra RAH bits,
  1554. * so save everything except the lower 16 bits that hold part
  1555. * of the address and the address valid bit.
  1556. */
  1557. rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
  1558. rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
  1559. rar_high |= ((u32)addr[4] | ((u32)addr[5] << 8));
  1560. if (enable_addr != 0)
  1561. rar_high |= IXGBE_RAH_AV;
  1562. IXGBE_WRITE_REG(hw, IXGBE_RAL(index), rar_low);
  1563. IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
  1564. return 0;
  1565. }
  1566. /**
  1567. * ixgbe_clear_rar_generic - Remove Rx address register
  1568. * @hw: pointer to hardware structure
  1569. * @index: Receive address register to write
  1570. *
  1571. * Clears an ethernet address from a receive address register.
  1572. **/
  1573. s32 ixgbe_clear_rar_generic(struct ixgbe_hw *hw, u32 index)
  1574. {
  1575. u32 rar_high;
  1576. u32 rar_entries = hw->mac.num_rar_entries;
  1577. /* Make sure we are using a valid rar index range */
  1578. if (index >= rar_entries) {
  1579. hw_dbg(hw, "RAR index %d is out of range.\n", index);
  1580. return IXGBE_ERR_INVALID_ARGUMENT;
  1581. }
  1582. /*
  1583. * Some parts put the VMDq setting in the extra RAH bits,
  1584. * so save everything except the lower 16 bits that hold part
  1585. * of the address and the address valid bit.
  1586. */
  1587. rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
  1588. rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
  1589. IXGBE_WRITE_REG(hw, IXGBE_RAL(index), 0);
  1590. IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
  1591. /* clear VMDq pool/queue selection for this RAR */
  1592. hw->mac.ops.clear_vmdq(hw, index, IXGBE_CLEAR_VMDQ_ALL);
  1593. return 0;
  1594. }
  1595. /**
  1596. * ixgbe_init_rx_addrs_generic - Initializes receive address filters.
  1597. * @hw: pointer to hardware structure
  1598. *
  1599. * Places the MAC address in receive address register 0 and clears the rest
  1600. * of the receive address registers. Clears the multicast table. Assumes
  1601. * the receiver is in reset when the routine is called.
  1602. **/
  1603. s32 ixgbe_init_rx_addrs_generic(struct ixgbe_hw *hw)
  1604. {
  1605. u32 i;
  1606. u32 rar_entries = hw->mac.num_rar_entries;
  1607. /*
  1608. * If the current mac address is valid, assume it is a software override
  1609. * to the permanent address.
  1610. * Otherwise, use the permanent address from the eeprom.
  1611. */
  1612. if (!is_valid_ether_addr(hw->mac.addr)) {
  1613. /* Get the MAC address from the RAR0 for later reference */
  1614. hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
  1615. hw_dbg(hw, " Keeping Current RAR0 Addr =%pM\n", hw->mac.addr);
  1616. } else {
  1617. /* Setup the receive address. */
  1618. hw_dbg(hw, "Overriding MAC Address in RAR[0]\n");
  1619. hw_dbg(hw, " New MAC Addr =%pM\n", hw->mac.addr);
  1620. hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
  1621. }
  1622. /* clear VMDq pool/queue selection for RAR 0 */
  1623. hw->mac.ops.clear_vmdq(hw, 0, IXGBE_CLEAR_VMDQ_ALL);
  1624. hw->addr_ctrl.overflow_promisc = 0;
  1625. hw->addr_ctrl.rar_used_count = 1;
  1626. /* Zero out the other receive addresses. */
  1627. hw_dbg(hw, "Clearing RAR[1-%d]\n", rar_entries - 1);
  1628. for (i = 1; i < rar_entries; i++) {
  1629. IXGBE_WRITE_REG(hw, IXGBE_RAL(i), 0);
  1630. IXGBE_WRITE_REG(hw, IXGBE_RAH(i), 0);
  1631. }
  1632. /* Clear the MTA */
  1633. hw->addr_ctrl.mta_in_use = 0;
  1634. IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
  1635. hw_dbg(hw, " Clearing MTA\n");
  1636. for (i = 0; i < hw->mac.mcft_size; i++)
  1637. IXGBE_WRITE_REG(hw, IXGBE_MTA(i), 0);
  1638. if (hw->mac.ops.init_uta_tables)
  1639. hw->mac.ops.init_uta_tables(hw);
  1640. return 0;
  1641. }
  1642. /**
  1643. * ixgbe_mta_vector - Determines bit-vector in multicast table to set
  1644. * @hw: pointer to hardware structure
  1645. * @mc_addr: the multicast address
  1646. *
  1647. * Extracts the 12 bits, from a multicast address, to determine which
  1648. * bit-vector to set in the multicast table. The hardware uses 12 bits, from
  1649. * incoming rx multicast addresses, to determine the bit-vector to check in
  1650. * the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
  1651. * by the MO field of the MCSTCTRL. The MO field is set during initialization
  1652. * to mc_filter_type.
  1653. **/
  1654. static s32 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr)
  1655. {
  1656. u32 vector = 0;
  1657. switch (hw->mac.mc_filter_type) {
  1658. case 0: /* use bits [47:36] of the address */
  1659. vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
  1660. break;
  1661. case 1: /* use bits [46:35] of the address */
  1662. vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
  1663. break;
  1664. case 2: /* use bits [45:34] of the address */
  1665. vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
  1666. break;
  1667. case 3: /* use bits [43:32] of the address */
  1668. vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
  1669. break;
  1670. default: /* Invalid mc_filter_type */
  1671. hw_dbg(hw, "MC filter type param set incorrectly\n");
  1672. break;
  1673. }
  1674. /* vector can only be 12-bits or boundary will be exceeded */
  1675. vector &= 0xFFF;
  1676. return vector;
  1677. }
  1678. /**
  1679. * ixgbe_set_mta - Set bit-vector in multicast table
  1680. * @hw: pointer to hardware structure
  1681. * @hash_value: Multicast address hash value
  1682. *
  1683. * Sets the bit-vector in the multicast table.
  1684. **/
  1685. static void ixgbe_set_mta(struct ixgbe_hw *hw, u8 *mc_addr)
  1686. {
  1687. u32 vector;
  1688. u32 vector_bit;
  1689. u32 vector_reg;
  1690. hw->addr_ctrl.mta_in_use++;
  1691. vector = ixgbe_mta_vector(hw, mc_addr);
  1692. hw_dbg(hw, " bit-vector = 0x%03X\n", vector);
  1693. /*
  1694. * The MTA is a register array of 128 32-bit registers. It is treated
  1695. * like an array of 4096 bits. We want to set bit
  1696. * BitArray[vector_value]. So we figure out what register the bit is
  1697. * in, read it, OR in the new bit, then write back the new value. The
  1698. * register is determined by the upper 7 bits of the vector value and
  1699. * the bit within that register are determined by the lower 5 bits of
  1700. * the value.
  1701. */
  1702. vector_reg = (vector >> 5) & 0x7F;
  1703. vector_bit = vector & 0x1F;
  1704. hw->mac.mta_shadow[vector_reg] |= (1 << vector_bit);
  1705. }
  1706. /**
  1707. * ixgbe_update_mc_addr_list_generic - Updates MAC list of multicast addresses
  1708. * @hw: pointer to hardware structure
  1709. * @netdev: pointer to net device structure
  1710. *
  1711. * The given list replaces any existing list. Clears the MC addrs from receive
  1712. * address registers and the multicast table. Uses unused receive address
  1713. * registers for the first multicast addresses, and hashes the rest into the
  1714. * multicast table.
  1715. **/
  1716. s32 ixgbe_update_mc_addr_list_generic(struct ixgbe_hw *hw,
  1717. struct net_device *netdev)
  1718. {
  1719. struct netdev_hw_addr *ha;
  1720. u32 i;
  1721. /*
  1722. * Set the new number of MC addresses that we are being requested to
  1723. * use.
  1724. */
  1725. hw->addr_ctrl.num_mc_addrs = netdev_mc_count(netdev);
  1726. hw->addr_ctrl.mta_in_use = 0;
  1727. /* Clear mta_shadow */
  1728. hw_dbg(hw, " Clearing MTA\n");
  1729. memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
  1730. /* Update mta shadow */
  1731. netdev_for_each_mc_addr(ha, netdev) {
  1732. hw_dbg(hw, " Adding the multicast addresses:\n");
  1733. ixgbe_set_mta(hw, ha->addr);
  1734. }
  1735. /* Enable mta */
  1736. for (i = 0; i < hw->mac.mcft_size; i++)
  1737. IXGBE_WRITE_REG_ARRAY(hw, IXGBE_MTA(0), i,
  1738. hw->mac.mta_shadow[i]);
  1739. if (hw->addr_ctrl.mta_in_use > 0)
  1740. IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL,
  1741. IXGBE_MCSTCTRL_MFE | hw->mac.mc_filter_type);
  1742. hw_dbg(hw, "ixgbe_update_mc_addr_list_generic Complete\n");
  1743. return 0;
  1744. }
  1745. /**
  1746. * ixgbe_enable_mc_generic - Enable multicast address in RAR
  1747. * @hw: pointer to hardware structure
  1748. *
  1749. * Enables multicast address in RAR and the use of the multicast hash table.
  1750. **/
  1751. s32 ixgbe_enable_mc_generic(struct ixgbe_hw *hw)
  1752. {
  1753. struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
  1754. if (a->mta_in_use > 0)
  1755. IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, IXGBE_MCSTCTRL_MFE |
  1756. hw->mac.mc_filter_type);
  1757. return 0;
  1758. }
  1759. /**
  1760. * ixgbe_disable_mc_generic - Disable multicast address in RAR
  1761. * @hw: pointer to hardware structure
  1762. *
  1763. * Disables multicast address in RAR and the use of the multicast hash table.
  1764. **/
  1765. s32 ixgbe_disable_mc_generic(struct ixgbe_hw *hw)
  1766. {
  1767. struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
  1768. if (a->mta_in_use > 0)
  1769. IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
  1770. return 0;
  1771. }
  1772. /**
  1773. * ixgbe_fc_enable_generic - Enable flow control
  1774. * @hw: pointer to hardware structure
  1775. *
  1776. * Enable flow control according to the current settings.
  1777. **/
  1778. s32 ixgbe_fc_enable_generic(struct ixgbe_hw *hw)
  1779. {
  1780. u32 mflcn_reg, fccfg_reg;
  1781. u32 reg;
  1782. u32 fcrtl, fcrth;
  1783. int i;
  1784. /* Validate the water mark configuration. */
  1785. if (!hw->fc.pause_time)
  1786. return IXGBE_ERR_INVALID_LINK_SETTINGS;
  1787. /* Low water mark of zero causes XOFF floods */
  1788. for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
  1789. if ((hw->fc.current_mode & ixgbe_fc_tx_pause) &&
  1790. hw->fc.high_water[i]) {
  1791. if (!hw->fc.low_water[i] ||
  1792. hw->fc.low_water[i] >= hw->fc.high_water[i]) {
  1793. hw_dbg(hw, "Invalid water mark configuration\n");
  1794. return IXGBE_ERR_INVALID_LINK_SETTINGS;
  1795. }
  1796. }
  1797. }
  1798. /* Negotiate the fc mode to use */
  1799. ixgbe_fc_autoneg(hw);
  1800. /* Disable any previous flow control settings */
  1801. mflcn_reg = IXGBE_READ_REG(hw, IXGBE_MFLCN);
  1802. mflcn_reg &= ~(IXGBE_MFLCN_RPFCE_MASK | IXGBE_MFLCN_RFCE);
  1803. fccfg_reg = IXGBE_READ_REG(hw, IXGBE_FCCFG);
  1804. fccfg_reg &= ~(IXGBE_FCCFG_TFCE_802_3X | IXGBE_FCCFG_TFCE_PRIORITY);
  1805. /*
  1806. * The possible values of fc.current_mode are:
  1807. * 0: Flow control is completely disabled
  1808. * 1: Rx flow control is enabled (we can receive pause frames,
  1809. * but not send pause frames).
  1810. * 2: Tx flow control is enabled (we can send pause frames but
  1811. * we do not support receiving pause frames).
  1812. * 3: Both Rx and Tx flow control (symmetric) are enabled.
  1813. * other: Invalid.
  1814. */
  1815. switch (hw->fc.current_mode) {
  1816. case ixgbe_fc_none:
  1817. /*
  1818. * Flow control is disabled by software override or autoneg.
  1819. * The code below will actually disable it in the HW.
  1820. */
  1821. break;
  1822. case ixgbe_fc_rx_pause:
  1823. /*
  1824. * Rx Flow control is enabled and Tx Flow control is
  1825. * disabled by software override. Since there really
  1826. * isn't a way to advertise that we are capable of RX
  1827. * Pause ONLY, we will advertise that we support both
  1828. * symmetric and asymmetric Rx PAUSE. Later, we will
  1829. * disable the adapter's ability to send PAUSE frames.
  1830. */
  1831. mflcn_reg |= IXGBE_MFLCN_RFCE;
  1832. break;
  1833. case ixgbe_fc_tx_pause:
  1834. /*
  1835. * Tx Flow control is enabled, and Rx Flow control is
  1836. * disabled by software override.
  1837. */
  1838. fccfg_reg |= IXGBE_FCCFG_TFCE_802_3X;
  1839. break;
  1840. case ixgbe_fc_full:
  1841. /* Flow control (both Rx and Tx) is enabled by SW override. */
  1842. mflcn_reg |= IXGBE_MFLCN_RFCE;
  1843. fccfg_reg |= IXGBE_FCCFG_TFCE_802_3X;
  1844. break;
  1845. default:
  1846. hw_dbg(hw, "Flow control param set incorrectly\n");
  1847. return IXGBE_ERR_CONFIG;
  1848. }
  1849. /* Set 802.3x based flow control settings. */
  1850. mflcn_reg |= IXGBE_MFLCN_DPF;
  1851. IXGBE_WRITE_REG(hw, IXGBE_MFLCN, mflcn_reg);
  1852. IXGBE_WRITE_REG(hw, IXGBE_FCCFG, fccfg_reg);
  1853. /* Set up and enable Rx high/low water mark thresholds, enable XON. */
  1854. for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
  1855. if ((hw->fc.current_mode & ixgbe_fc_tx_pause) &&
  1856. hw->fc.high_water[i]) {
  1857. fcrtl = (hw->fc.low_water[i] << 10) | IXGBE_FCRTL_XONE;
  1858. IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(i), fcrtl);
  1859. fcrth = (hw->fc.high_water[i] << 10) | IXGBE_FCRTH_FCEN;
  1860. } else {
  1861. IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(i), 0);
  1862. /*
  1863. * In order to prevent Tx hangs when the internal Tx
  1864. * switch is enabled we must set the high water mark
  1865. * to the Rx packet buffer size - 24KB. This allows
  1866. * the Tx switch to function even under heavy Rx
  1867. * workloads.
  1868. */
  1869. fcrth = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(i)) - 24576;
  1870. }
  1871. IXGBE_WRITE_REG(hw, IXGBE_FCRTH_82599(i), fcrth);
  1872. }
  1873. /* Configure pause time (2 TCs per register) */
  1874. reg = hw->fc.pause_time * 0x00010001;
  1875. for (i = 0; i < (MAX_TRAFFIC_CLASS / 2); i++)
  1876. IXGBE_WRITE_REG(hw, IXGBE_FCTTV(i), reg);
  1877. IXGBE_WRITE_REG(hw, IXGBE_FCRTV, hw->fc.pause_time / 2);
  1878. return 0;
  1879. }
  1880. /**
  1881. * ixgbe_negotiate_fc - Negotiate flow control
  1882. * @hw: pointer to hardware structure
  1883. * @adv_reg: flow control advertised settings
  1884. * @lp_reg: link partner's flow control settings
  1885. * @adv_sym: symmetric pause bit in advertisement
  1886. * @adv_asm: asymmetric pause bit in advertisement
  1887. * @lp_sym: symmetric pause bit in link partner advertisement
  1888. * @lp_asm: asymmetric pause bit in link partner advertisement
  1889. *
  1890. * Find the intersection between advertised settings and link partner's
  1891. * advertised settings
  1892. **/
  1893. static s32 ixgbe_negotiate_fc(struct ixgbe_hw *hw, u32 adv_reg, u32 lp_reg,
  1894. u32 adv_sym, u32 adv_asm, u32 lp_sym, u32 lp_asm)
  1895. {
  1896. if ((!(adv_reg)) || (!(lp_reg)))
  1897. return IXGBE_ERR_FC_NOT_NEGOTIATED;
  1898. if ((adv_reg & adv_sym) && (lp_reg & lp_sym)) {
  1899. /*
  1900. * Now we need to check if the user selected Rx ONLY
  1901. * of pause frames. In this case, we had to advertise
  1902. * FULL flow control because we could not advertise RX
  1903. * ONLY. Hence, we must now check to see if we need to
  1904. * turn OFF the TRANSMISSION of PAUSE frames.
  1905. */
  1906. if (hw->fc.requested_mode == ixgbe_fc_full) {
  1907. hw->fc.current_mode = ixgbe_fc_full;
  1908. hw_dbg(hw, "Flow Control = FULL.\n");
  1909. } else {
  1910. hw->fc.current_mode = ixgbe_fc_rx_pause;
  1911. hw_dbg(hw, "Flow Control=RX PAUSE frames only\n");
  1912. }
  1913. } else if (!(adv_reg & adv_sym) && (adv_reg & adv_asm) &&
  1914. (lp_reg & lp_sym) && (lp_reg & lp_asm)) {
  1915. hw->fc.current_mode = ixgbe_fc_tx_pause;
  1916. hw_dbg(hw, "Flow Control = TX PAUSE frames only.\n");
  1917. } else if ((adv_reg & adv_sym) && (adv_reg & adv_asm) &&
  1918. !(lp_reg & lp_sym) && (lp_reg & lp_asm)) {
  1919. hw->fc.current_mode = ixgbe_fc_rx_pause;
  1920. hw_dbg(hw, "Flow Control = RX PAUSE frames only.\n");
  1921. } else {
  1922. hw->fc.current_mode = ixgbe_fc_none;
  1923. hw_dbg(hw, "Flow Control = NONE.\n");
  1924. }
  1925. return 0;
  1926. }
  1927. /**
  1928. * ixgbe_fc_autoneg_fiber - Enable flow control on 1 gig fiber
  1929. * @hw: pointer to hardware structure
  1930. *
  1931. * Enable flow control according on 1 gig fiber.
  1932. **/
  1933. static s32 ixgbe_fc_autoneg_fiber(struct ixgbe_hw *hw)
  1934. {
  1935. u32 pcs_anadv_reg, pcs_lpab_reg, linkstat;
  1936. s32 ret_val;
  1937. /*
  1938. * On multispeed fiber at 1g, bail out if
  1939. * - link is up but AN did not complete, or if
  1940. * - link is up and AN completed but timed out
  1941. */
  1942. linkstat = IXGBE_READ_REG(hw, IXGBE_PCS1GLSTA);
  1943. if ((!!(linkstat & IXGBE_PCS1GLSTA_AN_COMPLETE) == 0) ||
  1944. (!!(linkstat & IXGBE_PCS1GLSTA_AN_TIMED_OUT) == 1))
  1945. return IXGBE_ERR_FC_NOT_NEGOTIATED;
  1946. pcs_anadv_reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANA);
  1947. pcs_lpab_reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANLP);
  1948. ret_val = ixgbe_negotiate_fc(hw, pcs_anadv_reg,
  1949. pcs_lpab_reg, IXGBE_PCS1GANA_SYM_PAUSE,
  1950. IXGBE_PCS1GANA_ASM_PAUSE,
  1951. IXGBE_PCS1GANA_SYM_PAUSE,
  1952. IXGBE_PCS1GANA_ASM_PAUSE);
  1953. return ret_val;
  1954. }
  1955. /**
  1956. * ixgbe_fc_autoneg_backplane - Enable flow control IEEE clause 37
  1957. * @hw: pointer to hardware structure
  1958. *
  1959. * Enable flow control according to IEEE clause 37.
  1960. **/
  1961. static s32 ixgbe_fc_autoneg_backplane(struct ixgbe_hw *hw)
  1962. {
  1963. u32 links2, anlp1_reg, autoc_reg, links;
  1964. s32 ret_val;
  1965. /*
  1966. * On backplane, bail out if
  1967. * - backplane autoneg was not completed, or if
  1968. * - we are 82599 and link partner is not AN enabled
  1969. */
  1970. links = IXGBE_READ_REG(hw, IXGBE_LINKS);
  1971. if ((links & IXGBE_LINKS_KX_AN_COMP) == 0)
  1972. return IXGBE_ERR_FC_NOT_NEGOTIATED;
  1973. if (hw->mac.type == ixgbe_mac_82599EB) {
  1974. links2 = IXGBE_READ_REG(hw, IXGBE_LINKS2);
  1975. if ((links2 & IXGBE_LINKS2_AN_SUPPORTED) == 0)
  1976. return IXGBE_ERR_FC_NOT_NEGOTIATED;
  1977. }
  1978. /*
  1979. * Read the 10g AN autoc and LP ability registers and resolve
  1980. * local flow control settings accordingly
  1981. */
  1982. autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
  1983. anlp1_reg = IXGBE_READ_REG(hw, IXGBE_ANLP1);
  1984. ret_val = ixgbe_negotiate_fc(hw, autoc_reg,
  1985. anlp1_reg, IXGBE_AUTOC_SYM_PAUSE, IXGBE_AUTOC_ASM_PAUSE,
  1986. IXGBE_ANLP1_SYM_PAUSE, IXGBE_ANLP1_ASM_PAUSE);
  1987. return ret_val;
  1988. }
  1989. /**
  1990. * ixgbe_fc_autoneg_copper - Enable flow control IEEE clause 37
  1991. * @hw: pointer to hardware structure
  1992. *
  1993. * Enable flow control according to IEEE clause 37.
  1994. **/
  1995. static s32 ixgbe_fc_autoneg_copper(struct ixgbe_hw *hw)
  1996. {
  1997. u16 technology_ability_reg = 0;
  1998. u16 lp_technology_ability_reg = 0;
  1999. hw->phy.ops.read_reg(hw, MDIO_AN_ADVERTISE,
  2000. MDIO_MMD_AN,
  2001. &technology_ability_reg);
  2002. hw->phy.ops.read_reg(hw, MDIO_AN_LPA,
  2003. MDIO_MMD_AN,
  2004. &lp_technology_ability_reg);
  2005. return ixgbe_negotiate_fc(hw, (u32)technology_ability_reg,
  2006. (u32)lp_technology_ability_reg,
  2007. IXGBE_TAF_SYM_PAUSE, IXGBE_TAF_ASM_PAUSE,
  2008. IXGBE_TAF_SYM_PAUSE, IXGBE_TAF_ASM_PAUSE);
  2009. }
  2010. /**
  2011. * ixgbe_fc_autoneg - Configure flow control
  2012. * @hw: pointer to hardware structure
  2013. *
  2014. * Compares our advertised flow control capabilities to those advertised by
  2015. * our link partner, and determines the proper flow control mode to use.
  2016. **/
  2017. void ixgbe_fc_autoneg(struct ixgbe_hw *hw)
  2018. {
  2019. s32 ret_val = IXGBE_ERR_FC_NOT_NEGOTIATED;
  2020. ixgbe_link_speed speed;
  2021. bool link_up;
  2022. /*
  2023. * AN should have completed when the cable was plugged in.
  2024. * Look for reasons to bail out. Bail out if:
  2025. * - FC autoneg is disabled, or if
  2026. * - link is not up.
  2027. *
  2028. * Since we're being called from an LSC, link is already known to be up.
  2029. * So use link_up_wait_to_complete=false.
  2030. */
  2031. if (hw->fc.disable_fc_autoneg)
  2032. goto out;
  2033. hw->mac.ops.check_link(hw, &speed, &link_up, false);
  2034. if (!link_up)
  2035. goto out;
  2036. switch (hw->phy.media_type) {
  2037. /* Autoneg flow control on fiber adapters */
  2038. case ixgbe_media_type_fiber:
  2039. if (speed == IXGBE_LINK_SPEED_1GB_FULL)
  2040. ret_val = ixgbe_fc_autoneg_fiber(hw);
  2041. break;
  2042. /* Autoneg flow control on backplane adapters */
  2043. case ixgbe_media_type_backplane:
  2044. ret_val = ixgbe_fc_autoneg_backplane(hw);
  2045. break;
  2046. /* Autoneg flow control on copper adapters */
  2047. case ixgbe_media_type_copper:
  2048. if (ixgbe_device_supports_autoneg_fc(hw))
  2049. ret_val = ixgbe_fc_autoneg_copper(hw);
  2050. break;
  2051. default:
  2052. break;
  2053. }
  2054. out:
  2055. if (ret_val == 0) {
  2056. hw->fc.fc_was_autonegged = true;
  2057. } else {
  2058. hw->fc.fc_was_autonegged = false;
  2059. hw->fc.current_mode = hw->fc.requested_mode;
  2060. }
  2061. }
  2062. /**
  2063. * ixgbe_pcie_timeout_poll - Return number of times to poll for completion
  2064. * @hw: pointer to hardware structure
  2065. *
  2066. * System-wide timeout range is encoded in PCIe Device Control2 register.
  2067. *
  2068. * Add 10% to specified maximum and return the number of times to poll for
  2069. * completion timeout, in units of 100 microsec. Never return less than
  2070. * 800 = 80 millisec.
  2071. **/
  2072. static u32 ixgbe_pcie_timeout_poll(struct ixgbe_hw *hw)
  2073. {
  2074. s16 devctl2;
  2075. u32 pollcnt;
  2076. devctl2 = ixgbe_read_pci_cfg_word(hw, IXGBE_PCI_DEVICE_CONTROL2);
  2077. devctl2 &= IXGBE_PCIDEVCTRL2_TIMEO_MASK;
  2078. switch (devctl2) {
  2079. case IXGBE_PCIDEVCTRL2_65_130ms:
  2080. pollcnt = 1300; /* 130 millisec */
  2081. break;
  2082. case IXGBE_PCIDEVCTRL2_260_520ms:
  2083. pollcnt = 5200; /* 520 millisec */
  2084. break;
  2085. case IXGBE_PCIDEVCTRL2_1_2s:
  2086. pollcnt = 20000; /* 2 sec */
  2087. break;
  2088. case IXGBE_PCIDEVCTRL2_4_8s:
  2089. pollcnt = 80000; /* 8 sec */
  2090. break;
  2091. case IXGBE_PCIDEVCTRL2_17_34s:
  2092. pollcnt = 34000; /* 34 sec */
  2093. break;
  2094. case IXGBE_PCIDEVCTRL2_50_100us: /* 100 microsecs */
  2095. case IXGBE_PCIDEVCTRL2_1_2ms: /* 2 millisecs */
  2096. case IXGBE_PCIDEVCTRL2_16_32ms: /* 32 millisec */
  2097. case IXGBE_PCIDEVCTRL2_16_32ms_def: /* 32 millisec default */
  2098. default:
  2099. pollcnt = 800; /* 80 millisec minimum */
  2100. break;
  2101. }
  2102. /* add 10% to spec maximum */
  2103. return (pollcnt * 11) / 10;
  2104. }
  2105. /**
  2106. * ixgbe_disable_pcie_master - Disable PCI-express master access
  2107. * @hw: pointer to hardware structure
  2108. *
  2109. * Disables PCI-Express master access and verifies there are no pending
  2110. * requests. IXGBE_ERR_MASTER_REQUESTS_PENDING is returned if master disable
  2111. * bit hasn't caused the master requests to be disabled, else 0
  2112. * is returned signifying master requests disabled.
  2113. **/
  2114. static s32 ixgbe_disable_pcie_master(struct ixgbe_hw *hw)
  2115. {
  2116. u32 i, poll;
  2117. u16 value;
  2118. /* Always set this bit to ensure any future transactions are blocked */
  2119. IXGBE_WRITE_REG(hw, IXGBE_CTRL, IXGBE_CTRL_GIO_DIS);
  2120. /* Poll for bit to read as set */
  2121. for (i = 0; i < IXGBE_PCI_MASTER_DISABLE_TIMEOUT; i++) {
  2122. if (IXGBE_READ_REG(hw, IXGBE_CTRL) & IXGBE_CTRL_GIO_DIS)
  2123. break;
  2124. usleep_range(100, 120);
  2125. }
  2126. if (i >= IXGBE_PCI_MASTER_DISABLE_TIMEOUT) {
  2127. hw_dbg(hw, "GIO disable did not set - requesting resets\n");
  2128. goto gio_disable_fail;
  2129. }
  2130. /* Exit if master requests are blocked */
  2131. if (!(IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_GIO) ||
  2132. ixgbe_removed(hw->hw_addr))
  2133. return 0;
  2134. /* Poll for master request bit to clear */
  2135. for (i = 0; i < IXGBE_PCI_MASTER_DISABLE_TIMEOUT; i++) {
  2136. udelay(100);
  2137. if (!(IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_GIO))
  2138. return 0;
  2139. }
  2140. /*
  2141. * Two consecutive resets are required via CTRL.RST per datasheet
  2142. * 5.2.5.3.2 Master Disable. We set a flag to inform the reset routine
  2143. * of this need. The first reset prevents new master requests from
  2144. * being issued by our device. We then must wait 1usec or more for any
  2145. * remaining completions from the PCIe bus to trickle in, and then reset
  2146. * again to clear out any effects they may have had on our device.
  2147. */
  2148. hw_dbg(hw, "GIO Master Disable bit didn't clear - requesting resets\n");
  2149. gio_disable_fail:
  2150. hw->mac.flags |= IXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
  2151. if (hw->mac.type >= ixgbe_mac_X550)
  2152. return 0;
  2153. /*
  2154. * Before proceeding, make sure that the PCIe block does not have
  2155. * transactions pending.
  2156. */
  2157. poll = ixgbe_pcie_timeout_poll(hw);
  2158. for (i = 0; i < poll; i++) {
  2159. udelay(100);
  2160. value = ixgbe_read_pci_cfg_word(hw, IXGBE_PCI_DEVICE_STATUS);
  2161. if (ixgbe_removed(hw->hw_addr))
  2162. return 0;
  2163. if (!(value & IXGBE_PCI_DEVICE_STATUS_TRANSACTION_PENDING))
  2164. return 0;
  2165. }
  2166. hw_dbg(hw, "PCIe transaction pending bit also did not clear.\n");
  2167. return IXGBE_ERR_MASTER_REQUESTS_PENDING;
  2168. }
  2169. /**
  2170. * ixgbe_acquire_swfw_sync - Acquire SWFW semaphore
  2171. * @hw: pointer to hardware structure
  2172. * @mask: Mask to specify which semaphore to acquire
  2173. *
  2174. * Acquires the SWFW semaphore through the GSSR register for the specified
  2175. * function (CSR, PHY0, PHY1, EEPROM, Flash)
  2176. **/
  2177. s32 ixgbe_acquire_swfw_sync(struct ixgbe_hw *hw, u32 mask)
  2178. {
  2179. u32 gssr = 0;
  2180. u32 swmask = mask;
  2181. u32 fwmask = mask << 5;
  2182. u32 timeout = 200;
  2183. u32 i;
  2184. for (i = 0; i < timeout; i++) {
  2185. /*
  2186. * SW NVM semaphore bit is used for access to all
  2187. * SW_FW_SYNC bits (not just NVM)
  2188. */
  2189. if (ixgbe_get_eeprom_semaphore(hw))
  2190. return IXGBE_ERR_SWFW_SYNC;
  2191. gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
  2192. if (!(gssr & (fwmask | swmask))) {
  2193. gssr |= swmask;
  2194. IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
  2195. ixgbe_release_eeprom_semaphore(hw);
  2196. return 0;
  2197. } else {
  2198. /* Resource is currently in use by FW or SW */
  2199. ixgbe_release_eeprom_semaphore(hw);
  2200. usleep_range(5000, 10000);
  2201. }
  2202. }
  2203. /* If time expired clear the bits holding the lock and retry */
  2204. if (gssr & (fwmask | swmask))
  2205. ixgbe_release_swfw_sync(hw, gssr & (fwmask | swmask));
  2206. usleep_range(5000, 10000);
  2207. return IXGBE_ERR_SWFW_SYNC;
  2208. }
  2209. /**
  2210. * ixgbe_release_swfw_sync - Release SWFW semaphore
  2211. * @hw: pointer to hardware structure
  2212. * @mask: Mask to specify which semaphore to release
  2213. *
  2214. * Releases the SWFW semaphore through the GSSR register for the specified
  2215. * function (CSR, PHY0, PHY1, EEPROM, Flash)
  2216. **/
  2217. void ixgbe_release_swfw_sync(struct ixgbe_hw *hw, u32 mask)
  2218. {
  2219. u32 gssr;
  2220. u32 swmask = mask;
  2221. ixgbe_get_eeprom_semaphore(hw);
  2222. gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
  2223. gssr &= ~swmask;
  2224. IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
  2225. ixgbe_release_eeprom_semaphore(hw);
  2226. }
  2227. /**
  2228. * prot_autoc_read_generic - Hides MAC differences needed for AUTOC read
  2229. * @hw: pointer to hardware structure
  2230. * @reg_val: Value we read from AUTOC
  2231. * @locked: bool to indicate whether the SW/FW lock should be taken. Never
  2232. * true in this the generic case.
  2233. *
  2234. * The default case requires no protection so just to the register read.
  2235. **/
  2236. s32 prot_autoc_read_generic(struct ixgbe_hw *hw, bool *locked, u32 *reg_val)
  2237. {
  2238. *locked = false;
  2239. *reg_val = IXGBE_READ_REG(hw, IXGBE_AUTOC);
  2240. return 0;
  2241. }
  2242. /**
  2243. * prot_autoc_write_generic - Hides MAC differences needed for AUTOC write
  2244. * @hw: pointer to hardware structure
  2245. * @reg_val: value to write to AUTOC
  2246. * @locked: bool to indicate whether the SW/FW lock was already taken by
  2247. * previous read.
  2248. **/
  2249. s32 prot_autoc_write_generic(struct ixgbe_hw *hw, u32 reg_val, bool locked)
  2250. {
  2251. IXGBE_WRITE_REG(hw, IXGBE_AUTOC, reg_val);
  2252. return 0;
  2253. }
  2254. /**
  2255. * ixgbe_disable_rx_buff_generic - Stops the receive data path
  2256. * @hw: pointer to hardware structure
  2257. *
  2258. * Stops the receive data path and waits for the HW to internally
  2259. * empty the Rx security block.
  2260. **/
  2261. s32 ixgbe_disable_rx_buff_generic(struct ixgbe_hw *hw)
  2262. {
  2263. #define IXGBE_MAX_SECRX_POLL 40
  2264. int i;
  2265. int secrxreg;
  2266. secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
  2267. secrxreg |= IXGBE_SECRXCTRL_RX_DIS;
  2268. IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, secrxreg);
  2269. for (i = 0; i < IXGBE_MAX_SECRX_POLL; i++) {
  2270. secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT);
  2271. if (secrxreg & IXGBE_SECRXSTAT_SECRX_RDY)
  2272. break;
  2273. else
  2274. /* Use interrupt-safe sleep just in case */
  2275. udelay(1000);
  2276. }
  2277. /* For informational purposes only */
  2278. if (i >= IXGBE_MAX_SECRX_POLL)
  2279. hw_dbg(hw, "Rx unit being enabled before security path fully disabled. Continuing with init.\n");
  2280. return 0;
  2281. }
  2282. /**
  2283. * ixgbe_enable_rx_buff - Enables the receive data path
  2284. * @hw: pointer to hardware structure
  2285. *
  2286. * Enables the receive data path
  2287. **/
  2288. s32 ixgbe_enable_rx_buff_generic(struct ixgbe_hw *hw)
  2289. {
  2290. int secrxreg;
  2291. secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
  2292. secrxreg &= ~IXGBE_SECRXCTRL_RX_DIS;
  2293. IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, secrxreg);
  2294. IXGBE_WRITE_FLUSH(hw);
  2295. return 0;
  2296. }
  2297. /**
  2298. * ixgbe_enable_rx_dma_generic - Enable the Rx DMA unit
  2299. * @hw: pointer to hardware structure
  2300. * @regval: register value to write to RXCTRL
  2301. *
  2302. * Enables the Rx DMA unit
  2303. **/
  2304. s32 ixgbe_enable_rx_dma_generic(struct ixgbe_hw *hw, u32 regval)
  2305. {
  2306. if (regval & IXGBE_RXCTRL_RXEN)
  2307. hw->mac.ops.enable_rx(hw);
  2308. else
  2309. hw->mac.ops.disable_rx(hw);
  2310. return 0;
  2311. }
  2312. /**
  2313. * ixgbe_blink_led_start_generic - Blink LED based on index.
  2314. * @hw: pointer to hardware structure
  2315. * @index: led number to blink
  2316. **/
  2317. s32 ixgbe_blink_led_start_generic(struct ixgbe_hw *hw, u32 index)
  2318. {
  2319. ixgbe_link_speed speed = 0;
  2320. bool link_up = false;
  2321. u32 autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
  2322. u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
  2323. bool locked = false;
  2324. s32 ret_val;
  2325. /*
  2326. * Link must be up to auto-blink the LEDs;
  2327. * Force it if link is down.
  2328. */
  2329. hw->mac.ops.check_link(hw, &speed, &link_up, false);
  2330. if (!link_up) {
  2331. ret_val = hw->mac.ops.prot_autoc_read(hw, &locked, &autoc_reg);
  2332. if (ret_val)
  2333. return ret_val;
  2334. autoc_reg |= IXGBE_AUTOC_AN_RESTART;
  2335. autoc_reg |= IXGBE_AUTOC_FLU;
  2336. ret_val = hw->mac.ops.prot_autoc_write(hw, autoc_reg, locked);
  2337. if (ret_val)
  2338. return ret_val;
  2339. IXGBE_WRITE_FLUSH(hw);
  2340. usleep_range(10000, 20000);
  2341. }
  2342. led_reg &= ~IXGBE_LED_MODE_MASK(index);
  2343. led_reg |= IXGBE_LED_BLINK(index);
  2344. IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
  2345. IXGBE_WRITE_FLUSH(hw);
  2346. return 0;
  2347. }
  2348. /**
  2349. * ixgbe_blink_led_stop_generic - Stop blinking LED based on index.
  2350. * @hw: pointer to hardware structure
  2351. * @index: led number to stop blinking
  2352. **/
  2353. s32 ixgbe_blink_led_stop_generic(struct ixgbe_hw *hw, u32 index)
  2354. {
  2355. u32 autoc_reg = 0;
  2356. u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
  2357. bool locked = false;
  2358. s32 ret_val;
  2359. ret_val = hw->mac.ops.prot_autoc_read(hw, &locked, &autoc_reg);
  2360. if (ret_val)
  2361. return ret_val;
  2362. autoc_reg &= ~IXGBE_AUTOC_FLU;
  2363. autoc_reg |= IXGBE_AUTOC_AN_RESTART;
  2364. ret_val = hw->mac.ops.prot_autoc_write(hw, autoc_reg, locked);
  2365. if (ret_val)
  2366. return ret_val;
  2367. led_reg &= ~IXGBE_LED_MODE_MASK(index);
  2368. led_reg &= ~IXGBE_LED_BLINK(index);
  2369. led_reg |= IXGBE_LED_LINK_ACTIVE << IXGBE_LED_MODE_SHIFT(index);
  2370. IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
  2371. IXGBE_WRITE_FLUSH(hw);
  2372. return 0;
  2373. }
  2374. /**
  2375. * ixgbe_get_san_mac_addr_offset - Get SAN MAC address offset from the EEPROM
  2376. * @hw: pointer to hardware structure
  2377. * @san_mac_offset: SAN MAC address offset
  2378. *
  2379. * This function will read the EEPROM location for the SAN MAC address
  2380. * pointer, and returns the value at that location. This is used in both
  2381. * get and set mac_addr routines.
  2382. **/
  2383. static s32 ixgbe_get_san_mac_addr_offset(struct ixgbe_hw *hw,
  2384. u16 *san_mac_offset)
  2385. {
  2386. s32 ret_val;
  2387. /*
  2388. * First read the EEPROM pointer to see if the MAC addresses are
  2389. * available.
  2390. */
  2391. ret_val = hw->eeprom.ops.read(hw, IXGBE_SAN_MAC_ADDR_PTR,
  2392. san_mac_offset);
  2393. if (ret_val)
  2394. hw_err(hw, "eeprom read at offset %d failed\n",
  2395. IXGBE_SAN_MAC_ADDR_PTR);
  2396. return ret_val;
  2397. }
  2398. /**
  2399. * ixgbe_get_san_mac_addr_generic - SAN MAC address retrieval from the EEPROM
  2400. * @hw: pointer to hardware structure
  2401. * @san_mac_addr: SAN MAC address
  2402. *
  2403. * Reads the SAN MAC address from the EEPROM, if it's available. This is
  2404. * per-port, so set_lan_id() must be called before reading the addresses.
  2405. * set_lan_id() is called by identify_sfp(), but this cannot be relied
  2406. * upon for non-SFP connections, so we must call it here.
  2407. **/
  2408. s32 ixgbe_get_san_mac_addr_generic(struct ixgbe_hw *hw, u8 *san_mac_addr)
  2409. {
  2410. u16 san_mac_data, san_mac_offset;
  2411. u8 i;
  2412. s32 ret_val;
  2413. /*
  2414. * First read the EEPROM pointer to see if the MAC addresses are
  2415. * available. If they're not, no point in calling set_lan_id() here.
  2416. */
  2417. ret_val = ixgbe_get_san_mac_addr_offset(hw, &san_mac_offset);
  2418. if (ret_val || san_mac_offset == 0 || san_mac_offset == 0xFFFF)
  2419. goto san_mac_addr_clr;
  2420. /* make sure we know which port we need to program */
  2421. hw->mac.ops.set_lan_id(hw);
  2422. /* apply the port offset to the address offset */
  2423. (hw->bus.func) ? (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT1_OFFSET) :
  2424. (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT0_OFFSET);
  2425. for (i = 0; i < 3; i++) {
  2426. ret_val = hw->eeprom.ops.read(hw, san_mac_offset,
  2427. &san_mac_data);
  2428. if (ret_val) {
  2429. hw_err(hw, "eeprom read at offset %d failed\n",
  2430. san_mac_offset);
  2431. goto san_mac_addr_clr;
  2432. }
  2433. san_mac_addr[i * 2] = (u8)(san_mac_data);
  2434. san_mac_addr[i * 2 + 1] = (u8)(san_mac_data >> 8);
  2435. san_mac_offset++;
  2436. }
  2437. return 0;
  2438. san_mac_addr_clr:
  2439. /* No addresses available in this EEPROM. It's not necessarily an
  2440. * error though, so just wipe the local address and return.
  2441. */
  2442. for (i = 0; i < 6; i++)
  2443. san_mac_addr[i] = 0xFF;
  2444. return ret_val;
  2445. }
  2446. /**
  2447. * ixgbe_get_pcie_msix_count_generic - Gets MSI-X vector count
  2448. * @hw: pointer to hardware structure
  2449. *
  2450. * Read PCIe configuration space, and get the MSI-X vector count from
  2451. * the capabilities table.
  2452. **/
  2453. u16 ixgbe_get_pcie_msix_count_generic(struct ixgbe_hw *hw)
  2454. {
  2455. u16 msix_count;
  2456. u16 max_msix_count;
  2457. u16 pcie_offset;
  2458. switch (hw->mac.type) {
  2459. case ixgbe_mac_82598EB:
  2460. pcie_offset = IXGBE_PCIE_MSIX_82598_CAPS;
  2461. max_msix_count = IXGBE_MAX_MSIX_VECTORS_82598;
  2462. break;
  2463. case ixgbe_mac_82599EB:
  2464. case ixgbe_mac_X540:
  2465. case ixgbe_mac_X550:
  2466. case ixgbe_mac_X550EM_x:
  2467. case ixgbe_mac_x550em_a:
  2468. pcie_offset = IXGBE_PCIE_MSIX_82599_CAPS;
  2469. max_msix_count = IXGBE_MAX_MSIX_VECTORS_82599;
  2470. break;
  2471. default:
  2472. return 1;
  2473. }
  2474. msix_count = ixgbe_read_pci_cfg_word(hw, pcie_offset);
  2475. if (ixgbe_removed(hw->hw_addr))
  2476. msix_count = 0;
  2477. msix_count &= IXGBE_PCIE_MSIX_TBL_SZ_MASK;
  2478. /* MSI-X count is zero-based in HW */
  2479. msix_count++;
  2480. if (msix_count > max_msix_count)
  2481. msix_count = max_msix_count;
  2482. return msix_count;
  2483. }
  2484. /**
  2485. * ixgbe_clear_vmdq_generic - Disassociate a VMDq pool index from a rx address
  2486. * @hw: pointer to hardware struct
  2487. * @rar: receive address register index to disassociate
  2488. * @vmdq: VMDq pool index to remove from the rar
  2489. **/
  2490. s32 ixgbe_clear_vmdq_generic(struct ixgbe_hw *hw, u32 rar, u32 vmdq)
  2491. {
  2492. u32 mpsar_lo, mpsar_hi;
  2493. u32 rar_entries = hw->mac.num_rar_entries;
  2494. /* Make sure we are using a valid rar index range */
  2495. if (rar >= rar_entries) {
  2496. hw_dbg(hw, "RAR index %d is out of range.\n", rar);
  2497. return IXGBE_ERR_INVALID_ARGUMENT;
  2498. }
  2499. mpsar_lo = IXGBE_READ_REG(hw, IXGBE_MPSAR_LO(rar));
  2500. mpsar_hi = IXGBE_READ_REG(hw, IXGBE_MPSAR_HI(rar));
  2501. if (ixgbe_removed(hw->hw_addr))
  2502. return 0;
  2503. if (!mpsar_lo && !mpsar_hi)
  2504. return 0;
  2505. if (vmdq == IXGBE_CLEAR_VMDQ_ALL) {
  2506. if (mpsar_lo) {
  2507. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 0);
  2508. mpsar_lo = 0;
  2509. }
  2510. if (mpsar_hi) {
  2511. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 0);
  2512. mpsar_hi = 0;
  2513. }
  2514. } else if (vmdq < 32) {
  2515. mpsar_lo &= ~(1 << vmdq);
  2516. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), mpsar_lo);
  2517. } else {
  2518. mpsar_hi &= ~(1 << (vmdq - 32));
  2519. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), mpsar_hi);
  2520. }
  2521. /* was that the last pool using this rar? */
  2522. if (mpsar_lo == 0 && mpsar_hi == 0 && rar != 0)
  2523. hw->mac.ops.clear_rar(hw, rar);
  2524. return 0;
  2525. }
  2526. /**
  2527. * ixgbe_set_vmdq_generic - Associate a VMDq pool index with a rx address
  2528. * @hw: pointer to hardware struct
  2529. * @rar: receive address register index to associate with a VMDq index
  2530. * @vmdq: VMDq pool index
  2531. **/
  2532. s32 ixgbe_set_vmdq_generic(struct ixgbe_hw *hw, u32 rar, u32 vmdq)
  2533. {
  2534. u32 mpsar;
  2535. u32 rar_entries = hw->mac.num_rar_entries;
  2536. /* Make sure we are using a valid rar index range */
  2537. if (rar >= rar_entries) {
  2538. hw_dbg(hw, "RAR index %d is out of range.\n", rar);
  2539. return IXGBE_ERR_INVALID_ARGUMENT;
  2540. }
  2541. if (vmdq < 32) {
  2542. mpsar = IXGBE_READ_REG(hw, IXGBE_MPSAR_LO(rar));
  2543. mpsar |= 1 << vmdq;
  2544. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), mpsar);
  2545. } else {
  2546. mpsar = IXGBE_READ_REG(hw, IXGBE_MPSAR_HI(rar));
  2547. mpsar |= 1 << (vmdq - 32);
  2548. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), mpsar);
  2549. }
  2550. return 0;
  2551. }
  2552. /**
  2553. * This function should only be involved in the IOV mode.
  2554. * In IOV mode, Default pool is next pool after the number of
  2555. * VFs advertized and not 0.
  2556. * MPSAR table needs to be updated for SAN_MAC RAR [hw->mac.san_mac_rar_index]
  2557. *
  2558. * ixgbe_set_vmdq_san_mac - Associate default VMDq pool index with a rx address
  2559. * @hw: pointer to hardware struct
  2560. * @vmdq: VMDq pool index
  2561. **/
  2562. s32 ixgbe_set_vmdq_san_mac_generic(struct ixgbe_hw *hw, u32 vmdq)
  2563. {
  2564. u32 rar = hw->mac.san_mac_rar_index;
  2565. if (vmdq < 32) {
  2566. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 1 << vmdq);
  2567. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 0);
  2568. } else {
  2569. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 0);
  2570. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 1 << (vmdq - 32));
  2571. }
  2572. return 0;
  2573. }
  2574. /**
  2575. * ixgbe_init_uta_tables_generic - Initialize the Unicast Table Array
  2576. * @hw: pointer to hardware structure
  2577. **/
  2578. s32 ixgbe_init_uta_tables_generic(struct ixgbe_hw *hw)
  2579. {
  2580. int i;
  2581. for (i = 0; i < 128; i++)
  2582. IXGBE_WRITE_REG(hw, IXGBE_UTA(i), 0);
  2583. return 0;
  2584. }
  2585. /**
  2586. * ixgbe_find_vlvf_slot - find the vlanid or the first empty slot
  2587. * @hw: pointer to hardware structure
  2588. * @vlan: VLAN id to write to VLAN filter
  2589. *
  2590. * return the VLVF index where this VLAN id should be placed
  2591. *
  2592. **/
  2593. static s32 ixgbe_find_vlvf_slot(struct ixgbe_hw *hw, u32 vlan, bool vlvf_bypass)
  2594. {
  2595. s32 regindex, first_empty_slot;
  2596. u32 bits;
  2597. /* short cut the special case */
  2598. if (vlan == 0)
  2599. return 0;
  2600. /* if vlvf_bypass is set we don't want to use an empty slot, we
  2601. * will simply bypass the VLVF if there are no entries present in the
  2602. * VLVF that contain our VLAN
  2603. */
  2604. first_empty_slot = vlvf_bypass ? IXGBE_ERR_NO_SPACE : 0;
  2605. /* add VLAN enable bit for comparison */
  2606. vlan |= IXGBE_VLVF_VIEN;
  2607. /* Search for the vlan id in the VLVF entries. Save off the first empty
  2608. * slot found along the way.
  2609. *
  2610. * pre-decrement loop covering (IXGBE_VLVF_ENTRIES - 1) .. 1
  2611. */
  2612. for (regindex = IXGBE_VLVF_ENTRIES; --regindex;) {
  2613. bits = IXGBE_READ_REG(hw, IXGBE_VLVF(regindex));
  2614. if (bits == vlan)
  2615. return regindex;
  2616. if (!first_empty_slot && !bits)
  2617. first_empty_slot = regindex;
  2618. }
  2619. /* If we are here then we didn't find the VLAN. Return first empty
  2620. * slot we found during our search, else error.
  2621. */
  2622. if (!first_empty_slot)
  2623. hw_dbg(hw, "No space in VLVF.\n");
  2624. return first_empty_slot ? : IXGBE_ERR_NO_SPACE;
  2625. }
  2626. /**
  2627. * ixgbe_set_vfta_generic - Set VLAN filter table
  2628. * @hw: pointer to hardware structure
  2629. * @vlan: VLAN id to write to VLAN filter
  2630. * @vind: VMDq output index that maps queue to VLAN id in VFVFB
  2631. * @vlan_on: boolean flag to turn on/off VLAN in VFVF
  2632. * @vlvf_bypass: boolean flag indicating updating default pool is okay
  2633. *
  2634. * Turn on/off specified VLAN in the VLAN filter table.
  2635. **/
  2636. s32 ixgbe_set_vfta_generic(struct ixgbe_hw *hw, u32 vlan, u32 vind,
  2637. bool vlan_on, bool vlvf_bypass)
  2638. {
  2639. u32 regidx, vfta_delta, vfta, bits;
  2640. s32 vlvf_index;
  2641. if ((vlan > 4095) || (vind > 63))
  2642. return IXGBE_ERR_PARAM;
  2643. /*
  2644. * this is a 2 part operation - first the VFTA, then the
  2645. * VLVF and VLVFB if VT Mode is set
  2646. * We don't write the VFTA until we know the VLVF part succeeded.
  2647. */
  2648. /* Part 1
  2649. * The VFTA is a bitstring made up of 128 32-bit registers
  2650. * that enable the particular VLAN id, much like the MTA:
  2651. * bits[11-5]: which register
  2652. * bits[4-0]: which bit in the register
  2653. */
  2654. regidx = vlan / 32;
  2655. vfta_delta = 1 << (vlan % 32);
  2656. vfta = IXGBE_READ_REG(hw, IXGBE_VFTA(regidx));
  2657. /* vfta_delta represents the difference between the current value
  2658. * of vfta and the value we want in the register. Since the diff
  2659. * is an XOR mask we can just update vfta using an XOR.
  2660. */
  2661. vfta_delta &= vlan_on ? ~vfta : vfta;
  2662. vfta ^= vfta_delta;
  2663. /* Part 2
  2664. * If VT Mode is set
  2665. * Either vlan_on
  2666. * make sure the vlan is in VLVF
  2667. * set the vind bit in the matching VLVFB
  2668. * Or !vlan_on
  2669. * clear the pool bit and possibly the vind
  2670. */
  2671. if (!(IXGBE_READ_REG(hw, IXGBE_VT_CTL) & IXGBE_VT_CTL_VT_ENABLE))
  2672. goto vfta_update;
  2673. vlvf_index = ixgbe_find_vlvf_slot(hw, vlan, vlvf_bypass);
  2674. if (vlvf_index < 0) {
  2675. if (vlvf_bypass)
  2676. goto vfta_update;
  2677. return vlvf_index;
  2678. }
  2679. bits = IXGBE_READ_REG(hw, IXGBE_VLVFB(vlvf_index * 2 + vind / 32));
  2680. /* set the pool bit */
  2681. bits |= 1 << (vind % 32);
  2682. if (vlan_on)
  2683. goto vlvf_update;
  2684. /* clear the pool bit */
  2685. bits ^= 1 << (vind % 32);
  2686. if (!bits &&
  2687. !IXGBE_READ_REG(hw, IXGBE_VLVFB(vlvf_index * 2 + 1 - vind / 32))) {
  2688. /* Clear VFTA first, then disable VLVF. Otherwise
  2689. * we run the risk of stray packets leaking into
  2690. * the PF via the default pool
  2691. */
  2692. if (vfta_delta)
  2693. IXGBE_WRITE_REG(hw, IXGBE_VFTA(regidx), vfta);
  2694. /* disable VLVF and clear remaining bit from pool */
  2695. IXGBE_WRITE_REG(hw, IXGBE_VLVF(vlvf_index), 0);
  2696. IXGBE_WRITE_REG(hw, IXGBE_VLVFB(vlvf_index * 2 + vind / 32), 0);
  2697. return 0;
  2698. }
  2699. /* If there are still bits set in the VLVFB registers
  2700. * for the VLAN ID indicated we need to see if the
  2701. * caller is requesting that we clear the VFTA entry bit.
  2702. * If the caller has requested that we clear the VFTA
  2703. * entry bit but there are still pools/VFs using this VLAN
  2704. * ID entry then ignore the request. We're not worried
  2705. * about the case where we're turning the VFTA VLAN ID
  2706. * entry bit on, only when requested to turn it off as
  2707. * there may be multiple pools and/or VFs using the
  2708. * VLAN ID entry. In that case we cannot clear the
  2709. * VFTA bit until all pools/VFs using that VLAN ID have also
  2710. * been cleared. This will be indicated by "bits" being
  2711. * zero.
  2712. */
  2713. vfta_delta = 0;
  2714. vlvf_update:
  2715. /* record pool change and enable VLAN ID if not already enabled */
  2716. IXGBE_WRITE_REG(hw, IXGBE_VLVFB(vlvf_index * 2 + vind / 32), bits);
  2717. IXGBE_WRITE_REG(hw, IXGBE_VLVF(vlvf_index), IXGBE_VLVF_VIEN | vlan);
  2718. vfta_update:
  2719. /* Update VFTA now that we are ready for traffic */
  2720. if (vfta_delta)
  2721. IXGBE_WRITE_REG(hw, IXGBE_VFTA(regidx), vfta);
  2722. return 0;
  2723. }
  2724. /**
  2725. * ixgbe_clear_vfta_generic - Clear VLAN filter table
  2726. * @hw: pointer to hardware structure
  2727. *
  2728. * Clears the VLAN filer table, and the VMDq index associated with the filter
  2729. **/
  2730. s32 ixgbe_clear_vfta_generic(struct ixgbe_hw *hw)
  2731. {
  2732. u32 offset;
  2733. for (offset = 0; offset < hw->mac.vft_size; offset++)
  2734. IXGBE_WRITE_REG(hw, IXGBE_VFTA(offset), 0);
  2735. for (offset = 0; offset < IXGBE_VLVF_ENTRIES; offset++) {
  2736. IXGBE_WRITE_REG(hw, IXGBE_VLVF(offset), 0);
  2737. IXGBE_WRITE_REG(hw, IXGBE_VLVFB(offset * 2), 0);
  2738. IXGBE_WRITE_REG(hw, IXGBE_VLVFB(offset * 2 + 1), 0);
  2739. }
  2740. return 0;
  2741. }
  2742. /**
  2743. * ixgbe_check_mac_link_generic - Determine link and speed status
  2744. * @hw: pointer to hardware structure
  2745. * @speed: pointer to link speed
  2746. * @link_up: true when link is up
  2747. * @link_up_wait_to_complete: bool used to wait for link up or not
  2748. *
  2749. * Reads the links register to determine if link is up and the current speed
  2750. **/
  2751. s32 ixgbe_check_mac_link_generic(struct ixgbe_hw *hw, ixgbe_link_speed *speed,
  2752. bool *link_up, bool link_up_wait_to_complete)
  2753. {
  2754. u32 links_reg, links_orig;
  2755. u32 i;
  2756. /* clear the old state */
  2757. links_orig = IXGBE_READ_REG(hw, IXGBE_LINKS);
  2758. links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
  2759. if (links_orig != links_reg) {
  2760. hw_dbg(hw, "LINKS changed from %08X to %08X\n",
  2761. links_orig, links_reg);
  2762. }
  2763. if (link_up_wait_to_complete) {
  2764. for (i = 0; i < IXGBE_LINK_UP_TIME; i++) {
  2765. if (links_reg & IXGBE_LINKS_UP) {
  2766. *link_up = true;
  2767. break;
  2768. } else {
  2769. *link_up = false;
  2770. }
  2771. msleep(100);
  2772. links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
  2773. }
  2774. } else {
  2775. if (links_reg & IXGBE_LINKS_UP)
  2776. *link_up = true;
  2777. else
  2778. *link_up = false;
  2779. }
  2780. switch (links_reg & IXGBE_LINKS_SPEED_82599) {
  2781. case IXGBE_LINKS_SPEED_10G_82599:
  2782. if ((hw->mac.type >= ixgbe_mac_X550) &&
  2783. (links_reg & IXGBE_LINKS_SPEED_NON_STD))
  2784. *speed = IXGBE_LINK_SPEED_2_5GB_FULL;
  2785. else
  2786. *speed = IXGBE_LINK_SPEED_10GB_FULL;
  2787. break;
  2788. case IXGBE_LINKS_SPEED_1G_82599:
  2789. *speed = IXGBE_LINK_SPEED_1GB_FULL;
  2790. break;
  2791. case IXGBE_LINKS_SPEED_100_82599:
  2792. if ((hw->mac.type >= ixgbe_mac_X550) &&
  2793. (links_reg & IXGBE_LINKS_SPEED_NON_STD))
  2794. *speed = IXGBE_LINK_SPEED_5GB_FULL;
  2795. else
  2796. *speed = IXGBE_LINK_SPEED_100_FULL;
  2797. break;
  2798. default:
  2799. *speed = IXGBE_LINK_SPEED_UNKNOWN;
  2800. }
  2801. return 0;
  2802. }
  2803. /**
  2804. * ixgbe_get_wwn_prefix_generic - Get alternative WWNN/WWPN prefix from
  2805. * the EEPROM
  2806. * @hw: pointer to hardware structure
  2807. * @wwnn_prefix: the alternative WWNN prefix
  2808. * @wwpn_prefix: the alternative WWPN prefix
  2809. *
  2810. * This function will read the EEPROM from the alternative SAN MAC address
  2811. * block to check the support for the alternative WWNN/WWPN prefix support.
  2812. **/
  2813. s32 ixgbe_get_wwn_prefix_generic(struct ixgbe_hw *hw, u16 *wwnn_prefix,
  2814. u16 *wwpn_prefix)
  2815. {
  2816. u16 offset, caps;
  2817. u16 alt_san_mac_blk_offset;
  2818. /* clear output first */
  2819. *wwnn_prefix = 0xFFFF;
  2820. *wwpn_prefix = 0xFFFF;
  2821. /* check if alternative SAN MAC is supported */
  2822. offset = IXGBE_ALT_SAN_MAC_ADDR_BLK_PTR;
  2823. if (hw->eeprom.ops.read(hw, offset, &alt_san_mac_blk_offset))
  2824. goto wwn_prefix_err;
  2825. if ((alt_san_mac_blk_offset == 0) ||
  2826. (alt_san_mac_blk_offset == 0xFFFF))
  2827. return 0;
  2828. /* check capability in alternative san mac address block */
  2829. offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_CAPS_OFFSET;
  2830. if (hw->eeprom.ops.read(hw, offset, &caps))
  2831. goto wwn_prefix_err;
  2832. if (!(caps & IXGBE_ALT_SAN_MAC_ADDR_CAPS_ALTWWN))
  2833. return 0;
  2834. /* get the corresponding prefix for WWNN/WWPN */
  2835. offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_WWNN_OFFSET;
  2836. if (hw->eeprom.ops.read(hw, offset, wwnn_prefix))
  2837. hw_err(hw, "eeprom read at offset %d failed\n", offset);
  2838. offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_WWPN_OFFSET;
  2839. if (hw->eeprom.ops.read(hw, offset, wwpn_prefix))
  2840. goto wwn_prefix_err;
  2841. return 0;
  2842. wwn_prefix_err:
  2843. hw_err(hw, "eeprom read at offset %d failed\n", offset);
  2844. return 0;
  2845. }
  2846. /**
  2847. * ixgbe_set_mac_anti_spoofing - Enable/Disable MAC anti-spoofing
  2848. * @hw: pointer to hardware structure
  2849. * @enable: enable or disable switch for anti-spoofing
  2850. * @pf: Physical Function pool - do not enable anti-spoofing for the PF
  2851. *
  2852. **/
  2853. void ixgbe_set_mac_anti_spoofing(struct ixgbe_hw *hw, bool enable, int pf)
  2854. {
  2855. int j;
  2856. int pf_target_reg = pf >> 3;
  2857. int pf_target_shift = pf % 8;
  2858. u32 pfvfspoof = 0;
  2859. if (hw->mac.type == ixgbe_mac_82598EB)
  2860. return;
  2861. if (enable)
  2862. pfvfspoof = IXGBE_SPOOF_MACAS_MASK;
  2863. /*
  2864. * PFVFSPOOF register array is size 8 with 8 bits assigned to
  2865. * MAC anti-spoof enables in each register array element.
  2866. */
  2867. for (j = 0; j < pf_target_reg; j++)
  2868. IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(j), pfvfspoof);
  2869. /*
  2870. * The PF should be allowed to spoof so that it can support
  2871. * emulation mode NICs. Do not set the bits assigned to the PF
  2872. */
  2873. pfvfspoof &= (1 << pf_target_shift) - 1;
  2874. IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(j), pfvfspoof);
  2875. /*
  2876. * Remaining pools belong to the PF so they do not need to have
  2877. * anti-spoofing enabled.
  2878. */
  2879. for (j++; j < IXGBE_PFVFSPOOF_REG_COUNT; j++)
  2880. IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(j), 0);
  2881. }
  2882. /**
  2883. * ixgbe_set_vlan_anti_spoofing - Enable/Disable VLAN anti-spoofing
  2884. * @hw: pointer to hardware structure
  2885. * @enable: enable or disable switch for VLAN anti-spoofing
  2886. * @pf: Virtual Function pool - VF Pool to set for VLAN anti-spoofing
  2887. *
  2888. **/
  2889. void ixgbe_set_vlan_anti_spoofing(struct ixgbe_hw *hw, bool enable, int vf)
  2890. {
  2891. int vf_target_reg = vf >> 3;
  2892. int vf_target_shift = vf % 8 + IXGBE_SPOOF_VLANAS_SHIFT;
  2893. u32 pfvfspoof;
  2894. if (hw->mac.type == ixgbe_mac_82598EB)
  2895. return;
  2896. pfvfspoof = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
  2897. if (enable)
  2898. pfvfspoof |= (1 << vf_target_shift);
  2899. else
  2900. pfvfspoof &= ~(1 << vf_target_shift);
  2901. IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), pfvfspoof);
  2902. }
  2903. /**
  2904. * ixgbe_get_device_caps_generic - Get additional device capabilities
  2905. * @hw: pointer to hardware structure
  2906. * @device_caps: the EEPROM word with the extra device capabilities
  2907. *
  2908. * This function will read the EEPROM location for the device capabilities,
  2909. * and return the word through device_caps.
  2910. **/
  2911. s32 ixgbe_get_device_caps_generic(struct ixgbe_hw *hw, u16 *device_caps)
  2912. {
  2913. hw->eeprom.ops.read(hw, IXGBE_DEVICE_CAPS, device_caps);
  2914. return 0;
  2915. }
  2916. /**
  2917. * ixgbe_set_rxpba_generic - Initialize RX packet buffer
  2918. * @hw: pointer to hardware structure
  2919. * @num_pb: number of packet buffers to allocate
  2920. * @headroom: reserve n KB of headroom
  2921. * @strategy: packet buffer allocation strategy
  2922. **/
  2923. void ixgbe_set_rxpba_generic(struct ixgbe_hw *hw,
  2924. int num_pb,
  2925. u32 headroom,
  2926. int strategy)
  2927. {
  2928. u32 pbsize = hw->mac.rx_pb_size;
  2929. int i = 0;
  2930. u32 rxpktsize, txpktsize, txpbthresh;
  2931. /* Reserve headroom */
  2932. pbsize -= headroom;
  2933. if (!num_pb)
  2934. num_pb = 1;
  2935. /* Divide remaining packet buffer space amongst the number
  2936. * of packet buffers requested using supplied strategy.
  2937. */
  2938. switch (strategy) {
  2939. case (PBA_STRATEGY_WEIGHTED):
  2940. /* pba_80_48 strategy weight first half of packet buffer with
  2941. * 5/8 of the packet buffer space.
  2942. */
  2943. rxpktsize = ((pbsize * 5 * 2) / (num_pb * 8));
  2944. pbsize -= rxpktsize * (num_pb / 2);
  2945. rxpktsize <<= IXGBE_RXPBSIZE_SHIFT;
  2946. for (; i < (num_pb / 2); i++)
  2947. IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), rxpktsize);
  2948. /* Fall through to configure remaining packet buffers */
  2949. case (PBA_STRATEGY_EQUAL):
  2950. /* Divide the remaining Rx packet buffer evenly among the TCs */
  2951. rxpktsize = (pbsize / (num_pb - i)) << IXGBE_RXPBSIZE_SHIFT;
  2952. for (; i < num_pb; i++)
  2953. IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), rxpktsize);
  2954. break;
  2955. default:
  2956. break;
  2957. }
  2958. /*
  2959. * Setup Tx packet buffer and threshold equally for all TCs
  2960. * TXPBTHRESH register is set in K so divide by 1024 and subtract
  2961. * 10 since the largest packet we support is just over 9K.
  2962. */
  2963. txpktsize = IXGBE_TXPBSIZE_MAX / num_pb;
  2964. txpbthresh = (txpktsize / 1024) - IXGBE_TXPKT_SIZE_MAX;
  2965. for (i = 0; i < num_pb; i++) {
  2966. IXGBE_WRITE_REG(hw, IXGBE_TXPBSIZE(i), txpktsize);
  2967. IXGBE_WRITE_REG(hw, IXGBE_TXPBTHRESH(i), txpbthresh);
  2968. }
  2969. /* Clear unused TCs, if any, to zero buffer size*/
  2970. for (; i < IXGBE_MAX_PB; i++) {
  2971. IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), 0);
  2972. IXGBE_WRITE_REG(hw, IXGBE_TXPBSIZE(i), 0);
  2973. IXGBE_WRITE_REG(hw, IXGBE_TXPBTHRESH(i), 0);
  2974. }
  2975. }
  2976. /**
  2977. * ixgbe_calculate_checksum - Calculate checksum for buffer
  2978. * @buffer: pointer to EEPROM
  2979. * @length: size of EEPROM to calculate a checksum for
  2980. *
  2981. * Calculates the checksum for some buffer on a specified length. The
  2982. * checksum calculated is returned.
  2983. **/
  2984. static u8 ixgbe_calculate_checksum(u8 *buffer, u32 length)
  2985. {
  2986. u32 i;
  2987. u8 sum = 0;
  2988. if (!buffer)
  2989. return 0;
  2990. for (i = 0; i < length; i++)
  2991. sum += buffer[i];
  2992. return (u8) (0 - sum);
  2993. }
  2994. /**
  2995. * ixgbe_host_interface_command - Issue command to manageability block
  2996. * @hw: pointer to the HW structure
  2997. * @buffer: contains the command to write and where the return status will
  2998. * be placed
  2999. * @length: length of buffer, must be multiple of 4 bytes
  3000. * @timeout: time in ms to wait for command completion
  3001. * @return_data: read and return data from the buffer (true) or not (false)
  3002. * Needed because FW structures are big endian and decoding of
  3003. * these fields can be 8 bit or 16 bit based on command. Decoding
  3004. * is not easily understood without making a table of commands.
  3005. * So we will leave this up to the caller to read back the data
  3006. * in these cases.
  3007. *
  3008. * Communicates with the manageability block. On success return 0
  3009. * else return IXGBE_ERR_HOST_INTERFACE_COMMAND.
  3010. **/
  3011. s32 ixgbe_host_interface_command(struct ixgbe_hw *hw, void *buffer,
  3012. u32 length, u32 timeout,
  3013. bool return_data)
  3014. {
  3015. u32 hdr_size = sizeof(struct ixgbe_hic_hdr);
  3016. u32 hicr, i, bi, fwsts;
  3017. u16 buf_len, dword_len;
  3018. union {
  3019. struct ixgbe_hic_hdr hdr;
  3020. u32 u32arr[1];
  3021. } *bp = buffer;
  3022. s32 status;
  3023. if (!length || length > IXGBE_HI_MAX_BLOCK_BYTE_LENGTH) {
  3024. hw_dbg(hw, "Buffer length failure buffersize-%d.\n", length);
  3025. return IXGBE_ERR_HOST_INTERFACE_COMMAND;
  3026. }
  3027. /* Take management host interface semaphore */
  3028. status = hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_SW_MNG_SM);
  3029. if (status)
  3030. return status;
  3031. /* Set bit 9 of FWSTS clearing FW reset indication */
  3032. fwsts = IXGBE_READ_REG(hw, IXGBE_FWSTS);
  3033. IXGBE_WRITE_REG(hw, IXGBE_FWSTS, fwsts | IXGBE_FWSTS_FWRI);
  3034. /* Check that the host interface is enabled. */
  3035. hicr = IXGBE_READ_REG(hw, IXGBE_HICR);
  3036. if (!(hicr & IXGBE_HICR_EN)) {
  3037. hw_dbg(hw, "IXGBE_HOST_EN bit disabled.\n");
  3038. status = IXGBE_ERR_HOST_INTERFACE_COMMAND;
  3039. goto rel_out;
  3040. }
  3041. /* Calculate length in DWORDs. We must be DWORD aligned */
  3042. if (length % sizeof(u32)) {
  3043. hw_dbg(hw, "Buffer length failure, not aligned to dword");
  3044. status = IXGBE_ERR_INVALID_ARGUMENT;
  3045. goto rel_out;
  3046. }
  3047. dword_len = length >> 2;
  3048. /* The device driver writes the relevant command block
  3049. * into the ram area.
  3050. */
  3051. for (i = 0; i < dword_len; i++)
  3052. IXGBE_WRITE_REG_ARRAY(hw, IXGBE_FLEX_MNG,
  3053. i, cpu_to_le32(bp->u32arr[i]));
  3054. /* Setting this bit tells the ARC that a new command is pending. */
  3055. IXGBE_WRITE_REG(hw, IXGBE_HICR, hicr | IXGBE_HICR_C);
  3056. for (i = 0; i < timeout; i++) {
  3057. hicr = IXGBE_READ_REG(hw, IXGBE_HICR);
  3058. if (!(hicr & IXGBE_HICR_C))
  3059. break;
  3060. usleep_range(1000, 2000);
  3061. }
  3062. /* Check command successful completion. */
  3063. if ((timeout && i == timeout) ||
  3064. !(IXGBE_READ_REG(hw, IXGBE_HICR) & IXGBE_HICR_SV)) {
  3065. hw_dbg(hw, "Command has failed with no status valid.\n");
  3066. status = IXGBE_ERR_HOST_INTERFACE_COMMAND;
  3067. goto rel_out;
  3068. }
  3069. if (!return_data)
  3070. goto rel_out;
  3071. /* Calculate length in DWORDs */
  3072. dword_len = hdr_size >> 2;
  3073. /* first pull in the header so we know the buffer length */
  3074. for (bi = 0; bi < dword_len; bi++) {
  3075. bp->u32arr[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi);
  3076. le32_to_cpus(&bp->u32arr[bi]);
  3077. }
  3078. /* If there is any thing in data position pull it in */
  3079. buf_len = bp->hdr.buf_len;
  3080. if (!buf_len)
  3081. goto rel_out;
  3082. if (length < round_up(buf_len, 4) + hdr_size) {
  3083. hw_dbg(hw, "Buffer not large enough for reply message.\n");
  3084. status = IXGBE_ERR_HOST_INTERFACE_COMMAND;
  3085. goto rel_out;
  3086. }
  3087. /* Calculate length in DWORDs, add 3 for odd lengths */
  3088. dword_len = (buf_len + 3) >> 2;
  3089. /* Pull in the rest of the buffer (bi is where we left off) */
  3090. for (; bi <= dword_len; bi++) {
  3091. bp->u32arr[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi);
  3092. le32_to_cpus(&bp->u32arr[bi]);
  3093. }
  3094. rel_out:
  3095. hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_SW_MNG_SM);
  3096. return status;
  3097. }
  3098. /**
  3099. * ixgbe_set_fw_drv_ver_generic - Sends driver version to firmware
  3100. * @hw: pointer to the HW structure
  3101. * @maj: driver version major number
  3102. * @min: driver version minor number
  3103. * @build: driver version build number
  3104. * @sub: driver version sub build number
  3105. *
  3106. * Sends driver version number to firmware through the manageability
  3107. * block. On success return 0
  3108. * else returns IXGBE_ERR_SWFW_SYNC when encountering an error acquiring
  3109. * semaphore or IXGBE_ERR_HOST_INTERFACE_COMMAND when command fails.
  3110. **/
  3111. s32 ixgbe_set_fw_drv_ver_generic(struct ixgbe_hw *hw, u8 maj, u8 min,
  3112. u8 build, u8 sub)
  3113. {
  3114. struct ixgbe_hic_drv_info fw_cmd;
  3115. int i;
  3116. s32 ret_val;
  3117. fw_cmd.hdr.cmd = FW_CEM_CMD_DRIVER_INFO;
  3118. fw_cmd.hdr.buf_len = FW_CEM_CMD_DRIVER_INFO_LEN;
  3119. fw_cmd.hdr.cmd_or_resp.cmd_resv = FW_CEM_CMD_RESERVED;
  3120. fw_cmd.port_num = hw->bus.func;
  3121. fw_cmd.ver_maj = maj;
  3122. fw_cmd.ver_min = min;
  3123. fw_cmd.ver_build = build;
  3124. fw_cmd.ver_sub = sub;
  3125. fw_cmd.hdr.checksum = 0;
  3126. fw_cmd.hdr.checksum = ixgbe_calculate_checksum((u8 *)&fw_cmd,
  3127. (FW_CEM_HDR_LEN + fw_cmd.hdr.buf_len));
  3128. fw_cmd.pad = 0;
  3129. fw_cmd.pad2 = 0;
  3130. for (i = 0; i <= FW_CEM_MAX_RETRIES; i++) {
  3131. ret_val = ixgbe_host_interface_command(hw, &fw_cmd,
  3132. sizeof(fw_cmd),
  3133. IXGBE_HI_COMMAND_TIMEOUT,
  3134. true);
  3135. if (ret_val != 0)
  3136. continue;
  3137. if (fw_cmd.hdr.cmd_or_resp.ret_status ==
  3138. FW_CEM_RESP_STATUS_SUCCESS)
  3139. ret_val = 0;
  3140. else
  3141. ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
  3142. break;
  3143. }
  3144. return ret_val;
  3145. }
  3146. /**
  3147. * ixgbe_clear_tx_pending - Clear pending TX work from the PCIe fifo
  3148. * @hw: pointer to the hardware structure
  3149. *
  3150. * The 82599 and x540 MACs can experience issues if TX work is still pending
  3151. * when a reset occurs. This function prevents this by flushing the PCIe
  3152. * buffers on the system.
  3153. **/
  3154. void ixgbe_clear_tx_pending(struct ixgbe_hw *hw)
  3155. {
  3156. u32 gcr_ext, hlreg0, i, poll;
  3157. u16 value;
  3158. /*
  3159. * If double reset is not requested then all transactions should
  3160. * already be clear and as such there is no work to do
  3161. */
  3162. if (!(hw->mac.flags & IXGBE_FLAGS_DOUBLE_RESET_REQUIRED))
  3163. return;
  3164. /*
  3165. * Set loopback enable to prevent any transmits from being sent
  3166. * should the link come up. This assumes that the RXCTRL.RXEN bit
  3167. * has already been cleared.
  3168. */
  3169. hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
  3170. IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0 | IXGBE_HLREG0_LPBK);
  3171. /* wait for a last completion before clearing buffers */
  3172. IXGBE_WRITE_FLUSH(hw);
  3173. usleep_range(3000, 6000);
  3174. /* Before proceeding, make sure that the PCIe block does not have
  3175. * transactions pending.
  3176. */
  3177. poll = ixgbe_pcie_timeout_poll(hw);
  3178. for (i = 0; i < poll; i++) {
  3179. usleep_range(100, 200);
  3180. value = ixgbe_read_pci_cfg_word(hw, IXGBE_PCI_DEVICE_STATUS);
  3181. if (ixgbe_removed(hw->hw_addr))
  3182. break;
  3183. if (!(value & IXGBE_PCI_DEVICE_STATUS_TRANSACTION_PENDING))
  3184. break;
  3185. }
  3186. /* initiate cleaning flow for buffers in the PCIe transaction layer */
  3187. gcr_ext = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
  3188. IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT,
  3189. gcr_ext | IXGBE_GCR_EXT_BUFFERS_CLEAR);
  3190. /* Flush all writes and allow 20usec for all transactions to clear */
  3191. IXGBE_WRITE_FLUSH(hw);
  3192. udelay(20);
  3193. /* restore previous register values */
  3194. IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr_ext);
  3195. IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
  3196. }
  3197. static const u8 ixgbe_emc_temp_data[4] = {
  3198. IXGBE_EMC_INTERNAL_DATA,
  3199. IXGBE_EMC_DIODE1_DATA,
  3200. IXGBE_EMC_DIODE2_DATA,
  3201. IXGBE_EMC_DIODE3_DATA
  3202. };
  3203. static const u8 ixgbe_emc_therm_limit[4] = {
  3204. IXGBE_EMC_INTERNAL_THERM_LIMIT,
  3205. IXGBE_EMC_DIODE1_THERM_LIMIT,
  3206. IXGBE_EMC_DIODE2_THERM_LIMIT,
  3207. IXGBE_EMC_DIODE3_THERM_LIMIT
  3208. };
  3209. /**
  3210. * ixgbe_get_ets_data - Extracts the ETS bit data
  3211. * @hw: pointer to hardware structure
  3212. * @ets_cfg: extected ETS data
  3213. * @ets_offset: offset of ETS data
  3214. *
  3215. * Returns error code.
  3216. **/
  3217. static s32 ixgbe_get_ets_data(struct ixgbe_hw *hw, u16 *ets_cfg,
  3218. u16 *ets_offset)
  3219. {
  3220. s32 status;
  3221. status = hw->eeprom.ops.read(hw, IXGBE_ETS_CFG, ets_offset);
  3222. if (status)
  3223. return status;
  3224. if ((*ets_offset == 0x0000) || (*ets_offset == 0xFFFF))
  3225. return IXGBE_NOT_IMPLEMENTED;
  3226. status = hw->eeprom.ops.read(hw, *ets_offset, ets_cfg);
  3227. if (status)
  3228. return status;
  3229. if ((*ets_cfg & IXGBE_ETS_TYPE_MASK) != IXGBE_ETS_TYPE_EMC_SHIFTED)
  3230. return IXGBE_NOT_IMPLEMENTED;
  3231. return 0;
  3232. }
  3233. /**
  3234. * ixgbe_get_thermal_sensor_data - Gathers thermal sensor data
  3235. * @hw: pointer to hardware structure
  3236. *
  3237. * Returns the thermal sensor data structure
  3238. **/
  3239. s32 ixgbe_get_thermal_sensor_data_generic(struct ixgbe_hw *hw)
  3240. {
  3241. s32 status;
  3242. u16 ets_offset;
  3243. u16 ets_cfg;
  3244. u16 ets_sensor;
  3245. u8 num_sensors;
  3246. u8 i;
  3247. struct ixgbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
  3248. /* Only support thermal sensors attached to physical port 0 */
  3249. if ((IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1))
  3250. return IXGBE_NOT_IMPLEMENTED;
  3251. status = ixgbe_get_ets_data(hw, &ets_cfg, &ets_offset);
  3252. if (status)
  3253. return status;
  3254. num_sensors = (ets_cfg & IXGBE_ETS_NUM_SENSORS_MASK);
  3255. if (num_sensors > IXGBE_MAX_SENSORS)
  3256. num_sensors = IXGBE_MAX_SENSORS;
  3257. for (i = 0; i < num_sensors; i++) {
  3258. u8 sensor_index;
  3259. u8 sensor_location;
  3260. status = hw->eeprom.ops.read(hw, (ets_offset + 1 + i),
  3261. &ets_sensor);
  3262. if (status)
  3263. return status;
  3264. sensor_index = ((ets_sensor & IXGBE_ETS_DATA_INDEX_MASK) >>
  3265. IXGBE_ETS_DATA_INDEX_SHIFT);
  3266. sensor_location = ((ets_sensor & IXGBE_ETS_DATA_LOC_MASK) >>
  3267. IXGBE_ETS_DATA_LOC_SHIFT);
  3268. if (sensor_location != 0) {
  3269. status = hw->phy.ops.read_i2c_byte(hw,
  3270. ixgbe_emc_temp_data[sensor_index],
  3271. IXGBE_I2C_THERMAL_SENSOR_ADDR,
  3272. &data->sensor[i].temp);
  3273. if (status)
  3274. return status;
  3275. }
  3276. }
  3277. return 0;
  3278. }
  3279. /**
  3280. * ixgbe_init_thermal_sensor_thresh_generic - Inits thermal sensor thresholds
  3281. * @hw: pointer to hardware structure
  3282. *
  3283. * Inits the thermal sensor thresholds according to the NVM map
  3284. * and save off the threshold and location values into mac.thermal_sensor_data
  3285. **/
  3286. s32 ixgbe_init_thermal_sensor_thresh_generic(struct ixgbe_hw *hw)
  3287. {
  3288. s32 status;
  3289. u16 ets_offset;
  3290. u16 ets_cfg;
  3291. u16 ets_sensor;
  3292. u8 low_thresh_delta;
  3293. u8 num_sensors;
  3294. u8 therm_limit;
  3295. u8 i;
  3296. struct ixgbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
  3297. memset(data, 0, sizeof(struct ixgbe_thermal_sensor_data));
  3298. /* Only support thermal sensors attached to physical port 0 */
  3299. if ((IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1))
  3300. return IXGBE_NOT_IMPLEMENTED;
  3301. status = ixgbe_get_ets_data(hw, &ets_cfg, &ets_offset);
  3302. if (status)
  3303. return status;
  3304. low_thresh_delta = ((ets_cfg & IXGBE_ETS_LTHRES_DELTA_MASK) >>
  3305. IXGBE_ETS_LTHRES_DELTA_SHIFT);
  3306. num_sensors = (ets_cfg & IXGBE_ETS_NUM_SENSORS_MASK);
  3307. if (num_sensors > IXGBE_MAX_SENSORS)
  3308. num_sensors = IXGBE_MAX_SENSORS;
  3309. for (i = 0; i < num_sensors; i++) {
  3310. u8 sensor_index;
  3311. u8 sensor_location;
  3312. if (hw->eeprom.ops.read(hw, ets_offset + 1 + i, &ets_sensor)) {
  3313. hw_err(hw, "eeprom read at offset %d failed\n",
  3314. ets_offset + 1 + i);
  3315. continue;
  3316. }
  3317. sensor_index = ((ets_sensor & IXGBE_ETS_DATA_INDEX_MASK) >>
  3318. IXGBE_ETS_DATA_INDEX_SHIFT);
  3319. sensor_location = ((ets_sensor & IXGBE_ETS_DATA_LOC_MASK) >>
  3320. IXGBE_ETS_DATA_LOC_SHIFT);
  3321. therm_limit = ets_sensor & IXGBE_ETS_DATA_HTHRESH_MASK;
  3322. hw->phy.ops.write_i2c_byte(hw,
  3323. ixgbe_emc_therm_limit[sensor_index],
  3324. IXGBE_I2C_THERMAL_SENSOR_ADDR, therm_limit);
  3325. if (sensor_location == 0)
  3326. continue;
  3327. data->sensor[i].location = sensor_location;
  3328. data->sensor[i].caution_thresh = therm_limit;
  3329. data->sensor[i].max_op_thresh = therm_limit - low_thresh_delta;
  3330. }
  3331. return 0;
  3332. }
  3333. void ixgbe_disable_rx_generic(struct ixgbe_hw *hw)
  3334. {
  3335. u32 rxctrl;
  3336. rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
  3337. if (rxctrl & IXGBE_RXCTRL_RXEN) {
  3338. if (hw->mac.type != ixgbe_mac_82598EB) {
  3339. u32 pfdtxgswc;
  3340. pfdtxgswc = IXGBE_READ_REG(hw, IXGBE_PFDTXGSWC);
  3341. if (pfdtxgswc & IXGBE_PFDTXGSWC_VT_LBEN) {
  3342. pfdtxgswc &= ~IXGBE_PFDTXGSWC_VT_LBEN;
  3343. IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, pfdtxgswc);
  3344. hw->mac.set_lben = true;
  3345. } else {
  3346. hw->mac.set_lben = false;
  3347. }
  3348. }
  3349. rxctrl &= ~IXGBE_RXCTRL_RXEN;
  3350. IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl);
  3351. }
  3352. }
  3353. void ixgbe_enable_rx_generic(struct ixgbe_hw *hw)
  3354. {
  3355. u32 rxctrl;
  3356. rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
  3357. IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, (rxctrl | IXGBE_RXCTRL_RXEN));
  3358. if (hw->mac.type != ixgbe_mac_82598EB) {
  3359. if (hw->mac.set_lben) {
  3360. u32 pfdtxgswc;
  3361. pfdtxgswc = IXGBE_READ_REG(hw, IXGBE_PFDTXGSWC);
  3362. pfdtxgswc |= IXGBE_PFDTXGSWC_VT_LBEN;
  3363. IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, pfdtxgswc);
  3364. hw->mac.set_lben = false;
  3365. }
  3366. }
  3367. }
  3368. /** ixgbe_mng_present - returns true when management capability is present
  3369. * @hw: pointer to hardware structure
  3370. **/
  3371. bool ixgbe_mng_present(struct ixgbe_hw *hw)
  3372. {
  3373. u32 fwsm;
  3374. if (hw->mac.type < ixgbe_mac_82599EB)
  3375. return false;
  3376. fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM(hw));
  3377. fwsm &= IXGBE_FWSM_MODE_MASK;
  3378. return fwsm == IXGBE_FWSM_FW_MODE_PT;
  3379. }
  3380. /**
  3381. * ixgbe_setup_mac_link_multispeed_fiber - Set MAC link speed
  3382. * @hw: pointer to hardware structure
  3383. * @speed: new link speed
  3384. * @autoneg_wait_to_complete: true when waiting for completion is needed
  3385. *
  3386. * Set the link speed in the MAC and/or PHY register and restarts link.
  3387. */
  3388. s32 ixgbe_setup_mac_link_multispeed_fiber(struct ixgbe_hw *hw,
  3389. ixgbe_link_speed speed,
  3390. bool autoneg_wait_to_complete)
  3391. {
  3392. ixgbe_link_speed link_speed = IXGBE_LINK_SPEED_UNKNOWN;
  3393. ixgbe_link_speed highest_link_speed = IXGBE_LINK_SPEED_UNKNOWN;
  3394. s32 status = 0;
  3395. u32 speedcnt = 0;
  3396. u32 i = 0;
  3397. bool autoneg, link_up = false;
  3398. /* Mask off requested but non-supported speeds */
  3399. status = hw->mac.ops.get_link_capabilities(hw, &link_speed, &autoneg);
  3400. if (status)
  3401. return status;
  3402. speed &= link_speed;
  3403. /* Try each speed one by one, highest priority first. We do this in
  3404. * software because 10Gb fiber doesn't support speed autonegotiation.
  3405. */
  3406. if (speed & IXGBE_LINK_SPEED_10GB_FULL) {
  3407. speedcnt++;
  3408. highest_link_speed = IXGBE_LINK_SPEED_10GB_FULL;
  3409. /* If we already have link at this speed, just jump out */
  3410. status = hw->mac.ops.check_link(hw, &link_speed, &link_up,
  3411. false);
  3412. if (status)
  3413. return status;
  3414. if (link_speed == IXGBE_LINK_SPEED_10GB_FULL && link_up)
  3415. goto out;
  3416. /* Set the module link speed */
  3417. switch (hw->phy.media_type) {
  3418. case ixgbe_media_type_fiber:
  3419. hw->mac.ops.set_rate_select_speed(hw,
  3420. IXGBE_LINK_SPEED_10GB_FULL);
  3421. break;
  3422. case ixgbe_media_type_fiber_qsfp:
  3423. /* QSFP module automatically detects MAC link speed */
  3424. break;
  3425. default:
  3426. hw_dbg(hw, "Unexpected media type\n");
  3427. break;
  3428. }
  3429. /* Allow module to change analog characteristics (1G->10G) */
  3430. msleep(40);
  3431. status = hw->mac.ops.setup_mac_link(hw,
  3432. IXGBE_LINK_SPEED_10GB_FULL,
  3433. autoneg_wait_to_complete);
  3434. if (status)
  3435. return status;
  3436. /* Flap the Tx laser if it has not already been done */
  3437. if (hw->mac.ops.flap_tx_laser)
  3438. hw->mac.ops.flap_tx_laser(hw);
  3439. /* Wait for the controller to acquire link. Per IEEE 802.3ap,
  3440. * Section 73.10.2, we may have to wait up to 500ms if KR is
  3441. * attempted. 82599 uses the same timing for 10g SFI.
  3442. */
  3443. for (i = 0; i < 5; i++) {
  3444. /* Wait for the link partner to also set speed */
  3445. msleep(100);
  3446. /* If we have link, just jump out */
  3447. status = hw->mac.ops.check_link(hw, &link_speed,
  3448. &link_up, false);
  3449. if (status)
  3450. return status;
  3451. if (link_up)
  3452. goto out;
  3453. }
  3454. }
  3455. if (speed & IXGBE_LINK_SPEED_1GB_FULL) {
  3456. speedcnt++;
  3457. if (highest_link_speed == IXGBE_LINK_SPEED_UNKNOWN)
  3458. highest_link_speed = IXGBE_LINK_SPEED_1GB_FULL;
  3459. /* If we already have link at this speed, just jump out */
  3460. status = hw->mac.ops.check_link(hw, &link_speed, &link_up,
  3461. false);
  3462. if (status)
  3463. return status;
  3464. if (link_speed == IXGBE_LINK_SPEED_1GB_FULL && link_up)
  3465. goto out;
  3466. /* Set the module link speed */
  3467. switch (hw->phy.media_type) {
  3468. case ixgbe_media_type_fiber:
  3469. hw->mac.ops.set_rate_select_speed(hw,
  3470. IXGBE_LINK_SPEED_1GB_FULL);
  3471. break;
  3472. case ixgbe_media_type_fiber_qsfp:
  3473. /* QSFP module automatically detects link speed */
  3474. break;
  3475. default:
  3476. hw_dbg(hw, "Unexpected media type\n");
  3477. break;
  3478. }
  3479. /* Allow module to change analog characteristics (10G->1G) */
  3480. msleep(40);
  3481. status = hw->mac.ops.setup_mac_link(hw,
  3482. IXGBE_LINK_SPEED_1GB_FULL,
  3483. autoneg_wait_to_complete);
  3484. if (status)
  3485. return status;
  3486. /* Flap the Tx laser if it has not already been done */
  3487. if (hw->mac.ops.flap_tx_laser)
  3488. hw->mac.ops.flap_tx_laser(hw);
  3489. /* Wait for the link partner to also set speed */
  3490. msleep(100);
  3491. /* If we have link, just jump out */
  3492. status = hw->mac.ops.check_link(hw, &link_speed, &link_up,
  3493. false);
  3494. if (status)
  3495. return status;
  3496. if (link_up)
  3497. goto out;
  3498. }
  3499. /* We didn't get link. Configure back to the highest speed we tried,
  3500. * (if there was more than one). We call ourselves back with just the
  3501. * single highest speed that the user requested.
  3502. */
  3503. if (speedcnt > 1)
  3504. status = ixgbe_setup_mac_link_multispeed_fiber(hw,
  3505. highest_link_speed,
  3506. autoneg_wait_to_complete);
  3507. out:
  3508. /* Set autoneg_advertised value based on input link speed */
  3509. hw->phy.autoneg_advertised = 0;
  3510. if (speed & IXGBE_LINK_SPEED_10GB_FULL)
  3511. hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_10GB_FULL;
  3512. if (speed & IXGBE_LINK_SPEED_1GB_FULL)
  3513. hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_1GB_FULL;
  3514. return status;
  3515. }
  3516. /**
  3517. * ixgbe_set_soft_rate_select_speed - Set module link speed
  3518. * @hw: pointer to hardware structure
  3519. * @speed: link speed to set
  3520. *
  3521. * Set module link speed via the soft rate select.
  3522. */
  3523. void ixgbe_set_soft_rate_select_speed(struct ixgbe_hw *hw,
  3524. ixgbe_link_speed speed)
  3525. {
  3526. s32 status;
  3527. u8 rs, eeprom_data;
  3528. switch (speed) {
  3529. case IXGBE_LINK_SPEED_10GB_FULL:
  3530. /* one bit mask same as setting on */
  3531. rs = IXGBE_SFF_SOFT_RS_SELECT_10G;
  3532. break;
  3533. case IXGBE_LINK_SPEED_1GB_FULL:
  3534. rs = IXGBE_SFF_SOFT_RS_SELECT_1G;
  3535. break;
  3536. default:
  3537. hw_dbg(hw, "Invalid fixed module speed\n");
  3538. return;
  3539. }
  3540. /* Set RS0 */
  3541. status = hw->phy.ops.read_i2c_byte(hw, IXGBE_SFF_SFF_8472_OSCB,
  3542. IXGBE_I2C_EEPROM_DEV_ADDR2,
  3543. &eeprom_data);
  3544. if (status) {
  3545. hw_dbg(hw, "Failed to read Rx Rate Select RS0\n");
  3546. return;
  3547. }
  3548. eeprom_data = (eeprom_data & ~IXGBE_SFF_SOFT_RS_SELECT_MASK) | rs;
  3549. status = hw->phy.ops.write_i2c_byte(hw, IXGBE_SFF_SFF_8472_OSCB,
  3550. IXGBE_I2C_EEPROM_DEV_ADDR2,
  3551. eeprom_data);
  3552. if (status) {
  3553. hw_dbg(hw, "Failed to write Rx Rate Select RS0\n");
  3554. return;
  3555. }
  3556. }