csio_hw.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398
  1. /*
  2. * This file is part of the Chelsio FCoE driver for Linux.
  3. *
  4. * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/pci.h>
  35. #include <linux/pci_regs.h>
  36. #include <linux/firmware.h>
  37. #include <linux/stddef.h>
  38. #include <linux/delay.h>
  39. #include <linux/string.h>
  40. #include <linux/compiler.h>
  41. #include <linux/jiffies.h>
  42. #include <linux/kernel.h>
  43. #include <linux/log2.h>
  44. #include "csio_hw.h"
  45. #include "csio_lnode.h"
  46. #include "csio_rnode.h"
  47. int csio_force_master;
  48. int csio_dbg_level = 0xFEFF;
  49. unsigned int csio_port_mask = 0xf;
  50. /* Default FW event queue entries. */
  51. static uint32_t csio_evtq_sz = CSIO_EVTQ_SIZE;
  52. /* Default MSI param level */
  53. int csio_msi = 2;
  54. /* FCoE function instances */
  55. static int dev_num;
  56. /* FCoE Adapter types & its description */
  57. static const struct csio_adap_desc csio_fcoe_adapters[] = {
  58. {"T440-Dbg 10G", "Chelsio T440-Dbg 10G [FCoE]"},
  59. {"T420-CR 10G", "Chelsio T420-CR 10G [FCoE]"},
  60. {"T422-CR 10G/1G", "Chelsio T422-CR 10G/1G [FCoE]"},
  61. {"T440-CR 10G", "Chelsio T440-CR 10G [FCoE]"},
  62. {"T420-BCH 10G", "Chelsio T420-BCH 10G [FCoE]"},
  63. {"T440-BCH 10G", "Chelsio T440-BCH 10G [FCoE]"},
  64. {"T440-CH 10G", "Chelsio T440-CH 10G [FCoE]"},
  65. {"T420-SO 10G", "Chelsio T420-SO 10G [FCoE]"},
  66. {"T420-CX4 10G", "Chelsio T420-CX4 10G [FCoE]"},
  67. {"T420-BT 10G", "Chelsio T420-BT 10G [FCoE]"},
  68. {"T404-BT 1G", "Chelsio T404-BT 1G [FCoE]"},
  69. {"B420-SR 10G", "Chelsio B420-SR 10G [FCoE]"},
  70. {"B404-BT 1G", "Chelsio B404-BT 1G [FCoE]"},
  71. {"T480-CR 10G", "Chelsio T480-CR 10G [FCoE]"},
  72. {"T440-LP-CR 10G", "Chelsio T440-LP-CR 10G [FCoE]"},
  73. {"T4 FPGA", "Chelsio T4 FPGA [FCoE]"}
  74. };
  75. static void csio_mgmtm_cleanup(struct csio_mgmtm *);
  76. static void csio_hw_mbm_cleanup(struct csio_hw *);
  77. /* State machine forward declarations */
  78. static void csio_hws_uninit(struct csio_hw *, enum csio_hw_ev);
  79. static void csio_hws_configuring(struct csio_hw *, enum csio_hw_ev);
  80. static void csio_hws_initializing(struct csio_hw *, enum csio_hw_ev);
  81. static void csio_hws_ready(struct csio_hw *, enum csio_hw_ev);
  82. static void csio_hws_quiescing(struct csio_hw *, enum csio_hw_ev);
  83. static void csio_hws_quiesced(struct csio_hw *, enum csio_hw_ev);
  84. static void csio_hws_resetting(struct csio_hw *, enum csio_hw_ev);
  85. static void csio_hws_removing(struct csio_hw *, enum csio_hw_ev);
  86. static void csio_hws_pcierr(struct csio_hw *, enum csio_hw_ev);
  87. static void csio_hw_initialize(struct csio_hw *hw);
  88. static void csio_evtq_stop(struct csio_hw *hw);
  89. static void csio_evtq_start(struct csio_hw *hw);
  90. int csio_is_hw_ready(struct csio_hw *hw)
  91. {
  92. return csio_match_state(hw, csio_hws_ready);
  93. }
  94. int csio_is_hw_removing(struct csio_hw *hw)
  95. {
  96. return csio_match_state(hw, csio_hws_removing);
  97. }
  98. /*
  99. * csio_hw_wait_op_done_val - wait until an operation is completed
  100. * @hw: the HW module
  101. * @reg: the register to check for completion
  102. * @mask: a single-bit field within @reg that indicates completion
  103. * @polarity: the value of the field when the operation is completed
  104. * @attempts: number of check iterations
  105. * @delay: delay in usecs between iterations
  106. * @valp: where to store the value of the register at completion time
  107. *
  108. * Wait until an operation is completed by checking a bit in a register
  109. * up to @attempts times. If @valp is not NULL the value of the register
  110. * at the time it indicated completion is stored there. Returns 0 if the
  111. * operation completes and -EAGAIN otherwise.
  112. */
  113. static int
  114. csio_hw_wait_op_done_val(struct csio_hw *hw, int reg, uint32_t mask,
  115. int polarity, int attempts, int delay, uint32_t *valp)
  116. {
  117. uint32_t val;
  118. while (1) {
  119. val = csio_rd_reg32(hw, reg);
  120. if (!!(val & mask) == polarity) {
  121. if (valp)
  122. *valp = val;
  123. return 0;
  124. }
  125. if (--attempts == 0)
  126. return -EAGAIN;
  127. if (delay)
  128. udelay(delay);
  129. }
  130. }
  131. void
  132. csio_set_reg_field(struct csio_hw *hw, uint32_t reg, uint32_t mask,
  133. uint32_t value)
  134. {
  135. uint32_t val = csio_rd_reg32(hw, reg) & ~mask;
  136. csio_wr_reg32(hw, val | value, reg);
  137. /* Flush */
  138. csio_rd_reg32(hw, reg);
  139. }
  140. /*
  141. * csio_hw_mc_read - read from MC through backdoor accesses
  142. * @hw: the hw module
  143. * @addr: address of first byte requested
  144. * @data: 64 bytes of data containing the requested address
  145. * @ecc: where to store the corresponding 64-bit ECC word
  146. *
  147. * Read 64 bytes of data from MC starting at a 64-byte-aligned address
  148. * that covers the requested address @addr. If @parity is not %NULL it
  149. * is assigned the 64-bit ECC word for the read data.
  150. */
  151. int
  152. csio_hw_mc_read(struct csio_hw *hw, uint32_t addr, __be32 *data,
  153. uint64_t *ecc)
  154. {
  155. int i;
  156. if (csio_rd_reg32(hw, MC_BIST_CMD) & START_BIST)
  157. return -EBUSY;
  158. csio_wr_reg32(hw, addr & ~0x3fU, MC_BIST_CMD_ADDR);
  159. csio_wr_reg32(hw, 64, MC_BIST_CMD_LEN);
  160. csio_wr_reg32(hw, 0xc, MC_BIST_DATA_PATTERN);
  161. csio_wr_reg32(hw, BIST_OPCODE(1) | START_BIST | BIST_CMD_GAP(1),
  162. MC_BIST_CMD);
  163. i = csio_hw_wait_op_done_val(hw, MC_BIST_CMD, START_BIST,
  164. 0, 10, 1, NULL);
  165. if (i)
  166. return i;
  167. #define MC_DATA(i) MC_BIST_STATUS_REG(MC_BIST_STATUS_RDATA, i)
  168. for (i = 15; i >= 0; i--)
  169. *data++ = htonl(csio_rd_reg32(hw, MC_DATA(i)));
  170. if (ecc)
  171. *ecc = csio_rd_reg64(hw, MC_DATA(16));
  172. #undef MC_DATA
  173. return 0;
  174. }
  175. /*
  176. * csio_hw_edc_read - read from EDC through backdoor accesses
  177. * @hw: the hw module
  178. * @idx: which EDC to access
  179. * @addr: address of first byte requested
  180. * @data: 64 bytes of data containing the requested address
  181. * @ecc: where to store the corresponding 64-bit ECC word
  182. *
  183. * Read 64 bytes of data from EDC starting at a 64-byte-aligned address
  184. * that covers the requested address @addr. If @parity is not %NULL it
  185. * is assigned the 64-bit ECC word for the read data.
  186. */
  187. int
  188. csio_hw_edc_read(struct csio_hw *hw, int idx, uint32_t addr, __be32 *data,
  189. uint64_t *ecc)
  190. {
  191. int i;
  192. idx *= EDC_STRIDE;
  193. if (csio_rd_reg32(hw, EDC_BIST_CMD + idx) & START_BIST)
  194. return -EBUSY;
  195. csio_wr_reg32(hw, addr & ~0x3fU, EDC_BIST_CMD_ADDR + idx);
  196. csio_wr_reg32(hw, 64, EDC_BIST_CMD_LEN + idx);
  197. csio_wr_reg32(hw, 0xc, EDC_BIST_DATA_PATTERN + idx);
  198. csio_wr_reg32(hw, BIST_OPCODE(1) | BIST_CMD_GAP(1) | START_BIST,
  199. EDC_BIST_CMD + idx);
  200. i = csio_hw_wait_op_done_val(hw, EDC_BIST_CMD + idx, START_BIST,
  201. 0, 10, 1, NULL);
  202. if (i)
  203. return i;
  204. #define EDC_DATA(i) (EDC_BIST_STATUS_REG(EDC_BIST_STATUS_RDATA, i) + idx)
  205. for (i = 15; i >= 0; i--)
  206. *data++ = htonl(csio_rd_reg32(hw, EDC_DATA(i)));
  207. if (ecc)
  208. *ecc = csio_rd_reg64(hw, EDC_DATA(16));
  209. #undef EDC_DATA
  210. return 0;
  211. }
  212. /*
  213. * csio_mem_win_rw - read/write memory through PCIE memory window
  214. * @hw: the adapter
  215. * @addr: address of first byte requested
  216. * @data: MEMWIN0_APERTURE bytes of data containing the requested address
  217. * @dir: direction of transfer 1 => read, 0 => write
  218. *
  219. * Read/write MEMWIN0_APERTURE bytes of data from MC starting at a
  220. * MEMWIN0_APERTURE-byte-aligned address that covers the requested
  221. * address @addr.
  222. */
  223. static int
  224. csio_mem_win_rw(struct csio_hw *hw, u32 addr, u32 *data, int dir)
  225. {
  226. int i;
  227. /*
  228. * Setup offset into PCIE memory window. Address must be a
  229. * MEMWIN0_APERTURE-byte-aligned address. (Read back MA register to
  230. * ensure that changes propagate before we attempt to use the new
  231. * values.)
  232. */
  233. csio_wr_reg32(hw, addr & ~(MEMWIN0_APERTURE - 1),
  234. PCIE_MEM_ACCESS_OFFSET);
  235. csio_rd_reg32(hw, PCIE_MEM_ACCESS_OFFSET);
  236. /* Collecting data 4 bytes at a time upto MEMWIN0_APERTURE */
  237. for (i = 0; i < MEMWIN0_APERTURE; i = i + sizeof(__be32)) {
  238. if (dir)
  239. *data++ = csio_rd_reg32(hw, (MEMWIN0_BASE + i));
  240. else
  241. csio_wr_reg32(hw, *data++, (MEMWIN0_BASE + i));
  242. }
  243. return 0;
  244. }
  245. /*
  246. * csio_memory_rw - read/write EDC 0, EDC 1 or MC via PCIE memory window
  247. * @hw: the csio_hw
  248. * @mtype: memory type: MEM_EDC0, MEM_EDC1 or MEM_MC
  249. * @addr: address within indicated memory type
  250. * @len: amount of memory to transfer
  251. * @buf: host memory buffer
  252. * @dir: direction of transfer 1 => read, 0 => write
  253. *
  254. * Reads/writes an [almost] arbitrary memory region in the firmware: the
  255. * firmware memory address, length and host buffer must be aligned on
  256. * 32-bit boudaries. The memory is transferred as a raw byte sequence
  257. * from/to the firmware's memory. If this memory contains data
  258. * structures which contain multi-byte integers, it's the callers
  259. * responsibility to perform appropriate byte order conversions.
  260. */
  261. static int
  262. csio_memory_rw(struct csio_hw *hw, int mtype, u32 addr, u32 len,
  263. uint32_t *buf, int dir)
  264. {
  265. uint32_t pos, start, end, offset, memoffset;
  266. int ret;
  267. uint32_t *data;
  268. /*
  269. * Argument sanity checks ...
  270. */
  271. if ((addr & 0x3) || (len & 0x3))
  272. return -EINVAL;
  273. data = kzalloc(MEMWIN0_APERTURE, GFP_KERNEL);
  274. if (!data)
  275. return -ENOMEM;
  276. /* Offset into the region of memory which is being accessed
  277. * MEM_EDC0 = 0
  278. * MEM_EDC1 = 1
  279. * MEM_MC = 2
  280. */
  281. memoffset = (mtype * (5 * 1024 * 1024));
  282. /* Determine the PCIE_MEM_ACCESS_OFFSET */
  283. addr = addr + memoffset;
  284. /*
  285. * The underlaying EDC/MC read routines read MEMWIN0_APERTURE bytes
  286. * at a time so we need to round down the start and round up the end.
  287. * We'll start copying out of the first line at (addr - start) a word
  288. * at a time.
  289. */
  290. start = addr & ~(MEMWIN0_APERTURE-1);
  291. end = (addr + len + MEMWIN0_APERTURE-1) & ~(MEMWIN0_APERTURE-1);
  292. offset = (addr - start)/sizeof(__be32);
  293. for (pos = start; pos < end; pos += MEMWIN0_APERTURE, offset = 0) {
  294. /*
  295. * If we're writing, copy the data from the caller's memory
  296. * buffer
  297. */
  298. if (!dir) {
  299. /*
  300. * If we're doing a partial write, then we need to do
  301. * a read-modify-write ...
  302. */
  303. if (offset || len < MEMWIN0_APERTURE) {
  304. ret = csio_mem_win_rw(hw, pos, data, 1);
  305. if (ret) {
  306. kfree(data);
  307. return ret;
  308. }
  309. }
  310. while (offset < (MEMWIN0_APERTURE/sizeof(__be32)) &&
  311. len > 0) {
  312. data[offset++] = *buf++;
  313. len -= sizeof(__be32);
  314. }
  315. }
  316. /*
  317. * Transfer a block of memory and bail if there's an error.
  318. */
  319. ret = csio_mem_win_rw(hw, pos, data, dir);
  320. if (ret) {
  321. kfree(data);
  322. return ret;
  323. }
  324. /*
  325. * If we're reading, copy the data into the caller's memory
  326. * buffer.
  327. */
  328. if (dir)
  329. while (offset < (MEMWIN0_APERTURE/sizeof(__be32)) &&
  330. len > 0) {
  331. *buf++ = data[offset++];
  332. len -= sizeof(__be32);
  333. }
  334. }
  335. kfree(data);
  336. return 0;
  337. }
  338. static int
  339. csio_memory_write(struct csio_hw *hw, int mtype, u32 addr, u32 len, u32 *buf)
  340. {
  341. return csio_memory_rw(hw, mtype, addr, len, buf, 0);
  342. }
  343. /*
  344. * EEPROM reads take a few tens of us while writes can take a bit over 5 ms.
  345. */
  346. #define EEPROM_MAX_RD_POLL 40
  347. #define EEPROM_MAX_WR_POLL 6
  348. #define EEPROM_STAT_ADDR 0x7bfc
  349. #define VPD_BASE 0x400
  350. #define VPD_BASE_OLD 0
  351. #define VPD_LEN 512
  352. #define VPD_INFO_FLD_HDR_SIZE 3
  353. /*
  354. * csio_hw_seeprom_read - read a serial EEPROM location
  355. * @hw: hw to read
  356. * @addr: EEPROM virtual address
  357. * @data: where to store the read data
  358. *
  359. * Read a 32-bit word from a location in serial EEPROM using the card's PCI
  360. * VPD capability. Note that this function must be called with a virtual
  361. * address.
  362. */
  363. static int
  364. csio_hw_seeprom_read(struct csio_hw *hw, uint32_t addr, uint32_t *data)
  365. {
  366. uint16_t val = 0;
  367. int attempts = EEPROM_MAX_RD_POLL;
  368. uint32_t base = hw->params.pci.vpd_cap_addr;
  369. if (addr >= EEPROMVSIZE || (addr & 3))
  370. return -EINVAL;
  371. pci_write_config_word(hw->pdev, base + PCI_VPD_ADDR, (uint16_t)addr);
  372. do {
  373. udelay(10);
  374. pci_read_config_word(hw->pdev, base + PCI_VPD_ADDR, &val);
  375. } while (!(val & PCI_VPD_ADDR_F) && --attempts);
  376. if (!(val & PCI_VPD_ADDR_F)) {
  377. csio_err(hw, "reading EEPROM address 0x%x failed\n", addr);
  378. return -EINVAL;
  379. }
  380. pci_read_config_dword(hw->pdev, base + PCI_VPD_DATA, data);
  381. *data = le32_to_cpu(*data);
  382. return 0;
  383. }
  384. /*
  385. * Partial EEPROM Vital Product Data structure. Includes only the ID and
  386. * VPD-R sections.
  387. */
  388. struct t4_vpd_hdr {
  389. u8 id_tag;
  390. u8 id_len[2];
  391. u8 id_data[ID_LEN];
  392. u8 vpdr_tag;
  393. u8 vpdr_len[2];
  394. };
  395. /*
  396. * csio_hw_get_vpd_keyword_val - Locates an information field keyword in
  397. * the VPD
  398. * @v: Pointer to buffered vpd data structure
  399. * @kw: The keyword to search for
  400. *
  401. * Returns the value of the information field keyword or
  402. * -EINVAL otherwise.
  403. */
  404. static int
  405. csio_hw_get_vpd_keyword_val(const struct t4_vpd_hdr *v, const char *kw)
  406. {
  407. int32_t i;
  408. int32_t offset , len;
  409. const uint8_t *buf = &v->id_tag;
  410. const uint8_t *vpdr_len = &v->vpdr_tag;
  411. offset = sizeof(struct t4_vpd_hdr);
  412. len = (uint16_t)vpdr_len[1] + ((uint16_t)vpdr_len[2] << 8);
  413. if (len + sizeof(struct t4_vpd_hdr) > VPD_LEN)
  414. return -EINVAL;
  415. for (i = offset; (i + VPD_INFO_FLD_HDR_SIZE) <= (offset + len);) {
  416. if (memcmp(buf + i , kw, 2) == 0) {
  417. i += VPD_INFO_FLD_HDR_SIZE;
  418. return i;
  419. }
  420. i += VPD_INFO_FLD_HDR_SIZE + buf[i+2];
  421. }
  422. return -EINVAL;
  423. }
  424. static int
  425. csio_pci_capability(struct pci_dev *pdev, int cap, int *pos)
  426. {
  427. *pos = pci_find_capability(pdev, cap);
  428. if (*pos)
  429. return 0;
  430. return -1;
  431. }
  432. /*
  433. * csio_hw_get_vpd_params - read VPD parameters from VPD EEPROM
  434. * @hw: HW module
  435. * @p: where to store the parameters
  436. *
  437. * Reads card parameters stored in VPD EEPROM.
  438. */
  439. static int
  440. csio_hw_get_vpd_params(struct csio_hw *hw, struct csio_vpd *p)
  441. {
  442. int i, ret, ec, sn, addr;
  443. uint8_t *vpd, csum;
  444. const struct t4_vpd_hdr *v;
  445. /* To get around compilation warning from strstrip */
  446. char *s;
  447. if (csio_is_valid_vpd(hw))
  448. return 0;
  449. ret = csio_pci_capability(hw->pdev, PCI_CAP_ID_VPD,
  450. &hw->params.pci.vpd_cap_addr);
  451. if (ret)
  452. return -EINVAL;
  453. vpd = kzalloc(VPD_LEN, GFP_ATOMIC);
  454. if (vpd == NULL)
  455. return -ENOMEM;
  456. /*
  457. * Card information normally starts at VPD_BASE but early cards had
  458. * it at 0.
  459. */
  460. ret = csio_hw_seeprom_read(hw, VPD_BASE, (uint32_t *)(vpd));
  461. addr = *vpd == 0x82 ? VPD_BASE : VPD_BASE_OLD;
  462. for (i = 0; i < VPD_LEN; i += 4) {
  463. ret = csio_hw_seeprom_read(hw, addr + i, (uint32_t *)(vpd + i));
  464. if (ret) {
  465. kfree(vpd);
  466. return ret;
  467. }
  468. }
  469. /* Reset the VPD flag! */
  470. hw->flags &= (~CSIO_HWF_VPD_VALID);
  471. v = (const struct t4_vpd_hdr *)vpd;
  472. #define FIND_VPD_KW(var, name) do { \
  473. var = csio_hw_get_vpd_keyword_val(v, name); \
  474. if (var < 0) { \
  475. csio_err(hw, "missing VPD keyword " name "\n"); \
  476. kfree(vpd); \
  477. return -EINVAL; \
  478. } \
  479. } while (0)
  480. FIND_VPD_KW(i, "RV");
  481. for (csum = 0; i >= 0; i--)
  482. csum += vpd[i];
  483. if (csum) {
  484. csio_err(hw, "corrupted VPD EEPROM, actual csum %u\n", csum);
  485. kfree(vpd);
  486. return -EINVAL;
  487. }
  488. FIND_VPD_KW(ec, "EC");
  489. FIND_VPD_KW(sn, "SN");
  490. #undef FIND_VPD_KW
  491. memcpy(p->id, v->id_data, ID_LEN);
  492. s = strstrip(p->id);
  493. memcpy(p->ec, vpd + ec, EC_LEN);
  494. s = strstrip(p->ec);
  495. i = vpd[sn - VPD_INFO_FLD_HDR_SIZE + 2];
  496. memcpy(p->sn, vpd + sn, min(i, SERNUM_LEN));
  497. s = strstrip(p->sn);
  498. csio_valid_vpd_copied(hw);
  499. kfree(vpd);
  500. return 0;
  501. }
  502. /*
  503. * csio_hw_sf1_read - read data from the serial flash
  504. * @hw: the HW module
  505. * @byte_cnt: number of bytes to read
  506. * @cont: whether another operation will be chained
  507. * @lock: whether to lock SF for PL access only
  508. * @valp: where to store the read data
  509. *
  510. * Reads up to 4 bytes of data from the serial flash. The location of
  511. * the read needs to be specified prior to calling this by issuing the
  512. * appropriate commands to the serial flash.
  513. */
  514. static int
  515. csio_hw_sf1_read(struct csio_hw *hw, uint32_t byte_cnt, int32_t cont,
  516. int32_t lock, uint32_t *valp)
  517. {
  518. int ret;
  519. if (!byte_cnt || byte_cnt > 4)
  520. return -EINVAL;
  521. if (csio_rd_reg32(hw, SF_OP) & SF_BUSY)
  522. return -EBUSY;
  523. cont = cont ? SF_CONT : 0;
  524. lock = lock ? SF_LOCK : 0;
  525. csio_wr_reg32(hw, lock | cont | BYTECNT(byte_cnt - 1), SF_OP);
  526. ret = csio_hw_wait_op_done_val(hw, SF_OP, SF_BUSY, 0, SF_ATTEMPTS,
  527. 10, NULL);
  528. if (!ret)
  529. *valp = csio_rd_reg32(hw, SF_DATA);
  530. return ret;
  531. }
  532. /*
  533. * csio_hw_sf1_write - write data to the serial flash
  534. * @hw: the HW module
  535. * @byte_cnt: number of bytes to write
  536. * @cont: whether another operation will be chained
  537. * @lock: whether to lock SF for PL access only
  538. * @val: value to write
  539. *
  540. * Writes up to 4 bytes of data to the serial flash. The location of
  541. * the write needs to be specified prior to calling this by issuing the
  542. * appropriate commands to the serial flash.
  543. */
  544. static int
  545. csio_hw_sf1_write(struct csio_hw *hw, uint32_t byte_cnt, uint32_t cont,
  546. int32_t lock, uint32_t val)
  547. {
  548. if (!byte_cnt || byte_cnt > 4)
  549. return -EINVAL;
  550. if (csio_rd_reg32(hw, SF_OP) & SF_BUSY)
  551. return -EBUSY;
  552. cont = cont ? SF_CONT : 0;
  553. lock = lock ? SF_LOCK : 0;
  554. csio_wr_reg32(hw, val, SF_DATA);
  555. csio_wr_reg32(hw, cont | BYTECNT(byte_cnt - 1) | OP_WR | lock, SF_OP);
  556. return csio_hw_wait_op_done_val(hw, SF_OP, SF_BUSY, 0, SF_ATTEMPTS,
  557. 10, NULL);
  558. }
  559. /*
  560. * csio_hw_flash_wait_op - wait for a flash operation to complete
  561. * @hw: the HW module
  562. * @attempts: max number of polls of the status register
  563. * @delay: delay between polls in ms
  564. *
  565. * Wait for a flash operation to complete by polling the status register.
  566. */
  567. static int
  568. csio_hw_flash_wait_op(struct csio_hw *hw, int32_t attempts, int32_t delay)
  569. {
  570. int ret;
  571. uint32_t status;
  572. while (1) {
  573. ret = csio_hw_sf1_write(hw, 1, 1, 1, SF_RD_STATUS);
  574. if (ret != 0)
  575. return ret;
  576. ret = csio_hw_sf1_read(hw, 1, 0, 1, &status);
  577. if (ret != 0)
  578. return ret;
  579. if (!(status & 1))
  580. return 0;
  581. if (--attempts == 0)
  582. return -EAGAIN;
  583. if (delay)
  584. msleep(delay);
  585. }
  586. }
  587. /*
  588. * csio_hw_read_flash - read words from serial flash
  589. * @hw: the HW module
  590. * @addr: the start address for the read
  591. * @nwords: how many 32-bit words to read
  592. * @data: where to store the read data
  593. * @byte_oriented: whether to store data as bytes or as words
  594. *
  595. * Read the specified number of 32-bit words from the serial flash.
  596. * If @byte_oriented is set the read data is stored as a byte array
  597. * (i.e., big-endian), otherwise as 32-bit words in the platform's
  598. * natural endianess.
  599. */
  600. static int
  601. csio_hw_read_flash(struct csio_hw *hw, uint32_t addr, uint32_t nwords,
  602. uint32_t *data, int32_t byte_oriented)
  603. {
  604. int ret;
  605. if (addr + nwords * sizeof(uint32_t) > hw->params.sf_size || (addr & 3))
  606. return -EINVAL;
  607. addr = swab32(addr) | SF_RD_DATA_FAST;
  608. ret = csio_hw_sf1_write(hw, 4, 1, 0, addr);
  609. if (ret != 0)
  610. return ret;
  611. ret = csio_hw_sf1_read(hw, 1, 1, 0, data);
  612. if (ret != 0)
  613. return ret;
  614. for ( ; nwords; nwords--, data++) {
  615. ret = csio_hw_sf1_read(hw, 4, nwords > 1, nwords == 1, data);
  616. if (nwords == 1)
  617. csio_wr_reg32(hw, 0, SF_OP); /* unlock SF */
  618. if (ret)
  619. return ret;
  620. if (byte_oriented)
  621. *data = htonl(*data);
  622. }
  623. return 0;
  624. }
  625. /*
  626. * csio_hw_write_flash - write up to a page of data to the serial flash
  627. * @hw: the hw
  628. * @addr: the start address to write
  629. * @n: length of data to write in bytes
  630. * @data: the data to write
  631. *
  632. * Writes up to a page of data (256 bytes) to the serial flash starting
  633. * at the given address. All the data must be written to the same page.
  634. */
  635. static int
  636. csio_hw_write_flash(struct csio_hw *hw, uint32_t addr,
  637. uint32_t n, const uint8_t *data)
  638. {
  639. int ret = -EINVAL;
  640. uint32_t buf[64];
  641. uint32_t i, c, left, val, offset = addr & 0xff;
  642. if (addr >= hw->params.sf_size || offset + n > SF_PAGE_SIZE)
  643. return -EINVAL;
  644. val = swab32(addr) | SF_PROG_PAGE;
  645. ret = csio_hw_sf1_write(hw, 1, 0, 1, SF_WR_ENABLE);
  646. if (ret != 0)
  647. goto unlock;
  648. ret = csio_hw_sf1_write(hw, 4, 1, 1, val);
  649. if (ret != 0)
  650. goto unlock;
  651. for (left = n; left; left -= c) {
  652. c = min(left, 4U);
  653. for (val = 0, i = 0; i < c; ++i)
  654. val = (val << 8) + *data++;
  655. ret = csio_hw_sf1_write(hw, c, c != left, 1, val);
  656. if (ret)
  657. goto unlock;
  658. }
  659. ret = csio_hw_flash_wait_op(hw, 8, 1);
  660. if (ret)
  661. goto unlock;
  662. csio_wr_reg32(hw, 0, SF_OP); /* unlock SF */
  663. /* Read the page to verify the write succeeded */
  664. ret = csio_hw_read_flash(hw, addr & ~0xff, ARRAY_SIZE(buf), buf, 1);
  665. if (ret)
  666. return ret;
  667. if (memcmp(data - n, (uint8_t *)buf + offset, n)) {
  668. csio_err(hw,
  669. "failed to correctly write the flash page at %#x\n",
  670. addr);
  671. return -EINVAL;
  672. }
  673. return 0;
  674. unlock:
  675. csio_wr_reg32(hw, 0, SF_OP); /* unlock SF */
  676. return ret;
  677. }
  678. /*
  679. * csio_hw_flash_erase_sectors - erase a range of flash sectors
  680. * @hw: the HW module
  681. * @start: the first sector to erase
  682. * @end: the last sector to erase
  683. *
  684. * Erases the sectors in the given inclusive range.
  685. */
  686. static int
  687. csio_hw_flash_erase_sectors(struct csio_hw *hw, int32_t start, int32_t end)
  688. {
  689. int ret = 0;
  690. while (start <= end) {
  691. ret = csio_hw_sf1_write(hw, 1, 0, 1, SF_WR_ENABLE);
  692. if (ret != 0)
  693. goto out;
  694. ret = csio_hw_sf1_write(hw, 4, 0, 1,
  695. SF_ERASE_SECTOR | (start << 8));
  696. if (ret != 0)
  697. goto out;
  698. ret = csio_hw_flash_wait_op(hw, 14, 500);
  699. if (ret != 0)
  700. goto out;
  701. start++;
  702. }
  703. out:
  704. if (ret)
  705. csio_err(hw, "erase of flash sector %d failed, error %d\n",
  706. start, ret);
  707. csio_wr_reg32(hw, 0, SF_OP); /* unlock SF */
  708. return 0;
  709. }
  710. /*
  711. * csio_hw_flash_cfg_addr - return the address of the flash
  712. * configuration file
  713. * @hw: the HW module
  714. *
  715. * Return the address within the flash where the Firmware Configuration
  716. * File is stored.
  717. */
  718. static unsigned int
  719. csio_hw_flash_cfg_addr(struct csio_hw *hw)
  720. {
  721. if (hw->params.sf_size == 0x100000)
  722. return FPGA_FLASH_CFG_OFFSET;
  723. else
  724. return FLASH_CFG_OFFSET;
  725. }
  726. static void
  727. csio_hw_print_fw_version(struct csio_hw *hw, char *str)
  728. {
  729. csio_info(hw, "%s: %u.%u.%u.%u\n", str,
  730. FW_HDR_FW_VER_MAJOR_GET(hw->fwrev),
  731. FW_HDR_FW_VER_MINOR_GET(hw->fwrev),
  732. FW_HDR_FW_VER_MICRO_GET(hw->fwrev),
  733. FW_HDR_FW_VER_BUILD_GET(hw->fwrev));
  734. }
  735. /*
  736. * csio_hw_get_fw_version - read the firmware version
  737. * @hw: HW module
  738. * @vers: where to place the version
  739. *
  740. * Reads the FW version from flash.
  741. */
  742. static int
  743. csio_hw_get_fw_version(struct csio_hw *hw, uint32_t *vers)
  744. {
  745. return csio_hw_read_flash(hw, FW_IMG_START +
  746. offsetof(struct fw_hdr, fw_ver), 1,
  747. vers, 0);
  748. }
  749. /*
  750. * csio_hw_get_tp_version - read the TP microcode version
  751. * @hw: HW module
  752. * @vers: where to place the version
  753. *
  754. * Reads the TP microcode version from flash.
  755. */
  756. static int
  757. csio_hw_get_tp_version(struct csio_hw *hw, u32 *vers)
  758. {
  759. return csio_hw_read_flash(hw, FLASH_FW_START +
  760. offsetof(struct fw_hdr, tp_microcode_ver), 1,
  761. vers, 0);
  762. }
  763. /*
  764. * csio_hw_check_fw_version - check if the FW is compatible with
  765. * this driver
  766. * @hw: HW module
  767. *
  768. * Checks if an adapter's FW is compatible with the driver. Returns 0
  769. * if there's exact match, a negative error if the version could not be
  770. * read or there's a major/minor version mismatch/minor.
  771. */
  772. static int
  773. csio_hw_check_fw_version(struct csio_hw *hw)
  774. {
  775. int ret, major, minor, micro;
  776. ret = csio_hw_get_fw_version(hw, &hw->fwrev);
  777. if (!ret)
  778. ret = csio_hw_get_tp_version(hw, &hw->tp_vers);
  779. if (ret)
  780. return ret;
  781. major = FW_HDR_FW_VER_MAJOR_GET(hw->fwrev);
  782. minor = FW_HDR_FW_VER_MINOR_GET(hw->fwrev);
  783. micro = FW_HDR_FW_VER_MICRO_GET(hw->fwrev);
  784. if (major != FW_VERSION_MAJOR) { /* major mismatch - fail */
  785. csio_err(hw, "card FW has major version %u, driver wants %u\n",
  786. major, FW_VERSION_MAJOR);
  787. return -EINVAL;
  788. }
  789. if (minor == FW_VERSION_MINOR && micro == FW_VERSION_MICRO)
  790. return 0; /* perfect match */
  791. /* Minor/micro version mismatch */
  792. return -EINVAL;
  793. }
  794. /*
  795. * csio_hw_fw_dload - download firmware.
  796. * @hw: HW module
  797. * @fw_data: firmware image to write.
  798. * @size: image size
  799. *
  800. * Write the supplied firmware image to the card's serial flash.
  801. */
  802. static int
  803. csio_hw_fw_dload(struct csio_hw *hw, uint8_t *fw_data, uint32_t size)
  804. {
  805. uint32_t csum;
  806. int32_t addr;
  807. int ret;
  808. uint32_t i;
  809. uint8_t first_page[SF_PAGE_SIZE];
  810. const __be32 *p = (const __be32 *)fw_data;
  811. struct fw_hdr *hdr = (struct fw_hdr *)fw_data;
  812. uint32_t sf_sec_size;
  813. if ((!hw->params.sf_size) || (!hw->params.sf_nsec)) {
  814. csio_err(hw, "Serial Flash data invalid\n");
  815. return -EINVAL;
  816. }
  817. if (!size) {
  818. csio_err(hw, "FW image has no data\n");
  819. return -EINVAL;
  820. }
  821. if (size & 511) {
  822. csio_err(hw, "FW image size not multiple of 512 bytes\n");
  823. return -EINVAL;
  824. }
  825. if (ntohs(hdr->len512) * 512 != size) {
  826. csio_err(hw, "FW image size differs from size in FW header\n");
  827. return -EINVAL;
  828. }
  829. if (size > FW_MAX_SIZE) {
  830. csio_err(hw, "FW image too large, max is %u bytes\n",
  831. FW_MAX_SIZE);
  832. return -EINVAL;
  833. }
  834. for (csum = 0, i = 0; i < size / sizeof(csum); i++)
  835. csum += ntohl(p[i]);
  836. if (csum != 0xffffffff) {
  837. csio_err(hw, "corrupted firmware image, checksum %#x\n", csum);
  838. return -EINVAL;
  839. }
  840. sf_sec_size = hw->params.sf_size / hw->params.sf_nsec;
  841. i = DIV_ROUND_UP(size, sf_sec_size); /* # of sectors spanned */
  842. csio_dbg(hw, "Erasing sectors... start:%d end:%d\n",
  843. FW_START_SEC, FW_START_SEC + i - 1);
  844. ret = csio_hw_flash_erase_sectors(hw, FW_START_SEC,
  845. FW_START_SEC + i - 1);
  846. if (ret) {
  847. csio_err(hw, "Flash Erase failed\n");
  848. goto out;
  849. }
  850. /*
  851. * We write the correct version at the end so the driver can see a bad
  852. * version if the FW write fails. Start by writing a copy of the
  853. * first page with a bad version.
  854. */
  855. memcpy(first_page, fw_data, SF_PAGE_SIZE);
  856. ((struct fw_hdr *)first_page)->fw_ver = htonl(0xffffffff);
  857. ret = csio_hw_write_flash(hw, FW_IMG_START, SF_PAGE_SIZE, first_page);
  858. if (ret)
  859. goto out;
  860. csio_dbg(hw, "Writing Flash .. start:%d end:%d\n",
  861. FW_IMG_START, FW_IMG_START + size);
  862. addr = FW_IMG_START;
  863. for (size -= SF_PAGE_SIZE; size; size -= SF_PAGE_SIZE) {
  864. addr += SF_PAGE_SIZE;
  865. fw_data += SF_PAGE_SIZE;
  866. ret = csio_hw_write_flash(hw, addr, SF_PAGE_SIZE, fw_data);
  867. if (ret)
  868. goto out;
  869. }
  870. ret = csio_hw_write_flash(hw,
  871. FW_IMG_START +
  872. offsetof(struct fw_hdr, fw_ver),
  873. sizeof(hdr->fw_ver),
  874. (const uint8_t *)&hdr->fw_ver);
  875. out:
  876. if (ret)
  877. csio_err(hw, "firmware download failed, error %d\n", ret);
  878. return ret;
  879. }
  880. static int
  881. csio_hw_get_flash_params(struct csio_hw *hw)
  882. {
  883. int ret;
  884. uint32_t info = 0;
  885. ret = csio_hw_sf1_write(hw, 1, 1, 0, SF_RD_ID);
  886. if (!ret)
  887. ret = csio_hw_sf1_read(hw, 3, 0, 1, &info);
  888. csio_wr_reg32(hw, 0, SF_OP); /* unlock SF */
  889. if (ret != 0)
  890. return ret;
  891. if ((info & 0xff) != 0x20) /* not a Numonix flash */
  892. return -EINVAL;
  893. info >>= 16; /* log2 of size */
  894. if (info >= 0x14 && info < 0x18)
  895. hw->params.sf_nsec = 1 << (info - 16);
  896. else if (info == 0x18)
  897. hw->params.sf_nsec = 64;
  898. else
  899. return -EINVAL;
  900. hw->params.sf_size = 1 << info;
  901. return 0;
  902. }
  903. static void
  904. csio_set_pcie_completion_timeout(struct csio_hw *hw, u8 range)
  905. {
  906. uint16_t val;
  907. uint32_t pcie_cap;
  908. if (!csio_pci_capability(hw->pdev, PCI_CAP_ID_EXP, &pcie_cap)) {
  909. pci_read_config_word(hw->pdev,
  910. pcie_cap + PCI_EXP_DEVCTL2, &val);
  911. val &= 0xfff0;
  912. val |= range ;
  913. pci_write_config_word(hw->pdev,
  914. pcie_cap + PCI_EXP_DEVCTL2, val);
  915. }
  916. }
  917. /*
  918. * Return the specified PCI-E Configuration Space register from our Physical
  919. * Function. We try first via a Firmware LDST Command since we prefer to let
  920. * the firmware own all of these registers, but if that fails we go for it
  921. * directly ourselves.
  922. */
  923. static uint32_t
  924. csio_read_pcie_cfg4(struct csio_hw *hw, int reg)
  925. {
  926. u32 val = 0;
  927. struct csio_mb *mbp;
  928. int rv;
  929. struct fw_ldst_cmd *ldst_cmd;
  930. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  931. if (!mbp) {
  932. CSIO_INC_STATS(hw, n_err_nomem);
  933. pci_read_config_dword(hw->pdev, reg, &val);
  934. return val;
  935. }
  936. csio_mb_ldst(hw, mbp, CSIO_MB_DEFAULT_TMO, reg);
  937. rv = csio_mb_issue(hw, mbp);
  938. /*
  939. * If the LDST Command suucceeded, exctract the returned register
  940. * value. Otherwise read it directly ourself.
  941. */
  942. if (rv == 0) {
  943. ldst_cmd = (struct fw_ldst_cmd *)(mbp->mb);
  944. val = ntohl(ldst_cmd->u.pcie.data[0]);
  945. } else
  946. pci_read_config_dword(hw->pdev, reg, &val);
  947. mempool_free(mbp, hw->mb_mempool);
  948. return val;
  949. } /* csio_read_pcie_cfg4 */
  950. static int
  951. csio_hw_set_mem_win(struct csio_hw *hw)
  952. {
  953. u32 bar0;
  954. /*
  955. * Truncation intentional: we only read the bottom 32-bits of the
  956. * 64-bit BAR0/BAR1 ... We use the hardware backdoor mechanism to
  957. * read BAR0 instead of using pci_resource_start() because we could be
  958. * operating from within a Virtual Machine which is trapping our
  959. * accesses to our Configuration Space and we need to set up the PCI-E
  960. * Memory Window decoders with the actual addresses which will be
  961. * coming across the PCI-E link.
  962. */
  963. bar0 = csio_read_pcie_cfg4(hw, PCI_BASE_ADDRESS_0);
  964. bar0 &= PCI_BASE_ADDRESS_MEM_MASK;
  965. /*
  966. * Set up memory window for accessing adapter memory ranges. (Read
  967. * back MA register to ensure that changes propagate before we attempt
  968. * to use the new values.)
  969. */
  970. csio_wr_reg32(hw, (bar0 + MEMWIN0_BASE) | BIR(0) |
  971. WINDOW(ilog2(MEMWIN0_APERTURE) - 10),
  972. PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN, 0));
  973. csio_wr_reg32(hw, (bar0 + MEMWIN1_BASE) | BIR(0) |
  974. WINDOW(ilog2(MEMWIN1_APERTURE) - 10),
  975. PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN, 1));
  976. csio_wr_reg32(hw, (bar0 + MEMWIN2_BASE) | BIR(0) |
  977. WINDOW(ilog2(MEMWIN2_APERTURE) - 10),
  978. PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN, 2));
  979. csio_rd_reg32(hw, PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN, 2));
  980. return 0;
  981. } /* csio_hw_set_mem_win */
  982. /*****************************************************************************/
  983. /* HW State machine assists */
  984. /*****************************************************************************/
  985. static int
  986. csio_hw_dev_ready(struct csio_hw *hw)
  987. {
  988. uint32_t reg;
  989. int cnt = 6;
  990. while (((reg = csio_rd_reg32(hw, PL_WHOAMI)) == 0xFFFFFFFF) &&
  991. (--cnt != 0))
  992. mdelay(100);
  993. if ((cnt == 0) && (((int32_t)(SOURCEPF_GET(reg)) < 0) ||
  994. (SOURCEPF_GET(reg) >= CSIO_MAX_PFN))) {
  995. csio_err(hw, "PL_WHOAMI returned 0x%x, cnt:%d\n", reg, cnt);
  996. return -EIO;
  997. }
  998. hw->pfn = SOURCEPF_GET(reg);
  999. return 0;
  1000. }
  1001. /*
  1002. * csio_do_hello - Perform the HELLO FW Mailbox command and process response.
  1003. * @hw: HW module
  1004. * @state: Device state
  1005. *
  1006. * FW_HELLO_CMD has to be polled for completion.
  1007. */
  1008. static int
  1009. csio_do_hello(struct csio_hw *hw, enum csio_dev_state *state)
  1010. {
  1011. struct csio_mb *mbp;
  1012. int rv = 0;
  1013. enum csio_dev_master master;
  1014. enum fw_retval retval;
  1015. uint8_t mpfn;
  1016. char state_str[16];
  1017. int retries = FW_CMD_HELLO_RETRIES;
  1018. memset(state_str, 0, sizeof(state_str));
  1019. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1020. if (!mbp) {
  1021. rv = -ENOMEM;
  1022. CSIO_INC_STATS(hw, n_err_nomem);
  1023. goto out;
  1024. }
  1025. master = csio_force_master ? CSIO_MASTER_MUST : CSIO_MASTER_MAY;
  1026. retry:
  1027. csio_mb_hello(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn,
  1028. hw->pfn, master, NULL);
  1029. rv = csio_mb_issue(hw, mbp);
  1030. if (rv) {
  1031. csio_err(hw, "failed to issue HELLO cmd. ret:%d.\n", rv);
  1032. goto out_free_mb;
  1033. }
  1034. csio_mb_process_hello_rsp(hw, mbp, &retval, state, &mpfn);
  1035. if (retval != FW_SUCCESS) {
  1036. csio_err(hw, "HELLO cmd failed with ret: %d\n", retval);
  1037. rv = -EINVAL;
  1038. goto out_free_mb;
  1039. }
  1040. /* Firmware has designated us to be master */
  1041. if (hw->pfn == mpfn) {
  1042. hw->flags |= CSIO_HWF_MASTER;
  1043. } else if (*state == CSIO_DEV_STATE_UNINIT) {
  1044. /*
  1045. * If we're not the Master PF then we need to wait around for
  1046. * the Master PF Driver to finish setting up the adapter.
  1047. *
  1048. * Note that we also do this wait if we're a non-Master-capable
  1049. * PF and there is no current Master PF; a Master PF may show up
  1050. * momentarily and we wouldn't want to fail pointlessly. (This
  1051. * can happen when an OS loads lots of different drivers rapidly
  1052. * at the same time). In this case, the Master PF returned by
  1053. * the firmware will be PCIE_FW_MASTER_MASK so the test below
  1054. * will work ...
  1055. */
  1056. int waiting = FW_CMD_HELLO_TIMEOUT;
  1057. /*
  1058. * Wait for the firmware to either indicate an error or
  1059. * initialized state. If we see either of these we bail out
  1060. * and report the issue to the caller. If we exhaust the
  1061. * "hello timeout" and we haven't exhausted our retries, try
  1062. * again. Otherwise bail with a timeout error.
  1063. */
  1064. for (;;) {
  1065. uint32_t pcie_fw;
  1066. msleep(50);
  1067. waiting -= 50;
  1068. /*
  1069. * If neither Error nor Initialialized are indicated
  1070. * by the firmware keep waiting till we exaust our
  1071. * timeout ... and then retry if we haven't exhausted
  1072. * our retries ...
  1073. */
  1074. pcie_fw = csio_rd_reg32(hw, PCIE_FW);
  1075. if (!(pcie_fw & (PCIE_FW_ERR|PCIE_FW_INIT))) {
  1076. if (waiting <= 0) {
  1077. if (retries-- > 0)
  1078. goto retry;
  1079. rv = -ETIMEDOUT;
  1080. break;
  1081. }
  1082. continue;
  1083. }
  1084. /*
  1085. * We either have an Error or Initialized condition
  1086. * report errors preferentially.
  1087. */
  1088. if (state) {
  1089. if (pcie_fw & PCIE_FW_ERR) {
  1090. *state = CSIO_DEV_STATE_ERR;
  1091. rv = -ETIMEDOUT;
  1092. } else if (pcie_fw & PCIE_FW_INIT)
  1093. *state = CSIO_DEV_STATE_INIT;
  1094. }
  1095. /*
  1096. * If we arrived before a Master PF was selected and
  1097. * there's not a valid Master PF, grab its identity
  1098. * for our caller.
  1099. */
  1100. if (mpfn == PCIE_FW_MASTER_MASK &&
  1101. (pcie_fw & PCIE_FW_MASTER_VLD))
  1102. mpfn = PCIE_FW_MASTER_GET(pcie_fw);
  1103. break;
  1104. }
  1105. hw->flags &= ~CSIO_HWF_MASTER;
  1106. }
  1107. switch (*state) {
  1108. case CSIO_DEV_STATE_UNINIT:
  1109. strcpy(state_str, "Initializing");
  1110. break;
  1111. case CSIO_DEV_STATE_INIT:
  1112. strcpy(state_str, "Initialized");
  1113. break;
  1114. case CSIO_DEV_STATE_ERR:
  1115. strcpy(state_str, "Error");
  1116. break;
  1117. default:
  1118. strcpy(state_str, "Unknown");
  1119. break;
  1120. }
  1121. if (hw->pfn == mpfn)
  1122. csio_info(hw, "PF: %d, Coming up as MASTER, HW state: %s\n",
  1123. hw->pfn, state_str);
  1124. else
  1125. csio_info(hw,
  1126. "PF: %d, Coming up as SLAVE, Master PF: %d, HW state: %s\n",
  1127. hw->pfn, mpfn, state_str);
  1128. out_free_mb:
  1129. mempool_free(mbp, hw->mb_mempool);
  1130. out:
  1131. return rv;
  1132. }
  1133. /*
  1134. * csio_do_bye - Perform the BYE FW Mailbox command and process response.
  1135. * @hw: HW module
  1136. *
  1137. */
  1138. static int
  1139. csio_do_bye(struct csio_hw *hw)
  1140. {
  1141. struct csio_mb *mbp;
  1142. enum fw_retval retval;
  1143. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1144. if (!mbp) {
  1145. CSIO_INC_STATS(hw, n_err_nomem);
  1146. return -ENOMEM;
  1147. }
  1148. csio_mb_bye(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
  1149. if (csio_mb_issue(hw, mbp)) {
  1150. csio_err(hw, "Issue of BYE command failed\n");
  1151. mempool_free(mbp, hw->mb_mempool);
  1152. return -EINVAL;
  1153. }
  1154. retval = csio_mb_fw_retval(mbp);
  1155. if (retval != FW_SUCCESS) {
  1156. mempool_free(mbp, hw->mb_mempool);
  1157. return -EINVAL;
  1158. }
  1159. mempool_free(mbp, hw->mb_mempool);
  1160. return 0;
  1161. }
  1162. /*
  1163. * csio_do_reset- Perform the device reset.
  1164. * @hw: HW module
  1165. * @fw_rst: FW reset
  1166. *
  1167. * If fw_rst is set, issues FW reset mbox cmd otherwise
  1168. * does PIO reset.
  1169. * Performs reset of the function.
  1170. */
  1171. static int
  1172. csio_do_reset(struct csio_hw *hw, bool fw_rst)
  1173. {
  1174. struct csio_mb *mbp;
  1175. enum fw_retval retval;
  1176. if (!fw_rst) {
  1177. /* PIO reset */
  1178. csio_wr_reg32(hw, PIORSTMODE | PIORST, PL_RST);
  1179. mdelay(2000);
  1180. return 0;
  1181. }
  1182. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1183. if (!mbp) {
  1184. CSIO_INC_STATS(hw, n_err_nomem);
  1185. return -ENOMEM;
  1186. }
  1187. csio_mb_reset(hw, mbp, CSIO_MB_DEFAULT_TMO,
  1188. PIORSTMODE | PIORST, 0, NULL);
  1189. if (csio_mb_issue(hw, mbp)) {
  1190. csio_err(hw, "Issue of RESET command failed.n");
  1191. mempool_free(mbp, hw->mb_mempool);
  1192. return -EINVAL;
  1193. }
  1194. retval = csio_mb_fw_retval(mbp);
  1195. if (retval != FW_SUCCESS) {
  1196. csio_err(hw, "RESET cmd failed with ret:0x%x.\n", retval);
  1197. mempool_free(mbp, hw->mb_mempool);
  1198. return -EINVAL;
  1199. }
  1200. mempool_free(mbp, hw->mb_mempool);
  1201. return 0;
  1202. }
  1203. static int
  1204. csio_hw_validate_caps(struct csio_hw *hw, struct csio_mb *mbp)
  1205. {
  1206. struct fw_caps_config_cmd *rsp = (struct fw_caps_config_cmd *)mbp->mb;
  1207. uint16_t caps;
  1208. caps = ntohs(rsp->fcoecaps);
  1209. if (!(caps & FW_CAPS_CONFIG_FCOE_INITIATOR)) {
  1210. csio_err(hw, "No FCoE Initiator capability in the firmware.\n");
  1211. return -EINVAL;
  1212. }
  1213. if (!(caps & FW_CAPS_CONFIG_FCOE_CTRL_OFLD)) {
  1214. csio_err(hw, "No FCoE Control Offload capability\n");
  1215. return -EINVAL;
  1216. }
  1217. return 0;
  1218. }
  1219. /*
  1220. * csio_hw_fw_halt - issue a reset/halt to FW and put uP into RESET
  1221. * @hw: the HW module
  1222. * @mbox: mailbox to use for the FW RESET command (if desired)
  1223. * @force: force uP into RESET even if FW RESET command fails
  1224. *
  1225. * Issues a RESET command to firmware (if desired) with a HALT indication
  1226. * and then puts the microprocessor into RESET state. The RESET command
  1227. * will only be issued if a legitimate mailbox is provided (mbox <=
  1228. * PCIE_FW_MASTER_MASK).
  1229. *
  1230. * This is generally used in order for the host to safely manipulate the
  1231. * adapter without fear of conflicting with whatever the firmware might
  1232. * be doing. The only way out of this state is to RESTART the firmware
  1233. * ...
  1234. */
  1235. static int
  1236. csio_hw_fw_halt(struct csio_hw *hw, uint32_t mbox, int32_t force)
  1237. {
  1238. enum fw_retval retval = 0;
  1239. /*
  1240. * If a legitimate mailbox is provided, issue a RESET command
  1241. * with a HALT indication.
  1242. */
  1243. if (mbox <= PCIE_FW_MASTER_MASK) {
  1244. struct csio_mb *mbp;
  1245. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1246. if (!mbp) {
  1247. CSIO_INC_STATS(hw, n_err_nomem);
  1248. return -ENOMEM;
  1249. }
  1250. csio_mb_reset(hw, mbp, CSIO_MB_DEFAULT_TMO,
  1251. PIORSTMODE | PIORST, FW_RESET_CMD_HALT(1),
  1252. NULL);
  1253. if (csio_mb_issue(hw, mbp)) {
  1254. csio_err(hw, "Issue of RESET command failed!\n");
  1255. mempool_free(mbp, hw->mb_mempool);
  1256. return -EINVAL;
  1257. }
  1258. retval = csio_mb_fw_retval(mbp);
  1259. mempool_free(mbp, hw->mb_mempool);
  1260. }
  1261. /*
  1262. * Normally we won't complete the operation if the firmware RESET
  1263. * command fails but if our caller insists we'll go ahead and put the
  1264. * uP into RESET. This can be useful if the firmware is hung or even
  1265. * missing ... We'll have to take the risk of putting the uP into
  1266. * RESET without the cooperation of firmware in that case.
  1267. *
  1268. * We also force the firmware's HALT flag to be on in case we bypassed
  1269. * the firmware RESET command above or we're dealing with old firmware
  1270. * which doesn't have the HALT capability. This will serve as a flag
  1271. * for the incoming firmware to know that it's coming out of a HALT
  1272. * rather than a RESET ... if it's new enough to understand that ...
  1273. */
  1274. if (retval == 0 || force) {
  1275. csio_set_reg_field(hw, CIM_BOOT_CFG, UPCRST, UPCRST);
  1276. csio_set_reg_field(hw, PCIE_FW, PCIE_FW_HALT, PCIE_FW_HALT);
  1277. }
  1278. /*
  1279. * And we always return the result of the firmware RESET command
  1280. * even when we force the uP into RESET ...
  1281. */
  1282. return retval ? -EINVAL : 0;
  1283. }
  1284. /*
  1285. * csio_hw_fw_restart - restart the firmware by taking the uP out of RESET
  1286. * @hw: the HW module
  1287. * @reset: if we want to do a RESET to restart things
  1288. *
  1289. * Restart firmware previously halted by csio_hw_fw_halt(). On successful
  1290. * return the previous PF Master remains as the new PF Master and there
  1291. * is no need to issue a new HELLO command, etc.
  1292. *
  1293. * We do this in two ways:
  1294. *
  1295. * 1. If we're dealing with newer firmware we'll simply want to take
  1296. * the chip's microprocessor out of RESET. This will cause the
  1297. * firmware to start up from its start vector. And then we'll loop
  1298. * until the firmware indicates it's started again (PCIE_FW.HALT
  1299. * reset to 0) or we timeout.
  1300. *
  1301. * 2. If we're dealing with older firmware then we'll need to RESET
  1302. * the chip since older firmware won't recognize the PCIE_FW.HALT
  1303. * flag and automatically RESET itself on startup.
  1304. */
  1305. static int
  1306. csio_hw_fw_restart(struct csio_hw *hw, uint32_t mbox, int32_t reset)
  1307. {
  1308. if (reset) {
  1309. /*
  1310. * Since we're directing the RESET instead of the firmware
  1311. * doing it automatically, we need to clear the PCIE_FW.HALT
  1312. * bit.
  1313. */
  1314. csio_set_reg_field(hw, PCIE_FW, PCIE_FW_HALT, 0);
  1315. /*
  1316. * If we've been given a valid mailbox, first try to get the
  1317. * firmware to do the RESET. If that works, great and we can
  1318. * return success. Otherwise, if we haven't been given a
  1319. * valid mailbox or the RESET command failed, fall back to
  1320. * hitting the chip with a hammer.
  1321. */
  1322. if (mbox <= PCIE_FW_MASTER_MASK) {
  1323. csio_set_reg_field(hw, CIM_BOOT_CFG, UPCRST, 0);
  1324. msleep(100);
  1325. if (csio_do_reset(hw, true) == 0)
  1326. return 0;
  1327. }
  1328. csio_wr_reg32(hw, PIORSTMODE | PIORST, PL_RST);
  1329. msleep(2000);
  1330. } else {
  1331. int ms;
  1332. csio_set_reg_field(hw, CIM_BOOT_CFG, UPCRST, 0);
  1333. for (ms = 0; ms < FW_CMD_MAX_TIMEOUT; ) {
  1334. if (!(csio_rd_reg32(hw, PCIE_FW) & PCIE_FW_HALT))
  1335. return 0;
  1336. msleep(100);
  1337. ms += 100;
  1338. }
  1339. return -ETIMEDOUT;
  1340. }
  1341. return 0;
  1342. }
  1343. /*
  1344. * csio_hw_fw_upgrade - perform all of the steps necessary to upgrade FW
  1345. * @hw: the HW module
  1346. * @mbox: mailbox to use for the FW RESET command (if desired)
  1347. * @fw_data: the firmware image to write
  1348. * @size: image size
  1349. * @force: force upgrade even if firmware doesn't cooperate
  1350. *
  1351. * Perform all of the steps necessary for upgrading an adapter's
  1352. * firmware image. Normally this requires the cooperation of the
  1353. * existing firmware in order to halt all existing activities
  1354. * but if an invalid mailbox token is passed in we skip that step
  1355. * (though we'll still put the adapter microprocessor into RESET in
  1356. * that case).
  1357. *
  1358. * On successful return the new firmware will have been loaded and
  1359. * the adapter will have been fully RESET losing all previous setup
  1360. * state. On unsuccessful return the adapter may be completely hosed ...
  1361. * positive errno indicates that the adapter is ~probably~ intact, a
  1362. * negative errno indicates that things are looking bad ...
  1363. */
  1364. static int
  1365. csio_hw_fw_upgrade(struct csio_hw *hw, uint32_t mbox,
  1366. const u8 *fw_data, uint32_t size, int32_t force)
  1367. {
  1368. const struct fw_hdr *fw_hdr = (const struct fw_hdr *)fw_data;
  1369. int reset, ret;
  1370. ret = csio_hw_fw_halt(hw, mbox, force);
  1371. if (ret != 0 && !force)
  1372. return ret;
  1373. ret = csio_hw_fw_dload(hw, (uint8_t *) fw_data, size);
  1374. if (ret != 0)
  1375. return ret;
  1376. /*
  1377. * Older versions of the firmware don't understand the new
  1378. * PCIE_FW.HALT flag and so won't know to perform a RESET when they
  1379. * restart. So for newly loaded older firmware we'll have to do the
  1380. * RESET for it so it starts up on a clean slate. We can tell if
  1381. * the newly loaded firmware will handle this right by checking
  1382. * its header flags to see if it advertises the capability.
  1383. */
  1384. reset = ((ntohl(fw_hdr->flags) & FW_HDR_FLAGS_RESET_HALT) == 0);
  1385. return csio_hw_fw_restart(hw, mbox, reset);
  1386. }
  1387. /*
  1388. * csio_hw_fw_config_file - setup an adapter via a Configuration File
  1389. * @hw: the HW module
  1390. * @mbox: mailbox to use for the FW command
  1391. * @mtype: the memory type where the Configuration File is located
  1392. * @maddr: the memory address where the Configuration File is located
  1393. * @finiver: return value for CF [fini] version
  1394. * @finicsum: return value for CF [fini] checksum
  1395. * @cfcsum: return value for CF computed checksum
  1396. *
  1397. * Issue a command to get the firmware to process the Configuration
  1398. * File located at the specified mtype/maddress. If the Configuration
  1399. * File is processed successfully and return value pointers are
  1400. * provided, the Configuration File "[fini] section version and
  1401. * checksum values will be returned along with the computed checksum.
  1402. * It's up to the caller to decide how it wants to respond to the
  1403. * checksums not matching but it recommended that a prominant warning
  1404. * be emitted in order to help people rapidly identify changed or
  1405. * corrupted Configuration Files.
  1406. *
  1407. * Also note that it's possible to modify things like "niccaps",
  1408. * "toecaps",etc. between processing the Configuration File and telling
  1409. * the firmware to use the new configuration. Callers which want to
  1410. * do this will need to "hand-roll" their own CAPS_CONFIGS commands for
  1411. * Configuration Files if they want to do this.
  1412. */
  1413. static int
  1414. csio_hw_fw_config_file(struct csio_hw *hw,
  1415. unsigned int mtype, unsigned int maddr,
  1416. uint32_t *finiver, uint32_t *finicsum, uint32_t *cfcsum)
  1417. {
  1418. struct csio_mb *mbp;
  1419. struct fw_caps_config_cmd *caps_cmd;
  1420. int rv = -EINVAL;
  1421. enum fw_retval ret;
  1422. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1423. if (!mbp) {
  1424. CSIO_INC_STATS(hw, n_err_nomem);
  1425. return -ENOMEM;
  1426. }
  1427. /*
  1428. * Tell the firmware to process the indicated Configuration File.
  1429. * If there are no errors and the caller has provided return value
  1430. * pointers for the [fini] section version, checksum and computed
  1431. * checksum, pass those back to the caller.
  1432. */
  1433. caps_cmd = (struct fw_caps_config_cmd *)(mbp->mb);
  1434. CSIO_INIT_MBP(mbp, caps_cmd, CSIO_MB_DEFAULT_TMO, hw, NULL, 1);
  1435. caps_cmd->op_to_write =
  1436. htonl(FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
  1437. FW_CMD_REQUEST |
  1438. FW_CMD_READ);
  1439. caps_cmd->cfvalid_to_len16 =
  1440. htonl(FW_CAPS_CONFIG_CMD_CFVALID |
  1441. FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
  1442. FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(maddr >> 16) |
  1443. FW_LEN16(*caps_cmd));
  1444. if (csio_mb_issue(hw, mbp)) {
  1445. csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD failed!\n");
  1446. goto out;
  1447. }
  1448. ret = csio_mb_fw_retval(mbp);
  1449. if (ret != FW_SUCCESS) {
  1450. csio_dbg(hw, "FW_CAPS_CONFIG_CMD returned %d!\n", rv);
  1451. goto out;
  1452. }
  1453. if (finiver)
  1454. *finiver = ntohl(caps_cmd->finiver);
  1455. if (finicsum)
  1456. *finicsum = ntohl(caps_cmd->finicsum);
  1457. if (cfcsum)
  1458. *cfcsum = ntohl(caps_cmd->cfcsum);
  1459. /* Validate device capabilities */
  1460. if (csio_hw_validate_caps(hw, mbp)) {
  1461. rv = -ENOENT;
  1462. goto out;
  1463. }
  1464. /*
  1465. * And now tell the firmware to use the configuration we just loaded.
  1466. */
  1467. caps_cmd->op_to_write =
  1468. htonl(FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
  1469. FW_CMD_REQUEST |
  1470. FW_CMD_WRITE);
  1471. caps_cmd->cfvalid_to_len16 = htonl(FW_LEN16(*caps_cmd));
  1472. if (csio_mb_issue(hw, mbp)) {
  1473. csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD failed!\n");
  1474. goto out;
  1475. }
  1476. ret = csio_mb_fw_retval(mbp);
  1477. if (ret != FW_SUCCESS) {
  1478. csio_dbg(hw, "FW_CAPS_CONFIG_CMD returned %d!\n", rv);
  1479. goto out;
  1480. }
  1481. rv = 0;
  1482. out:
  1483. mempool_free(mbp, hw->mb_mempool);
  1484. return rv;
  1485. }
  1486. /*
  1487. * csio_get_device_params - Get device parameters.
  1488. * @hw: HW module
  1489. *
  1490. */
  1491. static int
  1492. csio_get_device_params(struct csio_hw *hw)
  1493. {
  1494. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  1495. struct csio_mb *mbp;
  1496. enum fw_retval retval;
  1497. u32 param[6];
  1498. int i, j = 0;
  1499. /* Initialize portids to -1 */
  1500. for (i = 0; i < CSIO_MAX_PPORTS; i++)
  1501. hw->pport[i].portid = -1;
  1502. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1503. if (!mbp) {
  1504. CSIO_INC_STATS(hw, n_err_nomem);
  1505. return -ENOMEM;
  1506. }
  1507. /* Get port vec information. */
  1508. param[0] = FW_PARAM_DEV(PORTVEC);
  1509. /* Get Core clock. */
  1510. param[1] = FW_PARAM_DEV(CCLK);
  1511. /* Get EQ id start and end. */
  1512. param[2] = FW_PARAM_PFVF(EQ_START);
  1513. param[3] = FW_PARAM_PFVF(EQ_END);
  1514. /* Get IQ id start and end. */
  1515. param[4] = FW_PARAM_PFVF(IQFLINT_START);
  1516. param[5] = FW_PARAM_PFVF(IQFLINT_END);
  1517. csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0,
  1518. ARRAY_SIZE(param), param, NULL, false, NULL);
  1519. if (csio_mb_issue(hw, mbp)) {
  1520. csio_err(hw, "Issue of FW_PARAMS_CMD(read) failed!\n");
  1521. mempool_free(mbp, hw->mb_mempool);
  1522. return -EINVAL;
  1523. }
  1524. csio_mb_process_read_params_rsp(hw, mbp, &retval,
  1525. ARRAY_SIZE(param), param);
  1526. if (retval != FW_SUCCESS) {
  1527. csio_err(hw, "FW_PARAMS_CMD(read) failed with ret:0x%x!\n",
  1528. retval);
  1529. mempool_free(mbp, hw->mb_mempool);
  1530. return -EINVAL;
  1531. }
  1532. /* cache the information. */
  1533. hw->port_vec = param[0];
  1534. hw->vpd.cclk = param[1];
  1535. wrm->fw_eq_start = param[2];
  1536. wrm->fw_iq_start = param[4];
  1537. /* Using FW configured max iqs & eqs */
  1538. if ((hw->flags & CSIO_HWF_USING_SOFT_PARAMS) ||
  1539. !csio_is_hw_master(hw)) {
  1540. hw->cfg_niq = param[5] - param[4] + 1;
  1541. hw->cfg_neq = param[3] - param[2] + 1;
  1542. csio_dbg(hw, "Using fwconfig max niqs %d neqs %d\n",
  1543. hw->cfg_niq, hw->cfg_neq);
  1544. }
  1545. hw->port_vec &= csio_port_mask;
  1546. hw->num_pports = hweight32(hw->port_vec);
  1547. csio_dbg(hw, "Port vector: 0x%x, #ports: %d\n",
  1548. hw->port_vec, hw->num_pports);
  1549. for (i = 0; i < hw->num_pports; i++) {
  1550. while ((hw->port_vec & (1 << j)) == 0)
  1551. j++;
  1552. hw->pport[i].portid = j++;
  1553. csio_dbg(hw, "Found Port:%d\n", hw->pport[i].portid);
  1554. }
  1555. mempool_free(mbp, hw->mb_mempool);
  1556. return 0;
  1557. }
  1558. /*
  1559. * csio_config_device_caps - Get and set device capabilities.
  1560. * @hw: HW module
  1561. *
  1562. */
  1563. static int
  1564. csio_config_device_caps(struct csio_hw *hw)
  1565. {
  1566. struct csio_mb *mbp;
  1567. enum fw_retval retval;
  1568. int rv = -EINVAL;
  1569. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1570. if (!mbp) {
  1571. CSIO_INC_STATS(hw, n_err_nomem);
  1572. return -ENOMEM;
  1573. }
  1574. /* Get device capabilities */
  1575. csio_mb_caps_config(hw, mbp, CSIO_MB_DEFAULT_TMO, 0, 0, 0, 0, NULL);
  1576. if (csio_mb_issue(hw, mbp)) {
  1577. csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD(r) failed!\n");
  1578. goto out;
  1579. }
  1580. retval = csio_mb_fw_retval(mbp);
  1581. if (retval != FW_SUCCESS) {
  1582. csio_err(hw, "FW_CAPS_CONFIG_CMD(r) returned %d!\n", retval);
  1583. goto out;
  1584. }
  1585. /* Validate device capabilities */
  1586. if (csio_hw_validate_caps(hw, mbp))
  1587. goto out;
  1588. /* Don't config device capabilities if already configured */
  1589. if (hw->fw_state == CSIO_DEV_STATE_INIT) {
  1590. rv = 0;
  1591. goto out;
  1592. }
  1593. /* Write back desired device capabilities */
  1594. csio_mb_caps_config(hw, mbp, CSIO_MB_DEFAULT_TMO, true, true,
  1595. false, true, NULL);
  1596. if (csio_mb_issue(hw, mbp)) {
  1597. csio_err(hw, "Issue of FW_CAPS_CONFIG_CMD(w) failed!\n");
  1598. goto out;
  1599. }
  1600. retval = csio_mb_fw_retval(mbp);
  1601. if (retval != FW_SUCCESS) {
  1602. csio_err(hw, "FW_CAPS_CONFIG_CMD(w) returned %d!\n", retval);
  1603. goto out;
  1604. }
  1605. rv = 0;
  1606. out:
  1607. mempool_free(mbp, hw->mb_mempool);
  1608. return rv;
  1609. }
  1610. static int
  1611. csio_config_global_rss(struct csio_hw *hw)
  1612. {
  1613. struct csio_mb *mbp;
  1614. enum fw_retval retval;
  1615. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1616. if (!mbp) {
  1617. CSIO_INC_STATS(hw, n_err_nomem);
  1618. return -ENOMEM;
  1619. }
  1620. csio_rss_glb_config(hw, mbp, CSIO_MB_DEFAULT_TMO,
  1621. FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
  1622. FW_RSS_GLB_CONFIG_CMD_TNLMAPEN |
  1623. FW_RSS_GLB_CONFIG_CMD_HASHTOEPLITZ |
  1624. FW_RSS_GLB_CONFIG_CMD_TNLALLLKP,
  1625. NULL);
  1626. if (csio_mb_issue(hw, mbp)) {
  1627. csio_err(hw, "Issue of FW_RSS_GLB_CONFIG_CMD failed!\n");
  1628. mempool_free(mbp, hw->mb_mempool);
  1629. return -EINVAL;
  1630. }
  1631. retval = csio_mb_fw_retval(mbp);
  1632. if (retval != FW_SUCCESS) {
  1633. csio_err(hw, "FW_RSS_GLB_CONFIG_CMD returned 0x%x!\n", retval);
  1634. mempool_free(mbp, hw->mb_mempool);
  1635. return -EINVAL;
  1636. }
  1637. mempool_free(mbp, hw->mb_mempool);
  1638. return 0;
  1639. }
  1640. /*
  1641. * csio_config_pfvf - Configure Physical/Virtual functions settings.
  1642. * @hw: HW module
  1643. *
  1644. */
  1645. static int
  1646. csio_config_pfvf(struct csio_hw *hw)
  1647. {
  1648. struct csio_mb *mbp;
  1649. enum fw_retval retval;
  1650. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1651. if (!mbp) {
  1652. CSIO_INC_STATS(hw, n_err_nomem);
  1653. return -ENOMEM;
  1654. }
  1655. /*
  1656. * For now, allow all PFs to access to all ports using a pmask
  1657. * value of 0xF (M_FW_PFVF_CMD_PMASK). Once we have VFs, we will
  1658. * need to provide access based on some rule.
  1659. */
  1660. csio_mb_pfvf(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0, CSIO_NEQ,
  1661. CSIO_NETH_CTRL, CSIO_NIQ_FLINT, 0, 0, CSIO_NVI, CSIO_CMASK,
  1662. CSIO_PMASK, CSIO_NEXACTF, CSIO_R_CAPS, CSIO_WX_CAPS, NULL);
  1663. if (csio_mb_issue(hw, mbp)) {
  1664. csio_err(hw, "Issue of FW_PFVF_CMD failed!\n");
  1665. mempool_free(mbp, hw->mb_mempool);
  1666. return -EINVAL;
  1667. }
  1668. retval = csio_mb_fw_retval(mbp);
  1669. if (retval != FW_SUCCESS) {
  1670. csio_err(hw, "FW_PFVF_CMD returned 0x%x!\n", retval);
  1671. mempool_free(mbp, hw->mb_mempool);
  1672. return -EINVAL;
  1673. }
  1674. mempool_free(mbp, hw->mb_mempool);
  1675. return 0;
  1676. }
  1677. /*
  1678. * csio_enable_ports - Bring up all available ports.
  1679. * @hw: HW module.
  1680. *
  1681. */
  1682. static int
  1683. csio_enable_ports(struct csio_hw *hw)
  1684. {
  1685. struct csio_mb *mbp;
  1686. enum fw_retval retval;
  1687. uint8_t portid;
  1688. int i;
  1689. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1690. if (!mbp) {
  1691. CSIO_INC_STATS(hw, n_err_nomem);
  1692. return -ENOMEM;
  1693. }
  1694. for (i = 0; i < hw->num_pports; i++) {
  1695. portid = hw->pport[i].portid;
  1696. /* Read PORT information */
  1697. csio_mb_port(hw, mbp, CSIO_MB_DEFAULT_TMO, portid,
  1698. false, 0, 0, NULL);
  1699. if (csio_mb_issue(hw, mbp)) {
  1700. csio_err(hw, "failed to issue FW_PORT_CMD(r) port:%d\n",
  1701. portid);
  1702. mempool_free(mbp, hw->mb_mempool);
  1703. return -EINVAL;
  1704. }
  1705. csio_mb_process_read_port_rsp(hw, mbp, &retval,
  1706. &hw->pport[i].pcap);
  1707. if (retval != FW_SUCCESS) {
  1708. csio_err(hw, "FW_PORT_CMD(r) port:%d failed: 0x%x\n",
  1709. portid, retval);
  1710. mempool_free(mbp, hw->mb_mempool);
  1711. return -EINVAL;
  1712. }
  1713. /* Write back PORT information */
  1714. csio_mb_port(hw, mbp, CSIO_MB_DEFAULT_TMO, portid, true,
  1715. (PAUSE_RX | PAUSE_TX), hw->pport[i].pcap, NULL);
  1716. if (csio_mb_issue(hw, mbp)) {
  1717. csio_err(hw, "failed to issue FW_PORT_CMD(w) port:%d\n",
  1718. portid);
  1719. mempool_free(mbp, hw->mb_mempool);
  1720. return -EINVAL;
  1721. }
  1722. retval = csio_mb_fw_retval(mbp);
  1723. if (retval != FW_SUCCESS) {
  1724. csio_err(hw, "FW_PORT_CMD(w) port:%d failed :0x%x\n",
  1725. portid, retval);
  1726. mempool_free(mbp, hw->mb_mempool);
  1727. return -EINVAL;
  1728. }
  1729. } /* For all ports */
  1730. mempool_free(mbp, hw->mb_mempool);
  1731. return 0;
  1732. }
  1733. /*
  1734. * csio_get_fcoe_resinfo - Read fcoe fw resource info.
  1735. * @hw: HW module
  1736. * Issued with lock held.
  1737. */
  1738. static int
  1739. csio_get_fcoe_resinfo(struct csio_hw *hw)
  1740. {
  1741. struct csio_fcoe_res_info *res_info = &hw->fres_info;
  1742. struct fw_fcoe_res_info_cmd *rsp;
  1743. struct csio_mb *mbp;
  1744. enum fw_retval retval;
  1745. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1746. if (!mbp) {
  1747. CSIO_INC_STATS(hw, n_err_nomem);
  1748. return -ENOMEM;
  1749. }
  1750. /* Get FCoE FW resource information */
  1751. csio_fcoe_read_res_info_init_mb(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
  1752. if (csio_mb_issue(hw, mbp)) {
  1753. csio_err(hw, "failed to issue FW_FCOE_RES_INFO_CMD\n");
  1754. mempool_free(mbp, hw->mb_mempool);
  1755. return -EINVAL;
  1756. }
  1757. rsp = (struct fw_fcoe_res_info_cmd *)(mbp->mb);
  1758. retval = FW_CMD_RETVAL_GET(ntohl(rsp->retval_len16));
  1759. if (retval != FW_SUCCESS) {
  1760. csio_err(hw, "FW_FCOE_RES_INFO_CMD failed with ret x%x\n",
  1761. retval);
  1762. mempool_free(mbp, hw->mb_mempool);
  1763. return -EINVAL;
  1764. }
  1765. res_info->e_d_tov = ntohs(rsp->e_d_tov);
  1766. res_info->r_a_tov_seq = ntohs(rsp->r_a_tov_seq);
  1767. res_info->r_a_tov_els = ntohs(rsp->r_a_tov_els);
  1768. res_info->r_r_tov = ntohs(rsp->r_r_tov);
  1769. res_info->max_xchgs = ntohl(rsp->max_xchgs);
  1770. res_info->max_ssns = ntohl(rsp->max_ssns);
  1771. res_info->used_xchgs = ntohl(rsp->used_xchgs);
  1772. res_info->used_ssns = ntohl(rsp->used_ssns);
  1773. res_info->max_fcfs = ntohl(rsp->max_fcfs);
  1774. res_info->max_vnps = ntohl(rsp->max_vnps);
  1775. res_info->used_fcfs = ntohl(rsp->used_fcfs);
  1776. res_info->used_vnps = ntohl(rsp->used_vnps);
  1777. csio_dbg(hw, "max ssns:%d max xchgs:%d\n", res_info->max_ssns,
  1778. res_info->max_xchgs);
  1779. mempool_free(mbp, hw->mb_mempool);
  1780. return 0;
  1781. }
  1782. static int
  1783. csio_hw_check_fwconfig(struct csio_hw *hw, u32 *param)
  1784. {
  1785. struct csio_mb *mbp;
  1786. enum fw_retval retval;
  1787. u32 _param[1];
  1788. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  1789. if (!mbp) {
  1790. CSIO_INC_STATS(hw, n_err_nomem);
  1791. return -ENOMEM;
  1792. }
  1793. /*
  1794. * Find out whether we're dealing with a version of
  1795. * the firmware which has configuration file support.
  1796. */
  1797. _param[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
  1798. FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_CF));
  1799. csio_mb_params(hw, mbp, CSIO_MB_DEFAULT_TMO, hw->pfn, 0,
  1800. ARRAY_SIZE(_param), _param, NULL, false, NULL);
  1801. if (csio_mb_issue(hw, mbp)) {
  1802. csio_err(hw, "Issue of FW_PARAMS_CMD(read) failed!\n");
  1803. mempool_free(mbp, hw->mb_mempool);
  1804. return -EINVAL;
  1805. }
  1806. csio_mb_process_read_params_rsp(hw, mbp, &retval,
  1807. ARRAY_SIZE(_param), _param);
  1808. if (retval != FW_SUCCESS) {
  1809. csio_err(hw, "FW_PARAMS_CMD(read) failed with ret:0x%x!\n",
  1810. retval);
  1811. mempool_free(mbp, hw->mb_mempool);
  1812. return -EINVAL;
  1813. }
  1814. mempool_free(mbp, hw->mb_mempool);
  1815. *param = _param[0];
  1816. return 0;
  1817. }
  1818. static int
  1819. csio_hw_flash_config(struct csio_hw *hw, u32 *fw_cfg_param, char *path)
  1820. {
  1821. int ret = 0;
  1822. const struct firmware *cf;
  1823. struct pci_dev *pci_dev = hw->pdev;
  1824. struct device *dev = &pci_dev->dev;
  1825. unsigned int mtype = 0, maddr = 0;
  1826. uint32_t *cfg_data;
  1827. int value_to_add = 0;
  1828. if (request_firmware(&cf, CSIO_CF_FNAME, dev) < 0) {
  1829. csio_err(hw, "could not find config file " CSIO_CF_FNAME
  1830. ",err: %d\n", ret);
  1831. return -ENOENT;
  1832. }
  1833. if (cf->size%4 != 0)
  1834. value_to_add = 4 - (cf->size % 4);
  1835. cfg_data = kzalloc(cf->size+value_to_add, GFP_KERNEL);
  1836. if (cfg_data == NULL) {
  1837. ret = -ENOMEM;
  1838. goto leave;
  1839. }
  1840. memcpy((void *)cfg_data, (const void *)cf->data, cf->size);
  1841. if (csio_hw_check_fwconfig(hw, fw_cfg_param) != 0) {
  1842. ret = -EINVAL;
  1843. goto leave;
  1844. }
  1845. mtype = FW_PARAMS_PARAM_Y_GET(*fw_cfg_param);
  1846. maddr = FW_PARAMS_PARAM_Z_GET(*fw_cfg_param) << 16;
  1847. ret = csio_memory_write(hw, mtype, maddr,
  1848. cf->size + value_to_add, cfg_data);
  1849. if (ret == 0) {
  1850. csio_info(hw, "config file upgraded to " CSIO_CF_FNAME "\n");
  1851. strncpy(path, "/lib/firmware/" CSIO_CF_FNAME, 64);
  1852. }
  1853. leave:
  1854. kfree(cfg_data);
  1855. release_firmware(cf);
  1856. return ret;
  1857. }
  1858. /*
  1859. * HW initialization: contact FW, obtain config, perform basic init.
  1860. *
  1861. * If the firmware we're dealing with has Configuration File support, then
  1862. * we use that to perform all configuration -- either using the configuration
  1863. * file stored in flash on the adapter or using a filesystem-local file
  1864. * if available.
  1865. *
  1866. * If we don't have configuration file support in the firmware, then we'll
  1867. * have to set things up the old fashioned way with hard-coded register
  1868. * writes and firmware commands ...
  1869. */
  1870. /*
  1871. * Attempt to initialize the HW via a Firmware Configuration File.
  1872. */
  1873. static int
  1874. csio_hw_use_fwconfig(struct csio_hw *hw, int reset, u32 *fw_cfg_param)
  1875. {
  1876. unsigned int mtype, maddr;
  1877. int rv;
  1878. uint32_t finiver, finicsum, cfcsum;
  1879. int using_flash;
  1880. char path[64];
  1881. /*
  1882. * Reset device if necessary
  1883. */
  1884. if (reset) {
  1885. rv = csio_do_reset(hw, true);
  1886. if (rv != 0)
  1887. goto bye;
  1888. }
  1889. /*
  1890. * If we have a configuration file in host ,
  1891. * then use that. Otherwise, use the configuration file stored
  1892. * in the HW flash ...
  1893. */
  1894. spin_unlock_irq(&hw->lock);
  1895. rv = csio_hw_flash_config(hw, fw_cfg_param, path);
  1896. spin_lock_irq(&hw->lock);
  1897. if (rv != 0) {
  1898. if (rv == -ENOENT) {
  1899. /*
  1900. * config file was not found. Use default
  1901. * config file from flash.
  1902. */
  1903. mtype = FW_MEMTYPE_CF_FLASH;
  1904. maddr = csio_hw_flash_cfg_addr(hw);
  1905. using_flash = 1;
  1906. } else {
  1907. /*
  1908. * we revert back to the hardwired config if
  1909. * flashing failed.
  1910. */
  1911. goto bye;
  1912. }
  1913. } else {
  1914. mtype = FW_PARAMS_PARAM_Y_GET(*fw_cfg_param);
  1915. maddr = FW_PARAMS_PARAM_Z_GET(*fw_cfg_param) << 16;
  1916. using_flash = 0;
  1917. }
  1918. hw->cfg_store = (uint8_t)mtype;
  1919. /*
  1920. * Issue a Capability Configuration command to the firmware to get it
  1921. * to parse the Configuration File.
  1922. */
  1923. rv = csio_hw_fw_config_file(hw, mtype, maddr, &finiver,
  1924. &finicsum, &cfcsum);
  1925. if (rv != 0)
  1926. goto bye;
  1927. hw->cfg_finiver = finiver;
  1928. hw->cfg_finicsum = finicsum;
  1929. hw->cfg_cfcsum = cfcsum;
  1930. hw->cfg_csum_status = true;
  1931. if (finicsum != cfcsum) {
  1932. csio_warn(hw,
  1933. "Config File checksum mismatch: csum=%#x, computed=%#x\n",
  1934. finicsum, cfcsum);
  1935. hw->cfg_csum_status = false;
  1936. }
  1937. /*
  1938. * Note that we're operating with parameters
  1939. * not supplied by the driver, rather than from hard-wired
  1940. * initialization constants buried in the driver.
  1941. */
  1942. hw->flags |= CSIO_HWF_USING_SOFT_PARAMS;
  1943. /* device parameters */
  1944. rv = csio_get_device_params(hw);
  1945. if (rv != 0)
  1946. goto bye;
  1947. /* Configure SGE */
  1948. csio_wr_sge_init(hw);
  1949. /*
  1950. * And finally tell the firmware to initialize itself using the
  1951. * parameters from the Configuration File.
  1952. */
  1953. /* Post event to notify completion of configuration */
  1954. csio_post_event(&hw->sm, CSIO_HWE_INIT);
  1955. csio_info(hw,
  1956. "Firmware Configuration File %s, version %#x, computed checksum %#x\n",
  1957. (using_flash ? "in device FLASH" : path), finiver, cfcsum);
  1958. return 0;
  1959. /*
  1960. * Something bad happened. Return the error ...
  1961. */
  1962. bye:
  1963. hw->flags &= ~CSIO_HWF_USING_SOFT_PARAMS;
  1964. csio_dbg(hw, "Configuration file error %d\n", rv);
  1965. return rv;
  1966. }
  1967. /*
  1968. * Attempt to initialize the adapter via hard-coded, driver supplied
  1969. * parameters ...
  1970. */
  1971. static int
  1972. csio_hw_no_fwconfig(struct csio_hw *hw, int reset)
  1973. {
  1974. int rv;
  1975. /*
  1976. * Reset device if necessary
  1977. */
  1978. if (reset) {
  1979. rv = csio_do_reset(hw, true);
  1980. if (rv != 0)
  1981. goto out;
  1982. }
  1983. /* Get and set device capabilities */
  1984. rv = csio_config_device_caps(hw);
  1985. if (rv != 0)
  1986. goto out;
  1987. /* Config Global RSS command */
  1988. rv = csio_config_global_rss(hw);
  1989. if (rv != 0)
  1990. goto out;
  1991. /* Configure PF/VF capabilities of device */
  1992. rv = csio_config_pfvf(hw);
  1993. if (rv != 0)
  1994. goto out;
  1995. /* device parameters */
  1996. rv = csio_get_device_params(hw);
  1997. if (rv != 0)
  1998. goto out;
  1999. /* Configure SGE */
  2000. csio_wr_sge_init(hw);
  2001. /* Post event to notify completion of configuration */
  2002. csio_post_event(&hw->sm, CSIO_HWE_INIT);
  2003. out:
  2004. return rv;
  2005. }
  2006. /*
  2007. * Returns -EINVAL if attempts to flash the firmware failed
  2008. * else returns 0,
  2009. * if flashing was not attempted because the card had the
  2010. * latest firmware ECANCELED is returned
  2011. */
  2012. static int
  2013. csio_hw_flash_fw(struct csio_hw *hw)
  2014. {
  2015. int ret = -ECANCELED;
  2016. const struct firmware *fw;
  2017. const struct fw_hdr *hdr;
  2018. u32 fw_ver;
  2019. struct pci_dev *pci_dev = hw->pdev;
  2020. struct device *dev = &pci_dev->dev ;
  2021. if (request_firmware(&fw, CSIO_FW_FNAME, dev) < 0) {
  2022. csio_err(hw, "could not find firmware image " CSIO_FW_FNAME
  2023. ",err: %d\n", ret);
  2024. return -EINVAL;
  2025. }
  2026. hdr = (const struct fw_hdr *)fw->data;
  2027. fw_ver = ntohl(hdr->fw_ver);
  2028. if (FW_HDR_FW_VER_MAJOR_GET(fw_ver) != FW_VERSION_MAJOR)
  2029. return -EINVAL; /* wrong major version, won't do */
  2030. /*
  2031. * If the flash FW is unusable or we found something newer, load it.
  2032. */
  2033. if (FW_HDR_FW_VER_MAJOR_GET(hw->fwrev) != FW_VERSION_MAJOR ||
  2034. fw_ver > hw->fwrev) {
  2035. ret = csio_hw_fw_upgrade(hw, hw->pfn, fw->data, fw->size,
  2036. /*force=*/false);
  2037. if (!ret)
  2038. csio_info(hw, "firmware upgraded to version %pI4 from "
  2039. CSIO_FW_FNAME "\n", &hdr->fw_ver);
  2040. else
  2041. csio_err(hw, "firmware upgrade failed! err=%d\n", ret);
  2042. }
  2043. release_firmware(fw);
  2044. return ret;
  2045. }
  2046. /*
  2047. * csio_hw_configure - Configure HW
  2048. * @hw - HW module
  2049. *
  2050. */
  2051. static void
  2052. csio_hw_configure(struct csio_hw *hw)
  2053. {
  2054. int reset = 1;
  2055. int rv;
  2056. u32 param[1];
  2057. rv = csio_hw_dev_ready(hw);
  2058. if (rv != 0) {
  2059. CSIO_INC_STATS(hw, n_err_fatal);
  2060. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  2061. goto out;
  2062. }
  2063. /* HW version */
  2064. hw->chip_ver = (char)csio_rd_reg32(hw, PL_REV);
  2065. /* Needed for FW download */
  2066. rv = csio_hw_get_flash_params(hw);
  2067. if (rv != 0) {
  2068. csio_err(hw, "Failed to get serial flash params rv:%d\n", rv);
  2069. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  2070. goto out;
  2071. }
  2072. /* Set pci completion timeout value to 4 seconds. */
  2073. csio_set_pcie_completion_timeout(hw, 0xd);
  2074. csio_hw_set_mem_win(hw);
  2075. rv = csio_hw_get_fw_version(hw, &hw->fwrev);
  2076. if (rv != 0)
  2077. goto out;
  2078. csio_hw_print_fw_version(hw, "Firmware revision");
  2079. rv = csio_do_hello(hw, &hw->fw_state);
  2080. if (rv != 0) {
  2081. CSIO_INC_STATS(hw, n_err_fatal);
  2082. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  2083. goto out;
  2084. }
  2085. /* Read vpd */
  2086. rv = csio_hw_get_vpd_params(hw, &hw->vpd);
  2087. if (rv != 0)
  2088. goto out;
  2089. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  2090. rv = csio_hw_check_fw_version(hw);
  2091. if (rv == -EINVAL) {
  2092. /* Do firmware update */
  2093. spin_unlock_irq(&hw->lock);
  2094. rv = csio_hw_flash_fw(hw);
  2095. spin_lock_irq(&hw->lock);
  2096. if (rv == 0) {
  2097. reset = 0;
  2098. /*
  2099. * Note that the chip was reset as part of the
  2100. * firmware upgrade so we don't reset it again
  2101. * below and grab the new firmware version.
  2102. */
  2103. rv = csio_hw_check_fw_version(hw);
  2104. }
  2105. }
  2106. /*
  2107. * If the firmware doesn't support Configuration
  2108. * Files, use the old Driver-based, hard-wired
  2109. * initialization. Otherwise, try using the
  2110. * Configuration File support and fall back to the
  2111. * Driver-based initialization if there's no
  2112. * Configuration File found.
  2113. */
  2114. if (csio_hw_check_fwconfig(hw, param) == 0) {
  2115. rv = csio_hw_use_fwconfig(hw, reset, param);
  2116. if (rv == -ENOENT)
  2117. goto out;
  2118. if (rv != 0) {
  2119. csio_info(hw,
  2120. "No Configuration File present "
  2121. "on adapter. Using hard-wired "
  2122. "configuration parameters.\n");
  2123. rv = csio_hw_no_fwconfig(hw, reset);
  2124. }
  2125. } else {
  2126. rv = csio_hw_no_fwconfig(hw, reset);
  2127. }
  2128. if (rv != 0)
  2129. goto out;
  2130. } else {
  2131. if (hw->fw_state == CSIO_DEV_STATE_INIT) {
  2132. /* device parameters */
  2133. rv = csio_get_device_params(hw);
  2134. if (rv != 0)
  2135. goto out;
  2136. /* Get device capabilities */
  2137. rv = csio_config_device_caps(hw);
  2138. if (rv != 0)
  2139. goto out;
  2140. /* Configure SGE */
  2141. csio_wr_sge_init(hw);
  2142. /* Post event to notify completion of configuration */
  2143. csio_post_event(&hw->sm, CSIO_HWE_INIT);
  2144. goto out;
  2145. }
  2146. } /* if not master */
  2147. out:
  2148. return;
  2149. }
  2150. /*
  2151. * csio_hw_initialize - Initialize HW
  2152. * @hw - HW module
  2153. *
  2154. */
  2155. static void
  2156. csio_hw_initialize(struct csio_hw *hw)
  2157. {
  2158. struct csio_mb *mbp;
  2159. enum fw_retval retval;
  2160. int rv;
  2161. int i;
  2162. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  2163. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  2164. if (!mbp)
  2165. goto out;
  2166. csio_mb_initialize(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
  2167. if (csio_mb_issue(hw, mbp)) {
  2168. csio_err(hw, "Issue of FW_INITIALIZE_CMD failed!\n");
  2169. goto free_and_out;
  2170. }
  2171. retval = csio_mb_fw_retval(mbp);
  2172. if (retval != FW_SUCCESS) {
  2173. csio_err(hw, "FW_INITIALIZE_CMD returned 0x%x!\n",
  2174. retval);
  2175. goto free_and_out;
  2176. }
  2177. mempool_free(mbp, hw->mb_mempool);
  2178. }
  2179. rv = csio_get_fcoe_resinfo(hw);
  2180. if (rv != 0) {
  2181. csio_err(hw, "Failed to read fcoe resource info: %d\n", rv);
  2182. goto out;
  2183. }
  2184. spin_unlock_irq(&hw->lock);
  2185. rv = csio_config_queues(hw);
  2186. spin_lock_irq(&hw->lock);
  2187. if (rv != 0) {
  2188. csio_err(hw, "Config of queues failed!: %d\n", rv);
  2189. goto out;
  2190. }
  2191. for (i = 0; i < hw->num_pports; i++)
  2192. hw->pport[i].mod_type = FW_PORT_MOD_TYPE_NA;
  2193. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  2194. rv = csio_enable_ports(hw);
  2195. if (rv != 0) {
  2196. csio_err(hw, "Failed to enable ports: %d\n", rv);
  2197. goto out;
  2198. }
  2199. }
  2200. csio_post_event(&hw->sm, CSIO_HWE_INIT_DONE);
  2201. return;
  2202. free_and_out:
  2203. mempool_free(mbp, hw->mb_mempool);
  2204. out:
  2205. return;
  2206. }
  2207. #define PF_INTR_MASK (PFSW | PFCIM)
  2208. /*
  2209. * csio_hw_intr_enable - Enable HW interrupts
  2210. * @hw: Pointer to HW module.
  2211. *
  2212. * Enable interrupts in HW registers.
  2213. */
  2214. static void
  2215. csio_hw_intr_enable(struct csio_hw *hw)
  2216. {
  2217. uint16_t vec = (uint16_t)csio_get_mb_intr_idx(csio_hw_to_mbm(hw));
  2218. uint32_t pf = SOURCEPF_GET(csio_rd_reg32(hw, PL_WHOAMI));
  2219. uint32_t pl = csio_rd_reg32(hw, PL_INT_ENABLE);
  2220. /*
  2221. * Set aivec for MSI/MSIX. PCIE_PF_CFG.INTXType is set up
  2222. * by FW, so do nothing for INTX.
  2223. */
  2224. if (hw->intr_mode == CSIO_IM_MSIX)
  2225. csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG),
  2226. AIVEC(AIVEC_MASK), vec);
  2227. else if (hw->intr_mode == CSIO_IM_MSI)
  2228. csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG),
  2229. AIVEC(AIVEC_MASK), 0);
  2230. csio_wr_reg32(hw, PF_INTR_MASK, MYPF_REG(PL_PF_INT_ENABLE));
  2231. /* Turn on MB interrupts - this will internally flush PIO as well */
  2232. csio_mb_intr_enable(hw);
  2233. /* These are common registers - only a master can modify them */
  2234. if (csio_is_hw_master(hw)) {
  2235. /*
  2236. * Disable the Serial FLASH interrupt, if enabled!
  2237. */
  2238. pl &= (~SF);
  2239. csio_wr_reg32(hw, pl, PL_INT_ENABLE);
  2240. csio_wr_reg32(hw, ERR_CPL_EXCEED_IQE_SIZE |
  2241. EGRESS_SIZE_ERR | ERR_INVALID_CIDX_INC |
  2242. ERR_CPL_OPCODE_0 | ERR_DROPPED_DB |
  2243. ERR_DATA_CPL_ON_HIGH_QID1 |
  2244. ERR_DATA_CPL_ON_HIGH_QID0 | ERR_BAD_DB_PIDX3 |
  2245. ERR_BAD_DB_PIDX2 | ERR_BAD_DB_PIDX1 |
  2246. ERR_BAD_DB_PIDX0 | ERR_ING_CTXT_PRIO |
  2247. ERR_EGR_CTXT_PRIO | INGRESS_SIZE_ERR,
  2248. SGE_INT_ENABLE3);
  2249. csio_set_reg_field(hw, PL_INT_MAP0, 0, 1 << pf);
  2250. }
  2251. hw->flags |= CSIO_HWF_HW_INTR_ENABLED;
  2252. }
  2253. /*
  2254. * csio_hw_intr_disable - Disable HW interrupts
  2255. * @hw: Pointer to HW module.
  2256. *
  2257. * Turn off Mailbox and PCI_PF_CFG interrupts.
  2258. */
  2259. void
  2260. csio_hw_intr_disable(struct csio_hw *hw)
  2261. {
  2262. uint32_t pf = SOURCEPF_GET(csio_rd_reg32(hw, PL_WHOAMI));
  2263. if (!(hw->flags & CSIO_HWF_HW_INTR_ENABLED))
  2264. return;
  2265. hw->flags &= ~CSIO_HWF_HW_INTR_ENABLED;
  2266. csio_wr_reg32(hw, 0, MYPF_REG(PL_PF_INT_ENABLE));
  2267. if (csio_is_hw_master(hw))
  2268. csio_set_reg_field(hw, PL_INT_MAP0, 1 << pf, 0);
  2269. /* Turn off MB interrupts */
  2270. csio_mb_intr_disable(hw);
  2271. }
  2272. static void
  2273. csio_hw_fatal_err(struct csio_hw *hw)
  2274. {
  2275. csio_set_reg_field(hw, SGE_CONTROL, GLOBALENABLE, 0);
  2276. csio_hw_intr_disable(hw);
  2277. /* Do not reset HW, we may need FW state for debugging */
  2278. csio_fatal(hw, "HW Fatal error encountered!\n");
  2279. }
  2280. /*****************************************************************************/
  2281. /* START: HW SM */
  2282. /*****************************************************************************/
  2283. /*
  2284. * csio_hws_uninit - Uninit state
  2285. * @hw - HW module
  2286. * @evt - Event
  2287. *
  2288. */
  2289. static void
  2290. csio_hws_uninit(struct csio_hw *hw, enum csio_hw_ev evt)
  2291. {
  2292. hw->prev_evt = hw->cur_evt;
  2293. hw->cur_evt = evt;
  2294. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2295. switch (evt) {
  2296. case CSIO_HWE_CFG:
  2297. csio_set_state(&hw->sm, csio_hws_configuring);
  2298. csio_hw_configure(hw);
  2299. break;
  2300. default:
  2301. CSIO_INC_STATS(hw, n_evt_unexp);
  2302. break;
  2303. }
  2304. }
  2305. /*
  2306. * csio_hws_configuring - Configuring state
  2307. * @hw - HW module
  2308. * @evt - Event
  2309. *
  2310. */
  2311. static void
  2312. csio_hws_configuring(struct csio_hw *hw, enum csio_hw_ev evt)
  2313. {
  2314. hw->prev_evt = hw->cur_evt;
  2315. hw->cur_evt = evt;
  2316. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2317. switch (evt) {
  2318. case CSIO_HWE_INIT:
  2319. csio_set_state(&hw->sm, csio_hws_initializing);
  2320. csio_hw_initialize(hw);
  2321. break;
  2322. case CSIO_HWE_INIT_DONE:
  2323. csio_set_state(&hw->sm, csio_hws_ready);
  2324. /* Fan out event to all lnode SMs */
  2325. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
  2326. break;
  2327. case CSIO_HWE_FATAL:
  2328. csio_set_state(&hw->sm, csio_hws_uninit);
  2329. break;
  2330. case CSIO_HWE_PCI_REMOVE:
  2331. csio_do_bye(hw);
  2332. break;
  2333. default:
  2334. CSIO_INC_STATS(hw, n_evt_unexp);
  2335. break;
  2336. }
  2337. }
  2338. /*
  2339. * csio_hws_initializing - Initialiazing state
  2340. * @hw - HW module
  2341. * @evt - Event
  2342. *
  2343. */
  2344. static void
  2345. csio_hws_initializing(struct csio_hw *hw, enum csio_hw_ev evt)
  2346. {
  2347. hw->prev_evt = hw->cur_evt;
  2348. hw->cur_evt = evt;
  2349. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2350. switch (evt) {
  2351. case CSIO_HWE_INIT_DONE:
  2352. csio_set_state(&hw->sm, csio_hws_ready);
  2353. /* Fan out event to all lnode SMs */
  2354. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
  2355. /* Enable interrupts */
  2356. csio_hw_intr_enable(hw);
  2357. break;
  2358. case CSIO_HWE_FATAL:
  2359. csio_set_state(&hw->sm, csio_hws_uninit);
  2360. break;
  2361. case CSIO_HWE_PCI_REMOVE:
  2362. csio_do_bye(hw);
  2363. break;
  2364. default:
  2365. CSIO_INC_STATS(hw, n_evt_unexp);
  2366. break;
  2367. }
  2368. }
  2369. /*
  2370. * csio_hws_ready - Ready state
  2371. * @hw - HW module
  2372. * @evt - Event
  2373. *
  2374. */
  2375. static void
  2376. csio_hws_ready(struct csio_hw *hw, enum csio_hw_ev evt)
  2377. {
  2378. /* Remember the event */
  2379. hw->evtflag = evt;
  2380. hw->prev_evt = hw->cur_evt;
  2381. hw->cur_evt = evt;
  2382. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2383. switch (evt) {
  2384. case CSIO_HWE_HBA_RESET:
  2385. case CSIO_HWE_FW_DLOAD:
  2386. case CSIO_HWE_SUSPEND:
  2387. case CSIO_HWE_PCI_REMOVE:
  2388. case CSIO_HWE_PCIERR_DETECTED:
  2389. csio_set_state(&hw->sm, csio_hws_quiescing);
  2390. /* cleanup all outstanding cmds */
  2391. if (evt == CSIO_HWE_HBA_RESET ||
  2392. evt == CSIO_HWE_PCIERR_DETECTED)
  2393. csio_scsim_cleanup_io(csio_hw_to_scsim(hw), false);
  2394. else
  2395. csio_scsim_cleanup_io(csio_hw_to_scsim(hw), true);
  2396. csio_hw_intr_disable(hw);
  2397. csio_hw_mbm_cleanup(hw);
  2398. csio_evtq_stop(hw);
  2399. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWSTOP);
  2400. csio_evtq_flush(hw);
  2401. csio_mgmtm_cleanup(csio_hw_to_mgmtm(hw));
  2402. csio_post_event(&hw->sm, CSIO_HWE_QUIESCED);
  2403. break;
  2404. case CSIO_HWE_FATAL:
  2405. csio_set_state(&hw->sm, csio_hws_uninit);
  2406. break;
  2407. default:
  2408. CSIO_INC_STATS(hw, n_evt_unexp);
  2409. break;
  2410. }
  2411. }
  2412. /*
  2413. * csio_hws_quiescing - Quiescing state
  2414. * @hw - HW module
  2415. * @evt - Event
  2416. *
  2417. */
  2418. static void
  2419. csio_hws_quiescing(struct csio_hw *hw, enum csio_hw_ev evt)
  2420. {
  2421. hw->prev_evt = hw->cur_evt;
  2422. hw->cur_evt = evt;
  2423. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2424. switch (evt) {
  2425. case CSIO_HWE_QUIESCED:
  2426. switch (hw->evtflag) {
  2427. case CSIO_HWE_FW_DLOAD:
  2428. csio_set_state(&hw->sm, csio_hws_resetting);
  2429. /* Download firmware */
  2430. /* Fall through */
  2431. case CSIO_HWE_HBA_RESET:
  2432. csio_set_state(&hw->sm, csio_hws_resetting);
  2433. /* Start reset of the HBA */
  2434. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWRESET);
  2435. csio_wr_destroy_queues(hw, false);
  2436. csio_do_reset(hw, false);
  2437. csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET_DONE);
  2438. break;
  2439. case CSIO_HWE_PCI_REMOVE:
  2440. csio_set_state(&hw->sm, csio_hws_removing);
  2441. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREMOVE);
  2442. csio_wr_destroy_queues(hw, true);
  2443. /* Now send the bye command */
  2444. csio_do_bye(hw);
  2445. break;
  2446. case CSIO_HWE_SUSPEND:
  2447. csio_set_state(&hw->sm, csio_hws_quiesced);
  2448. break;
  2449. case CSIO_HWE_PCIERR_DETECTED:
  2450. csio_set_state(&hw->sm, csio_hws_pcierr);
  2451. csio_wr_destroy_queues(hw, false);
  2452. break;
  2453. default:
  2454. CSIO_INC_STATS(hw, n_evt_unexp);
  2455. break;
  2456. }
  2457. break;
  2458. default:
  2459. CSIO_INC_STATS(hw, n_evt_unexp);
  2460. break;
  2461. }
  2462. }
  2463. /*
  2464. * csio_hws_quiesced - Quiesced state
  2465. * @hw - HW module
  2466. * @evt - Event
  2467. *
  2468. */
  2469. static void
  2470. csio_hws_quiesced(struct csio_hw *hw, enum csio_hw_ev evt)
  2471. {
  2472. hw->prev_evt = hw->cur_evt;
  2473. hw->cur_evt = evt;
  2474. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2475. switch (evt) {
  2476. case CSIO_HWE_RESUME:
  2477. csio_set_state(&hw->sm, csio_hws_configuring);
  2478. csio_hw_configure(hw);
  2479. break;
  2480. default:
  2481. CSIO_INC_STATS(hw, n_evt_unexp);
  2482. break;
  2483. }
  2484. }
  2485. /*
  2486. * csio_hws_resetting - HW Resetting state
  2487. * @hw - HW module
  2488. * @evt - Event
  2489. *
  2490. */
  2491. static void
  2492. csio_hws_resetting(struct csio_hw *hw, enum csio_hw_ev evt)
  2493. {
  2494. hw->prev_evt = hw->cur_evt;
  2495. hw->cur_evt = evt;
  2496. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2497. switch (evt) {
  2498. case CSIO_HWE_HBA_RESET_DONE:
  2499. csio_evtq_start(hw);
  2500. csio_set_state(&hw->sm, csio_hws_configuring);
  2501. csio_hw_configure(hw);
  2502. break;
  2503. default:
  2504. CSIO_INC_STATS(hw, n_evt_unexp);
  2505. break;
  2506. }
  2507. }
  2508. /*
  2509. * csio_hws_removing - PCI Hotplug removing state
  2510. * @hw - HW module
  2511. * @evt - Event
  2512. *
  2513. */
  2514. static void
  2515. csio_hws_removing(struct csio_hw *hw, enum csio_hw_ev evt)
  2516. {
  2517. hw->prev_evt = hw->cur_evt;
  2518. hw->cur_evt = evt;
  2519. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2520. switch (evt) {
  2521. case CSIO_HWE_HBA_RESET:
  2522. if (!csio_is_hw_master(hw))
  2523. break;
  2524. /*
  2525. * The BYE should have alerady been issued, so we cant
  2526. * use the mailbox interface. Hence we use the PL_RST
  2527. * register directly.
  2528. */
  2529. csio_err(hw, "Resetting HW and waiting 2 seconds...\n");
  2530. csio_wr_reg32(hw, PIORSTMODE | PIORST, PL_RST);
  2531. mdelay(2000);
  2532. break;
  2533. /* Should never receive any new events */
  2534. default:
  2535. CSIO_INC_STATS(hw, n_evt_unexp);
  2536. break;
  2537. }
  2538. }
  2539. /*
  2540. * csio_hws_pcierr - PCI Error state
  2541. * @hw - HW module
  2542. * @evt - Event
  2543. *
  2544. */
  2545. static void
  2546. csio_hws_pcierr(struct csio_hw *hw, enum csio_hw_ev evt)
  2547. {
  2548. hw->prev_evt = hw->cur_evt;
  2549. hw->cur_evt = evt;
  2550. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2551. switch (evt) {
  2552. case CSIO_HWE_PCIERR_SLOT_RESET:
  2553. csio_evtq_start(hw);
  2554. csio_set_state(&hw->sm, csio_hws_configuring);
  2555. csio_hw_configure(hw);
  2556. break;
  2557. default:
  2558. CSIO_INC_STATS(hw, n_evt_unexp);
  2559. break;
  2560. }
  2561. }
  2562. /*****************************************************************************/
  2563. /* END: HW SM */
  2564. /*****************************************************************************/
  2565. /* Slow path handlers */
  2566. struct intr_info {
  2567. unsigned int mask; /* bits to check in interrupt status */
  2568. const char *msg; /* message to print or NULL */
  2569. short stat_idx; /* stat counter to increment or -1 */
  2570. unsigned short fatal; /* whether the condition reported is fatal */
  2571. };
  2572. /*
  2573. * csio_handle_intr_status - table driven interrupt handler
  2574. * @hw: HW instance
  2575. * @reg: the interrupt status register to process
  2576. * @acts: table of interrupt actions
  2577. *
  2578. * A table driven interrupt handler that applies a set of masks to an
  2579. * interrupt status word and performs the corresponding actions if the
  2580. * interrupts described by the mask have occured. The actions include
  2581. * optionally emitting a warning or alert message. The table is terminated
  2582. * by an entry specifying mask 0. Returns the number of fatal interrupt
  2583. * conditions.
  2584. */
  2585. static int
  2586. csio_handle_intr_status(struct csio_hw *hw, unsigned int reg,
  2587. const struct intr_info *acts)
  2588. {
  2589. int fatal = 0;
  2590. unsigned int mask = 0;
  2591. unsigned int status = csio_rd_reg32(hw, reg);
  2592. for ( ; acts->mask; ++acts) {
  2593. if (!(status & acts->mask))
  2594. continue;
  2595. if (acts->fatal) {
  2596. fatal++;
  2597. csio_fatal(hw, "Fatal %s (0x%x)\n",
  2598. acts->msg, status & acts->mask);
  2599. } else if (acts->msg)
  2600. csio_info(hw, "%s (0x%x)\n",
  2601. acts->msg, status & acts->mask);
  2602. mask |= acts->mask;
  2603. }
  2604. status &= mask;
  2605. if (status) /* clear processed interrupts */
  2606. csio_wr_reg32(hw, status, reg);
  2607. return fatal;
  2608. }
  2609. /*
  2610. * Interrupt handler for the PCIE module.
  2611. */
  2612. static void
  2613. csio_pcie_intr_handler(struct csio_hw *hw)
  2614. {
  2615. static struct intr_info sysbus_intr_info[] = {
  2616. { RNPP, "RXNP array parity error", -1, 1 },
  2617. { RPCP, "RXPC array parity error", -1, 1 },
  2618. { RCIP, "RXCIF array parity error", -1, 1 },
  2619. { RCCP, "Rx completions control array parity error", -1, 1 },
  2620. { RFTP, "RXFT array parity error", -1, 1 },
  2621. { 0, NULL, 0, 0 }
  2622. };
  2623. static struct intr_info pcie_port_intr_info[] = {
  2624. { TPCP, "TXPC array parity error", -1, 1 },
  2625. { TNPP, "TXNP array parity error", -1, 1 },
  2626. { TFTP, "TXFT array parity error", -1, 1 },
  2627. { TCAP, "TXCA array parity error", -1, 1 },
  2628. { TCIP, "TXCIF array parity error", -1, 1 },
  2629. { RCAP, "RXCA array parity error", -1, 1 },
  2630. { OTDD, "outbound request TLP discarded", -1, 1 },
  2631. { RDPE, "Rx data parity error", -1, 1 },
  2632. { TDUE, "Tx uncorrectable data error", -1, 1 },
  2633. { 0, NULL, 0, 0 }
  2634. };
  2635. static struct intr_info pcie_intr_info[] = {
  2636. { MSIADDRLPERR, "MSI AddrL parity error", -1, 1 },
  2637. { MSIADDRHPERR, "MSI AddrH parity error", -1, 1 },
  2638. { MSIDATAPERR, "MSI data parity error", -1, 1 },
  2639. { MSIXADDRLPERR, "MSI-X AddrL parity error", -1, 1 },
  2640. { MSIXADDRHPERR, "MSI-X AddrH parity error", -1, 1 },
  2641. { MSIXDATAPERR, "MSI-X data parity error", -1, 1 },
  2642. { MSIXDIPERR, "MSI-X DI parity error", -1, 1 },
  2643. { PIOCPLPERR, "PCI PIO completion FIFO parity error", -1, 1 },
  2644. { PIOREQPERR, "PCI PIO request FIFO parity error", -1, 1 },
  2645. { TARTAGPERR, "PCI PCI target tag FIFO parity error", -1, 1 },
  2646. { CCNTPERR, "PCI CMD channel count parity error", -1, 1 },
  2647. { CREQPERR, "PCI CMD channel request parity error", -1, 1 },
  2648. { CRSPPERR, "PCI CMD channel response parity error", -1, 1 },
  2649. { DCNTPERR, "PCI DMA channel count parity error", -1, 1 },
  2650. { DREQPERR, "PCI DMA channel request parity error", -1, 1 },
  2651. { DRSPPERR, "PCI DMA channel response parity error", -1, 1 },
  2652. { HCNTPERR, "PCI HMA channel count parity error", -1, 1 },
  2653. { HREQPERR, "PCI HMA channel request parity error", -1, 1 },
  2654. { HRSPPERR, "PCI HMA channel response parity error", -1, 1 },
  2655. { CFGSNPPERR, "PCI config snoop FIFO parity error", -1, 1 },
  2656. { FIDPERR, "PCI FID parity error", -1, 1 },
  2657. { INTXCLRPERR, "PCI INTx clear parity error", -1, 1 },
  2658. { MATAGPERR, "PCI MA tag parity error", -1, 1 },
  2659. { PIOTAGPERR, "PCI PIO tag parity error", -1, 1 },
  2660. { RXCPLPERR, "PCI Rx completion parity error", -1, 1 },
  2661. { RXWRPERR, "PCI Rx write parity error", -1, 1 },
  2662. { RPLPERR, "PCI replay buffer parity error", -1, 1 },
  2663. { PCIESINT, "PCI core secondary fault", -1, 1 },
  2664. { PCIEPINT, "PCI core primary fault", -1, 1 },
  2665. { UNXSPLCPLERR, "PCI unexpected split completion error", -1,
  2666. 0 },
  2667. { 0, NULL, 0, 0 }
  2668. };
  2669. int fat;
  2670. fat = csio_handle_intr_status(hw,
  2671. PCIE_CORE_UTL_SYSTEM_BUS_AGENT_STATUS,
  2672. sysbus_intr_info) +
  2673. csio_handle_intr_status(hw,
  2674. PCIE_CORE_UTL_PCI_EXPRESS_PORT_STATUS,
  2675. pcie_port_intr_info) +
  2676. csio_handle_intr_status(hw, PCIE_INT_CAUSE, pcie_intr_info);
  2677. if (fat)
  2678. csio_hw_fatal_err(hw);
  2679. }
  2680. /*
  2681. * TP interrupt handler.
  2682. */
  2683. static void csio_tp_intr_handler(struct csio_hw *hw)
  2684. {
  2685. static struct intr_info tp_intr_info[] = {
  2686. { 0x3fffffff, "TP parity error", -1, 1 },
  2687. { FLMTXFLSTEMPTY, "TP out of Tx pages", -1, 1 },
  2688. { 0, NULL, 0, 0 }
  2689. };
  2690. if (csio_handle_intr_status(hw, TP_INT_CAUSE, tp_intr_info))
  2691. csio_hw_fatal_err(hw);
  2692. }
  2693. /*
  2694. * SGE interrupt handler.
  2695. */
  2696. static void csio_sge_intr_handler(struct csio_hw *hw)
  2697. {
  2698. uint64_t v;
  2699. static struct intr_info sge_intr_info[] = {
  2700. { ERR_CPL_EXCEED_IQE_SIZE,
  2701. "SGE received CPL exceeding IQE size", -1, 1 },
  2702. { ERR_INVALID_CIDX_INC,
  2703. "SGE GTS CIDX increment too large", -1, 0 },
  2704. { ERR_CPL_OPCODE_0, "SGE received 0-length CPL", -1, 0 },
  2705. { ERR_DROPPED_DB, "SGE doorbell dropped", -1, 0 },
  2706. { ERR_DATA_CPL_ON_HIGH_QID1 | ERR_DATA_CPL_ON_HIGH_QID0,
  2707. "SGE IQID > 1023 received CPL for FL", -1, 0 },
  2708. { ERR_BAD_DB_PIDX3, "SGE DBP 3 pidx increment too large", -1,
  2709. 0 },
  2710. { ERR_BAD_DB_PIDX2, "SGE DBP 2 pidx increment too large", -1,
  2711. 0 },
  2712. { ERR_BAD_DB_PIDX1, "SGE DBP 1 pidx increment too large", -1,
  2713. 0 },
  2714. { ERR_BAD_DB_PIDX0, "SGE DBP 0 pidx increment too large", -1,
  2715. 0 },
  2716. { ERR_ING_CTXT_PRIO,
  2717. "SGE too many priority ingress contexts", -1, 0 },
  2718. { ERR_EGR_CTXT_PRIO,
  2719. "SGE too many priority egress contexts", -1, 0 },
  2720. { INGRESS_SIZE_ERR, "SGE illegal ingress QID", -1, 0 },
  2721. { EGRESS_SIZE_ERR, "SGE illegal egress QID", -1, 0 },
  2722. { 0, NULL, 0, 0 }
  2723. };
  2724. v = (uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE1) |
  2725. ((uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE2) << 32);
  2726. if (v) {
  2727. csio_fatal(hw, "SGE parity error (%#llx)\n",
  2728. (unsigned long long)v);
  2729. csio_wr_reg32(hw, (uint32_t)(v & 0xFFFFFFFF),
  2730. SGE_INT_CAUSE1);
  2731. csio_wr_reg32(hw, (uint32_t)(v >> 32), SGE_INT_CAUSE2);
  2732. }
  2733. v |= csio_handle_intr_status(hw, SGE_INT_CAUSE3, sge_intr_info);
  2734. if (csio_handle_intr_status(hw, SGE_INT_CAUSE3, sge_intr_info) ||
  2735. v != 0)
  2736. csio_hw_fatal_err(hw);
  2737. }
  2738. #define CIM_OBQ_INTR (OBQULP0PARERR | OBQULP1PARERR | OBQULP2PARERR |\
  2739. OBQULP3PARERR | OBQSGEPARERR | OBQNCSIPARERR)
  2740. #define CIM_IBQ_INTR (IBQTP0PARERR | IBQTP1PARERR | IBQULPPARERR |\
  2741. IBQSGEHIPARERR | IBQSGELOPARERR | IBQNCSIPARERR)
  2742. /*
  2743. * CIM interrupt handler.
  2744. */
  2745. static void csio_cim_intr_handler(struct csio_hw *hw)
  2746. {
  2747. static struct intr_info cim_intr_info[] = {
  2748. { PREFDROPINT, "CIM control register prefetch drop", -1, 1 },
  2749. { CIM_OBQ_INTR, "CIM OBQ parity error", -1, 1 },
  2750. { CIM_IBQ_INTR, "CIM IBQ parity error", -1, 1 },
  2751. { MBUPPARERR, "CIM mailbox uP parity error", -1, 1 },
  2752. { MBHOSTPARERR, "CIM mailbox host parity error", -1, 1 },
  2753. { TIEQINPARERRINT, "CIM TIEQ outgoing parity error", -1, 1 },
  2754. { TIEQOUTPARERRINT, "CIM TIEQ incoming parity error", -1, 1 },
  2755. { 0, NULL, 0, 0 }
  2756. };
  2757. static struct intr_info cim_upintr_info[] = {
  2758. { RSVDSPACEINT, "CIM reserved space access", -1, 1 },
  2759. { ILLTRANSINT, "CIM illegal transaction", -1, 1 },
  2760. { ILLWRINT, "CIM illegal write", -1, 1 },
  2761. { ILLRDINT, "CIM illegal read", -1, 1 },
  2762. { ILLRDBEINT, "CIM illegal read BE", -1, 1 },
  2763. { ILLWRBEINT, "CIM illegal write BE", -1, 1 },
  2764. { SGLRDBOOTINT, "CIM single read from boot space", -1, 1 },
  2765. { SGLWRBOOTINT, "CIM single write to boot space", -1, 1 },
  2766. { BLKWRBOOTINT, "CIM block write to boot space", -1, 1 },
  2767. { SGLRDFLASHINT, "CIM single read from flash space", -1, 1 },
  2768. { SGLWRFLASHINT, "CIM single write to flash space", -1, 1 },
  2769. { BLKWRFLASHINT, "CIM block write to flash space", -1, 1 },
  2770. { SGLRDEEPROMINT, "CIM single EEPROM read", -1, 1 },
  2771. { SGLWREEPROMINT, "CIM single EEPROM write", -1, 1 },
  2772. { BLKRDEEPROMINT, "CIM block EEPROM read", -1, 1 },
  2773. { BLKWREEPROMINT, "CIM block EEPROM write", -1, 1 },
  2774. { SGLRDCTLINT , "CIM single read from CTL space", -1, 1 },
  2775. { SGLWRCTLINT , "CIM single write to CTL space", -1, 1 },
  2776. { BLKRDCTLINT , "CIM block read from CTL space", -1, 1 },
  2777. { BLKWRCTLINT , "CIM block write to CTL space", -1, 1 },
  2778. { SGLRDPLINT , "CIM single read from PL space", -1, 1 },
  2779. { SGLWRPLINT , "CIM single write to PL space", -1, 1 },
  2780. { BLKRDPLINT , "CIM block read from PL space", -1, 1 },
  2781. { BLKWRPLINT , "CIM block write to PL space", -1, 1 },
  2782. { REQOVRLOOKUPINT , "CIM request FIFO overwrite", -1, 1 },
  2783. { RSPOVRLOOKUPINT , "CIM response FIFO overwrite", -1, 1 },
  2784. { TIMEOUTINT , "CIM PIF timeout", -1, 1 },
  2785. { TIMEOUTMAINT , "CIM PIF MA timeout", -1, 1 },
  2786. { 0, NULL, 0, 0 }
  2787. };
  2788. int fat;
  2789. fat = csio_handle_intr_status(hw, CIM_HOST_INT_CAUSE,
  2790. cim_intr_info) +
  2791. csio_handle_intr_status(hw, CIM_HOST_UPACC_INT_CAUSE,
  2792. cim_upintr_info);
  2793. if (fat)
  2794. csio_hw_fatal_err(hw);
  2795. }
  2796. /*
  2797. * ULP RX interrupt handler.
  2798. */
  2799. static void csio_ulprx_intr_handler(struct csio_hw *hw)
  2800. {
  2801. static struct intr_info ulprx_intr_info[] = {
  2802. { 0x1800000, "ULPRX context error", -1, 1 },
  2803. { 0x7fffff, "ULPRX parity error", -1, 1 },
  2804. { 0, NULL, 0, 0 }
  2805. };
  2806. if (csio_handle_intr_status(hw, ULP_RX_INT_CAUSE, ulprx_intr_info))
  2807. csio_hw_fatal_err(hw);
  2808. }
  2809. /*
  2810. * ULP TX interrupt handler.
  2811. */
  2812. static void csio_ulptx_intr_handler(struct csio_hw *hw)
  2813. {
  2814. static struct intr_info ulptx_intr_info[] = {
  2815. { PBL_BOUND_ERR_CH3, "ULPTX channel 3 PBL out of bounds", -1,
  2816. 0 },
  2817. { PBL_BOUND_ERR_CH2, "ULPTX channel 2 PBL out of bounds", -1,
  2818. 0 },
  2819. { PBL_BOUND_ERR_CH1, "ULPTX channel 1 PBL out of bounds", -1,
  2820. 0 },
  2821. { PBL_BOUND_ERR_CH0, "ULPTX channel 0 PBL out of bounds", -1,
  2822. 0 },
  2823. { 0xfffffff, "ULPTX parity error", -1, 1 },
  2824. { 0, NULL, 0, 0 }
  2825. };
  2826. if (csio_handle_intr_status(hw, ULP_TX_INT_CAUSE, ulptx_intr_info))
  2827. csio_hw_fatal_err(hw);
  2828. }
  2829. /*
  2830. * PM TX interrupt handler.
  2831. */
  2832. static void csio_pmtx_intr_handler(struct csio_hw *hw)
  2833. {
  2834. static struct intr_info pmtx_intr_info[] = {
  2835. { PCMD_LEN_OVFL0, "PMTX channel 0 pcmd too large", -1, 1 },
  2836. { PCMD_LEN_OVFL1, "PMTX channel 1 pcmd too large", -1, 1 },
  2837. { PCMD_LEN_OVFL2, "PMTX channel 2 pcmd too large", -1, 1 },
  2838. { ZERO_C_CMD_ERROR, "PMTX 0-length pcmd", -1, 1 },
  2839. { 0xffffff0, "PMTX framing error", -1, 1 },
  2840. { OESPI_PAR_ERROR, "PMTX oespi parity error", -1, 1 },
  2841. { DB_OPTIONS_PAR_ERROR, "PMTX db_options parity error", -1,
  2842. 1 },
  2843. { ICSPI_PAR_ERROR, "PMTX icspi parity error", -1, 1 },
  2844. { C_PCMD_PAR_ERROR, "PMTX c_pcmd parity error", -1, 1},
  2845. { 0, NULL, 0, 0 }
  2846. };
  2847. if (csio_handle_intr_status(hw, PM_TX_INT_CAUSE, pmtx_intr_info))
  2848. csio_hw_fatal_err(hw);
  2849. }
  2850. /*
  2851. * PM RX interrupt handler.
  2852. */
  2853. static void csio_pmrx_intr_handler(struct csio_hw *hw)
  2854. {
  2855. static struct intr_info pmrx_intr_info[] = {
  2856. { ZERO_E_CMD_ERROR, "PMRX 0-length pcmd", -1, 1 },
  2857. { 0x3ffff0, "PMRX framing error", -1, 1 },
  2858. { OCSPI_PAR_ERROR, "PMRX ocspi parity error", -1, 1 },
  2859. { DB_OPTIONS_PAR_ERROR, "PMRX db_options parity error", -1,
  2860. 1 },
  2861. { IESPI_PAR_ERROR, "PMRX iespi parity error", -1, 1 },
  2862. { E_PCMD_PAR_ERROR, "PMRX e_pcmd parity error", -1, 1},
  2863. { 0, NULL, 0, 0 }
  2864. };
  2865. if (csio_handle_intr_status(hw, PM_RX_INT_CAUSE, pmrx_intr_info))
  2866. csio_hw_fatal_err(hw);
  2867. }
  2868. /*
  2869. * CPL switch interrupt handler.
  2870. */
  2871. static void csio_cplsw_intr_handler(struct csio_hw *hw)
  2872. {
  2873. static struct intr_info cplsw_intr_info[] = {
  2874. { CIM_OP_MAP_PERR, "CPLSW CIM op_map parity error", -1, 1 },
  2875. { CIM_OVFL_ERROR, "CPLSW CIM overflow", -1, 1 },
  2876. { TP_FRAMING_ERROR, "CPLSW TP framing error", -1, 1 },
  2877. { SGE_FRAMING_ERROR, "CPLSW SGE framing error", -1, 1 },
  2878. { CIM_FRAMING_ERROR, "CPLSW CIM framing error", -1, 1 },
  2879. { ZERO_SWITCH_ERROR, "CPLSW no-switch error", -1, 1 },
  2880. { 0, NULL, 0, 0 }
  2881. };
  2882. if (csio_handle_intr_status(hw, CPL_INTR_CAUSE, cplsw_intr_info))
  2883. csio_hw_fatal_err(hw);
  2884. }
  2885. /*
  2886. * LE interrupt handler.
  2887. */
  2888. static void csio_le_intr_handler(struct csio_hw *hw)
  2889. {
  2890. static struct intr_info le_intr_info[] = {
  2891. { LIPMISS, "LE LIP miss", -1, 0 },
  2892. { LIP0, "LE 0 LIP error", -1, 0 },
  2893. { PARITYERR, "LE parity error", -1, 1 },
  2894. { UNKNOWNCMD, "LE unknown command", -1, 1 },
  2895. { REQQPARERR, "LE request queue parity error", -1, 1 },
  2896. { 0, NULL, 0, 0 }
  2897. };
  2898. if (csio_handle_intr_status(hw, LE_DB_INT_CAUSE, le_intr_info))
  2899. csio_hw_fatal_err(hw);
  2900. }
  2901. /*
  2902. * MPS interrupt handler.
  2903. */
  2904. static void csio_mps_intr_handler(struct csio_hw *hw)
  2905. {
  2906. static struct intr_info mps_rx_intr_info[] = {
  2907. { 0xffffff, "MPS Rx parity error", -1, 1 },
  2908. { 0, NULL, 0, 0 }
  2909. };
  2910. static struct intr_info mps_tx_intr_info[] = {
  2911. { TPFIFO, "MPS Tx TP FIFO parity error", -1, 1 },
  2912. { NCSIFIFO, "MPS Tx NC-SI FIFO parity error", -1, 1 },
  2913. { TXDATAFIFO, "MPS Tx data FIFO parity error", -1, 1 },
  2914. { TXDESCFIFO, "MPS Tx desc FIFO parity error", -1, 1 },
  2915. { BUBBLE, "MPS Tx underflow", -1, 1 },
  2916. { SECNTERR, "MPS Tx SOP/EOP error", -1, 1 },
  2917. { FRMERR, "MPS Tx framing error", -1, 1 },
  2918. { 0, NULL, 0, 0 }
  2919. };
  2920. static struct intr_info mps_trc_intr_info[] = {
  2921. { FILTMEM, "MPS TRC filter parity error", -1, 1 },
  2922. { PKTFIFO, "MPS TRC packet FIFO parity error", -1, 1 },
  2923. { MISCPERR, "MPS TRC misc parity error", -1, 1 },
  2924. { 0, NULL, 0, 0 }
  2925. };
  2926. static struct intr_info mps_stat_sram_intr_info[] = {
  2927. { 0x1fffff, "MPS statistics SRAM parity error", -1, 1 },
  2928. { 0, NULL, 0, 0 }
  2929. };
  2930. static struct intr_info mps_stat_tx_intr_info[] = {
  2931. { 0xfffff, "MPS statistics Tx FIFO parity error", -1, 1 },
  2932. { 0, NULL, 0, 0 }
  2933. };
  2934. static struct intr_info mps_stat_rx_intr_info[] = {
  2935. { 0xffffff, "MPS statistics Rx FIFO parity error", -1, 1 },
  2936. { 0, NULL, 0, 0 }
  2937. };
  2938. static struct intr_info mps_cls_intr_info[] = {
  2939. { MATCHSRAM, "MPS match SRAM parity error", -1, 1 },
  2940. { MATCHTCAM, "MPS match TCAM parity error", -1, 1 },
  2941. { HASHSRAM, "MPS hash SRAM parity error", -1, 1 },
  2942. { 0, NULL, 0, 0 }
  2943. };
  2944. int fat;
  2945. fat = csio_handle_intr_status(hw, MPS_RX_PERR_INT_CAUSE,
  2946. mps_rx_intr_info) +
  2947. csio_handle_intr_status(hw, MPS_TX_INT_CAUSE,
  2948. mps_tx_intr_info) +
  2949. csio_handle_intr_status(hw, MPS_TRC_INT_CAUSE,
  2950. mps_trc_intr_info) +
  2951. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_SRAM,
  2952. mps_stat_sram_intr_info) +
  2953. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_TX_FIFO,
  2954. mps_stat_tx_intr_info) +
  2955. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_RX_FIFO,
  2956. mps_stat_rx_intr_info) +
  2957. csio_handle_intr_status(hw, MPS_CLS_INT_CAUSE,
  2958. mps_cls_intr_info);
  2959. csio_wr_reg32(hw, 0, MPS_INT_CAUSE);
  2960. csio_rd_reg32(hw, MPS_INT_CAUSE); /* flush */
  2961. if (fat)
  2962. csio_hw_fatal_err(hw);
  2963. }
  2964. #define MEM_INT_MASK (PERR_INT_CAUSE | ECC_CE_INT_CAUSE | ECC_UE_INT_CAUSE)
  2965. /*
  2966. * EDC/MC interrupt handler.
  2967. */
  2968. static void csio_mem_intr_handler(struct csio_hw *hw, int idx)
  2969. {
  2970. static const char name[3][5] = { "EDC0", "EDC1", "MC" };
  2971. unsigned int addr, cnt_addr, v;
  2972. if (idx <= MEM_EDC1) {
  2973. addr = EDC_REG(EDC_INT_CAUSE, idx);
  2974. cnt_addr = EDC_REG(EDC_ECC_STATUS, idx);
  2975. } else {
  2976. addr = MC_INT_CAUSE;
  2977. cnt_addr = MC_ECC_STATUS;
  2978. }
  2979. v = csio_rd_reg32(hw, addr) & MEM_INT_MASK;
  2980. if (v & PERR_INT_CAUSE)
  2981. csio_fatal(hw, "%s FIFO parity error\n", name[idx]);
  2982. if (v & ECC_CE_INT_CAUSE) {
  2983. uint32_t cnt = ECC_CECNT_GET(csio_rd_reg32(hw, cnt_addr));
  2984. csio_wr_reg32(hw, ECC_CECNT_MASK, cnt_addr);
  2985. csio_warn(hw, "%u %s correctable ECC data error%s\n",
  2986. cnt, name[idx], cnt > 1 ? "s" : "");
  2987. }
  2988. if (v & ECC_UE_INT_CAUSE)
  2989. csio_fatal(hw, "%s uncorrectable ECC data error\n", name[idx]);
  2990. csio_wr_reg32(hw, v, addr);
  2991. if (v & (PERR_INT_CAUSE | ECC_UE_INT_CAUSE))
  2992. csio_hw_fatal_err(hw);
  2993. }
  2994. /*
  2995. * MA interrupt handler.
  2996. */
  2997. static void csio_ma_intr_handler(struct csio_hw *hw)
  2998. {
  2999. uint32_t v, status = csio_rd_reg32(hw, MA_INT_CAUSE);
  3000. if (status & MEM_PERR_INT_CAUSE)
  3001. csio_fatal(hw, "MA parity error, parity status %#x\n",
  3002. csio_rd_reg32(hw, MA_PARITY_ERROR_STATUS));
  3003. if (status & MEM_WRAP_INT_CAUSE) {
  3004. v = csio_rd_reg32(hw, MA_INT_WRAP_STATUS);
  3005. csio_fatal(hw,
  3006. "MA address wrap-around error by client %u to address %#x\n",
  3007. MEM_WRAP_CLIENT_NUM_GET(v), MEM_WRAP_ADDRESS_GET(v) << 4);
  3008. }
  3009. csio_wr_reg32(hw, status, MA_INT_CAUSE);
  3010. csio_hw_fatal_err(hw);
  3011. }
  3012. /*
  3013. * SMB interrupt handler.
  3014. */
  3015. static void csio_smb_intr_handler(struct csio_hw *hw)
  3016. {
  3017. static struct intr_info smb_intr_info[] = {
  3018. { MSTTXFIFOPARINT, "SMB master Tx FIFO parity error", -1, 1 },
  3019. { MSTRXFIFOPARINT, "SMB master Rx FIFO parity error", -1, 1 },
  3020. { SLVFIFOPARINT, "SMB slave FIFO parity error", -1, 1 },
  3021. { 0, NULL, 0, 0 }
  3022. };
  3023. if (csio_handle_intr_status(hw, SMB_INT_CAUSE, smb_intr_info))
  3024. csio_hw_fatal_err(hw);
  3025. }
  3026. /*
  3027. * NC-SI interrupt handler.
  3028. */
  3029. static void csio_ncsi_intr_handler(struct csio_hw *hw)
  3030. {
  3031. static struct intr_info ncsi_intr_info[] = {
  3032. { CIM_DM_PRTY_ERR, "NC-SI CIM parity error", -1, 1 },
  3033. { MPS_DM_PRTY_ERR, "NC-SI MPS parity error", -1, 1 },
  3034. { TXFIFO_PRTY_ERR, "NC-SI Tx FIFO parity error", -1, 1 },
  3035. { RXFIFO_PRTY_ERR, "NC-SI Rx FIFO parity error", -1, 1 },
  3036. { 0, NULL, 0, 0 }
  3037. };
  3038. if (csio_handle_intr_status(hw, NCSI_INT_CAUSE, ncsi_intr_info))
  3039. csio_hw_fatal_err(hw);
  3040. }
  3041. /*
  3042. * XGMAC interrupt handler.
  3043. */
  3044. static void csio_xgmac_intr_handler(struct csio_hw *hw, int port)
  3045. {
  3046. uint32_t v = csio_rd_reg32(hw, PORT_REG(port, XGMAC_PORT_INT_CAUSE));
  3047. v &= TXFIFO_PRTY_ERR | RXFIFO_PRTY_ERR;
  3048. if (!v)
  3049. return;
  3050. if (v & TXFIFO_PRTY_ERR)
  3051. csio_fatal(hw, "XGMAC %d Tx FIFO parity error\n", port);
  3052. if (v & RXFIFO_PRTY_ERR)
  3053. csio_fatal(hw, "XGMAC %d Rx FIFO parity error\n", port);
  3054. csio_wr_reg32(hw, v, PORT_REG(port, XGMAC_PORT_INT_CAUSE));
  3055. csio_hw_fatal_err(hw);
  3056. }
  3057. /*
  3058. * PL interrupt handler.
  3059. */
  3060. static void csio_pl_intr_handler(struct csio_hw *hw)
  3061. {
  3062. static struct intr_info pl_intr_info[] = {
  3063. { FATALPERR, "T4 fatal parity error", -1, 1 },
  3064. { PERRVFID, "PL VFID_MAP parity error", -1, 1 },
  3065. { 0, NULL, 0, 0 }
  3066. };
  3067. if (csio_handle_intr_status(hw, PL_PL_INT_CAUSE, pl_intr_info))
  3068. csio_hw_fatal_err(hw);
  3069. }
  3070. /*
  3071. * csio_hw_slow_intr_handler - control path interrupt handler
  3072. * @hw: HW module
  3073. *
  3074. * Interrupt handler for non-data global interrupt events, e.g., errors.
  3075. * The designation 'slow' is because it involves register reads, while
  3076. * data interrupts typically don't involve any MMIOs.
  3077. */
  3078. int
  3079. csio_hw_slow_intr_handler(struct csio_hw *hw)
  3080. {
  3081. uint32_t cause = csio_rd_reg32(hw, PL_INT_CAUSE);
  3082. if (!(cause & CSIO_GLBL_INTR_MASK)) {
  3083. CSIO_INC_STATS(hw, n_plint_unexp);
  3084. return 0;
  3085. }
  3086. csio_dbg(hw, "Slow interrupt! cause: 0x%x\n", cause);
  3087. CSIO_INC_STATS(hw, n_plint_cnt);
  3088. if (cause & CIM)
  3089. csio_cim_intr_handler(hw);
  3090. if (cause & MPS)
  3091. csio_mps_intr_handler(hw);
  3092. if (cause & NCSI)
  3093. csio_ncsi_intr_handler(hw);
  3094. if (cause & PL)
  3095. csio_pl_intr_handler(hw);
  3096. if (cause & SMB)
  3097. csio_smb_intr_handler(hw);
  3098. if (cause & XGMAC0)
  3099. csio_xgmac_intr_handler(hw, 0);
  3100. if (cause & XGMAC1)
  3101. csio_xgmac_intr_handler(hw, 1);
  3102. if (cause & XGMAC_KR0)
  3103. csio_xgmac_intr_handler(hw, 2);
  3104. if (cause & XGMAC_KR1)
  3105. csio_xgmac_intr_handler(hw, 3);
  3106. if (cause & PCIE)
  3107. csio_pcie_intr_handler(hw);
  3108. if (cause & MC)
  3109. csio_mem_intr_handler(hw, MEM_MC);
  3110. if (cause & EDC0)
  3111. csio_mem_intr_handler(hw, MEM_EDC0);
  3112. if (cause & EDC1)
  3113. csio_mem_intr_handler(hw, MEM_EDC1);
  3114. if (cause & LE)
  3115. csio_le_intr_handler(hw);
  3116. if (cause & TP)
  3117. csio_tp_intr_handler(hw);
  3118. if (cause & MA)
  3119. csio_ma_intr_handler(hw);
  3120. if (cause & PM_TX)
  3121. csio_pmtx_intr_handler(hw);
  3122. if (cause & PM_RX)
  3123. csio_pmrx_intr_handler(hw);
  3124. if (cause & ULP_RX)
  3125. csio_ulprx_intr_handler(hw);
  3126. if (cause & CPL_SWITCH)
  3127. csio_cplsw_intr_handler(hw);
  3128. if (cause & SGE)
  3129. csio_sge_intr_handler(hw);
  3130. if (cause & ULP_TX)
  3131. csio_ulptx_intr_handler(hw);
  3132. /* Clear the interrupts just processed for which we are the master. */
  3133. csio_wr_reg32(hw, cause & CSIO_GLBL_INTR_MASK, PL_INT_CAUSE);
  3134. csio_rd_reg32(hw, PL_INT_CAUSE); /* flush */
  3135. return 1;
  3136. }
  3137. /*****************************************************************************
  3138. * HW <--> mailbox interfacing routines.
  3139. ****************************************************************************/
  3140. /*
  3141. * csio_mberr_worker - Worker thread (dpc) for mailbox/error completions
  3142. *
  3143. * @data: Private data pointer.
  3144. *
  3145. * Called from worker thread context.
  3146. */
  3147. static void
  3148. csio_mberr_worker(void *data)
  3149. {
  3150. struct csio_hw *hw = (struct csio_hw *)data;
  3151. struct csio_mbm *mbm = &hw->mbm;
  3152. LIST_HEAD(cbfn_q);
  3153. struct csio_mb *mbp_next;
  3154. int rv;
  3155. del_timer_sync(&mbm->timer);
  3156. spin_lock_irq(&hw->lock);
  3157. if (list_empty(&mbm->cbfn_q)) {
  3158. spin_unlock_irq(&hw->lock);
  3159. return;
  3160. }
  3161. list_splice_tail_init(&mbm->cbfn_q, &cbfn_q);
  3162. mbm->stats.n_cbfnq = 0;
  3163. /* Try to start waiting mailboxes */
  3164. if (!list_empty(&mbm->req_q)) {
  3165. mbp_next = list_first_entry(&mbm->req_q, struct csio_mb, list);
  3166. list_del_init(&mbp_next->list);
  3167. rv = csio_mb_issue(hw, mbp_next);
  3168. if (rv != 0)
  3169. list_add_tail(&mbp_next->list, &mbm->req_q);
  3170. else
  3171. CSIO_DEC_STATS(mbm, n_activeq);
  3172. }
  3173. spin_unlock_irq(&hw->lock);
  3174. /* Now callback completions */
  3175. csio_mb_completions(hw, &cbfn_q);
  3176. }
  3177. /*
  3178. * csio_hw_mb_timer - Top-level Mailbox timeout handler.
  3179. *
  3180. * @data: private data pointer
  3181. *
  3182. **/
  3183. static void
  3184. csio_hw_mb_timer(uintptr_t data)
  3185. {
  3186. struct csio_hw *hw = (struct csio_hw *)data;
  3187. struct csio_mb *mbp = NULL;
  3188. spin_lock_irq(&hw->lock);
  3189. mbp = csio_mb_tmo_handler(hw);
  3190. spin_unlock_irq(&hw->lock);
  3191. /* Call back the function for the timed-out Mailbox */
  3192. if (mbp)
  3193. mbp->mb_cbfn(hw, mbp);
  3194. }
  3195. /*
  3196. * csio_hw_mbm_cleanup - Cleanup Mailbox module.
  3197. * @hw: HW module
  3198. *
  3199. * Called with lock held, should exit with lock held.
  3200. * Cancels outstanding mailboxes (waiting, in-flight) and gathers them
  3201. * into a local queue. Drops lock and calls the completions. Holds
  3202. * lock and returns.
  3203. */
  3204. static void
  3205. csio_hw_mbm_cleanup(struct csio_hw *hw)
  3206. {
  3207. LIST_HEAD(cbfn_q);
  3208. csio_mb_cancel_all(hw, &cbfn_q);
  3209. spin_unlock_irq(&hw->lock);
  3210. csio_mb_completions(hw, &cbfn_q);
  3211. spin_lock_irq(&hw->lock);
  3212. }
  3213. /*****************************************************************************
  3214. * Event handling
  3215. ****************************************************************************/
  3216. int
  3217. csio_enqueue_evt(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
  3218. uint16_t len)
  3219. {
  3220. struct csio_evt_msg *evt_entry = NULL;
  3221. if (type >= CSIO_EVT_MAX)
  3222. return -EINVAL;
  3223. if (len > CSIO_EVT_MSG_SIZE)
  3224. return -EINVAL;
  3225. if (hw->flags & CSIO_HWF_FWEVT_STOP)
  3226. return -EINVAL;
  3227. if (list_empty(&hw->evt_free_q)) {
  3228. csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
  3229. type, len);
  3230. return -ENOMEM;
  3231. }
  3232. evt_entry = list_first_entry(&hw->evt_free_q,
  3233. struct csio_evt_msg, list);
  3234. list_del_init(&evt_entry->list);
  3235. /* copy event msg and queue the event */
  3236. evt_entry->type = type;
  3237. memcpy((void *)evt_entry->data, evt_msg, len);
  3238. list_add_tail(&evt_entry->list, &hw->evt_active_q);
  3239. CSIO_DEC_STATS(hw, n_evt_freeq);
  3240. CSIO_INC_STATS(hw, n_evt_activeq);
  3241. return 0;
  3242. }
  3243. static int
  3244. csio_enqueue_evt_lock(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
  3245. uint16_t len, bool msg_sg)
  3246. {
  3247. struct csio_evt_msg *evt_entry = NULL;
  3248. struct csio_fl_dma_buf *fl_sg;
  3249. uint32_t off = 0;
  3250. unsigned long flags;
  3251. int n, ret = 0;
  3252. if (type >= CSIO_EVT_MAX)
  3253. return -EINVAL;
  3254. if (len > CSIO_EVT_MSG_SIZE)
  3255. return -EINVAL;
  3256. spin_lock_irqsave(&hw->lock, flags);
  3257. if (hw->flags & CSIO_HWF_FWEVT_STOP) {
  3258. ret = -EINVAL;
  3259. goto out;
  3260. }
  3261. if (list_empty(&hw->evt_free_q)) {
  3262. csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
  3263. type, len);
  3264. ret = -ENOMEM;
  3265. goto out;
  3266. }
  3267. evt_entry = list_first_entry(&hw->evt_free_q,
  3268. struct csio_evt_msg, list);
  3269. list_del_init(&evt_entry->list);
  3270. /* copy event msg and queue the event */
  3271. evt_entry->type = type;
  3272. /* If Payload in SG list*/
  3273. if (msg_sg) {
  3274. fl_sg = (struct csio_fl_dma_buf *) evt_msg;
  3275. for (n = 0; (n < CSIO_MAX_FLBUF_PER_IQWR && off < len); n++) {
  3276. memcpy((void *)((uintptr_t)evt_entry->data + off),
  3277. fl_sg->flbufs[n].vaddr,
  3278. fl_sg->flbufs[n].len);
  3279. off += fl_sg->flbufs[n].len;
  3280. }
  3281. } else
  3282. memcpy((void *)evt_entry->data, evt_msg, len);
  3283. list_add_tail(&evt_entry->list, &hw->evt_active_q);
  3284. CSIO_DEC_STATS(hw, n_evt_freeq);
  3285. CSIO_INC_STATS(hw, n_evt_activeq);
  3286. out:
  3287. spin_unlock_irqrestore(&hw->lock, flags);
  3288. return ret;
  3289. }
  3290. static void
  3291. csio_free_evt(struct csio_hw *hw, struct csio_evt_msg *evt_entry)
  3292. {
  3293. if (evt_entry) {
  3294. spin_lock_irq(&hw->lock);
  3295. list_del_init(&evt_entry->list);
  3296. list_add_tail(&evt_entry->list, &hw->evt_free_q);
  3297. CSIO_DEC_STATS(hw, n_evt_activeq);
  3298. CSIO_INC_STATS(hw, n_evt_freeq);
  3299. spin_unlock_irq(&hw->lock);
  3300. }
  3301. }
  3302. void
  3303. csio_evtq_flush(struct csio_hw *hw)
  3304. {
  3305. uint32_t count;
  3306. count = 30;
  3307. while (hw->flags & CSIO_HWF_FWEVT_PENDING && count--) {
  3308. spin_unlock_irq(&hw->lock);
  3309. msleep(2000);
  3310. spin_lock_irq(&hw->lock);
  3311. }
  3312. CSIO_DB_ASSERT(!(hw->flags & CSIO_HWF_FWEVT_PENDING));
  3313. }
  3314. static void
  3315. csio_evtq_stop(struct csio_hw *hw)
  3316. {
  3317. hw->flags |= CSIO_HWF_FWEVT_STOP;
  3318. }
  3319. static void
  3320. csio_evtq_start(struct csio_hw *hw)
  3321. {
  3322. hw->flags &= ~CSIO_HWF_FWEVT_STOP;
  3323. }
  3324. static void
  3325. csio_evtq_cleanup(struct csio_hw *hw)
  3326. {
  3327. struct list_head *evt_entry, *next_entry;
  3328. /* Release outstanding events from activeq to freeq*/
  3329. if (!list_empty(&hw->evt_active_q))
  3330. list_splice_tail_init(&hw->evt_active_q, &hw->evt_free_q);
  3331. hw->stats.n_evt_activeq = 0;
  3332. hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
  3333. /* Freeup event entry */
  3334. list_for_each_safe(evt_entry, next_entry, &hw->evt_free_q) {
  3335. kfree(evt_entry);
  3336. CSIO_DEC_STATS(hw, n_evt_freeq);
  3337. }
  3338. hw->stats.n_evt_freeq = 0;
  3339. }
  3340. static void
  3341. csio_process_fwevtq_entry(struct csio_hw *hw, void *wr, uint32_t len,
  3342. struct csio_fl_dma_buf *flb, void *priv)
  3343. {
  3344. __u8 op;
  3345. __be64 *data;
  3346. void *msg = NULL;
  3347. uint32_t msg_len = 0;
  3348. bool msg_sg = 0;
  3349. op = ((struct rss_header *) wr)->opcode;
  3350. if (op == CPL_FW6_PLD) {
  3351. CSIO_INC_STATS(hw, n_cpl_fw6_pld);
  3352. if (!flb || !flb->totlen) {
  3353. CSIO_INC_STATS(hw, n_cpl_unexp);
  3354. return;
  3355. }
  3356. msg = (void *) flb;
  3357. msg_len = flb->totlen;
  3358. msg_sg = 1;
  3359. data = (__be64 *) msg;
  3360. } else if (op == CPL_FW6_MSG || op == CPL_FW4_MSG) {
  3361. CSIO_INC_STATS(hw, n_cpl_fw6_msg);
  3362. /* skip RSS header */
  3363. msg = (void *)((uintptr_t)wr + sizeof(__be64));
  3364. msg_len = (op == CPL_FW6_MSG) ? sizeof(struct cpl_fw6_msg) :
  3365. sizeof(struct cpl_fw4_msg);
  3366. data = (__be64 *) msg;
  3367. } else {
  3368. csio_warn(hw, "unexpected CPL %#x on FW event queue\n", op);
  3369. CSIO_INC_STATS(hw, n_cpl_unexp);
  3370. return;
  3371. }
  3372. /*
  3373. * Enqueue event to EventQ. Events processing happens
  3374. * in Event worker thread context
  3375. */
  3376. if (csio_enqueue_evt_lock(hw, CSIO_EVT_FW, msg,
  3377. (uint16_t)msg_len, msg_sg))
  3378. CSIO_INC_STATS(hw, n_evt_drop);
  3379. }
  3380. void
  3381. csio_evtq_worker(struct work_struct *work)
  3382. {
  3383. struct csio_hw *hw = container_of(work, struct csio_hw, evtq_work);
  3384. struct list_head *evt_entry, *next_entry;
  3385. LIST_HEAD(evt_q);
  3386. struct csio_evt_msg *evt_msg;
  3387. struct cpl_fw6_msg *msg;
  3388. struct csio_rnode *rn;
  3389. int rv = 0;
  3390. uint8_t evtq_stop = 0;
  3391. csio_dbg(hw, "event worker thread active evts#%d\n",
  3392. hw->stats.n_evt_activeq);
  3393. spin_lock_irq(&hw->lock);
  3394. while (!list_empty(&hw->evt_active_q)) {
  3395. list_splice_tail_init(&hw->evt_active_q, &evt_q);
  3396. spin_unlock_irq(&hw->lock);
  3397. list_for_each_safe(evt_entry, next_entry, &evt_q) {
  3398. evt_msg = (struct csio_evt_msg *) evt_entry;
  3399. /* Drop events if queue is STOPPED */
  3400. spin_lock_irq(&hw->lock);
  3401. if (hw->flags & CSIO_HWF_FWEVT_STOP)
  3402. evtq_stop = 1;
  3403. spin_unlock_irq(&hw->lock);
  3404. if (evtq_stop) {
  3405. CSIO_INC_STATS(hw, n_evt_drop);
  3406. goto free_evt;
  3407. }
  3408. switch (evt_msg->type) {
  3409. case CSIO_EVT_FW:
  3410. msg = (struct cpl_fw6_msg *)(evt_msg->data);
  3411. if ((msg->opcode == CPL_FW6_MSG ||
  3412. msg->opcode == CPL_FW4_MSG) &&
  3413. !msg->type) {
  3414. rv = csio_mb_fwevt_handler(hw,
  3415. msg->data);
  3416. if (!rv)
  3417. break;
  3418. /* Handle any remaining fw events */
  3419. csio_fcoe_fwevt_handler(hw,
  3420. msg->opcode, msg->data);
  3421. } else if (msg->opcode == CPL_FW6_PLD) {
  3422. csio_fcoe_fwevt_handler(hw,
  3423. msg->opcode, msg->data);
  3424. } else {
  3425. csio_warn(hw,
  3426. "Unhandled FW msg op %x type %x\n",
  3427. msg->opcode, msg->type);
  3428. CSIO_INC_STATS(hw, n_evt_drop);
  3429. }
  3430. break;
  3431. case CSIO_EVT_MBX:
  3432. csio_mberr_worker(hw);
  3433. break;
  3434. case CSIO_EVT_DEV_LOSS:
  3435. memcpy(&rn, evt_msg->data, sizeof(rn));
  3436. csio_rnode_devloss_handler(rn);
  3437. break;
  3438. default:
  3439. csio_warn(hw, "Unhandled event %x on evtq\n",
  3440. evt_msg->type);
  3441. CSIO_INC_STATS(hw, n_evt_unexp);
  3442. break;
  3443. }
  3444. free_evt:
  3445. csio_free_evt(hw, evt_msg);
  3446. }
  3447. spin_lock_irq(&hw->lock);
  3448. }
  3449. hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
  3450. spin_unlock_irq(&hw->lock);
  3451. }
  3452. int
  3453. csio_fwevtq_handler(struct csio_hw *hw)
  3454. {
  3455. int rv;
  3456. if (csio_q_iqid(hw, hw->fwevt_iq_idx) == CSIO_MAX_QID) {
  3457. CSIO_INC_STATS(hw, n_int_stray);
  3458. return -EINVAL;
  3459. }
  3460. rv = csio_wr_process_iq_idx(hw, hw->fwevt_iq_idx,
  3461. csio_process_fwevtq_entry, NULL);
  3462. return rv;
  3463. }
  3464. /****************************************************************************
  3465. * Entry points
  3466. ****************************************************************************/
  3467. /* Management module */
  3468. /*
  3469. * csio_mgmt_req_lookup - Lookup the given IO req exist in Active Q.
  3470. * mgmt - mgmt module
  3471. * @io_req - io request
  3472. *
  3473. * Return - 0:if given IO Req exists in active Q.
  3474. * -EINVAL :if lookup fails.
  3475. */
  3476. int
  3477. csio_mgmt_req_lookup(struct csio_mgmtm *mgmtm, struct csio_ioreq *io_req)
  3478. {
  3479. struct list_head *tmp;
  3480. /* Lookup ioreq in the ACTIVEQ */
  3481. list_for_each(tmp, &mgmtm->active_q) {
  3482. if (io_req == (struct csio_ioreq *)tmp)
  3483. return 0;
  3484. }
  3485. return -EINVAL;
  3486. }
  3487. #define ECM_MIN_TMO 1000 /* Minimum timeout value for req */
  3488. /*
  3489. * csio_mgmts_tmo_handler - MGMT IO Timeout handler.
  3490. * @data - Event data.
  3491. *
  3492. * Return - none.
  3493. */
  3494. static void
  3495. csio_mgmt_tmo_handler(uintptr_t data)
  3496. {
  3497. struct csio_mgmtm *mgmtm = (struct csio_mgmtm *) data;
  3498. struct list_head *tmp;
  3499. struct csio_ioreq *io_req;
  3500. csio_dbg(mgmtm->hw, "Mgmt timer invoked!\n");
  3501. spin_lock_irq(&mgmtm->hw->lock);
  3502. list_for_each(tmp, &mgmtm->active_q) {
  3503. io_req = (struct csio_ioreq *) tmp;
  3504. io_req->tmo -= min_t(uint32_t, io_req->tmo, ECM_MIN_TMO);
  3505. if (!io_req->tmo) {
  3506. /* Dequeue the request from retry Q. */
  3507. tmp = csio_list_prev(tmp);
  3508. list_del_init(&io_req->sm.sm_list);
  3509. if (io_req->io_cbfn) {
  3510. /* io_req will be freed by completion handler */
  3511. io_req->wr_status = -ETIMEDOUT;
  3512. io_req->io_cbfn(mgmtm->hw, io_req);
  3513. } else {
  3514. CSIO_DB_ASSERT(0);
  3515. }
  3516. }
  3517. }
  3518. /* If retry queue is not empty, re-arm timer */
  3519. if (!list_empty(&mgmtm->active_q))
  3520. mod_timer(&mgmtm->mgmt_timer,
  3521. jiffies + msecs_to_jiffies(ECM_MIN_TMO));
  3522. spin_unlock_irq(&mgmtm->hw->lock);
  3523. }
  3524. static void
  3525. csio_mgmtm_cleanup(struct csio_mgmtm *mgmtm)
  3526. {
  3527. struct csio_hw *hw = mgmtm->hw;
  3528. struct csio_ioreq *io_req;
  3529. struct list_head *tmp;
  3530. uint32_t count;
  3531. count = 30;
  3532. /* Wait for all outstanding req to complete gracefully */
  3533. while ((!list_empty(&mgmtm->active_q)) && count--) {
  3534. spin_unlock_irq(&hw->lock);
  3535. msleep(2000);
  3536. spin_lock_irq(&hw->lock);
  3537. }
  3538. /* release outstanding req from ACTIVEQ */
  3539. list_for_each(tmp, &mgmtm->active_q) {
  3540. io_req = (struct csio_ioreq *) tmp;
  3541. tmp = csio_list_prev(tmp);
  3542. list_del_init(&io_req->sm.sm_list);
  3543. mgmtm->stats.n_active--;
  3544. if (io_req->io_cbfn) {
  3545. /* io_req will be freed by completion handler */
  3546. io_req->wr_status = -ETIMEDOUT;
  3547. io_req->io_cbfn(mgmtm->hw, io_req);
  3548. }
  3549. }
  3550. }
  3551. /*
  3552. * csio_mgmt_init - Mgmt module init entry point
  3553. * @mgmtsm - mgmt module
  3554. * @hw - HW module
  3555. *
  3556. * Initialize mgmt timer, resource wait queue, active queue,
  3557. * completion q. Allocate Egress and Ingress
  3558. * WR queues and save off the queue index returned by the WR
  3559. * module for future use. Allocate and save off mgmt reqs in the
  3560. * mgmt_req_freelist for future use. Make sure their SM is initialized
  3561. * to uninit state.
  3562. * Returns: 0 - on success
  3563. * -ENOMEM - on error.
  3564. */
  3565. static int
  3566. csio_mgmtm_init(struct csio_mgmtm *mgmtm, struct csio_hw *hw)
  3567. {
  3568. struct timer_list *timer = &mgmtm->mgmt_timer;
  3569. init_timer(timer);
  3570. timer->function = csio_mgmt_tmo_handler;
  3571. timer->data = (unsigned long)mgmtm;
  3572. INIT_LIST_HEAD(&mgmtm->active_q);
  3573. INIT_LIST_HEAD(&mgmtm->cbfn_q);
  3574. mgmtm->hw = hw;
  3575. /*mgmtm->iq_idx = hw->fwevt_iq_idx;*/
  3576. return 0;
  3577. }
  3578. /*
  3579. * csio_mgmtm_exit - MGMT module exit entry point
  3580. * @mgmtsm - mgmt module
  3581. *
  3582. * This function called during MGMT module uninit.
  3583. * Stop timers, free ioreqs allocated.
  3584. * Returns: None
  3585. *
  3586. */
  3587. static void
  3588. csio_mgmtm_exit(struct csio_mgmtm *mgmtm)
  3589. {
  3590. del_timer_sync(&mgmtm->mgmt_timer);
  3591. }
  3592. /**
  3593. * csio_hw_start - Kicks off the HW State machine
  3594. * @hw: Pointer to HW module.
  3595. *
  3596. * It is assumed that the initialization is a synchronous operation.
  3597. * So when we return afer posting the event, the HW SM should be in
  3598. * the ready state, if there were no errors during init.
  3599. */
  3600. int
  3601. csio_hw_start(struct csio_hw *hw)
  3602. {
  3603. spin_lock_irq(&hw->lock);
  3604. csio_post_event(&hw->sm, CSIO_HWE_CFG);
  3605. spin_unlock_irq(&hw->lock);
  3606. if (csio_is_hw_ready(hw))
  3607. return 0;
  3608. else
  3609. return -EINVAL;
  3610. }
  3611. int
  3612. csio_hw_stop(struct csio_hw *hw)
  3613. {
  3614. csio_post_event(&hw->sm, CSIO_HWE_PCI_REMOVE);
  3615. if (csio_is_hw_removing(hw))
  3616. return 0;
  3617. else
  3618. return -EINVAL;
  3619. }
  3620. /* Max reset retries */
  3621. #define CSIO_MAX_RESET_RETRIES 3
  3622. /**
  3623. * csio_hw_reset - Reset the hardware
  3624. * @hw: HW module.
  3625. *
  3626. * Caller should hold lock across this function.
  3627. */
  3628. int
  3629. csio_hw_reset(struct csio_hw *hw)
  3630. {
  3631. if (!csio_is_hw_master(hw))
  3632. return -EPERM;
  3633. if (hw->rst_retries >= CSIO_MAX_RESET_RETRIES) {
  3634. csio_dbg(hw, "Max hw reset attempts reached..");
  3635. return -EINVAL;
  3636. }
  3637. hw->rst_retries++;
  3638. csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET);
  3639. if (csio_is_hw_ready(hw)) {
  3640. hw->rst_retries = 0;
  3641. hw->stats.n_reset_start = jiffies_to_msecs(jiffies);
  3642. return 0;
  3643. } else
  3644. return -EINVAL;
  3645. }
  3646. /*
  3647. * csio_hw_get_device_id - Caches the Adapter's vendor & device id.
  3648. * @hw: HW module.
  3649. */
  3650. static void
  3651. csio_hw_get_device_id(struct csio_hw *hw)
  3652. {
  3653. /* Is the adapter device id cached already ?*/
  3654. if (csio_is_dev_id_cached(hw))
  3655. return;
  3656. /* Get the PCI vendor & device id */
  3657. pci_read_config_word(hw->pdev, PCI_VENDOR_ID,
  3658. &hw->params.pci.vendor_id);
  3659. pci_read_config_word(hw->pdev, PCI_DEVICE_ID,
  3660. &hw->params.pci.device_id);
  3661. csio_dev_id_cached(hw);
  3662. } /* csio_hw_get_device_id */
  3663. /*
  3664. * csio_hw_set_description - Set the model, description of the hw.
  3665. * @hw: HW module.
  3666. * @ven_id: PCI Vendor ID
  3667. * @dev_id: PCI Device ID
  3668. */
  3669. static void
  3670. csio_hw_set_description(struct csio_hw *hw, uint16_t ven_id, uint16_t dev_id)
  3671. {
  3672. uint32_t adap_type, prot_type;
  3673. if (ven_id == CSIO_VENDOR_ID) {
  3674. prot_type = (dev_id & CSIO_ASIC_DEVID_PROTO_MASK);
  3675. adap_type = (dev_id & CSIO_ASIC_DEVID_TYPE_MASK);
  3676. if (prot_type == CSIO_FPGA) {
  3677. memcpy(hw->model_desc,
  3678. csio_fcoe_adapters[13].description, 32);
  3679. } else if (prot_type == CSIO_T4_FCOE_ASIC) {
  3680. memcpy(hw->hw_ver,
  3681. csio_fcoe_adapters[adap_type].model_no, 16);
  3682. memcpy(hw->model_desc,
  3683. csio_fcoe_adapters[adap_type].description, 32);
  3684. } else {
  3685. char tempName[32] = "Chelsio FCoE Controller";
  3686. memcpy(hw->model_desc, tempName, 32);
  3687. CSIO_DB_ASSERT(0);
  3688. }
  3689. }
  3690. } /* csio_hw_set_description */
  3691. /**
  3692. * csio_hw_init - Initialize HW module.
  3693. * @hw: Pointer to HW module.
  3694. *
  3695. * Initialize the members of the HW module.
  3696. */
  3697. int
  3698. csio_hw_init(struct csio_hw *hw)
  3699. {
  3700. int rv = -EINVAL;
  3701. uint32_t i;
  3702. uint16_t ven_id, dev_id;
  3703. struct csio_evt_msg *evt_entry;
  3704. INIT_LIST_HEAD(&hw->sm.sm_list);
  3705. csio_init_state(&hw->sm, csio_hws_uninit);
  3706. spin_lock_init(&hw->lock);
  3707. INIT_LIST_HEAD(&hw->sln_head);
  3708. /* Get the PCI vendor & device id */
  3709. csio_hw_get_device_id(hw);
  3710. strcpy(hw->name, CSIO_HW_NAME);
  3711. /* Set the model & its description */
  3712. ven_id = hw->params.pci.vendor_id;
  3713. dev_id = hw->params.pci.device_id;
  3714. csio_hw_set_description(hw, ven_id, dev_id);
  3715. /* Initialize default log level */
  3716. hw->params.log_level = (uint32_t) csio_dbg_level;
  3717. csio_set_fwevt_intr_idx(hw, -1);
  3718. csio_set_nondata_intr_idx(hw, -1);
  3719. /* Init all the modules: Mailbox, WorkRequest and Transport */
  3720. if (csio_mbm_init(csio_hw_to_mbm(hw), hw, csio_hw_mb_timer))
  3721. goto err;
  3722. rv = csio_wrm_init(csio_hw_to_wrm(hw), hw);
  3723. if (rv)
  3724. goto err_mbm_exit;
  3725. rv = csio_scsim_init(csio_hw_to_scsim(hw), hw);
  3726. if (rv)
  3727. goto err_wrm_exit;
  3728. rv = csio_mgmtm_init(csio_hw_to_mgmtm(hw), hw);
  3729. if (rv)
  3730. goto err_scsim_exit;
  3731. /* Pre-allocate evtq and initialize them */
  3732. INIT_LIST_HEAD(&hw->evt_active_q);
  3733. INIT_LIST_HEAD(&hw->evt_free_q);
  3734. for (i = 0; i < csio_evtq_sz; i++) {
  3735. evt_entry = kzalloc(sizeof(struct csio_evt_msg), GFP_KERNEL);
  3736. if (!evt_entry) {
  3737. csio_err(hw, "Failed to initialize eventq");
  3738. goto err_evtq_cleanup;
  3739. }
  3740. list_add_tail(&evt_entry->list, &hw->evt_free_q);
  3741. CSIO_INC_STATS(hw, n_evt_freeq);
  3742. }
  3743. hw->dev_num = dev_num;
  3744. dev_num++;
  3745. return 0;
  3746. err_evtq_cleanup:
  3747. csio_evtq_cleanup(hw);
  3748. csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
  3749. err_scsim_exit:
  3750. csio_scsim_exit(csio_hw_to_scsim(hw));
  3751. err_wrm_exit:
  3752. csio_wrm_exit(csio_hw_to_wrm(hw), hw);
  3753. err_mbm_exit:
  3754. csio_mbm_exit(csio_hw_to_mbm(hw));
  3755. err:
  3756. return rv;
  3757. }
  3758. /**
  3759. * csio_hw_exit - Un-initialize HW module.
  3760. * @hw: Pointer to HW module.
  3761. *
  3762. */
  3763. void
  3764. csio_hw_exit(struct csio_hw *hw)
  3765. {
  3766. csio_evtq_cleanup(hw);
  3767. csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
  3768. csio_scsim_exit(csio_hw_to_scsim(hw));
  3769. csio_wrm_exit(csio_hw_to_wrm(hw), hw);
  3770. csio_mbm_exit(csio_hw_to_mbm(hw));
  3771. }