ibmvfc.c 133 KB

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