lpfc_hbadisc.c 159 KB

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