mpt2sas_base.c 107 KB

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