mptscsih.c 88 KB

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