mptscsih.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516
  1. /*
  2. * linux/drivers/message/fusion/mptscsih.c
  3. * For use with LSI PCI chip/adapter(s)
  4. * running LSI Fusion MPT (Message Passing Technology) firmware.
  5. *
  6. * Copyright (c) 1999-2008 LSI Corporation
  7. * (mailto:DL-MPTFusionLinux@lsi.com)
  8. *
  9. */
  10. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  11. /*
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; version 2 of the License.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  40. */
  41. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  42. #include <linux/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/init.h>
  45. #include <linux/errno.h>
  46. #include <linux/kdev_t.h>
  47. #include <linux/blkdev.h>
  48. #include <linux/delay.h> /* for mdelay */
  49. #include <linux/interrupt.h> /* needed for in_interrupt() proto */
  50. #include <linux/reboot.h> /* notifier code */
  51. #include <linux/workqueue.h>
  52. #include <scsi/scsi.h>
  53. #include <scsi/scsi_cmnd.h>
  54. #include <scsi/scsi_device.h>
  55. #include <scsi/scsi_host.h>
  56. #include <scsi/scsi_tcq.h>
  57. #include <scsi/scsi_dbg.h>
  58. #include "mptbase.h"
  59. #include "mptscsih.h"
  60. #include "lsi/mpi_log_sas.h"
  61. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  62. #define my_NAME "Fusion MPT SCSI Host driver"
  63. #define my_VERSION MPT_LINUX_VERSION_COMMON
  64. #define MYNAM "mptscsih"
  65. MODULE_AUTHOR(MODULEAUTHOR);
  66. MODULE_DESCRIPTION(my_NAME);
  67. MODULE_LICENSE("GPL");
  68. MODULE_VERSION(my_VERSION);
  69. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  70. /*
  71. * Other private/forward protos...
  72. */
  73. static struct scsi_cmnd * mptscsih_get_scsi_lookup(MPT_ADAPTER *ioc, int i);
  74. static struct scsi_cmnd * mptscsih_getclear_scsi_lookup(MPT_ADAPTER *ioc, int i);
  75. static void mptscsih_set_scsi_lookup(MPT_ADAPTER *ioc, int i, struct scsi_cmnd *scmd);
  76. static int SCPNT_TO_LOOKUP_IDX(MPT_ADAPTER *ioc, struct scsi_cmnd *scmd);
  77. int mptscsih_io_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
  78. static void mptscsih_report_queue_full(struct scsi_cmnd *sc, SCSIIOReply_t *pScsiReply, SCSIIORequest_t *pScsiReq);
  79. int mptscsih_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
  80. static int mptscsih_AddSGE(MPT_ADAPTER *ioc, struct scsi_cmnd *SCpnt,
  81. SCSIIORequest_t *pReq, int req_idx);
  82. static void mptscsih_freeChainBuffers(MPT_ADAPTER *ioc, int req_idx);
  83. static void mptscsih_copy_sense_data(struct scsi_cmnd *sc, MPT_SCSI_HOST *hd, MPT_FRAME_HDR *mf, SCSIIOReply_t *pScsiReply);
  84. static int mptscsih_tm_pending_wait(MPT_SCSI_HOST * hd);
  85. static int mptscsih_tm_wait_for_completion(MPT_SCSI_HOST * hd, ulong timeout );
  86. static int mptscsih_IssueTaskMgmt(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 id, int lun, int ctx2abort, ulong timeout);
  87. int mptscsih_ioc_reset(MPT_ADAPTER *ioc, int post_reset);
  88. int mptscsih_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply);
  89. int mptscsih_scandv_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
  90. static int mptscsih_do_cmd(MPT_SCSI_HOST *hd, INTERNAL_CMD *iocmd);
  91. static void mptscsih_synchronize_cache(MPT_SCSI_HOST *hd, VirtDevice *vdevice);
  92. void mptscsih_remove(struct pci_dev *);
  93. void mptscsih_shutdown(struct pci_dev *);
  94. #ifdef CONFIG_PM
  95. int mptscsih_suspend(struct pci_dev *pdev, pm_message_t state);
  96. int mptscsih_resume(struct pci_dev *pdev);
  97. #endif
  98. #define SNS_LEN(scp) SCSI_SENSE_BUFFERSIZE
  99. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  100. /**
  101. * mptscsih_add_sge - Place a simple SGE at address pAddr.
  102. * @pAddr: virtual address for SGE
  103. * @flagslength: SGE flags and data transfer length
  104. * @dma_addr: Physical address
  105. *
  106. * This routine places a MPT request frame back on the MPT adapter's
  107. * FreeQ.
  108. */
  109. static inline void
  110. mptscsih_add_sge(char *pAddr, u32 flagslength, dma_addr_t dma_addr)
  111. {
  112. if (sizeof(dma_addr_t) == sizeof(u64)) {
  113. SGESimple64_t *pSge = (SGESimple64_t *) pAddr;
  114. u32 tmp = dma_addr & 0xFFFFFFFF;
  115. pSge->FlagsLength = cpu_to_le32(flagslength);
  116. pSge->Address.Low = cpu_to_le32(tmp);
  117. tmp = (u32) ((u64)dma_addr >> 32);
  118. pSge->Address.High = cpu_to_le32(tmp);
  119. } else {
  120. SGESimple32_t *pSge = (SGESimple32_t *) pAddr;
  121. pSge->FlagsLength = cpu_to_le32(flagslength);
  122. pSge->Address = cpu_to_le32(dma_addr);
  123. }
  124. } /* mptscsih_add_sge() */
  125. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  126. /**
  127. * mptscsih_add_chain - Place a chain SGE at address pAddr.
  128. * @pAddr: virtual address for SGE
  129. * @next: nextChainOffset value (u32's)
  130. * @length: length of next SGL segment
  131. * @dma_addr: Physical address
  132. *
  133. * This routine places a MPT request frame back on the MPT adapter's
  134. * FreeQ.
  135. */
  136. static inline void
  137. mptscsih_add_chain(char *pAddr, u8 next, u16 length, dma_addr_t dma_addr)
  138. {
  139. if (sizeof(dma_addr_t) == sizeof(u64)) {
  140. SGEChain64_t *pChain = (SGEChain64_t *) pAddr;
  141. u32 tmp = dma_addr & 0xFFFFFFFF;
  142. pChain->Length = cpu_to_le16(length);
  143. pChain->Flags = MPI_SGE_FLAGS_CHAIN_ELEMENT | mpt_addr_size();
  144. pChain->NextChainOffset = next;
  145. pChain->Address.Low = cpu_to_le32(tmp);
  146. tmp = (u32) ((u64)dma_addr >> 32);
  147. pChain->Address.High = cpu_to_le32(tmp);
  148. } else {
  149. SGEChain32_t *pChain = (SGEChain32_t *) pAddr;
  150. pChain->Length = cpu_to_le16(length);
  151. pChain->Flags = MPI_SGE_FLAGS_CHAIN_ELEMENT | mpt_addr_size();
  152. pChain->NextChainOffset = next;
  153. pChain->Address = cpu_to_le32(dma_addr);
  154. }
  155. } /* mptscsih_add_chain() */
  156. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  157. /*
  158. * mptscsih_getFreeChainBuffer - Function to get a free chain
  159. * from the MPT_SCSI_HOST FreeChainQ.
  160. * @ioc: Pointer to MPT_ADAPTER structure
  161. * @req_idx: Index of the SCSI IO request frame. (output)
  162. *
  163. * return SUCCESS or FAILED
  164. */
  165. static inline int
  166. mptscsih_getFreeChainBuffer(MPT_ADAPTER *ioc, int *retIndex)
  167. {
  168. MPT_FRAME_HDR *chainBuf;
  169. unsigned long flags;
  170. int rc;
  171. int chain_idx;
  172. dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT "getFreeChainBuffer called\n",
  173. ioc->name));
  174. spin_lock_irqsave(&ioc->FreeQlock, flags);
  175. if (!list_empty(&ioc->FreeChainQ)) {
  176. int offset;
  177. chainBuf = list_entry(ioc->FreeChainQ.next, MPT_FRAME_HDR,
  178. u.frame.linkage.list);
  179. list_del(&chainBuf->u.frame.linkage.list);
  180. offset = (u8 *)chainBuf - (u8 *)ioc->ChainBuffer;
  181. chain_idx = offset / ioc->req_sz;
  182. rc = SUCCESS;
  183. dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  184. "getFreeChainBuffer chainBuf=%p ChainBuffer=%p offset=%d chain_idx=%d\n",
  185. ioc->name, chainBuf, ioc->ChainBuffer, offset, chain_idx));
  186. } else {
  187. rc = FAILED;
  188. chain_idx = MPT_HOST_NO_CHAIN;
  189. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT "getFreeChainBuffer failed\n",
  190. ioc->name));
  191. }
  192. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  193. *retIndex = chain_idx;
  194. return rc;
  195. } /* mptscsih_getFreeChainBuffer() */
  196. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  197. /*
  198. * mptscsih_AddSGE - Add a SGE (plus chain buffers) to the
  199. * SCSIIORequest_t Message Frame.
  200. * @ioc: Pointer to MPT_ADAPTER structure
  201. * @SCpnt: Pointer to scsi_cmnd structure
  202. * @pReq: Pointer to SCSIIORequest_t structure
  203. *
  204. * Returns ...
  205. */
  206. static int
  207. mptscsih_AddSGE(MPT_ADAPTER *ioc, struct scsi_cmnd *SCpnt,
  208. SCSIIORequest_t *pReq, int req_idx)
  209. {
  210. char *psge;
  211. char *chainSge;
  212. struct scatterlist *sg;
  213. int frm_sz;
  214. int sges_left, sg_done;
  215. int chain_idx = MPT_HOST_NO_CHAIN;
  216. int sgeOffset;
  217. int numSgeSlots, numSgeThisFrame;
  218. u32 sgflags, sgdir, thisxfer = 0;
  219. int chain_dma_off = 0;
  220. int newIndex;
  221. int ii;
  222. dma_addr_t v2;
  223. u32 RequestNB;
  224. sgdir = le32_to_cpu(pReq->Control) & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK;
  225. if (sgdir == MPI_SCSIIO_CONTROL_WRITE) {
  226. sgdir = MPT_TRANSFER_HOST_TO_IOC;
  227. } else {
  228. sgdir = MPT_TRANSFER_IOC_TO_HOST;
  229. }
  230. psge = (char *) &pReq->SGL;
  231. frm_sz = ioc->req_sz;
  232. /* Map the data portion, if any.
  233. * sges_left = 0 if no data transfer.
  234. */
  235. sges_left = scsi_dma_map(SCpnt);
  236. if (sges_left < 0)
  237. return FAILED;
  238. /* Handle the SG case.
  239. */
  240. sg = scsi_sglist(SCpnt);
  241. sg_done = 0;
  242. sgeOffset = sizeof(SCSIIORequest_t) - sizeof(SGE_IO_UNION);
  243. chainSge = NULL;
  244. /* Prior to entering this loop - the following must be set
  245. * current MF: sgeOffset (bytes)
  246. * chainSge (Null if original MF is not a chain buffer)
  247. * sg_done (num SGE done for this MF)
  248. */
  249. nextSGEset:
  250. numSgeSlots = ((frm_sz - sgeOffset) / (sizeof(u32) + sizeof(dma_addr_t)) );
  251. numSgeThisFrame = (sges_left < numSgeSlots) ? sges_left : numSgeSlots;
  252. sgflags = MPT_SGE_FLAGS_SIMPLE_ELEMENT | MPT_SGE_FLAGS_ADDRESSING | sgdir;
  253. /* Get first (num - 1) SG elements
  254. * Skip any SG entries with a length of 0
  255. * NOTE: at finish, sg and psge pointed to NEXT data/location positions
  256. */
  257. for (ii=0; ii < (numSgeThisFrame-1); ii++) {
  258. thisxfer = sg_dma_len(sg);
  259. if (thisxfer == 0) {
  260. sg = sg_next(sg); /* Get next SG element from the OS */
  261. sg_done++;
  262. continue;
  263. }
  264. v2 = sg_dma_address(sg);
  265. mptscsih_add_sge(psge, sgflags | thisxfer, v2);
  266. sg = sg_next(sg); /* Get next SG element from the OS */
  267. psge += (sizeof(u32) + sizeof(dma_addr_t));
  268. sgeOffset += (sizeof(u32) + sizeof(dma_addr_t));
  269. sg_done++;
  270. }
  271. if (numSgeThisFrame == sges_left) {
  272. /* Add last element, end of buffer and end of list flags.
  273. */
  274. sgflags |= MPT_SGE_FLAGS_LAST_ELEMENT |
  275. MPT_SGE_FLAGS_END_OF_BUFFER |
  276. MPT_SGE_FLAGS_END_OF_LIST;
  277. /* Add last SGE and set termination flags.
  278. * Note: Last SGE may have a length of 0 - which should be ok.
  279. */
  280. thisxfer = sg_dma_len(sg);
  281. v2 = sg_dma_address(sg);
  282. mptscsih_add_sge(psge, sgflags | thisxfer, v2);
  283. /*
  284. sg = sg_next(sg);
  285. psge += (sizeof(u32) + sizeof(dma_addr_t));
  286. */
  287. sgeOffset += (sizeof(u32) + sizeof(dma_addr_t));
  288. sg_done++;
  289. if (chainSge) {
  290. /* The current buffer is a chain buffer,
  291. * but there is not another one.
  292. * Update the chain element
  293. * Offset and Length fields.
  294. */
  295. mptscsih_add_chain((char *)chainSge, 0, sgeOffset, ioc->ChainBufferDMA + chain_dma_off);
  296. } else {
  297. /* The current buffer is the original MF
  298. * and there is no Chain buffer.
  299. */
  300. pReq->ChainOffset = 0;
  301. RequestNB = (((sgeOffset - 1) >> ioc->NBShiftFactor) + 1) & 0x03;
  302. dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  303. "Single Buffer RequestNB=%x, sgeOffset=%d\n", ioc->name, RequestNB, sgeOffset));
  304. ioc->RequestNB[req_idx] = RequestNB;
  305. }
  306. } else {
  307. /* At least one chain buffer is needed.
  308. * Complete the first MF
  309. * - last SGE element, set the LastElement bit
  310. * - set ChainOffset (words) for orig MF
  311. * (OR finish previous MF chain buffer)
  312. * - update MFStructPtr ChainIndex
  313. * - Populate chain element
  314. * Also
  315. * Loop until done.
  316. */
  317. dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT "SG: Chain Required! sg done %d\n",
  318. ioc->name, sg_done));
  319. /* Set LAST_ELEMENT flag for last non-chain element
  320. * in the buffer. Since psge points at the NEXT
  321. * SGE element, go back one SGE element, update the flags
  322. * and reset the pointer. (Note: sgflags & thisxfer are already
  323. * set properly).
  324. */
  325. if (sg_done) {
  326. u32 *ptmp = (u32 *) (psge - (sizeof(u32) + sizeof(dma_addr_t)));
  327. sgflags = le32_to_cpu(*ptmp);
  328. sgflags |= MPT_SGE_FLAGS_LAST_ELEMENT;
  329. *ptmp = cpu_to_le32(sgflags);
  330. }
  331. if (chainSge) {
  332. /* The current buffer is a chain buffer.
  333. * chainSge points to the previous Chain Element.
  334. * Update its chain element Offset and Length (must
  335. * include chain element size) fields.
  336. * Old chain element is now complete.
  337. */
  338. u8 nextChain = (u8) (sgeOffset >> 2);
  339. sgeOffset += (sizeof(u32) + sizeof(dma_addr_t));
  340. mptscsih_add_chain((char *)chainSge, nextChain, sgeOffset, ioc->ChainBufferDMA + chain_dma_off);
  341. } else {
  342. /* The original MF buffer requires a chain buffer -
  343. * set the offset.
  344. * Last element in this MF is a chain element.
  345. */
  346. pReq->ChainOffset = (u8) (sgeOffset >> 2);
  347. RequestNB = (((sgeOffset - 1) >> ioc->NBShiftFactor) + 1) & 0x03;
  348. dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Chain Buffer Needed, RequestNB=%x sgeOffset=%d\n", ioc->name, RequestNB, sgeOffset));
  349. ioc->RequestNB[req_idx] = RequestNB;
  350. }
  351. sges_left -= sg_done;
  352. /* NOTE: psge points to the beginning of the chain element
  353. * in current buffer. Get a chain buffer.
  354. */
  355. if ((mptscsih_getFreeChainBuffer(ioc, &newIndex)) == FAILED) {
  356. dfailprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  357. "getFreeChainBuffer FAILED SCSI cmd=%02x (%p)\n",
  358. ioc->name, pReq->CDB[0], SCpnt));
  359. return FAILED;
  360. }
  361. /* Update the tracking arrays.
  362. * If chainSge == NULL, update ReqToChain, else ChainToChain
  363. */
  364. if (chainSge) {
  365. ioc->ChainToChain[chain_idx] = newIndex;
  366. } else {
  367. ioc->ReqToChain[req_idx] = newIndex;
  368. }
  369. chain_idx = newIndex;
  370. chain_dma_off = ioc->req_sz * chain_idx;
  371. /* Populate the chainSGE for the current buffer.
  372. * - Set chain buffer pointer to psge and fill
  373. * out the Address and Flags fields.
  374. */
  375. chainSge = (char *) psge;
  376. dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT " Current buff @ %p (index 0x%x)",
  377. ioc->name, psge, req_idx));
  378. /* Start the SGE for the next buffer
  379. */
  380. psge = (char *) (ioc->ChainBuffer + chain_dma_off);
  381. sgeOffset = 0;
  382. sg_done = 0;
  383. dsgprintk(ioc, printk(MYIOC_s_DEBUG_FMT " Chain buff @ %p (index 0x%x)\n",
  384. ioc->name, psge, chain_idx));
  385. /* Start the SGE for the next buffer
  386. */
  387. goto nextSGEset;
  388. }
  389. return SUCCESS;
  390. } /* mptscsih_AddSGE() */
  391. static void
  392. mptscsih_issue_sep_command(MPT_ADAPTER *ioc, VirtTarget *vtarget,
  393. U32 SlotStatus)
  394. {
  395. MPT_FRAME_HDR *mf;
  396. SEPRequest_t *SEPMsg;
  397. if (ioc->bus_type != SAS)
  398. return;
  399. /* Not supported for hidden raid components
  400. */
  401. if (vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT)
  402. return;
  403. if ((mf = mpt_get_msg_frame(ioc->InternalCtx, ioc)) == NULL) {
  404. dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s: no msg frames!!\n",
  405. ioc->name,__func__));
  406. return;
  407. }
  408. SEPMsg = (SEPRequest_t *)mf;
  409. SEPMsg->Function = MPI_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
  410. SEPMsg->Bus = vtarget->channel;
  411. SEPMsg->TargetID = vtarget->id;
  412. SEPMsg->Action = MPI_SEP_REQ_ACTION_WRITE_STATUS;
  413. SEPMsg->SlotStatus = SlotStatus;
  414. devtverboseprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  415. "Sending SEP cmd=%x channel=%d id=%d\n",
  416. ioc->name, SlotStatus, SEPMsg->Bus, SEPMsg->TargetID));
  417. mpt_put_msg_frame(ioc->DoneCtx, ioc, mf);
  418. }
  419. #ifdef CONFIG_FUSION_LOGGING
  420. /**
  421. * mptscsih_info_scsiio - debug print info on reply frame
  422. * @ioc: Pointer to MPT_ADAPTER structure
  423. * @sc: original scsi cmnd pointer
  424. * @pScsiReply: Pointer to MPT reply frame
  425. *
  426. * MPT_DEBUG_REPLY needs to be enabled to obtain this info
  427. *
  428. * Refer to lsi/mpi.h.
  429. **/
  430. static void
  431. mptscsih_info_scsiio(MPT_ADAPTER *ioc, struct scsi_cmnd *sc, SCSIIOReply_t * pScsiReply)
  432. {
  433. char *desc = NULL;
  434. char *desc1 = NULL;
  435. u16 ioc_status;
  436. u8 skey, asc, ascq;
  437. ioc_status = le16_to_cpu(pScsiReply->IOCStatus) & MPI_IOCSTATUS_MASK;
  438. switch (ioc_status) {
  439. case MPI_IOCSTATUS_SUCCESS:
  440. desc = "success";
  441. break;
  442. case MPI_IOCSTATUS_SCSI_INVALID_BUS:
  443. desc = "invalid bus";
  444. break;
  445. case MPI_IOCSTATUS_SCSI_INVALID_TARGETID:
  446. desc = "invalid target_id";
  447. break;
  448. case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  449. desc = "device not there";
  450. break;
  451. case MPI_IOCSTATUS_SCSI_DATA_OVERRUN:
  452. desc = "data overrun";
  453. break;
  454. case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN:
  455. desc = "data underrun";
  456. break;
  457. case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR:
  458. desc = "I/O data error";
  459. break;
  460. case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  461. desc = "protocol error";
  462. break;
  463. case MPI_IOCSTATUS_SCSI_TASK_TERMINATED:
  464. desc = "task terminated";
  465. break;
  466. case MPI_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  467. desc = "residual mismatch";
  468. break;
  469. case MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  470. desc = "task management failed";
  471. break;
  472. case MPI_IOCSTATUS_SCSI_IOC_TERMINATED:
  473. desc = "IOC terminated";
  474. break;
  475. case MPI_IOCSTATUS_SCSI_EXT_TERMINATED:
  476. desc = "ext terminated";
  477. break;
  478. default:
  479. desc = "";
  480. break;
  481. }
  482. switch (pScsiReply->SCSIStatus)
  483. {
  484. case MPI_SCSI_STATUS_SUCCESS:
  485. desc1 = "success";
  486. break;
  487. case MPI_SCSI_STATUS_CHECK_CONDITION:
  488. desc1 = "check condition";
  489. break;
  490. case MPI_SCSI_STATUS_CONDITION_MET:
  491. desc1 = "condition met";
  492. break;
  493. case MPI_SCSI_STATUS_BUSY:
  494. desc1 = "busy";
  495. break;
  496. case MPI_SCSI_STATUS_INTERMEDIATE:
  497. desc1 = "intermediate";
  498. break;
  499. case MPI_SCSI_STATUS_INTERMEDIATE_CONDMET:
  500. desc1 = "intermediate condmet";
  501. break;
  502. case MPI_SCSI_STATUS_RESERVATION_CONFLICT:
  503. desc1 = "reservation conflict";
  504. break;
  505. case MPI_SCSI_STATUS_COMMAND_TERMINATED:
  506. desc1 = "command terminated";
  507. break;
  508. case MPI_SCSI_STATUS_TASK_SET_FULL:
  509. desc1 = "task set full";
  510. break;
  511. case MPI_SCSI_STATUS_ACA_ACTIVE:
  512. desc1 = "aca active";
  513. break;
  514. case MPI_SCSI_STATUS_FCPEXT_DEVICE_LOGGED_OUT:
  515. desc1 = "fcpext device logged out";
  516. break;
  517. case MPI_SCSI_STATUS_FCPEXT_NO_LINK:
  518. desc1 = "fcpext no link";
  519. break;
  520. case MPI_SCSI_STATUS_FCPEXT_UNASSIGNED:
  521. desc1 = "fcpext unassigned";
  522. break;
  523. default:
  524. desc1 = "";
  525. break;
  526. }
  527. scsi_print_command(sc);
  528. printk(MYIOC_s_DEBUG_FMT "\tfw_channel = %d, fw_id = %d\n",
  529. ioc->name, pScsiReply->Bus, pScsiReply->TargetID);
  530. printk(MYIOC_s_DEBUG_FMT "\trequest_len = %d, underflow = %d, "
  531. "resid = %d\n", ioc->name, scsi_bufflen(sc), sc->underflow,
  532. scsi_get_resid(sc));
  533. printk(MYIOC_s_DEBUG_FMT "\ttag = %d, transfer_count = %d, "
  534. "sc->result = %08X\n", ioc->name, le16_to_cpu(pScsiReply->TaskTag),
  535. le32_to_cpu(pScsiReply->TransferCount), sc->result);
  536. printk(MYIOC_s_DEBUG_FMT "\tiocstatus = %s (0x%04x), "
  537. "scsi_status = %s (0x%02x), scsi_state = (0x%02x)\n",
  538. ioc->name, desc, ioc_status, desc1, pScsiReply->SCSIStatus,
  539. pScsiReply->SCSIState);
  540. if (pScsiReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_VALID) {
  541. skey = sc->sense_buffer[2] & 0x0F;
  542. asc = sc->sense_buffer[12];
  543. ascq = sc->sense_buffer[13];
  544. printk(MYIOC_s_DEBUG_FMT "\t[sense_key,asc,ascq]: "
  545. "[0x%02x,0x%02x,0x%02x]\n", ioc->name, skey, asc, ascq);
  546. }
  547. /*
  548. * Look for + dump FCP ResponseInfo[]!
  549. */
  550. if (pScsiReply->SCSIState & MPI_SCSI_STATE_RESPONSE_INFO_VALID &&
  551. pScsiReply->ResponseInfo)
  552. printk(MYIOC_s_DEBUG_FMT "response_info = %08xh\n",
  553. ioc->name, le32_to_cpu(pScsiReply->ResponseInfo));
  554. }
  555. #endif
  556. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  557. /*
  558. * mptscsih_io_done - Main SCSI IO callback routine registered to
  559. * Fusion MPT (base) driver
  560. * @ioc: Pointer to MPT_ADAPTER structure
  561. * @mf: Pointer to original MPT request frame
  562. * @r: Pointer to MPT reply frame (NULL if TurboReply)
  563. *
  564. * This routine is called from mpt.c::mpt_interrupt() at the completion
  565. * of any SCSI IO request.
  566. * This routine is registered with the Fusion MPT (base) driver at driver
  567. * load/init time via the mpt_register() API call.
  568. *
  569. * Returns 1 indicating alloc'd request frame ptr should be freed.
  570. */
  571. int
  572. mptscsih_io_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
  573. {
  574. struct scsi_cmnd *sc;
  575. MPT_SCSI_HOST *hd;
  576. SCSIIORequest_t *pScsiReq;
  577. SCSIIOReply_t *pScsiReply;
  578. u16 req_idx, req_idx_MR;
  579. VirtDevice *vdevice;
  580. VirtTarget *vtarget;
  581. hd = shost_priv(ioc->sh);
  582. req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  583. req_idx_MR = (mr != NULL) ?
  584. le16_to_cpu(mr->u.frame.hwhdr.msgctxu.fld.req_idx) : req_idx;
  585. if ((req_idx != req_idx_MR) ||
  586. (mf->u.frame.linkage.arg1 == 0xdeadbeaf)) {
  587. printk(MYIOC_s_ERR_FMT "Received a mf that was already freed\n",
  588. ioc->name);
  589. printk (MYIOC_s_ERR_FMT
  590. "req_idx=%x req_idx_MR=%x mf=%p mr=%p sc=%p\n",
  591. ioc->name, req_idx, req_idx_MR, mf, mr,
  592. mptscsih_get_scsi_lookup(ioc, req_idx_MR));
  593. return 0;
  594. }
  595. sc = mptscsih_getclear_scsi_lookup(ioc, req_idx);
  596. if (sc == NULL) {
  597. MPIHeader_t *hdr = (MPIHeader_t *)mf;
  598. /* Remark: writeSDP1 will use the ScsiDoneCtx
  599. * If a SCSI I/O cmd, device disabled by OS and
  600. * completion done. Cannot touch sc struct. Just free mem.
  601. */
  602. if (hdr->Function == MPI_FUNCTION_SCSI_IO_REQUEST)
  603. printk(MYIOC_s_ERR_FMT "NULL ScsiCmd ptr!\n",
  604. ioc->name);
  605. mptscsih_freeChainBuffers(ioc, req_idx);
  606. return 1;
  607. }
  608. if ((unsigned char *)mf != sc->host_scribble) {
  609. mptscsih_freeChainBuffers(ioc, req_idx);
  610. return 1;
  611. }
  612. sc->host_scribble = NULL;
  613. sc->result = DID_OK << 16; /* Set default reply as OK */
  614. pScsiReq = (SCSIIORequest_t *) mf;
  615. pScsiReply = (SCSIIOReply_t *) mr;
  616. if((ioc->facts.MsgVersion >= MPI_VERSION_01_05) && pScsiReply){
  617. dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  618. "ScsiDone (mf=%p,mr=%p,sc=%p,idx=%d,task-tag=%d)\n",
  619. ioc->name, mf, mr, sc, req_idx, pScsiReply->TaskTag));
  620. }else{
  621. dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  622. "ScsiDone (mf=%p,mr=%p,sc=%p,idx=%d)\n",
  623. ioc->name, mf, mr, sc, req_idx));
  624. }
  625. if (pScsiReply == NULL) {
  626. /* special context reply handling */
  627. ;
  628. } else {
  629. u32 xfer_cnt;
  630. u16 status;
  631. u8 scsi_state, scsi_status;
  632. u32 log_info;
  633. status = le16_to_cpu(pScsiReply->IOCStatus) & MPI_IOCSTATUS_MASK;
  634. scsi_state = pScsiReply->SCSIState;
  635. scsi_status = pScsiReply->SCSIStatus;
  636. xfer_cnt = le32_to_cpu(pScsiReply->TransferCount);
  637. scsi_set_resid(sc, scsi_bufflen(sc) - xfer_cnt);
  638. log_info = le32_to_cpu(pScsiReply->IOCLogInfo);
  639. /*
  640. * if we get a data underrun indication, yet no data was
  641. * transferred and the SCSI status indicates that the
  642. * command was never started, change the data underrun
  643. * to success
  644. */
  645. if (status == MPI_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
  646. (scsi_status == MPI_SCSI_STATUS_BUSY ||
  647. scsi_status == MPI_SCSI_STATUS_RESERVATION_CONFLICT ||
  648. scsi_status == MPI_SCSI_STATUS_TASK_SET_FULL)) {
  649. status = MPI_IOCSTATUS_SUCCESS;
  650. }
  651. if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID)
  652. mptscsih_copy_sense_data(sc, hd, mf, pScsiReply);
  653. /*
  654. * Look for + dump FCP ResponseInfo[]!
  655. */
  656. if (scsi_state & MPI_SCSI_STATE_RESPONSE_INFO_VALID &&
  657. pScsiReply->ResponseInfo) {
  658. printk(MYIOC_s_NOTE_FMT "[%d:%d:%d:%d] "
  659. "FCP_ResponseInfo=%08xh\n", ioc->name,
  660. sc->device->host->host_no, sc->device->channel,
  661. sc->device->id, sc->device->lun,
  662. le32_to_cpu(pScsiReply->ResponseInfo));
  663. }
  664. switch(status) {
  665. case MPI_IOCSTATUS_BUSY: /* 0x0002 */
  666. /* CHECKME!
  667. * Maybe: DRIVER_BUSY | SUGGEST_RETRY | DID_SOFT_ERROR (retry)
  668. * But not: DID_BUS_BUSY lest one risk
  669. * killing interrupt handler:-(
  670. */
  671. sc->result = SAM_STAT_BUSY;
  672. break;
  673. case MPI_IOCSTATUS_SCSI_INVALID_BUS: /* 0x0041 */
  674. case MPI_IOCSTATUS_SCSI_INVALID_TARGETID: /* 0x0042 */
  675. sc->result = DID_BAD_TARGET << 16;
  676. break;
  677. case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE: /* 0x0043 */
  678. /* Spoof to SCSI Selection Timeout! */
  679. if (ioc->bus_type != FC)
  680. sc->result = DID_NO_CONNECT << 16;
  681. /* else fibre, just stall until rescan event */
  682. else
  683. sc->result = DID_REQUEUE << 16;
  684. if (hd->sel_timeout[pScsiReq->TargetID] < 0xFFFF)
  685. hd->sel_timeout[pScsiReq->TargetID]++;
  686. vdevice = sc->device->hostdata;
  687. if (!vdevice)
  688. break;
  689. vtarget = vdevice->vtarget;
  690. if (vtarget->tflags & MPT_TARGET_FLAGS_LED_ON) {
  691. mptscsih_issue_sep_command(ioc, vtarget,
  692. MPI_SEP_REQ_SLOTSTATUS_UNCONFIGURED);
  693. vtarget->tflags &= ~MPT_TARGET_FLAGS_LED_ON;
  694. }
  695. break;
  696. case MPI_IOCSTATUS_SCSI_IOC_TERMINATED: /* 0x004B */
  697. if ( ioc->bus_type == SAS ) {
  698. u16 ioc_status = le16_to_cpu(pScsiReply->IOCStatus);
  699. if (ioc_status & MPI_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
  700. if ((log_info & SAS_LOGINFO_MASK)
  701. == SAS_LOGINFO_NEXUS_LOSS) {
  702. sc->result = (DID_BUS_BUSY << 16);
  703. break;
  704. }
  705. }
  706. } else if (ioc->bus_type == FC) {
  707. /*
  708. * The FC IOC may kill a request for variety of
  709. * reasons, some of which may be recovered by a
  710. * retry, some which are unlikely to be
  711. * recovered. Return DID_ERROR instead of
  712. * DID_RESET to permit retry of the command,
  713. * just not an infinite number of them
  714. */
  715. sc->result = DID_ERROR << 16;
  716. break;
  717. }
  718. /*
  719. * Allow non-SAS & non-NEXUS_LOSS to drop into below code
  720. */
  721. case MPI_IOCSTATUS_SCSI_TASK_TERMINATED: /* 0x0048 */
  722. case MPI_IOCSTATUS_SCSI_EXT_TERMINATED: /* 0x004C */
  723. /* Linux handles an unsolicited DID_RESET better
  724. * than an unsolicited DID_ABORT.
  725. */
  726. sc->result = DID_RESET << 16;
  727. break;
  728. case MPI_IOCSTATUS_SCSI_RESIDUAL_MISMATCH: /* 0x0049 */
  729. scsi_set_resid(sc, scsi_bufflen(sc) - xfer_cnt);
  730. if((xfer_cnt==0)||(sc->underflow > xfer_cnt))
  731. sc->result=DID_SOFT_ERROR << 16;
  732. else /* Sufficient data transfer occurred */
  733. sc->result = (DID_OK << 16) | scsi_status;
  734. dreplyprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  735. "RESIDUAL_MISMATCH: result=%x on channel=%d id=%d\n",
  736. ioc->name, sc->result, sc->device->channel, sc->device->id));
  737. break;
  738. case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN: /* 0x0045 */
  739. /*
  740. * Do upfront check for valid SenseData and give it
  741. * precedence!
  742. */
  743. sc->result = (DID_OK << 16) | scsi_status;
  744. if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID) {
  745. /* Have already saved the status and sense data
  746. */
  747. ;
  748. } else {
  749. if (xfer_cnt < sc->underflow) {
  750. if (scsi_status == SAM_STAT_BUSY)
  751. sc->result = SAM_STAT_BUSY;
  752. else
  753. sc->result = DID_SOFT_ERROR << 16;
  754. }
  755. if (scsi_state & (MPI_SCSI_STATE_AUTOSENSE_FAILED | MPI_SCSI_STATE_NO_SCSI_STATUS)) {
  756. /* What to do?
  757. */
  758. sc->result = DID_SOFT_ERROR << 16;
  759. }
  760. else if (scsi_state & MPI_SCSI_STATE_TERMINATED) {
  761. /* Not real sure here either... */
  762. sc->result = DID_RESET << 16;
  763. }
  764. }
  765. dreplyprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  766. " sc->underflow={report ERR if < %02xh bytes xfer'd}\n",
  767. ioc->name, sc->underflow));
  768. dreplyprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  769. " ActBytesXferd=%02xh\n", ioc->name, xfer_cnt));
  770. /* Report Queue Full
  771. */
  772. if (scsi_status == MPI_SCSI_STATUS_TASK_SET_FULL)
  773. mptscsih_report_queue_full(sc, pScsiReply, pScsiReq);
  774. break;
  775. case MPI_IOCSTATUS_SCSI_DATA_OVERRUN: /* 0x0044 */
  776. scsi_set_resid(sc, 0);
  777. case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR: /* 0x0040 */
  778. case MPI_IOCSTATUS_SUCCESS: /* 0x0000 */
  779. sc->result = (DID_OK << 16) | scsi_status;
  780. if (scsi_state == 0) {
  781. ;
  782. } else if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID) {
  783. /*
  784. * If running against circa 200003dd 909 MPT f/w,
  785. * may get this (AUTOSENSE_VALID) for actual TASK_SET_FULL
  786. * (QUEUE_FULL) returned from device! --> get 0x0000?128
  787. * and with SenseBytes set to 0.
  788. */
  789. if (pScsiReply->SCSIStatus == MPI_SCSI_STATUS_TASK_SET_FULL)
  790. mptscsih_report_queue_full(sc, pScsiReply, pScsiReq);
  791. }
  792. else if (scsi_state &
  793. (MPI_SCSI_STATE_AUTOSENSE_FAILED | MPI_SCSI_STATE_NO_SCSI_STATUS)
  794. ) {
  795. /*
  796. * What to do?
  797. */
  798. sc->result = DID_SOFT_ERROR << 16;
  799. }
  800. else if (scsi_state & MPI_SCSI_STATE_TERMINATED) {
  801. /* Not real sure here either... */
  802. sc->result = DID_RESET << 16;
  803. }
  804. else if (scsi_state & MPI_SCSI_STATE_QUEUE_TAG_REJECTED) {
  805. /* Device Inq. data indicates that it supports
  806. * QTags, but rejects QTag messages.
  807. * This command completed OK.
  808. *
  809. * Not real sure here either so do nothing... */
  810. }
  811. if (sc->result == MPI_SCSI_STATUS_TASK_SET_FULL)
  812. mptscsih_report_queue_full(sc, pScsiReply, pScsiReq);
  813. /* Add handling of:
  814. * Reservation Conflict, Busy,
  815. * Command Terminated, CHECK
  816. */
  817. break;
  818. case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR: /* 0x0047 */
  819. sc->result = DID_SOFT_ERROR << 16;
  820. break;
  821. case MPI_IOCSTATUS_INVALID_FUNCTION: /* 0x0001 */
  822. case MPI_IOCSTATUS_INVALID_SGL: /* 0x0003 */
  823. case MPI_IOCSTATUS_INTERNAL_ERROR: /* 0x0004 */
  824. case MPI_IOCSTATUS_RESERVED: /* 0x0005 */
  825. case MPI_IOCSTATUS_INSUFFICIENT_RESOURCES: /* 0x0006 */
  826. case MPI_IOCSTATUS_INVALID_FIELD: /* 0x0007 */
  827. case MPI_IOCSTATUS_INVALID_STATE: /* 0x0008 */
  828. case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR: /* 0x0046 */
  829. case MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED: /* 0x004A */
  830. default:
  831. /*
  832. * What to do?
  833. */
  834. sc->result = DID_SOFT_ERROR << 16;
  835. break;
  836. } /* switch(status) */
  837. #ifdef CONFIG_FUSION_LOGGING
  838. if (sc->result && (ioc->debug_level & MPT_DEBUG_REPLY))
  839. mptscsih_info_scsiio(ioc, sc, pScsiReply);
  840. #endif
  841. } /* end of address reply case */
  842. /* Unmap the DMA buffers, if any. */
  843. scsi_dma_unmap(sc);
  844. sc->scsi_done(sc); /* Issue the command callback */
  845. /* Free Chain buffers */
  846. mptscsih_freeChainBuffers(ioc, req_idx);
  847. return 1;
  848. }
  849. /*
  850. * mptscsih_flush_running_cmds - For each command found, search
  851. * Scsi_Host instance taskQ and reply to OS.
  852. * Called only if recovering from a FW reload.
  853. * @hd: Pointer to a SCSI HOST structure
  854. *
  855. * Returns: None.
  856. *
  857. * Must be called while new I/Os are being queued.
  858. */
  859. static void
  860. mptscsih_flush_running_cmds(MPT_SCSI_HOST *hd)
  861. {
  862. MPT_ADAPTER *ioc = hd->ioc;
  863. struct scsi_cmnd *sc;
  864. SCSIIORequest_t *mf = NULL;
  865. int ii;
  866. int channel, id;
  867. for (ii= 0; ii < ioc->req_depth; ii++) {
  868. sc = mptscsih_getclear_scsi_lookup(ioc, ii);
  869. if (!sc)
  870. continue;
  871. mf = (SCSIIORequest_t *)MPT_INDEX_2_MFPTR(ioc, ii);
  872. if (!mf)
  873. continue;
  874. channel = mf->Bus;
  875. id = mf->TargetID;
  876. mptscsih_freeChainBuffers(ioc, ii);
  877. mpt_free_msg_frame(ioc, (MPT_FRAME_HDR *)mf);
  878. if ((unsigned char *)mf != sc->host_scribble)
  879. continue;
  880. scsi_dma_unmap(sc);
  881. sc->result = DID_RESET << 16;
  882. sc->host_scribble = NULL;
  883. sdev_printk(KERN_INFO, sc->device, MYIOC_s_FMT
  884. "completing cmds: fw_channel %d, fw_id %d, sc=%p,"
  885. " mf = %p, idx=%x\n", ioc->name, channel, id, sc, mf, ii);
  886. sc->scsi_done(sc);
  887. }
  888. }
  889. /*
  890. * mptscsih_search_running_cmds - Delete any commands associated
  891. * with the specified target and lun. Function called only
  892. * when a lun is disable by mid-layer.
  893. * Do NOT access the referenced scsi_cmnd structure or
  894. * members. Will cause either a paging or NULL ptr error.
  895. * (BUT, BUT, BUT, the code does reference it! - mdr)
  896. * @hd: Pointer to a SCSI HOST structure
  897. * @vdevice: per device private data
  898. *
  899. * Returns: None.
  900. *
  901. * Called from slave_destroy.
  902. */
  903. static void
  904. mptscsih_search_running_cmds(MPT_SCSI_HOST *hd, VirtDevice *vdevice)
  905. {
  906. SCSIIORequest_t *mf = NULL;
  907. int ii;
  908. struct scsi_cmnd *sc;
  909. struct scsi_lun lun;
  910. MPT_ADAPTER *ioc = hd->ioc;
  911. unsigned long flags;
  912. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  913. for (ii = 0; ii < ioc->req_depth; ii++) {
  914. if ((sc = ioc->ScsiLookup[ii]) != NULL) {
  915. mf = (SCSIIORequest_t *)MPT_INDEX_2_MFPTR(ioc, ii);
  916. if (mf == NULL)
  917. continue;
  918. /* If the device is a hidden raid component, then its
  919. * expected that the mf->function will be RAID_SCSI_IO
  920. */
  921. if (vdevice->vtarget->tflags &
  922. MPT_TARGET_FLAGS_RAID_COMPONENT && mf->Function !=
  923. MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)
  924. continue;
  925. int_to_scsilun(vdevice->lun, &lun);
  926. if ((mf->Bus != vdevice->vtarget->channel) ||
  927. (mf->TargetID != vdevice->vtarget->id) ||
  928. memcmp(lun.scsi_lun, mf->LUN, 8))
  929. continue;
  930. if ((unsigned char *)mf != sc->host_scribble)
  931. continue;
  932. ioc->ScsiLookup[ii] = NULL;
  933. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  934. mptscsih_freeChainBuffers(ioc, ii);
  935. mpt_free_msg_frame(ioc, (MPT_FRAME_HDR *)mf);
  936. scsi_dma_unmap(sc);
  937. sc->host_scribble = NULL;
  938. sc->result = DID_NO_CONNECT << 16;
  939. sdev_printk(KERN_INFO, sc->device, MYIOC_s_FMT "completing cmds: fw_channel %d,"
  940. "fw_id %d, sc=%p, mf = %p, idx=%x\n", ioc->name, vdevice->vtarget->channel,
  941. vdevice->vtarget->id, sc, mf, ii);
  942. sc->scsi_done(sc);
  943. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  944. }
  945. }
  946. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  947. return;
  948. }
  949. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  950. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  951. /*
  952. * mptscsih_report_queue_full - Report QUEUE_FULL status returned
  953. * from a SCSI target device.
  954. * @sc: Pointer to scsi_cmnd structure
  955. * @pScsiReply: Pointer to SCSIIOReply_t
  956. * @pScsiReq: Pointer to original SCSI request
  957. *
  958. * This routine periodically reports QUEUE_FULL status returned from a
  959. * SCSI target device. It reports this to the console via kernel
  960. * printk() API call, not more than once every 10 seconds.
  961. */
  962. static void
  963. mptscsih_report_queue_full(struct scsi_cmnd *sc, SCSIIOReply_t *pScsiReply, SCSIIORequest_t *pScsiReq)
  964. {
  965. long time = jiffies;
  966. MPT_SCSI_HOST *hd;
  967. MPT_ADAPTER *ioc;
  968. if (sc->device == NULL)
  969. return;
  970. if (sc->device->host == NULL)
  971. return;
  972. if ((hd = shost_priv(sc->device->host)) == NULL)
  973. return;
  974. ioc = hd->ioc;
  975. if (time - hd->last_queue_full > 10 * HZ) {
  976. dprintk(ioc, printk(MYIOC_s_WARN_FMT "Device (%d:%d:%d) reported QUEUE_FULL!\n",
  977. ioc->name, 0, sc->device->id, sc->device->lun));
  978. hd->last_queue_full = time;
  979. }
  980. }
  981. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  982. /*
  983. * mptscsih_remove - Removed scsi devices
  984. * @pdev: Pointer to pci_dev structure
  985. *
  986. *
  987. */
  988. void
  989. mptscsih_remove(struct pci_dev *pdev)
  990. {
  991. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  992. struct Scsi_Host *host = ioc->sh;
  993. MPT_SCSI_HOST *hd;
  994. int sz1;
  995. if(!host) {
  996. mpt_detach(pdev);
  997. return;
  998. }
  999. scsi_remove_host(host);
  1000. if((hd = shost_priv(host)) == NULL)
  1001. return;
  1002. mptscsih_shutdown(pdev);
  1003. sz1=0;
  1004. if (ioc->ScsiLookup != NULL) {
  1005. sz1 = ioc->req_depth * sizeof(void *);
  1006. kfree(ioc->ScsiLookup);
  1007. ioc->ScsiLookup = NULL;
  1008. }
  1009. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1010. "Free'd ScsiLookup (%d) memory\n",
  1011. ioc->name, sz1));
  1012. kfree(hd->info_kbuf);
  1013. /* NULL the Scsi_Host pointer
  1014. */
  1015. ioc->sh = NULL;
  1016. scsi_host_put(host);
  1017. mpt_detach(pdev);
  1018. }
  1019. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1020. /*
  1021. * mptscsih_shutdown - reboot notifier
  1022. *
  1023. */
  1024. void
  1025. mptscsih_shutdown(struct pci_dev *pdev)
  1026. {
  1027. }
  1028. #ifdef CONFIG_PM
  1029. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1030. /*
  1031. * mptscsih_suspend - Fusion MPT scsi driver suspend routine.
  1032. *
  1033. *
  1034. */
  1035. int
  1036. mptscsih_suspend(struct pci_dev *pdev, pm_message_t state)
  1037. {
  1038. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  1039. scsi_block_requests(ioc->sh);
  1040. flush_scheduled_work();
  1041. mptscsih_shutdown(pdev);
  1042. return mpt_suspend(pdev,state);
  1043. }
  1044. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1045. /*
  1046. * mptscsih_resume - Fusion MPT scsi driver resume routine.
  1047. *
  1048. *
  1049. */
  1050. int
  1051. mptscsih_resume(struct pci_dev *pdev)
  1052. {
  1053. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  1054. int rc;
  1055. rc = mpt_resume(pdev);
  1056. scsi_unblock_requests(ioc->sh);
  1057. return rc;
  1058. }
  1059. #endif
  1060. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1061. /**
  1062. * mptscsih_info - Return information about MPT adapter
  1063. * @SChost: Pointer to Scsi_Host structure
  1064. *
  1065. * (linux scsi_host_template.info routine)
  1066. *
  1067. * Returns pointer to buffer where information was written.
  1068. */
  1069. const char *
  1070. mptscsih_info(struct Scsi_Host *SChost)
  1071. {
  1072. MPT_SCSI_HOST *h;
  1073. int size = 0;
  1074. h = shost_priv(SChost);
  1075. if (h) {
  1076. if (h->info_kbuf == NULL)
  1077. if ((h->info_kbuf = kmalloc(0x1000 /* 4Kb */, GFP_KERNEL)) == NULL)
  1078. return h->info_kbuf;
  1079. h->info_kbuf[0] = '\0';
  1080. mpt_print_ioc_summary(h->ioc, h->info_kbuf, &size, 0, 0);
  1081. h->info_kbuf[size-1] = '\0';
  1082. }
  1083. return h->info_kbuf;
  1084. }
  1085. struct info_str {
  1086. char *buffer;
  1087. int length;
  1088. int offset;
  1089. int pos;
  1090. };
  1091. static void
  1092. mptscsih_copy_mem_info(struct info_str *info, char *data, int len)
  1093. {
  1094. if (info->pos + len > info->length)
  1095. len = info->length - info->pos;
  1096. if (info->pos + len < info->offset) {
  1097. info->pos += len;
  1098. return;
  1099. }
  1100. if (info->pos < info->offset) {
  1101. data += (info->offset - info->pos);
  1102. len -= (info->offset - info->pos);
  1103. }
  1104. if (len > 0) {
  1105. memcpy(info->buffer + info->pos, data, len);
  1106. info->pos += len;
  1107. }
  1108. }
  1109. static int
  1110. mptscsih_copy_info(struct info_str *info, char *fmt, ...)
  1111. {
  1112. va_list args;
  1113. char buf[81];
  1114. int len;
  1115. va_start(args, fmt);
  1116. len = vsprintf(buf, fmt, args);
  1117. va_end(args);
  1118. mptscsih_copy_mem_info(info, buf, len);
  1119. return len;
  1120. }
  1121. static int
  1122. mptscsih_host_info(MPT_ADAPTER *ioc, char *pbuf, off_t offset, int len)
  1123. {
  1124. struct info_str info;
  1125. info.buffer = pbuf;
  1126. info.length = len;
  1127. info.offset = offset;
  1128. info.pos = 0;
  1129. mptscsih_copy_info(&info, "%s: %s, ", ioc->name, ioc->prod_name);
  1130. mptscsih_copy_info(&info, "%s%08xh, ", MPT_FW_REV_MAGIC_ID_STRING, ioc->facts.FWVersion.Word);
  1131. mptscsih_copy_info(&info, "Ports=%d, ", ioc->facts.NumberOfPorts);
  1132. mptscsih_copy_info(&info, "MaxQ=%d\n", ioc->req_depth);
  1133. return ((info.pos > info.offset) ? info.pos - info.offset : 0);
  1134. }
  1135. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1136. /**
  1137. * mptscsih_proc_info - Return information about MPT adapter
  1138. * @host: scsi host struct
  1139. * @buffer: if write, user data; if read, buffer for user
  1140. * @start: returns the buffer address
  1141. * @offset: if write, 0; if read, the current offset into the buffer from
  1142. * the previous read.
  1143. * @length: if write, return length;
  1144. * @func: write = 1; read = 0
  1145. *
  1146. * (linux scsi_host_template.info routine)
  1147. */
  1148. int
  1149. mptscsih_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
  1150. int length, int func)
  1151. {
  1152. MPT_SCSI_HOST *hd = shost_priv(host);
  1153. MPT_ADAPTER *ioc = hd->ioc;
  1154. int size = 0;
  1155. if (func) {
  1156. /*
  1157. * write is not supported
  1158. */
  1159. } else {
  1160. if (start)
  1161. *start = buffer;
  1162. size = mptscsih_host_info(ioc, buffer, offset, length);
  1163. }
  1164. return size;
  1165. }
  1166. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1167. #define ADD_INDEX_LOG(req_ent) do { } while(0)
  1168. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1169. /**
  1170. * mptscsih_qcmd - Primary Fusion MPT SCSI initiator IO start routine.
  1171. * @SCpnt: Pointer to scsi_cmnd structure
  1172. * @done: Pointer SCSI mid-layer IO completion function
  1173. *
  1174. * (linux scsi_host_template.queuecommand routine)
  1175. * This is the primary SCSI IO start routine. Create a MPI SCSIIORequest
  1176. * from a linux scsi_cmnd request and send it to the IOC.
  1177. *
  1178. * Returns 0. (rtn value discarded by linux scsi mid-layer)
  1179. */
  1180. int
  1181. mptscsih_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  1182. {
  1183. MPT_SCSI_HOST *hd;
  1184. MPT_FRAME_HDR *mf;
  1185. SCSIIORequest_t *pScsiReq;
  1186. VirtDevice *vdevice = SCpnt->device->hostdata;
  1187. int lun;
  1188. u32 datalen;
  1189. u32 scsictl;
  1190. u32 scsidir;
  1191. u32 cmd_len;
  1192. int my_idx;
  1193. int ii;
  1194. MPT_ADAPTER *ioc;
  1195. hd = shost_priv(SCpnt->device->host);
  1196. ioc = hd->ioc;
  1197. lun = SCpnt->device->lun;
  1198. SCpnt->scsi_done = done;
  1199. dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT "qcmd: SCpnt=%p, done()=%p\n",
  1200. ioc->name, SCpnt, done));
  1201. if (hd->resetPending) {
  1202. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT "qcmd: SCpnt=%p timeout + 60HZ\n",
  1203. ioc->name, SCpnt));
  1204. return SCSI_MLQUEUE_HOST_BUSY;
  1205. }
  1206. /*
  1207. * Put together a MPT SCSI request...
  1208. */
  1209. if ((mf = mpt_get_msg_frame(ioc->DoneCtx, ioc)) == NULL) {
  1210. dprintk(ioc, printk(MYIOC_s_WARN_FMT "QueueCmd, no msg frames!!\n",
  1211. ioc->name));
  1212. return SCSI_MLQUEUE_HOST_BUSY;
  1213. }
  1214. pScsiReq = (SCSIIORequest_t *) mf;
  1215. my_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  1216. ADD_INDEX_LOG(my_idx);
  1217. /* TUR's being issued with scsictl=0x02000000 (DATA_IN)!
  1218. * Seems we may receive a buffer (datalen>0) even when there
  1219. * will be no data transfer! GRRRRR...
  1220. */
  1221. if (SCpnt->sc_data_direction == DMA_FROM_DEVICE) {
  1222. datalen = scsi_bufflen(SCpnt);
  1223. scsidir = MPI_SCSIIO_CONTROL_READ; /* DATA IN (host<--ioc<--dev) */
  1224. } else if (SCpnt->sc_data_direction == DMA_TO_DEVICE) {
  1225. datalen = scsi_bufflen(SCpnt);
  1226. scsidir = MPI_SCSIIO_CONTROL_WRITE; /* DATA OUT (host-->ioc-->dev) */
  1227. } else {
  1228. datalen = 0;
  1229. scsidir = MPI_SCSIIO_CONTROL_NODATATRANSFER;
  1230. }
  1231. /* Default to untagged. Once a target structure has been allocated,
  1232. * use the Inquiry data to determine if device supports tagged.
  1233. */
  1234. if (vdevice
  1235. && (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_Q_YES)
  1236. && (SCpnt->device->tagged_supported)) {
  1237. scsictl = scsidir | MPI_SCSIIO_CONTROL_SIMPLEQ;
  1238. } else {
  1239. scsictl = scsidir | MPI_SCSIIO_CONTROL_UNTAGGED;
  1240. }
  1241. /* Use the above information to set up the message frame
  1242. */
  1243. pScsiReq->TargetID = (u8) vdevice->vtarget->id;
  1244. pScsiReq->Bus = vdevice->vtarget->channel;
  1245. pScsiReq->ChainOffset = 0;
  1246. if (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT)
  1247. pScsiReq->Function = MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
  1248. else
  1249. pScsiReq->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
  1250. pScsiReq->CDBLength = SCpnt->cmd_len;
  1251. pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
  1252. pScsiReq->Reserved = 0;
  1253. pScsiReq->MsgFlags = mpt_msg_flags();
  1254. int_to_scsilun(SCpnt->device->lun, (struct scsi_lun *)pScsiReq->LUN);
  1255. pScsiReq->Control = cpu_to_le32(scsictl);
  1256. /*
  1257. * Write SCSI CDB into the message
  1258. */
  1259. cmd_len = SCpnt->cmd_len;
  1260. for (ii=0; ii < cmd_len; ii++)
  1261. pScsiReq->CDB[ii] = SCpnt->cmnd[ii];
  1262. for (ii=cmd_len; ii < 16; ii++)
  1263. pScsiReq->CDB[ii] = 0;
  1264. /* DataLength */
  1265. pScsiReq->DataLength = cpu_to_le32(datalen);
  1266. /* SenseBuffer low address */
  1267. pScsiReq->SenseBufferLowAddr = cpu_to_le32(ioc->sense_buf_low_dma
  1268. + (my_idx * MPT_SENSE_BUFFER_ALLOC));
  1269. /* Now add the SG list
  1270. * Always have a SGE even if null length.
  1271. */
  1272. if (datalen == 0) {
  1273. /* Add a NULL SGE */
  1274. mptscsih_add_sge((char *)&pScsiReq->SGL, MPT_SGE_FLAGS_SSIMPLE_READ | 0,
  1275. (dma_addr_t) -1);
  1276. } else {
  1277. /* Add a 32 or 64 bit SGE */
  1278. if (mptscsih_AddSGE(ioc, SCpnt, pScsiReq, my_idx) != SUCCESS)
  1279. goto fail;
  1280. }
  1281. SCpnt->host_scribble = (unsigned char *)mf;
  1282. mptscsih_set_scsi_lookup(ioc, my_idx, SCpnt);
  1283. mpt_put_msg_frame(ioc->DoneCtx, ioc, mf);
  1284. dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Issued SCSI cmd (%p) mf=%p idx=%d\n",
  1285. ioc->name, SCpnt, mf, my_idx));
  1286. DBG_DUMP_REQUEST_FRAME(ioc, (u32 *)mf);
  1287. return 0;
  1288. fail:
  1289. mptscsih_freeChainBuffers(ioc, my_idx);
  1290. mpt_free_msg_frame(ioc, mf);
  1291. return SCSI_MLQUEUE_HOST_BUSY;
  1292. }
  1293. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1294. /*
  1295. * mptscsih_freeChainBuffers - Function to free chain buffers associated
  1296. * with a SCSI IO request
  1297. * @hd: Pointer to the MPT_SCSI_HOST instance
  1298. * @req_idx: Index of the SCSI IO request frame.
  1299. *
  1300. * Called if SG chain buffer allocation fails and mptscsih callbacks.
  1301. * No return.
  1302. */
  1303. static void
  1304. mptscsih_freeChainBuffers(MPT_ADAPTER *ioc, int req_idx)
  1305. {
  1306. MPT_FRAME_HDR *chain;
  1307. unsigned long flags;
  1308. int chain_idx;
  1309. int next;
  1310. /* Get the first chain index and reset
  1311. * tracker state.
  1312. */
  1313. chain_idx = ioc->ReqToChain[req_idx];
  1314. ioc->ReqToChain[req_idx] = MPT_HOST_NO_CHAIN;
  1315. while (chain_idx != MPT_HOST_NO_CHAIN) {
  1316. /* Save the next chain buffer index */
  1317. next = ioc->ChainToChain[chain_idx];
  1318. /* Free this chain buffer and reset
  1319. * tracker
  1320. */
  1321. ioc->ChainToChain[chain_idx] = MPT_HOST_NO_CHAIN;
  1322. chain = (MPT_FRAME_HDR *) (ioc->ChainBuffer
  1323. + (chain_idx * ioc->req_sz));
  1324. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1325. list_add_tail(&chain->u.frame.linkage.list, &ioc->FreeChainQ);
  1326. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1327. dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT "FreeChainBuffers (index %d)\n",
  1328. ioc->name, chain_idx));
  1329. /* handle next */
  1330. chain_idx = next;
  1331. }
  1332. return;
  1333. }
  1334. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1335. /*
  1336. * Reset Handling
  1337. */
  1338. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1339. /**
  1340. * mptscsih_TMHandler - Generic handler for SCSI Task Management.
  1341. * @hd: Pointer to MPT SCSI HOST structure
  1342. * @type: Task Management type
  1343. * @channel: channel number for task management
  1344. * @id: Logical Target ID for reset (if appropriate)
  1345. * @lun: Logical Unit for reset (if appropriate)
  1346. * @ctx2abort: Context for the task to be aborted (if appropriate)
  1347. * @timeout: timeout for task management control
  1348. *
  1349. * Fall through to mpt_HardResetHandler if: not operational, too many
  1350. * failed TM requests or handshake failure.
  1351. *
  1352. * Remark: Currently invoked from a non-interrupt thread (_bh).
  1353. *
  1354. * Note: With old EH code, at most 1 SCSI TaskMgmt function per IOC
  1355. * will be active.
  1356. *
  1357. * Returns 0 for SUCCESS, or %FAILED.
  1358. **/
  1359. int
  1360. mptscsih_TMHandler(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 id, int lun, int ctx2abort, ulong timeout)
  1361. {
  1362. MPT_ADAPTER *ioc;
  1363. int rc = -1;
  1364. u32 ioc_raw_state;
  1365. unsigned long flags;
  1366. ioc = hd->ioc;
  1367. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TMHandler Entered!\n", ioc->name));
  1368. // SJR - CHECKME - Can we avoid this here?
  1369. // (mpt_HardResetHandler has this check...)
  1370. spin_lock_irqsave(&ioc->diagLock, flags);
  1371. if ((ioc->diagPending) || (ioc->alt_ioc && ioc->alt_ioc->diagPending)) {
  1372. spin_unlock_irqrestore(&ioc->diagLock, flags);
  1373. return FAILED;
  1374. }
  1375. spin_unlock_irqrestore(&ioc->diagLock, flags);
  1376. /* Wait a fixed amount of time for the TM pending flag to be cleared.
  1377. * If we time out and not bus reset, then we return a FAILED status
  1378. * to the caller.
  1379. * The call to mptscsih_tm_pending_wait() will set the pending flag
  1380. * if we are
  1381. * successful. Otherwise, reload the FW.
  1382. */
  1383. if (mptscsih_tm_pending_wait(hd) == FAILED) {
  1384. if (type == MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK) {
  1385. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TMHandler abort: "
  1386. "Timed out waiting for last TM (%d) to complete! \n",
  1387. ioc->name, hd->tmPending));
  1388. return FAILED;
  1389. } else if (type == MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET) {
  1390. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TMHandler target "
  1391. "reset: Timed out waiting for last TM (%d) "
  1392. "to complete! \n", ioc->name,
  1393. hd->tmPending));
  1394. return FAILED;
  1395. } else if (type == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS) {
  1396. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TMHandler bus reset: "
  1397. "Timed out waiting for last TM (%d) to complete! \n",
  1398. ioc->name, hd->tmPending));
  1399. return FAILED;
  1400. }
  1401. } else {
  1402. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1403. hd->tmPending |= (1 << type);
  1404. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1405. }
  1406. ioc_raw_state = mpt_GetIocState(ioc, 0);
  1407. if ((ioc_raw_state & MPI_IOC_STATE_MASK) != MPI_IOC_STATE_OPERATIONAL) {
  1408. printk(MYIOC_s_WARN_FMT
  1409. "TM Handler for type=%x: IOC Not operational (0x%x)!\n",
  1410. ioc->name, type, ioc_raw_state);
  1411. printk(MYIOC_s_WARN_FMT " Issuing HardReset!!\n", ioc->name);
  1412. if (mpt_HardResetHandler(ioc, CAN_SLEEP) < 0)
  1413. printk(MYIOC_s_WARN_FMT "TMHandler: HardReset "
  1414. "FAILED!!\n", ioc->name);
  1415. return FAILED;
  1416. }
  1417. if (ioc_raw_state & MPI_DOORBELL_ACTIVE) {
  1418. printk(MYIOC_s_WARN_FMT
  1419. "TM Handler for type=%x: ioc_state: "
  1420. "DOORBELL_ACTIVE (0x%x)!\n",
  1421. ioc->name, type, ioc_raw_state);
  1422. return FAILED;
  1423. }
  1424. /* Isse the Task Mgmt request.
  1425. */
  1426. if (hd->hard_resets < -1)
  1427. hd->hard_resets++;
  1428. rc = mptscsih_IssueTaskMgmt(hd, type, channel, id, lun,
  1429. ctx2abort, timeout);
  1430. if (rc)
  1431. printk(MYIOC_s_INFO_FMT "Issue of TaskMgmt failed!\n",
  1432. ioc->name);
  1433. else
  1434. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Issue of TaskMgmt Successful!\n",
  1435. ioc->name));
  1436. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1437. "TMHandler rc = %d!\n", ioc->name, rc));
  1438. return rc;
  1439. }
  1440. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1441. /**
  1442. * mptscsih_IssueTaskMgmt - Generic send Task Management function.
  1443. * @hd: Pointer to MPT_SCSI_HOST structure
  1444. * @type: Task Management type
  1445. * @channel: channel number for task management
  1446. * @id: Logical Target ID for reset (if appropriate)
  1447. * @lun: Logical Unit for reset (if appropriate)
  1448. * @ctx2abort: Context for the task to be aborted (if appropriate)
  1449. * @timeout: timeout for task management control
  1450. *
  1451. * Remark: _HardResetHandler can be invoked from an interrupt thread (timer)
  1452. * or a non-interrupt thread. In the former, must not call schedule().
  1453. *
  1454. * Not all fields are meaningfull for all task types.
  1455. *
  1456. * Returns 0 for SUCCESS, or FAILED.
  1457. *
  1458. **/
  1459. static int
  1460. mptscsih_IssueTaskMgmt(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 id, int lun, int ctx2abort, ulong timeout)
  1461. {
  1462. MPT_FRAME_HDR *mf;
  1463. SCSITaskMgmt_t *pScsiTm;
  1464. int ii;
  1465. int retval;
  1466. MPT_ADAPTER *ioc = hd->ioc;
  1467. /* Return Fail to calling function if no message frames available.
  1468. */
  1469. if ((mf = mpt_get_msg_frame(ioc->TaskCtx, ioc)) == NULL) {
  1470. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT "IssueTaskMgmt, no msg frames!!\n",
  1471. ioc->name));
  1472. return FAILED;
  1473. }
  1474. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "IssueTaskMgmt request @ %p\n",
  1475. ioc->name, mf));
  1476. /* Format the Request
  1477. */
  1478. pScsiTm = (SCSITaskMgmt_t *) mf;
  1479. pScsiTm->TargetID = id;
  1480. pScsiTm->Bus = channel;
  1481. pScsiTm->ChainOffset = 0;
  1482. pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
  1483. pScsiTm->Reserved = 0;
  1484. pScsiTm->TaskType = type;
  1485. pScsiTm->Reserved1 = 0;
  1486. pScsiTm->MsgFlags = (type == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS)
  1487. ? MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION : 0;
  1488. int_to_scsilun(lun, (struct scsi_lun *)pScsiTm->LUN);
  1489. for (ii=0; ii < 7; ii++)
  1490. pScsiTm->Reserved2[ii] = 0;
  1491. pScsiTm->TaskMsgContext = ctx2abort;
  1492. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "IssueTaskMgmt: ctx2abort (0x%08x) "
  1493. "type=%d\n", ioc->name, ctx2abort, type));
  1494. DBG_DUMP_TM_REQUEST_FRAME(ioc, (u32 *)pScsiTm);
  1495. if ((ioc->facts.IOCCapabilities & MPI_IOCFACTS_CAPABILITY_HIGH_PRI_Q) &&
  1496. (ioc->facts.MsgVersion >= MPI_VERSION_01_05))
  1497. mpt_put_msg_frame_hi_pri(ioc->TaskCtx, ioc, mf);
  1498. else {
  1499. retval = mpt_send_handshake_request(ioc->TaskCtx, ioc,
  1500. sizeof(SCSITaskMgmt_t), (u32*)pScsiTm, CAN_SLEEP);
  1501. if (retval) {
  1502. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT "send_handshake FAILED!"
  1503. " (hd %p, ioc %p, mf %p, rc=%d) \n", ioc->name, hd,
  1504. ioc, mf, retval));
  1505. goto fail_out;
  1506. }
  1507. }
  1508. if(mptscsih_tm_wait_for_completion(hd, timeout) == FAILED) {
  1509. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT "task management request TIMED OUT!"
  1510. " (hd %p, ioc %p, mf %p) \n", ioc->name, hd,
  1511. ioc, mf));
  1512. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Calling HardReset! \n",
  1513. ioc->name));
  1514. retval = mpt_HardResetHandler(ioc, CAN_SLEEP);
  1515. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "rc=%d \n",
  1516. ioc->name, retval));
  1517. goto fail_out;
  1518. }
  1519. /*
  1520. * Handle success case, see if theres a non-zero ioc_status.
  1521. */
  1522. if (hd->tm_iocstatus == MPI_IOCSTATUS_SUCCESS ||
  1523. hd->tm_iocstatus == MPI_IOCSTATUS_SCSI_TASK_TERMINATED ||
  1524. hd->tm_iocstatus == MPI_IOCSTATUS_SCSI_IOC_TERMINATED)
  1525. retval = 0;
  1526. else
  1527. retval = FAILED;
  1528. return retval;
  1529. fail_out:
  1530. /*
  1531. * Free task management mf, and corresponding tm flags
  1532. */
  1533. mpt_free_msg_frame(ioc, mf);
  1534. hd->tmPending = 0;
  1535. hd->tmState = TM_STATE_NONE;
  1536. return FAILED;
  1537. }
  1538. static int
  1539. mptscsih_get_tm_timeout(MPT_ADAPTER *ioc)
  1540. {
  1541. switch (ioc->bus_type) {
  1542. case FC:
  1543. return 40;
  1544. case SAS:
  1545. return 10;
  1546. case SPI:
  1547. default:
  1548. return 2;
  1549. }
  1550. }
  1551. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1552. /**
  1553. * mptscsih_abort - Abort linux scsi_cmnd routine, new_eh variant
  1554. * @SCpnt: Pointer to scsi_cmnd structure, IO to be aborted
  1555. *
  1556. * (linux scsi_host_template.eh_abort_handler routine)
  1557. *
  1558. * Returns SUCCESS or FAILED.
  1559. **/
  1560. int
  1561. mptscsih_abort(struct scsi_cmnd * SCpnt)
  1562. {
  1563. MPT_SCSI_HOST *hd;
  1564. MPT_FRAME_HDR *mf;
  1565. u32 ctx2abort;
  1566. int scpnt_idx;
  1567. int retval;
  1568. VirtDevice *vdevice;
  1569. ulong sn = SCpnt->serial_number;
  1570. MPT_ADAPTER *ioc;
  1571. /* If we can't locate our host adapter structure, return FAILED status.
  1572. */
  1573. if ((hd = shost_priv(SCpnt->device->host)) == NULL) {
  1574. SCpnt->result = DID_RESET << 16;
  1575. SCpnt->scsi_done(SCpnt);
  1576. printk(KERN_ERR MYNAM ": task abort: "
  1577. "can't locate host! (sc=%p)\n", SCpnt);
  1578. return FAILED;
  1579. }
  1580. ioc = hd->ioc;
  1581. printk(MYIOC_s_INFO_FMT "attempting task abort! (sc=%p)\n",
  1582. ioc->name, SCpnt);
  1583. scsi_print_command(SCpnt);
  1584. vdevice = SCpnt->device->hostdata;
  1585. if (!vdevice || !vdevice->vtarget) {
  1586. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1587. "task abort: device has been deleted (sc=%p)\n",
  1588. ioc->name, SCpnt));
  1589. SCpnt->result = DID_NO_CONNECT << 16;
  1590. SCpnt->scsi_done(SCpnt);
  1591. retval = 0;
  1592. goto out;
  1593. }
  1594. /* Task aborts are not supported for hidden raid components.
  1595. */
  1596. if (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1597. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1598. "task abort: hidden raid component (sc=%p)\n",
  1599. ioc->name, SCpnt));
  1600. SCpnt->result = DID_RESET << 16;
  1601. retval = FAILED;
  1602. goto out;
  1603. }
  1604. /* Find this command
  1605. */
  1606. if ((scpnt_idx = SCPNT_TO_LOOKUP_IDX(ioc, SCpnt)) < 0) {
  1607. /* Cmd not found in ScsiLookup.
  1608. * Do OS callback.
  1609. */
  1610. SCpnt->result = DID_RESET << 16;
  1611. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "task abort: "
  1612. "Command not in the active list! (sc=%p)\n", ioc->name,
  1613. SCpnt));
  1614. retval = 0;
  1615. goto out;
  1616. }
  1617. if (hd->resetPending) {
  1618. retval = FAILED;
  1619. goto out;
  1620. }
  1621. if (hd->timeouts < -1)
  1622. hd->timeouts++;
  1623. /* Most important! Set TaskMsgContext to SCpnt's MsgContext!
  1624. * (the IO to be ABORT'd)
  1625. *
  1626. * NOTE: Since we do not byteswap MsgContext, we do not
  1627. * swap it here either. It is an opaque cookie to
  1628. * the controller, so it does not matter. -DaveM
  1629. */
  1630. mf = MPT_INDEX_2_MFPTR(ioc, scpnt_idx);
  1631. ctx2abort = mf->u.frame.hwhdr.msgctxu.MsgContext;
  1632. hd->abortSCpnt = SCpnt;
  1633. retval = mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
  1634. vdevice->vtarget->channel, vdevice->vtarget->id, vdevice->lun,
  1635. ctx2abort, mptscsih_get_tm_timeout(ioc));
  1636. if (SCPNT_TO_LOOKUP_IDX(ioc, SCpnt) == scpnt_idx &&
  1637. SCpnt->serial_number == sn)
  1638. retval = FAILED;
  1639. out:
  1640. printk(MYIOC_s_INFO_FMT "task abort: %s (sc=%p)\n",
  1641. ioc->name, ((retval == 0) ? "SUCCESS" : "FAILED" ), SCpnt);
  1642. if (retval == 0)
  1643. return SUCCESS;
  1644. else
  1645. return FAILED;
  1646. }
  1647. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1648. /**
  1649. * mptscsih_dev_reset - Perform a SCSI TARGET_RESET! new_eh variant
  1650. * @SCpnt: Pointer to scsi_cmnd structure, IO which reset is due to
  1651. *
  1652. * (linux scsi_host_template.eh_dev_reset_handler routine)
  1653. *
  1654. * Returns SUCCESS or FAILED.
  1655. **/
  1656. int
  1657. mptscsih_dev_reset(struct scsi_cmnd * SCpnt)
  1658. {
  1659. MPT_SCSI_HOST *hd;
  1660. int retval;
  1661. VirtDevice *vdevice;
  1662. MPT_ADAPTER *ioc;
  1663. /* If we can't locate our host adapter structure, return FAILED status.
  1664. */
  1665. if ((hd = shost_priv(SCpnt->device->host)) == NULL){
  1666. printk(KERN_ERR MYNAM ": target reset: "
  1667. "Can't locate host! (sc=%p)\n", SCpnt);
  1668. return FAILED;
  1669. }
  1670. ioc = hd->ioc;
  1671. printk(MYIOC_s_INFO_FMT "attempting target reset! (sc=%p)\n",
  1672. ioc->name, SCpnt);
  1673. scsi_print_command(SCpnt);
  1674. if (hd->resetPending) {
  1675. retval = FAILED;
  1676. goto out;
  1677. }
  1678. vdevice = SCpnt->device->hostdata;
  1679. if (!vdevice || !vdevice->vtarget) {
  1680. retval = 0;
  1681. goto out;
  1682. }
  1683. /* Target reset to hidden raid component is not supported
  1684. */
  1685. if (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1686. retval = FAILED;
  1687. goto out;
  1688. }
  1689. retval = mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
  1690. vdevice->vtarget->channel, vdevice->vtarget->id, 0, 0,
  1691. mptscsih_get_tm_timeout(ioc));
  1692. out:
  1693. printk (MYIOC_s_INFO_FMT "target reset: %s (sc=%p)\n",
  1694. ioc->name, ((retval == 0) ? "SUCCESS" : "FAILED" ), SCpnt);
  1695. if (retval == 0)
  1696. return SUCCESS;
  1697. else
  1698. return FAILED;
  1699. }
  1700. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1701. /**
  1702. * mptscsih_bus_reset - Perform a SCSI BUS_RESET! new_eh variant
  1703. * @SCpnt: Pointer to scsi_cmnd structure, IO which reset is due to
  1704. *
  1705. * (linux scsi_host_template.eh_bus_reset_handler routine)
  1706. *
  1707. * Returns SUCCESS or FAILED.
  1708. **/
  1709. int
  1710. mptscsih_bus_reset(struct scsi_cmnd * SCpnt)
  1711. {
  1712. MPT_SCSI_HOST *hd;
  1713. int retval;
  1714. VirtDevice *vdevice;
  1715. MPT_ADAPTER *ioc;
  1716. /* If we can't locate our host adapter structure, return FAILED status.
  1717. */
  1718. if ((hd = shost_priv(SCpnt->device->host)) == NULL){
  1719. printk(KERN_ERR MYNAM ": bus reset: "
  1720. "Can't locate host! (sc=%p)\n", SCpnt);
  1721. return FAILED;
  1722. }
  1723. ioc = hd->ioc;
  1724. printk(MYIOC_s_INFO_FMT "attempting bus reset! (sc=%p)\n",
  1725. ioc->name, SCpnt);
  1726. scsi_print_command(SCpnt);
  1727. if (hd->timeouts < -1)
  1728. hd->timeouts++;
  1729. vdevice = SCpnt->device->hostdata;
  1730. retval = mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,
  1731. vdevice->vtarget->channel, 0, 0, 0, mptscsih_get_tm_timeout(ioc));
  1732. printk(MYIOC_s_INFO_FMT "bus reset: %s (sc=%p)\n",
  1733. ioc->name, ((retval == 0) ? "SUCCESS" : "FAILED" ), SCpnt);
  1734. if (retval == 0)
  1735. return SUCCESS;
  1736. else
  1737. return FAILED;
  1738. }
  1739. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1740. /**
  1741. * mptscsih_host_reset - Perform a SCSI host adapter RESET (new_eh variant)
  1742. * @SCpnt: Pointer to scsi_cmnd structure, IO which reset is due to
  1743. *
  1744. * (linux scsi_host_template.eh_host_reset_handler routine)
  1745. *
  1746. * Returns SUCCESS or FAILED.
  1747. */
  1748. int
  1749. mptscsih_host_reset(struct scsi_cmnd *SCpnt)
  1750. {
  1751. MPT_SCSI_HOST * hd;
  1752. int retval;
  1753. MPT_ADAPTER *ioc;
  1754. /* If we can't locate the host to reset, then we failed. */
  1755. if ((hd = shost_priv(SCpnt->device->host)) == NULL){
  1756. printk(KERN_ERR MYNAM ": host reset: "
  1757. "Can't locate host! (sc=%p)\n", SCpnt);
  1758. return FAILED;
  1759. }
  1760. ioc = hd->ioc;
  1761. printk(MYIOC_s_INFO_FMT "attempting host reset! (sc=%p)\n",
  1762. ioc->name, SCpnt);
  1763. /* If our attempts to reset the host failed, then return a failed
  1764. * status. The host will be taken off line by the SCSI mid-layer.
  1765. */
  1766. if (mpt_HardResetHandler(ioc, CAN_SLEEP) < 0) {
  1767. retval = FAILED;
  1768. } else {
  1769. /* Make sure TM pending is cleared and TM state is set to
  1770. * NONE.
  1771. */
  1772. retval = 0;
  1773. hd->tmPending = 0;
  1774. hd->tmState = TM_STATE_NONE;
  1775. }
  1776. printk(MYIOC_s_INFO_FMT "host reset: %s (sc=%p)\n",
  1777. ioc->name, ((retval == 0) ? "SUCCESS" : "FAILED" ), SCpnt);
  1778. return retval;
  1779. }
  1780. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1781. /**
  1782. * mptscsih_tm_pending_wait - wait for pending task management request to complete
  1783. * @hd: Pointer to MPT host structure.
  1784. *
  1785. * Returns {SUCCESS,FAILED}.
  1786. */
  1787. static int
  1788. mptscsih_tm_pending_wait(MPT_SCSI_HOST * hd)
  1789. {
  1790. unsigned long flags;
  1791. int loop_count = 4 * 10; /* Wait 10 seconds */
  1792. int status = FAILED;
  1793. MPT_ADAPTER *ioc = hd->ioc;
  1794. do {
  1795. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1796. if (hd->tmState == TM_STATE_NONE) {
  1797. hd->tmState = TM_STATE_IN_PROGRESS;
  1798. hd->tmPending = 1;
  1799. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1800. status = SUCCESS;
  1801. break;
  1802. }
  1803. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1804. msleep(250);
  1805. } while (--loop_count);
  1806. return status;
  1807. }
  1808. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1809. /**
  1810. * mptscsih_tm_wait_for_completion - wait for completion of TM task
  1811. * @hd: Pointer to MPT host structure.
  1812. * @timeout: timeout value
  1813. *
  1814. * Returns {SUCCESS,FAILED}.
  1815. */
  1816. static int
  1817. mptscsih_tm_wait_for_completion(MPT_SCSI_HOST * hd, ulong timeout )
  1818. {
  1819. unsigned long flags;
  1820. int loop_count = 4 * timeout;
  1821. int status = FAILED;
  1822. MPT_ADAPTER *ioc = hd->ioc;
  1823. do {
  1824. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1825. if(hd->tmPending == 0) {
  1826. status = SUCCESS;
  1827. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1828. break;
  1829. }
  1830. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1831. msleep(250);
  1832. } while (--loop_count);
  1833. return status;
  1834. }
  1835. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1836. static void
  1837. mptscsih_taskmgmt_response_code(MPT_ADAPTER *ioc, u8 response_code)
  1838. {
  1839. char *desc;
  1840. switch (response_code) {
  1841. case MPI_SCSITASKMGMT_RSP_TM_COMPLETE:
  1842. desc = "The task completed.";
  1843. break;
  1844. case MPI_SCSITASKMGMT_RSP_INVALID_FRAME:
  1845. desc = "The IOC received an invalid frame status.";
  1846. break;
  1847. case MPI_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
  1848. desc = "The task type is not supported.";
  1849. break;
  1850. case MPI_SCSITASKMGMT_RSP_TM_FAILED:
  1851. desc = "The requested task failed.";
  1852. break;
  1853. case MPI_SCSITASKMGMT_RSP_TM_SUCCEEDED:
  1854. desc = "The task completed successfully.";
  1855. break;
  1856. case MPI_SCSITASKMGMT_RSP_TM_INVALID_LUN:
  1857. desc = "The LUN request is invalid.";
  1858. break;
  1859. case MPI_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
  1860. desc = "The task is in the IOC queue and has not been sent to target.";
  1861. break;
  1862. default:
  1863. desc = "unknown";
  1864. break;
  1865. }
  1866. printk(MYIOC_s_INFO_FMT "Response Code(0x%08x): F/W: %s\n",
  1867. ioc->name, response_code, desc);
  1868. }
  1869. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1870. /**
  1871. * mptscsih_taskmgmt_complete - Registered with Fusion MPT base driver
  1872. * @ioc: Pointer to MPT_ADAPTER structure
  1873. * @mf: Pointer to SCSI task mgmt request frame
  1874. * @mr: Pointer to SCSI task mgmt reply frame
  1875. *
  1876. * This routine is called from mptbase.c::mpt_interrupt() at the completion
  1877. * of any SCSI task management request.
  1878. * This routine is registered with the MPT (base) driver at driver
  1879. * load/init time via the mpt_register() API call.
  1880. *
  1881. * Returns 1 indicating alloc'd request frame ptr should be freed.
  1882. **/
  1883. int
  1884. mptscsih_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
  1885. {
  1886. SCSITaskMgmtReply_t *pScsiTmReply;
  1887. SCSITaskMgmt_t *pScsiTmReq;
  1888. MPT_SCSI_HOST *hd;
  1889. unsigned long flags;
  1890. u16 iocstatus;
  1891. u8 tmType;
  1892. u32 termination_count;
  1893. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TaskMgmt completed (mf=%p,mr=%p)\n",
  1894. ioc->name, mf, mr));
  1895. if (!ioc->sh) {
  1896. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT
  1897. "TaskMgmt Complete: NULL Scsi Host Ptr\n", ioc->name));
  1898. return 1;
  1899. }
  1900. if (mr == NULL) {
  1901. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT
  1902. "ERROR! TaskMgmt Reply: NULL Request %p\n", ioc->name, mf));
  1903. return 1;
  1904. }
  1905. hd = shost_priv(ioc->sh);
  1906. pScsiTmReply = (SCSITaskMgmtReply_t*)mr;
  1907. pScsiTmReq = (SCSITaskMgmt_t*)mf;
  1908. tmType = pScsiTmReq->TaskType;
  1909. iocstatus = le16_to_cpu(pScsiTmReply->IOCStatus) & MPI_IOCSTATUS_MASK;
  1910. termination_count = le32_to_cpu(pScsiTmReply->TerminationCount);
  1911. if (ioc->facts.MsgVersion >= MPI_VERSION_01_05 &&
  1912. pScsiTmReply->ResponseCode)
  1913. mptscsih_taskmgmt_response_code(ioc,
  1914. pScsiTmReply->ResponseCode);
  1915. DBG_DUMP_TM_REPLY_FRAME(ioc, (u32 *)pScsiTmReply);
  1916. #ifdef CONFIG_FUSION_LOGGING
  1917. if ((ioc->debug_level & MPT_DEBUG_REPLY) ||
  1918. (ioc->debug_level & MPT_DEBUG_TM ))
  1919. printk("%s: ha=%d [%d:%d:0] task_type=0x%02X "
  1920. "iocstatus=0x%04X\n\tloginfo=0x%08X response_code=0x%02X "
  1921. "term_cmnds=%d\n", __func__, ioc->id, pScsiTmReply->Bus,
  1922. pScsiTmReply->TargetID, pScsiTmReq->TaskType,
  1923. le16_to_cpu(pScsiTmReply->IOCStatus),
  1924. le32_to_cpu(pScsiTmReply->IOCLogInfo),pScsiTmReply->ResponseCode,
  1925. le32_to_cpu(pScsiTmReply->TerminationCount));
  1926. #endif
  1927. if (!iocstatus) {
  1928. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT " TaskMgmt SUCCESS\n", ioc->name));
  1929. hd->abortSCpnt = NULL;
  1930. goto out;
  1931. }
  1932. /* Error? (anything non-zero?) */
  1933. /* clear flags and continue.
  1934. */
  1935. switch (tmType) {
  1936. case MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
  1937. if (termination_count == 1)
  1938. iocstatus = MPI_IOCSTATUS_SCSI_TASK_TERMINATED;
  1939. hd->abortSCpnt = NULL;
  1940. break;
  1941. case MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS:
  1942. /* If an internal command is present
  1943. * or the TM failed - reload the FW.
  1944. * FC FW may respond FAILED to an ABORT
  1945. */
  1946. if (iocstatus == MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED ||
  1947. hd->cmdPtr)
  1948. if (mpt_HardResetHandler(ioc, NO_SLEEP) < 0)
  1949. printk(MYIOC_s_WARN_FMT " Firmware Reload FAILED!!\n", ioc->name);
  1950. break;
  1951. case MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
  1952. default:
  1953. break;
  1954. }
  1955. out:
  1956. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1957. hd->tmPending = 0;
  1958. hd->tmState = TM_STATE_NONE;
  1959. hd->tm_iocstatus = iocstatus;
  1960. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1961. return 1;
  1962. }
  1963. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1964. /*
  1965. * This is anyones guess quite frankly.
  1966. */
  1967. int
  1968. mptscsih_bios_param(struct scsi_device * sdev, struct block_device *bdev,
  1969. sector_t capacity, int geom[])
  1970. {
  1971. int heads;
  1972. int sectors;
  1973. sector_t cylinders;
  1974. ulong dummy;
  1975. heads = 64;
  1976. sectors = 32;
  1977. dummy = heads * sectors;
  1978. cylinders = capacity;
  1979. sector_div(cylinders,dummy);
  1980. /*
  1981. * Handle extended translation size for logical drives
  1982. * > 1Gb
  1983. */
  1984. if ((ulong)capacity >= 0x200000) {
  1985. heads = 255;
  1986. sectors = 63;
  1987. dummy = heads * sectors;
  1988. cylinders = capacity;
  1989. sector_div(cylinders,dummy);
  1990. }
  1991. /* return result */
  1992. geom[0] = heads;
  1993. geom[1] = sectors;
  1994. geom[2] = cylinders;
  1995. return 0;
  1996. }
  1997. /* Search IOC page 3 to determine if this is hidden physical disk
  1998. *
  1999. */
  2000. int
  2001. mptscsih_is_phys_disk(MPT_ADAPTER *ioc, u8 channel, u8 id)
  2002. {
  2003. struct inactive_raid_component_info *component_info;
  2004. int i;
  2005. int rc = 0;
  2006. if (!ioc->raid_data.pIocPg3)
  2007. goto out;
  2008. for (i = 0; i < ioc->raid_data.pIocPg3->NumPhysDisks; i++) {
  2009. if ((id == ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskID) &&
  2010. (channel == ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskBus)) {
  2011. rc = 1;
  2012. goto out;
  2013. }
  2014. }
  2015. /*
  2016. * Check inactive list for matching phys disks
  2017. */
  2018. if (list_empty(&ioc->raid_data.inactive_list))
  2019. goto out;
  2020. mutex_lock(&ioc->raid_data.inactive_list_mutex);
  2021. list_for_each_entry(component_info, &ioc->raid_data.inactive_list,
  2022. list) {
  2023. if ((component_info->d.PhysDiskID == id) &&
  2024. (component_info->d.PhysDiskBus == channel))
  2025. rc = 1;
  2026. }
  2027. mutex_unlock(&ioc->raid_data.inactive_list_mutex);
  2028. out:
  2029. return rc;
  2030. }
  2031. EXPORT_SYMBOL(mptscsih_is_phys_disk);
  2032. u8
  2033. mptscsih_raid_id_to_num(MPT_ADAPTER *ioc, u8 channel, u8 id)
  2034. {
  2035. struct inactive_raid_component_info *component_info;
  2036. int i;
  2037. int rc = -ENXIO;
  2038. if (!ioc->raid_data.pIocPg3)
  2039. goto out;
  2040. for (i = 0; i < ioc->raid_data.pIocPg3->NumPhysDisks; i++) {
  2041. if ((id == ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskID) &&
  2042. (channel == ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskBus)) {
  2043. rc = ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskNum;
  2044. goto out;
  2045. }
  2046. }
  2047. /*
  2048. * Check inactive list for matching phys disks
  2049. */
  2050. if (list_empty(&ioc->raid_data.inactive_list))
  2051. goto out;
  2052. mutex_lock(&ioc->raid_data.inactive_list_mutex);
  2053. list_for_each_entry(component_info, &ioc->raid_data.inactive_list,
  2054. list) {
  2055. if ((component_info->d.PhysDiskID == id) &&
  2056. (component_info->d.PhysDiskBus == channel))
  2057. rc = component_info->d.PhysDiskNum;
  2058. }
  2059. mutex_unlock(&ioc->raid_data.inactive_list_mutex);
  2060. out:
  2061. return rc;
  2062. }
  2063. EXPORT_SYMBOL(mptscsih_raid_id_to_num);
  2064. /*
  2065. * OS entry point to allow for host driver to free allocated memory
  2066. * Called if no device present or device being unloaded
  2067. */
  2068. void
  2069. mptscsih_slave_destroy(struct scsi_device *sdev)
  2070. {
  2071. struct Scsi_Host *host = sdev->host;
  2072. MPT_SCSI_HOST *hd = shost_priv(host);
  2073. VirtTarget *vtarget;
  2074. VirtDevice *vdevice;
  2075. struct scsi_target *starget;
  2076. starget = scsi_target(sdev);
  2077. vtarget = starget->hostdata;
  2078. vdevice = sdev->hostdata;
  2079. mptscsih_search_running_cmds(hd, vdevice);
  2080. vtarget->num_luns--;
  2081. mptscsih_synchronize_cache(hd, vdevice);
  2082. kfree(vdevice);
  2083. sdev->hostdata = NULL;
  2084. }
  2085. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2086. /*
  2087. * mptscsih_change_queue_depth - This function will set a devices queue depth
  2088. * @sdev: per scsi_device pointer
  2089. * @qdepth: requested queue depth
  2090. *
  2091. * Adding support for new 'change_queue_depth' api.
  2092. */
  2093. int
  2094. mptscsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
  2095. {
  2096. MPT_SCSI_HOST *hd = shost_priv(sdev->host);
  2097. VirtTarget *vtarget;
  2098. struct scsi_target *starget;
  2099. int max_depth;
  2100. int tagged;
  2101. MPT_ADAPTER *ioc = hd->ioc;
  2102. starget = scsi_target(sdev);
  2103. vtarget = starget->hostdata;
  2104. if (ioc->bus_type == SPI) {
  2105. if (!(vtarget->tflags & MPT_TARGET_FLAGS_Q_YES))
  2106. max_depth = 1;
  2107. else if (sdev->type == TYPE_DISK &&
  2108. vtarget->minSyncFactor <= MPT_ULTRA160)
  2109. max_depth = MPT_SCSI_CMD_PER_DEV_HIGH;
  2110. else
  2111. max_depth = MPT_SCSI_CMD_PER_DEV_LOW;
  2112. } else
  2113. max_depth = MPT_SCSI_CMD_PER_DEV_HIGH;
  2114. if (qdepth > max_depth)
  2115. qdepth = max_depth;
  2116. if (qdepth == 1)
  2117. tagged = 0;
  2118. else
  2119. tagged = MSG_SIMPLE_TAG;
  2120. scsi_adjust_queue_depth(sdev, tagged, qdepth);
  2121. return sdev->queue_depth;
  2122. }
  2123. /*
  2124. * OS entry point to adjust the queue_depths on a per-device basis.
  2125. * Called once per device the bus scan. Use it to force the queue_depth
  2126. * member to 1 if a device does not support Q tags.
  2127. * Return non-zero if fails.
  2128. */
  2129. int
  2130. mptscsih_slave_configure(struct scsi_device *sdev)
  2131. {
  2132. struct Scsi_Host *sh = sdev->host;
  2133. VirtTarget *vtarget;
  2134. VirtDevice *vdevice;
  2135. struct scsi_target *starget;
  2136. MPT_SCSI_HOST *hd = shost_priv(sh);
  2137. MPT_ADAPTER *ioc = hd->ioc;
  2138. starget = scsi_target(sdev);
  2139. vtarget = starget->hostdata;
  2140. vdevice = sdev->hostdata;
  2141. dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2142. "device @ %p, channel=%d, id=%d, lun=%d\n",
  2143. ioc->name, sdev, sdev->channel, sdev->id, sdev->lun));
  2144. if (ioc->bus_type == SPI)
  2145. dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2146. "sdtr %d wdtr %d ppr %d inq length=%d\n",
  2147. ioc->name, sdev->sdtr, sdev->wdtr,
  2148. sdev->ppr, sdev->inquiry_len));
  2149. vdevice->configured_lun = 1;
  2150. mptscsih_change_queue_depth(sdev, MPT_SCSI_CMD_PER_DEV_HIGH);
  2151. dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2152. "Queue depth=%d, tflags=%x\n",
  2153. ioc->name, sdev->queue_depth, vtarget->tflags));
  2154. if (ioc->bus_type == SPI)
  2155. dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2156. "negoFlags=%x, maxOffset=%x, SyncFactor=%x\n",
  2157. ioc->name, vtarget->negoFlags, vtarget->maxOffset,
  2158. vtarget->minSyncFactor));
  2159. dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2160. "tagged %d, simple %d, ordered %d\n",
  2161. ioc->name,sdev->tagged_supported, sdev->simple_tags,
  2162. sdev->ordered_tags));
  2163. return 0;
  2164. }
  2165. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2166. /*
  2167. * Private routines...
  2168. */
  2169. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2170. /* Utility function to copy sense data from the scsi_cmnd buffer
  2171. * to the FC and SCSI target structures.
  2172. *
  2173. */
  2174. static void
  2175. mptscsih_copy_sense_data(struct scsi_cmnd *sc, MPT_SCSI_HOST *hd, MPT_FRAME_HDR *mf, SCSIIOReply_t *pScsiReply)
  2176. {
  2177. VirtDevice *vdevice;
  2178. SCSIIORequest_t *pReq;
  2179. u32 sense_count = le32_to_cpu(pScsiReply->SenseCount);
  2180. MPT_ADAPTER *ioc = hd->ioc;
  2181. /* Get target structure
  2182. */
  2183. pReq = (SCSIIORequest_t *) mf;
  2184. vdevice = sc->device->hostdata;
  2185. if (sense_count) {
  2186. u8 *sense_data;
  2187. int req_index;
  2188. /* Copy the sense received into the scsi command block. */
  2189. req_index = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  2190. sense_data = ((u8 *)ioc->sense_buf_pool + (req_index * MPT_SENSE_BUFFER_ALLOC));
  2191. memcpy(sc->sense_buffer, sense_data, SNS_LEN(sc));
  2192. /* Log SMART data (asc = 0x5D, non-IM case only) if required.
  2193. */
  2194. if ((ioc->events) && (ioc->eventTypes & (1 << MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE))) {
  2195. if ((sense_data[12] == 0x5D) && (vdevice->vtarget->raidVolume == 0)) {
  2196. int idx;
  2197. idx = ioc->eventContext % MPTCTL_EVENT_LOG_SIZE;
  2198. ioc->events[idx].event = MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE;
  2199. ioc->events[idx].eventContext = ioc->eventContext;
  2200. ioc->events[idx].data[0] = (pReq->LUN[1] << 24) |
  2201. (MPI_EVENT_SCSI_DEV_STAT_RC_SMART_DATA << 16) |
  2202. (sc->device->channel << 8) | sc->device->id;
  2203. ioc->events[idx].data[1] = (sense_data[13] << 8) | sense_data[12];
  2204. ioc->eventContext++;
  2205. if (ioc->pcidev->vendor ==
  2206. PCI_VENDOR_ID_IBM) {
  2207. mptscsih_issue_sep_command(ioc,
  2208. vdevice->vtarget, MPI_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
  2209. vdevice->vtarget->tflags |=
  2210. MPT_TARGET_FLAGS_LED_ON;
  2211. }
  2212. }
  2213. }
  2214. } else {
  2215. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Hmmm... SenseData len=0! (?)\n",
  2216. ioc->name));
  2217. }
  2218. }
  2219. /**
  2220. * mptscsih_get_scsi_lookup
  2221. * @ioc: Pointer to MPT_ADAPTER structure
  2222. * @i: index into the array
  2223. *
  2224. * retrieves scmd entry from ScsiLookup[] array list
  2225. *
  2226. * Returns the scsi_cmd pointer
  2227. **/
  2228. static struct scsi_cmnd *
  2229. mptscsih_get_scsi_lookup(MPT_ADAPTER *ioc, int i)
  2230. {
  2231. unsigned long flags;
  2232. struct scsi_cmnd *scmd;
  2233. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  2234. scmd = ioc->ScsiLookup[i];
  2235. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2236. return scmd;
  2237. }
  2238. /**
  2239. * mptscsih_getclear_scsi_lookup
  2240. * @ioc: Pointer to MPT_ADAPTER structure
  2241. * @i: index into the array
  2242. *
  2243. * retrieves and clears scmd entry from ScsiLookup[] array list
  2244. *
  2245. * Returns the scsi_cmd pointer
  2246. **/
  2247. static struct scsi_cmnd *
  2248. mptscsih_getclear_scsi_lookup(MPT_ADAPTER *ioc, int i)
  2249. {
  2250. unsigned long flags;
  2251. struct scsi_cmnd *scmd;
  2252. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  2253. scmd = ioc->ScsiLookup[i];
  2254. ioc->ScsiLookup[i] = NULL;
  2255. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2256. return scmd;
  2257. }
  2258. /**
  2259. * mptscsih_set_scsi_lookup
  2260. *
  2261. * writes a scmd entry into the ScsiLookup[] array list
  2262. *
  2263. * @ioc: Pointer to MPT_ADAPTER structure
  2264. * @i: index into the array
  2265. * @scmd: scsi_cmnd pointer
  2266. *
  2267. **/
  2268. static void
  2269. mptscsih_set_scsi_lookup(MPT_ADAPTER *ioc, int i, struct scsi_cmnd *scmd)
  2270. {
  2271. unsigned long flags;
  2272. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  2273. ioc->ScsiLookup[i] = scmd;
  2274. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2275. }
  2276. /**
  2277. * SCPNT_TO_LOOKUP_IDX - searches for a given scmd in the ScsiLookup[] array list
  2278. * @ioc: Pointer to MPT_ADAPTER structure
  2279. * @sc: scsi_cmnd pointer
  2280. */
  2281. static int
  2282. SCPNT_TO_LOOKUP_IDX(MPT_ADAPTER *ioc, struct scsi_cmnd *sc)
  2283. {
  2284. unsigned long flags;
  2285. int i, index=-1;
  2286. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  2287. for (i = 0; i < ioc->req_depth; i++) {
  2288. if (ioc->ScsiLookup[i] == sc) {
  2289. index = i;
  2290. goto out;
  2291. }
  2292. }
  2293. out:
  2294. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2295. return index;
  2296. }
  2297. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2298. int
  2299. mptscsih_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
  2300. {
  2301. MPT_SCSI_HOST *hd;
  2302. unsigned long flags;
  2303. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2304. ": IOC %s_reset routed to SCSI host driver!\n",
  2305. ioc->name, reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
  2306. reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
  2307. /* If a FW reload request arrives after base installed but
  2308. * before all scsi hosts have been attached, then an alt_ioc
  2309. * may have a NULL sh pointer.
  2310. */
  2311. if (ioc->sh == NULL || shost_priv(ioc->sh) == NULL)
  2312. return 0;
  2313. else
  2314. hd = shost_priv(ioc->sh);
  2315. if (reset_phase == MPT_IOC_SETUP_RESET) {
  2316. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Setup-Diag Reset\n", ioc->name));
  2317. /* Clean Up:
  2318. * 1. Set Hard Reset Pending Flag
  2319. * All new commands go to doneQ
  2320. */
  2321. hd->resetPending = 1;
  2322. } else if (reset_phase == MPT_IOC_PRE_RESET) {
  2323. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Pre-Diag Reset\n", ioc->name));
  2324. /* 2. Flush running commands
  2325. * Clean ScsiLookup (and associated memory)
  2326. * AND clean mytaskQ
  2327. */
  2328. /* 2b. Reply to OS all known outstanding I/O commands.
  2329. */
  2330. mptscsih_flush_running_cmds(hd);
  2331. /* 2c. If there was an internal command that
  2332. * has not completed, configuration or io request,
  2333. * free these resources.
  2334. */
  2335. if (hd->cmdPtr) {
  2336. del_timer(&hd->timer);
  2337. mpt_free_msg_frame(ioc, hd->cmdPtr);
  2338. }
  2339. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Pre-Reset complete.\n", ioc->name));
  2340. } else {
  2341. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Post-Diag Reset\n", ioc->name));
  2342. /* Once a FW reload begins, all new OS commands are
  2343. * redirected to the doneQ w/ a reset status.
  2344. * Init all control structures.
  2345. */
  2346. /* 2. Chain Buffer initialization
  2347. */
  2348. /* 4. Renegotiate to all devices, if SPI
  2349. */
  2350. /* 5. Enable new commands to be posted
  2351. */
  2352. spin_lock_irqsave(&ioc->FreeQlock, flags);
  2353. hd->tmPending = 0;
  2354. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  2355. hd->resetPending = 0;
  2356. hd->tmState = TM_STATE_NONE;
  2357. /* 6. If there was an internal command,
  2358. * wake this process up.
  2359. */
  2360. if (hd->cmdPtr) {
  2361. /*
  2362. * Wake up the original calling thread
  2363. */
  2364. hd->pLocal = &hd->localReply;
  2365. hd->pLocal->completion = MPT_SCANDV_DID_RESET;
  2366. hd->scandv_wait_done = 1;
  2367. wake_up(&hd->scandv_waitq);
  2368. hd->cmdPtr = NULL;
  2369. }
  2370. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Post-Reset complete.\n", ioc->name));
  2371. }
  2372. return 1; /* currently means nothing really */
  2373. }
  2374. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2375. int
  2376. mptscsih_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
  2377. {
  2378. MPT_SCSI_HOST *hd;
  2379. u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
  2380. devtverboseprintk(ioc, printk(MYIOC_s_DEBUG_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
  2381. ioc->name, event));
  2382. if (ioc->sh == NULL ||
  2383. ((hd = shost_priv(ioc->sh)) == NULL))
  2384. return 1;
  2385. switch (event) {
  2386. case MPI_EVENT_UNIT_ATTENTION: /* 03 */
  2387. /* FIXME! */
  2388. break;
  2389. case MPI_EVENT_IOC_BUS_RESET: /* 04 */
  2390. case MPI_EVENT_EXT_BUS_RESET: /* 05 */
  2391. if (hd && (ioc->bus_type == SPI) && (hd->soft_resets < -1))
  2392. hd->soft_resets++;
  2393. break;
  2394. case MPI_EVENT_LOGOUT: /* 09 */
  2395. /* FIXME! */
  2396. break;
  2397. case MPI_EVENT_RESCAN: /* 06 */
  2398. break;
  2399. /*
  2400. * CHECKME! Don't think we need to do
  2401. * anything for these, but...
  2402. */
  2403. case MPI_EVENT_LINK_STATUS_CHANGE: /* 07 */
  2404. case MPI_EVENT_LOOP_STATE_CHANGE: /* 08 */
  2405. /*
  2406. * CHECKME! Falling thru...
  2407. */
  2408. break;
  2409. case MPI_EVENT_INTEGRATED_RAID: /* 0B */
  2410. break;
  2411. case MPI_EVENT_NONE: /* 00 */
  2412. case MPI_EVENT_LOG_DATA: /* 01 */
  2413. case MPI_EVENT_STATE_CHANGE: /* 02 */
  2414. case MPI_EVENT_EVENT_CHANGE: /* 0A */
  2415. default:
  2416. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT ": Ignoring event (=%02Xh)\n",
  2417. ioc->name, event));
  2418. break;
  2419. }
  2420. return 1; /* currently means nothing really */
  2421. }
  2422. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2423. /*
  2424. * Bus Scan and Domain Validation functionality ...
  2425. */
  2426. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2427. /*
  2428. * mptscsih_scandv_complete - Scan and DV callback routine registered
  2429. * to Fustion MPT (base) driver.
  2430. *
  2431. * @ioc: Pointer to MPT_ADAPTER structure
  2432. * @mf: Pointer to original MPT request frame
  2433. * @mr: Pointer to MPT reply frame (NULL if TurboReply)
  2434. *
  2435. * This routine is called from mpt.c::mpt_interrupt() at the completion
  2436. * of any SCSI IO request.
  2437. * This routine is registered with the Fusion MPT (base) driver at driver
  2438. * load/init time via the mpt_register() API call.
  2439. *
  2440. * Returns 1 indicating alloc'd request frame ptr should be freed.
  2441. *
  2442. * Remark: Sets a completion code and (possibly) saves sense data
  2443. * in the IOC member localReply structure.
  2444. * Used ONLY for DV and other internal commands.
  2445. */
  2446. int
  2447. mptscsih_scandv_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
  2448. {
  2449. MPT_SCSI_HOST *hd;
  2450. SCSIIORequest_t *pReq;
  2451. int completionCode;
  2452. u16 req_idx;
  2453. hd = shost_priv(ioc->sh);
  2454. if ((mf == NULL) ||
  2455. (mf >= MPT_INDEX_2_MFPTR(ioc, ioc->req_depth))) {
  2456. printk(MYIOC_s_ERR_FMT
  2457. "ScanDvComplete, %s req frame ptr! (=%p)\n",
  2458. ioc->name, mf?"BAD":"NULL", (void *) mf);
  2459. goto wakeup;
  2460. }
  2461. del_timer(&hd->timer);
  2462. req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  2463. mptscsih_set_scsi_lookup(ioc, req_idx, NULL);
  2464. pReq = (SCSIIORequest_t *) mf;
  2465. if (mf != hd->cmdPtr) {
  2466. printk(MYIOC_s_WARN_FMT "ScanDvComplete (mf=%p, cmdPtr=%p, idx=%d)\n",
  2467. ioc->name, (void *)mf, (void *) hd->cmdPtr, req_idx);
  2468. }
  2469. hd->cmdPtr = NULL;
  2470. ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT "ScanDvComplete (mf=%p,mr=%p,idx=%d)\n",
  2471. ioc->name, mf, mr, req_idx));
  2472. hd->pLocal = &hd->localReply;
  2473. hd->pLocal->scsiStatus = 0;
  2474. /* If target struct exists, clear sense valid flag.
  2475. */
  2476. if (mr == NULL) {
  2477. completionCode = MPT_SCANDV_GOOD;
  2478. } else {
  2479. SCSIIOReply_t *pReply;
  2480. u16 status;
  2481. u8 scsi_status;
  2482. pReply = (SCSIIOReply_t *) mr;
  2483. status = le16_to_cpu(pReply->IOCStatus) & MPI_IOCSTATUS_MASK;
  2484. scsi_status = pReply->SCSIStatus;
  2485. switch(status) {
  2486. case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE: /* 0x0043 */
  2487. completionCode = MPT_SCANDV_SELECTION_TIMEOUT;
  2488. break;
  2489. case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR: /* 0x0046 */
  2490. case MPI_IOCSTATUS_SCSI_TASK_TERMINATED: /* 0x0048 */
  2491. case MPI_IOCSTATUS_SCSI_IOC_TERMINATED: /* 0x004B */
  2492. case MPI_IOCSTATUS_SCSI_EXT_TERMINATED: /* 0x004C */
  2493. completionCode = MPT_SCANDV_DID_RESET;
  2494. break;
  2495. case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN: /* 0x0045 */
  2496. case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR: /* 0x0040 */
  2497. case MPI_IOCSTATUS_SUCCESS: /* 0x0000 */
  2498. if (pReply->Function == MPI_FUNCTION_CONFIG) {
  2499. ConfigReply_t *pr = (ConfigReply_t *)mr;
  2500. completionCode = MPT_SCANDV_GOOD;
  2501. hd->pLocal->header.PageVersion = pr->Header.PageVersion;
  2502. hd->pLocal->header.PageLength = pr->Header.PageLength;
  2503. hd->pLocal->header.PageNumber = pr->Header.PageNumber;
  2504. hd->pLocal->header.PageType = pr->Header.PageType;
  2505. } else if (pReply->Function == MPI_FUNCTION_RAID_ACTION) {
  2506. /* If the RAID Volume request is successful,
  2507. * return GOOD, else indicate that
  2508. * some type of error occurred.
  2509. */
  2510. MpiRaidActionReply_t *pr = (MpiRaidActionReply_t *)mr;
  2511. if (le16_to_cpu(pr->ActionStatus) == MPI_RAID_ACTION_ASTATUS_SUCCESS)
  2512. completionCode = MPT_SCANDV_GOOD;
  2513. else
  2514. completionCode = MPT_SCANDV_SOME_ERROR;
  2515. memcpy(hd->pLocal->sense, pr, sizeof(hd->pLocal->sense));
  2516. } else if (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_VALID) {
  2517. u8 *sense_data;
  2518. int sz;
  2519. /* save sense data in global structure
  2520. */
  2521. completionCode = MPT_SCANDV_SENSE;
  2522. hd->pLocal->scsiStatus = scsi_status;
  2523. sense_data = ((u8 *)ioc->sense_buf_pool +
  2524. (req_idx * MPT_SENSE_BUFFER_ALLOC));
  2525. sz = min_t(int, pReq->SenseBufferLength,
  2526. SCSI_STD_SENSE_BYTES);
  2527. memcpy(hd->pLocal->sense, sense_data, sz);
  2528. ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT " Check Condition, sense ptr %p\n",
  2529. ioc->name, sense_data));
  2530. } else if (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_FAILED) {
  2531. if (pReq->CDB[0] == INQUIRY)
  2532. completionCode = MPT_SCANDV_ISSUE_SENSE;
  2533. else
  2534. completionCode = MPT_SCANDV_DID_RESET;
  2535. }
  2536. else if (pReply->SCSIState & MPI_SCSI_STATE_NO_SCSI_STATUS)
  2537. completionCode = MPT_SCANDV_DID_RESET;
  2538. else if (pReply->SCSIState & MPI_SCSI_STATE_TERMINATED)
  2539. completionCode = MPT_SCANDV_DID_RESET;
  2540. else {
  2541. completionCode = MPT_SCANDV_GOOD;
  2542. hd->pLocal->scsiStatus = scsi_status;
  2543. }
  2544. break;
  2545. case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR: /* 0x0047 */
  2546. if (pReply->SCSIState & MPI_SCSI_STATE_TERMINATED)
  2547. completionCode = MPT_SCANDV_DID_RESET;
  2548. else
  2549. completionCode = MPT_SCANDV_SOME_ERROR;
  2550. break;
  2551. default:
  2552. completionCode = MPT_SCANDV_SOME_ERROR;
  2553. break;
  2554. } /* switch(status) */
  2555. } /* end of address reply case */
  2556. hd->pLocal->completion = completionCode;
  2557. /* MF and RF are freed in mpt_interrupt
  2558. */
  2559. wakeup:
  2560. /* Free Chain buffers (will never chain) in scan or dv */
  2561. //mptscsih_freeChainBuffers(ioc, req_idx);
  2562. /*
  2563. * Wake up the original calling thread
  2564. */
  2565. hd->scandv_wait_done = 1;
  2566. wake_up(&hd->scandv_waitq);
  2567. return 1;
  2568. }
  2569. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2570. /* mptscsih_timer_expired - Call back for timer process.
  2571. * Used only for dv functionality.
  2572. * @data: Pointer to MPT_SCSI_HOST recast as an unsigned long
  2573. *
  2574. */
  2575. void
  2576. mptscsih_timer_expired(unsigned long data)
  2577. {
  2578. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *) data;
  2579. MPT_ADAPTER *ioc = hd->ioc;
  2580. ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Timer Expired! Cmd %p\n", ioc->name, hd->cmdPtr));
  2581. if (hd->cmdPtr) {
  2582. MPIHeader_t *cmd = (MPIHeader_t *)hd->cmdPtr;
  2583. if (cmd->Function == MPI_FUNCTION_SCSI_IO_REQUEST) {
  2584. /* Desire to issue a task management request here.
  2585. * TM requests MUST be single threaded.
  2586. * If old eh code and no TM current, issue request.
  2587. * If new eh code, do nothing. Wait for OS cmd timeout
  2588. * for bus reset.
  2589. */
  2590. } else {
  2591. /* Perform a FW reload */
  2592. if (mpt_HardResetHandler(ioc, NO_SLEEP) < 0) {
  2593. printk(MYIOC_s_WARN_FMT "Firmware Reload FAILED!\n", ioc->name);
  2594. }
  2595. }
  2596. } else {
  2597. /* This should NEVER happen */
  2598. printk(MYIOC_s_WARN_FMT "Null cmdPtr!!!!\n", ioc->name);
  2599. }
  2600. /* No more processing.
  2601. * TM call will generate an interrupt for SCSI TM Management.
  2602. * The FW will reply to all outstanding commands, callback will finish cleanup.
  2603. * Hard reset clean-up will free all resources.
  2604. */
  2605. ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Timer Expired Complete!\n", ioc->name));
  2606. return;
  2607. }
  2608. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2609. /**
  2610. * mptscsih_do_cmd - Do internal command.
  2611. * @hd: MPT_SCSI_HOST pointer
  2612. * @io: INTERNAL_CMD pointer.
  2613. *
  2614. * Issue the specified internally generated command and do command
  2615. * specific cleanup. For bus scan / DV only.
  2616. * NOTES: If command is Inquiry and status is good,
  2617. * initialize a target structure, save the data
  2618. *
  2619. * Remark: Single threaded access only.
  2620. *
  2621. * Return:
  2622. * < 0 if an illegal command or no resources
  2623. *
  2624. * 0 if good
  2625. *
  2626. * > 0 if command complete but some type of completion error.
  2627. */
  2628. static int
  2629. mptscsih_do_cmd(MPT_SCSI_HOST *hd, INTERNAL_CMD *io)
  2630. {
  2631. MPT_FRAME_HDR *mf;
  2632. SCSIIORequest_t *pScsiReq;
  2633. SCSIIORequest_t ReqCopy;
  2634. int my_idx, ii, dir;
  2635. int rc, cmdTimeout;
  2636. int in_isr;
  2637. char cmdLen;
  2638. char CDB[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  2639. char cmd = io->cmd;
  2640. MPT_ADAPTER *ioc = hd->ioc;
  2641. in_isr = in_interrupt();
  2642. if (in_isr) {
  2643. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Internal SCSI IO request not allowed in ISR context!\n",
  2644. ioc->name));
  2645. return -EPERM;
  2646. }
  2647. /* Set command specific information
  2648. */
  2649. switch (cmd) {
  2650. case INQUIRY:
  2651. cmdLen = 6;
  2652. dir = MPI_SCSIIO_CONTROL_READ;
  2653. CDB[0] = cmd;
  2654. CDB[4] = io->size;
  2655. cmdTimeout = 10;
  2656. break;
  2657. case TEST_UNIT_READY:
  2658. cmdLen = 6;
  2659. dir = MPI_SCSIIO_CONTROL_READ;
  2660. cmdTimeout = 10;
  2661. break;
  2662. case START_STOP:
  2663. cmdLen = 6;
  2664. dir = MPI_SCSIIO_CONTROL_READ;
  2665. CDB[0] = cmd;
  2666. CDB[4] = 1; /*Spin up the disk */
  2667. cmdTimeout = 15;
  2668. break;
  2669. case REQUEST_SENSE:
  2670. cmdLen = 6;
  2671. CDB[0] = cmd;
  2672. CDB[4] = io->size;
  2673. dir = MPI_SCSIIO_CONTROL_READ;
  2674. cmdTimeout = 10;
  2675. break;
  2676. case READ_BUFFER:
  2677. cmdLen = 10;
  2678. dir = MPI_SCSIIO_CONTROL_READ;
  2679. CDB[0] = cmd;
  2680. if (io->flags & MPT_ICFLAG_ECHO) {
  2681. CDB[1] = 0x0A;
  2682. } else {
  2683. CDB[1] = 0x02;
  2684. }
  2685. if (io->flags & MPT_ICFLAG_BUF_CAP) {
  2686. CDB[1] |= 0x01;
  2687. }
  2688. CDB[6] = (io->size >> 16) & 0xFF;
  2689. CDB[7] = (io->size >> 8) & 0xFF;
  2690. CDB[8] = io->size & 0xFF;
  2691. cmdTimeout = 10;
  2692. break;
  2693. case WRITE_BUFFER:
  2694. cmdLen = 10;
  2695. dir = MPI_SCSIIO_CONTROL_WRITE;
  2696. CDB[0] = cmd;
  2697. if (io->flags & MPT_ICFLAG_ECHO) {
  2698. CDB[1] = 0x0A;
  2699. } else {
  2700. CDB[1] = 0x02;
  2701. }
  2702. CDB[6] = (io->size >> 16) & 0xFF;
  2703. CDB[7] = (io->size >> 8) & 0xFF;
  2704. CDB[8] = io->size & 0xFF;
  2705. cmdTimeout = 10;
  2706. break;
  2707. case RESERVE:
  2708. cmdLen = 6;
  2709. dir = MPI_SCSIIO_CONTROL_READ;
  2710. CDB[0] = cmd;
  2711. cmdTimeout = 10;
  2712. break;
  2713. case RELEASE:
  2714. cmdLen = 6;
  2715. dir = MPI_SCSIIO_CONTROL_READ;
  2716. CDB[0] = cmd;
  2717. cmdTimeout = 10;
  2718. break;
  2719. case SYNCHRONIZE_CACHE:
  2720. cmdLen = 10;
  2721. dir = MPI_SCSIIO_CONTROL_READ;
  2722. CDB[0] = cmd;
  2723. // CDB[1] = 0x02; /* set immediate bit */
  2724. cmdTimeout = 10;
  2725. break;
  2726. default:
  2727. /* Error Case */
  2728. return -EFAULT;
  2729. }
  2730. /* Get and Populate a free Frame
  2731. */
  2732. if ((mf = mpt_get_msg_frame(ioc->InternalCtx, ioc)) == NULL) {
  2733. dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "No msg frames!\n",
  2734. ioc->name));
  2735. return -EBUSY;
  2736. }
  2737. pScsiReq = (SCSIIORequest_t *) mf;
  2738. /* Get the request index */
  2739. my_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  2740. ADD_INDEX_LOG(my_idx); /* for debug */
  2741. if (io->flags & MPT_ICFLAG_PHYS_DISK) {
  2742. pScsiReq->TargetID = io->physDiskNum;
  2743. pScsiReq->Bus = 0;
  2744. pScsiReq->ChainOffset = 0;
  2745. pScsiReq->Function = MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
  2746. } else {
  2747. pScsiReq->TargetID = io->id;
  2748. pScsiReq->Bus = io->channel;
  2749. pScsiReq->ChainOffset = 0;
  2750. pScsiReq->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
  2751. }
  2752. pScsiReq->CDBLength = cmdLen;
  2753. pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
  2754. pScsiReq->Reserved = 0;
  2755. pScsiReq->MsgFlags = mpt_msg_flags();
  2756. /* MsgContext set in mpt_get_msg_fram call */
  2757. int_to_scsilun(io->lun, (struct scsi_lun *)pScsiReq->LUN);
  2758. if (io->flags & MPT_ICFLAG_TAGGED_CMD)
  2759. pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_SIMPLEQ);
  2760. else
  2761. pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_UNTAGGED);
  2762. if (cmd == REQUEST_SENSE) {
  2763. pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_UNTAGGED);
  2764. ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Untagged! 0x%2x\n",
  2765. ioc->name, cmd));
  2766. }
  2767. for (ii=0; ii < 16; ii++)
  2768. pScsiReq->CDB[ii] = CDB[ii];
  2769. pScsiReq->DataLength = cpu_to_le32(io->size);
  2770. pScsiReq->SenseBufferLowAddr = cpu_to_le32(ioc->sense_buf_low_dma
  2771. + (my_idx * MPT_SENSE_BUFFER_ALLOC));
  2772. ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Sending Command 0x%x for (%d:%d:%d)\n",
  2773. ioc->name, cmd, io->channel, io->id, io->lun));
  2774. if (dir == MPI_SCSIIO_CONTROL_READ) {
  2775. mpt_add_sge((char *) &pScsiReq->SGL,
  2776. MPT_SGE_FLAGS_SSIMPLE_READ | io->size,
  2777. io->data_dma);
  2778. } else {
  2779. mpt_add_sge((char *) &pScsiReq->SGL,
  2780. MPT_SGE_FLAGS_SSIMPLE_WRITE | io->size,
  2781. io->data_dma);
  2782. }
  2783. /* The ISR will free the request frame, but we need
  2784. * the information to initialize the target. Duplicate.
  2785. */
  2786. memcpy(&ReqCopy, pScsiReq, sizeof(SCSIIORequest_t));
  2787. /* Issue this command after:
  2788. * finish init
  2789. * add timer
  2790. * Wait until the reply has been received
  2791. * ScsiScanDvCtx callback function will
  2792. * set hd->pLocal;
  2793. * set scandv_wait_done and call wake_up
  2794. */
  2795. hd->pLocal = NULL;
  2796. hd->timer.expires = jiffies + HZ*cmdTimeout;
  2797. hd->scandv_wait_done = 0;
  2798. /* Save cmd pointer, for resource free if timeout or
  2799. * FW reload occurs
  2800. */
  2801. hd->cmdPtr = mf;
  2802. add_timer(&hd->timer);
  2803. mpt_put_msg_frame(ioc->InternalCtx, ioc, mf);
  2804. wait_event(hd->scandv_waitq, hd->scandv_wait_done);
  2805. if (hd->pLocal) {
  2806. rc = hd->pLocal->completion;
  2807. hd->pLocal->skip = 0;
  2808. /* Always set fatal error codes in some cases.
  2809. */
  2810. if (rc == MPT_SCANDV_SELECTION_TIMEOUT)
  2811. rc = -ENXIO;
  2812. else if (rc == MPT_SCANDV_SOME_ERROR)
  2813. rc = -rc;
  2814. } else {
  2815. rc = -EFAULT;
  2816. /* This should never happen. */
  2817. ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT "_do_cmd: Null pLocal!!!\n",
  2818. ioc->name));
  2819. }
  2820. return rc;
  2821. }
  2822. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2823. /**
  2824. * mptscsih_synchronize_cache - Send SYNCHRONIZE_CACHE to all disks.
  2825. * @hd: Pointer to a SCSI HOST structure
  2826. * @vdevice: virtual target device
  2827. *
  2828. * Uses the ISR, but with special processing.
  2829. * MUST be single-threaded.
  2830. *
  2831. */
  2832. static void
  2833. mptscsih_synchronize_cache(MPT_SCSI_HOST *hd, VirtDevice *vdevice)
  2834. {
  2835. INTERNAL_CMD iocmd;
  2836. /* Ignore hidden raid components, this is handled when the command
  2837. * is sent to the volume
  2838. */
  2839. if (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT)
  2840. return;
  2841. if (vdevice->vtarget->type != TYPE_DISK || vdevice->vtarget->deleted ||
  2842. !vdevice->configured_lun)
  2843. return;
  2844. /* Following parameters will not change
  2845. * in this routine.
  2846. */
  2847. iocmd.cmd = SYNCHRONIZE_CACHE;
  2848. iocmd.flags = 0;
  2849. iocmd.physDiskNum = -1;
  2850. iocmd.data = NULL;
  2851. iocmd.data_dma = -1;
  2852. iocmd.size = 0;
  2853. iocmd.rsvd = iocmd.rsvd2 = 0;
  2854. iocmd.channel = vdevice->vtarget->channel;
  2855. iocmd.id = vdevice->vtarget->id;
  2856. iocmd.lun = vdevice->lun;
  2857. mptscsih_do_cmd(hd, &iocmd);
  2858. }
  2859. static ssize_t
  2860. mptscsih_version_fw_show(struct device *dev, struct device_attribute *attr,
  2861. char *buf)
  2862. {
  2863. struct Scsi_Host *host = class_to_shost(dev);
  2864. MPT_SCSI_HOST *hd = shost_priv(host);
  2865. MPT_ADAPTER *ioc = hd->ioc;
  2866. return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
  2867. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  2868. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  2869. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  2870. ioc->facts.FWVersion.Word & 0x000000FF);
  2871. }
  2872. static DEVICE_ATTR(version_fw, S_IRUGO, mptscsih_version_fw_show, NULL);
  2873. static ssize_t
  2874. mptscsih_version_bios_show(struct device *dev, struct device_attribute *attr,
  2875. char *buf)
  2876. {
  2877. struct Scsi_Host *host = class_to_shost(dev);
  2878. MPT_SCSI_HOST *hd = shost_priv(host);
  2879. MPT_ADAPTER *ioc = hd->ioc;
  2880. return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x.%02x\n",
  2881. (ioc->biosVersion & 0xFF000000) >> 24,
  2882. (ioc->biosVersion & 0x00FF0000) >> 16,
  2883. (ioc->biosVersion & 0x0000FF00) >> 8,
  2884. ioc->biosVersion & 0x000000FF);
  2885. }
  2886. static DEVICE_ATTR(version_bios, S_IRUGO, mptscsih_version_bios_show, NULL);
  2887. static ssize_t
  2888. mptscsih_version_mpi_show(struct device *dev, struct device_attribute *attr,
  2889. char *buf)
  2890. {
  2891. struct Scsi_Host *host = class_to_shost(dev);
  2892. MPT_SCSI_HOST *hd = shost_priv(host);
  2893. MPT_ADAPTER *ioc = hd->ioc;
  2894. return snprintf(buf, PAGE_SIZE, "%03x\n", ioc->facts.MsgVersion);
  2895. }
  2896. static DEVICE_ATTR(version_mpi, S_IRUGO, mptscsih_version_mpi_show, NULL);
  2897. static ssize_t
  2898. mptscsih_version_product_show(struct device *dev,
  2899. struct device_attribute *attr,
  2900. char *buf)
  2901. {
  2902. struct Scsi_Host *host = class_to_shost(dev);
  2903. MPT_SCSI_HOST *hd = shost_priv(host);
  2904. MPT_ADAPTER *ioc = hd->ioc;
  2905. return snprintf(buf, PAGE_SIZE, "%s\n", ioc->prod_name);
  2906. }
  2907. static DEVICE_ATTR(version_product, S_IRUGO,
  2908. mptscsih_version_product_show, NULL);
  2909. static ssize_t
  2910. mptscsih_version_nvdata_persistent_show(struct device *dev,
  2911. struct device_attribute *attr,
  2912. char *buf)
  2913. {
  2914. struct Scsi_Host *host = class_to_shost(dev);
  2915. MPT_SCSI_HOST *hd = shost_priv(host);
  2916. MPT_ADAPTER *ioc = hd->ioc;
  2917. return snprintf(buf, PAGE_SIZE, "%02xh\n",
  2918. ioc->nvdata_version_persistent);
  2919. }
  2920. static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
  2921. mptscsih_version_nvdata_persistent_show, NULL);
  2922. static ssize_t
  2923. mptscsih_version_nvdata_default_show(struct device *dev,
  2924. struct device_attribute *attr, char *buf)
  2925. {
  2926. struct Scsi_Host *host = class_to_shost(dev);
  2927. MPT_SCSI_HOST *hd = shost_priv(host);
  2928. MPT_ADAPTER *ioc = hd->ioc;
  2929. return snprintf(buf, PAGE_SIZE, "%02xh\n",ioc->nvdata_version_default);
  2930. }
  2931. static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
  2932. mptscsih_version_nvdata_default_show, NULL);
  2933. static ssize_t
  2934. mptscsih_board_name_show(struct device *dev, struct device_attribute *attr,
  2935. char *buf)
  2936. {
  2937. struct Scsi_Host *host = class_to_shost(dev);
  2938. MPT_SCSI_HOST *hd = shost_priv(host);
  2939. MPT_ADAPTER *ioc = hd->ioc;
  2940. return snprintf(buf, PAGE_SIZE, "%s\n", ioc->board_name);
  2941. }
  2942. static DEVICE_ATTR(board_name, S_IRUGO, mptscsih_board_name_show, NULL);
  2943. static ssize_t
  2944. mptscsih_board_assembly_show(struct device *dev,
  2945. struct device_attribute *attr, char *buf)
  2946. {
  2947. struct Scsi_Host *host = class_to_shost(dev);
  2948. MPT_SCSI_HOST *hd = shost_priv(host);
  2949. MPT_ADAPTER *ioc = hd->ioc;
  2950. return snprintf(buf, PAGE_SIZE, "%s\n", ioc->board_assembly);
  2951. }
  2952. static DEVICE_ATTR(board_assembly, S_IRUGO,
  2953. mptscsih_board_assembly_show, NULL);
  2954. static ssize_t
  2955. mptscsih_board_tracer_show(struct device *dev, struct device_attribute *attr,
  2956. char *buf)
  2957. {
  2958. struct Scsi_Host *host = class_to_shost(dev);
  2959. MPT_SCSI_HOST *hd = shost_priv(host);
  2960. MPT_ADAPTER *ioc = hd->ioc;
  2961. return snprintf(buf, PAGE_SIZE, "%s\n", ioc->board_tracer);
  2962. }
  2963. static DEVICE_ATTR(board_tracer, S_IRUGO,
  2964. mptscsih_board_tracer_show, NULL);
  2965. static ssize_t
  2966. mptscsih_io_delay_show(struct device *dev, struct device_attribute *attr,
  2967. char *buf)
  2968. {
  2969. struct Scsi_Host *host = class_to_shost(dev);
  2970. MPT_SCSI_HOST *hd = shost_priv(host);
  2971. MPT_ADAPTER *ioc = hd->ioc;
  2972. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
  2973. }
  2974. static DEVICE_ATTR(io_delay, S_IRUGO,
  2975. mptscsih_io_delay_show, NULL);
  2976. static ssize_t
  2977. mptscsih_device_delay_show(struct device *dev, struct device_attribute *attr,
  2978. char *buf)
  2979. {
  2980. struct Scsi_Host *host = class_to_shost(dev);
  2981. MPT_SCSI_HOST *hd = shost_priv(host);
  2982. MPT_ADAPTER *ioc = hd->ioc;
  2983. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
  2984. }
  2985. static DEVICE_ATTR(device_delay, S_IRUGO,
  2986. mptscsih_device_delay_show, NULL);
  2987. static ssize_t
  2988. mptscsih_debug_level_show(struct device *dev, struct device_attribute *attr,
  2989. char *buf)
  2990. {
  2991. struct Scsi_Host *host = class_to_shost(dev);
  2992. MPT_SCSI_HOST *hd = shost_priv(host);
  2993. MPT_ADAPTER *ioc = hd->ioc;
  2994. return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->debug_level);
  2995. }
  2996. static ssize_t
  2997. mptscsih_debug_level_store(struct device *dev, struct device_attribute *attr,
  2998. const char *buf, size_t count)
  2999. {
  3000. struct Scsi_Host *host = class_to_shost(dev);
  3001. MPT_SCSI_HOST *hd = shost_priv(host);
  3002. MPT_ADAPTER *ioc = hd->ioc;
  3003. int val = 0;
  3004. if (sscanf(buf, "%x", &val) != 1)
  3005. return -EINVAL;
  3006. ioc->debug_level = val;
  3007. printk(MYIOC_s_INFO_FMT "debug_level=%08xh\n",
  3008. ioc->name, ioc->debug_level);
  3009. return strlen(buf);
  3010. }
  3011. static DEVICE_ATTR(debug_level, S_IRUGO | S_IWUSR,
  3012. mptscsih_debug_level_show, mptscsih_debug_level_store);
  3013. struct device_attribute *mptscsih_host_attrs[] = {
  3014. &dev_attr_version_fw,
  3015. &dev_attr_version_bios,
  3016. &dev_attr_version_mpi,
  3017. &dev_attr_version_product,
  3018. &dev_attr_version_nvdata_persistent,
  3019. &dev_attr_version_nvdata_default,
  3020. &dev_attr_board_name,
  3021. &dev_attr_board_assembly,
  3022. &dev_attr_board_tracer,
  3023. &dev_attr_io_delay,
  3024. &dev_attr_device_delay,
  3025. &dev_attr_debug_level,
  3026. NULL,
  3027. };
  3028. EXPORT_SYMBOL(mptscsih_host_attrs);
  3029. EXPORT_SYMBOL(mptscsih_remove);
  3030. EXPORT_SYMBOL(mptscsih_shutdown);
  3031. #ifdef CONFIG_PM
  3032. EXPORT_SYMBOL(mptscsih_suspend);
  3033. EXPORT_SYMBOL(mptscsih_resume);
  3034. #endif
  3035. EXPORT_SYMBOL(mptscsih_proc_info);
  3036. EXPORT_SYMBOL(mptscsih_info);
  3037. EXPORT_SYMBOL(mptscsih_qcmd);
  3038. EXPORT_SYMBOL(mptscsih_slave_destroy);
  3039. EXPORT_SYMBOL(mptscsih_slave_configure);
  3040. EXPORT_SYMBOL(mptscsih_abort);
  3041. EXPORT_SYMBOL(mptscsih_dev_reset);
  3042. EXPORT_SYMBOL(mptscsih_bus_reset);
  3043. EXPORT_SYMBOL(mptscsih_host_reset);
  3044. EXPORT_SYMBOL(mptscsih_bios_param);
  3045. EXPORT_SYMBOL(mptscsih_io_done);
  3046. EXPORT_SYMBOL(mptscsih_taskmgmt_complete);
  3047. EXPORT_SYMBOL(mptscsih_scandv_complete);
  3048. EXPORT_SYMBOL(mptscsih_event_process);
  3049. EXPORT_SYMBOL(mptscsih_ioc_reset);
  3050. EXPORT_SYMBOL(mptscsih_change_queue_depth);
  3051. EXPORT_SYMBOL(mptscsih_timer_expired);
  3052. EXPORT_SYMBOL(mptscsih_TMHandler);
  3053. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/