mpt2sas_base.c 105 KB

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