lpfc_hbadisc.c 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550
  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 (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. FCF_DISCOVERY);
  1677. spin_unlock_irq(&phba->hbalock);
  1678. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1679. "2836 The new FCF record (x%x) "
  1680. "matches the in-use FCF record "
  1681. "(x%x)\n",
  1682. phba->fcf.current_rec.fcf_indx,
  1683. bf_get(lpfc_fcf_record_fcf_index,
  1684. new_fcf_record));
  1685. goto out;
  1686. }
  1687. /*
  1688. * Read next FCF record from HBA searching for the matching
  1689. * with in-use record only if not during the fast failover
  1690. * period. In case of fast failover period, it shall try to
  1691. * determine whether the FCF record just read should be the
  1692. * next candidate.
  1693. */
  1694. if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
  1695. spin_unlock_irq(&phba->hbalock);
  1696. goto read_next_fcf;
  1697. }
  1698. }
  1699. /*
  1700. * Update on failover FCF record only if it's in FCF fast-failover
  1701. * period; otherwise, update on current FCF record.
  1702. */
  1703. if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
  1704. fcf_rec = &phba->fcf.failover_rec;
  1705. else
  1706. fcf_rec = &phba->fcf.current_rec;
  1707. if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
  1708. /*
  1709. * If the driver FCF record does not have boot flag
  1710. * set and new hba fcf record has boot flag set, use
  1711. * the new hba fcf record.
  1712. */
  1713. if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
  1714. /* Choose this FCF record */
  1715. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1716. "2837 Update current FCF record "
  1717. "(x%x) with new FCF record (x%x)\n",
  1718. fcf_rec->fcf_indx,
  1719. bf_get(lpfc_fcf_record_fcf_index,
  1720. new_fcf_record));
  1721. __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
  1722. addr_mode, vlan_id, BOOT_ENABLE);
  1723. spin_unlock_irq(&phba->hbalock);
  1724. goto read_next_fcf;
  1725. }
  1726. /*
  1727. * If the driver FCF record has boot flag set and the
  1728. * new hba FCF record does not have boot flag, read
  1729. * the next FCF record.
  1730. */
  1731. if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
  1732. spin_unlock_irq(&phba->hbalock);
  1733. goto read_next_fcf;
  1734. }
  1735. /*
  1736. * If the new hba FCF record has lower priority value
  1737. * than the driver FCF record, use the new record.
  1738. */
  1739. if (new_fcf_record->fip_priority < fcf_rec->priority) {
  1740. /* Choose the new FCF record with lower priority */
  1741. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1742. "2838 Update current FCF record "
  1743. "(x%x) with new FCF record (x%x)\n",
  1744. fcf_rec->fcf_indx,
  1745. bf_get(lpfc_fcf_record_fcf_index,
  1746. new_fcf_record));
  1747. __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
  1748. addr_mode, vlan_id, 0);
  1749. /* Reset running random FCF selection count */
  1750. phba->fcf.eligible_fcf_cnt = 1;
  1751. } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
  1752. /* Update running random FCF selection count */
  1753. phba->fcf.eligible_fcf_cnt++;
  1754. select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
  1755. phba->fcf.eligible_fcf_cnt);
  1756. if (select_new_fcf) {
  1757. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1758. "2839 Update current FCF record "
  1759. "(x%x) with new FCF record (x%x)\n",
  1760. fcf_rec->fcf_indx,
  1761. bf_get(lpfc_fcf_record_fcf_index,
  1762. new_fcf_record));
  1763. /* Choose the new FCF by random selection */
  1764. __lpfc_update_fcf_record(phba, fcf_rec,
  1765. new_fcf_record,
  1766. addr_mode, vlan_id, 0);
  1767. }
  1768. }
  1769. spin_unlock_irq(&phba->hbalock);
  1770. goto read_next_fcf;
  1771. }
  1772. /*
  1773. * This is the first suitable FCF record, choose this record for
  1774. * initial best-fit FCF.
  1775. */
  1776. if (fcf_rec) {
  1777. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1778. "2840 Update current FCF record "
  1779. "with initial FCF record (x%x)\n",
  1780. bf_get(lpfc_fcf_record_fcf_index,
  1781. new_fcf_record));
  1782. __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
  1783. addr_mode, vlan_id, (boot_flag ?
  1784. BOOT_ENABLE : 0));
  1785. phba->fcf.fcf_flag |= FCF_AVAILABLE;
  1786. /* Setup initial running random FCF selection count */
  1787. phba->fcf.eligible_fcf_cnt = 1;
  1788. /* Seeding the random number generator for random selection */
  1789. seed = (uint32_t)(0xFFFFFFFF & jiffies);
  1790. srandom32(seed);
  1791. }
  1792. spin_unlock_irq(&phba->hbalock);
  1793. goto read_next_fcf;
  1794. read_next_fcf:
  1795. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1796. if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
  1797. if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
  1798. /*
  1799. * Case of FCF fast failover scan
  1800. */
  1801. /*
  1802. * It has not found any suitable FCF record, cancel
  1803. * FCF scan inprogress, and do nothing
  1804. */
  1805. if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
  1806. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  1807. "2782 No suitable FCF record "
  1808. "found during this round of "
  1809. "post FCF rediscovery scan: "
  1810. "fcf_evt_tag:x%x, fcf_index: "
  1811. "x%x\n",
  1812. phba->fcoe_eventtag_at_fcf_scan,
  1813. bf_get(lpfc_fcf_record_fcf_index,
  1814. new_fcf_record));
  1815. /*
  1816. * Let next new FCF event trigger fast
  1817. * failover
  1818. */
  1819. spin_lock_irq(&phba->hbalock);
  1820. phba->hba_flag &= ~FCF_DISC_INPROGRESS;
  1821. spin_unlock_irq(&phba->hbalock);
  1822. return;
  1823. }
  1824. /*
  1825. * It has found a suitable FCF record that is not
  1826. * the same as in-use FCF record, unregister the
  1827. * in-use FCF record, replace the in-use FCF record
  1828. * with the new FCF record, mark FCF fast failover
  1829. * completed, and then start register the new FCF
  1830. * record.
  1831. */
  1832. /* Unregister the current in-use FCF record */
  1833. lpfc_unregister_fcf(phba);
  1834. /* Replace in-use record with the new record */
  1835. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1836. "2842 Replace the current in-use "
  1837. "FCF record (x%x) with failover FCF "
  1838. "record (x%x)\n",
  1839. phba->fcf.current_rec.fcf_indx,
  1840. phba->fcf.failover_rec.fcf_indx);
  1841. memcpy(&phba->fcf.current_rec,
  1842. &phba->fcf.failover_rec,
  1843. sizeof(struct lpfc_fcf_rec));
  1844. /*
  1845. * Mark the fast FCF failover rediscovery completed
  1846. * and the start of the first round of the roundrobin
  1847. * FCF failover.
  1848. */
  1849. spin_lock_irq(&phba->hbalock);
  1850. phba->fcf.fcf_flag &=
  1851. ~(FCF_REDISC_FOV | FCF_REDISC_RRU);
  1852. spin_unlock_irq(&phba->hbalock);
  1853. /*
  1854. * Set up the initial registered FCF index for FLOGI
  1855. * round robin FCF failover.
  1856. */
  1857. phba->fcf.fcf_rr_init_indx =
  1858. phba->fcf.failover_rec.fcf_indx;
  1859. /* Register to the new FCF record */
  1860. lpfc_register_fcf(phba);
  1861. } else {
  1862. /*
  1863. * In case of transaction period to fast FCF failover,
  1864. * do nothing when search to the end of the FCF table.
  1865. */
  1866. if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
  1867. (phba->fcf.fcf_flag & FCF_REDISC_PEND))
  1868. return;
  1869. if (phba->fcf.fcf_flag & FCF_IN_USE) {
  1870. /*
  1871. * In case the current in-use FCF record no
  1872. * longer existed during FCF discovery that
  1873. * was not triggered by fast FCF failover
  1874. * process, treat it as fast FCF failover.
  1875. */
  1876. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1877. "2841 In-use FCF record (x%x) "
  1878. "not reported, entering fast "
  1879. "FCF failover mode scanning.\n",
  1880. phba->fcf.current_rec.fcf_indx);
  1881. spin_lock_irq(&phba->hbalock);
  1882. phba->fcf.fcf_flag |= FCF_REDISC_FOV;
  1883. spin_unlock_irq(&phba->hbalock);
  1884. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1885. lpfc_sli4_fcf_scan_read_fcf_rec(phba,
  1886. LPFC_FCOE_FCF_GET_FIRST);
  1887. return;
  1888. }
  1889. /*
  1890. * Otherwise, initial scan or post linkdown rescan,
  1891. * register with the best FCF record found so far
  1892. * through the FCF scanning process.
  1893. */
  1894. /*
  1895. * Mark the initial FCF discovery completed and
  1896. * the start of the first round of the roundrobin
  1897. * FCF failover.
  1898. */
  1899. spin_lock_irq(&phba->hbalock);
  1900. phba->fcf.fcf_flag &=
  1901. ~(FCF_INIT_DISC | FCF_REDISC_RRU);
  1902. spin_unlock_irq(&phba->hbalock);
  1903. /*
  1904. * Set up the initial registered FCF index for FLOGI
  1905. * round robin FCF failover
  1906. */
  1907. phba->fcf.fcf_rr_init_indx =
  1908. phba->fcf.current_rec.fcf_indx;
  1909. /* Register to the new FCF record */
  1910. lpfc_register_fcf(phba);
  1911. }
  1912. } else
  1913. lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
  1914. return;
  1915. out:
  1916. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1917. lpfc_register_fcf(phba);
  1918. return;
  1919. }
  1920. /**
  1921. * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf round robin read_fcf mbox cmpl hdler
  1922. * @phba: pointer to lpfc hba data structure.
  1923. * @mboxq: pointer to mailbox object.
  1924. *
  1925. * This is the callback function for FLOGI failure round robin FCF failover
  1926. * read FCF record mailbox command from the eligible FCF record bmask for
  1927. * performing the failover. If the FCF read back is not valid/available, it
  1928. * fails through to retrying FLOGI to the currently registered FCF again.
  1929. * Otherwise, if the FCF read back is valid and available, it will set the
  1930. * newly read FCF record to the failover FCF record, unregister currently
  1931. * registered FCF record, copy the failover FCF record to the current
  1932. * FCF record, and then register the current FCF record before proceeding
  1933. * to trying FLOGI on the new failover FCF.
  1934. */
  1935. void
  1936. lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  1937. {
  1938. struct fcf_record *new_fcf_record;
  1939. uint32_t boot_flag, addr_mode;
  1940. uint16_t next_fcf_index;
  1941. uint16_t current_fcf_index;
  1942. uint16_t vlan_id;
  1943. /* If link state is not up, stop the round robin failover process */
  1944. if (phba->link_state < LPFC_LINK_UP) {
  1945. spin_lock_irq(&phba->hbalock);
  1946. phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
  1947. spin_unlock_irq(&phba->hbalock);
  1948. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1949. return;
  1950. }
  1951. /* Parse the FCF record from the non-embedded mailbox command */
  1952. new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
  1953. &next_fcf_index);
  1954. if (!new_fcf_record) {
  1955. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  1956. "2766 Mailbox command READ_FCF_RECORD "
  1957. "failed to retrieve a FCF record.\n");
  1958. goto out;
  1959. }
  1960. /* Get the needed parameters from FCF record */
  1961. lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
  1962. &addr_mode, &vlan_id);
  1963. /* Log the FCF record information if turned on */
  1964. lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
  1965. next_fcf_index);
  1966. /* Upload new FCF record to the failover FCF record */
  1967. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1968. "2834 Update the current FCF record (x%x) "
  1969. "with the next FCF record (x%x)\n",
  1970. phba->fcf.failover_rec.fcf_indx,
  1971. bf_get(lpfc_fcf_record_fcf_index, new_fcf_record));
  1972. spin_lock_irq(&phba->hbalock);
  1973. __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
  1974. new_fcf_record, addr_mode, vlan_id,
  1975. (boot_flag ? BOOT_ENABLE : 0));
  1976. spin_unlock_irq(&phba->hbalock);
  1977. current_fcf_index = phba->fcf.current_rec.fcf_indx;
  1978. /* Unregister the current in-use FCF record */
  1979. lpfc_unregister_fcf(phba);
  1980. /* Replace in-use record with the new record */
  1981. memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
  1982. sizeof(struct lpfc_fcf_rec));
  1983. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  1984. "2783 FLOGI round robin FCF failover from FCF "
  1985. "(x%x) to FCF (x%x).\n",
  1986. current_fcf_index,
  1987. bf_get(lpfc_fcf_record_fcf_index, new_fcf_record));
  1988. out:
  1989. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1990. lpfc_register_fcf(phba);
  1991. }
  1992. /**
  1993. * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
  1994. * @phba: pointer to lpfc hba data structure.
  1995. * @mboxq: pointer to mailbox object.
  1996. *
  1997. * This is the callback function of read FCF record mailbox command for
  1998. * updating the eligible FCF bmask for FLOGI failure round robin FCF
  1999. * failover when a new FCF event happened. If the FCF read back is
  2000. * valid/available and it passes the connection list check, it updates
  2001. * the bmask for the eligible FCF record for round robin failover.
  2002. */
  2003. void
  2004. lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  2005. {
  2006. struct fcf_record *new_fcf_record;
  2007. uint32_t boot_flag, addr_mode;
  2008. uint16_t fcf_index, next_fcf_index;
  2009. uint16_t vlan_id;
  2010. int rc;
  2011. /* If link state is not up, no need to proceed */
  2012. if (phba->link_state < LPFC_LINK_UP)
  2013. goto out;
  2014. /* If FCF discovery period is over, no need to proceed */
  2015. if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
  2016. goto out;
  2017. /* Parse the FCF record from the non-embedded mailbox command */
  2018. new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
  2019. &next_fcf_index);
  2020. if (!new_fcf_record) {
  2021. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  2022. "2767 Mailbox command READ_FCF_RECORD "
  2023. "failed to retrieve a FCF record.\n");
  2024. goto out;
  2025. }
  2026. /* Check the connection list for eligibility */
  2027. rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
  2028. &addr_mode, &vlan_id);
  2029. /* Log the FCF record information if turned on */
  2030. lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
  2031. next_fcf_index);
  2032. if (!rc)
  2033. goto out;
  2034. /* Update the eligible FCF record index bmask */
  2035. fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
  2036. rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
  2037. out:
  2038. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  2039. }
  2040. /**
  2041. * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
  2042. * @phba: pointer to lpfc hba data structure.
  2043. * @mboxq: pointer to mailbox data structure.
  2044. *
  2045. * This function handles completion of init vpi mailbox command.
  2046. */
  2047. void
  2048. lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  2049. {
  2050. struct lpfc_vport *vport = mboxq->vport;
  2051. struct lpfc_nodelist *ndlp;
  2052. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2053. if (mboxq->u.mb.mbxStatus) {
  2054. lpfc_printf_vlog(vport, KERN_ERR,
  2055. LOG_MBOX,
  2056. "2609 Init VPI mailbox failed 0x%x\n",
  2057. mboxq->u.mb.mbxStatus);
  2058. mempool_free(mboxq, phba->mbox_mem_pool);
  2059. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2060. return;
  2061. }
  2062. spin_lock_irq(shost->host_lock);
  2063. vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
  2064. spin_unlock_irq(shost->host_lock);
  2065. /* If this port is physical port or FDISC is done, do reg_vpi */
  2066. if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
  2067. ndlp = lpfc_findnode_did(vport, Fabric_DID);
  2068. if (!ndlp)
  2069. lpfc_printf_vlog(vport, KERN_ERR,
  2070. LOG_DISCOVERY,
  2071. "2731 Cannot find fabric "
  2072. "controller node\n");
  2073. else
  2074. lpfc_register_new_vport(phba, vport, ndlp);
  2075. mempool_free(mboxq, phba->mbox_mem_pool);
  2076. return;
  2077. }
  2078. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
  2079. lpfc_initial_fdisc(vport);
  2080. else {
  2081. lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
  2082. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  2083. "2606 No NPIV Fabric support\n");
  2084. }
  2085. mempool_free(mboxq, phba->mbox_mem_pool);
  2086. return;
  2087. }
  2088. /**
  2089. * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
  2090. * @vport: pointer to lpfc_vport data structure.
  2091. *
  2092. * This function issue a init_vpi mailbox command to initialize
  2093. * VPI for the vport.
  2094. */
  2095. void
  2096. lpfc_issue_init_vpi(struct lpfc_vport *vport)
  2097. {
  2098. LPFC_MBOXQ_t *mboxq;
  2099. int rc;
  2100. mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
  2101. if (!mboxq) {
  2102. lpfc_printf_vlog(vport, KERN_ERR,
  2103. LOG_MBOX, "2607 Failed to allocate "
  2104. "init_vpi mailbox\n");
  2105. return;
  2106. }
  2107. lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
  2108. mboxq->vport = vport;
  2109. mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
  2110. rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
  2111. if (rc == MBX_NOT_FINISHED) {
  2112. lpfc_printf_vlog(vport, KERN_ERR,
  2113. LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
  2114. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  2115. }
  2116. }
  2117. /**
  2118. * lpfc_start_fdiscs - send fdiscs for each vports on this port.
  2119. * @phba: pointer to lpfc hba data structure.
  2120. *
  2121. * This function loops through the list of vports on the @phba and issues an
  2122. * FDISC if possible.
  2123. */
  2124. void
  2125. lpfc_start_fdiscs(struct lpfc_hba *phba)
  2126. {
  2127. struct lpfc_vport **vports;
  2128. int i;
  2129. vports = lpfc_create_vport_work_array(phba);
  2130. if (vports != NULL) {
  2131. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  2132. if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
  2133. continue;
  2134. /* There are no vpi for this vport */
  2135. if (vports[i]->vpi > phba->max_vpi) {
  2136. lpfc_vport_set_state(vports[i],
  2137. FC_VPORT_FAILED);
  2138. continue;
  2139. }
  2140. if (phba->fc_topology == TOPOLOGY_LOOP) {
  2141. lpfc_vport_set_state(vports[i],
  2142. FC_VPORT_LINKDOWN);
  2143. continue;
  2144. }
  2145. if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
  2146. lpfc_issue_init_vpi(vports[i]);
  2147. continue;
  2148. }
  2149. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
  2150. lpfc_initial_fdisc(vports[i]);
  2151. else {
  2152. lpfc_vport_set_state(vports[i],
  2153. FC_VPORT_NO_FABRIC_SUPP);
  2154. lpfc_printf_vlog(vports[i], KERN_ERR,
  2155. LOG_ELS,
  2156. "0259 No NPIV "
  2157. "Fabric support\n");
  2158. }
  2159. }
  2160. }
  2161. lpfc_destroy_vport_work_array(phba, vports);
  2162. }
  2163. void
  2164. lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  2165. {
  2166. struct lpfc_dmabuf *dmabuf = mboxq->context1;
  2167. struct lpfc_vport *vport = mboxq->vport;
  2168. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2169. if (mboxq->u.mb.mbxStatus) {
  2170. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  2171. "2018 REG_VFI mbxStatus error x%x "
  2172. "HBA state x%x\n",
  2173. mboxq->u.mb.mbxStatus, vport->port_state);
  2174. if (phba->fc_topology == TOPOLOGY_LOOP) {
  2175. /* FLOGI failed, use loop map to make discovery list */
  2176. lpfc_disc_list_loopmap(vport);
  2177. /* Start discovery */
  2178. lpfc_disc_start(vport);
  2179. goto fail_free_mem;
  2180. }
  2181. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2182. goto fail_free_mem;
  2183. }
  2184. /* The VPI is implicitly registered when the VFI is registered */
  2185. spin_lock_irq(shost->host_lock);
  2186. vport->vpi_state |= LPFC_VPI_REGISTERED;
  2187. vport->fc_flag |= FC_VFI_REGISTERED;
  2188. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  2189. spin_unlock_irq(shost->host_lock);
  2190. if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
  2191. lpfc_start_fdiscs(phba);
  2192. lpfc_do_scr_ns_plogi(phba, vport);
  2193. }
  2194. fail_free_mem:
  2195. mempool_free(mboxq, phba->mbox_mem_pool);
  2196. lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
  2197. kfree(dmabuf);
  2198. return;
  2199. }
  2200. static void
  2201. lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2202. {
  2203. MAILBOX_t *mb = &pmb->u.mb;
  2204. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
  2205. struct lpfc_vport *vport = pmb->vport;
  2206. /* Check for error */
  2207. if (mb->mbxStatus) {
  2208. /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
  2209. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  2210. "0319 READ_SPARAM mbxStatus error x%x "
  2211. "hba state x%x>\n",
  2212. mb->mbxStatus, vport->port_state);
  2213. lpfc_linkdown(phba);
  2214. goto out;
  2215. }
  2216. memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
  2217. sizeof (struct serv_parm));
  2218. if (phba->cfg_soft_wwnn)
  2219. u64_to_wwn(phba->cfg_soft_wwnn,
  2220. vport->fc_sparam.nodeName.u.wwn);
  2221. if (phba->cfg_soft_wwpn)
  2222. u64_to_wwn(phba->cfg_soft_wwpn,
  2223. vport->fc_sparam.portName.u.wwn);
  2224. memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
  2225. sizeof(vport->fc_nodename));
  2226. memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
  2227. sizeof(vport->fc_portname));
  2228. if (vport->port_type == LPFC_PHYSICAL_PORT) {
  2229. memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
  2230. memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
  2231. }
  2232. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2233. kfree(mp);
  2234. mempool_free(pmb, phba->mbox_mem_pool);
  2235. return;
  2236. out:
  2237. pmb->context1 = NULL;
  2238. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2239. kfree(mp);
  2240. lpfc_issue_clear_la(phba, vport);
  2241. mempool_free(pmb, phba->mbox_mem_pool);
  2242. return;
  2243. }
  2244. static void
  2245. lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
  2246. {
  2247. struct lpfc_vport *vport = phba->pport;
  2248. LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
  2249. int i;
  2250. struct lpfc_dmabuf *mp;
  2251. int rc;
  2252. struct fcf_record *fcf_record;
  2253. spin_lock_irq(&phba->hbalock);
  2254. switch (la->UlnkSpeed) {
  2255. case LA_1GHZ_LINK:
  2256. phba->fc_linkspeed = LA_1GHZ_LINK;
  2257. break;
  2258. case LA_2GHZ_LINK:
  2259. phba->fc_linkspeed = LA_2GHZ_LINK;
  2260. break;
  2261. case LA_4GHZ_LINK:
  2262. phba->fc_linkspeed = LA_4GHZ_LINK;
  2263. break;
  2264. case LA_8GHZ_LINK:
  2265. phba->fc_linkspeed = LA_8GHZ_LINK;
  2266. break;
  2267. case LA_10GHZ_LINK:
  2268. phba->fc_linkspeed = LA_10GHZ_LINK;
  2269. break;
  2270. default:
  2271. phba->fc_linkspeed = LA_UNKNW_LINK;
  2272. break;
  2273. }
  2274. phba->fc_topology = la->topology;
  2275. phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
  2276. if (phba->fc_topology == TOPOLOGY_LOOP) {
  2277. phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
  2278. /* if npiv is enabled and this adapter supports npiv log
  2279. * a message that npiv is not supported in this topology
  2280. */
  2281. if (phba->cfg_enable_npiv && phba->max_vpi)
  2282. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2283. "1309 Link Up Event npiv not supported in loop "
  2284. "topology\n");
  2285. /* Get Loop Map information */
  2286. if (la->il)
  2287. vport->fc_flag |= FC_LBIT;
  2288. vport->fc_myDID = la->granted_AL_PA;
  2289. i = la->un.lilpBde64.tus.f.bdeSize;
  2290. if (i == 0) {
  2291. phba->alpa_map[0] = 0;
  2292. } else {
  2293. if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
  2294. int numalpa, j, k;
  2295. union {
  2296. uint8_t pamap[16];
  2297. struct {
  2298. uint32_t wd1;
  2299. uint32_t wd2;
  2300. uint32_t wd3;
  2301. uint32_t wd4;
  2302. } pa;
  2303. } un;
  2304. numalpa = phba->alpa_map[0];
  2305. j = 0;
  2306. while (j < numalpa) {
  2307. memset(un.pamap, 0, 16);
  2308. for (k = 1; j < numalpa; k++) {
  2309. un.pamap[k - 1] =
  2310. phba->alpa_map[j + 1];
  2311. j++;
  2312. if (k == 16)
  2313. break;
  2314. }
  2315. /* Link Up Event ALPA map */
  2316. lpfc_printf_log(phba,
  2317. KERN_WARNING,
  2318. LOG_LINK_EVENT,
  2319. "1304 Link Up Event "
  2320. "ALPA map Data: x%x "
  2321. "x%x x%x x%x\n",
  2322. un.pa.wd1, un.pa.wd2,
  2323. un.pa.wd3, un.pa.wd4);
  2324. }
  2325. }
  2326. }
  2327. } else {
  2328. if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
  2329. if (phba->max_vpi && phba->cfg_enable_npiv &&
  2330. (phba->sli_rev == 3))
  2331. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  2332. }
  2333. vport->fc_myDID = phba->fc_pref_DID;
  2334. vport->fc_flag |= FC_LBIT;
  2335. }
  2336. spin_unlock_irq(&phba->hbalock);
  2337. lpfc_linkup(phba);
  2338. sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2339. if (!sparam_mbox)
  2340. goto out;
  2341. rc = lpfc_read_sparam(phba, sparam_mbox, 0);
  2342. if (rc) {
  2343. mempool_free(sparam_mbox, phba->mbox_mem_pool);
  2344. goto out;
  2345. }
  2346. sparam_mbox->vport = vport;
  2347. sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
  2348. rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
  2349. if (rc == MBX_NOT_FINISHED) {
  2350. mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
  2351. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2352. kfree(mp);
  2353. mempool_free(sparam_mbox, phba->mbox_mem_pool);
  2354. goto out;
  2355. }
  2356. if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
  2357. cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2358. if (!cfglink_mbox)
  2359. goto out;
  2360. vport->port_state = LPFC_LOCAL_CFG_LINK;
  2361. lpfc_config_link(phba, cfglink_mbox);
  2362. cfglink_mbox->vport = vport;
  2363. cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
  2364. rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
  2365. if (rc == MBX_NOT_FINISHED) {
  2366. mempool_free(cfglink_mbox, phba->mbox_mem_pool);
  2367. goto out;
  2368. }
  2369. } else {
  2370. vport->port_state = LPFC_VPORT_UNKNOWN;
  2371. /*
  2372. * Add the driver's default FCF record at FCF index 0 now. This
  2373. * is phase 1 implementation that support FCF index 0 and driver
  2374. * defaults.
  2375. */
  2376. if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
  2377. fcf_record = kzalloc(sizeof(struct fcf_record),
  2378. GFP_KERNEL);
  2379. if (unlikely(!fcf_record)) {
  2380. lpfc_printf_log(phba, KERN_ERR,
  2381. LOG_MBOX | LOG_SLI,
  2382. "2554 Could not allocate memmory for "
  2383. "fcf record\n");
  2384. rc = -ENODEV;
  2385. goto out;
  2386. }
  2387. lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
  2388. LPFC_FCOE_FCF_DEF_INDEX);
  2389. rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
  2390. if (unlikely(rc)) {
  2391. lpfc_printf_log(phba, KERN_ERR,
  2392. LOG_MBOX | LOG_SLI,
  2393. "2013 Could not manually add FCF "
  2394. "record 0, status %d\n", rc);
  2395. rc = -ENODEV;
  2396. kfree(fcf_record);
  2397. goto out;
  2398. }
  2399. kfree(fcf_record);
  2400. }
  2401. /*
  2402. * The driver is expected to do FIP/FCF. Call the port
  2403. * and get the FCF Table.
  2404. */
  2405. spin_lock_irq(&phba->hbalock);
  2406. if (phba->hba_flag & FCF_DISC_INPROGRESS) {
  2407. spin_unlock_irq(&phba->hbalock);
  2408. return;
  2409. }
  2410. /* This is the initial FCF discovery scan */
  2411. phba->fcf.fcf_flag |= FCF_INIT_DISC;
  2412. spin_unlock_irq(&phba->hbalock);
  2413. lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
  2414. "2778 Start FCF table scan at linkup\n");
  2415. rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
  2416. LPFC_FCOE_FCF_GET_FIRST);
  2417. if (rc) {
  2418. spin_lock_irq(&phba->hbalock);
  2419. phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
  2420. spin_unlock_irq(&phba->hbalock);
  2421. goto out;
  2422. }
  2423. }
  2424. return;
  2425. out:
  2426. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2427. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  2428. "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
  2429. vport->port_state, sparam_mbox, cfglink_mbox);
  2430. lpfc_issue_clear_la(phba, vport);
  2431. return;
  2432. }
  2433. static void
  2434. lpfc_enable_la(struct lpfc_hba *phba)
  2435. {
  2436. uint32_t control;
  2437. struct lpfc_sli *psli = &phba->sli;
  2438. spin_lock_irq(&phba->hbalock);
  2439. psli->sli_flag |= LPFC_PROCESS_LA;
  2440. if (phba->sli_rev <= LPFC_SLI_REV3) {
  2441. control = readl(phba->HCregaddr);
  2442. control |= HC_LAINT_ENA;
  2443. writel(control, phba->HCregaddr);
  2444. readl(phba->HCregaddr); /* flush */
  2445. }
  2446. spin_unlock_irq(&phba->hbalock);
  2447. }
  2448. static void
  2449. lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
  2450. {
  2451. lpfc_linkdown(phba);
  2452. lpfc_enable_la(phba);
  2453. lpfc_unregister_unused_fcf(phba);
  2454. /* turn on Link Attention interrupts - no CLEAR_LA needed */
  2455. }
  2456. /*
  2457. * This routine handles processing a READ_LA mailbox
  2458. * command upon completion. It is setup in the LPFC_MBOXQ
  2459. * as the completion routine when the command is
  2460. * handed off to the SLI layer.
  2461. */
  2462. void
  2463. lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2464. {
  2465. struct lpfc_vport *vport = pmb->vport;
  2466. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2467. READ_LA_VAR *la;
  2468. MAILBOX_t *mb = &pmb->u.mb;
  2469. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  2470. /* Unblock ELS traffic */
  2471. phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
  2472. /* Check for error */
  2473. if (mb->mbxStatus) {
  2474. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  2475. "1307 READ_LA mbox error x%x state x%x\n",
  2476. mb->mbxStatus, vport->port_state);
  2477. lpfc_mbx_issue_link_down(phba);
  2478. phba->link_state = LPFC_HBA_ERROR;
  2479. goto lpfc_mbx_cmpl_read_la_free_mbuf;
  2480. }
  2481. la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
  2482. memcpy(&phba->alpa_map[0], mp->virt, 128);
  2483. spin_lock_irq(shost->host_lock);
  2484. if (la->pb)
  2485. vport->fc_flag |= FC_BYPASSED_MODE;
  2486. else
  2487. vport->fc_flag &= ~FC_BYPASSED_MODE;
  2488. spin_unlock_irq(shost->host_lock);
  2489. if ((phba->fc_eventTag < la->eventTag) ||
  2490. (phba->fc_eventTag == la->eventTag)) {
  2491. phba->fc_stat.LinkMultiEvent++;
  2492. if (la->attType == AT_LINK_UP)
  2493. if (phba->fc_eventTag != 0)
  2494. lpfc_linkdown(phba);
  2495. }
  2496. phba->fc_eventTag = la->eventTag;
  2497. spin_lock_irq(&phba->hbalock);
  2498. if (la->mm)
  2499. phba->sli.sli_flag |= LPFC_MENLO_MAINT;
  2500. else
  2501. phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
  2502. spin_unlock_irq(&phba->hbalock);
  2503. phba->link_events++;
  2504. if (la->attType == AT_LINK_UP && (!la->mm)) {
  2505. phba->fc_stat.LinkUp++;
  2506. if (phba->link_flag & LS_LOOPBACK_MODE) {
  2507. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2508. "1306 Link Up Event in loop back mode "
  2509. "x%x received Data: x%x x%x x%x x%x\n",
  2510. la->eventTag, phba->fc_eventTag,
  2511. la->granted_AL_PA, la->UlnkSpeed,
  2512. phba->alpa_map[0]);
  2513. } else {
  2514. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2515. "1303 Link Up Event x%x received "
  2516. "Data: x%x x%x x%x x%x x%x x%x %d\n",
  2517. la->eventTag, phba->fc_eventTag,
  2518. la->granted_AL_PA, la->UlnkSpeed,
  2519. phba->alpa_map[0],
  2520. la->mm, la->fa,
  2521. phba->wait_4_mlo_maint_flg);
  2522. }
  2523. lpfc_mbx_process_link_up(phba, la);
  2524. } else if (la->attType == AT_LINK_DOWN) {
  2525. phba->fc_stat.LinkDown++;
  2526. if (phba->link_flag & LS_LOOPBACK_MODE) {
  2527. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2528. "1308 Link Down Event in loop back mode "
  2529. "x%x received "
  2530. "Data: x%x x%x x%x\n",
  2531. la->eventTag, phba->fc_eventTag,
  2532. phba->pport->port_state, vport->fc_flag);
  2533. }
  2534. else {
  2535. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2536. "1305 Link Down Event x%x received "
  2537. "Data: x%x x%x x%x x%x x%x\n",
  2538. la->eventTag, phba->fc_eventTag,
  2539. phba->pport->port_state, vport->fc_flag,
  2540. la->mm, la->fa);
  2541. }
  2542. lpfc_mbx_issue_link_down(phba);
  2543. }
  2544. if (la->mm && la->attType == AT_LINK_UP) {
  2545. if (phba->link_state != LPFC_LINK_DOWN) {
  2546. phba->fc_stat.LinkDown++;
  2547. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2548. "1312 Link Down Event x%x received "
  2549. "Data: x%x x%x x%x\n",
  2550. la->eventTag, phba->fc_eventTag,
  2551. phba->pport->port_state, vport->fc_flag);
  2552. lpfc_mbx_issue_link_down(phba);
  2553. } else
  2554. lpfc_enable_la(phba);
  2555. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  2556. "1310 Menlo Maint Mode Link up Event x%x rcvd "
  2557. "Data: x%x x%x x%x\n",
  2558. la->eventTag, phba->fc_eventTag,
  2559. phba->pport->port_state, vport->fc_flag);
  2560. /*
  2561. * The cmnd that triggered this will be waiting for this
  2562. * signal.
  2563. */
  2564. /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
  2565. if (phba->wait_4_mlo_maint_flg) {
  2566. phba->wait_4_mlo_maint_flg = 0;
  2567. wake_up_interruptible(&phba->wait_4_mlo_m_q);
  2568. }
  2569. }
  2570. if (la->fa) {
  2571. if (la->mm)
  2572. lpfc_issue_clear_la(phba, vport);
  2573. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  2574. "1311 fa %d\n", la->fa);
  2575. }
  2576. lpfc_mbx_cmpl_read_la_free_mbuf:
  2577. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2578. kfree(mp);
  2579. mempool_free(pmb, phba->mbox_mem_pool);
  2580. return;
  2581. }
  2582. /*
  2583. * This routine handles processing a REG_LOGIN mailbox
  2584. * command upon completion. It is setup in the LPFC_MBOXQ
  2585. * as the completion routine when the command is
  2586. * handed off to the SLI layer.
  2587. */
  2588. void
  2589. lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2590. {
  2591. struct lpfc_vport *vport = pmb->vport;
  2592. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  2593. struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
  2594. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2595. pmb->context1 = NULL;
  2596. if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
  2597. ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
  2598. if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
  2599. ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
  2600. /* We rcvd a rscn after issuing this
  2601. * mbox reg login, we may have cycled
  2602. * back through the state and be
  2603. * back at reg login state so this
  2604. * mbox needs to be ignored becase
  2605. * there is another reg login in
  2606. * proccess.
  2607. */
  2608. spin_lock_irq(shost->host_lock);
  2609. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  2610. spin_unlock_irq(shost->host_lock);
  2611. if (phba->sli_rev == LPFC_SLI_REV4)
  2612. lpfc_sli4_free_rpi(phba,
  2613. pmb->u.mb.un.varRegLogin.rpi);
  2614. } else
  2615. /* Good status, call state machine */
  2616. lpfc_disc_state_machine(vport, ndlp, pmb,
  2617. NLP_EVT_CMPL_REG_LOGIN);
  2618. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2619. kfree(mp);
  2620. mempool_free(pmb, phba->mbox_mem_pool);
  2621. /* decrement the node reference count held for this callback
  2622. * function.
  2623. */
  2624. lpfc_nlp_put(ndlp);
  2625. return;
  2626. }
  2627. static void
  2628. lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2629. {
  2630. MAILBOX_t *mb = &pmb->u.mb;
  2631. struct lpfc_vport *vport = pmb->vport;
  2632. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2633. switch (mb->mbxStatus) {
  2634. case 0x0011:
  2635. case 0x0020:
  2636. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  2637. "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
  2638. mb->mbxStatus);
  2639. break;
  2640. /* If VPI is busy, reset the HBA */
  2641. case 0x9700:
  2642. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
  2643. "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
  2644. vport->vpi, mb->mbxStatus);
  2645. if (!(phba->pport->load_flag & FC_UNLOADING))
  2646. lpfc_workq_post_event(phba, NULL, NULL,
  2647. LPFC_EVT_RESET_HBA);
  2648. }
  2649. spin_lock_irq(shost->host_lock);
  2650. vport->vpi_state &= ~LPFC_VPI_REGISTERED;
  2651. vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  2652. spin_unlock_irq(shost->host_lock);
  2653. vport->unreg_vpi_cmpl = VPORT_OK;
  2654. mempool_free(pmb, phba->mbox_mem_pool);
  2655. /*
  2656. * This shost reference might have been taken at the beginning of
  2657. * lpfc_vport_delete()
  2658. */
  2659. if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
  2660. scsi_host_put(shost);
  2661. }
  2662. int
  2663. lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
  2664. {
  2665. struct lpfc_hba *phba = vport->phba;
  2666. LPFC_MBOXQ_t *mbox;
  2667. int rc;
  2668. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2669. if (!mbox)
  2670. return 1;
  2671. lpfc_unreg_vpi(phba, vport->vpi, mbox);
  2672. mbox->vport = vport;
  2673. mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
  2674. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  2675. if (rc == MBX_NOT_FINISHED) {
  2676. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
  2677. "1800 Could not issue unreg_vpi\n");
  2678. mempool_free(mbox, phba->mbox_mem_pool);
  2679. vport->unreg_vpi_cmpl = VPORT_ERROR;
  2680. return rc;
  2681. }
  2682. return 0;
  2683. }
  2684. static void
  2685. lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2686. {
  2687. struct lpfc_vport *vport = pmb->vport;
  2688. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2689. MAILBOX_t *mb = &pmb->u.mb;
  2690. switch (mb->mbxStatus) {
  2691. case 0x0011:
  2692. case 0x9601:
  2693. case 0x9602:
  2694. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  2695. "0912 cmpl_reg_vpi, mb status = 0x%x\n",
  2696. mb->mbxStatus);
  2697. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2698. spin_lock_irq(shost->host_lock);
  2699. vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
  2700. spin_unlock_irq(shost->host_lock);
  2701. vport->fc_myDID = 0;
  2702. goto out;
  2703. }
  2704. spin_lock_irq(shost->host_lock);
  2705. vport->vpi_state |= LPFC_VPI_REGISTERED;
  2706. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  2707. spin_unlock_irq(shost->host_lock);
  2708. vport->num_disc_nodes = 0;
  2709. /* go thru NPR list and issue ELS PLOGIs */
  2710. if (vport->fc_npr_cnt)
  2711. lpfc_els_disc_plogi(vport);
  2712. if (!vport->num_disc_nodes) {
  2713. spin_lock_irq(shost->host_lock);
  2714. vport->fc_flag &= ~FC_NDISC_ACTIVE;
  2715. spin_unlock_irq(shost->host_lock);
  2716. lpfc_can_disctmo(vport);
  2717. }
  2718. vport->port_state = LPFC_VPORT_READY;
  2719. out:
  2720. mempool_free(pmb, phba->mbox_mem_pool);
  2721. return;
  2722. }
  2723. /**
  2724. * lpfc_create_static_vport - Read HBA config region to create static vports.
  2725. * @phba: pointer to lpfc hba data structure.
  2726. *
  2727. * This routine issue a DUMP mailbox command for config region 22 to get
  2728. * the list of static vports to be created. The function create vports
  2729. * based on the information returned from the HBA.
  2730. **/
  2731. void
  2732. lpfc_create_static_vport(struct lpfc_hba *phba)
  2733. {
  2734. LPFC_MBOXQ_t *pmb = NULL;
  2735. MAILBOX_t *mb;
  2736. struct static_vport_info *vport_info;
  2737. int rc = 0, i;
  2738. struct fc_vport_identifiers vport_id;
  2739. struct fc_vport *new_fc_vport;
  2740. struct Scsi_Host *shost;
  2741. struct lpfc_vport *vport;
  2742. uint16_t offset = 0;
  2743. uint8_t *vport_buff;
  2744. struct lpfc_dmabuf *mp;
  2745. uint32_t byte_count = 0;
  2746. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2747. if (!pmb) {
  2748. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2749. "0542 lpfc_create_static_vport failed to"
  2750. " allocate mailbox memory\n");
  2751. return;
  2752. }
  2753. mb = &pmb->u.mb;
  2754. vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
  2755. if (!vport_info) {
  2756. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2757. "0543 lpfc_create_static_vport failed to"
  2758. " allocate vport_info\n");
  2759. mempool_free(pmb, phba->mbox_mem_pool);
  2760. return;
  2761. }
  2762. vport_buff = (uint8_t *) vport_info;
  2763. do {
  2764. if (lpfc_dump_static_vport(phba, pmb, offset))
  2765. goto out;
  2766. pmb->vport = phba->pport;
  2767. rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
  2768. if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
  2769. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  2770. "0544 lpfc_create_static_vport failed to"
  2771. " issue dump mailbox command ret 0x%x "
  2772. "status 0x%x\n",
  2773. rc, mb->mbxStatus);
  2774. goto out;
  2775. }
  2776. if (phba->sli_rev == LPFC_SLI_REV4) {
  2777. byte_count = pmb->u.mqe.un.mb_words[5];
  2778. mp = (struct lpfc_dmabuf *) pmb->context2;
  2779. if (byte_count > sizeof(struct static_vport_info) -
  2780. offset)
  2781. byte_count = sizeof(struct static_vport_info)
  2782. - offset;
  2783. memcpy(vport_buff + offset, mp->virt, byte_count);
  2784. offset += byte_count;
  2785. } else {
  2786. if (mb->un.varDmp.word_cnt >
  2787. sizeof(struct static_vport_info) - offset)
  2788. mb->un.varDmp.word_cnt =
  2789. sizeof(struct static_vport_info)
  2790. - offset;
  2791. byte_count = mb->un.varDmp.word_cnt;
  2792. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  2793. vport_buff + offset,
  2794. byte_count);
  2795. offset += byte_count;
  2796. }
  2797. } while (byte_count &&
  2798. offset < sizeof(struct static_vport_info));
  2799. if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
  2800. ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
  2801. != VPORT_INFO_REV)) {
  2802. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2803. "0545 lpfc_create_static_vport bad"
  2804. " information header 0x%x 0x%x\n",
  2805. le32_to_cpu(vport_info->signature),
  2806. le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
  2807. goto out;
  2808. }
  2809. shost = lpfc_shost_from_vport(phba->pport);
  2810. for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
  2811. memset(&vport_id, 0, sizeof(vport_id));
  2812. vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
  2813. vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
  2814. if (!vport_id.port_name || !vport_id.node_name)
  2815. continue;
  2816. vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
  2817. vport_id.vport_type = FC_PORTTYPE_NPIV;
  2818. vport_id.disable = false;
  2819. new_fc_vport = fc_vport_create(shost, 0, &vport_id);
  2820. if (!new_fc_vport) {
  2821. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  2822. "0546 lpfc_create_static_vport failed to"
  2823. " create vport\n");
  2824. continue;
  2825. }
  2826. vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
  2827. vport->vport_flag |= STATIC_VPORT;
  2828. }
  2829. out:
  2830. kfree(vport_info);
  2831. if (rc != MBX_TIMEOUT) {
  2832. if (pmb->context2) {
  2833. mp = (struct lpfc_dmabuf *) pmb->context2;
  2834. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2835. kfree(mp);
  2836. }
  2837. mempool_free(pmb, phba->mbox_mem_pool);
  2838. }
  2839. return;
  2840. }
  2841. /*
  2842. * This routine handles processing a Fabric REG_LOGIN mailbox
  2843. * command upon completion. It is setup in the LPFC_MBOXQ
  2844. * as the completion routine when the command is
  2845. * handed off to the SLI layer.
  2846. */
  2847. void
  2848. lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2849. {
  2850. struct lpfc_vport *vport = pmb->vport;
  2851. MAILBOX_t *mb = &pmb->u.mb;
  2852. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  2853. struct lpfc_nodelist *ndlp;
  2854. ndlp = (struct lpfc_nodelist *) pmb->context2;
  2855. pmb->context1 = NULL;
  2856. pmb->context2 = NULL;
  2857. if (mb->mbxStatus) {
  2858. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  2859. "0258 Register Fabric login error: 0x%x\n",
  2860. mb->mbxStatus);
  2861. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2862. kfree(mp);
  2863. mempool_free(pmb, phba->mbox_mem_pool);
  2864. if (phba->fc_topology == TOPOLOGY_LOOP) {
  2865. /* FLOGI failed, use loop map to make discovery list */
  2866. lpfc_disc_list_loopmap(vport);
  2867. /* Start discovery */
  2868. lpfc_disc_start(vport);
  2869. /* Decrement the reference count to ndlp after the
  2870. * reference to the ndlp are done.
  2871. */
  2872. lpfc_nlp_put(ndlp);
  2873. return;
  2874. }
  2875. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2876. /* Decrement the reference count to ndlp after the reference
  2877. * to the ndlp are done.
  2878. */
  2879. lpfc_nlp_put(ndlp);
  2880. return;
  2881. }
  2882. ndlp->nlp_rpi = mb->un.varWords[0];
  2883. ndlp->nlp_flag |= NLP_RPI_VALID;
  2884. ndlp->nlp_type |= NLP_FABRIC;
  2885. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  2886. if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
  2887. /* when physical port receive logo donot start
  2888. * vport discovery */
  2889. if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
  2890. lpfc_start_fdiscs(phba);
  2891. else
  2892. vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
  2893. lpfc_do_scr_ns_plogi(phba, vport);
  2894. }
  2895. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2896. kfree(mp);
  2897. mempool_free(pmb, phba->mbox_mem_pool);
  2898. /* Drop the reference count from the mbox at the end after
  2899. * all the current reference to the ndlp have been done.
  2900. */
  2901. lpfc_nlp_put(ndlp);
  2902. return;
  2903. }
  2904. /*
  2905. * This routine handles processing a NameServer REG_LOGIN mailbox
  2906. * command upon completion. It is setup in the LPFC_MBOXQ
  2907. * as the completion routine when the command is
  2908. * handed off to the SLI layer.
  2909. */
  2910. void
  2911. lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2912. {
  2913. MAILBOX_t *mb = &pmb->u.mb;
  2914. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  2915. struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
  2916. struct lpfc_vport *vport = pmb->vport;
  2917. if (mb->mbxStatus) {
  2918. out:
  2919. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  2920. "0260 Register NameServer error: 0x%x\n",
  2921. mb->mbxStatus);
  2922. /* decrement the node reference count held for this
  2923. * callback function.
  2924. */
  2925. lpfc_nlp_put(ndlp);
  2926. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2927. kfree(mp);
  2928. mempool_free(pmb, phba->mbox_mem_pool);
  2929. /* If no other thread is using the ndlp, free it */
  2930. lpfc_nlp_not_used(ndlp);
  2931. if (phba->fc_topology == TOPOLOGY_LOOP) {
  2932. /*
  2933. * RegLogin failed, use loop map to make discovery
  2934. * list
  2935. */
  2936. lpfc_disc_list_loopmap(vport);
  2937. /* Start discovery */
  2938. lpfc_disc_start(vport);
  2939. return;
  2940. }
  2941. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2942. return;
  2943. }
  2944. pmb->context1 = NULL;
  2945. ndlp->nlp_rpi = mb->un.varWords[0];
  2946. ndlp->nlp_flag |= NLP_RPI_VALID;
  2947. ndlp->nlp_type |= NLP_FABRIC;
  2948. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  2949. if (vport->port_state < LPFC_VPORT_READY) {
  2950. /* Link up discovery requires Fabric registration. */
  2951. lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
  2952. lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
  2953. lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
  2954. lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
  2955. lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
  2956. /* Issue SCR just before NameServer GID_FT Query */
  2957. lpfc_issue_els_scr(vport, SCR_DID, 0);
  2958. }
  2959. vport->fc_ns_retry = 0;
  2960. /* Good status, issue CT Request to NameServer */
  2961. if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
  2962. /* Cannot issue NameServer Query, so finish up discovery */
  2963. goto out;
  2964. }
  2965. /* decrement the node reference count held for this
  2966. * callback function.
  2967. */
  2968. lpfc_nlp_put(ndlp);
  2969. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2970. kfree(mp);
  2971. mempool_free(pmb, phba->mbox_mem_pool);
  2972. return;
  2973. }
  2974. static void
  2975. lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  2976. {
  2977. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2978. struct fc_rport *rport;
  2979. struct lpfc_rport_data *rdata;
  2980. struct fc_rport_identifiers rport_ids;
  2981. struct lpfc_hba *phba = vport->phba;
  2982. /* Remote port has reappeared. Re-register w/ FC transport */
  2983. rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
  2984. rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
  2985. rport_ids.port_id = ndlp->nlp_DID;
  2986. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  2987. /*
  2988. * We leave our node pointer in rport->dd_data when we unregister a
  2989. * FCP target port. But fc_remote_port_add zeros the space to which
  2990. * rport->dd_data points. So, if we're reusing a previously
  2991. * registered port, drop the reference that we took the last time we
  2992. * registered the port.
  2993. */
  2994. if (ndlp->rport && ndlp->rport->dd_data &&
  2995. ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
  2996. lpfc_nlp_put(ndlp);
  2997. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
  2998. "rport add: did:x%x flg:x%x type x%x",
  2999. ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
  3000. ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
  3001. if (!rport || !get_device(&rport->dev)) {
  3002. dev_printk(KERN_WARNING, &phba->pcidev->dev,
  3003. "Warning: fc_remote_port_add failed\n");
  3004. return;
  3005. }
  3006. /* initialize static port data */
  3007. rport->maxframe_size = ndlp->nlp_maxframe;
  3008. rport->supported_classes = ndlp->nlp_class_sup;
  3009. rdata = rport->dd_data;
  3010. rdata->pnode = lpfc_nlp_get(ndlp);
  3011. if (ndlp->nlp_type & NLP_FCP_TARGET)
  3012. rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
  3013. if (ndlp->nlp_type & NLP_FCP_INITIATOR)
  3014. rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  3015. if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
  3016. fc_remote_port_rolechg(rport, rport_ids.roles);
  3017. if ((rport->scsi_target_id != -1) &&
  3018. (rport->scsi_target_id < LPFC_MAX_TARGET)) {
  3019. ndlp->nlp_sid = rport->scsi_target_id;
  3020. }
  3021. return;
  3022. }
  3023. static void
  3024. lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
  3025. {
  3026. struct fc_rport *rport = ndlp->rport;
  3027. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
  3028. "rport delete: did:x%x flg:x%x type x%x",
  3029. ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
  3030. fc_remote_port_delete(rport);
  3031. return;
  3032. }
  3033. static void
  3034. lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
  3035. {
  3036. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3037. spin_lock_irq(shost->host_lock);
  3038. switch (state) {
  3039. case NLP_STE_UNUSED_NODE:
  3040. vport->fc_unused_cnt += count;
  3041. break;
  3042. case NLP_STE_PLOGI_ISSUE:
  3043. vport->fc_plogi_cnt += count;
  3044. break;
  3045. case NLP_STE_ADISC_ISSUE:
  3046. vport->fc_adisc_cnt += count;
  3047. break;
  3048. case NLP_STE_REG_LOGIN_ISSUE:
  3049. vport->fc_reglogin_cnt += count;
  3050. break;
  3051. case NLP_STE_PRLI_ISSUE:
  3052. vport->fc_prli_cnt += count;
  3053. break;
  3054. case NLP_STE_UNMAPPED_NODE:
  3055. vport->fc_unmap_cnt += count;
  3056. break;
  3057. case NLP_STE_MAPPED_NODE:
  3058. vport->fc_map_cnt += count;
  3059. break;
  3060. case NLP_STE_NPR_NODE:
  3061. vport->fc_npr_cnt += count;
  3062. break;
  3063. }
  3064. spin_unlock_irq(shost->host_lock);
  3065. }
  3066. static void
  3067. lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  3068. int old_state, int new_state)
  3069. {
  3070. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3071. if (new_state == NLP_STE_UNMAPPED_NODE) {
  3072. ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
  3073. ndlp->nlp_type |= NLP_FC_NODE;
  3074. }
  3075. if (new_state == NLP_STE_MAPPED_NODE)
  3076. ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
  3077. if (new_state == NLP_STE_NPR_NODE)
  3078. ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
  3079. /* Transport interface */
  3080. if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
  3081. old_state == NLP_STE_UNMAPPED_NODE)) {
  3082. vport->phba->nport_event_cnt++;
  3083. lpfc_unregister_remote_port(ndlp);
  3084. }
  3085. if (new_state == NLP_STE_MAPPED_NODE ||
  3086. new_state == NLP_STE_UNMAPPED_NODE) {
  3087. vport->phba->nport_event_cnt++;
  3088. /*
  3089. * Tell the fc transport about the port, if we haven't
  3090. * already. If we have, and it's a scsi entity, be
  3091. * sure to unblock any attached scsi devices
  3092. */
  3093. lpfc_register_remote_port(vport, ndlp);
  3094. }
  3095. if ((new_state == NLP_STE_MAPPED_NODE) &&
  3096. (vport->stat_data_enabled)) {
  3097. /*
  3098. * A new target is discovered, if there is no buffer for
  3099. * statistical data collection allocate buffer.
  3100. */
  3101. ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
  3102. sizeof(struct lpfc_scsicmd_bkt),
  3103. GFP_KERNEL);
  3104. if (!ndlp->lat_data)
  3105. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
  3106. "0286 lpfc_nlp_state_cleanup failed to "
  3107. "allocate statistical data buffer DID "
  3108. "0x%x\n", ndlp->nlp_DID);
  3109. }
  3110. /*
  3111. * if we added to Mapped list, but the remote port
  3112. * registration failed or assigned a target id outside
  3113. * our presentable range - move the node to the
  3114. * Unmapped List
  3115. */
  3116. if (new_state == NLP_STE_MAPPED_NODE &&
  3117. (!ndlp->rport ||
  3118. ndlp->rport->scsi_target_id == -1 ||
  3119. ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
  3120. spin_lock_irq(shost->host_lock);
  3121. ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
  3122. spin_unlock_irq(shost->host_lock);
  3123. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  3124. }
  3125. }
  3126. static char *
  3127. lpfc_nlp_state_name(char *buffer, size_t size, int state)
  3128. {
  3129. static char *states[] = {
  3130. [NLP_STE_UNUSED_NODE] = "UNUSED",
  3131. [NLP_STE_PLOGI_ISSUE] = "PLOGI",
  3132. [NLP_STE_ADISC_ISSUE] = "ADISC",
  3133. [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
  3134. [NLP_STE_PRLI_ISSUE] = "PRLI",
  3135. [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
  3136. [NLP_STE_MAPPED_NODE] = "MAPPED",
  3137. [NLP_STE_NPR_NODE] = "NPR",
  3138. };
  3139. if (state < NLP_STE_MAX_STATE && states[state])
  3140. strlcpy(buffer, states[state], size);
  3141. else
  3142. snprintf(buffer, size, "unknown (%d)", state);
  3143. return buffer;
  3144. }
  3145. void
  3146. lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  3147. int state)
  3148. {
  3149. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3150. int old_state = ndlp->nlp_state;
  3151. char name1[16], name2[16];
  3152. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  3153. "0904 NPort state transition x%06x, %s -> %s\n",
  3154. ndlp->nlp_DID,
  3155. lpfc_nlp_state_name(name1, sizeof(name1), old_state),
  3156. lpfc_nlp_state_name(name2, sizeof(name2), state));
  3157. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
  3158. "node statechg did:x%x old:%d ste:%d",
  3159. ndlp->nlp_DID, old_state, state);
  3160. if (old_state == NLP_STE_NPR_NODE &&
  3161. state != NLP_STE_NPR_NODE)
  3162. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  3163. if (old_state == NLP_STE_UNMAPPED_NODE) {
  3164. ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
  3165. ndlp->nlp_type &= ~NLP_FC_NODE;
  3166. }
  3167. if (list_empty(&ndlp->nlp_listp)) {
  3168. spin_lock_irq(shost->host_lock);
  3169. list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
  3170. spin_unlock_irq(shost->host_lock);
  3171. } else if (old_state)
  3172. lpfc_nlp_counters(vport, old_state, -1);
  3173. ndlp->nlp_state = state;
  3174. lpfc_nlp_counters(vport, state, 1);
  3175. lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
  3176. }
  3177. void
  3178. lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  3179. {
  3180. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3181. if (list_empty(&ndlp->nlp_listp)) {
  3182. spin_lock_irq(shost->host_lock);
  3183. list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
  3184. spin_unlock_irq(shost->host_lock);
  3185. }
  3186. }
  3187. void
  3188. lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  3189. {
  3190. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3191. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  3192. if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
  3193. lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
  3194. spin_lock_irq(shost->host_lock);
  3195. list_del_init(&ndlp->nlp_listp);
  3196. spin_unlock_irq(shost->host_lock);
  3197. lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
  3198. NLP_STE_UNUSED_NODE);
  3199. }
  3200. static void
  3201. lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  3202. {
  3203. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  3204. if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
  3205. lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
  3206. lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
  3207. NLP_STE_UNUSED_NODE);
  3208. }
  3209. /**
  3210. * lpfc_initialize_node - Initialize all fields of node object
  3211. * @vport: Pointer to Virtual Port object.
  3212. * @ndlp: Pointer to FC node object.
  3213. * @did: FC_ID of the node.
  3214. *
  3215. * This function is always called when node object need to be initialized.
  3216. * It initializes all the fields of the node object. Although the reference
  3217. * to phba from @ndlp can be obtained indirectly through it's reference to
  3218. * @vport, a direct reference to phba is taken here by @ndlp. This is due
  3219. * to the life-span of the @ndlp might go beyond the existence of @vport as
  3220. * the final release of ndlp is determined by its reference count. And, the
  3221. * operation on @ndlp needs the reference to phba.
  3222. **/
  3223. static inline void
  3224. lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  3225. uint32_t did)
  3226. {
  3227. INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
  3228. INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
  3229. init_timer(&ndlp->nlp_delayfunc);
  3230. ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
  3231. ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
  3232. ndlp->nlp_DID = did;
  3233. ndlp->vport = vport;
  3234. ndlp->phba = vport->phba;
  3235. ndlp->nlp_sid = NLP_NO_SID;
  3236. kref_init(&ndlp->kref);
  3237. NLP_INT_NODE_ACT(ndlp);
  3238. atomic_set(&ndlp->cmd_pending, 0);
  3239. ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
  3240. }
  3241. struct lpfc_nodelist *
  3242. lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  3243. int state)
  3244. {
  3245. struct lpfc_hba *phba = vport->phba;
  3246. uint32_t did;
  3247. unsigned long flags;
  3248. if (!ndlp)
  3249. return NULL;
  3250. spin_lock_irqsave(&phba->ndlp_lock, flags);
  3251. /* The ndlp should not be in memory free mode */
  3252. if (NLP_CHK_FREE_REQ(ndlp)) {
  3253. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  3254. lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
  3255. "0277 lpfc_enable_node: ndlp:x%p "
  3256. "usgmap:x%x refcnt:%d\n",
  3257. (void *)ndlp, ndlp->nlp_usg_map,
  3258. atomic_read(&ndlp->kref.refcount));
  3259. return NULL;
  3260. }
  3261. /* The ndlp should not already be in active mode */
  3262. if (NLP_CHK_NODE_ACT(ndlp)) {
  3263. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  3264. lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
  3265. "0278 lpfc_enable_node: ndlp:x%p "
  3266. "usgmap:x%x refcnt:%d\n",
  3267. (void *)ndlp, ndlp->nlp_usg_map,
  3268. atomic_read(&ndlp->kref.refcount));
  3269. return NULL;
  3270. }
  3271. /* Keep the original DID */
  3272. did = ndlp->nlp_DID;
  3273. /* re-initialize ndlp except of ndlp linked list pointer */
  3274. memset((((char *)ndlp) + sizeof (struct list_head)), 0,
  3275. sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
  3276. lpfc_initialize_node(vport, ndlp, did);
  3277. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  3278. if (state != NLP_STE_UNUSED_NODE)
  3279. lpfc_nlp_set_state(vport, ndlp, state);
  3280. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
  3281. "node enable: did:x%x",
  3282. ndlp->nlp_DID, 0, 0);
  3283. return ndlp;
  3284. }
  3285. void
  3286. lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  3287. {
  3288. /*
  3289. * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
  3290. * be used if we wish to issue the "last" lpfc_nlp_put() to remove
  3291. * the ndlp from the vport. The ndlp marked as UNUSED on the list
  3292. * until ALL other outstanding threads have completed. We check
  3293. * that the ndlp not already in the UNUSED state before we proceed.
  3294. */
  3295. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  3296. return;
  3297. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
  3298. lpfc_nlp_put(ndlp);
  3299. return;
  3300. }
  3301. /*
  3302. * Start / ReStart rescue timer for Discovery / RSCN handling
  3303. */
  3304. void
  3305. lpfc_set_disctmo(struct lpfc_vport *vport)
  3306. {
  3307. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3308. struct lpfc_hba *phba = vport->phba;
  3309. uint32_t tmo;
  3310. if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
  3311. /* For FAN, timeout should be greater than edtov */
  3312. tmo = (((phba->fc_edtov + 999) / 1000) + 1);
  3313. } else {
  3314. /* Normal discovery timeout should be > than ELS/CT timeout
  3315. * FC spec states we need 3 * ratov for CT requests
  3316. */
  3317. tmo = ((phba->fc_ratov * 3) + 3);
  3318. }
  3319. if (!timer_pending(&vport->fc_disctmo)) {
  3320. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  3321. "set disc timer: tmo:x%x state:x%x flg:x%x",
  3322. tmo, vport->port_state, vport->fc_flag);
  3323. }
  3324. mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
  3325. spin_lock_irq(shost->host_lock);
  3326. vport->fc_flag |= FC_DISC_TMO;
  3327. spin_unlock_irq(shost->host_lock);
  3328. /* Start Discovery Timer state <hba_state> */
  3329. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  3330. "0247 Start Discovery Timer state x%x "
  3331. "Data: x%x x%lx x%x x%x\n",
  3332. vport->port_state, tmo,
  3333. (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
  3334. vport->fc_adisc_cnt);
  3335. return;
  3336. }
  3337. /*
  3338. * Cancel rescue timer for Discovery / RSCN handling
  3339. */
  3340. int
  3341. lpfc_can_disctmo(struct lpfc_vport *vport)
  3342. {
  3343. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3344. unsigned long iflags;
  3345. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  3346. "can disc timer: state:x%x rtry:x%x flg:x%x",
  3347. vport->port_state, vport->fc_ns_retry, vport->fc_flag);
  3348. /* Turn off discovery timer if its running */
  3349. if (vport->fc_flag & FC_DISC_TMO) {
  3350. spin_lock_irqsave(shost->host_lock, iflags);
  3351. vport->fc_flag &= ~FC_DISC_TMO;
  3352. spin_unlock_irqrestore(shost->host_lock, iflags);
  3353. del_timer_sync(&vport->fc_disctmo);
  3354. spin_lock_irqsave(&vport->work_port_lock, iflags);
  3355. vport->work_port_events &= ~WORKER_DISC_TMO;
  3356. spin_unlock_irqrestore(&vport->work_port_lock, iflags);
  3357. }
  3358. /* Cancel Discovery Timer state <hba_state> */
  3359. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  3360. "0248 Cancel Discovery Timer state x%x "
  3361. "Data: x%x x%x x%x\n",
  3362. vport->port_state, vport->fc_flag,
  3363. vport->fc_plogi_cnt, vport->fc_adisc_cnt);
  3364. return 0;
  3365. }
  3366. /*
  3367. * Check specified ring for outstanding IOCB on the SLI queue
  3368. * Return true if iocb matches the specified nport
  3369. */
  3370. int
  3371. lpfc_check_sli_ndlp(struct lpfc_hba *phba,
  3372. struct lpfc_sli_ring *pring,
  3373. struct lpfc_iocbq *iocb,
  3374. struct lpfc_nodelist *ndlp)
  3375. {
  3376. struct lpfc_sli *psli = &phba->sli;
  3377. IOCB_t *icmd = &iocb->iocb;
  3378. struct lpfc_vport *vport = ndlp->vport;
  3379. if (iocb->vport != vport)
  3380. return 0;
  3381. if (pring->ringno == LPFC_ELS_RING) {
  3382. switch (icmd->ulpCommand) {
  3383. case CMD_GEN_REQUEST64_CR:
  3384. if (iocb->context_un.ndlp == ndlp)
  3385. return 1;
  3386. case CMD_ELS_REQUEST64_CR:
  3387. if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
  3388. return 1;
  3389. case CMD_XMIT_ELS_RSP64_CX:
  3390. if (iocb->context1 == (uint8_t *) ndlp)
  3391. return 1;
  3392. }
  3393. } else if (pring->ringno == psli->extra_ring) {
  3394. } else if (pring->ringno == psli->fcp_ring) {
  3395. /* Skip match check if waiting to relogin to FCP target */
  3396. if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
  3397. (ndlp->nlp_flag & NLP_DELAY_TMO)) {
  3398. return 0;
  3399. }
  3400. if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
  3401. return 1;
  3402. }
  3403. } else if (pring->ringno == psli->next_ring) {
  3404. }
  3405. return 0;
  3406. }
  3407. /*
  3408. * Free resources / clean up outstanding I/Os
  3409. * associated with nlp_rpi in the LPFC_NODELIST entry.
  3410. */
  3411. static int
  3412. lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  3413. {
  3414. LIST_HEAD(completions);
  3415. struct lpfc_sli *psli;
  3416. struct lpfc_sli_ring *pring;
  3417. struct lpfc_iocbq *iocb, *next_iocb;
  3418. uint32_t i;
  3419. lpfc_fabric_abort_nport(ndlp);
  3420. /*
  3421. * Everything that matches on txcmplq will be returned
  3422. * by firmware with a no rpi error.
  3423. */
  3424. psli = &phba->sli;
  3425. if (ndlp->nlp_flag & NLP_RPI_VALID) {
  3426. /* Now process each ring */
  3427. for (i = 0; i < psli->num_rings; i++) {
  3428. pring = &psli->ring[i];
  3429. spin_lock_irq(&phba->hbalock);
  3430. list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
  3431. list) {
  3432. /*
  3433. * Check to see if iocb matches the nport we are
  3434. * looking for
  3435. */
  3436. if ((lpfc_check_sli_ndlp(phba, pring, iocb,
  3437. ndlp))) {
  3438. /* It matches, so deque and call compl
  3439. with an error */
  3440. list_move_tail(&iocb->list,
  3441. &completions);
  3442. pring->txq_cnt--;
  3443. }
  3444. }
  3445. spin_unlock_irq(&phba->hbalock);
  3446. }
  3447. }
  3448. /* Cancel all the IOCBs from the completions list */
  3449. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3450. IOERR_SLI_ABORTED);
  3451. return 0;
  3452. }
  3453. /*
  3454. * Free rpi associated with LPFC_NODELIST entry.
  3455. * This routine is called from lpfc_freenode(), when we are removing
  3456. * a LPFC_NODELIST entry. It is also called if the driver initiates a
  3457. * LOGO that completes successfully, and we are waiting to PLOGI back
  3458. * to the remote NPort. In addition, it is called after we receive
  3459. * and unsolicated ELS cmd, send back a rsp, the rsp completes and
  3460. * we are waiting to PLOGI back to the remote NPort.
  3461. */
  3462. int
  3463. lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  3464. {
  3465. struct lpfc_hba *phba = vport->phba;
  3466. LPFC_MBOXQ_t *mbox;
  3467. int rc;
  3468. if (ndlp->nlp_flag & NLP_RPI_VALID) {
  3469. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3470. if (mbox) {
  3471. lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
  3472. mbox->vport = vport;
  3473. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3474. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  3475. if (rc == MBX_NOT_FINISHED)
  3476. mempool_free(mbox, phba->mbox_mem_pool);
  3477. }
  3478. lpfc_no_rpi(phba, ndlp);
  3479. ndlp->nlp_rpi = 0;
  3480. ndlp->nlp_flag &= ~NLP_RPI_VALID;
  3481. ndlp->nlp_flag &= ~NLP_NPR_ADISC;
  3482. return 1;
  3483. }
  3484. return 0;
  3485. }
  3486. /**
  3487. * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
  3488. * @phba: pointer to lpfc hba data structure.
  3489. *
  3490. * This routine is invoked to unregister all the currently registered RPIs
  3491. * to the HBA.
  3492. **/
  3493. void
  3494. lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
  3495. {
  3496. struct lpfc_vport **vports;
  3497. struct lpfc_nodelist *ndlp;
  3498. struct Scsi_Host *shost;
  3499. int i;
  3500. vports = lpfc_create_vport_work_array(phba);
  3501. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  3502. shost = lpfc_shost_from_vport(vports[i]);
  3503. spin_lock_irq(shost->host_lock);
  3504. list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
  3505. if (ndlp->nlp_flag & NLP_RPI_VALID) {
  3506. /* The mempool_alloc might sleep */
  3507. spin_unlock_irq(shost->host_lock);
  3508. lpfc_unreg_rpi(vports[i], ndlp);
  3509. spin_lock_irq(shost->host_lock);
  3510. }
  3511. }
  3512. spin_unlock_irq(shost->host_lock);
  3513. }
  3514. lpfc_destroy_vport_work_array(phba, vports);
  3515. }
  3516. void
  3517. lpfc_unreg_all_rpis(struct lpfc_vport *vport)
  3518. {
  3519. struct lpfc_hba *phba = vport->phba;
  3520. LPFC_MBOXQ_t *mbox;
  3521. int rc;
  3522. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3523. if (mbox) {
  3524. lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
  3525. mbox->vport = vport;
  3526. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3527. mbox->context1 = NULL;
  3528. rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  3529. if (rc != MBX_TIMEOUT)
  3530. mempool_free(mbox, phba->mbox_mem_pool);
  3531. if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
  3532. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
  3533. "1836 Could not issue "
  3534. "unreg_login(all_rpis) status %d\n", rc);
  3535. }
  3536. }
  3537. void
  3538. lpfc_unreg_default_rpis(struct lpfc_vport *vport)
  3539. {
  3540. struct lpfc_hba *phba = vport->phba;
  3541. LPFC_MBOXQ_t *mbox;
  3542. int rc;
  3543. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3544. if (mbox) {
  3545. lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
  3546. mbox->vport = vport;
  3547. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3548. mbox->context1 = NULL;
  3549. rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  3550. if (rc != MBX_TIMEOUT)
  3551. mempool_free(mbox, phba->mbox_mem_pool);
  3552. if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
  3553. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
  3554. "1815 Could not issue "
  3555. "unreg_did (default rpis) status %d\n",
  3556. rc);
  3557. }
  3558. }
  3559. /*
  3560. * Free resources associated with LPFC_NODELIST entry
  3561. * so it can be freed.
  3562. */
  3563. static int
  3564. lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  3565. {
  3566. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3567. struct lpfc_hba *phba = vport->phba;
  3568. LPFC_MBOXQ_t *mb, *nextmb;
  3569. struct lpfc_dmabuf *mp;
  3570. /* Cleanup node for NPort <nlp_DID> */
  3571. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  3572. "0900 Cleanup node for NPort x%x "
  3573. "Data: x%x x%x x%x\n",
  3574. ndlp->nlp_DID, ndlp->nlp_flag,
  3575. ndlp->nlp_state, ndlp->nlp_rpi);
  3576. if (NLP_CHK_FREE_REQ(ndlp)) {
  3577. lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
  3578. "0280 lpfc_cleanup_node: ndlp:x%p "
  3579. "usgmap:x%x refcnt:%d\n",
  3580. (void *)ndlp, ndlp->nlp_usg_map,
  3581. atomic_read(&ndlp->kref.refcount));
  3582. lpfc_dequeue_node(vport, ndlp);
  3583. } else {
  3584. lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
  3585. "0281 lpfc_cleanup_node: ndlp:x%p "
  3586. "usgmap:x%x refcnt:%d\n",
  3587. (void *)ndlp, ndlp->nlp_usg_map,
  3588. atomic_read(&ndlp->kref.refcount));
  3589. lpfc_disable_node(vport, ndlp);
  3590. }
  3591. /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
  3592. if ((mb = phba->sli.mbox_active)) {
  3593. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
  3594. (ndlp == (struct lpfc_nodelist *) mb->context2)) {
  3595. mb->context2 = NULL;
  3596. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3597. }
  3598. }
  3599. spin_lock_irq(&phba->hbalock);
  3600. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  3601. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
  3602. (ndlp == (struct lpfc_nodelist *) mb->context2)) {
  3603. mp = (struct lpfc_dmabuf *) (mb->context1);
  3604. if (mp) {
  3605. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3606. kfree(mp);
  3607. }
  3608. list_del(&mb->list);
  3609. if (phba->sli_rev == LPFC_SLI_REV4)
  3610. lpfc_sli4_free_rpi(phba,
  3611. mb->u.mb.un.varRegLogin.rpi);
  3612. mempool_free(mb, phba->mbox_mem_pool);
  3613. /* We shall not invoke the lpfc_nlp_put to decrement
  3614. * the ndlp reference count as we are in the process
  3615. * of lpfc_nlp_release.
  3616. */
  3617. }
  3618. }
  3619. spin_unlock_irq(&phba->hbalock);
  3620. lpfc_els_abort(phba, ndlp);
  3621. spin_lock_irq(shost->host_lock);
  3622. ndlp->nlp_flag &= ~NLP_DELAY_TMO;
  3623. spin_unlock_irq(shost->host_lock);
  3624. ndlp->nlp_last_elscmd = 0;
  3625. del_timer_sync(&ndlp->nlp_delayfunc);
  3626. list_del_init(&ndlp->els_retry_evt.evt_listp);
  3627. list_del_init(&ndlp->dev_loss_evt.evt_listp);
  3628. lpfc_unreg_rpi(vport, ndlp);
  3629. return 0;
  3630. }
  3631. /*
  3632. * Check to see if we can free the nlp back to the freelist.
  3633. * If we are in the middle of using the nlp in the discovery state
  3634. * machine, defer the free till we reach the end of the state machine.
  3635. */
  3636. static void
  3637. lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  3638. {
  3639. struct lpfc_hba *phba = vport->phba;
  3640. struct lpfc_rport_data *rdata;
  3641. LPFC_MBOXQ_t *mbox;
  3642. int rc;
  3643. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  3644. if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
  3645. !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
  3646. !(ndlp->nlp_flag & NLP_RPI_VALID)) {
  3647. /* For this case we need to cleanup the default rpi
  3648. * allocated by the firmware.
  3649. */
  3650. if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
  3651. != NULL) {
  3652. rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
  3653. (uint8_t *) &vport->fc_sparam, mbox, 0);
  3654. if (rc) {
  3655. mempool_free(mbox, phba->mbox_mem_pool);
  3656. }
  3657. else {
  3658. mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
  3659. mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  3660. mbox->vport = vport;
  3661. mbox->context2 = NULL;
  3662. rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  3663. if (rc == MBX_NOT_FINISHED) {
  3664. mempool_free(mbox, phba->mbox_mem_pool);
  3665. }
  3666. }
  3667. }
  3668. }
  3669. lpfc_cleanup_node(vport, ndlp);
  3670. /*
  3671. * We can get here with a non-NULL ndlp->rport because when we
  3672. * unregister a rport we don't break the rport/node linkage. So if we
  3673. * do, make sure we don't leaving any dangling pointers behind.
  3674. */
  3675. if (ndlp->rport) {
  3676. rdata = ndlp->rport->dd_data;
  3677. rdata->pnode = NULL;
  3678. ndlp->rport = NULL;
  3679. }
  3680. }
  3681. static int
  3682. lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  3683. uint32_t did)
  3684. {
  3685. D_ID mydid, ndlpdid, matchdid;
  3686. if (did == Bcast_DID)
  3687. return 0;
  3688. /* First check for Direct match */
  3689. if (ndlp->nlp_DID == did)
  3690. return 1;
  3691. /* Next check for area/domain identically equals 0 match */
  3692. mydid.un.word = vport->fc_myDID;
  3693. if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
  3694. return 0;
  3695. }
  3696. matchdid.un.word = did;
  3697. ndlpdid.un.word = ndlp->nlp_DID;
  3698. if (matchdid.un.b.id == ndlpdid.un.b.id) {
  3699. if ((mydid.un.b.domain == matchdid.un.b.domain) &&
  3700. (mydid.un.b.area == matchdid.un.b.area)) {
  3701. if ((ndlpdid.un.b.domain == 0) &&
  3702. (ndlpdid.un.b.area == 0)) {
  3703. if (ndlpdid.un.b.id)
  3704. return 1;
  3705. }
  3706. return 0;
  3707. }
  3708. matchdid.un.word = ndlp->nlp_DID;
  3709. if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
  3710. (mydid.un.b.area == ndlpdid.un.b.area)) {
  3711. if ((matchdid.un.b.domain == 0) &&
  3712. (matchdid.un.b.area == 0)) {
  3713. if (matchdid.un.b.id)
  3714. return 1;
  3715. }
  3716. }
  3717. }
  3718. return 0;
  3719. }
  3720. /* Search for a nodelist entry */
  3721. static struct lpfc_nodelist *
  3722. __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
  3723. {
  3724. struct lpfc_nodelist *ndlp;
  3725. uint32_t data1;
  3726. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  3727. if (lpfc_matchdid(vport, ndlp, did)) {
  3728. data1 = (((uint32_t) ndlp->nlp_state << 24) |
  3729. ((uint32_t) ndlp->nlp_xri << 16) |
  3730. ((uint32_t) ndlp->nlp_type << 8) |
  3731. ((uint32_t) ndlp->nlp_rpi & 0xff));
  3732. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  3733. "0929 FIND node DID "
  3734. "Data: x%p x%x x%x x%x\n",
  3735. ndlp, ndlp->nlp_DID,
  3736. ndlp->nlp_flag, data1);
  3737. return ndlp;
  3738. }
  3739. }
  3740. /* FIND node did <did> NOT FOUND */
  3741. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  3742. "0932 FIND node did x%x NOT FOUND.\n", did);
  3743. return NULL;
  3744. }
  3745. struct lpfc_nodelist *
  3746. lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
  3747. {
  3748. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3749. struct lpfc_nodelist *ndlp;
  3750. spin_lock_irq(shost->host_lock);
  3751. ndlp = __lpfc_findnode_did(vport, did);
  3752. spin_unlock_irq(shost->host_lock);
  3753. return ndlp;
  3754. }
  3755. struct lpfc_nodelist *
  3756. lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
  3757. {
  3758. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3759. struct lpfc_nodelist *ndlp;
  3760. ndlp = lpfc_findnode_did(vport, did);
  3761. if (!ndlp) {
  3762. if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
  3763. lpfc_rscn_payload_check(vport, did) == 0)
  3764. return NULL;
  3765. ndlp = (struct lpfc_nodelist *)
  3766. mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
  3767. if (!ndlp)
  3768. return NULL;
  3769. lpfc_nlp_init(vport, ndlp, did);
  3770. lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
  3771. spin_lock_irq(shost->host_lock);
  3772. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  3773. spin_unlock_irq(shost->host_lock);
  3774. return ndlp;
  3775. } else if (!NLP_CHK_NODE_ACT(ndlp)) {
  3776. ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
  3777. if (!ndlp)
  3778. return NULL;
  3779. spin_lock_irq(shost->host_lock);
  3780. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  3781. spin_unlock_irq(shost->host_lock);
  3782. return ndlp;
  3783. }
  3784. if ((vport->fc_flag & FC_RSCN_MODE) &&
  3785. !(vport->fc_flag & FC_NDISC_ACTIVE)) {
  3786. if (lpfc_rscn_payload_check(vport, did)) {
  3787. /* If we've already recieved a PLOGI from this NPort
  3788. * we don't need to try to discover it again.
  3789. */
  3790. if (ndlp->nlp_flag & NLP_RCV_PLOGI)
  3791. return NULL;
  3792. /* Since this node is marked for discovery,
  3793. * delay timeout is not needed.
  3794. */
  3795. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  3796. spin_lock_irq(shost->host_lock);
  3797. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  3798. spin_unlock_irq(shost->host_lock);
  3799. } else
  3800. ndlp = NULL;
  3801. } else {
  3802. /* If we've already recieved a PLOGI from this NPort,
  3803. * or we are already in the process of discovery on it,
  3804. * we don't need to try to discover it again.
  3805. */
  3806. if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
  3807. ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
  3808. ndlp->nlp_flag & NLP_RCV_PLOGI)
  3809. return NULL;
  3810. lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
  3811. spin_lock_irq(shost->host_lock);
  3812. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  3813. spin_unlock_irq(shost->host_lock);
  3814. }
  3815. return ndlp;
  3816. }
  3817. /* Build a list of nodes to discover based on the loopmap */
  3818. void
  3819. lpfc_disc_list_loopmap(struct lpfc_vport *vport)
  3820. {
  3821. struct lpfc_hba *phba = vport->phba;
  3822. int j;
  3823. uint32_t alpa, index;
  3824. if (!lpfc_is_link_up(phba))
  3825. return;
  3826. if (phba->fc_topology != TOPOLOGY_LOOP)
  3827. return;
  3828. /* Check for loop map present or not */
  3829. if (phba->alpa_map[0]) {
  3830. for (j = 1; j <= phba->alpa_map[0]; j++) {
  3831. alpa = phba->alpa_map[j];
  3832. if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
  3833. continue;
  3834. lpfc_setup_disc_node(vport, alpa);
  3835. }
  3836. } else {
  3837. /* No alpamap, so try all alpa's */
  3838. for (j = 0; j < FC_MAXLOOP; j++) {
  3839. /* If cfg_scan_down is set, start from highest
  3840. * ALPA (0xef) to lowest (0x1).
  3841. */
  3842. if (vport->cfg_scan_down)
  3843. index = j;
  3844. else
  3845. index = FC_MAXLOOP - j - 1;
  3846. alpa = lpfcAlpaArray[index];
  3847. if ((vport->fc_myDID & 0xff) == alpa)
  3848. continue;
  3849. lpfc_setup_disc_node(vport, alpa);
  3850. }
  3851. }
  3852. return;
  3853. }
  3854. void
  3855. lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
  3856. {
  3857. LPFC_MBOXQ_t *mbox;
  3858. struct lpfc_sli *psli = &phba->sli;
  3859. struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
  3860. struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
  3861. struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
  3862. int rc;
  3863. /*
  3864. * if it's not a physical port or if we already send
  3865. * clear_la then don't send it.
  3866. */
  3867. if ((phba->link_state >= LPFC_CLEAR_LA) ||
  3868. (vport->port_type != LPFC_PHYSICAL_PORT) ||
  3869. (phba->sli_rev == LPFC_SLI_REV4))
  3870. return;
  3871. /* Link up discovery */
  3872. if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
  3873. phba->link_state = LPFC_CLEAR_LA;
  3874. lpfc_clear_la(phba, mbox);
  3875. mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
  3876. mbox->vport = vport;
  3877. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  3878. if (rc == MBX_NOT_FINISHED) {
  3879. mempool_free(mbox, phba->mbox_mem_pool);
  3880. lpfc_disc_flush_list(vport);
  3881. extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
  3882. fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
  3883. next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
  3884. phba->link_state = LPFC_HBA_ERROR;
  3885. }
  3886. }
  3887. }
  3888. /* Reg_vpi to tell firmware to resume normal operations */
  3889. void
  3890. lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
  3891. {
  3892. LPFC_MBOXQ_t *regvpimbox;
  3893. regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3894. if (regvpimbox) {
  3895. lpfc_reg_vpi(vport, regvpimbox);
  3896. regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
  3897. regvpimbox->vport = vport;
  3898. if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
  3899. == MBX_NOT_FINISHED) {
  3900. mempool_free(regvpimbox, phba->mbox_mem_pool);
  3901. }
  3902. }
  3903. }
  3904. /* Start Link up / RSCN discovery on NPR nodes */
  3905. void
  3906. lpfc_disc_start(struct lpfc_vport *vport)
  3907. {
  3908. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3909. struct lpfc_hba *phba = vport->phba;
  3910. uint32_t num_sent;
  3911. uint32_t clear_la_pending;
  3912. int did_changed;
  3913. if (!lpfc_is_link_up(phba))
  3914. return;
  3915. if (phba->link_state == LPFC_CLEAR_LA)
  3916. clear_la_pending = 1;
  3917. else
  3918. clear_la_pending = 0;
  3919. if (vport->port_state < LPFC_VPORT_READY)
  3920. vport->port_state = LPFC_DISC_AUTH;
  3921. lpfc_set_disctmo(vport);
  3922. if (vport->fc_prevDID == vport->fc_myDID)
  3923. did_changed = 0;
  3924. else
  3925. did_changed = 1;
  3926. vport->fc_prevDID = vport->fc_myDID;
  3927. vport->num_disc_nodes = 0;
  3928. /* Start Discovery state <hba_state> */
  3929. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  3930. "0202 Start Discovery hba state x%x "
  3931. "Data: x%x x%x x%x\n",
  3932. vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
  3933. vport->fc_adisc_cnt);
  3934. /* First do ADISCs - if any */
  3935. num_sent = lpfc_els_disc_adisc(vport);
  3936. if (num_sent)
  3937. return;
  3938. /*
  3939. * For SLI3, cmpl_reg_vpi will set port_state to READY, and
  3940. * continue discovery.
  3941. */
  3942. if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
  3943. !(vport->fc_flag & FC_PT2PT) &&
  3944. !(vport->fc_flag & FC_RSCN_MODE) &&
  3945. (phba->sli_rev < LPFC_SLI_REV4)) {
  3946. lpfc_issue_reg_vpi(phba, vport);
  3947. return;
  3948. }
  3949. /*
  3950. * For SLI2, we need to set port_state to READY and continue
  3951. * discovery.
  3952. */
  3953. if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
  3954. /* If we get here, there is nothing to ADISC */
  3955. if (vport->port_type == LPFC_PHYSICAL_PORT)
  3956. lpfc_issue_clear_la(phba, vport);
  3957. if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
  3958. vport->num_disc_nodes = 0;
  3959. /* go thru NPR nodes and issue ELS PLOGIs */
  3960. if (vport->fc_npr_cnt)
  3961. lpfc_els_disc_plogi(vport);
  3962. if (!vport->num_disc_nodes) {
  3963. spin_lock_irq(shost->host_lock);
  3964. vport->fc_flag &= ~FC_NDISC_ACTIVE;
  3965. spin_unlock_irq(shost->host_lock);
  3966. lpfc_can_disctmo(vport);
  3967. }
  3968. }
  3969. vport->port_state = LPFC_VPORT_READY;
  3970. } else {
  3971. /* Next do PLOGIs - if any */
  3972. num_sent = lpfc_els_disc_plogi(vport);
  3973. if (num_sent)
  3974. return;
  3975. if (vport->fc_flag & FC_RSCN_MODE) {
  3976. /* Check to see if more RSCNs came in while we
  3977. * were processing this one.
  3978. */
  3979. if ((vport->fc_rscn_id_cnt == 0) &&
  3980. (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
  3981. spin_lock_irq(shost->host_lock);
  3982. vport->fc_flag &= ~FC_RSCN_MODE;
  3983. spin_unlock_irq(shost->host_lock);
  3984. lpfc_can_disctmo(vport);
  3985. } else
  3986. lpfc_els_handle_rscn(vport);
  3987. }
  3988. }
  3989. return;
  3990. }
  3991. /*
  3992. * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
  3993. * ring the match the sppecified nodelist.
  3994. */
  3995. static void
  3996. lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  3997. {
  3998. LIST_HEAD(completions);
  3999. struct lpfc_sli *psli;
  4000. IOCB_t *icmd;
  4001. struct lpfc_iocbq *iocb, *next_iocb;
  4002. struct lpfc_sli_ring *pring;
  4003. psli = &phba->sli;
  4004. pring = &psli->ring[LPFC_ELS_RING];
  4005. /* Error matching iocb on txq or txcmplq
  4006. * First check the txq.
  4007. */
  4008. spin_lock_irq(&phba->hbalock);
  4009. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  4010. if (iocb->context1 != ndlp) {
  4011. continue;
  4012. }
  4013. icmd = &iocb->iocb;
  4014. if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
  4015. (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
  4016. list_move_tail(&iocb->list, &completions);
  4017. pring->txq_cnt--;
  4018. }
  4019. }
  4020. /* Next check the txcmplq */
  4021. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
  4022. if (iocb->context1 != ndlp) {
  4023. continue;
  4024. }
  4025. icmd = &iocb->iocb;
  4026. if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
  4027. icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
  4028. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  4029. }
  4030. }
  4031. spin_unlock_irq(&phba->hbalock);
  4032. /* Cancel all the IOCBs from the completions list */
  4033. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  4034. IOERR_SLI_ABORTED);
  4035. }
  4036. static void
  4037. lpfc_disc_flush_list(struct lpfc_vport *vport)
  4038. {
  4039. struct lpfc_nodelist *ndlp, *next_ndlp;
  4040. struct lpfc_hba *phba = vport->phba;
  4041. if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
  4042. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
  4043. nlp_listp) {
  4044. if (!NLP_CHK_NODE_ACT(ndlp))
  4045. continue;
  4046. if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
  4047. ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
  4048. lpfc_free_tx(phba, ndlp);
  4049. }
  4050. }
  4051. }
  4052. }
  4053. void
  4054. lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
  4055. {
  4056. lpfc_els_flush_rscn(vport);
  4057. lpfc_els_flush_cmd(vport);
  4058. lpfc_disc_flush_list(vport);
  4059. }
  4060. /*****************************************************************************/
  4061. /*
  4062. * NAME: lpfc_disc_timeout
  4063. *
  4064. * FUNCTION: Fibre Channel driver discovery timeout routine.
  4065. *
  4066. * EXECUTION ENVIRONMENT: interrupt only
  4067. *
  4068. * CALLED FROM:
  4069. * Timer function
  4070. *
  4071. * RETURNS:
  4072. * none
  4073. */
  4074. /*****************************************************************************/
  4075. void
  4076. lpfc_disc_timeout(unsigned long ptr)
  4077. {
  4078. struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
  4079. struct lpfc_hba *phba = vport->phba;
  4080. uint32_t tmo_posted;
  4081. unsigned long flags = 0;
  4082. if (unlikely(!phba))
  4083. return;
  4084. spin_lock_irqsave(&vport->work_port_lock, flags);
  4085. tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
  4086. if (!tmo_posted)
  4087. vport->work_port_events |= WORKER_DISC_TMO;
  4088. spin_unlock_irqrestore(&vport->work_port_lock, flags);
  4089. if (!tmo_posted)
  4090. lpfc_worker_wake_up(phba);
  4091. return;
  4092. }
  4093. static void
  4094. lpfc_disc_timeout_handler(struct lpfc_vport *vport)
  4095. {
  4096. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  4097. struct lpfc_hba *phba = vport->phba;
  4098. struct lpfc_sli *psli = &phba->sli;
  4099. struct lpfc_nodelist *ndlp, *next_ndlp;
  4100. LPFC_MBOXQ_t *initlinkmbox;
  4101. int rc, clrlaerr = 0;
  4102. if (!(vport->fc_flag & FC_DISC_TMO))
  4103. return;
  4104. spin_lock_irq(shost->host_lock);
  4105. vport->fc_flag &= ~FC_DISC_TMO;
  4106. spin_unlock_irq(shost->host_lock);
  4107. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  4108. "disc timeout: state:x%x rtry:x%x flg:x%x",
  4109. vport->port_state, vport->fc_ns_retry, vport->fc_flag);
  4110. switch (vport->port_state) {
  4111. case LPFC_LOCAL_CFG_LINK:
  4112. /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
  4113. * FAN
  4114. */
  4115. /* FAN timeout */
  4116. lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
  4117. "0221 FAN timeout\n");
  4118. /* Start discovery by sending FLOGI, clean up old rpis */
  4119. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
  4120. nlp_listp) {
  4121. if (!NLP_CHK_NODE_ACT(ndlp))
  4122. continue;
  4123. if (ndlp->nlp_state != NLP_STE_NPR_NODE)
  4124. continue;
  4125. if (ndlp->nlp_type & NLP_FABRIC) {
  4126. /* Clean up the ndlp on Fabric connections */
  4127. lpfc_drop_node(vport, ndlp);
  4128. } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
  4129. /* Fail outstanding IO now since device
  4130. * is marked for PLOGI.
  4131. */
  4132. lpfc_unreg_rpi(vport, ndlp);
  4133. }
  4134. }
  4135. if (vport->port_state != LPFC_FLOGI) {
  4136. lpfc_initial_flogi(vport);
  4137. return;
  4138. }
  4139. break;
  4140. case LPFC_FDISC:
  4141. case LPFC_FLOGI:
  4142. /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
  4143. /* Initial FLOGI timeout */
  4144. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4145. "0222 Initial %s timeout\n",
  4146. vport->vpi ? "FDISC" : "FLOGI");
  4147. /* Assume no Fabric and go on with discovery.
  4148. * Check for outstanding ELS FLOGI to abort.
  4149. */
  4150. /* FLOGI failed, so just use loop map to make discovery list */
  4151. lpfc_disc_list_loopmap(vport);
  4152. /* Start discovery */
  4153. lpfc_disc_start(vport);
  4154. break;
  4155. case LPFC_FABRIC_CFG_LINK:
  4156. /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
  4157. NameServer login */
  4158. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4159. "0223 Timeout while waiting for "
  4160. "NameServer login\n");
  4161. /* Next look for NameServer ndlp */
  4162. ndlp = lpfc_findnode_did(vport, NameServer_DID);
  4163. if (ndlp && NLP_CHK_NODE_ACT(ndlp))
  4164. lpfc_els_abort(phba, ndlp);
  4165. /* ReStart discovery */
  4166. goto restart_disc;
  4167. case LPFC_NS_QRY:
  4168. /* Check for wait for NameServer Rsp timeout */
  4169. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4170. "0224 NameServer Query timeout "
  4171. "Data: x%x x%x\n",
  4172. vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
  4173. if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
  4174. /* Try it one more time */
  4175. vport->fc_ns_retry++;
  4176. rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
  4177. vport->fc_ns_retry, 0);
  4178. if (rc == 0)
  4179. break;
  4180. }
  4181. vport->fc_ns_retry = 0;
  4182. restart_disc:
  4183. /*
  4184. * Discovery is over.
  4185. * set port_state to PORT_READY if SLI2.
  4186. * cmpl_reg_vpi will set port_state to READY for SLI3.
  4187. */
  4188. if (phba->sli_rev < LPFC_SLI_REV4) {
  4189. if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
  4190. lpfc_issue_reg_vpi(phba, vport);
  4191. else { /* NPIV Not enabled */
  4192. lpfc_issue_clear_la(phba, vport);
  4193. vport->port_state = LPFC_VPORT_READY;
  4194. }
  4195. }
  4196. /* Setup and issue mailbox INITIALIZE LINK command */
  4197. initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4198. if (!initlinkmbox) {
  4199. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4200. "0206 Device Discovery "
  4201. "completion error\n");
  4202. phba->link_state = LPFC_HBA_ERROR;
  4203. break;
  4204. }
  4205. lpfc_linkdown(phba);
  4206. lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
  4207. phba->cfg_link_speed);
  4208. initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
  4209. initlinkmbox->vport = vport;
  4210. initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  4211. rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
  4212. lpfc_set_loopback_flag(phba);
  4213. if (rc == MBX_NOT_FINISHED)
  4214. mempool_free(initlinkmbox, phba->mbox_mem_pool);
  4215. break;
  4216. case LPFC_DISC_AUTH:
  4217. /* Node Authentication timeout */
  4218. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4219. "0227 Node Authentication timeout\n");
  4220. lpfc_disc_flush_list(vport);
  4221. /*
  4222. * set port_state to PORT_READY if SLI2.
  4223. * cmpl_reg_vpi will set port_state to READY for SLI3.
  4224. */
  4225. if (phba->sli_rev < LPFC_SLI_REV4) {
  4226. if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
  4227. lpfc_issue_reg_vpi(phba, vport);
  4228. else { /* NPIV Not enabled */
  4229. lpfc_issue_clear_la(phba, vport);
  4230. vport->port_state = LPFC_VPORT_READY;
  4231. }
  4232. }
  4233. break;
  4234. case LPFC_VPORT_READY:
  4235. if (vport->fc_flag & FC_RSCN_MODE) {
  4236. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4237. "0231 RSCN timeout Data: x%x "
  4238. "x%x\n",
  4239. vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
  4240. /* Cleanup any outstanding ELS commands */
  4241. lpfc_els_flush_cmd(vport);
  4242. lpfc_els_flush_rscn(vport);
  4243. lpfc_disc_flush_list(vport);
  4244. }
  4245. break;
  4246. default:
  4247. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4248. "0273 Unexpected discovery timeout, "
  4249. "vport State x%x\n", vport->port_state);
  4250. break;
  4251. }
  4252. switch (phba->link_state) {
  4253. case LPFC_CLEAR_LA:
  4254. /* CLEAR LA timeout */
  4255. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4256. "0228 CLEAR LA timeout\n");
  4257. clrlaerr = 1;
  4258. break;
  4259. case LPFC_LINK_UP:
  4260. lpfc_issue_clear_la(phba, vport);
  4261. /* Drop thru */
  4262. case LPFC_LINK_UNKNOWN:
  4263. case LPFC_WARM_START:
  4264. case LPFC_INIT_START:
  4265. case LPFC_INIT_MBX_CMDS:
  4266. case LPFC_LINK_DOWN:
  4267. case LPFC_HBA_ERROR:
  4268. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  4269. "0230 Unexpected timeout, hba link "
  4270. "state x%x\n", phba->link_state);
  4271. clrlaerr = 1;
  4272. break;
  4273. case LPFC_HBA_READY:
  4274. break;
  4275. }
  4276. if (clrlaerr) {
  4277. lpfc_disc_flush_list(vport);
  4278. psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
  4279. psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
  4280. psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
  4281. vport->port_state = LPFC_VPORT_READY;
  4282. }
  4283. return;
  4284. }
  4285. /*
  4286. * This routine handles processing a NameServer REG_LOGIN mailbox
  4287. * command upon completion. It is setup in the LPFC_MBOXQ
  4288. * as the completion routine when the command is
  4289. * handed off to the SLI layer.
  4290. */
  4291. void
  4292. lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  4293. {
  4294. MAILBOX_t *mb = &pmb->u.mb;
  4295. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  4296. struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
  4297. struct lpfc_vport *vport = pmb->vport;
  4298. pmb->context1 = NULL;
  4299. ndlp->nlp_rpi = mb->un.varWords[0];
  4300. ndlp->nlp_flag |= NLP_RPI_VALID;
  4301. ndlp->nlp_type |= NLP_FABRIC;
  4302. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  4303. /*
  4304. * Start issuing Fabric-Device Management Interface (FDMI) command to
  4305. * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
  4306. * fdmi-on=2 (supporting RPA/hostnmae)
  4307. */
  4308. if (vport->cfg_fdmi_on == 1)
  4309. lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
  4310. else
  4311. mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
  4312. /* decrement the node reference count held for this callback
  4313. * function.
  4314. */
  4315. lpfc_nlp_put(ndlp);
  4316. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4317. kfree(mp);
  4318. mempool_free(pmb, phba->mbox_mem_pool);
  4319. return;
  4320. }
  4321. static int
  4322. lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
  4323. {
  4324. uint16_t *rpi = param;
  4325. return ndlp->nlp_rpi == *rpi;
  4326. }
  4327. static int
  4328. lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
  4329. {
  4330. return memcmp(&ndlp->nlp_portname, param,
  4331. sizeof(ndlp->nlp_portname)) == 0;
  4332. }
  4333. static struct lpfc_nodelist *
  4334. __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
  4335. {
  4336. struct lpfc_nodelist *ndlp;
  4337. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  4338. if (filter(ndlp, param))
  4339. return ndlp;
  4340. }
  4341. return NULL;
  4342. }
  4343. /*
  4344. * This routine looks up the ndlp lists for the given RPI. If rpi found it
  4345. * returns the node list element pointer else return NULL.
  4346. */
  4347. struct lpfc_nodelist *
  4348. __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
  4349. {
  4350. return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
  4351. }
  4352. /*
  4353. * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
  4354. * returns the node element list pointer else return NULL.
  4355. */
  4356. struct lpfc_nodelist *
  4357. lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
  4358. {
  4359. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  4360. struct lpfc_nodelist *ndlp;
  4361. spin_lock_irq(shost->host_lock);
  4362. ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
  4363. spin_unlock_irq(shost->host_lock);
  4364. return ndlp;
  4365. }
  4366. void
  4367. lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  4368. uint32_t did)
  4369. {
  4370. memset(ndlp, 0, sizeof (struct lpfc_nodelist));
  4371. lpfc_initialize_node(vport, ndlp, did);
  4372. INIT_LIST_HEAD(&ndlp->nlp_listp);
  4373. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
  4374. "node init: did:x%x",
  4375. ndlp->nlp_DID, 0, 0);
  4376. return;
  4377. }
  4378. /* This routine releases all resources associated with a specifc NPort's ndlp
  4379. * and mempool_free's the nodelist.
  4380. */
  4381. static void
  4382. lpfc_nlp_release(struct kref *kref)
  4383. {
  4384. struct lpfc_hba *phba;
  4385. unsigned long flags;
  4386. struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
  4387. kref);
  4388. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  4389. "node release: did:x%x flg:x%x type:x%x",
  4390. ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
  4391. lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
  4392. "0279 lpfc_nlp_release: ndlp:x%p "
  4393. "usgmap:x%x refcnt:%d\n",
  4394. (void *)ndlp, ndlp->nlp_usg_map,
  4395. atomic_read(&ndlp->kref.refcount));
  4396. /* remove ndlp from action. */
  4397. lpfc_nlp_remove(ndlp->vport, ndlp);
  4398. /* clear the ndlp active flag for all release cases */
  4399. phba = ndlp->phba;
  4400. spin_lock_irqsave(&phba->ndlp_lock, flags);
  4401. NLP_CLR_NODE_ACT(ndlp);
  4402. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4403. /* free ndlp memory for final ndlp release */
  4404. if (NLP_CHK_FREE_REQ(ndlp)) {
  4405. kfree(ndlp->lat_data);
  4406. mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
  4407. }
  4408. }
  4409. /* This routine bumps the reference count for a ndlp structure to ensure
  4410. * that one discovery thread won't free a ndlp while another discovery thread
  4411. * is using it.
  4412. */
  4413. struct lpfc_nodelist *
  4414. lpfc_nlp_get(struct lpfc_nodelist *ndlp)
  4415. {
  4416. struct lpfc_hba *phba;
  4417. unsigned long flags;
  4418. if (ndlp) {
  4419. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  4420. "node get: did:x%x flg:x%x refcnt:x%x",
  4421. ndlp->nlp_DID, ndlp->nlp_flag,
  4422. atomic_read(&ndlp->kref.refcount));
  4423. /* The check of ndlp usage to prevent incrementing the
  4424. * ndlp reference count that is in the process of being
  4425. * released.
  4426. */
  4427. phba = ndlp->phba;
  4428. spin_lock_irqsave(&phba->ndlp_lock, flags);
  4429. if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
  4430. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4431. lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
  4432. "0276 lpfc_nlp_get: ndlp:x%p "
  4433. "usgmap:x%x refcnt:%d\n",
  4434. (void *)ndlp, ndlp->nlp_usg_map,
  4435. atomic_read(&ndlp->kref.refcount));
  4436. return NULL;
  4437. } else
  4438. kref_get(&ndlp->kref);
  4439. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4440. }
  4441. return ndlp;
  4442. }
  4443. /* This routine decrements the reference count for a ndlp structure. If the
  4444. * count goes to 0, this indicates the the associated nodelist should be
  4445. * freed. Returning 1 indicates the ndlp resource has been released; on the
  4446. * other hand, returning 0 indicates the ndlp resource has not been released
  4447. * yet.
  4448. */
  4449. int
  4450. lpfc_nlp_put(struct lpfc_nodelist *ndlp)
  4451. {
  4452. struct lpfc_hba *phba;
  4453. unsigned long flags;
  4454. if (!ndlp)
  4455. return 1;
  4456. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  4457. "node put: did:x%x flg:x%x refcnt:x%x",
  4458. ndlp->nlp_DID, ndlp->nlp_flag,
  4459. atomic_read(&ndlp->kref.refcount));
  4460. phba = ndlp->phba;
  4461. spin_lock_irqsave(&phba->ndlp_lock, flags);
  4462. /* Check the ndlp memory free acknowledge flag to avoid the
  4463. * possible race condition that kref_put got invoked again
  4464. * after previous one has done ndlp memory free.
  4465. */
  4466. if (NLP_CHK_FREE_ACK(ndlp)) {
  4467. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4468. lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
  4469. "0274 lpfc_nlp_put: ndlp:x%p "
  4470. "usgmap:x%x refcnt:%d\n",
  4471. (void *)ndlp, ndlp->nlp_usg_map,
  4472. atomic_read(&ndlp->kref.refcount));
  4473. return 1;
  4474. }
  4475. /* Check the ndlp inactivate log flag to avoid the possible
  4476. * race condition that kref_put got invoked again after ndlp
  4477. * is already in inactivating state.
  4478. */
  4479. if (NLP_CHK_IACT_REQ(ndlp)) {
  4480. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4481. lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
  4482. "0275 lpfc_nlp_put: ndlp:x%p "
  4483. "usgmap:x%x refcnt:%d\n",
  4484. (void *)ndlp, ndlp->nlp_usg_map,
  4485. atomic_read(&ndlp->kref.refcount));
  4486. return 1;
  4487. }
  4488. /* For last put, mark the ndlp usage flags to make sure no
  4489. * other kref_get and kref_put on the same ndlp shall get
  4490. * in between the process when the final kref_put has been
  4491. * invoked on this ndlp.
  4492. */
  4493. if (atomic_read(&ndlp->kref.refcount) == 1) {
  4494. /* Indicate ndlp is put to inactive state. */
  4495. NLP_SET_IACT_REQ(ndlp);
  4496. /* Acknowledge ndlp memory free has been seen. */
  4497. if (NLP_CHK_FREE_REQ(ndlp))
  4498. NLP_SET_FREE_ACK(ndlp);
  4499. }
  4500. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  4501. /* Note, the kref_put returns 1 when decrementing a reference
  4502. * count that was 1, it invokes the release callback function,
  4503. * but it still left the reference count as 1 (not actually
  4504. * performs the last decrementation). Otherwise, it actually
  4505. * decrements the reference count and returns 0.
  4506. */
  4507. return kref_put(&ndlp->kref, lpfc_nlp_release);
  4508. }
  4509. /* This routine free's the specified nodelist if it is not in use
  4510. * by any other discovery thread. This routine returns 1 if the
  4511. * ndlp has been freed. A return value of 0 indicates the ndlp is
  4512. * not yet been released.
  4513. */
  4514. int
  4515. lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
  4516. {
  4517. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  4518. "node not used: did:x%x flg:x%x refcnt:x%x",
  4519. ndlp->nlp_DID, ndlp->nlp_flag,
  4520. atomic_read(&ndlp->kref.refcount));
  4521. if (atomic_read(&ndlp->kref.refcount) == 1)
  4522. if (lpfc_nlp_put(ndlp))
  4523. return 1;
  4524. return 0;
  4525. }
  4526. /**
  4527. * lpfc_fcf_inuse - Check if FCF can be unregistered.
  4528. * @phba: Pointer to hba context object.
  4529. *
  4530. * This function iterate through all FC nodes associated
  4531. * will all vports to check if there is any node with
  4532. * fc_rports associated with it. If there is an fc_rport
  4533. * associated with the node, then the node is either in
  4534. * discovered state or its devloss_timer is pending.
  4535. */
  4536. static int
  4537. lpfc_fcf_inuse(struct lpfc_hba *phba)
  4538. {
  4539. struct lpfc_vport **vports;
  4540. int i, ret = 0;
  4541. struct lpfc_nodelist *ndlp;
  4542. struct Scsi_Host *shost;
  4543. vports = lpfc_create_vport_work_array(phba);
  4544. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  4545. shost = lpfc_shost_from_vport(vports[i]);
  4546. spin_lock_irq(shost->host_lock);
  4547. list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
  4548. if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
  4549. (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
  4550. ret = 1;
  4551. spin_unlock_irq(shost->host_lock);
  4552. goto out;
  4553. } else {
  4554. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  4555. "2624 RPI %x DID %x flg %x still "
  4556. "logged in\n",
  4557. ndlp->nlp_rpi, ndlp->nlp_DID,
  4558. ndlp->nlp_flag);
  4559. if (ndlp->nlp_flag & NLP_RPI_VALID)
  4560. ret = 1;
  4561. }
  4562. }
  4563. spin_unlock_irq(shost->host_lock);
  4564. }
  4565. out:
  4566. lpfc_destroy_vport_work_array(phba, vports);
  4567. return ret;
  4568. }
  4569. /**
  4570. * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
  4571. * @phba: Pointer to hba context object.
  4572. * @mboxq: Pointer to mailbox object.
  4573. *
  4574. * This function frees memory associated with the mailbox command.
  4575. */
  4576. static void
  4577. lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  4578. {
  4579. struct lpfc_vport *vport = mboxq->vport;
  4580. if (mboxq->u.mb.mbxStatus) {
  4581. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  4582. "2555 UNREG_VFI mbxStatus error x%x "
  4583. "HBA state x%x\n",
  4584. mboxq->u.mb.mbxStatus, vport->port_state);
  4585. }
  4586. mempool_free(mboxq, phba->mbox_mem_pool);
  4587. return;
  4588. }
  4589. /**
  4590. * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
  4591. * @phba: Pointer to hba context object.
  4592. * @mboxq: Pointer to mailbox object.
  4593. *
  4594. * This function frees memory associated with the mailbox command.
  4595. */
  4596. static void
  4597. lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  4598. {
  4599. struct lpfc_vport *vport = mboxq->vport;
  4600. if (mboxq->u.mb.mbxStatus) {
  4601. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  4602. "2550 UNREG_FCFI mbxStatus error x%x "
  4603. "HBA state x%x\n",
  4604. mboxq->u.mb.mbxStatus, vport->port_state);
  4605. }
  4606. mempool_free(mboxq, phba->mbox_mem_pool);
  4607. return;
  4608. }
  4609. /**
  4610. * lpfc_unregister_fcf_prep - Unregister fcf record preparation
  4611. * @phba: Pointer to hba context object.
  4612. *
  4613. * This function prepare the HBA for unregistering the currently registered
  4614. * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
  4615. * VFIs.
  4616. */
  4617. int
  4618. lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
  4619. {
  4620. LPFC_MBOXQ_t *mbox;
  4621. struct lpfc_vport **vports;
  4622. struct lpfc_nodelist *ndlp;
  4623. struct Scsi_Host *shost;
  4624. int i, rc;
  4625. /* Unregister RPIs */
  4626. if (lpfc_fcf_inuse(phba))
  4627. lpfc_unreg_hba_rpis(phba);
  4628. /* At this point, all discovery is aborted */
  4629. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  4630. /* Unregister VPIs */
  4631. vports = lpfc_create_vport_work_array(phba);
  4632. if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
  4633. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  4634. /* Stop FLOGI/FDISC retries */
  4635. ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
  4636. if (ndlp)
  4637. lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
  4638. lpfc_cleanup_pending_mbox(vports[i]);
  4639. lpfc_mbx_unreg_vpi(vports[i]);
  4640. shost = lpfc_shost_from_vport(vports[i]);
  4641. spin_lock_irq(shost->host_lock);
  4642. vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
  4643. vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
  4644. spin_unlock_irq(shost->host_lock);
  4645. }
  4646. lpfc_destroy_vport_work_array(phba, vports);
  4647. /* Cleanup any outstanding ELS commands */
  4648. lpfc_els_flush_all_cmd(phba);
  4649. /* Unregister VFI */
  4650. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4651. if (!mbox) {
  4652. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  4653. "2556 UNREG_VFI mbox allocation failed"
  4654. "HBA state x%x\n", phba->pport->port_state);
  4655. return -ENOMEM;
  4656. }
  4657. lpfc_unreg_vfi(mbox, phba->pport);
  4658. mbox->vport = phba->pport;
  4659. mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
  4660. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  4661. if (rc == MBX_NOT_FINISHED) {
  4662. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  4663. "2557 UNREG_VFI issue mbox failed rc x%x "
  4664. "HBA state x%x\n",
  4665. rc, phba->pport->port_state);
  4666. mempool_free(mbox, phba->mbox_mem_pool);
  4667. return -EIO;
  4668. }
  4669. shost = lpfc_shost_from_vport(phba->pport);
  4670. spin_lock_irq(shost->host_lock);
  4671. phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
  4672. spin_unlock_irq(shost->host_lock);
  4673. return 0;
  4674. }
  4675. /**
  4676. * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
  4677. * @phba: Pointer to hba context object.
  4678. *
  4679. * This function issues synchronous unregister FCF mailbox command to HBA to
  4680. * unregister the currently registered FCF record. The driver does not reset
  4681. * the driver FCF usage state flags.
  4682. *
  4683. * Return 0 if successfully issued, none-zero otherwise.
  4684. */
  4685. int
  4686. lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
  4687. {
  4688. LPFC_MBOXQ_t *mbox;
  4689. int rc;
  4690. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4691. if (!mbox) {
  4692. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  4693. "2551 UNREG_FCFI mbox allocation failed"
  4694. "HBA state x%x\n", phba->pport->port_state);
  4695. return -ENOMEM;
  4696. }
  4697. lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
  4698. mbox->vport = phba->pport;
  4699. mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
  4700. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  4701. if (rc == MBX_NOT_FINISHED) {
  4702. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4703. "2552 Unregister FCFI command failed rc x%x "
  4704. "HBA state x%x\n",
  4705. rc, phba->pport->port_state);
  4706. return -EINVAL;
  4707. }
  4708. return 0;
  4709. }
  4710. /**
  4711. * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
  4712. * @phba: Pointer to hba context object.
  4713. *
  4714. * This function unregisters the currently reigstered FCF. This function
  4715. * also tries to find another FCF for discovery by rescan the HBA FCF table.
  4716. */
  4717. void
  4718. lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
  4719. {
  4720. int rc;
  4721. /* Preparation for unregistering fcf */
  4722. rc = lpfc_unregister_fcf_prep(phba);
  4723. if (rc) {
  4724. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
  4725. "2748 Failed to prepare for unregistering "
  4726. "HBA's FCF record: rc=%d\n", rc);
  4727. return;
  4728. }
  4729. /* Now, unregister FCF record and reset HBA FCF state */
  4730. rc = lpfc_sli4_unregister_fcf(phba);
  4731. if (rc)
  4732. return;
  4733. /* Reset HBA FCF states after successful unregister FCF */
  4734. phba->fcf.fcf_flag = 0;
  4735. phba->fcf.current_rec.flag = 0;
  4736. /*
  4737. * If driver is not unloading, check if there is any other
  4738. * FCF record that can be used for discovery.
  4739. */
  4740. if ((phba->pport->load_flag & FC_UNLOADING) ||
  4741. (phba->link_state < LPFC_LINK_UP))
  4742. return;
  4743. /* This is considered as the initial FCF discovery scan */
  4744. spin_lock_irq(&phba->hbalock);
  4745. phba->fcf.fcf_flag |= FCF_INIT_DISC;
  4746. spin_unlock_irq(&phba->hbalock);
  4747. rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
  4748. if (rc) {
  4749. spin_lock_irq(&phba->hbalock);
  4750. phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
  4751. spin_unlock_irq(&phba->hbalock);
  4752. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  4753. "2553 lpfc_unregister_unused_fcf failed "
  4754. "to read FCF record HBA state x%x\n",
  4755. phba->pport->port_state);
  4756. }
  4757. }
  4758. /**
  4759. * lpfc_unregister_fcf - Unregister the currently registered fcf record
  4760. * @phba: Pointer to hba context object.
  4761. *
  4762. * This function just unregisters the currently reigstered FCF. It does not
  4763. * try to find another FCF for discovery.
  4764. */
  4765. void
  4766. lpfc_unregister_fcf(struct lpfc_hba *phba)
  4767. {
  4768. int rc;
  4769. /* Preparation for unregistering fcf */
  4770. rc = lpfc_unregister_fcf_prep(phba);
  4771. if (rc) {
  4772. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
  4773. "2749 Failed to prepare for unregistering "
  4774. "HBA's FCF record: rc=%d\n", rc);
  4775. return;
  4776. }
  4777. /* Now, unregister FCF record and reset HBA FCF state */
  4778. rc = lpfc_sli4_unregister_fcf(phba);
  4779. if (rc)
  4780. return;
  4781. /* Set proper HBA FCF states after successful unregister FCF */
  4782. spin_lock_irq(&phba->hbalock);
  4783. phba->fcf.fcf_flag &= ~FCF_REGISTERED;
  4784. spin_unlock_irq(&phba->hbalock);
  4785. }
  4786. /**
  4787. * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
  4788. * @phba: Pointer to hba context object.
  4789. *
  4790. * This function check if there are any connected remote port for the FCF and
  4791. * if all the devices are disconnected, this function unregister FCFI.
  4792. * This function also tries to use another FCF for discovery.
  4793. */
  4794. void
  4795. lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
  4796. {
  4797. /*
  4798. * If HBA is not running in FIP mode, if HBA does not support
  4799. * FCoE, if FCF discovery is ongoing, or if FCF has not been
  4800. * registered, do nothing.
  4801. */
  4802. spin_lock_irq(&phba->hbalock);
  4803. if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
  4804. !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
  4805. !(phba->hba_flag & HBA_FIP_SUPPORT) ||
  4806. (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
  4807. (phba->pport->port_state == LPFC_FLOGI)) {
  4808. spin_unlock_irq(&phba->hbalock);
  4809. return;
  4810. }
  4811. spin_unlock_irq(&phba->hbalock);
  4812. if (lpfc_fcf_inuse(phba))
  4813. return;
  4814. lpfc_unregister_fcf_rescan(phba);
  4815. }
  4816. /**
  4817. * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
  4818. * @phba: Pointer to hba context object.
  4819. * @buff: Buffer containing the FCF connection table as in the config
  4820. * region.
  4821. * This function create driver data structure for the FCF connection
  4822. * record table read from config region 23.
  4823. */
  4824. static void
  4825. lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
  4826. uint8_t *buff)
  4827. {
  4828. struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
  4829. struct lpfc_fcf_conn_hdr *conn_hdr;
  4830. struct lpfc_fcf_conn_rec *conn_rec;
  4831. uint32_t record_count;
  4832. int i;
  4833. /* Free the current connect table */
  4834. list_for_each_entry_safe(conn_entry, next_conn_entry,
  4835. &phba->fcf_conn_rec_list, list) {
  4836. list_del_init(&conn_entry->list);
  4837. kfree(conn_entry);
  4838. }
  4839. conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
  4840. record_count = conn_hdr->length * sizeof(uint32_t)/
  4841. sizeof(struct lpfc_fcf_conn_rec);
  4842. conn_rec = (struct lpfc_fcf_conn_rec *)
  4843. (buff + sizeof(struct lpfc_fcf_conn_hdr));
  4844. for (i = 0; i < record_count; i++) {
  4845. if (!(conn_rec[i].flags & FCFCNCT_VALID))
  4846. continue;
  4847. conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
  4848. GFP_KERNEL);
  4849. if (!conn_entry) {
  4850. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4851. "2566 Failed to allocate connection"
  4852. " table entry\n");
  4853. return;
  4854. }
  4855. memcpy(&conn_entry->conn_rec, &conn_rec[i],
  4856. sizeof(struct lpfc_fcf_conn_rec));
  4857. conn_entry->conn_rec.vlan_tag =
  4858. le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
  4859. conn_entry->conn_rec.flags =
  4860. le16_to_cpu(conn_entry->conn_rec.flags);
  4861. list_add_tail(&conn_entry->list,
  4862. &phba->fcf_conn_rec_list);
  4863. }
  4864. }
  4865. /**
  4866. * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
  4867. * @phba: Pointer to hba context object.
  4868. * @buff: Buffer containing the FCoE parameter data structure.
  4869. *
  4870. * This function update driver data structure with config
  4871. * parameters read from config region 23.
  4872. */
  4873. static void
  4874. lpfc_read_fcoe_param(struct lpfc_hba *phba,
  4875. uint8_t *buff)
  4876. {
  4877. struct lpfc_fip_param_hdr *fcoe_param_hdr;
  4878. struct lpfc_fcoe_params *fcoe_param;
  4879. fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
  4880. buff;
  4881. fcoe_param = (struct lpfc_fcoe_params *)
  4882. (buff + sizeof(struct lpfc_fip_param_hdr));
  4883. if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
  4884. (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
  4885. return;
  4886. if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
  4887. phba->valid_vlan = 1;
  4888. phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
  4889. 0xFFF;
  4890. }
  4891. phba->fc_map[0] = fcoe_param->fc_map[0];
  4892. phba->fc_map[1] = fcoe_param->fc_map[1];
  4893. phba->fc_map[2] = fcoe_param->fc_map[2];
  4894. return;
  4895. }
  4896. /**
  4897. * lpfc_get_rec_conf23 - Get a record type in config region data.
  4898. * @buff: Buffer containing config region 23 data.
  4899. * @size: Size of the data buffer.
  4900. * @rec_type: Record type to be searched.
  4901. *
  4902. * This function searches config region data to find the begining
  4903. * of the record specified by record_type. If record found, this
  4904. * function return pointer to the record else return NULL.
  4905. */
  4906. static uint8_t *
  4907. lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
  4908. {
  4909. uint32_t offset = 0, rec_length;
  4910. if ((buff[0] == LPFC_REGION23_LAST_REC) ||
  4911. (size < sizeof(uint32_t)))
  4912. return NULL;
  4913. rec_length = buff[offset + 1];
  4914. /*
  4915. * One TLV record has one word header and number of data words
  4916. * specified in the rec_length field of the record header.
  4917. */
  4918. while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
  4919. <= size) {
  4920. if (buff[offset] == rec_type)
  4921. return &buff[offset];
  4922. if (buff[offset] == LPFC_REGION23_LAST_REC)
  4923. return NULL;
  4924. offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
  4925. rec_length = buff[offset + 1];
  4926. }
  4927. return NULL;
  4928. }
  4929. /**
  4930. * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
  4931. * @phba: Pointer to lpfc_hba data structure.
  4932. * @buff: Buffer containing config region 23 data.
  4933. * @size: Size of the data buffer.
  4934. *
  4935. * This fuction parse the FCoE config parameters in config region 23 and
  4936. * populate driver data structure with the parameters.
  4937. */
  4938. void
  4939. lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
  4940. uint8_t *buff,
  4941. uint32_t size)
  4942. {
  4943. uint32_t offset = 0, rec_length;
  4944. uint8_t *rec_ptr;
  4945. /*
  4946. * If data size is less than 2 words signature and version cannot be
  4947. * verified.
  4948. */
  4949. if (size < 2*sizeof(uint32_t))
  4950. return;
  4951. /* Check the region signature first */
  4952. if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
  4953. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4954. "2567 Config region 23 has bad signature\n");
  4955. return;
  4956. }
  4957. offset += 4;
  4958. /* Check the data structure version */
  4959. if (buff[offset] != LPFC_REGION23_VERSION) {
  4960. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4961. "2568 Config region 23 has bad version\n");
  4962. return;
  4963. }
  4964. offset += 4;
  4965. rec_length = buff[offset + 1];
  4966. /* Read FCoE param record */
  4967. rec_ptr = lpfc_get_rec_conf23(&buff[offset],
  4968. size - offset, FCOE_PARAM_TYPE);
  4969. if (rec_ptr)
  4970. lpfc_read_fcoe_param(phba, rec_ptr);
  4971. /* Read FCF connection table */
  4972. rec_ptr = lpfc_get_rec_conf23(&buff[offset],
  4973. size - offset, FCOE_CONN_TBL_TYPE);
  4974. if (rec_ptr)
  4975. lpfc_read_fcf_conn_tbl(phba, rec_ptr);
  4976. }