ibmvfc.c 113 KB

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