lpfc_sli.c 489 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2012 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/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/fc/fc_fs.h>
  32. #include <linux/aer.h>
  33. #include "lpfc_hw4.h"
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_nl.h"
  38. #include "lpfc_disc.h"
  39. #include "lpfc_scsi.h"
  40. #include "lpfc.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_compat.h"
  44. #include "lpfc_debugfs.h"
  45. #include "lpfc_vport.h"
  46. /* There are only four IOCB completion types. */
  47. typedef enum _lpfc_iocb_type {
  48. LPFC_UNKNOWN_IOCB,
  49. LPFC_UNSOL_IOCB,
  50. LPFC_SOL_IOCB,
  51. LPFC_ABORT_IOCB
  52. } lpfc_iocb_type;
  53. /* Provide function prototypes local to this module. */
  54. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint32_t);
  56. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  57. uint8_t *, uint32_t *);
  58. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  59. struct lpfc_iocbq *);
  60. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  61. struct hbq_dmabuf *);
  62. static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
  63. struct lpfc_cqe *);
  64. static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
  65. int);
  66. static IOCB_t *
  67. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  68. {
  69. return &iocbq->iocb;
  70. }
  71. /**
  72. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  73. * @q: The Work Queue to operate on.
  74. * @wqe: The work Queue Entry to put on the Work queue.
  75. *
  76. * This routine will copy the contents of @wqe to the next available entry on
  77. * the @q. This function will then ring the Work Queue Doorbell to signal the
  78. * HBA to start processing the Work Queue Entry. This function returns 0 if
  79. * successful. If no entries are available on @q then this function will return
  80. * -ENOMEM.
  81. * The caller is expected to hold the hbalock when calling this routine.
  82. **/
  83. static uint32_t
  84. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  85. {
  86. union lpfc_wqe *temp_wqe;
  87. struct lpfc_register doorbell;
  88. uint32_t host_index;
  89. /* sanity check on queue memory */
  90. if (unlikely(!q))
  91. return -ENOMEM;
  92. temp_wqe = q->qe[q->host_index].wqe;
  93. /* If the host has not yet processed the next entry then we are done */
  94. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  95. return -ENOMEM;
  96. /* set consumption flag every once in a while */
  97. if (!((q->host_index + 1) % q->entry_repost))
  98. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  99. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  100. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  101. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  102. /* Update the host index before invoking device */
  103. host_index = q->host_index;
  104. q->host_index = ((q->host_index + 1) % q->entry_count);
  105. /* Ring Doorbell */
  106. doorbell.word0 = 0;
  107. bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
  108. bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
  109. bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
  110. writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
  111. readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
  112. return 0;
  113. }
  114. /**
  115. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  116. * @q: The Work Queue to operate on.
  117. * @index: The index to advance the hba index to.
  118. *
  119. * This routine will update the HBA index of a queue to reflect consumption of
  120. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  121. * an entry the host calls this function to update the queue's internal
  122. * pointers. This routine returns the number of entries that were consumed by
  123. * the HBA.
  124. **/
  125. static uint32_t
  126. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  127. {
  128. uint32_t released = 0;
  129. /* sanity check on queue memory */
  130. if (unlikely(!q))
  131. return 0;
  132. if (q->hba_index == index)
  133. return 0;
  134. do {
  135. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  136. released++;
  137. } while (q->hba_index != index);
  138. return released;
  139. }
  140. /**
  141. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  142. * @q: The Mailbox Queue to operate on.
  143. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  144. *
  145. * This routine will copy the contents of @mqe to the next available entry on
  146. * the @q. This function will then ring the Work Queue Doorbell to signal the
  147. * HBA to start processing the Work Queue Entry. This function returns 0 if
  148. * successful. If no entries are available on @q then this function will return
  149. * -ENOMEM.
  150. * The caller is expected to hold the hbalock when calling this routine.
  151. **/
  152. static uint32_t
  153. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  154. {
  155. struct lpfc_mqe *temp_mqe;
  156. struct lpfc_register doorbell;
  157. uint32_t host_index;
  158. /* sanity check on queue memory */
  159. if (unlikely(!q))
  160. return -ENOMEM;
  161. temp_mqe = q->qe[q->host_index].mqe;
  162. /* If the host has not yet processed the next entry then we are done */
  163. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  164. return -ENOMEM;
  165. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  166. /* Save off the mailbox pointer for completion */
  167. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  168. /* Update the host index before invoking device */
  169. host_index = q->host_index;
  170. q->host_index = ((q->host_index + 1) % q->entry_count);
  171. /* Ring Doorbell */
  172. doorbell.word0 = 0;
  173. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  174. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  175. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  176. readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
  177. return 0;
  178. }
  179. /**
  180. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  181. * @q: The Mailbox Queue to operate on.
  182. *
  183. * This routine will update the HBA index of a queue to reflect consumption of
  184. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  185. * an entry the host calls this function to update the queue's internal
  186. * pointers. This routine returns the number of entries that were consumed by
  187. * the HBA.
  188. **/
  189. static uint32_t
  190. lpfc_sli4_mq_release(struct lpfc_queue *q)
  191. {
  192. /* sanity check on queue memory */
  193. if (unlikely(!q))
  194. return 0;
  195. /* Clear the mailbox pointer for completion */
  196. q->phba->mbox = NULL;
  197. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  198. return 1;
  199. }
  200. /**
  201. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  202. * @q: The Event Queue to get the first valid EQE from
  203. *
  204. * This routine will get the first valid Event Queue Entry from @q, update
  205. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  206. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  207. * processed, but not popped back to the HBA then this routine will return NULL.
  208. **/
  209. static struct lpfc_eqe *
  210. lpfc_sli4_eq_get(struct lpfc_queue *q)
  211. {
  212. struct lpfc_eqe *eqe;
  213. /* sanity check on queue memory */
  214. if (unlikely(!q))
  215. return NULL;
  216. eqe = q->qe[q->hba_index].eqe;
  217. /* If the next EQE is not valid then we are done */
  218. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  219. return NULL;
  220. /* If the host has not yet processed the next entry then we are done */
  221. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  222. return NULL;
  223. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  224. return eqe;
  225. }
  226. /**
  227. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  228. * @q: The Event Queue that the host has completed processing for.
  229. * @arm: Indicates whether the host wants to arms this CQ.
  230. *
  231. * This routine will mark all Event Queue Entries on @q, from the last
  232. * known completed entry to the last entry that was processed, as completed
  233. * by clearing the valid bit for each completion queue entry. Then it will
  234. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  235. * The internal host index in the @q will be updated by this routine to indicate
  236. * that the host has finished processing the entries. The @arm parameter
  237. * indicates that the queue should be rearmed when ringing the doorbell.
  238. *
  239. * This function will return the number of EQEs that were popped.
  240. **/
  241. uint32_t
  242. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  243. {
  244. uint32_t released = 0;
  245. struct lpfc_eqe *temp_eqe;
  246. struct lpfc_register doorbell;
  247. /* sanity check on queue memory */
  248. if (unlikely(!q))
  249. return 0;
  250. /* while there are valid entries */
  251. while (q->hba_index != q->host_index) {
  252. temp_eqe = q->qe[q->host_index].eqe;
  253. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  254. released++;
  255. q->host_index = ((q->host_index + 1) % q->entry_count);
  256. }
  257. if (unlikely(released == 0 && !arm))
  258. return 0;
  259. /* ring doorbell for number popped */
  260. doorbell.word0 = 0;
  261. if (arm) {
  262. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  263. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  264. }
  265. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  266. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  267. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  268. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  269. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  270. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  271. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  272. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  273. readl(q->phba->sli4_hba.EQCQDBregaddr);
  274. return released;
  275. }
  276. /**
  277. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  278. * @q: The Completion Queue to get the first valid CQE from
  279. *
  280. * This routine will get the first valid Completion Queue Entry from @q, update
  281. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  282. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  283. * processed, but not popped back to the HBA then this routine will return NULL.
  284. **/
  285. static struct lpfc_cqe *
  286. lpfc_sli4_cq_get(struct lpfc_queue *q)
  287. {
  288. struct lpfc_cqe *cqe;
  289. /* sanity check on queue memory */
  290. if (unlikely(!q))
  291. return NULL;
  292. /* If the next CQE is not valid then we are done */
  293. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  294. return NULL;
  295. /* If the host has not yet processed the next entry then we are done */
  296. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  297. return NULL;
  298. cqe = q->qe[q->hba_index].cqe;
  299. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  300. return cqe;
  301. }
  302. /**
  303. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  304. * @q: The Completion Queue that the host has completed processing for.
  305. * @arm: Indicates whether the host wants to arms this CQ.
  306. *
  307. * This routine will mark all Completion queue entries on @q, from the last
  308. * known completed entry to the last entry that was processed, as completed
  309. * by clearing the valid bit for each completion queue entry. Then it will
  310. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  311. * The internal host index in the @q will be updated by this routine to indicate
  312. * that the host has finished processing the entries. The @arm parameter
  313. * indicates that the queue should be rearmed when ringing the doorbell.
  314. *
  315. * This function will return the number of CQEs that were released.
  316. **/
  317. uint32_t
  318. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  319. {
  320. uint32_t released = 0;
  321. struct lpfc_cqe *temp_qe;
  322. struct lpfc_register doorbell;
  323. /* sanity check on queue memory */
  324. if (unlikely(!q))
  325. return 0;
  326. /* while there are valid entries */
  327. while (q->hba_index != q->host_index) {
  328. temp_qe = q->qe[q->host_index].cqe;
  329. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  330. released++;
  331. q->host_index = ((q->host_index + 1) % q->entry_count);
  332. }
  333. if (unlikely(released == 0 && !arm))
  334. return 0;
  335. /* ring doorbell for number popped */
  336. doorbell.word0 = 0;
  337. if (arm)
  338. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  339. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  340. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  341. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  342. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  343. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  344. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  345. return released;
  346. }
  347. /**
  348. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  349. * @q: The Header Receive Queue to operate on.
  350. * @wqe: The Receive Queue Entry to put on the Receive queue.
  351. *
  352. * This routine will copy the contents of @wqe to the next available entry on
  353. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  354. * HBA to start processing the Receive Queue Entry. This function returns the
  355. * index that the rqe was copied to if successful. If no entries are available
  356. * on @q then this function will return -ENOMEM.
  357. * The caller is expected to hold the hbalock when calling this routine.
  358. **/
  359. static int
  360. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  361. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  362. {
  363. struct lpfc_rqe *temp_hrqe;
  364. struct lpfc_rqe *temp_drqe;
  365. struct lpfc_register doorbell;
  366. int put_index = hq->host_index;
  367. /* sanity check on queue memory */
  368. if (unlikely(!hq) || unlikely(!dq))
  369. return -ENOMEM;
  370. temp_hrqe = hq->qe[hq->host_index].rqe;
  371. temp_drqe = dq->qe[dq->host_index].rqe;
  372. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  373. return -EINVAL;
  374. if (hq->host_index != dq->host_index)
  375. return -EINVAL;
  376. /* If the host has not yet processed the next entry then we are done */
  377. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  378. return -EBUSY;
  379. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  380. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  381. /* Update the host index to point to the next slot */
  382. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  383. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  384. /* Ring The Header Receive Queue Doorbell */
  385. if (!(hq->host_index % hq->entry_repost)) {
  386. doorbell.word0 = 0;
  387. bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
  388. hq->entry_repost);
  389. bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
  390. writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
  391. }
  392. return put_index;
  393. }
  394. /**
  395. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  396. * @q: The Header Receive Queue to operate on.
  397. *
  398. * This routine will update the HBA index of a queue to reflect consumption of
  399. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  400. * consumed an entry the host calls this function to update the queue's
  401. * internal pointers. This routine returns the number of entries that were
  402. * consumed by the HBA.
  403. **/
  404. static uint32_t
  405. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  406. {
  407. /* sanity check on queue memory */
  408. if (unlikely(!hq) || unlikely(!dq))
  409. return 0;
  410. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  411. return 0;
  412. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  413. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  414. return 1;
  415. }
  416. /**
  417. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  418. * @phba: Pointer to HBA context object.
  419. * @pring: Pointer to driver SLI ring object.
  420. *
  421. * This function returns pointer to next command iocb entry
  422. * in the command ring. The caller must hold hbalock to prevent
  423. * other threads consume the next command iocb.
  424. * SLI-2/SLI-3 provide different sized iocbs.
  425. **/
  426. static inline IOCB_t *
  427. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  428. {
  429. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  430. pring->cmdidx * phba->iocb_cmd_size);
  431. }
  432. /**
  433. * lpfc_resp_iocb - Get next response iocb entry in the ring
  434. * @phba: Pointer to HBA context object.
  435. * @pring: Pointer to driver SLI ring object.
  436. *
  437. * This function returns pointer to next response iocb entry
  438. * in the response ring. The caller must hold hbalock to make sure
  439. * that no other thread consume the next response iocb.
  440. * SLI-2/SLI-3 provide different sized iocbs.
  441. **/
  442. static inline IOCB_t *
  443. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  444. {
  445. return (IOCB_t *) (((char *) pring->rspringaddr) +
  446. pring->rspidx * phba->iocb_rsp_size);
  447. }
  448. /**
  449. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  450. * @phba: Pointer to HBA context object.
  451. *
  452. * This function is called with hbalock held. This function
  453. * allocates a new driver iocb object from the iocb pool. If the
  454. * allocation is successful, it returns pointer to the newly
  455. * allocated iocb object else it returns NULL.
  456. **/
  457. struct lpfc_iocbq *
  458. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  459. {
  460. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  461. struct lpfc_iocbq * iocbq = NULL;
  462. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  463. if (iocbq)
  464. phba->iocb_cnt++;
  465. if (phba->iocb_cnt > phba->iocb_max)
  466. phba->iocb_max = phba->iocb_cnt;
  467. return iocbq;
  468. }
  469. /**
  470. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  471. * @phba: Pointer to HBA context object.
  472. * @xritag: XRI value.
  473. *
  474. * This function clears the sglq pointer from the array of acive
  475. * sglq's. The xritag that is passed in is used to index into the
  476. * array. Before the xritag can be used it needs to be adjusted
  477. * by subtracting the xribase.
  478. *
  479. * Returns sglq ponter = success, NULL = Failure.
  480. **/
  481. static struct lpfc_sglq *
  482. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  483. {
  484. struct lpfc_sglq *sglq;
  485. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  486. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  487. return sglq;
  488. }
  489. /**
  490. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  491. * @phba: Pointer to HBA context object.
  492. * @xritag: XRI value.
  493. *
  494. * This function returns the sglq pointer from the array of acive
  495. * sglq's. The xritag that is passed in is used to index into the
  496. * array. Before the xritag can be used it needs to be adjusted
  497. * by subtracting the xribase.
  498. *
  499. * Returns sglq ponter = success, NULL = Failure.
  500. **/
  501. struct lpfc_sglq *
  502. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  503. {
  504. struct lpfc_sglq *sglq;
  505. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  506. return sglq;
  507. }
  508. /**
  509. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  510. * @phba: Pointer to HBA context object.
  511. * @xritag: xri used in this exchange.
  512. * @rrq: The RRQ to be cleared.
  513. *
  514. **/
  515. void
  516. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  517. uint16_t xritag,
  518. struct lpfc_node_rrq *rrq)
  519. {
  520. struct lpfc_nodelist *ndlp = NULL;
  521. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  522. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  523. /* The target DID could have been swapped (cable swap)
  524. * we should use the ndlp from the findnode if it is
  525. * available.
  526. */
  527. if ((!ndlp) && rrq->ndlp)
  528. ndlp = rrq->ndlp;
  529. if (!ndlp)
  530. goto out;
  531. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  532. rrq->send_rrq = 0;
  533. rrq->xritag = 0;
  534. rrq->rrq_stop_time = 0;
  535. }
  536. out:
  537. mempool_free(rrq, phba->rrq_pool);
  538. }
  539. /**
  540. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  541. * @phba: Pointer to HBA context object.
  542. *
  543. * This function is called with hbalock held. This function
  544. * Checks if stop_time (ratov from setting rrq active) has
  545. * been reached, if it has and the send_rrq flag is set then
  546. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  547. * then it will just call the routine to clear the rrq and
  548. * free the rrq resource.
  549. * The timer is set to the next rrq that is going to expire before
  550. * leaving the routine.
  551. *
  552. **/
  553. void
  554. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  555. {
  556. struct lpfc_node_rrq *rrq;
  557. struct lpfc_node_rrq *nextrrq;
  558. unsigned long next_time;
  559. unsigned long iflags;
  560. LIST_HEAD(send_rrq);
  561. spin_lock_irqsave(&phba->hbalock, iflags);
  562. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  563. next_time = jiffies + HZ * (phba->fc_ratov + 1);
  564. list_for_each_entry_safe(rrq, nextrrq,
  565. &phba->active_rrq_list, list) {
  566. if (time_after(jiffies, rrq->rrq_stop_time))
  567. list_move(&rrq->list, &send_rrq);
  568. else if (time_before(rrq->rrq_stop_time, next_time))
  569. next_time = rrq->rrq_stop_time;
  570. }
  571. spin_unlock_irqrestore(&phba->hbalock, iflags);
  572. if (!list_empty(&phba->active_rrq_list))
  573. mod_timer(&phba->rrq_tmr, next_time);
  574. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  575. list_del(&rrq->list);
  576. if (!rrq->send_rrq)
  577. /* this call will free the rrq */
  578. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  579. else if (lpfc_send_rrq(phba, rrq)) {
  580. /* if we send the rrq then the completion handler
  581. * will clear the bit in the xribitmap.
  582. */
  583. lpfc_clr_rrq_active(phba, rrq->xritag,
  584. rrq);
  585. }
  586. }
  587. }
  588. /**
  589. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  590. * @vport: Pointer to vport context object.
  591. * @xri: The xri used in the exchange.
  592. * @did: The targets DID for this exchange.
  593. *
  594. * returns NULL = rrq not found in the phba->active_rrq_list.
  595. * rrq = rrq for this xri and target.
  596. **/
  597. struct lpfc_node_rrq *
  598. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  599. {
  600. struct lpfc_hba *phba = vport->phba;
  601. struct lpfc_node_rrq *rrq;
  602. struct lpfc_node_rrq *nextrrq;
  603. unsigned long iflags;
  604. if (phba->sli_rev != LPFC_SLI_REV4)
  605. return NULL;
  606. spin_lock_irqsave(&phba->hbalock, iflags);
  607. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  608. if (rrq->vport == vport && rrq->xritag == xri &&
  609. rrq->nlp_DID == did){
  610. list_del(&rrq->list);
  611. spin_unlock_irqrestore(&phba->hbalock, iflags);
  612. return rrq;
  613. }
  614. }
  615. spin_unlock_irqrestore(&phba->hbalock, iflags);
  616. return NULL;
  617. }
  618. /**
  619. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  620. * @vport: Pointer to vport context object.
  621. * @ndlp: Pointer to the lpfc_node_list structure.
  622. * If ndlp is NULL Remove all active RRQs for this vport from the
  623. * phba->active_rrq_list and clear the rrq.
  624. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  625. **/
  626. void
  627. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  628. {
  629. struct lpfc_hba *phba = vport->phba;
  630. struct lpfc_node_rrq *rrq;
  631. struct lpfc_node_rrq *nextrrq;
  632. unsigned long iflags;
  633. LIST_HEAD(rrq_list);
  634. if (phba->sli_rev != LPFC_SLI_REV4)
  635. return;
  636. if (!ndlp) {
  637. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  638. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  639. }
  640. spin_lock_irqsave(&phba->hbalock, iflags);
  641. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  642. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  643. list_move(&rrq->list, &rrq_list);
  644. spin_unlock_irqrestore(&phba->hbalock, iflags);
  645. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  646. list_del(&rrq->list);
  647. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  648. }
  649. }
  650. /**
  651. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  652. * @phba: Pointer to HBA context object.
  653. *
  654. * Remove all rrqs from the phba->active_rrq_list and free them by
  655. * calling __lpfc_clr_active_rrq
  656. *
  657. **/
  658. void
  659. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  660. {
  661. struct lpfc_node_rrq *rrq;
  662. struct lpfc_node_rrq *nextrrq;
  663. unsigned long next_time;
  664. unsigned long iflags;
  665. LIST_HEAD(rrq_list);
  666. if (phba->sli_rev != LPFC_SLI_REV4)
  667. return;
  668. spin_lock_irqsave(&phba->hbalock, iflags);
  669. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  670. next_time = jiffies + HZ * (phba->fc_ratov * 2);
  671. list_splice_init(&phba->active_rrq_list, &rrq_list);
  672. spin_unlock_irqrestore(&phba->hbalock, iflags);
  673. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  674. list_del(&rrq->list);
  675. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  676. }
  677. if (!list_empty(&phba->active_rrq_list))
  678. mod_timer(&phba->rrq_tmr, next_time);
  679. }
  680. /**
  681. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  682. * @phba: Pointer to HBA context object.
  683. * @ndlp: Targets nodelist pointer for this exchange.
  684. * @xritag the xri in the bitmap to test.
  685. *
  686. * This function is called with hbalock held. This function
  687. * returns 0 = rrq not active for this xri
  688. * 1 = rrq is valid for this xri.
  689. **/
  690. int
  691. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  692. uint16_t xritag)
  693. {
  694. if (!ndlp)
  695. return 0;
  696. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  697. return 1;
  698. else
  699. return 0;
  700. }
  701. /**
  702. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  703. * @phba: Pointer to HBA context object.
  704. * @ndlp: nodelist pointer for this target.
  705. * @xritag: xri used in this exchange.
  706. * @rxid: Remote Exchange ID.
  707. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  708. *
  709. * This function takes the hbalock.
  710. * The active bit is always set in the active rrq xri_bitmap even
  711. * if there is no slot avaiable for the other rrq information.
  712. *
  713. * returns 0 rrq actived for this xri
  714. * < 0 No memory or invalid ndlp.
  715. **/
  716. int
  717. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  718. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  719. {
  720. unsigned long iflags;
  721. struct lpfc_node_rrq *rrq;
  722. int empty;
  723. if (!ndlp)
  724. return -EINVAL;
  725. if (!phba->cfg_enable_rrq)
  726. return -EINVAL;
  727. spin_lock_irqsave(&phba->hbalock, iflags);
  728. if (phba->pport->load_flag & FC_UNLOADING) {
  729. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  730. goto out;
  731. }
  732. /*
  733. * set the active bit even if there is no mem available.
  734. */
  735. if (NLP_CHK_FREE_REQ(ndlp))
  736. goto out;
  737. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  738. goto out;
  739. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  740. goto out;
  741. spin_unlock_irqrestore(&phba->hbalock, iflags);
  742. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  743. if (!rrq) {
  744. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  745. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  746. " DID:0x%x Send:%d\n",
  747. xritag, rxid, ndlp->nlp_DID, send_rrq);
  748. return -EINVAL;
  749. }
  750. rrq->send_rrq = send_rrq;
  751. rrq->xritag = xritag;
  752. rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
  753. rrq->ndlp = ndlp;
  754. rrq->nlp_DID = ndlp->nlp_DID;
  755. rrq->vport = ndlp->vport;
  756. rrq->rxid = rxid;
  757. rrq->send_rrq = send_rrq;
  758. spin_lock_irqsave(&phba->hbalock, iflags);
  759. empty = list_empty(&phba->active_rrq_list);
  760. list_add_tail(&rrq->list, &phba->active_rrq_list);
  761. phba->hba_flag |= HBA_RRQ_ACTIVE;
  762. if (empty)
  763. lpfc_worker_wake_up(phba);
  764. spin_unlock_irqrestore(&phba->hbalock, iflags);
  765. return 0;
  766. out:
  767. spin_unlock_irqrestore(&phba->hbalock, iflags);
  768. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  769. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  770. " DID:0x%x Send:%d\n",
  771. xritag, rxid, ndlp->nlp_DID, send_rrq);
  772. return -EINVAL;
  773. }
  774. /**
  775. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  776. * @phba: Pointer to HBA context object.
  777. * @piocb: Pointer to the iocbq.
  778. *
  779. * This function is called with hbalock held. This function
  780. * gets a new driver sglq object from the sglq list. If the
  781. * list is not empty then it is successful, it returns pointer to the newly
  782. * allocated sglq object else it returns NULL.
  783. **/
  784. static struct lpfc_sglq *
  785. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  786. {
  787. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  788. struct lpfc_sglq *sglq = NULL;
  789. struct lpfc_sglq *start_sglq = NULL;
  790. struct lpfc_scsi_buf *lpfc_cmd;
  791. struct lpfc_nodelist *ndlp;
  792. int found = 0;
  793. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  794. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  795. ndlp = lpfc_cmd->rdata->pnode;
  796. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  797. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  798. ndlp = piocbq->context_un.ndlp;
  799. else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
  800. (piocbq->iocb_flag & LPFC_IO_LIBDFC))
  801. ndlp = piocbq->context_un.ndlp;
  802. else
  803. ndlp = piocbq->context1;
  804. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  805. start_sglq = sglq;
  806. while (!found) {
  807. if (!sglq)
  808. return NULL;
  809. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
  810. /* This xri has an rrq outstanding for this DID.
  811. * put it back in the list and get another xri.
  812. */
  813. list_add_tail(&sglq->list, lpfc_sgl_list);
  814. sglq = NULL;
  815. list_remove_head(lpfc_sgl_list, sglq,
  816. struct lpfc_sglq, list);
  817. if (sglq == start_sglq) {
  818. sglq = NULL;
  819. break;
  820. } else
  821. continue;
  822. }
  823. sglq->ndlp = ndlp;
  824. found = 1;
  825. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  826. sglq->state = SGL_ALLOCATED;
  827. }
  828. return sglq;
  829. }
  830. /**
  831. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  832. * @phba: Pointer to HBA context object.
  833. *
  834. * This function is called with no lock held. This function
  835. * allocates a new driver iocb object from the iocb pool. If the
  836. * allocation is successful, it returns pointer to the newly
  837. * allocated iocb object else it returns NULL.
  838. **/
  839. struct lpfc_iocbq *
  840. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  841. {
  842. struct lpfc_iocbq * iocbq = NULL;
  843. unsigned long iflags;
  844. spin_lock_irqsave(&phba->hbalock, iflags);
  845. iocbq = __lpfc_sli_get_iocbq(phba);
  846. spin_unlock_irqrestore(&phba->hbalock, iflags);
  847. return iocbq;
  848. }
  849. /**
  850. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  851. * @phba: Pointer to HBA context object.
  852. * @iocbq: Pointer to driver iocb object.
  853. *
  854. * This function is called with hbalock held to release driver
  855. * iocb object to the iocb pool. The iotag in the iocb object
  856. * does not change for each use of the iocb object. This function
  857. * clears all other fields of the iocb object when it is freed.
  858. * The sqlq structure that holds the xritag and phys and virtual
  859. * mappings for the scatter gather list is retrieved from the
  860. * active array of sglq. The get of the sglq pointer also clears
  861. * the entry in the array. If the status of the IO indiactes that
  862. * this IO was aborted then the sglq entry it put on the
  863. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  864. * IO has good status or fails for any other reason then the sglq
  865. * entry is added to the free list (lpfc_sgl_list).
  866. **/
  867. static void
  868. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  869. {
  870. struct lpfc_sglq *sglq;
  871. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  872. unsigned long iflag = 0;
  873. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  874. if (iocbq->sli4_xritag == NO_XRI)
  875. sglq = NULL;
  876. else
  877. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  878. if (sglq) {
  879. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  880. (sglq->state != SGL_XRI_ABORTED)) {
  881. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  882. iflag);
  883. list_add(&sglq->list,
  884. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  885. spin_unlock_irqrestore(
  886. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  887. } else {
  888. sglq->state = SGL_FREED;
  889. sglq->ndlp = NULL;
  890. list_add_tail(&sglq->list,
  891. &phba->sli4_hba.lpfc_sgl_list);
  892. /* Check if TXQ queue needs to be serviced */
  893. if (pring->txq_cnt)
  894. lpfc_worker_wake_up(phba);
  895. }
  896. }
  897. /*
  898. * Clean all volatile data fields, preserve iotag and node struct.
  899. */
  900. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  901. iocbq->sli4_lxritag = NO_XRI;
  902. iocbq->sli4_xritag = NO_XRI;
  903. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  904. }
  905. /**
  906. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  907. * @phba: Pointer to HBA context object.
  908. * @iocbq: Pointer to driver iocb object.
  909. *
  910. * This function is called with hbalock held to release driver
  911. * iocb object to the iocb pool. The iotag in the iocb object
  912. * does not change for each use of the iocb object. This function
  913. * clears all other fields of the iocb object when it is freed.
  914. **/
  915. static void
  916. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  917. {
  918. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  919. /*
  920. * Clean all volatile data fields, preserve iotag and node struct.
  921. */
  922. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  923. iocbq->sli4_xritag = NO_XRI;
  924. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  925. }
  926. /**
  927. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  928. * @phba: Pointer to HBA context object.
  929. * @iocbq: Pointer to driver iocb object.
  930. *
  931. * This function is called with hbalock held to release driver
  932. * iocb object to the iocb pool. The iotag in the iocb object
  933. * does not change for each use of the iocb object. This function
  934. * clears all other fields of the iocb object when it is freed.
  935. **/
  936. static void
  937. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  938. {
  939. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  940. phba->iocb_cnt--;
  941. }
  942. /**
  943. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  944. * @phba: Pointer to HBA context object.
  945. * @iocbq: Pointer to driver iocb object.
  946. *
  947. * This function is called with no lock held to release the iocb to
  948. * iocb pool.
  949. **/
  950. void
  951. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  952. {
  953. unsigned long iflags;
  954. /*
  955. * Clean all volatile data fields, preserve iotag and node struct.
  956. */
  957. spin_lock_irqsave(&phba->hbalock, iflags);
  958. __lpfc_sli_release_iocbq(phba, iocbq);
  959. spin_unlock_irqrestore(&phba->hbalock, iflags);
  960. }
  961. /**
  962. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  963. * @phba: Pointer to HBA context object.
  964. * @iocblist: List of IOCBs.
  965. * @ulpstatus: ULP status in IOCB command field.
  966. * @ulpWord4: ULP word-4 in IOCB command field.
  967. *
  968. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  969. * on the list by invoking the complete callback function associated with the
  970. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  971. * fields.
  972. **/
  973. void
  974. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  975. uint32_t ulpstatus, uint32_t ulpWord4)
  976. {
  977. struct lpfc_iocbq *piocb;
  978. while (!list_empty(iocblist)) {
  979. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  980. if (!piocb->iocb_cmpl)
  981. lpfc_sli_release_iocbq(phba, piocb);
  982. else {
  983. piocb->iocb.ulpStatus = ulpstatus;
  984. piocb->iocb.un.ulpWord[4] = ulpWord4;
  985. (piocb->iocb_cmpl) (phba, piocb, piocb);
  986. }
  987. }
  988. return;
  989. }
  990. /**
  991. * lpfc_sli_iocb_cmd_type - Get the iocb type
  992. * @iocb_cmnd: iocb command code.
  993. *
  994. * This function is called by ring event handler function to get the iocb type.
  995. * This function translates the iocb command to an iocb command type used to
  996. * decide the final disposition of each completed IOCB.
  997. * The function returns
  998. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  999. * LPFC_SOL_IOCB if it is a solicited iocb completion
  1000. * LPFC_ABORT_IOCB if it is an abort iocb
  1001. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  1002. *
  1003. * The caller is not required to hold any lock.
  1004. **/
  1005. static lpfc_iocb_type
  1006. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1007. {
  1008. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1009. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1010. return 0;
  1011. switch (iocb_cmnd) {
  1012. case CMD_XMIT_SEQUENCE_CR:
  1013. case CMD_XMIT_SEQUENCE_CX:
  1014. case CMD_XMIT_BCAST_CN:
  1015. case CMD_XMIT_BCAST_CX:
  1016. case CMD_ELS_REQUEST_CR:
  1017. case CMD_ELS_REQUEST_CX:
  1018. case CMD_CREATE_XRI_CR:
  1019. case CMD_CREATE_XRI_CX:
  1020. case CMD_GET_RPI_CN:
  1021. case CMD_XMIT_ELS_RSP_CX:
  1022. case CMD_GET_RPI_CR:
  1023. case CMD_FCP_IWRITE_CR:
  1024. case CMD_FCP_IWRITE_CX:
  1025. case CMD_FCP_IREAD_CR:
  1026. case CMD_FCP_IREAD_CX:
  1027. case CMD_FCP_ICMND_CR:
  1028. case CMD_FCP_ICMND_CX:
  1029. case CMD_FCP_TSEND_CX:
  1030. case CMD_FCP_TRSP_CX:
  1031. case CMD_FCP_TRECEIVE_CX:
  1032. case CMD_FCP_AUTO_TRSP_CX:
  1033. case CMD_ADAPTER_MSG:
  1034. case CMD_ADAPTER_DUMP:
  1035. case CMD_XMIT_SEQUENCE64_CR:
  1036. case CMD_XMIT_SEQUENCE64_CX:
  1037. case CMD_XMIT_BCAST64_CN:
  1038. case CMD_XMIT_BCAST64_CX:
  1039. case CMD_ELS_REQUEST64_CR:
  1040. case CMD_ELS_REQUEST64_CX:
  1041. case CMD_FCP_IWRITE64_CR:
  1042. case CMD_FCP_IWRITE64_CX:
  1043. case CMD_FCP_IREAD64_CR:
  1044. case CMD_FCP_IREAD64_CX:
  1045. case CMD_FCP_ICMND64_CR:
  1046. case CMD_FCP_ICMND64_CX:
  1047. case CMD_FCP_TSEND64_CX:
  1048. case CMD_FCP_TRSP64_CX:
  1049. case CMD_FCP_TRECEIVE64_CX:
  1050. case CMD_GEN_REQUEST64_CR:
  1051. case CMD_GEN_REQUEST64_CX:
  1052. case CMD_XMIT_ELS_RSP64_CX:
  1053. case DSSCMD_IWRITE64_CR:
  1054. case DSSCMD_IWRITE64_CX:
  1055. case DSSCMD_IREAD64_CR:
  1056. case DSSCMD_IREAD64_CX:
  1057. type = LPFC_SOL_IOCB;
  1058. break;
  1059. case CMD_ABORT_XRI_CN:
  1060. case CMD_ABORT_XRI_CX:
  1061. case CMD_CLOSE_XRI_CN:
  1062. case CMD_CLOSE_XRI_CX:
  1063. case CMD_XRI_ABORTED_CX:
  1064. case CMD_ABORT_MXRI64_CN:
  1065. case CMD_XMIT_BLS_RSP64_CX:
  1066. type = LPFC_ABORT_IOCB;
  1067. break;
  1068. case CMD_RCV_SEQUENCE_CX:
  1069. case CMD_RCV_ELS_REQ_CX:
  1070. case CMD_RCV_SEQUENCE64_CX:
  1071. case CMD_RCV_ELS_REQ64_CX:
  1072. case CMD_ASYNC_STATUS:
  1073. case CMD_IOCB_RCV_SEQ64_CX:
  1074. case CMD_IOCB_RCV_ELS64_CX:
  1075. case CMD_IOCB_RCV_CONT64_CX:
  1076. case CMD_IOCB_RET_XRI64_CX:
  1077. type = LPFC_UNSOL_IOCB;
  1078. break;
  1079. case CMD_IOCB_XMIT_MSEQ64_CR:
  1080. case CMD_IOCB_XMIT_MSEQ64_CX:
  1081. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1082. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1083. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1084. case CMD_IOCB_ABORT_EXTENDED_CN:
  1085. case CMD_IOCB_RET_HBQE64_CN:
  1086. case CMD_IOCB_FCP_IBIDIR64_CR:
  1087. case CMD_IOCB_FCP_IBIDIR64_CX:
  1088. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1089. case CMD_IOCB_LOGENTRY_CN:
  1090. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1091. printk("%s - Unhandled SLI-3 Command x%x\n",
  1092. __func__, iocb_cmnd);
  1093. type = LPFC_UNKNOWN_IOCB;
  1094. break;
  1095. default:
  1096. type = LPFC_UNKNOWN_IOCB;
  1097. break;
  1098. }
  1099. return type;
  1100. }
  1101. /**
  1102. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1103. * @phba: Pointer to HBA context object.
  1104. *
  1105. * This function is called from SLI initialization code
  1106. * to configure every ring of the HBA's SLI interface. The
  1107. * caller is not required to hold any lock. This function issues
  1108. * a config_ring mailbox command for each ring.
  1109. * This function returns zero if successful else returns a negative
  1110. * error code.
  1111. **/
  1112. static int
  1113. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1114. {
  1115. struct lpfc_sli *psli = &phba->sli;
  1116. LPFC_MBOXQ_t *pmb;
  1117. MAILBOX_t *pmbox;
  1118. int i, rc, ret = 0;
  1119. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1120. if (!pmb)
  1121. return -ENOMEM;
  1122. pmbox = &pmb->u.mb;
  1123. phba->link_state = LPFC_INIT_MBX_CMDS;
  1124. for (i = 0; i < psli->num_rings; i++) {
  1125. lpfc_config_ring(phba, i, pmb);
  1126. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1127. if (rc != MBX_SUCCESS) {
  1128. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1129. "0446 Adapter failed to init (%d), "
  1130. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1131. "ring %d\n",
  1132. rc, pmbox->mbxCommand,
  1133. pmbox->mbxStatus, i);
  1134. phba->link_state = LPFC_HBA_ERROR;
  1135. ret = -ENXIO;
  1136. break;
  1137. }
  1138. }
  1139. mempool_free(pmb, phba->mbox_mem_pool);
  1140. return ret;
  1141. }
  1142. /**
  1143. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1144. * @phba: Pointer to HBA context object.
  1145. * @pring: Pointer to driver SLI ring object.
  1146. * @piocb: Pointer to the driver iocb object.
  1147. *
  1148. * This function is called with hbalock held. The function adds the
  1149. * new iocb to txcmplq of the given ring. This function always returns
  1150. * 0. If this function is called for ELS ring, this function checks if
  1151. * there is a vport associated with the ELS command. This function also
  1152. * starts els_tmofunc timer if this is an ELS command.
  1153. **/
  1154. static int
  1155. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1156. struct lpfc_iocbq *piocb)
  1157. {
  1158. list_add_tail(&piocb->list, &pring->txcmplq);
  1159. piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
  1160. pring->txcmplq_cnt++;
  1161. if (pring->txcmplq_cnt > pring->txcmplq_max)
  1162. pring->txcmplq_max = pring->txcmplq_cnt;
  1163. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1164. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1165. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1166. if (!piocb->vport)
  1167. BUG();
  1168. else
  1169. mod_timer(&piocb->vport->els_tmofunc,
  1170. jiffies + HZ * (phba->fc_ratov << 1));
  1171. }
  1172. return 0;
  1173. }
  1174. /**
  1175. * lpfc_sli_ringtx_get - Get first element of the txq
  1176. * @phba: Pointer to HBA context object.
  1177. * @pring: Pointer to driver SLI ring object.
  1178. *
  1179. * This function is called with hbalock held to get next
  1180. * iocb in txq of the given ring. If there is any iocb in
  1181. * the txq, the function returns first iocb in the list after
  1182. * removing the iocb from the list, else it returns NULL.
  1183. **/
  1184. struct lpfc_iocbq *
  1185. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1186. {
  1187. struct lpfc_iocbq *cmd_iocb;
  1188. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1189. if (cmd_iocb != NULL)
  1190. pring->txq_cnt--;
  1191. return cmd_iocb;
  1192. }
  1193. /**
  1194. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1195. * @phba: Pointer to HBA context object.
  1196. * @pring: Pointer to driver SLI ring object.
  1197. *
  1198. * This function is called with hbalock held and the caller must post the
  1199. * iocb without releasing the lock. If the caller releases the lock,
  1200. * iocb slot returned by the function is not guaranteed to be available.
  1201. * The function returns pointer to the next available iocb slot if there
  1202. * is available slot in the ring, else it returns NULL.
  1203. * If the get index of the ring is ahead of the put index, the function
  1204. * will post an error attention event to the worker thread to take the
  1205. * HBA to offline state.
  1206. **/
  1207. static IOCB_t *
  1208. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1209. {
  1210. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1211. uint32_t max_cmd_idx = pring->numCiocb;
  1212. if ((pring->next_cmdidx == pring->cmdidx) &&
  1213. (++pring->next_cmdidx >= max_cmd_idx))
  1214. pring->next_cmdidx = 0;
  1215. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  1216. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1217. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  1218. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1219. "0315 Ring %d issue: portCmdGet %d "
  1220. "is bigger than cmd ring %d\n",
  1221. pring->ringno,
  1222. pring->local_getidx, max_cmd_idx);
  1223. phba->link_state = LPFC_HBA_ERROR;
  1224. /*
  1225. * All error attention handlers are posted to
  1226. * worker thread
  1227. */
  1228. phba->work_ha |= HA_ERATT;
  1229. phba->work_hs = HS_FFER3;
  1230. lpfc_worker_wake_up(phba);
  1231. return NULL;
  1232. }
  1233. if (pring->local_getidx == pring->next_cmdidx)
  1234. return NULL;
  1235. }
  1236. return lpfc_cmd_iocb(phba, pring);
  1237. }
  1238. /**
  1239. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1240. * @phba: Pointer to HBA context object.
  1241. * @iocbq: Pointer to driver iocb object.
  1242. *
  1243. * This function gets an iotag for the iocb. If there is no unused iotag and
  1244. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1245. * array and assigns a new iotag.
  1246. * The function returns the allocated iotag if successful, else returns zero.
  1247. * Zero is not a valid iotag.
  1248. * The caller is not required to hold any lock.
  1249. **/
  1250. uint16_t
  1251. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1252. {
  1253. struct lpfc_iocbq **new_arr;
  1254. struct lpfc_iocbq **old_arr;
  1255. size_t new_len;
  1256. struct lpfc_sli *psli = &phba->sli;
  1257. uint16_t iotag;
  1258. spin_lock_irq(&phba->hbalock);
  1259. iotag = psli->last_iotag;
  1260. if(++iotag < psli->iocbq_lookup_len) {
  1261. psli->last_iotag = iotag;
  1262. psli->iocbq_lookup[iotag] = iocbq;
  1263. spin_unlock_irq(&phba->hbalock);
  1264. iocbq->iotag = iotag;
  1265. return iotag;
  1266. } else if (psli->iocbq_lookup_len < (0xffff
  1267. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1268. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1269. spin_unlock_irq(&phba->hbalock);
  1270. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1271. GFP_KERNEL);
  1272. if (new_arr) {
  1273. spin_lock_irq(&phba->hbalock);
  1274. old_arr = psli->iocbq_lookup;
  1275. if (new_len <= psli->iocbq_lookup_len) {
  1276. /* highly unprobable case */
  1277. kfree(new_arr);
  1278. iotag = psli->last_iotag;
  1279. if(++iotag < psli->iocbq_lookup_len) {
  1280. psli->last_iotag = iotag;
  1281. psli->iocbq_lookup[iotag] = iocbq;
  1282. spin_unlock_irq(&phba->hbalock);
  1283. iocbq->iotag = iotag;
  1284. return iotag;
  1285. }
  1286. spin_unlock_irq(&phba->hbalock);
  1287. return 0;
  1288. }
  1289. if (psli->iocbq_lookup)
  1290. memcpy(new_arr, old_arr,
  1291. ((psli->last_iotag + 1) *
  1292. sizeof (struct lpfc_iocbq *)));
  1293. psli->iocbq_lookup = new_arr;
  1294. psli->iocbq_lookup_len = new_len;
  1295. psli->last_iotag = iotag;
  1296. psli->iocbq_lookup[iotag] = iocbq;
  1297. spin_unlock_irq(&phba->hbalock);
  1298. iocbq->iotag = iotag;
  1299. kfree(old_arr);
  1300. return iotag;
  1301. }
  1302. } else
  1303. spin_unlock_irq(&phba->hbalock);
  1304. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1305. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1306. psli->last_iotag);
  1307. return 0;
  1308. }
  1309. /**
  1310. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1311. * @phba: Pointer to HBA context object.
  1312. * @pring: Pointer to driver SLI ring object.
  1313. * @iocb: Pointer to iocb slot in the ring.
  1314. * @nextiocb: Pointer to driver iocb object which need to be
  1315. * posted to firmware.
  1316. *
  1317. * This function is called with hbalock held to post a new iocb to
  1318. * the firmware. This function copies the new iocb to ring iocb slot and
  1319. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1320. * a completion call back for this iocb else the function will free the
  1321. * iocb object.
  1322. **/
  1323. static void
  1324. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1325. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1326. {
  1327. /*
  1328. * Set up an iotag
  1329. */
  1330. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1331. if (pring->ringno == LPFC_ELS_RING) {
  1332. lpfc_debugfs_slow_ring_trc(phba,
  1333. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1334. *(((uint32_t *) &nextiocb->iocb) + 4),
  1335. *(((uint32_t *) &nextiocb->iocb) + 6),
  1336. *(((uint32_t *) &nextiocb->iocb) + 7));
  1337. }
  1338. /*
  1339. * Issue iocb command to adapter
  1340. */
  1341. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1342. wmb();
  1343. pring->stats.iocb_cmd++;
  1344. /*
  1345. * If there is no completion routine to call, we can release the
  1346. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1347. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1348. */
  1349. if (nextiocb->iocb_cmpl)
  1350. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1351. else
  1352. __lpfc_sli_release_iocbq(phba, nextiocb);
  1353. /*
  1354. * Let the HBA know what IOCB slot will be the next one the
  1355. * driver will put a command into.
  1356. */
  1357. pring->cmdidx = pring->next_cmdidx;
  1358. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1359. }
  1360. /**
  1361. * lpfc_sli_update_full_ring - Update the chip attention register
  1362. * @phba: Pointer to HBA context object.
  1363. * @pring: Pointer to driver SLI ring object.
  1364. *
  1365. * The caller is not required to hold any lock for calling this function.
  1366. * This function updates the chip attention bits for the ring to inform firmware
  1367. * that there are pending work to be done for this ring and requests an
  1368. * interrupt when there is space available in the ring. This function is
  1369. * called when the driver is unable to post more iocbs to the ring due
  1370. * to unavailability of space in the ring.
  1371. **/
  1372. static void
  1373. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1374. {
  1375. int ringno = pring->ringno;
  1376. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1377. wmb();
  1378. /*
  1379. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1380. * The HBA will tell us when an IOCB entry is available.
  1381. */
  1382. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1383. readl(phba->CAregaddr); /* flush */
  1384. pring->stats.iocb_cmd_full++;
  1385. }
  1386. /**
  1387. * lpfc_sli_update_ring - Update chip attention register
  1388. * @phba: Pointer to HBA context object.
  1389. * @pring: Pointer to driver SLI ring object.
  1390. *
  1391. * This function updates the chip attention register bit for the
  1392. * given ring to inform HBA that there is more work to be done
  1393. * in this ring. The caller is not required to hold any lock.
  1394. **/
  1395. static void
  1396. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1397. {
  1398. int ringno = pring->ringno;
  1399. /*
  1400. * Tell the HBA that there is work to do in this ring.
  1401. */
  1402. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1403. wmb();
  1404. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1405. readl(phba->CAregaddr); /* flush */
  1406. }
  1407. }
  1408. /**
  1409. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1410. * @phba: Pointer to HBA context object.
  1411. * @pring: Pointer to driver SLI ring object.
  1412. *
  1413. * This function is called with hbalock held to post pending iocbs
  1414. * in the txq to the firmware. This function is called when driver
  1415. * detects space available in the ring.
  1416. **/
  1417. static void
  1418. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1419. {
  1420. IOCB_t *iocb;
  1421. struct lpfc_iocbq *nextiocb;
  1422. /*
  1423. * Check to see if:
  1424. * (a) there is anything on the txq to send
  1425. * (b) link is up
  1426. * (c) link attention events can be processed (fcp ring only)
  1427. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1428. */
  1429. if (pring->txq_cnt &&
  1430. lpfc_is_link_up(phba) &&
  1431. (pring->ringno != phba->sli.fcp_ring ||
  1432. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1433. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1434. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1435. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1436. if (iocb)
  1437. lpfc_sli_update_ring(phba, pring);
  1438. else
  1439. lpfc_sli_update_full_ring(phba, pring);
  1440. }
  1441. return;
  1442. }
  1443. /**
  1444. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1445. * @phba: Pointer to HBA context object.
  1446. * @hbqno: HBQ number.
  1447. *
  1448. * This function is called with hbalock held to get the next
  1449. * available slot for the given HBQ. If there is free slot
  1450. * available for the HBQ it will return pointer to the next available
  1451. * HBQ entry else it will return NULL.
  1452. **/
  1453. static struct lpfc_hbq_entry *
  1454. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1455. {
  1456. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1457. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1458. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1459. hbqp->next_hbqPutIdx = 0;
  1460. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1461. uint32_t raw_index = phba->hbq_get[hbqno];
  1462. uint32_t getidx = le32_to_cpu(raw_index);
  1463. hbqp->local_hbqGetIdx = getidx;
  1464. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1465. lpfc_printf_log(phba, KERN_ERR,
  1466. LOG_SLI | LOG_VPORT,
  1467. "1802 HBQ %d: local_hbqGetIdx "
  1468. "%u is > than hbqp->entry_count %u\n",
  1469. hbqno, hbqp->local_hbqGetIdx,
  1470. hbqp->entry_count);
  1471. phba->link_state = LPFC_HBA_ERROR;
  1472. return NULL;
  1473. }
  1474. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1475. return NULL;
  1476. }
  1477. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1478. hbqp->hbqPutIdx;
  1479. }
  1480. /**
  1481. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1482. * @phba: Pointer to HBA context object.
  1483. *
  1484. * This function is called with no lock held to free all the
  1485. * hbq buffers while uninitializing the SLI interface. It also
  1486. * frees the HBQ buffers returned by the firmware but not yet
  1487. * processed by the upper layers.
  1488. **/
  1489. void
  1490. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1491. {
  1492. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1493. struct hbq_dmabuf *hbq_buf;
  1494. unsigned long flags;
  1495. int i, hbq_count;
  1496. uint32_t hbqno;
  1497. hbq_count = lpfc_sli_hbq_count();
  1498. /* Return all memory used by all HBQs */
  1499. spin_lock_irqsave(&phba->hbalock, flags);
  1500. for (i = 0; i < hbq_count; ++i) {
  1501. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1502. &phba->hbqs[i].hbq_buffer_list, list) {
  1503. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1504. list_del(&hbq_buf->dbuf.list);
  1505. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1506. }
  1507. phba->hbqs[i].buffer_count = 0;
  1508. }
  1509. /* Return all HBQ buffer that are in-fly */
  1510. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1511. list) {
  1512. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1513. list_del(&hbq_buf->dbuf.list);
  1514. if (hbq_buf->tag == -1) {
  1515. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1516. (phba, hbq_buf);
  1517. } else {
  1518. hbqno = hbq_buf->tag >> 16;
  1519. if (hbqno >= LPFC_MAX_HBQS)
  1520. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1521. (phba, hbq_buf);
  1522. else
  1523. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1524. hbq_buf);
  1525. }
  1526. }
  1527. /* Mark the HBQs not in use */
  1528. phba->hbq_in_use = 0;
  1529. spin_unlock_irqrestore(&phba->hbalock, flags);
  1530. }
  1531. /**
  1532. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1533. * @phba: Pointer to HBA context object.
  1534. * @hbqno: HBQ number.
  1535. * @hbq_buf: Pointer to HBQ buffer.
  1536. *
  1537. * This function is called with the hbalock held to post a
  1538. * hbq buffer to the firmware. If the function finds an empty
  1539. * slot in the HBQ, it will post the buffer. The function will return
  1540. * pointer to the hbq entry if it successfully post the buffer
  1541. * else it will return NULL.
  1542. **/
  1543. static int
  1544. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1545. struct hbq_dmabuf *hbq_buf)
  1546. {
  1547. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1548. }
  1549. /**
  1550. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1551. * @phba: Pointer to HBA context object.
  1552. * @hbqno: HBQ number.
  1553. * @hbq_buf: Pointer to HBQ buffer.
  1554. *
  1555. * This function is called with the hbalock held to post a hbq buffer to the
  1556. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1557. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1558. * it successfully post the buffer else it will return an error.
  1559. **/
  1560. static int
  1561. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1562. struct hbq_dmabuf *hbq_buf)
  1563. {
  1564. struct lpfc_hbq_entry *hbqe;
  1565. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1566. /* Get next HBQ entry slot to use */
  1567. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1568. if (hbqe) {
  1569. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1570. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1571. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1572. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1573. hbqe->bde.tus.f.bdeFlags = 0;
  1574. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1575. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1576. /* Sync SLIM */
  1577. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1578. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1579. /* flush */
  1580. readl(phba->hbq_put + hbqno);
  1581. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1582. return 0;
  1583. } else
  1584. return -ENOMEM;
  1585. }
  1586. /**
  1587. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1588. * @phba: Pointer to HBA context object.
  1589. * @hbqno: HBQ number.
  1590. * @hbq_buf: Pointer to HBQ buffer.
  1591. *
  1592. * This function is called with the hbalock held to post an RQE to the SLI4
  1593. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1594. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1595. **/
  1596. static int
  1597. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1598. struct hbq_dmabuf *hbq_buf)
  1599. {
  1600. int rc;
  1601. struct lpfc_rqe hrqe;
  1602. struct lpfc_rqe drqe;
  1603. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1604. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1605. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1606. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1607. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1608. &hrqe, &drqe);
  1609. if (rc < 0)
  1610. return rc;
  1611. hbq_buf->tag = rc;
  1612. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1613. return 0;
  1614. }
  1615. /* HBQ for ELS and CT traffic. */
  1616. static struct lpfc_hbq_init lpfc_els_hbq = {
  1617. .rn = 1,
  1618. .entry_count = 256,
  1619. .mask_count = 0,
  1620. .profile = 0,
  1621. .ring_mask = (1 << LPFC_ELS_RING),
  1622. .buffer_count = 0,
  1623. .init_count = 40,
  1624. .add_count = 40,
  1625. };
  1626. /* HBQ for the extra ring if needed */
  1627. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1628. .rn = 1,
  1629. .entry_count = 200,
  1630. .mask_count = 0,
  1631. .profile = 0,
  1632. .ring_mask = (1 << LPFC_EXTRA_RING),
  1633. .buffer_count = 0,
  1634. .init_count = 0,
  1635. .add_count = 5,
  1636. };
  1637. /* Array of HBQs */
  1638. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1639. &lpfc_els_hbq,
  1640. &lpfc_extra_hbq,
  1641. };
  1642. /**
  1643. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1644. * @phba: Pointer to HBA context object.
  1645. * @hbqno: HBQ number.
  1646. * @count: Number of HBQ buffers to be posted.
  1647. *
  1648. * This function is called with no lock held to post more hbq buffers to the
  1649. * given HBQ. The function returns the number of HBQ buffers successfully
  1650. * posted.
  1651. **/
  1652. static int
  1653. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1654. {
  1655. uint32_t i, posted = 0;
  1656. unsigned long flags;
  1657. struct hbq_dmabuf *hbq_buffer;
  1658. LIST_HEAD(hbq_buf_list);
  1659. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1660. return 0;
  1661. if ((phba->hbqs[hbqno].buffer_count + count) >
  1662. lpfc_hbq_defs[hbqno]->entry_count)
  1663. count = lpfc_hbq_defs[hbqno]->entry_count -
  1664. phba->hbqs[hbqno].buffer_count;
  1665. if (!count)
  1666. return 0;
  1667. /* Allocate HBQ entries */
  1668. for (i = 0; i < count; i++) {
  1669. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1670. if (!hbq_buffer)
  1671. break;
  1672. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1673. }
  1674. /* Check whether HBQ is still in use */
  1675. spin_lock_irqsave(&phba->hbalock, flags);
  1676. if (!phba->hbq_in_use)
  1677. goto err;
  1678. while (!list_empty(&hbq_buf_list)) {
  1679. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1680. dbuf.list);
  1681. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1682. (hbqno << 16));
  1683. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1684. phba->hbqs[hbqno].buffer_count++;
  1685. posted++;
  1686. } else
  1687. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1688. }
  1689. spin_unlock_irqrestore(&phba->hbalock, flags);
  1690. return posted;
  1691. err:
  1692. spin_unlock_irqrestore(&phba->hbalock, flags);
  1693. while (!list_empty(&hbq_buf_list)) {
  1694. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1695. dbuf.list);
  1696. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1697. }
  1698. return 0;
  1699. }
  1700. /**
  1701. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1702. * @phba: Pointer to HBA context object.
  1703. * @qno: HBQ number.
  1704. *
  1705. * This function posts more buffers to the HBQ. This function
  1706. * is called with no lock held. The function returns the number of HBQ entries
  1707. * successfully allocated.
  1708. **/
  1709. int
  1710. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1711. {
  1712. if (phba->sli_rev == LPFC_SLI_REV4)
  1713. return 0;
  1714. else
  1715. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1716. lpfc_hbq_defs[qno]->add_count);
  1717. }
  1718. /**
  1719. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1720. * @phba: Pointer to HBA context object.
  1721. * @qno: HBQ queue number.
  1722. *
  1723. * This function is called from SLI initialization code path with
  1724. * no lock held to post initial HBQ buffers to firmware. The
  1725. * function returns the number of HBQ entries successfully allocated.
  1726. **/
  1727. static int
  1728. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1729. {
  1730. if (phba->sli_rev == LPFC_SLI_REV4)
  1731. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1732. lpfc_hbq_defs[qno]->entry_count);
  1733. else
  1734. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1735. lpfc_hbq_defs[qno]->init_count);
  1736. }
  1737. /**
  1738. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1739. * @phba: Pointer to HBA context object.
  1740. * @hbqno: HBQ number.
  1741. *
  1742. * This function removes the first hbq buffer on an hbq list and returns a
  1743. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1744. **/
  1745. static struct hbq_dmabuf *
  1746. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1747. {
  1748. struct lpfc_dmabuf *d_buf;
  1749. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1750. if (!d_buf)
  1751. return NULL;
  1752. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1753. }
  1754. /**
  1755. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1756. * @phba: Pointer to HBA context object.
  1757. * @tag: Tag of the hbq buffer.
  1758. *
  1759. * This function is called with hbalock held. This function searches
  1760. * for the hbq buffer associated with the given tag in the hbq buffer
  1761. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1762. * it returns NULL.
  1763. **/
  1764. static struct hbq_dmabuf *
  1765. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1766. {
  1767. struct lpfc_dmabuf *d_buf;
  1768. struct hbq_dmabuf *hbq_buf;
  1769. uint32_t hbqno;
  1770. hbqno = tag >> 16;
  1771. if (hbqno >= LPFC_MAX_HBQS)
  1772. return NULL;
  1773. spin_lock_irq(&phba->hbalock);
  1774. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1775. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1776. if (hbq_buf->tag == tag) {
  1777. spin_unlock_irq(&phba->hbalock);
  1778. return hbq_buf;
  1779. }
  1780. }
  1781. spin_unlock_irq(&phba->hbalock);
  1782. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1783. "1803 Bad hbq tag. Data: x%x x%x\n",
  1784. tag, phba->hbqs[tag >> 16].buffer_count);
  1785. return NULL;
  1786. }
  1787. /**
  1788. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1789. * @phba: Pointer to HBA context object.
  1790. * @hbq_buffer: Pointer to HBQ buffer.
  1791. *
  1792. * This function is called with hbalock. This function gives back
  1793. * the hbq buffer to firmware. If the HBQ does not have space to
  1794. * post the buffer, it will free the buffer.
  1795. **/
  1796. void
  1797. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1798. {
  1799. uint32_t hbqno;
  1800. if (hbq_buffer) {
  1801. hbqno = hbq_buffer->tag >> 16;
  1802. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1803. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1804. }
  1805. }
  1806. /**
  1807. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1808. * @mbxCommand: mailbox command code.
  1809. *
  1810. * This function is called by the mailbox event handler function to verify
  1811. * that the completed mailbox command is a legitimate mailbox command. If the
  1812. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1813. * and the mailbox event handler will take the HBA offline.
  1814. **/
  1815. static int
  1816. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1817. {
  1818. uint8_t ret;
  1819. switch (mbxCommand) {
  1820. case MBX_LOAD_SM:
  1821. case MBX_READ_NV:
  1822. case MBX_WRITE_NV:
  1823. case MBX_WRITE_VPARMS:
  1824. case MBX_RUN_BIU_DIAG:
  1825. case MBX_INIT_LINK:
  1826. case MBX_DOWN_LINK:
  1827. case MBX_CONFIG_LINK:
  1828. case MBX_CONFIG_RING:
  1829. case MBX_RESET_RING:
  1830. case MBX_READ_CONFIG:
  1831. case MBX_READ_RCONFIG:
  1832. case MBX_READ_SPARM:
  1833. case MBX_READ_STATUS:
  1834. case MBX_READ_RPI:
  1835. case MBX_READ_XRI:
  1836. case MBX_READ_REV:
  1837. case MBX_READ_LNK_STAT:
  1838. case MBX_REG_LOGIN:
  1839. case MBX_UNREG_LOGIN:
  1840. case MBX_CLEAR_LA:
  1841. case MBX_DUMP_MEMORY:
  1842. case MBX_DUMP_CONTEXT:
  1843. case MBX_RUN_DIAGS:
  1844. case MBX_RESTART:
  1845. case MBX_UPDATE_CFG:
  1846. case MBX_DOWN_LOAD:
  1847. case MBX_DEL_LD_ENTRY:
  1848. case MBX_RUN_PROGRAM:
  1849. case MBX_SET_MASK:
  1850. case MBX_SET_VARIABLE:
  1851. case MBX_UNREG_D_ID:
  1852. case MBX_KILL_BOARD:
  1853. case MBX_CONFIG_FARP:
  1854. case MBX_BEACON:
  1855. case MBX_LOAD_AREA:
  1856. case MBX_RUN_BIU_DIAG64:
  1857. case MBX_CONFIG_PORT:
  1858. case MBX_READ_SPARM64:
  1859. case MBX_READ_RPI64:
  1860. case MBX_REG_LOGIN64:
  1861. case MBX_READ_TOPOLOGY:
  1862. case MBX_WRITE_WWN:
  1863. case MBX_SET_DEBUG:
  1864. case MBX_LOAD_EXP_ROM:
  1865. case MBX_ASYNCEVT_ENABLE:
  1866. case MBX_REG_VPI:
  1867. case MBX_UNREG_VPI:
  1868. case MBX_HEARTBEAT:
  1869. case MBX_PORT_CAPABILITIES:
  1870. case MBX_PORT_IOV_CONTROL:
  1871. case MBX_SLI4_CONFIG:
  1872. case MBX_SLI4_REQ_FTRS:
  1873. case MBX_REG_FCFI:
  1874. case MBX_UNREG_FCFI:
  1875. case MBX_REG_VFI:
  1876. case MBX_UNREG_VFI:
  1877. case MBX_INIT_VPI:
  1878. case MBX_INIT_VFI:
  1879. case MBX_RESUME_RPI:
  1880. case MBX_READ_EVENT_LOG_STATUS:
  1881. case MBX_READ_EVENT_LOG:
  1882. case MBX_SECURITY_MGMT:
  1883. case MBX_AUTH_PORT:
  1884. ret = mbxCommand;
  1885. break;
  1886. default:
  1887. ret = MBX_SHUTDOWN;
  1888. break;
  1889. }
  1890. return ret;
  1891. }
  1892. /**
  1893. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1894. * @phba: Pointer to HBA context object.
  1895. * @pmboxq: Pointer to mailbox command.
  1896. *
  1897. * This is completion handler function for mailbox commands issued from
  1898. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1899. * mailbox event handler function with no lock held. This function
  1900. * will wake up thread waiting on the wait queue pointed by context1
  1901. * of the mailbox.
  1902. **/
  1903. void
  1904. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1905. {
  1906. wait_queue_head_t *pdone_q;
  1907. unsigned long drvr_flag;
  1908. /*
  1909. * If pdone_q is empty, the driver thread gave up waiting and
  1910. * continued running.
  1911. */
  1912. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1913. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1914. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1915. if (pdone_q)
  1916. wake_up_interruptible(pdone_q);
  1917. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1918. return;
  1919. }
  1920. /**
  1921. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1922. * @phba: Pointer to HBA context object.
  1923. * @pmb: Pointer to mailbox object.
  1924. *
  1925. * This function is the default mailbox completion handler. It
  1926. * frees the memory resources associated with the completed mailbox
  1927. * command. If the completed command is a REG_LOGIN mailbox command,
  1928. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1929. **/
  1930. void
  1931. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1932. {
  1933. struct lpfc_vport *vport = pmb->vport;
  1934. struct lpfc_dmabuf *mp;
  1935. struct lpfc_nodelist *ndlp;
  1936. struct Scsi_Host *shost;
  1937. uint16_t rpi, vpi;
  1938. int rc;
  1939. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1940. if (mp) {
  1941. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1942. kfree(mp);
  1943. }
  1944. /*
  1945. * If a REG_LOGIN succeeded after node is destroyed or node
  1946. * is in re-discovery driver need to cleanup the RPI.
  1947. */
  1948. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1949. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1950. !pmb->u.mb.mbxStatus) {
  1951. rpi = pmb->u.mb.un.varWords[0];
  1952. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1953. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1954. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1955. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1956. if (rc != MBX_NOT_FINISHED)
  1957. return;
  1958. }
  1959. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  1960. !(phba->pport->load_flag & FC_UNLOADING) &&
  1961. !pmb->u.mb.mbxStatus) {
  1962. shost = lpfc_shost_from_vport(vport);
  1963. spin_lock_irq(shost->host_lock);
  1964. vport->vpi_state |= LPFC_VPI_REGISTERED;
  1965. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  1966. spin_unlock_irq(shost->host_lock);
  1967. }
  1968. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  1969. ndlp = (struct lpfc_nodelist *)pmb->context2;
  1970. lpfc_nlp_put(ndlp);
  1971. pmb->context2 = NULL;
  1972. }
  1973. /* Check security permission status on INIT_LINK mailbox command */
  1974. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  1975. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  1976. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1977. "2860 SLI authentication is required "
  1978. "for INIT_LINK but has not done yet\n");
  1979. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  1980. lpfc_sli4_mbox_cmd_free(phba, pmb);
  1981. else
  1982. mempool_free(pmb, phba->mbox_mem_pool);
  1983. }
  1984. /**
  1985. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  1986. * @phba: Pointer to HBA context object.
  1987. *
  1988. * This function is called with no lock held. This function processes all
  1989. * the completed mailbox commands and gives it to upper layers. The interrupt
  1990. * service routine processes mailbox completion interrupt and adds completed
  1991. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  1992. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  1993. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  1994. * function returns the mailbox commands to the upper layer by calling the
  1995. * completion handler function of each mailbox.
  1996. **/
  1997. int
  1998. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  1999. {
  2000. MAILBOX_t *pmbox;
  2001. LPFC_MBOXQ_t *pmb;
  2002. int rc;
  2003. LIST_HEAD(cmplq);
  2004. phba->sli.slistat.mbox_event++;
  2005. /* Get all completed mailboxe buffers into the cmplq */
  2006. spin_lock_irq(&phba->hbalock);
  2007. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2008. spin_unlock_irq(&phba->hbalock);
  2009. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2010. do {
  2011. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2012. if (pmb == NULL)
  2013. break;
  2014. pmbox = &pmb->u.mb;
  2015. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2016. if (pmb->vport) {
  2017. lpfc_debugfs_disc_trc(pmb->vport,
  2018. LPFC_DISC_TRC_MBOX_VPORT,
  2019. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2020. (uint32_t)pmbox->mbxCommand,
  2021. pmbox->un.varWords[0],
  2022. pmbox->un.varWords[1]);
  2023. }
  2024. else {
  2025. lpfc_debugfs_disc_trc(phba->pport,
  2026. LPFC_DISC_TRC_MBOX,
  2027. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2028. (uint32_t)pmbox->mbxCommand,
  2029. pmbox->un.varWords[0],
  2030. pmbox->un.varWords[1]);
  2031. }
  2032. }
  2033. /*
  2034. * It is a fatal error if unknown mbox command completion.
  2035. */
  2036. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2037. MBX_SHUTDOWN) {
  2038. /* Unknown mailbox command compl */
  2039. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2040. "(%d):0323 Unknown Mailbox command "
  2041. "x%x (x%x/x%x) Cmpl\n",
  2042. pmb->vport ? pmb->vport->vpi : 0,
  2043. pmbox->mbxCommand,
  2044. lpfc_sli_config_mbox_subsys_get(phba,
  2045. pmb),
  2046. lpfc_sli_config_mbox_opcode_get(phba,
  2047. pmb));
  2048. phba->link_state = LPFC_HBA_ERROR;
  2049. phba->work_hs = HS_FFER3;
  2050. lpfc_handle_eratt(phba);
  2051. continue;
  2052. }
  2053. if (pmbox->mbxStatus) {
  2054. phba->sli.slistat.mbox_stat_err++;
  2055. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2056. /* Mbox cmd cmpl error - RETRYing */
  2057. lpfc_printf_log(phba, KERN_INFO,
  2058. LOG_MBOX | LOG_SLI,
  2059. "(%d):0305 Mbox cmd cmpl "
  2060. "error - RETRYing Data: x%x "
  2061. "(x%x/x%x) x%x x%x x%x\n",
  2062. pmb->vport ? pmb->vport->vpi : 0,
  2063. pmbox->mbxCommand,
  2064. lpfc_sli_config_mbox_subsys_get(phba,
  2065. pmb),
  2066. lpfc_sli_config_mbox_opcode_get(phba,
  2067. pmb),
  2068. pmbox->mbxStatus,
  2069. pmbox->un.varWords[0],
  2070. pmb->vport->port_state);
  2071. pmbox->mbxStatus = 0;
  2072. pmbox->mbxOwner = OWN_HOST;
  2073. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2074. if (rc != MBX_NOT_FINISHED)
  2075. continue;
  2076. }
  2077. }
  2078. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2079. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2080. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2081. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2082. pmb->vport ? pmb->vport->vpi : 0,
  2083. pmbox->mbxCommand,
  2084. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2085. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2086. pmb->mbox_cmpl,
  2087. *((uint32_t *) pmbox),
  2088. pmbox->un.varWords[0],
  2089. pmbox->un.varWords[1],
  2090. pmbox->un.varWords[2],
  2091. pmbox->un.varWords[3],
  2092. pmbox->un.varWords[4],
  2093. pmbox->un.varWords[5],
  2094. pmbox->un.varWords[6],
  2095. pmbox->un.varWords[7]);
  2096. if (pmb->mbox_cmpl)
  2097. pmb->mbox_cmpl(phba,pmb);
  2098. } while (1);
  2099. return 0;
  2100. }
  2101. /**
  2102. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2103. * @phba: Pointer to HBA context object.
  2104. * @pring: Pointer to driver SLI ring object.
  2105. * @tag: buffer tag.
  2106. *
  2107. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2108. * is set in the tag the buffer is posted for a particular exchange,
  2109. * the function will return the buffer without replacing the buffer.
  2110. * If the buffer is for unsolicited ELS or CT traffic, this function
  2111. * returns the buffer and also posts another buffer to the firmware.
  2112. **/
  2113. static struct lpfc_dmabuf *
  2114. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2115. struct lpfc_sli_ring *pring,
  2116. uint32_t tag)
  2117. {
  2118. struct hbq_dmabuf *hbq_entry;
  2119. if (tag & QUE_BUFTAG_BIT)
  2120. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2121. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2122. if (!hbq_entry)
  2123. return NULL;
  2124. return &hbq_entry->dbuf;
  2125. }
  2126. /**
  2127. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2128. * @phba: Pointer to HBA context object.
  2129. * @pring: Pointer to driver SLI ring object.
  2130. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2131. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2132. * @fch_type: the type for the first frame of the sequence.
  2133. *
  2134. * This function is called with no lock held. This function uses the r_ctl and
  2135. * type of the received sequence to find the correct callback function to call
  2136. * to process the sequence.
  2137. **/
  2138. static int
  2139. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2140. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2141. uint32_t fch_type)
  2142. {
  2143. int i;
  2144. /* unSolicited Responses */
  2145. if (pring->prt[0].profile) {
  2146. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2147. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2148. saveq);
  2149. return 1;
  2150. }
  2151. /* We must search, based on rctl / type
  2152. for the right routine */
  2153. for (i = 0; i < pring->num_mask; i++) {
  2154. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2155. (pring->prt[i].type == fch_type)) {
  2156. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2157. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2158. (phba, pring, saveq);
  2159. return 1;
  2160. }
  2161. }
  2162. return 0;
  2163. }
  2164. /**
  2165. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2166. * @phba: Pointer to HBA context object.
  2167. * @pring: Pointer to driver SLI ring object.
  2168. * @saveq: Pointer to the unsolicited iocb.
  2169. *
  2170. * This function is called with no lock held by the ring event handler
  2171. * when there is an unsolicited iocb posted to the response ring by the
  2172. * firmware. This function gets the buffer associated with the iocbs
  2173. * and calls the event handler for the ring. This function handles both
  2174. * qring buffers and hbq buffers.
  2175. * When the function returns 1 the caller can free the iocb object otherwise
  2176. * upper layer functions will free the iocb objects.
  2177. **/
  2178. static int
  2179. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2180. struct lpfc_iocbq *saveq)
  2181. {
  2182. IOCB_t * irsp;
  2183. WORD5 * w5p;
  2184. uint32_t Rctl, Type;
  2185. uint32_t match;
  2186. struct lpfc_iocbq *iocbq;
  2187. struct lpfc_dmabuf *dmzbuf;
  2188. match = 0;
  2189. irsp = &(saveq->iocb);
  2190. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2191. if (pring->lpfc_sli_rcv_async_status)
  2192. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2193. else
  2194. lpfc_printf_log(phba,
  2195. KERN_WARNING,
  2196. LOG_SLI,
  2197. "0316 Ring %d handler: unexpected "
  2198. "ASYNC_STATUS iocb received evt_code "
  2199. "0x%x\n",
  2200. pring->ringno,
  2201. irsp->un.asyncstat.evt_code);
  2202. return 1;
  2203. }
  2204. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2205. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2206. if (irsp->ulpBdeCount > 0) {
  2207. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2208. irsp->un.ulpWord[3]);
  2209. lpfc_in_buf_free(phba, dmzbuf);
  2210. }
  2211. if (irsp->ulpBdeCount > 1) {
  2212. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2213. irsp->unsli3.sli3Words[3]);
  2214. lpfc_in_buf_free(phba, dmzbuf);
  2215. }
  2216. if (irsp->ulpBdeCount > 2) {
  2217. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2218. irsp->unsli3.sli3Words[7]);
  2219. lpfc_in_buf_free(phba, dmzbuf);
  2220. }
  2221. return 1;
  2222. }
  2223. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2224. if (irsp->ulpBdeCount != 0) {
  2225. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2226. irsp->un.ulpWord[3]);
  2227. if (!saveq->context2)
  2228. lpfc_printf_log(phba,
  2229. KERN_ERR,
  2230. LOG_SLI,
  2231. "0341 Ring %d Cannot find buffer for "
  2232. "an unsolicited iocb. tag 0x%x\n",
  2233. pring->ringno,
  2234. irsp->un.ulpWord[3]);
  2235. }
  2236. if (irsp->ulpBdeCount == 2) {
  2237. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2238. irsp->unsli3.sli3Words[7]);
  2239. if (!saveq->context3)
  2240. lpfc_printf_log(phba,
  2241. KERN_ERR,
  2242. LOG_SLI,
  2243. "0342 Ring %d Cannot find buffer for an"
  2244. " unsolicited iocb. tag 0x%x\n",
  2245. pring->ringno,
  2246. irsp->unsli3.sli3Words[7]);
  2247. }
  2248. list_for_each_entry(iocbq, &saveq->list, list) {
  2249. irsp = &(iocbq->iocb);
  2250. if (irsp->ulpBdeCount != 0) {
  2251. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2252. irsp->un.ulpWord[3]);
  2253. if (!iocbq->context2)
  2254. lpfc_printf_log(phba,
  2255. KERN_ERR,
  2256. LOG_SLI,
  2257. "0343 Ring %d Cannot find "
  2258. "buffer for an unsolicited iocb"
  2259. ". tag 0x%x\n", pring->ringno,
  2260. irsp->un.ulpWord[3]);
  2261. }
  2262. if (irsp->ulpBdeCount == 2) {
  2263. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2264. irsp->unsli3.sli3Words[7]);
  2265. if (!iocbq->context3)
  2266. lpfc_printf_log(phba,
  2267. KERN_ERR,
  2268. LOG_SLI,
  2269. "0344 Ring %d Cannot find "
  2270. "buffer for an unsolicited "
  2271. "iocb. tag 0x%x\n",
  2272. pring->ringno,
  2273. irsp->unsli3.sli3Words[7]);
  2274. }
  2275. }
  2276. }
  2277. if (irsp->ulpBdeCount != 0 &&
  2278. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2279. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2280. int found = 0;
  2281. /* search continue save q for same XRI */
  2282. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2283. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2284. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2285. list_add_tail(&saveq->list, &iocbq->list);
  2286. found = 1;
  2287. break;
  2288. }
  2289. }
  2290. if (!found)
  2291. list_add_tail(&saveq->clist,
  2292. &pring->iocb_continue_saveq);
  2293. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2294. list_del_init(&iocbq->clist);
  2295. saveq = iocbq;
  2296. irsp = &(saveq->iocb);
  2297. } else
  2298. return 0;
  2299. }
  2300. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2301. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2302. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2303. Rctl = FC_RCTL_ELS_REQ;
  2304. Type = FC_TYPE_ELS;
  2305. } else {
  2306. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2307. Rctl = w5p->hcsw.Rctl;
  2308. Type = w5p->hcsw.Type;
  2309. /* Firmware Workaround */
  2310. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2311. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2312. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2313. Rctl = FC_RCTL_ELS_REQ;
  2314. Type = FC_TYPE_ELS;
  2315. w5p->hcsw.Rctl = Rctl;
  2316. w5p->hcsw.Type = Type;
  2317. }
  2318. }
  2319. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2320. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2321. "0313 Ring %d handler: unexpected Rctl x%x "
  2322. "Type x%x received\n",
  2323. pring->ringno, Rctl, Type);
  2324. return 1;
  2325. }
  2326. /**
  2327. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2328. * @phba: Pointer to HBA context object.
  2329. * @pring: Pointer to driver SLI ring object.
  2330. * @prspiocb: Pointer to response iocb object.
  2331. *
  2332. * This function looks up the iocb_lookup table to get the command iocb
  2333. * corresponding to the given response iocb using the iotag of the
  2334. * response iocb. This function is called with the hbalock held.
  2335. * This function returns the command iocb object if it finds the command
  2336. * iocb else returns NULL.
  2337. **/
  2338. static struct lpfc_iocbq *
  2339. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2340. struct lpfc_sli_ring *pring,
  2341. struct lpfc_iocbq *prspiocb)
  2342. {
  2343. struct lpfc_iocbq *cmd_iocb = NULL;
  2344. uint16_t iotag;
  2345. iotag = prspiocb->iocb.ulpIoTag;
  2346. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2347. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2348. list_del_init(&cmd_iocb->list);
  2349. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2350. pring->txcmplq_cnt--;
  2351. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2352. }
  2353. return cmd_iocb;
  2354. }
  2355. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2356. "0317 iotag x%x is out off "
  2357. "range: max iotag x%x wd0 x%x\n",
  2358. iotag, phba->sli.last_iotag,
  2359. *(((uint32_t *) &prspiocb->iocb) + 7));
  2360. return NULL;
  2361. }
  2362. /**
  2363. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2364. * @phba: Pointer to HBA context object.
  2365. * @pring: Pointer to driver SLI ring object.
  2366. * @iotag: IOCB tag.
  2367. *
  2368. * This function looks up the iocb_lookup table to get the command iocb
  2369. * corresponding to the given iotag. This function is called with the
  2370. * hbalock held.
  2371. * This function returns the command iocb object if it finds the command
  2372. * iocb else returns NULL.
  2373. **/
  2374. static struct lpfc_iocbq *
  2375. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2376. struct lpfc_sli_ring *pring, uint16_t iotag)
  2377. {
  2378. struct lpfc_iocbq *cmd_iocb;
  2379. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2380. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2381. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2382. /* remove from txcmpl queue list */
  2383. list_del_init(&cmd_iocb->list);
  2384. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2385. pring->txcmplq_cnt--;
  2386. return cmd_iocb;
  2387. }
  2388. }
  2389. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2390. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2391. iotag, phba->sli.last_iotag);
  2392. return NULL;
  2393. }
  2394. /**
  2395. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2396. * @phba: Pointer to HBA context object.
  2397. * @pring: Pointer to driver SLI ring object.
  2398. * @saveq: Pointer to the response iocb to be processed.
  2399. *
  2400. * This function is called by the ring event handler for non-fcp
  2401. * rings when there is a new response iocb in the response ring.
  2402. * The caller is not required to hold any locks. This function
  2403. * gets the command iocb associated with the response iocb and
  2404. * calls the completion handler for the command iocb. If there
  2405. * is no completion handler, the function will free the resources
  2406. * associated with command iocb. If the response iocb is for
  2407. * an already aborted command iocb, the status of the completion
  2408. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2409. * This function always returns 1.
  2410. **/
  2411. static int
  2412. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2413. struct lpfc_iocbq *saveq)
  2414. {
  2415. struct lpfc_iocbq *cmdiocbp;
  2416. int rc = 1;
  2417. unsigned long iflag;
  2418. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2419. spin_lock_irqsave(&phba->hbalock, iflag);
  2420. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2421. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2422. if (cmdiocbp) {
  2423. if (cmdiocbp->iocb_cmpl) {
  2424. /*
  2425. * If an ELS command failed send an event to mgmt
  2426. * application.
  2427. */
  2428. if (saveq->iocb.ulpStatus &&
  2429. (pring->ringno == LPFC_ELS_RING) &&
  2430. (cmdiocbp->iocb.ulpCommand ==
  2431. CMD_ELS_REQUEST64_CR))
  2432. lpfc_send_els_failure_event(phba,
  2433. cmdiocbp, saveq);
  2434. /*
  2435. * Post all ELS completions to the worker thread.
  2436. * All other are passed to the completion callback.
  2437. */
  2438. if (pring->ringno == LPFC_ELS_RING) {
  2439. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2440. (cmdiocbp->iocb_flag &
  2441. LPFC_DRIVER_ABORTED)) {
  2442. spin_lock_irqsave(&phba->hbalock,
  2443. iflag);
  2444. cmdiocbp->iocb_flag &=
  2445. ~LPFC_DRIVER_ABORTED;
  2446. spin_unlock_irqrestore(&phba->hbalock,
  2447. iflag);
  2448. saveq->iocb.ulpStatus =
  2449. IOSTAT_LOCAL_REJECT;
  2450. saveq->iocb.un.ulpWord[4] =
  2451. IOERR_SLI_ABORTED;
  2452. /* Firmware could still be in progress
  2453. * of DMAing payload, so don't free data
  2454. * buffer till after a hbeat.
  2455. */
  2456. spin_lock_irqsave(&phba->hbalock,
  2457. iflag);
  2458. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2459. spin_unlock_irqrestore(&phba->hbalock,
  2460. iflag);
  2461. }
  2462. if (phba->sli_rev == LPFC_SLI_REV4) {
  2463. if (saveq->iocb_flag &
  2464. LPFC_EXCHANGE_BUSY) {
  2465. /* Set cmdiocb flag for the
  2466. * exchange busy so sgl (xri)
  2467. * will not be released until
  2468. * the abort xri is received
  2469. * from hba.
  2470. */
  2471. spin_lock_irqsave(
  2472. &phba->hbalock, iflag);
  2473. cmdiocbp->iocb_flag |=
  2474. LPFC_EXCHANGE_BUSY;
  2475. spin_unlock_irqrestore(
  2476. &phba->hbalock, iflag);
  2477. }
  2478. if (cmdiocbp->iocb_flag &
  2479. LPFC_DRIVER_ABORTED) {
  2480. /*
  2481. * Clear LPFC_DRIVER_ABORTED
  2482. * bit in case it was driver
  2483. * initiated abort.
  2484. */
  2485. spin_lock_irqsave(
  2486. &phba->hbalock, iflag);
  2487. cmdiocbp->iocb_flag &=
  2488. ~LPFC_DRIVER_ABORTED;
  2489. spin_unlock_irqrestore(
  2490. &phba->hbalock, iflag);
  2491. cmdiocbp->iocb.ulpStatus =
  2492. IOSTAT_LOCAL_REJECT;
  2493. cmdiocbp->iocb.un.ulpWord[4] =
  2494. IOERR_ABORT_REQUESTED;
  2495. /*
  2496. * For SLI4, irsiocb contains
  2497. * NO_XRI in sli_xritag, it
  2498. * shall not affect releasing
  2499. * sgl (xri) process.
  2500. */
  2501. saveq->iocb.ulpStatus =
  2502. IOSTAT_LOCAL_REJECT;
  2503. saveq->iocb.un.ulpWord[4] =
  2504. IOERR_SLI_ABORTED;
  2505. spin_lock_irqsave(
  2506. &phba->hbalock, iflag);
  2507. saveq->iocb_flag |=
  2508. LPFC_DELAY_MEM_FREE;
  2509. spin_unlock_irqrestore(
  2510. &phba->hbalock, iflag);
  2511. }
  2512. }
  2513. }
  2514. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2515. } else
  2516. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2517. } else {
  2518. /*
  2519. * Unknown initiating command based on the response iotag.
  2520. * This could be the case on the ELS ring because of
  2521. * lpfc_els_abort().
  2522. */
  2523. if (pring->ringno != LPFC_ELS_RING) {
  2524. /*
  2525. * Ring <ringno> handler: unexpected completion IoTag
  2526. * <IoTag>
  2527. */
  2528. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2529. "0322 Ring %d handler: "
  2530. "unexpected completion IoTag x%x "
  2531. "Data: x%x x%x x%x x%x\n",
  2532. pring->ringno,
  2533. saveq->iocb.ulpIoTag,
  2534. saveq->iocb.ulpStatus,
  2535. saveq->iocb.un.ulpWord[4],
  2536. saveq->iocb.ulpCommand,
  2537. saveq->iocb.ulpContext);
  2538. }
  2539. }
  2540. return rc;
  2541. }
  2542. /**
  2543. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2544. * @phba: Pointer to HBA context object.
  2545. * @pring: Pointer to driver SLI ring object.
  2546. *
  2547. * This function is called from the iocb ring event handlers when
  2548. * put pointer is ahead of the get pointer for a ring. This function signal
  2549. * an error attention condition to the worker thread and the worker
  2550. * thread will transition the HBA to offline state.
  2551. **/
  2552. static void
  2553. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2554. {
  2555. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2556. /*
  2557. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2558. * rsp ring <portRspMax>
  2559. */
  2560. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2561. "0312 Ring %d handler: portRspPut %d "
  2562. "is bigger than rsp ring %d\n",
  2563. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2564. pring->numRiocb);
  2565. phba->link_state = LPFC_HBA_ERROR;
  2566. /*
  2567. * All error attention handlers are posted to
  2568. * worker thread
  2569. */
  2570. phba->work_ha |= HA_ERATT;
  2571. phba->work_hs = HS_FFER3;
  2572. lpfc_worker_wake_up(phba);
  2573. return;
  2574. }
  2575. /**
  2576. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2577. * @ptr: Pointer to address of HBA context object.
  2578. *
  2579. * This function is invoked by the Error Attention polling timer when the
  2580. * timer times out. It will check the SLI Error Attention register for
  2581. * possible attention events. If so, it will post an Error Attention event
  2582. * and wake up worker thread to process it. Otherwise, it will set up the
  2583. * Error Attention polling timer for the next poll.
  2584. **/
  2585. void lpfc_poll_eratt(unsigned long ptr)
  2586. {
  2587. struct lpfc_hba *phba;
  2588. uint32_t eratt = 0;
  2589. phba = (struct lpfc_hba *)ptr;
  2590. /* Check chip HA register for error event */
  2591. eratt = lpfc_sli_check_eratt(phba);
  2592. if (eratt)
  2593. /* Tell the worker thread there is work to do */
  2594. lpfc_worker_wake_up(phba);
  2595. else
  2596. /* Restart the timer for next eratt poll */
  2597. mod_timer(&phba->eratt_poll, jiffies +
  2598. HZ * LPFC_ERATT_POLL_INTERVAL);
  2599. return;
  2600. }
  2601. /**
  2602. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2603. * @phba: Pointer to HBA context object.
  2604. * @pring: Pointer to driver SLI ring object.
  2605. * @mask: Host attention register mask for this ring.
  2606. *
  2607. * This function is called from the interrupt context when there is a ring
  2608. * event for the fcp ring. The caller does not hold any lock.
  2609. * The function processes each response iocb in the response ring until it
  2610. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2611. * LE bit set. The function will call the completion handler of the command iocb
  2612. * if the response iocb indicates a completion for a command iocb or it is
  2613. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2614. * function if this is an unsolicited iocb.
  2615. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2616. * to check it explicitly.
  2617. */
  2618. int
  2619. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2620. struct lpfc_sli_ring *pring, uint32_t mask)
  2621. {
  2622. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2623. IOCB_t *irsp = NULL;
  2624. IOCB_t *entry = NULL;
  2625. struct lpfc_iocbq *cmdiocbq = NULL;
  2626. struct lpfc_iocbq rspiocbq;
  2627. uint32_t status;
  2628. uint32_t portRspPut, portRspMax;
  2629. int rc = 1;
  2630. lpfc_iocb_type type;
  2631. unsigned long iflag;
  2632. uint32_t rsp_cmpl = 0;
  2633. spin_lock_irqsave(&phba->hbalock, iflag);
  2634. pring->stats.iocb_event++;
  2635. /*
  2636. * The next available response entry should never exceed the maximum
  2637. * entries. If it does, treat it as an adapter hardware error.
  2638. */
  2639. portRspMax = pring->numRiocb;
  2640. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2641. if (unlikely(portRspPut >= portRspMax)) {
  2642. lpfc_sli_rsp_pointers_error(phba, pring);
  2643. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2644. return 1;
  2645. }
  2646. if (phba->fcp_ring_in_use) {
  2647. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2648. return 1;
  2649. } else
  2650. phba->fcp_ring_in_use = 1;
  2651. rmb();
  2652. while (pring->rspidx != portRspPut) {
  2653. /*
  2654. * Fetch an entry off the ring and copy it into a local data
  2655. * structure. The copy involves a byte-swap since the
  2656. * network byte order and pci byte orders are different.
  2657. */
  2658. entry = lpfc_resp_iocb(phba, pring);
  2659. phba->last_completion_time = jiffies;
  2660. if (++pring->rspidx >= portRspMax)
  2661. pring->rspidx = 0;
  2662. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2663. (uint32_t *) &rspiocbq.iocb,
  2664. phba->iocb_rsp_size);
  2665. INIT_LIST_HEAD(&(rspiocbq.list));
  2666. irsp = &rspiocbq.iocb;
  2667. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2668. pring->stats.iocb_rsp++;
  2669. rsp_cmpl++;
  2670. if (unlikely(irsp->ulpStatus)) {
  2671. /*
  2672. * If resource errors reported from HBA, reduce
  2673. * queuedepths of the SCSI device.
  2674. */
  2675. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2676. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2677. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2678. phba->lpfc_rampdown_queue_depth(phba);
  2679. spin_lock_irqsave(&phba->hbalock, iflag);
  2680. }
  2681. /* Rsp ring <ringno> error: IOCB */
  2682. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2683. "0336 Rsp Ring %d error: IOCB Data: "
  2684. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2685. pring->ringno,
  2686. irsp->un.ulpWord[0],
  2687. irsp->un.ulpWord[1],
  2688. irsp->un.ulpWord[2],
  2689. irsp->un.ulpWord[3],
  2690. irsp->un.ulpWord[4],
  2691. irsp->un.ulpWord[5],
  2692. *(uint32_t *)&irsp->un1,
  2693. *((uint32_t *)&irsp->un1 + 1));
  2694. }
  2695. switch (type) {
  2696. case LPFC_ABORT_IOCB:
  2697. case LPFC_SOL_IOCB:
  2698. /*
  2699. * Idle exchange closed via ABTS from port. No iocb
  2700. * resources need to be recovered.
  2701. */
  2702. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2703. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2704. "0333 IOCB cmd 0x%x"
  2705. " processed. Skipping"
  2706. " completion\n",
  2707. irsp->ulpCommand);
  2708. break;
  2709. }
  2710. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2711. &rspiocbq);
  2712. if (unlikely(!cmdiocbq))
  2713. break;
  2714. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2715. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2716. if (cmdiocbq->iocb_cmpl) {
  2717. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2718. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2719. &rspiocbq);
  2720. spin_lock_irqsave(&phba->hbalock, iflag);
  2721. }
  2722. break;
  2723. case LPFC_UNSOL_IOCB:
  2724. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2725. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2726. spin_lock_irqsave(&phba->hbalock, iflag);
  2727. break;
  2728. default:
  2729. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2730. char adaptermsg[LPFC_MAX_ADPTMSG];
  2731. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2732. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2733. MAX_MSG_DATA);
  2734. dev_warn(&((phba->pcidev)->dev),
  2735. "lpfc%d: %s\n",
  2736. phba->brd_no, adaptermsg);
  2737. } else {
  2738. /* Unknown IOCB command */
  2739. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2740. "0334 Unknown IOCB command "
  2741. "Data: x%x, x%x x%x x%x x%x\n",
  2742. type, irsp->ulpCommand,
  2743. irsp->ulpStatus,
  2744. irsp->ulpIoTag,
  2745. irsp->ulpContext);
  2746. }
  2747. break;
  2748. }
  2749. /*
  2750. * The response IOCB has been processed. Update the ring
  2751. * pointer in SLIM. If the port response put pointer has not
  2752. * been updated, sync the pgp->rspPutInx and fetch the new port
  2753. * response put pointer.
  2754. */
  2755. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2756. if (pring->rspidx == portRspPut)
  2757. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2758. }
  2759. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2760. pring->stats.iocb_rsp_full++;
  2761. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2762. writel(status, phba->CAregaddr);
  2763. readl(phba->CAregaddr);
  2764. }
  2765. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2766. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2767. pring->stats.iocb_cmd_empty++;
  2768. /* Force update of the local copy of cmdGetInx */
  2769. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2770. lpfc_sli_resume_iocb(phba, pring);
  2771. if ((pring->lpfc_sli_cmd_available))
  2772. (pring->lpfc_sli_cmd_available) (phba, pring);
  2773. }
  2774. phba->fcp_ring_in_use = 0;
  2775. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2776. return rc;
  2777. }
  2778. /**
  2779. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2780. * @phba: Pointer to HBA context object.
  2781. * @pring: Pointer to driver SLI ring object.
  2782. * @rspiocbp: Pointer to driver response IOCB object.
  2783. *
  2784. * This function is called from the worker thread when there is a slow-path
  2785. * response IOCB to process. This function chains all the response iocbs until
  2786. * seeing the iocb with the LE bit set. The function will call
  2787. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2788. * completion of a command iocb. The function will call the
  2789. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2790. * The function frees the resources or calls the completion handler if this
  2791. * iocb is an abort completion. The function returns NULL when the response
  2792. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2793. * this function shall chain the iocb on to the iocb_continueq and return the
  2794. * response iocb passed in.
  2795. **/
  2796. static struct lpfc_iocbq *
  2797. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2798. struct lpfc_iocbq *rspiocbp)
  2799. {
  2800. struct lpfc_iocbq *saveq;
  2801. struct lpfc_iocbq *cmdiocbp;
  2802. struct lpfc_iocbq *next_iocb;
  2803. IOCB_t *irsp = NULL;
  2804. uint32_t free_saveq;
  2805. uint8_t iocb_cmd_type;
  2806. lpfc_iocb_type type;
  2807. unsigned long iflag;
  2808. int rc;
  2809. spin_lock_irqsave(&phba->hbalock, iflag);
  2810. /* First add the response iocb to the countinueq list */
  2811. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2812. pring->iocb_continueq_cnt++;
  2813. /* Now, determine whether the list is completed for processing */
  2814. irsp = &rspiocbp->iocb;
  2815. if (irsp->ulpLe) {
  2816. /*
  2817. * By default, the driver expects to free all resources
  2818. * associated with this iocb completion.
  2819. */
  2820. free_saveq = 1;
  2821. saveq = list_get_first(&pring->iocb_continueq,
  2822. struct lpfc_iocbq, list);
  2823. irsp = &(saveq->iocb);
  2824. list_del_init(&pring->iocb_continueq);
  2825. pring->iocb_continueq_cnt = 0;
  2826. pring->stats.iocb_rsp++;
  2827. /*
  2828. * If resource errors reported from HBA, reduce
  2829. * queuedepths of the SCSI device.
  2830. */
  2831. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2832. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2833. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2834. phba->lpfc_rampdown_queue_depth(phba);
  2835. spin_lock_irqsave(&phba->hbalock, iflag);
  2836. }
  2837. if (irsp->ulpStatus) {
  2838. /* Rsp ring <ringno> error: IOCB */
  2839. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2840. "0328 Rsp Ring %d error: "
  2841. "IOCB Data: "
  2842. "x%x x%x x%x x%x "
  2843. "x%x x%x x%x x%x "
  2844. "x%x x%x x%x x%x "
  2845. "x%x x%x x%x x%x\n",
  2846. pring->ringno,
  2847. irsp->un.ulpWord[0],
  2848. irsp->un.ulpWord[1],
  2849. irsp->un.ulpWord[2],
  2850. irsp->un.ulpWord[3],
  2851. irsp->un.ulpWord[4],
  2852. irsp->un.ulpWord[5],
  2853. *(((uint32_t *) irsp) + 6),
  2854. *(((uint32_t *) irsp) + 7),
  2855. *(((uint32_t *) irsp) + 8),
  2856. *(((uint32_t *) irsp) + 9),
  2857. *(((uint32_t *) irsp) + 10),
  2858. *(((uint32_t *) irsp) + 11),
  2859. *(((uint32_t *) irsp) + 12),
  2860. *(((uint32_t *) irsp) + 13),
  2861. *(((uint32_t *) irsp) + 14),
  2862. *(((uint32_t *) irsp) + 15));
  2863. }
  2864. /*
  2865. * Fetch the IOCB command type and call the correct completion
  2866. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2867. * get freed back to the lpfc_iocb_list by the discovery
  2868. * kernel thread.
  2869. */
  2870. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2871. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2872. switch (type) {
  2873. case LPFC_SOL_IOCB:
  2874. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2875. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2876. spin_lock_irqsave(&phba->hbalock, iflag);
  2877. break;
  2878. case LPFC_UNSOL_IOCB:
  2879. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2880. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2881. spin_lock_irqsave(&phba->hbalock, iflag);
  2882. if (!rc)
  2883. free_saveq = 0;
  2884. break;
  2885. case LPFC_ABORT_IOCB:
  2886. cmdiocbp = NULL;
  2887. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2888. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2889. saveq);
  2890. if (cmdiocbp) {
  2891. /* Call the specified completion routine */
  2892. if (cmdiocbp->iocb_cmpl) {
  2893. spin_unlock_irqrestore(&phba->hbalock,
  2894. iflag);
  2895. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2896. saveq);
  2897. spin_lock_irqsave(&phba->hbalock,
  2898. iflag);
  2899. } else
  2900. __lpfc_sli_release_iocbq(phba,
  2901. cmdiocbp);
  2902. }
  2903. break;
  2904. case LPFC_UNKNOWN_IOCB:
  2905. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2906. char adaptermsg[LPFC_MAX_ADPTMSG];
  2907. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2908. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2909. MAX_MSG_DATA);
  2910. dev_warn(&((phba->pcidev)->dev),
  2911. "lpfc%d: %s\n",
  2912. phba->brd_no, adaptermsg);
  2913. } else {
  2914. /* Unknown IOCB command */
  2915. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2916. "0335 Unknown IOCB "
  2917. "command Data: x%x "
  2918. "x%x x%x x%x\n",
  2919. irsp->ulpCommand,
  2920. irsp->ulpStatus,
  2921. irsp->ulpIoTag,
  2922. irsp->ulpContext);
  2923. }
  2924. break;
  2925. }
  2926. if (free_saveq) {
  2927. list_for_each_entry_safe(rspiocbp, next_iocb,
  2928. &saveq->list, list) {
  2929. list_del(&rspiocbp->list);
  2930. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2931. }
  2932. __lpfc_sli_release_iocbq(phba, saveq);
  2933. }
  2934. rspiocbp = NULL;
  2935. }
  2936. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2937. return rspiocbp;
  2938. }
  2939. /**
  2940. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2941. * @phba: Pointer to HBA context object.
  2942. * @pring: Pointer to driver SLI ring object.
  2943. * @mask: Host attention register mask for this ring.
  2944. *
  2945. * This routine wraps the actual slow_ring event process routine from the
  2946. * API jump table function pointer from the lpfc_hba struct.
  2947. **/
  2948. void
  2949. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2950. struct lpfc_sli_ring *pring, uint32_t mask)
  2951. {
  2952. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2953. }
  2954. /**
  2955. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  2956. * @phba: Pointer to HBA context object.
  2957. * @pring: Pointer to driver SLI ring object.
  2958. * @mask: Host attention register mask for this ring.
  2959. *
  2960. * This function is called from the worker thread when there is a ring event
  2961. * for non-fcp rings. The caller does not hold any lock. The function will
  2962. * remove each response iocb in the response ring and calls the handle
  2963. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2964. **/
  2965. static void
  2966. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2967. struct lpfc_sli_ring *pring, uint32_t mask)
  2968. {
  2969. struct lpfc_pgp *pgp;
  2970. IOCB_t *entry;
  2971. IOCB_t *irsp = NULL;
  2972. struct lpfc_iocbq *rspiocbp = NULL;
  2973. uint32_t portRspPut, portRspMax;
  2974. unsigned long iflag;
  2975. uint32_t status;
  2976. pgp = &phba->port_gp[pring->ringno];
  2977. spin_lock_irqsave(&phba->hbalock, iflag);
  2978. pring->stats.iocb_event++;
  2979. /*
  2980. * The next available response entry should never exceed the maximum
  2981. * entries. If it does, treat it as an adapter hardware error.
  2982. */
  2983. portRspMax = pring->numRiocb;
  2984. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2985. if (portRspPut >= portRspMax) {
  2986. /*
  2987. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2988. * rsp ring <portRspMax>
  2989. */
  2990. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2991. "0303 Ring %d handler: portRspPut %d "
  2992. "is bigger than rsp ring %d\n",
  2993. pring->ringno, portRspPut, portRspMax);
  2994. phba->link_state = LPFC_HBA_ERROR;
  2995. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2996. phba->work_hs = HS_FFER3;
  2997. lpfc_handle_eratt(phba);
  2998. return;
  2999. }
  3000. rmb();
  3001. while (pring->rspidx != portRspPut) {
  3002. /*
  3003. * Build a completion list and call the appropriate handler.
  3004. * The process is to get the next available response iocb, get
  3005. * a free iocb from the list, copy the response data into the
  3006. * free iocb, insert to the continuation list, and update the
  3007. * next response index to slim. This process makes response
  3008. * iocb's in the ring available to DMA as fast as possible but
  3009. * pays a penalty for a copy operation. Since the iocb is
  3010. * only 32 bytes, this penalty is considered small relative to
  3011. * the PCI reads for register values and a slim write. When
  3012. * the ulpLe field is set, the entire Command has been
  3013. * received.
  3014. */
  3015. entry = lpfc_resp_iocb(phba, pring);
  3016. phba->last_completion_time = jiffies;
  3017. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3018. if (rspiocbp == NULL) {
  3019. printk(KERN_ERR "%s: out of buffers! Failing "
  3020. "completion.\n", __func__);
  3021. break;
  3022. }
  3023. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3024. phba->iocb_rsp_size);
  3025. irsp = &rspiocbp->iocb;
  3026. if (++pring->rspidx >= portRspMax)
  3027. pring->rspidx = 0;
  3028. if (pring->ringno == LPFC_ELS_RING) {
  3029. lpfc_debugfs_slow_ring_trc(phba,
  3030. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3031. *(((uint32_t *) irsp) + 4),
  3032. *(((uint32_t *) irsp) + 6),
  3033. *(((uint32_t *) irsp) + 7));
  3034. }
  3035. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  3036. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3037. /* Handle the response IOCB */
  3038. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3039. spin_lock_irqsave(&phba->hbalock, iflag);
  3040. /*
  3041. * If the port response put pointer has not been updated, sync
  3042. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3043. * response put pointer.
  3044. */
  3045. if (pring->rspidx == portRspPut) {
  3046. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3047. }
  3048. } /* while (pring->rspidx != portRspPut) */
  3049. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3050. /* At least one response entry has been freed */
  3051. pring->stats.iocb_rsp_full++;
  3052. /* SET RxRE_RSP in Chip Att register */
  3053. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3054. writel(status, phba->CAregaddr);
  3055. readl(phba->CAregaddr); /* flush */
  3056. }
  3057. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3058. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3059. pring->stats.iocb_cmd_empty++;
  3060. /* Force update of the local copy of cmdGetInx */
  3061. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3062. lpfc_sli_resume_iocb(phba, pring);
  3063. if ((pring->lpfc_sli_cmd_available))
  3064. (pring->lpfc_sli_cmd_available) (phba, pring);
  3065. }
  3066. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3067. return;
  3068. }
  3069. /**
  3070. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3071. * @phba: Pointer to HBA context object.
  3072. * @pring: Pointer to driver SLI ring object.
  3073. * @mask: Host attention register mask for this ring.
  3074. *
  3075. * This function is called from the worker thread when there is a pending
  3076. * ELS response iocb on the driver internal slow-path response iocb worker
  3077. * queue. The caller does not hold any lock. The function will remove each
  3078. * response iocb from the response worker queue and calls the handle
  3079. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3080. **/
  3081. static void
  3082. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3083. struct lpfc_sli_ring *pring, uint32_t mask)
  3084. {
  3085. struct lpfc_iocbq *irspiocbq;
  3086. struct hbq_dmabuf *dmabuf;
  3087. struct lpfc_cq_event *cq_event;
  3088. unsigned long iflag;
  3089. spin_lock_irqsave(&phba->hbalock, iflag);
  3090. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3091. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3092. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3093. /* Get the response iocb from the head of work queue */
  3094. spin_lock_irqsave(&phba->hbalock, iflag);
  3095. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3096. cq_event, struct lpfc_cq_event, list);
  3097. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3098. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3099. case CQE_CODE_COMPL_WQE:
  3100. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3101. cq_event);
  3102. /* Translate ELS WCQE to response IOCBQ */
  3103. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3104. irspiocbq);
  3105. if (irspiocbq)
  3106. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3107. irspiocbq);
  3108. break;
  3109. case CQE_CODE_RECEIVE:
  3110. case CQE_CODE_RECEIVE_V1:
  3111. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3112. cq_event);
  3113. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3114. break;
  3115. default:
  3116. break;
  3117. }
  3118. }
  3119. }
  3120. /**
  3121. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3122. * @phba: Pointer to HBA context object.
  3123. * @pring: Pointer to driver SLI ring object.
  3124. *
  3125. * This function aborts all iocbs in the given ring and frees all the iocb
  3126. * objects in txq. This function issues an abort iocb for all the iocb commands
  3127. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3128. * the return of this function. The caller is not required to hold any locks.
  3129. **/
  3130. void
  3131. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3132. {
  3133. LIST_HEAD(completions);
  3134. struct lpfc_iocbq *iocb, *next_iocb;
  3135. if (pring->ringno == LPFC_ELS_RING) {
  3136. lpfc_fabric_abort_hba(phba);
  3137. }
  3138. /* Error everything on txq and txcmplq
  3139. * First do the txq.
  3140. */
  3141. spin_lock_irq(&phba->hbalock);
  3142. list_splice_init(&pring->txq, &completions);
  3143. pring->txq_cnt = 0;
  3144. /* Next issue ABTS for everything on the txcmplq */
  3145. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3146. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3147. spin_unlock_irq(&phba->hbalock);
  3148. /* Cancel all the IOCBs from the completions list */
  3149. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3150. IOERR_SLI_ABORTED);
  3151. }
  3152. /**
  3153. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3154. * @phba: Pointer to HBA context object.
  3155. *
  3156. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3157. * objects in txq and txcmplq. This function will not issue abort iocbs
  3158. * for all the iocb commands in txcmplq, they will just be returned with
  3159. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3160. * slot has been permanently disabled.
  3161. **/
  3162. void
  3163. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3164. {
  3165. LIST_HEAD(txq);
  3166. LIST_HEAD(txcmplq);
  3167. struct lpfc_sli *psli = &phba->sli;
  3168. struct lpfc_sli_ring *pring;
  3169. /* Currently, only one fcp ring */
  3170. pring = &psli->ring[psli->fcp_ring];
  3171. spin_lock_irq(&phba->hbalock);
  3172. /* Retrieve everything on txq */
  3173. list_splice_init(&pring->txq, &txq);
  3174. pring->txq_cnt = 0;
  3175. /* Retrieve everything on the txcmplq */
  3176. list_splice_init(&pring->txcmplq, &txcmplq);
  3177. pring->txcmplq_cnt = 0;
  3178. /* Indicate the I/O queues are flushed */
  3179. phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
  3180. spin_unlock_irq(&phba->hbalock);
  3181. /* Flush the txq */
  3182. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3183. IOERR_SLI_DOWN);
  3184. /* Flush the txcmpq */
  3185. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3186. IOERR_SLI_DOWN);
  3187. }
  3188. /**
  3189. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3190. * @phba: Pointer to HBA context object.
  3191. * @mask: Bit mask to be checked.
  3192. *
  3193. * This function reads the host status register and compares
  3194. * with the provided bit mask to check if HBA completed
  3195. * the restart. This function will wait in a loop for the
  3196. * HBA to complete restart. If the HBA does not restart within
  3197. * 15 iterations, the function will reset the HBA again. The
  3198. * function returns 1 when HBA fail to restart otherwise returns
  3199. * zero.
  3200. **/
  3201. static int
  3202. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3203. {
  3204. uint32_t status;
  3205. int i = 0;
  3206. int retval = 0;
  3207. /* Read the HBA Host Status Register */
  3208. if (lpfc_readl(phba->HSregaddr, &status))
  3209. return 1;
  3210. /*
  3211. * Check status register every 100ms for 5 retries, then every
  3212. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3213. * every 2.5 sec for 4.
  3214. * Break our of the loop if errors occurred during init.
  3215. */
  3216. while (((status & mask) != mask) &&
  3217. !(status & HS_FFERM) &&
  3218. i++ < 20) {
  3219. if (i <= 5)
  3220. msleep(10);
  3221. else if (i <= 10)
  3222. msleep(500);
  3223. else
  3224. msleep(2500);
  3225. if (i == 15) {
  3226. /* Do post */
  3227. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3228. lpfc_sli_brdrestart(phba);
  3229. }
  3230. /* Read the HBA Host Status Register */
  3231. if (lpfc_readl(phba->HSregaddr, &status)) {
  3232. retval = 1;
  3233. break;
  3234. }
  3235. }
  3236. /* Check to see if any errors occurred during init */
  3237. if ((status & HS_FFERM) || (i >= 20)) {
  3238. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3239. "2751 Adapter failed to restart, "
  3240. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3241. status,
  3242. readl(phba->MBslimaddr + 0xa8),
  3243. readl(phba->MBslimaddr + 0xac));
  3244. phba->link_state = LPFC_HBA_ERROR;
  3245. retval = 1;
  3246. }
  3247. return retval;
  3248. }
  3249. /**
  3250. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3251. * @phba: Pointer to HBA context object.
  3252. * @mask: Bit mask to be checked.
  3253. *
  3254. * This function checks the host status register to check if HBA is
  3255. * ready. This function will wait in a loop for the HBA to be ready
  3256. * If the HBA is not ready , the function will will reset the HBA PCI
  3257. * function again. The function returns 1 when HBA fail to be ready
  3258. * otherwise returns zero.
  3259. **/
  3260. static int
  3261. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3262. {
  3263. uint32_t status;
  3264. int retval = 0;
  3265. /* Read the HBA Host Status Register */
  3266. status = lpfc_sli4_post_status_check(phba);
  3267. if (status) {
  3268. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3269. lpfc_sli_brdrestart(phba);
  3270. status = lpfc_sli4_post_status_check(phba);
  3271. }
  3272. /* Check to see if any errors occurred during init */
  3273. if (status) {
  3274. phba->link_state = LPFC_HBA_ERROR;
  3275. retval = 1;
  3276. } else
  3277. phba->sli4_hba.intr_enable = 0;
  3278. return retval;
  3279. }
  3280. /**
  3281. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3282. * @phba: Pointer to HBA context object.
  3283. * @mask: Bit mask to be checked.
  3284. *
  3285. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3286. * from the API jump table function pointer from the lpfc_hba struct.
  3287. **/
  3288. int
  3289. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3290. {
  3291. return phba->lpfc_sli_brdready(phba, mask);
  3292. }
  3293. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3294. /**
  3295. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3296. * @phba: Pointer to HBA context object.
  3297. *
  3298. * This function is called before resetting an HBA. This function is called
  3299. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3300. **/
  3301. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3302. {
  3303. uint32_t __iomem *resp_buf;
  3304. uint32_t __iomem *mbox_buf;
  3305. volatile uint32_t mbox;
  3306. uint32_t hc_copy, ha_copy, resp_data;
  3307. int i;
  3308. uint8_t hdrtype;
  3309. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3310. if (hdrtype != 0x80 ||
  3311. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3312. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3313. return;
  3314. /*
  3315. * Tell the other part of the chip to suspend temporarily all
  3316. * its DMA activity.
  3317. */
  3318. resp_buf = phba->MBslimaddr;
  3319. /* Disable the error attention */
  3320. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3321. return;
  3322. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3323. readl(phba->HCregaddr); /* flush */
  3324. phba->link_flag |= LS_IGNORE_ERATT;
  3325. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3326. return;
  3327. if (ha_copy & HA_ERATT) {
  3328. /* Clear Chip error bit */
  3329. writel(HA_ERATT, phba->HAregaddr);
  3330. phba->pport->stopped = 1;
  3331. }
  3332. mbox = 0;
  3333. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3334. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3335. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3336. mbox_buf = phba->MBslimaddr;
  3337. writel(mbox, mbox_buf);
  3338. for (i = 0; i < 50; i++) {
  3339. if (lpfc_readl((resp_buf + 1), &resp_data))
  3340. return;
  3341. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3342. mdelay(1);
  3343. else
  3344. break;
  3345. }
  3346. resp_data = 0;
  3347. if (lpfc_readl((resp_buf + 1), &resp_data))
  3348. return;
  3349. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3350. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3351. phba->pport->stopped)
  3352. goto restore_hc;
  3353. else
  3354. goto clear_errat;
  3355. }
  3356. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3357. resp_data = 0;
  3358. for (i = 0; i < 500; i++) {
  3359. if (lpfc_readl(resp_buf, &resp_data))
  3360. return;
  3361. if (resp_data != mbox)
  3362. mdelay(1);
  3363. else
  3364. break;
  3365. }
  3366. clear_errat:
  3367. while (++i < 500) {
  3368. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3369. return;
  3370. if (!(ha_copy & HA_ERATT))
  3371. mdelay(1);
  3372. else
  3373. break;
  3374. }
  3375. if (readl(phba->HAregaddr) & HA_ERATT) {
  3376. writel(HA_ERATT, phba->HAregaddr);
  3377. phba->pport->stopped = 1;
  3378. }
  3379. restore_hc:
  3380. phba->link_flag &= ~LS_IGNORE_ERATT;
  3381. writel(hc_copy, phba->HCregaddr);
  3382. readl(phba->HCregaddr); /* flush */
  3383. }
  3384. /**
  3385. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3386. * @phba: Pointer to HBA context object.
  3387. *
  3388. * This function issues a kill_board mailbox command and waits for
  3389. * the error attention interrupt. This function is called for stopping
  3390. * the firmware processing. The caller is not required to hold any
  3391. * locks. This function calls lpfc_hba_down_post function to free
  3392. * any pending commands after the kill. The function will return 1 when it
  3393. * fails to kill the board else will return 0.
  3394. **/
  3395. int
  3396. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3397. {
  3398. struct lpfc_sli *psli;
  3399. LPFC_MBOXQ_t *pmb;
  3400. uint32_t status;
  3401. uint32_t ha_copy;
  3402. int retval;
  3403. int i = 0;
  3404. psli = &phba->sli;
  3405. /* Kill HBA */
  3406. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3407. "0329 Kill HBA Data: x%x x%x\n",
  3408. phba->pport->port_state, psli->sli_flag);
  3409. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3410. if (!pmb)
  3411. return 1;
  3412. /* Disable the error attention */
  3413. spin_lock_irq(&phba->hbalock);
  3414. if (lpfc_readl(phba->HCregaddr, &status)) {
  3415. spin_unlock_irq(&phba->hbalock);
  3416. mempool_free(pmb, phba->mbox_mem_pool);
  3417. return 1;
  3418. }
  3419. status &= ~HC_ERINT_ENA;
  3420. writel(status, phba->HCregaddr);
  3421. readl(phba->HCregaddr); /* flush */
  3422. phba->link_flag |= LS_IGNORE_ERATT;
  3423. spin_unlock_irq(&phba->hbalock);
  3424. lpfc_kill_board(phba, pmb);
  3425. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3426. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3427. if (retval != MBX_SUCCESS) {
  3428. if (retval != MBX_BUSY)
  3429. mempool_free(pmb, phba->mbox_mem_pool);
  3430. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3431. "2752 KILL_BOARD command failed retval %d\n",
  3432. retval);
  3433. spin_lock_irq(&phba->hbalock);
  3434. phba->link_flag &= ~LS_IGNORE_ERATT;
  3435. spin_unlock_irq(&phba->hbalock);
  3436. return 1;
  3437. }
  3438. spin_lock_irq(&phba->hbalock);
  3439. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3440. spin_unlock_irq(&phba->hbalock);
  3441. mempool_free(pmb, phba->mbox_mem_pool);
  3442. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3443. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3444. * 3 seconds we still set HBA_ERROR state because the status of the
  3445. * board is now undefined.
  3446. */
  3447. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3448. return 1;
  3449. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3450. mdelay(100);
  3451. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3452. return 1;
  3453. }
  3454. del_timer_sync(&psli->mbox_tmo);
  3455. if (ha_copy & HA_ERATT) {
  3456. writel(HA_ERATT, phba->HAregaddr);
  3457. phba->pport->stopped = 1;
  3458. }
  3459. spin_lock_irq(&phba->hbalock);
  3460. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3461. psli->mbox_active = NULL;
  3462. phba->link_flag &= ~LS_IGNORE_ERATT;
  3463. spin_unlock_irq(&phba->hbalock);
  3464. lpfc_hba_down_post(phba);
  3465. phba->link_state = LPFC_HBA_ERROR;
  3466. return ha_copy & HA_ERATT ? 0 : 1;
  3467. }
  3468. /**
  3469. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3470. * @phba: Pointer to HBA context object.
  3471. *
  3472. * This function resets the HBA by writing HC_INITFF to the control
  3473. * register. After the HBA resets, this function resets all the iocb ring
  3474. * indices. This function disables PCI layer parity checking during
  3475. * the reset.
  3476. * This function returns 0 always.
  3477. * The caller is not required to hold any locks.
  3478. **/
  3479. int
  3480. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3481. {
  3482. struct lpfc_sli *psli;
  3483. struct lpfc_sli_ring *pring;
  3484. uint16_t cfg_value;
  3485. int i;
  3486. psli = &phba->sli;
  3487. /* Reset HBA */
  3488. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3489. "0325 Reset HBA Data: x%x x%x\n",
  3490. phba->pport->port_state, psli->sli_flag);
  3491. /* perform board reset */
  3492. phba->fc_eventTag = 0;
  3493. phba->link_events = 0;
  3494. phba->pport->fc_myDID = 0;
  3495. phba->pport->fc_prevDID = 0;
  3496. /* Turn off parity checking and serr during the physical reset */
  3497. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3498. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3499. (cfg_value &
  3500. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3501. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3502. /* Now toggle INITFF bit in the Host Control Register */
  3503. writel(HC_INITFF, phba->HCregaddr);
  3504. mdelay(1);
  3505. readl(phba->HCregaddr); /* flush */
  3506. writel(0, phba->HCregaddr);
  3507. readl(phba->HCregaddr); /* flush */
  3508. /* Restore PCI cmd register */
  3509. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3510. /* Initialize relevant SLI info */
  3511. for (i = 0; i < psli->num_rings; i++) {
  3512. pring = &psli->ring[i];
  3513. pring->flag = 0;
  3514. pring->rspidx = 0;
  3515. pring->next_cmdidx = 0;
  3516. pring->local_getidx = 0;
  3517. pring->cmdidx = 0;
  3518. pring->missbufcnt = 0;
  3519. }
  3520. phba->link_state = LPFC_WARM_START;
  3521. return 0;
  3522. }
  3523. /**
  3524. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3525. * @phba: Pointer to HBA context object.
  3526. *
  3527. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3528. * checking during resets the device. The caller is not required to hold
  3529. * any locks.
  3530. *
  3531. * This function returns 0 always.
  3532. **/
  3533. int
  3534. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3535. {
  3536. struct lpfc_sli *psli = &phba->sli;
  3537. uint16_t cfg_value;
  3538. int rc;
  3539. /* Reset HBA */
  3540. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3541. "0295 Reset HBA Data: x%x x%x\n",
  3542. phba->pport->port_state, psli->sli_flag);
  3543. /* perform board reset */
  3544. phba->fc_eventTag = 0;
  3545. phba->link_events = 0;
  3546. phba->pport->fc_myDID = 0;
  3547. phba->pport->fc_prevDID = 0;
  3548. spin_lock_irq(&phba->hbalock);
  3549. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3550. phba->fcf.fcf_flag = 0;
  3551. spin_unlock_irq(&phba->hbalock);
  3552. /* Now physically reset the device */
  3553. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3554. "0389 Performing PCI function reset!\n");
  3555. /* Turn off parity checking and serr during the physical reset */
  3556. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3557. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3558. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3559. /* Perform FCoE PCI function reset */
  3560. lpfc_sli4_queue_destroy(phba);
  3561. rc = lpfc_pci_function_reset(phba);
  3562. /* Restore PCI cmd register */
  3563. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3564. return rc;
  3565. }
  3566. /**
  3567. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3568. * @phba: Pointer to HBA context object.
  3569. *
  3570. * This function is called in the SLI initialization code path to
  3571. * restart the HBA. The caller is not required to hold any lock.
  3572. * This function writes MBX_RESTART mailbox command to the SLIM and
  3573. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3574. * function to free any pending commands. The function enables
  3575. * POST only during the first initialization. The function returns zero.
  3576. * The function does not guarantee completion of MBX_RESTART mailbox
  3577. * command before the return of this function.
  3578. **/
  3579. static int
  3580. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3581. {
  3582. MAILBOX_t *mb;
  3583. struct lpfc_sli *psli;
  3584. volatile uint32_t word0;
  3585. void __iomem *to_slim;
  3586. uint32_t hba_aer_enabled;
  3587. spin_lock_irq(&phba->hbalock);
  3588. /* Take PCIe device Advanced Error Reporting (AER) state */
  3589. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3590. psli = &phba->sli;
  3591. /* Restart HBA */
  3592. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3593. "0337 Restart HBA Data: x%x x%x\n",
  3594. phba->pport->port_state, psli->sli_flag);
  3595. word0 = 0;
  3596. mb = (MAILBOX_t *) &word0;
  3597. mb->mbxCommand = MBX_RESTART;
  3598. mb->mbxHc = 1;
  3599. lpfc_reset_barrier(phba);
  3600. to_slim = phba->MBslimaddr;
  3601. writel(*(uint32_t *) mb, to_slim);
  3602. readl(to_slim); /* flush */
  3603. /* Only skip post after fc_ffinit is completed */
  3604. if (phba->pport->port_state)
  3605. word0 = 1; /* This is really setting up word1 */
  3606. else
  3607. word0 = 0; /* This is really setting up word1 */
  3608. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3609. writel(*(uint32_t *) mb, to_slim);
  3610. readl(to_slim); /* flush */
  3611. lpfc_sli_brdreset(phba);
  3612. phba->pport->stopped = 0;
  3613. phba->link_state = LPFC_INIT_START;
  3614. phba->hba_flag = 0;
  3615. spin_unlock_irq(&phba->hbalock);
  3616. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3617. psli->stats_start = get_seconds();
  3618. /* Give the INITFF and Post time to settle. */
  3619. mdelay(100);
  3620. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3621. if (hba_aer_enabled)
  3622. pci_disable_pcie_error_reporting(phba->pcidev);
  3623. lpfc_hba_down_post(phba);
  3624. return 0;
  3625. }
  3626. /**
  3627. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3628. * @phba: Pointer to HBA context object.
  3629. *
  3630. * This function is called in the SLI initialization code path to restart
  3631. * a SLI4 HBA. The caller is not required to hold any lock.
  3632. * At the end of the function, it calls lpfc_hba_down_post function to
  3633. * free any pending commands.
  3634. **/
  3635. static int
  3636. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3637. {
  3638. struct lpfc_sli *psli = &phba->sli;
  3639. uint32_t hba_aer_enabled;
  3640. int rc;
  3641. /* Restart HBA */
  3642. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3643. "0296 Restart HBA Data: x%x x%x\n",
  3644. phba->pport->port_state, psli->sli_flag);
  3645. /* Take PCIe device Advanced Error Reporting (AER) state */
  3646. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3647. rc = lpfc_sli4_brdreset(phba);
  3648. spin_lock_irq(&phba->hbalock);
  3649. phba->pport->stopped = 0;
  3650. phba->link_state = LPFC_INIT_START;
  3651. phba->hba_flag = 0;
  3652. spin_unlock_irq(&phba->hbalock);
  3653. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3654. psli->stats_start = get_seconds();
  3655. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3656. if (hba_aer_enabled)
  3657. pci_disable_pcie_error_reporting(phba->pcidev);
  3658. lpfc_hba_down_post(phba);
  3659. return rc;
  3660. }
  3661. /**
  3662. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3663. * @phba: Pointer to HBA context object.
  3664. *
  3665. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3666. * API jump table function pointer from the lpfc_hba struct.
  3667. **/
  3668. int
  3669. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3670. {
  3671. return phba->lpfc_sli_brdrestart(phba);
  3672. }
  3673. /**
  3674. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3675. * @phba: Pointer to HBA context object.
  3676. *
  3677. * This function is called after a HBA restart to wait for successful
  3678. * restart of the HBA. Successful restart of the HBA is indicated by
  3679. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3680. * iteration, the function will restart the HBA again. The function returns
  3681. * zero if HBA successfully restarted else returns negative error code.
  3682. **/
  3683. static int
  3684. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3685. {
  3686. uint32_t status, i = 0;
  3687. /* Read the HBA Host Status Register */
  3688. if (lpfc_readl(phba->HSregaddr, &status))
  3689. return -EIO;
  3690. /* Check status register to see what current state is */
  3691. i = 0;
  3692. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3693. /* Check every 10ms for 10 retries, then every 100ms for 90
  3694. * retries, then every 1 sec for 50 retires for a total of
  3695. * ~60 seconds before reset the board again and check every
  3696. * 1 sec for 50 retries. The up to 60 seconds before the
  3697. * board ready is required by the Falcon FIPS zeroization
  3698. * complete, and any reset the board in between shall cause
  3699. * restart of zeroization, further delay the board ready.
  3700. */
  3701. if (i++ >= 200) {
  3702. /* Adapter failed to init, timeout, status reg
  3703. <status> */
  3704. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3705. "0436 Adapter failed to init, "
  3706. "timeout, status reg x%x, "
  3707. "FW Data: A8 x%x AC x%x\n", status,
  3708. readl(phba->MBslimaddr + 0xa8),
  3709. readl(phba->MBslimaddr + 0xac));
  3710. phba->link_state = LPFC_HBA_ERROR;
  3711. return -ETIMEDOUT;
  3712. }
  3713. /* Check to see if any errors occurred during init */
  3714. if (status & HS_FFERM) {
  3715. /* ERROR: During chipset initialization */
  3716. /* Adapter failed to init, chipset, status reg
  3717. <status> */
  3718. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3719. "0437 Adapter failed to init, "
  3720. "chipset, status reg x%x, "
  3721. "FW Data: A8 x%x AC x%x\n", status,
  3722. readl(phba->MBslimaddr + 0xa8),
  3723. readl(phba->MBslimaddr + 0xac));
  3724. phba->link_state = LPFC_HBA_ERROR;
  3725. return -EIO;
  3726. }
  3727. if (i <= 10)
  3728. msleep(10);
  3729. else if (i <= 100)
  3730. msleep(100);
  3731. else
  3732. msleep(1000);
  3733. if (i == 150) {
  3734. /* Do post */
  3735. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3736. lpfc_sli_brdrestart(phba);
  3737. }
  3738. /* Read the HBA Host Status Register */
  3739. if (lpfc_readl(phba->HSregaddr, &status))
  3740. return -EIO;
  3741. }
  3742. /* Check to see if any errors occurred during init */
  3743. if (status & HS_FFERM) {
  3744. /* ERROR: During chipset initialization */
  3745. /* Adapter failed to init, chipset, status reg <status> */
  3746. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3747. "0438 Adapter failed to init, chipset, "
  3748. "status reg x%x, "
  3749. "FW Data: A8 x%x AC x%x\n", status,
  3750. readl(phba->MBslimaddr + 0xa8),
  3751. readl(phba->MBslimaddr + 0xac));
  3752. phba->link_state = LPFC_HBA_ERROR;
  3753. return -EIO;
  3754. }
  3755. /* Clear all interrupt enable conditions */
  3756. writel(0, phba->HCregaddr);
  3757. readl(phba->HCregaddr); /* flush */
  3758. /* setup host attn register */
  3759. writel(0xffffffff, phba->HAregaddr);
  3760. readl(phba->HAregaddr); /* flush */
  3761. return 0;
  3762. }
  3763. /**
  3764. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3765. *
  3766. * This function calculates and returns the number of HBQs required to be
  3767. * configured.
  3768. **/
  3769. int
  3770. lpfc_sli_hbq_count(void)
  3771. {
  3772. return ARRAY_SIZE(lpfc_hbq_defs);
  3773. }
  3774. /**
  3775. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3776. *
  3777. * This function adds the number of hbq entries in every HBQ to get
  3778. * the total number of hbq entries required for the HBA and returns
  3779. * the total count.
  3780. **/
  3781. static int
  3782. lpfc_sli_hbq_entry_count(void)
  3783. {
  3784. int hbq_count = lpfc_sli_hbq_count();
  3785. int count = 0;
  3786. int i;
  3787. for (i = 0; i < hbq_count; ++i)
  3788. count += lpfc_hbq_defs[i]->entry_count;
  3789. return count;
  3790. }
  3791. /**
  3792. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3793. *
  3794. * This function calculates amount of memory required for all hbq entries
  3795. * to be configured and returns the total memory required.
  3796. **/
  3797. int
  3798. lpfc_sli_hbq_size(void)
  3799. {
  3800. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3801. }
  3802. /**
  3803. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3804. * @phba: Pointer to HBA context object.
  3805. *
  3806. * This function is called during the SLI initialization to configure
  3807. * all the HBQs and post buffers to the HBQ. The caller is not
  3808. * required to hold any locks. This function will return zero if successful
  3809. * else it will return negative error code.
  3810. **/
  3811. static int
  3812. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3813. {
  3814. int hbq_count = lpfc_sli_hbq_count();
  3815. LPFC_MBOXQ_t *pmb;
  3816. MAILBOX_t *pmbox;
  3817. uint32_t hbqno;
  3818. uint32_t hbq_entry_index;
  3819. /* Get a Mailbox buffer to setup mailbox
  3820. * commands for HBA initialization
  3821. */
  3822. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3823. if (!pmb)
  3824. return -ENOMEM;
  3825. pmbox = &pmb->u.mb;
  3826. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3827. phba->link_state = LPFC_INIT_MBX_CMDS;
  3828. phba->hbq_in_use = 1;
  3829. hbq_entry_index = 0;
  3830. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3831. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3832. phba->hbqs[hbqno].hbqPutIdx = 0;
  3833. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3834. phba->hbqs[hbqno].entry_count =
  3835. lpfc_hbq_defs[hbqno]->entry_count;
  3836. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3837. hbq_entry_index, pmb);
  3838. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3839. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3840. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3841. mbxStatus <status>, ring <num> */
  3842. lpfc_printf_log(phba, KERN_ERR,
  3843. LOG_SLI | LOG_VPORT,
  3844. "1805 Adapter failed to init. "
  3845. "Data: x%x x%x x%x\n",
  3846. pmbox->mbxCommand,
  3847. pmbox->mbxStatus, hbqno);
  3848. phba->link_state = LPFC_HBA_ERROR;
  3849. mempool_free(pmb, phba->mbox_mem_pool);
  3850. return -ENXIO;
  3851. }
  3852. }
  3853. phba->hbq_count = hbq_count;
  3854. mempool_free(pmb, phba->mbox_mem_pool);
  3855. /* Initially populate or replenish the HBQs */
  3856. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3857. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3858. return 0;
  3859. }
  3860. /**
  3861. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3862. * @phba: Pointer to HBA context object.
  3863. *
  3864. * This function is called during the SLI initialization to configure
  3865. * all the HBQs and post buffers to the HBQ. The caller is not
  3866. * required to hold any locks. This function will return zero if successful
  3867. * else it will return negative error code.
  3868. **/
  3869. static int
  3870. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3871. {
  3872. phba->hbq_in_use = 1;
  3873. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3874. phba->hbq_count = 1;
  3875. /* Initially populate or replenish the HBQs */
  3876. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3877. return 0;
  3878. }
  3879. /**
  3880. * lpfc_sli_config_port - Issue config port mailbox command
  3881. * @phba: Pointer to HBA context object.
  3882. * @sli_mode: sli mode - 2/3
  3883. *
  3884. * This function is called by the sli intialization code path
  3885. * to issue config_port mailbox command. This function restarts the
  3886. * HBA firmware and issues a config_port mailbox command to configure
  3887. * the SLI interface in the sli mode specified by sli_mode
  3888. * variable. The caller is not required to hold any locks.
  3889. * The function returns 0 if successful, else returns negative error
  3890. * code.
  3891. **/
  3892. int
  3893. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3894. {
  3895. LPFC_MBOXQ_t *pmb;
  3896. uint32_t resetcount = 0, rc = 0, done = 0;
  3897. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3898. if (!pmb) {
  3899. phba->link_state = LPFC_HBA_ERROR;
  3900. return -ENOMEM;
  3901. }
  3902. phba->sli_rev = sli_mode;
  3903. while (resetcount < 2 && !done) {
  3904. spin_lock_irq(&phba->hbalock);
  3905. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3906. spin_unlock_irq(&phba->hbalock);
  3907. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3908. lpfc_sli_brdrestart(phba);
  3909. rc = lpfc_sli_chipset_init(phba);
  3910. if (rc)
  3911. break;
  3912. spin_lock_irq(&phba->hbalock);
  3913. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3914. spin_unlock_irq(&phba->hbalock);
  3915. resetcount++;
  3916. /* Call pre CONFIG_PORT mailbox command initialization. A
  3917. * value of 0 means the call was successful. Any other
  3918. * nonzero value is a failure, but if ERESTART is returned,
  3919. * the driver may reset the HBA and try again.
  3920. */
  3921. rc = lpfc_config_port_prep(phba);
  3922. if (rc == -ERESTART) {
  3923. phba->link_state = LPFC_LINK_UNKNOWN;
  3924. continue;
  3925. } else if (rc)
  3926. break;
  3927. phba->link_state = LPFC_INIT_MBX_CMDS;
  3928. lpfc_config_port(phba, pmb);
  3929. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3930. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3931. LPFC_SLI3_HBQ_ENABLED |
  3932. LPFC_SLI3_CRP_ENABLED |
  3933. LPFC_SLI3_BG_ENABLED |
  3934. LPFC_SLI3_DSS_ENABLED);
  3935. if (rc != MBX_SUCCESS) {
  3936. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3937. "0442 Adapter failed to init, mbxCmd x%x "
  3938. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3939. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3940. spin_lock_irq(&phba->hbalock);
  3941. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3942. spin_unlock_irq(&phba->hbalock);
  3943. rc = -ENXIO;
  3944. } else {
  3945. /* Allow asynchronous mailbox command to go through */
  3946. spin_lock_irq(&phba->hbalock);
  3947. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3948. spin_unlock_irq(&phba->hbalock);
  3949. done = 1;
  3950. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  3951. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  3952. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3953. "3110 Port did not grant ASABT\n");
  3954. }
  3955. }
  3956. if (!done) {
  3957. rc = -EINVAL;
  3958. goto do_prep_failed;
  3959. }
  3960. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3961. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3962. rc = -ENXIO;
  3963. goto do_prep_failed;
  3964. }
  3965. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3966. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3967. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3968. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3969. phba->max_vpi : phba->max_vports;
  3970. } else
  3971. phba->max_vpi = 0;
  3972. phba->fips_level = 0;
  3973. phba->fips_spec_rev = 0;
  3974. if (pmb->u.mb.un.varCfgPort.gdss) {
  3975. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3976. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  3977. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  3978. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3979. "2850 Security Crypto Active. FIPS x%d "
  3980. "(Spec Rev: x%d)",
  3981. phba->fips_level, phba->fips_spec_rev);
  3982. }
  3983. if (pmb->u.mb.un.varCfgPort.sec_err) {
  3984. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3985. "2856 Config Port Security Crypto "
  3986. "Error: x%x ",
  3987. pmb->u.mb.un.varCfgPort.sec_err);
  3988. }
  3989. if (pmb->u.mb.un.varCfgPort.gerbm)
  3990. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  3991. if (pmb->u.mb.un.varCfgPort.gcrp)
  3992. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  3993. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  3994. phba->port_gp = phba->mbox->us.s3_pgp.port;
  3995. if (phba->cfg_enable_bg) {
  3996. if (pmb->u.mb.un.varCfgPort.gbg)
  3997. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  3998. else
  3999. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4000. "0443 Adapter did not grant "
  4001. "BlockGuard\n");
  4002. }
  4003. } else {
  4004. phba->hbq_get = NULL;
  4005. phba->port_gp = phba->mbox->us.s2.port;
  4006. phba->max_vpi = 0;
  4007. }
  4008. do_prep_failed:
  4009. mempool_free(pmb, phba->mbox_mem_pool);
  4010. return rc;
  4011. }
  4012. /**
  4013. * lpfc_sli_hba_setup - SLI intialization function
  4014. * @phba: Pointer to HBA context object.
  4015. *
  4016. * This function is the main SLI intialization function. This function
  4017. * is called by the HBA intialization code, HBA reset code and HBA
  4018. * error attention handler code. Caller is not required to hold any
  4019. * locks. This function issues config_port mailbox command to configure
  4020. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4021. * calls the config_port_post function to issue init_link mailbox
  4022. * command and to start the discovery. The function will return zero
  4023. * if successful, else it will return negative error code.
  4024. **/
  4025. int
  4026. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4027. {
  4028. uint32_t rc;
  4029. int mode = 3, i;
  4030. int longs;
  4031. switch (lpfc_sli_mode) {
  4032. case 2:
  4033. if (phba->cfg_enable_npiv) {
  4034. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4035. "1824 NPIV enabled: Override lpfc_sli_mode "
  4036. "parameter (%d) to auto (0).\n",
  4037. lpfc_sli_mode);
  4038. break;
  4039. }
  4040. mode = 2;
  4041. break;
  4042. case 0:
  4043. case 3:
  4044. break;
  4045. default:
  4046. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4047. "1819 Unrecognized lpfc_sli_mode "
  4048. "parameter: %d.\n", lpfc_sli_mode);
  4049. break;
  4050. }
  4051. rc = lpfc_sli_config_port(phba, mode);
  4052. if (rc && lpfc_sli_mode == 3)
  4053. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4054. "1820 Unable to select SLI-3. "
  4055. "Not supported by adapter.\n");
  4056. if (rc && mode != 2)
  4057. rc = lpfc_sli_config_port(phba, 2);
  4058. if (rc)
  4059. goto lpfc_sli_hba_setup_error;
  4060. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4061. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4062. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4063. if (!rc) {
  4064. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4065. "2709 This device supports "
  4066. "Advanced Error Reporting (AER)\n");
  4067. spin_lock_irq(&phba->hbalock);
  4068. phba->hba_flag |= HBA_AER_ENABLED;
  4069. spin_unlock_irq(&phba->hbalock);
  4070. } else {
  4071. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4072. "2708 This device does not support "
  4073. "Advanced Error Reporting (AER)\n");
  4074. phba->cfg_aer_support = 0;
  4075. }
  4076. }
  4077. if (phba->sli_rev == 3) {
  4078. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4079. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4080. } else {
  4081. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4082. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4083. phba->sli3_options = 0;
  4084. }
  4085. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4086. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4087. phba->sli_rev, phba->max_vpi);
  4088. rc = lpfc_sli_ring_map(phba);
  4089. if (rc)
  4090. goto lpfc_sli_hba_setup_error;
  4091. /* Initialize VPIs. */
  4092. if (phba->sli_rev == LPFC_SLI_REV3) {
  4093. /*
  4094. * The VPI bitmask and physical ID array are allocated
  4095. * and initialized once only - at driver load. A port
  4096. * reset doesn't need to reinitialize this memory.
  4097. */
  4098. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4099. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4100. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4101. GFP_KERNEL);
  4102. if (!phba->vpi_bmask) {
  4103. rc = -ENOMEM;
  4104. goto lpfc_sli_hba_setup_error;
  4105. }
  4106. phba->vpi_ids = kzalloc(
  4107. (phba->max_vpi+1) * sizeof(uint16_t),
  4108. GFP_KERNEL);
  4109. if (!phba->vpi_ids) {
  4110. kfree(phba->vpi_bmask);
  4111. rc = -ENOMEM;
  4112. goto lpfc_sli_hba_setup_error;
  4113. }
  4114. for (i = 0; i < phba->max_vpi; i++)
  4115. phba->vpi_ids[i] = i;
  4116. }
  4117. }
  4118. /* Init HBQs */
  4119. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4120. rc = lpfc_sli_hbq_setup(phba);
  4121. if (rc)
  4122. goto lpfc_sli_hba_setup_error;
  4123. }
  4124. spin_lock_irq(&phba->hbalock);
  4125. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4126. spin_unlock_irq(&phba->hbalock);
  4127. rc = lpfc_config_port_post(phba);
  4128. if (rc)
  4129. goto lpfc_sli_hba_setup_error;
  4130. return rc;
  4131. lpfc_sli_hba_setup_error:
  4132. phba->link_state = LPFC_HBA_ERROR;
  4133. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4134. "0445 Firmware initialization failed\n");
  4135. return rc;
  4136. }
  4137. /**
  4138. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4139. * @phba: Pointer to HBA context object.
  4140. * @mboxq: mailbox pointer.
  4141. * This function issue a dump mailbox command to read config region
  4142. * 23 and parse the records in the region and populate driver
  4143. * data structure.
  4144. **/
  4145. static int
  4146. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4147. {
  4148. LPFC_MBOXQ_t *mboxq;
  4149. struct lpfc_dmabuf *mp;
  4150. struct lpfc_mqe *mqe;
  4151. uint32_t data_length;
  4152. int rc;
  4153. /* Program the default value of vlan_id and fc_map */
  4154. phba->valid_vlan = 0;
  4155. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4156. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4157. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4158. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4159. if (!mboxq)
  4160. return -ENOMEM;
  4161. mqe = &mboxq->u.mqe;
  4162. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4163. rc = -ENOMEM;
  4164. goto out_free_mboxq;
  4165. }
  4166. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4167. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4168. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4169. "(%d):2571 Mailbox cmd x%x Status x%x "
  4170. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4171. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4172. "CQ: x%x x%x x%x x%x\n",
  4173. mboxq->vport ? mboxq->vport->vpi : 0,
  4174. bf_get(lpfc_mqe_command, mqe),
  4175. bf_get(lpfc_mqe_status, mqe),
  4176. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4177. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4178. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4179. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4180. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4181. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4182. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4183. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4184. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4185. mboxq->mcqe.word0,
  4186. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4187. mboxq->mcqe.trailer);
  4188. if (rc) {
  4189. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4190. kfree(mp);
  4191. rc = -EIO;
  4192. goto out_free_mboxq;
  4193. }
  4194. data_length = mqe->un.mb_words[5];
  4195. if (data_length > DMP_RGN23_SIZE) {
  4196. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4197. kfree(mp);
  4198. rc = -EIO;
  4199. goto out_free_mboxq;
  4200. }
  4201. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4202. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4203. kfree(mp);
  4204. rc = 0;
  4205. out_free_mboxq:
  4206. mempool_free(mboxq, phba->mbox_mem_pool);
  4207. return rc;
  4208. }
  4209. /**
  4210. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4211. * @phba: pointer to lpfc hba data structure.
  4212. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4213. * @vpd: pointer to the memory to hold resulting port vpd data.
  4214. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4215. * On output, the number of data bytes in @vpd.
  4216. *
  4217. * This routine executes a READ_REV SLI4 mailbox command. In
  4218. * addition, this routine gets the port vpd data.
  4219. *
  4220. * Return codes
  4221. * 0 - successful
  4222. * -ENOMEM - could not allocated memory.
  4223. **/
  4224. static int
  4225. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4226. uint8_t *vpd, uint32_t *vpd_size)
  4227. {
  4228. int rc = 0;
  4229. uint32_t dma_size;
  4230. struct lpfc_dmabuf *dmabuf;
  4231. struct lpfc_mqe *mqe;
  4232. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4233. if (!dmabuf)
  4234. return -ENOMEM;
  4235. /*
  4236. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4237. * mailbox command.
  4238. */
  4239. dma_size = *vpd_size;
  4240. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4241. dma_size,
  4242. &dmabuf->phys,
  4243. GFP_KERNEL);
  4244. if (!dmabuf->virt) {
  4245. kfree(dmabuf);
  4246. return -ENOMEM;
  4247. }
  4248. memset(dmabuf->virt, 0, dma_size);
  4249. /*
  4250. * The SLI4 implementation of READ_REV conflicts at word1,
  4251. * bits 31:16 and SLI4 adds vpd functionality not present
  4252. * in SLI3. This code corrects the conflicts.
  4253. */
  4254. lpfc_read_rev(phba, mboxq);
  4255. mqe = &mboxq->u.mqe;
  4256. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4257. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4258. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4259. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4260. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4261. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4262. if (rc) {
  4263. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4264. dmabuf->virt, dmabuf->phys);
  4265. kfree(dmabuf);
  4266. return -EIO;
  4267. }
  4268. /*
  4269. * The available vpd length cannot be bigger than the
  4270. * DMA buffer passed to the port. Catch the less than
  4271. * case and update the caller's size.
  4272. */
  4273. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4274. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4275. memcpy(vpd, dmabuf->virt, *vpd_size);
  4276. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4277. dmabuf->virt, dmabuf->phys);
  4278. kfree(dmabuf);
  4279. return 0;
  4280. }
  4281. /**
  4282. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4283. * @phba: pointer to lpfc hba data structure.
  4284. *
  4285. * This routine retrieves SLI4 device physical port name this PCI function
  4286. * is attached to.
  4287. *
  4288. * Return codes
  4289. * 0 - sucessful
  4290. * otherwise - failed to retrieve physical port name
  4291. **/
  4292. static int
  4293. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4294. {
  4295. LPFC_MBOXQ_t *mboxq;
  4296. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4297. struct lpfc_controller_attribute *cntl_attr;
  4298. struct lpfc_mbx_get_port_name *get_port_name;
  4299. void *virtaddr = NULL;
  4300. uint32_t alloclen, reqlen;
  4301. uint32_t shdr_status, shdr_add_status;
  4302. union lpfc_sli4_cfg_shdr *shdr;
  4303. char cport_name = 0;
  4304. int rc;
  4305. /* We assume nothing at this point */
  4306. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4307. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4308. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4309. if (!mboxq)
  4310. return -ENOMEM;
  4311. /* obtain link type and link number via READ_CONFIG */
  4312. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4313. lpfc_sli4_read_config(phba);
  4314. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4315. goto retrieve_ppname;
  4316. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4317. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4318. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4319. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4320. LPFC_SLI4_MBX_NEMBED);
  4321. if (alloclen < reqlen) {
  4322. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4323. "3084 Allocated DMA memory size (%d) is "
  4324. "less than the requested DMA memory size "
  4325. "(%d)\n", alloclen, reqlen);
  4326. rc = -ENOMEM;
  4327. goto out_free_mboxq;
  4328. }
  4329. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4330. virtaddr = mboxq->sge_array->addr[0];
  4331. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4332. shdr = &mbx_cntl_attr->cfg_shdr;
  4333. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4334. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4335. if (shdr_status || shdr_add_status || rc) {
  4336. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4337. "3085 Mailbox x%x (x%x/x%x) failed, "
  4338. "rc:x%x, status:x%x, add_status:x%x\n",
  4339. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4340. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4341. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4342. rc, shdr_status, shdr_add_status);
  4343. rc = -ENXIO;
  4344. goto out_free_mboxq;
  4345. }
  4346. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4347. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4348. phba->sli4_hba.lnk_info.lnk_tp =
  4349. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4350. phba->sli4_hba.lnk_info.lnk_no =
  4351. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4352. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4353. "3086 lnk_type:%d, lnk_numb:%d\n",
  4354. phba->sli4_hba.lnk_info.lnk_tp,
  4355. phba->sli4_hba.lnk_info.lnk_no);
  4356. retrieve_ppname:
  4357. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4358. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4359. sizeof(struct lpfc_mbx_get_port_name) -
  4360. sizeof(struct lpfc_sli4_cfg_mhdr),
  4361. LPFC_SLI4_MBX_EMBED);
  4362. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4363. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4364. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4365. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4366. phba->sli4_hba.lnk_info.lnk_tp);
  4367. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4368. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4369. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4370. if (shdr_status || shdr_add_status || rc) {
  4371. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4372. "3087 Mailbox x%x (x%x/x%x) failed: "
  4373. "rc:x%x, status:x%x, add_status:x%x\n",
  4374. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4375. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4376. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4377. rc, shdr_status, shdr_add_status);
  4378. rc = -ENXIO;
  4379. goto out_free_mboxq;
  4380. }
  4381. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4382. case LPFC_LINK_NUMBER_0:
  4383. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4384. &get_port_name->u.response);
  4385. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4386. break;
  4387. case LPFC_LINK_NUMBER_1:
  4388. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4389. &get_port_name->u.response);
  4390. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4391. break;
  4392. case LPFC_LINK_NUMBER_2:
  4393. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4394. &get_port_name->u.response);
  4395. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4396. break;
  4397. case LPFC_LINK_NUMBER_3:
  4398. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4399. &get_port_name->u.response);
  4400. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4401. break;
  4402. default:
  4403. break;
  4404. }
  4405. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4406. phba->Port[0] = cport_name;
  4407. phba->Port[1] = '\0';
  4408. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4409. "3091 SLI get port name: %s\n", phba->Port);
  4410. }
  4411. out_free_mboxq:
  4412. if (rc != MBX_TIMEOUT) {
  4413. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4414. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4415. else
  4416. mempool_free(mboxq, phba->mbox_mem_pool);
  4417. }
  4418. return rc;
  4419. }
  4420. /**
  4421. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4422. * @phba: pointer to lpfc hba data structure.
  4423. *
  4424. * This routine is called to explicitly arm the SLI4 device's completion and
  4425. * event queues
  4426. **/
  4427. static void
  4428. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4429. {
  4430. uint8_t fcp_eqidx;
  4431. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4432. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4433. fcp_eqidx = 0;
  4434. if (phba->sli4_hba.fcp_cq) {
  4435. do
  4436. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4437. LPFC_QUEUE_REARM);
  4438. while (++fcp_eqidx < phba->cfg_fcp_eq_count);
  4439. }
  4440. lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
  4441. if (phba->sli4_hba.fp_eq) {
  4442. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
  4443. fcp_eqidx++)
  4444. lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
  4445. LPFC_QUEUE_REARM);
  4446. }
  4447. }
  4448. /**
  4449. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4450. * @phba: Pointer to HBA context object.
  4451. * @type: The resource extent type.
  4452. * @extnt_count: buffer to hold port available extent count.
  4453. * @extnt_size: buffer to hold element count per extent.
  4454. *
  4455. * This function calls the port and retrievs the number of available
  4456. * extents and their size for a particular extent type.
  4457. *
  4458. * Returns: 0 if successful. Nonzero otherwise.
  4459. **/
  4460. int
  4461. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4462. uint16_t *extnt_count, uint16_t *extnt_size)
  4463. {
  4464. int rc = 0;
  4465. uint32_t length;
  4466. uint32_t mbox_tmo;
  4467. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4468. LPFC_MBOXQ_t *mbox;
  4469. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4470. if (!mbox)
  4471. return -ENOMEM;
  4472. /* Find out how many extents are available for this resource type */
  4473. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4474. sizeof(struct lpfc_sli4_cfg_mhdr));
  4475. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4476. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4477. length, LPFC_SLI4_MBX_EMBED);
  4478. /* Send an extents count of 0 - the GET doesn't use it. */
  4479. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4480. LPFC_SLI4_MBX_EMBED);
  4481. if (unlikely(rc)) {
  4482. rc = -EIO;
  4483. goto err_exit;
  4484. }
  4485. if (!phba->sli4_hba.intr_enable)
  4486. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4487. else {
  4488. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4489. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4490. }
  4491. if (unlikely(rc)) {
  4492. rc = -EIO;
  4493. goto err_exit;
  4494. }
  4495. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4496. if (bf_get(lpfc_mbox_hdr_status,
  4497. &rsrc_info->header.cfg_shdr.response)) {
  4498. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4499. "2930 Failed to get resource extents "
  4500. "Status 0x%x Add'l Status 0x%x\n",
  4501. bf_get(lpfc_mbox_hdr_status,
  4502. &rsrc_info->header.cfg_shdr.response),
  4503. bf_get(lpfc_mbox_hdr_add_status,
  4504. &rsrc_info->header.cfg_shdr.response));
  4505. rc = -EIO;
  4506. goto err_exit;
  4507. }
  4508. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4509. &rsrc_info->u.rsp);
  4510. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4511. &rsrc_info->u.rsp);
  4512. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4513. "3162 Retrieved extents type-%d from port: count:%d, "
  4514. "size:%d\n", type, *extnt_count, *extnt_size);
  4515. err_exit:
  4516. mempool_free(mbox, phba->mbox_mem_pool);
  4517. return rc;
  4518. }
  4519. /**
  4520. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4521. * @phba: Pointer to HBA context object.
  4522. * @type: The extent type to check.
  4523. *
  4524. * This function reads the current available extents from the port and checks
  4525. * if the extent count or extent size has changed since the last access.
  4526. * Callers use this routine post port reset to understand if there is a
  4527. * extent reprovisioning requirement.
  4528. *
  4529. * Returns:
  4530. * -Error: error indicates problem.
  4531. * 1: Extent count or size has changed.
  4532. * 0: No changes.
  4533. **/
  4534. static int
  4535. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4536. {
  4537. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4538. uint16_t size_diff, rsrc_ext_size;
  4539. int rc = 0;
  4540. struct lpfc_rsrc_blks *rsrc_entry;
  4541. struct list_head *rsrc_blk_list = NULL;
  4542. size_diff = 0;
  4543. curr_ext_cnt = 0;
  4544. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4545. &rsrc_ext_cnt,
  4546. &rsrc_ext_size);
  4547. if (unlikely(rc))
  4548. return -EIO;
  4549. switch (type) {
  4550. case LPFC_RSC_TYPE_FCOE_RPI:
  4551. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4552. break;
  4553. case LPFC_RSC_TYPE_FCOE_VPI:
  4554. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4555. break;
  4556. case LPFC_RSC_TYPE_FCOE_XRI:
  4557. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4558. break;
  4559. case LPFC_RSC_TYPE_FCOE_VFI:
  4560. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4561. break;
  4562. default:
  4563. break;
  4564. }
  4565. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4566. curr_ext_cnt++;
  4567. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4568. size_diff++;
  4569. }
  4570. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4571. rc = 1;
  4572. return rc;
  4573. }
  4574. /**
  4575. * lpfc_sli4_cfg_post_extnts -
  4576. * @phba: Pointer to HBA context object.
  4577. * @extnt_cnt - number of available extents.
  4578. * @type - the extent type (rpi, xri, vfi, vpi).
  4579. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4580. * @mbox - pointer to the caller's allocated mailbox structure.
  4581. *
  4582. * This function executes the extents allocation request. It also
  4583. * takes care of the amount of memory needed to allocate or get the
  4584. * allocated extents. It is the caller's responsibility to evaluate
  4585. * the response.
  4586. *
  4587. * Returns:
  4588. * -Error: Error value describes the condition found.
  4589. * 0: if successful
  4590. **/
  4591. static int
  4592. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  4593. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4594. {
  4595. int rc = 0;
  4596. uint32_t req_len;
  4597. uint32_t emb_len;
  4598. uint32_t alloc_len, mbox_tmo;
  4599. /* Calculate the total requested length of the dma memory */
  4600. req_len = extnt_cnt * sizeof(uint16_t);
  4601. /*
  4602. * Calculate the size of an embedded mailbox. The uint32_t
  4603. * accounts for extents-specific word.
  4604. */
  4605. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4606. sizeof(uint32_t);
  4607. /*
  4608. * Presume the allocation and response will fit into an embedded
  4609. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4610. */
  4611. *emb = LPFC_SLI4_MBX_EMBED;
  4612. if (req_len > emb_len) {
  4613. req_len = extnt_cnt * sizeof(uint16_t) +
  4614. sizeof(union lpfc_sli4_cfg_shdr) +
  4615. sizeof(uint32_t);
  4616. *emb = LPFC_SLI4_MBX_NEMBED;
  4617. }
  4618. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4619. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4620. req_len, *emb);
  4621. if (alloc_len < req_len) {
  4622. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4623. "2982 Allocated DMA memory size (x%x) is "
  4624. "less than the requested DMA memory "
  4625. "size (x%x)\n", alloc_len, req_len);
  4626. return -ENOMEM;
  4627. }
  4628. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  4629. if (unlikely(rc))
  4630. return -EIO;
  4631. if (!phba->sli4_hba.intr_enable)
  4632. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4633. else {
  4634. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4635. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4636. }
  4637. if (unlikely(rc))
  4638. rc = -EIO;
  4639. return rc;
  4640. }
  4641. /**
  4642. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4643. * @phba: Pointer to HBA context object.
  4644. * @type: The resource extent type to allocate.
  4645. *
  4646. * This function allocates the number of elements for the specified
  4647. * resource type.
  4648. **/
  4649. static int
  4650. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4651. {
  4652. bool emb = false;
  4653. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4654. uint16_t rsrc_id, rsrc_start, j, k;
  4655. uint16_t *ids;
  4656. int i, rc;
  4657. unsigned long longs;
  4658. unsigned long *bmask;
  4659. struct lpfc_rsrc_blks *rsrc_blks;
  4660. LPFC_MBOXQ_t *mbox;
  4661. uint32_t length;
  4662. struct lpfc_id_range *id_array = NULL;
  4663. void *virtaddr = NULL;
  4664. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4665. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4666. struct list_head *ext_blk_list;
  4667. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4668. &rsrc_cnt,
  4669. &rsrc_size);
  4670. if (unlikely(rc))
  4671. return -EIO;
  4672. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4673. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4674. "3009 No available Resource Extents "
  4675. "for resource type 0x%x: Count: 0x%x, "
  4676. "Size 0x%x\n", type, rsrc_cnt,
  4677. rsrc_size);
  4678. return -ENOMEM;
  4679. }
  4680. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  4681. "2903 Post resource extents type-0x%x: "
  4682. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  4683. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4684. if (!mbox)
  4685. return -ENOMEM;
  4686. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  4687. if (unlikely(rc)) {
  4688. rc = -EIO;
  4689. goto err_exit;
  4690. }
  4691. /*
  4692. * Figure out where the response is located. Then get local pointers
  4693. * to the response data. The port does not guarantee to respond to
  4694. * all extents counts request so update the local variable with the
  4695. * allocated count from the port.
  4696. */
  4697. if (emb == LPFC_SLI4_MBX_EMBED) {
  4698. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4699. id_array = &rsrc_ext->u.rsp.id[0];
  4700. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4701. } else {
  4702. virtaddr = mbox->sge_array->addr[0];
  4703. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4704. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4705. id_array = &n_rsrc->id;
  4706. }
  4707. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4708. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4709. /*
  4710. * Based on the resource size and count, correct the base and max
  4711. * resource values.
  4712. */
  4713. length = sizeof(struct lpfc_rsrc_blks);
  4714. switch (type) {
  4715. case LPFC_RSC_TYPE_FCOE_RPI:
  4716. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4717. sizeof(unsigned long),
  4718. GFP_KERNEL);
  4719. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4720. rc = -ENOMEM;
  4721. goto err_exit;
  4722. }
  4723. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4724. sizeof(uint16_t),
  4725. GFP_KERNEL);
  4726. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4727. kfree(phba->sli4_hba.rpi_bmask);
  4728. rc = -ENOMEM;
  4729. goto err_exit;
  4730. }
  4731. /*
  4732. * The next_rpi was initialized with the maximum available
  4733. * count but the port may allocate a smaller number. Catch
  4734. * that case and update the next_rpi.
  4735. */
  4736. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4737. /* Initialize local ptrs for common extent processing later. */
  4738. bmask = phba->sli4_hba.rpi_bmask;
  4739. ids = phba->sli4_hba.rpi_ids;
  4740. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4741. break;
  4742. case LPFC_RSC_TYPE_FCOE_VPI:
  4743. phba->vpi_bmask = kzalloc(longs *
  4744. sizeof(unsigned long),
  4745. GFP_KERNEL);
  4746. if (unlikely(!phba->vpi_bmask)) {
  4747. rc = -ENOMEM;
  4748. goto err_exit;
  4749. }
  4750. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4751. sizeof(uint16_t),
  4752. GFP_KERNEL);
  4753. if (unlikely(!phba->vpi_ids)) {
  4754. kfree(phba->vpi_bmask);
  4755. rc = -ENOMEM;
  4756. goto err_exit;
  4757. }
  4758. /* Initialize local ptrs for common extent processing later. */
  4759. bmask = phba->vpi_bmask;
  4760. ids = phba->vpi_ids;
  4761. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4762. break;
  4763. case LPFC_RSC_TYPE_FCOE_XRI:
  4764. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4765. sizeof(unsigned long),
  4766. GFP_KERNEL);
  4767. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4768. rc = -ENOMEM;
  4769. goto err_exit;
  4770. }
  4771. phba->sli4_hba.max_cfg_param.xri_used = 0;
  4772. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4773. sizeof(uint16_t),
  4774. GFP_KERNEL);
  4775. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4776. kfree(phba->sli4_hba.xri_bmask);
  4777. rc = -ENOMEM;
  4778. goto err_exit;
  4779. }
  4780. /* Initialize local ptrs for common extent processing later. */
  4781. bmask = phba->sli4_hba.xri_bmask;
  4782. ids = phba->sli4_hba.xri_ids;
  4783. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4784. break;
  4785. case LPFC_RSC_TYPE_FCOE_VFI:
  4786. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4787. sizeof(unsigned long),
  4788. GFP_KERNEL);
  4789. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4790. rc = -ENOMEM;
  4791. goto err_exit;
  4792. }
  4793. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4794. sizeof(uint16_t),
  4795. GFP_KERNEL);
  4796. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4797. kfree(phba->sli4_hba.vfi_bmask);
  4798. rc = -ENOMEM;
  4799. goto err_exit;
  4800. }
  4801. /* Initialize local ptrs for common extent processing later. */
  4802. bmask = phba->sli4_hba.vfi_bmask;
  4803. ids = phba->sli4_hba.vfi_ids;
  4804. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4805. break;
  4806. default:
  4807. /* Unsupported Opcode. Fail call. */
  4808. id_array = NULL;
  4809. bmask = NULL;
  4810. ids = NULL;
  4811. ext_blk_list = NULL;
  4812. goto err_exit;
  4813. }
  4814. /*
  4815. * Complete initializing the extent configuration with the
  4816. * allocated ids assigned to this function. The bitmask serves
  4817. * as an index into the array and manages the available ids. The
  4818. * array just stores the ids communicated to the port via the wqes.
  4819. */
  4820. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4821. if ((i % 2) == 0)
  4822. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4823. &id_array[k]);
  4824. else
  4825. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4826. &id_array[k]);
  4827. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4828. if (unlikely(!rsrc_blks)) {
  4829. rc = -ENOMEM;
  4830. kfree(bmask);
  4831. kfree(ids);
  4832. goto err_exit;
  4833. }
  4834. rsrc_blks->rsrc_start = rsrc_id;
  4835. rsrc_blks->rsrc_size = rsrc_size;
  4836. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4837. rsrc_start = rsrc_id;
  4838. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4839. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4840. lpfc_sli4_get_els_iocb_cnt(phba);
  4841. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4842. ids[j] = rsrc_id;
  4843. rsrc_id++;
  4844. j++;
  4845. }
  4846. /* Entire word processed. Get next word.*/
  4847. if ((i % 2) == 1)
  4848. k++;
  4849. }
  4850. err_exit:
  4851. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4852. return rc;
  4853. }
  4854. /**
  4855. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4856. * @phba: Pointer to HBA context object.
  4857. * @type: the extent's type.
  4858. *
  4859. * This function deallocates all extents of a particular resource type.
  4860. * SLI4 does not allow for deallocating a particular extent range. It
  4861. * is the caller's responsibility to release all kernel memory resources.
  4862. **/
  4863. static int
  4864. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4865. {
  4866. int rc;
  4867. uint32_t length, mbox_tmo = 0;
  4868. LPFC_MBOXQ_t *mbox;
  4869. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4870. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4871. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4872. if (!mbox)
  4873. return -ENOMEM;
  4874. /*
  4875. * This function sends an embedded mailbox because it only sends the
  4876. * the resource type. All extents of this type are released by the
  4877. * port.
  4878. */
  4879. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4880. sizeof(struct lpfc_sli4_cfg_mhdr));
  4881. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4882. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4883. length, LPFC_SLI4_MBX_EMBED);
  4884. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4885. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4886. LPFC_SLI4_MBX_EMBED);
  4887. if (unlikely(rc)) {
  4888. rc = -EIO;
  4889. goto out_free_mbox;
  4890. }
  4891. if (!phba->sli4_hba.intr_enable)
  4892. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4893. else {
  4894. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4895. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4896. }
  4897. if (unlikely(rc)) {
  4898. rc = -EIO;
  4899. goto out_free_mbox;
  4900. }
  4901. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4902. if (bf_get(lpfc_mbox_hdr_status,
  4903. &dealloc_rsrc->header.cfg_shdr.response)) {
  4904. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4905. "2919 Failed to release resource extents "
  4906. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4907. "Resource memory not released.\n",
  4908. type,
  4909. bf_get(lpfc_mbox_hdr_status,
  4910. &dealloc_rsrc->header.cfg_shdr.response),
  4911. bf_get(lpfc_mbox_hdr_add_status,
  4912. &dealloc_rsrc->header.cfg_shdr.response));
  4913. rc = -EIO;
  4914. goto out_free_mbox;
  4915. }
  4916. /* Release kernel memory resources for the specific type. */
  4917. switch (type) {
  4918. case LPFC_RSC_TYPE_FCOE_VPI:
  4919. kfree(phba->vpi_bmask);
  4920. kfree(phba->vpi_ids);
  4921. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4922. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4923. &phba->lpfc_vpi_blk_list, list) {
  4924. list_del_init(&rsrc_blk->list);
  4925. kfree(rsrc_blk);
  4926. }
  4927. break;
  4928. case LPFC_RSC_TYPE_FCOE_XRI:
  4929. kfree(phba->sli4_hba.xri_bmask);
  4930. kfree(phba->sli4_hba.xri_ids);
  4931. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4932. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4933. list_del_init(&rsrc_blk->list);
  4934. kfree(rsrc_blk);
  4935. }
  4936. break;
  4937. case LPFC_RSC_TYPE_FCOE_VFI:
  4938. kfree(phba->sli4_hba.vfi_bmask);
  4939. kfree(phba->sli4_hba.vfi_ids);
  4940. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4941. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4942. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  4943. list_del_init(&rsrc_blk->list);
  4944. kfree(rsrc_blk);
  4945. }
  4946. break;
  4947. case LPFC_RSC_TYPE_FCOE_RPI:
  4948. /* RPI bitmask and physical id array are cleaned up earlier. */
  4949. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4950. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  4951. list_del_init(&rsrc_blk->list);
  4952. kfree(rsrc_blk);
  4953. }
  4954. break;
  4955. default:
  4956. break;
  4957. }
  4958. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4959. out_free_mbox:
  4960. mempool_free(mbox, phba->mbox_mem_pool);
  4961. return rc;
  4962. }
  4963. /**
  4964. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  4965. * @phba: Pointer to HBA context object.
  4966. *
  4967. * This function allocates all SLI4 resource identifiers.
  4968. **/
  4969. int
  4970. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  4971. {
  4972. int i, rc, error = 0;
  4973. uint16_t count, base;
  4974. unsigned long longs;
  4975. if (!phba->sli4_hba.rpi_hdrs_in_use)
  4976. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  4977. if (phba->sli4_hba.extents_in_use) {
  4978. /*
  4979. * The port supports resource extents. The XRI, VPI, VFI, RPI
  4980. * resource extent count must be read and allocated before
  4981. * provisioning the resource id arrays.
  4982. */
  4983. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  4984. LPFC_IDX_RSRC_RDY) {
  4985. /*
  4986. * Extent-based resources are set - the driver could
  4987. * be in a port reset. Figure out if any corrective
  4988. * actions need to be taken.
  4989. */
  4990. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4991. LPFC_RSC_TYPE_FCOE_VFI);
  4992. if (rc != 0)
  4993. error++;
  4994. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4995. LPFC_RSC_TYPE_FCOE_VPI);
  4996. if (rc != 0)
  4997. error++;
  4998. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4999. LPFC_RSC_TYPE_FCOE_XRI);
  5000. if (rc != 0)
  5001. error++;
  5002. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5003. LPFC_RSC_TYPE_FCOE_RPI);
  5004. if (rc != 0)
  5005. error++;
  5006. /*
  5007. * It's possible that the number of resources
  5008. * provided to this port instance changed between
  5009. * resets. Detect this condition and reallocate
  5010. * resources. Otherwise, there is no action.
  5011. */
  5012. if (error) {
  5013. lpfc_printf_log(phba, KERN_INFO,
  5014. LOG_MBOX | LOG_INIT,
  5015. "2931 Detected extent resource "
  5016. "change. Reallocating all "
  5017. "extents.\n");
  5018. rc = lpfc_sli4_dealloc_extent(phba,
  5019. LPFC_RSC_TYPE_FCOE_VFI);
  5020. rc = lpfc_sli4_dealloc_extent(phba,
  5021. LPFC_RSC_TYPE_FCOE_VPI);
  5022. rc = lpfc_sli4_dealloc_extent(phba,
  5023. LPFC_RSC_TYPE_FCOE_XRI);
  5024. rc = lpfc_sli4_dealloc_extent(phba,
  5025. LPFC_RSC_TYPE_FCOE_RPI);
  5026. } else
  5027. return 0;
  5028. }
  5029. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5030. if (unlikely(rc))
  5031. goto err_exit;
  5032. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5033. if (unlikely(rc))
  5034. goto err_exit;
  5035. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5036. if (unlikely(rc))
  5037. goto err_exit;
  5038. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5039. if (unlikely(rc))
  5040. goto err_exit;
  5041. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5042. LPFC_IDX_RSRC_RDY);
  5043. return rc;
  5044. } else {
  5045. /*
  5046. * The port does not support resource extents. The XRI, VPI,
  5047. * VFI, RPI resource ids were determined from READ_CONFIG.
  5048. * Just allocate the bitmasks and provision the resource id
  5049. * arrays. If a port reset is active, the resources don't
  5050. * need any action - just exit.
  5051. */
  5052. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5053. LPFC_IDX_RSRC_RDY) {
  5054. lpfc_sli4_dealloc_resource_identifiers(phba);
  5055. lpfc_sli4_remove_rpis(phba);
  5056. }
  5057. /* RPIs. */
  5058. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5059. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5060. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5061. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5062. sizeof(unsigned long),
  5063. GFP_KERNEL);
  5064. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5065. rc = -ENOMEM;
  5066. goto err_exit;
  5067. }
  5068. phba->sli4_hba.rpi_ids = kzalloc(count *
  5069. sizeof(uint16_t),
  5070. GFP_KERNEL);
  5071. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5072. rc = -ENOMEM;
  5073. goto free_rpi_bmask;
  5074. }
  5075. for (i = 0; i < count; i++)
  5076. phba->sli4_hba.rpi_ids[i] = base + i;
  5077. /* VPIs. */
  5078. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5079. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5080. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5081. phba->vpi_bmask = kzalloc(longs *
  5082. sizeof(unsigned long),
  5083. GFP_KERNEL);
  5084. if (unlikely(!phba->vpi_bmask)) {
  5085. rc = -ENOMEM;
  5086. goto free_rpi_ids;
  5087. }
  5088. phba->vpi_ids = kzalloc(count *
  5089. sizeof(uint16_t),
  5090. GFP_KERNEL);
  5091. if (unlikely(!phba->vpi_ids)) {
  5092. rc = -ENOMEM;
  5093. goto free_vpi_bmask;
  5094. }
  5095. for (i = 0; i < count; i++)
  5096. phba->vpi_ids[i] = base + i;
  5097. /* XRIs. */
  5098. count = phba->sli4_hba.max_cfg_param.max_xri;
  5099. base = phba->sli4_hba.max_cfg_param.xri_base;
  5100. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5101. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5102. sizeof(unsigned long),
  5103. GFP_KERNEL);
  5104. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5105. rc = -ENOMEM;
  5106. goto free_vpi_ids;
  5107. }
  5108. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5109. phba->sli4_hba.xri_ids = kzalloc(count *
  5110. sizeof(uint16_t),
  5111. GFP_KERNEL);
  5112. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5113. rc = -ENOMEM;
  5114. goto free_xri_bmask;
  5115. }
  5116. for (i = 0; i < count; i++)
  5117. phba->sli4_hba.xri_ids[i] = base + i;
  5118. /* VFIs. */
  5119. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5120. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5121. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5122. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5123. sizeof(unsigned long),
  5124. GFP_KERNEL);
  5125. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5126. rc = -ENOMEM;
  5127. goto free_xri_ids;
  5128. }
  5129. phba->sli4_hba.vfi_ids = kzalloc(count *
  5130. sizeof(uint16_t),
  5131. GFP_KERNEL);
  5132. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5133. rc = -ENOMEM;
  5134. goto free_vfi_bmask;
  5135. }
  5136. for (i = 0; i < count; i++)
  5137. phba->sli4_hba.vfi_ids[i] = base + i;
  5138. /*
  5139. * Mark all resources ready. An HBA reset doesn't need
  5140. * to reset the initialization.
  5141. */
  5142. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5143. LPFC_IDX_RSRC_RDY);
  5144. return 0;
  5145. }
  5146. free_vfi_bmask:
  5147. kfree(phba->sli4_hba.vfi_bmask);
  5148. free_xri_ids:
  5149. kfree(phba->sli4_hba.xri_ids);
  5150. free_xri_bmask:
  5151. kfree(phba->sli4_hba.xri_bmask);
  5152. free_vpi_ids:
  5153. kfree(phba->vpi_ids);
  5154. free_vpi_bmask:
  5155. kfree(phba->vpi_bmask);
  5156. free_rpi_ids:
  5157. kfree(phba->sli4_hba.rpi_ids);
  5158. free_rpi_bmask:
  5159. kfree(phba->sli4_hba.rpi_bmask);
  5160. err_exit:
  5161. return rc;
  5162. }
  5163. /**
  5164. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5165. * @phba: Pointer to HBA context object.
  5166. *
  5167. * This function allocates the number of elements for the specified
  5168. * resource type.
  5169. **/
  5170. int
  5171. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5172. {
  5173. if (phba->sli4_hba.extents_in_use) {
  5174. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5175. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5176. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5177. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5178. } else {
  5179. kfree(phba->vpi_bmask);
  5180. kfree(phba->vpi_ids);
  5181. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5182. kfree(phba->sli4_hba.xri_bmask);
  5183. kfree(phba->sli4_hba.xri_ids);
  5184. kfree(phba->sli4_hba.vfi_bmask);
  5185. kfree(phba->sli4_hba.vfi_ids);
  5186. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5187. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5188. }
  5189. return 0;
  5190. }
  5191. /**
  5192. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5193. * @phba: Pointer to HBA context object.
  5194. * @type: The resource extent type.
  5195. * @extnt_count: buffer to hold port extent count response
  5196. * @extnt_size: buffer to hold port extent size response.
  5197. *
  5198. * This function calls the port to read the host allocated extents
  5199. * for a particular type.
  5200. **/
  5201. int
  5202. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5203. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5204. {
  5205. bool emb;
  5206. int rc = 0;
  5207. uint16_t curr_blks = 0;
  5208. uint32_t req_len, emb_len;
  5209. uint32_t alloc_len, mbox_tmo;
  5210. struct list_head *blk_list_head;
  5211. struct lpfc_rsrc_blks *rsrc_blk;
  5212. LPFC_MBOXQ_t *mbox;
  5213. void *virtaddr = NULL;
  5214. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5215. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5216. union lpfc_sli4_cfg_shdr *shdr;
  5217. switch (type) {
  5218. case LPFC_RSC_TYPE_FCOE_VPI:
  5219. blk_list_head = &phba->lpfc_vpi_blk_list;
  5220. break;
  5221. case LPFC_RSC_TYPE_FCOE_XRI:
  5222. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5223. break;
  5224. case LPFC_RSC_TYPE_FCOE_VFI:
  5225. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5226. break;
  5227. case LPFC_RSC_TYPE_FCOE_RPI:
  5228. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5229. break;
  5230. default:
  5231. return -EIO;
  5232. }
  5233. /* Count the number of extents currently allocatd for this type. */
  5234. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5235. if (curr_blks == 0) {
  5236. /*
  5237. * The GET_ALLOCATED mailbox does not return the size,
  5238. * just the count. The size should be just the size
  5239. * stored in the current allocated block and all sizes
  5240. * for an extent type are the same so set the return
  5241. * value now.
  5242. */
  5243. *extnt_size = rsrc_blk->rsrc_size;
  5244. }
  5245. curr_blks++;
  5246. }
  5247. /* Calculate the total requested length of the dma memory. */
  5248. req_len = curr_blks * sizeof(uint16_t);
  5249. /*
  5250. * Calculate the size of an embedded mailbox. The uint32_t
  5251. * accounts for extents-specific word.
  5252. */
  5253. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5254. sizeof(uint32_t);
  5255. /*
  5256. * Presume the allocation and response will fit into an embedded
  5257. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5258. */
  5259. emb = LPFC_SLI4_MBX_EMBED;
  5260. req_len = emb_len;
  5261. if (req_len > emb_len) {
  5262. req_len = curr_blks * sizeof(uint16_t) +
  5263. sizeof(union lpfc_sli4_cfg_shdr) +
  5264. sizeof(uint32_t);
  5265. emb = LPFC_SLI4_MBX_NEMBED;
  5266. }
  5267. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5268. if (!mbox)
  5269. return -ENOMEM;
  5270. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5271. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5272. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5273. req_len, emb);
  5274. if (alloc_len < req_len) {
  5275. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5276. "2983 Allocated DMA memory size (x%x) is "
  5277. "less than the requested DMA memory "
  5278. "size (x%x)\n", alloc_len, req_len);
  5279. rc = -ENOMEM;
  5280. goto err_exit;
  5281. }
  5282. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5283. if (unlikely(rc)) {
  5284. rc = -EIO;
  5285. goto err_exit;
  5286. }
  5287. if (!phba->sli4_hba.intr_enable)
  5288. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5289. else {
  5290. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5291. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5292. }
  5293. if (unlikely(rc)) {
  5294. rc = -EIO;
  5295. goto err_exit;
  5296. }
  5297. /*
  5298. * Figure out where the response is located. Then get local pointers
  5299. * to the response data. The port does not guarantee to respond to
  5300. * all extents counts request so update the local variable with the
  5301. * allocated count from the port.
  5302. */
  5303. if (emb == LPFC_SLI4_MBX_EMBED) {
  5304. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5305. shdr = &rsrc_ext->header.cfg_shdr;
  5306. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5307. } else {
  5308. virtaddr = mbox->sge_array->addr[0];
  5309. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5310. shdr = &n_rsrc->cfg_shdr;
  5311. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5312. }
  5313. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5314. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5315. "2984 Failed to read allocated resources "
  5316. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5317. type,
  5318. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5319. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5320. rc = -EIO;
  5321. goto err_exit;
  5322. }
  5323. err_exit:
  5324. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5325. return rc;
  5326. }
  5327. /**
  5328. * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
  5329. * @phba: pointer to lpfc hba data structure.
  5330. *
  5331. * This routine walks the list of els buffers that have been allocated and
  5332. * repost them to the port by using SGL block post. This is needed after a
  5333. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5334. * of els buffer sgls which contains contiguous xris and uses the non-embedded
  5335. * SGL block post mailbox commands to post them to the port. For single els
  5336. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5337. * mailbox command for posting.
  5338. *
  5339. * Returns: 0 = success, non-zero failure.
  5340. **/
  5341. static int
  5342. lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
  5343. {
  5344. struct lpfc_sglq *sglq_entry = NULL;
  5345. struct lpfc_sglq *sglq_entry_next = NULL;
  5346. struct lpfc_sglq *sglq_entry_first = NULL;
  5347. int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
  5348. int last_xritag = NO_XRI;
  5349. LIST_HEAD(prep_sgl_list);
  5350. LIST_HEAD(blck_sgl_list);
  5351. LIST_HEAD(allc_sgl_list);
  5352. LIST_HEAD(post_sgl_list);
  5353. LIST_HEAD(free_sgl_list);
  5354. spin_lock(&phba->hbalock);
  5355. list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
  5356. spin_unlock(&phba->hbalock);
  5357. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  5358. &allc_sgl_list, list) {
  5359. list_del_init(&sglq_entry->list);
  5360. block_cnt++;
  5361. if ((last_xritag != NO_XRI) &&
  5362. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  5363. /* a hole in xri block, form a sgl posting block */
  5364. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  5365. post_cnt = block_cnt - 1;
  5366. /* prepare list for next posting block */
  5367. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5368. block_cnt = 1;
  5369. } else {
  5370. /* prepare list for next posting block */
  5371. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5372. /* enough sgls for non-embed sgl mbox command */
  5373. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  5374. list_splice_init(&prep_sgl_list,
  5375. &blck_sgl_list);
  5376. post_cnt = block_cnt;
  5377. block_cnt = 0;
  5378. }
  5379. }
  5380. num_posted++;
  5381. /* keep track of last sgl's xritag */
  5382. last_xritag = sglq_entry->sli4_xritag;
  5383. /* end of repost sgl list condition for els buffers */
  5384. if (num_posted == phba->sli4_hba.els_xri_cnt) {
  5385. if (post_cnt == 0) {
  5386. list_splice_init(&prep_sgl_list,
  5387. &blck_sgl_list);
  5388. post_cnt = block_cnt;
  5389. } else if (block_cnt == 1) {
  5390. status = lpfc_sli4_post_sgl(phba,
  5391. sglq_entry->phys, 0,
  5392. sglq_entry->sli4_xritag);
  5393. if (!status) {
  5394. /* successful, put sgl to posted list */
  5395. list_add_tail(&sglq_entry->list,
  5396. &post_sgl_list);
  5397. } else {
  5398. /* Failure, put sgl to free list */
  5399. lpfc_printf_log(phba, KERN_WARNING,
  5400. LOG_SLI,
  5401. "3159 Failed to post els "
  5402. "sgl, xritag:x%x\n",
  5403. sglq_entry->sli4_xritag);
  5404. list_add_tail(&sglq_entry->list,
  5405. &free_sgl_list);
  5406. spin_lock_irq(&phba->hbalock);
  5407. phba->sli4_hba.els_xri_cnt--;
  5408. spin_unlock_irq(&phba->hbalock);
  5409. }
  5410. }
  5411. }
  5412. /* continue until a nembed page worth of sgls */
  5413. if (post_cnt == 0)
  5414. continue;
  5415. /* post the els buffer list sgls as a block */
  5416. status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
  5417. post_cnt);
  5418. if (!status) {
  5419. /* success, put sgl list to posted sgl list */
  5420. list_splice_init(&blck_sgl_list, &post_sgl_list);
  5421. } else {
  5422. /* Failure, put sgl list to free sgl list */
  5423. sglq_entry_first = list_first_entry(&blck_sgl_list,
  5424. struct lpfc_sglq,
  5425. list);
  5426. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5427. "3160 Failed to post els sgl-list, "
  5428. "xritag:x%x-x%x\n",
  5429. sglq_entry_first->sli4_xritag,
  5430. (sglq_entry_first->sli4_xritag +
  5431. post_cnt - 1));
  5432. list_splice_init(&blck_sgl_list, &free_sgl_list);
  5433. spin_lock_irq(&phba->hbalock);
  5434. phba->sli4_hba.els_xri_cnt -= post_cnt;
  5435. spin_unlock_irq(&phba->hbalock);
  5436. }
  5437. /* don't reset xirtag due to hole in xri block */
  5438. if (block_cnt == 0)
  5439. last_xritag = NO_XRI;
  5440. /* reset els sgl post count for next round of posting */
  5441. post_cnt = 0;
  5442. }
  5443. /* free the els sgls failed to post */
  5444. lpfc_free_sgl_list(phba, &free_sgl_list);
  5445. /* push els sgls posted to the availble list */
  5446. if (!list_empty(&post_sgl_list)) {
  5447. spin_lock(&phba->hbalock);
  5448. list_splice_init(&post_sgl_list,
  5449. &phba->sli4_hba.lpfc_sgl_list);
  5450. spin_unlock(&phba->hbalock);
  5451. } else {
  5452. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5453. "3161 Failure to post els sgl to port.\n");
  5454. return -EIO;
  5455. }
  5456. return 0;
  5457. }
  5458. /**
  5459. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5460. * @phba: Pointer to HBA context object.
  5461. *
  5462. * This function is the main SLI4 device intialization PCI function. This
  5463. * function is called by the HBA intialization code, HBA reset code and
  5464. * HBA error attention handler code. Caller is not required to hold any
  5465. * locks.
  5466. **/
  5467. int
  5468. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5469. {
  5470. int rc;
  5471. LPFC_MBOXQ_t *mboxq;
  5472. struct lpfc_mqe *mqe;
  5473. uint8_t *vpd;
  5474. uint32_t vpd_size;
  5475. uint32_t ftr_rsp = 0;
  5476. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5477. struct lpfc_vport *vport = phba->pport;
  5478. struct lpfc_dmabuf *mp;
  5479. /* Perform a PCI function reset to start from clean */
  5480. rc = lpfc_pci_function_reset(phba);
  5481. if (unlikely(rc))
  5482. return -ENODEV;
  5483. /* Check the HBA Host Status Register for readyness */
  5484. rc = lpfc_sli4_post_status_check(phba);
  5485. if (unlikely(rc))
  5486. return -ENODEV;
  5487. else {
  5488. spin_lock_irq(&phba->hbalock);
  5489. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5490. spin_unlock_irq(&phba->hbalock);
  5491. }
  5492. /*
  5493. * Allocate a single mailbox container for initializing the
  5494. * port.
  5495. */
  5496. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5497. if (!mboxq)
  5498. return -ENOMEM;
  5499. /* Issue READ_REV to collect vpd and FW information. */
  5500. vpd_size = SLI4_PAGE_SIZE;
  5501. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5502. if (!vpd) {
  5503. rc = -ENOMEM;
  5504. goto out_free_mbox;
  5505. }
  5506. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5507. if (unlikely(rc)) {
  5508. kfree(vpd);
  5509. goto out_free_mbox;
  5510. }
  5511. mqe = &mboxq->u.mqe;
  5512. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5513. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5514. phba->hba_flag |= HBA_FCOE_MODE;
  5515. else
  5516. phba->hba_flag &= ~HBA_FCOE_MODE;
  5517. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5518. LPFC_DCBX_CEE_MODE)
  5519. phba->hba_flag |= HBA_FIP_SUPPORT;
  5520. else
  5521. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5522. phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
  5523. if (phba->sli_rev != LPFC_SLI_REV4) {
  5524. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5525. "0376 READ_REV Error. SLI Level %d "
  5526. "FCoE enabled %d\n",
  5527. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5528. rc = -EIO;
  5529. kfree(vpd);
  5530. goto out_free_mbox;
  5531. }
  5532. /*
  5533. * Continue initialization with default values even if driver failed
  5534. * to read FCoE param config regions, only read parameters if the
  5535. * board is FCoE
  5536. */
  5537. if (phba->hba_flag & HBA_FCOE_MODE &&
  5538. lpfc_sli4_read_fcoe_params(phba))
  5539. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5540. "2570 Failed to read FCoE parameters\n");
  5541. /*
  5542. * Retrieve sli4 device physical port name, failure of doing it
  5543. * is considered as non-fatal.
  5544. */
  5545. rc = lpfc_sli4_retrieve_pport_name(phba);
  5546. if (!rc)
  5547. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5548. "3080 Successful retrieving SLI4 device "
  5549. "physical port name: %s.\n", phba->Port);
  5550. /*
  5551. * Evaluate the read rev and vpd data. Populate the driver
  5552. * state with the results. If this routine fails, the failure
  5553. * is not fatal as the driver will use generic values.
  5554. */
  5555. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5556. if (unlikely(!rc)) {
  5557. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5558. "0377 Error %d parsing vpd. "
  5559. "Using defaults.\n", rc);
  5560. rc = 0;
  5561. }
  5562. kfree(vpd);
  5563. /* Save information as VPD data */
  5564. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5565. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5566. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5567. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5568. &mqe->un.read_rev);
  5569. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5570. &mqe->un.read_rev);
  5571. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5572. &mqe->un.read_rev);
  5573. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5574. &mqe->un.read_rev);
  5575. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5576. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5577. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5578. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5579. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5580. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5581. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5582. "(%d):0380 READ_REV Status x%x "
  5583. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5584. mboxq->vport ? mboxq->vport->vpi : 0,
  5585. bf_get(lpfc_mqe_status, mqe),
  5586. phba->vpd.rev.opFwName,
  5587. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5588. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5589. /*
  5590. * Discover the port's supported feature set and match it against the
  5591. * hosts requests.
  5592. */
  5593. lpfc_request_features(phba, mboxq);
  5594. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5595. if (unlikely(rc)) {
  5596. rc = -EIO;
  5597. goto out_free_mbox;
  5598. }
  5599. /*
  5600. * The port must support FCP initiator mode as this is the
  5601. * only mode running in the host.
  5602. */
  5603. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5604. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5605. "0378 No support for fcpi mode.\n");
  5606. ftr_rsp++;
  5607. }
  5608. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5609. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5610. else
  5611. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5612. /*
  5613. * If the port cannot support the host's requested features
  5614. * then turn off the global config parameters to disable the
  5615. * feature in the driver. This is not a fatal error.
  5616. */
  5617. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5618. if (phba->cfg_enable_bg) {
  5619. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5620. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5621. else
  5622. ftr_rsp++;
  5623. }
  5624. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5625. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5626. ftr_rsp++;
  5627. if (ftr_rsp) {
  5628. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5629. "0379 Feature Mismatch Data: x%08x %08x "
  5630. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5631. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5632. phba->cfg_enable_npiv, phba->max_vpi);
  5633. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5634. phba->cfg_enable_bg = 0;
  5635. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5636. phba->cfg_enable_npiv = 0;
  5637. }
  5638. /* These SLI3 features are assumed in SLI4 */
  5639. spin_lock_irq(&phba->hbalock);
  5640. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5641. spin_unlock_irq(&phba->hbalock);
  5642. /*
  5643. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5644. * calls depends on these resources to complete port setup.
  5645. */
  5646. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5647. if (rc) {
  5648. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5649. "2920 Failed to alloc Resource IDs "
  5650. "rc = x%x\n", rc);
  5651. goto out_free_mbox;
  5652. }
  5653. /* Read the port's service parameters. */
  5654. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5655. if (rc) {
  5656. phba->link_state = LPFC_HBA_ERROR;
  5657. rc = -ENOMEM;
  5658. goto out_free_mbox;
  5659. }
  5660. mboxq->vport = vport;
  5661. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5662. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5663. if (rc == MBX_SUCCESS) {
  5664. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5665. rc = 0;
  5666. }
  5667. /*
  5668. * This memory was allocated by the lpfc_read_sparam routine. Release
  5669. * it to the mbuf pool.
  5670. */
  5671. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5672. kfree(mp);
  5673. mboxq->context1 = NULL;
  5674. if (unlikely(rc)) {
  5675. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5676. "0382 READ_SPARAM command failed "
  5677. "status %d, mbxStatus x%x\n",
  5678. rc, bf_get(lpfc_mqe_status, mqe));
  5679. phba->link_state = LPFC_HBA_ERROR;
  5680. rc = -EIO;
  5681. goto out_free_mbox;
  5682. }
  5683. lpfc_update_vport_wwn(vport);
  5684. /* Update the fc_host data structures with new wwn. */
  5685. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5686. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5687. /* update host els and scsi xri-sgl sizes and mappings */
  5688. rc = lpfc_sli4_xri_sgl_update(phba);
  5689. if (unlikely(rc)) {
  5690. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5691. "1400 Failed to update xri-sgl size and "
  5692. "mapping: %d\n", rc);
  5693. goto out_free_mbox;
  5694. }
  5695. /* register the els sgl pool to the port */
  5696. rc = lpfc_sli4_repost_els_sgl_list(phba);
  5697. if (unlikely(rc)) {
  5698. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5699. "0582 Error %d during els sgl post "
  5700. "operation\n", rc);
  5701. rc = -ENODEV;
  5702. goto out_free_mbox;
  5703. }
  5704. /* register the allocated scsi sgl pool to the port */
  5705. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5706. if (unlikely(rc)) {
  5707. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5708. "0383 Error %d during scsi sgl post "
  5709. "operation\n", rc);
  5710. /* Some Scsi buffers were moved to the abort scsi list */
  5711. /* A pci function reset will repost them */
  5712. rc = -ENODEV;
  5713. goto out_free_mbox;
  5714. }
  5715. /* Post the rpi header region to the device. */
  5716. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5717. if (unlikely(rc)) {
  5718. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5719. "0393 Error %d during rpi post operation\n",
  5720. rc);
  5721. rc = -ENODEV;
  5722. goto out_free_mbox;
  5723. }
  5724. lpfc_sli4_node_prep(phba);
  5725. /* Create all the SLI4 queues */
  5726. rc = lpfc_sli4_queue_create(phba);
  5727. if (rc) {
  5728. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5729. "3089 Failed to allocate queues\n");
  5730. rc = -ENODEV;
  5731. goto out_stop_timers;
  5732. }
  5733. /* Set up all the queues to the device */
  5734. rc = lpfc_sli4_queue_setup(phba);
  5735. if (unlikely(rc)) {
  5736. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5737. "0381 Error %d during queue setup.\n ", rc);
  5738. goto out_destroy_queue;
  5739. }
  5740. /* Arm the CQs and then EQs on device */
  5741. lpfc_sli4_arm_cqeq_intr(phba);
  5742. /* Indicate device interrupt mode */
  5743. phba->sli4_hba.intr_enable = 1;
  5744. /* Allow asynchronous mailbox command to go through */
  5745. spin_lock_irq(&phba->hbalock);
  5746. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5747. spin_unlock_irq(&phba->hbalock);
  5748. /* Post receive buffers to the device */
  5749. lpfc_sli4_rb_setup(phba);
  5750. /* Reset HBA FCF states after HBA reset */
  5751. phba->fcf.fcf_flag = 0;
  5752. phba->fcf.current_rec.flag = 0;
  5753. /* Start the ELS watchdog timer */
  5754. mod_timer(&vport->els_tmofunc,
  5755. jiffies + HZ * (phba->fc_ratov * 2));
  5756. /* Start heart beat timer */
  5757. mod_timer(&phba->hb_tmofunc,
  5758. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5759. phba->hb_outstanding = 0;
  5760. phba->last_completion_time = jiffies;
  5761. /* Start error attention (ERATT) polling timer */
  5762. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5763. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5764. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5765. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5766. if (!rc) {
  5767. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5768. "2829 This device supports "
  5769. "Advanced Error Reporting (AER)\n");
  5770. spin_lock_irq(&phba->hbalock);
  5771. phba->hba_flag |= HBA_AER_ENABLED;
  5772. spin_unlock_irq(&phba->hbalock);
  5773. } else {
  5774. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5775. "2830 This device does not support "
  5776. "Advanced Error Reporting (AER)\n");
  5777. phba->cfg_aer_support = 0;
  5778. }
  5779. rc = 0;
  5780. }
  5781. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5782. /*
  5783. * The FC Port needs to register FCFI (index 0)
  5784. */
  5785. lpfc_reg_fcfi(phba, mboxq);
  5786. mboxq->vport = phba->pport;
  5787. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5788. if (rc != MBX_SUCCESS)
  5789. goto out_unset_queue;
  5790. rc = 0;
  5791. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5792. &mboxq->u.mqe.un.reg_fcfi);
  5793. /* Check if the port is configured to be disabled */
  5794. lpfc_sli_read_link_ste(phba);
  5795. }
  5796. /*
  5797. * The port is ready, set the host's link state to LINK_DOWN
  5798. * in preparation for link interrupts.
  5799. */
  5800. spin_lock_irq(&phba->hbalock);
  5801. phba->link_state = LPFC_LINK_DOWN;
  5802. spin_unlock_irq(&phba->hbalock);
  5803. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5804. (phba->hba_flag & LINK_DISABLED)) {
  5805. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5806. "3103 Adapter Link is disabled.\n");
  5807. lpfc_down_link(phba, mboxq);
  5808. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5809. if (rc != MBX_SUCCESS) {
  5810. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5811. "3104 Adapter failed to issue "
  5812. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5813. goto out_unset_queue;
  5814. }
  5815. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5816. /* don't perform init_link on SLI4 FC port loopback test */
  5817. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5818. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5819. if (rc)
  5820. goto out_unset_queue;
  5821. }
  5822. }
  5823. mempool_free(mboxq, phba->mbox_mem_pool);
  5824. return rc;
  5825. out_unset_queue:
  5826. /* Unset all the queues set up in this routine when error out */
  5827. lpfc_sli4_queue_unset(phba);
  5828. out_destroy_queue:
  5829. lpfc_sli4_queue_destroy(phba);
  5830. out_stop_timers:
  5831. lpfc_stop_hba_timers(phba);
  5832. out_free_mbox:
  5833. mempool_free(mboxq, phba->mbox_mem_pool);
  5834. return rc;
  5835. }
  5836. /**
  5837. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5838. * @ptr: context object - pointer to hba structure.
  5839. *
  5840. * This is the callback function for mailbox timer. The mailbox
  5841. * timer is armed when a new mailbox command is issued and the timer
  5842. * is deleted when the mailbox complete. The function is called by
  5843. * the kernel timer code when a mailbox does not complete within
  5844. * expected time. This function wakes up the worker thread to
  5845. * process the mailbox timeout and returns. All the processing is
  5846. * done by the worker thread function lpfc_mbox_timeout_handler.
  5847. **/
  5848. void
  5849. lpfc_mbox_timeout(unsigned long ptr)
  5850. {
  5851. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5852. unsigned long iflag;
  5853. uint32_t tmo_posted;
  5854. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5855. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5856. if (!tmo_posted)
  5857. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5858. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5859. if (!tmo_posted)
  5860. lpfc_worker_wake_up(phba);
  5861. return;
  5862. }
  5863. /**
  5864. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5865. * @phba: Pointer to HBA context object.
  5866. *
  5867. * This function is called from worker thread when a mailbox command times out.
  5868. * The caller is not required to hold any locks. This function will reset the
  5869. * HBA and recover all the pending commands.
  5870. **/
  5871. void
  5872. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5873. {
  5874. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5875. MAILBOX_t *mb = &pmbox->u.mb;
  5876. struct lpfc_sli *psli = &phba->sli;
  5877. struct lpfc_sli_ring *pring;
  5878. /* Check the pmbox pointer first. There is a race condition
  5879. * between the mbox timeout handler getting executed in the
  5880. * worklist and the mailbox actually completing. When this
  5881. * race condition occurs, the mbox_active will be NULL.
  5882. */
  5883. spin_lock_irq(&phba->hbalock);
  5884. if (pmbox == NULL) {
  5885. lpfc_printf_log(phba, KERN_WARNING,
  5886. LOG_MBOX | LOG_SLI,
  5887. "0353 Active Mailbox cleared - mailbox timeout "
  5888. "exiting\n");
  5889. spin_unlock_irq(&phba->hbalock);
  5890. return;
  5891. }
  5892. /* Mbox cmd <mbxCommand> timeout */
  5893. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5894. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5895. mb->mbxCommand,
  5896. phba->pport->port_state,
  5897. phba->sli.sli_flag,
  5898. phba->sli.mbox_active);
  5899. spin_unlock_irq(&phba->hbalock);
  5900. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5901. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5902. * it to fail all outstanding SCSI IO.
  5903. */
  5904. spin_lock_irq(&phba->pport->work_port_lock);
  5905. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5906. spin_unlock_irq(&phba->pport->work_port_lock);
  5907. spin_lock_irq(&phba->hbalock);
  5908. phba->link_state = LPFC_LINK_UNKNOWN;
  5909. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5910. spin_unlock_irq(&phba->hbalock);
  5911. pring = &psli->ring[psli->fcp_ring];
  5912. lpfc_sli_abort_iocb_ring(phba, pring);
  5913. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5914. "0345 Resetting board due to mailbox timeout\n");
  5915. /* Reset the HBA device */
  5916. lpfc_reset_hba(phba);
  5917. }
  5918. /**
  5919. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  5920. * @phba: Pointer to HBA context object.
  5921. * @pmbox: Pointer to mailbox object.
  5922. * @flag: Flag indicating how the mailbox need to be processed.
  5923. *
  5924. * This function is called by discovery code and HBA management code
  5925. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  5926. * function gets the hbalock to protect the data structures.
  5927. * The mailbox command can be submitted in polling mode, in which case
  5928. * this function will wait in a polling loop for the completion of the
  5929. * mailbox.
  5930. * If the mailbox is submitted in no_wait mode (not polling) the
  5931. * function will submit the command and returns immediately without waiting
  5932. * for the mailbox completion. The no_wait is supported only when HBA
  5933. * is in SLI2/SLI3 mode - interrupts are enabled.
  5934. * The SLI interface allows only one mailbox pending at a time. If the
  5935. * mailbox is issued in polling mode and there is already a mailbox
  5936. * pending, then the function will return an error. If the mailbox is issued
  5937. * in NO_WAIT mode and there is a mailbox pending already, the function
  5938. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  5939. * The sli layer owns the mailbox object until the completion of mailbox
  5940. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  5941. * return codes the caller owns the mailbox command after the return of
  5942. * the function.
  5943. **/
  5944. static int
  5945. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  5946. uint32_t flag)
  5947. {
  5948. MAILBOX_t *mb;
  5949. struct lpfc_sli *psli = &phba->sli;
  5950. uint32_t status, evtctr;
  5951. uint32_t ha_copy, hc_copy;
  5952. int i;
  5953. unsigned long timeout;
  5954. unsigned long drvr_flag = 0;
  5955. uint32_t word0, ldata;
  5956. void __iomem *to_slim;
  5957. int processing_queue = 0;
  5958. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5959. if (!pmbox) {
  5960. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5961. /* processing mbox queue from intr_handler */
  5962. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5963. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5964. return MBX_SUCCESS;
  5965. }
  5966. processing_queue = 1;
  5967. pmbox = lpfc_mbox_get(phba);
  5968. if (!pmbox) {
  5969. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5970. return MBX_SUCCESS;
  5971. }
  5972. }
  5973. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  5974. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  5975. if(!pmbox->vport) {
  5976. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5977. lpfc_printf_log(phba, KERN_ERR,
  5978. LOG_MBOX | LOG_VPORT,
  5979. "1806 Mbox x%x failed. No vport\n",
  5980. pmbox->u.mb.mbxCommand);
  5981. dump_stack();
  5982. goto out_not_finished;
  5983. }
  5984. }
  5985. /* If the PCI channel is in offline state, do not post mbox. */
  5986. if (unlikely(pci_channel_offline(phba->pcidev))) {
  5987. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5988. goto out_not_finished;
  5989. }
  5990. /* If HBA has a deferred error attention, fail the iocb. */
  5991. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  5992. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5993. goto out_not_finished;
  5994. }
  5995. psli = &phba->sli;
  5996. mb = &pmbox->u.mb;
  5997. status = MBX_SUCCESS;
  5998. if (phba->link_state == LPFC_HBA_ERROR) {
  5999. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6000. /* Mbox command <mbxCommand> cannot issue */
  6001. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6002. "(%d):0311 Mailbox command x%x cannot "
  6003. "issue Data: x%x x%x\n",
  6004. pmbox->vport ? pmbox->vport->vpi : 0,
  6005. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6006. goto out_not_finished;
  6007. }
  6008. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6009. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6010. !(hc_copy & HC_MBINT_ENA)) {
  6011. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6012. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6013. "(%d):2528 Mailbox command x%x cannot "
  6014. "issue Data: x%x x%x\n",
  6015. pmbox->vport ? pmbox->vport->vpi : 0,
  6016. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6017. goto out_not_finished;
  6018. }
  6019. }
  6020. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6021. /* Polling for a mbox command when another one is already active
  6022. * is not allowed in SLI. Also, the driver must have established
  6023. * SLI2 mode to queue and process multiple mbox commands.
  6024. */
  6025. if (flag & MBX_POLL) {
  6026. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6027. /* Mbox command <mbxCommand> cannot issue */
  6028. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6029. "(%d):2529 Mailbox command x%x "
  6030. "cannot issue Data: x%x x%x\n",
  6031. pmbox->vport ? pmbox->vport->vpi : 0,
  6032. pmbox->u.mb.mbxCommand,
  6033. psli->sli_flag, flag);
  6034. goto out_not_finished;
  6035. }
  6036. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  6037. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6038. /* Mbox command <mbxCommand> cannot issue */
  6039. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6040. "(%d):2530 Mailbox command x%x "
  6041. "cannot issue Data: x%x x%x\n",
  6042. pmbox->vport ? pmbox->vport->vpi : 0,
  6043. pmbox->u.mb.mbxCommand,
  6044. psli->sli_flag, flag);
  6045. goto out_not_finished;
  6046. }
  6047. /* Another mailbox command is still being processed, queue this
  6048. * command to be processed later.
  6049. */
  6050. lpfc_mbox_put(phba, pmbox);
  6051. /* Mbox cmd issue - BUSY */
  6052. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6053. "(%d):0308 Mbox cmd issue - BUSY Data: "
  6054. "x%x x%x x%x x%x\n",
  6055. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  6056. mb->mbxCommand, phba->pport->port_state,
  6057. psli->sli_flag, flag);
  6058. psli->slistat.mbox_busy++;
  6059. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6060. if (pmbox->vport) {
  6061. lpfc_debugfs_disc_trc(pmbox->vport,
  6062. LPFC_DISC_TRC_MBOX_VPORT,
  6063. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  6064. (uint32_t)mb->mbxCommand,
  6065. mb->un.varWords[0], mb->un.varWords[1]);
  6066. }
  6067. else {
  6068. lpfc_debugfs_disc_trc(phba->pport,
  6069. LPFC_DISC_TRC_MBOX,
  6070. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  6071. (uint32_t)mb->mbxCommand,
  6072. mb->un.varWords[0], mb->un.varWords[1]);
  6073. }
  6074. return MBX_BUSY;
  6075. }
  6076. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6077. /* If we are not polling, we MUST be in SLI2 mode */
  6078. if (flag != MBX_POLL) {
  6079. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  6080. (mb->mbxCommand != MBX_KILL_BOARD)) {
  6081. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6082. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6083. /* Mbox command <mbxCommand> cannot issue */
  6084. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6085. "(%d):2531 Mailbox command x%x "
  6086. "cannot issue Data: x%x x%x\n",
  6087. pmbox->vport ? pmbox->vport->vpi : 0,
  6088. pmbox->u.mb.mbxCommand,
  6089. psli->sli_flag, flag);
  6090. goto out_not_finished;
  6091. }
  6092. /* timeout active mbox command */
  6093. mod_timer(&psli->mbox_tmo, (jiffies +
  6094. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  6095. }
  6096. /* Mailbox cmd <cmd> issue */
  6097. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6098. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  6099. "x%x\n",
  6100. pmbox->vport ? pmbox->vport->vpi : 0,
  6101. mb->mbxCommand, phba->pport->port_state,
  6102. psli->sli_flag, flag);
  6103. if (mb->mbxCommand != MBX_HEARTBEAT) {
  6104. if (pmbox->vport) {
  6105. lpfc_debugfs_disc_trc(pmbox->vport,
  6106. LPFC_DISC_TRC_MBOX_VPORT,
  6107. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6108. (uint32_t)mb->mbxCommand,
  6109. mb->un.varWords[0], mb->un.varWords[1]);
  6110. }
  6111. else {
  6112. lpfc_debugfs_disc_trc(phba->pport,
  6113. LPFC_DISC_TRC_MBOX,
  6114. "MBOX Send: cmd:x%x mb:x%x x%x",
  6115. (uint32_t)mb->mbxCommand,
  6116. mb->un.varWords[0], mb->un.varWords[1]);
  6117. }
  6118. }
  6119. psli->slistat.mbox_cmd++;
  6120. evtctr = psli->slistat.mbox_event;
  6121. /* next set own bit for the adapter and copy over command word */
  6122. mb->mbxOwner = OWN_CHIP;
  6123. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6124. /* Populate mbox extension offset word. */
  6125. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6126. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6127. = (uint8_t *)phba->mbox_ext
  6128. - (uint8_t *)phba->mbox;
  6129. }
  6130. /* Copy the mailbox extension data */
  6131. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6132. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6133. (uint8_t *)phba->mbox_ext,
  6134. pmbox->in_ext_byte_len);
  6135. }
  6136. /* Copy command data to host SLIM area */
  6137. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6138. } else {
  6139. /* Populate mbox extension offset word. */
  6140. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6141. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6142. = MAILBOX_HBA_EXT_OFFSET;
  6143. /* Copy the mailbox extension data */
  6144. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6145. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6146. MAILBOX_HBA_EXT_OFFSET,
  6147. pmbox->context2, pmbox->in_ext_byte_len);
  6148. }
  6149. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6150. /* copy command data into host mbox for cmpl */
  6151. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6152. }
  6153. /* First copy mbox command data to HBA SLIM, skip past first
  6154. word */
  6155. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6156. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  6157. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6158. /* Next copy over first word, with mbxOwner set */
  6159. ldata = *((uint32_t *)mb);
  6160. to_slim = phba->MBslimaddr;
  6161. writel(ldata, to_slim);
  6162. readl(to_slim); /* flush */
  6163. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6164. /* switch over to host mailbox */
  6165. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6166. }
  6167. }
  6168. wmb();
  6169. switch (flag) {
  6170. case MBX_NOWAIT:
  6171. /* Set up reference to mailbox command */
  6172. psli->mbox_active = pmbox;
  6173. /* Interrupt board to do it */
  6174. writel(CA_MBATT, phba->CAregaddr);
  6175. readl(phba->CAregaddr); /* flush */
  6176. /* Don't wait for it to finish, just return */
  6177. break;
  6178. case MBX_POLL:
  6179. /* Set up null reference to mailbox command */
  6180. psli->mbox_active = NULL;
  6181. /* Interrupt board to do it */
  6182. writel(CA_MBATT, phba->CAregaddr);
  6183. readl(phba->CAregaddr); /* flush */
  6184. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6185. /* First read mbox status word */
  6186. word0 = *((uint32_t *)phba->mbox);
  6187. word0 = le32_to_cpu(word0);
  6188. } else {
  6189. /* First read mbox status word */
  6190. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6191. spin_unlock_irqrestore(&phba->hbalock,
  6192. drvr_flag);
  6193. goto out_not_finished;
  6194. }
  6195. }
  6196. /* Read the HBA Host Attention Register */
  6197. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6198. spin_unlock_irqrestore(&phba->hbalock,
  6199. drvr_flag);
  6200. goto out_not_finished;
  6201. }
  6202. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6203. 1000) + jiffies;
  6204. i = 0;
  6205. /* Wait for command to complete */
  6206. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6207. (!(ha_copy & HA_MBATT) &&
  6208. (phba->link_state > LPFC_WARM_START))) {
  6209. if (time_after(jiffies, timeout)) {
  6210. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6211. spin_unlock_irqrestore(&phba->hbalock,
  6212. drvr_flag);
  6213. goto out_not_finished;
  6214. }
  6215. /* Check if we took a mbox interrupt while we were
  6216. polling */
  6217. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6218. && (evtctr != psli->slistat.mbox_event))
  6219. break;
  6220. if (i++ > 10) {
  6221. spin_unlock_irqrestore(&phba->hbalock,
  6222. drvr_flag);
  6223. msleep(1);
  6224. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6225. }
  6226. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6227. /* First copy command data */
  6228. word0 = *((uint32_t *)phba->mbox);
  6229. word0 = le32_to_cpu(word0);
  6230. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6231. MAILBOX_t *slimmb;
  6232. uint32_t slimword0;
  6233. /* Check real SLIM for any errors */
  6234. slimword0 = readl(phba->MBslimaddr);
  6235. slimmb = (MAILBOX_t *) & slimword0;
  6236. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6237. && slimmb->mbxStatus) {
  6238. psli->sli_flag &=
  6239. ~LPFC_SLI_ACTIVE;
  6240. word0 = slimword0;
  6241. }
  6242. }
  6243. } else {
  6244. /* First copy command data */
  6245. word0 = readl(phba->MBslimaddr);
  6246. }
  6247. /* Read the HBA Host Attention Register */
  6248. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6249. spin_unlock_irqrestore(&phba->hbalock,
  6250. drvr_flag);
  6251. goto out_not_finished;
  6252. }
  6253. }
  6254. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6255. /* copy results back to user */
  6256. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  6257. /* Copy the mailbox extension data */
  6258. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6259. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6260. pmbox->context2,
  6261. pmbox->out_ext_byte_len);
  6262. }
  6263. } else {
  6264. /* First copy command data */
  6265. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  6266. MAILBOX_CMD_SIZE);
  6267. /* Copy the mailbox extension data */
  6268. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6269. lpfc_memcpy_from_slim(pmbox->context2,
  6270. phba->MBslimaddr +
  6271. MAILBOX_HBA_EXT_OFFSET,
  6272. pmbox->out_ext_byte_len);
  6273. }
  6274. }
  6275. writel(HA_MBATT, phba->HAregaddr);
  6276. readl(phba->HAregaddr); /* flush */
  6277. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6278. status = mb->mbxStatus;
  6279. }
  6280. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6281. return status;
  6282. out_not_finished:
  6283. if (processing_queue) {
  6284. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6285. lpfc_mbox_cmpl_put(phba, pmbox);
  6286. }
  6287. return MBX_NOT_FINISHED;
  6288. }
  6289. /**
  6290. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6291. * @phba: Pointer to HBA context object.
  6292. *
  6293. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6294. * the driver internal pending mailbox queue. It will then try to wait out the
  6295. * possible outstanding mailbox command before return.
  6296. *
  6297. * Returns:
  6298. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6299. * the outstanding mailbox command timed out.
  6300. **/
  6301. static int
  6302. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6303. {
  6304. struct lpfc_sli *psli = &phba->sli;
  6305. int rc = 0;
  6306. unsigned long timeout = 0;
  6307. /* Mark the asynchronous mailbox command posting as blocked */
  6308. spin_lock_irq(&phba->hbalock);
  6309. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6310. /* Determine how long we might wait for the active mailbox
  6311. * command to be gracefully completed by firmware.
  6312. */
  6313. if (phba->sli.mbox_active)
  6314. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6315. phba->sli.mbox_active) *
  6316. 1000) + jiffies;
  6317. spin_unlock_irq(&phba->hbalock);
  6318. /* Wait for the outstnading mailbox command to complete */
  6319. while (phba->sli.mbox_active) {
  6320. /* Check active mailbox complete status every 2ms */
  6321. msleep(2);
  6322. if (time_after(jiffies, timeout)) {
  6323. /* Timeout, marked the outstanding cmd not complete */
  6324. rc = 1;
  6325. break;
  6326. }
  6327. }
  6328. /* Can not cleanly block async mailbox command, fails it */
  6329. if (rc) {
  6330. spin_lock_irq(&phba->hbalock);
  6331. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6332. spin_unlock_irq(&phba->hbalock);
  6333. }
  6334. return rc;
  6335. }
  6336. /**
  6337. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6338. * @phba: Pointer to HBA context object.
  6339. *
  6340. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6341. * commands from the driver internal pending mailbox queue. It makes sure
  6342. * that there is no outstanding mailbox command before resuming posting
  6343. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6344. * mailbox command, it will try to wait it out before resuming asynchronous
  6345. * mailbox command posting.
  6346. **/
  6347. static void
  6348. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6349. {
  6350. struct lpfc_sli *psli = &phba->sli;
  6351. spin_lock_irq(&phba->hbalock);
  6352. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6353. /* Asynchronous mailbox posting is not blocked, do nothing */
  6354. spin_unlock_irq(&phba->hbalock);
  6355. return;
  6356. }
  6357. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6358. * successful or timeout, after timing-out the outstanding mailbox
  6359. * command shall always be removed, so just unblock posting async
  6360. * mailbox command and resume
  6361. */
  6362. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6363. spin_unlock_irq(&phba->hbalock);
  6364. /* wake up worker thread to post asynchronlous mailbox command */
  6365. lpfc_worker_wake_up(phba);
  6366. }
  6367. /**
  6368. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6369. * @phba: Pointer to HBA context object.
  6370. * @mboxq: Pointer to mailbox object.
  6371. *
  6372. * The function posts a mailbox to the port. The mailbox is expected
  6373. * to be comletely filled in and ready for the port to operate on it.
  6374. * This routine executes a synchronous completion operation on the
  6375. * mailbox by polling for its completion.
  6376. *
  6377. * The caller must not be holding any locks when calling this routine.
  6378. *
  6379. * Returns:
  6380. * MBX_SUCCESS - mailbox posted successfully
  6381. * Any of the MBX error values.
  6382. **/
  6383. static int
  6384. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6385. {
  6386. int rc = MBX_SUCCESS;
  6387. unsigned long iflag;
  6388. uint32_t db_ready;
  6389. uint32_t mcqe_status;
  6390. uint32_t mbx_cmnd;
  6391. unsigned long timeout;
  6392. struct lpfc_sli *psli = &phba->sli;
  6393. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6394. struct lpfc_bmbx_create *mbox_rgn;
  6395. struct dma_address *dma_address;
  6396. struct lpfc_register bmbx_reg;
  6397. /*
  6398. * Only one mailbox can be active to the bootstrap mailbox region
  6399. * at a time and there is no queueing provided.
  6400. */
  6401. spin_lock_irqsave(&phba->hbalock, iflag);
  6402. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6403. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6404. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6405. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6406. "cannot issue Data: x%x x%x\n",
  6407. mboxq->vport ? mboxq->vport->vpi : 0,
  6408. mboxq->u.mb.mbxCommand,
  6409. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6410. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6411. psli->sli_flag, MBX_POLL);
  6412. return MBXERR_ERROR;
  6413. }
  6414. /* The server grabs the token and owns it until release */
  6415. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6416. phba->sli.mbox_active = mboxq;
  6417. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6418. /*
  6419. * Initialize the bootstrap memory region to avoid stale data areas
  6420. * in the mailbox post. Then copy the caller's mailbox contents to
  6421. * the bmbx mailbox region.
  6422. */
  6423. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6424. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6425. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6426. sizeof(struct lpfc_mqe));
  6427. /* Post the high mailbox dma address to the port and wait for ready. */
  6428. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6429. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6430. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6431. * 1000) + jiffies;
  6432. do {
  6433. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6434. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6435. if (!db_ready)
  6436. msleep(2);
  6437. if (time_after(jiffies, timeout)) {
  6438. rc = MBXERR_ERROR;
  6439. goto exit;
  6440. }
  6441. } while (!db_ready);
  6442. /* Post the low mailbox dma address to the port. */
  6443. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6444. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6445. * 1000) + jiffies;
  6446. do {
  6447. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6448. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6449. if (!db_ready)
  6450. msleep(2);
  6451. if (time_after(jiffies, timeout)) {
  6452. rc = MBXERR_ERROR;
  6453. goto exit;
  6454. }
  6455. } while (!db_ready);
  6456. /*
  6457. * Read the CQ to ensure the mailbox has completed.
  6458. * If so, update the mailbox status so that the upper layers
  6459. * can complete the request normally.
  6460. */
  6461. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6462. sizeof(struct lpfc_mqe));
  6463. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6464. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6465. sizeof(struct lpfc_mcqe));
  6466. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6467. /*
  6468. * When the CQE status indicates a failure and the mailbox status
  6469. * indicates success then copy the CQE status into the mailbox status
  6470. * (and prefix it with x4000).
  6471. */
  6472. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6473. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6474. bf_set(lpfc_mqe_status, mb,
  6475. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6476. rc = MBXERR_ERROR;
  6477. } else
  6478. lpfc_sli4_swap_str(phba, mboxq);
  6479. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6480. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6481. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6482. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6483. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6484. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6485. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6486. bf_get(lpfc_mqe_status, mb),
  6487. mb->un.mb_words[0], mb->un.mb_words[1],
  6488. mb->un.mb_words[2], mb->un.mb_words[3],
  6489. mb->un.mb_words[4], mb->un.mb_words[5],
  6490. mb->un.mb_words[6], mb->un.mb_words[7],
  6491. mb->un.mb_words[8], mb->un.mb_words[9],
  6492. mb->un.mb_words[10], mb->un.mb_words[11],
  6493. mb->un.mb_words[12], mboxq->mcqe.word0,
  6494. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6495. mboxq->mcqe.trailer);
  6496. exit:
  6497. /* We are holding the token, no needed for lock when release */
  6498. spin_lock_irqsave(&phba->hbalock, iflag);
  6499. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6500. phba->sli.mbox_active = NULL;
  6501. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6502. return rc;
  6503. }
  6504. /**
  6505. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6506. * @phba: Pointer to HBA context object.
  6507. * @pmbox: Pointer to mailbox object.
  6508. * @flag: Flag indicating how the mailbox need to be processed.
  6509. *
  6510. * This function is called by discovery code and HBA management code to submit
  6511. * a mailbox command to firmware with SLI-4 interface spec.
  6512. *
  6513. * Return codes the caller owns the mailbox command after the return of the
  6514. * function.
  6515. **/
  6516. static int
  6517. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6518. uint32_t flag)
  6519. {
  6520. struct lpfc_sli *psli = &phba->sli;
  6521. unsigned long iflags;
  6522. int rc;
  6523. /* dump from issue mailbox command if setup */
  6524. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6525. rc = lpfc_mbox_dev_check(phba);
  6526. if (unlikely(rc)) {
  6527. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6528. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6529. "cannot issue Data: x%x x%x\n",
  6530. mboxq->vport ? mboxq->vport->vpi : 0,
  6531. mboxq->u.mb.mbxCommand,
  6532. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6533. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6534. psli->sli_flag, flag);
  6535. goto out_not_finished;
  6536. }
  6537. /* Detect polling mode and jump to a handler */
  6538. if (!phba->sli4_hba.intr_enable) {
  6539. if (flag == MBX_POLL)
  6540. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6541. else
  6542. rc = -EIO;
  6543. if (rc != MBX_SUCCESS)
  6544. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6545. "(%d):2541 Mailbox command x%x "
  6546. "(x%x/x%x) failure: "
  6547. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6548. "Data: x%x x%x\n,",
  6549. mboxq->vport ? mboxq->vport->vpi : 0,
  6550. mboxq->u.mb.mbxCommand,
  6551. lpfc_sli_config_mbox_subsys_get(phba,
  6552. mboxq),
  6553. lpfc_sli_config_mbox_opcode_get(phba,
  6554. mboxq),
  6555. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6556. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6557. bf_get(lpfc_mcqe_ext_status,
  6558. &mboxq->mcqe),
  6559. psli->sli_flag, flag);
  6560. return rc;
  6561. } else if (flag == MBX_POLL) {
  6562. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6563. "(%d):2542 Try to issue mailbox command "
  6564. "x%x (x%x/x%x) synchronously ahead of async"
  6565. "mailbox command queue: x%x x%x\n",
  6566. mboxq->vport ? mboxq->vport->vpi : 0,
  6567. mboxq->u.mb.mbxCommand,
  6568. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6569. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6570. psli->sli_flag, flag);
  6571. /* Try to block the asynchronous mailbox posting */
  6572. rc = lpfc_sli4_async_mbox_block(phba);
  6573. if (!rc) {
  6574. /* Successfully blocked, now issue sync mbox cmd */
  6575. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6576. if (rc != MBX_SUCCESS)
  6577. lpfc_printf_log(phba, KERN_WARNING,
  6578. LOG_MBOX | LOG_SLI,
  6579. "(%d):2597 Sync Mailbox command "
  6580. "x%x (x%x/x%x) failure: "
  6581. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6582. "Data: x%x x%x\n,",
  6583. mboxq->vport ? mboxq->vport->vpi : 0,
  6584. mboxq->u.mb.mbxCommand,
  6585. lpfc_sli_config_mbox_subsys_get(phba,
  6586. mboxq),
  6587. lpfc_sli_config_mbox_opcode_get(phba,
  6588. mboxq),
  6589. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6590. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6591. bf_get(lpfc_mcqe_ext_status,
  6592. &mboxq->mcqe),
  6593. psli->sli_flag, flag);
  6594. /* Unblock the async mailbox posting afterward */
  6595. lpfc_sli4_async_mbox_unblock(phba);
  6596. }
  6597. return rc;
  6598. }
  6599. /* Now, interrupt mode asynchrous mailbox command */
  6600. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6601. if (rc) {
  6602. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6603. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6604. "cannot issue Data: x%x x%x\n",
  6605. mboxq->vport ? mboxq->vport->vpi : 0,
  6606. mboxq->u.mb.mbxCommand,
  6607. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6608. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6609. psli->sli_flag, flag);
  6610. goto out_not_finished;
  6611. }
  6612. /* Put the mailbox command to the driver internal FIFO */
  6613. psli->slistat.mbox_busy++;
  6614. spin_lock_irqsave(&phba->hbalock, iflags);
  6615. lpfc_mbox_put(phba, mboxq);
  6616. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6617. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6618. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6619. "x%x (x%x/x%x) x%x x%x x%x\n",
  6620. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6621. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6622. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6623. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6624. phba->pport->port_state,
  6625. psli->sli_flag, MBX_NOWAIT);
  6626. /* Wake up worker thread to transport mailbox command from head */
  6627. lpfc_worker_wake_up(phba);
  6628. return MBX_BUSY;
  6629. out_not_finished:
  6630. return MBX_NOT_FINISHED;
  6631. }
  6632. /**
  6633. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6634. * @phba: Pointer to HBA context object.
  6635. *
  6636. * This function is called by worker thread to send a mailbox command to
  6637. * SLI4 HBA firmware.
  6638. *
  6639. **/
  6640. int
  6641. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6642. {
  6643. struct lpfc_sli *psli = &phba->sli;
  6644. LPFC_MBOXQ_t *mboxq;
  6645. int rc = MBX_SUCCESS;
  6646. unsigned long iflags;
  6647. struct lpfc_mqe *mqe;
  6648. uint32_t mbx_cmnd;
  6649. /* Check interrupt mode before post async mailbox command */
  6650. if (unlikely(!phba->sli4_hba.intr_enable))
  6651. return MBX_NOT_FINISHED;
  6652. /* Check for mailbox command service token */
  6653. spin_lock_irqsave(&phba->hbalock, iflags);
  6654. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6655. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6656. return MBX_NOT_FINISHED;
  6657. }
  6658. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6659. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6660. return MBX_NOT_FINISHED;
  6661. }
  6662. if (unlikely(phba->sli.mbox_active)) {
  6663. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6664. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6665. "0384 There is pending active mailbox cmd\n");
  6666. return MBX_NOT_FINISHED;
  6667. }
  6668. /* Take the mailbox command service token */
  6669. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6670. /* Get the next mailbox command from head of queue */
  6671. mboxq = lpfc_mbox_get(phba);
  6672. /* If no more mailbox command waiting for post, we're done */
  6673. if (!mboxq) {
  6674. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6675. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6676. return MBX_SUCCESS;
  6677. }
  6678. phba->sli.mbox_active = mboxq;
  6679. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6680. /* Check device readiness for posting mailbox command */
  6681. rc = lpfc_mbox_dev_check(phba);
  6682. if (unlikely(rc))
  6683. /* Driver clean routine will clean up pending mailbox */
  6684. goto out_not_finished;
  6685. /* Prepare the mbox command to be posted */
  6686. mqe = &mboxq->u.mqe;
  6687. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6688. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6689. mod_timer(&psli->mbox_tmo, (jiffies +
  6690. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6691. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6692. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6693. "x%x x%x\n",
  6694. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6695. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6696. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6697. phba->pport->port_state, psli->sli_flag);
  6698. if (mbx_cmnd != MBX_HEARTBEAT) {
  6699. if (mboxq->vport) {
  6700. lpfc_debugfs_disc_trc(mboxq->vport,
  6701. LPFC_DISC_TRC_MBOX_VPORT,
  6702. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6703. mbx_cmnd, mqe->un.mb_words[0],
  6704. mqe->un.mb_words[1]);
  6705. } else {
  6706. lpfc_debugfs_disc_trc(phba->pport,
  6707. LPFC_DISC_TRC_MBOX,
  6708. "MBOX Send: cmd:x%x mb:x%x x%x",
  6709. mbx_cmnd, mqe->un.mb_words[0],
  6710. mqe->un.mb_words[1]);
  6711. }
  6712. }
  6713. psli->slistat.mbox_cmd++;
  6714. /* Post the mailbox command to the port */
  6715. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6716. if (rc != MBX_SUCCESS) {
  6717. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6718. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6719. "cannot issue Data: x%x x%x\n",
  6720. mboxq->vport ? mboxq->vport->vpi : 0,
  6721. mboxq->u.mb.mbxCommand,
  6722. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6723. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6724. psli->sli_flag, MBX_NOWAIT);
  6725. goto out_not_finished;
  6726. }
  6727. return rc;
  6728. out_not_finished:
  6729. spin_lock_irqsave(&phba->hbalock, iflags);
  6730. if (phba->sli.mbox_active) {
  6731. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6732. __lpfc_mbox_cmpl_put(phba, mboxq);
  6733. /* Release the token */
  6734. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6735. phba->sli.mbox_active = NULL;
  6736. }
  6737. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6738. return MBX_NOT_FINISHED;
  6739. }
  6740. /**
  6741. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6742. * @phba: Pointer to HBA context object.
  6743. * @pmbox: Pointer to mailbox object.
  6744. * @flag: Flag indicating how the mailbox need to be processed.
  6745. *
  6746. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6747. * the API jump table function pointer from the lpfc_hba struct.
  6748. *
  6749. * Return codes the caller owns the mailbox command after the return of the
  6750. * function.
  6751. **/
  6752. int
  6753. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6754. {
  6755. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6756. }
  6757. /**
  6758. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6759. * @phba: The hba struct for which this call is being executed.
  6760. * @dev_grp: The HBA PCI-Device group number.
  6761. *
  6762. * This routine sets up the mbox interface API function jump table in @phba
  6763. * struct.
  6764. * Returns: 0 - success, -ENODEV - failure.
  6765. **/
  6766. int
  6767. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6768. {
  6769. switch (dev_grp) {
  6770. case LPFC_PCI_DEV_LP:
  6771. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6772. phba->lpfc_sli_handle_slow_ring_event =
  6773. lpfc_sli_handle_slow_ring_event_s3;
  6774. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6775. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6776. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6777. break;
  6778. case LPFC_PCI_DEV_OC:
  6779. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6780. phba->lpfc_sli_handle_slow_ring_event =
  6781. lpfc_sli_handle_slow_ring_event_s4;
  6782. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6783. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6784. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6785. break;
  6786. default:
  6787. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6788. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6789. dev_grp);
  6790. return -ENODEV;
  6791. break;
  6792. }
  6793. return 0;
  6794. }
  6795. /**
  6796. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6797. * @phba: Pointer to HBA context object.
  6798. * @pring: Pointer to driver SLI ring object.
  6799. * @piocb: Pointer to address of newly added command iocb.
  6800. *
  6801. * This function is called with hbalock held to add a command
  6802. * iocb to the txq when SLI layer cannot submit the command iocb
  6803. * to the ring.
  6804. **/
  6805. void
  6806. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6807. struct lpfc_iocbq *piocb)
  6808. {
  6809. /* Insert the caller's iocb in the txq tail for later processing. */
  6810. list_add_tail(&piocb->list, &pring->txq);
  6811. pring->txq_cnt++;
  6812. }
  6813. /**
  6814. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6815. * @phba: Pointer to HBA context object.
  6816. * @pring: Pointer to driver SLI ring object.
  6817. * @piocb: Pointer to address of newly added command iocb.
  6818. *
  6819. * This function is called with hbalock held before a new
  6820. * iocb is submitted to the firmware. This function checks
  6821. * txq to flush the iocbs in txq to Firmware before
  6822. * submitting new iocbs to the Firmware.
  6823. * If there are iocbs in the txq which need to be submitted
  6824. * to firmware, lpfc_sli_next_iocb returns the first element
  6825. * of the txq after dequeuing it from txq.
  6826. * If there is no iocb in the txq then the function will return
  6827. * *piocb and *piocb is set to NULL. Caller needs to check
  6828. * *piocb to find if there are more commands in the txq.
  6829. **/
  6830. static struct lpfc_iocbq *
  6831. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6832. struct lpfc_iocbq **piocb)
  6833. {
  6834. struct lpfc_iocbq * nextiocb;
  6835. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6836. if (!nextiocb) {
  6837. nextiocb = *piocb;
  6838. *piocb = NULL;
  6839. }
  6840. return nextiocb;
  6841. }
  6842. /**
  6843. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6844. * @phba: Pointer to HBA context object.
  6845. * @ring_number: SLI ring number to issue iocb on.
  6846. * @piocb: Pointer to command iocb.
  6847. * @flag: Flag indicating if this command can be put into txq.
  6848. *
  6849. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6850. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6851. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6852. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6853. * this function allows only iocbs for posting buffers. This function finds
  6854. * next available slot in the command ring and posts the command to the
  6855. * available slot and writes the port attention register to request HBA start
  6856. * processing new iocb. If there is no slot available in the ring and
  6857. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6858. * the function returns IOCB_BUSY.
  6859. *
  6860. * This function is called with hbalock held. The function will return success
  6861. * after it successfully submit the iocb to firmware or after adding to the
  6862. * txq.
  6863. **/
  6864. static int
  6865. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6866. struct lpfc_iocbq *piocb, uint32_t flag)
  6867. {
  6868. struct lpfc_iocbq *nextiocb;
  6869. IOCB_t *iocb;
  6870. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6871. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6872. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6873. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6874. lpfc_printf_log(phba, KERN_ERR,
  6875. LOG_SLI | LOG_VPORT,
  6876. "1807 IOCB x%x failed. No vport\n",
  6877. piocb->iocb.ulpCommand);
  6878. dump_stack();
  6879. return IOCB_ERROR;
  6880. }
  6881. /* If the PCI channel is in offline state, do not post iocbs. */
  6882. if (unlikely(pci_channel_offline(phba->pcidev)))
  6883. return IOCB_ERROR;
  6884. /* If HBA has a deferred error attention, fail the iocb. */
  6885. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6886. return IOCB_ERROR;
  6887. /*
  6888. * We should never get an IOCB if we are in a < LINK_DOWN state
  6889. */
  6890. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6891. return IOCB_ERROR;
  6892. /*
  6893. * Check to see if we are blocking IOCB processing because of a
  6894. * outstanding event.
  6895. */
  6896. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  6897. goto iocb_busy;
  6898. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  6899. /*
  6900. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  6901. * can be issued if the link is not up.
  6902. */
  6903. switch (piocb->iocb.ulpCommand) {
  6904. case CMD_GEN_REQUEST64_CR:
  6905. case CMD_GEN_REQUEST64_CX:
  6906. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  6907. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  6908. FC_RCTL_DD_UNSOL_CMD) ||
  6909. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  6910. MENLO_TRANSPORT_TYPE))
  6911. goto iocb_busy;
  6912. break;
  6913. case CMD_QUE_RING_BUF_CN:
  6914. case CMD_QUE_RING_BUF64_CN:
  6915. /*
  6916. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  6917. * completion, iocb_cmpl MUST be 0.
  6918. */
  6919. if (piocb->iocb_cmpl)
  6920. piocb->iocb_cmpl = NULL;
  6921. /*FALLTHROUGH*/
  6922. case CMD_CREATE_XRI_CR:
  6923. case CMD_CLOSE_XRI_CN:
  6924. case CMD_CLOSE_XRI_CX:
  6925. break;
  6926. default:
  6927. goto iocb_busy;
  6928. }
  6929. /*
  6930. * For FCP commands, we must be in a state where we can process link
  6931. * attention events.
  6932. */
  6933. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  6934. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  6935. goto iocb_busy;
  6936. }
  6937. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  6938. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  6939. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  6940. if (iocb)
  6941. lpfc_sli_update_ring(phba, pring);
  6942. else
  6943. lpfc_sli_update_full_ring(phba, pring);
  6944. if (!piocb)
  6945. return IOCB_SUCCESS;
  6946. goto out_busy;
  6947. iocb_busy:
  6948. pring->stats.iocb_cmd_delay++;
  6949. out_busy:
  6950. if (!(flag & SLI_IOCB_RET_IOCB)) {
  6951. __lpfc_sli_ringtx_put(phba, pring, piocb);
  6952. return IOCB_SUCCESS;
  6953. }
  6954. return IOCB_BUSY;
  6955. }
  6956. /**
  6957. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  6958. * @phba: Pointer to HBA context object.
  6959. * @piocb: Pointer to command iocb.
  6960. * @sglq: Pointer to the scatter gather queue object.
  6961. *
  6962. * This routine converts the bpl or bde that is in the IOCB
  6963. * to a sgl list for the sli4 hardware. The physical address
  6964. * of the bpl/bde is converted back to a virtual address.
  6965. * If the IOCB contains a BPL then the list of BDE's is
  6966. * converted to sli4_sge's. If the IOCB contains a single
  6967. * BDE then it is converted to a single sli_sge.
  6968. * The IOCB is still in cpu endianess so the contents of
  6969. * the bpl can be used without byte swapping.
  6970. *
  6971. * Returns valid XRI = Success, NO_XRI = Failure.
  6972. **/
  6973. static uint16_t
  6974. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  6975. struct lpfc_sglq *sglq)
  6976. {
  6977. uint16_t xritag = NO_XRI;
  6978. struct ulp_bde64 *bpl = NULL;
  6979. struct ulp_bde64 bde;
  6980. struct sli4_sge *sgl = NULL;
  6981. struct lpfc_dmabuf *dmabuf;
  6982. IOCB_t *icmd;
  6983. int numBdes = 0;
  6984. int i = 0;
  6985. uint32_t offset = 0; /* accumulated offset in the sg request list */
  6986. int inbound = 0; /* number of sg reply entries inbound from firmware */
  6987. if (!piocbq || !sglq)
  6988. return xritag;
  6989. sgl = (struct sli4_sge *)sglq->sgl;
  6990. icmd = &piocbq->iocb;
  6991. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  6992. return sglq->sli4_xritag;
  6993. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6994. numBdes = icmd->un.genreq64.bdl.bdeSize /
  6995. sizeof(struct ulp_bde64);
  6996. /* The addrHigh and addrLow fields within the IOCB
  6997. * have not been byteswapped yet so there is no
  6998. * need to swap them back.
  6999. */
  7000. if (piocbq->context3)
  7001. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  7002. else
  7003. return xritag;
  7004. bpl = (struct ulp_bde64 *)dmabuf->virt;
  7005. if (!bpl)
  7006. return xritag;
  7007. for (i = 0; i < numBdes; i++) {
  7008. /* Should already be byte swapped. */
  7009. sgl->addr_hi = bpl->addrHigh;
  7010. sgl->addr_lo = bpl->addrLow;
  7011. sgl->word2 = le32_to_cpu(sgl->word2);
  7012. if ((i+1) == numBdes)
  7013. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7014. else
  7015. bf_set(lpfc_sli4_sge_last, sgl, 0);
  7016. /* swap the size field back to the cpu so we
  7017. * can assign it to the sgl.
  7018. */
  7019. bde.tus.w = le32_to_cpu(bpl->tus.w);
  7020. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  7021. /* The offsets in the sgl need to be accumulated
  7022. * separately for the request and reply lists.
  7023. * The request is always first, the reply follows.
  7024. */
  7025. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  7026. /* add up the reply sg entries */
  7027. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  7028. inbound++;
  7029. /* first inbound? reset the offset */
  7030. if (inbound == 1)
  7031. offset = 0;
  7032. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  7033. bf_set(lpfc_sli4_sge_type, sgl,
  7034. LPFC_SGE_TYPE_DATA);
  7035. offset += bde.tus.f.bdeSize;
  7036. }
  7037. sgl->word2 = cpu_to_le32(sgl->word2);
  7038. bpl++;
  7039. sgl++;
  7040. }
  7041. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  7042. /* The addrHigh and addrLow fields of the BDE have not
  7043. * been byteswapped yet so they need to be swapped
  7044. * before putting them in the sgl.
  7045. */
  7046. sgl->addr_hi =
  7047. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  7048. sgl->addr_lo =
  7049. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  7050. sgl->word2 = le32_to_cpu(sgl->word2);
  7051. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7052. sgl->word2 = cpu_to_le32(sgl->word2);
  7053. sgl->sge_len =
  7054. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  7055. }
  7056. return sglq->sli4_xritag;
  7057. }
  7058. /**
  7059. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  7060. * @phba: Pointer to HBA context object.
  7061. *
  7062. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  7063. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  7064. * held.
  7065. *
  7066. * Return: index into SLI4 fast-path FCP queue index.
  7067. **/
  7068. static uint32_t
  7069. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  7070. {
  7071. ++phba->fcp_qidx;
  7072. if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
  7073. phba->fcp_qidx = 0;
  7074. return phba->fcp_qidx;
  7075. }
  7076. /**
  7077. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  7078. * @phba: Pointer to HBA context object.
  7079. * @piocb: Pointer to command iocb.
  7080. * @wqe: Pointer to the work queue entry.
  7081. *
  7082. * This routine converts the iocb command to its Work Queue Entry
  7083. * equivalent. The wqe pointer should not have any fields set when
  7084. * this routine is called because it will memcpy over them.
  7085. * This routine does not set the CQ_ID or the WQEC bits in the
  7086. * wqe.
  7087. *
  7088. * Returns: 0 = Success, IOCB_ERROR = Failure.
  7089. **/
  7090. static int
  7091. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  7092. union lpfc_wqe *wqe)
  7093. {
  7094. uint32_t xmit_len = 0, total_len = 0;
  7095. uint8_t ct = 0;
  7096. uint32_t fip;
  7097. uint32_t abort_tag;
  7098. uint8_t command_type = ELS_COMMAND_NON_FIP;
  7099. uint8_t cmnd;
  7100. uint16_t xritag;
  7101. uint16_t abrt_iotag;
  7102. struct lpfc_iocbq *abrtiocbq;
  7103. struct ulp_bde64 *bpl = NULL;
  7104. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  7105. int numBdes, i;
  7106. struct ulp_bde64 bde;
  7107. struct lpfc_nodelist *ndlp;
  7108. uint32_t *pcmd;
  7109. uint32_t if_type;
  7110. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  7111. /* The fcp commands will set command type */
  7112. if (iocbq->iocb_flag & LPFC_IO_FCP)
  7113. command_type = FCP_COMMAND;
  7114. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  7115. command_type = ELS_COMMAND_FIP;
  7116. else
  7117. command_type = ELS_COMMAND_NON_FIP;
  7118. /* Some of the fields are in the right position already */
  7119. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  7120. abort_tag = (uint32_t) iocbq->iotag;
  7121. xritag = iocbq->sli4_xritag;
  7122. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  7123. /* words0-2 bpl convert bde */
  7124. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7125. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7126. sizeof(struct ulp_bde64);
  7127. bpl = (struct ulp_bde64 *)
  7128. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  7129. if (!bpl)
  7130. return IOCB_ERROR;
  7131. /* Should already be byte swapped. */
  7132. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7133. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7134. /* swap the size field back to the cpu so we
  7135. * can assign it to the sgl.
  7136. */
  7137. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7138. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7139. total_len = 0;
  7140. for (i = 0; i < numBdes; i++) {
  7141. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7142. total_len += bde.tus.f.bdeSize;
  7143. }
  7144. } else
  7145. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7146. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7147. cmnd = iocbq->iocb.ulpCommand;
  7148. switch (iocbq->iocb.ulpCommand) {
  7149. case CMD_ELS_REQUEST64_CR:
  7150. if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
  7151. ndlp = iocbq->context_un.ndlp;
  7152. else
  7153. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7154. if (!iocbq->iocb.ulpLe) {
  7155. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7156. "2007 Only Limited Edition cmd Format"
  7157. " supported 0x%x\n",
  7158. iocbq->iocb.ulpCommand);
  7159. return IOCB_ERROR;
  7160. }
  7161. wqe->els_req.payload_len = xmit_len;
  7162. /* Els_reguest64 has a TMO */
  7163. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7164. iocbq->iocb.ulpTimeout);
  7165. /* Need a VF for word 4 set the vf bit*/
  7166. bf_set(els_req64_vf, &wqe->els_req, 0);
  7167. /* And a VFID for word 12 */
  7168. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7169. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7170. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7171. iocbq->iocb.ulpContext);
  7172. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7173. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7174. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7175. if (command_type == ELS_COMMAND_FIP)
  7176. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7177. >> LPFC_FIP_ELS_ID_SHIFT);
  7178. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7179. iocbq->context2)->virt);
  7180. if_type = bf_get(lpfc_sli_intf_if_type,
  7181. &phba->sli4_hba.sli_intf);
  7182. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7183. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7184. *pcmd == ELS_CMD_SCR ||
  7185. *pcmd == ELS_CMD_FDISC ||
  7186. *pcmd == ELS_CMD_LOGO ||
  7187. *pcmd == ELS_CMD_PLOGI)) {
  7188. bf_set(els_req64_sp, &wqe->els_req, 1);
  7189. bf_set(els_req64_sid, &wqe->els_req,
  7190. iocbq->vport->fc_myDID);
  7191. if ((*pcmd == ELS_CMD_FLOGI) &&
  7192. !(phba->fc_topology ==
  7193. LPFC_TOPOLOGY_LOOP))
  7194. bf_set(els_req64_sid, &wqe->els_req, 0);
  7195. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7196. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7197. phba->vpi_ids[iocbq->vport->vpi]);
  7198. } else if (pcmd && iocbq->context1) {
  7199. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7200. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7201. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7202. }
  7203. }
  7204. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7205. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7206. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7207. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7208. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7209. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7210. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7211. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7212. break;
  7213. case CMD_XMIT_SEQUENCE64_CX:
  7214. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7215. iocbq->iocb.un.ulpWord[3]);
  7216. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7217. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7218. /* The entire sequence is transmitted for this IOCB */
  7219. xmit_len = total_len;
  7220. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7221. if (phba->link_flag & LS_LOOPBACK_MODE)
  7222. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7223. case CMD_XMIT_SEQUENCE64_CR:
  7224. /* word3 iocb=io_tag32 wqe=reserved */
  7225. wqe->xmit_sequence.rsvd3 = 0;
  7226. /* word4 relative_offset memcpy */
  7227. /* word5 r_ctl/df_ctl memcpy */
  7228. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7229. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7230. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7231. LPFC_WQE_IOD_WRITE);
  7232. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7233. LPFC_WQE_LENLOC_WORD12);
  7234. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7235. wqe->xmit_sequence.xmit_len = xmit_len;
  7236. command_type = OTHER_COMMAND;
  7237. break;
  7238. case CMD_XMIT_BCAST64_CN:
  7239. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7240. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7241. /* word4 iocb=rsvd wqe=rsvd */
  7242. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7243. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7244. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7245. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7246. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7247. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7248. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7249. LPFC_WQE_LENLOC_WORD3);
  7250. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7251. break;
  7252. case CMD_FCP_IWRITE64_CR:
  7253. command_type = FCP_COMMAND_DATA_OUT;
  7254. /* word3 iocb=iotag wqe=payload_offset_len */
  7255. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7256. wqe->fcp_iwrite.payload_offset_len =
  7257. xmit_len + sizeof(struct fcp_rsp);
  7258. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7259. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7260. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7261. iocbq->iocb.ulpFCP2Rcvy);
  7262. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7263. /* Always open the exchange */
  7264. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7265. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7266. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7267. LPFC_WQE_LENLOC_WORD4);
  7268. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7269. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7270. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7271. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7272. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7273. }
  7274. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7275. break;
  7276. case CMD_FCP_IREAD64_CR:
  7277. /* word3 iocb=iotag wqe=payload_offset_len */
  7278. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7279. wqe->fcp_iread.payload_offset_len =
  7280. xmit_len + sizeof(struct fcp_rsp);
  7281. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7282. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7283. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7284. iocbq->iocb.ulpFCP2Rcvy);
  7285. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7286. /* Always open the exchange */
  7287. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7288. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7289. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7290. LPFC_WQE_LENLOC_WORD4);
  7291. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7292. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7293. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7294. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7295. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7296. }
  7297. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7298. break;
  7299. case CMD_FCP_ICMND64_CR:
  7300. /* word3 iocb=IO_TAG wqe=reserved */
  7301. wqe->fcp_icmd.rsrvd3 = 0;
  7302. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7303. /* Always open the exchange */
  7304. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7305. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7306. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7307. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7308. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7309. LPFC_WQE_LENLOC_NONE);
  7310. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7311. break;
  7312. case CMD_GEN_REQUEST64_CR:
  7313. /* For this command calculate the xmit length of the
  7314. * request bde.
  7315. */
  7316. xmit_len = 0;
  7317. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7318. sizeof(struct ulp_bde64);
  7319. for (i = 0; i < numBdes; i++) {
  7320. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7321. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7322. break;
  7323. xmit_len += bde.tus.f.bdeSize;
  7324. }
  7325. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7326. wqe->gen_req.request_payload_len = xmit_len;
  7327. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7328. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7329. /* word6 context tag copied in memcpy */
  7330. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7331. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7332. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7333. "2015 Invalid CT %x command 0x%x\n",
  7334. ct, iocbq->iocb.ulpCommand);
  7335. return IOCB_ERROR;
  7336. }
  7337. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7338. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7339. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7340. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7341. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7342. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7343. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7344. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7345. command_type = OTHER_COMMAND;
  7346. break;
  7347. case CMD_XMIT_ELS_RSP64_CX:
  7348. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7349. /* words0-2 BDE memcpy */
  7350. /* word3 iocb=iotag32 wqe=response_payload_len */
  7351. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7352. /* word4 */
  7353. wqe->xmit_els_rsp.word4 = 0;
  7354. /* word5 iocb=rsvd wge=did */
  7355. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7356. iocbq->iocb.un.xseq64.xmit_els_remoteID);
  7357. if_type = bf_get(lpfc_sli_intf_if_type,
  7358. &phba->sli4_hba.sli_intf);
  7359. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7360. if (iocbq->vport->fc_flag & FC_PT2PT) {
  7361. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7362. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7363. iocbq->vport->fc_myDID);
  7364. if (iocbq->vport->fc_myDID == Fabric_DID) {
  7365. bf_set(wqe_els_did,
  7366. &wqe->xmit_els_rsp.wqe_dest, 0);
  7367. }
  7368. }
  7369. }
  7370. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7371. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7372. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7373. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7374. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7375. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7376. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7377. phba->vpi_ids[iocbq->vport->vpi]);
  7378. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7379. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7380. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7381. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7382. LPFC_WQE_LENLOC_WORD3);
  7383. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7384. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7385. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7386. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7387. iocbq->context2)->virt);
  7388. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7389. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7390. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7391. iocbq->vport->fc_myDID);
  7392. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
  7393. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7394. phba->vpi_ids[phba->pport->vpi]);
  7395. }
  7396. command_type = OTHER_COMMAND;
  7397. break;
  7398. case CMD_CLOSE_XRI_CN:
  7399. case CMD_ABORT_XRI_CN:
  7400. case CMD_ABORT_XRI_CX:
  7401. /* words 0-2 memcpy should be 0 rserved */
  7402. /* port will send abts */
  7403. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7404. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7405. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7406. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7407. } else
  7408. fip = 0;
  7409. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7410. /*
  7411. * The link is down, or the command was ELS_FIP
  7412. * so the fw does not need to send abts
  7413. * on the wire.
  7414. */
  7415. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7416. else
  7417. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7418. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7419. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7420. wqe->abort_cmd.rsrvd5 = 0;
  7421. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7422. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7423. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7424. /*
  7425. * The abort handler will send us CMD_ABORT_XRI_CN or
  7426. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7427. */
  7428. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7429. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7430. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7431. LPFC_WQE_LENLOC_NONE);
  7432. cmnd = CMD_ABORT_XRI_CX;
  7433. command_type = OTHER_COMMAND;
  7434. xritag = 0;
  7435. break;
  7436. case CMD_XMIT_BLS_RSP64_CX:
  7437. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7438. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7439. * we re-construct this WQE here based on information in
  7440. * iocbq from scratch.
  7441. */
  7442. memset(wqe, 0, sizeof(union lpfc_wqe));
  7443. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7444. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7445. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7446. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7447. LPFC_ABTS_UNSOL_INT) {
  7448. /* ABTS sent by initiator to CT exchange, the
  7449. * RX_ID field will be filled with the newly
  7450. * allocated responder XRI.
  7451. */
  7452. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7453. iocbq->sli4_xritag);
  7454. } else {
  7455. /* ABTS sent by responder to CT exchange, the
  7456. * RX_ID field will be filled with the responder
  7457. * RX_ID from ABTS.
  7458. */
  7459. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7460. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7461. }
  7462. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7463. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7464. /* Use CT=VPI */
  7465. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7466. ndlp->nlp_DID);
  7467. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7468. iocbq->iocb.ulpContext);
  7469. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7470. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7471. phba->vpi_ids[phba->pport->vpi]);
  7472. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7473. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7474. LPFC_WQE_LENLOC_NONE);
  7475. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7476. command_type = OTHER_COMMAND;
  7477. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7478. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7479. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7480. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7481. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7482. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7483. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7484. }
  7485. break;
  7486. case CMD_XRI_ABORTED_CX:
  7487. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7488. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7489. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7490. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7491. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7492. default:
  7493. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7494. "2014 Invalid command 0x%x\n",
  7495. iocbq->iocb.ulpCommand);
  7496. return IOCB_ERROR;
  7497. break;
  7498. }
  7499. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7500. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7501. wqe->generic.wqe_com.abort_tag = abort_tag;
  7502. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7503. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7504. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7505. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7506. return 0;
  7507. }
  7508. /**
  7509. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7510. * @phba: Pointer to HBA context object.
  7511. * @ring_number: SLI ring number to issue iocb on.
  7512. * @piocb: Pointer to command iocb.
  7513. * @flag: Flag indicating if this command can be put into txq.
  7514. *
  7515. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7516. * an iocb command to an HBA with SLI-4 interface spec.
  7517. *
  7518. * This function is called with hbalock held. The function will return success
  7519. * after it successfully submit the iocb to firmware or after adding to the
  7520. * txq.
  7521. **/
  7522. static int
  7523. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7524. struct lpfc_iocbq *piocb, uint32_t flag)
  7525. {
  7526. struct lpfc_sglq *sglq;
  7527. union lpfc_wqe wqe;
  7528. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7529. if (piocb->sli4_xritag == NO_XRI) {
  7530. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7531. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7532. sglq = NULL;
  7533. else {
  7534. if (pring->txq_cnt) {
  7535. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7536. __lpfc_sli_ringtx_put(phba,
  7537. pring, piocb);
  7538. return IOCB_SUCCESS;
  7539. } else {
  7540. return IOCB_BUSY;
  7541. }
  7542. } else {
  7543. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7544. if (!sglq) {
  7545. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7546. __lpfc_sli_ringtx_put(phba,
  7547. pring,
  7548. piocb);
  7549. return IOCB_SUCCESS;
  7550. } else
  7551. return IOCB_BUSY;
  7552. }
  7553. }
  7554. }
  7555. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7556. /* These IO's already have an XRI and a mapped sgl. */
  7557. sglq = NULL;
  7558. } else {
  7559. /*
  7560. * This is a continuation of a commandi,(CX) so this
  7561. * sglq is on the active list
  7562. */
  7563. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  7564. if (!sglq)
  7565. return IOCB_ERROR;
  7566. }
  7567. if (sglq) {
  7568. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7569. piocb->sli4_xritag = sglq->sli4_xritag;
  7570. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7571. return IOCB_ERROR;
  7572. }
  7573. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7574. return IOCB_ERROR;
  7575. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7576. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7577. /*
  7578. * For FCP command IOCB, get a new WQ index to distribute
  7579. * WQE across the WQsr. On the other hand, for abort IOCB,
  7580. * it carries the same WQ index to the original command
  7581. * IOCB.
  7582. */
  7583. if (piocb->iocb_flag & LPFC_IO_FCP)
  7584. piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7585. if (unlikely(!phba->sli4_hba.fcp_wq))
  7586. return IOCB_ERROR;
  7587. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7588. &wqe))
  7589. return IOCB_ERROR;
  7590. } else {
  7591. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7592. return IOCB_ERROR;
  7593. }
  7594. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7595. return 0;
  7596. }
  7597. /**
  7598. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7599. *
  7600. * This routine wraps the actual lockless version for issusing IOCB function
  7601. * pointer from the lpfc_hba struct.
  7602. *
  7603. * Return codes:
  7604. * IOCB_ERROR - Error
  7605. * IOCB_SUCCESS - Success
  7606. * IOCB_BUSY - Busy
  7607. **/
  7608. int
  7609. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7610. struct lpfc_iocbq *piocb, uint32_t flag)
  7611. {
  7612. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7613. }
  7614. /**
  7615. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7616. * @phba: The hba struct for which this call is being executed.
  7617. * @dev_grp: The HBA PCI-Device group number.
  7618. *
  7619. * This routine sets up the SLI interface API function jump table in @phba
  7620. * struct.
  7621. * Returns: 0 - success, -ENODEV - failure.
  7622. **/
  7623. int
  7624. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7625. {
  7626. switch (dev_grp) {
  7627. case LPFC_PCI_DEV_LP:
  7628. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7629. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7630. break;
  7631. case LPFC_PCI_DEV_OC:
  7632. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7633. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7634. break;
  7635. default:
  7636. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7637. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7638. dev_grp);
  7639. return -ENODEV;
  7640. break;
  7641. }
  7642. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7643. return 0;
  7644. }
  7645. /**
  7646. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7647. * @phba: Pointer to HBA context object.
  7648. * @pring: Pointer to driver SLI ring object.
  7649. * @piocb: Pointer to command iocb.
  7650. * @flag: Flag indicating if this command can be put into txq.
  7651. *
  7652. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7653. * function. This function gets the hbalock and calls
  7654. * __lpfc_sli_issue_iocb function and will return the error returned
  7655. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7656. * functions which do not hold hbalock.
  7657. **/
  7658. int
  7659. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7660. struct lpfc_iocbq *piocb, uint32_t flag)
  7661. {
  7662. unsigned long iflags;
  7663. int rc;
  7664. spin_lock_irqsave(&phba->hbalock, iflags);
  7665. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7666. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7667. return rc;
  7668. }
  7669. /**
  7670. * lpfc_extra_ring_setup - Extra ring setup function
  7671. * @phba: Pointer to HBA context object.
  7672. *
  7673. * This function is called while driver attaches with the
  7674. * HBA to setup the extra ring. The extra ring is used
  7675. * only when driver needs to support target mode functionality
  7676. * or IP over FC functionalities.
  7677. *
  7678. * This function is called with no lock held.
  7679. **/
  7680. static int
  7681. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7682. {
  7683. struct lpfc_sli *psli;
  7684. struct lpfc_sli_ring *pring;
  7685. psli = &phba->sli;
  7686. /* Adjust cmd/rsp ring iocb entries more evenly */
  7687. /* Take some away from the FCP ring */
  7688. pring = &psli->ring[psli->fcp_ring];
  7689. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7690. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7691. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7692. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7693. /* and give them to the extra ring */
  7694. pring = &psli->ring[psli->extra_ring];
  7695. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7696. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7697. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7698. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7699. /* Setup default profile for this ring */
  7700. pring->iotag_max = 4096;
  7701. pring->num_mask = 1;
  7702. pring->prt[0].profile = 0; /* Mask 0 */
  7703. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7704. pring->prt[0].type = phba->cfg_multi_ring_type;
  7705. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7706. return 0;
  7707. }
  7708. /* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
  7709. * @vport: pointer to virtual port object.
  7710. * @ndlp: nodelist pointer for the impacted rport.
  7711. *
  7712. * The driver calls this routine in response to a XRI ABORT CQE
  7713. * event from the port. In this event, the driver is required to
  7714. * recover its login to the rport even though its login may be valid
  7715. * from the driver's perspective. The failed ABTS notice from the
  7716. * port indicates the rport is not responding.
  7717. */
  7718. static void
  7719. lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
  7720. struct lpfc_nodelist *ndlp)
  7721. {
  7722. struct Scsi_Host *shost;
  7723. struct lpfc_hba *phba;
  7724. unsigned long flags = 0;
  7725. shost = lpfc_shost_from_vport(vport);
  7726. phba = vport->phba;
  7727. if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
  7728. lpfc_printf_log(phba, KERN_INFO,
  7729. LOG_SLI, "3093 No rport recovery needed. "
  7730. "rport in state 0x%x\n",
  7731. ndlp->nlp_state);
  7732. return;
  7733. }
  7734. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7735. "3094 Start rport recovery on shost id 0x%x "
  7736. "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
  7737. "flags 0x%x\n",
  7738. shost->host_no, ndlp->nlp_DID,
  7739. vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
  7740. ndlp->nlp_flag);
  7741. /*
  7742. * The rport is not responding. Don't attempt ADISC recovery.
  7743. * Remove the FCP-2 flag to force a PLOGI.
  7744. */
  7745. spin_lock_irqsave(shost->host_lock, flags);
  7746. ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  7747. spin_unlock_irqrestore(shost->host_lock, flags);
  7748. lpfc_disc_state_machine(vport, ndlp, NULL,
  7749. NLP_EVT_DEVICE_RECOVERY);
  7750. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  7751. spin_lock_irqsave(shost->host_lock, flags);
  7752. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  7753. spin_unlock_irqrestore(shost->host_lock, flags);
  7754. lpfc_disc_start(vport);
  7755. }
  7756. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7757. * @phba: Pointer to HBA context object.
  7758. * @iocbq: Pointer to iocb object.
  7759. *
  7760. * The async_event handler calls this routine when it receives
  7761. * an ASYNC_STATUS_CN event from the port. The port generates
  7762. * this event when an Abort Sequence request to an rport fails
  7763. * twice in succession. The abort could be originated by the
  7764. * driver or by the port. The ABTS could have been for an ELS
  7765. * or FCP IO. The port only generates this event when an ABTS
  7766. * fails to complete after one retry.
  7767. */
  7768. static void
  7769. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7770. struct lpfc_iocbq *iocbq)
  7771. {
  7772. struct lpfc_nodelist *ndlp = NULL;
  7773. uint16_t rpi = 0, vpi = 0;
  7774. struct lpfc_vport *vport = NULL;
  7775. /* The rpi in the ulpContext is vport-sensitive. */
  7776. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7777. rpi = iocbq->iocb.ulpContext;
  7778. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7779. "3092 Port generated ABTS async event "
  7780. "on vpi %d rpi %d status 0x%x\n",
  7781. vpi, rpi, iocbq->iocb.ulpStatus);
  7782. vport = lpfc_find_vport_by_vpid(phba, vpi);
  7783. if (!vport)
  7784. goto err_exit;
  7785. ndlp = lpfc_findnode_rpi(vport, rpi);
  7786. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7787. goto err_exit;
  7788. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  7789. lpfc_sli_abts_recover_port(vport, ndlp);
  7790. return;
  7791. err_exit:
  7792. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7793. "3095 Event Context not found, no "
  7794. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  7795. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  7796. vpi, rpi);
  7797. }
  7798. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  7799. * @phba: pointer to HBA context object.
  7800. * @ndlp: nodelist pointer for the impacted rport.
  7801. * @axri: pointer to the wcqe containing the failed exchange.
  7802. *
  7803. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  7804. * port. The port generates this event when an abort exchange request to an
  7805. * rport fails twice in succession with no reply. The abort could be originated
  7806. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  7807. */
  7808. void
  7809. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  7810. struct lpfc_nodelist *ndlp,
  7811. struct sli4_wcqe_xri_aborted *axri)
  7812. {
  7813. struct lpfc_vport *vport;
  7814. uint32_t ext_status = 0;
  7815. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  7816. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7817. "3115 Node Context not found, driver "
  7818. "ignoring abts err event\n");
  7819. return;
  7820. }
  7821. vport = ndlp->vport;
  7822. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7823. "3116 Port generated FCP XRI ABORT event on "
  7824. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  7825. ndlp->vport->vpi, ndlp->nlp_rpi,
  7826. bf_get(lpfc_wcqe_xa_xri, axri),
  7827. bf_get(lpfc_wcqe_xa_status, axri),
  7828. axri->parameter);
  7829. /*
  7830. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  7831. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  7832. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  7833. */
  7834. ext_status = axri->parameter & WCQE_PARAM_MASK;
  7835. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  7836. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  7837. lpfc_sli_abts_recover_port(vport, ndlp);
  7838. }
  7839. /**
  7840. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7841. * @phba: Pointer to HBA context object.
  7842. * @pring: Pointer to driver SLI ring object.
  7843. * @iocbq: Pointer to iocb object.
  7844. *
  7845. * This function is called by the slow ring event handler
  7846. * function when there is an ASYNC event iocb in the ring.
  7847. * This function is called with no lock held.
  7848. * Currently this function handles only temperature related
  7849. * ASYNC events. The function decodes the temperature sensor
  7850. * event message and posts events for the management applications.
  7851. **/
  7852. static void
  7853. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7854. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7855. {
  7856. IOCB_t *icmd;
  7857. uint16_t evt_code;
  7858. struct temp_event temp_event_data;
  7859. struct Scsi_Host *shost;
  7860. uint32_t *iocb_w;
  7861. icmd = &iocbq->iocb;
  7862. evt_code = icmd->un.asyncstat.evt_code;
  7863. switch (evt_code) {
  7864. case ASYNC_TEMP_WARN:
  7865. case ASYNC_TEMP_SAFE:
  7866. temp_event_data.data = (uint32_t) icmd->ulpContext;
  7867. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7868. if (evt_code == ASYNC_TEMP_WARN) {
  7869. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7870. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7871. "0347 Adapter is very hot, please take "
  7872. "corrective action. temperature : %d Celsius\n",
  7873. (uint32_t) icmd->ulpContext);
  7874. } else {
  7875. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7876. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7877. "0340 Adapter temperature is OK now. "
  7878. "temperature : %d Celsius\n",
  7879. (uint32_t) icmd->ulpContext);
  7880. }
  7881. /* Send temperature change event to applications */
  7882. shost = lpfc_shost_from_vport(phba->pport);
  7883. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7884. sizeof(temp_event_data), (char *) &temp_event_data,
  7885. LPFC_NL_VENDOR_ID);
  7886. break;
  7887. case ASYNC_STATUS_CN:
  7888. lpfc_sli_abts_err_handler(phba, iocbq);
  7889. break;
  7890. default:
  7891. iocb_w = (uint32_t *) icmd;
  7892. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7893. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7894. " evt_code 0x%x\n"
  7895. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7896. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7897. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7898. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7899. pring->ringno, icmd->un.asyncstat.evt_code,
  7900. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7901. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  7902. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  7903. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  7904. break;
  7905. }
  7906. }
  7907. /**
  7908. * lpfc_sli_setup - SLI ring setup function
  7909. * @phba: Pointer to HBA context object.
  7910. *
  7911. * lpfc_sli_setup sets up rings of the SLI interface with
  7912. * number of iocbs per ring and iotags. This function is
  7913. * called while driver attach to the HBA and before the
  7914. * interrupts are enabled. So there is no need for locking.
  7915. *
  7916. * This function always returns 0.
  7917. **/
  7918. int
  7919. lpfc_sli_setup(struct lpfc_hba *phba)
  7920. {
  7921. int i, totiocbsize = 0;
  7922. struct lpfc_sli *psli = &phba->sli;
  7923. struct lpfc_sli_ring *pring;
  7924. psli->num_rings = MAX_CONFIGURED_RINGS;
  7925. psli->sli_flag = 0;
  7926. psli->fcp_ring = LPFC_FCP_RING;
  7927. psli->next_ring = LPFC_FCP_NEXT_RING;
  7928. psli->extra_ring = LPFC_EXTRA_RING;
  7929. psli->iocbq_lookup = NULL;
  7930. psli->iocbq_lookup_len = 0;
  7931. psli->last_iotag = 0;
  7932. for (i = 0; i < psli->num_rings; i++) {
  7933. pring = &psli->ring[i];
  7934. switch (i) {
  7935. case LPFC_FCP_RING: /* ring 0 - FCP */
  7936. /* numCiocb and numRiocb are used in config_port */
  7937. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  7938. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  7939. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7940. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7941. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7942. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7943. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7944. SLI3_IOCB_CMD_SIZE :
  7945. SLI2_IOCB_CMD_SIZE;
  7946. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7947. SLI3_IOCB_RSP_SIZE :
  7948. SLI2_IOCB_RSP_SIZE;
  7949. pring->iotag_ctr = 0;
  7950. pring->iotag_max =
  7951. (phba->cfg_hba_queue_depth * 2);
  7952. pring->fast_iotag = pring->iotag_max;
  7953. pring->num_mask = 0;
  7954. break;
  7955. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  7956. /* numCiocb and numRiocb are used in config_port */
  7957. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  7958. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  7959. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7960. SLI3_IOCB_CMD_SIZE :
  7961. SLI2_IOCB_CMD_SIZE;
  7962. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7963. SLI3_IOCB_RSP_SIZE :
  7964. SLI2_IOCB_RSP_SIZE;
  7965. pring->iotag_max = phba->cfg_hba_queue_depth;
  7966. pring->num_mask = 0;
  7967. break;
  7968. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  7969. /* numCiocb and numRiocb are used in config_port */
  7970. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  7971. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  7972. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7973. SLI3_IOCB_CMD_SIZE :
  7974. SLI2_IOCB_CMD_SIZE;
  7975. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7976. SLI3_IOCB_RSP_SIZE :
  7977. SLI2_IOCB_RSP_SIZE;
  7978. pring->fast_iotag = 0;
  7979. pring->iotag_ctr = 0;
  7980. pring->iotag_max = 4096;
  7981. pring->lpfc_sli_rcv_async_status =
  7982. lpfc_sli_async_event_handler;
  7983. pring->num_mask = LPFC_MAX_RING_MASK;
  7984. pring->prt[0].profile = 0; /* Mask 0 */
  7985. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  7986. pring->prt[0].type = FC_TYPE_ELS;
  7987. pring->prt[0].lpfc_sli_rcv_unsol_event =
  7988. lpfc_els_unsol_event;
  7989. pring->prt[1].profile = 0; /* Mask 1 */
  7990. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  7991. pring->prt[1].type = FC_TYPE_ELS;
  7992. pring->prt[1].lpfc_sli_rcv_unsol_event =
  7993. lpfc_els_unsol_event;
  7994. pring->prt[2].profile = 0; /* Mask 2 */
  7995. /* NameServer Inquiry */
  7996. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  7997. /* NameServer */
  7998. pring->prt[2].type = FC_TYPE_CT;
  7999. pring->prt[2].lpfc_sli_rcv_unsol_event =
  8000. lpfc_ct_unsol_event;
  8001. pring->prt[3].profile = 0; /* Mask 3 */
  8002. /* NameServer response */
  8003. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  8004. /* NameServer */
  8005. pring->prt[3].type = FC_TYPE_CT;
  8006. pring->prt[3].lpfc_sli_rcv_unsol_event =
  8007. lpfc_ct_unsol_event;
  8008. /* abort unsolicited sequence */
  8009. pring->prt[4].profile = 0; /* Mask 4 */
  8010. pring->prt[4].rctl = FC_RCTL_BA_ABTS;
  8011. pring->prt[4].type = FC_TYPE_BLS;
  8012. pring->prt[4].lpfc_sli_rcv_unsol_event =
  8013. lpfc_sli4_ct_abort_unsol_event;
  8014. break;
  8015. }
  8016. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  8017. (pring->numRiocb * pring->sizeRiocb);
  8018. }
  8019. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  8020. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  8021. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  8022. "SLI2 SLIM Data: x%x x%lx\n",
  8023. phba->brd_no, totiocbsize,
  8024. (unsigned long) MAX_SLIM_IOCB_SIZE);
  8025. }
  8026. if (phba->cfg_multi_ring_support == 2)
  8027. lpfc_extra_ring_setup(phba);
  8028. return 0;
  8029. }
  8030. /**
  8031. * lpfc_sli_queue_setup - Queue initialization function
  8032. * @phba: Pointer to HBA context object.
  8033. *
  8034. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  8035. * ring. This function also initializes ring indices of each ring.
  8036. * This function is called during the initialization of the SLI
  8037. * interface of an HBA.
  8038. * This function is called with no lock held and always returns
  8039. * 1.
  8040. **/
  8041. int
  8042. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  8043. {
  8044. struct lpfc_sli *psli;
  8045. struct lpfc_sli_ring *pring;
  8046. int i;
  8047. psli = &phba->sli;
  8048. spin_lock_irq(&phba->hbalock);
  8049. INIT_LIST_HEAD(&psli->mboxq);
  8050. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  8051. /* Initialize list headers for txq and txcmplq as double linked lists */
  8052. for (i = 0; i < psli->num_rings; i++) {
  8053. pring = &psli->ring[i];
  8054. pring->ringno = i;
  8055. pring->next_cmdidx = 0;
  8056. pring->local_getidx = 0;
  8057. pring->cmdidx = 0;
  8058. INIT_LIST_HEAD(&pring->txq);
  8059. INIT_LIST_HEAD(&pring->txcmplq);
  8060. INIT_LIST_HEAD(&pring->iocb_continueq);
  8061. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  8062. INIT_LIST_HEAD(&pring->postbufq);
  8063. }
  8064. spin_unlock_irq(&phba->hbalock);
  8065. return 1;
  8066. }
  8067. /**
  8068. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  8069. * @phba: Pointer to HBA context object.
  8070. *
  8071. * This routine flushes the mailbox command subsystem. It will unconditionally
  8072. * flush all the mailbox commands in the three possible stages in the mailbox
  8073. * command sub-system: pending mailbox command queue; the outstanding mailbox
  8074. * command; and completed mailbox command queue. It is caller's responsibility
  8075. * to make sure that the driver is in the proper state to flush the mailbox
  8076. * command sub-system. Namely, the posting of mailbox commands into the
  8077. * pending mailbox command queue from the various clients must be stopped;
  8078. * either the HBA is in a state that it will never works on the outstanding
  8079. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  8080. * mailbox command has been completed.
  8081. **/
  8082. static void
  8083. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  8084. {
  8085. LIST_HEAD(completions);
  8086. struct lpfc_sli *psli = &phba->sli;
  8087. LPFC_MBOXQ_t *pmb;
  8088. unsigned long iflag;
  8089. /* Flush all the mailbox commands in the mbox system */
  8090. spin_lock_irqsave(&phba->hbalock, iflag);
  8091. /* The pending mailbox command queue */
  8092. list_splice_init(&phba->sli.mboxq, &completions);
  8093. /* The outstanding active mailbox command */
  8094. if (psli->mbox_active) {
  8095. list_add_tail(&psli->mbox_active->list, &completions);
  8096. psli->mbox_active = NULL;
  8097. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  8098. }
  8099. /* The completed mailbox command queue */
  8100. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  8101. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8102. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  8103. while (!list_empty(&completions)) {
  8104. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  8105. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  8106. if (pmb->mbox_cmpl)
  8107. pmb->mbox_cmpl(phba, pmb);
  8108. }
  8109. }
  8110. /**
  8111. * lpfc_sli_host_down - Vport cleanup function
  8112. * @vport: Pointer to virtual port object.
  8113. *
  8114. * lpfc_sli_host_down is called to clean up the resources
  8115. * associated with a vport before destroying virtual
  8116. * port data structures.
  8117. * This function does following operations:
  8118. * - Free discovery resources associated with this virtual
  8119. * port.
  8120. * - Free iocbs associated with this virtual port in
  8121. * the txq.
  8122. * - Send abort for all iocb commands associated with this
  8123. * vport in txcmplq.
  8124. *
  8125. * This function is called with no lock held and always returns 1.
  8126. **/
  8127. int
  8128. lpfc_sli_host_down(struct lpfc_vport *vport)
  8129. {
  8130. LIST_HEAD(completions);
  8131. struct lpfc_hba *phba = vport->phba;
  8132. struct lpfc_sli *psli = &phba->sli;
  8133. struct lpfc_sli_ring *pring;
  8134. struct lpfc_iocbq *iocb, *next_iocb;
  8135. int i;
  8136. unsigned long flags = 0;
  8137. uint16_t prev_pring_flag;
  8138. lpfc_cleanup_discovery_resources(vport);
  8139. spin_lock_irqsave(&phba->hbalock, flags);
  8140. for (i = 0; i < psli->num_rings; i++) {
  8141. pring = &psli->ring[i];
  8142. prev_pring_flag = pring->flag;
  8143. /* Only slow rings */
  8144. if (pring->ringno == LPFC_ELS_RING) {
  8145. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8146. /* Set the lpfc data pending flag */
  8147. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8148. }
  8149. /*
  8150. * Error everything on the txq since these iocbs have not been
  8151. * given to the FW yet.
  8152. */
  8153. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  8154. if (iocb->vport != vport)
  8155. continue;
  8156. list_move_tail(&iocb->list, &completions);
  8157. pring->txq_cnt--;
  8158. }
  8159. /* Next issue ABTS for everything on the txcmplq */
  8160. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8161. list) {
  8162. if (iocb->vport != vport)
  8163. continue;
  8164. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8165. }
  8166. pring->flag = prev_pring_flag;
  8167. }
  8168. spin_unlock_irqrestore(&phba->hbalock, flags);
  8169. /* Cancel all the IOCBs from the completions list */
  8170. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8171. IOERR_SLI_DOWN);
  8172. return 1;
  8173. }
  8174. /**
  8175. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8176. * @phba: Pointer to HBA context object.
  8177. *
  8178. * This function cleans up all iocb, buffers, mailbox commands
  8179. * while shutting down the HBA. This function is called with no
  8180. * lock held and always returns 1.
  8181. * This function does the following to cleanup driver resources:
  8182. * - Free discovery resources for each virtual port
  8183. * - Cleanup any pending fabric iocbs
  8184. * - Iterate through the iocb txq and free each entry
  8185. * in the list.
  8186. * - Free up any buffer posted to the HBA
  8187. * - Free mailbox commands in the mailbox queue.
  8188. **/
  8189. int
  8190. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8191. {
  8192. LIST_HEAD(completions);
  8193. struct lpfc_sli *psli = &phba->sli;
  8194. struct lpfc_sli_ring *pring;
  8195. struct lpfc_dmabuf *buf_ptr;
  8196. unsigned long flags = 0;
  8197. int i;
  8198. /* Shutdown the mailbox command sub-system */
  8199. lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
  8200. lpfc_hba_down_prep(phba);
  8201. lpfc_fabric_abort_hba(phba);
  8202. spin_lock_irqsave(&phba->hbalock, flags);
  8203. for (i = 0; i < psli->num_rings; i++) {
  8204. pring = &psli->ring[i];
  8205. /* Only slow rings */
  8206. if (pring->ringno == LPFC_ELS_RING) {
  8207. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8208. /* Set the lpfc data pending flag */
  8209. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8210. }
  8211. /*
  8212. * Error everything on the txq since these iocbs have not been
  8213. * given to the FW yet.
  8214. */
  8215. list_splice_init(&pring->txq, &completions);
  8216. pring->txq_cnt = 0;
  8217. }
  8218. spin_unlock_irqrestore(&phba->hbalock, flags);
  8219. /* Cancel all the IOCBs from the completions list */
  8220. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8221. IOERR_SLI_DOWN);
  8222. spin_lock_irqsave(&phba->hbalock, flags);
  8223. list_splice_init(&phba->elsbuf, &completions);
  8224. phba->elsbuf_cnt = 0;
  8225. phba->elsbuf_prev_cnt = 0;
  8226. spin_unlock_irqrestore(&phba->hbalock, flags);
  8227. while (!list_empty(&completions)) {
  8228. list_remove_head(&completions, buf_ptr,
  8229. struct lpfc_dmabuf, list);
  8230. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8231. kfree(buf_ptr);
  8232. }
  8233. /* Return any active mbox cmds */
  8234. del_timer_sync(&psli->mbox_tmo);
  8235. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8236. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8237. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8238. return 1;
  8239. }
  8240. /**
  8241. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8242. * @srcp: Source memory pointer.
  8243. * @destp: Destination memory pointer.
  8244. * @cnt: Number of words required to be copied.
  8245. *
  8246. * This function is used for copying data between driver memory
  8247. * and the SLI memory. This function also changes the endianness
  8248. * of each word if native endianness is different from SLI
  8249. * endianness. This function can be called with or without
  8250. * lock.
  8251. **/
  8252. void
  8253. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8254. {
  8255. uint32_t *src = srcp;
  8256. uint32_t *dest = destp;
  8257. uint32_t ldata;
  8258. int i;
  8259. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8260. ldata = *src;
  8261. ldata = le32_to_cpu(ldata);
  8262. *dest = ldata;
  8263. src++;
  8264. dest++;
  8265. }
  8266. }
  8267. /**
  8268. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8269. * @srcp: Source memory pointer.
  8270. * @destp: Destination memory pointer.
  8271. * @cnt: Number of words required to be copied.
  8272. *
  8273. * This function is used for copying data between a data structure
  8274. * with big endian representation to local endianness.
  8275. * This function can be called with or without lock.
  8276. **/
  8277. void
  8278. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8279. {
  8280. uint32_t *src = srcp;
  8281. uint32_t *dest = destp;
  8282. uint32_t ldata;
  8283. int i;
  8284. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8285. ldata = *src;
  8286. ldata = be32_to_cpu(ldata);
  8287. *dest = ldata;
  8288. src++;
  8289. dest++;
  8290. }
  8291. }
  8292. /**
  8293. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8294. * @phba: Pointer to HBA context object.
  8295. * @pring: Pointer to driver SLI ring object.
  8296. * @mp: Pointer to driver buffer object.
  8297. *
  8298. * This function is called with no lock held.
  8299. * It always return zero after adding the buffer to the postbufq
  8300. * buffer list.
  8301. **/
  8302. int
  8303. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8304. struct lpfc_dmabuf *mp)
  8305. {
  8306. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8307. later */
  8308. spin_lock_irq(&phba->hbalock);
  8309. list_add_tail(&mp->list, &pring->postbufq);
  8310. pring->postbufq_cnt++;
  8311. spin_unlock_irq(&phba->hbalock);
  8312. return 0;
  8313. }
  8314. /**
  8315. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8316. * @phba: Pointer to HBA context object.
  8317. *
  8318. * When HBQ is enabled, buffers are searched based on tags. This function
  8319. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8320. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8321. * does not conflict with tags of buffer posted for unsolicited events.
  8322. * The function returns the allocated tag. The function is called with
  8323. * no locks held.
  8324. **/
  8325. uint32_t
  8326. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8327. {
  8328. spin_lock_irq(&phba->hbalock);
  8329. phba->buffer_tag_count++;
  8330. /*
  8331. * Always set the QUE_BUFTAG_BIT to distiguish between
  8332. * a tag assigned by HBQ.
  8333. */
  8334. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8335. spin_unlock_irq(&phba->hbalock);
  8336. return phba->buffer_tag_count;
  8337. }
  8338. /**
  8339. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8340. * @phba: Pointer to HBA context object.
  8341. * @pring: Pointer to driver SLI ring object.
  8342. * @tag: Buffer tag.
  8343. *
  8344. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8345. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8346. * iocb is posted to the response ring with the tag of the buffer.
  8347. * This function searches the pring->postbufq list using the tag
  8348. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8349. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8350. * buffer is returned to the caller else NULL is returned.
  8351. * This function is called with no lock held.
  8352. **/
  8353. struct lpfc_dmabuf *
  8354. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8355. uint32_t tag)
  8356. {
  8357. struct lpfc_dmabuf *mp, *next_mp;
  8358. struct list_head *slp = &pring->postbufq;
  8359. /* Search postbufq, from the beginning, looking for a match on tag */
  8360. spin_lock_irq(&phba->hbalock);
  8361. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8362. if (mp->buffer_tag == tag) {
  8363. list_del_init(&mp->list);
  8364. pring->postbufq_cnt--;
  8365. spin_unlock_irq(&phba->hbalock);
  8366. return mp;
  8367. }
  8368. }
  8369. spin_unlock_irq(&phba->hbalock);
  8370. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8371. "0402 Cannot find virtual addr for buffer tag on "
  8372. "ring %d Data x%lx x%p x%p x%x\n",
  8373. pring->ringno, (unsigned long) tag,
  8374. slp->next, slp->prev, pring->postbufq_cnt);
  8375. return NULL;
  8376. }
  8377. /**
  8378. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8379. * @phba: Pointer to HBA context object.
  8380. * @pring: Pointer to driver SLI ring object.
  8381. * @phys: DMA address of the buffer.
  8382. *
  8383. * This function searches the buffer list using the dma_address
  8384. * of unsolicited event to find the driver's lpfc_dmabuf object
  8385. * corresponding to the dma_address. The function returns the
  8386. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8387. * This function is called by the ct and els unsolicited event
  8388. * handlers to get the buffer associated with the unsolicited
  8389. * event.
  8390. *
  8391. * This function is called with no lock held.
  8392. **/
  8393. struct lpfc_dmabuf *
  8394. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8395. dma_addr_t phys)
  8396. {
  8397. struct lpfc_dmabuf *mp, *next_mp;
  8398. struct list_head *slp = &pring->postbufq;
  8399. /* Search postbufq, from the beginning, looking for a match on phys */
  8400. spin_lock_irq(&phba->hbalock);
  8401. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8402. if (mp->phys == phys) {
  8403. list_del_init(&mp->list);
  8404. pring->postbufq_cnt--;
  8405. spin_unlock_irq(&phba->hbalock);
  8406. return mp;
  8407. }
  8408. }
  8409. spin_unlock_irq(&phba->hbalock);
  8410. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8411. "0410 Cannot find virtual addr for mapped buf on "
  8412. "ring %d Data x%llx x%p x%p x%x\n",
  8413. pring->ringno, (unsigned long long)phys,
  8414. slp->next, slp->prev, pring->postbufq_cnt);
  8415. return NULL;
  8416. }
  8417. /**
  8418. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8419. * @phba: Pointer to HBA context object.
  8420. * @cmdiocb: Pointer to driver command iocb object.
  8421. * @rspiocb: Pointer to driver response iocb object.
  8422. *
  8423. * This function is the completion handler for the abort iocbs for
  8424. * ELS commands. This function is called from the ELS ring event
  8425. * handler with no lock held. This function frees memory resources
  8426. * associated with the abort iocb.
  8427. **/
  8428. static void
  8429. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8430. struct lpfc_iocbq *rspiocb)
  8431. {
  8432. IOCB_t *irsp = &rspiocb->iocb;
  8433. uint16_t abort_iotag, abort_context;
  8434. struct lpfc_iocbq *abort_iocb = NULL;
  8435. if (irsp->ulpStatus) {
  8436. /*
  8437. * Assume that the port already completed and returned, or
  8438. * will return the iocb. Just Log the message.
  8439. */
  8440. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8441. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8442. spin_lock_irq(&phba->hbalock);
  8443. if (phba->sli_rev < LPFC_SLI_REV4) {
  8444. if (abort_iotag != 0 &&
  8445. abort_iotag <= phba->sli.last_iotag)
  8446. abort_iocb =
  8447. phba->sli.iocbq_lookup[abort_iotag];
  8448. } else
  8449. /* For sli4 the abort_tag is the XRI,
  8450. * so the abort routine puts the iotag of the iocb
  8451. * being aborted in the context field of the abort
  8452. * IOCB.
  8453. */
  8454. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8455. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8456. "0327 Cannot abort els iocb %p "
  8457. "with tag %x context %x, abort status %x, "
  8458. "abort code %x\n",
  8459. abort_iocb, abort_iotag, abort_context,
  8460. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8461. spin_unlock_irq(&phba->hbalock);
  8462. }
  8463. lpfc_sli_release_iocbq(phba, cmdiocb);
  8464. return;
  8465. }
  8466. /**
  8467. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8468. * @phba: Pointer to HBA context object.
  8469. * @cmdiocb: Pointer to driver command iocb object.
  8470. * @rspiocb: Pointer to driver response iocb object.
  8471. *
  8472. * The function is called from SLI ring event handler with no
  8473. * lock held. This function is the completion handler for ELS commands
  8474. * which are aborted. The function frees memory resources used for
  8475. * the aborted ELS commands.
  8476. **/
  8477. static void
  8478. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8479. struct lpfc_iocbq *rspiocb)
  8480. {
  8481. IOCB_t *irsp = &rspiocb->iocb;
  8482. /* ELS cmd tag <ulpIoTag> completes */
  8483. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8484. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8485. "x%x x%x x%x\n",
  8486. irsp->ulpIoTag, irsp->ulpStatus,
  8487. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8488. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8489. lpfc_ct_free_iocb(phba, cmdiocb);
  8490. else
  8491. lpfc_els_free_iocb(phba, cmdiocb);
  8492. return;
  8493. }
  8494. /**
  8495. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8496. * @phba: Pointer to HBA context object.
  8497. * @pring: Pointer to driver SLI ring object.
  8498. * @cmdiocb: Pointer to driver command iocb object.
  8499. *
  8500. * This function issues an abort iocb for the provided command iocb down to
  8501. * the port. Other than the case the outstanding command iocb is an abort
  8502. * request, this function issues abort out unconditionally. This function is
  8503. * called with hbalock held. The function returns 0 when it fails due to
  8504. * memory allocation failure or when the command iocb is an abort request.
  8505. **/
  8506. static int
  8507. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8508. struct lpfc_iocbq *cmdiocb)
  8509. {
  8510. struct lpfc_vport *vport = cmdiocb->vport;
  8511. struct lpfc_iocbq *abtsiocbp;
  8512. IOCB_t *icmd = NULL;
  8513. IOCB_t *iabt = NULL;
  8514. int retval;
  8515. /*
  8516. * There are certain command types we don't want to abort. And we
  8517. * don't want to abort commands that are already in the process of
  8518. * being aborted.
  8519. */
  8520. icmd = &cmdiocb->iocb;
  8521. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8522. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8523. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8524. return 0;
  8525. /* issue ABTS for this IOCB based on iotag */
  8526. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8527. if (abtsiocbp == NULL)
  8528. return 0;
  8529. /* This signals the response to set the correct status
  8530. * before calling the completion handler
  8531. */
  8532. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8533. iabt = &abtsiocbp->iocb;
  8534. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8535. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8536. if (phba->sli_rev == LPFC_SLI_REV4) {
  8537. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8538. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8539. }
  8540. else
  8541. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8542. iabt->ulpLe = 1;
  8543. iabt->ulpClass = icmd->ulpClass;
  8544. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8545. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8546. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8547. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8548. if (phba->link_state >= LPFC_LINK_UP)
  8549. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8550. else
  8551. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8552. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8553. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8554. "0339 Abort xri x%x, original iotag x%x, "
  8555. "abort cmd iotag x%x\n",
  8556. iabt->un.acxri.abortIoTag,
  8557. iabt->un.acxri.abortContextTag,
  8558. abtsiocbp->iotag);
  8559. retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
  8560. if (retval)
  8561. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8562. /*
  8563. * Caller to this routine should check for IOCB_ERROR
  8564. * and handle it properly. This routine no longer removes
  8565. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8566. */
  8567. return retval;
  8568. }
  8569. /**
  8570. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8571. * @phba: Pointer to HBA context object.
  8572. * @pring: Pointer to driver SLI ring object.
  8573. * @cmdiocb: Pointer to driver command iocb object.
  8574. *
  8575. * This function issues an abort iocb for the provided command iocb. In case
  8576. * of unloading, the abort iocb will not be issued to commands on the ELS
  8577. * ring. Instead, the callback function shall be changed to those commands
  8578. * so that nothing happens when them finishes. This function is called with
  8579. * hbalock held. The function returns 0 when the command iocb is an abort
  8580. * request.
  8581. **/
  8582. int
  8583. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8584. struct lpfc_iocbq *cmdiocb)
  8585. {
  8586. struct lpfc_vport *vport = cmdiocb->vport;
  8587. int retval = IOCB_ERROR;
  8588. IOCB_t *icmd = NULL;
  8589. /*
  8590. * There are certain command types we don't want to abort. And we
  8591. * don't want to abort commands that are already in the process of
  8592. * being aborted.
  8593. */
  8594. icmd = &cmdiocb->iocb;
  8595. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8596. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8597. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8598. return 0;
  8599. /*
  8600. * If we're unloading, don't abort iocb on the ELS ring, but change
  8601. * the callback so that nothing happens when it finishes.
  8602. */
  8603. if ((vport->load_flag & FC_UNLOADING) &&
  8604. (pring->ringno == LPFC_ELS_RING)) {
  8605. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8606. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8607. else
  8608. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8609. goto abort_iotag_exit;
  8610. }
  8611. /* Now, we try to issue the abort to the cmdiocb out */
  8612. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8613. abort_iotag_exit:
  8614. /*
  8615. * Caller to this routine should check for IOCB_ERROR
  8616. * and handle it properly. This routine no longer removes
  8617. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8618. */
  8619. return retval;
  8620. }
  8621. /**
  8622. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8623. * @phba: Pointer to HBA context object.
  8624. * @pring: Pointer to driver SLI ring object.
  8625. *
  8626. * This function aborts all iocbs in the given ring and frees all the iocb
  8627. * objects in txq. This function issues abort iocbs unconditionally for all
  8628. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8629. * to complete before the return of this function. The caller is not required
  8630. * to hold any locks.
  8631. **/
  8632. static void
  8633. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8634. {
  8635. LIST_HEAD(completions);
  8636. struct lpfc_iocbq *iocb, *next_iocb;
  8637. if (pring->ringno == LPFC_ELS_RING)
  8638. lpfc_fabric_abort_hba(phba);
  8639. spin_lock_irq(&phba->hbalock);
  8640. /* Take off all the iocbs on txq for cancelling */
  8641. list_splice_init(&pring->txq, &completions);
  8642. pring->txq_cnt = 0;
  8643. /* Next issue ABTS for everything on the txcmplq */
  8644. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8645. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8646. spin_unlock_irq(&phba->hbalock);
  8647. /* Cancel all the IOCBs from the completions list */
  8648. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8649. IOERR_SLI_ABORTED);
  8650. }
  8651. /**
  8652. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8653. * @phba: pointer to lpfc HBA data structure.
  8654. *
  8655. * This routine will abort all pending and outstanding iocbs to an HBA.
  8656. **/
  8657. void
  8658. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8659. {
  8660. struct lpfc_sli *psli = &phba->sli;
  8661. struct lpfc_sli_ring *pring;
  8662. int i;
  8663. for (i = 0; i < psli->num_rings; i++) {
  8664. pring = &psli->ring[i];
  8665. lpfc_sli_iocb_ring_abort(phba, pring);
  8666. }
  8667. }
  8668. /**
  8669. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8670. * @iocbq: Pointer to driver iocb object.
  8671. * @vport: Pointer to driver virtual port object.
  8672. * @tgt_id: SCSI ID of the target.
  8673. * @lun_id: LUN ID of the scsi device.
  8674. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8675. *
  8676. * This function acts as an iocb filter for functions which abort or count
  8677. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8678. * 0 if the filtering criteria is met for the given iocb and will return
  8679. * 1 if the filtering criteria is not met.
  8680. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8681. * given iocb is for the SCSI device specified by vport, tgt_id and
  8682. * lun_id parameter.
  8683. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8684. * given iocb is for the SCSI target specified by vport and tgt_id
  8685. * parameters.
  8686. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8687. * given iocb is for the SCSI host associated with the given vport.
  8688. * This function is called with no locks held.
  8689. **/
  8690. static int
  8691. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8692. uint16_t tgt_id, uint64_t lun_id,
  8693. lpfc_ctx_cmd ctx_cmd)
  8694. {
  8695. struct lpfc_scsi_buf *lpfc_cmd;
  8696. int rc = 1;
  8697. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8698. return rc;
  8699. if (iocbq->vport != vport)
  8700. return rc;
  8701. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8702. if (lpfc_cmd->pCmd == NULL)
  8703. return rc;
  8704. switch (ctx_cmd) {
  8705. case LPFC_CTX_LUN:
  8706. if ((lpfc_cmd->rdata->pnode) &&
  8707. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8708. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8709. rc = 0;
  8710. break;
  8711. case LPFC_CTX_TGT:
  8712. if ((lpfc_cmd->rdata->pnode) &&
  8713. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8714. rc = 0;
  8715. break;
  8716. case LPFC_CTX_HOST:
  8717. rc = 0;
  8718. break;
  8719. default:
  8720. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8721. __func__, ctx_cmd);
  8722. break;
  8723. }
  8724. return rc;
  8725. }
  8726. /**
  8727. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8728. * @vport: Pointer to virtual port.
  8729. * @tgt_id: SCSI ID of the target.
  8730. * @lun_id: LUN ID of the scsi device.
  8731. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8732. *
  8733. * This function returns number of FCP commands pending for the vport.
  8734. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8735. * commands pending on the vport associated with SCSI device specified
  8736. * by tgt_id and lun_id parameters.
  8737. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8738. * commands pending on the vport associated with SCSI target specified
  8739. * by tgt_id parameter.
  8740. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8741. * commands pending on the vport.
  8742. * This function returns the number of iocbs which satisfy the filter.
  8743. * This function is called without any lock held.
  8744. **/
  8745. int
  8746. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8747. lpfc_ctx_cmd ctx_cmd)
  8748. {
  8749. struct lpfc_hba *phba = vport->phba;
  8750. struct lpfc_iocbq *iocbq;
  8751. int sum, i;
  8752. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8753. iocbq = phba->sli.iocbq_lookup[i];
  8754. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8755. ctx_cmd) == 0)
  8756. sum++;
  8757. }
  8758. return sum;
  8759. }
  8760. /**
  8761. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8762. * @phba: Pointer to HBA context object
  8763. * @cmdiocb: Pointer to command iocb object.
  8764. * @rspiocb: Pointer to response iocb object.
  8765. *
  8766. * This function is called when an aborted FCP iocb completes. This
  8767. * function is called by the ring event handler with no lock held.
  8768. * This function frees the iocb.
  8769. **/
  8770. void
  8771. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8772. struct lpfc_iocbq *rspiocb)
  8773. {
  8774. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8775. "3096 ABORT_XRI_CN completing on xri x%x "
  8776. "original iotag x%x, abort cmd iotag x%x "
  8777. "status 0x%x, reason 0x%x\n",
  8778. cmdiocb->iocb.un.acxri.abortContextTag,
  8779. cmdiocb->iocb.un.acxri.abortIoTag,
  8780. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  8781. rspiocb->iocb.un.ulpWord[4]);
  8782. lpfc_sli_release_iocbq(phba, cmdiocb);
  8783. return;
  8784. }
  8785. /**
  8786. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8787. * @vport: Pointer to virtual port.
  8788. * @pring: Pointer to driver SLI ring object.
  8789. * @tgt_id: SCSI ID of the target.
  8790. * @lun_id: LUN ID of the scsi device.
  8791. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8792. *
  8793. * This function sends an abort command for every SCSI command
  8794. * associated with the given virtual port pending on the ring
  8795. * filtered by lpfc_sli_validate_fcp_iocb function.
  8796. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8797. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8798. * parameters
  8799. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8800. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8801. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8802. * FCP iocbs associated with virtual port.
  8803. * This function returns number of iocbs it failed to abort.
  8804. * This function is called with no locks held.
  8805. **/
  8806. int
  8807. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8808. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8809. {
  8810. struct lpfc_hba *phba = vport->phba;
  8811. struct lpfc_iocbq *iocbq;
  8812. struct lpfc_iocbq *abtsiocb;
  8813. IOCB_t *cmd = NULL;
  8814. int errcnt = 0, ret_val = 0;
  8815. int i;
  8816. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8817. iocbq = phba->sli.iocbq_lookup[i];
  8818. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8819. abort_cmd) != 0)
  8820. continue;
  8821. /* issue ABTS for this IOCB based on iotag */
  8822. abtsiocb = lpfc_sli_get_iocbq(phba);
  8823. if (abtsiocb == NULL) {
  8824. errcnt++;
  8825. continue;
  8826. }
  8827. cmd = &iocbq->iocb;
  8828. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8829. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8830. if (phba->sli_rev == LPFC_SLI_REV4)
  8831. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8832. else
  8833. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8834. abtsiocb->iocb.ulpLe = 1;
  8835. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8836. abtsiocb->vport = phba->pport;
  8837. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8838. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8839. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8840. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8841. if (lpfc_is_link_up(phba))
  8842. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8843. else
  8844. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8845. /* Setup callback routine and issue the command. */
  8846. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8847. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8848. abtsiocb, 0);
  8849. if (ret_val == IOCB_ERROR) {
  8850. lpfc_sli_release_iocbq(phba, abtsiocb);
  8851. errcnt++;
  8852. continue;
  8853. }
  8854. }
  8855. return errcnt;
  8856. }
  8857. /**
  8858. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8859. * @phba: Pointer to HBA context object.
  8860. * @cmdiocbq: Pointer to command iocb.
  8861. * @rspiocbq: Pointer to response iocb.
  8862. *
  8863. * This function is the completion handler for iocbs issued using
  8864. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8865. * ring event handler function without any lock held. This function
  8866. * can be called from both worker thread context and interrupt
  8867. * context. This function also can be called from other thread which
  8868. * cleans up the SLI layer objects.
  8869. * This function copy the contents of the response iocb to the
  8870. * response iocb memory object provided by the caller of
  8871. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8872. * sleeps for the iocb completion.
  8873. **/
  8874. static void
  8875. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8876. struct lpfc_iocbq *cmdiocbq,
  8877. struct lpfc_iocbq *rspiocbq)
  8878. {
  8879. wait_queue_head_t *pdone_q;
  8880. unsigned long iflags;
  8881. struct lpfc_scsi_buf *lpfc_cmd;
  8882. spin_lock_irqsave(&phba->hbalock, iflags);
  8883. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8884. if (cmdiocbq->context2 && rspiocbq)
  8885. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8886. &rspiocbq->iocb, sizeof(IOCB_t));
  8887. /* Set the exchange busy flag for task management commands */
  8888. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8889. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  8890. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  8891. cur_iocbq);
  8892. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  8893. }
  8894. pdone_q = cmdiocbq->context_un.wait_queue;
  8895. if (pdone_q)
  8896. wake_up(pdone_q);
  8897. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8898. return;
  8899. }
  8900. /**
  8901. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  8902. * @phba: Pointer to HBA context object..
  8903. * @piocbq: Pointer to command iocb.
  8904. * @flag: Flag to test.
  8905. *
  8906. * This routine grabs the hbalock and then test the iocb_flag to
  8907. * see if the passed in flag is set.
  8908. * Returns:
  8909. * 1 if flag is set.
  8910. * 0 if flag is not set.
  8911. **/
  8912. static int
  8913. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  8914. struct lpfc_iocbq *piocbq, uint32_t flag)
  8915. {
  8916. unsigned long iflags;
  8917. int ret;
  8918. spin_lock_irqsave(&phba->hbalock, iflags);
  8919. ret = piocbq->iocb_flag & flag;
  8920. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8921. return ret;
  8922. }
  8923. /**
  8924. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  8925. * @phba: Pointer to HBA context object..
  8926. * @pring: Pointer to sli ring.
  8927. * @piocb: Pointer to command iocb.
  8928. * @prspiocbq: Pointer to response iocb.
  8929. * @timeout: Timeout in number of seconds.
  8930. *
  8931. * This function issues the iocb to firmware and waits for the
  8932. * iocb to complete. If the iocb command is not
  8933. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  8934. * Caller should not free the iocb resources if this function
  8935. * returns IOCB_TIMEDOUT.
  8936. * The function waits for the iocb completion using an
  8937. * non-interruptible wait.
  8938. * This function will sleep while waiting for iocb completion.
  8939. * So, this function should not be called from any context which
  8940. * does not allow sleeping. Due to the same reason, this function
  8941. * cannot be called with interrupt disabled.
  8942. * This function assumes that the iocb completions occur while
  8943. * this function sleep. So, this function cannot be called from
  8944. * the thread which process iocb completion for this ring.
  8945. * This function clears the iocb_flag of the iocb object before
  8946. * issuing the iocb and the iocb completion handler sets this
  8947. * flag and wakes this thread when the iocb completes.
  8948. * The contents of the response iocb will be copied to prspiocbq
  8949. * by the completion handler when the command completes.
  8950. * This function returns IOCB_SUCCESS when success.
  8951. * This function is called with no lock held.
  8952. **/
  8953. int
  8954. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  8955. uint32_t ring_number,
  8956. struct lpfc_iocbq *piocb,
  8957. struct lpfc_iocbq *prspiocbq,
  8958. uint32_t timeout)
  8959. {
  8960. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8961. long timeleft, timeout_req = 0;
  8962. int retval = IOCB_SUCCESS;
  8963. uint32_t creg_val;
  8964. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  8965. /*
  8966. * If the caller has provided a response iocbq buffer, then context2
  8967. * is NULL or its an error.
  8968. */
  8969. if (prspiocbq) {
  8970. if (piocb->context2)
  8971. return IOCB_ERROR;
  8972. piocb->context2 = prspiocbq;
  8973. }
  8974. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  8975. piocb->context_un.wait_queue = &done_q;
  8976. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  8977. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8978. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8979. return IOCB_ERROR;
  8980. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  8981. writel(creg_val, phba->HCregaddr);
  8982. readl(phba->HCregaddr); /* flush */
  8983. }
  8984. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  8985. SLI_IOCB_RET_IOCB);
  8986. if (retval == IOCB_SUCCESS) {
  8987. timeout_req = timeout * HZ;
  8988. timeleft = wait_event_timeout(done_q,
  8989. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  8990. timeout_req);
  8991. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  8992. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8993. "0331 IOCB wake signaled\n");
  8994. } else if (timeleft == 0) {
  8995. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8996. "0338 IOCB wait timeout error - no "
  8997. "wake response Data x%x\n", timeout);
  8998. retval = IOCB_TIMEDOUT;
  8999. } else {
  9000. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9001. "0330 IOCB wake NOT set, "
  9002. "Data x%x x%lx\n",
  9003. timeout, (timeleft / jiffies));
  9004. retval = IOCB_TIMEDOUT;
  9005. }
  9006. } else if (retval == IOCB_BUSY) {
  9007. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9008. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  9009. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  9010. return retval;
  9011. } else {
  9012. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9013. "0332 IOCB wait issue failed, Data x%x\n",
  9014. retval);
  9015. retval = IOCB_ERROR;
  9016. }
  9017. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9018. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9019. return IOCB_ERROR;
  9020. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  9021. writel(creg_val, phba->HCregaddr);
  9022. readl(phba->HCregaddr); /* flush */
  9023. }
  9024. if (prspiocbq)
  9025. piocb->context2 = NULL;
  9026. piocb->context_un.wait_queue = NULL;
  9027. piocb->iocb_cmpl = NULL;
  9028. return retval;
  9029. }
  9030. /**
  9031. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  9032. * @phba: Pointer to HBA context object.
  9033. * @pmboxq: Pointer to driver mailbox object.
  9034. * @timeout: Timeout in number of seconds.
  9035. *
  9036. * This function issues the mailbox to firmware and waits for the
  9037. * mailbox command to complete. If the mailbox command is not
  9038. * completed within timeout seconds, it returns MBX_TIMEOUT.
  9039. * The function waits for the mailbox completion using an
  9040. * interruptible wait. If the thread is woken up due to a
  9041. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  9042. * should not free the mailbox resources, if this function returns
  9043. * MBX_TIMEOUT.
  9044. * This function will sleep while waiting for mailbox completion.
  9045. * So, this function should not be called from any context which
  9046. * does not allow sleeping. Due to the same reason, this function
  9047. * cannot be called with interrupt disabled.
  9048. * This function assumes that the mailbox completion occurs while
  9049. * this function sleep. So, this function cannot be called from
  9050. * the worker thread which processes mailbox completion.
  9051. * This function is called in the context of HBA management
  9052. * applications.
  9053. * This function returns MBX_SUCCESS when successful.
  9054. * This function is called with no lock held.
  9055. **/
  9056. int
  9057. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  9058. uint32_t timeout)
  9059. {
  9060. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9061. int retval;
  9062. unsigned long flag;
  9063. /* The caller must leave context1 empty. */
  9064. if (pmboxq->context1)
  9065. return MBX_NOT_FINISHED;
  9066. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  9067. /* setup wake call as IOCB callback */
  9068. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  9069. /* setup context field to pass wait_queue pointer to wake function */
  9070. pmboxq->context1 = &done_q;
  9071. /* now issue the command */
  9072. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  9073. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  9074. wait_event_interruptible_timeout(done_q,
  9075. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  9076. timeout * HZ);
  9077. spin_lock_irqsave(&phba->hbalock, flag);
  9078. pmboxq->context1 = NULL;
  9079. /*
  9080. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  9081. * else do not free the resources.
  9082. */
  9083. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  9084. retval = MBX_SUCCESS;
  9085. lpfc_sli4_swap_str(phba, pmboxq);
  9086. } else {
  9087. retval = MBX_TIMEOUT;
  9088. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9089. }
  9090. spin_unlock_irqrestore(&phba->hbalock, flag);
  9091. }
  9092. return retval;
  9093. }
  9094. /**
  9095. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  9096. * @phba: Pointer to HBA context.
  9097. *
  9098. * This function is called to shutdown the driver's mailbox sub-system.
  9099. * It first marks the mailbox sub-system is in a block state to prevent
  9100. * the asynchronous mailbox command from issued off the pending mailbox
  9101. * command queue. If the mailbox command sub-system shutdown is due to
  9102. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  9103. * the mailbox sub-system flush routine to forcefully bring down the
  9104. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  9105. * as with offline or HBA function reset), this routine will wait for the
  9106. * outstanding mailbox command to complete before invoking the mailbox
  9107. * sub-system flush routine to gracefully bring down mailbox sub-system.
  9108. **/
  9109. void
  9110. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
  9111. {
  9112. struct lpfc_sli *psli = &phba->sli;
  9113. unsigned long timeout;
  9114. if (mbx_action == LPFC_MBX_NO_WAIT) {
  9115. /* delay 100ms for port state */
  9116. msleep(100);
  9117. lpfc_sli_mbox_sys_flush(phba);
  9118. return;
  9119. }
  9120. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  9121. spin_lock_irq(&phba->hbalock);
  9122. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  9123. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  9124. /* Determine how long we might wait for the active mailbox
  9125. * command to be gracefully completed by firmware.
  9126. */
  9127. if (phba->sli.mbox_active)
  9128. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  9129. phba->sli.mbox_active) *
  9130. 1000) + jiffies;
  9131. spin_unlock_irq(&phba->hbalock);
  9132. while (phba->sli.mbox_active) {
  9133. /* Check active mailbox complete status every 2ms */
  9134. msleep(2);
  9135. if (time_after(jiffies, timeout))
  9136. /* Timeout, let the mailbox flush routine to
  9137. * forcefully release active mailbox command
  9138. */
  9139. break;
  9140. }
  9141. } else
  9142. spin_unlock_irq(&phba->hbalock);
  9143. lpfc_sli_mbox_sys_flush(phba);
  9144. }
  9145. /**
  9146. * lpfc_sli_eratt_read - read sli-3 error attention events
  9147. * @phba: Pointer to HBA context.
  9148. *
  9149. * This function is called to read the SLI3 device error attention registers
  9150. * for possible error attention events. The caller must hold the hostlock
  9151. * with spin_lock_irq().
  9152. *
  9153. * This function returns 1 when there is Error Attention in the Host Attention
  9154. * Register and returns 0 otherwise.
  9155. **/
  9156. static int
  9157. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  9158. {
  9159. uint32_t ha_copy;
  9160. /* Read chip Host Attention (HA) register */
  9161. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9162. goto unplug_err;
  9163. if (ha_copy & HA_ERATT) {
  9164. /* Read host status register to retrieve error event */
  9165. if (lpfc_sli_read_hs(phba))
  9166. goto unplug_err;
  9167. /* Check if there is a deferred error condition is active */
  9168. if ((HS_FFER1 & phba->work_hs) &&
  9169. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9170. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9171. phba->hba_flag |= DEFER_ERATT;
  9172. /* Clear all interrupt enable conditions */
  9173. writel(0, phba->HCregaddr);
  9174. readl(phba->HCregaddr);
  9175. }
  9176. /* Set the driver HA work bitmap */
  9177. phba->work_ha |= HA_ERATT;
  9178. /* Indicate polling handles this ERATT */
  9179. phba->hba_flag |= HBA_ERATT_HANDLED;
  9180. return 1;
  9181. }
  9182. return 0;
  9183. unplug_err:
  9184. /* Set the driver HS work bitmap */
  9185. phba->work_hs |= UNPLUG_ERR;
  9186. /* Set the driver HA work bitmap */
  9187. phba->work_ha |= HA_ERATT;
  9188. /* Indicate polling handles this ERATT */
  9189. phba->hba_flag |= HBA_ERATT_HANDLED;
  9190. return 1;
  9191. }
  9192. /**
  9193. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9194. * @phba: Pointer to HBA context.
  9195. *
  9196. * This function is called to read the SLI4 device error attention registers
  9197. * for possible error attention events. The caller must hold the hostlock
  9198. * with spin_lock_irq().
  9199. *
  9200. * This function returns 1 when there is Error Attention in the Host Attention
  9201. * Register and returns 0 otherwise.
  9202. **/
  9203. static int
  9204. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9205. {
  9206. uint32_t uerr_sta_hi, uerr_sta_lo;
  9207. uint32_t if_type, portsmphr;
  9208. struct lpfc_register portstat_reg;
  9209. /*
  9210. * For now, use the SLI4 device internal unrecoverable error
  9211. * registers for error attention. This can be changed later.
  9212. */
  9213. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9214. switch (if_type) {
  9215. case LPFC_SLI_INTF_IF_TYPE_0:
  9216. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9217. &uerr_sta_lo) ||
  9218. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9219. &uerr_sta_hi)) {
  9220. phba->work_hs |= UNPLUG_ERR;
  9221. phba->work_ha |= HA_ERATT;
  9222. phba->hba_flag |= HBA_ERATT_HANDLED;
  9223. return 1;
  9224. }
  9225. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9226. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9227. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9228. "1423 HBA Unrecoverable error: "
  9229. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9230. "ue_mask_lo_reg=0x%x, "
  9231. "ue_mask_hi_reg=0x%x\n",
  9232. uerr_sta_lo, uerr_sta_hi,
  9233. phba->sli4_hba.ue_mask_lo,
  9234. phba->sli4_hba.ue_mask_hi);
  9235. phba->work_status[0] = uerr_sta_lo;
  9236. phba->work_status[1] = uerr_sta_hi;
  9237. phba->work_ha |= HA_ERATT;
  9238. phba->hba_flag |= HBA_ERATT_HANDLED;
  9239. return 1;
  9240. }
  9241. break;
  9242. case LPFC_SLI_INTF_IF_TYPE_2:
  9243. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9244. &portstat_reg.word0) ||
  9245. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9246. &portsmphr)){
  9247. phba->work_hs |= UNPLUG_ERR;
  9248. phba->work_ha |= HA_ERATT;
  9249. phba->hba_flag |= HBA_ERATT_HANDLED;
  9250. return 1;
  9251. }
  9252. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9253. phba->work_status[0] =
  9254. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9255. phba->work_status[1] =
  9256. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9257. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9258. "2885 Port Status Event: "
  9259. "port status reg 0x%x, "
  9260. "port smphr reg 0x%x, "
  9261. "error 1=0x%x, error 2=0x%x\n",
  9262. portstat_reg.word0,
  9263. portsmphr,
  9264. phba->work_status[0],
  9265. phba->work_status[1]);
  9266. phba->work_ha |= HA_ERATT;
  9267. phba->hba_flag |= HBA_ERATT_HANDLED;
  9268. return 1;
  9269. }
  9270. break;
  9271. case LPFC_SLI_INTF_IF_TYPE_1:
  9272. default:
  9273. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9274. "2886 HBA Error Attention on unsupported "
  9275. "if type %d.", if_type);
  9276. return 1;
  9277. }
  9278. return 0;
  9279. }
  9280. /**
  9281. * lpfc_sli_check_eratt - check error attention events
  9282. * @phba: Pointer to HBA context.
  9283. *
  9284. * This function is called from timer soft interrupt context to check HBA's
  9285. * error attention register bit for error attention events.
  9286. *
  9287. * This function returns 1 when there is Error Attention in the Host Attention
  9288. * Register and returns 0 otherwise.
  9289. **/
  9290. int
  9291. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9292. {
  9293. uint32_t ha_copy;
  9294. /* If somebody is waiting to handle an eratt, don't process it
  9295. * here. The brdkill function will do this.
  9296. */
  9297. if (phba->link_flag & LS_IGNORE_ERATT)
  9298. return 0;
  9299. /* Check if interrupt handler handles this ERATT */
  9300. spin_lock_irq(&phba->hbalock);
  9301. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9302. /* Interrupt handler has handled ERATT */
  9303. spin_unlock_irq(&phba->hbalock);
  9304. return 0;
  9305. }
  9306. /*
  9307. * If there is deferred error attention, do not check for error
  9308. * attention
  9309. */
  9310. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9311. spin_unlock_irq(&phba->hbalock);
  9312. return 0;
  9313. }
  9314. /* If PCI channel is offline, don't process it */
  9315. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9316. spin_unlock_irq(&phba->hbalock);
  9317. return 0;
  9318. }
  9319. switch (phba->sli_rev) {
  9320. case LPFC_SLI_REV2:
  9321. case LPFC_SLI_REV3:
  9322. /* Read chip Host Attention (HA) register */
  9323. ha_copy = lpfc_sli_eratt_read(phba);
  9324. break;
  9325. case LPFC_SLI_REV4:
  9326. /* Read device Uncoverable Error (UERR) registers */
  9327. ha_copy = lpfc_sli4_eratt_read(phba);
  9328. break;
  9329. default:
  9330. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9331. "0299 Invalid SLI revision (%d)\n",
  9332. phba->sli_rev);
  9333. ha_copy = 0;
  9334. break;
  9335. }
  9336. spin_unlock_irq(&phba->hbalock);
  9337. return ha_copy;
  9338. }
  9339. /**
  9340. * lpfc_intr_state_check - Check device state for interrupt handling
  9341. * @phba: Pointer to HBA context.
  9342. *
  9343. * This inline routine checks whether a device or its PCI slot is in a state
  9344. * that the interrupt should be handled.
  9345. *
  9346. * This function returns 0 if the device or the PCI slot is in a state that
  9347. * interrupt should be handled, otherwise -EIO.
  9348. */
  9349. static inline int
  9350. lpfc_intr_state_check(struct lpfc_hba *phba)
  9351. {
  9352. /* If the pci channel is offline, ignore all the interrupts */
  9353. if (unlikely(pci_channel_offline(phba->pcidev)))
  9354. return -EIO;
  9355. /* Update device level interrupt statistics */
  9356. phba->sli.slistat.sli_intr++;
  9357. /* Ignore all interrupts during initialization. */
  9358. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9359. return -EIO;
  9360. return 0;
  9361. }
  9362. /**
  9363. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9364. * @irq: Interrupt number.
  9365. * @dev_id: The device context pointer.
  9366. *
  9367. * This function is directly called from the PCI layer as an interrupt
  9368. * service routine when device with SLI-3 interface spec is enabled with
  9369. * MSI-X multi-message interrupt mode and there are slow-path events in
  9370. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9371. * interrupt mode, this function is called as part of the device-level
  9372. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9373. * is undergoing initialization, the interrupt handler will not process
  9374. * the interrupt. The link attention and ELS ring attention events are
  9375. * handled by the worker thread. The interrupt handler signals the worker
  9376. * thread and returns for these events. This function is called without
  9377. * any lock held. It gets the hbalock to access and update SLI data
  9378. * structures.
  9379. *
  9380. * This function returns IRQ_HANDLED when interrupt is handled else it
  9381. * returns IRQ_NONE.
  9382. **/
  9383. irqreturn_t
  9384. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9385. {
  9386. struct lpfc_hba *phba;
  9387. uint32_t ha_copy, hc_copy;
  9388. uint32_t work_ha_copy;
  9389. unsigned long status;
  9390. unsigned long iflag;
  9391. uint32_t control;
  9392. MAILBOX_t *mbox, *pmbox;
  9393. struct lpfc_vport *vport;
  9394. struct lpfc_nodelist *ndlp;
  9395. struct lpfc_dmabuf *mp;
  9396. LPFC_MBOXQ_t *pmb;
  9397. int rc;
  9398. /*
  9399. * Get the driver's phba structure from the dev_id and
  9400. * assume the HBA is not interrupting.
  9401. */
  9402. phba = (struct lpfc_hba *)dev_id;
  9403. if (unlikely(!phba))
  9404. return IRQ_NONE;
  9405. /*
  9406. * Stuff needs to be attented to when this function is invoked as an
  9407. * individual interrupt handler in MSI-X multi-message interrupt mode
  9408. */
  9409. if (phba->intr_type == MSIX) {
  9410. /* Check device state for handling interrupt */
  9411. if (lpfc_intr_state_check(phba))
  9412. return IRQ_NONE;
  9413. /* Need to read HA REG for slow-path events */
  9414. spin_lock_irqsave(&phba->hbalock, iflag);
  9415. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9416. goto unplug_error;
  9417. /* If somebody is waiting to handle an eratt don't process it
  9418. * here. The brdkill function will do this.
  9419. */
  9420. if (phba->link_flag & LS_IGNORE_ERATT)
  9421. ha_copy &= ~HA_ERATT;
  9422. /* Check the need for handling ERATT in interrupt handler */
  9423. if (ha_copy & HA_ERATT) {
  9424. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9425. /* ERATT polling has handled ERATT */
  9426. ha_copy &= ~HA_ERATT;
  9427. else
  9428. /* Indicate interrupt handler handles ERATT */
  9429. phba->hba_flag |= HBA_ERATT_HANDLED;
  9430. }
  9431. /*
  9432. * If there is deferred error attention, do not check for any
  9433. * interrupt.
  9434. */
  9435. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9436. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9437. return IRQ_NONE;
  9438. }
  9439. /* Clear up only attention source related to slow-path */
  9440. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9441. goto unplug_error;
  9442. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9443. HC_LAINT_ENA | HC_ERINT_ENA),
  9444. phba->HCregaddr);
  9445. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9446. phba->HAregaddr);
  9447. writel(hc_copy, phba->HCregaddr);
  9448. readl(phba->HAregaddr); /* flush */
  9449. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9450. } else
  9451. ha_copy = phba->ha_copy;
  9452. work_ha_copy = ha_copy & phba->work_ha_mask;
  9453. if (work_ha_copy) {
  9454. if (work_ha_copy & HA_LATT) {
  9455. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9456. /*
  9457. * Turn off Link Attention interrupts
  9458. * until CLEAR_LA done
  9459. */
  9460. spin_lock_irqsave(&phba->hbalock, iflag);
  9461. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9462. if (lpfc_readl(phba->HCregaddr, &control))
  9463. goto unplug_error;
  9464. control &= ~HC_LAINT_ENA;
  9465. writel(control, phba->HCregaddr);
  9466. readl(phba->HCregaddr); /* flush */
  9467. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9468. }
  9469. else
  9470. work_ha_copy &= ~HA_LATT;
  9471. }
  9472. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9473. /*
  9474. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9475. * the only slow ring.
  9476. */
  9477. status = (work_ha_copy &
  9478. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9479. status >>= (4*LPFC_ELS_RING);
  9480. if (status & HA_RXMASK) {
  9481. spin_lock_irqsave(&phba->hbalock, iflag);
  9482. if (lpfc_readl(phba->HCregaddr, &control))
  9483. goto unplug_error;
  9484. lpfc_debugfs_slow_ring_trc(phba,
  9485. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9486. control, status,
  9487. (uint32_t)phba->sli.slistat.sli_intr);
  9488. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9489. lpfc_debugfs_slow_ring_trc(phba,
  9490. "ISR Disable ring:"
  9491. "pwork:x%x hawork:x%x wait:x%x",
  9492. phba->work_ha, work_ha_copy,
  9493. (uint32_t)((unsigned long)
  9494. &phba->work_waitq));
  9495. control &=
  9496. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9497. writel(control, phba->HCregaddr);
  9498. readl(phba->HCregaddr); /* flush */
  9499. }
  9500. else {
  9501. lpfc_debugfs_slow_ring_trc(phba,
  9502. "ISR slow ring: pwork:"
  9503. "x%x hawork:x%x wait:x%x",
  9504. phba->work_ha, work_ha_copy,
  9505. (uint32_t)((unsigned long)
  9506. &phba->work_waitq));
  9507. }
  9508. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9509. }
  9510. }
  9511. spin_lock_irqsave(&phba->hbalock, iflag);
  9512. if (work_ha_copy & HA_ERATT) {
  9513. if (lpfc_sli_read_hs(phba))
  9514. goto unplug_error;
  9515. /*
  9516. * Check if there is a deferred error condition
  9517. * is active
  9518. */
  9519. if ((HS_FFER1 & phba->work_hs) &&
  9520. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9521. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9522. phba->work_hs)) {
  9523. phba->hba_flag |= DEFER_ERATT;
  9524. /* Clear all interrupt enable conditions */
  9525. writel(0, phba->HCregaddr);
  9526. readl(phba->HCregaddr);
  9527. }
  9528. }
  9529. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9530. pmb = phba->sli.mbox_active;
  9531. pmbox = &pmb->u.mb;
  9532. mbox = phba->mbox;
  9533. vport = pmb->vport;
  9534. /* First check out the status word */
  9535. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9536. if (pmbox->mbxOwner != OWN_HOST) {
  9537. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9538. /*
  9539. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9540. * mbxStatus <status>
  9541. */
  9542. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9543. LOG_SLI,
  9544. "(%d):0304 Stray Mailbox "
  9545. "Interrupt mbxCommand x%x "
  9546. "mbxStatus x%x\n",
  9547. (vport ? vport->vpi : 0),
  9548. pmbox->mbxCommand,
  9549. pmbox->mbxStatus);
  9550. /* clear mailbox attention bit */
  9551. work_ha_copy &= ~HA_MBATT;
  9552. } else {
  9553. phba->sli.mbox_active = NULL;
  9554. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9555. phba->last_completion_time = jiffies;
  9556. del_timer(&phba->sli.mbox_tmo);
  9557. if (pmb->mbox_cmpl) {
  9558. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9559. MAILBOX_CMD_SIZE);
  9560. if (pmb->out_ext_byte_len &&
  9561. pmb->context2)
  9562. lpfc_sli_pcimem_bcopy(
  9563. phba->mbox_ext,
  9564. pmb->context2,
  9565. pmb->out_ext_byte_len);
  9566. }
  9567. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9568. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9569. lpfc_debugfs_disc_trc(vport,
  9570. LPFC_DISC_TRC_MBOX_VPORT,
  9571. "MBOX dflt rpi: : "
  9572. "status:x%x rpi:x%x",
  9573. (uint32_t)pmbox->mbxStatus,
  9574. pmbox->un.varWords[0], 0);
  9575. if (!pmbox->mbxStatus) {
  9576. mp = (struct lpfc_dmabuf *)
  9577. (pmb->context1);
  9578. ndlp = (struct lpfc_nodelist *)
  9579. pmb->context2;
  9580. /* Reg_LOGIN of dflt RPI was
  9581. * successful. new lets get
  9582. * rid of the RPI using the
  9583. * same mbox buffer.
  9584. */
  9585. lpfc_unreg_login(phba,
  9586. vport->vpi,
  9587. pmbox->un.varWords[0],
  9588. pmb);
  9589. pmb->mbox_cmpl =
  9590. lpfc_mbx_cmpl_dflt_rpi;
  9591. pmb->context1 = mp;
  9592. pmb->context2 = ndlp;
  9593. pmb->vport = vport;
  9594. rc = lpfc_sli_issue_mbox(phba,
  9595. pmb,
  9596. MBX_NOWAIT);
  9597. if (rc != MBX_BUSY)
  9598. lpfc_printf_log(phba,
  9599. KERN_ERR,
  9600. LOG_MBOX | LOG_SLI,
  9601. "0350 rc should have"
  9602. "been MBX_BUSY\n");
  9603. if (rc != MBX_NOT_FINISHED)
  9604. goto send_current_mbox;
  9605. }
  9606. }
  9607. spin_lock_irqsave(
  9608. &phba->pport->work_port_lock,
  9609. iflag);
  9610. phba->pport->work_port_events &=
  9611. ~WORKER_MBOX_TMO;
  9612. spin_unlock_irqrestore(
  9613. &phba->pport->work_port_lock,
  9614. iflag);
  9615. lpfc_mbox_cmpl_put(phba, pmb);
  9616. }
  9617. } else
  9618. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9619. if ((work_ha_copy & HA_MBATT) &&
  9620. (phba->sli.mbox_active == NULL)) {
  9621. send_current_mbox:
  9622. /* Process next mailbox command if there is one */
  9623. do {
  9624. rc = lpfc_sli_issue_mbox(phba, NULL,
  9625. MBX_NOWAIT);
  9626. } while (rc == MBX_NOT_FINISHED);
  9627. if (rc != MBX_SUCCESS)
  9628. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9629. LOG_SLI, "0349 rc should be "
  9630. "MBX_SUCCESS\n");
  9631. }
  9632. spin_lock_irqsave(&phba->hbalock, iflag);
  9633. phba->work_ha |= work_ha_copy;
  9634. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9635. lpfc_worker_wake_up(phba);
  9636. }
  9637. return IRQ_HANDLED;
  9638. unplug_error:
  9639. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9640. return IRQ_HANDLED;
  9641. } /* lpfc_sli_sp_intr_handler */
  9642. /**
  9643. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9644. * @irq: Interrupt number.
  9645. * @dev_id: The device context pointer.
  9646. *
  9647. * This function is directly called from the PCI layer as an interrupt
  9648. * service routine when device with SLI-3 interface spec is enabled with
  9649. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9650. * ring event in the HBA. However, when the device is enabled with either
  9651. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9652. * device-level interrupt handler. When the PCI slot is in error recovery
  9653. * or the HBA is undergoing initialization, the interrupt handler will not
  9654. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9655. * the intrrupt context. This function is called without any lock held.
  9656. * It gets the hbalock to access and update SLI data structures.
  9657. *
  9658. * This function returns IRQ_HANDLED when interrupt is handled else it
  9659. * returns IRQ_NONE.
  9660. **/
  9661. irqreturn_t
  9662. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9663. {
  9664. struct lpfc_hba *phba;
  9665. uint32_t ha_copy;
  9666. unsigned long status;
  9667. unsigned long iflag;
  9668. /* Get the driver's phba structure from the dev_id and
  9669. * assume the HBA is not interrupting.
  9670. */
  9671. phba = (struct lpfc_hba *) dev_id;
  9672. if (unlikely(!phba))
  9673. return IRQ_NONE;
  9674. /*
  9675. * Stuff needs to be attented to when this function is invoked as an
  9676. * individual interrupt handler in MSI-X multi-message interrupt mode
  9677. */
  9678. if (phba->intr_type == MSIX) {
  9679. /* Check device state for handling interrupt */
  9680. if (lpfc_intr_state_check(phba))
  9681. return IRQ_NONE;
  9682. /* Need to read HA REG for FCP ring and other ring events */
  9683. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9684. return IRQ_HANDLED;
  9685. /* Clear up only attention source related to fast-path */
  9686. spin_lock_irqsave(&phba->hbalock, iflag);
  9687. /*
  9688. * If there is deferred error attention, do not check for
  9689. * any interrupt.
  9690. */
  9691. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9692. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9693. return IRQ_NONE;
  9694. }
  9695. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9696. phba->HAregaddr);
  9697. readl(phba->HAregaddr); /* flush */
  9698. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9699. } else
  9700. ha_copy = phba->ha_copy;
  9701. /*
  9702. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9703. */
  9704. ha_copy &= ~(phba->work_ha_mask);
  9705. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9706. status >>= (4*LPFC_FCP_RING);
  9707. if (status & HA_RXMASK)
  9708. lpfc_sli_handle_fast_ring_event(phba,
  9709. &phba->sli.ring[LPFC_FCP_RING],
  9710. status);
  9711. if (phba->cfg_multi_ring_support == 2) {
  9712. /*
  9713. * Process all events on extra ring. Take the optimized path
  9714. * for extra ring IO.
  9715. */
  9716. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9717. status >>= (4*LPFC_EXTRA_RING);
  9718. if (status & HA_RXMASK) {
  9719. lpfc_sli_handle_fast_ring_event(phba,
  9720. &phba->sli.ring[LPFC_EXTRA_RING],
  9721. status);
  9722. }
  9723. }
  9724. return IRQ_HANDLED;
  9725. } /* lpfc_sli_fp_intr_handler */
  9726. /**
  9727. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9728. * @irq: Interrupt number.
  9729. * @dev_id: The device context pointer.
  9730. *
  9731. * This function is the HBA device-level interrupt handler to device with
  9732. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9733. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9734. * requires driver attention. This function invokes the slow-path interrupt
  9735. * attention handling function and fast-path interrupt attention handling
  9736. * function in turn to process the relevant HBA attention events. This
  9737. * function is called without any lock held. It gets the hbalock to access
  9738. * and update SLI data structures.
  9739. *
  9740. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9741. * returns IRQ_NONE.
  9742. **/
  9743. irqreturn_t
  9744. lpfc_sli_intr_handler(int irq, void *dev_id)
  9745. {
  9746. struct lpfc_hba *phba;
  9747. irqreturn_t sp_irq_rc, fp_irq_rc;
  9748. unsigned long status1, status2;
  9749. uint32_t hc_copy;
  9750. /*
  9751. * Get the driver's phba structure from the dev_id and
  9752. * assume the HBA is not interrupting.
  9753. */
  9754. phba = (struct lpfc_hba *) dev_id;
  9755. if (unlikely(!phba))
  9756. return IRQ_NONE;
  9757. /* Check device state for handling interrupt */
  9758. if (lpfc_intr_state_check(phba))
  9759. return IRQ_NONE;
  9760. spin_lock(&phba->hbalock);
  9761. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9762. spin_unlock(&phba->hbalock);
  9763. return IRQ_HANDLED;
  9764. }
  9765. if (unlikely(!phba->ha_copy)) {
  9766. spin_unlock(&phba->hbalock);
  9767. return IRQ_NONE;
  9768. } else if (phba->ha_copy & HA_ERATT) {
  9769. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9770. /* ERATT polling has handled ERATT */
  9771. phba->ha_copy &= ~HA_ERATT;
  9772. else
  9773. /* Indicate interrupt handler handles ERATT */
  9774. phba->hba_flag |= HBA_ERATT_HANDLED;
  9775. }
  9776. /*
  9777. * If there is deferred error attention, do not check for any interrupt.
  9778. */
  9779. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9780. spin_unlock(&phba->hbalock);
  9781. return IRQ_NONE;
  9782. }
  9783. /* Clear attention sources except link and error attentions */
  9784. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9785. spin_unlock(&phba->hbalock);
  9786. return IRQ_HANDLED;
  9787. }
  9788. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9789. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9790. phba->HCregaddr);
  9791. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9792. writel(hc_copy, phba->HCregaddr);
  9793. readl(phba->HAregaddr); /* flush */
  9794. spin_unlock(&phba->hbalock);
  9795. /*
  9796. * Invokes slow-path host attention interrupt handling as appropriate.
  9797. */
  9798. /* status of events with mailbox and link attention */
  9799. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9800. /* status of events with ELS ring */
  9801. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9802. status2 >>= (4*LPFC_ELS_RING);
  9803. if (status1 || (status2 & HA_RXMASK))
  9804. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9805. else
  9806. sp_irq_rc = IRQ_NONE;
  9807. /*
  9808. * Invoke fast-path host attention interrupt handling as appropriate.
  9809. */
  9810. /* status of events with FCP ring */
  9811. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9812. status1 >>= (4*LPFC_FCP_RING);
  9813. /* status of events with extra ring */
  9814. if (phba->cfg_multi_ring_support == 2) {
  9815. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9816. status2 >>= (4*LPFC_EXTRA_RING);
  9817. } else
  9818. status2 = 0;
  9819. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9820. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9821. else
  9822. fp_irq_rc = IRQ_NONE;
  9823. /* Return device-level interrupt handling status */
  9824. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9825. } /* lpfc_sli_intr_handler */
  9826. /**
  9827. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9828. * @phba: pointer to lpfc hba data structure.
  9829. *
  9830. * This routine is invoked by the worker thread to process all the pending
  9831. * SLI4 FCP abort XRI events.
  9832. **/
  9833. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9834. {
  9835. struct lpfc_cq_event *cq_event;
  9836. /* First, declare the fcp xri abort event has been handled */
  9837. spin_lock_irq(&phba->hbalock);
  9838. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9839. spin_unlock_irq(&phba->hbalock);
  9840. /* Now, handle all the fcp xri abort events */
  9841. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9842. /* Get the first event from the head of the event queue */
  9843. spin_lock_irq(&phba->hbalock);
  9844. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9845. cq_event, struct lpfc_cq_event, list);
  9846. spin_unlock_irq(&phba->hbalock);
  9847. /* Notify aborted XRI for FCP work queue */
  9848. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9849. /* Free the event processed back to the free pool */
  9850. lpfc_sli4_cq_event_release(phba, cq_event);
  9851. }
  9852. }
  9853. /**
  9854. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9855. * @phba: pointer to lpfc hba data structure.
  9856. *
  9857. * This routine is invoked by the worker thread to process all the pending
  9858. * SLI4 els abort xri events.
  9859. **/
  9860. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9861. {
  9862. struct lpfc_cq_event *cq_event;
  9863. /* First, declare the els xri abort event has been handled */
  9864. spin_lock_irq(&phba->hbalock);
  9865. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9866. spin_unlock_irq(&phba->hbalock);
  9867. /* Now, handle all the els xri abort events */
  9868. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9869. /* Get the first event from the head of the event queue */
  9870. spin_lock_irq(&phba->hbalock);
  9871. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9872. cq_event, struct lpfc_cq_event, list);
  9873. spin_unlock_irq(&phba->hbalock);
  9874. /* Notify aborted XRI for ELS work queue */
  9875. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9876. /* Free the event processed back to the free pool */
  9877. lpfc_sli4_cq_event_release(phba, cq_event);
  9878. }
  9879. }
  9880. /**
  9881. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9882. * @phba: pointer to lpfc hba data structure
  9883. * @pIocbIn: pointer to the rspiocbq
  9884. * @pIocbOut: pointer to the cmdiocbq
  9885. * @wcqe: pointer to the complete wcqe
  9886. *
  9887. * This routine transfers the fields of a command iocbq to a response iocbq
  9888. * by copying all the IOCB fields from command iocbq and transferring the
  9889. * completion status information from the complete wcqe.
  9890. **/
  9891. static void
  9892. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  9893. struct lpfc_iocbq *pIocbIn,
  9894. struct lpfc_iocbq *pIocbOut,
  9895. struct lpfc_wcqe_complete *wcqe)
  9896. {
  9897. unsigned long iflags;
  9898. uint32_t status;
  9899. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  9900. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  9901. sizeof(struct lpfc_iocbq) - offset);
  9902. /* Map WCQE parameters into irspiocb parameters */
  9903. status = bf_get(lpfc_wcqe_c_status, wcqe);
  9904. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  9905. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  9906. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  9907. pIocbIn->iocb.un.fcpi.fcpi_parm =
  9908. pIocbOut->iocb.un.fcpi.fcpi_parm -
  9909. wcqe->total_data_placed;
  9910. else
  9911. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9912. else {
  9913. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9914. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  9915. }
  9916. /* Convert BG errors for completion status */
  9917. if (status == CQE_STATUS_DI_ERROR) {
  9918. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  9919. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  9920. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  9921. else
  9922. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  9923. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  9924. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  9925. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9926. BGS_GUARD_ERR_MASK;
  9927. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  9928. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9929. BGS_APPTAG_ERR_MASK;
  9930. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  9931. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9932. BGS_REFTAG_ERR_MASK;
  9933. /* Check to see if there was any good data before the error */
  9934. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  9935. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9936. BGS_HI_WATER_MARK_PRESENT_MASK;
  9937. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  9938. wcqe->total_data_placed;
  9939. }
  9940. /*
  9941. * Set ALL the error bits to indicate we don't know what
  9942. * type of error it is.
  9943. */
  9944. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  9945. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9946. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  9947. BGS_GUARD_ERR_MASK);
  9948. }
  9949. /* Pick up HBA exchange busy condition */
  9950. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  9951. spin_lock_irqsave(&phba->hbalock, iflags);
  9952. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  9953. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9954. }
  9955. }
  9956. /**
  9957. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  9958. * @phba: Pointer to HBA context object.
  9959. * @wcqe: Pointer to work-queue completion queue entry.
  9960. *
  9961. * This routine handles an ELS work-queue completion event and construct
  9962. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  9963. * discovery engine to handle.
  9964. *
  9965. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  9966. **/
  9967. static struct lpfc_iocbq *
  9968. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  9969. struct lpfc_iocbq *irspiocbq)
  9970. {
  9971. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9972. struct lpfc_iocbq *cmdiocbq;
  9973. struct lpfc_wcqe_complete *wcqe;
  9974. unsigned long iflags;
  9975. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  9976. spin_lock_irqsave(&phba->hbalock, iflags);
  9977. pring->stats.iocb_event++;
  9978. /* Look up the ELS command IOCB and create pseudo response IOCB */
  9979. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  9980. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9981. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9982. if (unlikely(!cmdiocbq)) {
  9983. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9984. "0386 ELS complete with no corresponding "
  9985. "cmdiocb: iotag (%d)\n",
  9986. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9987. lpfc_sli_release_iocbq(phba, irspiocbq);
  9988. return NULL;
  9989. }
  9990. /* Fake the irspiocbq and copy necessary response information */
  9991. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  9992. return irspiocbq;
  9993. }
  9994. /**
  9995. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  9996. * @phba: Pointer to HBA context object.
  9997. * @cqe: Pointer to mailbox completion queue entry.
  9998. *
  9999. * This routine process a mailbox completion queue entry with asynchrous
  10000. * event.
  10001. *
  10002. * Return: true if work posted to worker thread, otherwise false.
  10003. **/
  10004. static bool
  10005. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10006. {
  10007. struct lpfc_cq_event *cq_event;
  10008. unsigned long iflags;
  10009. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10010. "0392 Async Event: word0:x%x, word1:x%x, "
  10011. "word2:x%x, word3:x%x\n", mcqe->word0,
  10012. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  10013. /* Allocate a new internal CQ_EVENT entry */
  10014. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10015. if (!cq_event) {
  10016. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10017. "0394 Failed to allocate CQ_EVENT entry\n");
  10018. return false;
  10019. }
  10020. /* Move the CQE into an asynchronous event entry */
  10021. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  10022. spin_lock_irqsave(&phba->hbalock, iflags);
  10023. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  10024. /* Set the async event flag */
  10025. phba->hba_flag |= ASYNC_EVENT;
  10026. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10027. return true;
  10028. }
  10029. /**
  10030. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  10031. * @phba: Pointer to HBA context object.
  10032. * @cqe: Pointer to mailbox completion queue entry.
  10033. *
  10034. * This routine process a mailbox completion queue entry with mailbox
  10035. * completion event.
  10036. *
  10037. * Return: true if work posted to worker thread, otherwise false.
  10038. **/
  10039. static bool
  10040. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10041. {
  10042. uint32_t mcqe_status;
  10043. MAILBOX_t *mbox, *pmbox;
  10044. struct lpfc_mqe *mqe;
  10045. struct lpfc_vport *vport;
  10046. struct lpfc_nodelist *ndlp;
  10047. struct lpfc_dmabuf *mp;
  10048. unsigned long iflags;
  10049. LPFC_MBOXQ_t *pmb;
  10050. bool workposted = false;
  10051. int rc;
  10052. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  10053. if (!bf_get(lpfc_trailer_completed, mcqe))
  10054. goto out_no_mqe_complete;
  10055. /* Get the reference to the active mbox command */
  10056. spin_lock_irqsave(&phba->hbalock, iflags);
  10057. pmb = phba->sli.mbox_active;
  10058. if (unlikely(!pmb)) {
  10059. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10060. "1832 No pending MBOX command to handle\n");
  10061. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10062. goto out_no_mqe_complete;
  10063. }
  10064. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10065. mqe = &pmb->u.mqe;
  10066. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  10067. mbox = phba->mbox;
  10068. vport = pmb->vport;
  10069. /* Reset heartbeat timer */
  10070. phba->last_completion_time = jiffies;
  10071. del_timer(&phba->sli.mbox_tmo);
  10072. /* Move mbox data to caller's mailbox region, do endian swapping */
  10073. if (pmb->mbox_cmpl && mbox)
  10074. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  10075. /*
  10076. * For mcqe errors, conditionally move a modified error code to
  10077. * the mbox so that the error will not be missed.
  10078. */
  10079. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  10080. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  10081. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  10082. bf_set(lpfc_mqe_status, mqe,
  10083. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  10084. }
  10085. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10086. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10087. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  10088. "MBOX dflt rpi: status:x%x rpi:x%x",
  10089. mcqe_status,
  10090. pmbox->un.varWords[0], 0);
  10091. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  10092. mp = (struct lpfc_dmabuf *)(pmb->context1);
  10093. ndlp = (struct lpfc_nodelist *)pmb->context2;
  10094. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  10095. * RID of the PPI using the same mbox buffer.
  10096. */
  10097. lpfc_unreg_login(phba, vport->vpi,
  10098. pmbox->un.varWords[0], pmb);
  10099. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  10100. pmb->context1 = mp;
  10101. pmb->context2 = ndlp;
  10102. pmb->vport = vport;
  10103. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  10104. if (rc != MBX_BUSY)
  10105. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10106. LOG_SLI, "0385 rc should "
  10107. "have been MBX_BUSY\n");
  10108. if (rc != MBX_NOT_FINISHED)
  10109. goto send_current_mbox;
  10110. }
  10111. }
  10112. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  10113. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  10114. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  10115. /* There is mailbox completion work to do */
  10116. spin_lock_irqsave(&phba->hbalock, iflags);
  10117. __lpfc_mbox_cmpl_put(phba, pmb);
  10118. phba->work_ha |= HA_MBATT;
  10119. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10120. workposted = true;
  10121. send_current_mbox:
  10122. spin_lock_irqsave(&phba->hbalock, iflags);
  10123. /* Release the mailbox command posting token */
  10124. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10125. /* Setting active mailbox pointer need to be in sync to flag clear */
  10126. phba->sli.mbox_active = NULL;
  10127. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10128. /* Wake up worker thread to post the next pending mailbox command */
  10129. lpfc_worker_wake_up(phba);
  10130. out_no_mqe_complete:
  10131. if (bf_get(lpfc_trailer_consumed, mcqe))
  10132. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  10133. return workposted;
  10134. }
  10135. /**
  10136. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  10137. * @phba: Pointer to HBA context object.
  10138. * @cqe: Pointer to mailbox completion queue entry.
  10139. *
  10140. * This routine process a mailbox completion queue entry, it invokes the
  10141. * proper mailbox complete handling or asynchrous event handling routine
  10142. * according to the MCQE's async bit.
  10143. *
  10144. * Return: true if work posted to worker thread, otherwise false.
  10145. **/
  10146. static bool
  10147. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  10148. {
  10149. struct lpfc_mcqe mcqe;
  10150. bool workposted;
  10151. /* Copy the mailbox MCQE and convert endian order as needed */
  10152. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  10153. /* Invoke the proper event handling routine */
  10154. if (!bf_get(lpfc_trailer_async, &mcqe))
  10155. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  10156. else
  10157. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  10158. return workposted;
  10159. }
  10160. /**
  10161. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  10162. * @phba: Pointer to HBA context object.
  10163. * @wcqe: Pointer to work-queue completion queue entry.
  10164. *
  10165. * This routine handles an ELS work-queue completion event.
  10166. *
  10167. * Return: true if work posted to worker thread, otherwise false.
  10168. **/
  10169. static bool
  10170. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
  10171. struct lpfc_wcqe_complete *wcqe)
  10172. {
  10173. struct lpfc_iocbq *irspiocbq;
  10174. unsigned long iflags;
  10175. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10176. /* Get an irspiocbq for later ELS response processing use */
  10177. irspiocbq = lpfc_sli_get_iocbq(phba);
  10178. if (!irspiocbq) {
  10179. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10180. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10181. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10182. pring->txq_cnt, phba->iocb_cnt,
  10183. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  10184. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  10185. return false;
  10186. }
  10187. /* Save off the slow-path queue event for work thread to process */
  10188. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10189. spin_lock_irqsave(&phba->hbalock, iflags);
  10190. list_add_tail(&irspiocbq->cq_event.list,
  10191. &phba->sli4_hba.sp_queue_event);
  10192. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10193. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10194. return true;
  10195. }
  10196. /**
  10197. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10198. * @phba: Pointer to HBA context object.
  10199. * @wcqe: Pointer to work-queue completion queue entry.
  10200. *
  10201. * This routine handles slow-path WQ entry comsumed event by invoking the
  10202. * proper WQ release routine to the slow-path WQ.
  10203. **/
  10204. static void
  10205. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10206. struct lpfc_wcqe_release *wcqe)
  10207. {
  10208. /* sanity check on queue memory */
  10209. if (unlikely(!phba->sli4_hba.els_wq))
  10210. return;
  10211. /* Check for the slow-path ELS work queue */
  10212. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10213. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10214. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10215. else
  10216. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10217. "2579 Slow-path wqe consume event carries "
  10218. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10219. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10220. phba->sli4_hba.els_wq->queue_id);
  10221. }
  10222. /**
  10223. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10224. * @phba: Pointer to HBA context object.
  10225. * @cq: Pointer to a WQ completion queue.
  10226. * @wcqe: Pointer to work-queue completion queue entry.
  10227. *
  10228. * This routine handles an XRI abort event.
  10229. *
  10230. * Return: true if work posted to worker thread, otherwise false.
  10231. **/
  10232. static bool
  10233. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10234. struct lpfc_queue *cq,
  10235. struct sli4_wcqe_xri_aborted *wcqe)
  10236. {
  10237. bool workposted = false;
  10238. struct lpfc_cq_event *cq_event;
  10239. unsigned long iflags;
  10240. /* Allocate a new internal CQ_EVENT entry */
  10241. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10242. if (!cq_event) {
  10243. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10244. "0602 Failed to allocate CQ_EVENT entry\n");
  10245. return false;
  10246. }
  10247. /* Move the CQE into the proper xri abort event list */
  10248. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10249. switch (cq->subtype) {
  10250. case LPFC_FCP:
  10251. spin_lock_irqsave(&phba->hbalock, iflags);
  10252. list_add_tail(&cq_event->list,
  10253. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10254. /* Set the fcp xri abort event flag */
  10255. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10256. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10257. workposted = true;
  10258. break;
  10259. case LPFC_ELS:
  10260. spin_lock_irqsave(&phba->hbalock, iflags);
  10261. list_add_tail(&cq_event->list,
  10262. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10263. /* Set the els xri abort event flag */
  10264. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10265. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10266. workposted = true;
  10267. break;
  10268. default:
  10269. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10270. "0603 Invalid work queue CQE subtype (x%x)\n",
  10271. cq->subtype);
  10272. workposted = false;
  10273. break;
  10274. }
  10275. return workposted;
  10276. }
  10277. /**
  10278. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10279. * @phba: Pointer to HBA context object.
  10280. * @rcqe: Pointer to receive-queue completion queue entry.
  10281. *
  10282. * This routine process a receive-queue completion queue entry.
  10283. *
  10284. * Return: true if work posted to worker thread, otherwise false.
  10285. **/
  10286. static bool
  10287. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10288. {
  10289. bool workposted = false;
  10290. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10291. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10292. struct hbq_dmabuf *dma_buf;
  10293. uint32_t status, rq_id;
  10294. unsigned long iflags;
  10295. /* sanity check on queue memory */
  10296. if (unlikely(!hrq) || unlikely(!drq))
  10297. return workposted;
  10298. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10299. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10300. else
  10301. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10302. if (rq_id != hrq->queue_id)
  10303. goto out;
  10304. status = bf_get(lpfc_rcqe_status, rcqe);
  10305. switch (status) {
  10306. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10307. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10308. "2537 Receive Frame Truncated!!\n");
  10309. case FC_STATUS_RQ_SUCCESS:
  10310. lpfc_sli4_rq_release(hrq, drq);
  10311. spin_lock_irqsave(&phba->hbalock, iflags);
  10312. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10313. if (!dma_buf) {
  10314. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10315. goto out;
  10316. }
  10317. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10318. /* save off the frame for the word thread to process */
  10319. list_add_tail(&dma_buf->cq_event.list,
  10320. &phba->sli4_hba.sp_queue_event);
  10321. /* Frame received */
  10322. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10323. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10324. workposted = true;
  10325. break;
  10326. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10327. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10328. /* Post more buffers if possible */
  10329. spin_lock_irqsave(&phba->hbalock, iflags);
  10330. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10331. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10332. workposted = true;
  10333. break;
  10334. }
  10335. out:
  10336. return workposted;
  10337. }
  10338. /**
  10339. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10340. * @phba: Pointer to HBA context object.
  10341. * @cq: Pointer to the completion queue.
  10342. * @wcqe: Pointer to a completion queue entry.
  10343. *
  10344. * This routine process a slow-path work-queue or receive queue completion queue
  10345. * entry.
  10346. *
  10347. * Return: true if work posted to worker thread, otherwise false.
  10348. **/
  10349. static bool
  10350. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10351. struct lpfc_cqe *cqe)
  10352. {
  10353. struct lpfc_cqe cqevt;
  10354. bool workposted = false;
  10355. /* Copy the work queue CQE and convert endian order if needed */
  10356. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10357. /* Check and process for different type of WCQE and dispatch */
  10358. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10359. case CQE_CODE_COMPL_WQE:
  10360. /* Process the WQ/RQ complete event */
  10361. phba->last_completion_time = jiffies;
  10362. workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
  10363. (struct lpfc_wcqe_complete *)&cqevt);
  10364. break;
  10365. case CQE_CODE_RELEASE_WQE:
  10366. /* Process the WQ release event */
  10367. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10368. (struct lpfc_wcqe_release *)&cqevt);
  10369. break;
  10370. case CQE_CODE_XRI_ABORTED:
  10371. /* Process the WQ XRI abort event */
  10372. phba->last_completion_time = jiffies;
  10373. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10374. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10375. break;
  10376. case CQE_CODE_RECEIVE:
  10377. case CQE_CODE_RECEIVE_V1:
  10378. /* Process the RQ event */
  10379. phba->last_completion_time = jiffies;
  10380. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10381. (struct lpfc_rcqe *)&cqevt);
  10382. break;
  10383. default:
  10384. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10385. "0388 Not a valid WCQE code: x%x\n",
  10386. bf_get(lpfc_cqe_code, &cqevt));
  10387. break;
  10388. }
  10389. return workposted;
  10390. }
  10391. /**
  10392. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10393. * @phba: Pointer to HBA context object.
  10394. * @eqe: Pointer to fast-path event queue entry.
  10395. *
  10396. * This routine process a event queue entry from the slow-path event queue.
  10397. * It will check the MajorCode and MinorCode to determine this is for a
  10398. * completion event on a completion queue, if not, an error shall be logged
  10399. * and just return. Otherwise, it will get to the corresponding completion
  10400. * queue and process all the entries on that completion queue, rearm the
  10401. * completion queue, and then return.
  10402. *
  10403. **/
  10404. static void
  10405. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  10406. {
  10407. struct lpfc_queue *cq = NULL, *childq, *speq;
  10408. struct lpfc_cqe *cqe;
  10409. bool workposted = false;
  10410. int ecount = 0;
  10411. uint16_t cqid;
  10412. if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
  10413. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10414. "0359 Not a valid slow-path completion "
  10415. "event: majorcode=x%x, minorcode=x%x\n",
  10416. bf_get_le32(lpfc_eqe_major_code, eqe),
  10417. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10418. return;
  10419. }
  10420. /* Get the reference to the corresponding CQ */
  10421. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10422. /* Search for completion queue pointer matching this cqid */
  10423. speq = phba->sli4_hba.sp_eq;
  10424. /* sanity check on queue memory */
  10425. if (unlikely(!speq))
  10426. return;
  10427. list_for_each_entry(childq, &speq->child_list, list) {
  10428. if (childq->queue_id == cqid) {
  10429. cq = childq;
  10430. break;
  10431. }
  10432. }
  10433. if (unlikely(!cq)) {
  10434. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10435. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10436. "0365 Slow-path CQ identifier "
  10437. "(%d) does not exist\n", cqid);
  10438. return;
  10439. }
  10440. /* Process all the entries to the CQ */
  10441. switch (cq->type) {
  10442. case LPFC_MCQ:
  10443. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10444. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10445. if (!(++ecount % cq->entry_repost))
  10446. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10447. }
  10448. break;
  10449. case LPFC_WCQ:
  10450. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10451. if (cq->subtype == LPFC_FCP)
  10452. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10453. cqe);
  10454. else
  10455. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10456. cqe);
  10457. if (!(++ecount % cq->entry_repost))
  10458. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10459. }
  10460. break;
  10461. default:
  10462. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10463. "0370 Invalid completion queue type (%d)\n",
  10464. cq->type);
  10465. return;
  10466. }
  10467. /* Catch the no cq entry condition, log an error */
  10468. if (unlikely(ecount == 0))
  10469. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10470. "0371 No entry from the CQ: identifier "
  10471. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10472. /* In any case, flash and re-arm the RCQ */
  10473. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10474. /* wake up worker thread if there are works to be done */
  10475. if (workposted)
  10476. lpfc_worker_wake_up(phba);
  10477. }
  10478. /**
  10479. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10480. * @eqe: Pointer to fast-path completion queue entry.
  10481. *
  10482. * This routine process a fast-path work queue completion entry from fast-path
  10483. * event queue for FCP command response completion.
  10484. **/
  10485. static void
  10486. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
  10487. struct lpfc_wcqe_complete *wcqe)
  10488. {
  10489. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10490. struct lpfc_iocbq *cmdiocbq;
  10491. struct lpfc_iocbq irspiocbq;
  10492. unsigned long iflags;
  10493. spin_lock_irqsave(&phba->hbalock, iflags);
  10494. pring->stats.iocb_event++;
  10495. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10496. /* Check for response status */
  10497. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10498. /* If resource errors reported from HBA, reduce queue
  10499. * depth of the SCSI device.
  10500. */
  10501. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10502. IOSTAT_LOCAL_REJECT) &&
  10503. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  10504. phba->lpfc_rampdown_queue_depth(phba);
  10505. }
  10506. /* Log the error status */
  10507. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10508. "0373 FCP complete error: status=x%x, "
  10509. "hw_status=x%x, total_data_specified=%d, "
  10510. "parameter=x%x, word3=x%x\n",
  10511. bf_get(lpfc_wcqe_c_status, wcqe),
  10512. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10513. wcqe->total_data_placed, wcqe->parameter,
  10514. wcqe->word3);
  10515. }
  10516. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10517. spin_lock_irqsave(&phba->hbalock, iflags);
  10518. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10519. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10520. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10521. if (unlikely(!cmdiocbq)) {
  10522. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10523. "0374 FCP complete with no corresponding "
  10524. "cmdiocb: iotag (%d)\n",
  10525. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10526. return;
  10527. }
  10528. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10529. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10530. "0375 FCP cmdiocb not callback function "
  10531. "iotag: (%d)\n",
  10532. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10533. return;
  10534. }
  10535. /* Fake the irspiocb and copy necessary response information */
  10536. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10537. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10538. spin_lock_irqsave(&phba->hbalock, iflags);
  10539. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10540. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10541. }
  10542. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10543. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10544. }
  10545. /**
  10546. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10547. * @phba: Pointer to HBA context object.
  10548. * @cq: Pointer to completion queue.
  10549. * @wcqe: Pointer to work-queue completion queue entry.
  10550. *
  10551. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10552. * proper WQ release routine to the slow-path WQ.
  10553. **/
  10554. static void
  10555. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10556. struct lpfc_wcqe_release *wcqe)
  10557. {
  10558. struct lpfc_queue *childwq;
  10559. bool wqid_matched = false;
  10560. uint16_t fcp_wqid;
  10561. /* Check for fast-path FCP work queue release */
  10562. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10563. list_for_each_entry(childwq, &cq->child_list, list) {
  10564. if (childwq->queue_id == fcp_wqid) {
  10565. lpfc_sli4_wq_release(childwq,
  10566. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10567. wqid_matched = true;
  10568. break;
  10569. }
  10570. }
  10571. /* Report warning log message if no match found */
  10572. if (wqid_matched != true)
  10573. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10574. "2580 Fast-path wqe consume event carries "
  10575. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10576. }
  10577. /**
  10578. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10579. * @cq: Pointer to the completion queue.
  10580. * @eqe: Pointer to fast-path completion queue entry.
  10581. *
  10582. * This routine process a fast-path work queue completion entry from fast-path
  10583. * event queue for FCP command response completion.
  10584. **/
  10585. static int
  10586. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10587. struct lpfc_cqe *cqe)
  10588. {
  10589. struct lpfc_wcqe_release wcqe;
  10590. bool workposted = false;
  10591. /* Copy the work queue CQE and convert endian order if needed */
  10592. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10593. /* Check and process for different type of WCQE and dispatch */
  10594. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10595. case CQE_CODE_COMPL_WQE:
  10596. /* Process the WQ complete event */
  10597. phba->last_completion_time = jiffies;
  10598. lpfc_sli4_fp_handle_fcp_wcqe(phba,
  10599. (struct lpfc_wcqe_complete *)&wcqe);
  10600. break;
  10601. case CQE_CODE_RELEASE_WQE:
  10602. /* Process the WQ release event */
  10603. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10604. (struct lpfc_wcqe_release *)&wcqe);
  10605. break;
  10606. case CQE_CODE_XRI_ABORTED:
  10607. /* Process the WQ XRI abort event */
  10608. phba->last_completion_time = jiffies;
  10609. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10610. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10611. break;
  10612. default:
  10613. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10614. "0144 Not a valid WCQE code: x%x\n",
  10615. bf_get(lpfc_wcqe_c_code, &wcqe));
  10616. break;
  10617. }
  10618. return workposted;
  10619. }
  10620. /**
  10621. * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
  10622. * @phba: Pointer to HBA context object.
  10623. * @eqe: Pointer to fast-path event queue entry.
  10624. *
  10625. * This routine process a event queue entry from the fast-path event queue.
  10626. * It will check the MajorCode and MinorCode to determine this is for a
  10627. * completion event on a completion queue, if not, an error shall be logged
  10628. * and just return. Otherwise, it will get to the corresponding completion
  10629. * queue and process all the entries on the completion queue, rearm the
  10630. * completion queue, and then return.
  10631. **/
  10632. static void
  10633. lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10634. uint32_t fcp_cqidx)
  10635. {
  10636. struct lpfc_queue *cq;
  10637. struct lpfc_cqe *cqe;
  10638. bool workposted = false;
  10639. uint16_t cqid;
  10640. int ecount = 0;
  10641. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10642. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10643. "0366 Not a valid fast-path completion "
  10644. "event: majorcode=x%x, minorcode=x%x\n",
  10645. bf_get_le32(lpfc_eqe_major_code, eqe),
  10646. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10647. return;
  10648. }
  10649. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10650. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10651. "3146 Fast-path completion queues "
  10652. "does not exist\n");
  10653. return;
  10654. }
  10655. cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
  10656. if (unlikely(!cq)) {
  10657. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10658. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10659. "0367 Fast-path completion queue "
  10660. "(%d) does not exist\n", fcp_cqidx);
  10661. return;
  10662. }
  10663. /* Get the reference to the corresponding CQ */
  10664. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10665. if (unlikely(cqid != cq->queue_id)) {
  10666. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10667. "0368 Miss-matched fast-path completion "
  10668. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10669. cqid, cq->queue_id);
  10670. return;
  10671. }
  10672. /* Process all the entries to the CQ */
  10673. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10674. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10675. if (!(++ecount % cq->entry_repost))
  10676. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10677. }
  10678. /* Catch the no cq entry condition */
  10679. if (unlikely(ecount == 0))
  10680. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10681. "0369 No entry from fast-path completion "
  10682. "queue fcpcqid=%d\n", cq->queue_id);
  10683. /* In any case, flash and re-arm the CQ */
  10684. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10685. /* wake up worker thread if there are works to be done */
  10686. if (workposted)
  10687. lpfc_worker_wake_up(phba);
  10688. }
  10689. static void
  10690. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10691. {
  10692. struct lpfc_eqe *eqe;
  10693. /* walk all the EQ entries and drop on the floor */
  10694. while ((eqe = lpfc_sli4_eq_get(eq)))
  10695. ;
  10696. /* Clear and re-arm the EQ */
  10697. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10698. }
  10699. /**
  10700. * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
  10701. * @irq: Interrupt number.
  10702. * @dev_id: The device context pointer.
  10703. *
  10704. * This function is directly called from the PCI layer as an interrupt
  10705. * service routine when device with SLI-4 interface spec is enabled with
  10706. * MSI-X multi-message interrupt mode and there are slow-path events in
  10707. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  10708. * interrupt mode, this function is called as part of the device-level
  10709. * interrupt handler. When the PCI slot is in error recovery or the HBA is
  10710. * undergoing initialization, the interrupt handler will not process the
  10711. * interrupt. The link attention and ELS ring attention events are handled
  10712. * by the worker thread. The interrupt handler signals the worker thread
  10713. * and returns for these events. This function is called without any lock
  10714. * held. It gets the hbalock to access and update SLI data structures.
  10715. *
  10716. * This function returns IRQ_HANDLED when interrupt is handled else it
  10717. * returns IRQ_NONE.
  10718. **/
  10719. irqreturn_t
  10720. lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
  10721. {
  10722. struct lpfc_hba *phba;
  10723. struct lpfc_queue *speq;
  10724. struct lpfc_eqe *eqe;
  10725. unsigned long iflag;
  10726. int ecount = 0;
  10727. /*
  10728. * Get the driver's phba structure from the dev_id
  10729. */
  10730. phba = (struct lpfc_hba *)dev_id;
  10731. if (unlikely(!phba))
  10732. return IRQ_NONE;
  10733. /* Get to the EQ struct associated with this vector */
  10734. speq = phba->sli4_hba.sp_eq;
  10735. if (unlikely(!speq))
  10736. return IRQ_NONE;
  10737. /* Check device state for handling interrupt */
  10738. if (unlikely(lpfc_intr_state_check(phba))) {
  10739. /* Check again for link_state with lock held */
  10740. spin_lock_irqsave(&phba->hbalock, iflag);
  10741. if (phba->link_state < LPFC_LINK_DOWN)
  10742. /* Flush, clear interrupt, and rearm the EQ */
  10743. lpfc_sli4_eq_flush(phba, speq);
  10744. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10745. return IRQ_NONE;
  10746. }
  10747. /*
  10748. * Process all the event on FCP slow-path EQ
  10749. */
  10750. while ((eqe = lpfc_sli4_eq_get(speq))) {
  10751. lpfc_sli4_sp_handle_eqe(phba, eqe);
  10752. if (!(++ecount % speq->entry_repost))
  10753. lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
  10754. }
  10755. /* Always clear and re-arm the slow-path EQ */
  10756. lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
  10757. /* Catch the no cq entry condition */
  10758. if (unlikely(ecount == 0)) {
  10759. if (phba->intr_type == MSIX)
  10760. /* MSI-X treated interrupt served as no EQ share INT */
  10761. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10762. "0357 MSI-X interrupt with no EQE\n");
  10763. else
  10764. /* Non MSI-X treated on interrupt as EQ share INT */
  10765. return IRQ_NONE;
  10766. }
  10767. return IRQ_HANDLED;
  10768. } /* lpfc_sli4_sp_intr_handler */
  10769. /**
  10770. * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
  10771. * @irq: Interrupt number.
  10772. * @dev_id: The device context pointer.
  10773. *
  10774. * This function is directly called from the PCI layer as an interrupt
  10775. * service routine when device with SLI-4 interface spec is enabled with
  10776. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10777. * ring event in the HBA. However, when the device is enabled with either
  10778. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10779. * device-level interrupt handler. When the PCI slot is in error recovery
  10780. * or the HBA is undergoing initialization, the interrupt handler will not
  10781. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10782. * the intrrupt context. This function is called without any lock held.
  10783. * It gets the hbalock to access and update SLI data structures. Note that,
  10784. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10785. * equal to that of FCP CQ index.
  10786. *
  10787. * This function returns IRQ_HANDLED when interrupt is handled else it
  10788. * returns IRQ_NONE.
  10789. **/
  10790. irqreturn_t
  10791. lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
  10792. {
  10793. struct lpfc_hba *phba;
  10794. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10795. struct lpfc_queue *fpeq;
  10796. struct lpfc_eqe *eqe;
  10797. unsigned long iflag;
  10798. int ecount = 0;
  10799. uint32_t fcp_eqidx;
  10800. /* Get the driver's phba structure from the dev_id */
  10801. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10802. phba = fcp_eq_hdl->phba;
  10803. fcp_eqidx = fcp_eq_hdl->idx;
  10804. if (unlikely(!phba))
  10805. return IRQ_NONE;
  10806. if (unlikely(!phba->sli4_hba.fp_eq))
  10807. return IRQ_NONE;
  10808. /* Get to the EQ struct associated with this vector */
  10809. fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
  10810. if (unlikely(!fpeq))
  10811. return IRQ_NONE;
  10812. /* Check device state for handling interrupt */
  10813. if (unlikely(lpfc_intr_state_check(phba))) {
  10814. /* Check again for link_state with lock held */
  10815. spin_lock_irqsave(&phba->hbalock, iflag);
  10816. if (phba->link_state < LPFC_LINK_DOWN)
  10817. /* Flush, clear interrupt, and rearm the EQ */
  10818. lpfc_sli4_eq_flush(phba, fpeq);
  10819. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10820. return IRQ_NONE;
  10821. }
  10822. /*
  10823. * Process all the event on FCP fast-path EQ
  10824. */
  10825. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10826. lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
  10827. if (!(++ecount % fpeq->entry_repost))
  10828. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10829. }
  10830. /* Always clear and re-arm the fast-path EQ */
  10831. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10832. if (unlikely(ecount == 0)) {
  10833. if (phba->intr_type == MSIX)
  10834. /* MSI-X treated interrupt served as no EQ share INT */
  10835. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10836. "0358 MSI-X interrupt with no EQE\n");
  10837. else
  10838. /* Non MSI-X treated on interrupt as EQ share INT */
  10839. return IRQ_NONE;
  10840. }
  10841. return IRQ_HANDLED;
  10842. } /* lpfc_sli4_fp_intr_handler */
  10843. /**
  10844. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10845. * @irq: Interrupt number.
  10846. * @dev_id: The device context pointer.
  10847. *
  10848. * This function is the device-level interrupt handler to device with SLI-4
  10849. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10850. * interrupt mode is enabled and there is an event in the HBA which requires
  10851. * driver attention. This function invokes the slow-path interrupt attention
  10852. * handling function and fast-path interrupt attention handling function in
  10853. * turn to process the relevant HBA attention events. This function is called
  10854. * without any lock held. It gets the hbalock to access and update SLI data
  10855. * structures.
  10856. *
  10857. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10858. * returns IRQ_NONE.
  10859. **/
  10860. irqreturn_t
  10861. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10862. {
  10863. struct lpfc_hba *phba;
  10864. irqreturn_t sp_irq_rc, fp_irq_rc;
  10865. bool fp_handled = false;
  10866. uint32_t fcp_eqidx;
  10867. /* Get the driver's phba structure from the dev_id */
  10868. phba = (struct lpfc_hba *)dev_id;
  10869. if (unlikely(!phba))
  10870. return IRQ_NONE;
  10871. /*
  10872. * Invokes slow-path host attention interrupt handling as appropriate.
  10873. */
  10874. sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
  10875. /*
  10876. * Invoke fast-path host attention interrupt handling as appropriate.
  10877. */
  10878. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
  10879. fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
  10880. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10881. if (fp_irq_rc == IRQ_HANDLED)
  10882. fp_handled |= true;
  10883. }
  10884. return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
  10885. } /* lpfc_sli4_intr_handler */
  10886. /**
  10887. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10888. * @queue: The queue structure to free.
  10889. *
  10890. * This function frees a queue structure and the DMAable memory used for
  10891. * the host resident queue. This function must be called after destroying the
  10892. * queue on the HBA.
  10893. **/
  10894. void
  10895. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10896. {
  10897. struct lpfc_dmabuf *dmabuf;
  10898. if (!queue)
  10899. return;
  10900. while (!list_empty(&queue->page_list)) {
  10901. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10902. list);
  10903. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10904. dmabuf->virt, dmabuf->phys);
  10905. kfree(dmabuf);
  10906. }
  10907. kfree(queue);
  10908. return;
  10909. }
  10910. /**
  10911. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10912. * @phba: The HBA that this queue is being created on.
  10913. * @entry_size: The size of each queue entry for this queue.
  10914. * @entry count: The number of entries that this queue will handle.
  10915. *
  10916. * This function allocates a queue structure and the DMAable memory used for
  10917. * the host resident queue. This function must be called before creating the
  10918. * queue on the HBA.
  10919. **/
  10920. struct lpfc_queue *
  10921. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10922. uint32_t entry_count)
  10923. {
  10924. struct lpfc_queue *queue;
  10925. struct lpfc_dmabuf *dmabuf;
  10926. int x, total_qe_count;
  10927. void *dma_pointer;
  10928. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10929. if (!phba->sli4_hba.pc_sli4_params.supported)
  10930. hw_page_size = SLI4_PAGE_SIZE;
  10931. queue = kzalloc(sizeof(struct lpfc_queue) +
  10932. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  10933. if (!queue)
  10934. return NULL;
  10935. queue->page_count = (ALIGN(entry_size * entry_count,
  10936. hw_page_size))/hw_page_size;
  10937. INIT_LIST_HEAD(&queue->list);
  10938. INIT_LIST_HEAD(&queue->page_list);
  10939. INIT_LIST_HEAD(&queue->child_list);
  10940. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  10941. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  10942. if (!dmabuf)
  10943. goto out_fail;
  10944. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  10945. hw_page_size, &dmabuf->phys,
  10946. GFP_KERNEL);
  10947. if (!dmabuf->virt) {
  10948. kfree(dmabuf);
  10949. goto out_fail;
  10950. }
  10951. memset(dmabuf->virt, 0, hw_page_size);
  10952. dmabuf->buffer_tag = x;
  10953. list_add_tail(&dmabuf->list, &queue->page_list);
  10954. /* initialize queue's entry array */
  10955. dma_pointer = dmabuf->virt;
  10956. for (; total_qe_count < entry_count &&
  10957. dma_pointer < (hw_page_size + dmabuf->virt);
  10958. total_qe_count++, dma_pointer += entry_size) {
  10959. queue->qe[total_qe_count].address = dma_pointer;
  10960. }
  10961. }
  10962. queue->entry_size = entry_size;
  10963. queue->entry_count = entry_count;
  10964. /*
  10965. * entry_repost is calculated based on the number of entries in the
  10966. * queue. This works out except for RQs. If buffers are NOT initially
  10967. * posted for every RQE, entry_repost should be adjusted accordingly.
  10968. */
  10969. queue->entry_repost = (entry_count >> 3);
  10970. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  10971. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  10972. queue->phba = phba;
  10973. return queue;
  10974. out_fail:
  10975. lpfc_sli4_queue_free(queue);
  10976. return NULL;
  10977. }
  10978. /**
  10979. * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
  10980. * @phba: HBA structure that indicates port to create a queue on.
  10981. * @startq: The starting FCP EQ to modify
  10982. *
  10983. * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
  10984. *
  10985. * The @phba struct is used to send mailbox command to HBA. The @startq
  10986. * is used to get the starting FCP EQ to change.
  10987. * This function is asynchronous and will wait for the mailbox
  10988. * command to finish before continuing.
  10989. *
  10990. * On success this function will return a zero. If unable to allocate enough
  10991. * memory this function will return -ENOMEM. If the queue create mailbox command
  10992. * fails this function will return -ENXIO.
  10993. **/
  10994. uint32_t
  10995. lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
  10996. {
  10997. struct lpfc_mbx_modify_eq_delay *eq_delay;
  10998. LPFC_MBOXQ_t *mbox;
  10999. struct lpfc_queue *eq;
  11000. int cnt, rc, length, status = 0;
  11001. uint32_t shdr_status, shdr_add_status;
  11002. int fcp_eqidx;
  11003. union lpfc_sli4_cfg_shdr *shdr;
  11004. uint16_t dmult;
  11005. if (startq >= phba->cfg_fcp_eq_count)
  11006. return 0;
  11007. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11008. if (!mbox)
  11009. return -ENOMEM;
  11010. length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
  11011. sizeof(struct lpfc_sli4_cfg_mhdr));
  11012. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11013. LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
  11014. length, LPFC_SLI4_MBX_EMBED);
  11015. eq_delay = &mbox->u.mqe.un.eq_delay;
  11016. /* Calculate delay multiper from maximum interrupt per second */
  11017. dmult = LPFC_DMULT_CONST/phba->cfg_fcp_imax - 1;
  11018. cnt = 0;
  11019. for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_eq_count;
  11020. fcp_eqidx++) {
  11021. eq = phba->sli4_hba.fp_eq[fcp_eqidx];
  11022. if (!eq)
  11023. continue;
  11024. eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
  11025. eq_delay->u.request.eq[cnt].phase = 0;
  11026. eq_delay->u.request.eq[cnt].delay_multi = dmult;
  11027. cnt++;
  11028. if (cnt >= LPFC_MAX_EQ_DELAY)
  11029. break;
  11030. }
  11031. eq_delay->u.request.num_eq = cnt;
  11032. mbox->vport = phba->pport;
  11033. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11034. mbox->context1 = NULL;
  11035. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11036. shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
  11037. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11038. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11039. if (shdr_status || shdr_add_status || rc) {
  11040. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11041. "2512 MODIFY_EQ_DELAY mailbox failed with "
  11042. "status x%x add_status x%x, mbx status x%x\n",
  11043. shdr_status, shdr_add_status, rc);
  11044. status = -ENXIO;
  11045. }
  11046. mempool_free(mbox, phba->mbox_mem_pool);
  11047. return status;
  11048. }
  11049. /**
  11050. * lpfc_eq_create - Create an Event Queue on the HBA
  11051. * @phba: HBA structure that indicates port to create a queue on.
  11052. * @eq: The queue structure to use to create the event queue.
  11053. * @imax: The maximum interrupt per second limit.
  11054. *
  11055. * This function creates an event queue, as detailed in @eq, on a port,
  11056. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  11057. *
  11058. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  11059. * is used to get the entry count and entry size that are necessary to
  11060. * determine the number of pages to allocate and use for this queue. This
  11061. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  11062. * event queue. This function is asynchronous and will wait for the mailbox
  11063. * command to finish before continuing.
  11064. *
  11065. * On success this function will return a zero. If unable to allocate enough
  11066. * memory this function will return -ENOMEM. If the queue create mailbox command
  11067. * fails this function will return -ENXIO.
  11068. **/
  11069. uint32_t
  11070. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  11071. {
  11072. struct lpfc_mbx_eq_create *eq_create;
  11073. LPFC_MBOXQ_t *mbox;
  11074. int rc, length, status = 0;
  11075. struct lpfc_dmabuf *dmabuf;
  11076. uint32_t shdr_status, shdr_add_status;
  11077. union lpfc_sli4_cfg_shdr *shdr;
  11078. uint16_t dmult;
  11079. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11080. /* sanity check on queue memory */
  11081. if (!eq)
  11082. return -ENODEV;
  11083. if (!phba->sli4_hba.pc_sli4_params.supported)
  11084. hw_page_size = SLI4_PAGE_SIZE;
  11085. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11086. if (!mbox)
  11087. return -ENOMEM;
  11088. length = (sizeof(struct lpfc_mbx_eq_create) -
  11089. sizeof(struct lpfc_sli4_cfg_mhdr));
  11090. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11091. LPFC_MBOX_OPCODE_EQ_CREATE,
  11092. length, LPFC_SLI4_MBX_EMBED);
  11093. eq_create = &mbox->u.mqe.un.eq_create;
  11094. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  11095. eq->page_count);
  11096. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  11097. LPFC_EQE_SIZE);
  11098. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  11099. /* Calculate delay multiper from maximum interrupt per second */
  11100. dmult = LPFC_DMULT_CONST/imax - 1;
  11101. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  11102. dmult);
  11103. switch (eq->entry_count) {
  11104. default:
  11105. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11106. "0360 Unsupported EQ count. (%d)\n",
  11107. eq->entry_count);
  11108. if (eq->entry_count < 256)
  11109. return -EINVAL;
  11110. /* otherwise default to smallest count (drop through) */
  11111. case 256:
  11112. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11113. LPFC_EQ_CNT_256);
  11114. break;
  11115. case 512:
  11116. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11117. LPFC_EQ_CNT_512);
  11118. break;
  11119. case 1024:
  11120. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11121. LPFC_EQ_CNT_1024);
  11122. break;
  11123. case 2048:
  11124. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11125. LPFC_EQ_CNT_2048);
  11126. break;
  11127. case 4096:
  11128. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11129. LPFC_EQ_CNT_4096);
  11130. break;
  11131. }
  11132. list_for_each_entry(dmabuf, &eq->page_list, list) {
  11133. memset(dmabuf->virt, 0, hw_page_size);
  11134. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11135. putPaddrLow(dmabuf->phys);
  11136. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11137. putPaddrHigh(dmabuf->phys);
  11138. }
  11139. mbox->vport = phba->pport;
  11140. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11141. mbox->context1 = NULL;
  11142. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11143. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  11144. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11145. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11146. if (shdr_status || shdr_add_status || rc) {
  11147. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11148. "2500 EQ_CREATE mailbox failed with "
  11149. "status x%x add_status x%x, mbx status x%x\n",
  11150. shdr_status, shdr_add_status, rc);
  11151. status = -ENXIO;
  11152. }
  11153. eq->type = LPFC_EQ;
  11154. eq->subtype = LPFC_NONE;
  11155. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  11156. if (eq->queue_id == 0xFFFF)
  11157. status = -ENXIO;
  11158. eq->host_index = 0;
  11159. eq->hba_index = 0;
  11160. mempool_free(mbox, phba->mbox_mem_pool);
  11161. return status;
  11162. }
  11163. /**
  11164. * lpfc_cq_create - Create a Completion Queue on the HBA
  11165. * @phba: HBA structure that indicates port to create a queue on.
  11166. * @cq: The queue structure to use to create the completion queue.
  11167. * @eq: The event queue to bind this completion queue to.
  11168. *
  11169. * This function creates a completion queue, as detailed in @wq, on a port,
  11170. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  11171. *
  11172. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11173. * is used to get the entry count and entry size that are necessary to
  11174. * determine the number of pages to allocate and use for this queue. The @eq
  11175. * is used to indicate which event queue to bind this completion queue to. This
  11176. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  11177. * completion queue. This function is asynchronous and will wait for the mailbox
  11178. * command to finish before continuing.
  11179. *
  11180. * On success this function will return a zero. If unable to allocate enough
  11181. * memory this function will return -ENOMEM. If the queue create mailbox command
  11182. * fails this function will return -ENXIO.
  11183. **/
  11184. uint32_t
  11185. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11186. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  11187. {
  11188. struct lpfc_mbx_cq_create *cq_create;
  11189. struct lpfc_dmabuf *dmabuf;
  11190. LPFC_MBOXQ_t *mbox;
  11191. int rc, length, status = 0;
  11192. uint32_t shdr_status, shdr_add_status;
  11193. union lpfc_sli4_cfg_shdr *shdr;
  11194. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11195. /* sanity check on queue memory */
  11196. if (!cq || !eq)
  11197. return -ENODEV;
  11198. if (!phba->sli4_hba.pc_sli4_params.supported)
  11199. hw_page_size = SLI4_PAGE_SIZE;
  11200. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11201. if (!mbox)
  11202. return -ENOMEM;
  11203. length = (sizeof(struct lpfc_mbx_cq_create) -
  11204. sizeof(struct lpfc_sli4_cfg_mhdr));
  11205. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11206. LPFC_MBOX_OPCODE_CQ_CREATE,
  11207. length, LPFC_SLI4_MBX_EMBED);
  11208. cq_create = &mbox->u.mqe.un.cq_create;
  11209. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  11210. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  11211. cq->page_count);
  11212. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  11213. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  11214. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11215. phba->sli4_hba.pc_sli4_params.cqv);
  11216. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  11217. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  11218. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  11219. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  11220. eq->queue_id);
  11221. } else {
  11222. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  11223. eq->queue_id);
  11224. }
  11225. switch (cq->entry_count) {
  11226. default:
  11227. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11228. "0361 Unsupported CQ count. (%d)\n",
  11229. cq->entry_count);
  11230. if (cq->entry_count < 256) {
  11231. status = -EINVAL;
  11232. goto out;
  11233. }
  11234. /* otherwise default to smallest count (drop through) */
  11235. case 256:
  11236. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11237. LPFC_CQ_CNT_256);
  11238. break;
  11239. case 512:
  11240. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11241. LPFC_CQ_CNT_512);
  11242. break;
  11243. case 1024:
  11244. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11245. LPFC_CQ_CNT_1024);
  11246. break;
  11247. }
  11248. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11249. memset(dmabuf->virt, 0, hw_page_size);
  11250. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11251. putPaddrLow(dmabuf->phys);
  11252. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11253. putPaddrHigh(dmabuf->phys);
  11254. }
  11255. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11256. /* The IOCTL status is embedded in the mailbox subheader. */
  11257. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11258. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11259. if (shdr_status || shdr_add_status || rc) {
  11260. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11261. "2501 CQ_CREATE mailbox failed with "
  11262. "status x%x add_status x%x, mbx status x%x\n",
  11263. shdr_status, shdr_add_status, rc);
  11264. status = -ENXIO;
  11265. goto out;
  11266. }
  11267. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11268. if (cq->queue_id == 0xFFFF) {
  11269. status = -ENXIO;
  11270. goto out;
  11271. }
  11272. /* link the cq onto the parent eq child list */
  11273. list_add_tail(&cq->list, &eq->child_list);
  11274. /* Set up completion queue's type and subtype */
  11275. cq->type = type;
  11276. cq->subtype = subtype;
  11277. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11278. cq->assoc_qid = eq->queue_id;
  11279. cq->host_index = 0;
  11280. cq->hba_index = 0;
  11281. out:
  11282. mempool_free(mbox, phba->mbox_mem_pool);
  11283. return status;
  11284. }
  11285. /**
  11286. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11287. * @phba: HBA structure that indicates port to create a queue on.
  11288. * @mq: The queue structure to use to create the mailbox queue.
  11289. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11290. * @cq: The completion queue to associate with this cq.
  11291. *
  11292. * This function provides failback (fb) functionality when the
  11293. * mq_create_ext fails on older FW generations. It's purpose is identical
  11294. * to mq_create_ext otherwise.
  11295. *
  11296. * This routine cannot fail as all attributes were previously accessed and
  11297. * initialized in mq_create_ext.
  11298. **/
  11299. static void
  11300. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11301. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11302. {
  11303. struct lpfc_mbx_mq_create *mq_create;
  11304. struct lpfc_dmabuf *dmabuf;
  11305. int length;
  11306. length = (sizeof(struct lpfc_mbx_mq_create) -
  11307. sizeof(struct lpfc_sli4_cfg_mhdr));
  11308. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11309. LPFC_MBOX_OPCODE_MQ_CREATE,
  11310. length, LPFC_SLI4_MBX_EMBED);
  11311. mq_create = &mbox->u.mqe.un.mq_create;
  11312. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11313. mq->page_count);
  11314. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11315. cq->queue_id);
  11316. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11317. switch (mq->entry_count) {
  11318. case 16:
  11319. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11320. LPFC_MQ_RING_SIZE_16);
  11321. break;
  11322. case 32:
  11323. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11324. LPFC_MQ_RING_SIZE_32);
  11325. break;
  11326. case 64:
  11327. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11328. LPFC_MQ_RING_SIZE_64);
  11329. break;
  11330. case 128:
  11331. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11332. LPFC_MQ_RING_SIZE_128);
  11333. break;
  11334. }
  11335. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11336. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11337. putPaddrLow(dmabuf->phys);
  11338. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11339. putPaddrHigh(dmabuf->phys);
  11340. }
  11341. }
  11342. /**
  11343. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11344. * @phba: HBA structure that indicates port to create a queue on.
  11345. * @mq: The queue structure to use to create the mailbox queue.
  11346. * @cq: The completion queue to associate with this cq.
  11347. * @subtype: The queue's subtype.
  11348. *
  11349. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11350. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11351. *
  11352. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11353. * is used to get the entry count and entry size that are necessary to
  11354. * determine the number of pages to allocate and use for this queue. This
  11355. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  11356. * mailbox queue. This function is asynchronous and will wait for the mailbox
  11357. * command to finish before continuing.
  11358. *
  11359. * On success this function will return a zero. If unable to allocate enough
  11360. * memory this function will return -ENOMEM. If the queue create mailbox command
  11361. * fails this function will return -ENXIO.
  11362. **/
  11363. int32_t
  11364. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11365. struct lpfc_queue *cq, uint32_t subtype)
  11366. {
  11367. struct lpfc_mbx_mq_create *mq_create;
  11368. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11369. struct lpfc_dmabuf *dmabuf;
  11370. LPFC_MBOXQ_t *mbox;
  11371. int rc, length, status = 0;
  11372. uint32_t shdr_status, shdr_add_status;
  11373. union lpfc_sli4_cfg_shdr *shdr;
  11374. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11375. /* sanity check on queue memory */
  11376. if (!mq || !cq)
  11377. return -ENODEV;
  11378. if (!phba->sli4_hba.pc_sli4_params.supported)
  11379. hw_page_size = SLI4_PAGE_SIZE;
  11380. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11381. if (!mbox)
  11382. return -ENOMEM;
  11383. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11384. sizeof(struct lpfc_sli4_cfg_mhdr));
  11385. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11386. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11387. length, LPFC_SLI4_MBX_EMBED);
  11388. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11389. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11390. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11391. &mq_create_ext->u.request, mq->page_count);
  11392. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11393. &mq_create_ext->u.request, 1);
  11394. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11395. &mq_create_ext->u.request, 1);
  11396. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11397. &mq_create_ext->u.request, 1);
  11398. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11399. &mq_create_ext->u.request, 1);
  11400. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11401. &mq_create_ext->u.request, 1);
  11402. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11403. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11404. phba->sli4_hba.pc_sli4_params.mqv);
  11405. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11406. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11407. cq->queue_id);
  11408. else
  11409. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11410. cq->queue_id);
  11411. switch (mq->entry_count) {
  11412. default:
  11413. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11414. "0362 Unsupported MQ count. (%d)\n",
  11415. mq->entry_count);
  11416. if (mq->entry_count < 16) {
  11417. status = -EINVAL;
  11418. goto out;
  11419. }
  11420. /* otherwise default to smallest count (drop through) */
  11421. case 16:
  11422. bf_set(lpfc_mq_context_ring_size,
  11423. &mq_create_ext->u.request.context,
  11424. LPFC_MQ_RING_SIZE_16);
  11425. break;
  11426. case 32:
  11427. bf_set(lpfc_mq_context_ring_size,
  11428. &mq_create_ext->u.request.context,
  11429. LPFC_MQ_RING_SIZE_32);
  11430. break;
  11431. case 64:
  11432. bf_set(lpfc_mq_context_ring_size,
  11433. &mq_create_ext->u.request.context,
  11434. LPFC_MQ_RING_SIZE_64);
  11435. break;
  11436. case 128:
  11437. bf_set(lpfc_mq_context_ring_size,
  11438. &mq_create_ext->u.request.context,
  11439. LPFC_MQ_RING_SIZE_128);
  11440. break;
  11441. }
  11442. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11443. memset(dmabuf->virt, 0, hw_page_size);
  11444. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11445. putPaddrLow(dmabuf->phys);
  11446. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11447. putPaddrHigh(dmabuf->phys);
  11448. }
  11449. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11450. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11451. &mq_create_ext->u.response);
  11452. if (rc != MBX_SUCCESS) {
  11453. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11454. "2795 MQ_CREATE_EXT failed with "
  11455. "status x%x. Failback to MQ_CREATE.\n",
  11456. rc);
  11457. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11458. mq_create = &mbox->u.mqe.un.mq_create;
  11459. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11460. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11461. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11462. &mq_create->u.response);
  11463. }
  11464. /* The IOCTL status is embedded in the mailbox subheader. */
  11465. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11466. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11467. if (shdr_status || shdr_add_status || rc) {
  11468. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11469. "2502 MQ_CREATE mailbox failed with "
  11470. "status x%x add_status x%x, mbx status x%x\n",
  11471. shdr_status, shdr_add_status, rc);
  11472. status = -ENXIO;
  11473. goto out;
  11474. }
  11475. if (mq->queue_id == 0xFFFF) {
  11476. status = -ENXIO;
  11477. goto out;
  11478. }
  11479. mq->type = LPFC_MQ;
  11480. mq->assoc_qid = cq->queue_id;
  11481. mq->subtype = subtype;
  11482. mq->host_index = 0;
  11483. mq->hba_index = 0;
  11484. /* link the mq onto the parent cq child list */
  11485. list_add_tail(&mq->list, &cq->child_list);
  11486. out:
  11487. mempool_free(mbox, phba->mbox_mem_pool);
  11488. return status;
  11489. }
  11490. /**
  11491. * lpfc_wq_create - Create a Work Queue on the HBA
  11492. * @phba: HBA structure that indicates port to create a queue on.
  11493. * @wq: The queue structure to use to create the work queue.
  11494. * @cq: The completion queue to bind this work queue to.
  11495. * @subtype: The subtype of the work queue indicating its functionality.
  11496. *
  11497. * This function creates a work queue, as detailed in @wq, on a port, described
  11498. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11499. *
  11500. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11501. * is used to get the entry count and entry size that are necessary to
  11502. * determine the number of pages to allocate and use for this queue. The @cq
  11503. * is used to indicate which completion queue to bind this work queue to. This
  11504. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11505. * work queue. This function is asynchronous and will wait for the mailbox
  11506. * command to finish before continuing.
  11507. *
  11508. * On success this function will return a zero. If unable to allocate enough
  11509. * memory this function will return -ENOMEM. If the queue create mailbox command
  11510. * fails this function will return -ENXIO.
  11511. **/
  11512. uint32_t
  11513. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11514. struct lpfc_queue *cq, uint32_t subtype)
  11515. {
  11516. struct lpfc_mbx_wq_create *wq_create;
  11517. struct lpfc_dmabuf *dmabuf;
  11518. LPFC_MBOXQ_t *mbox;
  11519. int rc, length, status = 0;
  11520. uint32_t shdr_status, shdr_add_status;
  11521. union lpfc_sli4_cfg_shdr *shdr;
  11522. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11523. struct dma_address *page;
  11524. /* sanity check on queue memory */
  11525. if (!wq || !cq)
  11526. return -ENODEV;
  11527. if (!phba->sli4_hba.pc_sli4_params.supported)
  11528. hw_page_size = SLI4_PAGE_SIZE;
  11529. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11530. if (!mbox)
  11531. return -ENOMEM;
  11532. length = (sizeof(struct lpfc_mbx_wq_create) -
  11533. sizeof(struct lpfc_sli4_cfg_mhdr));
  11534. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11535. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11536. length, LPFC_SLI4_MBX_EMBED);
  11537. wq_create = &mbox->u.mqe.un.wq_create;
  11538. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11539. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11540. wq->page_count);
  11541. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11542. cq->queue_id);
  11543. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11544. phba->sli4_hba.pc_sli4_params.wqv);
  11545. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11546. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11547. wq->entry_count);
  11548. switch (wq->entry_size) {
  11549. default:
  11550. case 64:
  11551. bf_set(lpfc_mbx_wq_create_wqe_size,
  11552. &wq_create->u.request_1,
  11553. LPFC_WQ_WQE_SIZE_64);
  11554. break;
  11555. case 128:
  11556. bf_set(lpfc_mbx_wq_create_wqe_size,
  11557. &wq_create->u.request_1,
  11558. LPFC_WQ_WQE_SIZE_128);
  11559. break;
  11560. }
  11561. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11562. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11563. page = wq_create->u.request_1.page;
  11564. } else {
  11565. page = wq_create->u.request.page;
  11566. }
  11567. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11568. memset(dmabuf->virt, 0, hw_page_size);
  11569. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11570. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11571. }
  11572. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11573. /* The IOCTL status is embedded in the mailbox subheader. */
  11574. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11575. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11576. if (shdr_status || shdr_add_status || rc) {
  11577. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11578. "2503 WQ_CREATE mailbox failed with "
  11579. "status x%x add_status x%x, mbx status x%x\n",
  11580. shdr_status, shdr_add_status, rc);
  11581. status = -ENXIO;
  11582. goto out;
  11583. }
  11584. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11585. if (wq->queue_id == 0xFFFF) {
  11586. status = -ENXIO;
  11587. goto out;
  11588. }
  11589. wq->type = LPFC_WQ;
  11590. wq->assoc_qid = cq->queue_id;
  11591. wq->subtype = subtype;
  11592. wq->host_index = 0;
  11593. wq->hba_index = 0;
  11594. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  11595. /* link the wq onto the parent cq child list */
  11596. list_add_tail(&wq->list, &cq->child_list);
  11597. out:
  11598. mempool_free(mbox, phba->mbox_mem_pool);
  11599. return status;
  11600. }
  11601. /**
  11602. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11603. * @phba: HBA structure that indicates port to create a queue on.
  11604. * @rq: The queue structure to use for the receive queue.
  11605. * @qno: The associated HBQ number
  11606. *
  11607. *
  11608. * For SLI4 we need to adjust the RQ repost value based on
  11609. * the number of buffers that are initially posted to the RQ.
  11610. */
  11611. void
  11612. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11613. {
  11614. uint32_t cnt;
  11615. /* sanity check on queue memory */
  11616. if (!rq)
  11617. return;
  11618. cnt = lpfc_hbq_defs[qno]->entry_count;
  11619. /* Recalc repost for RQs based on buffers initially posted */
  11620. cnt = (cnt >> 3);
  11621. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11622. cnt = LPFC_QUEUE_MIN_REPOST;
  11623. rq->entry_repost = cnt;
  11624. }
  11625. /**
  11626. * lpfc_rq_create - Create a Receive Queue on the HBA
  11627. * @phba: HBA structure that indicates port to create a queue on.
  11628. * @hrq: The queue structure to use to create the header receive queue.
  11629. * @drq: The queue structure to use to create the data receive queue.
  11630. * @cq: The completion queue to bind this work queue to.
  11631. *
  11632. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11633. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11634. * to the HBA.
  11635. *
  11636. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11637. * struct is used to get the entry count that is necessary to determine the
  11638. * number of pages to use for this queue. The @cq is used to indicate which
  11639. * completion queue to bind received buffers that are posted to these queues to.
  11640. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11641. * receive queue pair. This function is asynchronous and will wait for the
  11642. * mailbox command to finish before continuing.
  11643. *
  11644. * On success this function will return a zero. If unable to allocate enough
  11645. * memory this function will return -ENOMEM. If the queue create mailbox command
  11646. * fails this function will return -ENXIO.
  11647. **/
  11648. uint32_t
  11649. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11650. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11651. {
  11652. struct lpfc_mbx_rq_create *rq_create;
  11653. struct lpfc_dmabuf *dmabuf;
  11654. LPFC_MBOXQ_t *mbox;
  11655. int rc, length, status = 0;
  11656. uint32_t shdr_status, shdr_add_status;
  11657. union lpfc_sli4_cfg_shdr *shdr;
  11658. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11659. /* sanity check on queue memory */
  11660. if (!hrq || !drq || !cq)
  11661. return -ENODEV;
  11662. if (!phba->sli4_hba.pc_sli4_params.supported)
  11663. hw_page_size = SLI4_PAGE_SIZE;
  11664. if (hrq->entry_count != drq->entry_count)
  11665. return -EINVAL;
  11666. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11667. if (!mbox)
  11668. return -ENOMEM;
  11669. length = (sizeof(struct lpfc_mbx_rq_create) -
  11670. sizeof(struct lpfc_sli4_cfg_mhdr));
  11671. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11672. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11673. length, LPFC_SLI4_MBX_EMBED);
  11674. rq_create = &mbox->u.mqe.un.rq_create;
  11675. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11676. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11677. phba->sli4_hba.pc_sli4_params.rqv);
  11678. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11679. bf_set(lpfc_rq_context_rqe_count_1,
  11680. &rq_create->u.request.context,
  11681. hrq->entry_count);
  11682. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11683. bf_set(lpfc_rq_context_rqe_size,
  11684. &rq_create->u.request.context,
  11685. LPFC_RQE_SIZE_8);
  11686. bf_set(lpfc_rq_context_page_size,
  11687. &rq_create->u.request.context,
  11688. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11689. } else {
  11690. switch (hrq->entry_count) {
  11691. default:
  11692. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11693. "2535 Unsupported RQ count. (%d)\n",
  11694. hrq->entry_count);
  11695. if (hrq->entry_count < 512) {
  11696. status = -EINVAL;
  11697. goto out;
  11698. }
  11699. /* otherwise default to smallest count (drop through) */
  11700. case 512:
  11701. bf_set(lpfc_rq_context_rqe_count,
  11702. &rq_create->u.request.context,
  11703. LPFC_RQ_RING_SIZE_512);
  11704. break;
  11705. case 1024:
  11706. bf_set(lpfc_rq_context_rqe_count,
  11707. &rq_create->u.request.context,
  11708. LPFC_RQ_RING_SIZE_1024);
  11709. break;
  11710. case 2048:
  11711. bf_set(lpfc_rq_context_rqe_count,
  11712. &rq_create->u.request.context,
  11713. LPFC_RQ_RING_SIZE_2048);
  11714. break;
  11715. case 4096:
  11716. bf_set(lpfc_rq_context_rqe_count,
  11717. &rq_create->u.request.context,
  11718. LPFC_RQ_RING_SIZE_4096);
  11719. break;
  11720. }
  11721. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11722. LPFC_HDR_BUF_SIZE);
  11723. }
  11724. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11725. cq->queue_id);
  11726. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11727. hrq->page_count);
  11728. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11729. memset(dmabuf->virt, 0, hw_page_size);
  11730. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11731. putPaddrLow(dmabuf->phys);
  11732. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11733. putPaddrHigh(dmabuf->phys);
  11734. }
  11735. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11736. /* The IOCTL status is embedded in the mailbox subheader. */
  11737. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11738. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11739. if (shdr_status || shdr_add_status || rc) {
  11740. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11741. "2504 RQ_CREATE mailbox failed with "
  11742. "status x%x add_status x%x, mbx status x%x\n",
  11743. shdr_status, shdr_add_status, rc);
  11744. status = -ENXIO;
  11745. goto out;
  11746. }
  11747. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11748. if (hrq->queue_id == 0xFFFF) {
  11749. status = -ENXIO;
  11750. goto out;
  11751. }
  11752. hrq->type = LPFC_HRQ;
  11753. hrq->assoc_qid = cq->queue_id;
  11754. hrq->subtype = subtype;
  11755. hrq->host_index = 0;
  11756. hrq->hba_index = 0;
  11757. /* now create the data queue */
  11758. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11759. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11760. length, LPFC_SLI4_MBX_EMBED);
  11761. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11762. phba->sli4_hba.pc_sli4_params.rqv);
  11763. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11764. bf_set(lpfc_rq_context_rqe_count_1,
  11765. &rq_create->u.request.context, hrq->entry_count);
  11766. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11767. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11768. LPFC_RQE_SIZE_8);
  11769. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11770. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11771. } else {
  11772. switch (drq->entry_count) {
  11773. default:
  11774. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11775. "2536 Unsupported RQ count. (%d)\n",
  11776. drq->entry_count);
  11777. if (drq->entry_count < 512) {
  11778. status = -EINVAL;
  11779. goto out;
  11780. }
  11781. /* otherwise default to smallest count (drop through) */
  11782. case 512:
  11783. bf_set(lpfc_rq_context_rqe_count,
  11784. &rq_create->u.request.context,
  11785. LPFC_RQ_RING_SIZE_512);
  11786. break;
  11787. case 1024:
  11788. bf_set(lpfc_rq_context_rqe_count,
  11789. &rq_create->u.request.context,
  11790. LPFC_RQ_RING_SIZE_1024);
  11791. break;
  11792. case 2048:
  11793. bf_set(lpfc_rq_context_rqe_count,
  11794. &rq_create->u.request.context,
  11795. LPFC_RQ_RING_SIZE_2048);
  11796. break;
  11797. case 4096:
  11798. bf_set(lpfc_rq_context_rqe_count,
  11799. &rq_create->u.request.context,
  11800. LPFC_RQ_RING_SIZE_4096);
  11801. break;
  11802. }
  11803. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11804. LPFC_DATA_BUF_SIZE);
  11805. }
  11806. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11807. cq->queue_id);
  11808. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11809. drq->page_count);
  11810. list_for_each_entry(dmabuf, &drq->page_list, list) {
  11811. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11812. putPaddrLow(dmabuf->phys);
  11813. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11814. putPaddrHigh(dmabuf->phys);
  11815. }
  11816. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11817. /* The IOCTL status is embedded in the mailbox subheader. */
  11818. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11819. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11820. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11821. if (shdr_status || shdr_add_status || rc) {
  11822. status = -ENXIO;
  11823. goto out;
  11824. }
  11825. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11826. if (drq->queue_id == 0xFFFF) {
  11827. status = -ENXIO;
  11828. goto out;
  11829. }
  11830. drq->type = LPFC_DRQ;
  11831. drq->assoc_qid = cq->queue_id;
  11832. drq->subtype = subtype;
  11833. drq->host_index = 0;
  11834. drq->hba_index = 0;
  11835. /* link the header and data RQs onto the parent cq child list */
  11836. list_add_tail(&hrq->list, &cq->child_list);
  11837. list_add_tail(&drq->list, &cq->child_list);
  11838. out:
  11839. mempool_free(mbox, phba->mbox_mem_pool);
  11840. return status;
  11841. }
  11842. /**
  11843. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  11844. * @eq: The queue structure associated with the queue to destroy.
  11845. *
  11846. * This function destroys a queue, as detailed in @eq by sending an mailbox
  11847. * command, specific to the type of queue, to the HBA.
  11848. *
  11849. * The @eq struct is used to get the queue ID of the queue to destroy.
  11850. *
  11851. * On success this function will return a zero. If the queue destroy mailbox
  11852. * command fails this function will return -ENXIO.
  11853. **/
  11854. uint32_t
  11855. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11856. {
  11857. LPFC_MBOXQ_t *mbox;
  11858. int rc, length, status = 0;
  11859. uint32_t shdr_status, shdr_add_status;
  11860. union lpfc_sli4_cfg_shdr *shdr;
  11861. /* sanity check on queue memory */
  11862. if (!eq)
  11863. return -ENODEV;
  11864. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  11865. if (!mbox)
  11866. return -ENOMEM;
  11867. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  11868. sizeof(struct lpfc_sli4_cfg_mhdr));
  11869. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11870. LPFC_MBOX_OPCODE_EQ_DESTROY,
  11871. length, LPFC_SLI4_MBX_EMBED);
  11872. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  11873. eq->queue_id);
  11874. mbox->vport = eq->phba->pport;
  11875. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11876. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  11877. /* The IOCTL status is embedded in the mailbox subheader. */
  11878. shdr = (union lpfc_sli4_cfg_shdr *)
  11879. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  11880. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11881. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11882. if (shdr_status || shdr_add_status || rc) {
  11883. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11884. "2505 EQ_DESTROY mailbox failed with "
  11885. "status x%x add_status x%x, mbx status x%x\n",
  11886. shdr_status, shdr_add_status, rc);
  11887. status = -ENXIO;
  11888. }
  11889. /* Remove eq from any list */
  11890. list_del_init(&eq->list);
  11891. mempool_free(mbox, eq->phba->mbox_mem_pool);
  11892. return status;
  11893. }
  11894. /**
  11895. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  11896. * @cq: The queue structure associated with the queue to destroy.
  11897. *
  11898. * This function destroys a queue, as detailed in @cq by sending an mailbox
  11899. * command, specific to the type of queue, to the HBA.
  11900. *
  11901. * The @cq struct is used to get the queue ID of the queue to destroy.
  11902. *
  11903. * On success this function will return a zero. If the queue destroy mailbox
  11904. * command fails this function will return -ENXIO.
  11905. **/
  11906. uint32_t
  11907. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  11908. {
  11909. LPFC_MBOXQ_t *mbox;
  11910. int rc, length, status = 0;
  11911. uint32_t shdr_status, shdr_add_status;
  11912. union lpfc_sli4_cfg_shdr *shdr;
  11913. /* sanity check on queue memory */
  11914. if (!cq)
  11915. return -ENODEV;
  11916. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  11917. if (!mbox)
  11918. return -ENOMEM;
  11919. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  11920. sizeof(struct lpfc_sli4_cfg_mhdr));
  11921. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11922. LPFC_MBOX_OPCODE_CQ_DESTROY,
  11923. length, LPFC_SLI4_MBX_EMBED);
  11924. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  11925. cq->queue_id);
  11926. mbox->vport = cq->phba->pport;
  11927. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11928. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  11929. /* The IOCTL status is embedded in the mailbox subheader. */
  11930. shdr = (union lpfc_sli4_cfg_shdr *)
  11931. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  11932. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11933. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11934. if (shdr_status || shdr_add_status || rc) {
  11935. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11936. "2506 CQ_DESTROY mailbox failed with "
  11937. "status x%x add_status x%x, mbx status x%x\n",
  11938. shdr_status, shdr_add_status, rc);
  11939. status = -ENXIO;
  11940. }
  11941. /* Remove cq from any list */
  11942. list_del_init(&cq->list);
  11943. mempool_free(mbox, cq->phba->mbox_mem_pool);
  11944. return status;
  11945. }
  11946. /**
  11947. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  11948. * @qm: The queue structure associated with the queue to destroy.
  11949. *
  11950. * This function destroys a queue, as detailed in @mq by sending an mailbox
  11951. * command, specific to the type of queue, to the HBA.
  11952. *
  11953. * The @mq struct is used to get the queue ID of the queue to destroy.
  11954. *
  11955. * On success this function will return a zero. If the queue destroy mailbox
  11956. * command fails this function will return -ENXIO.
  11957. **/
  11958. uint32_t
  11959. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  11960. {
  11961. LPFC_MBOXQ_t *mbox;
  11962. int rc, length, status = 0;
  11963. uint32_t shdr_status, shdr_add_status;
  11964. union lpfc_sli4_cfg_shdr *shdr;
  11965. /* sanity check on queue memory */
  11966. if (!mq)
  11967. return -ENODEV;
  11968. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  11969. if (!mbox)
  11970. return -ENOMEM;
  11971. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  11972. sizeof(struct lpfc_sli4_cfg_mhdr));
  11973. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11974. LPFC_MBOX_OPCODE_MQ_DESTROY,
  11975. length, LPFC_SLI4_MBX_EMBED);
  11976. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  11977. mq->queue_id);
  11978. mbox->vport = mq->phba->pport;
  11979. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11980. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  11981. /* The IOCTL status is embedded in the mailbox subheader. */
  11982. shdr = (union lpfc_sli4_cfg_shdr *)
  11983. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  11984. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11985. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11986. if (shdr_status || shdr_add_status || rc) {
  11987. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11988. "2507 MQ_DESTROY mailbox failed with "
  11989. "status x%x add_status x%x, mbx status x%x\n",
  11990. shdr_status, shdr_add_status, rc);
  11991. status = -ENXIO;
  11992. }
  11993. /* Remove mq from any list */
  11994. list_del_init(&mq->list);
  11995. mempool_free(mbox, mq->phba->mbox_mem_pool);
  11996. return status;
  11997. }
  11998. /**
  11999. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  12000. * @wq: The queue structure associated with the queue to destroy.
  12001. *
  12002. * This function destroys a queue, as detailed in @wq by sending an mailbox
  12003. * command, specific to the type of queue, to the HBA.
  12004. *
  12005. * The @wq struct is used to get the queue ID of the queue to destroy.
  12006. *
  12007. * On success this function will return a zero. If the queue destroy mailbox
  12008. * command fails this function will return -ENXIO.
  12009. **/
  12010. uint32_t
  12011. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  12012. {
  12013. LPFC_MBOXQ_t *mbox;
  12014. int rc, length, status = 0;
  12015. uint32_t shdr_status, shdr_add_status;
  12016. union lpfc_sli4_cfg_shdr *shdr;
  12017. /* sanity check on queue memory */
  12018. if (!wq)
  12019. return -ENODEV;
  12020. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  12021. if (!mbox)
  12022. return -ENOMEM;
  12023. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  12024. sizeof(struct lpfc_sli4_cfg_mhdr));
  12025. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12026. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  12027. length, LPFC_SLI4_MBX_EMBED);
  12028. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  12029. wq->queue_id);
  12030. mbox->vport = wq->phba->pport;
  12031. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12032. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  12033. shdr = (union lpfc_sli4_cfg_shdr *)
  12034. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  12035. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12036. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12037. if (shdr_status || shdr_add_status || rc) {
  12038. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12039. "2508 WQ_DESTROY mailbox failed with "
  12040. "status x%x add_status x%x, mbx status x%x\n",
  12041. shdr_status, shdr_add_status, rc);
  12042. status = -ENXIO;
  12043. }
  12044. /* Remove wq from any list */
  12045. list_del_init(&wq->list);
  12046. mempool_free(mbox, wq->phba->mbox_mem_pool);
  12047. return status;
  12048. }
  12049. /**
  12050. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  12051. * @rq: The queue structure associated with the queue to destroy.
  12052. *
  12053. * This function destroys a queue, as detailed in @rq by sending an mailbox
  12054. * command, specific to the type of queue, to the HBA.
  12055. *
  12056. * The @rq struct is used to get the queue ID of the queue to destroy.
  12057. *
  12058. * On success this function will return a zero. If the queue destroy mailbox
  12059. * command fails this function will return -ENXIO.
  12060. **/
  12061. uint32_t
  12062. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  12063. struct lpfc_queue *drq)
  12064. {
  12065. LPFC_MBOXQ_t *mbox;
  12066. int rc, length, status = 0;
  12067. uint32_t shdr_status, shdr_add_status;
  12068. union lpfc_sli4_cfg_shdr *shdr;
  12069. /* sanity check on queue memory */
  12070. if (!hrq || !drq)
  12071. return -ENODEV;
  12072. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  12073. if (!mbox)
  12074. return -ENOMEM;
  12075. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  12076. sizeof(struct lpfc_sli4_cfg_mhdr));
  12077. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12078. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  12079. length, LPFC_SLI4_MBX_EMBED);
  12080. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12081. hrq->queue_id);
  12082. mbox->vport = hrq->phba->pport;
  12083. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12084. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  12085. /* The IOCTL status is embedded in the mailbox subheader. */
  12086. shdr = (union lpfc_sli4_cfg_shdr *)
  12087. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12088. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12089. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12090. if (shdr_status || shdr_add_status || rc) {
  12091. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12092. "2509 RQ_DESTROY mailbox failed with "
  12093. "status x%x add_status x%x, mbx status x%x\n",
  12094. shdr_status, shdr_add_status, rc);
  12095. if (rc != MBX_TIMEOUT)
  12096. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12097. return -ENXIO;
  12098. }
  12099. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12100. drq->queue_id);
  12101. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  12102. shdr = (union lpfc_sli4_cfg_shdr *)
  12103. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12104. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12105. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12106. if (shdr_status || shdr_add_status || rc) {
  12107. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12108. "2510 RQ_DESTROY mailbox failed with "
  12109. "status x%x add_status x%x, mbx status x%x\n",
  12110. shdr_status, shdr_add_status, rc);
  12111. status = -ENXIO;
  12112. }
  12113. list_del_init(&hrq->list);
  12114. list_del_init(&drq->list);
  12115. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12116. return status;
  12117. }
  12118. /**
  12119. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  12120. * @phba: The virtual port for which this call being executed.
  12121. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  12122. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  12123. * @xritag: the xritag that ties this io to the SGL pages.
  12124. *
  12125. * This routine will post the sgl pages for the IO that has the xritag
  12126. * that is in the iocbq structure. The xritag is assigned during iocbq
  12127. * creation and persists for as long as the driver is loaded.
  12128. * if the caller has fewer than 256 scatter gather segments to map then
  12129. * pdma_phys_addr1 should be 0.
  12130. * If the caller needs to map more than 256 scatter gather segment then
  12131. * pdma_phys_addr1 should be a valid physical address.
  12132. * physical address for SGLs must be 64 byte aligned.
  12133. * If you are going to map 2 SGL's then the first one must have 256 entries
  12134. * the second sgl can have between 1 and 256 entries.
  12135. *
  12136. * Return codes:
  12137. * 0 - Success
  12138. * -ENXIO, -ENOMEM - Failure
  12139. **/
  12140. int
  12141. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  12142. dma_addr_t pdma_phys_addr0,
  12143. dma_addr_t pdma_phys_addr1,
  12144. uint16_t xritag)
  12145. {
  12146. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  12147. LPFC_MBOXQ_t *mbox;
  12148. int rc;
  12149. uint32_t shdr_status, shdr_add_status;
  12150. uint32_t mbox_tmo;
  12151. union lpfc_sli4_cfg_shdr *shdr;
  12152. if (xritag == NO_XRI) {
  12153. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12154. "0364 Invalid param:\n");
  12155. return -EINVAL;
  12156. }
  12157. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12158. if (!mbox)
  12159. return -ENOMEM;
  12160. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12161. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12162. sizeof(struct lpfc_mbx_post_sgl_pages) -
  12163. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  12164. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  12165. &mbox->u.mqe.un.post_sgl_pages;
  12166. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  12167. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  12168. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  12169. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  12170. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  12171. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  12172. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  12173. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  12174. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  12175. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  12176. if (!phba->sli4_hba.intr_enable)
  12177. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12178. else {
  12179. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12180. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12181. }
  12182. /* The IOCTL status is embedded in the mailbox subheader. */
  12183. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  12184. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12185. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12186. if (rc != MBX_TIMEOUT)
  12187. mempool_free(mbox, phba->mbox_mem_pool);
  12188. if (shdr_status || shdr_add_status || rc) {
  12189. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12190. "2511 POST_SGL mailbox failed with "
  12191. "status x%x add_status x%x, mbx status x%x\n",
  12192. shdr_status, shdr_add_status, rc);
  12193. rc = -ENXIO;
  12194. }
  12195. return 0;
  12196. }
  12197. /**
  12198. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  12199. * @phba: pointer to lpfc hba data structure.
  12200. *
  12201. * This routine is invoked to post rpi header templates to the
  12202. * HBA consistent with the SLI-4 interface spec. This routine
  12203. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  12204. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  12205. *
  12206. * Returns
  12207. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  12208. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  12209. **/
  12210. uint16_t
  12211. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  12212. {
  12213. unsigned long xri;
  12214. /*
  12215. * Fetch the next logical xri. Because this index is logical,
  12216. * the driver starts at 0 each time.
  12217. */
  12218. spin_lock_irq(&phba->hbalock);
  12219. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  12220. phba->sli4_hba.max_cfg_param.max_xri, 0);
  12221. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  12222. spin_unlock_irq(&phba->hbalock);
  12223. return NO_XRI;
  12224. } else {
  12225. set_bit(xri, phba->sli4_hba.xri_bmask);
  12226. phba->sli4_hba.max_cfg_param.xri_used++;
  12227. }
  12228. spin_unlock_irq(&phba->hbalock);
  12229. return xri;
  12230. }
  12231. /**
  12232. * lpfc_sli4_free_xri - Release an xri for reuse.
  12233. * @phba: pointer to lpfc hba data structure.
  12234. *
  12235. * This routine is invoked to release an xri to the pool of
  12236. * available rpis maintained by the driver.
  12237. **/
  12238. void
  12239. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12240. {
  12241. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  12242. phba->sli4_hba.max_cfg_param.xri_used--;
  12243. }
  12244. }
  12245. /**
  12246. * lpfc_sli4_free_xri - Release an xri for reuse.
  12247. * @phba: pointer to lpfc hba data structure.
  12248. *
  12249. * This routine is invoked to release an xri to the pool of
  12250. * available rpis maintained by the driver.
  12251. **/
  12252. void
  12253. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12254. {
  12255. spin_lock_irq(&phba->hbalock);
  12256. __lpfc_sli4_free_xri(phba, xri);
  12257. spin_unlock_irq(&phba->hbalock);
  12258. }
  12259. /**
  12260. * lpfc_sli4_next_xritag - Get an xritag for the io
  12261. * @phba: Pointer to HBA context object.
  12262. *
  12263. * This function gets an xritag for the iocb. If there is no unused xritag
  12264. * it will return 0xffff.
  12265. * The function returns the allocated xritag if successful, else returns zero.
  12266. * Zero is not a valid xritag.
  12267. * The caller is not required to hold any lock.
  12268. **/
  12269. uint16_t
  12270. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  12271. {
  12272. uint16_t xri_index;
  12273. xri_index = lpfc_sli4_alloc_xri(phba);
  12274. if (xri_index == NO_XRI)
  12275. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  12276. "2004 Failed to allocate XRI.last XRITAG is %d"
  12277. " Max XRI is %d, Used XRI is %d\n",
  12278. xri_index,
  12279. phba->sli4_hba.max_cfg_param.max_xri,
  12280. phba->sli4_hba.max_cfg_param.xri_used);
  12281. return xri_index;
  12282. }
  12283. /**
  12284. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  12285. * @phba: pointer to lpfc hba data structure.
  12286. * @post_sgl_list: pointer to els sgl entry list.
  12287. * @count: number of els sgl entries on the list.
  12288. *
  12289. * This routine is invoked to post a block of driver's sgl pages to the
  12290. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12291. * is only called when the driver is loading and after all IO has been
  12292. * stopped.
  12293. **/
  12294. static int
  12295. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
  12296. struct list_head *post_sgl_list,
  12297. int post_cnt)
  12298. {
  12299. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  12300. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12301. struct sgl_page_pairs *sgl_pg_pairs;
  12302. void *viraddr;
  12303. LPFC_MBOXQ_t *mbox;
  12304. uint32_t reqlen, alloclen, pg_pairs;
  12305. uint32_t mbox_tmo;
  12306. uint16_t xritag_start = 0;
  12307. int rc = 0;
  12308. uint32_t shdr_status, shdr_add_status;
  12309. union lpfc_sli4_cfg_shdr *shdr;
  12310. reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12311. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12312. if (reqlen > SLI4_PAGE_SIZE) {
  12313. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12314. "2559 Block sgl registration required DMA "
  12315. "size (%d) great than a page\n", reqlen);
  12316. return -ENOMEM;
  12317. }
  12318. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12319. if (!mbox)
  12320. return -ENOMEM;
  12321. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12322. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12323. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12324. LPFC_SLI4_MBX_NEMBED);
  12325. if (alloclen < reqlen) {
  12326. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12327. "0285 Allocated DMA memory size (%d) is "
  12328. "less than the requested DMA memory "
  12329. "size (%d)\n", alloclen, reqlen);
  12330. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12331. return -ENOMEM;
  12332. }
  12333. /* Set up the SGL pages in the non-embedded DMA pages */
  12334. viraddr = mbox->sge_array->addr[0];
  12335. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12336. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12337. pg_pairs = 0;
  12338. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  12339. /* Set up the sge entry */
  12340. sgl_pg_pairs->sgl_pg0_addr_lo =
  12341. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12342. sgl_pg_pairs->sgl_pg0_addr_hi =
  12343. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12344. sgl_pg_pairs->sgl_pg1_addr_lo =
  12345. cpu_to_le32(putPaddrLow(0));
  12346. sgl_pg_pairs->sgl_pg1_addr_hi =
  12347. cpu_to_le32(putPaddrHigh(0));
  12348. /* Keep the first xritag on the list */
  12349. if (pg_pairs == 0)
  12350. xritag_start = sglq_entry->sli4_xritag;
  12351. sgl_pg_pairs++;
  12352. pg_pairs++;
  12353. }
  12354. /* Complete initialization and perform endian conversion. */
  12355. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12356. bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
  12357. sgl->word0 = cpu_to_le32(sgl->word0);
  12358. if (!phba->sli4_hba.intr_enable)
  12359. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12360. else {
  12361. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12362. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12363. }
  12364. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12365. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12366. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12367. if (rc != MBX_TIMEOUT)
  12368. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12369. if (shdr_status || shdr_add_status || rc) {
  12370. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12371. "2513 POST_SGL_BLOCK mailbox command failed "
  12372. "status x%x add_status x%x mbx status x%x\n",
  12373. shdr_status, shdr_add_status, rc);
  12374. rc = -ENXIO;
  12375. }
  12376. return rc;
  12377. }
  12378. /**
  12379. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12380. * @phba: pointer to lpfc hba data structure.
  12381. * @sblist: pointer to scsi buffer list.
  12382. * @count: number of scsi buffers on the list.
  12383. *
  12384. * This routine is invoked to post a block of @count scsi sgl pages from a
  12385. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12386. * No Lock is held.
  12387. *
  12388. **/
  12389. int
  12390. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  12391. struct list_head *sblist,
  12392. int count)
  12393. {
  12394. struct lpfc_scsi_buf *psb;
  12395. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12396. struct sgl_page_pairs *sgl_pg_pairs;
  12397. void *viraddr;
  12398. LPFC_MBOXQ_t *mbox;
  12399. uint32_t reqlen, alloclen, pg_pairs;
  12400. uint32_t mbox_tmo;
  12401. uint16_t xritag_start = 0;
  12402. int rc = 0;
  12403. uint32_t shdr_status, shdr_add_status;
  12404. dma_addr_t pdma_phys_bpl1;
  12405. union lpfc_sli4_cfg_shdr *shdr;
  12406. /* Calculate the requested length of the dma memory */
  12407. reqlen = count * sizeof(struct sgl_page_pairs) +
  12408. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12409. if (reqlen > SLI4_PAGE_SIZE) {
  12410. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12411. "0217 Block sgl registration required DMA "
  12412. "size (%d) great than a page\n", reqlen);
  12413. return -ENOMEM;
  12414. }
  12415. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12416. if (!mbox) {
  12417. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12418. "0283 Failed to allocate mbox cmd memory\n");
  12419. return -ENOMEM;
  12420. }
  12421. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12422. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12423. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12424. LPFC_SLI4_MBX_NEMBED);
  12425. if (alloclen < reqlen) {
  12426. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12427. "2561 Allocated DMA memory size (%d) is "
  12428. "less than the requested DMA memory "
  12429. "size (%d)\n", alloclen, reqlen);
  12430. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12431. return -ENOMEM;
  12432. }
  12433. /* Get the first SGE entry from the non-embedded DMA memory */
  12434. viraddr = mbox->sge_array->addr[0];
  12435. /* Set up the SGL pages in the non-embedded DMA pages */
  12436. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12437. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12438. pg_pairs = 0;
  12439. list_for_each_entry(psb, sblist, list) {
  12440. /* Set up the sge entry */
  12441. sgl_pg_pairs->sgl_pg0_addr_lo =
  12442. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12443. sgl_pg_pairs->sgl_pg0_addr_hi =
  12444. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12445. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12446. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12447. else
  12448. pdma_phys_bpl1 = 0;
  12449. sgl_pg_pairs->sgl_pg1_addr_lo =
  12450. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12451. sgl_pg_pairs->sgl_pg1_addr_hi =
  12452. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12453. /* Keep the first xritag on the list */
  12454. if (pg_pairs == 0)
  12455. xritag_start = psb->cur_iocbq.sli4_xritag;
  12456. sgl_pg_pairs++;
  12457. pg_pairs++;
  12458. }
  12459. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12460. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12461. /* Perform endian conversion if necessary */
  12462. sgl->word0 = cpu_to_le32(sgl->word0);
  12463. if (!phba->sli4_hba.intr_enable)
  12464. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12465. else {
  12466. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12467. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12468. }
  12469. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12470. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12471. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12472. if (rc != MBX_TIMEOUT)
  12473. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12474. if (shdr_status || shdr_add_status || rc) {
  12475. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12476. "2564 POST_SGL_BLOCK mailbox command failed "
  12477. "status x%x add_status x%x mbx status x%x\n",
  12478. shdr_status, shdr_add_status, rc);
  12479. rc = -ENXIO;
  12480. }
  12481. return rc;
  12482. }
  12483. /**
  12484. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12485. * @phba: pointer to lpfc_hba struct that the frame was received on
  12486. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12487. *
  12488. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12489. * valid type of frame that the LPFC driver will handle. This function will
  12490. * return a zero if the frame is a valid frame or a non zero value when the
  12491. * frame does not pass the check.
  12492. **/
  12493. static int
  12494. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12495. {
  12496. /* make rctl_names static to save stack space */
  12497. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12498. char *type_names[] = FC_TYPE_NAMES_INIT;
  12499. struct fc_vft_header *fc_vft_hdr;
  12500. uint32_t *header = (uint32_t *) fc_hdr;
  12501. switch (fc_hdr->fh_r_ctl) {
  12502. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12503. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12504. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12505. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12506. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12507. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12508. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12509. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12510. case FC_RCTL_ELS_REQ: /* extended link services request */
  12511. case FC_RCTL_ELS_REP: /* extended link services reply */
  12512. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12513. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12514. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12515. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12516. case FC_RCTL_BA_RMC: /* remove connection */
  12517. case FC_RCTL_BA_ACC: /* basic accept */
  12518. case FC_RCTL_BA_RJT: /* basic reject */
  12519. case FC_RCTL_BA_PRMT:
  12520. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12521. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12522. case FC_RCTL_P_RJT: /* port reject */
  12523. case FC_RCTL_F_RJT: /* fabric reject */
  12524. case FC_RCTL_P_BSY: /* port busy */
  12525. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12526. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12527. case FC_RCTL_LCR: /* link credit reset */
  12528. case FC_RCTL_END: /* end */
  12529. break;
  12530. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12531. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12532. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12533. return lpfc_fc_frame_check(phba, fc_hdr);
  12534. default:
  12535. goto drop;
  12536. }
  12537. switch (fc_hdr->fh_type) {
  12538. case FC_TYPE_BLS:
  12539. case FC_TYPE_ELS:
  12540. case FC_TYPE_FCP:
  12541. case FC_TYPE_CT:
  12542. break;
  12543. case FC_TYPE_IP:
  12544. case FC_TYPE_ILS:
  12545. default:
  12546. goto drop;
  12547. }
  12548. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12549. "2538 Received frame rctl:%s type:%s "
  12550. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12551. rctl_names[fc_hdr->fh_r_ctl],
  12552. type_names[fc_hdr->fh_type],
  12553. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12554. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12555. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12556. return 0;
  12557. drop:
  12558. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12559. "2539 Dropped frame rctl:%s type:%s\n",
  12560. rctl_names[fc_hdr->fh_r_ctl],
  12561. type_names[fc_hdr->fh_type]);
  12562. return 1;
  12563. }
  12564. /**
  12565. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12566. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12567. *
  12568. * This function processes the FC header to retrieve the VFI from the VF
  12569. * header, if one exists. This function will return the VFI if one exists
  12570. * or 0 if no VSAN Header exists.
  12571. **/
  12572. static uint32_t
  12573. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12574. {
  12575. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12576. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12577. return 0;
  12578. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12579. }
  12580. /**
  12581. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12582. * @phba: Pointer to the HBA structure to search for the vport on
  12583. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12584. * @fcfi: The FC Fabric ID that the frame came from
  12585. *
  12586. * This function searches the @phba for a vport that matches the content of the
  12587. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12588. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12589. * returns the matching vport pointer or NULL if unable to match frame to a
  12590. * vport.
  12591. **/
  12592. static struct lpfc_vport *
  12593. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12594. uint16_t fcfi)
  12595. {
  12596. struct lpfc_vport **vports;
  12597. struct lpfc_vport *vport = NULL;
  12598. int i;
  12599. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12600. fc_hdr->fh_d_id[1] << 8 |
  12601. fc_hdr->fh_d_id[2]);
  12602. if (did == Fabric_DID)
  12603. return phba->pport;
  12604. if ((phba->pport->fc_flag & FC_PT2PT) &&
  12605. !(phba->link_state == LPFC_HBA_READY))
  12606. return phba->pport;
  12607. vports = lpfc_create_vport_work_array(phba);
  12608. if (vports != NULL)
  12609. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12610. if (phba->fcf.fcfi == fcfi &&
  12611. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12612. vports[i]->fc_myDID == did) {
  12613. vport = vports[i];
  12614. break;
  12615. }
  12616. }
  12617. lpfc_destroy_vport_work_array(phba, vports);
  12618. return vport;
  12619. }
  12620. /**
  12621. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12622. * @vport: The vport to work on.
  12623. *
  12624. * This function updates the receive sequence time stamp for this vport. The
  12625. * receive sequence time stamp indicates the time that the last frame of the
  12626. * the sequence that has been idle for the longest amount of time was received.
  12627. * the driver uses this time stamp to indicate if any received sequences have
  12628. * timed out.
  12629. **/
  12630. void
  12631. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12632. {
  12633. struct lpfc_dmabuf *h_buf;
  12634. struct hbq_dmabuf *dmabuf = NULL;
  12635. /* get the oldest sequence on the rcv list */
  12636. h_buf = list_get_first(&vport->rcv_buffer_list,
  12637. struct lpfc_dmabuf, list);
  12638. if (!h_buf)
  12639. return;
  12640. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12641. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12642. }
  12643. /**
  12644. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12645. * @vport: The vport that the received sequences were sent to.
  12646. *
  12647. * This function cleans up all outstanding received sequences. This is called
  12648. * by the driver when a link event or user action invalidates all the received
  12649. * sequences.
  12650. **/
  12651. void
  12652. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12653. {
  12654. struct lpfc_dmabuf *h_buf, *hnext;
  12655. struct lpfc_dmabuf *d_buf, *dnext;
  12656. struct hbq_dmabuf *dmabuf = NULL;
  12657. /* start with the oldest sequence on the rcv list */
  12658. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12659. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12660. list_del_init(&dmabuf->hbuf.list);
  12661. list_for_each_entry_safe(d_buf, dnext,
  12662. &dmabuf->dbuf.list, list) {
  12663. list_del_init(&d_buf->list);
  12664. lpfc_in_buf_free(vport->phba, d_buf);
  12665. }
  12666. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12667. }
  12668. }
  12669. /**
  12670. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12671. * @vport: The vport that the received sequences were sent to.
  12672. *
  12673. * This function determines whether any received sequences have timed out by
  12674. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12675. * indicates that there is at least one timed out sequence this routine will
  12676. * go through the received sequences one at a time from most inactive to most
  12677. * active to determine which ones need to be cleaned up. Once it has determined
  12678. * that a sequence needs to be cleaned up it will simply free up the resources
  12679. * without sending an abort.
  12680. **/
  12681. void
  12682. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12683. {
  12684. struct lpfc_dmabuf *h_buf, *hnext;
  12685. struct lpfc_dmabuf *d_buf, *dnext;
  12686. struct hbq_dmabuf *dmabuf = NULL;
  12687. unsigned long timeout;
  12688. int abort_count = 0;
  12689. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12690. vport->rcv_buffer_time_stamp);
  12691. if (list_empty(&vport->rcv_buffer_list) ||
  12692. time_before(jiffies, timeout))
  12693. return;
  12694. /* start with the oldest sequence on the rcv list */
  12695. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12696. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12697. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12698. dmabuf->time_stamp);
  12699. if (time_before(jiffies, timeout))
  12700. break;
  12701. abort_count++;
  12702. list_del_init(&dmabuf->hbuf.list);
  12703. list_for_each_entry_safe(d_buf, dnext,
  12704. &dmabuf->dbuf.list, list) {
  12705. list_del_init(&d_buf->list);
  12706. lpfc_in_buf_free(vport->phba, d_buf);
  12707. }
  12708. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12709. }
  12710. if (abort_count)
  12711. lpfc_update_rcv_time_stamp(vport);
  12712. }
  12713. /**
  12714. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12715. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12716. *
  12717. * This function searches through the existing incomplete sequences that have
  12718. * been sent to this @vport. If the frame matches one of the incomplete
  12719. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12720. * make up that sequence. If no sequence is found that matches this frame then
  12721. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12722. * This function returns a pointer to the first dmabuf in the sequence list that
  12723. * the frame was linked to.
  12724. **/
  12725. static struct hbq_dmabuf *
  12726. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12727. {
  12728. struct fc_frame_header *new_hdr;
  12729. struct fc_frame_header *temp_hdr;
  12730. struct lpfc_dmabuf *d_buf;
  12731. struct lpfc_dmabuf *h_buf;
  12732. struct hbq_dmabuf *seq_dmabuf = NULL;
  12733. struct hbq_dmabuf *temp_dmabuf = NULL;
  12734. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12735. dmabuf->time_stamp = jiffies;
  12736. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12737. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12738. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12739. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12740. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12741. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12742. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12743. continue;
  12744. /* found a pending sequence that matches this frame */
  12745. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12746. break;
  12747. }
  12748. if (!seq_dmabuf) {
  12749. /*
  12750. * This indicates first frame received for this sequence.
  12751. * Queue the buffer on the vport's rcv_buffer_list.
  12752. */
  12753. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12754. lpfc_update_rcv_time_stamp(vport);
  12755. return dmabuf;
  12756. }
  12757. temp_hdr = seq_dmabuf->hbuf.virt;
  12758. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12759. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12760. list_del_init(&seq_dmabuf->hbuf.list);
  12761. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12762. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12763. lpfc_update_rcv_time_stamp(vport);
  12764. return dmabuf;
  12765. }
  12766. /* move this sequence to the tail to indicate a young sequence */
  12767. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12768. seq_dmabuf->time_stamp = jiffies;
  12769. lpfc_update_rcv_time_stamp(vport);
  12770. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12771. temp_hdr = dmabuf->hbuf.virt;
  12772. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12773. return seq_dmabuf;
  12774. }
  12775. /* find the correct place in the sequence to insert this frame */
  12776. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12777. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12778. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12779. /*
  12780. * If the frame's sequence count is greater than the frame on
  12781. * the list then insert the frame right after this frame
  12782. */
  12783. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  12784. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12785. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  12786. return seq_dmabuf;
  12787. }
  12788. }
  12789. return NULL;
  12790. }
  12791. /**
  12792. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  12793. * @vport: pointer to a vitural port
  12794. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12795. *
  12796. * This function tries to abort from the partially assembed sequence, described
  12797. * by the information from basic abbort @dmabuf. It checks to see whether such
  12798. * partially assembled sequence held by the driver. If so, it shall free up all
  12799. * the frames from the partially assembled sequence.
  12800. *
  12801. * Return
  12802. * true -- if there is matching partially assembled sequence present and all
  12803. * the frames freed with the sequence;
  12804. * false -- if there is no matching partially assembled sequence present so
  12805. * nothing got aborted in the lower layer driver
  12806. **/
  12807. static bool
  12808. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  12809. struct hbq_dmabuf *dmabuf)
  12810. {
  12811. struct fc_frame_header *new_hdr;
  12812. struct fc_frame_header *temp_hdr;
  12813. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  12814. struct hbq_dmabuf *seq_dmabuf = NULL;
  12815. /* Use the hdr_buf to find the sequence that matches this frame */
  12816. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12817. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  12818. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12819. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12820. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12821. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12822. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12823. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12824. continue;
  12825. /* found a pending sequence that matches this frame */
  12826. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12827. break;
  12828. }
  12829. /* Free up all the frames from the partially assembled sequence */
  12830. if (seq_dmabuf) {
  12831. list_for_each_entry_safe(d_buf, n_buf,
  12832. &seq_dmabuf->dbuf.list, list) {
  12833. list_del_init(&d_buf->list);
  12834. lpfc_in_buf_free(vport->phba, d_buf);
  12835. }
  12836. return true;
  12837. }
  12838. return false;
  12839. }
  12840. /**
  12841. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  12842. * @phba: Pointer to HBA context object.
  12843. * @cmd_iocbq: pointer to the command iocbq structure.
  12844. * @rsp_iocbq: pointer to the response iocbq structure.
  12845. *
  12846. * This function handles the sequence abort response iocb command complete
  12847. * event. It properly releases the memory allocated to the sequence abort
  12848. * accept iocb.
  12849. **/
  12850. static void
  12851. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  12852. struct lpfc_iocbq *cmd_iocbq,
  12853. struct lpfc_iocbq *rsp_iocbq)
  12854. {
  12855. if (cmd_iocbq)
  12856. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  12857. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  12858. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  12859. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12860. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  12861. rsp_iocbq->iocb.ulpStatus,
  12862. rsp_iocbq->iocb.un.ulpWord[4]);
  12863. }
  12864. /**
  12865. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  12866. * @phba: Pointer to HBA context object.
  12867. * @xri: xri id in transaction.
  12868. *
  12869. * This function validates the xri maps to the known range of XRIs allocated an
  12870. * used by the driver.
  12871. **/
  12872. uint16_t
  12873. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  12874. uint16_t xri)
  12875. {
  12876. int i;
  12877. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  12878. if (xri == phba->sli4_hba.xri_ids[i])
  12879. return i;
  12880. }
  12881. return NO_XRI;
  12882. }
  12883. /**
  12884. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  12885. * @phba: Pointer to HBA context object.
  12886. * @fc_hdr: pointer to a FC frame header.
  12887. *
  12888. * This function sends a basic response to a previous unsol sequence abort
  12889. * event after aborting the sequence handling.
  12890. **/
  12891. static void
  12892. lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
  12893. struct fc_frame_header *fc_hdr)
  12894. {
  12895. struct lpfc_iocbq *ctiocb = NULL;
  12896. struct lpfc_nodelist *ndlp;
  12897. uint16_t oxid, rxid, xri, lxri;
  12898. uint32_t sid, fctl;
  12899. IOCB_t *icmd;
  12900. int rc;
  12901. if (!lpfc_is_link_up(phba))
  12902. return;
  12903. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12904. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  12905. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  12906. ndlp = lpfc_findnode_did(phba->pport, sid);
  12907. if (!ndlp) {
  12908. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12909. "1268 Find ndlp returned NULL for oxid:x%x "
  12910. "SID:x%x\n", oxid, sid);
  12911. return;
  12912. }
  12913. /* Allocate buffer for rsp iocb */
  12914. ctiocb = lpfc_sli_get_iocbq(phba);
  12915. if (!ctiocb)
  12916. return;
  12917. /* Extract the F_CTL field from FC_HDR */
  12918. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  12919. icmd = &ctiocb->iocb;
  12920. icmd->un.xseq64.bdl.bdeSize = 0;
  12921. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  12922. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  12923. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  12924. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  12925. /* Fill in the rest of iocb fields */
  12926. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  12927. icmd->ulpBdeCount = 0;
  12928. icmd->ulpLe = 1;
  12929. icmd->ulpClass = CLASS3;
  12930. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  12931. ctiocb->context1 = ndlp;
  12932. ctiocb->iocb_cmpl = NULL;
  12933. ctiocb->vport = phba->pport;
  12934. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  12935. ctiocb->sli4_lxritag = NO_XRI;
  12936. ctiocb->sli4_xritag = NO_XRI;
  12937. if (fctl & FC_FC_EX_CTX)
  12938. /* Exchange responder sent the abort so we
  12939. * own the oxid.
  12940. */
  12941. xri = oxid;
  12942. else
  12943. xri = rxid;
  12944. lxri = lpfc_sli4_xri_inrange(phba, xri);
  12945. if (lxri != NO_XRI)
  12946. lpfc_set_rrq_active(phba, ndlp, lxri,
  12947. (xri == oxid) ? rxid : oxid, 0);
  12948. /* If the oxid maps to the FCP XRI range or if it is out of range,
  12949. * send a BLS_RJT. The driver no longer has that exchange.
  12950. * Override the IOCB for a BA_RJT.
  12951. */
  12952. if (xri > (phba->sli4_hba.max_cfg_param.max_xri +
  12953. phba->sli4_hba.max_cfg_param.xri_base) ||
  12954. xri > (lpfc_sli4_get_els_iocb_cnt(phba) +
  12955. phba->sli4_hba.max_cfg_param.xri_base)) {
  12956. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  12957. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  12958. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  12959. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  12960. }
  12961. if (fctl & FC_FC_EX_CTX) {
  12962. /* ABTS sent by responder to CT exchange, construction
  12963. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  12964. * field and RX_ID from ABTS for RX_ID field.
  12965. */
  12966. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  12967. } else {
  12968. /* ABTS sent by initiator to CT exchange, construction
  12969. * of BA_ACC will need to allocate a new XRI as for the
  12970. * XRI_TAG field.
  12971. */
  12972. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  12973. }
  12974. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  12975. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  12976. /* Xmit CT abts response on exchange <xid> */
  12977. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12978. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  12979. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  12980. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  12981. if (rc == IOCB_ERROR) {
  12982. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  12983. "2925 Failed to issue CT ABTS RSP x%x on "
  12984. "xri x%x, Data x%x\n",
  12985. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  12986. phba->link_state);
  12987. lpfc_sli_release_iocbq(phba, ctiocb);
  12988. }
  12989. }
  12990. /**
  12991. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  12992. * @vport: Pointer to the vport on which this sequence was received
  12993. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12994. *
  12995. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  12996. * receive sequence is only partially assembed by the driver, it shall abort
  12997. * the partially assembled frames for the sequence. Otherwise, if the
  12998. * unsolicited receive sequence has been completely assembled and passed to
  12999. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  13000. * unsolicited sequence has been aborted. After that, it will issue a basic
  13001. * accept to accept the abort.
  13002. **/
  13003. void
  13004. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  13005. struct hbq_dmabuf *dmabuf)
  13006. {
  13007. struct lpfc_hba *phba = vport->phba;
  13008. struct fc_frame_header fc_hdr;
  13009. uint32_t fctl;
  13010. bool abts_par;
  13011. /* Make a copy of fc_hdr before the dmabuf being released */
  13012. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  13013. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  13014. if (fctl & FC_FC_EX_CTX) {
  13015. /*
  13016. * ABTS sent by responder to exchange, just free the buffer
  13017. */
  13018. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13019. } else {
  13020. /*
  13021. * ABTS sent by initiator to exchange, need to do cleanup
  13022. */
  13023. /* Try to abort partially assembled seq */
  13024. abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  13025. /* Send abort to ULP if partially seq abort failed */
  13026. if (abts_par == false)
  13027. lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
  13028. else
  13029. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13030. }
  13031. /* Send basic accept (BA_ACC) to the abort requester */
  13032. lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
  13033. }
  13034. /**
  13035. * lpfc_seq_complete - Indicates if a sequence is complete
  13036. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13037. *
  13038. * This function checks the sequence, starting with the frame described by
  13039. * @dmabuf, to see if all the frames associated with this sequence are present.
  13040. * the frames associated with this sequence are linked to the @dmabuf using the
  13041. * dbuf list. This function looks for two major things. 1) That the first frame
  13042. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  13043. * set. 3) That there are no holes in the sequence count. The function will
  13044. * return 1 when the sequence is complete, otherwise it will return 0.
  13045. **/
  13046. static int
  13047. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  13048. {
  13049. struct fc_frame_header *hdr;
  13050. struct lpfc_dmabuf *d_buf;
  13051. struct hbq_dmabuf *seq_dmabuf;
  13052. uint32_t fctl;
  13053. int seq_count = 0;
  13054. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13055. /* make sure first fame of sequence has a sequence count of zero */
  13056. if (hdr->fh_seq_cnt != seq_count)
  13057. return 0;
  13058. fctl = (hdr->fh_f_ctl[0] << 16 |
  13059. hdr->fh_f_ctl[1] << 8 |
  13060. hdr->fh_f_ctl[2]);
  13061. /* If last frame of sequence we can return success. */
  13062. if (fctl & FC_FC_END_SEQ)
  13063. return 1;
  13064. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  13065. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13066. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13067. /* If there is a hole in the sequence count then fail. */
  13068. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  13069. return 0;
  13070. fctl = (hdr->fh_f_ctl[0] << 16 |
  13071. hdr->fh_f_ctl[1] << 8 |
  13072. hdr->fh_f_ctl[2]);
  13073. /* If last frame of sequence we can return success. */
  13074. if (fctl & FC_FC_END_SEQ)
  13075. return 1;
  13076. }
  13077. return 0;
  13078. }
  13079. /**
  13080. * lpfc_prep_seq - Prep sequence for ULP processing
  13081. * @vport: Pointer to the vport on which this sequence was received
  13082. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13083. *
  13084. * This function takes a sequence, described by a list of frames, and creates
  13085. * a list of iocbq structures to describe the sequence. This iocbq list will be
  13086. * used to issue to the generic unsolicited sequence handler. This routine
  13087. * returns a pointer to the first iocbq in the list. If the function is unable
  13088. * to allocate an iocbq then it throw out the received frames that were not
  13089. * able to be described and return a pointer to the first iocbq. If unable to
  13090. * allocate any iocbqs (including the first) this function will return NULL.
  13091. **/
  13092. static struct lpfc_iocbq *
  13093. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  13094. {
  13095. struct hbq_dmabuf *hbq_buf;
  13096. struct lpfc_dmabuf *d_buf, *n_buf;
  13097. struct lpfc_iocbq *first_iocbq, *iocbq;
  13098. struct fc_frame_header *fc_hdr;
  13099. uint32_t sid;
  13100. uint32_t len, tot_len;
  13101. struct ulp_bde64 *pbde;
  13102. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13103. /* remove from receive buffer list */
  13104. list_del_init(&seq_dmabuf->hbuf.list);
  13105. lpfc_update_rcv_time_stamp(vport);
  13106. /* get the Remote Port's SID */
  13107. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13108. tot_len = 0;
  13109. /* Get an iocbq struct to fill in. */
  13110. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  13111. if (first_iocbq) {
  13112. /* Initialize the first IOCB. */
  13113. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  13114. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  13115. /* Check FC Header to see what TYPE of frame we are rcv'ing */
  13116. if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
  13117. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
  13118. first_iocbq->iocb.un.rcvels.parmRo =
  13119. sli4_did_from_fc_hdr(fc_hdr);
  13120. first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
  13121. } else
  13122. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  13123. first_iocbq->iocb.ulpContext = NO_XRI;
  13124. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  13125. be16_to_cpu(fc_hdr->fh_ox_id);
  13126. /* iocbq is prepped for internal consumption. Physical vpi. */
  13127. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  13128. vport->phba->vpi_ids[vport->vpi];
  13129. /* put the first buffer into the first IOCBq */
  13130. first_iocbq->context2 = &seq_dmabuf->dbuf;
  13131. first_iocbq->context3 = NULL;
  13132. first_iocbq->iocb.ulpBdeCount = 1;
  13133. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13134. LPFC_DATA_BUF_SIZE;
  13135. first_iocbq->iocb.un.rcvels.remoteID = sid;
  13136. tot_len = bf_get(lpfc_rcqe_length,
  13137. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  13138. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13139. }
  13140. iocbq = first_iocbq;
  13141. /*
  13142. * Each IOCBq can have two Buffers assigned, so go through the list
  13143. * of buffers for this sequence and save two buffers in each IOCBq
  13144. */
  13145. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13146. if (!iocbq) {
  13147. lpfc_in_buf_free(vport->phba, d_buf);
  13148. continue;
  13149. }
  13150. if (!iocbq->context3) {
  13151. iocbq->context3 = d_buf;
  13152. iocbq->iocb.ulpBdeCount++;
  13153. pbde = (struct ulp_bde64 *)
  13154. &iocbq->iocb.unsli3.sli3Words[4];
  13155. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13156. /* We need to get the size out of the right CQE */
  13157. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13158. len = bf_get(lpfc_rcqe_length,
  13159. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13160. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13161. tot_len += len;
  13162. } else {
  13163. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13164. if (!iocbq) {
  13165. if (first_iocbq) {
  13166. first_iocbq->iocb.ulpStatus =
  13167. IOSTAT_FCP_RSP_ERROR;
  13168. first_iocbq->iocb.un.ulpWord[4] =
  13169. IOERR_NO_RESOURCES;
  13170. }
  13171. lpfc_in_buf_free(vport->phba, d_buf);
  13172. continue;
  13173. }
  13174. iocbq->context2 = d_buf;
  13175. iocbq->context3 = NULL;
  13176. iocbq->iocb.ulpBdeCount = 1;
  13177. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13178. LPFC_DATA_BUF_SIZE;
  13179. /* We need to get the size out of the right CQE */
  13180. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13181. len = bf_get(lpfc_rcqe_length,
  13182. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13183. tot_len += len;
  13184. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13185. iocbq->iocb.un.rcvels.remoteID = sid;
  13186. list_add_tail(&iocbq->list, &first_iocbq->list);
  13187. }
  13188. }
  13189. return first_iocbq;
  13190. }
  13191. static void
  13192. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13193. struct hbq_dmabuf *seq_dmabuf)
  13194. {
  13195. struct fc_frame_header *fc_hdr;
  13196. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13197. struct lpfc_hba *phba = vport->phba;
  13198. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13199. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13200. if (!iocbq) {
  13201. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13202. "2707 Ring %d handler: Failed to allocate "
  13203. "iocb Rctl x%x Type x%x received\n",
  13204. LPFC_ELS_RING,
  13205. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13206. return;
  13207. }
  13208. if (!lpfc_complete_unsol_iocb(phba,
  13209. &phba->sli.ring[LPFC_ELS_RING],
  13210. iocbq, fc_hdr->fh_r_ctl,
  13211. fc_hdr->fh_type))
  13212. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13213. "2540 Ring %d handler: unexpected Rctl "
  13214. "x%x Type x%x received\n",
  13215. LPFC_ELS_RING,
  13216. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13217. /* Free iocb created in lpfc_prep_seq */
  13218. list_for_each_entry_safe(curr_iocb, next_iocb,
  13219. &iocbq->list, list) {
  13220. list_del_init(&curr_iocb->list);
  13221. lpfc_sli_release_iocbq(phba, curr_iocb);
  13222. }
  13223. lpfc_sli_release_iocbq(phba, iocbq);
  13224. }
  13225. /**
  13226. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13227. * @phba: Pointer to HBA context object.
  13228. *
  13229. * This function is called with no lock held. This function processes all
  13230. * the received buffers and gives it to upper layers when a received buffer
  13231. * indicates that it is the final frame in the sequence. The interrupt
  13232. * service routine processes received buffers at interrupt contexts and adds
  13233. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13234. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13235. * appropriate receive function when the final frame in a sequence is received.
  13236. **/
  13237. void
  13238. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13239. struct hbq_dmabuf *dmabuf)
  13240. {
  13241. struct hbq_dmabuf *seq_dmabuf;
  13242. struct fc_frame_header *fc_hdr;
  13243. struct lpfc_vport *vport;
  13244. uint32_t fcfi;
  13245. uint32_t did;
  13246. /* Process each received buffer */
  13247. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13248. /* check to see if this a valid type of frame */
  13249. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13250. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13251. return;
  13252. }
  13253. if ((bf_get(lpfc_cqe_code,
  13254. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13255. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13256. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13257. else
  13258. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13259. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13260. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13261. if (!vport) {
  13262. /* throw out the frame */
  13263. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13264. return;
  13265. }
  13266. /* d_id this frame is directed to */
  13267. did = sli4_did_from_fc_hdr(fc_hdr);
  13268. /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
  13269. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
  13270. (did != Fabric_DID)) {
  13271. /*
  13272. * Throw out the frame if we are not pt2pt.
  13273. * The pt2pt protocol allows for discovery frames
  13274. * to be received without a registered VPI.
  13275. */
  13276. if (!(vport->fc_flag & FC_PT2PT) ||
  13277. (phba->link_state == LPFC_HBA_READY)) {
  13278. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13279. return;
  13280. }
  13281. }
  13282. /* Handle the basic abort sequence (BA_ABTS) event */
  13283. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13284. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13285. return;
  13286. }
  13287. /* Link this frame */
  13288. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13289. if (!seq_dmabuf) {
  13290. /* unable to add frame to vport - throw it out */
  13291. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13292. return;
  13293. }
  13294. /* If not last frame in sequence continue processing frames. */
  13295. if (!lpfc_seq_complete(seq_dmabuf))
  13296. return;
  13297. /* Send the complete sequence to the upper layer protocol */
  13298. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13299. }
  13300. /**
  13301. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13302. * @phba: pointer to lpfc hba data structure.
  13303. *
  13304. * This routine is invoked to post rpi header templates to the
  13305. * HBA consistent with the SLI-4 interface spec. This routine
  13306. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13307. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13308. *
  13309. * This routine does not require any locks. It's usage is expected
  13310. * to be driver load or reset recovery when the driver is
  13311. * sequential.
  13312. *
  13313. * Return codes
  13314. * 0 - successful
  13315. * -EIO - The mailbox failed to complete successfully.
  13316. * When this error occurs, the driver is not guaranteed
  13317. * to have any rpi regions posted to the device and
  13318. * must either attempt to repost the regions or take a
  13319. * fatal error.
  13320. **/
  13321. int
  13322. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13323. {
  13324. struct lpfc_rpi_hdr *rpi_page;
  13325. uint32_t rc = 0;
  13326. uint16_t lrpi = 0;
  13327. /* SLI4 ports that support extents do not require RPI headers. */
  13328. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13329. goto exit;
  13330. if (phba->sli4_hba.extents_in_use)
  13331. return -EIO;
  13332. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13333. /*
  13334. * Assign the rpi headers a physical rpi only if the driver
  13335. * has not initialized those resources. A port reset only
  13336. * needs the headers posted.
  13337. */
  13338. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13339. LPFC_RPI_RSRC_RDY)
  13340. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13341. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13342. if (rc != MBX_SUCCESS) {
  13343. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13344. "2008 Error %d posting all rpi "
  13345. "headers\n", rc);
  13346. rc = -EIO;
  13347. break;
  13348. }
  13349. }
  13350. exit:
  13351. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13352. LPFC_RPI_RSRC_RDY);
  13353. return rc;
  13354. }
  13355. /**
  13356. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13357. * @phba: pointer to lpfc hba data structure.
  13358. * @rpi_page: pointer to the rpi memory region.
  13359. *
  13360. * This routine is invoked to post a single rpi header to the
  13361. * HBA consistent with the SLI-4 interface spec. This memory region
  13362. * maps up to 64 rpi context regions.
  13363. *
  13364. * Return codes
  13365. * 0 - successful
  13366. * -ENOMEM - No available memory
  13367. * -EIO - The mailbox failed to complete successfully.
  13368. **/
  13369. int
  13370. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13371. {
  13372. LPFC_MBOXQ_t *mboxq;
  13373. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13374. uint32_t rc = 0;
  13375. uint32_t shdr_status, shdr_add_status;
  13376. union lpfc_sli4_cfg_shdr *shdr;
  13377. /* SLI4 ports that support extents do not require RPI headers. */
  13378. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13379. return rc;
  13380. if (phba->sli4_hba.extents_in_use)
  13381. return -EIO;
  13382. /* The port is notified of the header region via a mailbox command. */
  13383. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13384. if (!mboxq) {
  13385. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13386. "2001 Unable to allocate memory for issuing "
  13387. "SLI_CONFIG_SPECIAL mailbox command\n");
  13388. return -ENOMEM;
  13389. }
  13390. /* Post all rpi memory regions to the port. */
  13391. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13392. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13393. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13394. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13395. sizeof(struct lpfc_sli4_cfg_mhdr),
  13396. LPFC_SLI4_MBX_EMBED);
  13397. /* Post the physical rpi to the port for this rpi header. */
  13398. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13399. rpi_page->start_rpi);
  13400. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13401. hdr_tmpl, rpi_page->page_count);
  13402. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13403. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13404. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13405. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13406. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13407. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13408. if (rc != MBX_TIMEOUT)
  13409. mempool_free(mboxq, phba->mbox_mem_pool);
  13410. if (shdr_status || shdr_add_status || rc) {
  13411. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13412. "2514 POST_RPI_HDR mailbox failed with "
  13413. "status x%x add_status x%x, mbx status x%x\n",
  13414. shdr_status, shdr_add_status, rc);
  13415. rc = -ENXIO;
  13416. }
  13417. return rc;
  13418. }
  13419. /**
  13420. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13421. * @phba: pointer to lpfc hba data structure.
  13422. *
  13423. * This routine is invoked to post rpi header templates to the
  13424. * HBA consistent with the SLI-4 interface spec. This routine
  13425. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13426. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13427. *
  13428. * Returns
  13429. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13430. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13431. **/
  13432. int
  13433. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13434. {
  13435. unsigned long rpi;
  13436. uint16_t max_rpi, rpi_limit;
  13437. uint16_t rpi_remaining, lrpi = 0;
  13438. struct lpfc_rpi_hdr *rpi_hdr;
  13439. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13440. rpi_limit = phba->sli4_hba.next_rpi;
  13441. /*
  13442. * Fetch the next logical rpi. Because this index is logical,
  13443. * the driver starts at 0 each time.
  13444. */
  13445. spin_lock_irq(&phba->hbalock);
  13446. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13447. if (rpi >= rpi_limit)
  13448. rpi = LPFC_RPI_ALLOC_ERROR;
  13449. else {
  13450. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13451. phba->sli4_hba.max_cfg_param.rpi_used++;
  13452. phba->sli4_hba.rpi_count++;
  13453. }
  13454. /*
  13455. * Don't try to allocate more rpi header regions if the device limit
  13456. * has been exhausted.
  13457. */
  13458. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13459. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13460. spin_unlock_irq(&phba->hbalock);
  13461. return rpi;
  13462. }
  13463. /*
  13464. * RPI header postings are not required for SLI4 ports capable of
  13465. * extents.
  13466. */
  13467. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13468. spin_unlock_irq(&phba->hbalock);
  13469. return rpi;
  13470. }
  13471. /*
  13472. * If the driver is running low on rpi resources, allocate another
  13473. * page now. Note that the next_rpi value is used because
  13474. * it represents how many are actually in use whereas max_rpi notes
  13475. * how many are supported max by the device.
  13476. */
  13477. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13478. spin_unlock_irq(&phba->hbalock);
  13479. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13480. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13481. if (!rpi_hdr) {
  13482. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13483. "2002 Error Could not grow rpi "
  13484. "count\n");
  13485. } else {
  13486. lrpi = rpi_hdr->start_rpi;
  13487. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13488. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13489. }
  13490. }
  13491. return rpi;
  13492. }
  13493. /**
  13494. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13495. * @phba: pointer to lpfc hba data structure.
  13496. *
  13497. * This routine is invoked to release an rpi to the pool of
  13498. * available rpis maintained by the driver.
  13499. **/
  13500. void
  13501. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13502. {
  13503. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13504. phba->sli4_hba.rpi_count--;
  13505. phba->sli4_hba.max_cfg_param.rpi_used--;
  13506. }
  13507. }
  13508. /**
  13509. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13510. * @phba: pointer to lpfc hba data structure.
  13511. *
  13512. * This routine is invoked to release an rpi to the pool of
  13513. * available rpis maintained by the driver.
  13514. **/
  13515. void
  13516. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13517. {
  13518. spin_lock_irq(&phba->hbalock);
  13519. __lpfc_sli4_free_rpi(phba, rpi);
  13520. spin_unlock_irq(&phba->hbalock);
  13521. }
  13522. /**
  13523. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13524. * @phba: pointer to lpfc hba data structure.
  13525. *
  13526. * This routine is invoked to remove the memory region that
  13527. * provided rpi via a bitmask.
  13528. **/
  13529. void
  13530. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13531. {
  13532. kfree(phba->sli4_hba.rpi_bmask);
  13533. kfree(phba->sli4_hba.rpi_ids);
  13534. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13535. }
  13536. /**
  13537. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  13538. * @phba: pointer to lpfc hba data structure.
  13539. *
  13540. * This routine is invoked to remove the memory region that
  13541. * provided rpi via a bitmask.
  13542. **/
  13543. int
  13544. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  13545. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  13546. {
  13547. LPFC_MBOXQ_t *mboxq;
  13548. struct lpfc_hba *phba = ndlp->phba;
  13549. int rc;
  13550. /* The port is notified of the header region via a mailbox command. */
  13551. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13552. if (!mboxq)
  13553. return -ENOMEM;
  13554. /* Post all rpi memory regions to the port. */
  13555. lpfc_resume_rpi(mboxq, ndlp);
  13556. if (cmpl) {
  13557. mboxq->mbox_cmpl = cmpl;
  13558. mboxq->context1 = arg;
  13559. mboxq->context2 = ndlp;
  13560. } else
  13561. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13562. mboxq->vport = ndlp->vport;
  13563. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13564. if (rc == MBX_NOT_FINISHED) {
  13565. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13566. "2010 Resume RPI Mailbox failed "
  13567. "status %d, mbxStatus x%x\n", rc,
  13568. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13569. mempool_free(mboxq, phba->mbox_mem_pool);
  13570. return -EIO;
  13571. }
  13572. return 0;
  13573. }
  13574. /**
  13575. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13576. * @vport: Pointer to the vport for which the vpi is being initialized
  13577. *
  13578. * This routine is invoked to activate a vpi with the port.
  13579. *
  13580. * Returns:
  13581. * 0 success
  13582. * -Evalue otherwise
  13583. **/
  13584. int
  13585. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13586. {
  13587. LPFC_MBOXQ_t *mboxq;
  13588. int rc = 0;
  13589. int retval = MBX_SUCCESS;
  13590. uint32_t mbox_tmo;
  13591. struct lpfc_hba *phba = vport->phba;
  13592. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13593. if (!mboxq)
  13594. return -ENOMEM;
  13595. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13596. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13597. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13598. if (rc != MBX_SUCCESS) {
  13599. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13600. "2022 INIT VPI Mailbox failed "
  13601. "status %d, mbxStatus x%x\n", rc,
  13602. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13603. retval = -EIO;
  13604. }
  13605. if (rc != MBX_TIMEOUT)
  13606. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13607. return retval;
  13608. }
  13609. /**
  13610. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13611. * @phba: pointer to lpfc hba data structure.
  13612. * @mboxq: Pointer to mailbox object.
  13613. *
  13614. * This routine is invoked to manually add a single FCF record. The caller
  13615. * must pass a completely initialized FCF_Record. This routine takes
  13616. * care of the nonembedded mailbox operations.
  13617. **/
  13618. static void
  13619. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13620. {
  13621. void *virt_addr;
  13622. union lpfc_sli4_cfg_shdr *shdr;
  13623. uint32_t shdr_status, shdr_add_status;
  13624. virt_addr = mboxq->sge_array->addr[0];
  13625. /* The IOCTL status is embedded in the mailbox subheader. */
  13626. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13627. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13628. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13629. if ((shdr_status || shdr_add_status) &&
  13630. (shdr_status != STATUS_FCF_IN_USE))
  13631. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13632. "2558 ADD_FCF_RECORD mailbox failed with "
  13633. "status x%x add_status x%x\n",
  13634. shdr_status, shdr_add_status);
  13635. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13636. }
  13637. /**
  13638. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13639. * @phba: pointer to lpfc hba data structure.
  13640. * @fcf_record: pointer to the initialized fcf record to add.
  13641. *
  13642. * This routine is invoked to manually add a single FCF record. The caller
  13643. * must pass a completely initialized FCF_Record. This routine takes
  13644. * care of the nonembedded mailbox operations.
  13645. **/
  13646. int
  13647. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13648. {
  13649. int rc = 0;
  13650. LPFC_MBOXQ_t *mboxq;
  13651. uint8_t *bytep;
  13652. void *virt_addr;
  13653. dma_addr_t phys_addr;
  13654. struct lpfc_mbx_sge sge;
  13655. uint32_t alloc_len, req_len;
  13656. uint32_t fcfindex;
  13657. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13658. if (!mboxq) {
  13659. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13660. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13661. return -ENOMEM;
  13662. }
  13663. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13664. sizeof(uint32_t);
  13665. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13666. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13667. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13668. req_len, LPFC_SLI4_MBX_NEMBED);
  13669. if (alloc_len < req_len) {
  13670. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13671. "2523 Allocated DMA memory size (x%x) is "
  13672. "less than the requested DMA memory "
  13673. "size (x%x)\n", alloc_len, req_len);
  13674. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13675. return -ENOMEM;
  13676. }
  13677. /*
  13678. * Get the first SGE entry from the non-embedded DMA memory. This
  13679. * routine only uses a single SGE.
  13680. */
  13681. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13682. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13683. virt_addr = mboxq->sge_array->addr[0];
  13684. /*
  13685. * Configure the FCF record for FCFI 0. This is the driver's
  13686. * hardcoded default and gets used in nonFIP mode.
  13687. */
  13688. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13689. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13690. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13691. /*
  13692. * Copy the fcf_index and the FCF Record Data. The data starts after
  13693. * the FCoE header plus word10. The data copy needs to be endian
  13694. * correct.
  13695. */
  13696. bytep += sizeof(uint32_t);
  13697. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13698. mboxq->vport = phba->pport;
  13699. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13700. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13701. if (rc == MBX_NOT_FINISHED) {
  13702. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13703. "2515 ADD_FCF_RECORD mailbox failed with "
  13704. "status 0x%x\n", rc);
  13705. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13706. rc = -EIO;
  13707. } else
  13708. rc = 0;
  13709. return rc;
  13710. }
  13711. /**
  13712. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13713. * @phba: pointer to lpfc hba data structure.
  13714. * @fcf_record: pointer to the fcf record to write the default data.
  13715. * @fcf_index: FCF table entry index.
  13716. *
  13717. * This routine is invoked to build the driver's default FCF record. The
  13718. * values used are hardcoded. This routine handles memory initialization.
  13719. *
  13720. **/
  13721. void
  13722. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13723. struct fcf_record *fcf_record,
  13724. uint16_t fcf_index)
  13725. {
  13726. memset(fcf_record, 0, sizeof(struct fcf_record));
  13727. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  13728. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  13729. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  13730. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  13731. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  13732. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  13733. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  13734. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  13735. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  13736. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  13737. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  13738. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  13739. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  13740. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  13741. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  13742. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  13743. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  13744. /* Set the VLAN bit map */
  13745. if (phba->valid_vlan) {
  13746. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  13747. = 1 << (phba->vlan_id % 8);
  13748. }
  13749. }
  13750. /**
  13751. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  13752. * @phba: pointer to lpfc hba data structure.
  13753. * @fcf_index: FCF table entry offset.
  13754. *
  13755. * This routine is invoked to scan the entire FCF table by reading FCF
  13756. * record and processing it one at a time starting from the @fcf_index
  13757. * for initial FCF discovery or fast FCF failover rediscovery.
  13758. *
  13759. * Return 0 if the mailbox command is submitted successfully, none 0
  13760. * otherwise.
  13761. **/
  13762. int
  13763. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13764. {
  13765. int rc = 0, error;
  13766. LPFC_MBOXQ_t *mboxq;
  13767. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  13768. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  13769. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13770. if (!mboxq) {
  13771. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13772. "2000 Failed to allocate mbox for "
  13773. "READ_FCF cmd\n");
  13774. error = -ENOMEM;
  13775. goto fail_fcf_scan;
  13776. }
  13777. /* Construct the read FCF record mailbox command */
  13778. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13779. if (rc) {
  13780. error = -EINVAL;
  13781. goto fail_fcf_scan;
  13782. }
  13783. /* Issue the mailbox command asynchronously */
  13784. mboxq->vport = phba->pport;
  13785. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  13786. spin_lock_irq(&phba->hbalock);
  13787. phba->hba_flag |= FCF_TS_INPROG;
  13788. spin_unlock_irq(&phba->hbalock);
  13789. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13790. if (rc == MBX_NOT_FINISHED)
  13791. error = -EIO;
  13792. else {
  13793. /* Reset eligible FCF count for new scan */
  13794. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  13795. phba->fcf.eligible_fcf_cnt = 0;
  13796. error = 0;
  13797. }
  13798. fail_fcf_scan:
  13799. if (error) {
  13800. if (mboxq)
  13801. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13802. /* FCF scan failed, clear FCF_TS_INPROG flag */
  13803. spin_lock_irq(&phba->hbalock);
  13804. phba->hba_flag &= ~FCF_TS_INPROG;
  13805. spin_unlock_irq(&phba->hbalock);
  13806. }
  13807. return error;
  13808. }
  13809. /**
  13810. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  13811. * @phba: pointer to lpfc hba data structure.
  13812. * @fcf_index: FCF table entry offset.
  13813. *
  13814. * This routine is invoked to read an FCF record indicated by @fcf_index
  13815. * and to use it for FLOGI roundrobin FCF failover.
  13816. *
  13817. * Return 0 if the mailbox command is submitted successfully, none 0
  13818. * otherwise.
  13819. **/
  13820. int
  13821. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13822. {
  13823. int rc = 0, error;
  13824. LPFC_MBOXQ_t *mboxq;
  13825. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13826. if (!mboxq) {
  13827. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13828. "2763 Failed to allocate mbox for "
  13829. "READ_FCF cmd\n");
  13830. error = -ENOMEM;
  13831. goto fail_fcf_read;
  13832. }
  13833. /* Construct the read FCF record mailbox command */
  13834. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13835. if (rc) {
  13836. error = -EINVAL;
  13837. goto fail_fcf_read;
  13838. }
  13839. /* Issue the mailbox command asynchronously */
  13840. mboxq->vport = phba->pport;
  13841. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  13842. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13843. if (rc == MBX_NOT_FINISHED)
  13844. error = -EIO;
  13845. else
  13846. error = 0;
  13847. fail_fcf_read:
  13848. if (error && mboxq)
  13849. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13850. return error;
  13851. }
  13852. /**
  13853. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  13854. * @phba: pointer to lpfc hba data structure.
  13855. * @fcf_index: FCF table entry offset.
  13856. *
  13857. * This routine is invoked to read an FCF record indicated by @fcf_index to
  13858. * determine whether it's eligible for FLOGI roundrobin failover list.
  13859. *
  13860. * Return 0 if the mailbox command is submitted successfully, none 0
  13861. * otherwise.
  13862. **/
  13863. int
  13864. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13865. {
  13866. int rc = 0, error;
  13867. LPFC_MBOXQ_t *mboxq;
  13868. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13869. if (!mboxq) {
  13870. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13871. "2758 Failed to allocate mbox for "
  13872. "READ_FCF cmd\n");
  13873. error = -ENOMEM;
  13874. goto fail_fcf_read;
  13875. }
  13876. /* Construct the read FCF record mailbox command */
  13877. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13878. if (rc) {
  13879. error = -EINVAL;
  13880. goto fail_fcf_read;
  13881. }
  13882. /* Issue the mailbox command asynchronously */
  13883. mboxq->vport = phba->pport;
  13884. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  13885. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13886. if (rc == MBX_NOT_FINISHED)
  13887. error = -EIO;
  13888. else
  13889. error = 0;
  13890. fail_fcf_read:
  13891. if (error && mboxq)
  13892. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13893. return error;
  13894. }
  13895. /**
  13896. * lpfc_check_next_fcf_pri
  13897. * phba pointer to the lpfc_hba struct for this port.
  13898. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  13899. * routine when the rr_bmask is empty. The FCF indecies are put into the
  13900. * rr_bmask based on their priority level. Starting from the highest priority
  13901. * to the lowest. The most likely FCF candidate will be in the highest
  13902. * priority group. When this routine is called it searches the fcf_pri list for
  13903. * next lowest priority group and repopulates the rr_bmask with only those
  13904. * fcf_indexes.
  13905. * returns:
  13906. * 1=success 0=failure
  13907. **/
  13908. int
  13909. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  13910. {
  13911. uint16_t next_fcf_pri;
  13912. uint16_t last_index;
  13913. struct lpfc_fcf_pri *fcf_pri;
  13914. int rc;
  13915. int ret = 0;
  13916. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  13917. LPFC_SLI4_FCF_TBL_INDX_MAX);
  13918. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13919. "3060 Last IDX %d\n", last_index);
  13920. if (list_empty(&phba->fcf.fcf_pri_list)) {
  13921. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13922. "3061 Last IDX %d\n", last_index);
  13923. return 0; /* Empty rr list */
  13924. }
  13925. next_fcf_pri = 0;
  13926. /*
  13927. * Clear the rr_bmask and set all of the bits that are at this
  13928. * priority.
  13929. */
  13930. memset(phba->fcf.fcf_rr_bmask, 0,
  13931. sizeof(*phba->fcf.fcf_rr_bmask));
  13932. spin_lock_irq(&phba->hbalock);
  13933. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13934. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  13935. continue;
  13936. /*
  13937. * the 1st priority that has not FLOGI failed
  13938. * will be the highest.
  13939. */
  13940. if (!next_fcf_pri)
  13941. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13942. spin_unlock_irq(&phba->hbalock);
  13943. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13944. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13945. fcf_pri->fcf_rec.fcf_index);
  13946. if (rc)
  13947. return 0;
  13948. }
  13949. spin_lock_irq(&phba->hbalock);
  13950. }
  13951. /*
  13952. * if next_fcf_pri was not set above and the list is not empty then
  13953. * we have failed flogis on all of them. So reset flogi failed
  13954. * and start at the begining.
  13955. */
  13956. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  13957. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13958. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  13959. /*
  13960. * the 1st priority that has not FLOGI failed
  13961. * will be the highest.
  13962. */
  13963. if (!next_fcf_pri)
  13964. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13965. spin_unlock_irq(&phba->hbalock);
  13966. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13967. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13968. fcf_pri->fcf_rec.fcf_index);
  13969. if (rc)
  13970. return 0;
  13971. }
  13972. spin_lock_irq(&phba->hbalock);
  13973. }
  13974. } else
  13975. ret = 1;
  13976. spin_unlock_irq(&phba->hbalock);
  13977. return ret;
  13978. }
  13979. /**
  13980. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  13981. * @phba: pointer to lpfc hba data structure.
  13982. *
  13983. * This routine is to get the next eligible FCF record index in a round
  13984. * robin fashion. If the next eligible FCF record index equals to the
  13985. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  13986. * shall be returned, otherwise, the next eligible FCF record's index
  13987. * shall be returned.
  13988. **/
  13989. uint16_t
  13990. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  13991. {
  13992. uint16_t next_fcf_index;
  13993. /* Search start from next bit of currently registered FCF index */
  13994. next_priority:
  13995. next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
  13996. LPFC_SLI4_FCF_TBL_INDX_MAX;
  13997. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13998. LPFC_SLI4_FCF_TBL_INDX_MAX,
  13999. next_fcf_index);
  14000. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  14001. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14002. /*
  14003. * If we have wrapped then we need to clear the bits that
  14004. * have been tested so that we can detect when we should
  14005. * change the priority level.
  14006. */
  14007. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14008. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  14009. }
  14010. /* Check roundrobin failover list empty condition */
  14011. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  14012. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  14013. /*
  14014. * If next fcf index is not found check if there are lower
  14015. * Priority level fcf's in the fcf_priority list.
  14016. * Set up the rr_bmask with all of the avaiable fcf bits
  14017. * at that level and continue the selection process.
  14018. */
  14019. if (lpfc_check_next_fcf_pri_level(phba))
  14020. goto next_priority;
  14021. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14022. "2844 No roundrobin failover FCF available\n");
  14023. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  14024. return LPFC_FCOE_FCF_NEXT_NONE;
  14025. else {
  14026. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14027. "3063 Only FCF available idx %d, flag %x\n",
  14028. next_fcf_index,
  14029. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  14030. return next_fcf_index;
  14031. }
  14032. }
  14033. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  14034. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  14035. LPFC_FCF_FLOGI_FAILED)
  14036. goto next_priority;
  14037. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14038. "2845 Get next roundrobin failover FCF (x%x)\n",
  14039. next_fcf_index);
  14040. return next_fcf_index;
  14041. }
  14042. /**
  14043. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  14044. * @phba: pointer to lpfc hba data structure.
  14045. *
  14046. * This routine sets the FCF record index in to the eligible bmask for
  14047. * roundrobin failover search. It checks to make sure that the index
  14048. * does not go beyond the range of the driver allocated bmask dimension
  14049. * before setting the bit.
  14050. *
  14051. * Returns 0 if the index bit successfully set, otherwise, it returns
  14052. * -EINVAL.
  14053. **/
  14054. int
  14055. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  14056. {
  14057. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14058. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14059. "2610 FCF (x%x) reached driver's book "
  14060. "keeping dimension:x%x\n",
  14061. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14062. return -EINVAL;
  14063. }
  14064. /* Set the eligible FCF record index bmask */
  14065. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14066. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14067. "2790 Set FCF (x%x) to roundrobin FCF failover "
  14068. "bmask\n", fcf_index);
  14069. return 0;
  14070. }
  14071. /**
  14072. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  14073. * @phba: pointer to lpfc hba data structure.
  14074. *
  14075. * This routine clears the FCF record index from the eligible bmask for
  14076. * roundrobin failover search. It checks to make sure that the index
  14077. * does not go beyond the range of the driver allocated bmask dimension
  14078. * before clearing the bit.
  14079. **/
  14080. void
  14081. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  14082. {
  14083. struct lpfc_fcf_pri *fcf_pri;
  14084. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14085. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14086. "2762 FCF (x%x) reached driver's book "
  14087. "keeping dimension:x%x\n",
  14088. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14089. return;
  14090. }
  14091. /* Clear the eligible FCF record index bmask */
  14092. spin_lock_irq(&phba->hbalock);
  14093. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14094. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  14095. list_del_init(&fcf_pri->list);
  14096. break;
  14097. }
  14098. }
  14099. spin_unlock_irq(&phba->hbalock);
  14100. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14101. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14102. "2791 Clear FCF (x%x) from roundrobin failover "
  14103. "bmask\n", fcf_index);
  14104. }
  14105. /**
  14106. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  14107. * @phba: pointer to lpfc hba data structure.
  14108. *
  14109. * This routine is the completion routine for the rediscover FCF table mailbox
  14110. * command. If the mailbox command returned failure, it will try to stop the
  14111. * FCF rediscover wait timer.
  14112. **/
  14113. void
  14114. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  14115. {
  14116. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14117. uint32_t shdr_status, shdr_add_status;
  14118. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14119. shdr_status = bf_get(lpfc_mbox_hdr_status,
  14120. &redisc_fcf->header.cfg_shdr.response);
  14121. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  14122. &redisc_fcf->header.cfg_shdr.response);
  14123. if (shdr_status || shdr_add_status) {
  14124. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14125. "2746 Requesting for FCF rediscovery failed "
  14126. "status x%x add_status x%x\n",
  14127. shdr_status, shdr_add_status);
  14128. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  14129. spin_lock_irq(&phba->hbalock);
  14130. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  14131. spin_unlock_irq(&phba->hbalock);
  14132. /*
  14133. * CVL event triggered FCF rediscover request failed,
  14134. * last resort to re-try current registered FCF entry.
  14135. */
  14136. lpfc_retry_pport_discovery(phba);
  14137. } else {
  14138. spin_lock_irq(&phba->hbalock);
  14139. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  14140. spin_unlock_irq(&phba->hbalock);
  14141. /*
  14142. * DEAD FCF event triggered FCF rediscover request
  14143. * failed, last resort to fail over as a link down
  14144. * to FCF registration.
  14145. */
  14146. lpfc_sli4_fcf_dead_failthrough(phba);
  14147. }
  14148. } else {
  14149. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14150. "2775 Start FCF rediscover quiescent timer\n");
  14151. /*
  14152. * Start FCF rediscovery wait timer for pending FCF
  14153. * before rescan FCF record table.
  14154. */
  14155. lpfc_fcf_redisc_wait_start_timer(phba);
  14156. }
  14157. mempool_free(mbox, phba->mbox_mem_pool);
  14158. }
  14159. /**
  14160. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14161. * @phba: pointer to lpfc hba data structure.
  14162. *
  14163. * This routine is invoked to request for rediscovery of the entire FCF table
  14164. * by the port.
  14165. **/
  14166. int
  14167. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14168. {
  14169. LPFC_MBOXQ_t *mbox;
  14170. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14171. int rc, length;
  14172. /* Cancel retry delay timers to all vports before FCF rediscover */
  14173. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14174. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14175. if (!mbox) {
  14176. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14177. "2745 Failed to allocate mbox for "
  14178. "requesting FCF rediscover.\n");
  14179. return -ENOMEM;
  14180. }
  14181. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14182. sizeof(struct lpfc_sli4_cfg_mhdr));
  14183. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14184. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14185. length, LPFC_SLI4_MBX_EMBED);
  14186. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14187. /* Set count to 0 for invalidating the entire FCF database */
  14188. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14189. /* Issue the mailbox command asynchronously */
  14190. mbox->vport = phba->pport;
  14191. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14192. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14193. if (rc == MBX_NOT_FINISHED) {
  14194. mempool_free(mbox, phba->mbox_mem_pool);
  14195. return -EIO;
  14196. }
  14197. return 0;
  14198. }
  14199. /**
  14200. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14201. * @phba: pointer to lpfc hba data structure.
  14202. *
  14203. * This function is the failover routine as a last resort to the FCF DEAD
  14204. * event when driver failed to perform fast FCF failover.
  14205. **/
  14206. void
  14207. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14208. {
  14209. uint32_t link_state;
  14210. /*
  14211. * Last resort as FCF DEAD event failover will treat this as
  14212. * a link down, but save the link state because we don't want
  14213. * it to be changed to Link Down unless it is already down.
  14214. */
  14215. link_state = phba->link_state;
  14216. lpfc_linkdown(phba);
  14217. phba->link_state = link_state;
  14218. /* Unregister FCF if no devices connected to it */
  14219. lpfc_unregister_unused_fcf(phba);
  14220. }
  14221. /**
  14222. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14223. * @phba: pointer to lpfc hba data structure.
  14224. * @rgn23_data: pointer to configure region 23 data.
  14225. *
  14226. * This function gets SLI3 port configure region 23 data through memory dump
  14227. * mailbox command. When it successfully retrieves data, the size of the data
  14228. * will be returned, otherwise, 0 will be returned.
  14229. **/
  14230. static uint32_t
  14231. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14232. {
  14233. LPFC_MBOXQ_t *pmb = NULL;
  14234. MAILBOX_t *mb;
  14235. uint32_t offset = 0;
  14236. int rc;
  14237. if (!rgn23_data)
  14238. return 0;
  14239. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14240. if (!pmb) {
  14241. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14242. "2600 failed to allocate mailbox memory\n");
  14243. return 0;
  14244. }
  14245. mb = &pmb->u.mb;
  14246. do {
  14247. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14248. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14249. if (rc != MBX_SUCCESS) {
  14250. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14251. "2601 failed to read config "
  14252. "region 23, rc 0x%x Status 0x%x\n",
  14253. rc, mb->mbxStatus);
  14254. mb->un.varDmp.word_cnt = 0;
  14255. }
  14256. /*
  14257. * dump mem may return a zero when finished or we got a
  14258. * mailbox error, either way we are done.
  14259. */
  14260. if (mb->un.varDmp.word_cnt == 0)
  14261. break;
  14262. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14263. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14264. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14265. rgn23_data + offset,
  14266. mb->un.varDmp.word_cnt);
  14267. offset += mb->un.varDmp.word_cnt;
  14268. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14269. mempool_free(pmb, phba->mbox_mem_pool);
  14270. return offset;
  14271. }
  14272. /**
  14273. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14274. * @phba: pointer to lpfc hba data structure.
  14275. * @rgn23_data: pointer to configure region 23 data.
  14276. *
  14277. * This function gets SLI4 port configure region 23 data through memory dump
  14278. * mailbox command. When it successfully retrieves data, the size of the data
  14279. * will be returned, otherwise, 0 will be returned.
  14280. **/
  14281. static uint32_t
  14282. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14283. {
  14284. LPFC_MBOXQ_t *mboxq = NULL;
  14285. struct lpfc_dmabuf *mp = NULL;
  14286. struct lpfc_mqe *mqe;
  14287. uint32_t data_length = 0;
  14288. int rc;
  14289. if (!rgn23_data)
  14290. return 0;
  14291. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14292. if (!mboxq) {
  14293. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14294. "3105 failed to allocate mailbox memory\n");
  14295. return 0;
  14296. }
  14297. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14298. goto out;
  14299. mqe = &mboxq->u.mqe;
  14300. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14301. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14302. if (rc)
  14303. goto out;
  14304. data_length = mqe->un.mb_words[5];
  14305. if (data_length == 0)
  14306. goto out;
  14307. if (data_length > DMP_RGN23_SIZE) {
  14308. data_length = 0;
  14309. goto out;
  14310. }
  14311. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14312. out:
  14313. mempool_free(mboxq, phba->mbox_mem_pool);
  14314. if (mp) {
  14315. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14316. kfree(mp);
  14317. }
  14318. return data_length;
  14319. }
  14320. /**
  14321. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14322. * @phba: pointer to lpfc hba data structure.
  14323. *
  14324. * This function read region 23 and parse TLV for port status to
  14325. * decide if the user disaled the port. If the TLV indicates the
  14326. * port is disabled, the hba_flag is set accordingly.
  14327. **/
  14328. void
  14329. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14330. {
  14331. uint8_t *rgn23_data = NULL;
  14332. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14333. uint32_t offset = 0;
  14334. /* Get adapter Region 23 data */
  14335. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14336. if (!rgn23_data)
  14337. goto out;
  14338. if (phba->sli_rev < LPFC_SLI_REV4)
  14339. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14340. else {
  14341. if_type = bf_get(lpfc_sli_intf_if_type,
  14342. &phba->sli4_hba.sli_intf);
  14343. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14344. goto out;
  14345. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14346. }
  14347. if (!data_size)
  14348. goto out;
  14349. /* Check the region signature first */
  14350. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14351. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14352. "2619 Config region 23 has bad signature\n");
  14353. goto out;
  14354. }
  14355. offset += 4;
  14356. /* Check the data structure version */
  14357. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14358. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14359. "2620 Config region 23 has bad version\n");
  14360. goto out;
  14361. }
  14362. offset += 4;
  14363. /* Parse TLV entries in the region */
  14364. while (offset < data_size) {
  14365. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14366. break;
  14367. /*
  14368. * If the TLV is not driver specific TLV or driver id is
  14369. * not linux driver id, skip the record.
  14370. */
  14371. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14372. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14373. (rgn23_data[offset + 3] != 0)) {
  14374. offset += rgn23_data[offset + 1] * 4 + 4;
  14375. continue;
  14376. }
  14377. /* Driver found a driver specific TLV in the config region */
  14378. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14379. offset += 4;
  14380. tlv_offset = 0;
  14381. /*
  14382. * Search for configured port state sub-TLV.
  14383. */
  14384. while ((offset < data_size) &&
  14385. (tlv_offset < sub_tlv_len)) {
  14386. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14387. offset += 4;
  14388. tlv_offset += 4;
  14389. break;
  14390. }
  14391. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14392. offset += rgn23_data[offset + 1] * 4 + 4;
  14393. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14394. continue;
  14395. }
  14396. /* This HBA contains PORT_STE configured */
  14397. if (!rgn23_data[offset + 2])
  14398. phba->hba_flag |= LINK_DISABLED;
  14399. goto out;
  14400. }
  14401. }
  14402. out:
  14403. kfree(rgn23_data);
  14404. return;
  14405. }
  14406. /**
  14407. * lpfc_wr_object - write an object to the firmware
  14408. * @phba: HBA structure that indicates port to create a queue on.
  14409. * @dmabuf_list: list of dmabufs to write to the port.
  14410. * @size: the total byte value of the objects to write to the port.
  14411. * @offset: the current offset to be used to start the transfer.
  14412. *
  14413. * This routine will create a wr_object mailbox command to send to the port.
  14414. * the mailbox command will be constructed using the dma buffers described in
  14415. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14416. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14417. * used to indicate the starting offset of the transfer and will also return
  14418. * the offset after the write object mailbox has completed. @size is used to
  14419. * determine the end of the object and whether the eof bit should be set.
  14420. *
  14421. * Return 0 is successful and offset will contain the the new offset to use
  14422. * for the next write.
  14423. * Return negative value for error cases.
  14424. **/
  14425. int
  14426. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14427. uint32_t size, uint32_t *offset)
  14428. {
  14429. struct lpfc_mbx_wr_object *wr_object;
  14430. LPFC_MBOXQ_t *mbox;
  14431. int rc = 0, i = 0;
  14432. uint32_t shdr_status, shdr_add_status;
  14433. uint32_t mbox_tmo;
  14434. union lpfc_sli4_cfg_shdr *shdr;
  14435. struct lpfc_dmabuf *dmabuf;
  14436. uint32_t written = 0;
  14437. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14438. if (!mbox)
  14439. return -ENOMEM;
  14440. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14441. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14442. sizeof(struct lpfc_mbx_wr_object) -
  14443. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14444. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14445. wr_object->u.request.write_offset = *offset;
  14446. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14447. wr_object->u.request.object_name[0] =
  14448. cpu_to_le32(wr_object->u.request.object_name[0]);
  14449. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14450. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14451. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14452. break;
  14453. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14454. wr_object->u.request.bde[i].addrHigh =
  14455. putPaddrHigh(dmabuf->phys);
  14456. if (written + SLI4_PAGE_SIZE >= size) {
  14457. wr_object->u.request.bde[i].tus.f.bdeSize =
  14458. (size - written);
  14459. written += (size - written);
  14460. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14461. } else {
  14462. wr_object->u.request.bde[i].tus.f.bdeSize =
  14463. SLI4_PAGE_SIZE;
  14464. written += SLI4_PAGE_SIZE;
  14465. }
  14466. i++;
  14467. }
  14468. wr_object->u.request.bde_count = i;
  14469. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14470. if (!phba->sli4_hba.intr_enable)
  14471. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14472. else {
  14473. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14474. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14475. }
  14476. /* The IOCTL status is embedded in the mailbox subheader. */
  14477. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14478. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14479. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14480. if (rc != MBX_TIMEOUT)
  14481. mempool_free(mbox, phba->mbox_mem_pool);
  14482. if (shdr_status || shdr_add_status || rc) {
  14483. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14484. "3025 Write Object mailbox failed with "
  14485. "status x%x add_status x%x, mbx status x%x\n",
  14486. shdr_status, shdr_add_status, rc);
  14487. rc = -ENXIO;
  14488. } else
  14489. *offset += wr_object->u.response.actual_write_length;
  14490. return rc;
  14491. }
  14492. /**
  14493. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14494. * @vport: pointer to vport data structure.
  14495. *
  14496. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14497. * and REG_VPI mailbox commands associated with the vport. This function
  14498. * is called when driver want to restart discovery of the vport due to
  14499. * a Clear Virtual Link event.
  14500. **/
  14501. void
  14502. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14503. {
  14504. struct lpfc_hba *phba = vport->phba;
  14505. LPFC_MBOXQ_t *mb, *nextmb;
  14506. struct lpfc_dmabuf *mp;
  14507. struct lpfc_nodelist *ndlp;
  14508. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14509. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14510. LIST_HEAD(mbox_cmd_list);
  14511. uint8_t restart_loop;
  14512. /* Clean up internally queued mailbox commands with the vport */
  14513. spin_lock_irq(&phba->hbalock);
  14514. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14515. if (mb->vport != vport)
  14516. continue;
  14517. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14518. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14519. continue;
  14520. list_del(&mb->list);
  14521. list_add_tail(&mb->list, &mbox_cmd_list);
  14522. }
  14523. /* Clean up active mailbox command with the vport */
  14524. mb = phba->sli.mbox_active;
  14525. if (mb && (mb->vport == vport)) {
  14526. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14527. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14528. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14529. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14530. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14531. /* Put reference count for delayed processing */
  14532. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14533. /* Unregister the RPI when mailbox complete */
  14534. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14535. }
  14536. }
  14537. /* Cleanup any mailbox completions which are not yet processed */
  14538. do {
  14539. restart_loop = 0;
  14540. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14541. /*
  14542. * If this mailox is already processed or it is
  14543. * for another vport ignore it.
  14544. */
  14545. if ((mb->vport != vport) ||
  14546. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14547. continue;
  14548. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14549. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14550. continue;
  14551. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14552. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14553. ndlp = (struct lpfc_nodelist *)mb->context2;
  14554. /* Unregister the RPI when mailbox complete */
  14555. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14556. restart_loop = 1;
  14557. spin_unlock_irq(&phba->hbalock);
  14558. spin_lock(shost->host_lock);
  14559. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14560. spin_unlock(shost->host_lock);
  14561. spin_lock_irq(&phba->hbalock);
  14562. break;
  14563. }
  14564. }
  14565. } while (restart_loop);
  14566. spin_unlock_irq(&phba->hbalock);
  14567. /* Release the cleaned-up mailbox commands */
  14568. while (!list_empty(&mbox_cmd_list)) {
  14569. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14570. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14571. mp = (struct lpfc_dmabuf *) (mb->context1);
  14572. if (mp) {
  14573. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14574. kfree(mp);
  14575. }
  14576. ndlp = (struct lpfc_nodelist *) mb->context2;
  14577. mb->context2 = NULL;
  14578. if (ndlp) {
  14579. spin_lock(shost->host_lock);
  14580. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14581. spin_unlock(shost->host_lock);
  14582. lpfc_nlp_put(ndlp);
  14583. }
  14584. }
  14585. mempool_free(mb, phba->mbox_mem_pool);
  14586. }
  14587. /* Release the ndlp with the cleaned-up active mailbox command */
  14588. if (act_mbx_ndlp) {
  14589. spin_lock(shost->host_lock);
  14590. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14591. spin_unlock(shost->host_lock);
  14592. lpfc_nlp_put(act_mbx_ndlp);
  14593. }
  14594. }
  14595. /**
  14596. * lpfc_drain_txq - Drain the txq
  14597. * @phba: Pointer to HBA context object.
  14598. *
  14599. * This function attempt to submit IOCBs on the txq
  14600. * to the adapter. For SLI4 adapters, the txq contains
  14601. * ELS IOCBs that have been deferred because the there
  14602. * are no SGLs. This congestion can occur with large
  14603. * vport counts during node discovery.
  14604. **/
  14605. uint32_t
  14606. lpfc_drain_txq(struct lpfc_hba *phba)
  14607. {
  14608. LIST_HEAD(completions);
  14609. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14610. struct lpfc_iocbq *piocbq = 0;
  14611. unsigned long iflags = 0;
  14612. char *fail_msg = NULL;
  14613. struct lpfc_sglq *sglq;
  14614. union lpfc_wqe wqe;
  14615. spin_lock_irqsave(&phba->hbalock, iflags);
  14616. if (pring->txq_cnt > pring->txq_max)
  14617. pring->txq_max = pring->txq_cnt;
  14618. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14619. while (pring->txq_cnt) {
  14620. spin_lock_irqsave(&phba->hbalock, iflags);
  14621. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14622. if (!piocbq) {
  14623. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14624. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14625. "2823 txq empty and txq_cnt is %d\n ",
  14626. pring->txq_cnt);
  14627. break;
  14628. }
  14629. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14630. if (!sglq) {
  14631. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14632. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14633. break;
  14634. }
  14635. /* The xri and iocb resources secured,
  14636. * attempt to issue request
  14637. */
  14638. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14639. piocbq->sli4_xritag = sglq->sli4_xritag;
  14640. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14641. fail_msg = "to convert bpl to sgl";
  14642. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14643. fail_msg = "to convert iocb to wqe";
  14644. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14645. fail_msg = " - Wq is full";
  14646. else
  14647. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14648. if (fail_msg) {
  14649. /* Failed means we can't issue and need to cancel */
  14650. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14651. "2822 IOCB failed %s iotag 0x%x "
  14652. "xri 0x%x\n",
  14653. fail_msg,
  14654. piocbq->iotag, piocbq->sli4_xritag);
  14655. list_add_tail(&piocbq->list, &completions);
  14656. }
  14657. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14658. }
  14659. /* Cancel all the IOCBs that cannot be issued */
  14660. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14661. IOERR_SLI_ABORTED);
  14662. return pring->txq_cnt;
  14663. }