mpt2sas_base.c 98 KB

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