mptscsih.c 94 KB

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