ibmvfc.c 184 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
  4. *
  5. * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
  6. *
  7. * Copyright (C) IBM Corporation, 2008
  8. */
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/dmapool.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/kthread.h>
  17. #include <linux/slab.h>
  18. #include <linux/of.h>
  19. #include <linux/pm.h>
  20. #include <linux/stringify.h>
  21. #include <linux/bsg-lib.h>
  22. #include <asm/firmware.h>
  23. #include <asm/irq.h>
  24. #include <asm/vio.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_cmnd.h>
  27. #include <scsi/scsi_host.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/scsi_bsg_fc.h>
  32. #include "ibmvfc.h"
  33. static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
  34. static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
  35. static u64 max_lun = IBMVFC_MAX_LUN;
  36. static unsigned int max_targets = IBMVFC_MAX_TARGETS;
  37. static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
  38. static u16 max_sectors = IBMVFC_MAX_SECTORS;
  39. static u16 scsi_qdepth = IBMVFC_SCSI_QDEPTH;
  40. static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
  41. static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
  42. static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
  43. static unsigned int cls3_error = IBMVFC_CLS3_ERROR;
  44. static unsigned int mq_enabled = IBMVFC_MQ;
  45. static unsigned int nr_scsi_hw_queues = IBMVFC_SCSI_HW_QUEUES;
  46. static unsigned int nr_scsi_channels = IBMVFC_SCSI_CHANNELS;
  47. static unsigned int mig_channels_only = IBMVFC_MIG_NO_SUB_TO_CRQ;
  48. static unsigned int mig_no_less_channels = IBMVFC_MIG_NO_N_TO_M;
  49. static LIST_HEAD(ibmvfc_head);
  50. static DEFINE_SPINLOCK(ibmvfc_driver_lock);
  51. static struct scsi_transport_template *ibmvfc_transport_template;
  52. MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
  53. MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
  54. MODULE_LICENSE("GPL");
  55. MODULE_VERSION(IBMVFC_DRIVER_VERSION);
  56. module_param_named(mq, mq_enabled, uint, S_IRUGO);
  57. MODULE_PARM_DESC(mq, "Enable multiqueue support. "
  58. "[Default=" __stringify(IBMVFC_MQ) "]");
  59. module_param_named(scsi_host_queues, nr_scsi_hw_queues, uint, S_IRUGO);
  60. MODULE_PARM_DESC(scsi_host_queues, "Number of SCSI Host submission queues. "
  61. "[Default=" __stringify(IBMVFC_SCSI_HW_QUEUES) "]");
  62. module_param_named(scsi_hw_channels, nr_scsi_channels, uint, S_IRUGO);
  63. MODULE_PARM_DESC(scsi_hw_channels, "Number of hw scsi channels to request. "
  64. "[Default=" __stringify(IBMVFC_SCSI_CHANNELS) "]");
  65. module_param_named(mig_channels_only, mig_channels_only, uint, S_IRUGO);
  66. MODULE_PARM_DESC(mig_channels_only, "Prevent migration to non-channelized system. "
  67. "[Default=" __stringify(IBMVFC_MIG_NO_SUB_TO_CRQ) "]");
  68. module_param_named(mig_no_less_channels, mig_no_less_channels, uint, S_IRUGO);
  69. MODULE_PARM_DESC(mig_no_less_channels, "Prevent migration to system with less channels. "
  70. "[Default=" __stringify(IBMVFC_MIG_NO_N_TO_M) "]");
  71. module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
  72. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
  73. "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
  74. module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
  75. MODULE_PARM_DESC(default_timeout,
  76. "Default timeout in seconds for initialization and EH commands. "
  77. "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
  78. module_param_named(max_requests, max_requests, uint, S_IRUGO);
  79. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
  80. "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
  81. module_param_named(max_sectors, max_sectors, ushort, S_IRUGO);
  82. MODULE_PARM_DESC(max_sectors, "Maximum sectors for this adapter. "
  83. "[Default=" __stringify(IBMVFC_MAX_SECTORS) "]");
  84. module_param_named(scsi_qdepth, scsi_qdepth, ushort, S_IRUGO);
  85. MODULE_PARM_DESC(scsi_qdepth, "Maximum scsi command depth per adapter queue. "
  86. "[Default=" __stringify(IBMVFC_SCSI_QDEPTH) "]");
  87. module_param_named(max_lun, max_lun, ullong, S_IRUGO);
  88. MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
  89. "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
  90. module_param_named(max_targets, max_targets, uint, S_IRUGO);
  91. MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
  92. "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
  93. module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
  94. MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
  95. "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
  96. module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
  97. MODULE_PARM_DESC(debug, "Enable driver debug information. "
  98. "[Default=" __stringify(IBMVFC_DEBUG) "]");
  99. module_param_named(log_level, log_level, uint, 0);
  100. MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
  101. "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
  102. module_param_named(cls3_error, cls3_error, uint, 0);
  103. MODULE_PARM_DESC(cls3_error, "Enable FC Class 3 Error Recovery. "
  104. "[Default=" __stringify(IBMVFC_CLS3_ERROR) "]");
  105. static const struct {
  106. u16 status;
  107. u16 error;
  108. u8 result;
  109. u8 retry;
  110. int log;
  111. char *name;
  112. } cmd_status [] = {
  113. { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
  114. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
  115. { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
  116. { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
  117. { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
  118. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
  119. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
  120. { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
  121. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
  122. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
  123. { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
  124. { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
  125. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
  126. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
  127. { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
  128. { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
  129. { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
  130. { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
  131. { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
  132. { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
  133. { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
  134. { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
  135. { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
  136. { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
  137. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
  138. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
  139. { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
  140. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
  141. { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
  142. { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
  143. { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
  144. { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
  145. { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
  146. { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
  147. { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
  148. { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
  149. { IBMVFC_FC_SCSI_ERROR, IBMVFC_COMMAND_FAILED, DID_ERROR, 0, 1, "PRLI to device failed." },
  150. };
  151. static void ibmvfc_npiv_login(struct ibmvfc_host *);
  152. static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
  153. static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
  154. static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
  155. static void ibmvfc_npiv_logout(struct ibmvfc_host *);
  156. static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *);
  157. static void ibmvfc_tgt_move_login(struct ibmvfc_target *);
  158. static void ibmvfc_dereg_sub_crqs(struct ibmvfc_host *, struct ibmvfc_channels *);
  159. static void ibmvfc_reg_sub_crqs(struct ibmvfc_host *, struct ibmvfc_channels *);
  160. static const char *unknown_error = "unknown error";
  161. static long h_reg_sub_crq(unsigned long unit_address, unsigned long ioba,
  162. unsigned long length, unsigned long *cookie,
  163. unsigned long *irq)
  164. {
  165. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  166. long rc;
  167. rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, ioba, length);
  168. *cookie = retbuf[0];
  169. *irq = retbuf[1];
  170. return rc;
  171. }
  172. static int ibmvfc_check_caps(struct ibmvfc_host *vhost, unsigned long cap_flags)
  173. {
  174. u64 host_caps = be64_to_cpu(vhost->login_buf->resp.capabilities);
  175. return (host_caps & cap_flags) ? 1 : 0;
  176. }
  177. static struct ibmvfc_fcp_cmd_iu *ibmvfc_get_fcp_iu(struct ibmvfc_host *vhost,
  178. struct ibmvfc_cmd *vfc_cmd)
  179. {
  180. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
  181. return &vfc_cmd->v2.iu;
  182. else
  183. return &vfc_cmd->v1.iu;
  184. }
  185. static struct ibmvfc_fcp_rsp *ibmvfc_get_fcp_rsp(struct ibmvfc_host *vhost,
  186. struct ibmvfc_cmd *vfc_cmd)
  187. {
  188. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
  189. return &vfc_cmd->v2.rsp;
  190. else
  191. return &vfc_cmd->v1.rsp;
  192. }
  193. #ifdef CONFIG_SCSI_IBMVFC_TRACE
  194. /**
  195. * ibmvfc_trc_start - Log a start trace entry
  196. * @evt: ibmvfc event struct
  197. *
  198. **/
  199. static void ibmvfc_trc_start(struct ibmvfc_event *evt)
  200. {
  201. struct ibmvfc_host *vhost = evt->vhost;
  202. struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
  203. struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
  204. struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
  205. struct ibmvfc_trace_entry *entry;
  206. int index = atomic_inc_return(&vhost->trace_index) & IBMVFC_TRACE_INDEX_MASK;
  207. entry = &vhost->trace[index];
  208. entry->evt = evt;
  209. entry->time = jiffies;
  210. entry->fmt = evt->crq.format;
  211. entry->type = IBMVFC_TRC_START;
  212. switch (entry->fmt) {
  213. case IBMVFC_CMD_FORMAT:
  214. entry->op_code = iu->cdb[0];
  215. entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
  216. entry->lun = scsilun_to_int(&iu->lun);
  217. entry->tmf_flags = iu->tmf_flags;
  218. entry->u.start.xfer_len = be32_to_cpu(iu->xfer_len);
  219. break;
  220. case IBMVFC_MAD_FORMAT:
  221. entry->op_code = be32_to_cpu(mad->opcode);
  222. break;
  223. default:
  224. break;
  225. }
  226. }
  227. /**
  228. * ibmvfc_trc_end - Log an end trace entry
  229. * @evt: ibmvfc event struct
  230. *
  231. **/
  232. static void ibmvfc_trc_end(struct ibmvfc_event *evt)
  233. {
  234. struct ibmvfc_host *vhost = evt->vhost;
  235. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  236. struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
  237. struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
  238. struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
  239. struct ibmvfc_trace_entry *entry;
  240. int index = atomic_inc_return(&vhost->trace_index) & IBMVFC_TRACE_INDEX_MASK;
  241. entry = &vhost->trace[index];
  242. entry->evt = evt;
  243. entry->time = jiffies;
  244. entry->fmt = evt->crq.format;
  245. entry->type = IBMVFC_TRC_END;
  246. switch (entry->fmt) {
  247. case IBMVFC_CMD_FORMAT:
  248. entry->op_code = iu->cdb[0];
  249. entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
  250. entry->lun = scsilun_to_int(&iu->lun);
  251. entry->tmf_flags = iu->tmf_flags;
  252. entry->u.end.status = be16_to_cpu(vfc_cmd->status);
  253. entry->u.end.error = be16_to_cpu(vfc_cmd->error);
  254. entry->u.end.fcp_rsp_flags = rsp->flags;
  255. entry->u.end.rsp_code = rsp->data.info.rsp_code;
  256. entry->u.end.scsi_status = rsp->scsi_status;
  257. break;
  258. case IBMVFC_MAD_FORMAT:
  259. entry->op_code = be32_to_cpu(mad->opcode);
  260. entry->u.end.status = be16_to_cpu(mad->status);
  261. break;
  262. default:
  263. break;
  264. }
  265. }
  266. #else
  267. #define ibmvfc_trc_start(evt) do { } while (0)
  268. #define ibmvfc_trc_end(evt) do { } while (0)
  269. #endif
  270. /**
  271. * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
  272. * @status: status / error class
  273. * @error: error
  274. *
  275. * Return value:
  276. * index into cmd_status / -EINVAL on failure
  277. **/
  278. static int ibmvfc_get_err_index(u16 status, u16 error)
  279. {
  280. int i;
  281. for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
  282. if ((cmd_status[i].status & status) == cmd_status[i].status &&
  283. cmd_status[i].error == error)
  284. return i;
  285. return -EINVAL;
  286. }
  287. /**
  288. * ibmvfc_get_cmd_error - Find the error description for the fcp response
  289. * @status: status / error class
  290. * @error: error
  291. *
  292. * Return value:
  293. * error description string
  294. **/
  295. static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
  296. {
  297. int rc = ibmvfc_get_err_index(status, error);
  298. if (rc >= 0)
  299. return cmd_status[rc].name;
  300. return unknown_error;
  301. }
  302. /**
  303. * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
  304. * @vhost: ibmvfc host struct
  305. * @vfc_cmd: ibmvfc command struct
  306. *
  307. * Return value:
  308. * SCSI result value to return for completed command
  309. **/
  310. static int ibmvfc_get_err_result(struct ibmvfc_host *vhost, struct ibmvfc_cmd *vfc_cmd)
  311. {
  312. int err;
  313. struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
  314. int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
  315. if ((rsp->flags & FCP_RSP_LEN_VALID) &&
  316. ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
  317. rsp->data.info.rsp_code))
  318. return DID_ERROR << 16;
  319. err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
  320. if (err >= 0)
  321. return rsp->scsi_status | (cmd_status[err].result << 16);
  322. return rsp->scsi_status | (DID_ERROR << 16);
  323. }
  324. /**
  325. * ibmvfc_retry_cmd - Determine if error status is retryable
  326. * @status: status / error class
  327. * @error: error
  328. *
  329. * Return value:
  330. * 1 if error should be retried / 0 if it should not
  331. **/
  332. static int ibmvfc_retry_cmd(u16 status, u16 error)
  333. {
  334. int rc = ibmvfc_get_err_index(status, error);
  335. if (rc >= 0)
  336. return cmd_status[rc].retry;
  337. return 1;
  338. }
  339. static const char *unknown_fc_explain = "unknown fc explain";
  340. static const struct {
  341. u16 fc_explain;
  342. char *name;
  343. } ls_explain [] = {
  344. { 0x00, "no additional explanation" },
  345. { 0x01, "service parameter error - options" },
  346. { 0x03, "service parameter error - initiator control" },
  347. { 0x05, "service parameter error - recipient control" },
  348. { 0x07, "service parameter error - received data field size" },
  349. { 0x09, "service parameter error - concurrent seq" },
  350. { 0x0B, "service parameter error - credit" },
  351. { 0x0D, "invalid N_Port/F_Port_Name" },
  352. { 0x0E, "invalid node/Fabric Name" },
  353. { 0x0F, "invalid common service parameters" },
  354. { 0x11, "invalid association header" },
  355. { 0x13, "association header required" },
  356. { 0x15, "invalid originator S_ID" },
  357. { 0x17, "invalid OX_ID-RX-ID combination" },
  358. { 0x19, "command (request) already in progress" },
  359. { 0x1E, "N_Port Login requested" },
  360. { 0x1F, "Invalid N_Port_ID" },
  361. };
  362. static const struct {
  363. u16 fc_explain;
  364. char *name;
  365. } gs_explain [] = {
  366. { 0x00, "no additional explanation" },
  367. { 0x01, "port identifier not registered" },
  368. { 0x02, "port name not registered" },
  369. { 0x03, "node name not registered" },
  370. { 0x04, "class of service not registered" },
  371. { 0x06, "initial process associator not registered" },
  372. { 0x07, "FC-4 TYPEs not registered" },
  373. { 0x08, "symbolic port name not registered" },
  374. { 0x09, "symbolic node name not registered" },
  375. { 0x0A, "port type not registered" },
  376. { 0xF0, "authorization exception" },
  377. { 0xF1, "authentication exception" },
  378. { 0xF2, "data base full" },
  379. { 0xF3, "data base empty" },
  380. { 0xF4, "processing request" },
  381. { 0xF5, "unable to verify connection" },
  382. { 0xF6, "devices not in a common zone" },
  383. };
  384. /**
  385. * ibmvfc_get_ls_explain - Return the FC Explain description text
  386. * @status: FC Explain status
  387. *
  388. * Returns:
  389. * error string
  390. **/
  391. static const char *ibmvfc_get_ls_explain(u16 status)
  392. {
  393. int i;
  394. for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
  395. if (ls_explain[i].fc_explain == status)
  396. return ls_explain[i].name;
  397. return unknown_fc_explain;
  398. }
  399. /**
  400. * ibmvfc_get_gs_explain - Return the FC Explain description text
  401. * @status: FC Explain status
  402. *
  403. * Returns:
  404. * error string
  405. **/
  406. static const char *ibmvfc_get_gs_explain(u16 status)
  407. {
  408. int i;
  409. for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
  410. if (gs_explain[i].fc_explain == status)
  411. return gs_explain[i].name;
  412. return unknown_fc_explain;
  413. }
  414. static const struct {
  415. enum ibmvfc_fc_type fc_type;
  416. char *name;
  417. } fc_type [] = {
  418. { IBMVFC_FABRIC_REJECT, "fabric reject" },
  419. { IBMVFC_PORT_REJECT, "port reject" },
  420. { IBMVFC_LS_REJECT, "ELS reject" },
  421. { IBMVFC_FABRIC_BUSY, "fabric busy" },
  422. { IBMVFC_PORT_BUSY, "port busy" },
  423. { IBMVFC_BASIC_REJECT, "basic reject" },
  424. };
  425. static const char *unknown_fc_type = "unknown fc type";
  426. /**
  427. * ibmvfc_get_fc_type - Return the FC Type description text
  428. * @status: FC Type error status
  429. *
  430. * Returns:
  431. * error string
  432. **/
  433. static const char *ibmvfc_get_fc_type(u16 status)
  434. {
  435. int i;
  436. for (i = 0; i < ARRAY_SIZE(fc_type); i++)
  437. if (fc_type[i].fc_type == status)
  438. return fc_type[i].name;
  439. return unknown_fc_type;
  440. }
  441. /**
  442. * ibmvfc_set_tgt_action - Set the next init action for the target
  443. * @tgt: ibmvfc target struct
  444. * @action: action to perform
  445. *
  446. * Returns:
  447. * 0 if action changed / non-zero if not changed
  448. **/
  449. static int ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
  450. enum ibmvfc_target_action action)
  451. {
  452. int rc = -EINVAL;
  453. switch (tgt->action) {
  454. case IBMVFC_TGT_ACTION_LOGOUT_RPORT:
  455. if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT ||
  456. action == IBMVFC_TGT_ACTION_DEL_RPORT) {
  457. tgt->action = action;
  458. rc = 0;
  459. }
  460. break;
  461. case IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT:
  462. if (action == IBMVFC_TGT_ACTION_DEL_RPORT ||
  463. action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
  464. tgt->action = action;
  465. rc = 0;
  466. }
  467. break;
  468. case IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT:
  469. if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
  470. tgt->action = action;
  471. rc = 0;
  472. }
  473. break;
  474. case IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT:
  475. if (action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
  476. tgt->action = action;
  477. rc = 0;
  478. }
  479. break;
  480. case IBMVFC_TGT_ACTION_DEL_RPORT:
  481. if (action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
  482. tgt->action = action;
  483. rc = 0;
  484. }
  485. break;
  486. case IBMVFC_TGT_ACTION_DELETED_RPORT:
  487. break;
  488. default:
  489. tgt->action = action;
  490. rc = 0;
  491. break;
  492. }
  493. if (action >= IBMVFC_TGT_ACTION_LOGOUT_RPORT)
  494. tgt->add_rport = 0;
  495. return rc;
  496. }
  497. /**
  498. * ibmvfc_set_host_state - Set the state for the host
  499. * @vhost: ibmvfc host struct
  500. * @state: state to set host to
  501. *
  502. * Returns:
  503. * 0 if state changed / non-zero if not changed
  504. **/
  505. static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
  506. enum ibmvfc_host_state state)
  507. {
  508. int rc = 0;
  509. switch (vhost->state) {
  510. case IBMVFC_HOST_OFFLINE:
  511. rc = -EINVAL;
  512. break;
  513. default:
  514. vhost->state = state;
  515. break;
  516. }
  517. return rc;
  518. }
  519. /**
  520. * ibmvfc_set_host_action - Set the next init action for the host
  521. * @vhost: ibmvfc host struct
  522. * @action: action to perform
  523. *
  524. **/
  525. static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
  526. enum ibmvfc_host_action action)
  527. {
  528. switch (action) {
  529. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  530. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
  531. vhost->action = action;
  532. break;
  533. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  534. if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
  535. vhost->action = action;
  536. break;
  537. case IBMVFC_HOST_ACTION_INIT_WAIT:
  538. if (vhost->action == IBMVFC_HOST_ACTION_INIT)
  539. vhost->action = action;
  540. break;
  541. case IBMVFC_HOST_ACTION_QUERY:
  542. switch (vhost->action) {
  543. case IBMVFC_HOST_ACTION_INIT_WAIT:
  544. case IBMVFC_HOST_ACTION_NONE:
  545. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  546. vhost->action = action;
  547. break;
  548. default:
  549. break;
  550. }
  551. break;
  552. case IBMVFC_HOST_ACTION_TGT_INIT:
  553. if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
  554. vhost->action = action;
  555. break;
  556. case IBMVFC_HOST_ACTION_REENABLE:
  557. case IBMVFC_HOST_ACTION_RESET:
  558. vhost->action = action;
  559. break;
  560. case IBMVFC_HOST_ACTION_INIT:
  561. case IBMVFC_HOST_ACTION_TGT_DEL:
  562. case IBMVFC_HOST_ACTION_LOGO:
  563. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  564. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  565. case IBMVFC_HOST_ACTION_NONE:
  566. default:
  567. switch (vhost->action) {
  568. case IBMVFC_HOST_ACTION_RESET:
  569. case IBMVFC_HOST_ACTION_REENABLE:
  570. break;
  571. default:
  572. vhost->action = action;
  573. break;
  574. }
  575. break;
  576. }
  577. }
  578. /**
  579. * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
  580. * @vhost: ibmvfc host struct
  581. *
  582. * Return value:
  583. * nothing
  584. **/
  585. static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
  586. {
  587. if (vhost->action == IBMVFC_HOST_ACTION_NONE &&
  588. vhost->state == IBMVFC_ACTIVE) {
  589. if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  590. scsi_block_requests(vhost->host);
  591. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  592. }
  593. } else
  594. vhost->reinit = 1;
  595. wake_up(&vhost->work_wait_q);
  596. }
  597. /**
  598. * ibmvfc_del_tgt - Schedule cleanup and removal of the target
  599. * @tgt: ibmvfc target struct
  600. **/
  601. static void ibmvfc_del_tgt(struct ibmvfc_target *tgt)
  602. {
  603. if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT)) {
  604. tgt->job_step = ibmvfc_tgt_implicit_logout_and_del;
  605. tgt->init_retries = 0;
  606. }
  607. wake_up(&tgt->vhost->work_wait_q);
  608. }
  609. /**
  610. * ibmvfc_link_down - Handle a link down event from the adapter
  611. * @vhost: ibmvfc host struct
  612. * @state: ibmvfc host state to enter
  613. *
  614. **/
  615. static void ibmvfc_link_down(struct ibmvfc_host *vhost,
  616. enum ibmvfc_host_state state)
  617. {
  618. struct ibmvfc_target *tgt;
  619. ENTER;
  620. scsi_block_requests(vhost->host);
  621. list_for_each_entry(tgt, &vhost->targets, queue)
  622. ibmvfc_del_tgt(tgt);
  623. ibmvfc_set_host_state(vhost, state);
  624. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
  625. vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
  626. wake_up(&vhost->work_wait_q);
  627. LEAVE;
  628. }
  629. /**
  630. * ibmvfc_init_host - Start host initialization
  631. * @vhost: ibmvfc host struct
  632. *
  633. * Return value:
  634. * nothing
  635. **/
  636. static void ibmvfc_init_host(struct ibmvfc_host *vhost)
  637. {
  638. struct ibmvfc_target *tgt;
  639. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
  640. if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
  641. dev_err(vhost->dev,
  642. "Host initialization retries exceeded. Taking adapter offline\n");
  643. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  644. return;
  645. }
  646. }
  647. if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  648. memset(vhost->async_crq.msgs.async, 0, PAGE_SIZE);
  649. vhost->async_crq.cur = 0;
  650. list_for_each_entry(tgt, &vhost->targets, queue) {
  651. if (vhost->client_migrated)
  652. tgt->need_login = 1;
  653. else
  654. ibmvfc_del_tgt(tgt);
  655. }
  656. scsi_block_requests(vhost->host);
  657. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  658. vhost->job_step = ibmvfc_npiv_login;
  659. wake_up(&vhost->work_wait_q);
  660. }
  661. }
  662. /**
  663. * ibmvfc_send_crq - Send a CRQ
  664. * @vhost: ibmvfc host struct
  665. * @word1: the first 64 bits of the data
  666. * @word2: the second 64 bits of the data
  667. *
  668. * Return value:
  669. * 0 on success / other on failure
  670. **/
  671. static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
  672. {
  673. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  674. return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
  675. }
  676. static int ibmvfc_send_sub_crq(struct ibmvfc_host *vhost, u64 cookie, u64 word1,
  677. u64 word2, u64 word3, u64 word4)
  678. {
  679. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  680. return plpar_hcall_norets(H_SEND_SUB_CRQ, vdev->unit_address, cookie,
  681. word1, word2, word3, word4);
  682. }
  683. /**
  684. * ibmvfc_send_crq_init - Send a CRQ init message
  685. * @vhost: ibmvfc host struct
  686. *
  687. * Return value:
  688. * 0 on success / other on failure
  689. **/
  690. static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
  691. {
  692. ibmvfc_dbg(vhost, "Sending CRQ init\n");
  693. return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
  694. }
  695. /**
  696. * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
  697. * @vhost: ibmvfc host struct
  698. *
  699. * Return value:
  700. * 0 on success / other on failure
  701. **/
  702. static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
  703. {
  704. ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
  705. return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
  706. }
  707. /**
  708. * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
  709. * @vhost: ibmvfc host who owns the event pool
  710. * @queue: ibmvfc queue struct
  711. *
  712. * Returns zero on success.
  713. **/
  714. static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost,
  715. struct ibmvfc_queue *queue)
  716. {
  717. int i;
  718. struct ibmvfc_event_pool *pool = &queue->evt_pool;
  719. ENTER;
  720. if (!queue->total_depth)
  721. return 0;
  722. pool->size = queue->total_depth;
  723. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  724. if (!pool->events)
  725. return -ENOMEM;
  726. pool->iu_storage = dma_alloc_coherent(vhost->dev,
  727. pool->size * sizeof(*pool->iu_storage),
  728. &pool->iu_token, 0);
  729. if (!pool->iu_storage) {
  730. kfree(pool->events);
  731. return -ENOMEM;
  732. }
  733. INIT_LIST_HEAD(&queue->sent);
  734. INIT_LIST_HEAD(&queue->free);
  735. queue->evt_free = queue->evt_depth;
  736. queue->reserved_free = queue->reserved_depth;
  737. spin_lock_init(&queue->l_lock);
  738. for (i = 0; i < pool->size; ++i) {
  739. struct ibmvfc_event *evt = &pool->events[i];
  740. /*
  741. * evt->active states
  742. * 1 = in flight
  743. * 0 = being completed
  744. * -1 = free/freed
  745. */
  746. atomic_set(&evt->active, -1);
  747. atomic_set(&evt->free, 1);
  748. evt->crq.valid = 0x80;
  749. evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
  750. evt->xfer_iu = pool->iu_storage + i;
  751. evt->vhost = vhost;
  752. evt->queue = queue;
  753. evt->ext_list = NULL;
  754. list_add_tail(&evt->queue_list, &queue->free);
  755. }
  756. LEAVE;
  757. return 0;
  758. }
  759. /**
  760. * ibmvfc_free_event_pool - Frees memory of the event pool of a host
  761. * @vhost: ibmvfc host who owns the event pool
  762. * @queue: ibmvfc queue struct
  763. *
  764. **/
  765. static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost,
  766. struct ibmvfc_queue *queue)
  767. {
  768. int i;
  769. struct ibmvfc_event_pool *pool = &queue->evt_pool;
  770. ENTER;
  771. for (i = 0; i < pool->size; ++i) {
  772. list_del(&pool->events[i].queue_list);
  773. BUG_ON(atomic_read(&pool->events[i].free) != 1);
  774. if (pool->events[i].ext_list)
  775. dma_pool_free(vhost->sg_pool,
  776. pool->events[i].ext_list,
  777. pool->events[i].ext_list_token);
  778. }
  779. kfree(pool->events);
  780. dma_free_coherent(vhost->dev,
  781. pool->size * sizeof(*pool->iu_storage),
  782. pool->iu_storage, pool->iu_token);
  783. LEAVE;
  784. }
  785. /**
  786. * ibmvfc_free_queue - Deallocate queue
  787. * @vhost: ibmvfc host struct
  788. * @queue: ibmvfc queue struct
  789. *
  790. * Unmaps dma and deallocates page for messages
  791. **/
  792. static void ibmvfc_free_queue(struct ibmvfc_host *vhost,
  793. struct ibmvfc_queue *queue)
  794. {
  795. struct device *dev = vhost->dev;
  796. dma_unmap_single(dev, queue->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
  797. free_page((unsigned long)queue->msgs.handle);
  798. queue->msgs.handle = NULL;
  799. ibmvfc_free_event_pool(vhost, queue);
  800. }
  801. /**
  802. * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
  803. * @vhost: ibmvfc host struct
  804. *
  805. * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
  806. * the crq with the hypervisor.
  807. **/
  808. static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
  809. {
  810. long rc = 0;
  811. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  812. struct ibmvfc_queue *crq = &vhost->crq;
  813. ibmvfc_dbg(vhost, "Releasing CRQ\n");
  814. free_irq(vdev->irq, vhost);
  815. tasklet_kill(&vhost->tasklet);
  816. do {
  817. if (rc)
  818. msleep(100);
  819. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  820. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  821. vhost->state = IBMVFC_NO_CRQ;
  822. vhost->logged_in = 0;
  823. ibmvfc_free_queue(vhost, crq);
  824. }
  825. /**
  826. * ibmvfc_reenable_crq_queue - reenables the CRQ
  827. * @vhost: ibmvfc host struct
  828. *
  829. * Return value:
  830. * 0 on success / other on failure
  831. **/
  832. static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
  833. {
  834. int rc = 0;
  835. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  836. unsigned long flags;
  837. ibmvfc_dereg_sub_crqs(vhost, &vhost->scsi_scrqs);
  838. /* Re-enable the CRQ */
  839. do {
  840. if (rc)
  841. msleep(100);
  842. rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
  843. } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
  844. if (rc)
  845. dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
  846. spin_lock_irqsave(vhost->host->host_lock, flags);
  847. spin_lock(vhost->crq.q_lock);
  848. vhost->do_enquiry = 1;
  849. vhost->using_channels = 0;
  850. spin_unlock(vhost->crq.q_lock);
  851. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  852. ibmvfc_reg_sub_crqs(vhost, &vhost->scsi_scrqs);
  853. return rc;
  854. }
  855. /**
  856. * ibmvfc_reset_crq - resets a crq after a failure
  857. * @vhost: ibmvfc host struct
  858. *
  859. * Return value:
  860. * 0 on success / other on failure
  861. **/
  862. static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
  863. {
  864. int rc = 0;
  865. unsigned long flags;
  866. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  867. struct ibmvfc_queue *crq = &vhost->crq;
  868. ibmvfc_dereg_sub_crqs(vhost, &vhost->scsi_scrqs);
  869. /* Close the CRQ */
  870. do {
  871. if (rc)
  872. msleep(100);
  873. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  874. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  875. spin_lock_irqsave(vhost->host->host_lock, flags);
  876. spin_lock(vhost->crq.q_lock);
  877. vhost->state = IBMVFC_NO_CRQ;
  878. vhost->logged_in = 0;
  879. vhost->do_enquiry = 1;
  880. vhost->using_channels = 0;
  881. /* Clean out the queue */
  882. memset(crq->msgs.crq, 0, PAGE_SIZE);
  883. crq->cur = 0;
  884. /* And re-open it again */
  885. rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  886. crq->msg_token, PAGE_SIZE);
  887. if (rc == H_CLOSED)
  888. /* Adapter is good, but other end is not ready */
  889. dev_warn(vhost->dev, "Partner adapter not ready\n");
  890. else if (rc != 0)
  891. dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
  892. spin_unlock(vhost->crq.q_lock);
  893. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  894. ibmvfc_reg_sub_crqs(vhost, &vhost->scsi_scrqs);
  895. return rc;
  896. }
  897. /**
  898. * ibmvfc_valid_event - Determines if event is valid.
  899. * @pool: event_pool that contains the event
  900. * @evt: ibmvfc event to be checked for validity
  901. *
  902. * Return value:
  903. * 1 if event is valid / 0 if event is not valid
  904. **/
  905. static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
  906. struct ibmvfc_event *evt)
  907. {
  908. int index = evt - pool->events;
  909. if (index < 0 || index >= pool->size) /* outside of bounds */
  910. return 0;
  911. if (evt != pool->events + index) /* unaligned */
  912. return 0;
  913. return 1;
  914. }
  915. /**
  916. * ibmvfc_free_event - Free the specified event
  917. * @evt: ibmvfc_event to be freed
  918. *
  919. **/
  920. static void ibmvfc_free_event(struct ibmvfc_event *evt)
  921. {
  922. struct ibmvfc_event_pool *pool = &evt->queue->evt_pool;
  923. unsigned long flags;
  924. BUG_ON(!ibmvfc_valid_event(pool, evt));
  925. BUG_ON(atomic_inc_return(&evt->free) != 1);
  926. BUG_ON(atomic_dec_and_test(&evt->active));
  927. spin_lock_irqsave(&evt->queue->l_lock, flags);
  928. list_add_tail(&evt->queue_list, &evt->queue->free);
  929. if (evt->reserved) {
  930. evt->reserved = 0;
  931. evt->queue->reserved_free++;
  932. } else {
  933. evt->queue->evt_free++;
  934. }
  935. if (evt->eh_comp)
  936. complete(evt->eh_comp);
  937. spin_unlock_irqrestore(&evt->queue->l_lock, flags);
  938. }
  939. /**
  940. * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
  941. * @evt: ibmvfc event struct
  942. *
  943. * This function does not setup any error status, that must be done
  944. * before this function gets called.
  945. **/
  946. static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
  947. {
  948. struct scsi_cmnd *cmnd = evt->cmnd;
  949. if (cmnd) {
  950. scsi_dma_unmap(cmnd);
  951. scsi_done(cmnd);
  952. }
  953. ibmvfc_free_event(evt);
  954. }
  955. /**
  956. * ibmvfc_complete_purge - Complete failed command list
  957. * @purge_list: list head of failed commands
  958. *
  959. * This function runs completions on commands to fail as a result of a
  960. * host reset or platform migration.
  961. **/
  962. static void ibmvfc_complete_purge(struct list_head *purge_list)
  963. {
  964. struct ibmvfc_event *evt, *pos;
  965. list_for_each_entry_safe(evt, pos, purge_list, queue_list) {
  966. list_del(&evt->queue_list);
  967. ibmvfc_trc_end(evt);
  968. evt->done(evt);
  969. }
  970. }
  971. /**
  972. * ibmvfc_fail_request - Fail request with specified error code
  973. * @evt: ibmvfc event struct
  974. * @error_code: error code to fail request with
  975. *
  976. * Return value:
  977. * none
  978. **/
  979. static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
  980. {
  981. /*
  982. * Anything we are failing should still be active. Otherwise, it
  983. * implies we already got a response for the command and are doing
  984. * something bad like double completing it.
  985. */
  986. BUG_ON(!atomic_dec_and_test(&evt->active));
  987. if (evt->cmnd) {
  988. evt->cmnd->result = (error_code << 16);
  989. evt->done = ibmvfc_scsi_eh_done;
  990. } else
  991. evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
  992. del_timer(&evt->timer);
  993. }
  994. /**
  995. * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
  996. * @vhost: ibmvfc host struct
  997. * @error_code: error code to fail requests with
  998. *
  999. * Return value:
  1000. * none
  1001. **/
  1002. static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
  1003. {
  1004. struct ibmvfc_event *evt, *pos;
  1005. struct ibmvfc_queue *queues = vhost->scsi_scrqs.scrqs;
  1006. unsigned long flags;
  1007. int hwqs = 0;
  1008. int i;
  1009. if (vhost->using_channels)
  1010. hwqs = vhost->scsi_scrqs.active_queues;
  1011. ibmvfc_dbg(vhost, "Purging all requests\n");
  1012. spin_lock_irqsave(&vhost->crq.l_lock, flags);
  1013. list_for_each_entry_safe(evt, pos, &vhost->crq.sent, queue_list)
  1014. ibmvfc_fail_request(evt, error_code);
  1015. list_splice_init(&vhost->crq.sent, &vhost->purge);
  1016. spin_unlock_irqrestore(&vhost->crq.l_lock, flags);
  1017. for (i = 0; i < hwqs; i++) {
  1018. spin_lock_irqsave(queues[i].q_lock, flags);
  1019. spin_lock(&queues[i].l_lock);
  1020. list_for_each_entry_safe(evt, pos, &queues[i].sent, queue_list)
  1021. ibmvfc_fail_request(evt, error_code);
  1022. list_splice_init(&queues[i].sent, &vhost->purge);
  1023. spin_unlock(&queues[i].l_lock);
  1024. spin_unlock_irqrestore(queues[i].q_lock, flags);
  1025. }
  1026. }
  1027. /**
  1028. * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
  1029. * @vhost: struct ibmvfc host to reset
  1030. **/
  1031. static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
  1032. {
  1033. ibmvfc_purge_requests(vhost, DID_ERROR);
  1034. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  1035. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
  1036. }
  1037. /**
  1038. * __ibmvfc_reset_host - Reset the connection to the server (no locking)
  1039. * @vhost: struct ibmvfc host to reset
  1040. **/
  1041. static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
  1042. {
  1043. if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
  1044. !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  1045. scsi_block_requests(vhost->host);
  1046. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
  1047. vhost->job_step = ibmvfc_npiv_logout;
  1048. wake_up(&vhost->work_wait_q);
  1049. } else
  1050. ibmvfc_hard_reset_host(vhost);
  1051. }
  1052. /**
  1053. * ibmvfc_reset_host - Reset the connection to the server
  1054. * @vhost: ibmvfc host struct
  1055. **/
  1056. static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
  1057. {
  1058. unsigned long flags;
  1059. spin_lock_irqsave(vhost->host->host_lock, flags);
  1060. __ibmvfc_reset_host(vhost);
  1061. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1062. }
  1063. /**
  1064. * ibmvfc_retry_host_init - Retry host initialization if allowed
  1065. * @vhost: ibmvfc host struct
  1066. *
  1067. * Returns: 1 if init will be retried / 0 if not
  1068. *
  1069. **/
  1070. static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
  1071. {
  1072. int retry = 0;
  1073. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
  1074. vhost->delay_init = 1;
  1075. if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
  1076. dev_err(vhost->dev,
  1077. "Host initialization retries exceeded. Taking adapter offline\n");
  1078. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  1079. } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
  1080. __ibmvfc_reset_host(vhost);
  1081. else {
  1082. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  1083. retry = 1;
  1084. }
  1085. }
  1086. wake_up(&vhost->work_wait_q);
  1087. return retry;
  1088. }
  1089. /**
  1090. * __ibmvfc_get_target - Find the specified scsi_target (no locking)
  1091. * @starget: scsi target struct
  1092. *
  1093. * Return value:
  1094. * ibmvfc_target struct / NULL if not found
  1095. **/
  1096. static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
  1097. {
  1098. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1099. struct ibmvfc_host *vhost = shost_priv(shost);
  1100. struct ibmvfc_target *tgt;
  1101. list_for_each_entry(tgt, &vhost->targets, queue)
  1102. if (tgt->target_id == starget->id) {
  1103. kref_get(&tgt->kref);
  1104. return tgt;
  1105. }
  1106. return NULL;
  1107. }
  1108. /**
  1109. * ibmvfc_get_target - Find the specified scsi_target
  1110. * @starget: scsi target struct
  1111. *
  1112. * Return value:
  1113. * ibmvfc_target struct / NULL if not found
  1114. **/
  1115. static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
  1116. {
  1117. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1118. struct ibmvfc_target *tgt;
  1119. unsigned long flags;
  1120. spin_lock_irqsave(shost->host_lock, flags);
  1121. tgt = __ibmvfc_get_target(starget);
  1122. spin_unlock_irqrestore(shost->host_lock, flags);
  1123. return tgt;
  1124. }
  1125. /**
  1126. * ibmvfc_get_host_speed - Get host port speed
  1127. * @shost: scsi host struct
  1128. *
  1129. * Return value:
  1130. * none
  1131. **/
  1132. static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
  1133. {
  1134. struct ibmvfc_host *vhost = shost_priv(shost);
  1135. unsigned long flags;
  1136. spin_lock_irqsave(shost->host_lock, flags);
  1137. if (vhost->state == IBMVFC_ACTIVE) {
  1138. switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
  1139. case 1:
  1140. fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
  1141. break;
  1142. case 2:
  1143. fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
  1144. break;
  1145. case 4:
  1146. fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
  1147. break;
  1148. case 8:
  1149. fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
  1150. break;
  1151. case 10:
  1152. fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
  1153. break;
  1154. case 16:
  1155. fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
  1156. break;
  1157. default:
  1158. ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
  1159. be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
  1160. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  1161. break;
  1162. }
  1163. } else
  1164. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  1165. spin_unlock_irqrestore(shost->host_lock, flags);
  1166. }
  1167. /**
  1168. * ibmvfc_get_host_port_state - Get host port state
  1169. * @shost: scsi host struct
  1170. *
  1171. * Return value:
  1172. * none
  1173. **/
  1174. static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
  1175. {
  1176. struct ibmvfc_host *vhost = shost_priv(shost);
  1177. unsigned long flags;
  1178. spin_lock_irqsave(shost->host_lock, flags);
  1179. switch (vhost->state) {
  1180. case IBMVFC_INITIALIZING:
  1181. case IBMVFC_ACTIVE:
  1182. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  1183. break;
  1184. case IBMVFC_LINK_DOWN:
  1185. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  1186. break;
  1187. case IBMVFC_LINK_DEAD:
  1188. case IBMVFC_HOST_OFFLINE:
  1189. fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
  1190. break;
  1191. case IBMVFC_HALTED:
  1192. fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
  1193. break;
  1194. case IBMVFC_NO_CRQ:
  1195. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  1196. break;
  1197. default:
  1198. ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
  1199. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  1200. break;
  1201. }
  1202. spin_unlock_irqrestore(shost->host_lock, flags);
  1203. }
  1204. /**
  1205. * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
  1206. * @rport: rport struct
  1207. * @timeout: timeout value
  1208. *
  1209. * Return value:
  1210. * none
  1211. **/
  1212. static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  1213. {
  1214. if (timeout)
  1215. rport->dev_loss_tmo = timeout;
  1216. else
  1217. rport->dev_loss_tmo = 1;
  1218. }
  1219. /**
  1220. * ibmvfc_release_tgt - Free memory allocated for a target
  1221. * @kref: kref struct
  1222. *
  1223. **/
  1224. static void ibmvfc_release_tgt(struct kref *kref)
  1225. {
  1226. struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
  1227. kfree(tgt);
  1228. }
  1229. /**
  1230. * ibmvfc_get_starget_node_name - Get SCSI target's node name
  1231. * @starget: scsi target struct
  1232. *
  1233. * Return value:
  1234. * none
  1235. **/
  1236. static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
  1237. {
  1238. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  1239. fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
  1240. if (tgt)
  1241. kref_put(&tgt->kref, ibmvfc_release_tgt);
  1242. }
  1243. /**
  1244. * ibmvfc_get_starget_port_name - Get SCSI target's port name
  1245. * @starget: scsi target struct
  1246. *
  1247. * Return value:
  1248. * none
  1249. **/
  1250. static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
  1251. {
  1252. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  1253. fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
  1254. if (tgt)
  1255. kref_put(&tgt->kref, ibmvfc_release_tgt);
  1256. }
  1257. /**
  1258. * ibmvfc_get_starget_port_id - Get SCSI target's port ID
  1259. * @starget: scsi target struct
  1260. *
  1261. * Return value:
  1262. * none
  1263. **/
  1264. static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
  1265. {
  1266. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  1267. fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
  1268. if (tgt)
  1269. kref_put(&tgt->kref, ibmvfc_release_tgt);
  1270. }
  1271. /**
  1272. * ibmvfc_wait_while_resetting - Wait while the host resets
  1273. * @vhost: ibmvfc host struct
  1274. *
  1275. * Return value:
  1276. * 0 on success / other on failure
  1277. **/
  1278. static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
  1279. {
  1280. long timeout = wait_event_timeout(vhost->init_wait_q,
  1281. ((vhost->state == IBMVFC_ACTIVE ||
  1282. vhost->state == IBMVFC_HOST_OFFLINE ||
  1283. vhost->state == IBMVFC_LINK_DEAD) &&
  1284. vhost->action == IBMVFC_HOST_ACTION_NONE),
  1285. (init_timeout * HZ));
  1286. return timeout ? 0 : -EIO;
  1287. }
  1288. /**
  1289. * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
  1290. * @shost: scsi host struct
  1291. *
  1292. * Return value:
  1293. * 0 on success / other on failure
  1294. **/
  1295. static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
  1296. {
  1297. struct ibmvfc_host *vhost = shost_priv(shost);
  1298. dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
  1299. ibmvfc_reset_host(vhost);
  1300. return ibmvfc_wait_while_resetting(vhost);
  1301. }
  1302. /**
  1303. * ibmvfc_gather_partition_info - Gather info about the LPAR
  1304. * @vhost: ibmvfc host struct
  1305. *
  1306. * Return value:
  1307. * none
  1308. **/
  1309. static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
  1310. {
  1311. struct device_node *rootdn;
  1312. const char *name;
  1313. const unsigned int *num;
  1314. rootdn = of_find_node_by_path("/");
  1315. if (!rootdn)
  1316. return;
  1317. name = of_get_property(rootdn, "ibm,partition-name", NULL);
  1318. if (name)
  1319. strscpy(vhost->partition_name, name, sizeof(vhost->partition_name));
  1320. num = of_get_property(rootdn, "ibm,partition-no", NULL);
  1321. if (num)
  1322. vhost->partition_number = *num;
  1323. of_node_put(rootdn);
  1324. }
  1325. /**
  1326. * ibmvfc_set_login_info - Setup info for NPIV login
  1327. * @vhost: ibmvfc host struct
  1328. *
  1329. * Return value:
  1330. * none
  1331. **/
  1332. static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
  1333. {
  1334. struct ibmvfc_npiv_login *login_info = &vhost->login_info;
  1335. struct ibmvfc_queue *async_crq = &vhost->async_crq;
  1336. struct device_node *of_node = vhost->dev->of_node;
  1337. const char *location;
  1338. u16 max_cmds;
  1339. max_cmds = scsi_qdepth + IBMVFC_NUM_INTERNAL_REQ;
  1340. if (mq_enabled)
  1341. max_cmds += (scsi_qdepth + IBMVFC_NUM_INTERNAL_SUBQ_REQ) *
  1342. vhost->scsi_scrqs.desired_queues;
  1343. memset(login_info, 0, sizeof(*login_info));
  1344. login_info->ostype = cpu_to_be32(IBMVFC_OS_LINUX);
  1345. login_info->max_dma_len = cpu_to_be64(max_sectors << 9);
  1346. login_info->max_payload = cpu_to_be32(sizeof(struct ibmvfc_fcp_cmd_iu));
  1347. login_info->max_response = cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp));
  1348. login_info->partition_num = cpu_to_be32(vhost->partition_number);
  1349. login_info->vfc_frame_version = cpu_to_be32(1);
  1350. login_info->fcp_version = cpu_to_be16(3);
  1351. login_info->flags = cpu_to_be16(IBMVFC_FLUSH_ON_HALT);
  1352. if (vhost->client_migrated)
  1353. login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
  1354. login_info->max_cmds = cpu_to_be32(max_cmds);
  1355. login_info->capabilities = cpu_to_be64(IBMVFC_CAN_MIGRATE | IBMVFC_CAN_SEND_VF_WWPN);
  1356. if (vhost->mq_enabled || vhost->using_channels)
  1357. login_info->capabilities |= cpu_to_be64(IBMVFC_CAN_USE_CHANNELS);
  1358. login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token);
  1359. login_info->async.len = cpu_to_be32(async_crq->size *
  1360. sizeof(*async_crq->msgs.async));
  1361. strscpy(login_info->partition_name, vhost->partition_name,
  1362. sizeof(login_info->partition_name));
  1363. strscpy(login_info->device_name,
  1364. dev_name(&vhost->host->shost_gendev), sizeof(login_info->device_name));
  1365. location = of_get_property(of_node, "ibm,loc-code", NULL);
  1366. location = location ? location : dev_name(vhost->dev);
  1367. strscpy(login_info->drc_name, location, sizeof(login_info->drc_name));
  1368. }
  1369. /**
  1370. * __ibmvfc_get_event - Gets the next free event in pool
  1371. * @queue: ibmvfc queue struct
  1372. * @reserved: event is for a reserved management command
  1373. *
  1374. * Returns a free event from the pool.
  1375. **/
  1376. static struct ibmvfc_event *__ibmvfc_get_event(struct ibmvfc_queue *queue, int reserved)
  1377. {
  1378. struct ibmvfc_event *evt = NULL;
  1379. unsigned long flags;
  1380. spin_lock_irqsave(&queue->l_lock, flags);
  1381. if (reserved && queue->reserved_free) {
  1382. evt = list_entry(queue->free.next, struct ibmvfc_event, queue_list);
  1383. evt->reserved = 1;
  1384. queue->reserved_free--;
  1385. } else if (queue->evt_free) {
  1386. evt = list_entry(queue->free.next, struct ibmvfc_event, queue_list);
  1387. queue->evt_free--;
  1388. } else {
  1389. goto out;
  1390. }
  1391. atomic_set(&evt->free, 0);
  1392. list_del(&evt->queue_list);
  1393. out:
  1394. spin_unlock_irqrestore(&queue->l_lock, flags);
  1395. return evt;
  1396. }
  1397. #define ibmvfc_get_event(queue) __ibmvfc_get_event(queue, 0)
  1398. #define ibmvfc_get_reserved_event(queue) __ibmvfc_get_event(queue, 1)
  1399. /**
  1400. * ibmvfc_locked_done - Calls evt completion with host_lock held
  1401. * @evt: ibmvfc evt to complete
  1402. *
  1403. * All non-scsi command completion callbacks have the expectation that the
  1404. * host_lock is held. This callback is used by ibmvfc_init_event to wrap a
  1405. * MAD evt with the host_lock.
  1406. **/
  1407. static void ibmvfc_locked_done(struct ibmvfc_event *evt)
  1408. {
  1409. unsigned long flags;
  1410. spin_lock_irqsave(evt->vhost->host->host_lock, flags);
  1411. evt->_done(evt);
  1412. spin_unlock_irqrestore(evt->vhost->host->host_lock, flags);
  1413. }
  1414. /**
  1415. * ibmvfc_init_event - Initialize fields in an event struct that are always
  1416. * required.
  1417. * @evt: The event
  1418. * @done: Routine to call when the event is responded to
  1419. * @format: SRP or MAD format
  1420. **/
  1421. static void ibmvfc_init_event(struct ibmvfc_event *evt,
  1422. void (*done) (struct ibmvfc_event *), u8 format)
  1423. {
  1424. evt->cmnd = NULL;
  1425. evt->sync_iu = NULL;
  1426. evt->eh_comp = NULL;
  1427. evt->crq.format = format;
  1428. if (format == IBMVFC_CMD_FORMAT)
  1429. evt->done = done;
  1430. else {
  1431. evt->_done = done;
  1432. evt->done = ibmvfc_locked_done;
  1433. }
  1434. evt->hwq = 0;
  1435. }
  1436. /**
  1437. * ibmvfc_map_sg_list - Initialize scatterlist
  1438. * @scmd: scsi command struct
  1439. * @nseg: number of scatterlist segments
  1440. * @md: memory descriptor list to initialize
  1441. **/
  1442. static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
  1443. struct srp_direct_buf *md)
  1444. {
  1445. int i;
  1446. struct scatterlist *sg;
  1447. scsi_for_each_sg(scmd, sg, nseg, i) {
  1448. md[i].va = cpu_to_be64(sg_dma_address(sg));
  1449. md[i].len = cpu_to_be32(sg_dma_len(sg));
  1450. md[i].key = 0;
  1451. }
  1452. }
  1453. /**
  1454. * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes descriptor fields
  1455. * @scmd: struct scsi_cmnd with the scatterlist
  1456. * @evt: ibmvfc event struct
  1457. * @vfc_cmd: vfc_cmd that contains the memory descriptor
  1458. * @dev: device for which to map dma memory
  1459. *
  1460. * Returns:
  1461. * 0 on success / non-zero on failure
  1462. **/
  1463. static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
  1464. struct ibmvfc_event *evt,
  1465. struct ibmvfc_cmd *vfc_cmd, struct device *dev)
  1466. {
  1467. int sg_mapped;
  1468. struct srp_direct_buf *data = &vfc_cmd->ioba;
  1469. struct ibmvfc_host *vhost = dev_get_drvdata(dev);
  1470. struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(evt->vhost, vfc_cmd);
  1471. if (cls3_error)
  1472. vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR);
  1473. sg_mapped = scsi_dma_map(scmd);
  1474. if (!sg_mapped) {
  1475. vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
  1476. return 0;
  1477. } else if (unlikely(sg_mapped < 0)) {
  1478. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1479. scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
  1480. return sg_mapped;
  1481. }
  1482. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  1483. vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
  1484. iu->add_cdb_len |= IBMVFC_WRDATA;
  1485. } else {
  1486. vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
  1487. iu->add_cdb_len |= IBMVFC_RDDATA;
  1488. }
  1489. if (sg_mapped == 1) {
  1490. ibmvfc_map_sg_list(scmd, sg_mapped, data);
  1491. return 0;
  1492. }
  1493. vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
  1494. if (!evt->ext_list) {
  1495. evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
  1496. &evt->ext_list_token);
  1497. if (!evt->ext_list) {
  1498. scsi_dma_unmap(scmd);
  1499. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1500. scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
  1501. return -ENOMEM;
  1502. }
  1503. }
  1504. ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
  1505. data->va = cpu_to_be64(evt->ext_list_token);
  1506. data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
  1507. data->key = 0;
  1508. return 0;
  1509. }
  1510. /**
  1511. * ibmvfc_timeout - Internal command timeout handler
  1512. * @t: struct ibmvfc_event that timed out
  1513. *
  1514. * Called when an internally generated command times out
  1515. **/
  1516. static void ibmvfc_timeout(struct timer_list *t)
  1517. {
  1518. struct ibmvfc_event *evt = from_timer(evt, t, timer);
  1519. struct ibmvfc_host *vhost = evt->vhost;
  1520. dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
  1521. ibmvfc_reset_host(vhost);
  1522. }
  1523. /**
  1524. * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
  1525. * @evt: event to be sent
  1526. * @vhost: ibmvfc host struct
  1527. * @timeout: timeout in seconds - 0 means do not time command
  1528. *
  1529. * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
  1530. **/
  1531. static int ibmvfc_send_event(struct ibmvfc_event *evt,
  1532. struct ibmvfc_host *vhost, unsigned long timeout)
  1533. {
  1534. __be64 *crq_as_u64 = (__be64 *) &evt->crq;
  1535. unsigned long flags;
  1536. int rc;
  1537. /* Copy the IU into the transfer area */
  1538. *evt->xfer_iu = evt->iu;
  1539. if (evt->crq.format == IBMVFC_CMD_FORMAT)
  1540. evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
  1541. else if (evt->crq.format == IBMVFC_MAD_FORMAT)
  1542. evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
  1543. else
  1544. BUG();
  1545. timer_setup(&evt->timer, ibmvfc_timeout, 0);
  1546. if (timeout) {
  1547. evt->timer.expires = jiffies + (timeout * HZ);
  1548. add_timer(&evt->timer);
  1549. }
  1550. spin_lock_irqsave(&evt->queue->l_lock, flags);
  1551. list_add_tail(&evt->queue_list, &evt->queue->sent);
  1552. atomic_set(&evt->active, 1);
  1553. mb();
  1554. if (evt->queue->fmt == IBMVFC_SUB_CRQ_FMT)
  1555. rc = ibmvfc_send_sub_crq(vhost,
  1556. evt->queue->vios_cookie,
  1557. be64_to_cpu(crq_as_u64[0]),
  1558. be64_to_cpu(crq_as_u64[1]),
  1559. 0, 0);
  1560. else
  1561. rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
  1562. be64_to_cpu(crq_as_u64[1]));
  1563. if (rc) {
  1564. atomic_set(&evt->active, 0);
  1565. list_del(&evt->queue_list);
  1566. spin_unlock_irqrestore(&evt->queue->l_lock, flags);
  1567. del_timer(&evt->timer);
  1568. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  1569. * Firmware will send a CRQ with a transport event (0xFF) to
  1570. * tell this client what has happened to the transport. This
  1571. * will be handled in ibmvfc_handle_crq()
  1572. */
  1573. if (rc == H_CLOSED) {
  1574. if (printk_ratelimit())
  1575. dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
  1576. if (evt->cmnd)
  1577. scsi_dma_unmap(evt->cmnd);
  1578. ibmvfc_free_event(evt);
  1579. return SCSI_MLQUEUE_HOST_BUSY;
  1580. }
  1581. dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
  1582. if (evt->cmnd) {
  1583. evt->cmnd->result = DID_ERROR << 16;
  1584. evt->done = ibmvfc_scsi_eh_done;
  1585. } else
  1586. evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
  1587. evt->done(evt);
  1588. } else {
  1589. spin_unlock_irqrestore(&evt->queue->l_lock, flags);
  1590. ibmvfc_trc_start(evt);
  1591. }
  1592. return 0;
  1593. }
  1594. /**
  1595. * ibmvfc_log_error - Log an error for the failed command if appropriate
  1596. * @evt: ibmvfc event to log
  1597. *
  1598. **/
  1599. static void ibmvfc_log_error(struct ibmvfc_event *evt)
  1600. {
  1601. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  1602. struct ibmvfc_host *vhost = evt->vhost;
  1603. struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
  1604. struct scsi_cmnd *cmnd = evt->cmnd;
  1605. const char *err = unknown_error;
  1606. int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
  1607. int logerr = 0;
  1608. int rsp_code = 0;
  1609. if (index >= 0) {
  1610. logerr = cmd_status[index].log;
  1611. err = cmd_status[index].name;
  1612. }
  1613. if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
  1614. return;
  1615. if (rsp->flags & FCP_RSP_LEN_VALID)
  1616. rsp_code = rsp->data.info.rsp_code;
  1617. scmd_printk(KERN_ERR, cmnd, "Command (%02X) : %s (%x:%x) "
  1618. "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
  1619. cmnd->cmnd[0], err, be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error),
  1620. rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
  1621. }
  1622. /**
  1623. * ibmvfc_relogin - Log back into the specified device
  1624. * @sdev: scsi device struct
  1625. *
  1626. **/
  1627. static void ibmvfc_relogin(struct scsi_device *sdev)
  1628. {
  1629. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1630. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1631. struct ibmvfc_target *tgt;
  1632. unsigned long flags;
  1633. spin_lock_irqsave(vhost->host->host_lock, flags);
  1634. list_for_each_entry(tgt, &vhost->targets, queue) {
  1635. if (rport == tgt->rport) {
  1636. ibmvfc_del_tgt(tgt);
  1637. break;
  1638. }
  1639. }
  1640. ibmvfc_reinit_host(vhost);
  1641. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1642. }
  1643. /**
  1644. * ibmvfc_scsi_done - Handle responses from commands
  1645. * @evt: ibmvfc event to be handled
  1646. *
  1647. * Used as a callback when sending scsi cmds.
  1648. **/
  1649. static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
  1650. {
  1651. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  1652. struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(evt->vhost, vfc_cmd);
  1653. struct scsi_cmnd *cmnd = evt->cmnd;
  1654. u32 rsp_len = 0;
  1655. u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
  1656. if (cmnd) {
  1657. if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
  1658. scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
  1659. else if (rsp->flags & FCP_RESID_UNDER)
  1660. scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
  1661. else
  1662. scsi_set_resid(cmnd, 0);
  1663. if (vfc_cmd->status) {
  1664. cmnd->result = ibmvfc_get_err_result(evt->vhost, vfc_cmd);
  1665. if (rsp->flags & FCP_RSP_LEN_VALID)
  1666. rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
  1667. if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
  1668. sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
  1669. if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
  1670. memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
  1671. if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
  1672. (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
  1673. ibmvfc_relogin(cmnd->device);
  1674. if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
  1675. cmnd->result = (DID_ERROR << 16);
  1676. ibmvfc_log_error(evt);
  1677. }
  1678. if (!cmnd->result &&
  1679. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
  1680. cmnd->result = (DID_ERROR << 16);
  1681. scsi_dma_unmap(cmnd);
  1682. scsi_done(cmnd);
  1683. }
  1684. ibmvfc_free_event(evt);
  1685. }
  1686. /**
  1687. * ibmvfc_host_chkready - Check if the host can accept commands
  1688. * @vhost: struct ibmvfc host
  1689. *
  1690. * Returns:
  1691. * 1 if host can accept command / 0 if not
  1692. **/
  1693. static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
  1694. {
  1695. int result = 0;
  1696. switch (vhost->state) {
  1697. case IBMVFC_LINK_DEAD:
  1698. case IBMVFC_HOST_OFFLINE:
  1699. result = DID_NO_CONNECT << 16;
  1700. break;
  1701. case IBMVFC_NO_CRQ:
  1702. case IBMVFC_INITIALIZING:
  1703. case IBMVFC_HALTED:
  1704. case IBMVFC_LINK_DOWN:
  1705. result = DID_REQUEUE << 16;
  1706. break;
  1707. case IBMVFC_ACTIVE:
  1708. result = 0;
  1709. break;
  1710. }
  1711. return result;
  1712. }
  1713. static struct ibmvfc_cmd *ibmvfc_init_vfc_cmd(struct ibmvfc_event *evt, struct scsi_device *sdev)
  1714. {
  1715. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1716. struct ibmvfc_host *vhost = evt->vhost;
  1717. struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
  1718. struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
  1719. struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
  1720. size_t offset;
  1721. memset(vfc_cmd, 0, sizeof(*vfc_cmd));
  1722. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
  1723. offset = offsetof(struct ibmvfc_cmd, v2.rsp);
  1724. vfc_cmd->target_wwpn = cpu_to_be64(rport->port_name);
  1725. } else
  1726. offset = offsetof(struct ibmvfc_cmd, v1.rsp);
  1727. vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offset);
  1728. vfc_cmd->resp.len = cpu_to_be32(sizeof(*rsp));
  1729. vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
  1730. vfc_cmd->payload_len = cpu_to_be32(sizeof(*iu));
  1731. vfc_cmd->resp_len = cpu_to_be32(sizeof(*rsp));
  1732. vfc_cmd->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
  1733. vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
  1734. int_to_scsilun(sdev->lun, &iu->lun);
  1735. return vfc_cmd;
  1736. }
  1737. /**
  1738. * ibmvfc_queuecommand - The queuecommand function of the scsi template
  1739. * @shost: scsi host struct
  1740. * @cmnd: struct scsi_cmnd to be executed
  1741. *
  1742. * Returns:
  1743. * 0 on success / other on failure
  1744. **/
  1745. static int ibmvfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
  1746. {
  1747. struct ibmvfc_host *vhost = shost_priv(shost);
  1748. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  1749. struct ibmvfc_cmd *vfc_cmd;
  1750. struct ibmvfc_fcp_cmd_iu *iu;
  1751. struct ibmvfc_event *evt;
  1752. u32 tag_and_hwq = blk_mq_unique_tag(scsi_cmd_to_rq(cmnd));
  1753. u16 hwq = blk_mq_unique_tag_to_hwq(tag_and_hwq);
  1754. u16 scsi_channel;
  1755. int rc;
  1756. if (unlikely((rc = fc_remote_port_chkready(rport))) ||
  1757. unlikely((rc = ibmvfc_host_chkready(vhost)))) {
  1758. cmnd->result = rc;
  1759. scsi_done(cmnd);
  1760. return 0;
  1761. }
  1762. cmnd->result = (DID_OK << 16);
  1763. if (vhost->using_channels) {
  1764. scsi_channel = hwq % vhost->scsi_scrqs.active_queues;
  1765. evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[scsi_channel]);
  1766. if (!evt)
  1767. return SCSI_MLQUEUE_HOST_BUSY;
  1768. evt->hwq = hwq % vhost->scsi_scrqs.active_queues;
  1769. } else {
  1770. evt = ibmvfc_get_event(&vhost->crq);
  1771. if (!evt)
  1772. return SCSI_MLQUEUE_HOST_BUSY;
  1773. }
  1774. ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
  1775. evt->cmnd = cmnd;
  1776. vfc_cmd = ibmvfc_init_vfc_cmd(evt, cmnd->device);
  1777. iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
  1778. iu->xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
  1779. memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
  1780. if (cmnd->flags & SCMD_TAGGED) {
  1781. vfc_cmd->task_tag = cpu_to_be64(scsi_cmd_to_rq(cmnd)->tag);
  1782. iu->pri_task_attr = IBMVFC_SIMPLE_TASK;
  1783. }
  1784. vfc_cmd->correlation = cpu_to_be64((u64)evt);
  1785. if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
  1786. return ibmvfc_send_event(evt, vhost, 0);
  1787. ibmvfc_free_event(evt);
  1788. if (rc == -ENOMEM)
  1789. return SCSI_MLQUEUE_HOST_BUSY;
  1790. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1791. scmd_printk(KERN_ERR, cmnd,
  1792. "Failed to map DMA buffer for command. rc=%d\n", rc);
  1793. cmnd->result = DID_ERROR << 16;
  1794. scsi_done(cmnd);
  1795. return 0;
  1796. }
  1797. /**
  1798. * ibmvfc_sync_completion - Signal that a synchronous command has completed
  1799. * @evt: ibmvfc event struct
  1800. *
  1801. **/
  1802. static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
  1803. {
  1804. /* copy the response back */
  1805. if (evt->sync_iu)
  1806. *evt->sync_iu = *evt->xfer_iu;
  1807. complete(&evt->comp);
  1808. }
  1809. /**
  1810. * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
  1811. * @evt: struct ibmvfc_event
  1812. *
  1813. **/
  1814. static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
  1815. {
  1816. struct ibmvfc_host *vhost = evt->vhost;
  1817. ibmvfc_free_event(evt);
  1818. vhost->aborting_passthru = 0;
  1819. dev_info(vhost->dev, "Passthru command cancelled\n");
  1820. }
  1821. /**
  1822. * ibmvfc_bsg_timeout - Handle a BSG timeout
  1823. * @job: struct bsg_job that timed out
  1824. *
  1825. * Returns:
  1826. * 0 on success / other on failure
  1827. **/
  1828. static int ibmvfc_bsg_timeout(struct bsg_job *job)
  1829. {
  1830. struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
  1831. unsigned long port_id = (unsigned long)job->dd_data;
  1832. struct ibmvfc_event *evt;
  1833. struct ibmvfc_tmf *tmf;
  1834. unsigned long flags;
  1835. int rc;
  1836. ENTER;
  1837. spin_lock_irqsave(vhost->host->host_lock, flags);
  1838. if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
  1839. __ibmvfc_reset_host(vhost);
  1840. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1841. return 0;
  1842. }
  1843. vhost->aborting_passthru = 1;
  1844. evt = ibmvfc_get_reserved_event(&vhost->crq);
  1845. if (!evt) {
  1846. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1847. return -ENOMEM;
  1848. }
  1849. ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
  1850. tmf = &evt->iu.tmf;
  1851. memset(tmf, 0, sizeof(*tmf));
  1852. tmf->common.version = cpu_to_be32(1);
  1853. tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
  1854. tmf->common.length = cpu_to_be16(sizeof(*tmf));
  1855. tmf->scsi_id = cpu_to_be64(port_id);
  1856. tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
  1857. tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
  1858. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1859. if (rc != 0) {
  1860. vhost->aborting_passthru = 0;
  1861. dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
  1862. rc = -EIO;
  1863. } else
  1864. dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
  1865. port_id);
  1866. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1867. LEAVE;
  1868. return rc;
  1869. }
  1870. /**
  1871. * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
  1872. * @vhost: struct ibmvfc_host to send command
  1873. * @port_id: port ID to send command
  1874. *
  1875. * Returns:
  1876. * 0 on success / other on failure
  1877. **/
  1878. static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
  1879. {
  1880. struct ibmvfc_port_login *plogi;
  1881. struct ibmvfc_target *tgt;
  1882. struct ibmvfc_event *evt;
  1883. union ibmvfc_iu rsp_iu;
  1884. unsigned long flags;
  1885. int rc = 0, issue_login = 1;
  1886. ENTER;
  1887. spin_lock_irqsave(vhost->host->host_lock, flags);
  1888. list_for_each_entry(tgt, &vhost->targets, queue) {
  1889. if (tgt->scsi_id == port_id) {
  1890. issue_login = 0;
  1891. break;
  1892. }
  1893. }
  1894. if (!issue_login)
  1895. goto unlock_out;
  1896. if (unlikely((rc = ibmvfc_host_chkready(vhost))))
  1897. goto unlock_out;
  1898. evt = ibmvfc_get_reserved_event(&vhost->crq);
  1899. if (!evt) {
  1900. rc = -ENOMEM;
  1901. goto unlock_out;
  1902. }
  1903. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  1904. plogi = &evt->iu.plogi;
  1905. memset(plogi, 0, sizeof(*plogi));
  1906. plogi->common.version = cpu_to_be32(1);
  1907. plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
  1908. plogi->common.length = cpu_to_be16(sizeof(*plogi));
  1909. plogi->scsi_id = cpu_to_be64(port_id);
  1910. evt->sync_iu = &rsp_iu;
  1911. init_completion(&evt->comp);
  1912. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1913. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1914. if (rc)
  1915. return -EIO;
  1916. wait_for_completion(&evt->comp);
  1917. if (rsp_iu.plogi.common.status)
  1918. rc = -EIO;
  1919. spin_lock_irqsave(vhost->host->host_lock, flags);
  1920. ibmvfc_free_event(evt);
  1921. unlock_out:
  1922. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1923. LEAVE;
  1924. return rc;
  1925. }
  1926. /**
  1927. * ibmvfc_bsg_request - Handle a BSG request
  1928. * @job: struct bsg_job to be executed
  1929. *
  1930. * Returns:
  1931. * 0 on success / other on failure
  1932. **/
  1933. static int ibmvfc_bsg_request(struct bsg_job *job)
  1934. {
  1935. struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
  1936. struct fc_rport *rport = fc_bsg_to_rport(job);
  1937. struct ibmvfc_passthru_mad *mad;
  1938. struct ibmvfc_event *evt;
  1939. union ibmvfc_iu rsp_iu;
  1940. unsigned long flags, port_id = -1;
  1941. struct fc_bsg_request *bsg_request = job->request;
  1942. struct fc_bsg_reply *bsg_reply = job->reply;
  1943. unsigned int code = bsg_request->msgcode;
  1944. int rc = 0, req_seg, rsp_seg, issue_login = 0;
  1945. u32 fc_flags, rsp_len;
  1946. ENTER;
  1947. bsg_reply->reply_payload_rcv_len = 0;
  1948. if (rport)
  1949. port_id = rport->port_id;
  1950. switch (code) {
  1951. case FC_BSG_HST_ELS_NOLOGIN:
  1952. port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
  1953. (bsg_request->rqst_data.h_els.port_id[1] << 8) |
  1954. bsg_request->rqst_data.h_els.port_id[2];
  1955. fallthrough;
  1956. case FC_BSG_RPT_ELS:
  1957. fc_flags = IBMVFC_FC_ELS;
  1958. break;
  1959. case FC_BSG_HST_CT:
  1960. issue_login = 1;
  1961. port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
  1962. (bsg_request->rqst_data.h_ct.port_id[1] << 8) |
  1963. bsg_request->rqst_data.h_ct.port_id[2];
  1964. fallthrough;
  1965. case FC_BSG_RPT_CT:
  1966. fc_flags = IBMVFC_FC_CT_IU;
  1967. break;
  1968. default:
  1969. return -ENOTSUPP;
  1970. }
  1971. if (port_id == -1)
  1972. return -EINVAL;
  1973. if (!mutex_trylock(&vhost->passthru_mutex))
  1974. return -EBUSY;
  1975. job->dd_data = (void *)port_id;
  1976. req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
  1977. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1978. if (!req_seg) {
  1979. mutex_unlock(&vhost->passthru_mutex);
  1980. return -ENOMEM;
  1981. }
  1982. rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
  1983. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1984. if (!rsp_seg) {
  1985. dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
  1986. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1987. mutex_unlock(&vhost->passthru_mutex);
  1988. return -ENOMEM;
  1989. }
  1990. if (req_seg > 1 || rsp_seg > 1) {
  1991. rc = -EINVAL;
  1992. goto out;
  1993. }
  1994. if (issue_login)
  1995. rc = ibmvfc_bsg_plogi(vhost, port_id);
  1996. spin_lock_irqsave(vhost->host->host_lock, flags);
  1997. if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
  1998. unlikely((rc = ibmvfc_host_chkready(vhost)))) {
  1999. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2000. goto out;
  2001. }
  2002. evt = ibmvfc_get_reserved_event(&vhost->crq);
  2003. if (!evt) {
  2004. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2005. rc = -ENOMEM;
  2006. goto out;
  2007. }
  2008. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  2009. mad = &evt->iu.passthru;
  2010. memset(mad, 0, sizeof(*mad));
  2011. mad->common.version = cpu_to_be32(1);
  2012. mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
  2013. mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
  2014. mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
  2015. offsetof(struct ibmvfc_passthru_mad, iu));
  2016. mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
  2017. mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
  2018. mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
  2019. mad->iu.flags = cpu_to_be32(fc_flags);
  2020. mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
  2021. mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
  2022. mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
  2023. mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
  2024. mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
  2025. mad->iu.scsi_id = cpu_to_be64(port_id);
  2026. mad->iu.tag = cpu_to_be64((u64)evt);
  2027. rsp_len = be32_to_cpu(mad->iu.rsp.len);
  2028. evt->sync_iu = &rsp_iu;
  2029. init_completion(&evt->comp);
  2030. rc = ibmvfc_send_event(evt, vhost, 0);
  2031. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2032. if (rc) {
  2033. rc = -EIO;
  2034. goto out;
  2035. }
  2036. wait_for_completion(&evt->comp);
  2037. if (rsp_iu.passthru.common.status)
  2038. rc = -EIO;
  2039. else
  2040. bsg_reply->reply_payload_rcv_len = rsp_len;
  2041. spin_lock_irqsave(vhost->host->host_lock, flags);
  2042. ibmvfc_free_event(evt);
  2043. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2044. bsg_reply->result = rc;
  2045. bsg_job_done(job, bsg_reply->result,
  2046. bsg_reply->reply_payload_rcv_len);
  2047. rc = 0;
  2048. out:
  2049. dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
  2050. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  2051. dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
  2052. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  2053. mutex_unlock(&vhost->passthru_mutex);
  2054. LEAVE;
  2055. return rc;
  2056. }
  2057. /**
  2058. * ibmvfc_reset_device - Reset the device with the specified reset type
  2059. * @sdev: scsi device to reset
  2060. * @type: reset type
  2061. * @desc: reset type description for log messages
  2062. *
  2063. * Returns:
  2064. * 0 on success / other on failure
  2065. **/
  2066. static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
  2067. {
  2068. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2069. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  2070. struct ibmvfc_cmd *tmf;
  2071. struct ibmvfc_event *evt = NULL;
  2072. union ibmvfc_iu rsp_iu;
  2073. struct ibmvfc_fcp_cmd_iu *iu;
  2074. struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
  2075. int rsp_rc = -EBUSY;
  2076. unsigned long flags;
  2077. int rsp_code = 0;
  2078. spin_lock_irqsave(vhost->host->host_lock, flags);
  2079. if (vhost->state == IBMVFC_ACTIVE) {
  2080. if (vhost->using_channels)
  2081. evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[0]);
  2082. else
  2083. evt = ibmvfc_get_event(&vhost->crq);
  2084. if (!evt) {
  2085. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2086. return -ENOMEM;
  2087. }
  2088. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
  2089. tmf = ibmvfc_init_vfc_cmd(evt, sdev);
  2090. iu = ibmvfc_get_fcp_iu(vhost, tmf);
  2091. tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
  2092. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
  2093. tmf->target_wwpn = cpu_to_be64(rport->port_name);
  2094. iu->tmf_flags = type;
  2095. evt->sync_iu = &rsp_iu;
  2096. init_completion(&evt->comp);
  2097. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  2098. }
  2099. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2100. if (rsp_rc != 0) {
  2101. sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
  2102. desc, rsp_rc);
  2103. return -EIO;
  2104. }
  2105. sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
  2106. wait_for_completion(&evt->comp);
  2107. if (rsp_iu.cmd.status)
  2108. rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
  2109. if (rsp_code) {
  2110. if (fc_rsp->flags & FCP_RSP_LEN_VALID)
  2111. rsp_code = fc_rsp->data.info.rsp_code;
  2112. sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
  2113. "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
  2114. ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
  2115. be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
  2116. fc_rsp->scsi_status);
  2117. rsp_rc = -EIO;
  2118. } else
  2119. sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
  2120. spin_lock_irqsave(vhost->host->host_lock, flags);
  2121. ibmvfc_free_event(evt);
  2122. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2123. return rsp_rc;
  2124. }
  2125. /**
  2126. * ibmvfc_match_rport - Match function for specified remote port
  2127. * @evt: ibmvfc event struct
  2128. * @rport: device to match
  2129. *
  2130. * Returns:
  2131. * 1 if event matches rport / 0 if event does not match rport
  2132. **/
  2133. static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
  2134. {
  2135. struct fc_rport *cmd_rport;
  2136. if (evt->cmnd) {
  2137. cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
  2138. if (cmd_rport == rport)
  2139. return 1;
  2140. }
  2141. return 0;
  2142. }
  2143. /**
  2144. * ibmvfc_match_target - Match function for specified target
  2145. * @evt: ibmvfc event struct
  2146. * @device: device to match (starget)
  2147. *
  2148. * Returns:
  2149. * 1 if event matches starget / 0 if event does not match starget
  2150. **/
  2151. static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
  2152. {
  2153. if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
  2154. return 1;
  2155. return 0;
  2156. }
  2157. /**
  2158. * ibmvfc_match_lun - Match function for specified LUN
  2159. * @evt: ibmvfc event struct
  2160. * @device: device to match (sdev)
  2161. *
  2162. * Returns:
  2163. * 1 if event matches sdev / 0 if event does not match sdev
  2164. **/
  2165. static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
  2166. {
  2167. if (evt->cmnd && evt->cmnd->device == device)
  2168. return 1;
  2169. return 0;
  2170. }
  2171. /**
  2172. * ibmvfc_event_is_free - Check if event is free or not
  2173. * @evt: ibmvfc event struct
  2174. *
  2175. * Returns:
  2176. * true / false
  2177. **/
  2178. static bool ibmvfc_event_is_free(struct ibmvfc_event *evt)
  2179. {
  2180. struct ibmvfc_event *loop_evt;
  2181. list_for_each_entry(loop_evt, &evt->queue->free, queue_list)
  2182. if (loop_evt == evt)
  2183. return true;
  2184. return false;
  2185. }
  2186. /**
  2187. * ibmvfc_wait_for_ops - Wait for ops to complete
  2188. * @vhost: ibmvfc host struct
  2189. * @device: device to match (starget or sdev)
  2190. * @match: match function
  2191. *
  2192. * Returns:
  2193. * SUCCESS / FAILED
  2194. **/
  2195. static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
  2196. int (*match) (struct ibmvfc_event *, void *))
  2197. {
  2198. struct ibmvfc_event *evt;
  2199. DECLARE_COMPLETION_ONSTACK(comp);
  2200. int wait, i, q_index, q_size;
  2201. unsigned long flags;
  2202. signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
  2203. struct ibmvfc_queue *queues;
  2204. ENTER;
  2205. if (vhost->mq_enabled && vhost->using_channels) {
  2206. queues = vhost->scsi_scrqs.scrqs;
  2207. q_size = vhost->scsi_scrqs.active_queues;
  2208. } else {
  2209. queues = &vhost->crq;
  2210. q_size = 1;
  2211. }
  2212. do {
  2213. wait = 0;
  2214. spin_lock_irqsave(vhost->host->host_lock, flags);
  2215. for (q_index = 0; q_index < q_size; q_index++) {
  2216. spin_lock(&queues[q_index].l_lock);
  2217. for (i = 0; i < queues[q_index].evt_pool.size; i++) {
  2218. evt = &queues[q_index].evt_pool.events[i];
  2219. if (!ibmvfc_event_is_free(evt)) {
  2220. if (match(evt, device)) {
  2221. evt->eh_comp = &comp;
  2222. wait++;
  2223. }
  2224. }
  2225. }
  2226. spin_unlock(&queues[q_index].l_lock);
  2227. }
  2228. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2229. if (wait) {
  2230. timeout = wait_for_completion_timeout(&comp, timeout);
  2231. if (!timeout) {
  2232. wait = 0;
  2233. spin_lock_irqsave(vhost->host->host_lock, flags);
  2234. for (q_index = 0; q_index < q_size; q_index++) {
  2235. spin_lock(&queues[q_index].l_lock);
  2236. for (i = 0; i < queues[q_index].evt_pool.size; i++) {
  2237. evt = &queues[q_index].evt_pool.events[i];
  2238. if (!ibmvfc_event_is_free(evt)) {
  2239. if (match(evt, device)) {
  2240. evt->eh_comp = NULL;
  2241. wait++;
  2242. }
  2243. }
  2244. }
  2245. spin_unlock(&queues[q_index].l_lock);
  2246. }
  2247. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2248. if (wait)
  2249. dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
  2250. LEAVE;
  2251. return wait ? FAILED : SUCCESS;
  2252. }
  2253. }
  2254. } while (wait);
  2255. LEAVE;
  2256. return SUCCESS;
  2257. }
  2258. static struct ibmvfc_event *ibmvfc_init_tmf(struct ibmvfc_queue *queue,
  2259. struct scsi_device *sdev,
  2260. int type)
  2261. {
  2262. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2263. struct scsi_target *starget = scsi_target(sdev);
  2264. struct fc_rport *rport = starget_to_rport(starget);
  2265. struct ibmvfc_event *evt;
  2266. struct ibmvfc_tmf *tmf;
  2267. evt = ibmvfc_get_reserved_event(queue);
  2268. if (!evt)
  2269. return NULL;
  2270. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  2271. tmf = &evt->iu.tmf;
  2272. memset(tmf, 0, sizeof(*tmf));
  2273. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
  2274. tmf->common.version = cpu_to_be32(2);
  2275. tmf->target_wwpn = cpu_to_be64(rport->port_name);
  2276. } else {
  2277. tmf->common.version = cpu_to_be32(1);
  2278. }
  2279. tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
  2280. tmf->common.length = cpu_to_be16(sizeof(*tmf));
  2281. tmf->scsi_id = cpu_to_be64(rport->port_id);
  2282. int_to_scsilun(sdev->lun, &tmf->lun);
  2283. if (!ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPRESS_ABTS))
  2284. type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
  2285. if (vhost->state == IBMVFC_ACTIVE)
  2286. tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
  2287. else
  2288. tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
  2289. tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
  2290. tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
  2291. init_completion(&evt->comp);
  2292. return evt;
  2293. }
  2294. static int ibmvfc_cancel_all_mq(struct scsi_device *sdev, int type)
  2295. {
  2296. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2297. struct ibmvfc_event *evt, *found_evt, *temp;
  2298. struct ibmvfc_queue *queues = vhost->scsi_scrqs.scrqs;
  2299. unsigned long flags;
  2300. int num_hwq, i;
  2301. int fail = 0;
  2302. LIST_HEAD(cancelq);
  2303. u16 status;
  2304. ENTER;
  2305. spin_lock_irqsave(vhost->host->host_lock, flags);
  2306. num_hwq = vhost->scsi_scrqs.active_queues;
  2307. for (i = 0; i < num_hwq; i++) {
  2308. spin_lock(queues[i].q_lock);
  2309. spin_lock(&queues[i].l_lock);
  2310. found_evt = NULL;
  2311. list_for_each_entry(evt, &queues[i].sent, queue_list) {
  2312. if (evt->cmnd && evt->cmnd->device == sdev) {
  2313. found_evt = evt;
  2314. break;
  2315. }
  2316. }
  2317. spin_unlock(&queues[i].l_lock);
  2318. if (found_evt && vhost->logged_in) {
  2319. evt = ibmvfc_init_tmf(&queues[i], sdev, type);
  2320. if (!evt) {
  2321. spin_unlock(queues[i].q_lock);
  2322. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2323. return -ENOMEM;
  2324. }
  2325. evt->sync_iu = &queues[i].cancel_rsp;
  2326. ibmvfc_send_event(evt, vhost, default_timeout);
  2327. list_add_tail(&evt->cancel, &cancelq);
  2328. }
  2329. spin_unlock(queues[i].q_lock);
  2330. }
  2331. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2332. if (list_empty(&cancelq)) {
  2333. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  2334. sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
  2335. return 0;
  2336. }
  2337. sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
  2338. list_for_each_entry_safe(evt, temp, &cancelq, cancel) {
  2339. wait_for_completion(&evt->comp);
  2340. status = be16_to_cpu(evt->queue->cancel_rsp.mad_common.status);
  2341. list_del(&evt->cancel);
  2342. ibmvfc_free_event(evt);
  2343. if (status != IBMVFC_MAD_SUCCESS) {
  2344. sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
  2345. switch (status) {
  2346. case IBMVFC_MAD_DRIVER_FAILED:
  2347. case IBMVFC_MAD_CRQ_ERROR:
  2348. /* Host adapter most likely going through reset, return success to
  2349. * the caller will wait for the command being cancelled to get returned
  2350. */
  2351. break;
  2352. default:
  2353. fail = 1;
  2354. break;
  2355. }
  2356. }
  2357. }
  2358. if (fail)
  2359. return -EIO;
  2360. sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
  2361. LEAVE;
  2362. return 0;
  2363. }
  2364. static int ibmvfc_cancel_all_sq(struct scsi_device *sdev, int type)
  2365. {
  2366. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2367. struct ibmvfc_event *evt, *found_evt;
  2368. union ibmvfc_iu rsp;
  2369. int rsp_rc = -EBUSY;
  2370. unsigned long flags;
  2371. u16 status;
  2372. ENTER;
  2373. found_evt = NULL;
  2374. spin_lock_irqsave(vhost->host->host_lock, flags);
  2375. spin_lock(&vhost->crq.l_lock);
  2376. list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
  2377. if (evt->cmnd && evt->cmnd->device == sdev) {
  2378. found_evt = evt;
  2379. break;
  2380. }
  2381. }
  2382. spin_unlock(&vhost->crq.l_lock);
  2383. if (!found_evt) {
  2384. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  2385. sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
  2386. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2387. return 0;
  2388. }
  2389. if (vhost->logged_in) {
  2390. evt = ibmvfc_init_tmf(&vhost->crq, sdev, type);
  2391. evt->sync_iu = &rsp;
  2392. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  2393. }
  2394. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2395. if (rsp_rc != 0) {
  2396. sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
  2397. /* If failure is received, the host adapter is most likely going
  2398. through reset, return success so the caller will wait for the command
  2399. being cancelled to get returned */
  2400. return 0;
  2401. }
  2402. sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
  2403. wait_for_completion(&evt->comp);
  2404. status = be16_to_cpu(rsp.mad_common.status);
  2405. spin_lock_irqsave(vhost->host->host_lock, flags);
  2406. ibmvfc_free_event(evt);
  2407. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2408. if (status != IBMVFC_MAD_SUCCESS) {
  2409. sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
  2410. switch (status) {
  2411. case IBMVFC_MAD_DRIVER_FAILED:
  2412. case IBMVFC_MAD_CRQ_ERROR:
  2413. /* Host adapter most likely going through reset, return success to
  2414. the caller will wait for the command being cancelled to get returned */
  2415. return 0;
  2416. default:
  2417. return -EIO;
  2418. };
  2419. }
  2420. sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
  2421. return 0;
  2422. }
  2423. /**
  2424. * ibmvfc_cancel_all - Cancel all outstanding commands to the device
  2425. * @sdev: scsi device to cancel commands
  2426. * @type: type of error recovery being performed
  2427. *
  2428. * This sends a cancel to the VIOS for the specified device. This does
  2429. * NOT send any abort to the actual device. That must be done separately.
  2430. *
  2431. * Returns:
  2432. * 0 on success / other on failure
  2433. **/
  2434. static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
  2435. {
  2436. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2437. if (vhost->mq_enabled && vhost->using_channels)
  2438. return ibmvfc_cancel_all_mq(sdev, type);
  2439. else
  2440. return ibmvfc_cancel_all_sq(sdev, type);
  2441. }
  2442. /**
  2443. * ibmvfc_match_key - Match function for specified cancel key
  2444. * @evt: ibmvfc event struct
  2445. * @key: cancel key to match
  2446. *
  2447. * Returns:
  2448. * 1 if event matches key / 0 if event does not match key
  2449. **/
  2450. static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
  2451. {
  2452. unsigned long cancel_key = (unsigned long)key;
  2453. if (evt->crq.format == IBMVFC_CMD_FORMAT &&
  2454. be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
  2455. return 1;
  2456. return 0;
  2457. }
  2458. /**
  2459. * ibmvfc_match_evt - Match function for specified event
  2460. * @evt: ibmvfc event struct
  2461. * @match: event to match
  2462. *
  2463. * Returns:
  2464. * 1 if event matches key / 0 if event does not match key
  2465. **/
  2466. static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
  2467. {
  2468. if (evt == match)
  2469. return 1;
  2470. return 0;
  2471. }
  2472. /**
  2473. * ibmvfc_abort_task_set - Abort outstanding commands to the device
  2474. * @sdev: scsi device to abort commands
  2475. *
  2476. * This sends an Abort Task Set to the VIOS for the specified device. This does
  2477. * NOT send any cancel to the VIOS. That must be done separately.
  2478. *
  2479. * Returns:
  2480. * 0 on success / other on failure
  2481. **/
  2482. static int ibmvfc_abort_task_set(struct scsi_device *sdev)
  2483. {
  2484. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2485. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  2486. struct ibmvfc_cmd *tmf;
  2487. struct ibmvfc_event *evt, *found_evt;
  2488. union ibmvfc_iu rsp_iu;
  2489. struct ibmvfc_fcp_cmd_iu *iu;
  2490. struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
  2491. int rc, rsp_rc = -EBUSY;
  2492. unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
  2493. int rsp_code = 0;
  2494. found_evt = NULL;
  2495. spin_lock_irqsave(vhost->host->host_lock, flags);
  2496. spin_lock(&vhost->crq.l_lock);
  2497. list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
  2498. if (evt->cmnd && evt->cmnd->device == sdev) {
  2499. found_evt = evt;
  2500. break;
  2501. }
  2502. }
  2503. spin_unlock(&vhost->crq.l_lock);
  2504. if (!found_evt) {
  2505. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  2506. sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
  2507. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2508. return 0;
  2509. }
  2510. if (vhost->state == IBMVFC_ACTIVE) {
  2511. evt = ibmvfc_get_event(&vhost->crq);
  2512. if (!evt) {
  2513. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2514. return -ENOMEM;
  2515. }
  2516. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
  2517. tmf = ibmvfc_init_vfc_cmd(evt, sdev);
  2518. iu = ibmvfc_get_fcp_iu(vhost, tmf);
  2519. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
  2520. tmf->target_wwpn = cpu_to_be64(rport->port_name);
  2521. iu->tmf_flags = IBMVFC_ABORT_TASK_SET;
  2522. tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
  2523. evt->sync_iu = &rsp_iu;
  2524. tmf->correlation = cpu_to_be64((u64)evt);
  2525. init_completion(&evt->comp);
  2526. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  2527. }
  2528. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2529. if (rsp_rc != 0) {
  2530. sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
  2531. return -EIO;
  2532. }
  2533. sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
  2534. timeout = wait_for_completion_timeout(&evt->comp, timeout);
  2535. if (!timeout) {
  2536. rc = ibmvfc_cancel_all(sdev, 0);
  2537. if (!rc) {
  2538. rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
  2539. if (rc == SUCCESS)
  2540. rc = 0;
  2541. }
  2542. if (rc) {
  2543. sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
  2544. ibmvfc_reset_host(vhost);
  2545. rsp_rc = -EIO;
  2546. rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
  2547. if (rc == SUCCESS)
  2548. rsp_rc = 0;
  2549. rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
  2550. if (rc != SUCCESS) {
  2551. spin_lock_irqsave(vhost->host->host_lock, flags);
  2552. ibmvfc_hard_reset_host(vhost);
  2553. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2554. rsp_rc = 0;
  2555. }
  2556. goto out;
  2557. }
  2558. }
  2559. if (rsp_iu.cmd.status)
  2560. rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
  2561. if (rsp_code) {
  2562. if (fc_rsp->flags & FCP_RSP_LEN_VALID)
  2563. rsp_code = fc_rsp->data.info.rsp_code;
  2564. sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
  2565. "flags: %x fcp_rsp: %x, scsi_status: %x\n",
  2566. ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
  2567. be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
  2568. fc_rsp->scsi_status);
  2569. rsp_rc = -EIO;
  2570. } else
  2571. sdev_printk(KERN_INFO, sdev, "Abort successful\n");
  2572. out:
  2573. spin_lock_irqsave(vhost->host->host_lock, flags);
  2574. ibmvfc_free_event(evt);
  2575. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2576. return rsp_rc;
  2577. }
  2578. /**
  2579. * ibmvfc_eh_abort_handler - Abort a command
  2580. * @cmd: scsi command to abort
  2581. *
  2582. * Returns:
  2583. * SUCCESS / FAST_IO_FAIL / FAILED
  2584. **/
  2585. static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
  2586. {
  2587. struct scsi_device *sdev = cmd->device;
  2588. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2589. int cancel_rc, block_rc;
  2590. int rc = FAILED;
  2591. ENTER;
  2592. block_rc = fc_block_scsi_eh(cmd);
  2593. ibmvfc_wait_while_resetting(vhost);
  2594. if (block_rc != FAST_IO_FAIL) {
  2595. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
  2596. ibmvfc_abort_task_set(sdev);
  2597. } else
  2598. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2599. if (!cancel_rc)
  2600. rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
  2601. if (block_rc == FAST_IO_FAIL && rc != FAILED)
  2602. rc = FAST_IO_FAIL;
  2603. LEAVE;
  2604. return rc;
  2605. }
  2606. /**
  2607. * ibmvfc_eh_device_reset_handler - Reset a single LUN
  2608. * @cmd: scsi command struct
  2609. *
  2610. * Returns:
  2611. * SUCCESS / FAST_IO_FAIL / FAILED
  2612. **/
  2613. static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
  2614. {
  2615. struct scsi_device *sdev = cmd->device;
  2616. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2617. int cancel_rc, block_rc, reset_rc = 0;
  2618. int rc = FAILED;
  2619. ENTER;
  2620. block_rc = fc_block_scsi_eh(cmd);
  2621. ibmvfc_wait_while_resetting(vhost);
  2622. if (block_rc != FAST_IO_FAIL) {
  2623. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
  2624. reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
  2625. } else
  2626. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2627. if (!cancel_rc && !reset_rc)
  2628. rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
  2629. if (block_rc == FAST_IO_FAIL && rc != FAILED)
  2630. rc = FAST_IO_FAIL;
  2631. LEAVE;
  2632. return rc;
  2633. }
  2634. /**
  2635. * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
  2636. * @sdev: scsi device struct
  2637. * @data: return code
  2638. *
  2639. **/
  2640. static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
  2641. {
  2642. unsigned long *rc = data;
  2643. *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2644. }
  2645. /**
  2646. * ibmvfc_eh_target_reset_handler - Reset the target
  2647. * @cmd: scsi command struct
  2648. *
  2649. * Returns:
  2650. * SUCCESS / FAST_IO_FAIL / FAILED
  2651. **/
  2652. static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
  2653. {
  2654. struct scsi_target *starget = scsi_target(cmd->device);
  2655. struct fc_rport *rport = starget_to_rport(starget);
  2656. struct Scsi_Host *shost = rport_to_shost(rport);
  2657. struct ibmvfc_host *vhost = shost_priv(shost);
  2658. int block_rc;
  2659. int reset_rc = 0;
  2660. int rc = FAILED;
  2661. unsigned long cancel_rc = 0;
  2662. bool tgt_reset = false;
  2663. ENTER;
  2664. block_rc = fc_block_rport(rport);
  2665. ibmvfc_wait_while_resetting(vhost);
  2666. if (block_rc != FAST_IO_FAIL) {
  2667. struct scsi_device *sdev;
  2668. shost_for_each_device(sdev, shost) {
  2669. if ((sdev->channel != starget->channel) ||
  2670. (sdev->id != starget->id))
  2671. continue;
  2672. cancel_rc |= ibmvfc_cancel_all(sdev,
  2673. IBMVFC_TMF_TGT_RESET);
  2674. if (!tgt_reset) {
  2675. reset_rc = ibmvfc_reset_device(sdev,
  2676. IBMVFC_TARGET_RESET, "target");
  2677. tgt_reset = true;
  2678. }
  2679. }
  2680. } else
  2681. starget_for_each_device(starget, &cancel_rc,
  2682. ibmvfc_dev_cancel_all_noreset);
  2683. if (!cancel_rc && !reset_rc)
  2684. rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
  2685. if (block_rc == FAST_IO_FAIL && rc != FAILED)
  2686. rc = FAST_IO_FAIL;
  2687. LEAVE;
  2688. return rc;
  2689. }
  2690. /**
  2691. * ibmvfc_eh_host_reset_handler - Reset the connection to the server
  2692. * @cmd: struct scsi_cmnd having problems
  2693. *
  2694. **/
  2695. static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
  2696. {
  2697. int rc;
  2698. struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
  2699. dev_err(vhost->dev, "Resetting connection due to error recovery\n");
  2700. rc = ibmvfc_issue_fc_host_lip(vhost->host);
  2701. return rc ? FAILED : SUCCESS;
  2702. }
  2703. /**
  2704. * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
  2705. * @rport: rport struct
  2706. *
  2707. * Return value:
  2708. * none
  2709. **/
  2710. static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
  2711. {
  2712. struct Scsi_Host *shost = rport_to_shost(rport);
  2713. struct ibmvfc_host *vhost = shost_priv(shost);
  2714. struct fc_rport *dev_rport;
  2715. struct scsi_device *sdev;
  2716. struct ibmvfc_target *tgt;
  2717. unsigned long rc, flags;
  2718. unsigned int found;
  2719. ENTER;
  2720. shost_for_each_device(sdev, shost) {
  2721. dev_rport = starget_to_rport(scsi_target(sdev));
  2722. if (dev_rport != rport)
  2723. continue;
  2724. ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2725. }
  2726. rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
  2727. if (rc == FAILED)
  2728. ibmvfc_issue_fc_host_lip(shost);
  2729. spin_lock_irqsave(shost->host_lock, flags);
  2730. found = 0;
  2731. list_for_each_entry(tgt, &vhost->targets, queue) {
  2732. if (tgt->scsi_id == rport->port_id) {
  2733. found++;
  2734. break;
  2735. }
  2736. }
  2737. if (found && tgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
  2738. /*
  2739. * If we get here, that means we previously attempted to send
  2740. * an implicit logout to the target but it failed, most likely
  2741. * due to I/O being pending, so we need to send it again
  2742. */
  2743. ibmvfc_del_tgt(tgt);
  2744. ibmvfc_reinit_host(vhost);
  2745. }
  2746. spin_unlock_irqrestore(shost->host_lock, flags);
  2747. LEAVE;
  2748. }
  2749. static const struct ibmvfc_async_desc ae_desc [] = {
  2750. { "PLOGI", IBMVFC_AE_ELS_PLOGI, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2751. { "LOGO", IBMVFC_AE_ELS_LOGO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2752. { "PRLO", IBMVFC_AE_ELS_PRLO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2753. { "N-Port SCN", IBMVFC_AE_SCN_NPORT, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2754. { "Group SCN", IBMVFC_AE_SCN_GROUP, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2755. { "Domain SCN", IBMVFC_AE_SCN_DOMAIN, IBMVFC_DEFAULT_LOG_LEVEL },
  2756. { "Fabric SCN", IBMVFC_AE_SCN_FABRIC, IBMVFC_DEFAULT_LOG_LEVEL },
  2757. { "Link Up", IBMVFC_AE_LINK_UP, IBMVFC_DEFAULT_LOG_LEVEL },
  2758. { "Link Down", IBMVFC_AE_LINK_DOWN, IBMVFC_DEFAULT_LOG_LEVEL },
  2759. { "Link Dead", IBMVFC_AE_LINK_DEAD, IBMVFC_DEFAULT_LOG_LEVEL },
  2760. { "Halt", IBMVFC_AE_HALT, IBMVFC_DEFAULT_LOG_LEVEL },
  2761. { "Resume", IBMVFC_AE_RESUME, IBMVFC_DEFAULT_LOG_LEVEL },
  2762. { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
  2763. };
  2764. static const struct ibmvfc_async_desc unknown_ae = {
  2765. "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
  2766. };
  2767. /**
  2768. * ibmvfc_get_ae_desc - Get text description for async event
  2769. * @ae: async event
  2770. *
  2771. **/
  2772. static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
  2773. {
  2774. int i;
  2775. for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
  2776. if (ae_desc[i].ae == ae)
  2777. return &ae_desc[i];
  2778. return &unknown_ae;
  2779. }
  2780. static const struct {
  2781. enum ibmvfc_ae_link_state state;
  2782. const char *desc;
  2783. } link_desc [] = {
  2784. { IBMVFC_AE_LS_LINK_UP, " link up" },
  2785. { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
  2786. { IBMVFC_AE_LS_LINK_DOWN, " link down" },
  2787. { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
  2788. };
  2789. /**
  2790. * ibmvfc_get_link_state - Get text description for link state
  2791. * @state: link state
  2792. *
  2793. **/
  2794. static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
  2795. {
  2796. int i;
  2797. for (i = 0; i < ARRAY_SIZE(link_desc); i++)
  2798. if (link_desc[i].state == state)
  2799. return link_desc[i].desc;
  2800. return "";
  2801. }
  2802. /**
  2803. * ibmvfc_handle_async - Handle an async event from the adapter
  2804. * @crq: crq to process
  2805. * @vhost: ibmvfc host struct
  2806. *
  2807. **/
  2808. static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
  2809. struct ibmvfc_host *vhost)
  2810. {
  2811. const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
  2812. struct ibmvfc_target *tgt;
  2813. ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
  2814. " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
  2815. be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
  2816. ibmvfc_get_link_state(crq->link_state));
  2817. switch (be64_to_cpu(crq->event)) {
  2818. case IBMVFC_AE_RESUME:
  2819. switch (crq->link_state) {
  2820. case IBMVFC_AE_LS_LINK_DOWN:
  2821. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2822. break;
  2823. case IBMVFC_AE_LS_LINK_DEAD:
  2824. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2825. break;
  2826. case IBMVFC_AE_LS_LINK_UP:
  2827. case IBMVFC_AE_LS_LINK_BOUNCED:
  2828. default:
  2829. vhost->events_to_log |= IBMVFC_AE_LINKUP;
  2830. vhost->delay_init = 1;
  2831. __ibmvfc_reset_host(vhost);
  2832. break;
  2833. }
  2834. break;
  2835. case IBMVFC_AE_LINK_UP:
  2836. vhost->events_to_log |= IBMVFC_AE_LINKUP;
  2837. vhost->delay_init = 1;
  2838. __ibmvfc_reset_host(vhost);
  2839. break;
  2840. case IBMVFC_AE_SCN_FABRIC:
  2841. case IBMVFC_AE_SCN_DOMAIN:
  2842. vhost->events_to_log |= IBMVFC_AE_RSCN;
  2843. if (vhost->state < IBMVFC_HALTED) {
  2844. vhost->delay_init = 1;
  2845. __ibmvfc_reset_host(vhost);
  2846. }
  2847. break;
  2848. case IBMVFC_AE_SCN_NPORT:
  2849. case IBMVFC_AE_SCN_GROUP:
  2850. vhost->events_to_log |= IBMVFC_AE_RSCN;
  2851. ibmvfc_reinit_host(vhost);
  2852. break;
  2853. case IBMVFC_AE_ELS_LOGO:
  2854. case IBMVFC_AE_ELS_PRLO:
  2855. case IBMVFC_AE_ELS_PLOGI:
  2856. list_for_each_entry(tgt, &vhost->targets, queue) {
  2857. if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
  2858. break;
  2859. if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
  2860. continue;
  2861. if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
  2862. continue;
  2863. if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
  2864. continue;
  2865. if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
  2866. tgt->logo_rcvd = 1;
  2867. if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
  2868. ibmvfc_del_tgt(tgt);
  2869. ibmvfc_reinit_host(vhost);
  2870. }
  2871. }
  2872. break;
  2873. case IBMVFC_AE_LINK_DOWN:
  2874. case IBMVFC_AE_ADAPTER_FAILED:
  2875. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2876. break;
  2877. case IBMVFC_AE_LINK_DEAD:
  2878. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2879. break;
  2880. case IBMVFC_AE_HALT:
  2881. ibmvfc_link_down(vhost, IBMVFC_HALTED);
  2882. break;
  2883. default:
  2884. dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
  2885. break;
  2886. }
  2887. }
  2888. /**
  2889. * ibmvfc_handle_crq - Handles and frees received events in the CRQ
  2890. * @crq: Command/Response queue
  2891. * @vhost: ibmvfc host struct
  2892. * @evt_doneq: Event done queue
  2893. *
  2894. **/
  2895. static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
  2896. struct list_head *evt_doneq)
  2897. {
  2898. long rc;
  2899. struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
  2900. switch (crq->valid) {
  2901. case IBMVFC_CRQ_INIT_RSP:
  2902. switch (crq->format) {
  2903. case IBMVFC_CRQ_INIT:
  2904. dev_info(vhost->dev, "Partner initialized\n");
  2905. /* Send back a response */
  2906. rc = ibmvfc_send_crq_init_complete(vhost);
  2907. if (rc == 0)
  2908. ibmvfc_init_host(vhost);
  2909. else
  2910. dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
  2911. break;
  2912. case IBMVFC_CRQ_INIT_COMPLETE:
  2913. dev_info(vhost->dev, "Partner initialization complete\n");
  2914. ibmvfc_init_host(vhost);
  2915. break;
  2916. default:
  2917. dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
  2918. }
  2919. return;
  2920. case IBMVFC_CRQ_XPORT_EVENT:
  2921. vhost->state = IBMVFC_NO_CRQ;
  2922. vhost->logged_in = 0;
  2923. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  2924. if (crq->format == IBMVFC_PARTITION_MIGRATED) {
  2925. /* We need to re-setup the interpartition connection */
  2926. dev_info(vhost->dev, "Partition migrated, Re-enabling adapter\n");
  2927. vhost->client_migrated = 1;
  2928. scsi_block_requests(vhost->host);
  2929. ibmvfc_purge_requests(vhost, DID_REQUEUE);
  2930. ibmvfc_set_host_state(vhost, IBMVFC_LINK_DOWN);
  2931. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
  2932. wake_up(&vhost->work_wait_q);
  2933. } else if (crq->format == IBMVFC_PARTNER_FAILED || crq->format == IBMVFC_PARTNER_DEREGISTER) {
  2934. dev_err(vhost->dev, "Host partner adapter deregistered or failed (rc=%d)\n", crq->format);
  2935. ibmvfc_purge_requests(vhost, DID_ERROR);
  2936. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2937. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
  2938. } else {
  2939. dev_err(vhost->dev, "Received unknown transport event from partner (rc=%d)\n", crq->format);
  2940. }
  2941. return;
  2942. case IBMVFC_CRQ_CMD_RSP:
  2943. break;
  2944. default:
  2945. dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
  2946. return;
  2947. }
  2948. if (crq->format == IBMVFC_ASYNC_EVENT)
  2949. return;
  2950. /* The only kind of payload CRQs we should get are responses to
  2951. * things we send. Make sure this response is to something we
  2952. * actually sent
  2953. */
  2954. if (unlikely(!ibmvfc_valid_event(&vhost->crq.evt_pool, evt))) {
  2955. dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
  2956. crq->ioba);
  2957. return;
  2958. }
  2959. if (unlikely(atomic_dec_if_positive(&evt->active))) {
  2960. dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
  2961. crq->ioba);
  2962. return;
  2963. }
  2964. spin_lock(&evt->queue->l_lock);
  2965. list_move_tail(&evt->queue_list, evt_doneq);
  2966. spin_unlock(&evt->queue->l_lock);
  2967. }
  2968. /**
  2969. * ibmvfc_scan_finished - Check if the device scan is done.
  2970. * @shost: scsi host struct
  2971. * @time: current elapsed time
  2972. *
  2973. * Returns:
  2974. * 0 if scan is not done / 1 if scan is done
  2975. **/
  2976. static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
  2977. {
  2978. unsigned long flags;
  2979. struct ibmvfc_host *vhost = shost_priv(shost);
  2980. int done = 0;
  2981. spin_lock_irqsave(shost->host_lock, flags);
  2982. if (!vhost->scan_timeout)
  2983. done = 1;
  2984. else if (time >= (vhost->scan_timeout * HZ)) {
  2985. dev_info(vhost->dev, "Scan taking longer than %d seconds, "
  2986. "continuing initialization\n", vhost->scan_timeout);
  2987. done = 1;
  2988. }
  2989. if (vhost->scan_complete) {
  2990. vhost->scan_timeout = init_timeout;
  2991. done = 1;
  2992. }
  2993. spin_unlock_irqrestore(shost->host_lock, flags);
  2994. return done;
  2995. }
  2996. /**
  2997. * ibmvfc_slave_alloc - Setup the device's task set value
  2998. * @sdev: struct scsi_device device to configure
  2999. *
  3000. * Set the device's task set value so that error handling works as
  3001. * expected.
  3002. *
  3003. * Returns:
  3004. * 0 on success / -ENXIO if device does not exist
  3005. **/
  3006. static int ibmvfc_slave_alloc(struct scsi_device *sdev)
  3007. {
  3008. struct Scsi_Host *shost = sdev->host;
  3009. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  3010. struct ibmvfc_host *vhost = shost_priv(shost);
  3011. unsigned long flags = 0;
  3012. if (!rport || fc_remote_port_chkready(rport))
  3013. return -ENXIO;
  3014. spin_lock_irqsave(shost->host_lock, flags);
  3015. sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
  3016. spin_unlock_irqrestore(shost->host_lock, flags);
  3017. return 0;
  3018. }
  3019. /**
  3020. * ibmvfc_target_alloc - Setup the target's task set value
  3021. * @starget: struct scsi_target
  3022. *
  3023. * Set the target's task set value so that error handling works as
  3024. * expected.
  3025. *
  3026. * Returns:
  3027. * 0 on success / -ENXIO if device does not exist
  3028. **/
  3029. static int ibmvfc_target_alloc(struct scsi_target *starget)
  3030. {
  3031. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  3032. struct ibmvfc_host *vhost = shost_priv(shost);
  3033. unsigned long flags = 0;
  3034. spin_lock_irqsave(shost->host_lock, flags);
  3035. starget->hostdata = (void *)(unsigned long)vhost->task_set++;
  3036. spin_unlock_irqrestore(shost->host_lock, flags);
  3037. return 0;
  3038. }
  3039. /**
  3040. * ibmvfc_slave_configure - Configure the device
  3041. * @sdev: struct scsi_device device to configure
  3042. *
  3043. * Enable allow_restart for a device if it is a disk. Adjust the
  3044. * queue_depth here also.
  3045. *
  3046. * Returns:
  3047. * 0
  3048. **/
  3049. static int ibmvfc_slave_configure(struct scsi_device *sdev)
  3050. {
  3051. struct Scsi_Host *shost = sdev->host;
  3052. unsigned long flags = 0;
  3053. spin_lock_irqsave(shost->host_lock, flags);
  3054. if (sdev->type == TYPE_DISK) {
  3055. sdev->allow_restart = 1;
  3056. blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
  3057. }
  3058. spin_unlock_irqrestore(shost->host_lock, flags);
  3059. return 0;
  3060. }
  3061. /**
  3062. * ibmvfc_change_queue_depth - Change the device's queue depth
  3063. * @sdev: scsi device struct
  3064. * @qdepth: depth to set
  3065. *
  3066. * Return value:
  3067. * actual depth set
  3068. **/
  3069. static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
  3070. {
  3071. if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
  3072. qdepth = IBMVFC_MAX_CMDS_PER_LUN;
  3073. return scsi_change_queue_depth(sdev, qdepth);
  3074. }
  3075. static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
  3076. struct device_attribute *attr, char *buf)
  3077. {
  3078. struct Scsi_Host *shost = class_to_shost(dev);
  3079. struct ibmvfc_host *vhost = shost_priv(shost);
  3080. return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.partition_name);
  3081. }
  3082. static ssize_t ibmvfc_show_host_device_name(struct device *dev,
  3083. struct device_attribute *attr, char *buf)
  3084. {
  3085. struct Scsi_Host *shost = class_to_shost(dev);
  3086. struct ibmvfc_host *vhost = shost_priv(shost);
  3087. return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.device_name);
  3088. }
  3089. static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
  3090. struct device_attribute *attr, char *buf)
  3091. {
  3092. struct Scsi_Host *shost = class_to_shost(dev);
  3093. struct ibmvfc_host *vhost = shost_priv(shost);
  3094. return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.port_loc_code);
  3095. }
  3096. static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
  3097. struct device_attribute *attr, char *buf)
  3098. {
  3099. struct Scsi_Host *shost = class_to_shost(dev);
  3100. struct ibmvfc_host *vhost = shost_priv(shost);
  3101. return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.drc_name);
  3102. }
  3103. static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
  3104. struct device_attribute *attr, char *buf)
  3105. {
  3106. struct Scsi_Host *shost = class_to_shost(dev);
  3107. struct ibmvfc_host *vhost = shost_priv(shost);
  3108. return sysfs_emit(buf, "%d\n",
  3109. be32_to_cpu(vhost->login_buf->resp.version));
  3110. }
  3111. static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
  3112. struct device_attribute *attr, char *buf)
  3113. {
  3114. struct Scsi_Host *shost = class_to_shost(dev);
  3115. struct ibmvfc_host *vhost = shost_priv(shost);
  3116. return sysfs_emit(buf, "%llx\n",
  3117. be64_to_cpu(vhost->login_buf->resp.capabilities));
  3118. }
  3119. /**
  3120. * ibmvfc_show_log_level - Show the adapter's error logging level
  3121. * @dev: class device struct
  3122. * @attr: unused
  3123. * @buf: buffer
  3124. *
  3125. * Return value:
  3126. * number of bytes printed to buffer
  3127. **/
  3128. static ssize_t ibmvfc_show_log_level(struct device *dev,
  3129. struct device_attribute *attr, char *buf)
  3130. {
  3131. struct Scsi_Host *shost = class_to_shost(dev);
  3132. struct ibmvfc_host *vhost = shost_priv(shost);
  3133. unsigned long flags = 0;
  3134. int len;
  3135. spin_lock_irqsave(shost->host_lock, flags);
  3136. len = sysfs_emit(buf, "%d\n", vhost->log_level);
  3137. spin_unlock_irqrestore(shost->host_lock, flags);
  3138. return len;
  3139. }
  3140. /**
  3141. * ibmvfc_store_log_level - Change the adapter's error logging level
  3142. * @dev: class device struct
  3143. * @attr: unused
  3144. * @buf: buffer
  3145. * @count: buffer size
  3146. *
  3147. * Return value:
  3148. * number of bytes printed to buffer
  3149. **/
  3150. static ssize_t ibmvfc_store_log_level(struct device *dev,
  3151. struct device_attribute *attr,
  3152. const char *buf, size_t count)
  3153. {
  3154. struct Scsi_Host *shost = class_to_shost(dev);
  3155. struct ibmvfc_host *vhost = shost_priv(shost);
  3156. unsigned long flags = 0;
  3157. spin_lock_irqsave(shost->host_lock, flags);
  3158. vhost->log_level = simple_strtoul(buf, NULL, 10);
  3159. spin_unlock_irqrestore(shost->host_lock, flags);
  3160. return strlen(buf);
  3161. }
  3162. static ssize_t ibmvfc_show_scsi_channels(struct device *dev,
  3163. struct device_attribute *attr, char *buf)
  3164. {
  3165. struct Scsi_Host *shost = class_to_shost(dev);
  3166. struct ibmvfc_host *vhost = shost_priv(shost);
  3167. struct ibmvfc_channels *scsi = &vhost->scsi_scrqs;
  3168. unsigned long flags = 0;
  3169. int len;
  3170. spin_lock_irqsave(shost->host_lock, flags);
  3171. len = sysfs_emit(buf, "%d\n", scsi->desired_queues);
  3172. spin_unlock_irqrestore(shost->host_lock, flags);
  3173. return len;
  3174. }
  3175. static ssize_t ibmvfc_store_scsi_channels(struct device *dev,
  3176. struct device_attribute *attr,
  3177. const char *buf, size_t count)
  3178. {
  3179. struct Scsi_Host *shost = class_to_shost(dev);
  3180. struct ibmvfc_host *vhost = shost_priv(shost);
  3181. struct ibmvfc_channels *scsi = &vhost->scsi_scrqs;
  3182. unsigned long flags = 0;
  3183. unsigned int channels;
  3184. spin_lock_irqsave(shost->host_lock, flags);
  3185. channels = simple_strtoul(buf, NULL, 10);
  3186. scsi->desired_queues = min(channels, shost->nr_hw_queues);
  3187. ibmvfc_hard_reset_host(vhost);
  3188. spin_unlock_irqrestore(shost->host_lock, flags);
  3189. return strlen(buf);
  3190. }
  3191. static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
  3192. static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
  3193. static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
  3194. static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
  3195. static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
  3196. static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
  3197. static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
  3198. ibmvfc_show_log_level, ibmvfc_store_log_level);
  3199. static DEVICE_ATTR(nr_scsi_channels, S_IRUGO | S_IWUSR,
  3200. ibmvfc_show_scsi_channels, ibmvfc_store_scsi_channels);
  3201. #ifdef CONFIG_SCSI_IBMVFC_TRACE
  3202. /**
  3203. * ibmvfc_read_trace - Dump the adapter trace
  3204. * @filp: open sysfs file
  3205. * @kobj: kobject struct
  3206. * @bin_attr: bin_attribute struct
  3207. * @buf: buffer
  3208. * @off: offset
  3209. * @count: buffer size
  3210. *
  3211. * Return value:
  3212. * number of bytes printed to buffer
  3213. **/
  3214. static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
  3215. struct bin_attribute *bin_attr,
  3216. char *buf, loff_t off, size_t count)
  3217. {
  3218. struct device *dev = kobj_to_dev(kobj);
  3219. struct Scsi_Host *shost = class_to_shost(dev);
  3220. struct ibmvfc_host *vhost = shost_priv(shost);
  3221. unsigned long flags = 0;
  3222. int size = IBMVFC_TRACE_SIZE;
  3223. char *src = (char *)vhost->trace;
  3224. if (off > size)
  3225. return 0;
  3226. if (off + count > size) {
  3227. size -= off;
  3228. count = size;
  3229. }
  3230. spin_lock_irqsave(shost->host_lock, flags);
  3231. memcpy(buf, &src[off], count);
  3232. spin_unlock_irqrestore(shost->host_lock, flags);
  3233. return count;
  3234. }
  3235. static struct bin_attribute ibmvfc_trace_attr = {
  3236. .attr = {
  3237. .name = "trace",
  3238. .mode = S_IRUGO,
  3239. },
  3240. .size = 0,
  3241. .read = ibmvfc_read_trace,
  3242. };
  3243. #endif
  3244. static struct attribute *ibmvfc_host_attrs[] = {
  3245. &dev_attr_partition_name.attr,
  3246. &dev_attr_device_name.attr,
  3247. &dev_attr_port_loc_code.attr,
  3248. &dev_attr_drc_name.attr,
  3249. &dev_attr_npiv_version.attr,
  3250. &dev_attr_capabilities.attr,
  3251. &dev_attr_log_level.attr,
  3252. &dev_attr_nr_scsi_channels.attr,
  3253. NULL
  3254. };
  3255. ATTRIBUTE_GROUPS(ibmvfc_host);
  3256. static const struct scsi_host_template driver_template = {
  3257. .module = THIS_MODULE,
  3258. .name = "IBM POWER Virtual FC Adapter",
  3259. .proc_name = IBMVFC_NAME,
  3260. .queuecommand = ibmvfc_queuecommand,
  3261. .eh_timed_out = fc_eh_timed_out,
  3262. .eh_abort_handler = ibmvfc_eh_abort_handler,
  3263. .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
  3264. .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
  3265. .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
  3266. .slave_alloc = ibmvfc_slave_alloc,
  3267. .slave_configure = ibmvfc_slave_configure,
  3268. .target_alloc = ibmvfc_target_alloc,
  3269. .scan_finished = ibmvfc_scan_finished,
  3270. .change_queue_depth = ibmvfc_change_queue_depth,
  3271. .cmd_per_lun = 16,
  3272. .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
  3273. .this_id = -1,
  3274. .sg_tablesize = SG_ALL,
  3275. .max_sectors = IBMVFC_MAX_SECTORS,
  3276. .shost_groups = ibmvfc_host_groups,
  3277. .track_queue_depth = 1,
  3278. };
  3279. /**
  3280. * ibmvfc_next_async_crq - Returns the next entry in async queue
  3281. * @vhost: ibmvfc host struct
  3282. *
  3283. * Returns:
  3284. * Pointer to next entry in queue / NULL if empty
  3285. **/
  3286. static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
  3287. {
  3288. struct ibmvfc_queue *async_crq = &vhost->async_crq;
  3289. struct ibmvfc_async_crq *crq;
  3290. crq = &async_crq->msgs.async[async_crq->cur];
  3291. if (crq->valid & 0x80) {
  3292. if (++async_crq->cur == async_crq->size)
  3293. async_crq->cur = 0;
  3294. rmb();
  3295. } else
  3296. crq = NULL;
  3297. return crq;
  3298. }
  3299. /**
  3300. * ibmvfc_next_crq - Returns the next entry in message queue
  3301. * @vhost: ibmvfc host struct
  3302. *
  3303. * Returns:
  3304. * Pointer to next entry in queue / NULL if empty
  3305. **/
  3306. static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
  3307. {
  3308. struct ibmvfc_queue *queue = &vhost->crq;
  3309. struct ibmvfc_crq *crq;
  3310. crq = &queue->msgs.crq[queue->cur];
  3311. if (crq->valid & 0x80) {
  3312. if (++queue->cur == queue->size)
  3313. queue->cur = 0;
  3314. rmb();
  3315. } else
  3316. crq = NULL;
  3317. return crq;
  3318. }
  3319. /**
  3320. * ibmvfc_interrupt - Interrupt handler
  3321. * @irq: number of irq to handle, not used
  3322. * @dev_instance: ibmvfc_host that received interrupt
  3323. *
  3324. * Returns:
  3325. * IRQ_HANDLED
  3326. **/
  3327. static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
  3328. {
  3329. struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
  3330. unsigned long flags;
  3331. spin_lock_irqsave(vhost->host->host_lock, flags);
  3332. vio_disable_interrupts(to_vio_dev(vhost->dev));
  3333. tasklet_schedule(&vhost->tasklet);
  3334. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3335. return IRQ_HANDLED;
  3336. }
  3337. /**
  3338. * ibmvfc_tasklet - Interrupt handler tasklet
  3339. * @data: ibmvfc host struct
  3340. *
  3341. * Returns:
  3342. * Nothing
  3343. **/
  3344. static void ibmvfc_tasklet(void *data)
  3345. {
  3346. struct ibmvfc_host *vhost = data;
  3347. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  3348. struct ibmvfc_crq *crq;
  3349. struct ibmvfc_async_crq *async;
  3350. struct ibmvfc_event *evt, *temp;
  3351. unsigned long flags;
  3352. int done = 0;
  3353. LIST_HEAD(evt_doneq);
  3354. spin_lock_irqsave(vhost->host->host_lock, flags);
  3355. spin_lock(vhost->crq.q_lock);
  3356. while (!done) {
  3357. /* Pull all the valid messages off the async CRQ */
  3358. while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
  3359. ibmvfc_handle_async(async, vhost);
  3360. async->valid = 0;
  3361. wmb();
  3362. }
  3363. /* Pull all the valid messages off the CRQ */
  3364. while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
  3365. ibmvfc_handle_crq(crq, vhost, &evt_doneq);
  3366. crq->valid = 0;
  3367. wmb();
  3368. }
  3369. vio_enable_interrupts(vdev);
  3370. if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
  3371. vio_disable_interrupts(vdev);
  3372. ibmvfc_handle_async(async, vhost);
  3373. async->valid = 0;
  3374. wmb();
  3375. } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
  3376. vio_disable_interrupts(vdev);
  3377. ibmvfc_handle_crq(crq, vhost, &evt_doneq);
  3378. crq->valid = 0;
  3379. wmb();
  3380. } else
  3381. done = 1;
  3382. }
  3383. spin_unlock(vhost->crq.q_lock);
  3384. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3385. list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
  3386. del_timer(&evt->timer);
  3387. list_del(&evt->queue_list);
  3388. ibmvfc_trc_end(evt);
  3389. evt->done(evt);
  3390. }
  3391. }
  3392. static int ibmvfc_toggle_scrq_irq(struct ibmvfc_queue *scrq, int enable)
  3393. {
  3394. struct device *dev = scrq->vhost->dev;
  3395. struct vio_dev *vdev = to_vio_dev(dev);
  3396. unsigned long rc;
  3397. int irq_action = H_ENABLE_VIO_INTERRUPT;
  3398. if (!enable)
  3399. irq_action = H_DISABLE_VIO_INTERRUPT;
  3400. rc = plpar_hcall_norets(H_VIOCTL, vdev->unit_address, irq_action,
  3401. scrq->hw_irq, 0, 0);
  3402. if (rc)
  3403. dev_err(dev, "Couldn't %s sub-crq[%lu] irq. rc=%ld\n",
  3404. enable ? "enable" : "disable", scrq->hwq_id, rc);
  3405. return rc;
  3406. }
  3407. static void ibmvfc_handle_scrq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
  3408. struct list_head *evt_doneq)
  3409. {
  3410. struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
  3411. switch (crq->valid) {
  3412. case IBMVFC_CRQ_CMD_RSP:
  3413. break;
  3414. case IBMVFC_CRQ_XPORT_EVENT:
  3415. return;
  3416. default:
  3417. dev_err(vhost->dev, "Got and invalid message type 0x%02x\n", crq->valid);
  3418. return;
  3419. }
  3420. /* The only kind of payload CRQs we should get are responses to
  3421. * things we send. Make sure this response is to something we
  3422. * actually sent
  3423. */
  3424. if (unlikely(!ibmvfc_valid_event(&evt->queue->evt_pool, evt))) {
  3425. dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
  3426. crq->ioba);
  3427. return;
  3428. }
  3429. if (unlikely(atomic_dec_if_positive(&evt->active))) {
  3430. dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
  3431. crq->ioba);
  3432. return;
  3433. }
  3434. spin_lock(&evt->queue->l_lock);
  3435. list_move_tail(&evt->queue_list, evt_doneq);
  3436. spin_unlock(&evt->queue->l_lock);
  3437. }
  3438. static struct ibmvfc_crq *ibmvfc_next_scrq(struct ibmvfc_queue *scrq)
  3439. {
  3440. struct ibmvfc_crq *crq;
  3441. crq = &scrq->msgs.scrq[scrq->cur].crq;
  3442. if (crq->valid & 0x80) {
  3443. if (++scrq->cur == scrq->size)
  3444. scrq->cur = 0;
  3445. rmb();
  3446. } else
  3447. crq = NULL;
  3448. return crq;
  3449. }
  3450. static void ibmvfc_drain_sub_crq(struct ibmvfc_queue *scrq)
  3451. {
  3452. struct ibmvfc_crq *crq;
  3453. struct ibmvfc_event *evt, *temp;
  3454. unsigned long flags;
  3455. int done = 0;
  3456. LIST_HEAD(evt_doneq);
  3457. spin_lock_irqsave(scrq->q_lock, flags);
  3458. while (!done) {
  3459. while ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
  3460. ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
  3461. crq->valid = 0;
  3462. wmb();
  3463. }
  3464. ibmvfc_toggle_scrq_irq(scrq, 1);
  3465. if ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
  3466. ibmvfc_toggle_scrq_irq(scrq, 0);
  3467. ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
  3468. crq->valid = 0;
  3469. wmb();
  3470. } else
  3471. done = 1;
  3472. }
  3473. spin_unlock_irqrestore(scrq->q_lock, flags);
  3474. list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
  3475. del_timer(&evt->timer);
  3476. list_del(&evt->queue_list);
  3477. ibmvfc_trc_end(evt);
  3478. evt->done(evt);
  3479. }
  3480. }
  3481. static irqreturn_t ibmvfc_interrupt_mq(int irq, void *scrq_instance)
  3482. {
  3483. struct ibmvfc_queue *scrq = (struct ibmvfc_queue *)scrq_instance;
  3484. ibmvfc_toggle_scrq_irq(scrq, 0);
  3485. ibmvfc_drain_sub_crq(scrq);
  3486. return IRQ_HANDLED;
  3487. }
  3488. /**
  3489. * ibmvfc_init_tgt - Set the next init job step for the target
  3490. * @tgt: ibmvfc target struct
  3491. * @job_step: job step to perform
  3492. *
  3493. **/
  3494. static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
  3495. void (*job_step) (struct ibmvfc_target *))
  3496. {
  3497. if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT))
  3498. tgt->job_step = job_step;
  3499. wake_up(&tgt->vhost->work_wait_q);
  3500. }
  3501. /**
  3502. * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
  3503. * @tgt: ibmvfc target struct
  3504. * @job_step: initialization job step
  3505. *
  3506. * Returns: 1 if step will be retried / 0 if not
  3507. *
  3508. **/
  3509. static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
  3510. void (*job_step) (struct ibmvfc_target *))
  3511. {
  3512. if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
  3513. ibmvfc_del_tgt(tgt);
  3514. wake_up(&tgt->vhost->work_wait_q);
  3515. return 0;
  3516. } else
  3517. ibmvfc_init_tgt(tgt, job_step);
  3518. return 1;
  3519. }
  3520. /* Defined in FC-LS */
  3521. static const struct {
  3522. int code;
  3523. int retry;
  3524. int logged_in;
  3525. } prli_rsp [] = {
  3526. { 0, 1, 0 },
  3527. { 1, 0, 1 },
  3528. { 2, 1, 0 },
  3529. { 3, 1, 0 },
  3530. { 4, 0, 0 },
  3531. { 5, 0, 0 },
  3532. { 6, 0, 1 },
  3533. { 7, 0, 0 },
  3534. { 8, 1, 0 },
  3535. };
  3536. /**
  3537. * ibmvfc_get_prli_rsp - Find PRLI response index
  3538. * @flags: PRLI response flags
  3539. *
  3540. **/
  3541. static int ibmvfc_get_prli_rsp(u16 flags)
  3542. {
  3543. int i;
  3544. int code = (flags & 0x0f00) >> 8;
  3545. for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
  3546. if (prli_rsp[i].code == code)
  3547. return i;
  3548. return 0;
  3549. }
  3550. /**
  3551. * ibmvfc_tgt_prli_done - Completion handler for Process Login
  3552. * @evt: ibmvfc event struct
  3553. *
  3554. **/
  3555. static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
  3556. {
  3557. struct ibmvfc_target *tgt = evt->tgt;
  3558. struct ibmvfc_host *vhost = evt->vhost;
  3559. struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
  3560. struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
  3561. u32 status = be16_to_cpu(rsp->common.status);
  3562. int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
  3563. vhost->discovery_threads--;
  3564. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3565. switch (status) {
  3566. case IBMVFC_MAD_SUCCESS:
  3567. tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
  3568. parms->type, parms->flags, parms->service_parms);
  3569. if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
  3570. index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
  3571. if (prli_rsp[index].logged_in) {
  3572. if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
  3573. tgt->need_login = 0;
  3574. tgt->ids.roles = 0;
  3575. if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
  3576. tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
  3577. if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
  3578. tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
  3579. tgt->add_rport = 1;
  3580. } else
  3581. ibmvfc_del_tgt(tgt);
  3582. } else if (prli_rsp[index].retry)
  3583. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  3584. else
  3585. ibmvfc_del_tgt(tgt);
  3586. } else
  3587. ibmvfc_del_tgt(tgt);
  3588. break;
  3589. case IBMVFC_MAD_DRIVER_FAILED:
  3590. break;
  3591. case IBMVFC_MAD_CRQ_ERROR:
  3592. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  3593. break;
  3594. case IBMVFC_MAD_FAILED:
  3595. default:
  3596. if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
  3597. be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
  3598. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3599. else if (tgt->logo_rcvd)
  3600. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3601. else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  3602. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  3603. else
  3604. ibmvfc_del_tgt(tgt);
  3605. tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
  3606. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  3607. be16_to_cpu(rsp->status), be16_to_cpu(rsp->error), status);
  3608. break;
  3609. }
  3610. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3611. ibmvfc_free_event(evt);
  3612. wake_up(&vhost->work_wait_q);
  3613. }
  3614. /**
  3615. * ibmvfc_tgt_send_prli - Send a process login
  3616. * @tgt: ibmvfc target struct
  3617. *
  3618. **/
  3619. static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
  3620. {
  3621. struct ibmvfc_process_login *prli;
  3622. struct ibmvfc_host *vhost = tgt->vhost;
  3623. struct ibmvfc_event *evt;
  3624. if (vhost->discovery_threads >= disc_threads)
  3625. return;
  3626. kref_get(&tgt->kref);
  3627. evt = ibmvfc_get_reserved_event(&vhost->crq);
  3628. if (!evt) {
  3629. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3630. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3631. __ibmvfc_reset_host(vhost);
  3632. return;
  3633. }
  3634. vhost->discovery_threads++;
  3635. ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
  3636. evt->tgt = tgt;
  3637. prli = &evt->iu.prli;
  3638. memset(prli, 0, sizeof(*prli));
  3639. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
  3640. prli->common.version = cpu_to_be32(2);
  3641. prli->target_wwpn = cpu_to_be64(tgt->wwpn);
  3642. } else {
  3643. prli->common.version = cpu_to_be32(1);
  3644. }
  3645. prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
  3646. prli->common.length = cpu_to_be16(sizeof(*prli));
  3647. prli->scsi_id = cpu_to_be64(tgt->scsi_id);
  3648. prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
  3649. prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
  3650. prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
  3651. prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
  3652. if (cls3_error)
  3653. prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
  3654. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3655. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3656. vhost->discovery_threads--;
  3657. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3658. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3659. } else
  3660. tgt_dbg(tgt, "Sent process login\n");
  3661. }
  3662. /**
  3663. * ibmvfc_tgt_plogi_done - Completion handler for Port Login
  3664. * @evt: ibmvfc event struct
  3665. *
  3666. **/
  3667. static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
  3668. {
  3669. struct ibmvfc_target *tgt = evt->tgt;
  3670. struct ibmvfc_host *vhost = evt->vhost;
  3671. struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
  3672. u32 status = be16_to_cpu(rsp->common.status);
  3673. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3674. vhost->discovery_threads--;
  3675. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3676. switch (status) {
  3677. case IBMVFC_MAD_SUCCESS:
  3678. tgt_dbg(tgt, "Port Login succeeded\n");
  3679. if (tgt->ids.port_name &&
  3680. tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
  3681. vhost->reinit = 1;
  3682. tgt_dbg(tgt, "Port re-init required\n");
  3683. break;
  3684. }
  3685. tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
  3686. tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
  3687. tgt->ids.port_id = tgt->scsi_id;
  3688. memcpy(&tgt->service_parms, &rsp->service_parms,
  3689. sizeof(tgt->service_parms));
  3690. memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
  3691. sizeof(tgt->service_parms_change));
  3692. ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
  3693. break;
  3694. case IBMVFC_MAD_DRIVER_FAILED:
  3695. break;
  3696. case IBMVFC_MAD_CRQ_ERROR:
  3697. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3698. break;
  3699. case IBMVFC_MAD_FAILED:
  3700. default:
  3701. if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  3702. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3703. else
  3704. ibmvfc_del_tgt(tgt);
  3705. tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  3706. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  3707. be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
  3708. ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
  3709. ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
  3710. break;
  3711. }
  3712. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3713. ibmvfc_free_event(evt);
  3714. wake_up(&vhost->work_wait_q);
  3715. }
  3716. /**
  3717. * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
  3718. * @tgt: ibmvfc target struct
  3719. *
  3720. **/
  3721. static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
  3722. {
  3723. struct ibmvfc_port_login *plogi;
  3724. struct ibmvfc_host *vhost = tgt->vhost;
  3725. struct ibmvfc_event *evt;
  3726. if (vhost->discovery_threads >= disc_threads)
  3727. return;
  3728. kref_get(&tgt->kref);
  3729. tgt->logo_rcvd = 0;
  3730. evt = ibmvfc_get_reserved_event(&vhost->crq);
  3731. if (!evt) {
  3732. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3733. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3734. __ibmvfc_reset_host(vhost);
  3735. return;
  3736. }
  3737. vhost->discovery_threads++;
  3738. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3739. ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
  3740. evt->tgt = tgt;
  3741. plogi = &evt->iu.plogi;
  3742. memset(plogi, 0, sizeof(*plogi));
  3743. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
  3744. plogi->common.version = cpu_to_be32(2);
  3745. plogi->target_wwpn = cpu_to_be64(tgt->wwpn);
  3746. } else {
  3747. plogi->common.version = cpu_to_be32(1);
  3748. }
  3749. plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
  3750. plogi->common.length = cpu_to_be16(sizeof(*plogi));
  3751. plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
  3752. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3753. vhost->discovery_threads--;
  3754. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3755. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3756. } else
  3757. tgt_dbg(tgt, "Sent port login\n");
  3758. }
  3759. /**
  3760. * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
  3761. * @evt: ibmvfc event struct
  3762. *
  3763. **/
  3764. static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
  3765. {
  3766. struct ibmvfc_target *tgt = evt->tgt;
  3767. struct ibmvfc_host *vhost = evt->vhost;
  3768. struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
  3769. u32 status = be16_to_cpu(rsp->common.status);
  3770. vhost->discovery_threads--;
  3771. ibmvfc_free_event(evt);
  3772. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3773. switch (status) {
  3774. case IBMVFC_MAD_SUCCESS:
  3775. tgt_dbg(tgt, "Implicit Logout succeeded\n");
  3776. break;
  3777. case IBMVFC_MAD_DRIVER_FAILED:
  3778. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3779. wake_up(&vhost->work_wait_q);
  3780. return;
  3781. case IBMVFC_MAD_FAILED:
  3782. default:
  3783. tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
  3784. break;
  3785. }
  3786. ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
  3787. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3788. wake_up(&vhost->work_wait_q);
  3789. }
  3790. /**
  3791. * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
  3792. * @tgt: ibmvfc target struct
  3793. * @done: Routine to call when the event is responded to
  3794. *
  3795. * Returns:
  3796. * Allocated and initialized ibmvfc_event struct
  3797. **/
  3798. static struct ibmvfc_event *__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target *tgt,
  3799. void (*done) (struct ibmvfc_event *))
  3800. {
  3801. struct ibmvfc_implicit_logout *mad;
  3802. struct ibmvfc_host *vhost = tgt->vhost;
  3803. struct ibmvfc_event *evt;
  3804. kref_get(&tgt->kref);
  3805. evt = ibmvfc_get_reserved_event(&vhost->crq);
  3806. if (!evt)
  3807. return NULL;
  3808. ibmvfc_init_event(evt, done, IBMVFC_MAD_FORMAT);
  3809. evt->tgt = tgt;
  3810. mad = &evt->iu.implicit_logout;
  3811. memset(mad, 0, sizeof(*mad));
  3812. mad->common.version = cpu_to_be32(1);
  3813. mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
  3814. mad->common.length = cpu_to_be16(sizeof(*mad));
  3815. mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
  3816. return evt;
  3817. }
  3818. /**
  3819. * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
  3820. * @tgt: ibmvfc target struct
  3821. *
  3822. **/
  3823. static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
  3824. {
  3825. struct ibmvfc_host *vhost = tgt->vhost;
  3826. struct ibmvfc_event *evt;
  3827. if (vhost->discovery_threads >= disc_threads)
  3828. return;
  3829. vhost->discovery_threads++;
  3830. evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
  3831. ibmvfc_tgt_implicit_logout_done);
  3832. if (!evt) {
  3833. vhost->discovery_threads--;
  3834. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3835. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3836. __ibmvfc_reset_host(vhost);
  3837. return;
  3838. }
  3839. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3840. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3841. vhost->discovery_threads--;
  3842. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3843. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3844. } else
  3845. tgt_dbg(tgt, "Sent Implicit Logout\n");
  3846. }
  3847. /**
  3848. * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
  3849. * @evt: ibmvfc event struct
  3850. *
  3851. **/
  3852. static void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event *evt)
  3853. {
  3854. struct ibmvfc_target *tgt = evt->tgt;
  3855. struct ibmvfc_host *vhost = evt->vhost;
  3856. struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
  3857. u32 status = be16_to_cpu(mad->common.status);
  3858. vhost->discovery_threads--;
  3859. ibmvfc_free_event(evt);
  3860. /*
  3861. * If our state is IBMVFC_HOST_OFFLINE, we could be unloading the
  3862. * driver in which case we need to free up all the targets. If we are
  3863. * not unloading, we will still go through a hard reset to get out of
  3864. * offline state, so there is no need to track the old targets in that
  3865. * case.
  3866. */
  3867. if (status == IBMVFC_MAD_SUCCESS || vhost->state == IBMVFC_HOST_OFFLINE)
  3868. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3869. else
  3870. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT);
  3871. tgt_dbg(tgt, "Implicit Logout %s\n", (status == IBMVFC_MAD_SUCCESS) ? "succeeded" : "failed");
  3872. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3873. wake_up(&vhost->work_wait_q);
  3874. }
  3875. /**
  3876. * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
  3877. * @tgt: ibmvfc target struct
  3878. *
  3879. **/
  3880. static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *tgt)
  3881. {
  3882. struct ibmvfc_host *vhost = tgt->vhost;
  3883. struct ibmvfc_event *evt;
  3884. if (!vhost->logged_in) {
  3885. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3886. return;
  3887. }
  3888. if (vhost->discovery_threads >= disc_threads)
  3889. return;
  3890. vhost->discovery_threads++;
  3891. evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
  3892. ibmvfc_tgt_implicit_logout_and_del_done);
  3893. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT);
  3894. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3895. vhost->discovery_threads--;
  3896. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3897. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3898. } else
  3899. tgt_dbg(tgt, "Sent Implicit Logout\n");
  3900. }
  3901. /**
  3902. * ibmvfc_tgt_move_login_done - Completion handler for Move Login
  3903. * @evt: ibmvfc event struct
  3904. *
  3905. **/
  3906. static void ibmvfc_tgt_move_login_done(struct ibmvfc_event *evt)
  3907. {
  3908. struct ibmvfc_target *tgt = evt->tgt;
  3909. struct ibmvfc_host *vhost = evt->vhost;
  3910. struct ibmvfc_move_login *rsp = &evt->xfer_iu->move_login;
  3911. u32 status = be16_to_cpu(rsp->common.status);
  3912. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3913. vhost->discovery_threads--;
  3914. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3915. switch (status) {
  3916. case IBMVFC_MAD_SUCCESS:
  3917. tgt_dbg(tgt, "Move Login succeeded for new scsi_id: %llX\n", tgt->new_scsi_id);
  3918. tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
  3919. tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
  3920. tgt->scsi_id = tgt->new_scsi_id;
  3921. tgt->ids.port_id = tgt->scsi_id;
  3922. memcpy(&tgt->service_parms, &rsp->service_parms,
  3923. sizeof(tgt->service_parms));
  3924. memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
  3925. sizeof(tgt->service_parms_change));
  3926. ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
  3927. break;
  3928. case IBMVFC_MAD_DRIVER_FAILED:
  3929. break;
  3930. case IBMVFC_MAD_CRQ_ERROR:
  3931. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
  3932. break;
  3933. case IBMVFC_MAD_FAILED:
  3934. default:
  3935. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
  3936. tgt_log(tgt, level,
  3937. "Move Login failed: new scsi_id: %llX, flags:%x, vios_flags:%x, rc=0x%02X\n",
  3938. tgt->new_scsi_id, be32_to_cpu(rsp->flags), be16_to_cpu(rsp->vios_flags),
  3939. status);
  3940. break;
  3941. }
  3942. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3943. ibmvfc_free_event(evt);
  3944. wake_up(&vhost->work_wait_q);
  3945. }
  3946. /**
  3947. * ibmvfc_tgt_move_login - Initiate a move login for specified target
  3948. * @tgt: ibmvfc target struct
  3949. *
  3950. **/
  3951. static void ibmvfc_tgt_move_login(struct ibmvfc_target *tgt)
  3952. {
  3953. struct ibmvfc_host *vhost = tgt->vhost;
  3954. struct ibmvfc_move_login *move;
  3955. struct ibmvfc_event *evt;
  3956. if (vhost->discovery_threads >= disc_threads)
  3957. return;
  3958. kref_get(&tgt->kref);
  3959. evt = ibmvfc_get_reserved_event(&vhost->crq);
  3960. if (!evt) {
  3961. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3962. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3963. __ibmvfc_reset_host(vhost);
  3964. return;
  3965. }
  3966. vhost->discovery_threads++;
  3967. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3968. ibmvfc_init_event(evt, ibmvfc_tgt_move_login_done, IBMVFC_MAD_FORMAT);
  3969. evt->tgt = tgt;
  3970. move = &evt->iu.move_login;
  3971. memset(move, 0, sizeof(*move));
  3972. move->common.version = cpu_to_be32(1);
  3973. move->common.opcode = cpu_to_be32(IBMVFC_MOVE_LOGIN);
  3974. move->common.length = cpu_to_be16(sizeof(*move));
  3975. move->old_scsi_id = cpu_to_be64(tgt->scsi_id);
  3976. move->new_scsi_id = cpu_to_be64(tgt->new_scsi_id);
  3977. move->wwpn = cpu_to_be64(tgt->wwpn);
  3978. move->node_name = cpu_to_be64(tgt->ids.node_name);
  3979. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3980. vhost->discovery_threads--;
  3981. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3982. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3983. } else
  3984. tgt_dbg(tgt, "Sent Move Login for new scsi_id: %llX\n", tgt->new_scsi_id);
  3985. }
  3986. /**
  3987. * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
  3988. * @mad: ibmvfc passthru mad struct
  3989. * @tgt: ibmvfc target struct
  3990. *
  3991. * Returns:
  3992. * 1 if PLOGI needed / 0 if PLOGI not needed
  3993. **/
  3994. static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
  3995. struct ibmvfc_target *tgt)
  3996. {
  3997. if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
  3998. return 1;
  3999. if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
  4000. return 1;
  4001. if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
  4002. return 1;
  4003. return 0;
  4004. }
  4005. /**
  4006. * ibmvfc_tgt_adisc_done - Completion handler for ADISC
  4007. * @evt: ibmvfc event struct
  4008. *
  4009. **/
  4010. static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
  4011. {
  4012. struct ibmvfc_target *tgt = evt->tgt;
  4013. struct ibmvfc_host *vhost = evt->vhost;
  4014. struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
  4015. u32 status = be16_to_cpu(mad->common.status);
  4016. u8 fc_reason, fc_explain;
  4017. vhost->discovery_threads--;
  4018. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  4019. del_timer(&tgt->timer);
  4020. switch (status) {
  4021. case IBMVFC_MAD_SUCCESS:
  4022. tgt_dbg(tgt, "ADISC succeeded\n");
  4023. if (ibmvfc_adisc_needs_plogi(mad, tgt))
  4024. ibmvfc_del_tgt(tgt);
  4025. break;
  4026. case IBMVFC_MAD_DRIVER_FAILED:
  4027. break;
  4028. case IBMVFC_MAD_FAILED:
  4029. default:
  4030. ibmvfc_del_tgt(tgt);
  4031. fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
  4032. fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
  4033. tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  4034. ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
  4035. be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
  4036. ibmvfc_get_fc_type(fc_reason), fc_reason,
  4037. ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
  4038. break;
  4039. }
  4040. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4041. ibmvfc_free_event(evt);
  4042. wake_up(&vhost->work_wait_q);
  4043. }
  4044. /**
  4045. * ibmvfc_init_passthru - Initialize an event struct for FC passthru
  4046. * @evt: ibmvfc event struct
  4047. *
  4048. **/
  4049. static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
  4050. {
  4051. struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
  4052. memset(mad, 0, sizeof(*mad));
  4053. mad->common.version = cpu_to_be32(1);
  4054. mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
  4055. mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
  4056. mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
  4057. offsetof(struct ibmvfc_passthru_mad, iu));
  4058. mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
  4059. mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
  4060. mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
  4061. mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
  4062. offsetof(struct ibmvfc_passthru_mad, fc_iu) +
  4063. offsetof(struct ibmvfc_passthru_fc_iu, payload));
  4064. mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
  4065. mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
  4066. offsetof(struct ibmvfc_passthru_mad, fc_iu) +
  4067. offsetof(struct ibmvfc_passthru_fc_iu, response));
  4068. mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
  4069. }
  4070. /**
  4071. * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
  4072. * @evt: ibmvfc event struct
  4073. *
  4074. * Just cleanup this event struct. Everything else is handled by
  4075. * the ADISC completion handler. If the ADISC never actually comes
  4076. * back, we still have the timer running on the ADISC event struct
  4077. * which will fire and cause the CRQ to get reset.
  4078. *
  4079. **/
  4080. static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
  4081. {
  4082. struct ibmvfc_host *vhost = evt->vhost;
  4083. struct ibmvfc_target *tgt = evt->tgt;
  4084. tgt_dbg(tgt, "ADISC cancel complete\n");
  4085. vhost->abort_threads--;
  4086. ibmvfc_free_event(evt);
  4087. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4088. wake_up(&vhost->work_wait_q);
  4089. }
  4090. /**
  4091. * ibmvfc_adisc_timeout - Handle an ADISC timeout
  4092. * @t: ibmvfc target struct
  4093. *
  4094. * If an ADISC times out, send a cancel. If the cancel times
  4095. * out, reset the CRQ. When the ADISC comes back as cancelled,
  4096. * log back into the target.
  4097. **/
  4098. static void ibmvfc_adisc_timeout(struct timer_list *t)
  4099. {
  4100. struct ibmvfc_target *tgt = from_timer(tgt, t, timer);
  4101. struct ibmvfc_host *vhost = tgt->vhost;
  4102. struct ibmvfc_event *evt;
  4103. struct ibmvfc_tmf *tmf;
  4104. unsigned long flags;
  4105. int rc;
  4106. tgt_dbg(tgt, "ADISC timeout\n");
  4107. spin_lock_irqsave(vhost->host->host_lock, flags);
  4108. if (vhost->abort_threads >= disc_threads ||
  4109. tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
  4110. vhost->state != IBMVFC_INITIALIZING ||
  4111. vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
  4112. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4113. return;
  4114. }
  4115. vhost->abort_threads++;
  4116. kref_get(&tgt->kref);
  4117. evt = ibmvfc_get_reserved_event(&vhost->crq);
  4118. if (!evt) {
  4119. tgt_err(tgt, "Failed to get cancel event for ADISC.\n");
  4120. vhost->abort_threads--;
  4121. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4122. __ibmvfc_reset_host(vhost);
  4123. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4124. return;
  4125. }
  4126. ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
  4127. evt->tgt = tgt;
  4128. tmf = &evt->iu.tmf;
  4129. memset(tmf, 0, sizeof(*tmf));
  4130. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
  4131. tmf->common.version = cpu_to_be32(2);
  4132. tmf->target_wwpn = cpu_to_be64(tgt->wwpn);
  4133. } else {
  4134. tmf->common.version = cpu_to_be32(1);
  4135. }
  4136. tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
  4137. tmf->common.length = cpu_to_be16(sizeof(*tmf));
  4138. tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
  4139. tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
  4140. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  4141. if (rc) {
  4142. tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
  4143. vhost->abort_threads--;
  4144. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4145. __ibmvfc_reset_host(vhost);
  4146. } else
  4147. tgt_dbg(tgt, "Attempting to cancel ADISC\n");
  4148. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4149. }
  4150. /**
  4151. * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
  4152. * @tgt: ibmvfc target struct
  4153. *
  4154. * When sending an ADISC we end up with two timers running. The
  4155. * first timer is the timer in the ibmvfc target struct. If this
  4156. * fires, we send a cancel to the target. The second timer is the
  4157. * timer on the ibmvfc event for the ADISC, which is longer. If that
  4158. * fires, it means the ADISC timed out and our attempt to cancel it
  4159. * also failed, so we need to reset the CRQ.
  4160. **/
  4161. static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
  4162. {
  4163. struct ibmvfc_passthru_mad *mad;
  4164. struct ibmvfc_host *vhost = tgt->vhost;
  4165. struct ibmvfc_event *evt;
  4166. if (vhost->discovery_threads >= disc_threads)
  4167. return;
  4168. kref_get(&tgt->kref);
  4169. evt = ibmvfc_get_reserved_event(&vhost->crq);
  4170. if (!evt) {
  4171. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  4172. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4173. __ibmvfc_reset_host(vhost);
  4174. return;
  4175. }
  4176. vhost->discovery_threads++;
  4177. ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
  4178. evt->tgt = tgt;
  4179. ibmvfc_init_passthru(evt);
  4180. mad = &evt->iu.passthru;
  4181. mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
  4182. mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
  4183. mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
  4184. mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
  4185. memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
  4186. sizeof(vhost->login_buf->resp.port_name));
  4187. memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
  4188. sizeof(vhost->login_buf->resp.node_name));
  4189. mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
  4190. if (timer_pending(&tgt->timer))
  4191. mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
  4192. else {
  4193. tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
  4194. add_timer(&tgt->timer);
  4195. }
  4196. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  4197. if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
  4198. vhost->discovery_threads--;
  4199. del_timer(&tgt->timer);
  4200. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  4201. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4202. } else
  4203. tgt_dbg(tgt, "Sent ADISC\n");
  4204. }
  4205. /**
  4206. * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
  4207. * @evt: ibmvfc event struct
  4208. *
  4209. **/
  4210. static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
  4211. {
  4212. struct ibmvfc_target *tgt = evt->tgt;
  4213. struct ibmvfc_host *vhost = evt->vhost;
  4214. struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
  4215. u32 status = be16_to_cpu(rsp->common.status);
  4216. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4217. vhost->discovery_threads--;
  4218. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  4219. switch (status) {
  4220. case IBMVFC_MAD_SUCCESS:
  4221. tgt_dbg(tgt, "Query Target succeeded\n");
  4222. if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
  4223. ibmvfc_del_tgt(tgt);
  4224. else
  4225. ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
  4226. break;
  4227. case IBMVFC_MAD_DRIVER_FAILED:
  4228. break;
  4229. case IBMVFC_MAD_CRQ_ERROR:
  4230. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
  4231. break;
  4232. case IBMVFC_MAD_FAILED:
  4233. default:
  4234. if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
  4235. be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
  4236. be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
  4237. ibmvfc_del_tgt(tgt);
  4238. else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  4239. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
  4240. else
  4241. ibmvfc_del_tgt(tgt);
  4242. tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  4243. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  4244. be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
  4245. ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
  4246. ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
  4247. status);
  4248. break;
  4249. }
  4250. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4251. ibmvfc_free_event(evt);
  4252. wake_up(&vhost->work_wait_q);
  4253. }
  4254. /**
  4255. * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
  4256. * @tgt: ibmvfc target struct
  4257. *
  4258. **/
  4259. static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
  4260. {
  4261. struct ibmvfc_query_tgt *query_tgt;
  4262. struct ibmvfc_host *vhost = tgt->vhost;
  4263. struct ibmvfc_event *evt;
  4264. if (vhost->discovery_threads >= disc_threads)
  4265. return;
  4266. kref_get(&tgt->kref);
  4267. evt = ibmvfc_get_reserved_event(&vhost->crq);
  4268. if (!evt) {
  4269. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  4270. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4271. __ibmvfc_reset_host(vhost);
  4272. return;
  4273. }
  4274. vhost->discovery_threads++;
  4275. evt->tgt = tgt;
  4276. ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
  4277. query_tgt = &evt->iu.query_tgt;
  4278. memset(query_tgt, 0, sizeof(*query_tgt));
  4279. query_tgt->common.version = cpu_to_be32(1);
  4280. query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
  4281. query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
  4282. query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
  4283. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  4284. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  4285. vhost->discovery_threads--;
  4286. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  4287. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4288. } else
  4289. tgt_dbg(tgt, "Sent Query Target\n");
  4290. }
  4291. /**
  4292. * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
  4293. * @vhost: ibmvfc host struct
  4294. * @target: Holds SCSI ID to allocate target forand the WWPN
  4295. *
  4296. * Returns:
  4297. * 0 on success / other on failure
  4298. **/
  4299. static int ibmvfc_alloc_target(struct ibmvfc_host *vhost,
  4300. struct ibmvfc_discover_targets_entry *target)
  4301. {
  4302. struct ibmvfc_target *stgt = NULL;
  4303. struct ibmvfc_target *wtgt = NULL;
  4304. struct ibmvfc_target *tgt;
  4305. unsigned long flags;
  4306. u64 scsi_id = be32_to_cpu(target->scsi_id) & IBMVFC_DISC_TGT_SCSI_ID_MASK;
  4307. u64 wwpn = be64_to_cpu(target->wwpn);
  4308. /* Look to see if we already have a target allocated for this SCSI ID or WWPN */
  4309. spin_lock_irqsave(vhost->host->host_lock, flags);
  4310. list_for_each_entry(tgt, &vhost->targets, queue) {
  4311. if (tgt->wwpn == wwpn) {
  4312. wtgt = tgt;
  4313. break;
  4314. }
  4315. }
  4316. list_for_each_entry(tgt, &vhost->targets, queue) {
  4317. if (tgt->scsi_id == scsi_id) {
  4318. stgt = tgt;
  4319. break;
  4320. }
  4321. }
  4322. if (wtgt && !stgt) {
  4323. /*
  4324. * A WWPN target has moved and we still are tracking the old
  4325. * SCSI ID. The only way we should be able to get here is if
  4326. * we attempted to send an implicit logout for the old SCSI ID
  4327. * and it failed for some reason, such as there being I/O
  4328. * pending to the target. In this case, we will have already
  4329. * deleted the rport from the FC transport so we do a move
  4330. * login, which works even with I/O pending, however, if
  4331. * there is still I/O pending, it will stay outstanding, so
  4332. * we only do this if fast fail is disabled for the rport,
  4333. * otherwise we let terminate_rport_io clean up the port
  4334. * before we login at the new location.
  4335. */
  4336. if (wtgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
  4337. if (wtgt->move_login) {
  4338. /*
  4339. * Do a move login here. The old target is no longer
  4340. * known to the transport layer We don't use the
  4341. * normal ibmvfc_set_tgt_action to set this, as we
  4342. * don't normally want to allow this state change.
  4343. */
  4344. wtgt->new_scsi_id = scsi_id;
  4345. wtgt->action = IBMVFC_TGT_ACTION_INIT;
  4346. wtgt->init_retries = 0;
  4347. ibmvfc_init_tgt(wtgt, ibmvfc_tgt_move_login);
  4348. }
  4349. goto unlock_out;
  4350. } else {
  4351. tgt_err(wtgt, "Unexpected target state: %d, %p\n",
  4352. wtgt->action, wtgt->rport);
  4353. }
  4354. } else if (stgt) {
  4355. if (tgt->need_login)
  4356. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  4357. goto unlock_out;
  4358. }
  4359. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4360. tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
  4361. memset(tgt, 0, sizeof(*tgt));
  4362. tgt->scsi_id = scsi_id;
  4363. tgt->wwpn = wwpn;
  4364. tgt->vhost = vhost;
  4365. tgt->need_login = 1;
  4366. timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
  4367. kref_init(&tgt->kref);
  4368. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  4369. spin_lock_irqsave(vhost->host->host_lock, flags);
  4370. tgt->cancel_key = vhost->task_set++;
  4371. list_add_tail(&tgt->queue, &vhost->targets);
  4372. unlock_out:
  4373. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4374. return 0;
  4375. }
  4376. /**
  4377. * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
  4378. * @vhost: ibmvfc host struct
  4379. *
  4380. * Returns:
  4381. * 0 on success / other on failure
  4382. **/
  4383. static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
  4384. {
  4385. int i, rc;
  4386. for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
  4387. rc = ibmvfc_alloc_target(vhost, &vhost->scsi_scrqs.disc_buf[i]);
  4388. return rc;
  4389. }
  4390. /**
  4391. * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
  4392. * @evt: ibmvfc event struct
  4393. *
  4394. **/
  4395. static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
  4396. {
  4397. struct ibmvfc_host *vhost = evt->vhost;
  4398. struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
  4399. u32 mad_status = be16_to_cpu(rsp->common.status);
  4400. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4401. switch (mad_status) {
  4402. case IBMVFC_MAD_SUCCESS:
  4403. ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
  4404. vhost->num_targets = be32_to_cpu(rsp->num_written);
  4405. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
  4406. break;
  4407. case IBMVFC_MAD_FAILED:
  4408. level += ibmvfc_retry_host_init(vhost);
  4409. ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
  4410. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  4411. be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
  4412. break;
  4413. case IBMVFC_MAD_DRIVER_FAILED:
  4414. break;
  4415. default:
  4416. dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
  4417. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4418. break;
  4419. }
  4420. ibmvfc_free_event(evt);
  4421. wake_up(&vhost->work_wait_q);
  4422. }
  4423. /**
  4424. * ibmvfc_discover_targets - Send Discover Targets MAD
  4425. * @vhost: ibmvfc host struct
  4426. *
  4427. **/
  4428. static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
  4429. {
  4430. struct ibmvfc_discover_targets *mad;
  4431. struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
  4432. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4433. if (!evt) {
  4434. ibmvfc_log(vhost, level, "Discover Targets failed: no available events\n");
  4435. ibmvfc_hard_reset_host(vhost);
  4436. return;
  4437. }
  4438. ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
  4439. mad = &evt->iu.discover_targets;
  4440. memset(mad, 0, sizeof(*mad));
  4441. mad->common.version = cpu_to_be32(1);
  4442. mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
  4443. mad->common.length = cpu_to_be16(sizeof(*mad));
  4444. mad->bufflen = cpu_to_be32(vhost->scsi_scrqs.disc_buf_sz);
  4445. mad->buffer.va = cpu_to_be64(vhost->scsi_scrqs.disc_buf_dma);
  4446. mad->buffer.len = cpu_to_be32(vhost->scsi_scrqs.disc_buf_sz);
  4447. mad->flags = cpu_to_be32(IBMVFC_DISC_TGT_PORT_ID_WWPN_LIST);
  4448. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  4449. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  4450. ibmvfc_dbg(vhost, "Sent discover targets\n");
  4451. else
  4452. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4453. }
  4454. static void ibmvfc_channel_setup_done(struct ibmvfc_event *evt)
  4455. {
  4456. struct ibmvfc_host *vhost = evt->vhost;
  4457. struct ibmvfc_channel_setup *setup = vhost->channel_setup_buf;
  4458. struct ibmvfc_channels *scrqs = &vhost->scsi_scrqs;
  4459. u32 mad_status = be16_to_cpu(evt->xfer_iu->channel_setup.common.status);
  4460. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4461. int flags, active_queues, i;
  4462. ibmvfc_free_event(evt);
  4463. switch (mad_status) {
  4464. case IBMVFC_MAD_SUCCESS:
  4465. ibmvfc_dbg(vhost, "Channel Setup succeeded\n");
  4466. flags = be32_to_cpu(setup->flags);
  4467. vhost->do_enquiry = 0;
  4468. active_queues = be32_to_cpu(setup->num_scsi_subq_channels);
  4469. scrqs->active_queues = active_queues;
  4470. if (flags & IBMVFC_CHANNELS_CANCELED) {
  4471. ibmvfc_dbg(vhost, "Channels Canceled\n");
  4472. vhost->using_channels = 0;
  4473. } else {
  4474. if (active_queues)
  4475. vhost->using_channels = 1;
  4476. for (i = 0; i < active_queues; i++)
  4477. scrqs->scrqs[i].vios_cookie =
  4478. be64_to_cpu(setup->channel_handles[i]);
  4479. ibmvfc_dbg(vhost, "Using %u channels\n",
  4480. vhost->scsi_scrqs.active_queues);
  4481. }
  4482. break;
  4483. case IBMVFC_MAD_FAILED:
  4484. level += ibmvfc_retry_host_init(vhost);
  4485. ibmvfc_log(vhost, level, "Channel Setup failed\n");
  4486. fallthrough;
  4487. case IBMVFC_MAD_DRIVER_FAILED:
  4488. return;
  4489. default:
  4490. dev_err(vhost->dev, "Invalid Channel Setup response: 0x%x\n",
  4491. mad_status);
  4492. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4493. return;
  4494. }
  4495. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  4496. wake_up(&vhost->work_wait_q);
  4497. }
  4498. static void ibmvfc_channel_setup(struct ibmvfc_host *vhost)
  4499. {
  4500. struct ibmvfc_channel_setup_mad *mad;
  4501. struct ibmvfc_channel_setup *setup_buf = vhost->channel_setup_buf;
  4502. struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
  4503. struct ibmvfc_channels *scrqs = &vhost->scsi_scrqs;
  4504. unsigned int num_channels =
  4505. min(scrqs->desired_queues, vhost->max_vios_scsi_channels);
  4506. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4507. int i;
  4508. if (!evt) {
  4509. ibmvfc_log(vhost, level, "Channel Setup failed: no available events\n");
  4510. ibmvfc_hard_reset_host(vhost);
  4511. return;
  4512. }
  4513. memset(setup_buf, 0, sizeof(*setup_buf));
  4514. if (num_channels == 0)
  4515. setup_buf->flags = cpu_to_be32(IBMVFC_CANCEL_CHANNELS);
  4516. else {
  4517. setup_buf->num_scsi_subq_channels = cpu_to_be32(num_channels);
  4518. for (i = 0; i < num_channels; i++)
  4519. setup_buf->channel_handles[i] = cpu_to_be64(scrqs->scrqs[i].cookie);
  4520. }
  4521. ibmvfc_init_event(evt, ibmvfc_channel_setup_done, IBMVFC_MAD_FORMAT);
  4522. mad = &evt->iu.channel_setup;
  4523. memset(mad, 0, sizeof(*mad));
  4524. mad->common.version = cpu_to_be32(1);
  4525. mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_SETUP);
  4526. mad->common.length = cpu_to_be16(sizeof(*mad));
  4527. mad->buffer.va = cpu_to_be64(vhost->channel_setup_dma);
  4528. mad->buffer.len = cpu_to_be32(sizeof(*vhost->channel_setup_buf));
  4529. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  4530. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  4531. ibmvfc_dbg(vhost, "Sent channel setup\n");
  4532. else
  4533. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  4534. }
  4535. static void ibmvfc_channel_enquiry_done(struct ibmvfc_event *evt)
  4536. {
  4537. struct ibmvfc_host *vhost = evt->vhost;
  4538. struct ibmvfc_channel_enquiry *rsp = &evt->xfer_iu->channel_enquiry;
  4539. u32 mad_status = be16_to_cpu(rsp->common.status);
  4540. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4541. switch (mad_status) {
  4542. case IBMVFC_MAD_SUCCESS:
  4543. ibmvfc_dbg(vhost, "Channel Enquiry succeeded\n");
  4544. vhost->max_vios_scsi_channels = be32_to_cpu(rsp->num_scsi_subq_channels);
  4545. ibmvfc_free_event(evt);
  4546. break;
  4547. case IBMVFC_MAD_FAILED:
  4548. level += ibmvfc_retry_host_init(vhost);
  4549. ibmvfc_log(vhost, level, "Channel Enquiry failed\n");
  4550. fallthrough;
  4551. case IBMVFC_MAD_DRIVER_FAILED:
  4552. ibmvfc_free_event(evt);
  4553. return;
  4554. default:
  4555. dev_err(vhost->dev, "Invalid Channel Enquiry response: 0x%x\n",
  4556. mad_status);
  4557. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4558. ibmvfc_free_event(evt);
  4559. return;
  4560. }
  4561. ibmvfc_channel_setup(vhost);
  4562. }
  4563. static void ibmvfc_channel_enquiry(struct ibmvfc_host *vhost)
  4564. {
  4565. struct ibmvfc_channel_enquiry *mad;
  4566. struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
  4567. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4568. if (!evt) {
  4569. ibmvfc_log(vhost, level, "Channel Enquiry failed: no available events\n");
  4570. ibmvfc_hard_reset_host(vhost);
  4571. return;
  4572. }
  4573. ibmvfc_init_event(evt, ibmvfc_channel_enquiry_done, IBMVFC_MAD_FORMAT);
  4574. mad = &evt->iu.channel_enquiry;
  4575. memset(mad, 0, sizeof(*mad));
  4576. mad->common.version = cpu_to_be32(1);
  4577. mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_ENQUIRY);
  4578. mad->common.length = cpu_to_be16(sizeof(*mad));
  4579. if (mig_channels_only)
  4580. mad->flags |= cpu_to_be32(IBMVFC_NO_CHANNELS_TO_CRQ_SUPPORT);
  4581. if (mig_no_less_channels)
  4582. mad->flags |= cpu_to_be32(IBMVFC_NO_N_TO_M_CHANNELS_SUPPORT);
  4583. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  4584. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  4585. ibmvfc_dbg(vhost, "Send channel enquiry\n");
  4586. else
  4587. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4588. }
  4589. /**
  4590. * ibmvfc_npiv_login_done - Completion handler for NPIV Login
  4591. * @evt: ibmvfc event struct
  4592. *
  4593. **/
  4594. static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
  4595. {
  4596. struct ibmvfc_host *vhost = evt->vhost;
  4597. u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
  4598. struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
  4599. unsigned int npiv_max_sectors;
  4600. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4601. switch (mad_status) {
  4602. case IBMVFC_MAD_SUCCESS:
  4603. ibmvfc_free_event(evt);
  4604. break;
  4605. case IBMVFC_MAD_FAILED:
  4606. if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  4607. level += ibmvfc_retry_host_init(vhost);
  4608. else
  4609. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4610. ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
  4611. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  4612. be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
  4613. ibmvfc_free_event(evt);
  4614. return;
  4615. case IBMVFC_MAD_CRQ_ERROR:
  4616. ibmvfc_retry_host_init(vhost);
  4617. fallthrough;
  4618. case IBMVFC_MAD_DRIVER_FAILED:
  4619. ibmvfc_free_event(evt);
  4620. return;
  4621. default:
  4622. dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
  4623. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4624. ibmvfc_free_event(evt);
  4625. return;
  4626. }
  4627. vhost->client_migrated = 0;
  4628. if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
  4629. dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
  4630. rsp->flags);
  4631. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4632. wake_up(&vhost->work_wait_q);
  4633. return;
  4634. }
  4635. if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
  4636. dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
  4637. rsp->max_cmds);
  4638. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4639. wake_up(&vhost->work_wait_q);
  4640. return;
  4641. }
  4642. vhost->logged_in = 1;
  4643. npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), max_sectors);
  4644. dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
  4645. rsp->partition_name, rsp->device_name, rsp->port_loc_code,
  4646. rsp->drc_name, npiv_max_sectors);
  4647. fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
  4648. fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
  4649. fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
  4650. fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
  4651. fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
  4652. fc_host_supported_classes(vhost->host) = 0;
  4653. if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
  4654. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
  4655. if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
  4656. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
  4657. if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
  4658. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
  4659. fc_host_maxframe_size(vhost->host) =
  4660. be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
  4661. vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
  4662. vhost->host->max_sectors = npiv_max_sectors;
  4663. if (ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPORT_CHANNELS) && vhost->do_enquiry) {
  4664. ibmvfc_channel_enquiry(vhost);
  4665. } else {
  4666. vhost->do_enquiry = 0;
  4667. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  4668. wake_up(&vhost->work_wait_q);
  4669. }
  4670. }
  4671. /**
  4672. * ibmvfc_npiv_login - Sends NPIV login
  4673. * @vhost: ibmvfc host struct
  4674. *
  4675. **/
  4676. static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
  4677. {
  4678. struct ibmvfc_npiv_login_mad *mad;
  4679. struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
  4680. if (!evt) {
  4681. ibmvfc_dbg(vhost, "NPIV Login failed: no available events\n");
  4682. ibmvfc_hard_reset_host(vhost);
  4683. return;
  4684. }
  4685. ibmvfc_gather_partition_info(vhost);
  4686. ibmvfc_set_login_info(vhost);
  4687. ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
  4688. memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
  4689. mad = &evt->iu.npiv_login;
  4690. memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
  4691. mad->common.version = cpu_to_be32(1);
  4692. mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
  4693. mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
  4694. mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
  4695. mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
  4696. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  4697. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  4698. ibmvfc_dbg(vhost, "Sent NPIV login\n");
  4699. else
  4700. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4701. }
  4702. /**
  4703. * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
  4704. * @evt: ibmvfc event struct
  4705. *
  4706. **/
  4707. static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
  4708. {
  4709. struct ibmvfc_host *vhost = evt->vhost;
  4710. u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
  4711. ibmvfc_free_event(evt);
  4712. switch (mad_status) {
  4713. case IBMVFC_MAD_SUCCESS:
  4714. if (list_empty(&vhost->crq.sent) &&
  4715. vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
  4716. ibmvfc_init_host(vhost);
  4717. return;
  4718. }
  4719. break;
  4720. case IBMVFC_MAD_FAILED:
  4721. case IBMVFC_MAD_NOT_SUPPORTED:
  4722. case IBMVFC_MAD_CRQ_ERROR:
  4723. case IBMVFC_MAD_DRIVER_FAILED:
  4724. default:
  4725. ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
  4726. break;
  4727. }
  4728. ibmvfc_hard_reset_host(vhost);
  4729. }
  4730. /**
  4731. * ibmvfc_npiv_logout - Issue an NPIV Logout
  4732. * @vhost: ibmvfc host struct
  4733. *
  4734. **/
  4735. static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
  4736. {
  4737. struct ibmvfc_npiv_logout_mad *mad;
  4738. struct ibmvfc_event *evt;
  4739. evt = ibmvfc_get_reserved_event(&vhost->crq);
  4740. if (!evt) {
  4741. ibmvfc_dbg(vhost, "NPIV Logout failed: no available events\n");
  4742. ibmvfc_hard_reset_host(vhost);
  4743. return;
  4744. }
  4745. ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
  4746. mad = &evt->iu.npiv_logout;
  4747. memset(mad, 0, sizeof(*mad));
  4748. mad->common.version = cpu_to_be32(1);
  4749. mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
  4750. mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
  4751. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
  4752. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  4753. ibmvfc_dbg(vhost, "Sent NPIV logout\n");
  4754. else
  4755. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4756. }
  4757. /**
  4758. * ibmvfc_dev_init_to_do - Is there target initialization work to do?
  4759. * @vhost: ibmvfc host struct
  4760. *
  4761. * Returns:
  4762. * 1 if work to do / 0 if not
  4763. **/
  4764. static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
  4765. {
  4766. struct ibmvfc_target *tgt;
  4767. list_for_each_entry(tgt, &vhost->targets, queue) {
  4768. if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
  4769. tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
  4770. return 1;
  4771. }
  4772. return 0;
  4773. }
  4774. /**
  4775. * ibmvfc_dev_logo_to_do - Is there target logout work to do?
  4776. * @vhost: ibmvfc host struct
  4777. *
  4778. * Returns:
  4779. * 1 if work to do / 0 if not
  4780. **/
  4781. static int ibmvfc_dev_logo_to_do(struct ibmvfc_host *vhost)
  4782. {
  4783. struct ibmvfc_target *tgt;
  4784. list_for_each_entry(tgt, &vhost->targets, queue) {
  4785. if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
  4786. tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
  4787. return 1;
  4788. }
  4789. return 0;
  4790. }
  4791. /**
  4792. * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
  4793. * @vhost: ibmvfc host struct
  4794. *
  4795. * Returns:
  4796. * 1 if work to do / 0 if not
  4797. **/
  4798. static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
  4799. {
  4800. struct ibmvfc_target *tgt;
  4801. if (kthread_should_stop())
  4802. return 1;
  4803. switch (vhost->action) {
  4804. case IBMVFC_HOST_ACTION_NONE:
  4805. case IBMVFC_HOST_ACTION_INIT_WAIT:
  4806. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  4807. return 0;
  4808. case IBMVFC_HOST_ACTION_TGT_INIT:
  4809. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  4810. if (vhost->discovery_threads == disc_threads)
  4811. return 0;
  4812. list_for_each_entry(tgt, &vhost->targets, queue)
  4813. if (tgt->action == IBMVFC_TGT_ACTION_INIT)
  4814. return 1;
  4815. list_for_each_entry(tgt, &vhost->targets, queue)
  4816. if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
  4817. return 0;
  4818. return 1;
  4819. case IBMVFC_HOST_ACTION_TGT_DEL:
  4820. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  4821. if (vhost->discovery_threads == disc_threads)
  4822. return 0;
  4823. list_for_each_entry(tgt, &vhost->targets, queue)
  4824. if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
  4825. return 1;
  4826. list_for_each_entry(tgt, &vhost->targets, queue)
  4827. if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
  4828. return 0;
  4829. return 1;
  4830. case IBMVFC_HOST_ACTION_LOGO:
  4831. case IBMVFC_HOST_ACTION_INIT:
  4832. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  4833. case IBMVFC_HOST_ACTION_QUERY:
  4834. case IBMVFC_HOST_ACTION_RESET:
  4835. case IBMVFC_HOST_ACTION_REENABLE:
  4836. default:
  4837. break;
  4838. }
  4839. return 1;
  4840. }
  4841. /**
  4842. * ibmvfc_work_to_do - Is there task level work to do?
  4843. * @vhost: ibmvfc host struct
  4844. *
  4845. * Returns:
  4846. * 1 if work to do / 0 if not
  4847. **/
  4848. static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
  4849. {
  4850. unsigned long flags;
  4851. int rc;
  4852. spin_lock_irqsave(vhost->host->host_lock, flags);
  4853. rc = __ibmvfc_work_to_do(vhost);
  4854. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4855. return rc;
  4856. }
  4857. /**
  4858. * ibmvfc_log_ae - Log async events if necessary
  4859. * @vhost: ibmvfc host struct
  4860. * @events: events to log
  4861. *
  4862. **/
  4863. static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
  4864. {
  4865. if (events & IBMVFC_AE_RSCN)
  4866. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
  4867. if ((events & IBMVFC_AE_LINKDOWN) &&
  4868. vhost->state >= IBMVFC_HALTED)
  4869. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
  4870. if ((events & IBMVFC_AE_LINKUP) &&
  4871. vhost->state == IBMVFC_INITIALIZING)
  4872. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
  4873. }
  4874. /**
  4875. * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
  4876. * @tgt: ibmvfc target struct
  4877. *
  4878. **/
  4879. static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
  4880. {
  4881. struct ibmvfc_host *vhost = tgt->vhost;
  4882. struct fc_rport *rport;
  4883. unsigned long flags;
  4884. tgt_dbg(tgt, "Adding rport\n");
  4885. rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
  4886. spin_lock_irqsave(vhost->host->host_lock, flags);
  4887. if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
  4888. tgt_dbg(tgt, "Deleting rport\n");
  4889. list_del(&tgt->queue);
  4890. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
  4891. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4892. fc_remote_port_delete(rport);
  4893. del_timer_sync(&tgt->timer);
  4894. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4895. return;
  4896. } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
  4897. tgt_dbg(tgt, "Deleting rport with outstanding I/O\n");
  4898. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
  4899. tgt->rport = NULL;
  4900. tgt->init_retries = 0;
  4901. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4902. fc_remote_port_delete(rport);
  4903. return;
  4904. } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
  4905. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4906. return;
  4907. }
  4908. if (rport) {
  4909. tgt_dbg(tgt, "rport add succeeded\n");
  4910. tgt->rport = rport;
  4911. rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
  4912. rport->supported_classes = 0;
  4913. tgt->target_id = rport->scsi_target_id;
  4914. if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
  4915. rport->supported_classes |= FC_COS_CLASS1;
  4916. if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
  4917. rport->supported_classes |= FC_COS_CLASS2;
  4918. if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
  4919. rport->supported_classes |= FC_COS_CLASS3;
  4920. } else
  4921. tgt_dbg(tgt, "rport add failed\n");
  4922. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4923. }
  4924. /**
  4925. * ibmvfc_do_work - Do task level work
  4926. * @vhost: ibmvfc host struct
  4927. *
  4928. **/
  4929. static void ibmvfc_do_work(struct ibmvfc_host *vhost)
  4930. {
  4931. struct ibmvfc_target *tgt;
  4932. unsigned long flags;
  4933. struct fc_rport *rport;
  4934. LIST_HEAD(purge);
  4935. int rc;
  4936. ibmvfc_log_ae(vhost, vhost->events_to_log);
  4937. spin_lock_irqsave(vhost->host->host_lock, flags);
  4938. vhost->events_to_log = 0;
  4939. switch (vhost->action) {
  4940. case IBMVFC_HOST_ACTION_NONE:
  4941. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  4942. case IBMVFC_HOST_ACTION_INIT_WAIT:
  4943. break;
  4944. case IBMVFC_HOST_ACTION_RESET:
  4945. list_splice_init(&vhost->purge, &purge);
  4946. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4947. ibmvfc_complete_purge(&purge);
  4948. rc = ibmvfc_reset_crq(vhost);
  4949. spin_lock_irqsave(vhost->host->host_lock, flags);
  4950. if (!rc || rc == H_CLOSED)
  4951. vio_enable_interrupts(to_vio_dev(vhost->dev));
  4952. if (vhost->action == IBMVFC_HOST_ACTION_RESET) {
  4953. /*
  4954. * The only action we could have changed to would have
  4955. * been reenable, in which case, we skip the rest of
  4956. * this path and wait until we've done the re-enable
  4957. * before sending the crq init.
  4958. */
  4959. vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
  4960. if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
  4961. (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
  4962. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4963. dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
  4964. }
  4965. }
  4966. break;
  4967. case IBMVFC_HOST_ACTION_REENABLE:
  4968. list_splice_init(&vhost->purge, &purge);
  4969. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4970. ibmvfc_complete_purge(&purge);
  4971. rc = ibmvfc_reenable_crq_queue(vhost);
  4972. spin_lock_irqsave(vhost->host->host_lock, flags);
  4973. if (vhost->action == IBMVFC_HOST_ACTION_REENABLE) {
  4974. /*
  4975. * The only action we could have changed to would have
  4976. * been reset, in which case, we skip the rest of this
  4977. * path and wait until we've done the reset before
  4978. * sending the crq init.
  4979. */
  4980. vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
  4981. if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
  4982. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4983. dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
  4984. }
  4985. }
  4986. break;
  4987. case IBMVFC_HOST_ACTION_LOGO:
  4988. vhost->job_step(vhost);
  4989. break;
  4990. case IBMVFC_HOST_ACTION_INIT:
  4991. BUG_ON(vhost->state != IBMVFC_INITIALIZING);
  4992. if (vhost->delay_init) {
  4993. vhost->delay_init = 0;
  4994. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4995. ssleep(15);
  4996. return;
  4997. } else
  4998. vhost->job_step(vhost);
  4999. break;
  5000. case IBMVFC_HOST_ACTION_QUERY:
  5001. list_for_each_entry(tgt, &vhost->targets, queue)
  5002. ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
  5003. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
  5004. break;
  5005. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  5006. list_for_each_entry(tgt, &vhost->targets, queue) {
  5007. if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
  5008. tgt->job_step(tgt);
  5009. break;
  5010. }
  5011. }
  5012. if (!ibmvfc_dev_init_to_do(vhost))
  5013. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
  5014. break;
  5015. case IBMVFC_HOST_ACTION_TGT_DEL:
  5016. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  5017. list_for_each_entry(tgt, &vhost->targets, queue) {
  5018. if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
  5019. tgt->job_step(tgt);
  5020. break;
  5021. }
  5022. }
  5023. if (ibmvfc_dev_logo_to_do(vhost)) {
  5024. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5025. return;
  5026. }
  5027. list_for_each_entry(tgt, &vhost->targets, queue) {
  5028. if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
  5029. tgt_dbg(tgt, "Deleting rport\n");
  5030. rport = tgt->rport;
  5031. tgt->rport = NULL;
  5032. list_del(&tgt->queue);
  5033. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
  5034. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5035. if (rport)
  5036. fc_remote_port_delete(rport);
  5037. del_timer_sync(&tgt->timer);
  5038. kref_put(&tgt->kref, ibmvfc_release_tgt);
  5039. return;
  5040. } else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
  5041. tgt_dbg(tgt, "Deleting rport with I/O outstanding\n");
  5042. rport = tgt->rport;
  5043. tgt->rport = NULL;
  5044. tgt->init_retries = 0;
  5045. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
  5046. /*
  5047. * If fast fail is enabled, we wait for it to fire and then clean up
  5048. * the old port, since we expect the fast fail timer to clean up the
  5049. * outstanding I/O faster than waiting for normal command timeouts.
  5050. * However, if fast fail is disabled, any I/O outstanding to the
  5051. * rport LUNs will stay outstanding indefinitely, since the EH handlers
  5052. * won't get invoked for I/O's timing out. If this is a NPIV failover
  5053. * scenario, the better alternative is to use the move login.
  5054. */
  5055. if (rport && rport->fast_io_fail_tmo == -1)
  5056. tgt->move_login = 1;
  5057. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5058. if (rport)
  5059. fc_remote_port_delete(rport);
  5060. return;
  5061. }
  5062. }
  5063. if (vhost->state == IBMVFC_INITIALIZING) {
  5064. if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
  5065. if (vhost->reinit) {
  5066. vhost->reinit = 0;
  5067. scsi_block_requests(vhost->host);
  5068. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  5069. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5070. } else {
  5071. ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
  5072. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  5073. wake_up(&vhost->init_wait_q);
  5074. schedule_work(&vhost->rport_add_work_q);
  5075. vhost->init_retries = 0;
  5076. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5077. scsi_unblock_requests(vhost->host);
  5078. }
  5079. return;
  5080. } else {
  5081. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  5082. vhost->job_step = ibmvfc_discover_targets;
  5083. }
  5084. } else {
  5085. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  5086. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5087. scsi_unblock_requests(vhost->host);
  5088. wake_up(&vhost->init_wait_q);
  5089. return;
  5090. }
  5091. break;
  5092. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  5093. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
  5094. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5095. ibmvfc_alloc_targets(vhost);
  5096. spin_lock_irqsave(vhost->host->host_lock, flags);
  5097. break;
  5098. case IBMVFC_HOST_ACTION_TGT_INIT:
  5099. list_for_each_entry(tgt, &vhost->targets, queue) {
  5100. if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
  5101. tgt->job_step(tgt);
  5102. break;
  5103. }
  5104. }
  5105. if (!ibmvfc_dev_init_to_do(vhost))
  5106. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
  5107. break;
  5108. default:
  5109. break;
  5110. }
  5111. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5112. }
  5113. /**
  5114. * ibmvfc_work - Do task level work
  5115. * @data: ibmvfc host struct
  5116. *
  5117. * Returns:
  5118. * zero
  5119. **/
  5120. static int ibmvfc_work(void *data)
  5121. {
  5122. struct ibmvfc_host *vhost = data;
  5123. int rc;
  5124. set_user_nice(current, MIN_NICE);
  5125. while (1) {
  5126. rc = wait_event_interruptible(vhost->work_wait_q,
  5127. ibmvfc_work_to_do(vhost));
  5128. BUG_ON(rc);
  5129. if (kthread_should_stop())
  5130. break;
  5131. ibmvfc_do_work(vhost);
  5132. }
  5133. ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
  5134. return 0;
  5135. }
  5136. /**
  5137. * ibmvfc_alloc_queue - Allocate queue
  5138. * @vhost: ibmvfc host struct
  5139. * @queue: ibmvfc queue to allocate
  5140. * @fmt: queue format to allocate
  5141. *
  5142. * Returns:
  5143. * 0 on success / non-zero on failure
  5144. **/
  5145. static int ibmvfc_alloc_queue(struct ibmvfc_host *vhost,
  5146. struct ibmvfc_queue *queue,
  5147. enum ibmvfc_msg_fmt fmt)
  5148. {
  5149. struct device *dev = vhost->dev;
  5150. size_t fmt_size;
  5151. ENTER;
  5152. spin_lock_init(&queue->_lock);
  5153. queue->q_lock = &queue->_lock;
  5154. switch (fmt) {
  5155. case IBMVFC_CRQ_FMT:
  5156. fmt_size = sizeof(*queue->msgs.crq);
  5157. queue->total_depth = scsi_qdepth + IBMVFC_NUM_INTERNAL_REQ;
  5158. queue->evt_depth = scsi_qdepth;
  5159. queue->reserved_depth = IBMVFC_NUM_INTERNAL_REQ;
  5160. break;
  5161. case IBMVFC_ASYNC_FMT:
  5162. fmt_size = sizeof(*queue->msgs.async);
  5163. break;
  5164. case IBMVFC_SUB_CRQ_FMT:
  5165. fmt_size = sizeof(*queue->msgs.scrq);
  5166. /* We need one extra event for Cancel Commands */
  5167. queue->total_depth = scsi_qdepth + IBMVFC_NUM_INTERNAL_SUBQ_REQ;
  5168. queue->evt_depth = scsi_qdepth;
  5169. queue->reserved_depth = IBMVFC_NUM_INTERNAL_SUBQ_REQ;
  5170. break;
  5171. default:
  5172. dev_warn(dev, "Unknown command/response queue message format: %d\n", fmt);
  5173. return -EINVAL;
  5174. }
  5175. queue->fmt = fmt;
  5176. if (ibmvfc_init_event_pool(vhost, queue)) {
  5177. dev_err(dev, "Couldn't initialize event pool.\n");
  5178. return -ENOMEM;
  5179. }
  5180. queue->msgs.handle = (void *)get_zeroed_page(GFP_KERNEL);
  5181. if (!queue->msgs.handle)
  5182. return -ENOMEM;
  5183. queue->msg_token = dma_map_single(dev, queue->msgs.handle, PAGE_SIZE,
  5184. DMA_BIDIRECTIONAL);
  5185. if (dma_mapping_error(dev, queue->msg_token)) {
  5186. free_page((unsigned long)queue->msgs.handle);
  5187. queue->msgs.handle = NULL;
  5188. return -ENOMEM;
  5189. }
  5190. queue->cur = 0;
  5191. queue->size = PAGE_SIZE / fmt_size;
  5192. queue->vhost = vhost;
  5193. return 0;
  5194. }
  5195. /**
  5196. * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
  5197. * @vhost: ibmvfc host struct
  5198. *
  5199. * Allocates a page for messages, maps it for dma, and registers
  5200. * the crq with the hypervisor.
  5201. *
  5202. * Return value:
  5203. * zero on success / other on failure
  5204. **/
  5205. static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
  5206. {
  5207. int rc, retrc = -ENOMEM;
  5208. struct device *dev = vhost->dev;
  5209. struct vio_dev *vdev = to_vio_dev(dev);
  5210. struct ibmvfc_queue *crq = &vhost->crq;
  5211. ENTER;
  5212. if (ibmvfc_alloc_queue(vhost, crq, IBMVFC_CRQ_FMT))
  5213. return -ENOMEM;
  5214. retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  5215. crq->msg_token, PAGE_SIZE);
  5216. if (rc == H_RESOURCE)
  5217. /* maybe kexecing and resource is busy. try a reset */
  5218. retrc = rc = ibmvfc_reset_crq(vhost);
  5219. if (rc == H_CLOSED)
  5220. dev_warn(dev, "Partner adapter not ready\n");
  5221. else if (rc) {
  5222. dev_warn(dev, "Error %d opening adapter\n", rc);
  5223. goto reg_crq_failed;
  5224. }
  5225. retrc = 0;
  5226. tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
  5227. if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
  5228. dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
  5229. goto req_irq_failed;
  5230. }
  5231. if ((rc = vio_enable_interrupts(vdev))) {
  5232. dev_err(dev, "Error %d enabling interrupts\n", rc);
  5233. goto req_irq_failed;
  5234. }
  5235. LEAVE;
  5236. return retrc;
  5237. req_irq_failed:
  5238. tasklet_kill(&vhost->tasklet);
  5239. do {
  5240. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  5241. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  5242. reg_crq_failed:
  5243. ibmvfc_free_queue(vhost, crq);
  5244. return retrc;
  5245. }
  5246. static int ibmvfc_register_channel(struct ibmvfc_host *vhost,
  5247. struct ibmvfc_channels *channels,
  5248. int index)
  5249. {
  5250. struct device *dev = vhost->dev;
  5251. struct vio_dev *vdev = to_vio_dev(dev);
  5252. struct ibmvfc_queue *scrq = &channels->scrqs[index];
  5253. int rc = -ENOMEM;
  5254. ENTER;
  5255. rc = h_reg_sub_crq(vdev->unit_address, scrq->msg_token, PAGE_SIZE,
  5256. &scrq->cookie, &scrq->hw_irq);
  5257. /* H_CLOSED indicates successful register, but no CRQ partner */
  5258. if (rc && rc != H_CLOSED) {
  5259. dev_warn(dev, "Error registering sub-crq: %d\n", rc);
  5260. if (rc == H_PARAMETER)
  5261. dev_warn_once(dev, "Firmware may not support MQ\n");
  5262. goto reg_failed;
  5263. }
  5264. scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
  5265. if (!scrq->irq) {
  5266. rc = -EINVAL;
  5267. dev_err(dev, "Error mapping sub-crq[%d] irq\n", index);
  5268. goto irq_failed;
  5269. }
  5270. switch (channels->protocol) {
  5271. case IBMVFC_PROTO_SCSI:
  5272. snprintf(scrq->name, sizeof(scrq->name), "ibmvfc-%x-scsi%d",
  5273. vdev->unit_address, index);
  5274. scrq->handler = ibmvfc_interrupt_mq;
  5275. break;
  5276. case IBMVFC_PROTO_NVME:
  5277. snprintf(scrq->name, sizeof(scrq->name), "ibmvfc-%x-nvmf%d",
  5278. vdev->unit_address, index);
  5279. scrq->handler = ibmvfc_interrupt_mq;
  5280. break;
  5281. default:
  5282. dev_err(dev, "Unknown channel protocol (%d)\n",
  5283. channels->protocol);
  5284. goto irq_failed;
  5285. }
  5286. rc = request_irq(scrq->irq, scrq->handler, 0, scrq->name, scrq);
  5287. if (rc) {
  5288. dev_err(dev, "Couldn't register sub-crq[%d] irq\n", index);
  5289. irq_dispose_mapping(scrq->irq);
  5290. goto irq_failed;
  5291. }
  5292. scrq->hwq_id = index;
  5293. LEAVE;
  5294. return 0;
  5295. irq_failed:
  5296. do {
  5297. rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address, scrq->cookie);
  5298. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  5299. reg_failed:
  5300. LEAVE;
  5301. return rc;
  5302. }
  5303. static void ibmvfc_deregister_channel(struct ibmvfc_host *vhost,
  5304. struct ibmvfc_channels *channels,
  5305. int index)
  5306. {
  5307. struct device *dev = vhost->dev;
  5308. struct vio_dev *vdev = to_vio_dev(dev);
  5309. struct ibmvfc_queue *scrq = &channels->scrqs[index];
  5310. long rc;
  5311. ENTER;
  5312. free_irq(scrq->irq, scrq);
  5313. irq_dispose_mapping(scrq->irq);
  5314. scrq->irq = 0;
  5315. do {
  5316. rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address,
  5317. scrq->cookie);
  5318. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  5319. if (rc)
  5320. dev_err(dev, "Failed to free sub-crq[%d]: rc=%ld\n", index, rc);
  5321. /* Clean out the queue */
  5322. memset(scrq->msgs.crq, 0, PAGE_SIZE);
  5323. scrq->cur = 0;
  5324. LEAVE;
  5325. }
  5326. static void ibmvfc_reg_sub_crqs(struct ibmvfc_host *vhost,
  5327. struct ibmvfc_channels *channels)
  5328. {
  5329. int i, j;
  5330. ENTER;
  5331. if (!vhost->mq_enabled || !channels->scrqs)
  5332. return;
  5333. for (i = 0; i < channels->max_queues; i++) {
  5334. if (ibmvfc_register_channel(vhost, channels, i)) {
  5335. for (j = i; j > 0; j--)
  5336. ibmvfc_deregister_channel(vhost, channels, j - 1);
  5337. vhost->do_enquiry = 0;
  5338. return;
  5339. }
  5340. }
  5341. LEAVE;
  5342. }
  5343. static void ibmvfc_dereg_sub_crqs(struct ibmvfc_host *vhost,
  5344. struct ibmvfc_channels *channels)
  5345. {
  5346. int i;
  5347. ENTER;
  5348. if (!vhost->mq_enabled || !channels->scrqs)
  5349. return;
  5350. for (i = 0; i < channels->max_queues; i++)
  5351. ibmvfc_deregister_channel(vhost, channels, i);
  5352. LEAVE;
  5353. }
  5354. static int ibmvfc_alloc_channels(struct ibmvfc_host *vhost,
  5355. struct ibmvfc_channels *channels)
  5356. {
  5357. struct ibmvfc_queue *scrq;
  5358. int i, j;
  5359. int rc = 0;
  5360. channels->scrqs = kcalloc(channels->max_queues,
  5361. sizeof(*channels->scrqs),
  5362. GFP_KERNEL);
  5363. if (!channels->scrqs)
  5364. return -ENOMEM;
  5365. for (i = 0; i < channels->max_queues; i++) {
  5366. scrq = &channels->scrqs[i];
  5367. rc = ibmvfc_alloc_queue(vhost, scrq, IBMVFC_SUB_CRQ_FMT);
  5368. if (rc) {
  5369. for (j = i; j > 0; j--) {
  5370. scrq = &channels->scrqs[j - 1];
  5371. ibmvfc_free_queue(vhost, scrq);
  5372. }
  5373. kfree(channels->scrqs);
  5374. channels->scrqs = NULL;
  5375. channels->active_queues = 0;
  5376. return rc;
  5377. }
  5378. }
  5379. return rc;
  5380. }
  5381. static void ibmvfc_init_sub_crqs(struct ibmvfc_host *vhost)
  5382. {
  5383. ENTER;
  5384. if (!vhost->mq_enabled)
  5385. return;
  5386. if (ibmvfc_alloc_channels(vhost, &vhost->scsi_scrqs)) {
  5387. vhost->do_enquiry = 0;
  5388. vhost->mq_enabled = 0;
  5389. return;
  5390. }
  5391. ibmvfc_reg_sub_crqs(vhost, &vhost->scsi_scrqs);
  5392. LEAVE;
  5393. }
  5394. static void ibmvfc_release_channels(struct ibmvfc_host *vhost,
  5395. struct ibmvfc_channels *channels)
  5396. {
  5397. struct ibmvfc_queue *scrq;
  5398. int i;
  5399. if (channels->scrqs) {
  5400. for (i = 0; i < channels->max_queues; i++) {
  5401. scrq = &channels->scrqs[i];
  5402. ibmvfc_free_queue(vhost, scrq);
  5403. }
  5404. kfree(channels->scrqs);
  5405. channels->scrqs = NULL;
  5406. channels->active_queues = 0;
  5407. }
  5408. }
  5409. static void ibmvfc_release_sub_crqs(struct ibmvfc_host *vhost)
  5410. {
  5411. ENTER;
  5412. if (!vhost->scsi_scrqs.scrqs)
  5413. return;
  5414. ibmvfc_dereg_sub_crqs(vhost, &vhost->scsi_scrqs);
  5415. ibmvfc_release_channels(vhost, &vhost->scsi_scrqs);
  5416. LEAVE;
  5417. }
  5418. static void ibmvfc_free_disc_buf(struct device *dev, struct ibmvfc_channels *channels)
  5419. {
  5420. dma_free_coherent(dev, channels->disc_buf_sz, channels->disc_buf,
  5421. channels->disc_buf_dma);
  5422. }
  5423. /**
  5424. * ibmvfc_free_mem - Free memory for vhost
  5425. * @vhost: ibmvfc host struct
  5426. *
  5427. * Return value:
  5428. * none
  5429. **/
  5430. static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
  5431. {
  5432. struct ibmvfc_queue *async_q = &vhost->async_crq;
  5433. ENTER;
  5434. mempool_destroy(vhost->tgt_pool);
  5435. kfree(vhost->trace);
  5436. ibmvfc_free_disc_buf(vhost->dev, &vhost->scsi_scrqs);
  5437. dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
  5438. vhost->login_buf, vhost->login_buf_dma);
  5439. dma_free_coherent(vhost->dev, sizeof(*vhost->channel_setup_buf),
  5440. vhost->channel_setup_buf, vhost->channel_setup_dma);
  5441. dma_pool_destroy(vhost->sg_pool);
  5442. ibmvfc_free_queue(vhost, async_q);
  5443. LEAVE;
  5444. }
  5445. static int ibmvfc_alloc_disc_buf(struct device *dev, struct ibmvfc_channels *channels)
  5446. {
  5447. channels->disc_buf_sz = sizeof(*channels->disc_buf) * max_targets;
  5448. channels->disc_buf = dma_alloc_coherent(dev, channels->disc_buf_sz,
  5449. &channels->disc_buf_dma, GFP_KERNEL);
  5450. if (!channels->disc_buf) {
  5451. dev_err(dev, "Couldn't allocate %s Discover Targets buffer\n",
  5452. (channels->protocol == IBMVFC_PROTO_SCSI) ? "SCSI" : "NVMe");
  5453. return -ENOMEM;
  5454. }
  5455. return 0;
  5456. }
  5457. /**
  5458. * ibmvfc_alloc_mem - Allocate memory for vhost
  5459. * @vhost: ibmvfc host struct
  5460. *
  5461. * Return value:
  5462. * 0 on success / non-zero on failure
  5463. **/
  5464. static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
  5465. {
  5466. struct ibmvfc_queue *async_q = &vhost->async_crq;
  5467. struct device *dev = vhost->dev;
  5468. ENTER;
  5469. if (ibmvfc_alloc_queue(vhost, async_q, IBMVFC_ASYNC_FMT)) {
  5470. dev_err(dev, "Couldn't allocate/map async queue.\n");
  5471. goto nomem;
  5472. }
  5473. vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
  5474. SG_ALL * sizeof(struct srp_direct_buf),
  5475. sizeof(struct srp_direct_buf), 0);
  5476. if (!vhost->sg_pool) {
  5477. dev_err(dev, "Failed to allocate sg pool\n");
  5478. goto unmap_async_crq;
  5479. }
  5480. vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
  5481. &vhost->login_buf_dma, GFP_KERNEL);
  5482. if (!vhost->login_buf) {
  5483. dev_err(dev, "Couldn't allocate NPIV login buffer\n");
  5484. goto free_sg_pool;
  5485. }
  5486. if (ibmvfc_alloc_disc_buf(dev, &vhost->scsi_scrqs))
  5487. goto free_login_buffer;
  5488. vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
  5489. sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
  5490. atomic_set(&vhost->trace_index, -1);
  5491. if (!vhost->trace)
  5492. goto free_scsi_disc_buffer;
  5493. vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
  5494. sizeof(struct ibmvfc_target));
  5495. if (!vhost->tgt_pool) {
  5496. dev_err(dev, "Couldn't allocate target memory pool\n");
  5497. goto free_trace;
  5498. }
  5499. vhost->channel_setup_buf = dma_alloc_coherent(dev, sizeof(*vhost->channel_setup_buf),
  5500. &vhost->channel_setup_dma,
  5501. GFP_KERNEL);
  5502. if (!vhost->channel_setup_buf) {
  5503. dev_err(dev, "Couldn't allocate Channel Setup buffer\n");
  5504. goto free_tgt_pool;
  5505. }
  5506. LEAVE;
  5507. return 0;
  5508. free_tgt_pool:
  5509. mempool_destroy(vhost->tgt_pool);
  5510. free_trace:
  5511. kfree(vhost->trace);
  5512. free_scsi_disc_buffer:
  5513. ibmvfc_free_disc_buf(dev, &vhost->scsi_scrqs);
  5514. free_login_buffer:
  5515. dma_free_coherent(dev, sizeof(*vhost->login_buf),
  5516. vhost->login_buf, vhost->login_buf_dma);
  5517. free_sg_pool:
  5518. dma_pool_destroy(vhost->sg_pool);
  5519. unmap_async_crq:
  5520. ibmvfc_free_queue(vhost, async_q);
  5521. nomem:
  5522. LEAVE;
  5523. return -ENOMEM;
  5524. }
  5525. /**
  5526. * ibmvfc_rport_add_thread - Worker thread for rport adds
  5527. * @work: work struct
  5528. *
  5529. **/
  5530. static void ibmvfc_rport_add_thread(struct work_struct *work)
  5531. {
  5532. struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
  5533. rport_add_work_q);
  5534. struct ibmvfc_target *tgt;
  5535. struct fc_rport *rport;
  5536. unsigned long flags;
  5537. int did_work;
  5538. ENTER;
  5539. spin_lock_irqsave(vhost->host->host_lock, flags);
  5540. do {
  5541. did_work = 0;
  5542. if (vhost->state != IBMVFC_ACTIVE)
  5543. break;
  5544. list_for_each_entry(tgt, &vhost->targets, queue) {
  5545. if (tgt->add_rport) {
  5546. did_work = 1;
  5547. tgt->add_rport = 0;
  5548. kref_get(&tgt->kref);
  5549. rport = tgt->rport;
  5550. if (!rport) {
  5551. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5552. ibmvfc_tgt_add_rport(tgt);
  5553. } else if (get_device(&rport->dev)) {
  5554. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5555. tgt_dbg(tgt, "Setting rport roles\n");
  5556. fc_remote_port_rolechg(rport, tgt->ids.roles);
  5557. put_device(&rport->dev);
  5558. } else {
  5559. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5560. }
  5561. kref_put(&tgt->kref, ibmvfc_release_tgt);
  5562. spin_lock_irqsave(vhost->host->host_lock, flags);
  5563. break;
  5564. }
  5565. }
  5566. } while(did_work);
  5567. if (vhost->state == IBMVFC_ACTIVE)
  5568. vhost->scan_complete = 1;
  5569. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5570. LEAVE;
  5571. }
  5572. /**
  5573. * ibmvfc_probe - Adapter hot plug add entry point
  5574. * @vdev: vio device struct
  5575. * @id: vio device id struct
  5576. *
  5577. * Return value:
  5578. * 0 on success / non-zero on failure
  5579. **/
  5580. static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  5581. {
  5582. struct ibmvfc_host *vhost;
  5583. struct Scsi_Host *shost;
  5584. struct device *dev = &vdev->dev;
  5585. int rc = -ENOMEM;
  5586. unsigned int online_cpus = num_online_cpus();
  5587. unsigned int max_scsi_queues = min((unsigned int)IBMVFC_MAX_SCSI_QUEUES, online_cpus);
  5588. ENTER;
  5589. shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
  5590. if (!shost) {
  5591. dev_err(dev, "Couldn't allocate host data\n");
  5592. goto out;
  5593. }
  5594. shost->transportt = ibmvfc_transport_template;
  5595. shost->can_queue = scsi_qdepth;
  5596. shost->max_lun = max_lun;
  5597. shost->max_id = max_targets;
  5598. shost->max_sectors = max_sectors;
  5599. shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
  5600. shost->unique_id = shost->host_no;
  5601. shost->nr_hw_queues = mq_enabled ? min(max_scsi_queues, nr_scsi_hw_queues) : 1;
  5602. vhost = shost_priv(shost);
  5603. INIT_LIST_HEAD(&vhost->targets);
  5604. INIT_LIST_HEAD(&vhost->purge);
  5605. sprintf(vhost->name, IBMVFC_NAME);
  5606. vhost->host = shost;
  5607. vhost->dev = dev;
  5608. vhost->partition_number = -1;
  5609. vhost->log_level = log_level;
  5610. vhost->task_set = 1;
  5611. vhost->mq_enabled = mq_enabled;
  5612. vhost->scsi_scrqs.desired_queues = min(shost->nr_hw_queues, nr_scsi_channels);
  5613. vhost->scsi_scrqs.max_queues = shost->nr_hw_queues;
  5614. vhost->scsi_scrqs.protocol = IBMVFC_PROTO_SCSI;
  5615. vhost->using_channels = 0;
  5616. vhost->do_enquiry = 1;
  5617. vhost->scan_timeout = 0;
  5618. strcpy(vhost->partition_name, "UNKNOWN");
  5619. init_waitqueue_head(&vhost->work_wait_q);
  5620. init_waitqueue_head(&vhost->init_wait_q);
  5621. INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
  5622. mutex_init(&vhost->passthru_mutex);
  5623. if ((rc = ibmvfc_alloc_mem(vhost)))
  5624. goto free_scsi_host;
  5625. vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
  5626. shost->host_no);
  5627. if (IS_ERR(vhost->work_thread)) {
  5628. dev_err(dev, "Couldn't create kernel thread: %ld\n",
  5629. PTR_ERR(vhost->work_thread));
  5630. rc = PTR_ERR(vhost->work_thread);
  5631. goto free_host_mem;
  5632. }
  5633. if ((rc = ibmvfc_init_crq(vhost))) {
  5634. dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
  5635. goto kill_kthread;
  5636. }
  5637. if ((rc = scsi_add_host(shost, dev)))
  5638. goto release_crq;
  5639. fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
  5640. if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
  5641. &ibmvfc_trace_attr))) {
  5642. dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
  5643. goto remove_shost;
  5644. }
  5645. ibmvfc_init_sub_crqs(vhost);
  5646. dev_set_drvdata(dev, vhost);
  5647. spin_lock(&ibmvfc_driver_lock);
  5648. list_add_tail(&vhost->queue, &ibmvfc_head);
  5649. spin_unlock(&ibmvfc_driver_lock);
  5650. ibmvfc_send_crq_init(vhost);
  5651. scsi_scan_host(shost);
  5652. return 0;
  5653. remove_shost:
  5654. scsi_remove_host(shost);
  5655. release_crq:
  5656. ibmvfc_release_crq_queue(vhost);
  5657. kill_kthread:
  5658. kthread_stop(vhost->work_thread);
  5659. free_host_mem:
  5660. ibmvfc_free_mem(vhost);
  5661. free_scsi_host:
  5662. scsi_host_put(shost);
  5663. out:
  5664. LEAVE;
  5665. return rc;
  5666. }
  5667. /**
  5668. * ibmvfc_remove - Adapter hot plug remove entry point
  5669. * @vdev: vio device struct
  5670. *
  5671. * Return value:
  5672. * 0
  5673. **/
  5674. static void ibmvfc_remove(struct vio_dev *vdev)
  5675. {
  5676. struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
  5677. LIST_HEAD(purge);
  5678. unsigned long flags;
  5679. ENTER;
  5680. ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
  5681. spin_lock_irqsave(vhost->host->host_lock, flags);
  5682. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  5683. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5684. ibmvfc_wait_while_resetting(vhost);
  5685. kthread_stop(vhost->work_thread);
  5686. fc_remove_host(vhost->host);
  5687. scsi_remove_host(vhost->host);
  5688. spin_lock_irqsave(vhost->host->host_lock, flags);
  5689. ibmvfc_purge_requests(vhost, DID_ERROR);
  5690. list_splice_init(&vhost->purge, &purge);
  5691. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5692. ibmvfc_complete_purge(&purge);
  5693. ibmvfc_release_sub_crqs(vhost);
  5694. ibmvfc_release_crq_queue(vhost);
  5695. ibmvfc_free_mem(vhost);
  5696. spin_lock(&ibmvfc_driver_lock);
  5697. list_del(&vhost->queue);
  5698. spin_unlock(&ibmvfc_driver_lock);
  5699. scsi_host_put(vhost->host);
  5700. LEAVE;
  5701. }
  5702. /**
  5703. * ibmvfc_resume - Resume from suspend
  5704. * @dev: device struct
  5705. *
  5706. * We may have lost an interrupt across suspend/resume, so kick the
  5707. * interrupt handler
  5708. *
  5709. */
  5710. static int ibmvfc_resume(struct device *dev)
  5711. {
  5712. unsigned long flags;
  5713. struct ibmvfc_host *vhost = dev_get_drvdata(dev);
  5714. struct vio_dev *vdev = to_vio_dev(dev);
  5715. spin_lock_irqsave(vhost->host->host_lock, flags);
  5716. vio_disable_interrupts(vdev);
  5717. tasklet_schedule(&vhost->tasklet);
  5718. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5719. return 0;
  5720. }
  5721. /**
  5722. * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
  5723. * @vdev: vio device struct
  5724. *
  5725. * Return value:
  5726. * Number of bytes the driver will need to DMA map at the same time in
  5727. * order to perform well.
  5728. */
  5729. static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
  5730. {
  5731. unsigned long pool_dma;
  5732. pool_dma = (IBMVFC_MAX_SCSI_QUEUES * scsi_qdepth) * sizeof(union ibmvfc_iu);
  5733. return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
  5734. }
  5735. static const struct vio_device_id ibmvfc_device_table[] = {
  5736. {"fcp", "IBM,vfc-client"},
  5737. { "", "" }
  5738. };
  5739. MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
  5740. static const struct dev_pm_ops ibmvfc_pm_ops = {
  5741. .resume = ibmvfc_resume
  5742. };
  5743. static struct vio_driver ibmvfc_driver = {
  5744. .id_table = ibmvfc_device_table,
  5745. .probe = ibmvfc_probe,
  5746. .remove = ibmvfc_remove,
  5747. .get_desired_dma = ibmvfc_get_desired_dma,
  5748. .name = IBMVFC_NAME,
  5749. .pm = &ibmvfc_pm_ops,
  5750. };
  5751. static struct fc_function_template ibmvfc_transport_functions = {
  5752. .show_host_fabric_name = 1,
  5753. .show_host_node_name = 1,
  5754. .show_host_port_name = 1,
  5755. .show_host_supported_classes = 1,
  5756. .show_host_port_type = 1,
  5757. .show_host_port_id = 1,
  5758. .show_host_maxframe_size = 1,
  5759. .get_host_port_state = ibmvfc_get_host_port_state,
  5760. .show_host_port_state = 1,
  5761. .get_host_speed = ibmvfc_get_host_speed,
  5762. .show_host_speed = 1,
  5763. .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
  5764. .terminate_rport_io = ibmvfc_terminate_rport_io,
  5765. .show_rport_maxframe_size = 1,
  5766. .show_rport_supported_classes = 1,
  5767. .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
  5768. .show_rport_dev_loss_tmo = 1,
  5769. .get_starget_node_name = ibmvfc_get_starget_node_name,
  5770. .show_starget_node_name = 1,
  5771. .get_starget_port_name = ibmvfc_get_starget_port_name,
  5772. .show_starget_port_name = 1,
  5773. .get_starget_port_id = ibmvfc_get_starget_port_id,
  5774. .show_starget_port_id = 1,
  5775. .max_bsg_segments = 1,
  5776. .bsg_request = ibmvfc_bsg_request,
  5777. .bsg_timeout = ibmvfc_bsg_timeout,
  5778. };
  5779. /**
  5780. * ibmvfc_module_init - Initialize the ibmvfc module
  5781. *
  5782. * Return value:
  5783. * 0 on success / other on failure
  5784. **/
  5785. static int __init ibmvfc_module_init(void)
  5786. {
  5787. int min_max_sectors = PAGE_SIZE >> 9;
  5788. int rc;
  5789. if (!firmware_has_feature(FW_FEATURE_VIO))
  5790. return -ENODEV;
  5791. printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
  5792. IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
  5793. /*
  5794. * Range check the max_sectors module parameter. The upper bounds is
  5795. * implicity checked since the parameter is a ushort.
  5796. */
  5797. if (max_sectors < min_max_sectors) {
  5798. printk(KERN_ERR IBMVFC_NAME ": max_sectors must be at least %d.\n",
  5799. min_max_sectors);
  5800. max_sectors = min_max_sectors;
  5801. }
  5802. ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
  5803. if (!ibmvfc_transport_template)
  5804. return -ENOMEM;
  5805. rc = vio_register_driver(&ibmvfc_driver);
  5806. if (rc)
  5807. fc_release_transport(ibmvfc_transport_template);
  5808. return rc;
  5809. }
  5810. /**
  5811. * ibmvfc_module_exit - Teardown the ibmvfc module
  5812. *
  5813. * Return value:
  5814. * nothing
  5815. **/
  5816. static void __exit ibmvfc_module_exit(void)
  5817. {
  5818. vio_unregister_driver(&ibmvfc_driver);
  5819. fc_release_transport(ibmvfc_transport_template);
  5820. }
  5821. module_init(ibmvfc_module_init);
  5822. module_exit(ibmvfc_module_exit);