mpt3sas_base.c 140 KB

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