mptscsih.c 93 KB

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