ibmvfc.c 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082
  1. /*
  2. * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
  3. *
  4. * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
  5. *
  6. * Copyright (C) IBM Corporation, 2008
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/dmapool.h>
  27. #include <linux/delay.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/kthread.h>
  30. #include <linux/of.h>
  31. #include <linux/stringify.h>
  32. #include <asm/firmware.h>
  33. #include <asm/irq.h>
  34. #include <asm/vio.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/scsi_cmnd.h>
  37. #include <scsi/scsi_host.h>
  38. #include <scsi/scsi_device.h>
  39. #include <scsi/scsi_tcq.h>
  40. #include <scsi/scsi_transport_fc.h>
  41. #include "ibmvfc.h"
  42. static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
  43. static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
  44. static unsigned int max_lun = IBMVFC_MAX_LUN;
  45. static unsigned int max_targets = IBMVFC_MAX_TARGETS;
  46. static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
  47. static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
  48. static unsigned int dev_loss_tmo = IBMVFC_DEV_LOSS_TMO;
  49. static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
  50. static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
  51. static LIST_HEAD(ibmvfc_head);
  52. static DEFINE_SPINLOCK(ibmvfc_driver_lock);
  53. static struct scsi_transport_template *ibmvfc_transport_template;
  54. MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
  55. MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
  56. MODULE_LICENSE("GPL");
  57. MODULE_VERSION(IBMVFC_DRIVER_VERSION);
  58. module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
  59. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
  60. "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
  61. module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
  62. MODULE_PARM_DESC(default_timeout,
  63. "Default timeout in seconds for initialization and EH commands. "
  64. "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
  65. module_param_named(max_requests, max_requests, uint, S_IRUGO);
  66. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
  67. "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
  68. module_param_named(max_lun, max_lun, uint, S_IRUGO);
  69. MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
  70. "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
  71. module_param_named(max_targets, max_targets, uint, S_IRUGO);
  72. MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
  73. "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
  74. module_param_named(disc_threads, disc_threads, uint, S_IRUGO | S_IWUSR);
  75. MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
  76. "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
  77. module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
  78. MODULE_PARM_DESC(debug, "Enable driver debug information. "
  79. "[Default=" __stringify(IBMVFC_DEBUG) "]");
  80. module_param_named(dev_loss_tmo, dev_loss_tmo, uint, S_IRUGO | S_IWUSR);
  81. MODULE_PARM_DESC(dev_loss_tmo, "Maximum number of seconds that the FC "
  82. "transport should insulate the loss of a remote port. Once this "
  83. "value is exceeded, the scsi target is removed. "
  84. "[Default=" __stringify(IBMVFC_DEV_LOSS_TMO) "]");
  85. module_param_named(log_level, log_level, uint, 0);
  86. MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
  87. "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
  88. static const struct {
  89. u16 status;
  90. u16 error;
  91. u8 result;
  92. u8 retry;
  93. int log;
  94. char *name;
  95. } cmd_status [] = {
  96. { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
  97. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
  98. { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
  99. { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_NO_CONNECT, 1, 1, "network down" },
  100. { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
  101. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
  102. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
  103. { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
  104. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
  105. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
  106. { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
  107. { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
  108. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 0, 0, "link halted" },
  109. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
  110. { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
  111. { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
  112. { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ABORT, 0, 1, "invalid parameter" },
  113. { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ABORT, 0, 1, "missing parameter" },
  114. { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
  115. { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ABORT, 0, 1, "transaction cancelled" },
  116. { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ABORT, 0, 1, "transaction cancelled implicit" },
  117. { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
  118. { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
  119. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
  120. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
  121. { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
  122. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
  123. { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
  124. { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
  125. { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
  126. { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
  127. { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
  128. { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
  129. { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
  130. { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
  131. };
  132. static void ibmvfc_npiv_login(struct ibmvfc_host *);
  133. static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
  134. static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
  135. static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
  136. static const char *unknown_error = "unknown error";
  137. #ifdef CONFIG_SCSI_IBMVFC_TRACE
  138. /**
  139. * ibmvfc_trc_start - Log a start trace entry
  140. * @evt: ibmvfc event struct
  141. *
  142. **/
  143. static void ibmvfc_trc_start(struct ibmvfc_event *evt)
  144. {
  145. struct ibmvfc_host *vhost = evt->vhost;
  146. struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
  147. struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
  148. struct ibmvfc_trace_entry *entry;
  149. entry = &vhost->trace[vhost->trace_index++];
  150. entry->evt = evt;
  151. entry->time = jiffies;
  152. entry->fmt = evt->crq.format;
  153. entry->type = IBMVFC_TRC_START;
  154. switch (entry->fmt) {
  155. case IBMVFC_CMD_FORMAT:
  156. entry->op_code = vfc_cmd->iu.cdb[0];
  157. entry->scsi_id = vfc_cmd->tgt_scsi_id;
  158. entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
  159. entry->tmf_flags = vfc_cmd->iu.tmf_flags;
  160. entry->u.start.xfer_len = vfc_cmd->iu.xfer_len;
  161. break;
  162. case IBMVFC_MAD_FORMAT:
  163. entry->op_code = mad->opcode;
  164. break;
  165. default:
  166. break;
  167. };
  168. }
  169. /**
  170. * ibmvfc_trc_end - Log an end trace entry
  171. * @evt: ibmvfc event struct
  172. *
  173. **/
  174. static void ibmvfc_trc_end(struct ibmvfc_event *evt)
  175. {
  176. struct ibmvfc_host *vhost = evt->vhost;
  177. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  178. struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
  179. struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
  180. entry->evt = evt;
  181. entry->time = jiffies;
  182. entry->fmt = evt->crq.format;
  183. entry->type = IBMVFC_TRC_END;
  184. switch (entry->fmt) {
  185. case IBMVFC_CMD_FORMAT:
  186. entry->op_code = vfc_cmd->iu.cdb[0];
  187. entry->scsi_id = vfc_cmd->tgt_scsi_id;
  188. entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
  189. entry->tmf_flags = vfc_cmd->iu.tmf_flags;
  190. entry->u.end.status = vfc_cmd->status;
  191. entry->u.end.error = vfc_cmd->error;
  192. entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
  193. entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
  194. entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
  195. break;
  196. case IBMVFC_MAD_FORMAT:
  197. entry->op_code = mad->opcode;
  198. entry->u.end.status = mad->status;
  199. break;
  200. default:
  201. break;
  202. };
  203. }
  204. #else
  205. #define ibmvfc_trc_start(evt) do { } while (0)
  206. #define ibmvfc_trc_end(evt) do { } while (0)
  207. #endif
  208. /**
  209. * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
  210. * @status: status / error class
  211. * @error: error
  212. *
  213. * Return value:
  214. * index into cmd_status / -EINVAL on failure
  215. **/
  216. static int ibmvfc_get_err_index(u16 status, u16 error)
  217. {
  218. int i;
  219. for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
  220. if ((cmd_status[i].status & status) == cmd_status[i].status &&
  221. cmd_status[i].error == error)
  222. return i;
  223. return -EINVAL;
  224. }
  225. /**
  226. * ibmvfc_get_cmd_error - Find the error description for the fcp response
  227. * @status: status / error class
  228. * @error: error
  229. *
  230. * Return value:
  231. * error description string
  232. **/
  233. static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
  234. {
  235. int rc = ibmvfc_get_err_index(status, error);
  236. if (rc >= 0)
  237. return cmd_status[rc].name;
  238. return unknown_error;
  239. }
  240. /**
  241. * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
  242. * @vfc_cmd: ibmvfc command struct
  243. *
  244. * Return value:
  245. * SCSI result value to return for completed command
  246. **/
  247. static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
  248. {
  249. int err;
  250. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  251. int fc_rsp_len = rsp->fcp_rsp_len;
  252. if ((rsp->flags & FCP_RSP_LEN_VALID) &&
  253. ((!fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
  254. rsp->data.info.rsp_code))
  255. return DID_ERROR << 16;
  256. if (!vfc_cmd->status) {
  257. if (rsp->flags & FCP_RESID_OVER)
  258. return rsp->scsi_status | (DID_ERROR << 16);
  259. else
  260. return rsp->scsi_status | (DID_OK << 16);
  261. }
  262. err = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
  263. if (err >= 0)
  264. return rsp->scsi_status | (cmd_status[err].result << 16);
  265. return rsp->scsi_status | (DID_ERROR << 16);
  266. }
  267. /**
  268. * ibmvfc_retry_cmd - Determine if error status is retryable
  269. * @status: status / error class
  270. * @error: error
  271. *
  272. * Return value:
  273. * 1 if error should be retried / 0 if it should not
  274. **/
  275. static int ibmvfc_retry_cmd(u16 status, u16 error)
  276. {
  277. int rc = ibmvfc_get_err_index(status, error);
  278. if (rc >= 0)
  279. return cmd_status[rc].retry;
  280. return 1;
  281. }
  282. static const char *unknown_fc_explain = "unknown fc explain";
  283. static const struct {
  284. u16 fc_explain;
  285. char *name;
  286. } ls_explain [] = {
  287. { 0x00, "no additional explanation" },
  288. { 0x01, "service parameter error - options" },
  289. { 0x03, "service parameter error - initiator control" },
  290. { 0x05, "service parameter error - recipient control" },
  291. { 0x07, "service parameter error - received data field size" },
  292. { 0x09, "service parameter error - concurrent seq" },
  293. { 0x0B, "service parameter error - credit" },
  294. { 0x0D, "invalid N_Port/F_Port_Name" },
  295. { 0x0E, "invalid node/Fabric Name" },
  296. { 0x0F, "invalid common service parameters" },
  297. { 0x11, "invalid association header" },
  298. { 0x13, "association header required" },
  299. { 0x15, "invalid originator S_ID" },
  300. { 0x17, "invalid OX_ID-RX-ID combination" },
  301. { 0x19, "command (request) already in progress" },
  302. { 0x1E, "N_Port Login requested" },
  303. { 0x1F, "Invalid N_Port_ID" },
  304. };
  305. static const struct {
  306. u16 fc_explain;
  307. char *name;
  308. } gs_explain [] = {
  309. { 0x00, "no additional explanation" },
  310. { 0x01, "port identifier not registered" },
  311. { 0x02, "port name not registered" },
  312. { 0x03, "node name not registered" },
  313. { 0x04, "class of service not registered" },
  314. { 0x06, "initial process associator not registered" },
  315. { 0x07, "FC-4 TYPEs not registered" },
  316. { 0x08, "symbolic port name not registered" },
  317. { 0x09, "symbolic node name not registered" },
  318. { 0x0A, "port type not registered" },
  319. { 0xF0, "authorization exception" },
  320. { 0xF1, "authentication exception" },
  321. { 0xF2, "data base full" },
  322. { 0xF3, "data base empty" },
  323. { 0xF4, "processing request" },
  324. { 0xF5, "unable to verify connection" },
  325. { 0xF6, "devices not in a common zone" },
  326. };
  327. /**
  328. * ibmvfc_get_ls_explain - Return the FC Explain description text
  329. * @status: FC Explain status
  330. *
  331. * Returns:
  332. * error string
  333. **/
  334. static const char *ibmvfc_get_ls_explain(u16 status)
  335. {
  336. int i;
  337. for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
  338. if (ls_explain[i].fc_explain == status)
  339. return ls_explain[i].name;
  340. return unknown_fc_explain;
  341. }
  342. /**
  343. * ibmvfc_get_gs_explain - Return the FC Explain description text
  344. * @status: FC Explain status
  345. *
  346. * Returns:
  347. * error string
  348. **/
  349. static const char *ibmvfc_get_gs_explain(u16 status)
  350. {
  351. int i;
  352. for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
  353. if (gs_explain[i].fc_explain == status)
  354. return gs_explain[i].name;
  355. return unknown_fc_explain;
  356. }
  357. static const struct {
  358. enum ibmvfc_fc_type fc_type;
  359. char *name;
  360. } fc_type [] = {
  361. { IBMVFC_FABRIC_REJECT, "fabric reject" },
  362. { IBMVFC_PORT_REJECT, "port reject" },
  363. { IBMVFC_LS_REJECT, "ELS reject" },
  364. { IBMVFC_FABRIC_BUSY, "fabric busy" },
  365. { IBMVFC_PORT_BUSY, "port busy" },
  366. { IBMVFC_BASIC_REJECT, "basic reject" },
  367. };
  368. static const char *unknown_fc_type = "unknown fc type";
  369. /**
  370. * ibmvfc_get_fc_type - Return the FC Type description text
  371. * @status: FC Type error status
  372. *
  373. * Returns:
  374. * error string
  375. **/
  376. static const char *ibmvfc_get_fc_type(u16 status)
  377. {
  378. int i;
  379. for (i = 0; i < ARRAY_SIZE(fc_type); i++)
  380. if (fc_type[i].fc_type == status)
  381. return fc_type[i].name;
  382. return unknown_fc_type;
  383. }
  384. /**
  385. * ibmvfc_set_tgt_action - Set the next init action for the target
  386. * @tgt: ibmvfc target struct
  387. * @action: action to perform
  388. *
  389. **/
  390. static void ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
  391. enum ibmvfc_target_action action)
  392. {
  393. switch (tgt->action) {
  394. case IBMVFC_TGT_ACTION_DEL_RPORT:
  395. break;
  396. default:
  397. tgt->action = action;
  398. break;
  399. }
  400. }
  401. /**
  402. * ibmvfc_set_host_state - Set the state for the host
  403. * @vhost: ibmvfc host struct
  404. * @state: state to set host to
  405. *
  406. * Returns:
  407. * 0 if state changed / non-zero if not changed
  408. **/
  409. static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
  410. enum ibmvfc_host_state state)
  411. {
  412. int rc = 0;
  413. switch (vhost->state) {
  414. case IBMVFC_HOST_OFFLINE:
  415. rc = -EINVAL;
  416. break;
  417. default:
  418. vhost->state = state;
  419. break;
  420. };
  421. return rc;
  422. }
  423. /**
  424. * ibmvfc_set_host_action - Set the next init action for the host
  425. * @vhost: ibmvfc host struct
  426. * @action: action to perform
  427. *
  428. **/
  429. static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
  430. enum ibmvfc_host_action action)
  431. {
  432. switch (action) {
  433. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  434. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
  435. vhost->action = action;
  436. break;
  437. case IBMVFC_HOST_ACTION_INIT_WAIT:
  438. if (vhost->action == IBMVFC_HOST_ACTION_INIT)
  439. vhost->action = action;
  440. break;
  441. case IBMVFC_HOST_ACTION_QUERY:
  442. switch (vhost->action) {
  443. case IBMVFC_HOST_ACTION_INIT_WAIT:
  444. case IBMVFC_HOST_ACTION_NONE:
  445. case IBMVFC_HOST_ACTION_TGT_ADD:
  446. vhost->action = action;
  447. break;
  448. default:
  449. break;
  450. };
  451. break;
  452. case IBMVFC_HOST_ACTION_TGT_INIT:
  453. if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
  454. vhost->action = action;
  455. break;
  456. case IBMVFC_HOST_ACTION_INIT:
  457. case IBMVFC_HOST_ACTION_TGT_DEL:
  458. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  459. case IBMVFC_HOST_ACTION_TGT_ADD:
  460. case IBMVFC_HOST_ACTION_NONE:
  461. default:
  462. vhost->action = action;
  463. break;
  464. };
  465. }
  466. /**
  467. * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
  468. * @vhost: ibmvfc host struct
  469. *
  470. * Return value:
  471. * nothing
  472. **/
  473. static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
  474. {
  475. if (vhost->action == IBMVFC_HOST_ACTION_NONE) {
  476. if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  477. scsi_block_requests(vhost->host);
  478. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  479. }
  480. } else
  481. vhost->reinit = 1;
  482. wake_up(&vhost->work_wait_q);
  483. }
  484. /**
  485. * ibmvfc_link_down - Handle a link down event from the adapter
  486. * @vhost: ibmvfc host struct
  487. * @state: ibmvfc host state to enter
  488. *
  489. **/
  490. static void ibmvfc_link_down(struct ibmvfc_host *vhost,
  491. enum ibmvfc_host_state state)
  492. {
  493. struct ibmvfc_target *tgt;
  494. ENTER;
  495. scsi_block_requests(vhost->host);
  496. list_for_each_entry(tgt, &vhost->targets, queue)
  497. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  498. ibmvfc_set_host_state(vhost, state);
  499. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
  500. vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
  501. wake_up(&vhost->work_wait_q);
  502. LEAVE;
  503. }
  504. /**
  505. * ibmvfc_init_host - Start host initialization
  506. * @vhost: ibmvfc host struct
  507. * @relogin: is this a re-login?
  508. *
  509. * Return value:
  510. * nothing
  511. **/
  512. static void ibmvfc_init_host(struct ibmvfc_host *vhost, int relogin)
  513. {
  514. struct ibmvfc_target *tgt;
  515. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
  516. if (++vhost->init_retries > IBMVFC_MAX_INIT_RETRIES) {
  517. dev_err(vhost->dev,
  518. "Host initialization retries exceeded. Taking adapter offline\n");
  519. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  520. return;
  521. }
  522. }
  523. if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  524. if (!relogin) {
  525. memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
  526. vhost->async_crq.cur = 0;
  527. }
  528. list_for_each_entry(tgt, &vhost->targets, queue)
  529. tgt->need_login = 1;
  530. scsi_block_requests(vhost->host);
  531. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  532. vhost->job_step = ibmvfc_npiv_login;
  533. wake_up(&vhost->work_wait_q);
  534. }
  535. }
  536. /**
  537. * ibmvfc_send_crq - Send a CRQ
  538. * @vhost: ibmvfc host struct
  539. * @word1: the first 64 bits of the data
  540. * @word2: the second 64 bits of the data
  541. *
  542. * Return value:
  543. * 0 on success / other on failure
  544. **/
  545. static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
  546. {
  547. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  548. return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
  549. }
  550. /**
  551. * ibmvfc_send_crq_init - Send a CRQ init message
  552. * @vhost: ibmvfc host struct
  553. *
  554. * Return value:
  555. * 0 on success / other on failure
  556. **/
  557. static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
  558. {
  559. ibmvfc_dbg(vhost, "Sending CRQ init\n");
  560. return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
  561. }
  562. /**
  563. * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
  564. * @vhost: ibmvfc host struct
  565. *
  566. * Return value:
  567. * 0 on success / other on failure
  568. **/
  569. static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
  570. {
  571. ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
  572. return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
  573. }
  574. /**
  575. * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
  576. * @vhost: ibmvfc host struct
  577. *
  578. * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
  579. * the crq with the hypervisor.
  580. **/
  581. static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
  582. {
  583. long rc;
  584. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  585. struct ibmvfc_crq_queue *crq = &vhost->crq;
  586. ibmvfc_dbg(vhost, "Releasing CRQ\n");
  587. free_irq(vdev->irq, vhost);
  588. do {
  589. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  590. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  591. vhost->state = IBMVFC_NO_CRQ;
  592. dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
  593. free_page((unsigned long)crq->msgs);
  594. }
  595. /**
  596. * ibmvfc_reenable_crq_queue - reenables the CRQ
  597. * @vhost: ibmvfc host struct
  598. *
  599. * Return value:
  600. * 0 on success / other on failure
  601. **/
  602. static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
  603. {
  604. int rc;
  605. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  606. /* Re-enable the CRQ */
  607. do {
  608. rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
  609. } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
  610. if (rc)
  611. dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
  612. return rc;
  613. }
  614. /**
  615. * ibmvfc_reset_crq - resets a crq after a failure
  616. * @vhost: ibmvfc host struct
  617. *
  618. * Return value:
  619. * 0 on success / other on failure
  620. **/
  621. static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
  622. {
  623. int rc;
  624. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  625. struct ibmvfc_crq_queue *crq = &vhost->crq;
  626. /* Close the CRQ */
  627. do {
  628. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  629. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  630. vhost->state = IBMVFC_NO_CRQ;
  631. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  632. /* Clean out the queue */
  633. memset(crq->msgs, 0, PAGE_SIZE);
  634. crq->cur = 0;
  635. /* And re-open it again */
  636. rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  637. crq->msg_token, PAGE_SIZE);
  638. if (rc == H_CLOSED)
  639. /* Adapter is good, but other end is not ready */
  640. dev_warn(vhost->dev, "Partner adapter not ready\n");
  641. else if (rc != 0)
  642. dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
  643. return rc;
  644. }
  645. /**
  646. * ibmvfc_valid_event - Determines if event is valid.
  647. * @pool: event_pool that contains the event
  648. * @evt: ibmvfc event to be checked for validity
  649. *
  650. * Return value:
  651. * 1 if event is valid / 0 if event is not valid
  652. **/
  653. static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
  654. struct ibmvfc_event *evt)
  655. {
  656. int index = evt - pool->events;
  657. if (index < 0 || index >= pool->size) /* outside of bounds */
  658. return 0;
  659. if (evt != pool->events + index) /* unaligned */
  660. return 0;
  661. return 1;
  662. }
  663. /**
  664. * ibmvfc_free_event - Free the specified event
  665. * @evt: ibmvfc_event to be freed
  666. *
  667. **/
  668. static void ibmvfc_free_event(struct ibmvfc_event *evt)
  669. {
  670. struct ibmvfc_host *vhost = evt->vhost;
  671. struct ibmvfc_event_pool *pool = &vhost->pool;
  672. BUG_ON(!ibmvfc_valid_event(pool, evt));
  673. BUG_ON(atomic_inc_return(&evt->free) != 1);
  674. list_add_tail(&evt->queue, &vhost->free);
  675. }
  676. /**
  677. * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
  678. * @evt: ibmvfc event struct
  679. *
  680. * This function does not setup any error status, that must be done
  681. * before this function gets called.
  682. **/
  683. static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
  684. {
  685. struct scsi_cmnd *cmnd = evt->cmnd;
  686. if (cmnd) {
  687. scsi_dma_unmap(cmnd);
  688. cmnd->scsi_done(cmnd);
  689. }
  690. ibmvfc_free_event(evt);
  691. }
  692. /**
  693. * ibmvfc_fail_request - Fail request with specified error code
  694. * @evt: ibmvfc event struct
  695. * @error_code: error code to fail request with
  696. *
  697. * Return value:
  698. * none
  699. **/
  700. static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
  701. {
  702. if (evt->cmnd) {
  703. evt->cmnd->result = (error_code << 16);
  704. evt->done = ibmvfc_scsi_eh_done;
  705. } else
  706. evt->xfer_iu->mad_common.status = IBMVFC_MAD_DRIVER_FAILED;
  707. list_del(&evt->queue);
  708. del_timer(&evt->timer);
  709. ibmvfc_trc_end(evt);
  710. evt->done(evt);
  711. }
  712. /**
  713. * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
  714. * @vhost: ibmvfc host struct
  715. * @error_code: error code to fail requests with
  716. *
  717. * Return value:
  718. * none
  719. **/
  720. static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
  721. {
  722. struct ibmvfc_event *evt, *pos;
  723. ibmvfc_dbg(vhost, "Purging all requests\n");
  724. list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
  725. ibmvfc_fail_request(evt, error_code);
  726. }
  727. /**
  728. * __ibmvfc_reset_host - Reset the connection to the server (no locking)
  729. * @vhost: struct ibmvfc host to reset
  730. **/
  731. static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
  732. {
  733. int rc;
  734. scsi_block_requests(vhost->host);
  735. ibmvfc_purge_requests(vhost, DID_ERROR);
  736. if ((rc = ibmvfc_reset_crq(vhost)) ||
  737. (rc = ibmvfc_send_crq_init(vhost)) ||
  738. (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
  739. dev_err(vhost->dev, "Error after reset rc=%d\n", rc);
  740. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  741. } else
  742. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  743. }
  744. /**
  745. * ibmvfc_reset_host - Reset the connection to the server
  746. * @vhost: struct ibmvfc host to reset
  747. **/
  748. static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
  749. {
  750. unsigned long flags;
  751. spin_lock_irqsave(vhost->host->host_lock, flags);
  752. __ibmvfc_reset_host(vhost);
  753. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  754. }
  755. /**
  756. * ibmvfc_retry_host_init - Retry host initialization if allowed
  757. * @vhost: ibmvfc host struct
  758. *
  759. **/
  760. static void ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
  761. {
  762. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
  763. if (++vhost->init_retries > IBMVFC_MAX_INIT_RETRIES) {
  764. dev_err(vhost->dev,
  765. "Host initialization retries exceeded. Taking adapter offline\n");
  766. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  767. } else if (vhost->init_retries == IBMVFC_MAX_INIT_RETRIES)
  768. __ibmvfc_reset_host(vhost);
  769. else
  770. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  771. }
  772. wake_up(&vhost->work_wait_q);
  773. }
  774. /**
  775. * __ibmvfc_get_target - Find the specified scsi_target (no locking)
  776. * @starget: scsi target struct
  777. *
  778. * Return value:
  779. * ibmvfc_target struct / NULL if not found
  780. **/
  781. static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
  782. {
  783. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  784. struct ibmvfc_host *vhost = shost_priv(shost);
  785. struct ibmvfc_target *tgt;
  786. list_for_each_entry(tgt, &vhost->targets, queue)
  787. if (tgt->target_id == starget->id) {
  788. kref_get(&tgt->kref);
  789. return tgt;
  790. }
  791. return NULL;
  792. }
  793. /**
  794. * ibmvfc_get_target - Find the specified scsi_target
  795. * @starget: scsi target struct
  796. *
  797. * Return value:
  798. * ibmvfc_target struct / NULL if not found
  799. **/
  800. static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
  801. {
  802. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  803. struct ibmvfc_target *tgt;
  804. unsigned long flags;
  805. spin_lock_irqsave(shost->host_lock, flags);
  806. tgt = __ibmvfc_get_target(starget);
  807. spin_unlock_irqrestore(shost->host_lock, flags);
  808. return tgt;
  809. }
  810. /**
  811. * ibmvfc_get_host_speed - Get host port speed
  812. * @shost: scsi host struct
  813. *
  814. * Return value:
  815. * none
  816. **/
  817. static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
  818. {
  819. struct ibmvfc_host *vhost = shost_priv(shost);
  820. unsigned long flags;
  821. spin_lock_irqsave(shost->host_lock, flags);
  822. if (vhost->state == IBMVFC_ACTIVE) {
  823. switch (vhost->login_buf->resp.link_speed / 100) {
  824. case 1:
  825. fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
  826. break;
  827. case 2:
  828. fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
  829. break;
  830. case 4:
  831. fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
  832. break;
  833. case 8:
  834. fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
  835. break;
  836. case 10:
  837. fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
  838. break;
  839. case 16:
  840. fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
  841. break;
  842. default:
  843. ibmvfc_log(vhost, 3, "Unknown port speed: %ld Gbit\n",
  844. vhost->login_buf->resp.link_speed / 100);
  845. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  846. break;
  847. }
  848. } else
  849. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  850. spin_unlock_irqrestore(shost->host_lock, flags);
  851. }
  852. /**
  853. * ibmvfc_get_host_port_state - Get host port state
  854. * @shost: scsi host struct
  855. *
  856. * Return value:
  857. * none
  858. **/
  859. static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
  860. {
  861. struct ibmvfc_host *vhost = shost_priv(shost);
  862. unsigned long flags;
  863. spin_lock_irqsave(shost->host_lock, flags);
  864. switch (vhost->state) {
  865. case IBMVFC_INITIALIZING:
  866. case IBMVFC_ACTIVE:
  867. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  868. break;
  869. case IBMVFC_LINK_DOWN:
  870. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  871. break;
  872. case IBMVFC_LINK_DEAD:
  873. case IBMVFC_HOST_OFFLINE:
  874. fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
  875. break;
  876. case IBMVFC_HALTED:
  877. fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
  878. break;
  879. case IBMVFC_NO_CRQ:
  880. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  881. break;
  882. default:
  883. ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
  884. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  885. break;
  886. }
  887. spin_unlock_irqrestore(shost->host_lock, flags);
  888. }
  889. /**
  890. * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
  891. * @rport: rport struct
  892. * @timeout: timeout value
  893. *
  894. * Return value:
  895. * none
  896. **/
  897. static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  898. {
  899. if (timeout)
  900. rport->dev_loss_tmo = timeout;
  901. else
  902. rport->dev_loss_tmo = 1;
  903. }
  904. /**
  905. * ibmvfc_release_tgt - Free memory allocated for a target
  906. * @kref: kref struct
  907. *
  908. **/
  909. static void ibmvfc_release_tgt(struct kref *kref)
  910. {
  911. struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
  912. kfree(tgt);
  913. }
  914. /**
  915. * ibmvfc_get_starget_node_name - Get SCSI target's node name
  916. * @starget: scsi target struct
  917. *
  918. * Return value:
  919. * none
  920. **/
  921. static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
  922. {
  923. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  924. fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
  925. if (tgt)
  926. kref_put(&tgt->kref, ibmvfc_release_tgt);
  927. }
  928. /**
  929. * ibmvfc_get_starget_port_name - Get SCSI target's port name
  930. * @starget: scsi target struct
  931. *
  932. * Return value:
  933. * none
  934. **/
  935. static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
  936. {
  937. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  938. fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
  939. if (tgt)
  940. kref_put(&tgt->kref, ibmvfc_release_tgt);
  941. }
  942. /**
  943. * ibmvfc_get_starget_port_id - Get SCSI target's port ID
  944. * @starget: scsi target struct
  945. *
  946. * Return value:
  947. * none
  948. **/
  949. static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
  950. {
  951. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  952. fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
  953. if (tgt)
  954. kref_put(&tgt->kref, ibmvfc_release_tgt);
  955. }
  956. /**
  957. * ibmvfc_wait_while_resetting - Wait while the host resets
  958. * @vhost: ibmvfc host struct
  959. *
  960. * Return value:
  961. * 0 on success / other on failure
  962. **/
  963. static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
  964. {
  965. long timeout = wait_event_timeout(vhost->init_wait_q,
  966. ((vhost->state == IBMVFC_ACTIVE ||
  967. vhost->state == IBMVFC_HOST_OFFLINE ||
  968. vhost->state == IBMVFC_LINK_DEAD) &&
  969. vhost->action == IBMVFC_HOST_ACTION_NONE),
  970. (init_timeout * HZ));
  971. return timeout ? 0 : -EIO;
  972. }
  973. /**
  974. * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
  975. * @shost: scsi host struct
  976. *
  977. * Return value:
  978. * 0 on success / other on failure
  979. **/
  980. static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
  981. {
  982. struct ibmvfc_host *vhost = shost_priv(shost);
  983. dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
  984. ibmvfc_reset_host(vhost);
  985. return ibmvfc_wait_while_resetting(vhost);
  986. }
  987. /**
  988. * ibmvfc_gather_partition_info - Gather info about the LPAR
  989. *
  990. * Return value:
  991. * none
  992. **/
  993. static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
  994. {
  995. struct device_node *rootdn;
  996. const char *name;
  997. const unsigned int *num;
  998. rootdn = of_find_node_by_path("/");
  999. if (!rootdn)
  1000. return;
  1001. name = of_get_property(rootdn, "ibm,partition-name", NULL);
  1002. if (name)
  1003. strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
  1004. num = of_get_property(rootdn, "ibm,partition-no", NULL);
  1005. if (num)
  1006. vhost->partition_number = *num;
  1007. of_node_put(rootdn);
  1008. }
  1009. /**
  1010. * ibmvfc_set_login_info - Setup info for NPIV login
  1011. * @vhost: ibmvfc host struct
  1012. *
  1013. * Return value:
  1014. * none
  1015. **/
  1016. static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
  1017. {
  1018. struct ibmvfc_npiv_login *login_info = &vhost->login_info;
  1019. struct device_node *of_node = vhost->dev->archdata.of_node;
  1020. const char *location;
  1021. memset(login_info, 0, sizeof(*login_info));
  1022. login_info->ostype = IBMVFC_OS_LINUX;
  1023. login_info->max_dma_len = IBMVFC_MAX_SECTORS << 9;
  1024. login_info->max_payload = sizeof(struct ibmvfc_fcp_cmd_iu);
  1025. login_info->max_response = sizeof(struct ibmvfc_fcp_rsp);
  1026. login_info->partition_num = vhost->partition_number;
  1027. login_info->vfc_frame_version = 1;
  1028. login_info->fcp_version = 3;
  1029. if (vhost->client_migrated)
  1030. login_info->flags = IBMVFC_CLIENT_MIGRATED;
  1031. login_info->max_cmds = max_requests + IBMVFC_NUM_INTERNAL_REQ;
  1032. login_info->capabilities = IBMVFC_CAN_MIGRATE;
  1033. login_info->async.va = vhost->async_crq.msg_token;
  1034. login_info->async.len = vhost->async_crq.size * sizeof(*vhost->async_crq.msgs);
  1035. strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
  1036. strncpy(login_info->device_name,
  1037. vhost->host->shost_gendev.bus_id, IBMVFC_MAX_NAME);
  1038. location = of_get_property(of_node, "ibm,loc-code", NULL);
  1039. location = location ? location : vhost->dev->bus_id;
  1040. strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
  1041. }
  1042. /**
  1043. * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
  1044. * @vhost: ibmvfc host who owns the event pool
  1045. *
  1046. * Returns zero on success.
  1047. **/
  1048. static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
  1049. {
  1050. int i;
  1051. struct ibmvfc_event_pool *pool = &vhost->pool;
  1052. ENTER;
  1053. pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
  1054. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  1055. if (!pool->events)
  1056. return -ENOMEM;
  1057. pool->iu_storage = dma_alloc_coherent(vhost->dev,
  1058. pool->size * sizeof(*pool->iu_storage),
  1059. &pool->iu_token, 0);
  1060. if (!pool->iu_storage) {
  1061. kfree(pool->events);
  1062. return -ENOMEM;
  1063. }
  1064. for (i = 0; i < pool->size; ++i) {
  1065. struct ibmvfc_event *evt = &pool->events[i];
  1066. atomic_set(&evt->free, 1);
  1067. evt->crq.valid = 0x80;
  1068. evt->crq.ioba = pool->iu_token + (sizeof(*evt->xfer_iu) * i);
  1069. evt->xfer_iu = pool->iu_storage + i;
  1070. evt->vhost = vhost;
  1071. evt->ext_list = NULL;
  1072. list_add_tail(&evt->queue, &vhost->free);
  1073. }
  1074. LEAVE;
  1075. return 0;
  1076. }
  1077. /**
  1078. * ibmvfc_free_event_pool - Frees memory of the event pool of a host
  1079. * @vhost: ibmvfc host who owns the event pool
  1080. *
  1081. **/
  1082. static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
  1083. {
  1084. int i;
  1085. struct ibmvfc_event_pool *pool = &vhost->pool;
  1086. ENTER;
  1087. for (i = 0; i < pool->size; ++i) {
  1088. list_del(&pool->events[i].queue);
  1089. BUG_ON(atomic_read(&pool->events[i].free) != 1);
  1090. if (pool->events[i].ext_list)
  1091. dma_pool_free(vhost->sg_pool,
  1092. pool->events[i].ext_list,
  1093. pool->events[i].ext_list_token);
  1094. }
  1095. kfree(pool->events);
  1096. dma_free_coherent(vhost->dev,
  1097. pool->size * sizeof(*pool->iu_storage),
  1098. pool->iu_storage, pool->iu_token);
  1099. LEAVE;
  1100. }
  1101. /**
  1102. * ibmvfc_get_event - Gets the next free event in pool
  1103. * @vhost: ibmvfc host struct
  1104. *
  1105. * Returns a free event from the pool.
  1106. **/
  1107. static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
  1108. {
  1109. struct ibmvfc_event *evt;
  1110. BUG_ON(list_empty(&vhost->free));
  1111. evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
  1112. atomic_set(&evt->free, 0);
  1113. list_del(&evt->queue);
  1114. return evt;
  1115. }
  1116. /**
  1117. * ibmvfc_init_event - Initialize fields in an event struct that are always
  1118. * required.
  1119. * @evt: The event
  1120. * @done: Routine to call when the event is responded to
  1121. * @format: SRP or MAD format
  1122. **/
  1123. static void ibmvfc_init_event(struct ibmvfc_event *evt,
  1124. void (*done) (struct ibmvfc_event *), u8 format)
  1125. {
  1126. evt->cmnd = NULL;
  1127. evt->sync_iu = NULL;
  1128. evt->crq.format = format;
  1129. evt->done = done;
  1130. }
  1131. /**
  1132. * ibmvfc_map_sg_list - Initialize scatterlist
  1133. * @scmd: scsi command struct
  1134. * @nseg: number of scatterlist segments
  1135. * @md: memory descriptor list to initialize
  1136. **/
  1137. static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
  1138. struct srp_direct_buf *md)
  1139. {
  1140. int i;
  1141. struct scatterlist *sg;
  1142. scsi_for_each_sg(scmd, sg, nseg, i) {
  1143. md[i].va = sg_dma_address(sg);
  1144. md[i].len = sg_dma_len(sg);
  1145. md[i].key = 0;
  1146. }
  1147. }
  1148. /**
  1149. * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
  1150. * @scmd: Scsi_Cmnd with the scatterlist
  1151. * @evt: ibmvfc event struct
  1152. * @vfc_cmd: vfc_cmd that contains the memory descriptor
  1153. * @dev: device for which to map dma memory
  1154. *
  1155. * Returns:
  1156. * 0 on success / non-zero on failure
  1157. **/
  1158. static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
  1159. struct ibmvfc_event *evt,
  1160. struct ibmvfc_cmd *vfc_cmd, struct device *dev)
  1161. {
  1162. int sg_mapped;
  1163. struct srp_direct_buf *data = &vfc_cmd->ioba;
  1164. struct ibmvfc_host *vhost = dev_get_drvdata(dev);
  1165. sg_mapped = scsi_dma_map(scmd);
  1166. if (!sg_mapped) {
  1167. vfc_cmd->flags |= IBMVFC_NO_MEM_DESC;
  1168. return 0;
  1169. } else if (unlikely(sg_mapped < 0)) {
  1170. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1171. scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
  1172. return sg_mapped;
  1173. }
  1174. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  1175. vfc_cmd->flags |= IBMVFC_WRITE;
  1176. vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
  1177. } else {
  1178. vfc_cmd->flags |= IBMVFC_READ;
  1179. vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
  1180. }
  1181. if (sg_mapped == 1) {
  1182. ibmvfc_map_sg_list(scmd, sg_mapped, data);
  1183. return 0;
  1184. }
  1185. vfc_cmd->flags |= IBMVFC_SCATTERLIST;
  1186. if (!evt->ext_list) {
  1187. evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
  1188. &evt->ext_list_token);
  1189. if (!evt->ext_list) {
  1190. scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
  1191. return -ENOMEM;
  1192. }
  1193. }
  1194. ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
  1195. data->va = evt->ext_list_token;
  1196. data->len = sg_mapped * sizeof(struct srp_direct_buf);
  1197. data->key = 0;
  1198. return 0;
  1199. }
  1200. /**
  1201. * ibmvfc_timeout - Internal command timeout handler
  1202. * @evt: struct ibmvfc_event that timed out
  1203. *
  1204. * Called when an internally generated command times out
  1205. **/
  1206. static void ibmvfc_timeout(struct ibmvfc_event *evt)
  1207. {
  1208. struct ibmvfc_host *vhost = evt->vhost;
  1209. dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
  1210. ibmvfc_reset_host(vhost);
  1211. }
  1212. /**
  1213. * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
  1214. * @evt: event to be sent
  1215. * @vhost: ibmvfc host struct
  1216. * @timeout: timeout in seconds - 0 means do not time command
  1217. *
  1218. * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
  1219. **/
  1220. static int ibmvfc_send_event(struct ibmvfc_event *evt,
  1221. struct ibmvfc_host *vhost, unsigned long timeout)
  1222. {
  1223. u64 *crq_as_u64 = (u64 *) &evt->crq;
  1224. int rc;
  1225. /* Copy the IU into the transfer area */
  1226. *evt->xfer_iu = evt->iu;
  1227. if (evt->crq.format == IBMVFC_CMD_FORMAT)
  1228. evt->xfer_iu->cmd.tag = (u64)evt;
  1229. else if (evt->crq.format == IBMVFC_MAD_FORMAT)
  1230. evt->xfer_iu->mad_common.tag = (u64)evt;
  1231. else
  1232. BUG();
  1233. list_add_tail(&evt->queue, &vhost->sent);
  1234. init_timer(&evt->timer);
  1235. if (timeout) {
  1236. evt->timer.data = (unsigned long) evt;
  1237. evt->timer.expires = jiffies + (timeout * HZ);
  1238. evt->timer.function = (void (*)(unsigned long))ibmvfc_timeout;
  1239. add_timer(&evt->timer);
  1240. }
  1241. if ((rc = ibmvfc_send_crq(vhost, crq_as_u64[0], crq_as_u64[1]))) {
  1242. list_del(&evt->queue);
  1243. del_timer(&evt->timer);
  1244. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  1245. * Firmware will send a CRQ with a transport event (0xFF) to
  1246. * tell this client what has happened to the transport. This
  1247. * will be handled in ibmvfc_handle_crq()
  1248. */
  1249. if (rc == H_CLOSED) {
  1250. if (printk_ratelimit())
  1251. dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
  1252. if (evt->cmnd)
  1253. scsi_dma_unmap(evt->cmnd);
  1254. ibmvfc_free_event(evt);
  1255. return SCSI_MLQUEUE_HOST_BUSY;
  1256. }
  1257. dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
  1258. if (evt->cmnd) {
  1259. evt->cmnd->result = DID_ERROR << 16;
  1260. evt->done = ibmvfc_scsi_eh_done;
  1261. } else
  1262. evt->xfer_iu->mad_common.status = IBMVFC_MAD_CRQ_ERROR;
  1263. evt->done(evt);
  1264. } else
  1265. ibmvfc_trc_start(evt);
  1266. return 0;
  1267. }
  1268. /**
  1269. * ibmvfc_log_error - Log an error for the failed command if appropriate
  1270. * @evt: ibmvfc event to log
  1271. *
  1272. **/
  1273. static void ibmvfc_log_error(struct ibmvfc_event *evt)
  1274. {
  1275. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  1276. struct ibmvfc_host *vhost = evt->vhost;
  1277. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  1278. struct scsi_cmnd *cmnd = evt->cmnd;
  1279. const char *err = unknown_error;
  1280. int index = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
  1281. int logerr = 0;
  1282. int rsp_code = 0;
  1283. if (index >= 0) {
  1284. logerr = cmd_status[index].log;
  1285. err = cmd_status[index].name;
  1286. }
  1287. if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
  1288. return;
  1289. if (rsp->flags & FCP_RSP_LEN_VALID)
  1290. rsp_code = rsp->data.info.rsp_code;
  1291. scmd_printk(KERN_ERR, cmnd, "Command (%02X) failed: %s (%x:%x) "
  1292. "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
  1293. cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error,
  1294. rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
  1295. }
  1296. /**
  1297. * ibmvfc_scsi_done - Handle responses from commands
  1298. * @evt: ibmvfc event to be handled
  1299. *
  1300. * Used as a callback when sending scsi cmds.
  1301. **/
  1302. static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
  1303. {
  1304. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  1305. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  1306. struct scsi_cmnd *cmnd = evt->cmnd;
  1307. u32 rsp_len = 0;
  1308. u32 sense_len = rsp->fcp_sense_len;
  1309. if (cmnd) {
  1310. if (vfc_cmd->response_flags & IBMVFC_ADAPTER_RESID_VALID)
  1311. scsi_set_resid(cmnd, vfc_cmd->adapter_resid);
  1312. else if (rsp->flags & FCP_RESID_UNDER)
  1313. scsi_set_resid(cmnd, rsp->fcp_resid);
  1314. else
  1315. scsi_set_resid(cmnd, 0);
  1316. if (vfc_cmd->status) {
  1317. cmnd->result = ibmvfc_get_err_result(vfc_cmd);
  1318. if (rsp->flags & FCP_RSP_LEN_VALID)
  1319. rsp_len = rsp->fcp_rsp_len;
  1320. if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
  1321. sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
  1322. if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
  1323. memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
  1324. ibmvfc_log_error(evt);
  1325. }
  1326. if (!cmnd->result &&
  1327. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
  1328. cmnd->result = (DID_ERROR << 16);
  1329. scsi_dma_unmap(cmnd);
  1330. cmnd->scsi_done(cmnd);
  1331. }
  1332. ibmvfc_free_event(evt);
  1333. }
  1334. /**
  1335. * ibmvfc_host_chkready - Check if the host can accept commands
  1336. * @vhost: struct ibmvfc host
  1337. *
  1338. * Returns:
  1339. * 1 if host can accept command / 0 if not
  1340. **/
  1341. static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
  1342. {
  1343. int result = 0;
  1344. switch (vhost->state) {
  1345. case IBMVFC_LINK_DEAD:
  1346. case IBMVFC_HOST_OFFLINE:
  1347. result = DID_NO_CONNECT << 16;
  1348. break;
  1349. case IBMVFC_NO_CRQ:
  1350. case IBMVFC_INITIALIZING:
  1351. case IBMVFC_HALTED:
  1352. case IBMVFC_LINK_DOWN:
  1353. result = DID_REQUEUE << 16;
  1354. break;
  1355. case IBMVFC_ACTIVE:
  1356. result = 0;
  1357. break;
  1358. };
  1359. return result;
  1360. }
  1361. /**
  1362. * ibmvfc_queuecommand - The queuecommand function of the scsi template
  1363. * @cmnd: struct scsi_cmnd to be executed
  1364. * @done: Callback function to be called when cmnd is completed
  1365. *
  1366. * Returns:
  1367. * 0 on success / other on failure
  1368. **/
  1369. static int ibmvfc_queuecommand(struct scsi_cmnd *cmnd,
  1370. void (*done) (struct scsi_cmnd *))
  1371. {
  1372. struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
  1373. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  1374. struct ibmvfc_cmd *vfc_cmd;
  1375. struct ibmvfc_event *evt;
  1376. u8 tag[2];
  1377. int rc;
  1378. if (unlikely((rc = fc_remote_port_chkready(rport))) ||
  1379. unlikely((rc = ibmvfc_host_chkready(vhost)))) {
  1380. cmnd->result = rc;
  1381. done(cmnd);
  1382. return 0;
  1383. }
  1384. cmnd->result = (DID_OK << 16);
  1385. evt = ibmvfc_get_event(vhost);
  1386. ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
  1387. evt->cmnd = cmnd;
  1388. cmnd->scsi_done = done;
  1389. vfc_cmd = &evt->iu.cmd;
  1390. memset(vfc_cmd, 0, sizeof(*vfc_cmd));
  1391. vfc_cmd->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
  1392. vfc_cmd->resp.len = sizeof(vfc_cmd->rsp);
  1393. vfc_cmd->frame_type = IBMVFC_SCSI_FCP_TYPE;
  1394. vfc_cmd->payload_len = sizeof(vfc_cmd->iu);
  1395. vfc_cmd->resp_len = sizeof(vfc_cmd->rsp);
  1396. vfc_cmd->cancel_key = (unsigned long)cmnd->device->hostdata;
  1397. vfc_cmd->tgt_scsi_id = rport->port_id;
  1398. if ((rport->supported_classes & FC_COS_CLASS3) &&
  1399. (fc_host_supported_classes(vhost->host) & FC_COS_CLASS3))
  1400. vfc_cmd->flags = IBMVFC_CLASS_3_ERR;
  1401. vfc_cmd->iu.xfer_len = scsi_bufflen(cmnd);
  1402. int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
  1403. memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
  1404. if (scsi_populate_tag_msg(cmnd, tag)) {
  1405. vfc_cmd->task_tag = tag[1];
  1406. switch (tag[0]) {
  1407. case MSG_SIMPLE_TAG:
  1408. vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
  1409. break;
  1410. case MSG_HEAD_TAG:
  1411. vfc_cmd->iu.pri_task_attr = IBMVFC_HEAD_OF_QUEUE;
  1412. break;
  1413. case MSG_ORDERED_TAG:
  1414. vfc_cmd->iu.pri_task_attr = IBMVFC_ORDERED_TASK;
  1415. break;
  1416. };
  1417. }
  1418. if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
  1419. return ibmvfc_send_event(evt, vhost, 0);
  1420. ibmvfc_free_event(evt);
  1421. if (rc == -ENOMEM)
  1422. return SCSI_MLQUEUE_HOST_BUSY;
  1423. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1424. scmd_printk(KERN_ERR, cmnd,
  1425. "Failed to map DMA buffer for command. rc=%d\n", rc);
  1426. cmnd->result = DID_ERROR << 16;
  1427. done(cmnd);
  1428. return 0;
  1429. }
  1430. /**
  1431. * ibmvfc_sync_completion - Signal that a synchronous command has completed
  1432. * @evt: ibmvfc event struct
  1433. *
  1434. **/
  1435. static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
  1436. {
  1437. /* copy the response back */
  1438. if (evt->sync_iu)
  1439. *evt->sync_iu = *evt->xfer_iu;
  1440. complete(&evt->comp);
  1441. }
  1442. /**
  1443. * ibmvfc_reset_device - Reset the device with the specified reset type
  1444. * @sdev: scsi device to reset
  1445. * @type: reset type
  1446. * @desc: reset type description for log messages
  1447. *
  1448. * Returns:
  1449. * 0 on success / other on failure
  1450. **/
  1451. static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
  1452. {
  1453. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1454. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1455. struct ibmvfc_cmd *tmf;
  1456. struct ibmvfc_event *evt;
  1457. union ibmvfc_iu rsp_iu;
  1458. struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
  1459. int rsp_rc = -EBUSY;
  1460. unsigned long flags;
  1461. int rsp_code = 0;
  1462. spin_lock_irqsave(vhost->host->host_lock, flags);
  1463. if (vhost->state == IBMVFC_ACTIVE) {
  1464. evt = ibmvfc_get_event(vhost);
  1465. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
  1466. tmf = &evt->iu.cmd;
  1467. memset(tmf, 0, sizeof(*tmf));
  1468. tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
  1469. tmf->resp.len = sizeof(tmf->rsp);
  1470. tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
  1471. tmf->payload_len = sizeof(tmf->iu);
  1472. tmf->resp_len = sizeof(tmf->rsp);
  1473. tmf->cancel_key = (unsigned long)sdev->hostdata;
  1474. tmf->tgt_scsi_id = rport->port_id;
  1475. int_to_scsilun(sdev->lun, &tmf->iu.lun);
  1476. tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
  1477. tmf->iu.tmf_flags = type;
  1478. evt->sync_iu = &rsp_iu;
  1479. init_completion(&evt->comp);
  1480. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1481. }
  1482. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1483. if (rsp_rc != 0) {
  1484. sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
  1485. desc, rsp_rc);
  1486. return -EIO;
  1487. }
  1488. sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
  1489. wait_for_completion(&evt->comp);
  1490. if (rsp_iu.cmd.status) {
  1491. if (fc_rsp->flags & FCP_RSP_LEN_VALID)
  1492. rsp_code = fc_rsp->data.info.rsp_code;
  1493. sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
  1494. "flags: %x fcp_rsp: %x, scsi_status: %x\n",
  1495. desc, ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
  1496. rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
  1497. fc_rsp->scsi_status);
  1498. rsp_rc = -EIO;
  1499. } else
  1500. sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
  1501. spin_lock_irqsave(vhost->host->host_lock, flags);
  1502. ibmvfc_free_event(evt);
  1503. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1504. return rsp_rc;
  1505. }
  1506. /**
  1507. * ibmvfc_abort_task_set - Abort outstanding commands to the device
  1508. * @sdev: scsi device to abort commands
  1509. *
  1510. * This sends an Abort Task Set to the VIOS for the specified device. This does
  1511. * NOT send any cancel to the VIOS. That must be done separately.
  1512. *
  1513. * Returns:
  1514. * 0 on success / other on failure
  1515. **/
  1516. static int ibmvfc_abort_task_set(struct scsi_device *sdev)
  1517. {
  1518. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1519. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1520. struct ibmvfc_cmd *tmf;
  1521. struct ibmvfc_event *evt, *found_evt;
  1522. union ibmvfc_iu rsp_iu;
  1523. struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
  1524. int rsp_rc = -EBUSY;
  1525. unsigned long flags;
  1526. int rsp_code = 0;
  1527. spin_lock_irqsave(vhost->host->host_lock, flags);
  1528. found_evt = NULL;
  1529. list_for_each_entry(evt, &vhost->sent, queue) {
  1530. if (evt->cmnd && evt->cmnd->device == sdev) {
  1531. found_evt = evt;
  1532. break;
  1533. }
  1534. }
  1535. if (!found_evt) {
  1536. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1537. sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
  1538. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1539. return 0;
  1540. }
  1541. if (vhost->state == IBMVFC_ACTIVE) {
  1542. evt = ibmvfc_get_event(vhost);
  1543. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
  1544. tmf = &evt->iu.cmd;
  1545. memset(tmf, 0, sizeof(*tmf));
  1546. tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
  1547. tmf->resp.len = sizeof(tmf->rsp);
  1548. tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
  1549. tmf->payload_len = sizeof(tmf->iu);
  1550. tmf->resp_len = sizeof(tmf->rsp);
  1551. tmf->cancel_key = (unsigned long)sdev->hostdata;
  1552. tmf->tgt_scsi_id = rport->port_id;
  1553. int_to_scsilun(sdev->lun, &tmf->iu.lun);
  1554. tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
  1555. tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
  1556. evt->sync_iu = &rsp_iu;
  1557. init_completion(&evt->comp);
  1558. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1559. }
  1560. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1561. if (rsp_rc != 0) {
  1562. sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
  1563. return -EIO;
  1564. }
  1565. sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
  1566. wait_for_completion(&evt->comp);
  1567. if (rsp_iu.cmd.status) {
  1568. if (fc_rsp->flags & FCP_RSP_LEN_VALID)
  1569. rsp_code = fc_rsp->data.info.rsp_code;
  1570. sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
  1571. "flags: %x fcp_rsp: %x, scsi_status: %x\n",
  1572. ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
  1573. rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
  1574. fc_rsp->scsi_status);
  1575. rsp_rc = -EIO;
  1576. } else
  1577. sdev_printk(KERN_INFO, sdev, "Abort successful\n");
  1578. spin_lock_irqsave(vhost->host->host_lock, flags);
  1579. ibmvfc_free_event(evt);
  1580. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1581. return rsp_rc;
  1582. }
  1583. /**
  1584. * ibmvfc_cancel_all - Cancel all outstanding commands to the device
  1585. * @sdev: scsi device to cancel commands
  1586. * @type: type of error recovery being performed
  1587. *
  1588. * This sends a cancel to the VIOS for the specified device. This does
  1589. * NOT send any abort to the actual device. That must be done separately.
  1590. *
  1591. * Returns:
  1592. * 0 on success / other on failure
  1593. **/
  1594. static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
  1595. {
  1596. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1597. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1598. struct ibmvfc_tmf *tmf;
  1599. struct ibmvfc_event *evt, *found_evt;
  1600. union ibmvfc_iu rsp;
  1601. int rsp_rc = -EBUSY;
  1602. unsigned long flags;
  1603. u16 status;
  1604. ENTER;
  1605. spin_lock_irqsave(vhost->host->host_lock, flags);
  1606. found_evt = NULL;
  1607. list_for_each_entry(evt, &vhost->sent, queue) {
  1608. if (evt->cmnd && evt->cmnd->device == sdev) {
  1609. found_evt = evt;
  1610. break;
  1611. }
  1612. }
  1613. if (!found_evt) {
  1614. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1615. sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
  1616. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1617. return 0;
  1618. }
  1619. if (vhost->state == IBMVFC_ACTIVE) {
  1620. evt = ibmvfc_get_event(vhost);
  1621. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  1622. tmf = &evt->iu.tmf;
  1623. memset(tmf, 0, sizeof(*tmf));
  1624. tmf->common.version = 1;
  1625. tmf->common.opcode = IBMVFC_TMF_MAD;
  1626. tmf->common.length = sizeof(*tmf);
  1627. tmf->scsi_id = rport->port_id;
  1628. int_to_scsilun(sdev->lun, &tmf->lun);
  1629. tmf->flags = (type | IBMVFC_TMF_LUA_VALID);
  1630. tmf->cancel_key = (unsigned long)sdev->hostdata;
  1631. tmf->my_cancel_key = (IBMVFC_TMF_CANCEL_KEY | (unsigned long)sdev->hostdata);
  1632. evt->sync_iu = &rsp;
  1633. init_completion(&evt->comp);
  1634. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1635. }
  1636. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1637. if (rsp_rc != 0) {
  1638. sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
  1639. return -EIO;
  1640. }
  1641. sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
  1642. wait_for_completion(&evt->comp);
  1643. status = rsp.mad_common.status;
  1644. spin_lock_irqsave(vhost->host->host_lock, flags);
  1645. ibmvfc_free_event(evt);
  1646. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1647. if (status != IBMVFC_MAD_SUCCESS) {
  1648. sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
  1649. return -EIO;
  1650. }
  1651. sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
  1652. return 0;
  1653. }
  1654. /**
  1655. * ibmvfc_eh_abort_handler - Abort a command
  1656. * @cmd: scsi command to abort
  1657. *
  1658. * Returns:
  1659. * SUCCESS / FAILED
  1660. **/
  1661. static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
  1662. {
  1663. struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
  1664. struct ibmvfc_event *evt, *pos;
  1665. int cancel_rc, abort_rc;
  1666. unsigned long flags;
  1667. ENTER;
  1668. ibmvfc_wait_while_resetting(vhost);
  1669. cancel_rc = ibmvfc_cancel_all(cmd->device, IBMVFC_TMF_ABORT_TASK_SET);
  1670. abort_rc = ibmvfc_abort_task_set(cmd->device);
  1671. if (!cancel_rc && !abort_rc) {
  1672. spin_lock_irqsave(vhost->host->host_lock, flags);
  1673. list_for_each_entry_safe(evt, pos, &vhost->sent, queue) {
  1674. if (evt->cmnd && evt->cmnd->device == cmd->device)
  1675. ibmvfc_fail_request(evt, DID_ABORT);
  1676. }
  1677. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1678. LEAVE;
  1679. return SUCCESS;
  1680. }
  1681. LEAVE;
  1682. return FAILED;
  1683. }
  1684. /**
  1685. * ibmvfc_eh_device_reset_handler - Reset a single LUN
  1686. * @cmd: scsi command struct
  1687. *
  1688. * Returns:
  1689. * SUCCESS / FAILED
  1690. **/
  1691. static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
  1692. {
  1693. struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
  1694. struct ibmvfc_event *evt, *pos;
  1695. int cancel_rc, reset_rc;
  1696. unsigned long flags;
  1697. ENTER;
  1698. ibmvfc_wait_while_resetting(vhost);
  1699. cancel_rc = ibmvfc_cancel_all(cmd->device, IBMVFC_TMF_LUN_RESET);
  1700. reset_rc = ibmvfc_reset_device(cmd->device, IBMVFC_LUN_RESET, "LUN");
  1701. if (!cancel_rc && !reset_rc) {
  1702. spin_lock_irqsave(vhost->host->host_lock, flags);
  1703. list_for_each_entry_safe(evt, pos, &vhost->sent, queue) {
  1704. if (evt->cmnd && evt->cmnd->device == cmd->device)
  1705. ibmvfc_fail_request(evt, DID_ABORT);
  1706. }
  1707. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1708. LEAVE;
  1709. return SUCCESS;
  1710. }
  1711. LEAVE;
  1712. return FAILED;
  1713. }
  1714. /**
  1715. * ibmvfc_dev_cancel_all - Device iterated cancel all function
  1716. * @sdev: scsi device struct
  1717. * @data: return code
  1718. *
  1719. **/
  1720. static void ibmvfc_dev_cancel_all(struct scsi_device *sdev, void *data)
  1721. {
  1722. unsigned long *rc = data;
  1723. *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
  1724. }
  1725. /**
  1726. * ibmvfc_dev_abort_all - Device iterated abort task set function
  1727. * @sdev: scsi device struct
  1728. * @data: return code
  1729. *
  1730. **/
  1731. static void ibmvfc_dev_abort_all(struct scsi_device *sdev, void *data)
  1732. {
  1733. unsigned long *rc = data;
  1734. *rc |= ibmvfc_abort_task_set(sdev);
  1735. }
  1736. /**
  1737. * ibmvfc_eh_target_reset_handler - Reset the target
  1738. * @cmd: scsi command struct
  1739. *
  1740. * Returns:
  1741. * SUCCESS / FAILED
  1742. **/
  1743. static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
  1744. {
  1745. struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
  1746. struct scsi_target *starget = scsi_target(cmd->device);
  1747. struct ibmvfc_event *evt, *pos;
  1748. int reset_rc;
  1749. unsigned long cancel_rc = 0;
  1750. unsigned long flags;
  1751. ENTER;
  1752. ibmvfc_wait_while_resetting(vhost);
  1753. starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all);
  1754. reset_rc = ibmvfc_reset_device(cmd->device, IBMVFC_TARGET_RESET, "target");
  1755. if (!cancel_rc && !reset_rc) {
  1756. spin_lock_irqsave(vhost->host->host_lock, flags);
  1757. list_for_each_entry_safe(evt, pos, &vhost->sent, queue) {
  1758. if (evt->cmnd && scsi_target(evt->cmnd->device) == starget)
  1759. ibmvfc_fail_request(evt, DID_ABORT);
  1760. }
  1761. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1762. LEAVE;
  1763. return SUCCESS;
  1764. }
  1765. LEAVE;
  1766. return FAILED;
  1767. }
  1768. /**
  1769. * ibmvfc_eh_host_reset_handler - Reset the connection to the server
  1770. * @cmd: struct scsi_cmnd having problems
  1771. *
  1772. **/
  1773. static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
  1774. {
  1775. int rc;
  1776. struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
  1777. dev_err(vhost->dev, "Resetting connection due to error recovery\n");
  1778. rc = ibmvfc_issue_fc_host_lip(vhost->host);
  1779. return rc ? FAILED : SUCCESS;
  1780. }
  1781. /**
  1782. * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
  1783. * @rport: rport struct
  1784. *
  1785. * Return value:
  1786. * none
  1787. **/
  1788. static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
  1789. {
  1790. struct scsi_target *starget = to_scsi_target(&rport->dev);
  1791. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1792. struct ibmvfc_host *vhost = shost_priv(shost);
  1793. struct ibmvfc_event *evt, *pos;
  1794. unsigned long cancel_rc = 0;
  1795. unsigned long abort_rc = 0;
  1796. unsigned long flags;
  1797. ENTER;
  1798. starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all);
  1799. starget_for_each_device(starget, &abort_rc, ibmvfc_dev_abort_all);
  1800. if (!cancel_rc && !abort_rc) {
  1801. spin_lock_irqsave(shost->host_lock, flags);
  1802. list_for_each_entry_safe(evt, pos, &vhost->sent, queue) {
  1803. if (evt->cmnd && scsi_target(evt->cmnd->device) == starget)
  1804. ibmvfc_fail_request(evt, DID_ABORT);
  1805. }
  1806. spin_unlock_irqrestore(shost->host_lock, flags);
  1807. } else
  1808. ibmvfc_issue_fc_host_lip(shost);
  1809. scsi_target_unblock(&rport->dev);
  1810. LEAVE;
  1811. }
  1812. static const struct {
  1813. enum ibmvfc_async_event ae;
  1814. const char *desc;
  1815. } ae_desc [] = {
  1816. { IBMVFC_AE_ELS_PLOGI, "PLOGI" },
  1817. { IBMVFC_AE_ELS_LOGO, "LOGO" },
  1818. { IBMVFC_AE_ELS_PRLO, "PRLO" },
  1819. { IBMVFC_AE_SCN_NPORT, "N-Port SCN" },
  1820. { IBMVFC_AE_SCN_GROUP, "Group SCN" },
  1821. { IBMVFC_AE_SCN_DOMAIN, "Domain SCN" },
  1822. { IBMVFC_AE_SCN_FABRIC, "Fabric SCN" },
  1823. { IBMVFC_AE_LINK_UP, "Link Up" },
  1824. { IBMVFC_AE_LINK_DOWN, "Link Down" },
  1825. { IBMVFC_AE_LINK_DEAD, "Link Dead" },
  1826. { IBMVFC_AE_HALT, "Halt" },
  1827. { IBMVFC_AE_RESUME, "Resume" },
  1828. { IBMVFC_AE_ADAPTER_FAILED, "Adapter Failed" },
  1829. };
  1830. static const char *unknown_ae = "Unknown async";
  1831. /**
  1832. * ibmvfc_get_ae_desc - Get text description for async event
  1833. * @ae: async event
  1834. *
  1835. **/
  1836. static const char *ibmvfc_get_ae_desc(u64 ae)
  1837. {
  1838. int i;
  1839. for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
  1840. if (ae_desc[i].ae == ae)
  1841. return ae_desc[i].desc;
  1842. return unknown_ae;
  1843. }
  1844. /**
  1845. * ibmvfc_handle_async - Handle an async event from the adapter
  1846. * @crq: crq to process
  1847. * @vhost: ibmvfc host struct
  1848. *
  1849. **/
  1850. static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
  1851. struct ibmvfc_host *vhost)
  1852. {
  1853. const char *desc = ibmvfc_get_ae_desc(crq->event);
  1854. ibmvfc_log(vhost, 3, "%s event received. scsi_id: %lx, wwpn: %lx,"
  1855. " node_name: %lx\n", desc, crq->scsi_id, crq->wwpn, crq->node_name);
  1856. switch (crq->event) {
  1857. case IBMVFC_AE_LINK_UP:
  1858. case IBMVFC_AE_RESUME:
  1859. vhost->events_to_log |= IBMVFC_AE_LINKUP;
  1860. ibmvfc_init_host(vhost, 1);
  1861. break;
  1862. case IBMVFC_AE_SCN_FABRIC:
  1863. vhost->events_to_log |= IBMVFC_AE_RSCN;
  1864. ibmvfc_init_host(vhost, 1);
  1865. break;
  1866. case IBMVFC_AE_SCN_NPORT:
  1867. case IBMVFC_AE_SCN_GROUP:
  1868. case IBMVFC_AE_SCN_DOMAIN:
  1869. vhost->events_to_log |= IBMVFC_AE_RSCN;
  1870. case IBMVFC_AE_ELS_LOGO:
  1871. case IBMVFC_AE_ELS_PRLO:
  1872. case IBMVFC_AE_ELS_PLOGI:
  1873. ibmvfc_reinit_host(vhost);
  1874. break;
  1875. case IBMVFC_AE_LINK_DOWN:
  1876. case IBMVFC_AE_ADAPTER_FAILED:
  1877. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  1878. break;
  1879. case IBMVFC_AE_LINK_DEAD:
  1880. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  1881. break;
  1882. case IBMVFC_AE_HALT:
  1883. ibmvfc_link_down(vhost, IBMVFC_HALTED);
  1884. break;
  1885. default:
  1886. dev_err(vhost->dev, "Unknown async event received: %ld\n", crq->event);
  1887. break;
  1888. };
  1889. }
  1890. /**
  1891. * ibmvfc_handle_crq - Handles and frees received events in the CRQ
  1892. * @crq: Command/Response queue
  1893. * @vhost: ibmvfc host struct
  1894. *
  1895. **/
  1896. static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
  1897. {
  1898. long rc;
  1899. struct ibmvfc_event *evt = (struct ibmvfc_event *)crq->ioba;
  1900. switch (crq->valid) {
  1901. case IBMVFC_CRQ_INIT_RSP:
  1902. switch (crq->format) {
  1903. case IBMVFC_CRQ_INIT:
  1904. dev_info(vhost->dev, "Partner initialized\n");
  1905. /* Send back a response */
  1906. rc = ibmvfc_send_crq_init_complete(vhost);
  1907. if (rc == 0)
  1908. ibmvfc_init_host(vhost, 0);
  1909. else
  1910. dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
  1911. break;
  1912. case IBMVFC_CRQ_INIT_COMPLETE:
  1913. dev_info(vhost->dev, "Partner initialization complete\n");
  1914. ibmvfc_init_host(vhost, 0);
  1915. break;
  1916. default:
  1917. dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
  1918. }
  1919. return;
  1920. case IBMVFC_CRQ_XPORT_EVENT:
  1921. vhost->state = IBMVFC_NO_CRQ;
  1922. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  1923. if (crq->format == IBMVFC_PARTITION_MIGRATED) {
  1924. /* We need to re-setup the interpartition connection */
  1925. dev_info(vhost->dev, "Re-enabling adapter\n");
  1926. vhost->client_migrated = 1;
  1927. ibmvfc_purge_requests(vhost, DID_REQUEUE);
  1928. if ((rc = ibmvfc_reenable_crq_queue(vhost)) ||
  1929. (rc = ibmvfc_send_crq_init(vhost))) {
  1930. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  1931. dev_err(vhost->dev, "Error after enable (rc=%ld)\n", rc);
  1932. } else
  1933. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  1934. } else {
  1935. dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
  1936. ibmvfc_purge_requests(vhost, DID_ERROR);
  1937. if ((rc = ibmvfc_reset_crq(vhost)) ||
  1938. (rc = ibmvfc_send_crq_init(vhost))) {
  1939. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  1940. dev_err(vhost->dev, "Error after reset (rc=%ld)\n", rc);
  1941. } else
  1942. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  1943. }
  1944. return;
  1945. case IBMVFC_CRQ_CMD_RSP:
  1946. break;
  1947. default:
  1948. dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
  1949. return;
  1950. }
  1951. if (crq->format == IBMVFC_ASYNC_EVENT)
  1952. return;
  1953. /* The only kind of payload CRQs we should get are responses to
  1954. * things we send. Make sure this response is to something we
  1955. * actually sent
  1956. */
  1957. if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
  1958. dev_err(vhost->dev, "Returned correlation_token 0x%08lx is invalid!\n",
  1959. crq->ioba);
  1960. return;
  1961. }
  1962. if (unlikely(atomic_read(&evt->free))) {
  1963. dev_err(vhost->dev, "Received duplicate correlation_token 0x%08lx!\n",
  1964. crq->ioba);
  1965. return;
  1966. }
  1967. del_timer(&evt->timer);
  1968. list_del(&evt->queue);
  1969. ibmvfc_trc_end(evt);
  1970. evt->done(evt);
  1971. }
  1972. /**
  1973. * ibmvfc_scan_finished - Check if the device scan is done.
  1974. * @shost: scsi host struct
  1975. * @time: current elapsed time
  1976. *
  1977. * Returns:
  1978. * 0 if scan is not done / 1 if scan is done
  1979. **/
  1980. static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
  1981. {
  1982. unsigned long flags;
  1983. struct ibmvfc_host *vhost = shost_priv(shost);
  1984. int done = 0;
  1985. spin_lock_irqsave(shost->host_lock, flags);
  1986. if (time >= (init_timeout * HZ)) {
  1987. dev_info(vhost->dev, "Scan taking longer than %d seconds, "
  1988. "continuing initialization\n", init_timeout);
  1989. done = 1;
  1990. }
  1991. if (vhost->state != IBMVFC_NO_CRQ && vhost->action == IBMVFC_HOST_ACTION_NONE)
  1992. done = 1;
  1993. spin_unlock_irqrestore(shost->host_lock, flags);
  1994. return done;
  1995. }
  1996. /**
  1997. * ibmvfc_slave_alloc - Setup the device's task set value
  1998. * @sdev: struct scsi_device device to configure
  1999. *
  2000. * Set the device's task set value so that error handling works as
  2001. * expected.
  2002. *
  2003. * Returns:
  2004. * 0 on success / -ENXIO if device does not exist
  2005. **/
  2006. static int ibmvfc_slave_alloc(struct scsi_device *sdev)
  2007. {
  2008. struct Scsi_Host *shost = sdev->host;
  2009. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  2010. struct ibmvfc_host *vhost = shost_priv(shost);
  2011. unsigned long flags = 0;
  2012. if (!rport || fc_remote_port_chkready(rport))
  2013. return -ENXIO;
  2014. spin_lock_irqsave(shost->host_lock, flags);
  2015. sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
  2016. spin_unlock_irqrestore(shost->host_lock, flags);
  2017. return 0;
  2018. }
  2019. /**
  2020. * ibmvfc_slave_configure - Configure the device
  2021. * @sdev: struct scsi_device device to configure
  2022. *
  2023. * Enable allow_restart for a device if it is a disk. Adjust the
  2024. * queue_depth here also.
  2025. *
  2026. * Returns:
  2027. * 0
  2028. **/
  2029. static int ibmvfc_slave_configure(struct scsi_device *sdev)
  2030. {
  2031. struct Scsi_Host *shost = sdev->host;
  2032. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  2033. unsigned long flags = 0;
  2034. spin_lock_irqsave(shost->host_lock, flags);
  2035. if (sdev->type == TYPE_DISK)
  2036. sdev->allow_restart = 1;
  2037. if (sdev->tagged_supported) {
  2038. scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
  2039. scsi_activate_tcq(sdev, sdev->queue_depth);
  2040. } else
  2041. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  2042. rport->dev_loss_tmo = dev_loss_tmo;
  2043. spin_unlock_irqrestore(shost->host_lock, flags);
  2044. return 0;
  2045. }
  2046. /**
  2047. * ibmvfc_change_queue_depth - Change the device's queue depth
  2048. * @sdev: scsi device struct
  2049. * @qdepth: depth to set
  2050. *
  2051. * Return value:
  2052. * actual depth set
  2053. **/
  2054. static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
  2055. {
  2056. if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
  2057. qdepth = IBMVFC_MAX_CMDS_PER_LUN;
  2058. scsi_adjust_queue_depth(sdev, 0, qdepth);
  2059. return sdev->queue_depth;
  2060. }
  2061. /**
  2062. * ibmvfc_change_queue_type - Change the device's queue type
  2063. * @sdev: scsi device struct
  2064. * @tag_type: type of tags to use
  2065. *
  2066. * Return value:
  2067. * actual queue type set
  2068. **/
  2069. static int ibmvfc_change_queue_type(struct scsi_device *sdev, int tag_type)
  2070. {
  2071. if (sdev->tagged_supported) {
  2072. scsi_set_tag_type(sdev, tag_type);
  2073. if (tag_type)
  2074. scsi_activate_tcq(sdev, sdev->queue_depth);
  2075. else
  2076. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  2077. } else
  2078. tag_type = 0;
  2079. return tag_type;
  2080. }
  2081. static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
  2082. struct device_attribute *attr, char *buf)
  2083. {
  2084. struct Scsi_Host *shost = class_to_shost(dev);
  2085. struct ibmvfc_host *vhost = shost_priv(shost);
  2086. return snprintf(buf, PAGE_SIZE, "%s\n",
  2087. vhost->login_buf->resp.partition_name);
  2088. }
  2089. static struct device_attribute ibmvfc_host_partition_name = {
  2090. .attr = {
  2091. .name = "partition_name",
  2092. .mode = S_IRUGO,
  2093. },
  2094. .show = ibmvfc_show_host_partition_name,
  2095. };
  2096. static ssize_t ibmvfc_show_host_device_name(struct device *dev,
  2097. struct device_attribute *attr, char *buf)
  2098. {
  2099. struct Scsi_Host *shost = class_to_shost(dev);
  2100. struct ibmvfc_host *vhost = shost_priv(shost);
  2101. return snprintf(buf, PAGE_SIZE, "%s\n",
  2102. vhost->login_buf->resp.device_name);
  2103. }
  2104. static struct device_attribute ibmvfc_host_device_name = {
  2105. .attr = {
  2106. .name = "device_name",
  2107. .mode = S_IRUGO,
  2108. },
  2109. .show = ibmvfc_show_host_device_name,
  2110. };
  2111. static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
  2112. struct device_attribute *attr, char *buf)
  2113. {
  2114. struct Scsi_Host *shost = class_to_shost(dev);
  2115. struct ibmvfc_host *vhost = shost_priv(shost);
  2116. return snprintf(buf, PAGE_SIZE, "%s\n",
  2117. vhost->login_buf->resp.port_loc_code);
  2118. }
  2119. static struct device_attribute ibmvfc_host_loc_code = {
  2120. .attr = {
  2121. .name = "port_loc_code",
  2122. .mode = S_IRUGO,
  2123. },
  2124. .show = ibmvfc_show_host_loc_code,
  2125. };
  2126. static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
  2127. struct device_attribute *attr, char *buf)
  2128. {
  2129. struct Scsi_Host *shost = class_to_shost(dev);
  2130. struct ibmvfc_host *vhost = shost_priv(shost);
  2131. return snprintf(buf, PAGE_SIZE, "%s\n",
  2132. vhost->login_buf->resp.drc_name);
  2133. }
  2134. static struct device_attribute ibmvfc_host_drc_name = {
  2135. .attr = {
  2136. .name = "drc_name",
  2137. .mode = S_IRUGO,
  2138. },
  2139. .show = ibmvfc_show_host_drc_name,
  2140. };
  2141. static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
  2142. struct device_attribute *attr, char *buf)
  2143. {
  2144. struct Scsi_Host *shost = class_to_shost(dev);
  2145. struct ibmvfc_host *vhost = shost_priv(shost);
  2146. return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
  2147. }
  2148. static struct device_attribute ibmvfc_host_npiv_version = {
  2149. .attr = {
  2150. .name = "npiv_version",
  2151. .mode = S_IRUGO,
  2152. },
  2153. .show = ibmvfc_show_host_npiv_version,
  2154. };
  2155. /**
  2156. * ibmvfc_show_log_level - Show the adapter's error logging level
  2157. * @dev: class device struct
  2158. * @buf: buffer
  2159. *
  2160. * Return value:
  2161. * number of bytes printed to buffer
  2162. **/
  2163. static ssize_t ibmvfc_show_log_level(struct device *dev,
  2164. struct device_attribute *attr, char *buf)
  2165. {
  2166. struct Scsi_Host *shost = class_to_shost(dev);
  2167. struct ibmvfc_host *vhost = shost_priv(shost);
  2168. unsigned long flags = 0;
  2169. int len;
  2170. spin_lock_irqsave(shost->host_lock, flags);
  2171. len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
  2172. spin_unlock_irqrestore(shost->host_lock, flags);
  2173. return len;
  2174. }
  2175. /**
  2176. * ibmvfc_store_log_level - Change the adapter's error logging level
  2177. * @dev: class device struct
  2178. * @buf: buffer
  2179. *
  2180. * Return value:
  2181. * number of bytes printed to buffer
  2182. **/
  2183. static ssize_t ibmvfc_store_log_level(struct device *dev,
  2184. struct device_attribute *attr,
  2185. const char *buf, size_t count)
  2186. {
  2187. struct Scsi_Host *shost = class_to_shost(dev);
  2188. struct ibmvfc_host *vhost = shost_priv(shost);
  2189. unsigned long flags = 0;
  2190. spin_lock_irqsave(shost->host_lock, flags);
  2191. vhost->log_level = simple_strtoul(buf, NULL, 10);
  2192. spin_unlock_irqrestore(shost->host_lock, flags);
  2193. return strlen(buf);
  2194. }
  2195. static struct device_attribute ibmvfc_log_level_attr = {
  2196. .attr = {
  2197. .name = "log_level",
  2198. .mode = S_IRUGO | S_IWUSR,
  2199. },
  2200. .show = ibmvfc_show_log_level,
  2201. .store = ibmvfc_store_log_level
  2202. };
  2203. #ifdef CONFIG_SCSI_IBMVFC_TRACE
  2204. /**
  2205. * ibmvfc_read_trace - Dump the adapter trace
  2206. * @kobj: kobject struct
  2207. * @bin_attr: bin_attribute struct
  2208. * @buf: buffer
  2209. * @off: offset
  2210. * @count: buffer size
  2211. *
  2212. * Return value:
  2213. * number of bytes printed to buffer
  2214. **/
  2215. static ssize_t ibmvfc_read_trace(struct kobject *kobj,
  2216. struct bin_attribute *bin_attr,
  2217. char *buf, loff_t off, size_t count)
  2218. {
  2219. struct device *dev = container_of(kobj, struct device, kobj);
  2220. struct Scsi_Host *shost = class_to_shost(dev);
  2221. struct ibmvfc_host *vhost = shost_priv(shost);
  2222. unsigned long flags = 0;
  2223. int size = IBMVFC_TRACE_SIZE;
  2224. char *src = (char *)vhost->trace;
  2225. if (off > size)
  2226. return 0;
  2227. if (off + count > size) {
  2228. size -= off;
  2229. count = size;
  2230. }
  2231. spin_lock_irqsave(shost->host_lock, flags);
  2232. memcpy(buf, &src[off], count);
  2233. spin_unlock_irqrestore(shost->host_lock, flags);
  2234. return count;
  2235. }
  2236. static struct bin_attribute ibmvfc_trace_attr = {
  2237. .attr = {
  2238. .name = "trace",
  2239. .mode = S_IRUGO,
  2240. },
  2241. .size = 0,
  2242. .read = ibmvfc_read_trace,
  2243. };
  2244. #endif
  2245. static struct device_attribute *ibmvfc_attrs[] = {
  2246. &ibmvfc_host_partition_name,
  2247. &ibmvfc_host_device_name,
  2248. &ibmvfc_host_loc_code,
  2249. &ibmvfc_host_drc_name,
  2250. &ibmvfc_host_npiv_version,
  2251. &ibmvfc_log_level_attr,
  2252. NULL
  2253. };
  2254. static struct scsi_host_template driver_template = {
  2255. .module = THIS_MODULE,
  2256. .name = "IBM POWER Virtual FC Adapter",
  2257. .proc_name = IBMVFC_NAME,
  2258. .queuecommand = ibmvfc_queuecommand,
  2259. .eh_abort_handler = ibmvfc_eh_abort_handler,
  2260. .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
  2261. .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
  2262. .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
  2263. .slave_alloc = ibmvfc_slave_alloc,
  2264. .slave_configure = ibmvfc_slave_configure,
  2265. .scan_finished = ibmvfc_scan_finished,
  2266. .change_queue_depth = ibmvfc_change_queue_depth,
  2267. .change_queue_type = ibmvfc_change_queue_type,
  2268. .cmd_per_lun = 16,
  2269. .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
  2270. .this_id = -1,
  2271. .sg_tablesize = SG_ALL,
  2272. .max_sectors = IBMVFC_MAX_SECTORS,
  2273. .use_clustering = ENABLE_CLUSTERING,
  2274. .shost_attrs = ibmvfc_attrs,
  2275. };
  2276. /**
  2277. * ibmvfc_next_async_crq - Returns the next entry in async queue
  2278. * @vhost: ibmvfc host struct
  2279. *
  2280. * Returns:
  2281. * Pointer to next entry in queue / NULL if empty
  2282. **/
  2283. static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
  2284. {
  2285. struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
  2286. struct ibmvfc_async_crq *crq;
  2287. crq = &async_crq->msgs[async_crq->cur];
  2288. if (crq->valid & 0x80) {
  2289. if (++async_crq->cur == async_crq->size)
  2290. async_crq->cur = 0;
  2291. } else
  2292. crq = NULL;
  2293. return crq;
  2294. }
  2295. /**
  2296. * ibmvfc_next_crq - Returns the next entry in message queue
  2297. * @vhost: ibmvfc host struct
  2298. *
  2299. * Returns:
  2300. * Pointer to next entry in queue / NULL if empty
  2301. **/
  2302. static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
  2303. {
  2304. struct ibmvfc_crq_queue *queue = &vhost->crq;
  2305. struct ibmvfc_crq *crq;
  2306. crq = &queue->msgs[queue->cur];
  2307. if (crq->valid & 0x80) {
  2308. if (++queue->cur == queue->size)
  2309. queue->cur = 0;
  2310. } else
  2311. crq = NULL;
  2312. return crq;
  2313. }
  2314. /**
  2315. * ibmvfc_interrupt - Interrupt handler
  2316. * @irq: number of irq to handle, not used
  2317. * @dev_instance: ibmvfc_host that received interrupt
  2318. *
  2319. * Returns:
  2320. * IRQ_HANDLED
  2321. **/
  2322. static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
  2323. {
  2324. struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
  2325. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  2326. struct ibmvfc_crq *crq;
  2327. struct ibmvfc_async_crq *async;
  2328. unsigned long flags;
  2329. int done = 0;
  2330. spin_lock_irqsave(vhost->host->host_lock, flags);
  2331. vio_disable_interrupts(to_vio_dev(vhost->dev));
  2332. while (!done) {
  2333. /* Pull all the valid messages off the CRQ */
  2334. while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
  2335. ibmvfc_handle_crq(crq, vhost);
  2336. crq->valid = 0;
  2337. }
  2338. /* Pull all the valid messages off the async CRQ */
  2339. while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
  2340. ibmvfc_handle_async(async, vhost);
  2341. async->valid = 0;
  2342. }
  2343. vio_enable_interrupts(vdev);
  2344. if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
  2345. vio_disable_interrupts(vdev);
  2346. ibmvfc_handle_crq(crq, vhost);
  2347. crq->valid = 0;
  2348. } else if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
  2349. vio_disable_interrupts(vdev);
  2350. ibmvfc_handle_async(async, vhost);
  2351. crq->valid = 0;
  2352. } else
  2353. done = 1;
  2354. }
  2355. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2356. return IRQ_HANDLED;
  2357. }
  2358. /**
  2359. * ibmvfc_init_tgt - Set the next init job step for the target
  2360. * @tgt: ibmvfc target struct
  2361. * @job_step: job step to perform
  2362. *
  2363. **/
  2364. static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
  2365. void (*job_step) (struct ibmvfc_target *))
  2366. {
  2367. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
  2368. tgt->job_step = job_step;
  2369. wake_up(&tgt->vhost->work_wait_q);
  2370. }
  2371. /**
  2372. * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
  2373. * @tgt: ibmvfc target struct
  2374. * @job_step: initialization job step
  2375. *
  2376. **/
  2377. static void ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
  2378. void (*job_step) (struct ibmvfc_target *))
  2379. {
  2380. if (++tgt->init_retries > IBMVFC_MAX_INIT_RETRIES) {
  2381. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2382. wake_up(&tgt->vhost->work_wait_q);
  2383. } else
  2384. ibmvfc_init_tgt(tgt, job_step);
  2385. }
  2386. /**
  2387. * ibmvfc_tgt_prli_done - Completion handler for Process Login
  2388. * @evt: ibmvfc event struct
  2389. *
  2390. **/
  2391. static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
  2392. {
  2393. struct ibmvfc_target *tgt = evt->tgt;
  2394. struct ibmvfc_host *vhost = evt->vhost;
  2395. struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
  2396. u32 status = rsp->common.status;
  2397. vhost->discovery_threads--;
  2398. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2399. switch (status) {
  2400. case IBMVFC_MAD_SUCCESS:
  2401. tgt_dbg(tgt, "Process Login succeeded\n");
  2402. tgt->need_login = 0;
  2403. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_ADD_RPORT);
  2404. break;
  2405. case IBMVFC_MAD_DRIVER_FAILED:
  2406. break;
  2407. case IBMVFC_MAD_CRQ_ERROR:
  2408. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  2409. break;
  2410. case IBMVFC_MAD_FAILED:
  2411. default:
  2412. tgt_err(tgt, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
  2413. ibmvfc_get_cmd_error(rsp->status, rsp->error),
  2414. rsp->status, rsp->error, status);
  2415. if (ibmvfc_retry_cmd(rsp->status, rsp->error))
  2416. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  2417. break;
  2418. };
  2419. kref_put(&tgt->kref, ibmvfc_release_tgt);
  2420. ibmvfc_free_event(evt);
  2421. wake_up(&vhost->work_wait_q);
  2422. }
  2423. /**
  2424. * ibmvfc_tgt_send_prli - Send a process login
  2425. * @tgt: ibmvfc target struct
  2426. *
  2427. **/
  2428. static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
  2429. {
  2430. struct ibmvfc_process_login *prli;
  2431. struct ibmvfc_host *vhost = tgt->vhost;
  2432. struct ibmvfc_event *evt;
  2433. if (vhost->discovery_threads >= disc_threads)
  2434. return;
  2435. kref_get(&tgt->kref);
  2436. evt = ibmvfc_get_event(vhost);
  2437. vhost->discovery_threads++;
  2438. ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
  2439. evt->tgt = tgt;
  2440. prli = &evt->iu.prli;
  2441. memset(prli, 0, sizeof(*prli));
  2442. prli->common.version = 1;
  2443. prli->common.opcode = IBMVFC_PROCESS_LOGIN;
  2444. prli->common.length = sizeof(*prli);
  2445. prli->scsi_id = tgt->scsi_id;
  2446. prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
  2447. prli->parms.flags = IBMVFC_PRLI_EST_IMG_PAIR;
  2448. prli->parms.service_parms = IBMVFC_PRLI_INITIATOR_FUNC;
  2449. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  2450. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  2451. vhost->discovery_threads--;
  2452. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2453. kref_put(&tgt->kref, ibmvfc_release_tgt);
  2454. } else
  2455. tgt_dbg(tgt, "Sent process login\n");
  2456. }
  2457. /**
  2458. * ibmvfc_tgt_plogi_done - Completion handler for Port Login
  2459. * @evt: ibmvfc event struct
  2460. *
  2461. **/
  2462. static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
  2463. {
  2464. struct ibmvfc_target *tgt = evt->tgt;
  2465. struct ibmvfc_host *vhost = evt->vhost;
  2466. struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
  2467. u32 status = rsp->common.status;
  2468. vhost->discovery_threads--;
  2469. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2470. switch (status) {
  2471. case IBMVFC_MAD_SUCCESS:
  2472. tgt_dbg(tgt, "Port Login succeeded\n");
  2473. if (tgt->ids.port_name &&
  2474. tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
  2475. vhost->reinit = 1;
  2476. tgt_dbg(tgt, "Port re-init required\n");
  2477. break;
  2478. }
  2479. tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
  2480. tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
  2481. tgt->ids.port_id = tgt->scsi_id;
  2482. tgt->ids.roles = FC_PORT_ROLE_FCP_TARGET;
  2483. memcpy(&tgt->service_parms, &rsp->service_parms,
  2484. sizeof(tgt->service_parms));
  2485. memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
  2486. sizeof(tgt->service_parms_change));
  2487. ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
  2488. break;
  2489. case IBMVFC_MAD_DRIVER_FAILED:
  2490. break;
  2491. case IBMVFC_MAD_CRQ_ERROR:
  2492. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  2493. break;
  2494. case IBMVFC_MAD_FAILED:
  2495. default:
  2496. tgt_err(tgt, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  2497. ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
  2498. ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
  2499. ibmvfc_get_ls_explain(rsp->fc_explain), rsp->fc_explain, status);
  2500. if (ibmvfc_retry_cmd(rsp->status, rsp->error))
  2501. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  2502. break;
  2503. };
  2504. kref_put(&tgt->kref, ibmvfc_release_tgt);
  2505. ibmvfc_free_event(evt);
  2506. wake_up(&vhost->work_wait_q);
  2507. }
  2508. /**
  2509. * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
  2510. * @tgt: ibmvfc target struct
  2511. *
  2512. **/
  2513. static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
  2514. {
  2515. struct ibmvfc_port_login *plogi;
  2516. struct ibmvfc_host *vhost = tgt->vhost;
  2517. struct ibmvfc_event *evt;
  2518. if (vhost->discovery_threads >= disc_threads)
  2519. return;
  2520. kref_get(&tgt->kref);
  2521. evt = ibmvfc_get_event(vhost);
  2522. vhost->discovery_threads++;
  2523. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  2524. ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
  2525. evt->tgt = tgt;
  2526. plogi = &evt->iu.plogi;
  2527. memset(plogi, 0, sizeof(*plogi));
  2528. plogi->common.version = 1;
  2529. plogi->common.opcode = IBMVFC_PORT_LOGIN;
  2530. plogi->common.length = sizeof(*plogi);
  2531. plogi->scsi_id = tgt->scsi_id;
  2532. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  2533. vhost->discovery_threads--;
  2534. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2535. kref_put(&tgt->kref, ibmvfc_release_tgt);
  2536. } else
  2537. tgt_dbg(tgt, "Sent port login\n");
  2538. }
  2539. /**
  2540. * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
  2541. * @evt: ibmvfc event struct
  2542. *
  2543. **/
  2544. static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
  2545. {
  2546. struct ibmvfc_target *tgt = evt->tgt;
  2547. struct ibmvfc_host *vhost = evt->vhost;
  2548. struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
  2549. u32 status = rsp->common.status;
  2550. vhost->discovery_threads--;
  2551. ibmvfc_free_event(evt);
  2552. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2553. switch (status) {
  2554. case IBMVFC_MAD_SUCCESS:
  2555. tgt_dbg(tgt, "Implicit Logout succeeded\n");
  2556. break;
  2557. case IBMVFC_MAD_DRIVER_FAILED:
  2558. kref_put(&tgt->kref, ibmvfc_release_tgt);
  2559. wake_up(&vhost->work_wait_q);
  2560. return;
  2561. case IBMVFC_MAD_FAILED:
  2562. default:
  2563. tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
  2564. break;
  2565. };
  2566. if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
  2567. ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
  2568. else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
  2569. tgt->scsi_id != tgt->new_scsi_id)
  2570. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2571. kref_put(&tgt->kref, ibmvfc_release_tgt);
  2572. wake_up(&vhost->work_wait_q);
  2573. }
  2574. /**
  2575. * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
  2576. * @tgt: ibmvfc target struct
  2577. *
  2578. **/
  2579. static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
  2580. {
  2581. struct ibmvfc_implicit_logout *mad;
  2582. struct ibmvfc_host *vhost = tgt->vhost;
  2583. struct ibmvfc_event *evt;
  2584. if (vhost->discovery_threads >= disc_threads)
  2585. return;
  2586. kref_get(&tgt->kref);
  2587. evt = ibmvfc_get_event(vhost);
  2588. vhost->discovery_threads++;
  2589. ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
  2590. evt->tgt = tgt;
  2591. mad = &evt->iu.implicit_logout;
  2592. memset(mad, 0, sizeof(*mad));
  2593. mad->common.version = 1;
  2594. mad->common.opcode = IBMVFC_IMPLICIT_LOGOUT;
  2595. mad->common.length = sizeof(*mad);
  2596. mad->old_scsi_id = tgt->scsi_id;
  2597. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  2598. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  2599. vhost->discovery_threads--;
  2600. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2601. kref_put(&tgt->kref, ibmvfc_release_tgt);
  2602. } else
  2603. tgt_dbg(tgt, "Sent Implicit Logout\n");
  2604. }
  2605. /**
  2606. * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
  2607. * @mad: ibmvfc passthru mad struct
  2608. * @tgt: ibmvfc target struct
  2609. *
  2610. * Returns:
  2611. * 1 if PLOGI needed / 0 if PLOGI not needed
  2612. **/
  2613. static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
  2614. struct ibmvfc_target *tgt)
  2615. {
  2616. if (memcmp(&mad->fc_iu.response[2], &tgt->ids.port_name,
  2617. sizeof(tgt->ids.port_name)))
  2618. return 1;
  2619. if (memcmp(&mad->fc_iu.response[4], &tgt->ids.node_name,
  2620. sizeof(tgt->ids.node_name)))
  2621. return 1;
  2622. if (mad->fc_iu.response[6] != tgt->scsi_id)
  2623. return 1;
  2624. return 0;
  2625. }
  2626. /**
  2627. * ibmvfc_tgt_adisc_done - Completion handler for ADISC
  2628. * @evt: ibmvfc event struct
  2629. *
  2630. **/
  2631. static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
  2632. {
  2633. struct ibmvfc_target *tgt = evt->tgt;
  2634. struct ibmvfc_host *vhost = evt->vhost;
  2635. struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
  2636. u32 status = mad->common.status;
  2637. u8 fc_reason, fc_explain;
  2638. vhost->discovery_threads--;
  2639. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2640. switch (status) {
  2641. case IBMVFC_MAD_SUCCESS:
  2642. tgt_dbg(tgt, "ADISC succeeded\n");
  2643. if (ibmvfc_adisc_needs_plogi(mad, tgt))
  2644. tgt->need_login = 1;
  2645. break;
  2646. case IBMVFC_MAD_DRIVER_FAILED:
  2647. break;
  2648. case IBMVFC_MAD_FAILED:
  2649. default:
  2650. tgt->need_login = 1;
  2651. fc_reason = (mad->fc_iu.response[1] & 0x00ff0000) >> 16;
  2652. fc_explain = (mad->fc_iu.response[1] & 0x0000ff00) >> 8;
  2653. tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  2654. ibmvfc_get_cmd_error(mad->iu.status, mad->iu.error),
  2655. mad->iu.status, mad->iu.error,
  2656. ibmvfc_get_fc_type(fc_reason), fc_reason,
  2657. ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
  2658. break;
  2659. };
  2660. kref_put(&tgt->kref, ibmvfc_release_tgt);
  2661. ibmvfc_free_event(evt);
  2662. wake_up(&vhost->work_wait_q);
  2663. }
  2664. /**
  2665. * ibmvfc_init_passthru - Initialize an event struct for FC passthru
  2666. * @evt: ibmvfc event struct
  2667. *
  2668. **/
  2669. static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
  2670. {
  2671. struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
  2672. memset(mad, 0, sizeof(*mad));
  2673. mad->common.version = 1;
  2674. mad->common.opcode = IBMVFC_PASSTHRU;
  2675. mad->common.length = sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu);
  2676. mad->cmd_ioba.va = (u64)evt->crq.ioba +
  2677. offsetof(struct ibmvfc_passthru_mad, iu);
  2678. mad->cmd_ioba.len = sizeof(mad->iu);
  2679. mad->iu.cmd_len = sizeof(mad->fc_iu.payload);
  2680. mad->iu.rsp_len = sizeof(mad->fc_iu.response);
  2681. mad->iu.cmd.va = (u64)evt->crq.ioba +
  2682. offsetof(struct ibmvfc_passthru_mad, fc_iu) +
  2683. offsetof(struct ibmvfc_passthru_fc_iu, payload);
  2684. mad->iu.cmd.len = sizeof(mad->fc_iu.payload);
  2685. mad->iu.rsp.va = (u64)evt->crq.ioba +
  2686. offsetof(struct ibmvfc_passthru_mad, fc_iu) +
  2687. offsetof(struct ibmvfc_passthru_fc_iu, response);
  2688. mad->iu.rsp.len = sizeof(mad->fc_iu.response);
  2689. }
  2690. /**
  2691. * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
  2692. * @tgt: ibmvfc target struct
  2693. *
  2694. **/
  2695. static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
  2696. {
  2697. struct ibmvfc_passthru_mad *mad;
  2698. struct ibmvfc_host *vhost = tgt->vhost;
  2699. struct ibmvfc_event *evt;
  2700. if (vhost->discovery_threads >= disc_threads)
  2701. return;
  2702. kref_get(&tgt->kref);
  2703. evt = ibmvfc_get_event(vhost);
  2704. vhost->discovery_threads++;
  2705. ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
  2706. evt->tgt = tgt;
  2707. ibmvfc_init_passthru(evt);
  2708. mad = &evt->iu.passthru;
  2709. mad->iu.flags = IBMVFC_FC_ELS;
  2710. mad->iu.scsi_id = tgt->scsi_id;
  2711. mad->fc_iu.payload[0] = IBMVFC_ADISC;
  2712. memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
  2713. sizeof(vhost->login_buf->resp.port_name));
  2714. memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
  2715. sizeof(vhost->login_buf->resp.node_name));
  2716. mad->fc_iu.payload[6] = vhost->login_buf->resp.scsi_id & 0x00ffffff;
  2717. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  2718. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  2719. vhost->discovery_threads--;
  2720. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2721. kref_put(&tgt->kref, ibmvfc_release_tgt);
  2722. } else
  2723. tgt_dbg(tgt, "Sent ADISC\n");
  2724. }
  2725. /**
  2726. * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
  2727. * @evt: ibmvfc event struct
  2728. *
  2729. **/
  2730. static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
  2731. {
  2732. struct ibmvfc_target *tgt = evt->tgt;
  2733. struct ibmvfc_host *vhost = evt->vhost;
  2734. struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
  2735. u32 status = rsp->common.status;
  2736. vhost->discovery_threads--;
  2737. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2738. switch (status) {
  2739. case IBMVFC_MAD_SUCCESS:
  2740. tgt_dbg(tgt, "Query Target succeeded\n");
  2741. tgt->new_scsi_id = rsp->scsi_id;
  2742. if (rsp->scsi_id != tgt->scsi_id)
  2743. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  2744. else
  2745. ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
  2746. break;
  2747. case IBMVFC_MAD_DRIVER_FAILED:
  2748. break;
  2749. case IBMVFC_MAD_CRQ_ERROR:
  2750. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
  2751. break;
  2752. case IBMVFC_MAD_FAILED:
  2753. default:
  2754. tgt_err(tgt, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  2755. ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
  2756. ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
  2757. ibmvfc_get_gs_explain(rsp->fc_explain), rsp->fc_explain, status);
  2758. if ((rsp->status & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
  2759. rsp->error == IBMVFC_UNABLE_TO_PERFORM_REQ &&
  2760. rsp->fc_explain == IBMVFC_PORT_NAME_NOT_REG)
  2761. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2762. else if (ibmvfc_retry_cmd(rsp->status, rsp->error))
  2763. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
  2764. break;
  2765. };
  2766. kref_put(&tgt->kref, ibmvfc_release_tgt);
  2767. ibmvfc_free_event(evt);
  2768. wake_up(&vhost->work_wait_q);
  2769. }
  2770. /**
  2771. * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
  2772. * @tgt: ibmvfc target struct
  2773. *
  2774. **/
  2775. static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
  2776. {
  2777. struct ibmvfc_query_tgt *query_tgt;
  2778. struct ibmvfc_host *vhost = tgt->vhost;
  2779. struct ibmvfc_event *evt;
  2780. if (vhost->discovery_threads >= disc_threads)
  2781. return;
  2782. kref_get(&tgt->kref);
  2783. evt = ibmvfc_get_event(vhost);
  2784. vhost->discovery_threads++;
  2785. evt->tgt = tgt;
  2786. ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
  2787. query_tgt = &evt->iu.query_tgt;
  2788. memset(query_tgt, 0, sizeof(*query_tgt));
  2789. query_tgt->common.version = 1;
  2790. query_tgt->common.opcode = IBMVFC_QUERY_TARGET;
  2791. query_tgt->common.length = sizeof(*query_tgt);
  2792. query_tgt->wwpn = tgt->ids.port_name;
  2793. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  2794. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  2795. vhost->discovery_threads--;
  2796. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2797. kref_put(&tgt->kref, ibmvfc_release_tgt);
  2798. } else
  2799. tgt_dbg(tgt, "Sent Query Target\n");
  2800. }
  2801. /**
  2802. * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
  2803. * @vhost: ibmvfc host struct
  2804. * @scsi_id: SCSI ID to allocate target for
  2805. *
  2806. * Returns:
  2807. * 0 on success / other on failure
  2808. **/
  2809. static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
  2810. {
  2811. struct ibmvfc_target *tgt;
  2812. unsigned long flags;
  2813. spin_lock_irqsave(vhost->host->host_lock, flags);
  2814. list_for_each_entry(tgt, &vhost->targets, queue) {
  2815. if (tgt->scsi_id == scsi_id) {
  2816. if (tgt->need_login)
  2817. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  2818. goto unlock_out;
  2819. }
  2820. }
  2821. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2822. tgt = mempool_alloc(vhost->tgt_pool, GFP_KERNEL);
  2823. if (!tgt) {
  2824. dev_err(vhost->dev, "Target allocation failure for scsi id %08lx\n",
  2825. scsi_id);
  2826. return -ENOMEM;
  2827. }
  2828. tgt->scsi_id = scsi_id;
  2829. tgt->new_scsi_id = scsi_id;
  2830. tgt->vhost = vhost;
  2831. tgt->need_login = 1;
  2832. kref_init(&tgt->kref);
  2833. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  2834. spin_lock_irqsave(vhost->host->host_lock, flags);
  2835. list_add_tail(&tgt->queue, &vhost->targets);
  2836. unlock_out:
  2837. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2838. return 0;
  2839. }
  2840. /**
  2841. * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
  2842. * @vhost: ibmvfc host struct
  2843. *
  2844. * Returns:
  2845. * 0 on success / other on failure
  2846. **/
  2847. static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
  2848. {
  2849. int i, rc;
  2850. for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
  2851. rc = ibmvfc_alloc_target(vhost,
  2852. vhost->disc_buf->scsi_id[i] & IBMVFC_DISC_TGT_SCSI_ID_MASK);
  2853. return rc;
  2854. }
  2855. /**
  2856. * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
  2857. * @evt: ibmvfc event struct
  2858. *
  2859. **/
  2860. static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
  2861. {
  2862. struct ibmvfc_host *vhost = evt->vhost;
  2863. struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
  2864. u32 mad_status = rsp->common.status;
  2865. switch (mad_status) {
  2866. case IBMVFC_MAD_SUCCESS:
  2867. ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
  2868. vhost->num_targets = rsp->num_written;
  2869. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
  2870. break;
  2871. case IBMVFC_MAD_FAILED:
  2872. dev_err(vhost->dev, "Discover Targets failed: %s (%x:%x)\n",
  2873. ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
  2874. ibmvfc_retry_host_init(vhost);
  2875. break;
  2876. case IBMVFC_MAD_DRIVER_FAILED:
  2877. break;
  2878. default:
  2879. dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
  2880. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2881. break;
  2882. }
  2883. ibmvfc_free_event(evt);
  2884. wake_up(&vhost->work_wait_q);
  2885. }
  2886. /**
  2887. * ibmvfc_discover_targets - Send Discover Targets MAD
  2888. * @vhost: ibmvfc host struct
  2889. *
  2890. **/
  2891. static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
  2892. {
  2893. struct ibmvfc_discover_targets *mad;
  2894. struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
  2895. ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
  2896. mad = &evt->iu.discover_targets;
  2897. memset(mad, 0, sizeof(*mad));
  2898. mad->common.version = 1;
  2899. mad->common.opcode = IBMVFC_DISC_TARGETS;
  2900. mad->common.length = sizeof(*mad);
  2901. mad->bufflen = vhost->disc_buf_sz;
  2902. mad->buffer.va = vhost->disc_buf_dma;
  2903. mad->buffer.len = vhost->disc_buf_sz;
  2904. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  2905. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  2906. ibmvfc_dbg(vhost, "Sent discover targets\n");
  2907. else
  2908. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2909. }
  2910. /**
  2911. * ibmvfc_npiv_login_done - Completion handler for NPIV Login
  2912. * @evt: ibmvfc event struct
  2913. *
  2914. **/
  2915. static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
  2916. {
  2917. struct ibmvfc_host *vhost = evt->vhost;
  2918. u32 mad_status = evt->xfer_iu->npiv_login.common.status;
  2919. struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
  2920. unsigned int npiv_max_sectors;
  2921. switch (mad_status) {
  2922. case IBMVFC_MAD_SUCCESS:
  2923. ibmvfc_free_event(evt);
  2924. break;
  2925. case IBMVFC_MAD_FAILED:
  2926. dev_err(vhost->dev, "NPIV Login failed: %s (%x:%x)\n",
  2927. ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
  2928. if (ibmvfc_retry_cmd(rsp->status, rsp->error))
  2929. ibmvfc_retry_host_init(vhost);
  2930. else
  2931. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2932. ibmvfc_free_event(evt);
  2933. return;
  2934. case IBMVFC_MAD_CRQ_ERROR:
  2935. ibmvfc_retry_host_init(vhost);
  2936. case IBMVFC_MAD_DRIVER_FAILED:
  2937. ibmvfc_free_event(evt);
  2938. return;
  2939. default:
  2940. dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
  2941. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2942. ibmvfc_free_event(evt);
  2943. return;
  2944. }
  2945. vhost->client_migrated = 0;
  2946. if (!(rsp->flags & IBMVFC_NATIVE_FC)) {
  2947. dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
  2948. rsp->flags);
  2949. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2950. wake_up(&vhost->work_wait_q);
  2951. return;
  2952. }
  2953. if (rsp->max_cmds <= IBMVFC_NUM_INTERNAL_REQ) {
  2954. dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
  2955. rsp->max_cmds);
  2956. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2957. wake_up(&vhost->work_wait_q);
  2958. return;
  2959. }
  2960. npiv_max_sectors = min((uint)(rsp->max_dma_len >> 9), IBMVFC_MAX_SECTORS);
  2961. dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
  2962. rsp->partition_name, rsp->device_name, rsp->port_loc_code,
  2963. rsp->drc_name, npiv_max_sectors);
  2964. fc_host_fabric_name(vhost->host) = rsp->node_name;
  2965. fc_host_node_name(vhost->host) = rsp->node_name;
  2966. fc_host_port_name(vhost->host) = rsp->port_name;
  2967. fc_host_port_id(vhost->host) = rsp->scsi_id;
  2968. fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
  2969. fc_host_supported_classes(vhost->host) = 0;
  2970. if (rsp->service_parms.class1_parms[0] & 0x80000000)
  2971. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
  2972. if (rsp->service_parms.class2_parms[0] & 0x80000000)
  2973. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
  2974. if (rsp->service_parms.class3_parms[0] & 0x80000000)
  2975. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
  2976. fc_host_maxframe_size(vhost->host) =
  2977. rsp->service_parms.common.bb_rcv_sz & 0x0fff;
  2978. vhost->host->can_queue = rsp->max_cmds - IBMVFC_NUM_INTERNAL_REQ;
  2979. vhost->host->max_sectors = npiv_max_sectors;
  2980. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  2981. wake_up(&vhost->work_wait_q);
  2982. }
  2983. /**
  2984. * ibmvfc_npiv_login - Sends NPIV login
  2985. * @vhost: ibmvfc host struct
  2986. *
  2987. **/
  2988. static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
  2989. {
  2990. struct ibmvfc_npiv_login_mad *mad;
  2991. struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
  2992. ibmvfc_gather_partition_info(vhost);
  2993. ibmvfc_set_login_info(vhost);
  2994. ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
  2995. memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
  2996. mad = &evt->iu.npiv_login;
  2997. memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
  2998. mad->common.version = 1;
  2999. mad->common.opcode = IBMVFC_NPIV_LOGIN;
  3000. mad->common.length = sizeof(struct ibmvfc_npiv_login_mad);
  3001. mad->buffer.va = vhost->login_buf_dma;
  3002. mad->buffer.len = sizeof(*vhost->login_buf);
  3003. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  3004. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  3005. ibmvfc_dbg(vhost, "Sent NPIV login\n");
  3006. else
  3007. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3008. };
  3009. /**
  3010. * ibmvfc_dev_init_to_do - Is there target initialization work to do?
  3011. * @vhost: ibmvfc host struct
  3012. *
  3013. * Returns:
  3014. * 1 if work to do / 0 if not
  3015. **/
  3016. static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
  3017. {
  3018. struct ibmvfc_target *tgt;
  3019. list_for_each_entry(tgt, &vhost->targets, queue) {
  3020. if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
  3021. tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
  3022. return 1;
  3023. }
  3024. return 0;
  3025. }
  3026. /**
  3027. * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
  3028. * @vhost: ibmvfc host struct
  3029. *
  3030. * Returns:
  3031. * 1 if work to do / 0 if not
  3032. **/
  3033. static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
  3034. {
  3035. struct ibmvfc_target *tgt;
  3036. if (kthread_should_stop())
  3037. return 1;
  3038. switch (vhost->action) {
  3039. case IBMVFC_HOST_ACTION_NONE:
  3040. case IBMVFC_HOST_ACTION_INIT_WAIT:
  3041. return 0;
  3042. case IBMVFC_HOST_ACTION_TGT_INIT:
  3043. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  3044. if (vhost->discovery_threads == disc_threads)
  3045. return 0;
  3046. list_for_each_entry(tgt, &vhost->targets, queue)
  3047. if (tgt->action == IBMVFC_TGT_ACTION_INIT)
  3048. return 1;
  3049. list_for_each_entry(tgt, &vhost->targets, queue)
  3050. if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
  3051. return 0;
  3052. return 1;
  3053. case IBMVFC_HOST_ACTION_INIT:
  3054. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  3055. case IBMVFC_HOST_ACTION_TGT_ADD:
  3056. case IBMVFC_HOST_ACTION_TGT_DEL:
  3057. case IBMVFC_HOST_ACTION_QUERY:
  3058. default:
  3059. break;
  3060. };
  3061. return 1;
  3062. }
  3063. /**
  3064. * ibmvfc_work_to_do - Is there task level work to do?
  3065. * @vhost: ibmvfc host struct
  3066. *
  3067. * Returns:
  3068. * 1 if work to do / 0 if not
  3069. **/
  3070. static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
  3071. {
  3072. unsigned long flags;
  3073. int rc;
  3074. spin_lock_irqsave(vhost->host->host_lock, flags);
  3075. rc = __ibmvfc_work_to_do(vhost);
  3076. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3077. return rc;
  3078. }
  3079. /**
  3080. * ibmvfc_log_ae - Log async events if necessary
  3081. * @vhost: ibmvfc host struct
  3082. * @events: events to log
  3083. *
  3084. **/
  3085. static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
  3086. {
  3087. if (events & IBMVFC_AE_RSCN)
  3088. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
  3089. if ((events & IBMVFC_AE_LINKDOWN) &&
  3090. vhost->state >= IBMVFC_HALTED)
  3091. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
  3092. if ((events & IBMVFC_AE_LINKUP) &&
  3093. vhost->state == IBMVFC_INITIALIZING)
  3094. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
  3095. }
  3096. /**
  3097. * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
  3098. * @tgt: ibmvfc target struct
  3099. *
  3100. **/
  3101. static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
  3102. {
  3103. struct ibmvfc_host *vhost = tgt->vhost;
  3104. struct fc_rport *rport;
  3105. unsigned long flags;
  3106. tgt_dbg(tgt, "Adding rport\n");
  3107. rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
  3108. spin_lock_irqsave(vhost->host->host_lock, flags);
  3109. tgt->rport = rport;
  3110. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3111. if (rport) {
  3112. tgt_dbg(tgt, "rport add succeeded\n");
  3113. rport->maxframe_size = tgt->service_parms.common.bb_rcv_sz & 0x0fff;
  3114. rport->supported_classes = 0;
  3115. tgt->target_id = rport->scsi_target_id;
  3116. if (tgt->service_parms.class1_parms[0] & 0x80000000)
  3117. rport->supported_classes |= FC_COS_CLASS1;
  3118. if (tgt->service_parms.class2_parms[0] & 0x80000000)
  3119. rport->supported_classes |= FC_COS_CLASS2;
  3120. if (tgt->service_parms.class3_parms[0] & 0x80000000)
  3121. rport->supported_classes |= FC_COS_CLASS3;
  3122. } else
  3123. tgt_dbg(tgt, "rport add failed\n");
  3124. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3125. }
  3126. /**
  3127. * ibmvfc_do_work - Do task level work
  3128. * @vhost: ibmvfc host struct
  3129. *
  3130. **/
  3131. static void ibmvfc_do_work(struct ibmvfc_host *vhost)
  3132. {
  3133. struct ibmvfc_target *tgt;
  3134. unsigned long flags;
  3135. struct fc_rport *rport;
  3136. ibmvfc_log_ae(vhost, vhost->events_to_log);
  3137. spin_lock_irqsave(vhost->host->host_lock, flags);
  3138. vhost->events_to_log = 0;
  3139. switch (vhost->action) {
  3140. case IBMVFC_HOST_ACTION_NONE:
  3141. case IBMVFC_HOST_ACTION_INIT_WAIT:
  3142. break;
  3143. case IBMVFC_HOST_ACTION_INIT:
  3144. BUG_ON(vhost->state != IBMVFC_INITIALIZING);
  3145. vhost->job_step(vhost);
  3146. break;
  3147. case IBMVFC_HOST_ACTION_QUERY:
  3148. list_for_each_entry(tgt, &vhost->targets, queue)
  3149. ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
  3150. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
  3151. break;
  3152. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  3153. list_for_each_entry(tgt, &vhost->targets, queue) {
  3154. if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
  3155. tgt->job_step(tgt);
  3156. break;
  3157. }
  3158. }
  3159. if (!ibmvfc_dev_init_to_do(vhost))
  3160. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
  3161. break;
  3162. case IBMVFC_HOST_ACTION_TGT_DEL:
  3163. list_for_each_entry(tgt, &vhost->targets, queue) {
  3164. if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
  3165. tgt_dbg(tgt, "Deleting rport\n");
  3166. rport = tgt->rport;
  3167. tgt->rport = NULL;
  3168. list_del(&tgt->queue);
  3169. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3170. if (rport)
  3171. fc_remote_port_delete(rport);
  3172. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3173. return;
  3174. }
  3175. }
  3176. if (vhost->state == IBMVFC_INITIALIZING) {
  3177. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  3178. vhost->job_step = ibmvfc_discover_targets;
  3179. } else {
  3180. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  3181. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3182. scsi_unblock_requests(vhost->host);
  3183. wake_up(&vhost->init_wait_q);
  3184. return;
  3185. }
  3186. break;
  3187. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  3188. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
  3189. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3190. ibmvfc_alloc_targets(vhost);
  3191. spin_lock_irqsave(vhost->host->host_lock, flags);
  3192. break;
  3193. case IBMVFC_HOST_ACTION_TGT_INIT:
  3194. list_for_each_entry(tgt, &vhost->targets, queue) {
  3195. if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
  3196. tgt->job_step(tgt);
  3197. break;
  3198. }
  3199. }
  3200. if (!ibmvfc_dev_init_to_do(vhost)) {
  3201. ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
  3202. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_ADD);
  3203. vhost->init_retries = 0;
  3204. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3205. scsi_unblock_requests(vhost->host);
  3206. return;
  3207. }
  3208. break;
  3209. case IBMVFC_HOST_ACTION_TGT_ADD:
  3210. list_for_each_entry(tgt, &vhost->targets, queue) {
  3211. if (tgt->action == IBMVFC_TGT_ACTION_ADD_RPORT) {
  3212. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3213. ibmvfc_tgt_add_rport(tgt);
  3214. return;
  3215. } else if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
  3216. tgt_dbg(tgt, "Deleting rport\n");
  3217. rport = tgt->rport;
  3218. tgt->rport = NULL;
  3219. list_del(&tgt->queue);
  3220. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3221. if (rport)
  3222. fc_remote_port_delete(rport);
  3223. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3224. return;
  3225. }
  3226. }
  3227. if (vhost->reinit && !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  3228. vhost->reinit = 0;
  3229. scsi_block_requests(vhost->host);
  3230. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  3231. } else {
  3232. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  3233. wake_up(&vhost->init_wait_q);
  3234. }
  3235. break;
  3236. default:
  3237. break;
  3238. };
  3239. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3240. }
  3241. /**
  3242. * ibmvfc_work - Do task level work
  3243. * @data: ibmvfc host struct
  3244. *
  3245. * Returns:
  3246. * zero
  3247. **/
  3248. static int ibmvfc_work(void *data)
  3249. {
  3250. struct ibmvfc_host *vhost = data;
  3251. int rc;
  3252. set_user_nice(current, -20);
  3253. while (1) {
  3254. rc = wait_event_interruptible(vhost->work_wait_q,
  3255. ibmvfc_work_to_do(vhost));
  3256. BUG_ON(rc);
  3257. if (kthread_should_stop())
  3258. break;
  3259. ibmvfc_do_work(vhost);
  3260. }
  3261. ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
  3262. return 0;
  3263. }
  3264. /**
  3265. * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
  3266. * @vhost: ibmvfc host struct
  3267. *
  3268. * Allocates a page for messages, maps it for dma, and registers
  3269. * the crq with the hypervisor.
  3270. *
  3271. * Return value:
  3272. * zero on success / other on failure
  3273. **/
  3274. static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
  3275. {
  3276. int rc, retrc = -ENOMEM;
  3277. struct device *dev = vhost->dev;
  3278. struct vio_dev *vdev = to_vio_dev(dev);
  3279. struct ibmvfc_crq_queue *crq = &vhost->crq;
  3280. ENTER;
  3281. crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
  3282. if (!crq->msgs)
  3283. return -ENOMEM;
  3284. crq->size = PAGE_SIZE / sizeof(*crq->msgs);
  3285. crq->msg_token = dma_map_single(dev, crq->msgs,
  3286. PAGE_SIZE, DMA_BIDIRECTIONAL);
  3287. if (dma_mapping_error(dev, crq->msg_token))
  3288. goto map_failed;
  3289. retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  3290. crq->msg_token, PAGE_SIZE);
  3291. if (rc == H_RESOURCE)
  3292. /* maybe kexecing and resource is busy. try a reset */
  3293. retrc = rc = ibmvfc_reset_crq(vhost);
  3294. if (rc == H_CLOSED)
  3295. dev_warn(dev, "Partner adapter not ready\n");
  3296. else if (rc) {
  3297. dev_warn(dev, "Error %d opening adapter\n", rc);
  3298. goto reg_crq_failed;
  3299. }
  3300. retrc = 0;
  3301. if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
  3302. dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
  3303. goto req_irq_failed;
  3304. }
  3305. if ((rc = vio_enable_interrupts(vdev))) {
  3306. dev_err(dev, "Error %d enabling interrupts\n", rc);
  3307. goto req_irq_failed;
  3308. }
  3309. crq->cur = 0;
  3310. LEAVE;
  3311. return retrc;
  3312. req_irq_failed:
  3313. do {
  3314. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  3315. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  3316. reg_crq_failed:
  3317. dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
  3318. map_failed:
  3319. free_page((unsigned long)crq->msgs);
  3320. return retrc;
  3321. }
  3322. /**
  3323. * ibmvfc_free_mem - Free memory for vhost
  3324. * @vhost: ibmvfc host struct
  3325. *
  3326. * Return value:
  3327. * none
  3328. **/
  3329. static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
  3330. {
  3331. struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
  3332. ENTER;
  3333. mempool_destroy(vhost->tgt_pool);
  3334. kfree(vhost->trace);
  3335. dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
  3336. vhost->disc_buf_dma);
  3337. dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
  3338. vhost->login_buf, vhost->login_buf_dma);
  3339. dma_pool_destroy(vhost->sg_pool);
  3340. dma_unmap_single(vhost->dev, async_q->msg_token,
  3341. async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
  3342. free_page((unsigned long)async_q->msgs);
  3343. LEAVE;
  3344. }
  3345. /**
  3346. * ibmvfc_alloc_mem - Allocate memory for vhost
  3347. * @vhost: ibmvfc host struct
  3348. *
  3349. * Return value:
  3350. * 0 on success / non-zero on failure
  3351. **/
  3352. static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
  3353. {
  3354. struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
  3355. struct device *dev = vhost->dev;
  3356. ENTER;
  3357. async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
  3358. if (!async_q->msgs) {
  3359. dev_err(dev, "Couldn't allocate async queue.\n");
  3360. goto nomem;
  3361. }
  3362. async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
  3363. async_q->msg_token = dma_map_single(dev, async_q->msgs,
  3364. async_q->size * sizeof(*async_q->msgs),
  3365. DMA_BIDIRECTIONAL);
  3366. if (dma_mapping_error(dev, async_q->msg_token)) {
  3367. dev_err(dev, "Failed to map async queue\n");
  3368. goto free_async_crq;
  3369. }
  3370. vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
  3371. SG_ALL * sizeof(struct srp_direct_buf),
  3372. sizeof(struct srp_direct_buf), 0);
  3373. if (!vhost->sg_pool) {
  3374. dev_err(dev, "Failed to allocate sg pool\n");
  3375. goto unmap_async_crq;
  3376. }
  3377. vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
  3378. &vhost->login_buf_dma, GFP_KERNEL);
  3379. if (!vhost->login_buf) {
  3380. dev_err(dev, "Couldn't allocate NPIV login buffer\n");
  3381. goto free_sg_pool;
  3382. }
  3383. vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
  3384. vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
  3385. &vhost->disc_buf_dma, GFP_KERNEL);
  3386. if (!vhost->disc_buf) {
  3387. dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
  3388. goto free_login_buffer;
  3389. }
  3390. vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
  3391. sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
  3392. if (!vhost->trace)
  3393. goto free_disc_buffer;
  3394. vhost->tgt_pool = mempool_create_kzalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
  3395. sizeof(struct ibmvfc_target));
  3396. if (!vhost->tgt_pool) {
  3397. dev_err(dev, "Couldn't allocate target memory pool\n");
  3398. goto free_trace;
  3399. }
  3400. LEAVE;
  3401. return 0;
  3402. free_trace:
  3403. kfree(vhost->trace);
  3404. free_disc_buffer:
  3405. dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
  3406. vhost->disc_buf_dma);
  3407. free_login_buffer:
  3408. dma_free_coherent(dev, sizeof(*vhost->login_buf),
  3409. vhost->login_buf, vhost->login_buf_dma);
  3410. free_sg_pool:
  3411. dma_pool_destroy(vhost->sg_pool);
  3412. unmap_async_crq:
  3413. dma_unmap_single(dev, async_q->msg_token,
  3414. async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
  3415. free_async_crq:
  3416. free_page((unsigned long)async_q->msgs);
  3417. nomem:
  3418. LEAVE;
  3419. return -ENOMEM;
  3420. }
  3421. /**
  3422. * ibmvfc_probe - Adapter hot plug add entry point
  3423. * @vdev: vio device struct
  3424. * @id: vio device id struct
  3425. *
  3426. * Return value:
  3427. * 0 on success / non-zero on failure
  3428. **/
  3429. static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  3430. {
  3431. struct ibmvfc_host *vhost;
  3432. struct Scsi_Host *shost;
  3433. struct device *dev = &vdev->dev;
  3434. int rc = -ENOMEM;
  3435. ENTER;
  3436. shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
  3437. if (!shost) {
  3438. dev_err(dev, "Couldn't allocate host data\n");
  3439. goto out;
  3440. }
  3441. shost->transportt = ibmvfc_transport_template;
  3442. shost->can_queue = max_requests;
  3443. shost->max_lun = max_lun;
  3444. shost->max_id = max_targets;
  3445. shost->max_sectors = IBMVFC_MAX_SECTORS;
  3446. shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
  3447. shost->unique_id = shost->host_no;
  3448. vhost = shost_priv(shost);
  3449. INIT_LIST_HEAD(&vhost->sent);
  3450. INIT_LIST_HEAD(&vhost->free);
  3451. INIT_LIST_HEAD(&vhost->targets);
  3452. sprintf(vhost->name, IBMVFC_NAME);
  3453. vhost->host = shost;
  3454. vhost->dev = dev;
  3455. vhost->partition_number = -1;
  3456. vhost->log_level = log_level;
  3457. strcpy(vhost->partition_name, "UNKNOWN");
  3458. init_waitqueue_head(&vhost->work_wait_q);
  3459. init_waitqueue_head(&vhost->init_wait_q);
  3460. if ((rc = ibmvfc_alloc_mem(vhost)))
  3461. goto free_scsi_host;
  3462. vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
  3463. shost->host_no);
  3464. if (IS_ERR(vhost->work_thread)) {
  3465. dev_err(dev, "Couldn't create kernel thread: %ld\n",
  3466. PTR_ERR(vhost->work_thread));
  3467. goto free_host_mem;
  3468. }
  3469. if ((rc = ibmvfc_init_crq(vhost))) {
  3470. dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
  3471. goto kill_kthread;
  3472. }
  3473. if ((rc = ibmvfc_init_event_pool(vhost))) {
  3474. dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
  3475. goto release_crq;
  3476. }
  3477. if ((rc = scsi_add_host(shost, dev)))
  3478. goto release_event_pool;
  3479. if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
  3480. &ibmvfc_trace_attr))) {
  3481. dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
  3482. goto remove_shost;
  3483. }
  3484. dev_set_drvdata(dev, vhost);
  3485. spin_lock(&ibmvfc_driver_lock);
  3486. list_add_tail(&vhost->queue, &ibmvfc_head);
  3487. spin_unlock(&ibmvfc_driver_lock);
  3488. ibmvfc_send_crq_init(vhost);
  3489. scsi_scan_host(shost);
  3490. return 0;
  3491. remove_shost:
  3492. scsi_remove_host(shost);
  3493. release_event_pool:
  3494. ibmvfc_free_event_pool(vhost);
  3495. release_crq:
  3496. ibmvfc_release_crq_queue(vhost);
  3497. kill_kthread:
  3498. kthread_stop(vhost->work_thread);
  3499. free_host_mem:
  3500. ibmvfc_free_mem(vhost);
  3501. free_scsi_host:
  3502. scsi_host_put(shost);
  3503. out:
  3504. LEAVE;
  3505. return rc;
  3506. }
  3507. /**
  3508. * ibmvfc_remove - Adapter hot plug remove entry point
  3509. * @vdev: vio device struct
  3510. *
  3511. * Return value:
  3512. * 0
  3513. **/
  3514. static int ibmvfc_remove(struct vio_dev *vdev)
  3515. {
  3516. struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
  3517. unsigned long flags;
  3518. ENTER;
  3519. ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
  3520. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  3521. ibmvfc_wait_while_resetting(vhost);
  3522. ibmvfc_release_crq_queue(vhost);
  3523. kthread_stop(vhost->work_thread);
  3524. fc_remove_host(vhost->host);
  3525. scsi_remove_host(vhost->host);
  3526. spin_lock_irqsave(vhost->host->host_lock, flags);
  3527. ibmvfc_purge_requests(vhost, DID_ERROR);
  3528. ibmvfc_free_event_pool(vhost);
  3529. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3530. ibmvfc_free_mem(vhost);
  3531. spin_lock(&ibmvfc_driver_lock);
  3532. list_del(&vhost->queue);
  3533. spin_unlock(&ibmvfc_driver_lock);
  3534. scsi_host_put(vhost->host);
  3535. LEAVE;
  3536. return 0;
  3537. }
  3538. /**
  3539. * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
  3540. * @vdev: vio device struct
  3541. *
  3542. * Return value:
  3543. * Number of bytes the driver will need to DMA map at the same time in
  3544. * order to perform well.
  3545. */
  3546. static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
  3547. {
  3548. unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
  3549. return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
  3550. }
  3551. static struct vio_device_id ibmvfc_device_table[] __devinitdata = {
  3552. {"fcp", "IBM,vfc-client"},
  3553. { "", "" }
  3554. };
  3555. MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
  3556. static struct vio_driver ibmvfc_driver = {
  3557. .id_table = ibmvfc_device_table,
  3558. .probe = ibmvfc_probe,
  3559. .remove = ibmvfc_remove,
  3560. .get_desired_dma = ibmvfc_get_desired_dma,
  3561. .driver = {
  3562. .name = IBMVFC_NAME,
  3563. .owner = THIS_MODULE,
  3564. }
  3565. };
  3566. static struct fc_function_template ibmvfc_transport_functions = {
  3567. .show_host_fabric_name = 1,
  3568. .show_host_node_name = 1,
  3569. .show_host_port_name = 1,
  3570. .show_host_supported_classes = 1,
  3571. .show_host_port_type = 1,
  3572. .show_host_port_id = 1,
  3573. .get_host_port_state = ibmvfc_get_host_port_state,
  3574. .show_host_port_state = 1,
  3575. .get_host_speed = ibmvfc_get_host_speed,
  3576. .show_host_speed = 1,
  3577. .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
  3578. .terminate_rport_io = ibmvfc_terminate_rport_io,
  3579. .show_rport_maxframe_size = 1,
  3580. .show_rport_supported_classes = 1,
  3581. .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
  3582. .show_rport_dev_loss_tmo = 1,
  3583. .get_starget_node_name = ibmvfc_get_starget_node_name,
  3584. .show_starget_node_name = 1,
  3585. .get_starget_port_name = ibmvfc_get_starget_port_name,
  3586. .show_starget_port_name = 1,
  3587. .get_starget_port_id = ibmvfc_get_starget_port_id,
  3588. .show_starget_port_id = 1,
  3589. };
  3590. /**
  3591. * ibmvfc_module_init - Initialize the ibmvfc module
  3592. *
  3593. * Return value:
  3594. * 0 on success / other on failure
  3595. **/
  3596. static int __init ibmvfc_module_init(void)
  3597. {
  3598. int rc;
  3599. if (!firmware_has_feature(FW_FEATURE_VIO))
  3600. return -ENODEV;
  3601. printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
  3602. IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
  3603. ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
  3604. if (!ibmvfc_transport_template)
  3605. return -ENOMEM;
  3606. rc = vio_register_driver(&ibmvfc_driver);
  3607. if (rc)
  3608. fc_release_transport(ibmvfc_transport_template);
  3609. return rc;
  3610. }
  3611. /**
  3612. * ibmvfc_module_exit - Teardown the ibmvfc module
  3613. *
  3614. * Return value:
  3615. * nothing
  3616. **/
  3617. static void __exit ibmvfc_module_exit(void)
  3618. {
  3619. vio_unregister_driver(&ibmvfc_driver);
  3620. fc_release_transport(ibmvfc_transport_template);
  3621. }
  3622. module_init(ibmvfc_module_init);
  3623. module_exit(ibmvfc_module_exit);