lpfc_hbadisc.c 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559
  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. unsigned long flags;
  895. if (mboxq->u.mb.mbxStatus) {
  896. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  897. "2017 REG_FCFI mbxStatus error x%x "
  898. "HBA state x%x\n",
  899. mboxq->u.mb.mbxStatus, vport->port_state);
  900. mempool_free(mboxq, phba->mbox_mem_pool);
  901. return;
  902. }
  903. /* Start FCoE discovery by sending a FLOGI. */
  904. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
  905. /* Set the FCFI registered flag */
  906. spin_lock_irqsave(&phba->hbalock, flags);
  907. phba->fcf.fcf_flag |= FCF_REGISTERED;
  908. spin_unlock_irqrestore(&phba->hbalock, flags);
  909. /* If there is a pending FCoE event, restart FCF table scan. */
  910. if (lpfc_check_pending_fcoe_event(phba, 1)) {
  911. mempool_free(mboxq, phba->mbox_mem_pool);
  912. return;
  913. }
  914. spin_lock_irqsave(&phba->hbalock, flags);
  915. phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
  916. phba->hba_flag &= ~FCF_DISC_INPROGRESS;
  917. spin_unlock_irqrestore(&phba->hbalock, flags);
  918. if (vport->port_state != LPFC_FLOGI)
  919. lpfc_initial_flogi(vport);
  920. mempool_free(mboxq, phba->mbox_mem_pool);
  921. return;
  922. }
  923. /**
  924. * lpfc_fab_name_match - Check if the fcf fabric name match.
  925. * @fab_name: pointer to fabric name.
  926. * @new_fcf_record: pointer to fcf record.
  927. *
  928. * This routine compare the fcf record's fabric name with provided
  929. * fabric name. If the fabric name are identical this function
  930. * returns 1 else return 0.
  931. **/
  932. static uint32_t
  933. lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
  934. {
  935. if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
  936. return 0;
  937. if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
  938. return 0;
  939. if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
  940. return 0;
  941. if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
  942. return 0;
  943. if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
  944. return 0;
  945. if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
  946. return 0;
  947. if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
  948. return 0;
  949. if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
  950. return 0;
  951. return 1;
  952. }
  953. /**
  954. * lpfc_sw_name_match - Check if the fcf switch name match.
  955. * @fab_name: pointer to fabric name.
  956. * @new_fcf_record: pointer to fcf record.
  957. *
  958. * This routine compare the fcf record's switch name with provided
  959. * switch name. If the switch name are identical this function
  960. * returns 1 else return 0.
  961. **/
  962. static uint32_t
  963. lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
  964. {
  965. if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
  966. return 0;
  967. if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
  968. return 0;
  969. if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
  970. return 0;
  971. if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
  972. return 0;
  973. if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
  974. return 0;
  975. if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
  976. return 0;
  977. if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
  978. return 0;
  979. if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
  980. return 0;
  981. return 1;
  982. }
  983. /**
  984. * lpfc_mac_addr_match - Check if the fcf mac address match.
  985. * @mac_addr: pointer to mac address.
  986. * @new_fcf_record: pointer to fcf record.
  987. *
  988. * This routine compare the fcf record's mac address with HBA's
  989. * FCF mac address. If the mac addresses are identical this function
  990. * returns 1 else return 0.
  991. **/
  992. static uint32_t
  993. lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
  994. {
  995. if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
  996. return 0;
  997. if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
  998. return 0;
  999. if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
  1000. return 0;
  1001. if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
  1002. return 0;
  1003. if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
  1004. return 0;
  1005. if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
  1006. return 0;
  1007. return 1;
  1008. }
  1009. static bool
  1010. lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
  1011. {
  1012. return (curr_vlan_id == new_vlan_id);
  1013. }
  1014. /**
  1015. * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
  1016. * @fcf: pointer to driver fcf record.
  1017. * @new_fcf_record: pointer to fcf record.
  1018. *
  1019. * This routine copies the FCF information from the FCF
  1020. * record to lpfc_hba data structure.
  1021. **/
  1022. static void
  1023. lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
  1024. struct fcf_record *new_fcf_record)
  1025. {
  1026. /* Fabric name */
  1027. fcf_rec->fabric_name[0] =
  1028. bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
  1029. fcf_rec->fabric_name[1] =
  1030. bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
  1031. fcf_rec->fabric_name[2] =
  1032. bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
  1033. fcf_rec->fabric_name[3] =
  1034. bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
  1035. fcf_rec->fabric_name[4] =
  1036. bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
  1037. fcf_rec->fabric_name[5] =
  1038. bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
  1039. fcf_rec->fabric_name[6] =
  1040. bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
  1041. fcf_rec->fabric_name[7] =
  1042. bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
  1043. /* Mac address */
  1044. fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
  1045. fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
  1046. fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
  1047. fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
  1048. fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
  1049. fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
  1050. /* FCF record index */
  1051. fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
  1052. /* FCF record priority */
  1053. fcf_rec->priority = new_fcf_record->fip_priority;
  1054. /* Switch name */
  1055. fcf_rec->switch_name[0] =
  1056. bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
  1057. fcf_rec->switch_name[1] =
  1058. bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
  1059. fcf_rec->switch_name[2] =
  1060. bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
  1061. fcf_rec->switch_name[3] =
  1062. bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
  1063. fcf_rec->switch_name[4] =
  1064. bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
  1065. fcf_rec->switch_name[5] =
  1066. bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
  1067. fcf_rec->switch_name[6] =
  1068. bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
  1069. fcf_rec->switch_name[7] =
  1070. bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
  1071. }
  1072. /**
  1073. * lpfc_update_fcf_record - Update driver fcf record
  1074. * @phba: pointer to lpfc hba data structure.
  1075. * @fcf_rec: pointer to driver fcf record.
  1076. * @new_fcf_record: pointer to hba fcf record.
  1077. * @addr_mode: address mode to be set to the driver fcf record.
  1078. * @vlan_id: vlan tag to be set to the driver fcf record.
  1079. * @flag: flag bits to be set to the driver fcf record.
  1080. *
  1081. * This routine updates the driver FCF record from the new HBA FCF record
  1082. * together with the address mode, vlan_id, and other informations. This
  1083. * routine is called with the host lock held.
  1084. **/
  1085. static void
  1086. __lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
  1087. struct fcf_record *new_fcf_record, uint32_t addr_mode,
  1088. uint16_t vlan_id, uint32_t flag)
  1089. {
  1090. /* Copy the fields from the HBA's FCF record */
  1091. lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
  1092. /* Update other fields of driver FCF record */
  1093. fcf_rec->addr_mode = addr_mode;
  1094. fcf_rec->vlan_id = vlan_id;
  1095. fcf_rec->flag |= (flag | RECORD_VALID);
  1096. }
  1097. /**
  1098. * lpfc_register_fcf - Register the FCF with hba.
  1099. * @phba: pointer to lpfc hba data structure.
  1100. *
  1101. * This routine issues a register fcfi mailbox command to register
  1102. * the fcf with HBA.
  1103. **/
  1104. static void
  1105. lpfc_register_fcf(struct lpfc_hba *phba)
  1106. {
  1107. LPFC_MBOXQ_t *fcf_mbxq;
  1108. int rc;
  1109. unsigned long flags;
  1110. spin_lock_irqsave(&phba->hbalock, flags);
  1111. /* If the FCF is not availabe do nothing. */
  1112. if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
  1113. phba->hba_flag &= ~FCF_DISC_INPROGRESS;
  1114. spin_unlock_irqrestore(&phba->hbalock, flags);
  1115. return;
  1116. }
  1117. /* The FCF is already registered, start discovery */
  1118. if (phba->fcf.fcf_flag & FCF_REGISTERED) {
  1119. phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
  1120. phba->hba_flag &= ~FCF_DISC_INPROGRESS;
  1121. spin_unlock_irqrestore(&phba->hbalock, flags);
  1122. if (phba->pport->port_state != LPFC_FLOGI)
  1123. lpfc_initial_flogi(phba->pport);
  1124. return;
  1125. }
  1126. spin_unlock_irqrestore(&phba->hbalock, flags);
  1127. fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
  1128. GFP_KERNEL);
  1129. if (!fcf_mbxq) {
  1130. spin_lock_irqsave(&phba->hbalock, flags);
  1131. phba->hba_flag &= ~FCF_DISC_INPROGRESS;
  1132. spin_unlock_irqrestore(&phba->hbalock, flags);
  1133. return;
  1134. }
  1135. lpfc_reg_fcfi(phba, fcf_mbxq);
  1136. fcf_mbxq->vport = phba->pport;
  1137. fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
  1138. rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
  1139. if (rc == MBX_NOT_FINISHED) {
  1140. spin_lock_irqsave(&phba->hbalock, flags);
  1141. phba->hba_flag &= ~FCF_DISC_INPROGRESS;
  1142. spin_unlock_irqrestore(&phba->hbalock, flags);
  1143. mempool_free(fcf_mbxq, phba->mbox_mem_pool);
  1144. }
  1145. return;
  1146. }
  1147. /**
  1148. * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
  1149. * @phba: pointer to lpfc hba data structure.
  1150. * @new_fcf_record: pointer to fcf record.
  1151. * @boot_flag: Indicates if this record used by boot bios.
  1152. * @addr_mode: The address mode to be used by this FCF
  1153. * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
  1154. *
  1155. * This routine compare the fcf record with connect list obtained from the
  1156. * config region to decide if this FCF can be used for SAN discovery. It returns
  1157. * 1 if this record can be used for SAN discovery else return zero. If this FCF
  1158. * record can be used for SAN discovery, the boot_flag will indicate if this FCF
  1159. * is used by boot bios and addr_mode will indicate the addressing mode to be
  1160. * used for this FCF when the function returns.
  1161. * If the FCF record need to be used with a particular vlan id, the vlan is
  1162. * set in the vlan_id on return of the function. If not VLAN tagging need to
  1163. * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
  1164. **/
  1165. static int
  1166. lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
  1167. struct fcf_record *new_fcf_record,
  1168. uint32_t *boot_flag, uint32_t *addr_mode,
  1169. uint16_t *vlan_id)
  1170. {
  1171. struct lpfc_fcf_conn_entry *conn_entry;
  1172. int i, j, fcf_vlan_id = 0;
  1173. /* Find the lowest VLAN id in the FCF record */
  1174. for (i = 0; i < 512; i++) {
  1175. if (new_fcf_record->vlan_bitmap[i]) {
  1176. fcf_vlan_id = i * 8;
  1177. j = 0;
  1178. while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
  1179. j++;
  1180. fcf_vlan_id++;
  1181. }
  1182. break;
  1183. }
  1184. }
  1185. /* If FCF not available return 0 */
  1186. if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
  1187. !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
  1188. return 0;
  1189. if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
  1190. *boot_flag = 0;
  1191. *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
  1192. new_fcf_record);
  1193. if (phba->valid_vlan)
  1194. *vlan_id = phba->vlan_id;
  1195. else
  1196. *vlan_id = LPFC_FCOE_NULL_VID;
  1197. return 1;
  1198. }
  1199. /*
  1200. * If there are no FCF connection table entry, driver connect to all
  1201. * FCFs.
  1202. */
  1203. if (list_empty(&phba->fcf_conn_rec_list)) {
  1204. *boot_flag = 0;
  1205. *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
  1206. new_fcf_record);
  1207. /*
  1208. * When there are no FCF connect entries, use driver's default
  1209. * addressing mode - FPMA.
  1210. */
  1211. if (*addr_mode & LPFC_FCF_FPMA)
  1212. *addr_mode = LPFC_FCF_FPMA;
  1213. /* If FCF record report a vlan id use that vlan id */
  1214. if (fcf_vlan_id)
  1215. *vlan_id = fcf_vlan_id;
  1216. else
  1217. *vlan_id = LPFC_FCOE_NULL_VID;
  1218. return 1;
  1219. }
  1220. list_for_each_entry(conn_entry,
  1221. &phba->fcf_conn_rec_list, list) {
  1222. if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
  1223. continue;
  1224. if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
  1225. !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
  1226. new_fcf_record))
  1227. continue;
  1228. if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
  1229. !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
  1230. new_fcf_record))
  1231. continue;
  1232. if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
  1233. /*
  1234. * If the vlan bit map does not have the bit set for the
  1235. * vlan id to be used, then it is not a match.
  1236. */
  1237. if (!(new_fcf_record->vlan_bitmap
  1238. [conn_entry->conn_rec.vlan_tag / 8] &
  1239. (1 << (conn_entry->conn_rec.vlan_tag % 8))))
  1240. continue;
  1241. }
  1242. /*
  1243. * If connection record does not support any addressing mode,
  1244. * skip the FCF record.
  1245. */
  1246. if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
  1247. & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
  1248. continue;
  1249. /*
  1250. * Check if the connection record specifies a required
  1251. * addressing mode.
  1252. */
  1253. if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
  1254. !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
  1255. /*
  1256. * If SPMA required but FCF not support this continue.
  1257. */
  1258. if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
  1259. !(bf_get(lpfc_fcf_record_mac_addr_prov,
  1260. new_fcf_record) & LPFC_FCF_SPMA))
  1261. continue;
  1262. /*
  1263. * If FPMA required but FCF not support this continue.
  1264. */
  1265. if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
  1266. !(bf_get(lpfc_fcf_record_mac_addr_prov,
  1267. new_fcf_record) & LPFC_FCF_FPMA))
  1268. continue;
  1269. }
  1270. /*
  1271. * This fcf record matches filtering criteria.
  1272. */
  1273. if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
  1274. *boot_flag = 1;
  1275. else
  1276. *boot_flag = 0;
  1277. /*
  1278. * If user did not specify any addressing mode, or if the
  1279. * prefered addressing mode specified by user is not supported
  1280. * by FCF, allow fabric to pick the addressing mode.
  1281. */
  1282. *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
  1283. new_fcf_record);
  1284. /*
  1285. * If the user specified a required address mode, assign that
  1286. * address mode
  1287. */
  1288. if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
  1289. (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
  1290. *addr_mode = (conn_entry->conn_rec.flags &
  1291. FCFCNCT_AM_SPMA) ?
  1292. LPFC_FCF_SPMA : LPFC_FCF_FPMA;
  1293. /*
  1294. * If the user specified a prefered address mode, use the
  1295. * addr mode only if FCF support the addr_mode.
  1296. */
  1297. else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
  1298. (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
  1299. (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
  1300. (*addr_mode & LPFC_FCF_SPMA))
  1301. *addr_mode = LPFC_FCF_SPMA;
  1302. else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
  1303. (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
  1304. !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
  1305. (*addr_mode & LPFC_FCF_FPMA))
  1306. *addr_mode = LPFC_FCF_FPMA;
  1307. /* If matching connect list has a vlan id, use it */
  1308. if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
  1309. *vlan_id = conn_entry->conn_rec.vlan_tag;
  1310. /*
  1311. * If no vlan id is specified in connect list, use the vlan id
  1312. * in the FCF record
  1313. */
  1314. else if (fcf_vlan_id)
  1315. *vlan_id = fcf_vlan_id;
  1316. else
  1317. *vlan_id = LPFC_FCOE_NULL_VID;
  1318. return 1;
  1319. }
  1320. return 0;
  1321. }
  1322. /**
  1323. * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
  1324. * @phba: pointer to lpfc hba data structure.
  1325. * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
  1326. *
  1327. * This function check if there is any fcoe event pending while driver
  1328. * scan FCF entries. If there is any pending event, it will restart the
  1329. * FCF saning and return 1 else return 0.
  1330. */
  1331. int
  1332. lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
  1333. {
  1334. /*
  1335. * If the Link is up and no FCoE events while in the
  1336. * FCF discovery, no need to restart FCF discovery.
  1337. */
  1338. if ((phba->link_state >= LPFC_LINK_UP) &&
  1339. (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
  1340. return 0;
  1341. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1342. "2768 Pending link or FCF event during current "
  1343. "handling of the previous event: link_state:x%x, "
  1344. "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
  1345. phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
  1346. phba->fcoe_eventtag);
  1347. spin_lock_irq(&phba->hbalock);
  1348. phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
  1349. spin_unlock_irq(&phba->hbalock);
  1350. if (phba->link_state >= LPFC_LINK_UP) {
  1351. lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
  1352. "2780 Restart FCF table scan due to "
  1353. "pending FCF event:evt_tag_at_scan:x%x, "
  1354. "evt_tag_current:x%x\n",
  1355. phba->fcoe_eventtag_at_fcf_scan,
  1356. phba->fcoe_eventtag);
  1357. lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
  1358. } else {
  1359. /*
  1360. * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
  1361. * flag
  1362. */
  1363. lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
  1364. "2833 Stop FCF discovery process due to link "
  1365. "state change (x%x)\n", phba->link_state);
  1366. spin_lock_irq(&phba->hbalock);
  1367. phba->hba_flag &= ~FCF_DISC_INPROGRESS;
  1368. phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
  1369. spin_unlock_irq(&phba->hbalock);
  1370. }
  1371. /* Unregister the currently registered FCF if required */
  1372. if (unreg_fcf) {
  1373. spin_lock_irq(&phba->hbalock);
  1374. phba->fcf.fcf_flag &= ~FCF_REGISTERED;
  1375. spin_unlock_irq(&phba->hbalock);
  1376. lpfc_sli4_unregister_fcf(phba);
  1377. }
  1378. return 1;
  1379. }
  1380. /**
  1381. * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
  1382. * @phba: pointer to lpfc hba data structure.
  1383. * @fcf_cnt: number of eligible fcf record seen so far.
  1384. *
  1385. * This function makes an running random selection decision on FCF record to
  1386. * use through a sequence of @fcf_cnt eligible FCF records with equal
  1387. * probability. To perform integer manunipulation of random numbers with
  1388. * size unit32_t, the lower 16 bits of the 32-bit random number returned
  1389. * from random32() are taken as the random random number generated.
  1390. *
  1391. * Returns true when outcome is for the newly read FCF record should be
  1392. * chosen; otherwise, return false when outcome is for keeping the previously
  1393. * chosen FCF record.
  1394. **/
  1395. static bool
  1396. lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
  1397. {
  1398. uint32_t rand_num;
  1399. /* Get 16-bit uniform random number */
  1400. rand_num = (0xFFFF & random32());
  1401. /* Decision with probability 1/fcf_cnt */
  1402. if ((fcf_cnt * rand_num) < 0xFFFF)
  1403. return true;
  1404. else
  1405. return false;
  1406. }
  1407. /**
  1408. * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
  1409. * @phba: pointer to lpfc hba data structure.
  1410. * @mboxq: pointer to mailbox object.
  1411. * @next_fcf_index: pointer to holder of next fcf index.
  1412. *
  1413. * This routine parses the non-embedded fcf mailbox command by performing the
  1414. * necessarily error checking, non-embedded read FCF record mailbox command
  1415. * SGE parsing, and endianness swapping.
  1416. *
  1417. * Returns the pointer to the new FCF record in the non-embedded mailbox
  1418. * command DMA memory if successfully, other NULL.
  1419. */
  1420. static struct fcf_record *
  1421. lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  1422. uint16_t *next_fcf_index)
  1423. {
  1424. void *virt_addr;
  1425. dma_addr_t phys_addr;
  1426. struct lpfc_mbx_sge sge;
  1427. struct lpfc_mbx_read_fcf_tbl *read_fcf;
  1428. uint32_t shdr_status, shdr_add_status;
  1429. union lpfc_sli4_cfg_shdr *shdr;
  1430. struct fcf_record *new_fcf_record;
  1431. /* Get the first SGE entry from the non-embedded DMA memory. This
  1432. * routine only uses a single SGE.
  1433. */
  1434. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  1435. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  1436. if (unlikely(!mboxq->sge_array)) {
  1437. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  1438. "2524 Failed to get the non-embedded SGE "
  1439. "virtual address\n");
  1440. return NULL;
  1441. }
  1442. virt_addr = mboxq->sge_array->addr[0];
  1443. shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
  1444. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  1445. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  1446. if (shdr_status || shdr_add_status) {
  1447. if (shdr_status == STATUS_FCF_TABLE_EMPTY)
  1448. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  1449. "2726 READ_FCF_RECORD Indicates empty "
  1450. "FCF table.\n");
  1451. else
  1452. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  1453. "2521 READ_FCF_RECORD mailbox failed "
  1454. "with status x%x add_status x%x, "
  1455. "mbx\n", shdr_status, shdr_add_status);
  1456. return NULL;
  1457. }
  1458. /* Interpreting the returned information of the FCF record */
  1459. read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
  1460. lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
  1461. sizeof(struct lpfc_mbx_read_fcf_tbl));
  1462. *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
  1463. new_fcf_record = (struct fcf_record *)(virt_addr +
  1464. sizeof(struct lpfc_mbx_read_fcf_tbl));
  1465. lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
  1466. offsetof(struct fcf_record, vlan_bitmap));
  1467. new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
  1468. new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
  1469. return new_fcf_record;
  1470. }
  1471. /**
  1472. * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
  1473. * @phba: pointer to lpfc hba data structure.
  1474. * @fcf_record: pointer to the fcf record.
  1475. * @vlan_id: the lowest vlan identifier associated to this fcf record.
  1476. * @next_fcf_index: the index to the next fcf record in hba's fcf table.
  1477. *
  1478. * This routine logs the detailed FCF record if the LOG_FIP loggin is
  1479. * enabled.
  1480. **/
  1481. static void
  1482. lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
  1483. struct fcf_record *fcf_record,
  1484. uint16_t vlan_id,
  1485. uint16_t next_fcf_index)
  1486. {
  1487. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1488. "2764 READ_FCF_RECORD:\n"
  1489. "\tFCF_Index : x%x\n"
  1490. "\tFCF_Avail : x%x\n"
  1491. "\tFCF_Valid : x%x\n"
  1492. "\tFIP_Priority : x%x\n"
  1493. "\tMAC_Provider : x%x\n"
  1494. "\tLowest VLANID : x%x\n"
  1495. "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
  1496. "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
  1497. "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
  1498. "\tNext_FCF_Index: x%x\n",
  1499. bf_get(lpfc_fcf_record_fcf_index, fcf_record),
  1500. bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
  1501. bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
  1502. fcf_record->fip_priority,
  1503. bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
  1504. vlan_id,
  1505. bf_get(lpfc_fcf_record_mac_0, fcf_record),
  1506. bf_get(lpfc_fcf_record_mac_1, fcf_record),
  1507. bf_get(lpfc_fcf_record_mac_2, fcf_record),
  1508. bf_get(lpfc_fcf_record_mac_3, fcf_record),
  1509. bf_get(lpfc_fcf_record_mac_4, fcf_record),
  1510. bf_get(lpfc_fcf_record_mac_5, fcf_record),
  1511. bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
  1512. bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
  1513. bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
  1514. bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
  1515. bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
  1516. bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
  1517. bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
  1518. bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
  1519. bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
  1520. bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
  1521. bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
  1522. bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
  1523. bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
  1524. bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
  1525. bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
  1526. bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
  1527. next_fcf_index);
  1528. }
  1529. /**
  1530. lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
  1531. * @phba: pointer to lpfc hba data structure.
  1532. * @fcf_rec: pointer to an existing FCF record.
  1533. * @new_fcf_record: pointer to a new FCF record.
  1534. * @new_vlan_id: vlan id from the new FCF record.
  1535. *
  1536. * This function performs matching test of a new FCF record against an existing
  1537. * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
  1538. * will not be used as part of the FCF record matching criteria.
  1539. *
  1540. * Returns true if all the fields matching, otherwise returns false.
  1541. */
  1542. static bool
  1543. lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
  1544. struct lpfc_fcf_rec *fcf_rec,
  1545. struct fcf_record *new_fcf_record,
  1546. uint16_t new_vlan_id)
  1547. {
  1548. if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
  1549. if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
  1550. return false;
  1551. if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
  1552. return false;
  1553. if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
  1554. return false;
  1555. if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
  1556. return false;
  1557. return true;
  1558. }
  1559. /**
  1560. * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
  1561. * @phba: pointer to lpfc hba data structure.
  1562. * @mboxq: pointer to mailbox object.
  1563. *
  1564. * This function iterates through all the fcf records available in
  1565. * HBA and chooses the optimal FCF record for discovery. After finding
  1566. * the FCF for discovery it registers the FCF record and kicks start
  1567. * discovery.
  1568. * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
  1569. * use an FCF record which matches fabric name and mac address of the
  1570. * currently used FCF record.
  1571. * If the driver supports only one FCF, it will try to use the FCF record
  1572. * used by BOOT_BIOS.
  1573. */
  1574. void
  1575. lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  1576. {
  1577. struct fcf_record *new_fcf_record;
  1578. uint32_t boot_flag, addr_mode;
  1579. uint16_t fcf_index, next_fcf_index;
  1580. struct lpfc_fcf_rec *fcf_rec = NULL;
  1581. uint16_t vlan_id;
  1582. uint32_t seed;
  1583. bool select_new_fcf;
  1584. int rc;
  1585. /* If there is pending FCoE event restart FCF table scan */
  1586. if (lpfc_check_pending_fcoe_event(phba, 0)) {
  1587. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1588. return;
  1589. }
  1590. /* Parse the FCF record from the non-embedded mailbox command */
  1591. new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
  1592. &next_fcf_index);
  1593. if (!new_fcf_record) {
  1594. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  1595. "2765 Mailbox command READ_FCF_RECORD "
  1596. "failed to retrieve a FCF record.\n");
  1597. /* Let next new FCF event trigger fast failover */
  1598. spin_lock_irq(&phba->hbalock);
  1599. phba->hba_flag &= ~FCF_DISC_INPROGRESS;
  1600. spin_unlock_irq(&phba->hbalock);
  1601. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1602. return;
  1603. }
  1604. /* Check the FCF record against the connection list */
  1605. rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
  1606. &addr_mode, &vlan_id);
  1607. /* Log the FCF record information if turned on */
  1608. lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
  1609. next_fcf_index);
  1610. /*
  1611. * If the fcf record does not match with connect list entries
  1612. * read the next entry; otherwise, this is an eligible FCF
  1613. * record for round robin FCF failover.
  1614. */
  1615. if (!rc) {
  1616. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  1617. "2781 FCF record (x%x) failed FCF "
  1618. "connection list check, fcf_avail:x%x, "
  1619. "fcf_valid:x%x\n",
  1620. bf_get(lpfc_fcf_record_fcf_index,
  1621. new_fcf_record),
  1622. bf_get(lpfc_fcf_record_fcf_avail,
  1623. new_fcf_record),
  1624. bf_get(lpfc_fcf_record_fcf_valid,
  1625. new_fcf_record));
  1626. if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
  1627. lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
  1628. new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
  1629. /*
  1630. * In case the current in-use FCF record becomes
  1631. * invalid/unavailable during FCF discovery that
  1632. * was not triggered by fast FCF failover process,
  1633. * treat it as fast FCF failover.
  1634. */
  1635. if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
  1636. !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
  1637. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  1638. "2835 Invalid in-use FCF "
  1639. "record (x%x) reported, "
  1640. "entering fast FCF failover "
  1641. "mode scanning.\n",
  1642. phba->fcf.current_rec.fcf_indx);
  1643. spin_lock_irq(&phba->hbalock);
  1644. phba->fcf.fcf_flag |= FCF_REDISC_FOV;
  1645. spin_unlock_irq(&phba->hbalock);
  1646. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1647. lpfc_sli4_fcf_scan_read_fcf_rec(phba,
  1648. LPFC_FCOE_FCF_GET_FIRST);
  1649. return;
  1650. }
  1651. }
  1652. goto read_next_fcf;
  1653. } else {
  1654. fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
  1655. rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
  1656. if (rc)
  1657. goto read_next_fcf;
  1658. }
  1659. /*
  1660. * If this is not the first FCF discovery of the HBA, use last
  1661. * FCF record for the discovery. The condition that a rescan
  1662. * matches the in-use FCF record: fabric name, switch name, mac
  1663. * address, and vlan_id.
  1664. */
  1665. spin_lock_irq(&phba->hbalock);
  1666. if (phba->fcf.fcf_flag & FCF_IN_USE) {
  1667. if (lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
  1668. new_fcf_record, vlan_id)) {
  1669. phba->fcf.fcf_flag |= FCF_AVAILABLE;
  1670. if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
  1671. /* Stop FCF redisc wait timer if pending */
  1672. __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
  1673. else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
  1674. /* If in fast failover, mark it's completed */
  1675. phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
  1676. spin_unlock_irq(&phba->hbalock);
  1677. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1678. "2836 The new FCF record (x%x) "
  1679. "matches the in-use FCF record "
  1680. "(x%x)\n",
  1681. phba->fcf.current_rec.fcf_indx,
  1682. bf_get(lpfc_fcf_record_fcf_index,
  1683. new_fcf_record));
  1684. goto out;
  1685. }
  1686. /*
  1687. * Read next FCF record from HBA searching for the matching
  1688. * with in-use record only if not during the fast failover
  1689. * period. In case of fast failover period, it shall try to
  1690. * determine whether the FCF record just read should be the
  1691. * next candidate.
  1692. */
  1693. if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
  1694. spin_unlock_irq(&phba->hbalock);
  1695. goto read_next_fcf;
  1696. }
  1697. }
  1698. /*
  1699. * Update on failover FCF record only if it's in FCF fast-failover
  1700. * period; otherwise, update on current FCF record.
  1701. */
  1702. if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
  1703. fcf_rec = &phba->fcf.failover_rec;
  1704. else
  1705. fcf_rec = &phba->fcf.current_rec;
  1706. if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
  1707. /*
  1708. * If the driver FCF record does not have boot flag
  1709. * set and new hba fcf record has boot flag set, use
  1710. * the new hba fcf record.
  1711. */
  1712. if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
  1713. /* Choose this FCF record */
  1714. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1715. "2837 Update current FCF record "
  1716. "(x%x) with new FCF record (x%x)\n",
  1717. fcf_rec->fcf_indx,
  1718. bf_get(lpfc_fcf_record_fcf_index,
  1719. new_fcf_record));
  1720. __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
  1721. addr_mode, vlan_id, BOOT_ENABLE);
  1722. spin_unlock_irq(&phba->hbalock);
  1723. goto read_next_fcf;
  1724. }
  1725. /*
  1726. * If the driver FCF record has boot flag set and the
  1727. * new hba FCF record does not have boot flag, read
  1728. * the next FCF record.
  1729. */
  1730. if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
  1731. spin_unlock_irq(&phba->hbalock);
  1732. goto read_next_fcf;
  1733. }
  1734. /*
  1735. * If the new hba FCF record has lower priority value
  1736. * than the driver FCF record, use the new record.
  1737. */
  1738. if (new_fcf_record->fip_priority < fcf_rec->priority) {
  1739. /* Choose the new FCF record with lower priority */
  1740. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1741. "2838 Update current FCF record "
  1742. "(x%x) with new FCF record (x%x)\n",
  1743. fcf_rec->fcf_indx,
  1744. bf_get(lpfc_fcf_record_fcf_index,
  1745. new_fcf_record));
  1746. __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
  1747. addr_mode, vlan_id, 0);
  1748. /* Reset running random FCF selection count */
  1749. phba->fcf.eligible_fcf_cnt = 1;
  1750. } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
  1751. /* Update running random FCF selection count */
  1752. phba->fcf.eligible_fcf_cnt++;
  1753. select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
  1754. phba->fcf.eligible_fcf_cnt);
  1755. if (select_new_fcf) {
  1756. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1757. "2839 Update current FCF record "
  1758. "(x%x) with new FCF record (x%x)\n",
  1759. fcf_rec->fcf_indx,
  1760. bf_get(lpfc_fcf_record_fcf_index,
  1761. new_fcf_record));
  1762. /* Choose the new FCF by random selection */
  1763. __lpfc_update_fcf_record(phba, fcf_rec,
  1764. new_fcf_record,
  1765. addr_mode, vlan_id, 0);
  1766. }
  1767. }
  1768. spin_unlock_irq(&phba->hbalock);
  1769. goto read_next_fcf;
  1770. }
  1771. /*
  1772. * This is the first suitable FCF record, choose this record for
  1773. * initial best-fit FCF.
  1774. */
  1775. if (fcf_rec) {
  1776. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1777. "2840 Update current FCF record "
  1778. "with initial FCF record (x%x)\n",
  1779. bf_get(lpfc_fcf_record_fcf_index,
  1780. new_fcf_record));
  1781. __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
  1782. addr_mode, vlan_id, (boot_flag ?
  1783. BOOT_ENABLE : 0));
  1784. phba->fcf.fcf_flag |= FCF_AVAILABLE;
  1785. /* Setup initial running random FCF selection count */
  1786. phba->fcf.eligible_fcf_cnt = 1;
  1787. /* Seeding the random number generator for random selection */
  1788. seed = (uint32_t)(0xFFFFFFFF & jiffies);
  1789. srandom32(seed);
  1790. }
  1791. spin_unlock_irq(&phba->hbalock);
  1792. goto read_next_fcf;
  1793. read_next_fcf:
  1794. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1795. if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
  1796. if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
  1797. /*
  1798. * Case of FCF fast failover scan
  1799. */
  1800. /*
  1801. * It has not found any suitable FCF record, cancel
  1802. * FCF scan inprogress, and do nothing
  1803. */
  1804. if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
  1805. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  1806. "2782 No suitable FCF record "
  1807. "found during this round of "
  1808. "post FCF rediscovery scan: "
  1809. "fcf_evt_tag:x%x, fcf_index: "
  1810. "x%x\n",
  1811. phba->fcoe_eventtag_at_fcf_scan,
  1812. bf_get(lpfc_fcf_record_fcf_index,
  1813. new_fcf_record));
  1814. /*
  1815. * Let next new FCF event trigger fast
  1816. * failover
  1817. */
  1818. spin_lock_irq(&phba->hbalock);
  1819. phba->hba_flag &= ~FCF_DISC_INPROGRESS;
  1820. spin_unlock_irq(&phba->hbalock);
  1821. return;
  1822. }
  1823. /*
  1824. * It has found a suitable FCF record that is not
  1825. * the same as in-use FCF record, unregister the
  1826. * in-use FCF record, replace the in-use FCF record
  1827. * with the new FCF record, mark FCF fast failover
  1828. * completed, and then start register the new FCF
  1829. * record.
  1830. */
  1831. /* Unregister the current in-use FCF record */
  1832. lpfc_unregister_fcf(phba);
  1833. /* Replace in-use record with the new record */
  1834. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1835. "2842 Replace the current in-use "
  1836. "FCF record (x%x) with failover FCF "
  1837. "record (x%x)\n",
  1838. phba->fcf.current_rec.fcf_indx,
  1839. phba->fcf.failover_rec.fcf_indx);
  1840. memcpy(&phba->fcf.current_rec,
  1841. &phba->fcf.failover_rec,
  1842. sizeof(struct lpfc_fcf_rec));
  1843. /*
  1844. * Mark the fast FCF failover rediscovery completed
  1845. * and the start of the first round of the roundrobin
  1846. * FCF failover.
  1847. */
  1848. spin_lock_irq(&phba->hbalock);
  1849. phba->fcf.fcf_flag &=
  1850. ~(FCF_REDISC_FOV | FCF_REDISC_RRU);
  1851. spin_unlock_irq(&phba->hbalock);
  1852. /*
  1853. * Set up the initial registered FCF index for FLOGI
  1854. * round robin FCF failover.
  1855. */
  1856. phba->fcf.fcf_rr_init_indx =
  1857. phba->fcf.failover_rec.fcf_indx;
  1858. /* Register to the new FCF record */
  1859. lpfc_register_fcf(phba);
  1860. } else {
  1861. /*
  1862. * In case of transaction period to fast FCF failover,
  1863. * do nothing when search to the end of the FCF table.
  1864. */
  1865. if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
  1866. (phba->fcf.fcf_flag & FCF_REDISC_PEND))
  1867. return;
  1868. if (phba->fcf.fcf_flag & FCF_IN_USE) {
  1869. /*
  1870. * In case the current in-use FCF record no
  1871. * longer existed during FCF discovery that
  1872. * was not triggered by fast FCF failover
  1873. * process, treat it as fast FCF failover.
  1874. */
  1875. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1876. "2841 In-use FCF record (x%x) "
  1877. "not reported, entering fast "
  1878. "FCF failover mode scanning.\n",
  1879. phba->fcf.current_rec.fcf_indx);
  1880. spin_lock_irq(&phba->hbalock);
  1881. phba->fcf.fcf_flag |= FCF_REDISC_FOV;
  1882. spin_unlock_irq(&phba->hbalock);
  1883. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1884. lpfc_sli4_fcf_scan_read_fcf_rec(phba,
  1885. LPFC_FCOE_FCF_GET_FIRST);
  1886. return;
  1887. }
  1888. /*
  1889. * Otherwise, initial scan or post linkdown rescan,
  1890. * register with the best FCF record found so far
  1891. * through the FCF scanning process.
  1892. */
  1893. /*
  1894. * Mark the initial FCF discovery completed and
  1895. * the start of the first round of the roundrobin
  1896. * FCF failover.
  1897. */
  1898. spin_lock_irq(&phba->hbalock);
  1899. phba->fcf.fcf_flag &=
  1900. ~(FCF_INIT_DISC | FCF_REDISC_RRU);
  1901. spin_unlock_irq(&phba->hbalock);
  1902. /*
  1903. * Set up the initial registered FCF index for FLOGI
  1904. * round robin FCF failover
  1905. */
  1906. phba->fcf.fcf_rr_init_indx =
  1907. phba->fcf.current_rec.fcf_indx;
  1908. /* Register to the new FCF record */
  1909. lpfc_register_fcf(phba);
  1910. }
  1911. } else
  1912. lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
  1913. return;
  1914. out:
  1915. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1916. lpfc_register_fcf(phba);
  1917. return;
  1918. }
  1919. /**
  1920. * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf round robin read_fcf mbox cmpl hdler
  1921. * @phba: pointer to lpfc hba data structure.
  1922. * @mboxq: pointer to mailbox object.
  1923. *
  1924. * This is the callback function for FLOGI failure round robin FCF failover
  1925. * read FCF record mailbox command from the eligible FCF record bmask for
  1926. * performing the failover. If the FCF read back is not valid/available, it
  1927. * fails through to retrying FLOGI to the currently registered FCF again.
  1928. * Otherwise, if the FCF read back is valid and available, it will set the
  1929. * newly read FCF record to the failover FCF record, unregister currently
  1930. * registered FCF record, copy the failover FCF record to the current
  1931. * FCF record, and then register the current FCF record before proceeding
  1932. * to trying FLOGI on the new failover FCF.
  1933. */
  1934. void
  1935. lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  1936. {
  1937. struct fcf_record *new_fcf_record;
  1938. uint32_t boot_flag, addr_mode;
  1939. uint16_t next_fcf_index;
  1940. uint16_t current_fcf_index;
  1941. uint16_t vlan_id;
  1942. /* If link state is not up, stop the round robin failover process */
  1943. if (phba->link_state < LPFC_LINK_UP) {
  1944. spin_lock_irq(&phba->hbalock);
  1945. phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
  1946. spin_unlock_irq(&phba->hbalock);
  1947. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1948. return;
  1949. }
  1950. /* Parse the FCF record from the non-embedded mailbox command */
  1951. new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
  1952. &next_fcf_index);
  1953. if (!new_fcf_record) {
  1954. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  1955. "2766 Mailbox command READ_FCF_RECORD "
  1956. "failed to retrieve a FCF record.\n");
  1957. goto out;
  1958. }
  1959. /* Get the needed parameters from FCF record */
  1960. lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
  1961. &addr_mode, &vlan_id);
  1962. /* Log the FCF record information if turned on */
  1963. lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
  1964. next_fcf_index);
  1965. /* Upload new FCF record to the failover FCF record */
  1966. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1967. "2834 Update the current FCF record (x%x) "
  1968. "with the next FCF record (x%x)\n",
  1969. phba->fcf.failover_rec.fcf_indx,
  1970. bf_get(lpfc_fcf_record_fcf_index, new_fcf_record));
  1971. spin_lock_irq(&phba->hbalock);
  1972. __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
  1973. new_fcf_record, addr_mode, vlan_id,
  1974. (boot_flag ? BOOT_ENABLE : 0));
  1975. spin_unlock_irq(&phba->hbalock);
  1976. current_fcf_index = phba->fcf.current_rec.fcf_indx;
  1977. /* Unregister the current in-use FCF record */
  1978. lpfc_unregister_fcf(phba);
  1979. /* Replace in-use record with the new record */
  1980. memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
  1981. sizeof(struct lpfc_fcf_rec));
  1982. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1983. "2783 FLOGI round robin FCF failover from FCF "
  1984. "(x%x) to FCF (x%x).\n",
  1985. current_fcf_index,
  1986. bf_get(lpfc_fcf_record_fcf_index, new_fcf_record));
  1987. out:
  1988. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1989. lpfc_register_fcf(phba);
  1990. }
  1991. /**
  1992. * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
  1993. * @phba: pointer to lpfc hba data structure.
  1994. * @mboxq: pointer to mailbox object.
  1995. *
  1996. * This is the callback function of read FCF record mailbox command for
  1997. * updating the eligible FCF bmask for FLOGI failure round robin FCF
  1998. * failover when a new FCF event happened. If the FCF read back is
  1999. * valid/available and it passes the connection list check, it updates
  2000. * the bmask for the eligible FCF record for round robin failover.
  2001. */
  2002. void
  2003. lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  2004. {
  2005. struct fcf_record *new_fcf_record;
  2006. uint32_t boot_flag, addr_mode;
  2007. uint16_t fcf_index, next_fcf_index;
  2008. uint16_t vlan_id;
  2009. int rc;
  2010. /* If link state is not up, no need to proceed */
  2011. if (phba->link_state < LPFC_LINK_UP)
  2012. goto out;
  2013. /* If FCF discovery period is over, no need to proceed */
  2014. if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
  2015. goto out;
  2016. /* Parse the FCF record from the non-embedded mailbox command */
  2017. new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
  2018. &next_fcf_index);
  2019. if (!new_fcf_record) {
  2020. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  2021. "2767 Mailbox command READ_FCF_RECORD "
  2022. "failed to retrieve a FCF record.\n");
  2023. goto out;
  2024. }
  2025. /* Check the connection list for eligibility */
  2026. rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
  2027. &addr_mode, &vlan_id);
  2028. /* Log the FCF record information if turned on */
  2029. lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
  2030. next_fcf_index);
  2031. if (!rc)
  2032. goto out;
  2033. /* Update the eligible FCF record index bmask */
  2034. fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
  2035. rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
  2036. out:
  2037. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  2038. }
  2039. /**
  2040. * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
  2041. * @phba: pointer to lpfc hba data structure.
  2042. * @mboxq: pointer to mailbox data structure.
  2043. *
  2044. * This function handles completion of init vpi mailbox command.
  2045. */
  2046. void
  2047. lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  2048. {
  2049. struct lpfc_vport *vport = mboxq->vport;
  2050. struct lpfc_nodelist *ndlp;
  2051. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2052. if (mboxq->u.mb.mbxStatus) {
  2053. lpfc_printf_vlog(vport, KERN_ERR,
  2054. LOG_MBOX,
  2055. "2609 Init VPI mailbox failed 0x%x\n",
  2056. mboxq->u.mb.mbxStatus);
  2057. mempool_free(mboxq, phba->mbox_mem_pool);
  2058. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2059. return;
  2060. }
  2061. spin_lock_irq(shost->host_lock);
  2062. vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
  2063. spin_unlock_irq(shost->host_lock);
  2064. /* If this port is physical port or FDISC is done, do reg_vpi */
  2065. if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
  2066. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  2067. if (!ndlp)
  2068. lpfc_printf_vlog(vport, KERN_ERR,
  2069. LOG_DISCOVERY,
  2070. "2731 Cannot find fabric "
  2071. "controller node\n");
  2072. else
  2073. lpfc_register_new_vport(phba, vport, ndlp);
  2074. mempool_free(mboxq, phba->mbox_mem_pool);
  2075. return;
  2076. }
  2077. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
  2078. lpfc_initial_fdisc(vport);
  2079. else {
  2080. lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
  2081. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  2082. "2606 No NPIV Fabric support\n");
  2083. }
  2084. mempool_free(mboxq, phba->mbox_mem_pool);
  2085. return;
  2086. }
  2087. /**
  2088. * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
  2089. * @vport: pointer to lpfc_vport data structure.
  2090. *
  2091. * This function issue a init_vpi mailbox command to initialize
  2092. * VPI for the vport.
  2093. */
  2094. void
  2095. lpfc_issue_init_vpi(struct lpfc_vport *vport)
  2096. {
  2097. LPFC_MBOXQ_t *mboxq;
  2098. int rc;
  2099. mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
  2100. if (!mboxq) {
  2101. lpfc_printf_vlog(vport, KERN_ERR,
  2102. LOG_MBOX, "2607 Failed to allocate "
  2103. "init_vpi mailbox\n");
  2104. return;
  2105. }
  2106. lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
  2107. mboxq->vport = vport;
  2108. mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
  2109. rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
  2110. if (rc == MBX_NOT_FINISHED) {
  2111. lpfc_printf_vlog(vport, KERN_ERR,
  2112. LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
  2113. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  2114. }
  2115. }
  2116. /**
  2117. * lpfc_start_fdiscs - send fdiscs for each vports on this port.
  2118. * @phba: pointer to lpfc hba data structure.
  2119. *
  2120. * This function loops through the list of vports on the @phba and issues an
  2121. * FDISC if possible.
  2122. */
  2123. void
  2124. lpfc_start_fdiscs(struct lpfc_hba *phba)
  2125. {
  2126. struct lpfc_vport **vports;
  2127. int i;
  2128. vports = lpfc_create_vport_work_array(phba);
  2129. if (vports != NULL) {
  2130. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  2131. if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
  2132. continue;
  2133. /* There are no vpi for this vport */
  2134. if (vports[i]->vpi > phba->max_vpi) {
  2135. lpfc_vport_set_state(vports[i],
  2136. FC_VPORT_FAILED);
  2137. continue;
  2138. }
  2139. if (phba->fc_topology == TOPOLOGY_LOOP) {
  2140. lpfc_vport_set_state(vports[i],
  2141. FC_VPORT_LINKDOWN);
  2142. continue;
  2143. }
  2144. if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
  2145. lpfc_issue_init_vpi(vports[i]);
  2146. continue;
  2147. }
  2148. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
  2149. lpfc_initial_fdisc(vports[i]);
  2150. else {
  2151. lpfc_vport_set_state(vports[i],
  2152. FC_VPORT_NO_FABRIC_SUPP);
  2153. lpfc_printf_vlog(vports[i], KERN_ERR,
  2154. LOG_ELS,
  2155. "0259 No NPIV "
  2156. "Fabric support\n");
  2157. }
  2158. }
  2159. }
  2160. lpfc_destroy_vport_work_array(phba, vports);
  2161. }
  2162. void
  2163. lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  2164. {
  2165. struct lpfc_dmabuf *dmabuf = mboxq->context1;
  2166. struct lpfc_vport *vport = mboxq->vport;
  2167. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2168. if (mboxq->u.mb.mbxStatus) {
  2169. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  2170. "2018 REG_VFI mbxStatus error x%x "
  2171. "HBA state x%x\n",
  2172. mboxq->u.mb.mbxStatus, vport->port_state);
  2173. if (phba->fc_topology == TOPOLOGY_LOOP) {
  2174. /* FLOGI failed, use loop map to make discovery list */
  2175. lpfc_disc_list_loopmap(vport);
  2176. /* Start discovery */
  2177. lpfc_disc_start(vport);
  2178. goto fail_free_mem;
  2179. }
  2180. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2181. goto fail_free_mem;
  2182. }
  2183. /* The VPI is implicitly registered when the VFI is registered */
  2184. spin_lock_irq(shost->host_lock);
  2185. vport->vpi_state |= LPFC_VPI_REGISTERED;
  2186. vport->fc_flag |= FC_VFI_REGISTERED;
  2187. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  2188. spin_unlock_irq(shost->host_lock);
  2189. if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
  2190. lpfc_start_fdiscs(phba);
  2191. lpfc_do_scr_ns_plogi(phba, vport);
  2192. }
  2193. fail_free_mem:
  2194. mempool_free(mboxq, phba->mbox_mem_pool);
  2195. lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
  2196. kfree(dmabuf);
  2197. return;
  2198. }
  2199. static void
  2200. lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2201. {
  2202. MAILBOX_t *mb = &pmb->u.mb;
  2203. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
  2204. struct lpfc_vport *vport = pmb->vport;
  2205. /* Check for error */
  2206. if (mb->mbxStatus) {
  2207. /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
  2208. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  2209. "0319 READ_SPARAM mbxStatus error x%x "
  2210. "hba state x%x>\n",
  2211. mb->mbxStatus, vport->port_state);
  2212. lpfc_linkdown(phba);
  2213. goto out;
  2214. }
  2215. memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
  2216. sizeof (struct serv_parm));
  2217. if (phba->cfg_soft_wwnn)
  2218. u64_to_wwn(phba->cfg_soft_wwnn,
  2219. vport->fc_sparam.nodeName.u.wwn);
  2220. if (phba->cfg_soft_wwpn)
  2221. u64_to_wwn(phba->cfg_soft_wwpn,
  2222. vport->fc_sparam.portName.u.wwn);
  2223. memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
  2224. sizeof(vport->fc_nodename));
  2225. memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
  2226. sizeof(vport->fc_portname));
  2227. if (vport->port_type == LPFC_PHYSICAL_PORT) {
  2228. memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
  2229. memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
  2230. }
  2231. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2232. kfree(mp);
  2233. mempool_free(pmb, phba->mbox_mem_pool);
  2234. return;
  2235. out:
  2236. pmb->context1 = NULL;
  2237. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2238. kfree(mp);
  2239. lpfc_issue_clear_la(phba, vport);
  2240. mempool_free(pmb, phba->mbox_mem_pool);
  2241. return;
  2242. }
  2243. static void
  2244. lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
  2245. {
  2246. struct lpfc_vport *vport = phba->pport;
  2247. LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
  2248. int i;
  2249. struct lpfc_dmabuf *mp;
  2250. int rc;
  2251. struct fcf_record *fcf_record;
  2252. spin_lock_irq(&phba->hbalock);
  2253. switch (la->UlnkSpeed) {
  2254. case LA_1GHZ_LINK:
  2255. phba->fc_linkspeed = LA_1GHZ_LINK;
  2256. break;
  2257. case LA_2GHZ_LINK:
  2258. phba->fc_linkspeed = LA_2GHZ_LINK;
  2259. break;
  2260. case LA_4GHZ_LINK:
  2261. phba->fc_linkspeed = LA_4GHZ_LINK;
  2262. break;
  2263. case LA_8GHZ_LINK:
  2264. phba->fc_linkspeed = LA_8GHZ_LINK;
  2265. break;
  2266. case LA_10GHZ_LINK:
  2267. phba->fc_linkspeed = LA_10GHZ_LINK;
  2268. break;
  2269. default:
  2270. phba->fc_linkspeed = LA_UNKNW_LINK;
  2271. break;
  2272. }
  2273. phba->fc_topology = la->topology;
  2274. phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
  2275. if (phba->fc_topology == TOPOLOGY_LOOP) {
  2276. phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
  2277. /* if npiv is enabled and this adapter supports npiv log
  2278. * a message that npiv is not supported in this topology
  2279. */
  2280. if (phba->cfg_enable_npiv && phba->max_vpi)
  2281. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2282. "1309 Link Up Event npiv not supported in loop "
  2283. "topology\n");
  2284. /* Get Loop Map information */
  2285. if (la->il)
  2286. vport->fc_flag |= FC_LBIT;
  2287. vport->fc_myDID = la->granted_AL_PA;
  2288. i = la->un.lilpBde64.tus.f.bdeSize;
  2289. if (i == 0) {
  2290. phba->alpa_map[0] = 0;
  2291. } else {
  2292. if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
  2293. int numalpa, j, k;
  2294. union {
  2295. uint8_t pamap[16];
  2296. struct {
  2297. uint32_t wd1;
  2298. uint32_t wd2;
  2299. uint32_t wd3;
  2300. uint32_t wd4;
  2301. } pa;
  2302. } un;
  2303. numalpa = phba->alpa_map[0];
  2304. j = 0;
  2305. while (j < numalpa) {
  2306. memset(un.pamap, 0, 16);
  2307. for (k = 1; j < numalpa; k++) {
  2308. un.pamap[k - 1] =
  2309. phba->alpa_map[j + 1];
  2310. j++;
  2311. if (k == 16)
  2312. break;
  2313. }
  2314. /* Link Up Event ALPA map */
  2315. lpfc_printf_log(phba,
  2316. KERN_WARNING,
  2317. LOG_LINK_EVENT,
  2318. "1304 Link Up Event "
  2319. "ALPA map Data: x%x "
  2320. "x%x x%x x%x\n",
  2321. un.pa.wd1, un.pa.wd2,
  2322. un.pa.wd3, un.pa.wd4);
  2323. }
  2324. }
  2325. }
  2326. } else {
  2327. if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
  2328. if (phba->max_vpi && phba->cfg_enable_npiv &&
  2329. (phba->sli_rev == 3))
  2330. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  2331. }
  2332. vport->fc_myDID = phba->fc_pref_DID;
  2333. vport->fc_flag |= FC_LBIT;
  2334. }
  2335. spin_unlock_irq(&phba->hbalock);
  2336. lpfc_linkup(phba);
  2337. sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2338. if (!sparam_mbox)
  2339. goto out;
  2340. rc = lpfc_read_sparam(phba, sparam_mbox, 0);
  2341. if (rc) {
  2342. mempool_free(sparam_mbox, phba->mbox_mem_pool);
  2343. goto out;
  2344. }
  2345. sparam_mbox->vport = vport;
  2346. sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
  2347. rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
  2348. if (rc == MBX_NOT_FINISHED) {
  2349. mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
  2350. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2351. kfree(mp);
  2352. mempool_free(sparam_mbox, phba->mbox_mem_pool);
  2353. goto out;
  2354. }
  2355. if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
  2356. cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2357. if (!cfglink_mbox)
  2358. goto out;
  2359. vport->port_state = LPFC_LOCAL_CFG_LINK;
  2360. lpfc_config_link(phba, cfglink_mbox);
  2361. cfglink_mbox->vport = vport;
  2362. cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
  2363. rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
  2364. if (rc == MBX_NOT_FINISHED) {
  2365. mempool_free(cfglink_mbox, phba->mbox_mem_pool);
  2366. goto out;
  2367. }
  2368. } else {
  2369. vport->port_state = LPFC_VPORT_UNKNOWN;
  2370. /*
  2371. * Add the driver's default FCF record at FCF index 0 now. This
  2372. * is phase 1 implementation that support FCF index 0 and driver
  2373. * defaults.
  2374. */
  2375. if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
  2376. fcf_record = kzalloc(sizeof(struct fcf_record),
  2377. GFP_KERNEL);
  2378. if (unlikely(!fcf_record)) {
  2379. lpfc_printf_log(phba, KERN_ERR,
  2380. LOG_MBOX | LOG_SLI,
  2381. "2554 Could not allocate memmory for "
  2382. "fcf record\n");
  2383. rc = -ENODEV;
  2384. goto out;
  2385. }
  2386. lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
  2387. LPFC_FCOE_FCF_DEF_INDEX);
  2388. rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
  2389. if (unlikely(rc)) {
  2390. lpfc_printf_log(phba, KERN_ERR,
  2391. LOG_MBOX | LOG_SLI,
  2392. "2013 Could not manually add FCF "
  2393. "record 0, status %d\n", rc);
  2394. rc = -ENODEV;
  2395. kfree(fcf_record);
  2396. goto out;
  2397. }
  2398. kfree(fcf_record);
  2399. }
  2400. /*
  2401. * The driver is expected to do FIP/FCF. Call the port
  2402. * and get the FCF Table.
  2403. */
  2404. spin_lock_irq(&phba->hbalock);
  2405. if (phba->hba_flag & FCF_DISC_INPROGRESS) {
  2406. spin_unlock_irq(&phba->hbalock);
  2407. return;
  2408. }
  2409. /* This is the initial FCF discovery scan */
  2410. phba->fcf.fcf_flag |= FCF_INIT_DISC;
  2411. spin_unlock_irq(&phba->hbalock);
  2412. lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
  2413. "2778 Start FCF table scan at linkup\n");
  2414. rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
  2415. LPFC_FCOE_FCF_GET_FIRST);
  2416. if (rc) {
  2417. spin_lock_irq(&phba->hbalock);
  2418. phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
  2419. spin_unlock_irq(&phba->hbalock);
  2420. goto out;
  2421. }
  2422. /* Reset FCF roundrobin bmask for new discovery */
  2423. memset(phba->fcf.fcf_rr_bmask, 0,
  2424. sizeof(*phba->fcf.fcf_rr_bmask));
  2425. }
  2426. return;
  2427. out:
  2428. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2429. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  2430. "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
  2431. vport->port_state, sparam_mbox, cfglink_mbox);
  2432. lpfc_issue_clear_la(phba, vport);
  2433. return;
  2434. }
  2435. static void
  2436. lpfc_enable_la(struct lpfc_hba *phba)
  2437. {
  2438. uint32_t control;
  2439. struct lpfc_sli *psli = &phba->sli;
  2440. spin_lock_irq(&phba->hbalock);
  2441. psli->sli_flag |= LPFC_PROCESS_LA;
  2442. if (phba->sli_rev <= LPFC_SLI_REV3) {
  2443. control = readl(phba->HCregaddr);
  2444. control |= HC_LAINT_ENA;
  2445. writel(control, phba->HCregaddr);
  2446. readl(phba->HCregaddr); /* flush */
  2447. }
  2448. spin_unlock_irq(&phba->hbalock);
  2449. }
  2450. static void
  2451. lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
  2452. {
  2453. lpfc_linkdown(phba);
  2454. lpfc_enable_la(phba);
  2455. lpfc_unregister_unused_fcf(phba);
  2456. /* turn on Link Attention interrupts - no CLEAR_LA needed */
  2457. }
  2458. /*
  2459. * This routine handles processing a READ_LA mailbox
  2460. * command upon completion. It is setup in the LPFC_MBOXQ
  2461. * as the completion routine when the command is
  2462. * handed off to the SLI layer.
  2463. */
  2464. void
  2465. lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2466. {
  2467. struct lpfc_vport *vport = pmb->vport;
  2468. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2469. READ_LA_VAR *la;
  2470. MAILBOX_t *mb = &pmb->u.mb;
  2471. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  2472. /* Unblock ELS traffic */
  2473. phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
  2474. /* Check for error */
  2475. if (mb->mbxStatus) {
  2476. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  2477. "1307 READ_LA mbox error x%x state x%x\n",
  2478. mb->mbxStatus, vport->port_state);
  2479. lpfc_mbx_issue_link_down(phba);
  2480. phba->link_state = LPFC_HBA_ERROR;
  2481. goto lpfc_mbx_cmpl_read_la_free_mbuf;
  2482. }
  2483. la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
  2484. memcpy(&phba->alpa_map[0], mp->virt, 128);
  2485. spin_lock_irq(shost->host_lock);
  2486. if (la->pb)
  2487. vport->fc_flag |= FC_BYPASSED_MODE;
  2488. else
  2489. vport->fc_flag &= ~FC_BYPASSED_MODE;
  2490. spin_unlock_irq(shost->host_lock);
  2491. if ((phba->fc_eventTag < la->eventTag) ||
  2492. (phba->fc_eventTag == la->eventTag)) {
  2493. phba->fc_stat.LinkMultiEvent++;
  2494. if (la->attType == AT_LINK_UP)
  2495. if (phba->fc_eventTag != 0)
  2496. lpfc_linkdown(phba);
  2497. }
  2498. phba->fc_eventTag = la->eventTag;
  2499. spin_lock_irq(&phba->hbalock);
  2500. if (la->mm)
  2501. phba->sli.sli_flag |= LPFC_MENLO_MAINT;
  2502. else
  2503. phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
  2504. spin_unlock_irq(&phba->hbalock);
  2505. phba->link_events++;
  2506. if (la->attType == AT_LINK_UP && (!la->mm)) {
  2507. phba->fc_stat.LinkUp++;
  2508. if (phba->link_flag & LS_LOOPBACK_MODE) {
  2509. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2510. "1306 Link Up Event in loop back mode "
  2511. "x%x received Data: x%x x%x x%x x%x\n",
  2512. la->eventTag, phba->fc_eventTag,
  2513. la->granted_AL_PA, la->UlnkSpeed,
  2514. phba->alpa_map[0]);
  2515. } else {
  2516. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2517. "1303 Link Up Event x%x received "
  2518. "Data: x%x x%x x%x x%x x%x x%x %d\n",
  2519. la->eventTag, phba->fc_eventTag,
  2520. la->granted_AL_PA, la->UlnkSpeed,
  2521. phba->alpa_map[0],
  2522. la->mm, la->fa,
  2523. phba->wait_4_mlo_maint_flg);
  2524. }
  2525. lpfc_mbx_process_link_up(phba, la);
  2526. } else if (la->attType == AT_LINK_DOWN) {
  2527. phba->fc_stat.LinkDown++;
  2528. if (phba->link_flag & LS_LOOPBACK_MODE) {
  2529. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2530. "1308 Link Down Event in loop back mode "
  2531. "x%x received "
  2532. "Data: x%x x%x x%x\n",
  2533. la->eventTag, phba->fc_eventTag,
  2534. phba->pport->port_state, vport->fc_flag);
  2535. }
  2536. else {
  2537. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2538. "1305 Link Down Event x%x received "
  2539. "Data: x%x x%x x%x x%x x%x\n",
  2540. la->eventTag, phba->fc_eventTag,
  2541. phba->pport->port_state, vport->fc_flag,
  2542. la->mm, la->fa);
  2543. }
  2544. lpfc_mbx_issue_link_down(phba);
  2545. }
  2546. if (la->mm && la->attType == AT_LINK_UP) {
  2547. if (phba->link_state != LPFC_LINK_DOWN) {
  2548. phba->fc_stat.LinkDown++;
  2549. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2550. "1312 Link Down Event x%x received "
  2551. "Data: x%x x%x x%x\n",
  2552. la->eventTag, phba->fc_eventTag,
  2553. phba->pport->port_state, vport->fc_flag);
  2554. lpfc_mbx_issue_link_down(phba);
  2555. } else
  2556. lpfc_enable_la(phba);
  2557. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2558. "1310 Menlo Maint Mode Link up Event x%x rcvd "
  2559. "Data: x%x x%x x%x\n",
  2560. la->eventTag, phba->fc_eventTag,
  2561. phba->pport->port_state, vport->fc_flag);
  2562. /*
  2563. * The cmnd that triggered this will be waiting for this
  2564. * signal.
  2565. */
  2566. /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
  2567. if (phba->wait_4_mlo_maint_flg) {
  2568. phba->wait_4_mlo_maint_flg = 0;
  2569. wake_up_interruptible(&phba->wait_4_mlo_m_q);
  2570. }
  2571. }
  2572. if (la->fa) {
  2573. if (la->mm)
  2574. lpfc_issue_clear_la(phba, vport);
  2575. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  2576. "1311 fa %d\n", la->fa);
  2577. }
  2578. lpfc_mbx_cmpl_read_la_free_mbuf:
  2579. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2580. kfree(mp);
  2581. mempool_free(pmb, phba->mbox_mem_pool);
  2582. return;
  2583. }
  2584. /*
  2585. * This routine handles processing a REG_LOGIN mailbox
  2586. * command upon completion. It is setup in the LPFC_MBOXQ
  2587. * as the completion routine when the command is
  2588. * handed off to the SLI layer.
  2589. */
  2590. void
  2591. lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2592. {
  2593. struct lpfc_vport *vport = pmb->vport;
  2594. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  2595. struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
  2596. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2597. pmb->context1 = NULL;
  2598. if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
  2599. ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
  2600. if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
  2601. ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
  2602. /* We rcvd a rscn after issuing this
  2603. * mbox reg login, we may have cycled
  2604. * back through the state and be
  2605. * back at reg login state so this
  2606. * mbox needs to be ignored becase
  2607. * there is another reg login in
  2608. * proccess.
  2609. */
  2610. spin_lock_irq(shost->host_lock);
  2611. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  2612. spin_unlock_irq(shost->host_lock);
  2613. if (phba->sli_rev == LPFC_SLI_REV4)
  2614. lpfc_sli4_free_rpi(phba,
  2615. pmb->u.mb.un.varRegLogin.rpi);
  2616. } else
  2617. /* Good status, call state machine */
  2618. lpfc_disc_state_machine(vport, ndlp, pmb,
  2619. NLP_EVT_CMPL_REG_LOGIN);
  2620. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2621. kfree(mp);
  2622. mempool_free(pmb, phba->mbox_mem_pool);
  2623. /* decrement the node reference count held for this callback
  2624. * function.
  2625. */
  2626. lpfc_nlp_put(ndlp);
  2627. return;
  2628. }
  2629. static void
  2630. lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2631. {
  2632. MAILBOX_t *mb = &pmb->u.mb;
  2633. struct lpfc_vport *vport = pmb->vport;
  2634. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2635. switch (mb->mbxStatus) {
  2636. case 0x0011:
  2637. case 0x0020:
  2638. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  2639. "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
  2640. mb->mbxStatus);
  2641. break;
  2642. /* If VPI is busy, reset the HBA */
  2643. case 0x9700:
  2644. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
  2645. "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
  2646. vport->vpi, mb->mbxStatus);
  2647. if (!(phba->pport->load_flag & FC_UNLOADING))
  2648. lpfc_workq_post_event(phba, NULL, NULL,
  2649. LPFC_EVT_RESET_HBA);
  2650. }
  2651. spin_lock_irq(shost->host_lock);
  2652. vport->vpi_state &= ~LPFC_VPI_REGISTERED;
  2653. vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  2654. spin_unlock_irq(shost->host_lock);
  2655. vport->unreg_vpi_cmpl = VPORT_OK;
  2656. mempool_free(pmb, phba->mbox_mem_pool);
  2657. /*
  2658. * This shost reference might have been taken at the beginning of
  2659. * lpfc_vport_delete()
  2660. */
  2661. if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
  2662. scsi_host_put(shost);
  2663. }
  2664. int
  2665. lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
  2666. {
  2667. struct lpfc_hba *phba = vport->phba;
  2668. LPFC_MBOXQ_t *mbox;
  2669. int rc;
  2670. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2671. if (!mbox)
  2672. return 1;
  2673. lpfc_unreg_vpi(phba, vport->vpi, mbox);
  2674. mbox->vport = vport;
  2675. mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
  2676. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  2677. if (rc == MBX_NOT_FINISHED) {
  2678. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
  2679. "1800 Could not issue unreg_vpi\n");
  2680. mempool_free(mbox, phba->mbox_mem_pool);
  2681. vport->unreg_vpi_cmpl = VPORT_ERROR;
  2682. return rc;
  2683. }
  2684. return 0;
  2685. }
  2686. static void
  2687. lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2688. {
  2689. struct lpfc_vport *vport = pmb->vport;
  2690. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2691. MAILBOX_t *mb = &pmb->u.mb;
  2692. switch (mb->mbxStatus) {
  2693. case 0x0011:
  2694. case 0x9601:
  2695. case 0x9602:
  2696. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  2697. "0912 cmpl_reg_vpi, mb status = 0x%x\n",
  2698. mb->mbxStatus);
  2699. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2700. spin_lock_irq(shost->host_lock);
  2701. vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
  2702. spin_unlock_irq(shost->host_lock);
  2703. vport->fc_myDID = 0;
  2704. goto out;
  2705. }
  2706. spin_lock_irq(shost->host_lock);
  2707. vport->vpi_state |= LPFC_VPI_REGISTERED;
  2708. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  2709. spin_unlock_irq(shost->host_lock);
  2710. vport->num_disc_nodes = 0;
  2711. /* go thru NPR list and issue ELS PLOGIs */
  2712. if (vport->fc_npr_cnt)
  2713. lpfc_els_disc_plogi(vport);
  2714. if (!vport->num_disc_nodes) {
  2715. spin_lock_irq(shost->host_lock);
  2716. vport->fc_flag &= ~FC_NDISC_ACTIVE;
  2717. spin_unlock_irq(shost->host_lock);
  2718. lpfc_can_disctmo(vport);
  2719. }
  2720. vport->port_state = LPFC_VPORT_READY;
  2721. out:
  2722. mempool_free(pmb, phba->mbox_mem_pool);
  2723. return;
  2724. }
  2725. /**
  2726. * lpfc_create_static_vport - Read HBA config region to create static vports.
  2727. * @phba: pointer to lpfc hba data structure.
  2728. *
  2729. * This routine issue a DUMP mailbox command for config region 22 to get
  2730. * the list of static vports to be created. The function create vports
  2731. * based on the information returned from the HBA.
  2732. **/
  2733. void
  2734. lpfc_create_static_vport(struct lpfc_hba *phba)
  2735. {
  2736. LPFC_MBOXQ_t *pmb = NULL;
  2737. MAILBOX_t *mb;
  2738. struct static_vport_info *vport_info;
  2739. int rc = 0, i;
  2740. struct fc_vport_identifiers vport_id;
  2741. struct fc_vport *new_fc_vport;
  2742. struct Scsi_Host *shost;
  2743. struct lpfc_vport *vport;
  2744. uint16_t offset = 0;
  2745. uint8_t *vport_buff;
  2746. struct lpfc_dmabuf *mp;
  2747. uint32_t byte_count = 0;
  2748. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2749. if (!pmb) {
  2750. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2751. "0542 lpfc_create_static_vport failed to"
  2752. " allocate mailbox memory\n");
  2753. return;
  2754. }
  2755. mb = &pmb->u.mb;
  2756. vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
  2757. if (!vport_info) {
  2758. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2759. "0543 lpfc_create_static_vport failed to"
  2760. " allocate vport_info\n");
  2761. mempool_free(pmb, phba->mbox_mem_pool);
  2762. return;
  2763. }
  2764. vport_buff = (uint8_t *) vport_info;
  2765. do {
  2766. if (lpfc_dump_static_vport(phba, pmb, offset))
  2767. goto out;
  2768. pmb->vport = phba->pport;
  2769. rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
  2770. if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
  2771. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  2772. "0544 lpfc_create_static_vport failed to"
  2773. " issue dump mailbox command ret 0x%x "
  2774. "status 0x%x\n",
  2775. rc, mb->mbxStatus);
  2776. goto out;
  2777. }
  2778. if (phba->sli_rev == LPFC_SLI_REV4) {
  2779. byte_count = pmb->u.mqe.un.mb_words[5];
  2780. mp = (struct lpfc_dmabuf *) pmb->context2;
  2781. if (byte_count > sizeof(struct static_vport_info) -
  2782. offset)
  2783. byte_count = sizeof(struct static_vport_info)
  2784. - offset;
  2785. memcpy(vport_buff + offset, mp->virt, byte_count);
  2786. offset += byte_count;
  2787. } else {
  2788. if (mb->un.varDmp.word_cnt >
  2789. sizeof(struct static_vport_info) - offset)
  2790. mb->un.varDmp.word_cnt =
  2791. sizeof(struct static_vport_info)
  2792. - offset;
  2793. byte_count = mb->un.varDmp.word_cnt;
  2794. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  2795. vport_buff + offset,
  2796. byte_count);
  2797. offset += byte_count;
  2798. }
  2799. } while (byte_count &&
  2800. offset < sizeof(struct static_vport_info));
  2801. if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
  2802. ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
  2803. != VPORT_INFO_REV)) {
  2804. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2805. "0545 lpfc_create_static_vport bad"
  2806. " information header 0x%x 0x%x\n",
  2807. le32_to_cpu(vport_info->signature),
  2808. le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
  2809. goto out;
  2810. }
  2811. shost = lpfc_shost_from_vport(phba->pport);
  2812. for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
  2813. memset(&vport_id, 0, sizeof(vport_id));
  2814. vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
  2815. vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
  2816. if (!vport_id.port_name || !vport_id.node_name)
  2817. continue;
  2818. vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
  2819. vport_id.vport_type = FC_PORTTYPE_NPIV;
  2820. vport_id.disable = false;
  2821. new_fc_vport = fc_vport_create(shost, 0, &vport_id);
  2822. if (!new_fc_vport) {
  2823. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  2824. "0546 lpfc_create_static_vport failed to"
  2825. " create vport\n");
  2826. continue;
  2827. }
  2828. vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
  2829. vport->vport_flag |= STATIC_VPORT;
  2830. }
  2831. out:
  2832. kfree(vport_info);
  2833. if (rc != MBX_TIMEOUT) {
  2834. if (pmb->context2) {
  2835. mp = (struct lpfc_dmabuf *) pmb->context2;
  2836. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2837. kfree(mp);
  2838. }
  2839. mempool_free(pmb, phba->mbox_mem_pool);
  2840. }
  2841. return;
  2842. }
  2843. /*
  2844. * This routine handles processing a Fabric REG_LOGIN mailbox
  2845. * command upon completion. It is setup in the LPFC_MBOXQ
  2846. * as the completion routine when the command is
  2847. * handed off to the SLI layer.
  2848. */
  2849. void
  2850. lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2851. {
  2852. struct lpfc_vport *vport = pmb->vport;
  2853. MAILBOX_t *mb = &pmb->u.mb;
  2854. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  2855. struct lpfc_nodelist *ndlp;
  2856. ndlp = (struct lpfc_nodelist *) pmb->context2;
  2857. pmb->context1 = NULL;
  2858. pmb->context2 = NULL;
  2859. if (mb->mbxStatus) {
  2860. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  2861. "0258 Register Fabric login error: 0x%x\n",
  2862. mb->mbxStatus);
  2863. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2864. kfree(mp);
  2865. mempool_free(pmb, phba->mbox_mem_pool);
  2866. if (phba->fc_topology == TOPOLOGY_LOOP) {
  2867. /* FLOGI failed, use loop map to make discovery list */
  2868. lpfc_disc_list_loopmap(vport);
  2869. /* Start discovery */
  2870. lpfc_disc_start(vport);
  2871. /* Decrement the reference count to ndlp after the
  2872. * reference to the ndlp are done.
  2873. */
  2874. lpfc_nlp_put(ndlp);
  2875. return;
  2876. }
  2877. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2878. /* Decrement the reference count to ndlp after the reference
  2879. * to the ndlp are done.
  2880. */
  2881. lpfc_nlp_put(ndlp);
  2882. return;
  2883. }
  2884. ndlp->nlp_rpi = mb->un.varWords[0];
  2885. ndlp->nlp_flag |= NLP_RPI_VALID;
  2886. ndlp->nlp_type |= NLP_FABRIC;
  2887. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  2888. if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
  2889. /* when physical port receive logo donot start
  2890. * vport discovery */
  2891. if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
  2892. lpfc_start_fdiscs(phba);
  2893. else
  2894. vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
  2895. lpfc_do_scr_ns_plogi(phba, vport);
  2896. }
  2897. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2898. kfree(mp);
  2899. mempool_free(pmb, phba->mbox_mem_pool);
  2900. /* Drop the reference count from the mbox at the end after
  2901. * all the current reference to the ndlp have been done.
  2902. */
  2903. lpfc_nlp_put(ndlp);
  2904. return;
  2905. }
  2906. /*
  2907. * This routine handles processing a NameServer REG_LOGIN mailbox
  2908. * command upon completion. It is setup in the LPFC_MBOXQ
  2909. * as the completion routine when the command is
  2910. * handed off to the SLI layer.
  2911. */
  2912. void
  2913. lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2914. {
  2915. MAILBOX_t *mb = &pmb->u.mb;
  2916. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  2917. struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
  2918. struct lpfc_vport *vport = pmb->vport;
  2919. if (mb->mbxStatus) {
  2920. out:
  2921. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  2922. "0260 Register NameServer error: 0x%x\n",
  2923. mb->mbxStatus);
  2924. /* decrement the node reference count held for this
  2925. * callback function.
  2926. */
  2927. lpfc_nlp_put(ndlp);
  2928. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2929. kfree(mp);
  2930. mempool_free(pmb, phba->mbox_mem_pool);
  2931. /* If no other thread is using the ndlp, free it */
  2932. lpfc_nlp_not_used(ndlp);
  2933. if (phba->fc_topology == TOPOLOGY_LOOP) {
  2934. /*
  2935. * RegLogin failed, use loop map to make discovery
  2936. * list
  2937. */
  2938. lpfc_disc_list_loopmap(vport);
  2939. /* Start discovery */
  2940. lpfc_disc_start(vport);
  2941. return;
  2942. }
  2943. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2944. return;
  2945. }
  2946. pmb->context1 = NULL;
  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. ndlp->nlp_rpi = mb->un.varWords[0];
  4302. ndlp->nlp_flag |= NLP_RPI_VALID;
  4303. ndlp->nlp_type |= NLP_FABRIC;
  4304. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  4305. /*
  4306. * Start issuing Fabric-Device Management Interface (FDMI) command to
  4307. * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
  4308. * fdmi-on=2 (supporting RPA/hostnmae)
  4309. */
  4310. if (vport->cfg_fdmi_on == 1)
  4311. lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
  4312. else
  4313. mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
  4314. /* decrement the node reference count held for this callback
  4315. * function.
  4316. */
  4317. lpfc_nlp_put(ndlp);
  4318. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4319. kfree(mp);
  4320. mempool_free(pmb, phba->mbox_mem_pool);
  4321. return;
  4322. }
  4323. static int
  4324. lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
  4325. {
  4326. uint16_t *rpi = param;
  4327. return ndlp->nlp_rpi == *rpi;
  4328. }
  4329. static int
  4330. lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
  4331. {
  4332. return memcmp(&ndlp->nlp_portname, param,
  4333. sizeof(ndlp->nlp_portname)) == 0;
  4334. }
  4335. static struct lpfc_nodelist *
  4336. __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
  4337. {
  4338. struct lpfc_nodelist *ndlp;
  4339. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  4340. if (filter(ndlp, param))
  4341. return ndlp;
  4342. }
  4343. return NULL;
  4344. }
  4345. /*
  4346. * This routine looks up the ndlp lists for the given RPI. If rpi found it
  4347. * returns the node list element pointer else return NULL.
  4348. */
  4349. struct lpfc_nodelist *
  4350. __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
  4351. {
  4352. return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
  4353. }
  4354. /*
  4355. * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
  4356. * returns the node element list pointer else return NULL.
  4357. */
  4358. struct lpfc_nodelist *
  4359. lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
  4360. {
  4361. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  4362. struct lpfc_nodelist *ndlp;
  4363. spin_lock_irq(shost->host_lock);
  4364. ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
  4365. spin_unlock_irq(shost->host_lock);
  4366. return ndlp;
  4367. }
  4368. void
  4369. lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  4370. uint32_t did)
  4371. {
  4372. memset(ndlp, 0, sizeof (struct lpfc_nodelist));
  4373. lpfc_initialize_node(vport, ndlp, did);
  4374. INIT_LIST_HEAD(&ndlp->nlp_listp);
  4375. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
  4376. "node init: did:x%x",
  4377. ndlp->nlp_DID, 0, 0);
  4378. return;
  4379. }
  4380. /* This routine releases all resources associated with a specifc NPort's ndlp
  4381. * and mempool_free's the nodelist.
  4382. */
  4383. static void
  4384. lpfc_nlp_release(struct kref *kref)
  4385. {
  4386. struct lpfc_hba *phba;
  4387. unsigned long flags;
  4388. struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
  4389. kref);
  4390. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  4391. "node release: did:x%x flg:x%x type:x%x",
  4392. ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
  4393. lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
  4394. "0279 lpfc_nlp_release: ndlp:x%p "
  4395. "usgmap:x%x refcnt:%d\n",
  4396. (void *)ndlp, ndlp->nlp_usg_map,
  4397. atomic_read(&ndlp->kref.refcount));
  4398. /* remove ndlp from action. */
  4399. lpfc_nlp_remove(ndlp->vport, ndlp);
  4400. /* clear the ndlp active flag for all release cases */
  4401. phba = ndlp->phba;
  4402. spin_lock_irqsave(&phba->ndlp_lock, flags);
  4403. NLP_CLR_NODE_ACT(ndlp);
  4404. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4405. /* free ndlp memory for final ndlp release */
  4406. if (NLP_CHK_FREE_REQ(ndlp)) {
  4407. kfree(ndlp->lat_data);
  4408. mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
  4409. }
  4410. }
  4411. /* This routine bumps the reference count for a ndlp structure to ensure
  4412. * that one discovery thread won't free a ndlp while another discovery thread
  4413. * is using it.
  4414. */
  4415. struct lpfc_nodelist *
  4416. lpfc_nlp_get(struct lpfc_nodelist *ndlp)
  4417. {
  4418. struct lpfc_hba *phba;
  4419. unsigned long flags;
  4420. if (ndlp) {
  4421. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  4422. "node get: did:x%x flg:x%x refcnt:x%x",
  4423. ndlp->nlp_DID, ndlp->nlp_flag,
  4424. atomic_read(&ndlp->kref.refcount));
  4425. /* The check of ndlp usage to prevent incrementing the
  4426. * ndlp reference count that is in the process of being
  4427. * released.
  4428. */
  4429. phba = ndlp->phba;
  4430. spin_lock_irqsave(&phba->ndlp_lock, flags);
  4431. if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
  4432. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4433. lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
  4434. "0276 lpfc_nlp_get: ndlp:x%p "
  4435. "usgmap:x%x refcnt:%d\n",
  4436. (void *)ndlp, ndlp->nlp_usg_map,
  4437. atomic_read(&ndlp->kref.refcount));
  4438. return NULL;
  4439. } else
  4440. kref_get(&ndlp->kref);
  4441. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4442. }
  4443. return ndlp;
  4444. }
  4445. /* This routine decrements the reference count for a ndlp structure. If the
  4446. * count goes to 0, this indicates the the associated nodelist should be
  4447. * freed. Returning 1 indicates the ndlp resource has been released; on the
  4448. * other hand, returning 0 indicates the ndlp resource has not been released
  4449. * yet.
  4450. */
  4451. int
  4452. lpfc_nlp_put(struct lpfc_nodelist *ndlp)
  4453. {
  4454. struct lpfc_hba *phba;
  4455. unsigned long flags;
  4456. if (!ndlp)
  4457. return 1;
  4458. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  4459. "node put: did:x%x flg:x%x refcnt:x%x",
  4460. ndlp->nlp_DID, ndlp->nlp_flag,
  4461. atomic_read(&ndlp->kref.refcount));
  4462. phba = ndlp->phba;
  4463. spin_lock_irqsave(&phba->ndlp_lock, flags);
  4464. /* Check the ndlp memory free acknowledge flag to avoid the
  4465. * possible race condition that kref_put got invoked again
  4466. * after previous one has done ndlp memory free.
  4467. */
  4468. if (NLP_CHK_FREE_ACK(ndlp)) {
  4469. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4470. lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
  4471. "0274 lpfc_nlp_put: ndlp:x%p "
  4472. "usgmap:x%x refcnt:%d\n",
  4473. (void *)ndlp, ndlp->nlp_usg_map,
  4474. atomic_read(&ndlp->kref.refcount));
  4475. return 1;
  4476. }
  4477. /* Check the ndlp inactivate log flag to avoid the possible
  4478. * race condition that kref_put got invoked again after ndlp
  4479. * is already in inactivating state.
  4480. */
  4481. if (NLP_CHK_IACT_REQ(ndlp)) {
  4482. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4483. lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
  4484. "0275 lpfc_nlp_put: ndlp:x%p "
  4485. "usgmap:x%x refcnt:%d\n",
  4486. (void *)ndlp, ndlp->nlp_usg_map,
  4487. atomic_read(&ndlp->kref.refcount));
  4488. return 1;
  4489. }
  4490. /* For last put, mark the ndlp usage flags to make sure no
  4491. * other kref_get and kref_put on the same ndlp shall get
  4492. * in between the process when the final kref_put has been
  4493. * invoked on this ndlp.
  4494. */
  4495. if (atomic_read(&ndlp->kref.refcount) == 1) {
  4496. /* Indicate ndlp is put to inactive state. */
  4497. NLP_SET_IACT_REQ(ndlp);
  4498. /* Acknowledge ndlp memory free has been seen. */
  4499. if (NLP_CHK_FREE_REQ(ndlp))
  4500. NLP_SET_FREE_ACK(ndlp);
  4501. }
  4502. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4503. /* Note, the kref_put returns 1 when decrementing a reference
  4504. * count that was 1, it invokes the release callback function,
  4505. * but it still left the reference count as 1 (not actually
  4506. * performs the last decrementation). Otherwise, it actually
  4507. * decrements the reference count and returns 0.
  4508. */
  4509. return kref_put(&ndlp->kref, lpfc_nlp_release);
  4510. }
  4511. /* This routine free's the specified nodelist if it is not in use
  4512. * by any other discovery thread. This routine returns 1 if the
  4513. * ndlp has been freed. A return value of 0 indicates the ndlp is
  4514. * not yet been released.
  4515. */
  4516. int
  4517. lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
  4518. {
  4519. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  4520. "node not used: did:x%x flg:x%x refcnt:x%x",
  4521. ndlp->nlp_DID, ndlp->nlp_flag,
  4522. atomic_read(&ndlp->kref.refcount));
  4523. if (atomic_read(&ndlp->kref.refcount) == 1)
  4524. if (lpfc_nlp_put(ndlp))
  4525. return 1;
  4526. return 0;
  4527. }
  4528. /**
  4529. * lpfc_fcf_inuse - Check if FCF can be unregistered.
  4530. * @phba: Pointer to hba context object.
  4531. *
  4532. * This function iterate through all FC nodes associated
  4533. * will all vports to check if there is any node with
  4534. * fc_rports associated with it. If there is an fc_rport
  4535. * associated with the node, then the node is either in
  4536. * discovered state or its devloss_timer is pending.
  4537. */
  4538. static int
  4539. lpfc_fcf_inuse(struct lpfc_hba *phba)
  4540. {
  4541. struct lpfc_vport **vports;
  4542. int i, ret = 0;
  4543. struct lpfc_nodelist *ndlp;
  4544. struct Scsi_Host *shost;
  4545. vports = lpfc_create_vport_work_array(phba);
  4546. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  4547. shost = lpfc_shost_from_vport(vports[i]);
  4548. spin_lock_irq(shost->host_lock);
  4549. list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
  4550. if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
  4551. (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
  4552. ret = 1;
  4553. spin_unlock_irq(shost->host_lock);
  4554. goto out;
  4555. } else {
  4556. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  4557. "2624 RPI %x DID %x flg %x still "
  4558. "logged in\n",
  4559. ndlp->nlp_rpi, ndlp->nlp_DID,
  4560. ndlp->nlp_flag);
  4561. if (ndlp->nlp_flag & NLP_RPI_VALID)
  4562. ret = 1;
  4563. }
  4564. }
  4565. spin_unlock_irq(shost->host_lock);
  4566. }
  4567. out:
  4568. lpfc_destroy_vport_work_array(phba, vports);
  4569. return ret;
  4570. }
  4571. /**
  4572. * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
  4573. * @phba: Pointer to hba context object.
  4574. * @mboxq: Pointer to mailbox object.
  4575. *
  4576. * This function frees memory associated with the mailbox command.
  4577. */
  4578. static void
  4579. lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  4580. {
  4581. struct lpfc_vport *vport = mboxq->vport;
  4582. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  4583. if (mboxq->u.mb.mbxStatus) {
  4584. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  4585. "2555 UNREG_VFI mbxStatus error x%x "
  4586. "HBA state x%x\n",
  4587. mboxq->u.mb.mbxStatus, vport->port_state);
  4588. }
  4589. spin_lock_irq(shost->host_lock);
  4590. phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
  4591. spin_unlock_irq(shost->host_lock);
  4592. mempool_free(mboxq, phba->mbox_mem_pool);
  4593. return;
  4594. }
  4595. /**
  4596. * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
  4597. * @phba: Pointer to hba context object.
  4598. * @mboxq: Pointer to mailbox object.
  4599. *
  4600. * This function frees memory associated with the mailbox command.
  4601. */
  4602. static void
  4603. lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  4604. {
  4605. struct lpfc_vport *vport = mboxq->vport;
  4606. if (mboxq->u.mb.mbxStatus) {
  4607. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  4608. "2550 UNREG_FCFI mbxStatus error x%x "
  4609. "HBA state x%x\n",
  4610. mboxq->u.mb.mbxStatus, vport->port_state);
  4611. }
  4612. mempool_free(mboxq, phba->mbox_mem_pool);
  4613. return;
  4614. }
  4615. /**
  4616. * lpfc_unregister_fcf_prep - Unregister fcf record preparation
  4617. * @phba: Pointer to hba context object.
  4618. *
  4619. * This function prepare the HBA for unregistering the currently registered
  4620. * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
  4621. * VFIs.
  4622. */
  4623. int
  4624. lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
  4625. {
  4626. LPFC_MBOXQ_t *mbox;
  4627. struct lpfc_vport **vports;
  4628. struct lpfc_nodelist *ndlp;
  4629. struct Scsi_Host *shost;
  4630. int i, rc;
  4631. /* Unregister RPIs */
  4632. if (lpfc_fcf_inuse(phba))
  4633. lpfc_unreg_hba_rpis(phba);
  4634. /* At this point, all discovery is aborted */
  4635. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  4636. /* Unregister VPIs */
  4637. vports = lpfc_create_vport_work_array(phba);
  4638. if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
  4639. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  4640. /* Stop FLOGI/FDISC retries */
  4641. ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
  4642. if (ndlp)
  4643. lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
  4644. lpfc_cleanup_pending_mbox(vports[i]);
  4645. lpfc_mbx_unreg_vpi(vports[i]);
  4646. shost = lpfc_shost_from_vport(vports[i]);
  4647. spin_lock_irq(shost->host_lock);
  4648. vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
  4649. vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
  4650. spin_unlock_irq(shost->host_lock);
  4651. }
  4652. lpfc_destroy_vport_work_array(phba, vports);
  4653. /* Cleanup any outstanding ELS commands */
  4654. lpfc_els_flush_all_cmd(phba);
  4655. /* Unregister VFI */
  4656. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4657. if (!mbox) {
  4658. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  4659. "2556 UNREG_VFI mbox allocation failed"
  4660. "HBA state x%x\n", phba->pport->port_state);
  4661. return -ENOMEM;
  4662. }
  4663. lpfc_unreg_vfi(mbox, phba->pport);
  4664. mbox->vport = phba->pport;
  4665. mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
  4666. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  4667. if (rc == MBX_NOT_FINISHED) {
  4668. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  4669. "2557 UNREG_VFI issue mbox failed rc x%x "
  4670. "HBA state x%x\n",
  4671. rc, phba->pport->port_state);
  4672. mempool_free(mbox, phba->mbox_mem_pool);
  4673. return -EIO;
  4674. }
  4675. shost = lpfc_shost_from_vport(phba->pport);
  4676. spin_lock_irq(shost->host_lock);
  4677. phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
  4678. spin_unlock_irq(shost->host_lock);
  4679. return 0;
  4680. }
  4681. /**
  4682. * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
  4683. * @phba: Pointer to hba context object.
  4684. *
  4685. * This function issues synchronous unregister FCF mailbox command to HBA to
  4686. * unregister the currently registered FCF record. The driver does not reset
  4687. * the driver FCF usage state flags.
  4688. *
  4689. * Return 0 if successfully issued, none-zero otherwise.
  4690. */
  4691. int
  4692. lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
  4693. {
  4694. LPFC_MBOXQ_t *mbox;
  4695. int rc;
  4696. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4697. if (!mbox) {
  4698. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  4699. "2551 UNREG_FCFI mbox allocation failed"
  4700. "HBA state x%x\n", phba->pport->port_state);
  4701. return -ENOMEM;
  4702. }
  4703. lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
  4704. mbox->vport = phba->pport;
  4705. mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
  4706. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  4707. if (rc == MBX_NOT_FINISHED) {
  4708. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4709. "2552 Unregister FCFI command failed rc x%x "
  4710. "HBA state x%x\n",
  4711. rc, phba->pport->port_state);
  4712. return -EINVAL;
  4713. }
  4714. return 0;
  4715. }
  4716. /**
  4717. * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
  4718. * @phba: Pointer to hba context object.
  4719. *
  4720. * This function unregisters the currently reigstered FCF. This function
  4721. * also tries to find another FCF for discovery by rescan the HBA FCF table.
  4722. */
  4723. void
  4724. lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
  4725. {
  4726. int rc;
  4727. /* Preparation for unregistering fcf */
  4728. rc = lpfc_unregister_fcf_prep(phba);
  4729. if (rc) {
  4730. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
  4731. "2748 Failed to prepare for unregistering "
  4732. "HBA's FCF record: rc=%d\n", rc);
  4733. return;
  4734. }
  4735. /* Now, unregister FCF record and reset HBA FCF state */
  4736. rc = lpfc_sli4_unregister_fcf(phba);
  4737. if (rc)
  4738. return;
  4739. /* Reset HBA FCF states after successful unregister FCF */
  4740. phba->fcf.fcf_flag = 0;
  4741. phba->fcf.current_rec.flag = 0;
  4742. /*
  4743. * If driver is not unloading, check if there is any other
  4744. * FCF record that can be used for discovery.
  4745. */
  4746. if ((phba->pport->load_flag & FC_UNLOADING) ||
  4747. (phba->link_state < LPFC_LINK_UP))
  4748. return;
  4749. /* This is considered as the initial FCF discovery scan */
  4750. spin_lock_irq(&phba->hbalock);
  4751. phba->fcf.fcf_flag |= FCF_INIT_DISC;
  4752. spin_unlock_irq(&phba->hbalock);
  4753. /* Reset FCF roundrobin bmask for new discovery */
  4754. memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
  4755. rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
  4756. if (rc) {
  4757. spin_lock_irq(&phba->hbalock);
  4758. phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
  4759. spin_unlock_irq(&phba->hbalock);
  4760. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  4761. "2553 lpfc_unregister_unused_fcf failed "
  4762. "to read FCF record HBA state x%x\n",
  4763. phba->pport->port_state);
  4764. }
  4765. }
  4766. /**
  4767. * lpfc_unregister_fcf - Unregister the currently registered fcf record
  4768. * @phba: Pointer to hba context object.
  4769. *
  4770. * This function just unregisters the currently reigstered FCF. It does not
  4771. * try to find another FCF for discovery.
  4772. */
  4773. void
  4774. lpfc_unregister_fcf(struct lpfc_hba *phba)
  4775. {
  4776. int rc;
  4777. /* Preparation for unregistering fcf */
  4778. rc = lpfc_unregister_fcf_prep(phba);
  4779. if (rc) {
  4780. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
  4781. "2749 Failed to prepare for unregistering "
  4782. "HBA's FCF record: rc=%d\n", rc);
  4783. return;
  4784. }
  4785. /* Now, unregister FCF record and reset HBA FCF state */
  4786. rc = lpfc_sli4_unregister_fcf(phba);
  4787. if (rc)
  4788. return;
  4789. /* Set proper HBA FCF states after successful unregister FCF */
  4790. spin_lock_irq(&phba->hbalock);
  4791. phba->fcf.fcf_flag &= ~FCF_REGISTERED;
  4792. spin_unlock_irq(&phba->hbalock);
  4793. }
  4794. /**
  4795. * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
  4796. * @phba: Pointer to hba context object.
  4797. *
  4798. * This function check if there are any connected remote port for the FCF and
  4799. * if all the devices are disconnected, this function unregister FCFI.
  4800. * This function also tries to use another FCF for discovery.
  4801. */
  4802. void
  4803. lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
  4804. {
  4805. /*
  4806. * If HBA is not running in FIP mode, if HBA does not support
  4807. * FCoE, if FCF discovery is ongoing, or if FCF has not been
  4808. * registered, do nothing.
  4809. */
  4810. spin_lock_irq(&phba->hbalock);
  4811. if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
  4812. !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
  4813. !(phba->hba_flag & HBA_FIP_SUPPORT) ||
  4814. (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
  4815. (phba->pport->port_state == LPFC_FLOGI)) {
  4816. spin_unlock_irq(&phba->hbalock);
  4817. return;
  4818. }
  4819. spin_unlock_irq(&phba->hbalock);
  4820. if (lpfc_fcf_inuse(phba))
  4821. return;
  4822. lpfc_unregister_fcf_rescan(phba);
  4823. }
  4824. /**
  4825. * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
  4826. * @phba: Pointer to hba context object.
  4827. * @buff: Buffer containing the FCF connection table as in the config
  4828. * region.
  4829. * This function create driver data structure for the FCF connection
  4830. * record table read from config region 23.
  4831. */
  4832. static void
  4833. lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
  4834. uint8_t *buff)
  4835. {
  4836. struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
  4837. struct lpfc_fcf_conn_hdr *conn_hdr;
  4838. struct lpfc_fcf_conn_rec *conn_rec;
  4839. uint32_t record_count;
  4840. int i;
  4841. /* Free the current connect table */
  4842. list_for_each_entry_safe(conn_entry, next_conn_entry,
  4843. &phba->fcf_conn_rec_list, list) {
  4844. list_del_init(&conn_entry->list);
  4845. kfree(conn_entry);
  4846. }
  4847. conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
  4848. record_count = conn_hdr->length * sizeof(uint32_t)/
  4849. sizeof(struct lpfc_fcf_conn_rec);
  4850. conn_rec = (struct lpfc_fcf_conn_rec *)
  4851. (buff + sizeof(struct lpfc_fcf_conn_hdr));
  4852. for (i = 0; i < record_count; i++) {
  4853. if (!(conn_rec[i].flags & FCFCNCT_VALID))
  4854. continue;
  4855. conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
  4856. GFP_KERNEL);
  4857. if (!conn_entry) {
  4858. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4859. "2566 Failed to allocate connection"
  4860. " table entry\n");
  4861. return;
  4862. }
  4863. memcpy(&conn_entry->conn_rec, &conn_rec[i],
  4864. sizeof(struct lpfc_fcf_conn_rec));
  4865. conn_entry->conn_rec.vlan_tag =
  4866. le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
  4867. conn_entry->conn_rec.flags =
  4868. le16_to_cpu(conn_entry->conn_rec.flags);
  4869. list_add_tail(&conn_entry->list,
  4870. &phba->fcf_conn_rec_list);
  4871. }
  4872. }
  4873. /**
  4874. * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
  4875. * @phba: Pointer to hba context object.
  4876. * @buff: Buffer containing the FCoE parameter data structure.
  4877. *
  4878. * This function update driver data structure with config
  4879. * parameters read from config region 23.
  4880. */
  4881. static void
  4882. lpfc_read_fcoe_param(struct lpfc_hba *phba,
  4883. uint8_t *buff)
  4884. {
  4885. struct lpfc_fip_param_hdr *fcoe_param_hdr;
  4886. struct lpfc_fcoe_params *fcoe_param;
  4887. fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
  4888. buff;
  4889. fcoe_param = (struct lpfc_fcoe_params *)
  4890. (buff + sizeof(struct lpfc_fip_param_hdr));
  4891. if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
  4892. (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
  4893. return;
  4894. if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
  4895. phba->valid_vlan = 1;
  4896. phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
  4897. 0xFFF;
  4898. }
  4899. phba->fc_map[0] = fcoe_param->fc_map[0];
  4900. phba->fc_map[1] = fcoe_param->fc_map[1];
  4901. phba->fc_map[2] = fcoe_param->fc_map[2];
  4902. return;
  4903. }
  4904. /**
  4905. * lpfc_get_rec_conf23 - Get a record type in config region data.
  4906. * @buff: Buffer containing config region 23 data.
  4907. * @size: Size of the data buffer.
  4908. * @rec_type: Record type to be searched.
  4909. *
  4910. * This function searches config region data to find the begining
  4911. * of the record specified by record_type. If record found, this
  4912. * function return pointer to the record else return NULL.
  4913. */
  4914. static uint8_t *
  4915. lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
  4916. {
  4917. uint32_t offset = 0, rec_length;
  4918. if ((buff[0] == LPFC_REGION23_LAST_REC) ||
  4919. (size < sizeof(uint32_t)))
  4920. return NULL;
  4921. rec_length = buff[offset + 1];
  4922. /*
  4923. * One TLV record has one word header and number of data words
  4924. * specified in the rec_length field of the record header.
  4925. */
  4926. while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
  4927. <= size) {
  4928. if (buff[offset] == rec_type)
  4929. return &buff[offset];
  4930. if (buff[offset] == LPFC_REGION23_LAST_REC)
  4931. return NULL;
  4932. offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
  4933. rec_length = buff[offset + 1];
  4934. }
  4935. return NULL;
  4936. }
  4937. /**
  4938. * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
  4939. * @phba: Pointer to lpfc_hba data structure.
  4940. * @buff: Buffer containing config region 23 data.
  4941. * @size: Size of the data buffer.
  4942. *
  4943. * This fuction parse the FCoE config parameters in config region 23 and
  4944. * populate driver data structure with the parameters.
  4945. */
  4946. void
  4947. lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
  4948. uint8_t *buff,
  4949. uint32_t size)
  4950. {
  4951. uint32_t offset = 0, rec_length;
  4952. uint8_t *rec_ptr;
  4953. /*
  4954. * If data size is less than 2 words signature and version cannot be
  4955. * verified.
  4956. */
  4957. if (size < 2*sizeof(uint32_t))
  4958. return;
  4959. /* Check the region signature first */
  4960. if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
  4961. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4962. "2567 Config region 23 has bad signature\n");
  4963. return;
  4964. }
  4965. offset += 4;
  4966. /* Check the data structure version */
  4967. if (buff[offset] != LPFC_REGION23_VERSION) {
  4968. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4969. "2568 Config region 23 has bad version\n");
  4970. return;
  4971. }
  4972. offset += 4;
  4973. rec_length = buff[offset + 1];
  4974. /* Read FCoE param record */
  4975. rec_ptr = lpfc_get_rec_conf23(&buff[offset],
  4976. size - offset, FCOE_PARAM_TYPE);
  4977. if (rec_ptr)
  4978. lpfc_read_fcoe_param(phba, rec_ptr);
  4979. /* Read FCF connection table */
  4980. rec_ptr = lpfc_get_rec_conf23(&buff[offset],
  4981. size - offset, FCOE_CONN_TBL_TYPE);
  4982. if (rec_ptr)
  4983. lpfc_read_fcf_conn_tbl(phba, rec_ptr);
  4984. }