lpfc_hbadisc.c 153 KB

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