lpfc_init.c 460 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2017-2024 Broadcom. All Rights Reserved. The term *
  5. * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
  6. * Copyright (C) 2004-2016 Emulex. All rights reserved. *
  7. * EMULEX and SLI are trademarks of Emulex. *
  8. * www.broadcom.com *
  9. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  10. * *
  11. * This program is free software; you can redistribute it and/or *
  12. * modify it under the terms of version 2 of the GNU General *
  13. * Public License as published by the Free Software Foundation. *
  14. * This program is distributed in the hope that it will be useful. *
  15. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  16. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  17. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  18. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  19. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  20. * more details, a copy of which can be found in the file COPYING *
  21. * included with this package. *
  22. *******************************************************************/
  23. #include <linux/blkdev.h>
  24. #include <linux/delay.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/idr.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/module.h>
  29. #include <linux/kthread.h>
  30. #include <linux/pci.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/sched/clock.h>
  33. #include <linux/ctype.h>
  34. #include <linux/slab.h>
  35. #include <linux/firmware.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/percpu.h>
  38. #include <linux/irq.h>
  39. #include <linux/bitops.h>
  40. #include <linux/crash_dump.h>
  41. #include <linux/cpu.h>
  42. #include <linux/cpuhotplug.h>
  43. #include <scsi/scsi.h>
  44. #include <scsi/scsi_device.h>
  45. #include <scsi/scsi_host.h>
  46. #include <scsi/scsi_transport_fc.h>
  47. #include <scsi/scsi_tcq.h>
  48. #include <scsi/fc/fc_fs.h>
  49. #include "lpfc_hw4.h"
  50. #include "lpfc_hw.h"
  51. #include "lpfc_sli.h"
  52. #include "lpfc_sli4.h"
  53. #include "lpfc_nl.h"
  54. #include "lpfc_disc.h"
  55. #include "lpfc.h"
  56. #include "lpfc_scsi.h"
  57. #include "lpfc_nvme.h"
  58. #include "lpfc_logmsg.h"
  59. #include "lpfc_crtn.h"
  60. #include "lpfc_vport.h"
  61. #include "lpfc_version.h"
  62. #include "lpfc_ids.h"
  63. static enum cpuhp_state lpfc_cpuhp_state;
  64. /* Used when mapping IRQ vectors in a driver centric manner */
  65. static uint32_t lpfc_present_cpu;
  66. static bool lpfc_pldv_detect;
  67. static void __lpfc_cpuhp_remove(struct lpfc_hba *phba);
  68. static void lpfc_cpuhp_remove(struct lpfc_hba *phba);
  69. static void lpfc_cpuhp_add(struct lpfc_hba *phba);
  70. static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
  71. static int lpfc_post_rcv_buf(struct lpfc_hba *);
  72. static int lpfc_sli4_queue_verify(struct lpfc_hba *);
  73. static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
  74. static int lpfc_setup_endian_order(struct lpfc_hba *);
  75. static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
  76. static void lpfc_free_els_sgl_list(struct lpfc_hba *);
  77. static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
  78. static void lpfc_init_sgl_list(struct lpfc_hba *);
  79. static int lpfc_init_active_sgl_array(struct lpfc_hba *);
  80. static void lpfc_free_active_sgl(struct lpfc_hba *);
  81. static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
  82. static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
  83. static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
  84. static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
  85. static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
  86. static void lpfc_sli4_disable_intr(struct lpfc_hba *);
  87. static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
  88. static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
  89. static uint16_t lpfc_find_cpu_handle(struct lpfc_hba *, uint16_t, int);
  90. static void lpfc_setup_bg(struct lpfc_hba *, struct Scsi_Host *);
  91. static int lpfc_sli4_cgn_parm_chg_evt(struct lpfc_hba *);
  92. static void lpfc_sli4_async_cmstat_evt(struct lpfc_hba *phba);
  93. static void lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba);
  94. static struct scsi_transport_template *lpfc_transport_template = NULL;
  95. static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
  96. static DEFINE_IDR(lpfc_hba_index);
  97. #define LPFC_NVMET_BUF_POST 254
  98. static int lpfc_vmid_res_alloc(struct lpfc_hba *phba, struct lpfc_vport *vport);
  99. static void lpfc_cgn_update_tstamp(struct lpfc_hba *phba, struct lpfc_cgn_ts *ts);
  100. /**
  101. * lpfc_config_port_prep - Perform lpfc initialization prior to config port
  102. * @phba: pointer to lpfc hba data structure.
  103. *
  104. * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
  105. * mailbox command. It retrieves the revision information from the HBA and
  106. * collects the Vital Product Data (VPD) about the HBA for preparing the
  107. * configuration of the HBA.
  108. *
  109. * Return codes:
  110. * 0 - success.
  111. * -ERESTART - requests the SLI layer to reset the HBA and try again.
  112. * Any other value - indicates an error.
  113. **/
  114. int
  115. lpfc_config_port_prep(struct lpfc_hba *phba)
  116. {
  117. lpfc_vpd_t *vp = &phba->vpd;
  118. int i = 0, rc;
  119. LPFC_MBOXQ_t *pmb;
  120. MAILBOX_t *mb;
  121. char *lpfc_vpd_data = NULL;
  122. uint16_t offset = 0;
  123. static char licensed[56] =
  124. "key unlock for use with gnu public licensed code only\0";
  125. static int init_key = 1;
  126. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  127. if (!pmb) {
  128. phba->link_state = LPFC_HBA_ERROR;
  129. return -ENOMEM;
  130. }
  131. mb = &pmb->u.mb;
  132. phba->link_state = LPFC_INIT_MBX_CMDS;
  133. if (lpfc_is_LC_HBA(phba->pcidev->device)) {
  134. if (init_key) {
  135. uint32_t *ptext = (uint32_t *) licensed;
  136. for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
  137. *ptext = cpu_to_be32(*ptext);
  138. init_key = 0;
  139. }
  140. lpfc_read_nv(phba, pmb);
  141. memset((char*)mb->un.varRDnvp.rsvd3, 0,
  142. sizeof (mb->un.varRDnvp.rsvd3));
  143. memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
  144. sizeof (licensed));
  145. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  146. if (rc != MBX_SUCCESS) {
  147. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  148. "0324 Config Port initialization "
  149. "error, mbxCmd x%x READ_NVPARM, "
  150. "mbxStatus x%x\n",
  151. mb->mbxCommand, mb->mbxStatus);
  152. mempool_free(pmb, phba->mbox_mem_pool);
  153. return -ERESTART;
  154. }
  155. memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
  156. sizeof(phba->wwnn));
  157. memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
  158. sizeof(phba->wwpn));
  159. }
  160. /*
  161. * Clear all option bits except LPFC_SLI3_BG_ENABLED,
  162. * which was already set in lpfc_get_cfgparam()
  163. */
  164. phba->sli3_options &= (uint32_t)LPFC_SLI3_BG_ENABLED;
  165. /* Setup and issue mailbox READ REV command */
  166. lpfc_read_rev(phba, pmb);
  167. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  168. if (rc != MBX_SUCCESS) {
  169. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  170. "0439 Adapter failed to init, mbxCmd x%x "
  171. "READ_REV, mbxStatus x%x\n",
  172. mb->mbxCommand, mb->mbxStatus);
  173. mempool_free( pmb, phba->mbox_mem_pool);
  174. return -ERESTART;
  175. }
  176. /*
  177. * The value of rr must be 1 since the driver set the cv field to 1.
  178. * This setting requires the FW to set all revision fields.
  179. */
  180. if (mb->un.varRdRev.rr == 0) {
  181. vp->rev.rBit = 0;
  182. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  183. "0440 Adapter failed to init, READ_REV has "
  184. "missing revision information.\n");
  185. mempool_free(pmb, phba->mbox_mem_pool);
  186. return -ERESTART;
  187. }
  188. if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
  189. mempool_free(pmb, phba->mbox_mem_pool);
  190. return -EINVAL;
  191. }
  192. /* Save information as VPD data */
  193. vp->rev.rBit = 1;
  194. memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
  195. vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
  196. memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
  197. vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
  198. memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
  199. vp->rev.biuRev = mb->un.varRdRev.biuRev;
  200. vp->rev.smRev = mb->un.varRdRev.smRev;
  201. vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
  202. vp->rev.endecRev = mb->un.varRdRev.endecRev;
  203. vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
  204. vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
  205. vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
  206. vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
  207. vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
  208. vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
  209. /* If the sli feature level is less then 9, we must
  210. * tear down all RPIs and VPIs on link down if NPIV
  211. * is enabled.
  212. */
  213. if (vp->rev.feaLevelHigh < 9)
  214. phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
  215. if (lpfc_is_LC_HBA(phba->pcidev->device))
  216. memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
  217. sizeof (phba->RandomData));
  218. /* Get adapter VPD information */
  219. lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
  220. if (!lpfc_vpd_data)
  221. goto out_free_mbox;
  222. do {
  223. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
  224. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  225. if (rc != MBX_SUCCESS) {
  226. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  227. "0441 VPD not present on adapter, "
  228. "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
  229. mb->mbxCommand, mb->mbxStatus);
  230. mb->un.varDmp.word_cnt = 0;
  231. }
  232. /* dump mem may return a zero when finished or we got a
  233. * mailbox error, either way we are done.
  234. */
  235. if (mb->un.varDmp.word_cnt == 0)
  236. break;
  237. if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
  238. mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
  239. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  240. lpfc_vpd_data + offset,
  241. mb->un.varDmp.word_cnt);
  242. offset += mb->un.varDmp.word_cnt;
  243. } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
  244. lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
  245. kfree(lpfc_vpd_data);
  246. out_free_mbox:
  247. mempool_free(pmb, phba->mbox_mem_pool);
  248. return 0;
  249. }
  250. /**
  251. * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
  252. * @phba: pointer to lpfc hba data structure.
  253. * @pmboxq: pointer to the driver internal queue element for mailbox command.
  254. *
  255. * This is the completion handler for driver's configuring asynchronous event
  256. * mailbox command to the device. If the mailbox command returns successfully,
  257. * it will set internal async event support flag to 1; otherwise, it will
  258. * set internal async event support flag to 0.
  259. **/
  260. static void
  261. lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
  262. {
  263. if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
  264. phba->temp_sensor_support = 1;
  265. else
  266. phba->temp_sensor_support = 0;
  267. mempool_free(pmboxq, phba->mbox_mem_pool);
  268. return;
  269. }
  270. /**
  271. * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
  272. * @phba: pointer to lpfc hba data structure.
  273. * @pmboxq: pointer to the driver internal queue element for mailbox command.
  274. *
  275. * This is the completion handler for dump mailbox command for getting
  276. * wake up parameters. When this command complete, the response contain
  277. * Option rom version of the HBA. This function translate the version number
  278. * into a human readable string and store it in OptionROMVersion.
  279. **/
  280. static void
  281. lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  282. {
  283. struct prog_id *prg;
  284. uint32_t prog_id_word;
  285. char dist = ' ';
  286. /* character array used for decoding dist type. */
  287. char dist_char[] = "nabx";
  288. if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
  289. mempool_free(pmboxq, phba->mbox_mem_pool);
  290. return;
  291. }
  292. prg = (struct prog_id *) &prog_id_word;
  293. /* word 7 contain option rom version */
  294. prog_id_word = pmboxq->u.mb.un.varWords[7];
  295. /* Decode the Option rom version word to a readable string */
  296. dist = dist_char[prg->dist];
  297. if ((prg->dist == 3) && (prg->num == 0))
  298. snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
  299. prg->ver, prg->rev, prg->lev);
  300. else
  301. snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
  302. prg->ver, prg->rev, prg->lev,
  303. dist, prg->num);
  304. mempool_free(pmboxq, phba->mbox_mem_pool);
  305. return;
  306. }
  307. /**
  308. * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
  309. * @vport: pointer to lpfc vport data structure.
  310. *
  311. *
  312. * Return codes
  313. * None.
  314. **/
  315. void
  316. lpfc_update_vport_wwn(struct lpfc_vport *vport)
  317. {
  318. struct lpfc_hba *phba = vport->phba;
  319. /*
  320. * If the name is empty or there exists a soft name
  321. * then copy the service params name, otherwise use the fc name
  322. */
  323. if (vport->fc_nodename.u.wwn[0] == 0)
  324. memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
  325. sizeof(struct lpfc_name));
  326. else
  327. memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
  328. sizeof(struct lpfc_name));
  329. /*
  330. * If the port name has changed, then set the Param changes flag
  331. * to unreg the login
  332. */
  333. if (vport->fc_portname.u.wwn[0] != 0 &&
  334. memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
  335. sizeof(struct lpfc_name))) {
  336. vport->vport_flag |= FAWWPN_PARAM_CHG;
  337. if (phba->sli_rev == LPFC_SLI_REV4 &&
  338. vport->port_type == LPFC_PHYSICAL_PORT &&
  339. phba->sli4_hba.fawwpn_flag & LPFC_FAWWPN_FABRIC) {
  340. if (!(phba->sli4_hba.fawwpn_flag & LPFC_FAWWPN_CONFIG))
  341. phba->sli4_hba.fawwpn_flag &=
  342. ~LPFC_FAWWPN_FABRIC;
  343. lpfc_printf_log(phba, KERN_INFO,
  344. LOG_SLI | LOG_DISCOVERY | LOG_ELS,
  345. "2701 FA-PWWN change WWPN from %llx to "
  346. "%llx: vflag x%x fawwpn_flag x%x\n",
  347. wwn_to_u64(vport->fc_portname.u.wwn),
  348. wwn_to_u64
  349. (vport->fc_sparam.portName.u.wwn),
  350. vport->vport_flag,
  351. phba->sli4_hba.fawwpn_flag);
  352. memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
  353. sizeof(struct lpfc_name));
  354. }
  355. }
  356. if (vport->fc_portname.u.wwn[0] == 0)
  357. memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
  358. sizeof(struct lpfc_name));
  359. else
  360. memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
  361. sizeof(struct lpfc_name));
  362. }
  363. /**
  364. * lpfc_config_port_post - Perform lpfc initialization after config port
  365. * @phba: pointer to lpfc hba data structure.
  366. *
  367. * This routine will do LPFC initialization after the CONFIG_PORT mailbox
  368. * command call. It performs all internal resource and state setups on the
  369. * port: post IOCB buffers, enable appropriate host interrupt attentions,
  370. * ELS ring timers, etc.
  371. *
  372. * Return codes
  373. * 0 - success.
  374. * Any other value - error.
  375. **/
  376. int
  377. lpfc_config_port_post(struct lpfc_hba *phba)
  378. {
  379. struct lpfc_vport *vport = phba->pport;
  380. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  381. LPFC_MBOXQ_t *pmb;
  382. MAILBOX_t *mb;
  383. struct lpfc_dmabuf *mp;
  384. struct lpfc_sli *psli = &phba->sli;
  385. uint32_t status, timeout;
  386. int i, j;
  387. int rc;
  388. spin_lock_irq(&phba->hbalock);
  389. /*
  390. * If the Config port completed correctly the HBA is not
  391. * over heated any more.
  392. */
  393. if (phba->over_temp_state == HBA_OVER_TEMP)
  394. phba->over_temp_state = HBA_NORMAL_TEMP;
  395. spin_unlock_irq(&phba->hbalock);
  396. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  397. if (!pmb) {
  398. phba->link_state = LPFC_HBA_ERROR;
  399. return -ENOMEM;
  400. }
  401. mb = &pmb->u.mb;
  402. /* Get login parameters for NID. */
  403. rc = lpfc_read_sparam(phba, pmb, 0);
  404. if (rc) {
  405. mempool_free(pmb, phba->mbox_mem_pool);
  406. return -ENOMEM;
  407. }
  408. pmb->vport = vport;
  409. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  410. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  411. "0448 Adapter failed init, mbxCmd x%x "
  412. "READ_SPARM mbxStatus x%x\n",
  413. mb->mbxCommand, mb->mbxStatus);
  414. phba->link_state = LPFC_HBA_ERROR;
  415. lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
  416. return -EIO;
  417. }
  418. mp = pmb->ctx_buf;
  419. /* This dmabuf was allocated by lpfc_read_sparam. The dmabuf is no
  420. * longer needed. Prevent unintended ctx_buf access as the mbox is
  421. * reused.
  422. */
  423. memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
  424. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  425. kfree(mp);
  426. pmb->ctx_buf = NULL;
  427. lpfc_update_vport_wwn(vport);
  428. /* Update the fc_host data structures with new wwn. */
  429. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  430. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  431. fc_host_max_npiv_vports(shost) = phba->max_vpi;
  432. /* If no serial number in VPD data, use low 6 bytes of WWNN */
  433. /* This should be consolidated into parse_vpd ? - mr */
  434. if (phba->SerialNumber[0] == 0) {
  435. uint8_t *outptr;
  436. outptr = &vport->fc_nodename.u.s.IEEE[0];
  437. for (i = 0; i < 12; i++) {
  438. status = *outptr++;
  439. j = ((status & 0xf0) >> 4);
  440. if (j <= 9)
  441. phba->SerialNumber[i] =
  442. (char)((uint8_t) 0x30 + (uint8_t) j);
  443. else
  444. phba->SerialNumber[i] =
  445. (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
  446. i++;
  447. j = (status & 0xf);
  448. if (j <= 9)
  449. phba->SerialNumber[i] =
  450. (char)((uint8_t) 0x30 + (uint8_t) j);
  451. else
  452. phba->SerialNumber[i] =
  453. (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
  454. }
  455. }
  456. lpfc_read_config(phba, pmb);
  457. pmb->vport = vport;
  458. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  459. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  460. "0453 Adapter failed to init, mbxCmd x%x "
  461. "READ_CONFIG, mbxStatus x%x\n",
  462. mb->mbxCommand, mb->mbxStatus);
  463. phba->link_state = LPFC_HBA_ERROR;
  464. mempool_free( pmb, phba->mbox_mem_pool);
  465. return -EIO;
  466. }
  467. /* Check if the port is disabled */
  468. lpfc_sli_read_link_ste(phba);
  469. /* Reset the DFT_HBA_Q_DEPTH to the max xri */
  470. if (phba->cfg_hba_queue_depth > mb->un.varRdConfig.max_xri) {
  471. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  472. "3359 HBA queue depth changed from %d to %d\n",
  473. phba->cfg_hba_queue_depth,
  474. mb->un.varRdConfig.max_xri);
  475. phba->cfg_hba_queue_depth = mb->un.varRdConfig.max_xri;
  476. }
  477. phba->lmt = mb->un.varRdConfig.lmt;
  478. /* Get the default values for Model Name and Description */
  479. lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
  480. phba->link_state = LPFC_LINK_DOWN;
  481. /* Only process IOCBs on ELS ring till hba_state is READY */
  482. if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
  483. psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
  484. if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
  485. psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
  486. /* Post receive buffers for desired rings */
  487. if (phba->sli_rev != 3)
  488. lpfc_post_rcv_buf(phba);
  489. /*
  490. * Configure HBA MSI-X attention conditions to messages if MSI-X mode
  491. */
  492. if (phba->intr_type == MSIX) {
  493. rc = lpfc_config_msi(phba, pmb);
  494. if (rc) {
  495. mempool_free(pmb, phba->mbox_mem_pool);
  496. return -EIO;
  497. }
  498. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  499. if (rc != MBX_SUCCESS) {
  500. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  501. "0352 Config MSI mailbox command "
  502. "failed, mbxCmd x%x, mbxStatus x%x\n",
  503. pmb->u.mb.mbxCommand,
  504. pmb->u.mb.mbxStatus);
  505. mempool_free(pmb, phba->mbox_mem_pool);
  506. return -EIO;
  507. }
  508. }
  509. spin_lock_irq(&phba->hbalock);
  510. /* Initialize ERATT handling flag */
  511. clear_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
  512. /* Enable appropriate host interrupts */
  513. if (lpfc_readl(phba->HCregaddr, &status)) {
  514. spin_unlock_irq(&phba->hbalock);
  515. return -EIO;
  516. }
  517. status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
  518. if (psli->num_rings > 0)
  519. status |= HC_R0INT_ENA;
  520. if (psli->num_rings > 1)
  521. status |= HC_R1INT_ENA;
  522. if (psli->num_rings > 2)
  523. status |= HC_R2INT_ENA;
  524. if (psli->num_rings > 3)
  525. status |= HC_R3INT_ENA;
  526. if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
  527. (phba->cfg_poll & DISABLE_FCP_RING_INT))
  528. status &= ~(HC_R0INT_ENA);
  529. writel(status, phba->HCregaddr);
  530. readl(phba->HCregaddr); /* flush */
  531. spin_unlock_irq(&phba->hbalock);
  532. /* Set up ring-0 (ELS) timer */
  533. timeout = phba->fc_ratov * 2;
  534. mod_timer(&vport->els_tmofunc,
  535. jiffies + msecs_to_jiffies(1000 * timeout));
  536. /* Set up heart beat (HB) timer */
  537. mod_timer(&phba->hb_tmofunc,
  538. jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
  539. clear_bit(HBA_HBEAT_INP, &phba->hba_flag);
  540. clear_bit(HBA_HBEAT_TMO, &phba->hba_flag);
  541. phba->last_completion_time = jiffies;
  542. /* Set up error attention (ERATT) polling timer */
  543. mod_timer(&phba->eratt_poll,
  544. jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
  545. if (test_bit(LINK_DISABLED, &phba->hba_flag)) {
  546. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  547. "2598 Adapter Link is disabled.\n");
  548. lpfc_down_link(phba, pmb);
  549. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  550. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  551. if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
  552. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  553. "2599 Adapter failed to issue DOWN_LINK"
  554. " mbox command rc 0x%x\n", rc);
  555. mempool_free(pmb, phba->mbox_mem_pool);
  556. return -EIO;
  557. }
  558. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  559. mempool_free(pmb, phba->mbox_mem_pool);
  560. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  561. if (rc)
  562. return rc;
  563. }
  564. /* MBOX buffer will be freed in mbox compl */
  565. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  566. if (!pmb) {
  567. phba->link_state = LPFC_HBA_ERROR;
  568. return -ENOMEM;
  569. }
  570. lpfc_config_async(phba, pmb, LPFC_ELS_RING);
  571. pmb->mbox_cmpl = lpfc_config_async_cmpl;
  572. pmb->vport = phba->pport;
  573. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  574. if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
  575. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  576. "0456 Adapter failed to issue "
  577. "ASYNCEVT_ENABLE mbox status x%x\n",
  578. rc);
  579. mempool_free(pmb, phba->mbox_mem_pool);
  580. }
  581. /* Get Option rom version */
  582. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  583. if (!pmb) {
  584. phba->link_state = LPFC_HBA_ERROR;
  585. return -ENOMEM;
  586. }
  587. lpfc_dump_wakeup_param(phba, pmb);
  588. pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
  589. pmb->vport = phba->pport;
  590. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  591. if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
  592. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  593. "0435 Adapter failed "
  594. "to get Option ROM version status x%x\n", rc);
  595. mempool_free(pmb, phba->mbox_mem_pool);
  596. }
  597. return 0;
  598. }
  599. /**
  600. * lpfc_sli4_refresh_params - update driver copy of params.
  601. * @phba: Pointer to HBA context object.
  602. *
  603. * This is called to refresh driver copy of dynamic fields from the
  604. * common_get_sli4_parameters descriptor.
  605. **/
  606. int
  607. lpfc_sli4_refresh_params(struct lpfc_hba *phba)
  608. {
  609. LPFC_MBOXQ_t *mboxq;
  610. struct lpfc_mqe *mqe;
  611. struct lpfc_sli4_parameters *mbx_sli4_parameters;
  612. int length, rc;
  613. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  614. if (!mboxq)
  615. return -ENOMEM;
  616. mqe = &mboxq->u.mqe;
  617. /* Read the port's SLI4 Config Parameters */
  618. length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
  619. sizeof(struct lpfc_sli4_cfg_mhdr));
  620. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  621. LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
  622. length, LPFC_SLI4_MBX_EMBED);
  623. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  624. if (unlikely(rc)) {
  625. mempool_free(mboxq, phba->mbox_mem_pool);
  626. return rc;
  627. }
  628. mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
  629. phba->sli4_hba.pc_sli4_params.mi_cap =
  630. bf_get(cfg_mi_ver, mbx_sli4_parameters);
  631. /* Are we forcing MI off via module parameter? */
  632. if (phba->cfg_enable_mi)
  633. phba->sli4_hba.pc_sli4_params.mi_ver =
  634. bf_get(cfg_mi_ver, mbx_sli4_parameters);
  635. else
  636. phba->sli4_hba.pc_sli4_params.mi_ver = 0;
  637. phba->sli4_hba.pc_sli4_params.cmf =
  638. bf_get(cfg_cmf, mbx_sli4_parameters);
  639. phba->sli4_hba.pc_sli4_params.pls =
  640. bf_get(cfg_pvl, mbx_sli4_parameters);
  641. mempool_free(mboxq, phba->mbox_mem_pool);
  642. return rc;
  643. }
  644. /**
  645. * lpfc_hba_init_link - Initialize the FC link
  646. * @phba: pointer to lpfc hba data structure.
  647. * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
  648. *
  649. * This routine will issue the INIT_LINK mailbox command call.
  650. * It is available to other drivers through the lpfc_hba data
  651. * structure for use as a delayed link up mechanism with the
  652. * module parameter lpfc_suppress_link_up.
  653. *
  654. * Return code
  655. * 0 - success
  656. * Any other value - error
  657. **/
  658. static int
  659. lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
  660. {
  661. return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
  662. }
  663. /**
  664. * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
  665. * @phba: pointer to lpfc hba data structure.
  666. * @fc_topology: desired fc topology.
  667. * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
  668. *
  669. * This routine will issue the INIT_LINK mailbox command call.
  670. * It is available to other drivers through the lpfc_hba data
  671. * structure for use as a delayed link up mechanism with the
  672. * module parameter lpfc_suppress_link_up.
  673. *
  674. * Return code
  675. * 0 - success
  676. * Any other value - error
  677. **/
  678. int
  679. lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
  680. uint32_t flag)
  681. {
  682. struct lpfc_vport *vport = phba->pport;
  683. LPFC_MBOXQ_t *pmb;
  684. MAILBOX_t *mb;
  685. int rc;
  686. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  687. if (!pmb) {
  688. phba->link_state = LPFC_HBA_ERROR;
  689. return -ENOMEM;
  690. }
  691. mb = &pmb->u.mb;
  692. pmb->vport = vport;
  693. if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
  694. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
  695. !(phba->lmt & LMT_1Gb)) ||
  696. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
  697. !(phba->lmt & LMT_2Gb)) ||
  698. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
  699. !(phba->lmt & LMT_4Gb)) ||
  700. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
  701. !(phba->lmt & LMT_8Gb)) ||
  702. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
  703. !(phba->lmt & LMT_10Gb)) ||
  704. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
  705. !(phba->lmt & LMT_16Gb)) ||
  706. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
  707. !(phba->lmt & LMT_32Gb)) ||
  708. ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_64G) &&
  709. !(phba->lmt & LMT_64Gb))) {
  710. /* Reset link speed to auto */
  711. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  712. "1302 Invalid speed for this board:%d "
  713. "Reset link speed to auto.\n",
  714. phba->cfg_link_speed);
  715. phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
  716. }
  717. lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
  718. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  719. if (phba->sli_rev < LPFC_SLI_REV4)
  720. lpfc_set_loopback_flag(phba);
  721. rc = lpfc_sli_issue_mbox(phba, pmb, flag);
  722. if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
  723. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  724. "0498 Adapter failed to init, mbxCmd x%x "
  725. "INIT_LINK, mbxStatus x%x\n",
  726. mb->mbxCommand, mb->mbxStatus);
  727. if (phba->sli_rev <= LPFC_SLI_REV3) {
  728. /* Clear all interrupt enable conditions */
  729. writel(0, phba->HCregaddr);
  730. readl(phba->HCregaddr); /* flush */
  731. /* Clear all pending interrupts */
  732. writel(0xffffffff, phba->HAregaddr);
  733. readl(phba->HAregaddr); /* flush */
  734. }
  735. phba->link_state = LPFC_HBA_ERROR;
  736. if (rc != MBX_BUSY || flag == MBX_POLL)
  737. mempool_free(pmb, phba->mbox_mem_pool);
  738. return -EIO;
  739. }
  740. phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
  741. if (flag == MBX_POLL)
  742. mempool_free(pmb, phba->mbox_mem_pool);
  743. return 0;
  744. }
  745. /**
  746. * lpfc_hba_down_link - this routine downs the FC link
  747. * @phba: pointer to lpfc hba data structure.
  748. * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
  749. *
  750. * This routine will issue the DOWN_LINK mailbox command call.
  751. * It is available to other drivers through the lpfc_hba data
  752. * structure for use to stop the link.
  753. *
  754. * Return code
  755. * 0 - success
  756. * Any other value - error
  757. **/
  758. static int
  759. lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
  760. {
  761. LPFC_MBOXQ_t *pmb;
  762. int rc;
  763. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  764. if (!pmb) {
  765. phba->link_state = LPFC_HBA_ERROR;
  766. return -ENOMEM;
  767. }
  768. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  769. "0491 Adapter Link is disabled.\n");
  770. lpfc_down_link(phba, pmb);
  771. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  772. rc = lpfc_sli_issue_mbox(phba, pmb, flag);
  773. if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
  774. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  775. "2522 Adapter failed to issue DOWN_LINK"
  776. " mbox command rc 0x%x\n", rc);
  777. mempool_free(pmb, phba->mbox_mem_pool);
  778. return -EIO;
  779. }
  780. if (flag == MBX_POLL)
  781. mempool_free(pmb, phba->mbox_mem_pool);
  782. return 0;
  783. }
  784. /**
  785. * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
  786. * @phba: pointer to lpfc HBA data structure.
  787. *
  788. * This routine will do LPFC uninitialization before the HBA is reset when
  789. * bringing down the SLI Layer.
  790. *
  791. * Return codes
  792. * 0 - success.
  793. * Any other value - error.
  794. **/
  795. int
  796. lpfc_hba_down_prep(struct lpfc_hba *phba)
  797. {
  798. struct lpfc_vport **vports;
  799. int i;
  800. if (phba->sli_rev <= LPFC_SLI_REV3) {
  801. /* Disable interrupts */
  802. writel(0, phba->HCregaddr);
  803. readl(phba->HCregaddr); /* flush */
  804. }
  805. if (test_bit(FC_UNLOADING, &phba->pport->load_flag))
  806. lpfc_cleanup_discovery_resources(phba->pport);
  807. else {
  808. vports = lpfc_create_vport_work_array(phba);
  809. if (vports != NULL)
  810. for (i = 0; i <= phba->max_vports &&
  811. vports[i] != NULL; i++)
  812. lpfc_cleanup_discovery_resources(vports[i]);
  813. lpfc_destroy_vport_work_array(phba, vports);
  814. }
  815. return 0;
  816. }
  817. /**
  818. * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
  819. * rspiocb which got deferred
  820. *
  821. * @phba: pointer to lpfc HBA data structure.
  822. *
  823. * This routine will cleanup completed slow path events after HBA is reset
  824. * when bringing down the SLI Layer.
  825. *
  826. *
  827. * Return codes
  828. * void.
  829. **/
  830. static void
  831. lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
  832. {
  833. struct lpfc_iocbq *rspiocbq;
  834. struct hbq_dmabuf *dmabuf;
  835. struct lpfc_cq_event *cq_event;
  836. clear_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
  837. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  838. /* Get the response iocb from the head of work queue */
  839. spin_lock_irq(&phba->hbalock);
  840. list_remove_head(&phba->sli4_hba.sp_queue_event,
  841. cq_event, struct lpfc_cq_event, list);
  842. spin_unlock_irq(&phba->hbalock);
  843. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  844. case CQE_CODE_COMPL_WQE:
  845. rspiocbq = container_of(cq_event, struct lpfc_iocbq,
  846. cq_event);
  847. lpfc_sli_release_iocbq(phba, rspiocbq);
  848. break;
  849. case CQE_CODE_RECEIVE:
  850. case CQE_CODE_RECEIVE_V1:
  851. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  852. cq_event);
  853. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  854. }
  855. }
  856. }
  857. /**
  858. * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
  859. * @phba: pointer to lpfc HBA data structure.
  860. *
  861. * This routine will cleanup posted ELS buffers after the HBA is reset
  862. * when bringing down the SLI Layer.
  863. *
  864. *
  865. * Return codes
  866. * void.
  867. **/
  868. static void
  869. lpfc_hba_free_post_buf(struct lpfc_hba *phba)
  870. {
  871. struct lpfc_sli *psli = &phba->sli;
  872. struct lpfc_sli_ring *pring;
  873. struct lpfc_dmabuf *mp, *next_mp;
  874. LIST_HEAD(buflist);
  875. int count;
  876. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
  877. lpfc_sli_hbqbuf_free_all(phba);
  878. else {
  879. /* Cleanup preposted buffers on the ELS ring */
  880. pring = &psli->sli3_ring[LPFC_ELS_RING];
  881. spin_lock_irq(&phba->hbalock);
  882. list_splice_init(&pring->postbufq, &buflist);
  883. spin_unlock_irq(&phba->hbalock);
  884. count = 0;
  885. list_for_each_entry_safe(mp, next_mp, &buflist, list) {
  886. list_del(&mp->list);
  887. count++;
  888. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  889. kfree(mp);
  890. }
  891. spin_lock_irq(&phba->hbalock);
  892. pring->postbufq_cnt -= count;
  893. spin_unlock_irq(&phba->hbalock);
  894. }
  895. }
  896. /**
  897. * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
  898. * @phba: pointer to lpfc HBA data structure.
  899. *
  900. * This routine will cleanup the txcmplq after the HBA is reset when bringing
  901. * down the SLI Layer.
  902. *
  903. * Return codes
  904. * void
  905. **/
  906. static void
  907. lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
  908. {
  909. struct lpfc_sli *psli = &phba->sli;
  910. struct lpfc_queue *qp = NULL;
  911. struct lpfc_sli_ring *pring;
  912. LIST_HEAD(completions);
  913. int i;
  914. struct lpfc_iocbq *piocb, *next_iocb;
  915. if (phba->sli_rev != LPFC_SLI_REV4) {
  916. for (i = 0; i < psli->num_rings; i++) {
  917. pring = &psli->sli3_ring[i];
  918. spin_lock_irq(&phba->hbalock);
  919. /* At this point in time the HBA is either reset or DOA
  920. * Nothing should be on txcmplq as it will
  921. * NEVER complete.
  922. */
  923. list_splice_init(&pring->txcmplq, &completions);
  924. pring->txcmplq_cnt = 0;
  925. spin_unlock_irq(&phba->hbalock);
  926. lpfc_sli_abort_iocb_ring(phba, pring);
  927. }
  928. /* Cancel all the IOCBs from the completions list */
  929. lpfc_sli_cancel_iocbs(phba, &completions,
  930. IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
  931. return;
  932. }
  933. list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
  934. pring = qp->pring;
  935. if (!pring)
  936. continue;
  937. spin_lock_irq(&pring->ring_lock);
  938. list_for_each_entry_safe(piocb, next_iocb,
  939. &pring->txcmplq, list)
  940. piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
  941. list_splice_init(&pring->txcmplq, &completions);
  942. pring->txcmplq_cnt = 0;
  943. spin_unlock_irq(&pring->ring_lock);
  944. lpfc_sli_abort_iocb_ring(phba, pring);
  945. }
  946. /* Cancel all the IOCBs from the completions list */
  947. lpfc_sli_cancel_iocbs(phba, &completions,
  948. IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
  949. }
  950. /**
  951. * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
  952. * @phba: pointer to lpfc HBA data structure.
  953. *
  954. * This routine will do uninitialization after the HBA is reset when bring
  955. * down the SLI Layer.
  956. *
  957. * Return codes
  958. * 0 - success.
  959. * Any other value - error.
  960. **/
  961. static int
  962. lpfc_hba_down_post_s3(struct lpfc_hba *phba)
  963. {
  964. lpfc_hba_free_post_buf(phba);
  965. lpfc_hba_clean_txcmplq(phba);
  966. return 0;
  967. }
  968. /**
  969. * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
  970. * @phba: pointer to lpfc HBA data structure.
  971. *
  972. * This routine will do uninitialization after the HBA is reset when bring
  973. * down the SLI Layer.
  974. *
  975. * Return codes
  976. * 0 - success.
  977. * Any other value - error.
  978. **/
  979. static int
  980. lpfc_hba_down_post_s4(struct lpfc_hba *phba)
  981. {
  982. struct lpfc_io_buf *psb, *psb_next;
  983. struct lpfc_async_xchg_ctx *ctxp, *ctxp_next;
  984. struct lpfc_sli4_hdw_queue *qp;
  985. LIST_HEAD(aborts);
  986. LIST_HEAD(nvme_aborts);
  987. LIST_HEAD(nvmet_aborts);
  988. struct lpfc_sglq *sglq_entry = NULL;
  989. int cnt, idx;
  990. lpfc_sli_hbqbuf_free_all(phba);
  991. lpfc_hba_clean_txcmplq(phba);
  992. /* At this point in time the HBA is either reset or DOA. Either
  993. * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
  994. * on the lpfc_els_sgl_list so that it can either be freed if the
  995. * driver is unloading or reposted if the driver is restarting
  996. * the port.
  997. */
  998. /* sgl_list_lock required because worker thread uses this
  999. * list.
  1000. */
  1001. spin_lock_irq(&phba->sli4_hba.sgl_list_lock);
  1002. list_for_each_entry(sglq_entry,
  1003. &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
  1004. sglq_entry->state = SGL_FREED;
  1005. list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
  1006. &phba->sli4_hba.lpfc_els_sgl_list);
  1007. spin_unlock_irq(&phba->sli4_hba.sgl_list_lock);
  1008. /* abts_xxxx_buf_list_lock required because worker thread uses this
  1009. * list.
  1010. */
  1011. spin_lock_irq(&phba->hbalock);
  1012. cnt = 0;
  1013. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  1014. qp = &phba->sli4_hba.hdwq[idx];
  1015. spin_lock(&qp->abts_io_buf_list_lock);
  1016. list_splice_init(&qp->lpfc_abts_io_buf_list,
  1017. &aborts);
  1018. list_for_each_entry_safe(psb, psb_next, &aborts, list) {
  1019. psb->pCmd = NULL;
  1020. psb->status = IOSTAT_SUCCESS;
  1021. cnt++;
  1022. }
  1023. spin_lock(&qp->io_buf_list_put_lock);
  1024. list_splice_init(&aborts, &qp->lpfc_io_buf_list_put);
  1025. qp->put_io_bufs += qp->abts_scsi_io_bufs;
  1026. qp->put_io_bufs += qp->abts_nvme_io_bufs;
  1027. qp->abts_scsi_io_bufs = 0;
  1028. qp->abts_nvme_io_bufs = 0;
  1029. spin_unlock(&qp->io_buf_list_put_lock);
  1030. spin_unlock(&qp->abts_io_buf_list_lock);
  1031. }
  1032. spin_unlock_irq(&phba->hbalock);
  1033. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  1034. spin_lock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
  1035. list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
  1036. &nvmet_aborts);
  1037. spin_unlock_irq(&phba->sli4_hba.abts_nvmet_buf_list_lock);
  1038. list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
  1039. ctxp->flag &= ~(LPFC_NVME_XBUSY | LPFC_NVME_ABORT_OP);
  1040. lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
  1041. }
  1042. }
  1043. lpfc_sli4_free_sp_events(phba);
  1044. return cnt;
  1045. }
  1046. /**
  1047. * lpfc_hba_down_post - Wrapper func for hba down post routine
  1048. * @phba: pointer to lpfc HBA data structure.
  1049. *
  1050. * This routine wraps the actual SLI3 or SLI4 routine for performing
  1051. * uninitialization after the HBA is reset when bring down the SLI Layer.
  1052. *
  1053. * Return codes
  1054. * 0 - success.
  1055. * Any other value - error.
  1056. **/
  1057. int
  1058. lpfc_hba_down_post(struct lpfc_hba *phba)
  1059. {
  1060. return (*phba->lpfc_hba_down_post)(phba);
  1061. }
  1062. /**
  1063. * lpfc_hb_timeout - The HBA-timer timeout handler
  1064. * @t: timer context used to obtain the pointer to lpfc hba data structure.
  1065. *
  1066. * This is the HBA-timer timeout handler registered to the lpfc driver. When
  1067. * this timer fires, a HBA timeout event shall be posted to the lpfc driver
  1068. * work-port-events bitmap and the worker thread is notified. This timeout
  1069. * event will be used by the worker thread to invoke the actual timeout
  1070. * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
  1071. * be performed in the timeout handler and the HBA timeout event bit shall
  1072. * be cleared by the worker thread after it has taken the event bitmap out.
  1073. **/
  1074. static void
  1075. lpfc_hb_timeout(struct timer_list *t)
  1076. {
  1077. struct lpfc_hba *phba;
  1078. uint32_t tmo_posted;
  1079. unsigned long iflag;
  1080. phba = from_timer(phba, t, hb_tmofunc);
  1081. /* Check for heart beat timeout conditions */
  1082. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  1083. tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
  1084. if (!tmo_posted)
  1085. phba->pport->work_port_events |= WORKER_HB_TMO;
  1086. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  1087. /* Tell the worker thread there is work to do */
  1088. if (!tmo_posted)
  1089. lpfc_worker_wake_up(phba);
  1090. return;
  1091. }
  1092. /**
  1093. * lpfc_rrq_timeout - The RRQ-timer timeout handler
  1094. * @t: timer context used to obtain the pointer to lpfc hba data structure.
  1095. *
  1096. * This is the RRQ-timer timeout handler registered to the lpfc driver. When
  1097. * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
  1098. * work-port-events bitmap and the worker thread is notified. This timeout
  1099. * event will be used by the worker thread to invoke the actual timeout
  1100. * handler routine, lpfc_rrq_handler. Any periodical operations will
  1101. * be performed in the timeout handler and the RRQ timeout event bit shall
  1102. * be cleared by the worker thread after it has taken the event bitmap out.
  1103. **/
  1104. static void
  1105. lpfc_rrq_timeout(struct timer_list *t)
  1106. {
  1107. struct lpfc_hba *phba;
  1108. phba = from_timer(phba, t, rrq_tmr);
  1109. if (test_bit(FC_UNLOADING, &phba->pport->load_flag)) {
  1110. clear_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);
  1111. return;
  1112. }
  1113. set_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);
  1114. lpfc_worker_wake_up(phba);
  1115. }
  1116. /**
  1117. * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
  1118. * @phba: pointer to lpfc hba data structure.
  1119. * @pmboxq: pointer to the driver internal queue element for mailbox command.
  1120. *
  1121. * This is the callback function to the lpfc heart-beat mailbox command.
  1122. * If configured, the lpfc driver issues the heart-beat mailbox command to
  1123. * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
  1124. * heart-beat mailbox command is issued, the driver shall set up heart-beat
  1125. * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
  1126. * heart-beat outstanding state. Once the mailbox command comes back and
  1127. * no error conditions detected, the heart-beat mailbox command timer is
  1128. * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
  1129. * state is cleared for the next heart-beat. If the timer expired with the
  1130. * heart-beat outstanding state set, the driver will put the HBA offline.
  1131. **/
  1132. static void
  1133. lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
  1134. {
  1135. clear_bit(HBA_HBEAT_INP, &phba->hba_flag);
  1136. clear_bit(HBA_HBEAT_TMO, &phba->hba_flag);
  1137. /* Check and reset heart-beat timer if necessary */
  1138. mempool_free(pmboxq, phba->mbox_mem_pool);
  1139. if (!test_bit(FC_OFFLINE_MODE, &phba->pport->fc_flag) &&
  1140. !(phba->link_state == LPFC_HBA_ERROR) &&
  1141. !test_bit(FC_UNLOADING, &phba->pport->load_flag))
  1142. mod_timer(&phba->hb_tmofunc,
  1143. jiffies +
  1144. msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
  1145. return;
  1146. }
  1147. /*
  1148. * lpfc_idle_stat_delay_work - idle_stat tracking
  1149. *
  1150. * This routine tracks per-eq idle_stat and determines polling decisions.
  1151. *
  1152. * Return codes:
  1153. * None
  1154. **/
  1155. static void
  1156. lpfc_idle_stat_delay_work(struct work_struct *work)
  1157. {
  1158. struct lpfc_hba *phba = container_of(to_delayed_work(work),
  1159. struct lpfc_hba,
  1160. idle_stat_delay_work);
  1161. struct lpfc_queue *eq;
  1162. struct lpfc_sli4_hdw_queue *hdwq;
  1163. struct lpfc_idle_stat *idle_stat;
  1164. u32 i, idle_percent;
  1165. u64 wall, wall_idle, diff_wall, diff_idle, busy_time;
  1166. if (test_bit(FC_UNLOADING, &phba->pport->load_flag))
  1167. return;
  1168. if (phba->link_state == LPFC_HBA_ERROR ||
  1169. test_bit(FC_OFFLINE_MODE, &phba->pport->fc_flag) ||
  1170. phba->cmf_active_mode != LPFC_CFG_OFF)
  1171. goto requeue;
  1172. for_each_present_cpu(i) {
  1173. hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
  1174. eq = hdwq->hba_eq;
  1175. /* Skip if we've already handled this eq's primary CPU */
  1176. if (eq->chann != i)
  1177. continue;
  1178. idle_stat = &phba->sli4_hba.idle_stat[i];
  1179. /* get_cpu_idle_time returns values as running counters. Thus,
  1180. * to know the amount for this period, the prior counter values
  1181. * need to be subtracted from the current counter values.
  1182. * From there, the idle time stat can be calculated as a
  1183. * percentage of 100 - the sum of the other consumption times.
  1184. */
  1185. wall_idle = get_cpu_idle_time(i, &wall, 1);
  1186. diff_idle = wall_idle - idle_stat->prev_idle;
  1187. diff_wall = wall - idle_stat->prev_wall;
  1188. if (diff_wall <= diff_idle)
  1189. busy_time = 0;
  1190. else
  1191. busy_time = diff_wall - diff_idle;
  1192. idle_percent = div64_u64(100 * busy_time, diff_wall);
  1193. idle_percent = 100 - idle_percent;
  1194. if (idle_percent < 15)
  1195. eq->poll_mode = LPFC_QUEUE_WORK;
  1196. else
  1197. eq->poll_mode = LPFC_THREADED_IRQ;
  1198. idle_stat->prev_idle = wall_idle;
  1199. idle_stat->prev_wall = wall;
  1200. }
  1201. requeue:
  1202. schedule_delayed_work(&phba->idle_stat_delay_work,
  1203. msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
  1204. }
  1205. static void
  1206. lpfc_hb_eq_delay_work(struct work_struct *work)
  1207. {
  1208. struct lpfc_hba *phba = container_of(to_delayed_work(work),
  1209. struct lpfc_hba, eq_delay_work);
  1210. struct lpfc_eq_intr_info *eqi, *eqi_new;
  1211. struct lpfc_queue *eq, *eq_next;
  1212. unsigned char *ena_delay = NULL;
  1213. uint32_t usdelay;
  1214. int i;
  1215. if (!phba->cfg_auto_imax ||
  1216. test_bit(FC_UNLOADING, &phba->pport->load_flag))
  1217. return;
  1218. if (phba->link_state == LPFC_HBA_ERROR ||
  1219. test_bit(FC_OFFLINE_MODE, &phba->pport->fc_flag))
  1220. goto requeue;
  1221. ena_delay = kcalloc(phba->sli4_hba.num_possible_cpu, sizeof(*ena_delay),
  1222. GFP_KERNEL);
  1223. if (!ena_delay)
  1224. goto requeue;
  1225. for (i = 0; i < phba->cfg_irq_chann; i++) {
  1226. /* Get the EQ corresponding to the IRQ vector */
  1227. eq = phba->sli4_hba.hba_eq_hdl[i].eq;
  1228. if (!eq)
  1229. continue;
  1230. if (eq->q_mode || eq->q_flag & HBA_EQ_DELAY_CHK) {
  1231. eq->q_flag &= ~HBA_EQ_DELAY_CHK;
  1232. ena_delay[eq->last_cpu] = 1;
  1233. }
  1234. }
  1235. for_each_present_cpu(i) {
  1236. eqi = per_cpu_ptr(phba->sli4_hba.eq_info, i);
  1237. if (ena_delay[i]) {
  1238. usdelay = (eqi->icnt >> 10) * LPFC_EQ_DELAY_STEP;
  1239. if (usdelay > LPFC_MAX_AUTO_EQ_DELAY)
  1240. usdelay = LPFC_MAX_AUTO_EQ_DELAY;
  1241. } else {
  1242. usdelay = 0;
  1243. }
  1244. eqi->icnt = 0;
  1245. list_for_each_entry_safe(eq, eq_next, &eqi->list, cpu_list) {
  1246. if (unlikely(eq->last_cpu != i)) {
  1247. eqi_new = per_cpu_ptr(phba->sli4_hba.eq_info,
  1248. eq->last_cpu);
  1249. list_move_tail(&eq->cpu_list, &eqi_new->list);
  1250. continue;
  1251. }
  1252. if (usdelay != eq->q_mode)
  1253. lpfc_modify_hba_eq_delay(phba, eq->hdwq, 1,
  1254. usdelay);
  1255. }
  1256. }
  1257. kfree(ena_delay);
  1258. requeue:
  1259. queue_delayed_work(phba->wq, &phba->eq_delay_work,
  1260. msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
  1261. }
  1262. /**
  1263. * lpfc_hb_mxp_handler - Multi-XRI pools handler to adjust XRI distribution
  1264. * @phba: pointer to lpfc hba data structure.
  1265. *
  1266. * For each heartbeat, this routine does some heuristic methods to adjust
  1267. * XRI distribution. The goal is to fully utilize free XRIs.
  1268. **/
  1269. static void lpfc_hb_mxp_handler(struct lpfc_hba *phba)
  1270. {
  1271. u32 i;
  1272. u32 hwq_count;
  1273. hwq_count = phba->cfg_hdw_queue;
  1274. for (i = 0; i < hwq_count; i++) {
  1275. /* Adjust XRIs in private pool */
  1276. lpfc_adjust_pvt_pool_count(phba, i);
  1277. /* Adjust high watermark */
  1278. lpfc_adjust_high_watermark(phba, i);
  1279. #ifdef LPFC_MXP_STAT
  1280. /* Snapshot pbl, pvt and busy count */
  1281. lpfc_snapshot_mxp(phba, i);
  1282. #endif
  1283. }
  1284. }
  1285. /**
  1286. * lpfc_issue_hb_mbox - Issues heart-beat mailbox command
  1287. * @phba: pointer to lpfc hba data structure.
  1288. *
  1289. * If a HB mbox is not already in progrees, this routine will allocate
  1290. * a LPFC_MBOXQ_t, populate it with a MBX_HEARTBEAT (0x31) command,
  1291. * and issue it. The HBA_HBEAT_INP flag means the command is in progress.
  1292. **/
  1293. int
  1294. lpfc_issue_hb_mbox(struct lpfc_hba *phba)
  1295. {
  1296. LPFC_MBOXQ_t *pmboxq;
  1297. int retval;
  1298. /* Is a Heartbeat mbox already in progress */
  1299. if (test_bit(HBA_HBEAT_INP, &phba->hba_flag))
  1300. return 0;
  1301. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1302. if (!pmboxq)
  1303. return -ENOMEM;
  1304. lpfc_heart_beat(phba, pmboxq);
  1305. pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
  1306. pmboxq->vport = phba->pport;
  1307. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  1308. if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
  1309. mempool_free(pmboxq, phba->mbox_mem_pool);
  1310. return -ENXIO;
  1311. }
  1312. set_bit(HBA_HBEAT_INP, &phba->hba_flag);
  1313. return 0;
  1314. }
  1315. /**
  1316. * lpfc_issue_hb_tmo - Signals heartbeat timer to issue mbox command
  1317. * @phba: pointer to lpfc hba data structure.
  1318. *
  1319. * The heartbeat timer (every 5 sec) will fire. If the HBA_HBEAT_TMO
  1320. * flag is set, it will force a MBX_HEARTBEAT mbox command, regardless
  1321. * of the value of lpfc_enable_hba_heartbeat.
  1322. * If lpfc_enable_hba_heartbeat is set, the timeout routine will always
  1323. * try to issue a MBX_HEARTBEAT mbox command.
  1324. **/
  1325. void
  1326. lpfc_issue_hb_tmo(struct lpfc_hba *phba)
  1327. {
  1328. if (phba->cfg_enable_hba_heartbeat)
  1329. return;
  1330. set_bit(HBA_HBEAT_TMO, &phba->hba_flag);
  1331. }
  1332. /**
  1333. * lpfc_hb_timeout_handler - The HBA-timer timeout handler
  1334. * @phba: pointer to lpfc hba data structure.
  1335. *
  1336. * This is the actual HBA-timer timeout handler to be invoked by the worker
  1337. * thread whenever the HBA timer fired and HBA-timeout event posted. This
  1338. * handler performs any periodic operations needed for the device. If such
  1339. * periodic event has already been attended to either in the interrupt handler
  1340. * or by processing slow-ring or fast-ring events within the HBA-timer
  1341. * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
  1342. * the timer for the next timeout period. If lpfc heart-beat mailbox command
  1343. * is configured and there is no heart-beat mailbox command outstanding, a
  1344. * heart-beat mailbox is issued and timer set properly. Otherwise, if there
  1345. * has been a heart-beat mailbox command outstanding, the HBA shall be put
  1346. * to offline.
  1347. **/
  1348. void
  1349. lpfc_hb_timeout_handler(struct lpfc_hba *phba)
  1350. {
  1351. struct lpfc_vport **vports;
  1352. struct lpfc_dmabuf *buf_ptr;
  1353. int retval = 0;
  1354. int i, tmo;
  1355. struct lpfc_sli *psli = &phba->sli;
  1356. LIST_HEAD(completions);
  1357. if (phba->cfg_xri_rebalancing) {
  1358. /* Multi-XRI pools handler */
  1359. lpfc_hb_mxp_handler(phba);
  1360. }
  1361. vports = lpfc_create_vport_work_array(phba);
  1362. if (vports != NULL)
  1363. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  1364. lpfc_rcv_seq_check_edtov(vports[i]);
  1365. lpfc_fdmi_change_check(vports[i]);
  1366. }
  1367. lpfc_destroy_vport_work_array(phba, vports);
  1368. if (phba->link_state == LPFC_HBA_ERROR ||
  1369. test_bit(FC_UNLOADING, &phba->pport->load_flag) ||
  1370. test_bit(FC_OFFLINE_MODE, &phba->pport->fc_flag))
  1371. return;
  1372. if (phba->elsbuf_cnt &&
  1373. (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
  1374. spin_lock_irq(&phba->hbalock);
  1375. list_splice_init(&phba->elsbuf, &completions);
  1376. phba->elsbuf_cnt = 0;
  1377. phba->elsbuf_prev_cnt = 0;
  1378. spin_unlock_irq(&phba->hbalock);
  1379. while (!list_empty(&completions)) {
  1380. list_remove_head(&completions, buf_ptr,
  1381. struct lpfc_dmabuf, list);
  1382. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  1383. kfree(buf_ptr);
  1384. }
  1385. }
  1386. phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
  1387. /* If there is no heart beat outstanding, issue a heartbeat command */
  1388. if (phba->cfg_enable_hba_heartbeat) {
  1389. /* If IOs are completing, no need to issue a MBX_HEARTBEAT */
  1390. spin_lock_irq(&phba->pport->work_port_lock);
  1391. if (time_after(phba->last_completion_time +
  1392. msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
  1393. jiffies)) {
  1394. spin_unlock_irq(&phba->pport->work_port_lock);
  1395. if (test_bit(HBA_HBEAT_INP, &phba->hba_flag))
  1396. tmo = (1000 * LPFC_HB_MBOX_TIMEOUT);
  1397. else
  1398. tmo = (1000 * LPFC_HB_MBOX_INTERVAL);
  1399. goto out;
  1400. }
  1401. spin_unlock_irq(&phba->pport->work_port_lock);
  1402. /* Check if a MBX_HEARTBEAT is already in progress */
  1403. if (test_bit(HBA_HBEAT_INP, &phba->hba_flag)) {
  1404. /*
  1405. * If heart beat timeout called with HBA_HBEAT_INP set
  1406. * we need to give the hb mailbox cmd a chance to
  1407. * complete or TMO.
  1408. */
  1409. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  1410. "0459 Adapter heartbeat still outstanding: "
  1411. "last compl time was %d ms.\n",
  1412. jiffies_to_msecs(jiffies
  1413. - phba->last_completion_time));
  1414. tmo = (1000 * LPFC_HB_MBOX_TIMEOUT);
  1415. } else {
  1416. if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
  1417. (list_empty(&psli->mboxq))) {
  1418. retval = lpfc_issue_hb_mbox(phba);
  1419. if (retval) {
  1420. tmo = (1000 * LPFC_HB_MBOX_INTERVAL);
  1421. goto out;
  1422. }
  1423. phba->skipped_hb = 0;
  1424. } else if (time_before_eq(phba->last_completion_time,
  1425. phba->skipped_hb)) {
  1426. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  1427. "2857 Last completion time not "
  1428. " updated in %d ms\n",
  1429. jiffies_to_msecs(jiffies
  1430. - phba->last_completion_time));
  1431. } else
  1432. phba->skipped_hb = jiffies;
  1433. tmo = (1000 * LPFC_HB_MBOX_TIMEOUT);
  1434. goto out;
  1435. }
  1436. } else {
  1437. /* Check to see if we want to force a MBX_HEARTBEAT */
  1438. if (test_bit(HBA_HBEAT_TMO, &phba->hba_flag)) {
  1439. retval = lpfc_issue_hb_mbox(phba);
  1440. if (retval)
  1441. tmo = (1000 * LPFC_HB_MBOX_INTERVAL);
  1442. else
  1443. tmo = (1000 * LPFC_HB_MBOX_TIMEOUT);
  1444. goto out;
  1445. }
  1446. tmo = (1000 * LPFC_HB_MBOX_INTERVAL);
  1447. }
  1448. out:
  1449. mod_timer(&phba->hb_tmofunc, jiffies + msecs_to_jiffies(tmo));
  1450. }
  1451. /**
  1452. * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
  1453. * @phba: pointer to lpfc hba data structure.
  1454. *
  1455. * This routine is called to bring the HBA offline when HBA hardware error
  1456. * other than Port Error 6 has been detected.
  1457. **/
  1458. static void
  1459. lpfc_offline_eratt(struct lpfc_hba *phba)
  1460. {
  1461. struct lpfc_sli *psli = &phba->sli;
  1462. spin_lock_irq(&phba->hbalock);
  1463. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  1464. spin_unlock_irq(&phba->hbalock);
  1465. lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
  1466. lpfc_offline(phba);
  1467. lpfc_reset_barrier(phba);
  1468. spin_lock_irq(&phba->hbalock);
  1469. lpfc_sli_brdreset(phba);
  1470. spin_unlock_irq(&phba->hbalock);
  1471. lpfc_hba_down_post(phba);
  1472. lpfc_sli_brdready(phba, HS_MBRDY);
  1473. lpfc_unblock_mgmt_io(phba);
  1474. phba->link_state = LPFC_HBA_ERROR;
  1475. return;
  1476. }
  1477. /**
  1478. * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
  1479. * @phba: pointer to lpfc hba data structure.
  1480. *
  1481. * This routine is called to bring a SLI4 HBA offline when HBA hardware error
  1482. * other than Port Error 6 has been detected.
  1483. **/
  1484. void
  1485. lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
  1486. {
  1487. spin_lock_irq(&phba->hbalock);
  1488. if (phba->link_state == LPFC_HBA_ERROR &&
  1489. test_bit(HBA_PCI_ERR, &phba->bit_flags)) {
  1490. spin_unlock_irq(&phba->hbalock);
  1491. return;
  1492. }
  1493. phba->link_state = LPFC_HBA_ERROR;
  1494. spin_unlock_irq(&phba->hbalock);
  1495. lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
  1496. lpfc_sli_flush_io_rings(phba);
  1497. lpfc_offline(phba);
  1498. lpfc_hba_down_post(phba);
  1499. lpfc_unblock_mgmt_io(phba);
  1500. }
  1501. /**
  1502. * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
  1503. * @phba: pointer to lpfc hba data structure.
  1504. *
  1505. * This routine is invoked to handle the deferred HBA hardware error
  1506. * conditions. This type of error is indicated by HBA by setting ER1
  1507. * and another ER bit in the host status register. The driver will
  1508. * wait until the ER1 bit clears before handling the error condition.
  1509. **/
  1510. static void
  1511. lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
  1512. {
  1513. uint32_t old_host_status = phba->work_hs;
  1514. struct lpfc_sli *psli = &phba->sli;
  1515. /* If the pci channel is offline, ignore possible errors,
  1516. * since we cannot communicate with the pci card anyway.
  1517. */
  1518. if (pci_channel_offline(phba->pcidev)) {
  1519. clear_bit(DEFER_ERATT, &phba->hba_flag);
  1520. return;
  1521. }
  1522. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1523. "0479 Deferred Adapter Hardware Error "
  1524. "Data: x%x x%x x%x\n",
  1525. phba->work_hs, phba->work_status[0],
  1526. phba->work_status[1]);
  1527. spin_lock_irq(&phba->hbalock);
  1528. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  1529. spin_unlock_irq(&phba->hbalock);
  1530. /*
  1531. * Firmware stops when it triggred erratt. That could cause the I/Os
  1532. * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
  1533. * SCSI layer retry it after re-establishing link.
  1534. */
  1535. lpfc_sli_abort_fcp_rings(phba);
  1536. /*
  1537. * There was a firmware error. Take the hba offline and then
  1538. * attempt to restart it.
  1539. */
  1540. lpfc_offline_prep(phba, LPFC_MBX_WAIT);
  1541. lpfc_offline(phba);
  1542. /* Wait for the ER1 bit to clear.*/
  1543. while (phba->work_hs & HS_FFER1) {
  1544. msleep(100);
  1545. if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
  1546. phba->work_hs = UNPLUG_ERR ;
  1547. break;
  1548. }
  1549. /* If driver is unloading let the worker thread continue */
  1550. if (test_bit(FC_UNLOADING, &phba->pport->load_flag)) {
  1551. phba->work_hs = 0;
  1552. break;
  1553. }
  1554. }
  1555. /*
  1556. * This is to ptrotect against a race condition in which
  1557. * first write to the host attention register clear the
  1558. * host status register.
  1559. */
  1560. if (!phba->work_hs && !test_bit(FC_UNLOADING, &phba->pport->load_flag))
  1561. phba->work_hs = old_host_status & ~HS_FFER1;
  1562. clear_bit(DEFER_ERATT, &phba->hba_flag);
  1563. phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
  1564. phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
  1565. }
  1566. static void
  1567. lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
  1568. {
  1569. struct lpfc_board_event_header board_event;
  1570. struct Scsi_Host *shost;
  1571. board_event.event_type = FC_REG_BOARD_EVENT;
  1572. board_event.subcategory = LPFC_EVENT_PORTINTERR;
  1573. shost = lpfc_shost_from_vport(phba->pport);
  1574. fc_host_post_vendor_event(shost, fc_get_event_number(),
  1575. sizeof(board_event),
  1576. (char *) &board_event,
  1577. LPFC_NL_VENDOR_ID);
  1578. }
  1579. /**
  1580. * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
  1581. * @phba: pointer to lpfc hba data structure.
  1582. *
  1583. * This routine is invoked to handle the following HBA hardware error
  1584. * conditions:
  1585. * 1 - HBA error attention interrupt
  1586. * 2 - DMA ring index out of range
  1587. * 3 - Mailbox command came back as unknown
  1588. **/
  1589. static void
  1590. lpfc_handle_eratt_s3(struct lpfc_hba *phba)
  1591. {
  1592. struct lpfc_vport *vport = phba->pport;
  1593. struct lpfc_sli *psli = &phba->sli;
  1594. uint32_t event_data;
  1595. unsigned long temperature;
  1596. struct temp_event temp_event_data;
  1597. struct Scsi_Host *shost;
  1598. /* If the pci channel is offline, ignore possible errors,
  1599. * since we cannot communicate with the pci card anyway.
  1600. */
  1601. if (pci_channel_offline(phba->pcidev)) {
  1602. clear_bit(DEFER_ERATT, &phba->hba_flag);
  1603. return;
  1604. }
  1605. /* If resets are disabled then leave the HBA alone and return */
  1606. if (!phba->cfg_enable_hba_reset)
  1607. return;
  1608. /* Send an internal error event to mgmt application */
  1609. lpfc_board_errevt_to_mgmt(phba);
  1610. if (test_bit(DEFER_ERATT, &phba->hba_flag))
  1611. lpfc_handle_deferred_eratt(phba);
  1612. if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
  1613. if (phba->work_hs & HS_FFER6)
  1614. /* Re-establishing Link */
  1615. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  1616. "1301 Re-establishing Link "
  1617. "Data: x%x x%x x%x\n",
  1618. phba->work_hs, phba->work_status[0],
  1619. phba->work_status[1]);
  1620. if (phba->work_hs & HS_FFER8)
  1621. /* Device Zeroization */
  1622. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  1623. "2861 Host Authentication device "
  1624. "zeroization Data:x%x x%x x%x\n",
  1625. phba->work_hs, phba->work_status[0],
  1626. phba->work_status[1]);
  1627. spin_lock_irq(&phba->hbalock);
  1628. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  1629. spin_unlock_irq(&phba->hbalock);
  1630. /*
  1631. * Firmware stops when it triggled erratt with HS_FFER6.
  1632. * That could cause the I/Os dropped by the firmware.
  1633. * Error iocb (I/O) on txcmplq and let the SCSI layer
  1634. * retry it after re-establishing link.
  1635. */
  1636. lpfc_sli_abort_fcp_rings(phba);
  1637. /*
  1638. * There was a firmware error. Take the hba offline and then
  1639. * attempt to restart it.
  1640. */
  1641. lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
  1642. lpfc_offline(phba);
  1643. lpfc_sli_brdrestart(phba);
  1644. if (lpfc_online(phba) == 0) { /* Initialize the HBA */
  1645. lpfc_unblock_mgmt_io(phba);
  1646. return;
  1647. }
  1648. lpfc_unblock_mgmt_io(phba);
  1649. } else if (phba->work_hs & HS_CRIT_TEMP) {
  1650. temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
  1651. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  1652. temp_event_data.event_code = LPFC_CRIT_TEMP;
  1653. temp_event_data.data = (uint32_t)temperature;
  1654. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1655. "0406 Adapter maximum temperature exceeded "
  1656. "(%ld), taking this port offline "
  1657. "Data: x%x x%x x%x\n",
  1658. temperature, phba->work_hs,
  1659. phba->work_status[0], phba->work_status[1]);
  1660. shost = lpfc_shost_from_vport(phba->pport);
  1661. fc_host_post_vendor_event(shost, fc_get_event_number(),
  1662. sizeof(temp_event_data),
  1663. (char *) &temp_event_data,
  1664. SCSI_NL_VID_TYPE_PCI
  1665. | PCI_VENDOR_ID_EMULEX);
  1666. spin_lock_irq(&phba->hbalock);
  1667. phba->over_temp_state = HBA_OVER_TEMP;
  1668. spin_unlock_irq(&phba->hbalock);
  1669. lpfc_offline_eratt(phba);
  1670. } else {
  1671. /* The if clause above forces this code path when the status
  1672. * failure is a value other than FFER6. Do not call the offline
  1673. * twice. This is the adapter hardware error path.
  1674. */
  1675. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1676. "0457 Adapter Hardware Error "
  1677. "Data: x%x x%x x%x\n",
  1678. phba->work_hs,
  1679. phba->work_status[0], phba->work_status[1]);
  1680. event_data = FC_REG_DUMP_EVENT;
  1681. shost = lpfc_shost_from_vport(vport);
  1682. fc_host_post_vendor_event(shost, fc_get_event_number(),
  1683. sizeof(event_data), (char *) &event_data,
  1684. SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
  1685. lpfc_offline_eratt(phba);
  1686. }
  1687. return;
  1688. }
  1689. /**
  1690. * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
  1691. * @phba: pointer to lpfc hba data structure.
  1692. * @mbx_action: flag for mailbox shutdown action.
  1693. * @en_rn_msg: send reset/port recovery message.
  1694. * This routine is invoked to perform an SLI4 port PCI function reset in
  1695. * response to port status register polling attention. It waits for port
  1696. * status register (ERR, RDY, RN) bits before proceeding with function reset.
  1697. * During this process, interrupt vectors are freed and later requested
  1698. * for handling possible port resource change.
  1699. **/
  1700. static int
  1701. lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
  1702. bool en_rn_msg)
  1703. {
  1704. int rc;
  1705. uint32_t intr_mode;
  1706. LPFC_MBOXQ_t *mboxq;
  1707. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
  1708. LPFC_SLI_INTF_IF_TYPE_2) {
  1709. /*
  1710. * On error status condition, driver need to wait for port
  1711. * ready before performing reset.
  1712. */
  1713. rc = lpfc_sli4_pdev_status_reg_wait(phba);
  1714. if (rc)
  1715. return rc;
  1716. }
  1717. /* need reset: attempt for port recovery */
  1718. if (en_rn_msg)
  1719. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1720. "2887 Reset Needed: Attempting Port "
  1721. "Recovery...\n");
  1722. /* If we are no wait, the HBA has been reset and is not
  1723. * functional, thus we should clear
  1724. * (LPFC_SLI_ACTIVE | LPFC_SLI_MBOX_ACTIVE) flags.
  1725. */
  1726. if (mbx_action == LPFC_MBX_NO_WAIT) {
  1727. spin_lock_irq(&phba->hbalock);
  1728. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  1729. if (phba->sli.mbox_active) {
  1730. mboxq = phba->sli.mbox_active;
  1731. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  1732. __lpfc_mbox_cmpl_put(phba, mboxq);
  1733. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  1734. phba->sli.mbox_active = NULL;
  1735. }
  1736. spin_unlock_irq(&phba->hbalock);
  1737. }
  1738. lpfc_offline_prep(phba, mbx_action);
  1739. lpfc_sli_flush_io_rings(phba);
  1740. lpfc_offline(phba);
  1741. /* release interrupt for possible resource change */
  1742. lpfc_sli4_disable_intr(phba);
  1743. rc = lpfc_sli_brdrestart(phba);
  1744. if (rc) {
  1745. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1746. "6309 Failed to restart board\n");
  1747. return rc;
  1748. }
  1749. /* request and enable interrupt */
  1750. intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
  1751. if (intr_mode == LPFC_INTR_ERROR) {
  1752. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1753. "3175 Failed to enable interrupt\n");
  1754. return -EIO;
  1755. }
  1756. phba->intr_mode = intr_mode;
  1757. rc = lpfc_online(phba);
  1758. if (rc == 0)
  1759. lpfc_unblock_mgmt_io(phba);
  1760. return rc;
  1761. }
  1762. /**
  1763. * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
  1764. * @phba: pointer to lpfc hba data structure.
  1765. *
  1766. * This routine is invoked to handle the SLI4 HBA hardware error attention
  1767. * conditions.
  1768. **/
  1769. static void
  1770. lpfc_handle_eratt_s4(struct lpfc_hba *phba)
  1771. {
  1772. struct lpfc_vport *vport = phba->pport;
  1773. uint32_t event_data;
  1774. struct Scsi_Host *shost;
  1775. uint32_t if_type;
  1776. struct lpfc_register portstat_reg = {0};
  1777. uint32_t reg_err1, reg_err2;
  1778. uint32_t uerrlo_reg, uemasklo_reg;
  1779. uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
  1780. bool en_rn_msg = true;
  1781. struct temp_event temp_event_data;
  1782. struct lpfc_register portsmphr_reg;
  1783. int rc, i;
  1784. /* If the pci channel is offline, ignore possible errors, since
  1785. * we cannot communicate with the pci card anyway.
  1786. */
  1787. if (pci_channel_offline(phba->pcidev)) {
  1788. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1789. "3166 pci channel is offline\n");
  1790. lpfc_sli_flush_io_rings(phba);
  1791. return;
  1792. }
  1793. memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
  1794. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  1795. switch (if_type) {
  1796. case LPFC_SLI_INTF_IF_TYPE_0:
  1797. pci_rd_rc1 = lpfc_readl(
  1798. phba->sli4_hba.u.if_type0.UERRLOregaddr,
  1799. &uerrlo_reg);
  1800. pci_rd_rc2 = lpfc_readl(
  1801. phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
  1802. &uemasklo_reg);
  1803. /* consider PCI bus read error as pci_channel_offline */
  1804. if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
  1805. return;
  1806. if (!test_bit(HBA_RECOVERABLE_UE, &phba->hba_flag)) {
  1807. lpfc_sli4_offline_eratt(phba);
  1808. return;
  1809. }
  1810. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1811. "7623 Checking UE recoverable");
  1812. for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
  1813. if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  1814. &portsmphr_reg.word0))
  1815. continue;
  1816. smphr_port_status = bf_get(lpfc_port_smphr_port_status,
  1817. &portsmphr_reg);
  1818. if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
  1819. LPFC_PORT_SEM_UE_RECOVERABLE)
  1820. break;
  1821. /*Sleep for 1Sec, before checking SEMAPHORE */
  1822. msleep(1000);
  1823. }
  1824. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1825. "4827 smphr_port_status x%x : Waited %dSec",
  1826. smphr_port_status, i);
  1827. /* Recoverable UE, reset the HBA device */
  1828. if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
  1829. LPFC_PORT_SEM_UE_RECOVERABLE) {
  1830. for (i = 0; i < 20; i++) {
  1831. msleep(1000);
  1832. if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  1833. &portsmphr_reg.word0) &&
  1834. (LPFC_POST_STAGE_PORT_READY ==
  1835. bf_get(lpfc_port_smphr_port_status,
  1836. &portsmphr_reg))) {
  1837. rc = lpfc_sli4_port_sta_fn_reset(phba,
  1838. LPFC_MBX_NO_WAIT, en_rn_msg);
  1839. if (rc == 0)
  1840. return;
  1841. lpfc_printf_log(phba, KERN_ERR,
  1842. LOG_TRACE_EVENT,
  1843. "4215 Failed to recover UE");
  1844. break;
  1845. }
  1846. }
  1847. }
  1848. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1849. "7624 Firmware not ready: Failing UE recovery,"
  1850. " waited %dSec", i);
  1851. phba->link_state = LPFC_HBA_ERROR;
  1852. break;
  1853. case LPFC_SLI_INTF_IF_TYPE_2:
  1854. case LPFC_SLI_INTF_IF_TYPE_6:
  1855. pci_rd_rc1 = lpfc_readl(
  1856. phba->sli4_hba.u.if_type2.STATUSregaddr,
  1857. &portstat_reg.word0);
  1858. /* consider PCI bus read error as pci_channel_offline */
  1859. if (pci_rd_rc1 == -EIO) {
  1860. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1861. "3151 PCI bus read access failure: x%x\n",
  1862. readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
  1863. lpfc_sli4_offline_eratt(phba);
  1864. return;
  1865. }
  1866. reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  1867. reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  1868. if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
  1869. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1870. "2889 Port Overtemperature event, "
  1871. "taking port offline Data: x%x x%x\n",
  1872. reg_err1, reg_err2);
  1873. phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
  1874. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  1875. temp_event_data.event_code = LPFC_CRIT_TEMP;
  1876. temp_event_data.data = 0xFFFFFFFF;
  1877. shost = lpfc_shost_from_vport(phba->pport);
  1878. fc_host_post_vendor_event(shost, fc_get_event_number(),
  1879. sizeof(temp_event_data),
  1880. (char *)&temp_event_data,
  1881. SCSI_NL_VID_TYPE_PCI
  1882. | PCI_VENDOR_ID_EMULEX);
  1883. spin_lock_irq(&phba->hbalock);
  1884. phba->over_temp_state = HBA_OVER_TEMP;
  1885. spin_unlock_irq(&phba->hbalock);
  1886. lpfc_sli4_offline_eratt(phba);
  1887. return;
  1888. }
  1889. if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
  1890. reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
  1891. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1892. "3143 Port Down: Firmware Update "
  1893. "Detected\n");
  1894. en_rn_msg = false;
  1895. } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
  1896. reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
  1897. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1898. "3144 Port Down: Debug Dump\n");
  1899. else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
  1900. reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
  1901. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1902. "3145 Port Down: Provisioning\n");
  1903. /* If resets are disabled then leave the HBA alone and return */
  1904. if (!phba->cfg_enable_hba_reset)
  1905. return;
  1906. /* Check port status register for function reset */
  1907. rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
  1908. en_rn_msg);
  1909. if (rc == 0) {
  1910. /* don't report event on forced debug dump */
  1911. if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
  1912. reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
  1913. return;
  1914. else
  1915. break;
  1916. }
  1917. /* fall through for not able to recover */
  1918. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  1919. "3152 Unrecoverable error\n");
  1920. lpfc_sli4_offline_eratt(phba);
  1921. break;
  1922. case LPFC_SLI_INTF_IF_TYPE_1:
  1923. default:
  1924. break;
  1925. }
  1926. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  1927. "3123 Report dump event to upper layer\n");
  1928. /* Send an internal error event to mgmt application */
  1929. lpfc_board_errevt_to_mgmt(phba);
  1930. event_data = FC_REG_DUMP_EVENT;
  1931. shost = lpfc_shost_from_vport(vport);
  1932. fc_host_post_vendor_event(shost, fc_get_event_number(),
  1933. sizeof(event_data), (char *) &event_data,
  1934. SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
  1935. }
  1936. /**
  1937. * lpfc_handle_eratt - Wrapper func for handling hba error attention
  1938. * @phba: pointer to lpfc HBA data structure.
  1939. *
  1940. * This routine wraps the actual SLI3 or SLI4 hba error attention handling
  1941. * routine from the API jump table function pointer from the lpfc_hba struct.
  1942. *
  1943. * Return codes
  1944. * 0 - success.
  1945. * Any other value - error.
  1946. **/
  1947. void
  1948. lpfc_handle_eratt(struct lpfc_hba *phba)
  1949. {
  1950. (*phba->lpfc_handle_eratt)(phba);
  1951. }
  1952. /**
  1953. * lpfc_handle_latt - The HBA link event handler
  1954. * @phba: pointer to lpfc hba data structure.
  1955. *
  1956. * This routine is invoked from the worker thread to handle a HBA host
  1957. * attention link event. SLI3 only.
  1958. **/
  1959. void
  1960. lpfc_handle_latt(struct lpfc_hba *phba)
  1961. {
  1962. struct lpfc_vport *vport = phba->pport;
  1963. struct lpfc_sli *psli = &phba->sli;
  1964. LPFC_MBOXQ_t *pmb;
  1965. volatile uint32_t control;
  1966. int rc = 0;
  1967. pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1968. if (!pmb) {
  1969. rc = 1;
  1970. goto lpfc_handle_latt_err_exit;
  1971. }
  1972. rc = lpfc_mbox_rsrc_prep(phba, pmb);
  1973. if (rc) {
  1974. rc = 2;
  1975. mempool_free(pmb, phba->mbox_mem_pool);
  1976. goto lpfc_handle_latt_err_exit;
  1977. }
  1978. /* Cleanup any outstanding ELS commands */
  1979. lpfc_els_flush_all_cmd(phba);
  1980. psli->slistat.link_event++;
  1981. lpfc_read_topology(phba, pmb, pmb->ctx_buf);
  1982. pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
  1983. pmb->vport = vport;
  1984. /* Block ELS IOCBs until we have processed this mbox command */
  1985. phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
  1986. rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
  1987. if (rc == MBX_NOT_FINISHED) {
  1988. rc = 4;
  1989. goto lpfc_handle_latt_free_mbuf;
  1990. }
  1991. /* Clear Link Attention in HA REG */
  1992. spin_lock_irq(&phba->hbalock);
  1993. writel(HA_LATT, phba->HAregaddr);
  1994. readl(phba->HAregaddr); /* flush */
  1995. spin_unlock_irq(&phba->hbalock);
  1996. return;
  1997. lpfc_handle_latt_free_mbuf:
  1998. phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
  1999. lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
  2000. lpfc_handle_latt_err_exit:
  2001. /* Enable Link attention interrupts */
  2002. spin_lock_irq(&phba->hbalock);
  2003. psli->sli_flag |= LPFC_PROCESS_LA;
  2004. control = readl(phba->HCregaddr);
  2005. control |= HC_LAINT_ENA;
  2006. writel(control, phba->HCregaddr);
  2007. readl(phba->HCregaddr); /* flush */
  2008. /* Clear Link Attention in HA REG */
  2009. writel(HA_LATT, phba->HAregaddr);
  2010. readl(phba->HAregaddr); /* flush */
  2011. spin_unlock_irq(&phba->hbalock);
  2012. lpfc_linkdown(phba);
  2013. phba->link_state = LPFC_HBA_ERROR;
  2014. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  2015. "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
  2016. return;
  2017. }
  2018. static void
  2019. lpfc_fill_vpd(struct lpfc_hba *phba, uint8_t *vpd, int length, int *pindex)
  2020. {
  2021. int i, j;
  2022. while (length > 0) {
  2023. /* Look for Serial Number */
  2024. if ((vpd[*pindex] == 'S') && (vpd[*pindex + 1] == 'N')) {
  2025. *pindex += 2;
  2026. i = vpd[*pindex];
  2027. *pindex += 1;
  2028. j = 0;
  2029. length -= (3+i);
  2030. while (i--) {
  2031. phba->SerialNumber[j++] = vpd[(*pindex)++];
  2032. if (j == 31)
  2033. break;
  2034. }
  2035. phba->SerialNumber[j] = 0;
  2036. continue;
  2037. } else if ((vpd[*pindex] == 'V') && (vpd[*pindex + 1] == '1')) {
  2038. phba->vpd_flag |= VPD_MODEL_DESC;
  2039. *pindex += 2;
  2040. i = vpd[*pindex];
  2041. *pindex += 1;
  2042. j = 0;
  2043. length -= (3+i);
  2044. while (i--) {
  2045. phba->ModelDesc[j++] = vpd[(*pindex)++];
  2046. if (j == 255)
  2047. break;
  2048. }
  2049. phba->ModelDesc[j] = 0;
  2050. continue;
  2051. } else if ((vpd[*pindex] == 'V') && (vpd[*pindex + 1] == '2')) {
  2052. phba->vpd_flag |= VPD_MODEL_NAME;
  2053. *pindex += 2;
  2054. i = vpd[*pindex];
  2055. *pindex += 1;
  2056. j = 0;
  2057. length -= (3+i);
  2058. while (i--) {
  2059. phba->ModelName[j++] = vpd[(*pindex)++];
  2060. if (j == 79)
  2061. break;
  2062. }
  2063. phba->ModelName[j] = 0;
  2064. continue;
  2065. } else if ((vpd[*pindex] == 'V') && (vpd[*pindex + 1] == '3')) {
  2066. phba->vpd_flag |= VPD_PROGRAM_TYPE;
  2067. *pindex += 2;
  2068. i = vpd[*pindex];
  2069. *pindex += 1;
  2070. j = 0;
  2071. length -= (3+i);
  2072. while (i--) {
  2073. phba->ProgramType[j++] = vpd[(*pindex)++];
  2074. if (j == 255)
  2075. break;
  2076. }
  2077. phba->ProgramType[j] = 0;
  2078. continue;
  2079. } else if ((vpd[*pindex] == 'V') && (vpd[*pindex + 1] == '4')) {
  2080. phba->vpd_flag |= VPD_PORT;
  2081. *pindex += 2;
  2082. i = vpd[*pindex];
  2083. *pindex += 1;
  2084. j = 0;
  2085. length -= (3 + i);
  2086. while (i--) {
  2087. if ((phba->sli_rev == LPFC_SLI_REV4) &&
  2088. (phba->sli4_hba.pport_name_sta ==
  2089. LPFC_SLI4_PPNAME_GET)) {
  2090. j++;
  2091. (*pindex)++;
  2092. } else
  2093. phba->Port[j++] = vpd[(*pindex)++];
  2094. if (j == 19)
  2095. break;
  2096. }
  2097. if ((phba->sli_rev != LPFC_SLI_REV4) ||
  2098. (phba->sli4_hba.pport_name_sta ==
  2099. LPFC_SLI4_PPNAME_NON))
  2100. phba->Port[j] = 0;
  2101. continue;
  2102. } else {
  2103. *pindex += 2;
  2104. i = vpd[*pindex];
  2105. *pindex += 1;
  2106. *pindex += i;
  2107. length -= (3 + i);
  2108. }
  2109. }
  2110. }
  2111. /**
  2112. * lpfc_parse_vpd - Parse VPD (Vital Product Data)
  2113. * @phba: pointer to lpfc hba data structure.
  2114. * @vpd: pointer to the vital product data.
  2115. * @len: length of the vital product data in bytes.
  2116. *
  2117. * This routine parses the Vital Product Data (VPD). The VPD is treated as
  2118. * an array of characters. In this routine, the ModelName, ProgramType, and
  2119. * ModelDesc, etc. fields of the phba data structure will be populated.
  2120. *
  2121. * Return codes
  2122. * 0 - pointer to the VPD passed in is NULL
  2123. * 1 - success
  2124. **/
  2125. int
  2126. lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
  2127. {
  2128. uint8_t lenlo, lenhi;
  2129. int Length;
  2130. int i;
  2131. int finished = 0;
  2132. int index = 0;
  2133. if (!vpd)
  2134. return 0;
  2135. /* Vital Product */
  2136. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2137. "0455 Vital Product Data: x%x x%x x%x x%x\n",
  2138. (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
  2139. (uint32_t) vpd[3]);
  2140. while (!finished && (index < (len - 4))) {
  2141. switch (vpd[index]) {
  2142. case 0x82:
  2143. case 0x91:
  2144. index += 1;
  2145. lenlo = vpd[index];
  2146. index += 1;
  2147. lenhi = vpd[index];
  2148. index += 1;
  2149. i = ((((unsigned short)lenhi) << 8) + lenlo);
  2150. index += i;
  2151. break;
  2152. case 0x90:
  2153. index += 1;
  2154. lenlo = vpd[index];
  2155. index += 1;
  2156. lenhi = vpd[index];
  2157. index += 1;
  2158. Length = ((((unsigned short)lenhi) << 8) + lenlo);
  2159. if (Length > len - index)
  2160. Length = len - index;
  2161. lpfc_fill_vpd(phba, vpd, Length, &index);
  2162. finished = 0;
  2163. break;
  2164. case 0x78:
  2165. finished = 1;
  2166. break;
  2167. default:
  2168. index ++;
  2169. break;
  2170. }
  2171. }
  2172. return(1);
  2173. }
  2174. /**
  2175. * lpfc_get_atto_model_desc - Retrieve ATTO HBA device model name and description
  2176. * @phba: pointer to lpfc hba data structure.
  2177. * @mdp: pointer to the data structure to hold the derived model name.
  2178. * @descp: pointer to the data structure to hold the derived description.
  2179. *
  2180. * This routine retrieves HBA's description based on its registered PCI device
  2181. * ID. The @descp passed into this function points to an array of 256 chars. It
  2182. * shall be returned with the model name, maximum speed, and the host bus type.
  2183. * The @mdp passed into this function points to an array of 80 chars. When the
  2184. * function returns, the @mdp will be filled with the model name.
  2185. **/
  2186. static void
  2187. lpfc_get_atto_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
  2188. {
  2189. uint16_t sub_dev_id = phba->pcidev->subsystem_device;
  2190. char *model = "<Unknown>";
  2191. int tbolt = 0;
  2192. switch (sub_dev_id) {
  2193. case PCI_DEVICE_ID_CLRY_161E:
  2194. model = "161E";
  2195. break;
  2196. case PCI_DEVICE_ID_CLRY_162E:
  2197. model = "162E";
  2198. break;
  2199. case PCI_DEVICE_ID_CLRY_164E:
  2200. model = "164E";
  2201. break;
  2202. case PCI_DEVICE_ID_CLRY_161P:
  2203. model = "161P";
  2204. break;
  2205. case PCI_DEVICE_ID_CLRY_162P:
  2206. model = "162P";
  2207. break;
  2208. case PCI_DEVICE_ID_CLRY_164P:
  2209. model = "164P";
  2210. break;
  2211. case PCI_DEVICE_ID_CLRY_321E:
  2212. model = "321E";
  2213. break;
  2214. case PCI_DEVICE_ID_CLRY_322E:
  2215. model = "322E";
  2216. break;
  2217. case PCI_DEVICE_ID_CLRY_324E:
  2218. model = "324E";
  2219. break;
  2220. case PCI_DEVICE_ID_CLRY_321P:
  2221. model = "321P";
  2222. break;
  2223. case PCI_DEVICE_ID_CLRY_322P:
  2224. model = "322P";
  2225. break;
  2226. case PCI_DEVICE_ID_CLRY_324P:
  2227. model = "324P";
  2228. break;
  2229. case PCI_DEVICE_ID_TLFC_2XX2:
  2230. model = "2XX2";
  2231. tbolt = 1;
  2232. break;
  2233. case PCI_DEVICE_ID_TLFC_3162:
  2234. model = "3162";
  2235. tbolt = 1;
  2236. break;
  2237. case PCI_DEVICE_ID_TLFC_3322:
  2238. model = "3322";
  2239. tbolt = 1;
  2240. break;
  2241. default:
  2242. model = "Unknown";
  2243. break;
  2244. }
  2245. if (mdp && mdp[0] == '\0')
  2246. snprintf(mdp, 79, "%s", model);
  2247. if (descp && descp[0] == '\0')
  2248. snprintf(descp, 255,
  2249. "ATTO %s%s, Fibre Channel Adapter Initiator, Port %s",
  2250. (tbolt) ? "ThunderLink FC " : "Celerity FC-",
  2251. model,
  2252. phba->Port);
  2253. }
  2254. /**
  2255. * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
  2256. * @phba: pointer to lpfc hba data structure.
  2257. * @mdp: pointer to the data structure to hold the derived model name.
  2258. * @descp: pointer to the data structure to hold the derived description.
  2259. *
  2260. * This routine retrieves HBA's description based on its registered PCI device
  2261. * ID. The @descp passed into this function points to an array of 256 chars. It
  2262. * shall be returned with the model name, maximum speed, and the host bus type.
  2263. * The @mdp passed into this function points to an array of 80 chars. When the
  2264. * function returns, the @mdp will be filled with the model name.
  2265. **/
  2266. static void
  2267. lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
  2268. {
  2269. lpfc_vpd_t *vp;
  2270. uint16_t dev_id = phba->pcidev->device;
  2271. int max_speed;
  2272. int GE = 0;
  2273. int oneConnect = 0; /* default is not a oneConnect */
  2274. struct {
  2275. char *name;
  2276. char *bus;
  2277. char *function;
  2278. } m = {"<Unknown>", "", ""};
  2279. if (mdp && mdp[0] != '\0'
  2280. && descp && descp[0] != '\0')
  2281. return;
  2282. if (phba->pcidev->vendor == PCI_VENDOR_ID_ATTO) {
  2283. lpfc_get_atto_model_desc(phba, mdp, descp);
  2284. return;
  2285. }
  2286. if (phba->lmt & LMT_64Gb)
  2287. max_speed = 64;
  2288. else if (phba->lmt & LMT_32Gb)
  2289. max_speed = 32;
  2290. else if (phba->lmt & LMT_16Gb)
  2291. max_speed = 16;
  2292. else if (phba->lmt & LMT_10Gb)
  2293. max_speed = 10;
  2294. else if (phba->lmt & LMT_8Gb)
  2295. max_speed = 8;
  2296. else if (phba->lmt & LMT_4Gb)
  2297. max_speed = 4;
  2298. else if (phba->lmt & LMT_2Gb)
  2299. max_speed = 2;
  2300. else if (phba->lmt & LMT_1Gb)
  2301. max_speed = 1;
  2302. else
  2303. max_speed = 0;
  2304. vp = &phba->vpd;
  2305. switch (dev_id) {
  2306. case PCI_DEVICE_ID_FIREFLY:
  2307. m = (typeof(m)){"LP6000", "PCI",
  2308. "Obsolete, Unsupported Fibre Channel Adapter"};
  2309. break;
  2310. case PCI_DEVICE_ID_SUPERFLY:
  2311. if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
  2312. m = (typeof(m)){"LP7000", "PCI", ""};
  2313. else
  2314. m = (typeof(m)){"LP7000E", "PCI", ""};
  2315. m.function = "Obsolete, Unsupported Fibre Channel Adapter";
  2316. break;
  2317. case PCI_DEVICE_ID_DRAGONFLY:
  2318. m = (typeof(m)){"LP8000", "PCI",
  2319. "Obsolete, Unsupported Fibre Channel Adapter"};
  2320. break;
  2321. case PCI_DEVICE_ID_CENTAUR:
  2322. if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
  2323. m = (typeof(m)){"LP9002", "PCI", ""};
  2324. else
  2325. m = (typeof(m)){"LP9000", "PCI", ""};
  2326. m.function = "Obsolete, Unsupported Fibre Channel Adapter";
  2327. break;
  2328. case PCI_DEVICE_ID_RFLY:
  2329. m = (typeof(m)){"LP952", "PCI",
  2330. "Obsolete, Unsupported Fibre Channel Adapter"};
  2331. break;
  2332. case PCI_DEVICE_ID_PEGASUS:
  2333. m = (typeof(m)){"LP9802", "PCI-X",
  2334. "Obsolete, Unsupported Fibre Channel Adapter"};
  2335. break;
  2336. case PCI_DEVICE_ID_THOR:
  2337. m = (typeof(m)){"LP10000", "PCI-X",
  2338. "Obsolete, Unsupported Fibre Channel Adapter"};
  2339. break;
  2340. case PCI_DEVICE_ID_VIPER:
  2341. m = (typeof(m)){"LPX1000", "PCI-X",
  2342. "Obsolete, Unsupported Fibre Channel Adapter"};
  2343. break;
  2344. case PCI_DEVICE_ID_PFLY:
  2345. m = (typeof(m)){"LP982", "PCI-X",
  2346. "Obsolete, Unsupported Fibre Channel Adapter"};
  2347. break;
  2348. case PCI_DEVICE_ID_TFLY:
  2349. m = (typeof(m)){"LP1050", "PCI-X",
  2350. "Obsolete, Unsupported Fibre Channel Adapter"};
  2351. break;
  2352. case PCI_DEVICE_ID_HELIOS:
  2353. m = (typeof(m)){"LP11000", "PCI-X2",
  2354. "Obsolete, Unsupported Fibre Channel Adapter"};
  2355. break;
  2356. case PCI_DEVICE_ID_HELIOS_SCSP:
  2357. m = (typeof(m)){"LP11000-SP", "PCI-X2",
  2358. "Obsolete, Unsupported Fibre Channel Adapter"};
  2359. break;
  2360. case PCI_DEVICE_ID_HELIOS_DCSP:
  2361. m = (typeof(m)){"LP11002-SP", "PCI-X2",
  2362. "Obsolete, Unsupported Fibre Channel Adapter"};
  2363. break;
  2364. case PCI_DEVICE_ID_NEPTUNE:
  2365. m = (typeof(m)){"LPe1000", "PCIe",
  2366. "Obsolete, Unsupported Fibre Channel Adapter"};
  2367. break;
  2368. case PCI_DEVICE_ID_NEPTUNE_SCSP:
  2369. m = (typeof(m)){"LPe1000-SP", "PCIe",
  2370. "Obsolete, Unsupported Fibre Channel Adapter"};
  2371. break;
  2372. case PCI_DEVICE_ID_NEPTUNE_DCSP:
  2373. m = (typeof(m)){"LPe1002-SP", "PCIe",
  2374. "Obsolete, Unsupported Fibre Channel Adapter"};
  2375. break;
  2376. case PCI_DEVICE_ID_BMID:
  2377. m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
  2378. break;
  2379. case PCI_DEVICE_ID_BSMB:
  2380. m = (typeof(m)){"LP111", "PCI-X2",
  2381. "Obsolete, Unsupported Fibre Channel Adapter"};
  2382. break;
  2383. case PCI_DEVICE_ID_ZEPHYR:
  2384. m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
  2385. break;
  2386. case PCI_DEVICE_ID_ZEPHYR_SCSP:
  2387. m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
  2388. break;
  2389. case PCI_DEVICE_ID_ZEPHYR_DCSP:
  2390. m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
  2391. GE = 1;
  2392. break;
  2393. case PCI_DEVICE_ID_ZMID:
  2394. m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
  2395. break;
  2396. case PCI_DEVICE_ID_ZSMB:
  2397. m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
  2398. break;
  2399. case PCI_DEVICE_ID_LP101:
  2400. m = (typeof(m)){"LP101", "PCI-X",
  2401. "Obsolete, Unsupported Fibre Channel Adapter"};
  2402. break;
  2403. case PCI_DEVICE_ID_LP10000S:
  2404. m = (typeof(m)){"LP10000-S", "PCI",
  2405. "Obsolete, Unsupported Fibre Channel Adapter"};
  2406. break;
  2407. case PCI_DEVICE_ID_LP11000S:
  2408. m = (typeof(m)){"LP11000-S", "PCI-X2",
  2409. "Obsolete, Unsupported Fibre Channel Adapter"};
  2410. break;
  2411. case PCI_DEVICE_ID_LPE11000S:
  2412. m = (typeof(m)){"LPe11000-S", "PCIe",
  2413. "Obsolete, Unsupported Fibre Channel Adapter"};
  2414. break;
  2415. case PCI_DEVICE_ID_SAT:
  2416. m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
  2417. break;
  2418. case PCI_DEVICE_ID_SAT_MID:
  2419. m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
  2420. break;
  2421. case PCI_DEVICE_ID_SAT_SMB:
  2422. m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
  2423. break;
  2424. case PCI_DEVICE_ID_SAT_DCSP:
  2425. m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
  2426. break;
  2427. case PCI_DEVICE_ID_SAT_SCSP:
  2428. m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
  2429. break;
  2430. case PCI_DEVICE_ID_SAT_S:
  2431. m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
  2432. break;
  2433. case PCI_DEVICE_ID_PROTEUS_VF:
  2434. m = (typeof(m)){"LPev12000", "PCIe IOV",
  2435. "Obsolete, Unsupported Fibre Channel Adapter"};
  2436. break;
  2437. case PCI_DEVICE_ID_PROTEUS_PF:
  2438. m = (typeof(m)){"LPev12000", "PCIe IOV",
  2439. "Obsolete, Unsupported Fibre Channel Adapter"};
  2440. break;
  2441. case PCI_DEVICE_ID_PROTEUS_S:
  2442. m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
  2443. "Obsolete, Unsupported Fibre Channel Adapter"};
  2444. break;
  2445. case PCI_DEVICE_ID_TIGERSHARK:
  2446. oneConnect = 1;
  2447. m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
  2448. break;
  2449. case PCI_DEVICE_ID_TOMCAT:
  2450. oneConnect = 1;
  2451. m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
  2452. break;
  2453. case PCI_DEVICE_ID_FALCON:
  2454. m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
  2455. "EmulexSecure Fibre"};
  2456. break;
  2457. case PCI_DEVICE_ID_BALIUS:
  2458. m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
  2459. "Obsolete, Unsupported Fibre Channel Adapter"};
  2460. break;
  2461. case PCI_DEVICE_ID_LANCER_FC:
  2462. m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
  2463. break;
  2464. case PCI_DEVICE_ID_LANCER_FC_VF:
  2465. m = (typeof(m)){"LPe16000", "PCIe",
  2466. "Obsolete, Unsupported Fibre Channel Adapter"};
  2467. break;
  2468. case PCI_DEVICE_ID_LANCER_FCOE:
  2469. oneConnect = 1;
  2470. m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
  2471. break;
  2472. case PCI_DEVICE_ID_LANCER_FCOE_VF:
  2473. oneConnect = 1;
  2474. m = (typeof(m)){"OCe15100", "PCIe",
  2475. "Obsolete, Unsupported FCoE"};
  2476. break;
  2477. case PCI_DEVICE_ID_LANCER_G6_FC:
  2478. m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
  2479. break;
  2480. case PCI_DEVICE_ID_LANCER_G7_FC:
  2481. m = (typeof(m)){"LPe36000", "PCIe", "Fibre Channel Adapter"};
  2482. break;
  2483. case PCI_DEVICE_ID_LANCER_G7P_FC:
  2484. m = (typeof(m)){"LPe38000", "PCIe", "Fibre Channel Adapter"};
  2485. break;
  2486. case PCI_DEVICE_ID_SKYHAWK:
  2487. case PCI_DEVICE_ID_SKYHAWK_VF:
  2488. oneConnect = 1;
  2489. m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
  2490. break;
  2491. default:
  2492. m = (typeof(m)){"Unknown", "", ""};
  2493. break;
  2494. }
  2495. if (mdp && mdp[0] == '\0')
  2496. snprintf(mdp, 79,"%s", m.name);
  2497. /*
  2498. * oneConnect hba requires special processing, they are all initiators
  2499. * and we put the port number on the end
  2500. */
  2501. if (descp && descp[0] == '\0') {
  2502. if (oneConnect)
  2503. snprintf(descp, 255,
  2504. "Emulex OneConnect %s, %s Initiator %s",
  2505. m.name, m.function,
  2506. phba->Port);
  2507. else if (max_speed == 0)
  2508. snprintf(descp, 255,
  2509. "Emulex %s %s %s",
  2510. m.name, m.bus, m.function);
  2511. else
  2512. snprintf(descp, 255,
  2513. "Emulex %s %d%s %s %s",
  2514. m.name, max_speed, (GE) ? "GE" : "Gb",
  2515. m.bus, m.function);
  2516. }
  2517. }
  2518. /**
  2519. * lpfc_sli3_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
  2520. * @phba: pointer to lpfc hba data structure.
  2521. * @pring: pointer to a IOCB ring.
  2522. * @cnt: the number of IOCBs to be posted to the IOCB ring.
  2523. *
  2524. * This routine posts a given number of IOCBs with the associated DMA buffer
  2525. * descriptors specified by the cnt argument to the given IOCB ring.
  2526. *
  2527. * Return codes
  2528. * The number of IOCBs NOT able to be posted to the IOCB ring.
  2529. **/
  2530. int
  2531. lpfc_sli3_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
  2532. {
  2533. IOCB_t *icmd;
  2534. struct lpfc_iocbq *iocb;
  2535. struct lpfc_dmabuf *mp1, *mp2;
  2536. cnt += pring->missbufcnt;
  2537. /* While there are buffers to post */
  2538. while (cnt > 0) {
  2539. /* Allocate buffer for command iocb */
  2540. iocb = lpfc_sli_get_iocbq(phba);
  2541. if (iocb == NULL) {
  2542. pring->missbufcnt = cnt;
  2543. return cnt;
  2544. }
  2545. icmd = &iocb->iocb;
  2546. /* 2 buffers can be posted per command */
  2547. /* Allocate buffer to post */
  2548. mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
  2549. if (mp1)
  2550. mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
  2551. if (!mp1 || !mp1->virt) {
  2552. kfree(mp1);
  2553. lpfc_sli_release_iocbq(phba, iocb);
  2554. pring->missbufcnt = cnt;
  2555. return cnt;
  2556. }
  2557. INIT_LIST_HEAD(&mp1->list);
  2558. /* Allocate buffer to post */
  2559. if (cnt > 1) {
  2560. mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
  2561. if (mp2)
  2562. mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
  2563. &mp2->phys);
  2564. if (!mp2 || !mp2->virt) {
  2565. kfree(mp2);
  2566. lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
  2567. kfree(mp1);
  2568. lpfc_sli_release_iocbq(phba, iocb);
  2569. pring->missbufcnt = cnt;
  2570. return cnt;
  2571. }
  2572. INIT_LIST_HEAD(&mp2->list);
  2573. } else {
  2574. mp2 = NULL;
  2575. }
  2576. icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
  2577. icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
  2578. icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
  2579. icmd->ulpBdeCount = 1;
  2580. cnt--;
  2581. if (mp2) {
  2582. icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
  2583. icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
  2584. icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
  2585. cnt--;
  2586. icmd->ulpBdeCount = 2;
  2587. }
  2588. icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
  2589. icmd->ulpLe = 1;
  2590. if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
  2591. IOCB_ERROR) {
  2592. lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
  2593. kfree(mp1);
  2594. cnt++;
  2595. if (mp2) {
  2596. lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
  2597. kfree(mp2);
  2598. cnt++;
  2599. }
  2600. lpfc_sli_release_iocbq(phba, iocb);
  2601. pring->missbufcnt = cnt;
  2602. return cnt;
  2603. }
  2604. lpfc_sli_ringpostbuf_put(phba, pring, mp1);
  2605. if (mp2)
  2606. lpfc_sli_ringpostbuf_put(phba, pring, mp2);
  2607. }
  2608. pring->missbufcnt = 0;
  2609. return 0;
  2610. }
  2611. /**
  2612. * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
  2613. * @phba: pointer to lpfc hba data structure.
  2614. *
  2615. * This routine posts initial receive IOCB buffers to the ELS ring. The
  2616. * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
  2617. * set to 64 IOCBs. SLI3 only.
  2618. *
  2619. * Return codes
  2620. * 0 - success (currently always success)
  2621. **/
  2622. static int
  2623. lpfc_post_rcv_buf(struct lpfc_hba *phba)
  2624. {
  2625. struct lpfc_sli *psli = &phba->sli;
  2626. /* Ring 0, ELS / CT buffers */
  2627. lpfc_sli3_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
  2628. /* Ring 2 - FCP no buffers needed */
  2629. return 0;
  2630. }
  2631. #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
  2632. /**
  2633. * lpfc_sha_init - Set up initial array of hash table entries
  2634. * @HashResultPointer: pointer to an array as hash table.
  2635. *
  2636. * This routine sets up the initial values to the array of hash table entries
  2637. * for the LC HBAs.
  2638. **/
  2639. static void
  2640. lpfc_sha_init(uint32_t * HashResultPointer)
  2641. {
  2642. HashResultPointer[0] = 0x67452301;
  2643. HashResultPointer[1] = 0xEFCDAB89;
  2644. HashResultPointer[2] = 0x98BADCFE;
  2645. HashResultPointer[3] = 0x10325476;
  2646. HashResultPointer[4] = 0xC3D2E1F0;
  2647. }
  2648. /**
  2649. * lpfc_sha_iterate - Iterate initial hash table with the working hash table
  2650. * @HashResultPointer: pointer to an initial/result hash table.
  2651. * @HashWorkingPointer: pointer to an working hash table.
  2652. *
  2653. * This routine iterates an initial hash table pointed by @HashResultPointer
  2654. * with the values from the working hash table pointeed by @HashWorkingPointer.
  2655. * The results are putting back to the initial hash table, returned through
  2656. * the @HashResultPointer as the result hash table.
  2657. **/
  2658. static void
  2659. lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
  2660. {
  2661. int t;
  2662. uint32_t TEMP;
  2663. uint32_t A, B, C, D, E;
  2664. t = 16;
  2665. do {
  2666. HashWorkingPointer[t] =
  2667. S(1,
  2668. HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
  2669. 8] ^
  2670. HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
  2671. } while (++t <= 79);
  2672. t = 0;
  2673. A = HashResultPointer[0];
  2674. B = HashResultPointer[1];
  2675. C = HashResultPointer[2];
  2676. D = HashResultPointer[3];
  2677. E = HashResultPointer[4];
  2678. do {
  2679. if (t < 20) {
  2680. TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
  2681. } else if (t < 40) {
  2682. TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
  2683. } else if (t < 60) {
  2684. TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
  2685. } else {
  2686. TEMP = (B ^ C ^ D) + 0xCA62C1D6;
  2687. }
  2688. TEMP += S(5, A) + E + HashWorkingPointer[t];
  2689. E = D;
  2690. D = C;
  2691. C = S(30, B);
  2692. B = A;
  2693. A = TEMP;
  2694. } while (++t <= 79);
  2695. HashResultPointer[0] += A;
  2696. HashResultPointer[1] += B;
  2697. HashResultPointer[2] += C;
  2698. HashResultPointer[3] += D;
  2699. HashResultPointer[4] += E;
  2700. }
  2701. /**
  2702. * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
  2703. * @RandomChallenge: pointer to the entry of host challenge random number array.
  2704. * @HashWorking: pointer to the entry of the working hash array.
  2705. *
  2706. * This routine calculates the working hash array referred by @HashWorking
  2707. * from the challenge random numbers associated with the host, referred by
  2708. * @RandomChallenge. The result is put into the entry of the working hash
  2709. * array and returned by reference through @HashWorking.
  2710. **/
  2711. static void
  2712. lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
  2713. {
  2714. *HashWorking = (*RandomChallenge ^ *HashWorking);
  2715. }
  2716. /**
  2717. * lpfc_hba_init - Perform special handling for LC HBA initialization
  2718. * @phba: pointer to lpfc hba data structure.
  2719. * @hbainit: pointer to an array of unsigned 32-bit integers.
  2720. *
  2721. * This routine performs the special handling for LC HBA initialization.
  2722. **/
  2723. void
  2724. lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
  2725. {
  2726. int t;
  2727. uint32_t *HashWorking;
  2728. uint32_t *pwwnn = (uint32_t *) phba->wwnn;
  2729. HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
  2730. if (!HashWorking)
  2731. return;
  2732. HashWorking[0] = HashWorking[78] = *pwwnn++;
  2733. HashWorking[1] = HashWorking[79] = *pwwnn;
  2734. for (t = 0; t < 7; t++)
  2735. lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
  2736. lpfc_sha_init(hbainit);
  2737. lpfc_sha_iterate(hbainit, HashWorking);
  2738. kfree(HashWorking);
  2739. }
  2740. /**
  2741. * lpfc_cleanup - Performs vport cleanups before deleting a vport
  2742. * @vport: pointer to a virtual N_Port data structure.
  2743. *
  2744. * This routine performs the necessary cleanups before deleting the @vport.
  2745. * It invokes the discovery state machine to perform necessary state
  2746. * transitions and to release the ndlps associated with the @vport. Note,
  2747. * the physical port is treated as @vport 0.
  2748. **/
  2749. void
  2750. lpfc_cleanup(struct lpfc_vport *vport)
  2751. {
  2752. struct lpfc_hba *phba = vport->phba;
  2753. struct lpfc_nodelist *ndlp, *next_ndlp;
  2754. int i = 0;
  2755. if (phba->link_state > LPFC_LINK_DOWN)
  2756. lpfc_port_link_failure(vport);
  2757. /* Clean up VMID resources */
  2758. if (lpfc_is_vmid_enabled(phba))
  2759. lpfc_vmid_vport_cleanup(vport);
  2760. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  2761. if (vport->port_type != LPFC_PHYSICAL_PORT &&
  2762. ndlp->nlp_DID == Fabric_DID) {
  2763. /* Just free up ndlp with Fabric_DID for vports */
  2764. lpfc_nlp_put(ndlp);
  2765. continue;
  2766. }
  2767. if (ndlp->nlp_DID == Fabric_Cntl_DID &&
  2768. ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
  2769. lpfc_nlp_put(ndlp);
  2770. continue;
  2771. }
  2772. /* Fabric Ports not in UNMAPPED state are cleaned up in the
  2773. * DEVICE_RM event.
  2774. */
  2775. if (ndlp->nlp_type & NLP_FABRIC &&
  2776. ndlp->nlp_state == NLP_STE_UNMAPPED_NODE)
  2777. lpfc_disc_state_machine(vport, ndlp, NULL,
  2778. NLP_EVT_DEVICE_RECOVERY);
  2779. if (!(ndlp->fc4_xpt_flags & (NVME_XPT_REGD|SCSI_XPT_REGD)))
  2780. lpfc_disc_state_machine(vport, ndlp, NULL,
  2781. NLP_EVT_DEVICE_RM);
  2782. }
  2783. /* This is a special case flush to return all
  2784. * IOs before entering this loop. There are
  2785. * two points in the code where a flush is
  2786. * avoided if the FC_UNLOADING flag is set.
  2787. * one is in the multipool destroy,
  2788. * (this prevents a crash) and the other is
  2789. * in the nvme abort handler, ( also prevents
  2790. * a crash). Both of these exceptions are
  2791. * cases where the slot is still accessible.
  2792. * The flush here is only when the pci slot
  2793. * is offline.
  2794. */
  2795. if (test_bit(FC_UNLOADING, &vport->load_flag) &&
  2796. pci_channel_offline(phba->pcidev))
  2797. lpfc_sli_flush_io_rings(vport->phba);
  2798. /* At this point, ALL ndlp's should be gone
  2799. * because of the previous NLP_EVT_DEVICE_RM.
  2800. * Lets wait for this to happen, if needed.
  2801. */
  2802. while (!list_empty(&vport->fc_nodes)) {
  2803. if (i++ > 3000) {
  2804. lpfc_printf_vlog(vport, KERN_ERR,
  2805. LOG_TRACE_EVENT,
  2806. "0233 Nodelist not empty\n");
  2807. list_for_each_entry_safe(ndlp, next_ndlp,
  2808. &vport->fc_nodes, nlp_listp) {
  2809. lpfc_printf_vlog(ndlp->vport, KERN_ERR,
  2810. LOG_DISCOVERY,
  2811. "0282 did:x%x ndlp:x%px "
  2812. "refcnt:%d xflags x%x nflag x%x\n",
  2813. ndlp->nlp_DID, (void *)ndlp,
  2814. kref_read(&ndlp->kref),
  2815. ndlp->fc4_xpt_flags,
  2816. ndlp->nlp_flag);
  2817. }
  2818. break;
  2819. }
  2820. /* Wait for any activity on ndlps to settle */
  2821. msleep(10);
  2822. }
  2823. lpfc_cleanup_vports_rrqs(vport, NULL);
  2824. }
  2825. /**
  2826. * lpfc_stop_vport_timers - Stop all the timers associated with a vport
  2827. * @vport: pointer to a virtual N_Port data structure.
  2828. *
  2829. * This routine stops all the timers associated with a @vport. This function
  2830. * is invoked before disabling or deleting a @vport. Note that the physical
  2831. * port is treated as @vport 0.
  2832. **/
  2833. void
  2834. lpfc_stop_vport_timers(struct lpfc_vport *vport)
  2835. {
  2836. del_timer_sync(&vport->els_tmofunc);
  2837. del_timer_sync(&vport->delayed_disc_tmo);
  2838. lpfc_can_disctmo(vport);
  2839. return;
  2840. }
  2841. /**
  2842. * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
  2843. * @phba: pointer to lpfc hba data structure.
  2844. *
  2845. * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
  2846. * caller of this routine should already hold the host lock.
  2847. **/
  2848. void
  2849. __lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
  2850. {
  2851. /* Clear pending FCF rediscovery wait flag */
  2852. phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
  2853. /* Now, try to stop the timer */
  2854. del_timer(&phba->fcf.redisc_wait);
  2855. }
  2856. /**
  2857. * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
  2858. * @phba: pointer to lpfc hba data structure.
  2859. *
  2860. * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
  2861. * checks whether the FCF rediscovery wait timer is pending with the host
  2862. * lock held before proceeding with disabling the timer and clearing the
  2863. * wait timer pendig flag.
  2864. **/
  2865. void
  2866. lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
  2867. {
  2868. spin_lock_irq(&phba->hbalock);
  2869. if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
  2870. /* FCF rediscovery timer already fired or stopped */
  2871. spin_unlock_irq(&phba->hbalock);
  2872. return;
  2873. }
  2874. __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
  2875. /* Clear failover in progress flags */
  2876. phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
  2877. spin_unlock_irq(&phba->hbalock);
  2878. }
  2879. /**
  2880. * lpfc_cmf_stop - Stop CMF processing
  2881. * @phba: pointer to lpfc hba data structure.
  2882. *
  2883. * This is called when the link goes down or if CMF mode is turned OFF.
  2884. * It is also called when going offline or unloaded just before the
  2885. * congestion info buffer is unregistered.
  2886. **/
  2887. void
  2888. lpfc_cmf_stop(struct lpfc_hba *phba)
  2889. {
  2890. int cpu;
  2891. struct lpfc_cgn_stat *cgs;
  2892. /* We only do something if CMF is enabled */
  2893. if (!phba->sli4_hba.pc_sli4_params.cmf)
  2894. return;
  2895. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
  2896. "6221 Stop CMF / Cancel Timer\n");
  2897. /* Cancel the CMF timer */
  2898. hrtimer_cancel(&phba->cmf_stats_timer);
  2899. hrtimer_cancel(&phba->cmf_timer);
  2900. /* Zero CMF counters */
  2901. atomic_set(&phba->cmf_busy, 0);
  2902. for_each_present_cpu(cpu) {
  2903. cgs = per_cpu_ptr(phba->cmf_stat, cpu);
  2904. atomic64_set(&cgs->total_bytes, 0);
  2905. atomic64_set(&cgs->rcv_bytes, 0);
  2906. atomic_set(&cgs->rx_io_cnt, 0);
  2907. atomic64_set(&cgs->rx_latency, 0);
  2908. }
  2909. atomic_set(&phba->cmf_bw_wait, 0);
  2910. /* Resume any blocked IO - Queue unblock on workqueue */
  2911. queue_work(phba->wq, &phba->unblock_request_work);
  2912. }
  2913. static inline uint64_t
  2914. lpfc_get_max_line_rate(struct lpfc_hba *phba)
  2915. {
  2916. uint64_t rate = lpfc_sli_port_speed_get(phba);
  2917. return ((((unsigned long)rate) * 1024 * 1024) / 10);
  2918. }
  2919. void
  2920. lpfc_cmf_signal_init(struct lpfc_hba *phba)
  2921. {
  2922. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
  2923. "6223 Signal CMF init\n");
  2924. /* Use the new fc_linkspeed to recalculate */
  2925. phba->cmf_interval_rate = LPFC_CMF_INTERVAL;
  2926. phba->cmf_max_line_rate = lpfc_get_max_line_rate(phba);
  2927. phba->cmf_link_byte_count = div_u64(phba->cmf_max_line_rate *
  2928. phba->cmf_interval_rate, 1000);
  2929. phba->cmf_max_bytes_per_interval = phba->cmf_link_byte_count;
  2930. /* This is a signal to firmware to sync up CMF BW with link speed */
  2931. lpfc_issue_cmf_sync_wqe(phba, 0, 0);
  2932. }
  2933. /**
  2934. * lpfc_cmf_start - Start CMF processing
  2935. * @phba: pointer to lpfc hba data structure.
  2936. *
  2937. * This is called when the link comes up or if CMF mode is turned OFF
  2938. * to Monitor or Managed.
  2939. **/
  2940. void
  2941. lpfc_cmf_start(struct lpfc_hba *phba)
  2942. {
  2943. struct lpfc_cgn_stat *cgs;
  2944. int cpu;
  2945. /* We only do something if CMF is enabled */
  2946. if (!phba->sli4_hba.pc_sli4_params.cmf ||
  2947. phba->cmf_active_mode == LPFC_CFG_OFF)
  2948. return;
  2949. /* Reinitialize congestion buffer info */
  2950. lpfc_init_congestion_buf(phba);
  2951. atomic_set(&phba->cgn_fabric_warn_cnt, 0);
  2952. atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
  2953. atomic_set(&phba->cgn_sync_alarm_cnt, 0);
  2954. atomic_set(&phba->cgn_sync_warn_cnt, 0);
  2955. atomic_set(&phba->cmf_busy, 0);
  2956. for_each_present_cpu(cpu) {
  2957. cgs = per_cpu_ptr(phba->cmf_stat, cpu);
  2958. atomic64_set(&cgs->total_bytes, 0);
  2959. atomic64_set(&cgs->rcv_bytes, 0);
  2960. atomic_set(&cgs->rx_io_cnt, 0);
  2961. atomic64_set(&cgs->rx_latency, 0);
  2962. }
  2963. phba->cmf_latency.tv_sec = 0;
  2964. phba->cmf_latency.tv_nsec = 0;
  2965. lpfc_cmf_signal_init(phba);
  2966. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
  2967. "6222 Start CMF / Timer\n");
  2968. phba->cmf_timer_cnt = 0;
  2969. hrtimer_start(&phba->cmf_timer,
  2970. ktime_set(0, LPFC_CMF_INTERVAL * NSEC_PER_MSEC),
  2971. HRTIMER_MODE_REL);
  2972. hrtimer_start(&phba->cmf_stats_timer,
  2973. ktime_set(0, LPFC_SEC_MIN * NSEC_PER_SEC),
  2974. HRTIMER_MODE_REL);
  2975. /* Setup for latency check in IO cmpl routines */
  2976. ktime_get_real_ts64(&phba->cmf_latency);
  2977. atomic_set(&phba->cmf_bw_wait, 0);
  2978. atomic_set(&phba->cmf_stop_io, 0);
  2979. }
  2980. /**
  2981. * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
  2982. * @phba: pointer to lpfc hba data structure.
  2983. *
  2984. * This routine stops all the timers associated with a HBA. This function is
  2985. * invoked before either putting a HBA offline or unloading the driver.
  2986. **/
  2987. void
  2988. lpfc_stop_hba_timers(struct lpfc_hba *phba)
  2989. {
  2990. if (phba->pport)
  2991. lpfc_stop_vport_timers(phba->pport);
  2992. cancel_delayed_work_sync(&phba->eq_delay_work);
  2993. cancel_delayed_work_sync(&phba->idle_stat_delay_work);
  2994. del_timer_sync(&phba->sli.mbox_tmo);
  2995. del_timer_sync(&phba->fabric_block_timer);
  2996. del_timer_sync(&phba->eratt_poll);
  2997. del_timer_sync(&phba->hb_tmofunc);
  2998. if (phba->sli_rev == LPFC_SLI_REV4) {
  2999. del_timer_sync(&phba->rrq_tmr);
  3000. clear_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);
  3001. }
  3002. clear_bit(HBA_HBEAT_INP, &phba->hba_flag);
  3003. clear_bit(HBA_HBEAT_TMO, &phba->hba_flag);
  3004. switch (phba->pci_dev_grp) {
  3005. case LPFC_PCI_DEV_LP:
  3006. /* Stop any LightPulse device specific driver timers */
  3007. del_timer_sync(&phba->fcp_poll_timer);
  3008. break;
  3009. case LPFC_PCI_DEV_OC:
  3010. /* Stop any OneConnect device specific driver timers */
  3011. lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
  3012. break;
  3013. default:
  3014. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  3015. "0297 Invalid device group (x%x)\n",
  3016. phba->pci_dev_grp);
  3017. break;
  3018. }
  3019. return;
  3020. }
  3021. /**
  3022. * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
  3023. * @phba: pointer to lpfc hba data structure.
  3024. * @mbx_action: flag for mailbox no wait action.
  3025. *
  3026. * This routine marks a HBA's management interface as blocked. Once the HBA's
  3027. * management interface is marked as blocked, all the user space access to
  3028. * the HBA, whether they are from sysfs interface or libdfc interface will
  3029. * all be blocked. The HBA is set to block the management interface when the
  3030. * driver prepares the HBA interface for online or offline.
  3031. **/
  3032. static void
  3033. lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
  3034. {
  3035. unsigned long iflag;
  3036. uint8_t actcmd = MBX_HEARTBEAT;
  3037. unsigned long timeout;
  3038. spin_lock_irqsave(&phba->hbalock, iflag);
  3039. phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
  3040. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3041. if (mbx_action == LPFC_MBX_NO_WAIT)
  3042. return;
  3043. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  3044. spin_lock_irqsave(&phba->hbalock, iflag);
  3045. if (phba->sli.mbox_active) {
  3046. actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
  3047. /* Determine how long we might wait for the active mailbox
  3048. * command to be gracefully completed by firmware.
  3049. */
  3050. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  3051. phba->sli.mbox_active) * 1000) + jiffies;
  3052. }
  3053. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3054. /* Wait for the outstnading mailbox command to complete */
  3055. while (phba->sli.mbox_active) {
  3056. /* Check active mailbox complete status every 2ms */
  3057. msleep(2);
  3058. if (time_after(jiffies, timeout)) {
  3059. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  3060. "2813 Mgmt IO is Blocked %x "
  3061. "- mbox cmd %x still active\n",
  3062. phba->sli.sli_flag, actcmd);
  3063. break;
  3064. }
  3065. }
  3066. }
  3067. /**
  3068. * lpfc_sli4_node_prep - Assign RPIs for active nodes.
  3069. * @phba: pointer to lpfc hba data structure.
  3070. *
  3071. * Allocate RPIs for all active remote nodes. This is needed whenever
  3072. * an SLI4 adapter is reset and the driver is not unloading. Its purpose
  3073. * is to fixup the temporary rpi assignments.
  3074. **/
  3075. void
  3076. lpfc_sli4_node_prep(struct lpfc_hba *phba)
  3077. {
  3078. struct lpfc_nodelist *ndlp, *next_ndlp;
  3079. struct lpfc_vport **vports;
  3080. int i, rpi;
  3081. if (phba->sli_rev != LPFC_SLI_REV4)
  3082. return;
  3083. vports = lpfc_create_vport_work_array(phba);
  3084. if (vports == NULL)
  3085. return;
  3086. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  3087. if (test_bit(FC_UNLOADING, &vports[i]->load_flag))
  3088. continue;
  3089. list_for_each_entry_safe(ndlp, next_ndlp,
  3090. &vports[i]->fc_nodes,
  3091. nlp_listp) {
  3092. rpi = lpfc_sli4_alloc_rpi(phba);
  3093. if (rpi == LPFC_RPI_ALLOC_ERROR) {
  3094. /* TODO print log? */
  3095. continue;
  3096. }
  3097. ndlp->nlp_rpi = rpi;
  3098. lpfc_printf_vlog(ndlp->vport, KERN_INFO,
  3099. LOG_NODE | LOG_DISCOVERY,
  3100. "0009 Assign RPI x%x to ndlp x%px "
  3101. "DID:x%06x flg:x%x\n",
  3102. ndlp->nlp_rpi, ndlp, ndlp->nlp_DID,
  3103. ndlp->nlp_flag);
  3104. }
  3105. }
  3106. lpfc_destroy_vport_work_array(phba, vports);
  3107. }
  3108. /**
  3109. * lpfc_create_expedite_pool - create expedite pool
  3110. * @phba: pointer to lpfc hba data structure.
  3111. *
  3112. * This routine moves a batch of XRIs from lpfc_io_buf_list_put of HWQ 0
  3113. * to expedite pool. Mark them as expedite.
  3114. **/
  3115. static void lpfc_create_expedite_pool(struct lpfc_hba *phba)
  3116. {
  3117. struct lpfc_sli4_hdw_queue *qp;
  3118. struct lpfc_io_buf *lpfc_ncmd;
  3119. struct lpfc_io_buf *lpfc_ncmd_next;
  3120. struct lpfc_epd_pool *epd_pool;
  3121. unsigned long iflag;
  3122. epd_pool = &phba->epd_pool;
  3123. qp = &phba->sli4_hba.hdwq[0];
  3124. spin_lock_init(&epd_pool->lock);
  3125. spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
  3126. spin_lock(&epd_pool->lock);
  3127. INIT_LIST_HEAD(&epd_pool->list);
  3128. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  3129. &qp->lpfc_io_buf_list_put, list) {
  3130. list_move_tail(&lpfc_ncmd->list, &epd_pool->list);
  3131. lpfc_ncmd->expedite = true;
  3132. qp->put_io_bufs--;
  3133. epd_pool->count++;
  3134. if (epd_pool->count >= XRI_BATCH)
  3135. break;
  3136. }
  3137. spin_unlock(&epd_pool->lock);
  3138. spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
  3139. }
  3140. /**
  3141. * lpfc_destroy_expedite_pool - destroy expedite pool
  3142. * @phba: pointer to lpfc hba data structure.
  3143. *
  3144. * This routine returns XRIs from expedite pool to lpfc_io_buf_list_put
  3145. * of HWQ 0. Clear the mark.
  3146. **/
  3147. static void lpfc_destroy_expedite_pool(struct lpfc_hba *phba)
  3148. {
  3149. struct lpfc_sli4_hdw_queue *qp;
  3150. struct lpfc_io_buf *lpfc_ncmd;
  3151. struct lpfc_io_buf *lpfc_ncmd_next;
  3152. struct lpfc_epd_pool *epd_pool;
  3153. unsigned long iflag;
  3154. epd_pool = &phba->epd_pool;
  3155. qp = &phba->sli4_hba.hdwq[0];
  3156. spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
  3157. spin_lock(&epd_pool->lock);
  3158. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  3159. &epd_pool->list, list) {
  3160. list_move_tail(&lpfc_ncmd->list,
  3161. &qp->lpfc_io_buf_list_put);
  3162. lpfc_ncmd->flags = false;
  3163. qp->put_io_bufs++;
  3164. epd_pool->count--;
  3165. }
  3166. spin_unlock(&epd_pool->lock);
  3167. spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
  3168. }
  3169. /**
  3170. * lpfc_create_multixri_pools - create multi-XRI pools
  3171. * @phba: pointer to lpfc hba data structure.
  3172. *
  3173. * This routine initialize public, private per HWQ. Then, move XRIs from
  3174. * lpfc_io_buf_list_put to public pool. High and low watermark are also
  3175. * Initialized.
  3176. **/
  3177. void lpfc_create_multixri_pools(struct lpfc_hba *phba)
  3178. {
  3179. u32 i, j;
  3180. u32 hwq_count;
  3181. u32 count_per_hwq;
  3182. struct lpfc_io_buf *lpfc_ncmd;
  3183. struct lpfc_io_buf *lpfc_ncmd_next;
  3184. unsigned long iflag;
  3185. struct lpfc_sli4_hdw_queue *qp;
  3186. struct lpfc_multixri_pool *multixri_pool;
  3187. struct lpfc_pbl_pool *pbl_pool;
  3188. struct lpfc_pvt_pool *pvt_pool;
  3189. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3190. "1234 num_hdw_queue=%d num_present_cpu=%d common_xri_cnt=%d\n",
  3191. phba->cfg_hdw_queue, phba->sli4_hba.num_present_cpu,
  3192. phba->sli4_hba.io_xri_cnt);
  3193. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
  3194. lpfc_create_expedite_pool(phba);
  3195. hwq_count = phba->cfg_hdw_queue;
  3196. count_per_hwq = phba->sli4_hba.io_xri_cnt / hwq_count;
  3197. for (i = 0; i < hwq_count; i++) {
  3198. multixri_pool = kzalloc(sizeof(*multixri_pool), GFP_KERNEL);
  3199. if (!multixri_pool) {
  3200. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3201. "1238 Failed to allocate memory for "
  3202. "multixri_pool\n");
  3203. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
  3204. lpfc_destroy_expedite_pool(phba);
  3205. j = 0;
  3206. while (j < i) {
  3207. qp = &phba->sli4_hba.hdwq[j];
  3208. kfree(qp->p_multixri_pool);
  3209. j++;
  3210. }
  3211. phba->cfg_xri_rebalancing = 0;
  3212. return;
  3213. }
  3214. qp = &phba->sli4_hba.hdwq[i];
  3215. qp->p_multixri_pool = multixri_pool;
  3216. multixri_pool->xri_limit = count_per_hwq;
  3217. multixri_pool->rrb_next_hwqid = i;
  3218. /* Deal with public free xri pool */
  3219. pbl_pool = &multixri_pool->pbl_pool;
  3220. spin_lock_init(&pbl_pool->lock);
  3221. spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
  3222. spin_lock(&pbl_pool->lock);
  3223. INIT_LIST_HEAD(&pbl_pool->list);
  3224. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  3225. &qp->lpfc_io_buf_list_put, list) {
  3226. list_move_tail(&lpfc_ncmd->list, &pbl_pool->list);
  3227. qp->put_io_bufs--;
  3228. pbl_pool->count++;
  3229. }
  3230. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3231. "1235 Moved %d buffers from PUT list over to pbl_pool[%d]\n",
  3232. pbl_pool->count, i);
  3233. spin_unlock(&pbl_pool->lock);
  3234. spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
  3235. /* Deal with private free xri pool */
  3236. pvt_pool = &multixri_pool->pvt_pool;
  3237. pvt_pool->high_watermark = multixri_pool->xri_limit / 2;
  3238. pvt_pool->low_watermark = XRI_BATCH;
  3239. spin_lock_init(&pvt_pool->lock);
  3240. spin_lock_irqsave(&pvt_pool->lock, iflag);
  3241. INIT_LIST_HEAD(&pvt_pool->list);
  3242. pvt_pool->count = 0;
  3243. spin_unlock_irqrestore(&pvt_pool->lock, iflag);
  3244. }
  3245. }
  3246. /**
  3247. * lpfc_destroy_multixri_pools - destroy multi-XRI pools
  3248. * @phba: pointer to lpfc hba data structure.
  3249. *
  3250. * This routine returns XRIs from public/private to lpfc_io_buf_list_put.
  3251. **/
  3252. static void lpfc_destroy_multixri_pools(struct lpfc_hba *phba)
  3253. {
  3254. u32 i;
  3255. u32 hwq_count;
  3256. struct lpfc_io_buf *lpfc_ncmd;
  3257. struct lpfc_io_buf *lpfc_ncmd_next;
  3258. unsigned long iflag;
  3259. struct lpfc_sli4_hdw_queue *qp;
  3260. struct lpfc_multixri_pool *multixri_pool;
  3261. struct lpfc_pbl_pool *pbl_pool;
  3262. struct lpfc_pvt_pool *pvt_pool;
  3263. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
  3264. lpfc_destroy_expedite_pool(phba);
  3265. if (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
  3266. lpfc_sli_flush_io_rings(phba);
  3267. hwq_count = phba->cfg_hdw_queue;
  3268. for (i = 0; i < hwq_count; i++) {
  3269. qp = &phba->sli4_hba.hdwq[i];
  3270. multixri_pool = qp->p_multixri_pool;
  3271. if (!multixri_pool)
  3272. continue;
  3273. qp->p_multixri_pool = NULL;
  3274. spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag);
  3275. /* Deal with public free xri pool */
  3276. pbl_pool = &multixri_pool->pbl_pool;
  3277. spin_lock(&pbl_pool->lock);
  3278. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3279. "1236 Moving %d buffers from pbl_pool[%d] TO PUT list\n",
  3280. pbl_pool->count, i);
  3281. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  3282. &pbl_pool->list, list) {
  3283. list_move_tail(&lpfc_ncmd->list,
  3284. &qp->lpfc_io_buf_list_put);
  3285. qp->put_io_bufs++;
  3286. pbl_pool->count--;
  3287. }
  3288. INIT_LIST_HEAD(&pbl_pool->list);
  3289. pbl_pool->count = 0;
  3290. spin_unlock(&pbl_pool->lock);
  3291. /* Deal with private free xri pool */
  3292. pvt_pool = &multixri_pool->pvt_pool;
  3293. spin_lock(&pvt_pool->lock);
  3294. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3295. "1237 Moving %d buffers from pvt_pool[%d] TO PUT list\n",
  3296. pvt_pool->count, i);
  3297. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  3298. &pvt_pool->list, list) {
  3299. list_move_tail(&lpfc_ncmd->list,
  3300. &qp->lpfc_io_buf_list_put);
  3301. qp->put_io_bufs++;
  3302. pvt_pool->count--;
  3303. }
  3304. INIT_LIST_HEAD(&pvt_pool->list);
  3305. pvt_pool->count = 0;
  3306. spin_unlock(&pvt_pool->lock);
  3307. spin_unlock_irqrestore(&qp->io_buf_list_put_lock, iflag);
  3308. kfree(multixri_pool);
  3309. }
  3310. }
  3311. /**
  3312. * lpfc_online - Initialize and bring a HBA online
  3313. * @phba: pointer to lpfc hba data structure.
  3314. *
  3315. * This routine initializes the HBA and brings a HBA online. During this
  3316. * process, the management interface is blocked to prevent user space access
  3317. * to the HBA interfering with the driver initialization.
  3318. *
  3319. * Return codes
  3320. * 0 - successful
  3321. * 1 - failed
  3322. **/
  3323. int
  3324. lpfc_online(struct lpfc_hba *phba)
  3325. {
  3326. struct lpfc_vport *vport;
  3327. struct lpfc_vport **vports;
  3328. int i, error = 0;
  3329. bool vpis_cleared = false;
  3330. if (!phba)
  3331. return 0;
  3332. vport = phba->pport;
  3333. if (!test_bit(FC_OFFLINE_MODE, &vport->fc_flag))
  3334. return 0;
  3335. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3336. "0458 Bring Adapter online\n");
  3337. lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
  3338. if (phba->sli_rev == LPFC_SLI_REV4) {
  3339. if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
  3340. lpfc_unblock_mgmt_io(phba);
  3341. return 1;
  3342. }
  3343. spin_lock_irq(&phba->hbalock);
  3344. if (!phba->sli4_hba.max_cfg_param.vpi_used)
  3345. vpis_cleared = true;
  3346. spin_unlock_irq(&phba->hbalock);
  3347. /* Reestablish the local initiator port.
  3348. * The offline process destroyed the previous lport.
  3349. */
  3350. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
  3351. !phba->nvmet_support) {
  3352. error = lpfc_nvme_create_localport(phba->pport);
  3353. if (error)
  3354. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  3355. "6132 NVME restore reg failed "
  3356. "on nvmei error x%x\n", error);
  3357. }
  3358. } else {
  3359. lpfc_sli_queue_init(phba);
  3360. if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
  3361. lpfc_unblock_mgmt_io(phba);
  3362. return 1;
  3363. }
  3364. }
  3365. vports = lpfc_create_vport_work_array(phba);
  3366. if (vports != NULL) {
  3367. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  3368. clear_bit(FC_OFFLINE_MODE, &vports[i]->fc_flag);
  3369. if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
  3370. set_bit(FC_VPORT_NEEDS_REG_VPI,
  3371. &vports[i]->fc_flag);
  3372. if (phba->sli_rev == LPFC_SLI_REV4) {
  3373. set_bit(FC_VPORT_NEEDS_INIT_VPI,
  3374. &vports[i]->fc_flag);
  3375. if ((vpis_cleared) &&
  3376. (vports[i]->port_type !=
  3377. LPFC_PHYSICAL_PORT))
  3378. vports[i]->vpi = 0;
  3379. }
  3380. }
  3381. }
  3382. lpfc_destroy_vport_work_array(phba, vports);
  3383. if (phba->cfg_xri_rebalancing)
  3384. lpfc_create_multixri_pools(phba);
  3385. lpfc_cpuhp_add(phba);
  3386. lpfc_unblock_mgmt_io(phba);
  3387. return 0;
  3388. }
  3389. /**
  3390. * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
  3391. * @phba: pointer to lpfc hba data structure.
  3392. *
  3393. * This routine marks a HBA's management interface as not blocked. Once the
  3394. * HBA's management interface is marked as not blocked, all the user space
  3395. * access to the HBA, whether they are from sysfs interface or libdfc
  3396. * interface will be allowed. The HBA is set to block the management interface
  3397. * when the driver prepares the HBA interface for online or offline and then
  3398. * set to unblock the management interface afterwards.
  3399. **/
  3400. void
  3401. lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
  3402. {
  3403. unsigned long iflag;
  3404. spin_lock_irqsave(&phba->hbalock, iflag);
  3405. phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
  3406. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3407. }
  3408. /**
  3409. * lpfc_offline_prep - Prepare a HBA to be brought offline
  3410. * @phba: pointer to lpfc hba data structure.
  3411. * @mbx_action: flag for mailbox shutdown action.
  3412. *
  3413. * This routine is invoked to prepare a HBA to be brought offline. It performs
  3414. * unregistration login to all the nodes on all vports and flushes the mailbox
  3415. * queue to make it ready to be brought offline.
  3416. **/
  3417. void
  3418. lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
  3419. {
  3420. struct lpfc_vport *vport = phba->pport;
  3421. struct lpfc_nodelist *ndlp, *next_ndlp;
  3422. struct lpfc_vport **vports;
  3423. struct Scsi_Host *shost;
  3424. int i;
  3425. int offline;
  3426. bool hba_pci_err;
  3427. if (test_bit(FC_OFFLINE_MODE, &vport->fc_flag))
  3428. return;
  3429. lpfc_block_mgmt_io(phba, mbx_action);
  3430. lpfc_linkdown(phba);
  3431. offline = pci_channel_offline(phba->pcidev);
  3432. hba_pci_err = test_bit(HBA_PCI_ERR, &phba->bit_flags);
  3433. /* Issue an unreg_login to all nodes on all vports */
  3434. vports = lpfc_create_vport_work_array(phba);
  3435. if (vports != NULL) {
  3436. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  3437. if (test_bit(FC_UNLOADING, &vports[i]->load_flag))
  3438. continue;
  3439. shost = lpfc_shost_from_vport(vports[i]);
  3440. spin_lock_irq(shost->host_lock);
  3441. vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
  3442. spin_unlock_irq(shost->host_lock);
  3443. set_bit(FC_VPORT_NEEDS_REG_VPI, &vports[i]->fc_flag);
  3444. clear_bit(FC_VFI_REGISTERED, &vports[i]->fc_flag);
  3445. list_for_each_entry_safe(ndlp, next_ndlp,
  3446. &vports[i]->fc_nodes,
  3447. nlp_listp) {
  3448. spin_lock_irq(&ndlp->lock);
  3449. ndlp->nlp_flag &= ~NLP_NPR_ADISC;
  3450. spin_unlock_irq(&ndlp->lock);
  3451. if (offline || hba_pci_err) {
  3452. spin_lock_irq(&ndlp->lock);
  3453. ndlp->nlp_flag &= ~(NLP_UNREG_INP |
  3454. NLP_RPI_REGISTERED);
  3455. spin_unlock_irq(&ndlp->lock);
  3456. if (phba->sli_rev == LPFC_SLI_REV4)
  3457. lpfc_sli_rpi_release(vports[i],
  3458. ndlp);
  3459. } else {
  3460. lpfc_unreg_rpi(vports[i], ndlp);
  3461. }
  3462. /*
  3463. * Whenever an SLI4 port goes offline, free the
  3464. * RPI. Get a new RPI when the adapter port
  3465. * comes back online.
  3466. */
  3467. if (phba->sli_rev == LPFC_SLI_REV4) {
  3468. lpfc_printf_vlog(vports[i], KERN_INFO,
  3469. LOG_NODE | LOG_DISCOVERY,
  3470. "0011 Free RPI x%x on "
  3471. "ndlp: x%px did x%x\n",
  3472. ndlp->nlp_rpi, ndlp,
  3473. ndlp->nlp_DID);
  3474. lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
  3475. ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
  3476. }
  3477. if (ndlp->nlp_type & NLP_FABRIC) {
  3478. lpfc_disc_state_machine(vports[i], ndlp,
  3479. NULL, NLP_EVT_DEVICE_RECOVERY);
  3480. /* Don't remove the node unless the node
  3481. * has been unregistered with the
  3482. * transport, and we're not in recovery
  3483. * before dev_loss_tmo triggered.
  3484. * Otherwise, let dev_loss take care of
  3485. * the node.
  3486. */
  3487. if (!(ndlp->save_flags &
  3488. NLP_IN_RECOV_POST_DEV_LOSS) &&
  3489. !(ndlp->fc4_xpt_flags &
  3490. (NVME_XPT_REGD | SCSI_XPT_REGD)))
  3491. lpfc_disc_state_machine
  3492. (vports[i], ndlp,
  3493. NULL,
  3494. NLP_EVT_DEVICE_RM);
  3495. }
  3496. }
  3497. }
  3498. }
  3499. lpfc_destroy_vport_work_array(phba, vports);
  3500. lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
  3501. if (phba->wq)
  3502. flush_workqueue(phba->wq);
  3503. }
  3504. /**
  3505. * lpfc_offline - Bring a HBA offline
  3506. * @phba: pointer to lpfc hba data structure.
  3507. *
  3508. * This routine actually brings a HBA offline. It stops all the timers
  3509. * associated with the HBA, brings down the SLI layer, and eventually
  3510. * marks the HBA as in offline state for the upper layer protocol.
  3511. **/
  3512. void
  3513. lpfc_offline(struct lpfc_hba *phba)
  3514. {
  3515. struct Scsi_Host *shost;
  3516. struct lpfc_vport **vports;
  3517. int i;
  3518. if (test_bit(FC_OFFLINE_MODE, &phba->pport->fc_flag))
  3519. return;
  3520. /* stop port and all timers associated with this hba */
  3521. lpfc_stop_port(phba);
  3522. /* Tear down the local and target port registrations. The
  3523. * nvme transports need to cleanup.
  3524. */
  3525. lpfc_nvmet_destroy_targetport(phba);
  3526. lpfc_nvme_destroy_localport(phba->pport);
  3527. vports = lpfc_create_vport_work_array(phba);
  3528. if (vports != NULL)
  3529. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
  3530. lpfc_stop_vport_timers(vports[i]);
  3531. lpfc_destroy_vport_work_array(phba, vports);
  3532. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3533. "0460 Bring Adapter offline\n");
  3534. /* Bring down the SLI Layer and cleanup. The HBA is offline
  3535. now. */
  3536. lpfc_sli_hba_down(phba);
  3537. spin_lock_irq(&phba->hbalock);
  3538. phba->work_ha = 0;
  3539. spin_unlock_irq(&phba->hbalock);
  3540. vports = lpfc_create_vport_work_array(phba);
  3541. if (vports != NULL)
  3542. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  3543. shost = lpfc_shost_from_vport(vports[i]);
  3544. spin_lock_irq(shost->host_lock);
  3545. vports[i]->work_port_events = 0;
  3546. spin_unlock_irq(shost->host_lock);
  3547. set_bit(FC_OFFLINE_MODE, &vports[i]->fc_flag);
  3548. }
  3549. lpfc_destroy_vport_work_array(phba, vports);
  3550. /* If OFFLINE flag is clear (i.e. unloading), cpuhp removal is handled
  3551. * in hba_unset
  3552. */
  3553. if (test_bit(FC_OFFLINE_MODE, &phba->pport->fc_flag))
  3554. __lpfc_cpuhp_remove(phba);
  3555. if (phba->cfg_xri_rebalancing)
  3556. lpfc_destroy_multixri_pools(phba);
  3557. }
  3558. /**
  3559. * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
  3560. * @phba: pointer to lpfc hba data structure.
  3561. *
  3562. * This routine is to free all the SCSI buffers and IOCBs from the driver
  3563. * list back to kernel. It is called from lpfc_pci_remove_one to free
  3564. * the internal resources before the device is removed from the system.
  3565. **/
  3566. static void
  3567. lpfc_scsi_free(struct lpfc_hba *phba)
  3568. {
  3569. struct lpfc_io_buf *sb, *sb_next;
  3570. if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
  3571. return;
  3572. spin_lock_irq(&phba->hbalock);
  3573. /* Release all the lpfc_scsi_bufs maintained by this host. */
  3574. spin_lock(&phba->scsi_buf_list_put_lock);
  3575. list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
  3576. list) {
  3577. list_del(&sb->list);
  3578. dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
  3579. sb->dma_handle);
  3580. kfree(sb);
  3581. phba->total_scsi_bufs--;
  3582. }
  3583. spin_unlock(&phba->scsi_buf_list_put_lock);
  3584. spin_lock(&phba->scsi_buf_list_get_lock);
  3585. list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
  3586. list) {
  3587. list_del(&sb->list);
  3588. dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
  3589. sb->dma_handle);
  3590. kfree(sb);
  3591. phba->total_scsi_bufs--;
  3592. }
  3593. spin_unlock(&phba->scsi_buf_list_get_lock);
  3594. spin_unlock_irq(&phba->hbalock);
  3595. }
  3596. /**
  3597. * lpfc_io_free - Free all the IO buffers and IOCBs from driver lists
  3598. * @phba: pointer to lpfc hba data structure.
  3599. *
  3600. * This routine is to free all the IO buffers and IOCBs from the driver
  3601. * list back to kernel. It is called from lpfc_pci_remove_one to free
  3602. * the internal resources before the device is removed from the system.
  3603. **/
  3604. void
  3605. lpfc_io_free(struct lpfc_hba *phba)
  3606. {
  3607. struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
  3608. struct lpfc_sli4_hdw_queue *qp;
  3609. int idx;
  3610. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  3611. qp = &phba->sli4_hba.hdwq[idx];
  3612. /* Release all the lpfc_nvme_bufs maintained by this host. */
  3613. spin_lock(&qp->io_buf_list_put_lock);
  3614. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  3615. &qp->lpfc_io_buf_list_put,
  3616. list) {
  3617. list_del(&lpfc_ncmd->list);
  3618. qp->put_io_bufs--;
  3619. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  3620. lpfc_ncmd->data, lpfc_ncmd->dma_handle);
  3621. if (phba->cfg_xpsgl && !phba->nvmet_support)
  3622. lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
  3623. lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
  3624. kfree(lpfc_ncmd);
  3625. qp->total_io_bufs--;
  3626. }
  3627. spin_unlock(&qp->io_buf_list_put_lock);
  3628. spin_lock(&qp->io_buf_list_get_lock);
  3629. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  3630. &qp->lpfc_io_buf_list_get,
  3631. list) {
  3632. list_del(&lpfc_ncmd->list);
  3633. qp->get_io_bufs--;
  3634. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  3635. lpfc_ncmd->data, lpfc_ncmd->dma_handle);
  3636. if (phba->cfg_xpsgl && !phba->nvmet_support)
  3637. lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
  3638. lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
  3639. kfree(lpfc_ncmd);
  3640. qp->total_io_bufs--;
  3641. }
  3642. spin_unlock(&qp->io_buf_list_get_lock);
  3643. }
  3644. }
  3645. /**
  3646. * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
  3647. * @phba: pointer to lpfc hba data structure.
  3648. *
  3649. * This routine first calculates the sizes of the current els and allocated
  3650. * scsi sgl lists, and then goes through all sgls to updates the physical
  3651. * XRIs assigned due to port function reset. During port initialization, the
  3652. * current els and allocated scsi sgl lists are 0s.
  3653. *
  3654. * Return codes
  3655. * 0 - successful (for now, it always returns 0)
  3656. **/
  3657. int
  3658. lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
  3659. {
  3660. struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
  3661. uint16_t i, lxri, xri_cnt, els_xri_cnt;
  3662. LIST_HEAD(els_sgl_list);
  3663. int rc;
  3664. /*
  3665. * update on pci function's els xri-sgl list
  3666. */
  3667. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  3668. if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
  3669. /* els xri-sgl expanded */
  3670. xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
  3671. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3672. "3157 ELS xri-sgl count increased from "
  3673. "%d to %d\n", phba->sli4_hba.els_xri_cnt,
  3674. els_xri_cnt);
  3675. /* allocate the additional els sgls */
  3676. for (i = 0; i < xri_cnt; i++) {
  3677. sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
  3678. GFP_KERNEL);
  3679. if (sglq_entry == NULL) {
  3680. lpfc_printf_log(phba, KERN_ERR,
  3681. LOG_TRACE_EVENT,
  3682. "2562 Failure to allocate an "
  3683. "ELS sgl entry:%d\n", i);
  3684. rc = -ENOMEM;
  3685. goto out_free_mem;
  3686. }
  3687. sglq_entry->buff_type = GEN_BUFF_TYPE;
  3688. sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
  3689. &sglq_entry->phys);
  3690. if (sglq_entry->virt == NULL) {
  3691. kfree(sglq_entry);
  3692. lpfc_printf_log(phba, KERN_ERR,
  3693. LOG_TRACE_EVENT,
  3694. "2563 Failure to allocate an "
  3695. "ELS mbuf:%d\n", i);
  3696. rc = -ENOMEM;
  3697. goto out_free_mem;
  3698. }
  3699. sglq_entry->sgl = sglq_entry->virt;
  3700. memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
  3701. sglq_entry->state = SGL_FREED;
  3702. list_add_tail(&sglq_entry->list, &els_sgl_list);
  3703. }
  3704. spin_lock_irq(&phba->sli4_hba.sgl_list_lock);
  3705. list_splice_init(&els_sgl_list,
  3706. &phba->sli4_hba.lpfc_els_sgl_list);
  3707. spin_unlock_irq(&phba->sli4_hba.sgl_list_lock);
  3708. } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
  3709. /* els xri-sgl shrinked */
  3710. xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
  3711. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3712. "3158 ELS xri-sgl count decreased from "
  3713. "%d to %d\n", phba->sli4_hba.els_xri_cnt,
  3714. els_xri_cnt);
  3715. spin_lock_irq(&phba->sli4_hba.sgl_list_lock);
  3716. list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
  3717. &els_sgl_list);
  3718. /* release extra els sgls from list */
  3719. for (i = 0; i < xri_cnt; i++) {
  3720. list_remove_head(&els_sgl_list,
  3721. sglq_entry, struct lpfc_sglq, list);
  3722. if (sglq_entry) {
  3723. __lpfc_mbuf_free(phba, sglq_entry->virt,
  3724. sglq_entry->phys);
  3725. kfree(sglq_entry);
  3726. }
  3727. }
  3728. list_splice_init(&els_sgl_list,
  3729. &phba->sli4_hba.lpfc_els_sgl_list);
  3730. spin_unlock_irq(&phba->sli4_hba.sgl_list_lock);
  3731. } else
  3732. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3733. "3163 ELS xri-sgl count unchanged: %d\n",
  3734. els_xri_cnt);
  3735. phba->sli4_hba.els_xri_cnt = els_xri_cnt;
  3736. /* update xris to els sgls on the list */
  3737. sglq_entry = NULL;
  3738. sglq_entry_next = NULL;
  3739. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  3740. &phba->sli4_hba.lpfc_els_sgl_list, list) {
  3741. lxri = lpfc_sli4_next_xritag(phba);
  3742. if (lxri == NO_XRI) {
  3743. lpfc_printf_log(phba, KERN_ERR,
  3744. LOG_TRACE_EVENT,
  3745. "2400 Failed to allocate xri for "
  3746. "ELS sgl\n");
  3747. rc = -ENOMEM;
  3748. goto out_free_mem;
  3749. }
  3750. sglq_entry->sli4_lxritag = lxri;
  3751. sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  3752. }
  3753. return 0;
  3754. out_free_mem:
  3755. lpfc_free_els_sgl_list(phba);
  3756. return rc;
  3757. }
  3758. /**
  3759. * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
  3760. * @phba: pointer to lpfc hba data structure.
  3761. *
  3762. * This routine first calculates the sizes of the current els and allocated
  3763. * scsi sgl lists, and then goes through all sgls to updates the physical
  3764. * XRIs assigned due to port function reset. During port initialization, the
  3765. * current els and allocated scsi sgl lists are 0s.
  3766. *
  3767. * Return codes
  3768. * 0 - successful (for now, it always returns 0)
  3769. **/
  3770. int
  3771. lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
  3772. {
  3773. struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
  3774. uint16_t i, lxri, xri_cnt, els_xri_cnt;
  3775. uint16_t nvmet_xri_cnt;
  3776. LIST_HEAD(nvmet_sgl_list);
  3777. int rc;
  3778. /*
  3779. * update on pci function's nvmet xri-sgl list
  3780. */
  3781. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  3782. /* For NVMET, ALL remaining XRIs are dedicated for IO processing */
  3783. nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
  3784. if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
  3785. /* els xri-sgl expanded */
  3786. xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
  3787. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3788. "6302 NVMET xri-sgl cnt grew from %d to %d\n",
  3789. phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
  3790. /* allocate the additional nvmet sgls */
  3791. for (i = 0; i < xri_cnt; i++) {
  3792. sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
  3793. GFP_KERNEL);
  3794. if (sglq_entry == NULL) {
  3795. lpfc_printf_log(phba, KERN_ERR,
  3796. LOG_TRACE_EVENT,
  3797. "6303 Failure to allocate an "
  3798. "NVMET sgl entry:%d\n", i);
  3799. rc = -ENOMEM;
  3800. goto out_free_mem;
  3801. }
  3802. sglq_entry->buff_type = NVMET_BUFF_TYPE;
  3803. sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
  3804. &sglq_entry->phys);
  3805. if (sglq_entry->virt == NULL) {
  3806. kfree(sglq_entry);
  3807. lpfc_printf_log(phba, KERN_ERR,
  3808. LOG_TRACE_EVENT,
  3809. "6304 Failure to allocate an "
  3810. "NVMET buf:%d\n", i);
  3811. rc = -ENOMEM;
  3812. goto out_free_mem;
  3813. }
  3814. sglq_entry->sgl = sglq_entry->virt;
  3815. memset(sglq_entry->sgl, 0,
  3816. phba->cfg_sg_dma_buf_size);
  3817. sglq_entry->state = SGL_FREED;
  3818. list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
  3819. }
  3820. spin_lock_irq(&phba->hbalock);
  3821. spin_lock(&phba->sli4_hba.sgl_list_lock);
  3822. list_splice_init(&nvmet_sgl_list,
  3823. &phba->sli4_hba.lpfc_nvmet_sgl_list);
  3824. spin_unlock(&phba->sli4_hba.sgl_list_lock);
  3825. spin_unlock_irq(&phba->hbalock);
  3826. } else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
  3827. /* nvmet xri-sgl shrunk */
  3828. xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
  3829. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3830. "6305 NVMET xri-sgl count decreased from "
  3831. "%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
  3832. nvmet_xri_cnt);
  3833. spin_lock_irq(&phba->hbalock);
  3834. spin_lock(&phba->sli4_hba.sgl_list_lock);
  3835. list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
  3836. &nvmet_sgl_list);
  3837. /* release extra nvmet sgls from list */
  3838. for (i = 0; i < xri_cnt; i++) {
  3839. list_remove_head(&nvmet_sgl_list,
  3840. sglq_entry, struct lpfc_sglq, list);
  3841. if (sglq_entry) {
  3842. lpfc_nvmet_buf_free(phba, sglq_entry->virt,
  3843. sglq_entry->phys);
  3844. kfree(sglq_entry);
  3845. }
  3846. }
  3847. list_splice_init(&nvmet_sgl_list,
  3848. &phba->sli4_hba.lpfc_nvmet_sgl_list);
  3849. spin_unlock(&phba->sli4_hba.sgl_list_lock);
  3850. spin_unlock_irq(&phba->hbalock);
  3851. } else
  3852. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3853. "6306 NVMET xri-sgl count unchanged: %d\n",
  3854. nvmet_xri_cnt);
  3855. phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
  3856. /* update xris to nvmet sgls on the list */
  3857. sglq_entry = NULL;
  3858. sglq_entry_next = NULL;
  3859. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  3860. &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
  3861. lxri = lpfc_sli4_next_xritag(phba);
  3862. if (lxri == NO_XRI) {
  3863. lpfc_printf_log(phba, KERN_ERR,
  3864. LOG_TRACE_EVENT,
  3865. "6307 Failed to allocate xri for "
  3866. "NVMET sgl\n");
  3867. rc = -ENOMEM;
  3868. goto out_free_mem;
  3869. }
  3870. sglq_entry->sli4_lxritag = lxri;
  3871. sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  3872. }
  3873. return 0;
  3874. out_free_mem:
  3875. lpfc_free_nvmet_sgl_list(phba);
  3876. return rc;
  3877. }
  3878. int
  3879. lpfc_io_buf_flush(struct lpfc_hba *phba, struct list_head *cbuf)
  3880. {
  3881. LIST_HEAD(blist);
  3882. struct lpfc_sli4_hdw_queue *qp;
  3883. struct lpfc_io_buf *lpfc_cmd;
  3884. struct lpfc_io_buf *iobufp, *prev_iobufp;
  3885. int idx, cnt, xri, inserted;
  3886. cnt = 0;
  3887. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  3888. qp = &phba->sli4_hba.hdwq[idx];
  3889. spin_lock_irq(&qp->io_buf_list_get_lock);
  3890. spin_lock(&qp->io_buf_list_put_lock);
  3891. /* Take everything off the get and put lists */
  3892. list_splice_init(&qp->lpfc_io_buf_list_get, &blist);
  3893. list_splice(&qp->lpfc_io_buf_list_put, &blist);
  3894. INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
  3895. INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
  3896. cnt += qp->get_io_bufs + qp->put_io_bufs;
  3897. qp->get_io_bufs = 0;
  3898. qp->put_io_bufs = 0;
  3899. qp->total_io_bufs = 0;
  3900. spin_unlock(&qp->io_buf_list_put_lock);
  3901. spin_unlock_irq(&qp->io_buf_list_get_lock);
  3902. }
  3903. /*
  3904. * Take IO buffers off blist and put on cbuf sorted by XRI.
  3905. * This is because POST_SGL takes a sequential range of XRIs
  3906. * to post to the firmware.
  3907. */
  3908. for (idx = 0; idx < cnt; idx++) {
  3909. list_remove_head(&blist, lpfc_cmd, struct lpfc_io_buf, list);
  3910. if (!lpfc_cmd)
  3911. return cnt;
  3912. if (idx == 0) {
  3913. list_add_tail(&lpfc_cmd->list, cbuf);
  3914. continue;
  3915. }
  3916. xri = lpfc_cmd->cur_iocbq.sli4_xritag;
  3917. inserted = 0;
  3918. prev_iobufp = NULL;
  3919. list_for_each_entry(iobufp, cbuf, list) {
  3920. if (xri < iobufp->cur_iocbq.sli4_xritag) {
  3921. if (prev_iobufp)
  3922. list_add(&lpfc_cmd->list,
  3923. &prev_iobufp->list);
  3924. else
  3925. list_add(&lpfc_cmd->list, cbuf);
  3926. inserted = 1;
  3927. break;
  3928. }
  3929. prev_iobufp = iobufp;
  3930. }
  3931. if (!inserted)
  3932. list_add_tail(&lpfc_cmd->list, cbuf);
  3933. }
  3934. return cnt;
  3935. }
  3936. int
  3937. lpfc_io_buf_replenish(struct lpfc_hba *phba, struct list_head *cbuf)
  3938. {
  3939. struct lpfc_sli4_hdw_queue *qp;
  3940. struct lpfc_io_buf *lpfc_cmd;
  3941. int idx, cnt;
  3942. unsigned long iflags;
  3943. qp = phba->sli4_hba.hdwq;
  3944. cnt = 0;
  3945. while (!list_empty(cbuf)) {
  3946. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  3947. list_remove_head(cbuf, lpfc_cmd,
  3948. struct lpfc_io_buf, list);
  3949. if (!lpfc_cmd)
  3950. return cnt;
  3951. cnt++;
  3952. qp = &phba->sli4_hba.hdwq[idx];
  3953. lpfc_cmd->hdwq_no = idx;
  3954. lpfc_cmd->hdwq = qp;
  3955. lpfc_cmd->cur_iocbq.cmd_cmpl = NULL;
  3956. spin_lock_irqsave(&qp->io_buf_list_put_lock, iflags);
  3957. list_add_tail(&lpfc_cmd->list,
  3958. &qp->lpfc_io_buf_list_put);
  3959. qp->put_io_bufs++;
  3960. qp->total_io_bufs++;
  3961. spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
  3962. iflags);
  3963. }
  3964. }
  3965. return cnt;
  3966. }
  3967. /**
  3968. * lpfc_sli4_io_sgl_update - update xri-sgl sizing and mapping
  3969. * @phba: pointer to lpfc hba data structure.
  3970. *
  3971. * This routine first calculates the sizes of the current els and allocated
  3972. * scsi sgl lists, and then goes through all sgls to updates the physical
  3973. * XRIs assigned due to port function reset. During port initialization, the
  3974. * current els and allocated scsi sgl lists are 0s.
  3975. *
  3976. * Return codes
  3977. * 0 - successful (for now, it always returns 0)
  3978. **/
  3979. int
  3980. lpfc_sli4_io_sgl_update(struct lpfc_hba *phba)
  3981. {
  3982. struct lpfc_io_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
  3983. uint16_t i, lxri, els_xri_cnt;
  3984. uint16_t io_xri_cnt, io_xri_max;
  3985. LIST_HEAD(io_sgl_list);
  3986. int rc, cnt;
  3987. /*
  3988. * update on pci function's allocated nvme xri-sgl list
  3989. */
  3990. /* maximum number of xris available for nvme buffers */
  3991. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  3992. io_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
  3993. phba->sli4_hba.io_xri_max = io_xri_max;
  3994. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3995. "6074 Current allocated XRI sgl count:%d, "
  3996. "maximum XRI count:%d els_xri_cnt:%d\n\n",
  3997. phba->sli4_hba.io_xri_cnt,
  3998. phba->sli4_hba.io_xri_max,
  3999. els_xri_cnt);
  4000. cnt = lpfc_io_buf_flush(phba, &io_sgl_list);
  4001. if (phba->sli4_hba.io_xri_cnt > phba->sli4_hba.io_xri_max) {
  4002. /* max nvme xri shrunk below the allocated nvme buffers */
  4003. io_xri_cnt = phba->sli4_hba.io_xri_cnt -
  4004. phba->sli4_hba.io_xri_max;
  4005. /* release the extra allocated nvme buffers */
  4006. for (i = 0; i < io_xri_cnt; i++) {
  4007. list_remove_head(&io_sgl_list, lpfc_ncmd,
  4008. struct lpfc_io_buf, list);
  4009. if (lpfc_ncmd) {
  4010. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  4011. lpfc_ncmd->data,
  4012. lpfc_ncmd->dma_handle);
  4013. kfree(lpfc_ncmd);
  4014. }
  4015. }
  4016. phba->sli4_hba.io_xri_cnt -= io_xri_cnt;
  4017. }
  4018. /* update xris associated to remaining allocated nvme buffers */
  4019. lpfc_ncmd = NULL;
  4020. lpfc_ncmd_next = NULL;
  4021. phba->sli4_hba.io_xri_cnt = cnt;
  4022. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  4023. &io_sgl_list, list) {
  4024. lxri = lpfc_sli4_next_xritag(phba);
  4025. if (lxri == NO_XRI) {
  4026. lpfc_printf_log(phba, KERN_ERR,
  4027. LOG_TRACE_EVENT,
  4028. "6075 Failed to allocate xri for "
  4029. "nvme buffer\n");
  4030. rc = -ENOMEM;
  4031. goto out_free_mem;
  4032. }
  4033. lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
  4034. lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  4035. }
  4036. cnt = lpfc_io_buf_replenish(phba, &io_sgl_list);
  4037. return 0;
  4038. out_free_mem:
  4039. lpfc_io_free(phba);
  4040. return rc;
  4041. }
  4042. /**
  4043. * lpfc_new_io_buf - IO buffer allocator for HBA with SLI4 IF spec
  4044. * @phba: Pointer to lpfc hba data structure.
  4045. * @num_to_alloc: The requested number of buffers to allocate.
  4046. *
  4047. * This routine allocates nvme buffers for device with SLI-4 interface spec,
  4048. * the nvme buffer contains all the necessary information needed to initiate
  4049. * an I/O. After allocating up to @num_to_allocate IO buffers and put
  4050. * them on a list, it post them to the port by using SGL block post.
  4051. *
  4052. * Return codes:
  4053. * int - number of IO buffers that were allocated and posted.
  4054. * 0 = failure, less than num_to_alloc is a partial failure.
  4055. **/
  4056. int
  4057. lpfc_new_io_buf(struct lpfc_hba *phba, int num_to_alloc)
  4058. {
  4059. struct lpfc_io_buf *lpfc_ncmd;
  4060. struct lpfc_iocbq *pwqeq;
  4061. uint16_t iotag, lxri = 0;
  4062. int bcnt, num_posted;
  4063. LIST_HEAD(prep_nblist);
  4064. LIST_HEAD(post_nblist);
  4065. LIST_HEAD(nvme_nblist);
  4066. phba->sli4_hba.io_xri_cnt = 0;
  4067. for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
  4068. lpfc_ncmd = kzalloc(sizeof(*lpfc_ncmd), GFP_KERNEL);
  4069. if (!lpfc_ncmd)
  4070. break;
  4071. /*
  4072. * Get memory from the pci pool to map the virt space to
  4073. * pci bus space for an I/O. The DMA buffer includes the
  4074. * number of SGE's necessary to support the sg_tablesize.
  4075. */
  4076. lpfc_ncmd->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
  4077. GFP_KERNEL,
  4078. &lpfc_ncmd->dma_handle);
  4079. if (!lpfc_ncmd->data) {
  4080. kfree(lpfc_ncmd);
  4081. break;
  4082. }
  4083. if (phba->cfg_xpsgl && !phba->nvmet_support) {
  4084. INIT_LIST_HEAD(&lpfc_ncmd->dma_sgl_xtra_list);
  4085. } else {
  4086. /*
  4087. * 4K Page alignment is CRITICAL to BlockGuard, double
  4088. * check to be sure.
  4089. */
  4090. if ((phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  4091. (((unsigned long)(lpfc_ncmd->data) &
  4092. (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
  4093. lpfc_printf_log(phba, KERN_ERR,
  4094. LOG_TRACE_EVENT,
  4095. "3369 Memory alignment err: "
  4096. "addr=%lx\n",
  4097. (unsigned long)lpfc_ncmd->data);
  4098. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  4099. lpfc_ncmd->data,
  4100. lpfc_ncmd->dma_handle);
  4101. kfree(lpfc_ncmd);
  4102. break;
  4103. }
  4104. }
  4105. INIT_LIST_HEAD(&lpfc_ncmd->dma_cmd_rsp_list);
  4106. lxri = lpfc_sli4_next_xritag(phba);
  4107. if (lxri == NO_XRI) {
  4108. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  4109. lpfc_ncmd->data, lpfc_ncmd->dma_handle);
  4110. kfree(lpfc_ncmd);
  4111. break;
  4112. }
  4113. pwqeq = &lpfc_ncmd->cur_iocbq;
  4114. /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
  4115. iotag = lpfc_sli_next_iotag(phba, pwqeq);
  4116. if (iotag == 0) {
  4117. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  4118. lpfc_ncmd->data, lpfc_ncmd->dma_handle);
  4119. kfree(lpfc_ncmd);
  4120. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  4121. "6121 Failed to allocate IOTAG for"
  4122. " XRI:0x%x\n", lxri);
  4123. lpfc_sli4_free_xri(phba, lxri);
  4124. break;
  4125. }
  4126. pwqeq->sli4_lxritag = lxri;
  4127. pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  4128. /* Initialize local short-hand pointers. */
  4129. lpfc_ncmd->dma_sgl = lpfc_ncmd->data;
  4130. lpfc_ncmd->dma_phys_sgl = lpfc_ncmd->dma_handle;
  4131. lpfc_ncmd->cur_iocbq.io_buf = lpfc_ncmd;
  4132. spin_lock_init(&lpfc_ncmd->buf_lock);
  4133. /* add the nvme buffer to a post list */
  4134. list_add_tail(&lpfc_ncmd->list, &post_nblist);
  4135. phba->sli4_hba.io_xri_cnt++;
  4136. }
  4137. lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
  4138. "6114 Allocate %d out of %d requested new NVME "
  4139. "buffers of size x%zu bytes\n", bcnt, num_to_alloc,
  4140. sizeof(*lpfc_ncmd));
  4141. /* post the list of nvme buffer sgls to port if available */
  4142. if (!list_empty(&post_nblist))
  4143. num_posted = lpfc_sli4_post_io_sgl_list(
  4144. phba, &post_nblist, bcnt);
  4145. else
  4146. num_posted = 0;
  4147. return num_posted;
  4148. }
  4149. static uint64_t
  4150. lpfc_get_wwpn(struct lpfc_hba *phba)
  4151. {
  4152. uint64_t wwn;
  4153. int rc;
  4154. LPFC_MBOXQ_t *mboxq;
  4155. MAILBOX_t *mb;
  4156. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
  4157. GFP_KERNEL);
  4158. if (!mboxq)
  4159. return (uint64_t)-1;
  4160. /* First get WWN of HBA instance */
  4161. lpfc_read_nv(phba, mboxq);
  4162. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4163. if (rc != MBX_SUCCESS) {
  4164. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  4165. "6019 Mailbox failed , mbxCmd x%x "
  4166. "READ_NV, mbxStatus x%x\n",
  4167. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4168. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  4169. mempool_free(mboxq, phba->mbox_mem_pool);
  4170. return (uint64_t) -1;
  4171. }
  4172. mb = &mboxq->u.mb;
  4173. memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
  4174. /* wwn is WWPN of HBA instance */
  4175. mempool_free(mboxq, phba->mbox_mem_pool);
  4176. if (phba->sli_rev == LPFC_SLI_REV4)
  4177. return be64_to_cpu(wwn);
  4178. else
  4179. return rol64(wwn, 32);
  4180. }
  4181. static unsigned short lpfc_get_sg_tablesize(struct lpfc_hba *phba)
  4182. {
  4183. if (phba->sli_rev == LPFC_SLI_REV4)
  4184. if (phba->cfg_xpsgl && !phba->nvmet_support)
  4185. return LPFC_MAX_SG_TABLESIZE;
  4186. else
  4187. return phba->cfg_scsi_seg_cnt;
  4188. else
  4189. return phba->cfg_sg_seg_cnt;
  4190. }
  4191. /**
  4192. * lpfc_vmid_res_alloc - Allocates resources for VMID
  4193. * @phba: pointer to lpfc hba data structure.
  4194. * @vport: pointer to vport data structure
  4195. *
  4196. * This routine allocated the resources needed for the VMID.
  4197. *
  4198. * Return codes
  4199. * 0 on Success
  4200. * Non-0 on Failure
  4201. */
  4202. static int
  4203. lpfc_vmid_res_alloc(struct lpfc_hba *phba, struct lpfc_vport *vport)
  4204. {
  4205. /* VMID feature is supported only on SLI4 */
  4206. if (phba->sli_rev == LPFC_SLI_REV3) {
  4207. phba->cfg_vmid_app_header = 0;
  4208. phba->cfg_vmid_priority_tagging = 0;
  4209. }
  4210. if (lpfc_is_vmid_enabled(phba)) {
  4211. vport->vmid =
  4212. kcalloc(phba->cfg_max_vmid, sizeof(struct lpfc_vmid),
  4213. GFP_KERNEL);
  4214. if (!vport->vmid)
  4215. return -ENOMEM;
  4216. rwlock_init(&vport->vmid_lock);
  4217. /* Set the VMID parameters for the vport */
  4218. vport->vmid_priority_tagging = phba->cfg_vmid_priority_tagging;
  4219. vport->vmid_inactivity_timeout =
  4220. phba->cfg_vmid_inactivity_timeout;
  4221. vport->max_vmid = phba->cfg_max_vmid;
  4222. vport->cur_vmid_cnt = 0;
  4223. vport->vmid_priority_range = bitmap_zalloc
  4224. (LPFC_VMID_MAX_PRIORITY_RANGE, GFP_KERNEL);
  4225. if (!vport->vmid_priority_range) {
  4226. kfree(vport->vmid);
  4227. return -ENOMEM;
  4228. }
  4229. hash_init(vport->hash_table);
  4230. }
  4231. return 0;
  4232. }
  4233. /**
  4234. * lpfc_create_port - Create an FC port
  4235. * @phba: pointer to lpfc hba data structure.
  4236. * @instance: a unique integer ID to this FC port.
  4237. * @dev: pointer to the device data structure.
  4238. *
  4239. * This routine creates a FC port for the upper layer protocol. The FC port
  4240. * can be created on top of either a physical port or a virtual port provided
  4241. * by the HBA. This routine also allocates a SCSI host data structure (shost)
  4242. * and associates the FC port created before adding the shost into the SCSI
  4243. * layer.
  4244. *
  4245. * Return codes
  4246. * @vport - pointer to the virtual N_Port data structure.
  4247. * NULL - port create failed.
  4248. **/
  4249. struct lpfc_vport *
  4250. lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
  4251. {
  4252. struct lpfc_vport *vport;
  4253. struct Scsi_Host *shost = NULL;
  4254. struct scsi_host_template *template;
  4255. int error = 0;
  4256. int i;
  4257. uint64_t wwn;
  4258. bool use_no_reset_hba = false;
  4259. int rc;
  4260. u8 if_type;
  4261. if (lpfc_no_hba_reset_cnt) {
  4262. if (phba->sli_rev < LPFC_SLI_REV4 &&
  4263. dev == &phba->pcidev->dev) {
  4264. /* Reset the port first */
  4265. lpfc_sli_brdrestart(phba);
  4266. rc = lpfc_sli_chipset_init(phba);
  4267. if (rc)
  4268. return NULL;
  4269. }
  4270. wwn = lpfc_get_wwpn(phba);
  4271. }
  4272. for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
  4273. if (wwn == lpfc_no_hba_reset[i]) {
  4274. lpfc_printf_log(phba, KERN_ERR,
  4275. LOG_TRACE_EVENT,
  4276. "6020 Setting use_no_reset port=%llx\n",
  4277. wwn);
  4278. use_no_reset_hba = true;
  4279. break;
  4280. }
  4281. }
  4282. /* Seed template for SCSI host registration */
  4283. if (dev == &phba->pcidev->dev) {
  4284. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
  4285. /* Seed physical port template */
  4286. template = &lpfc_template;
  4287. if (use_no_reset_hba)
  4288. /* template is for a no reset SCSI Host */
  4289. template->eh_host_reset_handler = NULL;
  4290. /* Seed updated value of sg_tablesize */
  4291. template->sg_tablesize = lpfc_get_sg_tablesize(phba);
  4292. } else {
  4293. /* NVMET is for physical port only */
  4294. template = &lpfc_template_nvme;
  4295. }
  4296. } else {
  4297. /* Seed vport template */
  4298. template = &lpfc_vport_template;
  4299. /* Seed updated value of sg_tablesize */
  4300. template->sg_tablesize = lpfc_get_sg_tablesize(phba);
  4301. }
  4302. shost = scsi_host_alloc(template, sizeof(struct lpfc_vport));
  4303. if (!shost)
  4304. goto out;
  4305. vport = (struct lpfc_vport *) shost->hostdata;
  4306. vport->phba = phba;
  4307. set_bit(FC_LOADING, &vport->load_flag);
  4308. set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
  4309. vport->fc_rscn_flush = 0;
  4310. atomic_set(&vport->fc_plogi_cnt, 0);
  4311. atomic_set(&vport->fc_adisc_cnt, 0);
  4312. atomic_set(&vport->fc_reglogin_cnt, 0);
  4313. atomic_set(&vport->fc_prli_cnt, 0);
  4314. atomic_set(&vport->fc_unmap_cnt, 0);
  4315. atomic_set(&vport->fc_map_cnt, 0);
  4316. atomic_set(&vport->fc_npr_cnt, 0);
  4317. atomic_set(&vport->fc_unused_cnt, 0);
  4318. lpfc_get_vport_cfgparam(vport);
  4319. /* Adjust value in vport */
  4320. vport->cfg_enable_fc4_type = phba->cfg_enable_fc4_type;
  4321. shost->unique_id = instance;
  4322. shost->max_id = LPFC_MAX_TARGET;
  4323. shost->max_lun = vport->cfg_max_luns;
  4324. shost->this_id = -1;
  4325. /* Set max_cmd_len applicable to ASIC support */
  4326. if (phba->sli_rev == LPFC_SLI_REV4) {
  4327. if_type = bf_get(lpfc_sli_intf_if_type,
  4328. &phba->sli4_hba.sli_intf);
  4329. switch (if_type) {
  4330. case LPFC_SLI_INTF_IF_TYPE_2:
  4331. fallthrough;
  4332. case LPFC_SLI_INTF_IF_TYPE_6:
  4333. shost->max_cmd_len = LPFC_FCP_CDB_LEN_32;
  4334. break;
  4335. default:
  4336. shost->max_cmd_len = LPFC_FCP_CDB_LEN;
  4337. break;
  4338. }
  4339. } else {
  4340. shost->max_cmd_len = LPFC_FCP_CDB_LEN;
  4341. }
  4342. if (phba->sli_rev == LPFC_SLI_REV4) {
  4343. if (!phba->cfg_fcp_mq_threshold ||
  4344. phba->cfg_fcp_mq_threshold > phba->cfg_hdw_queue)
  4345. phba->cfg_fcp_mq_threshold = phba->cfg_hdw_queue;
  4346. shost->nr_hw_queues = min_t(int, 2 * num_possible_nodes(),
  4347. phba->cfg_fcp_mq_threshold);
  4348. shost->dma_boundary =
  4349. phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
  4350. } else
  4351. /* SLI-3 has a limited number of hardware queues (3),
  4352. * thus there is only one for FCP processing.
  4353. */
  4354. shost->nr_hw_queues = 1;
  4355. /*
  4356. * Set initial can_queue value since 0 is no longer supported and
  4357. * scsi_add_host will fail. This will be adjusted later based on the
  4358. * max xri value determined in hba setup.
  4359. */
  4360. shost->can_queue = phba->cfg_hba_queue_depth - 10;
  4361. if (dev != &phba->pcidev->dev) {
  4362. shost->transportt = lpfc_vport_transport_template;
  4363. vport->port_type = LPFC_NPIV_PORT;
  4364. } else {
  4365. shost->transportt = lpfc_transport_template;
  4366. vport->port_type = LPFC_PHYSICAL_PORT;
  4367. }
  4368. lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
  4369. "9081 CreatePort TMPLATE type %x TBLsize %d "
  4370. "SEGcnt %d/%d\n",
  4371. vport->port_type, shost->sg_tablesize,
  4372. phba->cfg_scsi_seg_cnt, phba->cfg_sg_seg_cnt);
  4373. /* Allocate the resources for VMID */
  4374. rc = lpfc_vmid_res_alloc(phba, vport);
  4375. if (rc)
  4376. goto out_put_shost;
  4377. /* Initialize all internally managed lists. */
  4378. INIT_LIST_HEAD(&vport->fc_nodes);
  4379. spin_lock_init(&vport->fc_nodes_list_lock);
  4380. INIT_LIST_HEAD(&vport->rcv_buffer_list);
  4381. spin_lock_init(&vport->work_port_lock);
  4382. timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
  4383. timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
  4384. timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
  4385. if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
  4386. lpfc_setup_bg(phba, shost);
  4387. error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
  4388. if (error)
  4389. goto out_free_vmid;
  4390. spin_lock_irq(&phba->port_list_lock);
  4391. list_add_tail(&vport->listentry, &phba->port_list);
  4392. spin_unlock_irq(&phba->port_list_lock);
  4393. return vport;
  4394. out_free_vmid:
  4395. kfree(vport->vmid);
  4396. bitmap_free(vport->vmid_priority_range);
  4397. out_put_shost:
  4398. scsi_host_put(shost);
  4399. out:
  4400. return NULL;
  4401. }
  4402. /**
  4403. * destroy_port - destroy an FC port
  4404. * @vport: pointer to an lpfc virtual N_Port data structure.
  4405. *
  4406. * This routine destroys a FC port from the upper layer protocol. All the
  4407. * resources associated with the port are released.
  4408. **/
  4409. void
  4410. destroy_port(struct lpfc_vport *vport)
  4411. {
  4412. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  4413. struct lpfc_hba *phba = vport->phba;
  4414. lpfc_debugfs_terminate(vport);
  4415. fc_remove_host(shost);
  4416. scsi_remove_host(shost);
  4417. spin_lock_irq(&phba->port_list_lock);
  4418. list_del_init(&vport->listentry);
  4419. spin_unlock_irq(&phba->port_list_lock);
  4420. lpfc_cleanup(vport);
  4421. return;
  4422. }
  4423. /**
  4424. * lpfc_get_instance - Get a unique integer ID
  4425. *
  4426. * This routine allocates a unique integer ID from lpfc_hba_index pool. It
  4427. * uses the kernel idr facility to perform the task.
  4428. *
  4429. * Return codes:
  4430. * instance - a unique integer ID allocated as the new instance.
  4431. * -1 - lpfc get instance failed.
  4432. **/
  4433. int
  4434. lpfc_get_instance(void)
  4435. {
  4436. int ret;
  4437. ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
  4438. return ret < 0 ? -1 : ret;
  4439. }
  4440. /**
  4441. * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
  4442. * @shost: pointer to SCSI host data structure.
  4443. * @time: elapsed time of the scan in jiffies.
  4444. *
  4445. * This routine is called by the SCSI layer with a SCSI host to determine
  4446. * whether the scan host is finished.
  4447. *
  4448. * Note: there is no scan_start function as adapter initialization will have
  4449. * asynchronously kicked off the link initialization.
  4450. *
  4451. * Return codes
  4452. * 0 - SCSI host scan is not over yet.
  4453. * 1 - SCSI host scan is over.
  4454. **/
  4455. int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
  4456. {
  4457. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  4458. struct lpfc_hba *phba = vport->phba;
  4459. int stat = 0;
  4460. spin_lock_irq(shost->host_lock);
  4461. if (test_bit(FC_UNLOADING, &vport->load_flag)) {
  4462. stat = 1;
  4463. goto finished;
  4464. }
  4465. if (time >= msecs_to_jiffies(30 * 1000)) {
  4466. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4467. "0461 Scanning longer than 30 "
  4468. "seconds. Continuing initialization\n");
  4469. stat = 1;
  4470. goto finished;
  4471. }
  4472. if (time >= msecs_to_jiffies(15 * 1000) &&
  4473. phba->link_state <= LPFC_LINK_DOWN) {
  4474. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4475. "0465 Link down longer than 15 "
  4476. "seconds. Continuing initialization\n");
  4477. stat = 1;
  4478. goto finished;
  4479. }
  4480. if (vport->port_state != LPFC_VPORT_READY)
  4481. goto finished;
  4482. if (vport->num_disc_nodes || vport->fc_prli_sent)
  4483. goto finished;
  4484. if (!atomic_read(&vport->fc_map_cnt) &&
  4485. time < msecs_to_jiffies(2 * 1000))
  4486. goto finished;
  4487. if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
  4488. goto finished;
  4489. stat = 1;
  4490. finished:
  4491. spin_unlock_irq(shost->host_lock);
  4492. return stat;
  4493. }
  4494. static void lpfc_host_supported_speeds_set(struct Scsi_Host *shost)
  4495. {
  4496. struct lpfc_vport *vport = (struct lpfc_vport *)shost->hostdata;
  4497. struct lpfc_hba *phba = vport->phba;
  4498. fc_host_supported_speeds(shost) = 0;
  4499. /*
  4500. * Avoid reporting supported link speed for FCoE as it can't be
  4501. * controlled via FCoE.
  4502. */
  4503. if (test_bit(HBA_FCOE_MODE, &phba->hba_flag))
  4504. return;
  4505. if (phba->lmt & LMT_256Gb)
  4506. fc_host_supported_speeds(shost) |= FC_PORTSPEED_256GBIT;
  4507. if (phba->lmt & LMT_128Gb)
  4508. fc_host_supported_speeds(shost) |= FC_PORTSPEED_128GBIT;
  4509. if (phba->lmt & LMT_64Gb)
  4510. fc_host_supported_speeds(shost) |= FC_PORTSPEED_64GBIT;
  4511. if (phba->lmt & LMT_32Gb)
  4512. fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
  4513. if (phba->lmt & LMT_16Gb)
  4514. fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
  4515. if (phba->lmt & LMT_10Gb)
  4516. fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
  4517. if (phba->lmt & LMT_8Gb)
  4518. fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
  4519. if (phba->lmt & LMT_4Gb)
  4520. fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
  4521. if (phba->lmt & LMT_2Gb)
  4522. fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
  4523. if (phba->lmt & LMT_1Gb)
  4524. fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
  4525. }
  4526. /**
  4527. * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
  4528. * @shost: pointer to SCSI host data structure.
  4529. *
  4530. * This routine initializes a given SCSI host attributes on a FC port. The
  4531. * SCSI host can be either on top of a physical port or a virtual port.
  4532. **/
  4533. void lpfc_host_attrib_init(struct Scsi_Host *shost)
  4534. {
  4535. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  4536. struct lpfc_hba *phba = vport->phba;
  4537. /*
  4538. * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
  4539. */
  4540. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  4541. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  4542. fc_host_supported_classes(shost) = FC_COS_CLASS3;
  4543. memset(fc_host_supported_fc4s(shost), 0,
  4544. sizeof(fc_host_supported_fc4s(shost)));
  4545. fc_host_supported_fc4s(shost)[2] = 1;
  4546. fc_host_supported_fc4s(shost)[7] = 1;
  4547. lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
  4548. sizeof fc_host_symbolic_name(shost));
  4549. lpfc_host_supported_speeds_set(shost);
  4550. fc_host_maxframe_size(shost) =
  4551. (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
  4552. (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
  4553. fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
  4554. /* This value is also unchanging */
  4555. memset(fc_host_active_fc4s(shost), 0,
  4556. sizeof(fc_host_active_fc4s(shost)));
  4557. fc_host_active_fc4s(shost)[2] = 1;
  4558. fc_host_active_fc4s(shost)[7] = 1;
  4559. fc_host_max_npiv_vports(shost) = phba->max_vpi;
  4560. clear_bit(FC_LOADING, &vport->load_flag);
  4561. }
  4562. /**
  4563. * lpfc_stop_port_s3 - Stop SLI3 device port
  4564. * @phba: pointer to lpfc hba data structure.
  4565. *
  4566. * This routine is invoked to stop an SLI3 device port, it stops the device
  4567. * from generating interrupts and stops the device driver's timers for the
  4568. * device.
  4569. **/
  4570. static void
  4571. lpfc_stop_port_s3(struct lpfc_hba *phba)
  4572. {
  4573. /* Clear all interrupt enable conditions */
  4574. writel(0, phba->HCregaddr);
  4575. readl(phba->HCregaddr); /* flush */
  4576. /* Clear all pending interrupts */
  4577. writel(0xffffffff, phba->HAregaddr);
  4578. readl(phba->HAregaddr); /* flush */
  4579. /* Reset some HBA SLI setup states */
  4580. lpfc_stop_hba_timers(phba);
  4581. phba->pport->work_port_events = 0;
  4582. }
  4583. /**
  4584. * lpfc_stop_port_s4 - Stop SLI4 device port
  4585. * @phba: pointer to lpfc hba data structure.
  4586. *
  4587. * This routine is invoked to stop an SLI4 device port, it stops the device
  4588. * from generating interrupts and stops the device driver's timers for the
  4589. * device.
  4590. **/
  4591. static void
  4592. lpfc_stop_port_s4(struct lpfc_hba *phba)
  4593. {
  4594. /* Reset some HBA SLI4 setup states */
  4595. lpfc_stop_hba_timers(phba);
  4596. if (phba->pport)
  4597. phba->pport->work_port_events = 0;
  4598. phba->sli4_hba.intr_enable = 0;
  4599. }
  4600. /**
  4601. * lpfc_stop_port - Wrapper function for stopping hba port
  4602. * @phba: Pointer to HBA context object.
  4603. *
  4604. * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
  4605. * the API jump table function pointer from the lpfc_hba struct.
  4606. **/
  4607. void
  4608. lpfc_stop_port(struct lpfc_hba *phba)
  4609. {
  4610. phba->lpfc_stop_port(phba);
  4611. if (phba->wq)
  4612. flush_workqueue(phba->wq);
  4613. }
  4614. /**
  4615. * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
  4616. * @phba: Pointer to hba for which this call is being executed.
  4617. *
  4618. * This routine starts the timer waiting for the FCF rediscovery to complete.
  4619. **/
  4620. void
  4621. lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
  4622. {
  4623. unsigned long fcf_redisc_wait_tmo =
  4624. (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
  4625. /* Start fcf rediscovery wait period timer */
  4626. mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
  4627. spin_lock_irq(&phba->hbalock);
  4628. /* Allow action to new fcf asynchronous event */
  4629. phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
  4630. /* Mark the FCF rediscovery pending state */
  4631. phba->fcf.fcf_flag |= FCF_REDISC_PEND;
  4632. spin_unlock_irq(&phba->hbalock);
  4633. }
  4634. /**
  4635. * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
  4636. * @t: Timer context used to obtain the pointer to lpfc hba data structure.
  4637. *
  4638. * This routine is invoked when waiting for FCF table rediscover has been
  4639. * timed out. If new FCF record(s) has (have) been discovered during the
  4640. * wait period, a new FCF event shall be added to the FCOE async event
  4641. * list, and then worker thread shall be waked up for processing from the
  4642. * worker thread context.
  4643. **/
  4644. static void
  4645. lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
  4646. {
  4647. struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
  4648. /* Don't send FCF rediscovery event if timer cancelled */
  4649. spin_lock_irq(&phba->hbalock);
  4650. if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
  4651. spin_unlock_irq(&phba->hbalock);
  4652. return;
  4653. }
  4654. /* Clear FCF rediscovery timer pending flag */
  4655. phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
  4656. /* FCF rediscovery event to worker thread */
  4657. phba->fcf.fcf_flag |= FCF_REDISC_EVT;
  4658. spin_unlock_irq(&phba->hbalock);
  4659. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  4660. "2776 FCF rediscover quiescent timer expired\n");
  4661. /* wake up worker thread */
  4662. lpfc_worker_wake_up(phba);
  4663. }
  4664. /**
  4665. * lpfc_vmid_poll - VMID timeout detection
  4666. * @t: Timer context used to obtain the pointer to lpfc hba data structure.
  4667. *
  4668. * This routine is invoked when there is no I/O on by a VM for the specified
  4669. * amount of time. When this situation is detected, the VMID has to be
  4670. * deregistered from the switch and all the local resources freed. The VMID
  4671. * will be reassigned to the VM once the I/O begins.
  4672. **/
  4673. static void
  4674. lpfc_vmid_poll(struct timer_list *t)
  4675. {
  4676. struct lpfc_hba *phba = from_timer(phba, t, inactive_vmid_poll);
  4677. u32 wake_up = 0;
  4678. /* check if there is a need to issue QFPA */
  4679. if (phba->pport->vmid_priority_tagging) {
  4680. wake_up = 1;
  4681. phba->pport->work_port_events |= WORKER_CHECK_VMID_ISSUE_QFPA;
  4682. }
  4683. /* Is the vmid inactivity timer enabled */
  4684. if (phba->pport->vmid_inactivity_timeout ||
  4685. test_bit(FC_DEREGISTER_ALL_APP_ID, &phba->pport->load_flag)) {
  4686. wake_up = 1;
  4687. phba->pport->work_port_events |= WORKER_CHECK_INACTIVE_VMID;
  4688. }
  4689. if (wake_up)
  4690. lpfc_worker_wake_up(phba);
  4691. /* restart the timer for the next iteration */
  4692. mod_timer(&phba->inactive_vmid_poll, jiffies + msecs_to_jiffies(1000 *
  4693. LPFC_VMID_TIMER));
  4694. }
  4695. /**
  4696. * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
  4697. * @phba: pointer to lpfc hba data structure.
  4698. * @acqe_link: pointer to the async link completion queue entry.
  4699. *
  4700. * This routine is to parse the SLI4 link-attention link fault code.
  4701. **/
  4702. static void
  4703. lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
  4704. struct lpfc_acqe_link *acqe_link)
  4705. {
  4706. switch (bf_get(lpfc_acqe_fc_la_att_type, acqe_link)) {
  4707. case LPFC_FC_LA_TYPE_LINK_DOWN:
  4708. case LPFC_FC_LA_TYPE_TRUNKING_EVENT:
  4709. case LPFC_FC_LA_TYPE_ACTIVATE_FAIL:
  4710. case LPFC_FC_LA_TYPE_LINK_RESET_PRTCL_EVT:
  4711. break;
  4712. default:
  4713. switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
  4714. case LPFC_ASYNC_LINK_FAULT_NONE:
  4715. case LPFC_ASYNC_LINK_FAULT_LOCAL:
  4716. case LPFC_ASYNC_LINK_FAULT_REMOTE:
  4717. case LPFC_ASYNC_LINK_FAULT_LR_LRR:
  4718. break;
  4719. default:
  4720. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  4721. "0398 Unknown link fault code: x%x\n",
  4722. bf_get(lpfc_acqe_link_fault, acqe_link));
  4723. break;
  4724. }
  4725. break;
  4726. }
  4727. }
  4728. /**
  4729. * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
  4730. * @phba: pointer to lpfc hba data structure.
  4731. * @acqe_link: pointer to the async link completion queue entry.
  4732. *
  4733. * This routine is to parse the SLI4 link attention type and translate it
  4734. * into the base driver's link attention type coding.
  4735. *
  4736. * Return: Link attention type in terms of base driver's coding.
  4737. **/
  4738. static uint8_t
  4739. lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
  4740. struct lpfc_acqe_link *acqe_link)
  4741. {
  4742. uint8_t att_type;
  4743. switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
  4744. case LPFC_ASYNC_LINK_STATUS_DOWN:
  4745. case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
  4746. att_type = LPFC_ATT_LINK_DOWN;
  4747. break;
  4748. case LPFC_ASYNC_LINK_STATUS_UP:
  4749. /* Ignore physical link up events - wait for logical link up */
  4750. att_type = LPFC_ATT_RESERVED;
  4751. break;
  4752. case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
  4753. att_type = LPFC_ATT_LINK_UP;
  4754. break;
  4755. default:
  4756. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  4757. "0399 Invalid link attention type: x%x\n",
  4758. bf_get(lpfc_acqe_link_status, acqe_link));
  4759. att_type = LPFC_ATT_RESERVED;
  4760. break;
  4761. }
  4762. return att_type;
  4763. }
  4764. /**
  4765. * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
  4766. * @phba: pointer to lpfc hba data structure.
  4767. *
  4768. * This routine is to get an SLI3 FC port's link speed in Mbps.
  4769. *
  4770. * Return: link speed in terms of Mbps.
  4771. **/
  4772. uint32_t
  4773. lpfc_sli_port_speed_get(struct lpfc_hba *phba)
  4774. {
  4775. uint32_t link_speed;
  4776. if (!lpfc_is_link_up(phba))
  4777. return 0;
  4778. if (phba->sli_rev <= LPFC_SLI_REV3) {
  4779. switch (phba->fc_linkspeed) {
  4780. case LPFC_LINK_SPEED_1GHZ:
  4781. link_speed = 1000;
  4782. break;
  4783. case LPFC_LINK_SPEED_2GHZ:
  4784. link_speed = 2000;
  4785. break;
  4786. case LPFC_LINK_SPEED_4GHZ:
  4787. link_speed = 4000;
  4788. break;
  4789. case LPFC_LINK_SPEED_8GHZ:
  4790. link_speed = 8000;
  4791. break;
  4792. case LPFC_LINK_SPEED_10GHZ:
  4793. link_speed = 10000;
  4794. break;
  4795. case LPFC_LINK_SPEED_16GHZ:
  4796. link_speed = 16000;
  4797. break;
  4798. default:
  4799. link_speed = 0;
  4800. }
  4801. } else {
  4802. if (phba->sli4_hba.link_state.logical_speed)
  4803. link_speed =
  4804. phba->sli4_hba.link_state.logical_speed;
  4805. else
  4806. link_speed = phba->sli4_hba.link_state.speed;
  4807. }
  4808. return link_speed;
  4809. }
  4810. /**
  4811. * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
  4812. * @phba: pointer to lpfc hba data structure.
  4813. * @evt_code: asynchronous event code.
  4814. * @speed_code: asynchronous event link speed code.
  4815. *
  4816. * This routine is to parse the giving SLI4 async event link speed code into
  4817. * value of Mbps for the link speed.
  4818. *
  4819. * Return: link speed in terms of Mbps.
  4820. **/
  4821. static uint32_t
  4822. lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
  4823. uint8_t speed_code)
  4824. {
  4825. uint32_t port_speed;
  4826. switch (evt_code) {
  4827. case LPFC_TRAILER_CODE_LINK:
  4828. switch (speed_code) {
  4829. case LPFC_ASYNC_LINK_SPEED_ZERO:
  4830. port_speed = 0;
  4831. break;
  4832. case LPFC_ASYNC_LINK_SPEED_10MBPS:
  4833. port_speed = 10;
  4834. break;
  4835. case LPFC_ASYNC_LINK_SPEED_100MBPS:
  4836. port_speed = 100;
  4837. break;
  4838. case LPFC_ASYNC_LINK_SPEED_1GBPS:
  4839. port_speed = 1000;
  4840. break;
  4841. case LPFC_ASYNC_LINK_SPEED_10GBPS:
  4842. port_speed = 10000;
  4843. break;
  4844. case LPFC_ASYNC_LINK_SPEED_20GBPS:
  4845. port_speed = 20000;
  4846. break;
  4847. case LPFC_ASYNC_LINK_SPEED_25GBPS:
  4848. port_speed = 25000;
  4849. break;
  4850. case LPFC_ASYNC_LINK_SPEED_40GBPS:
  4851. port_speed = 40000;
  4852. break;
  4853. case LPFC_ASYNC_LINK_SPEED_100GBPS:
  4854. port_speed = 100000;
  4855. break;
  4856. default:
  4857. port_speed = 0;
  4858. }
  4859. break;
  4860. case LPFC_TRAILER_CODE_FC:
  4861. switch (speed_code) {
  4862. case LPFC_FC_LA_SPEED_UNKNOWN:
  4863. port_speed = 0;
  4864. break;
  4865. case LPFC_FC_LA_SPEED_1G:
  4866. port_speed = 1000;
  4867. break;
  4868. case LPFC_FC_LA_SPEED_2G:
  4869. port_speed = 2000;
  4870. break;
  4871. case LPFC_FC_LA_SPEED_4G:
  4872. port_speed = 4000;
  4873. break;
  4874. case LPFC_FC_LA_SPEED_8G:
  4875. port_speed = 8000;
  4876. break;
  4877. case LPFC_FC_LA_SPEED_10G:
  4878. port_speed = 10000;
  4879. break;
  4880. case LPFC_FC_LA_SPEED_16G:
  4881. port_speed = 16000;
  4882. break;
  4883. case LPFC_FC_LA_SPEED_32G:
  4884. port_speed = 32000;
  4885. break;
  4886. case LPFC_FC_LA_SPEED_64G:
  4887. port_speed = 64000;
  4888. break;
  4889. case LPFC_FC_LA_SPEED_128G:
  4890. port_speed = 128000;
  4891. break;
  4892. case LPFC_FC_LA_SPEED_256G:
  4893. port_speed = 256000;
  4894. break;
  4895. default:
  4896. port_speed = 0;
  4897. }
  4898. break;
  4899. default:
  4900. port_speed = 0;
  4901. }
  4902. return port_speed;
  4903. }
  4904. /**
  4905. * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
  4906. * @phba: pointer to lpfc hba data structure.
  4907. * @acqe_link: pointer to the async link completion queue entry.
  4908. *
  4909. * This routine is to handle the SLI4 asynchronous FCoE link event.
  4910. **/
  4911. static void
  4912. lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
  4913. struct lpfc_acqe_link *acqe_link)
  4914. {
  4915. LPFC_MBOXQ_t *pmb;
  4916. MAILBOX_t *mb;
  4917. struct lpfc_mbx_read_top *la;
  4918. uint8_t att_type;
  4919. int rc;
  4920. att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
  4921. if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
  4922. return;
  4923. phba->fcoe_eventtag = acqe_link->event_tag;
  4924. pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4925. if (!pmb) {
  4926. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  4927. "0395 The mboxq allocation failed\n");
  4928. return;
  4929. }
  4930. rc = lpfc_mbox_rsrc_prep(phba, pmb);
  4931. if (rc) {
  4932. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  4933. "0396 mailbox allocation failed\n");
  4934. goto out_free_pmb;
  4935. }
  4936. /* Cleanup any outstanding ELS commands */
  4937. lpfc_els_flush_all_cmd(phba);
  4938. /* Block ELS IOCBs until we have done process link event */
  4939. phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
  4940. /* Update link event statistics */
  4941. phba->sli.slistat.link_event++;
  4942. /* Create lpfc_handle_latt mailbox command from link ACQE */
  4943. lpfc_read_topology(phba, pmb, pmb->ctx_buf);
  4944. pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
  4945. pmb->vport = phba->pport;
  4946. /* Keep the link status for extra SLI4 state machine reference */
  4947. phba->sli4_hba.link_state.speed =
  4948. lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
  4949. bf_get(lpfc_acqe_link_speed, acqe_link));
  4950. phba->sli4_hba.link_state.duplex =
  4951. bf_get(lpfc_acqe_link_duplex, acqe_link);
  4952. phba->sli4_hba.link_state.status =
  4953. bf_get(lpfc_acqe_link_status, acqe_link);
  4954. phba->sli4_hba.link_state.type =
  4955. bf_get(lpfc_acqe_link_type, acqe_link);
  4956. phba->sli4_hba.link_state.number =
  4957. bf_get(lpfc_acqe_link_number, acqe_link);
  4958. phba->sli4_hba.link_state.fault =
  4959. bf_get(lpfc_acqe_link_fault, acqe_link);
  4960. phba->sli4_hba.link_state.logical_speed =
  4961. bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
  4962. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4963. "2900 Async FC/FCoE Link event - Speed:%dGBit "
  4964. "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
  4965. "Logical speed:%dMbps Fault:%d\n",
  4966. phba->sli4_hba.link_state.speed,
  4967. phba->sli4_hba.link_state.topology,
  4968. phba->sli4_hba.link_state.status,
  4969. phba->sli4_hba.link_state.type,
  4970. phba->sli4_hba.link_state.number,
  4971. phba->sli4_hba.link_state.logical_speed,
  4972. phba->sli4_hba.link_state.fault);
  4973. /*
  4974. * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
  4975. * topology info. Note: Optional for non FC-AL ports.
  4976. */
  4977. if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
  4978. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  4979. if (rc == MBX_NOT_FINISHED)
  4980. goto out_free_pmb;
  4981. return;
  4982. }
  4983. /*
  4984. * For FCoE Mode: fill in all the topology information we need and call
  4985. * the READ_TOPOLOGY completion routine to continue without actually
  4986. * sending the READ_TOPOLOGY mailbox command to the port.
  4987. */
  4988. /* Initialize completion status */
  4989. mb = &pmb->u.mb;
  4990. mb->mbxStatus = MBX_SUCCESS;
  4991. /* Parse port fault information field */
  4992. lpfc_sli4_parse_latt_fault(phba, acqe_link);
  4993. /* Parse and translate link attention fields */
  4994. la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
  4995. la->eventTag = acqe_link->event_tag;
  4996. bf_set(lpfc_mbx_read_top_att_type, la, att_type);
  4997. bf_set(lpfc_mbx_read_top_link_spd, la,
  4998. (bf_get(lpfc_acqe_link_speed, acqe_link)));
  4999. /* Fake the following irrelevant fields */
  5000. bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
  5001. bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
  5002. bf_set(lpfc_mbx_read_top_il, la, 0);
  5003. bf_set(lpfc_mbx_read_top_pb, la, 0);
  5004. bf_set(lpfc_mbx_read_top_fa, la, 0);
  5005. bf_set(lpfc_mbx_read_top_mm, la, 0);
  5006. /* Invoke the lpfc_handle_latt mailbox command callback function */
  5007. lpfc_mbx_cmpl_read_topology(phba, pmb);
  5008. return;
  5009. out_free_pmb:
  5010. lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
  5011. }
  5012. /**
  5013. * lpfc_async_link_speed_to_read_top - Parse async evt link speed code to read
  5014. * topology.
  5015. * @phba: pointer to lpfc hba data structure.
  5016. * @speed_code: asynchronous event link speed code.
  5017. *
  5018. * This routine is to parse the giving SLI4 async event link speed code into
  5019. * value of Read topology link speed.
  5020. *
  5021. * Return: link speed in terms of Read topology.
  5022. **/
  5023. static uint8_t
  5024. lpfc_async_link_speed_to_read_top(struct lpfc_hba *phba, uint8_t speed_code)
  5025. {
  5026. uint8_t port_speed;
  5027. switch (speed_code) {
  5028. case LPFC_FC_LA_SPEED_1G:
  5029. port_speed = LPFC_LINK_SPEED_1GHZ;
  5030. break;
  5031. case LPFC_FC_LA_SPEED_2G:
  5032. port_speed = LPFC_LINK_SPEED_2GHZ;
  5033. break;
  5034. case LPFC_FC_LA_SPEED_4G:
  5035. port_speed = LPFC_LINK_SPEED_4GHZ;
  5036. break;
  5037. case LPFC_FC_LA_SPEED_8G:
  5038. port_speed = LPFC_LINK_SPEED_8GHZ;
  5039. break;
  5040. case LPFC_FC_LA_SPEED_16G:
  5041. port_speed = LPFC_LINK_SPEED_16GHZ;
  5042. break;
  5043. case LPFC_FC_LA_SPEED_32G:
  5044. port_speed = LPFC_LINK_SPEED_32GHZ;
  5045. break;
  5046. case LPFC_FC_LA_SPEED_64G:
  5047. port_speed = LPFC_LINK_SPEED_64GHZ;
  5048. break;
  5049. case LPFC_FC_LA_SPEED_128G:
  5050. port_speed = LPFC_LINK_SPEED_128GHZ;
  5051. break;
  5052. case LPFC_FC_LA_SPEED_256G:
  5053. port_speed = LPFC_LINK_SPEED_256GHZ;
  5054. break;
  5055. default:
  5056. port_speed = 0;
  5057. break;
  5058. }
  5059. return port_speed;
  5060. }
  5061. void
  5062. lpfc_cgn_dump_rxmonitor(struct lpfc_hba *phba)
  5063. {
  5064. if (!phba->rx_monitor) {
  5065. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
  5066. "4411 Rx Monitor Info is empty.\n");
  5067. } else {
  5068. lpfc_rx_monitor_report(phba, phba->rx_monitor, NULL, 0,
  5069. LPFC_MAX_RXMONITOR_DUMP);
  5070. }
  5071. }
  5072. /**
  5073. * lpfc_cgn_update_stat - Save data into congestion stats buffer
  5074. * @phba: pointer to lpfc hba data structure.
  5075. * @dtag: FPIN descriptor received
  5076. *
  5077. * Increment the FPIN received counter/time when it happens.
  5078. */
  5079. void
  5080. lpfc_cgn_update_stat(struct lpfc_hba *phba, uint32_t dtag)
  5081. {
  5082. struct lpfc_cgn_info *cp;
  5083. u32 value;
  5084. /* Make sure we have a congestion info buffer */
  5085. if (!phba->cgn_i)
  5086. return;
  5087. cp = (struct lpfc_cgn_info *)phba->cgn_i->virt;
  5088. /* Update congestion statistics */
  5089. switch (dtag) {
  5090. case ELS_DTAG_LNK_INTEGRITY:
  5091. le32_add_cpu(&cp->link_integ_notification, 1);
  5092. lpfc_cgn_update_tstamp(phba, &cp->stat_lnk);
  5093. break;
  5094. case ELS_DTAG_DELIVERY:
  5095. le32_add_cpu(&cp->delivery_notification, 1);
  5096. lpfc_cgn_update_tstamp(phba, &cp->stat_delivery);
  5097. break;
  5098. case ELS_DTAG_PEER_CONGEST:
  5099. le32_add_cpu(&cp->cgn_peer_notification, 1);
  5100. lpfc_cgn_update_tstamp(phba, &cp->stat_peer);
  5101. break;
  5102. case ELS_DTAG_CONGESTION:
  5103. le32_add_cpu(&cp->cgn_notification, 1);
  5104. lpfc_cgn_update_tstamp(phba, &cp->stat_fpin);
  5105. }
  5106. if (phba->cgn_fpin_frequency &&
  5107. phba->cgn_fpin_frequency != LPFC_FPIN_INIT_FREQ) {
  5108. value = LPFC_CGN_TIMER_TO_MIN / phba->cgn_fpin_frequency;
  5109. cp->cgn_stat_npm = value;
  5110. }
  5111. value = lpfc_cgn_calc_crc32(cp, LPFC_CGN_INFO_SZ,
  5112. LPFC_CGN_CRC32_SEED);
  5113. cp->cgn_info_crc = cpu_to_le32(value);
  5114. }
  5115. /**
  5116. * lpfc_cgn_update_tstamp - Update cmf timestamp
  5117. * @phba: pointer to lpfc hba data structure.
  5118. * @ts: structure to write the timestamp to.
  5119. */
  5120. void
  5121. lpfc_cgn_update_tstamp(struct lpfc_hba *phba, struct lpfc_cgn_ts *ts)
  5122. {
  5123. struct timespec64 cur_time;
  5124. struct tm tm_val;
  5125. ktime_get_real_ts64(&cur_time);
  5126. time64_to_tm(cur_time.tv_sec, 0, &tm_val);
  5127. ts->month = tm_val.tm_mon + 1;
  5128. ts->day = tm_val.tm_mday;
  5129. ts->year = tm_val.tm_year - 100;
  5130. ts->hour = tm_val.tm_hour;
  5131. ts->minute = tm_val.tm_min;
  5132. ts->second = tm_val.tm_sec;
  5133. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
  5134. "2646 Updated CMF timestamp : "
  5135. "%u/%u/%u %u:%u:%u\n",
  5136. ts->day, ts->month,
  5137. ts->year, ts->hour,
  5138. ts->minute, ts->second);
  5139. }
  5140. /**
  5141. * lpfc_cmf_stats_timer - Save data into registered congestion buffer
  5142. * @timer: Timer cookie to access lpfc private data
  5143. *
  5144. * Save the congestion event data every minute.
  5145. * On the hour collapse all the minute data into hour data. Every day
  5146. * collapse all the hour data into daily data. Separate driver
  5147. * and fabrc congestion event counters that will be saved out
  5148. * to the registered congestion buffer every minute.
  5149. */
  5150. static enum hrtimer_restart
  5151. lpfc_cmf_stats_timer(struct hrtimer *timer)
  5152. {
  5153. struct lpfc_hba *phba;
  5154. struct lpfc_cgn_info *cp;
  5155. uint32_t i, index;
  5156. uint16_t value, mvalue;
  5157. uint64_t bps;
  5158. uint32_t mbps;
  5159. uint32_t dvalue, wvalue, lvalue, avalue;
  5160. uint64_t latsum;
  5161. __le16 *ptr;
  5162. __le32 *lptr;
  5163. __le16 *mptr;
  5164. phba = container_of(timer, struct lpfc_hba, cmf_stats_timer);
  5165. /* Make sure we have a congestion info buffer */
  5166. if (!phba->cgn_i)
  5167. return HRTIMER_NORESTART;
  5168. cp = (struct lpfc_cgn_info *)phba->cgn_i->virt;
  5169. phba->cgn_evt_timestamp = jiffies +
  5170. msecs_to_jiffies(LPFC_CGN_TIMER_TO_MIN);
  5171. phba->cgn_evt_minute++;
  5172. /* We should get to this point in the routine on 1 minute intervals */
  5173. lpfc_cgn_update_tstamp(phba, &cp->base_time);
  5174. if (phba->cgn_fpin_frequency &&
  5175. phba->cgn_fpin_frequency != LPFC_FPIN_INIT_FREQ) {
  5176. value = LPFC_CGN_TIMER_TO_MIN / phba->cgn_fpin_frequency;
  5177. cp->cgn_stat_npm = value;
  5178. }
  5179. /* Read and clear the latency counters for this minute */
  5180. lvalue = atomic_read(&phba->cgn_latency_evt_cnt);
  5181. latsum = atomic64_read(&phba->cgn_latency_evt);
  5182. atomic_set(&phba->cgn_latency_evt_cnt, 0);
  5183. atomic64_set(&phba->cgn_latency_evt, 0);
  5184. /* We need to store MB/sec bandwidth in the congestion information.
  5185. * block_cnt is count of 512 byte blocks for the entire minute,
  5186. * bps will get bytes per sec before finally converting to MB/sec.
  5187. */
  5188. bps = div_u64(phba->rx_block_cnt, LPFC_SEC_MIN) * 512;
  5189. phba->rx_block_cnt = 0;
  5190. mvalue = bps / (1024 * 1024); /* convert to MB/sec */
  5191. /* Every minute */
  5192. /* cgn parameters */
  5193. cp->cgn_info_mode = phba->cgn_p.cgn_param_mode;
  5194. cp->cgn_info_level0 = phba->cgn_p.cgn_param_level0;
  5195. cp->cgn_info_level1 = phba->cgn_p.cgn_param_level1;
  5196. cp->cgn_info_level2 = phba->cgn_p.cgn_param_level2;
  5197. /* Fill in default LUN qdepth */
  5198. value = (uint16_t)(phba->pport->cfg_lun_queue_depth);
  5199. cp->cgn_lunq = cpu_to_le16(value);
  5200. /* Record congestion buffer info - every minute
  5201. * cgn_driver_evt_cnt (Driver events)
  5202. * cgn_fabric_warn_cnt (Congestion Warnings)
  5203. * cgn_latency_evt_cnt / cgn_latency_evt (IO Latency)
  5204. * cgn_fabric_alarm_cnt (Congestion Alarms)
  5205. */
  5206. index = ++cp->cgn_index_minute;
  5207. if (cp->cgn_index_minute == LPFC_MIN_HOUR) {
  5208. cp->cgn_index_minute = 0;
  5209. index = 0;
  5210. }
  5211. /* Get the number of driver events in this sample and reset counter */
  5212. dvalue = atomic_read(&phba->cgn_driver_evt_cnt);
  5213. atomic_set(&phba->cgn_driver_evt_cnt, 0);
  5214. /* Get the number of warning events - FPIN and Signal for this minute */
  5215. wvalue = 0;
  5216. if ((phba->cgn_reg_fpin & LPFC_CGN_FPIN_WARN) ||
  5217. phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY ||
  5218. phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM)
  5219. wvalue = atomic_read(&phba->cgn_fabric_warn_cnt);
  5220. atomic_set(&phba->cgn_fabric_warn_cnt, 0);
  5221. /* Get the number of alarm events - FPIN and Signal for this minute */
  5222. avalue = 0;
  5223. if ((phba->cgn_reg_fpin & LPFC_CGN_FPIN_ALARM) ||
  5224. phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM)
  5225. avalue = atomic_read(&phba->cgn_fabric_alarm_cnt);
  5226. atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
  5227. /* Collect the driver, warning, alarm and latency counts for this
  5228. * minute into the driver congestion buffer.
  5229. */
  5230. ptr = &cp->cgn_drvr_min[index];
  5231. value = (uint16_t)dvalue;
  5232. *ptr = cpu_to_le16(value);
  5233. ptr = &cp->cgn_warn_min[index];
  5234. value = (uint16_t)wvalue;
  5235. *ptr = cpu_to_le16(value);
  5236. ptr = &cp->cgn_alarm_min[index];
  5237. value = (uint16_t)avalue;
  5238. *ptr = cpu_to_le16(value);
  5239. lptr = &cp->cgn_latency_min[index];
  5240. if (lvalue) {
  5241. lvalue = (uint32_t)div_u64(latsum, lvalue);
  5242. *lptr = cpu_to_le32(lvalue);
  5243. } else {
  5244. *lptr = 0;
  5245. }
  5246. /* Collect the bandwidth value into the driver's congesion buffer. */
  5247. mptr = &cp->cgn_bw_min[index];
  5248. *mptr = cpu_to_le16(mvalue);
  5249. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
  5250. "2418 Congestion Info - minute (%d): %d %d %d %d %d\n",
  5251. index, dvalue, wvalue, *lptr, mvalue, avalue);
  5252. /* Every hour */
  5253. if ((phba->cgn_evt_minute % LPFC_MIN_HOUR) == 0) {
  5254. /* Record congestion buffer info - every hour
  5255. * Collapse all minutes into an hour
  5256. */
  5257. index = ++cp->cgn_index_hour;
  5258. if (cp->cgn_index_hour == LPFC_HOUR_DAY) {
  5259. cp->cgn_index_hour = 0;
  5260. index = 0;
  5261. }
  5262. dvalue = 0;
  5263. wvalue = 0;
  5264. lvalue = 0;
  5265. avalue = 0;
  5266. mvalue = 0;
  5267. mbps = 0;
  5268. for (i = 0; i < LPFC_MIN_HOUR; i++) {
  5269. dvalue += le16_to_cpu(cp->cgn_drvr_min[i]);
  5270. wvalue += le16_to_cpu(cp->cgn_warn_min[i]);
  5271. lvalue += le32_to_cpu(cp->cgn_latency_min[i]);
  5272. mbps += le16_to_cpu(cp->cgn_bw_min[i]);
  5273. avalue += le16_to_cpu(cp->cgn_alarm_min[i]);
  5274. }
  5275. if (lvalue) /* Avg of latency averages */
  5276. lvalue /= LPFC_MIN_HOUR;
  5277. if (mbps) /* Avg of Bandwidth averages */
  5278. mvalue = mbps / LPFC_MIN_HOUR;
  5279. lptr = &cp->cgn_drvr_hr[index];
  5280. *lptr = cpu_to_le32(dvalue);
  5281. lptr = &cp->cgn_warn_hr[index];
  5282. *lptr = cpu_to_le32(wvalue);
  5283. lptr = &cp->cgn_latency_hr[index];
  5284. *lptr = cpu_to_le32(lvalue);
  5285. mptr = &cp->cgn_bw_hr[index];
  5286. *mptr = cpu_to_le16(mvalue);
  5287. lptr = &cp->cgn_alarm_hr[index];
  5288. *lptr = cpu_to_le32(avalue);
  5289. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
  5290. "2419 Congestion Info - hour "
  5291. "(%d): %d %d %d %d %d\n",
  5292. index, dvalue, wvalue, lvalue, mvalue, avalue);
  5293. }
  5294. /* Every day */
  5295. if ((phba->cgn_evt_minute % LPFC_MIN_DAY) == 0) {
  5296. /* Record congestion buffer info - every hour
  5297. * Collapse all hours into a day. Rotate days
  5298. * after LPFC_MAX_CGN_DAYS.
  5299. */
  5300. index = ++cp->cgn_index_day;
  5301. if (cp->cgn_index_day == LPFC_MAX_CGN_DAYS) {
  5302. cp->cgn_index_day = 0;
  5303. index = 0;
  5304. }
  5305. dvalue = 0;
  5306. wvalue = 0;
  5307. lvalue = 0;
  5308. mvalue = 0;
  5309. mbps = 0;
  5310. avalue = 0;
  5311. for (i = 0; i < LPFC_HOUR_DAY; i++) {
  5312. dvalue += le32_to_cpu(cp->cgn_drvr_hr[i]);
  5313. wvalue += le32_to_cpu(cp->cgn_warn_hr[i]);
  5314. lvalue += le32_to_cpu(cp->cgn_latency_hr[i]);
  5315. mbps += le16_to_cpu(cp->cgn_bw_hr[i]);
  5316. avalue += le32_to_cpu(cp->cgn_alarm_hr[i]);
  5317. }
  5318. if (lvalue) /* Avg of latency averages */
  5319. lvalue /= LPFC_HOUR_DAY;
  5320. if (mbps) /* Avg of Bandwidth averages */
  5321. mvalue = mbps / LPFC_HOUR_DAY;
  5322. lptr = &cp->cgn_drvr_day[index];
  5323. *lptr = cpu_to_le32(dvalue);
  5324. lptr = &cp->cgn_warn_day[index];
  5325. *lptr = cpu_to_le32(wvalue);
  5326. lptr = &cp->cgn_latency_day[index];
  5327. *lptr = cpu_to_le32(lvalue);
  5328. mptr = &cp->cgn_bw_day[index];
  5329. *mptr = cpu_to_le16(mvalue);
  5330. lptr = &cp->cgn_alarm_day[index];
  5331. *lptr = cpu_to_le32(avalue);
  5332. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
  5333. "2420 Congestion Info - daily (%d): "
  5334. "%d %d %d %d %d\n",
  5335. index, dvalue, wvalue, lvalue, mvalue, avalue);
  5336. }
  5337. /* Use the frequency found in the last rcv'ed FPIN */
  5338. value = phba->cgn_fpin_frequency;
  5339. cp->cgn_warn_freq = cpu_to_le16(value);
  5340. cp->cgn_alarm_freq = cpu_to_le16(value);
  5341. lvalue = lpfc_cgn_calc_crc32(cp, LPFC_CGN_INFO_SZ,
  5342. LPFC_CGN_CRC32_SEED);
  5343. cp->cgn_info_crc = cpu_to_le32(lvalue);
  5344. hrtimer_forward_now(timer, ktime_set(0, LPFC_SEC_MIN * NSEC_PER_SEC));
  5345. return HRTIMER_RESTART;
  5346. }
  5347. /**
  5348. * lpfc_calc_cmf_latency - latency from start of rxate timer interval
  5349. * @phba: The Hba for which this call is being executed.
  5350. *
  5351. * The routine calculates the latency from the beginning of the CMF timer
  5352. * interval to the current point in time. It is called from IO completion
  5353. * when we exceed our Bandwidth limitation for the time interval.
  5354. */
  5355. uint32_t
  5356. lpfc_calc_cmf_latency(struct lpfc_hba *phba)
  5357. {
  5358. struct timespec64 cmpl_time;
  5359. uint32_t msec = 0;
  5360. ktime_get_real_ts64(&cmpl_time);
  5361. /* This routine works on a ms granularity so sec and usec are
  5362. * converted accordingly.
  5363. */
  5364. if (cmpl_time.tv_sec == phba->cmf_latency.tv_sec) {
  5365. msec = (cmpl_time.tv_nsec - phba->cmf_latency.tv_nsec) /
  5366. NSEC_PER_MSEC;
  5367. } else {
  5368. if (cmpl_time.tv_nsec >= phba->cmf_latency.tv_nsec) {
  5369. msec = (cmpl_time.tv_sec -
  5370. phba->cmf_latency.tv_sec) * MSEC_PER_SEC;
  5371. msec += ((cmpl_time.tv_nsec -
  5372. phba->cmf_latency.tv_nsec) / NSEC_PER_MSEC);
  5373. } else {
  5374. msec = (cmpl_time.tv_sec - phba->cmf_latency.tv_sec -
  5375. 1) * MSEC_PER_SEC;
  5376. msec += (((NSEC_PER_SEC - phba->cmf_latency.tv_nsec) +
  5377. cmpl_time.tv_nsec) / NSEC_PER_MSEC);
  5378. }
  5379. }
  5380. return msec;
  5381. }
  5382. /**
  5383. * lpfc_cmf_timer - This is the timer function for one congestion
  5384. * rate interval.
  5385. * @timer: Pointer to the high resolution timer that expired
  5386. */
  5387. static enum hrtimer_restart
  5388. lpfc_cmf_timer(struct hrtimer *timer)
  5389. {
  5390. struct lpfc_hba *phba = container_of(timer, struct lpfc_hba,
  5391. cmf_timer);
  5392. struct rx_info_entry entry;
  5393. uint32_t io_cnt;
  5394. uint32_t busy, max_read;
  5395. uint64_t total, rcv, lat, mbpi, extra, cnt;
  5396. int timer_interval = LPFC_CMF_INTERVAL;
  5397. uint32_t ms;
  5398. struct lpfc_cgn_stat *cgs;
  5399. int cpu;
  5400. /* Only restart the timer if congestion mgmt is on */
  5401. if (phba->cmf_active_mode == LPFC_CFG_OFF ||
  5402. !phba->cmf_latency.tv_sec) {
  5403. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
  5404. "6224 CMF timer exit: %d %lld\n",
  5405. phba->cmf_active_mode,
  5406. (uint64_t)phba->cmf_latency.tv_sec);
  5407. return HRTIMER_NORESTART;
  5408. }
  5409. /* If pport is not ready yet, just exit and wait for
  5410. * the next timer cycle to hit.
  5411. */
  5412. if (!phba->pport)
  5413. goto skip;
  5414. /* Do not block SCSI IO while in the timer routine since
  5415. * total_bytes will be cleared
  5416. */
  5417. atomic_set(&phba->cmf_stop_io, 1);
  5418. /* First we need to calculate the actual ms between
  5419. * the last timer interrupt and this one. We ask for
  5420. * LPFC_CMF_INTERVAL, however the actual time may
  5421. * vary depending on system overhead.
  5422. */
  5423. ms = lpfc_calc_cmf_latency(phba);
  5424. /* Immediately after we calculate the time since the last
  5425. * timer interrupt, set the start time for the next
  5426. * interrupt
  5427. */
  5428. ktime_get_real_ts64(&phba->cmf_latency);
  5429. phba->cmf_link_byte_count =
  5430. div_u64(phba->cmf_max_line_rate * LPFC_CMF_INTERVAL, 1000);
  5431. /* Collect all the stats from the prior timer interval */
  5432. total = 0;
  5433. io_cnt = 0;
  5434. lat = 0;
  5435. rcv = 0;
  5436. for_each_present_cpu(cpu) {
  5437. cgs = per_cpu_ptr(phba->cmf_stat, cpu);
  5438. total += atomic64_xchg(&cgs->total_bytes, 0);
  5439. io_cnt += atomic_xchg(&cgs->rx_io_cnt, 0);
  5440. lat += atomic64_xchg(&cgs->rx_latency, 0);
  5441. rcv += atomic64_xchg(&cgs->rcv_bytes, 0);
  5442. }
  5443. /* Before we issue another CMF_SYNC_WQE, retrieve the BW
  5444. * returned from the last CMF_SYNC_WQE issued, from
  5445. * cmf_last_sync_bw. This will be the target BW for
  5446. * this next timer interval.
  5447. */
  5448. if (phba->cmf_active_mode == LPFC_CFG_MANAGED &&
  5449. phba->link_state != LPFC_LINK_DOWN &&
  5450. test_bit(HBA_SETUP, &phba->hba_flag)) {
  5451. mbpi = phba->cmf_last_sync_bw;
  5452. phba->cmf_last_sync_bw = 0;
  5453. extra = 0;
  5454. /* Calculate any extra bytes needed to account for the
  5455. * timer accuracy. If we are less than LPFC_CMF_INTERVAL
  5456. * calculate the adjustment needed for total to reflect
  5457. * a full LPFC_CMF_INTERVAL.
  5458. */
  5459. if (ms && ms < LPFC_CMF_INTERVAL) {
  5460. cnt = div_u64(total, ms); /* bytes per ms */
  5461. cnt *= LPFC_CMF_INTERVAL; /* what total should be */
  5462. extra = cnt - total;
  5463. }
  5464. lpfc_issue_cmf_sync_wqe(phba, LPFC_CMF_INTERVAL, total + extra);
  5465. } else {
  5466. /* For Monitor mode or link down we want mbpi
  5467. * to be the full link speed
  5468. */
  5469. mbpi = phba->cmf_link_byte_count;
  5470. extra = 0;
  5471. }
  5472. phba->cmf_timer_cnt++;
  5473. if (io_cnt) {
  5474. /* Update congestion info buffer latency in us */
  5475. atomic_add(io_cnt, &phba->cgn_latency_evt_cnt);
  5476. atomic64_add(lat, &phba->cgn_latency_evt);
  5477. }
  5478. busy = atomic_xchg(&phba->cmf_busy, 0);
  5479. max_read = atomic_xchg(&phba->rx_max_read_cnt, 0);
  5480. /* Calculate MBPI for the next timer interval */
  5481. if (mbpi) {
  5482. if (mbpi > phba->cmf_link_byte_count ||
  5483. phba->cmf_active_mode == LPFC_CFG_MONITOR)
  5484. mbpi = phba->cmf_link_byte_count;
  5485. /* Change max_bytes_per_interval to what the prior
  5486. * CMF_SYNC_WQE cmpl indicated.
  5487. */
  5488. if (mbpi != phba->cmf_max_bytes_per_interval)
  5489. phba->cmf_max_bytes_per_interval = mbpi;
  5490. }
  5491. /* Save rxmonitor information for debug */
  5492. if (phba->rx_monitor) {
  5493. entry.total_bytes = total;
  5494. entry.cmf_bytes = total + extra;
  5495. entry.rcv_bytes = rcv;
  5496. entry.cmf_busy = busy;
  5497. entry.cmf_info = phba->cmf_active_info;
  5498. if (io_cnt) {
  5499. entry.avg_io_latency = div_u64(lat, io_cnt);
  5500. entry.avg_io_size = div_u64(rcv, io_cnt);
  5501. } else {
  5502. entry.avg_io_latency = 0;
  5503. entry.avg_io_size = 0;
  5504. }
  5505. entry.max_read_cnt = max_read;
  5506. entry.io_cnt = io_cnt;
  5507. entry.max_bytes_per_interval = mbpi;
  5508. if (phba->cmf_active_mode == LPFC_CFG_MANAGED)
  5509. entry.timer_utilization = phba->cmf_last_ts;
  5510. else
  5511. entry.timer_utilization = ms;
  5512. entry.timer_interval = ms;
  5513. phba->cmf_last_ts = 0;
  5514. lpfc_rx_monitor_record(phba->rx_monitor, &entry);
  5515. }
  5516. if (phba->cmf_active_mode == LPFC_CFG_MONITOR) {
  5517. /* If Monitor mode, check if we are oversubscribed
  5518. * against the full line rate.
  5519. */
  5520. if (mbpi && total > mbpi)
  5521. atomic_inc(&phba->cgn_driver_evt_cnt);
  5522. }
  5523. phba->rx_block_cnt += div_u64(rcv, 512); /* save 512 byte block cnt */
  5524. /* Since total_bytes has already been zero'ed, its okay to unblock
  5525. * after max_bytes_per_interval is setup.
  5526. */
  5527. if (atomic_xchg(&phba->cmf_bw_wait, 0))
  5528. queue_work(phba->wq, &phba->unblock_request_work);
  5529. /* SCSI IO is now unblocked */
  5530. atomic_set(&phba->cmf_stop_io, 0);
  5531. skip:
  5532. hrtimer_forward_now(timer,
  5533. ktime_set(0, timer_interval * NSEC_PER_MSEC));
  5534. return HRTIMER_RESTART;
  5535. }
  5536. #define trunk_link_status(__idx)\
  5537. bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
  5538. ((phba->trunk_link.link##__idx.state == LPFC_LINK_UP) ?\
  5539. "Link up" : "Link down") : "NA"
  5540. /* Did port __idx reported an error */
  5541. #define trunk_port_fault(__idx)\
  5542. bf_get(lpfc_acqe_fc_la_trunk_config_port##__idx, acqe_fc) ?\
  5543. (port_fault & (1 << __idx) ? "YES" : "NO") : "NA"
  5544. static void
  5545. lpfc_update_trunk_link_status(struct lpfc_hba *phba,
  5546. struct lpfc_acqe_fc_la *acqe_fc)
  5547. {
  5548. uint8_t port_fault = bf_get(lpfc_acqe_fc_la_trunk_linkmask, acqe_fc);
  5549. uint8_t err = bf_get(lpfc_acqe_fc_la_trunk_fault, acqe_fc);
  5550. u8 cnt = 0;
  5551. phba->sli4_hba.link_state.speed =
  5552. lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
  5553. bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
  5554. phba->sli4_hba.link_state.logical_speed =
  5555. bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
  5556. /* We got FC link speed, convert to fc_linkspeed (READ_TOPOLOGY) */
  5557. phba->fc_linkspeed =
  5558. lpfc_async_link_speed_to_read_top(
  5559. phba,
  5560. bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
  5561. if (bf_get(lpfc_acqe_fc_la_trunk_config_port0, acqe_fc)) {
  5562. phba->trunk_link.link0.state =
  5563. bf_get(lpfc_acqe_fc_la_trunk_link_status_port0, acqe_fc)
  5564. ? LPFC_LINK_UP : LPFC_LINK_DOWN;
  5565. phba->trunk_link.link0.fault = port_fault & 0x1 ? err : 0;
  5566. cnt++;
  5567. }
  5568. if (bf_get(lpfc_acqe_fc_la_trunk_config_port1, acqe_fc)) {
  5569. phba->trunk_link.link1.state =
  5570. bf_get(lpfc_acqe_fc_la_trunk_link_status_port1, acqe_fc)
  5571. ? LPFC_LINK_UP : LPFC_LINK_DOWN;
  5572. phba->trunk_link.link1.fault = port_fault & 0x2 ? err : 0;
  5573. cnt++;
  5574. }
  5575. if (bf_get(lpfc_acqe_fc_la_trunk_config_port2, acqe_fc)) {
  5576. phba->trunk_link.link2.state =
  5577. bf_get(lpfc_acqe_fc_la_trunk_link_status_port2, acqe_fc)
  5578. ? LPFC_LINK_UP : LPFC_LINK_DOWN;
  5579. phba->trunk_link.link2.fault = port_fault & 0x4 ? err : 0;
  5580. cnt++;
  5581. }
  5582. if (bf_get(lpfc_acqe_fc_la_trunk_config_port3, acqe_fc)) {
  5583. phba->trunk_link.link3.state =
  5584. bf_get(lpfc_acqe_fc_la_trunk_link_status_port3, acqe_fc)
  5585. ? LPFC_LINK_UP : LPFC_LINK_DOWN;
  5586. phba->trunk_link.link3.fault = port_fault & 0x8 ? err : 0;
  5587. cnt++;
  5588. }
  5589. if (cnt)
  5590. phba->trunk_link.phy_lnk_speed =
  5591. phba->sli4_hba.link_state.logical_speed / (cnt * 1000);
  5592. else
  5593. phba->trunk_link.phy_lnk_speed = LPFC_LINK_SPEED_UNKNOWN;
  5594. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  5595. "2910 Async FC Trunking Event - Speed:%d\n"
  5596. "\tLogical speed:%d "
  5597. "port0: %s port1: %s port2: %s port3: %s\n",
  5598. phba->sli4_hba.link_state.speed,
  5599. phba->sli4_hba.link_state.logical_speed,
  5600. trunk_link_status(0), trunk_link_status(1),
  5601. trunk_link_status(2), trunk_link_status(3));
  5602. if (phba->cmf_active_mode != LPFC_CFG_OFF)
  5603. lpfc_cmf_signal_init(phba);
  5604. if (port_fault)
  5605. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  5606. "3202 trunk error:0x%x (%s) seen on port0:%s "
  5607. /*
  5608. * SLI-4: We have only 0xA error codes
  5609. * defined as of now. print an appropriate
  5610. * message in case driver needs to be updated.
  5611. */
  5612. "port1:%s port2:%s port3:%s\n", err, err > 0xA ?
  5613. "UNDEFINED. update driver." : trunk_errmsg[err],
  5614. trunk_port_fault(0), trunk_port_fault(1),
  5615. trunk_port_fault(2), trunk_port_fault(3));
  5616. }
  5617. /**
  5618. * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
  5619. * @phba: pointer to lpfc hba data structure.
  5620. * @acqe_fc: pointer to the async fc completion queue entry.
  5621. *
  5622. * This routine is to handle the SLI4 asynchronous FC event. It will simply log
  5623. * that the event was received and then issue a read_topology mailbox command so
  5624. * that the rest of the driver will treat it the same as SLI3.
  5625. **/
  5626. static void
  5627. lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
  5628. {
  5629. LPFC_MBOXQ_t *pmb;
  5630. MAILBOX_t *mb;
  5631. struct lpfc_mbx_read_top *la;
  5632. char *log_level;
  5633. int rc;
  5634. if (bf_get(lpfc_trailer_type, acqe_fc) !=
  5635. LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
  5636. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  5637. "2895 Non FC link Event detected.(%d)\n",
  5638. bf_get(lpfc_trailer_type, acqe_fc));
  5639. return;
  5640. }
  5641. if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
  5642. LPFC_FC_LA_TYPE_TRUNKING_EVENT) {
  5643. lpfc_update_trunk_link_status(phba, acqe_fc);
  5644. return;
  5645. }
  5646. /* Keep the link status for extra SLI4 state machine reference */
  5647. phba->sli4_hba.link_state.speed =
  5648. lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
  5649. bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
  5650. phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
  5651. phba->sli4_hba.link_state.topology =
  5652. bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
  5653. phba->sli4_hba.link_state.status =
  5654. bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
  5655. phba->sli4_hba.link_state.type =
  5656. bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
  5657. phba->sli4_hba.link_state.number =
  5658. bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
  5659. phba->sli4_hba.link_state.fault =
  5660. bf_get(lpfc_acqe_link_fault, acqe_fc);
  5661. phba->sli4_hba.link_state.link_status =
  5662. bf_get(lpfc_acqe_fc_la_link_status, acqe_fc);
  5663. /*
  5664. * Only select attention types need logical speed modification to what
  5665. * was previously set.
  5666. */
  5667. if (phba->sli4_hba.link_state.status >= LPFC_FC_LA_TYPE_LINK_UP &&
  5668. phba->sli4_hba.link_state.status < LPFC_FC_LA_TYPE_ACTIVATE_FAIL) {
  5669. if (bf_get(lpfc_acqe_fc_la_att_type, acqe_fc) ==
  5670. LPFC_FC_LA_TYPE_LINK_DOWN)
  5671. phba->sli4_hba.link_state.logical_speed = 0;
  5672. else if (!phba->sli4_hba.conf_trunk)
  5673. phba->sli4_hba.link_state.logical_speed =
  5674. bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
  5675. }
  5676. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  5677. "2896 Async FC event - Speed:%dGBaud Topology:x%x "
  5678. "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
  5679. "%dMbps Fault:x%x Link Status:x%x\n",
  5680. phba->sli4_hba.link_state.speed,
  5681. phba->sli4_hba.link_state.topology,
  5682. phba->sli4_hba.link_state.status,
  5683. phba->sli4_hba.link_state.type,
  5684. phba->sli4_hba.link_state.number,
  5685. phba->sli4_hba.link_state.logical_speed,
  5686. phba->sli4_hba.link_state.fault,
  5687. phba->sli4_hba.link_state.link_status);
  5688. /*
  5689. * The following attention types are informational only, providing
  5690. * further details about link status. Overwrite the value of
  5691. * link_state.status appropriately. No further action is required.
  5692. */
  5693. if (phba->sli4_hba.link_state.status >= LPFC_FC_LA_TYPE_ACTIVATE_FAIL) {
  5694. switch (phba->sli4_hba.link_state.status) {
  5695. case LPFC_FC_LA_TYPE_ACTIVATE_FAIL:
  5696. log_level = KERN_WARNING;
  5697. phba->sli4_hba.link_state.status =
  5698. LPFC_FC_LA_TYPE_LINK_DOWN;
  5699. break;
  5700. case LPFC_FC_LA_TYPE_LINK_RESET_PRTCL_EVT:
  5701. /*
  5702. * During bb credit recovery establishment, receiving
  5703. * this attention type is normal. Link Up attention
  5704. * type is expected to occur before this informational
  5705. * attention type so keep the Link Up status.
  5706. */
  5707. log_level = KERN_INFO;
  5708. phba->sli4_hba.link_state.status =
  5709. LPFC_FC_LA_TYPE_LINK_UP;
  5710. break;
  5711. default:
  5712. log_level = KERN_INFO;
  5713. break;
  5714. }
  5715. lpfc_log_msg(phba, log_level, LOG_SLI,
  5716. "2992 Async FC event - Informational Link "
  5717. "Attention Type x%x\n",
  5718. bf_get(lpfc_acqe_fc_la_att_type, acqe_fc));
  5719. return;
  5720. }
  5721. pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5722. if (!pmb) {
  5723. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  5724. "2897 The mboxq allocation failed\n");
  5725. return;
  5726. }
  5727. rc = lpfc_mbox_rsrc_prep(phba, pmb);
  5728. if (rc) {
  5729. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  5730. "2898 The mboxq prep failed\n");
  5731. goto out_free_pmb;
  5732. }
  5733. /* Cleanup any outstanding ELS commands */
  5734. lpfc_els_flush_all_cmd(phba);
  5735. /* Block ELS IOCBs until we have done process link event */
  5736. phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
  5737. /* Update link event statistics */
  5738. phba->sli.slistat.link_event++;
  5739. /* Create lpfc_handle_latt mailbox command from link ACQE */
  5740. lpfc_read_topology(phba, pmb, pmb->ctx_buf);
  5741. pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
  5742. pmb->vport = phba->pport;
  5743. if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
  5744. phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
  5745. switch (phba->sli4_hba.link_state.status) {
  5746. case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
  5747. phba->link_flag |= LS_MDS_LINK_DOWN;
  5748. break;
  5749. case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
  5750. phba->link_flag |= LS_MDS_LOOPBACK;
  5751. break;
  5752. default:
  5753. break;
  5754. }
  5755. /* Initialize completion status */
  5756. mb = &pmb->u.mb;
  5757. mb->mbxStatus = MBX_SUCCESS;
  5758. /* Parse port fault information field */
  5759. lpfc_sli4_parse_latt_fault(phba, (void *)acqe_fc);
  5760. /* Parse and translate link attention fields */
  5761. la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
  5762. la->eventTag = acqe_fc->event_tag;
  5763. if (phba->sli4_hba.link_state.status ==
  5764. LPFC_FC_LA_TYPE_UNEXP_WWPN) {
  5765. bf_set(lpfc_mbx_read_top_att_type, la,
  5766. LPFC_FC_LA_TYPE_UNEXP_WWPN);
  5767. } else {
  5768. bf_set(lpfc_mbx_read_top_att_type, la,
  5769. LPFC_FC_LA_TYPE_LINK_DOWN);
  5770. }
  5771. /* Invoke the mailbox command callback function */
  5772. lpfc_mbx_cmpl_read_topology(phba, pmb);
  5773. return;
  5774. }
  5775. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  5776. if (rc == MBX_NOT_FINISHED)
  5777. goto out_free_pmb;
  5778. return;
  5779. out_free_pmb:
  5780. lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
  5781. }
  5782. /**
  5783. * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
  5784. * @phba: pointer to lpfc hba data structure.
  5785. * @acqe_sli: pointer to the async SLI completion queue entry.
  5786. *
  5787. * This routine is to handle the SLI4 asynchronous SLI events.
  5788. **/
  5789. static void
  5790. lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
  5791. {
  5792. char port_name;
  5793. char message[128];
  5794. uint8_t status;
  5795. uint8_t evt_type;
  5796. uint8_t operational = 0;
  5797. struct temp_event temp_event_data;
  5798. struct lpfc_acqe_misconfigured_event *misconfigured;
  5799. struct lpfc_acqe_cgn_signal *cgn_signal;
  5800. struct Scsi_Host *shost;
  5801. struct lpfc_vport **vports;
  5802. int rc, i, cnt;
  5803. evt_type = bf_get(lpfc_trailer_type, acqe_sli);
  5804. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  5805. "2901 Async SLI event - Type:%d, Event Data: x%08x "
  5806. "x%08x x%08x x%08x\n", evt_type,
  5807. acqe_sli->event_data1, acqe_sli->event_data2,
  5808. acqe_sli->event_data3, acqe_sli->trailer);
  5809. port_name = phba->Port[0];
  5810. if (port_name == 0x00)
  5811. port_name = '?'; /* get port name is empty */
  5812. switch (evt_type) {
  5813. case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
  5814. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  5815. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  5816. temp_event_data.data = (uint32_t)acqe_sli->event_data1;
  5817. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5818. "3190 Over Temperature:%d Celsius- Port Name %c\n",
  5819. acqe_sli->event_data1, port_name);
  5820. phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
  5821. shost = lpfc_shost_from_vport(phba->pport);
  5822. fc_host_post_vendor_event(shost, fc_get_event_number(),
  5823. sizeof(temp_event_data),
  5824. (char *)&temp_event_data,
  5825. SCSI_NL_VID_TYPE_PCI
  5826. | PCI_VENDOR_ID_EMULEX);
  5827. break;
  5828. case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
  5829. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  5830. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  5831. temp_event_data.data = (uint32_t)acqe_sli->event_data1;
  5832. lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_LDS_EVENT,
  5833. "3191 Normal Temperature:%d Celsius - Port Name %c\n",
  5834. acqe_sli->event_data1, port_name);
  5835. shost = lpfc_shost_from_vport(phba->pport);
  5836. fc_host_post_vendor_event(shost, fc_get_event_number(),
  5837. sizeof(temp_event_data),
  5838. (char *)&temp_event_data,
  5839. SCSI_NL_VID_TYPE_PCI
  5840. | PCI_VENDOR_ID_EMULEX);
  5841. break;
  5842. case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
  5843. misconfigured = (struct lpfc_acqe_misconfigured_event *)
  5844. &acqe_sli->event_data1;
  5845. /* fetch the status for this port */
  5846. switch (phba->sli4_hba.lnk_info.lnk_no) {
  5847. case LPFC_LINK_NUMBER_0:
  5848. status = bf_get(lpfc_sli_misconfigured_port0_state,
  5849. &misconfigured->theEvent);
  5850. operational = bf_get(lpfc_sli_misconfigured_port0_op,
  5851. &misconfigured->theEvent);
  5852. break;
  5853. case LPFC_LINK_NUMBER_1:
  5854. status = bf_get(lpfc_sli_misconfigured_port1_state,
  5855. &misconfigured->theEvent);
  5856. operational = bf_get(lpfc_sli_misconfigured_port1_op,
  5857. &misconfigured->theEvent);
  5858. break;
  5859. case LPFC_LINK_NUMBER_2:
  5860. status = bf_get(lpfc_sli_misconfigured_port2_state,
  5861. &misconfigured->theEvent);
  5862. operational = bf_get(lpfc_sli_misconfigured_port2_op,
  5863. &misconfigured->theEvent);
  5864. break;
  5865. case LPFC_LINK_NUMBER_3:
  5866. status = bf_get(lpfc_sli_misconfigured_port3_state,
  5867. &misconfigured->theEvent);
  5868. operational = bf_get(lpfc_sli_misconfigured_port3_op,
  5869. &misconfigured->theEvent);
  5870. break;
  5871. default:
  5872. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  5873. "3296 "
  5874. "LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
  5875. "event: Invalid link %d",
  5876. phba->sli4_hba.lnk_info.lnk_no);
  5877. return;
  5878. }
  5879. /* Skip if optic state unchanged */
  5880. if (phba->sli4_hba.lnk_info.optic_state == status)
  5881. return;
  5882. switch (status) {
  5883. case LPFC_SLI_EVENT_STATUS_VALID:
  5884. sprintf(message, "Physical Link is functional");
  5885. break;
  5886. case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
  5887. sprintf(message, "Optics faulted/incorrectly "
  5888. "installed/not installed - Reseat optics, "
  5889. "if issue not resolved, replace.");
  5890. break;
  5891. case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
  5892. sprintf(message,
  5893. "Optics of two types installed - Remove one "
  5894. "optic or install matching pair of optics.");
  5895. break;
  5896. case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
  5897. sprintf(message, "Incompatible optics - Replace with "
  5898. "compatible optics for card to function.");
  5899. break;
  5900. case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
  5901. sprintf(message, "Unqualified optics - Replace with "
  5902. "Avago optics for Warranty and Technical "
  5903. "Support - Link is%s operational",
  5904. (operational) ? " not" : "");
  5905. break;
  5906. case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
  5907. sprintf(message, "Uncertified optics - Replace with "
  5908. "Avago-certified optics to enable link "
  5909. "operation - Link is%s operational",
  5910. (operational) ? " not" : "");
  5911. break;
  5912. default:
  5913. /* firmware is reporting a status we don't know about */
  5914. sprintf(message, "Unknown event status x%02x", status);
  5915. break;
  5916. }
  5917. /* Issue READ_CONFIG mbox command to refresh supported speeds */
  5918. rc = lpfc_sli4_read_config(phba);
  5919. if (rc) {
  5920. phba->lmt = 0;
  5921. lpfc_printf_log(phba, KERN_ERR,
  5922. LOG_TRACE_EVENT,
  5923. "3194 Unable to retrieve supported "
  5924. "speeds, rc = 0x%x\n", rc);
  5925. }
  5926. rc = lpfc_sli4_refresh_params(phba);
  5927. if (rc) {
  5928. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5929. "3174 Unable to update pls support, "
  5930. "rc x%x\n", rc);
  5931. }
  5932. vports = lpfc_create_vport_work_array(phba);
  5933. if (vports != NULL) {
  5934. for (i = 0; i <= phba->max_vports && vports[i] != NULL;
  5935. i++) {
  5936. shost = lpfc_shost_from_vport(vports[i]);
  5937. lpfc_host_supported_speeds_set(shost);
  5938. }
  5939. }
  5940. lpfc_destroy_vport_work_array(phba, vports);
  5941. phba->sli4_hba.lnk_info.optic_state = status;
  5942. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5943. "3176 Port Name %c %s\n", port_name, message);
  5944. break;
  5945. case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
  5946. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  5947. "3192 Remote DPort Test Initiated - "
  5948. "Event Data1:x%08x Event Data2: x%08x\n",
  5949. acqe_sli->event_data1, acqe_sli->event_data2);
  5950. break;
  5951. case LPFC_SLI_EVENT_TYPE_PORT_PARAMS_CHG:
  5952. /* Call FW to obtain active parms */
  5953. lpfc_sli4_cgn_parm_chg_evt(phba);
  5954. break;
  5955. case LPFC_SLI_EVENT_TYPE_MISCONF_FAWWN:
  5956. /* Misconfigured WWN. Reports that the SLI Port is configured
  5957. * to use FA-WWN, but the attached device doesn’t support it.
  5958. * Event Data1 - N.A, Event Data2 - N.A
  5959. * This event only happens on the physical port.
  5960. */
  5961. lpfc_log_msg(phba, KERN_WARNING, LOG_SLI | LOG_DISCOVERY,
  5962. "2699 Misconfigured FA-PWWN - Attached device "
  5963. "does not support FA-PWWN\n");
  5964. phba->sli4_hba.fawwpn_flag &= ~LPFC_FAWWPN_FABRIC;
  5965. memset(phba->pport->fc_portname.u.wwn, 0,
  5966. sizeof(struct lpfc_name));
  5967. break;
  5968. case LPFC_SLI_EVENT_TYPE_EEPROM_FAILURE:
  5969. /* EEPROM failure. No driver action is required */
  5970. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5971. "2518 EEPROM failure - "
  5972. "Event Data1: x%08x Event Data2: x%08x\n",
  5973. acqe_sli->event_data1, acqe_sli->event_data2);
  5974. break;
  5975. case LPFC_SLI_EVENT_TYPE_CGN_SIGNAL:
  5976. if (phba->cmf_active_mode == LPFC_CFG_OFF)
  5977. break;
  5978. cgn_signal = (struct lpfc_acqe_cgn_signal *)
  5979. &acqe_sli->event_data1;
  5980. phba->cgn_acqe_cnt++;
  5981. cnt = bf_get(lpfc_warn_acqe, cgn_signal);
  5982. atomic64_add(cnt, &phba->cgn_acqe_stat.warn);
  5983. atomic64_add(cgn_signal->alarm_cnt, &phba->cgn_acqe_stat.alarm);
  5984. /* no threshold for CMF, even 1 signal will trigger an event */
  5985. /* Alarm overrides warning, so check that first */
  5986. if (cgn_signal->alarm_cnt) {
  5987. if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
  5988. /* Keep track of alarm cnt for CMF_SYNC_WQE */
  5989. atomic_add(cgn_signal->alarm_cnt,
  5990. &phba->cgn_sync_alarm_cnt);
  5991. }
  5992. } else if (cnt) {
  5993. /* signal action needs to be taken */
  5994. if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY ||
  5995. phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
  5996. /* Keep track of warning cnt for CMF_SYNC_WQE */
  5997. atomic_add(cnt, &phba->cgn_sync_warn_cnt);
  5998. }
  5999. }
  6000. break;
  6001. case LPFC_SLI_EVENT_TYPE_RD_SIGNAL:
  6002. /* May be accompanied by a temperature event */
  6003. lpfc_printf_log(phba, KERN_INFO,
  6004. LOG_SLI | LOG_LINK_EVENT | LOG_LDS_EVENT,
  6005. "2902 Remote Degrade Signaling: x%08x x%08x "
  6006. "x%08x\n",
  6007. acqe_sli->event_data1, acqe_sli->event_data2,
  6008. acqe_sli->event_data3);
  6009. break;
  6010. case LPFC_SLI_EVENT_TYPE_RESET_CM_STATS:
  6011. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
  6012. "2905 Reset CM statistics\n");
  6013. lpfc_sli4_async_cmstat_evt(phba);
  6014. break;
  6015. default:
  6016. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  6017. "3193 Unrecognized SLI event, type: 0x%x",
  6018. evt_type);
  6019. break;
  6020. }
  6021. }
  6022. /**
  6023. * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
  6024. * @vport: pointer to vport data structure.
  6025. *
  6026. * This routine is to perform Clear Virtual Link (CVL) on a vport in
  6027. * response to a CVL event.
  6028. *
  6029. * Return the pointer to the ndlp with the vport if successful, otherwise
  6030. * return NULL.
  6031. **/
  6032. static struct lpfc_nodelist *
  6033. lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
  6034. {
  6035. struct lpfc_nodelist *ndlp;
  6036. struct Scsi_Host *shost;
  6037. struct lpfc_hba *phba;
  6038. if (!vport)
  6039. return NULL;
  6040. phba = vport->phba;
  6041. if (!phba)
  6042. return NULL;
  6043. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  6044. if (!ndlp) {
  6045. /* Cannot find existing Fabric ndlp, so allocate a new one */
  6046. ndlp = lpfc_nlp_init(vport, Fabric_DID);
  6047. if (!ndlp)
  6048. return NULL;
  6049. /* Set the node type */
  6050. ndlp->nlp_type |= NLP_FABRIC;
  6051. /* Put ndlp onto node list */
  6052. lpfc_enqueue_node(vport, ndlp);
  6053. }
  6054. if ((phba->pport->port_state < LPFC_FLOGI) &&
  6055. (phba->pport->port_state != LPFC_VPORT_FAILED))
  6056. return NULL;
  6057. /* If virtual link is not yet instantiated ignore CVL */
  6058. if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
  6059. && (vport->port_state != LPFC_VPORT_FAILED))
  6060. return NULL;
  6061. shost = lpfc_shost_from_vport(vport);
  6062. if (!shost)
  6063. return NULL;
  6064. lpfc_linkdown_port(vport);
  6065. lpfc_cleanup_pending_mbox(vport);
  6066. set_bit(FC_VPORT_CVL_RCVD, &vport->fc_flag);
  6067. return ndlp;
  6068. }
  6069. /**
  6070. * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
  6071. * @phba: pointer to lpfc hba data structure.
  6072. *
  6073. * This routine is to perform Clear Virtual Link (CVL) on all vports in
  6074. * response to a FCF dead event.
  6075. **/
  6076. static void
  6077. lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
  6078. {
  6079. struct lpfc_vport **vports;
  6080. int i;
  6081. vports = lpfc_create_vport_work_array(phba);
  6082. if (vports)
  6083. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
  6084. lpfc_sli4_perform_vport_cvl(vports[i]);
  6085. lpfc_destroy_vport_work_array(phba, vports);
  6086. }
  6087. /**
  6088. * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
  6089. * @phba: pointer to lpfc hba data structure.
  6090. * @acqe_fip: pointer to the async fcoe completion queue entry.
  6091. *
  6092. * This routine is to handle the SLI4 asynchronous fcoe event.
  6093. **/
  6094. static void
  6095. lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
  6096. struct lpfc_acqe_fip *acqe_fip)
  6097. {
  6098. uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
  6099. int rc;
  6100. struct lpfc_vport *vport;
  6101. struct lpfc_nodelist *ndlp;
  6102. int active_vlink_present;
  6103. struct lpfc_vport **vports;
  6104. int i;
  6105. phba->fc_eventTag = acqe_fip->event_tag;
  6106. phba->fcoe_eventtag = acqe_fip->event_tag;
  6107. switch (event_type) {
  6108. case LPFC_FIP_EVENT_TYPE_NEW_FCF:
  6109. case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
  6110. if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
  6111. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  6112. "2546 New FCF event, evt_tag:x%x, "
  6113. "index:x%x\n",
  6114. acqe_fip->event_tag,
  6115. acqe_fip->index);
  6116. else
  6117. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
  6118. LOG_DISCOVERY,
  6119. "2788 FCF param modified event, "
  6120. "evt_tag:x%x, index:x%x\n",
  6121. acqe_fip->event_tag,
  6122. acqe_fip->index);
  6123. if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
  6124. /*
  6125. * During period of FCF discovery, read the FCF
  6126. * table record indexed by the event to update
  6127. * FCF roundrobin failover eligible FCF bmask.
  6128. */
  6129. lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
  6130. LOG_DISCOVERY,
  6131. "2779 Read FCF (x%x) for updating "
  6132. "roundrobin FCF failover bmask\n",
  6133. acqe_fip->index);
  6134. rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
  6135. }
  6136. /* If the FCF discovery is in progress, do nothing. */
  6137. if (test_bit(FCF_TS_INPROG, &phba->hba_flag))
  6138. break;
  6139. spin_lock_irq(&phba->hbalock);
  6140. /* If fast FCF failover rescan event is pending, do nothing */
  6141. if (phba->fcf.fcf_flag & (FCF_REDISC_EVT | FCF_REDISC_PEND)) {
  6142. spin_unlock_irq(&phba->hbalock);
  6143. break;
  6144. }
  6145. /* If the FCF has been in discovered state, do nothing. */
  6146. if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
  6147. spin_unlock_irq(&phba->hbalock);
  6148. break;
  6149. }
  6150. spin_unlock_irq(&phba->hbalock);
  6151. /* Otherwise, scan the entire FCF table and re-discover SAN */
  6152. lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
  6153. "2770 Start FCF table scan per async FCF "
  6154. "event, evt_tag:x%x, index:x%x\n",
  6155. acqe_fip->event_tag, acqe_fip->index);
  6156. rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
  6157. LPFC_FCOE_FCF_GET_FIRST);
  6158. if (rc)
  6159. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  6160. "2547 Issue FCF scan read FCF mailbox "
  6161. "command failed (x%x)\n", rc);
  6162. break;
  6163. case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
  6164. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  6165. "2548 FCF Table full count 0x%x tag 0x%x\n",
  6166. bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
  6167. acqe_fip->event_tag);
  6168. break;
  6169. case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
  6170. phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
  6171. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  6172. "2549 FCF (x%x) disconnected from network, "
  6173. "tag:x%x\n", acqe_fip->index,
  6174. acqe_fip->event_tag);
  6175. /*
  6176. * If we are in the middle of FCF failover process, clear
  6177. * the corresponding FCF bit in the roundrobin bitmap.
  6178. */
  6179. spin_lock_irq(&phba->hbalock);
  6180. if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
  6181. (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
  6182. spin_unlock_irq(&phba->hbalock);
  6183. /* Update FLOGI FCF failover eligible FCF bmask */
  6184. lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
  6185. break;
  6186. }
  6187. spin_unlock_irq(&phba->hbalock);
  6188. /* If the event is not for currently used fcf do nothing */
  6189. if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
  6190. break;
  6191. /*
  6192. * Otherwise, request the port to rediscover the entire FCF
  6193. * table for a fast recovery from case that the current FCF
  6194. * is no longer valid as we are not in the middle of FCF
  6195. * failover process already.
  6196. */
  6197. spin_lock_irq(&phba->hbalock);
  6198. /* Mark the fast failover process in progress */
  6199. phba->fcf.fcf_flag |= FCF_DEAD_DISC;
  6200. spin_unlock_irq(&phba->hbalock);
  6201. lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
  6202. "2771 Start FCF fast failover process due to "
  6203. "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
  6204. "\n", acqe_fip->event_tag, acqe_fip->index);
  6205. rc = lpfc_sli4_redisc_fcf_table(phba);
  6206. if (rc) {
  6207. lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
  6208. LOG_TRACE_EVENT,
  6209. "2772 Issue FCF rediscover mailbox "
  6210. "command failed, fail through to FCF "
  6211. "dead event\n");
  6212. spin_lock_irq(&phba->hbalock);
  6213. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  6214. spin_unlock_irq(&phba->hbalock);
  6215. /*
  6216. * Last resort will fail over by treating this
  6217. * as a link down to FCF registration.
  6218. */
  6219. lpfc_sli4_fcf_dead_failthrough(phba);
  6220. } else {
  6221. /* Reset FCF roundrobin bmask for new discovery */
  6222. lpfc_sli4_clear_fcf_rr_bmask(phba);
  6223. /*
  6224. * Handling fast FCF failover to a DEAD FCF event is
  6225. * considered equalivant to receiving CVL to all vports.
  6226. */
  6227. lpfc_sli4_perform_all_vport_cvl(phba);
  6228. }
  6229. break;
  6230. case LPFC_FIP_EVENT_TYPE_CVL:
  6231. phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
  6232. lpfc_printf_log(phba, KERN_ERR,
  6233. LOG_TRACE_EVENT,
  6234. "2718 Clear Virtual Link Received for VPI 0x%x"
  6235. " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
  6236. vport = lpfc_find_vport_by_vpid(phba,
  6237. acqe_fip->index);
  6238. ndlp = lpfc_sli4_perform_vport_cvl(vport);
  6239. if (!ndlp)
  6240. break;
  6241. active_vlink_present = 0;
  6242. vports = lpfc_create_vport_work_array(phba);
  6243. if (vports) {
  6244. for (i = 0; i <= phba->max_vports && vports[i] != NULL;
  6245. i++) {
  6246. if (!test_bit(FC_VPORT_CVL_RCVD,
  6247. &vports[i]->fc_flag) &&
  6248. vports[i]->port_state > LPFC_FDISC) {
  6249. active_vlink_present = 1;
  6250. break;
  6251. }
  6252. }
  6253. lpfc_destroy_vport_work_array(phba, vports);
  6254. }
  6255. /*
  6256. * Don't re-instantiate if vport is marked for deletion.
  6257. * If we are here first then vport_delete is going to wait
  6258. * for discovery to complete.
  6259. */
  6260. if (!test_bit(FC_UNLOADING, &vport->load_flag) &&
  6261. active_vlink_present) {
  6262. /*
  6263. * If there are other active VLinks present,
  6264. * re-instantiate the Vlink using FDISC.
  6265. */
  6266. mod_timer(&ndlp->nlp_delayfunc,
  6267. jiffies + msecs_to_jiffies(1000));
  6268. spin_lock_irq(&ndlp->lock);
  6269. ndlp->nlp_flag |= NLP_DELAY_TMO;
  6270. spin_unlock_irq(&ndlp->lock);
  6271. ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
  6272. vport->port_state = LPFC_FDISC;
  6273. } else {
  6274. /*
  6275. * Otherwise, we request port to rediscover
  6276. * the entire FCF table for a fast recovery
  6277. * from possible case that the current FCF
  6278. * is no longer valid if we are not already
  6279. * in the FCF failover process.
  6280. */
  6281. spin_lock_irq(&phba->hbalock);
  6282. if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
  6283. spin_unlock_irq(&phba->hbalock);
  6284. break;
  6285. }
  6286. /* Mark the fast failover process in progress */
  6287. phba->fcf.fcf_flag |= FCF_ACVL_DISC;
  6288. spin_unlock_irq(&phba->hbalock);
  6289. lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
  6290. LOG_DISCOVERY,
  6291. "2773 Start FCF failover per CVL, "
  6292. "evt_tag:x%x\n", acqe_fip->event_tag);
  6293. rc = lpfc_sli4_redisc_fcf_table(phba);
  6294. if (rc) {
  6295. lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
  6296. LOG_TRACE_EVENT,
  6297. "2774 Issue FCF rediscover "
  6298. "mailbox command failed, "
  6299. "through to CVL event\n");
  6300. spin_lock_irq(&phba->hbalock);
  6301. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  6302. spin_unlock_irq(&phba->hbalock);
  6303. /*
  6304. * Last resort will be re-try on the
  6305. * the current registered FCF entry.
  6306. */
  6307. lpfc_retry_pport_discovery(phba);
  6308. } else
  6309. /*
  6310. * Reset FCF roundrobin bmask for new
  6311. * discovery.
  6312. */
  6313. lpfc_sli4_clear_fcf_rr_bmask(phba);
  6314. }
  6315. break;
  6316. default:
  6317. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  6318. "0288 Unknown FCoE event type 0x%x event tag "
  6319. "0x%x\n", event_type, acqe_fip->event_tag);
  6320. break;
  6321. }
  6322. }
  6323. /**
  6324. * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
  6325. * @phba: pointer to lpfc hba data structure.
  6326. * @acqe_dcbx: pointer to the async dcbx completion queue entry.
  6327. *
  6328. * This routine is to handle the SLI4 asynchronous dcbx event.
  6329. **/
  6330. static void
  6331. lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
  6332. struct lpfc_acqe_dcbx *acqe_dcbx)
  6333. {
  6334. phba->fc_eventTag = acqe_dcbx->event_tag;
  6335. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  6336. "0290 The SLI4 DCBX asynchronous event is not "
  6337. "handled yet\n");
  6338. }
  6339. /**
  6340. * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
  6341. * @phba: pointer to lpfc hba data structure.
  6342. * @acqe_grp5: pointer to the async grp5 completion queue entry.
  6343. *
  6344. * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
  6345. * is an asynchronous notified of a logical link speed change. The Port
  6346. * reports the logical link speed in units of 10Mbps.
  6347. **/
  6348. static void
  6349. lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
  6350. struct lpfc_acqe_grp5 *acqe_grp5)
  6351. {
  6352. uint16_t prev_ll_spd;
  6353. phba->fc_eventTag = acqe_grp5->event_tag;
  6354. phba->fcoe_eventtag = acqe_grp5->event_tag;
  6355. prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
  6356. phba->sli4_hba.link_state.logical_speed =
  6357. (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
  6358. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  6359. "2789 GRP5 Async Event: Updating logical link speed "
  6360. "from %dMbps to %dMbps\n", prev_ll_spd,
  6361. phba->sli4_hba.link_state.logical_speed);
  6362. }
  6363. /**
  6364. * lpfc_sli4_async_cmstat_evt - Process the asynchronous cmstat event
  6365. * @phba: pointer to lpfc hba data structure.
  6366. *
  6367. * This routine is to handle the SLI4 asynchronous cmstat event. A cmstat event
  6368. * is an asynchronous notification of a request to reset CM stats.
  6369. **/
  6370. static void
  6371. lpfc_sli4_async_cmstat_evt(struct lpfc_hba *phba)
  6372. {
  6373. if (!phba->cgn_i)
  6374. return;
  6375. lpfc_init_congestion_stat(phba);
  6376. }
  6377. /**
  6378. * lpfc_cgn_params_val - Validate FW congestion parameters.
  6379. * @phba: pointer to lpfc hba data structure.
  6380. * @p_cfg_param: pointer to FW provided congestion parameters.
  6381. *
  6382. * This routine validates the congestion parameters passed
  6383. * by the FW to the driver via an ACQE event.
  6384. **/
  6385. static void
  6386. lpfc_cgn_params_val(struct lpfc_hba *phba, struct lpfc_cgn_param *p_cfg_param)
  6387. {
  6388. spin_lock_irq(&phba->hbalock);
  6389. if (!lpfc_rangecheck(p_cfg_param->cgn_param_mode, LPFC_CFG_OFF,
  6390. LPFC_CFG_MONITOR)) {
  6391. lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT,
  6392. "6225 CMF mode param out of range: %d\n",
  6393. p_cfg_param->cgn_param_mode);
  6394. p_cfg_param->cgn_param_mode = LPFC_CFG_OFF;
  6395. }
  6396. spin_unlock_irq(&phba->hbalock);
  6397. }
  6398. static const char * const lpfc_cmf_mode_to_str[] = {
  6399. "OFF",
  6400. "MANAGED",
  6401. "MONITOR",
  6402. };
  6403. /**
  6404. * lpfc_cgn_params_parse - Process a FW cong parm change event
  6405. * @phba: pointer to lpfc hba data structure.
  6406. * @p_cgn_param: pointer to a data buffer with the FW cong params.
  6407. * @len: the size of pdata in bytes.
  6408. *
  6409. * This routine validates the congestion management buffer signature
  6410. * from the FW, validates the contents and makes corrections for
  6411. * valid, in-range values. If the signature magic is correct and
  6412. * after parameter validation, the contents are copied to the driver's
  6413. * @phba structure. If the magic is incorrect, an error message is
  6414. * logged.
  6415. **/
  6416. static void
  6417. lpfc_cgn_params_parse(struct lpfc_hba *phba,
  6418. struct lpfc_cgn_param *p_cgn_param, uint32_t len)
  6419. {
  6420. struct lpfc_cgn_info *cp;
  6421. uint32_t crc, oldmode;
  6422. char acr_string[4] = {0};
  6423. /* Make sure the FW has encoded the correct magic number to
  6424. * validate the congestion parameter in FW memory.
  6425. */
  6426. if (p_cgn_param->cgn_param_magic == LPFC_CFG_PARAM_MAGIC_NUM) {
  6427. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT | LOG_INIT,
  6428. "4668 FW cgn parm buffer data: "
  6429. "magic 0x%x version %d mode %d "
  6430. "level0 %d level1 %d "
  6431. "level2 %d byte13 %d "
  6432. "byte14 %d byte15 %d "
  6433. "byte11 %d byte12 %d activeMode %d\n",
  6434. p_cgn_param->cgn_param_magic,
  6435. p_cgn_param->cgn_param_version,
  6436. p_cgn_param->cgn_param_mode,
  6437. p_cgn_param->cgn_param_level0,
  6438. p_cgn_param->cgn_param_level1,
  6439. p_cgn_param->cgn_param_level2,
  6440. p_cgn_param->byte13,
  6441. p_cgn_param->byte14,
  6442. p_cgn_param->byte15,
  6443. p_cgn_param->byte11,
  6444. p_cgn_param->byte12,
  6445. phba->cmf_active_mode);
  6446. oldmode = phba->cmf_active_mode;
  6447. /* Any parameters out of range are corrected to defaults
  6448. * by this routine. No need to fail.
  6449. */
  6450. lpfc_cgn_params_val(phba, p_cgn_param);
  6451. /* Parameters are verified, move them into driver storage */
  6452. spin_lock_irq(&phba->hbalock);
  6453. memcpy(&phba->cgn_p, p_cgn_param,
  6454. sizeof(struct lpfc_cgn_param));
  6455. /* Update parameters in congestion info buffer now */
  6456. if (phba->cgn_i) {
  6457. cp = (struct lpfc_cgn_info *)phba->cgn_i->virt;
  6458. cp->cgn_info_mode = phba->cgn_p.cgn_param_mode;
  6459. cp->cgn_info_level0 = phba->cgn_p.cgn_param_level0;
  6460. cp->cgn_info_level1 = phba->cgn_p.cgn_param_level1;
  6461. cp->cgn_info_level2 = phba->cgn_p.cgn_param_level2;
  6462. crc = lpfc_cgn_calc_crc32(cp, LPFC_CGN_INFO_SZ,
  6463. LPFC_CGN_CRC32_SEED);
  6464. cp->cgn_info_crc = cpu_to_le32(crc);
  6465. }
  6466. spin_unlock_irq(&phba->hbalock);
  6467. phba->cmf_active_mode = phba->cgn_p.cgn_param_mode;
  6468. switch (oldmode) {
  6469. case LPFC_CFG_OFF:
  6470. if (phba->cgn_p.cgn_param_mode != LPFC_CFG_OFF) {
  6471. /* Turning CMF on */
  6472. lpfc_cmf_start(phba);
  6473. if (phba->link_state >= LPFC_LINK_UP) {
  6474. phba->cgn_reg_fpin =
  6475. phba->cgn_init_reg_fpin;
  6476. phba->cgn_reg_signal =
  6477. phba->cgn_init_reg_signal;
  6478. lpfc_issue_els_edc(phba->pport, 0);
  6479. }
  6480. }
  6481. break;
  6482. case LPFC_CFG_MANAGED:
  6483. switch (phba->cgn_p.cgn_param_mode) {
  6484. case LPFC_CFG_OFF:
  6485. /* Turning CMF off */
  6486. lpfc_cmf_stop(phba);
  6487. if (phba->link_state >= LPFC_LINK_UP)
  6488. lpfc_issue_els_edc(phba->pport, 0);
  6489. break;
  6490. case LPFC_CFG_MONITOR:
  6491. phba->cmf_max_bytes_per_interval =
  6492. phba->cmf_link_byte_count;
  6493. /* Resume blocked IO - unblock on workqueue */
  6494. queue_work(phba->wq,
  6495. &phba->unblock_request_work);
  6496. break;
  6497. }
  6498. break;
  6499. case LPFC_CFG_MONITOR:
  6500. switch (phba->cgn_p.cgn_param_mode) {
  6501. case LPFC_CFG_OFF:
  6502. /* Turning CMF off */
  6503. lpfc_cmf_stop(phba);
  6504. if (phba->link_state >= LPFC_LINK_UP)
  6505. lpfc_issue_els_edc(phba->pport, 0);
  6506. break;
  6507. case LPFC_CFG_MANAGED:
  6508. lpfc_cmf_signal_init(phba);
  6509. break;
  6510. }
  6511. break;
  6512. }
  6513. if (oldmode != LPFC_CFG_OFF ||
  6514. oldmode != phba->cgn_p.cgn_param_mode) {
  6515. if (phba->cgn_p.cgn_param_mode == LPFC_CFG_MANAGED)
  6516. scnprintf(acr_string, sizeof(acr_string), "%u",
  6517. phba->cgn_p.cgn_param_level0);
  6518. else
  6519. scnprintf(acr_string, sizeof(acr_string), "NA");
  6520. dev_info(&phba->pcidev->dev, "%d: "
  6521. "4663 CMF: Mode %s acr %s\n",
  6522. phba->brd_no,
  6523. lpfc_cmf_mode_to_str
  6524. [phba->cgn_p.cgn_param_mode],
  6525. acr_string);
  6526. }
  6527. } else {
  6528. lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
  6529. "4669 FW cgn parm buf wrong magic 0x%x "
  6530. "version %d\n", p_cgn_param->cgn_param_magic,
  6531. p_cgn_param->cgn_param_version);
  6532. }
  6533. }
  6534. /**
  6535. * lpfc_sli4_cgn_params_read - Read and Validate FW congestion parameters.
  6536. * @phba: pointer to lpfc hba data structure.
  6537. *
  6538. * This routine issues a read_object mailbox command to
  6539. * get the congestion management parameters from the FW
  6540. * parses it and updates the driver maintained values.
  6541. *
  6542. * Returns
  6543. * 0 if the object was empty
  6544. * -Eval if an error was encountered
  6545. * Count if bytes were read from object
  6546. **/
  6547. int
  6548. lpfc_sli4_cgn_params_read(struct lpfc_hba *phba)
  6549. {
  6550. int ret = 0;
  6551. struct lpfc_cgn_param *p_cgn_param = NULL;
  6552. u32 *pdata = NULL;
  6553. u32 len = 0;
  6554. /* Find out if the FW has a new set of congestion parameters. */
  6555. len = sizeof(struct lpfc_cgn_param);
  6556. pdata = kzalloc(len, GFP_KERNEL);
  6557. if (!pdata)
  6558. return -ENOMEM;
  6559. ret = lpfc_read_object(phba, (char *)LPFC_PORT_CFG_NAME,
  6560. pdata, len);
  6561. /* 0 means no data. A negative means error. A positive means
  6562. * bytes were copied.
  6563. */
  6564. if (!ret) {
  6565. lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
  6566. "4670 CGN RD OBJ returns no data\n");
  6567. goto rd_obj_err;
  6568. } else if (ret < 0) {
  6569. /* Some error. Just exit and return it to the caller.*/
  6570. goto rd_obj_err;
  6571. }
  6572. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT | LOG_INIT,
  6573. "6234 READ CGN PARAMS Successful %d\n", len);
  6574. /* Parse data pointer over len and update the phba congestion
  6575. * parameters with values passed back. The receive rate values
  6576. * may have been altered in FW, but take no action here.
  6577. */
  6578. p_cgn_param = (struct lpfc_cgn_param *)pdata;
  6579. lpfc_cgn_params_parse(phba, p_cgn_param, len);
  6580. rd_obj_err:
  6581. kfree(pdata);
  6582. return ret;
  6583. }
  6584. /**
  6585. * lpfc_sli4_cgn_parm_chg_evt - Process a FW congestion param change event
  6586. * @phba: pointer to lpfc hba data structure.
  6587. *
  6588. * The FW generated Async ACQE SLI event calls this routine when
  6589. * the event type is an SLI Internal Port Event and the Event Code
  6590. * indicates a change to the FW maintained congestion parameters.
  6591. *
  6592. * This routine executes a Read_Object mailbox call to obtain the
  6593. * current congestion parameters maintained in FW and corrects
  6594. * the driver's active congestion parameters.
  6595. *
  6596. * The acqe event is not passed because there is no further data
  6597. * required.
  6598. *
  6599. * Returns nonzero error if event processing encountered an error.
  6600. * Zero otherwise for success.
  6601. **/
  6602. static int
  6603. lpfc_sli4_cgn_parm_chg_evt(struct lpfc_hba *phba)
  6604. {
  6605. int ret = 0;
  6606. if (!phba->sli4_hba.pc_sli4_params.cmf) {
  6607. lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
  6608. "4664 Cgn Evt when E2E off. Drop event\n");
  6609. return -EACCES;
  6610. }
  6611. /* If the event is claiming an empty object, it's ok. A write
  6612. * could have cleared it. Only error is a negative return
  6613. * status.
  6614. */
  6615. ret = lpfc_sli4_cgn_params_read(phba);
  6616. if (ret < 0) {
  6617. lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
  6618. "4667 Error reading Cgn Params (%d)\n",
  6619. ret);
  6620. } else if (!ret) {
  6621. lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
  6622. "4673 CGN Event empty object.\n");
  6623. }
  6624. return ret;
  6625. }
  6626. /**
  6627. * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
  6628. * @phba: pointer to lpfc hba data structure.
  6629. *
  6630. * This routine is invoked by the worker thread to process all the pending
  6631. * SLI4 asynchronous events.
  6632. **/
  6633. void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
  6634. {
  6635. struct lpfc_cq_event *cq_event;
  6636. unsigned long iflags;
  6637. /* First, declare the async event has been handled */
  6638. clear_bit(ASYNC_EVENT, &phba->hba_flag);
  6639. /* Now, handle all the async events */
  6640. spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
  6641. while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
  6642. list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
  6643. cq_event, struct lpfc_cq_event, list);
  6644. spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock,
  6645. iflags);
  6646. /* Process the asynchronous event */
  6647. switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
  6648. case LPFC_TRAILER_CODE_LINK:
  6649. lpfc_sli4_async_link_evt(phba,
  6650. &cq_event->cqe.acqe_link);
  6651. break;
  6652. case LPFC_TRAILER_CODE_FCOE:
  6653. lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
  6654. break;
  6655. case LPFC_TRAILER_CODE_DCBX:
  6656. lpfc_sli4_async_dcbx_evt(phba,
  6657. &cq_event->cqe.acqe_dcbx);
  6658. break;
  6659. case LPFC_TRAILER_CODE_GRP5:
  6660. lpfc_sli4_async_grp5_evt(phba,
  6661. &cq_event->cqe.acqe_grp5);
  6662. break;
  6663. case LPFC_TRAILER_CODE_FC:
  6664. lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
  6665. break;
  6666. case LPFC_TRAILER_CODE_SLI:
  6667. lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
  6668. break;
  6669. default:
  6670. lpfc_printf_log(phba, KERN_ERR,
  6671. LOG_TRACE_EVENT,
  6672. "1804 Invalid asynchronous event code: "
  6673. "x%x\n", bf_get(lpfc_trailer_code,
  6674. &cq_event->cqe.mcqe_cmpl));
  6675. break;
  6676. }
  6677. /* Free the completion event processed to the free pool */
  6678. lpfc_sli4_cq_event_release(phba, cq_event);
  6679. spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
  6680. }
  6681. spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
  6682. }
  6683. /**
  6684. * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
  6685. * @phba: pointer to lpfc hba data structure.
  6686. *
  6687. * This routine is invoked by the worker thread to process FCF table
  6688. * rediscovery pending completion event.
  6689. **/
  6690. void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
  6691. {
  6692. int rc;
  6693. spin_lock_irq(&phba->hbalock);
  6694. /* Clear FCF rediscovery timeout event */
  6695. phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
  6696. /* Clear driver fast failover FCF record flag */
  6697. phba->fcf.failover_rec.flag = 0;
  6698. /* Set state for FCF fast failover */
  6699. phba->fcf.fcf_flag |= FCF_REDISC_FOV;
  6700. spin_unlock_irq(&phba->hbalock);
  6701. /* Scan FCF table from the first entry to re-discover SAN */
  6702. lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
  6703. "2777 Start post-quiescent FCF table scan\n");
  6704. rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
  6705. if (rc)
  6706. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  6707. "2747 Issue FCF scan read FCF mailbox "
  6708. "command failed 0x%x\n", rc);
  6709. }
  6710. /**
  6711. * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
  6712. * @phba: pointer to lpfc hba data structure.
  6713. * @dev_grp: The HBA PCI-Device group number.
  6714. *
  6715. * This routine is invoked to set up the per HBA PCI-Device group function
  6716. * API jump table entries.
  6717. *
  6718. * Return: 0 if success, otherwise -ENODEV
  6719. **/
  6720. int
  6721. lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6722. {
  6723. int rc;
  6724. /* Set up lpfc PCI-device group */
  6725. phba->pci_dev_grp = dev_grp;
  6726. /* The LPFC_PCI_DEV_OC uses SLI4 */
  6727. if (dev_grp == LPFC_PCI_DEV_OC)
  6728. phba->sli_rev = LPFC_SLI_REV4;
  6729. /* Set up device INIT API function jump table */
  6730. rc = lpfc_init_api_table_setup(phba, dev_grp);
  6731. if (rc)
  6732. return -ENODEV;
  6733. /* Set up SCSI API function jump table */
  6734. rc = lpfc_scsi_api_table_setup(phba, dev_grp);
  6735. if (rc)
  6736. return -ENODEV;
  6737. /* Set up SLI API function jump table */
  6738. rc = lpfc_sli_api_table_setup(phba, dev_grp);
  6739. if (rc)
  6740. return -ENODEV;
  6741. /* Set up MBOX API function jump table */
  6742. rc = lpfc_mbox_api_table_setup(phba, dev_grp);
  6743. if (rc)
  6744. return -ENODEV;
  6745. return 0;
  6746. }
  6747. /**
  6748. * lpfc_log_intr_mode - Log the active interrupt mode
  6749. * @phba: pointer to lpfc hba data structure.
  6750. * @intr_mode: active interrupt mode adopted.
  6751. *
  6752. * This routine it invoked to log the currently used active interrupt mode
  6753. * to the device.
  6754. **/
  6755. static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
  6756. {
  6757. switch (intr_mode) {
  6758. case 0:
  6759. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  6760. "0470 Enable INTx interrupt mode.\n");
  6761. break;
  6762. case 1:
  6763. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  6764. "0481 Enabled MSI interrupt mode.\n");
  6765. break;
  6766. case 2:
  6767. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  6768. "0480 Enabled MSI-X interrupt mode.\n");
  6769. break;
  6770. default:
  6771. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  6772. "0482 Illegal interrupt mode.\n");
  6773. break;
  6774. }
  6775. return;
  6776. }
  6777. /**
  6778. * lpfc_enable_pci_dev - Enable a generic PCI device.
  6779. * @phba: pointer to lpfc hba data structure.
  6780. *
  6781. * This routine is invoked to enable the PCI device that is common to all
  6782. * PCI devices.
  6783. *
  6784. * Return codes
  6785. * 0 - successful
  6786. * other values - error
  6787. **/
  6788. static int
  6789. lpfc_enable_pci_dev(struct lpfc_hba *phba)
  6790. {
  6791. struct pci_dev *pdev;
  6792. /* Obtain PCI device reference */
  6793. if (!phba->pcidev)
  6794. goto out_error;
  6795. else
  6796. pdev = phba->pcidev;
  6797. /* Enable PCI device */
  6798. if (pci_enable_device_mem(pdev))
  6799. goto out_error;
  6800. /* Request PCI resource for the device */
  6801. if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
  6802. goto out_disable_device;
  6803. /* Set up device as PCI master and save state for EEH */
  6804. pci_set_master(pdev);
  6805. pci_try_set_mwi(pdev);
  6806. pci_save_state(pdev);
  6807. /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
  6808. if (pci_is_pcie(pdev))
  6809. pdev->needs_freset = 1;
  6810. return 0;
  6811. out_disable_device:
  6812. pci_disable_device(pdev);
  6813. out_error:
  6814. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6815. "1401 Failed to enable pci device\n");
  6816. return -ENODEV;
  6817. }
  6818. /**
  6819. * lpfc_disable_pci_dev - Disable a generic PCI device.
  6820. * @phba: pointer to lpfc hba data structure.
  6821. *
  6822. * This routine is invoked to disable the PCI device that is common to all
  6823. * PCI devices.
  6824. **/
  6825. static void
  6826. lpfc_disable_pci_dev(struct lpfc_hba *phba)
  6827. {
  6828. struct pci_dev *pdev;
  6829. /* Obtain PCI device reference */
  6830. if (!phba->pcidev)
  6831. return;
  6832. else
  6833. pdev = phba->pcidev;
  6834. /* Release PCI resource and disable PCI device */
  6835. pci_release_mem_regions(pdev);
  6836. pci_disable_device(pdev);
  6837. return;
  6838. }
  6839. /**
  6840. * lpfc_reset_hba - Reset a hba
  6841. * @phba: pointer to lpfc hba data structure.
  6842. *
  6843. * This routine is invoked to reset a hba device. It brings the HBA
  6844. * offline, performs a board restart, and then brings the board back
  6845. * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
  6846. * on outstanding mailbox commands.
  6847. **/
  6848. void
  6849. lpfc_reset_hba(struct lpfc_hba *phba)
  6850. {
  6851. int rc = 0;
  6852. /* If resets are disabled then set error state and return. */
  6853. if (!phba->cfg_enable_hba_reset) {
  6854. phba->link_state = LPFC_HBA_ERROR;
  6855. return;
  6856. }
  6857. /* If not LPFC_SLI_ACTIVE, force all IO to be flushed */
  6858. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE) {
  6859. lpfc_offline_prep(phba, LPFC_MBX_WAIT);
  6860. } else {
  6861. if (test_bit(MBX_TMO_ERR, &phba->bit_flags)) {
  6862. /* Perform a PCI function reset to start from clean */
  6863. rc = lpfc_pci_function_reset(phba);
  6864. lpfc_els_flush_all_cmd(phba);
  6865. }
  6866. lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
  6867. lpfc_sli_flush_io_rings(phba);
  6868. }
  6869. lpfc_offline(phba);
  6870. clear_bit(MBX_TMO_ERR, &phba->bit_flags);
  6871. if (unlikely(rc)) {
  6872. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  6873. "8888 PCI function reset failed rc %x\n",
  6874. rc);
  6875. } else {
  6876. lpfc_sli_brdrestart(phba);
  6877. lpfc_online(phba);
  6878. lpfc_unblock_mgmt_io(phba);
  6879. }
  6880. }
  6881. /**
  6882. * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
  6883. * @phba: pointer to lpfc hba data structure.
  6884. *
  6885. * This function enables the PCI SR-IOV virtual functions to a physical
  6886. * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
  6887. * enable the number of virtual functions to the physical function. As
  6888. * not all devices support SR-IOV, the return code from the pci_enable_sriov()
  6889. * API call does not considered as an error condition for most of the device.
  6890. **/
  6891. uint16_t
  6892. lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
  6893. {
  6894. struct pci_dev *pdev = phba->pcidev;
  6895. uint16_t nr_virtfn;
  6896. int pos;
  6897. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
  6898. if (pos == 0)
  6899. return 0;
  6900. pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
  6901. return nr_virtfn;
  6902. }
  6903. /**
  6904. * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
  6905. * @phba: pointer to lpfc hba data structure.
  6906. * @nr_vfn: number of virtual functions to be enabled.
  6907. *
  6908. * This function enables the PCI SR-IOV virtual functions to a physical
  6909. * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
  6910. * enable the number of virtual functions to the physical function. As
  6911. * not all devices support SR-IOV, the return code from the pci_enable_sriov()
  6912. * API call does not considered as an error condition for most of the device.
  6913. **/
  6914. int
  6915. lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
  6916. {
  6917. struct pci_dev *pdev = phba->pcidev;
  6918. uint16_t max_nr_vfn;
  6919. int rc;
  6920. max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
  6921. if (nr_vfn > max_nr_vfn) {
  6922. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  6923. "3057 Requested vfs (%d) greater than "
  6924. "supported vfs (%d)", nr_vfn, max_nr_vfn);
  6925. return -EINVAL;
  6926. }
  6927. rc = pci_enable_sriov(pdev, nr_vfn);
  6928. if (rc) {
  6929. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  6930. "2806 Failed to enable sriov on this device "
  6931. "with vfn number nr_vf:%d, rc:%d\n",
  6932. nr_vfn, rc);
  6933. } else
  6934. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  6935. "2807 Successful enable sriov on this device "
  6936. "with vfn number nr_vf:%d\n", nr_vfn);
  6937. return rc;
  6938. }
  6939. static void
  6940. lpfc_unblock_requests_work(struct work_struct *work)
  6941. {
  6942. struct lpfc_hba *phba = container_of(work, struct lpfc_hba,
  6943. unblock_request_work);
  6944. lpfc_unblock_requests(phba);
  6945. }
  6946. /**
  6947. * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
  6948. * @phba: pointer to lpfc hba data structure.
  6949. *
  6950. * This routine is invoked to set up the driver internal resources before the
  6951. * device specific resource setup to support the HBA device it attached to.
  6952. *
  6953. * Return codes
  6954. * 0 - successful
  6955. * other values - error
  6956. **/
  6957. static int
  6958. lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
  6959. {
  6960. struct lpfc_sli *psli = &phba->sli;
  6961. /*
  6962. * Driver resources common to all SLI revisions
  6963. */
  6964. atomic_set(&phba->fast_event_count, 0);
  6965. atomic_set(&phba->dbg_log_idx, 0);
  6966. atomic_set(&phba->dbg_log_cnt, 0);
  6967. atomic_set(&phba->dbg_log_dmping, 0);
  6968. spin_lock_init(&phba->hbalock);
  6969. /* Initialize port_list spinlock */
  6970. spin_lock_init(&phba->port_list_lock);
  6971. INIT_LIST_HEAD(&phba->port_list);
  6972. INIT_LIST_HEAD(&phba->work_list);
  6973. /* Initialize the wait queue head for the kernel thread */
  6974. init_waitqueue_head(&phba->work_waitq);
  6975. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  6976. "1403 Protocols supported %s %s %s\n",
  6977. ((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
  6978. "SCSI" : " "),
  6979. ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
  6980. "NVME" : " "),
  6981. (phba->nvmet_support ? "NVMET" : " "));
  6982. /* ras_fwlog state */
  6983. spin_lock_init(&phba->ras_fwlog_lock);
  6984. /* Initialize the IO buffer list used by driver for SLI3 SCSI */
  6985. spin_lock_init(&phba->scsi_buf_list_get_lock);
  6986. INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
  6987. spin_lock_init(&phba->scsi_buf_list_put_lock);
  6988. INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
  6989. /* Initialize the fabric iocb list */
  6990. INIT_LIST_HEAD(&phba->fabric_iocb_list);
  6991. /* Initialize list to save ELS buffers */
  6992. INIT_LIST_HEAD(&phba->elsbuf);
  6993. /* Initialize FCF connection rec list */
  6994. INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
  6995. /* Initialize OAS configuration list */
  6996. spin_lock_init(&phba->devicelock);
  6997. INIT_LIST_HEAD(&phba->luns);
  6998. /* MBOX heartbeat timer */
  6999. timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
  7000. /* Fabric block timer */
  7001. timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
  7002. /* EA polling mode timer */
  7003. timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
  7004. /* Heartbeat timer */
  7005. timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
  7006. INIT_DELAYED_WORK(&phba->eq_delay_work, lpfc_hb_eq_delay_work);
  7007. INIT_DELAYED_WORK(&phba->idle_stat_delay_work,
  7008. lpfc_idle_stat_delay_work);
  7009. INIT_WORK(&phba->unblock_request_work, lpfc_unblock_requests_work);
  7010. return 0;
  7011. }
  7012. /**
  7013. * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
  7014. * @phba: pointer to lpfc hba data structure.
  7015. *
  7016. * This routine is invoked to set up the driver internal resources specific to
  7017. * support the SLI-3 HBA device it attached to.
  7018. *
  7019. * Return codes
  7020. * 0 - successful
  7021. * other values - error
  7022. **/
  7023. static int
  7024. lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
  7025. {
  7026. int rc, entry_sz;
  7027. /*
  7028. * Initialize timers used by driver
  7029. */
  7030. /* FCP polling mode timer */
  7031. timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
  7032. /* Host attention work mask setup */
  7033. phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
  7034. phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
  7035. /* Get all the module params for configuring this host */
  7036. lpfc_get_cfgparam(phba);
  7037. /* Set up phase-1 common device driver resources */
  7038. rc = lpfc_setup_driver_resource_phase1(phba);
  7039. if (rc)
  7040. return -ENODEV;
  7041. if (!phba->sli.sli3_ring)
  7042. phba->sli.sli3_ring = kcalloc(LPFC_SLI3_MAX_RING,
  7043. sizeof(struct lpfc_sli_ring),
  7044. GFP_KERNEL);
  7045. if (!phba->sli.sli3_ring)
  7046. return -ENOMEM;
  7047. /*
  7048. * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
  7049. * used to create the sg_dma_buf_pool must be dynamically calculated.
  7050. */
  7051. if (phba->sli_rev == LPFC_SLI_REV4)
  7052. entry_sz = sizeof(struct sli4_sge);
  7053. else
  7054. entry_sz = sizeof(struct ulp_bde64);
  7055. /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
  7056. if (phba->cfg_enable_bg) {
  7057. /*
  7058. * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
  7059. * the FCP rsp, and a BDE for each. Sice we have no control
  7060. * over how many protection data segments the SCSI Layer
  7061. * will hand us (ie: there could be one for every block
  7062. * in the IO), we just allocate enough BDEs to accomidate
  7063. * our max amount and we need to limit lpfc_sg_seg_cnt to
  7064. * minimize the risk of running out.
  7065. */
  7066. phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
  7067. sizeof(struct fcp_rsp) +
  7068. (LPFC_MAX_SG_SEG_CNT * entry_sz);
  7069. if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
  7070. phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
  7071. /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
  7072. phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
  7073. } else {
  7074. /*
  7075. * The scsi_buf for a regular I/O will hold the FCP cmnd,
  7076. * the FCP rsp, a BDE for each, and a BDE for up to
  7077. * cfg_sg_seg_cnt data segments.
  7078. */
  7079. phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
  7080. sizeof(struct fcp_rsp) +
  7081. ((phba->cfg_sg_seg_cnt + 2) * entry_sz);
  7082. /* Total BDEs in BPL for scsi_sg_list */
  7083. phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
  7084. }
  7085. lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
  7086. "9088 INIT sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
  7087. phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
  7088. phba->cfg_total_seg_cnt);
  7089. phba->max_vpi = LPFC_MAX_VPI;
  7090. /* This will be set to correct value after config_port mbox */
  7091. phba->max_vports = 0;
  7092. /*
  7093. * Initialize the SLI Layer to run with lpfc HBAs.
  7094. */
  7095. lpfc_sli_setup(phba);
  7096. lpfc_sli_queue_init(phba);
  7097. /* Allocate device driver memory */
  7098. if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
  7099. return -ENOMEM;
  7100. phba->lpfc_sg_dma_buf_pool =
  7101. dma_pool_create("lpfc_sg_dma_buf_pool",
  7102. &phba->pcidev->dev, phba->cfg_sg_dma_buf_size,
  7103. BPL_ALIGN_SZ, 0);
  7104. if (!phba->lpfc_sg_dma_buf_pool)
  7105. goto fail_free_mem;
  7106. phba->lpfc_cmd_rsp_buf_pool =
  7107. dma_pool_create("lpfc_cmd_rsp_buf_pool",
  7108. &phba->pcidev->dev,
  7109. sizeof(struct fcp_cmnd) +
  7110. sizeof(struct fcp_rsp),
  7111. BPL_ALIGN_SZ, 0);
  7112. if (!phba->lpfc_cmd_rsp_buf_pool)
  7113. goto fail_free_dma_buf_pool;
  7114. /*
  7115. * Enable sr-iov virtual functions if supported and configured
  7116. * through the module parameter.
  7117. */
  7118. if (phba->cfg_sriov_nr_virtfn > 0) {
  7119. rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
  7120. phba->cfg_sriov_nr_virtfn);
  7121. if (rc) {
  7122. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  7123. "2808 Requested number of SR-IOV "
  7124. "virtual functions (%d) is not "
  7125. "supported\n",
  7126. phba->cfg_sriov_nr_virtfn);
  7127. phba->cfg_sriov_nr_virtfn = 0;
  7128. }
  7129. }
  7130. return 0;
  7131. fail_free_dma_buf_pool:
  7132. dma_pool_destroy(phba->lpfc_sg_dma_buf_pool);
  7133. phba->lpfc_sg_dma_buf_pool = NULL;
  7134. fail_free_mem:
  7135. lpfc_mem_free(phba);
  7136. return -ENOMEM;
  7137. }
  7138. /**
  7139. * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
  7140. * @phba: pointer to lpfc hba data structure.
  7141. *
  7142. * This routine is invoked to unset the driver internal resources set up
  7143. * specific for supporting the SLI-3 HBA device it attached to.
  7144. **/
  7145. static void
  7146. lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
  7147. {
  7148. /* Free device driver memory allocated */
  7149. lpfc_mem_free_all(phba);
  7150. return;
  7151. }
  7152. /**
  7153. * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
  7154. * @phba: pointer to lpfc hba data structure.
  7155. *
  7156. * This routine is invoked to set up the driver internal resources specific to
  7157. * support the SLI-4 HBA device it attached to.
  7158. *
  7159. * Return codes
  7160. * 0 - successful
  7161. * other values - error
  7162. **/
  7163. static int
  7164. lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
  7165. {
  7166. LPFC_MBOXQ_t *mboxq;
  7167. MAILBOX_t *mb;
  7168. int rc, i, max_buf_size;
  7169. int longs;
  7170. int extra;
  7171. uint64_t wwn;
  7172. u32 if_type;
  7173. u32 if_fam;
  7174. phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
  7175. phba->sli4_hba.num_possible_cpu = cpumask_last(cpu_possible_mask) + 1;
  7176. phba->sli4_hba.curr_disp_cpu = 0;
  7177. /* Get all the module params for configuring this host */
  7178. lpfc_get_cfgparam(phba);
  7179. /* Set up phase-1 common device driver resources */
  7180. rc = lpfc_setup_driver_resource_phase1(phba);
  7181. if (rc)
  7182. return -ENODEV;
  7183. /* Before proceed, wait for POST done and device ready */
  7184. rc = lpfc_sli4_post_status_check(phba);
  7185. if (rc)
  7186. return -ENODEV;
  7187. /* Allocate all driver workqueues here */
  7188. /* The lpfc_wq workqueue for deferred irq use */
  7189. phba->wq = alloc_workqueue("lpfc_wq", WQ_MEM_RECLAIM, 0);
  7190. if (!phba->wq)
  7191. return -ENOMEM;
  7192. /*
  7193. * Initialize timers used by driver
  7194. */
  7195. timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
  7196. /* FCF rediscover timer */
  7197. timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
  7198. /* CMF congestion timer */
  7199. hrtimer_init(&phba->cmf_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  7200. phba->cmf_timer.function = lpfc_cmf_timer;
  7201. /* CMF 1 minute stats collection timer */
  7202. hrtimer_init(&phba->cmf_stats_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  7203. phba->cmf_stats_timer.function = lpfc_cmf_stats_timer;
  7204. /*
  7205. * Control structure for handling external multi-buffer mailbox
  7206. * command pass-through.
  7207. */
  7208. memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
  7209. sizeof(struct lpfc_mbox_ext_buf_ctx));
  7210. INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  7211. phba->max_vpi = LPFC_MAX_VPI;
  7212. /* This will be set to correct value after the read_config mbox */
  7213. phba->max_vports = 0;
  7214. /* Program the default value of vlan_id and fc_map */
  7215. phba->valid_vlan = 0;
  7216. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  7217. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  7218. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  7219. /*
  7220. * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
  7221. * we will associate a new ring, for each EQ/CQ/WQ tuple.
  7222. * The WQ create will allocate the ring.
  7223. */
  7224. /* Initialize buffer queue management fields */
  7225. INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
  7226. phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
  7227. phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
  7228. /* for VMID idle timeout if VMID is enabled */
  7229. if (lpfc_is_vmid_enabled(phba))
  7230. timer_setup(&phba->inactive_vmid_poll, lpfc_vmid_poll, 0);
  7231. /*
  7232. * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
  7233. */
  7234. /* Initialize the Abort buffer list used by driver */
  7235. spin_lock_init(&phba->sli4_hba.abts_io_buf_list_lock);
  7236. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_io_buf_list);
  7237. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  7238. /* Initialize the Abort nvme buffer list used by driver */
  7239. spin_lock_init(&phba->sli4_hba.abts_nvmet_buf_list_lock);
  7240. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
  7241. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
  7242. spin_lock_init(&phba->sli4_hba.t_active_list_lock);
  7243. INIT_LIST_HEAD(&phba->sli4_hba.t_active_ctx_list);
  7244. }
  7245. /* This abort list used by worker thread */
  7246. spin_lock_init(&phba->sli4_hba.sgl_list_lock);
  7247. spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
  7248. spin_lock_init(&phba->sli4_hba.asynce_list_lock);
  7249. spin_lock_init(&phba->sli4_hba.els_xri_abrt_list_lock);
  7250. /*
  7251. * Initialize driver internal slow-path work queues
  7252. */
  7253. /* Driver internel slow-path CQ Event pool */
  7254. INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
  7255. /* Response IOCB work queue list */
  7256. INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
  7257. /* Asynchronous event CQ Event work queue list */
  7258. INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
  7259. /* Slow-path XRI aborted CQ Event work queue list */
  7260. INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
  7261. /* Receive queue CQ Event work queue list */
  7262. INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
  7263. /* Initialize extent block lists. */
  7264. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
  7265. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
  7266. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
  7267. INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
  7268. /* Initialize mboxq lists. If the early init routines fail
  7269. * these lists need to be correctly initialized.
  7270. */
  7271. INIT_LIST_HEAD(&phba->sli.mboxq);
  7272. INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
  7273. /* initialize optic_state to 0xFF */
  7274. phba->sli4_hba.lnk_info.optic_state = 0xff;
  7275. /* Allocate device driver memory */
  7276. rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
  7277. if (rc)
  7278. goto out_destroy_workqueue;
  7279. /* IF Type 2 ports get initialized now. */
  7280. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
  7281. LPFC_SLI_INTF_IF_TYPE_2) {
  7282. rc = lpfc_pci_function_reset(phba);
  7283. if (unlikely(rc)) {
  7284. rc = -ENODEV;
  7285. goto out_free_mem;
  7286. }
  7287. phba->temp_sensor_support = 1;
  7288. }
  7289. /* Create the bootstrap mailbox command */
  7290. rc = lpfc_create_bootstrap_mbox(phba);
  7291. if (unlikely(rc))
  7292. goto out_free_mem;
  7293. /* Set up the host's endian order with the device. */
  7294. rc = lpfc_setup_endian_order(phba);
  7295. if (unlikely(rc))
  7296. goto out_free_bsmbx;
  7297. /* Set up the hba's configuration parameters. */
  7298. rc = lpfc_sli4_read_config(phba);
  7299. if (unlikely(rc))
  7300. goto out_free_bsmbx;
  7301. if (phba->sli4_hba.fawwpn_flag & LPFC_FAWWPN_CONFIG) {
  7302. /* Right now the link is down, if FA-PWWN is configured the
  7303. * firmware will try FLOGI before the driver gets a link up.
  7304. * If it fails, the driver should get a MISCONFIGURED async
  7305. * event which will clear this flag. The only notification
  7306. * the driver gets is if it fails, if it succeeds there is no
  7307. * notification given. Assume success.
  7308. */
  7309. phba->sli4_hba.fawwpn_flag |= LPFC_FAWWPN_FABRIC;
  7310. }
  7311. rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
  7312. if (unlikely(rc))
  7313. goto out_free_bsmbx;
  7314. /* IF Type 0 ports get initialized now. */
  7315. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
  7316. LPFC_SLI_INTF_IF_TYPE_0) {
  7317. rc = lpfc_pci_function_reset(phba);
  7318. if (unlikely(rc))
  7319. goto out_free_bsmbx;
  7320. }
  7321. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
  7322. GFP_KERNEL);
  7323. if (!mboxq) {
  7324. rc = -ENOMEM;
  7325. goto out_free_bsmbx;
  7326. }
  7327. /* Check for NVMET being configured */
  7328. phba->nvmet_support = 0;
  7329. if (lpfc_enable_nvmet_cnt) {
  7330. /* First get WWN of HBA instance */
  7331. lpfc_read_nv(phba, mboxq);
  7332. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  7333. if (rc != MBX_SUCCESS) {
  7334. lpfc_printf_log(phba, KERN_ERR,
  7335. LOG_TRACE_EVENT,
  7336. "6016 Mailbox failed , mbxCmd x%x "
  7337. "READ_NV, mbxStatus x%x\n",
  7338. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  7339. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  7340. mempool_free(mboxq, phba->mbox_mem_pool);
  7341. rc = -EIO;
  7342. goto out_free_bsmbx;
  7343. }
  7344. mb = &mboxq->u.mb;
  7345. memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
  7346. sizeof(uint64_t));
  7347. wwn = cpu_to_be64(wwn);
  7348. phba->sli4_hba.wwnn.u.name = wwn;
  7349. memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
  7350. sizeof(uint64_t));
  7351. /* wwn is WWPN of HBA instance */
  7352. wwn = cpu_to_be64(wwn);
  7353. phba->sli4_hba.wwpn.u.name = wwn;
  7354. /* Check to see if it matches any module parameter */
  7355. for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
  7356. if (wwn == lpfc_enable_nvmet[i]) {
  7357. #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
  7358. if (lpfc_nvmet_mem_alloc(phba))
  7359. break;
  7360. phba->nvmet_support = 1; /* a match */
  7361. lpfc_printf_log(phba, KERN_ERR,
  7362. LOG_TRACE_EVENT,
  7363. "6017 NVME Target %016llx\n",
  7364. wwn);
  7365. #else
  7366. lpfc_printf_log(phba, KERN_ERR,
  7367. LOG_TRACE_EVENT,
  7368. "6021 Can't enable NVME Target."
  7369. " NVME_TARGET_FC infrastructure"
  7370. " is not in kernel\n");
  7371. #endif
  7372. /* Not supported for NVMET */
  7373. phba->cfg_xri_rebalancing = 0;
  7374. if (phba->irq_chann_mode == NHT_MODE) {
  7375. phba->cfg_irq_chann =
  7376. phba->sli4_hba.num_present_cpu;
  7377. phba->cfg_hdw_queue =
  7378. phba->sli4_hba.num_present_cpu;
  7379. phba->irq_chann_mode = NORMAL_MODE;
  7380. }
  7381. break;
  7382. }
  7383. }
  7384. }
  7385. lpfc_nvme_mod_param_dep(phba);
  7386. /*
  7387. * Get sli4 parameters that override parameters from Port capabilities.
  7388. * If this call fails, it isn't critical unless the SLI4 parameters come
  7389. * back in conflict.
  7390. */
  7391. rc = lpfc_get_sli4_parameters(phba, mboxq);
  7392. if (rc) {
  7393. if_type = bf_get(lpfc_sli_intf_if_type,
  7394. &phba->sli4_hba.sli_intf);
  7395. if_fam = bf_get(lpfc_sli_intf_sli_family,
  7396. &phba->sli4_hba.sli_intf);
  7397. if (phba->sli4_hba.extents_in_use &&
  7398. phba->sli4_hba.rpi_hdrs_in_use) {
  7399. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  7400. "2999 Unsupported SLI4 Parameters "
  7401. "Extents and RPI headers enabled.\n");
  7402. if (if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
  7403. if_fam == LPFC_SLI_INTF_FAMILY_BE2) {
  7404. mempool_free(mboxq, phba->mbox_mem_pool);
  7405. rc = -EIO;
  7406. goto out_free_bsmbx;
  7407. }
  7408. }
  7409. if (!(if_type == LPFC_SLI_INTF_IF_TYPE_0 &&
  7410. if_fam == LPFC_SLI_INTF_FAMILY_BE2)) {
  7411. mempool_free(mboxq, phba->mbox_mem_pool);
  7412. rc = -EIO;
  7413. goto out_free_bsmbx;
  7414. }
  7415. }
  7416. /*
  7417. * 1 for cmd, 1 for rsp, NVME adds an extra one
  7418. * for boundary conditions in its max_sgl_segment template.
  7419. */
  7420. extra = 2;
  7421. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
  7422. extra++;
  7423. /*
  7424. * It doesn't matter what family our adapter is in, we are
  7425. * limited to 2 Pages, 512 SGEs, for our SGL.
  7426. * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
  7427. */
  7428. max_buf_size = (2 * SLI4_PAGE_SIZE);
  7429. /*
  7430. * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
  7431. * used to create the sg_dma_buf_pool must be calculated.
  7432. */
  7433. if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
  7434. /* Both cfg_enable_bg and cfg_external_dif code paths */
  7435. /*
  7436. * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
  7437. * the FCP rsp, and a SGE. Sice we have no control
  7438. * over how many protection segments the SCSI Layer
  7439. * will hand us (ie: there could be one for every block
  7440. * in the IO), just allocate enough SGEs to accomidate
  7441. * our max amount and we need to limit lpfc_sg_seg_cnt
  7442. * to minimize the risk of running out.
  7443. */
  7444. phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd32) +
  7445. sizeof(struct fcp_rsp) + max_buf_size;
  7446. /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
  7447. phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
  7448. /*
  7449. * If supporting DIF, reduce the seg count for scsi to
  7450. * allow room for the DIF sges.
  7451. */
  7452. if (phba->cfg_enable_bg &&
  7453. phba->cfg_sg_seg_cnt > LPFC_MAX_BG_SLI4_SEG_CNT_DIF)
  7454. phba->cfg_scsi_seg_cnt = LPFC_MAX_BG_SLI4_SEG_CNT_DIF;
  7455. else
  7456. phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
  7457. } else {
  7458. /*
  7459. * The scsi_buf for a regular I/O holds the FCP cmnd,
  7460. * the FCP rsp, a SGE for each, and a SGE for up to
  7461. * cfg_sg_seg_cnt data segments.
  7462. */
  7463. phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd32) +
  7464. sizeof(struct fcp_rsp) +
  7465. ((phba->cfg_sg_seg_cnt + extra) *
  7466. sizeof(struct sli4_sge));
  7467. /* Total SGEs for scsi_sg_list */
  7468. phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + extra;
  7469. phba->cfg_scsi_seg_cnt = phba->cfg_sg_seg_cnt;
  7470. /*
  7471. * NOTE: if (phba->cfg_sg_seg_cnt + extra) <= 256 we only
  7472. * need to post 1 page for the SGL.
  7473. */
  7474. }
  7475. if (phba->cfg_xpsgl && !phba->nvmet_support)
  7476. phba->cfg_sg_dma_buf_size = LPFC_DEFAULT_XPSGL_SIZE;
  7477. else if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
  7478. phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
  7479. else
  7480. phba->cfg_sg_dma_buf_size =
  7481. SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
  7482. phba->border_sge_num = phba->cfg_sg_dma_buf_size /
  7483. sizeof(struct sli4_sge);
  7484. /* Limit to LPFC_MAX_NVME_SEG_CNT for NVME. */
  7485. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  7486. if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
  7487. lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
  7488. "6300 Reducing NVME sg segment "
  7489. "cnt to %d\n",
  7490. LPFC_MAX_NVME_SEG_CNT);
  7491. phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
  7492. } else
  7493. phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
  7494. }
  7495. lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
  7496. "9087 sg_seg_cnt:%d dmabuf_size:%d "
  7497. "total:%d scsi:%d nvme:%d\n",
  7498. phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
  7499. phba->cfg_total_seg_cnt, phba->cfg_scsi_seg_cnt,
  7500. phba->cfg_nvme_seg_cnt);
  7501. if (phba->cfg_sg_dma_buf_size < SLI4_PAGE_SIZE)
  7502. i = phba->cfg_sg_dma_buf_size;
  7503. else
  7504. i = SLI4_PAGE_SIZE;
  7505. phba->lpfc_sg_dma_buf_pool =
  7506. dma_pool_create("lpfc_sg_dma_buf_pool",
  7507. &phba->pcidev->dev,
  7508. phba->cfg_sg_dma_buf_size,
  7509. i, 0);
  7510. if (!phba->lpfc_sg_dma_buf_pool) {
  7511. rc = -ENOMEM;
  7512. goto out_free_bsmbx;
  7513. }
  7514. phba->lpfc_cmd_rsp_buf_pool =
  7515. dma_pool_create("lpfc_cmd_rsp_buf_pool",
  7516. &phba->pcidev->dev,
  7517. sizeof(struct fcp_cmnd32) +
  7518. sizeof(struct fcp_rsp),
  7519. i, 0);
  7520. if (!phba->lpfc_cmd_rsp_buf_pool) {
  7521. rc = -ENOMEM;
  7522. goto out_free_sg_dma_buf;
  7523. }
  7524. mempool_free(mboxq, phba->mbox_mem_pool);
  7525. /* Verify OAS is supported */
  7526. lpfc_sli4_oas_verify(phba);
  7527. /* Verify RAS support on adapter */
  7528. lpfc_sli4_ras_init(phba);
  7529. /* Verify all the SLI4 queues */
  7530. rc = lpfc_sli4_queue_verify(phba);
  7531. if (rc)
  7532. goto out_free_cmd_rsp_buf;
  7533. /* Create driver internal CQE event pool */
  7534. rc = lpfc_sli4_cq_event_pool_create(phba);
  7535. if (rc)
  7536. goto out_free_cmd_rsp_buf;
  7537. /* Initialize sgl lists per host */
  7538. lpfc_init_sgl_list(phba);
  7539. /* Allocate and initialize active sgl array */
  7540. rc = lpfc_init_active_sgl_array(phba);
  7541. if (rc) {
  7542. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  7543. "1430 Failed to initialize sgl list.\n");
  7544. goto out_destroy_cq_event_pool;
  7545. }
  7546. rc = lpfc_sli4_init_rpi_hdrs(phba);
  7547. if (rc) {
  7548. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  7549. "1432 Failed to initialize rpi headers.\n");
  7550. goto out_free_active_sgl;
  7551. }
  7552. /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
  7553. longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
  7554. phba->fcf.fcf_rr_bmask = kcalloc(longs, sizeof(unsigned long),
  7555. GFP_KERNEL);
  7556. if (!phba->fcf.fcf_rr_bmask) {
  7557. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  7558. "2759 Failed allocate memory for FCF round "
  7559. "robin failover bmask\n");
  7560. rc = -ENOMEM;
  7561. goto out_remove_rpi_hdrs;
  7562. }
  7563. phba->sli4_hba.hba_eq_hdl = kcalloc(phba->cfg_irq_chann,
  7564. sizeof(struct lpfc_hba_eq_hdl),
  7565. GFP_KERNEL);
  7566. if (!phba->sli4_hba.hba_eq_hdl) {
  7567. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  7568. "2572 Failed allocate memory for "
  7569. "fast-path per-EQ handle array\n");
  7570. rc = -ENOMEM;
  7571. goto out_free_fcf_rr_bmask;
  7572. }
  7573. phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_possible_cpu,
  7574. sizeof(struct lpfc_vector_map_info),
  7575. GFP_KERNEL);
  7576. if (!phba->sli4_hba.cpu_map) {
  7577. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  7578. "3327 Failed allocate memory for msi-x "
  7579. "interrupt vector mapping\n");
  7580. rc = -ENOMEM;
  7581. goto out_free_hba_eq_hdl;
  7582. }
  7583. phba->sli4_hba.eq_info = alloc_percpu(struct lpfc_eq_intr_info);
  7584. if (!phba->sli4_hba.eq_info) {
  7585. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  7586. "3321 Failed allocation for per_cpu stats\n");
  7587. rc = -ENOMEM;
  7588. goto out_free_hba_cpu_map;
  7589. }
  7590. phba->sli4_hba.idle_stat = kcalloc(phba->sli4_hba.num_possible_cpu,
  7591. sizeof(*phba->sli4_hba.idle_stat),
  7592. GFP_KERNEL);
  7593. if (!phba->sli4_hba.idle_stat) {
  7594. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  7595. "3390 Failed allocation for idle_stat\n");
  7596. rc = -ENOMEM;
  7597. goto out_free_hba_eq_info;
  7598. }
  7599. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  7600. phba->sli4_hba.c_stat = alloc_percpu(struct lpfc_hdwq_stat);
  7601. if (!phba->sli4_hba.c_stat) {
  7602. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  7603. "3332 Failed allocating per cpu hdwq stats\n");
  7604. rc = -ENOMEM;
  7605. goto out_free_hba_idle_stat;
  7606. }
  7607. #endif
  7608. phba->cmf_stat = alloc_percpu(struct lpfc_cgn_stat);
  7609. if (!phba->cmf_stat) {
  7610. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  7611. "3331 Failed allocating per cpu cgn stats\n");
  7612. rc = -ENOMEM;
  7613. goto out_free_hba_hdwq_info;
  7614. }
  7615. /*
  7616. * Enable sr-iov virtual functions if supported and configured
  7617. * through the module parameter.
  7618. */
  7619. if (phba->cfg_sriov_nr_virtfn > 0) {
  7620. rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
  7621. phba->cfg_sriov_nr_virtfn);
  7622. if (rc) {
  7623. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  7624. "3020 Requested number of SR-IOV "
  7625. "virtual functions (%d) is not "
  7626. "supported\n",
  7627. phba->cfg_sriov_nr_virtfn);
  7628. phba->cfg_sriov_nr_virtfn = 0;
  7629. }
  7630. }
  7631. return 0;
  7632. out_free_hba_hdwq_info:
  7633. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  7634. free_percpu(phba->sli4_hba.c_stat);
  7635. out_free_hba_idle_stat:
  7636. #endif
  7637. kfree(phba->sli4_hba.idle_stat);
  7638. out_free_hba_eq_info:
  7639. free_percpu(phba->sli4_hba.eq_info);
  7640. out_free_hba_cpu_map:
  7641. kfree(phba->sli4_hba.cpu_map);
  7642. out_free_hba_eq_hdl:
  7643. kfree(phba->sli4_hba.hba_eq_hdl);
  7644. out_free_fcf_rr_bmask:
  7645. kfree(phba->fcf.fcf_rr_bmask);
  7646. out_remove_rpi_hdrs:
  7647. lpfc_sli4_remove_rpi_hdrs(phba);
  7648. out_free_active_sgl:
  7649. lpfc_free_active_sgl(phba);
  7650. out_destroy_cq_event_pool:
  7651. lpfc_sli4_cq_event_pool_destroy(phba);
  7652. out_free_cmd_rsp_buf:
  7653. dma_pool_destroy(phba->lpfc_cmd_rsp_buf_pool);
  7654. phba->lpfc_cmd_rsp_buf_pool = NULL;
  7655. out_free_sg_dma_buf:
  7656. dma_pool_destroy(phba->lpfc_sg_dma_buf_pool);
  7657. phba->lpfc_sg_dma_buf_pool = NULL;
  7658. out_free_bsmbx:
  7659. lpfc_destroy_bootstrap_mbox(phba);
  7660. out_free_mem:
  7661. lpfc_mem_free(phba);
  7662. out_destroy_workqueue:
  7663. destroy_workqueue(phba->wq);
  7664. phba->wq = NULL;
  7665. return rc;
  7666. }
  7667. /**
  7668. * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
  7669. * @phba: pointer to lpfc hba data structure.
  7670. *
  7671. * This routine is invoked to unset the driver internal resources set up
  7672. * specific for supporting the SLI-4 HBA device it attached to.
  7673. **/
  7674. static void
  7675. lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
  7676. {
  7677. struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
  7678. free_percpu(phba->sli4_hba.eq_info);
  7679. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  7680. free_percpu(phba->sli4_hba.c_stat);
  7681. #endif
  7682. free_percpu(phba->cmf_stat);
  7683. kfree(phba->sli4_hba.idle_stat);
  7684. /* Free memory allocated for msi-x interrupt vector to CPU mapping */
  7685. kfree(phba->sli4_hba.cpu_map);
  7686. phba->sli4_hba.num_possible_cpu = 0;
  7687. phba->sli4_hba.num_present_cpu = 0;
  7688. phba->sli4_hba.curr_disp_cpu = 0;
  7689. cpumask_clear(&phba->sli4_hba.irq_aff_mask);
  7690. /* Free memory allocated for fast-path work queue handles */
  7691. kfree(phba->sli4_hba.hba_eq_hdl);
  7692. /* Free the allocated rpi headers. */
  7693. lpfc_sli4_remove_rpi_hdrs(phba);
  7694. lpfc_sli4_remove_rpis(phba);
  7695. /* Free eligible FCF index bmask */
  7696. kfree(phba->fcf.fcf_rr_bmask);
  7697. /* Free the ELS sgl list */
  7698. lpfc_free_active_sgl(phba);
  7699. lpfc_free_els_sgl_list(phba);
  7700. lpfc_free_nvmet_sgl_list(phba);
  7701. /* Free the completion queue EQ event pool */
  7702. lpfc_sli4_cq_event_release_all(phba);
  7703. lpfc_sli4_cq_event_pool_destroy(phba);
  7704. /* Release resource identifiers. */
  7705. lpfc_sli4_dealloc_resource_identifiers(phba);
  7706. /* Free the bsmbx region. */
  7707. lpfc_destroy_bootstrap_mbox(phba);
  7708. /* Free the SLI Layer memory with SLI4 HBAs */
  7709. lpfc_mem_free_all(phba);
  7710. /* Free the current connect table */
  7711. list_for_each_entry_safe(conn_entry, next_conn_entry,
  7712. &phba->fcf_conn_rec_list, list) {
  7713. list_del_init(&conn_entry->list);
  7714. kfree(conn_entry);
  7715. }
  7716. return;
  7717. }
  7718. /**
  7719. * lpfc_init_api_table_setup - Set up init api function jump table
  7720. * @phba: The hba struct for which this call is being executed.
  7721. * @dev_grp: The HBA PCI-Device group number.
  7722. *
  7723. * This routine sets up the device INIT interface API function jump table
  7724. * in @phba struct.
  7725. *
  7726. * Returns: 0 - success, -ENODEV - failure.
  7727. **/
  7728. int
  7729. lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7730. {
  7731. phba->lpfc_hba_init_link = lpfc_hba_init_link;
  7732. phba->lpfc_hba_down_link = lpfc_hba_down_link;
  7733. phba->lpfc_selective_reset = lpfc_selective_reset;
  7734. switch (dev_grp) {
  7735. case LPFC_PCI_DEV_LP:
  7736. phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
  7737. phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
  7738. phba->lpfc_stop_port = lpfc_stop_port_s3;
  7739. break;
  7740. case LPFC_PCI_DEV_OC:
  7741. phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
  7742. phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
  7743. phba->lpfc_stop_port = lpfc_stop_port_s4;
  7744. break;
  7745. default:
  7746. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7747. "1431 Invalid HBA PCI-device group: 0x%x\n",
  7748. dev_grp);
  7749. return -ENODEV;
  7750. }
  7751. return 0;
  7752. }
  7753. /**
  7754. * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
  7755. * @phba: pointer to lpfc hba data structure.
  7756. *
  7757. * This routine is invoked to set up the driver internal resources after the
  7758. * device specific resource setup to support the HBA device it attached to.
  7759. *
  7760. * Return codes
  7761. * 0 - successful
  7762. * other values - error
  7763. **/
  7764. static int
  7765. lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
  7766. {
  7767. int error;
  7768. /* Startup the kernel thread for this host adapter. */
  7769. phba->worker_thread = kthread_run(lpfc_do_work, phba,
  7770. "lpfc_worker_%d", phba->brd_no);
  7771. if (IS_ERR(phba->worker_thread)) {
  7772. error = PTR_ERR(phba->worker_thread);
  7773. return error;
  7774. }
  7775. return 0;
  7776. }
  7777. /**
  7778. * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
  7779. * @phba: pointer to lpfc hba data structure.
  7780. *
  7781. * This routine is invoked to unset the driver internal resources set up after
  7782. * the device specific resource setup for supporting the HBA device it
  7783. * attached to.
  7784. **/
  7785. static void
  7786. lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
  7787. {
  7788. if (phba->wq) {
  7789. destroy_workqueue(phba->wq);
  7790. phba->wq = NULL;
  7791. }
  7792. /* Stop kernel worker thread */
  7793. if (phba->worker_thread)
  7794. kthread_stop(phba->worker_thread);
  7795. }
  7796. /**
  7797. * lpfc_free_iocb_list - Free iocb list.
  7798. * @phba: pointer to lpfc hba data structure.
  7799. *
  7800. * This routine is invoked to free the driver's IOCB list and memory.
  7801. **/
  7802. void
  7803. lpfc_free_iocb_list(struct lpfc_hba *phba)
  7804. {
  7805. struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
  7806. spin_lock_irq(&phba->hbalock);
  7807. list_for_each_entry_safe(iocbq_entry, iocbq_next,
  7808. &phba->lpfc_iocb_list, list) {
  7809. list_del(&iocbq_entry->list);
  7810. kfree(iocbq_entry);
  7811. phba->total_iocbq_bufs--;
  7812. }
  7813. spin_unlock_irq(&phba->hbalock);
  7814. return;
  7815. }
  7816. /**
  7817. * lpfc_init_iocb_list - Allocate and initialize iocb list.
  7818. * @phba: pointer to lpfc hba data structure.
  7819. * @iocb_count: number of requested iocbs
  7820. *
  7821. * This routine is invoked to allocate and initizlize the driver's IOCB
  7822. * list and set up the IOCB tag array accordingly.
  7823. *
  7824. * Return codes
  7825. * 0 - successful
  7826. * other values - error
  7827. **/
  7828. int
  7829. lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
  7830. {
  7831. struct lpfc_iocbq *iocbq_entry = NULL;
  7832. uint16_t iotag;
  7833. int i;
  7834. /* Initialize and populate the iocb list per host. */
  7835. INIT_LIST_HEAD(&phba->lpfc_iocb_list);
  7836. for (i = 0; i < iocb_count; i++) {
  7837. iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
  7838. if (iocbq_entry == NULL) {
  7839. printk(KERN_ERR "%s: only allocated %d iocbs of "
  7840. "expected %d count. Unloading driver.\n",
  7841. __func__, i, iocb_count);
  7842. goto out_free_iocbq;
  7843. }
  7844. iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
  7845. if (iotag == 0) {
  7846. kfree(iocbq_entry);
  7847. printk(KERN_ERR "%s: failed to allocate IOTAG. "
  7848. "Unloading driver.\n", __func__);
  7849. goto out_free_iocbq;
  7850. }
  7851. iocbq_entry->sli4_lxritag = NO_XRI;
  7852. iocbq_entry->sli4_xritag = NO_XRI;
  7853. spin_lock_irq(&phba->hbalock);
  7854. list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
  7855. phba->total_iocbq_bufs++;
  7856. spin_unlock_irq(&phba->hbalock);
  7857. }
  7858. return 0;
  7859. out_free_iocbq:
  7860. lpfc_free_iocb_list(phba);
  7861. return -ENOMEM;
  7862. }
  7863. /**
  7864. * lpfc_free_sgl_list - Free a given sgl list.
  7865. * @phba: pointer to lpfc hba data structure.
  7866. * @sglq_list: pointer to the head of sgl list.
  7867. *
  7868. * This routine is invoked to free a give sgl list and memory.
  7869. **/
  7870. void
  7871. lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
  7872. {
  7873. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  7874. list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
  7875. list_del(&sglq_entry->list);
  7876. lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
  7877. kfree(sglq_entry);
  7878. }
  7879. }
  7880. /**
  7881. * lpfc_free_els_sgl_list - Free els sgl list.
  7882. * @phba: pointer to lpfc hba data structure.
  7883. *
  7884. * This routine is invoked to free the driver's els sgl list and memory.
  7885. **/
  7886. static void
  7887. lpfc_free_els_sgl_list(struct lpfc_hba *phba)
  7888. {
  7889. LIST_HEAD(sglq_list);
  7890. /* Retrieve all els sgls from driver list */
  7891. spin_lock_irq(&phba->sli4_hba.sgl_list_lock);
  7892. list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
  7893. spin_unlock_irq(&phba->sli4_hba.sgl_list_lock);
  7894. /* Now free the sgl list */
  7895. lpfc_free_sgl_list(phba, &sglq_list);
  7896. }
  7897. /**
  7898. * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
  7899. * @phba: pointer to lpfc hba data structure.
  7900. *
  7901. * This routine is invoked to free the driver's nvmet sgl list and memory.
  7902. **/
  7903. static void
  7904. lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
  7905. {
  7906. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  7907. LIST_HEAD(sglq_list);
  7908. /* Retrieve all nvmet sgls from driver list */
  7909. spin_lock_irq(&phba->hbalock);
  7910. spin_lock(&phba->sli4_hba.sgl_list_lock);
  7911. list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
  7912. spin_unlock(&phba->sli4_hba.sgl_list_lock);
  7913. spin_unlock_irq(&phba->hbalock);
  7914. /* Now free the sgl list */
  7915. list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
  7916. list_del(&sglq_entry->list);
  7917. lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
  7918. kfree(sglq_entry);
  7919. }
  7920. /* Update the nvmet_xri_cnt to reflect no current sgls.
  7921. * The next initialization cycle sets the count and allocates
  7922. * the sgls over again.
  7923. */
  7924. phba->sli4_hba.nvmet_xri_cnt = 0;
  7925. }
  7926. /**
  7927. * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
  7928. * @phba: pointer to lpfc hba data structure.
  7929. *
  7930. * This routine is invoked to allocate the driver's active sgl memory.
  7931. * This array will hold the sglq_entry's for active IOs.
  7932. **/
  7933. static int
  7934. lpfc_init_active_sgl_array(struct lpfc_hba *phba)
  7935. {
  7936. int size;
  7937. size = sizeof(struct lpfc_sglq *);
  7938. size *= phba->sli4_hba.max_cfg_param.max_xri;
  7939. phba->sli4_hba.lpfc_sglq_active_list =
  7940. kzalloc(size, GFP_KERNEL);
  7941. if (!phba->sli4_hba.lpfc_sglq_active_list)
  7942. return -ENOMEM;
  7943. return 0;
  7944. }
  7945. /**
  7946. * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
  7947. * @phba: pointer to lpfc hba data structure.
  7948. *
  7949. * This routine is invoked to walk through the array of active sglq entries
  7950. * and free all of the resources.
  7951. * This is just a place holder for now.
  7952. **/
  7953. static void
  7954. lpfc_free_active_sgl(struct lpfc_hba *phba)
  7955. {
  7956. kfree(phba->sli4_hba.lpfc_sglq_active_list);
  7957. }
  7958. /**
  7959. * lpfc_init_sgl_list - Allocate and initialize sgl list.
  7960. * @phba: pointer to lpfc hba data structure.
  7961. *
  7962. * This routine is invoked to allocate and initizlize the driver's sgl
  7963. * list and set up the sgl xritag tag array accordingly.
  7964. *
  7965. **/
  7966. static void
  7967. lpfc_init_sgl_list(struct lpfc_hba *phba)
  7968. {
  7969. /* Initialize and populate the sglq list per host/VF. */
  7970. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
  7971. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
  7972. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
  7973. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
  7974. /* els xri-sgl book keeping */
  7975. phba->sli4_hba.els_xri_cnt = 0;
  7976. /* nvme xri-buffer book keeping */
  7977. phba->sli4_hba.io_xri_cnt = 0;
  7978. }
  7979. /**
  7980. * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
  7981. * @phba: pointer to lpfc hba data structure.
  7982. *
  7983. * This routine is invoked to post rpi header templates to the
  7984. * port for those SLI4 ports that do not support extents. This routine
  7985. * posts a PAGE_SIZE memory region to the port to hold up to
  7986. * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
  7987. * and should be called only when interrupts are disabled.
  7988. *
  7989. * Return codes
  7990. * 0 - successful
  7991. * -ERROR - otherwise.
  7992. **/
  7993. int
  7994. lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
  7995. {
  7996. int rc = 0;
  7997. struct lpfc_rpi_hdr *rpi_hdr;
  7998. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
  7999. if (!phba->sli4_hba.rpi_hdrs_in_use)
  8000. return rc;
  8001. if (phba->sli4_hba.extents_in_use)
  8002. return -EIO;
  8003. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  8004. if (!rpi_hdr) {
  8005. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  8006. "0391 Error during rpi post operation\n");
  8007. lpfc_sli4_remove_rpis(phba);
  8008. rc = -ENODEV;
  8009. }
  8010. return rc;
  8011. }
  8012. /**
  8013. * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
  8014. * @phba: pointer to lpfc hba data structure.
  8015. *
  8016. * This routine is invoked to allocate a single 4KB memory region to
  8017. * support rpis and stores them in the phba. This single region
  8018. * provides support for up to 64 rpis. The region is used globally
  8019. * by the device.
  8020. *
  8021. * Returns:
  8022. * A valid rpi hdr on success.
  8023. * A NULL pointer on any failure.
  8024. **/
  8025. struct lpfc_rpi_hdr *
  8026. lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
  8027. {
  8028. uint16_t rpi_limit, curr_rpi_range;
  8029. struct lpfc_dmabuf *dmabuf;
  8030. struct lpfc_rpi_hdr *rpi_hdr;
  8031. /*
  8032. * If the SLI4 port supports extents, posting the rpi header isn't
  8033. * required. Set the expected maximum count and let the actual value
  8034. * get set when extents are fully allocated.
  8035. */
  8036. if (!phba->sli4_hba.rpi_hdrs_in_use)
  8037. return NULL;
  8038. if (phba->sli4_hba.extents_in_use)
  8039. return NULL;
  8040. /* The limit on the logical index is just the max_rpi count. */
  8041. rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
  8042. spin_lock_irq(&phba->hbalock);
  8043. /*
  8044. * Establish the starting RPI in this header block. The starting
  8045. * rpi is normalized to a zero base because the physical rpi is
  8046. * port based.
  8047. */
  8048. curr_rpi_range = phba->sli4_hba.next_rpi;
  8049. spin_unlock_irq(&phba->hbalock);
  8050. /* Reached full RPI range */
  8051. if (curr_rpi_range == rpi_limit)
  8052. return NULL;
  8053. /*
  8054. * First allocate the protocol header region for the port. The
  8055. * port expects a 4KB DMA-mapped memory region that is 4K aligned.
  8056. */
  8057. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  8058. if (!dmabuf)
  8059. return NULL;
  8060. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  8061. LPFC_HDR_TEMPLATE_SIZE,
  8062. &dmabuf->phys, GFP_KERNEL);
  8063. if (!dmabuf->virt) {
  8064. rpi_hdr = NULL;
  8065. goto err_free_dmabuf;
  8066. }
  8067. if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
  8068. rpi_hdr = NULL;
  8069. goto err_free_coherent;
  8070. }
  8071. /* Save the rpi header data for cleanup later. */
  8072. rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
  8073. if (!rpi_hdr)
  8074. goto err_free_coherent;
  8075. rpi_hdr->dmabuf = dmabuf;
  8076. rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
  8077. rpi_hdr->page_count = 1;
  8078. spin_lock_irq(&phba->hbalock);
  8079. /* The rpi_hdr stores the logical index only. */
  8080. rpi_hdr->start_rpi = curr_rpi_range;
  8081. rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
  8082. list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
  8083. spin_unlock_irq(&phba->hbalock);
  8084. return rpi_hdr;
  8085. err_free_coherent:
  8086. dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
  8087. dmabuf->virt, dmabuf->phys);
  8088. err_free_dmabuf:
  8089. kfree(dmabuf);
  8090. return NULL;
  8091. }
  8092. /**
  8093. * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
  8094. * @phba: pointer to lpfc hba data structure.
  8095. *
  8096. * This routine is invoked to remove all memory resources allocated
  8097. * to support rpis for SLI4 ports not supporting extents. This routine
  8098. * presumes the caller has released all rpis consumed by fabric or port
  8099. * logins and is prepared to have the header pages removed.
  8100. **/
  8101. void
  8102. lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
  8103. {
  8104. struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
  8105. if (!phba->sli4_hba.rpi_hdrs_in_use)
  8106. goto exit;
  8107. list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
  8108. &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  8109. list_del(&rpi_hdr->list);
  8110. dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
  8111. rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
  8112. kfree(rpi_hdr->dmabuf);
  8113. kfree(rpi_hdr);
  8114. }
  8115. exit:
  8116. /* There are no rpis available to the port now. */
  8117. phba->sli4_hba.next_rpi = 0;
  8118. }
  8119. /**
  8120. * lpfc_hba_alloc - Allocate driver hba data structure for a device.
  8121. * @pdev: pointer to pci device data structure.
  8122. *
  8123. * This routine is invoked to allocate the driver hba data structure for an
  8124. * HBA device. If the allocation is successful, the phba reference to the
  8125. * PCI device data structure is set.
  8126. *
  8127. * Return codes
  8128. * pointer to @phba - successful
  8129. * NULL - error
  8130. **/
  8131. static struct lpfc_hba *
  8132. lpfc_hba_alloc(struct pci_dev *pdev)
  8133. {
  8134. struct lpfc_hba *phba;
  8135. /* Allocate memory for HBA structure */
  8136. phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
  8137. if (!phba) {
  8138. dev_err(&pdev->dev, "failed to allocate hba struct\n");
  8139. return NULL;
  8140. }
  8141. /* Set reference to PCI device in HBA structure */
  8142. phba->pcidev = pdev;
  8143. /* Assign an unused board number */
  8144. phba->brd_no = lpfc_get_instance();
  8145. if (phba->brd_no < 0) {
  8146. kfree(phba);
  8147. return NULL;
  8148. }
  8149. phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
  8150. spin_lock_init(&phba->ct_ev_lock);
  8151. INIT_LIST_HEAD(&phba->ct_ev_waiters);
  8152. return phba;
  8153. }
  8154. /**
  8155. * lpfc_hba_free - Free driver hba data structure with a device.
  8156. * @phba: pointer to lpfc hba data structure.
  8157. *
  8158. * This routine is invoked to free the driver hba data structure with an
  8159. * HBA device.
  8160. **/
  8161. static void
  8162. lpfc_hba_free(struct lpfc_hba *phba)
  8163. {
  8164. if (phba->sli_rev == LPFC_SLI_REV4)
  8165. kfree(phba->sli4_hba.hdwq);
  8166. /* Release the driver assigned board number */
  8167. idr_remove(&lpfc_hba_index, phba->brd_no);
  8168. /* Free memory allocated with sli3 rings */
  8169. kfree(phba->sli.sli3_ring);
  8170. phba->sli.sli3_ring = NULL;
  8171. kfree(phba);
  8172. return;
  8173. }
  8174. /**
  8175. * lpfc_setup_fdmi_mask - Setup initial FDMI mask for HBA and Port attributes
  8176. * @vport: pointer to lpfc vport data structure.
  8177. *
  8178. * This routine is will setup initial FDMI attribute masks for
  8179. * FDMI2 or SmartSAN depending on module parameters. The driver will attempt
  8180. * to get these attributes first before falling back, the attribute
  8181. * fallback hierarchy is SmartSAN -> FDMI2 -> FMDI1
  8182. **/
  8183. void
  8184. lpfc_setup_fdmi_mask(struct lpfc_vport *vport)
  8185. {
  8186. struct lpfc_hba *phba = vport->phba;
  8187. set_bit(FC_ALLOW_FDMI, &vport->load_flag);
  8188. if (phba->cfg_enable_SmartSAN ||
  8189. phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT) {
  8190. /* Setup appropriate attribute masks */
  8191. vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
  8192. if (phba->cfg_enable_SmartSAN)
  8193. vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
  8194. else
  8195. vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
  8196. }
  8197. lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
  8198. "6077 Setup FDMI mask: hba x%x port x%x\n",
  8199. vport->fdmi_hba_mask, vport->fdmi_port_mask);
  8200. }
  8201. /**
  8202. * lpfc_create_shost - Create hba physical port with associated scsi host.
  8203. * @phba: pointer to lpfc hba data structure.
  8204. *
  8205. * This routine is invoked to create HBA physical port and associate a SCSI
  8206. * host with it.
  8207. *
  8208. * Return codes
  8209. * 0 - successful
  8210. * other values - error
  8211. **/
  8212. static int
  8213. lpfc_create_shost(struct lpfc_hba *phba)
  8214. {
  8215. struct lpfc_vport *vport;
  8216. struct Scsi_Host *shost;
  8217. /* Initialize HBA FC structure */
  8218. phba->fc_edtov = FF_DEF_EDTOV;
  8219. phba->fc_ratov = FF_DEF_RATOV;
  8220. phba->fc_altov = FF_DEF_ALTOV;
  8221. phba->fc_arbtov = FF_DEF_ARBTOV;
  8222. atomic_set(&phba->sdev_cnt, 0);
  8223. vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
  8224. if (!vport)
  8225. return -ENODEV;
  8226. shost = lpfc_shost_from_vport(vport);
  8227. phba->pport = vport;
  8228. if (phba->nvmet_support) {
  8229. /* Only 1 vport (pport) will support NVME target */
  8230. phba->targetport = NULL;
  8231. phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
  8232. lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME_DISC,
  8233. "6076 NVME Target Found\n");
  8234. }
  8235. lpfc_debugfs_initialize(vport);
  8236. /* Put reference to SCSI host to driver's device private data */
  8237. pci_set_drvdata(phba->pcidev, shost);
  8238. lpfc_setup_fdmi_mask(vport);
  8239. /*
  8240. * At this point we are fully registered with PSA. In addition,
  8241. * any initial discovery should be completed.
  8242. */
  8243. return 0;
  8244. }
  8245. /**
  8246. * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
  8247. * @phba: pointer to lpfc hba data structure.
  8248. *
  8249. * This routine is invoked to destroy HBA physical port and the associated
  8250. * SCSI host.
  8251. **/
  8252. static void
  8253. lpfc_destroy_shost(struct lpfc_hba *phba)
  8254. {
  8255. struct lpfc_vport *vport = phba->pport;
  8256. /* Destroy physical port that associated with the SCSI host */
  8257. destroy_port(vport);
  8258. return;
  8259. }
  8260. /**
  8261. * lpfc_setup_bg - Setup Block guard structures and debug areas.
  8262. * @phba: pointer to lpfc hba data structure.
  8263. * @shost: the shost to be used to detect Block guard settings.
  8264. *
  8265. * This routine sets up the local Block guard protocol settings for @shost.
  8266. * This routine also allocates memory for debugging bg buffers.
  8267. **/
  8268. static void
  8269. lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
  8270. {
  8271. uint32_t old_mask;
  8272. uint32_t old_guard;
  8273. if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
  8274. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  8275. "1478 Registering BlockGuard with the "
  8276. "SCSI layer\n");
  8277. old_mask = phba->cfg_prot_mask;
  8278. old_guard = phba->cfg_prot_guard;
  8279. /* Only allow supported values */
  8280. phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
  8281. SHOST_DIX_TYPE0_PROTECTION |
  8282. SHOST_DIX_TYPE1_PROTECTION);
  8283. phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
  8284. SHOST_DIX_GUARD_CRC);
  8285. /* DIF Type 1 protection for profiles AST1/C1 is end to end */
  8286. if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
  8287. phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
  8288. if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
  8289. if ((old_mask != phba->cfg_prot_mask) ||
  8290. (old_guard != phba->cfg_prot_guard))
  8291. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  8292. "1475 Registering BlockGuard with the "
  8293. "SCSI layer: mask %d guard %d\n",
  8294. phba->cfg_prot_mask,
  8295. phba->cfg_prot_guard);
  8296. scsi_host_set_prot(shost, phba->cfg_prot_mask);
  8297. scsi_host_set_guard(shost, phba->cfg_prot_guard);
  8298. } else
  8299. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  8300. "1479 Not Registering BlockGuard with the SCSI "
  8301. "layer, Bad protection parameters: %d %d\n",
  8302. old_mask, old_guard);
  8303. }
  8304. }
  8305. /**
  8306. * lpfc_post_init_setup - Perform necessary device post initialization setup.
  8307. * @phba: pointer to lpfc hba data structure.
  8308. *
  8309. * This routine is invoked to perform all the necessary post initialization
  8310. * setup for the device.
  8311. **/
  8312. static void
  8313. lpfc_post_init_setup(struct lpfc_hba *phba)
  8314. {
  8315. struct Scsi_Host *shost;
  8316. struct lpfc_adapter_event_header adapter_event;
  8317. /* Get the default values for Model Name and Description */
  8318. lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
  8319. /*
  8320. * hba setup may have changed the hba_queue_depth so we need to
  8321. * adjust the value of can_queue.
  8322. */
  8323. shost = pci_get_drvdata(phba->pcidev);
  8324. shost->can_queue = phba->cfg_hba_queue_depth - 10;
  8325. lpfc_host_attrib_init(shost);
  8326. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8327. spin_lock_irq(shost->host_lock);
  8328. lpfc_poll_start_timer(phba);
  8329. spin_unlock_irq(shost->host_lock);
  8330. }
  8331. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  8332. "0428 Perform SCSI scan\n");
  8333. /* Send board arrival event to upper layer */
  8334. adapter_event.event_type = FC_REG_ADAPTER_EVENT;
  8335. adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
  8336. fc_host_post_vendor_event(shost, fc_get_event_number(),
  8337. sizeof(adapter_event),
  8338. (char *) &adapter_event,
  8339. LPFC_NL_VENDOR_ID);
  8340. return;
  8341. }
  8342. /**
  8343. * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
  8344. * @phba: pointer to lpfc hba data structure.
  8345. *
  8346. * This routine is invoked to set up the PCI device memory space for device
  8347. * with SLI-3 interface spec.
  8348. *
  8349. * Return codes
  8350. * 0 - successful
  8351. * other values - error
  8352. **/
  8353. static int
  8354. lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
  8355. {
  8356. struct pci_dev *pdev = phba->pcidev;
  8357. unsigned long bar0map_len, bar2map_len;
  8358. int i, hbq_count;
  8359. void *ptr;
  8360. int error;
  8361. if (!pdev)
  8362. return -ENODEV;
  8363. /* Set the device DMA mask size */
  8364. error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  8365. if (error)
  8366. error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  8367. if (error)
  8368. return error;
  8369. error = -ENODEV;
  8370. /* Get the bus address of Bar0 and Bar2 and the number of bytes
  8371. * required by each mapping.
  8372. */
  8373. phba->pci_bar0_map = pci_resource_start(pdev, 0);
  8374. bar0map_len = pci_resource_len(pdev, 0);
  8375. phba->pci_bar2_map = pci_resource_start(pdev, 2);
  8376. bar2map_len = pci_resource_len(pdev, 2);
  8377. /* Map HBA SLIM to a kernel virtual address. */
  8378. phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
  8379. if (!phba->slim_memmap_p) {
  8380. dev_printk(KERN_ERR, &pdev->dev,
  8381. "ioremap failed for SLIM memory.\n");
  8382. goto out;
  8383. }
  8384. /* Map HBA Control Registers to a kernel virtual address. */
  8385. phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
  8386. if (!phba->ctrl_regs_memmap_p) {
  8387. dev_printk(KERN_ERR, &pdev->dev,
  8388. "ioremap failed for HBA control registers.\n");
  8389. goto out_iounmap_slim;
  8390. }
  8391. /* Allocate memory for SLI-2 structures */
  8392. phba->slim2p.virt = dma_alloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
  8393. &phba->slim2p.phys, GFP_KERNEL);
  8394. if (!phba->slim2p.virt)
  8395. goto out_iounmap;
  8396. phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
  8397. phba->mbox_ext = (phba->slim2p.virt +
  8398. offsetof(struct lpfc_sli2_slim, mbx_ext_words));
  8399. phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
  8400. phba->IOCBs = (phba->slim2p.virt +
  8401. offsetof(struct lpfc_sli2_slim, IOCBs));
  8402. phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
  8403. lpfc_sli_hbq_size(),
  8404. &phba->hbqslimp.phys,
  8405. GFP_KERNEL);
  8406. if (!phba->hbqslimp.virt)
  8407. goto out_free_slim;
  8408. hbq_count = lpfc_sli_hbq_count();
  8409. ptr = phba->hbqslimp.virt;
  8410. for (i = 0; i < hbq_count; ++i) {
  8411. phba->hbqs[i].hbq_virt = ptr;
  8412. INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
  8413. ptr += (lpfc_hbq_defs[i]->entry_count *
  8414. sizeof(struct lpfc_hbq_entry));
  8415. }
  8416. phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
  8417. phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
  8418. memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
  8419. phba->MBslimaddr = phba->slim_memmap_p;
  8420. phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
  8421. phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
  8422. phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
  8423. phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
  8424. return 0;
  8425. out_free_slim:
  8426. dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
  8427. phba->slim2p.virt, phba->slim2p.phys);
  8428. out_iounmap:
  8429. iounmap(phba->ctrl_regs_memmap_p);
  8430. out_iounmap_slim:
  8431. iounmap(phba->slim_memmap_p);
  8432. out:
  8433. return error;
  8434. }
  8435. /**
  8436. * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
  8437. * @phba: pointer to lpfc hba data structure.
  8438. *
  8439. * This routine is invoked to unset the PCI device memory space for device
  8440. * with SLI-3 interface spec.
  8441. **/
  8442. static void
  8443. lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
  8444. {
  8445. struct pci_dev *pdev;
  8446. /* Obtain PCI device reference */
  8447. if (!phba->pcidev)
  8448. return;
  8449. else
  8450. pdev = phba->pcidev;
  8451. /* Free coherent DMA memory allocated */
  8452. dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
  8453. phba->hbqslimp.virt, phba->hbqslimp.phys);
  8454. dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
  8455. phba->slim2p.virt, phba->slim2p.phys);
  8456. /* I/O memory unmap */
  8457. iounmap(phba->ctrl_regs_memmap_p);
  8458. iounmap(phba->slim_memmap_p);
  8459. return;
  8460. }
  8461. /**
  8462. * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
  8463. * @phba: pointer to lpfc hba data structure.
  8464. *
  8465. * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
  8466. * done and check status.
  8467. *
  8468. * Return 0 if successful, otherwise -ENODEV.
  8469. **/
  8470. int
  8471. lpfc_sli4_post_status_check(struct lpfc_hba *phba)
  8472. {
  8473. struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
  8474. struct lpfc_register reg_data;
  8475. int i, port_error = 0;
  8476. uint32_t if_type;
  8477. memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
  8478. memset(&reg_data, 0, sizeof(reg_data));
  8479. if (!phba->sli4_hba.PSMPHRregaddr)
  8480. return -ENODEV;
  8481. /* Wait up to 30 seconds for the SLI Port POST done and ready */
  8482. for (i = 0; i < 3000; i++) {
  8483. if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  8484. &portsmphr_reg.word0) ||
  8485. (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
  8486. /* Port has a fatal POST error, break out */
  8487. port_error = -ENODEV;
  8488. break;
  8489. }
  8490. if (LPFC_POST_STAGE_PORT_READY ==
  8491. bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
  8492. break;
  8493. msleep(10);
  8494. }
  8495. /*
  8496. * If there was a port error during POST, then don't proceed with
  8497. * other register reads as the data may not be valid. Just exit.
  8498. */
  8499. if (port_error) {
  8500. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  8501. "1408 Port Failed POST - portsmphr=0x%x, "
  8502. "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
  8503. "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
  8504. portsmphr_reg.word0,
  8505. bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
  8506. bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
  8507. bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
  8508. bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
  8509. bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
  8510. bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
  8511. bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
  8512. bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
  8513. } else {
  8514. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  8515. "2534 Device Info: SLIFamily=0x%x, "
  8516. "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
  8517. "SLIHint_2=0x%x, FT=0x%x\n",
  8518. bf_get(lpfc_sli_intf_sli_family,
  8519. &phba->sli4_hba.sli_intf),
  8520. bf_get(lpfc_sli_intf_slirev,
  8521. &phba->sli4_hba.sli_intf),
  8522. bf_get(lpfc_sli_intf_if_type,
  8523. &phba->sli4_hba.sli_intf),
  8524. bf_get(lpfc_sli_intf_sli_hint1,
  8525. &phba->sli4_hba.sli_intf),
  8526. bf_get(lpfc_sli_intf_sli_hint2,
  8527. &phba->sli4_hba.sli_intf),
  8528. bf_get(lpfc_sli_intf_func_type,
  8529. &phba->sli4_hba.sli_intf));
  8530. /*
  8531. * Check for other Port errors during the initialization
  8532. * process. Fail the load if the port did not come up
  8533. * correctly.
  8534. */
  8535. if_type = bf_get(lpfc_sli_intf_if_type,
  8536. &phba->sli4_hba.sli_intf);
  8537. switch (if_type) {
  8538. case LPFC_SLI_INTF_IF_TYPE_0:
  8539. phba->sli4_hba.ue_mask_lo =
  8540. readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
  8541. phba->sli4_hba.ue_mask_hi =
  8542. readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
  8543. uerrlo_reg.word0 =
  8544. readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
  8545. uerrhi_reg.word0 =
  8546. readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
  8547. if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
  8548. (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
  8549. lpfc_printf_log(phba, KERN_ERR,
  8550. LOG_TRACE_EVENT,
  8551. "1422 Unrecoverable Error "
  8552. "Detected during POST "
  8553. "uerr_lo_reg=0x%x, "
  8554. "uerr_hi_reg=0x%x, "
  8555. "ue_mask_lo_reg=0x%x, "
  8556. "ue_mask_hi_reg=0x%x\n",
  8557. uerrlo_reg.word0,
  8558. uerrhi_reg.word0,
  8559. phba->sli4_hba.ue_mask_lo,
  8560. phba->sli4_hba.ue_mask_hi);
  8561. port_error = -ENODEV;
  8562. }
  8563. break;
  8564. case LPFC_SLI_INTF_IF_TYPE_2:
  8565. case LPFC_SLI_INTF_IF_TYPE_6:
  8566. /* Final checks. The port status should be clean. */
  8567. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  8568. &reg_data.word0) ||
  8569. lpfc_sli4_unrecoverable_port(&reg_data)) {
  8570. phba->work_status[0] =
  8571. readl(phba->sli4_hba.u.if_type2.
  8572. ERR1regaddr);
  8573. phba->work_status[1] =
  8574. readl(phba->sli4_hba.u.if_type2.
  8575. ERR2regaddr);
  8576. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  8577. "2888 Unrecoverable port error "
  8578. "following POST: port status reg "
  8579. "0x%x, port_smphr reg 0x%x, "
  8580. "error 1=0x%x, error 2=0x%x\n",
  8581. reg_data.word0,
  8582. portsmphr_reg.word0,
  8583. phba->work_status[0],
  8584. phba->work_status[1]);
  8585. port_error = -ENODEV;
  8586. break;
  8587. }
  8588. if (lpfc_pldv_detect &&
  8589. bf_get(lpfc_sli_intf_sli_family,
  8590. &phba->sli4_hba.sli_intf) ==
  8591. LPFC_SLI_INTF_FAMILY_G6)
  8592. pci_write_config_byte(phba->pcidev,
  8593. LPFC_SLI_INTF, CFG_PLD);
  8594. break;
  8595. case LPFC_SLI_INTF_IF_TYPE_1:
  8596. default:
  8597. break;
  8598. }
  8599. }
  8600. return port_error;
  8601. }
  8602. /**
  8603. * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
  8604. * @phba: pointer to lpfc hba data structure.
  8605. * @if_type: The SLI4 interface type getting configured.
  8606. *
  8607. * This routine is invoked to set up SLI4 BAR0 PCI config space register
  8608. * memory map.
  8609. **/
  8610. static void
  8611. lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
  8612. {
  8613. switch (if_type) {
  8614. case LPFC_SLI_INTF_IF_TYPE_0:
  8615. phba->sli4_hba.u.if_type0.UERRLOregaddr =
  8616. phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
  8617. phba->sli4_hba.u.if_type0.UERRHIregaddr =
  8618. phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
  8619. phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
  8620. phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
  8621. phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
  8622. phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
  8623. phba->sli4_hba.SLIINTFregaddr =
  8624. phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
  8625. break;
  8626. case LPFC_SLI_INTF_IF_TYPE_2:
  8627. phba->sli4_hba.u.if_type2.EQDregaddr =
  8628. phba->sli4_hba.conf_regs_memmap_p +
  8629. LPFC_CTL_PORT_EQ_DELAY_OFFSET;
  8630. phba->sli4_hba.u.if_type2.ERR1regaddr =
  8631. phba->sli4_hba.conf_regs_memmap_p +
  8632. LPFC_CTL_PORT_ER1_OFFSET;
  8633. phba->sli4_hba.u.if_type2.ERR2regaddr =
  8634. phba->sli4_hba.conf_regs_memmap_p +
  8635. LPFC_CTL_PORT_ER2_OFFSET;
  8636. phba->sli4_hba.u.if_type2.CTRLregaddr =
  8637. phba->sli4_hba.conf_regs_memmap_p +
  8638. LPFC_CTL_PORT_CTL_OFFSET;
  8639. phba->sli4_hba.u.if_type2.STATUSregaddr =
  8640. phba->sli4_hba.conf_regs_memmap_p +
  8641. LPFC_CTL_PORT_STA_OFFSET;
  8642. phba->sli4_hba.SLIINTFregaddr =
  8643. phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
  8644. phba->sli4_hba.PSMPHRregaddr =
  8645. phba->sli4_hba.conf_regs_memmap_p +
  8646. LPFC_CTL_PORT_SEM_OFFSET;
  8647. phba->sli4_hba.RQDBregaddr =
  8648. phba->sli4_hba.conf_regs_memmap_p +
  8649. LPFC_ULP0_RQ_DOORBELL;
  8650. phba->sli4_hba.WQDBregaddr =
  8651. phba->sli4_hba.conf_regs_memmap_p +
  8652. LPFC_ULP0_WQ_DOORBELL;
  8653. phba->sli4_hba.CQDBregaddr =
  8654. phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
  8655. phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
  8656. phba->sli4_hba.MQDBregaddr =
  8657. phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
  8658. phba->sli4_hba.BMBXregaddr =
  8659. phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
  8660. break;
  8661. case LPFC_SLI_INTF_IF_TYPE_6:
  8662. phba->sli4_hba.u.if_type2.EQDregaddr =
  8663. phba->sli4_hba.conf_regs_memmap_p +
  8664. LPFC_CTL_PORT_EQ_DELAY_OFFSET;
  8665. phba->sli4_hba.u.if_type2.ERR1regaddr =
  8666. phba->sli4_hba.conf_regs_memmap_p +
  8667. LPFC_CTL_PORT_ER1_OFFSET;
  8668. phba->sli4_hba.u.if_type2.ERR2regaddr =
  8669. phba->sli4_hba.conf_regs_memmap_p +
  8670. LPFC_CTL_PORT_ER2_OFFSET;
  8671. phba->sli4_hba.u.if_type2.CTRLregaddr =
  8672. phba->sli4_hba.conf_regs_memmap_p +
  8673. LPFC_CTL_PORT_CTL_OFFSET;
  8674. phba->sli4_hba.u.if_type2.STATUSregaddr =
  8675. phba->sli4_hba.conf_regs_memmap_p +
  8676. LPFC_CTL_PORT_STA_OFFSET;
  8677. phba->sli4_hba.PSMPHRregaddr =
  8678. phba->sli4_hba.conf_regs_memmap_p +
  8679. LPFC_CTL_PORT_SEM_OFFSET;
  8680. phba->sli4_hba.BMBXregaddr =
  8681. phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
  8682. break;
  8683. case LPFC_SLI_INTF_IF_TYPE_1:
  8684. default:
  8685. dev_printk(KERN_ERR, &phba->pcidev->dev,
  8686. "FATAL - unsupported SLI4 interface type - %d\n",
  8687. if_type);
  8688. break;
  8689. }
  8690. }
  8691. /**
  8692. * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
  8693. * @phba: pointer to lpfc hba data structure.
  8694. * @if_type: sli if type to operate on.
  8695. *
  8696. * This routine is invoked to set up SLI4 BAR1 register memory map.
  8697. **/
  8698. static void
  8699. lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
  8700. {
  8701. switch (if_type) {
  8702. case LPFC_SLI_INTF_IF_TYPE_0:
  8703. phba->sli4_hba.PSMPHRregaddr =
  8704. phba->sli4_hba.ctrl_regs_memmap_p +
  8705. LPFC_SLIPORT_IF0_SMPHR;
  8706. phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
  8707. LPFC_HST_ISR0;
  8708. phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
  8709. LPFC_HST_IMR0;
  8710. phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
  8711. LPFC_HST_ISCR0;
  8712. break;
  8713. case LPFC_SLI_INTF_IF_TYPE_6:
  8714. phba->sli4_hba.RQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
  8715. LPFC_IF6_RQ_DOORBELL;
  8716. phba->sli4_hba.WQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
  8717. LPFC_IF6_WQ_DOORBELL;
  8718. phba->sli4_hba.CQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
  8719. LPFC_IF6_CQ_DOORBELL;
  8720. phba->sli4_hba.EQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
  8721. LPFC_IF6_EQ_DOORBELL;
  8722. phba->sli4_hba.MQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
  8723. LPFC_IF6_MQ_DOORBELL;
  8724. break;
  8725. case LPFC_SLI_INTF_IF_TYPE_2:
  8726. case LPFC_SLI_INTF_IF_TYPE_1:
  8727. default:
  8728. dev_err(&phba->pcidev->dev,
  8729. "FATAL - unsupported SLI4 interface type - %d\n",
  8730. if_type);
  8731. break;
  8732. }
  8733. }
  8734. /**
  8735. * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
  8736. * @phba: pointer to lpfc hba data structure.
  8737. * @vf: virtual function number
  8738. *
  8739. * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
  8740. * based on the given viftual function number, @vf.
  8741. *
  8742. * Return 0 if successful, otherwise -ENODEV.
  8743. **/
  8744. static int
  8745. lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
  8746. {
  8747. if (vf > LPFC_VIR_FUNC_MAX)
  8748. return -ENODEV;
  8749. phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
  8750. vf * LPFC_VFR_PAGE_SIZE +
  8751. LPFC_ULP0_RQ_DOORBELL);
  8752. phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
  8753. vf * LPFC_VFR_PAGE_SIZE +
  8754. LPFC_ULP0_WQ_DOORBELL);
  8755. phba->sli4_hba.CQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
  8756. vf * LPFC_VFR_PAGE_SIZE +
  8757. LPFC_EQCQ_DOORBELL);
  8758. phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
  8759. phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
  8760. vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
  8761. phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
  8762. vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
  8763. return 0;
  8764. }
  8765. /**
  8766. * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
  8767. * @phba: pointer to lpfc hba data structure.
  8768. *
  8769. * This routine is invoked to create the bootstrap mailbox
  8770. * region consistent with the SLI-4 interface spec. This
  8771. * routine allocates all memory necessary to communicate
  8772. * mailbox commands to the port and sets up all alignment
  8773. * needs. No locks are expected to be held when calling
  8774. * this routine.
  8775. *
  8776. * Return codes
  8777. * 0 - successful
  8778. * -ENOMEM - could not allocated memory.
  8779. **/
  8780. static int
  8781. lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
  8782. {
  8783. uint32_t bmbx_size;
  8784. struct lpfc_dmabuf *dmabuf;
  8785. struct dma_address *dma_address;
  8786. uint32_t pa_addr;
  8787. uint64_t phys_addr;
  8788. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  8789. if (!dmabuf)
  8790. return -ENOMEM;
  8791. /*
  8792. * The bootstrap mailbox region is comprised of 2 parts
  8793. * plus an alignment restriction of 16 bytes.
  8794. */
  8795. bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
  8796. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, bmbx_size,
  8797. &dmabuf->phys, GFP_KERNEL);
  8798. if (!dmabuf->virt) {
  8799. kfree(dmabuf);
  8800. return -ENOMEM;
  8801. }
  8802. /*
  8803. * Initialize the bootstrap mailbox pointers now so that the register
  8804. * operations are simple later. The mailbox dma address is required
  8805. * to be 16-byte aligned. Also align the virtual memory as each
  8806. * maibox is copied into the bmbx mailbox region before issuing the
  8807. * command to the port.
  8808. */
  8809. phba->sli4_hba.bmbx.dmabuf = dmabuf;
  8810. phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
  8811. phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
  8812. LPFC_ALIGN_16_BYTE);
  8813. phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
  8814. LPFC_ALIGN_16_BYTE);
  8815. /*
  8816. * Set the high and low physical addresses now. The SLI4 alignment
  8817. * requirement is 16 bytes and the mailbox is posted to the port
  8818. * as two 30-bit addresses. The other data is a bit marking whether
  8819. * the 30-bit address is the high or low address.
  8820. * Upcast bmbx aphys to 64bits so shift instruction compiles
  8821. * clean on 32 bit machines.
  8822. */
  8823. dma_address = &phba->sli4_hba.bmbx.dma_address;
  8824. phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
  8825. pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
  8826. dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
  8827. LPFC_BMBX_BIT1_ADDR_HI);
  8828. pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
  8829. dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
  8830. LPFC_BMBX_BIT1_ADDR_LO);
  8831. return 0;
  8832. }
  8833. /**
  8834. * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
  8835. * @phba: pointer to lpfc hba data structure.
  8836. *
  8837. * This routine is invoked to teardown the bootstrap mailbox
  8838. * region and release all host resources. This routine requires
  8839. * the caller to ensure all mailbox commands recovered, no
  8840. * additional mailbox comands are sent, and interrupts are disabled
  8841. * before calling this routine.
  8842. *
  8843. **/
  8844. static void
  8845. lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
  8846. {
  8847. dma_free_coherent(&phba->pcidev->dev,
  8848. phba->sli4_hba.bmbx.bmbx_size,
  8849. phba->sli4_hba.bmbx.dmabuf->virt,
  8850. phba->sli4_hba.bmbx.dmabuf->phys);
  8851. kfree(phba->sli4_hba.bmbx.dmabuf);
  8852. memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
  8853. }
  8854. static const char * const lpfc_topo_to_str[] = {
  8855. "Loop then P2P",
  8856. "Loopback",
  8857. "P2P Only",
  8858. "Unsupported",
  8859. "Loop Only",
  8860. "Unsupported",
  8861. "P2P then Loop",
  8862. };
  8863. #define LINK_FLAGS_DEF 0x0
  8864. #define LINK_FLAGS_P2P 0x1
  8865. #define LINK_FLAGS_LOOP 0x2
  8866. /**
  8867. * lpfc_map_topology - Map the topology read from READ_CONFIG
  8868. * @phba: pointer to lpfc hba data structure.
  8869. * @rd_config: pointer to read config data
  8870. *
  8871. * This routine is invoked to map the topology values as read
  8872. * from the read config mailbox command. If the persistent
  8873. * topology feature is supported, the firmware will provide the
  8874. * saved topology information to be used in INIT_LINK
  8875. **/
  8876. static void
  8877. lpfc_map_topology(struct lpfc_hba *phba, struct lpfc_mbx_read_config *rd_config)
  8878. {
  8879. u8 ptv, tf, pt;
  8880. ptv = bf_get(lpfc_mbx_rd_conf_ptv, rd_config);
  8881. tf = bf_get(lpfc_mbx_rd_conf_tf, rd_config);
  8882. pt = bf_get(lpfc_mbx_rd_conf_pt, rd_config);
  8883. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8884. "2027 Read Config Data : ptv:0x%x, tf:0x%x pt:0x%x",
  8885. ptv, tf, pt);
  8886. if (!ptv) {
  8887. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8888. "2019 FW does not support persistent topology "
  8889. "Using driver parameter defined value [%s]",
  8890. lpfc_topo_to_str[phba->cfg_topology]);
  8891. return;
  8892. }
  8893. /* FW supports persistent topology - override module parameter value */
  8894. set_bit(HBA_PERSISTENT_TOPO, &phba->hba_flag);
  8895. /* if ASIC_GEN_NUM >= 0xC) */
  8896. if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
  8897. LPFC_SLI_INTF_IF_TYPE_6) ||
  8898. (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
  8899. LPFC_SLI_INTF_FAMILY_G6)) {
  8900. if (!tf)
  8901. phba->cfg_topology = ((pt == LINK_FLAGS_LOOP)
  8902. ? FLAGS_TOPOLOGY_MODE_LOOP
  8903. : FLAGS_TOPOLOGY_MODE_PT_PT);
  8904. else
  8905. clear_bit(HBA_PERSISTENT_TOPO, &phba->hba_flag);
  8906. } else { /* G5 */
  8907. if (tf)
  8908. /* If topology failover set - pt is '0' or '1' */
  8909. phba->cfg_topology = (pt ? FLAGS_TOPOLOGY_MODE_PT_LOOP :
  8910. FLAGS_TOPOLOGY_MODE_LOOP_PT);
  8911. else
  8912. phba->cfg_topology = ((pt == LINK_FLAGS_P2P)
  8913. ? FLAGS_TOPOLOGY_MODE_PT_PT
  8914. : FLAGS_TOPOLOGY_MODE_LOOP);
  8915. }
  8916. if (test_bit(HBA_PERSISTENT_TOPO, &phba->hba_flag))
  8917. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8918. "2020 Using persistent topology value [%s]",
  8919. lpfc_topo_to_str[phba->cfg_topology]);
  8920. else
  8921. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8922. "2021 Invalid topology values from FW "
  8923. "Using driver parameter defined value [%s]",
  8924. lpfc_topo_to_str[phba->cfg_topology]);
  8925. }
  8926. /**
  8927. * lpfc_sli4_read_config - Get the config parameters.
  8928. * @phba: pointer to lpfc hba data structure.
  8929. *
  8930. * This routine is invoked to read the configuration parameters from the HBA.
  8931. * The configuration parameters are used to set the base and maximum values
  8932. * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
  8933. * allocation for the port.
  8934. *
  8935. * Return codes
  8936. * 0 - successful
  8937. * -ENOMEM - No available memory
  8938. * -EIO - The mailbox failed to complete successfully.
  8939. **/
  8940. int
  8941. lpfc_sli4_read_config(struct lpfc_hba *phba)
  8942. {
  8943. LPFC_MBOXQ_t *pmb;
  8944. struct lpfc_mbx_read_config *rd_config;
  8945. union lpfc_sli4_cfg_shdr *shdr;
  8946. uint32_t shdr_status, shdr_add_status;
  8947. struct lpfc_mbx_get_func_cfg *get_func_cfg;
  8948. struct lpfc_rsrc_desc_fcfcoe *desc;
  8949. char *pdesc_0;
  8950. uint16_t forced_link_speed;
  8951. uint32_t if_type, qmin, fawwpn;
  8952. int length, i, rc = 0, rc2;
  8953. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  8954. if (!pmb) {
  8955. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  8956. "2011 Unable to allocate memory for issuing "
  8957. "SLI_CONFIG_SPECIAL mailbox command\n");
  8958. return -ENOMEM;
  8959. }
  8960. lpfc_read_config(phba, pmb);
  8961. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  8962. if (rc != MBX_SUCCESS) {
  8963. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  8964. "2012 Mailbox failed , mbxCmd x%x "
  8965. "READ_CONFIG, mbxStatus x%x\n",
  8966. bf_get(lpfc_mqe_command, &pmb->u.mqe),
  8967. bf_get(lpfc_mqe_status, &pmb->u.mqe));
  8968. rc = -EIO;
  8969. } else {
  8970. rd_config = &pmb->u.mqe.un.rd_config;
  8971. if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
  8972. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  8973. phba->sli4_hba.lnk_info.lnk_tp =
  8974. bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
  8975. phba->sli4_hba.lnk_info.lnk_no =
  8976. bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
  8977. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8978. "3081 lnk_type:%d, lnk_numb:%d\n",
  8979. phba->sli4_hba.lnk_info.lnk_tp,
  8980. phba->sli4_hba.lnk_info.lnk_no);
  8981. } else
  8982. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8983. "3082 Mailbox (x%x) returned ldv:x0\n",
  8984. bf_get(lpfc_mqe_command, &pmb->u.mqe));
  8985. if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
  8986. phba->bbcredit_support = 1;
  8987. phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
  8988. }
  8989. fawwpn = bf_get(lpfc_mbx_rd_conf_fawwpn, rd_config);
  8990. if (fawwpn) {
  8991. lpfc_printf_log(phba, KERN_INFO,
  8992. LOG_INIT | LOG_DISCOVERY,
  8993. "2702 READ_CONFIG: FA-PWWN is "
  8994. "configured on\n");
  8995. phba->sli4_hba.fawwpn_flag |= LPFC_FAWWPN_CONFIG;
  8996. } else {
  8997. /* Clear FW configured flag, preserve driver flag */
  8998. phba->sli4_hba.fawwpn_flag &= ~LPFC_FAWWPN_CONFIG;
  8999. }
  9000. phba->sli4_hba.conf_trunk =
  9001. bf_get(lpfc_mbx_rd_conf_trunk, rd_config);
  9002. phba->sli4_hba.extents_in_use =
  9003. bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
  9004. phba->sli4_hba.max_cfg_param.max_xri =
  9005. bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
  9006. /* Reduce resource usage in kdump environment */
  9007. if (is_kdump_kernel() &&
  9008. phba->sli4_hba.max_cfg_param.max_xri > 512)
  9009. phba->sli4_hba.max_cfg_param.max_xri = 512;
  9010. phba->sli4_hba.max_cfg_param.xri_base =
  9011. bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
  9012. phba->sli4_hba.max_cfg_param.max_vpi =
  9013. bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
  9014. /* Limit the max we support */
  9015. if (phba->sli4_hba.max_cfg_param.max_vpi > LPFC_MAX_VPORTS)
  9016. phba->sli4_hba.max_cfg_param.max_vpi = LPFC_MAX_VPORTS;
  9017. phba->sli4_hba.max_cfg_param.vpi_base =
  9018. bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
  9019. phba->sli4_hba.max_cfg_param.max_rpi =
  9020. bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
  9021. phba->sli4_hba.max_cfg_param.rpi_base =
  9022. bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
  9023. phba->sli4_hba.max_cfg_param.max_vfi =
  9024. bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
  9025. phba->sli4_hba.max_cfg_param.vfi_base =
  9026. bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
  9027. phba->sli4_hba.max_cfg_param.max_fcfi =
  9028. bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
  9029. phba->sli4_hba.max_cfg_param.max_eq =
  9030. bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
  9031. phba->sli4_hba.max_cfg_param.max_rq =
  9032. bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
  9033. phba->sli4_hba.max_cfg_param.max_wq =
  9034. bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
  9035. phba->sli4_hba.max_cfg_param.max_cq =
  9036. bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
  9037. phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
  9038. phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
  9039. phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
  9040. phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
  9041. phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
  9042. (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
  9043. phba->max_vports = phba->max_vpi;
  9044. /* Next decide on FPIN or Signal E2E CGN support
  9045. * For congestion alarms and warnings valid combination are:
  9046. * 1. FPIN alarms / FPIN warnings
  9047. * 2. Signal alarms / Signal warnings
  9048. * 3. FPIN alarms / Signal warnings
  9049. * 4. Signal alarms / FPIN warnings
  9050. *
  9051. * Initialize the adapter frequency to 100 mSecs
  9052. */
  9053. phba->cgn_reg_fpin = LPFC_CGN_FPIN_BOTH;
  9054. phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
  9055. phba->cgn_sig_freq = lpfc_fabric_cgn_frequency;
  9056. if (lpfc_use_cgn_signal) {
  9057. if (bf_get(lpfc_mbx_rd_conf_wcs, rd_config)) {
  9058. phba->cgn_reg_signal = EDC_CG_SIG_WARN_ONLY;
  9059. phba->cgn_reg_fpin &= ~LPFC_CGN_FPIN_WARN;
  9060. }
  9061. if (bf_get(lpfc_mbx_rd_conf_acs, rd_config)) {
  9062. /* MUST support both alarm and warning
  9063. * because EDC does not support alarm alone.
  9064. */
  9065. if (phba->cgn_reg_signal !=
  9066. EDC_CG_SIG_WARN_ONLY) {
  9067. /* Must support both or none */
  9068. phba->cgn_reg_fpin = LPFC_CGN_FPIN_BOTH;
  9069. phba->cgn_reg_signal =
  9070. EDC_CG_SIG_NOTSUPPORTED;
  9071. } else {
  9072. phba->cgn_reg_signal =
  9073. EDC_CG_SIG_WARN_ALARM;
  9074. phba->cgn_reg_fpin =
  9075. LPFC_CGN_FPIN_NONE;
  9076. }
  9077. }
  9078. }
  9079. /* Set the congestion initial signal and fpin values. */
  9080. phba->cgn_init_reg_fpin = phba->cgn_reg_fpin;
  9081. phba->cgn_init_reg_signal = phba->cgn_reg_signal;
  9082. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
  9083. "6446 READ_CONFIG reg_sig x%x reg_fpin:x%x\n",
  9084. phba->cgn_reg_signal, phba->cgn_reg_fpin);
  9085. lpfc_map_topology(phba, rd_config);
  9086. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9087. "2003 cfg params Extents? %d "
  9088. "XRI(B:%d M:%d), "
  9089. "VPI(B:%d M:%d) "
  9090. "VFI(B:%d M:%d) "
  9091. "RPI(B:%d M:%d) "
  9092. "FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d lmt:x%x\n",
  9093. phba->sli4_hba.extents_in_use,
  9094. phba->sli4_hba.max_cfg_param.xri_base,
  9095. phba->sli4_hba.max_cfg_param.max_xri,
  9096. phba->sli4_hba.max_cfg_param.vpi_base,
  9097. phba->sli4_hba.max_cfg_param.max_vpi,
  9098. phba->sli4_hba.max_cfg_param.vfi_base,
  9099. phba->sli4_hba.max_cfg_param.max_vfi,
  9100. phba->sli4_hba.max_cfg_param.rpi_base,
  9101. phba->sli4_hba.max_cfg_param.max_rpi,
  9102. phba->sli4_hba.max_cfg_param.max_fcfi,
  9103. phba->sli4_hba.max_cfg_param.max_eq,
  9104. phba->sli4_hba.max_cfg_param.max_cq,
  9105. phba->sli4_hba.max_cfg_param.max_wq,
  9106. phba->sli4_hba.max_cfg_param.max_rq,
  9107. phba->lmt);
  9108. /*
  9109. * Calculate queue resources based on how
  9110. * many WQ/CQ/EQs are available.
  9111. */
  9112. qmin = phba->sli4_hba.max_cfg_param.max_wq;
  9113. if (phba->sli4_hba.max_cfg_param.max_cq < qmin)
  9114. qmin = phba->sli4_hba.max_cfg_param.max_cq;
  9115. /*
  9116. * Reserve 4 (ELS, NVME LS, MBOX, plus one extra) and
  9117. * the remainder can be used for NVME / FCP.
  9118. */
  9119. qmin -= 4;
  9120. if (phba->sli4_hba.max_cfg_param.max_eq < qmin)
  9121. qmin = phba->sli4_hba.max_cfg_param.max_eq;
  9122. /* Check to see if there is enough for default cfg */
  9123. if ((phba->cfg_irq_chann > qmin) ||
  9124. (phba->cfg_hdw_queue > qmin)) {
  9125. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9126. "2005 Reducing Queues - "
  9127. "FW resource limitation: "
  9128. "WQ %d CQ %d EQ %d: min %d: "
  9129. "IRQ %d HDWQ %d\n",
  9130. phba->sli4_hba.max_cfg_param.max_wq,
  9131. phba->sli4_hba.max_cfg_param.max_cq,
  9132. phba->sli4_hba.max_cfg_param.max_eq,
  9133. qmin, phba->cfg_irq_chann,
  9134. phba->cfg_hdw_queue);
  9135. if (phba->cfg_irq_chann > qmin)
  9136. phba->cfg_irq_chann = qmin;
  9137. if (phba->cfg_hdw_queue > qmin)
  9138. phba->cfg_hdw_queue = qmin;
  9139. }
  9140. }
  9141. if (rc)
  9142. goto read_cfg_out;
  9143. /* Update link speed if forced link speed is supported */
  9144. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9145. if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
  9146. forced_link_speed =
  9147. bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
  9148. if (forced_link_speed) {
  9149. set_bit(HBA_FORCED_LINK_SPEED, &phba->hba_flag);
  9150. switch (forced_link_speed) {
  9151. case LINK_SPEED_1G:
  9152. phba->cfg_link_speed =
  9153. LPFC_USER_LINK_SPEED_1G;
  9154. break;
  9155. case LINK_SPEED_2G:
  9156. phba->cfg_link_speed =
  9157. LPFC_USER_LINK_SPEED_2G;
  9158. break;
  9159. case LINK_SPEED_4G:
  9160. phba->cfg_link_speed =
  9161. LPFC_USER_LINK_SPEED_4G;
  9162. break;
  9163. case LINK_SPEED_8G:
  9164. phba->cfg_link_speed =
  9165. LPFC_USER_LINK_SPEED_8G;
  9166. break;
  9167. case LINK_SPEED_10G:
  9168. phba->cfg_link_speed =
  9169. LPFC_USER_LINK_SPEED_10G;
  9170. break;
  9171. case LINK_SPEED_16G:
  9172. phba->cfg_link_speed =
  9173. LPFC_USER_LINK_SPEED_16G;
  9174. break;
  9175. case LINK_SPEED_32G:
  9176. phba->cfg_link_speed =
  9177. LPFC_USER_LINK_SPEED_32G;
  9178. break;
  9179. case LINK_SPEED_64G:
  9180. phba->cfg_link_speed =
  9181. LPFC_USER_LINK_SPEED_64G;
  9182. break;
  9183. case 0xffff:
  9184. phba->cfg_link_speed =
  9185. LPFC_USER_LINK_SPEED_AUTO;
  9186. break;
  9187. default:
  9188. lpfc_printf_log(phba, KERN_ERR,
  9189. LOG_TRACE_EVENT,
  9190. "0047 Unrecognized link "
  9191. "speed : %d\n",
  9192. forced_link_speed);
  9193. phba->cfg_link_speed =
  9194. LPFC_USER_LINK_SPEED_AUTO;
  9195. }
  9196. }
  9197. }
  9198. /* Reset the DFT_HBA_Q_DEPTH to the max xri */
  9199. length = phba->sli4_hba.max_cfg_param.max_xri -
  9200. lpfc_sli4_get_els_iocb_cnt(phba);
  9201. if (phba->cfg_hba_queue_depth > length) {
  9202. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  9203. "3361 HBA queue depth changed from %d to %d\n",
  9204. phba->cfg_hba_queue_depth, length);
  9205. phba->cfg_hba_queue_depth = length;
  9206. }
  9207. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
  9208. LPFC_SLI_INTF_IF_TYPE_2)
  9209. goto read_cfg_out;
  9210. /* get the pf# and vf# for SLI4 if_type 2 port */
  9211. length = (sizeof(struct lpfc_mbx_get_func_cfg) -
  9212. sizeof(struct lpfc_sli4_cfg_mhdr));
  9213. lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
  9214. LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
  9215. length, LPFC_SLI4_MBX_EMBED);
  9216. rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  9217. shdr = (union lpfc_sli4_cfg_shdr *)
  9218. &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
  9219. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  9220. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  9221. if (rc2 || shdr_status || shdr_add_status) {
  9222. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9223. "3026 Mailbox failed , mbxCmd x%x "
  9224. "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
  9225. bf_get(lpfc_mqe_command, &pmb->u.mqe),
  9226. bf_get(lpfc_mqe_status, &pmb->u.mqe));
  9227. goto read_cfg_out;
  9228. }
  9229. /* search for fc_fcoe resrouce descriptor */
  9230. get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
  9231. pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
  9232. desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
  9233. length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
  9234. if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
  9235. length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
  9236. else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
  9237. goto read_cfg_out;
  9238. for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
  9239. desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
  9240. if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
  9241. bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
  9242. phba->sli4_hba.iov.pf_number =
  9243. bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
  9244. phba->sli4_hba.iov.vf_number =
  9245. bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
  9246. break;
  9247. }
  9248. }
  9249. if (i < LPFC_RSRC_DESC_MAX_NUM)
  9250. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9251. "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
  9252. "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
  9253. phba->sli4_hba.iov.vf_number);
  9254. else
  9255. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9256. "3028 GET_FUNCTION_CONFIG: failed to find "
  9257. "Resource Descriptor:x%x\n",
  9258. LPFC_RSRC_DESC_TYPE_FCFCOE);
  9259. read_cfg_out:
  9260. mempool_free(pmb, phba->mbox_mem_pool);
  9261. return rc;
  9262. }
  9263. /**
  9264. * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
  9265. * @phba: pointer to lpfc hba data structure.
  9266. *
  9267. * This routine is invoked to setup the port-side endian order when
  9268. * the port if_type is 0. This routine has no function for other
  9269. * if_types.
  9270. *
  9271. * Return codes
  9272. * 0 - successful
  9273. * -ENOMEM - No available memory
  9274. * -EIO - The mailbox failed to complete successfully.
  9275. **/
  9276. static int
  9277. lpfc_setup_endian_order(struct lpfc_hba *phba)
  9278. {
  9279. LPFC_MBOXQ_t *mboxq;
  9280. uint32_t if_type, rc = 0;
  9281. uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
  9282. HOST_ENDIAN_HIGH_WORD1};
  9283. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9284. switch (if_type) {
  9285. case LPFC_SLI_INTF_IF_TYPE_0:
  9286. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
  9287. GFP_KERNEL);
  9288. if (!mboxq) {
  9289. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9290. "0492 Unable to allocate memory for "
  9291. "issuing SLI_CONFIG_SPECIAL mailbox "
  9292. "command\n");
  9293. return -ENOMEM;
  9294. }
  9295. /*
  9296. * The SLI4_CONFIG_SPECIAL mailbox command requires the first
  9297. * two words to contain special data values and no other data.
  9298. */
  9299. memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
  9300. memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
  9301. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  9302. if (rc != MBX_SUCCESS) {
  9303. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9304. "0493 SLI_CONFIG_SPECIAL mailbox "
  9305. "failed with status x%x\n",
  9306. rc);
  9307. rc = -EIO;
  9308. }
  9309. mempool_free(mboxq, phba->mbox_mem_pool);
  9310. break;
  9311. case LPFC_SLI_INTF_IF_TYPE_6:
  9312. case LPFC_SLI_INTF_IF_TYPE_2:
  9313. case LPFC_SLI_INTF_IF_TYPE_1:
  9314. default:
  9315. break;
  9316. }
  9317. return rc;
  9318. }
  9319. /**
  9320. * lpfc_sli4_queue_verify - Verify and update EQ counts
  9321. * @phba: pointer to lpfc hba data structure.
  9322. *
  9323. * This routine is invoked to check the user settable queue counts for EQs.
  9324. * After this routine is called the counts will be set to valid values that
  9325. * adhere to the constraints of the system's interrupt vectors and the port's
  9326. * queue resources.
  9327. *
  9328. * Return codes
  9329. * 0 - successful
  9330. * -ENOMEM - No available memory
  9331. **/
  9332. static int
  9333. lpfc_sli4_queue_verify(struct lpfc_hba *phba)
  9334. {
  9335. /*
  9336. * Sanity check for configured queue parameters against the run-time
  9337. * device parameters
  9338. */
  9339. if (phba->nvmet_support) {
  9340. if (phba->cfg_hdw_queue < phba->cfg_nvmet_mrq)
  9341. phba->cfg_nvmet_mrq = phba->cfg_hdw_queue;
  9342. if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
  9343. phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
  9344. }
  9345. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9346. "2574 IO channels: hdwQ %d IRQ %d MRQ: %d\n",
  9347. phba->cfg_hdw_queue, phba->cfg_irq_chann,
  9348. phba->cfg_nvmet_mrq);
  9349. /* Get EQ depth from module parameter, fake the default for now */
  9350. phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
  9351. phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
  9352. /* Get CQ depth from module parameter, fake the default for now */
  9353. phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
  9354. phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
  9355. return 0;
  9356. }
  9357. static int
  9358. lpfc_alloc_io_wq_cq(struct lpfc_hba *phba, int idx)
  9359. {
  9360. struct lpfc_queue *qdesc;
  9361. u32 wqesize;
  9362. int cpu;
  9363. cpu = lpfc_find_cpu_handle(phba, idx, LPFC_FIND_BY_HDWQ);
  9364. /* Create Fast Path IO CQs */
  9365. if (phba->enab_exp_wqcq_pages)
  9366. /* Increase the CQ size when WQEs contain an embedded cdb */
  9367. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
  9368. phba->sli4_hba.cq_esize,
  9369. LPFC_CQE_EXP_COUNT, cpu);
  9370. else
  9371. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  9372. phba->sli4_hba.cq_esize,
  9373. phba->sli4_hba.cq_ecount, cpu);
  9374. if (!qdesc) {
  9375. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9376. "0499 Failed allocate fast-path IO CQ (%d)\n",
  9377. idx);
  9378. return 1;
  9379. }
  9380. qdesc->qe_valid = 1;
  9381. qdesc->hdwq = idx;
  9382. qdesc->chann = cpu;
  9383. phba->sli4_hba.hdwq[idx].io_cq = qdesc;
  9384. /* Create Fast Path IO WQs */
  9385. if (phba->enab_exp_wqcq_pages) {
  9386. /* Increase the WQ size when WQEs contain an embedded cdb */
  9387. wqesize = (phba->fcp_embed_io) ?
  9388. LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
  9389. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
  9390. wqesize,
  9391. LPFC_WQE_EXP_COUNT, cpu);
  9392. } else
  9393. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  9394. phba->sli4_hba.wq_esize,
  9395. phba->sli4_hba.wq_ecount, cpu);
  9396. if (!qdesc) {
  9397. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9398. "0503 Failed allocate fast-path IO WQ (%d)\n",
  9399. idx);
  9400. return 1;
  9401. }
  9402. qdesc->hdwq = idx;
  9403. qdesc->chann = cpu;
  9404. phba->sli4_hba.hdwq[idx].io_wq = qdesc;
  9405. list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
  9406. return 0;
  9407. }
  9408. /**
  9409. * lpfc_sli4_queue_create - Create all the SLI4 queues
  9410. * @phba: pointer to lpfc hba data structure.
  9411. *
  9412. * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
  9413. * operation. For each SLI4 queue type, the parameters such as queue entry
  9414. * count (queue depth) shall be taken from the module parameter. For now,
  9415. * we just use some constant number as place holder.
  9416. *
  9417. * Return codes
  9418. * 0 - successful
  9419. * -ENOMEM - No availble memory
  9420. * -EIO - The mailbox failed to complete successfully.
  9421. **/
  9422. int
  9423. lpfc_sli4_queue_create(struct lpfc_hba *phba)
  9424. {
  9425. struct lpfc_queue *qdesc;
  9426. int idx, cpu, eqcpu;
  9427. struct lpfc_sli4_hdw_queue *qp;
  9428. struct lpfc_vector_map_info *cpup;
  9429. struct lpfc_vector_map_info *eqcpup;
  9430. struct lpfc_eq_intr_info *eqi;
  9431. u32 wqesize;
  9432. /*
  9433. * Create HBA Record arrays.
  9434. * Both NVME and FCP will share that same vectors / EQs
  9435. */
  9436. phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
  9437. phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
  9438. phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
  9439. phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
  9440. phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
  9441. phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
  9442. phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
  9443. phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
  9444. phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
  9445. phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
  9446. if (!phba->sli4_hba.hdwq) {
  9447. phba->sli4_hba.hdwq = kcalloc(
  9448. phba->cfg_hdw_queue, sizeof(struct lpfc_sli4_hdw_queue),
  9449. GFP_KERNEL);
  9450. if (!phba->sli4_hba.hdwq) {
  9451. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9452. "6427 Failed allocate memory for "
  9453. "fast-path Hardware Queue array\n");
  9454. goto out_error;
  9455. }
  9456. /* Prepare hardware queues to take IO buffers */
  9457. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  9458. qp = &phba->sli4_hba.hdwq[idx];
  9459. spin_lock_init(&qp->io_buf_list_get_lock);
  9460. spin_lock_init(&qp->io_buf_list_put_lock);
  9461. INIT_LIST_HEAD(&qp->lpfc_io_buf_list_get);
  9462. INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
  9463. qp->get_io_bufs = 0;
  9464. qp->put_io_bufs = 0;
  9465. qp->total_io_bufs = 0;
  9466. spin_lock_init(&qp->abts_io_buf_list_lock);
  9467. INIT_LIST_HEAD(&qp->lpfc_abts_io_buf_list);
  9468. qp->abts_scsi_io_bufs = 0;
  9469. qp->abts_nvme_io_bufs = 0;
  9470. INIT_LIST_HEAD(&qp->sgl_list);
  9471. INIT_LIST_HEAD(&qp->cmd_rsp_buf_list);
  9472. spin_lock_init(&qp->hdwq_lock);
  9473. }
  9474. }
  9475. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  9476. if (phba->nvmet_support) {
  9477. phba->sli4_hba.nvmet_cqset = kcalloc(
  9478. phba->cfg_nvmet_mrq,
  9479. sizeof(struct lpfc_queue *),
  9480. GFP_KERNEL);
  9481. if (!phba->sli4_hba.nvmet_cqset) {
  9482. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9483. "3121 Fail allocate memory for "
  9484. "fast-path CQ set array\n");
  9485. goto out_error;
  9486. }
  9487. phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
  9488. phba->cfg_nvmet_mrq,
  9489. sizeof(struct lpfc_queue *),
  9490. GFP_KERNEL);
  9491. if (!phba->sli4_hba.nvmet_mrq_hdr) {
  9492. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9493. "3122 Fail allocate memory for "
  9494. "fast-path RQ set hdr array\n");
  9495. goto out_error;
  9496. }
  9497. phba->sli4_hba.nvmet_mrq_data = kcalloc(
  9498. phba->cfg_nvmet_mrq,
  9499. sizeof(struct lpfc_queue *),
  9500. GFP_KERNEL);
  9501. if (!phba->sli4_hba.nvmet_mrq_data) {
  9502. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9503. "3124 Fail allocate memory for "
  9504. "fast-path RQ set data array\n");
  9505. goto out_error;
  9506. }
  9507. }
  9508. }
  9509. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
  9510. /* Create HBA Event Queues (EQs) */
  9511. for_each_present_cpu(cpu) {
  9512. /* We only want to create 1 EQ per vector, even though
  9513. * multiple CPUs might be using that vector. so only
  9514. * selects the CPUs that are LPFC_CPU_FIRST_IRQ.
  9515. */
  9516. cpup = &phba->sli4_hba.cpu_map[cpu];
  9517. if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
  9518. continue;
  9519. /* Get a ptr to the Hardware Queue associated with this CPU */
  9520. qp = &phba->sli4_hba.hdwq[cpup->hdwq];
  9521. /* Allocate an EQ */
  9522. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  9523. phba->sli4_hba.eq_esize,
  9524. phba->sli4_hba.eq_ecount, cpu);
  9525. if (!qdesc) {
  9526. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9527. "0497 Failed allocate EQ (%d)\n",
  9528. cpup->hdwq);
  9529. goto out_error;
  9530. }
  9531. qdesc->qe_valid = 1;
  9532. qdesc->hdwq = cpup->hdwq;
  9533. qdesc->chann = cpu; /* First CPU this EQ is affinitized to */
  9534. qdesc->last_cpu = qdesc->chann;
  9535. /* Save the allocated EQ in the Hardware Queue */
  9536. qp->hba_eq = qdesc;
  9537. eqi = per_cpu_ptr(phba->sli4_hba.eq_info, qdesc->last_cpu);
  9538. list_add(&qdesc->cpu_list, &eqi->list);
  9539. }
  9540. /* Now we need to populate the other Hardware Queues, that share
  9541. * an IRQ vector, with the associated EQ ptr.
  9542. */
  9543. for_each_present_cpu(cpu) {
  9544. cpup = &phba->sli4_hba.cpu_map[cpu];
  9545. /* Check for EQ already allocated in previous loop */
  9546. if (cpup->flag & LPFC_CPU_FIRST_IRQ)
  9547. continue;
  9548. /* Check for multiple CPUs per hdwq */
  9549. qp = &phba->sli4_hba.hdwq[cpup->hdwq];
  9550. if (qp->hba_eq)
  9551. continue;
  9552. /* We need to share an EQ for this hdwq */
  9553. eqcpu = lpfc_find_cpu_handle(phba, cpup->eq, LPFC_FIND_BY_EQ);
  9554. eqcpup = &phba->sli4_hba.cpu_map[eqcpu];
  9555. qp->hba_eq = phba->sli4_hba.hdwq[eqcpup->hdwq].hba_eq;
  9556. }
  9557. /* Allocate IO Path SLI4 CQ/WQs */
  9558. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  9559. if (lpfc_alloc_io_wq_cq(phba, idx))
  9560. goto out_error;
  9561. }
  9562. if (phba->nvmet_support) {
  9563. for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
  9564. cpu = lpfc_find_cpu_handle(phba, idx,
  9565. LPFC_FIND_BY_HDWQ);
  9566. qdesc = lpfc_sli4_queue_alloc(phba,
  9567. LPFC_DEFAULT_PAGE_SIZE,
  9568. phba->sli4_hba.cq_esize,
  9569. phba->sli4_hba.cq_ecount,
  9570. cpu);
  9571. if (!qdesc) {
  9572. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9573. "3142 Failed allocate NVME "
  9574. "CQ Set (%d)\n", idx);
  9575. goto out_error;
  9576. }
  9577. qdesc->qe_valid = 1;
  9578. qdesc->hdwq = idx;
  9579. qdesc->chann = cpu;
  9580. phba->sli4_hba.nvmet_cqset[idx] = qdesc;
  9581. }
  9582. }
  9583. /*
  9584. * Create Slow Path Completion Queues (CQs)
  9585. */
  9586. cpu = lpfc_find_cpu_handle(phba, 0, LPFC_FIND_BY_EQ);
  9587. /* Create slow-path Mailbox Command Complete Queue */
  9588. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  9589. phba->sli4_hba.cq_esize,
  9590. phba->sli4_hba.cq_ecount, cpu);
  9591. if (!qdesc) {
  9592. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9593. "0500 Failed allocate slow-path mailbox CQ\n");
  9594. goto out_error;
  9595. }
  9596. qdesc->qe_valid = 1;
  9597. phba->sli4_hba.mbx_cq = qdesc;
  9598. /* Create slow-path ELS Complete Queue */
  9599. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  9600. phba->sli4_hba.cq_esize,
  9601. phba->sli4_hba.cq_ecount, cpu);
  9602. if (!qdesc) {
  9603. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9604. "0501 Failed allocate slow-path ELS CQ\n");
  9605. goto out_error;
  9606. }
  9607. qdesc->qe_valid = 1;
  9608. qdesc->chann = cpu;
  9609. phba->sli4_hba.els_cq = qdesc;
  9610. /*
  9611. * Create Slow Path Work Queues (WQs)
  9612. */
  9613. /* Create Mailbox Command Queue */
  9614. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  9615. phba->sli4_hba.mq_esize,
  9616. phba->sli4_hba.mq_ecount, cpu);
  9617. if (!qdesc) {
  9618. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9619. "0505 Failed allocate slow-path MQ\n");
  9620. goto out_error;
  9621. }
  9622. qdesc->chann = cpu;
  9623. phba->sli4_hba.mbx_wq = qdesc;
  9624. /*
  9625. * Create ELS Work Queues
  9626. */
  9627. /*
  9628. * Create slow-path ELS Work Queue.
  9629. * Increase the ELS WQ size when WQEs contain an embedded cdb
  9630. */
  9631. wqesize = (phba->fcp_embed_io) ?
  9632. LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
  9633. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  9634. wqesize,
  9635. phba->sli4_hba.wq_ecount, cpu);
  9636. if (!qdesc) {
  9637. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9638. "0504 Failed allocate slow-path ELS WQ\n");
  9639. goto out_error;
  9640. }
  9641. qdesc->chann = cpu;
  9642. phba->sli4_hba.els_wq = qdesc;
  9643. list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
  9644. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  9645. /* Create NVME LS Complete Queue */
  9646. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  9647. phba->sli4_hba.cq_esize,
  9648. phba->sli4_hba.cq_ecount, cpu);
  9649. if (!qdesc) {
  9650. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9651. "6079 Failed allocate NVME LS CQ\n");
  9652. goto out_error;
  9653. }
  9654. qdesc->chann = cpu;
  9655. qdesc->qe_valid = 1;
  9656. phba->sli4_hba.nvmels_cq = qdesc;
  9657. /* Create NVME LS Work Queue */
  9658. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  9659. phba->sli4_hba.wq_esize,
  9660. phba->sli4_hba.wq_ecount, cpu);
  9661. if (!qdesc) {
  9662. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9663. "6080 Failed allocate NVME LS WQ\n");
  9664. goto out_error;
  9665. }
  9666. qdesc->chann = cpu;
  9667. phba->sli4_hba.nvmels_wq = qdesc;
  9668. list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
  9669. }
  9670. /*
  9671. * Create Receive Queue (RQ)
  9672. */
  9673. /* Create Receive Queue for header */
  9674. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  9675. phba->sli4_hba.rq_esize,
  9676. phba->sli4_hba.rq_ecount, cpu);
  9677. if (!qdesc) {
  9678. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9679. "0506 Failed allocate receive HRQ\n");
  9680. goto out_error;
  9681. }
  9682. phba->sli4_hba.hdr_rq = qdesc;
  9683. /* Create Receive Queue for data */
  9684. qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
  9685. phba->sli4_hba.rq_esize,
  9686. phba->sli4_hba.rq_ecount, cpu);
  9687. if (!qdesc) {
  9688. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9689. "0507 Failed allocate receive DRQ\n");
  9690. goto out_error;
  9691. }
  9692. phba->sli4_hba.dat_rq = qdesc;
  9693. if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
  9694. phba->nvmet_support) {
  9695. for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
  9696. cpu = lpfc_find_cpu_handle(phba, idx,
  9697. LPFC_FIND_BY_HDWQ);
  9698. /* Create NVMET Receive Queue for header */
  9699. qdesc = lpfc_sli4_queue_alloc(phba,
  9700. LPFC_DEFAULT_PAGE_SIZE,
  9701. phba->sli4_hba.rq_esize,
  9702. LPFC_NVMET_RQE_DEF_COUNT,
  9703. cpu);
  9704. if (!qdesc) {
  9705. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9706. "3146 Failed allocate "
  9707. "receive HRQ\n");
  9708. goto out_error;
  9709. }
  9710. qdesc->hdwq = idx;
  9711. phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
  9712. /* Only needed for header of RQ pair */
  9713. qdesc->rqbp = kzalloc_node(sizeof(*qdesc->rqbp),
  9714. GFP_KERNEL,
  9715. cpu_to_node(cpu));
  9716. if (qdesc->rqbp == NULL) {
  9717. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9718. "6131 Failed allocate "
  9719. "Header RQBP\n");
  9720. goto out_error;
  9721. }
  9722. /* Put list in known state in case driver load fails. */
  9723. INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
  9724. /* Create NVMET Receive Queue for data */
  9725. qdesc = lpfc_sli4_queue_alloc(phba,
  9726. LPFC_DEFAULT_PAGE_SIZE,
  9727. phba->sli4_hba.rq_esize,
  9728. LPFC_NVMET_RQE_DEF_COUNT,
  9729. cpu);
  9730. if (!qdesc) {
  9731. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9732. "3156 Failed allocate "
  9733. "receive DRQ\n");
  9734. goto out_error;
  9735. }
  9736. qdesc->hdwq = idx;
  9737. phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
  9738. }
  9739. }
  9740. /* Clear NVME stats */
  9741. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  9742. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  9743. memset(&phba->sli4_hba.hdwq[idx].nvme_cstat, 0,
  9744. sizeof(phba->sli4_hba.hdwq[idx].nvme_cstat));
  9745. }
  9746. }
  9747. /* Clear SCSI stats */
  9748. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
  9749. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  9750. memset(&phba->sli4_hba.hdwq[idx].scsi_cstat, 0,
  9751. sizeof(phba->sli4_hba.hdwq[idx].scsi_cstat));
  9752. }
  9753. }
  9754. return 0;
  9755. out_error:
  9756. lpfc_sli4_queue_destroy(phba);
  9757. return -ENOMEM;
  9758. }
  9759. static inline void
  9760. __lpfc_sli4_release_queue(struct lpfc_queue **qp)
  9761. {
  9762. if (*qp != NULL) {
  9763. lpfc_sli4_queue_free(*qp);
  9764. *qp = NULL;
  9765. }
  9766. }
  9767. static inline void
  9768. lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
  9769. {
  9770. int idx;
  9771. if (*qs == NULL)
  9772. return;
  9773. for (idx = 0; idx < max; idx++)
  9774. __lpfc_sli4_release_queue(&(*qs)[idx]);
  9775. kfree(*qs);
  9776. *qs = NULL;
  9777. }
  9778. static inline void
  9779. lpfc_sli4_release_hdwq(struct lpfc_hba *phba)
  9780. {
  9781. struct lpfc_sli4_hdw_queue *hdwq;
  9782. struct lpfc_queue *eq;
  9783. uint32_t idx;
  9784. hdwq = phba->sli4_hba.hdwq;
  9785. /* Loop thru all Hardware Queues */
  9786. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  9787. /* Free the CQ/WQ corresponding to the Hardware Queue */
  9788. lpfc_sli4_queue_free(hdwq[idx].io_cq);
  9789. lpfc_sli4_queue_free(hdwq[idx].io_wq);
  9790. hdwq[idx].hba_eq = NULL;
  9791. hdwq[idx].io_cq = NULL;
  9792. hdwq[idx].io_wq = NULL;
  9793. if (phba->cfg_xpsgl && !phba->nvmet_support)
  9794. lpfc_free_sgl_per_hdwq(phba, &hdwq[idx]);
  9795. lpfc_free_cmd_rsp_buf_per_hdwq(phba, &hdwq[idx]);
  9796. }
  9797. /* Loop thru all IRQ vectors */
  9798. for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
  9799. /* Free the EQ corresponding to the IRQ vector */
  9800. eq = phba->sli4_hba.hba_eq_hdl[idx].eq;
  9801. lpfc_sli4_queue_free(eq);
  9802. phba->sli4_hba.hba_eq_hdl[idx].eq = NULL;
  9803. }
  9804. }
  9805. /**
  9806. * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
  9807. * @phba: pointer to lpfc hba data structure.
  9808. *
  9809. * This routine is invoked to release all the SLI4 queues with the FCoE HBA
  9810. * operation.
  9811. *
  9812. * Return codes
  9813. * 0 - successful
  9814. * -ENOMEM - No available memory
  9815. * -EIO - The mailbox failed to complete successfully.
  9816. **/
  9817. void
  9818. lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
  9819. {
  9820. /*
  9821. * Set FREE_INIT before beginning to free the queues.
  9822. * Wait until the users of queues to acknowledge to
  9823. * release queues by clearing FREE_WAIT.
  9824. */
  9825. spin_lock_irq(&phba->hbalock);
  9826. phba->sli.sli_flag |= LPFC_QUEUE_FREE_INIT;
  9827. while (phba->sli.sli_flag & LPFC_QUEUE_FREE_WAIT) {
  9828. spin_unlock_irq(&phba->hbalock);
  9829. msleep(20);
  9830. spin_lock_irq(&phba->hbalock);
  9831. }
  9832. spin_unlock_irq(&phba->hbalock);
  9833. lpfc_sli4_cleanup_poll_list(phba);
  9834. /* Release HBA eqs */
  9835. if (phba->sli4_hba.hdwq)
  9836. lpfc_sli4_release_hdwq(phba);
  9837. if (phba->nvmet_support) {
  9838. lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
  9839. phba->cfg_nvmet_mrq);
  9840. lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
  9841. phba->cfg_nvmet_mrq);
  9842. lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
  9843. phba->cfg_nvmet_mrq);
  9844. }
  9845. /* Release mailbox command work queue */
  9846. __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
  9847. /* Release ELS work queue */
  9848. __lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
  9849. /* Release ELS work queue */
  9850. __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
  9851. /* Release unsolicited receive queue */
  9852. __lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
  9853. __lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
  9854. /* Release ELS complete queue */
  9855. __lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
  9856. /* Release NVME LS complete queue */
  9857. __lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
  9858. /* Release mailbox command complete queue */
  9859. __lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
  9860. /* Everything on this list has been freed */
  9861. INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
  9862. /* Done with freeing the queues */
  9863. spin_lock_irq(&phba->hbalock);
  9864. phba->sli.sli_flag &= ~LPFC_QUEUE_FREE_INIT;
  9865. spin_unlock_irq(&phba->hbalock);
  9866. }
  9867. int
  9868. lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
  9869. {
  9870. struct lpfc_rqb *rqbp;
  9871. struct lpfc_dmabuf *h_buf;
  9872. struct rqb_dmabuf *rqb_buffer;
  9873. rqbp = rq->rqbp;
  9874. while (!list_empty(&rqbp->rqb_buffer_list)) {
  9875. list_remove_head(&rqbp->rqb_buffer_list, h_buf,
  9876. struct lpfc_dmabuf, list);
  9877. rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
  9878. (rqbp->rqb_free_buffer)(phba, rqb_buffer);
  9879. rqbp->buffer_count--;
  9880. }
  9881. return 1;
  9882. }
  9883. static int
  9884. lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
  9885. struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
  9886. int qidx, uint32_t qtype)
  9887. {
  9888. struct lpfc_sli_ring *pring;
  9889. int rc;
  9890. if (!eq || !cq || !wq) {
  9891. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9892. "6085 Fast-path %s (%d) not allocated\n",
  9893. ((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
  9894. return -ENOMEM;
  9895. }
  9896. /* create the Cq first */
  9897. rc = lpfc_cq_create(phba, cq, eq,
  9898. (qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
  9899. if (rc) {
  9900. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9901. "6086 Failed setup of CQ (%d), rc = 0x%x\n",
  9902. qidx, (uint32_t)rc);
  9903. return rc;
  9904. }
  9905. if (qtype != LPFC_MBOX) {
  9906. /* Setup cq_map for fast lookup */
  9907. if (cq_map)
  9908. *cq_map = cq->queue_id;
  9909. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  9910. "6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
  9911. qidx, cq->queue_id, qidx, eq->queue_id);
  9912. /* create the wq */
  9913. rc = lpfc_wq_create(phba, wq, cq, qtype);
  9914. if (rc) {
  9915. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9916. "4618 Fail setup fastpath WQ (%d), rc = 0x%x\n",
  9917. qidx, (uint32_t)rc);
  9918. /* no need to tear down cq - caller will do so */
  9919. return rc;
  9920. }
  9921. /* Bind this CQ/WQ to the NVME ring */
  9922. pring = wq->pring;
  9923. pring->sli.sli4.wqp = (void *)wq;
  9924. cq->pring = pring;
  9925. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  9926. "2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
  9927. qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
  9928. } else {
  9929. rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
  9930. if (rc) {
  9931. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  9932. "0539 Failed setup of slow-path MQ: "
  9933. "rc = 0x%x\n", rc);
  9934. /* no need to tear down cq - caller will do so */
  9935. return rc;
  9936. }
  9937. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  9938. "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
  9939. phba->sli4_hba.mbx_wq->queue_id,
  9940. phba->sli4_hba.mbx_cq->queue_id);
  9941. }
  9942. return 0;
  9943. }
  9944. /**
  9945. * lpfc_setup_cq_lookup - Setup the CQ lookup table
  9946. * @phba: pointer to lpfc hba data structure.
  9947. *
  9948. * This routine will populate the cq_lookup table by all
  9949. * available CQ queue_id's.
  9950. **/
  9951. static void
  9952. lpfc_setup_cq_lookup(struct lpfc_hba *phba)
  9953. {
  9954. struct lpfc_queue *eq, *childq;
  9955. int qidx;
  9956. memset(phba->sli4_hba.cq_lookup, 0,
  9957. (sizeof(struct lpfc_queue *) * (phba->sli4_hba.cq_max + 1)));
  9958. /* Loop thru all IRQ vectors */
  9959. for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
  9960. /* Get the EQ corresponding to the IRQ vector */
  9961. eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
  9962. if (!eq)
  9963. continue;
  9964. /* Loop through all CQs associated with that EQ */
  9965. list_for_each_entry(childq, &eq->child_list, list) {
  9966. if (childq->queue_id > phba->sli4_hba.cq_max)
  9967. continue;
  9968. if (childq->subtype == LPFC_IO)
  9969. phba->sli4_hba.cq_lookup[childq->queue_id] =
  9970. childq;
  9971. }
  9972. }
  9973. }
  9974. /**
  9975. * lpfc_sli4_queue_setup - Set up all the SLI4 queues
  9976. * @phba: pointer to lpfc hba data structure.
  9977. *
  9978. * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
  9979. * operation.
  9980. *
  9981. * Return codes
  9982. * 0 - successful
  9983. * -ENOMEM - No available memory
  9984. * -EIO - The mailbox failed to complete successfully.
  9985. **/
  9986. int
  9987. lpfc_sli4_queue_setup(struct lpfc_hba *phba)
  9988. {
  9989. uint32_t shdr_status, shdr_add_status;
  9990. union lpfc_sli4_cfg_shdr *shdr;
  9991. struct lpfc_vector_map_info *cpup;
  9992. struct lpfc_sli4_hdw_queue *qp;
  9993. LPFC_MBOXQ_t *mboxq;
  9994. int qidx, cpu;
  9995. uint32_t length, usdelay;
  9996. int rc = -ENOMEM;
  9997. /* Check for dual-ULP support */
  9998. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  9999. if (!mboxq) {
  10000. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10001. "3249 Unable to allocate memory for "
  10002. "QUERY_FW_CFG mailbox command\n");
  10003. return -ENOMEM;
  10004. }
  10005. length = (sizeof(struct lpfc_mbx_query_fw_config) -
  10006. sizeof(struct lpfc_sli4_cfg_mhdr));
  10007. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  10008. LPFC_MBOX_OPCODE_QUERY_FW_CFG,
  10009. length, LPFC_SLI4_MBX_EMBED);
  10010. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  10011. shdr = (union lpfc_sli4_cfg_shdr *)
  10012. &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
  10013. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10014. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10015. if (shdr_status || shdr_add_status || rc) {
  10016. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10017. "3250 QUERY_FW_CFG mailbox failed with status "
  10018. "x%x add_status x%x, mbx status x%x\n",
  10019. shdr_status, shdr_add_status, rc);
  10020. mempool_free(mboxq, phba->mbox_mem_pool);
  10021. rc = -ENXIO;
  10022. goto out_error;
  10023. }
  10024. phba->sli4_hba.fw_func_mode =
  10025. mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
  10026. phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
  10027. phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
  10028. phba->sli4_hba.physical_port =
  10029. mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
  10030. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  10031. "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
  10032. "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
  10033. phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
  10034. mempool_free(mboxq, phba->mbox_mem_pool);
  10035. /*
  10036. * Set up HBA Event Queues (EQs)
  10037. */
  10038. qp = phba->sli4_hba.hdwq;
  10039. /* Set up HBA event queue */
  10040. if (!qp) {
  10041. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10042. "3147 Fast-path EQs not allocated\n");
  10043. rc = -ENOMEM;
  10044. goto out_error;
  10045. }
  10046. /* Loop thru all IRQ vectors */
  10047. for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
  10048. /* Create HBA Event Queues (EQs) in order */
  10049. for_each_present_cpu(cpu) {
  10050. cpup = &phba->sli4_hba.cpu_map[cpu];
  10051. /* Look for the CPU thats using that vector with
  10052. * LPFC_CPU_FIRST_IRQ set.
  10053. */
  10054. if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
  10055. continue;
  10056. if (qidx != cpup->eq)
  10057. continue;
  10058. /* Create an EQ for that vector */
  10059. rc = lpfc_eq_create(phba, qp[cpup->hdwq].hba_eq,
  10060. phba->cfg_fcp_imax);
  10061. if (rc) {
  10062. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10063. "0523 Failed setup of fast-path"
  10064. " EQ (%d), rc = 0x%x\n",
  10065. cpup->eq, (uint32_t)rc);
  10066. goto out_destroy;
  10067. }
  10068. /* Save the EQ for that vector in the hba_eq_hdl */
  10069. phba->sli4_hba.hba_eq_hdl[cpup->eq].eq =
  10070. qp[cpup->hdwq].hba_eq;
  10071. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  10072. "2584 HBA EQ setup: queue[%d]-id=%d\n",
  10073. cpup->eq,
  10074. qp[cpup->hdwq].hba_eq->queue_id);
  10075. }
  10076. }
  10077. /* Loop thru all Hardware Queues */
  10078. for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
  10079. cpu = lpfc_find_cpu_handle(phba, qidx, LPFC_FIND_BY_HDWQ);
  10080. cpup = &phba->sli4_hba.cpu_map[cpu];
  10081. /* Create the CQ/WQ corresponding to the Hardware Queue */
  10082. rc = lpfc_create_wq_cq(phba,
  10083. phba->sli4_hba.hdwq[cpup->hdwq].hba_eq,
  10084. qp[qidx].io_cq,
  10085. qp[qidx].io_wq,
  10086. &phba->sli4_hba.hdwq[qidx].io_cq_map,
  10087. qidx,
  10088. LPFC_IO);
  10089. if (rc) {
  10090. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10091. "0535 Failed to setup fastpath "
  10092. "IO WQ/CQ (%d), rc = 0x%x\n",
  10093. qidx, (uint32_t)rc);
  10094. goto out_destroy;
  10095. }
  10096. }
  10097. /*
  10098. * Set up Slow Path Complete Queues (CQs)
  10099. */
  10100. /* Set up slow-path MBOX CQ/MQ */
  10101. if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
  10102. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10103. "0528 %s not allocated\n",
  10104. phba->sli4_hba.mbx_cq ?
  10105. "Mailbox WQ" : "Mailbox CQ");
  10106. rc = -ENOMEM;
  10107. goto out_destroy;
  10108. }
  10109. rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
  10110. phba->sli4_hba.mbx_cq,
  10111. phba->sli4_hba.mbx_wq,
  10112. NULL, 0, LPFC_MBOX);
  10113. if (rc) {
  10114. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10115. "0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
  10116. (uint32_t)rc);
  10117. goto out_destroy;
  10118. }
  10119. if (phba->nvmet_support) {
  10120. if (!phba->sli4_hba.nvmet_cqset) {
  10121. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10122. "3165 Fast-path NVME CQ Set "
  10123. "array not allocated\n");
  10124. rc = -ENOMEM;
  10125. goto out_destroy;
  10126. }
  10127. if (phba->cfg_nvmet_mrq > 1) {
  10128. rc = lpfc_cq_create_set(phba,
  10129. phba->sli4_hba.nvmet_cqset,
  10130. qp,
  10131. LPFC_WCQ, LPFC_NVMET);
  10132. if (rc) {
  10133. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10134. "3164 Failed setup of NVME CQ "
  10135. "Set, rc = 0x%x\n",
  10136. (uint32_t)rc);
  10137. goto out_destroy;
  10138. }
  10139. } else {
  10140. /* Set up NVMET Receive Complete Queue */
  10141. rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
  10142. qp[0].hba_eq,
  10143. LPFC_WCQ, LPFC_NVMET);
  10144. if (rc) {
  10145. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10146. "6089 Failed setup NVMET CQ: "
  10147. "rc = 0x%x\n", (uint32_t)rc);
  10148. goto out_destroy;
  10149. }
  10150. phba->sli4_hba.nvmet_cqset[0]->chann = 0;
  10151. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  10152. "6090 NVMET CQ setup: cq-id=%d, "
  10153. "parent eq-id=%d\n",
  10154. phba->sli4_hba.nvmet_cqset[0]->queue_id,
  10155. qp[0].hba_eq->queue_id);
  10156. }
  10157. }
  10158. /* Set up slow-path ELS WQ/CQ */
  10159. if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
  10160. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10161. "0530 ELS %s not allocated\n",
  10162. phba->sli4_hba.els_cq ? "WQ" : "CQ");
  10163. rc = -ENOMEM;
  10164. goto out_destroy;
  10165. }
  10166. rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
  10167. phba->sli4_hba.els_cq,
  10168. phba->sli4_hba.els_wq,
  10169. NULL, 0, LPFC_ELS);
  10170. if (rc) {
  10171. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10172. "0525 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
  10173. (uint32_t)rc);
  10174. goto out_destroy;
  10175. }
  10176. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  10177. "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
  10178. phba->sli4_hba.els_wq->queue_id,
  10179. phba->sli4_hba.els_cq->queue_id);
  10180. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  10181. /* Set up NVME LS Complete Queue */
  10182. if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
  10183. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10184. "6091 LS %s not allocated\n",
  10185. phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
  10186. rc = -ENOMEM;
  10187. goto out_destroy;
  10188. }
  10189. rc = lpfc_create_wq_cq(phba, qp[0].hba_eq,
  10190. phba->sli4_hba.nvmels_cq,
  10191. phba->sli4_hba.nvmels_wq,
  10192. NULL, 0, LPFC_NVME_LS);
  10193. if (rc) {
  10194. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10195. "0526 Failed setup of NVVME LS WQ/CQ: "
  10196. "rc = 0x%x\n", (uint32_t)rc);
  10197. goto out_destroy;
  10198. }
  10199. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  10200. "6096 ELS WQ setup: wq-id=%d, "
  10201. "parent cq-id=%d\n",
  10202. phba->sli4_hba.nvmels_wq->queue_id,
  10203. phba->sli4_hba.nvmels_cq->queue_id);
  10204. }
  10205. /*
  10206. * Create NVMET Receive Queue (RQ)
  10207. */
  10208. if (phba->nvmet_support) {
  10209. if ((!phba->sli4_hba.nvmet_cqset) ||
  10210. (!phba->sli4_hba.nvmet_mrq_hdr) ||
  10211. (!phba->sli4_hba.nvmet_mrq_data)) {
  10212. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10213. "6130 MRQ CQ Queues not "
  10214. "allocated\n");
  10215. rc = -ENOMEM;
  10216. goto out_destroy;
  10217. }
  10218. if (phba->cfg_nvmet_mrq > 1) {
  10219. rc = lpfc_mrq_create(phba,
  10220. phba->sli4_hba.nvmet_mrq_hdr,
  10221. phba->sli4_hba.nvmet_mrq_data,
  10222. phba->sli4_hba.nvmet_cqset,
  10223. LPFC_NVMET);
  10224. if (rc) {
  10225. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10226. "6098 Failed setup of NVMET "
  10227. "MRQ: rc = 0x%x\n",
  10228. (uint32_t)rc);
  10229. goto out_destroy;
  10230. }
  10231. } else {
  10232. rc = lpfc_rq_create(phba,
  10233. phba->sli4_hba.nvmet_mrq_hdr[0],
  10234. phba->sli4_hba.nvmet_mrq_data[0],
  10235. phba->sli4_hba.nvmet_cqset[0],
  10236. LPFC_NVMET);
  10237. if (rc) {
  10238. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10239. "6057 Failed setup of NVMET "
  10240. "Receive Queue: rc = 0x%x\n",
  10241. (uint32_t)rc);
  10242. goto out_destroy;
  10243. }
  10244. lpfc_printf_log(
  10245. phba, KERN_INFO, LOG_INIT,
  10246. "6099 NVMET RQ setup: hdr-rq-id=%d, "
  10247. "dat-rq-id=%d parent cq-id=%d\n",
  10248. phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
  10249. phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
  10250. phba->sli4_hba.nvmet_cqset[0]->queue_id);
  10251. }
  10252. }
  10253. if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
  10254. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10255. "0540 Receive Queue not allocated\n");
  10256. rc = -ENOMEM;
  10257. goto out_destroy;
  10258. }
  10259. rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  10260. phba->sli4_hba.els_cq, LPFC_USOL);
  10261. if (rc) {
  10262. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10263. "0541 Failed setup of Receive Queue: "
  10264. "rc = 0x%x\n", (uint32_t)rc);
  10265. goto out_destroy;
  10266. }
  10267. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  10268. "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
  10269. "parent cq-id=%d\n",
  10270. phba->sli4_hba.hdr_rq->queue_id,
  10271. phba->sli4_hba.dat_rq->queue_id,
  10272. phba->sli4_hba.els_cq->queue_id);
  10273. if (phba->cfg_fcp_imax)
  10274. usdelay = LPFC_SEC_TO_USEC / phba->cfg_fcp_imax;
  10275. else
  10276. usdelay = 0;
  10277. for (qidx = 0; qidx < phba->cfg_irq_chann;
  10278. qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
  10279. lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
  10280. usdelay);
  10281. if (phba->sli4_hba.cq_max) {
  10282. kfree(phba->sli4_hba.cq_lookup);
  10283. phba->sli4_hba.cq_lookup = kcalloc((phba->sli4_hba.cq_max + 1),
  10284. sizeof(struct lpfc_queue *), GFP_KERNEL);
  10285. if (!phba->sli4_hba.cq_lookup) {
  10286. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10287. "0549 Failed setup of CQ Lookup table: "
  10288. "size 0x%x\n", phba->sli4_hba.cq_max);
  10289. rc = -ENOMEM;
  10290. goto out_destroy;
  10291. }
  10292. lpfc_setup_cq_lookup(phba);
  10293. }
  10294. return 0;
  10295. out_destroy:
  10296. lpfc_sli4_queue_unset(phba);
  10297. out_error:
  10298. return rc;
  10299. }
  10300. /**
  10301. * lpfc_sli4_queue_unset - Unset all the SLI4 queues
  10302. * @phba: pointer to lpfc hba data structure.
  10303. *
  10304. * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
  10305. * operation.
  10306. *
  10307. * Return codes
  10308. * 0 - successful
  10309. * -ENOMEM - No available memory
  10310. * -EIO - The mailbox failed to complete successfully.
  10311. **/
  10312. void
  10313. lpfc_sli4_queue_unset(struct lpfc_hba *phba)
  10314. {
  10315. struct lpfc_sli4_hdw_queue *qp;
  10316. struct lpfc_queue *eq;
  10317. int qidx;
  10318. /* Unset mailbox command work queue */
  10319. if (phba->sli4_hba.mbx_wq)
  10320. lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
  10321. /* Unset NVME LS work queue */
  10322. if (phba->sli4_hba.nvmels_wq)
  10323. lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
  10324. /* Unset ELS work queue */
  10325. if (phba->sli4_hba.els_wq)
  10326. lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
  10327. /* Unset unsolicited receive queue */
  10328. if (phba->sli4_hba.hdr_rq)
  10329. lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
  10330. phba->sli4_hba.dat_rq);
  10331. /* Unset mailbox command complete queue */
  10332. if (phba->sli4_hba.mbx_cq)
  10333. lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
  10334. /* Unset ELS complete queue */
  10335. if (phba->sli4_hba.els_cq)
  10336. lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
  10337. /* Unset NVME LS complete queue */
  10338. if (phba->sli4_hba.nvmels_cq)
  10339. lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
  10340. if (phba->nvmet_support) {
  10341. /* Unset NVMET MRQ queue */
  10342. if (phba->sli4_hba.nvmet_mrq_hdr) {
  10343. for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
  10344. lpfc_rq_destroy(
  10345. phba,
  10346. phba->sli4_hba.nvmet_mrq_hdr[qidx],
  10347. phba->sli4_hba.nvmet_mrq_data[qidx]);
  10348. }
  10349. /* Unset NVMET CQ Set complete queue */
  10350. if (phba->sli4_hba.nvmet_cqset) {
  10351. for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
  10352. lpfc_cq_destroy(
  10353. phba, phba->sli4_hba.nvmet_cqset[qidx]);
  10354. }
  10355. }
  10356. /* Unset fast-path SLI4 queues */
  10357. if (phba->sli4_hba.hdwq) {
  10358. /* Loop thru all Hardware Queues */
  10359. for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
  10360. /* Destroy the CQ/WQ corresponding to Hardware Queue */
  10361. qp = &phba->sli4_hba.hdwq[qidx];
  10362. lpfc_wq_destroy(phba, qp->io_wq);
  10363. lpfc_cq_destroy(phba, qp->io_cq);
  10364. }
  10365. /* Loop thru all IRQ vectors */
  10366. for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
  10367. /* Destroy the EQ corresponding to the IRQ vector */
  10368. eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
  10369. lpfc_eq_destroy(phba, eq);
  10370. }
  10371. }
  10372. kfree(phba->sli4_hba.cq_lookup);
  10373. phba->sli4_hba.cq_lookup = NULL;
  10374. phba->sli4_hba.cq_max = 0;
  10375. }
  10376. /**
  10377. * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
  10378. * @phba: pointer to lpfc hba data structure.
  10379. *
  10380. * This routine is invoked to allocate and set up a pool of completion queue
  10381. * events. The body of the completion queue event is a completion queue entry
  10382. * CQE. For now, this pool is used for the interrupt service routine to queue
  10383. * the following HBA completion queue events for the worker thread to process:
  10384. * - Mailbox asynchronous events
  10385. * - Receive queue completion unsolicited events
  10386. * Later, this can be used for all the slow-path events.
  10387. *
  10388. * Return codes
  10389. * 0 - successful
  10390. * -ENOMEM - No available memory
  10391. **/
  10392. static int
  10393. lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
  10394. {
  10395. struct lpfc_cq_event *cq_event;
  10396. int i;
  10397. for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
  10398. cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
  10399. if (!cq_event)
  10400. goto out_pool_create_fail;
  10401. list_add_tail(&cq_event->list,
  10402. &phba->sli4_hba.sp_cqe_event_pool);
  10403. }
  10404. return 0;
  10405. out_pool_create_fail:
  10406. lpfc_sli4_cq_event_pool_destroy(phba);
  10407. return -ENOMEM;
  10408. }
  10409. /**
  10410. * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
  10411. * @phba: pointer to lpfc hba data structure.
  10412. *
  10413. * This routine is invoked to free the pool of completion queue events at
  10414. * driver unload time. Note that, it is the responsibility of the driver
  10415. * cleanup routine to free all the outstanding completion-queue events
  10416. * allocated from this pool back into the pool before invoking this routine
  10417. * to destroy the pool.
  10418. **/
  10419. static void
  10420. lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
  10421. {
  10422. struct lpfc_cq_event *cq_event, *next_cq_event;
  10423. list_for_each_entry_safe(cq_event, next_cq_event,
  10424. &phba->sli4_hba.sp_cqe_event_pool, list) {
  10425. list_del(&cq_event->list);
  10426. kfree(cq_event);
  10427. }
  10428. }
  10429. /**
  10430. * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
  10431. * @phba: pointer to lpfc hba data structure.
  10432. *
  10433. * This routine is the lock free version of the API invoked to allocate a
  10434. * completion-queue event from the free pool.
  10435. *
  10436. * Return: Pointer to the newly allocated completion-queue event if successful
  10437. * NULL otherwise.
  10438. **/
  10439. struct lpfc_cq_event *
  10440. __lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
  10441. {
  10442. struct lpfc_cq_event *cq_event = NULL;
  10443. list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
  10444. struct lpfc_cq_event, list);
  10445. return cq_event;
  10446. }
  10447. /**
  10448. * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
  10449. * @phba: pointer to lpfc hba data structure.
  10450. *
  10451. * This routine is the lock version of the API invoked to allocate a
  10452. * completion-queue event from the free pool.
  10453. *
  10454. * Return: Pointer to the newly allocated completion-queue event if successful
  10455. * NULL otherwise.
  10456. **/
  10457. struct lpfc_cq_event *
  10458. lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
  10459. {
  10460. struct lpfc_cq_event *cq_event;
  10461. unsigned long iflags;
  10462. spin_lock_irqsave(&phba->hbalock, iflags);
  10463. cq_event = __lpfc_sli4_cq_event_alloc(phba);
  10464. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10465. return cq_event;
  10466. }
  10467. /**
  10468. * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
  10469. * @phba: pointer to lpfc hba data structure.
  10470. * @cq_event: pointer to the completion queue event to be freed.
  10471. *
  10472. * This routine is the lock free version of the API invoked to release a
  10473. * completion-queue event back into the free pool.
  10474. **/
  10475. void
  10476. __lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
  10477. struct lpfc_cq_event *cq_event)
  10478. {
  10479. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
  10480. }
  10481. /**
  10482. * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
  10483. * @phba: pointer to lpfc hba data structure.
  10484. * @cq_event: pointer to the completion queue event to be freed.
  10485. *
  10486. * This routine is the lock version of the API invoked to release a
  10487. * completion-queue event back into the free pool.
  10488. **/
  10489. void
  10490. lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
  10491. struct lpfc_cq_event *cq_event)
  10492. {
  10493. unsigned long iflags;
  10494. spin_lock_irqsave(&phba->hbalock, iflags);
  10495. __lpfc_sli4_cq_event_release(phba, cq_event);
  10496. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10497. }
  10498. /**
  10499. * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
  10500. * @phba: pointer to lpfc hba data structure.
  10501. *
  10502. * This routine is to free all the pending completion-queue events to the
  10503. * back into the free pool for device reset.
  10504. **/
  10505. static void
  10506. lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
  10507. {
  10508. LIST_HEAD(cq_event_list);
  10509. struct lpfc_cq_event *cq_event;
  10510. unsigned long iflags;
  10511. /* Retrieve all the pending WCQEs from pending WCQE lists */
  10512. /* Pending ELS XRI abort events */
  10513. spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
  10514. list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  10515. &cq_event_list);
  10516. spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
  10517. /* Pending asynnc events */
  10518. spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
  10519. list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
  10520. &cq_event_list);
  10521. spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
  10522. while (!list_empty(&cq_event_list)) {
  10523. list_remove_head(&cq_event_list, cq_event,
  10524. struct lpfc_cq_event, list);
  10525. lpfc_sli4_cq_event_release(phba, cq_event);
  10526. }
  10527. }
  10528. /**
  10529. * lpfc_pci_function_reset - Reset pci function.
  10530. * @phba: pointer to lpfc hba data structure.
  10531. *
  10532. * This routine is invoked to request a PCI function reset. It will destroys
  10533. * all resources assigned to the PCI function which originates this request.
  10534. *
  10535. * Return codes
  10536. * 0 - successful
  10537. * -ENOMEM - No available memory
  10538. * -EIO - The mailbox failed to complete successfully.
  10539. **/
  10540. int
  10541. lpfc_pci_function_reset(struct lpfc_hba *phba)
  10542. {
  10543. LPFC_MBOXQ_t *mboxq;
  10544. uint32_t rc = 0, if_type;
  10545. uint32_t shdr_status, shdr_add_status;
  10546. uint32_t rdy_chk;
  10547. uint32_t port_reset = 0;
  10548. union lpfc_sli4_cfg_shdr *shdr;
  10549. struct lpfc_register reg_data;
  10550. uint16_t devid;
  10551. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  10552. switch (if_type) {
  10553. case LPFC_SLI_INTF_IF_TYPE_0:
  10554. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
  10555. GFP_KERNEL);
  10556. if (!mboxq) {
  10557. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10558. "0494 Unable to allocate memory for "
  10559. "issuing SLI_FUNCTION_RESET mailbox "
  10560. "command\n");
  10561. return -ENOMEM;
  10562. }
  10563. /* Setup PCI function reset mailbox-ioctl command */
  10564. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  10565. LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
  10566. LPFC_SLI4_MBX_EMBED);
  10567. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  10568. shdr = (union lpfc_sli4_cfg_shdr *)
  10569. &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
  10570. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10571. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  10572. &shdr->response);
  10573. mempool_free(mboxq, phba->mbox_mem_pool);
  10574. if (shdr_status || shdr_add_status || rc) {
  10575. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10576. "0495 SLI_FUNCTION_RESET mailbox "
  10577. "failed with status x%x add_status x%x,"
  10578. " mbx status x%x\n",
  10579. shdr_status, shdr_add_status, rc);
  10580. rc = -ENXIO;
  10581. }
  10582. break;
  10583. case LPFC_SLI_INTF_IF_TYPE_2:
  10584. case LPFC_SLI_INTF_IF_TYPE_6:
  10585. wait:
  10586. /*
  10587. * Poll the Port Status Register and wait for RDY for
  10588. * up to 30 seconds. If the port doesn't respond, treat
  10589. * it as an error.
  10590. */
  10591. for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
  10592. if (lpfc_readl(phba->sli4_hba.u.if_type2.
  10593. STATUSregaddr, &reg_data.word0)) {
  10594. rc = -ENODEV;
  10595. goto out;
  10596. }
  10597. if (bf_get(lpfc_sliport_status_rdy, &reg_data))
  10598. break;
  10599. msleep(20);
  10600. }
  10601. if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
  10602. phba->work_status[0] = readl(
  10603. phba->sli4_hba.u.if_type2.ERR1regaddr);
  10604. phba->work_status[1] = readl(
  10605. phba->sli4_hba.u.if_type2.ERR2regaddr);
  10606. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10607. "2890 Port not ready, port status reg "
  10608. "0x%x error 1=0x%x, error 2=0x%x\n",
  10609. reg_data.word0,
  10610. phba->work_status[0],
  10611. phba->work_status[1]);
  10612. rc = -ENODEV;
  10613. goto out;
  10614. }
  10615. if (bf_get(lpfc_sliport_status_pldv, &reg_data))
  10616. lpfc_pldv_detect = true;
  10617. if (!port_reset) {
  10618. /*
  10619. * Reset the port now
  10620. */
  10621. reg_data.word0 = 0;
  10622. bf_set(lpfc_sliport_ctrl_end, &reg_data,
  10623. LPFC_SLIPORT_LITTLE_ENDIAN);
  10624. bf_set(lpfc_sliport_ctrl_ip, &reg_data,
  10625. LPFC_SLIPORT_INIT_PORT);
  10626. writel(reg_data.word0, phba->sli4_hba.u.if_type2.
  10627. CTRLregaddr);
  10628. /* flush */
  10629. pci_read_config_word(phba->pcidev,
  10630. PCI_DEVICE_ID, &devid);
  10631. port_reset = 1;
  10632. msleep(20);
  10633. goto wait;
  10634. } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
  10635. rc = -ENODEV;
  10636. goto out;
  10637. }
  10638. break;
  10639. case LPFC_SLI_INTF_IF_TYPE_1:
  10640. default:
  10641. break;
  10642. }
  10643. out:
  10644. /* Catch the not-ready port failure after a port reset. */
  10645. if (rc) {
  10646. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10647. "3317 HBA not functional: IP Reset Failed "
  10648. "try: echo fw_reset > board_mode\n");
  10649. rc = -ENODEV;
  10650. }
  10651. return rc;
  10652. }
  10653. /**
  10654. * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
  10655. * @phba: pointer to lpfc hba data structure.
  10656. *
  10657. * This routine is invoked to set up the PCI device memory space for device
  10658. * with SLI-4 interface spec.
  10659. *
  10660. * Return codes
  10661. * 0 - successful
  10662. * other values - error
  10663. **/
  10664. static int
  10665. lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
  10666. {
  10667. struct pci_dev *pdev = phba->pcidev;
  10668. unsigned long bar0map_len, bar1map_len, bar2map_len;
  10669. int error;
  10670. uint32_t if_type;
  10671. if (!pdev)
  10672. return -ENODEV;
  10673. /* Set the device DMA mask size */
  10674. error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  10675. if (error)
  10676. error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  10677. if (error)
  10678. return error;
  10679. /*
  10680. * The BARs and register set definitions and offset locations are
  10681. * dependent on the if_type.
  10682. */
  10683. if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
  10684. &phba->sli4_hba.sli_intf.word0)) {
  10685. return -ENODEV;
  10686. }
  10687. /* There is no SLI3 failback for SLI4 devices. */
  10688. if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
  10689. LPFC_SLI_INTF_VALID) {
  10690. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10691. "2894 SLI_INTF reg contents invalid "
  10692. "sli_intf reg 0x%x\n",
  10693. phba->sli4_hba.sli_intf.word0);
  10694. return -ENODEV;
  10695. }
  10696. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  10697. /*
  10698. * Get the bus address of SLI4 device Bar regions and the
  10699. * number of bytes required by each mapping. The mapping of the
  10700. * particular PCI BARs regions is dependent on the type of
  10701. * SLI4 device.
  10702. */
  10703. if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
  10704. phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
  10705. bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
  10706. /*
  10707. * Map SLI4 PCI Config Space Register base to a kernel virtual
  10708. * addr
  10709. */
  10710. phba->sli4_hba.conf_regs_memmap_p =
  10711. ioremap(phba->pci_bar0_map, bar0map_len);
  10712. if (!phba->sli4_hba.conf_regs_memmap_p) {
  10713. dev_printk(KERN_ERR, &pdev->dev,
  10714. "ioremap failed for SLI4 PCI config "
  10715. "registers.\n");
  10716. return -ENODEV;
  10717. }
  10718. phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
  10719. /* Set up BAR0 PCI config space register memory map */
  10720. lpfc_sli4_bar0_register_memmap(phba, if_type);
  10721. } else {
  10722. phba->pci_bar0_map = pci_resource_start(pdev, 1);
  10723. bar0map_len = pci_resource_len(pdev, 1);
  10724. if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
  10725. dev_printk(KERN_ERR, &pdev->dev,
  10726. "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
  10727. return -ENODEV;
  10728. }
  10729. phba->sli4_hba.conf_regs_memmap_p =
  10730. ioremap(phba->pci_bar0_map, bar0map_len);
  10731. if (!phba->sli4_hba.conf_regs_memmap_p) {
  10732. dev_printk(KERN_ERR, &pdev->dev,
  10733. "ioremap failed for SLI4 PCI config "
  10734. "registers.\n");
  10735. return -ENODEV;
  10736. }
  10737. lpfc_sli4_bar0_register_memmap(phba, if_type);
  10738. }
  10739. if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
  10740. if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
  10741. /*
  10742. * Map SLI4 if type 0 HBA Control Register base to a
  10743. * kernel virtual address and setup the registers.
  10744. */
  10745. phba->pci_bar1_map = pci_resource_start(pdev,
  10746. PCI_64BIT_BAR2);
  10747. bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
  10748. phba->sli4_hba.ctrl_regs_memmap_p =
  10749. ioremap(phba->pci_bar1_map,
  10750. bar1map_len);
  10751. if (!phba->sli4_hba.ctrl_regs_memmap_p) {
  10752. dev_err(&pdev->dev,
  10753. "ioremap failed for SLI4 HBA "
  10754. "control registers.\n");
  10755. error = -ENOMEM;
  10756. goto out_iounmap_conf;
  10757. }
  10758. phba->pci_bar2_memmap_p =
  10759. phba->sli4_hba.ctrl_regs_memmap_p;
  10760. lpfc_sli4_bar1_register_memmap(phba, if_type);
  10761. } else {
  10762. error = -ENOMEM;
  10763. goto out_iounmap_conf;
  10764. }
  10765. }
  10766. if ((if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
  10767. (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
  10768. /*
  10769. * Map SLI4 if type 6 HBA Doorbell Register base to a kernel
  10770. * virtual address and setup the registers.
  10771. */
  10772. phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
  10773. bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
  10774. phba->sli4_hba.drbl_regs_memmap_p =
  10775. ioremap(phba->pci_bar1_map, bar1map_len);
  10776. if (!phba->sli4_hba.drbl_regs_memmap_p) {
  10777. dev_err(&pdev->dev,
  10778. "ioremap failed for SLI4 HBA doorbell registers.\n");
  10779. error = -ENOMEM;
  10780. goto out_iounmap_conf;
  10781. }
  10782. phba->pci_bar2_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
  10783. lpfc_sli4_bar1_register_memmap(phba, if_type);
  10784. }
  10785. if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
  10786. if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
  10787. /*
  10788. * Map SLI4 if type 0 HBA Doorbell Register base to
  10789. * a kernel virtual address and setup the registers.
  10790. */
  10791. phba->pci_bar2_map = pci_resource_start(pdev,
  10792. PCI_64BIT_BAR4);
  10793. bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
  10794. phba->sli4_hba.drbl_regs_memmap_p =
  10795. ioremap(phba->pci_bar2_map,
  10796. bar2map_len);
  10797. if (!phba->sli4_hba.drbl_regs_memmap_p) {
  10798. dev_err(&pdev->dev,
  10799. "ioremap failed for SLI4 HBA"
  10800. " doorbell registers.\n");
  10801. error = -ENOMEM;
  10802. goto out_iounmap_ctrl;
  10803. }
  10804. phba->pci_bar4_memmap_p =
  10805. phba->sli4_hba.drbl_regs_memmap_p;
  10806. error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
  10807. if (error)
  10808. goto out_iounmap_all;
  10809. } else {
  10810. error = -ENOMEM;
  10811. goto out_iounmap_ctrl;
  10812. }
  10813. }
  10814. if (if_type == LPFC_SLI_INTF_IF_TYPE_6 &&
  10815. pci_resource_start(pdev, PCI_64BIT_BAR4)) {
  10816. /*
  10817. * Map SLI4 if type 6 HBA DPP Register base to a kernel
  10818. * virtual address and setup the registers.
  10819. */
  10820. phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
  10821. bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
  10822. phba->sli4_hba.dpp_regs_memmap_p =
  10823. ioremap(phba->pci_bar2_map, bar2map_len);
  10824. if (!phba->sli4_hba.dpp_regs_memmap_p) {
  10825. dev_err(&pdev->dev,
  10826. "ioremap failed for SLI4 HBA dpp registers.\n");
  10827. error = -ENOMEM;
  10828. goto out_iounmap_all;
  10829. }
  10830. phba->pci_bar4_memmap_p = phba->sli4_hba.dpp_regs_memmap_p;
  10831. }
  10832. /* Set up the EQ/CQ register handeling functions now */
  10833. switch (if_type) {
  10834. case LPFC_SLI_INTF_IF_TYPE_0:
  10835. case LPFC_SLI_INTF_IF_TYPE_2:
  10836. phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_eq_clr_intr;
  10837. phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_write_eq_db;
  10838. phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_write_cq_db;
  10839. break;
  10840. case LPFC_SLI_INTF_IF_TYPE_6:
  10841. phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_if6_eq_clr_intr;
  10842. phba->sli4_hba.sli4_write_eq_db = lpfc_sli4_if6_write_eq_db;
  10843. phba->sli4_hba.sli4_write_cq_db = lpfc_sli4_if6_write_cq_db;
  10844. break;
  10845. default:
  10846. break;
  10847. }
  10848. return 0;
  10849. out_iounmap_all:
  10850. if (phba->sli4_hba.drbl_regs_memmap_p)
  10851. iounmap(phba->sli4_hba.drbl_regs_memmap_p);
  10852. out_iounmap_ctrl:
  10853. if (phba->sli4_hba.ctrl_regs_memmap_p)
  10854. iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
  10855. out_iounmap_conf:
  10856. iounmap(phba->sli4_hba.conf_regs_memmap_p);
  10857. return error;
  10858. }
  10859. /**
  10860. * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
  10861. * @phba: pointer to lpfc hba data structure.
  10862. *
  10863. * This routine is invoked to unset the PCI device memory space for device
  10864. * with SLI-4 interface spec.
  10865. **/
  10866. static void
  10867. lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
  10868. {
  10869. uint32_t if_type;
  10870. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  10871. switch (if_type) {
  10872. case LPFC_SLI_INTF_IF_TYPE_0:
  10873. iounmap(phba->sli4_hba.drbl_regs_memmap_p);
  10874. iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
  10875. iounmap(phba->sli4_hba.conf_regs_memmap_p);
  10876. break;
  10877. case LPFC_SLI_INTF_IF_TYPE_2:
  10878. iounmap(phba->sli4_hba.conf_regs_memmap_p);
  10879. break;
  10880. case LPFC_SLI_INTF_IF_TYPE_6:
  10881. iounmap(phba->sli4_hba.drbl_regs_memmap_p);
  10882. iounmap(phba->sli4_hba.conf_regs_memmap_p);
  10883. if (phba->sli4_hba.dpp_regs_memmap_p)
  10884. iounmap(phba->sli4_hba.dpp_regs_memmap_p);
  10885. break;
  10886. case LPFC_SLI_INTF_IF_TYPE_1:
  10887. break;
  10888. default:
  10889. dev_printk(KERN_ERR, &phba->pcidev->dev,
  10890. "FATAL - unsupported SLI4 interface type - %d\n",
  10891. if_type);
  10892. break;
  10893. }
  10894. }
  10895. /**
  10896. * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
  10897. * @phba: pointer to lpfc hba data structure.
  10898. *
  10899. * This routine is invoked to enable the MSI-X interrupt vectors to device
  10900. * with SLI-3 interface specs.
  10901. *
  10902. * Return codes
  10903. * 0 - successful
  10904. * other values - error
  10905. **/
  10906. static int
  10907. lpfc_sli_enable_msix(struct lpfc_hba *phba)
  10908. {
  10909. int rc;
  10910. LPFC_MBOXQ_t *pmb;
  10911. /* Set up MSI-X multi-message vectors */
  10912. rc = pci_alloc_irq_vectors(phba->pcidev,
  10913. LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
  10914. if (rc < 0) {
  10915. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  10916. "0420 PCI enable MSI-X failed (%d)\n", rc);
  10917. goto vec_fail_out;
  10918. }
  10919. /*
  10920. * Assign MSI-X vectors to interrupt handlers
  10921. */
  10922. /* vector-0 is associated to slow-path handler */
  10923. rc = request_irq(pci_irq_vector(phba->pcidev, 0),
  10924. &lpfc_sli_sp_intr_handler, 0,
  10925. LPFC_SP_DRIVER_HANDLER_NAME, phba);
  10926. if (rc) {
  10927. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  10928. "0421 MSI-X slow-path request_irq failed "
  10929. "(%d)\n", rc);
  10930. goto msi_fail_out;
  10931. }
  10932. /* vector-1 is associated to fast-path handler */
  10933. rc = request_irq(pci_irq_vector(phba->pcidev, 1),
  10934. &lpfc_sli_fp_intr_handler, 0,
  10935. LPFC_FP_DRIVER_HANDLER_NAME, phba);
  10936. if (rc) {
  10937. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  10938. "0429 MSI-X fast-path request_irq failed "
  10939. "(%d)\n", rc);
  10940. goto irq_fail_out;
  10941. }
  10942. /*
  10943. * Configure HBA MSI-X attention conditions to messages
  10944. */
  10945. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10946. if (!pmb) {
  10947. rc = -ENOMEM;
  10948. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  10949. "0474 Unable to allocate memory for issuing "
  10950. "MBOX_CONFIG_MSI command\n");
  10951. goto mem_fail_out;
  10952. }
  10953. rc = lpfc_config_msi(phba, pmb);
  10954. if (rc)
  10955. goto mbx_fail_out;
  10956. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  10957. if (rc != MBX_SUCCESS) {
  10958. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
  10959. "0351 Config MSI mailbox command failed, "
  10960. "mbxCmd x%x, mbxStatus x%x\n",
  10961. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
  10962. goto mbx_fail_out;
  10963. }
  10964. /* Free memory allocated for mailbox command */
  10965. mempool_free(pmb, phba->mbox_mem_pool);
  10966. return rc;
  10967. mbx_fail_out:
  10968. /* Free memory allocated for mailbox command */
  10969. mempool_free(pmb, phba->mbox_mem_pool);
  10970. mem_fail_out:
  10971. /* free the irq already requested */
  10972. free_irq(pci_irq_vector(phba->pcidev, 1), phba);
  10973. irq_fail_out:
  10974. /* free the irq already requested */
  10975. free_irq(pci_irq_vector(phba->pcidev, 0), phba);
  10976. msi_fail_out:
  10977. /* Unconfigure MSI-X capability structure */
  10978. pci_free_irq_vectors(phba->pcidev);
  10979. vec_fail_out:
  10980. return rc;
  10981. }
  10982. /**
  10983. * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
  10984. * @phba: pointer to lpfc hba data structure.
  10985. *
  10986. * This routine is invoked to enable the MSI interrupt mode to device with
  10987. * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
  10988. * enable the MSI vector. The device driver is responsible for calling the
  10989. * request_irq() to register MSI vector with a interrupt the handler, which
  10990. * is done in this function.
  10991. *
  10992. * Return codes
  10993. * 0 - successful
  10994. * other values - error
  10995. */
  10996. static int
  10997. lpfc_sli_enable_msi(struct lpfc_hba *phba)
  10998. {
  10999. int rc;
  11000. rc = pci_enable_msi(phba->pcidev);
  11001. if (!rc)
  11002. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11003. "0012 PCI enable MSI mode success.\n");
  11004. else {
  11005. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11006. "0471 PCI enable MSI mode failed (%d)\n", rc);
  11007. return rc;
  11008. }
  11009. rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
  11010. 0, LPFC_DRIVER_NAME, phba);
  11011. if (rc) {
  11012. pci_disable_msi(phba->pcidev);
  11013. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  11014. "0478 MSI request_irq failed (%d)\n", rc);
  11015. }
  11016. return rc;
  11017. }
  11018. /**
  11019. * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
  11020. * @phba: pointer to lpfc hba data structure.
  11021. * @cfg_mode: Interrupt configuration mode (INTx, MSI or MSI-X).
  11022. *
  11023. * This routine is invoked to enable device interrupt and associate driver's
  11024. * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
  11025. * spec. Depends on the interrupt mode configured to the driver, the driver
  11026. * will try to fallback from the configured interrupt mode to an interrupt
  11027. * mode which is supported by the platform, kernel, and device in the order
  11028. * of:
  11029. * MSI-X -> MSI -> IRQ.
  11030. *
  11031. * Return codes
  11032. * 0 - successful
  11033. * other values - error
  11034. **/
  11035. static uint32_t
  11036. lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
  11037. {
  11038. uint32_t intr_mode = LPFC_INTR_ERROR;
  11039. int retval;
  11040. /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
  11041. retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
  11042. if (retval)
  11043. return intr_mode;
  11044. clear_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
  11045. if (cfg_mode == 2) {
  11046. /* Now, try to enable MSI-X interrupt mode */
  11047. retval = lpfc_sli_enable_msix(phba);
  11048. if (!retval) {
  11049. /* Indicate initialization to MSI-X mode */
  11050. phba->intr_type = MSIX;
  11051. intr_mode = 2;
  11052. }
  11053. }
  11054. /* Fallback to MSI if MSI-X initialization failed */
  11055. if (cfg_mode >= 1 && phba->intr_type == NONE) {
  11056. retval = lpfc_sli_enable_msi(phba);
  11057. if (!retval) {
  11058. /* Indicate initialization to MSI mode */
  11059. phba->intr_type = MSI;
  11060. intr_mode = 1;
  11061. }
  11062. }
  11063. /* Fallback to INTx if both MSI-X/MSI initalization failed */
  11064. if (phba->intr_type == NONE) {
  11065. retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
  11066. IRQF_SHARED, LPFC_DRIVER_NAME, phba);
  11067. if (!retval) {
  11068. /* Indicate initialization to INTx mode */
  11069. phba->intr_type = INTx;
  11070. intr_mode = 0;
  11071. }
  11072. }
  11073. return intr_mode;
  11074. }
  11075. /**
  11076. * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
  11077. * @phba: pointer to lpfc hba data structure.
  11078. *
  11079. * This routine is invoked to disable device interrupt and disassociate the
  11080. * driver's interrupt handler(s) from interrupt vector(s) to device with
  11081. * SLI-3 interface spec. Depending on the interrupt mode, the driver will
  11082. * release the interrupt vector(s) for the message signaled interrupt.
  11083. **/
  11084. static void
  11085. lpfc_sli_disable_intr(struct lpfc_hba *phba)
  11086. {
  11087. int nr_irqs, i;
  11088. if (phba->intr_type == MSIX)
  11089. nr_irqs = LPFC_MSIX_VECTORS;
  11090. else
  11091. nr_irqs = 1;
  11092. for (i = 0; i < nr_irqs; i++)
  11093. free_irq(pci_irq_vector(phba->pcidev, i), phba);
  11094. pci_free_irq_vectors(phba->pcidev);
  11095. /* Reset interrupt management states */
  11096. phba->intr_type = NONE;
  11097. phba->sli.slistat.sli_intr = 0;
  11098. }
  11099. /**
  11100. * lpfc_find_cpu_handle - Find the CPU that corresponds to the specified Queue
  11101. * @phba: pointer to lpfc hba data structure.
  11102. * @id: EQ vector index or Hardware Queue index
  11103. * @match: LPFC_FIND_BY_EQ = match by EQ
  11104. * LPFC_FIND_BY_HDWQ = match by Hardware Queue
  11105. * Return the CPU that matches the selection criteria
  11106. */
  11107. static uint16_t
  11108. lpfc_find_cpu_handle(struct lpfc_hba *phba, uint16_t id, int match)
  11109. {
  11110. struct lpfc_vector_map_info *cpup;
  11111. int cpu;
  11112. /* Loop through all CPUs */
  11113. for_each_present_cpu(cpu) {
  11114. cpup = &phba->sli4_hba.cpu_map[cpu];
  11115. /* If we are matching by EQ, there may be multiple CPUs using
  11116. * using the same vector, so select the one with
  11117. * LPFC_CPU_FIRST_IRQ set.
  11118. */
  11119. if ((match == LPFC_FIND_BY_EQ) &&
  11120. (cpup->flag & LPFC_CPU_FIRST_IRQ) &&
  11121. (cpup->eq == id))
  11122. return cpu;
  11123. /* If matching by HDWQ, select the first CPU that matches */
  11124. if ((match == LPFC_FIND_BY_HDWQ) && (cpup->hdwq == id))
  11125. return cpu;
  11126. }
  11127. return 0;
  11128. }
  11129. #ifdef CONFIG_X86
  11130. /**
  11131. * lpfc_find_hyper - Determine if the CPU map entry is hyper-threaded
  11132. * @phba: pointer to lpfc hba data structure.
  11133. * @cpu: CPU map index
  11134. * @phys_id: CPU package physical id
  11135. * @core_id: CPU core id
  11136. */
  11137. static int
  11138. lpfc_find_hyper(struct lpfc_hba *phba, int cpu,
  11139. uint16_t phys_id, uint16_t core_id)
  11140. {
  11141. struct lpfc_vector_map_info *cpup;
  11142. int idx;
  11143. for_each_present_cpu(idx) {
  11144. cpup = &phba->sli4_hba.cpu_map[idx];
  11145. /* Does the cpup match the one we are looking for */
  11146. if ((cpup->phys_id == phys_id) &&
  11147. (cpup->core_id == core_id) &&
  11148. (cpu != idx))
  11149. return 1;
  11150. }
  11151. return 0;
  11152. }
  11153. #endif
  11154. /*
  11155. * lpfc_assign_eq_map_info - Assigns eq for vector_map structure
  11156. * @phba: pointer to lpfc hba data structure.
  11157. * @eqidx: index for eq and irq vector
  11158. * @flag: flags to set for vector_map structure
  11159. * @cpu: cpu used to index vector_map structure
  11160. *
  11161. * The routine assigns eq info into vector_map structure
  11162. */
  11163. static inline void
  11164. lpfc_assign_eq_map_info(struct lpfc_hba *phba, uint16_t eqidx, uint16_t flag,
  11165. unsigned int cpu)
  11166. {
  11167. struct lpfc_vector_map_info *cpup = &phba->sli4_hba.cpu_map[cpu];
  11168. struct lpfc_hba_eq_hdl *eqhdl = lpfc_get_eq_hdl(eqidx);
  11169. cpup->eq = eqidx;
  11170. cpup->flag |= flag;
  11171. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11172. "3336 Set Affinity: CPU %d irq %d eq %d flag x%x\n",
  11173. cpu, eqhdl->irq, cpup->eq, cpup->flag);
  11174. }
  11175. /**
  11176. * lpfc_cpu_map_array_init - Initialize cpu_map structure
  11177. * @phba: pointer to lpfc hba data structure.
  11178. *
  11179. * The routine initializes the cpu_map array structure
  11180. */
  11181. static void
  11182. lpfc_cpu_map_array_init(struct lpfc_hba *phba)
  11183. {
  11184. struct lpfc_vector_map_info *cpup;
  11185. struct lpfc_eq_intr_info *eqi;
  11186. int cpu;
  11187. for_each_possible_cpu(cpu) {
  11188. cpup = &phba->sli4_hba.cpu_map[cpu];
  11189. cpup->phys_id = LPFC_VECTOR_MAP_EMPTY;
  11190. cpup->core_id = LPFC_VECTOR_MAP_EMPTY;
  11191. cpup->hdwq = LPFC_VECTOR_MAP_EMPTY;
  11192. cpup->eq = LPFC_VECTOR_MAP_EMPTY;
  11193. cpup->flag = 0;
  11194. eqi = per_cpu_ptr(phba->sli4_hba.eq_info, cpu);
  11195. INIT_LIST_HEAD(&eqi->list);
  11196. eqi->icnt = 0;
  11197. }
  11198. }
  11199. /**
  11200. * lpfc_hba_eq_hdl_array_init - Initialize hba_eq_hdl structure
  11201. * @phba: pointer to lpfc hba data structure.
  11202. *
  11203. * The routine initializes the hba_eq_hdl array structure
  11204. */
  11205. static void
  11206. lpfc_hba_eq_hdl_array_init(struct lpfc_hba *phba)
  11207. {
  11208. struct lpfc_hba_eq_hdl *eqhdl;
  11209. int i;
  11210. for (i = 0; i < phba->cfg_irq_chann; i++) {
  11211. eqhdl = lpfc_get_eq_hdl(i);
  11212. eqhdl->irq = LPFC_IRQ_EMPTY;
  11213. eqhdl->phba = phba;
  11214. }
  11215. }
  11216. /**
  11217. * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
  11218. * @phba: pointer to lpfc hba data structure.
  11219. * @vectors: number of msix vectors allocated.
  11220. *
  11221. * The routine will figure out the CPU affinity assignment for every
  11222. * MSI-X vector allocated for the HBA.
  11223. * In addition, the CPU to IO channel mapping will be calculated
  11224. * and the phba->sli4_hba.cpu_map array will reflect this.
  11225. */
  11226. static void
  11227. lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
  11228. {
  11229. int i, cpu, idx, next_idx, new_cpu, start_cpu, first_cpu;
  11230. int max_phys_id, min_phys_id;
  11231. int max_core_id, min_core_id;
  11232. struct lpfc_vector_map_info *cpup;
  11233. struct lpfc_vector_map_info *new_cpup;
  11234. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  11235. struct lpfc_hdwq_stat *c_stat;
  11236. #endif
  11237. max_phys_id = 0;
  11238. min_phys_id = LPFC_VECTOR_MAP_EMPTY;
  11239. max_core_id = 0;
  11240. min_core_id = LPFC_VECTOR_MAP_EMPTY;
  11241. /* Update CPU map with physical id and core id of each CPU */
  11242. for_each_present_cpu(cpu) {
  11243. cpup = &phba->sli4_hba.cpu_map[cpu];
  11244. #ifdef CONFIG_X86
  11245. cpup->phys_id = topology_physical_package_id(cpu);
  11246. cpup->core_id = topology_core_id(cpu);
  11247. if (lpfc_find_hyper(phba, cpu, cpup->phys_id, cpup->core_id))
  11248. cpup->flag |= LPFC_CPU_MAP_HYPER;
  11249. #else
  11250. /* No distinction between CPUs for other platforms */
  11251. cpup->phys_id = 0;
  11252. cpup->core_id = cpu;
  11253. #endif
  11254. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11255. "3328 CPU %d physid %d coreid %d flag x%x\n",
  11256. cpu, cpup->phys_id, cpup->core_id, cpup->flag);
  11257. if (cpup->phys_id > max_phys_id)
  11258. max_phys_id = cpup->phys_id;
  11259. if (cpup->phys_id < min_phys_id)
  11260. min_phys_id = cpup->phys_id;
  11261. if (cpup->core_id > max_core_id)
  11262. max_core_id = cpup->core_id;
  11263. if (cpup->core_id < min_core_id)
  11264. min_core_id = cpup->core_id;
  11265. }
  11266. /* After looking at each irq vector assigned to this pcidev, its
  11267. * possible to see that not ALL CPUs have been accounted for.
  11268. * Next we will set any unassigned (unaffinitized) cpu map
  11269. * entries to a IRQ on the same phys_id.
  11270. */
  11271. first_cpu = cpumask_first(cpu_present_mask);
  11272. start_cpu = first_cpu;
  11273. for_each_present_cpu(cpu) {
  11274. cpup = &phba->sli4_hba.cpu_map[cpu];
  11275. /* Is this CPU entry unassigned */
  11276. if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
  11277. /* Mark CPU as IRQ not assigned by the kernel */
  11278. cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
  11279. /* If so, find a new_cpup that is on the SAME
  11280. * phys_id as cpup. start_cpu will start where we
  11281. * left off so all unassigned entries don't get assgined
  11282. * the IRQ of the first entry.
  11283. */
  11284. new_cpu = start_cpu;
  11285. for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
  11286. new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
  11287. if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
  11288. (new_cpup->eq != LPFC_VECTOR_MAP_EMPTY) &&
  11289. (new_cpup->phys_id == cpup->phys_id))
  11290. goto found_same;
  11291. new_cpu = lpfc_next_present_cpu(new_cpu);
  11292. }
  11293. /* At this point, we leave the CPU as unassigned */
  11294. continue;
  11295. found_same:
  11296. /* We found a matching phys_id, so copy the IRQ info */
  11297. cpup->eq = new_cpup->eq;
  11298. /* Bump start_cpu to the next slot to minmize the
  11299. * chance of having multiple unassigned CPU entries
  11300. * selecting the same IRQ.
  11301. */
  11302. start_cpu = lpfc_next_present_cpu(new_cpu);
  11303. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11304. "3337 Set Affinity: CPU %d "
  11305. "eq %d from peer cpu %d same "
  11306. "phys_id (%d)\n",
  11307. cpu, cpup->eq, new_cpu,
  11308. cpup->phys_id);
  11309. }
  11310. }
  11311. /* Set any unassigned cpu map entries to a IRQ on any phys_id */
  11312. start_cpu = first_cpu;
  11313. for_each_present_cpu(cpu) {
  11314. cpup = &phba->sli4_hba.cpu_map[cpu];
  11315. /* Is this entry unassigned */
  11316. if (cpup->eq == LPFC_VECTOR_MAP_EMPTY) {
  11317. /* Mark it as IRQ not assigned by the kernel */
  11318. cpup->flag |= LPFC_CPU_MAP_UNASSIGN;
  11319. /* If so, find a new_cpup thats on ANY phys_id
  11320. * as the cpup. start_cpu will start where we
  11321. * left off so all unassigned entries don't get
  11322. * assigned the IRQ of the first entry.
  11323. */
  11324. new_cpu = start_cpu;
  11325. for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
  11326. new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
  11327. if (!(new_cpup->flag & LPFC_CPU_MAP_UNASSIGN) &&
  11328. (new_cpup->eq != LPFC_VECTOR_MAP_EMPTY))
  11329. goto found_any;
  11330. new_cpu = lpfc_next_present_cpu(new_cpu);
  11331. }
  11332. /* We should never leave an entry unassigned */
  11333. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11334. "3339 Set Affinity: CPU %d "
  11335. "eq %d UNASSIGNED\n",
  11336. cpup->hdwq, cpup->eq);
  11337. continue;
  11338. found_any:
  11339. /* We found an available entry, copy the IRQ info */
  11340. cpup->eq = new_cpup->eq;
  11341. /* Bump start_cpu to the next slot to minmize the
  11342. * chance of having multiple unassigned CPU entries
  11343. * selecting the same IRQ.
  11344. */
  11345. start_cpu = lpfc_next_present_cpu(new_cpu);
  11346. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11347. "3338 Set Affinity: CPU %d "
  11348. "eq %d from peer cpu %d (%d/%d)\n",
  11349. cpu, cpup->eq, new_cpu,
  11350. new_cpup->phys_id, new_cpup->core_id);
  11351. }
  11352. }
  11353. /* Assign hdwq indices that are unique across all cpus in the map
  11354. * that are also FIRST_CPUs.
  11355. */
  11356. idx = 0;
  11357. for_each_present_cpu(cpu) {
  11358. cpup = &phba->sli4_hba.cpu_map[cpu];
  11359. /* Only FIRST IRQs get a hdwq index assignment. */
  11360. if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
  11361. continue;
  11362. /* 1 to 1, the first LPFC_CPU_FIRST_IRQ cpus to a unique hdwq */
  11363. cpup->hdwq = idx;
  11364. idx++;
  11365. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11366. "3333 Set Affinity: CPU %d (phys %d core %d): "
  11367. "hdwq %d eq %d flg x%x\n",
  11368. cpu, cpup->phys_id, cpup->core_id,
  11369. cpup->hdwq, cpup->eq, cpup->flag);
  11370. }
  11371. /* Associate a hdwq with each cpu_map entry
  11372. * This will be 1 to 1 - hdwq to cpu, unless there are less
  11373. * hardware queues then CPUs. For that case we will just round-robin
  11374. * the available hardware queues as they get assigned to CPUs.
  11375. * The next_idx is the idx from the FIRST_CPU loop above to account
  11376. * for irq_chann < hdwq. The idx is used for round-robin assignments
  11377. * and needs to start at 0.
  11378. */
  11379. next_idx = idx;
  11380. start_cpu = 0;
  11381. idx = 0;
  11382. for_each_present_cpu(cpu) {
  11383. cpup = &phba->sli4_hba.cpu_map[cpu];
  11384. /* FIRST cpus are already mapped. */
  11385. if (cpup->flag & LPFC_CPU_FIRST_IRQ)
  11386. continue;
  11387. /* If the cfg_irq_chann < cfg_hdw_queue, set the hdwq
  11388. * of the unassigned cpus to the next idx so that all
  11389. * hdw queues are fully utilized.
  11390. */
  11391. if (next_idx < phba->cfg_hdw_queue) {
  11392. cpup->hdwq = next_idx;
  11393. next_idx++;
  11394. continue;
  11395. }
  11396. /* Not a First CPU and all hdw_queues are used. Reuse a
  11397. * Hardware Queue for another CPU, so be smart about it
  11398. * and pick one that has its IRQ/EQ mapped to the same phys_id
  11399. * (CPU package) and core_id.
  11400. */
  11401. new_cpu = start_cpu;
  11402. for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
  11403. new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
  11404. if (new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY &&
  11405. new_cpup->phys_id == cpup->phys_id &&
  11406. new_cpup->core_id == cpup->core_id) {
  11407. goto found_hdwq;
  11408. }
  11409. new_cpu = lpfc_next_present_cpu(new_cpu);
  11410. }
  11411. /* If we can't match both phys_id and core_id,
  11412. * settle for just a phys_id match.
  11413. */
  11414. new_cpu = start_cpu;
  11415. for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
  11416. new_cpup = &phba->sli4_hba.cpu_map[new_cpu];
  11417. if (new_cpup->hdwq != LPFC_VECTOR_MAP_EMPTY &&
  11418. new_cpup->phys_id == cpup->phys_id)
  11419. goto found_hdwq;
  11420. new_cpu = lpfc_next_present_cpu(new_cpu);
  11421. }
  11422. /* Otherwise just round robin on cfg_hdw_queue */
  11423. cpup->hdwq = idx % phba->cfg_hdw_queue;
  11424. idx++;
  11425. goto logit;
  11426. found_hdwq:
  11427. /* We found an available entry, copy the IRQ info */
  11428. start_cpu = lpfc_next_present_cpu(new_cpu);
  11429. cpup->hdwq = new_cpup->hdwq;
  11430. logit:
  11431. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11432. "3335 Set Affinity: CPU %d (phys %d core %d): "
  11433. "hdwq %d eq %d flg x%x\n",
  11434. cpu, cpup->phys_id, cpup->core_id,
  11435. cpup->hdwq, cpup->eq, cpup->flag);
  11436. }
  11437. /*
  11438. * Initialize the cpu_map slots for not-present cpus in case
  11439. * a cpu is hot-added. Perform a simple hdwq round robin assignment.
  11440. */
  11441. idx = 0;
  11442. for_each_possible_cpu(cpu) {
  11443. cpup = &phba->sli4_hba.cpu_map[cpu];
  11444. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  11445. c_stat = per_cpu_ptr(phba->sli4_hba.c_stat, cpu);
  11446. c_stat->hdwq_no = cpup->hdwq;
  11447. #endif
  11448. if (cpup->hdwq != LPFC_VECTOR_MAP_EMPTY)
  11449. continue;
  11450. cpup->hdwq = idx++ % phba->cfg_hdw_queue;
  11451. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  11452. c_stat->hdwq_no = cpup->hdwq;
  11453. #endif
  11454. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11455. "3340 Set Affinity: not present "
  11456. "CPU %d hdwq %d\n",
  11457. cpu, cpup->hdwq);
  11458. }
  11459. /* The cpu_map array will be used later during initialization
  11460. * when EQ / CQ / WQs are allocated and configured.
  11461. */
  11462. return;
  11463. }
  11464. /**
  11465. * lpfc_cpuhp_get_eq
  11466. *
  11467. * @phba: pointer to lpfc hba data structure.
  11468. * @cpu: cpu going offline
  11469. * @eqlist: eq list to append to
  11470. */
  11471. static int
  11472. lpfc_cpuhp_get_eq(struct lpfc_hba *phba, unsigned int cpu,
  11473. struct list_head *eqlist)
  11474. {
  11475. const struct cpumask *maskp;
  11476. struct lpfc_queue *eq;
  11477. struct cpumask *tmp;
  11478. u16 idx;
  11479. tmp = kzalloc(cpumask_size(), GFP_KERNEL);
  11480. if (!tmp)
  11481. return -ENOMEM;
  11482. for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
  11483. maskp = pci_irq_get_affinity(phba->pcidev, idx);
  11484. if (!maskp)
  11485. continue;
  11486. /*
  11487. * if irq is not affinitized to the cpu going
  11488. * then we don't need to poll the eq attached
  11489. * to it.
  11490. */
  11491. if (!cpumask_and(tmp, maskp, cpumask_of(cpu)))
  11492. continue;
  11493. /* get the cpus that are online and are affini-
  11494. * tized to this irq vector. If the count is
  11495. * more than 1 then cpuhp is not going to shut-
  11496. * down this vector. Since this cpu has not
  11497. * gone offline yet, we need >1.
  11498. */
  11499. cpumask_and(tmp, maskp, cpu_online_mask);
  11500. if (cpumask_weight(tmp) > 1)
  11501. continue;
  11502. /* Now that we have an irq to shutdown, get the eq
  11503. * mapped to this irq. Note: multiple hdwq's in
  11504. * the software can share an eq, but eventually
  11505. * only eq will be mapped to this vector
  11506. */
  11507. eq = phba->sli4_hba.hba_eq_hdl[idx].eq;
  11508. list_add(&eq->_poll_list, eqlist);
  11509. }
  11510. kfree(tmp);
  11511. return 0;
  11512. }
  11513. static void __lpfc_cpuhp_remove(struct lpfc_hba *phba)
  11514. {
  11515. if (phba->sli_rev != LPFC_SLI_REV4)
  11516. return;
  11517. cpuhp_state_remove_instance_nocalls(lpfc_cpuhp_state,
  11518. &phba->cpuhp);
  11519. /*
  11520. * unregistering the instance doesn't stop the polling
  11521. * timer. Wait for the poll timer to retire.
  11522. */
  11523. synchronize_rcu();
  11524. del_timer_sync(&phba->cpuhp_poll_timer);
  11525. }
  11526. static void lpfc_cpuhp_remove(struct lpfc_hba *phba)
  11527. {
  11528. if (phba->pport &&
  11529. test_bit(FC_OFFLINE_MODE, &phba->pport->fc_flag))
  11530. return;
  11531. __lpfc_cpuhp_remove(phba);
  11532. }
  11533. static void lpfc_cpuhp_add(struct lpfc_hba *phba)
  11534. {
  11535. if (phba->sli_rev != LPFC_SLI_REV4)
  11536. return;
  11537. rcu_read_lock();
  11538. if (!list_empty(&phba->poll_list))
  11539. mod_timer(&phba->cpuhp_poll_timer,
  11540. jiffies + msecs_to_jiffies(LPFC_POLL_HB));
  11541. rcu_read_unlock();
  11542. cpuhp_state_add_instance_nocalls(lpfc_cpuhp_state,
  11543. &phba->cpuhp);
  11544. }
  11545. static int __lpfc_cpuhp_checks(struct lpfc_hba *phba, int *retval)
  11546. {
  11547. if (test_bit(FC_UNLOADING, &phba->pport->load_flag)) {
  11548. *retval = -EAGAIN;
  11549. return true;
  11550. }
  11551. if (phba->sli_rev != LPFC_SLI_REV4) {
  11552. *retval = 0;
  11553. return true;
  11554. }
  11555. /* proceed with the hotplug */
  11556. return false;
  11557. }
  11558. /**
  11559. * lpfc_irq_set_aff - set IRQ affinity
  11560. * @eqhdl: EQ handle
  11561. * @cpu: cpu to set affinity
  11562. *
  11563. **/
  11564. static inline void
  11565. lpfc_irq_set_aff(struct lpfc_hba_eq_hdl *eqhdl, unsigned int cpu)
  11566. {
  11567. cpumask_clear(&eqhdl->aff_mask);
  11568. cpumask_set_cpu(cpu, &eqhdl->aff_mask);
  11569. irq_set_status_flags(eqhdl->irq, IRQ_NO_BALANCING);
  11570. irq_set_affinity(eqhdl->irq, &eqhdl->aff_mask);
  11571. }
  11572. /**
  11573. * lpfc_irq_clear_aff - clear IRQ affinity
  11574. * @eqhdl: EQ handle
  11575. *
  11576. **/
  11577. static inline void
  11578. lpfc_irq_clear_aff(struct lpfc_hba_eq_hdl *eqhdl)
  11579. {
  11580. cpumask_clear(&eqhdl->aff_mask);
  11581. irq_clear_status_flags(eqhdl->irq, IRQ_NO_BALANCING);
  11582. }
  11583. /**
  11584. * lpfc_irq_rebalance - rebalances IRQ affinity according to cpuhp event
  11585. * @phba: pointer to HBA context object.
  11586. * @cpu: cpu going offline/online
  11587. * @offline: true, cpu is going offline. false, cpu is coming online.
  11588. *
  11589. * If cpu is going offline, we'll try our best effort to find the next
  11590. * online cpu on the phba's original_mask and migrate all offlining IRQ
  11591. * affinities.
  11592. *
  11593. * If cpu is coming online, reaffinitize the IRQ back to the onlining cpu.
  11594. *
  11595. * Note: Call only if NUMA or NHT mode is enabled, otherwise rely on
  11596. * PCI_IRQ_AFFINITY to auto-manage IRQ affinity.
  11597. *
  11598. **/
  11599. static void
  11600. lpfc_irq_rebalance(struct lpfc_hba *phba, unsigned int cpu, bool offline)
  11601. {
  11602. struct lpfc_vector_map_info *cpup;
  11603. struct cpumask *aff_mask;
  11604. unsigned int cpu_select, cpu_next, idx;
  11605. const struct cpumask *orig_mask;
  11606. if (phba->irq_chann_mode == NORMAL_MODE)
  11607. return;
  11608. orig_mask = &phba->sli4_hba.irq_aff_mask;
  11609. if (!cpumask_test_cpu(cpu, orig_mask))
  11610. return;
  11611. cpup = &phba->sli4_hba.cpu_map[cpu];
  11612. if (!(cpup->flag & LPFC_CPU_FIRST_IRQ))
  11613. return;
  11614. if (offline) {
  11615. /* Find next online CPU on original mask */
  11616. cpu_next = cpumask_next_wrap(cpu, orig_mask, cpu, true);
  11617. cpu_select = lpfc_next_online_cpu(orig_mask, cpu_next);
  11618. /* Found a valid CPU */
  11619. if ((cpu_select < nr_cpu_ids) && (cpu_select != cpu)) {
  11620. /* Go through each eqhdl and ensure offlining
  11621. * cpu aff_mask is migrated
  11622. */
  11623. for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
  11624. aff_mask = lpfc_get_aff_mask(idx);
  11625. /* Migrate affinity */
  11626. if (cpumask_test_cpu(cpu, aff_mask))
  11627. lpfc_irq_set_aff(lpfc_get_eq_hdl(idx),
  11628. cpu_select);
  11629. }
  11630. } else {
  11631. /* Rely on irqbalance if no online CPUs left on NUMA */
  11632. for (idx = 0; idx < phba->cfg_irq_chann; idx++)
  11633. lpfc_irq_clear_aff(lpfc_get_eq_hdl(idx));
  11634. }
  11635. } else {
  11636. /* Migrate affinity back to this CPU */
  11637. lpfc_irq_set_aff(lpfc_get_eq_hdl(cpup->eq), cpu);
  11638. }
  11639. }
  11640. static int lpfc_cpu_offline(unsigned int cpu, struct hlist_node *node)
  11641. {
  11642. struct lpfc_hba *phba = hlist_entry_safe(node, struct lpfc_hba, cpuhp);
  11643. struct lpfc_queue *eq, *next;
  11644. LIST_HEAD(eqlist);
  11645. int retval;
  11646. if (!phba) {
  11647. WARN_ONCE(!phba, "cpu: %u. phba:NULL", raw_smp_processor_id());
  11648. return 0;
  11649. }
  11650. if (__lpfc_cpuhp_checks(phba, &retval))
  11651. return retval;
  11652. lpfc_irq_rebalance(phba, cpu, true);
  11653. retval = lpfc_cpuhp_get_eq(phba, cpu, &eqlist);
  11654. if (retval)
  11655. return retval;
  11656. /* start polling on these eq's */
  11657. list_for_each_entry_safe(eq, next, &eqlist, _poll_list) {
  11658. list_del_init(&eq->_poll_list);
  11659. lpfc_sli4_start_polling(eq);
  11660. }
  11661. return 0;
  11662. }
  11663. static int lpfc_cpu_online(unsigned int cpu, struct hlist_node *node)
  11664. {
  11665. struct lpfc_hba *phba = hlist_entry_safe(node, struct lpfc_hba, cpuhp);
  11666. struct lpfc_queue *eq, *next;
  11667. unsigned int n;
  11668. int retval;
  11669. if (!phba) {
  11670. WARN_ONCE(!phba, "cpu: %u. phba:NULL", raw_smp_processor_id());
  11671. return 0;
  11672. }
  11673. if (__lpfc_cpuhp_checks(phba, &retval))
  11674. return retval;
  11675. lpfc_irq_rebalance(phba, cpu, false);
  11676. list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list) {
  11677. n = lpfc_find_cpu_handle(phba, eq->hdwq, LPFC_FIND_BY_HDWQ);
  11678. if (n == cpu)
  11679. lpfc_sli4_stop_polling(eq);
  11680. }
  11681. return 0;
  11682. }
  11683. /**
  11684. * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
  11685. * @phba: pointer to lpfc hba data structure.
  11686. *
  11687. * This routine is invoked to enable the MSI-X interrupt vectors to device
  11688. * with SLI-4 interface spec. It also allocates MSI-X vectors and maps them
  11689. * to cpus on the system.
  11690. *
  11691. * When cfg_irq_numa is enabled, the adapter will only allocate vectors for
  11692. * the number of cpus on the same numa node as this adapter. The vectors are
  11693. * allocated without requesting OS affinity mapping. A vector will be
  11694. * allocated and assigned to each online and offline cpu. If the cpu is
  11695. * online, then affinity will be set to that cpu. If the cpu is offline, then
  11696. * affinity will be set to the nearest peer cpu within the numa node that is
  11697. * online. If there are no online cpus within the numa node, affinity is not
  11698. * assigned and the OS may do as it pleases. Note: cpu vector affinity mapping
  11699. * is consistent with the way cpu online/offline is handled when cfg_irq_numa is
  11700. * configured.
  11701. *
  11702. * If numa mode is not enabled and there is more than 1 vector allocated, then
  11703. * the driver relies on the managed irq interface where the OS assigns vector to
  11704. * cpu affinity. The driver will then use that affinity mapping to setup its
  11705. * cpu mapping table.
  11706. *
  11707. * Return codes
  11708. * 0 - successful
  11709. * other values - error
  11710. **/
  11711. static int
  11712. lpfc_sli4_enable_msix(struct lpfc_hba *phba)
  11713. {
  11714. int vectors, rc, index;
  11715. char *name;
  11716. const struct cpumask *aff_mask = NULL;
  11717. unsigned int cpu = 0, cpu_cnt = 0, cpu_select = nr_cpu_ids;
  11718. struct lpfc_vector_map_info *cpup;
  11719. struct lpfc_hba_eq_hdl *eqhdl;
  11720. const struct cpumask *maskp;
  11721. unsigned int flags = PCI_IRQ_MSIX;
  11722. /* Set up MSI-X multi-message vectors */
  11723. vectors = phba->cfg_irq_chann;
  11724. if (phba->irq_chann_mode != NORMAL_MODE)
  11725. aff_mask = &phba->sli4_hba.irq_aff_mask;
  11726. if (aff_mask) {
  11727. cpu_cnt = cpumask_weight(aff_mask);
  11728. vectors = min(phba->cfg_irq_chann, cpu_cnt);
  11729. /* cpu: iterates over aff_mask including offline or online
  11730. * cpu_select: iterates over online aff_mask to set affinity
  11731. */
  11732. cpu = cpumask_first(aff_mask);
  11733. cpu_select = lpfc_next_online_cpu(aff_mask, cpu);
  11734. } else {
  11735. flags |= PCI_IRQ_AFFINITY;
  11736. }
  11737. rc = pci_alloc_irq_vectors(phba->pcidev, 1, vectors, flags);
  11738. if (rc < 0) {
  11739. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11740. "0484 PCI enable MSI-X failed (%d)\n", rc);
  11741. goto vec_fail_out;
  11742. }
  11743. vectors = rc;
  11744. /* Assign MSI-X vectors to interrupt handlers */
  11745. for (index = 0; index < vectors; index++) {
  11746. eqhdl = lpfc_get_eq_hdl(index);
  11747. name = eqhdl->handler_name;
  11748. memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
  11749. snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
  11750. LPFC_DRIVER_HANDLER_NAME"%d", index);
  11751. eqhdl->idx = index;
  11752. rc = pci_irq_vector(phba->pcidev, index);
  11753. if (rc < 0) {
  11754. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  11755. "0489 MSI-X fast-path (%d) "
  11756. "pci_irq_vec failed (%d)\n", index, rc);
  11757. goto cfg_fail_out;
  11758. }
  11759. eqhdl->irq = rc;
  11760. rc = request_threaded_irq(eqhdl->irq,
  11761. &lpfc_sli4_hba_intr_handler,
  11762. &lpfc_sli4_hba_intr_handler_th,
  11763. 0, name, eqhdl);
  11764. if (rc) {
  11765. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  11766. "0486 MSI-X fast-path (%d) "
  11767. "request_irq failed (%d)\n", index, rc);
  11768. goto cfg_fail_out;
  11769. }
  11770. if (aff_mask) {
  11771. /* If found a neighboring online cpu, set affinity */
  11772. if (cpu_select < nr_cpu_ids)
  11773. lpfc_irq_set_aff(eqhdl, cpu_select);
  11774. /* Assign EQ to cpu_map */
  11775. lpfc_assign_eq_map_info(phba, index,
  11776. LPFC_CPU_FIRST_IRQ,
  11777. cpu);
  11778. /* Iterate to next offline or online cpu in aff_mask */
  11779. cpu = cpumask_next(cpu, aff_mask);
  11780. /* Find next online cpu in aff_mask to set affinity */
  11781. cpu_select = lpfc_next_online_cpu(aff_mask, cpu);
  11782. } else if (vectors == 1) {
  11783. cpu = cpumask_first(cpu_present_mask);
  11784. lpfc_assign_eq_map_info(phba, index, LPFC_CPU_FIRST_IRQ,
  11785. cpu);
  11786. } else {
  11787. maskp = pci_irq_get_affinity(phba->pcidev, index);
  11788. /* Loop through all CPUs associated with vector index */
  11789. for_each_cpu_and(cpu, maskp, cpu_present_mask) {
  11790. cpup = &phba->sli4_hba.cpu_map[cpu];
  11791. /* If this is the first CPU thats assigned to
  11792. * this vector, set LPFC_CPU_FIRST_IRQ.
  11793. *
  11794. * With certain platforms its possible that irq
  11795. * vectors are affinitized to all the cpu's.
  11796. * This can result in each cpu_map.eq to be set
  11797. * to the last vector, resulting in overwrite
  11798. * of all the previous cpu_map.eq. Ensure that
  11799. * each vector receives a place in cpu_map.
  11800. * Later call to lpfc_cpu_affinity_check will
  11801. * ensure we are nicely balanced out.
  11802. */
  11803. if (cpup->eq != LPFC_VECTOR_MAP_EMPTY)
  11804. continue;
  11805. lpfc_assign_eq_map_info(phba, index,
  11806. LPFC_CPU_FIRST_IRQ,
  11807. cpu);
  11808. break;
  11809. }
  11810. }
  11811. }
  11812. if (vectors != phba->cfg_irq_chann) {
  11813. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  11814. "3238 Reducing IO channels to match number of "
  11815. "MSI-X vectors, requested %d got %d\n",
  11816. phba->cfg_irq_chann, vectors);
  11817. if (phba->cfg_irq_chann > vectors)
  11818. phba->cfg_irq_chann = vectors;
  11819. }
  11820. return rc;
  11821. cfg_fail_out:
  11822. /* free the irq already requested */
  11823. for (--index; index >= 0; index--) {
  11824. eqhdl = lpfc_get_eq_hdl(index);
  11825. lpfc_irq_clear_aff(eqhdl);
  11826. free_irq(eqhdl->irq, eqhdl);
  11827. }
  11828. /* Unconfigure MSI-X capability structure */
  11829. pci_free_irq_vectors(phba->pcidev);
  11830. vec_fail_out:
  11831. return rc;
  11832. }
  11833. /**
  11834. * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
  11835. * @phba: pointer to lpfc hba data structure.
  11836. *
  11837. * This routine is invoked to enable the MSI interrupt mode to device with
  11838. * SLI-4 interface spec. The kernel function pci_alloc_irq_vectors() is
  11839. * called to enable the MSI vector. The device driver is responsible for
  11840. * calling the request_irq() to register MSI vector with a interrupt the
  11841. * handler, which is done in this function.
  11842. *
  11843. * Return codes
  11844. * 0 - successful
  11845. * other values - error
  11846. **/
  11847. static int
  11848. lpfc_sli4_enable_msi(struct lpfc_hba *phba)
  11849. {
  11850. int rc, index;
  11851. unsigned int cpu;
  11852. struct lpfc_hba_eq_hdl *eqhdl;
  11853. rc = pci_alloc_irq_vectors(phba->pcidev, 1, 1,
  11854. PCI_IRQ_MSI | PCI_IRQ_AFFINITY);
  11855. if (rc > 0)
  11856. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11857. "0487 PCI enable MSI mode success.\n");
  11858. else {
  11859. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11860. "0488 PCI enable MSI mode failed (%d)\n", rc);
  11861. return rc ? rc : -1;
  11862. }
  11863. rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
  11864. 0, LPFC_DRIVER_NAME, phba);
  11865. if (rc) {
  11866. pci_free_irq_vectors(phba->pcidev);
  11867. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  11868. "0490 MSI request_irq failed (%d)\n", rc);
  11869. return rc;
  11870. }
  11871. eqhdl = lpfc_get_eq_hdl(0);
  11872. rc = pci_irq_vector(phba->pcidev, 0);
  11873. if (rc < 0) {
  11874. pci_free_irq_vectors(phba->pcidev);
  11875. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  11876. "0496 MSI pci_irq_vec failed (%d)\n", rc);
  11877. return rc;
  11878. }
  11879. eqhdl->irq = rc;
  11880. cpu = cpumask_first(cpu_present_mask);
  11881. lpfc_assign_eq_map_info(phba, 0, LPFC_CPU_FIRST_IRQ, cpu);
  11882. for (index = 0; index < phba->cfg_irq_chann; index++) {
  11883. eqhdl = lpfc_get_eq_hdl(index);
  11884. eqhdl->idx = index;
  11885. }
  11886. return 0;
  11887. }
  11888. /**
  11889. * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
  11890. * @phba: pointer to lpfc hba data structure.
  11891. * @cfg_mode: Interrupt configuration mode (INTx, MSI or MSI-X).
  11892. *
  11893. * This routine is invoked to enable device interrupt and associate driver's
  11894. * interrupt handler(s) to interrupt vector(s) to device with SLI-4
  11895. * interface spec. Depends on the interrupt mode configured to the driver,
  11896. * the driver will try to fallback from the configured interrupt mode to an
  11897. * interrupt mode which is supported by the platform, kernel, and device in
  11898. * the order of:
  11899. * MSI-X -> MSI -> IRQ.
  11900. *
  11901. * Return codes
  11902. * Interrupt mode (2, 1, 0) - successful
  11903. * LPFC_INTR_ERROR - error
  11904. **/
  11905. static uint32_t
  11906. lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
  11907. {
  11908. uint32_t intr_mode = LPFC_INTR_ERROR;
  11909. int retval, idx;
  11910. if (cfg_mode == 2) {
  11911. /* Preparation before conf_msi mbox cmd */
  11912. retval = 0;
  11913. if (!retval) {
  11914. /* Now, try to enable MSI-X interrupt mode */
  11915. retval = lpfc_sli4_enable_msix(phba);
  11916. if (!retval) {
  11917. /* Indicate initialization to MSI-X mode */
  11918. phba->intr_type = MSIX;
  11919. intr_mode = 2;
  11920. }
  11921. }
  11922. }
  11923. /* Fallback to MSI if MSI-X initialization failed */
  11924. if (cfg_mode >= 1 && phba->intr_type == NONE) {
  11925. retval = lpfc_sli4_enable_msi(phba);
  11926. if (!retval) {
  11927. /* Indicate initialization to MSI mode */
  11928. phba->intr_type = MSI;
  11929. intr_mode = 1;
  11930. }
  11931. }
  11932. /* Fallback to INTx if both MSI-X/MSI initalization failed */
  11933. if (phba->intr_type == NONE) {
  11934. retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
  11935. IRQF_SHARED, LPFC_DRIVER_NAME, phba);
  11936. if (!retval) {
  11937. struct lpfc_hba_eq_hdl *eqhdl;
  11938. unsigned int cpu;
  11939. /* Indicate initialization to INTx mode */
  11940. phba->intr_type = INTx;
  11941. intr_mode = 0;
  11942. eqhdl = lpfc_get_eq_hdl(0);
  11943. retval = pci_irq_vector(phba->pcidev, 0);
  11944. if (retval < 0) {
  11945. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  11946. "0502 INTR pci_irq_vec failed (%d)\n",
  11947. retval);
  11948. return LPFC_INTR_ERROR;
  11949. }
  11950. eqhdl->irq = retval;
  11951. cpu = cpumask_first(cpu_present_mask);
  11952. lpfc_assign_eq_map_info(phba, 0, LPFC_CPU_FIRST_IRQ,
  11953. cpu);
  11954. for (idx = 0; idx < phba->cfg_irq_chann; idx++) {
  11955. eqhdl = lpfc_get_eq_hdl(idx);
  11956. eqhdl->idx = idx;
  11957. }
  11958. }
  11959. }
  11960. return intr_mode;
  11961. }
  11962. /**
  11963. * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
  11964. * @phba: pointer to lpfc hba data structure.
  11965. *
  11966. * This routine is invoked to disable device interrupt and disassociate
  11967. * the driver's interrupt handler(s) from interrupt vector(s) to device
  11968. * with SLI-4 interface spec. Depending on the interrupt mode, the driver
  11969. * will release the interrupt vector(s) for the message signaled interrupt.
  11970. **/
  11971. static void
  11972. lpfc_sli4_disable_intr(struct lpfc_hba *phba)
  11973. {
  11974. /* Disable the currently initialized interrupt mode */
  11975. if (phba->intr_type == MSIX) {
  11976. int index;
  11977. struct lpfc_hba_eq_hdl *eqhdl;
  11978. /* Free up MSI-X multi-message vectors */
  11979. for (index = 0; index < phba->cfg_irq_chann; index++) {
  11980. eqhdl = lpfc_get_eq_hdl(index);
  11981. lpfc_irq_clear_aff(eqhdl);
  11982. free_irq(eqhdl->irq, eqhdl);
  11983. }
  11984. } else {
  11985. free_irq(phba->pcidev->irq, phba);
  11986. }
  11987. pci_free_irq_vectors(phba->pcidev);
  11988. /* Reset interrupt management states */
  11989. phba->intr_type = NONE;
  11990. phba->sli.slistat.sli_intr = 0;
  11991. }
  11992. /**
  11993. * lpfc_unset_hba - Unset SLI3 hba device initialization
  11994. * @phba: pointer to lpfc hba data structure.
  11995. *
  11996. * This routine is invoked to unset the HBA device initialization steps to
  11997. * a device with SLI-3 interface spec.
  11998. **/
  11999. static void
  12000. lpfc_unset_hba(struct lpfc_hba *phba)
  12001. {
  12002. set_bit(FC_UNLOADING, &phba->pport->load_flag);
  12003. kfree(phba->vpi_bmask);
  12004. kfree(phba->vpi_ids);
  12005. lpfc_stop_hba_timers(phba);
  12006. phba->pport->work_port_events = 0;
  12007. lpfc_sli_hba_down(phba);
  12008. lpfc_sli_brdrestart(phba);
  12009. lpfc_sli_disable_intr(phba);
  12010. return;
  12011. }
  12012. /**
  12013. * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
  12014. * @phba: Pointer to HBA context object.
  12015. *
  12016. * This function is called in the SLI4 code path to wait for completion
  12017. * of device's XRIs exchange busy. It will check the XRI exchange busy
  12018. * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
  12019. * that, it will check the XRI exchange busy on outstanding FCP and ELS
  12020. * I/Os every 30 seconds, log error message, and wait forever. Only when
  12021. * all XRI exchange busy complete, the driver unload shall proceed with
  12022. * invoking the function reset ioctl mailbox command to the CNA and the
  12023. * the rest of the driver unload resource release.
  12024. **/
  12025. static void
  12026. lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
  12027. {
  12028. struct lpfc_sli4_hdw_queue *qp;
  12029. int idx, ccnt;
  12030. int wait_time = 0;
  12031. int io_xri_cmpl = 1;
  12032. int nvmet_xri_cmpl = 1;
  12033. int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
  12034. /* Driver just aborted IOs during the hba_unset process. Pause
  12035. * here to give the HBA time to complete the IO and get entries
  12036. * into the abts lists.
  12037. */
  12038. msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1 * 5);
  12039. /* Wait for NVME pending IO to flush back to transport. */
  12040. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
  12041. lpfc_nvme_wait_for_io_drain(phba);
  12042. ccnt = 0;
  12043. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  12044. qp = &phba->sli4_hba.hdwq[idx];
  12045. io_xri_cmpl = list_empty(&qp->lpfc_abts_io_buf_list);
  12046. if (!io_xri_cmpl) /* if list is NOT empty */
  12047. ccnt++;
  12048. }
  12049. if (ccnt)
  12050. io_xri_cmpl = 0;
  12051. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  12052. nvmet_xri_cmpl =
  12053. list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
  12054. }
  12055. while (!els_xri_cmpl || !io_xri_cmpl || !nvmet_xri_cmpl) {
  12056. if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
  12057. if (!nvmet_xri_cmpl)
  12058. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  12059. "6424 NVMET XRI exchange busy "
  12060. "wait time: %d seconds.\n",
  12061. wait_time/1000);
  12062. if (!io_xri_cmpl)
  12063. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  12064. "6100 IO XRI exchange busy "
  12065. "wait time: %d seconds.\n",
  12066. wait_time/1000);
  12067. if (!els_xri_cmpl)
  12068. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  12069. "2878 ELS XRI exchange busy "
  12070. "wait time: %d seconds.\n",
  12071. wait_time/1000);
  12072. msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
  12073. wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
  12074. } else {
  12075. msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
  12076. wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
  12077. }
  12078. ccnt = 0;
  12079. for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
  12080. qp = &phba->sli4_hba.hdwq[idx];
  12081. io_xri_cmpl = list_empty(
  12082. &qp->lpfc_abts_io_buf_list);
  12083. if (!io_xri_cmpl) /* if list is NOT empty */
  12084. ccnt++;
  12085. }
  12086. if (ccnt)
  12087. io_xri_cmpl = 0;
  12088. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  12089. nvmet_xri_cmpl = list_empty(
  12090. &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
  12091. }
  12092. els_xri_cmpl =
  12093. list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
  12094. }
  12095. }
  12096. /**
  12097. * lpfc_sli4_hba_unset - Unset the fcoe hba
  12098. * @phba: Pointer to HBA context object.
  12099. *
  12100. * This function is called in the SLI4 code path to reset the HBA's FCoE
  12101. * function. The caller is not required to hold any lock. This routine
  12102. * issues PCI function reset mailbox command to reset the FCoE function.
  12103. * At the end of the function, it calls lpfc_hba_down_post function to
  12104. * free any pending commands.
  12105. **/
  12106. static void
  12107. lpfc_sli4_hba_unset(struct lpfc_hba *phba)
  12108. {
  12109. int wait_cnt = 0;
  12110. LPFC_MBOXQ_t *mboxq;
  12111. struct pci_dev *pdev = phba->pcidev;
  12112. lpfc_stop_hba_timers(phba);
  12113. hrtimer_cancel(&phba->cmf_stats_timer);
  12114. hrtimer_cancel(&phba->cmf_timer);
  12115. if (phba->pport)
  12116. phba->sli4_hba.intr_enable = 0;
  12117. /*
  12118. * Gracefully wait out the potential current outstanding asynchronous
  12119. * mailbox command.
  12120. */
  12121. /* First, block any pending async mailbox command from posted */
  12122. spin_lock_irq(&phba->hbalock);
  12123. phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  12124. spin_unlock_irq(&phba->hbalock);
  12125. /* Now, trying to wait it out if we can */
  12126. while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  12127. msleep(10);
  12128. if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
  12129. break;
  12130. }
  12131. /* Forcefully release the outstanding mailbox command if timed out */
  12132. if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  12133. spin_lock_irq(&phba->hbalock);
  12134. mboxq = phba->sli.mbox_active;
  12135. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  12136. __lpfc_mbox_cmpl_put(phba, mboxq);
  12137. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  12138. phba->sli.mbox_active = NULL;
  12139. spin_unlock_irq(&phba->hbalock);
  12140. }
  12141. /* Abort all iocbs associated with the hba */
  12142. lpfc_sli_hba_iocb_abort(phba);
  12143. if (!pci_channel_offline(phba->pcidev))
  12144. /* Wait for completion of device XRI exchange busy */
  12145. lpfc_sli4_xri_exchange_busy_wait(phba);
  12146. /* per-phba callback de-registration for hotplug event */
  12147. if (phba->pport)
  12148. lpfc_cpuhp_remove(phba);
  12149. /* Disable PCI subsystem interrupt */
  12150. lpfc_sli4_disable_intr(phba);
  12151. /* Disable SR-IOV if enabled */
  12152. if (phba->cfg_sriov_nr_virtfn)
  12153. pci_disable_sriov(pdev);
  12154. /* Stop kthread signal shall trigger work_done one more time */
  12155. kthread_stop(phba->worker_thread);
  12156. /* Disable FW logging to host memory */
  12157. lpfc_ras_stop_fwlog(phba);
  12158. lpfc_sli4_queue_unset(phba);
  12159. /* Reset SLI4 HBA FCoE function */
  12160. lpfc_pci_function_reset(phba);
  12161. /* release all queue allocated resources. */
  12162. lpfc_sli4_queue_destroy(phba);
  12163. /* Free RAS DMA memory */
  12164. if (phba->ras_fwlog.ras_enabled)
  12165. lpfc_sli4_ras_dma_free(phba);
  12166. /* Stop the SLI4 device port */
  12167. if (phba->pport)
  12168. phba->pport->work_port_events = 0;
  12169. }
  12170. static uint32_t
  12171. lpfc_cgn_crc32(uint32_t crc, u8 byte)
  12172. {
  12173. uint32_t msb = 0;
  12174. uint32_t bit;
  12175. for (bit = 0; bit < 8; bit++) {
  12176. msb = (crc >> 31) & 1;
  12177. crc <<= 1;
  12178. if (msb ^ (byte & 1)) {
  12179. crc ^= LPFC_CGN_CRC32_MAGIC_NUMBER;
  12180. crc |= 1;
  12181. }
  12182. byte >>= 1;
  12183. }
  12184. return crc;
  12185. }
  12186. static uint32_t
  12187. lpfc_cgn_reverse_bits(uint32_t wd)
  12188. {
  12189. uint32_t result = 0;
  12190. uint32_t i;
  12191. for (i = 0; i < 32; i++) {
  12192. result <<= 1;
  12193. result |= (1 & (wd >> i));
  12194. }
  12195. return result;
  12196. }
  12197. /*
  12198. * The routine corresponds with the algorithm the HBA firmware
  12199. * uses to validate the data integrity.
  12200. */
  12201. uint32_t
  12202. lpfc_cgn_calc_crc32(void *ptr, uint32_t byteLen, uint32_t crc)
  12203. {
  12204. uint32_t i;
  12205. uint32_t result;
  12206. uint8_t *data = (uint8_t *)ptr;
  12207. for (i = 0; i < byteLen; ++i)
  12208. crc = lpfc_cgn_crc32(crc, data[i]);
  12209. result = ~lpfc_cgn_reverse_bits(crc);
  12210. return result;
  12211. }
  12212. void
  12213. lpfc_init_congestion_buf(struct lpfc_hba *phba)
  12214. {
  12215. struct lpfc_cgn_info *cp;
  12216. uint16_t size;
  12217. uint32_t crc;
  12218. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
  12219. "6235 INIT Congestion Buffer %p\n", phba->cgn_i);
  12220. if (!phba->cgn_i)
  12221. return;
  12222. cp = (struct lpfc_cgn_info *)phba->cgn_i->virt;
  12223. atomic_set(&phba->cgn_fabric_warn_cnt, 0);
  12224. atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
  12225. atomic_set(&phba->cgn_sync_alarm_cnt, 0);
  12226. atomic_set(&phba->cgn_sync_warn_cnt, 0);
  12227. atomic_set(&phba->cgn_driver_evt_cnt, 0);
  12228. atomic_set(&phba->cgn_latency_evt_cnt, 0);
  12229. atomic64_set(&phba->cgn_latency_evt, 0);
  12230. phba->cgn_evt_minute = 0;
  12231. memset(cp, 0xff, offsetof(struct lpfc_cgn_info, cgn_stat));
  12232. cp->cgn_info_size = cpu_to_le16(LPFC_CGN_INFO_SZ);
  12233. cp->cgn_info_version = LPFC_CGN_INFO_V4;
  12234. /* cgn parameters */
  12235. cp->cgn_info_mode = phba->cgn_p.cgn_param_mode;
  12236. cp->cgn_info_level0 = phba->cgn_p.cgn_param_level0;
  12237. cp->cgn_info_level1 = phba->cgn_p.cgn_param_level1;
  12238. cp->cgn_info_level2 = phba->cgn_p.cgn_param_level2;
  12239. lpfc_cgn_update_tstamp(phba, &cp->base_time);
  12240. /* Fill in default LUN qdepth */
  12241. if (phba->pport) {
  12242. size = (uint16_t)(phba->pport->cfg_lun_queue_depth);
  12243. cp->cgn_lunq = cpu_to_le16(size);
  12244. }
  12245. /* last used Index initialized to 0xff already */
  12246. cp->cgn_warn_freq = cpu_to_le16(LPFC_FPIN_INIT_FREQ);
  12247. cp->cgn_alarm_freq = cpu_to_le16(LPFC_FPIN_INIT_FREQ);
  12248. crc = lpfc_cgn_calc_crc32(cp, LPFC_CGN_INFO_SZ, LPFC_CGN_CRC32_SEED);
  12249. cp->cgn_info_crc = cpu_to_le32(crc);
  12250. phba->cgn_evt_timestamp = jiffies +
  12251. msecs_to_jiffies(LPFC_CGN_TIMER_TO_MIN);
  12252. }
  12253. void
  12254. lpfc_init_congestion_stat(struct lpfc_hba *phba)
  12255. {
  12256. struct lpfc_cgn_info *cp;
  12257. uint32_t crc;
  12258. lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
  12259. "6236 INIT Congestion Stat %p\n", phba->cgn_i);
  12260. if (!phba->cgn_i)
  12261. return;
  12262. cp = (struct lpfc_cgn_info *)phba->cgn_i->virt;
  12263. memset(&cp->cgn_stat, 0, sizeof(cp->cgn_stat));
  12264. lpfc_cgn_update_tstamp(phba, &cp->stat_start);
  12265. crc = lpfc_cgn_calc_crc32(cp, LPFC_CGN_INFO_SZ, LPFC_CGN_CRC32_SEED);
  12266. cp->cgn_info_crc = cpu_to_le32(crc);
  12267. }
  12268. /**
  12269. * __lpfc_reg_congestion_buf - register congestion info buffer with HBA
  12270. * @phba: Pointer to hba context object.
  12271. * @reg: flag to determine register or unregister.
  12272. */
  12273. static int
  12274. __lpfc_reg_congestion_buf(struct lpfc_hba *phba, int reg)
  12275. {
  12276. struct lpfc_mbx_reg_congestion_buf *reg_congestion_buf;
  12277. union lpfc_sli4_cfg_shdr *shdr;
  12278. uint32_t shdr_status, shdr_add_status;
  12279. LPFC_MBOXQ_t *mboxq;
  12280. int length, rc;
  12281. if (!phba->cgn_i)
  12282. return -ENXIO;
  12283. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12284. if (!mboxq) {
  12285. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  12286. "2641 REG_CONGESTION_BUF mbox allocation fail: "
  12287. "HBA state x%x reg %d\n",
  12288. phba->pport->port_state, reg);
  12289. return -ENOMEM;
  12290. }
  12291. length = (sizeof(struct lpfc_mbx_reg_congestion_buf) -
  12292. sizeof(struct lpfc_sli4_cfg_mhdr));
  12293. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  12294. LPFC_MBOX_OPCODE_REG_CONGESTION_BUF, length,
  12295. LPFC_SLI4_MBX_EMBED);
  12296. reg_congestion_buf = &mboxq->u.mqe.un.reg_congestion_buf;
  12297. bf_set(lpfc_mbx_reg_cgn_buf_type, reg_congestion_buf, 1);
  12298. if (reg > 0)
  12299. bf_set(lpfc_mbx_reg_cgn_buf_cnt, reg_congestion_buf, 1);
  12300. else
  12301. bf_set(lpfc_mbx_reg_cgn_buf_cnt, reg_congestion_buf, 0);
  12302. reg_congestion_buf->length = sizeof(struct lpfc_cgn_info);
  12303. reg_congestion_buf->addr_lo =
  12304. putPaddrLow(phba->cgn_i->phys);
  12305. reg_congestion_buf->addr_hi =
  12306. putPaddrHigh(phba->cgn_i->phys);
  12307. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  12308. shdr = (union lpfc_sli4_cfg_shdr *)
  12309. &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
  12310. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12311. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  12312. &shdr->response);
  12313. mempool_free(mboxq, phba->mbox_mem_pool);
  12314. if (shdr_status || shdr_add_status || rc) {
  12315. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12316. "2642 REG_CONGESTION_BUF mailbox "
  12317. "failed with status x%x add_status x%x,"
  12318. " mbx status x%x reg %d\n",
  12319. shdr_status, shdr_add_status, rc, reg);
  12320. return -ENXIO;
  12321. }
  12322. return 0;
  12323. }
  12324. int
  12325. lpfc_unreg_congestion_buf(struct lpfc_hba *phba)
  12326. {
  12327. lpfc_cmf_stop(phba);
  12328. return __lpfc_reg_congestion_buf(phba, 0);
  12329. }
  12330. int
  12331. lpfc_reg_congestion_buf(struct lpfc_hba *phba)
  12332. {
  12333. return __lpfc_reg_congestion_buf(phba, 1);
  12334. }
  12335. /**
  12336. * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
  12337. * @phba: Pointer to HBA context object.
  12338. * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
  12339. *
  12340. * This function is called in the SLI4 code path to read the port's
  12341. * sli4 capabilities.
  12342. *
  12343. * This function may be be called from any context that can block-wait
  12344. * for the completion. The expectation is that this routine is called
  12345. * typically from probe_one or from the online routine.
  12346. **/
  12347. int
  12348. lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  12349. {
  12350. int rc;
  12351. struct lpfc_mqe *mqe = &mboxq->u.mqe;
  12352. struct lpfc_pc_sli4_params *sli4_params;
  12353. uint32_t mbox_tmo;
  12354. int length;
  12355. bool exp_wqcq_pages = true;
  12356. struct lpfc_sli4_parameters *mbx_sli4_parameters;
  12357. /*
  12358. * By default, the driver assumes the SLI4 port requires RPI
  12359. * header postings. The SLI4_PARAM response will correct this
  12360. * assumption.
  12361. */
  12362. phba->sli4_hba.rpi_hdrs_in_use = 1;
  12363. /* Read the port's SLI4 Config Parameters */
  12364. length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
  12365. sizeof(struct lpfc_sli4_cfg_mhdr));
  12366. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  12367. LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
  12368. length, LPFC_SLI4_MBX_EMBED);
  12369. if (!phba->sli4_hba.intr_enable)
  12370. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  12371. else {
  12372. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  12373. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  12374. }
  12375. if (unlikely(rc))
  12376. return rc;
  12377. sli4_params = &phba->sli4_hba.pc_sli4_params;
  12378. mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
  12379. sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
  12380. sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
  12381. sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
  12382. sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
  12383. mbx_sli4_parameters);
  12384. sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
  12385. mbx_sli4_parameters);
  12386. if (bf_get(cfg_phwq, mbx_sli4_parameters))
  12387. phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
  12388. else
  12389. phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
  12390. sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
  12391. sli4_params->loopbk_scope = bf_get(cfg_loopbk_scope,
  12392. mbx_sli4_parameters);
  12393. sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
  12394. sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
  12395. sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
  12396. sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
  12397. sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
  12398. sli4_params->eqav = bf_get(cfg_eqav, mbx_sli4_parameters);
  12399. sli4_params->cqav = bf_get(cfg_cqav, mbx_sli4_parameters);
  12400. sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
  12401. sli4_params->bv1s = bf_get(cfg_bv1s, mbx_sli4_parameters);
  12402. sli4_params->pls = bf_get(cfg_pvl, mbx_sli4_parameters);
  12403. sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
  12404. mbx_sli4_parameters);
  12405. sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
  12406. sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
  12407. mbx_sli4_parameters);
  12408. phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
  12409. phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
  12410. sli4_params->mi_cap = bf_get(cfg_mi_ver, mbx_sli4_parameters);
  12411. /* Check for Extended Pre-Registered SGL support */
  12412. phba->cfg_xpsgl = bf_get(cfg_xpsgl, mbx_sli4_parameters);
  12413. /* Check for firmware nvme support */
  12414. rc = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
  12415. bf_get(cfg_xib, mbx_sli4_parameters));
  12416. if (rc) {
  12417. /* Save this to indicate the Firmware supports NVME */
  12418. sli4_params->nvme = 1;
  12419. /* Firmware NVME support, check driver FC4 NVME support */
  12420. if (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) {
  12421. lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
  12422. "6133 Disabling NVME support: "
  12423. "FC4 type not supported: x%x\n",
  12424. phba->cfg_enable_fc4_type);
  12425. goto fcponly;
  12426. }
  12427. } else {
  12428. /* No firmware NVME support, check driver FC4 NVME support */
  12429. sli4_params->nvme = 0;
  12430. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  12431. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
  12432. "6101 Disabling NVME support: Not "
  12433. "supported by firmware (%d %d) x%x\n",
  12434. bf_get(cfg_nvme, mbx_sli4_parameters),
  12435. bf_get(cfg_xib, mbx_sli4_parameters),
  12436. phba->cfg_enable_fc4_type);
  12437. fcponly:
  12438. phba->nvmet_support = 0;
  12439. phba->cfg_nvmet_mrq = 0;
  12440. phba->cfg_nvme_seg_cnt = 0;
  12441. /* If no FC4 type support, move to just SCSI support */
  12442. if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
  12443. return -ENODEV;
  12444. phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
  12445. }
  12446. }
  12447. /* If the NVME FC4 type is enabled, scale the sg_seg_cnt to
  12448. * accommodate 512K and 1M IOs in a single nvme buf.
  12449. */
  12450. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
  12451. phba->cfg_sg_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
  12452. /* Enable embedded Payload BDE if support is indicated */
  12453. if (bf_get(cfg_pbde, mbx_sli4_parameters))
  12454. phba->cfg_enable_pbde = 1;
  12455. else
  12456. phba->cfg_enable_pbde = 0;
  12457. /*
  12458. * To support Suppress Response feature we must satisfy 3 conditions.
  12459. * lpfc_suppress_rsp module parameter must be set (default).
  12460. * In SLI4-Parameters Descriptor:
  12461. * Extended Inline Buffers (XIB) must be supported.
  12462. * Suppress Response IU Not Supported (SRIUNS) must NOT be supported
  12463. * (double negative).
  12464. */
  12465. if (phba->cfg_suppress_rsp && bf_get(cfg_xib, mbx_sli4_parameters) &&
  12466. !(bf_get(cfg_nosr, mbx_sli4_parameters)))
  12467. phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
  12468. else
  12469. phba->cfg_suppress_rsp = 0;
  12470. if (bf_get(cfg_eqdr, mbx_sli4_parameters))
  12471. phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
  12472. /* Make sure that sge_supp_len can be handled by the driver */
  12473. if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
  12474. sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
  12475. dma_set_max_seg_size(&phba->pcidev->dev, sli4_params->sge_supp_len);
  12476. /*
  12477. * Check whether the adapter supports an embedded copy of the
  12478. * FCP CMD IU within the WQE for FCP_Ixxx commands. In order
  12479. * to use this option, 128-byte WQEs must be used.
  12480. */
  12481. if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
  12482. phba->fcp_embed_io = 1;
  12483. else
  12484. phba->fcp_embed_io = 0;
  12485. lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
  12486. "6422 XIB %d PBDE %d: FCP %d NVME %d %d %d\n",
  12487. bf_get(cfg_xib, mbx_sli4_parameters),
  12488. phba->cfg_enable_pbde,
  12489. phba->fcp_embed_io, sli4_params->nvme,
  12490. phba->cfg_nvme_embed_cmd, phba->cfg_suppress_rsp);
  12491. if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
  12492. LPFC_SLI_INTF_IF_TYPE_2) &&
  12493. (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
  12494. LPFC_SLI_INTF_FAMILY_LNCR_A0))
  12495. exp_wqcq_pages = false;
  12496. if ((bf_get(cfg_cqpsize, mbx_sli4_parameters) & LPFC_CQ_16K_PAGE_SZ) &&
  12497. (bf_get(cfg_wqpsize, mbx_sli4_parameters) & LPFC_WQ_16K_PAGE_SZ) &&
  12498. exp_wqcq_pages &&
  12499. (sli4_params->wqsize & LPFC_WQ_SZ128_SUPPORT))
  12500. phba->enab_exp_wqcq_pages = 1;
  12501. else
  12502. phba->enab_exp_wqcq_pages = 0;
  12503. /*
  12504. * Check if the SLI port supports MDS Diagnostics
  12505. */
  12506. if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
  12507. phba->mds_diags_support = 1;
  12508. else
  12509. phba->mds_diags_support = 0;
  12510. /*
  12511. * Check if the SLI port supports NSLER
  12512. */
  12513. if (bf_get(cfg_nsler, mbx_sli4_parameters))
  12514. phba->nsler = 1;
  12515. else
  12516. phba->nsler = 0;
  12517. return 0;
  12518. }
  12519. /**
  12520. * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
  12521. * @pdev: pointer to PCI device
  12522. * @pid: pointer to PCI device identifier
  12523. *
  12524. * This routine is to be called to attach a device with SLI-3 interface spec
  12525. * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
  12526. * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
  12527. * information of the device and driver to see if the driver state that it can
  12528. * support this kind of device. If the match is successful, the driver core
  12529. * invokes this routine. If this routine determines it can claim the HBA, it
  12530. * does all the initialization that it needs to do to handle the HBA properly.
  12531. *
  12532. * Return code
  12533. * 0 - driver can claim the device
  12534. * negative value - driver can not claim the device
  12535. **/
  12536. static int
  12537. lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
  12538. {
  12539. struct lpfc_hba *phba;
  12540. struct lpfc_vport *vport = NULL;
  12541. struct Scsi_Host *shost = NULL;
  12542. int error;
  12543. uint32_t cfg_mode, intr_mode;
  12544. /* Allocate memory for HBA structure */
  12545. phba = lpfc_hba_alloc(pdev);
  12546. if (!phba)
  12547. return -ENOMEM;
  12548. /* Perform generic PCI device enabling operation */
  12549. error = lpfc_enable_pci_dev(phba);
  12550. if (error)
  12551. goto out_free_phba;
  12552. /* Set up SLI API function jump table for PCI-device group-0 HBAs */
  12553. error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
  12554. if (error)
  12555. goto out_disable_pci_dev;
  12556. /* Set up SLI-3 specific device PCI memory space */
  12557. error = lpfc_sli_pci_mem_setup(phba);
  12558. if (error) {
  12559. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12560. "1402 Failed to set up pci memory space.\n");
  12561. goto out_disable_pci_dev;
  12562. }
  12563. /* Set up SLI-3 specific device driver resources */
  12564. error = lpfc_sli_driver_resource_setup(phba);
  12565. if (error) {
  12566. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12567. "1404 Failed to set up driver resource.\n");
  12568. goto out_unset_pci_mem_s3;
  12569. }
  12570. /* Initialize and populate the iocb list per host */
  12571. error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
  12572. if (error) {
  12573. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12574. "1405 Failed to initialize iocb list.\n");
  12575. goto out_unset_driver_resource_s3;
  12576. }
  12577. /* Set up common device driver resources */
  12578. error = lpfc_setup_driver_resource_phase2(phba);
  12579. if (error) {
  12580. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12581. "1406 Failed to set up driver resource.\n");
  12582. goto out_free_iocb_list;
  12583. }
  12584. /* Get the default values for Model Name and Description */
  12585. lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
  12586. /* Create SCSI host to the physical port */
  12587. error = lpfc_create_shost(phba);
  12588. if (error) {
  12589. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12590. "1407 Failed to create scsi host.\n");
  12591. goto out_unset_driver_resource;
  12592. }
  12593. /* Configure sysfs attributes */
  12594. vport = phba->pport;
  12595. error = lpfc_alloc_sysfs_attr(vport);
  12596. if (error) {
  12597. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12598. "1476 Failed to allocate sysfs attr\n");
  12599. goto out_destroy_shost;
  12600. }
  12601. shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
  12602. /* Now, trying to enable interrupt and bring up the device */
  12603. cfg_mode = phba->cfg_use_msi;
  12604. while (true) {
  12605. /* Put device to a known state before enabling interrupt */
  12606. lpfc_stop_port(phba);
  12607. /* Configure and enable interrupt */
  12608. intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
  12609. if (intr_mode == LPFC_INTR_ERROR) {
  12610. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  12611. "0431 Failed to enable interrupt.\n");
  12612. error = -ENODEV;
  12613. goto out_free_sysfs_attr;
  12614. }
  12615. /* SLI-3 HBA setup */
  12616. if (lpfc_sli_hba_setup(phba)) {
  12617. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  12618. "1477 Failed to set up hba\n");
  12619. error = -ENODEV;
  12620. goto out_remove_device;
  12621. }
  12622. /* Wait 50ms for the interrupts of previous mailbox commands */
  12623. msleep(50);
  12624. /* Check active interrupts on message signaled interrupts */
  12625. if (intr_mode == 0 ||
  12626. phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
  12627. /* Log the current active interrupt mode */
  12628. phba->intr_mode = intr_mode;
  12629. lpfc_log_intr_mode(phba, intr_mode);
  12630. break;
  12631. } else {
  12632. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12633. "0447 Configure interrupt mode (%d) "
  12634. "failed active interrupt test.\n",
  12635. intr_mode);
  12636. /* Disable the current interrupt mode */
  12637. lpfc_sli_disable_intr(phba);
  12638. /* Try next level of interrupt mode */
  12639. cfg_mode = --intr_mode;
  12640. }
  12641. }
  12642. /* Perform post initialization setup */
  12643. lpfc_post_init_setup(phba);
  12644. /* Check if there are static vports to be created. */
  12645. lpfc_create_static_vport(phba);
  12646. return 0;
  12647. out_remove_device:
  12648. lpfc_unset_hba(phba);
  12649. out_free_sysfs_attr:
  12650. lpfc_free_sysfs_attr(vport);
  12651. out_destroy_shost:
  12652. lpfc_destroy_shost(phba);
  12653. out_unset_driver_resource:
  12654. lpfc_unset_driver_resource_phase2(phba);
  12655. out_free_iocb_list:
  12656. lpfc_free_iocb_list(phba);
  12657. out_unset_driver_resource_s3:
  12658. lpfc_sli_driver_resource_unset(phba);
  12659. out_unset_pci_mem_s3:
  12660. lpfc_sli_pci_mem_unset(phba);
  12661. out_disable_pci_dev:
  12662. lpfc_disable_pci_dev(phba);
  12663. if (shost)
  12664. scsi_host_put(shost);
  12665. out_free_phba:
  12666. lpfc_hba_free(phba);
  12667. return error;
  12668. }
  12669. /**
  12670. * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
  12671. * @pdev: pointer to PCI device
  12672. *
  12673. * This routine is to be called to disattach a device with SLI-3 interface
  12674. * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
  12675. * removed from PCI bus, it performs all the necessary cleanup for the HBA
  12676. * device to be removed from the PCI subsystem properly.
  12677. **/
  12678. static void
  12679. lpfc_pci_remove_one_s3(struct pci_dev *pdev)
  12680. {
  12681. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  12682. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  12683. struct lpfc_vport **vports;
  12684. struct lpfc_hba *phba = vport->phba;
  12685. int i;
  12686. set_bit(FC_UNLOADING, &vport->load_flag);
  12687. lpfc_free_sysfs_attr(vport);
  12688. /* Release all the vports against this physical port */
  12689. vports = lpfc_create_vport_work_array(phba);
  12690. if (vports != NULL)
  12691. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  12692. if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
  12693. continue;
  12694. fc_vport_terminate(vports[i]->fc_vport);
  12695. }
  12696. lpfc_destroy_vport_work_array(phba, vports);
  12697. /* Remove FC host with the physical port */
  12698. fc_remove_host(shost);
  12699. scsi_remove_host(shost);
  12700. /* Clean up all nodes, mailboxes and IOs. */
  12701. lpfc_cleanup(vport);
  12702. /*
  12703. * Bring down the SLI Layer. This step disable all interrupts,
  12704. * clears the rings, discards all mailbox commands, and resets
  12705. * the HBA.
  12706. */
  12707. /* HBA interrupt will be disabled after this call */
  12708. lpfc_sli_hba_down(phba);
  12709. /* Stop kthread signal shall trigger work_done one more time */
  12710. kthread_stop(phba->worker_thread);
  12711. /* Final cleanup of txcmplq and reset the HBA */
  12712. lpfc_sli_brdrestart(phba);
  12713. kfree(phba->vpi_bmask);
  12714. kfree(phba->vpi_ids);
  12715. lpfc_stop_hba_timers(phba);
  12716. spin_lock_irq(&phba->port_list_lock);
  12717. list_del_init(&vport->listentry);
  12718. spin_unlock_irq(&phba->port_list_lock);
  12719. lpfc_debugfs_terminate(vport);
  12720. /* Disable SR-IOV if enabled */
  12721. if (phba->cfg_sriov_nr_virtfn)
  12722. pci_disable_sriov(pdev);
  12723. /* Disable interrupt */
  12724. lpfc_sli_disable_intr(phba);
  12725. scsi_host_put(shost);
  12726. /*
  12727. * Call scsi_free before mem_free since scsi bufs are released to their
  12728. * corresponding pools here.
  12729. */
  12730. lpfc_scsi_free(phba);
  12731. lpfc_free_iocb_list(phba);
  12732. lpfc_mem_free_all(phba);
  12733. dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
  12734. phba->hbqslimp.virt, phba->hbqslimp.phys);
  12735. /* Free resources associated with SLI2 interface */
  12736. dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
  12737. phba->slim2p.virt, phba->slim2p.phys);
  12738. /* unmap adapter SLIM and Control Registers */
  12739. iounmap(phba->ctrl_regs_memmap_p);
  12740. iounmap(phba->slim_memmap_p);
  12741. lpfc_hba_free(phba);
  12742. pci_release_mem_regions(pdev);
  12743. pci_disable_device(pdev);
  12744. }
  12745. /**
  12746. * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
  12747. * @dev_d: pointer to device
  12748. *
  12749. * This routine is to be called from the kernel's PCI subsystem to support
  12750. * system Power Management (PM) to device with SLI-3 interface spec. When
  12751. * PM invokes this method, it quiesces the device by stopping the driver's
  12752. * worker thread for the device, turning off device's interrupt and DMA,
  12753. * and bring the device offline. Note that as the driver implements the
  12754. * minimum PM requirements to a power-aware driver's PM support for the
  12755. * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
  12756. * to the suspend() method call will be treated as SUSPEND and the driver will
  12757. * fully reinitialize its device during resume() method call, the driver will
  12758. * set device to PCI_D3hot state in PCI config space instead of setting it
  12759. * according to the @msg provided by the PM.
  12760. *
  12761. * Return code
  12762. * 0 - driver suspended the device
  12763. * Error otherwise
  12764. **/
  12765. static int __maybe_unused
  12766. lpfc_pci_suspend_one_s3(struct device *dev_d)
  12767. {
  12768. struct Scsi_Host *shost = dev_get_drvdata(dev_d);
  12769. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12770. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12771. "0473 PCI device Power Management suspend.\n");
  12772. /* Bring down the device */
  12773. lpfc_offline_prep(phba, LPFC_MBX_WAIT);
  12774. lpfc_offline(phba);
  12775. kthread_stop(phba->worker_thread);
  12776. /* Disable interrupt from device */
  12777. lpfc_sli_disable_intr(phba);
  12778. return 0;
  12779. }
  12780. /**
  12781. * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
  12782. * @dev_d: pointer to device
  12783. *
  12784. * This routine is to be called from the kernel's PCI subsystem to support
  12785. * system Power Management (PM) to device with SLI-3 interface spec. When PM
  12786. * invokes this method, it restores the device's PCI config space state and
  12787. * fully reinitializes the device and brings it online. Note that as the
  12788. * driver implements the minimum PM requirements to a power-aware driver's
  12789. * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
  12790. * FREEZE) to the suspend() method call will be treated as SUSPEND and the
  12791. * driver will fully reinitialize its device during resume() method call,
  12792. * the device will be set to PCI_D0 directly in PCI config space before
  12793. * restoring the state.
  12794. *
  12795. * Return code
  12796. * 0 - driver suspended the device
  12797. * Error otherwise
  12798. **/
  12799. static int __maybe_unused
  12800. lpfc_pci_resume_one_s3(struct device *dev_d)
  12801. {
  12802. struct Scsi_Host *shost = dev_get_drvdata(dev_d);
  12803. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12804. uint32_t intr_mode;
  12805. int error;
  12806. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12807. "0452 PCI device Power Management resume.\n");
  12808. /* Startup the kernel thread for this host adapter. */
  12809. phba->worker_thread = kthread_run(lpfc_do_work, phba,
  12810. "lpfc_worker_%d", phba->brd_no);
  12811. if (IS_ERR(phba->worker_thread)) {
  12812. error = PTR_ERR(phba->worker_thread);
  12813. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12814. "0434 PM resume failed to start worker "
  12815. "thread: error=x%x.\n", error);
  12816. return error;
  12817. }
  12818. /* Init cpu_map array */
  12819. lpfc_cpu_map_array_init(phba);
  12820. /* Init hba_eq_hdl array */
  12821. lpfc_hba_eq_hdl_array_init(phba);
  12822. /* Configure and enable interrupt */
  12823. intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
  12824. if (intr_mode == LPFC_INTR_ERROR) {
  12825. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  12826. "0430 PM resume Failed to enable interrupt\n");
  12827. return -EIO;
  12828. } else
  12829. phba->intr_mode = intr_mode;
  12830. /* Restart HBA and bring it online */
  12831. lpfc_sli_brdrestart(phba);
  12832. lpfc_online(phba);
  12833. /* Log the current active interrupt mode */
  12834. lpfc_log_intr_mode(phba, phba->intr_mode);
  12835. return 0;
  12836. }
  12837. /**
  12838. * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
  12839. * @phba: pointer to lpfc hba data structure.
  12840. *
  12841. * This routine is called to prepare the SLI3 device for PCI slot recover. It
  12842. * aborts all the outstanding SCSI I/Os to the pci device.
  12843. **/
  12844. static void
  12845. lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
  12846. {
  12847. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  12848. "2723 PCI channel I/O abort preparing for recovery\n");
  12849. /*
  12850. * There may be errored I/Os through HBA, abort all I/Os on txcmplq
  12851. * and let the SCSI mid-layer to retry them to recover.
  12852. */
  12853. lpfc_sli_abort_fcp_rings(phba);
  12854. }
  12855. /**
  12856. * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
  12857. * @phba: pointer to lpfc hba data structure.
  12858. *
  12859. * This routine is called to prepare the SLI3 device for PCI slot reset. It
  12860. * disables the device interrupt and pci device, and aborts the internal FCP
  12861. * pending I/Os.
  12862. **/
  12863. static void
  12864. lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
  12865. {
  12866. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  12867. "2710 PCI channel disable preparing for reset\n");
  12868. /* Block any management I/Os to the device */
  12869. lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
  12870. /* Block all SCSI devices' I/Os on the host */
  12871. lpfc_scsi_dev_block(phba);
  12872. /* Flush all driver's outstanding SCSI I/Os as we are to reset */
  12873. lpfc_sli_flush_io_rings(phba);
  12874. /* stop all timers */
  12875. lpfc_stop_hba_timers(phba);
  12876. /* Disable interrupt and pci device */
  12877. lpfc_sli_disable_intr(phba);
  12878. pci_disable_device(phba->pcidev);
  12879. }
  12880. /**
  12881. * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
  12882. * @phba: pointer to lpfc hba data structure.
  12883. *
  12884. * This routine is called to prepare the SLI3 device for PCI slot permanently
  12885. * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
  12886. * pending I/Os.
  12887. **/
  12888. static void
  12889. lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
  12890. {
  12891. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  12892. "2711 PCI channel permanent disable for failure\n");
  12893. /* Block all SCSI devices' I/Os on the host */
  12894. lpfc_scsi_dev_block(phba);
  12895. lpfc_sli4_prep_dev_for_reset(phba);
  12896. /* stop all timers */
  12897. lpfc_stop_hba_timers(phba);
  12898. /* Clean up all driver's outstanding SCSI I/Os */
  12899. lpfc_sli_flush_io_rings(phba);
  12900. }
  12901. /**
  12902. * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
  12903. * @pdev: pointer to PCI device.
  12904. * @state: the current PCI connection state.
  12905. *
  12906. * This routine is called from the PCI subsystem for I/O error handling to
  12907. * device with SLI-3 interface spec. This function is called by the PCI
  12908. * subsystem after a PCI bus error affecting this device has been detected.
  12909. * When this function is invoked, it will need to stop all the I/Os and
  12910. * interrupt(s) to the device. Once that is done, it will return
  12911. * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
  12912. * as desired.
  12913. *
  12914. * Return codes
  12915. * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
  12916. * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
  12917. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  12918. **/
  12919. static pci_ers_result_t
  12920. lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
  12921. {
  12922. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  12923. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12924. switch (state) {
  12925. case pci_channel_io_normal:
  12926. /* Non-fatal error, prepare for recovery */
  12927. lpfc_sli_prep_dev_for_recover(phba);
  12928. return PCI_ERS_RESULT_CAN_RECOVER;
  12929. case pci_channel_io_frozen:
  12930. /* Fatal error, prepare for slot reset */
  12931. lpfc_sli_prep_dev_for_reset(phba);
  12932. return PCI_ERS_RESULT_NEED_RESET;
  12933. case pci_channel_io_perm_failure:
  12934. /* Permanent failure, prepare for device down */
  12935. lpfc_sli_prep_dev_for_perm_failure(phba);
  12936. return PCI_ERS_RESULT_DISCONNECT;
  12937. default:
  12938. /* Unknown state, prepare and request slot reset */
  12939. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  12940. "0472 Unknown PCI error state: x%x\n", state);
  12941. lpfc_sli_prep_dev_for_reset(phba);
  12942. return PCI_ERS_RESULT_NEED_RESET;
  12943. }
  12944. }
  12945. /**
  12946. * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
  12947. * @pdev: pointer to PCI device.
  12948. *
  12949. * This routine is called from the PCI subsystem for error handling to
  12950. * device with SLI-3 interface spec. This is called after PCI bus has been
  12951. * reset to restart the PCI card from scratch, as if from a cold-boot.
  12952. * During the PCI subsystem error recovery, after driver returns
  12953. * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
  12954. * recovery and then call this routine before calling the .resume method
  12955. * to recover the device. This function will initialize the HBA device,
  12956. * enable the interrupt, but it will just put the HBA to offline state
  12957. * without passing any I/O traffic.
  12958. *
  12959. * Return codes
  12960. * PCI_ERS_RESULT_RECOVERED - the device has been recovered
  12961. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  12962. */
  12963. static pci_ers_result_t
  12964. lpfc_io_slot_reset_s3(struct pci_dev *pdev)
  12965. {
  12966. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  12967. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  12968. struct lpfc_sli *psli = &phba->sli;
  12969. uint32_t intr_mode;
  12970. dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
  12971. if (pci_enable_device_mem(pdev)) {
  12972. printk(KERN_ERR "lpfc: Cannot re-enable "
  12973. "PCI device after reset.\n");
  12974. return PCI_ERS_RESULT_DISCONNECT;
  12975. }
  12976. pci_restore_state(pdev);
  12977. /*
  12978. * As the new kernel behavior of pci_restore_state() API call clears
  12979. * device saved_state flag, need to save the restored state again.
  12980. */
  12981. pci_save_state(pdev);
  12982. if (pdev->is_busmaster)
  12983. pci_set_master(pdev);
  12984. spin_lock_irq(&phba->hbalock);
  12985. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  12986. spin_unlock_irq(&phba->hbalock);
  12987. /* Configure and enable interrupt */
  12988. intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
  12989. if (intr_mode == LPFC_INTR_ERROR) {
  12990. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  12991. "0427 Cannot re-enable interrupt after "
  12992. "slot reset.\n");
  12993. return PCI_ERS_RESULT_DISCONNECT;
  12994. } else
  12995. phba->intr_mode = intr_mode;
  12996. /* Take device offline, it will perform cleanup */
  12997. lpfc_offline_prep(phba, LPFC_MBX_WAIT);
  12998. lpfc_offline(phba);
  12999. lpfc_sli_brdrestart(phba);
  13000. /* Log the current active interrupt mode */
  13001. lpfc_log_intr_mode(phba, phba->intr_mode);
  13002. return PCI_ERS_RESULT_RECOVERED;
  13003. }
  13004. /**
  13005. * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
  13006. * @pdev: pointer to PCI device
  13007. *
  13008. * This routine is called from the PCI subsystem for error handling to device
  13009. * with SLI-3 interface spec. It is called when kernel error recovery tells
  13010. * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
  13011. * error recovery. After this call, traffic can start to flow from this device
  13012. * again.
  13013. */
  13014. static void
  13015. lpfc_io_resume_s3(struct pci_dev *pdev)
  13016. {
  13017. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  13018. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  13019. /* Bring device online, it will be no-op for non-fatal error resume */
  13020. lpfc_online(phba);
  13021. }
  13022. /**
  13023. * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
  13024. * @phba: pointer to lpfc hba data structure.
  13025. *
  13026. * returns the number of ELS/CT IOCBs to reserve
  13027. **/
  13028. int
  13029. lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
  13030. {
  13031. int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
  13032. if (phba->sli_rev == LPFC_SLI_REV4) {
  13033. if (max_xri <= 100)
  13034. return 10;
  13035. else if (max_xri <= 256)
  13036. return 25;
  13037. else if (max_xri <= 512)
  13038. return 50;
  13039. else if (max_xri <= 1024)
  13040. return 100;
  13041. else if (max_xri <= 1536)
  13042. return 150;
  13043. else if (max_xri <= 2048)
  13044. return 200;
  13045. else
  13046. return 250;
  13047. } else
  13048. return 0;
  13049. }
  13050. /**
  13051. * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
  13052. * @phba: pointer to lpfc hba data structure.
  13053. *
  13054. * returns the number of ELS/CT + NVMET IOCBs to reserve
  13055. **/
  13056. int
  13057. lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
  13058. {
  13059. int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
  13060. if (phba->nvmet_support)
  13061. max_xri += LPFC_NVMET_BUF_POST;
  13062. return max_xri;
  13063. }
  13064. static int
  13065. lpfc_log_write_firmware_error(struct lpfc_hba *phba, uint32_t offset,
  13066. uint32_t magic_number, uint32_t ftype, uint32_t fid, uint32_t fsize,
  13067. const struct firmware *fw)
  13068. {
  13069. int rc;
  13070. u8 sli_family;
  13071. sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
  13072. /* Three cases: (1) FW was not supported on the detected adapter.
  13073. * (2) FW update has been locked out administratively.
  13074. * (3) Some other error during FW update.
  13075. * In each case, an unmaskable message is written to the console
  13076. * for admin diagnosis.
  13077. */
  13078. if (offset == ADD_STATUS_FW_NOT_SUPPORTED ||
  13079. (sli_family == LPFC_SLI_INTF_FAMILY_G6 &&
  13080. magic_number != MAGIC_NUMBER_G6) ||
  13081. (sli_family == LPFC_SLI_INTF_FAMILY_G7 &&
  13082. magic_number != MAGIC_NUMBER_G7) ||
  13083. (sli_family == LPFC_SLI_INTF_FAMILY_G7P &&
  13084. magic_number != MAGIC_NUMBER_G7P)) {
  13085. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13086. "3030 This firmware version is not supported on"
  13087. " this HBA model. Device:%x Magic:%x Type:%x "
  13088. "ID:%x Size %d %zd\n",
  13089. phba->pcidev->device, magic_number, ftype, fid,
  13090. fsize, fw->size);
  13091. rc = -EINVAL;
  13092. } else if (offset == ADD_STATUS_FW_DOWNLOAD_HW_DISABLED) {
  13093. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13094. "3021 Firmware downloads have been prohibited "
  13095. "by a system configuration setting on "
  13096. "Device:%x Magic:%x Type:%x ID:%x Size %d "
  13097. "%zd\n",
  13098. phba->pcidev->device, magic_number, ftype, fid,
  13099. fsize, fw->size);
  13100. rc = -EACCES;
  13101. } else {
  13102. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13103. "3022 FW Download failed. Add Status x%x "
  13104. "Device:%x Magic:%x Type:%x ID:%x Size %d "
  13105. "%zd\n",
  13106. offset, phba->pcidev->device, magic_number,
  13107. ftype, fid, fsize, fw->size);
  13108. rc = -EIO;
  13109. }
  13110. return rc;
  13111. }
  13112. /**
  13113. * lpfc_write_firmware - attempt to write a firmware image to the port
  13114. * @fw: pointer to firmware image returned from request_firmware.
  13115. * @context: pointer to firmware image returned from request_firmware.
  13116. *
  13117. **/
  13118. static void
  13119. lpfc_write_firmware(const struct firmware *fw, void *context)
  13120. {
  13121. struct lpfc_hba *phba = (struct lpfc_hba *)context;
  13122. char fwrev[FW_REV_STR_SIZE];
  13123. struct lpfc_grp_hdr *image;
  13124. struct list_head dma_buffer_list;
  13125. int i, rc = 0;
  13126. struct lpfc_dmabuf *dmabuf, *next;
  13127. uint32_t offset = 0, temp_offset = 0;
  13128. uint32_t magic_number, ftype, fid, fsize;
  13129. /* It can be null in no-wait mode, sanity check */
  13130. if (!fw) {
  13131. rc = -ENXIO;
  13132. goto out;
  13133. }
  13134. image = (struct lpfc_grp_hdr *)fw->data;
  13135. magic_number = be32_to_cpu(image->magic_number);
  13136. ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
  13137. fid = bf_get_be32(lpfc_grp_hdr_id, image);
  13138. fsize = be32_to_cpu(image->size);
  13139. INIT_LIST_HEAD(&dma_buffer_list);
  13140. lpfc_decode_firmware_rev(phba, fwrev, 1);
  13141. if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
  13142. lpfc_log_msg(phba, KERN_NOTICE, LOG_INIT | LOG_SLI,
  13143. "3023 Updating Firmware, Current Version:%s "
  13144. "New Version:%s\n",
  13145. fwrev, image->revision);
  13146. for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
  13147. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
  13148. GFP_KERNEL);
  13149. if (!dmabuf) {
  13150. rc = -ENOMEM;
  13151. goto release_out;
  13152. }
  13153. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  13154. SLI4_PAGE_SIZE,
  13155. &dmabuf->phys,
  13156. GFP_KERNEL);
  13157. if (!dmabuf->virt) {
  13158. kfree(dmabuf);
  13159. rc = -ENOMEM;
  13160. goto release_out;
  13161. }
  13162. list_add_tail(&dmabuf->list, &dma_buffer_list);
  13163. }
  13164. while (offset < fw->size) {
  13165. temp_offset = offset;
  13166. list_for_each_entry(dmabuf, &dma_buffer_list, list) {
  13167. if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
  13168. memcpy(dmabuf->virt,
  13169. fw->data + temp_offset,
  13170. fw->size - temp_offset);
  13171. temp_offset = fw->size;
  13172. break;
  13173. }
  13174. memcpy(dmabuf->virt, fw->data + temp_offset,
  13175. SLI4_PAGE_SIZE);
  13176. temp_offset += SLI4_PAGE_SIZE;
  13177. }
  13178. rc = lpfc_wr_object(phba, &dma_buffer_list,
  13179. (fw->size - offset), &offset);
  13180. if (rc) {
  13181. rc = lpfc_log_write_firmware_error(phba, offset,
  13182. magic_number,
  13183. ftype,
  13184. fid,
  13185. fsize,
  13186. fw);
  13187. goto release_out;
  13188. }
  13189. }
  13190. rc = offset;
  13191. } else
  13192. lpfc_log_msg(phba, KERN_NOTICE, LOG_INIT | LOG_SLI,
  13193. "3029 Skipped Firmware update, Current "
  13194. "Version:%s New Version:%s\n",
  13195. fwrev, image->revision);
  13196. release_out:
  13197. list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
  13198. list_del(&dmabuf->list);
  13199. dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
  13200. dmabuf->virt, dmabuf->phys);
  13201. kfree(dmabuf);
  13202. }
  13203. release_firmware(fw);
  13204. out:
  13205. if (rc < 0)
  13206. lpfc_log_msg(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  13207. "3062 Firmware update error, status %d.\n", rc);
  13208. else
  13209. lpfc_log_msg(phba, KERN_NOTICE, LOG_INIT | LOG_SLI,
  13210. "3024 Firmware update success: size %d.\n", rc);
  13211. }
  13212. /**
  13213. * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
  13214. * @phba: pointer to lpfc hba data structure.
  13215. * @fw_upgrade: which firmware to update.
  13216. *
  13217. * This routine is called to perform Linux generic firmware upgrade on device
  13218. * that supports such feature.
  13219. **/
  13220. int
  13221. lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
  13222. {
  13223. char file_name[ELX_FW_NAME_SIZE] = {0};
  13224. int ret;
  13225. const struct firmware *fw;
  13226. /* Only supported on SLI4 interface type 2 for now */
  13227. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
  13228. LPFC_SLI_INTF_IF_TYPE_2)
  13229. return -EPERM;
  13230. scnprintf(file_name, sizeof(file_name), "%s.grp", phba->ModelName);
  13231. if (fw_upgrade == INT_FW_UPGRADE) {
  13232. ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_UEVENT,
  13233. file_name, &phba->pcidev->dev,
  13234. GFP_KERNEL, (void *)phba,
  13235. lpfc_write_firmware);
  13236. } else if (fw_upgrade == RUN_FW_UPGRADE) {
  13237. ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
  13238. if (!ret)
  13239. lpfc_write_firmware(fw, (void *)phba);
  13240. } else {
  13241. ret = -EINVAL;
  13242. }
  13243. return ret;
  13244. }
  13245. /**
  13246. * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
  13247. * @pdev: pointer to PCI device
  13248. * @pid: pointer to PCI device identifier
  13249. *
  13250. * This routine is called from the kernel's PCI subsystem to device with
  13251. * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
  13252. * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
  13253. * information of the device and driver to see if the driver state that it
  13254. * can support this kind of device. If the match is successful, the driver
  13255. * core invokes this routine. If this routine determines it can claim the HBA,
  13256. * it does all the initialization that it needs to do to handle the HBA
  13257. * properly.
  13258. *
  13259. * Return code
  13260. * 0 - driver can claim the device
  13261. * negative value - driver can not claim the device
  13262. **/
  13263. static int
  13264. lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
  13265. {
  13266. struct lpfc_hba *phba;
  13267. struct lpfc_vport *vport = NULL;
  13268. struct Scsi_Host *shost = NULL;
  13269. int error;
  13270. uint32_t cfg_mode, intr_mode;
  13271. /* Allocate memory for HBA structure */
  13272. phba = lpfc_hba_alloc(pdev);
  13273. if (!phba)
  13274. return -ENOMEM;
  13275. INIT_LIST_HEAD(&phba->poll_list);
  13276. /* Perform generic PCI device enabling operation */
  13277. error = lpfc_enable_pci_dev(phba);
  13278. if (error)
  13279. goto out_free_phba;
  13280. /* Set up SLI API function jump table for PCI-device group-1 HBAs */
  13281. error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
  13282. if (error)
  13283. goto out_disable_pci_dev;
  13284. /* Set up SLI-4 specific device PCI memory space */
  13285. error = lpfc_sli4_pci_mem_setup(phba);
  13286. if (error) {
  13287. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13288. "1410 Failed to set up pci memory space.\n");
  13289. goto out_disable_pci_dev;
  13290. }
  13291. /* Set up SLI-4 Specific device driver resources */
  13292. error = lpfc_sli4_driver_resource_setup(phba);
  13293. if (error) {
  13294. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13295. "1412 Failed to set up driver resource.\n");
  13296. goto out_unset_pci_mem_s4;
  13297. }
  13298. spin_lock_init(&phba->rrq_list_lock);
  13299. INIT_LIST_HEAD(&phba->active_rrq_list);
  13300. INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
  13301. /* Set up common device driver resources */
  13302. error = lpfc_setup_driver_resource_phase2(phba);
  13303. if (error) {
  13304. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13305. "1414 Failed to set up driver resource.\n");
  13306. goto out_unset_driver_resource_s4;
  13307. }
  13308. /* Get the default values for Model Name and Description */
  13309. lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
  13310. /* Now, trying to enable interrupt and bring up the device */
  13311. cfg_mode = phba->cfg_use_msi;
  13312. /* Put device to a known state before enabling interrupt */
  13313. phba->pport = NULL;
  13314. lpfc_stop_port(phba);
  13315. /* Init cpu_map array */
  13316. lpfc_cpu_map_array_init(phba);
  13317. /* Init hba_eq_hdl array */
  13318. lpfc_hba_eq_hdl_array_init(phba);
  13319. /* Configure and enable interrupt */
  13320. intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
  13321. if (intr_mode == LPFC_INTR_ERROR) {
  13322. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13323. "0426 Failed to enable interrupt.\n");
  13324. error = -ENODEV;
  13325. goto out_unset_driver_resource;
  13326. }
  13327. /* Default to single EQ for non-MSI-X */
  13328. if (phba->intr_type != MSIX) {
  13329. phba->cfg_irq_chann = 1;
  13330. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  13331. if (phba->nvmet_support)
  13332. phba->cfg_nvmet_mrq = 1;
  13333. }
  13334. }
  13335. lpfc_cpu_affinity_check(phba, phba->cfg_irq_chann);
  13336. /* Create SCSI host to the physical port */
  13337. error = lpfc_create_shost(phba);
  13338. if (error) {
  13339. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13340. "1415 Failed to create scsi host.\n");
  13341. goto out_disable_intr;
  13342. }
  13343. vport = phba->pport;
  13344. shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
  13345. /* Configure sysfs attributes */
  13346. error = lpfc_alloc_sysfs_attr(vport);
  13347. if (error) {
  13348. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13349. "1416 Failed to allocate sysfs attr\n");
  13350. goto out_destroy_shost;
  13351. }
  13352. /* Set up SLI-4 HBA */
  13353. if (lpfc_sli4_hba_setup(phba)) {
  13354. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13355. "1421 Failed to set up hba\n");
  13356. error = -ENODEV;
  13357. goto out_free_sysfs_attr;
  13358. }
  13359. /* Log the current active interrupt mode */
  13360. phba->intr_mode = intr_mode;
  13361. lpfc_log_intr_mode(phba, intr_mode);
  13362. /* Perform post initialization setup */
  13363. lpfc_post_init_setup(phba);
  13364. /* NVME support in FW earlier in the driver load corrects the
  13365. * FC4 type making a check for nvme_support unnecessary.
  13366. */
  13367. if (phba->nvmet_support == 0) {
  13368. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
  13369. /* Create NVME binding with nvme_fc_transport. This
  13370. * ensures the vport is initialized. If the localport
  13371. * create fails, it should not unload the driver to
  13372. * support field issues.
  13373. */
  13374. error = lpfc_nvme_create_localport(vport);
  13375. if (error) {
  13376. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13377. "6004 NVME registration "
  13378. "failed, error x%x\n",
  13379. error);
  13380. }
  13381. }
  13382. }
  13383. /* check for firmware upgrade or downgrade */
  13384. if (phba->cfg_request_firmware_upgrade)
  13385. lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
  13386. /* Check if there are static vports to be created. */
  13387. lpfc_create_static_vport(phba);
  13388. timer_setup(&phba->cpuhp_poll_timer, lpfc_sli4_poll_hbtimer, 0);
  13389. cpuhp_state_add_instance_nocalls(lpfc_cpuhp_state, &phba->cpuhp);
  13390. return 0;
  13391. out_free_sysfs_attr:
  13392. lpfc_free_sysfs_attr(vport);
  13393. out_destroy_shost:
  13394. lpfc_destroy_shost(phba);
  13395. out_disable_intr:
  13396. lpfc_sli4_disable_intr(phba);
  13397. out_unset_driver_resource:
  13398. lpfc_unset_driver_resource_phase2(phba);
  13399. out_unset_driver_resource_s4:
  13400. lpfc_sli4_driver_resource_unset(phba);
  13401. out_unset_pci_mem_s4:
  13402. lpfc_sli4_pci_mem_unset(phba);
  13403. out_disable_pci_dev:
  13404. lpfc_disable_pci_dev(phba);
  13405. if (shost)
  13406. scsi_host_put(shost);
  13407. out_free_phba:
  13408. lpfc_hba_free(phba);
  13409. return error;
  13410. }
  13411. /**
  13412. * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
  13413. * @pdev: pointer to PCI device
  13414. *
  13415. * This routine is called from the kernel's PCI subsystem to device with
  13416. * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
  13417. * removed from PCI bus, it performs all the necessary cleanup for the HBA
  13418. * device to be removed from the PCI subsystem properly.
  13419. **/
  13420. static void
  13421. lpfc_pci_remove_one_s4(struct pci_dev *pdev)
  13422. {
  13423. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  13424. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  13425. struct lpfc_vport **vports;
  13426. struct lpfc_hba *phba = vport->phba;
  13427. int i;
  13428. /* Mark the device unloading flag */
  13429. set_bit(FC_UNLOADING, &vport->load_flag);
  13430. if (phba->cgn_i)
  13431. lpfc_unreg_congestion_buf(phba);
  13432. lpfc_free_sysfs_attr(vport);
  13433. /* Release all the vports against this physical port */
  13434. vports = lpfc_create_vport_work_array(phba);
  13435. if (vports != NULL)
  13436. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  13437. if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
  13438. continue;
  13439. fc_vport_terminate(vports[i]->fc_vport);
  13440. }
  13441. lpfc_destroy_vport_work_array(phba, vports);
  13442. /* Remove FC host with the physical port */
  13443. fc_remove_host(shost);
  13444. scsi_remove_host(shost);
  13445. /* Perform ndlp cleanup on the physical port. The nvme and nvmet
  13446. * localports are destroyed after to cleanup all transport memory.
  13447. */
  13448. lpfc_cleanup(vport);
  13449. lpfc_nvmet_destroy_targetport(phba);
  13450. lpfc_nvme_destroy_localport(vport);
  13451. /* De-allocate multi-XRI pools */
  13452. if (phba->cfg_xri_rebalancing)
  13453. lpfc_destroy_multixri_pools(phba);
  13454. /*
  13455. * Bring down the SLI Layer. This step disables all interrupts,
  13456. * clears the rings, discards all mailbox commands, and resets
  13457. * the HBA FCoE function.
  13458. */
  13459. lpfc_debugfs_terminate(vport);
  13460. lpfc_stop_hba_timers(phba);
  13461. spin_lock_irq(&phba->port_list_lock);
  13462. list_del_init(&vport->listentry);
  13463. spin_unlock_irq(&phba->port_list_lock);
  13464. /* Perform scsi free before driver resource_unset since scsi
  13465. * buffers are released to their corresponding pools here.
  13466. */
  13467. lpfc_io_free(phba);
  13468. lpfc_free_iocb_list(phba);
  13469. lpfc_sli4_hba_unset(phba);
  13470. lpfc_unset_driver_resource_phase2(phba);
  13471. lpfc_sli4_driver_resource_unset(phba);
  13472. /* Unmap adapter Control and Doorbell registers */
  13473. lpfc_sli4_pci_mem_unset(phba);
  13474. /* Release PCI resources and disable device's PCI function */
  13475. scsi_host_put(shost);
  13476. lpfc_disable_pci_dev(phba);
  13477. /* Finally, free the driver's device data structure */
  13478. lpfc_hba_free(phba);
  13479. return;
  13480. }
  13481. /**
  13482. * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
  13483. * @dev_d: pointer to device
  13484. *
  13485. * This routine is called from the kernel's PCI subsystem to support system
  13486. * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
  13487. * this method, it quiesces the device by stopping the driver's worker
  13488. * thread for the device, turning off device's interrupt and DMA, and bring
  13489. * the device offline. Note that as the driver implements the minimum PM
  13490. * requirements to a power-aware driver's PM support for suspend/resume -- all
  13491. * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
  13492. * method call will be treated as SUSPEND and the driver will fully
  13493. * reinitialize its device during resume() method call, the driver will set
  13494. * device to PCI_D3hot state in PCI config space instead of setting it
  13495. * according to the @msg provided by the PM.
  13496. *
  13497. * Return code
  13498. * 0 - driver suspended the device
  13499. * Error otherwise
  13500. **/
  13501. static int __maybe_unused
  13502. lpfc_pci_suspend_one_s4(struct device *dev_d)
  13503. {
  13504. struct Scsi_Host *shost = dev_get_drvdata(dev_d);
  13505. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  13506. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  13507. "2843 PCI device Power Management suspend.\n");
  13508. /* Bring down the device */
  13509. lpfc_offline_prep(phba, LPFC_MBX_WAIT);
  13510. lpfc_offline(phba);
  13511. kthread_stop(phba->worker_thread);
  13512. /* Disable interrupt from device */
  13513. lpfc_sli4_disable_intr(phba);
  13514. lpfc_sli4_queue_destroy(phba);
  13515. return 0;
  13516. }
  13517. /**
  13518. * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
  13519. * @dev_d: pointer to device
  13520. *
  13521. * This routine is called from the kernel's PCI subsystem to support system
  13522. * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
  13523. * this method, it restores the device's PCI config space state and fully
  13524. * reinitializes the device and brings it online. Note that as the driver
  13525. * implements the minimum PM requirements to a power-aware driver's PM for
  13526. * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
  13527. * to the suspend() method call will be treated as SUSPEND and the driver
  13528. * will fully reinitialize its device during resume() method call, the device
  13529. * will be set to PCI_D0 directly in PCI config space before restoring the
  13530. * state.
  13531. *
  13532. * Return code
  13533. * 0 - driver suspended the device
  13534. * Error otherwise
  13535. **/
  13536. static int __maybe_unused
  13537. lpfc_pci_resume_one_s4(struct device *dev_d)
  13538. {
  13539. struct Scsi_Host *shost = dev_get_drvdata(dev_d);
  13540. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  13541. uint32_t intr_mode;
  13542. int error;
  13543. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  13544. "0292 PCI device Power Management resume.\n");
  13545. /* Startup the kernel thread for this host adapter. */
  13546. phba->worker_thread = kthread_run(lpfc_do_work, phba,
  13547. "lpfc_worker_%d", phba->brd_no);
  13548. if (IS_ERR(phba->worker_thread)) {
  13549. error = PTR_ERR(phba->worker_thread);
  13550. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13551. "0293 PM resume failed to start worker "
  13552. "thread: error=x%x.\n", error);
  13553. return error;
  13554. }
  13555. /* Configure and enable interrupt */
  13556. intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
  13557. if (intr_mode == LPFC_INTR_ERROR) {
  13558. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13559. "0294 PM resume Failed to enable interrupt\n");
  13560. return -EIO;
  13561. } else
  13562. phba->intr_mode = intr_mode;
  13563. /* Restart HBA and bring it online */
  13564. lpfc_sli_brdrestart(phba);
  13565. lpfc_online(phba);
  13566. /* Log the current active interrupt mode */
  13567. lpfc_log_intr_mode(phba, phba->intr_mode);
  13568. return 0;
  13569. }
  13570. /**
  13571. * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
  13572. * @phba: pointer to lpfc hba data structure.
  13573. *
  13574. * This routine is called to prepare the SLI4 device for PCI slot recover. It
  13575. * aborts all the outstanding SCSI I/Os to the pci device.
  13576. **/
  13577. static void
  13578. lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
  13579. {
  13580. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13581. "2828 PCI channel I/O abort preparing for recovery\n");
  13582. /*
  13583. * There may be errored I/Os through HBA, abort all I/Os on txcmplq
  13584. * and let the SCSI mid-layer to retry them to recover.
  13585. */
  13586. lpfc_sli_abort_fcp_rings(phba);
  13587. }
  13588. /**
  13589. * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
  13590. * @phba: pointer to lpfc hba data structure.
  13591. *
  13592. * This routine is called to prepare the SLI4 device for PCI slot reset. It
  13593. * disables the device interrupt and pci device, and aborts the internal FCP
  13594. * pending I/Os.
  13595. **/
  13596. static void
  13597. lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
  13598. {
  13599. int offline = pci_channel_offline(phba->pcidev);
  13600. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13601. "2826 PCI channel disable preparing for reset offline"
  13602. " %d\n", offline);
  13603. /* Block any management I/Os to the device */
  13604. lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
  13605. /* HBA_PCI_ERR was set in io_error_detect */
  13606. lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
  13607. /* Flush all driver's outstanding I/Os as we are to reset */
  13608. lpfc_sli_flush_io_rings(phba);
  13609. lpfc_offline(phba);
  13610. /* stop all timers */
  13611. lpfc_stop_hba_timers(phba);
  13612. lpfc_sli4_queue_destroy(phba);
  13613. /* Disable interrupt and pci device */
  13614. lpfc_sli4_disable_intr(phba);
  13615. pci_disable_device(phba->pcidev);
  13616. }
  13617. /**
  13618. * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
  13619. * @phba: pointer to lpfc hba data structure.
  13620. *
  13621. * This routine is called to prepare the SLI4 device for PCI slot permanently
  13622. * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
  13623. * pending I/Os.
  13624. **/
  13625. static void
  13626. lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
  13627. {
  13628. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13629. "2827 PCI channel permanent disable for failure\n");
  13630. /* Block all SCSI devices' I/Os on the host */
  13631. lpfc_scsi_dev_block(phba);
  13632. /* stop all timers */
  13633. lpfc_stop_hba_timers(phba);
  13634. /* Clean up all driver's outstanding I/Os */
  13635. lpfc_sli_flush_io_rings(phba);
  13636. }
  13637. /**
  13638. * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
  13639. * @pdev: pointer to PCI device.
  13640. * @state: the current PCI connection state.
  13641. *
  13642. * This routine is called from the PCI subsystem for error handling to device
  13643. * with SLI-4 interface spec. This function is called by the PCI subsystem
  13644. * after a PCI bus error affecting this device has been detected. When this
  13645. * function is invoked, it will need to stop all the I/Os and interrupt(s)
  13646. * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
  13647. * for the PCI subsystem to perform proper recovery as desired.
  13648. *
  13649. * Return codes
  13650. * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
  13651. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  13652. **/
  13653. static pci_ers_result_t
  13654. lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
  13655. {
  13656. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  13657. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  13658. bool hba_pci_err;
  13659. switch (state) {
  13660. case pci_channel_io_normal:
  13661. /* Non-fatal error, prepare for recovery */
  13662. lpfc_sli4_prep_dev_for_recover(phba);
  13663. return PCI_ERS_RESULT_CAN_RECOVER;
  13664. case pci_channel_io_frozen:
  13665. hba_pci_err = test_and_set_bit(HBA_PCI_ERR, &phba->bit_flags);
  13666. /* Fatal error, prepare for slot reset */
  13667. if (!hba_pci_err)
  13668. lpfc_sli4_prep_dev_for_reset(phba);
  13669. else
  13670. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13671. "2832 Already handling PCI error "
  13672. "state: x%x\n", state);
  13673. return PCI_ERS_RESULT_NEED_RESET;
  13674. case pci_channel_io_perm_failure:
  13675. set_bit(HBA_PCI_ERR, &phba->bit_flags);
  13676. /* Permanent failure, prepare for device down */
  13677. lpfc_sli4_prep_dev_for_perm_failure(phba);
  13678. return PCI_ERS_RESULT_DISCONNECT;
  13679. default:
  13680. hba_pci_err = test_and_set_bit(HBA_PCI_ERR, &phba->bit_flags);
  13681. if (!hba_pci_err)
  13682. lpfc_sli4_prep_dev_for_reset(phba);
  13683. /* Unknown state, prepare and request slot reset */
  13684. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13685. "2825 Unknown PCI error state: x%x\n", state);
  13686. lpfc_sli4_prep_dev_for_reset(phba);
  13687. return PCI_ERS_RESULT_NEED_RESET;
  13688. }
  13689. }
  13690. /**
  13691. * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
  13692. * @pdev: pointer to PCI device.
  13693. *
  13694. * This routine is called from the PCI subsystem for error handling to device
  13695. * with SLI-4 interface spec. It is called after PCI bus has been reset to
  13696. * restart the PCI card from scratch, as if from a cold-boot. During the
  13697. * PCI subsystem error recovery, after the driver returns
  13698. * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
  13699. * recovery and then call this routine before calling the .resume method to
  13700. * recover the device. This function will initialize the HBA device, enable
  13701. * the interrupt, but it will just put the HBA to offline state without
  13702. * passing any I/O traffic.
  13703. *
  13704. * Return codes
  13705. * PCI_ERS_RESULT_RECOVERED - the device has been recovered
  13706. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  13707. */
  13708. static pci_ers_result_t
  13709. lpfc_io_slot_reset_s4(struct pci_dev *pdev)
  13710. {
  13711. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  13712. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  13713. struct lpfc_sli *psli = &phba->sli;
  13714. uint32_t intr_mode;
  13715. bool hba_pci_err;
  13716. dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
  13717. if (pci_enable_device_mem(pdev)) {
  13718. printk(KERN_ERR "lpfc: Cannot re-enable "
  13719. "PCI device after reset.\n");
  13720. return PCI_ERS_RESULT_DISCONNECT;
  13721. }
  13722. pci_restore_state(pdev);
  13723. hba_pci_err = test_and_clear_bit(HBA_PCI_ERR, &phba->bit_flags);
  13724. if (!hba_pci_err)
  13725. dev_info(&pdev->dev,
  13726. "hba_pci_err was not set, recovering slot reset.\n");
  13727. /*
  13728. * As the new kernel behavior of pci_restore_state() API call clears
  13729. * device saved_state flag, need to save the restored state again.
  13730. */
  13731. pci_save_state(pdev);
  13732. if (pdev->is_busmaster)
  13733. pci_set_master(pdev);
  13734. spin_lock_irq(&phba->hbalock);
  13735. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  13736. spin_unlock_irq(&phba->hbalock);
  13737. /* Init cpu_map array */
  13738. lpfc_cpu_map_array_init(phba);
  13739. /* Configure and enable interrupt */
  13740. intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
  13741. if (intr_mode == LPFC_INTR_ERROR) {
  13742. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13743. "2824 Cannot re-enable interrupt after "
  13744. "slot reset.\n");
  13745. return PCI_ERS_RESULT_DISCONNECT;
  13746. } else
  13747. phba->intr_mode = intr_mode;
  13748. lpfc_cpu_affinity_check(phba, phba->cfg_irq_chann);
  13749. /* Log the current active interrupt mode */
  13750. lpfc_log_intr_mode(phba, phba->intr_mode);
  13751. return PCI_ERS_RESULT_RECOVERED;
  13752. }
  13753. /**
  13754. * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
  13755. * @pdev: pointer to PCI device
  13756. *
  13757. * This routine is called from the PCI subsystem for error handling to device
  13758. * with SLI-4 interface spec. It is called when kernel error recovery tells
  13759. * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
  13760. * error recovery. After this call, traffic can start to flow from this device
  13761. * again.
  13762. **/
  13763. static void
  13764. lpfc_io_resume_s4(struct pci_dev *pdev)
  13765. {
  13766. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  13767. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  13768. /*
  13769. * In case of slot reset, as function reset is performed through
  13770. * mailbox command which needs DMA to be enabled, this operation
  13771. * has to be moved to the io resume phase. Taking device offline
  13772. * will perform the necessary cleanup.
  13773. */
  13774. if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
  13775. /* Perform device reset */
  13776. lpfc_sli_brdrestart(phba);
  13777. /* Bring the device back online */
  13778. lpfc_online(phba);
  13779. }
  13780. }
  13781. /**
  13782. * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
  13783. * @pdev: pointer to PCI device
  13784. * @pid: pointer to PCI device identifier
  13785. *
  13786. * This routine is to be registered to the kernel's PCI subsystem. When an
  13787. * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
  13788. * at PCI device-specific information of the device and driver to see if the
  13789. * driver state that it can support this kind of device. If the match is
  13790. * successful, the driver core invokes this routine. This routine dispatches
  13791. * the action to the proper SLI-3 or SLI-4 device probing routine, which will
  13792. * do all the initialization that it needs to do to handle the HBA device
  13793. * properly.
  13794. *
  13795. * Return code
  13796. * 0 - driver can claim the device
  13797. * negative value - driver can not claim the device
  13798. **/
  13799. static int
  13800. lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
  13801. {
  13802. int rc;
  13803. struct lpfc_sli_intf intf;
  13804. if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
  13805. return -ENODEV;
  13806. if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
  13807. (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
  13808. rc = lpfc_pci_probe_one_s4(pdev, pid);
  13809. else
  13810. rc = lpfc_pci_probe_one_s3(pdev, pid);
  13811. return rc;
  13812. }
  13813. /**
  13814. * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
  13815. * @pdev: pointer to PCI device
  13816. *
  13817. * This routine is to be registered to the kernel's PCI subsystem. When an
  13818. * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
  13819. * This routine dispatches the action to the proper SLI-3 or SLI-4 device
  13820. * remove routine, which will perform all the necessary cleanup for the
  13821. * device to be removed from the PCI subsystem properly.
  13822. **/
  13823. static void
  13824. lpfc_pci_remove_one(struct pci_dev *pdev)
  13825. {
  13826. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  13827. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  13828. switch (phba->pci_dev_grp) {
  13829. case LPFC_PCI_DEV_LP:
  13830. lpfc_pci_remove_one_s3(pdev);
  13831. break;
  13832. case LPFC_PCI_DEV_OC:
  13833. lpfc_pci_remove_one_s4(pdev);
  13834. break;
  13835. default:
  13836. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13837. "1424 Invalid PCI device group: 0x%x\n",
  13838. phba->pci_dev_grp);
  13839. break;
  13840. }
  13841. return;
  13842. }
  13843. /**
  13844. * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
  13845. * @dev: pointer to device
  13846. *
  13847. * This routine is to be registered to the kernel's PCI subsystem to support
  13848. * system Power Management (PM). When PM invokes this method, it dispatches
  13849. * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
  13850. * suspend the device.
  13851. *
  13852. * Return code
  13853. * 0 - driver suspended the device
  13854. * Error otherwise
  13855. **/
  13856. static int __maybe_unused
  13857. lpfc_pci_suspend_one(struct device *dev)
  13858. {
  13859. struct Scsi_Host *shost = dev_get_drvdata(dev);
  13860. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  13861. int rc = -ENODEV;
  13862. switch (phba->pci_dev_grp) {
  13863. case LPFC_PCI_DEV_LP:
  13864. rc = lpfc_pci_suspend_one_s3(dev);
  13865. break;
  13866. case LPFC_PCI_DEV_OC:
  13867. rc = lpfc_pci_suspend_one_s4(dev);
  13868. break;
  13869. default:
  13870. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13871. "1425 Invalid PCI device group: 0x%x\n",
  13872. phba->pci_dev_grp);
  13873. break;
  13874. }
  13875. return rc;
  13876. }
  13877. /**
  13878. * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
  13879. * @dev: pointer to device
  13880. *
  13881. * This routine is to be registered to the kernel's PCI subsystem to support
  13882. * system Power Management (PM). When PM invokes this method, it dispatches
  13883. * the action to the proper SLI-3 or SLI-4 device resume routine, which will
  13884. * resume the device.
  13885. *
  13886. * Return code
  13887. * 0 - driver suspended the device
  13888. * Error otherwise
  13889. **/
  13890. static int __maybe_unused
  13891. lpfc_pci_resume_one(struct device *dev)
  13892. {
  13893. struct Scsi_Host *shost = dev_get_drvdata(dev);
  13894. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  13895. int rc = -ENODEV;
  13896. switch (phba->pci_dev_grp) {
  13897. case LPFC_PCI_DEV_LP:
  13898. rc = lpfc_pci_resume_one_s3(dev);
  13899. break;
  13900. case LPFC_PCI_DEV_OC:
  13901. rc = lpfc_pci_resume_one_s4(dev);
  13902. break;
  13903. default:
  13904. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13905. "1426 Invalid PCI device group: 0x%x\n",
  13906. phba->pci_dev_grp);
  13907. break;
  13908. }
  13909. return rc;
  13910. }
  13911. /**
  13912. * lpfc_io_error_detected - lpfc method for handling PCI I/O error
  13913. * @pdev: pointer to PCI device.
  13914. * @state: the current PCI connection state.
  13915. *
  13916. * This routine is registered to the PCI subsystem for error handling. This
  13917. * function is called by the PCI subsystem after a PCI bus error affecting
  13918. * this device has been detected. When this routine is invoked, it dispatches
  13919. * the action to the proper SLI-3 or SLI-4 device error detected handling
  13920. * routine, which will perform the proper error detected operation.
  13921. *
  13922. * Return codes
  13923. * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
  13924. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  13925. **/
  13926. static pci_ers_result_t
  13927. lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  13928. {
  13929. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  13930. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  13931. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  13932. if (phba->link_state == LPFC_HBA_ERROR &&
  13933. test_bit(HBA_IOQ_FLUSH, &phba->hba_flag))
  13934. return PCI_ERS_RESULT_NEED_RESET;
  13935. switch (phba->pci_dev_grp) {
  13936. case LPFC_PCI_DEV_LP:
  13937. rc = lpfc_io_error_detected_s3(pdev, state);
  13938. break;
  13939. case LPFC_PCI_DEV_OC:
  13940. rc = lpfc_io_error_detected_s4(pdev, state);
  13941. break;
  13942. default:
  13943. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13944. "1427 Invalid PCI device group: 0x%x\n",
  13945. phba->pci_dev_grp);
  13946. break;
  13947. }
  13948. return rc;
  13949. }
  13950. /**
  13951. * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
  13952. * @pdev: pointer to PCI device.
  13953. *
  13954. * This routine is registered to the PCI subsystem for error handling. This
  13955. * function is called after PCI bus has been reset to restart the PCI card
  13956. * from scratch, as if from a cold-boot. When this routine is invoked, it
  13957. * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
  13958. * routine, which will perform the proper device reset.
  13959. *
  13960. * Return codes
  13961. * PCI_ERS_RESULT_RECOVERED - the device has been recovered
  13962. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  13963. **/
  13964. static pci_ers_result_t
  13965. lpfc_io_slot_reset(struct pci_dev *pdev)
  13966. {
  13967. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  13968. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  13969. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  13970. switch (phba->pci_dev_grp) {
  13971. case LPFC_PCI_DEV_LP:
  13972. rc = lpfc_io_slot_reset_s3(pdev);
  13973. break;
  13974. case LPFC_PCI_DEV_OC:
  13975. rc = lpfc_io_slot_reset_s4(pdev);
  13976. break;
  13977. default:
  13978. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  13979. "1428 Invalid PCI device group: 0x%x\n",
  13980. phba->pci_dev_grp);
  13981. break;
  13982. }
  13983. return rc;
  13984. }
  13985. /**
  13986. * lpfc_io_resume - lpfc method for resuming PCI I/O operation
  13987. * @pdev: pointer to PCI device
  13988. *
  13989. * This routine is registered to the PCI subsystem for error handling. It
  13990. * is called when kernel error recovery tells the lpfc driver that it is
  13991. * OK to resume normal PCI operation after PCI bus error recovery. When
  13992. * this routine is invoked, it dispatches the action to the proper SLI-3
  13993. * or SLI-4 device io_resume routine, which will resume the device operation.
  13994. **/
  13995. static void
  13996. lpfc_io_resume(struct pci_dev *pdev)
  13997. {
  13998. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  13999. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  14000. switch (phba->pci_dev_grp) {
  14001. case LPFC_PCI_DEV_LP:
  14002. lpfc_io_resume_s3(pdev);
  14003. break;
  14004. case LPFC_PCI_DEV_OC:
  14005. lpfc_io_resume_s4(pdev);
  14006. break;
  14007. default:
  14008. lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
  14009. "1429 Invalid PCI device group: 0x%x\n",
  14010. phba->pci_dev_grp);
  14011. break;
  14012. }
  14013. return;
  14014. }
  14015. /**
  14016. * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
  14017. * @phba: pointer to lpfc hba data structure.
  14018. *
  14019. * This routine checks to see if OAS is supported for this adapter. If
  14020. * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
  14021. * the enable oas flag is cleared and the pool created for OAS device data
  14022. * is destroyed.
  14023. *
  14024. **/
  14025. static void
  14026. lpfc_sli4_oas_verify(struct lpfc_hba *phba)
  14027. {
  14028. if (!phba->cfg_EnableXLane)
  14029. return;
  14030. if (phba->sli4_hba.pc_sli4_params.oas_supported) {
  14031. phba->cfg_fof = 1;
  14032. } else {
  14033. phba->cfg_fof = 0;
  14034. mempool_destroy(phba->device_data_mem_pool);
  14035. phba->device_data_mem_pool = NULL;
  14036. }
  14037. return;
  14038. }
  14039. /**
  14040. * lpfc_sli4_ras_init - Verify RAS-FW log is supported by this adapter
  14041. * @phba: pointer to lpfc hba data structure.
  14042. *
  14043. * This routine checks to see if RAS is supported by the adapter. Check the
  14044. * function through which RAS support enablement is to be done.
  14045. **/
  14046. void
  14047. lpfc_sli4_ras_init(struct lpfc_hba *phba)
  14048. {
  14049. /* if ASIC_GEN_NUM >= 0xC) */
  14050. if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
  14051. LPFC_SLI_INTF_IF_TYPE_6) ||
  14052. (bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf) ==
  14053. LPFC_SLI_INTF_FAMILY_G6)) {
  14054. phba->ras_fwlog.ras_hwsupport = true;
  14055. if (phba->cfg_ras_fwlog_func == PCI_FUNC(phba->pcidev->devfn) &&
  14056. phba->cfg_ras_fwlog_buffsize)
  14057. phba->ras_fwlog.ras_enabled = true;
  14058. else
  14059. phba->ras_fwlog.ras_enabled = false;
  14060. } else {
  14061. phba->ras_fwlog.ras_hwsupport = false;
  14062. }
  14063. }
  14064. MODULE_DEVICE_TABLE(pci, lpfc_id_table);
  14065. static const struct pci_error_handlers lpfc_err_handler = {
  14066. .error_detected = lpfc_io_error_detected,
  14067. .slot_reset = lpfc_io_slot_reset,
  14068. .resume = lpfc_io_resume,
  14069. };
  14070. static SIMPLE_DEV_PM_OPS(lpfc_pci_pm_ops_one,
  14071. lpfc_pci_suspend_one,
  14072. lpfc_pci_resume_one);
  14073. static struct pci_driver lpfc_driver = {
  14074. .name = LPFC_DRIVER_NAME,
  14075. .id_table = lpfc_id_table,
  14076. .probe = lpfc_pci_probe_one,
  14077. .remove = lpfc_pci_remove_one,
  14078. .shutdown = lpfc_pci_remove_one,
  14079. .driver.pm = &lpfc_pci_pm_ops_one,
  14080. .err_handler = &lpfc_err_handler,
  14081. };
  14082. static const struct file_operations lpfc_mgmt_fop = {
  14083. .owner = THIS_MODULE,
  14084. };
  14085. static struct miscdevice lpfc_mgmt_dev = {
  14086. .minor = MISC_DYNAMIC_MINOR,
  14087. .name = "lpfcmgmt",
  14088. .fops = &lpfc_mgmt_fop,
  14089. };
  14090. /**
  14091. * lpfc_init - lpfc module initialization routine
  14092. *
  14093. * This routine is to be invoked when the lpfc module is loaded into the
  14094. * kernel. The special kernel macro module_init() is used to indicate the
  14095. * role of this routine to the kernel as lpfc module entry point.
  14096. *
  14097. * Return codes
  14098. * 0 - successful
  14099. * -ENOMEM - FC attach transport failed
  14100. * all others - failed
  14101. */
  14102. static int __init
  14103. lpfc_init(void)
  14104. {
  14105. int error = 0;
  14106. pr_info(LPFC_MODULE_DESC "\n");
  14107. pr_info(LPFC_COPYRIGHT "\n");
  14108. error = misc_register(&lpfc_mgmt_dev);
  14109. if (error)
  14110. printk(KERN_ERR "Could not register lpfcmgmt device, "
  14111. "misc_register returned with status %d", error);
  14112. error = -ENOMEM;
  14113. lpfc_transport_functions.vport_create = lpfc_vport_create;
  14114. lpfc_transport_functions.vport_delete = lpfc_vport_delete;
  14115. lpfc_transport_template =
  14116. fc_attach_transport(&lpfc_transport_functions);
  14117. if (lpfc_transport_template == NULL)
  14118. goto unregister;
  14119. lpfc_vport_transport_template =
  14120. fc_attach_transport(&lpfc_vport_transport_functions);
  14121. if (lpfc_vport_transport_template == NULL) {
  14122. fc_release_transport(lpfc_transport_template);
  14123. goto unregister;
  14124. }
  14125. lpfc_wqe_cmd_template();
  14126. lpfc_nvmet_cmd_template();
  14127. /* Initialize in case vector mapping is needed */
  14128. lpfc_present_cpu = num_present_cpus();
  14129. lpfc_pldv_detect = false;
  14130. error = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
  14131. "lpfc/sli4:online",
  14132. lpfc_cpu_online, lpfc_cpu_offline);
  14133. if (error < 0)
  14134. goto cpuhp_failure;
  14135. lpfc_cpuhp_state = error;
  14136. error = pci_register_driver(&lpfc_driver);
  14137. if (error)
  14138. goto unwind;
  14139. return error;
  14140. unwind:
  14141. cpuhp_remove_multi_state(lpfc_cpuhp_state);
  14142. cpuhp_failure:
  14143. fc_release_transport(lpfc_transport_template);
  14144. fc_release_transport(lpfc_vport_transport_template);
  14145. unregister:
  14146. misc_deregister(&lpfc_mgmt_dev);
  14147. return error;
  14148. }
  14149. void lpfc_dmp_dbg(struct lpfc_hba *phba)
  14150. {
  14151. unsigned int start_idx;
  14152. unsigned int dbg_cnt;
  14153. unsigned int temp_idx;
  14154. int i;
  14155. int j = 0;
  14156. unsigned long rem_nsec;
  14157. if (atomic_cmpxchg(&phba->dbg_log_dmping, 0, 1) != 0)
  14158. return;
  14159. start_idx = (unsigned int)atomic_read(&phba->dbg_log_idx) % DBG_LOG_SZ;
  14160. dbg_cnt = (unsigned int)atomic_read(&phba->dbg_log_cnt);
  14161. if (!dbg_cnt)
  14162. goto out;
  14163. temp_idx = start_idx;
  14164. if (dbg_cnt >= DBG_LOG_SZ) {
  14165. dbg_cnt = DBG_LOG_SZ;
  14166. temp_idx -= 1;
  14167. } else {
  14168. if ((start_idx + dbg_cnt) > (DBG_LOG_SZ - 1)) {
  14169. temp_idx = (start_idx + dbg_cnt) % DBG_LOG_SZ;
  14170. } else {
  14171. if (start_idx < dbg_cnt)
  14172. start_idx = DBG_LOG_SZ - (dbg_cnt - start_idx);
  14173. else
  14174. start_idx -= dbg_cnt;
  14175. }
  14176. }
  14177. dev_info(&phba->pcidev->dev, "start %d end %d cnt %d\n",
  14178. start_idx, temp_idx, dbg_cnt);
  14179. for (i = 0; i < dbg_cnt; i++) {
  14180. if ((start_idx + i) < DBG_LOG_SZ)
  14181. temp_idx = (start_idx + i) % DBG_LOG_SZ;
  14182. else
  14183. temp_idx = j++;
  14184. rem_nsec = do_div(phba->dbg_log[temp_idx].t_ns, NSEC_PER_SEC);
  14185. dev_info(&phba->pcidev->dev, "%d: [%5lu.%06lu] %s",
  14186. temp_idx,
  14187. (unsigned long)phba->dbg_log[temp_idx].t_ns,
  14188. rem_nsec / 1000,
  14189. phba->dbg_log[temp_idx].log);
  14190. }
  14191. out:
  14192. atomic_set(&phba->dbg_log_cnt, 0);
  14193. atomic_set(&phba->dbg_log_dmping, 0);
  14194. }
  14195. __printf(2, 3)
  14196. void lpfc_dbg_print(struct lpfc_hba *phba, const char *fmt, ...)
  14197. {
  14198. unsigned int idx;
  14199. va_list args;
  14200. int dbg_dmping = atomic_read(&phba->dbg_log_dmping);
  14201. struct va_format vaf;
  14202. va_start(args, fmt);
  14203. if (unlikely(dbg_dmping)) {
  14204. vaf.fmt = fmt;
  14205. vaf.va = &args;
  14206. dev_info(&phba->pcidev->dev, "%pV", &vaf);
  14207. va_end(args);
  14208. return;
  14209. }
  14210. idx = (unsigned int)atomic_fetch_add(1, &phba->dbg_log_idx) %
  14211. DBG_LOG_SZ;
  14212. atomic_inc(&phba->dbg_log_cnt);
  14213. vscnprintf(phba->dbg_log[idx].log,
  14214. sizeof(phba->dbg_log[idx].log), fmt, args);
  14215. va_end(args);
  14216. phba->dbg_log[idx].t_ns = local_clock();
  14217. }
  14218. /**
  14219. * lpfc_exit - lpfc module removal routine
  14220. *
  14221. * This routine is invoked when the lpfc module is removed from the kernel.
  14222. * The special kernel macro module_exit() is used to indicate the role of
  14223. * this routine to the kernel as lpfc module exit point.
  14224. */
  14225. static void __exit
  14226. lpfc_exit(void)
  14227. {
  14228. misc_deregister(&lpfc_mgmt_dev);
  14229. pci_unregister_driver(&lpfc_driver);
  14230. cpuhp_remove_multi_state(lpfc_cpuhp_state);
  14231. fc_release_transport(lpfc_transport_template);
  14232. fc_release_transport(lpfc_vport_transport_template);
  14233. idr_destroy(&lpfc_hba_index);
  14234. }
  14235. module_init(lpfc_init);
  14236. module_exit(lpfc_exit);
  14237. MODULE_LICENSE("GPL");
  14238. MODULE_DESCRIPTION(LPFC_MODULE_DESC);
  14239. MODULE_AUTHOR("Broadcom");
  14240. MODULE_VERSION("0:" LPFC_DRIVER_VERSION);