lpfc_sli.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2007 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_cmnd.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_host.h>
  29. #include <scsi/scsi_transport_fc.h>
  30. #include "lpfc_hw.h"
  31. #include "lpfc_sli.h"
  32. #include "lpfc_disc.h"
  33. #include "lpfc_scsi.h"
  34. #include "lpfc.h"
  35. #include "lpfc_crtn.h"
  36. #include "lpfc_logmsg.h"
  37. #include "lpfc_compat.h"
  38. #include "lpfc_debugfs.h"
  39. /*
  40. * Define macro to log: Mailbox command x%x cannot issue Data
  41. * This allows multiple uses of lpfc_msgBlk0311
  42. * w/o perturbing log msg utility.
  43. */
  44. #define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
  45. lpfc_printf_log(phba, \
  46. KERN_INFO, \
  47. LOG_MBOX | LOG_SLI, \
  48. "(%d):0311 Mailbox command x%x cannot " \
  49. "issue Data: x%x x%x x%x\n", \
  50. pmbox->vport ? pmbox->vport->vpi : 0, \
  51. pmbox->mb.mbxCommand, \
  52. phba->pport->port_state, \
  53. psli->sli_flag, \
  54. flag)
  55. /* There are only four IOCB completion types. */
  56. typedef enum _lpfc_iocb_type {
  57. LPFC_UNKNOWN_IOCB,
  58. LPFC_UNSOL_IOCB,
  59. LPFC_SOL_IOCB,
  60. LPFC_ABORT_IOCB
  61. } lpfc_iocb_type;
  62. /* SLI-2/SLI-3 provide different sized iocbs. Given a pointer
  63. * to the start of the ring, and the slot number of the
  64. * desired iocb entry, calc a pointer to that entry.
  65. */
  66. static inline IOCB_t *
  67. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  68. {
  69. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  70. pring->cmdidx * phba->iocb_cmd_size);
  71. }
  72. static inline IOCB_t *
  73. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  74. {
  75. return (IOCB_t *) (((char *) pring->rspringaddr) +
  76. pring->rspidx * phba->iocb_rsp_size);
  77. }
  78. static struct lpfc_iocbq *
  79. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  80. {
  81. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  82. struct lpfc_iocbq * iocbq = NULL;
  83. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  84. return iocbq;
  85. }
  86. struct lpfc_iocbq *
  87. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  88. {
  89. struct lpfc_iocbq * iocbq = NULL;
  90. unsigned long iflags;
  91. spin_lock_irqsave(&phba->hbalock, iflags);
  92. iocbq = __lpfc_sli_get_iocbq(phba);
  93. spin_unlock_irqrestore(&phba->hbalock, iflags);
  94. return iocbq;
  95. }
  96. static void
  97. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  98. {
  99. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  100. /*
  101. * Clean all volatile data fields, preserve iotag and node struct.
  102. */
  103. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  104. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  105. }
  106. void
  107. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  108. {
  109. unsigned long iflags;
  110. /*
  111. * Clean all volatile data fields, preserve iotag and node struct.
  112. */
  113. spin_lock_irqsave(&phba->hbalock, iflags);
  114. __lpfc_sli_release_iocbq(phba, iocbq);
  115. spin_unlock_irqrestore(&phba->hbalock, iflags);
  116. }
  117. /*
  118. * Translate the iocb command to an iocb command type used to decide the final
  119. * disposition of each completed IOCB.
  120. */
  121. static lpfc_iocb_type
  122. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  123. {
  124. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  125. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  126. return 0;
  127. switch (iocb_cmnd) {
  128. case CMD_XMIT_SEQUENCE_CR:
  129. case CMD_XMIT_SEQUENCE_CX:
  130. case CMD_XMIT_BCAST_CN:
  131. case CMD_XMIT_BCAST_CX:
  132. case CMD_ELS_REQUEST_CR:
  133. case CMD_ELS_REQUEST_CX:
  134. case CMD_CREATE_XRI_CR:
  135. case CMD_CREATE_XRI_CX:
  136. case CMD_GET_RPI_CN:
  137. case CMD_XMIT_ELS_RSP_CX:
  138. case CMD_GET_RPI_CR:
  139. case CMD_FCP_IWRITE_CR:
  140. case CMD_FCP_IWRITE_CX:
  141. case CMD_FCP_IREAD_CR:
  142. case CMD_FCP_IREAD_CX:
  143. case CMD_FCP_ICMND_CR:
  144. case CMD_FCP_ICMND_CX:
  145. case CMD_FCP_TSEND_CX:
  146. case CMD_FCP_TRSP_CX:
  147. case CMD_FCP_TRECEIVE_CX:
  148. case CMD_FCP_AUTO_TRSP_CX:
  149. case CMD_ADAPTER_MSG:
  150. case CMD_ADAPTER_DUMP:
  151. case CMD_XMIT_SEQUENCE64_CR:
  152. case CMD_XMIT_SEQUENCE64_CX:
  153. case CMD_XMIT_BCAST64_CN:
  154. case CMD_XMIT_BCAST64_CX:
  155. case CMD_ELS_REQUEST64_CR:
  156. case CMD_ELS_REQUEST64_CX:
  157. case CMD_FCP_IWRITE64_CR:
  158. case CMD_FCP_IWRITE64_CX:
  159. case CMD_FCP_IREAD64_CR:
  160. case CMD_FCP_IREAD64_CX:
  161. case CMD_FCP_ICMND64_CR:
  162. case CMD_FCP_ICMND64_CX:
  163. case CMD_FCP_TSEND64_CX:
  164. case CMD_FCP_TRSP64_CX:
  165. case CMD_FCP_TRECEIVE64_CX:
  166. case CMD_GEN_REQUEST64_CR:
  167. case CMD_GEN_REQUEST64_CX:
  168. case CMD_XMIT_ELS_RSP64_CX:
  169. type = LPFC_SOL_IOCB;
  170. break;
  171. case CMD_ABORT_XRI_CN:
  172. case CMD_ABORT_XRI_CX:
  173. case CMD_CLOSE_XRI_CN:
  174. case CMD_CLOSE_XRI_CX:
  175. case CMD_XRI_ABORTED_CX:
  176. case CMD_ABORT_MXRI64_CN:
  177. type = LPFC_ABORT_IOCB;
  178. break;
  179. case CMD_RCV_SEQUENCE_CX:
  180. case CMD_RCV_ELS_REQ_CX:
  181. case CMD_RCV_SEQUENCE64_CX:
  182. case CMD_RCV_ELS_REQ64_CX:
  183. case CMD_ASYNC_STATUS:
  184. case CMD_IOCB_RCV_SEQ64_CX:
  185. case CMD_IOCB_RCV_ELS64_CX:
  186. case CMD_IOCB_RCV_CONT64_CX:
  187. type = LPFC_UNSOL_IOCB;
  188. break;
  189. default:
  190. type = LPFC_UNKNOWN_IOCB;
  191. break;
  192. }
  193. return type;
  194. }
  195. static int
  196. lpfc_sli_ring_map(struct lpfc_hba *phba)
  197. {
  198. struct lpfc_sli *psli = &phba->sli;
  199. LPFC_MBOXQ_t *pmb;
  200. MAILBOX_t *pmbox;
  201. int i, rc, ret = 0;
  202. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  203. if (!pmb)
  204. return -ENOMEM;
  205. pmbox = &pmb->mb;
  206. phba->link_state = LPFC_INIT_MBX_CMDS;
  207. for (i = 0; i < psli->num_rings; i++) {
  208. lpfc_config_ring(phba, i, pmb);
  209. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  210. if (rc != MBX_SUCCESS) {
  211. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  212. "0446 Adapter failed to init (%d), "
  213. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  214. "ring %d\n",
  215. rc, pmbox->mbxCommand,
  216. pmbox->mbxStatus, i);
  217. phba->link_state = LPFC_HBA_ERROR;
  218. ret = -ENXIO;
  219. break;
  220. }
  221. }
  222. mempool_free(pmb, phba->mbox_mem_pool);
  223. return ret;
  224. }
  225. static int
  226. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  227. struct lpfc_iocbq *piocb)
  228. {
  229. list_add_tail(&piocb->list, &pring->txcmplq);
  230. pring->txcmplq_cnt++;
  231. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  232. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  233. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  234. if (!piocb->vport)
  235. BUG();
  236. else
  237. mod_timer(&piocb->vport->els_tmofunc,
  238. jiffies + HZ * (phba->fc_ratov << 1));
  239. }
  240. return 0;
  241. }
  242. static struct lpfc_iocbq *
  243. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  244. {
  245. struct lpfc_iocbq *cmd_iocb;
  246. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  247. if (cmd_iocb != NULL)
  248. pring->txq_cnt--;
  249. return cmd_iocb;
  250. }
  251. static IOCB_t *
  252. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  253. {
  254. struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
  255. &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
  256. &phba->slim2p->mbx.us.s2.port[pring->ringno];
  257. uint32_t max_cmd_idx = pring->numCiocb;
  258. if ((pring->next_cmdidx == pring->cmdidx) &&
  259. (++pring->next_cmdidx >= max_cmd_idx))
  260. pring->next_cmdidx = 0;
  261. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  262. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  263. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  264. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  265. "0315 Ring %d issue: portCmdGet %d "
  266. "is bigger then cmd ring %d\n",
  267. pring->ringno,
  268. pring->local_getidx, max_cmd_idx);
  269. phba->link_state = LPFC_HBA_ERROR;
  270. /*
  271. * All error attention handlers are posted to
  272. * worker thread
  273. */
  274. phba->work_ha |= HA_ERATT;
  275. phba->work_hs = HS_FFER3;
  276. /* hbalock should already be held */
  277. if (phba->work_wait)
  278. lpfc_worker_wake_up(phba);
  279. return NULL;
  280. }
  281. if (pring->local_getidx == pring->next_cmdidx)
  282. return NULL;
  283. }
  284. return lpfc_cmd_iocb(phba, pring);
  285. }
  286. uint16_t
  287. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  288. {
  289. struct lpfc_iocbq **new_arr;
  290. struct lpfc_iocbq **old_arr;
  291. size_t new_len;
  292. struct lpfc_sli *psli = &phba->sli;
  293. uint16_t iotag;
  294. spin_lock_irq(&phba->hbalock);
  295. iotag = psli->last_iotag;
  296. if(++iotag < psli->iocbq_lookup_len) {
  297. psli->last_iotag = iotag;
  298. psli->iocbq_lookup[iotag] = iocbq;
  299. spin_unlock_irq(&phba->hbalock);
  300. iocbq->iotag = iotag;
  301. return iotag;
  302. } else if (psli->iocbq_lookup_len < (0xffff
  303. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  304. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  305. spin_unlock_irq(&phba->hbalock);
  306. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  307. GFP_KERNEL);
  308. if (new_arr) {
  309. spin_lock_irq(&phba->hbalock);
  310. old_arr = psli->iocbq_lookup;
  311. if (new_len <= psli->iocbq_lookup_len) {
  312. /* highly unprobable case */
  313. kfree(new_arr);
  314. iotag = psli->last_iotag;
  315. if(++iotag < psli->iocbq_lookup_len) {
  316. psli->last_iotag = iotag;
  317. psli->iocbq_lookup[iotag] = iocbq;
  318. spin_unlock_irq(&phba->hbalock);
  319. iocbq->iotag = iotag;
  320. return iotag;
  321. }
  322. spin_unlock_irq(&phba->hbalock);
  323. return 0;
  324. }
  325. if (psli->iocbq_lookup)
  326. memcpy(new_arr, old_arr,
  327. ((psli->last_iotag + 1) *
  328. sizeof (struct lpfc_iocbq *)));
  329. psli->iocbq_lookup = new_arr;
  330. psli->iocbq_lookup_len = new_len;
  331. psli->last_iotag = iotag;
  332. psli->iocbq_lookup[iotag] = iocbq;
  333. spin_unlock_irq(&phba->hbalock);
  334. iocbq->iotag = iotag;
  335. kfree(old_arr);
  336. return iotag;
  337. }
  338. } else
  339. spin_unlock_irq(&phba->hbalock);
  340. lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
  341. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  342. psli->last_iotag);
  343. return 0;
  344. }
  345. static void
  346. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  347. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  348. {
  349. /*
  350. * Set up an iotag
  351. */
  352. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  353. if (pring->ringno == LPFC_ELS_RING) {
  354. lpfc_debugfs_slow_ring_trc(phba,
  355. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  356. *(((uint32_t *) &nextiocb->iocb) + 4),
  357. *(((uint32_t *) &nextiocb->iocb) + 6),
  358. *(((uint32_t *) &nextiocb->iocb) + 7));
  359. }
  360. /*
  361. * Issue iocb command to adapter
  362. */
  363. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  364. wmb();
  365. pring->stats.iocb_cmd++;
  366. /*
  367. * If there is no completion routine to call, we can release the
  368. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  369. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  370. */
  371. if (nextiocb->iocb_cmpl)
  372. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  373. else
  374. __lpfc_sli_release_iocbq(phba, nextiocb);
  375. /*
  376. * Let the HBA know what IOCB slot will be the next one the
  377. * driver will put a command into.
  378. */
  379. pring->cmdidx = pring->next_cmdidx;
  380. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  381. }
  382. static void
  383. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  384. {
  385. int ringno = pring->ringno;
  386. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  387. wmb();
  388. /*
  389. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  390. * The HBA will tell us when an IOCB entry is available.
  391. */
  392. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  393. readl(phba->CAregaddr); /* flush */
  394. pring->stats.iocb_cmd_full++;
  395. }
  396. static void
  397. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  398. {
  399. int ringno = pring->ringno;
  400. /*
  401. * Tell the HBA that there is work to do in this ring.
  402. */
  403. wmb();
  404. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  405. readl(phba->CAregaddr); /* flush */
  406. }
  407. static void
  408. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  409. {
  410. IOCB_t *iocb;
  411. struct lpfc_iocbq *nextiocb;
  412. /*
  413. * Check to see if:
  414. * (a) there is anything on the txq to send
  415. * (b) link is up
  416. * (c) link attention events can be processed (fcp ring only)
  417. * (d) IOCB processing is not blocked by the outstanding mbox command.
  418. */
  419. if (pring->txq_cnt &&
  420. lpfc_is_link_up(phba) &&
  421. (pring->ringno != phba->sli.fcp_ring ||
  422. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  423. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  424. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  425. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  426. if (iocb)
  427. lpfc_sli_update_ring(phba, pring);
  428. else
  429. lpfc_sli_update_full_ring(phba, pring);
  430. }
  431. return;
  432. }
  433. static struct lpfc_hbq_entry *
  434. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  435. {
  436. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  437. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  438. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  439. hbqp->next_hbqPutIdx = 0;
  440. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  441. uint32_t raw_index = phba->hbq_get[hbqno];
  442. uint32_t getidx = le32_to_cpu(raw_index);
  443. hbqp->local_hbqGetIdx = getidx;
  444. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  445. lpfc_printf_log(phba, KERN_ERR,
  446. LOG_SLI | LOG_VPORT,
  447. "1802 HBQ %d: local_hbqGetIdx "
  448. "%u is > than hbqp->entry_count %u\n",
  449. hbqno, hbqp->local_hbqGetIdx,
  450. hbqp->entry_count);
  451. phba->link_state = LPFC_HBA_ERROR;
  452. return NULL;
  453. }
  454. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  455. return NULL;
  456. }
  457. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  458. hbqp->hbqPutIdx;
  459. }
  460. void
  461. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  462. {
  463. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  464. struct hbq_dmabuf *hbq_buf;
  465. int i, hbq_count;
  466. hbq_count = lpfc_sli_hbq_count();
  467. /* Return all memory used by all HBQs */
  468. for (i = 0; i < hbq_count; ++i) {
  469. list_for_each_entry_safe(dmabuf, next_dmabuf,
  470. &phba->hbqs[i].hbq_buffer_list, list) {
  471. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  472. list_del(&hbq_buf->dbuf.list);
  473. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  474. }
  475. phba->hbqs[i].buffer_count = 0;
  476. }
  477. }
  478. static struct lpfc_hbq_entry *
  479. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  480. struct hbq_dmabuf *hbq_buf)
  481. {
  482. struct lpfc_hbq_entry *hbqe;
  483. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  484. /* Get next HBQ entry slot to use */
  485. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  486. if (hbqe) {
  487. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  488. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  489. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  490. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  491. hbqe->bde.tus.f.bdeFlags = 0;
  492. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  493. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  494. /* Sync SLIM */
  495. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  496. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  497. /* flush */
  498. readl(phba->hbq_put + hbqno);
  499. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  500. }
  501. return hbqe;
  502. }
  503. static struct lpfc_hbq_init lpfc_els_hbq = {
  504. .rn = 1,
  505. .entry_count = 200,
  506. .mask_count = 0,
  507. .profile = 0,
  508. .ring_mask = (1 << LPFC_ELS_RING),
  509. .buffer_count = 0,
  510. .init_count = 20,
  511. .add_count = 5,
  512. };
  513. static struct lpfc_hbq_init lpfc_extra_hbq = {
  514. .rn = 1,
  515. .entry_count = 200,
  516. .mask_count = 0,
  517. .profile = 0,
  518. .ring_mask = (1 << LPFC_EXTRA_RING),
  519. .buffer_count = 0,
  520. .init_count = 0,
  521. .add_count = 5,
  522. };
  523. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  524. &lpfc_els_hbq,
  525. &lpfc_extra_hbq,
  526. };
  527. static int
  528. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  529. {
  530. uint32_t i, start, end;
  531. struct hbq_dmabuf *hbq_buffer;
  532. if (!phba->hbqs[hbqno].hbq_alloc_buffer) {
  533. return 0;
  534. }
  535. start = phba->hbqs[hbqno].buffer_count;
  536. end = count + start;
  537. if (end > lpfc_hbq_defs[hbqno]->entry_count) {
  538. end = lpfc_hbq_defs[hbqno]->entry_count;
  539. }
  540. /* Populate HBQ entries */
  541. for (i = start; i < end; i++) {
  542. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  543. if (!hbq_buffer)
  544. return 1;
  545. hbq_buffer->tag = (i | (hbqno << 16));
  546. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  547. phba->hbqs[hbqno].buffer_count++;
  548. else
  549. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  550. }
  551. return 0;
  552. }
  553. int
  554. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  555. {
  556. return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  557. lpfc_hbq_defs[qno]->add_count));
  558. }
  559. static int
  560. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  561. {
  562. return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  563. lpfc_hbq_defs[qno]->init_count));
  564. }
  565. static struct hbq_dmabuf *
  566. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  567. {
  568. struct lpfc_dmabuf *d_buf;
  569. struct hbq_dmabuf *hbq_buf;
  570. uint32_t hbqno;
  571. hbqno = tag >> 16;
  572. if (hbqno >= LPFC_MAX_HBQS)
  573. return NULL;
  574. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  575. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  576. if (hbq_buf->tag == tag) {
  577. return hbq_buf;
  578. }
  579. }
  580. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  581. "1803 Bad hbq tag. Data: x%x x%x\n",
  582. tag, phba->hbqs[tag >> 16].buffer_count);
  583. return NULL;
  584. }
  585. void
  586. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  587. {
  588. uint32_t hbqno;
  589. if (hbq_buffer) {
  590. hbqno = hbq_buffer->tag >> 16;
  591. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  592. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  593. }
  594. }
  595. }
  596. static int
  597. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  598. {
  599. uint8_t ret;
  600. switch (mbxCommand) {
  601. case MBX_LOAD_SM:
  602. case MBX_READ_NV:
  603. case MBX_WRITE_NV:
  604. case MBX_WRITE_VPARMS:
  605. case MBX_RUN_BIU_DIAG:
  606. case MBX_INIT_LINK:
  607. case MBX_DOWN_LINK:
  608. case MBX_CONFIG_LINK:
  609. case MBX_CONFIG_RING:
  610. case MBX_RESET_RING:
  611. case MBX_READ_CONFIG:
  612. case MBX_READ_RCONFIG:
  613. case MBX_READ_SPARM:
  614. case MBX_READ_STATUS:
  615. case MBX_READ_RPI:
  616. case MBX_READ_XRI:
  617. case MBX_READ_REV:
  618. case MBX_READ_LNK_STAT:
  619. case MBX_REG_LOGIN:
  620. case MBX_UNREG_LOGIN:
  621. case MBX_READ_LA:
  622. case MBX_CLEAR_LA:
  623. case MBX_DUMP_MEMORY:
  624. case MBX_DUMP_CONTEXT:
  625. case MBX_RUN_DIAGS:
  626. case MBX_RESTART:
  627. case MBX_UPDATE_CFG:
  628. case MBX_DOWN_LOAD:
  629. case MBX_DEL_LD_ENTRY:
  630. case MBX_RUN_PROGRAM:
  631. case MBX_SET_MASK:
  632. case MBX_SET_VARIABLE:
  633. case MBX_UNREG_D_ID:
  634. case MBX_KILL_BOARD:
  635. case MBX_CONFIG_FARP:
  636. case MBX_BEACON:
  637. case MBX_LOAD_AREA:
  638. case MBX_RUN_BIU_DIAG64:
  639. case MBX_CONFIG_PORT:
  640. case MBX_READ_SPARM64:
  641. case MBX_READ_RPI64:
  642. case MBX_REG_LOGIN64:
  643. case MBX_READ_LA64:
  644. case MBX_WRITE_WWN:
  645. case MBX_SET_DEBUG:
  646. case MBX_LOAD_EXP_ROM:
  647. case MBX_ASYNCEVT_ENABLE:
  648. case MBX_REG_VPI:
  649. case MBX_UNREG_VPI:
  650. case MBX_HEARTBEAT:
  651. ret = mbxCommand;
  652. break;
  653. default:
  654. ret = MBX_SHUTDOWN;
  655. break;
  656. }
  657. return ret;
  658. }
  659. static void
  660. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  661. {
  662. wait_queue_head_t *pdone_q;
  663. unsigned long drvr_flag;
  664. /*
  665. * If pdone_q is empty, the driver thread gave up waiting and
  666. * continued running.
  667. */
  668. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  669. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  670. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  671. if (pdone_q)
  672. wake_up_interruptible(pdone_q);
  673. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  674. return;
  675. }
  676. void
  677. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  678. {
  679. struct lpfc_dmabuf *mp;
  680. uint16_t rpi;
  681. int rc;
  682. mp = (struct lpfc_dmabuf *) (pmb->context1);
  683. if (mp) {
  684. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  685. kfree(mp);
  686. }
  687. /*
  688. * If a REG_LOGIN succeeded after node is destroyed or node
  689. * is in re-discovery driver need to cleanup the RPI.
  690. */
  691. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  692. pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
  693. !pmb->mb.mbxStatus) {
  694. rpi = pmb->mb.un.varWords[0];
  695. lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
  696. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  697. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  698. if (rc != MBX_NOT_FINISHED)
  699. return;
  700. }
  701. mempool_free(pmb, phba->mbox_mem_pool);
  702. return;
  703. }
  704. int
  705. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  706. {
  707. MAILBOX_t *pmbox;
  708. LPFC_MBOXQ_t *pmb;
  709. int rc;
  710. LIST_HEAD(cmplq);
  711. phba->sli.slistat.mbox_event++;
  712. /* Get all completed mailboxe buffers into the cmplq */
  713. spin_lock_irq(&phba->hbalock);
  714. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  715. spin_unlock_irq(&phba->hbalock);
  716. /* Get a Mailbox buffer to setup mailbox commands for callback */
  717. do {
  718. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  719. if (pmb == NULL)
  720. break;
  721. pmbox = &pmb->mb;
  722. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  723. if (pmb->vport) {
  724. lpfc_debugfs_disc_trc(pmb->vport,
  725. LPFC_DISC_TRC_MBOX_VPORT,
  726. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  727. (uint32_t)pmbox->mbxCommand,
  728. pmbox->un.varWords[0],
  729. pmbox->un.varWords[1]);
  730. }
  731. else {
  732. lpfc_debugfs_disc_trc(phba->pport,
  733. LPFC_DISC_TRC_MBOX,
  734. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  735. (uint32_t)pmbox->mbxCommand,
  736. pmbox->un.varWords[0],
  737. pmbox->un.varWords[1]);
  738. }
  739. }
  740. /*
  741. * It is a fatal error if unknown mbox command completion.
  742. */
  743. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  744. MBX_SHUTDOWN) {
  745. /* Unknow mailbox command compl */
  746. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  747. "(%d):0323 Unknown Mailbox command "
  748. "%x Cmpl\n",
  749. pmb->vport ? pmb->vport->vpi : 0,
  750. pmbox->mbxCommand);
  751. phba->link_state = LPFC_HBA_ERROR;
  752. phba->work_hs = HS_FFER3;
  753. lpfc_handle_eratt(phba);
  754. continue;
  755. }
  756. if (pmbox->mbxStatus) {
  757. phba->sli.slistat.mbox_stat_err++;
  758. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  759. /* Mbox cmd cmpl error - RETRYing */
  760. lpfc_printf_log(phba, KERN_INFO,
  761. LOG_MBOX | LOG_SLI,
  762. "(%d):0305 Mbox cmd cmpl "
  763. "error - RETRYing Data: x%x "
  764. "x%x x%x x%x\n",
  765. pmb->vport ? pmb->vport->vpi :0,
  766. pmbox->mbxCommand,
  767. pmbox->mbxStatus,
  768. pmbox->un.varWords[0],
  769. pmb->vport->port_state);
  770. pmbox->mbxStatus = 0;
  771. pmbox->mbxOwner = OWN_HOST;
  772. spin_lock_irq(&phba->hbalock);
  773. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  774. spin_unlock_irq(&phba->hbalock);
  775. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  776. if (rc == MBX_SUCCESS)
  777. continue;
  778. }
  779. }
  780. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  781. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  782. "(%d):0307 Mailbox cmd x%x Cmpl x%p "
  783. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  784. pmb->vport ? pmb->vport->vpi : 0,
  785. pmbox->mbxCommand,
  786. pmb->mbox_cmpl,
  787. *((uint32_t *) pmbox),
  788. pmbox->un.varWords[0],
  789. pmbox->un.varWords[1],
  790. pmbox->un.varWords[2],
  791. pmbox->un.varWords[3],
  792. pmbox->un.varWords[4],
  793. pmbox->un.varWords[5],
  794. pmbox->un.varWords[6],
  795. pmbox->un.varWords[7]);
  796. if (pmb->mbox_cmpl)
  797. pmb->mbox_cmpl(phba,pmb);
  798. } while (1);
  799. return 0;
  800. }
  801. static struct lpfc_dmabuf *
  802. lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
  803. {
  804. struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
  805. uint32_t hbqno;
  806. void *virt; /* virtual address ptr */
  807. dma_addr_t phys; /* mapped address */
  808. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  809. if (hbq_entry == NULL)
  810. return NULL;
  811. list_del(&hbq_entry->dbuf.list);
  812. hbqno = tag >> 16;
  813. new_hbq_entry = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  814. if (new_hbq_entry == NULL)
  815. return &hbq_entry->dbuf;
  816. new_hbq_entry->tag = -1;
  817. phys = new_hbq_entry->dbuf.phys;
  818. virt = new_hbq_entry->dbuf.virt;
  819. new_hbq_entry->dbuf.phys = hbq_entry->dbuf.phys;
  820. new_hbq_entry->dbuf.virt = hbq_entry->dbuf.virt;
  821. hbq_entry->dbuf.phys = phys;
  822. hbq_entry->dbuf.virt = virt;
  823. lpfc_sli_free_hbq(phba, hbq_entry);
  824. return &new_hbq_entry->dbuf;
  825. }
  826. static struct lpfc_dmabuf *
  827. lpfc_sli_get_buff(struct lpfc_hba *phba,
  828. struct lpfc_sli_ring *pring,
  829. uint32_t tag)
  830. {
  831. if (tag & QUE_BUFTAG_BIT)
  832. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  833. else
  834. return lpfc_sli_replace_hbqbuff(phba, tag);
  835. }
  836. static int
  837. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  838. struct lpfc_iocbq *saveq)
  839. {
  840. IOCB_t * irsp;
  841. WORD5 * w5p;
  842. uint32_t Rctl, Type;
  843. uint32_t match, i;
  844. struct lpfc_iocbq *iocbq;
  845. match = 0;
  846. irsp = &(saveq->iocb);
  847. if (irsp->ulpStatus == IOSTAT_NEED_BUFFER)
  848. return 1;
  849. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  850. if (pring->lpfc_sli_rcv_async_status)
  851. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  852. else
  853. lpfc_printf_log(phba,
  854. KERN_WARNING,
  855. LOG_SLI,
  856. "0316 Ring %d handler: unexpected "
  857. "ASYNC_STATUS iocb received evt_code "
  858. "0x%x\n",
  859. pring->ringno,
  860. irsp->un.asyncstat.evt_code);
  861. return 1;
  862. }
  863. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  864. if (irsp->ulpBdeCount != 0) {
  865. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  866. irsp->un.ulpWord[3]);
  867. if (!saveq->context2)
  868. lpfc_printf_log(phba,
  869. KERN_ERR,
  870. LOG_SLI,
  871. "0341 Ring %d Cannot find buffer for "
  872. "an unsolicited iocb. tag 0x%x\n",
  873. pring->ringno,
  874. irsp->un.ulpWord[3]);
  875. }
  876. if (irsp->ulpBdeCount == 2) {
  877. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  878. irsp->unsli3.sli3Words[7]);
  879. if (!saveq->context3)
  880. lpfc_printf_log(phba,
  881. KERN_ERR,
  882. LOG_SLI,
  883. "0342 Ring %d Cannot find buffer for an"
  884. " unsolicited iocb. tag 0x%x\n",
  885. pring->ringno,
  886. irsp->unsli3.sli3Words[7]);
  887. }
  888. list_for_each_entry(iocbq, &saveq->list, list) {
  889. irsp = &(iocbq->iocb);
  890. if (irsp->ulpBdeCount != 0) {
  891. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  892. irsp->un.ulpWord[3]);
  893. if (!iocbq->context2)
  894. lpfc_printf_log(phba,
  895. KERN_ERR,
  896. LOG_SLI,
  897. "0343 Ring %d Cannot find "
  898. "buffer for an unsolicited iocb"
  899. ". tag 0x%x\n", pring->ringno,
  900. irsp->un.ulpWord[3]);
  901. }
  902. if (irsp->ulpBdeCount == 2) {
  903. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  904. irsp->unsli3.sli3Words[7]);
  905. if (!iocbq->context3)
  906. lpfc_printf_log(phba,
  907. KERN_ERR,
  908. LOG_SLI,
  909. "0344 Ring %d Cannot find "
  910. "buffer for an unsolicited "
  911. "iocb. tag 0x%x\n",
  912. pring->ringno,
  913. irsp->unsli3.sli3Words[7]);
  914. }
  915. }
  916. }
  917. if (irsp->ulpBdeCount != 0 &&
  918. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  919. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  920. int found = 0;
  921. /* search continue save q for same XRI */
  922. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  923. if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
  924. list_add_tail(&saveq->list, &iocbq->list);
  925. found = 1;
  926. break;
  927. }
  928. }
  929. if (!found)
  930. list_add_tail(&saveq->clist,
  931. &pring->iocb_continue_saveq);
  932. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  933. list_del_init(&iocbq->clist);
  934. saveq = iocbq;
  935. irsp = &(saveq->iocb);
  936. } else
  937. return 0;
  938. }
  939. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  940. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  941. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  942. Rctl = FC_ELS_REQ;
  943. Type = FC_ELS_DATA;
  944. } else {
  945. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  946. Rctl = w5p->hcsw.Rctl;
  947. Type = w5p->hcsw.Type;
  948. /* Firmware Workaround */
  949. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  950. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  951. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  952. Rctl = FC_ELS_REQ;
  953. Type = FC_ELS_DATA;
  954. w5p->hcsw.Rctl = Rctl;
  955. w5p->hcsw.Type = Type;
  956. }
  957. }
  958. /* unSolicited Responses */
  959. if (pring->prt[0].profile) {
  960. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  961. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  962. saveq);
  963. match = 1;
  964. } else {
  965. /* We must search, based on rctl / type
  966. for the right routine */
  967. for (i = 0; i < pring->num_mask; i++) {
  968. if ((pring->prt[i].rctl == Rctl)
  969. && (pring->prt[i].type == Type)) {
  970. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  971. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  972. (phba, pring, saveq);
  973. match = 1;
  974. break;
  975. }
  976. }
  977. }
  978. if (match == 0) {
  979. /* Unexpected Rctl / Type received */
  980. /* Ring <ringno> handler: unexpected
  981. Rctl <Rctl> Type <Type> received */
  982. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  983. "0313 Ring %d handler: unexpected Rctl x%x "
  984. "Type x%x received\n",
  985. pring->ringno, Rctl, Type);
  986. }
  987. return 1;
  988. }
  989. static struct lpfc_iocbq *
  990. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  991. struct lpfc_sli_ring *pring,
  992. struct lpfc_iocbq *prspiocb)
  993. {
  994. struct lpfc_iocbq *cmd_iocb = NULL;
  995. uint16_t iotag;
  996. iotag = prspiocb->iocb.ulpIoTag;
  997. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  998. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  999. list_del_init(&cmd_iocb->list);
  1000. pring->txcmplq_cnt--;
  1001. return cmd_iocb;
  1002. }
  1003. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1004. "0317 iotag x%x is out off "
  1005. "range: max iotag x%x wd0 x%x\n",
  1006. iotag, phba->sli.last_iotag,
  1007. *(((uint32_t *) &prspiocb->iocb) + 7));
  1008. return NULL;
  1009. }
  1010. static int
  1011. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1012. struct lpfc_iocbq *saveq)
  1013. {
  1014. struct lpfc_iocbq *cmdiocbp;
  1015. int rc = 1;
  1016. unsigned long iflag;
  1017. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  1018. spin_lock_irqsave(&phba->hbalock, iflag);
  1019. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  1020. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1021. if (cmdiocbp) {
  1022. if (cmdiocbp->iocb_cmpl) {
  1023. /*
  1024. * Post all ELS completions to the worker thread.
  1025. * All other are passed to the completion callback.
  1026. */
  1027. if (pring->ringno == LPFC_ELS_RING) {
  1028. if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
  1029. cmdiocbp->iocb_flag &=
  1030. ~LPFC_DRIVER_ABORTED;
  1031. saveq->iocb.ulpStatus =
  1032. IOSTAT_LOCAL_REJECT;
  1033. saveq->iocb.un.ulpWord[4] =
  1034. IOERR_SLI_ABORTED;
  1035. /* Firmware could still be in progress
  1036. * of DMAing payload, so don't free data
  1037. * buffer till after a hbeat.
  1038. */
  1039. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  1040. }
  1041. }
  1042. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  1043. } else
  1044. lpfc_sli_release_iocbq(phba, cmdiocbp);
  1045. } else {
  1046. /*
  1047. * Unknown initiating command based on the response iotag.
  1048. * This could be the case on the ELS ring because of
  1049. * lpfc_els_abort().
  1050. */
  1051. if (pring->ringno != LPFC_ELS_RING) {
  1052. /*
  1053. * Ring <ringno> handler: unexpected completion IoTag
  1054. * <IoTag>
  1055. */
  1056. lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
  1057. "0322 Ring %d handler: "
  1058. "unexpected completion IoTag x%x "
  1059. "Data: x%x x%x x%x x%x\n",
  1060. pring->ringno,
  1061. saveq->iocb.ulpIoTag,
  1062. saveq->iocb.ulpStatus,
  1063. saveq->iocb.un.ulpWord[4],
  1064. saveq->iocb.ulpCommand,
  1065. saveq->iocb.ulpContext);
  1066. }
  1067. }
  1068. return rc;
  1069. }
  1070. static void
  1071. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1072. {
  1073. struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
  1074. &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
  1075. &phba->slim2p->mbx.us.s2.port[pring->ringno];
  1076. /*
  1077. * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
  1078. * rsp ring <portRspMax>
  1079. */
  1080. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1081. "0312 Ring %d handler: portRspPut %d "
  1082. "is bigger then rsp ring %d\n",
  1083. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  1084. pring->numRiocb);
  1085. phba->link_state = LPFC_HBA_ERROR;
  1086. /*
  1087. * All error attention handlers are posted to
  1088. * worker thread
  1089. */
  1090. phba->work_ha |= HA_ERATT;
  1091. phba->work_hs = HS_FFER3;
  1092. /* hbalock should already be held */
  1093. if (phba->work_wait)
  1094. lpfc_worker_wake_up(phba);
  1095. return;
  1096. }
  1097. void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
  1098. {
  1099. struct lpfc_sli *psli = &phba->sli;
  1100. struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
  1101. IOCB_t *irsp = NULL;
  1102. IOCB_t *entry = NULL;
  1103. struct lpfc_iocbq *cmdiocbq = NULL;
  1104. struct lpfc_iocbq rspiocbq;
  1105. struct lpfc_pgp *pgp;
  1106. uint32_t status;
  1107. uint32_t portRspPut, portRspMax;
  1108. int type;
  1109. uint32_t rsp_cmpl = 0;
  1110. uint32_t ha_copy;
  1111. unsigned long iflags;
  1112. pring->stats.iocb_event++;
  1113. pgp = (phba->sli_rev == 3) ?
  1114. &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
  1115. &phba->slim2p->mbx.us.s2.port[pring->ringno];
  1116. /*
  1117. * The next available response entry should never exceed the maximum
  1118. * entries. If it does, treat it as an adapter hardware error.
  1119. */
  1120. portRspMax = pring->numRiocb;
  1121. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1122. if (unlikely(portRspPut >= portRspMax)) {
  1123. lpfc_sli_rsp_pointers_error(phba, pring);
  1124. return;
  1125. }
  1126. rmb();
  1127. while (pring->rspidx != portRspPut) {
  1128. entry = lpfc_resp_iocb(phba, pring);
  1129. if (++pring->rspidx >= portRspMax)
  1130. pring->rspidx = 0;
  1131. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  1132. (uint32_t *) &rspiocbq.iocb,
  1133. phba->iocb_rsp_size);
  1134. irsp = &rspiocbq.iocb;
  1135. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  1136. pring->stats.iocb_rsp++;
  1137. rsp_cmpl++;
  1138. if (unlikely(irsp->ulpStatus)) {
  1139. /* Rsp ring <ringno> error: IOCB */
  1140. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1141. "0326 Rsp Ring %d error: IOCB Data: "
  1142. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1143. pring->ringno,
  1144. irsp->un.ulpWord[0],
  1145. irsp->un.ulpWord[1],
  1146. irsp->un.ulpWord[2],
  1147. irsp->un.ulpWord[3],
  1148. irsp->un.ulpWord[4],
  1149. irsp->un.ulpWord[5],
  1150. *(((uint32_t *) irsp) + 6),
  1151. *(((uint32_t *) irsp) + 7));
  1152. }
  1153. switch (type) {
  1154. case LPFC_ABORT_IOCB:
  1155. case LPFC_SOL_IOCB:
  1156. /*
  1157. * Idle exchange closed via ABTS from port. No iocb
  1158. * resources need to be recovered.
  1159. */
  1160. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  1161. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  1162. "0314 IOCB cmd 0x%x "
  1163. "processed. Skipping "
  1164. "completion",
  1165. irsp->ulpCommand);
  1166. break;
  1167. }
  1168. spin_lock_irqsave(&phba->hbalock, iflags);
  1169. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  1170. &rspiocbq);
  1171. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1172. if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
  1173. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  1174. &rspiocbq);
  1175. }
  1176. break;
  1177. default:
  1178. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  1179. char adaptermsg[LPFC_MAX_ADPTMSG];
  1180. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  1181. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  1182. MAX_MSG_DATA);
  1183. dev_warn(&((phba->pcidev)->dev),
  1184. "lpfc%d: %s\n",
  1185. phba->brd_no, adaptermsg);
  1186. } else {
  1187. /* Unknown IOCB command */
  1188. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1189. "0321 Unknown IOCB command "
  1190. "Data: x%x, x%x x%x x%x x%x\n",
  1191. type, irsp->ulpCommand,
  1192. irsp->ulpStatus,
  1193. irsp->ulpIoTag,
  1194. irsp->ulpContext);
  1195. }
  1196. break;
  1197. }
  1198. /*
  1199. * The response IOCB has been processed. Update the ring
  1200. * pointer in SLIM. If the port response put pointer has not
  1201. * been updated, sync the pgp->rspPutInx and fetch the new port
  1202. * response put pointer.
  1203. */
  1204. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  1205. if (pring->rspidx == portRspPut)
  1206. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1207. }
  1208. ha_copy = readl(phba->HAregaddr);
  1209. ha_copy >>= (LPFC_FCP_RING * 4);
  1210. if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
  1211. spin_lock_irqsave(&phba->hbalock, iflags);
  1212. pring->stats.iocb_rsp_full++;
  1213. status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
  1214. writel(status, phba->CAregaddr);
  1215. readl(phba->CAregaddr);
  1216. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1217. }
  1218. if ((ha_copy & HA_R0CE_RSP) &&
  1219. (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  1220. spin_lock_irqsave(&phba->hbalock, iflags);
  1221. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  1222. pring->stats.iocb_cmd_empty++;
  1223. /* Force update of the local copy of cmdGetInx */
  1224. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1225. lpfc_sli_resume_iocb(phba, pring);
  1226. if ((pring->lpfc_sli_cmd_available))
  1227. (pring->lpfc_sli_cmd_available) (phba, pring);
  1228. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1229. }
  1230. return;
  1231. }
  1232. /*
  1233. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  1234. * to check it explicitly.
  1235. */
  1236. static int
  1237. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  1238. struct lpfc_sli_ring *pring, uint32_t mask)
  1239. {
  1240. struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
  1241. &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
  1242. &phba->slim2p->mbx.us.s2.port[pring->ringno];
  1243. IOCB_t *irsp = NULL;
  1244. IOCB_t *entry = NULL;
  1245. struct lpfc_iocbq *cmdiocbq = NULL;
  1246. struct lpfc_iocbq rspiocbq;
  1247. uint32_t status;
  1248. uint32_t portRspPut, portRspMax;
  1249. int rc = 1;
  1250. lpfc_iocb_type type;
  1251. unsigned long iflag;
  1252. uint32_t rsp_cmpl = 0;
  1253. spin_lock_irqsave(&phba->hbalock, iflag);
  1254. pring->stats.iocb_event++;
  1255. /*
  1256. * The next available response entry should never exceed the maximum
  1257. * entries. If it does, treat it as an adapter hardware error.
  1258. */
  1259. portRspMax = pring->numRiocb;
  1260. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1261. if (unlikely(portRspPut >= portRspMax)) {
  1262. lpfc_sli_rsp_pointers_error(phba, pring);
  1263. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1264. return 1;
  1265. }
  1266. rmb();
  1267. while (pring->rspidx != portRspPut) {
  1268. /*
  1269. * Fetch an entry off the ring and copy it into a local data
  1270. * structure. The copy involves a byte-swap since the
  1271. * network byte order and pci byte orders are different.
  1272. */
  1273. entry = lpfc_resp_iocb(phba, pring);
  1274. phba->last_completion_time = jiffies;
  1275. if (++pring->rspidx >= portRspMax)
  1276. pring->rspidx = 0;
  1277. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  1278. (uint32_t *) &rspiocbq.iocb,
  1279. phba->iocb_rsp_size);
  1280. INIT_LIST_HEAD(&(rspiocbq.list));
  1281. irsp = &rspiocbq.iocb;
  1282. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  1283. pring->stats.iocb_rsp++;
  1284. rsp_cmpl++;
  1285. if (unlikely(irsp->ulpStatus)) {
  1286. /*
  1287. * If resource errors reported from HBA, reduce
  1288. * queuedepths of the SCSI device.
  1289. */
  1290. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  1291. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  1292. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1293. lpfc_adjust_queue_depth(phba);
  1294. spin_lock_irqsave(&phba->hbalock, iflag);
  1295. }
  1296. /* Rsp ring <ringno> error: IOCB */
  1297. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1298. "0336 Rsp Ring %d error: IOCB Data: "
  1299. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1300. pring->ringno,
  1301. irsp->un.ulpWord[0],
  1302. irsp->un.ulpWord[1],
  1303. irsp->un.ulpWord[2],
  1304. irsp->un.ulpWord[3],
  1305. irsp->un.ulpWord[4],
  1306. irsp->un.ulpWord[5],
  1307. *(((uint32_t *) irsp) + 6),
  1308. *(((uint32_t *) irsp) + 7));
  1309. }
  1310. switch (type) {
  1311. case LPFC_ABORT_IOCB:
  1312. case LPFC_SOL_IOCB:
  1313. /*
  1314. * Idle exchange closed via ABTS from port. No iocb
  1315. * resources need to be recovered.
  1316. */
  1317. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  1318. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  1319. "0333 IOCB cmd 0x%x"
  1320. " processed. Skipping"
  1321. " completion\n",
  1322. irsp->ulpCommand);
  1323. break;
  1324. }
  1325. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  1326. &rspiocbq);
  1327. if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
  1328. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  1329. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  1330. &rspiocbq);
  1331. } else {
  1332. spin_unlock_irqrestore(&phba->hbalock,
  1333. iflag);
  1334. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  1335. &rspiocbq);
  1336. spin_lock_irqsave(&phba->hbalock,
  1337. iflag);
  1338. }
  1339. }
  1340. break;
  1341. case LPFC_UNSOL_IOCB:
  1342. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1343. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  1344. spin_lock_irqsave(&phba->hbalock, iflag);
  1345. break;
  1346. default:
  1347. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  1348. char adaptermsg[LPFC_MAX_ADPTMSG];
  1349. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  1350. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  1351. MAX_MSG_DATA);
  1352. dev_warn(&((phba->pcidev)->dev),
  1353. "lpfc%d: %s\n",
  1354. phba->brd_no, adaptermsg);
  1355. } else {
  1356. /* Unknown IOCB command */
  1357. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1358. "0334 Unknown IOCB command "
  1359. "Data: x%x, x%x x%x x%x x%x\n",
  1360. type, irsp->ulpCommand,
  1361. irsp->ulpStatus,
  1362. irsp->ulpIoTag,
  1363. irsp->ulpContext);
  1364. }
  1365. break;
  1366. }
  1367. /*
  1368. * The response IOCB has been processed. Update the ring
  1369. * pointer in SLIM. If the port response put pointer has not
  1370. * been updated, sync the pgp->rspPutInx and fetch the new port
  1371. * response put pointer.
  1372. */
  1373. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  1374. if (pring->rspidx == portRspPut)
  1375. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1376. }
  1377. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  1378. pring->stats.iocb_rsp_full++;
  1379. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  1380. writel(status, phba->CAregaddr);
  1381. readl(phba->CAregaddr);
  1382. }
  1383. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  1384. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  1385. pring->stats.iocb_cmd_empty++;
  1386. /* Force update of the local copy of cmdGetInx */
  1387. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1388. lpfc_sli_resume_iocb(phba, pring);
  1389. if ((pring->lpfc_sli_cmd_available))
  1390. (pring->lpfc_sli_cmd_available) (phba, pring);
  1391. }
  1392. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1393. return rc;
  1394. }
  1395. int
  1396. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  1397. struct lpfc_sli_ring *pring, uint32_t mask)
  1398. {
  1399. struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
  1400. &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
  1401. &phba->slim2p->mbx.us.s2.port[pring->ringno];
  1402. IOCB_t *entry;
  1403. IOCB_t *irsp = NULL;
  1404. struct lpfc_iocbq *rspiocbp = NULL;
  1405. struct lpfc_iocbq *next_iocb;
  1406. struct lpfc_iocbq *cmdiocbp;
  1407. struct lpfc_iocbq *saveq;
  1408. uint8_t iocb_cmd_type;
  1409. lpfc_iocb_type type;
  1410. uint32_t status, free_saveq;
  1411. uint32_t portRspPut, portRspMax;
  1412. int rc = 1;
  1413. unsigned long iflag;
  1414. spin_lock_irqsave(&phba->hbalock, iflag);
  1415. pring->stats.iocb_event++;
  1416. /*
  1417. * The next available response entry should never exceed the maximum
  1418. * entries. If it does, treat it as an adapter hardware error.
  1419. */
  1420. portRspMax = pring->numRiocb;
  1421. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1422. if (portRspPut >= portRspMax) {
  1423. /*
  1424. * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
  1425. * rsp ring <portRspMax>
  1426. */
  1427. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1428. "0303 Ring %d handler: portRspPut %d "
  1429. "is bigger then rsp ring %d\n",
  1430. pring->ringno, portRspPut, portRspMax);
  1431. phba->link_state = LPFC_HBA_ERROR;
  1432. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1433. phba->work_hs = HS_FFER3;
  1434. lpfc_handle_eratt(phba);
  1435. return 1;
  1436. }
  1437. rmb();
  1438. while (pring->rspidx != portRspPut) {
  1439. /*
  1440. * Build a completion list and call the appropriate handler.
  1441. * The process is to get the next available response iocb, get
  1442. * a free iocb from the list, copy the response data into the
  1443. * free iocb, insert to the continuation list, and update the
  1444. * next response index to slim. This process makes response
  1445. * iocb's in the ring available to DMA as fast as possible but
  1446. * pays a penalty for a copy operation. Since the iocb is
  1447. * only 32 bytes, this penalty is considered small relative to
  1448. * the PCI reads for register values and a slim write. When
  1449. * the ulpLe field is set, the entire Command has been
  1450. * received.
  1451. */
  1452. entry = lpfc_resp_iocb(phba, pring);
  1453. phba->last_completion_time = jiffies;
  1454. rspiocbp = __lpfc_sli_get_iocbq(phba);
  1455. if (rspiocbp == NULL) {
  1456. printk(KERN_ERR "%s: out of buffers! Failing "
  1457. "completion.\n", __FUNCTION__);
  1458. break;
  1459. }
  1460. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  1461. phba->iocb_rsp_size);
  1462. irsp = &rspiocbp->iocb;
  1463. if (++pring->rspidx >= portRspMax)
  1464. pring->rspidx = 0;
  1465. if (pring->ringno == LPFC_ELS_RING) {
  1466. lpfc_debugfs_slow_ring_trc(phba,
  1467. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1468. *(((uint32_t *) irsp) + 4),
  1469. *(((uint32_t *) irsp) + 6),
  1470. *(((uint32_t *) irsp) + 7));
  1471. }
  1472. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  1473. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  1474. pring->iocb_continueq_cnt++;
  1475. if (irsp->ulpLe) {
  1476. /*
  1477. * By default, the driver expects to free all resources
  1478. * associated with this iocb completion.
  1479. */
  1480. free_saveq = 1;
  1481. saveq = list_get_first(&pring->iocb_continueq,
  1482. struct lpfc_iocbq, list);
  1483. irsp = &(saveq->iocb);
  1484. list_del_init(&pring->iocb_continueq);
  1485. pring->iocb_continueq_cnt = 0;
  1486. pring->stats.iocb_rsp++;
  1487. /*
  1488. * If resource errors reported from HBA, reduce
  1489. * queuedepths of the SCSI device.
  1490. */
  1491. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  1492. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  1493. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1494. lpfc_adjust_queue_depth(phba);
  1495. spin_lock_irqsave(&phba->hbalock, iflag);
  1496. }
  1497. if (irsp->ulpStatus) {
  1498. /* Rsp ring <ringno> error: IOCB */
  1499. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1500. "0328 Rsp Ring %d error: "
  1501. "IOCB Data: "
  1502. "x%x x%x x%x x%x "
  1503. "x%x x%x x%x x%x "
  1504. "x%x x%x x%x x%x "
  1505. "x%x x%x x%x x%x\n",
  1506. pring->ringno,
  1507. irsp->un.ulpWord[0],
  1508. irsp->un.ulpWord[1],
  1509. irsp->un.ulpWord[2],
  1510. irsp->un.ulpWord[3],
  1511. irsp->un.ulpWord[4],
  1512. irsp->un.ulpWord[5],
  1513. *(((uint32_t *) irsp) + 6),
  1514. *(((uint32_t *) irsp) + 7),
  1515. *(((uint32_t *) irsp) + 8),
  1516. *(((uint32_t *) irsp) + 9),
  1517. *(((uint32_t *) irsp) + 10),
  1518. *(((uint32_t *) irsp) + 11),
  1519. *(((uint32_t *) irsp) + 12),
  1520. *(((uint32_t *) irsp) + 13),
  1521. *(((uint32_t *) irsp) + 14),
  1522. *(((uint32_t *) irsp) + 15));
  1523. }
  1524. /*
  1525. * Fetch the IOCB command type and call the correct
  1526. * completion routine. Solicited and Unsolicited
  1527. * IOCBs on the ELS ring get freed back to the
  1528. * lpfc_iocb_list by the discovery kernel thread.
  1529. */
  1530. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  1531. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  1532. if (type == LPFC_SOL_IOCB) {
  1533. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1534. rc = lpfc_sli_process_sol_iocb(phba, pring,
  1535. saveq);
  1536. spin_lock_irqsave(&phba->hbalock, iflag);
  1537. } else if (type == LPFC_UNSOL_IOCB) {
  1538. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1539. rc = lpfc_sli_process_unsol_iocb(phba, pring,
  1540. saveq);
  1541. spin_lock_irqsave(&phba->hbalock, iflag);
  1542. if (!rc)
  1543. free_saveq = 0;
  1544. } else if (type == LPFC_ABORT_IOCB) {
  1545. if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
  1546. ((cmdiocbp =
  1547. lpfc_sli_iocbq_lookup(phba, pring,
  1548. saveq)))) {
  1549. /* Call the specified completion
  1550. routine */
  1551. if (cmdiocbp->iocb_cmpl) {
  1552. spin_unlock_irqrestore(
  1553. &phba->hbalock,
  1554. iflag);
  1555. (cmdiocbp->iocb_cmpl) (phba,
  1556. cmdiocbp, saveq);
  1557. spin_lock_irqsave(
  1558. &phba->hbalock,
  1559. iflag);
  1560. } else
  1561. __lpfc_sli_release_iocbq(phba,
  1562. cmdiocbp);
  1563. }
  1564. } else if (type == LPFC_UNKNOWN_IOCB) {
  1565. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  1566. char adaptermsg[LPFC_MAX_ADPTMSG];
  1567. memset(adaptermsg, 0,
  1568. LPFC_MAX_ADPTMSG);
  1569. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  1570. MAX_MSG_DATA);
  1571. dev_warn(&((phba->pcidev)->dev),
  1572. "lpfc%d: %s\n",
  1573. phba->brd_no, adaptermsg);
  1574. } else {
  1575. /* Unknown IOCB command */
  1576. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1577. "0335 Unknown IOCB "
  1578. "command Data: x%x "
  1579. "x%x x%x x%x\n",
  1580. irsp->ulpCommand,
  1581. irsp->ulpStatus,
  1582. irsp->ulpIoTag,
  1583. irsp->ulpContext);
  1584. }
  1585. }
  1586. if (free_saveq) {
  1587. list_for_each_entry_safe(rspiocbp, next_iocb,
  1588. &saveq->list, list) {
  1589. list_del(&rspiocbp->list);
  1590. __lpfc_sli_release_iocbq(phba,
  1591. rspiocbp);
  1592. }
  1593. __lpfc_sli_release_iocbq(phba, saveq);
  1594. }
  1595. rspiocbp = NULL;
  1596. }
  1597. /*
  1598. * If the port response put pointer has not been updated, sync
  1599. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  1600. * response put pointer.
  1601. */
  1602. if (pring->rspidx == portRspPut) {
  1603. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1604. }
  1605. } /* while (pring->rspidx != portRspPut) */
  1606. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  1607. /* At least one response entry has been freed */
  1608. pring->stats.iocb_rsp_full++;
  1609. /* SET RxRE_RSP in Chip Att register */
  1610. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  1611. writel(status, phba->CAregaddr);
  1612. readl(phba->CAregaddr); /* flush */
  1613. }
  1614. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  1615. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  1616. pring->stats.iocb_cmd_empty++;
  1617. /* Force update of the local copy of cmdGetInx */
  1618. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1619. lpfc_sli_resume_iocb(phba, pring);
  1620. if ((pring->lpfc_sli_cmd_available))
  1621. (pring->lpfc_sli_cmd_available) (phba, pring);
  1622. }
  1623. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1624. return rc;
  1625. }
  1626. void
  1627. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1628. {
  1629. LIST_HEAD(completions);
  1630. struct lpfc_iocbq *iocb, *next_iocb;
  1631. IOCB_t *cmd = NULL;
  1632. if (pring->ringno == LPFC_ELS_RING) {
  1633. lpfc_fabric_abort_hba(phba);
  1634. }
  1635. /* Error everything on txq and txcmplq
  1636. * First do the txq.
  1637. */
  1638. spin_lock_irq(&phba->hbalock);
  1639. list_splice_init(&pring->txq, &completions);
  1640. pring->txq_cnt = 0;
  1641. /* Next issue ABTS for everything on the txcmplq */
  1642. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  1643. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  1644. spin_unlock_irq(&phba->hbalock);
  1645. while (!list_empty(&completions)) {
  1646. iocb = list_get_first(&completions, struct lpfc_iocbq, list);
  1647. cmd = &iocb->iocb;
  1648. list_del_init(&iocb->list);
  1649. if (!iocb->iocb_cmpl)
  1650. lpfc_sli_release_iocbq(phba, iocb);
  1651. else {
  1652. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  1653. cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
  1654. (iocb->iocb_cmpl) (phba, iocb, iocb);
  1655. }
  1656. }
  1657. }
  1658. int
  1659. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  1660. {
  1661. uint32_t status;
  1662. int i = 0;
  1663. int retval = 0;
  1664. /* Read the HBA Host Status Register */
  1665. status = readl(phba->HSregaddr);
  1666. /*
  1667. * Check status register every 100ms for 5 retries, then every
  1668. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  1669. * every 2.5 sec for 4.
  1670. * Break our of the loop if errors occurred during init.
  1671. */
  1672. while (((status & mask) != mask) &&
  1673. !(status & HS_FFERM) &&
  1674. i++ < 20) {
  1675. if (i <= 5)
  1676. msleep(10);
  1677. else if (i <= 10)
  1678. msleep(500);
  1679. else
  1680. msleep(2500);
  1681. if (i == 15) {
  1682. /* Do post */
  1683. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  1684. lpfc_sli_brdrestart(phba);
  1685. }
  1686. /* Read the HBA Host Status Register */
  1687. status = readl(phba->HSregaddr);
  1688. }
  1689. /* Check to see if any errors occurred during init */
  1690. if ((status & HS_FFERM) || (i >= 20)) {
  1691. phba->link_state = LPFC_HBA_ERROR;
  1692. retval = 1;
  1693. }
  1694. return retval;
  1695. }
  1696. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  1697. void lpfc_reset_barrier(struct lpfc_hba *phba)
  1698. {
  1699. uint32_t __iomem *resp_buf;
  1700. uint32_t __iomem *mbox_buf;
  1701. volatile uint32_t mbox;
  1702. uint32_t hc_copy;
  1703. int i;
  1704. uint8_t hdrtype;
  1705. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  1706. if (hdrtype != 0x80 ||
  1707. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  1708. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  1709. return;
  1710. /*
  1711. * Tell the other part of the chip to suspend temporarily all
  1712. * its DMA activity.
  1713. */
  1714. resp_buf = phba->MBslimaddr;
  1715. /* Disable the error attention */
  1716. hc_copy = readl(phba->HCregaddr);
  1717. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  1718. readl(phba->HCregaddr); /* flush */
  1719. phba->link_flag |= LS_IGNORE_ERATT;
  1720. if (readl(phba->HAregaddr) & HA_ERATT) {
  1721. /* Clear Chip error bit */
  1722. writel(HA_ERATT, phba->HAregaddr);
  1723. phba->pport->stopped = 1;
  1724. }
  1725. mbox = 0;
  1726. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  1727. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  1728. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  1729. mbox_buf = phba->MBslimaddr;
  1730. writel(mbox, mbox_buf);
  1731. for (i = 0;
  1732. readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
  1733. mdelay(1);
  1734. if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
  1735. if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
  1736. phba->pport->stopped)
  1737. goto restore_hc;
  1738. else
  1739. goto clear_errat;
  1740. }
  1741. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  1742. for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
  1743. mdelay(1);
  1744. clear_errat:
  1745. while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
  1746. mdelay(1);
  1747. if (readl(phba->HAregaddr) & HA_ERATT) {
  1748. writel(HA_ERATT, phba->HAregaddr);
  1749. phba->pport->stopped = 1;
  1750. }
  1751. restore_hc:
  1752. phba->link_flag &= ~LS_IGNORE_ERATT;
  1753. writel(hc_copy, phba->HCregaddr);
  1754. readl(phba->HCregaddr); /* flush */
  1755. }
  1756. int
  1757. lpfc_sli_brdkill(struct lpfc_hba *phba)
  1758. {
  1759. struct lpfc_sli *psli;
  1760. LPFC_MBOXQ_t *pmb;
  1761. uint32_t status;
  1762. uint32_t ha_copy;
  1763. int retval;
  1764. int i = 0;
  1765. psli = &phba->sli;
  1766. /* Kill HBA */
  1767. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  1768. "0329 Kill HBA Data: x%x x%x\n",
  1769. phba->pport->port_state, psli->sli_flag);
  1770. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1771. if (!pmb)
  1772. return 1;
  1773. /* Disable the error attention */
  1774. spin_lock_irq(&phba->hbalock);
  1775. status = readl(phba->HCregaddr);
  1776. status &= ~HC_ERINT_ENA;
  1777. writel(status, phba->HCregaddr);
  1778. readl(phba->HCregaddr); /* flush */
  1779. phba->link_flag |= LS_IGNORE_ERATT;
  1780. spin_unlock_irq(&phba->hbalock);
  1781. lpfc_kill_board(phba, pmb);
  1782. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1783. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1784. if (retval != MBX_SUCCESS) {
  1785. if (retval != MBX_BUSY)
  1786. mempool_free(pmb, phba->mbox_mem_pool);
  1787. spin_lock_irq(&phba->hbalock);
  1788. phba->link_flag &= ~LS_IGNORE_ERATT;
  1789. spin_unlock_irq(&phba->hbalock);
  1790. return 1;
  1791. }
  1792. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  1793. mempool_free(pmb, phba->mbox_mem_pool);
  1794. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  1795. * attention every 100ms for 3 seconds. If we don't get ERATT after
  1796. * 3 seconds we still set HBA_ERROR state because the status of the
  1797. * board is now undefined.
  1798. */
  1799. ha_copy = readl(phba->HAregaddr);
  1800. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  1801. mdelay(100);
  1802. ha_copy = readl(phba->HAregaddr);
  1803. }
  1804. del_timer_sync(&psli->mbox_tmo);
  1805. if (ha_copy & HA_ERATT) {
  1806. writel(HA_ERATT, phba->HAregaddr);
  1807. phba->pport->stopped = 1;
  1808. }
  1809. spin_lock_irq(&phba->hbalock);
  1810. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  1811. phba->link_flag &= ~LS_IGNORE_ERATT;
  1812. spin_unlock_irq(&phba->hbalock);
  1813. psli->mbox_active = NULL;
  1814. lpfc_hba_down_post(phba);
  1815. phba->link_state = LPFC_HBA_ERROR;
  1816. return ha_copy & HA_ERATT ? 0 : 1;
  1817. }
  1818. int
  1819. lpfc_sli_brdreset(struct lpfc_hba *phba)
  1820. {
  1821. struct lpfc_sli *psli;
  1822. struct lpfc_sli_ring *pring;
  1823. uint16_t cfg_value;
  1824. int i;
  1825. psli = &phba->sli;
  1826. /* Reset HBA */
  1827. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  1828. "0325 Reset HBA Data: x%x x%x\n",
  1829. phba->pport->port_state, psli->sli_flag);
  1830. /* perform board reset */
  1831. phba->fc_eventTag = 0;
  1832. phba->pport->fc_myDID = 0;
  1833. phba->pport->fc_prevDID = 0;
  1834. /* Turn off parity checking and serr during the physical reset */
  1835. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  1836. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  1837. (cfg_value &
  1838. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  1839. psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
  1840. /* Now toggle INITFF bit in the Host Control Register */
  1841. writel(HC_INITFF, phba->HCregaddr);
  1842. mdelay(1);
  1843. readl(phba->HCregaddr); /* flush */
  1844. writel(0, phba->HCregaddr);
  1845. readl(phba->HCregaddr); /* flush */
  1846. /* Restore PCI cmd register */
  1847. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  1848. /* Initialize relevant SLI info */
  1849. for (i = 0; i < psli->num_rings; i++) {
  1850. pring = &psli->ring[i];
  1851. pring->flag = 0;
  1852. pring->rspidx = 0;
  1853. pring->next_cmdidx = 0;
  1854. pring->local_getidx = 0;
  1855. pring->cmdidx = 0;
  1856. pring->missbufcnt = 0;
  1857. }
  1858. phba->link_state = LPFC_WARM_START;
  1859. return 0;
  1860. }
  1861. int
  1862. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  1863. {
  1864. MAILBOX_t *mb;
  1865. struct lpfc_sli *psli;
  1866. uint16_t skip_post;
  1867. volatile uint32_t word0;
  1868. void __iomem *to_slim;
  1869. spin_lock_irq(&phba->hbalock);
  1870. psli = &phba->sli;
  1871. /* Restart HBA */
  1872. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  1873. "0337 Restart HBA Data: x%x x%x\n",
  1874. phba->pport->port_state, psli->sli_flag);
  1875. word0 = 0;
  1876. mb = (MAILBOX_t *) &word0;
  1877. mb->mbxCommand = MBX_RESTART;
  1878. mb->mbxHc = 1;
  1879. lpfc_reset_barrier(phba);
  1880. to_slim = phba->MBslimaddr;
  1881. writel(*(uint32_t *) mb, to_slim);
  1882. readl(to_slim); /* flush */
  1883. /* Only skip post after fc_ffinit is completed */
  1884. if (phba->pport->port_state) {
  1885. skip_post = 1;
  1886. word0 = 1; /* This is really setting up word1 */
  1887. } else {
  1888. skip_post = 0;
  1889. word0 = 0; /* This is really setting up word1 */
  1890. }
  1891. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  1892. writel(*(uint32_t *) mb, to_slim);
  1893. readl(to_slim); /* flush */
  1894. lpfc_sli_brdreset(phba);
  1895. phba->pport->stopped = 0;
  1896. phba->link_state = LPFC_INIT_START;
  1897. spin_unlock_irq(&phba->hbalock);
  1898. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  1899. psli->stats_start = get_seconds();
  1900. if (skip_post)
  1901. mdelay(100);
  1902. else
  1903. mdelay(2000);
  1904. lpfc_hba_down_post(phba);
  1905. return 0;
  1906. }
  1907. static int
  1908. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  1909. {
  1910. uint32_t status, i = 0;
  1911. /* Read the HBA Host Status Register */
  1912. status = readl(phba->HSregaddr);
  1913. /* Check status register to see what current state is */
  1914. i = 0;
  1915. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  1916. /* Check every 100ms for 5 retries, then every 500ms for 5, then
  1917. * every 2.5 sec for 5, then reset board and every 2.5 sec for
  1918. * 4.
  1919. */
  1920. if (i++ >= 20) {
  1921. /* Adapter failed to init, timeout, status reg
  1922. <status> */
  1923. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1924. "0436 Adapter failed to init, "
  1925. "timeout, status reg x%x, "
  1926. "FW Data: A8 x%x AC x%x\n", status,
  1927. readl(phba->MBslimaddr + 0xa8),
  1928. readl(phba->MBslimaddr + 0xac));
  1929. phba->link_state = LPFC_HBA_ERROR;
  1930. return -ETIMEDOUT;
  1931. }
  1932. /* Check to see if any errors occurred during init */
  1933. if (status & HS_FFERM) {
  1934. /* ERROR: During chipset initialization */
  1935. /* Adapter failed to init, chipset, status reg
  1936. <status> */
  1937. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1938. "0437 Adapter failed to init, "
  1939. "chipset, status reg x%x, "
  1940. "FW Data: A8 x%x AC x%x\n", status,
  1941. readl(phba->MBslimaddr + 0xa8),
  1942. readl(phba->MBslimaddr + 0xac));
  1943. phba->link_state = LPFC_HBA_ERROR;
  1944. return -EIO;
  1945. }
  1946. if (i <= 5) {
  1947. msleep(10);
  1948. } else if (i <= 10) {
  1949. msleep(500);
  1950. } else {
  1951. msleep(2500);
  1952. }
  1953. if (i == 15) {
  1954. /* Do post */
  1955. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  1956. lpfc_sli_brdrestart(phba);
  1957. }
  1958. /* Read the HBA Host Status Register */
  1959. status = readl(phba->HSregaddr);
  1960. }
  1961. /* Check to see if any errors occurred during init */
  1962. if (status & HS_FFERM) {
  1963. /* ERROR: During chipset initialization */
  1964. /* Adapter failed to init, chipset, status reg <status> */
  1965. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1966. "0438 Adapter failed to init, chipset, "
  1967. "status reg x%x, "
  1968. "FW Data: A8 x%x AC x%x\n", status,
  1969. readl(phba->MBslimaddr + 0xa8),
  1970. readl(phba->MBslimaddr + 0xac));
  1971. phba->link_state = LPFC_HBA_ERROR;
  1972. return -EIO;
  1973. }
  1974. /* Clear all interrupt enable conditions */
  1975. writel(0, phba->HCregaddr);
  1976. readl(phba->HCregaddr); /* flush */
  1977. /* setup host attn register */
  1978. writel(0xffffffff, phba->HAregaddr);
  1979. readl(phba->HAregaddr); /* flush */
  1980. return 0;
  1981. }
  1982. int
  1983. lpfc_sli_hbq_count(void)
  1984. {
  1985. return ARRAY_SIZE(lpfc_hbq_defs);
  1986. }
  1987. static int
  1988. lpfc_sli_hbq_entry_count(void)
  1989. {
  1990. int hbq_count = lpfc_sli_hbq_count();
  1991. int count = 0;
  1992. int i;
  1993. for (i = 0; i < hbq_count; ++i)
  1994. count += lpfc_hbq_defs[i]->entry_count;
  1995. return count;
  1996. }
  1997. int
  1998. lpfc_sli_hbq_size(void)
  1999. {
  2000. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  2001. }
  2002. static int
  2003. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  2004. {
  2005. int hbq_count = lpfc_sli_hbq_count();
  2006. LPFC_MBOXQ_t *pmb;
  2007. MAILBOX_t *pmbox;
  2008. uint32_t hbqno;
  2009. uint32_t hbq_entry_index;
  2010. /* Get a Mailbox buffer to setup mailbox
  2011. * commands for HBA initialization
  2012. */
  2013. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2014. if (!pmb)
  2015. return -ENOMEM;
  2016. pmbox = &pmb->mb;
  2017. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  2018. phba->link_state = LPFC_INIT_MBX_CMDS;
  2019. hbq_entry_index = 0;
  2020. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  2021. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  2022. phba->hbqs[hbqno].hbqPutIdx = 0;
  2023. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  2024. phba->hbqs[hbqno].entry_count =
  2025. lpfc_hbq_defs[hbqno]->entry_count;
  2026. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  2027. hbq_entry_index, pmb);
  2028. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  2029. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  2030. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  2031. mbxStatus <status>, ring <num> */
  2032. lpfc_printf_log(phba, KERN_ERR,
  2033. LOG_SLI | LOG_VPORT,
  2034. "1805 Adapter failed to init. "
  2035. "Data: x%x x%x x%x\n",
  2036. pmbox->mbxCommand,
  2037. pmbox->mbxStatus, hbqno);
  2038. phba->link_state = LPFC_HBA_ERROR;
  2039. mempool_free(pmb, phba->mbox_mem_pool);
  2040. return ENXIO;
  2041. }
  2042. }
  2043. phba->hbq_count = hbq_count;
  2044. mempool_free(pmb, phba->mbox_mem_pool);
  2045. /* Initially populate or replenish the HBQs */
  2046. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  2047. if (lpfc_sli_hbqbuf_init_hbqs(phba, hbqno))
  2048. return -ENOMEM;
  2049. }
  2050. return 0;
  2051. }
  2052. static int
  2053. lpfc_do_config_port(struct lpfc_hba *phba, int sli_mode)
  2054. {
  2055. LPFC_MBOXQ_t *pmb;
  2056. uint32_t resetcount = 0, rc = 0, done = 0;
  2057. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2058. if (!pmb) {
  2059. phba->link_state = LPFC_HBA_ERROR;
  2060. return -ENOMEM;
  2061. }
  2062. phba->sli_rev = sli_mode;
  2063. while (resetcount < 2 && !done) {
  2064. spin_lock_irq(&phba->hbalock);
  2065. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  2066. spin_unlock_irq(&phba->hbalock);
  2067. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  2068. lpfc_sli_brdrestart(phba);
  2069. msleep(2500);
  2070. rc = lpfc_sli_chipset_init(phba);
  2071. if (rc)
  2072. break;
  2073. spin_lock_irq(&phba->hbalock);
  2074. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  2075. spin_unlock_irq(&phba->hbalock);
  2076. resetcount++;
  2077. /* Call pre CONFIG_PORT mailbox command initialization. A
  2078. * value of 0 means the call was successful. Any other
  2079. * nonzero value is a failure, but if ERESTART is returned,
  2080. * the driver may reset the HBA and try again.
  2081. */
  2082. rc = lpfc_config_port_prep(phba);
  2083. if (rc == -ERESTART) {
  2084. phba->link_state = LPFC_LINK_UNKNOWN;
  2085. continue;
  2086. } else if (rc) {
  2087. break;
  2088. }
  2089. phba->link_state = LPFC_INIT_MBX_CMDS;
  2090. lpfc_config_port(phba, pmb);
  2091. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  2092. if (rc != MBX_SUCCESS) {
  2093. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2094. "0442 Adapter failed to init, mbxCmd x%x "
  2095. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  2096. pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
  2097. spin_lock_irq(&phba->hbalock);
  2098. phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
  2099. spin_unlock_irq(&phba->hbalock);
  2100. rc = -ENXIO;
  2101. } else {
  2102. done = 1;
  2103. phba->max_vpi = (phba->max_vpi &&
  2104. pmb->mb.un.varCfgPort.gmv) != 0
  2105. ? pmb->mb.un.varCfgPort.max_vpi
  2106. : 0;
  2107. }
  2108. }
  2109. if (!done) {
  2110. rc = -EINVAL;
  2111. goto do_prep_failed;
  2112. }
  2113. if ((pmb->mb.un.varCfgPort.sli_mode == 3) &&
  2114. (!pmb->mb.un.varCfgPort.cMA)) {
  2115. rc = -ENXIO;
  2116. goto do_prep_failed;
  2117. }
  2118. return rc;
  2119. do_prep_failed:
  2120. mempool_free(pmb, phba->mbox_mem_pool);
  2121. return rc;
  2122. }
  2123. int
  2124. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  2125. {
  2126. uint32_t rc;
  2127. int mode = 3;
  2128. switch (lpfc_sli_mode) {
  2129. case 2:
  2130. if (phba->cfg_enable_npiv) {
  2131. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  2132. "1824 NPIV enabled: Override lpfc_sli_mode "
  2133. "parameter (%d) to auto (0).\n",
  2134. lpfc_sli_mode);
  2135. break;
  2136. }
  2137. mode = 2;
  2138. break;
  2139. case 0:
  2140. case 3:
  2141. break;
  2142. default:
  2143. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  2144. "1819 Unrecognized lpfc_sli_mode "
  2145. "parameter: %d.\n", lpfc_sli_mode);
  2146. break;
  2147. }
  2148. rc = lpfc_do_config_port(phba, mode);
  2149. if (rc && lpfc_sli_mode == 3)
  2150. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  2151. "1820 Unable to select SLI-3. "
  2152. "Not supported by adapter.\n");
  2153. if (rc && mode != 2)
  2154. rc = lpfc_do_config_port(phba, 2);
  2155. if (rc)
  2156. goto lpfc_sli_hba_setup_error;
  2157. if (phba->sli_rev == 3) {
  2158. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  2159. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  2160. phba->sli3_options |= LPFC_SLI3_ENABLED;
  2161. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  2162. } else {
  2163. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  2164. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  2165. phba->sli3_options = 0;
  2166. }
  2167. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2168. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  2169. phba->sli_rev, phba->max_vpi);
  2170. rc = lpfc_sli_ring_map(phba);
  2171. if (rc)
  2172. goto lpfc_sli_hba_setup_error;
  2173. /* Init HBQs */
  2174. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2175. rc = lpfc_sli_hbq_setup(phba);
  2176. if (rc)
  2177. goto lpfc_sli_hba_setup_error;
  2178. }
  2179. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  2180. rc = lpfc_config_port_post(phba);
  2181. if (rc)
  2182. goto lpfc_sli_hba_setup_error;
  2183. return rc;
  2184. lpfc_sli_hba_setup_error:
  2185. phba->link_state = LPFC_HBA_ERROR;
  2186. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2187. "0445 Firmware initialization failed\n");
  2188. return rc;
  2189. }
  2190. /*! lpfc_mbox_timeout
  2191. *
  2192. * \pre
  2193. * \post
  2194. * \param hba Pointer to per struct lpfc_hba structure
  2195. * \param l1 Pointer to the driver's mailbox queue.
  2196. * \return
  2197. * void
  2198. *
  2199. * \b Description:
  2200. *
  2201. * This routine handles mailbox timeout events at timer interrupt context.
  2202. */
  2203. void
  2204. lpfc_mbox_timeout(unsigned long ptr)
  2205. {
  2206. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  2207. unsigned long iflag;
  2208. uint32_t tmo_posted;
  2209. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  2210. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  2211. if (!tmo_posted)
  2212. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  2213. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  2214. if (!tmo_posted) {
  2215. spin_lock_irqsave(&phba->hbalock, iflag);
  2216. if (phba->work_wait)
  2217. lpfc_worker_wake_up(phba);
  2218. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2219. }
  2220. }
  2221. void
  2222. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  2223. {
  2224. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  2225. MAILBOX_t *mb = &pmbox->mb;
  2226. struct lpfc_sli *psli = &phba->sli;
  2227. struct lpfc_sli_ring *pring;
  2228. if (!(phba->pport->work_port_events & WORKER_MBOX_TMO)) {
  2229. return;
  2230. }
  2231. /* Mbox cmd <mbxCommand> timeout */
  2232. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2233. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  2234. mb->mbxCommand,
  2235. phba->pport->port_state,
  2236. phba->sli.sli_flag,
  2237. phba->sli.mbox_active);
  2238. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  2239. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  2240. * it to fail all oustanding SCSI IO.
  2241. */
  2242. spin_lock_irq(&phba->pport->work_port_lock);
  2243. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  2244. spin_unlock_irq(&phba->pport->work_port_lock);
  2245. spin_lock_irq(&phba->hbalock);
  2246. phba->link_state = LPFC_LINK_UNKNOWN;
  2247. phba->pport->fc_flag |= FC_ESTABLISH_LINK;
  2248. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  2249. spin_unlock_irq(&phba->hbalock);
  2250. pring = &psli->ring[psli->fcp_ring];
  2251. lpfc_sli_abort_iocb_ring(phba, pring);
  2252. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2253. "0345 Resetting board due to mailbox timeout\n");
  2254. /*
  2255. * lpfc_offline calls lpfc_sli_hba_down which will clean up
  2256. * on oustanding mailbox commands.
  2257. */
  2258. /* If resets are disabled then set error state and return. */
  2259. if (!phba->cfg_enable_hba_reset) {
  2260. phba->link_state = LPFC_HBA_ERROR;
  2261. return;
  2262. }
  2263. lpfc_offline_prep(phba);
  2264. lpfc_offline(phba);
  2265. lpfc_sli_brdrestart(phba);
  2266. if (lpfc_online(phba) == 0) /* Initialize the HBA */
  2267. mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
  2268. lpfc_unblock_mgmt_io(phba);
  2269. return;
  2270. }
  2271. int
  2272. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  2273. {
  2274. MAILBOX_t *mb;
  2275. struct lpfc_sli *psli = &phba->sli;
  2276. uint32_t status, evtctr;
  2277. uint32_t ha_copy;
  2278. int i;
  2279. unsigned long timeout;
  2280. unsigned long drvr_flag = 0;
  2281. volatile uint32_t word0, ldata;
  2282. void __iomem *to_slim;
  2283. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  2284. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  2285. if(!pmbox->vport) {
  2286. lpfc_printf_log(phba, KERN_ERR,
  2287. LOG_MBOX | LOG_VPORT,
  2288. "1806 Mbox x%x failed. No vport\n",
  2289. pmbox->mb.mbxCommand);
  2290. dump_stack();
  2291. return MBX_NOT_FINISHED;
  2292. }
  2293. }
  2294. /* If the PCI channel is in offline state, do not post mbox. */
  2295. if (unlikely(pci_channel_offline(phba->pcidev)))
  2296. return MBX_NOT_FINISHED;
  2297. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  2298. psli = &phba->sli;
  2299. mb = &pmbox->mb;
  2300. status = MBX_SUCCESS;
  2301. if (phba->link_state == LPFC_HBA_ERROR) {
  2302. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  2303. /* Mbox command <mbxCommand> cannot issue */
  2304. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag)
  2305. return MBX_NOT_FINISHED;
  2306. }
  2307. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
  2308. !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
  2309. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  2310. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag)
  2311. return MBX_NOT_FINISHED;
  2312. }
  2313. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  2314. /* Polling for a mbox command when another one is already active
  2315. * is not allowed in SLI. Also, the driver must have established
  2316. * SLI2 mode to queue and process multiple mbox commands.
  2317. */
  2318. if (flag & MBX_POLL) {
  2319. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  2320. /* Mbox command <mbxCommand> cannot issue */
  2321. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  2322. return MBX_NOT_FINISHED;
  2323. }
  2324. if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
  2325. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  2326. /* Mbox command <mbxCommand> cannot issue */
  2327. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  2328. return MBX_NOT_FINISHED;
  2329. }
  2330. /* Another mailbox command is still being processed, queue this
  2331. * command to be processed later.
  2332. */
  2333. lpfc_mbox_put(phba, pmbox);
  2334. /* Mbox cmd issue - BUSY */
  2335. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2336. "(%d):0308 Mbox cmd issue - BUSY Data: "
  2337. "x%x x%x x%x x%x\n",
  2338. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  2339. mb->mbxCommand, phba->pport->port_state,
  2340. psli->sli_flag, flag);
  2341. psli->slistat.mbox_busy++;
  2342. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  2343. if (pmbox->vport) {
  2344. lpfc_debugfs_disc_trc(pmbox->vport,
  2345. LPFC_DISC_TRC_MBOX_VPORT,
  2346. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  2347. (uint32_t)mb->mbxCommand,
  2348. mb->un.varWords[0], mb->un.varWords[1]);
  2349. }
  2350. else {
  2351. lpfc_debugfs_disc_trc(phba->pport,
  2352. LPFC_DISC_TRC_MBOX,
  2353. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  2354. (uint32_t)mb->mbxCommand,
  2355. mb->un.varWords[0], mb->un.varWords[1]);
  2356. }
  2357. return MBX_BUSY;
  2358. }
  2359. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  2360. /* If we are not polling, we MUST be in SLI2 mode */
  2361. if (flag != MBX_POLL) {
  2362. if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
  2363. (mb->mbxCommand != MBX_KILL_BOARD)) {
  2364. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  2365. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  2366. /* Mbox command <mbxCommand> cannot issue */
  2367. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  2368. return MBX_NOT_FINISHED;
  2369. }
  2370. /* timeout active mbox command */
  2371. mod_timer(&psli->mbox_tmo, (jiffies +
  2372. (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
  2373. }
  2374. /* Mailbox cmd <cmd> issue */
  2375. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2376. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  2377. "x%x\n",
  2378. pmbox->vport ? pmbox->vport->vpi : 0,
  2379. mb->mbxCommand, phba->pport->port_state,
  2380. psli->sli_flag, flag);
  2381. if (mb->mbxCommand != MBX_HEARTBEAT) {
  2382. if (pmbox->vport) {
  2383. lpfc_debugfs_disc_trc(pmbox->vport,
  2384. LPFC_DISC_TRC_MBOX_VPORT,
  2385. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  2386. (uint32_t)mb->mbxCommand,
  2387. mb->un.varWords[0], mb->un.varWords[1]);
  2388. }
  2389. else {
  2390. lpfc_debugfs_disc_trc(phba->pport,
  2391. LPFC_DISC_TRC_MBOX,
  2392. "MBOX Send: cmd:x%x mb:x%x x%x",
  2393. (uint32_t)mb->mbxCommand,
  2394. mb->un.varWords[0], mb->un.varWords[1]);
  2395. }
  2396. }
  2397. psli->slistat.mbox_cmd++;
  2398. evtctr = psli->slistat.mbox_event;
  2399. /* next set own bit for the adapter and copy over command word */
  2400. mb->mbxOwner = OWN_CHIP;
  2401. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  2402. /* First copy command data to host SLIM area */
  2403. lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
  2404. } else {
  2405. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  2406. /* copy command data into host mbox for cmpl */
  2407. lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
  2408. MAILBOX_CMD_SIZE);
  2409. }
  2410. /* First copy mbox command data to HBA SLIM, skip past first
  2411. word */
  2412. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  2413. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  2414. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  2415. /* Next copy over first word, with mbxOwner set */
  2416. ldata = *((volatile uint32_t *)mb);
  2417. to_slim = phba->MBslimaddr;
  2418. writel(ldata, to_slim);
  2419. readl(to_slim); /* flush */
  2420. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  2421. /* switch over to host mailbox */
  2422. psli->sli_flag |= LPFC_SLI2_ACTIVE;
  2423. }
  2424. }
  2425. wmb();
  2426. switch (flag) {
  2427. case MBX_NOWAIT:
  2428. /* Set up reference to mailbox command */
  2429. psli->mbox_active = pmbox;
  2430. /* Interrupt board to do it */
  2431. writel(CA_MBATT, phba->CAregaddr);
  2432. readl(phba->CAregaddr); /* flush */
  2433. /* Don't wait for it to finish, just return */
  2434. break;
  2435. case MBX_POLL:
  2436. /* Set up null reference to mailbox command */
  2437. psli->mbox_active = NULL;
  2438. /* Interrupt board to do it */
  2439. writel(CA_MBATT, phba->CAregaddr);
  2440. readl(phba->CAregaddr); /* flush */
  2441. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  2442. /* First read mbox status word */
  2443. word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
  2444. word0 = le32_to_cpu(word0);
  2445. } else {
  2446. /* First read mbox status word */
  2447. word0 = readl(phba->MBslimaddr);
  2448. }
  2449. /* Read the HBA Host Attention Register */
  2450. ha_copy = readl(phba->HAregaddr);
  2451. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  2452. mb->mbxCommand) *
  2453. 1000) + jiffies;
  2454. i = 0;
  2455. /* Wait for command to complete */
  2456. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  2457. (!(ha_copy & HA_MBATT) &&
  2458. (phba->link_state > LPFC_WARM_START))) {
  2459. if (time_after(jiffies, timeout)) {
  2460. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  2461. spin_unlock_irqrestore(&phba->hbalock,
  2462. drvr_flag);
  2463. return MBX_NOT_FINISHED;
  2464. }
  2465. /* Check if we took a mbox interrupt while we were
  2466. polling */
  2467. if (((word0 & OWN_CHIP) != OWN_CHIP)
  2468. && (evtctr != psli->slistat.mbox_event))
  2469. break;
  2470. if (i++ > 10) {
  2471. spin_unlock_irqrestore(&phba->hbalock,
  2472. drvr_flag);
  2473. msleep(1);
  2474. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  2475. }
  2476. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  2477. /* First copy command data */
  2478. word0 = *((volatile uint32_t *)
  2479. &phba->slim2p->mbx);
  2480. word0 = le32_to_cpu(word0);
  2481. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  2482. MAILBOX_t *slimmb;
  2483. volatile uint32_t slimword0;
  2484. /* Check real SLIM for any errors */
  2485. slimword0 = readl(phba->MBslimaddr);
  2486. slimmb = (MAILBOX_t *) & slimword0;
  2487. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  2488. && slimmb->mbxStatus) {
  2489. psli->sli_flag &=
  2490. ~LPFC_SLI2_ACTIVE;
  2491. word0 = slimword0;
  2492. }
  2493. }
  2494. } else {
  2495. /* First copy command data */
  2496. word0 = readl(phba->MBslimaddr);
  2497. }
  2498. /* Read the HBA Host Attention Register */
  2499. ha_copy = readl(phba->HAregaddr);
  2500. }
  2501. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  2502. /* copy results back to user */
  2503. lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
  2504. MAILBOX_CMD_SIZE);
  2505. } else {
  2506. /* First copy command data */
  2507. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  2508. MAILBOX_CMD_SIZE);
  2509. if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
  2510. pmbox->context2) {
  2511. lpfc_memcpy_from_slim((void *)pmbox->context2,
  2512. phba->MBslimaddr + DMP_RSP_OFFSET,
  2513. mb->un.varDmp.word_cnt);
  2514. }
  2515. }
  2516. writel(HA_MBATT, phba->HAregaddr);
  2517. readl(phba->HAregaddr); /* flush */
  2518. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  2519. status = mb->mbxStatus;
  2520. }
  2521. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  2522. return status;
  2523. }
  2524. /*
  2525. * Caller needs to hold lock.
  2526. */
  2527. static void
  2528. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2529. struct lpfc_iocbq *piocb)
  2530. {
  2531. /* Insert the caller's iocb in the txq tail for later processing. */
  2532. list_add_tail(&piocb->list, &pring->txq);
  2533. pring->txq_cnt++;
  2534. }
  2535. static struct lpfc_iocbq *
  2536. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2537. struct lpfc_iocbq **piocb)
  2538. {
  2539. struct lpfc_iocbq * nextiocb;
  2540. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  2541. if (!nextiocb) {
  2542. nextiocb = *piocb;
  2543. *piocb = NULL;
  2544. }
  2545. return nextiocb;
  2546. }
  2547. /*
  2548. * Lockless version of lpfc_sli_issue_iocb.
  2549. */
  2550. static int
  2551. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2552. struct lpfc_iocbq *piocb, uint32_t flag)
  2553. {
  2554. struct lpfc_iocbq *nextiocb;
  2555. IOCB_t *iocb;
  2556. if (piocb->iocb_cmpl && (!piocb->vport) &&
  2557. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  2558. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  2559. lpfc_printf_log(phba, KERN_ERR,
  2560. LOG_SLI | LOG_VPORT,
  2561. "1807 IOCB x%x failed. No vport\n",
  2562. piocb->iocb.ulpCommand);
  2563. dump_stack();
  2564. return IOCB_ERROR;
  2565. }
  2566. /* If the PCI channel is in offline state, do not post iocbs. */
  2567. if (unlikely(pci_channel_offline(phba->pcidev)))
  2568. return IOCB_ERROR;
  2569. /*
  2570. * We should never get an IOCB if we are in a < LINK_DOWN state
  2571. */
  2572. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  2573. return IOCB_ERROR;
  2574. /*
  2575. * Check to see if we are blocking IOCB processing because of a
  2576. * outstanding event.
  2577. */
  2578. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  2579. goto iocb_busy;
  2580. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  2581. /*
  2582. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  2583. * can be issued if the link is not up.
  2584. */
  2585. switch (piocb->iocb.ulpCommand) {
  2586. case CMD_QUE_RING_BUF_CN:
  2587. case CMD_QUE_RING_BUF64_CN:
  2588. /*
  2589. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  2590. * completion, iocb_cmpl MUST be 0.
  2591. */
  2592. if (piocb->iocb_cmpl)
  2593. piocb->iocb_cmpl = NULL;
  2594. /*FALLTHROUGH*/
  2595. case CMD_CREATE_XRI_CR:
  2596. case CMD_CLOSE_XRI_CN:
  2597. case CMD_CLOSE_XRI_CX:
  2598. break;
  2599. default:
  2600. goto iocb_busy;
  2601. }
  2602. /*
  2603. * For FCP commands, we must be in a state where we can process link
  2604. * attention events.
  2605. */
  2606. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  2607. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  2608. goto iocb_busy;
  2609. }
  2610. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  2611. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  2612. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  2613. if (iocb)
  2614. lpfc_sli_update_ring(phba, pring);
  2615. else
  2616. lpfc_sli_update_full_ring(phba, pring);
  2617. if (!piocb)
  2618. return IOCB_SUCCESS;
  2619. goto out_busy;
  2620. iocb_busy:
  2621. pring->stats.iocb_cmd_delay++;
  2622. out_busy:
  2623. if (!(flag & SLI_IOCB_RET_IOCB)) {
  2624. __lpfc_sli_ringtx_put(phba, pring, piocb);
  2625. return IOCB_SUCCESS;
  2626. }
  2627. return IOCB_BUSY;
  2628. }
  2629. int
  2630. lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2631. struct lpfc_iocbq *piocb, uint32_t flag)
  2632. {
  2633. unsigned long iflags;
  2634. int rc;
  2635. spin_lock_irqsave(&phba->hbalock, iflags);
  2636. rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
  2637. spin_unlock_irqrestore(&phba->hbalock, iflags);
  2638. return rc;
  2639. }
  2640. static int
  2641. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  2642. {
  2643. struct lpfc_sli *psli;
  2644. struct lpfc_sli_ring *pring;
  2645. psli = &phba->sli;
  2646. /* Adjust cmd/rsp ring iocb entries more evenly */
  2647. /* Take some away from the FCP ring */
  2648. pring = &psli->ring[psli->fcp_ring];
  2649. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  2650. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  2651. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  2652. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  2653. /* and give them to the extra ring */
  2654. pring = &psli->ring[psli->extra_ring];
  2655. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  2656. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  2657. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  2658. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  2659. /* Setup default profile for this ring */
  2660. pring->iotag_max = 4096;
  2661. pring->num_mask = 1;
  2662. pring->prt[0].profile = 0; /* Mask 0 */
  2663. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  2664. pring->prt[0].type = phba->cfg_multi_ring_type;
  2665. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  2666. return 0;
  2667. }
  2668. static void
  2669. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  2670. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  2671. {
  2672. IOCB_t *icmd;
  2673. uint16_t evt_code;
  2674. uint16_t temp;
  2675. struct temp_event temp_event_data;
  2676. struct Scsi_Host *shost;
  2677. icmd = &iocbq->iocb;
  2678. evt_code = icmd->un.asyncstat.evt_code;
  2679. temp = icmd->ulpContext;
  2680. if ((evt_code != ASYNC_TEMP_WARN) &&
  2681. (evt_code != ASYNC_TEMP_SAFE)) {
  2682. lpfc_printf_log(phba,
  2683. KERN_ERR,
  2684. LOG_SLI,
  2685. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  2686. " evt_code 0x%x\n",
  2687. pring->ringno,
  2688. icmd->un.asyncstat.evt_code);
  2689. return;
  2690. }
  2691. temp_event_data.data = (uint32_t)temp;
  2692. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  2693. if (evt_code == ASYNC_TEMP_WARN) {
  2694. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  2695. lpfc_printf_log(phba,
  2696. KERN_ERR,
  2697. LOG_TEMP,
  2698. "0347 Adapter is very hot, please take "
  2699. "corrective action. temperature : %d Celsius\n",
  2700. temp);
  2701. }
  2702. if (evt_code == ASYNC_TEMP_SAFE) {
  2703. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  2704. lpfc_printf_log(phba,
  2705. KERN_ERR,
  2706. LOG_TEMP,
  2707. "0340 Adapter temperature is OK now. "
  2708. "temperature : %d Celsius\n",
  2709. temp);
  2710. }
  2711. /* Send temperature change event to applications */
  2712. shost = lpfc_shost_from_vport(phba->pport);
  2713. fc_host_post_vendor_event(shost, fc_get_event_number(),
  2714. sizeof(temp_event_data), (char *) &temp_event_data,
  2715. SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
  2716. }
  2717. int
  2718. lpfc_sli_setup(struct lpfc_hba *phba)
  2719. {
  2720. int i, totiocbsize = 0;
  2721. struct lpfc_sli *psli = &phba->sli;
  2722. struct lpfc_sli_ring *pring;
  2723. psli->num_rings = MAX_CONFIGURED_RINGS;
  2724. psli->sli_flag = 0;
  2725. psli->fcp_ring = LPFC_FCP_RING;
  2726. psli->next_ring = LPFC_FCP_NEXT_RING;
  2727. psli->extra_ring = LPFC_EXTRA_RING;
  2728. psli->iocbq_lookup = NULL;
  2729. psli->iocbq_lookup_len = 0;
  2730. psli->last_iotag = 0;
  2731. for (i = 0; i < psli->num_rings; i++) {
  2732. pring = &psli->ring[i];
  2733. switch (i) {
  2734. case LPFC_FCP_RING: /* ring 0 - FCP */
  2735. /* numCiocb and numRiocb are used in config_port */
  2736. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  2737. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  2738. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  2739. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  2740. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  2741. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  2742. pring->sizeCiocb = (phba->sli_rev == 3) ?
  2743. SLI3_IOCB_CMD_SIZE :
  2744. SLI2_IOCB_CMD_SIZE;
  2745. pring->sizeRiocb = (phba->sli_rev == 3) ?
  2746. SLI3_IOCB_RSP_SIZE :
  2747. SLI2_IOCB_RSP_SIZE;
  2748. pring->iotag_ctr = 0;
  2749. pring->iotag_max =
  2750. (phba->cfg_hba_queue_depth * 2);
  2751. pring->fast_iotag = pring->iotag_max;
  2752. pring->num_mask = 0;
  2753. break;
  2754. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  2755. /* numCiocb and numRiocb are used in config_port */
  2756. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  2757. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  2758. pring->sizeCiocb = (phba->sli_rev == 3) ?
  2759. SLI3_IOCB_CMD_SIZE :
  2760. SLI2_IOCB_CMD_SIZE;
  2761. pring->sizeRiocb = (phba->sli_rev == 3) ?
  2762. SLI3_IOCB_RSP_SIZE :
  2763. SLI2_IOCB_RSP_SIZE;
  2764. pring->iotag_max = phba->cfg_hba_queue_depth;
  2765. pring->num_mask = 0;
  2766. break;
  2767. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  2768. /* numCiocb and numRiocb are used in config_port */
  2769. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  2770. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  2771. pring->sizeCiocb = (phba->sli_rev == 3) ?
  2772. SLI3_IOCB_CMD_SIZE :
  2773. SLI2_IOCB_CMD_SIZE;
  2774. pring->sizeRiocb = (phba->sli_rev == 3) ?
  2775. SLI3_IOCB_RSP_SIZE :
  2776. SLI2_IOCB_RSP_SIZE;
  2777. pring->fast_iotag = 0;
  2778. pring->iotag_ctr = 0;
  2779. pring->iotag_max = 4096;
  2780. pring->lpfc_sli_rcv_async_status =
  2781. lpfc_sli_async_event_handler;
  2782. pring->num_mask = 4;
  2783. pring->prt[0].profile = 0; /* Mask 0 */
  2784. pring->prt[0].rctl = FC_ELS_REQ;
  2785. pring->prt[0].type = FC_ELS_DATA;
  2786. pring->prt[0].lpfc_sli_rcv_unsol_event =
  2787. lpfc_els_unsol_event;
  2788. pring->prt[1].profile = 0; /* Mask 1 */
  2789. pring->prt[1].rctl = FC_ELS_RSP;
  2790. pring->prt[1].type = FC_ELS_DATA;
  2791. pring->prt[1].lpfc_sli_rcv_unsol_event =
  2792. lpfc_els_unsol_event;
  2793. pring->prt[2].profile = 0; /* Mask 2 */
  2794. /* NameServer Inquiry */
  2795. pring->prt[2].rctl = FC_UNSOL_CTL;
  2796. /* NameServer */
  2797. pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
  2798. pring->prt[2].lpfc_sli_rcv_unsol_event =
  2799. lpfc_ct_unsol_event;
  2800. pring->prt[3].profile = 0; /* Mask 3 */
  2801. /* NameServer response */
  2802. pring->prt[3].rctl = FC_SOL_CTL;
  2803. /* NameServer */
  2804. pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
  2805. pring->prt[3].lpfc_sli_rcv_unsol_event =
  2806. lpfc_ct_unsol_event;
  2807. break;
  2808. }
  2809. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  2810. (pring->numRiocb * pring->sizeRiocb);
  2811. }
  2812. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  2813. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  2814. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  2815. "SLI2 SLIM Data: x%x x%lx\n",
  2816. phba->brd_no, totiocbsize,
  2817. (unsigned long) MAX_SLIM_IOCB_SIZE);
  2818. }
  2819. if (phba->cfg_multi_ring_support == 2)
  2820. lpfc_extra_ring_setup(phba);
  2821. return 0;
  2822. }
  2823. int
  2824. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  2825. {
  2826. struct lpfc_sli *psli;
  2827. struct lpfc_sli_ring *pring;
  2828. int i;
  2829. psli = &phba->sli;
  2830. spin_lock_irq(&phba->hbalock);
  2831. INIT_LIST_HEAD(&psli->mboxq);
  2832. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  2833. /* Initialize list headers for txq and txcmplq as double linked lists */
  2834. for (i = 0; i < psli->num_rings; i++) {
  2835. pring = &psli->ring[i];
  2836. pring->ringno = i;
  2837. pring->next_cmdidx = 0;
  2838. pring->local_getidx = 0;
  2839. pring->cmdidx = 0;
  2840. INIT_LIST_HEAD(&pring->txq);
  2841. INIT_LIST_HEAD(&pring->txcmplq);
  2842. INIT_LIST_HEAD(&pring->iocb_continueq);
  2843. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  2844. INIT_LIST_HEAD(&pring->postbufq);
  2845. }
  2846. spin_unlock_irq(&phba->hbalock);
  2847. return 1;
  2848. }
  2849. int
  2850. lpfc_sli_host_down(struct lpfc_vport *vport)
  2851. {
  2852. LIST_HEAD(completions);
  2853. struct lpfc_hba *phba = vport->phba;
  2854. struct lpfc_sli *psli = &phba->sli;
  2855. struct lpfc_sli_ring *pring;
  2856. struct lpfc_iocbq *iocb, *next_iocb;
  2857. int i;
  2858. unsigned long flags = 0;
  2859. uint16_t prev_pring_flag;
  2860. lpfc_cleanup_discovery_resources(vport);
  2861. spin_lock_irqsave(&phba->hbalock, flags);
  2862. for (i = 0; i < psli->num_rings; i++) {
  2863. pring = &psli->ring[i];
  2864. prev_pring_flag = pring->flag;
  2865. if (pring->ringno == LPFC_ELS_RING) /* Only slow rings */
  2866. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  2867. /*
  2868. * Error everything on the txq since these iocbs have not been
  2869. * given to the FW yet.
  2870. */
  2871. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  2872. if (iocb->vport != vport)
  2873. continue;
  2874. list_move_tail(&iocb->list, &completions);
  2875. pring->txq_cnt--;
  2876. }
  2877. /* Next issue ABTS for everything on the txcmplq */
  2878. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  2879. list) {
  2880. if (iocb->vport != vport)
  2881. continue;
  2882. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  2883. }
  2884. pring->flag = prev_pring_flag;
  2885. }
  2886. spin_unlock_irqrestore(&phba->hbalock, flags);
  2887. while (!list_empty(&completions)) {
  2888. list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
  2889. if (!iocb->iocb_cmpl)
  2890. lpfc_sli_release_iocbq(phba, iocb);
  2891. else {
  2892. iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  2893. iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
  2894. (iocb->iocb_cmpl) (phba, iocb, iocb);
  2895. }
  2896. }
  2897. return 1;
  2898. }
  2899. int
  2900. lpfc_sli_hba_down(struct lpfc_hba *phba)
  2901. {
  2902. LIST_HEAD(completions);
  2903. struct lpfc_sli *psli = &phba->sli;
  2904. struct lpfc_sli_ring *pring;
  2905. struct lpfc_dmabuf *buf_ptr;
  2906. LPFC_MBOXQ_t *pmb;
  2907. struct lpfc_iocbq *iocb;
  2908. IOCB_t *cmd = NULL;
  2909. int i;
  2910. unsigned long flags = 0;
  2911. lpfc_hba_down_prep(phba);
  2912. lpfc_fabric_abort_hba(phba);
  2913. spin_lock_irqsave(&phba->hbalock, flags);
  2914. for (i = 0; i < psli->num_rings; i++) {
  2915. pring = &psli->ring[i];
  2916. if (pring->ringno == LPFC_ELS_RING) /* Only slow rings */
  2917. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  2918. /*
  2919. * Error everything on the txq since these iocbs have not been
  2920. * given to the FW yet.
  2921. */
  2922. list_splice_init(&pring->txq, &completions);
  2923. pring->txq_cnt = 0;
  2924. }
  2925. spin_unlock_irqrestore(&phba->hbalock, flags);
  2926. while (!list_empty(&completions)) {
  2927. list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
  2928. cmd = &iocb->iocb;
  2929. if (!iocb->iocb_cmpl)
  2930. lpfc_sli_release_iocbq(phba, iocb);
  2931. else {
  2932. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  2933. cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
  2934. (iocb->iocb_cmpl) (phba, iocb, iocb);
  2935. }
  2936. }
  2937. spin_lock_irqsave(&phba->hbalock, flags);
  2938. list_splice_init(&phba->elsbuf, &completions);
  2939. phba->elsbuf_cnt = 0;
  2940. phba->elsbuf_prev_cnt = 0;
  2941. spin_unlock_irqrestore(&phba->hbalock, flags);
  2942. while (!list_empty(&completions)) {
  2943. list_remove_head(&completions, buf_ptr,
  2944. struct lpfc_dmabuf, list);
  2945. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  2946. kfree(buf_ptr);
  2947. }
  2948. /* Return any active mbox cmds */
  2949. del_timer_sync(&psli->mbox_tmo);
  2950. spin_lock_irqsave(&phba->hbalock, flags);
  2951. spin_lock(&phba->pport->work_port_lock);
  2952. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  2953. spin_unlock(&phba->pport->work_port_lock);
  2954. if (psli->mbox_active) {
  2955. list_add_tail(&psli->mbox_active->list, &completions);
  2956. psli->mbox_active = NULL;
  2957. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  2958. }
  2959. /* Return any pending or completed mbox cmds */
  2960. list_splice_init(&phba->sli.mboxq, &completions);
  2961. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  2962. INIT_LIST_HEAD(&psli->mboxq);
  2963. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  2964. spin_unlock_irqrestore(&phba->hbalock, flags);
  2965. while (!list_empty(&completions)) {
  2966. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  2967. pmb->mb.mbxStatus = MBX_NOT_FINISHED;
  2968. if (pmb->mbox_cmpl) {
  2969. pmb->mbox_cmpl(phba,pmb);
  2970. }
  2971. }
  2972. return 1;
  2973. }
  2974. void
  2975. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  2976. {
  2977. uint32_t *src = srcp;
  2978. uint32_t *dest = destp;
  2979. uint32_t ldata;
  2980. int i;
  2981. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  2982. ldata = *src;
  2983. ldata = le32_to_cpu(ldata);
  2984. *dest = ldata;
  2985. src++;
  2986. dest++;
  2987. }
  2988. }
  2989. int
  2990. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2991. struct lpfc_dmabuf *mp)
  2992. {
  2993. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  2994. later */
  2995. spin_lock_irq(&phba->hbalock);
  2996. list_add_tail(&mp->list, &pring->postbufq);
  2997. pring->postbufq_cnt++;
  2998. spin_unlock_irq(&phba->hbalock);
  2999. return 0;
  3000. }
  3001. uint32_t
  3002. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  3003. {
  3004. spin_lock_irq(&phba->hbalock);
  3005. phba->buffer_tag_count++;
  3006. /*
  3007. * Always set the QUE_BUFTAG_BIT to distiguish between
  3008. * a tag assigned by HBQ.
  3009. */
  3010. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  3011. spin_unlock_irq(&phba->hbalock);
  3012. return phba->buffer_tag_count;
  3013. }
  3014. struct lpfc_dmabuf *
  3015. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3016. uint32_t tag)
  3017. {
  3018. struct lpfc_dmabuf *mp, *next_mp;
  3019. struct list_head *slp = &pring->postbufq;
  3020. /* Search postbufq, from the begining, looking for a match on tag */
  3021. spin_lock_irq(&phba->hbalock);
  3022. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  3023. if (mp->buffer_tag == tag) {
  3024. list_del_init(&mp->list);
  3025. pring->postbufq_cnt--;
  3026. spin_unlock_irq(&phba->hbalock);
  3027. return mp;
  3028. }
  3029. }
  3030. spin_unlock_irq(&phba->hbalock);
  3031. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3032. "0410 Cannot find virtual addr for buffer tag on "
  3033. "ring %d Data x%lx x%p x%p x%x\n",
  3034. pring->ringno, (unsigned long) tag,
  3035. slp->next, slp->prev, pring->postbufq_cnt);
  3036. return NULL;
  3037. }
  3038. struct lpfc_dmabuf *
  3039. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3040. dma_addr_t phys)
  3041. {
  3042. struct lpfc_dmabuf *mp, *next_mp;
  3043. struct list_head *slp = &pring->postbufq;
  3044. /* Search postbufq, from the begining, looking for a match on phys */
  3045. spin_lock_irq(&phba->hbalock);
  3046. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  3047. if (mp->phys == phys) {
  3048. list_del_init(&mp->list);
  3049. pring->postbufq_cnt--;
  3050. spin_unlock_irq(&phba->hbalock);
  3051. return mp;
  3052. }
  3053. }
  3054. spin_unlock_irq(&phba->hbalock);
  3055. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3056. "0410 Cannot find virtual addr for mapped buf on "
  3057. "ring %d Data x%llx x%p x%p x%x\n",
  3058. pring->ringno, (unsigned long long)phys,
  3059. slp->next, slp->prev, pring->postbufq_cnt);
  3060. return NULL;
  3061. }
  3062. static void
  3063. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  3064. struct lpfc_iocbq *rspiocb)
  3065. {
  3066. IOCB_t *irsp = &rspiocb->iocb;
  3067. uint16_t abort_iotag, abort_context;
  3068. struct lpfc_iocbq *abort_iocb;
  3069. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  3070. abort_iocb = NULL;
  3071. if (irsp->ulpStatus) {
  3072. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  3073. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  3074. spin_lock_irq(&phba->hbalock);
  3075. if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
  3076. abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
  3077. lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
  3078. "0327 Cannot abort els iocb %p "
  3079. "with tag %x context %x, abort status %x, "
  3080. "abort code %x\n",
  3081. abort_iocb, abort_iotag, abort_context,
  3082. irsp->ulpStatus, irsp->un.ulpWord[4]);
  3083. /*
  3084. * make sure we have the right iocbq before taking it
  3085. * off the txcmplq and try to call completion routine.
  3086. */
  3087. if (!abort_iocb ||
  3088. abort_iocb->iocb.ulpContext != abort_context ||
  3089. (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
  3090. spin_unlock_irq(&phba->hbalock);
  3091. else {
  3092. list_del_init(&abort_iocb->list);
  3093. pring->txcmplq_cnt--;
  3094. spin_unlock_irq(&phba->hbalock);
  3095. /* Firmware could still be in progress of DMAing
  3096. * payload, so don't free data buffer till after
  3097. * a hbeat.
  3098. */
  3099. abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
  3100. abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  3101. abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  3102. abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
  3103. (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
  3104. }
  3105. }
  3106. lpfc_sli_release_iocbq(phba, cmdiocb);
  3107. return;
  3108. }
  3109. static void
  3110. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  3111. struct lpfc_iocbq *rspiocb)
  3112. {
  3113. IOCB_t *irsp = &rspiocb->iocb;
  3114. /* ELS cmd tag <ulpIoTag> completes */
  3115. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  3116. "0133 Ignoring ELS cmd tag x%x completion Data: "
  3117. "x%x x%x x%x\n",
  3118. irsp->ulpIoTag, irsp->ulpStatus,
  3119. irsp->un.ulpWord[4], irsp->ulpTimeout);
  3120. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  3121. lpfc_ct_free_iocb(phba, cmdiocb);
  3122. else
  3123. lpfc_els_free_iocb(phba, cmdiocb);
  3124. return;
  3125. }
  3126. int
  3127. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3128. struct lpfc_iocbq *cmdiocb)
  3129. {
  3130. struct lpfc_vport *vport = cmdiocb->vport;
  3131. struct lpfc_iocbq *abtsiocbp;
  3132. IOCB_t *icmd = NULL;
  3133. IOCB_t *iabt = NULL;
  3134. int retval = IOCB_ERROR;
  3135. /*
  3136. * There are certain command types we don't want to abort. And we
  3137. * don't want to abort commands that are already in the process of
  3138. * being aborted.
  3139. */
  3140. icmd = &cmdiocb->iocb;
  3141. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  3142. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  3143. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  3144. return 0;
  3145. /* If we're unloading, don't abort iocb on the ELS ring, but change the
  3146. * callback so that nothing happens when it finishes.
  3147. */
  3148. if ((vport->load_flag & FC_UNLOADING) &&
  3149. (pring->ringno == LPFC_ELS_RING)) {
  3150. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  3151. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  3152. else
  3153. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  3154. goto abort_iotag_exit;
  3155. }
  3156. /* issue ABTS for this IOCB based on iotag */
  3157. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  3158. if (abtsiocbp == NULL)
  3159. return 0;
  3160. /* This signals the response to set the correct status
  3161. * before calling the completion handler.
  3162. */
  3163. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  3164. iabt = &abtsiocbp->iocb;
  3165. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  3166. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  3167. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  3168. iabt->ulpLe = 1;
  3169. iabt->ulpClass = icmd->ulpClass;
  3170. if (phba->link_state >= LPFC_LINK_UP)
  3171. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  3172. else
  3173. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  3174. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  3175. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  3176. "0339 Abort xri x%x, original iotag x%x, "
  3177. "abort cmd iotag x%x\n",
  3178. iabt->un.acxri.abortContextTag,
  3179. iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
  3180. retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
  3181. abort_iotag_exit:
  3182. /*
  3183. * Caller to this routine should check for IOCB_ERROR
  3184. * and handle it properly. This routine no longer removes
  3185. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  3186. */
  3187. return retval;
  3188. }
  3189. static int
  3190. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  3191. uint16_t tgt_id, uint64_t lun_id,
  3192. lpfc_ctx_cmd ctx_cmd)
  3193. {
  3194. struct lpfc_scsi_buf *lpfc_cmd;
  3195. struct scsi_cmnd *cmnd;
  3196. int rc = 1;
  3197. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  3198. return rc;
  3199. if (iocbq->vport != vport)
  3200. return rc;
  3201. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  3202. cmnd = lpfc_cmd->pCmd;
  3203. if (cmnd == NULL)
  3204. return rc;
  3205. switch (ctx_cmd) {
  3206. case LPFC_CTX_LUN:
  3207. if ((cmnd->device->id == tgt_id) &&
  3208. (cmnd->device->lun == lun_id))
  3209. rc = 0;
  3210. break;
  3211. case LPFC_CTX_TGT:
  3212. if (cmnd->device->id == tgt_id)
  3213. rc = 0;
  3214. break;
  3215. case LPFC_CTX_HOST:
  3216. rc = 0;
  3217. break;
  3218. default:
  3219. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  3220. __FUNCTION__, ctx_cmd);
  3221. break;
  3222. }
  3223. return rc;
  3224. }
  3225. int
  3226. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  3227. lpfc_ctx_cmd ctx_cmd)
  3228. {
  3229. struct lpfc_hba *phba = vport->phba;
  3230. struct lpfc_iocbq *iocbq;
  3231. int sum, i;
  3232. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  3233. iocbq = phba->sli.iocbq_lookup[i];
  3234. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  3235. ctx_cmd) == 0)
  3236. sum++;
  3237. }
  3238. return sum;
  3239. }
  3240. void
  3241. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  3242. struct lpfc_iocbq *rspiocb)
  3243. {
  3244. lpfc_sli_release_iocbq(phba, cmdiocb);
  3245. return;
  3246. }
  3247. int
  3248. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  3249. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  3250. {
  3251. struct lpfc_hba *phba = vport->phba;
  3252. struct lpfc_iocbq *iocbq;
  3253. struct lpfc_iocbq *abtsiocb;
  3254. IOCB_t *cmd = NULL;
  3255. int errcnt = 0, ret_val = 0;
  3256. int i;
  3257. for (i = 1; i <= phba->sli.last_iotag; i++) {
  3258. iocbq = phba->sli.iocbq_lookup[i];
  3259. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  3260. abort_cmd) != 0)
  3261. continue;
  3262. /* issue ABTS for this IOCB based on iotag */
  3263. abtsiocb = lpfc_sli_get_iocbq(phba);
  3264. if (abtsiocb == NULL) {
  3265. errcnt++;
  3266. continue;
  3267. }
  3268. cmd = &iocbq->iocb;
  3269. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  3270. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  3271. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  3272. abtsiocb->iocb.ulpLe = 1;
  3273. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  3274. abtsiocb->vport = phba->pport;
  3275. if (lpfc_is_link_up(phba))
  3276. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  3277. else
  3278. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  3279. /* Setup callback routine and issue the command. */
  3280. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  3281. ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
  3282. if (ret_val == IOCB_ERROR) {
  3283. lpfc_sli_release_iocbq(phba, abtsiocb);
  3284. errcnt++;
  3285. continue;
  3286. }
  3287. }
  3288. return errcnt;
  3289. }
  3290. static void
  3291. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  3292. struct lpfc_iocbq *cmdiocbq,
  3293. struct lpfc_iocbq *rspiocbq)
  3294. {
  3295. wait_queue_head_t *pdone_q;
  3296. unsigned long iflags;
  3297. spin_lock_irqsave(&phba->hbalock, iflags);
  3298. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  3299. if (cmdiocbq->context2 && rspiocbq)
  3300. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  3301. &rspiocbq->iocb, sizeof(IOCB_t));
  3302. pdone_q = cmdiocbq->context_un.wait_queue;
  3303. if (pdone_q)
  3304. wake_up(pdone_q);
  3305. spin_unlock_irqrestore(&phba->hbalock, iflags);
  3306. return;
  3307. }
  3308. /*
  3309. * Issue the caller's iocb and wait for its completion, but no longer than the
  3310. * caller's timeout. Note that iocb_flags is cleared before the
  3311. * lpfc_sli_issue_call since the wake routine sets a unique value and by
  3312. * definition this is a wait function.
  3313. */
  3314. int
  3315. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  3316. struct lpfc_sli_ring *pring,
  3317. struct lpfc_iocbq *piocb,
  3318. struct lpfc_iocbq *prspiocbq,
  3319. uint32_t timeout)
  3320. {
  3321. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  3322. long timeleft, timeout_req = 0;
  3323. int retval = IOCB_SUCCESS;
  3324. uint32_t creg_val;
  3325. /*
  3326. * If the caller has provided a response iocbq buffer, then context2
  3327. * is NULL or its an error.
  3328. */
  3329. if (prspiocbq) {
  3330. if (piocb->context2)
  3331. return IOCB_ERROR;
  3332. piocb->context2 = prspiocbq;
  3333. }
  3334. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  3335. piocb->context_un.wait_queue = &done_q;
  3336. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  3337. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  3338. creg_val = readl(phba->HCregaddr);
  3339. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  3340. writel(creg_val, phba->HCregaddr);
  3341. readl(phba->HCregaddr); /* flush */
  3342. }
  3343. retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
  3344. if (retval == IOCB_SUCCESS) {
  3345. timeout_req = timeout * HZ;
  3346. timeleft = wait_event_timeout(done_q,
  3347. piocb->iocb_flag & LPFC_IO_WAKE,
  3348. timeout_req);
  3349. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  3350. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3351. "0331 IOCB wake signaled\n");
  3352. } else if (timeleft == 0) {
  3353. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3354. "0338 IOCB wait timeout error - no "
  3355. "wake response Data x%x\n", timeout);
  3356. retval = IOCB_TIMEDOUT;
  3357. } else {
  3358. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3359. "0330 IOCB wake NOT set, "
  3360. "Data x%x x%lx\n",
  3361. timeout, (timeleft / jiffies));
  3362. retval = IOCB_TIMEDOUT;
  3363. }
  3364. } else {
  3365. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3366. ":0332 IOCB wait issue failed, Data x%x\n",
  3367. retval);
  3368. retval = IOCB_ERROR;
  3369. }
  3370. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  3371. creg_val = readl(phba->HCregaddr);
  3372. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  3373. writel(creg_val, phba->HCregaddr);
  3374. readl(phba->HCregaddr); /* flush */
  3375. }
  3376. if (prspiocbq)
  3377. piocb->context2 = NULL;
  3378. piocb->context_un.wait_queue = NULL;
  3379. piocb->iocb_cmpl = NULL;
  3380. return retval;
  3381. }
  3382. int
  3383. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  3384. uint32_t timeout)
  3385. {
  3386. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  3387. int retval;
  3388. unsigned long flag;
  3389. /* The caller must leave context1 empty. */
  3390. if (pmboxq->context1)
  3391. return MBX_NOT_FINISHED;
  3392. /* setup wake call as IOCB callback */
  3393. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  3394. /* setup context field to pass wait_queue pointer to wake function */
  3395. pmboxq->context1 = &done_q;
  3396. /* now issue the command */
  3397. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  3398. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  3399. wait_event_interruptible_timeout(done_q,
  3400. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  3401. timeout * HZ);
  3402. spin_lock_irqsave(&phba->hbalock, flag);
  3403. pmboxq->context1 = NULL;
  3404. /*
  3405. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  3406. * else do not free the resources.
  3407. */
  3408. if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
  3409. retval = MBX_SUCCESS;
  3410. else {
  3411. retval = MBX_TIMEOUT;
  3412. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3413. }
  3414. spin_unlock_irqrestore(&phba->hbalock, flag);
  3415. }
  3416. return retval;
  3417. }
  3418. int
  3419. lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
  3420. {
  3421. struct lpfc_vport *vport = phba->pport;
  3422. int i = 0;
  3423. uint32_t ha_copy;
  3424. while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
  3425. if (i++ > LPFC_MBOX_TMO * 1000)
  3426. return 1;
  3427. /*
  3428. * Call lpfc_sli_handle_mb_event only if a mailbox cmd
  3429. * did finish. This way we won't get the misleading
  3430. * "Stray Mailbox Interrupt" message.
  3431. */
  3432. spin_lock_irq(&phba->hbalock);
  3433. ha_copy = phba->work_ha;
  3434. phba->work_ha &= ~HA_MBATT;
  3435. spin_unlock_irq(&phba->hbalock);
  3436. if (ha_copy & HA_MBATT)
  3437. if (lpfc_sli_handle_mb_event(phba) == 0)
  3438. i = 0;
  3439. msleep(1);
  3440. }
  3441. return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
  3442. }
  3443. irqreturn_t
  3444. lpfc_intr_handler(int irq, void *dev_id)
  3445. {
  3446. struct lpfc_hba *phba;
  3447. uint32_t ha_copy;
  3448. uint32_t work_ha_copy;
  3449. unsigned long status;
  3450. uint32_t control;
  3451. MAILBOX_t *mbox, *pmbox;
  3452. struct lpfc_vport *vport;
  3453. struct lpfc_nodelist *ndlp;
  3454. struct lpfc_dmabuf *mp;
  3455. LPFC_MBOXQ_t *pmb;
  3456. int rc;
  3457. /*
  3458. * Get the driver's phba structure from the dev_id and
  3459. * assume the HBA is not interrupting.
  3460. */
  3461. phba = (struct lpfc_hba *) dev_id;
  3462. if (unlikely(!phba))
  3463. return IRQ_NONE;
  3464. /* If the pci channel is offline, ignore all the interrupts. */
  3465. if (unlikely(pci_channel_offline(phba->pcidev)))
  3466. return IRQ_NONE;
  3467. phba->sli.slistat.sli_intr++;
  3468. /*
  3469. * Call the HBA to see if it is interrupting. If not, don't claim
  3470. * the interrupt
  3471. */
  3472. /* Ignore all interrupts during initialization. */
  3473. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  3474. return IRQ_NONE;
  3475. /*
  3476. * Read host attention register to determine interrupt source
  3477. * Clear Attention Sources, except Error Attention (to
  3478. * preserve status) and Link Attention
  3479. */
  3480. spin_lock(&phba->hbalock);
  3481. ha_copy = readl(phba->HAregaddr);
  3482. /* If somebody is waiting to handle an eratt don't process it
  3483. * here. The brdkill function will do this.
  3484. */
  3485. if (phba->link_flag & LS_IGNORE_ERATT)
  3486. ha_copy &= ~HA_ERATT;
  3487. writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  3488. readl(phba->HAregaddr); /* flush */
  3489. spin_unlock(&phba->hbalock);
  3490. if (unlikely(!ha_copy))
  3491. return IRQ_NONE;
  3492. work_ha_copy = ha_copy & phba->work_ha_mask;
  3493. if (unlikely(work_ha_copy)) {
  3494. if (work_ha_copy & HA_LATT) {
  3495. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  3496. /*
  3497. * Turn off Link Attention interrupts
  3498. * until CLEAR_LA done
  3499. */
  3500. spin_lock(&phba->hbalock);
  3501. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  3502. control = readl(phba->HCregaddr);
  3503. control &= ~HC_LAINT_ENA;
  3504. writel(control, phba->HCregaddr);
  3505. readl(phba->HCregaddr); /* flush */
  3506. spin_unlock(&phba->hbalock);
  3507. }
  3508. else
  3509. work_ha_copy &= ~HA_LATT;
  3510. }
  3511. if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
  3512. /*
  3513. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  3514. * the only slow ring.
  3515. */
  3516. status = (work_ha_copy &
  3517. (HA_RXMASK << (4*LPFC_ELS_RING)));
  3518. status >>= (4*LPFC_ELS_RING);
  3519. if (status & HA_RXMASK) {
  3520. spin_lock(&phba->hbalock);
  3521. control = readl(phba->HCregaddr);
  3522. lpfc_debugfs_slow_ring_trc(phba,
  3523. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  3524. control, status,
  3525. (uint32_t)phba->sli.slistat.sli_intr);
  3526. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  3527. lpfc_debugfs_slow_ring_trc(phba,
  3528. "ISR Disable ring:"
  3529. "pwork:x%x hawork:x%x wait:x%x",
  3530. phba->work_ha, work_ha_copy,
  3531. (uint32_t)((unsigned long)
  3532. phba->work_wait));
  3533. control &=
  3534. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  3535. writel(control, phba->HCregaddr);
  3536. readl(phba->HCregaddr); /* flush */
  3537. }
  3538. else {
  3539. lpfc_debugfs_slow_ring_trc(phba,
  3540. "ISR slow ring: pwork:"
  3541. "x%x hawork:x%x wait:x%x",
  3542. phba->work_ha, work_ha_copy,
  3543. (uint32_t)((unsigned long)
  3544. phba->work_wait));
  3545. }
  3546. spin_unlock(&phba->hbalock);
  3547. }
  3548. }
  3549. if (work_ha_copy & HA_ERATT) {
  3550. /*
  3551. * There was a link/board error. Read the
  3552. * status register to retrieve the error event
  3553. * and process it.
  3554. */
  3555. phba->sli.slistat.err_attn_event++;
  3556. /* Save status info */
  3557. phba->work_hs = readl(phba->HSregaddr);
  3558. phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
  3559. phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
  3560. /* Clear Chip error bit */
  3561. writel(HA_ERATT, phba->HAregaddr);
  3562. readl(phba->HAregaddr); /* flush */
  3563. phba->pport->stopped = 1;
  3564. }
  3565. if ((work_ha_copy & HA_MBATT) &&
  3566. (phba->sli.mbox_active)) {
  3567. pmb = phba->sli.mbox_active;
  3568. pmbox = &pmb->mb;
  3569. mbox = &phba->slim2p->mbx;
  3570. vport = pmb->vport;
  3571. /* First check out the status word */
  3572. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  3573. if (pmbox->mbxOwner != OWN_HOST) {
  3574. /*
  3575. * Stray Mailbox Interrupt, mbxCommand <cmd>
  3576. * mbxStatus <status>
  3577. */
  3578. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  3579. LOG_SLI,
  3580. "(%d):0304 Stray Mailbox "
  3581. "Interrupt mbxCommand x%x "
  3582. "mbxStatus x%x\n",
  3583. (vport ? vport->vpi : 0),
  3584. pmbox->mbxCommand,
  3585. pmbox->mbxStatus);
  3586. /* clear mailbox attention bit */
  3587. work_ha_copy &= ~HA_MBATT;
  3588. } else {
  3589. phba->last_completion_time = jiffies;
  3590. del_timer(&phba->sli.mbox_tmo);
  3591. phba->sli.mbox_active = NULL;
  3592. if (pmb->mbox_cmpl) {
  3593. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  3594. MAILBOX_CMD_SIZE);
  3595. }
  3596. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  3597. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  3598. lpfc_debugfs_disc_trc(vport,
  3599. LPFC_DISC_TRC_MBOX_VPORT,
  3600. "MBOX dflt rpi: : "
  3601. "status:x%x rpi:x%x",
  3602. (uint32_t)pmbox->mbxStatus,
  3603. pmbox->un.varWords[0], 0);
  3604. if (!pmbox->mbxStatus) {
  3605. mp = (struct lpfc_dmabuf *)
  3606. (pmb->context1);
  3607. ndlp = (struct lpfc_nodelist *)
  3608. pmb->context2;
  3609. /* Reg_LOGIN of dflt RPI was
  3610. * successful. new lets get
  3611. * rid of the RPI using the
  3612. * same mbox buffer.
  3613. */
  3614. lpfc_unreg_login(phba,
  3615. vport->vpi,
  3616. pmbox->un.varWords[0],
  3617. pmb);
  3618. pmb->mbox_cmpl =
  3619. lpfc_mbx_cmpl_dflt_rpi;
  3620. pmb->context1 = mp;
  3621. pmb->context2 = ndlp;
  3622. pmb->vport = vport;
  3623. spin_lock(&phba->hbalock);
  3624. phba->sli.sli_flag &=
  3625. ~LPFC_SLI_MBOX_ACTIVE;
  3626. spin_unlock(&phba->hbalock);
  3627. goto send_current_mbox;
  3628. }
  3629. }
  3630. spin_lock(&phba->pport->work_port_lock);
  3631. phba->pport->work_port_events &=
  3632. ~WORKER_MBOX_TMO;
  3633. spin_unlock(&phba->pport->work_port_lock);
  3634. lpfc_mbox_cmpl_put(phba, pmb);
  3635. }
  3636. }
  3637. if ((work_ha_copy & HA_MBATT) &&
  3638. (phba->sli.mbox_active == NULL)) {
  3639. send_next_mbox:
  3640. spin_lock(&phba->hbalock);
  3641. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3642. pmb = lpfc_mbox_get(phba);
  3643. spin_unlock(&phba->hbalock);
  3644. send_current_mbox:
  3645. /* Process next mailbox command if there is one */
  3646. if (pmb != NULL) {
  3647. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3648. if (rc == MBX_NOT_FINISHED) {
  3649. pmb->mb.mbxStatus = MBX_NOT_FINISHED;
  3650. lpfc_mbox_cmpl_put(phba, pmb);
  3651. goto send_next_mbox;
  3652. }
  3653. }
  3654. }
  3655. spin_lock(&phba->hbalock);
  3656. phba->work_ha |= work_ha_copy;
  3657. if (phba->work_wait)
  3658. lpfc_worker_wake_up(phba);
  3659. spin_unlock(&phba->hbalock);
  3660. }
  3661. ha_copy &= ~(phba->work_ha_mask);
  3662. /*
  3663. * Process all events on FCP ring. Take the optimized path for
  3664. * FCP IO. Any other IO is slow path and is handled by
  3665. * the worker thread.
  3666. */
  3667. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  3668. status >>= (4*LPFC_FCP_RING);
  3669. if (status & HA_RXMASK)
  3670. lpfc_sli_handle_fast_ring_event(phba,
  3671. &phba->sli.ring[LPFC_FCP_RING],
  3672. status);
  3673. if (phba->cfg_multi_ring_support == 2) {
  3674. /*
  3675. * Process all events on extra ring. Take the optimized path
  3676. * for extra ring IO. Any other IO is slow path and is handled
  3677. * by the worker thread.
  3678. */
  3679. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  3680. status >>= (4*LPFC_EXTRA_RING);
  3681. if (status & HA_RXMASK) {
  3682. lpfc_sli_handle_fast_ring_event(phba,
  3683. &phba->sli.ring[LPFC_EXTRA_RING],
  3684. status);
  3685. }
  3686. }
  3687. return IRQ_HANDLED;
  3688. } /* lpfc_intr_handler */