mpt2sas_base.c 131 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638
  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/mpt2sas/mpt2_base.c
  6. * Copyright (C) 2007-2010 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/sort.h>
  55. #include <linux/io.h>
  56. #include <linux/time.h>
  57. #include <linux/kthread.h>
  58. #include <linux/aer.h>
  59. #include "mpt2sas_base.h"
  60. static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
  61. #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
  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 missing_delay[2] = {-1, -1};
  74. module_param_array(missing_delay, int, NULL, 0);
  75. MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
  76. static int mpt2sas_fwfault_debug;
  77. MODULE_PARM_DESC(mpt2sas_fwfault_debug, " enable detection of firmware fault "
  78. "and halt firmware - (default=0)");
  79. static int disable_discovery = -1;
  80. module_param(disable_discovery, int, 0);
  81. MODULE_PARM_DESC(disable_discovery, " disable discovery ");
  82. /**
  83. * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
  84. *
  85. */
  86. static int
  87. _scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
  88. {
  89. int ret = param_set_int(val, kp);
  90. struct MPT2SAS_ADAPTER *ioc;
  91. if (ret)
  92. return ret;
  93. printk(KERN_INFO "setting fwfault_debug(%d)\n", mpt2sas_fwfault_debug);
  94. list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
  95. ioc->fwfault_debug = mpt2sas_fwfault_debug;
  96. return 0;
  97. }
  98. module_param_call(mpt2sas_fwfault_debug, _scsih_set_fwfault_debug,
  99. param_get_int, &mpt2sas_fwfault_debug, 0644);
  100. /**
  101. * mpt2sas_remove_dead_ioc_func - kthread context to remove dead ioc
  102. * @arg: input argument, used to derive ioc
  103. *
  104. * Return 0 if controller is removed from pci subsystem.
  105. * Return -1 for other case.
  106. */
  107. static int mpt2sas_remove_dead_ioc_func(void *arg)
  108. {
  109. struct MPT2SAS_ADAPTER *ioc = (struct MPT2SAS_ADAPTER *)arg;
  110. struct pci_dev *pdev;
  111. if ((ioc == NULL))
  112. return -1;
  113. pdev = ioc->pdev;
  114. if ((pdev == NULL))
  115. return -1;
  116. pci_stop_and_remove_bus_device(pdev);
  117. return 0;
  118. }
  119. /**
  120. * _base_fault_reset_work - workq handling ioc fault conditions
  121. * @work: input argument, used to derive ioc
  122. * Context: sleep.
  123. *
  124. * Return nothing.
  125. */
  126. static void
  127. _base_fault_reset_work(struct work_struct *work)
  128. {
  129. struct MPT2SAS_ADAPTER *ioc =
  130. container_of(work, struct MPT2SAS_ADAPTER, fault_reset_work.work);
  131. unsigned long flags;
  132. u32 doorbell;
  133. int rc;
  134. struct task_struct *p;
  135. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  136. if (ioc->shost_recovery)
  137. goto rearm_timer;
  138. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  139. doorbell = mpt2sas_base_get_iocstate(ioc, 0);
  140. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
  141. printk(MPT2SAS_INFO_FMT "%s : SAS host is non-operational !!!!\n",
  142. ioc->name, __func__);
  143. /*
  144. * Call _scsih_flush_pending_cmds callback so that we flush all
  145. * pending commands back to OS. This call is required to aovid
  146. * deadlock at block layer. Dead IOC will fail to do diag reset,
  147. * and this call is safe since dead ioc will never return any
  148. * command back from HW.
  149. */
  150. ioc->schedule_dead_ioc_flush_running_cmds(ioc);
  151. /*
  152. * Set remove_host flag early since kernel thread will
  153. * take some time to execute.
  154. */
  155. ioc->remove_host = 1;
  156. /*Remove the Dead Host */
  157. p = kthread_run(mpt2sas_remove_dead_ioc_func, ioc,
  158. "mpt2sas_dead_ioc_%d", ioc->id);
  159. if (IS_ERR(p)) {
  160. printk(MPT2SAS_ERR_FMT
  161. "%s: Running mpt2sas_dead_ioc thread failed !!!!\n",
  162. ioc->name, __func__);
  163. } else {
  164. printk(MPT2SAS_ERR_FMT
  165. "%s: Running mpt2sas_dead_ioc thread success !!!!\n",
  166. ioc->name, __func__);
  167. }
  168. return; /* don't rearm timer */
  169. }
  170. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  171. rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  172. FORCE_BIG_HAMMER);
  173. printk(MPT2SAS_WARN_FMT "%s: hard reset: %s\n", ioc->name,
  174. __func__, (rc == 0) ? "success" : "failed");
  175. doorbell = mpt2sas_base_get_iocstate(ioc, 0);
  176. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  177. mpt2sas_base_fault_info(ioc, doorbell &
  178. MPI2_DOORBELL_DATA_MASK);
  179. }
  180. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  181. rearm_timer:
  182. if (ioc->fault_reset_work_q)
  183. queue_delayed_work(ioc->fault_reset_work_q,
  184. &ioc->fault_reset_work,
  185. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  186. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  187. }
  188. /**
  189. * mpt2sas_base_start_watchdog - start the fault_reset_work_q
  190. * @ioc: per adapter object
  191. * Context: sleep.
  192. *
  193. * Return nothing.
  194. */
  195. void
  196. mpt2sas_base_start_watchdog(struct MPT2SAS_ADAPTER *ioc)
  197. {
  198. unsigned long flags;
  199. if (ioc->fault_reset_work_q)
  200. return;
  201. /* initialize fault polling */
  202. INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
  203. snprintf(ioc->fault_reset_work_q_name,
  204. sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
  205. ioc->fault_reset_work_q =
  206. create_singlethread_workqueue(ioc->fault_reset_work_q_name);
  207. if (!ioc->fault_reset_work_q) {
  208. printk(MPT2SAS_ERR_FMT "%s: failed (line=%d)\n",
  209. ioc->name, __func__, __LINE__);
  210. return;
  211. }
  212. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  213. if (ioc->fault_reset_work_q)
  214. queue_delayed_work(ioc->fault_reset_work_q,
  215. &ioc->fault_reset_work,
  216. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  217. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  218. }
  219. /**
  220. * mpt2sas_base_stop_watchdog - stop the fault_reset_work_q
  221. * @ioc: per adapter object
  222. * Context: sleep.
  223. *
  224. * Return nothing.
  225. */
  226. void
  227. mpt2sas_base_stop_watchdog(struct MPT2SAS_ADAPTER *ioc)
  228. {
  229. unsigned long flags;
  230. struct workqueue_struct *wq;
  231. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  232. wq = ioc->fault_reset_work_q;
  233. ioc->fault_reset_work_q = NULL;
  234. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  235. if (wq) {
  236. if (!cancel_delayed_work(&ioc->fault_reset_work))
  237. flush_workqueue(wq);
  238. destroy_workqueue(wq);
  239. }
  240. }
  241. /**
  242. * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
  243. * @ioc: per adapter object
  244. * @fault_code: fault code
  245. *
  246. * Return nothing.
  247. */
  248. void
  249. mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
  250. {
  251. printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
  252. ioc->name, fault_code);
  253. }
  254. /**
  255. * mpt2sas_halt_firmware - halt's mpt controller firmware
  256. * @ioc: per adapter object
  257. *
  258. * For debugging timeout related issues. Writing 0xCOFFEE00
  259. * to the doorbell register will halt controller firmware. With
  260. * the purpose to stop both driver and firmware, the enduser can
  261. * obtain a ring buffer from controller UART.
  262. */
  263. void
  264. mpt2sas_halt_firmware(struct MPT2SAS_ADAPTER *ioc)
  265. {
  266. u32 doorbell;
  267. if (!ioc->fwfault_debug)
  268. return;
  269. dump_stack();
  270. doorbell = readl(&ioc->chip->Doorbell);
  271. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  272. mpt2sas_base_fault_info(ioc , doorbell);
  273. else {
  274. writel(0xC0FFEE00, &ioc->chip->Doorbell);
  275. printk(MPT2SAS_ERR_FMT "Firmware is halted due to command "
  276. "timeout\n", ioc->name);
  277. }
  278. panic("panic in %s\n", __func__);
  279. }
  280. #ifdef CONFIG_SCSI_MPT2SAS_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 MPT2SAS_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. printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
  473. " (%s)\n", 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 MPT2SAS_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. if (!ioc->hide_ir_msg)
  510. desc = "IR Operation Status";
  511. break;
  512. case MPI2_EVENT_SAS_DISCOVERY:
  513. {
  514. Mpi2EventDataSasDiscovery_t *event_data =
  515. (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
  516. printk(MPT2SAS_INFO_FMT "Discovery: (%s)", ioc->name,
  517. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  518. "start" : "stop");
  519. if (event_data->DiscoveryStatus)
  520. printk("discovery_status(0x%08x)",
  521. le32_to_cpu(event_data->DiscoveryStatus));
  522. printk("\n");
  523. return;
  524. }
  525. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  526. desc = "SAS Broadcast Primitive";
  527. break;
  528. case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
  529. desc = "SAS Init Device Status Change";
  530. break;
  531. case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
  532. desc = "SAS Init Table Overflow";
  533. break;
  534. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  535. desc = "SAS Topology Change List";
  536. break;
  537. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  538. desc = "SAS Enclosure Device Status Change";
  539. break;
  540. case MPI2_EVENT_IR_VOLUME:
  541. if (!ioc->hide_ir_msg)
  542. desc = "IR Volume";
  543. break;
  544. case MPI2_EVENT_IR_PHYSICAL_DISK:
  545. if (!ioc->hide_ir_msg)
  546. desc = "IR Physical Disk";
  547. break;
  548. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  549. if (!ioc->hide_ir_msg)
  550. desc = "IR Configuration Change List";
  551. break;
  552. case MPI2_EVENT_LOG_ENTRY_ADDED:
  553. if (!ioc->hide_ir_msg)
  554. desc = "Log Entry Added";
  555. break;
  556. }
  557. if (!desc)
  558. return;
  559. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
  560. }
  561. #endif
  562. /**
  563. * _base_sas_log_info - verbose translation of firmware log info
  564. * @ioc: per adapter object
  565. * @log_info: log info
  566. *
  567. * Return nothing.
  568. */
  569. static void
  570. _base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
  571. {
  572. union loginfo_type {
  573. u32 loginfo;
  574. struct {
  575. u32 subcode:16;
  576. u32 code:8;
  577. u32 originator:4;
  578. u32 bus_type:4;
  579. } dw;
  580. };
  581. union loginfo_type sas_loginfo;
  582. char *originator_str = NULL;
  583. sas_loginfo.loginfo = log_info;
  584. if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
  585. return;
  586. /* each nexus loss loginfo */
  587. if (log_info == 0x31170000)
  588. return;
  589. /* eat the loginfos associated with task aborts */
  590. if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
  591. 0x31140000 || log_info == 0x31130000))
  592. return;
  593. switch (sas_loginfo.dw.originator) {
  594. case 0:
  595. originator_str = "IOP";
  596. break;
  597. case 1:
  598. originator_str = "PL";
  599. break;
  600. case 2:
  601. if (!ioc->hide_ir_msg)
  602. originator_str = "IR";
  603. else
  604. originator_str = "WarpDrive";
  605. break;
  606. }
  607. printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
  608. "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
  609. originator_str, sas_loginfo.dw.code,
  610. sas_loginfo.dw.subcode);
  611. }
  612. /**
  613. * _base_display_reply_info -
  614. * @ioc: per adapter object
  615. * @smid: system request message index
  616. * @msix_index: MSIX table index supplied by the OS
  617. * @reply: reply message frame(lower 32bit addr)
  618. *
  619. * Return nothing.
  620. */
  621. static void
  622. _base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  623. u32 reply)
  624. {
  625. MPI2DefaultReply_t *mpi_reply;
  626. u16 ioc_status;
  627. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  628. if (unlikely(!mpi_reply)) {
  629. printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  630. ioc->name, __FILE__, __LINE__, __func__);
  631. return;
  632. }
  633. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  634. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  635. if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
  636. (ioc->logging_level & MPT_DEBUG_REPLY)) {
  637. _base_sas_ioc_info(ioc , mpi_reply,
  638. mpt2sas_base_get_msg_frame(ioc, smid));
  639. }
  640. #endif
  641. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
  642. _base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
  643. }
  644. /**
  645. * mpt2sas_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. mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  656. u32 reply)
  657. {
  658. MPI2DefaultReply_t *mpi_reply;
  659. mpi_reply = mpt2sas_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 == MPT2_CMD_NOT_USED)
  663. return 1;
  664. ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
  665. if (mpi_reply) {
  666. ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
  667. memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  668. }
  669. ioc->base_cmds.status &= ~MPT2_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 MPT2SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
  684. {
  685. Mpi2EventNotificationReply_t *mpi_reply;
  686. Mpi2EventAckRequest_t *ack_request;
  687. u16 smid;
  688. mpi_reply = mpt2sas_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_MPT2SAS_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 = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  699. if (!smid) {
  700. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  701. ioc->name, __func__);
  702. goto out;
  703. }
  704. ack_request = mpt2sas_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. mpt2sas_base_put_smid_default(ioc, smid);
  712. out:
  713. /* scsih callback handler */
  714. mpt2sas_scsih_event_callback(ioc, msix_index, reply);
  715. /* ctl callback handler */
  716. mpt2sas_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 MPT2SAS_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 MPT2SAS_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 MPT2SAS_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 MPT2SAS_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. MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
  829. reply = le32_to_cpu
  830. (rpf->AddressReply.ReplyFrameAddress);
  831. if (reply > ioc->reply_dma_max_address ||
  832. reply < ioc->reply_dma_min_address)
  833. reply = 0;
  834. } else if (request_desript_type ==
  835. MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
  836. goto next;
  837. else if (request_desript_type ==
  838. MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
  839. goto next;
  840. if (smid) {
  841. cb_idx = _base_get_cb_idx(ioc, smid);
  842. if ((likely(cb_idx < MPT_MAX_CALLBACKS))
  843. && (likely(mpt_callbacks[cb_idx] != NULL))) {
  844. rc = mpt_callbacks[cb_idx](ioc, smid,
  845. msix_index, reply);
  846. if (reply)
  847. _base_display_reply_info(ioc, smid,
  848. msix_index, reply);
  849. if (rc)
  850. mpt2sas_base_free_smid(ioc, smid);
  851. }
  852. }
  853. if (!smid)
  854. _base_async_event(ioc, msix_index, reply);
  855. /* reply free queue handling */
  856. if (reply) {
  857. ioc->reply_free_host_index =
  858. (ioc->reply_free_host_index ==
  859. (ioc->reply_free_queue_depth - 1)) ?
  860. 0 : ioc->reply_free_host_index + 1;
  861. ioc->reply_free[ioc->reply_free_host_index] =
  862. cpu_to_le32(reply);
  863. wmb();
  864. writel(ioc->reply_free_host_index,
  865. &ioc->chip->ReplyFreeHostIndex);
  866. }
  867. next:
  868. rpf->Words = cpu_to_le64(ULLONG_MAX);
  869. reply_q->reply_post_host_index =
  870. (reply_q->reply_post_host_index ==
  871. (ioc->reply_post_queue_depth - 1)) ? 0 :
  872. reply_q->reply_post_host_index + 1;
  873. request_desript_type =
  874. reply_q->reply_post_free[reply_q->reply_post_host_index].
  875. Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  876. completed_cmds++;
  877. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  878. goto out;
  879. if (!reply_q->reply_post_host_index)
  880. rpf = reply_q->reply_post_free;
  881. else
  882. rpf++;
  883. } while (1);
  884. out:
  885. if (!completed_cmds) {
  886. atomic_dec(&reply_q->busy);
  887. return IRQ_NONE;
  888. }
  889. wmb();
  890. if (ioc->is_warpdrive) {
  891. writel(reply_q->reply_post_host_index,
  892. ioc->reply_post_host_index[msix_index]);
  893. atomic_dec(&reply_q->busy);
  894. return IRQ_HANDLED;
  895. }
  896. writel(reply_q->reply_post_host_index | (msix_index <<
  897. MPI2_RPHI_MSIX_INDEX_SHIFT), &ioc->chip->ReplyPostHostIndex);
  898. atomic_dec(&reply_q->busy);
  899. return IRQ_HANDLED;
  900. }
  901. /**
  902. * _base_is_controller_msix_enabled - is controller support muli-reply queues
  903. * @ioc: per adapter object
  904. *
  905. */
  906. static inline int
  907. _base_is_controller_msix_enabled(struct MPT2SAS_ADAPTER *ioc)
  908. {
  909. return (ioc->facts.IOCCapabilities &
  910. MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
  911. }
  912. /**
  913. * mpt2sas_base_flush_reply_queues - flushing the MSIX reply queues
  914. * @ioc: per adapter object
  915. * Context: ISR conext
  916. *
  917. * Called when a Task Management request has completed. We want
  918. * to flush the other reply queues so all the outstanding IO has been
  919. * completed back to OS before we process the TM completetion.
  920. *
  921. * Return nothing.
  922. */
  923. void
  924. mpt2sas_base_flush_reply_queues(struct MPT2SAS_ADAPTER *ioc)
  925. {
  926. struct adapter_reply_queue *reply_q;
  927. /* If MSIX capability is turned off
  928. * then multi-queues are not enabled
  929. */
  930. if (!_base_is_controller_msix_enabled(ioc))
  931. return;
  932. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  933. if (ioc->shost_recovery)
  934. return;
  935. /* TMs are on msix_index == 0 */
  936. if (reply_q->msix_index == 0)
  937. continue;
  938. _base_interrupt(reply_q->vector, (void *)reply_q);
  939. }
  940. }
  941. /**
  942. * mpt2sas_base_release_callback_handler - clear interrupt callback handler
  943. * @cb_idx: callback index
  944. *
  945. * Return nothing.
  946. */
  947. void
  948. mpt2sas_base_release_callback_handler(u8 cb_idx)
  949. {
  950. mpt_callbacks[cb_idx] = NULL;
  951. }
  952. /**
  953. * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
  954. * @cb_func: callback function
  955. *
  956. * Returns cb_func.
  957. */
  958. u8
  959. mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
  960. {
  961. u8 cb_idx;
  962. for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
  963. if (mpt_callbacks[cb_idx] == NULL)
  964. break;
  965. mpt_callbacks[cb_idx] = cb_func;
  966. return cb_idx;
  967. }
  968. /**
  969. * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
  970. *
  971. * Return nothing.
  972. */
  973. void
  974. mpt2sas_base_initialize_callback_handler(void)
  975. {
  976. u8 cb_idx;
  977. for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
  978. mpt2sas_base_release_callback_handler(cb_idx);
  979. }
  980. /**
  981. * mpt2sas_base_build_zero_len_sge - build zero length sg entry
  982. * @ioc: per adapter object
  983. * @paddr: virtual address for SGE
  984. *
  985. * Create a zero length scatter gather entry to insure the IOCs hardware has
  986. * something to use if the target device goes brain dead and tries
  987. * to send data even when none is asked for.
  988. *
  989. * Return nothing.
  990. */
  991. void
  992. mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
  993. {
  994. u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
  995. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
  996. MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
  997. MPI2_SGE_FLAGS_SHIFT);
  998. ioc->base_add_sg_single(paddr, flags_length, -1);
  999. }
  1000. /**
  1001. * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
  1002. * @paddr: virtual address for SGE
  1003. * @flags_length: SGE flags and data transfer length
  1004. * @dma_addr: Physical address
  1005. *
  1006. * Return nothing.
  1007. */
  1008. static void
  1009. _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  1010. {
  1011. Mpi2SGESimple32_t *sgel = paddr;
  1012. flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
  1013. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  1014. sgel->FlagsLength = cpu_to_le32(flags_length);
  1015. sgel->Address = cpu_to_le32(dma_addr);
  1016. }
  1017. /**
  1018. * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
  1019. * @paddr: virtual address for SGE
  1020. * @flags_length: SGE flags and data transfer length
  1021. * @dma_addr: Physical address
  1022. *
  1023. * Return nothing.
  1024. */
  1025. static void
  1026. _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  1027. {
  1028. Mpi2SGESimple64_t *sgel = paddr;
  1029. flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
  1030. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  1031. sgel->FlagsLength = cpu_to_le32(flags_length);
  1032. sgel->Address = cpu_to_le64(dma_addr);
  1033. }
  1034. #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
  1035. /**
  1036. * _base_config_dma_addressing - set dma addressing
  1037. * @ioc: per adapter object
  1038. * @pdev: PCI device struct
  1039. *
  1040. * Returns 0 for success, non-zero for failure.
  1041. */
  1042. static int
  1043. _base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
  1044. {
  1045. struct sysinfo s;
  1046. char *desc = NULL;
  1047. if (sizeof(dma_addr_t) > 4) {
  1048. const uint64_t required_mask =
  1049. dma_get_required_mask(&pdev->dev);
  1050. if ((required_mask > DMA_BIT_MASK(32)) && !pci_set_dma_mask(pdev,
  1051. DMA_BIT_MASK(64)) && !pci_set_consistent_dma_mask(pdev,
  1052. DMA_BIT_MASK(64))) {
  1053. ioc->base_add_sg_single = &_base_add_sg_single_64;
  1054. ioc->sge_size = sizeof(Mpi2SGESimple64_t);
  1055. desc = "64";
  1056. goto out;
  1057. }
  1058. }
  1059. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
  1060. && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
  1061. ioc->base_add_sg_single = &_base_add_sg_single_32;
  1062. ioc->sge_size = sizeof(Mpi2SGESimple32_t);
  1063. desc = "32";
  1064. } else
  1065. return -ENODEV;
  1066. out:
  1067. si_meminfo(&s);
  1068. printk(MPT2SAS_INFO_FMT "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, "
  1069. "total mem (%ld kB)\n", ioc->name, desc, convert_to_kb(s.totalram));
  1070. return 0;
  1071. }
  1072. /**
  1073. * _base_check_enable_msix - checks MSIX capabable.
  1074. * @ioc: per adapter object
  1075. *
  1076. * Check to see if card is capable of MSIX, and set number
  1077. * of available msix vectors
  1078. */
  1079. static int
  1080. _base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
  1081. {
  1082. int base;
  1083. u16 message_control;
  1084. /* Check whether controller SAS2008 B0 controller,
  1085. if it is SAS2008 B0 controller use IO-APIC instead of MSIX */
  1086. if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
  1087. ioc->pdev->revision == 0x01) {
  1088. return -EINVAL;
  1089. }
  1090. base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
  1091. if (!base) {
  1092. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
  1093. "supported\n", ioc->name));
  1094. return -EINVAL;
  1095. }
  1096. /* get msix vector count */
  1097. /* NUMA_IO not supported for older controllers */
  1098. if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
  1099. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
  1100. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
  1101. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
  1102. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
  1103. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
  1104. ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
  1105. ioc->msix_vector_count = 1;
  1106. else {
  1107. pci_read_config_word(ioc->pdev, base + 2, &message_control);
  1108. ioc->msix_vector_count = (message_control & 0x3FF) + 1;
  1109. }
  1110. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
  1111. "vector_count(%d)\n", ioc->name, ioc->msix_vector_count));
  1112. return 0;
  1113. }
  1114. /**
  1115. * _base_free_irq - free irq
  1116. * @ioc: per adapter object
  1117. *
  1118. * Freeing respective reply_queue from the list.
  1119. */
  1120. static void
  1121. _base_free_irq(struct MPT2SAS_ADAPTER *ioc)
  1122. {
  1123. struct adapter_reply_queue *reply_q, *next;
  1124. if (list_empty(&ioc->reply_queue_list))
  1125. return;
  1126. list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
  1127. list_del(&reply_q->list);
  1128. synchronize_irq(reply_q->vector);
  1129. free_irq(reply_q->vector, reply_q);
  1130. kfree(reply_q);
  1131. }
  1132. }
  1133. /**
  1134. * _base_request_irq - request irq
  1135. * @ioc: per adapter object
  1136. * @index: msix index into vector table
  1137. * @vector: irq vector
  1138. *
  1139. * Inserting respective reply_queue into the list.
  1140. */
  1141. static int
  1142. _base_request_irq(struct MPT2SAS_ADAPTER *ioc, u8 index, u32 vector)
  1143. {
  1144. struct adapter_reply_queue *reply_q;
  1145. int r;
  1146. reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
  1147. if (!reply_q) {
  1148. printk(MPT2SAS_ERR_FMT "unable to allocate memory %d!\n",
  1149. ioc->name, (int)sizeof(struct adapter_reply_queue));
  1150. return -ENOMEM;
  1151. }
  1152. reply_q->ioc = ioc;
  1153. reply_q->msix_index = index;
  1154. reply_q->vector = vector;
  1155. atomic_set(&reply_q->busy, 0);
  1156. if (ioc->msix_enable)
  1157. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
  1158. MPT2SAS_DRIVER_NAME, ioc->id, index);
  1159. else
  1160. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
  1161. MPT2SAS_DRIVER_NAME, ioc->id);
  1162. r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
  1163. reply_q);
  1164. if (r) {
  1165. printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
  1166. reply_q->name, vector);
  1167. kfree(reply_q);
  1168. return -EBUSY;
  1169. }
  1170. INIT_LIST_HEAD(&reply_q->list);
  1171. list_add_tail(&reply_q->list, &ioc->reply_queue_list);
  1172. return 0;
  1173. }
  1174. /**
  1175. * _base_assign_reply_queues - assigning msix index for each cpu
  1176. * @ioc: per adapter object
  1177. *
  1178. * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
  1179. *
  1180. * It would nice if we could call irq_set_affinity, however it is not
  1181. * an exported symbol
  1182. */
  1183. static void
  1184. _base_assign_reply_queues(struct MPT2SAS_ADAPTER *ioc)
  1185. {
  1186. struct adapter_reply_queue *reply_q;
  1187. int cpu_id;
  1188. int cpu_grouping, loop, grouping, grouping_mod;
  1189. if (!_base_is_controller_msix_enabled(ioc))
  1190. return;
  1191. memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
  1192. /* when there are more cpus than available msix vectors,
  1193. * then group cpus togeather on same irq
  1194. */
  1195. if (ioc->cpu_count > ioc->msix_vector_count) {
  1196. grouping = ioc->cpu_count / ioc->msix_vector_count;
  1197. grouping_mod = ioc->cpu_count % ioc->msix_vector_count;
  1198. if (grouping < 2 || (grouping == 2 && !grouping_mod))
  1199. cpu_grouping = 2;
  1200. else if (grouping < 4 || (grouping == 4 && !grouping_mod))
  1201. cpu_grouping = 4;
  1202. else if (grouping < 8 || (grouping == 8 && !grouping_mod))
  1203. cpu_grouping = 8;
  1204. else
  1205. cpu_grouping = 16;
  1206. } else
  1207. cpu_grouping = 0;
  1208. loop = 0;
  1209. reply_q = list_entry(ioc->reply_queue_list.next,
  1210. struct adapter_reply_queue, list);
  1211. for_each_online_cpu(cpu_id) {
  1212. if (!cpu_grouping) {
  1213. ioc->cpu_msix_table[cpu_id] = reply_q->msix_index;
  1214. reply_q = list_entry(reply_q->list.next,
  1215. struct adapter_reply_queue, list);
  1216. } else {
  1217. if (loop < cpu_grouping) {
  1218. ioc->cpu_msix_table[cpu_id] =
  1219. reply_q->msix_index;
  1220. loop++;
  1221. } else {
  1222. reply_q = list_entry(reply_q->list.next,
  1223. struct adapter_reply_queue, list);
  1224. ioc->cpu_msix_table[cpu_id] =
  1225. reply_q->msix_index;
  1226. loop = 1;
  1227. }
  1228. }
  1229. }
  1230. }
  1231. /**
  1232. * _base_disable_msix - disables msix
  1233. * @ioc: per adapter object
  1234. *
  1235. */
  1236. static void
  1237. _base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
  1238. {
  1239. if (ioc->msix_enable) {
  1240. pci_disable_msix(ioc->pdev);
  1241. ioc->msix_enable = 0;
  1242. }
  1243. }
  1244. /**
  1245. * _base_enable_msix - enables msix, failback to io_apic
  1246. * @ioc: per adapter object
  1247. *
  1248. */
  1249. static int
  1250. _base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
  1251. {
  1252. struct msix_entry *entries, *a;
  1253. int r;
  1254. int i;
  1255. u8 try_msix = 0;
  1256. INIT_LIST_HEAD(&ioc->reply_queue_list);
  1257. if (msix_disable == -1 || msix_disable == 0)
  1258. try_msix = 1;
  1259. if (!try_msix)
  1260. goto try_ioapic;
  1261. if (_base_check_enable_msix(ioc) != 0)
  1262. goto try_ioapic;
  1263. ioc->reply_queue_count = min_t(int, ioc->cpu_count,
  1264. ioc->msix_vector_count);
  1265. entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
  1266. GFP_KERNEL);
  1267. if (!entries) {
  1268. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "kcalloc "
  1269. "failed @ at %s:%d/%s() !!!\n", ioc->name, __FILE__,
  1270. __LINE__, __func__));
  1271. goto try_ioapic;
  1272. }
  1273. for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
  1274. a->entry = i;
  1275. r = pci_enable_msix(ioc->pdev, entries, ioc->reply_queue_count);
  1276. if (r) {
  1277. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "pci_enable_msix "
  1278. "failed (r=%d) !!!\n", ioc->name, r));
  1279. kfree(entries);
  1280. goto try_ioapic;
  1281. }
  1282. ioc->msix_enable = 1;
  1283. for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
  1284. r = _base_request_irq(ioc, i, a->vector);
  1285. if (r) {
  1286. _base_free_irq(ioc);
  1287. _base_disable_msix(ioc);
  1288. kfree(entries);
  1289. goto try_ioapic;
  1290. }
  1291. }
  1292. kfree(entries);
  1293. return 0;
  1294. /* failback to io_apic interrupt routing */
  1295. try_ioapic:
  1296. r = _base_request_irq(ioc, 0, ioc->pdev->irq);
  1297. return r;
  1298. }
  1299. /**
  1300. * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
  1301. * @ioc: per adapter object
  1302. *
  1303. * Returns 0 for success, non-zero for failure.
  1304. */
  1305. int
  1306. mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
  1307. {
  1308. struct pci_dev *pdev = ioc->pdev;
  1309. u32 memap_sz;
  1310. u32 pio_sz;
  1311. int i, r = 0;
  1312. u64 pio_chip = 0;
  1313. u64 chip_phys = 0;
  1314. struct adapter_reply_queue *reply_q;
  1315. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n",
  1316. ioc->name, __func__));
  1317. ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
  1318. if (pci_enable_device_mem(pdev)) {
  1319. printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
  1320. "failed\n", ioc->name);
  1321. return -ENODEV;
  1322. }
  1323. if (pci_request_selected_regions(pdev, ioc->bars,
  1324. MPT2SAS_DRIVER_NAME)) {
  1325. printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
  1326. "failed\n", ioc->name);
  1327. r = -ENODEV;
  1328. goto out_fail;
  1329. }
  1330. /* AER (Advanced Error Reporting) hooks */
  1331. pci_enable_pcie_error_reporting(pdev);
  1332. pci_set_master(pdev);
  1333. if (_base_config_dma_addressing(ioc, pdev) != 0) {
  1334. printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
  1335. ioc->name, pci_name(pdev));
  1336. r = -ENODEV;
  1337. goto out_fail;
  1338. }
  1339. for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
  1340. if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
  1341. if (pio_sz)
  1342. continue;
  1343. pio_chip = (u64)pci_resource_start(pdev, i);
  1344. pio_sz = pci_resource_len(pdev, i);
  1345. } else {
  1346. if (memap_sz)
  1347. continue;
  1348. /* verify memory resource is valid before using */
  1349. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  1350. ioc->chip_phys = pci_resource_start(pdev, i);
  1351. chip_phys = (u64)ioc->chip_phys;
  1352. memap_sz = pci_resource_len(pdev, i);
  1353. ioc->chip = ioremap(ioc->chip_phys, memap_sz);
  1354. if (ioc->chip == NULL) {
  1355. printk(MPT2SAS_ERR_FMT "unable to map "
  1356. "adapter memory!\n", ioc->name);
  1357. r = -EINVAL;
  1358. goto out_fail;
  1359. }
  1360. }
  1361. }
  1362. }
  1363. _base_mask_interrupts(ioc);
  1364. r = _base_enable_msix(ioc);
  1365. if (r)
  1366. goto out_fail;
  1367. list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
  1368. printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
  1369. reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
  1370. "IO-APIC enabled"), reply_q->vector);
  1371. printk(MPT2SAS_INFO_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
  1372. ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
  1373. printk(MPT2SAS_INFO_FMT "ioport(0x%016llx), size(%d)\n",
  1374. ioc->name, (unsigned long long)pio_chip, pio_sz);
  1375. /* Save PCI configuration state for recovery from PCI AER/EEH errors */
  1376. pci_save_state(pdev);
  1377. return 0;
  1378. out_fail:
  1379. if (ioc->chip_phys)
  1380. iounmap(ioc->chip);
  1381. ioc->chip_phys = 0;
  1382. pci_release_selected_regions(ioc->pdev, ioc->bars);
  1383. pci_disable_pcie_error_reporting(pdev);
  1384. pci_disable_device(pdev);
  1385. return r;
  1386. }
  1387. /**
  1388. * mpt2sas_base_get_msg_frame - obtain request mf pointer
  1389. * @ioc: per adapter object
  1390. * @smid: system request message index(smid zero is invalid)
  1391. *
  1392. * Returns virt pointer to message frame.
  1393. */
  1394. void *
  1395. mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1396. {
  1397. return (void *)(ioc->request + (smid * ioc->request_sz));
  1398. }
  1399. /**
  1400. * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
  1401. * @ioc: per adapter object
  1402. * @smid: system request message index
  1403. *
  1404. * Returns virt pointer to sense buffer.
  1405. */
  1406. void *
  1407. mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1408. {
  1409. return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
  1410. }
  1411. /**
  1412. * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
  1413. * @ioc: per adapter object
  1414. * @smid: system request message index
  1415. *
  1416. * Returns phys pointer to the low 32bit address of the sense buffer.
  1417. */
  1418. __le32
  1419. mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1420. {
  1421. return cpu_to_le32(ioc->sense_dma +
  1422. ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
  1423. }
  1424. /**
  1425. * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
  1426. * @ioc: per adapter object
  1427. * @phys_addr: lower 32 physical addr of the reply
  1428. *
  1429. * Converts 32bit lower physical addr into a virt address.
  1430. */
  1431. void *
  1432. mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
  1433. {
  1434. if (!phys_addr)
  1435. return NULL;
  1436. return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
  1437. }
  1438. /**
  1439. * mpt2sas_base_get_smid - obtain a free smid from internal queue
  1440. * @ioc: per adapter object
  1441. * @cb_idx: callback index
  1442. *
  1443. * Returns smid (zero is invalid)
  1444. */
  1445. u16
  1446. mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
  1447. {
  1448. unsigned long flags;
  1449. struct request_tracker *request;
  1450. u16 smid;
  1451. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1452. if (list_empty(&ioc->internal_free_list)) {
  1453. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1454. printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
  1455. ioc->name, __func__);
  1456. return 0;
  1457. }
  1458. request = list_entry(ioc->internal_free_list.next,
  1459. struct request_tracker, tracker_list);
  1460. request->cb_idx = cb_idx;
  1461. smid = request->smid;
  1462. list_del(&request->tracker_list);
  1463. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1464. return smid;
  1465. }
  1466. /**
  1467. * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
  1468. * @ioc: per adapter object
  1469. * @cb_idx: callback index
  1470. * @scmd: pointer to scsi command object
  1471. *
  1472. * Returns smid (zero is invalid)
  1473. */
  1474. u16
  1475. mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
  1476. struct scsi_cmnd *scmd)
  1477. {
  1478. unsigned long flags;
  1479. struct scsiio_tracker *request;
  1480. u16 smid;
  1481. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1482. if (list_empty(&ioc->free_list)) {
  1483. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1484. printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
  1485. ioc->name, __func__);
  1486. return 0;
  1487. }
  1488. request = list_entry(ioc->free_list.next,
  1489. struct scsiio_tracker, tracker_list);
  1490. request->scmd = scmd;
  1491. request->cb_idx = cb_idx;
  1492. smid = request->smid;
  1493. list_del(&request->tracker_list);
  1494. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1495. return smid;
  1496. }
  1497. /**
  1498. * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
  1499. * @ioc: per adapter object
  1500. * @cb_idx: callback index
  1501. *
  1502. * Returns smid (zero is invalid)
  1503. */
  1504. u16
  1505. mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
  1506. {
  1507. unsigned long flags;
  1508. struct request_tracker *request;
  1509. u16 smid;
  1510. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1511. if (list_empty(&ioc->hpr_free_list)) {
  1512. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1513. return 0;
  1514. }
  1515. request = list_entry(ioc->hpr_free_list.next,
  1516. struct request_tracker, tracker_list);
  1517. request->cb_idx = cb_idx;
  1518. smid = request->smid;
  1519. list_del(&request->tracker_list);
  1520. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1521. return smid;
  1522. }
  1523. /**
  1524. * mpt2sas_base_free_smid - put smid back on free_list
  1525. * @ioc: per adapter object
  1526. * @smid: system request message index
  1527. *
  1528. * Return nothing.
  1529. */
  1530. void
  1531. mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1532. {
  1533. unsigned long flags;
  1534. int i;
  1535. struct chain_tracker *chain_req, *next;
  1536. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1537. if (smid < ioc->hi_priority_smid) {
  1538. /* scsiio queue */
  1539. i = smid - 1;
  1540. if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
  1541. list_for_each_entry_safe(chain_req, next,
  1542. &ioc->scsi_lookup[i].chain_list, tracker_list) {
  1543. list_del_init(&chain_req->tracker_list);
  1544. list_add_tail(&chain_req->tracker_list,
  1545. &ioc->free_chain_list);
  1546. }
  1547. }
  1548. ioc->scsi_lookup[i].cb_idx = 0xFF;
  1549. ioc->scsi_lookup[i].scmd = NULL;
  1550. ioc->scsi_lookup[i].direct_io = 0;
  1551. list_add_tail(&ioc->scsi_lookup[i].tracker_list,
  1552. &ioc->free_list);
  1553. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1554. /*
  1555. * See _wait_for_commands_to_complete() call with regards
  1556. * to this code.
  1557. */
  1558. if (ioc->shost_recovery && ioc->pending_io_count) {
  1559. if (ioc->pending_io_count == 1)
  1560. wake_up(&ioc->reset_wq);
  1561. ioc->pending_io_count--;
  1562. }
  1563. return;
  1564. } else if (smid < ioc->internal_smid) {
  1565. /* hi-priority */
  1566. i = smid - ioc->hi_priority_smid;
  1567. ioc->hpr_lookup[i].cb_idx = 0xFF;
  1568. list_add_tail(&ioc->hpr_lookup[i].tracker_list,
  1569. &ioc->hpr_free_list);
  1570. } else if (smid <= ioc->hba_queue_depth) {
  1571. /* internal queue */
  1572. i = smid - ioc->internal_smid;
  1573. ioc->internal_lookup[i].cb_idx = 0xFF;
  1574. list_add_tail(&ioc->internal_lookup[i].tracker_list,
  1575. &ioc->internal_free_list);
  1576. }
  1577. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1578. }
  1579. /**
  1580. * _base_writeq - 64 bit write to MMIO
  1581. * @ioc: per adapter object
  1582. * @b: data payload
  1583. * @addr: address in MMIO space
  1584. * @writeq_lock: spin lock
  1585. *
  1586. * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
  1587. * care of 32 bit environment where its not quarenteed to send the entire word
  1588. * in one transfer.
  1589. */
  1590. #ifndef writeq
  1591. static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
  1592. spinlock_t *writeq_lock)
  1593. {
  1594. unsigned long flags;
  1595. __u64 data_out = cpu_to_le64(b);
  1596. spin_lock_irqsave(writeq_lock, flags);
  1597. writel((u32)(data_out), addr);
  1598. writel((u32)(data_out >> 32), (addr + 4));
  1599. spin_unlock_irqrestore(writeq_lock, flags);
  1600. }
  1601. #else
  1602. static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
  1603. spinlock_t *writeq_lock)
  1604. {
  1605. writeq(cpu_to_le64(b), addr);
  1606. }
  1607. #endif
  1608. static inline u8
  1609. _base_get_msix_index(struct MPT2SAS_ADAPTER *ioc)
  1610. {
  1611. return ioc->cpu_msix_table[raw_smp_processor_id()];
  1612. }
  1613. /**
  1614. * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
  1615. * @ioc: per adapter object
  1616. * @smid: system request message index
  1617. * @handle: device handle
  1618. *
  1619. * Return nothing.
  1620. */
  1621. void
  1622. mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
  1623. {
  1624. Mpi2RequestDescriptorUnion_t descriptor;
  1625. u64 *request = (u64 *)&descriptor;
  1626. descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
  1627. descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
  1628. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  1629. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  1630. descriptor.SCSIIO.LMID = 0;
  1631. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1632. &ioc->scsi_lookup_lock);
  1633. }
  1634. /**
  1635. * mpt2sas_base_put_smid_hi_priority - send Task Management request to firmware
  1636. * @ioc: per adapter object
  1637. * @smid: system request message index
  1638. *
  1639. * Return nothing.
  1640. */
  1641. void
  1642. mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1643. {
  1644. Mpi2RequestDescriptorUnion_t descriptor;
  1645. u64 *request = (u64 *)&descriptor;
  1646. descriptor.HighPriority.RequestFlags =
  1647. MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
  1648. descriptor.HighPriority.MSIxIndex = 0;
  1649. descriptor.HighPriority.SMID = cpu_to_le16(smid);
  1650. descriptor.HighPriority.LMID = 0;
  1651. descriptor.HighPriority.Reserved1 = 0;
  1652. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1653. &ioc->scsi_lookup_lock);
  1654. }
  1655. /**
  1656. * mpt2sas_base_put_smid_default - Default, primarily used for config pages
  1657. * @ioc: per adapter object
  1658. * @smid: system request message index
  1659. *
  1660. * Return nothing.
  1661. */
  1662. void
  1663. mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1664. {
  1665. Mpi2RequestDescriptorUnion_t descriptor;
  1666. u64 *request = (u64 *)&descriptor;
  1667. descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
  1668. descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
  1669. descriptor.Default.SMID = cpu_to_le16(smid);
  1670. descriptor.Default.LMID = 0;
  1671. descriptor.Default.DescriptorTypeDependent = 0;
  1672. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1673. &ioc->scsi_lookup_lock);
  1674. }
  1675. /**
  1676. * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
  1677. * @ioc: per adapter object
  1678. * @smid: system request message index
  1679. * @io_index: value used to track the IO
  1680. *
  1681. * Return nothing.
  1682. */
  1683. void
  1684. mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
  1685. u16 io_index)
  1686. {
  1687. Mpi2RequestDescriptorUnion_t descriptor;
  1688. u64 *request = (u64 *)&descriptor;
  1689. descriptor.SCSITarget.RequestFlags =
  1690. MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
  1691. descriptor.SCSITarget.MSIxIndex = _base_get_msix_index(ioc);
  1692. descriptor.SCSITarget.SMID = cpu_to_le16(smid);
  1693. descriptor.SCSITarget.LMID = 0;
  1694. descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
  1695. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1696. &ioc->scsi_lookup_lock);
  1697. }
  1698. /**
  1699. * _base_display_dell_branding - Disply branding string
  1700. * @ioc: per adapter object
  1701. *
  1702. * Return nothing.
  1703. */
  1704. static void
  1705. _base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
  1706. {
  1707. char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
  1708. if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
  1709. return;
  1710. memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
  1711. switch (ioc->pdev->subsystem_device) {
  1712. case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
  1713. strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
  1714. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1715. break;
  1716. case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
  1717. strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
  1718. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1719. break;
  1720. case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
  1721. strncpy(dell_branding,
  1722. MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
  1723. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1724. break;
  1725. case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
  1726. strncpy(dell_branding,
  1727. MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
  1728. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1729. break;
  1730. case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
  1731. strncpy(dell_branding,
  1732. MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
  1733. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1734. break;
  1735. case MPT2SAS_DELL_PERC_H200_SSDID:
  1736. strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
  1737. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1738. break;
  1739. case MPT2SAS_DELL_6GBPS_SAS_SSDID:
  1740. strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
  1741. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1742. break;
  1743. default:
  1744. sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
  1745. break;
  1746. }
  1747. printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
  1748. " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
  1749. ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
  1750. ioc->pdev->subsystem_device);
  1751. }
  1752. /**
  1753. * _base_display_intel_branding - Display branding string
  1754. * @ioc: per adapter object
  1755. *
  1756. * Return nothing.
  1757. */
  1758. static void
  1759. _base_display_intel_branding(struct MPT2SAS_ADAPTER *ioc)
  1760. {
  1761. if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
  1762. return;
  1763. switch (ioc->pdev->device) {
  1764. case MPI2_MFGPAGE_DEVID_SAS2008:
  1765. switch (ioc->pdev->subsystem_device) {
  1766. case MPT2SAS_INTEL_RMS2LL080_SSDID:
  1767. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1768. MPT2SAS_INTEL_RMS2LL080_BRANDING);
  1769. break;
  1770. case MPT2SAS_INTEL_RMS2LL040_SSDID:
  1771. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1772. MPT2SAS_INTEL_RMS2LL040_BRANDING);
  1773. break;
  1774. case MPT2SAS_INTEL_RAMSDALE_SSDID:
  1775. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1776. MPT2SAS_INTEL_RAMSDALE_BRANDING);
  1777. break;
  1778. default:
  1779. break;
  1780. }
  1781. case MPI2_MFGPAGE_DEVID_SAS2308_2:
  1782. switch (ioc->pdev->subsystem_device) {
  1783. case MPT2SAS_INTEL_RS25GB008_SSDID:
  1784. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1785. MPT2SAS_INTEL_RS25GB008_BRANDING);
  1786. break;
  1787. case MPT2SAS_INTEL_RMS25JB080_SSDID:
  1788. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1789. MPT2SAS_INTEL_RMS25JB080_BRANDING);
  1790. break;
  1791. case MPT2SAS_INTEL_RMS25JB040_SSDID:
  1792. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1793. MPT2SAS_INTEL_RMS25JB040_BRANDING);
  1794. break;
  1795. case MPT2SAS_INTEL_RMS25KB080_SSDID:
  1796. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1797. MPT2SAS_INTEL_RMS25KB080_BRANDING);
  1798. break;
  1799. case MPT2SAS_INTEL_RMS25KB040_SSDID:
  1800. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1801. MPT2SAS_INTEL_RMS25KB040_BRANDING);
  1802. break;
  1803. default:
  1804. break;
  1805. }
  1806. default:
  1807. break;
  1808. }
  1809. }
  1810. /**
  1811. * _base_display_hp_branding - Display branding string
  1812. * @ioc: per adapter object
  1813. *
  1814. * Return nothing.
  1815. */
  1816. static void
  1817. _base_display_hp_branding(struct MPT2SAS_ADAPTER *ioc)
  1818. {
  1819. if (ioc->pdev->subsystem_vendor != MPT2SAS_HP_3PAR_SSVID)
  1820. return;
  1821. switch (ioc->pdev->device) {
  1822. case MPI2_MFGPAGE_DEVID_SAS2004:
  1823. switch (ioc->pdev->subsystem_device) {
  1824. case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
  1825. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1826. MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
  1827. break;
  1828. default:
  1829. break;
  1830. }
  1831. case MPI2_MFGPAGE_DEVID_SAS2308_2:
  1832. switch (ioc->pdev->subsystem_device) {
  1833. case MPT2SAS_HP_2_4_INTERNAL_SSDID:
  1834. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1835. MPT2SAS_HP_2_4_INTERNAL_BRANDING);
  1836. break;
  1837. case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
  1838. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1839. MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
  1840. break;
  1841. case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
  1842. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1843. MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
  1844. break;
  1845. case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
  1846. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1847. MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
  1848. break;
  1849. default:
  1850. break;
  1851. }
  1852. default:
  1853. break;
  1854. }
  1855. }
  1856. /**
  1857. * _base_display_ioc_capabilities - Disply IOC's capabilities.
  1858. * @ioc: per adapter object
  1859. *
  1860. * Return nothing.
  1861. */
  1862. static void
  1863. _base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
  1864. {
  1865. int i = 0;
  1866. char desc[16];
  1867. u32 iounit_pg1_flags;
  1868. u32 bios_version;
  1869. bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  1870. strncpy(desc, ioc->manu_pg0.ChipName, 16);
  1871. printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
  1872. "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
  1873. ioc->name, desc,
  1874. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  1875. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  1876. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  1877. ioc->facts.FWVersion.Word & 0x000000FF,
  1878. ioc->pdev->revision,
  1879. (bios_version & 0xFF000000) >> 24,
  1880. (bios_version & 0x00FF0000) >> 16,
  1881. (bios_version & 0x0000FF00) >> 8,
  1882. bios_version & 0x000000FF);
  1883. _base_display_dell_branding(ioc);
  1884. _base_display_intel_branding(ioc);
  1885. _base_display_hp_branding(ioc);
  1886. printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
  1887. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
  1888. printk("Initiator");
  1889. i++;
  1890. }
  1891. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
  1892. printk("%sTarget", i ? "," : "");
  1893. i++;
  1894. }
  1895. i = 0;
  1896. printk("), ");
  1897. printk("Capabilities=(");
  1898. if (!ioc->hide_ir_msg) {
  1899. if (ioc->facts.IOCCapabilities &
  1900. MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
  1901. printk("Raid");
  1902. i++;
  1903. }
  1904. }
  1905. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
  1906. printk("%sTLR", i ? "," : "");
  1907. i++;
  1908. }
  1909. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
  1910. printk("%sMulticast", i ? "," : "");
  1911. i++;
  1912. }
  1913. if (ioc->facts.IOCCapabilities &
  1914. MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
  1915. printk("%sBIDI Target", i ? "," : "");
  1916. i++;
  1917. }
  1918. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
  1919. printk("%sEEDP", i ? "," : "");
  1920. i++;
  1921. }
  1922. if (ioc->facts.IOCCapabilities &
  1923. MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
  1924. printk("%sSnapshot Buffer", i ? "," : "");
  1925. i++;
  1926. }
  1927. if (ioc->facts.IOCCapabilities &
  1928. MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
  1929. printk("%sDiag Trace Buffer", i ? "," : "");
  1930. i++;
  1931. }
  1932. if (ioc->facts.IOCCapabilities &
  1933. MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
  1934. printk(KERN_INFO "%sDiag Extended Buffer", i ? "," : "");
  1935. i++;
  1936. }
  1937. if (ioc->facts.IOCCapabilities &
  1938. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
  1939. printk("%sTask Set Full", i ? "," : "");
  1940. i++;
  1941. }
  1942. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  1943. if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
  1944. printk("%sNCQ", i ? "," : "");
  1945. i++;
  1946. }
  1947. printk(")\n");
  1948. }
  1949. /**
  1950. * _base_update_missing_delay - change the missing delay timers
  1951. * @ioc: per adapter object
  1952. * @device_missing_delay: amount of time till device is reported missing
  1953. * @io_missing_delay: interval IO is returned when there is a missing device
  1954. *
  1955. * Return nothing.
  1956. *
  1957. * Passed on the command line, this function will modify the device missing
  1958. * delay, as well as the io missing delay. This should be called at driver
  1959. * load time.
  1960. */
  1961. static void
  1962. _base_update_missing_delay(struct MPT2SAS_ADAPTER *ioc,
  1963. u16 device_missing_delay, u8 io_missing_delay)
  1964. {
  1965. u16 dmd, dmd_new, dmd_orignal;
  1966. u8 io_missing_delay_original;
  1967. u16 sz;
  1968. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1969. Mpi2ConfigReply_t mpi_reply;
  1970. u8 num_phys = 0;
  1971. u16 ioc_status;
  1972. mpt2sas_config_get_number_hba_phys(ioc, &num_phys);
  1973. if (!num_phys)
  1974. return;
  1975. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
  1976. sizeof(Mpi2SasIOUnit1PhyData_t));
  1977. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1978. if (!sas_iounit_pg1) {
  1979. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1980. ioc->name, __FILE__, __LINE__, __func__);
  1981. goto out;
  1982. }
  1983. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1984. sas_iounit_pg1, sz))) {
  1985. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1986. ioc->name, __FILE__, __LINE__, __func__);
  1987. goto out;
  1988. }
  1989. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1990. MPI2_IOCSTATUS_MASK;
  1991. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1992. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1993. ioc->name, __FILE__, __LINE__, __func__);
  1994. goto out;
  1995. }
  1996. /* device missing delay */
  1997. dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
  1998. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  1999. dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  2000. else
  2001. dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2002. dmd_orignal = dmd;
  2003. if (device_missing_delay > 0x7F) {
  2004. dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
  2005. device_missing_delay;
  2006. dmd = dmd / 16;
  2007. dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
  2008. } else
  2009. dmd = device_missing_delay;
  2010. sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
  2011. /* io missing delay */
  2012. io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
  2013. sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
  2014. if (!mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  2015. sz)) {
  2016. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  2017. dmd_new = (dmd &
  2018. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  2019. else
  2020. dmd_new =
  2021. dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2022. printk(MPT2SAS_INFO_FMT "device_missing_delay: old(%d), "
  2023. "new(%d)\n", ioc->name, dmd_orignal, dmd_new);
  2024. printk(MPT2SAS_INFO_FMT "ioc_missing_delay: old(%d), "
  2025. "new(%d)\n", ioc->name, io_missing_delay_original,
  2026. io_missing_delay);
  2027. ioc->device_missing_delay = dmd_new;
  2028. ioc->io_missing_delay = io_missing_delay;
  2029. }
  2030. out:
  2031. kfree(sas_iounit_pg1);
  2032. }
  2033. /**
  2034. * _base_static_config_pages - static start of day config pages
  2035. * @ioc: per adapter object
  2036. *
  2037. * Return nothing.
  2038. */
  2039. static void
  2040. _base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
  2041. {
  2042. Mpi2ConfigReply_t mpi_reply;
  2043. u32 iounit_pg1_flags;
  2044. mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
  2045. if (ioc->ir_firmware)
  2046. mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
  2047. &ioc->manu_pg10);
  2048. mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
  2049. mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
  2050. mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
  2051. mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
  2052. mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  2053. _base_display_ioc_capabilities(ioc);
  2054. /*
  2055. * Enable task_set_full handling in iounit_pg1 when the
  2056. * facts capabilities indicate that its supported.
  2057. */
  2058. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  2059. if ((ioc->facts.IOCCapabilities &
  2060. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
  2061. iounit_pg1_flags &=
  2062. ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  2063. else
  2064. iounit_pg1_flags |=
  2065. MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  2066. ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
  2067. mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  2068. }
  2069. /**
  2070. * _base_release_memory_pools - release memory
  2071. * @ioc: per adapter object
  2072. *
  2073. * Free memory allocated from _base_allocate_memory_pools.
  2074. *
  2075. * Return nothing.
  2076. */
  2077. static void
  2078. _base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
  2079. {
  2080. int i;
  2081. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  2082. __func__));
  2083. if (ioc->request) {
  2084. pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
  2085. ioc->request, ioc->request_dma);
  2086. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
  2087. ": free\n", ioc->name, ioc->request));
  2088. ioc->request = NULL;
  2089. }
  2090. if (ioc->sense) {
  2091. pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
  2092. if (ioc->sense_dma_pool)
  2093. pci_pool_destroy(ioc->sense_dma_pool);
  2094. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
  2095. ": free\n", ioc->name, ioc->sense));
  2096. ioc->sense = NULL;
  2097. }
  2098. if (ioc->reply) {
  2099. pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
  2100. if (ioc->reply_dma_pool)
  2101. pci_pool_destroy(ioc->reply_dma_pool);
  2102. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
  2103. ": free\n", ioc->name, ioc->reply));
  2104. ioc->reply = NULL;
  2105. }
  2106. if (ioc->reply_free) {
  2107. pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
  2108. ioc->reply_free_dma);
  2109. if (ioc->reply_free_dma_pool)
  2110. pci_pool_destroy(ioc->reply_free_dma_pool);
  2111. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
  2112. "(0x%p): free\n", ioc->name, ioc->reply_free));
  2113. ioc->reply_free = NULL;
  2114. }
  2115. if (ioc->reply_post_free) {
  2116. pci_pool_free(ioc->reply_post_free_dma_pool,
  2117. ioc->reply_post_free, ioc->reply_post_free_dma);
  2118. if (ioc->reply_post_free_dma_pool)
  2119. pci_pool_destroy(ioc->reply_post_free_dma_pool);
  2120. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
  2121. "reply_post_free_pool(0x%p): free\n", ioc->name,
  2122. ioc->reply_post_free));
  2123. ioc->reply_post_free = NULL;
  2124. }
  2125. if (ioc->config_page) {
  2126. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
  2127. "config_page(0x%p): free\n", ioc->name,
  2128. ioc->config_page));
  2129. pci_free_consistent(ioc->pdev, ioc->config_page_sz,
  2130. ioc->config_page, ioc->config_page_dma);
  2131. }
  2132. if (ioc->scsi_lookup) {
  2133. free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
  2134. ioc->scsi_lookup = NULL;
  2135. }
  2136. kfree(ioc->hpr_lookup);
  2137. kfree(ioc->internal_lookup);
  2138. if (ioc->chain_lookup) {
  2139. for (i = 0; i < ioc->chain_depth; i++) {
  2140. if (ioc->chain_lookup[i].chain_buffer)
  2141. pci_pool_free(ioc->chain_dma_pool,
  2142. ioc->chain_lookup[i].chain_buffer,
  2143. ioc->chain_lookup[i].chain_buffer_dma);
  2144. }
  2145. if (ioc->chain_dma_pool)
  2146. pci_pool_destroy(ioc->chain_dma_pool);
  2147. free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
  2148. ioc->chain_lookup = NULL;
  2149. }
  2150. }
  2151. /**
  2152. * _base_allocate_memory_pools - allocate start of day memory pools
  2153. * @ioc: per adapter object
  2154. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2155. *
  2156. * Returns 0 success, anything else error
  2157. */
  2158. static int
  2159. _base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  2160. {
  2161. struct mpt2sas_facts *facts;
  2162. u16 max_sge_elements;
  2163. u16 chains_needed_per_io;
  2164. u32 sz, total_sz, reply_post_free_sz;
  2165. u32 retry_sz;
  2166. u16 max_request_credit;
  2167. int i;
  2168. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  2169. __func__));
  2170. retry_sz = 0;
  2171. facts = &ioc->facts;
  2172. /* command line tunables for max sgl entries */
  2173. if (max_sgl_entries != -1) {
  2174. ioc->shost->sg_tablesize = (max_sgl_entries <
  2175. MPT2SAS_SG_DEPTH) ? max_sgl_entries :
  2176. MPT2SAS_SG_DEPTH;
  2177. } else {
  2178. ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
  2179. }
  2180. /* command line tunables for max controller queue depth */
  2181. if (max_queue_depth != -1 && max_queue_depth != 0) {
  2182. max_request_credit = min_t(u16, max_queue_depth +
  2183. ioc->hi_priority_depth + ioc->internal_depth,
  2184. facts->RequestCredit);
  2185. if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
  2186. max_request_credit = MAX_HBA_QUEUE_DEPTH;
  2187. } else
  2188. max_request_credit = min_t(u16, facts->RequestCredit,
  2189. MAX_HBA_QUEUE_DEPTH);
  2190. ioc->hba_queue_depth = max_request_credit;
  2191. ioc->hi_priority_depth = facts->HighPriorityCredit;
  2192. ioc->internal_depth = ioc->hi_priority_depth + 5;
  2193. /* request frame size */
  2194. ioc->request_sz = facts->IOCRequestFrameSize * 4;
  2195. /* reply frame size */
  2196. ioc->reply_sz = facts->ReplyFrameSize * 4;
  2197. retry_allocation:
  2198. total_sz = 0;
  2199. /* calculate number of sg elements left over in the 1st frame */
  2200. max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
  2201. sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
  2202. ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
  2203. /* now do the same for a chain buffer */
  2204. max_sge_elements = ioc->request_sz - ioc->sge_size;
  2205. ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
  2206. ioc->chain_offset_value_for_main_message =
  2207. ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
  2208. (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
  2209. /*
  2210. * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
  2211. */
  2212. chains_needed_per_io = ((ioc->shost->sg_tablesize -
  2213. ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
  2214. + 1;
  2215. if (chains_needed_per_io > facts->MaxChainDepth) {
  2216. chains_needed_per_io = facts->MaxChainDepth;
  2217. ioc->shost->sg_tablesize = min_t(u16,
  2218. ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
  2219. * chains_needed_per_io), ioc->shost->sg_tablesize);
  2220. }
  2221. ioc->chains_needed_per_io = chains_needed_per_io;
  2222. /* reply free queue sizing - taking into account for 64 FW events */
  2223. ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
  2224. /* align the reply post queue on the next 16 count boundary */
  2225. if (!ioc->reply_free_queue_depth % 16)
  2226. ioc->reply_post_queue_depth = ioc->reply_free_queue_depth + 16;
  2227. else
  2228. ioc->reply_post_queue_depth = ioc->reply_free_queue_depth +
  2229. 32 - (ioc->reply_free_queue_depth % 16);
  2230. if (ioc->reply_post_queue_depth >
  2231. facts->MaxReplyDescriptorPostQueueDepth) {
  2232. ioc->reply_post_queue_depth = min_t(u16,
  2233. (facts->MaxReplyDescriptorPostQueueDepth -
  2234. (facts->MaxReplyDescriptorPostQueueDepth % 16)),
  2235. (ioc->hba_queue_depth - (ioc->hba_queue_depth % 16)));
  2236. ioc->reply_free_queue_depth = ioc->reply_post_queue_depth - 16;
  2237. ioc->hba_queue_depth = ioc->reply_free_queue_depth - 64;
  2238. }
  2239. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
  2240. "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
  2241. "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
  2242. ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
  2243. ioc->chains_needed_per_io));
  2244. ioc->scsiio_depth = ioc->hba_queue_depth -
  2245. ioc->hi_priority_depth - ioc->internal_depth;
  2246. /* set the scsi host can_queue depth
  2247. * with some internal commands that could be outstanding
  2248. */
  2249. ioc->shost->can_queue = ioc->scsiio_depth;
  2250. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
  2251. "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
  2252. /* contiguous pool for request and chains, 16 byte align, one extra "
  2253. * "frame for smid=0
  2254. */
  2255. ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
  2256. sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
  2257. /* hi-priority queue */
  2258. sz += (ioc->hi_priority_depth * ioc->request_sz);
  2259. /* internal queue */
  2260. sz += (ioc->internal_depth * ioc->request_sz);
  2261. ioc->request_dma_sz = sz;
  2262. ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
  2263. if (!ioc->request) {
  2264. printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
  2265. "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
  2266. "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
  2267. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  2268. if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
  2269. goto out;
  2270. retry_sz += 64;
  2271. ioc->hba_queue_depth = max_request_credit - retry_sz;
  2272. goto retry_allocation;
  2273. }
  2274. if (retry_sz)
  2275. printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
  2276. "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
  2277. "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
  2278. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  2279. /* hi-priority queue */
  2280. ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
  2281. ioc->request_sz);
  2282. ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
  2283. ioc->request_sz);
  2284. /* internal queue */
  2285. ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
  2286. ioc->request_sz);
  2287. ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
  2288. ioc->request_sz);
  2289. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
  2290. "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
  2291. ioc->request, ioc->hba_queue_depth, ioc->request_sz,
  2292. (ioc->hba_queue_depth * ioc->request_sz)/1024));
  2293. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
  2294. ioc->name, (unsigned long long) ioc->request_dma));
  2295. total_sz += sz;
  2296. sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
  2297. ioc->scsi_lookup_pages = get_order(sz);
  2298. ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
  2299. GFP_KERNEL, ioc->scsi_lookup_pages);
  2300. if (!ioc->scsi_lookup) {
  2301. printk(MPT2SAS_ERR_FMT "scsi_lookup: get_free_pages failed, "
  2302. "sz(%d)\n", ioc->name, (int)sz);
  2303. goto out;
  2304. }
  2305. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
  2306. "depth(%d)\n", ioc->name, ioc->request,
  2307. ioc->scsiio_depth));
  2308. ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
  2309. sz = ioc->chain_depth * sizeof(struct chain_tracker);
  2310. ioc->chain_pages = get_order(sz);
  2311. ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
  2312. GFP_KERNEL, ioc->chain_pages);
  2313. if (!ioc->chain_lookup) {
  2314. printk(MPT2SAS_ERR_FMT "chain_lookup: get_free_pages failed, "
  2315. "sz(%d)\n", ioc->name, (int)sz);
  2316. goto out;
  2317. }
  2318. ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
  2319. ioc->request_sz, 16, 0);
  2320. if (!ioc->chain_dma_pool) {
  2321. printk(MPT2SAS_ERR_FMT "chain_dma_pool: pci_pool_create "
  2322. "failed\n", ioc->name);
  2323. goto out;
  2324. }
  2325. for (i = 0; i < ioc->chain_depth; i++) {
  2326. ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
  2327. ioc->chain_dma_pool , GFP_KERNEL,
  2328. &ioc->chain_lookup[i].chain_buffer_dma);
  2329. if (!ioc->chain_lookup[i].chain_buffer) {
  2330. ioc->chain_depth = i;
  2331. goto chain_done;
  2332. }
  2333. total_sz += ioc->request_sz;
  2334. }
  2335. chain_done:
  2336. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool depth"
  2337. "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
  2338. ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
  2339. ioc->request_sz))/1024));
  2340. /* initialize hi-priority queue smid's */
  2341. ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
  2342. sizeof(struct request_tracker), GFP_KERNEL);
  2343. if (!ioc->hpr_lookup) {
  2344. printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
  2345. ioc->name);
  2346. goto out;
  2347. }
  2348. ioc->hi_priority_smid = ioc->scsiio_depth + 1;
  2349. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
  2350. "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
  2351. ioc->hi_priority_depth, ioc->hi_priority_smid));
  2352. /* initialize internal queue smid's */
  2353. ioc->internal_lookup = kcalloc(ioc->internal_depth,
  2354. sizeof(struct request_tracker), GFP_KERNEL);
  2355. if (!ioc->internal_lookup) {
  2356. printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
  2357. ioc->name);
  2358. goto out;
  2359. }
  2360. ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
  2361. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
  2362. "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
  2363. ioc->internal_depth, ioc->internal_smid));
  2364. /* sense buffers, 4 byte align */
  2365. sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
  2366. ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
  2367. 0);
  2368. if (!ioc->sense_dma_pool) {
  2369. printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
  2370. ioc->name);
  2371. goto out;
  2372. }
  2373. ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
  2374. &ioc->sense_dma);
  2375. if (!ioc->sense) {
  2376. printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
  2377. ioc->name);
  2378. goto out;
  2379. }
  2380. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
  2381. "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
  2382. "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
  2383. SCSI_SENSE_BUFFERSIZE, sz/1024));
  2384. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
  2385. ioc->name, (unsigned long long)ioc->sense_dma));
  2386. total_sz += sz;
  2387. /* reply pool, 4 byte align */
  2388. sz = ioc->reply_free_queue_depth * ioc->reply_sz;
  2389. ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
  2390. 0);
  2391. if (!ioc->reply_dma_pool) {
  2392. printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
  2393. ioc->name);
  2394. goto out;
  2395. }
  2396. ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
  2397. &ioc->reply_dma);
  2398. if (!ioc->reply) {
  2399. printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
  2400. ioc->name);
  2401. goto out;
  2402. }
  2403. ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
  2404. ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
  2405. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
  2406. "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
  2407. ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
  2408. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
  2409. ioc->name, (unsigned long long)ioc->reply_dma));
  2410. total_sz += sz;
  2411. /* reply free queue, 16 byte align */
  2412. sz = ioc->reply_free_queue_depth * 4;
  2413. ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
  2414. ioc->pdev, sz, 16, 0);
  2415. if (!ioc->reply_free_dma_pool) {
  2416. printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
  2417. "failed\n", ioc->name);
  2418. goto out;
  2419. }
  2420. ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
  2421. &ioc->reply_free_dma);
  2422. if (!ioc->reply_free) {
  2423. printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
  2424. "failed\n", ioc->name);
  2425. goto out;
  2426. }
  2427. memset(ioc->reply_free, 0, sz);
  2428. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
  2429. "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
  2430. ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
  2431. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
  2432. "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
  2433. total_sz += sz;
  2434. /* reply post queue, 16 byte align */
  2435. reply_post_free_sz = ioc->reply_post_queue_depth *
  2436. sizeof(Mpi2DefaultReplyDescriptor_t);
  2437. if (_base_is_controller_msix_enabled(ioc))
  2438. sz = reply_post_free_sz * ioc->reply_queue_count;
  2439. else
  2440. sz = reply_post_free_sz;
  2441. ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
  2442. ioc->pdev, sz, 16, 0);
  2443. if (!ioc->reply_post_free_dma_pool) {
  2444. printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
  2445. "failed\n", ioc->name);
  2446. goto out;
  2447. }
  2448. ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
  2449. GFP_KERNEL, &ioc->reply_post_free_dma);
  2450. if (!ioc->reply_post_free) {
  2451. printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
  2452. "failed\n", ioc->name);
  2453. goto out;
  2454. }
  2455. memset(ioc->reply_post_free, 0, sz);
  2456. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
  2457. "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
  2458. ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
  2459. sz/1024));
  2460. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
  2461. "(0x%llx)\n", ioc->name, (unsigned long long)
  2462. ioc->reply_post_free_dma));
  2463. total_sz += sz;
  2464. ioc->config_page_sz = 512;
  2465. ioc->config_page = pci_alloc_consistent(ioc->pdev,
  2466. ioc->config_page_sz, &ioc->config_page_dma);
  2467. if (!ioc->config_page) {
  2468. printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
  2469. "failed\n", ioc->name);
  2470. goto out;
  2471. }
  2472. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
  2473. "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
  2474. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
  2475. "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
  2476. total_sz += ioc->config_page_sz;
  2477. printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
  2478. ioc->name, total_sz/1024);
  2479. printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
  2480. "Max Controller Queue Depth(%d)\n",
  2481. ioc->name, ioc->shost->can_queue, facts->RequestCredit);
  2482. printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
  2483. ioc->name, ioc->shost->sg_tablesize);
  2484. return 0;
  2485. out:
  2486. return -ENOMEM;
  2487. }
  2488. /**
  2489. * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
  2490. * @ioc: Pointer to MPT_ADAPTER structure
  2491. * @cooked: Request raw or cooked IOC state
  2492. *
  2493. * Returns all IOC Doorbell register bits if cooked==0, else just the
  2494. * Doorbell bits in MPI_IOC_STATE_MASK.
  2495. */
  2496. u32
  2497. mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
  2498. {
  2499. u32 s, sc;
  2500. s = readl(&ioc->chip->Doorbell);
  2501. sc = s & MPI2_IOC_STATE_MASK;
  2502. return cooked ? sc : s;
  2503. }
  2504. /**
  2505. * _base_wait_on_iocstate - waiting on a particular ioc state
  2506. * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
  2507. * @timeout: timeout in second
  2508. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2509. *
  2510. * Returns 0 for success, non-zero for failure.
  2511. */
  2512. static int
  2513. _base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
  2514. int sleep_flag)
  2515. {
  2516. u32 count, cntdn;
  2517. u32 current_state;
  2518. count = 0;
  2519. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2520. do {
  2521. current_state = mpt2sas_base_get_iocstate(ioc, 1);
  2522. if (current_state == ioc_state)
  2523. return 0;
  2524. if (count && current_state == MPI2_IOC_STATE_FAULT)
  2525. break;
  2526. if (sleep_flag == CAN_SLEEP)
  2527. msleep(1);
  2528. else
  2529. udelay(500);
  2530. count++;
  2531. } while (--cntdn);
  2532. return current_state;
  2533. }
  2534. /**
  2535. * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
  2536. * a write to the doorbell)
  2537. * @ioc: per adapter object
  2538. * @timeout: timeout in second
  2539. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2540. *
  2541. * Returns 0 for success, non-zero for failure.
  2542. *
  2543. * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
  2544. */
  2545. static int
  2546. _base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
  2547. int sleep_flag)
  2548. {
  2549. u32 cntdn, count;
  2550. u32 int_status;
  2551. count = 0;
  2552. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2553. do {
  2554. int_status = readl(&ioc->chip->HostInterruptStatus);
  2555. if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  2556. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  2557. "successful count(%d), timeout(%d)\n", ioc->name,
  2558. __func__, count, timeout));
  2559. return 0;
  2560. }
  2561. if (sleep_flag == CAN_SLEEP)
  2562. msleep(1);
  2563. else
  2564. udelay(500);
  2565. count++;
  2566. } while (--cntdn);
  2567. printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
  2568. "int_status(%x)!\n", ioc->name, __func__, count, int_status);
  2569. return -EFAULT;
  2570. }
  2571. /**
  2572. * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
  2573. * @ioc: per adapter object
  2574. * @timeout: timeout in second
  2575. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2576. *
  2577. * Returns 0 for success, non-zero for failure.
  2578. *
  2579. * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
  2580. * doorbell.
  2581. */
  2582. static int
  2583. _base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
  2584. int sleep_flag)
  2585. {
  2586. u32 cntdn, count;
  2587. u32 int_status;
  2588. u32 doorbell;
  2589. count = 0;
  2590. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2591. do {
  2592. int_status = readl(&ioc->chip->HostInterruptStatus);
  2593. if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
  2594. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  2595. "successful count(%d), timeout(%d)\n", ioc->name,
  2596. __func__, count, timeout));
  2597. return 0;
  2598. } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  2599. doorbell = readl(&ioc->chip->Doorbell);
  2600. if ((doorbell & MPI2_IOC_STATE_MASK) ==
  2601. MPI2_IOC_STATE_FAULT) {
  2602. mpt2sas_base_fault_info(ioc , doorbell);
  2603. return -EFAULT;
  2604. }
  2605. } else if (int_status == 0xFFFFFFFF)
  2606. goto out;
  2607. if (sleep_flag == CAN_SLEEP)
  2608. msleep(1);
  2609. else
  2610. udelay(500);
  2611. count++;
  2612. } while (--cntdn);
  2613. out:
  2614. printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
  2615. "int_status(%x)!\n", ioc->name, __func__, count, int_status);
  2616. return -EFAULT;
  2617. }
  2618. /**
  2619. * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
  2620. * @ioc: per adapter object
  2621. * @timeout: timeout in second
  2622. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2623. *
  2624. * Returns 0 for success, non-zero for failure.
  2625. *
  2626. */
  2627. static int
  2628. _base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
  2629. int sleep_flag)
  2630. {
  2631. u32 cntdn, count;
  2632. u32 doorbell_reg;
  2633. count = 0;
  2634. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2635. do {
  2636. doorbell_reg = readl(&ioc->chip->Doorbell);
  2637. if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
  2638. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  2639. "successful count(%d), timeout(%d)\n", ioc->name,
  2640. __func__, count, timeout));
  2641. return 0;
  2642. }
  2643. if (sleep_flag == CAN_SLEEP)
  2644. msleep(1);
  2645. else
  2646. udelay(500);
  2647. count++;
  2648. } while (--cntdn);
  2649. printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
  2650. "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
  2651. return -EFAULT;
  2652. }
  2653. /**
  2654. * _base_send_ioc_reset - send doorbell reset
  2655. * @ioc: per adapter object
  2656. * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
  2657. * @timeout: timeout in second
  2658. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2659. *
  2660. * Returns 0 for success, non-zero for failure.
  2661. */
  2662. static int
  2663. _base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
  2664. int sleep_flag)
  2665. {
  2666. u32 ioc_state;
  2667. int r = 0;
  2668. if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
  2669. printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
  2670. ioc->name, __func__);
  2671. return -EFAULT;
  2672. }
  2673. if (!(ioc->facts.IOCCapabilities &
  2674. MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
  2675. return -EFAULT;
  2676. printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
  2677. writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
  2678. &ioc->chip->Doorbell);
  2679. if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
  2680. r = -EFAULT;
  2681. goto out;
  2682. }
  2683. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
  2684. timeout, sleep_flag);
  2685. if (ioc_state) {
  2686. printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
  2687. " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
  2688. r = -EFAULT;
  2689. goto out;
  2690. }
  2691. out:
  2692. printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
  2693. ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
  2694. return r;
  2695. }
  2696. /**
  2697. * _base_handshake_req_reply_wait - send request thru doorbell interface
  2698. * @ioc: per adapter object
  2699. * @request_bytes: request length
  2700. * @request: pointer having request payload
  2701. * @reply_bytes: reply length
  2702. * @reply: pointer to reply payload
  2703. * @timeout: timeout in second
  2704. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2705. *
  2706. * Returns 0 for success, non-zero for failure.
  2707. */
  2708. static int
  2709. _base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
  2710. u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
  2711. {
  2712. MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
  2713. int i;
  2714. u8 failed;
  2715. u16 dummy;
  2716. __le32 *mfp;
  2717. /* make sure doorbell is not in use */
  2718. if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
  2719. printk(MPT2SAS_ERR_FMT "doorbell is in use "
  2720. " (line=%d)\n", ioc->name, __LINE__);
  2721. return -EFAULT;
  2722. }
  2723. /* clear pending doorbell interrupts from previous state changes */
  2724. if (readl(&ioc->chip->HostInterruptStatus) &
  2725. MPI2_HIS_IOC2SYS_DB_STATUS)
  2726. writel(0, &ioc->chip->HostInterruptStatus);
  2727. /* send message to ioc */
  2728. writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
  2729. ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
  2730. &ioc->chip->Doorbell);
  2731. if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
  2732. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2733. "int failed (line=%d)\n", ioc->name, __LINE__);
  2734. return -EFAULT;
  2735. }
  2736. writel(0, &ioc->chip->HostInterruptStatus);
  2737. if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
  2738. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2739. "ack failed (line=%d)\n", ioc->name, __LINE__);
  2740. return -EFAULT;
  2741. }
  2742. /* send message 32-bits at a time */
  2743. for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
  2744. writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
  2745. if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
  2746. failed = 1;
  2747. }
  2748. if (failed) {
  2749. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2750. "sending request failed (line=%d)\n", ioc->name, __LINE__);
  2751. return -EFAULT;
  2752. }
  2753. /* now wait for the reply */
  2754. if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
  2755. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2756. "int failed (line=%d)\n", ioc->name, __LINE__);
  2757. return -EFAULT;
  2758. }
  2759. /* read the first two 16-bits, it gives the total length of the reply */
  2760. reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2761. & MPI2_DOORBELL_DATA_MASK);
  2762. writel(0, &ioc->chip->HostInterruptStatus);
  2763. if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
  2764. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2765. "int failed (line=%d)\n", ioc->name, __LINE__);
  2766. return -EFAULT;
  2767. }
  2768. reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2769. & MPI2_DOORBELL_DATA_MASK);
  2770. writel(0, &ioc->chip->HostInterruptStatus);
  2771. for (i = 2; i < default_reply->MsgLength * 2; i++) {
  2772. if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
  2773. printk(MPT2SAS_ERR_FMT "doorbell "
  2774. "handshake int failed (line=%d)\n", ioc->name,
  2775. __LINE__);
  2776. return -EFAULT;
  2777. }
  2778. if (i >= reply_bytes/2) /* overflow case */
  2779. dummy = readl(&ioc->chip->Doorbell);
  2780. else
  2781. reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2782. & MPI2_DOORBELL_DATA_MASK);
  2783. writel(0, &ioc->chip->HostInterruptStatus);
  2784. }
  2785. _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
  2786. if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
  2787. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
  2788. " (line=%d)\n", ioc->name, __LINE__));
  2789. }
  2790. writel(0, &ioc->chip->HostInterruptStatus);
  2791. if (ioc->logging_level & MPT_DEBUG_INIT) {
  2792. mfp = (__le32 *)reply;
  2793. printk(KERN_INFO "\toffset:data\n");
  2794. for (i = 0; i < reply_bytes/4; i++)
  2795. printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
  2796. le32_to_cpu(mfp[i]));
  2797. }
  2798. return 0;
  2799. }
  2800. /**
  2801. * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
  2802. * @ioc: per adapter object
  2803. * @mpi_reply: the reply payload from FW
  2804. * @mpi_request: the request payload sent to FW
  2805. *
  2806. * The SAS IO Unit Control Request message allows the host to perform low-level
  2807. * operations, such as resets on the PHYs of the IO Unit, also allows the host
  2808. * to obtain the IOC assigned device handles for a device if it has other
  2809. * identifying information about the device, in addition allows the host to
  2810. * remove IOC resources associated with the device.
  2811. *
  2812. * Returns 0 for success, non-zero for failure.
  2813. */
  2814. int
  2815. mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
  2816. Mpi2SasIoUnitControlReply_t *mpi_reply,
  2817. Mpi2SasIoUnitControlRequest_t *mpi_request)
  2818. {
  2819. u16 smid;
  2820. u32 ioc_state;
  2821. unsigned long timeleft;
  2822. u8 issue_reset;
  2823. int rc;
  2824. void *request;
  2825. u16 wait_state_count;
  2826. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  2827. __func__));
  2828. mutex_lock(&ioc->base_cmds.mutex);
  2829. if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
  2830. printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
  2831. ioc->name, __func__);
  2832. rc = -EAGAIN;
  2833. goto out;
  2834. }
  2835. wait_state_count = 0;
  2836. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2837. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  2838. if (wait_state_count++ == 10) {
  2839. printk(MPT2SAS_ERR_FMT
  2840. "%s: failed due to ioc not operational\n",
  2841. ioc->name, __func__);
  2842. rc = -EFAULT;
  2843. goto out;
  2844. }
  2845. ssleep(1);
  2846. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2847. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  2848. "operational state(count=%d)\n", ioc->name,
  2849. __func__, wait_state_count);
  2850. }
  2851. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  2852. if (!smid) {
  2853. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2854. ioc->name, __func__);
  2855. rc = -EAGAIN;
  2856. goto out;
  2857. }
  2858. rc = 0;
  2859. ioc->base_cmds.status = MPT2_CMD_PENDING;
  2860. request = mpt2sas_base_get_msg_frame(ioc, smid);
  2861. ioc->base_cmds.smid = smid;
  2862. memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
  2863. if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  2864. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
  2865. ioc->ioc_link_reset_in_progress = 1;
  2866. init_completion(&ioc->base_cmds.done);
  2867. mpt2sas_base_put_smid_default(ioc, smid);
  2868. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
  2869. msecs_to_jiffies(10000));
  2870. if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  2871. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
  2872. ioc->ioc_link_reset_in_progress)
  2873. ioc->ioc_link_reset_in_progress = 0;
  2874. if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
  2875. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  2876. ioc->name, __func__);
  2877. _debug_dump_mf(mpi_request,
  2878. sizeof(Mpi2SasIoUnitControlRequest_t)/4);
  2879. if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
  2880. issue_reset = 1;
  2881. goto issue_host_reset;
  2882. }
  2883. if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
  2884. memcpy(mpi_reply, ioc->base_cmds.reply,
  2885. sizeof(Mpi2SasIoUnitControlReply_t));
  2886. else
  2887. memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  2888. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2889. goto out;
  2890. issue_host_reset:
  2891. if (issue_reset)
  2892. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  2893. FORCE_BIG_HAMMER);
  2894. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2895. rc = -EFAULT;
  2896. out:
  2897. mutex_unlock(&ioc->base_cmds.mutex);
  2898. return rc;
  2899. }
  2900. /**
  2901. * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
  2902. * @ioc: per adapter object
  2903. * @mpi_reply: the reply payload from FW
  2904. * @mpi_request: the request payload sent to FW
  2905. *
  2906. * The SCSI Enclosure Processor request message causes the IOC to
  2907. * communicate with SES devices to control LED status signals.
  2908. *
  2909. * Returns 0 for success, non-zero for failure.
  2910. */
  2911. int
  2912. mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
  2913. Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
  2914. {
  2915. u16 smid;
  2916. u32 ioc_state;
  2917. unsigned long timeleft;
  2918. u8 issue_reset;
  2919. int rc;
  2920. void *request;
  2921. u16 wait_state_count;
  2922. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  2923. __func__));
  2924. mutex_lock(&ioc->base_cmds.mutex);
  2925. if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
  2926. printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
  2927. ioc->name, __func__);
  2928. rc = -EAGAIN;
  2929. goto out;
  2930. }
  2931. wait_state_count = 0;
  2932. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2933. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  2934. if (wait_state_count++ == 10) {
  2935. printk(MPT2SAS_ERR_FMT
  2936. "%s: failed due to ioc not operational\n",
  2937. ioc->name, __func__);
  2938. rc = -EFAULT;
  2939. goto out;
  2940. }
  2941. ssleep(1);
  2942. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2943. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  2944. "operational state(count=%d)\n", ioc->name,
  2945. __func__, wait_state_count);
  2946. }
  2947. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  2948. if (!smid) {
  2949. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2950. ioc->name, __func__);
  2951. rc = -EAGAIN;
  2952. goto out;
  2953. }
  2954. rc = 0;
  2955. ioc->base_cmds.status = MPT2_CMD_PENDING;
  2956. request = mpt2sas_base_get_msg_frame(ioc, smid);
  2957. ioc->base_cmds.smid = smid;
  2958. memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
  2959. init_completion(&ioc->base_cmds.done);
  2960. mpt2sas_base_put_smid_default(ioc, smid);
  2961. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
  2962. msecs_to_jiffies(10000));
  2963. if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
  2964. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  2965. ioc->name, __func__);
  2966. _debug_dump_mf(mpi_request,
  2967. sizeof(Mpi2SepRequest_t)/4);
  2968. if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
  2969. issue_reset = 1;
  2970. goto issue_host_reset;
  2971. }
  2972. if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
  2973. memcpy(mpi_reply, ioc->base_cmds.reply,
  2974. sizeof(Mpi2SepReply_t));
  2975. else
  2976. memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
  2977. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2978. goto out;
  2979. issue_host_reset:
  2980. if (issue_reset)
  2981. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  2982. FORCE_BIG_HAMMER);
  2983. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2984. rc = -EFAULT;
  2985. out:
  2986. mutex_unlock(&ioc->base_cmds.mutex);
  2987. return rc;
  2988. }
  2989. /**
  2990. * _base_get_port_facts - obtain port facts reply and save in ioc
  2991. * @ioc: per adapter object
  2992. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2993. *
  2994. * Returns 0 for success, non-zero for failure.
  2995. */
  2996. static int
  2997. _base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
  2998. {
  2999. Mpi2PortFactsRequest_t mpi_request;
  3000. Mpi2PortFactsReply_t mpi_reply;
  3001. struct mpt2sas_port_facts *pfacts;
  3002. int mpi_reply_sz, mpi_request_sz, r;
  3003. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3004. __func__));
  3005. mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
  3006. mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
  3007. memset(&mpi_request, 0, mpi_request_sz);
  3008. mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
  3009. mpi_request.PortNumber = port;
  3010. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  3011. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
  3012. if (r != 0) {
  3013. printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
  3014. ioc->name, __func__, r);
  3015. return r;
  3016. }
  3017. pfacts = &ioc->pfacts[port];
  3018. memset(pfacts, 0, sizeof(struct mpt2sas_port_facts));
  3019. pfacts->PortNumber = mpi_reply.PortNumber;
  3020. pfacts->VP_ID = mpi_reply.VP_ID;
  3021. pfacts->VF_ID = mpi_reply.VF_ID;
  3022. pfacts->MaxPostedCmdBuffers =
  3023. le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
  3024. return 0;
  3025. }
  3026. /**
  3027. * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
  3028. * @ioc: per adapter object
  3029. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3030. *
  3031. * Returns 0 for success, non-zero for failure.
  3032. */
  3033. static int
  3034. _base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3035. {
  3036. Mpi2IOCFactsRequest_t mpi_request;
  3037. Mpi2IOCFactsReply_t mpi_reply;
  3038. struct mpt2sas_facts *facts;
  3039. int mpi_reply_sz, mpi_request_sz, r;
  3040. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3041. __func__));
  3042. mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
  3043. mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
  3044. memset(&mpi_request, 0, mpi_request_sz);
  3045. mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
  3046. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  3047. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
  3048. if (r != 0) {
  3049. printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
  3050. ioc->name, __func__, r);
  3051. return r;
  3052. }
  3053. facts = &ioc->facts;
  3054. memset(facts, 0, sizeof(struct mpt2sas_facts));
  3055. facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
  3056. facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
  3057. facts->VP_ID = mpi_reply.VP_ID;
  3058. facts->VF_ID = mpi_reply.VF_ID;
  3059. facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
  3060. facts->MaxChainDepth = mpi_reply.MaxChainDepth;
  3061. facts->WhoInit = mpi_reply.WhoInit;
  3062. facts->NumberOfPorts = mpi_reply.NumberOfPorts;
  3063. facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
  3064. facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
  3065. facts->MaxReplyDescriptorPostQueueDepth =
  3066. le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
  3067. facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
  3068. facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
  3069. if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
  3070. ioc->ir_firmware = 1;
  3071. facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
  3072. facts->IOCRequestFrameSize =
  3073. le16_to_cpu(mpi_reply.IOCRequestFrameSize);
  3074. facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
  3075. facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
  3076. ioc->shost->max_id = -1;
  3077. facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
  3078. facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
  3079. facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
  3080. facts->HighPriorityCredit =
  3081. le16_to_cpu(mpi_reply.HighPriorityCredit);
  3082. facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
  3083. facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
  3084. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
  3085. "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
  3086. facts->MaxChainDepth));
  3087. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
  3088. "reply frame size(%d)\n", ioc->name,
  3089. facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
  3090. return 0;
  3091. }
  3092. /**
  3093. * _base_send_ioc_init - send ioc_init to firmware
  3094. * @ioc: per adapter object
  3095. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3096. *
  3097. * Returns 0 for success, non-zero for failure.
  3098. */
  3099. static int
  3100. _base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3101. {
  3102. Mpi2IOCInitRequest_t mpi_request;
  3103. Mpi2IOCInitReply_t mpi_reply;
  3104. int r;
  3105. struct timeval current_time;
  3106. u16 ioc_status;
  3107. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3108. __func__));
  3109. memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
  3110. mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
  3111. mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
  3112. mpi_request.VF_ID = 0; /* TODO */
  3113. mpi_request.VP_ID = 0;
  3114. mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
  3115. mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
  3116. if (_base_is_controller_msix_enabled(ioc))
  3117. mpi_request.HostMSIxVectors = ioc->reply_queue_count;
  3118. mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
  3119. mpi_request.ReplyDescriptorPostQueueDepth =
  3120. cpu_to_le16(ioc->reply_post_queue_depth);
  3121. mpi_request.ReplyFreeQueueDepth =
  3122. cpu_to_le16(ioc->reply_free_queue_depth);
  3123. mpi_request.SenseBufferAddressHigh =
  3124. cpu_to_le32((u64)ioc->sense_dma >> 32);
  3125. mpi_request.SystemReplyAddressHigh =
  3126. cpu_to_le32((u64)ioc->reply_dma >> 32);
  3127. mpi_request.SystemRequestFrameBaseAddress =
  3128. cpu_to_le64((u64)ioc->request_dma);
  3129. mpi_request.ReplyFreeQueueAddress =
  3130. cpu_to_le64((u64)ioc->reply_free_dma);
  3131. mpi_request.ReplyDescriptorPostQueueAddress =
  3132. cpu_to_le64((u64)ioc->reply_post_free_dma);
  3133. /* This time stamp specifies number of milliseconds
  3134. * since epoch ~ midnight January 1, 1970.
  3135. */
  3136. do_gettimeofday(&current_time);
  3137. mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
  3138. (current_time.tv_usec / 1000));
  3139. if (ioc->logging_level & MPT_DEBUG_INIT) {
  3140. __le32 *mfp;
  3141. int i;
  3142. mfp = (__le32 *)&mpi_request;
  3143. printk(KERN_INFO "\toffset:data\n");
  3144. for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
  3145. printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
  3146. le32_to_cpu(mfp[i]));
  3147. }
  3148. r = _base_handshake_req_reply_wait(ioc,
  3149. sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
  3150. sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
  3151. sleep_flag);
  3152. if (r != 0) {
  3153. printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
  3154. ioc->name, __func__, r);
  3155. return r;
  3156. }
  3157. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  3158. if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
  3159. mpi_reply.IOCLogInfo) {
  3160. printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
  3161. r = -EIO;
  3162. }
  3163. return 0;
  3164. }
  3165. /**
  3166. * mpt2sas_port_enable_done - command completion routine for port enable
  3167. * @ioc: per adapter object
  3168. * @smid: system request message index
  3169. * @msix_index: MSIX table index supplied by the OS
  3170. * @reply: reply message frame(lower 32bit addr)
  3171. *
  3172. * Return 1 meaning mf should be freed from _base_interrupt
  3173. * 0 means the mf is freed from this function.
  3174. */
  3175. u8
  3176. mpt2sas_port_enable_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  3177. u32 reply)
  3178. {
  3179. MPI2DefaultReply_t *mpi_reply;
  3180. u16 ioc_status;
  3181. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  3182. if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
  3183. return 1;
  3184. if (ioc->port_enable_cmds.status == MPT2_CMD_NOT_USED)
  3185. return 1;
  3186. ioc->port_enable_cmds.status |= MPT2_CMD_COMPLETE;
  3187. if (mpi_reply) {
  3188. ioc->port_enable_cmds.status |= MPT2_CMD_REPLY_VALID;
  3189. memcpy(ioc->port_enable_cmds.reply, mpi_reply,
  3190. mpi_reply->MsgLength*4);
  3191. }
  3192. ioc->port_enable_cmds.status &= ~MPT2_CMD_PENDING;
  3193. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  3194. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  3195. ioc->port_enable_failed = 1;
  3196. if (ioc->is_driver_loading) {
  3197. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  3198. mpt2sas_port_enable_complete(ioc);
  3199. return 1;
  3200. } else {
  3201. ioc->start_scan_failed = ioc_status;
  3202. ioc->start_scan = 0;
  3203. return 1;
  3204. }
  3205. }
  3206. complete(&ioc->port_enable_cmds.done);
  3207. return 1;
  3208. }
  3209. /**
  3210. * _base_send_port_enable - send port_enable(discovery stuff) to firmware
  3211. * @ioc: per adapter object
  3212. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3213. *
  3214. * Returns 0 for success, non-zero for failure.
  3215. */
  3216. static int
  3217. _base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3218. {
  3219. Mpi2PortEnableRequest_t *mpi_request;
  3220. Mpi2PortEnableReply_t *mpi_reply;
  3221. unsigned long timeleft;
  3222. int r = 0;
  3223. u16 smid;
  3224. u16 ioc_status;
  3225. printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
  3226. if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
  3227. printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
  3228. ioc->name, __func__);
  3229. return -EAGAIN;
  3230. }
  3231. smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
  3232. if (!smid) {
  3233. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  3234. ioc->name, __func__);
  3235. return -EAGAIN;
  3236. }
  3237. ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
  3238. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  3239. ioc->port_enable_cmds.smid = smid;
  3240. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  3241. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  3242. init_completion(&ioc->port_enable_cmds.done);
  3243. mpt2sas_base_put_smid_default(ioc, smid);
  3244. timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
  3245. 300*HZ);
  3246. if (!(ioc->port_enable_cmds.status & MPT2_CMD_COMPLETE)) {
  3247. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  3248. ioc->name, __func__);
  3249. _debug_dump_mf(mpi_request,
  3250. sizeof(Mpi2PortEnableRequest_t)/4);
  3251. if (ioc->port_enable_cmds.status & MPT2_CMD_RESET)
  3252. r = -EFAULT;
  3253. else
  3254. r = -ETIME;
  3255. goto out;
  3256. }
  3257. mpi_reply = ioc->port_enable_cmds.reply;
  3258. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  3259. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3260. printk(MPT2SAS_ERR_FMT "%s: failed with (ioc_status=0x%08x)\n",
  3261. ioc->name, __func__, ioc_status);
  3262. r = -EFAULT;
  3263. goto out;
  3264. }
  3265. out:
  3266. ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
  3267. printk(MPT2SAS_INFO_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
  3268. "SUCCESS" : "FAILED"));
  3269. return r;
  3270. }
  3271. /**
  3272. * mpt2sas_port_enable - initiate firmware discovery (don't wait for reply)
  3273. * @ioc: per adapter object
  3274. *
  3275. * Returns 0 for success, non-zero for failure.
  3276. */
  3277. int
  3278. mpt2sas_port_enable(struct MPT2SAS_ADAPTER *ioc)
  3279. {
  3280. Mpi2PortEnableRequest_t *mpi_request;
  3281. u16 smid;
  3282. printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
  3283. if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
  3284. printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
  3285. ioc->name, __func__);
  3286. return -EAGAIN;
  3287. }
  3288. smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
  3289. if (!smid) {
  3290. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  3291. ioc->name, __func__);
  3292. return -EAGAIN;
  3293. }
  3294. ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
  3295. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  3296. ioc->port_enable_cmds.smid = smid;
  3297. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  3298. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  3299. mpt2sas_base_put_smid_default(ioc, smid);
  3300. return 0;
  3301. }
  3302. /**
  3303. * _base_determine_wait_on_discovery - desposition
  3304. * @ioc: per adapter object
  3305. *
  3306. * Decide whether to wait on discovery to complete. Used to either
  3307. * locate boot device, or report volumes ahead of physical devices.
  3308. *
  3309. * Returns 1 for wait, 0 for don't wait
  3310. */
  3311. static int
  3312. _base_determine_wait_on_discovery(struct MPT2SAS_ADAPTER *ioc)
  3313. {
  3314. /* We wait for discovery to complete if IR firmware is loaded.
  3315. * The sas topology events arrive before PD events, so we need time to
  3316. * turn on the bit in ioc->pd_handles to indicate PD
  3317. * Also, it maybe required to report Volumes ahead of physical
  3318. * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
  3319. */
  3320. if (ioc->ir_firmware)
  3321. return 1;
  3322. /* if no Bios, then we don't need to wait */
  3323. if (!ioc->bios_pg3.BiosVersion)
  3324. return 0;
  3325. /* Bios is present, then we drop down here.
  3326. *
  3327. * If there any entries in the Bios Page 2, then we wait
  3328. * for discovery to complete.
  3329. */
  3330. /* Current Boot Device */
  3331. if ((ioc->bios_pg2.CurrentBootDeviceForm &
  3332. MPI2_BIOSPAGE2_FORM_MASK) ==
  3333. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
  3334. /* Request Boot Device */
  3335. (ioc->bios_pg2.ReqBootDeviceForm &
  3336. MPI2_BIOSPAGE2_FORM_MASK) ==
  3337. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
  3338. /* Alternate Request Boot Device */
  3339. (ioc->bios_pg2.ReqAltBootDeviceForm &
  3340. MPI2_BIOSPAGE2_FORM_MASK) ==
  3341. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
  3342. return 0;
  3343. return 1;
  3344. }
  3345. /**
  3346. * _base_unmask_events - turn on notification for this event
  3347. * @ioc: per adapter object
  3348. * @event: firmware event
  3349. *
  3350. * The mask is stored in ioc->event_masks.
  3351. */
  3352. static void
  3353. _base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
  3354. {
  3355. u32 desired_event;
  3356. if (event >= 128)
  3357. return;
  3358. desired_event = (1 << (event % 32));
  3359. if (event < 32)
  3360. ioc->event_masks[0] &= ~desired_event;
  3361. else if (event < 64)
  3362. ioc->event_masks[1] &= ~desired_event;
  3363. else if (event < 96)
  3364. ioc->event_masks[2] &= ~desired_event;
  3365. else if (event < 128)
  3366. ioc->event_masks[3] &= ~desired_event;
  3367. }
  3368. /**
  3369. * _base_event_notification - send event notification
  3370. * @ioc: per adapter object
  3371. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3372. *
  3373. * Returns 0 for success, non-zero for failure.
  3374. */
  3375. static int
  3376. _base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3377. {
  3378. Mpi2EventNotificationRequest_t *mpi_request;
  3379. unsigned long timeleft;
  3380. u16 smid;
  3381. int r = 0;
  3382. int i;
  3383. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3384. __func__));
  3385. if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
  3386. printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
  3387. ioc->name, __func__);
  3388. return -EAGAIN;
  3389. }
  3390. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  3391. if (!smid) {
  3392. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  3393. ioc->name, __func__);
  3394. return -EAGAIN;
  3395. }
  3396. ioc->base_cmds.status = MPT2_CMD_PENDING;
  3397. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  3398. ioc->base_cmds.smid = smid;
  3399. memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
  3400. mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  3401. mpi_request->VF_ID = 0; /* TODO */
  3402. mpi_request->VP_ID = 0;
  3403. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  3404. mpi_request->EventMasks[i] =
  3405. cpu_to_le32(ioc->event_masks[i]);
  3406. init_completion(&ioc->base_cmds.done);
  3407. mpt2sas_base_put_smid_default(ioc, smid);
  3408. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
  3409. if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
  3410. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  3411. ioc->name, __func__);
  3412. _debug_dump_mf(mpi_request,
  3413. sizeof(Mpi2EventNotificationRequest_t)/4);
  3414. if (ioc->base_cmds.status & MPT2_CMD_RESET)
  3415. r = -EFAULT;
  3416. else
  3417. r = -ETIME;
  3418. } else
  3419. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
  3420. ioc->name, __func__));
  3421. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  3422. return r;
  3423. }
  3424. /**
  3425. * mpt2sas_base_validate_event_type - validating event types
  3426. * @ioc: per adapter object
  3427. * @event: firmware event
  3428. *
  3429. * This will turn on firmware event notification when application
  3430. * ask for that event. We don't mask events that are already enabled.
  3431. */
  3432. void
  3433. mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
  3434. {
  3435. int i, j;
  3436. u32 event_mask, desired_event;
  3437. u8 send_update_to_fw;
  3438. for (i = 0, send_update_to_fw = 0; i <
  3439. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
  3440. event_mask = ~event_type[i];
  3441. desired_event = 1;
  3442. for (j = 0; j < 32; j++) {
  3443. if (!(event_mask & desired_event) &&
  3444. (ioc->event_masks[i] & desired_event)) {
  3445. ioc->event_masks[i] &= ~desired_event;
  3446. send_update_to_fw = 1;
  3447. }
  3448. desired_event = (desired_event << 1);
  3449. }
  3450. }
  3451. if (!send_update_to_fw)
  3452. return;
  3453. mutex_lock(&ioc->base_cmds.mutex);
  3454. _base_event_notification(ioc, CAN_SLEEP);
  3455. mutex_unlock(&ioc->base_cmds.mutex);
  3456. }
  3457. /**
  3458. * _base_diag_reset - the "big hammer" start of day reset
  3459. * @ioc: per adapter object
  3460. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3461. *
  3462. * Returns 0 for success, non-zero for failure.
  3463. */
  3464. static int
  3465. _base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3466. {
  3467. u32 host_diagnostic;
  3468. u32 ioc_state;
  3469. u32 count;
  3470. u32 hcb_size;
  3471. printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
  3472. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "clear interrupts\n",
  3473. ioc->name));
  3474. count = 0;
  3475. do {
  3476. /* Write magic sequence to WriteSequence register
  3477. * Loop until in diagnostic mode
  3478. */
  3479. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "write magic "
  3480. "sequence\n", ioc->name));
  3481. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  3482. writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
  3483. writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
  3484. writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
  3485. writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3486. writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3487. writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3488. /* wait 100 msec */
  3489. if (sleep_flag == CAN_SLEEP)
  3490. msleep(100);
  3491. else
  3492. mdelay(100);
  3493. if (count++ > 20)
  3494. goto out;
  3495. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  3496. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "wrote magic "
  3497. "sequence: count(%d), host_diagnostic(0x%08x)\n",
  3498. ioc->name, count, host_diagnostic));
  3499. } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
  3500. hcb_size = readl(&ioc->chip->HCBSize);
  3501. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "diag reset: issued\n",
  3502. ioc->name));
  3503. writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
  3504. &ioc->chip->HostDiagnostic);
  3505. /* don't access any registers for 50 milliseconds */
  3506. msleep(50);
  3507. /* 300 second max wait */
  3508. for (count = 0; count < 3000000 ; count++) {
  3509. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  3510. if (host_diagnostic == 0xFFFFFFFF)
  3511. goto out;
  3512. if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
  3513. break;
  3514. /* wait 100 msec */
  3515. if (sleep_flag == CAN_SLEEP)
  3516. msleep(1);
  3517. else
  3518. mdelay(1);
  3519. }
  3520. if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
  3521. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter "
  3522. "assuming the HCB Address points to good F/W\n",
  3523. ioc->name));
  3524. host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
  3525. host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
  3526. writel(host_diagnostic, &ioc->chip->HostDiagnostic);
  3527. drsprintk(ioc, printk(MPT2SAS_INFO_FMT
  3528. "re-enable the HCDW\n", ioc->name));
  3529. writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
  3530. &ioc->chip->HCBSize);
  3531. }
  3532. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter\n",
  3533. ioc->name));
  3534. writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
  3535. &ioc->chip->HostDiagnostic);
  3536. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "disable writes to the "
  3537. "diagnostic register\n", ioc->name));
  3538. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  3539. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "Wait for FW to go to the "
  3540. "READY state\n", ioc->name));
  3541. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
  3542. sleep_flag);
  3543. if (ioc_state) {
  3544. printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
  3545. " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
  3546. goto out;
  3547. }
  3548. printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
  3549. return 0;
  3550. out:
  3551. printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
  3552. return -EFAULT;
  3553. }
  3554. /**
  3555. * _base_make_ioc_ready - put controller in READY state
  3556. * @ioc: per adapter object
  3557. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3558. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  3559. *
  3560. * Returns 0 for success, non-zero for failure.
  3561. */
  3562. static int
  3563. _base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
  3564. enum reset_type type)
  3565. {
  3566. u32 ioc_state;
  3567. int rc;
  3568. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3569. __func__));
  3570. if (ioc->pci_error_recovery)
  3571. return 0;
  3572. ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
  3573. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
  3574. ioc->name, __func__, ioc_state));
  3575. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
  3576. return 0;
  3577. if (ioc_state & MPI2_DOORBELL_USED) {
  3578. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
  3579. "active!\n", ioc->name));
  3580. goto issue_diag_reset;
  3581. }
  3582. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  3583. mpt2sas_base_fault_info(ioc, ioc_state &
  3584. MPI2_DOORBELL_DATA_MASK);
  3585. goto issue_diag_reset;
  3586. }
  3587. if (type == FORCE_BIG_HAMMER)
  3588. goto issue_diag_reset;
  3589. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
  3590. if (!(_base_send_ioc_reset(ioc,
  3591. MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
  3592. ioc->ioc_reset_count++;
  3593. return 0;
  3594. }
  3595. issue_diag_reset:
  3596. rc = _base_diag_reset(ioc, CAN_SLEEP);
  3597. ioc->ioc_reset_count++;
  3598. return rc;
  3599. }
  3600. /**
  3601. * _base_make_ioc_operational - put controller in OPERATIONAL state
  3602. * @ioc: per adapter object
  3603. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3604. *
  3605. * Returns 0 for success, non-zero for failure.
  3606. */
  3607. static int
  3608. _base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3609. {
  3610. int r, i;
  3611. unsigned long flags;
  3612. u32 reply_address;
  3613. u16 smid;
  3614. struct _tr_list *delayed_tr, *delayed_tr_next;
  3615. u8 hide_flag;
  3616. struct adapter_reply_queue *reply_q;
  3617. long reply_post_free;
  3618. u32 reply_post_free_sz;
  3619. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3620. __func__));
  3621. /* clean the delayed target reset list */
  3622. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  3623. &ioc->delayed_tr_list, list) {
  3624. list_del(&delayed_tr->list);
  3625. kfree(delayed_tr);
  3626. }
  3627. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  3628. &ioc->delayed_tr_volume_list, list) {
  3629. list_del(&delayed_tr->list);
  3630. kfree(delayed_tr);
  3631. }
  3632. /* initialize the scsi lookup free list */
  3633. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  3634. INIT_LIST_HEAD(&ioc->free_list);
  3635. smid = 1;
  3636. for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
  3637. INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
  3638. ioc->scsi_lookup[i].cb_idx = 0xFF;
  3639. ioc->scsi_lookup[i].smid = smid;
  3640. ioc->scsi_lookup[i].scmd = NULL;
  3641. ioc->scsi_lookup[i].direct_io = 0;
  3642. list_add_tail(&ioc->scsi_lookup[i].tracker_list,
  3643. &ioc->free_list);
  3644. }
  3645. /* hi-priority queue */
  3646. INIT_LIST_HEAD(&ioc->hpr_free_list);
  3647. smid = ioc->hi_priority_smid;
  3648. for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
  3649. ioc->hpr_lookup[i].cb_idx = 0xFF;
  3650. ioc->hpr_lookup[i].smid = smid;
  3651. list_add_tail(&ioc->hpr_lookup[i].tracker_list,
  3652. &ioc->hpr_free_list);
  3653. }
  3654. /* internal queue */
  3655. INIT_LIST_HEAD(&ioc->internal_free_list);
  3656. smid = ioc->internal_smid;
  3657. for (i = 0; i < ioc->internal_depth; i++, smid++) {
  3658. ioc->internal_lookup[i].cb_idx = 0xFF;
  3659. ioc->internal_lookup[i].smid = smid;
  3660. list_add_tail(&ioc->internal_lookup[i].tracker_list,
  3661. &ioc->internal_free_list);
  3662. }
  3663. /* chain pool */
  3664. INIT_LIST_HEAD(&ioc->free_chain_list);
  3665. for (i = 0; i < ioc->chain_depth; i++)
  3666. list_add_tail(&ioc->chain_lookup[i].tracker_list,
  3667. &ioc->free_chain_list);
  3668. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3669. /* initialize Reply Free Queue */
  3670. for (i = 0, reply_address = (u32)ioc->reply_dma ;
  3671. i < ioc->reply_free_queue_depth ; i++, reply_address +=
  3672. ioc->reply_sz)
  3673. ioc->reply_free[i] = cpu_to_le32(reply_address);
  3674. /* initialize reply queues */
  3675. if (ioc->is_driver_loading)
  3676. _base_assign_reply_queues(ioc);
  3677. /* initialize Reply Post Free Queue */
  3678. reply_post_free = (long)ioc->reply_post_free;
  3679. reply_post_free_sz = ioc->reply_post_queue_depth *
  3680. sizeof(Mpi2DefaultReplyDescriptor_t);
  3681. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  3682. reply_q->reply_post_host_index = 0;
  3683. reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
  3684. reply_post_free;
  3685. for (i = 0; i < ioc->reply_post_queue_depth; i++)
  3686. reply_q->reply_post_free[i].Words =
  3687. cpu_to_le64(ULLONG_MAX);
  3688. if (!_base_is_controller_msix_enabled(ioc))
  3689. goto skip_init_reply_post_free_queue;
  3690. reply_post_free += reply_post_free_sz;
  3691. }
  3692. skip_init_reply_post_free_queue:
  3693. r = _base_send_ioc_init(ioc, sleep_flag);
  3694. if (r)
  3695. return r;
  3696. /* initialize reply free host index */
  3697. ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
  3698. writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
  3699. /* initialize reply post host index */
  3700. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  3701. writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
  3702. &ioc->chip->ReplyPostHostIndex);
  3703. if (!_base_is_controller_msix_enabled(ioc))
  3704. goto skip_init_reply_post_host_index;
  3705. }
  3706. skip_init_reply_post_host_index:
  3707. _base_unmask_interrupts(ioc);
  3708. r = _base_event_notification(ioc, sleep_flag);
  3709. if (r)
  3710. return r;
  3711. if (sleep_flag == CAN_SLEEP)
  3712. _base_static_config_pages(ioc);
  3713. if (ioc->is_driver_loading) {
  3714. if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
  3715. == 0x80) {
  3716. hide_flag = (u8) (
  3717. le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
  3718. MFG_PAGE10_HIDE_SSDS_MASK);
  3719. if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
  3720. ioc->mfg_pg10_hide_flag = hide_flag;
  3721. }
  3722. ioc->wait_for_discovery_to_complete =
  3723. _base_determine_wait_on_discovery(ioc);
  3724. return r; /* scan_start and scan_finished support */
  3725. }
  3726. r = _base_send_port_enable(ioc, sleep_flag);
  3727. if (r)
  3728. return r;
  3729. return r;
  3730. }
  3731. /**
  3732. * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
  3733. * @ioc: per adapter object
  3734. *
  3735. * Return nothing.
  3736. */
  3737. void
  3738. mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
  3739. {
  3740. struct pci_dev *pdev = ioc->pdev;
  3741. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3742. __func__));
  3743. _base_mask_interrupts(ioc);
  3744. ioc->shost_recovery = 1;
  3745. _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
  3746. ioc->shost_recovery = 0;
  3747. _base_free_irq(ioc);
  3748. _base_disable_msix(ioc);
  3749. if (ioc->chip_phys)
  3750. iounmap(ioc->chip);
  3751. ioc->chip_phys = 0;
  3752. pci_release_selected_regions(ioc->pdev, ioc->bars);
  3753. pci_disable_pcie_error_reporting(pdev);
  3754. pci_disable_device(pdev);
  3755. return;
  3756. }
  3757. /**
  3758. * mpt2sas_base_attach - attach controller instance
  3759. * @ioc: per adapter object
  3760. *
  3761. * Returns 0 for success, non-zero for failure.
  3762. */
  3763. int
  3764. mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
  3765. {
  3766. int r, i;
  3767. int cpu_id, last_cpu_id = 0;
  3768. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3769. __func__));
  3770. /* setup cpu_msix_table */
  3771. ioc->cpu_count = num_online_cpus();
  3772. for_each_online_cpu(cpu_id)
  3773. last_cpu_id = cpu_id;
  3774. ioc->cpu_msix_table_sz = last_cpu_id + 1;
  3775. ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
  3776. ioc->reply_queue_count = 1;
  3777. if (!ioc->cpu_msix_table) {
  3778. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
  3779. "cpu_msix_table failed!!!\n", ioc->name));
  3780. r = -ENOMEM;
  3781. goto out_free_resources;
  3782. }
  3783. if (ioc->is_warpdrive) {
  3784. ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
  3785. sizeof(resource_size_t *), GFP_KERNEL);
  3786. if (!ioc->reply_post_host_index) {
  3787. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation "
  3788. "for cpu_msix_table failed!!!\n", ioc->name));
  3789. r = -ENOMEM;
  3790. goto out_free_resources;
  3791. }
  3792. }
  3793. r = mpt2sas_base_map_resources(ioc);
  3794. if (r)
  3795. goto out_free_resources;
  3796. if (ioc->is_warpdrive) {
  3797. ioc->reply_post_host_index[0] =
  3798. (resource_size_t *)&ioc->chip->ReplyPostHostIndex;
  3799. for (i = 1; i < ioc->cpu_msix_table_sz; i++)
  3800. ioc->reply_post_host_index[i] = (resource_size_t *)
  3801. ((u8 *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
  3802. * 4)));
  3803. }
  3804. pci_set_drvdata(ioc->pdev, ioc->shost);
  3805. r = _base_get_ioc_facts(ioc, CAN_SLEEP);
  3806. if (r)
  3807. goto out_free_resources;
  3808. r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
  3809. if (r)
  3810. goto out_free_resources;
  3811. ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
  3812. sizeof(struct mpt2sas_port_facts), GFP_KERNEL);
  3813. if (!ioc->pfacts) {
  3814. r = -ENOMEM;
  3815. goto out_free_resources;
  3816. }
  3817. for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
  3818. r = _base_get_port_facts(ioc, i, CAN_SLEEP);
  3819. if (r)
  3820. goto out_free_resources;
  3821. }
  3822. r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
  3823. if (r)
  3824. goto out_free_resources;
  3825. init_waitqueue_head(&ioc->reset_wq);
  3826. /* allocate memory pd handle bitmask list */
  3827. ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
  3828. if (ioc->facts.MaxDevHandle % 8)
  3829. ioc->pd_handles_sz++;
  3830. ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
  3831. GFP_KERNEL);
  3832. if (!ioc->pd_handles) {
  3833. r = -ENOMEM;
  3834. goto out_free_resources;
  3835. }
  3836. ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
  3837. GFP_KERNEL);
  3838. if (!ioc->blocking_handles) {
  3839. r = -ENOMEM;
  3840. goto out_free_resources;
  3841. }
  3842. ioc->fwfault_debug = mpt2sas_fwfault_debug;
  3843. /* base internal command bits */
  3844. mutex_init(&ioc->base_cmds.mutex);
  3845. ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3846. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  3847. /* port_enable command bits */
  3848. ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3849. ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
  3850. /* transport internal command bits */
  3851. ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3852. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  3853. mutex_init(&ioc->transport_cmds.mutex);
  3854. /* scsih internal command bits */
  3855. ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3856. ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
  3857. mutex_init(&ioc->scsih_cmds.mutex);
  3858. /* task management internal command bits */
  3859. ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3860. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  3861. mutex_init(&ioc->tm_cmds.mutex);
  3862. /* config page internal command bits */
  3863. ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3864. ioc->config_cmds.status = MPT2_CMD_NOT_USED;
  3865. mutex_init(&ioc->config_cmds.mutex);
  3866. /* ctl module internal command bits */
  3867. ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3868. ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
  3869. ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
  3870. mutex_init(&ioc->ctl_cmds.mutex);
  3871. if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
  3872. !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
  3873. !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
  3874. !ioc->ctl_cmds.sense) {
  3875. r = -ENOMEM;
  3876. goto out_free_resources;
  3877. }
  3878. if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
  3879. !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
  3880. !ioc->config_cmds.reply || !ioc->ctl_cmds.reply) {
  3881. r = -ENOMEM;
  3882. goto out_free_resources;
  3883. }
  3884. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  3885. ioc->event_masks[i] = -1;
  3886. /* here we enable the events we care about */
  3887. _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
  3888. _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
  3889. _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
  3890. _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  3891. _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
  3892. _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
  3893. _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
  3894. _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
  3895. _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
  3896. _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
  3897. r = _base_make_ioc_operational(ioc, CAN_SLEEP);
  3898. if (r)
  3899. goto out_free_resources;
  3900. if (missing_delay[0] != -1 && missing_delay[1] != -1)
  3901. _base_update_missing_delay(ioc, missing_delay[0],
  3902. missing_delay[1]);
  3903. return 0;
  3904. out_free_resources:
  3905. ioc->remove_host = 1;
  3906. mpt2sas_base_free_resources(ioc);
  3907. _base_release_memory_pools(ioc);
  3908. pci_set_drvdata(ioc->pdev, NULL);
  3909. kfree(ioc->cpu_msix_table);
  3910. if (ioc->is_warpdrive)
  3911. kfree(ioc->reply_post_host_index);
  3912. kfree(ioc->pd_handles);
  3913. kfree(ioc->blocking_handles);
  3914. kfree(ioc->tm_cmds.reply);
  3915. kfree(ioc->transport_cmds.reply);
  3916. kfree(ioc->scsih_cmds.reply);
  3917. kfree(ioc->config_cmds.reply);
  3918. kfree(ioc->base_cmds.reply);
  3919. kfree(ioc->port_enable_cmds.reply);
  3920. kfree(ioc->ctl_cmds.reply);
  3921. kfree(ioc->ctl_cmds.sense);
  3922. kfree(ioc->pfacts);
  3923. ioc->ctl_cmds.reply = NULL;
  3924. ioc->base_cmds.reply = NULL;
  3925. ioc->tm_cmds.reply = NULL;
  3926. ioc->scsih_cmds.reply = NULL;
  3927. ioc->transport_cmds.reply = NULL;
  3928. ioc->config_cmds.reply = NULL;
  3929. ioc->pfacts = NULL;
  3930. return r;
  3931. }
  3932. /**
  3933. * mpt2sas_base_detach - remove controller instance
  3934. * @ioc: per adapter object
  3935. *
  3936. * Return nothing.
  3937. */
  3938. void
  3939. mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
  3940. {
  3941. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3942. __func__));
  3943. mpt2sas_base_stop_watchdog(ioc);
  3944. mpt2sas_base_free_resources(ioc);
  3945. _base_release_memory_pools(ioc);
  3946. pci_set_drvdata(ioc->pdev, NULL);
  3947. kfree(ioc->cpu_msix_table);
  3948. if (ioc->is_warpdrive)
  3949. kfree(ioc->reply_post_host_index);
  3950. kfree(ioc->pd_handles);
  3951. kfree(ioc->blocking_handles);
  3952. kfree(ioc->pfacts);
  3953. kfree(ioc->ctl_cmds.reply);
  3954. kfree(ioc->ctl_cmds.sense);
  3955. kfree(ioc->base_cmds.reply);
  3956. kfree(ioc->port_enable_cmds.reply);
  3957. kfree(ioc->tm_cmds.reply);
  3958. kfree(ioc->transport_cmds.reply);
  3959. kfree(ioc->scsih_cmds.reply);
  3960. kfree(ioc->config_cmds.reply);
  3961. }
  3962. /**
  3963. * _base_reset_handler - reset callback handler (for base)
  3964. * @ioc: per adapter object
  3965. * @reset_phase: phase
  3966. *
  3967. * The handler for doing any required cleanup or initialization.
  3968. *
  3969. * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
  3970. * MPT2_IOC_DONE_RESET
  3971. *
  3972. * Return nothing.
  3973. */
  3974. static void
  3975. _base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
  3976. {
  3977. mpt2sas_scsih_reset_handler(ioc, reset_phase);
  3978. mpt2sas_ctl_reset_handler(ioc, reset_phase);
  3979. switch (reset_phase) {
  3980. case MPT2_IOC_PRE_RESET:
  3981. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  3982. "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
  3983. break;
  3984. case MPT2_IOC_AFTER_RESET:
  3985. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  3986. "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
  3987. if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
  3988. ioc->transport_cmds.status |= MPT2_CMD_RESET;
  3989. mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
  3990. complete(&ioc->transport_cmds.done);
  3991. }
  3992. if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
  3993. ioc->base_cmds.status |= MPT2_CMD_RESET;
  3994. mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
  3995. complete(&ioc->base_cmds.done);
  3996. }
  3997. if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
  3998. ioc->port_enable_failed = 1;
  3999. ioc->port_enable_cmds.status |= MPT2_CMD_RESET;
  4000. mpt2sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
  4001. if (ioc->is_driver_loading) {
  4002. ioc->start_scan_failed =
  4003. MPI2_IOCSTATUS_INTERNAL_ERROR;
  4004. ioc->start_scan = 0;
  4005. ioc->port_enable_cmds.status =
  4006. MPT2_CMD_NOT_USED;
  4007. } else
  4008. complete(&ioc->port_enable_cmds.done);
  4009. }
  4010. if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
  4011. ioc->config_cmds.status |= MPT2_CMD_RESET;
  4012. mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
  4013. ioc->config_cmds.smid = USHRT_MAX;
  4014. complete(&ioc->config_cmds.done);
  4015. }
  4016. break;
  4017. case MPT2_IOC_DONE_RESET:
  4018. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  4019. "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
  4020. break;
  4021. }
  4022. }
  4023. /**
  4024. * _wait_for_commands_to_complete - reset controller
  4025. * @ioc: Pointer to MPT_ADAPTER structure
  4026. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  4027. *
  4028. * This function waiting(3s) for all pending commands to complete
  4029. * prior to putting controller in reset.
  4030. */
  4031. static void
  4032. _wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  4033. {
  4034. u32 ioc_state;
  4035. unsigned long flags;
  4036. u16 i;
  4037. ioc->pending_io_count = 0;
  4038. if (sleep_flag != CAN_SLEEP)
  4039. return;
  4040. ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
  4041. if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
  4042. return;
  4043. /* pending command count */
  4044. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  4045. for (i = 0; i < ioc->scsiio_depth; i++)
  4046. if (ioc->scsi_lookup[i].cb_idx != 0xFF)
  4047. ioc->pending_io_count++;
  4048. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  4049. if (!ioc->pending_io_count)
  4050. return;
  4051. /* wait for pending commands to complete */
  4052. wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
  4053. }
  4054. /**
  4055. * mpt2sas_base_hard_reset_handler - reset controller
  4056. * @ioc: Pointer to MPT_ADAPTER structure
  4057. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  4058. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  4059. *
  4060. * Returns 0 for success, non-zero for failure.
  4061. */
  4062. int
  4063. mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
  4064. enum reset_type type)
  4065. {
  4066. int r;
  4067. unsigned long flags;
  4068. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
  4069. __func__));
  4070. if (ioc->pci_error_recovery) {
  4071. printk(MPT2SAS_ERR_FMT "%s: pci error recovery reset\n",
  4072. ioc->name, __func__);
  4073. r = 0;
  4074. goto out_unlocked;
  4075. }
  4076. if (mpt2sas_fwfault_debug)
  4077. mpt2sas_halt_firmware(ioc);
  4078. /* TODO - What we really should be doing is pulling
  4079. * out all the code associated with NO_SLEEP; its never used.
  4080. * That is legacy code from mpt fusion driver, ported over.
  4081. * I will leave this BUG_ON here for now till its been resolved.
  4082. */
  4083. BUG_ON(sleep_flag == NO_SLEEP);
  4084. /* wait for an active reset in progress to complete */
  4085. if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
  4086. do {
  4087. ssleep(1);
  4088. } while (ioc->shost_recovery == 1);
  4089. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
  4090. __func__));
  4091. return ioc->ioc_reset_in_progress_status;
  4092. }
  4093. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  4094. ioc->shost_recovery = 1;
  4095. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  4096. _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
  4097. _wait_for_commands_to_complete(ioc, sleep_flag);
  4098. _base_mask_interrupts(ioc);
  4099. r = _base_make_ioc_ready(ioc, sleep_flag, type);
  4100. if (r)
  4101. goto out;
  4102. _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
  4103. /* If this hard reset is called while port enable is active, then
  4104. * there is no reason to call make_ioc_operational
  4105. */
  4106. if (ioc->is_driver_loading && ioc->port_enable_failed) {
  4107. ioc->remove_host = 1;
  4108. r = -EFAULT;
  4109. goto out;
  4110. }
  4111. r = _base_make_ioc_operational(ioc, sleep_flag);
  4112. if (!r)
  4113. _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
  4114. out:
  4115. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %s\n",
  4116. ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
  4117. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  4118. ioc->ioc_reset_in_progress_status = r;
  4119. ioc->shost_recovery = 0;
  4120. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  4121. mutex_unlock(&ioc->reset_in_progress_mutex);
  4122. out_unlocked:
  4123. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
  4124. __func__));
  4125. return r;
  4126. }