mpt2sas_base.c 111 KB

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