mptscsih.c 96 KB

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