mpt3sas_base.c 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839
  1. /*
  2. * This is the Fusion MPT base driver providing common API layer interface
  3. * for access to MPT (Message Passing Technology) firmware.
  4. *
  5. * This code is based on drivers/scsi/mpt3sas/mpt3sas_base.c
  6. * Copyright (C) 2012 LSI Corporation
  7. * (mailto:DL-MPTFusionLinux@lsi.com)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * NO WARRANTY
  20. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24. * solely responsible for determining the appropriateness of using and
  25. * distributing the Program and assumes all risks associated with its
  26. * exercise of rights under this Agreement, including but not limited to
  27. * the risks and costs of program errors, damage to or loss of data,
  28. * programs or equipment, and unavailability or interruption of operations.
  29. * DISCLAIMER OF LIABILITY
  30. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37. * You should have received a copy of the GNU General Public License
  38. * along with this program; if not, write to the Free Software
  39. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  40. * USA.
  41. */
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/errno.h>
  45. #include <linux/init.h>
  46. #include <linux/slab.h>
  47. #include <linux/types.h>
  48. #include <linux/pci.h>
  49. #include <linux/kdev_t.h>
  50. #include <linux/blkdev.h>
  51. #include <linux/delay.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/dma-mapping.h>
  54. #include <linux/io.h>
  55. #include <linux/time.h>
  56. #include <linux/kthread.h>
  57. #include <linux/aer.h>
  58. #include "mpt3sas_base.h"
  59. static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
  60. #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
  61. /* maximum controller queue depth */
  62. #define MAX_HBA_QUEUE_DEPTH 30000
  63. #define MAX_CHAIN_DEPTH 100000
  64. static int max_queue_depth = -1;
  65. module_param(max_queue_depth, int, 0);
  66. MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
  67. static int max_sgl_entries = -1;
  68. module_param(max_sgl_entries, int, 0);
  69. MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
  70. static int msix_disable = -1;
  71. module_param(msix_disable, int, 0);
  72. MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
  73. static int mpt3sas_fwfault_debug;
  74. MODULE_PARM_DESC(mpt3sas_fwfault_debug,
  75. " enable detection of firmware fault and halt firmware - (default=0)");
  76. /**
  77. * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
  78. *
  79. */
  80. static int
  81. _scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
  82. {
  83. int ret = param_set_int(val, kp);
  84. struct MPT3SAS_ADAPTER *ioc;
  85. if (ret)
  86. return ret;
  87. pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
  88. list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
  89. ioc->fwfault_debug = mpt3sas_fwfault_debug;
  90. return 0;
  91. }
  92. module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
  93. param_get_int, &mpt3sas_fwfault_debug, 0644);
  94. /**
  95. * mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
  96. * @arg: input argument, used to derive ioc
  97. *
  98. * Return 0 if controller is removed from pci subsystem.
  99. * Return -1 for other case.
  100. */
  101. static int mpt3sas_remove_dead_ioc_func(void *arg)
  102. {
  103. struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
  104. struct pci_dev *pdev;
  105. if ((ioc == NULL))
  106. return -1;
  107. pdev = ioc->pdev;
  108. if ((pdev == NULL))
  109. return -1;
  110. pci_stop_and_remove_bus_device(pdev);
  111. return 0;
  112. }
  113. /**
  114. * _base_fault_reset_work - workq handling ioc fault conditions
  115. * @work: input argument, used to derive ioc
  116. * Context: sleep.
  117. *
  118. * Return nothing.
  119. */
  120. static void
  121. _base_fault_reset_work(struct work_struct *work)
  122. {
  123. struct MPT3SAS_ADAPTER *ioc =
  124. container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
  125. unsigned long flags;
  126. u32 doorbell;
  127. int rc;
  128. struct task_struct *p;
  129. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  130. if (ioc->shost_recovery)
  131. goto rearm_timer;
  132. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  133. doorbell = mpt3sas_base_get_iocstate(ioc, 0);
  134. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
  135. pr_err(MPT3SAS_FMT "SAS host is non-operational !!!!\n",
  136. ioc->name);
  137. /*
  138. * Call _scsih_flush_pending_cmds callback so that we flush all
  139. * pending commands back to OS. This call is required to aovid
  140. * deadlock at block layer. Dead IOC will fail to do diag reset,
  141. * and this call is safe since dead ioc will never return any
  142. * command back from HW.
  143. */
  144. ioc->schedule_dead_ioc_flush_running_cmds(ioc);
  145. /*
  146. * Set remove_host flag early since kernel thread will
  147. * take some time to execute.
  148. */
  149. ioc->remove_host = 1;
  150. /*Remove the Dead Host */
  151. p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
  152. "mpt3sas_dead_ioc_%d", ioc->id);
  153. if (IS_ERR(p))
  154. pr_err(MPT3SAS_FMT
  155. "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
  156. ioc->name, __func__);
  157. else
  158. pr_err(MPT3SAS_FMT
  159. "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
  160. ioc->name, __func__);
  161. return; /* don't rearm timer */
  162. }
  163. if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
  164. rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  165. FORCE_BIG_HAMMER);
  166. pr_warn(MPT3SAS_FMT "%s: hard reset: %s\n", ioc->name,
  167. __func__, (rc == 0) ? "success" : "failed");
  168. doorbell = mpt3sas_base_get_iocstate(ioc, 0);
  169. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  170. mpt3sas_base_fault_info(ioc, doorbell &
  171. MPI2_DOORBELL_DATA_MASK);
  172. if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
  173. MPI2_IOC_STATE_OPERATIONAL)
  174. return; /* don't rearm timer */
  175. }
  176. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  177. rearm_timer:
  178. if (ioc->fault_reset_work_q)
  179. queue_delayed_work(ioc->fault_reset_work_q,
  180. &ioc->fault_reset_work,
  181. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  182. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  183. }
  184. /**
  185. * mpt3sas_base_start_watchdog - start the fault_reset_work_q
  186. * @ioc: per adapter object
  187. * Context: sleep.
  188. *
  189. * Return nothing.
  190. */
  191. void
  192. mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
  193. {
  194. unsigned long flags;
  195. if (ioc->fault_reset_work_q)
  196. return;
  197. /* initialize fault polling */
  198. INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
  199. snprintf(ioc->fault_reset_work_q_name,
  200. sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
  201. ioc->fault_reset_work_q =
  202. create_singlethread_workqueue(ioc->fault_reset_work_q_name);
  203. if (!ioc->fault_reset_work_q) {
  204. pr_err(MPT3SAS_FMT "%s: failed (line=%d)\n",
  205. ioc->name, __func__, __LINE__);
  206. return;
  207. }
  208. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  209. if (ioc->fault_reset_work_q)
  210. queue_delayed_work(ioc->fault_reset_work_q,
  211. &ioc->fault_reset_work,
  212. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  213. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  214. }
  215. /**
  216. * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
  217. * @ioc: per adapter object
  218. * Context: sleep.
  219. *
  220. * Return nothing.
  221. */
  222. void
  223. mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
  224. {
  225. unsigned long flags;
  226. struct workqueue_struct *wq;
  227. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  228. wq = ioc->fault_reset_work_q;
  229. ioc->fault_reset_work_q = NULL;
  230. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  231. if (wq) {
  232. if (!cancel_delayed_work(&ioc->fault_reset_work))
  233. flush_workqueue(wq);
  234. destroy_workqueue(wq);
  235. }
  236. }
  237. /**
  238. * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
  239. * @ioc: per adapter object
  240. * @fault_code: fault code
  241. *
  242. * Return nothing.
  243. */
  244. void
  245. mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code)
  246. {
  247. pr_err(MPT3SAS_FMT "fault_state(0x%04x)!\n",
  248. ioc->name, fault_code);
  249. }
  250. /**
  251. * mpt3sas_halt_firmware - halt's mpt controller firmware
  252. * @ioc: per adapter object
  253. *
  254. * For debugging timeout related issues. Writing 0xCOFFEE00
  255. * to the doorbell register will halt controller firmware. With
  256. * the purpose to stop both driver and firmware, the enduser can
  257. * obtain a ring buffer from controller UART.
  258. */
  259. void
  260. mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
  261. {
  262. u32 doorbell;
  263. if (!ioc->fwfault_debug)
  264. return;
  265. dump_stack();
  266. doorbell = readl(&ioc->chip->Doorbell);
  267. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  268. mpt3sas_base_fault_info(ioc , doorbell);
  269. else {
  270. writel(0xC0FFEE00, &ioc->chip->Doorbell);
  271. pr_err(MPT3SAS_FMT "Firmware is halted due to command timeout\n",
  272. ioc->name);
  273. }
  274. if (ioc->fwfault_debug == 2)
  275. for (;;)
  276. ;
  277. else
  278. panic("panic in %s\n", __func__);
  279. }
  280. #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
  281. /**
  282. * _base_sas_ioc_info - verbose translation of the ioc status
  283. * @ioc: per adapter object
  284. * @mpi_reply: reply mf payload returned from firmware
  285. * @request_hdr: request mf
  286. *
  287. * Return nothing.
  288. */
  289. static void
  290. _base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
  291. MPI2RequestHeader_t *request_hdr)
  292. {
  293. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  294. MPI2_IOCSTATUS_MASK;
  295. char *desc = NULL;
  296. u16 frame_sz;
  297. char *func_str = NULL;
  298. /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
  299. if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  300. request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
  301. request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
  302. return;
  303. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  304. return;
  305. switch (ioc_status) {
  306. /****************************************************************************
  307. * Common IOCStatus values for all replies
  308. ****************************************************************************/
  309. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  310. desc = "invalid function";
  311. break;
  312. case MPI2_IOCSTATUS_BUSY:
  313. desc = "busy";
  314. break;
  315. case MPI2_IOCSTATUS_INVALID_SGL:
  316. desc = "invalid sgl";
  317. break;
  318. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  319. desc = "internal error";
  320. break;
  321. case MPI2_IOCSTATUS_INVALID_VPID:
  322. desc = "invalid vpid";
  323. break;
  324. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  325. desc = "insufficient resources";
  326. break;
  327. case MPI2_IOCSTATUS_INVALID_FIELD:
  328. desc = "invalid field";
  329. break;
  330. case MPI2_IOCSTATUS_INVALID_STATE:
  331. desc = "invalid state";
  332. break;
  333. case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
  334. desc = "op state not supported";
  335. break;
  336. /****************************************************************************
  337. * Config IOCStatus values
  338. ****************************************************************************/
  339. case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
  340. desc = "config invalid action";
  341. break;
  342. case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
  343. desc = "config invalid type";
  344. break;
  345. case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
  346. desc = "config invalid page";
  347. break;
  348. case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
  349. desc = "config invalid data";
  350. break;
  351. case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
  352. desc = "config no defaults";
  353. break;
  354. case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
  355. desc = "config cant commit";
  356. break;
  357. /****************************************************************************
  358. * SCSI IO Reply
  359. ****************************************************************************/
  360. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  361. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  362. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  363. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  364. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  365. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  366. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  367. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  368. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  369. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  370. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  371. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  372. break;
  373. /****************************************************************************
  374. * For use by SCSI Initiator and SCSI Target end-to-end data protection
  375. ****************************************************************************/
  376. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  377. desc = "eedp guard error";
  378. break;
  379. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  380. desc = "eedp ref tag error";
  381. break;
  382. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  383. desc = "eedp app tag error";
  384. break;
  385. /****************************************************************************
  386. * SCSI Target values
  387. ****************************************************************************/
  388. case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
  389. desc = "target invalid io index";
  390. break;
  391. case MPI2_IOCSTATUS_TARGET_ABORTED:
  392. desc = "target aborted";
  393. break;
  394. case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
  395. desc = "target no conn retryable";
  396. break;
  397. case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
  398. desc = "target no connection";
  399. break;
  400. case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
  401. desc = "target xfer count mismatch";
  402. break;
  403. case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
  404. desc = "target data offset error";
  405. break;
  406. case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
  407. desc = "target too much write data";
  408. break;
  409. case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
  410. desc = "target iu too short";
  411. break;
  412. case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
  413. desc = "target ack nak timeout";
  414. break;
  415. case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
  416. desc = "target nak received";
  417. break;
  418. /****************************************************************************
  419. * Serial Attached SCSI values
  420. ****************************************************************************/
  421. case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
  422. desc = "smp request failed";
  423. break;
  424. case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
  425. desc = "smp data overrun";
  426. break;
  427. /****************************************************************************
  428. * Diagnostic Buffer Post / Diagnostic Release values
  429. ****************************************************************************/
  430. case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
  431. desc = "diagnostic released";
  432. break;
  433. default:
  434. break;
  435. }
  436. if (!desc)
  437. return;
  438. switch (request_hdr->Function) {
  439. case MPI2_FUNCTION_CONFIG:
  440. frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
  441. func_str = "config_page";
  442. break;
  443. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  444. frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
  445. func_str = "task_mgmt";
  446. break;
  447. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  448. frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
  449. func_str = "sas_iounit_ctl";
  450. break;
  451. case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
  452. frame_sz = sizeof(Mpi2SepRequest_t);
  453. func_str = "enclosure";
  454. break;
  455. case MPI2_FUNCTION_IOC_INIT:
  456. frame_sz = sizeof(Mpi2IOCInitRequest_t);
  457. func_str = "ioc_init";
  458. break;
  459. case MPI2_FUNCTION_PORT_ENABLE:
  460. frame_sz = sizeof(Mpi2PortEnableRequest_t);
  461. func_str = "port_enable";
  462. break;
  463. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  464. frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
  465. func_str = "smp_passthru";
  466. break;
  467. default:
  468. frame_sz = 32;
  469. func_str = "unknown";
  470. break;
  471. }
  472. pr_warn(MPT3SAS_FMT "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
  473. ioc->name, desc, ioc_status, request_hdr, func_str);
  474. _debug_dump_mf(request_hdr, frame_sz/4);
  475. }
  476. /**
  477. * _base_display_event_data - verbose translation of firmware asyn events
  478. * @ioc: per adapter object
  479. * @mpi_reply: reply mf payload returned from firmware
  480. *
  481. * Return nothing.
  482. */
  483. static void
  484. _base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
  485. Mpi2EventNotificationReply_t *mpi_reply)
  486. {
  487. char *desc = NULL;
  488. u16 event;
  489. if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
  490. return;
  491. event = le16_to_cpu(mpi_reply->Event);
  492. switch (event) {
  493. case MPI2_EVENT_LOG_DATA:
  494. desc = "Log Data";
  495. break;
  496. case MPI2_EVENT_STATE_CHANGE:
  497. desc = "Status Change";
  498. break;
  499. case MPI2_EVENT_HARD_RESET_RECEIVED:
  500. desc = "Hard Reset Received";
  501. break;
  502. case MPI2_EVENT_EVENT_CHANGE:
  503. desc = "Event Change";
  504. break;
  505. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  506. desc = "Device Status Change";
  507. break;
  508. case MPI2_EVENT_IR_OPERATION_STATUS:
  509. desc = "IR Operation Status";
  510. break;
  511. case MPI2_EVENT_SAS_DISCOVERY:
  512. {
  513. Mpi2EventDataSasDiscovery_t *event_data =
  514. (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
  515. pr_info(MPT3SAS_FMT "Discovery: (%s)", ioc->name,
  516. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  517. "start" : "stop");
  518. if (event_data->DiscoveryStatus)
  519. pr_info("discovery_status(0x%08x)",
  520. le32_to_cpu(event_data->DiscoveryStatus));
  521. pr_info("\n");
  522. return;
  523. }
  524. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  525. desc = "SAS Broadcast Primitive";
  526. break;
  527. case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
  528. desc = "SAS Init Device Status Change";
  529. break;
  530. case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
  531. desc = "SAS Init Table Overflow";
  532. break;
  533. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  534. desc = "SAS Topology Change List";
  535. break;
  536. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  537. desc = "SAS Enclosure Device Status Change";
  538. break;
  539. case MPI2_EVENT_IR_VOLUME:
  540. desc = "IR Volume";
  541. break;
  542. case MPI2_EVENT_IR_PHYSICAL_DISK:
  543. desc = "IR Physical Disk";
  544. break;
  545. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  546. desc = "IR Configuration Change List";
  547. break;
  548. case MPI2_EVENT_LOG_ENTRY_ADDED:
  549. desc = "Log Entry Added";
  550. break;
  551. }
  552. if (!desc)
  553. return;
  554. pr_info(MPT3SAS_FMT "%s\n", ioc->name, desc);
  555. }
  556. #endif
  557. /**
  558. * _base_sas_log_info - verbose translation of firmware log info
  559. * @ioc: per adapter object
  560. * @log_info: log info
  561. *
  562. * Return nothing.
  563. */
  564. static void
  565. _base_sas_log_info(struct MPT3SAS_ADAPTER *ioc , u32 log_info)
  566. {
  567. union loginfo_type {
  568. u32 loginfo;
  569. struct {
  570. u32 subcode:16;
  571. u32 code:8;
  572. u32 originator:4;
  573. u32 bus_type:4;
  574. } dw;
  575. };
  576. union loginfo_type sas_loginfo;
  577. char *originator_str = NULL;
  578. sas_loginfo.loginfo = log_info;
  579. if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
  580. return;
  581. /* each nexus loss loginfo */
  582. if (log_info == 0x31170000)
  583. return;
  584. /* eat the loginfos associated with task aborts */
  585. if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
  586. 0x31140000 || log_info == 0x31130000))
  587. return;
  588. switch (sas_loginfo.dw.originator) {
  589. case 0:
  590. originator_str = "IOP";
  591. break;
  592. case 1:
  593. originator_str = "PL";
  594. break;
  595. case 2:
  596. originator_str = "IR";
  597. break;
  598. }
  599. pr_warn(MPT3SAS_FMT
  600. "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
  601. ioc->name, log_info,
  602. originator_str, sas_loginfo.dw.code,
  603. sas_loginfo.dw.subcode);
  604. }
  605. /**
  606. * _base_display_reply_info -
  607. * @ioc: per adapter object
  608. * @smid: system request message index
  609. * @msix_index: MSIX table index supplied by the OS
  610. * @reply: reply message frame(lower 32bit addr)
  611. *
  612. * Return nothing.
  613. */
  614. static void
  615. _base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  616. u32 reply)
  617. {
  618. MPI2DefaultReply_t *mpi_reply;
  619. u16 ioc_status;
  620. u32 loginfo = 0;
  621. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  622. if (unlikely(!mpi_reply)) {
  623. pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  624. ioc->name, __FILE__, __LINE__, __func__);
  625. return;
  626. }
  627. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  628. #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
  629. if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
  630. (ioc->logging_level & MPT_DEBUG_REPLY)) {
  631. _base_sas_ioc_info(ioc , mpi_reply,
  632. mpt3sas_base_get_msg_frame(ioc, smid));
  633. }
  634. #endif
  635. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
  636. loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
  637. _base_sas_log_info(ioc, loginfo);
  638. }
  639. if (ioc_status || loginfo) {
  640. ioc_status &= MPI2_IOCSTATUS_MASK;
  641. mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
  642. }
  643. }
  644. /**
  645. * mpt3sas_base_done - base internal command completion routine
  646. * @ioc: per adapter object
  647. * @smid: system request message index
  648. * @msix_index: MSIX table index supplied by the OS
  649. * @reply: reply message frame(lower 32bit addr)
  650. *
  651. * Return 1 meaning mf should be freed from _base_interrupt
  652. * 0 means the mf is freed from this function.
  653. */
  654. u8
  655. mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  656. u32 reply)
  657. {
  658. MPI2DefaultReply_t *mpi_reply;
  659. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  660. if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
  661. return 1;
  662. if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
  663. return 1;
  664. ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
  665. if (mpi_reply) {
  666. ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
  667. memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  668. }
  669. ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
  670. complete(&ioc->base_cmds.done);
  671. return 1;
  672. }
  673. /**
  674. * _base_async_event - main callback handler for firmware asyn events
  675. * @ioc: per adapter object
  676. * @msix_index: MSIX table index supplied by the OS
  677. * @reply: reply message frame(lower 32bit addr)
  678. *
  679. * Return 1 meaning mf should be freed from _base_interrupt
  680. * 0 means the mf is freed from this function.
  681. */
  682. static u8
  683. _base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
  684. {
  685. Mpi2EventNotificationReply_t *mpi_reply;
  686. Mpi2EventAckRequest_t *ack_request;
  687. u16 smid;
  688. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  689. if (!mpi_reply)
  690. return 1;
  691. if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
  692. return 1;
  693. #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
  694. _base_display_event_data(ioc, mpi_reply);
  695. #endif
  696. if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
  697. goto out;
  698. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  699. if (!smid) {
  700. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  701. ioc->name, __func__);
  702. goto out;
  703. }
  704. ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
  705. memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
  706. ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
  707. ack_request->Event = mpi_reply->Event;
  708. ack_request->EventContext = mpi_reply->EventContext;
  709. ack_request->VF_ID = 0; /* TODO */
  710. ack_request->VP_ID = 0;
  711. mpt3sas_base_put_smid_default(ioc, smid);
  712. out:
  713. /* scsih callback handler */
  714. mpt3sas_scsih_event_callback(ioc, msix_index, reply);
  715. /* ctl callback handler */
  716. mpt3sas_ctl_event_callback(ioc, msix_index, reply);
  717. return 1;
  718. }
  719. /**
  720. * _base_get_cb_idx - obtain the callback index
  721. * @ioc: per adapter object
  722. * @smid: system request message index
  723. *
  724. * Return callback index.
  725. */
  726. static u8
  727. _base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  728. {
  729. int i;
  730. u8 cb_idx;
  731. if (smid < ioc->hi_priority_smid) {
  732. i = smid - 1;
  733. cb_idx = ioc->scsi_lookup[i].cb_idx;
  734. } else if (smid < ioc->internal_smid) {
  735. i = smid - ioc->hi_priority_smid;
  736. cb_idx = ioc->hpr_lookup[i].cb_idx;
  737. } else if (smid <= ioc->hba_queue_depth) {
  738. i = smid - ioc->internal_smid;
  739. cb_idx = ioc->internal_lookup[i].cb_idx;
  740. } else
  741. cb_idx = 0xFF;
  742. return cb_idx;
  743. }
  744. /**
  745. * _base_mask_interrupts - disable interrupts
  746. * @ioc: per adapter object
  747. *
  748. * Disabling ResetIRQ, Reply and Doorbell Interrupts
  749. *
  750. * Return nothing.
  751. */
  752. static void
  753. _base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
  754. {
  755. u32 him_register;
  756. ioc->mask_interrupts = 1;
  757. him_register = readl(&ioc->chip->HostInterruptMask);
  758. him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
  759. writel(him_register, &ioc->chip->HostInterruptMask);
  760. readl(&ioc->chip->HostInterruptMask);
  761. }
  762. /**
  763. * _base_unmask_interrupts - enable interrupts
  764. * @ioc: per adapter object
  765. *
  766. * Enabling only Reply Interrupts
  767. *
  768. * Return nothing.
  769. */
  770. static void
  771. _base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
  772. {
  773. u32 him_register;
  774. him_register = readl(&ioc->chip->HostInterruptMask);
  775. him_register &= ~MPI2_HIM_RIM;
  776. writel(him_register, &ioc->chip->HostInterruptMask);
  777. ioc->mask_interrupts = 0;
  778. }
  779. union reply_descriptor {
  780. u64 word;
  781. struct {
  782. u32 low;
  783. u32 high;
  784. } u;
  785. };
  786. /**
  787. * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
  788. * @irq: irq number (not used)
  789. * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
  790. * @r: pt_regs pointer (not used)
  791. *
  792. * Return IRQ_HANDLE if processed, else IRQ_NONE.
  793. */
  794. static irqreturn_t
  795. _base_interrupt(int irq, void *bus_id)
  796. {
  797. struct adapter_reply_queue *reply_q = bus_id;
  798. union reply_descriptor rd;
  799. u32 completed_cmds;
  800. u8 request_desript_type;
  801. u16 smid;
  802. u8 cb_idx;
  803. u32 reply;
  804. u8 msix_index = reply_q->msix_index;
  805. struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
  806. Mpi2ReplyDescriptorsUnion_t *rpf;
  807. u8 rc;
  808. if (ioc->mask_interrupts)
  809. return IRQ_NONE;
  810. if (!atomic_add_unless(&reply_q->busy, 1, 1))
  811. return IRQ_NONE;
  812. rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
  813. request_desript_type = rpf->Default.ReplyFlags
  814. & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  815. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
  816. atomic_dec(&reply_q->busy);
  817. return IRQ_NONE;
  818. }
  819. completed_cmds = 0;
  820. cb_idx = 0xFF;
  821. do {
  822. rd.word = le64_to_cpu(rpf->Words);
  823. if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
  824. goto out;
  825. reply = 0;
  826. smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
  827. if (request_desript_type ==
  828. MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
  829. request_desript_type ==
  830. MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
  831. cb_idx = _base_get_cb_idx(ioc, smid);
  832. if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
  833. (likely(mpt_callbacks[cb_idx] != NULL))) {
  834. rc = mpt_callbacks[cb_idx](ioc, smid,
  835. msix_index, 0);
  836. if (rc)
  837. mpt3sas_base_free_smid(ioc, smid);
  838. }
  839. } else if (request_desript_type ==
  840. MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
  841. reply = le32_to_cpu(
  842. rpf->AddressReply.ReplyFrameAddress);
  843. if (reply > ioc->reply_dma_max_address ||
  844. reply < ioc->reply_dma_min_address)
  845. reply = 0;
  846. if (smid) {
  847. cb_idx = _base_get_cb_idx(ioc, smid);
  848. if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
  849. (likely(mpt_callbacks[cb_idx] != NULL))) {
  850. rc = mpt_callbacks[cb_idx](ioc, smid,
  851. msix_index, reply);
  852. if (reply)
  853. _base_display_reply_info(ioc,
  854. smid, msix_index, reply);
  855. if (rc)
  856. mpt3sas_base_free_smid(ioc,
  857. smid);
  858. }
  859. } else {
  860. _base_async_event(ioc, msix_index, reply);
  861. }
  862. /* reply free queue handling */
  863. if (reply) {
  864. ioc->reply_free_host_index =
  865. (ioc->reply_free_host_index ==
  866. (ioc->reply_free_queue_depth - 1)) ?
  867. 0 : ioc->reply_free_host_index + 1;
  868. ioc->reply_free[ioc->reply_free_host_index] =
  869. cpu_to_le32(reply);
  870. wmb();
  871. writel(ioc->reply_free_host_index,
  872. &ioc->chip->ReplyFreeHostIndex);
  873. }
  874. }
  875. rpf->Words = cpu_to_le64(ULLONG_MAX);
  876. reply_q->reply_post_host_index =
  877. (reply_q->reply_post_host_index ==
  878. (ioc->reply_post_queue_depth - 1)) ? 0 :
  879. reply_q->reply_post_host_index + 1;
  880. request_desript_type =
  881. reply_q->reply_post_free[reply_q->reply_post_host_index].
  882. Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  883. completed_cmds++;
  884. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  885. goto out;
  886. if (!reply_q->reply_post_host_index)
  887. rpf = reply_q->reply_post_free;
  888. else
  889. rpf++;
  890. } while (1);
  891. out:
  892. if (!completed_cmds) {
  893. atomic_dec(&reply_q->busy);
  894. return IRQ_NONE;
  895. }
  896. wmb();
  897. writel(reply_q->reply_post_host_index | (msix_index <<
  898. MPI2_RPHI_MSIX_INDEX_SHIFT), &ioc->chip->ReplyPostHostIndex);
  899. atomic_dec(&reply_q->busy);
  900. return IRQ_HANDLED;
  901. }
  902. /**
  903. * _base_is_controller_msix_enabled - is controller support muli-reply queues
  904. * @ioc: per adapter object
  905. *
  906. */
  907. static inline int
  908. _base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
  909. {
  910. return (ioc->facts.IOCCapabilities &
  911. MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
  912. }
  913. /**
  914. * mpt3sas_base_flush_reply_queues - flushing the MSIX reply queues
  915. * @ioc: per adapter object
  916. * Context: ISR conext
  917. *
  918. * Called when a Task Management request has completed. We want
  919. * to flush the other reply queues so all the outstanding IO has been
  920. * completed back to OS before we process the TM completetion.
  921. *
  922. * Return nothing.
  923. */
  924. void
  925. mpt3sas_base_flush_reply_queues(struct MPT3SAS_ADAPTER *ioc)
  926. {
  927. struct adapter_reply_queue *reply_q;
  928. /* If MSIX capability is turned off
  929. * then multi-queues are not enabled
  930. */
  931. if (!_base_is_controller_msix_enabled(ioc))
  932. return;
  933. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  934. if (ioc->shost_recovery)
  935. return;
  936. /* TMs are on msix_index == 0 */
  937. if (reply_q->msix_index == 0)
  938. continue;
  939. _base_interrupt(reply_q->vector, (void *)reply_q);
  940. }
  941. }
  942. /**
  943. * mpt3sas_base_release_callback_handler - clear interrupt callback handler
  944. * @cb_idx: callback index
  945. *
  946. * Return nothing.
  947. */
  948. void
  949. mpt3sas_base_release_callback_handler(u8 cb_idx)
  950. {
  951. mpt_callbacks[cb_idx] = NULL;
  952. }
  953. /**
  954. * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
  955. * @cb_func: callback function
  956. *
  957. * Returns cb_func.
  958. */
  959. u8
  960. mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
  961. {
  962. u8 cb_idx;
  963. for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
  964. if (mpt_callbacks[cb_idx] == NULL)
  965. break;
  966. mpt_callbacks[cb_idx] = cb_func;
  967. return cb_idx;
  968. }
  969. /**
  970. * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
  971. *
  972. * Return nothing.
  973. */
  974. void
  975. mpt3sas_base_initialize_callback_handler(void)
  976. {
  977. u8 cb_idx;
  978. for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
  979. mpt3sas_base_release_callback_handler(cb_idx);
  980. }
  981. /**
  982. * _base_build_zero_len_sge - build zero length sg entry
  983. * @ioc: per adapter object
  984. * @paddr: virtual address for SGE
  985. *
  986. * Create a zero length scatter gather entry to insure the IOCs hardware has
  987. * something to use if the target device goes brain dead and tries
  988. * to send data even when none is asked for.
  989. *
  990. * Return nothing.
  991. */
  992. static void
  993. _base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
  994. {
  995. u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
  996. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
  997. MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
  998. MPI2_SGE_FLAGS_SHIFT);
  999. ioc->base_add_sg_single(paddr, flags_length, -1);
  1000. }
  1001. /**
  1002. * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
  1003. * @paddr: virtual address for SGE
  1004. * @flags_length: SGE flags and data transfer length
  1005. * @dma_addr: Physical address
  1006. *
  1007. * Return nothing.
  1008. */
  1009. static void
  1010. _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  1011. {
  1012. Mpi2SGESimple32_t *sgel = paddr;
  1013. flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
  1014. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  1015. sgel->FlagsLength = cpu_to_le32(flags_length);
  1016. sgel->Address = cpu_to_le32(dma_addr);
  1017. }
  1018. /**
  1019. * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
  1020. * @paddr: virtual address for SGE
  1021. * @flags_length: SGE flags and data transfer length
  1022. * @dma_addr: Physical address
  1023. *
  1024. * Return nothing.
  1025. */
  1026. static void
  1027. _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  1028. {
  1029. Mpi2SGESimple64_t *sgel = paddr;
  1030. flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
  1031. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  1032. sgel->FlagsLength = cpu_to_le32(flags_length);
  1033. sgel->Address = cpu_to_le64(dma_addr);
  1034. }
  1035. /**
  1036. * _base_get_chain_buffer_tracker - obtain chain tracker
  1037. * @ioc: per adapter object
  1038. * @smid: smid associated to an IO request
  1039. *
  1040. * Returns chain tracker(from ioc->free_chain_list)
  1041. */
  1042. static struct chain_tracker *
  1043. _base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1044. {
  1045. struct chain_tracker *chain_req;
  1046. unsigned long flags;
  1047. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1048. if (list_empty(&ioc->free_chain_list)) {
  1049. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1050. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  1051. "chain buffers not available\n", ioc->name));
  1052. return NULL;
  1053. }
  1054. chain_req = list_entry(ioc->free_chain_list.next,
  1055. struct chain_tracker, tracker_list);
  1056. list_del_init(&chain_req->tracker_list);
  1057. list_add_tail(&chain_req->tracker_list,
  1058. &ioc->scsi_lookup[smid - 1].chain_list);
  1059. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1060. return chain_req;
  1061. }
  1062. /**
  1063. * _base_build_sg - build generic sg
  1064. * @ioc: per adapter object
  1065. * @psge: virtual address for SGE
  1066. * @data_out_dma: physical address for WRITES
  1067. * @data_out_sz: data xfer size for WRITES
  1068. * @data_in_dma: physical address for READS
  1069. * @data_in_sz: data xfer size for READS
  1070. *
  1071. * Return nothing.
  1072. */
  1073. static void
  1074. _base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
  1075. dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
  1076. size_t data_in_sz)
  1077. {
  1078. u32 sgl_flags;
  1079. if (!data_out_sz && !data_in_sz) {
  1080. _base_build_zero_len_sge(ioc, psge);
  1081. return;
  1082. }
  1083. if (data_out_sz && data_in_sz) {
  1084. /* WRITE sgel first */
  1085. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1086. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1087. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1088. ioc->base_add_sg_single(psge, sgl_flags |
  1089. data_out_sz, data_out_dma);
  1090. /* incr sgel */
  1091. psge += ioc->sge_size;
  1092. /* READ sgel last */
  1093. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1094. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1095. MPI2_SGE_FLAGS_END_OF_LIST);
  1096. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1097. ioc->base_add_sg_single(psge, sgl_flags |
  1098. data_in_sz, data_in_dma);
  1099. } else if (data_out_sz) /* WRITE */ {
  1100. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1101. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1102. MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1103. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1104. ioc->base_add_sg_single(psge, sgl_flags |
  1105. data_out_sz, data_out_dma);
  1106. } else if (data_in_sz) /* READ */ {
  1107. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1108. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1109. MPI2_SGE_FLAGS_END_OF_LIST);
  1110. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1111. ioc->base_add_sg_single(psge, sgl_flags |
  1112. data_in_sz, data_in_dma);
  1113. }
  1114. }
  1115. /* IEEE format sgls */
  1116. /**
  1117. * _base_add_sg_single_ieee - add sg element for IEEE format
  1118. * @paddr: virtual address for SGE
  1119. * @flags: SGE flags
  1120. * @chain_offset: number of 128 byte elements from start of segment
  1121. * @length: data transfer length
  1122. * @dma_addr: Physical address
  1123. *
  1124. * Return nothing.
  1125. */
  1126. static void
  1127. _base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
  1128. dma_addr_t dma_addr)
  1129. {
  1130. Mpi25IeeeSgeChain64_t *sgel = paddr;
  1131. sgel->Flags = flags;
  1132. sgel->NextChainOffset = chain_offset;
  1133. sgel->Length = cpu_to_le32(length);
  1134. sgel->Address = cpu_to_le64(dma_addr);
  1135. }
  1136. /**
  1137. * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
  1138. * @ioc: per adapter object
  1139. * @paddr: virtual address for SGE
  1140. *
  1141. * Create a zero length scatter gather entry to insure the IOCs hardware has
  1142. * something to use if the target device goes brain dead and tries
  1143. * to send data even when none is asked for.
  1144. *
  1145. * Return nothing.
  1146. */
  1147. static void
  1148. _base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
  1149. {
  1150. u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1151. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
  1152. MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
  1153. _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
  1154. }
  1155. /**
  1156. * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
  1157. * @ioc: per adapter object
  1158. * @scmd: scsi command
  1159. * @smid: system request message index
  1160. * Context: none.
  1161. *
  1162. * The main routine that builds scatter gather table from a given
  1163. * scsi request sent via the .queuecommand main handler.
  1164. *
  1165. * Returns 0 success, anything else error
  1166. */
  1167. static int
  1168. _base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
  1169. struct scsi_cmnd *scmd, u16 smid)
  1170. {
  1171. Mpi2SCSIIORequest_t *mpi_request;
  1172. dma_addr_t chain_dma;
  1173. struct scatterlist *sg_scmd;
  1174. void *sg_local, *chain;
  1175. u32 chain_offset;
  1176. u32 chain_length;
  1177. u32 chain_flags;
  1178. int sges_left;
  1179. u32 sges_in_segment;
  1180. u8 simple_sgl_flags;
  1181. u8 simple_sgl_flags_last;
  1182. u8 chain_sgl_flags;
  1183. struct chain_tracker *chain_req;
  1184. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1185. /* init scatter gather flags */
  1186. simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1187. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1188. simple_sgl_flags_last = simple_sgl_flags |
  1189. MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
  1190. chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  1191. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1192. sg_scmd = scsi_sglist(scmd);
  1193. sges_left = scsi_dma_map(scmd);
  1194. if (!sges_left) {
  1195. sdev_printk(KERN_ERR, scmd->device,
  1196. "pci_map_sg failed: request for %d bytes!\n",
  1197. scsi_bufflen(scmd));
  1198. return -ENOMEM;
  1199. }
  1200. sg_local = &mpi_request->SGL;
  1201. sges_in_segment = (ioc->request_sz -
  1202. offsetof(Mpi2SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
  1203. if (sges_left <= sges_in_segment)
  1204. goto fill_in_last_segment;
  1205. mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
  1206. (offsetof(Mpi2SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
  1207. /* fill in main message segment when there is a chain following */
  1208. while (sges_in_segment > 1) {
  1209. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  1210. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  1211. sg_scmd = sg_next(sg_scmd);
  1212. sg_local += ioc->sge_size_ieee;
  1213. sges_left--;
  1214. sges_in_segment--;
  1215. }
  1216. /* initializing the chain flags and pointers */
  1217. chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
  1218. chain_req = _base_get_chain_buffer_tracker(ioc, smid);
  1219. if (!chain_req)
  1220. return -1;
  1221. chain = chain_req->chain_buffer;
  1222. chain_dma = chain_req->chain_buffer_dma;
  1223. do {
  1224. sges_in_segment = (sges_left <=
  1225. ioc->max_sges_in_chain_message) ? sges_left :
  1226. ioc->max_sges_in_chain_message;
  1227. chain_offset = (sges_left == sges_in_segment) ?
  1228. 0 : sges_in_segment;
  1229. chain_length = sges_in_segment * ioc->sge_size_ieee;
  1230. if (chain_offset)
  1231. chain_length += ioc->sge_size_ieee;
  1232. _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
  1233. chain_offset, chain_length, chain_dma);
  1234. sg_local = chain;
  1235. if (!chain_offset)
  1236. goto fill_in_last_segment;
  1237. /* fill in chain segments */
  1238. while (sges_in_segment) {
  1239. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  1240. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  1241. sg_scmd = sg_next(sg_scmd);
  1242. sg_local += ioc->sge_size_ieee;
  1243. sges_left--;
  1244. sges_in_segment--;
  1245. }
  1246. chain_req = _base_get_chain_buffer_tracker(ioc, smid);
  1247. if (!chain_req)
  1248. return -1;
  1249. chain = chain_req->chain_buffer;
  1250. chain_dma = chain_req->chain_buffer_dma;
  1251. } while (1);
  1252. fill_in_last_segment:
  1253. /* fill the last segment */
  1254. while (sges_left) {
  1255. if (sges_left == 1)
  1256. _base_add_sg_single_ieee(sg_local,
  1257. simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
  1258. sg_dma_address(sg_scmd));
  1259. else
  1260. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  1261. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  1262. sg_scmd = sg_next(sg_scmd);
  1263. sg_local += ioc->sge_size_ieee;
  1264. sges_left--;
  1265. }
  1266. return 0;
  1267. }
  1268. /**
  1269. * _base_build_sg_ieee - build generic sg for IEEE format
  1270. * @ioc: per adapter object
  1271. * @psge: virtual address for SGE
  1272. * @data_out_dma: physical address for WRITES
  1273. * @data_out_sz: data xfer size for WRITES
  1274. * @data_in_dma: physical address for READS
  1275. * @data_in_sz: data xfer size for READS
  1276. *
  1277. * Return nothing.
  1278. */
  1279. static void
  1280. _base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
  1281. dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
  1282. size_t data_in_sz)
  1283. {
  1284. u8 sgl_flags;
  1285. if (!data_out_sz && !data_in_sz) {
  1286. _base_build_zero_len_sge_ieee(ioc, psge);
  1287. return;
  1288. }
  1289. if (data_out_sz && data_in_sz) {
  1290. /* WRITE sgel first */
  1291. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1292. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1293. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
  1294. data_out_dma);
  1295. /* incr sgel */
  1296. psge += ioc->sge_size_ieee;
  1297. /* READ sgel last */
  1298. sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
  1299. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
  1300. data_in_dma);
  1301. } else if (data_out_sz) /* WRITE */ {
  1302. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1303. MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
  1304. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1305. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
  1306. data_out_dma);
  1307. } else if (data_in_sz) /* READ */ {
  1308. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1309. MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
  1310. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1311. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
  1312. data_in_dma);
  1313. }
  1314. }
  1315. #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
  1316. /**
  1317. * _base_config_dma_addressing - set dma addressing
  1318. * @ioc: per adapter object
  1319. * @pdev: PCI device struct
  1320. *
  1321. * Returns 0 for success, non-zero for failure.
  1322. */
  1323. static int
  1324. _base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
  1325. {
  1326. struct sysinfo s;
  1327. char *desc = NULL;
  1328. if (sizeof(dma_addr_t) > 4) {
  1329. const uint64_t required_mask =
  1330. dma_get_required_mask(&pdev->dev);
  1331. if ((required_mask > DMA_BIT_MASK(32)) &&
  1332. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
  1333. !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
  1334. ioc->base_add_sg_single = &_base_add_sg_single_64;
  1335. ioc->sge_size = sizeof(Mpi2SGESimple64_t);
  1336. desc = "64";
  1337. goto out;
  1338. }
  1339. }
  1340. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
  1341. && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
  1342. ioc->base_add_sg_single = &_base_add_sg_single_32;
  1343. ioc->sge_size = sizeof(Mpi2SGESimple32_t);
  1344. desc = "32";
  1345. } else
  1346. return -ENODEV;
  1347. out:
  1348. si_meminfo(&s);
  1349. pr_info(MPT3SAS_FMT
  1350. "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
  1351. ioc->name, desc, convert_to_kb(s.totalram));
  1352. return 0;
  1353. }
  1354. /**
  1355. * _base_check_enable_msix - checks MSIX capabable.
  1356. * @ioc: per adapter object
  1357. *
  1358. * Check to see if card is capable of MSIX, and set number
  1359. * of available msix vectors
  1360. */
  1361. static int
  1362. _base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
  1363. {
  1364. int base;
  1365. u16 message_control;
  1366. base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
  1367. if (!base) {
  1368. dfailprintk(ioc, pr_info(MPT3SAS_FMT "msix not supported\n",
  1369. ioc->name));
  1370. return -EINVAL;
  1371. }
  1372. /* get msix vector count */
  1373. pci_read_config_word(ioc->pdev, base + 2, &message_control);
  1374. ioc->msix_vector_count = (message_control & 0x3FF) + 1;
  1375. if (ioc->msix_vector_count > 8)
  1376. ioc->msix_vector_count = 8;
  1377. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  1378. "msix is supported, vector_count(%d)\n",
  1379. ioc->name, ioc->msix_vector_count));
  1380. return 0;
  1381. }
  1382. /**
  1383. * _base_free_irq - free irq
  1384. * @ioc: per adapter object
  1385. *
  1386. * Freeing respective reply_queue from the list.
  1387. */
  1388. static void
  1389. _base_free_irq(struct MPT3SAS_ADAPTER *ioc)
  1390. {
  1391. struct adapter_reply_queue *reply_q, *next;
  1392. if (list_empty(&ioc->reply_queue_list))
  1393. return;
  1394. list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
  1395. list_del(&reply_q->list);
  1396. synchronize_irq(reply_q->vector);
  1397. free_irq(reply_q->vector, reply_q);
  1398. kfree(reply_q);
  1399. }
  1400. }
  1401. /**
  1402. * _base_request_irq - request irq
  1403. * @ioc: per adapter object
  1404. * @index: msix index into vector table
  1405. * @vector: irq vector
  1406. *
  1407. * Inserting respective reply_queue into the list.
  1408. */
  1409. static int
  1410. _base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index, u32 vector)
  1411. {
  1412. struct adapter_reply_queue *reply_q;
  1413. int r;
  1414. reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
  1415. if (!reply_q) {
  1416. pr_err(MPT3SAS_FMT "unable to allocate memory %d!\n",
  1417. ioc->name, (int)sizeof(struct adapter_reply_queue));
  1418. return -ENOMEM;
  1419. }
  1420. reply_q->ioc = ioc;
  1421. reply_q->msix_index = index;
  1422. reply_q->vector = vector;
  1423. atomic_set(&reply_q->busy, 0);
  1424. if (ioc->msix_enable)
  1425. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
  1426. MPT3SAS_DRIVER_NAME, ioc->id, index);
  1427. else
  1428. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
  1429. MPT3SAS_DRIVER_NAME, ioc->id);
  1430. r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
  1431. reply_q);
  1432. if (r) {
  1433. pr_err(MPT3SAS_FMT "unable to allocate interrupt %d!\n",
  1434. reply_q->name, vector);
  1435. kfree(reply_q);
  1436. return -EBUSY;
  1437. }
  1438. INIT_LIST_HEAD(&reply_q->list);
  1439. list_add_tail(&reply_q->list, &ioc->reply_queue_list);
  1440. return 0;
  1441. }
  1442. /**
  1443. * _base_assign_reply_queues - assigning msix index for each cpu
  1444. * @ioc: per adapter object
  1445. *
  1446. * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
  1447. *
  1448. * It would nice if we could call irq_set_affinity, however it is not
  1449. * an exported symbol
  1450. */
  1451. static void
  1452. _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
  1453. {
  1454. struct adapter_reply_queue *reply_q;
  1455. int cpu_id;
  1456. int cpu_grouping, loop, grouping, grouping_mod;
  1457. int reply_queue;
  1458. if (!_base_is_controller_msix_enabled(ioc))
  1459. return;
  1460. memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
  1461. /* NUMA Hardware bug workaround - drop to less reply queues */
  1462. if (ioc->reply_queue_count > ioc->facts.MaxMSIxVectors) {
  1463. ioc->reply_queue_count = ioc->facts.MaxMSIxVectors;
  1464. reply_queue = 0;
  1465. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  1466. reply_q->msix_index = reply_queue;
  1467. if (++reply_queue == ioc->reply_queue_count)
  1468. reply_queue = 0;
  1469. }
  1470. }
  1471. /* when there are more cpus than available msix vectors,
  1472. * then group cpus togeather on same irq
  1473. */
  1474. if (ioc->cpu_count > ioc->msix_vector_count) {
  1475. grouping = ioc->cpu_count / ioc->msix_vector_count;
  1476. grouping_mod = ioc->cpu_count % ioc->msix_vector_count;
  1477. if (grouping < 2 || (grouping == 2 && !grouping_mod))
  1478. cpu_grouping = 2;
  1479. else if (grouping < 4 || (grouping == 4 && !grouping_mod))
  1480. cpu_grouping = 4;
  1481. else if (grouping < 8 || (grouping == 8 && !grouping_mod))
  1482. cpu_grouping = 8;
  1483. else
  1484. cpu_grouping = 16;
  1485. } else
  1486. cpu_grouping = 0;
  1487. loop = 0;
  1488. reply_q = list_entry(ioc->reply_queue_list.next,
  1489. struct adapter_reply_queue, list);
  1490. for_each_online_cpu(cpu_id) {
  1491. if (!cpu_grouping) {
  1492. ioc->cpu_msix_table[cpu_id] = reply_q->msix_index;
  1493. reply_q = list_entry(reply_q->list.next,
  1494. struct adapter_reply_queue, list);
  1495. } else {
  1496. if (loop < cpu_grouping) {
  1497. ioc->cpu_msix_table[cpu_id] =
  1498. reply_q->msix_index;
  1499. loop++;
  1500. } else {
  1501. reply_q = list_entry(reply_q->list.next,
  1502. struct adapter_reply_queue, list);
  1503. ioc->cpu_msix_table[cpu_id] =
  1504. reply_q->msix_index;
  1505. loop = 1;
  1506. }
  1507. }
  1508. }
  1509. }
  1510. /**
  1511. * _base_disable_msix - disables msix
  1512. * @ioc: per adapter object
  1513. *
  1514. */
  1515. static void
  1516. _base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
  1517. {
  1518. if (!ioc->msix_enable)
  1519. return;
  1520. pci_disable_msix(ioc->pdev);
  1521. ioc->msix_enable = 0;
  1522. }
  1523. /**
  1524. * _base_enable_msix - enables msix, failback to io_apic
  1525. * @ioc: per adapter object
  1526. *
  1527. */
  1528. static int
  1529. _base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
  1530. {
  1531. struct msix_entry *entries, *a;
  1532. int r;
  1533. int i;
  1534. u8 try_msix = 0;
  1535. INIT_LIST_HEAD(&ioc->reply_queue_list);
  1536. if (msix_disable == -1 || msix_disable == 0)
  1537. try_msix = 1;
  1538. if (!try_msix)
  1539. goto try_ioapic;
  1540. if (_base_check_enable_msix(ioc) != 0)
  1541. goto try_ioapic;
  1542. ioc->reply_queue_count = min_t(int, ioc->cpu_count,
  1543. ioc->msix_vector_count);
  1544. entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
  1545. GFP_KERNEL);
  1546. if (!entries) {
  1547. dfailprintk(ioc, pr_info(MPT3SAS_FMT
  1548. "kcalloc failed @ at %s:%d/%s() !!!\n",
  1549. ioc->name, __FILE__, __LINE__, __func__));
  1550. goto try_ioapic;
  1551. }
  1552. for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
  1553. a->entry = i;
  1554. r = pci_enable_msix(ioc->pdev, entries, ioc->reply_queue_count);
  1555. if (r) {
  1556. dfailprintk(ioc, pr_info(MPT3SAS_FMT
  1557. "pci_enable_msix failed (r=%d) !!!\n",
  1558. ioc->name, r));
  1559. kfree(entries);
  1560. goto try_ioapic;
  1561. }
  1562. ioc->msix_enable = 1;
  1563. for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
  1564. r = _base_request_irq(ioc, i, a->vector);
  1565. if (r) {
  1566. _base_free_irq(ioc);
  1567. _base_disable_msix(ioc);
  1568. kfree(entries);
  1569. goto try_ioapic;
  1570. }
  1571. }
  1572. kfree(entries);
  1573. return 0;
  1574. /* failback to io_apic interrupt routing */
  1575. try_ioapic:
  1576. r = _base_request_irq(ioc, 0, ioc->pdev->irq);
  1577. return r;
  1578. }
  1579. /**
  1580. * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
  1581. * @ioc: per adapter object
  1582. *
  1583. * Returns 0 for success, non-zero for failure.
  1584. */
  1585. int
  1586. mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
  1587. {
  1588. struct pci_dev *pdev = ioc->pdev;
  1589. u32 memap_sz;
  1590. u32 pio_sz;
  1591. int i, r = 0;
  1592. u64 pio_chip = 0;
  1593. u64 chip_phys = 0;
  1594. struct adapter_reply_queue *reply_q;
  1595. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n",
  1596. ioc->name, __func__));
  1597. ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
  1598. if (pci_enable_device_mem(pdev)) {
  1599. pr_warn(MPT3SAS_FMT "pci_enable_device_mem: failed\n",
  1600. ioc->name);
  1601. return -ENODEV;
  1602. }
  1603. if (pci_request_selected_regions(pdev, ioc->bars,
  1604. MPT3SAS_DRIVER_NAME)) {
  1605. pr_warn(MPT3SAS_FMT "pci_request_selected_regions: failed\n",
  1606. ioc->name);
  1607. r = -ENODEV;
  1608. goto out_fail;
  1609. }
  1610. /* AER (Advanced Error Reporting) hooks */
  1611. pci_enable_pcie_error_reporting(pdev);
  1612. pci_set_master(pdev);
  1613. if (_base_config_dma_addressing(ioc, pdev) != 0) {
  1614. pr_warn(MPT3SAS_FMT "no suitable DMA mask for %s\n",
  1615. ioc->name, pci_name(pdev));
  1616. r = -ENODEV;
  1617. goto out_fail;
  1618. }
  1619. for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
  1620. if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
  1621. if (pio_sz)
  1622. continue;
  1623. pio_chip = (u64)pci_resource_start(pdev, i);
  1624. pio_sz = pci_resource_len(pdev, i);
  1625. } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  1626. if (memap_sz)
  1627. continue;
  1628. ioc->chip_phys = pci_resource_start(pdev, i);
  1629. chip_phys = (u64)ioc->chip_phys;
  1630. memap_sz = pci_resource_len(pdev, i);
  1631. ioc->chip = ioremap(ioc->chip_phys, memap_sz);
  1632. if (ioc->chip == NULL) {
  1633. pr_err(MPT3SAS_FMT "unable to map adapter memory!\n",
  1634. ioc->name);
  1635. r = -EINVAL;
  1636. goto out_fail;
  1637. }
  1638. }
  1639. }
  1640. _base_mask_interrupts(ioc);
  1641. r = _base_enable_msix(ioc);
  1642. if (r)
  1643. goto out_fail;
  1644. list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
  1645. pr_info(MPT3SAS_FMT "%s: IRQ %d\n",
  1646. reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
  1647. "IO-APIC enabled"), reply_q->vector);
  1648. pr_info(MPT3SAS_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
  1649. ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
  1650. pr_info(MPT3SAS_FMT "ioport(0x%016llx), size(%d)\n",
  1651. ioc->name, (unsigned long long)pio_chip, pio_sz);
  1652. /* Save PCI configuration state for recovery from PCI AER/EEH errors */
  1653. pci_save_state(pdev);
  1654. return 0;
  1655. out_fail:
  1656. if (ioc->chip_phys)
  1657. iounmap(ioc->chip);
  1658. ioc->chip_phys = 0;
  1659. pci_release_selected_regions(ioc->pdev, ioc->bars);
  1660. pci_disable_pcie_error_reporting(pdev);
  1661. pci_disable_device(pdev);
  1662. return r;
  1663. }
  1664. /**
  1665. * mpt3sas_base_get_msg_frame - obtain request mf pointer
  1666. * @ioc: per adapter object
  1667. * @smid: system request message index(smid zero is invalid)
  1668. *
  1669. * Returns virt pointer to message frame.
  1670. */
  1671. void *
  1672. mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1673. {
  1674. return (void *)(ioc->request + (smid * ioc->request_sz));
  1675. }
  1676. /**
  1677. * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
  1678. * @ioc: per adapter object
  1679. * @smid: system request message index
  1680. *
  1681. * Returns virt pointer to sense buffer.
  1682. */
  1683. void *
  1684. mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1685. {
  1686. return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
  1687. }
  1688. /**
  1689. * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
  1690. * @ioc: per adapter object
  1691. * @smid: system request message index
  1692. *
  1693. * Returns phys pointer to the low 32bit address of the sense buffer.
  1694. */
  1695. __le32
  1696. mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1697. {
  1698. return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
  1699. SCSI_SENSE_BUFFERSIZE));
  1700. }
  1701. /**
  1702. * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
  1703. * @ioc: per adapter object
  1704. * @phys_addr: lower 32 physical addr of the reply
  1705. *
  1706. * Converts 32bit lower physical addr into a virt address.
  1707. */
  1708. void *
  1709. mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
  1710. {
  1711. if (!phys_addr)
  1712. return NULL;
  1713. return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
  1714. }
  1715. /**
  1716. * mpt3sas_base_get_smid - obtain a free smid from internal queue
  1717. * @ioc: per adapter object
  1718. * @cb_idx: callback index
  1719. *
  1720. * Returns smid (zero is invalid)
  1721. */
  1722. u16
  1723. mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
  1724. {
  1725. unsigned long flags;
  1726. struct request_tracker *request;
  1727. u16 smid;
  1728. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1729. if (list_empty(&ioc->internal_free_list)) {
  1730. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1731. pr_err(MPT3SAS_FMT "%s: smid not available\n",
  1732. ioc->name, __func__);
  1733. return 0;
  1734. }
  1735. request = list_entry(ioc->internal_free_list.next,
  1736. struct request_tracker, tracker_list);
  1737. request->cb_idx = cb_idx;
  1738. smid = request->smid;
  1739. list_del(&request->tracker_list);
  1740. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1741. return smid;
  1742. }
  1743. /**
  1744. * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
  1745. * @ioc: per adapter object
  1746. * @cb_idx: callback index
  1747. * @scmd: pointer to scsi command object
  1748. *
  1749. * Returns smid (zero is invalid)
  1750. */
  1751. u16
  1752. mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
  1753. struct scsi_cmnd *scmd)
  1754. {
  1755. unsigned long flags;
  1756. struct scsiio_tracker *request;
  1757. u16 smid;
  1758. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1759. if (list_empty(&ioc->free_list)) {
  1760. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1761. pr_err(MPT3SAS_FMT "%s: smid not available\n",
  1762. ioc->name, __func__);
  1763. return 0;
  1764. }
  1765. request = list_entry(ioc->free_list.next,
  1766. struct scsiio_tracker, tracker_list);
  1767. request->scmd = scmd;
  1768. request->cb_idx = cb_idx;
  1769. smid = request->smid;
  1770. list_del(&request->tracker_list);
  1771. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1772. return smid;
  1773. }
  1774. /**
  1775. * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
  1776. * @ioc: per adapter object
  1777. * @cb_idx: callback index
  1778. *
  1779. * Returns smid (zero is invalid)
  1780. */
  1781. u16
  1782. mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
  1783. {
  1784. unsigned long flags;
  1785. struct request_tracker *request;
  1786. u16 smid;
  1787. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1788. if (list_empty(&ioc->hpr_free_list)) {
  1789. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1790. return 0;
  1791. }
  1792. request = list_entry(ioc->hpr_free_list.next,
  1793. struct request_tracker, tracker_list);
  1794. request->cb_idx = cb_idx;
  1795. smid = request->smid;
  1796. list_del(&request->tracker_list);
  1797. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1798. return smid;
  1799. }
  1800. /**
  1801. * mpt3sas_base_free_smid - put smid back on free_list
  1802. * @ioc: per adapter object
  1803. * @smid: system request message index
  1804. *
  1805. * Return nothing.
  1806. */
  1807. void
  1808. mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1809. {
  1810. unsigned long flags;
  1811. int i;
  1812. struct chain_tracker *chain_req, *next;
  1813. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1814. if (smid < ioc->hi_priority_smid) {
  1815. /* scsiio queue */
  1816. i = smid - 1;
  1817. if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
  1818. list_for_each_entry_safe(chain_req, next,
  1819. &ioc->scsi_lookup[i].chain_list, tracker_list) {
  1820. list_del_init(&chain_req->tracker_list);
  1821. list_add(&chain_req->tracker_list,
  1822. &ioc->free_chain_list);
  1823. }
  1824. }
  1825. ioc->scsi_lookup[i].cb_idx = 0xFF;
  1826. ioc->scsi_lookup[i].scmd = NULL;
  1827. list_add(&ioc->scsi_lookup[i].tracker_list, &ioc->free_list);
  1828. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1829. /*
  1830. * See _wait_for_commands_to_complete() call with regards
  1831. * to this code.
  1832. */
  1833. if (ioc->shost_recovery && ioc->pending_io_count) {
  1834. if (ioc->pending_io_count == 1)
  1835. wake_up(&ioc->reset_wq);
  1836. ioc->pending_io_count--;
  1837. }
  1838. return;
  1839. } else if (smid < ioc->internal_smid) {
  1840. /* hi-priority */
  1841. i = smid - ioc->hi_priority_smid;
  1842. ioc->hpr_lookup[i].cb_idx = 0xFF;
  1843. list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
  1844. } else if (smid <= ioc->hba_queue_depth) {
  1845. /* internal queue */
  1846. i = smid - ioc->internal_smid;
  1847. ioc->internal_lookup[i].cb_idx = 0xFF;
  1848. list_add(&ioc->internal_lookup[i].tracker_list,
  1849. &ioc->internal_free_list);
  1850. }
  1851. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1852. }
  1853. /**
  1854. * _base_writeq - 64 bit write to MMIO
  1855. * @ioc: per adapter object
  1856. * @b: data payload
  1857. * @addr: address in MMIO space
  1858. * @writeq_lock: spin lock
  1859. *
  1860. * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
  1861. * care of 32 bit environment where its not quarenteed to send the entire word
  1862. * in one transfer.
  1863. */
  1864. #if defined(writeq) && defined(CONFIG_64BIT)
  1865. static inline void
  1866. _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
  1867. {
  1868. writeq(cpu_to_le64(b), addr);
  1869. }
  1870. #else
  1871. static inline void
  1872. _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
  1873. {
  1874. unsigned long flags;
  1875. __u64 data_out = cpu_to_le64(b);
  1876. spin_lock_irqsave(writeq_lock, flags);
  1877. writel((u32)(data_out), addr);
  1878. writel((u32)(data_out >> 32), (addr + 4));
  1879. spin_unlock_irqrestore(writeq_lock, flags);
  1880. }
  1881. #endif
  1882. static inline u8
  1883. _base_get_msix_index(struct MPT3SAS_ADAPTER *ioc)
  1884. {
  1885. return ioc->cpu_msix_table[raw_smp_processor_id()];
  1886. }
  1887. /**
  1888. * mpt3sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
  1889. * @ioc: per adapter object
  1890. * @smid: system request message index
  1891. * @handle: device handle
  1892. *
  1893. * Return nothing.
  1894. */
  1895. void
  1896. mpt3sas_base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
  1897. {
  1898. Mpi2RequestDescriptorUnion_t descriptor;
  1899. u64 *request = (u64 *)&descriptor;
  1900. descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
  1901. descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
  1902. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  1903. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  1904. descriptor.SCSIIO.LMID = 0;
  1905. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1906. &ioc->scsi_lookup_lock);
  1907. }
  1908. /**
  1909. * mpt3sas_base_put_smid_fast_path - send fast path request to firmware
  1910. * @ioc: per adapter object
  1911. * @smid: system request message index
  1912. * @handle: device handle
  1913. *
  1914. * Return nothing.
  1915. */
  1916. void
  1917. mpt3sas_base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  1918. u16 handle)
  1919. {
  1920. Mpi2RequestDescriptorUnion_t descriptor;
  1921. u64 *request = (u64 *)&descriptor;
  1922. descriptor.SCSIIO.RequestFlags =
  1923. MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
  1924. descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
  1925. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  1926. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  1927. descriptor.SCSIIO.LMID = 0;
  1928. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1929. &ioc->scsi_lookup_lock);
  1930. }
  1931. /**
  1932. * mpt3sas_base_put_smid_hi_priority - send Task Managment request to firmware
  1933. * @ioc: per adapter object
  1934. * @smid: system request message index
  1935. *
  1936. * Return nothing.
  1937. */
  1938. void
  1939. mpt3sas_base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1940. {
  1941. Mpi2RequestDescriptorUnion_t descriptor;
  1942. u64 *request = (u64 *)&descriptor;
  1943. descriptor.HighPriority.RequestFlags =
  1944. MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
  1945. descriptor.HighPriority.MSIxIndex = 0;
  1946. descriptor.HighPriority.SMID = cpu_to_le16(smid);
  1947. descriptor.HighPriority.LMID = 0;
  1948. descriptor.HighPriority.Reserved1 = 0;
  1949. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1950. &ioc->scsi_lookup_lock);
  1951. }
  1952. /**
  1953. * mpt3sas_base_put_smid_default - Default, primarily used for config pages
  1954. * @ioc: per adapter object
  1955. * @smid: system request message index
  1956. *
  1957. * Return nothing.
  1958. */
  1959. void
  1960. mpt3sas_base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1961. {
  1962. Mpi2RequestDescriptorUnion_t descriptor;
  1963. u64 *request = (u64 *)&descriptor;
  1964. descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
  1965. descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
  1966. descriptor.Default.SMID = cpu_to_le16(smid);
  1967. descriptor.Default.LMID = 0;
  1968. descriptor.Default.DescriptorTypeDependent = 0;
  1969. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1970. &ioc->scsi_lookup_lock);
  1971. }
  1972. /**
  1973. * _base_display_ioc_capabilities - Disply IOC's capabilities.
  1974. * @ioc: per adapter object
  1975. *
  1976. * Return nothing.
  1977. */
  1978. static void
  1979. _base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
  1980. {
  1981. int i = 0;
  1982. char desc[16];
  1983. u32 iounit_pg1_flags;
  1984. u32 bios_version;
  1985. bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  1986. strncpy(desc, ioc->manu_pg0.ChipName, 16);
  1987. pr_info(MPT3SAS_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "\
  1988. "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
  1989. ioc->name, desc,
  1990. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  1991. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  1992. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  1993. ioc->facts.FWVersion.Word & 0x000000FF,
  1994. ioc->pdev->revision,
  1995. (bios_version & 0xFF000000) >> 24,
  1996. (bios_version & 0x00FF0000) >> 16,
  1997. (bios_version & 0x0000FF00) >> 8,
  1998. bios_version & 0x000000FF);
  1999. pr_info(MPT3SAS_FMT "Protocol=(", ioc->name);
  2000. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
  2001. pr_info("Initiator");
  2002. i++;
  2003. }
  2004. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
  2005. pr_info("%sTarget", i ? "," : "");
  2006. i++;
  2007. }
  2008. i = 0;
  2009. pr_info("), ");
  2010. pr_info("Capabilities=(");
  2011. if (ioc->facts.IOCCapabilities &
  2012. MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
  2013. pr_info("Raid");
  2014. i++;
  2015. }
  2016. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
  2017. pr_info("%sTLR", i ? "," : "");
  2018. i++;
  2019. }
  2020. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
  2021. pr_info("%sMulticast", i ? "," : "");
  2022. i++;
  2023. }
  2024. if (ioc->facts.IOCCapabilities &
  2025. MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
  2026. pr_info("%sBIDI Target", i ? "," : "");
  2027. i++;
  2028. }
  2029. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
  2030. pr_info("%sEEDP", i ? "," : "");
  2031. i++;
  2032. }
  2033. if (ioc->facts.IOCCapabilities &
  2034. MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
  2035. pr_info("%sSnapshot Buffer", i ? "," : "");
  2036. i++;
  2037. }
  2038. if (ioc->facts.IOCCapabilities &
  2039. MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
  2040. pr_info("%sDiag Trace Buffer", i ? "," : "");
  2041. i++;
  2042. }
  2043. if (ioc->facts.IOCCapabilities &
  2044. MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
  2045. pr_info("%sDiag Extended Buffer", i ? "," : "");
  2046. i++;
  2047. }
  2048. if (ioc->facts.IOCCapabilities &
  2049. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
  2050. pr_info("%sTask Set Full", i ? "," : "");
  2051. i++;
  2052. }
  2053. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  2054. if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
  2055. pr_info("%sNCQ", i ? "," : "");
  2056. i++;
  2057. }
  2058. pr_info(")\n");
  2059. }
  2060. /**
  2061. * mpt3sas_base_update_missing_delay - change the missing delay timers
  2062. * @ioc: per adapter object
  2063. * @device_missing_delay: amount of time till device is reported missing
  2064. * @io_missing_delay: interval IO is returned when there is a missing device
  2065. *
  2066. * Return nothing.
  2067. *
  2068. * Passed on the command line, this function will modify the device missing
  2069. * delay, as well as the io missing delay. This should be called at driver
  2070. * load time.
  2071. */
  2072. void
  2073. mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
  2074. u16 device_missing_delay, u8 io_missing_delay)
  2075. {
  2076. u16 dmd, dmd_new, dmd_orignal;
  2077. u8 io_missing_delay_original;
  2078. u16 sz;
  2079. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  2080. Mpi2ConfigReply_t mpi_reply;
  2081. u8 num_phys = 0;
  2082. u16 ioc_status;
  2083. mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
  2084. if (!num_phys)
  2085. return;
  2086. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
  2087. sizeof(Mpi2SasIOUnit1PhyData_t));
  2088. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  2089. if (!sas_iounit_pg1) {
  2090. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  2091. ioc->name, __FILE__, __LINE__, __func__);
  2092. goto out;
  2093. }
  2094. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  2095. sas_iounit_pg1, sz))) {
  2096. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  2097. ioc->name, __FILE__, __LINE__, __func__);
  2098. goto out;
  2099. }
  2100. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2101. MPI2_IOCSTATUS_MASK;
  2102. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2103. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  2104. ioc->name, __FILE__, __LINE__, __func__);
  2105. goto out;
  2106. }
  2107. /* device missing delay */
  2108. dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
  2109. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  2110. dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  2111. else
  2112. dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2113. dmd_orignal = dmd;
  2114. if (device_missing_delay > 0x7F) {
  2115. dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
  2116. device_missing_delay;
  2117. dmd = dmd / 16;
  2118. dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
  2119. } else
  2120. dmd = device_missing_delay;
  2121. sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
  2122. /* io missing delay */
  2123. io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
  2124. sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
  2125. if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  2126. sz)) {
  2127. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  2128. dmd_new = (dmd &
  2129. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  2130. else
  2131. dmd_new =
  2132. dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2133. pr_info(MPT3SAS_FMT "device_missing_delay: old(%d), new(%d)\n",
  2134. ioc->name, dmd_orignal, dmd_new);
  2135. pr_info(MPT3SAS_FMT "ioc_missing_delay: old(%d), new(%d)\n",
  2136. ioc->name, io_missing_delay_original,
  2137. io_missing_delay);
  2138. ioc->device_missing_delay = dmd_new;
  2139. ioc->io_missing_delay = io_missing_delay;
  2140. }
  2141. out:
  2142. kfree(sas_iounit_pg1);
  2143. }
  2144. /**
  2145. * _base_static_config_pages - static start of day config pages
  2146. * @ioc: per adapter object
  2147. *
  2148. * Return nothing.
  2149. */
  2150. static void
  2151. _base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
  2152. {
  2153. Mpi2ConfigReply_t mpi_reply;
  2154. u32 iounit_pg1_flags;
  2155. mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
  2156. if (ioc->ir_firmware)
  2157. mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
  2158. &ioc->manu_pg10);
  2159. /*
  2160. * Ensure correct T10 PI operation if vendor left EEDPTagMode
  2161. * flag unset in NVDATA.
  2162. */
  2163. mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply, &ioc->manu_pg11);
  2164. if (ioc->manu_pg11.EEDPTagMode == 0) {
  2165. pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
  2166. ioc->name);
  2167. ioc->manu_pg11.EEDPTagMode &= ~0x3;
  2168. ioc->manu_pg11.EEDPTagMode |= 0x1;
  2169. mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
  2170. &ioc->manu_pg11);
  2171. }
  2172. mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
  2173. mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
  2174. mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
  2175. mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
  2176. mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  2177. _base_display_ioc_capabilities(ioc);
  2178. /*
  2179. * Enable task_set_full handling in iounit_pg1 when the
  2180. * facts capabilities indicate that its supported.
  2181. */
  2182. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  2183. if ((ioc->facts.IOCCapabilities &
  2184. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
  2185. iounit_pg1_flags &=
  2186. ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  2187. else
  2188. iounit_pg1_flags |=
  2189. MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  2190. ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
  2191. mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  2192. }
  2193. /**
  2194. * _base_release_memory_pools - release memory
  2195. * @ioc: per adapter object
  2196. *
  2197. * Free memory allocated from _base_allocate_memory_pools.
  2198. *
  2199. * Return nothing.
  2200. */
  2201. static void
  2202. _base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
  2203. {
  2204. int i;
  2205. dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2206. __func__));
  2207. if (ioc->request) {
  2208. pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
  2209. ioc->request, ioc->request_dma);
  2210. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2211. "request_pool(0x%p): free\n",
  2212. ioc->name, ioc->request));
  2213. ioc->request = NULL;
  2214. }
  2215. if (ioc->sense) {
  2216. pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
  2217. if (ioc->sense_dma_pool)
  2218. pci_pool_destroy(ioc->sense_dma_pool);
  2219. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2220. "sense_pool(0x%p): free\n",
  2221. ioc->name, ioc->sense));
  2222. ioc->sense = NULL;
  2223. }
  2224. if (ioc->reply) {
  2225. pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
  2226. if (ioc->reply_dma_pool)
  2227. pci_pool_destroy(ioc->reply_dma_pool);
  2228. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2229. "reply_pool(0x%p): free\n",
  2230. ioc->name, ioc->reply));
  2231. ioc->reply = NULL;
  2232. }
  2233. if (ioc->reply_free) {
  2234. pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
  2235. ioc->reply_free_dma);
  2236. if (ioc->reply_free_dma_pool)
  2237. pci_pool_destroy(ioc->reply_free_dma_pool);
  2238. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2239. "reply_free_pool(0x%p): free\n",
  2240. ioc->name, ioc->reply_free));
  2241. ioc->reply_free = NULL;
  2242. }
  2243. if (ioc->reply_post_free) {
  2244. pci_pool_free(ioc->reply_post_free_dma_pool,
  2245. ioc->reply_post_free, ioc->reply_post_free_dma);
  2246. if (ioc->reply_post_free_dma_pool)
  2247. pci_pool_destroy(ioc->reply_post_free_dma_pool);
  2248. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2249. "reply_post_free_pool(0x%p): free\n", ioc->name,
  2250. ioc->reply_post_free));
  2251. ioc->reply_post_free = NULL;
  2252. }
  2253. if (ioc->config_page) {
  2254. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2255. "config_page(0x%p): free\n", ioc->name,
  2256. ioc->config_page));
  2257. pci_free_consistent(ioc->pdev, ioc->config_page_sz,
  2258. ioc->config_page, ioc->config_page_dma);
  2259. }
  2260. if (ioc->scsi_lookup) {
  2261. free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
  2262. ioc->scsi_lookup = NULL;
  2263. }
  2264. kfree(ioc->hpr_lookup);
  2265. kfree(ioc->internal_lookup);
  2266. if (ioc->chain_lookup) {
  2267. for (i = 0; i < ioc->chain_depth; i++) {
  2268. if (ioc->chain_lookup[i].chain_buffer)
  2269. pci_pool_free(ioc->chain_dma_pool,
  2270. ioc->chain_lookup[i].chain_buffer,
  2271. ioc->chain_lookup[i].chain_buffer_dma);
  2272. }
  2273. if (ioc->chain_dma_pool)
  2274. pci_pool_destroy(ioc->chain_dma_pool);
  2275. free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
  2276. ioc->chain_lookup = NULL;
  2277. }
  2278. }
  2279. /**
  2280. * _base_allocate_memory_pools - allocate start of day memory pools
  2281. * @ioc: per adapter object
  2282. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2283. *
  2284. * Returns 0 success, anything else error
  2285. */
  2286. static int
  2287. _base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  2288. {
  2289. struct mpt3sas_facts *facts;
  2290. u16 max_sge_elements;
  2291. u16 chains_needed_per_io;
  2292. u32 sz, total_sz, reply_post_free_sz;
  2293. u32 retry_sz;
  2294. u16 max_request_credit;
  2295. unsigned short sg_tablesize;
  2296. u16 sge_size;
  2297. int i;
  2298. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2299. __func__));
  2300. retry_sz = 0;
  2301. facts = &ioc->facts;
  2302. /* command line tunables for max sgl entries */
  2303. if (max_sgl_entries != -1)
  2304. sg_tablesize = max_sgl_entries;
  2305. else
  2306. sg_tablesize = MPT3SAS_SG_DEPTH;
  2307. if (sg_tablesize < MPT3SAS_MIN_PHYS_SEGMENTS)
  2308. sg_tablesize = MPT3SAS_MIN_PHYS_SEGMENTS;
  2309. else if (sg_tablesize > MPT3SAS_MAX_PHYS_SEGMENTS)
  2310. sg_tablesize = MPT3SAS_MAX_PHYS_SEGMENTS;
  2311. ioc->shost->sg_tablesize = sg_tablesize;
  2312. ioc->hi_priority_depth = facts->HighPriorityCredit;
  2313. ioc->internal_depth = ioc->hi_priority_depth + (5);
  2314. /* command line tunables for max controller queue depth */
  2315. if (max_queue_depth != -1 && max_queue_depth != 0) {
  2316. max_request_credit = min_t(u16, max_queue_depth +
  2317. ioc->hi_priority_depth + ioc->internal_depth,
  2318. facts->RequestCredit);
  2319. if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
  2320. max_request_credit = MAX_HBA_QUEUE_DEPTH;
  2321. } else
  2322. max_request_credit = min_t(u16, facts->RequestCredit,
  2323. MAX_HBA_QUEUE_DEPTH);
  2324. ioc->hba_queue_depth = max_request_credit;
  2325. /* request frame size */
  2326. ioc->request_sz = facts->IOCRequestFrameSize * 4;
  2327. /* reply frame size */
  2328. ioc->reply_sz = facts->ReplyFrameSize * 4;
  2329. /* calculate the max scatter element size */
  2330. sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
  2331. retry_allocation:
  2332. total_sz = 0;
  2333. /* calculate number of sg elements left over in the 1st frame */
  2334. max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
  2335. sizeof(Mpi2SGEIOUnion_t)) + sge_size);
  2336. ioc->max_sges_in_main_message = max_sge_elements/sge_size;
  2337. /* now do the same for a chain buffer */
  2338. max_sge_elements = ioc->request_sz - sge_size;
  2339. ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
  2340. /*
  2341. * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
  2342. */
  2343. chains_needed_per_io = ((ioc->shost->sg_tablesize -
  2344. ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
  2345. + 1;
  2346. if (chains_needed_per_io > facts->MaxChainDepth) {
  2347. chains_needed_per_io = facts->MaxChainDepth;
  2348. ioc->shost->sg_tablesize = min_t(u16,
  2349. ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
  2350. * chains_needed_per_io), ioc->shost->sg_tablesize);
  2351. }
  2352. ioc->chains_needed_per_io = chains_needed_per_io;
  2353. /* reply free queue sizing - taking into account for 64 FW events */
  2354. ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
  2355. /* calculate reply descriptor post queue depth */
  2356. ioc->reply_post_queue_depth = ioc->hba_queue_depth +
  2357. ioc->reply_free_queue_depth + 1 ;
  2358. /* align the reply post queue on the next 16 count boundary */
  2359. if (ioc->reply_post_queue_depth % 16)
  2360. ioc->reply_post_queue_depth += 16 -
  2361. (ioc->reply_post_queue_depth % 16);
  2362. if (ioc->reply_post_queue_depth >
  2363. facts->MaxReplyDescriptorPostQueueDepth) {
  2364. ioc->reply_post_queue_depth =
  2365. facts->MaxReplyDescriptorPostQueueDepth -
  2366. (facts->MaxReplyDescriptorPostQueueDepth % 16);
  2367. ioc->hba_queue_depth =
  2368. ((ioc->reply_post_queue_depth - 64) / 2) - 1;
  2369. ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
  2370. }
  2371. dinitprintk(ioc, pr_info(MPT3SAS_FMT "scatter gather: " \
  2372. "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
  2373. "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
  2374. ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
  2375. ioc->chains_needed_per_io));
  2376. ioc->scsiio_depth = ioc->hba_queue_depth -
  2377. ioc->hi_priority_depth - ioc->internal_depth;
  2378. /* set the scsi host can_queue depth
  2379. * with some internal commands that could be outstanding
  2380. */
  2381. ioc->shost->can_queue = ioc->scsiio_depth;
  2382. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2383. "scsi host: can_queue depth (%d)\n",
  2384. ioc->name, ioc->shost->can_queue));
  2385. /* contiguous pool for request and chains, 16 byte align, one extra "
  2386. * "frame for smid=0
  2387. */
  2388. ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
  2389. sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
  2390. /* hi-priority queue */
  2391. sz += (ioc->hi_priority_depth * ioc->request_sz);
  2392. /* internal queue */
  2393. sz += (ioc->internal_depth * ioc->request_sz);
  2394. ioc->request_dma_sz = sz;
  2395. ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
  2396. if (!ioc->request) {
  2397. pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
  2398. "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
  2399. "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
  2400. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  2401. if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
  2402. goto out;
  2403. retry_sz += 64;
  2404. ioc->hba_queue_depth = max_request_credit - retry_sz;
  2405. goto retry_allocation;
  2406. }
  2407. if (retry_sz)
  2408. pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
  2409. "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
  2410. "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
  2411. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  2412. /* hi-priority queue */
  2413. ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
  2414. ioc->request_sz);
  2415. ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
  2416. ioc->request_sz);
  2417. /* internal queue */
  2418. ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
  2419. ioc->request_sz);
  2420. ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
  2421. ioc->request_sz);
  2422. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2423. "request pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
  2424. ioc->name, ioc->request, ioc->hba_queue_depth, ioc->request_sz,
  2425. (ioc->hba_queue_depth * ioc->request_sz)/1024));
  2426. dinitprintk(ioc, pr_info(MPT3SAS_FMT "request pool: dma(0x%llx)\n",
  2427. ioc->name, (unsigned long long) ioc->request_dma));
  2428. total_sz += sz;
  2429. sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
  2430. ioc->scsi_lookup_pages = get_order(sz);
  2431. ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
  2432. GFP_KERNEL, ioc->scsi_lookup_pages);
  2433. if (!ioc->scsi_lookup) {
  2434. pr_err(MPT3SAS_FMT "scsi_lookup: get_free_pages failed, sz(%d)\n",
  2435. ioc->name, (int)sz);
  2436. goto out;
  2437. }
  2438. dinitprintk(ioc, pr_info(MPT3SAS_FMT "scsiio(0x%p): depth(%d)\n",
  2439. ioc->name, ioc->request, ioc->scsiio_depth));
  2440. ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
  2441. sz = ioc->chain_depth * sizeof(struct chain_tracker);
  2442. ioc->chain_pages = get_order(sz);
  2443. ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
  2444. GFP_KERNEL, ioc->chain_pages);
  2445. if (!ioc->chain_lookup) {
  2446. pr_err(MPT3SAS_FMT "chain_lookup: __get_free_pages failed\n",
  2447. ioc->name);
  2448. goto out;
  2449. }
  2450. ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
  2451. ioc->request_sz, 16, 0);
  2452. if (!ioc->chain_dma_pool) {
  2453. pr_err(MPT3SAS_FMT "chain_dma_pool: pci_pool_create failed\n",
  2454. ioc->name);
  2455. goto out;
  2456. }
  2457. for (i = 0; i < ioc->chain_depth; i++) {
  2458. ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
  2459. ioc->chain_dma_pool , GFP_KERNEL,
  2460. &ioc->chain_lookup[i].chain_buffer_dma);
  2461. if (!ioc->chain_lookup[i].chain_buffer) {
  2462. ioc->chain_depth = i;
  2463. goto chain_done;
  2464. }
  2465. total_sz += ioc->request_sz;
  2466. }
  2467. chain_done:
  2468. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2469. "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
  2470. ioc->name, ioc->chain_depth, ioc->request_sz,
  2471. ((ioc->chain_depth * ioc->request_sz))/1024));
  2472. /* initialize hi-priority queue smid's */
  2473. ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
  2474. sizeof(struct request_tracker), GFP_KERNEL);
  2475. if (!ioc->hpr_lookup) {
  2476. pr_err(MPT3SAS_FMT "hpr_lookup: kcalloc failed\n",
  2477. ioc->name);
  2478. goto out;
  2479. }
  2480. ioc->hi_priority_smid = ioc->scsiio_depth + 1;
  2481. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2482. "hi_priority(0x%p): depth(%d), start smid(%d)\n",
  2483. ioc->name, ioc->hi_priority,
  2484. ioc->hi_priority_depth, ioc->hi_priority_smid));
  2485. /* initialize internal queue smid's */
  2486. ioc->internal_lookup = kcalloc(ioc->internal_depth,
  2487. sizeof(struct request_tracker), GFP_KERNEL);
  2488. if (!ioc->internal_lookup) {
  2489. pr_err(MPT3SAS_FMT "internal_lookup: kcalloc failed\n",
  2490. ioc->name);
  2491. goto out;
  2492. }
  2493. ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
  2494. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2495. "internal(0x%p): depth(%d), start smid(%d)\n",
  2496. ioc->name, ioc->internal,
  2497. ioc->internal_depth, ioc->internal_smid));
  2498. /* sense buffers, 4 byte align */
  2499. sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
  2500. ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
  2501. 0);
  2502. if (!ioc->sense_dma_pool) {
  2503. pr_err(MPT3SAS_FMT "sense pool: pci_pool_create failed\n",
  2504. ioc->name);
  2505. goto out;
  2506. }
  2507. ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
  2508. &ioc->sense_dma);
  2509. if (!ioc->sense) {
  2510. pr_err(MPT3SAS_FMT "sense pool: pci_pool_alloc failed\n",
  2511. ioc->name);
  2512. goto out;
  2513. }
  2514. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2515. "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
  2516. "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
  2517. SCSI_SENSE_BUFFERSIZE, sz/1024));
  2518. dinitprintk(ioc, pr_info(MPT3SAS_FMT "sense_dma(0x%llx)\n",
  2519. ioc->name, (unsigned long long)ioc->sense_dma));
  2520. total_sz += sz;
  2521. /* reply pool, 4 byte align */
  2522. sz = ioc->reply_free_queue_depth * ioc->reply_sz;
  2523. ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
  2524. 0);
  2525. if (!ioc->reply_dma_pool) {
  2526. pr_err(MPT3SAS_FMT "reply pool: pci_pool_create failed\n",
  2527. ioc->name);
  2528. goto out;
  2529. }
  2530. ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
  2531. &ioc->reply_dma);
  2532. if (!ioc->reply) {
  2533. pr_err(MPT3SAS_FMT "reply pool: pci_pool_alloc failed\n",
  2534. ioc->name);
  2535. goto out;
  2536. }
  2537. ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
  2538. ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
  2539. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2540. "reply pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
  2541. ioc->name, ioc->reply,
  2542. ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
  2543. dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_dma(0x%llx)\n",
  2544. ioc->name, (unsigned long long)ioc->reply_dma));
  2545. total_sz += sz;
  2546. /* reply free queue, 16 byte align */
  2547. sz = ioc->reply_free_queue_depth * 4;
  2548. ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
  2549. ioc->pdev, sz, 16, 0);
  2550. if (!ioc->reply_free_dma_pool) {
  2551. pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_create failed\n",
  2552. ioc->name);
  2553. goto out;
  2554. }
  2555. ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
  2556. &ioc->reply_free_dma);
  2557. if (!ioc->reply_free) {
  2558. pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_alloc failed\n",
  2559. ioc->name);
  2560. goto out;
  2561. }
  2562. memset(ioc->reply_free, 0, sz);
  2563. dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_free pool(0x%p): " \
  2564. "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
  2565. ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
  2566. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2567. "reply_free_dma (0x%llx)\n",
  2568. ioc->name, (unsigned long long)ioc->reply_free_dma));
  2569. total_sz += sz;
  2570. /* reply post queue, 16 byte align */
  2571. reply_post_free_sz = ioc->reply_post_queue_depth *
  2572. sizeof(Mpi2DefaultReplyDescriptor_t);
  2573. if (_base_is_controller_msix_enabled(ioc))
  2574. sz = reply_post_free_sz * ioc->reply_queue_count;
  2575. else
  2576. sz = reply_post_free_sz;
  2577. ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
  2578. ioc->pdev, sz, 16, 0);
  2579. if (!ioc->reply_post_free_dma_pool) {
  2580. pr_err(MPT3SAS_FMT
  2581. "reply_post_free pool: pci_pool_create failed\n",
  2582. ioc->name);
  2583. goto out;
  2584. }
  2585. ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
  2586. GFP_KERNEL, &ioc->reply_post_free_dma);
  2587. if (!ioc->reply_post_free) {
  2588. pr_err(MPT3SAS_FMT
  2589. "reply_post_free pool: pci_pool_alloc failed\n",
  2590. ioc->name);
  2591. goto out;
  2592. }
  2593. memset(ioc->reply_post_free, 0, sz);
  2594. dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply post free pool" \
  2595. "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
  2596. ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
  2597. sz/1024));
  2598. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2599. "reply_post_free_dma = (0x%llx)\n",
  2600. ioc->name, (unsigned long long)
  2601. ioc->reply_post_free_dma));
  2602. total_sz += sz;
  2603. ioc->config_page_sz = 512;
  2604. ioc->config_page = pci_alloc_consistent(ioc->pdev,
  2605. ioc->config_page_sz, &ioc->config_page_dma);
  2606. if (!ioc->config_page) {
  2607. pr_err(MPT3SAS_FMT
  2608. "config page: pci_pool_alloc failed\n",
  2609. ioc->name);
  2610. goto out;
  2611. }
  2612. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2613. "config page(0x%p): size(%d)\n",
  2614. ioc->name, ioc->config_page, ioc->config_page_sz));
  2615. dinitprintk(ioc, pr_info(MPT3SAS_FMT "config_page_dma(0x%llx)\n",
  2616. ioc->name, (unsigned long long)ioc->config_page_dma));
  2617. total_sz += ioc->config_page_sz;
  2618. pr_info(MPT3SAS_FMT "Allocated physical memory: size(%d kB)\n",
  2619. ioc->name, total_sz/1024);
  2620. pr_info(MPT3SAS_FMT
  2621. "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
  2622. ioc->name, ioc->shost->can_queue, facts->RequestCredit);
  2623. pr_info(MPT3SAS_FMT "Scatter Gather Elements per IO(%d)\n",
  2624. ioc->name, ioc->shost->sg_tablesize);
  2625. return 0;
  2626. out:
  2627. return -ENOMEM;
  2628. }
  2629. /**
  2630. * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
  2631. * @ioc: Pointer to MPT_ADAPTER structure
  2632. * @cooked: Request raw or cooked IOC state
  2633. *
  2634. * Returns all IOC Doorbell register bits if cooked==0, else just the
  2635. * Doorbell bits in MPI_IOC_STATE_MASK.
  2636. */
  2637. u32
  2638. mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
  2639. {
  2640. u32 s, sc;
  2641. s = readl(&ioc->chip->Doorbell);
  2642. sc = s & MPI2_IOC_STATE_MASK;
  2643. return cooked ? sc : s;
  2644. }
  2645. /**
  2646. * _base_wait_on_iocstate - waiting on a particular ioc state
  2647. * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
  2648. * @timeout: timeout in second
  2649. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2650. *
  2651. * Returns 0 for success, non-zero for failure.
  2652. */
  2653. static int
  2654. _base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
  2655. int sleep_flag)
  2656. {
  2657. u32 count, cntdn;
  2658. u32 current_state;
  2659. count = 0;
  2660. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2661. do {
  2662. current_state = mpt3sas_base_get_iocstate(ioc, 1);
  2663. if (current_state == ioc_state)
  2664. return 0;
  2665. if (count && current_state == MPI2_IOC_STATE_FAULT)
  2666. break;
  2667. if (sleep_flag == CAN_SLEEP)
  2668. usleep_range(1000, 1500);
  2669. else
  2670. udelay(500);
  2671. count++;
  2672. } while (--cntdn);
  2673. return current_state;
  2674. }
  2675. /**
  2676. * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
  2677. * a write to the doorbell)
  2678. * @ioc: per adapter object
  2679. * @timeout: timeout in second
  2680. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2681. *
  2682. * Returns 0 for success, non-zero for failure.
  2683. *
  2684. * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
  2685. */
  2686. static int
  2687. _base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout,
  2688. int sleep_flag)
  2689. {
  2690. u32 cntdn, count;
  2691. u32 int_status;
  2692. count = 0;
  2693. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2694. do {
  2695. int_status = readl(&ioc->chip->HostInterruptStatus);
  2696. if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  2697. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  2698. "%s: successful count(%d), timeout(%d)\n",
  2699. ioc->name, __func__, count, timeout));
  2700. return 0;
  2701. }
  2702. if (sleep_flag == CAN_SLEEP)
  2703. usleep_range(1000, 1500);
  2704. else
  2705. udelay(500);
  2706. count++;
  2707. } while (--cntdn);
  2708. pr_err(MPT3SAS_FMT
  2709. "%s: failed due to timeout count(%d), int_status(%x)!\n",
  2710. ioc->name, __func__, count, int_status);
  2711. return -EFAULT;
  2712. }
  2713. /**
  2714. * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
  2715. * @ioc: per adapter object
  2716. * @timeout: timeout in second
  2717. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2718. *
  2719. * Returns 0 for success, non-zero for failure.
  2720. *
  2721. * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
  2722. * doorbell.
  2723. */
  2724. static int
  2725. _base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout,
  2726. int sleep_flag)
  2727. {
  2728. u32 cntdn, count;
  2729. u32 int_status;
  2730. u32 doorbell;
  2731. count = 0;
  2732. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2733. do {
  2734. int_status = readl(&ioc->chip->HostInterruptStatus);
  2735. if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
  2736. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  2737. "%s: successful count(%d), timeout(%d)\n",
  2738. ioc->name, __func__, count, timeout));
  2739. return 0;
  2740. } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  2741. doorbell = readl(&ioc->chip->Doorbell);
  2742. if ((doorbell & MPI2_IOC_STATE_MASK) ==
  2743. MPI2_IOC_STATE_FAULT) {
  2744. mpt3sas_base_fault_info(ioc , doorbell);
  2745. return -EFAULT;
  2746. }
  2747. } else if (int_status == 0xFFFFFFFF)
  2748. goto out;
  2749. if (sleep_flag == CAN_SLEEP)
  2750. usleep_range(1000, 1500);
  2751. else
  2752. udelay(500);
  2753. count++;
  2754. } while (--cntdn);
  2755. out:
  2756. pr_err(MPT3SAS_FMT
  2757. "%s: failed due to timeout count(%d), int_status(%x)!\n",
  2758. ioc->name, __func__, count, int_status);
  2759. return -EFAULT;
  2760. }
  2761. /**
  2762. * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
  2763. * @ioc: per adapter object
  2764. * @timeout: timeout in second
  2765. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2766. *
  2767. * Returns 0 for success, non-zero for failure.
  2768. *
  2769. */
  2770. static int
  2771. _base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout,
  2772. int sleep_flag)
  2773. {
  2774. u32 cntdn, count;
  2775. u32 doorbell_reg;
  2776. count = 0;
  2777. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2778. do {
  2779. doorbell_reg = readl(&ioc->chip->Doorbell);
  2780. if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
  2781. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  2782. "%s: successful count(%d), timeout(%d)\n",
  2783. ioc->name, __func__, count, timeout));
  2784. return 0;
  2785. }
  2786. if (sleep_flag == CAN_SLEEP)
  2787. usleep_range(1000, 1500);
  2788. else
  2789. udelay(500);
  2790. count++;
  2791. } while (--cntdn);
  2792. pr_err(MPT3SAS_FMT
  2793. "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
  2794. ioc->name, __func__, count, doorbell_reg);
  2795. return -EFAULT;
  2796. }
  2797. /**
  2798. * _base_send_ioc_reset - send doorbell reset
  2799. * @ioc: per adapter object
  2800. * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
  2801. * @timeout: timeout in second
  2802. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2803. *
  2804. * Returns 0 for success, non-zero for failure.
  2805. */
  2806. static int
  2807. _base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout,
  2808. int sleep_flag)
  2809. {
  2810. u32 ioc_state;
  2811. int r = 0;
  2812. if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
  2813. pr_err(MPT3SAS_FMT "%s: unknown reset_type\n",
  2814. ioc->name, __func__);
  2815. return -EFAULT;
  2816. }
  2817. if (!(ioc->facts.IOCCapabilities &
  2818. MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
  2819. return -EFAULT;
  2820. pr_info(MPT3SAS_FMT "sending message unit reset !!\n", ioc->name);
  2821. writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
  2822. &ioc->chip->Doorbell);
  2823. if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
  2824. r = -EFAULT;
  2825. goto out;
  2826. }
  2827. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
  2828. timeout, sleep_flag);
  2829. if (ioc_state) {
  2830. pr_err(MPT3SAS_FMT
  2831. "%s: failed going to ready state (ioc_state=0x%x)\n",
  2832. ioc->name, __func__, ioc_state);
  2833. r = -EFAULT;
  2834. goto out;
  2835. }
  2836. out:
  2837. pr_info(MPT3SAS_FMT "message unit reset: %s\n",
  2838. ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
  2839. return r;
  2840. }
  2841. /**
  2842. * _base_handshake_req_reply_wait - send request thru doorbell interface
  2843. * @ioc: per adapter object
  2844. * @request_bytes: request length
  2845. * @request: pointer having request payload
  2846. * @reply_bytes: reply length
  2847. * @reply: pointer to reply payload
  2848. * @timeout: timeout in second
  2849. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2850. *
  2851. * Returns 0 for success, non-zero for failure.
  2852. */
  2853. static int
  2854. _base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
  2855. u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
  2856. {
  2857. MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
  2858. int i;
  2859. u8 failed;
  2860. u16 dummy;
  2861. __le32 *mfp;
  2862. /* make sure doorbell is not in use */
  2863. if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
  2864. pr_err(MPT3SAS_FMT
  2865. "doorbell is in use (line=%d)\n",
  2866. ioc->name, __LINE__);
  2867. return -EFAULT;
  2868. }
  2869. /* clear pending doorbell interrupts from previous state changes */
  2870. if (readl(&ioc->chip->HostInterruptStatus) &
  2871. MPI2_HIS_IOC2SYS_DB_STATUS)
  2872. writel(0, &ioc->chip->HostInterruptStatus);
  2873. /* send message to ioc */
  2874. writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
  2875. ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
  2876. &ioc->chip->Doorbell);
  2877. if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
  2878. pr_err(MPT3SAS_FMT
  2879. "doorbell handshake int failed (line=%d)\n",
  2880. ioc->name, __LINE__);
  2881. return -EFAULT;
  2882. }
  2883. writel(0, &ioc->chip->HostInterruptStatus);
  2884. if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
  2885. pr_err(MPT3SAS_FMT
  2886. "doorbell handshake ack failed (line=%d)\n",
  2887. ioc->name, __LINE__);
  2888. return -EFAULT;
  2889. }
  2890. /* send message 32-bits at a time */
  2891. for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
  2892. writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
  2893. if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
  2894. failed = 1;
  2895. }
  2896. if (failed) {
  2897. pr_err(MPT3SAS_FMT
  2898. "doorbell handshake sending request failed (line=%d)\n",
  2899. ioc->name, __LINE__);
  2900. return -EFAULT;
  2901. }
  2902. /* now wait for the reply */
  2903. if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
  2904. pr_err(MPT3SAS_FMT
  2905. "doorbell handshake int failed (line=%d)\n",
  2906. ioc->name, __LINE__);
  2907. return -EFAULT;
  2908. }
  2909. /* read the first two 16-bits, it gives the total length of the reply */
  2910. reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2911. & MPI2_DOORBELL_DATA_MASK);
  2912. writel(0, &ioc->chip->HostInterruptStatus);
  2913. if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
  2914. pr_err(MPT3SAS_FMT
  2915. "doorbell handshake int failed (line=%d)\n",
  2916. ioc->name, __LINE__);
  2917. return -EFAULT;
  2918. }
  2919. reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2920. & MPI2_DOORBELL_DATA_MASK);
  2921. writel(0, &ioc->chip->HostInterruptStatus);
  2922. for (i = 2; i < default_reply->MsgLength * 2; i++) {
  2923. if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
  2924. pr_err(MPT3SAS_FMT
  2925. "doorbell handshake int failed (line=%d)\n",
  2926. ioc->name, __LINE__);
  2927. return -EFAULT;
  2928. }
  2929. if (i >= reply_bytes/2) /* overflow case */
  2930. dummy = readl(&ioc->chip->Doorbell);
  2931. else
  2932. reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2933. & MPI2_DOORBELL_DATA_MASK);
  2934. writel(0, &ioc->chip->HostInterruptStatus);
  2935. }
  2936. _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
  2937. if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
  2938. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  2939. "doorbell is in use (line=%d)\n", ioc->name, __LINE__));
  2940. }
  2941. writel(0, &ioc->chip->HostInterruptStatus);
  2942. if (ioc->logging_level & MPT_DEBUG_INIT) {
  2943. mfp = (__le32 *)reply;
  2944. pr_info("\toffset:data\n");
  2945. for (i = 0; i < reply_bytes/4; i++)
  2946. pr_info("\t[0x%02x]:%08x\n", i*4,
  2947. le32_to_cpu(mfp[i]));
  2948. }
  2949. return 0;
  2950. }
  2951. /**
  2952. * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
  2953. * @ioc: per adapter object
  2954. * @mpi_reply: the reply payload from FW
  2955. * @mpi_request: the request payload sent to FW
  2956. *
  2957. * The SAS IO Unit Control Request message allows the host to perform low-level
  2958. * operations, such as resets on the PHYs of the IO Unit, also allows the host
  2959. * to obtain the IOC assigned device handles for a device if it has other
  2960. * identifying information about the device, in addition allows the host to
  2961. * remove IOC resources associated with the device.
  2962. *
  2963. * Returns 0 for success, non-zero for failure.
  2964. */
  2965. int
  2966. mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
  2967. Mpi2SasIoUnitControlReply_t *mpi_reply,
  2968. Mpi2SasIoUnitControlRequest_t *mpi_request)
  2969. {
  2970. u16 smid;
  2971. u32 ioc_state;
  2972. unsigned long timeleft;
  2973. u8 issue_reset;
  2974. int rc;
  2975. void *request;
  2976. u16 wait_state_count;
  2977. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2978. __func__));
  2979. mutex_lock(&ioc->base_cmds.mutex);
  2980. if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
  2981. pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
  2982. ioc->name, __func__);
  2983. rc = -EAGAIN;
  2984. goto out;
  2985. }
  2986. wait_state_count = 0;
  2987. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  2988. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  2989. if (wait_state_count++ == 10) {
  2990. pr_err(MPT3SAS_FMT
  2991. "%s: failed due to ioc not operational\n",
  2992. ioc->name, __func__);
  2993. rc = -EFAULT;
  2994. goto out;
  2995. }
  2996. ssleep(1);
  2997. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  2998. pr_info(MPT3SAS_FMT
  2999. "%s: waiting for operational state(count=%d)\n",
  3000. ioc->name, __func__, wait_state_count);
  3001. }
  3002. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  3003. if (!smid) {
  3004. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  3005. ioc->name, __func__);
  3006. rc = -EAGAIN;
  3007. goto out;
  3008. }
  3009. rc = 0;
  3010. ioc->base_cmds.status = MPT3_CMD_PENDING;
  3011. request = mpt3sas_base_get_msg_frame(ioc, smid);
  3012. ioc->base_cmds.smid = smid;
  3013. memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
  3014. if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  3015. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
  3016. ioc->ioc_link_reset_in_progress = 1;
  3017. init_completion(&ioc->base_cmds.done);
  3018. mpt3sas_base_put_smid_default(ioc, smid);
  3019. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
  3020. msecs_to_jiffies(10000));
  3021. if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  3022. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
  3023. ioc->ioc_link_reset_in_progress)
  3024. ioc->ioc_link_reset_in_progress = 0;
  3025. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  3026. pr_err(MPT3SAS_FMT "%s: timeout\n",
  3027. ioc->name, __func__);
  3028. _debug_dump_mf(mpi_request,
  3029. sizeof(Mpi2SasIoUnitControlRequest_t)/4);
  3030. if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
  3031. issue_reset = 1;
  3032. goto issue_host_reset;
  3033. }
  3034. if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
  3035. memcpy(mpi_reply, ioc->base_cmds.reply,
  3036. sizeof(Mpi2SasIoUnitControlReply_t));
  3037. else
  3038. memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  3039. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3040. goto out;
  3041. issue_host_reset:
  3042. if (issue_reset)
  3043. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  3044. FORCE_BIG_HAMMER);
  3045. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3046. rc = -EFAULT;
  3047. out:
  3048. mutex_unlock(&ioc->base_cmds.mutex);
  3049. return rc;
  3050. }
  3051. /**
  3052. * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
  3053. * @ioc: per adapter object
  3054. * @mpi_reply: the reply payload from FW
  3055. * @mpi_request: the request payload sent to FW
  3056. *
  3057. * The SCSI Enclosure Processor request message causes the IOC to
  3058. * communicate with SES devices to control LED status signals.
  3059. *
  3060. * Returns 0 for success, non-zero for failure.
  3061. */
  3062. int
  3063. mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
  3064. Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
  3065. {
  3066. u16 smid;
  3067. u32 ioc_state;
  3068. unsigned long timeleft;
  3069. u8 issue_reset;
  3070. int rc;
  3071. void *request;
  3072. u16 wait_state_count;
  3073. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3074. __func__));
  3075. mutex_lock(&ioc->base_cmds.mutex);
  3076. if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
  3077. pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
  3078. ioc->name, __func__);
  3079. rc = -EAGAIN;
  3080. goto out;
  3081. }
  3082. wait_state_count = 0;
  3083. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  3084. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  3085. if (wait_state_count++ == 10) {
  3086. pr_err(MPT3SAS_FMT
  3087. "%s: failed due to ioc not operational\n",
  3088. ioc->name, __func__);
  3089. rc = -EFAULT;
  3090. goto out;
  3091. }
  3092. ssleep(1);
  3093. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  3094. pr_info(MPT3SAS_FMT
  3095. "%s: waiting for operational state(count=%d)\n",
  3096. ioc->name,
  3097. __func__, wait_state_count);
  3098. }
  3099. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  3100. if (!smid) {
  3101. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  3102. ioc->name, __func__);
  3103. rc = -EAGAIN;
  3104. goto out;
  3105. }
  3106. rc = 0;
  3107. ioc->base_cmds.status = MPT3_CMD_PENDING;
  3108. request = mpt3sas_base_get_msg_frame(ioc, smid);
  3109. ioc->base_cmds.smid = smid;
  3110. memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
  3111. init_completion(&ioc->base_cmds.done);
  3112. mpt3sas_base_put_smid_default(ioc, smid);
  3113. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
  3114. msecs_to_jiffies(10000));
  3115. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  3116. pr_err(MPT3SAS_FMT "%s: timeout\n",
  3117. ioc->name, __func__);
  3118. _debug_dump_mf(mpi_request,
  3119. sizeof(Mpi2SepRequest_t)/4);
  3120. if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
  3121. issue_reset = 1;
  3122. goto issue_host_reset;
  3123. }
  3124. if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
  3125. memcpy(mpi_reply, ioc->base_cmds.reply,
  3126. sizeof(Mpi2SepReply_t));
  3127. else
  3128. memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
  3129. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3130. goto out;
  3131. issue_host_reset:
  3132. if (issue_reset)
  3133. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  3134. FORCE_BIG_HAMMER);
  3135. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3136. rc = -EFAULT;
  3137. out:
  3138. mutex_unlock(&ioc->base_cmds.mutex);
  3139. return rc;
  3140. }
  3141. /**
  3142. * _base_get_port_facts - obtain port facts reply and save in ioc
  3143. * @ioc: per adapter object
  3144. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3145. *
  3146. * Returns 0 for success, non-zero for failure.
  3147. */
  3148. static int
  3149. _base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port, int sleep_flag)
  3150. {
  3151. Mpi2PortFactsRequest_t mpi_request;
  3152. Mpi2PortFactsReply_t mpi_reply;
  3153. struct mpt3sas_port_facts *pfacts;
  3154. int mpi_reply_sz, mpi_request_sz, r;
  3155. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3156. __func__));
  3157. mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
  3158. mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
  3159. memset(&mpi_request, 0, mpi_request_sz);
  3160. mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
  3161. mpi_request.PortNumber = port;
  3162. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  3163. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
  3164. if (r != 0) {
  3165. pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
  3166. ioc->name, __func__, r);
  3167. return r;
  3168. }
  3169. pfacts = &ioc->pfacts[port];
  3170. memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
  3171. pfacts->PortNumber = mpi_reply.PortNumber;
  3172. pfacts->VP_ID = mpi_reply.VP_ID;
  3173. pfacts->VF_ID = mpi_reply.VF_ID;
  3174. pfacts->MaxPostedCmdBuffers =
  3175. le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
  3176. return 0;
  3177. }
  3178. /**
  3179. * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
  3180. * @ioc: per adapter object
  3181. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3182. *
  3183. * Returns 0 for success, non-zero for failure.
  3184. */
  3185. static int
  3186. _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  3187. {
  3188. Mpi2IOCFactsRequest_t mpi_request;
  3189. Mpi2IOCFactsReply_t mpi_reply;
  3190. struct mpt3sas_facts *facts;
  3191. int mpi_reply_sz, mpi_request_sz, r;
  3192. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3193. __func__));
  3194. mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
  3195. mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
  3196. memset(&mpi_request, 0, mpi_request_sz);
  3197. mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
  3198. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  3199. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
  3200. if (r != 0) {
  3201. pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
  3202. ioc->name, __func__, r);
  3203. return r;
  3204. }
  3205. facts = &ioc->facts;
  3206. memset(facts, 0, sizeof(struct mpt3sas_facts));
  3207. facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
  3208. facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
  3209. facts->VP_ID = mpi_reply.VP_ID;
  3210. facts->VF_ID = mpi_reply.VF_ID;
  3211. facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
  3212. facts->MaxChainDepth = mpi_reply.MaxChainDepth;
  3213. facts->WhoInit = mpi_reply.WhoInit;
  3214. facts->NumberOfPorts = mpi_reply.NumberOfPorts;
  3215. facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
  3216. facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
  3217. facts->MaxReplyDescriptorPostQueueDepth =
  3218. le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
  3219. facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
  3220. facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
  3221. if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
  3222. ioc->ir_firmware = 1;
  3223. facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
  3224. facts->IOCRequestFrameSize =
  3225. le16_to_cpu(mpi_reply.IOCRequestFrameSize);
  3226. facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
  3227. facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
  3228. ioc->shost->max_id = -1;
  3229. facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
  3230. facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
  3231. facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
  3232. facts->HighPriorityCredit =
  3233. le16_to_cpu(mpi_reply.HighPriorityCredit);
  3234. facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
  3235. facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
  3236. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3237. "hba queue depth(%d), max chains per io(%d)\n",
  3238. ioc->name, facts->RequestCredit,
  3239. facts->MaxChainDepth));
  3240. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3241. "request frame size(%d), reply frame size(%d)\n", ioc->name,
  3242. facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
  3243. return 0;
  3244. }
  3245. /**
  3246. * _base_send_ioc_init - send ioc_init to firmware
  3247. * @ioc: per adapter object
  3248. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3249. *
  3250. * Returns 0 for success, non-zero for failure.
  3251. */
  3252. static int
  3253. _base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  3254. {
  3255. Mpi2IOCInitRequest_t mpi_request;
  3256. Mpi2IOCInitReply_t mpi_reply;
  3257. int r;
  3258. struct timeval current_time;
  3259. u16 ioc_status;
  3260. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3261. __func__));
  3262. memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
  3263. mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
  3264. mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
  3265. mpi_request.VF_ID = 0; /* TODO */
  3266. mpi_request.VP_ID = 0;
  3267. mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
  3268. mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
  3269. if (_base_is_controller_msix_enabled(ioc))
  3270. mpi_request.HostMSIxVectors = ioc->reply_queue_count;
  3271. mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
  3272. mpi_request.ReplyDescriptorPostQueueDepth =
  3273. cpu_to_le16(ioc->reply_post_queue_depth);
  3274. mpi_request.ReplyFreeQueueDepth =
  3275. cpu_to_le16(ioc->reply_free_queue_depth);
  3276. mpi_request.SenseBufferAddressHigh =
  3277. cpu_to_le32((u64)ioc->sense_dma >> 32);
  3278. mpi_request.SystemReplyAddressHigh =
  3279. cpu_to_le32((u64)ioc->reply_dma >> 32);
  3280. mpi_request.SystemRequestFrameBaseAddress =
  3281. cpu_to_le64((u64)ioc->request_dma);
  3282. mpi_request.ReplyFreeQueueAddress =
  3283. cpu_to_le64((u64)ioc->reply_free_dma);
  3284. mpi_request.ReplyDescriptorPostQueueAddress =
  3285. cpu_to_le64((u64)ioc->reply_post_free_dma);
  3286. /* This time stamp specifies number of milliseconds
  3287. * since epoch ~ midnight January 1, 1970.
  3288. */
  3289. do_gettimeofday(&current_time);
  3290. mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
  3291. (current_time.tv_usec / 1000));
  3292. if (ioc->logging_level & MPT_DEBUG_INIT) {
  3293. __le32 *mfp;
  3294. int i;
  3295. mfp = (__le32 *)&mpi_request;
  3296. pr_info("\toffset:data\n");
  3297. for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
  3298. pr_info("\t[0x%02x]:%08x\n", i*4,
  3299. le32_to_cpu(mfp[i]));
  3300. }
  3301. r = _base_handshake_req_reply_wait(ioc,
  3302. sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
  3303. sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
  3304. sleep_flag);
  3305. if (r != 0) {
  3306. pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
  3307. ioc->name, __func__, r);
  3308. return r;
  3309. }
  3310. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  3311. if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
  3312. mpi_reply.IOCLogInfo) {
  3313. pr_err(MPT3SAS_FMT "%s: failed\n", ioc->name, __func__);
  3314. r = -EIO;
  3315. }
  3316. return 0;
  3317. }
  3318. /**
  3319. * mpt3sas_port_enable_done - command completion routine for port enable
  3320. * @ioc: per adapter object
  3321. * @smid: system request message index
  3322. * @msix_index: MSIX table index supplied by the OS
  3323. * @reply: reply message frame(lower 32bit addr)
  3324. *
  3325. * Return 1 meaning mf should be freed from _base_interrupt
  3326. * 0 means the mf is freed from this function.
  3327. */
  3328. u8
  3329. mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  3330. u32 reply)
  3331. {
  3332. MPI2DefaultReply_t *mpi_reply;
  3333. u16 ioc_status;
  3334. if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
  3335. return 1;
  3336. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  3337. if (!mpi_reply)
  3338. return 1;
  3339. if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
  3340. return 1;
  3341. ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
  3342. ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
  3343. ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
  3344. memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  3345. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  3346. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  3347. ioc->port_enable_failed = 1;
  3348. if (ioc->is_driver_loading) {
  3349. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  3350. mpt3sas_port_enable_complete(ioc);
  3351. return 1;
  3352. } else {
  3353. ioc->start_scan_failed = ioc_status;
  3354. ioc->start_scan = 0;
  3355. return 1;
  3356. }
  3357. }
  3358. complete(&ioc->port_enable_cmds.done);
  3359. return 1;
  3360. }
  3361. /**
  3362. * _base_send_port_enable - send port_enable(discovery stuff) to firmware
  3363. * @ioc: per adapter object
  3364. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3365. *
  3366. * Returns 0 for success, non-zero for failure.
  3367. */
  3368. static int
  3369. _base_send_port_enable(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  3370. {
  3371. Mpi2PortEnableRequest_t *mpi_request;
  3372. Mpi2PortEnableReply_t *mpi_reply;
  3373. unsigned long timeleft;
  3374. int r = 0;
  3375. u16 smid;
  3376. u16 ioc_status;
  3377. pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
  3378. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  3379. pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
  3380. ioc->name, __func__);
  3381. return -EAGAIN;
  3382. }
  3383. smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
  3384. if (!smid) {
  3385. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  3386. ioc->name, __func__);
  3387. return -EAGAIN;
  3388. }
  3389. ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
  3390. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3391. ioc->port_enable_cmds.smid = smid;
  3392. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  3393. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  3394. init_completion(&ioc->port_enable_cmds.done);
  3395. mpt3sas_base_put_smid_default(ioc, smid);
  3396. timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
  3397. 300*HZ);
  3398. if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
  3399. pr_err(MPT3SAS_FMT "%s: timeout\n",
  3400. ioc->name, __func__);
  3401. _debug_dump_mf(mpi_request,
  3402. sizeof(Mpi2PortEnableRequest_t)/4);
  3403. if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
  3404. r = -EFAULT;
  3405. else
  3406. r = -ETIME;
  3407. goto out;
  3408. }
  3409. mpi_reply = ioc->port_enable_cmds.reply;
  3410. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  3411. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3412. pr_err(MPT3SAS_FMT "%s: failed with (ioc_status=0x%08x)\n",
  3413. ioc->name, __func__, ioc_status);
  3414. r = -EFAULT;
  3415. goto out;
  3416. }
  3417. out:
  3418. ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
  3419. pr_info(MPT3SAS_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
  3420. "SUCCESS" : "FAILED"));
  3421. return r;
  3422. }
  3423. /**
  3424. * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
  3425. * @ioc: per adapter object
  3426. *
  3427. * Returns 0 for success, non-zero for failure.
  3428. */
  3429. int
  3430. mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
  3431. {
  3432. Mpi2PortEnableRequest_t *mpi_request;
  3433. u16 smid;
  3434. pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
  3435. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  3436. pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
  3437. ioc->name, __func__);
  3438. return -EAGAIN;
  3439. }
  3440. smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
  3441. if (!smid) {
  3442. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  3443. ioc->name, __func__);
  3444. return -EAGAIN;
  3445. }
  3446. ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
  3447. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3448. ioc->port_enable_cmds.smid = smid;
  3449. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  3450. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  3451. mpt3sas_base_put_smid_default(ioc, smid);
  3452. return 0;
  3453. }
  3454. /**
  3455. * _base_determine_wait_on_discovery - desposition
  3456. * @ioc: per adapter object
  3457. *
  3458. * Decide whether to wait on discovery to complete. Used to either
  3459. * locate boot device, or report volumes ahead of physical devices.
  3460. *
  3461. * Returns 1 for wait, 0 for don't wait
  3462. */
  3463. static int
  3464. _base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
  3465. {
  3466. /* We wait for discovery to complete if IR firmware is loaded.
  3467. * The sas topology events arrive before PD events, so we need time to
  3468. * turn on the bit in ioc->pd_handles to indicate PD
  3469. * Also, it maybe required to report Volumes ahead of physical
  3470. * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
  3471. */
  3472. if (ioc->ir_firmware)
  3473. return 1;
  3474. /* if no Bios, then we don't need to wait */
  3475. if (!ioc->bios_pg3.BiosVersion)
  3476. return 0;
  3477. /* Bios is present, then we drop down here.
  3478. *
  3479. * If there any entries in the Bios Page 2, then we wait
  3480. * for discovery to complete.
  3481. */
  3482. /* Current Boot Device */
  3483. if ((ioc->bios_pg2.CurrentBootDeviceForm &
  3484. MPI2_BIOSPAGE2_FORM_MASK) ==
  3485. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
  3486. /* Request Boot Device */
  3487. (ioc->bios_pg2.ReqBootDeviceForm &
  3488. MPI2_BIOSPAGE2_FORM_MASK) ==
  3489. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
  3490. /* Alternate Request Boot Device */
  3491. (ioc->bios_pg2.ReqAltBootDeviceForm &
  3492. MPI2_BIOSPAGE2_FORM_MASK) ==
  3493. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
  3494. return 0;
  3495. return 1;
  3496. }
  3497. /**
  3498. * _base_unmask_events - turn on notification for this event
  3499. * @ioc: per adapter object
  3500. * @event: firmware event
  3501. *
  3502. * The mask is stored in ioc->event_masks.
  3503. */
  3504. static void
  3505. _base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
  3506. {
  3507. u32 desired_event;
  3508. if (event >= 128)
  3509. return;
  3510. desired_event = (1 << (event % 32));
  3511. if (event < 32)
  3512. ioc->event_masks[0] &= ~desired_event;
  3513. else if (event < 64)
  3514. ioc->event_masks[1] &= ~desired_event;
  3515. else if (event < 96)
  3516. ioc->event_masks[2] &= ~desired_event;
  3517. else if (event < 128)
  3518. ioc->event_masks[3] &= ~desired_event;
  3519. }
  3520. /**
  3521. * _base_event_notification - send event notification
  3522. * @ioc: per adapter object
  3523. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3524. *
  3525. * Returns 0 for success, non-zero for failure.
  3526. */
  3527. static int
  3528. _base_event_notification(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  3529. {
  3530. Mpi2EventNotificationRequest_t *mpi_request;
  3531. unsigned long timeleft;
  3532. u16 smid;
  3533. int r = 0;
  3534. int i;
  3535. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3536. __func__));
  3537. if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
  3538. pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
  3539. ioc->name, __func__);
  3540. return -EAGAIN;
  3541. }
  3542. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  3543. if (!smid) {
  3544. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  3545. ioc->name, __func__);
  3546. return -EAGAIN;
  3547. }
  3548. ioc->base_cmds.status = MPT3_CMD_PENDING;
  3549. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3550. ioc->base_cmds.smid = smid;
  3551. memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
  3552. mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  3553. mpi_request->VF_ID = 0; /* TODO */
  3554. mpi_request->VP_ID = 0;
  3555. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  3556. mpi_request->EventMasks[i] =
  3557. cpu_to_le32(ioc->event_masks[i]);
  3558. init_completion(&ioc->base_cmds.done);
  3559. mpt3sas_base_put_smid_default(ioc, smid);
  3560. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
  3561. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  3562. pr_err(MPT3SAS_FMT "%s: timeout\n",
  3563. ioc->name, __func__);
  3564. _debug_dump_mf(mpi_request,
  3565. sizeof(Mpi2EventNotificationRequest_t)/4);
  3566. if (ioc->base_cmds.status & MPT3_CMD_RESET)
  3567. r = -EFAULT;
  3568. else
  3569. r = -ETIME;
  3570. } else
  3571. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s: complete\n",
  3572. ioc->name, __func__));
  3573. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3574. return r;
  3575. }
  3576. /**
  3577. * mpt3sas_base_validate_event_type - validating event types
  3578. * @ioc: per adapter object
  3579. * @event: firmware event
  3580. *
  3581. * This will turn on firmware event notification when application
  3582. * ask for that event. We don't mask events that are already enabled.
  3583. */
  3584. void
  3585. mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
  3586. {
  3587. int i, j;
  3588. u32 event_mask, desired_event;
  3589. u8 send_update_to_fw;
  3590. for (i = 0, send_update_to_fw = 0; i <
  3591. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
  3592. event_mask = ~event_type[i];
  3593. desired_event = 1;
  3594. for (j = 0; j < 32; j++) {
  3595. if (!(event_mask & desired_event) &&
  3596. (ioc->event_masks[i] & desired_event)) {
  3597. ioc->event_masks[i] &= ~desired_event;
  3598. send_update_to_fw = 1;
  3599. }
  3600. desired_event = (desired_event << 1);
  3601. }
  3602. }
  3603. if (!send_update_to_fw)
  3604. return;
  3605. mutex_lock(&ioc->base_cmds.mutex);
  3606. _base_event_notification(ioc, CAN_SLEEP);
  3607. mutex_unlock(&ioc->base_cmds.mutex);
  3608. }
  3609. /**
  3610. * _base_diag_reset - the "big hammer" start of day reset
  3611. * @ioc: per adapter object
  3612. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3613. *
  3614. * Returns 0 for success, non-zero for failure.
  3615. */
  3616. static int
  3617. _base_diag_reset(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  3618. {
  3619. u32 host_diagnostic;
  3620. u32 ioc_state;
  3621. u32 count;
  3622. u32 hcb_size;
  3623. pr_info(MPT3SAS_FMT "sending diag reset !!\n", ioc->name);
  3624. drsprintk(ioc, pr_info(MPT3SAS_FMT "clear interrupts\n",
  3625. ioc->name));
  3626. count = 0;
  3627. do {
  3628. /* Write magic sequence to WriteSequence register
  3629. * Loop until in diagnostic mode
  3630. */
  3631. drsprintk(ioc, pr_info(MPT3SAS_FMT
  3632. "write magic sequence\n", ioc->name));
  3633. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  3634. writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
  3635. writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
  3636. writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
  3637. writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3638. writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3639. writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3640. /* wait 100 msec */
  3641. if (sleep_flag == CAN_SLEEP)
  3642. msleep(100);
  3643. else
  3644. mdelay(100);
  3645. if (count++ > 20)
  3646. goto out;
  3647. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  3648. drsprintk(ioc, pr_info(MPT3SAS_FMT
  3649. "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
  3650. ioc->name, count, host_diagnostic));
  3651. } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
  3652. hcb_size = readl(&ioc->chip->HCBSize);
  3653. drsprintk(ioc, pr_info(MPT3SAS_FMT "diag reset: issued\n",
  3654. ioc->name));
  3655. writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
  3656. &ioc->chip->HostDiagnostic);
  3657. /* don't access any registers for 50 milliseconds */
  3658. msleep(50);
  3659. /* 300 second max wait */
  3660. for (count = 0; count < 3000000 ; count++) {
  3661. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  3662. if (host_diagnostic == 0xFFFFFFFF)
  3663. goto out;
  3664. if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
  3665. break;
  3666. /* wait 1 msec */
  3667. if (sleep_flag == CAN_SLEEP)
  3668. usleep_range(1000, 1500);
  3669. else
  3670. mdelay(1);
  3671. }
  3672. if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
  3673. drsprintk(ioc, pr_info(MPT3SAS_FMT
  3674. "restart the adapter assuming the HCB Address points to good F/W\n",
  3675. ioc->name));
  3676. host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
  3677. host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
  3678. writel(host_diagnostic, &ioc->chip->HostDiagnostic);
  3679. drsprintk(ioc, pr_info(MPT3SAS_FMT
  3680. "re-enable the HCDW\n", ioc->name));
  3681. writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
  3682. &ioc->chip->HCBSize);
  3683. }
  3684. drsprintk(ioc, pr_info(MPT3SAS_FMT "restart the adapter\n",
  3685. ioc->name));
  3686. writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
  3687. &ioc->chip->HostDiagnostic);
  3688. drsprintk(ioc, pr_info(MPT3SAS_FMT
  3689. "disable writes to the diagnostic register\n", ioc->name));
  3690. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  3691. drsprintk(ioc, pr_info(MPT3SAS_FMT
  3692. "Wait for FW to go to the READY state\n", ioc->name));
  3693. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
  3694. sleep_flag);
  3695. if (ioc_state) {
  3696. pr_err(MPT3SAS_FMT
  3697. "%s: failed going to ready state (ioc_state=0x%x)\n",
  3698. ioc->name, __func__, ioc_state);
  3699. goto out;
  3700. }
  3701. pr_info(MPT3SAS_FMT "diag reset: SUCCESS\n", ioc->name);
  3702. return 0;
  3703. out:
  3704. pr_err(MPT3SAS_FMT "diag reset: FAILED\n", ioc->name);
  3705. return -EFAULT;
  3706. }
  3707. /**
  3708. * _base_make_ioc_ready - put controller in READY state
  3709. * @ioc: per adapter object
  3710. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3711. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  3712. *
  3713. * Returns 0 for success, non-zero for failure.
  3714. */
  3715. static int
  3716. _base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, int sleep_flag,
  3717. enum reset_type type)
  3718. {
  3719. u32 ioc_state;
  3720. int rc;
  3721. int count;
  3722. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3723. __func__));
  3724. if (ioc->pci_error_recovery)
  3725. return 0;
  3726. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  3727. dhsprintk(ioc, pr_info(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
  3728. ioc->name, __func__, ioc_state));
  3729. /* if in RESET state, it should move to READY state shortly */
  3730. count = 0;
  3731. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
  3732. while ((ioc_state & MPI2_IOC_STATE_MASK) !=
  3733. MPI2_IOC_STATE_READY) {
  3734. if (count++ == 10) {
  3735. pr_err(MPT3SAS_FMT
  3736. "%s: failed going to ready state (ioc_state=0x%x)\n",
  3737. ioc->name, __func__, ioc_state);
  3738. return -EFAULT;
  3739. }
  3740. if (sleep_flag == CAN_SLEEP)
  3741. ssleep(1);
  3742. else
  3743. mdelay(1000);
  3744. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  3745. }
  3746. }
  3747. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
  3748. return 0;
  3749. if (ioc_state & MPI2_DOORBELL_USED) {
  3750. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  3751. "unexpected doorbell active!\n",
  3752. ioc->name));
  3753. goto issue_diag_reset;
  3754. }
  3755. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  3756. mpt3sas_base_fault_info(ioc, ioc_state &
  3757. MPI2_DOORBELL_DATA_MASK);
  3758. goto issue_diag_reset;
  3759. }
  3760. if (type == FORCE_BIG_HAMMER)
  3761. goto issue_diag_reset;
  3762. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
  3763. if (!(_base_send_ioc_reset(ioc,
  3764. MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
  3765. return 0;
  3766. }
  3767. issue_diag_reset:
  3768. rc = _base_diag_reset(ioc, CAN_SLEEP);
  3769. return rc;
  3770. }
  3771. /**
  3772. * _base_make_ioc_operational - put controller in OPERATIONAL state
  3773. * @ioc: per adapter object
  3774. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3775. *
  3776. * Returns 0 for success, non-zero for failure.
  3777. */
  3778. static int
  3779. _base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  3780. {
  3781. int r, i;
  3782. unsigned long flags;
  3783. u32 reply_address;
  3784. u16 smid;
  3785. struct _tr_list *delayed_tr, *delayed_tr_next;
  3786. struct adapter_reply_queue *reply_q;
  3787. long reply_post_free;
  3788. u32 reply_post_free_sz;
  3789. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3790. __func__));
  3791. /* clean the delayed target reset list */
  3792. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  3793. &ioc->delayed_tr_list, list) {
  3794. list_del(&delayed_tr->list);
  3795. kfree(delayed_tr);
  3796. }
  3797. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  3798. &ioc->delayed_tr_volume_list, list) {
  3799. list_del(&delayed_tr->list);
  3800. kfree(delayed_tr);
  3801. }
  3802. /* initialize the scsi lookup free list */
  3803. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  3804. INIT_LIST_HEAD(&ioc->free_list);
  3805. smid = 1;
  3806. for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
  3807. INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
  3808. ioc->scsi_lookup[i].cb_idx = 0xFF;
  3809. ioc->scsi_lookup[i].smid = smid;
  3810. ioc->scsi_lookup[i].scmd = NULL;
  3811. list_add_tail(&ioc->scsi_lookup[i].tracker_list,
  3812. &ioc->free_list);
  3813. }
  3814. /* hi-priority queue */
  3815. INIT_LIST_HEAD(&ioc->hpr_free_list);
  3816. smid = ioc->hi_priority_smid;
  3817. for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
  3818. ioc->hpr_lookup[i].cb_idx = 0xFF;
  3819. ioc->hpr_lookup[i].smid = smid;
  3820. list_add_tail(&ioc->hpr_lookup[i].tracker_list,
  3821. &ioc->hpr_free_list);
  3822. }
  3823. /* internal queue */
  3824. INIT_LIST_HEAD(&ioc->internal_free_list);
  3825. smid = ioc->internal_smid;
  3826. for (i = 0; i < ioc->internal_depth; i++, smid++) {
  3827. ioc->internal_lookup[i].cb_idx = 0xFF;
  3828. ioc->internal_lookup[i].smid = smid;
  3829. list_add_tail(&ioc->internal_lookup[i].tracker_list,
  3830. &ioc->internal_free_list);
  3831. }
  3832. /* chain pool */
  3833. INIT_LIST_HEAD(&ioc->free_chain_list);
  3834. for (i = 0; i < ioc->chain_depth; i++)
  3835. list_add_tail(&ioc->chain_lookup[i].tracker_list,
  3836. &ioc->free_chain_list);
  3837. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3838. /* initialize Reply Free Queue */
  3839. for (i = 0, reply_address = (u32)ioc->reply_dma ;
  3840. i < ioc->reply_free_queue_depth ; i++, reply_address +=
  3841. ioc->reply_sz)
  3842. ioc->reply_free[i] = cpu_to_le32(reply_address);
  3843. /* initialize reply queues */
  3844. if (ioc->is_driver_loading)
  3845. _base_assign_reply_queues(ioc);
  3846. /* initialize Reply Post Free Queue */
  3847. reply_post_free = (long)ioc->reply_post_free;
  3848. reply_post_free_sz = ioc->reply_post_queue_depth *
  3849. sizeof(Mpi2DefaultReplyDescriptor_t);
  3850. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  3851. reply_q->reply_post_host_index = 0;
  3852. reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
  3853. reply_post_free;
  3854. for (i = 0; i < ioc->reply_post_queue_depth; i++)
  3855. reply_q->reply_post_free[i].Words =
  3856. cpu_to_le64(ULLONG_MAX);
  3857. if (!_base_is_controller_msix_enabled(ioc))
  3858. goto skip_init_reply_post_free_queue;
  3859. reply_post_free += reply_post_free_sz;
  3860. }
  3861. skip_init_reply_post_free_queue:
  3862. r = _base_send_ioc_init(ioc, sleep_flag);
  3863. if (r)
  3864. return r;
  3865. /* initialize reply free host index */
  3866. ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
  3867. writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
  3868. /* initialize reply post host index */
  3869. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  3870. writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
  3871. &ioc->chip->ReplyPostHostIndex);
  3872. if (!_base_is_controller_msix_enabled(ioc))
  3873. goto skip_init_reply_post_host_index;
  3874. }
  3875. skip_init_reply_post_host_index:
  3876. _base_unmask_interrupts(ioc);
  3877. r = _base_event_notification(ioc, sleep_flag);
  3878. if (r)
  3879. return r;
  3880. if (sleep_flag == CAN_SLEEP)
  3881. _base_static_config_pages(ioc);
  3882. if (ioc->is_driver_loading) {
  3883. ioc->wait_for_discovery_to_complete =
  3884. _base_determine_wait_on_discovery(ioc);
  3885. return r; /* scan_start and scan_finished support */
  3886. }
  3887. r = _base_send_port_enable(ioc, sleep_flag);
  3888. if (r)
  3889. return r;
  3890. return r;
  3891. }
  3892. /**
  3893. * mpt3sas_base_free_resources - free resources controller resources
  3894. * @ioc: per adapter object
  3895. *
  3896. * Return nothing.
  3897. */
  3898. void
  3899. mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
  3900. {
  3901. struct pci_dev *pdev = ioc->pdev;
  3902. dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3903. __func__));
  3904. _base_mask_interrupts(ioc);
  3905. ioc->shost_recovery = 1;
  3906. _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
  3907. ioc->shost_recovery = 0;
  3908. _base_free_irq(ioc);
  3909. _base_disable_msix(ioc);
  3910. if (ioc->chip_phys)
  3911. iounmap(ioc->chip);
  3912. ioc->chip_phys = 0;
  3913. pci_release_selected_regions(ioc->pdev, ioc->bars);
  3914. pci_disable_pcie_error_reporting(pdev);
  3915. pci_disable_device(pdev);
  3916. return;
  3917. }
  3918. /**
  3919. * mpt3sas_base_attach - attach controller instance
  3920. * @ioc: per adapter object
  3921. *
  3922. * Returns 0 for success, non-zero for failure.
  3923. */
  3924. int
  3925. mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
  3926. {
  3927. int r, i;
  3928. int cpu_id, last_cpu_id = 0;
  3929. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3930. __func__));
  3931. /* setup cpu_msix_table */
  3932. ioc->cpu_count = num_online_cpus();
  3933. for_each_online_cpu(cpu_id)
  3934. last_cpu_id = cpu_id;
  3935. ioc->cpu_msix_table_sz = last_cpu_id + 1;
  3936. ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
  3937. ioc->reply_queue_count = 1;
  3938. if (!ioc->cpu_msix_table) {
  3939. dfailprintk(ioc, pr_info(MPT3SAS_FMT
  3940. "allocation for cpu_msix_table failed!!!\n",
  3941. ioc->name));
  3942. r = -ENOMEM;
  3943. goto out_free_resources;
  3944. }
  3945. r = mpt3sas_base_map_resources(ioc);
  3946. if (r)
  3947. goto out_free_resources;
  3948. pci_set_drvdata(ioc->pdev, ioc->shost);
  3949. r = _base_get_ioc_facts(ioc, CAN_SLEEP);
  3950. if (r)
  3951. goto out_free_resources;
  3952. /*
  3953. * In SAS3.0,
  3954. * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
  3955. * Target Status - all require the IEEE formated scatter gather
  3956. * elements.
  3957. */
  3958. ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
  3959. ioc->build_sg = &_base_build_sg_ieee;
  3960. ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
  3961. ioc->mpi25 = 1;
  3962. ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
  3963. /*
  3964. * These function pointers for other requests that don't
  3965. * the require IEEE scatter gather elements.
  3966. *
  3967. * For example Configuration Pages and SAS IOUNIT Control don't.
  3968. */
  3969. ioc->build_sg_mpi = &_base_build_sg;
  3970. ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
  3971. r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
  3972. if (r)
  3973. goto out_free_resources;
  3974. ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
  3975. sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
  3976. if (!ioc->pfacts) {
  3977. r = -ENOMEM;
  3978. goto out_free_resources;
  3979. }
  3980. for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
  3981. r = _base_get_port_facts(ioc, i, CAN_SLEEP);
  3982. if (r)
  3983. goto out_free_resources;
  3984. }
  3985. r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
  3986. if (r)
  3987. goto out_free_resources;
  3988. init_waitqueue_head(&ioc->reset_wq);
  3989. /* allocate memory pd handle bitmask list */
  3990. ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
  3991. if (ioc->facts.MaxDevHandle % 8)
  3992. ioc->pd_handles_sz++;
  3993. ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
  3994. GFP_KERNEL);
  3995. if (!ioc->pd_handles) {
  3996. r = -ENOMEM;
  3997. goto out_free_resources;
  3998. }
  3999. ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
  4000. GFP_KERNEL);
  4001. if (!ioc->blocking_handles) {
  4002. r = -ENOMEM;
  4003. goto out_free_resources;
  4004. }
  4005. ioc->fwfault_debug = mpt3sas_fwfault_debug;
  4006. /* base internal command bits */
  4007. mutex_init(&ioc->base_cmds.mutex);
  4008. ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4009. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  4010. /* port_enable command bits */
  4011. ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4012. ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
  4013. /* transport internal command bits */
  4014. ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4015. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  4016. mutex_init(&ioc->transport_cmds.mutex);
  4017. /* scsih internal command bits */
  4018. ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4019. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  4020. mutex_init(&ioc->scsih_cmds.mutex);
  4021. /* task management internal command bits */
  4022. ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4023. ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
  4024. mutex_init(&ioc->tm_cmds.mutex);
  4025. /* config page internal command bits */
  4026. ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4027. ioc->config_cmds.status = MPT3_CMD_NOT_USED;
  4028. mutex_init(&ioc->config_cmds.mutex);
  4029. /* ctl module internal command bits */
  4030. ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4031. ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
  4032. ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
  4033. mutex_init(&ioc->ctl_cmds.mutex);
  4034. if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
  4035. !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
  4036. !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
  4037. !ioc->ctl_cmds.sense) {
  4038. r = -ENOMEM;
  4039. goto out_free_resources;
  4040. }
  4041. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  4042. ioc->event_masks[i] = -1;
  4043. /* here we enable the events we care about */
  4044. _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
  4045. _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
  4046. _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
  4047. _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  4048. _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
  4049. _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
  4050. _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
  4051. _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
  4052. _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
  4053. _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
  4054. r = _base_make_ioc_operational(ioc, CAN_SLEEP);
  4055. if (r)
  4056. goto out_free_resources;
  4057. return 0;
  4058. out_free_resources:
  4059. ioc->remove_host = 1;
  4060. mpt3sas_base_free_resources(ioc);
  4061. _base_release_memory_pools(ioc);
  4062. pci_set_drvdata(ioc->pdev, NULL);
  4063. kfree(ioc->cpu_msix_table);
  4064. kfree(ioc->pd_handles);
  4065. kfree(ioc->blocking_handles);
  4066. kfree(ioc->tm_cmds.reply);
  4067. kfree(ioc->transport_cmds.reply);
  4068. kfree(ioc->scsih_cmds.reply);
  4069. kfree(ioc->config_cmds.reply);
  4070. kfree(ioc->base_cmds.reply);
  4071. kfree(ioc->port_enable_cmds.reply);
  4072. kfree(ioc->ctl_cmds.reply);
  4073. kfree(ioc->ctl_cmds.sense);
  4074. kfree(ioc->pfacts);
  4075. ioc->ctl_cmds.reply = NULL;
  4076. ioc->base_cmds.reply = NULL;
  4077. ioc->tm_cmds.reply = NULL;
  4078. ioc->scsih_cmds.reply = NULL;
  4079. ioc->transport_cmds.reply = NULL;
  4080. ioc->config_cmds.reply = NULL;
  4081. ioc->pfacts = NULL;
  4082. return r;
  4083. }
  4084. /**
  4085. * mpt3sas_base_detach - remove controller instance
  4086. * @ioc: per adapter object
  4087. *
  4088. * Return nothing.
  4089. */
  4090. void
  4091. mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
  4092. {
  4093. dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  4094. __func__));
  4095. mpt3sas_base_stop_watchdog(ioc);
  4096. mpt3sas_base_free_resources(ioc);
  4097. _base_release_memory_pools(ioc);
  4098. pci_set_drvdata(ioc->pdev, NULL);
  4099. kfree(ioc->cpu_msix_table);
  4100. kfree(ioc->pd_handles);
  4101. kfree(ioc->blocking_handles);
  4102. kfree(ioc->pfacts);
  4103. kfree(ioc->ctl_cmds.reply);
  4104. kfree(ioc->ctl_cmds.sense);
  4105. kfree(ioc->base_cmds.reply);
  4106. kfree(ioc->port_enable_cmds.reply);
  4107. kfree(ioc->tm_cmds.reply);
  4108. kfree(ioc->transport_cmds.reply);
  4109. kfree(ioc->scsih_cmds.reply);
  4110. kfree(ioc->config_cmds.reply);
  4111. }
  4112. /**
  4113. * _base_reset_handler - reset callback handler (for base)
  4114. * @ioc: per adapter object
  4115. * @reset_phase: phase
  4116. *
  4117. * The handler for doing any required cleanup or initialization.
  4118. *
  4119. * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
  4120. * MPT3_IOC_DONE_RESET
  4121. *
  4122. * Return nothing.
  4123. */
  4124. static void
  4125. _base_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
  4126. {
  4127. mpt3sas_scsih_reset_handler(ioc, reset_phase);
  4128. mpt3sas_ctl_reset_handler(ioc, reset_phase);
  4129. switch (reset_phase) {
  4130. case MPT3_IOC_PRE_RESET:
  4131. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  4132. "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
  4133. break;
  4134. case MPT3_IOC_AFTER_RESET:
  4135. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  4136. "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
  4137. if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
  4138. ioc->transport_cmds.status |= MPT3_CMD_RESET;
  4139. mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
  4140. complete(&ioc->transport_cmds.done);
  4141. }
  4142. if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
  4143. ioc->base_cmds.status |= MPT3_CMD_RESET;
  4144. mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
  4145. complete(&ioc->base_cmds.done);
  4146. }
  4147. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  4148. ioc->port_enable_failed = 1;
  4149. ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
  4150. mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
  4151. if (ioc->is_driver_loading) {
  4152. ioc->start_scan_failed =
  4153. MPI2_IOCSTATUS_INTERNAL_ERROR;
  4154. ioc->start_scan = 0;
  4155. ioc->port_enable_cmds.status =
  4156. MPT3_CMD_NOT_USED;
  4157. } else
  4158. complete(&ioc->port_enable_cmds.done);
  4159. }
  4160. if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
  4161. ioc->config_cmds.status |= MPT3_CMD_RESET;
  4162. mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
  4163. ioc->config_cmds.smid = USHRT_MAX;
  4164. complete(&ioc->config_cmds.done);
  4165. }
  4166. break;
  4167. case MPT3_IOC_DONE_RESET:
  4168. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  4169. "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
  4170. break;
  4171. }
  4172. }
  4173. /**
  4174. * _wait_for_commands_to_complete - reset controller
  4175. * @ioc: Pointer to MPT_ADAPTER structure
  4176. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  4177. *
  4178. * This function waiting(3s) for all pending commands to complete
  4179. * prior to putting controller in reset.
  4180. */
  4181. static void
  4182. _wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  4183. {
  4184. u32 ioc_state;
  4185. unsigned long flags;
  4186. u16 i;
  4187. ioc->pending_io_count = 0;
  4188. if (sleep_flag != CAN_SLEEP)
  4189. return;
  4190. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  4191. if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
  4192. return;
  4193. /* pending command count */
  4194. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  4195. for (i = 0; i < ioc->scsiio_depth; i++)
  4196. if (ioc->scsi_lookup[i].cb_idx != 0xFF)
  4197. ioc->pending_io_count++;
  4198. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  4199. if (!ioc->pending_io_count)
  4200. return;
  4201. /* wait for pending commands to complete */
  4202. wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
  4203. }
  4204. /**
  4205. * mpt3sas_base_hard_reset_handler - reset controller
  4206. * @ioc: Pointer to MPT_ADAPTER structure
  4207. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  4208. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  4209. *
  4210. * Returns 0 for success, non-zero for failure.
  4211. */
  4212. int
  4213. mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc, int sleep_flag,
  4214. enum reset_type type)
  4215. {
  4216. int r;
  4217. unsigned long flags;
  4218. u32 ioc_state;
  4219. u8 is_fault = 0, is_trigger = 0;
  4220. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
  4221. __func__));
  4222. if (ioc->pci_error_recovery) {
  4223. pr_err(MPT3SAS_FMT "%s: pci error recovery reset\n",
  4224. ioc->name, __func__);
  4225. r = 0;
  4226. goto out_unlocked;
  4227. }
  4228. if (mpt3sas_fwfault_debug)
  4229. mpt3sas_halt_firmware(ioc);
  4230. /* TODO - What we really should be doing is pulling
  4231. * out all the code associated with NO_SLEEP; its never used.
  4232. * That is legacy code from mpt fusion driver, ported over.
  4233. * I will leave this BUG_ON here for now till its been resolved.
  4234. */
  4235. BUG_ON(sleep_flag == NO_SLEEP);
  4236. /* wait for an active reset in progress to complete */
  4237. if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
  4238. do {
  4239. ssleep(1);
  4240. } while (ioc->shost_recovery == 1);
  4241. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
  4242. __func__));
  4243. return ioc->ioc_reset_in_progress_status;
  4244. }
  4245. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  4246. ioc->shost_recovery = 1;
  4247. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  4248. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  4249. MPT3_DIAG_BUFFER_IS_REGISTERED) &&
  4250. (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  4251. MPT3_DIAG_BUFFER_IS_RELEASED))) {
  4252. is_trigger = 1;
  4253. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  4254. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  4255. is_fault = 1;
  4256. }
  4257. _base_reset_handler(ioc, MPT3_IOC_PRE_RESET);
  4258. _wait_for_commands_to_complete(ioc, sleep_flag);
  4259. _base_mask_interrupts(ioc);
  4260. r = _base_make_ioc_ready(ioc, sleep_flag, type);
  4261. if (r)
  4262. goto out;
  4263. _base_reset_handler(ioc, MPT3_IOC_AFTER_RESET);
  4264. /* If this hard reset is called while port enable is active, then
  4265. * there is no reason to call make_ioc_operational
  4266. */
  4267. if (ioc->is_driver_loading && ioc->port_enable_failed) {
  4268. ioc->remove_host = 1;
  4269. r = -EFAULT;
  4270. goto out;
  4271. }
  4272. r = _base_get_ioc_facts(ioc, CAN_SLEEP);
  4273. if (r)
  4274. goto out;
  4275. r = _base_make_ioc_operational(ioc, sleep_flag);
  4276. if (!r)
  4277. _base_reset_handler(ioc, MPT3_IOC_DONE_RESET);
  4278. out:
  4279. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: %s\n",
  4280. ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
  4281. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  4282. ioc->ioc_reset_in_progress_status = r;
  4283. ioc->shost_recovery = 0;
  4284. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  4285. ioc->ioc_reset_count++;
  4286. mutex_unlock(&ioc->reset_in_progress_mutex);
  4287. out_unlocked:
  4288. if ((r == 0) && is_trigger) {
  4289. if (is_fault)
  4290. mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
  4291. else
  4292. mpt3sas_trigger_master(ioc,
  4293. MASTER_TRIGGER_ADAPTER_RESET);
  4294. }
  4295. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
  4296. __func__));
  4297. return r;
  4298. }