mptscsih.c 96 KB

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