ixgbe_common.c 120 KB

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