mptscsih.c 97 KB

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