ibmvfc.c 181 KB

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