mpt2sas_base.c 100 KB

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