mpt2sas_base.c 108 KB

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