lpfc_hbadisc.c 179 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2011 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/delay.h>
  23. #include <linux/slab.h>
  24. #include <linux/pci.h>
  25. #include <linux/kthread.h>
  26. #include <linux/interrupt.h>
  27. #include <scsi/scsi.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include "lpfc_hw4.h"
  32. #include "lpfc_hw.h"
  33. #include "lpfc_nl.h"
  34. #include "lpfc_disc.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_scsi.h"
  38. #include "lpfc.h"
  39. #include "lpfc_logmsg.h"
  40. #include "lpfc_crtn.h"
  41. #include "lpfc_vport.h"
  42. #include "lpfc_debugfs.h"
  43. /* AlpaArray for assignment of scsid for scan-down and bind_method */
  44. static uint8_t lpfcAlpaArray[] = {
  45. 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
  46. 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
  47. 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
  48. 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
  49. 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
  50. 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
  51. 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
  52. 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
  53. 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
  54. 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
  55. 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
  56. 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
  57. 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
  58. };
  59. static void lpfc_disc_timeout_handler(struct lpfc_vport *);
  60. static void lpfc_disc_flush_list(struct lpfc_vport *vport);
  61. static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
  62. static int lpfc_fcf_inuse(struct lpfc_hba *);
  63. void
  64. lpfc_terminate_rport_io(struct fc_rport *rport)
  65. {
  66. struct lpfc_rport_data *rdata;
  67. struct lpfc_nodelist * ndlp;
  68. struct lpfc_hba *phba;
  69. rdata = rport->dd_data;
  70. ndlp = rdata->pnode;
  71. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  72. if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
  73. printk(KERN_ERR "Cannot find remote node"
  74. " to terminate I/O Data x%x\n",
  75. rport->port_id);
  76. return;
  77. }
  78. phba = ndlp->phba;
  79. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
  80. "rport terminate: sid:x%x did:x%x flg:x%x",
  81. ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
  82. if (ndlp->nlp_sid != NLP_NO_SID) {
  83. lpfc_sli_abort_iocb(ndlp->vport,
  84. &phba->sli.ring[phba->sli.fcp_ring],
  85. ndlp->nlp_sid, 0, LPFC_CTX_TGT);
  86. }
  87. }
  88. /*
  89. * This function will be called when dev_loss_tmo fire.
  90. */
  91. void
  92. lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
  93. {
  94. struct lpfc_rport_data *rdata;
  95. struct lpfc_nodelist * ndlp;
  96. struct lpfc_vport *vport;
  97. struct lpfc_hba *phba;
  98. struct lpfc_work_evt *evtp;
  99. int put_node;
  100. int put_rport;
  101. rdata = rport->dd_data;
  102. ndlp = rdata->pnode;
  103. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  104. return;
  105. vport = ndlp->vport;
  106. phba = vport->phba;
  107. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
  108. "rport devlosscb: sid:x%x did:x%x flg:x%x",
  109. ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
  110. /* Don't defer this if we are in the process of deleting the vport
  111. * or unloading the driver. The unload will cleanup the node
  112. * appropriately we just need to cleanup the ndlp rport info here.
  113. */
  114. if (vport->load_flag & FC_UNLOADING) {
  115. put_node = rdata->pnode != NULL;
  116. put_rport = ndlp->rport != NULL;
  117. rdata->pnode = NULL;
  118. ndlp->rport = NULL;
  119. if (put_node)
  120. lpfc_nlp_put(ndlp);
  121. if (put_rport)
  122. put_device(&rport->dev);
  123. return;
  124. }
  125. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
  126. return;
  127. evtp = &ndlp->dev_loss_evt;
  128. if (!list_empty(&evtp->evt_listp))
  129. return;
  130. spin_lock_irq(&phba->hbalock);
  131. /* We need to hold the node by incrementing the reference
  132. * count until this queued work is done
  133. */
  134. evtp->evt_arg1 = lpfc_nlp_get(ndlp);
  135. if (evtp->evt_arg1) {
  136. evtp->evt = LPFC_EVT_DEV_LOSS;
  137. list_add_tail(&evtp->evt_listp, &phba->work_list);
  138. lpfc_worker_wake_up(phba);
  139. }
  140. spin_unlock_irq(&phba->hbalock);
  141. return;
  142. }
  143. /**
  144. * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
  145. * @ndlp: Pointer to remote node object.
  146. *
  147. * This function is called from the worker thread when devloss timeout timer
  148. * expires. For SLI4 host, this routine shall return 1 when at lease one
  149. * remote node, including this @ndlp, is still in use of FCF; otherwise, this
  150. * routine shall return 0 when there is no remote node is still in use of FCF
  151. * when devloss timeout happened to this @ndlp.
  152. **/
  153. static int
  154. lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
  155. {
  156. struct lpfc_rport_data *rdata;
  157. struct fc_rport *rport;
  158. struct lpfc_vport *vport;
  159. struct lpfc_hba *phba;
  160. uint8_t *name;
  161. int put_node;
  162. int put_rport;
  163. int warn_on = 0;
  164. int fcf_inuse = 0;
  165. rport = ndlp->rport;
  166. if (!rport)
  167. return fcf_inuse;
  168. rdata = rport->dd_data;
  169. name = (uint8_t *) &ndlp->nlp_portname;
  170. vport = ndlp->vport;
  171. phba = vport->phba;
  172. if (phba->sli_rev == LPFC_SLI_REV4)
  173. fcf_inuse = lpfc_fcf_inuse(phba);
  174. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
  175. "rport devlosstmo:did:x%x type:x%x id:x%x",
  176. ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
  177. /* Don't defer this if we are in the process of deleting the vport
  178. * or unloading the driver. The unload will cleanup the node
  179. * appropriately we just need to cleanup the ndlp rport info here.
  180. */
  181. if (vport->load_flag & FC_UNLOADING) {
  182. if (ndlp->nlp_sid != NLP_NO_SID) {
  183. /* flush the target */
  184. lpfc_sli_abort_iocb(vport,
  185. &phba->sli.ring[phba->sli.fcp_ring],
  186. ndlp->nlp_sid, 0, LPFC_CTX_TGT);
  187. }
  188. put_node = rdata->pnode != NULL;
  189. put_rport = ndlp->rport != NULL;
  190. rdata->pnode = NULL;
  191. ndlp->rport = NULL;
  192. if (put_node)
  193. lpfc_nlp_put(ndlp);
  194. if (put_rport)
  195. put_device(&rport->dev);
  196. return fcf_inuse;
  197. }
  198. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
  199. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  200. "0284 Devloss timeout Ignored on "
  201. "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
  202. "NPort x%x\n",
  203. *name, *(name+1), *(name+2), *(name+3),
  204. *(name+4), *(name+5), *(name+6), *(name+7),
  205. ndlp->nlp_DID);
  206. return fcf_inuse;
  207. }
  208. if (ndlp->nlp_type & NLP_FABRIC) {
  209. /* We will clean up these Nodes in linkup */
  210. put_node = rdata->pnode != NULL;
  211. put_rport = ndlp->rport != NULL;
  212. rdata->pnode = NULL;
  213. ndlp->rport = NULL;
  214. if (put_node)
  215. lpfc_nlp_put(ndlp);
  216. if (put_rport)
  217. put_device(&rport->dev);
  218. return fcf_inuse;
  219. }
  220. if (ndlp->nlp_sid != NLP_NO_SID) {
  221. warn_on = 1;
  222. /* flush the target */
  223. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  224. ndlp->nlp_sid, 0, LPFC_CTX_TGT);
  225. }
  226. if (warn_on) {
  227. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  228. "0203 Devloss timeout on "
  229. "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
  230. "NPort x%06x Data: x%x x%x x%x\n",
  231. *name, *(name+1), *(name+2), *(name+3),
  232. *(name+4), *(name+5), *(name+6), *(name+7),
  233. ndlp->nlp_DID, ndlp->nlp_flag,
  234. ndlp->nlp_state, ndlp->nlp_rpi);
  235. } else {
  236. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  237. "0204 Devloss timeout on "
  238. "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
  239. "NPort x%06x Data: x%x x%x x%x\n",
  240. *name, *(name+1), *(name+2), *(name+3),
  241. *(name+4), *(name+5), *(name+6), *(name+7),
  242. ndlp->nlp_DID, ndlp->nlp_flag,
  243. ndlp->nlp_state, ndlp->nlp_rpi);
  244. }
  245. put_node = rdata->pnode != NULL;
  246. put_rport = ndlp->rport != NULL;
  247. rdata->pnode = NULL;
  248. ndlp->rport = NULL;
  249. if (put_node)
  250. lpfc_nlp_put(ndlp);
  251. if (put_rport)
  252. put_device(&rport->dev);
  253. if (!(vport->load_flag & FC_UNLOADING) &&
  254. !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
  255. !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
  256. (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
  257. (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
  258. (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
  259. lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
  260. return fcf_inuse;
  261. }
  262. /**
  263. * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
  264. * @phba: Pointer to hba context object.
  265. * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
  266. * @nlp_did: remote node identifer with devloss timeout.
  267. *
  268. * This function is called from the worker thread after invoking devloss
  269. * timeout handler and releasing the reference count for the ndlp with
  270. * which the devloss timeout was handled for SLI4 host. For the devloss
  271. * timeout of the last remote node which had been in use of FCF, when this
  272. * routine is invoked, it shall be guaranteed that none of the remote are
  273. * in-use of FCF. When devloss timeout to the last remote using the FCF,
  274. * if the FIP engine is neither in FCF table scan process nor roundrobin
  275. * failover process, the in-use FCF shall be unregistered. If the FIP
  276. * engine is in FCF discovery process, the devloss timeout state shall
  277. * be set for either the FCF table scan process or roundrobin failover
  278. * process to unregister the in-use FCF.
  279. **/
  280. static void
  281. lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
  282. uint32_t nlp_did)
  283. {
  284. /* If devloss timeout happened to a remote node when FCF had no
  285. * longer been in-use, do nothing.
  286. */
  287. if (!fcf_inuse)
  288. return;
  289. if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
  290. spin_lock_irq(&phba->hbalock);
  291. if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
  292. if (phba->hba_flag & HBA_DEVLOSS_TMO) {
  293. spin_unlock_irq(&phba->hbalock);
  294. return;
  295. }
  296. phba->hba_flag |= HBA_DEVLOSS_TMO;
  297. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  298. "2847 Last remote node (x%x) using "
  299. "FCF devloss tmo\n", nlp_did);
  300. }
  301. if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
  302. spin_unlock_irq(&phba->hbalock);
  303. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  304. "2868 Devloss tmo to FCF rediscovery "
  305. "in progress\n");
  306. return;
  307. }
  308. if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
  309. spin_unlock_irq(&phba->hbalock);
  310. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  311. "2869 Devloss tmo to idle FIP engine, "
  312. "unreg in-use FCF and rescan.\n");
  313. /* Unregister in-use FCF and rescan */
  314. lpfc_unregister_fcf_rescan(phba);
  315. return;
  316. }
  317. spin_unlock_irq(&phba->hbalock);
  318. if (phba->hba_flag & FCF_TS_INPROG)
  319. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  320. "2870 FCF table scan in progress\n");
  321. if (phba->hba_flag & FCF_RR_INPROG)
  322. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  323. "2871 FLOGI roundrobin FCF failover "
  324. "in progress\n");
  325. }
  326. lpfc_unregister_unused_fcf(phba);
  327. }
  328. /**
  329. * lpfc_alloc_fast_evt - Allocates data structure for posting event
  330. * @phba: Pointer to hba context object.
  331. *
  332. * This function is called from the functions which need to post
  333. * events from interrupt context. This function allocates data
  334. * structure required for posting event. It also keeps track of
  335. * number of events pending and prevent event storm when there are
  336. * too many events.
  337. **/
  338. struct lpfc_fast_path_event *
  339. lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
  340. struct lpfc_fast_path_event *ret;
  341. /* If there are lot of fast event do not exhaust memory due to this */
  342. if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
  343. return NULL;
  344. ret = kzalloc(sizeof(struct lpfc_fast_path_event),
  345. GFP_ATOMIC);
  346. if (ret) {
  347. atomic_inc(&phba->fast_event_count);
  348. INIT_LIST_HEAD(&ret->work_evt.evt_listp);
  349. ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
  350. }
  351. return ret;
  352. }
  353. /**
  354. * lpfc_free_fast_evt - Frees event data structure
  355. * @phba: Pointer to hba context object.
  356. * @evt: Event object which need to be freed.
  357. *
  358. * This function frees the data structure required for posting
  359. * events.
  360. **/
  361. void
  362. lpfc_free_fast_evt(struct lpfc_hba *phba,
  363. struct lpfc_fast_path_event *evt) {
  364. atomic_dec(&phba->fast_event_count);
  365. kfree(evt);
  366. }
  367. /**
  368. * lpfc_send_fastpath_evt - Posts events generated from fast path
  369. * @phba: Pointer to hba context object.
  370. * @evtp: Event data structure.
  371. *
  372. * This function is called from worker thread, when the interrupt
  373. * context need to post an event. This function posts the event
  374. * to fc transport netlink interface.
  375. **/
  376. static void
  377. lpfc_send_fastpath_evt(struct lpfc_hba *phba,
  378. struct lpfc_work_evt *evtp)
  379. {
  380. unsigned long evt_category, evt_sub_category;
  381. struct lpfc_fast_path_event *fast_evt_data;
  382. char *evt_data;
  383. uint32_t evt_data_size;
  384. struct Scsi_Host *shost;
  385. fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
  386. work_evt);
  387. evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
  388. evt_sub_category = (unsigned long) fast_evt_data->un.
  389. fabric_evt.subcategory;
  390. shost = lpfc_shost_from_vport(fast_evt_data->vport);
  391. if (evt_category == FC_REG_FABRIC_EVENT) {
  392. if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
  393. evt_data = (char *) &fast_evt_data->un.read_check_error;
  394. evt_data_size = sizeof(fast_evt_data->un.
  395. read_check_error);
  396. } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
  397. (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
  398. evt_data = (char *) &fast_evt_data->un.fabric_evt;
  399. evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
  400. } else {
  401. lpfc_free_fast_evt(phba, fast_evt_data);
  402. return;
  403. }
  404. } else if (evt_category == FC_REG_SCSI_EVENT) {
  405. switch (evt_sub_category) {
  406. case LPFC_EVENT_QFULL:
  407. case LPFC_EVENT_DEVBSY:
  408. evt_data = (char *) &fast_evt_data->un.scsi_evt;
  409. evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
  410. break;
  411. case LPFC_EVENT_CHECK_COND:
  412. evt_data = (char *) &fast_evt_data->un.check_cond_evt;
  413. evt_data_size = sizeof(fast_evt_data->un.
  414. check_cond_evt);
  415. break;
  416. case LPFC_EVENT_VARQUEDEPTH:
  417. evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
  418. evt_data_size = sizeof(fast_evt_data->un.
  419. queue_depth_evt);
  420. break;
  421. default:
  422. lpfc_free_fast_evt(phba, fast_evt_data);
  423. return;
  424. }
  425. } else {
  426. lpfc_free_fast_evt(phba, fast_evt_data);
  427. return;
  428. }
  429. fc_host_post_vendor_event(shost,
  430. fc_get_event_number(),
  431. evt_data_size,
  432. evt_data,
  433. LPFC_NL_VENDOR_ID);
  434. lpfc_free_fast_evt(phba, fast_evt_data);
  435. return;
  436. }
  437. static void
  438. lpfc_work_list_done(struct lpfc_hba *phba)
  439. {
  440. struct lpfc_work_evt *evtp = NULL;
  441. struct lpfc_nodelist *ndlp;
  442. int free_evt;
  443. int fcf_inuse;
  444. uint32_t nlp_did;
  445. spin_lock_irq(&phba->hbalock);
  446. while (!list_empty(&phba->work_list)) {
  447. list_remove_head((&phba->work_list), evtp, typeof(*evtp),
  448. evt_listp);
  449. spin_unlock_irq(&phba->hbalock);
  450. free_evt = 1;
  451. switch (evtp->evt) {
  452. case LPFC_EVT_ELS_RETRY:
  453. ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
  454. lpfc_els_retry_delay_handler(ndlp);
  455. free_evt = 0; /* evt is part of ndlp */
  456. /* decrement the node reference count held
  457. * for this queued work
  458. */
  459. lpfc_nlp_put(ndlp);
  460. break;
  461. case LPFC_EVT_DEV_LOSS:
  462. ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
  463. fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
  464. free_evt = 0;
  465. /* decrement the node reference count held for
  466. * this queued work
  467. */
  468. nlp_did = ndlp->nlp_DID;
  469. lpfc_nlp_put(ndlp);
  470. if (phba->sli_rev == LPFC_SLI_REV4)
  471. lpfc_sli4_post_dev_loss_tmo_handler(phba,
  472. fcf_inuse,
  473. nlp_did);
  474. break;
  475. case LPFC_EVT_ONLINE:
  476. if (phba->link_state < LPFC_LINK_DOWN)
  477. *(int *) (evtp->evt_arg1) = lpfc_online(phba);
  478. else
  479. *(int *) (evtp->evt_arg1) = 0;
  480. complete((struct completion *)(evtp->evt_arg2));
  481. break;
  482. case LPFC_EVT_OFFLINE_PREP:
  483. if (phba->link_state >= LPFC_LINK_DOWN)
  484. lpfc_offline_prep(phba);
  485. *(int *)(evtp->evt_arg1) = 0;
  486. complete((struct completion *)(evtp->evt_arg2));
  487. break;
  488. case LPFC_EVT_OFFLINE:
  489. lpfc_offline(phba);
  490. lpfc_sli_brdrestart(phba);
  491. *(int *)(evtp->evt_arg1) =
  492. lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
  493. lpfc_unblock_mgmt_io(phba);
  494. complete((struct completion *)(evtp->evt_arg2));
  495. break;
  496. case LPFC_EVT_WARM_START:
  497. lpfc_offline(phba);
  498. lpfc_reset_barrier(phba);
  499. lpfc_sli_brdreset(phba);
  500. lpfc_hba_down_post(phba);
  501. *(int *)(evtp->evt_arg1) =
  502. lpfc_sli_brdready(phba, HS_MBRDY);
  503. lpfc_unblock_mgmt_io(phba);
  504. complete((struct completion *)(evtp->evt_arg2));
  505. break;
  506. case LPFC_EVT_KILL:
  507. lpfc_offline(phba);
  508. *(int *)(evtp->evt_arg1)
  509. = (phba->pport->stopped)
  510. ? 0 : lpfc_sli_brdkill(phba);
  511. lpfc_unblock_mgmt_io(phba);
  512. complete((struct completion *)(evtp->evt_arg2));
  513. break;
  514. case LPFC_EVT_FASTPATH_MGMT_EVT:
  515. lpfc_send_fastpath_evt(phba, evtp);
  516. free_evt = 0;
  517. break;
  518. case LPFC_EVT_RESET_HBA:
  519. if (!(phba->pport->load_flag & FC_UNLOADING))
  520. lpfc_reset_hba(phba);
  521. break;
  522. }
  523. if (free_evt)
  524. kfree(evtp);
  525. spin_lock_irq(&phba->hbalock);
  526. }
  527. spin_unlock_irq(&phba->hbalock);
  528. }
  529. static void
  530. lpfc_work_done(struct lpfc_hba *phba)
  531. {
  532. struct lpfc_sli_ring *pring;
  533. uint32_t ha_copy, status, control, work_port_events;
  534. struct lpfc_vport **vports;
  535. struct lpfc_vport *vport;
  536. int i;
  537. spin_lock_irq(&phba->hbalock);
  538. ha_copy = phba->work_ha;
  539. phba->work_ha = 0;
  540. spin_unlock_irq(&phba->hbalock);
  541. /* First, try to post the next mailbox command to SLI4 device */
  542. if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
  543. lpfc_sli4_post_async_mbox(phba);
  544. if (ha_copy & HA_ERATT)
  545. /* Handle the error attention event */
  546. lpfc_handle_eratt(phba);
  547. if (ha_copy & HA_MBATT)
  548. lpfc_sli_handle_mb_event(phba);
  549. if (ha_copy & HA_LATT)
  550. lpfc_handle_latt(phba);
  551. /* Process SLI4 events */
  552. if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
  553. if (phba->hba_flag & HBA_RRQ_ACTIVE)
  554. lpfc_handle_rrq_active(phba);
  555. if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
  556. lpfc_sli4_fcp_xri_abort_event_proc(phba);
  557. if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
  558. lpfc_sli4_els_xri_abort_event_proc(phba);
  559. if (phba->hba_flag & ASYNC_EVENT)
  560. lpfc_sli4_async_event_proc(phba);
  561. if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
  562. spin_lock_irq(&phba->hbalock);
  563. phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
  564. spin_unlock_irq(&phba->hbalock);
  565. lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
  566. }
  567. if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
  568. lpfc_sli4_fcf_redisc_event_proc(phba);
  569. }
  570. vports = lpfc_create_vport_work_array(phba);
  571. if (vports != NULL)
  572. for (i = 0; i <= phba->max_vports; i++) {
  573. /*
  574. * We could have no vports in array if unloading, so if
  575. * this happens then just use the pport
  576. */
  577. if (vports[i] == NULL && i == 0)
  578. vport = phba->pport;
  579. else
  580. vport = vports[i];
  581. if (vport == NULL)
  582. break;
  583. spin_lock_irq(&vport->work_port_lock);
  584. work_port_events = vport->work_port_events;
  585. vport->work_port_events &= ~work_port_events;
  586. spin_unlock_irq(&vport->work_port_lock);
  587. if (work_port_events & WORKER_DISC_TMO)
  588. lpfc_disc_timeout_handler(vport);
  589. if (work_port_events & WORKER_ELS_TMO)
  590. lpfc_els_timeout_handler(vport);
  591. if (work_port_events & WORKER_HB_TMO)
  592. lpfc_hb_timeout_handler(phba);
  593. if (work_port_events & WORKER_MBOX_TMO)
  594. lpfc_mbox_timeout_handler(phba);
  595. if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
  596. lpfc_unblock_fabric_iocbs(phba);
  597. if (work_port_events & WORKER_FDMI_TMO)
  598. lpfc_fdmi_timeout_handler(vport);
  599. if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
  600. lpfc_ramp_down_queue_handler(phba);
  601. if (work_port_events & WORKER_RAMP_UP_QUEUE)
  602. lpfc_ramp_up_queue_handler(phba);
  603. if (work_port_events & WORKER_DELAYED_DISC_TMO)
  604. lpfc_delayed_disc_timeout_handler(vport);
  605. }
  606. lpfc_destroy_vport_work_array(phba, vports);
  607. pring = &phba->sli.ring[LPFC_ELS_RING];
  608. status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  609. status >>= (4*LPFC_ELS_RING);
  610. if ((status & HA_RXMASK) ||
  611. (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
  612. (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
  613. if (pring->flag & LPFC_STOP_IOCB_EVENT) {
  614. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  615. /* Set the lpfc data pending flag */
  616. set_bit(LPFC_DATA_READY, &phba->data_flags);
  617. } else {
  618. pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
  619. lpfc_sli_handle_slow_ring_event(phba, pring,
  620. (status &
  621. HA_RXMASK));
  622. }
  623. if ((phba->sli_rev == LPFC_SLI_REV4) && pring->txq_cnt)
  624. lpfc_drain_txq(phba);
  625. /*
  626. * Turn on Ring interrupts
  627. */
  628. if (phba->sli_rev <= LPFC_SLI_REV3) {
  629. spin_lock_irq(&phba->hbalock);
  630. control = readl(phba->HCregaddr);
  631. if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
  632. lpfc_debugfs_slow_ring_trc(phba,
  633. "WRK Enable ring: cntl:x%x hacopy:x%x",
  634. control, ha_copy, 0);
  635. control |= (HC_R0INT_ENA << LPFC_ELS_RING);
  636. writel(control, phba->HCregaddr);
  637. readl(phba->HCregaddr); /* flush */
  638. } else {
  639. lpfc_debugfs_slow_ring_trc(phba,
  640. "WRK Ring ok: cntl:x%x hacopy:x%x",
  641. control, ha_copy, 0);
  642. }
  643. spin_unlock_irq(&phba->hbalock);
  644. }
  645. }
  646. lpfc_work_list_done(phba);
  647. }
  648. int
  649. lpfc_do_work(void *p)
  650. {
  651. struct lpfc_hba *phba = p;
  652. int rc;
  653. set_user_nice(current, -20);
  654. phba->data_flags = 0;
  655. while (!kthread_should_stop()) {
  656. /* wait and check worker queue activities */
  657. rc = wait_event_interruptible(phba->work_waitq,
  658. (test_and_clear_bit(LPFC_DATA_READY,
  659. &phba->data_flags)
  660. || kthread_should_stop()));
  661. /* Signal wakeup shall terminate the worker thread */
  662. if (rc) {
  663. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  664. "0433 Wakeup on signal: rc=x%x\n", rc);
  665. break;
  666. }
  667. /* Attend pending lpfc data processing */
  668. lpfc_work_done(phba);
  669. }
  670. phba->worker_thread = NULL;
  671. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  672. "0432 Worker thread stopped.\n");
  673. return 0;
  674. }
  675. /*
  676. * This is only called to handle FC worker events. Since this a rare
  677. * occurrence, we allocate a struct lpfc_work_evt structure here instead of
  678. * embedding it in the IOCB.
  679. */
  680. int
  681. lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
  682. uint32_t evt)
  683. {
  684. struct lpfc_work_evt *evtp;
  685. unsigned long flags;
  686. /*
  687. * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
  688. * be queued to worker thread for processing
  689. */
  690. evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
  691. if (!evtp)
  692. return 0;
  693. evtp->evt_arg1 = arg1;
  694. evtp->evt_arg2 = arg2;
  695. evtp->evt = evt;
  696. spin_lock_irqsave(&phba->hbalock, flags);
  697. list_add_tail(&evtp->evt_listp, &phba->work_list);
  698. spin_unlock_irqrestore(&phba->hbalock, flags);
  699. lpfc_worker_wake_up(phba);
  700. return 1;
  701. }
  702. void
  703. lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
  704. {
  705. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  706. struct lpfc_hba *phba = vport->phba;
  707. struct lpfc_nodelist *ndlp, *next_ndlp;
  708. int rc;
  709. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  710. if (!NLP_CHK_NODE_ACT(ndlp))
  711. continue;
  712. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  713. continue;
  714. if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
  715. ((vport->port_type == LPFC_NPIV_PORT) &&
  716. (ndlp->nlp_DID == NameServer_DID)))
  717. lpfc_unreg_rpi(vport, ndlp);
  718. /* Leave Fabric nodes alone on link down */
  719. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  720. (!remove && ndlp->nlp_type & NLP_FABRIC))
  721. continue;
  722. rc = lpfc_disc_state_machine(vport, ndlp, NULL,
  723. remove
  724. ? NLP_EVT_DEVICE_RM
  725. : NLP_EVT_DEVICE_RECOVERY);
  726. }
  727. if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
  728. if (phba->sli_rev == LPFC_SLI_REV4)
  729. lpfc_sli4_unreg_all_rpis(vport);
  730. lpfc_mbx_unreg_vpi(vport);
  731. spin_lock_irq(shost->host_lock);
  732. vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  733. spin_unlock_irq(shost->host_lock);
  734. }
  735. }
  736. void
  737. lpfc_port_link_failure(struct lpfc_vport *vport)
  738. {
  739. lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
  740. /* Cleanup any outstanding received buffers */
  741. lpfc_cleanup_rcv_buffers(vport);
  742. /* Cleanup any outstanding RSCN activity */
  743. lpfc_els_flush_rscn(vport);
  744. /* Cleanup any outstanding ELS commands */
  745. lpfc_els_flush_cmd(vport);
  746. lpfc_cleanup_rpis(vport, 0);
  747. /* Turn off discovery timer if its running */
  748. lpfc_can_disctmo(vport);
  749. }
  750. void
  751. lpfc_linkdown_port(struct lpfc_vport *vport)
  752. {
  753. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  754. fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
  755. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  756. "Link Down: state:x%x rtry:x%x flg:x%x",
  757. vport->port_state, vport->fc_ns_retry, vport->fc_flag);
  758. lpfc_port_link_failure(vport);
  759. /* Stop delayed Nport discovery */
  760. spin_lock_irq(shost->host_lock);
  761. vport->fc_flag &= ~FC_DISC_DELAYED;
  762. spin_unlock_irq(shost->host_lock);
  763. del_timer_sync(&vport->delayed_disc_tmo);
  764. }
  765. int
  766. lpfc_linkdown(struct lpfc_hba *phba)
  767. {
  768. struct lpfc_vport *vport = phba->pport;
  769. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  770. struct lpfc_vport **vports;
  771. LPFC_MBOXQ_t *mb;
  772. int i;
  773. if (phba->link_state == LPFC_LINK_DOWN)
  774. return 0;
  775. /* Block all SCSI stack I/Os */
  776. lpfc_scsi_dev_block(phba);
  777. spin_lock_irq(&phba->hbalock);
  778. phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
  779. spin_unlock_irq(&phba->hbalock);
  780. if (phba->link_state > LPFC_LINK_DOWN) {
  781. phba->link_state = LPFC_LINK_DOWN;
  782. spin_lock_irq(shost->host_lock);
  783. phba->pport->fc_flag &= ~FC_LBIT;
  784. spin_unlock_irq(shost->host_lock);
  785. }
  786. vports = lpfc_create_vport_work_array(phba);
  787. if (vports != NULL)
  788. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  789. /* Issue a LINK DOWN event to all nodes */
  790. lpfc_linkdown_port(vports[i]);
  791. }
  792. lpfc_destroy_vport_work_array(phba, vports);
  793. /* Clean up any firmware default rpi's */
  794. mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  795. if (mb) {
  796. lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
  797. mb->vport = vport;
  798. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  799. if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
  800. == MBX_NOT_FINISHED) {
  801. mempool_free(mb, phba->mbox_mem_pool);
  802. }
  803. }
  804. /* Setup myDID for link up if we are in pt2pt mode */
  805. if (phba->pport->fc_flag & FC_PT2PT) {
  806. phba->pport->fc_myDID = 0;
  807. mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  808. if (mb) {
  809. lpfc_config_link(phba, mb);
  810. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  811. mb->vport = vport;
  812. if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
  813. == MBX_NOT_FINISHED) {
  814. mempool_free(mb, phba->mbox_mem_pool);
  815. }
  816. }
  817. spin_lock_irq(shost->host_lock);
  818. phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
  819. spin_unlock_irq(shost->host_lock);
  820. }
  821. return 0;
  822. }
  823. static void
  824. lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
  825. {
  826. struct lpfc_nodelist *ndlp;
  827. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  828. if (!NLP_CHK_NODE_ACT(ndlp))
  829. continue;
  830. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  831. continue;
  832. if (ndlp->nlp_type & NLP_FABRIC) {
  833. /* On Linkup its safe to clean up the ndlp
  834. * from Fabric connections.
  835. */
  836. if (ndlp->nlp_DID != Fabric_DID)
  837. lpfc_unreg_rpi(vport, ndlp);
  838. lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
  839. } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
  840. /* Fail outstanding IO now since device is
  841. * marked for PLOGI.
  842. */
  843. lpfc_unreg_rpi(vport, ndlp);
  844. }
  845. }
  846. }
  847. static void
  848. lpfc_linkup_port(struct lpfc_vport *vport)
  849. {
  850. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  851. struct lpfc_hba *phba = vport->phba;
  852. if ((vport->load_flag & FC_UNLOADING) != 0)
  853. return;
  854. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  855. "Link Up: top:x%x speed:x%x flg:x%x",
  856. phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
  857. /* If NPIV is not enabled, only bring the physical port up */
  858. if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
  859. (vport != phba->pport))
  860. return;
  861. fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
  862. spin_lock_irq(shost->host_lock);
  863. vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
  864. FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
  865. vport->fc_flag |= FC_NDISC_ACTIVE;
  866. vport->fc_ns_retry = 0;
  867. spin_unlock_irq(shost->host_lock);
  868. if (vport->fc_flag & FC_LBIT)
  869. lpfc_linkup_cleanup_nodes(vport);
  870. }
  871. static int
  872. lpfc_linkup(struct lpfc_hba *phba)
  873. {
  874. struct lpfc_vport **vports;
  875. int i;
  876. lpfc_cleanup_wt_rrqs(phba);
  877. phba->link_state = LPFC_LINK_UP;
  878. /* Unblock fabric iocbs if they are blocked */
  879. clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
  880. del_timer_sync(&phba->fabric_block_timer);
  881. vports = lpfc_create_vport_work_array(phba);
  882. if (vports != NULL)
  883. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
  884. lpfc_linkup_port(vports[i]);
  885. lpfc_destroy_vport_work_array(phba, vports);
  886. if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
  887. (phba->sli_rev < LPFC_SLI_REV4))
  888. lpfc_issue_clear_la(phba, phba->pport);
  889. return 0;
  890. }
  891. /*
  892. * This routine handles processing a CLEAR_LA mailbox
  893. * command upon completion. It is setup in the LPFC_MBOXQ
  894. * as the completion routine when the command is
  895. * handed off to the SLI layer.
  896. */
  897. static void
  898. lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  899. {
  900. struct lpfc_vport *vport = pmb->vport;
  901. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  902. struct lpfc_sli *psli = &phba->sli;
  903. MAILBOX_t *mb = &pmb->u.mb;
  904. uint32_t control;
  905. /* Since we don't do discovery right now, turn these off here */
  906. psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
  907. psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
  908. psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
  909. /* Check for error */
  910. if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
  911. /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
  912. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  913. "0320 CLEAR_LA mbxStatus error x%x hba "
  914. "state x%x\n",
  915. mb->mbxStatus, vport->port_state);
  916. phba->link_state = LPFC_HBA_ERROR;
  917. goto out;
  918. }
  919. if (vport->port_type == LPFC_PHYSICAL_PORT)
  920. phba->link_state = LPFC_HBA_READY;
  921. spin_lock_irq(&phba->hbalock);
  922. psli->sli_flag |= LPFC_PROCESS_LA;
  923. control = readl(phba->HCregaddr);
  924. control |= HC_LAINT_ENA;
  925. writel(control, phba->HCregaddr);
  926. readl(phba->HCregaddr); /* flush */
  927. spin_unlock_irq(&phba->hbalock);
  928. mempool_free(pmb, phba->mbox_mem_pool);
  929. return;
  930. out:
  931. /* Device Discovery completes */
  932. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  933. "0225 Device Discovery completes\n");
  934. mempool_free(pmb, phba->mbox_mem_pool);
  935. spin_lock_irq(shost->host_lock);
  936. vport->fc_flag &= ~FC_ABORT_DISCOVERY;
  937. spin_unlock_irq(shost->host_lock);
  938. lpfc_can_disctmo(vport);
  939. /* turn on Link Attention interrupts */
  940. spin_lock_irq(&phba->hbalock);
  941. psli->sli_flag |= LPFC_PROCESS_LA;
  942. control = readl(phba->HCregaddr);
  943. control |= HC_LAINT_ENA;
  944. writel(control, phba->HCregaddr);
  945. readl(phba->HCregaddr); /* flush */
  946. spin_unlock_irq(&phba->hbalock);
  947. return;
  948. }
  949. static void
  950. lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  951. {
  952. struct lpfc_vport *vport = pmb->vport;
  953. if (pmb->u.mb.mbxStatus)
  954. goto out;
  955. mempool_free(pmb, phba->mbox_mem_pool);
  956. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
  957. vport->fc_flag & FC_PUBLIC_LOOP &&
  958. !(vport->fc_flag & FC_LBIT)) {
  959. /* Need to wait for FAN - use discovery timer
  960. * for timeout. port_state is identically
  961. * LPFC_LOCAL_CFG_LINK while waiting for FAN
  962. */
  963. lpfc_set_disctmo(vport);
  964. return;
  965. }
  966. /* Start discovery by sending a FLOGI. port_state is identically
  967. * LPFC_FLOGI while waiting for FLOGI cmpl
  968. */
  969. if (vport->port_state != LPFC_FLOGI)
  970. lpfc_initial_flogi(vport);
  971. return;
  972. out:
  973. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  974. "0306 CONFIG_LINK mbxStatus error x%x "
  975. "HBA state x%x\n",
  976. pmb->u.mb.mbxStatus, vport->port_state);
  977. mempool_free(pmb, phba->mbox_mem_pool);
  978. lpfc_linkdown(phba);
  979. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  980. "0200 CONFIG_LINK bad hba state x%x\n",
  981. vport->port_state);
  982. lpfc_issue_clear_la(phba, vport);
  983. return;
  984. }
  985. /**
  986. * lpfc_sli4_clear_fcf_rr_bmask
  987. * @phba pointer to the struct lpfc_hba for this port.
  988. * This fucnction resets the round robin bit mask and clears the
  989. * fcf priority list. The list deletions are done while holding the
  990. * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
  991. * from the lpfc_fcf_pri record.
  992. **/
  993. void
  994. lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
  995. {
  996. struct lpfc_fcf_pri *fcf_pri;
  997. struct lpfc_fcf_pri *next_fcf_pri;
  998. memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
  999. spin_lock_irq(&phba->hbalock);
  1000. list_for_each_entry_safe(fcf_pri, next_fcf_pri,
  1001. &phba->fcf.fcf_pri_list, list) {
  1002. list_del_init(&fcf_pri->list);
  1003. fcf_pri->fcf_rec.flag = 0;
  1004. }
  1005. spin_unlock_irq(&phba->hbalock);
  1006. }
  1007. static void
  1008. lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  1009. {
  1010. struct lpfc_vport *vport = mboxq->vport;
  1011. if (mboxq->u.mb.mbxStatus) {
  1012. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  1013. "2017 REG_FCFI mbxStatus error x%x "
  1014. "HBA state x%x\n",
  1015. mboxq->u.mb.mbxStatus, vport->port_state);
  1016. goto fail_out;
  1017. }
  1018. /* Start FCoE discovery by sending a FLOGI. */
  1019. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
  1020. /* Set the FCFI registered flag */
  1021. spin_lock_irq(&phba->hbalock);
  1022. phba->fcf.fcf_flag |= FCF_REGISTERED;
  1023. spin_unlock_irq(&phba->hbalock);
  1024. /* If there is a pending FCoE event, restart FCF table scan. */
  1025. if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
  1026. lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
  1027. goto fail_out;
  1028. /* Mark successful completion of FCF table scan */
  1029. spin_lock_irq(&phba->hbalock);
  1030. phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
  1031. phba->hba_flag &= ~FCF_TS_INPROG;
  1032. if (vport->port_state != LPFC_FLOGI) {
  1033. phba->hba_flag |= FCF_RR_INPROG;
  1034. spin_unlock_irq(&phba->hbalock);
  1035. lpfc_issue_init_vfi(vport);
  1036. goto out;
  1037. }
  1038. spin_unlock_irq(&phba->hbalock);
  1039. goto out;
  1040. fail_out:
  1041. spin_lock_irq(&phba->hbalock);
  1042. phba->hba_flag &= ~FCF_RR_INPROG;
  1043. spin_unlock_irq(&phba->hbalock);
  1044. out:
  1045. mempool_free(mboxq, phba->mbox_mem_pool);
  1046. }
  1047. /**
  1048. * lpfc_fab_name_match - Check if the fcf fabric name match.
  1049. * @fab_name: pointer to fabric name.
  1050. * @new_fcf_record: pointer to fcf record.
  1051. *
  1052. * This routine compare the fcf record's fabric name with provided
  1053. * fabric name. If the fabric name are identical this function
  1054. * returns 1 else return 0.
  1055. **/
  1056. static uint32_t
  1057. lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
  1058. {
  1059. if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
  1060. return 0;
  1061. if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
  1062. return 0;
  1063. if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
  1064. return 0;
  1065. if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
  1066. return 0;
  1067. if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
  1068. return 0;
  1069. if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
  1070. return 0;
  1071. if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
  1072. return 0;
  1073. if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
  1074. return 0;
  1075. return 1;
  1076. }
  1077. /**
  1078. * lpfc_sw_name_match - Check if the fcf switch name match.
  1079. * @fab_name: pointer to fabric name.
  1080. * @new_fcf_record: pointer to fcf record.
  1081. *
  1082. * This routine compare the fcf record's switch name with provided
  1083. * switch name. If the switch name are identical this function
  1084. * returns 1 else return 0.
  1085. **/
  1086. static uint32_t
  1087. lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
  1088. {
  1089. if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
  1090. return 0;
  1091. if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
  1092. return 0;
  1093. if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
  1094. return 0;
  1095. if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
  1096. return 0;
  1097. if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
  1098. return 0;
  1099. if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
  1100. return 0;
  1101. if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
  1102. return 0;
  1103. if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
  1104. return 0;
  1105. return 1;
  1106. }
  1107. /**
  1108. * lpfc_mac_addr_match - Check if the fcf mac address match.
  1109. * @mac_addr: pointer to mac address.
  1110. * @new_fcf_record: pointer to fcf record.
  1111. *
  1112. * This routine compare the fcf record's mac address with HBA's
  1113. * FCF mac address. If the mac addresses are identical this function
  1114. * returns 1 else return 0.
  1115. **/
  1116. static uint32_t
  1117. lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
  1118. {
  1119. if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
  1120. return 0;
  1121. if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
  1122. return 0;
  1123. if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
  1124. return 0;
  1125. if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
  1126. return 0;
  1127. if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
  1128. return 0;
  1129. if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
  1130. return 0;
  1131. return 1;
  1132. }
  1133. static bool
  1134. lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
  1135. {
  1136. return (curr_vlan_id == new_vlan_id);
  1137. }
  1138. /**
  1139. * lpfc_update_fcf_record - Update driver fcf record
  1140. * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
  1141. * @phba: pointer to lpfc hba data structure.
  1142. * @fcf_index: Index for the lpfc_fcf_record.
  1143. * @new_fcf_record: pointer to hba fcf record.
  1144. *
  1145. * This routine updates the driver FCF priority record from the new HBA FCF
  1146. * record. This routine is called with the host lock held.
  1147. **/
  1148. static void
  1149. __lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
  1150. struct fcf_record *new_fcf_record
  1151. )
  1152. {
  1153. struct lpfc_fcf_pri *fcf_pri;
  1154. fcf_pri = &phba->fcf.fcf_pri[fcf_index];
  1155. fcf_pri->fcf_rec.fcf_index = fcf_index;
  1156. /* FCF record priority */
  1157. fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
  1158. }
  1159. /**
  1160. * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
  1161. * @fcf: pointer to driver fcf record.
  1162. * @new_fcf_record: pointer to fcf record.
  1163. *
  1164. * This routine copies the FCF information from the FCF
  1165. * record to lpfc_hba data structure.
  1166. **/
  1167. static void
  1168. lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
  1169. struct fcf_record *new_fcf_record)
  1170. {
  1171. /* Fabric name */
  1172. fcf_rec->fabric_name[0] =
  1173. bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
  1174. fcf_rec->fabric_name[1] =
  1175. bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
  1176. fcf_rec->fabric_name[2] =
  1177. bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
  1178. fcf_rec->fabric_name[3] =
  1179. bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
  1180. fcf_rec->fabric_name[4] =
  1181. bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
  1182. fcf_rec->fabric_name[5] =
  1183. bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
  1184. fcf_rec->fabric_name[6] =
  1185. bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
  1186. fcf_rec->fabric_name[7] =
  1187. bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
  1188. /* Mac address */
  1189. fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
  1190. fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
  1191. fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
  1192. fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
  1193. fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
  1194. fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
  1195. /* FCF record index */
  1196. fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
  1197. /* FCF record priority */
  1198. fcf_rec->priority = new_fcf_record->fip_priority;
  1199. /* Switch name */
  1200. fcf_rec->switch_name[0] =
  1201. bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
  1202. fcf_rec->switch_name[1] =
  1203. bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
  1204. fcf_rec->switch_name[2] =
  1205. bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
  1206. fcf_rec->switch_name[3] =
  1207. bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
  1208. fcf_rec->switch_name[4] =
  1209. bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
  1210. fcf_rec->switch_name[5] =
  1211. bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
  1212. fcf_rec->switch_name[6] =
  1213. bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
  1214. fcf_rec->switch_name[7] =
  1215. bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
  1216. }
  1217. /**
  1218. * lpfc_update_fcf_record - Update driver fcf record
  1219. * @phba: pointer to lpfc hba data structure.
  1220. * @fcf_rec: pointer to driver fcf record.
  1221. * @new_fcf_record: pointer to hba fcf record.
  1222. * @addr_mode: address mode to be set to the driver fcf record.
  1223. * @vlan_id: vlan tag to be set to the driver fcf record.
  1224. * @flag: flag bits to be set to the driver fcf record.
  1225. *
  1226. * This routine updates the driver FCF record from the new HBA FCF record
  1227. * together with the address mode, vlan_id, and other informations. This
  1228. * routine is called with the host lock held.
  1229. **/
  1230. static void
  1231. __lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
  1232. struct fcf_record *new_fcf_record, uint32_t addr_mode,
  1233. uint16_t vlan_id, uint32_t flag)
  1234. {
  1235. /* Copy the fields from the HBA's FCF record */
  1236. lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
  1237. /* Update other fields of driver FCF record */
  1238. fcf_rec->addr_mode = addr_mode;
  1239. fcf_rec->vlan_id = vlan_id;
  1240. fcf_rec->flag |= (flag | RECORD_VALID);
  1241. __lpfc_update_fcf_record_pri(phba,
  1242. bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
  1243. new_fcf_record);
  1244. }
  1245. /**
  1246. * lpfc_register_fcf - Register the FCF with hba.
  1247. * @phba: pointer to lpfc hba data structure.
  1248. *
  1249. * This routine issues a register fcfi mailbox command to register
  1250. * the fcf with HBA.
  1251. **/
  1252. static void
  1253. lpfc_register_fcf(struct lpfc_hba *phba)
  1254. {
  1255. LPFC_MBOXQ_t *fcf_mbxq;
  1256. int rc;
  1257. spin_lock_irq(&phba->hbalock);
  1258. /* If the FCF is not available do nothing. */
  1259. if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
  1260. phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
  1261. spin_unlock_irq(&phba->hbalock);
  1262. return;
  1263. }
  1264. /* The FCF is already registered, start discovery */
  1265. if (phba->fcf.fcf_flag & FCF_REGISTERED) {
  1266. phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
  1267. phba->hba_flag &= ~FCF_TS_INPROG;
  1268. if (phba->pport->port_state != LPFC_FLOGI) {
  1269. phba->hba_flag |= FCF_RR_INPROG;
  1270. spin_unlock_irq(&phba->hbalock);
  1271. lpfc_initial_flogi(phba->pport);
  1272. return;
  1273. }
  1274. spin_unlock_irq(&phba->hbalock);
  1275. return;
  1276. }
  1277. spin_unlock_irq(&phba->hbalock);
  1278. fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1279. if (!fcf_mbxq) {
  1280. spin_lock_irq(&phba->hbalock);
  1281. phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
  1282. spin_unlock_irq(&phba->hbalock);
  1283. return;
  1284. }
  1285. lpfc_reg_fcfi(phba, fcf_mbxq);
  1286. fcf_mbxq->vport = phba->pport;
  1287. fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
  1288. rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
  1289. if (rc == MBX_NOT_FINISHED) {
  1290. spin_lock_irq(&phba->hbalock);
  1291. phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
  1292. spin_unlock_irq(&phba->hbalock);
  1293. mempool_free(fcf_mbxq, phba->mbox_mem_pool);
  1294. }
  1295. return;
  1296. }
  1297. /**
  1298. * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
  1299. * @phba: pointer to lpfc hba data structure.
  1300. * @new_fcf_record: pointer to fcf record.
  1301. * @boot_flag: Indicates if this record used by boot bios.
  1302. * @addr_mode: The address mode to be used by this FCF
  1303. * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
  1304. *
  1305. * This routine compare the fcf record with connect list obtained from the
  1306. * config region to decide if this FCF can be used for SAN discovery. It returns
  1307. * 1 if this record can be used for SAN discovery else return zero. If this FCF
  1308. * record can be used for SAN discovery, the boot_flag will indicate if this FCF
  1309. * is used by boot bios and addr_mode will indicate the addressing mode to be
  1310. * used for this FCF when the function returns.
  1311. * If the FCF record need to be used with a particular vlan id, the vlan is
  1312. * set in the vlan_id on return of the function. If not VLAN tagging need to
  1313. * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
  1314. **/
  1315. static int
  1316. lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
  1317. struct fcf_record *new_fcf_record,
  1318. uint32_t *boot_flag, uint32_t *addr_mode,
  1319. uint16_t *vlan_id)
  1320. {
  1321. struct lpfc_fcf_conn_entry *conn_entry;
  1322. int i, j, fcf_vlan_id = 0;
  1323. /* Find the lowest VLAN id in the FCF record */
  1324. for (i = 0; i < 512; i++) {
  1325. if (new_fcf_record->vlan_bitmap[i]) {
  1326. fcf_vlan_id = i * 8;
  1327. j = 0;
  1328. while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
  1329. j++;
  1330. fcf_vlan_id++;
  1331. }
  1332. break;
  1333. }
  1334. }
  1335. /* If FCF not available return 0 */
  1336. if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
  1337. !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
  1338. return 0;
  1339. if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
  1340. *boot_flag = 0;
  1341. *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
  1342. new_fcf_record);
  1343. if (phba->valid_vlan)
  1344. *vlan_id = phba->vlan_id;
  1345. else
  1346. *vlan_id = LPFC_FCOE_NULL_VID;
  1347. return 1;
  1348. }
  1349. /*
  1350. * If there are no FCF connection table entry, driver connect to all
  1351. * FCFs.
  1352. */
  1353. if (list_empty(&phba->fcf_conn_rec_list)) {
  1354. *boot_flag = 0;
  1355. *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
  1356. new_fcf_record);
  1357. /*
  1358. * When there are no FCF connect entries, use driver's default
  1359. * addressing mode - FPMA.
  1360. */
  1361. if (*addr_mode & LPFC_FCF_FPMA)
  1362. *addr_mode = LPFC_FCF_FPMA;
  1363. /* If FCF record report a vlan id use that vlan id */
  1364. if (fcf_vlan_id)
  1365. *vlan_id = fcf_vlan_id;
  1366. else
  1367. *vlan_id = LPFC_FCOE_NULL_VID;
  1368. return 1;
  1369. }
  1370. list_for_each_entry(conn_entry,
  1371. &phba->fcf_conn_rec_list, list) {
  1372. if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
  1373. continue;
  1374. if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
  1375. !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
  1376. new_fcf_record))
  1377. continue;
  1378. if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
  1379. !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
  1380. new_fcf_record))
  1381. continue;
  1382. if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
  1383. /*
  1384. * If the vlan bit map does not have the bit set for the
  1385. * vlan id to be used, then it is not a match.
  1386. */
  1387. if (!(new_fcf_record->vlan_bitmap
  1388. [conn_entry->conn_rec.vlan_tag / 8] &
  1389. (1 << (conn_entry->conn_rec.vlan_tag % 8))))
  1390. continue;
  1391. }
  1392. /*
  1393. * If connection record does not support any addressing mode,
  1394. * skip the FCF record.
  1395. */
  1396. if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
  1397. & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
  1398. continue;
  1399. /*
  1400. * Check if the connection record specifies a required
  1401. * addressing mode.
  1402. */
  1403. if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
  1404. !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
  1405. /*
  1406. * If SPMA required but FCF not support this continue.
  1407. */
  1408. if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
  1409. !(bf_get(lpfc_fcf_record_mac_addr_prov,
  1410. new_fcf_record) & LPFC_FCF_SPMA))
  1411. continue;
  1412. /*
  1413. * If FPMA required but FCF not support this continue.
  1414. */
  1415. if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
  1416. !(bf_get(lpfc_fcf_record_mac_addr_prov,
  1417. new_fcf_record) & LPFC_FCF_FPMA))
  1418. continue;
  1419. }
  1420. /*
  1421. * This fcf record matches filtering criteria.
  1422. */
  1423. if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
  1424. *boot_flag = 1;
  1425. else
  1426. *boot_flag = 0;
  1427. /*
  1428. * If user did not specify any addressing mode, or if the
  1429. * preferred addressing mode specified by user is not supported
  1430. * by FCF, allow fabric to pick the addressing mode.
  1431. */
  1432. *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
  1433. new_fcf_record);
  1434. /*
  1435. * If the user specified a required address mode, assign that
  1436. * address mode
  1437. */
  1438. if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
  1439. (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
  1440. *addr_mode = (conn_entry->conn_rec.flags &
  1441. FCFCNCT_AM_SPMA) ?
  1442. LPFC_FCF_SPMA : LPFC_FCF_FPMA;
  1443. /*
  1444. * If the user specified a preferred address mode, use the
  1445. * addr mode only if FCF support the addr_mode.
  1446. */
  1447. else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
  1448. (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
  1449. (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
  1450. (*addr_mode & LPFC_FCF_SPMA))
  1451. *addr_mode = LPFC_FCF_SPMA;
  1452. else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
  1453. (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
  1454. !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
  1455. (*addr_mode & LPFC_FCF_FPMA))
  1456. *addr_mode = LPFC_FCF_FPMA;
  1457. /* If matching connect list has a vlan id, use it */
  1458. if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
  1459. *vlan_id = conn_entry->conn_rec.vlan_tag;
  1460. /*
  1461. * If no vlan id is specified in connect list, use the vlan id
  1462. * in the FCF record
  1463. */
  1464. else if (fcf_vlan_id)
  1465. *vlan_id = fcf_vlan_id;
  1466. else
  1467. *vlan_id = LPFC_FCOE_NULL_VID;
  1468. return 1;
  1469. }
  1470. return 0;
  1471. }
  1472. /**
  1473. * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
  1474. * @phba: pointer to lpfc hba data structure.
  1475. * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
  1476. *
  1477. * This function check if there is any fcoe event pending while driver
  1478. * scan FCF entries. If there is any pending event, it will restart the
  1479. * FCF saning and return 1 else return 0.
  1480. */
  1481. int
  1482. lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
  1483. {
  1484. /*
  1485. * If the Link is up and no FCoE events while in the
  1486. * FCF discovery, no need to restart FCF discovery.
  1487. */
  1488. if ((phba->link_state >= LPFC_LINK_UP) &&
  1489. (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
  1490. return 0;
  1491. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1492. "2768 Pending link or FCF event during current "
  1493. "handling of the previous event: link_state:x%x, "
  1494. "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
  1495. phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
  1496. phba->fcoe_eventtag);
  1497. spin_lock_irq(&phba->hbalock);
  1498. phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
  1499. spin_unlock_irq(&phba->hbalock);
  1500. if (phba->link_state >= LPFC_LINK_UP) {
  1501. lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
  1502. "2780 Restart FCF table scan due to "
  1503. "pending FCF event:evt_tag_at_scan:x%x, "
  1504. "evt_tag_current:x%x\n",
  1505. phba->fcoe_eventtag_at_fcf_scan,
  1506. phba->fcoe_eventtag);
  1507. lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
  1508. } else {
  1509. /*
  1510. * Do not continue FCF discovery and clear FCF_TS_INPROG
  1511. * flag
  1512. */
  1513. lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
  1514. "2833 Stop FCF discovery process due to link "
  1515. "state change (x%x)\n", phba->link_state);
  1516. spin_lock_irq(&phba->hbalock);
  1517. phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
  1518. phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
  1519. spin_unlock_irq(&phba->hbalock);
  1520. }
  1521. /* Unregister the currently registered FCF if required */
  1522. if (unreg_fcf) {
  1523. spin_lock_irq(&phba->hbalock);
  1524. phba->fcf.fcf_flag &= ~FCF_REGISTERED;
  1525. spin_unlock_irq(&phba->hbalock);
  1526. lpfc_sli4_unregister_fcf(phba);
  1527. }
  1528. return 1;
  1529. }
  1530. /**
  1531. * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
  1532. * @phba: pointer to lpfc hba data structure.
  1533. * @fcf_cnt: number of eligible fcf record seen so far.
  1534. *
  1535. * This function makes an running random selection decision on FCF record to
  1536. * use through a sequence of @fcf_cnt eligible FCF records with equal
  1537. * probability. To perform integer manunipulation of random numbers with
  1538. * size unit32_t, the lower 16 bits of the 32-bit random number returned
  1539. * from random32() are taken as the random random number generated.
  1540. *
  1541. * Returns true when outcome is for the newly read FCF record should be
  1542. * chosen; otherwise, return false when outcome is for keeping the previously
  1543. * chosen FCF record.
  1544. **/
  1545. static bool
  1546. lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
  1547. {
  1548. uint32_t rand_num;
  1549. /* Get 16-bit uniform random number */
  1550. rand_num = (0xFFFF & random32());
  1551. /* Decision with probability 1/fcf_cnt */
  1552. if ((fcf_cnt * rand_num) < 0xFFFF)
  1553. return true;
  1554. else
  1555. return false;
  1556. }
  1557. /**
  1558. * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
  1559. * @phba: pointer to lpfc hba data structure.
  1560. * @mboxq: pointer to mailbox object.
  1561. * @next_fcf_index: pointer to holder of next fcf index.
  1562. *
  1563. * This routine parses the non-embedded fcf mailbox command by performing the
  1564. * necessarily error checking, non-embedded read FCF record mailbox command
  1565. * SGE parsing, and endianness swapping.
  1566. *
  1567. * Returns the pointer to the new FCF record in the non-embedded mailbox
  1568. * command DMA memory if successfully, other NULL.
  1569. */
  1570. static struct fcf_record *
  1571. lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  1572. uint16_t *next_fcf_index)
  1573. {
  1574. void *virt_addr;
  1575. dma_addr_t phys_addr;
  1576. struct lpfc_mbx_sge sge;
  1577. struct lpfc_mbx_read_fcf_tbl *read_fcf;
  1578. uint32_t shdr_status, shdr_add_status;
  1579. union lpfc_sli4_cfg_shdr *shdr;
  1580. struct fcf_record *new_fcf_record;
  1581. /* Get the first SGE entry from the non-embedded DMA memory. This
  1582. * routine only uses a single SGE.
  1583. */
  1584. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  1585. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  1586. if (unlikely(!mboxq->sge_array)) {
  1587. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  1588. "2524 Failed to get the non-embedded SGE "
  1589. "virtual address\n");
  1590. return NULL;
  1591. }
  1592. virt_addr = mboxq->sge_array->addr[0];
  1593. shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
  1594. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  1595. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  1596. if (shdr_status || shdr_add_status) {
  1597. if (shdr_status == STATUS_FCF_TABLE_EMPTY)
  1598. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  1599. "2726 READ_FCF_RECORD Indicates empty "
  1600. "FCF table.\n");
  1601. else
  1602. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  1603. "2521 READ_FCF_RECORD mailbox failed "
  1604. "with status x%x add_status x%x, "
  1605. "mbx\n", shdr_status, shdr_add_status);
  1606. return NULL;
  1607. }
  1608. /* Interpreting the returned information of the FCF record */
  1609. read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
  1610. lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
  1611. sizeof(struct lpfc_mbx_read_fcf_tbl));
  1612. *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
  1613. new_fcf_record = (struct fcf_record *)(virt_addr +
  1614. sizeof(struct lpfc_mbx_read_fcf_tbl));
  1615. lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
  1616. offsetof(struct fcf_record, vlan_bitmap));
  1617. new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
  1618. new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
  1619. return new_fcf_record;
  1620. }
  1621. /**
  1622. * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
  1623. * @phba: pointer to lpfc hba data structure.
  1624. * @fcf_record: pointer to the fcf record.
  1625. * @vlan_id: the lowest vlan identifier associated to this fcf record.
  1626. * @next_fcf_index: the index to the next fcf record in hba's fcf table.
  1627. *
  1628. * This routine logs the detailed FCF record if the LOG_FIP loggin is
  1629. * enabled.
  1630. **/
  1631. static void
  1632. lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
  1633. struct fcf_record *fcf_record,
  1634. uint16_t vlan_id,
  1635. uint16_t next_fcf_index)
  1636. {
  1637. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1638. "2764 READ_FCF_RECORD:\n"
  1639. "\tFCF_Index : x%x\n"
  1640. "\tFCF_Avail : x%x\n"
  1641. "\tFCF_Valid : x%x\n"
  1642. "\tFIP_Priority : x%x\n"
  1643. "\tMAC_Provider : x%x\n"
  1644. "\tLowest VLANID : x%x\n"
  1645. "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
  1646. "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
  1647. "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
  1648. "\tNext_FCF_Index: x%x\n",
  1649. bf_get(lpfc_fcf_record_fcf_index, fcf_record),
  1650. bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
  1651. bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
  1652. fcf_record->fip_priority,
  1653. bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
  1654. vlan_id,
  1655. bf_get(lpfc_fcf_record_mac_0, fcf_record),
  1656. bf_get(lpfc_fcf_record_mac_1, fcf_record),
  1657. bf_get(lpfc_fcf_record_mac_2, fcf_record),
  1658. bf_get(lpfc_fcf_record_mac_3, fcf_record),
  1659. bf_get(lpfc_fcf_record_mac_4, fcf_record),
  1660. bf_get(lpfc_fcf_record_mac_5, fcf_record),
  1661. bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
  1662. bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
  1663. bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
  1664. bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
  1665. bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
  1666. bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
  1667. bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
  1668. bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
  1669. bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
  1670. bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
  1671. bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
  1672. bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
  1673. bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
  1674. bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
  1675. bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
  1676. bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
  1677. next_fcf_index);
  1678. }
  1679. /**
  1680. lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
  1681. * @phba: pointer to lpfc hba data structure.
  1682. * @fcf_rec: pointer to an existing FCF record.
  1683. * @new_fcf_record: pointer to a new FCF record.
  1684. * @new_vlan_id: vlan id from the new FCF record.
  1685. *
  1686. * This function performs matching test of a new FCF record against an existing
  1687. * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
  1688. * will not be used as part of the FCF record matching criteria.
  1689. *
  1690. * Returns true if all the fields matching, otherwise returns false.
  1691. */
  1692. static bool
  1693. lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
  1694. struct lpfc_fcf_rec *fcf_rec,
  1695. struct fcf_record *new_fcf_record,
  1696. uint16_t new_vlan_id)
  1697. {
  1698. if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
  1699. if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
  1700. return false;
  1701. if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
  1702. return false;
  1703. if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
  1704. return false;
  1705. if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
  1706. return false;
  1707. if (fcf_rec->priority != new_fcf_record->fip_priority)
  1708. return false;
  1709. return true;
  1710. }
  1711. /**
  1712. * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
  1713. * @vport: Pointer to vport object.
  1714. * @fcf_index: index to next fcf.
  1715. *
  1716. * This function processing the roundrobin fcf failover to next fcf index.
  1717. * When this function is invoked, there will be a current fcf registered
  1718. * for flogi.
  1719. * Return: 0 for continue retrying flogi on currently registered fcf;
  1720. * 1 for stop flogi on currently registered fcf;
  1721. */
  1722. int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
  1723. {
  1724. struct lpfc_hba *phba = vport->phba;
  1725. int rc;
  1726. if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
  1727. spin_lock_irq(&phba->hbalock);
  1728. if (phba->hba_flag & HBA_DEVLOSS_TMO) {
  1729. spin_unlock_irq(&phba->hbalock);
  1730. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1731. "2872 Devloss tmo with no eligible "
  1732. "FCF, unregister in-use FCF (x%x) "
  1733. "and rescan FCF table\n",
  1734. phba->fcf.current_rec.fcf_indx);
  1735. lpfc_unregister_fcf_rescan(phba);
  1736. goto stop_flogi_current_fcf;
  1737. }
  1738. /* Mark the end to FLOGI roundrobin failover */
  1739. phba->hba_flag &= ~FCF_RR_INPROG;
  1740. /* Allow action to new fcf asynchronous event */
  1741. phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
  1742. spin_unlock_irq(&phba->hbalock);
  1743. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1744. "2865 No FCF available, stop roundrobin FCF "
  1745. "failover and change port state:x%x/x%x\n",
  1746. phba->pport->port_state, LPFC_VPORT_UNKNOWN);
  1747. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  1748. goto stop_flogi_current_fcf;
  1749. } else {
  1750. lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
  1751. "2794 Try FLOGI roundrobin FCF failover to "
  1752. "(x%x)\n", fcf_index);
  1753. rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
  1754. if (rc)
  1755. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
  1756. "2761 FLOGI roundrobin FCF failover "
  1757. "failed (rc:x%x) to read FCF (x%x)\n",
  1758. rc, phba->fcf.current_rec.fcf_indx);
  1759. else
  1760. goto stop_flogi_current_fcf;
  1761. }
  1762. return 0;
  1763. stop_flogi_current_fcf:
  1764. lpfc_can_disctmo(vport);
  1765. return 1;
  1766. }
  1767. /**
  1768. * lpfc_sli4_fcf_pri_list_del
  1769. * @phba: pointer to lpfc hba data structure.
  1770. * @fcf_index the index of the fcf record to delete
  1771. * This routine checks the on list flag of the fcf_index to be deleted.
  1772. * If it is one the list then it is removed from the list, and the flag
  1773. * is cleared. This routine grab the hbalock before removing the fcf
  1774. * record from the list.
  1775. **/
  1776. static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
  1777. uint16_t fcf_index)
  1778. {
  1779. struct lpfc_fcf_pri *new_fcf_pri;
  1780. new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
  1781. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1782. "3058 deleting idx x%x pri x%x flg x%x\n",
  1783. fcf_index, new_fcf_pri->fcf_rec.priority,
  1784. new_fcf_pri->fcf_rec.flag);
  1785. spin_lock_irq(&phba->hbalock);
  1786. if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
  1787. if (phba->fcf.current_rec.priority ==
  1788. new_fcf_pri->fcf_rec.priority)
  1789. phba->fcf.eligible_fcf_cnt--;
  1790. list_del_init(&new_fcf_pri->list);
  1791. new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
  1792. }
  1793. spin_unlock_irq(&phba->hbalock);
  1794. }
  1795. /**
  1796. * lpfc_sli4_set_fcf_flogi_fail
  1797. * @phba: pointer to lpfc hba data structure.
  1798. * @fcf_index the index of the fcf record to update
  1799. * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
  1800. * flag so the the round robin slection for the particular priority level
  1801. * will try a different fcf record that does not have this bit set.
  1802. * If the fcf record is re-read for any reason this flag is cleared brfore
  1803. * adding it to the priority list.
  1804. **/
  1805. void
  1806. lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
  1807. {
  1808. struct lpfc_fcf_pri *new_fcf_pri;
  1809. new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
  1810. spin_lock_irq(&phba->hbalock);
  1811. new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
  1812. spin_unlock_irq(&phba->hbalock);
  1813. }
  1814. /**
  1815. * lpfc_sli4_fcf_pri_list_add
  1816. * @phba: pointer to lpfc hba data structure.
  1817. * @fcf_index the index of the fcf record to add
  1818. * This routine checks the priority of the fcf_index to be added.
  1819. * If it is a lower priority than the current head of the fcf_pri list
  1820. * then it is added to the list in the right order.
  1821. * If it is the same priority as the current head of the list then it
  1822. * is added to the head of the list and its bit in the rr_bmask is set.
  1823. * If the fcf_index to be added is of a higher priority than the current
  1824. * head of the list then the rr_bmask is cleared, its bit is set in the
  1825. * rr_bmask and it is added to the head of the list.
  1826. * returns:
  1827. * 0=success 1=failure
  1828. **/
  1829. int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba, uint16_t fcf_index,
  1830. struct fcf_record *new_fcf_record)
  1831. {
  1832. uint16_t current_fcf_pri;
  1833. uint16_t last_index;
  1834. struct lpfc_fcf_pri *fcf_pri;
  1835. struct lpfc_fcf_pri *next_fcf_pri;
  1836. struct lpfc_fcf_pri *new_fcf_pri;
  1837. int ret;
  1838. new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
  1839. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1840. "3059 adding idx x%x pri x%x flg x%x\n",
  1841. fcf_index, new_fcf_record->fip_priority,
  1842. new_fcf_pri->fcf_rec.flag);
  1843. spin_lock_irq(&phba->hbalock);
  1844. if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
  1845. list_del_init(&new_fcf_pri->list);
  1846. new_fcf_pri->fcf_rec.fcf_index = fcf_index;
  1847. new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
  1848. if (list_empty(&phba->fcf.fcf_pri_list)) {
  1849. list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
  1850. ret = lpfc_sli4_fcf_rr_index_set(phba,
  1851. new_fcf_pri->fcf_rec.fcf_index);
  1852. goto out;
  1853. }
  1854. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  1855. LPFC_SLI4_FCF_TBL_INDX_MAX);
  1856. if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  1857. ret = 0; /* Empty rr list */
  1858. goto out;
  1859. }
  1860. current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
  1861. if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
  1862. list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
  1863. if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
  1864. memset(phba->fcf.fcf_rr_bmask, 0,
  1865. sizeof(*phba->fcf.fcf_rr_bmask));
  1866. /* fcfs_at_this_priority_level = 1; */
  1867. phba->fcf.eligible_fcf_cnt = 1;
  1868. } else
  1869. /* fcfs_at_this_priority_level++; */
  1870. phba->fcf.eligible_fcf_cnt++;
  1871. ret = lpfc_sli4_fcf_rr_index_set(phba,
  1872. new_fcf_pri->fcf_rec.fcf_index);
  1873. goto out;
  1874. }
  1875. list_for_each_entry_safe(fcf_pri, next_fcf_pri,
  1876. &phba->fcf.fcf_pri_list, list) {
  1877. if (new_fcf_pri->fcf_rec.priority <=
  1878. fcf_pri->fcf_rec.priority) {
  1879. if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
  1880. list_add(&new_fcf_pri->list,
  1881. &phba->fcf.fcf_pri_list);
  1882. else
  1883. list_add(&new_fcf_pri->list,
  1884. &((struct lpfc_fcf_pri *)
  1885. fcf_pri->list.prev)->list);
  1886. ret = 0;
  1887. goto out;
  1888. } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
  1889. || new_fcf_pri->fcf_rec.priority <
  1890. next_fcf_pri->fcf_rec.priority) {
  1891. list_add(&new_fcf_pri->list, &fcf_pri->list);
  1892. ret = 0;
  1893. goto out;
  1894. }
  1895. if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
  1896. continue;
  1897. }
  1898. ret = 1;
  1899. out:
  1900. /* we use = instead of |= to clear the FLOGI_FAILED flag. */
  1901. new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
  1902. spin_unlock_irq(&phba->hbalock);
  1903. return ret;
  1904. }
  1905. /**
  1906. * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
  1907. * @phba: pointer to lpfc hba data structure.
  1908. * @mboxq: pointer to mailbox object.
  1909. *
  1910. * This function iterates through all the fcf records available in
  1911. * HBA and chooses the optimal FCF record for discovery. After finding
  1912. * the FCF for discovery it registers the FCF record and kicks start
  1913. * discovery.
  1914. * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
  1915. * use an FCF record which matches fabric name and mac address of the
  1916. * currently used FCF record.
  1917. * If the driver supports only one FCF, it will try to use the FCF record
  1918. * used by BOOT_BIOS.
  1919. */
  1920. void
  1921. lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  1922. {
  1923. struct fcf_record *new_fcf_record;
  1924. uint32_t boot_flag, addr_mode;
  1925. uint16_t fcf_index, next_fcf_index;
  1926. struct lpfc_fcf_rec *fcf_rec = NULL;
  1927. uint16_t vlan_id;
  1928. uint32_t seed;
  1929. bool select_new_fcf;
  1930. int rc;
  1931. /* If there is pending FCoE event restart FCF table scan */
  1932. if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
  1933. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1934. return;
  1935. }
  1936. /* Parse the FCF record from the non-embedded mailbox command */
  1937. new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
  1938. &next_fcf_index);
  1939. if (!new_fcf_record) {
  1940. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  1941. "2765 Mailbox command READ_FCF_RECORD "
  1942. "failed to retrieve a FCF record.\n");
  1943. /* Let next new FCF event trigger fast failover */
  1944. spin_lock_irq(&phba->hbalock);
  1945. phba->hba_flag &= ~FCF_TS_INPROG;
  1946. spin_unlock_irq(&phba->hbalock);
  1947. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1948. return;
  1949. }
  1950. /* Check the FCF record against the connection list */
  1951. rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
  1952. &addr_mode, &vlan_id);
  1953. /* Log the FCF record information if turned on */
  1954. lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
  1955. next_fcf_index);
  1956. /*
  1957. * If the fcf record does not match with connect list entries
  1958. * read the next entry; otherwise, this is an eligible FCF
  1959. * record for roundrobin FCF failover.
  1960. */
  1961. if (!rc) {
  1962. lpfc_sli4_fcf_pri_list_del(phba,
  1963. bf_get(lpfc_fcf_record_fcf_index,
  1964. new_fcf_record));
  1965. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  1966. "2781 FCF (x%x) failed connection "
  1967. "list check: (x%x/x%x)\n",
  1968. bf_get(lpfc_fcf_record_fcf_index,
  1969. new_fcf_record),
  1970. bf_get(lpfc_fcf_record_fcf_avail,
  1971. new_fcf_record),
  1972. bf_get(lpfc_fcf_record_fcf_valid,
  1973. new_fcf_record));
  1974. if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
  1975. lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
  1976. new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
  1977. if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
  1978. phba->fcf.current_rec.fcf_indx) {
  1979. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  1980. "2862 FCF (x%x) matches property "
  1981. "of in-use FCF (x%x)\n",
  1982. bf_get(lpfc_fcf_record_fcf_index,
  1983. new_fcf_record),
  1984. phba->fcf.current_rec.fcf_indx);
  1985. goto read_next_fcf;
  1986. }
  1987. /*
  1988. * In case the current in-use FCF record becomes
  1989. * invalid/unavailable during FCF discovery that
  1990. * was not triggered by fast FCF failover process,
  1991. * treat it as fast FCF failover.
  1992. */
  1993. if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
  1994. !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
  1995. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  1996. "2835 Invalid in-use FCF "
  1997. "(x%x), enter FCF failover "
  1998. "table scan.\n",
  1999. phba->fcf.current_rec.fcf_indx);
  2000. spin_lock_irq(&phba->hbalock);
  2001. phba->fcf.fcf_flag |= FCF_REDISC_FOV;
  2002. spin_unlock_irq(&phba->hbalock);
  2003. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  2004. lpfc_sli4_fcf_scan_read_fcf_rec(phba,
  2005. LPFC_FCOE_FCF_GET_FIRST);
  2006. return;
  2007. }
  2008. }
  2009. goto read_next_fcf;
  2010. } else {
  2011. fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
  2012. rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
  2013. new_fcf_record);
  2014. if (rc)
  2015. goto read_next_fcf;
  2016. }
  2017. /*
  2018. * If this is not the first FCF discovery of the HBA, use last
  2019. * FCF record for the discovery. The condition that a rescan
  2020. * matches the in-use FCF record: fabric name, switch name, mac
  2021. * address, and vlan_id.
  2022. */
  2023. spin_lock_irq(&phba->hbalock);
  2024. if (phba->fcf.fcf_flag & FCF_IN_USE) {
  2025. if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
  2026. lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
  2027. new_fcf_record, vlan_id)) {
  2028. if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
  2029. phba->fcf.current_rec.fcf_indx) {
  2030. phba->fcf.fcf_flag |= FCF_AVAILABLE;
  2031. if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
  2032. /* Stop FCF redisc wait timer */
  2033. __lpfc_sli4_stop_fcf_redisc_wait_timer(
  2034. phba);
  2035. else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
  2036. /* Fast failover, mark completed */
  2037. phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
  2038. spin_unlock_irq(&phba->hbalock);
  2039. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  2040. "2836 New FCF matches in-use "
  2041. "FCF (x%x)\n",
  2042. phba->fcf.current_rec.fcf_indx);
  2043. goto out;
  2044. } else
  2045. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  2046. "2863 New FCF (x%x) matches "
  2047. "property of in-use FCF (x%x)\n",
  2048. bf_get(lpfc_fcf_record_fcf_index,
  2049. new_fcf_record),
  2050. phba->fcf.current_rec.fcf_indx);
  2051. }
  2052. /*
  2053. * Read next FCF record from HBA searching for the matching
  2054. * with in-use record only if not during the fast failover
  2055. * period. In case of fast failover period, it shall try to
  2056. * determine whether the FCF record just read should be the
  2057. * next candidate.
  2058. */
  2059. if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
  2060. spin_unlock_irq(&phba->hbalock);
  2061. goto read_next_fcf;
  2062. }
  2063. }
  2064. /*
  2065. * Update on failover FCF record only if it's in FCF fast-failover
  2066. * period; otherwise, update on current FCF record.
  2067. */
  2068. if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
  2069. fcf_rec = &phba->fcf.failover_rec;
  2070. else
  2071. fcf_rec = &phba->fcf.current_rec;
  2072. if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
  2073. /*
  2074. * If the driver FCF record does not have boot flag
  2075. * set and new hba fcf record has boot flag set, use
  2076. * the new hba fcf record.
  2077. */
  2078. if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
  2079. /* Choose this FCF record */
  2080. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  2081. "2837 Update current FCF record "
  2082. "(x%x) with new FCF record (x%x)\n",
  2083. fcf_rec->fcf_indx,
  2084. bf_get(lpfc_fcf_record_fcf_index,
  2085. new_fcf_record));
  2086. __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
  2087. addr_mode, vlan_id, BOOT_ENABLE);
  2088. spin_unlock_irq(&phba->hbalock);
  2089. goto read_next_fcf;
  2090. }
  2091. /*
  2092. * If the driver FCF record has boot flag set and the
  2093. * new hba FCF record does not have boot flag, read
  2094. * the next FCF record.
  2095. */
  2096. if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
  2097. spin_unlock_irq(&phba->hbalock);
  2098. goto read_next_fcf;
  2099. }
  2100. /*
  2101. * If the new hba FCF record has lower priority value
  2102. * than the driver FCF record, use the new record.
  2103. */
  2104. if (new_fcf_record->fip_priority < fcf_rec->priority) {
  2105. /* Choose the new FCF record with lower priority */
  2106. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  2107. "2838 Update current FCF record "
  2108. "(x%x) with new FCF record (x%x)\n",
  2109. fcf_rec->fcf_indx,
  2110. bf_get(lpfc_fcf_record_fcf_index,
  2111. new_fcf_record));
  2112. __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
  2113. addr_mode, vlan_id, 0);
  2114. /* Reset running random FCF selection count */
  2115. phba->fcf.eligible_fcf_cnt = 1;
  2116. } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
  2117. /* Update running random FCF selection count */
  2118. phba->fcf.eligible_fcf_cnt++;
  2119. select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
  2120. phba->fcf.eligible_fcf_cnt);
  2121. if (select_new_fcf) {
  2122. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  2123. "2839 Update current FCF record "
  2124. "(x%x) with new FCF record (x%x)\n",
  2125. fcf_rec->fcf_indx,
  2126. bf_get(lpfc_fcf_record_fcf_index,
  2127. new_fcf_record));
  2128. /* Choose the new FCF by random selection */
  2129. __lpfc_update_fcf_record(phba, fcf_rec,
  2130. new_fcf_record,
  2131. addr_mode, vlan_id, 0);
  2132. }
  2133. }
  2134. spin_unlock_irq(&phba->hbalock);
  2135. goto read_next_fcf;
  2136. }
  2137. /*
  2138. * This is the first suitable FCF record, choose this record for
  2139. * initial best-fit FCF.
  2140. */
  2141. if (fcf_rec) {
  2142. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  2143. "2840 Update initial FCF candidate "
  2144. "with FCF (x%x)\n",
  2145. bf_get(lpfc_fcf_record_fcf_index,
  2146. new_fcf_record));
  2147. __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
  2148. addr_mode, vlan_id, (boot_flag ?
  2149. BOOT_ENABLE : 0));
  2150. phba->fcf.fcf_flag |= FCF_AVAILABLE;
  2151. /* Setup initial running random FCF selection count */
  2152. phba->fcf.eligible_fcf_cnt = 1;
  2153. /* Seeding the random number generator for random selection */
  2154. seed = (uint32_t)(0xFFFFFFFF & jiffies);
  2155. srandom32(seed);
  2156. }
  2157. spin_unlock_irq(&phba->hbalock);
  2158. goto read_next_fcf;
  2159. read_next_fcf:
  2160. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  2161. if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
  2162. if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
  2163. /*
  2164. * Case of FCF fast failover scan
  2165. */
  2166. /*
  2167. * It has not found any suitable FCF record, cancel
  2168. * FCF scan inprogress, and do nothing
  2169. */
  2170. if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
  2171. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  2172. "2782 No suitable FCF found: "
  2173. "(x%x/x%x)\n",
  2174. phba->fcoe_eventtag_at_fcf_scan,
  2175. bf_get(lpfc_fcf_record_fcf_index,
  2176. new_fcf_record));
  2177. spin_lock_irq(&phba->hbalock);
  2178. if (phba->hba_flag & HBA_DEVLOSS_TMO) {
  2179. phba->hba_flag &= ~FCF_TS_INPROG;
  2180. spin_unlock_irq(&phba->hbalock);
  2181. /* Unregister in-use FCF and rescan */
  2182. lpfc_printf_log(phba, KERN_INFO,
  2183. LOG_FIP,
  2184. "2864 On devloss tmo "
  2185. "unreg in-use FCF and "
  2186. "rescan FCF table\n");
  2187. lpfc_unregister_fcf_rescan(phba);
  2188. return;
  2189. }
  2190. /*
  2191. * Let next new FCF event trigger fast failover
  2192. */
  2193. phba->hba_flag &= ~FCF_TS_INPROG;
  2194. spin_unlock_irq(&phba->hbalock);
  2195. return;
  2196. }
  2197. /*
  2198. * It has found a suitable FCF record that is not
  2199. * the same as in-use FCF record, unregister the
  2200. * in-use FCF record, replace the in-use FCF record
  2201. * with the new FCF record, mark FCF fast failover
  2202. * completed, and then start register the new FCF
  2203. * record.
  2204. */
  2205. /* Unregister the current in-use FCF record */
  2206. lpfc_unregister_fcf(phba);
  2207. /* Replace in-use record with the new record */
  2208. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  2209. "2842 Replace in-use FCF (x%x) "
  2210. "with failover FCF (x%x)\n",
  2211. phba->fcf.current_rec.fcf_indx,
  2212. phba->fcf.failover_rec.fcf_indx);
  2213. memcpy(&phba->fcf.current_rec,
  2214. &phba->fcf.failover_rec,
  2215. sizeof(struct lpfc_fcf_rec));
  2216. /*
  2217. * Mark the fast FCF failover rediscovery completed
  2218. * and the start of the first round of the roundrobin
  2219. * FCF failover.
  2220. */
  2221. spin_lock_irq(&phba->hbalock);
  2222. phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
  2223. spin_unlock_irq(&phba->hbalock);
  2224. /* Register to the new FCF record */
  2225. lpfc_register_fcf(phba);
  2226. } else {
  2227. /*
  2228. * In case of transaction period to fast FCF failover,
  2229. * do nothing when search to the end of the FCF table.
  2230. */
  2231. if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
  2232. (phba->fcf.fcf_flag & FCF_REDISC_PEND))
  2233. return;
  2234. if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
  2235. phba->fcf.fcf_flag & FCF_IN_USE) {
  2236. /*
  2237. * In case the current in-use FCF record no
  2238. * longer existed during FCF discovery that
  2239. * was not triggered by fast FCF failover
  2240. * process, treat it as fast FCF failover.
  2241. */
  2242. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  2243. "2841 In-use FCF record (x%x) "
  2244. "not reported, entering fast "
  2245. "FCF failover mode scanning.\n",
  2246. phba->fcf.current_rec.fcf_indx);
  2247. spin_lock_irq(&phba->hbalock);
  2248. phba->fcf.fcf_flag |= FCF_REDISC_FOV;
  2249. spin_unlock_irq(&phba->hbalock);
  2250. lpfc_sli4_fcf_scan_read_fcf_rec(phba,
  2251. LPFC_FCOE_FCF_GET_FIRST);
  2252. return;
  2253. }
  2254. /* Register to the new FCF record */
  2255. lpfc_register_fcf(phba);
  2256. }
  2257. } else
  2258. lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
  2259. return;
  2260. out:
  2261. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  2262. lpfc_register_fcf(phba);
  2263. return;
  2264. }
  2265. /**
  2266. * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
  2267. * @phba: pointer to lpfc hba data structure.
  2268. * @mboxq: pointer to mailbox object.
  2269. *
  2270. * This is the callback function for FLOGI failure roundrobin FCF failover
  2271. * read FCF record mailbox command from the eligible FCF record bmask for
  2272. * performing the failover. If the FCF read back is not valid/available, it
  2273. * fails through to retrying FLOGI to the currently registered FCF again.
  2274. * Otherwise, if the FCF read back is valid and available, it will set the
  2275. * newly read FCF record to the failover FCF record, unregister currently
  2276. * registered FCF record, copy the failover FCF record to the current
  2277. * FCF record, and then register the current FCF record before proceeding
  2278. * to trying FLOGI on the new failover FCF.
  2279. */
  2280. void
  2281. lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  2282. {
  2283. struct fcf_record *new_fcf_record;
  2284. uint32_t boot_flag, addr_mode;
  2285. uint16_t next_fcf_index, fcf_index;
  2286. uint16_t current_fcf_index;
  2287. uint16_t vlan_id;
  2288. int rc;
  2289. /* If link state is not up, stop the roundrobin failover process */
  2290. if (phba->link_state < LPFC_LINK_UP) {
  2291. spin_lock_irq(&phba->hbalock);
  2292. phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
  2293. phba->hba_flag &= ~FCF_RR_INPROG;
  2294. spin_unlock_irq(&phba->hbalock);
  2295. goto out;
  2296. }
  2297. /* Parse the FCF record from the non-embedded mailbox command */
  2298. new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
  2299. &next_fcf_index);
  2300. if (!new_fcf_record) {
  2301. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  2302. "2766 Mailbox command READ_FCF_RECORD "
  2303. "failed to retrieve a FCF record.\n");
  2304. goto error_out;
  2305. }
  2306. /* Get the needed parameters from FCF record */
  2307. rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
  2308. &addr_mode, &vlan_id);
  2309. /* Log the FCF record information if turned on */
  2310. lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
  2311. next_fcf_index);
  2312. fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
  2313. if (!rc) {
  2314. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  2315. "2848 Remove ineligible FCF (x%x) from "
  2316. "from roundrobin bmask\n", fcf_index);
  2317. /* Clear roundrobin bmask bit for ineligible FCF */
  2318. lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
  2319. /* Perform next round of roundrobin FCF failover */
  2320. fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
  2321. rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
  2322. if (rc)
  2323. goto out;
  2324. goto error_out;
  2325. }
  2326. if (fcf_index == phba->fcf.current_rec.fcf_indx) {
  2327. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  2328. "2760 Perform FLOGI roundrobin FCF failover: "
  2329. "FCF (x%x) back to FCF (x%x)\n",
  2330. phba->fcf.current_rec.fcf_indx, fcf_index);
  2331. /* Wait 500 ms before retrying FLOGI to current FCF */
  2332. msleep(500);
  2333. lpfc_issue_init_vfi(phba->pport);
  2334. goto out;
  2335. }
  2336. /* Upload new FCF record to the failover FCF record */
  2337. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  2338. "2834 Update current FCF (x%x) with new FCF (x%x)\n",
  2339. phba->fcf.failover_rec.fcf_indx, fcf_index);
  2340. spin_lock_irq(&phba->hbalock);
  2341. __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
  2342. new_fcf_record, addr_mode, vlan_id,
  2343. (boot_flag ? BOOT_ENABLE : 0));
  2344. spin_unlock_irq(&phba->hbalock);
  2345. current_fcf_index = phba->fcf.current_rec.fcf_indx;
  2346. /* Unregister the current in-use FCF record */
  2347. lpfc_unregister_fcf(phba);
  2348. /* Replace in-use record with the new record */
  2349. memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
  2350. sizeof(struct lpfc_fcf_rec));
  2351. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  2352. "2783 Perform FLOGI roundrobin FCF failover: FCF "
  2353. "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
  2354. error_out:
  2355. lpfc_register_fcf(phba);
  2356. out:
  2357. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  2358. }
  2359. /**
  2360. * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
  2361. * @phba: pointer to lpfc hba data structure.
  2362. * @mboxq: pointer to mailbox object.
  2363. *
  2364. * This is the callback function of read FCF record mailbox command for
  2365. * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
  2366. * failover when a new FCF event happened. If the FCF read back is
  2367. * valid/available and it passes the connection list check, it updates
  2368. * the bmask for the eligible FCF record for roundrobin failover.
  2369. */
  2370. void
  2371. lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  2372. {
  2373. struct fcf_record *new_fcf_record;
  2374. uint32_t boot_flag, addr_mode;
  2375. uint16_t fcf_index, next_fcf_index;
  2376. uint16_t vlan_id;
  2377. int rc;
  2378. /* If link state is not up, no need to proceed */
  2379. if (phba->link_state < LPFC_LINK_UP)
  2380. goto out;
  2381. /* If FCF discovery period is over, no need to proceed */
  2382. if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
  2383. goto out;
  2384. /* Parse the FCF record from the non-embedded mailbox command */
  2385. new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
  2386. &next_fcf_index);
  2387. if (!new_fcf_record) {
  2388. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  2389. "2767 Mailbox command READ_FCF_RECORD "
  2390. "failed to retrieve a FCF record.\n");
  2391. goto out;
  2392. }
  2393. /* Check the connection list for eligibility */
  2394. rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
  2395. &addr_mode, &vlan_id);
  2396. /* Log the FCF record information if turned on */
  2397. lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
  2398. next_fcf_index);
  2399. if (!rc)
  2400. goto out;
  2401. /* Update the eligible FCF record index bmask */
  2402. fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
  2403. rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
  2404. out:
  2405. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  2406. }
  2407. /**
  2408. * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
  2409. * @phba: pointer to lpfc hba data structure.
  2410. * @mboxq: pointer to mailbox data structure.
  2411. *
  2412. * This function handles completion of init vfi mailbox command.
  2413. */
  2414. void
  2415. lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  2416. {
  2417. struct lpfc_vport *vport = mboxq->vport;
  2418. /*
  2419. * VFI not supported on interface type 0, just do the flogi
  2420. * Also continue if the VFI is in use - just use the same one.
  2421. */
  2422. if (mboxq->u.mb.mbxStatus &&
  2423. (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
  2424. LPFC_SLI_INTF_IF_TYPE_0) &&
  2425. mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
  2426. lpfc_printf_vlog(vport, KERN_ERR,
  2427. LOG_MBOX,
  2428. "2891 Init VFI mailbox failed 0x%x\n",
  2429. mboxq->u.mb.mbxStatus);
  2430. mempool_free(mboxq, phba->mbox_mem_pool);
  2431. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2432. return;
  2433. }
  2434. lpfc_initial_flogi(vport);
  2435. mempool_free(mboxq, phba->mbox_mem_pool);
  2436. return;
  2437. }
  2438. /**
  2439. * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
  2440. * @vport: pointer to lpfc_vport data structure.
  2441. *
  2442. * This function issue a init_vfi mailbox command to initialize the VFI and
  2443. * VPI for the physical port.
  2444. */
  2445. void
  2446. lpfc_issue_init_vfi(struct lpfc_vport *vport)
  2447. {
  2448. LPFC_MBOXQ_t *mboxq;
  2449. int rc;
  2450. struct lpfc_hba *phba = vport->phba;
  2451. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2452. if (!mboxq) {
  2453. lpfc_printf_vlog(vport, KERN_ERR,
  2454. LOG_MBOX, "2892 Failed to allocate "
  2455. "init_vfi mailbox\n");
  2456. return;
  2457. }
  2458. lpfc_init_vfi(mboxq, vport);
  2459. mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
  2460. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  2461. if (rc == MBX_NOT_FINISHED) {
  2462. lpfc_printf_vlog(vport, KERN_ERR,
  2463. LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
  2464. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  2465. }
  2466. }
  2467. /**
  2468. * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
  2469. * @phba: pointer to lpfc hba data structure.
  2470. * @mboxq: pointer to mailbox data structure.
  2471. *
  2472. * This function handles completion of init vpi mailbox command.
  2473. */
  2474. void
  2475. lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  2476. {
  2477. struct lpfc_vport *vport = mboxq->vport;
  2478. struct lpfc_nodelist *ndlp;
  2479. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2480. if (mboxq->u.mb.mbxStatus) {
  2481. lpfc_printf_vlog(vport, KERN_ERR,
  2482. LOG_MBOX,
  2483. "2609 Init VPI mailbox failed 0x%x\n",
  2484. mboxq->u.mb.mbxStatus);
  2485. mempool_free(mboxq, phba->mbox_mem_pool);
  2486. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2487. return;
  2488. }
  2489. spin_lock_irq(shost->host_lock);
  2490. vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
  2491. spin_unlock_irq(shost->host_lock);
  2492. /* If this port is physical port or FDISC is done, do reg_vpi */
  2493. if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
  2494. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  2495. if (!ndlp)
  2496. lpfc_printf_vlog(vport, KERN_ERR,
  2497. LOG_DISCOVERY,
  2498. "2731 Cannot find fabric "
  2499. "controller node\n");
  2500. else
  2501. lpfc_register_new_vport(phba, vport, ndlp);
  2502. mempool_free(mboxq, phba->mbox_mem_pool);
  2503. return;
  2504. }
  2505. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
  2506. lpfc_initial_fdisc(vport);
  2507. else {
  2508. lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
  2509. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  2510. "2606 No NPIV Fabric support\n");
  2511. }
  2512. mempool_free(mboxq, phba->mbox_mem_pool);
  2513. return;
  2514. }
  2515. /**
  2516. * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
  2517. * @vport: pointer to lpfc_vport data structure.
  2518. *
  2519. * This function issue a init_vpi mailbox command to initialize
  2520. * VPI for the vport.
  2521. */
  2522. void
  2523. lpfc_issue_init_vpi(struct lpfc_vport *vport)
  2524. {
  2525. LPFC_MBOXQ_t *mboxq;
  2526. int rc;
  2527. mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
  2528. if (!mboxq) {
  2529. lpfc_printf_vlog(vport, KERN_ERR,
  2530. LOG_MBOX, "2607 Failed to allocate "
  2531. "init_vpi mailbox\n");
  2532. return;
  2533. }
  2534. lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
  2535. mboxq->vport = vport;
  2536. mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
  2537. rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
  2538. if (rc == MBX_NOT_FINISHED) {
  2539. lpfc_printf_vlog(vport, KERN_ERR,
  2540. LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
  2541. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  2542. }
  2543. }
  2544. /**
  2545. * lpfc_start_fdiscs - send fdiscs for each vports on this port.
  2546. * @phba: pointer to lpfc hba data structure.
  2547. *
  2548. * This function loops through the list of vports on the @phba and issues an
  2549. * FDISC if possible.
  2550. */
  2551. void
  2552. lpfc_start_fdiscs(struct lpfc_hba *phba)
  2553. {
  2554. struct lpfc_vport **vports;
  2555. int i;
  2556. vports = lpfc_create_vport_work_array(phba);
  2557. if (vports != NULL) {
  2558. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  2559. if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
  2560. continue;
  2561. /* There are no vpi for this vport */
  2562. if (vports[i]->vpi > phba->max_vpi) {
  2563. lpfc_vport_set_state(vports[i],
  2564. FC_VPORT_FAILED);
  2565. continue;
  2566. }
  2567. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  2568. lpfc_vport_set_state(vports[i],
  2569. FC_VPORT_LINKDOWN);
  2570. continue;
  2571. }
  2572. if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
  2573. lpfc_issue_init_vpi(vports[i]);
  2574. continue;
  2575. }
  2576. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
  2577. lpfc_initial_fdisc(vports[i]);
  2578. else {
  2579. lpfc_vport_set_state(vports[i],
  2580. FC_VPORT_NO_FABRIC_SUPP);
  2581. lpfc_printf_vlog(vports[i], KERN_ERR,
  2582. LOG_ELS,
  2583. "0259 No NPIV "
  2584. "Fabric support\n");
  2585. }
  2586. }
  2587. }
  2588. lpfc_destroy_vport_work_array(phba, vports);
  2589. }
  2590. void
  2591. lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  2592. {
  2593. struct lpfc_dmabuf *dmabuf = mboxq->context1;
  2594. struct lpfc_vport *vport = mboxq->vport;
  2595. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2596. if (mboxq->u.mb.mbxStatus) {
  2597. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  2598. "2018 REG_VFI mbxStatus error x%x "
  2599. "HBA state x%x\n",
  2600. mboxq->u.mb.mbxStatus, vport->port_state);
  2601. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  2602. /* FLOGI failed, use loop map to make discovery list */
  2603. lpfc_disc_list_loopmap(vport);
  2604. /* Start discovery */
  2605. lpfc_disc_start(vport);
  2606. goto fail_free_mem;
  2607. }
  2608. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2609. goto fail_free_mem;
  2610. }
  2611. /* The VPI is implicitly registered when the VFI is registered */
  2612. spin_lock_irq(shost->host_lock);
  2613. vport->vpi_state |= LPFC_VPI_REGISTERED;
  2614. vport->fc_flag |= FC_VFI_REGISTERED;
  2615. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  2616. vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
  2617. spin_unlock_irq(shost->host_lock);
  2618. if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
  2619. /* For private loop just start discovery and we are done. */
  2620. if ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
  2621. !(vport->fc_flag & FC_PUBLIC_LOOP)) {
  2622. /* Use loop map to make discovery list */
  2623. lpfc_disc_list_loopmap(vport);
  2624. /* Start discovery */
  2625. lpfc_disc_start(vport);
  2626. } else {
  2627. lpfc_start_fdiscs(phba);
  2628. lpfc_do_scr_ns_plogi(phba, vport);
  2629. }
  2630. }
  2631. fail_free_mem:
  2632. mempool_free(mboxq, phba->mbox_mem_pool);
  2633. lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
  2634. kfree(dmabuf);
  2635. return;
  2636. }
  2637. static void
  2638. lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2639. {
  2640. MAILBOX_t *mb = &pmb->u.mb;
  2641. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
  2642. struct lpfc_vport *vport = pmb->vport;
  2643. /* Check for error */
  2644. if (mb->mbxStatus) {
  2645. /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
  2646. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  2647. "0319 READ_SPARAM mbxStatus error x%x "
  2648. "hba state x%x>\n",
  2649. mb->mbxStatus, vport->port_state);
  2650. lpfc_linkdown(phba);
  2651. goto out;
  2652. }
  2653. memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
  2654. sizeof (struct serv_parm));
  2655. lpfc_update_vport_wwn(vport);
  2656. if (vport->port_type == LPFC_PHYSICAL_PORT) {
  2657. memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
  2658. memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
  2659. }
  2660. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2661. kfree(mp);
  2662. mempool_free(pmb, phba->mbox_mem_pool);
  2663. return;
  2664. out:
  2665. pmb->context1 = NULL;
  2666. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2667. kfree(mp);
  2668. lpfc_issue_clear_la(phba, vport);
  2669. mempool_free(pmb, phba->mbox_mem_pool);
  2670. return;
  2671. }
  2672. static void
  2673. lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
  2674. {
  2675. struct lpfc_vport *vport = phba->pport;
  2676. LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
  2677. struct Scsi_Host *shost;
  2678. int i;
  2679. struct lpfc_dmabuf *mp;
  2680. int rc;
  2681. struct fcf_record *fcf_record;
  2682. spin_lock_irq(&phba->hbalock);
  2683. switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
  2684. case LPFC_LINK_SPEED_1GHZ:
  2685. case LPFC_LINK_SPEED_2GHZ:
  2686. case LPFC_LINK_SPEED_4GHZ:
  2687. case LPFC_LINK_SPEED_8GHZ:
  2688. case LPFC_LINK_SPEED_10GHZ:
  2689. case LPFC_LINK_SPEED_16GHZ:
  2690. phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
  2691. break;
  2692. default:
  2693. phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
  2694. break;
  2695. }
  2696. phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
  2697. phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
  2698. shost = lpfc_shost_from_vport(vport);
  2699. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  2700. phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
  2701. /* if npiv is enabled and this adapter supports npiv log
  2702. * a message that npiv is not supported in this topology
  2703. */
  2704. if (phba->cfg_enable_npiv && phba->max_vpi)
  2705. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2706. "1309 Link Up Event npiv not supported in loop "
  2707. "topology\n");
  2708. /* Get Loop Map information */
  2709. if (bf_get(lpfc_mbx_read_top_il, la)) {
  2710. spin_lock_irq(shost->host_lock);
  2711. vport->fc_flag |= FC_LBIT;
  2712. spin_unlock_irq(shost->host_lock);
  2713. }
  2714. vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
  2715. i = la->lilpBde64.tus.f.bdeSize;
  2716. if (i == 0) {
  2717. phba->alpa_map[0] = 0;
  2718. } else {
  2719. if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
  2720. int numalpa, j, k;
  2721. union {
  2722. uint8_t pamap[16];
  2723. struct {
  2724. uint32_t wd1;
  2725. uint32_t wd2;
  2726. uint32_t wd3;
  2727. uint32_t wd4;
  2728. } pa;
  2729. } un;
  2730. numalpa = phba->alpa_map[0];
  2731. j = 0;
  2732. while (j < numalpa) {
  2733. memset(un.pamap, 0, 16);
  2734. for (k = 1; j < numalpa; k++) {
  2735. un.pamap[k - 1] =
  2736. phba->alpa_map[j + 1];
  2737. j++;
  2738. if (k == 16)
  2739. break;
  2740. }
  2741. /* Link Up Event ALPA map */
  2742. lpfc_printf_log(phba,
  2743. KERN_WARNING,
  2744. LOG_LINK_EVENT,
  2745. "1304 Link Up Event "
  2746. "ALPA map Data: x%x "
  2747. "x%x x%x x%x\n",
  2748. un.pa.wd1, un.pa.wd2,
  2749. un.pa.wd3, un.pa.wd4);
  2750. }
  2751. }
  2752. }
  2753. } else {
  2754. if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
  2755. if (phba->max_vpi && phba->cfg_enable_npiv &&
  2756. (phba->sli_rev >= LPFC_SLI_REV3))
  2757. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  2758. }
  2759. vport->fc_myDID = phba->fc_pref_DID;
  2760. spin_lock_irq(shost->host_lock);
  2761. vport->fc_flag |= FC_LBIT;
  2762. spin_unlock_irq(shost->host_lock);
  2763. }
  2764. spin_unlock_irq(&phba->hbalock);
  2765. lpfc_linkup(phba);
  2766. sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2767. if (!sparam_mbox)
  2768. goto out;
  2769. rc = lpfc_read_sparam(phba, sparam_mbox, 0);
  2770. if (rc) {
  2771. mempool_free(sparam_mbox, phba->mbox_mem_pool);
  2772. goto out;
  2773. }
  2774. sparam_mbox->vport = vport;
  2775. sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
  2776. rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
  2777. if (rc == MBX_NOT_FINISHED) {
  2778. mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
  2779. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2780. kfree(mp);
  2781. mempool_free(sparam_mbox, phba->mbox_mem_pool);
  2782. goto out;
  2783. }
  2784. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  2785. cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2786. if (!cfglink_mbox)
  2787. goto out;
  2788. vport->port_state = LPFC_LOCAL_CFG_LINK;
  2789. lpfc_config_link(phba, cfglink_mbox);
  2790. cfglink_mbox->vport = vport;
  2791. cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
  2792. rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
  2793. if (rc == MBX_NOT_FINISHED) {
  2794. mempool_free(cfglink_mbox, phba->mbox_mem_pool);
  2795. goto out;
  2796. }
  2797. } else {
  2798. vport->port_state = LPFC_VPORT_UNKNOWN;
  2799. /*
  2800. * Add the driver's default FCF record at FCF index 0 now. This
  2801. * is phase 1 implementation that support FCF index 0 and driver
  2802. * defaults.
  2803. */
  2804. if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
  2805. fcf_record = kzalloc(sizeof(struct fcf_record),
  2806. GFP_KERNEL);
  2807. if (unlikely(!fcf_record)) {
  2808. lpfc_printf_log(phba, KERN_ERR,
  2809. LOG_MBOX | LOG_SLI,
  2810. "2554 Could not allocate memory for "
  2811. "fcf record\n");
  2812. rc = -ENODEV;
  2813. goto out;
  2814. }
  2815. lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
  2816. LPFC_FCOE_FCF_DEF_INDEX);
  2817. rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
  2818. if (unlikely(rc)) {
  2819. lpfc_printf_log(phba, KERN_ERR,
  2820. LOG_MBOX | LOG_SLI,
  2821. "2013 Could not manually add FCF "
  2822. "record 0, status %d\n", rc);
  2823. rc = -ENODEV;
  2824. kfree(fcf_record);
  2825. goto out;
  2826. }
  2827. kfree(fcf_record);
  2828. }
  2829. /*
  2830. * The driver is expected to do FIP/FCF. Call the port
  2831. * and get the FCF Table.
  2832. */
  2833. spin_lock_irq(&phba->hbalock);
  2834. if (phba->hba_flag & FCF_TS_INPROG) {
  2835. spin_unlock_irq(&phba->hbalock);
  2836. return;
  2837. }
  2838. /* This is the initial FCF discovery scan */
  2839. phba->fcf.fcf_flag |= FCF_INIT_DISC;
  2840. spin_unlock_irq(&phba->hbalock);
  2841. lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
  2842. "2778 Start FCF table scan at linkup\n");
  2843. rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
  2844. LPFC_FCOE_FCF_GET_FIRST);
  2845. if (rc) {
  2846. spin_lock_irq(&phba->hbalock);
  2847. phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
  2848. spin_unlock_irq(&phba->hbalock);
  2849. goto out;
  2850. }
  2851. /* Reset FCF roundrobin bmask for new discovery */
  2852. lpfc_sli4_clear_fcf_rr_bmask(phba);
  2853. }
  2854. return;
  2855. out:
  2856. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2857. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  2858. "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
  2859. vport->port_state, sparam_mbox, cfglink_mbox);
  2860. lpfc_issue_clear_la(phba, vport);
  2861. return;
  2862. }
  2863. static void
  2864. lpfc_enable_la(struct lpfc_hba *phba)
  2865. {
  2866. uint32_t control;
  2867. struct lpfc_sli *psli = &phba->sli;
  2868. spin_lock_irq(&phba->hbalock);
  2869. psli->sli_flag |= LPFC_PROCESS_LA;
  2870. if (phba->sli_rev <= LPFC_SLI_REV3) {
  2871. control = readl(phba->HCregaddr);
  2872. control |= HC_LAINT_ENA;
  2873. writel(control, phba->HCregaddr);
  2874. readl(phba->HCregaddr); /* flush */
  2875. }
  2876. spin_unlock_irq(&phba->hbalock);
  2877. }
  2878. static void
  2879. lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
  2880. {
  2881. lpfc_linkdown(phba);
  2882. lpfc_enable_la(phba);
  2883. lpfc_unregister_unused_fcf(phba);
  2884. /* turn on Link Attention interrupts - no CLEAR_LA needed */
  2885. }
  2886. /*
  2887. * This routine handles processing a READ_TOPOLOGY mailbox
  2888. * command upon completion. It is setup in the LPFC_MBOXQ
  2889. * as the completion routine when the command is
  2890. * handed off to the SLI layer.
  2891. */
  2892. void
  2893. lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2894. {
  2895. struct lpfc_vport *vport = pmb->vport;
  2896. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2897. struct lpfc_mbx_read_top *la;
  2898. MAILBOX_t *mb = &pmb->u.mb;
  2899. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  2900. /* Unblock ELS traffic */
  2901. phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
  2902. /* Check for error */
  2903. if (mb->mbxStatus) {
  2904. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  2905. "1307 READ_LA mbox error x%x state x%x\n",
  2906. mb->mbxStatus, vport->port_state);
  2907. lpfc_mbx_issue_link_down(phba);
  2908. phba->link_state = LPFC_HBA_ERROR;
  2909. goto lpfc_mbx_cmpl_read_topology_free_mbuf;
  2910. }
  2911. la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
  2912. memcpy(&phba->alpa_map[0], mp->virt, 128);
  2913. spin_lock_irq(shost->host_lock);
  2914. if (bf_get(lpfc_mbx_read_top_pb, la))
  2915. vport->fc_flag |= FC_BYPASSED_MODE;
  2916. else
  2917. vport->fc_flag &= ~FC_BYPASSED_MODE;
  2918. spin_unlock_irq(shost->host_lock);
  2919. if ((phba->fc_eventTag < la->eventTag) ||
  2920. (phba->fc_eventTag == la->eventTag)) {
  2921. phba->fc_stat.LinkMultiEvent++;
  2922. if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
  2923. if (phba->fc_eventTag != 0)
  2924. lpfc_linkdown(phba);
  2925. }
  2926. phba->fc_eventTag = la->eventTag;
  2927. spin_lock_irq(&phba->hbalock);
  2928. if (bf_get(lpfc_mbx_read_top_mm, la))
  2929. phba->sli.sli_flag |= LPFC_MENLO_MAINT;
  2930. else
  2931. phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
  2932. spin_unlock_irq(&phba->hbalock);
  2933. phba->link_events++;
  2934. if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
  2935. (!bf_get(lpfc_mbx_read_top_mm, la))) {
  2936. phba->fc_stat.LinkUp++;
  2937. if (phba->link_flag & LS_LOOPBACK_MODE) {
  2938. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2939. "1306 Link Up Event in loop back mode "
  2940. "x%x received Data: x%x x%x x%x x%x\n",
  2941. la->eventTag, phba->fc_eventTag,
  2942. bf_get(lpfc_mbx_read_top_alpa_granted,
  2943. la),
  2944. bf_get(lpfc_mbx_read_top_link_spd, la),
  2945. phba->alpa_map[0]);
  2946. } else {
  2947. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2948. "1303 Link Up Event x%x received "
  2949. "Data: x%x x%x x%x x%x x%x x%x %d\n",
  2950. la->eventTag, phba->fc_eventTag,
  2951. bf_get(lpfc_mbx_read_top_alpa_granted,
  2952. la),
  2953. bf_get(lpfc_mbx_read_top_link_spd, la),
  2954. phba->alpa_map[0],
  2955. bf_get(lpfc_mbx_read_top_mm, la),
  2956. bf_get(lpfc_mbx_read_top_fa, la),
  2957. phba->wait_4_mlo_maint_flg);
  2958. }
  2959. lpfc_mbx_process_link_up(phba, la);
  2960. } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
  2961. LPFC_ATT_LINK_DOWN) {
  2962. phba->fc_stat.LinkDown++;
  2963. if (phba->link_flag & LS_LOOPBACK_MODE) {
  2964. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2965. "1308 Link Down Event in loop back mode "
  2966. "x%x received "
  2967. "Data: x%x x%x x%x\n",
  2968. la->eventTag, phba->fc_eventTag,
  2969. phba->pport->port_state, vport->fc_flag);
  2970. }
  2971. else {
  2972. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2973. "1305 Link Down Event x%x received "
  2974. "Data: x%x x%x x%x x%x x%x\n",
  2975. la->eventTag, phba->fc_eventTag,
  2976. phba->pport->port_state, vport->fc_flag,
  2977. bf_get(lpfc_mbx_read_top_mm, la),
  2978. bf_get(lpfc_mbx_read_top_fa, la));
  2979. }
  2980. lpfc_mbx_issue_link_down(phba);
  2981. }
  2982. if ((bf_get(lpfc_mbx_read_top_mm, la)) &&
  2983. (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)) {
  2984. if (phba->link_state != LPFC_LINK_DOWN) {
  2985. phba->fc_stat.LinkDown++;
  2986. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2987. "1312 Link Down Event x%x received "
  2988. "Data: x%x x%x x%x\n",
  2989. la->eventTag, phba->fc_eventTag,
  2990. phba->pport->port_state, vport->fc_flag);
  2991. lpfc_mbx_issue_link_down(phba);
  2992. } else
  2993. lpfc_enable_la(phba);
  2994. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2995. "1310 Menlo Maint Mode Link up Event x%x rcvd "
  2996. "Data: x%x x%x x%x\n",
  2997. la->eventTag, phba->fc_eventTag,
  2998. phba->pport->port_state, vport->fc_flag);
  2999. /*
  3000. * The cmnd that triggered this will be waiting for this
  3001. * signal.
  3002. */
  3003. /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
  3004. if (phba->wait_4_mlo_maint_flg) {
  3005. phba->wait_4_mlo_maint_flg = 0;
  3006. wake_up_interruptible(&phba->wait_4_mlo_m_q);
  3007. }
  3008. }
  3009. if (bf_get(lpfc_mbx_read_top_fa, la)) {
  3010. if (bf_get(lpfc_mbx_read_top_mm, la))
  3011. lpfc_issue_clear_la(phba, vport);
  3012. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  3013. "1311 fa %d\n",
  3014. bf_get(lpfc_mbx_read_top_fa, la));
  3015. }
  3016. lpfc_mbx_cmpl_read_topology_free_mbuf:
  3017. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3018. kfree(mp);
  3019. mempool_free(pmb, phba->mbox_mem_pool);
  3020. return;
  3021. }
  3022. /*
  3023. * This routine handles processing a REG_LOGIN mailbox
  3024. * command upon completion. It is setup in the LPFC_MBOXQ
  3025. * as the completion routine when the command is
  3026. * handed off to the SLI layer.
  3027. */
  3028. void
  3029. lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  3030. {
  3031. struct lpfc_vport *vport = pmb->vport;
  3032. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  3033. struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
  3034. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3035. pmb->context1 = NULL;
  3036. pmb->context2 = NULL;
  3037. if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
  3038. ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
  3039. if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
  3040. ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
  3041. /* We rcvd a rscn after issuing this
  3042. * mbox reg login, we may have cycled
  3043. * back through the state and be
  3044. * back at reg login state so this
  3045. * mbox needs to be ignored becase
  3046. * there is another reg login in
  3047. * process.
  3048. */
  3049. spin_lock_irq(shost->host_lock);
  3050. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  3051. spin_unlock_irq(shost->host_lock);
  3052. } else
  3053. /* Good status, call state machine */
  3054. lpfc_disc_state_machine(vport, ndlp, pmb,
  3055. NLP_EVT_CMPL_REG_LOGIN);
  3056. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3057. kfree(mp);
  3058. mempool_free(pmb, phba->mbox_mem_pool);
  3059. /* decrement the node reference count held for this callback
  3060. * function.
  3061. */
  3062. lpfc_nlp_put(ndlp);
  3063. return;
  3064. }
  3065. static void
  3066. lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  3067. {
  3068. MAILBOX_t *mb = &pmb->u.mb;
  3069. struct lpfc_vport *vport = pmb->vport;
  3070. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3071. switch (mb->mbxStatus) {
  3072. case 0x0011:
  3073. case 0x0020:
  3074. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  3075. "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
  3076. mb->mbxStatus);
  3077. break;
  3078. /* If VPI is busy, reset the HBA */
  3079. case 0x9700:
  3080. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
  3081. "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
  3082. vport->vpi, mb->mbxStatus);
  3083. if (!(phba->pport->load_flag & FC_UNLOADING))
  3084. lpfc_workq_post_event(phba, NULL, NULL,
  3085. LPFC_EVT_RESET_HBA);
  3086. }
  3087. spin_lock_irq(shost->host_lock);
  3088. vport->vpi_state &= ~LPFC_VPI_REGISTERED;
  3089. vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  3090. spin_unlock_irq(shost->host_lock);
  3091. vport->unreg_vpi_cmpl = VPORT_OK;
  3092. mempool_free(pmb, phba->mbox_mem_pool);
  3093. lpfc_cleanup_vports_rrqs(vport, NULL);
  3094. /*
  3095. * This shost reference might have been taken at the beginning of
  3096. * lpfc_vport_delete()
  3097. */
  3098. if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
  3099. scsi_host_put(shost);
  3100. }
  3101. int
  3102. lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
  3103. {
  3104. struct lpfc_hba *phba = vport->phba;
  3105. LPFC_MBOXQ_t *mbox;
  3106. int rc;
  3107. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3108. if (!mbox)
  3109. return 1;
  3110. lpfc_unreg_vpi(phba, vport->vpi, mbox);
  3111. mbox->vport = vport;
  3112. mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
  3113. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  3114. if (rc == MBX_NOT_FINISHED) {
  3115. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
  3116. "1800 Could not issue unreg_vpi\n");
  3117. mempool_free(mbox, phba->mbox_mem_pool);
  3118. vport->unreg_vpi_cmpl = VPORT_ERROR;
  3119. return rc;
  3120. }
  3121. return 0;
  3122. }
  3123. static void
  3124. lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  3125. {
  3126. struct lpfc_vport *vport = pmb->vport;
  3127. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3128. MAILBOX_t *mb = &pmb->u.mb;
  3129. switch (mb->mbxStatus) {
  3130. case 0x0011:
  3131. case 0x9601:
  3132. case 0x9602:
  3133. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  3134. "0912 cmpl_reg_vpi, mb status = 0x%x\n",
  3135. mb->mbxStatus);
  3136. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  3137. spin_lock_irq(shost->host_lock);
  3138. vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
  3139. spin_unlock_irq(shost->host_lock);
  3140. vport->fc_myDID = 0;
  3141. goto out;
  3142. }
  3143. spin_lock_irq(shost->host_lock);
  3144. vport->vpi_state |= LPFC_VPI_REGISTERED;
  3145. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  3146. spin_unlock_irq(shost->host_lock);
  3147. vport->num_disc_nodes = 0;
  3148. /* go thru NPR list and issue ELS PLOGIs */
  3149. if (vport->fc_npr_cnt)
  3150. lpfc_els_disc_plogi(vport);
  3151. if (!vport->num_disc_nodes) {
  3152. spin_lock_irq(shost->host_lock);
  3153. vport->fc_flag &= ~FC_NDISC_ACTIVE;
  3154. spin_unlock_irq(shost->host_lock);
  3155. lpfc_can_disctmo(vport);
  3156. }
  3157. vport->port_state = LPFC_VPORT_READY;
  3158. out:
  3159. mempool_free(pmb, phba->mbox_mem_pool);
  3160. return;
  3161. }
  3162. /**
  3163. * lpfc_create_static_vport - Read HBA config region to create static vports.
  3164. * @phba: pointer to lpfc hba data structure.
  3165. *
  3166. * This routine issue a DUMP mailbox command for config region 22 to get
  3167. * the list of static vports to be created. The function create vports
  3168. * based on the information returned from the HBA.
  3169. **/
  3170. void
  3171. lpfc_create_static_vport(struct lpfc_hba *phba)
  3172. {
  3173. LPFC_MBOXQ_t *pmb = NULL;
  3174. MAILBOX_t *mb;
  3175. struct static_vport_info *vport_info;
  3176. int rc = 0, i;
  3177. struct fc_vport_identifiers vport_id;
  3178. struct fc_vport *new_fc_vport;
  3179. struct Scsi_Host *shost;
  3180. struct lpfc_vport *vport;
  3181. uint16_t offset = 0;
  3182. uint8_t *vport_buff;
  3183. struct lpfc_dmabuf *mp;
  3184. uint32_t byte_count = 0;
  3185. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3186. if (!pmb) {
  3187. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3188. "0542 lpfc_create_static_vport failed to"
  3189. " allocate mailbox memory\n");
  3190. return;
  3191. }
  3192. mb = &pmb->u.mb;
  3193. vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
  3194. if (!vport_info) {
  3195. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3196. "0543 lpfc_create_static_vport failed to"
  3197. " allocate vport_info\n");
  3198. mempool_free(pmb, phba->mbox_mem_pool);
  3199. return;
  3200. }
  3201. vport_buff = (uint8_t *) vport_info;
  3202. do {
  3203. if (lpfc_dump_static_vport(phba, pmb, offset))
  3204. goto out;
  3205. pmb->vport = phba->pport;
  3206. rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
  3207. if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
  3208. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3209. "0544 lpfc_create_static_vport failed to"
  3210. " issue dump mailbox command ret 0x%x "
  3211. "status 0x%x\n",
  3212. rc, mb->mbxStatus);
  3213. goto out;
  3214. }
  3215. if (phba->sli_rev == LPFC_SLI_REV4) {
  3216. byte_count = pmb->u.mqe.un.mb_words[5];
  3217. mp = (struct lpfc_dmabuf *) pmb->context2;
  3218. if (byte_count > sizeof(struct static_vport_info) -
  3219. offset)
  3220. byte_count = sizeof(struct static_vport_info)
  3221. - offset;
  3222. memcpy(vport_buff + offset, mp->virt, byte_count);
  3223. offset += byte_count;
  3224. } else {
  3225. if (mb->un.varDmp.word_cnt >
  3226. sizeof(struct static_vport_info) - offset)
  3227. mb->un.varDmp.word_cnt =
  3228. sizeof(struct static_vport_info)
  3229. - offset;
  3230. byte_count = mb->un.varDmp.word_cnt;
  3231. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  3232. vport_buff + offset,
  3233. byte_count);
  3234. offset += byte_count;
  3235. }
  3236. } while (byte_count &&
  3237. offset < sizeof(struct static_vport_info));
  3238. if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
  3239. ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
  3240. != VPORT_INFO_REV)) {
  3241. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3242. "0545 lpfc_create_static_vport bad"
  3243. " information header 0x%x 0x%x\n",
  3244. le32_to_cpu(vport_info->signature),
  3245. le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
  3246. goto out;
  3247. }
  3248. shost = lpfc_shost_from_vport(phba->pport);
  3249. for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
  3250. memset(&vport_id, 0, sizeof(vport_id));
  3251. vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
  3252. vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
  3253. if (!vport_id.port_name || !vport_id.node_name)
  3254. continue;
  3255. vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
  3256. vport_id.vport_type = FC_PORTTYPE_NPIV;
  3257. vport_id.disable = false;
  3258. new_fc_vport = fc_vport_create(shost, 0, &vport_id);
  3259. if (!new_fc_vport) {
  3260. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3261. "0546 lpfc_create_static_vport failed to"
  3262. " create vport\n");
  3263. continue;
  3264. }
  3265. vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
  3266. vport->vport_flag |= STATIC_VPORT;
  3267. }
  3268. out:
  3269. kfree(vport_info);
  3270. if (rc != MBX_TIMEOUT) {
  3271. if (pmb->context2) {
  3272. mp = (struct lpfc_dmabuf *) pmb->context2;
  3273. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3274. kfree(mp);
  3275. }
  3276. mempool_free(pmb, phba->mbox_mem_pool);
  3277. }
  3278. return;
  3279. }
  3280. /*
  3281. * This routine handles processing a Fabric REG_LOGIN mailbox
  3282. * command upon completion. It is setup in the LPFC_MBOXQ
  3283. * as the completion routine when the command is
  3284. * handed off to the SLI layer.
  3285. */
  3286. void
  3287. lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  3288. {
  3289. struct lpfc_vport *vport = pmb->vport;
  3290. MAILBOX_t *mb = &pmb->u.mb;
  3291. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  3292. struct lpfc_nodelist *ndlp;
  3293. struct Scsi_Host *shost;
  3294. ndlp = (struct lpfc_nodelist *) pmb->context2;
  3295. pmb->context1 = NULL;
  3296. pmb->context2 = NULL;
  3297. if (mb->mbxStatus) {
  3298. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  3299. "0258 Register Fabric login error: 0x%x\n",
  3300. mb->mbxStatus);
  3301. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3302. kfree(mp);
  3303. mempool_free(pmb, phba->mbox_mem_pool);
  3304. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  3305. /* FLOGI failed, use loop map to make discovery list */
  3306. lpfc_disc_list_loopmap(vport);
  3307. /* Start discovery */
  3308. lpfc_disc_start(vport);
  3309. /* Decrement the reference count to ndlp after the
  3310. * reference to the ndlp are done.
  3311. */
  3312. lpfc_nlp_put(ndlp);
  3313. return;
  3314. }
  3315. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  3316. /* Decrement the reference count to ndlp after the reference
  3317. * to the ndlp are done.
  3318. */
  3319. lpfc_nlp_put(ndlp);
  3320. return;
  3321. }
  3322. if (phba->sli_rev < LPFC_SLI_REV4)
  3323. ndlp->nlp_rpi = mb->un.varWords[0];
  3324. ndlp->nlp_flag |= NLP_RPI_REGISTERED;
  3325. ndlp->nlp_type |= NLP_FABRIC;
  3326. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  3327. if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
  3328. /* when physical port receive logo donot start
  3329. * vport discovery */
  3330. if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
  3331. lpfc_start_fdiscs(phba);
  3332. else {
  3333. shost = lpfc_shost_from_vport(vport);
  3334. spin_lock_irq(shost->host_lock);
  3335. vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
  3336. spin_unlock_irq(shost->host_lock);
  3337. }
  3338. lpfc_do_scr_ns_plogi(phba, vport);
  3339. }
  3340. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3341. kfree(mp);
  3342. mempool_free(pmb, phba->mbox_mem_pool);
  3343. /* Drop the reference count from the mbox at the end after
  3344. * all the current reference to the ndlp have been done.
  3345. */
  3346. lpfc_nlp_put(ndlp);
  3347. return;
  3348. }
  3349. /*
  3350. * This routine handles processing a NameServer REG_LOGIN mailbox
  3351. * command upon completion. It is setup in the LPFC_MBOXQ
  3352. * as the completion routine when the command is
  3353. * handed off to the SLI layer.
  3354. */
  3355. void
  3356. lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  3357. {
  3358. MAILBOX_t *mb = &pmb->u.mb;
  3359. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  3360. struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
  3361. struct lpfc_vport *vport = pmb->vport;
  3362. pmb->context1 = NULL;
  3363. pmb->context2 = NULL;
  3364. if (mb->mbxStatus) {
  3365. out:
  3366. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  3367. "0260 Register NameServer error: 0x%x\n",
  3368. mb->mbxStatus);
  3369. /* decrement the node reference count held for this
  3370. * callback function.
  3371. */
  3372. lpfc_nlp_put(ndlp);
  3373. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3374. kfree(mp);
  3375. mempool_free(pmb, phba->mbox_mem_pool);
  3376. /* If no other thread is using the ndlp, free it */
  3377. lpfc_nlp_not_used(ndlp);
  3378. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  3379. /*
  3380. * RegLogin failed, use loop map to make discovery
  3381. * list
  3382. */
  3383. lpfc_disc_list_loopmap(vport);
  3384. /* Start discovery */
  3385. lpfc_disc_start(vport);
  3386. return;
  3387. }
  3388. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  3389. return;
  3390. }
  3391. if (phba->sli_rev < LPFC_SLI_REV4)
  3392. ndlp->nlp_rpi = mb->un.varWords[0];
  3393. ndlp->nlp_flag |= NLP_RPI_REGISTERED;
  3394. ndlp->nlp_type |= NLP_FABRIC;
  3395. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  3396. if (vport->port_state < LPFC_VPORT_READY) {
  3397. /* Link up discovery requires Fabric registration. */
  3398. lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
  3399. lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
  3400. lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
  3401. lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
  3402. lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
  3403. /* Issue SCR just before NameServer GID_FT Query */
  3404. lpfc_issue_els_scr(vport, SCR_DID, 0);
  3405. }
  3406. vport->fc_ns_retry = 0;
  3407. /* Good status, issue CT Request to NameServer */
  3408. if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
  3409. /* Cannot issue NameServer Query, so finish up discovery */
  3410. goto out;
  3411. }
  3412. /* decrement the node reference count held for this
  3413. * callback function.
  3414. */
  3415. lpfc_nlp_put(ndlp);
  3416. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3417. kfree(mp);
  3418. mempool_free(pmb, phba->mbox_mem_pool);
  3419. return;
  3420. }
  3421. static void
  3422. lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  3423. {
  3424. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3425. struct fc_rport *rport;
  3426. struct lpfc_rport_data *rdata;
  3427. struct fc_rport_identifiers rport_ids;
  3428. struct lpfc_hba *phba = vport->phba;
  3429. /* Remote port has reappeared. Re-register w/ FC transport */
  3430. rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
  3431. rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
  3432. rport_ids.port_id = ndlp->nlp_DID;
  3433. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  3434. /*
  3435. * We leave our node pointer in rport->dd_data when we unregister a
  3436. * FCP target port. But fc_remote_port_add zeros the space to which
  3437. * rport->dd_data points. So, if we're reusing a previously
  3438. * registered port, drop the reference that we took the last time we
  3439. * registered the port.
  3440. */
  3441. if (ndlp->rport && ndlp->rport->dd_data &&
  3442. ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
  3443. lpfc_nlp_put(ndlp);
  3444. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
  3445. "rport add: did:x%x flg:x%x type x%x",
  3446. ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
  3447. /* Don't add the remote port if unloading. */
  3448. if (vport->load_flag & FC_UNLOADING)
  3449. return;
  3450. ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
  3451. if (!rport || !get_device(&rport->dev)) {
  3452. dev_printk(KERN_WARNING, &phba->pcidev->dev,
  3453. "Warning: fc_remote_port_add failed\n");
  3454. return;
  3455. }
  3456. /* initialize static port data */
  3457. rport->maxframe_size = ndlp->nlp_maxframe;
  3458. rport->supported_classes = ndlp->nlp_class_sup;
  3459. rdata = rport->dd_data;
  3460. rdata->pnode = lpfc_nlp_get(ndlp);
  3461. if (ndlp->nlp_type & NLP_FCP_TARGET)
  3462. rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
  3463. if (ndlp->nlp_type & NLP_FCP_INITIATOR)
  3464. rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  3465. if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
  3466. fc_remote_port_rolechg(rport, rport_ids.roles);
  3467. if ((rport->scsi_target_id != -1) &&
  3468. (rport->scsi_target_id < LPFC_MAX_TARGET)) {
  3469. ndlp->nlp_sid = rport->scsi_target_id;
  3470. }
  3471. return;
  3472. }
  3473. static void
  3474. lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
  3475. {
  3476. struct fc_rport *rport = ndlp->rport;
  3477. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
  3478. "rport delete: did:x%x flg:x%x type x%x",
  3479. ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
  3480. fc_remote_port_delete(rport);
  3481. return;
  3482. }
  3483. static void
  3484. lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
  3485. {
  3486. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3487. spin_lock_irq(shost->host_lock);
  3488. switch (state) {
  3489. case NLP_STE_UNUSED_NODE:
  3490. vport->fc_unused_cnt += count;
  3491. break;
  3492. case NLP_STE_PLOGI_ISSUE:
  3493. vport->fc_plogi_cnt += count;
  3494. break;
  3495. case NLP_STE_ADISC_ISSUE:
  3496. vport->fc_adisc_cnt += count;
  3497. break;
  3498. case NLP_STE_REG_LOGIN_ISSUE:
  3499. vport->fc_reglogin_cnt += count;
  3500. break;
  3501. case NLP_STE_PRLI_ISSUE:
  3502. vport->fc_prli_cnt += count;
  3503. break;
  3504. case NLP_STE_UNMAPPED_NODE:
  3505. vport->fc_unmap_cnt += count;
  3506. break;
  3507. case NLP_STE_MAPPED_NODE:
  3508. vport->fc_map_cnt += count;
  3509. break;
  3510. case NLP_STE_NPR_NODE:
  3511. vport->fc_npr_cnt += count;
  3512. break;
  3513. }
  3514. spin_unlock_irq(shost->host_lock);
  3515. }
  3516. static void
  3517. lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  3518. int old_state, int new_state)
  3519. {
  3520. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3521. if (new_state == NLP_STE_UNMAPPED_NODE) {
  3522. ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
  3523. ndlp->nlp_type |= NLP_FC_NODE;
  3524. }
  3525. if (new_state == NLP_STE_MAPPED_NODE)
  3526. ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
  3527. if (new_state == NLP_STE_NPR_NODE)
  3528. ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
  3529. /* Transport interface */
  3530. if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
  3531. old_state == NLP_STE_UNMAPPED_NODE)) {
  3532. vport->phba->nport_event_cnt++;
  3533. lpfc_unregister_remote_port(ndlp);
  3534. }
  3535. if (new_state == NLP_STE_MAPPED_NODE ||
  3536. new_state == NLP_STE_UNMAPPED_NODE) {
  3537. vport->phba->nport_event_cnt++;
  3538. /*
  3539. * Tell the fc transport about the port, if we haven't
  3540. * already. If we have, and it's a scsi entity, be
  3541. * sure to unblock any attached scsi devices
  3542. */
  3543. lpfc_register_remote_port(vport, ndlp);
  3544. }
  3545. if ((new_state == NLP_STE_MAPPED_NODE) &&
  3546. (vport->stat_data_enabled)) {
  3547. /*
  3548. * A new target is discovered, if there is no buffer for
  3549. * statistical data collection allocate buffer.
  3550. */
  3551. ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
  3552. sizeof(struct lpfc_scsicmd_bkt),
  3553. GFP_KERNEL);
  3554. if (!ndlp->lat_data)
  3555. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
  3556. "0286 lpfc_nlp_state_cleanup failed to "
  3557. "allocate statistical data buffer DID "
  3558. "0x%x\n", ndlp->nlp_DID);
  3559. }
  3560. /*
  3561. * if we added to Mapped list, but the remote port
  3562. * registration failed or assigned a target id outside
  3563. * our presentable range - move the node to the
  3564. * Unmapped List
  3565. */
  3566. if (new_state == NLP_STE_MAPPED_NODE &&
  3567. (!ndlp->rport ||
  3568. ndlp->rport->scsi_target_id == -1 ||
  3569. ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
  3570. spin_lock_irq(shost->host_lock);
  3571. ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
  3572. spin_unlock_irq(shost->host_lock);
  3573. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  3574. }
  3575. }
  3576. static char *
  3577. lpfc_nlp_state_name(char *buffer, size_t size, int state)
  3578. {
  3579. static char *states[] = {
  3580. [NLP_STE_UNUSED_NODE] = "UNUSED",
  3581. [NLP_STE_PLOGI_ISSUE] = "PLOGI",
  3582. [NLP_STE_ADISC_ISSUE] = "ADISC",
  3583. [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
  3584. [NLP_STE_PRLI_ISSUE] = "PRLI",
  3585. [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
  3586. [NLP_STE_MAPPED_NODE] = "MAPPED",
  3587. [NLP_STE_NPR_NODE] = "NPR",
  3588. };
  3589. if (state < NLP_STE_MAX_STATE && states[state])
  3590. strlcpy(buffer, states[state], size);
  3591. else
  3592. snprintf(buffer, size, "unknown (%d)", state);
  3593. return buffer;
  3594. }
  3595. void
  3596. lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  3597. int state)
  3598. {
  3599. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3600. int old_state = ndlp->nlp_state;
  3601. char name1[16], name2[16];
  3602. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  3603. "0904 NPort state transition x%06x, %s -> %s\n",
  3604. ndlp->nlp_DID,
  3605. lpfc_nlp_state_name(name1, sizeof(name1), old_state),
  3606. lpfc_nlp_state_name(name2, sizeof(name2), state));
  3607. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
  3608. "node statechg did:x%x old:%d ste:%d",
  3609. ndlp->nlp_DID, old_state, state);
  3610. if (old_state == NLP_STE_NPR_NODE &&
  3611. state != NLP_STE_NPR_NODE)
  3612. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  3613. if (old_state == NLP_STE_UNMAPPED_NODE) {
  3614. ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
  3615. ndlp->nlp_type &= ~NLP_FC_NODE;
  3616. }
  3617. if (list_empty(&ndlp->nlp_listp)) {
  3618. spin_lock_irq(shost->host_lock);
  3619. list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
  3620. spin_unlock_irq(shost->host_lock);
  3621. } else if (old_state)
  3622. lpfc_nlp_counters(vport, old_state, -1);
  3623. ndlp->nlp_state = state;
  3624. lpfc_nlp_counters(vport, state, 1);
  3625. lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
  3626. }
  3627. void
  3628. lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  3629. {
  3630. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3631. if (list_empty(&ndlp->nlp_listp)) {
  3632. spin_lock_irq(shost->host_lock);
  3633. list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
  3634. spin_unlock_irq(shost->host_lock);
  3635. }
  3636. }
  3637. void
  3638. lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  3639. {
  3640. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3641. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  3642. if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
  3643. lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
  3644. spin_lock_irq(shost->host_lock);
  3645. list_del_init(&ndlp->nlp_listp);
  3646. spin_unlock_irq(shost->host_lock);
  3647. lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
  3648. NLP_STE_UNUSED_NODE);
  3649. }
  3650. static void
  3651. lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  3652. {
  3653. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  3654. if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
  3655. lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
  3656. lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
  3657. NLP_STE_UNUSED_NODE);
  3658. }
  3659. /**
  3660. * lpfc_initialize_node - Initialize all fields of node object
  3661. * @vport: Pointer to Virtual Port object.
  3662. * @ndlp: Pointer to FC node object.
  3663. * @did: FC_ID of the node.
  3664. *
  3665. * This function is always called when node object need to be initialized.
  3666. * It initializes all the fields of the node object. Although the reference
  3667. * to phba from @ndlp can be obtained indirectly through it's reference to
  3668. * @vport, a direct reference to phba is taken here by @ndlp. This is due
  3669. * to the life-span of the @ndlp might go beyond the existence of @vport as
  3670. * the final release of ndlp is determined by its reference count. And, the
  3671. * operation on @ndlp needs the reference to phba.
  3672. **/
  3673. static inline void
  3674. lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  3675. uint32_t did)
  3676. {
  3677. INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
  3678. INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
  3679. init_timer(&ndlp->nlp_delayfunc);
  3680. ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
  3681. ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
  3682. ndlp->nlp_DID = did;
  3683. ndlp->vport = vport;
  3684. ndlp->phba = vport->phba;
  3685. ndlp->nlp_sid = NLP_NO_SID;
  3686. kref_init(&ndlp->kref);
  3687. NLP_INT_NODE_ACT(ndlp);
  3688. atomic_set(&ndlp->cmd_pending, 0);
  3689. ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
  3690. if (vport->phba->sli_rev == LPFC_SLI_REV4)
  3691. ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
  3692. }
  3693. struct lpfc_nodelist *
  3694. lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  3695. int state)
  3696. {
  3697. struct lpfc_hba *phba = vport->phba;
  3698. uint32_t did;
  3699. unsigned long flags;
  3700. if (!ndlp)
  3701. return NULL;
  3702. spin_lock_irqsave(&phba->ndlp_lock, flags);
  3703. /* The ndlp should not be in memory free mode */
  3704. if (NLP_CHK_FREE_REQ(ndlp)) {
  3705. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  3706. lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
  3707. "0277 lpfc_enable_node: ndlp:x%p "
  3708. "usgmap:x%x refcnt:%d\n",
  3709. (void *)ndlp, ndlp->nlp_usg_map,
  3710. atomic_read(&ndlp->kref.refcount));
  3711. return NULL;
  3712. }
  3713. /* The ndlp should not already be in active mode */
  3714. if (NLP_CHK_NODE_ACT(ndlp)) {
  3715. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  3716. lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
  3717. "0278 lpfc_enable_node: ndlp:x%p "
  3718. "usgmap:x%x refcnt:%d\n",
  3719. (void *)ndlp, ndlp->nlp_usg_map,
  3720. atomic_read(&ndlp->kref.refcount));
  3721. return NULL;
  3722. }
  3723. /* Keep the original DID */
  3724. did = ndlp->nlp_DID;
  3725. /* re-initialize ndlp except of ndlp linked list pointer */
  3726. memset((((char *)ndlp) + sizeof (struct list_head)), 0,
  3727. sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
  3728. lpfc_initialize_node(vport, ndlp, did);
  3729. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  3730. if (state != NLP_STE_UNUSED_NODE)
  3731. lpfc_nlp_set_state(vport, ndlp, state);
  3732. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
  3733. "node enable: did:x%x",
  3734. ndlp->nlp_DID, 0, 0);
  3735. return ndlp;
  3736. }
  3737. void
  3738. lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  3739. {
  3740. /*
  3741. * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
  3742. * be used if we wish to issue the "last" lpfc_nlp_put() to remove
  3743. * the ndlp from the vport. The ndlp marked as UNUSED on the list
  3744. * until ALL other outstanding threads have completed. We check
  3745. * that the ndlp not already in the UNUSED state before we proceed.
  3746. */
  3747. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  3748. return;
  3749. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
  3750. if (vport->phba->sli_rev == LPFC_SLI_REV4)
  3751. lpfc_cleanup_vports_rrqs(vport, ndlp);
  3752. lpfc_nlp_put(ndlp);
  3753. return;
  3754. }
  3755. /*
  3756. * Start / ReStart rescue timer for Discovery / RSCN handling
  3757. */
  3758. void
  3759. lpfc_set_disctmo(struct lpfc_vport *vport)
  3760. {
  3761. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3762. struct lpfc_hba *phba = vport->phba;
  3763. uint32_t tmo;
  3764. if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
  3765. /* For FAN, timeout should be greater than edtov */
  3766. tmo = (((phba->fc_edtov + 999) / 1000) + 1);
  3767. } else {
  3768. /* Normal discovery timeout should be > than ELS/CT timeout
  3769. * FC spec states we need 3 * ratov for CT requests
  3770. */
  3771. tmo = ((phba->fc_ratov * 3) + 3);
  3772. }
  3773. if (!timer_pending(&vport->fc_disctmo)) {
  3774. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  3775. "set disc timer: tmo:x%x state:x%x flg:x%x",
  3776. tmo, vport->port_state, vport->fc_flag);
  3777. }
  3778. mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
  3779. spin_lock_irq(shost->host_lock);
  3780. vport->fc_flag |= FC_DISC_TMO;
  3781. spin_unlock_irq(shost->host_lock);
  3782. /* Start Discovery Timer state <hba_state> */
  3783. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  3784. "0247 Start Discovery Timer state x%x "
  3785. "Data: x%x x%lx x%x x%x\n",
  3786. vport->port_state, tmo,
  3787. (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
  3788. vport->fc_adisc_cnt);
  3789. return;
  3790. }
  3791. /*
  3792. * Cancel rescue timer for Discovery / RSCN handling
  3793. */
  3794. int
  3795. lpfc_can_disctmo(struct lpfc_vport *vport)
  3796. {
  3797. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3798. unsigned long iflags;
  3799. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  3800. "can disc timer: state:x%x rtry:x%x flg:x%x",
  3801. vport->port_state, vport->fc_ns_retry, vport->fc_flag);
  3802. /* Turn off discovery timer if its running */
  3803. if (vport->fc_flag & FC_DISC_TMO) {
  3804. spin_lock_irqsave(shost->host_lock, iflags);
  3805. vport->fc_flag &= ~FC_DISC_TMO;
  3806. spin_unlock_irqrestore(shost->host_lock, iflags);
  3807. del_timer_sync(&vport->fc_disctmo);
  3808. spin_lock_irqsave(&vport->work_port_lock, iflags);
  3809. vport->work_port_events &= ~WORKER_DISC_TMO;
  3810. spin_unlock_irqrestore(&vport->work_port_lock, iflags);
  3811. }
  3812. /* Cancel Discovery Timer state <hba_state> */
  3813. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  3814. "0248 Cancel Discovery Timer state x%x "
  3815. "Data: x%x x%x x%x\n",
  3816. vport->port_state, vport->fc_flag,
  3817. vport->fc_plogi_cnt, vport->fc_adisc_cnt);
  3818. return 0;
  3819. }
  3820. /*
  3821. * Check specified ring for outstanding IOCB on the SLI queue
  3822. * Return true if iocb matches the specified nport
  3823. */
  3824. int
  3825. lpfc_check_sli_ndlp(struct lpfc_hba *phba,
  3826. struct lpfc_sli_ring *pring,
  3827. struct lpfc_iocbq *iocb,
  3828. struct lpfc_nodelist *ndlp)
  3829. {
  3830. struct lpfc_sli *psli = &phba->sli;
  3831. IOCB_t *icmd = &iocb->iocb;
  3832. struct lpfc_vport *vport = ndlp->vport;
  3833. if (iocb->vport != vport)
  3834. return 0;
  3835. if (pring->ringno == LPFC_ELS_RING) {
  3836. switch (icmd->ulpCommand) {
  3837. case CMD_GEN_REQUEST64_CR:
  3838. if (iocb->context_un.ndlp == ndlp)
  3839. return 1;
  3840. case CMD_ELS_REQUEST64_CR:
  3841. if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
  3842. return 1;
  3843. case CMD_XMIT_ELS_RSP64_CX:
  3844. if (iocb->context1 == (uint8_t *) ndlp)
  3845. return 1;
  3846. }
  3847. } else if (pring->ringno == psli->extra_ring) {
  3848. } else if (pring->ringno == psli->fcp_ring) {
  3849. /* Skip match check if waiting to relogin to FCP target */
  3850. if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
  3851. (ndlp->nlp_flag & NLP_DELAY_TMO)) {
  3852. return 0;
  3853. }
  3854. if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
  3855. return 1;
  3856. }
  3857. } else if (pring->ringno == psli->next_ring) {
  3858. }
  3859. return 0;
  3860. }
  3861. /*
  3862. * Free resources / clean up outstanding I/Os
  3863. * associated with nlp_rpi in the LPFC_NODELIST entry.
  3864. */
  3865. static int
  3866. lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  3867. {
  3868. LIST_HEAD(completions);
  3869. struct lpfc_sli *psli;
  3870. struct lpfc_sli_ring *pring;
  3871. struct lpfc_iocbq *iocb, *next_iocb;
  3872. uint32_t i;
  3873. lpfc_fabric_abort_nport(ndlp);
  3874. /*
  3875. * Everything that matches on txcmplq will be returned
  3876. * by firmware with a no rpi error.
  3877. */
  3878. psli = &phba->sli;
  3879. if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
  3880. /* Now process each ring */
  3881. for (i = 0; i < psli->num_rings; i++) {
  3882. pring = &psli->ring[i];
  3883. spin_lock_irq(&phba->hbalock);
  3884. list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
  3885. list) {
  3886. /*
  3887. * Check to see if iocb matches the nport we are
  3888. * looking for
  3889. */
  3890. if ((lpfc_check_sli_ndlp(phba, pring, iocb,
  3891. ndlp))) {
  3892. /* It matches, so deque and call compl
  3893. with an error */
  3894. list_move_tail(&iocb->list,
  3895. &completions);
  3896. pring->txq_cnt--;
  3897. }
  3898. }
  3899. spin_unlock_irq(&phba->hbalock);
  3900. }
  3901. }
  3902. /* Cancel all the IOCBs from the completions list */
  3903. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3904. IOERR_SLI_ABORTED);
  3905. return 0;
  3906. }
  3907. /*
  3908. * Free rpi associated with LPFC_NODELIST entry.
  3909. * This routine is called from lpfc_freenode(), when we are removing
  3910. * a LPFC_NODELIST entry. It is also called if the driver initiates a
  3911. * LOGO that completes successfully, and we are waiting to PLOGI back
  3912. * to the remote NPort. In addition, it is called after we receive
  3913. * and unsolicated ELS cmd, send back a rsp, the rsp completes and
  3914. * we are waiting to PLOGI back to the remote NPort.
  3915. */
  3916. int
  3917. lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  3918. {
  3919. struct lpfc_hba *phba = vport->phba;
  3920. LPFC_MBOXQ_t *mbox;
  3921. int rc;
  3922. uint16_t rpi;
  3923. if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
  3924. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3925. if (mbox) {
  3926. /* SLI4 ports require the physical rpi value. */
  3927. rpi = ndlp->nlp_rpi;
  3928. if (phba->sli_rev == LPFC_SLI_REV4)
  3929. rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  3930. lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
  3931. mbox->vport = vport;
  3932. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3933. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  3934. if (rc == MBX_NOT_FINISHED)
  3935. mempool_free(mbox, phba->mbox_mem_pool);
  3936. }
  3937. lpfc_no_rpi(phba, ndlp);
  3938. if (phba->sli_rev != LPFC_SLI_REV4)
  3939. ndlp->nlp_rpi = 0;
  3940. ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
  3941. ndlp->nlp_flag &= ~NLP_NPR_ADISC;
  3942. return 1;
  3943. }
  3944. return 0;
  3945. }
  3946. /**
  3947. * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
  3948. * @phba: pointer to lpfc hba data structure.
  3949. *
  3950. * This routine is invoked to unregister all the currently registered RPIs
  3951. * to the HBA.
  3952. **/
  3953. void
  3954. lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
  3955. {
  3956. struct lpfc_vport **vports;
  3957. struct lpfc_nodelist *ndlp;
  3958. struct Scsi_Host *shost;
  3959. int i;
  3960. vports = lpfc_create_vport_work_array(phba);
  3961. if (!vports) {
  3962. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
  3963. "2884 Vport array allocation failed \n");
  3964. return;
  3965. }
  3966. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  3967. shost = lpfc_shost_from_vport(vports[i]);
  3968. spin_lock_irq(shost->host_lock);
  3969. list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
  3970. if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
  3971. /* The mempool_alloc might sleep */
  3972. spin_unlock_irq(shost->host_lock);
  3973. lpfc_unreg_rpi(vports[i], ndlp);
  3974. spin_lock_irq(shost->host_lock);
  3975. }
  3976. }
  3977. spin_unlock_irq(shost->host_lock);
  3978. }
  3979. lpfc_destroy_vport_work_array(phba, vports);
  3980. }
  3981. void
  3982. lpfc_unreg_all_rpis(struct lpfc_vport *vport)
  3983. {
  3984. struct lpfc_hba *phba = vport->phba;
  3985. LPFC_MBOXQ_t *mbox;
  3986. int rc;
  3987. if (phba->sli_rev == LPFC_SLI_REV4) {
  3988. lpfc_sli4_unreg_all_rpis(vport);
  3989. return;
  3990. }
  3991. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3992. if (mbox) {
  3993. lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
  3994. mbox);
  3995. mbox->vport = vport;
  3996. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3997. mbox->context1 = NULL;
  3998. rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  3999. if (rc != MBX_TIMEOUT)
  4000. mempool_free(mbox, phba->mbox_mem_pool);
  4001. if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
  4002. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
  4003. "1836 Could not issue "
  4004. "unreg_login(all_rpis) status %d\n", rc);
  4005. }
  4006. }
  4007. void
  4008. lpfc_unreg_default_rpis(struct lpfc_vport *vport)
  4009. {
  4010. struct lpfc_hba *phba = vport->phba;
  4011. LPFC_MBOXQ_t *mbox;
  4012. int rc;
  4013. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4014. if (mbox) {
  4015. lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
  4016. mbox);
  4017. mbox->vport = vport;
  4018. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  4019. mbox->context1 = NULL;
  4020. rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  4021. if (rc != MBX_TIMEOUT)
  4022. mempool_free(mbox, phba->mbox_mem_pool);
  4023. if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
  4024. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
  4025. "1815 Could not issue "
  4026. "unreg_did (default rpis) status %d\n",
  4027. rc);
  4028. }
  4029. }
  4030. /*
  4031. * Free resources associated with LPFC_NODELIST entry
  4032. * so it can be freed.
  4033. */
  4034. static int
  4035. lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  4036. {
  4037. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  4038. struct lpfc_hba *phba = vport->phba;
  4039. LPFC_MBOXQ_t *mb, *nextmb;
  4040. struct lpfc_dmabuf *mp;
  4041. /* Cleanup node for NPort <nlp_DID> */
  4042. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  4043. "0900 Cleanup node for NPort x%x "
  4044. "Data: x%x x%x x%x\n",
  4045. ndlp->nlp_DID, ndlp->nlp_flag,
  4046. ndlp->nlp_state, ndlp->nlp_rpi);
  4047. if (NLP_CHK_FREE_REQ(ndlp)) {
  4048. lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
  4049. "0280 lpfc_cleanup_node: ndlp:x%p "
  4050. "usgmap:x%x refcnt:%d\n",
  4051. (void *)ndlp, ndlp->nlp_usg_map,
  4052. atomic_read(&ndlp->kref.refcount));
  4053. lpfc_dequeue_node(vport, ndlp);
  4054. } else {
  4055. lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
  4056. "0281 lpfc_cleanup_node: ndlp:x%p "
  4057. "usgmap:x%x refcnt:%d\n",
  4058. (void *)ndlp, ndlp->nlp_usg_map,
  4059. atomic_read(&ndlp->kref.refcount));
  4060. lpfc_disable_node(vport, ndlp);
  4061. }
  4062. /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
  4063. if ((mb = phba->sli.mbox_active)) {
  4064. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
  4065. (ndlp == (struct lpfc_nodelist *) mb->context2)) {
  4066. mb->context2 = NULL;
  4067. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  4068. }
  4069. }
  4070. spin_lock_irq(&phba->hbalock);
  4071. /* Cleanup REG_LOGIN completions which are not yet processed */
  4072. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  4073. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
  4074. (ndlp != (struct lpfc_nodelist *) mb->context2))
  4075. continue;
  4076. mb->context2 = NULL;
  4077. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  4078. }
  4079. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  4080. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
  4081. (ndlp == (struct lpfc_nodelist *) mb->context2)) {
  4082. mp = (struct lpfc_dmabuf *) (mb->context1);
  4083. if (mp) {
  4084. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4085. kfree(mp);
  4086. }
  4087. list_del(&mb->list);
  4088. mempool_free(mb, phba->mbox_mem_pool);
  4089. /* We shall not invoke the lpfc_nlp_put to decrement
  4090. * the ndlp reference count as we are in the process
  4091. * of lpfc_nlp_release.
  4092. */
  4093. }
  4094. }
  4095. spin_unlock_irq(&phba->hbalock);
  4096. lpfc_els_abort(phba, ndlp);
  4097. spin_lock_irq(shost->host_lock);
  4098. ndlp->nlp_flag &= ~NLP_DELAY_TMO;
  4099. spin_unlock_irq(shost->host_lock);
  4100. ndlp->nlp_last_elscmd = 0;
  4101. del_timer_sync(&ndlp->nlp_delayfunc);
  4102. list_del_init(&ndlp->els_retry_evt.evt_listp);
  4103. list_del_init(&ndlp->dev_loss_evt.evt_listp);
  4104. lpfc_cleanup_vports_rrqs(vport, ndlp);
  4105. lpfc_unreg_rpi(vport, ndlp);
  4106. return 0;
  4107. }
  4108. /*
  4109. * Check to see if we can free the nlp back to the freelist.
  4110. * If we are in the middle of using the nlp in the discovery state
  4111. * machine, defer the free till we reach the end of the state machine.
  4112. */
  4113. static void
  4114. lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  4115. {
  4116. struct lpfc_hba *phba = vport->phba;
  4117. struct lpfc_rport_data *rdata;
  4118. LPFC_MBOXQ_t *mbox;
  4119. int rc;
  4120. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  4121. if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
  4122. !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
  4123. !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
  4124. /* For this case we need to cleanup the default rpi
  4125. * allocated by the firmware.
  4126. */
  4127. if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
  4128. != NULL) {
  4129. rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
  4130. (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
  4131. if (rc) {
  4132. mempool_free(mbox, phba->mbox_mem_pool);
  4133. }
  4134. else {
  4135. mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
  4136. mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  4137. mbox->vport = vport;
  4138. mbox->context2 = NULL;
  4139. rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  4140. if (rc == MBX_NOT_FINISHED) {
  4141. mempool_free(mbox, phba->mbox_mem_pool);
  4142. }
  4143. }
  4144. }
  4145. }
  4146. lpfc_cleanup_node(vport, ndlp);
  4147. /*
  4148. * We can get here with a non-NULL ndlp->rport because when we
  4149. * unregister a rport we don't break the rport/node linkage. So if we
  4150. * do, make sure we don't leaving any dangling pointers behind.
  4151. */
  4152. if (ndlp->rport) {
  4153. rdata = ndlp->rport->dd_data;
  4154. rdata->pnode = NULL;
  4155. ndlp->rport = NULL;
  4156. }
  4157. }
  4158. static int
  4159. lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  4160. uint32_t did)
  4161. {
  4162. D_ID mydid, ndlpdid, matchdid;
  4163. if (did == Bcast_DID)
  4164. return 0;
  4165. /* First check for Direct match */
  4166. if (ndlp->nlp_DID == did)
  4167. return 1;
  4168. /* Next check for area/domain identically equals 0 match */
  4169. mydid.un.word = vport->fc_myDID;
  4170. if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
  4171. return 0;
  4172. }
  4173. matchdid.un.word = did;
  4174. ndlpdid.un.word = ndlp->nlp_DID;
  4175. if (matchdid.un.b.id == ndlpdid.un.b.id) {
  4176. if ((mydid.un.b.domain == matchdid.un.b.domain) &&
  4177. (mydid.un.b.area == matchdid.un.b.area)) {
  4178. if ((ndlpdid.un.b.domain == 0) &&
  4179. (ndlpdid.un.b.area == 0)) {
  4180. if (ndlpdid.un.b.id)
  4181. return 1;
  4182. }
  4183. return 0;
  4184. }
  4185. matchdid.un.word = ndlp->nlp_DID;
  4186. if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
  4187. (mydid.un.b.area == ndlpdid.un.b.area)) {
  4188. if ((matchdid.un.b.domain == 0) &&
  4189. (matchdid.un.b.area == 0)) {
  4190. if (matchdid.un.b.id)
  4191. return 1;
  4192. }
  4193. }
  4194. }
  4195. return 0;
  4196. }
  4197. /* Search for a nodelist entry */
  4198. static struct lpfc_nodelist *
  4199. __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
  4200. {
  4201. struct lpfc_nodelist *ndlp;
  4202. uint32_t data1;
  4203. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  4204. if (lpfc_matchdid(vport, ndlp, did)) {
  4205. data1 = (((uint32_t) ndlp->nlp_state << 24) |
  4206. ((uint32_t) ndlp->nlp_xri << 16) |
  4207. ((uint32_t) ndlp->nlp_type << 8) |
  4208. ((uint32_t) ndlp->nlp_rpi & 0xff));
  4209. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  4210. "0929 FIND node DID "
  4211. "Data: x%p x%x x%x x%x\n",
  4212. ndlp, ndlp->nlp_DID,
  4213. ndlp->nlp_flag, data1);
  4214. return ndlp;
  4215. }
  4216. }
  4217. /* FIND node did <did> NOT FOUND */
  4218. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  4219. "0932 FIND node did x%x NOT FOUND.\n", did);
  4220. return NULL;
  4221. }
  4222. struct lpfc_nodelist *
  4223. lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
  4224. {
  4225. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  4226. struct lpfc_nodelist *ndlp;
  4227. unsigned long iflags;
  4228. spin_lock_irqsave(shost->host_lock, iflags);
  4229. ndlp = __lpfc_findnode_did(vport, did);
  4230. spin_unlock_irqrestore(shost->host_lock, iflags);
  4231. return ndlp;
  4232. }
  4233. struct lpfc_nodelist *
  4234. lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
  4235. {
  4236. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  4237. struct lpfc_nodelist *ndlp;
  4238. ndlp = lpfc_findnode_did(vport, did);
  4239. if (!ndlp) {
  4240. if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
  4241. lpfc_rscn_payload_check(vport, did) == 0)
  4242. return NULL;
  4243. ndlp = (struct lpfc_nodelist *)
  4244. mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
  4245. if (!ndlp)
  4246. return NULL;
  4247. lpfc_nlp_init(vport, ndlp, did);
  4248. lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
  4249. spin_lock_irq(shost->host_lock);
  4250. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  4251. spin_unlock_irq(shost->host_lock);
  4252. return ndlp;
  4253. } else if (!NLP_CHK_NODE_ACT(ndlp)) {
  4254. ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
  4255. if (!ndlp)
  4256. return NULL;
  4257. spin_lock_irq(shost->host_lock);
  4258. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  4259. spin_unlock_irq(shost->host_lock);
  4260. return ndlp;
  4261. }
  4262. if ((vport->fc_flag & FC_RSCN_MODE) &&
  4263. !(vport->fc_flag & FC_NDISC_ACTIVE)) {
  4264. if (lpfc_rscn_payload_check(vport, did)) {
  4265. /* If we've already received a PLOGI from this NPort
  4266. * we don't need to try to discover it again.
  4267. */
  4268. if (ndlp->nlp_flag & NLP_RCV_PLOGI)
  4269. return NULL;
  4270. /* Since this node is marked for discovery,
  4271. * delay timeout is not needed.
  4272. */
  4273. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  4274. spin_lock_irq(shost->host_lock);
  4275. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  4276. spin_unlock_irq(shost->host_lock);
  4277. } else
  4278. ndlp = NULL;
  4279. } else {
  4280. /* If we've already received a PLOGI from this NPort,
  4281. * or we are already in the process of discovery on it,
  4282. * we don't need to try to discover it again.
  4283. */
  4284. if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
  4285. ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
  4286. ndlp->nlp_flag & NLP_RCV_PLOGI)
  4287. return NULL;
  4288. lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
  4289. spin_lock_irq(shost->host_lock);
  4290. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  4291. spin_unlock_irq(shost->host_lock);
  4292. }
  4293. return ndlp;
  4294. }
  4295. /* Build a list of nodes to discover based on the loopmap */
  4296. void
  4297. lpfc_disc_list_loopmap(struct lpfc_vport *vport)
  4298. {
  4299. struct lpfc_hba *phba = vport->phba;
  4300. int j;
  4301. uint32_t alpa, index;
  4302. if (!lpfc_is_link_up(phba))
  4303. return;
  4304. if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
  4305. return;
  4306. /* Check for loop map present or not */
  4307. if (phba->alpa_map[0]) {
  4308. for (j = 1; j <= phba->alpa_map[0]; j++) {
  4309. alpa = phba->alpa_map[j];
  4310. if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
  4311. continue;
  4312. lpfc_setup_disc_node(vport, alpa);
  4313. }
  4314. } else {
  4315. /* No alpamap, so try all alpa's */
  4316. for (j = 0; j < FC_MAXLOOP; j++) {
  4317. /* If cfg_scan_down is set, start from highest
  4318. * ALPA (0xef) to lowest (0x1).
  4319. */
  4320. if (vport->cfg_scan_down)
  4321. index = j;
  4322. else
  4323. index = FC_MAXLOOP - j - 1;
  4324. alpa = lpfcAlpaArray[index];
  4325. if ((vport->fc_myDID & 0xff) == alpa)
  4326. continue;
  4327. lpfc_setup_disc_node(vport, alpa);
  4328. }
  4329. }
  4330. return;
  4331. }
  4332. void
  4333. lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
  4334. {
  4335. LPFC_MBOXQ_t *mbox;
  4336. struct lpfc_sli *psli = &phba->sli;
  4337. struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
  4338. struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
  4339. struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
  4340. int rc;
  4341. /*
  4342. * if it's not a physical port or if we already send
  4343. * clear_la then don't send it.
  4344. */
  4345. if ((phba->link_state >= LPFC_CLEAR_LA) ||
  4346. (vport->port_type != LPFC_PHYSICAL_PORT) ||
  4347. (phba->sli_rev == LPFC_SLI_REV4))
  4348. return;
  4349. /* Link up discovery */
  4350. if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
  4351. phba->link_state = LPFC_CLEAR_LA;
  4352. lpfc_clear_la(phba, mbox);
  4353. mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
  4354. mbox->vport = vport;
  4355. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  4356. if (rc == MBX_NOT_FINISHED) {
  4357. mempool_free(mbox, phba->mbox_mem_pool);
  4358. lpfc_disc_flush_list(vport);
  4359. extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
  4360. fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
  4361. next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
  4362. phba->link_state = LPFC_HBA_ERROR;
  4363. }
  4364. }
  4365. }
  4366. /* Reg_vpi to tell firmware to resume normal operations */
  4367. void
  4368. lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
  4369. {
  4370. LPFC_MBOXQ_t *regvpimbox;
  4371. regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4372. if (regvpimbox) {
  4373. lpfc_reg_vpi(vport, regvpimbox);
  4374. regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
  4375. regvpimbox->vport = vport;
  4376. if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
  4377. == MBX_NOT_FINISHED) {
  4378. mempool_free(regvpimbox, phba->mbox_mem_pool);
  4379. }
  4380. }
  4381. }
  4382. /* Start Link up / RSCN discovery on NPR nodes */
  4383. void
  4384. lpfc_disc_start(struct lpfc_vport *vport)
  4385. {
  4386. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  4387. struct lpfc_hba *phba = vport->phba;
  4388. uint32_t num_sent;
  4389. uint32_t clear_la_pending;
  4390. int did_changed;
  4391. if (!lpfc_is_link_up(phba))
  4392. return;
  4393. if (phba->link_state == LPFC_CLEAR_LA)
  4394. clear_la_pending = 1;
  4395. else
  4396. clear_la_pending = 0;
  4397. if (vport->port_state < LPFC_VPORT_READY)
  4398. vport->port_state = LPFC_DISC_AUTH;
  4399. lpfc_set_disctmo(vport);
  4400. if (vport->fc_prevDID == vport->fc_myDID)
  4401. did_changed = 0;
  4402. else
  4403. did_changed = 1;
  4404. vport->fc_prevDID = vport->fc_myDID;
  4405. vport->num_disc_nodes = 0;
  4406. /* Start Discovery state <hba_state> */
  4407. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  4408. "0202 Start Discovery hba state x%x "
  4409. "Data: x%x x%x x%x\n",
  4410. vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
  4411. vport->fc_adisc_cnt);
  4412. /* First do ADISCs - if any */
  4413. num_sent = lpfc_els_disc_adisc(vport);
  4414. if (num_sent)
  4415. return;
  4416. /* Register the VPI for SLI3, NON-NPIV only. */
  4417. if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
  4418. !(vport->fc_flag & FC_PT2PT) &&
  4419. !(vport->fc_flag & FC_RSCN_MODE) &&
  4420. (phba->sli_rev < LPFC_SLI_REV4)) {
  4421. lpfc_issue_reg_vpi(phba, vport);
  4422. return;
  4423. }
  4424. /*
  4425. * For SLI2, we need to set port_state to READY and continue
  4426. * discovery.
  4427. */
  4428. if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
  4429. /* If we get here, there is nothing to ADISC */
  4430. if (vport->port_type == LPFC_PHYSICAL_PORT)
  4431. lpfc_issue_clear_la(phba, vport);
  4432. if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
  4433. vport->num_disc_nodes = 0;
  4434. /* go thru NPR nodes and issue ELS PLOGIs */
  4435. if (vport->fc_npr_cnt)
  4436. lpfc_els_disc_plogi(vport);
  4437. if (!vport->num_disc_nodes) {
  4438. spin_lock_irq(shost->host_lock);
  4439. vport->fc_flag &= ~FC_NDISC_ACTIVE;
  4440. spin_unlock_irq(shost->host_lock);
  4441. lpfc_can_disctmo(vport);
  4442. }
  4443. }
  4444. vport->port_state = LPFC_VPORT_READY;
  4445. } else {
  4446. /* Next do PLOGIs - if any */
  4447. num_sent = lpfc_els_disc_plogi(vport);
  4448. if (num_sent)
  4449. return;
  4450. if (vport->fc_flag & FC_RSCN_MODE) {
  4451. /* Check to see if more RSCNs came in while we
  4452. * were processing this one.
  4453. */
  4454. if ((vport->fc_rscn_id_cnt == 0) &&
  4455. (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
  4456. spin_lock_irq(shost->host_lock);
  4457. vport->fc_flag &= ~FC_RSCN_MODE;
  4458. spin_unlock_irq(shost->host_lock);
  4459. lpfc_can_disctmo(vport);
  4460. } else
  4461. lpfc_els_handle_rscn(vport);
  4462. }
  4463. }
  4464. return;
  4465. }
  4466. /*
  4467. * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
  4468. * ring the match the sppecified nodelist.
  4469. */
  4470. static void
  4471. lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  4472. {
  4473. LIST_HEAD(completions);
  4474. struct lpfc_sli *psli;
  4475. IOCB_t *icmd;
  4476. struct lpfc_iocbq *iocb, *next_iocb;
  4477. struct lpfc_sli_ring *pring;
  4478. psli = &phba->sli;
  4479. pring = &psli->ring[LPFC_ELS_RING];
  4480. /* Error matching iocb on txq or txcmplq
  4481. * First check the txq.
  4482. */
  4483. spin_lock_irq(&phba->hbalock);
  4484. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  4485. if (iocb->context1 != ndlp) {
  4486. continue;
  4487. }
  4488. icmd = &iocb->iocb;
  4489. if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
  4490. (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
  4491. list_move_tail(&iocb->list, &completions);
  4492. pring->txq_cnt--;
  4493. }
  4494. }
  4495. /* Next check the txcmplq */
  4496. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
  4497. if (iocb->context1 != ndlp) {
  4498. continue;
  4499. }
  4500. icmd = &iocb->iocb;
  4501. if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
  4502. icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
  4503. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  4504. }
  4505. }
  4506. spin_unlock_irq(&phba->hbalock);
  4507. /* Cancel all the IOCBs from the completions list */
  4508. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  4509. IOERR_SLI_ABORTED);
  4510. }
  4511. static void
  4512. lpfc_disc_flush_list(struct lpfc_vport *vport)
  4513. {
  4514. struct lpfc_nodelist *ndlp, *next_ndlp;
  4515. struct lpfc_hba *phba = vport->phba;
  4516. if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
  4517. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
  4518. nlp_listp) {
  4519. if (!NLP_CHK_NODE_ACT(ndlp))
  4520. continue;
  4521. if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
  4522. ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
  4523. lpfc_free_tx(phba, ndlp);
  4524. }
  4525. }
  4526. }
  4527. }
  4528. void
  4529. lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
  4530. {
  4531. lpfc_els_flush_rscn(vport);
  4532. lpfc_els_flush_cmd(vport);
  4533. lpfc_disc_flush_list(vport);
  4534. }
  4535. /*****************************************************************************/
  4536. /*
  4537. * NAME: lpfc_disc_timeout
  4538. *
  4539. * FUNCTION: Fibre Channel driver discovery timeout routine.
  4540. *
  4541. * EXECUTION ENVIRONMENT: interrupt only
  4542. *
  4543. * CALLED FROM:
  4544. * Timer function
  4545. *
  4546. * RETURNS:
  4547. * none
  4548. */
  4549. /*****************************************************************************/
  4550. void
  4551. lpfc_disc_timeout(unsigned long ptr)
  4552. {
  4553. struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
  4554. struct lpfc_hba *phba = vport->phba;
  4555. uint32_t tmo_posted;
  4556. unsigned long flags = 0;
  4557. if (unlikely(!phba))
  4558. return;
  4559. spin_lock_irqsave(&vport->work_port_lock, flags);
  4560. tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
  4561. if (!tmo_posted)
  4562. vport->work_port_events |= WORKER_DISC_TMO;
  4563. spin_unlock_irqrestore(&vport->work_port_lock, flags);
  4564. if (!tmo_posted)
  4565. lpfc_worker_wake_up(phba);
  4566. return;
  4567. }
  4568. static void
  4569. lpfc_disc_timeout_handler(struct lpfc_vport *vport)
  4570. {
  4571. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  4572. struct lpfc_hba *phba = vport->phba;
  4573. struct lpfc_sli *psli = &phba->sli;
  4574. struct lpfc_nodelist *ndlp, *next_ndlp;
  4575. LPFC_MBOXQ_t *initlinkmbox;
  4576. int rc, clrlaerr = 0;
  4577. if (!(vport->fc_flag & FC_DISC_TMO))
  4578. return;
  4579. spin_lock_irq(shost->host_lock);
  4580. vport->fc_flag &= ~FC_DISC_TMO;
  4581. spin_unlock_irq(shost->host_lock);
  4582. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  4583. "disc timeout: state:x%x rtry:x%x flg:x%x",
  4584. vport->port_state, vport->fc_ns_retry, vport->fc_flag);
  4585. switch (vport->port_state) {
  4586. case LPFC_LOCAL_CFG_LINK:
  4587. /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
  4588. * FAN
  4589. */
  4590. /* FAN timeout */
  4591. lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
  4592. "0221 FAN timeout\n");
  4593. /* Start discovery by sending FLOGI, clean up old rpis */
  4594. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
  4595. nlp_listp) {
  4596. if (!NLP_CHK_NODE_ACT(ndlp))
  4597. continue;
  4598. if (ndlp->nlp_state != NLP_STE_NPR_NODE)
  4599. continue;
  4600. if (ndlp->nlp_type & NLP_FABRIC) {
  4601. /* Clean up the ndlp on Fabric connections */
  4602. lpfc_drop_node(vport, ndlp);
  4603. } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
  4604. /* Fail outstanding IO now since device
  4605. * is marked for PLOGI.
  4606. */
  4607. lpfc_unreg_rpi(vport, ndlp);
  4608. }
  4609. }
  4610. if (vport->port_state != LPFC_FLOGI) {
  4611. if (phba->sli_rev <= LPFC_SLI_REV3)
  4612. lpfc_initial_flogi(vport);
  4613. else
  4614. lpfc_issue_init_vfi(vport);
  4615. return;
  4616. }
  4617. break;
  4618. case LPFC_FDISC:
  4619. case LPFC_FLOGI:
  4620. /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
  4621. /* Initial FLOGI timeout */
  4622. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4623. "0222 Initial %s timeout\n",
  4624. vport->vpi ? "FDISC" : "FLOGI");
  4625. /* Assume no Fabric and go on with discovery.
  4626. * Check for outstanding ELS FLOGI to abort.
  4627. */
  4628. /* FLOGI failed, so just use loop map to make discovery list */
  4629. lpfc_disc_list_loopmap(vport);
  4630. /* Start discovery */
  4631. lpfc_disc_start(vport);
  4632. break;
  4633. case LPFC_FABRIC_CFG_LINK:
  4634. /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
  4635. NameServer login */
  4636. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4637. "0223 Timeout while waiting for "
  4638. "NameServer login\n");
  4639. /* Next look for NameServer ndlp */
  4640. ndlp = lpfc_findnode_did(vport, NameServer_DID);
  4641. if (ndlp && NLP_CHK_NODE_ACT(ndlp))
  4642. lpfc_els_abort(phba, ndlp);
  4643. /* ReStart discovery */
  4644. goto restart_disc;
  4645. case LPFC_NS_QRY:
  4646. /* Check for wait for NameServer Rsp timeout */
  4647. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4648. "0224 NameServer Query timeout "
  4649. "Data: x%x x%x\n",
  4650. vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
  4651. if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
  4652. /* Try it one more time */
  4653. vport->fc_ns_retry++;
  4654. rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
  4655. vport->fc_ns_retry, 0);
  4656. if (rc == 0)
  4657. break;
  4658. }
  4659. vport->fc_ns_retry = 0;
  4660. restart_disc:
  4661. /*
  4662. * Discovery is over.
  4663. * set port_state to PORT_READY if SLI2.
  4664. * cmpl_reg_vpi will set port_state to READY for SLI3.
  4665. */
  4666. if (phba->sli_rev < LPFC_SLI_REV4) {
  4667. if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
  4668. lpfc_issue_reg_vpi(phba, vport);
  4669. else {
  4670. lpfc_issue_clear_la(phba, vport);
  4671. vport->port_state = LPFC_VPORT_READY;
  4672. }
  4673. }
  4674. /* Setup and issue mailbox INITIALIZE LINK command */
  4675. initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4676. if (!initlinkmbox) {
  4677. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4678. "0206 Device Discovery "
  4679. "completion error\n");
  4680. phba->link_state = LPFC_HBA_ERROR;
  4681. break;
  4682. }
  4683. lpfc_linkdown(phba);
  4684. lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
  4685. phba->cfg_link_speed);
  4686. initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
  4687. initlinkmbox->vport = vport;
  4688. initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  4689. rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
  4690. lpfc_set_loopback_flag(phba);
  4691. if (rc == MBX_NOT_FINISHED)
  4692. mempool_free(initlinkmbox, phba->mbox_mem_pool);
  4693. break;
  4694. case LPFC_DISC_AUTH:
  4695. /* Node Authentication timeout */
  4696. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4697. "0227 Node Authentication timeout\n");
  4698. lpfc_disc_flush_list(vport);
  4699. /*
  4700. * set port_state to PORT_READY if SLI2.
  4701. * cmpl_reg_vpi will set port_state to READY for SLI3.
  4702. */
  4703. if (phba->sli_rev < LPFC_SLI_REV4) {
  4704. if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
  4705. lpfc_issue_reg_vpi(phba, vport);
  4706. else { /* NPIV Not enabled */
  4707. lpfc_issue_clear_la(phba, vport);
  4708. vport->port_state = LPFC_VPORT_READY;
  4709. }
  4710. }
  4711. break;
  4712. case LPFC_VPORT_READY:
  4713. if (vport->fc_flag & FC_RSCN_MODE) {
  4714. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4715. "0231 RSCN timeout Data: x%x "
  4716. "x%x\n",
  4717. vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
  4718. /* Cleanup any outstanding ELS commands */
  4719. lpfc_els_flush_cmd(vport);
  4720. lpfc_els_flush_rscn(vport);
  4721. lpfc_disc_flush_list(vport);
  4722. }
  4723. break;
  4724. default:
  4725. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4726. "0273 Unexpected discovery timeout, "
  4727. "vport State x%x\n", vport->port_state);
  4728. break;
  4729. }
  4730. switch (phba->link_state) {
  4731. case LPFC_CLEAR_LA:
  4732. /* CLEAR LA timeout */
  4733. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4734. "0228 CLEAR LA timeout\n");
  4735. clrlaerr = 1;
  4736. break;
  4737. case LPFC_LINK_UP:
  4738. lpfc_issue_clear_la(phba, vport);
  4739. /* Drop thru */
  4740. case LPFC_LINK_UNKNOWN:
  4741. case LPFC_WARM_START:
  4742. case LPFC_INIT_START:
  4743. case LPFC_INIT_MBX_CMDS:
  4744. case LPFC_LINK_DOWN:
  4745. case LPFC_HBA_ERROR:
  4746. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4747. "0230 Unexpected timeout, hba link "
  4748. "state x%x\n", phba->link_state);
  4749. clrlaerr = 1;
  4750. break;
  4751. case LPFC_HBA_READY:
  4752. break;
  4753. }
  4754. if (clrlaerr) {
  4755. lpfc_disc_flush_list(vport);
  4756. psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
  4757. psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
  4758. psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
  4759. vport->port_state = LPFC_VPORT_READY;
  4760. }
  4761. return;
  4762. }
  4763. /*
  4764. * This routine handles processing a NameServer REG_LOGIN mailbox
  4765. * command upon completion. It is setup in the LPFC_MBOXQ
  4766. * as the completion routine when the command is
  4767. * handed off to the SLI layer.
  4768. */
  4769. void
  4770. lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  4771. {
  4772. MAILBOX_t *mb = &pmb->u.mb;
  4773. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  4774. struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
  4775. struct lpfc_vport *vport = pmb->vport;
  4776. pmb->context1 = NULL;
  4777. pmb->context2 = NULL;
  4778. if (phba->sli_rev < LPFC_SLI_REV4)
  4779. ndlp->nlp_rpi = mb->un.varWords[0];
  4780. ndlp->nlp_flag |= NLP_RPI_REGISTERED;
  4781. ndlp->nlp_type |= NLP_FABRIC;
  4782. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  4783. /*
  4784. * Start issuing Fabric-Device Management Interface (FDMI) command to
  4785. * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
  4786. * fdmi-on=2 (supporting RPA/hostnmae)
  4787. */
  4788. if (vport->cfg_fdmi_on == 1)
  4789. lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
  4790. else
  4791. mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
  4792. /* decrement the node reference count held for this callback
  4793. * function.
  4794. */
  4795. lpfc_nlp_put(ndlp);
  4796. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4797. kfree(mp);
  4798. mempool_free(pmb, phba->mbox_mem_pool);
  4799. return;
  4800. }
  4801. static int
  4802. lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
  4803. {
  4804. uint16_t *rpi = param;
  4805. return ndlp->nlp_rpi == *rpi;
  4806. }
  4807. static int
  4808. lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
  4809. {
  4810. return memcmp(&ndlp->nlp_portname, param,
  4811. sizeof(ndlp->nlp_portname)) == 0;
  4812. }
  4813. static struct lpfc_nodelist *
  4814. __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
  4815. {
  4816. struct lpfc_nodelist *ndlp;
  4817. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  4818. if (filter(ndlp, param))
  4819. return ndlp;
  4820. }
  4821. return NULL;
  4822. }
  4823. /*
  4824. * This routine looks up the ndlp lists for the given RPI. If rpi found it
  4825. * returns the node list element pointer else return NULL.
  4826. */
  4827. struct lpfc_nodelist *
  4828. __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
  4829. {
  4830. return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
  4831. }
  4832. /*
  4833. * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
  4834. * returns the node element list pointer else return NULL.
  4835. */
  4836. struct lpfc_nodelist *
  4837. lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
  4838. {
  4839. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  4840. struct lpfc_nodelist *ndlp;
  4841. spin_lock_irq(shost->host_lock);
  4842. ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
  4843. spin_unlock_irq(shost->host_lock);
  4844. return ndlp;
  4845. }
  4846. /*
  4847. * This routine looks up the ndlp lists for the given RPI. If the rpi
  4848. * is found, the routine returns the node element list pointer else
  4849. * return NULL.
  4850. */
  4851. struct lpfc_nodelist *
  4852. lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
  4853. {
  4854. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  4855. struct lpfc_nodelist *ndlp;
  4856. spin_lock_irq(shost->host_lock);
  4857. ndlp = __lpfc_findnode_rpi(vport, rpi);
  4858. spin_unlock_irq(shost->host_lock);
  4859. return ndlp;
  4860. }
  4861. /**
  4862. * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
  4863. * @phba: pointer to lpfc hba data structure.
  4864. * @vpi: the physical host virtual N_Port identifier.
  4865. *
  4866. * This routine finds a vport on a HBA (referred by @phba) through a
  4867. * @vpi. The function walks the HBA's vport list and returns the address
  4868. * of the vport with the matching @vpi.
  4869. *
  4870. * Return code
  4871. * NULL - No vport with the matching @vpi found
  4872. * Otherwise - Address to the vport with the matching @vpi.
  4873. **/
  4874. struct lpfc_vport *
  4875. lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
  4876. {
  4877. struct lpfc_vport *vport;
  4878. unsigned long flags;
  4879. int i = 0;
  4880. /* The physical ports are always vpi 0 - translate is unnecessary. */
  4881. if (vpi > 0) {
  4882. /*
  4883. * Translate the physical vpi to the logical vpi. The
  4884. * vport stores the logical vpi.
  4885. */
  4886. for (i = 0; i < phba->max_vpi; i++) {
  4887. if (vpi == phba->vpi_ids[i])
  4888. break;
  4889. }
  4890. if (i >= phba->max_vpi) {
  4891. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  4892. "2936 Could not find Vport mapped "
  4893. "to vpi %d\n", vpi);
  4894. return NULL;
  4895. }
  4896. }
  4897. spin_lock_irqsave(&phba->hbalock, flags);
  4898. list_for_each_entry(vport, &phba->port_list, listentry) {
  4899. if (vport->vpi == i) {
  4900. spin_unlock_irqrestore(&phba->hbalock, flags);
  4901. return vport;
  4902. }
  4903. }
  4904. spin_unlock_irqrestore(&phba->hbalock, flags);
  4905. return NULL;
  4906. }
  4907. void
  4908. lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  4909. uint32_t did)
  4910. {
  4911. memset(ndlp, 0, sizeof (struct lpfc_nodelist));
  4912. lpfc_initialize_node(vport, ndlp, did);
  4913. INIT_LIST_HEAD(&ndlp->nlp_listp);
  4914. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
  4915. "node init: did:x%x",
  4916. ndlp->nlp_DID, 0, 0);
  4917. return;
  4918. }
  4919. /* This routine releases all resources associated with a specifc NPort's ndlp
  4920. * and mempool_free's the nodelist.
  4921. */
  4922. static void
  4923. lpfc_nlp_release(struct kref *kref)
  4924. {
  4925. struct lpfc_hba *phba;
  4926. unsigned long flags;
  4927. struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
  4928. kref);
  4929. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  4930. "node release: did:x%x flg:x%x type:x%x",
  4931. ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
  4932. lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
  4933. "0279 lpfc_nlp_release: ndlp:x%p "
  4934. "usgmap:x%x refcnt:%d\n",
  4935. (void *)ndlp, ndlp->nlp_usg_map,
  4936. atomic_read(&ndlp->kref.refcount));
  4937. /* remove ndlp from action. */
  4938. lpfc_nlp_remove(ndlp->vport, ndlp);
  4939. /* clear the ndlp active flag for all release cases */
  4940. phba = ndlp->phba;
  4941. spin_lock_irqsave(&phba->ndlp_lock, flags);
  4942. NLP_CLR_NODE_ACT(ndlp);
  4943. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4944. if (phba->sli_rev == LPFC_SLI_REV4)
  4945. lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
  4946. /* free ndlp memory for final ndlp release */
  4947. if (NLP_CHK_FREE_REQ(ndlp)) {
  4948. kfree(ndlp->lat_data);
  4949. mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
  4950. }
  4951. }
  4952. /* This routine bumps the reference count for a ndlp structure to ensure
  4953. * that one discovery thread won't free a ndlp while another discovery thread
  4954. * is using it.
  4955. */
  4956. struct lpfc_nodelist *
  4957. lpfc_nlp_get(struct lpfc_nodelist *ndlp)
  4958. {
  4959. struct lpfc_hba *phba;
  4960. unsigned long flags;
  4961. if (ndlp) {
  4962. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  4963. "node get: did:x%x flg:x%x refcnt:x%x",
  4964. ndlp->nlp_DID, ndlp->nlp_flag,
  4965. atomic_read(&ndlp->kref.refcount));
  4966. /* The check of ndlp usage to prevent incrementing the
  4967. * ndlp reference count that is in the process of being
  4968. * released.
  4969. */
  4970. phba = ndlp->phba;
  4971. spin_lock_irqsave(&phba->ndlp_lock, flags);
  4972. if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
  4973. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4974. lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
  4975. "0276 lpfc_nlp_get: ndlp:x%p "
  4976. "usgmap:x%x refcnt:%d\n",
  4977. (void *)ndlp, ndlp->nlp_usg_map,
  4978. atomic_read(&ndlp->kref.refcount));
  4979. return NULL;
  4980. } else
  4981. kref_get(&ndlp->kref);
  4982. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4983. }
  4984. return ndlp;
  4985. }
  4986. /* This routine decrements the reference count for a ndlp structure. If the
  4987. * count goes to 0, this indicates the the associated nodelist should be
  4988. * freed. Returning 1 indicates the ndlp resource has been released; on the
  4989. * other hand, returning 0 indicates the ndlp resource has not been released
  4990. * yet.
  4991. */
  4992. int
  4993. lpfc_nlp_put(struct lpfc_nodelist *ndlp)
  4994. {
  4995. struct lpfc_hba *phba;
  4996. unsigned long flags;
  4997. if (!ndlp)
  4998. return 1;
  4999. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  5000. "node put: did:x%x flg:x%x refcnt:x%x",
  5001. ndlp->nlp_DID, ndlp->nlp_flag,
  5002. atomic_read(&ndlp->kref.refcount));
  5003. phba = ndlp->phba;
  5004. spin_lock_irqsave(&phba->ndlp_lock, flags);
  5005. /* Check the ndlp memory free acknowledge flag to avoid the
  5006. * possible race condition that kref_put got invoked again
  5007. * after previous one has done ndlp memory free.
  5008. */
  5009. if (NLP_CHK_FREE_ACK(ndlp)) {
  5010. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  5011. lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
  5012. "0274 lpfc_nlp_put: ndlp:x%p "
  5013. "usgmap:x%x refcnt:%d\n",
  5014. (void *)ndlp, ndlp->nlp_usg_map,
  5015. atomic_read(&ndlp->kref.refcount));
  5016. return 1;
  5017. }
  5018. /* Check the ndlp inactivate log flag to avoid the possible
  5019. * race condition that kref_put got invoked again after ndlp
  5020. * is already in inactivating state.
  5021. */
  5022. if (NLP_CHK_IACT_REQ(ndlp)) {
  5023. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  5024. lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
  5025. "0275 lpfc_nlp_put: ndlp:x%p "
  5026. "usgmap:x%x refcnt:%d\n",
  5027. (void *)ndlp, ndlp->nlp_usg_map,
  5028. atomic_read(&ndlp->kref.refcount));
  5029. return 1;
  5030. }
  5031. /* For last put, mark the ndlp usage flags to make sure no
  5032. * other kref_get and kref_put on the same ndlp shall get
  5033. * in between the process when the final kref_put has been
  5034. * invoked on this ndlp.
  5035. */
  5036. if (atomic_read(&ndlp->kref.refcount) == 1) {
  5037. /* Indicate ndlp is put to inactive state. */
  5038. NLP_SET_IACT_REQ(ndlp);
  5039. /* Acknowledge ndlp memory free has been seen. */
  5040. if (NLP_CHK_FREE_REQ(ndlp))
  5041. NLP_SET_FREE_ACK(ndlp);
  5042. }
  5043. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  5044. /* Note, the kref_put returns 1 when decrementing a reference
  5045. * count that was 1, it invokes the release callback function,
  5046. * but it still left the reference count as 1 (not actually
  5047. * performs the last decrementation). Otherwise, it actually
  5048. * decrements the reference count and returns 0.
  5049. */
  5050. return kref_put(&ndlp->kref, lpfc_nlp_release);
  5051. }
  5052. /* This routine free's the specified nodelist if it is not in use
  5053. * by any other discovery thread. This routine returns 1 if the
  5054. * ndlp has been freed. A return value of 0 indicates the ndlp is
  5055. * not yet been released.
  5056. */
  5057. int
  5058. lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
  5059. {
  5060. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  5061. "node not used: did:x%x flg:x%x refcnt:x%x",
  5062. ndlp->nlp_DID, ndlp->nlp_flag,
  5063. atomic_read(&ndlp->kref.refcount));
  5064. if (atomic_read(&ndlp->kref.refcount) == 1)
  5065. if (lpfc_nlp_put(ndlp))
  5066. return 1;
  5067. return 0;
  5068. }
  5069. /**
  5070. * lpfc_fcf_inuse - Check if FCF can be unregistered.
  5071. * @phba: Pointer to hba context object.
  5072. *
  5073. * This function iterate through all FC nodes associated
  5074. * will all vports to check if there is any node with
  5075. * fc_rports associated with it. If there is an fc_rport
  5076. * associated with the node, then the node is either in
  5077. * discovered state or its devloss_timer is pending.
  5078. */
  5079. static int
  5080. lpfc_fcf_inuse(struct lpfc_hba *phba)
  5081. {
  5082. struct lpfc_vport **vports;
  5083. int i, ret = 0;
  5084. struct lpfc_nodelist *ndlp;
  5085. struct Scsi_Host *shost;
  5086. vports = lpfc_create_vport_work_array(phba);
  5087. /* If driver cannot allocate memory, indicate fcf is in use */
  5088. if (!vports)
  5089. return 1;
  5090. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  5091. shost = lpfc_shost_from_vport(vports[i]);
  5092. spin_lock_irq(shost->host_lock);
  5093. /*
  5094. * IF the CVL_RCVD bit is not set then we have sent the
  5095. * flogi.
  5096. * If dev_loss fires while we are waiting we do not want to
  5097. * unreg the fcf.
  5098. */
  5099. if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
  5100. spin_unlock_irq(shost->host_lock);
  5101. ret = 1;
  5102. goto out;
  5103. }
  5104. list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
  5105. if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
  5106. (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
  5107. ret = 1;
  5108. spin_unlock_irq(shost->host_lock);
  5109. goto out;
  5110. } else {
  5111. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  5112. "2624 RPI %x DID %x flg %x still "
  5113. "logged in\n",
  5114. ndlp->nlp_rpi, ndlp->nlp_DID,
  5115. ndlp->nlp_flag);
  5116. if (ndlp->nlp_flag & NLP_RPI_REGISTERED)
  5117. ret = 1;
  5118. }
  5119. }
  5120. spin_unlock_irq(shost->host_lock);
  5121. }
  5122. out:
  5123. lpfc_destroy_vport_work_array(phba, vports);
  5124. return ret;
  5125. }
  5126. /**
  5127. * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
  5128. * @phba: Pointer to hba context object.
  5129. * @mboxq: Pointer to mailbox object.
  5130. *
  5131. * This function frees memory associated with the mailbox command.
  5132. */
  5133. static void
  5134. lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  5135. {
  5136. struct lpfc_vport *vport = mboxq->vport;
  5137. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  5138. if (mboxq->u.mb.mbxStatus) {
  5139. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  5140. "2555 UNREG_VFI mbxStatus error x%x "
  5141. "HBA state x%x\n",
  5142. mboxq->u.mb.mbxStatus, vport->port_state);
  5143. }
  5144. spin_lock_irq(shost->host_lock);
  5145. phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
  5146. spin_unlock_irq(shost->host_lock);
  5147. mempool_free(mboxq, phba->mbox_mem_pool);
  5148. return;
  5149. }
  5150. /**
  5151. * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
  5152. * @phba: Pointer to hba context object.
  5153. * @mboxq: Pointer to mailbox object.
  5154. *
  5155. * This function frees memory associated with the mailbox command.
  5156. */
  5157. static void
  5158. lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  5159. {
  5160. struct lpfc_vport *vport = mboxq->vport;
  5161. if (mboxq->u.mb.mbxStatus) {
  5162. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  5163. "2550 UNREG_FCFI mbxStatus error x%x "
  5164. "HBA state x%x\n",
  5165. mboxq->u.mb.mbxStatus, vport->port_state);
  5166. }
  5167. mempool_free(mboxq, phba->mbox_mem_pool);
  5168. return;
  5169. }
  5170. /**
  5171. * lpfc_unregister_fcf_prep - Unregister fcf record preparation
  5172. * @phba: Pointer to hba context object.
  5173. *
  5174. * This function prepare the HBA for unregistering the currently registered
  5175. * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
  5176. * VFIs.
  5177. */
  5178. int
  5179. lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
  5180. {
  5181. LPFC_MBOXQ_t *mbox;
  5182. struct lpfc_vport **vports;
  5183. struct lpfc_nodelist *ndlp;
  5184. struct Scsi_Host *shost;
  5185. int i, rc;
  5186. /* Unregister RPIs */
  5187. if (lpfc_fcf_inuse(phba))
  5188. lpfc_unreg_hba_rpis(phba);
  5189. /* At this point, all discovery is aborted */
  5190. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  5191. /* Unregister VPIs */
  5192. vports = lpfc_create_vport_work_array(phba);
  5193. if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
  5194. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  5195. /* Stop FLOGI/FDISC retries */
  5196. ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
  5197. if (ndlp)
  5198. lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
  5199. lpfc_cleanup_pending_mbox(vports[i]);
  5200. if (phba->sli_rev == LPFC_SLI_REV4)
  5201. lpfc_sli4_unreg_all_rpis(vports[i]);
  5202. lpfc_mbx_unreg_vpi(vports[i]);
  5203. shost = lpfc_shost_from_vport(vports[i]);
  5204. spin_lock_irq(shost->host_lock);
  5205. vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
  5206. vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
  5207. spin_unlock_irq(shost->host_lock);
  5208. }
  5209. lpfc_destroy_vport_work_array(phba, vports);
  5210. /* Cleanup any outstanding ELS commands */
  5211. lpfc_els_flush_all_cmd(phba);
  5212. /* Unregister VFI */
  5213. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5214. if (!mbox) {
  5215. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  5216. "2556 UNREG_VFI mbox allocation failed"
  5217. "HBA state x%x\n", phba->pport->port_state);
  5218. return -ENOMEM;
  5219. }
  5220. lpfc_unreg_vfi(mbox, phba->pport);
  5221. mbox->vport = phba->pport;
  5222. mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
  5223. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  5224. if (rc == MBX_NOT_FINISHED) {
  5225. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  5226. "2557 UNREG_VFI issue mbox failed rc x%x "
  5227. "HBA state x%x\n",
  5228. rc, phba->pport->port_state);
  5229. mempool_free(mbox, phba->mbox_mem_pool);
  5230. return -EIO;
  5231. }
  5232. shost = lpfc_shost_from_vport(phba->pport);
  5233. spin_lock_irq(shost->host_lock);
  5234. phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
  5235. spin_unlock_irq(shost->host_lock);
  5236. return 0;
  5237. }
  5238. /**
  5239. * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
  5240. * @phba: Pointer to hba context object.
  5241. *
  5242. * This function issues synchronous unregister FCF mailbox command to HBA to
  5243. * unregister the currently registered FCF record. The driver does not reset
  5244. * the driver FCF usage state flags.
  5245. *
  5246. * Return 0 if successfully issued, none-zero otherwise.
  5247. */
  5248. int
  5249. lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
  5250. {
  5251. LPFC_MBOXQ_t *mbox;
  5252. int rc;
  5253. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5254. if (!mbox) {
  5255. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  5256. "2551 UNREG_FCFI mbox allocation failed"
  5257. "HBA state x%x\n", phba->pport->port_state);
  5258. return -ENOMEM;
  5259. }
  5260. lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
  5261. mbox->vport = phba->pport;
  5262. mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
  5263. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  5264. if (rc == MBX_NOT_FINISHED) {
  5265. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5266. "2552 Unregister FCFI command failed rc x%x "
  5267. "HBA state x%x\n",
  5268. rc, phba->pport->port_state);
  5269. return -EINVAL;
  5270. }
  5271. return 0;
  5272. }
  5273. /**
  5274. * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
  5275. * @phba: Pointer to hba context object.
  5276. *
  5277. * This function unregisters the currently reigstered FCF. This function
  5278. * also tries to find another FCF for discovery by rescan the HBA FCF table.
  5279. */
  5280. void
  5281. lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
  5282. {
  5283. int rc;
  5284. /* Preparation for unregistering fcf */
  5285. rc = lpfc_unregister_fcf_prep(phba);
  5286. if (rc) {
  5287. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
  5288. "2748 Failed to prepare for unregistering "
  5289. "HBA's FCF record: rc=%d\n", rc);
  5290. return;
  5291. }
  5292. /* Now, unregister FCF record and reset HBA FCF state */
  5293. rc = lpfc_sli4_unregister_fcf(phba);
  5294. if (rc)
  5295. return;
  5296. /* Reset HBA FCF states after successful unregister FCF */
  5297. phba->fcf.fcf_flag = 0;
  5298. phba->fcf.current_rec.flag = 0;
  5299. /*
  5300. * If driver is not unloading, check if there is any other
  5301. * FCF record that can be used for discovery.
  5302. */
  5303. if ((phba->pport->load_flag & FC_UNLOADING) ||
  5304. (phba->link_state < LPFC_LINK_UP))
  5305. return;
  5306. /* This is considered as the initial FCF discovery scan */
  5307. spin_lock_irq(&phba->hbalock);
  5308. phba->fcf.fcf_flag |= FCF_INIT_DISC;
  5309. spin_unlock_irq(&phba->hbalock);
  5310. /* Reset FCF roundrobin bmask for new discovery */
  5311. lpfc_sli4_clear_fcf_rr_bmask(phba);
  5312. rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
  5313. if (rc) {
  5314. spin_lock_irq(&phba->hbalock);
  5315. phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
  5316. spin_unlock_irq(&phba->hbalock);
  5317. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  5318. "2553 lpfc_unregister_unused_fcf failed "
  5319. "to read FCF record HBA state x%x\n",
  5320. phba->pport->port_state);
  5321. }
  5322. }
  5323. /**
  5324. * lpfc_unregister_fcf - Unregister the currently registered fcf record
  5325. * @phba: Pointer to hba context object.
  5326. *
  5327. * This function just unregisters the currently reigstered FCF. It does not
  5328. * try to find another FCF for discovery.
  5329. */
  5330. void
  5331. lpfc_unregister_fcf(struct lpfc_hba *phba)
  5332. {
  5333. int rc;
  5334. /* Preparation for unregistering fcf */
  5335. rc = lpfc_unregister_fcf_prep(phba);
  5336. if (rc) {
  5337. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
  5338. "2749 Failed to prepare for unregistering "
  5339. "HBA's FCF record: rc=%d\n", rc);
  5340. return;
  5341. }
  5342. /* Now, unregister FCF record and reset HBA FCF state */
  5343. rc = lpfc_sli4_unregister_fcf(phba);
  5344. if (rc)
  5345. return;
  5346. /* Set proper HBA FCF states after successful unregister FCF */
  5347. spin_lock_irq(&phba->hbalock);
  5348. phba->fcf.fcf_flag &= ~FCF_REGISTERED;
  5349. spin_unlock_irq(&phba->hbalock);
  5350. }
  5351. /**
  5352. * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
  5353. * @phba: Pointer to hba context object.
  5354. *
  5355. * This function check if there are any connected remote port for the FCF and
  5356. * if all the devices are disconnected, this function unregister FCFI.
  5357. * This function also tries to use another FCF for discovery.
  5358. */
  5359. void
  5360. lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
  5361. {
  5362. /*
  5363. * If HBA is not running in FIP mode, if HBA does not support
  5364. * FCoE, if FCF discovery is ongoing, or if FCF has not been
  5365. * registered, do nothing.
  5366. */
  5367. spin_lock_irq(&phba->hbalock);
  5368. if (!(phba->hba_flag & HBA_FCOE_MODE) ||
  5369. !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
  5370. !(phba->hba_flag & HBA_FIP_SUPPORT) ||
  5371. (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
  5372. (phba->pport->port_state == LPFC_FLOGI)) {
  5373. spin_unlock_irq(&phba->hbalock);
  5374. return;
  5375. }
  5376. spin_unlock_irq(&phba->hbalock);
  5377. if (lpfc_fcf_inuse(phba))
  5378. return;
  5379. lpfc_unregister_fcf_rescan(phba);
  5380. }
  5381. /**
  5382. * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
  5383. * @phba: Pointer to hba context object.
  5384. * @buff: Buffer containing the FCF connection table as in the config
  5385. * region.
  5386. * This function create driver data structure for the FCF connection
  5387. * record table read from config region 23.
  5388. */
  5389. static void
  5390. lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
  5391. uint8_t *buff)
  5392. {
  5393. struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
  5394. struct lpfc_fcf_conn_hdr *conn_hdr;
  5395. struct lpfc_fcf_conn_rec *conn_rec;
  5396. uint32_t record_count;
  5397. int i;
  5398. /* Free the current connect table */
  5399. list_for_each_entry_safe(conn_entry, next_conn_entry,
  5400. &phba->fcf_conn_rec_list, list) {
  5401. list_del_init(&conn_entry->list);
  5402. kfree(conn_entry);
  5403. }
  5404. conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
  5405. record_count = conn_hdr->length * sizeof(uint32_t)/
  5406. sizeof(struct lpfc_fcf_conn_rec);
  5407. conn_rec = (struct lpfc_fcf_conn_rec *)
  5408. (buff + sizeof(struct lpfc_fcf_conn_hdr));
  5409. for (i = 0; i < record_count; i++) {
  5410. if (!(conn_rec[i].flags & FCFCNCT_VALID))
  5411. continue;
  5412. conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
  5413. GFP_KERNEL);
  5414. if (!conn_entry) {
  5415. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5416. "2566 Failed to allocate connection"
  5417. " table entry\n");
  5418. return;
  5419. }
  5420. memcpy(&conn_entry->conn_rec, &conn_rec[i],
  5421. sizeof(struct lpfc_fcf_conn_rec));
  5422. conn_entry->conn_rec.vlan_tag =
  5423. le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
  5424. conn_entry->conn_rec.flags =
  5425. le16_to_cpu(conn_entry->conn_rec.flags);
  5426. list_add_tail(&conn_entry->list,
  5427. &phba->fcf_conn_rec_list);
  5428. }
  5429. }
  5430. /**
  5431. * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
  5432. * @phba: Pointer to hba context object.
  5433. * @buff: Buffer containing the FCoE parameter data structure.
  5434. *
  5435. * This function update driver data structure with config
  5436. * parameters read from config region 23.
  5437. */
  5438. static void
  5439. lpfc_read_fcoe_param(struct lpfc_hba *phba,
  5440. uint8_t *buff)
  5441. {
  5442. struct lpfc_fip_param_hdr *fcoe_param_hdr;
  5443. struct lpfc_fcoe_params *fcoe_param;
  5444. fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
  5445. buff;
  5446. fcoe_param = (struct lpfc_fcoe_params *)
  5447. (buff + sizeof(struct lpfc_fip_param_hdr));
  5448. if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
  5449. (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
  5450. return;
  5451. if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
  5452. phba->valid_vlan = 1;
  5453. phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
  5454. 0xFFF;
  5455. }
  5456. phba->fc_map[0] = fcoe_param->fc_map[0];
  5457. phba->fc_map[1] = fcoe_param->fc_map[1];
  5458. phba->fc_map[2] = fcoe_param->fc_map[2];
  5459. return;
  5460. }
  5461. /**
  5462. * lpfc_get_rec_conf23 - Get a record type in config region data.
  5463. * @buff: Buffer containing config region 23 data.
  5464. * @size: Size of the data buffer.
  5465. * @rec_type: Record type to be searched.
  5466. *
  5467. * This function searches config region data to find the beginning
  5468. * of the record specified by record_type. If record found, this
  5469. * function return pointer to the record else return NULL.
  5470. */
  5471. static uint8_t *
  5472. lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
  5473. {
  5474. uint32_t offset = 0, rec_length;
  5475. if ((buff[0] == LPFC_REGION23_LAST_REC) ||
  5476. (size < sizeof(uint32_t)))
  5477. return NULL;
  5478. rec_length = buff[offset + 1];
  5479. /*
  5480. * One TLV record has one word header and number of data words
  5481. * specified in the rec_length field of the record header.
  5482. */
  5483. while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
  5484. <= size) {
  5485. if (buff[offset] == rec_type)
  5486. return &buff[offset];
  5487. if (buff[offset] == LPFC_REGION23_LAST_REC)
  5488. return NULL;
  5489. offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
  5490. rec_length = buff[offset + 1];
  5491. }
  5492. return NULL;
  5493. }
  5494. /**
  5495. * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
  5496. * @phba: Pointer to lpfc_hba data structure.
  5497. * @buff: Buffer containing config region 23 data.
  5498. * @size: Size of the data buffer.
  5499. *
  5500. * This function parses the FCoE config parameters in config region 23 and
  5501. * populate driver data structure with the parameters.
  5502. */
  5503. void
  5504. lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
  5505. uint8_t *buff,
  5506. uint32_t size)
  5507. {
  5508. uint32_t offset = 0, rec_length;
  5509. uint8_t *rec_ptr;
  5510. /*
  5511. * If data size is less than 2 words signature and version cannot be
  5512. * verified.
  5513. */
  5514. if (size < 2*sizeof(uint32_t))
  5515. return;
  5516. /* Check the region signature first */
  5517. if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
  5518. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5519. "2567 Config region 23 has bad signature\n");
  5520. return;
  5521. }
  5522. offset += 4;
  5523. /* Check the data structure version */
  5524. if (buff[offset] != LPFC_REGION23_VERSION) {
  5525. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5526. "2568 Config region 23 has bad version\n");
  5527. return;
  5528. }
  5529. offset += 4;
  5530. rec_length = buff[offset + 1];
  5531. /* Read FCoE param record */
  5532. rec_ptr = lpfc_get_rec_conf23(&buff[offset],
  5533. size - offset, FCOE_PARAM_TYPE);
  5534. if (rec_ptr)
  5535. lpfc_read_fcoe_param(phba, rec_ptr);
  5536. /* Read FCF connection table */
  5537. rec_ptr = lpfc_get_rec_conf23(&buff[offset],
  5538. size - offset, FCOE_CONN_TBL_TYPE);
  5539. if (rec_ptr)
  5540. lpfc_read_fcf_conn_tbl(phba, rec_ptr);
  5541. }