csio_hw.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395
  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. return -ENOMEM;
  1838. memcpy((void *)cfg_data, (const void *)cf->data, cf->size);
  1839. if (csio_hw_check_fwconfig(hw, fw_cfg_param) != 0)
  1840. return -EINVAL;
  1841. mtype = FW_PARAMS_PARAM_Y_GET(*fw_cfg_param);
  1842. maddr = FW_PARAMS_PARAM_Z_GET(*fw_cfg_param) << 16;
  1843. ret = csio_memory_write(hw, mtype, maddr,
  1844. cf->size + value_to_add, cfg_data);
  1845. if (ret == 0) {
  1846. csio_info(hw, "config file upgraded to " CSIO_CF_FNAME "\n");
  1847. strncpy(path, "/lib/firmware/" CSIO_CF_FNAME, 64);
  1848. }
  1849. kfree(cfg_data);
  1850. release_firmware(cf);
  1851. return ret;
  1852. }
  1853. /*
  1854. * HW initialization: contact FW, obtain config, perform basic init.
  1855. *
  1856. * If the firmware we're dealing with has Configuration File support, then
  1857. * we use that to perform all configuration -- either using the configuration
  1858. * file stored in flash on the adapter or using a filesystem-local file
  1859. * if available.
  1860. *
  1861. * If we don't have configuration file support in the firmware, then we'll
  1862. * have to set things up the old fashioned way with hard-coded register
  1863. * writes and firmware commands ...
  1864. */
  1865. /*
  1866. * Attempt to initialize the HW via a Firmware Configuration File.
  1867. */
  1868. static int
  1869. csio_hw_use_fwconfig(struct csio_hw *hw, int reset, u32 *fw_cfg_param)
  1870. {
  1871. unsigned int mtype, maddr;
  1872. int rv;
  1873. uint32_t finiver, finicsum, cfcsum;
  1874. int using_flash;
  1875. char path[64];
  1876. /*
  1877. * Reset device if necessary
  1878. */
  1879. if (reset) {
  1880. rv = csio_do_reset(hw, true);
  1881. if (rv != 0)
  1882. goto bye;
  1883. }
  1884. /*
  1885. * If we have a configuration file in host ,
  1886. * then use that. Otherwise, use the configuration file stored
  1887. * in the HW flash ...
  1888. */
  1889. spin_unlock_irq(&hw->lock);
  1890. rv = csio_hw_flash_config(hw, fw_cfg_param, path);
  1891. spin_lock_irq(&hw->lock);
  1892. if (rv != 0) {
  1893. if (rv == -ENOENT) {
  1894. /*
  1895. * config file was not found. Use default
  1896. * config file from flash.
  1897. */
  1898. mtype = FW_MEMTYPE_CF_FLASH;
  1899. maddr = csio_hw_flash_cfg_addr(hw);
  1900. using_flash = 1;
  1901. } else {
  1902. /*
  1903. * we revert back to the hardwired config if
  1904. * flashing failed.
  1905. */
  1906. goto bye;
  1907. }
  1908. } else {
  1909. mtype = FW_PARAMS_PARAM_Y_GET(*fw_cfg_param);
  1910. maddr = FW_PARAMS_PARAM_Z_GET(*fw_cfg_param) << 16;
  1911. using_flash = 0;
  1912. }
  1913. hw->cfg_store = (uint8_t)mtype;
  1914. /*
  1915. * Issue a Capability Configuration command to the firmware to get it
  1916. * to parse the Configuration File.
  1917. */
  1918. rv = csio_hw_fw_config_file(hw, mtype, maddr, &finiver,
  1919. &finicsum, &cfcsum);
  1920. if (rv != 0)
  1921. goto bye;
  1922. hw->cfg_finiver = finiver;
  1923. hw->cfg_finicsum = finicsum;
  1924. hw->cfg_cfcsum = cfcsum;
  1925. hw->cfg_csum_status = true;
  1926. if (finicsum != cfcsum) {
  1927. csio_warn(hw,
  1928. "Config File checksum mismatch: csum=%#x, computed=%#x\n",
  1929. finicsum, cfcsum);
  1930. hw->cfg_csum_status = false;
  1931. }
  1932. /*
  1933. * Note that we're operating with parameters
  1934. * not supplied by the driver, rather than from hard-wired
  1935. * initialization constants buried in the driver.
  1936. */
  1937. hw->flags |= CSIO_HWF_USING_SOFT_PARAMS;
  1938. /* device parameters */
  1939. rv = csio_get_device_params(hw);
  1940. if (rv != 0)
  1941. goto bye;
  1942. /* Configure SGE */
  1943. csio_wr_sge_init(hw);
  1944. /*
  1945. * And finally tell the firmware to initialize itself using the
  1946. * parameters from the Configuration File.
  1947. */
  1948. /* Post event to notify completion of configuration */
  1949. csio_post_event(&hw->sm, CSIO_HWE_INIT);
  1950. csio_info(hw,
  1951. "Firmware Configuration File %s, version %#x, computed checksum %#x\n",
  1952. (using_flash ? "in device FLASH" : path), finiver, cfcsum);
  1953. return 0;
  1954. /*
  1955. * Something bad happened. Return the error ...
  1956. */
  1957. bye:
  1958. hw->flags &= ~CSIO_HWF_USING_SOFT_PARAMS;
  1959. csio_dbg(hw, "Configuration file error %d\n", rv);
  1960. return rv;
  1961. }
  1962. /*
  1963. * Attempt to initialize the adapter via hard-coded, driver supplied
  1964. * parameters ...
  1965. */
  1966. static int
  1967. csio_hw_no_fwconfig(struct csio_hw *hw, int reset)
  1968. {
  1969. int rv;
  1970. /*
  1971. * Reset device if necessary
  1972. */
  1973. if (reset) {
  1974. rv = csio_do_reset(hw, true);
  1975. if (rv != 0)
  1976. goto out;
  1977. }
  1978. /* Get and set device capabilities */
  1979. rv = csio_config_device_caps(hw);
  1980. if (rv != 0)
  1981. goto out;
  1982. /* Config Global RSS command */
  1983. rv = csio_config_global_rss(hw);
  1984. if (rv != 0)
  1985. goto out;
  1986. /* Configure PF/VF capabilities of device */
  1987. rv = csio_config_pfvf(hw);
  1988. if (rv != 0)
  1989. goto out;
  1990. /* device parameters */
  1991. rv = csio_get_device_params(hw);
  1992. if (rv != 0)
  1993. goto out;
  1994. /* Configure SGE */
  1995. csio_wr_sge_init(hw);
  1996. /* Post event to notify completion of configuration */
  1997. csio_post_event(&hw->sm, CSIO_HWE_INIT);
  1998. out:
  1999. return rv;
  2000. }
  2001. /*
  2002. * Returns -EINVAL if attempts to flash the firmware failed
  2003. * else returns 0,
  2004. * if flashing was not attempted because the card had the
  2005. * latest firmware ECANCELED is returned
  2006. */
  2007. static int
  2008. csio_hw_flash_fw(struct csio_hw *hw)
  2009. {
  2010. int ret = -ECANCELED;
  2011. const struct firmware *fw;
  2012. const struct fw_hdr *hdr;
  2013. u32 fw_ver;
  2014. struct pci_dev *pci_dev = hw->pdev;
  2015. struct device *dev = &pci_dev->dev ;
  2016. if (request_firmware(&fw, CSIO_FW_FNAME, dev) < 0) {
  2017. csio_err(hw, "could not find firmware image " CSIO_FW_FNAME
  2018. ",err: %d\n", ret);
  2019. return -EINVAL;
  2020. }
  2021. hdr = (const struct fw_hdr *)fw->data;
  2022. fw_ver = ntohl(hdr->fw_ver);
  2023. if (FW_HDR_FW_VER_MAJOR_GET(fw_ver) != FW_VERSION_MAJOR)
  2024. return -EINVAL; /* wrong major version, won't do */
  2025. /*
  2026. * If the flash FW is unusable or we found something newer, load it.
  2027. */
  2028. if (FW_HDR_FW_VER_MAJOR_GET(hw->fwrev) != FW_VERSION_MAJOR ||
  2029. fw_ver > hw->fwrev) {
  2030. ret = csio_hw_fw_upgrade(hw, hw->pfn, fw->data, fw->size,
  2031. /*force=*/false);
  2032. if (!ret)
  2033. csio_info(hw, "firmware upgraded to version %pI4 from "
  2034. CSIO_FW_FNAME "\n", &hdr->fw_ver);
  2035. else
  2036. csio_err(hw, "firmware upgrade failed! err=%d\n", ret);
  2037. }
  2038. release_firmware(fw);
  2039. return ret;
  2040. }
  2041. /*
  2042. * csio_hw_configure - Configure HW
  2043. * @hw - HW module
  2044. *
  2045. */
  2046. static void
  2047. csio_hw_configure(struct csio_hw *hw)
  2048. {
  2049. int reset = 1;
  2050. int rv;
  2051. u32 param[1];
  2052. rv = csio_hw_dev_ready(hw);
  2053. if (rv != 0) {
  2054. CSIO_INC_STATS(hw, n_err_fatal);
  2055. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  2056. goto out;
  2057. }
  2058. /* HW version */
  2059. hw->chip_ver = (char)csio_rd_reg32(hw, PL_REV);
  2060. /* Needed for FW download */
  2061. rv = csio_hw_get_flash_params(hw);
  2062. if (rv != 0) {
  2063. csio_err(hw, "Failed to get serial flash params rv:%d\n", rv);
  2064. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  2065. goto out;
  2066. }
  2067. /* Set pci completion timeout value to 4 seconds. */
  2068. csio_set_pcie_completion_timeout(hw, 0xd);
  2069. csio_hw_set_mem_win(hw);
  2070. rv = csio_hw_get_fw_version(hw, &hw->fwrev);
  2071. if (rv != 0)
  2072. goto out;
  2073. csio_hw_print_fw_version(hw, "Firmware revision");
  2074. rv = csio_do_hello(hw, &hw->fw_state);
  2075. if (rv != 0) {
  2076. CSIO_INC_STATS(hw, n_err_fatal);
  2077. csio_post_event(&hw->sm, CSIO_HWE_FATAL);
  2078. goto out;
  2079. }
  2080. /* Read vpd */
  2081. rv = csio_hw_get_vpd_params(hw, &hw->vpd);
  2082. if (rv != 0)
  2083. goto out;
  2084. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  2085. rv = csio_hw_check_fw_version(hw);
  2086. if (rv == -EINVAL) {
  2087. /* Do firmware update */
  2088. spin_unlock_irq(&hw->lock);
  2089. rv = csio_hw_flash_fw(hw);
  2090. spin_lock_irq(&hw->lock);
  2091. if (rv == 0) {
  2092. reset = 0;
  2093. /*
  2094. * Note that the chip was reset as part of the
  2095. * firmware upgrade so we don't reset it again
  2096. * below and grab the new firmware version.
  2097. */
  2098. rv = csio_hw_check_fw_version(hw);
  2099. }
  2100. }
  2101. /*
  2102. * If the firmware doesn't support Configuration
  2103. * Files, use the old Driver-based, hard-wired
  2104. * initialization. Otherwise, try using the
  2105. * Configuration File support and fall back to the
  2106. * Driver-based initialization if there's no
  2107. * Configuration File found.
  2108. */
  2109. if (csio_hw_check_fwconfig(hw, param) == 0) {
  2110. rv = csio_hw_use_fwconfig(hw, reset, param);
  2111. if (rv == -ENOENT)
  2112. goto out;
  2113. if (rv != 0) {
  2114. csio_info(hw,
  2115. "No Configuration File present "
  2116. "on adapter. Using hard-wired "
  2117. "configuration parameters.\n");
  2118. rv = csio_hw_no_fwconfig(hw, reset);
  2119. }
  2120. } else {
  2121. rv = csio_hw_no_fwconfig(hw, reset);
  2122. }
  2123. if (rv != 0)
  2124. goto out;
  2125. } else {
  2126. if (hw->fw_state == CSIO_DEV_STATE_INIT) {
  2127. /* device parameters */
  2128. rv = csio_get_device_params(hw);
  2129. if (rv != 0)
  2130. goto out;
  2131. /* Get device capabilities */
  2132. rv = csio_config_device_caps(hw);
  2133. if (rv != 0)
  2134. goto out;
  2135. /* Configure SGE */
  2136. csio_wr_sge_init(hw);
  2137. /* Post event to notify completion of configuration */
  2138. csio_post_event(&hw->sm, CSIO_HWE_INIT);
  2139. goto out;
  2140. }
  2141. } /* if not master */
  2142. out:
  2143. return;
  2144. }
  2145. /*
  2146. * csio_hw_initialize - Initialize HW
  2147. * @hw - HW module
  2148. *
  2149. */
  2150. static void
  2151. csio_hw_initialize(struct csio_hw *hw)
  2152. {
  2153. struct csio_mb *mbp;
  2154. enum fw_retval retval;
  2155. int rv;
  2156. int i;
  2157. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  2158. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  2159. if (!mbp)
  2160. goto out;
  2161. csio_mb_initialize(hw, mbp, CSIO_MB_DEFAULT_TMO, NULL);
  2162. if (csio_mb_issue(hw, mbp)) {
  2163. csio_err(hw, "Issue of FW_INITIALIZE_CMD failed!\n");
  2164. goto free_and_out;
  2165. }
  2166. retval = csio_mb_fw_retval(mbp);
  2167. if (retval != FW_SUCCESS) {
  2168. csio_err(hw, "FW_INITIALIZE_CMD returned 0x%x!\n",
  2169. retval);
  2170. goto free_and_out;
  2171. }
  2172. mempool_free(mbp, hw->mb_mempool);
  2173. }
  2174. rv = csio_get_fcoe_resinfo(hw);
  2175. if (rv != 0) {
  2176. csio_err(hw, "Failed to read fcoe resource info: %d\n", rv);
  2177. goto out;
  2178. }
  2179. spin_unlock_irq(&hw->lock);
  2180. rv = csio_config_queues(hw);
  2181. spin_lock_irq(&hw->lock);
  2182. if (rv != 0) {
  2183. csio_err(hw, "Config of queues failed!: %d\n", rv);
  2184. goto out;
  2185. }
  2186. for (i = 0; i < hw->num_pports; i++)
  2187. hw->pport[i].mod_type = FW_PORT_MOD_TYPE_NA;
  2188. if (csio_is_hw_master(hw) && hw->fw_state != CSIO_DEV_STATE_INIT) {
  2189. rv = csio_enable_ports(hw);
  2190. if (rv != 0) {
  2191. csio_err(hw, "Failed to enable ports: %d\n", rv);
  2192. goto out;
  2193. }
  2194. }
  2195. csio_post_event(&hw->sm, CSIO_HWE_INIT_DONE);
  2196. return;
  2197. free_and_out:
  2198. mempool_free(mbp, hw->mb_mempool);
  2199. out:
  2200. return;
  2201. }
  2202. #define PF_INTR_MASK (PFSW | PFCIM)
  2203. /*
  2204. * csio_hw_intr_enable - Enable HW interrupts
  2205. * @hw: Pointer to HW module.
  2206. *
  2207. * Enable interrupts in HW registers.
  2208. */
  2209. static void
  2210. csio_hw_intr_enable(struct csio_hw *hw)
  2211. {
  2212. uint16_t vec = (uint16_t)csio_get_mb_intr_idx(csio_hw_to_mbm(hw));
  2213. uint32_t pf = SOURCEPF_GET(csio_rd_reg32(hw, PL_WHOAMI));
  2214. uint32_t pl = csio_rd_reg32(hw, PL_INT_ENABLE);
  2215. /*
  2216. * Set aivec for MSI/MSIX. PCIE_PF_CFG.INTXType is set up
  2217. * by FW, so do nothing for INTX.
  2218. */
  2219. if (hw->intr_mode == CSIO_IM_MSIX)
  2220. csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG),
  2221. AIVEC(AIVEC_MASK), vec);
  2222. else if (hw->intr_mode == CSIO_IM_MSI)
  2223. csio_set_reg_field(hw, MYPF_REG(PCIE_PF_CFG),
  2224. AIVEC(AIVEC_MASK), 0);
  2225. csio_wr_reg32(hw, PF_INTR_MASK, MYPF_REG(PL_PF_INT_ENABLE));
  2226. /* Turn on MB interrupts - this will internally flush PIO as well */
  2227. csio_mb_intr_enable(hw);
  2228. /* These are common registers - only a master can modify them */
  2229. if (csio_is_hw_master(hw)) {
  2230. /*
  2231. * Disable the Serial FLASH interrupt, if enabled!
  2232. */
  2233. pl &= (~SF);
  2234. csio_wr_reg32(hw, pl, PL_INT_ENABLE);
  2235. csio_wr_reg32(hw, ERR_CPL_EXCEED_IQE_SIZE |
  2236. EGRESS_SIZE_ERR | ERR_INVALID_CIDX_INC |
  2237. ERR_CPL_OPCODE_0 | ERR_DROPPED_DB |
  2238. ERR_DATA_CPL_ON_HIGH_QID1 |
  2239. ERR_DATA_CPL_ON_HIGH_QID0 | ERR_BAD_DB_PIDX3 |
  2240. ERR_BAD_DB_PIDX2 | ERR_BAD_DB_PIDX1 |
  2241. ERR_BAD_DB_PIDX0 | ERR_ING_CTXT_PRIO |
  2242. ERR_EGR_CTXT_PRIO | INGRESS_SIZE_ERR,
  2243. SGE_INT_ENABLE3);
  2244. csio_set_reg_field(hw, PL_INT_MAP0, 0, 1 << pf);
  2245. }
  2246. hw->flags |= CSIO_HWF_HW_INTR_ENABLED;
  2247. }
  2248. /*
  2249. * csio_hw_intr_disable - Disable HW interrupts
  2250. * @hw: Pointer to HW module.
  2251. *
  2252. * Turn off Mailbox and PCI_PF_CFG interrupts.
  2253. */
  2254. void
  2255. csio_hw_intr_disable(struct csio_hw *hw)
  2256. {
  2257. uint32_t pf = SOURCEPF_GET(csio_rd_reg32(hw, PL_WHOAMI));
  2258. if (!(hw->flags & CSIO_HWF_HW_INTR_ENABLED))
  2259. return;
  2260. hw->flags &= ~CSIO_HWF_HW_INTR_ENABLED;
  2261. csio_wr_reg32(hw, 0, MYPF_REG(PL_PF_INT_ENABLE));
  2262. if (csio_is_hw_master(hw))
  2263. csio_set_reg_field(hw, PL_INT_MAP0, 1 << pf, 0);
  2264. /* Turn off MB interrupts */
  2265. csio_mb_intr_disable(hw);
  2266. }
  2267. static void
  2268. csio_hw_fatal_err(struct csio_hw *hw)
  2269. {
  2270. csio_set_reg_field(hw, SGE_CONTROL, GLOBALENABLE, 0);
  2271. csio_hw_intr_disable(hw);
  2272. /* Do not reset HW, we may need FW state for debugging */
  2273. csio_fatal(hw, "HW Fatal error encountered!\n");
  2274. }
  2275. /*****************************************************************************/
  2276. /* START: HW SM */
  2277. /*****************************************************************************/
  2278. /*
  2279. * csio_hws_uninit - Uninit state
  2280. * @hw - HW module
  2281. * @evt - Event
  2282. *
  2283. */
  2284. static void
  2285. csio_hws_uninit(struct csio_hw *hw, enum csio_hw_ev evt)
  2286. {
  2287. hw->prev_evt = hw->cur_evt;
  2288. hw->cur_evt = evt;
  2289. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2290. switch (evt) {
  2291. case CSIO_HWE_CFG:
  2292. csio_set_state(&hw->sm, csio_hws_configuring);
  2293. csio_hw_configure(hw);
  2294. break;
  2295. default:
  2296. CSIO_INC_STATS(hw, n_evt_unexp);
  2297. break;
  2298. }
  2299. }
  2300. /*
  2301. * csio_hws_configuring - Configuring state
  2302. * @hw - HW module
  2303. * @evt - Event
  2304. *
  2305. */
  2306. static void
  2307. csio_hws_configuring(struct csio_hw *hw, enum csio_hw_ev evt)
  2308. {
  2309. hw->prev_evt = hw->cur_evt;
  2310. hw->cur_evt = evt;
  2311. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2312. switch (evt) {
  2313. case CSIO_HWE_INIT:
  2314. csio_set_state(&hw->sm, csio_hws_initializing);
  2315. csio_hw_initialize(hw);
  2316. break;
  2317. case CSIO_HWE_INIT_DONE:
  2318. csio_set_state(&hw->sm, csio_hws_ready);
  2319. /* Fan out event to all lnode SMs */
  2320. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
  2321. break;
  2322. case CSIO_HWE_FATAL:
  2323. csio_set_state(&hw->sm, csio_hws_uninit);
  2324. break;
  2325. case CSIO_HWE_PCI_REMOVE:
  2326. csio_do_bye(hw);
  2327. break;
  2328. default:
  2329. CSIO_INC_STATS(hw, n_evt_unexp);
  2330. break;
  2331. }
  2332. }
  2333. /*
  2334. * csio_hws_initializing - Initialiazing state
  2335. * @hw - HW module
  2336. * @evt - Event
  2337. *
  2338. */
  2339. static void
  2340. csio_hws_initializing(struct csio_hw *hw, enum csio_hw_ev evt)
  2341. {
  2342. hw->prev_evt = hw->cur_evt;
  2343. hw->cur_evt = evt;
  2344. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2345. switch (evt) {
  2346. case CSIO_HWE_INIT_DONE:
  2347. csio_set_state(&hw->sm, csio_hws_ready);
  2348. /* Fan out event to all lnode SMs */
  2349. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREADY);
  2350. /* Enable interrupts */
  2351. csio_hw_intr_enable(hw);
  2352. break;
  2353. case CSIO_HWE_FATAL:
  2354. csio_set_state(&hw->sm, csio_hws_uninit);
  2355. break;
  2356. case CSIO_HWE_PCI_REMOVE:
  2357. csio_do_bye(hw);
  2358. break;
  2359. default:
  2360. CSIO_INC_STATS(hw, n_evt_unexp);
  2361. break;
  2362. }
  2363. }
  2364. /*
  2365. * csio_hws_ready - Ready state
  2366. * @hw - HW module
  2367. * @evt - Event
  2368. *
  2369. */
  2370. static void
  2371. csio_hws_ready(struct csio_hw *hw, enum csio_hw_ev evt)
  2372. {
  2373. /* Remember the event */
  2374. hw->evtflag = evt;
  2375. hw->prev_evt = hw->cur_evt;
  2376. hw->cur_evt = evt;
  2377. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2378. switch (evt) {
  2379. case CSIO_HWE_HBA_RESET:
  2380. case CSIO_HWE_FW_DLOAD:
  2381. case CSIO_HWE_SUSPEND:
  2382. case CSIO_HWE_PCI_REMOVE:
  2383. case CSIO_HWE_PCIERR_DETECTED:
  2384. csio_set_state(&hw->sm, csio_hws_quiescing);
  2385. /* cleanup all outstanding cmds */
  2386. if (evt == CSIO_HWE_HBA_RESET ||
  2387. evt == CSIO_HWE_PCIERR_DETECTED)
  2388. csio_scsim_cleanup_io(csio_hw_to_scsim(hw), false);
  2389. else
  2390. csio_scsim_cleanup_io(csio_hw_to_scsim(hw), true);
  2391. csio_hw_intr_disable(hw);
  2392. csio_hw_mbm_cleanup(hw);
  2393. csio_evtq_stop(hw);
  2394. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWSTOP);
  2395. csio_evtq_flush(hw);
  2396. csio_mgmtm_cleanup(csio_hw_to_mgmtm(hw));
  2397. csio_post_event(&hw->sm, CSIO_HWE_QUIESCED);
  2398. break;
  2399. case CSIO_HWE_FATAL:
  2400. csio_set_state(&hw->sm, csio_hws_uninit);
  2401. break;
  2402. default:
  2403. CSIO_INC_STATS(hw, n_evt_unexp);
  2404. break;
  2405. }
  2406. }
  2407. /*
  2408. * csio_hws_quiescing - Quiescing state
  2409. * @hw - HW module
  2410. * @evt - Event
  2411. *
  2412. */
  2413. static void
  2414. csio_hws_quiescing(struct csio_hw *hw, enum csio_hw_ev evt)
  2415. {
  2416. hw->prev_evt = hw->cur_evt;
  2417. hw->cur_evt = evt;
  2418. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2419. switch (evt) {
  2420. case CSIO_HWE_QUIESCED:
  2421. switch (hw->evtflag) {
  2422. case CSIO_HWE_FW_DLOAD:
  2423. csio_set_state(&hw->sm, csio_hws_resetting);
  2424. /* Download firmware */
  2425. /* Fall through */
  2426. case CSIO_HWE_HBA_RESET:
  2427. csio_set_state(&hw->sm, csio_hws_resetting);
  2428. /* Start reset of the HBA */
  2429. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWRESET);
  2430. csio_wr_destroy_queues(hw, false);
  2431. csio_do_reset(hw, false);
  2432. csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET_DONE);
  2433. break;
  2434. case CSIO_HWE_PCI_REMOVE:
  2435. csio_set_state(&hw->sm, csio_hws_removing);
  2436. csio_notify_lnodes(hw, CSIO_LN_NOTIFY_HWREMOVE);
  2437. csio_wr_destroy_queues(hw, true);
  2438. /* Now send the bye command */
  2439. csio_do_bye(hw);
  2440. break;
  2441. case CSIO_HWE_SUSPEND:
  2442. csio_set_state(&hw->sm, csio_hws_quiesced);
  2443. break;
  2444. case CSIO_HWE_PCIERR_DETECTED:
  2445. csio_set_state(&hw->sm, csio_hws_pcierr);
  2446. csio_wr_destroy_queues(hw, false);
  2447. break;
  2448. default:
  2449. CSIO_INC_STATS(hw, n_evt_unexp);
  2450. break;
  2451. }
  2452. break;
  2453. default:
  2454. CSIO_INC_STATS(hw, n_evt_unexp);
  2455. break;
  2456. }
  2457. }
  2458. /*
  2459. * csio_hws_quiesced - Quiesced state
  2460. * @hw - HW module
  2461. * @evt - Event
  2462. *
  2463. */
  2464. static void
  2465. csio_hws_quiesced(struct csio_hw *hw, enum csio_hw_ev evt)
  2466. {
  2467. hw->prev_evt = hw->cur_evt;
  2468. hw->cur_evt = evt;
  2469. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2470. switch (evt) {
  2471. case CSIO_HWE_RESUME:
  2472. csio_set_state(&hw->sm, csio_hws_configuring);
  2473. csio_hw_configure(hw);
  2474. break;
  2475. default:
  2476. CSIO_INC_STATS(hw, n_evt_unexp);
  2477. break;
  2478. }
  2479. }
  2480. /*
  2481. * csio_hws_resetting - HW Resetting state
  2482. * @hw - HW module
  2483. * @evt - Event
  2484. *
  2485. */
  2486. static void
  2487. csio_hws_resetting(struct csio_hw *hw, enum csio_hw_ev evt)
  2488. {
  2489. hw->prev_evt = hw->cur_evt;
  2490. hw->cur_evt = evt;
  2491. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2492. switch (evt) {
  2493. case CSIO_HWE_HBA_RESET_DONE:
  2494. csio_evtq_start(hw);
  2495. csio_set_state(&hw->sm, csio_hws_configuring);
  2496. csio_hw_configure(hw);
  2497. break;
  2498. default:
  2499. CSIO_INC_STATS(hw, n_evt_unexp);
  2500. break;
  2501. }
  2502. }
  2503. /*
  2504. * csio_hws_removing - PCI Hotplug removing state
  2505. * @hw - HW module
  2506. * @evt - Event
  2507. *
  2508. */
  2509. static void
  2510. csio_hws_removing(struct csio_hw *hw, enum csio_hw_ev evt)
  2511. {
  2512. hw->prev_evt = hw->cur_evt;
  2513. hw->cur_evt = evt;
  2514. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2515. switch (evt) {
  2516. case CSIO_HWE_HBA_RESET:
  2517. if (!csio_is_hw_master(hw))
  2518. break;
  2519. /*
  2520. * The BYE should have alerady been issued, so we cant
  2521. * use the mailbox interface. Hence we use the PL_RST
  2522. * register directly.
  2523. */
  2524. csio_err(hw, "Resetting HW and waiting 2 seconds...\n");
  2525. csio_wr_reg32(hw, PIORSTMODE | PIORST, PL_RST);
  2526. mdelay(2000);
  2527. break;
  2528. /* Should never receive any new events */
  2529. default:
  2530. CSIO_INC_STATS(hw, n_evt_unexp);
  2531. break;
  2532. }
  2533. }
  2534. /*
  2535. * csio_hws_pcierr - PCI Error state
  2536. * @hw - HW module
  2537. * @evt - Event
  2538. *
  2539. */
  2540. static void
  2541. csio_hws_pcierr(struct csio_hw *hw, enum csio_hw_ev evt)
  2542. {
  2543. hw->prev_evt = hw->cur_evt;
  2544. hw->cur_evt = evt;
  2545. CSIO_INC_STATS(hw, n_evt_sm[evt]);
  2546. switch (evt) {
  2547. case CSIO_HWE_PCIERR_SLOT_RESET:
  2548. csio_evtq_start(hw);
  2549. csio_set_state(&hw->sm, csio_hws_configuring);
  2550. csio_hw_configure(hw);
  2551. break;
  2552. default:
  2553. CSIO_INC_STATS(hw, n_evt_unexp);
  2554. break;
  2555. }
  2556. }
  2557. /*****************************************************************************/
  2558. /* END: HW SM */
  2559. /*****************************************************************************/
  2560. /* Slow path handlers */
  2561. struct intr_info {
  2562. unsigned int mask; /* bits to check in interrupt status */
  2563. const char *msg; /* message to print or NULL */
  2564. short stat_idx; /* stat counter to increment or -1 */
  2565. unsigned short fatal; /* whether the condition reported is fatal */
  2566. };
  2567. /*
  2568. * csio_handle_intr_status - table driven interrupt handler
  2569. * @hw: HW instance
  2570. * @reg: the interrupt status register to process
  2571. * @acts: table of interrupt actions
  2572. *
  2573. * A table driven interrupt handler that applies a set of masks to an
  2574. * interrupt status word and performs the corresponding actions if the
  2575. * interrupts described by the mask have occured. The actions include
  2576. * optionally emitting a warning or alert message. The table is terminated
  2577. * by an entry specifying mask 0. Returns the number of fatal interrupt
  2578. * conditions.
  2579. */
  2580. static int
  2581. csio_handle_intr_status(struct csio_hw *hw, unsigned int reg,
  2582. const struct intr_info *acts)
  2583. {
  2584. int fatal = 0;
  2585. unsigned int mask = 0;
  2586. unsigned int status = csio_rd_reg32(hw, reg);
  2587. for ( ; acts->mask; ++acts) {
  2588. if (!(status & acts->mask))
  2589. continue;
  2590. if (acts->fatal) {
  2591. fatal++;
  2592. csio_fatal(hw, "Fatal %s (0x%x)\n",
  2593. acts->msg, status & acts->mask);
  2594. } else if (acts->msg)
  2595. csio_info(hw, "%s (0x%x)\n",
  2596. acts->msg, status & acts->mask);
  2597. mask |= acts->mask;
  2598. }
  2599. status &= mask;
  2600. if (status) /* clear processed interrupts */
  2601. csio_wr_reg32(hw, status, reg);
  2602. return fatal;
  2603. }
  2604. /*
  2605. * Interrupt handler for the PCIE module.
  2606. */
  2607. static void
  2608. csio_pcie_intr_handler(struct csio_hw *hw)
  2609. {
  2610. static struct intr_info sysbus_intr_info[] = {
  2611. { RNPP, "RXNP array parity error", -1, 1 },
  2612. { RPCP, "RXPC array parity error", -1, 1 },
  2613. { RCIP, "RXCIF array parity error", -1, 1 },
  2614. { RCCP, "Rx completions control array parity error", -1, 1 },
  2615. { RFTP, "RXFT array parity error", -1, 1 },
  2616. { 0, NULL, 0, 0 }
  2617. };
  2618. static struct intr_info pcie_port_intr_info[] = {
  2619. { TPCP, "TXPC array parity error", -1, 1 },
  2620. { TNPP, "TXNP array parity error", -1, 1 },
  2621. { TFTP, "TXFT array parity error", -1, 1 },
  2622. { TCAP, "TXCA array parity error", -1, 1 },
  2623. { TCIP, "TXCIF array parity error", -1, 1 },
  2624. { RCAP, "RXCA array parity error", -1, 1 },
  2625. { OTDD, "outbound request TLP discarded", -1, 1 },
  2626. { RDPE, "Rx data parity error", -1, 1 },
  2627. { TDUE, "Tx uncorrectable data error", -1, 1 },
  2628. { 0, NULL, 0, 0 }
  2629. };
  2630. static struct intr_info pcie_intr_info[] = {
  2631. { MSIADDRLPERR, "MSI AddrL parity error", -1, 1 },
  2632. { MSIADDRHPERR, "MSI AddrH parity error", -1, 1 },
  2633. { MSIDATAPERR, "MSI data parity error", -1, 1 },
  2634. { MSIXADDRLPERR, "MSI-X AddrL parity error", -1, 1 },
  2635. { MSIXADDRHPERR, "MSI-X AddrH parity error", -1, 1 },
  2636. { MSIXDATAPERR, "MSI-X data parity error", -1, 1 },
  2637. { MSIXDIPERR, "MSI-X DI parity error", -1, 1 },
  2638. { PIOCPLPERR, "PCI PIO completion FIFO parity error", -1, 1 },
  2639. { PIOREQPERR, "PCI PIO request FIFO parity error", -1, 1 },
  2640. { TARTAGPERR, "PCI PCI target tag FIFO parity error", -1, 1 },
  2641. { CCNTPERR, "PCI CMD channel count parity error", -1, 1 },
  2642. { CREQPERR, "PCI CMD channel request parity error", -1, 1 },
  2643. { CRSPPERR, "PCI CMD channel response parity error", -1, 1 },
  2644. { DCNTPERR, "PCI DMA channel count parity error", -1, 1 },
  2645. { DREQPERR, "PCI DMA channel request parity error", -1, 1 },
  2646. { DRSPPERR, "PCI DMA channel response parity error", -1, 1 },
  2647. { HCNTPERR, "PCI HMA channel count parity error", -1, 1 },
  2648. { HREQPERR, "PCI HMA channel request parity error", -1, 1 },
  2649. { HRSPPERR, "PCI HMA channel response parity error", -1, 1 },
  2650. { CFGSNPPERR, "PCI config snoop FIFO parity error", -1, 1 },
  2651. { FIDPERR, "PCI FID parity error", -1, 1 },
  2652. { INTXCLRPERR, "PCI INTx clear parity error", -1, 1 },
  2653. { MATAGPERR, "PCI MA tag parity error", -1, 1 },
  2654. { PIOTAGPERR, "PCI PIO tag parity error", -1, 1 },
  2655. { RXCPLPERR, "PCI Rx completion parity error", -1, 1 },
  2656. { RXWRPERR, "PCI Rx write parity error", -1, 1 },
  2657. { RPLPERR, "PCI replay buffer parity error", -1, 1 },
  2658. { PCIESINT, "PCI core secondary fault", -1, 1 },
  2659. { PCIEPINT, "PCI core primary fault", -1, 1 },
  2660. { UNXSPLCPLERR, "PCI unexpected split completion error", -1,
  2661. 0 },
  2662. { 0, NULL, 0, 0 }
  2663. };
  2664. int fat;
  2665. fat = csio_handle_intr_status(hw,
  2666. PCIE_CORE_UTL_SYSTEM_BUS_AGENT_STATUS,
  2667. sysbus_intr_info) +
  2668. csio_handle_intr_status(hw,
  2669. PCIE_CORE_UTL_PCI_EXPRESS_PORT_STATUS,
  2670. pcie_port_intr_info) +
  2671. csio_handle_intr_status(hw, PCIE_INT_CAUSE, pcie_intr_info);
  2672. if (fat)
  2673. csio_hw_fatal_err(hw);
  2674. }
  2675. /*
  2676. * TP interrupt handler.
  2677. */
  2678. static void csio_tp_intr_handler(struct csio_hw *hw)
  2679. {
  2680. static struct intr_info tp_intr_info[] = {
  2681. { 0x3fffffff, "TP parity error", -1, 1 },
  2682. { FLMTXFLSTEMPTY, "TP out of Tx pages", -1, 1 },
  2683. { 0, NULL, 0, 0 }
  2684. };
  2685. if (csio_handle_intr_status(hw, TP_INT_CAUSE, tp_intr_info))
  2686. csio_hw_fatal_err(hw);
  2687. }
  2688. /*
  2689. * SGE interrupt handler.
  2690. */
  2691. static void csio_sge_intr_handler(struct csio_hw *hw)
  2692. {
  2693. uint64_t v;
  2694. static struct intr_info sge_intr_info[] = {
  2695. { ERR_CPL_EXCEED_IQE_SIZE,
  2696. "SGE received CPL exceeding IQE size", -1, 1 },
  2697. { ERR_INVALID_CIDX_INC,
  2698. "SGE GTS CIDX increment too large", -1, 0 },
  2699. { ERR_CPL_OPCODE_0, "SGE received 0-length CPL", -1, 0 },
  2700. { ERR_DROPPED_DB, "SGE doorbell dropped", -1, 0 },
  2701. { ERR_DATA_CPL_ON_HIGH_QID1 | ERR_DATA_CPL_ON_HIGH_QID0,
  2702. "SGE IQID > 1023 received CPL for FL", -1, 0 },
  2703. { ERR_BAD_DB_PIDX3, "SGE DBP 3 pidx increment too large", -1,
  2704. 0 },
  2705. { ERR_BAD_DB_PIDX2, "SGE DBP 2 pidx increment too large", -1,
  2706. 0 },
  2707. { ERR_BAD_DB_PIDX1, "SGE DBP 1 pidx increment too large", -1,
  2708. 0 },
  2709. { ERR_BAD_DB_PIDX0, "SGE DBP 0 pidx increment too large", -1,
  2710. 0 },
  2711. { ERR_ING_CTXT_PRIO,
  2712. "SGE too many priority ingress contexts", -1, 0 },
  2713. { ERR_EGR_CTXT_PRIO,
  2714. "SGE too many priority egress contexts", -1, 0 },
  2715. { INGRESS_SIZE_ERR, "SGE illegal ingress QID", -1, 0 },
  2716. { EGRESS_SIZE_ERR, "SGE illegal egress QID", -1, 0 },
  2717. { 0, NULL, 0, 0 }
  2718. };
  2719. v = (uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE1) |
  2720. ((uint64_t)csio_rd_reg32(hw, SGE_INT_CAUSE2) << 32);
  2721. if (v) {
  2722. csio_fatal(hw, "SGE parity error (%#llx)\n",
  2723. (unsigned long long)v);
  2724. csio_wr_reg32(hw, (uint32_t)(v & 0xFFFFFFFF),
  2725. SGE_INT_CAUSE1);
  2726. csio_wr_reg32(hw, (uint32_t)(v >> 32), SGE_INT_CAUSE2);
  2727. }
  2728. v |= csio_handle_intr_status(hw, SGE_INT_CAUSE3, sge_intr_info);
  2729. if (csio_handle_intr_status(hw, SGE_INT_CAUSE3, sge_intr_info) ||
  2730. v != 0)
  2731. csio_hw_fatal_err(hw);
  2732. }
  2733. #define CIM_OBQ_INTR (OBQULP0PARERR | OBQULP1PARERR | OBQULP2PARERR |\
  2734. OBQULP3PARERR | OBQSGEPARERR | OBQNCSIPARERR)
  2735. #define CIM_IBQ_INTR (IBQTP0PARERR | IBQTP1PARERR | IBQULPPARERR |\
  2736. IBQSGEHIPARERR | IBQSGELOPARERR | IBQNCSIPARERR)
  2737. /*
  2738. * CIM interrupt handler.
  2739. */
  2740. static void csio_cim_intr_handler(struct csio_hw *hw)
  2741. {
  2742. static struct intr_info cim_intr_info[] = {
  2743. { PREFDROPINT, "CIM control register prefetch drop", -1, 1 },
  2744. { CIM_OBQ_INTR, "CIM OBQ parity error", -1, 1 },
  2745. { CIM_IBQ_INTR, "CIM IBQ parity error", -1, 1 },
  2746. { MBUPPARERR, "CIM mailbox uP parity error", -1, 1 },
  2747. { MBHOSTPARERR, "CIM mailbox host parity error", -1, 1 },
  2748. { TIEQINPARERRINT, "CIM TIEQ outgoing parity error", -1, 1 },
  2749. { TIEQOUTPARERRINT, "CIM TIEQ incoming parity error", -1, 1 },
  2750. { 0, NULL, 0, 0 }
  2751. };
  2752. static struct intr_info cim_upintr_info[] = {
  2753. { RSVDSPACEINT, "CIM reserved space access", -1, 1 },
  2754. { ILLTRANSINT, "CIM illegal transaction", -1, 1 },
  2755. { ILLWRINT, "CIM illegal write", -1, 1 },
  2756. { ILLRDINT, "CIM illegal read", -1, 1 },
  2757. { ILLRDBEINT, "CIM illegal read BE", -1, 1 },
  2758. { ILLWRBEINT, "CIM illegal write BE", -1, 1 },
  2759. { SGLRDBOOTINT, "CIM single read from boot space", -1, 1 },
  2760. { SGLWRBOOTINT, "CIM single write to boot space", -1, 1 },
  2761. { BLKWRBOOTINT, "CIM block write to boot space", -1, 1 },
  2762. { SGLRDFLASHINT, "CIM single read from flash space", -1, 1 },
  2763. { SGLWRFLASHINT, "CIM single write to flash space", -1, 1 },
  2764. { BLKWRFLASHINT, "CIM block write to flash space", -1, 1 },
  2765. { SGLRDEEPROMINT, "CIM single EEPROM read", -1, 1 },
  2766. { SGLWREEPROMINT, "CIM single EEPROM write", -1, 1 },
  2767. { BLKRDEEPROMINT, "CIM block EEPROM read", -1, 1 },
  2768. { BLKWREEPROMINT, "CIM block EEPROM write", -1, 1 },
  2769. { SGLRDCTLINT , "CIM single read from CTL space", -1, 1 },
  2770. { SGLWRCTLINT , "CIM single write to CTL space", -1, 1 },
  2771. { BLKRDCTLINT , "CIM block read from CTL space", -1, 1 },
  2772. { BLKWRCTLINT , "CIM block write to CTL space", -1, 1 },
  2773. { SGLRDPLINT , "CIM single read from PL space", -1, 1 },
  2774. { SGLWRPLINT , "CIM single write to PL space", -1, 1 },
  2775. { BLKRDPLINT , "CIM block read from PL space", -1, 1 },
  2776. { BLKWRPLINT , "CIM block write to PL space", -1, 1 },
  2777. { REQOVRLOOKUPINT , "CIM request FIFO overwrite", -1, 1 },
  2778. { RSPOVRLOOKUPINT , "CIM response FIFO overwrite", -1, 1 },
  2779. { TIMEOUTINT , "CIM PIF timeout", -1, 1 },
  2780. { TIMEOUTMAINT , "CIM PIF MA timeout", -1, 1 },
  2781. { 0, NULL, 0, 0 }
  2782. };
  2783. int fat;
  2784. fat = csio_handle_intr_status(hw, CIM_HOST_INT_CAUSE,
  2785. cim_intr_info) +
  2786. csio_handle_intr_status(hw, CIM_HOST_UPACC_INT_CAUSE,
  2787. cim_upintr_info);
  2788. if (fat)
  2789. csio_hw_fatal_err(hw);
  2790. }
  2791. /*
  2792. * ULP RX interrupt handler.
  2793. */
  2794. static void csio_ulprx_intr_handler(struct csio_hw *hw)
  2795. {
  2796. static struct intr_info ulprx_intr_info[] = {
  2797. { 0x1800000, "ULPRX context error", -1, 1 },
  2798. { 0x7fffff, "ULPRX parity error", -1, 1 },
  2799. { 0, NULL, 0, 0 }
  2800. };
  2801. if (csio_handle_intr_status(hw, ULP_RX_INT_CAUSE, ulprx_intr_info))
  2802. csio_hw_fatal_err(hw);
  2803. }
  2804. /*
  2805. * ULP TX interrupt handler.
  2806. */
  2807. static void csio_ulptx_intr_handler(struct csio_hw *hw)
  2808. {
  2809. static struct intr_info ulptx_intr_info[] = {
  2810. { PBL_BOUND_ERR_CH3, "ULPTX channel 3 PBL out of bounds", -1,
  2811. 0 },
  2812. { PBL_BOUND_ERR_CH2, "ULPTX channel 2 PBL out of bounds", -1,
  2813. 0 },
  2814. { PBL_BOUND_ERR_CH1, "ULPTX channel 1 PBL out of bounds", -1,
  2815. 0 },
  2816. { PBL_BOUND_ERR_CH0, "ULPTX channel 0 PBL out of bounds", -1,
  2817. 0 },
  2818. { 0xfffffff, "ULPTX parity error", -1, 1 },
  2819. { 0, NULL, 0, 0 }
  2820. };
  2821. if (csio_handle_intr_status(hw, ULP_TX_INT_CAUSE, ulptx_intr_info))
  2822. csio_hw_fatal_err(hw);
  2823. }
  2824. /*
  2825. * PM TX interrupt handler.
  2826. */
  2827. static void csio_pmtx_intr_handler(struct csio_hw *hw)
  2828. {
  2829. static struct intr_info pmtx_intr_info[] = {
  2830. { PCMD_LEN_OVFL0, "PMTX channel 0 pcmd too large", -1, 1 },
  2831. { PCMD_LEN_OVFL1, "PMTX channel 1 pcmd too large", -1, 1 },
  2832. { PCMD_LEN_OVFL2, "PMTX channel 2 pcmd too large", -1, 1 },
  2833. { ZERO_C_CMD_ERROR, "PMTX 0-length pcmd", -1, 1 },
  2834. { 0xffffff0, "PMTX framing error", -1, 1 },
  2835. { OESPI_PAR_ERROR, "PMTX oespi parity error", -1, 1 },
  2836. { DB_OPTIONS_PAR_ERROR, "PMTX db_options parity error", -1,
  2837. 1 },
  2838. { ICSPI_PAR_ERROR, "PMTX icspi parity error", -1, 1 },
  2839. { C_PCMD_PAR_ERROR, "PMTX c_pcmd parity error", -1, 1},
  2840. { 0, NULL, 0, 0 }
  2841. };
  2842. if (csio_handle_intr_status(hw, PM_TX_INT_CAUSE, pmtx_intr_info))
  2843. csio_hw_fatal_err(hw);
  2844. }
  2845. /*
  2846. * PM RX interrupt handler.
  2847. */
  2848. static void csio_pmrx_intr_handler(struct csio_hw *hw)
  2849. {
  2850. static struct intr_info pmrx_intr_info[] = {
  2851. { ZERO_E_CMD_ERROR, "PMRX 0-length pcmd", -1, 1 },
  2852. { 0x3ffff0, "PMRX framing error", -1, 1 },
  2853. { OCSPI_PAR_ERROR, "PMRX ocspi parity error", -1, 1 },
  2854. { DB_OPTIONS_PAR_ERROR, "PMRX db_options parity error", -1,
  2855. 1 },
  2856. { IESPI_PAR_ERROR, "PMRX iespi parity error", -1, 1 },
  2857. { E_PCMD_PAR_ERROR, "PMRX e_pcmd parity error", -1, 1},
  2858. { 0, NULL, 0, 0 }
  2859. };
  2860. if (csio_handle_intr_status(hw, PM_RX_INT_CAUSE, pmrx_intr_info))
  2861. csio_hw_fatal_err(hw);
  2862. }
  2863. /*
  2864. * CPL switch interrupt handler.
  2865. */
  2866. static void csio_cplsw_intr_handler(struct csio_hw *hw)
  2867. {
  2868. static struct intr_info cplsw_intr_info[] = {
  2869. { CIM_OP_MAP_PERR, "CPLSW CIM op_map parity error", -1, 1 },
  2870. { CIM_OVFL_ERROR, "CPLSW CIM overflow", -1, 1 },
  2871. { TP_FRAMING_ERROR, "CPLSW TP framing error", -1, 1 },
  2872. { SGE_FRAMING_ERROR, "CPLSW SGE framing error", -1, 1 },
  2873. { CIM_FRAMING_ERROR, "CPLSW CIM framing error", -1, 1 },
  2874. { ZERO_SWITCH_ERROR, "CPLSW no-switch error", -1, 1 },
  2875. { 0, NULL, 0, 0 }
  2876. };
  2877. if (csio_handle_intr_status(hw, CPL_INTR_CAUSE, cplsw_intr_info))
  2878. csio_hw_fatal_err(hw);
  2879. }
  2880. /*
  2881. * LE interrupt handler.
  2882. */
  2883. static void csio_le_intr_handler(struct csio_hw *hw)
  2884. {
  2885. static struct intr_info le_intr_info[] = {
  2886. { LIPMISS, "LE LIP miss", -1, 0 },
  2887. { LIP0, "LE 0 LIP error", -1, 0 },
  2888. { PARITYERR, "LE parity error", -1, 1 },
  2889. { UNKNOWNCMD, "LE unknown command", -1, 1 },
  2890. { REQQPARERR, "LE request queue parity error", -1, 1 },
  2891. { 0, NULL, 0, 0 }
  2892. };
  2893. if (csio_handle_intr_status(hw, LE_DB_INT_CAUSE, le_intr_info))
  2894. csio_hw_fatal_err(hw);
  2895. }
  2896. /*
  2897. * MPS interrupt handler.
  2898. */
  2899. static void csio_mps_intr_handler(struct csio_hw *hw)
  2900. {
  2901. static struct intr_info mps_rx_intr_info[] = {
  2902. { 0xffffff, "MPS Rx parity error", -1, 1 },
  2903. { 0, NULL, 0, 0 }
  2904. };
  2905. static struct intr_info mps_tx_intr_info[] = {
  2906. { TPFIFO, "MPS Tx TP FIFO parity error", -1, 1 },
  2907. { NCSIFIFO, "MPS Tx NC-SI FIFO parity error", -1, 1 },
  2908. { TXDATAFIFO, "MPS Tx data FIFO parity error", -1, 1 },
  2909. { TXDESCFIFO, "MPS Tx desc FIFO parity error", -1, 1 },
  2910. { BUBBLE, "MPS Tx underflow", -1, 1 },
  2911. { SECNTERR, "MPS Tx SOP/EOP error", -1, 1 },
  2912. { FRMERR, "MPS Tx framing error", -1, 1 },
  2913. { 0, NULL, 0, 0 }
  2914. };
  2915. static struct intr_info mps_trc_intr_info[] = {
  2916. { FILTMEM, "MPS TRC filter parity error", -1, 1 },
  2917. { PKTFIFO, "MPS TRC packet FIFO parity error", -1, 1 },
  2918. { MISCPERR, "MPS TRC misc parity error", -1, 1 },
  2919. { 0, NULL, 0, 0 }
  2920. };
  2921. static struct intr_info mps_stat_sram_intr_info[] = {
  2922. { 0x1fffff, "MPS statistics SRAM parity error", -1, 1 },
  2923. { 0, NULL, 0, 0 }
  2924. };
  2925. static struct intr_info mps_stat_tx_intr_info[] = {
  2926. { 0xfffff, "MPS statistics Tx FIFO parity error", -1, 1 },
  2927. { 0, NULL, 0, 0 }
  2928. };
  2929. static struct intr_info mps_stat_rx_intr_info[] = {
  2930. { 0xffffff, "MPS statistics Rx FIFO parity error", -1, 1 },
  2931. { 0, NULL, 0, 0 }
  2932. };
  2933. static struct intr_info mps_cls_intr_info[] = {
  2934. { MATCHSRAM, "MPS match SRAM parity error", -1, 1 },
  2935. { MATCHTCAM, "MPS match TCAM parity error", -1, 1 },
  2936. { HASHSRAM, "MPS hash SRAM parity error", -1, 1 },
  2937. { 0, NULL, 0, 0 }
  2938. };
  2939. int fat;
  2940. fat = csio_handle_intr_status(hw, MPS_RX_PERR_INT_CAUSE,
  2941. mps_rx_intr_info) +
  2942. csio_handle_intr_status(hw, MPS_TX_INT_CAUSE,
  2943. mps_tx_intr_info) +
  2944. csio_handle_intr_status(hw, MPS_TRC_INT_CAUSE,
  2945. mps_trc_intr_info) +
  2946. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_SRAM,
  2947. mps_stat_sram_intr_info) +
  2948. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_TX_FIFO,
  2949. mps_stat_tx_intr_info) +
  2950. csio_handle_intr_status(hw, MPS_STAT_PERR_INT_CAUSE_RX_FIFO,
  2951. mps_stat_rx_intr_info) +
  2952. csio_handle_intr_status(hw, MPS_CLS_INT_CAUSE,
  2953. mps_cls_intr_info);
  2954. csio_wr_reg32(hw, 0, MPS_INT_CAUSE);
  2955. csio_rd_reg32(hw, MPS_INT_CAUSE); /* flush */
  2956. if (fat)
  2957. csio_hw_fatal_err(hw);
  2958. }
  2959. #define MEM_INT_MASK (PERR_INT_CAUSE | ECC_CE_INT_CAUSE | ECC_UE_INT_CAUSE)
  2960. /*
  2961. * EDC/MC interrupt handler.
  2962. */
  2963. static void csio_mem_intr_handler(struct csio_hw *hw, int idx)
  2964. {
  2965. static const char name[3][5] = { "EDC0", "EDC1", "MC" };
  2966. unsigned int addr, cnt_addr, v;
  2967. if (idx <= MEM_EDC1) {
  2968. addr = EDC_REG(EDC_INT_CAUSE, idx);
  2969. cnt_addr = EDC_REG(EDC_ECC_STATUS, idx);
  2970. } else {
  2971. addr = MC_INT_CAUSE;
  2972. cnt_addr = MC_ECC_STATUS;
  2973. }
  2974. v = csio_rd_reg32(hw, addr) & MEM_INT_MASK;
  2975. if (v & PERR_INT_CAUSE)
  2976. csio_fatal(hw, "%s FIFO parity error\n", name[idx]);
  2977. if (v & ECC_CE_INT_CAUSE) {
  2978. uint32_t cnt = ECC_CECNT_GET(csio_rd_reg32(hw, cnt_addr));
  2979. csio_wr_reg32(hw, ECC_CECNT_MASK, cnt_addr);
  2980. csio_warn(hw, "%u %s correctable ECC data error%s\n",
  2981. cnt, name[idx], cnt > 1 ? "s" : "");
  2982. }
  2983. if (v & ECC_UE_INT_CAUSE)
  2984. csio_fatal(hw, "%s uncorrectable ECC data error\n", name[idx]);
  2985. csio_wr_reg32(hw, v, addr);
  2986. if (v & (PERR_INT_CAUSE | ECC_UE_INT_CAUSE))
  2987. csio_hw_fatal_err(hw);
  2988. }
  2989. /*
  2990. * MA interrupt handler.
  2991. */
  2992. static void csio_ma_intr_handler(struct csio_hw *hw)
  2993. {
  2994. uint32_t v, status = csio_rd_reg32(hw, MA_INT_CAUSE);
  2995. if (status & MEM_PERR_INT_CAUSE)
  2996. csio_fatal(hw, "MA parity error, parity status %#x\n",
  2997. csio_rd_reg32(hw, MA_PARITY_ERROR_STATUS));
  2998. if (status & MEM_WRAP_INT_CAUSE) {
  2999. v = csio_rd_reg32(hw, MA_INT_WRAP_STATUS);
  3000. csio_fatal(hw,
  3001. "MA address wrap-around error by client %u to address %#x\n",
  3002. MEM_WRAP_CLIENT_NUM_GET(v), MEM_WRAP_ADDRESS_GET(v) << 4);
  3003. }
  3004. csio_wr_reg32(hw, status, MA_INT_CAUSE);
  3005. csio_hw_fatal_err(hw);
  3006. }
  3007. /*
  3008. * SMB interrupt handler.
  3009. */
  3010. static void csio_smb_intr_handler(struct csio_hw *hw)
  3011. {
  3012. static struct intr_info smb_intr_info[] = {
  3013. { MSTTXFIFOPARINT, "SMB master Tx FIFO parity error", -1, 1 },
  3014. { MSTRXFIFOPARINT, "SMB master Rx FIFO parity error", -1, 1 },
  3015. { SLVFIFOPARINT, "SMB slave FIFO parity error", -1, 1 },
  3016. { 0, NULL, 0, 0 }
  3017. };
  3018. if (csio_handle_intr_status(hw, SMB_INT_CAUSE, smb_intr_info))
  3019. csio_hw_fatal_err(hw);
  3020. }
  3021. /*
  3022. * NC-SI interrupt handler.
  3023. */
  3024. static void csio_ncsi_intr_handler(struct csio_hw *hw)
  3025. {
  3026. static struct intr_info ncsi_intr_info[] = {
  3027. { CIM_DM_PRTY_ERR, "NC-SI CIM parity error", -1, 1 },
  3028. { MPS_DM_PRTY_ERR, "NC-SI MPS parity error", -1, 1 },
  3029. { TXFIFO_PRTY_ERR, "NC-SI Tx FIFO parity error", -1, 1 },
  3030. { RXFIFO_PRTY_ERR, "NC-SI Rx FIFO parity error", -1, 1 },
  3031. { 0, NULL, 0, 0 }
  3032. };
  3033. if (csio_handle_intr_status(hw, NCSI_INT_CAUSE, ncsi_intr_info))
  3034. csio_hw_fatal_err(hw);
  3035. }
  3036. /*
  3037. * XGMAC interrupt handler.
  3038. */
  3039. static void csio_xgmac_intr_handler(struct csio_hw *hw, int port)
  3040. {
  3041. uint32_t v = csio_rd_reg32(hw, PORT_REG(port, XGMAC_PORT_INT_CAUSE));
  3042. v &= TXFIFO_PRTY_ERR | RXFIFO_PRTY_ERR;
  3043. if (!v)
  3044. return;
  3045. if (v & TXFIFO_PRTY_ERR)
  3046. csio_fatal(hw, "XGMAC %d Tx FIFO parity error\n", port);
  3047. if (v & RXFIFO_PRTY_ERR)
  3048. csio_fatal(hw, "XGMAC %d Rx FIFO parity error\n", port);
  3049. csio_wr_reg32(hw, v, PORT_REG(port, XGMAC_PORT_INT_CAUSE));
  3050. csio_hw_fatal_err(hw);
  3051. }
  3052. /*
  3053. * PL interrupt handler.
  3054. */
  3055. static void csio_pl_intr_handler(struct csio_hw *hw)
  3056. {
  3057. static struct intr_info pl_intr_info[] = {
  3058. { FATALPERR, "T4 fatal parity error", -1, 1 },
  3059. { PERRVFID, "PL VFID_MAP parity error", -1, 1 },
  3060. { 0, NULL, 0, 0 }
  3061. };
  3062. if (csio_handle_intr_status(hw, PL_PL_INT_CAUSE, pl_intr_info))
  3063. csio_hw_fatal_err(hw);
  3064. }
  3065. /*
  3066. * csio_hw_slow_intr_handler - control path interrupt handler
  3067. * @hw: HW module
  3068. *
  3069. * Interrupt handler for non-data global interrupt events, e.g., errors.
  3070. * The designation 'slow' is because it involves register reads, while
  3071. * data interrupts typically don't involve any MMIOs.
  3072. */
  3073. int
  3074. csio_hw_slow_intr_handler(struct csio_hw *hw)
  3075. {
  3076. uint32_t cause = csio_rd_reg32(hw, PL_INT_CAUSE);
  3077. if (!(cause & CSIO_GLBL_INTR_MASK)) {
  3078. CSIO_INC_STATS(hw, n_plint_unexp);
  3079. return 0;
  3080. }
  3081. csio_dbg(hw, "Slow interrupt! cause: 0x%x\n", cause);
  3082. CSIO_INC_STATS(hw, n_plint_cnt);
  3083. if (cause & CIM)
  3084. csio_cim_intr_handler(hw);
  3085. if (cause & MPS)
  3086. csio_mps_intr_handler(hw);
  3087. if (cause & NCSI)
  3088. csio_ncsi_intr_handler(hw);
  3089. if (cause & PL)
  3090. csio_pl_intr_handler(hw);
  3091. if (cause & SMB)
  3092. csio_smb_intr_handler(hw);
  3093. if (cause & XGMAC0)
  3094. csio_xgmac_intr_handler(hw, 0);
  3095. if (cause & XGMAC1)
  3096. csio_xgmac_intr_handler(hw, 1);
  3097. if (cause & XGMAC_KR0)
  3098. csio_xgmac_intr_handler(hw, 2);
  3099. if (cause & XGMAC_KR1)
  3100. csio_xgmac_intr_handler(hw, 3);
  3101. if (cause & PCIE)
  3102. csio_pcie_intr_handler(hw);
  3103. if (cause & MC)
  3104. csio_mem_intr_handler(hw, MEM_MC);
  3105. if (cause & EDC0)
  3106. csio_mem_intr_handler(hw, MEM_EDC0);
  3107. if (cause & EDC1)
  3108. csio_mem_intr_handler(hw, MEM_EDC1);
  3109. if (cause & LE)
  3110. csio_le_intr_handler(hw);
  3111. if (cause & TP)
  3112. csio_tp_intr_handler(hw);
  3113. if (cause & MA)
  3114. csio_ma_intr_handler(hw);
  3115. if (cause & PM_TX)
  3116. csio_pmtx_intr_handler(hw);
  3117. if (cause & PM_RX)
  3118. csio_pmrx_intr_handler(hw);
  3119. if (cause & ULP_RX)
  3120. csio_ulprx_intr_handler(hw);
  3121. if (cause & CPL_SWITCH)
  3122. csio_cplsw_intr_handler(hw);
  3123. if (cause & SGE)
  3124. csio_sge_intr_handler(hw);
  3125. if (cause & ULP_TX)
  3126. csio_ulptx_intr_handler(hw);
  3127. /* Clear the interrupts just processed for which we are the master. */
  3128. csio_wr_reg32(hw, cause & CSIO_GLBL_INTR_MASK, PL_INT_CAUSE);
  3129. csio_rd_reg32(hw, PL_INT_CAUSE); /* flush */
  3130. return 1;
  3131. }
  3132. /*****************************************************************************
  3133. * HW <--> mailbox interfacing routines.
  3134. ****************************************************************************/
  3135. /*
  3136. * csio_mberr_worker - Worker thread (dpc) for mailbox/error completions
  3137. *
  3138. * @data: Private data pointer.
  3139. *
  3140. * Called from worker thread context.
  3141. */
  3142. static void
  3143. csio_mberr_worker(void *data)
  3144. {
  3145. struct csio_hw *hw = (struct csio_hw *)data;
  3146. struct csio_mbm *mbm = &hw->mbm;
  3147. LIST_HEAD(cbfn_q);
  3148. struct csio_mb *mbp_next;
  3149. int rv;
  3150. del_timer_sync(&mbm->timer);
  3151. spin_lock_irq(&hw->lock);
  3152. if (list_empty(&mbm->cbfn_q)) {
  3153. spin_unlock_irq(&hw->lock);
  3154. return;
  3155. }
  3156. list_splice_tail_init(&mbm->cbfn_q, &cbfn_q);
  3157. mbm->stats.n_cbfnq = 0;
  3158. /* Try to start waiting mailboxes */
  3159. if (!list_empty(&mbm->req_q)) {
  3160. mbp_next = list_first_entry(&mbm->req_q, struct csio_mb, list);
  3161. list_del_init(&mbp_next->list);
  3162. rv = csio_mb_issue(hw, mbp_next);
  3163. if (rv != 0)
  3164. list_add_tail(&mbp_next->list, &mbm->req_q);
  3165. else
  3166. CSIO_DEC_STATS(mbm, n_activeq);
  3167. }
  3168. spin_unlock_irq(&hw->lock);
  3169. /* Now callback completions */
  3170. csio_mb_completions(hw, &cbfn_q);
  3171. }
  3172. /*
  3173. * csio_hw_mb_timer - Top-level Mailbox timeout handler.
  3174. *
  3175. * @data: private data pointer
  3176. *
  3177. **/
  3178. static void
  3179. csio_hw_mb_timer(uintptr_t data)
  3180. {
  3181. struct csio_hw *hw = (struct csio_hw *)data;
  3182. struct csio_mb *mbp = NULL;
  3183. spin_lock_irq(&hw->lock);
  3184. mbp = csio_mb_tmo_handler(hw);
  3185. spin_unlock_irq(&hw->lock);
  3186. /* Call back the function for the timed-out Mailbox */
  3187. if (mbp)
  3188. mbp->mb_cbfn(hw, mbp);
  3189. }
  3190. /*
  3191. * csio_hw_mbm_cleanup - Cleanup Mailbox module.
  3192. * @hw: HW module
  3193. *
  3194. * Called with lock held, should exit with lock held.
  3195. * Cancels outstanding mailboxes (waiting, in-flight) and gathers them
  3196. * into a local queue. Drops lock and calls the completions. Holds
  3197. * lock and returns.
  3198. */
  3199. static void
  3200. csio_hw_mbm_cleanup(struct csio_hw *hw)
  3201. {
  3202. LIST_HEAD(cbfn_q);
  3203. csio_mb_cancel_all(hw, &cbfn_q);
  3204. spin_unlock_irq(&hw->lock);
  3205. csio_mb_completions(hw, &cbfn_q);
  3206. spin_lock_irq(&hw->lock);
  3207. }
  3208. /*****************************************************************************
  3209. * Event handling
  3210. ****************************************************************************/
  3211. int
  3212. csio_enqueue_evt(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
  3213. uint16_t len)
  3214. {
  3215. struct csio_evt_msg *evt_entry = NULL;
  3216. if (type >= CSIO_EVT_MAX)
  3217. return -EINVAL;
  3218. if (len > CSIO_EVT_MSG_SIZE)
  3219. return -EINVAL;
  3220. if (hw->flags & CSIO_HWF_FWEVT_STOP)
  3221. return -EINVAL;
  3222. if (list_empty(&hw->evt_free_q)) {
  3223. csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
  3224. type, len);
  3225. return -ENOMEM;
  3226. }
  3227. evt_entry = list_first_entry(&hw->evt_free_q,
  3228. struct csio_evt_msg, list);
  3229. list_del_init(&evt_entry->list);
  3230. /* copy event msg and queue the event */
  3231. evt_entry->type = type;
  3232. memcpy((void *)evt_entry->data, evt_msg, len);
  3233. list_add_tail(&evt_entry->list, &hw->evt_active_q);
  3234. CSIO_DEC_STATS(hw, n_evt_freeq);
  3235. CSIO_INC_STATS(hw, n_evt_activeq);
  3236. return 0;
  3237. }
  3238. static int
  3239. csio_enqueue_evt_lock(struct csio_hw *hw, enum csio_evt type, void *evt_msg,
  3240. uint16_t len, bool msg_sg)
  3241. {
  3242. struct csio_evt_msg *evt_entry = NULL;
  3243. struct csio_fl_dma_buf *fl_sg;
  3244. uint32_t off = 0;
  3245. unsigned long flags;
  3246. int n, ret = 0;
  3247. if (type >= CSIO_EVT_MAX)
  3248. return -EINVAL;
  3249. if (len > CSIO_EVT_MSG_SIZE)
  3250. return -EINVAL;
  3251. spin_lock_irqsave(&hw->lock, flags);
  3252. if (hw->flags & CSIO_HWF_FWEVT_STOP) {
  3253. ret = -EINVAL;
  3254. goto out;
  3255. }
  3256. if (list_empty(&hw->evt_free_q)) {
  3257. csio_err(hw, "Failed to alloc evt entry, msg type %d len %d\n",
  3258. type, len);
  3259. ret = -ENOMEM;
  3260. goto out;
  3261. }
  3262. evt_entry = list_first_entry(&hw->evt_free_q,
  3263. struct csio_evt_msg, list);
  3264. list_del_init(&evt_entry->list);
  3265. /* copy event msg and queue the event */
  3266. evt_entry->type = type;
  3267. /* If Payload in SG list*/
  3268. if (msg_sg) {
  3269. fl_sg = (struct csio_fl_dma_buf *) evt_msg;
  3270. for (n = 0; (n < CSIO_MAX_FLBUF_PER_IQWR && off < len); n++) {
  3271. memcpy((void *)((uintptr_t)evt_entry->data + off),
  3272. fl_sg->flbufs[n].vaddr,
  3273. fl_sg->flbufs[n].len);
  3274. off += fl_sg->flbufs[n].len;
  3275. }
  3276. } else
  3277. memcpy((void *)evt_entry->data, evt_msg, len);
  3278. list_add_tail(&evt_entry->list, &hw->evt_active_q);
  3279. CSIO_DEC_STATS(hw, n_evt_freeq);
  3280. CSIO_INC_STATS(hw, n_evt_activeq);
  3281. out:
  3282. spin_unlock_irqrestore(&hw->lock, flags);
  3283. return ret;
  3284. }
  3285. static void
  3286. csio_free_evt(struct csio_hw *hw, struct csio_evt_msg *evt_entry)
  3287. {
  3288. if (evt_entry) {
  3289. spin_lock_irq(&hw->lock);
  3290. list_del_init(&evt_entry->list);
  3291. list_add_tail(&evt_entry->list, &hw->evt_free_q);
  3292. CSIO_DEC_STATS(hw, n_evt_activeq);
  3293. CSIO_INC_STATS(hw, n_evt_freeq);
  3294. spin_unlock_irq(&hw->lock);
  3295. }
  3296. }
  3297. void
  3298. csio_evtq_flush(struct csio_hw *hw)
  3299. {
  3300. uint32_t count;
  3301. count = 30;
  3302. while (hw->flags & CSIO_HWF_FWEVT_PENDING && count--) {
  3303. spin_unlock_irq(&hw->lock);
  3304. msleep(2000);
  3305. spin_lock_irq(&hw->lock);
  3306. }
  3307. CSIO_DB_ASSERT(!(hw->flags & CSIO_HWF_FWEVT_PENDING));
  3308. }
  3309. static void
  3310. csio_evtq_stop(struct csio_hw *hw)
  3311. {
  3312. hw->flags |= CSIO_HWF_FWEVT_STOP;
  3313. }
  3314. static void
  3315. csio_evtq_start(struct csio_hw *hw)
  3316. {
  3317. hw->flags &= ~CSIO_HWF_FWEVT_STOP;
  3318. }
  3319. static void
  3320. csio_evtq_cleanup(struct csio_hw *hw)
  3321. {
  3322. struct list_head *evt_entry, *next_entry;
  3323. /* Release outstanding events from activeq to freeq*/
  3324. if (!list_empty(&hw->evt_active_q))
  3325. list_splice_tail_init(&hw->evt_active_q, &hw->evt_free_q);
  3326. hw->stats.n_evt_activeq = 0;
  3327. hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
  3328. /* Freeup event entry */
  3329. list_for_each_safe(evt_entry, next_entry, &hw->evt_free_q) {
  3330. kfree(evt_entry);
  3331. CSIO_DEC_STATS(hw, n_evt_freeq);
  3332. }
  3333. hw->stats.n_evt_freeq = 0;
  3334. }
  3335. static void
  3336. csio_process_fwevtq_entry(struct csio_hw *hw, void *wr, uint32_t len,
  3337. struct csio_fl_dma_buf *flb, void *priv)
  3338. {
  3339. __u8 op;
  3340. __be64 *data;
  3341. void *msg = NULL;
  3342. uint32_t msg_len = 0;
  3343. bool msg_sg = 0;
  3344. op = ((struct rss_header *) wr)->opcode;
  3345. if (op == CPL_FW6_PLD) {
  3346. CSIO_INC_STATS(hw, n_cpl_fw6_pld);
  3347. if (!flb || !flb->totlen) {
  3348. CSIO_INC_STATS(hw, n_cpl_unexp);
  3349. return;
  3350. }
  3351. msg = (void *) flb;
  3352. msg_len = flb->totlen;
  3353. msg_sg = 1;
  3354. data = (__be64 *) msg;
  3355. } else if (op == CPL_FW6_MSG || op == CPL_FW4_MSG) {
  3356. CSIO_INC_STATS(hw, n_cpl_fw6_msg);
  3357. /* skip RSS header */
  3358. msg = (void *)((uintptr_t)wr + sizeof(__be64));
  3359. msg_len = (op == CPL_FW6_MSG) ? sizeof(struct cpl_fw6_msg) :
  3360. sizeof(struct cpl_fw4_msg);
  3361. data = (__be64 *) msg;
  3362. } else {
  3363. csio_warn(hw, "unexpected CPL %#x on FW event queue\n", op);
  3364. CSIO_INC_STATS(hw, n_cpl_unexp);
  3365. return;
  3366. }
  3367. /*
  3368. * Enqueue event to EventQ. Events processing happens
  3369. * in Event worker thread context
  3370. */
  3371. if (csio_enqueue_evt_lock(hw, CSIO_EVT_FW, msg,
  3372. (uint16_t)msg_len, msg_sg))
  3373. CSIO_INC_STATS(hw, n_evt_drop);
  3374. }
  3375. void
  3376. csio_evtq_worker(struct work_struct *work)
  3377. {
  3378. struct csio_hw *hw = container_of(work, struct csio_hw, evtq_work);
  3379. struct list_head *evt_entry, *next_entry;
  3380. LIST_HEAD(evt_q);
  3381. struct csio_evt_msg *evt_msg;
  3382. struct cpl_fw6_msg *msg;
  3383. struct csio_rnode *rn;
  3384. int rv = 0;
  3385. uint8_t evtq_stop = 0;
  3386. csio_dbg(hw, "event worker thread active evts#%d\n",
  3387. hw->stats.n_evt_activeq);
  3388. spin_lock_irq(&hw->lock);
  3389. while (!list_empty(&hw->evt_active_q)) {
  3390. list_splice_tail_init(&hw->evt_active_q, &evt_q);
  3391. spin_unlock_irq(&hw->lock);
  3392. list_for_each_safe(evt_entry, next_entry, &evt_q) {
  3393. evt_msg = (struct csio_evt_msg *) evt_entry;
  3394. /* Drop events if queue is STOPPED */
  3395. spin_lock_irq(&hw->lock);
  3396. if (hw->flags & CSIO_HWF_FWEVT_STOP)
  3397. evtq_stop = 1;
  3398. spin_unlock_irq(&hw->lock);
  3399. if (evtq_stop) {
  3400. CSIO_INC_STATS(hw, n_evt_drop);
  3401. goto free_evt;
  3402. }
  3403. switch (evt_msg->type) {
  3404. case CSIO_EVT_FW:
  3405. msg = (struct cpl_fw6_msg *)(evt_msg->data);
  3406. if ((msg->opcode == CPL_FW6_MSG ||
  3407. msg->opcode == CPL_FW4_MSG) &&
  3408. !msg->type) {
  3409. rv = csio_mb_fwevt_handler(hw,
  3410. msg->data);
  3411. if (!rv)
  3412. break;
  3413. /* Handle any remaining fw events */
  3414. csio_fcoe_fwevt_handler(hw,
  3415. msg->opcode, msg->data);
  3416. } else if (msg->opcode == CPL_FW6_PLD) {
  3417. csio_fcoe_fwevt_handler(hw,
  3418. msg->opcode, msg->data);
  3419. } else {
  3420. csio_warn(hw,
  3421. "Unhandled FW msg op %x type %x\n",
  3422. msg->opcode, msg->type);
  3423. CSIO_INC_STATS(hw, n_evt_drop);
  3424. }
  3425. break;
  3426. case CSIO_EVT_MBX:
  3427. csio_mberr_worker(hw);
  3428. break;
  3429. case CSIO_EVT_DEV_LOSS:
  3430. memcpy(&rn, evt_msg->data, sizeof(rn));
  3431. csio_rnode_devloss_handler(rn);
  3432. break;
  3433. default:
  3434. csio_warn(hw, "Unhandled event %x on evtq\n",
  3435. evt_msg->type);
  3436. CSIO_INC_STATS(hw, n_evt_unexp);
  3437. break;
  3438. }
  3439. free_evt:
  3440. csio_free_evt(hw, evt_msg);
  3441. }
  3442. spin_lock_irq(&hw->lock);
  3443. }
  3444. hw->flags &= ~CSIO_HWF_FWEVT_PENDING;
  3445. spin_unlock_irq(&hw->lock);
  3446. }
  3447. int
  3448. csio_fwevtq_handler(struct csio_hw *hw)
  3449. {
  3450. int rv;
  3451. if (csio_q_iqid(hw, hw->fwevt_iq_idx) == CSIO_MAX_QID) {
  3452. CSIO_INC_STATS(hw, n_int_stray);
  3453. return -EINVAL;
  3454. }
  3455. rv = csio_wr_process_iq_idx(hw, hw->fwevt_iq_idx,
  3456. csio_process_fwevtq_entry, NULL);
  3457. return rv;
  3458. }
  3459. /****************************************************************************
  3460. * Entry points
  3461. ****************************************************************************/
  3462. /* Management module */
  3463. /*
  3464. * csio_mgmt_req_lookup - Lookup the given IO req exist in Active Q.
  3465. * mgmt - mgmt module
  3466. * @io_req - io request
  3467. *
  3468. * Return - 0:if given IO Req exists in active Q.
  3469. * -EINVAL :if lookup fails.
  3470. */
  3471. int
  3472. csio_mgmt_req_lookup(struct csio_mgmtm *mgmtm, struct csio_ioreq *io_req)
  3473. {
  3474. struct list_head *tmp;
  3475. /* Lookup ioreq in the ACTIVEQ */
  3476. list_for_each(tmp, &mgmtm->active_q) {
  3477. if (io_req == (struct csio_ioreq *)tmp)
  3478. return 0;
  3479. }
  3480. return -EINVAL;
  3481. }
  3482. #define ECM_MIN_TMO 1000 /* Minimum timeout value for req */
  3483. /*
  3484. * csio_mgmts_tmo_handler - MGMT IO Timeout handler.
  3485. * @data - Event data.
  3486. *
  3487. * Return - none.
  3488. */
  3489. static void
  3490. csio_mgmt_tmo_handler(uintptr_t data)
  3491. {
  3492. struct csio_mgmtm *mgmtm = (struct csio_mgmtm *) data;
  3493. struct list_head *tmp;
  3494. struct csio_ioreq *io_req;
  3495. csio_dbg(mgmtm->hw, "Mgmt timer invoked!\n");
  3496. spin_lock_irq(&mgmtm->hw->lock);
  3497. list_for_each(tmp, &mgmtm->active_q) {
  3498. io_req = (struct csio_ioreq *) tmp;
  3499. io_req->tmo -= min_t(uint32_t, io_req->tmo, ECM_MIN_TMO);
  3500. if (!io_req->tmo) {
  3501. /* Dequeue the request from retry Q. */
  3502. tmp = csio_list_prev(tmp);
  3503. list_del_init(&io_req->sm.sm_list);
  3504. if (io_req->io_cbfn) {
  3505. /* io_req will be freed by completion handler */
  3506. io_req->wr_status = -ETIMEDOUT;
  3507. io_req->io_cbfn(mgmtm->hw, io_req);
  3508. } else {
  3509. CSIO_DB_ASSERT(0);
  3510. }
  3511. }
  3512. }
  3513. /* If retry queue is not empty, re-arm timer */
  3514. if (!list_empty(&mgmtm->active_q))
  3515. mod_timer(&mgmtm->mgmt_timer,
  3516. jiffies + msecs_to_jiffies(ECM_MIN_TMO));
  3517. spin_unlock_irq(&mgmtm->hw->lock);
  3518. }
  3519. static void
  3520. csio_mgmtm_cleanup(struct csio_mgmtm *mgmtm)
  3521. {
  3522. struct csio_hw *hw = mgmtm->hw;
  3523. struct csio_ioreq *io_req;
  3524. struct list_head *tmp;
  3525. uint32_t count;
  3526. count = 30;
  3527. /* Wait for all outstanding req to complete gracefully */
  3528. while ((!list_empty(&mgmtm->active_q)) && count--) {
  3529. spin_unlock_irq(&hw->lock);
  3530. msleep(2000);
  3531. spin_lock_irq(&hw->lock);
  3532. }
  3533. /* release outstanding req from ACTIVEQ */
  3534. list_for_each(tmp, &mgmtm->active_q) {
  3535. io_req = (struct csio_ioreq *) tmp;
  3536. tmp = csio_list_prev(tmp);
  3537. list_del_init(&io_req->sm.sm_list);
  3538. mgmtm->stats.n_active--;
  3539. if (io_req->io_cbfn) {
  3540. /* io_req will be freed by completion handler */
  3541. io_req->wr_status = -ETIMEDOUT;
  3542. io_req->io_cbfn(mgmtm->hw, io_req);
  3543. }
  3544. }
  3545. }
  3546. /*
  3547. * csio_mgmt_init - Mgmt module init entry point
  3548. * @mgmtsm - mgmt module
  3549. * @hw - HW module
  3550. *
  3551. * Initialize mgmt timer, resource wait queue, active queue,
  3552. * completion q. Allocate Egress and Ingress
  3553. * WR queues and save off the queue index returned by the WR
  3554. * module for future use. Allocate and save off mgmt reqs in the
  3555. * mgmt_req_freelist for future use. Make sure their SM is initialized
  3556. * to uninit state.
  3557. * Returns: 0 - on success
  3558. * -ENOMEM - on error.
  3559. */
  3560. static int
  3561. csio_mgmtm_init(struct csio_mgmtm *mgmtm, struct csio_hw *hw)
  3562. {
  3563. struct timer_list *timer = &mgmtm->mgmt_timer;
  3564. init_timer(timer);
  3565. timer->function = csio_mgmt_tmo_handler;
  3566. timer->data = (unsigned long)mgmtm;
  3567. INIT_LIST_HEAD(&mgmtm->active_q);
  3568. INIT_LIST_HEAD(&mgmtm->cbfn_q);
  3569. mgmtm->hw = hw;
  3570. /*mgmtm->iq_idx = hw->fwevt_iq_idx;*/
  3571. return 0;
  3572. }
  3573. /*
  3574. * csio_mgmtm_exit - MGMT module exit entry point
  3575. * @mgmtsm - mgmt module
  3576. *
  3577. * This function called during MGMT module uninit.
  3578. * Stop timers, free ioreqs allocated.
  3579. * Returns: None
  3580. *
  3581. */
  3582. static void
  3583. csio_mgmtm_exit(struct csio_mgmtm *mgmtm)
  3584. {
  3585. del_timer_sync(&mgmtm->mgmt_timer);
  3586. }
  3587. /**
  3588. * csio_hw_start - Kicks off the HW State machine
  3589. * @hw: Pointer to HW module.
  3590. *
  3591. * It is assumed that the initialization is a synchronous operation.
  3592. * So when we return afer posting the event, the HW SM should be in
  3593. * the ready state, if there were no errors during init.
  3594. */
  3595. int
  3596. csio_hw_start(struct csio_hw *hw)
  3597. {
  3598. spin_lock_irq(&hw->lock);
  3599. csio_post_event(&hw->sm, CSIO_HWE_CFG);
  3600. spin_unlock_irq(&hw->lock);
  3601. if (csio_is_hw_ready(hw))
  3602. return 0;
  3603. else
  3604. return -EINVAL;
  3605. }
  3606. int
  3607. csio_hw_stop(struct csio_hw *hw)
  3608. {
  3609. csio_post_event(&hw->sm, CSIO_HWE_PCI_REMOVE);
  3610. if (csio_is_hw_removing(hw))
  3611. return 0;
  3612. else
  3613. return -EINVAL;
  3614. }
  3615. /* Max reset retries */
  3616. #define CSIO_MAX_RESET_RETRIES 3
  3617. /**
  3618. * csio_hw_reset - Reset the hardware
  3619. * @hw: HW module.
  3620. *
  3621. * Caller should hold lock across this function.
  3622. */
  3623. int
  3624. csio_hw_reset(struct csio_hw *hw)
  3625. {
  3626. if (!csio_is_hw_master(hw))
  3627. return -EPERM;
  3628. if (hw->rst_retries >= CSIO_MAX_RESET_RETRIES) {
  3629. csio_dbg(hw, "Max hw reset attempts reached..");
  3630. return -EINVAL;
  3631. }
  3632. hw->rst_retries++;
  3633. csio_post_event(&hw->sm, CSIO_HWE_HBA_RESET);
  3634. if (csio_is_hw_ready(hw)) {
  3635. hw->rst_retries = 0;
  3636. hw->stats.n_reset_start = jiffies_to_msecs(jiffies);
  3637. return 0;
  3638. } else
  3639. return -EINVAL;
  3640. }
  3641. /*
  3642. * csio_hw_get_device_id - Caches the Adapter's vendor & device id.
  3643. * @hw: HW module.
  3644. */
  3645. static void
  3646. csio_hw_get_device_id(struct csio_hw *hw)
  3647. {
  3648. /* Is the adapter device id cached already ?*/
  3649. if (csio_is_dev_id_cached(hw))
  3650. return;
  3651. /* Get the PCI vendor & device id */
  3652. pci_read_config_word(hw->pdev, PCI_VENDOR_ID,
  3653. &hw->params.pci.vendor_id);
  3654. pci_read_config_word(hw->pdev, PCI_DEVICE_ID,
  3655. &hw->params.pci.device_id);
  3656. csio_dev_id_cached(hw);
  3657. } /* csio_hw_get_device_id */
  3658. /*
  3659. * csio_hw_set_description - Set the model, description of the hw.
  3660. * @hw: HW module.
  3661. * @ven_id: PCI Vendor ID
  3662. * @dev_id: PCI Device ID
  3663. */
  3664. static void
  3665. csio_hw_set_description(struct csio_hw *hw, uint16_t ven_id, uint16_t dev_id)
  3666. {
  3667. uint32_t adap_type, prot_type;
  3668. if (ven_id == CSIO_VENDOR_ID) {
  3669. prot_type = (dev_id & CSIO_ASIC_DEVID_PROTO_MASK);
  3670. adap_type = (dev_id & CSIO_ASIC_DEVID_TYPE_MASK);
  3671. if (prot_type == CSIO_FPGA) {
  3672. memcpy(hw->model_desc,
  3673. csio_fcoe_adapters[13].description, 32);
  3674. } else if (prot_type == CSIO_T4_FCOE_ASIC) {
  3675. memcpy(hw->hw_ver,
  3676. csio_fcoe_adapters[adap_type].model_no, 16);
  3677. memcpy(hw->model_desc,
  3678. csio_fcoe_adapters[adap_type].description, 32);
  3679. } else {
  3680. char tempName[32] = "Chelsio FCoE Controller";
  3681. memcpy(hw->model_desc, tempName, 32);
  3682. CSIO_DB_ASSERT(0);
  3683. }
  3684. }
  3685. } /* csio_hw_set_description */
  3686. /**
  3687. * csio_hw_init - Initialize HW module.
  3688. * @hw: Pointer to HW module.
  3689. *
  3690. * Initialize the members of the HW module.
  3691. */
  3692. int
  3693. csio_hw_init(struct csio_hw *hw)
  3694. {
  3695. int rv = -EINVAL;
  3696. uint32_t i;
  3697. uint16_t ven_id, dev_id;
  3698. struct csio_evt_msg *evt_entry;
  3699. INIT_LIST_HEAD(&hw->sm.sm_list);
  3700. csio_init_state(&hw->sm, csio_hws_uninit);
  3701. spin_lock_init(&hw->lock);
  3702. INIT_LIST_HEAD(&hw->sln_head);
  3703. /* Get the PCI vendor & device id */
  3704. csio_hw_get_device_id(hw);
  3705. strcpy(hw->name, CSIO_HW_NAME);
  3706. /* Set the model & its description */
  3707. ven_id = hw->params.pci.vendor_id;
  3708. dev_id = hw->params.pci.device_id;
  3709. csio_hw_set_description(hw, ven_id, dev_id);
  3710. /* Initialize default log level */
  3711. hw->params.log_level = (uint32_t) csio_dbg_level;
  3712. csio_set_fwevt_intr_idx(hw, -1);
  3713. csio_set_nondata_intr_idx(hw, -1);
  3714. /* Init all the modules: Mailbox, WorkRequest and Transport */
  3715. if (csio_mbm_init(csio_hw_to_mbm(hw), hw, csio_hw_mb_timer))
  3716. goto err;
  3717. rv = csio_wrm_init(csio_hw_to_wrm(hw), hw);
  3718. if (rv)
  3719. goto err_mbm_exit;
  3720. rv = csio_scsim_init(csio_hw_to_scsim(hw), hw);
  3721. if (rv)
  3722. goto err_wrm_exit;
  3723. rv = csio_mgmtm_init(csio_hw_to_mgmtm(hw), hw);
  3724. if (rv)
  3725. goto err_scsim_exit;
  3726. /* Pre-allocate evtq and initialize them */
  3727. INIT_LIST_HEAD(&hw->evt_active_q);
  3728. INIT_LIST_HEAD(&hw->evt_free_q);
  3729. for (i = 0; i < csio_evtq_sz; i++) {
  3730. evt_entry = kzalloc(sizeof(struct csio_evt_msg), GFP_KERNEL);
  3731. if (!evt_entry) {
  3732. csio_err(hw, "Failed to initialize eventq");
  3733. goto err_evtq_cleanup;
  3734. }
  3735. list_add_tail(&evt_entry->list, &hw->evt_free_q);
  3736. CSIO_INC_STATS(hw, n_evt_freeq);
  3737. }
  3738. hw->dev_num = dev_num;
  3739. dev_num++;
  3740. return 0;
  3741. err_evtq_cleanup:
  3742. csio_evtq_cleanup(hw);
  3743. csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
  3744. err_scsim_exit:
  3745. csio_scsim_exit(csio_hw_to_scsim(hw));
  3746. err_wrm_exit:
  3747. csio_wrm_exit(csio_hw_to_wrm(hw), hw);
  3748. err_mbm_exit:
  3749. csio_mbm_exit(csio_hw_to_mbm(hw));
  3750. err:
  3751. return rv;
  3752. }
  3753. /**
  3754. * csio_hw_exit - Un-initialize HW module.
  3755. * @hw: Pointer to HW module.
  3756. *
  3757. */
  3758. void
  3759. csio_hw_exit(struct csio_hw *hw)
  3760. {
  3761. csio_evtq_cleanup(hw);
  3762. csio_mgmtm_exit(csio_hw_to_mgmtm(hw));
  3763. csio_scsim_exit(csio_hw_to_scsim(hw));
  3764. csio_wrm_exit(csio_hw_to_wrm(hw), hw);
  3765. csio_mbm_exit(csio_hw_to_mbm(hw));
  3766. }