mpt2sas_base.c 117 KB

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