mptscsih.c 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509
  1. /*
  2. * linux/drivers/message/fusion/mptscsih.c
  3. * For use with LSI PCI chip/adapter(s)
  4. * running LSI Fusion MPT (Message Passing Technology) firmware.
  5. *
  6. * Copyright (c) 1999-2007 LSI Corporation
  7. * (mailto:DL-MPTFusionLinux@lsi.com)
  8. *
  9. */
  10. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  11. /*
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; version 2 of the License.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. NO WARRANTY
  20. THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21. CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22. LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23. MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24. solely responsible for determining the appropriateness of using and
  25. distributing the Program and assumes all risks associated with its
  26. exercise of rights under this Agreement, including but not limited to
  27. the risks and costs of program errors, damage to or loss of data,
  28. programs or equipment, and unavailability or interruption of operations.
  29. DISCLAIMER OF LIABILITY
  30. NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36. HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37. You should have received a copy of the GNU General Public License
  38. along with this program; if not, write to the Free Software
  39. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  40. */
  41. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  42. #include <linux/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/init.h>
  45. #include <linux/errno.h>
  46. #include <linux/kdev_t.h>
  47. #include <linux/blkdev.h>
  48. #include <linux/delay.h> /* for mdelay */
  49. #include <linux/interrupt.h> /* needed for in_interrupt() proto */
  50. #include <linux/reboot.h> /* notifier code */
  51. #include <linux/workqueue.h>
  52. #include <scsi/scsi.h>
  53. #include <scsi/scsi_cmnd.h>
  54. #include <scsi/scsi_device.h>
  55. #include <scsi/scsi_host.h>
  56. #include <scsi/scsi_tcq.h>
  57. #include <scsi/scsi_dbg.h>
  58. #include "mptbase.h"
  59. #include "mptscsih.h"
  60. #include "lsi/mpi_log_sas.h"
  61. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  62. #define my_NAME "Fusion MPT SCSI Host driver"
  63. #define my_VERSION MPT_LINUX_VERSION_COMMON
  64. #define MYNAM "mptscsih"
  65. MODULE_AUTHOR(MODULEAUTHOR);
  66. MODULE_DESCRIPTION(my_NAME);
  67. MODULE_LICENSE("GPL");
  68. MODULE_VERSION(my_VERSION);
  69. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  70. /*
  71. * Other private/forward protos...
  72. */
  73. 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) sizeof((scp)->sense_buffer)
  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,__FUNCTION__));
  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. mptscsih_shutdown(pdev);
  1039. return mpt_suspend(pdev,state);
  1040. }
  1041. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1042. /*
  1043. * mptscsih_resume - Fusion MPT scsi driver resume routine.
  1044. *
  1045. *
  1046. */
  1047. int
  1048. mptscsih_resume(struct pci_dev *pdev)
  1049. {
  1050. return mpt_resume(pdev);
  1051. }
  1052. #endif
  1053. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1054. /**
  1055. * mptscsih_info - Return information about MPT adapter
  1056. * @SChost: Pointer to Scsi_Host structure
  1057. *
  1058. * (linux scsi_host_template.info routine)
  1059. *
  1060. * Returns pointer to buffer where information was written.
  1061. */
  1062. const char *
  1063. mptscsih_info(struct Scsi_Host *SChost)
  1064. {
  1065. MPT_SCSI_HOST *h;
  1066. int size = 0;
  1067. h = shost_priv(SChost);
  1068. if (h) {
  1069. if (h->info_kbuf == NULL)
  1070. if ((h->info_kbuf = kmalloc(0x1000 /* 4Kb */, GFP_KERNEL)) == NULL)
  1071. return h->info_kbuf;
  1072. h->info_kbuf[0] = '\0';
  1073. mpt_print_ioc_summary(h->ioc, h->info_kbuf, &size, 0, 0);
  1074. h->info_kbuf[size-1] = '\0';
  1075. }
  1076. return h->info_kbuf;
  1077. }
  1078. struct info_str {
  1079. char *buffer;
  1080. int length;
  1081. int offset;
  1082. int pos;
  1083. };
  1084. static void
  1085. mptscsih_copy_mem_info(struct info_str *info, char *data, int len)
  1086. {
  1087. if (info->pos + len > info->length)
  1088. len = info->length - info->pos;
  1089. if (info->pos + len < info->offset) {
  1090. info->pos += len;
  1091. return;
  1092. }
  1093. if (info->pos < info->offset) {
  1094. data += (info->offset - info->pos);
  1095. len -= (info->offset - info->pos);
  1096. }
  1097. if (len > 0) {
  1098. memcpy(info->buffer + info->pos, data, len);
  1099. info->pos += len;
  1100. }
  1101. }
  1102. static int
  1103. mptscsih_copy_info(struct info_str *info, char *fmt, ...)
  1104. {
  1105. va_list args;
  1106. char buf[81];
  1107. int len;
  1108. va_start(args, fmt);
  1109. len = vsprintf(buf, fmt, args);
  1110. va_end(args);
  1111. mptscsih_copy_mem_info(info, buf, len);
  1112. return len;
  1113. }
  1114. static int
  1115. mptscsih_host_info(MPT_ADAPTER *ioc, char *pbuf, off_t offset, int len)
  1116. {
  1117. struct info_str info;
  1118. info.buffer = pbuf;
  1119. info.length = len;
  1120. info.offset = offset;
  1121. info.pos = 0;
  1122. mptscsih_copy_info(&info, "%s: %s, ", ioc->name, ioc->prod_name);
  1123. mptscsih_copy_info(&info, "%s%08xh, ", MPT_FW_REV_MAGIC_ID_STRING, ioc->facts.FWVersion.Word);
  1124. mptscsih_copy_info(&info, "Ports=%d, ", ioc->facts.NumberOfPorts);
  1125. mptscsih_copy_info(&info, "MaxQ=%d\n", ioc->req_depth);
  1126. return ((info.pos > info.offset) ? info.pos - info.offset : 0);
  1127. }
  1128. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1129. /**
  1130. * mptscsih_proc_info - Return information about MPT adapter
  1131. * @host: scsi host struct
  1132. * @buffer: if write, user data; if read, buffer for user
  1133. * @start: returns the buffer address
  1134. * @offset: if write, 0; if read, the current offset into the buffer from
  1135. * the previous read.
  1136. * @length: if write, return length;
  1137. * @func: write = 1; read = 0
  1138. *
  1139. * (linux scsi_host_template.info routine)
  1140. */
  1141. int
  1142. mptscsih_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
  1143. int length, int func)
  1144. {
  1145. MPT_SCSI_HOST *hd = shost_priv(host);
  1146. MPT_ADAPTER *ioc = hd->ioc;
  1147. int size = 0;
  1148. if (func) {
  1149. /*
  1150. * write is not supported
  1151. */
  1152. } else {
  1153. if (start)
  1154. *start = buffer;
  1155. size = mptscsih_host_info(ioc, buffer, offset, length);
  1156. }
  1157. return size;
  1158. }
  1159. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1160. #define ADD_INDEX_LOG(req_ent) do { } while(0)
  1161. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1162. /**
  1163. * mptscsih_qcmd - Primary Fusion MPT SCSI initiator IO start routine.
  1164. * @SCpnt: Pointer to scsi_cmnd structure
  1165. * @done: Pointer SCSI mid-layer IO completion function
  1166. *
  1167. * (linux scsi_host_template.queuecommand routine)
  1168. * This is the primary SCSI IO start routine. Create a MPI SCSIIORequest
  1169. * from a linux scsi_cmnd request and send it to the IOC.
  1170. *
  1171. * Returns 0. (rtn value discarded by linux scsi mid-layer)
  1172. */
  1173. int
  1174. mptscsih_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  1175. {
  1176. MPT_SCSI_HOST *hd;
  1177. MPT_FRAME_HDR *mf;
  1178. SCSIIORequest_t *pScsiReq;
  1179. VirtDevice *vdevice = SCpnt->device->hostdata;
  1180. int lun;
  1181. u32 datalen;
  1182. u32 scsictl;
  1183. u32 scsidir;
  1184. u32 cmd_len;
  1185. int my_idx;
  1186. int ii;
  1187. MPT_ADAPTER *ioc;
  1188. hd = shost_priv(SCpnt->device->host);
  1189. ioc = hd->ioc;
  1190. lun = SCpnt->device->lun;
  1191. SCpnt->scsi_done = done;
  1192. dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT "qcmd: SCpnt=%p, done()=%p\n",
  1193. ioc->name, SCpnt, done));
  1194. if (hd->resetPending) {
  1195. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT "qcmd: SCpnt=%p timeout + 60HZ\n",
  1196. ioc->name, SCpnt));
  1197. return SCSI_MLQUEUE_HOST_BUSY;
  1198. }
  1199. /*
  1200. * Put together a MPT SCSI request...
  1201. */
  1202. if ((mf = mpt_get_msg_frame(ioc->DoneCtx, ioc)) == NULL) {
  1203. dprintk(ioc, printk(MYIOC_s_WARN_FMT "QueueCmd, no msg frames!!\n",
  1204. ioc->name));
  1205. return SCSI_MLQUEUE_HOST_BUSY;
  1206. }
  1207. pScsiReq = (SCSIIORequest_t *) mf;
  1208. my_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  1209. ADD_INDEX_LOG(my_idx);
  1210. /* TUR's being issued with scsictl=0x02000000 (DATA_IN)!
  1211. * Seems we may receive a buffer (datalen>0) even when there
  1212. * will be no data transfer! GRRRRR...
  1213. */
  1214. if (SCpnt->sc_data_direction == DMA_FROM_DEVICE) {
  1215. datalen = scsi_bufflen(SCpnt);
  1216. scsidir = MPI_SCSIIO_CONTROL_READ; /* DATA IN (host<--ioc<--dev) */
  1217. } else if (SCpnt->sc_data_direction == DMA_TO_DEVICE) {
  1218. datalen = scsi_bufflen(SCpnt);
  1219. scsidir = MPI_SCSIIO_CONTROL_WRITE; /* DATA OUT (host-->ioc-->dev) */
  1220. } else {
  1221. datalen = 0;
  1222. scsidir = MPI_SCSIIO_CONTROL_NODATATRANSFER;
  1223. }
  1224. /* Default to untagged. Once a target structure has been allocated,
  1225. * use the Inquiry data to determine if device supports tagged.
  1226. */
  1227. if (vdevice
  1228. && (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_Q_YES)
  1229. && (SCpnt->device->tagged_supported)) {
  1230. scsictl = scsidir | MPI_SCSIIO_CONTROL_SIMPLEQ;
  1231. } else {
  1232. scsictl = scsidir | MPI_SCSIIO_CONTROL_UNTAGGED;
  1233. }
  1234. /* Use the above information to set up the message frame
  1235. */
  1236. pScsiReq->TargetID = (u8) vdevice->vtarget->id;
  1237. pScsiReq->Bus = vdevice->vtarget->channel;
  1238. pScsiReq->ChainOffset = 0;
  1239. if (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT)
  1240. pScsiReq->Function = MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
  1241. else
  1242. pScsiReq->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
  1243. pScsiReq->CDBLength = SCpnt->cmd_len;
  1244. pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
  1245. pScsiReq->Reserved = 0;
  1246. pScsiReq->MsgFlags = mpt_msg_flags();
  1247. int_to_scsilun(SCpnt->device->lun, (struct scsi_lun *)pScsiReq->LUN);
  1248. pScsiReq->Control = cpu_to_le32(scsictl);
  1249. /*
  1250. * Write SCSI CDB into the message
  1251. */
  1252. cmd_len = SCpnt->cmd_len;
  1253. for (ii=0; ii < cmd_len; ii++)
  1254. pScsiReq->CDB[ii] = SCpnt->cmnd[ii];
  1255. for (ii=cmd_len; ii < 16; ii++)
  1256. pScsiReq->CDB[ii] = 0;
  1257. /* DataLength */
  1258. pScsiReq->DataLength = cpu_to_le32(datalen);
  1259. /* SenseBuffer low address */
  1260. pScsiReq->SenseBufferLowAddr = cpu_to_le32(ioc->sense_buf_low_dma
  1261. + (my_idx * MPT_SENSE_BUFFER_ALLOC));
  1262. /* Now add the SG list
  1263. * Always have a SGE even if null length.
  1264. */
  1265. if (datalen == 0) {
  1266. /* Add a NULL SGE */
  1267. mptscsih_add_sge((char *)&pScsiReq->SGL, MPT_SGE_FLAGS_SSIMPLE_READ | 0,
  1268. (dma_addr_t) -1);
  1269. } else {
  1270. /* Add a 32 or 64 bit SGE */
  1271. if (mptscsih_AddSGE(ioc, SCpnt, pScsiReq, my_idx) != SUCCESS)
  1272. goto fail;
  1273. }
  1274. SCpnt->host_scribble = (unsigned char *)mf;
  1275. mptscsih_set_scsi_lookup(ioc, my_idx, SCpnt);
  1276. mpt_put_msg_frame(ioc->DoneCtx, ioc, mf);
  1277. dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Issued SCSI cmd (%p) mf=%p idx=%d\n",
  1278. ioc->name, SCpnt, mf, my_idx));
  1279. DBG_DUMP_REQUEST_FRAME(ioc, (u32 *)mf);
  1280. return 0;
  1281. fail:
  1282. mptscsih_freeChainBuffers(ioc, my_idx);
  1283. mpt_free_msg_frame(ioc, mf);
  1284. return SCSI_MLQUEUE_HOST_BUSY;
  1285. }
  1286. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1287. /*
  1288. * mptscsih_freeChainBuffers - Function to free chain buffers associated
  1289. * with a SCSI IO request
  1290. * @hd: Pointer to the MPT_SCSI_HOST instance
  1291. * @req_idx: Index of the SCSI IO request frame.
  1292. *
  1293. * Called if SG chain buffer allocation fails and mptscsih callbacks.
  1294. * No return.
  1295. */
  1296. static void
  1297. mptscsih_freeChainBuffers(MPT_ADAPTER *ioc, int req_idx)
  1298. {
  1299. MPT_FRAME_HDR *chain;
  1300. unsigned long flags;
  1301. int chain_idx;
  1302. int next;
  1303. /* Get the first chain index and reset
  1304. * tracker state.
  1305. */
  1306. chain_idx = ioc->ReqToChain[req_idx];
  1307. ioc->ReqToChain[req_idx] = MPT_HOST_NO_CHAIN;
  1308. while (chain_idx != MPT_HOST_NO_CHAIN) {
  1309. /* Save the next chain buffer index */
  1310. next = ioc->ChainToChain[chain_idx];
  1311. /* Free this chain buffer and reset
  1312. * tracker
  1313. */
  1314. ioc->ChainToChain[chain_idx] = MPT_HOST_NO_CHAIN;
  1315. chain = (MPT_FRAME_HDR *) (ioc->ChainBuffer
  1316. + (chain_idx * ioc->req_sz));
  1317. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1318. list_add_tail(&chain->u.frame.linkage.list, &ioc->FreeChainQ);
  1319. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1320. dmfprintk(ioc, printk(MYIOC_s_DEBUG_FMT "FreeChainBuffers (index %d)\n",
  1321. ioc->name, chain_idx));
  1322. /* handle next */
  1323. chain_idx = next;
  1324. }
  1325. return;
  1326. }
  1327. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1328. /*
  1329. * Reset Handling
  1330. */
  1331. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1332. /**
  1333. * mptscsih_TMHandler - Generic handler for SCSI Task Management.
  1334. * @hd: Pointer to MPT SCSI HOST structure
  1335. * @type: Task Management type
  1336. * @channel: channel number for task management
  1337. * @id: Logical Target ID for reset (if appropriate)
  1338. * @lun: Logical Unit for reset (if appropriate)
  1339. * @ctx2abort: Context for the task to be aborted (if appropriate)
  1340. * @timeout: timeout for task management control
  1341. *
  1342. * Fall through to mpt_HardResetHandler if: not operational, too many
  1343. * failed TM requests or handshake failure.
  1344. *
  1345. * Remark: Currently invoked from a non-interrupt thread (_bh).
  1346. *
  1347. * Remark: With old EH code, at most 1 SCSI TaskMgmt function per IOC
  1348. * will be active.
  1349. *
  1350. * Returns 0 for SUCCESS, or %FAILED.
  1351. **/
  1352. int
  1353. mptscsih_TMHandler(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 id, int lun, int ctx2abort, ulong timeout)
  1354. {
  1355. MPT_ADAPTER *ioc;
  1356. int rc = -1;
  1357. u32 ioc_raw_state;
  1358. unsigned long flags;
  1359. ioc = hd->ioc;
  1360. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TMHandler Entered!\n", ioc->name));
  1361. // SJR - CHECKME - Can we avoid this here?
  1362. // (mpt_HardResetHandler has this check...)
  1363. spin_lock_irqsave(&ioc->diagLock, flags);
  1364. if ((ioc->diagPending) || (ioc->alt_ioc && ioc->alt_ioc->diagPending)) {
  1365. spin_unlock_irqrestore(&ioc->diagLock, flags);
  1366. return FAILED;
  1367. }
  1368. spin_unlock_irqrestore(&ioc->diagLock, flags);
  1369. /* Wait a fixed amount of time for the TM pending flag to be cleared.
  1370. * If we time out and not bus reset, then we return a FAILED status
  1371. * to the caller.
  1372. * The call to mptscsih_tm_pending_wait() will set the pending flag
  1373. * if we are
  1374. * successful. Otherwise, reload the FW.
  1375. */
  1376. if (mptscsih_tm_pending_wait(hd) == FAILED) {
  1377. if (type == MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK) {
  1378. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TMHandler abort: "
  1379. "Timed out waiting for last TM (%d) to complete! \n",
  1380. ioc->name, hd->tmPending));
  1381. return FAILED;
  1382. } else if (type == MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET) {
  1383. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TMHandler target "
  1384. "reset: Timed out waiting for last TM (%d) "
  1385. "to complete! \n", ioc->name,
  1386. hd->tmPending));
  1387. return FAILED;
  1388. } else if (type == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS) {
  1389. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TMHandler bus reset: "
  1390. "Timed out waiting for last TM (%d) to complete! \n",
  1391. ioc->name, hd->tmPending));
  1392. return FAILED;
  1393. }
  1394. } else {
  1395. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1396. hd->tmPending |= (1 << type);
  1397. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1398. }
  1399. ioc_raw_state = mpt_GetIocState(ioc, 0);
  1400. if ((ioc_raw_state & MPI_IOC_STATE_MASK) != MPI_IOC_STATE_OPERATIONAL) {
  1401. printk(MYIOC_s_WARN_FMT
  1402. "TM Handler for type=%x: IOC Not operational (0x%x)!\n",
  1403. ioc->name, type, ioc_raw_state);
  1404. printk(MYIOC_s_WARN_FMT " Issuing HardReset!!\n", ioc->name);
  1405. if (mpt_HardResetHandler(ioc, CAN_SLEEP) < 0)
  1406. printk(MYIOC_s_WARN_FMT "TMHandler: HardReset "
  1407. "FAILED!!\n", ioc->name);
  1408. return FAILED;
  1409. }
  1410. if (ioc_raw_state & MPI_DOORBELL_ACTIVE) {
  1411. printk(MYIOC_s_WARN_FMT
  1412. "TM Handler for type=%x: ioc_state: "
  1413. "DOORBELL_ACTIVE (0x%x)!\n",
  1414. ioc->name, type, ioc_raw_state);
  1415. return FAILED;
  1416. }
  1417. /* Isse the Task Mgmt request.
  1418. */
  1419. if (hd->hard_resets < -1)
  1420. hd->hard_resets++;
  1421. rc = mptscsih_IssueTaskMgmt(hd, type, channel, id, lun,
  1422. ctx2abort, timeout);
  1423. if (rc)
  1424. printk(MYIOC_s_INFO_FMT "Issue of TaskMgmt failed!\n",
  1425. ioc->name);
  1426. else
  1427. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Issue of TaskMgmt Successful!\n",
  1428. ioc->name));
  1429. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1430. "TMHandler rc = %d!\n", ioc->name, rc));
  1431. return rc;
  1432. }
  1433. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1434. /**
  1435. * mptscsih_IssueTaskMgmt - Generic send Task Management function.
  1436. * @hd: Pointer to MPT_SCSI_HOST structure
  1437. * @type: Task Management type
  1438. * @channel: channel number for task management
  1439. * @id: Logical Target ID for reset (if appropriate)
  1440. * @lun: Logical Unit for reset (if appropriate)
  1441. * @ctx2abort: Context for the task to be aborted (if appropriate)
  1442. * @timeout: timeout for task management control
  1443. *
  1444. * Remark: _HardResetHandler can be invoked from an interrupt thread (timer)
  1445. * or a non-interrupt thread. In the former, must not call schedule().
  1446. *
  1447. * Not all fields are meaningfull for all task types.
  1448. *
  1449. * Returns 0 for SUCCESS, or FAILED.
  1450. *
  1451. **/
  1452. static int
  1453. mptscsih_IssueTaskMgmt(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 id, int lun, int ctx2abort, ulong timeout)
  1454. {
  1455. MPT_FRAME_HDR *mf;
  1456. SCSITaskMgmt_t *pScsiTm;
  1457. int ii;
  1458. int retval;
  1459. MPT_ADAPTER *ioc = hd->ioc;
  1460. /* Return Fail to calling function if no message frames available.
  1461. */
  1462. if ((mf = mpt_get_msg_frame(ioc->TaskCtx, ioc)) == NULL) {
  1463. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT "IssueTaskMgmt, no msg frames!!\n",
  1464. ioc->name));
  1465. return FAILED;
  1466. }
  1467. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "IssueTaskMgmt request @ %p\n",
  1468. ioc->name, mf));
  1469. /* Format the Request
  1470. */
  1471. pScsiTm = (SCSITaskMgmt_t *) mf;
  1472. pScsiTm->TargetID = id;
  1473. pScsiTm->Bus = channel;
  1474. pScsiTm->ChainOffset = 0;
  1475. pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
  1476. pScsiTm->Reserved = 0;
  1477. pScsiTm->TaskType = type;
  1478. pScsiTm->Reserved1 = 0;
  1479. pScsiTm->MsgFlags = (type == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS)
  1480. ? MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION : 0;
  1481. int_to_scsilun(lun, (struct scsi_lun *)pScsiTm->LUN);
  1482. for (ii=0; ii < 7; ii++)
  1483. pScsiTm->Reserved2[ii] = 0;
  1484. pScsiTm->TaskMsgContext = ctx2abort;
  1485. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "IssueTaskMgmt: ctx2abort (0x%08x) "
  1486. "type=%d\n", ioc->name, ctx2abort, type));
  1487. DBG_DUMP_TM_REQUEST_FRAME(ioc, (u32 *)pScsiTm);
  1488. if ((ioc->facts.IOCCapabilities & MPI_IOCFACTS_CAPABILITY_HIGH_PRI_Q) &&
  1489. (ioc->facts.MsgVersion >= MPI_VERSION_01_05))
  1490. mpt_put_msg_frame_hi_pri(ioc->TaskCtx, ioc, mf);
  1491. else {
  1492. retval = mpt_send_handshake_request(ioc->TaskCtx, ioc,
  1493. sizeof(SCSITaskMgmt_t), (u32*)pScsiTm, CAN_SLEEP);
  1494. if (retval) {
  1495. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT "send_handshake FAILED!"
  1496. " (hd %p, ioc %p, mf %p, rc=%d) \n", ioc->name, hd,
  1497. ioc, mf, retval));
  1498. goto fail_out;
  1499. }
  1500. }
  1501. if(mptscsih_tm_wait_for_completion(hd, timeout) == FAILED) {
  1502. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT "task management request TIMED OUT!"
  1503. " (hd %p, ioc %p, mf %p) \n", ioc->name, hd,
  1504. ioc, mf));
  1505. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Calling HardReset! \n",
  1506. ioc->name));
  1507. retval = mpt_HardResetHandler(ioc, CAN_SLEEP);
  1508. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "rc=%d \n",
  1509. ioc->name, retval));
  1510. goto fail_out;
  1511. }
  1512. /*
  1513. * Handle success case, see if theres a non-zero ioc_status.
  1514. */
  1515. if (hd->tm_iocstatus == MPI_IOCSTATUS_SUCCESS ||
  1516. hd->tm_iocstatus == MPI_IOCSTATUS_SCSI_TASK_TERMINATED ||
  1517. hd->tm_iocstatus == MPI_IOCSTATUS_SCSI_IOC_TERMINATED)
  1518. retval = 0;
  1519. else
  1520. retval = FAILED;
  1521. return retval;
  1522. fail_out:
  1523. /*
  1524. * Free task managment mf, and corresponding tm flags
  1525. */
  1526. mpt_free_msg_frame(ioc, mf);
  1527. hd->tmPending = 0;
  1528. hd->tmState = TM_STATE_NONE;
  1529. return FAILED;
  1530. }
  1531. static int
  1532. mptscsih_get_tm_timeout(MPT_ADAPTER *ioc)
  1533. {
  1534. switch (ioc->bus_type) {
  1535. case FC:
  1536. return 40;
  1537. case SAS:
  1538. return 10;
  1539. case SPI:
  1540. default:
  1541. return 2;
  1542. }
  1543. }
  1544. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1545. /**
  1546. * mptscsih_abort - Abort linux scsi_cmnd routine, new_eh variant
  1547. * @SCpnt: Pointer to scsi_cmnd structure, IO to be aborted
  1548. *
  1549. * (linux scsi_host_template.eh_abort_handler routine)
  1550. *
  1551. * Returns SUCCESS or FAILED.
  1552. **/
  1553. int
  1554. mptscsih_abort(struct scsi_cmnd * SCpnt)
  1555. {
  1556. MPT_SCSI_HOST *hd;
  1557. MPT_FRAME_HDR *mf;
  1558. u32 ctx2abort;
  1559. int scpnt_idx;
  1560. int retval;
  1561. VirtDevice *vdevice;
  1562. ulong sn = SCpnt->serial_number;
  1563. MPT_ADAPTER *ioc;
  1564. /* If we can't locate our host adapter structure, return FAILED status.
  1565. */
  1566. if ((hd = shost_priv(SCpnt->device->host)) == NULL) {
  1567. SCpnt->result = DID_RESET << 16;
  1568. SCpnt->scsi_done(SCpnt);
  1569. printk(KERN_ERR MYNAM ": task abort: "
  1570. "can't locate host! (sc=%p)\n", SCpnt);
  1571. return FAILED;
  1572. }
  1573. ioc = hd->ioc;
  1574. printk(MYIOC_s_INFO_FMT "attempting task abort! (sc=%p)\n",
  1575. ioc->name, SCpnt);
  1576. scsi_print_command(SCpnt);
  1577. vdevice = SCpnt->device->hostdata;
  1578. if (!vdevice || !vdevice->vtarget) {
  1579. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1580. "task abort: device has been deleted (sc=%p)\n",
  1581. ioc->name, SCpnt));
  1582. SCpnt->result = DID_NO_CONNECT << 16;
  1583. SCpnt->scsi_done(SCpnt);
  1584. retval = 0;
  1585. goto out;
  1586. }
  1587. /* Task aborts are not supported for hidden raid components.
  1588. */
  1589. if (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1590. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1591. "task abort: hidden raid component (sc=%p)\n",
  1592. ioc->name, SCpnt));
  1593. SCpnt->result = DID_RESET << 16;
  1594. retval = FAILED;
  1595. goto out;
  1596. }
  1597. /* Find this command
  1598. */
  1599. if ((scpnt_idx = SCPNT_TO_LOOKUP_IDX(ioc, SCpnt)) < 0) {
  1600. /* Cmd not found in ScsiLookup.
  1601. * Do OS callback.
  1602. */
  1603. SCpnt->result = DID_RESET << 16;
  1604. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "task abort: "
  1605. "Command not in the active list! (sc=%p)\n", ioc->name,
  1606. SCpnt));
  1607. retval = 0;
  1608. goto out;
  1609. }
  1610. if (hd->resetPending) {
  1611. retval = FAILED;
  1612. goto out;
  1613. }
  1614. if (hd->timeouts < -1)
  1615. hd->timeouts++;
  1616. /* Most important! Set TaskMsgContext to SCpnt's MsgContext!
  1617. * (the IO to be ABORT'd)
  1618. *
  1619. * NOTE: Since we do not byteswap MsgContext, we do not
  1620. * swap it here either. It is an opaque cookie to
  1621. * the controller, so it does not matter. -DaveM
  1622. */
  1623. mf = MPT_INDEX_2_MFPTR(ioc, scpnt_idx);
  1624. ctx2abort = mf->u.frame.hwhdr.msgctxu.MsgContext;
  1625. hd->abortSCpnt = SCpnt;
  1626. retval = mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
  1627. vdevice->vtarget->channel, vdevice->vtarget->id, vdevice->lun,
  1628. ctx2abort, mptscsih_get_tm_timeout(ioc));
  1629. if (SCPNT_TO_LOOKUP_IDX(ioc, SCpnt) == scpnt_idx &&
  1630. SCpnt->serial_number == sn)
  1631. retval = FAILED;
  1632. out:
  1633. printk(MYIOC_s_INFO_FMT "task abort: %s (sc=%p)\n",
  1634. ioc->name, ((retval == 0) ? "SUCCESS" : "FAILED" ), SCpnt);
  1635. if (retval == 0)
  1636. return SUCCESS;
  1637. else
  1638. return FAILED;
  1639. }
  1640. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1641. /**
  1642. * mptscsih_dev_reset - Perform a SCSI TARGET_RESET! new_eh variant
  1643. * @SCpnt: Pointer to scsi_cmnd structure, IO which reset is due to
  1644. *
  1645. * (linux scsi_host_template.eh_dev_reset_handler routine)
  1646. *
  1647. * Returns SUCCESS or FAILED.
  1648. **/
  1649. int
  1650. mptscsih_dev_reset(struct scsi_cmnd * SCpnt)
  1651. {
  1652. MPT_SCSI_HOST *hd;
  1653. int retval;
  1654. VirtDevice *vdevice;
  1655. MPT_ADAPTER *ioc;
  1656. /* If we can't locate our host adapter structure, return FAILED status.
  1657. */
  1658. if ((hd = shost_priv(SCpnt->device->host)) == NULL){
  1659. printk(KERN_ERR MYNAM ": target reset: "
  1660. "Can't locate host! (sc=%p)\n", SCpnt);
  1661. return FAILED;
  1662. }
  1663. ioc = hd->ioc;
  1664. printk(MYIOC_s_INFO_FMT "attempting target reset! (sc=%p)\n",
  1665. ioc->name, SCpnt);
  1666. scsi_print_command(SCpnt);
  1667. if (hd->resetPending) {
  1668. retval = FAILED;
  1669. goto out;
  1670. }
  1671. vdevice = SCpnt->device->hostdata;
  1672. if (!vdevice || !vdevice->vtarget) {
  1673. retval = 0;
  1674. goto out;
  1675. }
  1676. /* Target reset to hidden raid component is not supported
  1677. */
  1678. if (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1679. retval = FAILED;
  1680. goto out;
  1681. }
  1682. retval = mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
  1683. vdevice->vtarget->channel, vdevice->vtarget->id, 0, 0,
  1684. mptscsih_get_tm_timeout(ioc));
  1685. out:
  1686. printk (MYIOC_s_INFO_FMT "target reset: %s (sc=%p)\n",
  1687. ioc->name, ((retval == 0) ? "SUCCESS" : "FAILED" ), SCpnt);
  1688. if (retval == 0)
  1689. return SUCCESS;
  1690. else
  1691. return FAILED;
  1692. }
  1693. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1694. /**
  1695. * mptscsih_bus_reset - Perform a SCSI BUS_RESET! new_eh variant
  1696. * @SCpnt: Pointer to scsi_cmnd structure, IO which reset is due to
  1697. *
  1698. * (linux scsi_host_template.eh_bus_reset_handler routine)
  1699. *
  1700. * Returns SUCCESS or FAILED.
  1701. **/
  1702. int
  1703. mptscsih_bus_reset(struct scsi_cmnd * SCpnt)
  1704. {
  1705. MPT_SCSI_HOST *hd;
  1706. int retval;
  1707. VirtDevice *vdevice;
  1708. MPT_ADAPTER *ioc;
  1709. /* If we can't locate our host adapter structure, return FAILED status.
  1710. */
  1711. if ((hd = shost_priv(SCpnt->device->host)) == NULL){
  1712. printk(KERN_ERR MYNAM ": bus reset: "
  1713. "Can't locate host! (sc=%p)\n", SCpnt);
  1714. return FAILED;
  1715. }
  1716. ioc = hd->ioc;
  1717. printk(MYIOC_s_INFO_FMT "attempting bus reset! (sc=%p)\n",
  1718. ioc->name, SCpnt);
  1719. scsi_print_command(SCpnt);
  1720. if (hd->timeouts < -1)
  1721. hd->timeouts++;
  1722. vdevice = SCpnt->device->hostdata;
  1723. retval = mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,
  1724. vdevice->vtarget->channel, 0, 0, 0, mptscsih_get_tm_timeout(ioc));
  1725. printk(MYIOC_s_INFO_FMT "bus reset: %s (sc=%p)\n",
  1726. ioc->name, ((retval == 0) ? "SUCCESS" : "FAILED" ), SCpnt);
  1727. if (retval == 0)
  1728. return SUCCESS;
  1729. else
  1730. return FAILED;
  1731. }
  1732. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1733. /**
  1734. * mptscsih_host_reset - Perform a SCSI host adapter RESET (new_eh variant)
  1735. * @SCpnt: Pointer to scsi_cmnd structure, IO which reset is due to
  1736. *
  1737. * (linux scsi_host_template.eh_host_reset_handler routine)
  1738. *
  1739. * Returns SUCCESS or FAILED.
  1740. */
  1741. int
  1742. mptscsih_host_reset(struct scsi_cmnd *SCpnt)
  1743. {
  1744. MPT_SCSI_HOST * hd;
  1745. int retval;
  1746. MPT_ADAPTER *ioc;
  1747. /* If we can't locate the host to reset, then we failed. */
  1748. if ((hd = shost_priv(SCpnt->device->host)) == NULL){
  1749. printk(KERN_ERR MYNAM ": host reset: "
  1750. "Can't locate host! (sc=%p)\n", SCpnt);
  1751. return FAILED;
  1752. }
  1753. ioc = hd->ioc;
  1754. printk(MYIOC_s_INFO_FMT "attempting host reset! (sc=%p)\n",
  1755. ioc->name, SCpnt);
  1756. /* If our attempts to reset the host failed, then return a failed
  1757. * status. The host will be taken off line by the SCSI mid-layer.
  1758. */
  1759. if (mpt_HardResetHandler(ioc, CAN_SLEEP) < 0) {
  1760. retval = FAILED;
  1761. } else {
  1762. /* Make sure TM pending is cleared and TM state is set to
  1763. * NONE.
  1764. */
  1765. retval = 0;
  1766. hd->tmPending = 0;
  1767. hd->tmState = TM_STATE_NONE;
  1768. }
  1769. printk(MYIOC_s_INFO_FMT "host reset: %s (sc=%p)\n",
  1770. ioc->name, ((retval == 0) ? "SUCCESS" : "FAILED" ), SCpnt);
  1771. return retval;
  1772. }
  1773. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1774. /**
  1775. * mptscsih_tm_pending_wait - wait for pending task management request to complete
  1776. * @hd: Pointer to MPT host structure.
  1777. *
  1778. * Returns {SUCCESS,FAILED}.
  1779. */
  1780. static int
  1781. mptscsih_tm_pending_wait(MPT_SCSI_HOST * hd)
  1782. {
  1783. unsigned long flags;
  1784. int loop_count = 4 * 10; /* Wait 10 seconds */
  1785. int status = FAILED;
  1786. MPT_ADAPTER *ioc = hd->ioc;
  1787. do {
  1788. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1789. if (hd->tmState == TM_STATE_NONE) {
  1790. hd->tmState = TM_STATE_IN_PROGRESS;
  1791. hd->tmPending = 1;
  1792. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1793. status = SUCCESS;
  1794. break;
  1795. }
  1796. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1797. msleep(250);
  1798. } while (--loop_count);
  1799. return status;
  1800. }
  1801. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1802. /**
  1803. * mptscsih_tm_wait_for_completion - wait for completion of TM task
  1804. * @hd: Pointer to MPT host structure.
  1805. * @timeout: timeout value
  1806. *
  1807. * Returns {SUCCESS,FAILED}.
  1808. */
  1809. static int
  1810. mptscsih_tm_wait_for_completion(MPT_SCSI_HOST * hd, ulong timeout )
  1811. {
  1812. unsigned long flags;
  1813. int loop_count = 4 * timeout;
  1814. int status = FAILED;
  1815. MPT_ADAPTER *ioc = hd->ioc;
  1816. do {
  1817. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1818. if(hd->tmPending == 0) {
  1819. status = SUCCESS;
  1820. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1821. break;
  1822. }
  1823. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1824. msleep(250);
  1825. } while (--loop_count);
  1826. return status;
  1827. }
  1828. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1829. static void
  1830. mptscsih_taskmgmt_response_code(MPT_ADAPTER *ioc, u8 response_code)
  1831. {
  1832. char *desc;
  1833. switch (response_code) {
  1834. case MPI_SCSITASKMGMT_RSP_TM_COMPLETE:
  1835. desc = "The task completed.";
  1836. break;
  1837. case MPI_SCSITASKMGMT_RSP_INVALID_FRAME:
  1838. desc = "The IOC received an invalid frame status.";
  1839. break;
  1840. case MPI_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
  1841. desc = "The task type is not supported.";
  1842. break;
  1843. case MPI_SCSITASKMGMT_RSP_TM_FAILED:
  1844. desc = "The requested task failed.";
  1845. break;
  1846. case MPI_SCSITASKMGMT_RSP_TM_SUCCEEDED:
  1847. desc = "The task completed successfully.";
  1848. break;
  1849. case MPI_SCSITASKMGMT_RSP_TM_INVALID_LUN:
  1850. desc = "The LUN request is invalid.";
  1851. break;
  1852. case MPI_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
  1853. desc = "The task is in the IOC queue and has not been sent to target.";
  1854. break;
  1855. default:
  1856. desc = "unknown";
  1857. break;
  1858. }
  1859. printk(MYIOC_s_INFO_FMT "Response Code(0x%08x): F/W: %s\n",
  1860. ioc->name, response_code, desc);
  1861. }
  1862. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1863. /**
  1864. * mptscsih_taskmgmt_complete - Registered with Fusion MPT base driver
  1865. * @ioc: Pointer to MPT_ADAPTER structure
  1866. * @mf: Pointer to SCSI task mgmt request frame
  1867. * @mr: Pointer to SCSI task mgmt reply frame
  1868. *
  1869. * This routine is called from mptbase.c::mpt_interrupt() at the completion
  1870. * of any SCSI task management request.
  1871. * This routine is registered with the MPT (base) driver at driver
  1872. * load/init time via the mpt_register() API call.
  1873. *
  1874. * Returns 1 indicating alloc'd request frame ptr should be freed.
  1875. **/
  1876. int
  1877. mptscsih_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
  1878. {
  1879. SCSITaskMgmtReply_t *pScsiTmReply;
  1880. SCSITaskMgmt_t *pScsiTmReq;
  1881. MPT_SCSI_HOST *hd;
  1882. unsigned long flags;
  1883. u16 iocstatus;
  1884. u8 tmType;
  1885. u32 termination_count;
  1886. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TaskMgmt completed (mf=%p,mr=%p)\n",
  1887. ioc->name, mf, mr));
  1888. if (!ioc->sh) {
  1889. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT
  1890. "TaskMgmt Complete: NULL Scsi Host Ptr\n", ioc->name));
  1891. return 1;
  1892. }
  1893. if (mr == NULL) {
  1894. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT
  1895. "ERROR! TaskMgmt Reply: NULL Request %p\n", ioc->name, mf));
  1896. return 1;
  1897. }
  1898. hd = shost_priv(ioc->sh);
  1899. pScsiTmReply = (SCSITaskMgmtReply_t*)mr;
  1900. pScsiTmReq = (SCSITaskMgmt_t*)mf;
  1901. tmType = pScsiTmReq->TaskType;
  1902. iocstatus = le16_to_cpu(pScsiTmReply->IOCStatus) & MPI_IOCSTATUS_MASK;
  1903. termination_count = le32_to_cpu(pScsiTmReply->TerminationCount);
  1904. if (ioc->facts.MsgVersion >= MPI_VERSION_01_05 &&
  1905. pScsiTmReply->ResponseCode)
  1906. mptscsih_taskmgmt_response_code(ioc,
  1907. pScsiTmReply->ResponseCode);
  1908. DBG_DUMP_TM_REPLY_FRAME(ioc, (u32 *)pScsiTmReply);
  1909. #ifdef CONFIG_FUSION_LOGGING
  1910. if ((ioc->debug_level & MPT_DEBUG_REPLY) ||
  1911. (ioc->debug_level & MPT_DEBUG_TM ))
  1912. printk("%s: ha=%d [%d:%d:0] task_type=0x%02X "
  1913. "iocstatus=0x%04X\n\tloginfo=0x%08X response_code=0x%02X "
  1914. "term_cmnds=%d\n", __FUNCTION__, ioc->id, pScsiTmReply->Bus,
  1915. pScsiTmReply->TargetID, pScsiTmReq->TaskType,
  1916. le16_to_cpu(pScsiTmReply->IOCStatus),
  1917. le32_to_cpu(pScsiTmReply->IOCLogInfo),pScsiTmReply->ResponseCode,
  1918. le32_to_cpu(pScsiTmReply->TerminationCount));
  1919. #endif
  1920. if (!iocstatus) {
  1921. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT " TaskMgmt SUCCESS\n", ioc->name));
  1922. hd->abortSCpnt = NULL;
  1923. goto out;
  1924. }
  1925. /* Error? (anything non-zero?) */
  1926. /* clear flags and continue.
  1927. */
  1928. switch (tmType) {
  1929. case MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
  1930. if (termination_count == 1)
  1931. iocstatus = MPI_IOCSTATUS_SCSI_TASK_TERMINATED;
  1932. hd->abortSCpnt = NULL;
  1933. break;
  1934. case MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS:
  1935. /* If an internal command is present
  1936. * or the TM failed - reload the FW.
  1937. * FC FW may respond FAILED to an ABORT
  1938. */
  1939. if (iocstatus == MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED ||
  1940. hd->cmdPtr)
  1941. if (mpt_HardResetHandler(ioc, NO_SLEEP) < 0)
  1942. printk(MYIOC_s_WARN_FMT " Firmware Reload FAILED!!\n", ioc->name);
  1943. break;
  1944. case MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
  1945. default:
  1946. break;
  1947. }
  1948. out:
  1949. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1950. hd->tmPending = 0;
  1951. hd->tmState = TM_STATE_NONE;
  1952. hd->tm_iocstatus = iocstatus;
  1953. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1954. return 1;
  1955. }
  1956. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1957. /*
  1958. * This is anyones guess quite frankly.
  1959. */
  1960. int
  1961. mptscsih_bios_param(struct scsi_device * sdev, struct block_device *bdev,
  1962. sector_t capacity, int geom[])
  1963. {
  1964. int heads;
  1965. int sectors;
  1966. sector_t cylinders;
  1967. ulong dummy;
  1968. heads = 64;
  1969. sectors = 32;
  1970. dummy = heads * sectors;
  1971. cylinders = capacity;
  1972. sector_div(cylinders,dummy);
  1973. /*
  1974. * Handle extended translation size for logical drives
  1975. * > 1Gb
  1976. */
  1977. if ((ulong)capacity >= 0x200000) {
  1978. heads = 255;
  1979. sectors = 63;
  1980. dummy = heads * sectors;
  1981. cylinders = capacity;
  1982. sector_div(cylinders,dummy);
  1983. }
  1984. /* return result */
  1985. geom[0] = heads;
  1986. geom[1] = sectors;
  1987. geom[2] = cylinders;
  1988. return 0;
  1989. }
  1990. /* Search IOC page 3 to determine if this is hidden physical disk
  1991. *
  1992. */
  1993. int
  1994. mptscsih_is_phys_disk(MPT_ADAPTER *ioc, u8 channel, u8 id)
  1995. {
  1996. struct inactive_raid_component_info *component_info;
  1997. int i;
  1998. int rc = 0;
  1999. if (!ioc->raid_data.pIocPg3)
  2000. goto out;
  2001. for (i = 0; i < ioc->raid_data.pIocPg3->NumPhysDisks; i++) {
  2002. if ((id == ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskID) &&
  2003. (channel == ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskBus)) {
  2004. rc = 1;
  2005. goto out;
  2006. }
  2007. }
  2008. /*
  2009. * Check inactive list for matching phys disks
  2010. */
  2011. if (list_empty(&ioc->raid_data.inactive_list))
  2012. goto out;
  2013. down(&ioc->raid_data.inactive_list_mutex);
  2014. list_for_each_entry(component_info, &ioc->raid_data.inactive_list,
  2015. list) {
  2016. if ((component_info->d.PhysDiskID == id) &&
  2017. (component_info->d.PhysDiskBus == channel))
  2018. rc = 1;
  2019. }
  2020. up(&ioc->raid_data.inactive_list_mutex);
  2021. out:
  2022. return rc;
  2023. }
  2024. EXPORT_SYMBOL(mptscsih_is_phys_disk);
  2025. u8
  2026. mptscsih_raid_id_to_num(MPT_ADAPTER *ioc, u8 channel, u8 id)
  2027. {
  2028. struct inactive_raid_component_info *component_info;
  2029. int i;
  2030. int rc = -ENXIO;
  2031. if (!ioc->raid_data.pIocPg3)
  2032. goto out;
  2033. for (i = 0; i < ioc->raid_data.pIocPg3->NumPhysDisks; i++) {
  2034. if ((id == ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskID) &&
  2035. (channel == ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskBus)) {
  2036. rc = ioc->raid_data.pIocPg3->PhysDisk[i].PhysDiskNum;
  2037. goto out;
  2038. }
  2039. }
  2040. /*
  2041. * Check inactive list for matching phys disks
  2042. */
  2043. if (list_empty(&ioc->raid_data.inactive_list))
  2044. goto out;
  2045. down(&ioc->raid_data.inactive_list_mutex);
  2046. list_for_each_entry(component_info, &ioc->raid_data.inactive_list,
  2047. list) {
  2048. if ((component_info->d.PhysDiskID == id) &&
  2049. (component_info->d.PhysDiskBus == channel))
  2050. rc = component_info->d.PhysDiskNum;
  2051. }
  2052. up(&ioc->raid_data.inactive_list_mutex);
  2053. out:
  2054. return rc;
  2055. }
  2056. EXPORT_SYMBOL(mptscsih_raid_id_to_num);
  2057. /*
  2058. * OS entry point to allow for host driver to free allocated memory
  2059. * Called if no device present or device being unloaded
  2060. */
  2061. void
  2062. mptscsih_slave_destroy(struct scsi_device *sdev)
  2063. {
  2064. struct Scsi_Host *host = sdev->host;
  2065. MPT_SCSI_HOST *hd = shost_priv(host);
  2066. VirtTarget *vtarget;
  2067. VirtDevice *vdevice;
  2068. struct scsi_target *starget;
  2069. starget = scsi_target(sdev);
  2070. vtarget = starget->hostdata;
  2071. vdevice = sdev->hostdata;
  2072. mptscsih_search_running_cmds(hd, vdevice);
  2073. vtarget->num_luns--;
  2074. mptscsih_synchronize_cache(hd, vdevice);
  2075. kfree(vdevice);
  2076. sdev->hostdata = NULL;
  2077. }
  2078. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2079. /*
  2080. * mptscsih_change_queue_depth - This function will set a devices queue depth
  2081. * @sdev: per scsi_device pointer
  2082. * @qdepth: requested queue depth
  2083. *
  2084. * Adding support for new 'change_queue_depth' api.
  2085. */
  2086. int
  2087. mptscsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
  2088. {
  2089. MPT_SCSI_HOST *hd = shost_priv(sdev->host);
  2090. VirtTarget *vtarget;
  2091. struct scsi_target *starget;
  2092. int max_depth;
  2093. int tagged;
  2094. MPT_ADAPTER *ioc = hd->ioc;
  2095. starget = scsi_target(sdev);
  2096. vtarget = starget->hostdata;
  2097. if (ioc->bus_type == SPI) {
  2098. if (!(vtarget->tflags & MPT_TARGET_FLAGS_Q_YES))
  2099. max_depth = 1;
  2100. else if (sdev->type == TYPE_DISK &&
  2101. vtarget->minSyncFactor <= MPT_ULTRA160)
  2102. max_depth = MPT_SCSI_CMD_PER_DEV_HIGH;
  2103. else
  2104. max_depth = MPT_SCSI_CMD_PER_DEV_LOW;
  2105. } else
  2106. max_depth = MPT_SCSI_CMD_PER_DEV_HIGH;
  2107. if (qdepth > max_depth)
  2108. qdepth = max_depth;
  2109. if (qdepth == 1)
  2110. tagged = 0;
  2111. else
  2112. tagged = MSG_SIMPLE_TAG;
  2113. scsi_adjust_queue_depth(sdev, tagged, qdepth);
  2114. return sdev->queue_depth;
  2115. }
  2116. /*
  2117. * OS entry point to adjust the queue_depths on a per-device basis.
  2118. * Called once per device the bus scan. Use it to force the queue_depth
  2119. * member to 1 if a device does not support Q tags.
  2120. * Return non-zero if fails.
  2121. */
  2122. int
  2123. mptscsih_slave_configure(struct scsi_device *sdev)
  2124. {
  2125. struct Scsi_Host *sh = sdev->host;
  2126. VirtTarget *vtarget;
  2127. VirtDevice *vdevice;
  2128. struct scsi_target *starget;
  2129. MPT_SCSI_HOST *hd = shost_priv(sh);
  2130. MPT_ADAPTER *ioc = hd->ioc;
  2131. starget = scsi_target(sdev);
  2132. vtarget = starget->hostdata;
  2133. vdevice = sdev->hostdata;
  2134. dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2135. "device @ %p, channel=%d, id=%d, lun=%d\n",
  2136. ioc->name, sdev, sdev->channel, sdev->id, sdev->lun));
  2137. if (ioc->bus_type == SPI)
  2138. dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2139. "sdtr %d wdtr %d ppr %d inq length=%d\n",
  2140. ioc->name, sdev->sdtr, sdev->wdtr,
  2141. sdev->ppr, sdev->inquiry_len));
  2142. if (sdev->id > sh->max_id) {
  2143. /* error case, should never happen */
  2144. scsi_adjust_queue_depth(sdev, 0, 1);
  2145. goto slave_configure_exit;
  2146. }
  2147. vdevice->configured_lun = 1;
  2148. mptscsih_change_queue_depth(sdev, MPT_SCSI_CMD_PER_DEV_HIGH);
  2149. dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2150. "Queue depth=%d, tflags=%x\n",
  2151. ioc->name, sdev->queue_depth, vtarget->tflags));
  2152. if (ioc->bus_type == SPI)
  2153. dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2154. "negoFlags=%x, maxOffset=%x, SyncFactor=%x\n",
  2155. ioc->name, vtarget->negoFlags, vtarget->maxOffset,
  2156. vtarget->minSyncFactor));
  2157. slave_configure_exit:
  2158. dsprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2159. "tagged %d, simple %d, ordered %d\n",
  2160. ioc->name,sdev->tagged_supported, sdev->simple_tags,
  2161. sdev->ordered_tags));
  2162. return 0;
  2163. }
  2164. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2165. /*
  2166. * Private routines...
  2167. */
  2168. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2169. /* Utility function to copy sense data from the scsi_cmnd buffer
  2170. * to the FC and SCSI target structures.
  2171. *
  2172. */
  2173. static void
  2174. mptscsih_copy_sense_data(struct scsi_cmnd *sc, MPT_SCSI_HOST *hd, MPT_FRAME_HDR *mf, SCSIIOReply_t *pScsiReply)
  2175. {
  2176. VirtDevice *vdevice;
  2177. SCSIIORequest_t *pReq;
  2178. u32 sense_count = le32_to_cpu(pScsiReply->SenseCount);
  2179. MPT_ADAPTER *ioc = hd->ioc;
  2180. /* Get target structure
  2181. */
  2182. pReq = (SCSIIORequest_t *) mf;
  2183. vdevice = sc->device->hostdata;
  2184. if (sense_count) {
  2185. u8 *sense_data;
  2186. int req_index;
  2187. /* Copy the sense received into the scsi command block. */
  2188. req_index = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  2189. sense_data = ((u8 *)ioc->sense_buf_pool + (req_index * MPT_SENSE_BUFFER_ALLOC));
  2190. memcpy(sc->sense_buffer, sense_data, SNS_LEN(sc));
  2191. /* Log SMART data (asc = 0x5D, non-IM case only) if required.
  2192. */
  2193. if ((ioc->events) && (ioc->eventTypes & (1 << MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE))) {
  2194. if ((sense_data[12] == 0x5D) && (vdevice->vtarget->raidVolume == 0)) {
  2195. int idx;
  2196. idx = ioc->eventContext % MPTCTL_EVENT_LOG_SIZE;
  2197. ioc->events[idx].event = MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE;
  2198. ioc->events[idx].eventContext = ioc->eventContext;
  2199. ioc->events[idx].data[0] = (pReq->LUN[1] << 24) |
  2200. (MPI_EVENT_SCSI_DEV_STAT_RC_SMART_DATA << 16) |
  2201. (sc->device->channel << 8) | sc->device->id;
  2202. ioc->events[idx].data[1] = (sense_data[13] << 8) | sense_data[12];
  2203. ioc->eventContext++;
  2204. if (ioc->pcidev->vendor ==
  2205. PCI_VENDOR_ID_IBM) {
  2206. mptscsih_issue_sep_command(ioc,
  2207. vdevice->vtarget, MPI_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
  2208. vdevice->vtarget->tflags |=
  2209. MPT_TARGET_FLAGS_LED_ON;
  2210. }
  2211. }
  2212. }
  2213. } else {
  2214. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Hmmm... SenseData len=0! (?)\n",
  2215. ioc->name));
  2216. }
  2217. }
  2218. /**
  2219. * mptscsih_get_scsi_lookup
  2220. *
  2221. * retrieves scmd entry from ScsiLookup[] array list
  2222. *
  2223. * @ioc: Pointer to MPT_ADAPTER structure
  2224. * @i: index into the array
  2225. *
  2226. * Returns the scsi_cmd pointer
  2227. *
  2228. **/
  2229. static struct scsi_cmnd *
  2230. mptscsih_get_scsi_lookup(MPT_ADAPTER *ioc, int i)
  2231. {
  2232. unsigned long flags;
  2233. struct scsi_cmnd *scmd;
  2234. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  2235. scmd = ioc->ScsiLookup[i];
  2236. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2237. return scmd;
  2238. }
  2239. /**
  2240. * mptscsih_getclear_scsi_lookup
  2241. *
  2242. * retrieves and clears scmd entry from ScsiLookup[] array list
  2243. *
  2244. * @ioc: Pointer to MPT_ADAPTER structure
  2245. * @i: index into the array
  2246. *
  2247. * Returns the scsi_cmd pointer
  2248. *
  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
  2281. *
  2282. * search's for a given scmd in the ScsiLookup[] array list
  2283. *
  2284. * @ioc: Pointer to MPT_ADAPTER structure
  2285. * @scmd: scsi_cmnd pointer
  2286. *
  2287. **/
  2288. static int
  2289. SCPNT_TO_LOOKUP_IDX(MPT_ADAPTER *ioc, struct scsi_cmnd *sc)
  2290. {
  2291. unsigned long flags;
  2292. int i, index=-1;
  2293. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  2294. for (i = 0; i < ioc->req_depth; i++) {
  2295. if (ioc->ScsiLookup[i] == sc) {
  2296. index = i;
  2297. goto out;
  2298. }
  2299. }
  2300. out:
  2301. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  2302. return index;
  2303. }
  2304. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2305. int
  2306. mptscsih_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
  2307. {
  2308. MPT_SCSI_HOST *hd;
  2309. unsigned long flags;
  2310. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2311. ": IOC %s_reset routed to SCSI host driver!\n",
  2312. ioc->name, reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
  2313. reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
  2314. /* If a FW reload request arrives after base installed but
  2315. * before all scsi hosts have been attached, then an alt_ioc
  2316. * may have a NULL sh pointer.
  2317. */
  2318. if (ioc->sh == NULL || shost_priv(ioc->sh) == NULL)
  2319. return 0;
  2320. else
  2321. hd = shost_priv(ioc->sh);
  2322. if (reset_phase == MPT_IOC_SETUP_RESET) {
  2323. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Setup-Diag Reset\n", ioc->name));
  2324. /* Clean Up:
  2325. * 1. Set Hard Reset Pending Flag
  2326. * All new commands go to doneQ
  2327. */
  2328. hd->resetPending = 1;
  2329. } else if (reset_phase == MPT_IOC_PRE_RESET) {
  2330. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Pre-Diag Reset\n", ioc->name));
  2331. /* 2. Flush running commands
  2332. * Clean ScsiLookup (and associated memory)
  2333. * AND clean mytaskQ
  2334. */
  2335. /* 2b. Reply to OS all known outstanding I/O commands.
  2336. */
  2337. mptscsih_flush_running_cmds(hd);
  2338. /* 2c. If there was an internal command that
  2339. * has not completed, configuration or io request,
  2340. * free these resources.
  2341. */
  2342. if (hd->cmdPtr) {
  2343. del_timer(&hd->timer);
  2344. mpt_free_msg_frame(ioc, hd->cmdPtr);
  2345. }
  2346. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Pre-Reset complete.\n", ioc->name));
  2347. } else {
  2348. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Post-Diag Reset\n", ioc->name));
  2349. /* Once a FW reload begins, all new OS commands are
  2350. * redirected to the doneQ w/ a reset status.
  2351. * Init all control structures.
  2352. */
  2353. /* 2. Chain Buffer initialization
  2354. */
  2355. /* 4. Renegotiate to all devices, if SPI
  2356. */
  2357. /* 5. Enable new commands to be posted
  2358. */
  2359. spin_lock_irqsave(&ioc->FreeQlock, flags);
  2360. hd->tmPending = 0;
  2361. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  2362. hd->resetPending = 0;
  2363. hd->tmState = TM_STATE_NONE;
  2364. /* 6. If there was an internal command,
  2365. * wake this process up.
  2366. */
  2367. if (hd->cmdPtr) {
  2368. /*
  2369. * Wake up the original calling thread
  2370. */
  2371. hd->pLocal = &hd->localReply;
  2372. hd->pLocal->completion = MPT_SCANDV_DID_RESET;
  2373. hd->scandv_wait_done = 1;
  2374. wake_up(&hd->scandv_waitq);
  2375. hd->cmdPtr = NULL;
  2376. }
  2377. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Post-Reset complete.\n", ioc->name));
  2378. }
  2379. return 1; /* currently means nothing really */
  2380. }
  2381. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2382. int
  2383. mptscsih_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
  2384. {
  2385. MPT_SCSI_HOST *hd;
  2386. u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
  2387. devtverboseprintk(ioc, printk(MYIOC_s_DEBUG_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
  2388. ioc->name, event));
  2389. if (ioc->sh == NULL ||
  2390. ((hd = shost_priv(ioc->sh)) == NULL))
  2391. return 1;
  2392. switch (event) {
  2393. case MPI_EVENT_UNIT_ATTENTION: /* 03 */
  2394. /* FIXME! */
  2395. break;
  2396. case MPI_EVENT_IOC_BUS_RESET: /* 04 */
  2397. case MPI_EVENT_EXT_BUS_RESET: /* 05 */
  2398. if (hd && (ioc->bus_type == SPI) && (hd->soft_resets < -1))
  2399. hd->soft_resets++;
  2400. break;
  2401. case MPI_EVENT_LOGOUT: /* 09 */
  2402. /* FIXME! */
  2403. break;
  2404. case MPI_EVENT_RESCAN: /* 06 */
  2405. break;
  2406. /*
  2407. * CHECKME! Don't think we need to do
  2408. * anything for these, but...
  2409. */
  2410. case MPI_EVENT_LINK_STATUS_CHANGE: /* 07 */
  2411. case MPI_EVENT_LOOP_STATE_CHANGE: /* 08 */
  2412. /*
  2413. * CHECKME! Falling thru...
  2414. */
  2415. break;
  2416. case MPI_EVENT_INTEGRATED_RAID: /* 0B */
  2417. break;
  2418. case MPI_EVENT_NONE: /* 00 */
  2419. case MPI_EVENT_LOG_DATA: /* 01 */
  2420. case MPI_EVENT_STATE_CHANGE: /* 02 */
  2421. case MPI_EVENT_EVENT_CHANGE: /* 0A */
  2422. default:
  2423. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT ": Ignoring event (=%02Xh)\n",
  2424. ioc->name, event));
  2425. break;
  2426. }
  2427. return 1; /* currently means nothing really */
  2428. }
  2429. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2430. /*
  2431. * Bus Scan and Domain Validation functionality ...
  2432. */
  2433. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2434. /*
  2435. * mptscsih_scandv_complete - Scan and DV callback routine registered
  2436. * to Fustion MPT (base) driver.
  2437. *
  2438. * @ioc: Pointer to MPT_ADAPTER structure
  2439. * @mf: Pointer to original MPT request frame
  2440. * @mr: Pointer to MPT reply frame (NULL if TurboReply)
  2441. *
  2442. * This routine is called from mpt.c::mpt_interrupt() at the completion
  2443. * of any SCSI IO request.
  2444. * This routine is registered with the Fusion MPT (base) driver at driver
  2445. * load/init time via the mpt_register() API call.
  2446. *
  2447. * Returns 1 indicating alloc'd request frame ptr should be freed.
  2448. *
  2449. * Remark: Sets a completion code and (possibly) saves sense data
  2450. * in the IOC member localReply structure.
  2451. * Used ONLY for DV and other internal commands.
  2452. */
  2453. int
  2454. mptscsih_scandv_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
  2455. {
  2456. MPT_SCSI_HOST *hd;
  2457. SCSIIORequest_t *pReq;
  2458. int completionCode;
  2459. u16 req_idx;
  2460. hd = shost_priv(ioc->sh);
  2461. if ((mf == NULL) ||
  2462. (mf >= MPT_INDEX_2_MFPTR(ioc, ioc->req_depth))) {
  2463. printk(MYIOC_s_ERR_FMT
  2464. "ScanDvComplete, %s req frame ptr! (=%p)\n",
  2465. ioc->name, mf?"BAD":"NULL", (void *) mf);
  2466. goto wakeup;
  2467. }
  2468. del_timer(&hd->timer);
  2469. req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  2470. mptscsih_set_scsi_lookup(ioc, req_idx, NULL);
  2471. pReq = (SCSIIORequest_t *) mf;
  2472. if (mf != hd->cmdPtr) {
  2473. printk(MYIOC_s_WARN_FMT "ScanDvComplete (mf=%p, cmdPtr=%p, idx=%d)\n",
  2474. ioc->name, (void *)mf, (void *) hd->cmdPtr, req_idx);
  2475. }
  2476. hd->cmdPtr = NULL;
  2477. ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT "ScanDvComplete (mf=%p,mr=%p,idx=%d)\n",
  2478. ioc->name, mf, mr, req_idx));
  2479. hd->pLocal = &hd->localReply;
  2480. hd->pLocal->scsiStatus = 0;
  2481. /* If target struct exists, clear sense valid flag.
  2482. */
  2483. if (mr == NULL) {
  2484. completionCode = MPT_SCANDV_GOOD;
  2485. } else {
  2486. SCSIIOReply_t *pReply;
  2487. u16 status;
  2488. u8 scsi_status;
  2489. pReply = (SCSIIOReply_t *) mr;
  2490. status = le16_to_cpu(pReply->IOCStatus) & MPI_IOCSTATUS_MASK;
  2491. scsi_status = pReply->SCSIStatus;
  2492. switch(status) {
  2493. case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE: /* 0x0043 */
  2494. completionCode = MPT_SCANDV_SELECTION_TIMEOUT;
  2495. break;
  2496. case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR: /* 0x0046 */
  2497. case MPI_IOCSTATUS_SCSI_TASK_TERMINATED: /* 0x0048 */
  2498. case MPI_IOCSTATUS_SCSI_IOC_TERMINATED: /* 0x004B */
  2499. case MPI_IOCSTATUS_SCSI_EXT_TERMINATED: /* 0x004C */
  2500. completionCode = MPT_SCANDV_DID_RESET;
  2501. break;
  2502. case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN: /* 0x0045 */
  2503. case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR: /* 0x0040 */
  2504. case MPI_IOCSTATUS_SUCCESS: /* 0x0000 */
  2505. if (pReply->Function == MPI_FUNCTION_CONFIG) {
  2506. ConfigReply_t *pr = (ConfigReply_t *)mr;
  2507. completionCode = MPT_SCANDV_GOOD;
  2508. hd->pLocal->header.PageVersion = pr->Header.PageVersion;
  2509. hd->pLocal->header.PageLength = pr->Header.PageLength;
  2510. hd->pLocal->header.PageNumber = pr->Header.PageNumber;
  2511. hd->pLocal->header.PageType = pr->Header.PageType;
  2512. } else if (pReply->Function == MPI_FUNCTION_RAID_ACTION) {
  2513. /* If the RAID Volume request is successful,
  2514. * return GOOD, else indicate that
  2515. * some type of error occurred.
  2516. */
  2517. MpiRaidActionReply_t *pr = (MpiRaidActionReply_t *)mr;
  2518. if (le16_to_cpu(pr->ActionStatus) == MPI_RAID_ACTION_ASTATUS_SUCCESS)
  2519. completionCode = MPT_SCANDV_GOOD;
  2520. else
  2521. completionCode = MPT_SCANDV_SOME_ERROR;
  2522. memcpy(hd->pLocal->sense, pr, sizeof(hd->pLocal->sense));
  2523. } else if (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_VALID) {
  2524. u8 *sense_data;
  2525. int sz;
  2526. /* save sense data in global structure
  2527. */
  2528. completionCode = MPT_SCANDV_SENSE;
  2529. hd->pLocal->scsiStatus = scsi_status;
  2530. sense_data = ((u8 *)ioc->sense_buf_pool +
  2531. (req_idx * MPT_SENSE_BUFFER_ALLOC));
  2532. sz = min_t(int, pReq->SenseBufferLength,
  2533. SCSI_STD_SENSE_BYTES);
  2534. memcpy(hd->pLocal->sense, sense_data, sz);
  2535. ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT " Check Condition, sense ptr %p\n",
  2536. ioc->name, sense_data));
  2537. } else if (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_FAILED) {
  2538. if (pReq->CDB[0] == INQUIRY)
  2539. completionCode = MPT_SCANDV_ISSUE_SENSE;
  2540. else
  2541. completionCode = MPT_SCANDV_DID_RESET;
  2542. }
  2543. else if (pReply->SCSIState & MPI_SCSI_STATE_NO_SCSI_STATUS)
  2544. completionCode = MPT_SCANDV_DID_RESET;
  2545. else if (pReply->SCSIState & MPI_SCSI_STATE_TERMINATED)
  2546. completionCode = MPT_SCANDV_DID_RESET;
  2547. else {
  2548. completionCode = MPT_SCANDV_GOOD;
  2549. hd->pLocal->scsiStatus = scsi_status;
  2550. }
  2551. break;
  2552. case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR: /* 0x0047 */
  2553. if (pReply->SCSIState & MPI_SCSI_STATE_TERMINATED)
  2554. completionCode = MPT_SCANDV_DID_RESET;
  2555. else
  2556. completionCode = MPT_SCANDV_SOME_ERROR;
  2557. break;
  2558. default:
  2559. completionCode = MPT_SCANDV_SOME_ERROR;
  2560. break;
  2561. } /* switch(status) */
  2562. } /* end of address reply case */
  2563. hd->pLocal->completion = completionCode;
  2564. /* MF and RF are freed in mpt_interrupt
  2565. */
  2566. wakeup:
  2567. /* Free Chain buffers (will never chain) in scan or dv */
  2568. //mptscsih_freeChainBuffers(ioc, req_idx);
  2569. /*
  2570. * Wake up the original calling thread
  2571. */
  2572. hd->scandv_wait_done = 1;
  2573. wake_up(&hd->scandv_waitq);
  2574. return 1;
  2575. }
  2576. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2577. /* mptscsih_timer_expired - Call back for timer process.
  2578. * Used only for dv functionality.
  2579. * @data: Pointer to MPT_SCSI_HOST recast as an unsigned long
  2580. *
  2581. */
  2582. void
  2583. mptscsih_timer_expired(unsigned long data)
  2584. {
  2585. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *) data;
  2586. MPT_ADAPTER *ioc = hd->ioc;
  2587. ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Timer Expired! Cmd %p\n", ioc->name, hd->cmdPtr));
  2588. if (hd->cmdPtr) {
  2589. MPIHeader_t *cmd = (MPIHeader_t *)hd->cmdPtr;
  2590. if (cmd->Function == MPI_FUNCTION_SCSI_IO_REQUEST) {
  2591. /* Desire to issue a task management request here.
  2592. * TM requests MUST be single threaded.
  2593. * If old eh code and no TM current, issue request.
  2594. * If new eh code, do nothing. Wait for OS cmd timeout
  2595. * for bus reset.
  2596. */
  2597. } else {
  2598. /* Perform a FW reload */
  2599. if (mpt_HardResetHandler(ioc, NO_SLEEP) < 0) {
  2600. printk(MYIOC_s_WARN_FMT "Firmware Reload FAILED!\n", ioc->name);
  2601. }
  2602. }
  2603. } else {
  2604. /* This should NEVER happen */
  2605. printk(MYIOC_s_WARN_FMT "Null cmdPtr!!!!\n", ioc->name);
  2606. }
  2607. /* No more processing.
  2608. * TM call will generate an interrupt for SCSI TM Management.
  2609. * The FW will reply to all outstanding commands, callback will finish cleanup.
  2610. * Hard reset clean-up will free all resources.
  2611. */
  2612. ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Timer Expired Complete!\n", ioc->name));
  2613. return;
  2614. }
  2615. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2616. /**
  2617. * mptscsih_do_cmd - Do internal command.
  2618. * @hd: MPT_SCSI_HOST pointer
  2619. * @io: INTERNAL_CMD pointer.
  2620. *
  2621. * Issue the specified internally generated command and do command
  2622. * specific cleanup. For bus scan / DV only.
  2623. * NOTES: If command is Inquiry and status is good,
  2624. * initialize a target structure, save the data
  2625. *
  2626. * Remark: Single threaded access only.
  2627. *
  2628. * Return:
  2629. * < 0 if an illegal command or no resources
  2630. *
  2631. * 0 if good
  2632. *
  2633. * > 0 if command complete but some type of completion error.
  2634. */
  2635. static int
  2636. mptscsih_do_cmd(MPT_SCSI_HOST *hd, INTERNAL_CMD *io)
  2637. {
  2638. MPT_FRAME_HDR *mf;
  2639. SCSIIORequest_t *pScsiReq;
  2640. SCSIIORequest_t ReqCopy;
  2641. int my_idx, ii, dir;
  2642. int rc, cmdTimeout;
  2643. int in_isr;
  2644. char cmdLen;
  2645. char CDB[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  2646. char cmd = io->cmd;
  2647. MPT_ADAPTER *ioc = hd->ioc;
  2648. in_isr = in_interrupt();
  2649. if (in_isr) {
  2650. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Internal SCSI IO request not allowed in ISR context!\n",
  2651. ioc->name));
  2652. return -EPERM;
  2653. }
  2654. /* Set command specific information
  2655. */
  2656. switch (cmd) {
  2657. case INQUIRY:
  2658. cmdLen = 6;
  2659. dir = MPI_SCSIIO_CONTROL_READ;
  2660. CDB[0] = cmd;
  2661. CDB[4] = io->size;
  2662. cmdTimeout = 10;
  2663. break;
  2664. case TEST_UNIT_READY:
  2665. cmdLen = 6;
  2666. dir = MPI_SCSIIO_CONTROL_READ;
  2667. cmdTimeout = 10;
  2668. break;
  2669. case START_STOP:
  2670. cmdLen = 6;
  2671. dir = MPI_SCSIIO_CONTROL_READ;
  2672. CDB[0] = cmd;
  2673. CDB[4] = 1; /*Spin up the disk */
  2674. cmdTimeout = 15;
  2675. break;
  2676. case REQUEST_SENSE:
  2677. cmdLen = 6;
  2678. CDB[0] = cmd;
  2679. CDB[4] = io->size;
  2680. dir = MPI_SCSIIO_CONTROL_READ;
  2681. cmdTimeout = 10;
  2682. break;
  2683. case READ_BUFFER:
  2684. cmdLen = 10;
  2685. dir = MPI_SCSIIO_CONTROL_READ;
  2686. CDB[0] = cmd;
  2687. if (io->flags & MPT_ICFLAG_ECHO) {
  2688. CDB[1] = 0x0A;
  2689. } else {
  2690. CDB[1] = 0x02;
  2691. }
  2692. if (io->flags & MPT_ICFLAG_BUF_CAP) {
  2693. CDB[1] |= 0x01;
  2694. }
  2695. CDB[6] = (io->size >> 16) & 0xFF;
  2696. CDB[7] = (io->size >> 8) & 0xFF;
  2697. CDB[8] = io->size & 0xFF;
  2698. cmdTimeout = 10;
  2699. break;
  2700. case WRITE_BUFFER:
  2701. cmdLen = 10;
  2702. dir = MPI_SCSIIO_CONTROL_WRITE;
  2703. CDB[0] = cmd;
  2704. if (io->flags & MPT_ICFLAG_ECHO) {
  2705. CDB[1] = 0x0A;
  2706. } else {
  2707. CDB[1] = 0x02;
  2708. }
  2709. CDB[6] = (io->size >> 16) & 0xFF;
  2710. CDB[7] = (io->size >> 8) & 0xFF;
  2711. CDB[8] = io->size & 0xFF;
  2712. cmdTimeout = 10;
  2713. break;
  2714. case RESERVE:
  2715. cmdLen = 6;
  2716. dir = MPI_SCSIIO_CONTROL_READ;
  2717. CDB[0] = cmd;
  2718. cmdTimeout = 10;
  2719. break;
  2720. case RELEASE:
  2721. cmdLen = 6;
  2722. dir = MPI_SCSIIO_CONTROL_READ;
  2723. CDB[0] = cmd;
  2724. cmdTimeout = 10;
  2725. break;
  2726. case SYNCHRONIZE_CACHE:
  2727. cmdLen = 10;
  2728. dir = MPI_SCSIIO_CONTROL_READ;
  2729. CDB[0] = cmd;
  2730. // CDB[1] = 0x02; /* set immediate bit */
  2731. cmdTimeout = 10;
  2732. break;
  2733. default:
  2734. /* Error Case */
  2735. return -EFAULT;
  2736. }
  2737. /* Get and Populate a free Frame
  2738. */
  2739. if ((mf = mpt_get_msg_frame(ioc->InternalCtx, ioc)) == NULL) {
  2740. dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "No msg frames!\n",
  2741. ioc->name));
  2742. return -EBUSY;
  2743. }
  2744. pScsiReq = (SCSIIORequest_t *) mf;
  2745. /* Get the request index */
  2746. my_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  2747. ADD_INDEX_LOG(my_idx); /* for debug */
  2748. if (io->flags & MPT_ICFLAG_PHYS_DISK) {
  2749. pScsiReq->TargetID = io->physDiskNum;
  2750. pScsiReq->Bus = 0;
  2751. pScsiReq->ChainOffset = 0;
  2752. pScsiReq->Function = MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
  2753. } else {
  2754. pScsiReq->TargetID = io->id;
  2755. pScsiReq->Bus = io->channel;
  2756. pScsiReq->ChainOffset = 0;
  2757. pScsiReq->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
  2758. }
  2759. pScsiReq->CDBLength = cmdLen;
  2760. pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
  2761. pScsiReq->Reserved = 0;
  2762. pScsiReq->MsgFlags = mpt_msg_flags();
  2763. /* MsgContext set in mpt_get_msg_fram call */
  2764. int_to_scsilun(io->lun, (struct scsi_lun *)pScsiReq->LUN);
  2765. if (io->flags & MPT_ICFLAG_TAGGED_CMD)
  2766. pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_SIMPLEQ);
  2767. else
  2768. pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_UNTAGGED);
  2769. if (cmd == REQUEST_SENSE) {
  2770. pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_UNTAGGED);
  2771. ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Untagged! 0x%2x\n",
  2772. ioc->name, cmd));
  2773. }
  2774. for (ii=0; ii < 16; ii++)
  2775. pScsiReq->CDB[ii] = CDB[ii];
  2776. pScsiReq->DataLength = cpu_to_le32(io->size);
  2777. pScsiReq->SenseBufferLowAddr = cpu_to_le32(ioc->sense_buf_low_dma
  2778. + (my_idx * MPT_SENSE_BUFFER_ALLOC));
  2779. ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Sending Command 0x%x for (%d:%d:%d)\n",
  2780. ioc->name, cmd, io->channel, io->id, io->lun));
  2781. if (dir == MPI_SCSIIO_CONTROL_READ) {
  2782. mpt_add_sge((char *) &pScsiReq->SGL,
  2783. MPT_SGE_FLAGS_SSIMPLE_READ | io->size,
  2784. io->data_dma);
  2785. } else {
  2786. mpt_add_sge((char *) &pScsiReq->SGL,
  2787. MPT_SGE_FLAGS_SSIMPLE_WRITE | io->size,
  2788. io->data_dma);
  2789. }
  2790. /* The ISR will free the request frame, but we need
  2791. * the information to initialize the target. Duplicate.
  2792. */
  2793. memcpy(&ReqCopy, pScsiReq, sizeof(SCSIIORequest_t));
  2794. /* Issue this command after:
  2795. * finish init
  2796. * add timer
  2797. * Wait until the reply has been received
  2798. * ScsiScanDvCtx callback function will
  2799. * set hd->pLocal;
  2800. * set scandv_wait_done and call wake_up
  2801. */
  2802. hd->pLocal = NULL;
  2803. hd->timer.expires = jiffies + HZ*cmdTimeout;
  2804. hd->scandv_wait_done = 0;
  2805. /* Save cmd pointer, for resource free if timeout or
  2806. * FW reload occurs
  2807. */
  2808. hd->cmdPtr = mf;
  2809. add_timer(&hd->timer);
  2810. mpt_put_msg_frame(ioc->InternalCtx, ioc, mf);
  2811. wait_event(hd->scandv_waitq, hd->scandv_wait_done);
  2812. if (hd->pLocal) {
  2813. rc = hd->pLocal->completion;
  2814. hd->pLocal->skip = 0;
  2815. /* Always set fatal error codes in some cases.
  2816. */
  2817. if (rc == MPT_SCANDV_SELECTION_TIMEOUT)
  2818. rc = -ENXIO;
  2819. else if (rc == MPT_SCANDV_SOME_ERROR)
  2820. rc = -rc;
  2821. } else {
  2822. rc = -EFAULT;
  2823. /* This should never happen. */
  2824. ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT "_do_cmd: Null pLocal!!!\n",
  2825. ioc->name));
  2826. }
  2827. return rc;
  2828. }
  2829. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2830. /**
  2831. * mptscsih_synchronize_cache - Send SYNCHRONIZE_CACHE to all disks.
  2832. * @hd: Pointer to a SCSI HOST structure
  2833. * @vdevice: virtual target device
  2834. *
  2835. * Uses the ISR, but with special processing.
  2836. * MUST be single-threaded.
  2837. *
  2838. */
  2839. static void
  2840. mptscsih_synchronize_cache(MPT_SCSI_HOST *hd, VirtDevice *vdevice)
  2841. {
  2842. INTERNAL_CMD iocmd;
  2843. /* Ignore hidden raid components, this is handled when the command
  2844. * is sent to the volume
  2845. */
  2846. if (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT)
  2847. return;
  2848. if (vdevice->vtarget->type != TYPE_DISK || vdevice->vtarget->deleted ||
  2849. !vdevice->configured_lun)
  2850. return;
  2851. /* Following parameters will not change
  2852. * in this routine.
  2853. */
  2854. iocmd.cmd = SYNCHRONIZE_CACHE;
  2855. iocmd.flags = 0;
  2856. iocmd.physDiskNum = -1;
  2857. iocmd.data = NULL;
  2858. iocmd.data_dma = -1;
  2859. iocmd.size = 0;
  2860. iocmd.rsvd = iocmd.rsvd2 = 0;
  2861. iocmd.channel = vdevice->vtarget->channel;
  2862. iocmd.id = vdevice->vtarget->id;
  2863. iocmd.lun = vdevice->lun;
  2864. mptscsih_do_cmd(hd, &iocmd);
  2865. }
  2866. static ssize_t
  2867. mptscsih_version_fw_show(struct class_device *cdev, char *buf)
  2868. {
  2869. struct Scsi_Host *host = class_to_shost(cdev);
  2870. MPT_SCSI_HOST *hd = shost_priv(host);
  2871. MPT_ADAPTER *ioc = hd->ioc;
  2872. return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
  2873. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  2874. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  2875. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  2876. ioc->facts.FWVersion.Word & 0x000000FF);
  2877. }
  2878. static CLASS_DEVICE_ATTR(version_fw, S_IRUGO, mptscsih_version_fw_show, NULL);
  2879. static ssize_t
  2880. mptscsih_version_bios_show(struct class_device *cdev, char *buf)
  2881. {
  2882. struct Scsi_Host *host = class_to_shost(cdev);
  2883. MPT_SCSI_HOST *hd = shost_priv(host);
  2884. MPT_ADAPTER *ioc = hd->ioc;
  2885. return snprintf(buf, PAGE_SIZE, "%02x.%02x.%02x.%02x\n",
  2886. (ioc->biosVersion & 0xFF000000) >> 24,
  2887. (ioc->biosVersion & 0x00FF0000) >> 16,
  2888. (ioc->biosVersion & 0x0000FF00) >> 8,
  2889. ioc->biosVersion & 0x000000FF);
  2890. }
  2891. static CLASS_DEVICE_ATTR(version_bios, S_IRUGO, mptscsih_version_bios_show, NULL);
  2892. static ssize_t
  2893. mptscsih_version_mpi_show(struct class_device *cdev, char *buf)
  2894. {
  2895. struct Scsi_Host *host = class_to_shost(cdev);
  2896. MPT_SCSI_HOST *hd = shost_priv(host);
  2897. MPT_ADAPTER *ioc = hd->ioc;
  2898. return snprintf(buf, PAGE_SIZE, "%03x\n", ioc->facts.MsgVersion);
  2899. }
  2900. static CLASS_DEVICE_ATTR(version_mpi, S_IRUGO, mptscsih_version_mpi_show, NULL);
  2901. static ssize_t
  2902. mptscsih_version_product_show(struct class_device *cdev, char *buf)
  2903. {
  2904. struct Scsi_Host *host = class_to_shost(cdev);
  2905. MPT_SCSI_HOST *hd = shost_priv(host);
  2906. MPT_ADAPTER *ioc = hd->ioc;
  2907. return snprintf(buf, PAGE_SIZE, "%s\n", ioc->prod_name);
  2908. }
  2909. static CLASS_DEVICE_ATTR(version_product, S_IRUGO,
  2910. mptscsih_version_product_show, NULL);
  2911. static ssize_t
  2912. mptscsih_version_nvdata_persistent_show(struct class_device *cdev, char *buf)
  2913. {
  2914. struct Scsi_Host *host = class_to_shost(cdev);
  2915. MPT_SCSI_HOST *hd = shost_priv(host);
  2916. MPT_ADAPTER *ioc = hd->ioc;
  2917. return snprintf(buf, PAGE_SIZE, "%02xh\n",
  2918. ioc->nvdata_version_persistent);
  2919. }
  2920. static CLASS_DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
  2921. mptscsih_version_nvdata_persistent_show, NULL);
  2922. static ssize_t
  2923. mptscsih_version_nvdata_default_show(struct class_device *cdev, char *buf)
  2924. {
  2925. struct Scsi_Host *host = class_to_shost(cdev);
  2926. MPT_SCSI_HOST *hd = shost_priv(host);
  2927. MPT_ADAPTER *ioc = hd->ioc;
  2928. return snprintf(buf, PAGE_SIZE, "%02xh\n",ioc->nvdata_version_default);
  2929. }
  2930. static CLASS_DEVICE_ATTR(version_nvdata_default, S_IRUGO,
  2931. mptscsih_version_nvdata_default_show, NULL);
  2932. static ssize_t
  2933. mptscsih_board_name_show(struct class_device *cdev, char *buf)
  2934. {
  2935. struct Scsi_Host *host = class_to_shost(cdev);
  2936. MPT_SCSI_HOST *hd = shost_priv(host);
  2937. MPT_ADAPTER *ioc = hd->ioc;
  2938. return snprintf(buf, PAGE_SIZE, "%s\n", ioc->board_name);
  2939. }
  2940. static CLASS_DEVICE_ATTR(board_name, S_IRUGO, mptscsih_board_name_show, NULL);
  2941. static ssize_t
  2942. mptscsih_board_assembly_show(struct class_device *cdev, char *buf)
  2943. {
  2944. struct Scsi_Host *host = class_to_shost(cdev);
  2945. MPT_SCSI_HOST *hd = shost_priv(host);
  2946. MPT_ADAPTER *ioc = hd->ioc;
  2947. return snprintf(buf, PAGE_SIZE, "%s\n", ioc->board_assembly);
  2948. }
  2949. static CLASS_DEVICE_ATTR(board_assembly, S_IRUGO,
  2950. mptscsih_board_assembly_show, NULL);
  2951. static ssize_t
  2952. mptscsih_board_tracer_show(struct class_device *cdev, char *buf)
  2953. {
  2954. struct Scsi_Host *host = class_to_shost(cdev);
  2955. MPT_SCSI_HOST *hd = shost_priv(host);
  2956. MPT_ADAPTER *ioc = hd->ioc;
  2957. return snprintf(buf, PAGE_SIZE, "%s\n", ioc->board_tracer);
  2958. }
  2959. static CLASS_DEVICE_ATTR(board_tracer, S_IRUGO,
  2960. mptscsih_board_tracer_show, NULL);
  2961. static ssize_t
  2962. mptscsih_io_delay_show(struct class_device *cdev, char *buf)
  2963. {
  2964. struct Scsi_Host *host = class_to_shost(cdev);
  2965. MPT_SCSI_HOST *hd = shost_priv(host);
  2966. MPT_ADAPTER *ioc = hd->ioc;
  2967. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
  2968. }
  2969. static CLASS_DEVICE_ATTR(io_delay, S_IRUGO,
  2970. mptscsih_io_delay_show, NULL);
  2971. static ssize_t
  2972. mptscsih_device_delay_show(struct class_device *cdev, char *buf)
  2973. {
  2974. struct Scsi_Host *host = class_to_shost(cdev);
  2975. MPT_SCSI_HOST *hd = shost_priv(host);
  2976. MPT_ADAPTER *ioc = hd->ioc;
  2977. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
  2978. }
  2979. static CLASS_DEVICE_ATTR(device_delay, S_IRUGO,
  2980. mptscsih_device_delay_show, NULL);
  2981. static ssize_t
  2982. mptscsih_debug_level_show(struct class_device *cdev, char *buf)
  2983. {
  2984. struct Scsi_Host *host = class_to_shost(cdev);
  2985. MPT_SCSI_HOST *hd = shost_priv(host);
  2986. MPT_ADAPTER *ioc = hd->ioc;
  2987. return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->debug_level);
  2988. }
  2989. static ssize_t
  2990. mptscsih_debug_level_store(struct class_device *cdev, const char *buf,
  2991. size_t count)
  2992. {
  2993. struct Scsi_Host *host = class_to_shost(cdev);
  2994. MPT_SCSI_HOST *hd = shost_priv(host);
  2995. MPT_ADAPTER *ioc = hd->ioc;
  2996. int val = 0;
  2997. if (sscanf(buf, "%x", &val) != 1)
  2998. return -EINVAL;
  2999. ioc->debug_level = val;
  3000. printk(MYIOC_s_INFO_FMT "debug_level=%08xh\n",
  3001. ioc->name, ioc->debug_level);
  3002. return strlen(buf);
  3003. }
  3004. static CLASS_DEVICE_ATTR(debug_level, S_IRUGO | S_IWUSR,
  3005. mptscsih_debug_level_show, mptscsih_debug_level_store);
  3006. struct class_device_attribute *mptscsih_host_attrs[] = {
  3007. &class_device_attr_version_fw,
  3008. &class_device_attr_version_bios,
  3009. &class_device_attr_version_mpi,
  3010. &class_device_attr_version_product,
  3011. &class_device_attr_version_nvdata_persistent,
  3012. &class_device_attr_version_nvdata_default,
  3013. &class_device_attr_board_name,
  3014. &class_device_attr_board_assembly,
  3015. &class_device_attr_board_tracer,
  3016. &class_device_attr_io_delay,
  3017. &class_device_attr_device_delay,
  3018. &class_device_attr_debug_level,
  3019. NULL,
  3020. };
  3021. EXPORT_SYMBOL(mptscsih_host_attrs);
  3022. EXPORT_SYMBOL(mptscsih_remove);
  3023. EXPORT_SYMBOL(mptscsih_shutdown);
  3024. #ifdef CONFIG_PM
  3025. EXPORT_SYMBOL(mptscsih_suspend);
  3026. EXPORT_SYMBOL(mptscsih_resume);
  3027. #endif
  3028. EXPORT_SYMBOL(mptscsih_proc_info);
  3029. EXPORT_SYMBOL(mptscsih_info);
  3030. EXPORT_SYMBOL(mptscsih_qcmd);
  3031. EXPORT_SYMBOL(mptscsih_slave_destroy);
  3032. EXPORT_SYMBOL(mptscsih_slave_configure);
  3033. EXPORT_SYMBOL(mptscsih_abort);
  3034. EXPORT_SYMBOL(mptscsih_dev_reset);
  3035. EXPORT_SYMBOL(mptscsih_bus_reset);
  3036. EXPORT_SYMBOL(mptscsih_host_reset);
  3037. EXPORT_SYMBOL(mptscsih_bios_param);
  3038. EXPORT_SYMBOL(mptscsih_io_done);
  3039. EXPORT_SYMBOL(mptscsih_taskmgmt_complete);
  3040. EXPORT_SYMBOL(mptscsih_scandv_complete);
  3041. EXPORT_SYMBOL(mptscsih_event_process);
  3042. EXPORT_SYMBOL(mptscsih_ioc_reset);
  3043. EXPORT_SYMBOL(mptscsih_change_queue_depth);
  3044. EXPORT_SYMBOL(mptscsih_timer_expired);
  3045. EXPORT_SYMBOL(mptscsih_TMHandler);
  3046. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/