csio_hw.c 100 KB

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