lpfc_hbadisc.c 151 KB

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