lpfc_sli.c 521 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2015 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 void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
  67. uint32_t);
  68. static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
  69. static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
  70. static IOCB_t *
  71. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  72. {
  73. return &iocbq->iocb;
  74. }
  75. /**
  76. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  77. * @q: The Work Queue to operate on.
  78. * @wqe: The work Queue Entry to put on the Work queue.
  79. *
  80. * This routine will copy the contents of @wqe to the next available entry on
  81. * the @q. This function will then ring the Work Queue Doorbell to signal the
  82. * HBA to start processing the Work Queue Entry. This function returns 0 if
  83. * successful. If no entries are available on @q then this function will return
  84. * -ENOMEM.
  85. * The caller is expected to hold the hbalock when calling this routine.
  86. **/
  87. static uint32_t
  88. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  89. {
  90. union lpfc_wqe *temp_wqe;
  91. struct lpfc_register doorbell;
  92. uint32_t host_index;
  93. uint32_t idx;
  94. /* sanity check on queue memory */
  95. if (unlikely(!q))
  96. return -ENOMEM;
  97. temp_wqe = q->qe[q->host_index].wqe;
  98. /* If the host has not yet processed the next entry then we are done */
  99. idx = ((q->host_index + 1) % q->entry_count);
  100. if (idx == q->hba_index) {
  101. q->WQ_overflow++;
  102. return -ENOMEM;
  103. }
  104. q->WQ_posted++;
  105. /* set consumption flag every once in a while */
  106. if (!((q->host_index + 1) % q->entry_repost))
  107. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  108. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  109. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  110. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  111. /* Update the host index before invoking device */
  112. host_index = q->host_index;
  113. q->host_index = idx;
  114. /* Ring Doorbell */
  115. doorbell.word0 = 0;
  116. if (q->db_format == LPFC_DB_LIST_FORMAT) {
  117. bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
  118. bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
  119. bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
  120. } else if (q->db_format == LPFC_DB_RING_FORMAT) {
  121. bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
  122. bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
  123. } else {
  124. return -EINVAL;
  125. }
  126. writel(doorbell.word0, q->db_regaddr);
  127. return 0;
  128. }
  129. /**
  130. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  131. * @q: The Work Queue to operate on.
  132. * @index: The index to advance the hba index to.
  133. *
  134. * This routine will update the HBA index of a queue to reflect consumption of
  135. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  136. * an entry the host calls this function to update the queue's internal
  137. * pointers. This routine returns the number of entries that were consumed by
  138. * the HBA.
  139. **/
  140. static uint32_t
  141. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  142. {
  143. uint32_t released = 0;
  144. /* sanity check on queue memory */
  145. if (unlikely(!q))
  146. return 0;
  147. if (q->hba_index == index)
  148. return 0;
  149. do {
  150. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  151. released++;
  152. } while (q->hba_index != index);
  153. return released;
  154. }
  155. /**
  156. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  157. * @q: The Mailbox Queue to operate on.
  158. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  159. *
  160. * This routine will copy the contents of @mqe to the next available entry on
  161. * the @q. This function will then ring the Work Queue Doorbell to signal the
  162. * HBA to start processing the Work Queue Entry. This function returns 0 if
  163. * successful. If no entries are available on @q then this function will return
  164. * -ENOMEM.
  165. * The caller is expected to hold the hbalock when calling this routine.
  166. **/
  167. static uint32_t
  168. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  169. {
  170. struct lpfc_mqe *temp_mqe;
  171. struct lpfc_register doorbell;
  172. /* sanity check on queue memory */
  173. if (unlikely(!q))
  174. return -ENOMEM;
  175. temp_mqe = q->qe[q->host_index].mqe;
  176. /* If the host has not yet processed the next entry then we are done */
  177. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  178. return -ENOMEM;
  179. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  180. /* Save off the mailbox pointer for completion */
  181. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  182. /* Update the host index before invoking device */
  183. q->host_index = ((q->host_index + 1) % q->entry_count);
  184. /* Ring Doorbell */
  185. doorbell.word0 = 0;
  186. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  187. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  188. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  189. return 0;
  190. }
  191. /**
  192. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  193. * @q: The Mailbox Queue to operate on.
  194. *
  195. * This routine will update the HBA index of a queue to reflect consumption of
  196. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  197. * an entry the host calls this function to update the queue's internal
  198. * pointers. This routine returns the number of entries that were consumed by
  199. * the HBA.
  200. **/
  201. static uint32_t
  202. lpfc_sli4_mq_release(struct lpfc_queue *q)
  203. {
  204. /* sanity check on queue memory */
  205. if (unlikely(!q))
  206. return 0;
  207. /* Clear the mailbox pointer for completion */
  208. q->phba->mbox = NULL;
  209. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  210. return 1;
  211. }
  212. /**
  213. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  214. * @q: The Event Queue to get the first valid EQE from
  215. *
  216. * This routine will get the first valid Event Queue Entry from @q, update
  217. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  218. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  219. * processed, but not popped back to the HBA then this routine will return NULL.
  220. **/
  221. static struct lpfc_eqe *
  222. lpfc_sli4_eq_get(struct lpfc_queue *q)
  223. {
  224. struct lpfc_eqe *eqe;
  225. uint32_t idx;
  226. /* sanity check on queue memory */
  227. if (unlikely(!q))
  228. return NULL;
  229. eqe = q->qe[q->hba_index].eqe;
  230. /* If the next EQE is not valid then we are done */
  231. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  232. return NULL;
  233. /* If the host has not yet processed the next entry then we are done */
  234. idx = ((q->hba_index + 1) % q->entry_count);
  235. if (idx == q->host_index)
  236. return NULL;
  237. q->hba_index = idx;
  238. /*
  239. * insert barrier for instruction interlock : data from the hardware
  240. * must have the valid bit checked before it can be copied and acted
  241. * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
  242. * instructions allowing action on content before valid bit checked,
  243. * add barrier here as well. May not be needed as "content" is a
  244. * single 32-bit entity here (vs multi word structure for cq's).
  245. */
  246. mb();
  247. return eqe;
  248. }
  249. /**
  250. * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
  251. * @q: The Event Queue to disable interrupts
  252. *
  253. **/
  254. static inline void
  255. lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
  256. {
  257. struct lpfc_register doorbell;
  258. doorbell.word0 = 0;
  259. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  260. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  261. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  262. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  263. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  264. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  265. }
  266. /**
  267. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  268. * @q: The Event Queue that the host has completed processing for.
  269. * @arm: Indicates whether the host wants to arms this CQ.
  270. *
  271. * This routine will mark all Event Queue Entries on @q, from the last
  272. * known completed entry to the last entry that was processed, as completed
  273. * by clearing the valid bit for each completion queue entry. Then it will
  274. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  275. * The internal host index in the @q will be updated by this routine to indicate
  276. * that the host has finished processing the entries. The @arm parameter
  277. * indicates that the queue should be rearmed when ringing the doorbell.
  278. *
  279. * This function will return the number of EQEs that were popped.
  280. **/
  281. uint32_t
  282. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  283. {
  284. uint32_t released = 0;
  285. struct lpfc_eqe *temp_eqe;
  286. struct lpfc_register doorbell;
  287. /* sanity check on queue memory */
  288. if (unlikely(!q))
  289. return 0;
  290. /* while there are valid entries */
  291. while (q->hba_index != q->host_index) {
  292. temp_eqe = q->qe[q->host_index].eqe;
  293. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  294. released++;
  295. q->host_index = ((q->host_index + 1) % q->entry_count);
  296. }
  297. if (unlikely(released == 0 && !arm))
  298. return 0;
  299. /* ring doorbell for number popped */
  300. doorbell.word0 = 0;
  301. if (arm) {
  302. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  303. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  304. }
  305. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  306. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  307. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  308. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  309. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  310. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  311. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  312. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  313. readl(q->phba->sli4_hba.EQCQDBregaddr);
  314. return released;
  315. }
  316. /**
  317. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  318. * @q: The Completion Queue to get the first valid CQE from
  319. *
  320. * This routine will get the first valid Completion Queue Entry from @q, update
  321. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  322. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  323. * processed, but not popped back to the HBA then this routine will return NULL.
  324. **/
  325. static struct lpfc_cqe *
  326. lpfc_sli4_cq_get(struct lpfc_queue *q)
  327. {
  328. struct lpfc_cqe *cqe;
  329. uint32_t idx;
  330. /* sanity check on queue memory */
  331. if (unlikely(!q))
  332. return NULL;
  333. /* If the next CQE is not valid then we are done */
  334. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  335. return NULL;
  336. /* If the host has not yet processed the next entry then we are done */
  337. idx = ((q->hba_index + 1) % q->entry_count);
  338. if (idx == q->host_index)
  339. return NULL;
  340. cqe = q->qe[q->hba_index].cqe;
  341. q->hba_index = idx;
  342. /*
  343. * insert barrier for instruction interlock : data from the hardware
  344. * must have the valid bit checked before it can be copied and acted
  345. * upon. Speculative instructions were allowing a bcopy at the start
  346. * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
  347. * after our return, to copy data before the valid bit check above
  348. * was done. As such, some of the copied data was stale. The barrier
  349. * ensures the check is before any data is copied.
  350. */
  351. mb();
  352. return cqe;
  353. }
  354. /**
  355. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  356. * @q: The Completion Queue that the host has completed processing for.
  357. * @arm: Indicates whether the host wants to arms this CQ.
  358. *
  359. * This routine will mark all Completion queue entries on @q, from the last
  360. * known completed entry to the last entry that was processed, as completed
  361. * by clearing the valid bit for each completion queue entry. Then it will
  362. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  363. * The internal host index in the @q will be updated by this routine to indicate
  364. * that the host has finished processing the entries. The @arm parameter
  365. * indicates that the queue should be rearmed when ringing the doorbell.
  366. *
  367. * This function will return the number of CQEs that were released.
  368. **/
  369. uint32_t
  370. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  371. {
  372. uint32_t released = 0;
  373. struct lpfc_cqe *temp_qe;
  374. struct lpfc_register doorbell;
  375. /* sanity check on queue memory */
  376. if (unlikely(!q))
  377. return 0;
  378. /* while there are valid entries */
  379. while (q->hba_index != q->host_index) {
  380. temp_qe = q->qe[q->host_index].cqe;
  381. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  382. released++;
  383. q->host_index = ((q->host_index + 1) % q->entry_count);
  384. }
  385. if (unlikely(released == 0 && !arm))
  386. return 0;
  387. /* ring doorbell for number popped */
  388. doorbell.word0 = 0;
  389. if (arm)
  390. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  391. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  392. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  393. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  394. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  395. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  396. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  397. return released;
  398. }
  399. /**
  400. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  401. * @q: The Header Receive Queue to operate on.
  402. * @wqe: The Receive Queue Entry to put on the Receive queue.
  403. *
  404. * This routine will copy the contents of @wqe to the next available entry on
  405. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  406. * HBA to start processing the Receive Queue Entry. This function returns the
  407. * index that the rqe was copied to if successful. If no entries are available
  408. * on @q then this function will return -ENOMEM.
  409. * The caller is expected to hold the hbalock when calling this routine.
  410. **/
  411. static int
  412. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  413. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  414. {
  415. struct lpfc_rqe *temp_hrqe;
  416. struct lpfc_rqe *temp_drqe;
  417. struct lpfc_register doorbell;
  418. int put_index;
  419. /* sanity check on queue memory */
  420. if (unlikely(!hq) || unlikely(!dq))
  421. return -ENOMEM;
  422. put_index = hq->host_index;
  423. temp_hrqe = hq->qe[hq->host_index].rqe;
  424. temp_drqe = dq->qe[dq->host_index].rqe;
  425. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  426. return -EINVAL;
  427. if (hq->host_index != dq->host_index)
  428. return -EINVAL;
  429. /* If the host has not yet processed the next entry then we are done */
  430. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  431. return -EBUSY;
  432. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  433. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  434. /* Update the host index to point to the next slot */
  435. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  436. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  437. /* Ring The Header Receive Queue Doorbell */
  438. if (!(hq->host_index % hq->entry_repost)) {
  439. doorbell.word0 = 0;
  440. if (hq->db_format == LPFC_DB_RING_FORMAT) {
  441. bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
  442. hq->entry_repost);
  443. bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
  444. } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
  445. bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
  446. hq->entry_repost);
  447. bf_set(lpfc_rq_db_list_fm_index, &doorbell,
  448. hq->host_index);
  449. bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
  450. } else {
  451. return -EINVAL;
  452. }
  453. writel(doorbell.word0, hq->db_regaddr);
  454. }
  455. return put_index;
  456. }
  457. /**
  458. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  459. * @q: The Header Receive Queue to operate on.
  460. *
  461. * This routine will update the HBA index of a queue to reflect consumption of
  462. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  463. * consumed an entry the host calls this function to update the queue's
  464. * internal pointers. This routine returns the number of entries that were
  465. * consumed by the HBA.
  466. **/
  467. static uint32_t
  468. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  469. {
  470. /* sanity check on queue memory */
  471. if (unlikely(!hq) || unlikely(!dq))
  472. return 0;
  473. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  474. return 0;
  475. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  476. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  477. return 1;
  478. }
  479. /**
  480. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  481. * @phba: Pointer to HBA context object.
  482. * @pring: Pointer to driver SLI ring object.
  483. *
  484. * This function returns pointer to next command iocb entry
  485. * in the command ring. The caller must hold hbalock to prevent
  486. * other threads consume the next command iocb.
  487. * SLI-2/SLI-3 provide different sized iocbs.
  488. **/
  489. static inline IOCB_t *
  490. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  491. {
  492. return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
  493. pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
  494. }
  495. /**
  496. * lpfc_resp_iocb - Get next response iocb entry in the ring
  497. * @phba: Pointer to HBA context object.
  498. * @pring: Pointer to driver SLI ring object.
  499. *
  500. * This function returns pointer to next response iocb entry
  501. * in the response ring. The caller must hold hbalock to make sure
  502. * that no other thread consume the next response iocb.
  503. * SLI-2/SLI-3 provide different sized iocbs.
  504. **/
  505. static inline IOCB_t *
  506. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  507. {
  508. return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
  509. pring->sli.sli3.rspidx * phba->iocb_rsp_size);
  510. }
  511. /**
  512. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  513. * @phba: Pointer to HBA context object.
  514. *
  515. * This function is called with hbalock held. This function
  516. * allocates a new driver iocb object from the iocb pool. If the
  517. * allocation is successful, it returns pointer to the newly
  518. * allocated iocb object else it returns NULL.
  519. **/
  520. struct lpfc_iocbq *
  521. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  522. {
  523. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  524. struct lpfc_iocbq * iocbq = NULL;
  525. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  526. if (iocbq)
  527. phba->iocb_cnt++;
  528. if (phba->iocb_cnt > phba->iocb_max)
  529. phba->iocb_max = phba->iocb_cnt;
  530. return iocbq;
  531. }
  532. /**
  533. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  534. * @phba: Pointer to HBA context object.
  535. * @xritag: XRI value.
  536. *
  537. * This function clears the sglq pointer from the array of acive
  538. * sglq's. The xritag that is passed in is used to index into the
  539. * array. Before the xritag can be used it needs to be adjusted
  540. * by subtracting the xribase.
  541. *
  542. * Returns sglq ponter = success, NULL = Failure.
  543. **/
  544. static struct lpfc_sglq *
  545. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  546. {
  547. struct lpfc_sglq *sglq;
  548. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  549. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  550. return sglq;
  551. }
  552. /**
  553. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  554. * @phba: Pointer to HBA context object.
  555. * @xritag: XRI value.
  556. *
  557. * This function returns the sglq pointer from the array of acive
  558. * sglq's. The xritag that is passed in is used to index into the
  559. * array. Before the xritag can be used it needs to be adjusted
  560. * by subtracting the xribase.
  561. *
  562. * Returns sglq ponter = success, NULL = Failure.
  563. **/
  564. struct lpfc_sglq *
  565. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  566. {
  567. struct lpfc_sglq *sglq;
  568. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  569. return sglq;
  570. }
  571. /**
  572. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  573. * @phba: Pointer to HBA context object.
  574. * @xritag: xri used in this exchange.
  575. * @rrq: The RRQ to be cleared.
  576. *
  577. **/
  578. void
  579. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  580. uint16_t xritag,
  581. struct lpfc_node_rrq *rrq)
  582. {
  583. struct lpfc_nodelist *ndlp = NULL;
  584. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  585. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  586. /* The target DID could have been swapped (cable swap)
  587. * we should use the ndlp from the findnode if it is
  588. * available.
  589. */
  590. if ((!ndlp) && rrq->ndlp)
  591. ndlp = rrq->ndlp;
  592. if (!ndlp)
  593. goto out;
  594. if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
  595. rrq->send_rrq = 0;
  596. rrq->xritag = 0;
  597. rrq->rrq_stop_time = 0;
  598. }
  599. out:
  600. mempool_free(rrq, phba->rrq_pool);
  601. }
  602. /**
  603. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  604. * @phba: Pointer to HBA context object.
  605. *
  606. * This function is called with hbalock held. This function
  607. * Checks if stop_time (ratov from setting rrq active) has
  608. * been reached, if it has and the send_rrq flag is set then
  609. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  610. * then it will just call the routine to clear the rrq and
  611. * free the rrq resource.
  612. * The timer is set to the next rrq that is going to expire before
  613. * leaving the routine.
  614. *
  615. **/
  616. void
  617. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  618. {
  619. struct lpfc_node_rrq *rrq;
  620. struct lpfc_node_rrq *nextrrq;
  621. unsigned long next_time;
  622. unsigned long iflags;
  623. LIST_HEAD(send_rrq);
  624. spin_lock_irqsave(&phba->hbalock, iflags);
  625. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  626. next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
  627. list_for_each_entry_safe(rrq, nextrrq,
  628. &phba->active_rrq_list, list) {
  629. if (time_after(jiffies, rrq->rrq_stop_time))
  630. list_move(&rrq->list, &send_rrq);
  631. else if (time_before(rrq->rrq_stop_time, next_time))
  632. next_time = rrq->rrq_stop_time;
  633. }
  634. spin_unlock_irqrestore(&phba->hbalock, iflags);
  635. if ((!list_empty(&phba->active_rrq_list)) &&
  636. (!(phba->pport->load_flag & FC_UNLOADING)))
  637. mod_timer(&phba->rrq_tmr, next_time);
  638. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  639. list_del(&rrq->list);
  640. if (!rrq->send_rrq)
  641. /* this call will free the rrq */
  642. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  643. else if (lpfc_send_rrq(phba, rrq)) {
  644. /* if we send the rrq then the completion handler
  645. * will clear the bit in the xribitmap.
  646. */
  647. lpfc_clr_rrq_active(phba, rrq->xritag,
  648. rrq);
  649. }
  650. }
  651. }
  652. /**
  653. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  654. * @vport: Pointer to vport context object.
  655. * @xri: The xri used in the exchange.
  656. * @did: The targets DID for this exchange.
  657. *
  658. * returns NULL = rrq not found in the phba->active_rrq_list.
  659. * rrq = rrq for this xri and target.
  660. **/
  661. struct lpfc_node_rrq *
  662. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  663. {
  664. struct lpfc_hba *phba = vport->phba;
  665. struct lpfc_node_rrq *rrq;
  666. struct lpfc_node_rrq *nextrrq;
  667. unsigned long iflags;
  668. if (phba->sli_rev != LPFC_SLI_REV4)
  669. return NULL;
  670. spin_lock_irqsave(&phba->hbalock, iflags);
  671. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  672. if (rrq->vport == vport && rrq->xritag == xri &&
  673. rrq->nlp_DID == did){
  674. list_del(&rrq->list);
  675. spin_unlock_irqrestore(&phba->hbalock, iflags);
  676. return rrq;
  677. }
  678. }
  679. spin_unlock_irqrestore(&phba->hbalock, iflags);
  680. return NULL;
  681. }
  682. /**
  683. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  684. * @vport: Pointer to vport context object.
  685. * @ndlp: Pointer to the lpfc_node_list structure.
  686. * If ndlp is NULL Remove all active RRQs for this vport from the
  687. * phba->active_rrq_list and clear the rrq.
  688. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  689. **/
  690. void
  691. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  692. {
  693. struct lpfc_hba *phba = vport->phba;
  694. struct lpfc_node_rrq *rrq;
  695. struct lpfc_node_rrq *nextrrq;
  696. unsigned long iflags;
  697. LIST_HEAD(rrq_list);
  698. if (phba->sli_rev != LPFC_SLI_REV4)
  699. return;
  700. if (!ndlp) {
  701. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  702. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  703. }
  704. spin_lock_irqsave(&phba->hbalock, iflags);
  705. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  706. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  707. list_move(&rrq->list, &rrq_list);
  708. spin_unlock_irqrestore(&phba->hbalock, iflags);
  709. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  710. list_del(&rrq->list);
  711. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  712. }
  713. }
  714. /**
  715. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  716. * @phba: Pointer to HBA context object.
  717. * @ndlp: Targets nodelist pointer for this exchange.
  718. * @xritag the xri in the bitmap to test.
  719. *
  720. * This function is called with hbalock held. This function
  721. * returns 0 = rrq not active for this xri
  722. * 1 = rrq is valid for this xri.
  723. **/
  724. int
  725. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  726. uint16_t xritag)
  727. {
  728. if (!ndlp)
  729. return 0;
  730. if (!ndlp->active_rrqs_xri_bitmap)
  731. return 0;
  732. if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
  733. return 1;
  734. else
  735. return 0;
  736. }
  737. /**
  738. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  739. * @phba: Pointer to HBA context object.
  740. * @ndlp: nodelist pointer for this target.
  741. * @xritag: xri used in this exchange.
  742. * @rxid: Remote Exchange ID.
  743. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  744. *
  745. * This function takes the hbalock.
  746. * The active bit is always set in the active rrq xri_bitmap even
  747. * if there is no slot avaiable for the other rrq information.
  748. *
  749. * returns 0 rrq actived for this xri
  750. * < 0 No memory or invalid ndlp.
  751. **/
  752. int
  753. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  754. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  755. {
  756. unsigned long iflags;
  757. struct lpfc_node_rrq *rrq;
  758. int empty;
  759. if (!ndlp)
  760. return -EINVAL;
  761. if (!phba->cfg_enable_rrq)
  762. return -EINVAL;
  763. spin_lock_irqsave(&phba->hbalock, iflags);
  764. if (phba->pport->load_flag & FC_UNLOADING) {
  765. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  766. goto out;
  767. }
  768. /*
  769. * set the active bit even if there is no mem available.
  770. */
  771. if (NLP_CHK_FREE_REQ(ndlp))
  772. goto out;
  773. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  774. goto out;
  775. if (!ndlp->active_rrqs_xri_bitmap)
  776. goto out;
  777. if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
  778. goto out;
  779. spin_unlock_irqrestore(&phba->hbalock, iflags);
  780. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  781. if (!rrq) {
  782. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  783. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  784. " DID:0x%x Send:%d\n",
  785. xritag, rxid, ndlp->nlp_DID, send_rrq);
  786. return -EINVAL;
  787. }
  788. if (phba->cfg_enable_rrq == 1)
  789. rrq->send_rrq = send_rrq;
  790. else
  791. rrq->send_rrq = 0;
  792. rrq->xritag = xritag;
  793. rrq->rrq_stop_time = jiffies +
  794. msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
  795. rrq->ndlp = ndlp;
  796. rrq->nlp_DID = ndlp->nlp_DID;
  797. rrq->vport = ndlp->vport;
  798. rrq->rxid = rxid;
  799. spin_lock_irqsave(&phba->hbalock, iflags);
  800. empty = list_empty(&phba->active_rrq_list);
  801. list_add_tail(&rrq->list, &phba->active_rrq_list);
  802. phba->hba_flag |= HBA_RRQ_ACTIVE;
  803. if (empty)
  804. lpfc_worker_wake_up(phba);
  805. spin_unlock_irqrestore(&phba->hbalock, iflags);
  806. return 0;
  807. out:
  808. spin_unlock_irqrestore(&phba->hbalock, iflags);
  809. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  810. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  811. " DID:0x%x Send:%d\n",
  812. xritag, rxid, ndlp->nlp_DID, send_rrq);
  813. return -EINVAL;
  814. }
  815. /**
  816. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  817. * @phba: Pointer to HBA context object.
  818. * @piocb: Pointer to the iocbq.
  819. *
  820. * This function is called with the ring lock held. This function
  821. * gets a new driver sglq object from the sglq list. If the
  822. * list is not empty then it is successful, it returns pointer to the newly
  823. * allocated sglq object else it returns NULL.
  824. **/
  825. static struct lpfc_sglq *
  826. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  827. {
  828. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  829. struct lpfc_sglq *sglq = NULL;
  830. struct lpfc_sglq *start_sglq = NULL;
  831. struct lpfc_scsi_buf *lpfc_cmd;
  832. struct lpfc_nodelist *ndlp;
  833. int found = 0;
  834. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  835. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  836. ndlp = lpfc_cmd->rdata->pnode;
  837. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  838. !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  839. ndlp = piocbq->context_un.ndlp;
  840. } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
  841. if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
  842. ndlp = NULL;
  843. else
  844. ndlp = piocbq->context_un.ndlp;
  845. } else {
  846. ndlp = piocbq->context1;
  847. }
  848. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  849. start_sglq = sglq;
  850. while (!found) {
  851. if (!sglq)
  852. return NULL;
  853. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
  854. /* This xri has an rrq outstanding for this DID.
  855. * put it back in the list and get another xri.
  856. */
  857. list_add_tail(&sglq->list, lpfc_sgl_list);
  858. sglq = NULL;
  859. list_remove_head(lpfc_sgl_list, sglq,
  860. struct lpfc_sglq, list);
  861. if (sglq == start_sglq) {
  862. sglq = NULL;
  863. break;
  864. } else
  865. continue;
  866. }
  867. sglq->ndlp = ndlp;
  868. found = 1;
  869. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  870. sglq->state = SGL_ALLOCATED;
  871. }
  872. return sglq;
  873. }
  874. /**
  875. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  876. * @phba: Pointer to HBA context object.
  877. *
  878. * This function is called with no lock held. This function
  879. * allocates a new driver iocb object from the iocb pool. If the
  880. * allocation is successful, it returns pointer to the newly
  881. * allocated iocb object else it returns NULL.
  882. **/
  883. struct lpfc_iocbq *
  884. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  885. {
  886. struct lpfc_iocbq * iocbq = NULL;
  887. unsigned long iflags;
  888. spin_lock_irqsave(&phba->hbalock, iflags);
  889. iocbq = __lpfc_sli_get_iocbq(phba);
  890. spin_unlock_irqrestore(&phba->hbalock, iflags);
  891. return iocbq;
  892. }
  893. /**
  894. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  895. * @phba: Pointer to HBA context object.
  896. * @iocbq: Pointer to driver iocb object.
  897. *
  898. * This function is called with hbalock held to release driver
  899. * iocb object to the iocb pool. The iotag in the iocb object
  900. * does not change for each use of the iocb object. This function
  901. * clears all other fields of the iocb object when it is freed.
  902. * The sqlq structure that holds the xritag and phys and virtual
  903. * mappings for the scatter gather list is retrieved from the
  904. * active array of sglq. The get of the sglq pointer also clears
  905. * the entry in the array. If the status of the IO indiactes that
  906. * this IO was aborted then the sglq entry it put on the
  907. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  908. * IO has good status or fails for any other reason then the sglq
  909. * entry is added to the free list (lpfc_sgl_list).
  910. **/
  911. static void
  912. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  913. {
  914. struct lpfc_sglq *sglq;
  915. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  916. unsigned long iflag = 0;
  917. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  918. if (iocbq->sli4_xritag == NO_XRI)
  919. sglq = NULL;
  920. else
  921. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  922. if (sglq) {
  923. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  924. (sglq->state != SGL_XRI_ABORTED)) {
  925. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  926. iflag);
  927. list_add(&sglq->list,
  928. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  929. spin_unlock_irqrestore(
  930. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  931. } else {
  932. spin_lock_irqsave(&pring->ring_lock, iflag);
  933. sglq->state = SGL_FREED;
  934. sglq->ndlp = NULL;
  935. list_add_tail(&sglq->list,
  936. &phba->sli4_hba.lpfc_sgl_list);
  937. spin_unlock_irqrestore(&pring->ring_lock, iflag);
  938. /* Check if TXQ queue needs to be serviced */
  939. if (!list_empty(&pring->txq))
  940. lpfc_worker_wake_up(phba);
  941. }
  942. }
  943. /*
  944. * Clean all volatile data fields, preserve iotag and node struct.
  945. */
  946. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  947. iocbq->sli4_lxritag = NO_XRI;
  948. iocbq->sli4_xritag = NO_XRI;
  949. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  950. }
  951. /**
  952. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  953. * @phba: Pointer to HBA context object.
  954. * @iocbq: Pointer to driver iocb object.
  955. *
  956. * This function is called with hbalock held to release driver
  957. * iocb object to the iocb pool. The iotag in the iocb object
  958. * does not change for each use of the iocb object. This function
  959. * clears all other fields of the iocb object when it is freed.
  960. **/
  961. static void
  962. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  963. {
  964. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  965. /*
  966. * Clean all volatile data fields, preserve iotag and node struct.
  967. */
  968. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  969. iocbq->sli4_xritag = NO_XRI;
  970. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  971. }
  972. /**
  973. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  974. * @phba: Pointer to HBA context object.
  975. * @iocbq: Pointer to driver iocb object.
  976. *
  977. * This function is called with hbalock held to release driver
  978. * iocb object to the iocb pool. The iotag in the iocb object
  979. * does not change for each use of the iocb object. This function
  980. * clears all other fields of the iocb object when it is freed.
  981. **/
  982. static void
  983. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  984. {
  985. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  986. phba->iocb_cnt--;
  987. }
  988. /**
  989. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  990. * @phba: Pointer to HBA context object.
  991. * @iocbq: Pointer to driver iocb object.
  992. *
  993. * This function is called with no lock held to release the iocb to
  994. * iocb pool.
  995. **/
  996. void
  997. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  998. {
  999. unsigned long iflags;
  1000. /*
  1001. * Clean all volatile data fields, preserve iotag and node struct.
  1002. */
  1003. spin_lock_irqsave(&phba->hbalock, iflags);
  1004. __lpfc_sli_release_iocbq(phba, iocbq);
  1005. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1006. }
  1007. /**
  1008. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  1009. * @phba: Pointer to HBA context object.
  1010. * @iocblist: List of IOCBs.
  1011. * @ulpstatus: ULP status in IOCB command field.
  1012. * @ulpWord4: ULP word-4 in IOCB command field.
  1013. *
  1014. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  1015. * on the list by invoking the complete callback function associated with the
  1016. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  1017. * fields.
  1018. **/
  1019. void
  1020. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  1021. uint32_t ulpstatus, uint32_t ulpWord4)
  1022. {
  1023. struct lpfc_iocbq *piocb;
  1024. while (!list_empty(iocblist)) {
  1025. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  1026. if (!piocb->iocb_cmpl)
  1027. lpfc_sli_release_iocbq(phba, piocb);
  1028. else {
  1029. piocb->iocb.ulpStatus = ulpstatus;
  1030. piocb->iocb.un.ulpWord[4] = ulpWord4;
  1031. (piocb->iocb_cmpl) (phba, piocb, piocb);
  1032. }
  1033. }
  1034. return;
  1035. }
  1036. /**
  1037. * lpfc_sli_iocb_cmd_type - Get the iocb type
  1038. * @iocb_cmnd: iocb command code.
  1039. *
  1040. * This function is called by ring event handler function to get the iocb type.
  1041. * This function translates the iocb command to an iocb command type used to
  1042. * decide the final disposition of each completed IOCB.
  1043. * The function returns
  1044. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  1045. * LPFC_SOL_IOCB if it is a solicited iocb completion
  1046. * LPFC_ABORT_IOCB if it is an abort iocb
  1047. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  1048. *
  1049. * The caller is not required to hold any lock.
  1050. **/
  1051. static lpfc_iocb_type
  1052. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1053. {
  1054. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1055. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1056. return 0;
  1057. switch (iocb_cmnd) {
  1058. case CMD_XMIT_SEQUENCE_CR:
  1059. case CMD_XMIT_SEQUENCE_CX:
  1060. case CMD_XMIT_BCAST_CN:
  1061. case CMD_XMIT_BCAST_CX:
  1062. case CMD_ELS_REQUEST_CR:
  1063. case CMD_ELS_REQUEST_CX:
  1064. case CMD_CREATE_XRI_CR:
  1065. case CMD_CREATE_XRI_CX:
  1066. case CMD_GET_RPI_CN:
  1067. case CMD_XMIT_ELS_RSP_CX:
  1068. case CMD_GET_RPI_CR:
  1069. case CMD_FCP_IWRITE_CR:
  1070. case CMD_FCP_IWRITE_CX:
  1071. case CMD_FCP_IREAD_CR:
  1072. case CMD_FCP_IREAD_CX:
  1073. case CMD_FCP_ICMND_CR:
  1074. case CMD_FCP_ICMND_CX:
  1075. case CMD_FCP_TSEND_CX:
  1076. case CMD_FCP_TRSP_CX:
  1077. case CMD_FCP_TRECEIVE_CX:
  1078. case CMD_FCP_AUTO_TRSP_CX:
  1079. case CMD_ADAPTER_MSG:
  1080. case CMD_ADAPTER_DUMP:
  1081. case CMD_XMIT_SEQUENCE64_CR:
  1082. case CMD_XMIT_SEQUENCE64_CX:
  1083. case CMD_XMIT_BCAST64_CN:
  1084. case CMD_XMIT_BCAST64_CX:
  1085. case CMD_ELS_REQUEST64_CR:
  1086. case CMD_ELS_REQUEST64_CX:
  1087. case CMD_FCP_IWRITE64_CR:
  1088. case CMD_FCP_IWRITE64_CX:
  1089. case CMD_FCP_IREAD64_CR:
  1090. case CMD_FCP_IREAD64_CX:
  1091. case CMD_FCP_ICMND64_CR:
  1092. case CMD_FCP_ICMND64_CX:
  1093. case CMD_FCP_TSEND64_CX:
  1094. case CMD_FCP_TRSP64_CX:
  1095. case CMD_FCP_TRECEIVE64_CX:
  1096. case CMD_GEN_REQUEST64_CR:
  1097. case CMD_GEN_REQUEST64_CX:
  1098. case CMD_XMIT_ELS_RSP64_CX:
  1099. case DSSCMD_IWRITE64_CR:
  1100. case DSSCMD_IWRITE64_CX:
  1101. case DSSCMD_IREAD64_CR:
  1102. case DSSCMD_IREAD64_CX:
  1103. type = LPFC_SOL_IOCB;
  1104. break;
  1105. case CMD_ABORT_XRI_CN:
  1106. case CMD_ABORT_XRI_CX:
  1107. case CMD_CLOSE_XRI_CN:
  1108. case CMD_CLOSE_XRI_CX:
  1109. case CMD_XRI_ABORTED_CX:
  1110. case CMD_ABORT_MXRI64_CN:
  1111. case CMD_XMIT_BLS_RSP64_CX:
  1112. type = LPFC_ABORT_IOCB;
  1113. break;
  1114. case CMD_RCV_SEQUENCE_CX:
  1115. case CMD_RCV_ELS_REQ_CX:
  1116. case CMD_RCV_SEQUENCE64_CX:
  1117. case CMD_RCV_ELS_REQ64_CX:
  1118. case CMD_ASYNC_STATUS:
  1119. case CMD_IOCB_RCV_SEQ64_CX:
  1120. case CMD_IOCB_RCV_ELS64_CX:
  1121. case CMD_IOCB_RCV_CONT64_CX:
  1122. case CMD_IOCB_RET_XRI64_CX:
  1123. type = LPFC_UNSOL_IOCB;
  1124. break;
  1125. case CMD_IOCB_XMIT_MSEQ64_CR:
  1126. case CMD_IOCB_XMIT_MSEQ64_CX:
  1127. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1128. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1129. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1130. case CMD_IOCB_ABORT_EXTENDED_CN:
  1131. case CMD_IOCB_RET_HBQE64_CN:
  1132. case CMD_IOCB_FCP_IBIDIR64_CR:
  1133. case CMD_IOCB_FCP_IBIDIR64_CX:
  1134. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1135. case CMD_IOCB_LOGENTRY_CN:
  1136. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1137. printk("%s - Unhandled SLI-3 Command x%x\n",
  1138. __func__, iocb_cmnd);
  1139. type = LPFC_UNKNOWN_IOCB;
  1140. break;
  1141. default:
  1142. type = LPFC_UNKNOWN_IOCB;
  1143. break;
  1144. }
  1145. return type;
  1146. }
  1147. /**
  1148. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1149. * @phba: Pointer to HBA context object.
  1150. *
  1151. * This function is called from SLI initialization code
  1152. * to configure every ring of the HBA's SLI interface. The
  1153. * caller is not required to hold any lock. This function issues
  1154. * a config_ring mailbox command for each ring.
  1155. * This function returns zero if successful else returns a negative
  1156. * error code.
  1157. **/
  1158. static int
  1159. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1160. {
  1161. struct lpfc_sli *psli = &phba->sli;
  1162. LPFC_MBOXQ_t *pmb;
  1163. MAILBOX_t *pmbox;
  1164. int i, rc, ret = 0;
  1165. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1166. if (!pmb)
  1167. return -ENOMEM;
  1168. pmbox = &pmb->u.mb;
  1169. phba->link_state = LPFC_INIT_MBX_CMDS;
  1170. for (i = 0; i < psli->num_rings; i++) {
  1171. lpfc_config_ring(phba, i, pmb);
  1172. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1173. if (rc != MBX_SUCCESS) {
  1174. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1175. "0446 Adapter failed to init (%d), "
  1176. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1177. "ring %d\n",
  1178. rc, pmbox->mbxCommand,
  1179. pmbox->mbxStatus, i);
  1180. phba->link_state = LPFC_HBA_ERROR;
  1181. ret = -ENXIO;
  1182. break;
  1183. }
  1184. }
  1185. mempool_free(pmb, phba->mbox_mem_pool);
  1186. return ret;
  1187. }
  1188. /**
  1189. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1190. * @phba: Pointer to HBA context object.
  1191. * @pring: Pointer to driver SLI ring object.
  1192. * @piocb: Pointer to the driver iocb object.
  1193. *
  1194. * This function is called with hbalock held. The function adds the
  1195. * new iocb to txcmplq of the given ring. This function always returns
  1196. * 0. If this function is called for ELS ring, this function checks if
  1197. * there is a vport associated with the ELS command. This function also
  1198. * starts els_tmofunc timer if this is an ELS command.
  1199. **/
  1200. static int
  1201. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1202. struct lpfc_iocbq *piocb)
  1203. {
  1204. list_add_tail(&piocb->list, &pring->txcmplq);
  1205. piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
  1206. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1207. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1208. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN) &&
  1209. (!(piocb->vport->load_flag & FC_UNLOADING))) {
  1210. if (!piocb->vport)
  1211. BUG();
  1212. else
  1213. mod_timer(&piocb->vport->els_tmofunc,
  1214. jiffies +
  1215. msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
  1216. }
  1217. return 0;
  1218. }
  1219. /**
  1220. * lpfc_sli_ringtx_get - Get first element of the txq
  1221. * @phba: Pointer to HBA context object.
  1222. * @pring: Pointer to driver SLI ring object.
  1223. *
  1224. * This function is called with hbalock held to get next
  1225. * iocb in txq of the given ring. If there is any iocb in
  1226. * the txq, the function returns first iocb in the list after
  1227. * removing the iocb from the list, else it returns NULL.
  1228. **/
  1229. struct lpfc_iocbq *
  1230. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1231. {
  1232. struct lpfc_iocbq *cmd_iocb;
  1233. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1234. return cmd_iocb;
  1235. }
  1236. /**
  1237. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1238. * @phba: Pointer to HBA context object.
  1239. * @pring: Pointer to driver SLI ring object.
  1240. *
  1241. * This function is called with hbalock held and the caller must post the
  1242. * iocb without releasing the lock. If the caller releases the lock,
  1243. * iocb slot returned by the function is not guaranteed to be available.
  1244. * The function returns pointer to the next available iocb slot if there
  1245. * is available slot in the ring, else it returns NULL.
  1246. * If the get index of the ring is ahead of the put index, the function
  1247. * will post an error attention event to the worker thread to take the
  1248. * HBA to offline state.
  1249. **/
  1250. static IOCB_t *
  1251. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1252. {
  1253. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1254. uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
  1255. if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
  1256. (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
  1257. pring->sli.sli3.next_cmdidx = 0;
  1258. if (unlikely(pring->sli.sli3.local_getidx ==
  1259. pring->sli.sli3.next_cmdidx)) {
  1260. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1261. if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
  1262. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1263. "0315 Ring %d issue: portCmdGet %d "
  1264. "is bigger than cmd ring %d\n",
  1265. pring->ringno,
  1266. pring->sli.sli3.local_getidx,
  1267. max_cmd_idx);
  1268. phba->link_state = LPFC_HBA_ERROR;
  1269. /*
  1270. * All error attention handlers are posted to
  1271. * worker thread
  1272. */
  1273. phba->work_ha |= HA_ERATT;
  1274. phba->work_hs = HS_FFER3;
  1275. lpfc_worker_wake_up(phba);
  1276. return NULL;
  1277. }
  1278. if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
  1279. return NULL;
  1280. }
  1281. return lpfc_cmd_iocb(phba, pring);
  1282. }
  1283. /**
  1284. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1285. * @phba: Pointer to HBA context object.
  1286. * @iocbq: Pointer to driver iocb object.
  1287. *
  1288. * This function gets an iotag for the iocb. If there is no unused iotag and
  1289. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1290. * array and assigns a new iotag.
  1291. * The function returns the allocated iotag if successful, else returns zero.
  1292. * Zero is not a valid iotag.
  1293. * The caller is not required to hold any lock.
  1294. **/
  1295. uint16_t
  1296. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1297. {
  1298. struct lpfc_iocbq **new_arr;
  1299. struct lpfc_iocbq **old_arr;
  1300. size_t new_len;
  1301. struct lpfc_sli *psli = &phba->sli;
  1302. uint16_t iotag;
  1303. spin_lock_irq(&phba->hbalock);
  1304. iotag = psli->last_iotag;
  1305. if(++iotag < psli->iocbq_lookup_len) {
  1306. psli->last_iotag = iotag;
  1307. psli->iocbq_lookup[iotag] = iocbq;
  1308. spin_unlock_irq(&phba->hbalock);
  1309. iocbq->iotag = iotag;
  1310. return iotag;
  1311. } else if (psli->iocbq_lookup_len < (0xffff
  1312. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1313. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1314. spin_unlock_irq(&phba->hbalock);
  1315. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1316. GFP_KERNEL);
  1317. if (new_arr) {
  1318. spin_lock_irq(&phba->hbalock);
  1319. old_arr = psli->iocbq_lookup;
  1320. if (new_len <= psli->iocbq_lookup_len) {
  1321. /* highly unprobable case */
  1322. kfree(new_arr);
  1323. iotag = psli->last_iotag;
  1324. if(++iotag < psli->iocbq_lookup_len) {
  1325. psli->last_iotag = iotag;
  1326. psli->iocbq_lookup[iotag] = iocbq;
  1327. spin_unlock_irq(&phba->hbalock);
  1328. iocbq->iotag = iotag;
  1329. return iotag;
  1330. }
  1331. spin_unlock_irq(&phba->hbalock);
  1332. return 0;
  1333. }
  1334. if (psli->iocbq_lookup)
  1335. memcpy(new_arr, old_arr,
  1336. ((psli->last_iotag + 1) *
  1337. sizeof (struct lpfc_iocbq *)));
  1338. psli->iocbq_lookup = new_arr;
  1339. psli->iocbq_lookup_len = new_len;
  1340. psli->last_iotag = iotag;
  1341. psli->iocbq_lookup[iotag] = iocbq;
  1342. spin_unlock_irq(&phba->hbalock);
  1343. iocbq->iotag = iotag;
  1344. kfree(old_arr);
  1345. return iotag;
  1346. }
  1347. } else
  1348. spin_unlock_irq(&phba->hbalock);
  1349. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1350. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1351. psli->last_iotag);
  1352. return 0;
  1353. }
  1354. /**
  1355. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1356. * @phba: Pointer to HBA context object.
  1357. * @pring: Pointer to driver SLI ring object.
  1358. * @iocb: Pointer to iocb slot in the ring.
  1359. * @nextiocb: Pointer to driver iocb object which need to be
  1360. * posted to firmware.
  1361. *
  1362. * This function is called with hbalock held to post a new iocb to
  1363. * the firmware. This function copies the new iocb to ring iocb slot and
  1364. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1365. * a completion call back for this iocb else the function will free the
  1366. * iocb object.
  1367. **/
  1368. static void
  1369. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1370. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1371. {
  1372. /*
  1373. * Set up an iotag
  1374. */
  1375. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1376. if (pring->ringno == LPFC_ELS_RING) {
  1377. lpfc_debugfs_slow_ring_trc(phba,
  1378. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1379. *(((uint32_t *) &nextiocb->iocb) + 4),
  1380. *(((uint32_t *) &nextiocb->iocb) + 6),
  1381. *(((uint32_t *) &nextiocb->iocb) + 7));
  1382. }
  1383. /*
  1384. * Issue iocb command to adapter
  1385. */
  1386. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1387. wmb();
  1388. pring->stats.iocb_cmd++;
  1389. /*
  1390. * If there is no completion routine to call, we can release the
  1391. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1392. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1393. */
  1394. if (nextiocb->iocb_cmpl)
  1395. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1396. else
  1397. __lpfc_sli_release_iocbq(phba, nextiocb);
  1398. /*
  1399. * Let the HBA know what IOCB slot will be the next one the
  1400. * driver will put a command into.
  1401. */
  1402. pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
  1403. writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1404. }
  1405. /**
  1406. * lpfc_sli_update_full_ring - Update the chip attention register
  1407. * @phba: Pointer to HBA context object.
  1408. * @pring: Pointer to driver SLI ring object.
  1409. *
  1410. * The caller is not required to hold any lock for calling this function.
  1411. * This function updates the chip attention bits for the ring to inform firmware
  1412. * that there are pending work to be done for this ring and requests an
  1413. * interrupt when there is space available in the ring. This function is
  1414. * called when the driver is unable to post more iocbs to the ring due
  1415. * to unavailability of space in the ring.
  1416. **/
  1417. static void
  1418. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1419. {
  1420. int ringno = pring->ringno;
  1421. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1422. wmb();
  1423. /*
  1424. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1425. * The HBA will tell us when an IOCB entry is available.
  1426. */
  1427. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1428. readl(phba->CAregaddr); /* flush */
  1429. pring->stats.iocb_cmd_full++;
  1430. }
  1431. /**
  1432. * lpfc_sli_update_ring - Update chip attention register
  1433. * @phba: Pointer to HBA context object.
  1434. * @pring: Pointer to driver SLI ring object.
  1435. *
  1436. * This function updates the chip attention register bit for the
  1437. * given ring to inform HBA that there is more work to be done
  1438. * in this ring. The caller is not required to hold any lock.
  1439. **/
  1440. static void
  1441. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1442. {
  1443. int ringno = pring->ringno;
  1444. /*
  1445. * Tell the HBA that there is work to do in this ring.
  1446. */
  1447. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1448. wmb();
  1449. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1450. readl(phba->CAregaddr); /* flush */
  1451. }
  1452. }
  1453. /**
  1454. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1455. * @phba: Pointer to HBA context object.
  1456. * @pring: Pointer to driver SLI ring object.
  1457. *
  1458. * This function is called with hbalock held to post pending iocbs
  1459. * in the txq to the firmware. This function is called when driver
  1460. * detects space available in the ring.
  1461. **/
  1462. static void
  1463. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1464. {
  1465. IOCB_t *iocb;
  1466. struct lpfc_iocbq *nextiocb;
  1467. /*
  1468. * Check to see if:
  1469. * (a) there is anything on the txq to send
  1470. * (b) link is up
  1471. * (c) link attention events can be processed (fcp ring only)
  1472. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1473. */
  1474. if (lpfc_is_link_up(phba) &&
  1475. (!list_empty(&pring->txq)) &&
  1476. (pring->ringno != phba->sli.fcp_ring ||
  1477. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1478. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1479. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1480. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1481. if (iocb)
  1482. lpfc_sli_update_ring(phba, pring);
  1483. else
  1484. lpfc_sli_update_full_ring(phba, pring);
  1485. }
  1486. return;
  1487. }
  1488. /**
  1489. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1490. * @phba: Pointer to HBA context object.
  1491. * @hbqno: HBQ number.
  1492. *
  1493. * This function is called with hbalock held to get the next
  1494. * available slot for the given HBQ. If there is free slot
  1495. * available for the HBQ it will return pointer to the next available
  1496. * HBQ entry else it will return NULL.
  1497. **/
  1498. static struct lpfc_hbq_entry *
  1499. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1500. {
  1501. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1502. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1503. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1504. hbqp->next_hbqPutIdx = 0;
  1505. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1506. uint32_t raw_index = phba->hbq_get[hbqno];
  1507. uint32_t getidx = le32_to_cpu(raw_index);
  1508. hbqp->local_hbqGetIdx = getidx;
  1509. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1510. lpfc_printf_log(phba, KERN_ERR,
  1511. LOG_SLI | LOG_VPORT,
  1512. "1802 HBQ %d: local_hbqGetIdx "
  1513. "%u is > than hbqp->entry_count %u\n",
  1514. hbqno, hbqp->local_hbqGetIdx,
  1515. hbqp->entry_count);
  1516. phba->link_state = LPFC_HBA_ERROR;
  1517. return NULL;
  1518. }
  1519. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1520. return NULL;
  1521. }
  1522. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1523. hbqp->hbqPutIdx;
  1524. }
  1525. /**
  1526. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1527. * @phba: Pointer to HBA context object.
  1528. *
  1529. * This function is called with no lock held to free all the
  1530. * hbq buffers while uninitializing the SLI interface. It also
  1531. * frees the HBQ buffers returned by the firmware but not yet
  1532. * processed by the upper layers.
  1533. **/
  1534. void
  1535. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1536. {
  1537. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1538. struct hbq_dmabuf *hbq_buf;
  1539. unsigned long flags;
  1540. int i, hbq_count;
  1541. uint32_t hbqno;
  1542. hbq_count = lpfc_sli_hbq_count();
  1543. /* Return all memory used by all HBQs */
  1544. spin_lock_irqsave(&phba->hbalock, flags);
  1545. for (i = 0; i < hbq_count; ++i) {
  1546. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1547. &phba->hbqs[i].hbq_buffer_list, list) {
  1548. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1549. list_del(&hbq_buf->dbuf.list);
  1550. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1551. }
  1552. phba->hbqs[i].buffer_count = 0;
  1553. }
  1554. /* Return all HBQ buffer that are in-fly */
  1555. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1556. list) {
  1557. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1558. list_del(&hbq_buf->dbuf.list);
  1559. if (hbq_buf->tag == -1) {
  1560. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1561. (phba, hbq_buf);
  1562. } else {
  1563. hbqno = hbq_buf->tag >> 16;
  1564. if (hbqno >= LPFC_MAX_HBQS)
  1565. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1566. (phba, hbq_buf);
  1567. else
  1568. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1569. hbq_buf);
  1570. }
  1571. }
  1572. /* Mark the HBQs not in use */
  1573. phba->hbq_in_use = 0;
  1574. spin_unlock_irqrestore(&phba->hbalock, flags);
  1575. }
  1576. /**
  1577. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1578. * @phba: Pointer to HBA context object.
  1579. * @hbqno: HBQ number.
  1580. * @hbq_buf: Pointer to HBQ buffer.
  1581. *
  1582. * This function is called with the hbalock held to post a
  1583. * hbq buffer to the firmware. If the function finds an empty
  1584. * slot in the HBQ, it will post the buffer. The function will return
  1585. * pointer to the hbq entry if it successfully post the buffer
  1586. * else it will return NULL.
  1587. **/
  1588. static int
  1589. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1590. struct hbq_dmabuf *hbq_buf)
  1591. {
  1592. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1593. }
  1594. /**
  1595. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1596. * @phba: Pointer to HBA context object.
  1597. * @hbqno: HBQ number.
  1598. * @hbq_buf: Pointer to HBQ buffer.
  1599. *
  1600. * This function is called with the hbalock held to post a hbq buffer to the
  1601. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1602. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1603. * it successfully post the buffer else it will return an error.
  1604. **/
  1605. static int
  1606. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1607. struct hbq_dmabuf *hbq_buf)
  1608. {
  1609. struct lpfc_hbq_entry *hbqe;
  1610. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1611. /* Get next HBQ entry slot to use */
  1612. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1613. if (hbqe) {
  1614. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1615. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1616. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1617. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1618. hbqe->bde.tus.f.bdeFlags = 0;
  1619. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1620. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1621. /* Sync SLIM */
  1622. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1623. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1624. /* flush */
  1625. readl(phba->hbq_put + hbqno);
  1626. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1627. return 0;
  1628. } else
  1629. return -ENOMEM;
  1630. }
  1631. /**
  1632. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1633. * @phba: Pointer to HBA context object.
  1634. * @hbqno: HBQ number.
  1635. * @hbq_buf: Pointer to HBQ buffer.
  1636. *
  1637. * This function is called with the hbalock held to post an RQE to the SLI4
  1638. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1639. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1640. **/
  1641. static int
  1642. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1643. struct hbq_dmabuf *hbq_buf)
  1644. {
  1645. int rc;
  1646. struct lpfc_rqe hrqe;
  1647. struct lpfc_rqe drqe;
  1648. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1649. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1650. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1651. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1652. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1653. &hrqe, &drqe);
  1654. if (rc < 0)
  1655. return rc;
  1656. hbq_buf->tag = rc;
  1657. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1658. return 0;
  1659. }
  1660. /* HBQ for ELS and CT traffic. */
  1661. static struct lpfc_hbq_init lpfc_els_hbq = {
  1662. .rn = 1,
  1663. .entry_count = 256,
  1664. .mask_count = 0,
  1665. .profile = 0,
  1666. .ring_mask = (1 << LPFC_ELS_RING),
  1667. .buffer_count = 0,
  1668. .init_count = 40,
  1669. .add_count = 40,
  1670. };
  1671. /* HBQ for the extra ring if needed */
  1672. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1673. .rn = 1,
  1674. .entry_count = 200,
  1675. .mask_count = 0,
  1676. .profile = 0,
  1677. .ring_mask = (1 << LPFC_EXTRA_RING),
  1678. .buffer_count = 0,
  1679. .init_count = 0,
  1680. .add_count = 5,
  1681. };
  1682. /* Array of HBQs */
  1683. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1684. &lpfc_els_hbq,
  1685. &lpfc_extra_hbq,
  1686. };
  1687. /**
  1688. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1689. * @phba: Pointer to HBA context object.
  1690. * @hbqno: HBQ number.
  1691. * @count: Number of HBQ buffers to be posted.
  1692. *
  1693. * This function is called with no lock held to post more hbq buffers to the
  1694. * given HBQ. The function returns the number of HBQ buffers successfully
  1695. * posted.
  1696. **/
  1697. static int
  1698. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1699. {
  1700. uint32_t i, posted = 0;
  1701. unsigned long flags;
  1702. struct hbq_dmabuf *hbq_buffer;
  1703. LIST_HEAD(hbq_buf_list);
  1704. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1705. return 0;
  1706. if ((phba->hbqs[hbqno].buffer_count + count) >
  1707. lpfc_hbq_defs[hbqno]->entry_count)
  1708. count = lpfc_hbq_defs[hbqno]->entry_count -
  1709. phba->hbqs[hbqno].buffer_count;
  1710. if (!count)
  1711. return 0;
  1712. /* Allocate HBQ entries */
  1713. for (i = 0; i < count; i++) {
  1714. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1715. if (!hbq_buffer)
  1716. break;
  1717. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1718. }
  1719. /* Check whether HBQ is still in use */
  1720. spin_lock_irqsave(&phba->hbalock, flags);
  1721. if (!phba->hbq_in_use)
  1722. goto err;
  1723. while (!list_empty(&hbq_buf_list)) {
  1724. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1725. dbuf.list);
  1726. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1727. (hbqno << 16));
  1728. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1729. phba->hbqs[hbqno].buffer_count++;
  1730. posted++;
  1731. } else
  1732. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1733. }
  1734. spin_unlock_irqrestore(&phba->hbalock, flags);
  1735. return posted;
  1736. err:
  1737. spin_unlock_irqrestore(&phba->hbalock, flags);
  1738. while (!list_empty(&hbq_buf_list)) {
  1739. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1740. dbuf.list);
  1741. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1742. }
  1743. return 0;
  1744. }
  1745. /**
  1746. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1747. * @phba: Pointer to HBA context object.
  1748. * @qno: HBQ number.
  1749. *
  1750. * This function posts more buffers to the HBQ. This function
  1751. * is called with no lock held. The function returns the number of HBQ entries
  1752. * successfully allocated.
  1753. **/
  1754. int
  1755. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1756. {
  1757. if (phba->sli_rev == LPFC_SLI_REV4)
  1758. return 0;
  1759. else
  1760. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1761. lpfc_hbq_defs[qno]->add_count);
  1762. }
  1763. /**
  1764. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1765. * @phba: Pointer to HBA context object.
  1766. * @qno: HBQ queue number.
  1767. *
  1768. * This function is called from SLI initialization code path with
  1769. * no lock held to post initial HBQ buffers to firmware. The
  1770. * function returns the number of HBQ entries successfully allocated.
  1771. **/
  1772. static int
  1773. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1774. {
  1775. if (phba->sli_rev == LPFC_SLI_REV4)
  1776. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1777. lpfc_hbq_defs[qno]->entry_count);
  1778. else
  1779. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1780. lpfc_hbq_defs[qno]->init_count);
  1781. }
  1782. /**
  1783. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1784. * @phba: Pointer to HBA context object.
  1785. * @hbqno: HBQ number.
  1786. *
  1787. * This function removes the first hbq buffer on an hbq list and returns a
  1788. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1789. **/
  1790. static struct hbq_dmabuf *
  1791. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1792. {
  1793. struct lpfc_dmabuf *d_buf;
  1794. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1795. if (!d_buf)
  1796. return NULL;
  1797. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1798. }
  1799. /**
  1800. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1801. * @phba: Pointer to HBA context object.
  1802. * @tag: Tag of the hbq buffer.
  1803. *
  1804. * This function is called with hbalock held. This function searches
  1805. * for the hbq buffer associated with the given tag in the hbq buffer
  1806. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1807. * it returns NULL.
  1808. **/
  1809. static struct hbq_dmabuf *
  1810. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1811. {
  1812. struct lpfc_dmabuf *d_buf;
  1813. struct hbq_dmabuf *hbq_buf;
  1814. uint32_t hbqno;
  1815. hbqno = tag >> 16;
  1816. if (hbqno >= LPFC_MAX_HBQS)
  1817. return NULL;
  1818. spin_lock_irq(&phba->hbalock);
  1819. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1820. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1821. if (hbq_buf->tag == tag) {
  1822. spin_unlock_irq(&phba->hbalock);
  1823. return hbq_buf;
  1824. }
  1825. }
  1826. spin_unlock_irq(&phba->hbalock);
  1827. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1828. "1803 Bad hbq tag. Data: x%x x%x\n",
  1829. tag, phba->hbqs[tag >> 16].buffer_count);
  1830. return NULL;
  1831. }
  1832. /**
  1833. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1834. * @phba: Pointer to HBA context object.
  1835. * @hbq_buffer: Pointer to HBQ buffer.
  1836. *
  1837. * This function is called with hbalock. This function gives back
  1838. * the hbq buffer to firmware. If the HBQ does not have space to
  1839. * post the buffer, it will free the buffer.
  1840. **/
  1841. void
  1842. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1843. {
  1844. uint32_t hbqno;
  1845. if (hbq_buffer) {
  1846. hbqno = hbq_buffer->tag >> 16;
  1847. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1848. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1849. }
  1850. }
  1851. /**
  1852. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1853. * @mbxCommand: mailbox command code.
  1854. *
  1855. * This function is called by the mailbox event handler function to verify
  1856. * that the completed mailbox command is a legitimate mailbox command. If the
  1857. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1858. * and the mailbox event handler will take the HBA offline.
  1859. **/
  1860. static int
  1861. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1862. {
  1863. uint8_t ret;
  1864. switch (mbxCommand) {
  1865. case MBX_LOAD_SM:
  1866. case MBX_READ_NV:
  1867. case MBX_WRITE_NV:
  1868. case MBX_WRITE_VPARMS:
  1869. case MBX_RUN_BIU_DIAG:
  1870. case MBX_INIT_LINK:
  1871. case MBX_DOWN_LINK:
  1872. case MBX_CONFIG_LINK:
  1873. case MBX_CONFIG_RING:
  1874. case MBX_RESET_RING:
  1875. case MBX_READ_CONFIG:
  1876. case MBX_READ_RCONFIG:
  1877. case MBX_READ_SPARM:
  1878. case MBX_READ_STATUS:
  1879. case MBX_READ_RPI:
  1880. case MBX_READ_XRI:
  1881. case MBX_READ_REV:
  1882. case MBX_READ_LNK_STAT:
  1883. case MBX_REG_LOGIN:
  1884. case MBX_UNREG_LOGIN:
  1885. case MBX_CLEAR_LA:
  1886. case MBX_DUMP_MEMORY:
  1887. case MBX_DUMP_CONTEXT:
  1888. case MBX_RUN_DIAGS:
  1889. case MBX_RESTART:
  1890. case MBX_UPDATE_CFG:
  1891. case MBX_DOWN_LOAD:
  1892. case MBX_DEL_LD_ENTRY:
  1893. case MBX_RUN_PROGRAM:
  1894. case MBX_SET_MASK:
  1895. case MBX_SET_VARIABLE:
  1896. case MBX_UNREG_D_ID:
  1897. case MBX_KILL_BOARD:
  1898. case MBX_CONFIG_FARP:
  1899. case MBX_BEACON:
  1900. case MBX_LOAD_AREA:
  1901. case MBX_RUN_BIU_DIAG64:
  1902. case MBX_CONFIG_PORT:
  1903. case MBX_READ_SPARM64:
  1904. case MBX_READ_RPI64:
  1905. case MBX_REG_LOGIN64:
  1906. case MBX_READ_TOPOLOGY:
  1907. case MBX_WRITE_WWN:
  1908. case MBX_SET_DEBUG:
  1909. case MBX_LOAD_EXP_ROM:
  1910. case MBX_ASYNCEVT_ENABLE:
  1911. case MBX_REG_VPI:
  1912. case MBX_UNREG_VPI:
  1913. case MBX_HEARTBEAT:
  1914. case MBX_PORT_CAPABILITIES:
  1915. case MBX_PORT_IOV_CONTROL:
  1916. case MBX_SLI4_CONFIG:
  1917. case MBX_SLI4_REQ_FTRS:
  1918. case MBX_REG_FCFI:
  1919. case MBX_UNREG_FCFI:
  1920. case MBX_REG_VFI:
  1921. case MBX_UNREG_VFI:
  1922. case MBX_INIT_VPI:
  1923. case MBX_INIT_VFI:
  1924. case MBX_RESUME_RPI:
  1925. case MBX_READ_EVENT_LOG_STATUS:
  1926. case MBX_READ_EVENT_LOG:
  1927. case MBX_SECURITY_MGMT:
  1928. case MBX_AUTH_PORT:
  1929. case MBX_ACCESS_VDATA:
  1930. ret = mbxCommand;
  1931. break;
  1932. default:
  1933. ret = MBX_SHUTDOWN;
  1934. break;
  1935. }
  1936. return ret;
  1937. }
  1938. /**
  1939. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1940. * @phba: Pointer to HBA context object.
  1941. * @pmboxq: Pointer to mailbox command.
  1942. *
  1943. * This is completion handler function for mailbox commands issued from
  1944. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1945. * mailbox event handler function with no lock held. This function
  1946. * will wake up thread waiting on the wait queue pointed by context1
  1947. * of the mailbox.
  1948. **/
  1949. void
  1950. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1951. {
  1952. wait_queue_head_t *pdone_q;
  1953. unsigned long drvr_flag;
  1954. /*
  1955. * If pdone_q is empty, the driver thread gave up waiting and
  1956. * continued running.
  1957. */
  1958. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1959. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1960. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1961. if (pdone_q)
  1962. wake_up_interruptible(pdone_q);
  1963. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1964. return;
  1965. }
  1966. /**
  1967. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1968. * @phba: Pointer to HBA context object.
  1969. * @pmb: Pointer to mailbox object.
  1970. *
  1971. * This function is the default mailbox completion handler. It
  1972. * frees the memory resources associated with the completed mailbox
  1973. * command. If the completed command is a REG_LOGIN mailbox command,
  1974. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1975. **/
  1976. void
  1977. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1978. {
  1979. struct lpfc_vport *vport = pmb->vport;
  1980. struct lpfc_dmabuf *mp;
  1981. struct lpfc_nodelist *ndlp;
  1982. struct Scsi_Host *shost;
  1983. uint16_t rpi, vpi;
  1984. int rc;
  1985. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1986. if (mp) {
  1987. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1988. kfree(mp);
  1989. }
  1990. /*
  1991. * If a REG_LOGIN succeeded after node is destroyed or node
  1992. * is in re-discovery driver need to cleanup the RPI.
  1993. */
  1994. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1995. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1996. !pmb->u.mb.mbxStatus) {
  1997. rpi = pmb->u.mb.un.varWords[0];
  1998. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1999. lpfc_unreg_login(phba, vpi, rpi, pmb);
  2000. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  2001. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2002. if (rc != MBX_NOT_FINISHED)
  2003. return;
  2004. }
  2005. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  2006. !(phba->pport->load_flag & FC_UNLOADING) &&
  2007. !pmb->u.mb.mbxStatus) {
  2008. shost = lpfc_shost_from_vport(vport);
  2009. spin_lock_irq(shost->host_lock);
  2010. vport->vpi_state |= LPFC_VPI_REGISTERED;
  2011. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  2012. spin_unlock_irq(shost->host_lock);
  2013. }
  2014. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  2015. ndlp = (struct lpfc_nodelist *)pmb->context2;
  2016. lpfc_nlp_put(ndlp);
  2017. pmb->context2 = NULL;
  2018. }
  2019. /* Check security permission status on INIT_LINK mailbox command */
  2020. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  2021. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  2022. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2023. "2860 SLI authentication is required "
  2024. "for INIT_LINK but has not done yet\n");
  2025. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  2026. lpfc_sli4_mbox_cmd_free(phba, pmb);
  2027. else
  2028. mempool_free(pmb, phba->mbox_mem_pool);
  2029. }
  2030. /**
  2031. * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
  2032. * @phba: Pointer to HBA context object.
  2033. * @pmb: Pointer to mailbox object.
  2034. *
  2035. * This function is the unreg rpi mailbox completion handler. It
  2036. * frees the memory resources associated with the completed mailbox
  2037. * command. An additional refrenece is put on the ndlp to prevent
  2038. * lpfc_nlp_release from freeing the rpi bit in the bitmask before
  2039. * the unreg mailbox command completes, this routine puts the
  2040. * reference back.
  2041. *
  2042. **/
  2043. void
  2044. lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2045. {
  2046. struct lpfc_vport *vport = pmb->vport;
  2047. struct lpfc_nodelist *ndlp;
  2048. ndlp = pmb->context1;
  2049. if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
  2050. if (phba->sli_rev == LPFC_SLI_REV4 &&
  2051. (bf_get(lpfc_sli_intf_if_type,
  2052. &phba->sli4_hba.sli_intf) ==
  2053. LPFC_SLI_INTF_IF_TYPE_2)) {
  2054. if (ndlp) {
  2055. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  2056. "0010 UNREG_LOGIN vpi:%x "
  2057. "rpi:%x DID:%x map:%x %p\n",
  2058. vport->vpi, ndlp->nlp_rpi,
  2059. ndlp->nlp_DID,
  2060. ndlp->nlp_usg_map, ndlp);
  2061. ndlp->nlp_flag &= ~NLP_LOGO_ACC;
  2062. lpfc_nlp_put(ndlp);
  2063. }
  2064. }
  2065. }
  2066. mempool_free(pmb, phba->mbox_mem_pool);
  2067. }
  2068. /**
  2069. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  2070. * @phba: Pointer to HBA context object.
  2071. *
  2072. * This function is called with no lock held. This function processes all
  2073. * the completed mailbox commands and gives it to upper layers. The interrupt
  2074. * service routine processes mailbox completion interrupt and adds completed
  2075. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  2076. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  2077. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  2078. * function returns the mailbox commands to the upper layer by calling the
  2079. * completion handler function of each mailbox.
  2080. **/
  2081. int
  2082. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  2083. {
  2084. MAILBOX_t *pmbox;
  2085. LPFC_MBOXQ_t *pmb;
  2086. int rc;
  2087. LIST_HEAD(cmplq);
  2088. phba->sli.slistat.mbox_event++;
  2089. /* Get all completed mailboxe buffers into the cmplq */
  2090. spin_lock_irq(&phba->hbalock);
  2091. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2092. spin_unlock_irq(&phba->hbalock);
  2093. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2094. do {
  2095. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2096. if (pmb == NULL)
  2097. break;
  2098. pmbox = &pmb->u.mb;
  2099. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2100. if (pmb->vport) {
  2101. lpfc_debugfs_disc_trc(pmb->vport,
  2102. LPFC_DISC_TRC_MBOX_VPORT,
  2103. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2104. (uint32_t)pmbox->mbxCommand,
  2105. pmbox->un.varWords[0],
  2106. pmbox->un.varWords[1]);
  2107. }
  2108. else {
  2109. lpfc_debugfs_disc_trc(phba->pport,
  2110. LPFC_DISC_TRC_MBOX,
  2111. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2112. (uint32_t)pmbox->mbxCommand,
  2113. pmbox->un.varWords[0],
  2114. pmbox->un.varWords[1]);
  2115. }
  2116. }
  2117. /*
  2118. * It is a fatal error if unknown mbox command completion.
  2119. */
  2120. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2121. MBX_SHUTDOWN) {
  2122. /* Unknown mailbox command compl */
  2123. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2124. "(%d):0323 Unknown Mailbox command "
  2125. "x%x (x%x/x%x) Cmpl\n",
  2126. pmb->vport ? pmb->vport->vpi : 0,
  2127. pmbox->mbxCommand,
  2128. lpfc_sli_config_mbox_subsys_get(phba,
  2129. pmb),
  2130. lpfc_sli_config_mbox_opcode_get(phba,
  2131. pmb));
  2132. phba->link_state = LPFC_HBA_ERROR;
  2133. phba->work_hs = HS_FFER3;
  2134. lpfc_handle_eratt(phba);
  2135. continue;
  2136. }
  2137. if (pmbox->mbxStatus) {
  2138. phba->sli.slistat.mbox_stat_err++;
  2139. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2140. /* Mbox cmd cmpl error - RETRYing */
  2141. lpfc_printf_log(phba, KERN_INFO,
  2142. LOG_MBOX | LOG_SLI,
  2143. "(%d):0305 Mbox cmd cmpl "
  2144. "error - RETRYing Data: x%x "
  2145. "(x%x/x%x) x%x x%x x%x\n",
  2146. pmb->vport ? pmb->vport->vpi : 0,
  2147. pmbox->mbxCommand,
  2148. lpfc_sli_config_mbox_subsys_get(phba,
  2149. pmb),
  2150. lpfc_sli_config_mbox_opcode_get(phba,
  2151. pmb),
  2152. pmbox->mbxStatus,
  2153. pmbox->un.varWords[0],
  2154. pmb->vport->port_state);
  2155. pmbox->mbxStatus = 0;
  2156. pmbox->mbxOwner = OWN_HOST;
  2157. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2158. if (rc != MBX_NOT_FINISHED)
  2159. continue;
  2160. }
  2161. }
  2162. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2163. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2164. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2165. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  2166. "x%x x%x x%x\n",
  2167. pmb->vport ? pmb->vport->vpi : 0,
  2168. pmbox->mbxCommand,
  2169. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2170. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2171. pmb->mbox_cmpl,
  2172. *((uint32_t *) pmbox),
  2173. pmbox->un.varWords[0],
  2174. pmbox->un.varWords[1],
  2175. pmbox->un.varWords[2],
  2176. pmbox->un.varWords[3],
  2177. pmbox->un.varWords[4],
  2178. pmbox->un.varWords[5],
  2179. pmbox->un.varWords[6],
  2180. pmbox->un.varWords[7],
  2181. pmbox->un.varWords[8],
  2182. pmbox->un.varWords[9],
  2183. pmbox->un.varWords[10]);
  2184. if (pmb->mbox_cmpl)
  2185. pmb->mbox_cmpl(phba,pmb);
  2186. } while (1);
  2187. return 0;
  2188. }
  2189. /**
  2190. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2191. * @phba: Pointer to HBA context object.
  2192. * @pring: Pointer to driver SLI ring object.
  2193. * @tag: buffer tag.
  2194. *
  2195. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2196. * is set in the tag the buffer is posted for a particular exchange,
  2197. * the function will return the buffer without replacing the buffer.
  2198. * If the buffer is for unsolicited ELS or CT traffic, this function
  2199. * returns the buffer and also posts another buffer to the firmware.
  2200. **/
  2201. static struct lpfc_dmabuf *
  2202. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2203. struct lpfc_sli_ring *pring,
  2204. uint32_t tag)
  2205. {
  2206. struct hbq_dmabuf *hbq_entry;
  2207. if (tag & QUE_BUFTAG_BIT)
  2208. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2209. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2210. if (!hbq_entry)
  2211. return NULL;
  2212. return &hbq_entry->dbuf;
  2213. }
  2214. /**
  2215. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2216. * @phba: Pointer to HBA context object.
  2217. * @pring: Pointer to driver SLI ring object.
  2218. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2219. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2220. * @fch_type: the type for the first frame of the sequence.
  2221. *
  2222. * This function is called with no lock held. This function uses the r_ctl and
  2223. * type of the received sequence to find the correct callback function to call
  2224. * to process the sequence.
  2225. **/
  2226. static int
  2227. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2228. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2229. uint32_t fch_type)
  2230. {
  2231. int i;
  2232. /* unSolicited Responses */
  2233. if (pring->prt[0].profile) {
  2234. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2235. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2236. saveq);
  2237. return 1;
  2238. }
  2239. /* We must search, based on rctl / type
  2240. for the right routine */
  2241. for (i = 0; i < pring->num_mask; i++) {
  2242. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2243. (pring->prt[i].type == fch_type)) {
  2244. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2245. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2246. (phba, pring, saveq);
  2247. return 1;
  2248. }
  2249. }
  2250. return 0;
  2251. }
  2252. /**
  2253. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2254. * @phba: Pointer to HBA context object.
  2255. * @pring: Pointer to driver SLI ring object.
  2256. * @saveq: Pointer to the unsolicited iocb.
  2257. *
  2258. * This function is called with no lock held by the ring event handler
  2259. * when there is an unsolicited iocb posted to the response ring by the
  2260. * firmware. This function gets the buffer associated with the iocbs
  2261. * and calls the event handler for the ring. This function handles both
  2262. * qring buffers and hbq buffers.
  2263. * When the function returns 1 the caller can free the iocb object otherwise
  2264. * upper layer functions will free the iocb objects.
  2265. **/
  2266. static int
  2267. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2268. struct lpfc_iocbq *saveq)
  2269. {
  2270. IOCB_t * irsp;
  2271. WORD5 * w5p;
  2272. uint32_t Rctl, Type;
  2273. struct lpfc_iocbq *iocbq;
  2274. struct lpfc_dmabuf *dmzbuf;
  2275. irsp = &(saveq->iocb);
  2276. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2277. if (pring->lpfc_sli_rcv_async_status)
  2278. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2279. else
  2280. lpfc_printf_log(phba,
  2281. KERN_WARNING,
  2282. LOG_SLI,
  2283. "0316 Ring %d handler: unexpected "
  2284. "ASYNC_STATUS iocb received evt_code "
  2285. "0x%x\n",
  2286. pring->ringno,
  2287. irsp->un.asyncstat.evt_code);
  2288. return 1;
  2289. }
  2290. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2291. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2292. if (irsp->ulpBdeCount > 0) {
  2293. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2294. irsp->un.ulpWord[3]);
  2295. lpfc_in_buf_free(phba, dmzbuf);
  2296. }
  2297. if (irsp->ulpBdeCount > 1) {
  2298. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2299. irsp->unsli3.sli3Words[3]);
  2300. lpfc_in_buf_free(phba, dmzbuf);
  2301. }
  2302. if (irsp->ulpBdeCount > 2) {
  2303. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2304. irsp->unsli3.sli3Words[7]);
  2305. lpfc_in_buf_free(phba, dmzbuf);
  2306. }
  2307. return 1;
  2308. }
  2309. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2310. if (irsp->ulpBdeCount != 0) {
  2311. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2312. irsp->un.ulpWord[3]);
  2313. if (!saveq->context2)
  2314. lpfc_printf_log(phba,
  2315. KERN_ERR,
  2316. LOG_SLI,
  2317. "0341 Ring %d Cannot find buffer for "
  2318. "an unsolicited iocb. tag 0x%x\n",
  2319. pring->ringno,
  2320. irsp->un.ulpWord[3]);
  2321. }
  2322. if (irsp->ulpBdeCount == 2) {
  2323. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2324. irsp->unsli3.sli3Words[7]);
  2325. if (!saveq->context3)
  2326. lpfc_printf_log(phba,
  2327. KERN_ERR,
  2328. LOG_SLI,
  2329. "0342 Ring %d Cannot find buffer for an"
  2330. " unsolicited iocb. tag 0x%x\n",
  2331. pring->ringno,
  2332. irsp->unsli3.sli3Words[7]);
  2333. }
  2334. list_for_each_entry(iocbq, &saveq->list, list) {
  2335. irsp = &(iocbq->iocb);
  2336. if (irsp->ulpBdeCount != 0) {
  2337. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2338. irsp->un.ulpWord[3]);
  2339. if (!iocbq->context2)
  2340. lpfc_printf_log(phba,
  2341. KERN_ERR,
  2342. LOG_SLI,
  2343. "0343 Ring %d Cannot find "
  2344. "buffer for an unsolicited iocb"
  2345. ". tag 0x%x\n", pring->ringno,
  2346. irsp->un.ulpWord[3]);
  2347. }
  2348. if (irsp->ulpBdeCount == 2) {
  2349. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2350. irsp->unsli3.sli3Words[7]);
  2351. if (!iocbq->context3)
  2352. lpfc_printf_log(phba,
  2353. KERN_ERR,
  2354. LOG_SLI,
  2355. "0344 Ring %d Cannot find "
  2356. "buffer for an unsolicited "
  2357. "iocb. tag 0x%x\n",
  2358. pring->ringno,
  2359. irsp->unsli3.sli3Words[7]);
  2360. }
  2361. }
  2362. }
  2363. if (irsp->ulpBdeCount != 0 &&
  2364. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2365. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2366. int found = 0;
  2367. /* search continue save q for same XRI */
  2368. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2369. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2370. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2371. list_add_tail(&saveq->list, &iocbq->list);
  2372. found = 1;
  2373. break;
  2374. }
  2375. }
  2376. if (!found)
  2377. list_add_tail(&saveq->clist,
  2378. &pring->iocb_continue_saveq);
  2379. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2380. list_del_init(&iocbq->clist);
  2381. saveq = iocbq;
  2382. irsp = &(saveq->iocb);
  2383. } else
  2384. return 0;
  2385. }
  2386. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2387. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2388. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2389. Rctl = FC_RCTL_ELS_REQ;
  2390. Type = FC_TYPE_ELS;
  2391. } else {
  2392. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2393. Rctl = w5p->hcsw.Rctl;
  2394. Type = w5p->hcsw.Type;
  2395. /* Firmware Workaround */
  2396. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2397. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2398. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2399. Rctl = FC_RCTL_ELS_REQ;
  2400. Type = FC_TYPE_ELS;
  2401. w5p->hcsw.Rctl = Rctl;
  2402. w5p->hcsw.Type = Type;
  2403. }
  2404. }
  2405. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2406. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2407. "0313 Ring %d handler: unexpected Rctl x%x "
  2408. "Type x%x received\n",
  2409. pring->ringno, Rctl, Type);
  2410. return 1;
  2411. }
  2412. /**
  2413. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2414. * @phba: Pointer to HBA context object.
  2415. * @pring: Pointer to driver SLI ring object.
  2416. * @prspiocb: Pointer to response iocb object.
  2417. *
  2418. * This function looks up the iocb_lookup table to get the command iocb
  2419. * corresponding to the given response iocb using the iotag of the
  2420. * response iocb. This function is called with the hbalock held.
  2421. * This function returns the command iocb object if it finds the command
  2422. * iocb else returns NULL.
  2423. **/
  2424. static struct lpfc_iocbq *
  2425. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2426. struct lpfc_sli_ring *pring,
  2427. struct lpfc_iocbq *prspiocb)
  2428. {
  2429. struct lpfc_iocbq *cmd_iocb = NULL;
  2430. uint16_t iotag;
  2431. iotag = prspiocb->iocb.ulpIoTag;
  2432. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2433. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2434. list_del_init(&cmd_iocb->list);
  2435. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2436. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2437. }
  2438. return cmd_iocb;
  2439. }
  2440. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2441. "0317 iotag x%x is out off "
  2442. "range: max iotag x%x wd0 x%x\n",
  2443. iotag, phba->sli.last_iotag,
  2444. *(((uint32_t *) &prspiocb->iocb) + 7));
  2445. return NULL;
  2446. }
  2447. /**
  2448. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2449. * @phba: Pointer to HBA context object.
  2450. * @pring: Pointer to driver SLI ring object.
  2451. * @iotag: IOCB tag.
  2452. *
  2453. * This function looks up the iocb_lookup table to get the command iocb
  2454. * corresponding to the given iotag. This function is called with the
  2455. * hbalock held.
  2456. * This function returns the command iocb object if it finds the command
  2457. * iocb else returns NULL.
  2458. **/
  2459. static struct lpfc_iocbq *
  2460. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2461. struct lpfc_sli_ring *pring, uint16_t iotag)
  2462. {
  2463. struct lpfc_iocbq *cmd_iocb;
  2464. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2465. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2466. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2467. /* remove from txcmpl queue list */
  2468. list_del_init(&cmd_iocb->list);
  2469. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2470. return cmd_iocb;
  2471. }
  2472. }
  2473. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2474. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2475. iotag, phba->sli.last_iotag);
  2476. return NULL;
  2477. }
  2478. /**
  2479. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2480. * @phba: Pointer to HBA context object.
  2481. * @pring: Pointer to driver SLI ring object.
  2482. * @saveq: Pointer to the response iocb to be processed.
  2483. *
  2484. * This function is called by the ring event handler for non-fcp
  2485. * rings when there is a new response iocb in the response ring.
  2486. * The caller is not required to hold any locks. This function
  2487. * gets the command iocb associated with the response iocb and
  2488. * calls the completion handler for the command iocb. If there
  2489. * is no completion handler, the function will free the resources
  2490. * associated with command iocb. If the response iocb is for
  2491. * an already aborted command iocb, the status of the completion
  2492. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2493. * This function always returns 1.
  2494. **/
  2495. static int
  2496. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2497. struct lpfc_iocbq *saveq)
  2498. {
  2499. struct lpfc_iocbq *cmdiocbp;
  2500. int rc = 1;
  2501. unsigned long iflag;
  2502. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2503. spin_lock_irqsave(&phba->hbalock, iflag);
  2504. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2505. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2506. if (cmdiocbp) {
  2507. if (cmdiocbp->iocb_cmpl) {
  2508. /*
  2509. * If an ELS command failed send an event to mgmt
  2510. * application.
  2511. */
  2512. if (saveq->iocb.ulpStatus &&
  2513. (pring->ringno == LPFC_ELS_RING) &&
  2514. (cmdiocbp->iocb.ulpCommand ==
  2515. CMD_ELS_REQUEST64_CR))
  2516. lpfc_send_els_failure_event(phba,
  2517. cmdiocbp, saveq);
  2518. /*
  2519. * Post all ELS completions to the worker thread.
  2520. * All other are passed to the completion callback.
  2521. */
  2522. if (pring->ringno == LPFC_ELS_RING) {
  2523. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2524. (cmdiocbp->iocb_flag &
  2525. LPFC_DRIVER_ABORTED)) {
  2526. spin_lock_irqsave(&phba->hbalock,
  2527. iflag);
  2528. cmdiocbp->iocb_flag &=
  2529. ~LPFC_DRIVER_ABORTED;
  2530. spin_unlock_irqrestore(&phba->hbalock,
  2531. iflag);
  2532. saveq->iocb.ulpStatus =
  2533. IOSTAT_LOCAL_REJECT;
  2534. saveq->iocb.un.ulpWord[4] =
  2535. IOERR_SLI_ABORTED;
  2536. /* Firmware could still be in progress
  2537. * of DMAing payload, so don't free data
  2538. * buffer till after a hbeat.
  2539. */
  2540. spin_lock_irqsave(&phba->hbalock,
  2541. iflag);
  2542. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2543. spin_unlock_irqrestore(&phba->hbalock,
  2544. iflag);
  2545. }
  2546. if (phba->sli_rev == LPFC_SLI_REV4) {
  2547. if (saveq->iocb_flag &
  2548. LPFC_EXCHANGE_BUSY) {
  2549. /* Set cmdiocb flag for the
  2550. * exchange busy so sgl (xri)
  2551. * will not be released until
  2552. * the abort xri is received
  2553. * from hba.
  2554. */
  2555. spin_lock_irqsave(
  2556. &phba->hbalock, iflag);
  2557. cmdiocbp->iocb_flag |=
  2558. LPFC_EXCHANGE_BUSY;
  2559. spin_unlock_irqrestore(
  2560. &phba->hbalock, iflag);
  2561. }
  2562. if (cmdiocbp->iocb_flag &
  2563. LPFC_DRIVER_ABORTED) {
  2564. /*
  2565. * Clear LPFC_DRIVER_ABORTED
  2566. * bit in case it was driver
  2567. * initiated abort.
  2568. */
  2569. spin_lock_irqsave(
  2570. &phba->hbalock, iflag);
  2571. cmdiocbp->iocb_flag &=
  2572. ~LPFC_DRIVER_ABORTED;
  2573. spin_unlock_irqrestore(
  2574. &phba->hbalock, iflag);
  2575. cmdiocbp->iocb.ulpStatus =
  2576. IOSTAT_LOCAL_REJECT;
  2577. cmdiocbp->iocb.un.ulpWord[4] =
  2578. IOERR_ABORT_REQUESTED;
  2579. /*
  2580. * For SLI4, irsiocb contains
  2581. * NO_XRI in sli_xritag, it
  2582. * shall not affect releasing
  2583. * sgl (xri) process.
  2584. */
  2585. saveq->iocb.ulpStatus =
  2586. IOSTAT_LOCAL_REJECT;
  2587. saveq->iocb.un.ulpWord[4] =
  2588. IOERR_SLI_ABORTED;
  2589. spin_lock_irqsave(
  2590. &phba->hbalock, iflag);
  2591. saveq->iocb_flag |=
  2592. LPFC_DELAY_MEM_FREE;
  2593. spin_unlock_irqrestore(
  2594. &phba->hbalock, iflag);
  2595. }
  2596. }
  2597. }
  2598. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2599. } else
  2600. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2601. } else {
  2602. /*
  2603. * Unknown initiating command based on the response iotag.
  2604. * This could be the case on the ELS ring because of
  2605. * lpfc_els_abort().
  2606. */
  2607. if (pring->ringno != LPFC_ELS_RING) {
  2608. /*
  2609. * Ring <ringno> handler: unexpected completion IoTag
  2610. * <IoTag>
  2611. */
  2612. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2613. "0322 Ring %d handler: "
  2614. "unexpected completion IoTag x%x "
  2615. "Data: x%x x%x x%x x%x\n",
  2616. pring->ringno,
  2617. saveq->iocb.ulpIoTag,
  2618. saveq->iocb.ulpStatus,
  2619. saveq->iocb.un.ulpWord[4],
  2620. saveq->iocb.ulpCommand,
  2621. saveq->iocb.ulpContext);
  2622. }
  2623. }
  2624. return rc;
  2625. }
  2626. /**
  2627. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2628. * @phba: Pointer to HBA context object.
  2629. * @pring: Pointer to driver SLI ring object.
  2630. *
  2631. * This function is called from the iocb ring event handlers when
  2632. * put pointer is ahead of the get pointer for a ring. This function signal
  2633. * an error attention condition to the worker thread and the worker
  2634. * thread will transition the HBA to offline state.
  2635. **/
  2636. static void
  2637. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2638. {
  2639. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2640. /*
  2641. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2642. * rsp ring <portRspMax>
  2643. */
  2644. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2645. "0312 Ring %d handler: portRspPut %d "
  2646. "is bigger than rsp ring %d\n",
  2647. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2648. pring->sli.sli3.numRiocb);
  2649. phba->link_state = LPFC_HBA_ERROR;
  2650. /*
  2651. * All error attention handlers are posted to
  2652. * worker thread
  2653. */
  2654. phba->work_ha |= HA_ERATT;
  2655. phba->work_hs = HS_FFER3;
  2656. lpfc_worker_wake_up(phba);
  2657. return;
  2658. }
  2659. /**
  2660. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2661. * @ptr: Pointer to address of HBA context object.
  2662. *
  2663. * This function is invoked by the Error Attention polling timer when the
  2664. * timer times out. It will check the SLI Error Attention register for
  2665. * possible attention events. If so, it will post an Error Attention event
  2666. * and wake up worker thread to process it. Otherwise, it will set up the
  2667. * Error Attention polling timer for the next poll.
  2668. **/
  2669. void lpfc_poll_eratt(unsigned long ptr)
  2670. {
  2671. struct lpfc_hba *phba;
  2672. uint32_t eratt = 0;
  2673. uint64_t sli_intr, cnt;
  2674. phba = (struct lpfc_hba *)ptr;
  2675. /* Here we will also keep track of interrupts per sec of the hba */
  2676. sli_intr = phba->sli.slistat.sli_intr;
  2677. if (phba->sli.slistat.sli_prev_intr > sli_intr)
  2678. cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
  2679. sli_intr);
  2680. else
  2681. cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
  2682. /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
  2683. do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
  2684. phba->sli.slistat.sli_ips = cnt;
  2685. phba->sli.slistat.sli_prev_intr = sli_intr;
  2686. /* Check chip HA register for error event */
  2687. eratt = lpfc_sli_check_eratt(phba);
  2688. if (eratt)
  2689. /* Tell the worker thread there is work to do */
  2690. lpfc_worker_wake_up(phba);
  2691. else
  2692. /* Restart the timer for next eratt poll */
  2693. mod_timer(&phba->eratt_poll,
  2694. jiffies +
  2695. msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
  2696. return;
  2697. }
  2698. /**
  2699. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2700. * @phba: Pointer to HBA context object.
  2701. * @pring: Pointer to driver SLI ring object.
  2702. * @mask: Host attention register mask for this ring.
  2703. *
  2704. * This function is called from the interrupt context when there is a ring
  2705. * event for the fcp ring. The caller does not hold any lock.
  2706. * The function processes each response iocb in the response ring until it
  2707. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2708. * LE bit set. The function will call the completion handler of the command iocb
  2709. * if the response iocb indicates a completion for a command iocb or it is
  2710. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2711. * function if this is an unsolicited iocb.
  2712. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2713. * to check it explicitly.
  2714. */
  2715. int
  2716. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2717. struct lpfc_sli_ring *pring, uint32_t mask)
  2718. {
  2719. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2720. IOCB_t *irsp = NULL;
  2721. IOCB_t *entry = NULL;
  2722. struct lpfc_iocbq *cmdiocbq = NULL;
  2723. struct lpfc_iocbq rspiocbq;
  2724. uint32_t status;
  2725. uint32_t portRspPut, portRspMax;
  2726. int rc = 1;
  2727. lpfc_iocb_type type;
  2728. unsigned long iflag;
  2729. uint32_t rsp_cmpl = 0;
  2730. spin_lock_irqsave(&phba->hbalock, iflag);
  2731. pring->stats.iocb_event++;
  2732. /*
  2733. * The next available response entry should never exceed the maximum
  2734. * entries. If it does, treat it as an adapter hardware error.
  2735. */
  2736. portRspMax = pring->sli.sli3.numRiocb;
  2737. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2738. if (unlikely(portRspPut >= portRspMax)) {
  2739. lpfc_sli_rsp_pointers_error(phba, pring);
  2740. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2741. return 1;
  2742. }
  2743. if (phba->fcp_ring_in_use) {
  2744. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2745. return 1;
  2746. } else
  2747. phba->fcp_ring_in_use = 1;
  2748. rmb();
  2749. while (pring->sli.sli3.rspidx != portRspPut) {
  2750. /*
  2751. * Fetch an entry off the ring and copy it into a local data
  2752. * structure. The copy involves a byte-swap since the
  2753. * network byte order and pci byte orders are different.
  2754. */
  2755. entry = lpfc_resp_iocb(phba, pring);
  2756. phba->last_completion_time = jiffies;
  2757. if (++pring->sli.sli3.rspidx >= portRspMax)
  2758. pring->sli.sli3.rspidx = 0;
  2759. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2760. (uint32_t *) &rspiocbq.iocb,
  2761. phba->iocb_rsp_size);
  2762. INIT_LIST_HEAD(&(rspiocbq.list));
  2763. irsp = &rspiocbq.iocb;
  2764. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2765. pring->stats.iocb_rsp++;
  2766. rsp_cmpl++;
  2767. if (unlikely(irsp->ulpStatus)) {
  2768. /*
  2769. * If resource errors reported from HBA, reduce
  2770. * queuedepths of the SCSI device.
  2771. */
  2772. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2773. ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
  2774. IOERR_NO_RESOURCES)) {
  2775. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2776. phba->lpfc_rampdown_queue_depth(phba);
  2777. spin_lock_irqsave(&phba->hbalock, iflag);
  2778. }
  2779. /* Rsp ring <ringno> error: IOCB */
  2780. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2781. "0336 Rsp Ring %d error: IOCB Data: "
  2782. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2783. pring->ringno,
  2784. irsp->un.ulpWord[0],
  2785. irsp->un.ulpWord[1],
  2786. irsp->un.ulpWord[2],
  2787. irsp->un.ulpWord[3],
  2788. irsp->un.ulpWord[4],
  2789. irsp->un.ulpWord[5],
  2790. *(uint32_t *)&irsp->un1,
  2791. *((uint32_t *)&irsp->un1 + 1));
  2792. }
  2793. switch (type) {
  2794. case LPFC_ABORT_IOCB:
  2795. case LPFC_SOL_IOCB:
  2796. /*
  2797. * Idle exchange closed via ABTS from port. No iocb
  2798. * resources need to be recovered.
  2799. */
  2800. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2801. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2802. "0333 IOCB cmd 0x%x"
  2803. " processed. Skipping"
  2804. " completion\n",
  2805. irsp->ulpCommand);
  2806. break;
  2807. }
  2808. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2809. &rspiocbq);
  2810. if (unlikely(!cmdiocbq))
  2811. break;
  2812. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2813. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2814. if (cmdiocbq->iocb_cmpl) {
  2815. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2816. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2817. &rspiocbq);
  2818. spin_lock_irqsave(&phba->hbalock, iflag);
  2819. }
  2820. break;
  2821. case LPFC_UNSOL_IOCB:
  2822. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2823. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2824. spin_lock_irqsave(&phba->hbalock, iflag);
  2825. break;
  2826. default:
  2827. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2828. char adaptermsg[LPFC_MAX_ADPTMSG];
  2829. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2830. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2831. MAX_MSG_DATA);
  2832. dev_warn(&((phba->pcidev)->dev),
  2833. "lpfc%d: %s\n",
  2834. phba->brd_no, adaptermsg);
  2835. } else {
  2836. /* Unknown IOCB command */
  2837. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2838. "0334 Unknown IOCB command "
  2839. "Data: x%x, x%x x%x x%x x%x\n",
  2840. type, irsp->ulpCommand,
  2841. irsp->ulpStatus,
  2842. irsp->ulpIoTag,
  2843. irsp->ulpContext);
  2844. }
  2845. break;
  2846. }
  2847. /*
  2848. * The response IOCB has been processed. Update the ring
  2849. * pointer in SLIM. If the port response put pointer has not
  2850. * been updated, sync the pgp->rspPutInx and fetch the new port
  2851. * response put pointer.
  2852. */
  2853. writel(pring->sli.sli3.rspidx,
  2854. &phba->host_gp[pring->ringno].rspGetInx);
  2855. if (pring->sli.sli3.rspidx == portRspPut)
  2856. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2857. }
  2858. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2859. pring->stats.iocb_rsp_full++;
  2860. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2861. writel(status, phba->CAregaddr);
  2862. readl(phba->CAregaddr);
  2863. }
  2864. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2865. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2866. pring->stats.iocb_cmd_empty++;
  2867. /* Force update of the local copy of cmdGetInx */
  2868. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2869. lpfc_sli_resume_iocb(phba, pring);
  2870. if ((pring->lpfc_sli_cmd_available))
  2871. (pring->lpfc_sli_cmd_available) (phba, pring);
  2872. }
  2873. phba->fcp_ring_in_use = 0;
  2874. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2875. return rc;
  2876. }
  2877. /**
  2878. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2879. * @phba: Pointer to HBA context object.
  2880. * @pring: Pointer to driver SLI ring object.
  2881. * @rspiocbp: Pointer to driver response IOCB object.
  2882. *
  2883. * This function is called from the worker thread when there is a slow-path
  2884. * response IOCB to process. This function chains all the response iocbs until
  2885. * seeing the iocb with the LE bit set. The function will call
  2886. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2887. * completion of a command iocb. The function will call the
  2888. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2889. * The function frees the resources or calls the completion handler if this
  2890. * iocb is an abort completion. The function returns NULL when the response
  2891. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2892. * this function shall chain the iocb on to the iocb_continueq and return the
  2893. * response iocb passed in.
  2894. **/
  2895. static struct lpfc_iocbq *
  2896. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2897. struct lpfc_iocbq *rspiocbp)
  2898. {
  2899. struct lpfc_iocbq *saveq;
  2900. struct lpfc_iocbq *cmdiocbp;
  2901. struct lpfc_iocbq *next_iocb;
  2902. IOCB_t *irsp = NULL;
  2903. uint32_t free_saveq;
  2904. uint8_t iocb_cmd_type;
  2905. lpfc_iocb_type type;
  2906. unsigned long iflag;
  2907. int rc;
  2908. spin_lock_irqsave(&phba->hbalock, iflag);
  2909. /* First add the response iocb to the countinueq list */
  2910. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2911. pring->iocb_continueq_cnt++;
  2912. /* Now, determine whether the list is completed for processing */
  2913. irsp = &rspiocbp->iocb;
  2914. if (irsp->ulpLe) {
  2915. /*
  2916. * By default, the driver expects to free all resources
  2917. * associated with this iocb completion.
  2918. */
  2919. free_saveq = 1;
  2920. saveq = list_get_first(&pring->iocb_continueq,
  2921. struct lpfc_iocbq, list);
  2922. irsp = &(saveq->iocb);
  2923. list_del_init(&pring->iocb_continueq);
  2924. pring->iocb_continueq_cnt = 0;
  2925. pring->stats.iocb_rsp++;
  2926. /*
  2927. * If resource errors reported from HBA, reduce
  2928. * queuedepths of the SCSI device.
  2929. */
  2930. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2931. ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
  2932. IOERR_NO_RESOURCES)) {
  2933. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2934. phba->lpfc_rampdown_queue_depth(phba);
  2935. spin_lock_irqsave(&phba->hbalock, iflag);
  2936. }
  2937. if (irsp->ulpStatus) {
  2938. /* Rsp ring <ringno> error: IOCB */
  2939. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2940. "0328 Rsp Ring %d error: "
  2941. "IOCB Data: "
  2942. "x%x x%x x%x x%x "
  2943. "x%x x%x x%x x%x "
  2944. "x%x x%x x%x x%x "
  2945. "x%x x%x x%x x%x\n",
  2946. pring->ringno,
  2947. irsp->un.ulpWord[0],
  2948. irsp->un.ulpWord[1],
  2949. irsp->un.ulpWord[2],
  2950. irsp->un.ulpWord[3],
  2951. irsp->un.ulpWord[4],
  2952. irsp->un.ulpWord[5],
  2953. *(((uint32_t *) irsp) + 6),
  2954. *(((uint32_t *) irsp) + 7),
  2955. *(((uint32_t *) irsp) + 8),
  2956. *(((uint32_t *) irsp) + 9),
  2957. *(((uint32_t *) irsp) + 10),
  2958. *(((uint32_t *) irsp) + 11),
  2959. *(((uint32_t *) irsp) + 12),
  2960. *(((uint32_t *) irsp) + 13),
  2961. *(((uint32_t *) irsp) + 14),
  2962. *(((uint32_t *) irsp) + 15));
  2963. }
  2964. /*
  2965. * Fetch the IOCB command type and call the correct completion
  2966. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2967. * get freed back to the lpfc_iocb_list by the discovery
  2968. * kernel thread.
  2969. */
  2970. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2971. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2972. switch (type) {
  2973. case LPFC_SOL_IOCB:
  2974. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2975. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2976. spin_lock_irqsave(&phba->hbalock, iflag);
  2977. break;
  2978. case LPFC_UNSOL_IOCB:
  2979. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2980. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2981. spin_lock_irqsave(&phba->hbalock, iflag);
  2982. if (!rc)
  2983. free_saveq = 0;
  2984. break;
  2985. case LPFC_ABORT_IOCB:
  2986. cmdiocbp = NULL;
  2987. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2988. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2989. saveq);
  2990. if (cmdiocbp) {
  2991. /* Call the specified completion routine */
  2992. if (cmdiocbp->iocb_cmpl) {
  2993. spin_unlock_irqrestore(&phba->hbalock,
  2994. iflag);
  2995. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2996. saveq);
  2997. spin_lock_irqsave(&phba->hbalock,
  2998. iflag);
  2999. } else
  3000. __lpfc_sli_release_iocbq(phba,
  3001. cmdiocbp);
  3002. }
  3003. break;
  3004. case LPFC_UNKNOWN_IOCB:
  3005. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  3006. char adaptermsg[LPFC_MAX_ADPTMSG];
  3007. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  3008. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  3009. MAX_MSG_DATA);
  3010. dev_warn(&((phba->pcidev)->dev),
  3011. "lpfc%d: %s\n",
  3012. phba->brd_no, adaptermsg);
  3013. } else {
  3014. /* Unknown IOCB command */
  3015. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3016. "0335 Unknown IOCB "
  3017. "command Data: x%x "
  3018. "x%x x%x x%x\n",
  3019. irsp->ulpCommand,
  3020. irsp->ulpStatus,
  3021. irsp->ulpIoTag,
  3022. irsp->ulpContext);
  3023. }
  3024. break;
  3025. }
  3026. if (free_saveq) {
  3027. list_for_each_entry_safe(rspiocbp, next_iocb,
  3028. &saveq->list, list) {
  3029. list_del_init(&rspiocbp->list);
  3030. __lpfc_sli_release_iocbq(phba, rspiocbp);
  3031. }
  3032. __lpfc_sli_release_iocbq(phba, saveq);
  3033. }
  3034. rspiocbp = NULL;
  3035. }
  3036. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3037. return rspiocbp;
  3038. }
  3039. /**
  3040. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  3041. * @phba: Pointer to HBA context object.
  3042. * @pring: Pointer to driver SLI ring object.
  3043. * @mask: Host attention register mask for this ring.
  3044. *
  3045. * This routine wraps the actual slow_ring event process routine from the
  3046. * API jump table function pointer from the lpfc_hba struct.
  3047. **/
  3048. void
  3049. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  3050. struct lpfc_sli_ring *pring, uint32_t mask)
  3051. {
  3052. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  3053. }
  3054. /**
  3055. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  3056. * @phba: Pointer to HBA context object.
  3057. * @pring: Pointer to driver SLI ring object.
  3058. * @mask: Host attention register mask for this ring.
  3059. *
  3060. * This function is called from the worker thread when there is a ring event
  3061. * for non-fcp rings. The caller does not hold any lock. The function will
  3062. * remove each response iocb in the response ring and calls the handle
  3063. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3064. **/
  3065. static void
  3066. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  3067. struct lpfc_sli_ring *pring, uint32_t mask)
  3068. {
  3069. struct lpfc_pgp *pgp;
  3070. IOCB_t *entry;
  3071. IOCB_t *irsp = NULL;
  3072. struct lpfc_iocbq *rspiocbp = NULL;
  3073. uint32_t portRspPut, portRspMax;
  3074. unsigned long iflag;
  3075. uint32_t status;
  3076. pgp = &phba->port_gp[pring->ringno];
  3077. spin_lock_irqsave(&phba->hbalock, iflag);
  3078. pring->stats.iocb_event++;
  3079. /*
  3080. * The next available response entry should never exceed the maximum
  3081. * entries. If it does, treat it as an adapter hardware error.
  3082. */
  3083. portRspMax = pring->sli.sli3.numRiocb;
  3084. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3085. if (portRspPut >= portRspMax) {
  3086. /*
  3087. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  3088. * rsp ring <portRspMax>
  3089. */
  3090. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3091. "0303 Ring %d handler: portRspPut %d "
  3092. "is bigger than rsp ring %d\n",
  3093. pring->ringno, portRspPut, portRspMax);
  3094. phba->link_state = LPFC_HBA_ERROR;
  3095. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3096. phba->work_hs = HS_FFER3;
  3097. lpfc_handle_eratt(phba);
  3098. return;
  3099. }
  3100. rmb();
  3101. while (pring->sli.sli3.rspidx != portRspPut) {
  3102. /*
  3103. * Build a completion list and call the appropriate handler.
  3104. * The process is to get the next available response iocb, get
  3105. * a free iocb from the list, copy the response data into the
  3106. * free iocb, insert to the continuation list, and update the
  3107. * next response index to slim. This process makes response
  3108. * iocb's in the ring available to DMA as fast as possible but
  3109. * pays a penalty for a copy operation. Since the iocb is
  3110. * only 32 bytes, this penalty is considered small relative to
  3111. * the PCI reads for register values and a slim write. When
  3112. * the ulpLe field is set, the entire Command has been
  3113. * received.
  3114. */
  3115. entry = lpfc_resp_iocb(phba, pring);
  3116. phba->last_completion_time = jiffies;
  3117. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3118. if (rspiocbp == NULL) {
  3119. printk(KERN_ERR "%s: out of buffers! Failing "
  3120. "completion.\n", __func__);
  3121. break;
  3122. }
  3123. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3124. phba->iocb_rsp_size);
  3125. irsp = &rspiocbp->iocb;
  3126. if (++pring->sli.sli3.rspidx >= portRspMax)
  3127. pring->sli.sli3.rspidx = 0;
  3128. if (pring->ringno == LPFC_ELS_RING) {
  3129. lpfc_debugfs_slow_ring_trc(phba,
  3130. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3131. *(((uint32_t *) irsp) + 4),
  3132. *(((uint32_t *) irsp) + 6),
  3133. *(((uint32_t *) irsp) + 7));
  3134. }
  3135. writel(pring->sli.sli3.rspidx,
  3136. &phba->host_gp[pring->ringno].rspGetInx);
  3137. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3138. /* Handle the response IOCB */
  3139. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3140. spin_lock_irqsave(&phba->hbalock, iflag);
  3141. /*
  3142. * If the port response put pointer has not been updated, sync
  3143. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3144. * response put pointer.
  3145. */
  3146. if (pring->sli.sli3.rspidx == portRspPut) {
  3147. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3148. }
  3149. } /* while (pring->sli.sli3.rspidx != portRspPut) */
  3150. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3151. /* At least one response entry has been freed */
  3152. pring->stats.iocb_rsp_full++;
  3153. /* SET RxRE_RSP in Chip Att register */
  3154. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3155. writel(status, phba->CAregaddr);
  3156. readl(phba->CAregaddr); /* flush */
  3157. }
  3158. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3159. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3160. pring->stats.iocb_cmd_empty++;
  3161. /* Force update of the local copy of cmdGetInx */
  3162. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3163. lpfc_sli_resume_iocb(phba, pring);
  3164. if ((pring->lpfc_sli_cmd_available))
  3165. (pring->lpfc_sli_cmd_available) (phba, pring);
  3166. }
  3167. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3168. return;
  3169. }
  3170. /**
  3171. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3172. * @phba: Pointer to HBA context object.
  3173. * @pring: Pointer to driver SLI ring object.
  3174. * @mask: Host attention register mask for this ring.
  3175. *
  3176. * This function is called from the worker thread when there is a pending
  3177. * ELS response iocb on the driver internal slow-path response iocb worker
  3178. * queue. The caller does not hold any lock. The function will remove each
  3179. * response iocb from the response worker queue and calls the handle
  3180. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3181. **/
  3182. static void
  3183. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3184. struct lpfc_sli_ring *pring, uint32_t mask)
  3185. {
  3186. struct lpfc_iocbq *irspiocbq;
  3187. struct hbq_dmabuf *dmabuf;
  3188. struct lpfc_cq_event *cq_event;
  3189. unsigned long iflag;
  3190. spin_lock_irqsave(&phba->hbalock, iflag);
  3191. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3192. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3193. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3194. /* Get the response iocb from the head of work queue */
  3195. spin_lock_irqsave(&phba->hbalock, iflag);
  3196. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3197. cq_event, struct lpfc_cq_event, list);
  3198. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3199. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3200. case CQE_CODE_COMPL_WQE:
  3201. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3202. cq_event);
  3203. /* Translate ELS WCQE to response IOCBQ */
  3204. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3205. irspiocbq);
  3206. if (irspiocbq)
  3207. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3208. irspiocbq);
  3209. break;
  3210. case CQE_CODE_RECEIVE:
  3211. case CQE_CODE_RECEIVE_V1:
  3212. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3213. cq_event);
  3214. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3215. break;
  3216. default:
  3217. break;
  3218. }
  3219. }
  3220. }
  3221. /**
  3222. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3223. * @phba: Pointer to HBA context object.
  3224. * @pring: Pointer to driver SLI ring object.
  3225. *
  3226. * This function aborts all iocbs in the given ring and frees all the iocb
  3227. * objects in txq. This function issues an abort iocb for all the iocb commands
  3228. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3229. * the return of this function. The caller is not required to hold any locks.
  3230. **/
  3231. void
  3232. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3233. {
  3234. LIST_HEAD(completions);
  3235. struct lpfc_iocbq *iocb, *next_iocb;
  3236. if (pring->ringno == LPFC_ELS_RING) {
  3237. lpfc_fabric_abort_hba(phba);
  3238. }
  3239. /* Error everything on txq and txcmplq
  3240. * First do the txq.
  3241. */
  3242. if (phba->sli_rev >= LPFC_SLI_REV4) {
  3243. spin_lock_irq(&pring->ring_lock);
  3244. list_splice_init(&pring->txq, &completions);
  3245. pring->txq_cnt = 0;
  3246. spin_unlock_irq(&pring->ring_lock);
  3247. spin_lock_irq(&phba->hbalock);
  3248. /* Next issue ABTS for everything on the txcmplq */
  3249. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3250. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3251. spin_unlock_irq(&phba->hbalock);
  3252. } else {
  3253. spin_lock_irq(&phba->hbalock);
  3254. list_splice_init(&pring->txq, &completions);
  3255. pring->txq_cnt = 0;
  3256. /* Next issue ABTS for everything on the txcmplq */
  3257. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3258. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3259. spin_unlock_irq(&phba->hbalock);
  3260. }
  3261. /* Cancel all the IOCBs from the completions list */
  3262. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3263. IOERR_SLI_ABORTED);
  3264. }
  3265. /**
  3266. * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
  3267. * @phba: Pointer to HBA context object.
  3268. * @pring: Pointer to driver SLI ring object.
  3269. *
  3270. * This function aborts all iocbs in FCP rings and frees all the iocb
  3271. * objects in txq. This function issues an abort iocb for all the iocb commands
  3272. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3273. * the return of this function. The caller is not required to hold any locks.
  3274. **/
  3275. void
  3276. lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
  3277. {
  3278. struct lpfc_sli *psli = &phba->sli;
  3279. struct lpfc_sli_ring *pring;
  3280. uint32_t i;
  3281. /* Look on all the FCP Rings for the iotag */
  3282. if (phba->sli_rev >= LPFC_SLI_REV4) {
  3283. for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
  3284. pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
  3285. lpfc_sli_abort_iocb_ring(phba, pring);
  3286. }
  3287. } else {
  3288. pring = &psli->ring[psli->fcp_ring];
  3289. lpfc_sli_abort_iocb_ring(phba, pring);
  3290. }
  3291. }
  3292. /**
  3293. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3294. * @phba: Pointer to HBA context object.
  3295. *
  3296. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3297. * objects in txq and txcmplq. This function will not issue abort iocbs
  3298. * for all the iocb commands in txcmplq, they will just be returned with
  3299. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3300. * slot has been permanently disabled.
  3301. **/
  3302. void
  3303. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3304. {
  3305. LIST_HEAD(txq);
  3306. LIST_HEAD(txcmplq);
  3307. struct lpfc_sli *psli = &phba->sli;
  3308. struct lpfc_sli_ring *pring;
  3309. uint32_t i;
  3310. spin_lock_irq(&phba->hbalock);
  3311. /* Indicate the I/O queues are flushed */
  3312. phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
  3313. spin_unlock_irq(&phba->hbalock);
  3314. /* Look on all the FCP Rings for the iotag */
  3315. if (phba->sli_rev >= LPFC_SLI_REV4) {
  3316. for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
  3317. pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
  3318. spin_lock_irq(&pring->ring_lock);
  3319. /* Retrieve everything on txq */
  3320. list_splice_init(&pring->txq, &txq);
  3321. /* Retrieve everything on the txcmplq */
  3322. list_splice_init(&pring->txcmplq, &txcmplq);
  3323. pring->txq_cnt = 0;
  3324. pring->txcmplq_cnt = 0;
  3325. spin_unlock_irq(&pring->ring_lock);
  3326. /* Flush the txq */
  3327. lpfc_sli_cancel_iocbs(phba, &txq,
  3328. IOSTAT_LOCAL_REJECT,
  3329. IOERR_SLI_DOWN);
  3330. /* Flush the txcmpq */
  3331. lpfc_sli_cancel_iocbs(phba, &txcmplq,
  3332. IOSTAT_LOCAL_REJECT,
  3333. IOERR_SLI_DOWN);
  3334. }
  3335. } else {
  3336. pring = &psli->ring[psli->fcp_ring];
  3337. spin_lock_irq(&phba->hbalock);
  3338. /* Retrieve everything on txq */
  3339. list_splice_init(&pring->txq, &txq);
  3340. /* Retrieve everything on the txcmplq */
  3341. list_splice_init(&pring->txcmplq, &txcmplq);
  3342. pring->txq_cnt = 0;
  3343. pring->txcmplq_cnt = 0;
  3344. spin_unlock_irq(&phba->hbalock);
  3345. /* Flush the txq */
  3346. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3347. IOERR_SLI_DOWN);
  3348. /* Flush the txcmpq */
  3349. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3350. IOERR_SLI_DOWN);
  3351. }
  3352. }
  3353. /**
  3354. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3355. * @phba: Pointer to HBA context object.
  3356. * @mask: Bit mask to be checked.
  3357. *
  3358. * This function reads the host status register and compares
  3359. * with the provided bit mask to check if HBA completed
  3360. * the restart. This function will wait in a loop for the
  3361. * HBA to complete restart. If the HBA does not restart within
  3362. * 15 iterations, the function will reset the HBA again. The
  3363. * function returns 1 when HBA fail to restart otherwise returns
  3364. * zero.
  3365. **/
  3366. static int
  3367. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3368. {
  3369. uint32_t status;
  3370. int i = 0;
  3371. int retval = 0;
  3372. /* Read the HBA Host Status Register */
  3373. if (lpfc_readl(phba->HSregaddr, &status))
  3374. return 1;
  3375. /*
  3376. * Check status register every 100ms for 5 retries, then every
  3377. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3378. * every 2.5 sec for 4.
  3379. * Break our of the loop if errors occurred during init.
  3380. */
  3381. while (((status & mask) != mask) &&
  3382. !(status & HS_FFERM) &&
  3383. i++ < 20) {
  3384. if (i <= 5)
  3385. msleep(10);
  3386. else if (i <= 10)
  3387. msleep(500);
  3388. else
  3389. msleep(2500);
  3390. if (i == 15) {
  3391. /* Do post */
  3392. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3393. lpfc_sli_brdrestart(phba);
  3394. }
  3395. /* Read the HBA Host Status Register */
  3396. if (lpfc_readl(phba->HSregaddr, &status)) {
  3397. retval = 1;
  3398. break;
  3399. }
  3400. }
  3401. /* Check to see if any errors occurred during init */
  3402. if ((status & HS_FFERM) || (i >= 20)) {
  3403. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3404. "2751 Adapter failed to restart, "
  3405. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3406. status,
  3407. readl(phba->MBslimaddr + 0xa8),
  3408. readl(phba->MBslimaddr + 0xac));
  3409. phba->link_state = LPFC_HBA_ERROR;
  3410. retval = 1;
  3411. }
  3412. return retval;
  3413. }
  3414. /**
  3415. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3416. * @phba: Pointer to HBA context object.
  3417. * @mask: Bit mask to be checked.
  3418. *
  3419. * This function checks the host status register to check if HBA is
  3420. * ready. This function will wait in a loop for the HBA to be ready
  3421. * If the HBA is not ready , the function will will reset the HBA PCI
  3422. * function again. The function returns 1 when HBA fail to be ready
  3423. * otherwise returns zero.
  3424. **/
  3425. static int
  3426. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3427. {
  3428. uint32_t status;
  3429. int retval = 0;
  3430. /* Read the HBA Host Status Register */
  3431. status = lpfc_sli4_post_status_check(phba);
  3432. if (status) {
  3433. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3434. lpfc_sli_brdrestart(phba);
  3435. status = lpfc_sli4_post_status_check(phba);
  3436. }
  3437. /* Check to see if any errors occurred during init */
  3438. if (status) {
  3439. phba->link_state = LPFC_HBA_ERROR;
  3440. retval = 1;
  3441. } else
  3442. phba->sli4_hba.intr_enable = 0;
  3443. return retval;
  3444. }
  3445. /**
  3446. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3447. * @phba: Pointer to HBA context object.
  3448. * @mask: Bit mask to be checked.
  3449. *
  3450. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3451. * from the API jump table function pointer from the lpfc_hba struct.
  3452. **/
  3453. int
  3454. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3455. {
  3456. return phba->lpfc_sli_brdready(phba, mask);
  3457. }
  3458. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3459. /**
  3460. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3461. * @phba: Pointer to HBA context object.
  3462. *
  3463. * This function is called before resetting an HBA. This function is called
  3464. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3465. **/
  3466. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3467. {
  3468. uint32_t __iomem *resp_buf;
  3469. uint32_t __iomem *mbox_buf;
  3470. volatile uint32_t mbox;
  3471. uint32_t hc_copy, ha_copy, resp_data;
  3472. int i;
  3473. uint8_t hdrtype;
  3474. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3475. if (hdrtype != 0x80 ||
  3476. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3477. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3478. return;
  3479. /*
  3480. * Tell the other part of the chip to suspend temporarily all
  3481. * its DMA activity.
  3482. */
  3483. resp_buf = phba->MBslimaddr;
  3484. /* Disable the error attention */
  3485. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3486. return;
  3487. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3488. readl(phba->HCregaddr); /* flush */
  3489. phba->link_flag |= LS_IGNORE_ERATT;
  3490. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3491. return;
  3492. if (ha_copy & HA_ERATT) {
  3493. /* Clear Chip error bit */
  3494. writel(HA_ERATT, phba->HAregaddr);
  3495. phba->pport->stopped = 1;
  3496. }
  3497. mbox = 0;
  3498. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3499. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3500. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3501. mbox_buf = phba->MBslimaddr;
  3502. writel(mbox, mbox_buf);
  3503. for (i = 0; i < 50; i++) {
  3504. if (lpfc_readl((resp_buf + 1), &resp_data))
  3505. return;
  3506. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3507. mdelay(1);
  3508. else
  3509. break;
  3510. }
  3511. resp_data = 0;
  3512. if (lpfc_readl((resp_buf + 1), &resp_data))
  3513. return;
  3514. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3515. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3516. phba->pport->stopped)
  3517. goto restore_hc;
  3518. else
  3519. goto clear_errat;
  3520. }
  3521. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3522. resp_data = 0;
  3523. for (i = 0; i < 500; i++) {
  3524. if (lpfc_readl(resp_buf, &resp_data))
  3525. return;
  3526. if (resp_data != mbox)
  3527. mdelay(1);
  3528. else
  3529. break;
  3530. }
  3531. clear_errat:
  3532. while (++i < 500) {
  3533. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3534. return;
  3535. if (!(ha_copy & HA_ERATT))
  3536. mdelay(1);
  3537. else
  3538. break;
  3539. }
  3540. if (readl(phba->HAregaddr) & HA_ERATT) {
  3541. writel(HA_ERATT, phba->HAregaddr);
  3542. phba->pport->stopped = 1;
  3543. }
  3544. restore_hc:
  3545. phba->link_flag &= ~LS_IGNORE_ERATT;
  3546. writel(hc_copy, phba->HCregaddr);
  3547. readl(phba->HCregaddr); /* flush */
  3548. }
  3549. /**
  3550. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3551. * @phba: Pointer to HBA context object.
  3552. *
  3553. * This function issues a kill_board mailbox command and waits for
  3554. * the error attention interrupt. This function is called for stopping
  3555. * the firmware processing. The caller is not required to hold any
  3556. * locks. This function calls lpfc_hba_down_post function to free
  3557. * any pending commands after the kill. The function will return 1 when it
  3558. * fails to kill the board else will return 0.
  3559. **/
  3560. int
  3561. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3562. {
  3563. struct lpfc_sli *psli;
  3564. LPFC_MBOXQ_t *pmb;
  3565. uint32_t status;
  3566. uint32_t ha_copy;
  3567. int retval;
  3568. int i = 0;
  3569. psli = &phba->sli;
  3570. /* Kill HBA */
  3571. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3572. "0329 Kill HBA Data: x%x x%x\n",
  3573. phba->pport->port_state, psli->sli_flag);
  3574. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3575. if (!pmb)
  3576. return 1;
  3577. /* Disable the error attention */
  3578. spin_lock_irq(&phba->hbalock);
  3579. if (lpfc_readl(phba->HCregaddr, &status)) {
  3580. spin_unlock_irq(&phba->hbalock);
  3581. mempool_free(pmb, phba->mbox_mem_pool);
  3582. return 1;
  3583. }
  3584. status &= ~HC_ERINT_ENA;
  3585. writel(status, phba->HCregaddr);
  3586. readl(phba->HCregaddr); /* flush */
  3587. phba->link_flag |= LS_IGNORE_ERATT;
  3588. spin_unlock_irq(&phba->hbalock);
  3589. lpfc_kill_board(phba, pmb);
  3590. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3591. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3592. if (retval != MBX_SUCCESS) {
  3593. if (retval != MBX_BUSY)
  3594. mempool_free(pmb, phba->mbox_mem_pool);
  3595. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3596. "2752 KILL_BOARD command failed retval %d\n",
  3597. retval);
  3598. spin_lock_irq(&phba->hbalock);
  3599. phba->link_flag &= ~LS_IGNORE_ERATT;
  3600. spin_unlock_irq(&phba->hbalock);
  3601. return 1;
  3602. }
  3603. spin_lock_irq(&phba->hbalock);
  3604. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3605. spin_unlock_irq(&phba->hbalock);
  3606. mempool_free(pmb, phba->mbox_mem_pool);
  3607. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3608. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3609. * 3 seconds we still set HBA_ERROR state because the status of the
  3610. * board is now undefined.
  3611. */
  3612. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3613. return 1;
  3614. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3615. mdelay(100);
  3616. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3617. return 1;
  3618. }
  3619. del_timer_sync(&psli->mbox_tmo);
  3620. if (ha_copy & HA_ERATT) {
  3621. writel(HA_ERATT, phba->HAregaddr);
  3622. phba->pport->stopped = 1;
  3623. }
  3624. spin_lock_irq(&phba->hbalock);
  3625. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3626. psli->mbox_active = NULL;
  3627. phba->link_flag &= ~LS_IGNORE_ERATT;
  3628. spin_unlock_irq(&phba->hbalock);
  3629. lpfc_hba_down_post(phba);
  3630. phba->link_state = LPFC_HBA_ERROR;
  3631. return ha_copy & HA_ERATT ? 0 : 1;
  3632. }
  3633. /**
  3634. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3635. * @phba: Pointer to HBA context object.
  3636. *
  3637. * This function resets the HBA by writing HC_INITFF to the control
  3638. * register. After the HBA resets, this function resets all the iocb ring
  3639. * indices. This function disables PCI layer parity checking during
  3640. * the reset.
  3641. * This function returns 0 always.
  3642. * The caller is not required to hold any locks.
  3643. **/
  3644. int
  3645. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3646. {
  3647. struct lpfc_sli *psli;
  3648. struct lpfc_sli_ring *pring;
  3649. uint16_t cfg_value;
  3650. int i;
  3651. psli = &phba->sli;
  3652. /* Reset HBA */
  3653. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3654. "0325 Reset HBA Data: x%x x%x\n",
  3655. phba->pport->port_state, psli->sli_flag);
  3656. /* perform board reset */
  3657. phba->fc_eventTag = 0;
  3658. phba->link_events = 0;
  3659. phba->pport->fc_myDID = 0;
  3660. phba->pport->fc_prevDID = 0;
  3661. /* Turn off parity checking and serr during the physical reset */
  3662. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3663. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3664. (cfg_value &
  3665. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3666. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3667. /* Now toggle INITFF bit in the Host Control Register */
  3668. writel(HC_INITFF, phba->HCregaddr);
  3669. mdelay(1);
  3670. readl(phba->HCregaddr); /* flush */
  3671. writel(0, phba->HCregaddr);
  3672. readl(phba->HCregaddr); /* flush */
  3673. /* Restore PCI cmd register */
  3674. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3675. /* Initialize relevant SLI info */
  3676. for (i = 0; i < psli->num_rings; i++) {
  3677. pring = &psli->ring[i];
  3678. pring->flag = 0;
  3679. pring->sli.sli3.rspidx = 0;
  3680. pring->sli.sli3.next_cmdidx = 0;
  3681. pring->sli.sli3.local_getidx = 0;
  3682. pring->sli.sli3.cmdidx = 0;
  3683. pring->missbufcnt = 0;
  3684. }
  3685. phba->link_state = LPFC_WARM_START;
  3686. return 0;
  3687. }
  3688. /**
  3689. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3690. * @phba: Pointer to HBA context object.
  3691. *
  3692. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3693. * checking during resets the device. The caller is not required to hold
  3694. * any locks.
  3695. *
  3696. * This function returns 0 always.
  3697. **/
  3698. int
  3699. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3700. {
  3701. struct lpfc_sli *psli = &phba->sli;
  3702. uint16_t cfg_value;
  3703. int rc = 0;
  3704. /* Reset HBA */
  3705. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3706. "0295 Reset HBA Data: x%x x%x x%x\n",
  3707. phba->pport->port_state, psli->sli_flag,
  3708. phba->hba_flag);
  3709. /* perform board reset */
  3710. phba->fc_eventTag = 0;
  3711. phba->link_events = 0;
  3712. phba->pport->fc_myDID = 0;
  3713. phba->pport->fc_prevDID = 0;
  3714. spin_lock_irq(&phba->hbalock);
  3715. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3716. phba->fcf.fcf_flag = 0;
  3717. spin_unlock_irq(&phba->hbalock);
  3718. /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
  3719. if (phba->hba_flag & HBA_FW_DUMP_OP) {
  3720. phba->hba_flag &= ~HBA_FW_DUMP_OP;
  3721. return rc;
  3722. }
  3723. /* Now physically reset the device */
  3724. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3725. "0389 Performing PCI function reset!\n");
  3726. /* Turn off parity checking and serr during the physical reset */
  3727. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3728. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3729. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3730. /* Perform FCoE PCI function reset before freeing queue memory */
  3731. rc = lpfc_pci_function_reset(phba);
  3732. lpfc_sli4_queue_destroy(phba);
  3733. /* Restore PCI cmd register */
  3734. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3735. return rc;
  3736. }
  3737. /**
  3738. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3739. * @phba: Pointer to HBA context object.
  3740. *
  3741. * This function is called in the SLI initialization code path to
  3742. * restart the HBA. The caller is not required to hold any lock.
  3743. * This function writes MBX_RESTART mailbox command to the SLIM and
  3744. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3745. * function to free any pending commands. The function enables
  3746. * POST only during the first initialization. The function returns zero.
  3747. * The function does not guarantee completion of MBX_RESTART mailbox
  3748. * command before the return of this function.
  3749. **/
  3750. static int
  3751. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3752. {
  3753. MAILBOX_t *mb;
  3754. struct lpfc_sli *psli;
  3755. volatile uint32_t word0;
  3756. void __iomem *to_slim;
  3757. uint32_t hba_aer_enabled;
  3758. spin_lock_irq(&phba->hbalock);
  3759. /* Take PCIe device Advanced Error Reporting (AER) state */
  3760. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3761. psli = &phba->sli;
  3762. /* Restart HBA */
  3763. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3764. "0337 Restart HBA Data: x%x x%x\n",
  3765. phba->pport->port_state, psli->sli_flag);
  3766. word0 = 0;
  3767. mb = (MAILBOX_t *) &word0;
  3768. mb->mbxCommand = MBX_RESTART;
  3769. mb->mbxHc = 1;
  3770. lpfc_reset_barrier(phba);
  3771. to_slim = phba->MBslimaddr;
  3772. writel(*(uint32_t *) mb, to_slim);
  3773. readl(to_slim); /* flush */
  3774. /* Only skip post after fc_ffinit is completed */
  3775. if (phba->pport->port_state)
  3776. word0 = 1; /* This is really setting up word1 */
  3777. else
  3778. word0 = 0; /* This is really setting up word1 */
  3779. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3780. writel(*(uint32_t *) mb, to_slim);
  3781. readl(to_slim); /* flush */
  3782. lpfc_sli_brdreset(phba);
  3783. phba->pport->stopped = 0;
  3784. phba->link_state = LPFC_INIT_START;
  3785. phba->hba_flag = 0;
  3786. spin_unlock_irq(&phba->hbalock);
  3787. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3788. psli->stats_start = get_seconds();
  3789. /* Give the INITFF and Post time to settle. */
  3790. mdelay(100);
  3791. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3792. if (hba_aer_enabled)
  3793. pci_disable_pcie_error_reporting(phba->pcidev);
  3794. lpfc_hba_down_post(phba);
  3795. return 0;
  3796. }
  3797. /**
  3798. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3799. * @phba: Pointer to HBA context object.
  3800. *
  3801. * This function is called in the SLI initialization code path to restart
  3802. * a SLI4 HBA. The caller is not required to hold any lock.
  3803. * At the end of the function, it calls lpfc_hba_down_post function to
  3804. * free any pending commands.
  3805. **/
  3806. static int
  3807. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3808. {
  3809. struct lpfc_sli *psli = &phba->sli;
  3810. uint32_t hba_aer_enabled;
  3811. int rc;
  3812. /* Restart HBA */
  3813. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3814. "0296 Restart HBA Data: x%x x%x\n",
  3815. phba->pport->port_state, psli->sli_flag);
  3816. /* Take PCIe device Advanced Error Reporting (AER) state */
  3817. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3818. rc = lpfc_sli4_brdreset(phba);
  3819. spin_lock_irq(&phba->hbalock);
  3820. phba->pport->stopped = 0;
  3821. phba->link_state = LPFC_INIT_START;
  3822. phba->hba_flag = 0;
  3823. spin_unlock_irq(&phba->hbalock);
  3824. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3825. psli->stats_start = get_seconds();
  3826. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3827. if (hba_aer_enabled)
  3828. pci_disable_pcie_error_reporting(phba->pcidev);
  3829. lpfc_hba_down_post(phba);
  3830. return rc;
  3831. }
  3832. /**
  3833. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3834. * @phba: Pointer to HBA context object.
  3835. *
  3836. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3837. * API jump table function pointer from the lpfc_hba struct.
  3838. **/
  3839. int
  3840. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3841. {
  3842. return phba->lpfc_sli_brdrestart(phba);
  3843. }
  3844. /**
  3845. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3846. * @phba: Pointer to HBA context object.
  3847. *
  3848. * This function is called after a HBA restart to wait for successful
  3849. * restart of the HBA. Successful restart of the HBA is indicated by
  3850. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3851. * iteration, the function will restart the HBA again. The function returns
  3852. * zero if HBA successfully restarted else returns negative error code.
  3853. **/
  3854. static int
  3855. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3856. {
  3857. uint32_t status, i = 0;
  3858. /* Read the HBA Host Status Register */
  3859. if (lpfc_readl(phba->HSregaddr, &status))
  3860. return -EIO;
  3861. /* Check status register to see what current state is */
  3862. i = 0;
  3863. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3864. /* Check every 10ms for 10 retries, then every 100ms for 90
  3865. * retries, then every 1 sec for 50 retires for a total of
  3866. * ~60 seconds before reset the board again and check every
  3867. * 1 sec for 50 retries. The up to 60 seconds before the
  3868. * board ready is required by the Falcon FIPS zeroization
  3869. * complete, and any reset the board in between shall cause
  3870. * restart of zeroization, further delay the board ready.
  3871. */
  3872. if (i++ >= 200) {
  3873. /* Adapter failed to init, timeout, status reg
  3874. <status> */
  3875. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3876. "0436 Adapter failed to init, "
  3877. "timeout, status reg x%x, "
  3878. "FW Data: A8 x%x AC x%x\n", status,
  3879. readl(phba->MBslimaddr + 0xa8),
  3880. readl(phba->MBslimaddr + 0xac));
  3881. phba->link_state = LPFC_HBA_ERROR;
  3882. return -ETIMEDOUT;
  3883. }
  3884. /* Check to see if any errors occurred during init */
  3885. if (status & HS_FFERM) {
  3886. /* ERROR: During chipset initialization */
  3887. /* Adapter failed to init, chipset, status reg
  3888. <status> */
  3889. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3890. "0437 Adapter failed to init, "
  3891. "chipset, status reg x%x, "
  3892. "FW Data: A8 x%x AC x%x\n", status,
  3893. readl(phba->MBslimaddr + 0xa8),
  3894. readl(phba->MBslimaddr + 0xac));
  3895. phba->link_state = LPFC_HBA_ERROR;
  3896. return -EIO;
  3897. }
  3898. if (i <= 10)
  3899. msleep(10);
  3900. else if (i <= 100)
  3901. msleep(100);
  3902. else
  3903. msleep(1000);
  3904. if (i == 150) {
  3905. /* Do post */
  3906. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3907. lpfc_sli_brdrestart(phba);
  3908. }
  3909. /* Read the HBA Host Status Register */
  3910. if (lpfc_readl(phba->HSregaddr, &status))
  3911. return -EIO;
  3912. }
  3913. /* Check to see if any errors occurred during init */
  3914. if (status & HS_FFERM) {
  3915. /* ERROR: During chipset initialization */
  3916. /* Adapter failed to init, chipset, status reg <status> */
  3917. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3918. "0438 Adapter failed to init, chipset, "
  3919. "status reg x%x, "
  3920. "FW Data: A8 x%x AC x%x\n", status,
  3921. readl(phba->MBslimaddr + 0xa8),
  3922. readl(phba->MBslimaddr + 0xac));
  3923. phba->link_state = LPFC_HBA_ERROR;
  3924. return -EIO;
  3925. }
  3926. /* Clear all interrupt enable conditions */
  3927. writel(0, phba->HCregaddr);
  3928. readl(phba->HCregaddr); /* flush */
  3929. /* setup host attn register */
  3930. writel(0xffffffff, phba->HAregaddr);
  3931. readl(phba->HAregaddr); /* flush */
  3932. return 0;
  3933. }
  3934. /**
  3935. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3936. *
  3937. * This function calculates and returns the number of HBQs required to be
  3938. * configured.
  3939. **/
  3940. int
  3941. lpfc_sli_hbq_count(void)
  3942. {
  3943. return ARRAY_SIZE(lpfc_hbq_defs);
  3944. }
  3945. /**
  3946. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3947. *
  3948. * This function adds the number of hbq entries in every HBQ to get
  3949. * the total number of hbq entries required for the HBA and returns
  3950. * the total count.
  3951. **/
  3952. static int
  3953. lpfc_sli_hbq_entry_count(void)
  3954. {
  3955. int hbq_count = lpfc_sli_hbq_count();
  3956. int count = 0;
  3957. int i;
  3958. for (i = 0; i < hbq_count; ++i)
  3959. count += lpfc_hbq_defs[i]->entry_count;
  3960. return count;
  3961. }
  3962. /**
  3963. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3964. *
  3965. * This function calculates amount of memory required for all hbq entries
  3966. * to be configured and returns the total memory required.
  3967. **/
  3968. int
  3969. lpfc_sli_hbq_size(void)
  3970. {
  3971. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3972. }
  3973. /**
  3974. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3975. * @phba: Pointer to HBA context object.
  3976. *
  3977. * This function is called during the SLI initialization to configure
  3978. * all the HBQs and post buffers to the HBQ. The caller is not
  3979. * required to hold any locks. This function will return zero if successful
  3980. * else it will return negative error code.
  3981. **/
  3982. static int
  3983. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3984. {
  3985. int hbq_count = lpfc_sli_hbq_count();
  3986. LPFC_MBOXQ_t *pmb;
  3987. MAILBOX_t *pmbox;
  3988. uint32_t hbqno;
  3989. uint32_t hbq_entry_index;
  3990. /* Get a Mailbox buffer to setup mailbox
  3991. * commands for HBA initialization
  3992. */
  3993. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3994. if (!pmb)
  3995. return -ENOMEM;
  3996. pmbox = &pmb->u.mb;
  3997. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3998. phba->link_state = LPFC_INIT_MBX_CMDS;
  3999. phba->hbq_in_use = 1;
  4000. hbq_entry_index = 0;
  4001. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  4002. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  4003. phba->hbqs[hbqno].hbqPutIdx = 0;
  4004. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  4005. phba->hbqs[hbqno].entry_count =
  4006. lpfc_hbq_defs[hbqno]->entry_count;
  4007. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  4008. hbq_entry_index, pmb);
  4009. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  4010. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  4011. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  4012. mbxStatus <status>, ring <num> */
  4013. lpfc_printf_log(phba, KERN_ERR,
  4014. LOG_SLI | LOG_VPORT,
  4015. "1805 Adapter failed to init. "
  4016. "Data: x%x x%x x%x\n",
  4017. pmbox->mbxCommand,
  4018. pmbox->mbxStatus, hbqno);
  4019. phba->link_state = LPFC_HBA_ERROR;
  4020. mempool_free(pmb, phba->mbox_mem_pool);
  4021. return -ENXIO;
  4022. }
  4023. }
  4024. phba->hbq_count = hbq_count;
  4025. mempool_free(pmb, phba->mbox_mem_pool);
  4026. /* Initially populate or replenish the HBQs */
  4027. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  4028. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  4029. return 0;
  4030. }
  4031. /**
  4032. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  4033. * @phba: Pointer to HBA context object.
  4034. *
  4035. * This function is called during the SLI initialization to configure
  4036. * all the HBQs and post buffers to the HBQ. The caller is not
  4037. * required to hold any locks. This function will return zero if successful
  4038. * else it will return negative error code.
  4039. **/
  4040. static int
  4041. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  4042. {
  4043. phba->hbq_in_use = 1;
  4044. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  4045. phba->hbq_count = 1;
  4046. /* Initially populate or replenish the HBQs */
  4047. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  4048. return 0;
  4049. }
  4050. /**
  4051. * lpfc_sli_config_port - Issue config port mailbox command
  4052. * @phba: Pointer to HBA context object.
  4053. * @sli_mode: sli mode - 2/3
  4054. *
  4055. * This function is called by the sli intialization code path
  4056. * to issue config_port mailbox command. This function restarts the
  4057. * HBA firmware and issues a config_port mailbox command to configure
  4058. * the SLI interface in the sli mode specified by sli_mode
  4059. * variable. The caller is not required to hold any locks.
  4060. * The function returns 0 if successful, else returns negative error
  4061. * code.
  4062. **/
  4063. int
  4064. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  4065. {
  4066. LPFC_MBOXQ_t *pmb;
  4067. uint32_t resetcount = 0, rc = 0, done = 0;
  4068. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4069. if (!pmb) {
  4070. phba->link_state = LPFC_HBA_ERROR;
  4071. return -ENOMEM;
  4072. }
  4073. phba->sli_rev = sli_mode;
  4074. while (resetcount < 2 && !done) {
  4075. spin_lock_irq(&phba->hbalock);
  4076. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  4077. spin_unlock_irq(&phba->hbalock);
  4078. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  4079. lpfc_sli_brdrestart(phba);
  4080. rc = lpfc_sli_chipset_init(phba);
  4081. if (rc)
  4082. break;
  4083. spin_lock_irq(&phba->hbalock);
  4084. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4085. spin_unlock_irq(&phba->hbalock);
  4086. resetcount++;
  4087. /* Call pre CONFIG_PORT mailbox command initialization. A
  4088. * value of 0 means the call was successful. Any other
  4089. * nonzero value is a failure, but if ERESTART is returned,
  4090. * the driver may reset the HBA and try again.
  4091. */
  4092. rc = lpfc_config_port_prep(phba);
  4093. if (rc == -ERESTART) {
  4094. phba->link_state = LPFC_LINK_UNKNOWN;
  4095. continue;
  4096. } else if (rc)
  4097. break;
  4098. phba->link_state = LPFC_INIT_MBX_CMDS;
  4099. lpfc_config_port(phba, pmb);
  4100. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  4101. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  4102. LPFC_SLI3_HBQ_ENABLED |
  4103. LPFC_SLI3_CRP_ENABLED |
  4104. LPFC_SLI3_BG_ENABLED |
  4105. LPFC_SLI3_DSS_ENABLED);
  4106. if (rc != MBX_SUCCESS) {
  4107. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4108. "0442 Adapter failed to init, mbxCmd x%x "
  4109. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  4110. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  4111. spin_lock_irq(&phba->hbalock);
  4112. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  4113. spin_unlock_irq(&phba->hbalock);
  4114. rc = -ENXIO;
  4115. } else {
  4116. /* Allow asynchronous mailbox command to go through */
  4117. spin_lock_irq(&phba->hbalock);
  4118. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  4119. spin_unlock_irq(&phba->hbalock);
  4120. done = 1;
  4121. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  4122. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  4123. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  4124. "3110 Port did not grant ASABT\n");
  4125. }
  4126. }
  4127. if (!done) {
  4128. rc = -EINVAL;
  4129. goto do_prep_failed;
  4130. }
  4131. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  4132. if (!pmb->u.mb.un.varCfgPort.cMA) {
  4133. rc = -ENXIO;
  4134. goto do_prep_failed;
  4135. }
  4136. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  4137. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  4138. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  4139. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  4140. phba->max_vpi : phba->max_vports;
  4141. } else
  4142. phba->max_vpi = 0;
  4143. phba->fips_level = 0;
  4144. phba->fips_spec_rev = 0;
  4145. if (pmb->u.mb.un.varCfgPort.gdss) {
  4146. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  4147. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  4148. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  4149. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4150. "2850 Security Crypto Active. FIPS x%d "
  4151. "(Spec Rev: x%d)",
  4152. phba->fips_level, phba->fips_spec_rev);
  4153. }
  4154. if (pmb->u.mb.un.varCfgPort.sec_err) {
  4155. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4156. "2856 Config Port Security Crypto "
  4157. "Error: x%x ",
  4158. pmb->u.mb.un.varCfgPort.sec_err);
  4159. }
  4160. if (pmb->u.mb.un.varCfgPort.gerbm)
  4161. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  4162. if (pmb->u.mb.un.varCfgPort.gcrp)
  4163. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  4164. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  4165. phba->port_gp = phba->mbox->us.s3_pgp.port;
  4166. if (phba->cfg_enable_bg) {
  4167. if (pmb->u.mb.un.varCfgPort.gbg)
  4168. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  4169. else
  4170. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4171. "0443 Adapter did not grant "
  4172. "BlockGuard\n");
  4173. }
  4174. } else {
  4175. phba->hbq_get = NULL;
  4176. phba->port_gp = phba->mbox->us.s2.port;
  4177. phba->max_vpi = 0;
  4178. }
  4179. do_prep_failed:
  4180. mempool_free(pmb, phba->mbox_mem_pool);
  4181. return rc;
  4182. }
  4183. /**
  4184. * lpfc_sli_hba_setup - SLI intialization function
  4185. * @phba: Pointer to HBA context object.
  4186. *
  4187. * This function is the main SLI intialization function. This function
  4188. * is called by the HBA intialization code, HBA reset code and HBA
  4189. * error attention handler code. Caller is not required to hold any
  4190. * locks. This function issues config_port mailbox command to configure
  4191. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4192. * calls the config_port_post function to issue init_link mailbox
  4193. * command and to start the discovery. The function will return zero
  4194. * if successful, else it will return negative error code.
  4195. **/
  4196. int
  4197. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4198. {
  4199. uint32_t rc;
  4200. int mode = 3, i;
  4201. int longs;
  4202. switch (lpfc_sli_mode) {
  4203. case 2:
  4204. if (phba->cfg_enable_npiv) {
  4205. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4206. "1824 NPIV enabled: Override lpfc_sli_mode "
  4207. "parameter (%d) to auto (0).\n",
  4208. lpfc_sli_mode);
  4209. break;
  4210. }
  4211. mode = 2;
  4212. break;
  4213. case 0:
  4214. case 3:
  4215. break;
  4216. default:
  4217. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4218. "1819 Unrecognized lpfc_sli_mode "
  4219. "parameter: %d.\n", lpfc_sli_mode);
  4220. break;
  4221. }
  4222. rc = lpfc_sli_config_port(phba, mode);
  4223. if (rc && lpfc_sli_mode == 3)
  4224. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4225. "1820 Unable to select SLI-3. "
  4226. "Not supported by adapter.\n");
  4227. if (rc && mode != 2)
  4228. rc = lpfc_sli_config_port(phba, 2);
  4229. if (rc)
  4230. goto lpfc_sli_hba_setup_error;
  4231. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4232. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4233. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4234. if (!rc) {
  4235. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4236. "2709 This device supports "
  4237. "Advanced Error Reporting (AER)\n");
  4238. spin_lock_irq(&phba->hbalock);
  4239. phba->hba_flag |= HBA_AER_ENABLED;
  4240. spin_unlock_irq(&phba->hbalock);
  4241. } else {
  4242. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4243. "2708 This device does not support "
  4244. "Advanced Error Reporting (AER): %d\n",
  4245. rc);
  4246. phba->cfg_aer_support = 0;
  4247. }
  4248. }
  4249. if (phba->sli_rev == 3) {
  4250. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4251. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4252. } else {
  4253. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4254. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4255. phba->sli3_options = 0;
  4256. }
  4257. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4258. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4259. phba->sli_rev, phba->max_vpi);
  4260. rc = lpfc_sli_ring_map(phba);
  4261. if (rc)
  4262. goto lpfc_sli_hba_setup_error;
  4263. /* Initialize VPIs. */
  4264. if (phba->sli_rev == LPFC_SLI_REV3) {
  4265. /*
  4266. * The VPI bitmask and physical ID array are allocated
  4267. * and initialized once only - at driver load. A port
  4268. * reset doesn't need to reinitialize this memory.
  4269. */
  4270. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4271. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4272. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4273. GFP_KERNEL);
  4274. if (!phba->vpi_bmask) {
  4275. rc = -ENOMEM;
  4276. goto lpfc_sli_hba_setup_error;
  4277. }
  4278. phba->vpi_ids = kzalloc(
  4279. (phba->max_vpi+1) * sizeof(uint16_t),
  4280. GFP_KERNEL);
  4281. if (!phba->vpi_ids) {
  4282. kfree(phba->vpi_bmask);
  4283. rc = -ENOMEM;
  4284. goto lpfc_sli_hba_setup_error;
  4285. }
  4286. for (i = 0; i < phba->max_vpi; i++)
  4287. phba->vpi_ids[i] = i;
  4288. }
  4289. }
  4290. /* Init HBQs */
  4291. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4292. rc = lpfc_sli_hbq_setup(phba);
  4293. if (rc)
  4294. goto lpfc_sli_hba_setup_error;
  4295. }
  4296. spin_lock_irq(&phba->hbalock);
  4297. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4298. spin_unlock_irq(&phba->hbalock);
  4299. rc = lpfc_config_port_post(phba);
  4300. if (rc)
  4301. goto lpfc_sli_hba_setup_error;
  4302. return rc;
  4303. lpfc_sli_hba_setup_error:
  4304. phba->link_state = LPFC_HBA_ERROR;
  4305. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4306. "0445 Firmware initialization failed\n");
  4307. return rc;
  4308. }
  4309. /**
  4310. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4311. * @phba: Pointer to HBA context object.
  4312. * @mboxq: mailbox pointer.
  4313. * This function issue a dump mailbox command to read config region
  4314. * 23 and parse the records in the region and populate driver
  4315. * data structure.
  4316. **/
  4317. static int
  4318. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4319. {
  4320. LPFC_MBOXQ_t *mboxq;
  4321. struct lpfc_dmabuf *mp;
  4322. struct lpfc_mqe *mqe;
  4323. uint32_t data_length;
  4324. int rc;
  4325. /* Program the default value of vlan_id and fc_map */
  4326. phba->valid_vlan = 0;
  4327. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4328. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4329. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4330. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4331. if (!mboxq)
  4332. return -ENOMEM;
  4333. mqe = &mboxq->u.mqe;
  4334. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4335. rc = -ENOMEM;
  4336. goto out_free_mboxq;
  4337. }
  4338. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4339. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4340. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4341. "(%d):2571 Mailbox cmd x%x Status x%x "
  4342. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4343. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4344. "CQ: x%x x%x x%x x%x\n",
  4345. mboxq->vport ? mboxq->vport->vpi : 0,
  4346. bf_get(lpfc_mqe_command, mqe),
  4347. bf_get(lpfc_mqe_status, mqe),
  4348. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4349. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4350. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4351. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4352. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4353. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4354. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4355. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4356. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4357. mboxq->mcqe.word0,
  4358. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4359. mboxq->mcqe.trailer);
  4360. if (rc) {
  4361. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4362. kfree(mp);
  4363. rc = -EIO;
  4364. goto out_free_mboxq;
  4365. }
  4366. data_length = mqe->un.mb_words[5];
  4367. if (data_length > DMP_RGN23_SIZE) {
  4368. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4369. kfree(mp);
  4370. rc = -EIO;
  4371. goto out_free_mboxq;
  4372. }
  4373. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4374. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4375. kfree(mp);
  4376. rc = 0;
  4377. out_free_mboxq:
  4378. mempool_free(mboxq, phba->mbox_mem_pool);
  4379. return rc;
  4380. }
  4381. /**
  4382. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4383. * @phba: pointer to lpfc hba data structure.
  4384. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4385. * @vpd: pointer to the memory to hold resulting port vpd data.
  4386. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4387. * On output, the number of data bytes in @vpd.
  4388. *
  4389. * This routine executes a READ_REV SLI4 mailbox command. In
  4390. * addition, this routine gets the port vpd data.
  4391. *
  4392. * Return codes
  4393. * 0 - successful
  4394. * -ENOMEM - could not allocated memory.
  4395. **/
  4396. static int
  4397. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4398. uint8_t *vpd, uint32_t *vpd_size)
  4399. {
  4400. int rc = 0;
  4401. uint32_t dma_size;
  4402. struct lpfc_dmabuf *dmabuf;
  4403. struct lpfc_mqe *mqe;
  4404. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4405. if (!dmabuf)
  4406. return -ENOMEM;
  4407. /*
  4408. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4409. * mailbox command.
  4410. */
  4411. dma_size = *vpd_size;
  4412. dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
  4413. &dmabuf->phys, GFP_KERNEL);
  4414. if (!dmabuf->virt) {
  4415. kfree(dmabuf);
  4416. return -ENOMEM;
  4417. }
  4418. /*
  4419. * The SLI4 implementation of READ_REV conflicts at word1,
  4420. * bits 31:16 and SLI4 adds vpd functionality not present
  4421. * in SLI3. This code corrects the conflicts.
  4422. */
  4423. lpfc_read_rev(phba, mboxq);
  4424. mqe = &mboxq->u.mqe;
  4425. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4426. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4427. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4428. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4429. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4430. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4431. if (rc) {
  4432. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4433. dmabuf->virt, dmabuf->phys);
  4434. kfree(dmabuf);
  4435. return -EIO;
  4436. }
  4437. /*
  4438. * The available vpd length cannot be bigger than the
  4439. * DMA buffer passed to the port. Catch the less than
  4440. * case and update the caller's size.
  4441. */
  4442. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4443. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4444. memcpy(vpd, dmabuf->virt, *vpd_size);
  4445. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4446. dmabuf->virt, dmabuf->phys);
  4447. kfree(dmabuf);
  4448. return 0;
  4449. }
  4450. /**
  4451. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4452. * @phba: pointer to lpfc hba data structure.
  4453. *
  4454. * This routine retrieves SLI4 device physical port name this PCI function
  4455. * is attached to.
  4456. *
  4457. * Return codes
  4458. * 0 - successful
  4459. * otherwise - failed to retrieve physical port name
  4460. **/
  4461. static int
  4462. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4463. {
  4464. LPFC_MBOXQ_t *mboxq;
  4465. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4466. struct lpfc_controller_attribute *cntl_attr;
  4467. struct lpfc_mbx_get_port_name *get_port_name;
  4468. void *virtaddr = NULL;
  4469. uint32_t alloclen, reqlen;
  4470. uint32_t shdr_status, shdr_add_status;
  4471. union lpfc_sli4_cfg_shdr *shdr;
  4472. char cport_name = 0;
  4473. int rc;
  4474. /* We assume nothing at this point */
  4475. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4476. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4477. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4478. if (!mboxq)
  4479. return -ENOMEM;
  4480. /* obtain link type and link number via READ_CONFIG */
  4481. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4482. lpfc_sli4_read_config(phba);
  4483. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4484. goto retrieve_ppname;
  4485. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4486. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4487. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4488. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4489. LPFC_SLI4_MBX_NEMBED);
  4490. if (alloclen < reqlen) {
  4491. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4492. "3084 Allocated DMA memory size (%d) is "
  4493. "less than the requested DMA memory size "
  4494. "(%d)\n", alloclen, reqlen);
  4495. rc = -ENOMEM;
  4496. goto out_free_mboxq;
  4497. }
  4498. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4499. virtaddr = mboxq->sge_array->addr[0];
  4500. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4501. shdr = &mbx_cntl_attr->cfg_shdr;
  4502. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4503. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4504. if (shdr_status || shdr_add_status || rc) {
  4505. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4506. "3085 Mailbox x%x (x%x/x%x) failed, "
  4507. "rc:x%x, status:x%x, add_status:x%x\n",
  4508. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4509. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4510. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4511. rc, shdr_status, shdr_add_status);
  4512. rc = -ENXIO;
  4513. goto out_free_mboxq;
  4514. }
  4515. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4516. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4517. phba->sli4_hba.lnk_info.lnk_tp =
  4518. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4519. phba->sli4_hba.lnk_info.lnk_no =
  4520. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4521. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4522. "3086 lnk_type:%d, lnk_numb:%d\n",
  4523. phba->sli4_hba.lnk_info.lnk_tp,
  4524. phba->sli4_hba.lnk_info.lnk_no);
  4525. retrieve_ppname:
  4526. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4527. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4528. sizeof(struct lpfc_mbx_get_port_name) -
  4529. sizeof(struct lpfc_sli4_cfg_mhdr),
  4530. LPFC_SLI4_MBX_EMBED);
  4531. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4532. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4533. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4534. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4535. phba->sli4_hba.lnk_info.lnk_tp);
  4536. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4537. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4538. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4539. if (shdr_status || shdr_add_status || rc) {
  4540. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4541. "3087 Mailbox x%x (x%x/x%x) failed: "
  4542. "rc:x%x, status:x%x, add_status:x%x\n",
  4543. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4544. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4545. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4546. rc, shdr_status, shdr_add_status);
  4547. rc = -ENXIO;
  4548. goto out_free_mboxq;
  4549. }
  4550. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4551. case LPFC_LINK_NUMBER_0:
  4552. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4553. &get_port_name->u.response);
  4554. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4555. break;
  4556. case LPFC_LINK_NUMBER_1:
  4557. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4558. &get_port_name->u.response);
  4559. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4560. break;
  4561. case LPFC_LINK_NUMBER_2:
  4562. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4563. &get_port_name->u.response);
  4564. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4565. break;
  4566. case LPFC_LINK_NUMBER_3:
  4567. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4568. &get_port_name->u.response);
  4569. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4570. break;
  4571. default:
  4572. break;
  4573. }
  4574. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4575. phba->Port[0] = cport_name;
  4576. phba->Port[1] = '\0';
  4577. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4578. "3091 SLI get port name: %s\n", phba->Port);
  4579. }
  4580. out_free_mboxq:
  4581. if (rc != MBX_TIMEOUT) {
  4582. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4583. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4584. else
  4585. mempool_free(mboxq, phba->mbox_mem_pool);
  4586. }
  4587. return rc;
  4588. }
  4589. /**
  4590. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4591. * @phba: pointer to lpfc hba data structure.
  4592. *
  4593. * This routine is called to explicitly arm the SLI4 device's completion and
  4594. * event queues
  4595. **/
  4596. static void
  4597. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4598. {
  4599. int fcp_eqidx;
  4600. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4601. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4602. fcp_eqidx = 0;
  4603. if (phba->sli4_hba.fcp_cq) {
  4604. do {
  4605. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4606. LPFC_QUEUE_REARM);
  4607. } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
  4608. }
  4609. if (phba->cfg_fof)
  4610. lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
  4611. if (phba->sli4_hba.hba_eq) {
  4612. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
  4613. fcp_eqidx++)
  4614. lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
  4615. LPFC_QUEUE_REARM);
  4616. }
  4617. if (phba->cfg_fof)
  4618. lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
  4619. }
  4620. /**
  4621. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4622. * @phba: Pointer to HBA context object.
  4623. * @type: The resource extent type.
  4624. * @extnt_count: buffer to hold port available extent count.
  4625. * @extnt_size: buffer to hold element count per extent.
  4626. *
  4627. * This function calls the port and retrievs the number of available
  4628. * extents and their size for a particular extent type.
  4629. *
  4630. * Returns: 0 if successful. Nonzero otherwise.
  4631. **/
  4632. int
  4633. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4634. uint16_t *extnt_count, uint16_t *extnt_size)
  4635. {
  4636. int rc = 0;
  4637. uint32_t length;
  4638. uint32_t mbox_tmo;
  4639. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4640. LPFC_MBOXQ_t *mbox;
  4641. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4642. if (!mbox)
  4643. return -ENOMEM;
  4644. /* Find out how many extents are available for this resource type */
  4645. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4646. sizeof(struct lpfc_sli4_cfg_mhdr));
  4647. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4648. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4649. length, LPFC_SLI4_MBX_EMBED);
  4650. /* Send an extents count of 0 - the GET doesn't use it. */
  4651. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4652. LPFC_SLI4_MBX_EMBED);
  4653. if (unlikely(rc)) {
  4654. rc = -EIO;
  4655. goto err_exit;
  4656. }
  4657. if (!phba->sli4_hba.intr_enable)
  4658. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4659. else {
  4660. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4661. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4662. }
  4663. if (unlikely(rc)) {
  4664. rc = -EIO;
  4665. goto err_exit;
  4666. }
  4667. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4668. if (bf_get(lpfc_mbox_hdr_status,
  4669. &rsrc_info->header.cfg_shdr.response)) {
  4670. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4671. "2930 Failed to get resource extents "
  4672. "Status 0x%x Add'l Status 0x%x\n",
  4673. bf_get(lpfc_mbox_hdr_status,
  4674. &rsrc_info->header.cfg_shdr.response),
  4675. bf_get(lpfc_mbox_hdr_add_status,
  4676. &rsrc_info->header.cfg_shdr.response));
  4677. rc = -EIO;
  4678. goto err_exit;
  4679. }
  4680. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4681. &rsrc_info->u.rsp);
  4682. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4683. &rsrc_info->u.rsp);
  4684. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4685. "3162 Retrieved extents type-%d from port: count:%d, "
  4686. "size:%d\n", type, *extnt_count, *extnt_size);
  4687. err_exit:
  4688. mempool_free(mbox, phba->mbox_mem_pool);
  4689. return rc;
  4690. }
  4691. /**
  4692. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4693. * @phba: Pointer to HBA context object.
  4694. * @type: The extent type to check.
  4695. *
  4696. * This function reads the current available extents from the port and checks
  4697. * if the extent count or extent size has changed since the last access.
  4698. * Callers use this routine post port reset to understand if there is a
  4699. * extent reprovisioning requirement.
  4700. *
  4701. * Returns:
  4702. * -Error: error indicates problem.
  4703. * 1: Extent count or size has changed.
  4704. * 0: No changes.
  4705. **/
  4706. static int
  4707. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4708. {
  4709. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4710. uint16_t size_diff, rsrc_ext_size;
  4711. int rc = 0;
  4712. struct lpfc_rsrc_blks *rsrc_entry;
  4713. struct list_head *rsrc_blk_list = NULL;
  4714. size_diff = 0;
  4715. curr_ext_cnt = 0;
  4716. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4717. &rsrc_ext_cnt,
  4718. &rsrc_ext_size);
  4719. if (unlikely(rc))
  4720. return -EIO;
  4721. switch (type) {
  4722. case LPFC_RSC_TYPE_FCOE_RPI:
  4723. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4724. break;
  4725. case LPFC_RSC_TYPE_FCOE_VPI:
  4726. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4727. break;
  4728. case LPFC_RSC_TYPE_FCOE_XRI:
  4729. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4730. break;
  4731. case LPFC_RSC_TYPE_FCOE_VFI:
  4732. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4733. break;
  4734. default:
  4735. break;
  4736. }
  4737. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4738. curr_ext_cnt++;
  4739. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4740. size_diff++;
  4741. }
  4742. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4743. rc = 1;
  4744. return rc;
  4745. }
  4746. /**
  4747. * lpfc_sli4_cfg_post_extnts -
  4748. * @phba: Pointer to HBA context object.
  4749. * @extnt_cnt - number of available extents.
  4750. * @type - the extent type (rpi, xri, vfi, vpi).
  4751. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4752. * @mbox - pointer to the caller's allocated mailbox structure.
  4753. *
  4754. * This function executes the extents allocation request. It also
  4755. * takes care of the amount of memory needed to allocate or get the
  4756. * allocated extents. It is the caller's responsibility to evaluate
  4757. * the response.
  4758. *
  4759. * Returns:
  4760. * -Error: Error value describes the condition found.
  4761. * 0: if successful
  4762. **/
  4763. static int
  4764. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  4765. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4766. {
  4767. int rc = 0;
  4768. uint32_t req_len;
  4769. uint32_t emb_len;
  4770. uint32_t alloc_len, mbox_tmo;
  4771. /* Calculate the total requested length of the dma memory */
  4772. req_len = extnt_cnt * sizeof(uint16_t);
  4773. /*
  4774. * Calculate the size of an embedded mailbox. The uint32_t
  4775. * accounts for extents-specific word.
  4776. */
  4777. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4778. sizeof(uint32_t);
  4779. /*
  4780. * Presume the allocation and response will fit into an embedded
  4781. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4782. */
  4783. *emb = LPFC_SLI4_MBX_EMBED;
  4784. if (req_len > emb_len) {
  4785. req_len = extnt_cnt * sizeof(uint16_t) +
  4786. sizeof(union lpfc_sli4_cfg_shdr) +
  4787. sizeof(uint32_t);
  4788. *emb = LPFC_SLI4_MBX_NEMBED;
  4789. }
  4790. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4791. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4792. req_len, *emb);
  4793. if (alloc_len < req_len) {
  4794. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4795. "2982 Allocated DMA memory size (x%x) is "
  4796. "less than the requested DMA memory "
  4797. "size (x%x)\n", alloc_len, req_len);
  4798. return -ENOMEM;
  4799. }
  4800. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  4801. if (unlikely(rc))
  4802. return -EIO;
  4803. if (!phba->sli4_hba.intr_enable)
  4804. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4805. else {
  4806. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4807. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4808. }
  4809. if (unlikely(rc))
  4810. rc = -EIO;
  4811. return rc;
  4812. }
  4813. /**
  4814. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4815. * @phba: Pointer to HBA context object.
  4816. * @type: The resource extent type to allocate.
  4817. *
  4818. * This function allocates the number of elements for the specified
  4819. * resource type.
  4820. **/
  4821. static int
  4822. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4823. {
  4824. bool emb = false;
  4825. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4826. uint16_t rsrc_id, rsrc_start, j, k;
  4827. uint16_t *ids;
  4828. int i, rc;
  4829. unsigned long longs;
  4830. unsigned long *bmask;
  4831. struct lpfc_rsrc_blks *rsrc_blks;
  4832. LPFC_MBOXQ_t *mbox;
  4833. uint32_t length;
  4834. struct lpfc_id_range *id_array = NULL;
  4835. void *virtaddr = NULL;
  4836. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4837. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4838. struct list_head *ext_blk_list;
  4839. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4840. &rsrc_cnt,
  4841. &rsrc_size);
  4842. if (unlikely(rc))
  4843. return -EIO;
  4844. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4845. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4846. "3009 No available Resource Extents "
  4847. "for resource type 0x%x: Count: 0x%x, "
  4848. "Size 0x%x\n", type, rsrc_cnt,
  4849. rsrc_size);
  4850. return -ENOMEM;
  4851. }
  4852. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  4853. "2903 Post resource extents type-0x%x: "
  4854. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  4855. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4856. if (!mbox)
  4857. return -ENOMEM;
  4858. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  4859. if (unlikely(rc)) {
  4860. rc = -EIO;
  4861. goto err_exit;
  4862. }
  4863. /*
  4864. * Figure out where the response is located. Then get local pointers
  4865. * to the response data. The port does not guarantee to respond to
  4866. * all extents counts request so update the local variable with the
  4867. * allocated count from the port.
  4868. */
  4869. if (emb == LPFC_SLI4_MBX_EMBED) {
  4870. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4871. id_array = &rsrc_ext->u.rsp.id[0];
  4872. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4873. } else {
  4874. virtaddr = mbox->sge_array->addr[0];
  4875. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4876. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4877. id_array = &n_rsrc->id;
  4878. }
  4879. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4880. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4881. /*
  4882. * Based on the resource size and count, correct the base and max
  4883. * resource values.
  4884. */
  4885. length = sizeof(struct lpfc_rsrc_blks);
  4886. switch (type) {
  4887. case LPFC_RSC_TYPE_FCOE_RPI:
  4888. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4889. sizeof(unsigned long),
  4890. GFP_KERNEL);
  4891. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4892. rc = -ENOMEM;
  4893. goto err_exit;
  4894. }
  4895. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4896. sizeof(uint16_t),
  4897. GFP_KERNEL);
  4898. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4899. kfree(phba->sli4_hba.rpi_bmask);
  4900. rc = -ENOMEM;
  4901. goto err_exit;
  4902. }
  4903. /*
  4904. * The next_rpi was initialized with the maximum available
  4905. * count but the port may allocate a smaller number. Catch
  4906. * that case and update the next_rpi.
  4907. */
  4908. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4909. /* Initialize local ptrs for common extent processing later. */
  4910. bmask = phba->sli4_hba.rpi_bmask;
  4911. ids = phba->sli4_hba.rpi_ids;
  4912. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4913. break;
  4914. case LPFC_RSC_TYPE_FCOE_VPI:
  4915. phba->vpi_bmask = kzalloc(longs *
  4916. sizeof(unsigned long),
  4917. GFP_KERNEL);
  4918. if (unlikely(!phba->vpi_bmask)) {
  4919. rc = -ENOMEM;
  4920. goto err_exit;
  4921. }
  4922. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4923. sizeof(uint16_t),
  4924. GFP_KERNEL);
  4925. if (unlikely(!phba->vpi_ids)) {
  4926. kfree(phba->vpi_bmask);
  4927. rc = -ENOMEM;
  4928. goto err_exit;
  4929. }
  4930. /* Initialize local ptrs for common extent processing later. */
  4931. bmask = phba->vpi_bmask;
  4932. ids = phba->vpi_ids;
  4933. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4934. break;
  4935. case LPFC_RSC_TYPE_FCOE_XRI:
  4936. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4937. sizeof(unsigned long),
  4938. GFP_KERNEL);
  4939. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4940. rc = -ENOMEM;
  4941. goto err_exit;
  4942. }
  4943. phba->sli4_hba.max_cfg_param.xri_used = 0;
  4944. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4945. sizeof(uint16_t),
  4946. GFP_KERNEL);
  4947. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4948. kfree(phba->sli4_hba.xri_bmask);
  4949. rc = -ENOMEM;
  4950. goto err_exit;
  4951. }
  4952. /* Initialize local ptrs for common extent processing later. */
  4953. bmask = phba->sli4_hba.xri_bmask;
  4954. ids = phba->sli4_hba.xri_ids;
  4955. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4956. break;
  4957. case LPFC_RSC_TYPE_FCOE_VFI:
  4958. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4959. sizeof(unsigned long),
  4960. GFP_KERNEL);
  4961. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4962. rc = -ENOMEM;
  4963. goto err_exit;
  4964. }
  4965. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4966. sizeof(uint16_t),
  4967. GFP_KERNEL);
  4968. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4969. kfree(phba->sli4_hba.vfi_bmask);
  4970. rc = -ENOMEM;
  4971. goto err_exit;
  4972. }
  4973. /* Initialize local ptrs for common extent processing later. */
  4974. bmask = phba->sli4_hba.vfi_bmask;
  4975. ids = phba->sli4_hba.vfi_ids;
  4976. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4977. break;
  4978. default:
  4979. /* Unsupported Opcode. Fail call. */
  4980. id_array = NULL;
  4981. bmask = NULL;
  4982. ids = NULL;
  4983. ext_blk_list = NULL;
  4984. goto err_exit;
  4985. }
  4986. /*
  4987. * Complete initializing the extent configuration with the
  4988. * allocated ids assigned to this function. The bitmask serves
  4989. * as an index into the array and manages the available ids. The
  4990. * array just stores the ids communicated to the port via the wqes.
  4991. */
  4992. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4993. if ((i % 2) == 0)
  4994. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4995. &id_array[k]);
  4996. else
  4997. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4998. &id_array[k]);
  4999. rsrc_blks = kzalloc(length, GFP_KERNEL);
  5000. if (unlikely(!rsrc_blks)) {
  5001. rc = -ENOMEM;
  5002. kfree(bmask);
  5003. kfree(ids);
  5004. goto err_exit;
  5005. }
  5006. rsrc_blks->rsrc_start = rsrc_id;
  5007. rsrc_blks->rsrc_size = rsrc_size;
  5008. list_add_tail(&rsrc_blks->list, ext_blk_list);
  5009. rsrc_start = rsrc_id;
  5010. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  5011. phba->sli4_hba.scsi_xri_start = rsrc_start +
  5012. lpfc_sli4_get_els_iocb_cnt(phba);
  5013. while (rsrc_id < (rsrc_start + rsrc_size)) {
  5014. ids[j] = rsrc_id;
  5015. rsrc_id++;
  5016. j++;
  5017. }
  5018. /* Entire word processed. Get next word.*/
  5019. if ((i % 2) == 1)
  5020. k++;
  5021. }
  5022. err_exit:
  5023. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5024. return rc;
  5025. }
  5026. /**
  5027. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  5028. * @phba: Pointer to HBA context object.
  5029. * @type: the extent's type.
  5030. *
  5031. * This function deallocates all extents of a particular resource type.
  5032. * SLI4 does not allow for deallocating a particular extent range. It
  5033. * is the caller's responsibility to release all kernel memory resources.
  5034. **/
  5035. static int
  5036. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  5037. {
  5038. int rc;
  5039. uint32_t length, mbox_tmo = 0;
  5040. LPFC_MBOXQ_t *mbox;
  5041. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  5042. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  5043. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5044. if (!mbox)
  5045. return -ENOMEM;
  5046. /*
  5047. * This function sends an embedded mailbox because it only sends the
  5048. * the resource type. All extents of this type are released by the
  5049. * port.
  5050. */
  5051. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  5052. sizeof(struct lpfc_sli4_cfg_mhdr));
  5053. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5054. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  5055. length, LPFC_SLI4_MBX_EMBED);
  5056. /* Send an extents count of 0 - the dealloc doesn't use it. */
  5057. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  5058. LPFC_SLI4_MBX_EMBED);
  5059. if (unlikely(rc)) {
  5060. rc = -EIO;
  5061. goto out_free_mbox;
  5062. }
  5063. if (!phba->sli4_hba.intr_enable)
  5064. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5065. else {
  5066. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5067. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5068. }
  5069. if (unlikely(rc)) {
  5070. rc = -EIO;
  5071. goto out_free_mbox;
  5072. }
  5073. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  5074. if (bf_get(lpfc_mbox_hdr_status,
  5075. &dealloc_rsrc->header.cfg_shdr.response)) {
  5076. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5077. "2919 Failed to release resource extents "
  5078. "for type %d - Status 0x%x Add'l Status 0x%x. "
  5079. "Resource memory not released.\n",
  5080. type,
  5081. bf_get(lpfc_mbox_hdr_status,
  5082. &dealloc_rsrc->header.cfg_shdr.response),
  5083. bf_get(lpfc_mbox_hdr_add_status,
  5084. &dealloc_rsrc->header.cfg_shdr.response));
  5085. rc = -EIO;
  5086. goto out_free_mbox;
  5087. }
  5088. /* Release kernel memory resources for the specific type. */
  5089. switch (type) {
  5090. case LPFC_RSC_TYPE_FCOE_VPI:
  5091. kfree(phba->vpi_bmask);
  5092. kfree(phba->vpi_ids);
  5093. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5094. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5095. &phba->lpfc_vpi_blk_list, list) {
  5096. list_del_init(&rsrc_blk->list);
  5097. kfree(rsrc_blk);
  5098. }
  5099. phba->sli4_hba.max_cfg_param.vpi_used = 0;
  5100. break;
  5101. case LPFC_RSC_TYPE_FCOE_XRI:
  5102. kfree(phba->sli4_hba.xri_bmask);
  5103. kfree(phba->sli4_hba.xri_ids);
  5104. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5105. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  5106. list_del_init(&rsrc_blk->list);
  5107. kfree(rsrc_blk);
  5108. }
  5109. break;
  5110. case LPFC_RSC_TYPE_FCOE_VFI:
  5111. kfree(phba->sli4_hba.vfi_bmask);
  5112. kfree(phba->sli4_hba.vfi_ids);
  5113. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5114. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5115. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  5116. list_del_init(&rsrc_blk->list);
  5117. kfree(rsrc_blk);
  5118. }
  5119. break;
  5120. case LPFC_RSC_TYPE_FCOE_RPI:
  5121. /* RPI bitmask and physical id array are cleaned up earlier. */
  5122. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5123. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  5124. list_del_init(&rsrc_blk->list);
  5125. kfree(rsrc_blk);
  5126. }
  5127. break;
  5128. default:
  5129. break;
  5130. }
  5131. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5132. out_free_mbox:
  5133. mempool_free(mbox, phba->mbox_mem_pool);
  5134. return rc;
  5135. }
  5136. /**
  5137. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  5138. * @phba: Pointer to HBA context object.
  5139. *
  5140. * This function allocates all SLI4 resource identifiers.
  5141. **/
  5142. int
  5143. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  5144. {
  5145. int i, rc, error = 0;
  5146. uint16_t count, base;
  5147. unsigned long longs;
  5148. if (!phba->sli4_hba.rpi_hdrs_in_use)
  5149. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  5150. if (phba->sli4_hba.extents_in_use) {
  5151. /*
  5152. * The port supports resource extents. The XRI, VPI, VFI, RPI
  5153. * resource extent count must be read and allocated before
  5154. * provisioning the resource id arrays.
  5155. */
  5156. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5157. LPFC_IDX_RSRC_RDY) {
  5158. /*
  5159. * Extent-based resources are set - the driver could
  5160. * be in a port reset. Figure out if any corrective
  5161. * actions need to be taken.
  5162. */
  5163. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5164. LPFC_RSC_TYPE_FCOE_VFI);
  5165. if (rc != 0)
  5166. error++;
  5167. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5168. LPFC_RSC_TYPE_FCOE_VPI);
  5169. if (rc != 0)
  5170. error++;
  5171. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5172. LPFC_RSC_TYPE_FCOE_XRI);
  5173. if (rc != 0)
  5174. error++;
  5175. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5176. LPFC_RSC_TYPE_FCOE_RPI);
  5177. if (rc != 0)
  5178. error++;
  5179. /*
  5180. * It's possible that the number of resources
  5181. * provided to this port instance changed between
  5182. * resets. Detect this condition and reallocate
  5183. * resources. Otherwise, there is no action.
  5184. */
  5185. if (error) {
  5186. lpfc_printf_log(phba, KERN_INFO,
  5187. LOG_MBOX | LOG_INIT,
  5188. "2931 Detected extent resource "
  5189. "change. Reallocating all "
  5190. "extents.\n");
  5191. rc = lpfc_sli4_dealloc_extent(phba,
  5192. LPFC_RSC_TYPE_FCOE_VFI);
  5193. rc = lpfc_sli4_dealloc_extent(phba,
  5194. LPFC_RSC_TYPE_FCOE_VPI);
  5195. rc = lpfc_sli4_dealloc_extent(phba,
  5196. LPFC_RSC_TYPE_FCOE_XRI);
  5197. rc = lpfc_sli4_dealloc_extent(phba,
  5198. LPFC_RSC_TYPE_FCOE_RPI);
  5199. } else
  5200. return 0;
  5201. }
  5202. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5203. if (unlikely(rc))
  5204. goto err_exit;
  5205. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5206. if (unlikely(rc))
  5207. goto err_exit;
  5208. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5209. if (unlikely(rc))
  5210. goto err_exit;
  5211. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5212. if (unlikely(rc))
  5213. goto err_exit;
  5214. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5215. LPFC_IDX_RSRC_RDY);
  5216. return rc;
  5217. } else {
  5218. /*
  5219. * The port does not support resource extents. The XRI, VPI,
  5220. * VFI, RPI resource ids were determined from READ_CONFIG.
  5221. * Just allocate the bitmasks and provision the resource id
  5222. * arrays. If a port reset is active, the resources don't
  5223. * need any action - just exit.
  5224. */
  5225. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5226. LPFC_IDX_RSRC_RDY) {
  5227. lpfc_sli4_dealloc_resource_identifiers(phba);
  5228. lpfc_sli4_remove_rpis(phba);
  5229. }
  5230. /* RPIs. */
  5231. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5232. if (count <= 0) {
  5233. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5234. "3279 Invalid provisioning of "
  5235. "rpi:%d\n", count);
  5236. rc = -EINVAL;
  5237. goto err_exit;
  5238. }
  5239. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5240. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5241. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5242. sizeof(unsigned long),
  5243. GFP_KERNEL);
  5244. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5245. rc = -ENOMEM;
  5246. goto err_exit;
  5247. }
  5248. phba->sli4_hba.rpi_ids = kzalloc(count *
  5249. sizeof(uint16_t),
  5250. GFP_KERNEL);
  5251. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5252. rc = -ENOMEM;
  5253. goto free_rpi_bmask;
  5254. }
  5255. for (i = 0; i < count; i++)
  5256. phba->sli4_hba.rpi_ids[i] = base + i;
  5257. /* VPIs. */
  5258. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5259. if (count <= 0) {
  5260. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5261. "3280 Invalid provisioning of "
  5262. "vpi:%d\n", count);
  5263. rc = -EINVAL;
  5264. goto free_rpi_ids;
  5265. }
  5266. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5267. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5268. phba->vpi_bmask = kzalloc(longs *
  5269. sizeof(unsigned long),
  5270. GFP_KERNEL);
  5271. if (unlikely(!phba->vpi_bmask)) {
  5272. rc = -ENOMEM;
  5273. goto free_rpi_ids;
  5274. }
  5275. phba->vpi_ids = kzalloc(count *
  5276. sizeof(uint16_t),
  5277. GFP_KERNEL);
  5278. if (unlikely(!phba->vpi_ids)) {
  5279. rc = -ENOMEM;
  5280. goto free_vpi_bmask;
  5281. }
  5282. for (i = 0; i < count; i++)
  5283. phba->vpi_ids[i] = base + i;
  5284. /* XRIs. */
  5285. count = phba->sli4_hba.max_cfg_param.max_xri;
  5286. if (count <= 0) {
  5287. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5288. "3281 Invalid provisioning of "
  5289. "xri:%d\n", count);
  5290. rc = -EINVAL;
  5291. goto free_vpi_ids;
  5292. }
  5293. base = phba->sli4_hba.max_cfg_param.xri_base;
  5294. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5295. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5296. sizeof(unsigned long),
  5297. GFP_KERNEL);
  5298. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5299. rc = -ENOMEM;
  5300. goto free_vpi_ids;
  5301. }
  5302. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5303. phba->sli4_hba.xri_ids = kzalloc(count *
  5304. sizeof(uint16_t),
  5305. GFP_KERNEL);
  5306. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5307. rc = -ENOMEM;
  5308. goto free_xri_bmask;
  5309. }
  5310. for (i = 0; i < count; i++)
  5311. phba->sli4_hba.xri_ids[i] = base + i;
  5312. /* VFIs. */
  5313. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5314. if (count <= 0) {
  5315. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5316. "3282 Invalid provisioning of "
  5317. "vfi:%d\n", count);
  5318. rc = -EINVAL;
  5319. goto free_xri_ids;
  5320. }
  5321. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5322. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5323. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5324. sizeof(unsigned long),
  5325. GFP_KERNEL);
  5326. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5327. rc = -ENOMEM;
  5328. goto free_xri_ids;
  5329. }
  5330. phba->sli4_hba.vfi_ids = kzalloc(count *
  5331. sizeof(uint16_t),
  5332. GFP_KERNEL);
  5333. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5334. rc = -ENOMEM;
  5335. goto free_vfi_bmask;
  5336. }
  5337. for (i = 0; i < count; i++)
  5338. phba->sli4_hba.vfi_ids[i] = base + i;
  5339. /*
  5340. * Mark all resources ready. An HBA reset doesn't need
  5341. * to reset the initialization.
  5342. */
  5343. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5344. LPFC_IDX_RSRC_RDY);
  5345. return 0;
  5346. }
  5347. free_vfi_bmask:
  5348. kfree(phba->sli4_hba.vfi_bmask);
  5349. free_xri_ids:
  5350. kfree(phba->sli4_hba.xri_ids);
  5351. free_xri_bmask:
  5352. kfree(phba->sli4_hba.xri_bmask);
  5353. free_vpi_ids:
  5354. kfree(phba->vpi_ids);
  5355. free_vpi_bmask:
  5356. kfree(phba->vpi_bmask);
  5357. free_rpi_ids:
  5358. kfree(phba->sli4_hba.rpi_ids);
  5359. free_rpi_bmask:
  5360. kfree(phba->sli4_hba.rpi_bmask);
  5361. err_exit:
  5362. return rc;
  5363. }
  5364. /**
  5365. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5366. * @phba: Pointer to HBA context object.
  5367. *
  5368. * This function allocates the number of elements for the specified
  5369. * resource type.
  5370. **/
  5371. int
  5372. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5373. {
  5374. if (phba->sli4_hba.extents_in_use) {
  5375. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5376. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5377. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5378. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5379. } else {
  5380. kfree(phba->vpi_bmask);
  5381. phba->sli4_hba.max_cfg_param.vpi_used = 0;
  5382. kfree(phba->vpi_ids);
  5383. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5384. kfree(phba->sli4_hba.xri_bmask);
  5385. kfree(phba->sli4_hba.xri_ids);
  5386. kfree(phba->sli4_hba.vfi_bmask);
  5387. kfree(phba->sli4_hba.vfi_ids);
  5388. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5389. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5390. }
  5391. return 0;
  5392. }
  5393. /**
  5394. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5395. * @phba: Pointer to HBA context object.
  5396. * @type: The resource extent type.
  5397. * @extnt_count: buffer to hold port extent count response
  5398. * @extnt_size: buffer to hold port extent size response.
  5399. *
  5400. * This function calls the port to read the host allocated extents
  5401. * for a particular type.
  5402. **/
  5403. int
  5404. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5405. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5406. {
  5407. bool emb;
  5408. int rc = 0;
  5409. uint16_t curr_blks = 0;
  5410. uint32_t req_len, emb_len;
  5411. uint32_t alloc_len, mbox_tmo;
  5412. struct list_head *blk_list_head;
  5413. struct lpfc_rsrc_blks *rsrc_blk;
  5414. LPFC_MBOXQ_t *mbox;
  5415. void *virtaddr = NULL;
  5416. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5417. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5418. union lpfc_sli4_cfg_shdr *shdr;
  5419. switch (type) {
  5420. case LPFC_RSC_TYPE_FCOE_VPI:
  5421. blk_list_head = &phba->lpfc_vpi_blk_list;
  5422. break;
  5423. case LPFC_RSC_TYPE_FCOE_XRI:
  5424. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5425. break;
  5426. case LPFC_RSC_TYPE_FCOE_VFI:
  5427. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5428. break;
  5429. case LPFC_RSC_TYPE_FCOE_RPI:
  5430. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5431. break;
  5432. default:
  5433. return -EIO;
  5434. }
  5435. /* Count the number of extents currently allocatd for this type. */
  5436. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5437. if (curr_blks == 0) {
  5438. /*
  5439. * The GET_ALLOCATED mailbox does not return the size,
  5440. * just the count. The size should be just the size
  5441. * stored in the current allocated block and all sizes
  5442. * for an extent type are the same so set the return
  5443. * value now.
  5444. */
  5445. *extnt_size = rsrc_blk->rsrc_size;
  5446. }
  5447. curr_blks++;
  5448. }
  5449. /*
  5450. * Calculate the size of an embedded mailbox. The uint32_t
  5451. * accounts for extents-specific word.
  5452. */
  5453. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5454. sizeof(uint32_t);
  5455. /*
  5456. * Presume the allocation and response will fit into an embedded
  5457. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5458. */
  5459. emb = LPFC_SLI4_MBX_EMBED;
  5460. req_len = emb_len;
  5461. if (req_len > emb_len) {
  5462. req_len = curr_blks * sizeof(uint16_t) +
  5463. sizeof(union lpfc_sli4_cfg_shdr) +
  5464. sizeof(uint32_t);
  5465. emb = LPFC_SLI4_MBX_NEMBED;
  5466. }
  5467. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5468. if (!mbox)
  5469. return -ENOMEM;
  5470. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5471. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5472. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5473. req_len, emb);
  5474. if (alloc_len < req_len) {
  5475. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5476. "2983 Allocated DMA memory size (x%x) is "
  5477. "less than the requested DMA memory "
  5478. "size (x%x)\n", alloc_len, req_len);
  5479. rc = -ENOMEM;
  5480. goto err_exit;
  5481. }
  5482. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5483. if (unlikely(rc)) {
  5484. rc = -EIO;
  5485. goto err_exit;
  5486. }
  5487. if (!phba->sli4_hba.intr_enable)
  5488. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5489. else {
  5490. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5491. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5492. }
  5493. if (unlikely(rc)) {
  5494. rc = -EIO;
  5495. goto err_exit;
  5496. }
  5497. /*
  5498. * Figure out where the response is located. Then get local pointers
  5499. * to the response data. The port does not guarantee to respond to
  5500. * all extents counts request so update the local variable with the
  5501. * allocated count from the port.
  5502. */
  5503. if (emb == LPFC_SLI4_MBX_EMBED) {
  5504. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5505. shdr = &rsrc_ext->header.cfg_shdr;
  5506. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5507. } else {
  5508. virtaddr = mbox->sge_array->addr[0];
  5509. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5510. shdr = &n_rsrc->cfg_shdr;
  5511. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5512. }
  5513. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5514. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5515. "2984 Failed to read allocated resources "
  5516. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5517. type,
  5518. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5519. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5520. rc = -EIO;
  5521. goto err_exit;
  5522. }
  5523. err_exit:
  5524. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5525. return rc;
  5526. }
  5527. /**
  5528. * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
  5529. * @phba: pointer to lpfc hba data structure.
  5530. *
  5531. * This routine walks the list of els buffers that have been allocated and
  5532. * repost them to the port by using SGL block post. This is needed after a
  5533. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5534. * of els buffer sgls which contains contiguous xris and uses the non-embedded
  5535. * SGL block post mailbox commands to post them to the port. For single els
  5536. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5537. * mailbox command for posting.
  5538. *
  5539. * Returns: 0 = success, non-zero failure.
  5540. **/
  5541. static int
  5542. lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
  5543. {
  5544. struct lpfc_sglq *sglq_entry = NULL;
  5545. struct lpfc_sglq *sglq_entry_next = NULL;
  5546. struct lpfc_sglq *sglq_entry_first = NULL;
  5547. int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
  5548. int last_xritag = NO_XRI;
  5549. struct lpfc_sli_ring *pring;
  5550. LIST_HEAD(prep_sgl_list);
  5551. LIST_HEAD(blck_sgl_list);
  5552. LIST_HEAD(allc_sgl_list);
  5553. LIST_HEAD(post_sgl_list);
  5554. LIST_HEAD(free_sgl_list);
  5555. pring = &phba->sli.ring[LPFC_ELS_RING];
  5556. spin_lock_irq(&phba->hbalock);
  5557. spin_lock(&pring->ring_lock);
  5558. list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
  5559. spin_unlock(&pring->ring_lock);
  5560. spin_unlock_irq(&phba->hbalock);
  5561. total_cnt = phba->sli4_hba.els_xri_cnt;
  5562. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  5563. &allc_sgl_list, list) {
  5564. list_del_init(&sglq_entry->list);
  5565. block_cnt++;
  5566. if ((last_xritag != NO_XRI) &&
  5567. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  5568. /* a hole in xri block, form a sgl posting block */
  5569. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  5570. post_cnt = block_cnt - 1;
  5571. /* prepare list for next posting block */
  5572. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5573. block_cnt = 1;
  5574. } else {
  5575. /* prepare list for next posting block */
  5576. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5577. /* enough sgls for non-embed sgl mbox command */
  5578. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  5579. list_splice_init(&prep_sgl_list,
  5580. &blck_sgl_list);
  5581. post_cnt = block_cnt;
  5582. block_cnt = 0;
  5583. }
  5584. }
  5585. num_posted++;
  5586. /* keep track of last sgl's xritag */
  5587. last_xritag = sglq_entry->sli4_xritag;
  5588. /* end of repost sgl list condition for els buffers */
  5589. if (num_posted == phba->sli4_hba.els_xri_cnt) {
  5590. if (post_cnt == 0) {
  5591. list_splice_init(&prep_sgl_list,
  5592. &blck_sgl_list);
  5593. post_cnt = block_cnt;
  5594. } else if (block_cnt == 1) {
  5595. status = lpfc_sli4_post_sgl(phba,
  5596. sglq_entry->phys, 0,
  5597. sglq_entry->sli4_xritag);
  5598. if (!status) {
  5599. /* successful, put sgl to posted list */
  5600. list_add_tail(&sglq_entry->list,
  5601. &post_sgl_list);
  5602. } else {
  5603. /* Failure, put sgl to free list */
  5604. lpfc_printf_log(phba, KERN_WARNING,
  5605. LOG_SLI,
  5606. "3159 Failed to post els "
  5607. "sgl, xritag:x%x\n",
  5608. sglq_entry->sli4_xritag);
  5609. list_add_tail(&sglq_entry->list,
  5610. &free_sgl_list);
  5611. total_cnt--;
  5612. }
  5613. }
  5614. }
  5615. /* continue until a nembed page worth of sgls */
  5616. if (post_cnt == 0)
  5617. continue;
  5618. /* post the els buffer list sgls as a block */
  5619. status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
  5620. post_cnt);
  5621. if (!status) {
  5622. /* success, put sgl list to posted sgl list */
  5623. list_splice_init(&blck_sgl_list, &post_sgl_list);
  5624. } else {
  5625. /* Failure, put sgl list to free sgl list */
  5626. sglq_entry_first = list_first_entry(&blck_sgl_list,
  5627. struct lpfc_sglq,
  5628. list);
  5629. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5630. "3160 Failed to post els sgl-list, "
  5631. "xritag:x%x-x%x\n",
  5632. sglq_entry_first->sli4_xritag,
  5633. (sglq_entry_first->sli4_xritag +
  5634. post_cnt - 1));
  5635. list_splice_init(&blck_sgl_list, &free_sgl_list);
  5636. total_cnt -= post_cnt;
  5637. }
  5638. /* don't reset xirtag due to hole in xri block */
  5639. if (block_cnt == 0)
  5640. last_xritag = NO_XRI;
  5641. /* reset els sgl post count for next round of posting */
  5642. post_cnt = 0;
  5643. }
  5644. /* update the number of XRIs posted for ELS */
  5645. phba->sli4_hba.els_xri_cnt = total_cnt;
  5646. /* free the els sgls failed to post */
  5647. lpfc_free_sgl_list(phba, &free_sgl_list);
  5648. /* push els sgls posted to the availble list */
  5649. if (!list_empty(&post_sgl_list)) {
  5650. spin_lock_irq(&phba->hbalock);
  5651. spin_lock(&pring->ring_lock);
  5652. list_splice_init(&post_sgl_list,
  5653. &phba->sli4_hba.lpfc_sgl_list);
  5654. spin_unlock(&pring->ring_lock);
  5655. spin_unlock_irq(&phba->hbalock);
  5656. } else {
  5657. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5658. "3161 Failure to post els sgl to port.\n");
  5659. return -EIO;
  5660. }
  5661. return 0;
  5662. }
  5663. /**
  5664. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5665. * @phba: Pointer to HBA context object.
  5666. *
  5667. * This function is the main SLI4 device intialization PCI function. This
  5668. * function is called by the HBA intialization code, HBA reset code and
  5669. * HBA error attention handler code. Caller is not required to hold any
  5670. * locks.
  5671. **/
  5672. int
  5673. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5674. {
  5675. int rc;
  5676. LPFC_MBOXQ_t *mboxq;
  5677. struct lpfc_mqe *mqe;
  5678. uint8_t *vpd;
  5679. uint32_t vpd_size;
  5680. uint32_t ftr_rsp = 0;
  5681. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5682. struct lpfc_vport *vport = phba->pport;
  5683. struct lpfc_dmabuf *mp;
  5684. /* Perform a PCI function reset to start from clean */
  5685. rc = lpfc_pci_function_reset(phba);
  5686. if (unlikely(rc))
  5687. return -ENODEV;
  5688. /* Check the HBA Host Status Register for readyness */
  5689. rc = lpfc_sli4_post_status_check(phba);
  5690. if (unlikely(rc))
  5691. return -ENODEV;
  5692. else {
  5693. spin_lock_irq(&phba->hbalock);
  5694. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5695. spin_unlock_irq(&phba->hbalock);
  5696. }
  5697. /*
  5698. * Allocate a single mailbox container for initializing the
  5699. * port.
  5700. */
  5701. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5702. if (!mboxq)
  5703. return -ENOMEM;
  5704. /* Issue READ_REV to collect vpd and FW information. */
  5705. vpd_size = SLI4_PAGE_SIZE;
  5706. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5707. if (!vpd) {
  5708. rc = -ENOMEM;
  5709. goto out_free_mbox;
  5710. }
  5711. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5712. if (unlikely(rc)) {
  5713. kfree(vpd);
  5714. goto out_free_mbox;
  5715. }
  5716. mqe = &mboxq->u.mqe;
  5717. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5718. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5719. phba->hba_flag |= HBA_FCOE_MODE;
  5720. else
  5721. phba->hba_flag &= ~HBA_FCOE_MODE;
  5722. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5723. LPFC_DCBX_CEE_MODE)
  5724. phba->hba_flag |= HBA_FIP_SUPPORT;
  5725. else
  5726. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5727. phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
  5728. if (phba->sli_rev != LPFC_SLI_REV4) {
  5729. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5730. "0376 READ_REV Error. SLI Level %d "
  5731. "FCoE enabled %d\n",
  5732. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5733. rc = -EIO;
  5734. kfree(vpd);
  5735. goto out_free_mbox;
  5736. }
  5737. /*
  5738. * Continue initialization with default values even if driver failed
  5739. * to read FCoE param config regions, only read parameters if the
  5740. * board is FCoE
  5741. */
  5742. if (phba->hba_flag & HBA_FCOE_MODE &&
  5743. lpfc_sli4_read_fcoe_params(phba))
  5744. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5745. "2570 Failed to read FCoE parameters\n");
  5746. /*
  5747. * Retrieve sli4 device physical port name, failure of doing it
  5748. * is considered as non-fatal.
  5749. */
  5750. rc = lpfc_sli4_retrieve_pport_name(phba);
  5751. if (!rc)
  5752. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5753. "3080 Successful retrieving SLI4 device "
  5754. "physical port name: %s.\n", phba->Port);
  5755. /*
  5756. * Evaluate the read rev and vpd data. Populate the driver
  5757. * state with the results. If this routine fails, the failure
  5758. * is not fatal as the driver will use generic values.
  5759. */
  5760. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5761. if (unlikely(!rc)) {
  5762. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5763. "0377 Error %d parsing vpd. "
  5764. "Using defaults.\n", rc);
  5765. rc = 0;
  5766. }
  5767. kfree(vpd);
  5768. /* Save information as VPD data */
  5769. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5770. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5771. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5772. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5773. &mqe->un.read_rev);
  5774. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5775. &mqe->un.read_rev);
  5776. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5777. &mqe->un.read_rev);
  5778. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5779. &mqe->un.read_rev);
  5780. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5781. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5782. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5783. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5784. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5785. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5786. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5787. "(%d):0380 READ_REV Status x%x "
  5788. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5789. mboxq->vport ? mboxq->vport->vpi : 0,
  5790. bf_get(lpfc_mqe_status, mqe),
  5791. phba->vpd.rev.opFwName,
  5792. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5793. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5794. /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
  5795. rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
  5796. if (phba->pport->cfg_lun_queue_depth > rc) {
  5797. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  5798. "3362 LUN queue depth changed from %d to %d\n",
  5799. phba->pport->cfg_lun_queue_depth, rc);
  5800. phba->pport->cfg_lun_queue_depth = rc;
  5801. }
  5802. /*
  5803. * Discover the port's supported feature set and match it against the
  5804. * hosts requests.
  5805. */
  5806. lpfc_request_features(phba, mboxq);
  5807. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5808. if (unlikely(rc)) {
  5809. rc = -EIO;
  5810. goto out_free_mbox;
  5811. }
  5812. /*
  5813. * The port must support FCP initiator mode as this is the
  5814. * only mode running in the host.
  5815. */
  5816. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5817. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5818. "0378 No support for fcpi mode.\n");
  5819. ftr_rsp++;
  5820. }
  5821. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5822. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5823. else
  5824. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5825. /*
  5826. * If the port cannot support the host's requested features
  5827. * then turn off the global config parameters to disable the
  5828. * feature in the driver. This is not a fatal error.
  5829. */
  5830. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5831. if (phba->cfg_enable_bg) {
  5832. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5833. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5834. else
  5835. ftr_rsp++;
  5836. }
  5837. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5838. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5839. ftr_rsp++;
  5840. if (ftr_rsp) {
  5841. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5842. "0379 Feature Mismatch Data: x%08x %08x "
  5843. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5844. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5845. phba->cfg_enable_npiv, phba->max_vpi);
  5846. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5847. phba->cfg_enable_bg = 0;
  5848. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5849. phba->cfg_enable_npiv = 0;
  5850. }
  5851. /* These SLI3 features are assumed in SLI4 */
  5852. spin_lock_irq(&phba->hbalock);
  5853. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5854. spin_unlock_irq(&phba->hbalock);
  5855. /*
  5856. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5857. * calls depends on these resources to complete port setup.
  5858. */
  5859. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5860. if (rc) {
  5861. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5862. "2920 Failed to alloc Resource IDs "
  5863. "rc = x%x\n", rc);
  5864. goto out_free_mbox;
  5865. }
  5866. /* Read the port's service parameters. */
  5867. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5868. if (rc) {
  5869. phba->link_state = LPFC_HBA_ERROR;
  5870. rc = -ENOMEM;
  5871. goto out_free_mbox;
  5872. }
  5873. mboxq->vport = vport;
  5874. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5875. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5876. if (rc == MBX_SUCCESS) {
  5877. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5878. rc = 0;
  5879. }
  5880. /*
  5881. * This memory was allocated by the lpfc_read_sparam routine. Release
  5882. * it to the mbuf pool.
  5883. */
  5884. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5885. kfree(mp);
  5886. mboxq->context1 = NULL;
  5887. if (unlikely(rc)) {
  5888. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5889. "0382 READ_SPARAM command failed "
  5890. "status %d, mbxStatus x%x\n",
  5891. rc, bf_get(lpfc_mqe_status, mqe));
  5892. phba->link_state = LPFC_HBA_ERROR;
  5893. rc = -EIO;
  5894. goto out_free_mbox;
  5895. }
  5896. lpfc_update_vport_wwn(vport);
  5897. /* Update the fc_host data structures with new wwn. */
  5898. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5899. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5900. /* update host els and scsi xri-sgl sizes and mappings */
  5901. rc = lpfc_sli4_xri_sgl_update(phba);
  5902. if (unlikely(rc)) {
  5903. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5904. "1400 Failed to update xri-sgl size and "
  5905. "mapping: %d\n", rc);
  5906. goto out_free_mbox;
  5907. }
  5908. /* register the els sgl pool to the port */
  5909. rc = lpfc_sli4_repost_els_sgl_list(phba);
  5910. if (unlikely(rc)) {
  5911. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5912. "0582 Error %d during els sgl post "
  5913. "operation\n", rc);
  5914. rc = -ENODEV;
  5915. goto out_free_mbox;
  5916. }
  5917. /* register the allocated scsi sgl pool to the port */
  5918. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5919. if (unlikely(rc)) {
  5920. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5921. "0383 Error %d during scsi sgl post "
  5922. "operation\n", rc);
  5923. /* Some Scsi buffers were moved to the abort scsi list */
  5924. /* A pci function reset will repost them */
  5925. rc = -ENODEV;
  5926. goto out_free_mbox;
  5927. }
  5928. /* Post the rpi header region to the device. */
  5929. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5930. if (unlikely(rc)) {
  5931. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5932. "0393 Error %d during rpi post operation\n",
  5933. rc);
  5934. rc = -ENODEV;
  5935. goto out_free_mbox;
  5936. }
  5937. lpfc_sli4_node_prep(phba);
  5938. /* Create all the SLI4 queues */
  5939. rc = lpfc_sli4_queue_create(phba);
  5940. if (rc) {
  5941. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5942. "3089 Failed to allocate queues\n");
  5943. rc = -ENODEV;
  5944. goto out_stop_timers;
  5945. }
  5946. /* Set up all the queues to the device */
  5947. rc = lpfc_sli4_queue_setup(phba);
  5948. if (unlikely(rc)) {
  5949. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5950. "0381 Error %d during queue setup.\n ", rc);
  5951. goto out_destroy_queue;
  5952. }
  5953. /* Arm the CQs and then EQs on device */
  5954. lpfc_sli4_arm_cqeq_intr(phba);
  5955. /* Indicate device interrupt mode */
  5956. phba->sli4_hba.intr_enable = 1;
  5957. /* Allow asynchronous mailbox command to go through */
  5958. spin_lock_irq(&phba->hbalock);
  5959. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5960. spin_unlock_irq(&phba->hbalock);
  5961. /* Post receive buffers to the device */
  5962. lpfc_sli4_rb_setup(phba);
  5963. /* Reset HBA FCF states after HBA reset */
  5964. phba->fcf.fcf_flag = 0;
  5965. phba->fcf.current_rec.flag = 0;
  5966. /* Start the ELS watchdog timer */
  5967. mod_timer(&vport->els_tmofunc,
  5968. jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
  5969. /* Start heart beat timer */
  5970. mod_timer(&phba->hb_tmofunc,
  5971. jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
  5972. phba->hb_outstanding = 0;
  5973. phba->last_completion_time = jiffies;
  5974. /* Start error attention (ERATT) polling timer */
  5975. mod_timer(&phba->eratt_poll,
  5976. jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
  5977. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5978. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5979. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5980. if (!rc) {
  5981. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5982. "2829 This device supports "
  5983. "Advanced Error Reporting (AER)\n");
  5984. spin_lock_irq(&phba->hbalock);
  5985. phba->hba_flag |= HBA_AER_ENABLED;
  5986. spin_unlock_irq(&phba->hbalock);
  5987. } else {
  5988. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5989. "2830 This device does not support "
  5990. "Advanced Error Reporting (AER)\n");
  5991. phba->cfg_aer_support = 0;
  5992. }
  5993. rc = 0;
  5994. }
  5995. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5996. /*
  5997. * The FC Port needs to register FCFI (index 0)
  5998. */
  5999. lpfc_reg_fcfi(phba, mboxq);
  6000. mboxq->vport = phba->pport;
  6001. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6002. if (rc != MBX_SUCCESS)
  6003. goto out_unset_queue;
  6004. rc = 0;
  6005. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  6006. &mboxq->u.mqe.un.reg_fcfi);
  6007. /* Check if the port is configured to be disabled */
  6008. lpfc_sli_read_link_ste(phba);
  6009. }
  6010. /*
  6011. * The port is ready, set the host's link state to LINK_DOWN
  6012. * in preparation for link interrupts.
  6013. */
  6014. spin_lock_irq(&phba->hbalock);
  6015. phba->link_state = LPFC_LINK_DOWN;
  6016. spin_unlock_irq(&phba->hbalock);
  6017. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  6018. (phba->hba_flag & LINK_DISABLED)) {
  6019. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  6020. "3103 Adapter Link is disabled.\n");
  6021. lpfc_down_link(phba, mboxq);
  6022. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6023. if (rc != MBX_SUCCESS) {
  6024. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  6025. "3104 Adapter failed to issue "
  6026. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  6027. goto out_unset_queue;
  6028. }
  6029. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  6030. /* don't perform init_link on SLI4 FC port loopback test */
  6031. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  6032. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  6033. if (rc)
  6034. goto out_unset_queue;
  6035. }
  6036. }
  6037. mempool_free(mboxq, phba->mbox_mem_pool);
  6038. return rc;
  6039. out_unset_queue:
  6040. /* Unset all the queues set up in this routine when error out */
  6041. lpfc_sli4_queue_unset(phba);
  6042. out_destroy_queue:
  6043. lpfc_sli4_queue_destroy(phba);
  6044. out_stop_timers:
  6045. lpfc_stop_hba_timers(phba);
  6046. out_free_mbox:
  6047. mempool_free(mboxq, phba->mbox_mem_pool);
  6048. return rc;
  6049. }
  6050. /**
  6051. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  6052. * @ptr: context object - pointer to hba structure.
  6053. *
  6054. * This is the callback function for mailbox timer. The mailbox
  6055. * timer is armed when a new mailbox command is issued and the timer
  6056. * is deleted when the mailbox complete. The function is called by
  6057. * the kernel timer code when a mailbox does not complete within
  6058. * expected time. This function wakes up the worker thread to
  6059. * process the mailbox timeout and returns. All the processing is
  6060. * done by the worker thread function lpfc_mbox_timeout_handler.
  6061. **/
  6062. void
  6063. lpfc_mbox_timeout(unsigned long ptr)
  6064. {
  6065. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  6066. unsigned long iflag;
  6067. uint32_t tmo_posted;
  6068. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  6069. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  6070. if (!tmo_posted)
  6071. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  6072. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  6073. if (!tmo_posted)
  6074. lpfc_worker_wake_up(phba);
  6075. return;
  6076. }
  6077. /**
  6078. * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
  6079. * are pending
  6080. * @phba: Pointer to HBA context object.
  6081. *
  6082. * This function checks if any mailbox completions are present on the mailbox
  6083. * completion queue.
  6084. **/
  6085. bool
  6086. lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
  6087. {
  6088. uint32_t idx;
  6089. struct lpfc_queue *mcq;
  6090. struct lpfc_mcqe *mcqe;
  6091. bool pending_completions = false;
  6092. if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
  6093. return false;
  6094. /* Check for completions on mailbox completion queue */
  6095. mcq = phba->sli4_hba.mbx_cq;
  6096. idx = mcq->hba_index;
  6097. while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
  6098. mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
  6099. if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
  6100. (!bf_get_le32(lpfc_trailer_async, mcqe))) {
  6101. pending_completions = true;
  6102. break;
  6103. }
  6104. idx = (idx + 1) % mcq->entry_count;
  6105. if (mcq->hba_index == idx)
  6106. break;
  6107. }
  6108. return pending_completions;
  6109. }
  6110. /**
  6111. * lpfc_sli4_process_missed_mbox_completions - process mbox completions
  6112. * that were missed.
  6113. * @phba: Pointer to HBA context object.
  6114. *
  6115. * For sli4, it is possible to miss an interrupt. As such mbox completions
  6116. * maybe missed causing erroneous mailbox timeouts to occur. This function
  6117. * checks to see if mbox completions are on the mailbox completion queue
  6118. * and will process all the completions associated with the eq for the
  6119. * mailbox completion queue.
  6120. **/
  6121. bool
  6122. lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
  6123. {
  6124. uint32_t eqidx;
  6125. struct lpfc_queue *fpeq = NULL;
  6126. struct lpfc_eqe *eqe;
  6127. bool mbox_pending;
  6128. if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
  6129. return false;
  6130. /* Find the eq associated with the mcq */
  6131. if (phba->sli4_hba.hba_eq)
  6132. for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
  6133. if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
  6134. phba->sli4_hba.mbx_cq->assoc_qid) {
  6135. fpeq = phba->sli4_hba.hba_eq[eqidx];
  6136. break;
  6137. }
  6138. if (!fpeq)
  6139. return false;
  6140. /* Turn off interrupts from this EQ */
  6141. lpfc_sli4_eq_clr_intr(fpeq);
  6142. /* Check to see if a mbox completion is pending */
  6143. mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
  6144. /*
  6145. * If a mbox completion is pending, process all the events on EQ
  6146. * associated with the mbox completion queue (this could include
  6147. * mailbox commands, async events, els commands, receive queue data
  6148. * and fcp commands)
  6149. */
  6150. if (mbox_pending)
  6151. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  6152. lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
  6153. fpeq->EQ_processed++;
  6154. }
  6155. /* Always clear and re-arm the EQ */
  6156. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  6157. return mbox_pending;
  6158. }
  6159. /**
  6160. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  6161. * @phba: Pointer to HBA context object.
  6162. *
  6163. * This function is called from worker thread when a mailbox command times out.
  6164. * The caller is not required to hold any locks. This function will reset the
  6165. * HBA and recover all the pending commands.
  6166. **/
  6167. void
  6168. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  6169. {
  6170. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  6171. MAILBOX_t *mb = NULL;
  6172. struct lpfc_sli *psli = &phba->sli;
  6173. /* If the mailbox completed, process the completion and return */
  6174. if (lpfc_sli4_process_missed_mbox_completions(phba))
  6175. return;
  6176. if (pmbox != NULL)
  6177. mb = &pmbox->u.mb;
  6178. /* Check the pmbox pointer first. There is a race condition
  6179. * between the mbox timeout handler getting executed in the
  6180. * worklist and the mailbox actually completing. When this
  6181. * race condition occurs, the mbox_active will be NULL.
  6182. */
  6183. spin_lock_irq(&phba->hbalock);
  6184. if (pmbox == NULL) {
  6185. lpfc_printf_log(phba, KERN_WARNING,
  6186. LOG_MBOX | LOG_SLI,
  6187. "0353 Active Mailbox cleared - mailbox timeout "
  6188. "exiting\n");
  6189. spin_unlock_irq(&phba->hbalock);
  6190. return;
  6191. }
  6192. /* Mbox cmd <mbxCommand> timeout */
  6193. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6194. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  6195. mb->mbxCommand,
  6196. phba->pport->port_state,
  6197. phba->sli.sli_flag,
  6198. phba->sli.mbox_active);
  6199. spin_unlock_irq(&phba->hbalock);
  6200. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  6201. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  6202. * it to fail all outstanding SCSI IO.
  6203. */
  6204. spin_lock_irq(&phba->pport->work_port_lock);
  6205. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  6206. spin_unlock_irq(&phba->pport->work_port_lock);
  6207. spin_lock_irq(&phba->hbalock);
  6208. phba->link_state = LPFC_LINK_UNKNOWN;
  6209. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  6210. spin_unlock_irq(&phba->hbalock);
  6211. lpfc_sli_abort_fcp_rings(phba);
  6212. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6213. "0345 Resetting board due to mailbox timeout\n");
  6214. /* Reset the HBA device */
  6215. lpfc_reset_hba(phba);
  6216. }
  6217. /**
  6218. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  6219. * @phba: Pointer to HBA context object.
  6220. * @pmbox: Pointer to mailbox object.
  6221. * @flag: Flag indicating how the mailbox need to be processed.
  6222. *
  6223. * This function is called by discovery code and HBA management code
  6224. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  6225. * function gets the hbalock to protect the data structures.
  6226. * The mailbox command can be submitted in polling mode, in which case
  6227. * this function will wait in a polling loop for the completion of the
  6228. * mailbox.
  6229. * If the mailbox is submitted in no_wait mode (not polling) the
  6230. * function will submit the command and returns immediately without waiting
  6231. * for the mailbox completion. The no_wait is supported only when HBA
  6232. * is in SLI2/SLI3 mode - interrupts are enabled.
  6233. * The SLI interface allows only one mailbox pending at a time. If the
  6234. * mailbox is issued in polling mode and there is already a mailbox
  6235. * pending, then the function will return an error. If the mailbox is issued
  6236. * in NO_WAIT mode and there is a mailbox pending already, the function
  6237. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  6238. * The sli layer owns the mailbox object until the completion of mailbox
  6239. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  6240. * return codes the caller owns the mailbox command after the return of
  6241. * the function.
  6242. **/
  6243. static int
  6244. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  6245. uint32_t flag)
  6246. {
  6247. MAILBOX_t *mbx;
  6248. struct lpfc_sli *psli = &phba->sli;
  6249. uint32_t status, evtctr;
  6250. uint32_t ha_copy, hc_copy;
  6251. int i;
  6252. unsigned long timeout;
  6253. unsigned long drvr_flag = 0;
  6254. uint32_t word0, ldata;
  6255. void __iomem *to_slim;
  6256. int processing_queue = 0;
  6257. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6258. if (!pmbox) {
  6259. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6260. /* processing mbox queue from intr_handler */
  6261. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6262. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6263. return MBX_SUCCESS;
  6264. }
  6265. processing_queue = 1;
  6266. pmbox = lpfc_mbox_get(phba);
  6267. if (!pmbox) {
  6268. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6269. return MBX_SUCCESS;
  6270. }
  6271. }
  6272. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  6273. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  6274. if(!pmbox->vport) {
  6275. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6276. lpfc_printf_log(phba, KERN_ERR,
  6277. LOG_MBOX | LOG_VPORT,
  6278. "1806 Mbox x%x failed. No vport\n",
  6279. pmbox->u.mb.mbxCommand);
  6280. dump_stack();
  6281. goto out_not_finished;
  6282. }
  6283. }
  6284. /* If the PCI channel is in offline state, do not post mbox. */
  6285. if (unlikely(pci_channel_offline(phba->pcidev))) {
  6286. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6287. goto out_not_finished;
  6288. }
  6289. /* If HBA has a deferred error attention, fail the iocb. */
  6290. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  6291. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6292. goto out_not_finished;
  6293. }
  6294. psli = &phba->sli;
  6295. mbx = &pmbox->u.mb;
  6296. status = MBX_SUCCESS;
  6297. if (phba->link_state == LPFC_HBA_ERROR) {
  6298. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6299. /* Mbox command <mbxCommand> cannot issue */
  6300. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6301. "(%d):0311 Mailbox command x%x cannot "
  6302. "issue Data: x%x x%x\n",
  6303. pmbox->vport ? pmbox->vport->vpi : 0,
  6304. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6305. goto out_not_finished;
  6306. }
  6307. if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6308. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6309. !(hc_copy & HC_MBINT_ENA)) {
  6310. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6311. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6312. "(%d):2528 Mailbox command x%x cannot "
  6313. "issue Data: x%x x%x\n",
  6314. pmbox->vport ? pmbox->vport->vpi : 0,
  6315. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6316. goto out_not_finished;
  6317. }
  6318. }
  6319. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6320. /* Polling for a mbox command when another one is already active
  6321. * is not allowed in SLI. Also, the driver must have established
  6322. * SLI2 mode to queue and process multiple mbox commands.
  6323. */
  6324. if (flag & MBX_POLL) {
  6325. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6326. /* Mbox command <mbxCommand> cannot issue */
  6327. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6328. "(%d):2529 Mailbox command x%x "
  6329. "cannot issue Data: x%x x%x\n",
  6330. pmbox->vport ? pmbox->vport->vpi : 0,
  6331. pmbox->u.mb.mbxCommand,
  6332. psli->sli_flag, flag);
  6333. goto out_not_finished;
  6334. }
  6335. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  6336. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6337. /* Mbox command <mbxCommand> cannot issue */
  6338. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6339. "(%d):2530 Mailbox command x%x "
  6340. "cannot issue Data: x%x x%x\n",
  6341. pmbox->vport ? pmbox->vport->vpi : 0,
  6342. pmbox->u.mb.mbxCommand,
  6343. psli->sli_flag, flag);
  6344. goto out_not_finished;
  6345. }
  6346. /* Another mailbox command is still being processed, queue this
  6347. * command to be processed later.
  6348. */
  6349. lpfc_mbox_put(phba, pmbox);
  6350. /* Mbox cmd issue - BUSY */
  6351. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6352. "(%d):0308 Mbox cmd issue - BUSY Data: "
  6353. "x%x x%x x%x x%x\n",
  6354. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  6355. mbx->mbxCommand, phba->pport->port_state,
  6356. psli->sli_flag, flag);
  6357. psli->slistat.mbox_busy++;
  6358. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6359. if (pmbox->vport) {
  6360. lpfc_debugfs_disc_trc(pmbox->vport,
  6361. LPFC_DISC_TRC_MBOX_VPORT,
  6362. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  6363. (uint32_t)mbx->mbxCommand,
  6364. mbx->un.varWords[0], mbx->un.varWords[1]);
  6365. }
  6366. else {
  6367. lpfc_debugfs_disc_trc(phba->pport,
  6368. LPFC_DISC_TRC_MBOX,
  6369. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  6370. (uint32_t)mbx->mbxCommand,
  6371. mbx->un.varWords[0], mbx->un.varWords[1]);
  6372. }
  6373. return MBX_BUSY;
  6374. }
  6375. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6376. /* If we are not polling, we MUST be in SLI2 mode */
  6377. if (flag != MBX_POLL) {
  6378. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  6379. (mbx->mbxCommand != MBX_KILL_BOARD)) {
  6380. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6381. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6382. /* Mbox command <mbxCommand> cannot issue */
  6383. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6384. "(%d):2531 Mailbox command x%x "
  6385. "cannot issue Data: x%x x%x\n",
  6386. pmbox->vport ? pmbox->vport->vpi : 0,
  6387. pmbox->u.mb.mbxCommand,
  6388. psli->sli_flag, flag);
  6389. goto out_not_finished;
  6390. }
  6391. /* timeout active mbox command */
  6392. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6393. 1000);
  6394. mod_timer(&psli->mbox_tmo, jiffies + timeout);
  6395. }
  6396. /* Mailbox cmd <cmd> issue */
  6397. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6398. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  6399. "x%x\n",
  6400. pmbox->vport ? pmbox->vport->vpi : 0,
  6401. mbx->mbxCommand, phba->pport->port_state,
  6402. psli->sli_flag, flag);
  6403. if (mbx->mbxCommand != MBX_HEARTBEAT) {
  6404. if (pmbox->vport) {
  6405. lpfc_debugfs_disc_trc(pmbox->vport,
  6406. LPFC_DISC_TRC_MBOX_VPORT,
  6407. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6408. (uint32_t)mbx->mbxCommand,
  6409. mbx->un.varWords[0], mbx->un.varWords[1]);
  6410. }
  6411. else {
  6412. lpfc_debugfs_disc_trc(phba->pport,
  6413. LPFC_DISC_TRC_MBOX,
  6414. "MBOX Send: cmd:x%x mb:x%x x%x",
  6415. (uint32_t)mbx->mbxCommand,
  6416. mbx->un.varWords[0], mbx->un.varWords[1]);
  6417. }
  6418. }
  6419. psli->slistat.mbox_cmd++;
  6420. evtctr = psli->slistat.mbox_event;
  6421. /* next set own bit for the adapter and copy over command word */
  6422. mbx->mbxOwner = OWN_CHIP;
  6423. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6424. /* Populate mbox extension offset word. */
  6425. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6426. *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
  6427. = (uint8_t *)phba->mbox_ext
  6428. - (uint8_t *)phba->mbox;
  6429. }
  6430. /* Copy the mailbox extension data */
  6431. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6432. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6433. (uint8_t *)phba->mbox_ext,
  6434. pmbox->in_ext_byte_len);
  6435. }
  6436. /* Copy command data to host SLIM area */
  6437. lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
  6438. } else {
  6439. /* Populate mbox extension offset word. */
  6440. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6441. *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
  6442. = MAILBOX_HBA_EXT_OFFSET;
  6443. /* Copy the mailbox extension data */
  6444. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6445. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6446. MAILBOX_HBA_EXT_OFFSET,
  6447. pmbox->context2, pmbox->in_ext_byte_len);
  6448. }
  6449. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6450. /* copy command data into host mbox for cmpl */
  6451. lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
  6452. }
  6453. /* First copy mbox command data to HBA SLIM, skip past first
  6454. word */
  6455. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6456. lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
  6457. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6458. /* Next copy over first word, with mbxOwner set */
  6459. ldata = *((uint32_t *)mbx);
  6460. to_slim = phba->MBslimaddr;
  6461. writel(ldata, to_slim);
  6462. readl(to_slim); /* flush */
  6463. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6464. /* switch over to host mailbox */
  6465. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6466. }
  6467. }
  6468. wmb();
  6469. switch (flag) {
  6470. case MBX_NOWAIT:
  6471. /* Set up reference to mailbox command */
  6472. psli->mbox_active = pmbox;
  6473. /* Interrupt board to do it */
  6474. writel(CA_MBATT, phba->CAregaddr);
  6475. readl(phba->CAregaddr); /* flush */
  6476. /* Don't wait for it to finish, just return */
  6477. break;
  6478. case MBX_POLL:
  6479. /* Set up null reference to mailbox command */
  6480. psli->mbox_active = NULL;
  6481. /* Interrupt board to do it */
  6482. writel(CA_MBATT, phba->CAregaddr);
  6483. readl(phba->CAregaddr); /* flush */
  6484. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6485. /* First read mbox status word */
  6486. word0 = *((uint32_t *)phba->mbox);
  6487. word0 = le32_to_cpu(word0);
  6488. } else {
  6489. /* First read mbox status word */
  6490. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6491. spin_unlock_irqrestore(&phba->hbalock,
  6492. drvr_flag);
  6493. goto out_not_finished;
  6494. }
  6495. }
  6496. /* Read the HBA Host Attention Register */
  6497. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6498. spin_unlock_irqrestore(&phba->hbalock,
  6499. drvr_flag);
  6500. goto out_not_finished;
  6501. }
  6502. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6503. 1000) + jiffies;
  6504. i = 0;
  6505. /* Wait for command to complete */
  6506. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6507. (!(ha_copy & HA_MBATT) &&
  6508. (phba->link_state > LPFC_WARM_START))) {
  6509. if (time_after(jiffies, timeout)) {
  6510. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6511. spin_unlock_irqrestore(&phba->hbalock,
  6512. drvr_flag);
  6513. goto out_not_finished;
  6514. }
  6515. /* Check if we took a mbox interrupt while we were
  6516. polling */
  6517. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6518. && (evtctr != psli->slistat.mbox_event))
  6519. break;
  6520. if (i++ > 10) {
  6521. spin_unlock_irqrestore(&phba->hbalock,
  6522. drvr_flag);
  6523. msleep(1);
  6524. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6525. }
  6526. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6527. /* First copy command data */
  6528. word0 = *((uint32_t *)phba->mbox);
  6529. word0 = le32_to_cpu(word0);
  6530. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6531. MAILBOX_t *slimmb;
  6532. uint32_t slimword0;
  6533. /* Check real SLIM for any errors */
  6534. slimword0 = readl(phba->MBslimaddr);
  6535. slimmb = (MAILBOX_t *) & slimword0;
  6536. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6537. && slimmb->mbxStatus) {
  6538. psli->sli_flag &=
  6539. ~LPFC_SLI_ACTIVE;
  6540. word0 = slimword0;
  6541. }
  6542. }
  6543. } else {
  6544. /* First copy command data */
  6545. word0 = readl(phba->MBslimaddr);
  6546. }
  6547. /* Read the HBA Host Attention Register */
  6548. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6549. spin_unlock_irqrestore(&phba->hbalock,
  6550. drvr_flag);
  6551. goto out_not_finished;
  6552. }
  6553. }
  6554. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6555. /* copy results back to user */
  6556. lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
  6557. /* Copy the mailbox extension data */
  6558. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6559. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6560. pmbox->context2,
  6561. pmbox->out_ext_byte_len);
  6562. }
  6563. } else {
  6564. /* First copy command data */
  6565. lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
  6566. MAILBOX_CMD_SIZE);
  6567. /* Copy the mailbox extension data */
  6568. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6569. lpfc_memcpy_from_slim(pmbox->context2,
  6570. phba->MBslimaddr +
  6571. MAILBOX_HBA_EXT_OFFSET,
  6572. pmbox->out_ext_byte_len);
  6573. }
  6574. }
  6575. writel(HA_MBATT, phba->HAregaddr);
  6576. readl(phba->HAregaddr); /* flush */
  6577. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6578. status = mbx->mbxStatus;
  6579. }
  6580. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6581. return status;
  6582. out_not_finished:
  6583. if (processing_queue) {
  6584. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6585. lpfc_mbox_cmpl_put(phba, pmbox);
  6586. }
  6587. return MBX_NOT_FINISHED;
  6588. }
  6589. /**
  6590. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6591. * @phba: Pointer to HBA context object.
  6592. *
  6593. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6594. * the driver internal pending mailbox queue. It will then try to wait out the
  6595. * possible outstanding mailbox command before return.
  6596. *
  6597. * Returns:
  6598. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6599. * the outstanding mailbox command timed out.
  6600. **/
  6601. static int
  6602. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6603. {
  6604. struct lpfc_sli *psli = &phba->sli;
  6605. int rc = 0;
  6606. unsigned long timeout = 0;
  6607. /* Mark the asynchronous mailbox command posting as blocked */
  6608. spin_lock_irq(&phba->hbalock);
  6609. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6610. /* Determine how long we might wait for the active mailbox
  6611. * command to be gracefully completed by firmware.
  6612. */
  6613. if (phba->sli.mbox_active)
  6614. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6615. phba->sli.mbox_active) *
  6616. 1000) + jiffies;
  6617. spin_unlock_irq(&phba->hbalock);
  6618. /* Make sure the mailbox is really active */
  6619. if (timeout)
  6620. lpfc_sli4_process_missed_mbox_completions(phba);
  6621. /* Wait for the outstnading mailbox command to complete */
  6622. while (phba->sli.mbox_active) {
  6623. /* Check active mailbox complete status every 2ms */
  6624. msleep(2);
  6625. if (time_after(jiffies, timeout)) {
  6626. /* Timeout, marked the outstanding cmd not complete */
  6627. rc = 1;
  6628. break;
  6629. }
  6630. }
  6631. /* Can not cleanly block async mailbox command, fails it */
  6632. if (rc) {
  6633. spin_lock_irq(&phba->hbalock);
  6634. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6635. spin_unlock_irq(&phba->hbalock);
  6636. }
  6637. return rc;
  6638. }
  6639. /**
  6640. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6641. * @phba: Pointer to HBA context object.
  6642. *
  6643. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6644. * commands from the driver internal pending mailbox queue. It makes sure
  6645. * that there is no outstanding mailbox command before resuming posting
  6646. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6647. * mailbox command, it will try to wait it out before resuming asynchronous
  6648. * mailbox command posting.
  6649. **/
  6650. static void
  6651. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6652. {
  6653. struct lpfc_sli *psli = &phba->sli;
  6654. spin_lock_irq(&phba->hbalock);
  6655. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6656. /* Asynchronous mailbox posting is not blocked, do nothing */
  6657. spin_unlock_irq(&phba->hbalock);
  6658. return;
  6659. }
  6660. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6661. * successful or timeout, after timing-out the outstanding mailbox
  6662. * command shall always be removed, so just unblock posting async
  6663. * mailbox command and resume
  6664. */
  6665. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6666. spin_unlock_irq(&phba->hbalock);
  6667. /* wake up worker thread to post asynchronlous mailbox command */
  6668. lpfc_worker_wake_up(phba);
  6669. }
  6670. /**
  6671. * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
  6672. * @phba: Pointer to HBA context object.
  6673. * @mboxq: Pointer to mailbox object.
  6674. *
  6675. * The function waits for the bootstrap mailbox register ready bit from
  6676. * port for twice the regular mailbox command timeout value.
  6677. *
  6678. * 0 - no timeout on waiting for bootstrap mailbox register ready.
  6679. * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
  6680. **/
  6681. static int
  6682. lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6683. {
  6684. uint32_t db_ready;
  6685. unsigned long timeout;
  6686. struct lpfc_register bmbx_reg;
  6687. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6688. * 1000) + jiffies;
  6689. do {
  6690. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6691. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6692. if (!db_ready)
  6693. msleep(2);
  6694. if (time_after(jiffies, timeout))
  6695. return MBXERR_ERROR;
  6696. } while (!db_ready);
  6697. return 0;
  6698. }
  6699. /**
  6700. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6701. * @phba: Pointer to HBA context object.
  6702. * @mboxq: Pointer to mailbox object.
  6703. *
  6704. * The function posts a mailbox to the port. The mailbox is expected
  6705. * to be comletely filled in and ready for the port to operate on it.
  6706. * This routine executes a synchronous completion operation on the
  6707. * mailbox by polling for its completion.
  6708. *
  6709. * The caller must not be holding any locks when calling this routine.
  6710. *
  6711. * Returns:
  6712. * MBX_SUCCESS - mailbox posted successfully
  6713. * Any of the MBX error values.
  6714. **/
  6715. static int
  6716. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6717. {
  6718. int rc = MBX_SUCCESS;
  6719. unsigned long iflag;
  6720. uint32_t mcqe_status;
  6721. uint32_t mbx_cmnd;
  6722. struct lpfc_sli *psli = &phba->sli;
  6723. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6724. struct lpfc_bmbx_create *mbox_rgn;
  6725. struct dma_address *dma_address;
  6726. /*
  6727. * Only one mailbox can be active to the bootstrap mailbox region
  6728. * at a time and there is no queueing provided.
  6729. */
  6730. spin_lock_irqsave(&phba->hbalock, iflag);
  6731. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6732. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6733. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6734. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6735. "cannot issue Data: x%x x%x\n",
  6736. mboxq->vport ? mboxq->vport->vpi : 0,
  6737. mboxq->u.mb.mbxCommand,
  6738. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6739. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6740. psli->sli_flag, MBX_POLL);
  6741. return MBXERR_ERROR;
  6742. }
  6743. /* The server grabs the token and owns it until release */
  6744. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6745. phba->sli.mbox_active = mboxq;
  6746. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6747. /* wait for bootstrap mbox register for readyness */
  6748. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6749. if (rc)
  6750. goto exit;
  6751. /*
  6752. * Initialize the bootstrap memory region to avoid stale data areas
  6753. * in the mailbox post. Then copy the caller's mailbox contents to
  6754. * the bmbx mailbox region.
  6755. */
  6756. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6757. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6758. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6759. sizeof(struct lpfc_mqe));
  6760. /* Post the high mailbox dma address to the port and wait for ready. */
  6761. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6762. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6763. /* wait for bootstrap mbox register for hi-address write done */
  6764. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6765. if (rc)
  6766. goto exit;
  6767. /* Post the low mailbox dma address to the port. */
  6768. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6769. /* wait for bootstrap mbox register for low address write done */
  6770. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6771. if (rc)
  6772. goto exit;
  6773. /*
  6774. * Read the CQ to ensure the mailbox has completed.
  6775. * If so, update the mailbox status so that the upper layers
  6776. * can complete the request normally.
  6777. */
  6778. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6779. sizeof(struct lpfc_mqe));
  6780. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6781. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6782. sizeof(struct lpfc_mcqe));
  6783. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6784. /*
  6785. * When the CQE status indicates a failure and the mailbox status
  6786. * indicates success then copy the CQE status into the mailbox status
  6787. * (and prefix it with x4000).
  6788. */
  6789. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6790. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6791. bf_set(lpfc_mqe_status, mb,
  6792. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6793. rc = MBXERR_ERROR;
  6794. } else
  6795. lpfc_sli4_swap_str(phba, mboxq);
  6796. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6797. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6798. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6799. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6800. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6801. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6802. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6803. bf_get(lpfc_mqe_status, mb),
  6804. mb->un.mb_words[0], mb->un.mb_words[1],
  6805. mb->un.mb_words[2], mb->un.mb_words[3],
  6806. mb->un.mb_words[4], mb->un.mb_words[5],
  6807. mb->un.mb_words[6], mb->un.mb_words[7],
  6808. mb->un.mb_words[8], mb->un.mb_words[9],
  6809. mb->un.mb_words[10], mb->un.mb_words[11],
  6810. mb->un.mb_words[12], mboxq->mcqe.word0,
  6811. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6812. mboxq->mcqe.trailer);
  6813. exit:
  6814. /* We are holding the token, no needed for lock when release */
  6815. spin_lock_irqsave(&phba->hbalock, iflag);
  6816. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6817. phba->sli.mbox_active = NULL;
  6818. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6819. return rc;
  6820. }
  6821. /**
  6822. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6823. * @phba: Pointer to HBA context object.
  6824. * @pmbox: Pointer to mailbox object.
  6825. * @flag: Flag indicating how the mailbox need to be processed.
  6826. *
  6827. * This function is called by discovery code and HBA management code to submit
  6828. * a mailbox command to firmware with SLI-4 interface spec.
  6829. *
  6830. * Return codes the caller owns the mailbox command after the return of the
  6831. * function.
  6832. **/
  6833. static int
  6834. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6835. uint32_t flag)
  6836. {
  6837. struct lpfc_sli *psli = &phba->sli;
  6838. unsigned long iflags;
  6839. int rc;
  6840. /* dump from issue mailbox command if setup */
  6841. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6842. rc = lpfc_mbox_dev_check(phba);
  6843. if (unlikely(rc)) {
  6844. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6845. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6846. "cannot issue Data: x%x x%x\n",
  6847. mboxq->vport ? mboxq->vport->vpi : 0,
  6848. mboxq->u.mb.mbxCommand,
  6849. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6850. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6851. psli->sli_flag, flag);
  6852. goto out_not_finished;
  6853. }
  6854. /* Detect polling mode and jump to a handler */
  6855. if (!phba->sli4_hba.intr_enable) {
  6856. if (flag == MBX_POLL)
  6857. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6858. else
  6859. rc = -EIO;
  6860. if (rc != MBX_SUCCESS)
  6861. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6862. "(%d):2541 Mailbox command x%x "
  6863. "(x%x/x%x) failure: "
  6864. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6865. "Data: x%x x%x\n,",
  6866. mboxq->vport ? mboxq->vport->vpi : 0,
  6867. mboxq->u.mb.mbxCommand,
  6868. lpfc_sli_config_mbox_subsys_get(phba,
  6869. mboxq),
  6870. lpfc_sli_config_mbox_opcode_get(phba,
  6871. mboxq),
  6872. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6873. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6874. bf_get(lpfc_mcqe_ext_status,
  6875. &mboxq->mcqe),
  6876. psli->sli_flag, flag);
  6877. return rc;
  6878. } else if (flag == MBX_POLL) {
  6879. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6880. "(%d):2542 Try to issue mailbox command "
  6881. "x%x (x%x/x%x) synchronously ahead of async"
  6882. "mailbox command queue: x%x x%x\n",
  6883. mboxq->vport ? mboxq->vport->vpi : 0,
  6884. mboxq->u.mb.mbxCommand,
  6885. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6886. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6887. psli->sli_flag, flag);
  6888. /* Try to block the asynchronous mailbox posting */
  6889. rc = lpfc_sli4_async_mbox_block(phba);
  6890. if (!rc) {
  6891. /* Successfully blocked, now issue sync mbox cmd */
  6892. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6893. if (rc != MBX_SUCCESS)
  6894. lpfc_printf_log(phba, KERN_WARNING,
  6895. LOG_MBOX | LOG_SLI,
  6896. "(%d):2597 Sync Mailbox command "
  6897. "x%x (x%x/x%x) failure: "
  6898. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6899. "Data: x%x x%x\n,",
  6900. mboxq->vport ? mboxq->vport->vpi : 0,
  6901. mboxq->u.mb.mbxCommand,
  6902. lpfc_sli_config_mbox_subsys_get(phba,
  6903. mboxq),
  6904. lpfc_sli_config_mbox_opcode_get(phba,
  6905. mboxq),
  6906. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6907. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6908. bf_get(lpfc_mcqe_ext_status,
  6909. &mboxq->mcqe),
  6910. psli->sli_flag, flag);
  6911. /* Unblock the async mailbox posting afterward */
  6912. lpfc_sli4_async_mbox_unblock(phba);
  6913. }
  6914. return rc;
  6915. }
  6916. /* Now, interrupt mode asynchrous mailbox command */
  6917. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6918. if (rc) {
  6919. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6920. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6921. "cannot issue Data: x%x x%x\n",
  6922. mboxq->vport ? mboxq->vport->vpi : 0,
  6923. mboxq->u.mb.mbxCommand,
  6924. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6925. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6926. psli->sli_flag, flag);
  6927. goto out_not_finished;
  6928. }
  6929. /* Put the mailbox command to the driver internal FIFO */
  6930. psli->slistat.mbox_busy++;
  6931. spin_lock_irqsave(&phba->hbalock, iflags);
  6932. lpfc_mbox_put(phba, mboxq);
  6933. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6934. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6935. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6936. "x%x (x%x/x%x) x%x x%x x%x\n",
  6937. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6938. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6939. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6940. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6941. phba->pport->port_state,
  6942. psli->sli_flag, MBX_NOWAIT);
  6943. /* Wake up worker thread to transport mailbox command from head */
  6944. lpfc_worker_wake_up(phba);
  6945. return MBX_BUSY;
  6946. out_not_finished:
  6947. return MBX_NOT_FINISHED;
  6948. }
  6949. /**
  6950. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6951. * @phba: Pointer to HBA context object.
  6952. *
  6953. * This function is called by worker thread to send a mailbox command to
  6954. * SLI4 HBA firmware.
  6955. *
  6956. **/
  6957. int
  6958. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6959. {
  6960. struct lpfc_sli *psli = &phba->sli;
  6961. LPFC_MBOXQ_t *mboxq;
  6962. int rc = MBX_SUCCESS;
  6963. unsigned long iflags;
  6964. struct lpfc_mqe *mqe;
  6965. uint32_t mbx_cmnd;
  6966. /* Check interrupt mode before post async mailbox command */
  6967. if (unlikely(!phba->sli4_hba.intr_enable))
  6968. return MBX_NOT_FINISHED;
  6969. /* Check for mailbox command service token */
  6970. spin_lock_irqsave(&phba->hbalock, iflags);
  6971. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6972. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6973. return MBX_NOT_FINISHED;
  6974. }
  6975. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6976. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6977. return MBX_NOT_FINISHED;
  6978. }
  6979. if (unlikely(phba->sli.mbox_active)) {
  6980. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6981. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6982. "0384 There is pending active mailbox cmd\n");
  6983. return MBX_NOT_FINISHED;
  6984. }
  6985. /* Take the mailbox command service token */
  6986. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6987. /* Get the next mailbox command from head of queue */
  6988. mboxq = lpfc_mbox_get(phba);
  6989. /* If no more mailbox command waiting for post, we're done */
  6990. if (!mboxq) {
  6991. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6992. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6993. return MBX_SUCCESS;
  6994. }
  6995. phba->sli.mbox_active = mboxq;
  6996. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6997. /* Check device readiness for posting mailbox command */
  6998. rc = lpfc_mbox_dev_check(phba);
  6999. if (unlikely(rc))
  7000. /* Driver clean routine will clean up pending mailbox */
  7001. goto out_not_finished;
  7002. /* Prepare the mbox command to be posted */
  7003. mqe = &mboxq->u.mqe;
  7004. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  7005. /* Start timer for the mbox_tmo and log some mailbox post messages */
  7006. mod_timer(&psli->mbox_tmo, (jiffies +
  7007. msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
  7008. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  7009. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  7010. "x%x x%x\n",
  7011. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  7012. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  7013. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  7014. phba->pport->port_state, psli->sli_flag);
  7015. if (mbx_cmnd != MBX_HEARTBEAT) {
  7016. if (mboxq->vport) {
  7017. lpfc_debugfs_disc_trc(mboxq->vport,
  7018. LPFC_DISC_TRC_MBOX_VPORT,
  7019. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  7020. mbx_cmnd, mqe->un.mb_words[0],
  7021. mqe->un.mb_words[1]);
  7022. } else {
  7023. lpfc_debugfs_disc_trc(phba->pport,
  7024. LPFC_DISC_TRC_MBOX,
  7025. "MBOX Send: cmd:x%x mb:x%x x%x",
  7026. mbx_cmnd, mqe->un.mb_words[0],
  7027. mqe->un.mb_words[1]);
  7028. }
  7029. }
  7030. psli->slistat.mbox_cmd++;
  7031. /* Post the mailbox command to the port */
  7032. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  7033. if (rc != MBX_SUCCESS) {
  7034. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  7035. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  7036. "cannot issue Data: x%x x%x\n",
  7037. mboxq->vport ? mboxq->vport->vpi : 0,
  7038. mboxq->u.mb.mbxCommand,
  7039. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  7040. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  7041. psli->sli_flag, MBX_NOWAIT);
  7042. goto out_not_finished;
  7043. }
  7044. return rc;
  7045. out_not_finished:
  7046. spin_lock_irqsave(&phba->hbalock, iflags);
  7047. if (phba->sli.mbox_active) {
  7048. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  7049. __lpfc_mbox_cmpl_put(phba, mboxq);
  7050. /* Release the token */
  7051. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7052. phba->sli.mbox_active = NULL;
  7053. }
  7054. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7055. return MBX_NOT_FINISHED;
  7056. }
  7057. /**
  7058. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  7059. * @phba: Pointer to HBA context object.
  7060. * @pmbox: Pointer to mailbox object.
  7061. * @flag: Flag indicating how the mailbox need to be processed.
  7062. *
  7063. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  7064. * the API jump table function pointer from the lpfc_hba struct.
  7065. *
  7066. * Return codes the caller owns the mailbox command after the return of the
  7067. * function.
  7068. **/
  7069. int
  7070. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  7071. {
  7072. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  7073. }
  7074. /**
  7075. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  7076. * @phba: The hba struct for which this call is being executed.
  7077. * @dev_grp: The HBA PCI-Device group number.
  7078. *
  7079. * This routine sets up the mbox interface API function jump table in @phba
  7080. * struct.
  7081. * Returns: 0 - success, -ENODEV - failure.
  7082. **/
  7083. int
  7084. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7085. {
  7086. switch (dev_grp) {
  7087. case LPFC_PCI_DEV_LP:
  7088. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  7089. phba->lpfc_sli_handle_slow_ring_event =
  7090. lpfc_sli_handle_slow_ring_event_s3;
  7091. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  7092. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  7093. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  7094. break;
  7095. case LPFC_PCI_DEV_OC:
  7096. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  7097. phba->lpfc_sli_handle_slow_ring_event =
  7098. lpfc_sli_handle_slow_ring_event_s4;
  7099. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  7100. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  7101. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  7102. break;
  7103. default:
  7104. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7105. "1420 Invalid HBA PCI-device group: 0x%x\n",
  7106. dev_grp);
  7107. return -ENODEV;
  7108. break;
  7109. }
  7110. return 0;
  7111. }
  7112. /**
  7113. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  7114. * @phba: Pointer to HBA context object.
  7115. * @pring: Pointer to driver SLI ring object.
  7116. * @piocb: Pointer to address of newly added command iocb.
  7117. *
  7118. * This function is called with hbalock held to add a command
  7119. * iocb to the txq when SLI layer cannot submit the command iocb
  7120. * to the ring.
  7121. **/
  7122. void
  7123. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7124. struct lpfc_iocbq *piocb)
  7125. {
  7126. /* Insert the caller's iocb in the txq tail for later processing. */
  7127. list_add_tail(&piocb->list, &pring->txq);
  7128. }
  7129. /**
  7130. * lpfc_sli_next_iocb - Get the next iocb in the txq
  7131. * @phba: Pointer to HBA context object.
  7132. * @pring: Pointer to driver SLI ring object.
  7133. * @piocb: Pointer to address of newly added command iocb.
  7134. *
  7135. * This function is called with hbalock held before a new
  7136. * iocb is submitted to the firmware. This function checks
  7137. * txq to flush the iocbs in txq to Firmware before
  7138. * submitting new iocbs to the Firmware.
  7139. * If there are iocbs in the txq which need to be submitted
  7140. * to firmware, lpfc_sli_next_iocb returns the first element
  7141. * of the txq after dequeuing it from txq.
  7142. * If there is no iocb in the txq then the function will return
  7143. * *piocb and *piocb is set to NULL. Caller needs to check
  7144. * *piocb to find if there are more commands in the txq.
  7145. **/
  7146. static struct lpfc_iocbq *
  7147. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7148. struct lpfc_iocbq **piocb)
  7149. {
  7150. struct lpfc_iocbq * nextiocb;
  7151. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  7152. if (!nextiocb) {
  7153. nextiocb = *piocb;
  7154. *piocb = NULL;
  7155. }
  7156. return nextiocb;
  7157. }
  7158. /**
  7159. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  7160. * @phba: Pointer to HBA context object.
  7161. * @ring_number: SLI ring number to issue iocb on.
  7162. * @piocb: Pointer to command iocb.
  7163. * @flag: Flag indicating if this command can be put into txq.
  7164. *
  7165. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  7166. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  7167. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  7168. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  7169. * this function allows only iocbs for posting buffers. This function finds
  7170. * next available slot in the command ring and posts the command to the
  7171. * available slot and writes the port attention register to request HBA start
  7172. * processing new iocb. If there is no slot available in the ring and
  7173. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  7174. * the function returns IOCB_BUSY.
  7175. *
  7176. * This function is called with hbalock held. The function will return success
  7177. * after it successfully submit the iocb to firmware or after adding to the
  7178. * txq.
  7179. **/
  7180. static int
  7181. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  7182. struct lpfc_iocbq *piocb, uint32_t flag)
  7183. {
  7184. struct lpfc_iocbq *nextiocb;
  7185. IOCB_t *iocb;
  7186. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7187. if (piocb->iocb_cmpl && (!piocb->vport) &&
  7188. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  7189. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  7190. lpfc_printf_log(phba, KERN_ERR,
  7191. LOG_SLI | LOG_VPORT,
  7192. "1807 IOCB x%x failed. No vport\n",
  7193. piocb->iocb.ulpCommand);
  7194. dump_stack();
  7195. return IOCB_ERROR;
  7196. }
  7197. /* If the PCI channel is in offline state, do not post iocbs. */
  7198. if (unlikely(pci_channel_offline(phba->pcidev)))
  7199. return IOCB_ERROR;
  7200. /* If HBA has a deferred error attention, fail the iocb. */
  7201. if (unlikely(phba->hba_flag & DEFER_ERATT))
  7202. return IOCB_ERROR;
  7203. /*
  7204. * We should never get an IOCB if we are in a < LINK_DOWN state
  7205. */
  7206. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  7207. return IOCB_ERROR;
  7208. /*
  7209. * Check to see if we are blocking IOCB processing because of a
  7210. * outstanding event.
  7211. */
  7212. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  7213. goto iocb_busy;
  7214. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  7215. /*
  7216. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  7217. * can be issued if the link is not up.
  7218. */
  7219. switch (piocb->iocb.ulpCommand) {
  7220. case CMD_GEN_REQUEST64_CR:
  7221. case CMD_GEN_REQUEST64_CX:
  7222. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  7223. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  7224. FC_RCTL_DD_UNSOL_CMD) ||
  7225. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  7226. MENLO_TRANSPORT_TYPE))
  7227. goto iocb_busy;
  7228. break;
  7229. case CMD_QUE_RING_BUF_CN:
  7230. case CMD_QUE_RING_BUF64_CN:
  7231. /*
  7232. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  7233. * completion, iocb_cmpl MUST be 0.
  7234. */
  7235. if (piocb->iocb_cmpl)
  7236. piocb->iocb_cmpl = NULL;
  7237. /*FALLTHROUGH*/
  7238. case CMD_CREATE_XRI_CR:
  7239. case CMD_CLOSE_XRI_CN:
  7240. case CMD_CLOSE_XRI_CX:
  7241. break;
  7242. default:
  7243. goto iocb_busy;
  7244. }
  7245. /*
  7246. * For FCP commands, we must be in a state where we can process link
  7247. * attention events.
  7248. */
  7249. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  7250. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  7251. goto iocb_busy;
  7252. }
  7253. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  7254. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  7255. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  7256. if (iocb)
  7257. lpfc_sli_update_ring(phba, pring);
  7258. else
  7259. lpfc_sli_update_full_ring(phba, pring);
  7260. if (!piocb)
  7261. return IOCB_SUCCESS;
  7262. goto out_busy;
  7263. iocb_busy:
  7264. pring->stats.iocb_cmd_delay++;
  7265. out_busy:
  7266. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7267. __lpfc_sli_ringtx_put(phba, pring, piocb);
  7268. return IOCB_SUCCESS;
  7269. }
  7270. return IOCB_BUSY;
  7271. }
  7272. /**
  7273. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  7274. * @phba: Pointer to HBA context object.
  7275. * @piocb: Pointer to command iocb.
  7276. * @sglq: Pointer to the scatter gather queue object.
  7277. *
  7278. * This routine converts the bpl or bde that is in the IOCB
  7279. * to a sgl list for the sli4 hardware. The physical address
  7280. * of the bpl/bde is converted back to a virtual address.
  7281. * If the IOCB contains a BPL then the list of BDE's is
  7282. * converted to sli4_sge's. If the IOCB contains a single
  7283. * BDE then it is converted to a single sli_sge.
  7284. * The IOCB is still in cpu endianess so the contents of
  7285. * the bpl can be used without byte swapping.
  7286. *
  7287. * Returns valid XRI = Success, NO_XRI = Failure.
  7288. **/
  7289. static uint16_t
  7290. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  7291. struct lpfc_sglq *sglq)
  7292. {
  7293. uint16_t xritag = NO_XRI;
  7294. struct ulp_bde64 *bpl = NULL;
  7295. struct ulp_bde64 bde;
  7296. struct sli4_sge *sgl = NULL;
  7297. struct lpfc_dmabuf *dmabuf;
  7298. IOCB_t *icmd;
  7299. int numBdes = 0;
  7300. int i = 0;
  7301. uint32_t offset = 0; /* accumulated offset in the sg request list */
  7302. int inbound = 0; /* number of sg reply entries inbound from firmware */
  7303. if (!piocbq || !sglq)
  7304. return xritag;
  7305. sgl = (struct sli4_sge *)sglq->sgl;
  7306. icmd = &piocbq->iocb;
  7307. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  7308. return sglq->sli4_xritag;
  7309. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7310. numBdes = icmd->un.genreq64.bdl.bdeSize /
  7311. sizeof(struct ulp_bde64);
  7312. /* The addrHigh and addrLow fields within the IOCB
  7313. * have not been byteswapped yet so there is no
  7314. * need to swap them back.
  7315. */
  7316. if (piocbq->context3)
  7317. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  7318. else
  7319. return xritag;
  7320. bpl = (struct ulp_bde64 *)dmabuf->virt;
  7321. if (!bpl)
  7322. return xritag;
  7323. for (i = 0; i < numBdes; i++) {
  7324. /* Should already be byte swapped. */
  7325. sgl->addr_hi = bpl->addrHigh;
  7326. sgl->addr_lo = bpl->addrLow;
  7327. sgl->word2 = le32_to_cpu(sgl->word2);
  7328. if ((i+1) == numBdes)
  7329. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7330. else
  7331. bf_set(lpfc_sli4_sge_last, sgl, 0);
  7332. /* swap the size field back to the cpu so we
  7333. * can assign it to the sgl.
  7334. */
  7335. bde.tus.w = le32_to_cpu(bpl->tus.w);
  7336. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  7337. /* The offsets in the sgl need to be accumulated
  7338. * separately for the request and reply lists.
  7339. * The request is always first, the reply follows.
  7340. */
  7341. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  7342. /* add up the reply sg entries */
  7343. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  7344. inbound++;
  7345. /* first inbound? reset the offset */
  7346. if (inbound == 1)
  7347. offset = 0;
  7348. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  7349. bf_set(lpfc_sli4_sge_type, sgl,
  7350. LPFC_SGE_TYPE_DATA);
  7351. offset += bde.tus.f.bdeSize;
  7352. }
  7353. sgl->word2 = cpu_to_le32(sgl->word2);
  7354. bpl++;
  7355. sgl++;
  7356. }
  7357. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  7358. /* The addrHigh and addrLow fields of the BDE have not
  7359. * been byteswapped yet so they need to be swapped
  7360. * before putting them in the sgl.
  7361. */
  7362. sgl->addr_hi =
  7363. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  7364. sgl->addr_lo =
  7365. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  7366. sgl->word2 = le32_to_cpu(sgl->word2);
  7367. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7368. sgl->word2 = cpu_to_le32(sgl->word2);
  7369. sgl->sge_len =
  7370. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  7371. }
  7372. return sglq->sli4_xritag;
  7373. }
  7374. /**
  7375. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  7376. * @phba: Pointer to HBA context object.
  7377. * @piocb: Pointer to command iocb.
  7378. * @wqe: Pointer to the work queue entry.
  7379. *
  7380. * This routine converts the iocb command to its Work Queue Entry
  7381. * equivalent. The wqe pointer should not have any fields set when
  7382. * this routine is called because it will memcpy over them.
  7383. * This routine does not set the CQ_ID or the WQEC bits in the
  7384. * wqe.
  7385. *
  7386. * Returns: 0 = Success, IOCB_ERROR = Failure.
  7387. **/
  7388. static int
  7389. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  7390. union lpfc_wqe *wqe)
  7391. {
  7392. uint32_t xmit_len = 0, total_len = 0;
  7393. uint8_t ct = 0;
  7394. uint32_t fip;
  7395. uint32_t abort_tag;
  7396. uint8_t command_type = ELS_COMMAND_NON_FIP;
  7397. uint8_t cmnd;
  7398. uint16_t xritag;
  7399. uint16_t abrt_iotag;
  7400. struct lpfc_iocbq *abrtiocbq;
  7401. struct ulp_bde64 *bpl = NULL;
  7402. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  7403. int numBdes, i;
  7404. struct ulp_bde64 bde;
  7405. struct lpfc_nodelist *ndlp;
  7406. uint32_t *pcmd;
  7407. uint32_t if_type;
  7408. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  7409. /* The fcp commands will set command type */
  7410. if (iocbq->iocb_flag & LPFC_IO_FCP)
  7411. command_type = FCP_COMMAND;
  7412. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  7413. command_type = ELS_COMMAND_FIP;
  7414. else
  7415. command_type = ELS_COMMAND_NON_FIP;
  7416. /* Some of the fields are in the right position already */
  7417. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  7418. abort_tag = (uint32_t) iocbq->iotag;
  7419. xritag = iocbq->sli4_xritag;
  7420. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  7421. wqe->generic.wqe_com.word10 = 0;
  7422. /* words0-2 bpl convert bde */
  7423. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7424. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7425. sizeof(struct ulp_bde64);
  7426. bpl = (struct ulp_bde64 *)
  7427. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  7428. if (!bpl)
  7429. return IOCB_ERROR;
  7430. /* Should already be byte swapped. */
  7431. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7432. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7433. /* swap the size field back to the cpu so we
  7434. * can assign it to the sgl.
  7435. */
  7436. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7437. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7438. total_len = 0;
  7439. for (i = 0; i < numBdes; i++) {
  7440. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7441. total_len += bde.tus.f.bdeSize;
  7442. }
  7443. } else
  7444. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7445. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7446. cmnd = iocbq->iocb.ulpCommand;
  7447. switch (iocbq->iocb.ulpCommand) {
  7448. case CMD_ELS_REQUEST64_CR:
  7449. if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
  7450. ndlp = iocbq->context_un.ndlp;
  7451. else
  7452. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7453. if (!iocbq->iocb.ulpLe) {
  7454. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7455. "2007 Only Limited Edition cmd Format"
  7456. " supported 0x%x\n",
  7457. iocbq->iocb.ulpCommand);
  7458. return IOCB_ERROR;
  7459. }
  7460. wqe->els_req.payload_len = xmit_len;
  7461. /* Els_reguest64 has a TMO */
  7462. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7463. iocbq->iocb.ulpTimeout);
  7464. /* Need a VF for word 4 set the vf bit*/
  7465. bf_set(els_req64_vf, &wqe->els_req, 0);
  7466. /* And a VFID for word 12 */
  7467. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7468. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7469. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7470. iocbq->iocb.ulpContext);
  7471. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7472. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7473. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7474. if (command_type == ELS_COMMAND_FIP)
  7475. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7476. >> LPFC_FIP_ELS_ID_SHIFT);
  7477. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7478. iocbq->context2)->virt);
  7479. if_type = bf_get(lpfc_sli_intf_if_type,
  7480. &phba->sli4_hba.sli_intf);
  7481. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7482. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7483. *pcmd == ELS_CMD_SCR ||
  7484. *pcmd == ELS_CMD_FDISC ||
  7485. *pcmd == ELS_CMD_LOGO ||
  7486. *pcmd == ELS_CMD_PLOGI)) {
  7487. bf_set(els_req64_sp, &wqe->els_req, 1);
  7488. bf_set(els_req64_sid, &wqe->els_req,
  7489. iocbq->vport->fc_myDID);
  7490. if ((*pcmd == ELS_CMD_FLOGI) &&
  7491. !(phba->fc_topology ==
  7492. LPFC_TOPOLOGY_LOOP))
  7493. bf_set(els_req64_sid, &wqe->els_req, 0);
  7494. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7495. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7496. phba->vpi_ids[iocbq->vport->vpi]);
  7497. } else if (pcmd && iocbq->context1) {
  7498. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7499. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7500. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7501. }
  7502. }
  7503. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7504. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7505. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7506. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7507. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7508. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7509. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7510. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7511. wqe->els_req.max_response_payload_len = total_len - xmit_len;
  7512. break;
  7513. case CMD_XMIT_SEQUENCE64_CX:
  7514. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7515. iocbq->iocb.un.ulpWord[3]);
  7516. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7517. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7518. /* The entire sequence is transmitted for this IOCB */
  7519. xmit_len = total_len;
  7520. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7521. if (phba->link_flag & LS_LOOPBACK_MODE)
  7522. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7523. case CMD_XMIT_SEQUENCE64_CR:
  7524. /* word3 iocb=io_tag32 wqe=reserved */
  7525. wqe->xmit_sequence.rsvd3 = 0;
  7526. /* word4 relative_offset memcpy */
  7527. /* word5 r_ctl/df_ctl memcpy */
  7528. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7529. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7530. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7531. LPFC_WQE_IOD_WRITE);
  7532. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7533. LPFC_WQE_LENLOC_WORD12);
  7534. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7535. wqe->xmit_sequence.xmit_len = xmit_len;
  7536. command_type = OTHER_COMMAND;
  7537. break;
  7538. case CMD_XMIT_BCAST64_CN:
  7539. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7540. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7541. /* word4 iocb=rsvd wqe=rsvd */
  7542. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7543. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7544. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7545. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7546. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7547. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7548. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7549. LPFC_WQE_LENLOC_WORD3);
  7550. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7551. break;
  7552. case CMD_FCP_IWRITE64_CR:
  7553. command_type = FCP_COMMAND_DATA_OUT;
  7554. /* word3 iocb=iotag wqe=payload_offset_len */
  7555. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7556. bf_set(payload_offset_len, &wqe->fcp_iwrite,
  7557. xmit_len + sizeof(struct fcp_rsp));
  7558. bf_set(cmd_buff_len, &wqe->fcp_iwrite,
  7559. 0);
  7560. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7561. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7562. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7563. iocbq->iocb.ulpFCP2Rcvy);
  7564. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7565. /* Always open the exchange */
  7566. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7567. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7568. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7569. LPFC_WQE_LENLOC_WORD4);
  7570. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7571. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7572. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7573. if (iocbq->iocb_flag & LPFC_IO_OAS) {
  7574. bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
  7575. if (phba->cfg_XLanePriority) {
  7576. bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
  7577. bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
  7578. (phba->cfg_XLanePriority << 1));
  7579. }
  7580. }
  7581. break;
  7582. case CMD_FCP_IREAD64_CR:
  7583. /* word3 iocb=iotag wqe=payload_offset_len */
  7584. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7585. bf_set(payload_offset_len, &wqe->fcp_iread,
  7586. xmit_len + sizeof(struct fcp_rsp));
  7587. bf_set(cmd_buff_len, &wqe->fcp_iread,
  7588. 0);
  7589. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7590. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7591. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7592. iocbq->iocb.ulpFCP2Rcvy);
  7593. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7594. /* Always open the exchange */
  7595. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7596. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7597. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7598. LPFC_WQE_LENLOC_WORD4);
  7599. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7600. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7601. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7602. if (iocbq->iocb_flag & LPFC_IO_OAS) {
  7603. bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
  7604. if (phba->cfg_XLanePriority) {
  7605. bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
  7606. bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
  7607. (phba->cfg_XLanePriority << 1));
  7608. }
  7609. }
  7610. break;
  7611. case CMD_FCP_ICMND64_CR:
  7612. /* word3 iocb=iotag wqe=payload_offset_len */
  7613. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7614. bf_set(payload_offset_len, &wqe->fcp_icmd,
  7615. xmit_len + sizeof(struct fcp_rsp));
  7616. bf_set(cmd_buff_len, &wqe->fcp_icmd,
  7617. 0);
  7618. /* word3 iocb=IO_TAG wqe=reserved */
  7619. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7620. /* Always open the exchange */
  7621. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7622. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7623. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7624. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7625. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7626. LPFC_WQE_LENLOC_NONE);
  7627. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7628. bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
  7629. iocbq->iocb.ulpFCP2Rcvy);
  7630. if (iocbq->iocb_flag & LPFC_IO_OAS) {
  7631. bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
  7632. if (phba->cfg_XLanePriority) {
  7633. bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
  7634. bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
  7635. (phba->cfg_XLanePriority << 1));
  7636. }
  7637. }
  7638. break;
  7639. case CMD_GEN_REQUEST64_CR:
  7640. /* For this command calculate the xmit length of the
  7641. * request bde.
  7642. */
  7643. xmit_len = 0;
  7644. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7645. sizeof(struct ulp_bde64);
  7646. for (i = 0; i < numBdes; i++) {
  7647. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7648. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7649. break;
  7650. xmit_len += bde.tus.f.bdeSize;
  7651. }
  7652. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7653. wqe->gen_req.request_payload_len = xmit_len;
  7654. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7655. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7656. /* word6 context tag copied in memcpy */
  7657. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7658. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7659. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7660. "2015 Invalid CT %x command 0x%x\n",
  7661. ct, iocbq->iocb.ulpCommand);
  7662. return IOCB_ERROR;
  7663. }
  7664. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7665. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7666. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7667. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7668. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7669. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7670. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7671. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7672. wqe->gen_req.max_response_payload_len = total_len - xmit_len;
  7673. command_type = OTHER_COMMAND;
  7674. break;
  7675. case CMD_XMIT_ELS_RSP64_CX:
  7676. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7677. /* words0-2 BDE memcpy */
  7678. /* word3 iocb=iotag32 wqe=response_payload_len */
  7679. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7680. /* word4 */
  7681. wqe->xmit_els_rsp.word4 = 0;
  7682. /* word5 iocb=rsvd wge=did */
  7683. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7684. iocbq->iocb.un.xseq64.xmit_els_remoteID);
  7685. if_type = bf_get(lpfc_sli_intf_if_type,
  7686. &phba->sli4_hba.sli_intf);
  7687. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7688. if (iocbq->vport->fc_flag & FC_PT2PT) {
  7689. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7690. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7691. iocbq->vport->fc_myDID);
  7692. if (iocbq->vport->fc_myDID == Fabric_DID) {
  7693. bf_set(wqe_els_did,
  7694. &wqe->xmit_els_rsp.wqe_dest, 0);
  7695. }
  7696. }
  7697. }
  7698. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7699. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7700. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7701. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7702. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7703. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7704. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7705. phba->vpi_ids[iocbq->vport->vpi]);
  7706. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7707. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7708. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7709. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7710. LPFC_WQE_LENLOC_WORD3);
  7711. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7712. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7713. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7714. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7715. iocbq->context2)->virt);
  7716. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7717. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7718. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7719. iocbq->vport->fc_myDID);
  7720. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
  7721. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7722. phba->vpi_ids[phba->pport->vpi]);
  7723. }
  7724. command_type = OTHER_COMMAND;
  7725. break;
  7726. case CMD_CLOSE_XRI_CN:
  7727. case CMD_ABORT_XRI_CN:
  7728. case CMD_ABORT_XRI_CX:
  7729. /* words 0-2 memcpy should be 0 rserved */
  7730. /* port will send abts */
  7731. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7732. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7733. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7734. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7735. } else
  7736. fip = 0;
  7737. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7738. /*
  7739. * The link is down, or the command was ELS_FIP
  7740. * so the fw does not need to send abts
  7741. * on the wire.
  7742. */
  7743. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7744. else
  7745. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7746. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7747. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7748. wqe->abort_cmd.rsrvd5 = 0;
  7749. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7750. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7751. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7752. /*
  7753. * The abort handler will send us CMD_ABORT_XRI_CN or
  7754. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7755. */
  7756. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7757. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7758. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7759. LPFC_WQE_LENLOC_NONE);
  7760. cmnd = CMD_ABORT_XRI_CX;
  7761. command_type = OTHER_COMMAND;
  7762. xritag = 0;
  7763. break;
  7764. case CMD_XMIT_BLS_RSP64_CX:
  7765. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7766. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7767. * we re-construct this WQE here based on information in
  7768. * iocbq from scratch.
  7769. */
  7770. memset(wqe, 0, sizeof(union lpfc_wqe));
  7771. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7772. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7773. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7774. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7775. LPFC_ABTS_UNSOL_INT) {
  7776. /* ABTS sent by initiator to CT exchange, the
  7777. * RX_ID field will be filled with the newly
  7778. * allocated responder XRI.
  7779. */
  7780. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7781. iocbq->sli4_xritag);
  7782. } else {
  7783. /* ABTS sent by responder to CT exchange, the
  7784. * RX_ID field will be filled with the responder
  7785. * RX_ID from ABTS.
  7786. */
  7787. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7788. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7789. }
  7790. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7791. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7792. /* Use CT=VPI */
  7793. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7794. ndlp->nlp_DID);
  7795. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7796. iocbq->iocb.ulpContext);
  7797. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7798. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7799. phba->vpi_ids[phba->pport->vpi]);
  7800. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7801. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7802. LPFC_WQE_LENLOC_NONE);
  7803. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7804. command_type = OTHER_COMMAND;
  7805. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7806. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7807. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7808. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7809. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7810. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7811. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7812. }
  7813. break;
  7814. case CMD_XRI_ABORTED_CX:
  7815. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7816. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7817. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7818. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7819. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7820. default:
  7821. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7822. "2014 Invalid command 0x%x\n",
  7823. iocbq->iocb.ulpCommand);
  7824. return IOCB_ERROR;
  7825. break;
  7826. }
  7827. if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
  7828. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
  7829. else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
  7830. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
  7831. else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
  7832. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
  7833. iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
  7834. LPFC_IO_DIF_INSERT);
  7835. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7836. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7837. wqe->generic.wqe_com.abort_tag = abort_tag;
  7838. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7839. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7840. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7841. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7842. return 0;
  7843. }
  7844. /**
  7845. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7846. * @phba: Pointer to HBA context object.
  7847. * @ring_number: SLI ring number to issue iocb on.
  7848. * @piocb: Pointer to command iocb.
  7849. * @flag: Flag indicating if this command can be put into txq.
  7850. *
  7851. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7852. * an iocb command to an HBA with SLI-4 interface spec.
  7853. *
  7854. * This function is called with hbalock held. The function will return success
  7855. * after it successfully submit the iocb to firmware or after adding to the
  7856. * txq.
  7857. **/
  7858. static int
  7859. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7860. struct lpfc_iocbq *piocb, uint32_t flag)
  7861. {
  7862. struct lpfc_sglq *sglq;
  7863. union lpfc_wqe wqe;
  7864. struct lpfc_queue *wq;
  7865. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7866. if (piocb->sli4_xritag == NO_XRI) {
  7867. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7868. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7869. sglq = NULL;
  7870. else {
  7871. if (!list_empty(&pring->txq)) {
  7872. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7873. __lpfc_sli_ringtx_put(phba,
  7874. pring, piocb);
  7875. return IOCB_SUCCESS;
  7876. } else {
  7877. return IOCB_BUSY;
  7878. }
  7879. } else {
  7880. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7881. if (!sglq) {
  7882. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7883. __lpfc_sli_ringtx_put(phba,
  7884. pring,
  7885. piocb);
  7886. return IOCB_SUCCESS;
  7887. } else
  7888. return IOCB_BUSY;
  7889. }
  7890. }
  7891. }
  7892. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7893. /* These IO's already have an XRI and a mapped sgl. */
  7894. sglq = NULL;
  7895. } else {
  7896. /*
  7897. * This is a continuation of a commandi,(CX) so this
  7898. * sglq is on the active list
  7899. */
  7900. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
  7901. if (!sglq)
  7902. return IOCB_ERROR;
  7903. }
  7904. if (sglq) {
  7905. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7906. piocb->sli4_xritag = sglq->sli4_xritag;
  7907. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7908. return IOCB_ERROR;
  7909. }
  7910. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7911. return IOCB_ERROR;
  7912. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7913. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7914. if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
  7915. wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
  7916. } else {
  7917. wq = phba->sli4_hba.oas_wq;
  7918. }
  7919. if (lpfc_sli4_wq_put(wq, &wqe))
  7920. return IOCB_ERROR;
  7921. } else {
  7922. if (unlikely(!phba->sli4_hba.els_wq))
  7923. return IOCB_ERROR;
  7924. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7925. return IOCB_ERROR;
  7926. }
  7927. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7928. return 0;
  7929. }
  7930. /**
  7931. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7932. *
  7933. * This routine wraps the actual lockless version for issusing IOCB function
  7934. * pointer from the lpfc_hba struct.
  7935. *
  7936. * Return codes:
  7937. * IOCB_ERROR - Error
  7938. * IOCB_SUCCESS - Success
  7939. * IOCB_BUSY - Busy
  7940. **/
  7941. int
  7942. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7943. struct lpfc_iocbq *piocb, uint32_t flag)
  7944. {
  7945. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7946. }
  7947. /**
  7948. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7949. * @phba: The hba struct for which this call is being executed.
  7950. * @dev_grp: The HBA PCI-Device group number.
  7951. *
  7952. * This routine sets up the SLI interface API function jump table in @phba
  7953. * struct.
  7954. * Returns: 0 - success, -ENODEV - failure.
  7955. **/
  7956. int
  7957. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7958. {
  7959. switch (dev_grp) {
  7960. case LPFC_PCI_DEV_LP:
  7961. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7962. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7963. break;
  7964. case LPFC_PCI_DEV_OC:
  7965. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7966. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7967. break;
  7968. default:
  7969. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7970. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7971. dev_grp);
  7972. return -ENODEV;
  7973. break;
  7974. }
  7975. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7976. return 0;
  7977. }
  7978. /**
  7979. * lpfc_sli_calc_ring - Calculates which ring to use
  7980. * @phba: Pointer to HBA context object.
  7981. * @ring_number: Initial ring
  7982. * @piocb: Pointer to command iocb.
  7983. *
  7984. * For SLI4, FCP IO can deferred to one fo many WQs, based on
  7985. * fcp_wqidx, thus we need to calculate the corresponding ring.
  7986. * Since ABORTS must go on the same WQ of the command they are
  7987. * aborting, we use command's fcp_wqidx.
  7988. */
  7989. int
  7990. lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
  7991. struct lpfc_iocbq *piocb)
  7992. {
  7993. if (phba->sli_rev < LPFC_SLI_REV4)
  7994. return ring_number;
  7995. if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
  7996. if (!(phba->cfg_fof) ||
  7997. (!(piocb->iocb_flag & LPFC_IO_FOF))) {
  7998. if (unlikely(!phba->sli4_hba.fcp_wq))
  7999. return LPFC_HBA_ERROR;
  8000. /*
  8001. * for abort iocb fcp_wqidx should already
  8002. * be setup based on what work queue we used.
  8003. */
  8004. if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
  8005. piocb->fcp_wqidx =
  8006. lpfc_sli4_scmd_to_wqidx_distr(phba,
  8007. piocb->context1);
  8008. ring_number = MAX_SLI3_CONFIGURED_RINGS +
  8009. piocb->fcp_wqidx;
  8010. } else {
  8011. if (unlikely(!phba->sli4_hba.oas_wq))
  8012. return LPFC_HBA_ERROR;
  8013. piocb->fcp_wqidx = 0;
  8014. ring_number = LPFC_FCP_OAS_RING;
  8015. }
  8016. }
  8017. return ring_number;
  8018. }
  8019. /**
  8020. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  8021. * @phba: Pointer to HBA context object.
  8022. * @pring: Pointer to driver SLI ring object.
  8023. * @piocb: Pointer to command iocb.
  8024. * @flag: Flag indicating if this command can be put into txq.
  8025. *
  8026. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  8027. * function. This function gets the hbalock and calls
  8028. * __lpfc_sli_issue_iocb function and will return the error returned
  8029. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  8030. * functions which do not hold hbalock.
  8031. **/
  8032. int
  8033. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  8034. struct lpfc_iocbq *piocb, uint32_t flag)
  8035. {
  8036. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  8037. struct lpfc_sli_ring *pring;
  8038. struct lpfc_queue *fpeq;
  8039. struct lpfc_eqe *eqe;
  8040. unsigned long iflags;
  8041. int rc, idx;
  8042. if (phba->sli_rev == LPFC_SLI_REV4) {
  8043. ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
  8044. if (unlikely(ring_number == LPFC_HBA_ERROR))
  8045. return IOCB_ERROR;
  8046. idx = piocb->fcp_wqidx;
  8047. pring = &phba->sli.ring[ring_number];
  8048. spin_lock_irqsave(&pring->ring_lock, iflags);
  8049. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  8050. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  8051. if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
  8052. fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
  8053. if (atomic_dec_and_test(&fcp_eq_hdl->
  8054. fcp_eq_in_use)) {
  8055. /* Get associated EQ with this index */
  8056. fpeq = phba->sli4_hba.hba_eq[idx];
  8057. /* Turn off interrupts from this EQ */
  8058. lpfc_sli4_eq_clr_intr(fpeq);
  8059. /*
  8060. * Process all the events on FCP EQ
  8061. */
  8062. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  8063. lpfc_sli4_hba_handle_eqe(phba,
  8064. eqe, idx);
  8065. fpeq->EQ_processed++;
  8066. }
  8067. /* Always clear and re-arm the EQ */
  8068. lpfc_sli4_eq_release(fpeq,
  8069. LPFC_QUEUE_REARM);
  8070. }
  8071. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  8072. }
  8073. } else {
  8074. /* For now, SLI2/3 will still use hbalock */
  8075. spin_lock_irqsave(&phba->hbalock, iflags);
  8076. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  8077. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8078. }
  8079. return rc;
  8080. }
  8081. /**
  8082. * lpfc_extra_ring_setup - Extra ring setup function
  8083. * @phba: Pointer to HBA context object.
  8084. *
  8085. * This function is called while driver attaches with the
  8086. * HBA to setup the extra ring. The extra ring is used
  8087. * only when driver needs to support target mode functionality
  8088. * or IP over FC functionalities.
  8089. *
  8090. * This function is called with no lock held.
  8091. **/
  8092. static int
  8093. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  8094. {
  8095. struct lpfc_sli *psli;
  8096. struct lpfc_sli_ring *pring;
  8097. psli = &phba->sli;
  8098. /* Adjust cmd/rsp ring iocb entries more evenly */
  8099. /* Take some away from the FCP ring */
  8100. pring = &psli->ring[psli->fcp_ring];
  8101. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  8102. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  8103. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  8104. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  8105. /* and give them to the extra ring */
  8106. pring = &psli->ring[psli->extra_ring];
  8107. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  8108. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  8109. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  8110. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  8111. /* Setup default profile for this ring */
  8112. pring->iotag_max = 4096;
  8113. pring->num_mask = 1;
  8114. pring->prt[0].profile = 0; /* Mask 0 */
  8115. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  8116. pring->prt[0].type = phba->cfg_multi_ring_type;
  8117. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  8118. return 0;
  8119. }
  8120. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  8121. * @phba: Pointer to HBA context object.
  8122. * @iocbq: Pointer to iocb object.
  8123. *
  8124. * The async_event handler calls this routine when it receives
  8125. * an ASYNC_STATUS_CN event from the port. The port generates
  8126. * this event when an Abort Sequence request to an rport fails
  8127. * twice in succession. The abort could be originated by the
  8128. * driver or by the port. The ABTS could have been for an ELS
  8129. * or FCP IO. The port only generates this event when an ABTS
  8130. * fails to complete after one retry.
  8131. */
  8132. static void
  8133. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  8134. struct lpfc_iocbq *iocbq)
  8135. {
  8136. struct lpfc_nodelist *ndlp = NULL;
  8137. uint16_t rpi = 0, vpi = 0;
  8138. struct lpfc_vport *vport = NULL;
  8139. /* The rpi in the ulpContext is vport-sensitive. */
  8140. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  8141. rpi = iocbq->iocb.ulpContext;
  8142. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8143. "3092 Port generated ABTS async event "
  8144. "on vpi %d rpi %d status 0x%x\n",
  8145. vpi, rpi, iocbq->iocb.ulpStatus);
  8146. vport = lpfc_find_vport_by_vpid(phba, vpi);
  8147. if (!vport)
  8148. goto err_exit;
  8149. ndlp = lpfc_findnode_rpi(vport, rpi);
  8150. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  8151. goto err_exit;
  8152. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  8153. lpfc_sli_abts_recover_port(vport, ndlp);
  8154. return;
  8155. err_exit:
  8156. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8157. "3095 Event Context not found, no "
  8158. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  8159. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  8160. vpi, rpi);
  8161. }
  8162. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  8163. * @phba: pointer to HBA context object.
  8164. * @ndlp: nodelist pointer for the impacted rport.
  8165. * @axri: pointer to the wcqe containing the failed exchange.
  8166. *
  8167. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  8168. * port. The port generates this event when an abort exchange request to an
  8169. * rport fails twice in succession with no reply. The abort could be originated
  8170. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  8171. */
  8172. void
  8173. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  8174. struct lpfc_nodelist *ndlp,
  8175. struct sli4_wcqe_xri_aborted *axri)
  8176. {
  8177. struct lpfc_vport *vport;
  8178. uint32_t ext_status = 0;
  8179. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  8180. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8181. "3115 Node Context not found, driver "
  8182. "ignoring abts err event\n");
  8183. return;
  8184. }
  8185. vport = ndlp->vport;
  8186. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8187. "3116 Port generated FCP XRI ABORT event on "
  8188. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  8189. ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
  8190. bf_get(lpfc_wcqe_xa_xri, axri),
  8191. bf_get(lpfc_wcqe_xa_status, axri),
  8192. axri->parameter);
  8193. /*
  8194. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  8195. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  8196. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  8197. */
  8198. ext_status = axri->parameter & IOERR_PARAM_MASK;
  8199. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  8200. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  8201. lpfc_sli_abts_recover_port(vport, ndlp);
  8202. }
  8203. /**
  8204. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  8205. * @phba: Pointer to HBA context object.
  8206. * @pring: Pointer to driver SLI ring object.
  8207. * @iocbq: Pointer to iocb object.
  8208. *
  8209. * This function is called by the slow ring event handler
  8210. * function when there is an ASYNC event iocb in the ring.
  8211. * This function is called with no lock held.
  8212. * Currently this function handles only temperature related
  8213. * ASYNC events. The function decodes the temperature sensor
  8214. * event message and posts events for the management applications.
  8215. **/
  8216. static void
  8217. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  8218. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  8219. {
  8220. IOCB_t *icmd;
  8221. uint16_t evt_code;
  8222. struct temp_event temp_event_data;
  8223. struct Scsi_Host *shost;
  8224. uint32_t *iocb_w;
  8225. icmd = &iocbq->iocb;
  8226. evt_code = icmd->un.asyncstat.evt_code;
  8227. switch (evt_code) {
  8228. case ASYNC_TEMP_WARN:
  8229. case ASYNC_TEMP_SAFE:
  8230. temp_event_data.data = (uint32_t) icmd->ulpContext;
  8231. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  8232. if (evt_code == ASYNC_TEMP_WARN) {
  8233. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  8234. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  8235. "0347 Adapter is very hot, please take "
  8236. "corrective action. temperature : %d Celsius\n",
  8237. (uint32_t) icmd->ulpContext);
  8238. } else {
  8239. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  8240. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  8241. "0340 Adapter temperature is OK now. "
  8242. "temperature : %d Celsius\n",
  8243. (uint32_t) icmd->ulpContext);
  8244. }
  8245. /* Send temperature change event to applications */
  8246. shost = lpfc_shost_from_vport(phba->pport);
  8247. fc_host_post_vendor_event(shost, fc_get_event_number(),
  8248. sizeof(temp_event_data), (char *) &temp_event_data,
  8249. LPFC_NL_VENDOR_ID);
  8250. break;
  8251. case ASYNC_STATUS_CN:
  8252. lpfc_sli_abts_err_handler(phba, iocbq);
  8253. break;
  8254. default:
  8255. iocb_w = (uint32_t *) icmd;
  8256. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8257. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  8258. " evt_code 0x%x\n"
  8259. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  8260. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  8261. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  8262. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  8263. pring->ringno, icmd->un.asyncstat.evt_code,
  8264. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  8265. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  8266. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  8267. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  8268. break;
  8269. }
  8270. }
  8271. /**
  8272. * lpfc_sli_setup - SLI ring setup function
  8273. * @phba: Pointer to HBA context object.
  8274. *
  8275. * lpfc_sli_setup sets up rings of the SLI interface with
  8276. * number of iocbs per ring and iotags. This function is
  8277. * called while driver attach to the HBA and before the
  8278. * interrupts are enabled. So there is no need for locking.
  8279. *
  8280. * This function always returns 0.
  8281. **/
  8282. int
  8283. lpfc_sli_setup(struct lpfc_hba *phba)
  8284. {
  8285. int i, totiocbsize = 0;
  8286. struct lpfc_sli *psli = &phba->sli;
  8287. struct lpfc_sli_ring *pring;
  8288. psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
  8289. if (phba->sli_rev == LPFC_SLI_REV4)
  8290. psli->num_rings += phba->cfg_fcp_io_channel;
  8291. psli->sli_flag = 0;
  8292. psli->fcp_ring = LPFC_FCP_RING;
  8293. psli->next_ring = LPFC_FCP_NEXT_RING;
  8294. psli->extra_ring = LPFC_EXTRA_RING;
  8295. psli->iocbq_lookup = NULL;
  8296. psli->iocbq_lookup_len = 0;
  8297. psli->last_iotag = 0;
  8298. for (i = 0; i < psli->num_rings; i++) {
  8299. pring = &psli->ring[i];
  8300. switch (i) {
  8301. case LPFC_FCP_RING: /* ring 0 - FCP */
  8302. /* numCiocb and numRiocb are used in config_port */
  8303. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  8304. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  8305. pring->sli.sli3.numCiocb +=
  8306. SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  8307. pring->sli.sli3.numRiocb +=
  8308. SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  8309. pring->sli.sli3.numCiocb +=
  8310. SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  8311. pring->sli.sli3.numRiocb +=
  8312. SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  8313. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8314. SLI3_IOCB_CMD_SIZE :
  8315. SLI2_IOCB_CMD_SIZE;
  8316. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8317. SLI3_IOCB_RSP_SIZE :
  8318. SLI2_IOCB_RSP_SIZE;
  8319. pring->iotag_ctr = 0;
  8320. pring->iotag_max =
  8321. (phba->cfg_hba_queue_depth * 2);
  8322. pring->fast_iotag = pring->iotag_max;
  8323. pring->num_mask = 0;
  8324. break;
  8325. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  8326. /* numCiocb and numRiocb are used in config_port */
  8327. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  8328. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  8329. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8330. SLI3_IOCB_CMD_SIZE :
  8331. SLI2_IOCB_CMD_SIZE;
  8332. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8333. SLI3_IOCB_RSP_SIZE :
  8334. SLI2_IOCB_RSP_SIZE;
  8335. pring->iotag_max = phba->cfg_hba_queue_depth;
  8336. pring->num_mask = 0;
  8337. break;
  8338. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  8339. /* numCiocb and numRiocb are used in config_port */
  8340. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  8341. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  8342. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8343. SLI3_IOCB_CMD_SIZE :
  8344. SLI2_IOCB_CMD_SIZE;
  8345. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8346. SLI3_IOCB_RSP_SIZE :
  8347. SLI2_IOCB_RSP_SIZE;
  8348. pring->fast_iotag = 0;
  8349. pring->iotag_ctr = 0;
  8350. pring->iotag_max = 4096;
  8351. pring->lpfc_sli_rcv_async_status =
  8352. lpfc_sli_async_event_handler;
  8353. pring->num_mask = LPFC_MAX_RING_MASK;
  8354. pring->prt[0].profile = 0; /* Mask 0 */
  8355. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  8356. pring->prt[0].type = FC_TYPE_ELS;
  8357. pring->prt[0].lpfc_sli_rcv_unsol_event =
  8358. lpfc_els_unsol_event;
  8359. pring->prt[1].profile = 0; /* Mask 1 */
  8360. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  8361. pring->prt[1].type = FC_TYPE_ELS;
  8362. pring->prt[1].lpfc_sli_rcv_unsol_event =
  8363. lpfc_els_unsol_event;
  8364. pring->prt[2].profile = 0; /* Mask 2 */
  8365. /* NameServer Inquiry */
  8366. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  8367. /* NameServer */
  8368. pring->prt[2].type = FC_TYPE_CT;
  8369. pring->prt[2].lpfc_sli_rcv_unsol_event =
  8370. lpfc_ct_unsol_event;
  8371. pring->prt[3].profile = 0; /* Mask 3 */
  8372. /* NameServer response */
  8373. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  8374. /* NameServer */
  8375. pring->prt[3].type = FC_TYPE_CT;
  8376. pring->prt[3].lpfc_sli_rcv_unsol_event =
  8377. lpfc_ct_unsol_event;
  8378. break;
  8379. }
  8380. totiocbsize += (pring->sli.sli3.numCiocb *
  8381. pring->sli.sli3.sizeCiocb) +
  8382. (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
  8383. }
  8384. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  8385. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  8386. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  8387. "SLI2 SLIM Data: x%x x%lx\n",
  8388. phba->brd_no, totiocbsize,
  8389. (unsigned long) MAX_SLIM_IOCB_SIZE);
  8390. }
  8391. if (phba->cfg_multi_ring_support == 2)
  8392. lpfc_extra_ring_setup(phba);
  8393. return 0;
  8394. }
  8395. /**
  8396. * lpfc_sli_queue_setup - Queue initialization function
  8397. * @phba: Pointer to HBA context object.
  8398. *
  8399. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  8400. * ring. This function also initializes ring indices of each ring.
  8401. * This function is called during the initialization of the SLI
  8402. * interface of an HBA.
  8403. * This function is called with no lock held and always returns
  8404. * 1.
  8405. **/
  8406. int
  8407. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  8408. {
  8409. struct lpfc_sli *psli;
  8410. struct lpfc_sli_ring *pring;
  8411. int i;
  8412. psli = &phba->sli;
  8413. spin_lock_irq(&phba->hbalock);
  8414. INIT_LIST_HEAD(&psli->mboxq);
  8415. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  8416. /* Initialize list headers for txq and txcmplq as double linked lists */
  8417. for (i = 0; i < psli->num_rings; i++) {
  8418. pring = &psli->ring[i];
  8419. pring->ringno = i;
  8420. pring->sli.sli3.next_cmdidx = 0;
  8421. pring->sli.sli3.local_getidx = 0;
  8422. pring->sli.sli3.cmdidx = 0;
  8423. pring->flag = 0;
  8424. INIT_LIST_HEAD(&pring->txq);
  8425. INIT_LIST_HEAD(&pring->txcmplq);
  8426. INIT_LIST_HEAD(&pring->iocb_continueq);
  8427. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  8428. INIT_LIST_HEAD(&pring->postbufq);
  8429. spin_lock_init(&pring->ring_lock);
  8430. }
  8431. spin_unlock_irq(&phba->hbalock);
  8432. return 1;
  8433. }
  8434. /**
  8435. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  8436. * @phba: Pointer to HBA context object.
  8437. *
  8438. * This routine flushes the mailbox command subsystem. It will unconditionally
  8439. * flush all the mailbox commands in the three possible stages in the mailbox
  8440. * command sub-system: pending mailbox command queue; the outstanding mailbox
  8441. * command; and completed mailbox command queue. It is caller's responsibility
  8442. * to make sure that the driver is in the proper state to flush the mailbox
  8443. * command sub-system. Namely, the posting of mailbox commands into the
  8444. * pending mailbox command queue from the various clients must be stopped;
  8445. * either the HBA is in a state that it will never works on the outstanding
  8446. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  8447. * mailbox command has been completed.
  8448. **/
  8449. static void
  8450. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  8451. {
  8452. LIST_HEAD(completions);
  8453. struct lpfc_sli *psli = &phba->sli;
  8454. LPFC_MBOXQ_t *pmb;
  8455. unsigned long iflag;
  8456. /* Flush all the mailbox commands in the mbox system */
  8457. spin_lock_irqsave(&phba->hbalock, iflag);
  8458. /* The pending mailbox command queue */
  8459. list_splice_init(&phba->sli.mboxq, &completions);
  8460. /* The outstanding active mailbox command */
  8461. if (psli->mbox_active) {
  8462. list_add_tail(&psli->mbox_active->list, &completions);
  8463. psli->mbox_active = NULL;
  8464. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  8465. }
  8466. /* The completed mailbox command queue */
  8467. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  8468. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8469. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  8470. while (!list_empty(&completions)) {
  8471. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  8472. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  8473. if (pmb->mbox_cmpl)
  8474. pmb->mbox_cmpl(phba, pmb);
  8475. }
  8476. }
  8477. /**
  8478. * lpfc_sli_host_down - Vport cleanup function
  8479. * @vport: Pointer to virtual port object.
  8480. *
  8481. * lpfc_sli_host_down is called to clean up the resources
  8482. * associated with a vport before destroying virtual
  8483. * port data structures.
  8484. * This function does following operations:
  8485. * - Free discovery resources associated with this virtual
  8486. * port.
  8487. * - Free iocbs associated with this virtual port in
  8488. * the txq.
  8489. * - Send abort for all iocb commands associated with this
  8490. * vport in txcmplq.
  8491. *
  8492. * This function is called with no lock held and always returns 1.
  8493. **/
  8494. int
  8495. lpfc_sli_host_down(struct lpfc_vport *vport)
  8496. {
  8497. LIST_HEAD(completions);
  8498. struct lpfc_hba *phba = vport->phba;
  8499. struct lpfc_sli *psli = &phba->sli;
  8500. struct lpfc_sli_ring *pring;
  8501. struct lpfc_iocbq *iocb, *next_iocb;
  8502. int i;
  8503. unsigned long flags = 0;
  8504. uint16_t prev_pring_flag;
  8505. lpfc_cleanup_discovery_resources(vport);
  8506. spin_lock_irqsave(&phba->hbalock, flags);
  8507. for (i = 0; i < psli->num_rings; i++) {
  8508. pring = &psli->ring[i];
  8509. prev_pring_flag = pring->flag;
  8510. /* Only slow rings */
  8511. if (pring->ringno == LPFC_ELS_RING) {
  8512. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8513. /* Set the lpfc data pending flag */
  8514. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8515. }
  8516. /*
  8517. * Error everything on the txq since these iocbs have not been
  8518. * given to the FW yet.
  8519. */
  8520. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  8521. if (iocb->vport != vport)
  8522. continue;
  8523. list_move_tail(&iocb->list, &completions);
  8524. }
  8525. /* Next issue ABTS for everything on the txcmplq */
  8526. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8527. list) {
  8528. if (iocb->vport != vport)
  8529. continue;
  8530. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8531. }
  8532. pring->flag = prev_pring_flag;
  8533. }
  8534. spin_unlock_irqrestore(&phba->hbalock, flags);
  8535. /* Cancel all the IOCBs from the completions list */
  8536. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8537. IOERR_SLI_DOWN);
  8538. return 1;
  8539. }
  8540. /**
  8541. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8542. * @phba: Pointer to HBA context object.
  8543. *
  8544. * This function cleans up all iocb, buffers, mailbox commands
  8545. * while shutting down the HBA. This function is called with no
  8546. * lock held and always returns 1.
  8547. * This function does the following to cleanup driver resources:
  8548. * - Free discovery resources for each virtual port
  8549. * - Cleanup any pending fabric iocbs
  8550. * - Iterate through the iocb txq and free each entry
  8551. * in the list.
  8552. * - Free up any buffer posted to the HBA
  8553. * - Free mailbox commands in the mailbox queue.
  8554. **/
  8555. int
  8556. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8557. {
  8558. LIST_HEAD(completions);
  8559. struct lpfc_sli *psli = &phba->sli;
  8560. struct lpfc_sli_ring *pring;
  8561. struct lpfc_dmabuf *buf_ptr;
  8562. unsigned long flags = 0;
  8563. int i;
  8564. /* Shutdown the mailbox command sub-system */
  8565. lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
  8566. lpfc_hba_down_prep(phba);
  8567. lpfc_fabric_abort_hba(phba);
  8568. spin_lock_irqsave(&phba->hbalock, flags);
  8569. for (i = 0; i < psli->num_rings; i++) {
  8570. pring = &psli->ring[i];
  8571. /* Only slow rings */
  8572. if (pring->ringno == LPFC_ELS_RING) {
  8573. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8574. /* Set the lpfc data pending flag */
  8575. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8576. }
  8577. /*
  8578. * Error everything on the txq since these iocbs have not been
  8579. * given to the FW yet.
  8580. */
  8581. list_splice_init(&pring->txq, &completions);
  8582. }
  8583. spin_unlock_irqrestore(&phba->hbalock, flags);
  8584. /* Cancel all the IOCBs from the completions list */
  8585. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8586. IOERR_SLI_DOWN);
  8587. spin_lock_irqsave(&phba->hbalock, flags);
  8588. list_splice_init(&phba->elsbuf, &completions);
  8589. phba->elsbuf_cnt = 0;
  8590. phba->elsbuf_prev_cnt = 0;
  8591. spin_unlock_irqrestore(&phba->hbalock, flags);
  8592. while (!list_empty(&completions)) {
  8593. list_remove_head(&completions, buf_ptr,
  8594. struct lpfc_dmabuf, list);
  8595. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8596. kfree(buf_ptr);
  8597. }
  8598. /* Return any active mbox cmds */
  8599. del_timer_sync(&psli->mbox_tmo);
  8600. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8601. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8602. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8603. return 1;
  8604. }
  8605. /**
  8606. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8607. * @srcp: Source memory pointer.
  8608. * @destp: Destination memory pointer.
  8609. * @cnt: Number of words required to be copied.
  8610. *
  8611. * This function is used for copying data between driver memory
  8612. * and the SLI memory. This function also changes the endianness
  8613. * of each word if native endianness is different from SLI
  8614. * endianness. This function can be called with or without
  8615. * lock.
  8616. **/
  8617. void
  8618. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8619. {
  8620. uint32_t *src = srcp;
  8621. uint32_t *dest = destp;
  8622. uint32_t ldata;
  8623. int i;
  8624. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8625. ldata = *src;
  8626. ldata = le32_to_cpu(ldata);
  8627. *dest = ldata;
  8628. src++;
  8629. dest++;
  8630. }
  8631. }
  8632. /**
  8633. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8634. * @srcp: Source memory pointer.
  8635. * @destp: Destination memory pointer.
  8636. * @cnt: Number of words required to be copied.
  8637. *
  8638. * This function is used for copying data between a data structure
  8639. * with big endian representation to local endianness.
  8640. * This function can be called with or without lock.
  8641. **/
  8642. void
  8643. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8644. {
  8645. uint32_t *src = srcp;
  8646. uint32_t *dest = destp;
  8647. uint32_t ldata;
  8648. int i;
  8649. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8650. ldata = *src;
  8651. ldata = be32_to_cpu(ldata);
  8652. *dest = ldata;
  8653. src++;
  8654. dest++;
  8655. }
  8656. }
  8657. /**
  8658. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8659. * @phba: Pointer to HBA context object.
  8660. * @pring: Pointer to driver SLI ring object.
  8661. * @mp: Pointer to driver buffer object.
  8662. *
  8663. * This function is called with no lock held.
  8664. * It always return zero after adding the buffer to the postbufq
  8665. * buffer list.
  8666. **/
  8667. int
  8668. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8669. struct lpfc_dmabuf *mp)
  8670. {
  8671. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8672. later */
  8673. spin_lock_irq(&phba->hbalock);
  8674. list_add_tail(&mp->list, &pring->postbufq);
  8675. pring->postbufq_cnt++;
  8676. spin_unlock_irq(&phba->hbalock);
  8677. return 0;
  8678. }
  8679. /**
  8680. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8681. * @phba: Pointer to HBA context object.
  8682. *
  8683. * When HBQ is enabled, buffers are searched based on tags. This function
  8684. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8685. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8686. * does not conflict with tags of buffer posted for unsolicited events.
  8687. * The function returns the allocated tag. The function is called with
  8688. * no locks held.
  8689. **/
  8690. uint32_t
  8691. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8692. {
  8693. spin_lock_irq(&phba->hbalock);
  8694. phba->buffer_tag_count++;
  8695. /*
  8696. * Always set the QUE_BUFTAG_BIT to distiguish between
  8697. * a tag assigned by HBQ.
  8698. */
  8699. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8700. spin_unlock_irq(&phba->hbalock);
  8701. return phba->buffer_tag_count;
  8702. }
  8703. /**
  8704. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8705. * @phba: Pointer to HBA context object.
  8706. * @pring: Pointer to driver SLI ring object.
  8707. * @tag: Buffer tag.
  8708. *
  8709. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8710. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8711. * iocb is posted to the response ring with the tag of the buffer.
  8712. * This function searches the pring->postbufq list using the tag
  8713. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8714. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8715. * buffer is returned to the caller else NULL is returned.
  8716. * This function is called with no lock held.
  8717. **/
  8718. struct lpfc_dmabuf *
  8719. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8720. uint32_t tag)
  8721. {
  8722. struct lpfc_dmabuf *mp, *next_mp;
  8723. struct list_head *slp = &pring->postbufq;
  8724. /* Search postbufq, from the beginning, looking for a match on tag */
  8725. spin_lock_irq(&phba->hbalock);
  8726. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8727. if (mp->buffer_tag == tag) {
  8728. list_del_init(&mp->list);
  8729. pring->postbufq_cnt--;
  8730. spin_unlock_irq(&phba->hbalock);
  8731. return mp;
  8732. }
  8733. }
  8734. spin_unlock_irq(&phba->hbalock);
  8735. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8736. "0402 Cannot find virtual addr for buffer tag on "
  8737. "ring %d Data x%lx x%p x%p x%x\n",
  8738. pring->ringno, (unsigned long) tag,
  8739. slp->next, slp->prev, pring->postbufq_cnt);
  8740. return NULL;
  8741. }
  8742. /**
  8743. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8744. * @phba: Pointer to HBA context object.
  8745. * @pring: Pointer to driver SLI ring object.
  8746. * @phys: DMA address of the buffer.
  8747. *
  8748. * This function searches the buffer list using the dma_address
  8749. * of unsolicited event to find the driver's lpfc_dmabuf object
  8750. * corresponding to the dma_address. The function returns the
  8751. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8752. * This function is called by the ct and els unsolicited event
  8753. * handlers to get the buffer associated with the unsolicited
  8754. * event.
  8755. *
  8756. * This function is called with no lock held.
  8757. **/
  8758. struct lpfc_dmabuf *
  8759. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8760. dma_addr_t phys)
  8761. {
  8762. struct lpfc_dmabuf *mp, *next_mp;
  8763. struct list_head *slp = &pring->postbufq;
  8764. /* Search postbufq, from the beginning, looking for a match on phys */
  8765. spin_lock_irq(&phba->hbalock);
  8766. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8767. if (mp->phys == phys) {
  8768. list_del_init(&mp->list);
  8769. pring->postbufq_cnt--;
  8770. spin_unlock_irq(&phba->hbalock);
  8771. return mp;
  8772. }
  8773. }
  8774. spin_unlock_irq(&phba->hbalock);
  8775. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8776. "0410 Cannot find virtual addr for mapped buf on "
  8777. "ring %d Data x%llx x%p x%p x%x\n",
  8778. pring->ringno, (unsigned long long)phys,
  8779. slp->next, slp->prev, pring->postbufq_cnt);
  8780. return NULL;
  8781. }
  8782. /**
  8783. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8784. * @phba: Pointer to HBA context object.
  8785. * @cmdiocb: Pointer to driver command iocb object.
  8786. * @rspiocb: Pointer to driver response iocb object.
  8787. *
  8788. * This function is the completion handler for the abort iocbs for
  8789. * ELS commands. This function is called from the ELS ring event
  8790. * handler with no lock held. This function frees memory resources
  8791. * associated with the abort iocb.
  8792. **/
  8793. static void
  8794. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8795. struct lpfc_iocbq *rspiocb)
  8796. {
  8797. IOCB_t *irsp = &rspiocb->iocb;
  8798. uint16_t abort_iotag, abort_context;
  8799. struct lpfc_iocbq *abort_iocb = NULL;
  8800. if (irsp->ulpStatus) {
  8801. /*
  8802. * Assume that the port already completed and returned, or
  8803. * will return the iocb. Just Log the message.
  8804. */
  8805. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8806. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8807. spin_lock_irq(&phba->hbalock);
  8808. if (phba->sli_rev < LPFC_SLI_REV4) {
  8809. if (abort_iotag != 0 &&
  8810. abort_iotag <= phba->sli.last_iotag)
  8811. abort_iocb =
  8812. phba->sli.iocbq_lookup[abort_iotag];
  8813. } else
  8814. /* For sli4 the abort_tag is the XRI,
  8815. * so the abort routine puts the iotag of the iocb
  8816. * being aborted in the context field of the abort
  8817. * IOCB.
  8818. */
  8819. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8820. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8821. "0327 Cannot abort els iocb %p "
  8822. "with tag %x context %x, abort status %x, "
  8823. "abort code %x\n",
  8824. abort_iocb, abort_iotag, abort_context,
  8825. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8826. spin_unlock_irq(&phba->hbalock);
  8827. }
  8828. lpfc_sli_release_iocbq(phba, cmdiocb);
  8829. return;
  8830. }
  8831. /**
  8832. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8833. * @phba: Pointer to HBA context object.
  8834. * @cmdiocb: Pointer to driver command iocb object.
  8835. * @rspiocb: Pointer to driver response iocb object.
  8836. *
  8837. * The function is called from SLI ring event handler with no
  8838. * lock held. This function is the completion handler for ELS commands
  8839. * which are aborted. The function frees memory resources used for
  8840. * the aborted ELS commands.
  8841. **/
  8842. static void
  8843. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8844. struct lpfc_iocbq *rspiocb)
  8845. {
  8846. IOCB_t *irsp = &rspiocb->iocb;
  8847. /* ELS cmd tag <ulpIoTag> completes */
  8848. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8849. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8850. "x%x x%x x%x\n",
  8851. irsp->ulpIoTag, irsp->ulpStatus,
  8852. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8853. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8854. lpfc_ct_free_iocb(phba, cmdiocb);
  8855. else
  8856. lpfc_els_free_iocb(phba, cmdiocb);
  8857. return;
  8858. }
  8859. /**
  8860. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8861. * @phba: Pointer to HBA context object.
  8862. * @pring: Pointer to driver SLI ring object.
  8863. * @cmdiocb: Pointer to driver command iocb object.
  8864. *
  8865. * This function issues an abort iocb for the provided command iocb down to
  8866. * the port. Other than the case the outstanding command iocb is an abort
  8867. * request, this function issues abort out unconditionally. This function is
  8868. * called with hbalock held. The function returns 0 when it fails due to
  8869. * memory allocation failure or when the command iocb is an abort request.
  8870. **/
  8871. static int
  8872. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8873. struct lpfc_iocbq *cmdiocb)
  8874. {
  8875. struct lpfc_vport *vport = cmdiocb->vport;
  8876. struct lpfc_iocbq *abtsiocbp;
  8877. IOCB_t *icmd = NULL;
  8878. IOCB_t *iabt = NULL;
  8879. int ring_number;
  8880. int retval;
  8881. unsigned long iflags;
  8882. /*
  8883. * There are certain command types we don't want to abort. And we
  8884. * don't want to abort commands that are already in the process of
  8885. * being aborted.
  8886. */
  8887. icmd = &cmdiocb->iocb;
  8888. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8889. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8890. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8891. return 0;
  8892. /* issue ABTS for this IOCB based on iotag */
  8893. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8894. if (abtsiocbp == NULL)
  8895. return 0;
  8896. /* This signals the response to set the correct status
  8897. * before calling the completion handler
  8898. */
  8899. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8900. iabt = &abtsiocbp->iocb;
  8901. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8902. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8903. if (phba->sli_rev == LPFC_SLI_REV4) {
  8904. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8905. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8906. }
  8907. else
  8908. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8909. iabt->ulpLe = 1;
  8910. iabt->ulpClass = icmd->ulpClass;
  8911. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8912. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8913. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8914. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8915. if (cmdiocb->iocb_flag & LPFC_IO_FOF)
  8916. abtsiocbp->iocb_flag |= LPFC_IO_FOF;
  8917. if (phba->link_state >= LPFC_LINK_UP)
  8918. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8919. else
  8920. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8921. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8922. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8923. "0339 Abort xri x%x, original iotag x%x, "
  8924. "abort cmd iotag x%x\n",
  8925. iabt->un.acxri.abortIoTag,
  8926. iabt->un.acxri.abortContextTag,
  8927. abtsiocbp->iotag);
  8928. if (phba->sli_rev == LPFC_SLI_REV4) {
  8929. ring_number =
  8930. lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
  8931. if (unlikely(ring_number == LPFC_HBA_ERROR))
  8932. return 0;
  8933. pring = &phba->sli.ring[ring_number];
  8934. /* Note: both hbalock and ring_lock need to be set here */
  8935. spin_lock_irqsave(&pring->ring_lock, iflags);
  8936. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8937. abtsiocbp, 0);
  8938. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  8939. } else {
  8940. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8941. abtsiocbp, 0);
  8942. }
  8943. if (retval)
  8944. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8945. /*
  8946. * Caller to this routine should check for IOCB_ERROR
  8947. * and handle it properly. This routine no longer removes
  8948. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8949. */
  8950. return retval;
  8951. }
  8952. /**
  8953. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8954. * @phba: Pointer to HBA context object.
  8955. * @pring: Pointer to driver SLI ring object.
  8956. * @cmdiocb: Pointer to driver command iocb object.
  8957. *
  8958. * This function issues an abort iocb for the provided command iocb. In case
  8959. * of unloading, the abort iocb will not be issued to commands on the ELS
  8960. * ring. Instead, the callback function shall be changed to those commands
  8961. * so that nothing happens when them finishes. This function is called with
  8962. * hbalock held. The function returns 0 when the command iocb is an abort
  8963. * request.
  8964. **/
  8965. int
  8966. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8967. struct lpfc_iocbq *cmdiocb)
  8968. {
  8969. struct lpfc_vport *vport = cmdiocb->vport;
  8970. int retval = IOCB_ERROR;
  8971. IOCB_t *icmd = NULL;
  8972. /*
  8973. * There are certain command types we don't want to abort. And we
  8974. * don't want to abort commands that are already in the process of
  8975. * being aborted.
  8976. */
  8977. icmd = &cmdiocb->iocb;
  8978. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8979. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8980. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8981. return 0;
  8982. /*
  8983. * If we're unloading, don't abort iocb on the ELS ring, but change
  8984. * the callback so that nothing happens when it finishes.
  8985. */
  8986. if ((vport->load_flag & FC_UNLOADING) &&
  8987. (pring->ringno == LPFC_ELS_RING)) {
  8988. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8989. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8990. else
  8991. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8992. goto abort_iotag_exit;
  8993. }
  8994. /* Now, we try to issue the abort to the cmdiocb out */
  8995. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8996. abort_iotag_exit:
  8997. /*
  8998. * Caller to this routine should check for IOCB_ERROR
  8999. * and handle it properly. This routine no longer removes
  9000. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  9001. */
  9002. return retval;
  9003. }
  9004. /**
  9005. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  9006. * @phba: pointer to lpfc HBA data structure.
  9007. *
  9008. * This routine will abort all pending and outstanding iocbs to an HBA.
  9009. **/
  9010. void
  9011. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  9012. {
  9013. struct lpfc_sli *psli = &phba->sli;
  9014. struct lpfc_sli_ring *pring;
  9015. int i;
  9016. for (i = 0; i < psli->num_rings; i++) {
  9017. pring = &psli->ring[i];
  9018. lpfc_sli_abort_iocb_ring(phba, pring);
  9019. }
  9020. }
  9021. /**
  9022. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  9023. * @iocbq: Pointer to driver iocb object.
  9024. * @vport: Pointer to driver virtual port object.
  9025. * @tgt_id: SCSI ID of the target.
  9026. * @lun_id: LUN ID of the scsi device.
  9027. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  9028. *
  9029. * This function acts as an iocb filter for functions which abort or count
  9030. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  9031. * 0 if the filtering criteria is met for the given iocb and will return
  9032. * 1 if the filtering criteria is not met.
  9033. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  9034. * given iocb is for the SCSI device specified by vport, tgt_id and
  9035. * lun_id parameter.
  9036. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  9037. * given iocb is for the SCSI target specified by vport and tgt_id
  9038. * parameters.
  9039. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  9040. * given iocb is for the SCSI host associated with the given vport.
  9041. * This function is called with no locks held.
  9042. **/
  9043. static int
  9044. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  9045. uint16_t tgt_id, uint64_t lun_id,
  9046. lpfc_ctx_cmd ctx_cmd)
  9047. {
  9048. struct lpfc_scsi_buf *lpfc_cmd;
  9049. int rc = 1;
  9050. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  9051. return rc;
  9052. if (iocbq->vport != vport)
  9053. return rc;
  9054. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  9055. if (lpfc_cmd->pCmd == NULL)
  9056. return rc;
  9057. switch (ctx_cmd) {
  9058. case LPFC_CTX_LUN:
  9059. if ((lpfc_cmd->rdata->pnode) &&
  9060. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  9061. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  9062. rc = 0;
  9063. break;
  9064. case LPFC_CTX_TGT:
  9065. if ((lpfc_cmd->rdata->pnode) &&
  9066. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  9067. rc = 0;
  9068. break;
  9069. case LPFC_CTX_HOST:
  9070. rc = 0;
  9071. break;
  9072. default:
  9073. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  9074. __func__, ctx_cmd);
  9075. break;
  9076. }
  9077. return rc;
  9078. }
  9079. /**
  9080. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  9081. * @vport: Pointer to virtual port.
  9082. * @tgt_id: SCSI ID of the target.
  9083. * @lun_id: LUN ID of the scsi device.
  9084. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  9085. *
  9086. * This function returns number of FCP commands pending for the vport.
  9087. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  9088. * commands pending on the vport associated with SCSI device specified
  9089. * by tgt_id and lun_id parameters.
  9090. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  9091. * commands pending on the vport associated with SCSI target specified
  9092. * by tgt_id parameter.
  9093. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  9094. * commands pending on the vport.
  9095. * This function returns the number of iocbs which satisfy the filter.
  9096. * This function is called without any lock held.
  9097. **/
  9098. int
  9099. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  9100. lpfc_ctx_cmd ctx_cmd)
  9101. {
  9102. struct lpfc_hba *phba = vport->phba;
  9103. struct lpfc_iocbq *iocbq;
  9104. int sum, i;
  9105. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  9106. iocbq = phba->sli.iocbq_lookup[i];
  9107. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  9108. ctx_cmd) == 0)
  9109. sum++;
  9110. }
  9111. return sum;
  9112. }
  9113. /**
  9114. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  9115. * @phba: Pointer to HBA context object
  9116. * @cmdiocb: Pointer to command iocb object.
  9117. * @rspiocb: Pointer to response iocb object.
  9118. *
  9119. * This function is called when an aborted FCP iocb completes. This
  9120. * function is called by the ring event handler with no lock held.
  9121. * This function frees the iocb.
  9122. **/
  9123. void
  9124. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  9125. struct lpfc_iocbq *rspiocb)
  9126. {
  9127. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9128. "3096 ABORT_XRI_CN completing on rpi x%x "
  9129. "original iotag x%x, abort cmd iotag x%x "
  9130. "status 0x%x, reason 0x%x\n",
  9131. cmdiocb->iocb.un.acxri.abortContextTag,
  9132. cmdiocb->iocb.un.acxri.abortIoTag,
  9133. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  9134. rspiocb->iocb.un.ulpWord[4]);
  9135. lpfc_sli_release_iocbq(phba, cmdiocb);
  9136. return;
  9137. }
  9138. /**
  9139. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  9140. * @vport: Pointer to virtual port.
  9141. * @pring: Pointer to driver SLI ring object.
  9142. * @tgt_id: SCSI ID of the target.
  9143. * @lun_id: LUN ID of the scsi device.
  9144. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  9145. *
  9146. * This function sends an abort command for every SCSI command
  9147. * associated with the given virtual port pending on the ring
  9148. * filtered by lpfc_sli_validate_fcp_iocb function.
  9149. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  9150. * FCP iocbs associated with lun specified by tgt_id and lun_id
  9151. * parameters
  9152. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  9153. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  9154. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  9155. * FCP iocbs associated with virtual port.
  9156. * This function returns number of iocbs it failed to abort.
  9157. * This function is called with no locks held.
  9158. **/
  9159. int
  9160. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  9161. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  9162. {
  9163. struct lpfc_hba *phba = vport->phba;
  9164. struct lpfc_iocbq *iocbq;
  9165. struct lpfc_iocbq *abtsiocb;
  9166. IOCB_t *cmd = NULL;
  9167. int errcnt = 0, ret_val = 0;
  9168. int i;
  9169. for (i = 1; i <= phba->sli.last_iotag; i++) {
  9170. iocbq = phba->sli.iocbq_lookup[i];
  9171. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  9172. abort_cmd) != 0)
  9173. continue;
  9174. /*
  9175. * If the iocbq is already being aborted, don't take a second
  9176. * action, but do count it.
  9177. */
  9178. if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  9179. continue;
  9180. /* issue ABTS for this IOCB based on iotag */
  9181. abtsiocb = lpfc_sli_get_iocbq(phba);
  9182. if (abtsiocb == NULL) {
  9183. errcnt++;
  9184. continue;
  9185. }
  9186. /* indicate the IO is being aborted by the driver. */
  9187. iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
  9188. cmd = &iocbq->iocb;
  9189. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  9190. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  9191. if (phba->sli_rev == LPFC_SLI_REV4)
  9192. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  9193. else
  9194. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  9195. abtsiocb->iocb.ulpLe = 1;
  9196. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  9197. abtsiocb->vport = vport;
  9198. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  9199. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  9200. if (iocbq->iocb_flag & LPFC_IO_FCP)
  9201. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  9202. if (iocbq->iocb_flag & LPFC_IO_FOF)
  9203. abtsiocb->iocb_flag |= LPFC_IO_FOF;
  9204. if (lpfc_is_link_up(phba))
  9205. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  9206. else
  9207. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  9208. /* Setup callback routine and issue the command. */
  9209. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  9210. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  9211. abtsiocb, 0);
  9212. if (ret_val == IOCB_ERROR) {
  9213. lpfc_sli_release_iocbq(phba, abtsiocb);
  9214. errcnt++;
  9215. continue;
  9216. }
  9217. }
  9218. return errcnt;
  9219. }
  9220. /**
  9221. * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
  9222. * @vport: Pointer to virtual port.
  9223. * @pring: Pointer to driver SLI ring object.
  9224. * @tgt_id: SCSI ID of the target.
  9225. * @lun_id: LUN ID of the scsi device.
  9226. * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  9227. *
  9228. * This function sends an abort command for every SCSI command
  9229. * associated with the given virtual port pending on the ring
  9230. * filtered by lpfc_sli_validate_fcp_iocb function.
  9231. * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
  9232. * FCP iocbs associated with lun specified by tgt_id and lun_id
  9233. * parameters
  9234. * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
  9235. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  9236. * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
  9237. * FCP iocbs associated with virtual port.
  9238. * This function returns number of iocbs it aborted .
  9239. * This function is called with no locks held right after a taskmgmt
  9240. * command is sent.
  9241. **/
  9242. int
  9243. lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  9244. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
  9245. {
  9246. struct lpfc_hba *phba = vport->phba;
  9247. struct lpfc_scsi_buf *lpfc_cmd;
  9248. struct lpfc_iocbq *abtsiocbq;
  9249. struct lpfc_nodelist *ndlp;
  9250. struct lpfc_iocbq *iocbq;
  9251. IOCB_t *icmd;
  9252. int sum, i, ret_val;
  9253. unsigned long iflags;
  9254. struct lpfc_sli_ring *pring_s4;
  9255. uint32_t ring_number;
  9256. spin_lock_irq(&phba->hbalock);
  9257. /* all I/Os are in process of being flushed */
  9258. if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
  9259. spin_unlock_irq(&phba->hbalock);
  9260. return 0;
  9261. }
  9262. sum = 0;
  9263. for (i = 1; i <= phba->sli.last_iotag; i++) {
  9264. iocbq = phba->sli.iocbq_lookup[i];
  9265. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  9266. cmd) != 0)
  9267. continue;
  9268. /*
  9269. * If the iocbq is already being aborted, don't take a second
  9270. * action, but do count it.
  9271. */
  9272. if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  9273. continue;
  9274. /* issue ABTS for this IOCB based on iotag */
  9275. abtsiocbq = __lpfc_sli_get_iocbq(phba);
  9276. if (abtsiocbq == NULL)
  9277. continue;
  9278. icmd = &iocbq->iocb;
  9279. abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  9280. abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
  9281. if (phba->sli_rev == LPFC_SLI_REV4)
  9282. abtsiocbq->iocb.un.acxri.abortIoTag =
  9283. iocbq->sli4_xritag;
  9284. else
  9285. abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
  9286. abtsiocbq->iocb.ulpLe = 1;
  9287. abtsiocbq->iocb.ulpClass = icmd->ulpClass;
  9288. abtsiocbq->vport = vport;
  9289. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  9290. abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
  9291. if (iocbq->iocb_flag & LPFC_IO_FCP)
  9292. abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
  9293. if (iocbq->iocb_flag & LPFC_IO_FOF)
  9294. abtsiocbq->iocb_flag |= LPFC_IO_FOF;
  9295. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  9296. ndlp = lpfc_cmd->rdata->pnode;
  9297. if (lpfc_is_link_up(phba) &&
  9298. (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
  9299. abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  9300. else
  9301. abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  9302. /* Setup callback routine and issue the command. */
  9303. abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  9304. /*
  9305. * Indicate the IO is being aborted by the driver and set
  9306. * the caller's flag into the aborted IO.
  9307. */
  9308. iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
  9309. if (phba->sli_rev == LPFC_SLI_REV4) {
  9310. ring_number = MAX_SLI3_CONFIGURED_RINGS +
  9311. iocbq->fcp_wqidx;
  9312. pring_s4 = &phba->sli.ring[ring_number];
  9313. /* Note: both hbalock and ring_lock must be set here */
  9314. spin_lock_irqsave(&pring_s4->ring_lock, iflags);
  9315. ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
  9316. abtsiocbq, 0);
  9317. spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
  9318. } else {
  9319. ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
  9320. abtsiocbq, 0);
  9321. }
  9322. if (ret_val == IOCB_ERROR)
  9323. __lpfc_sli_release_iocbq(phba, abtsiocbq);
  9324. else
  9325. sum++;
  9326. }
  9327. spin_unlock_irq(&phba->hbalock);
  9328. return sum;
  9329. }
  9330. /**
  9331. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  9332. * @phba: Pointer to HBA context object.
  9333. * @cmdiocbq: Pointer to command iocb.
  9334. * @rspiocbq: Pointer to response iocb.
  9335. *
  9336. * This function is the completion handler for iocbs issued using
  9337. * lpfc_sli_issue_iocb_wait function. This function is called by the
  9338. * ring event handler function without any lock held. This function
  9339. * can be called from both worker thread context and interrupt
  9340. * context. This function also can be called from other thread which
  9341. * cleans up the SLI layer objects.
  9342. * This function copy the contents of the response iocb to the
  9343. * response iocb memory object provided by the caller of
  9344. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  9345. * sleeps for the iocb completion.
  9346. **/
  9347. static void
  9348. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  9349. struct lpfc_iocbq *cmdiocbq,
  9350. struct lpfc_iocbq *rspiocbq)
  9351. {
  9352. wait_queue_head_t *pdone_q;
  9353. unsigned long iflags;
  9354. struct lpfc_scsi_buf *lpfc_cmd;
  9355. spin_lock_irqsave(&phba->hbalock, iflags);
  9356. if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
  9357. /*
  9358. * A time out has occurred for the iocb. If a time out
  9359. * completion handler has been supplied, call it. Otherwise,
  9360. * just free the iocbq.
  9361. */
  9362. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9363. cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
  9364. cmdiocbq->wait_iocb_cmpl = NULL;
  9365. if (cmdiocbq->iocb_cmpl)
  9366. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
  9367. else
  9368. lpfc_sli_release_iocbq(phba, cmdiocbq);
  9369. return;
  9370. }
  9371. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  9372. if (cmdiocbq->context2 && rspiocbq)
  9373. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  9374. &rspiocbq->iocb, sizeof(IOCB_t));
  9375. /* Set the exchange busy flag for task management commands */
  9376. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  9377. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  9378. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  9379. cur_iocbq);
  9380. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  9381. }
  9382. pdone_q = cmdiocbq->context_un.wait_queue;
  9383. if (pdone_q)
  9384. wake_up(pdone_q);
  9385. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9386. return;
  9387. }
  9388. /**
  9389. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  9390. * @phba: Pointer to HBA context object..
  9391. * @piocbq: Pointer to command iocb.
  9392. * @flag: Flag to test.
  9393. *
  9394. * This routine grabs the hbalock and then test the iocb_flag to
  9395. * see if the passed in flag is set.
  9396. * Returns:
  9397. * 1 if flag is set.
  9398. * 0 if flag is not set.
  9399. **/
  9400. static int
  9401. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  9402. struct lpfc_iocbq *piocbq, uint32_t flag)
  9403. {
  9404. unsigned long iflags;
  9405. int ret;
  9406. spin_lock_irqsave(&phba->hbalock, iflags);
  9407. ret = piocbq->iocb_flag & flag;
  9408. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9409. return ret;
  9410. }
  9411. /**
  9412. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  9413. * @phba: Pointer to HBA context object..
  9414. * @pring: Pointer to sli ring.
  9415. * @piocb: Pointer to command iocb.
  9416. * @prspiocbq: Pointer to response iocb.
  9417. * @timeout: Timeout in number of seconds.
  9418. *
  9419. * This function issues the iocb to firmware and waits for the
  9420. * iocb to complete. The iocb_cmpl field of the shall be used
  9421. * to handle iocbs which time out. If the field is NULL, the
  9422. * function shall free the iocbq structure. If more clean up is
  9423. * needed, the caller is expected to provide a completion function
  9424. * that will provide the needed clean up. If the iocb command is
  9425. * not completed within timeout seconds, the function will either
  9426. * free the iocbq structure (if iocb_cmpl == NULL) or execute the
  9427. * completion function set in the iocb_cmpl field and then return
  9428. * a status of IOCB_TIMEDOUT. The caller should not free the iocb
  9429. * resources if this function returns IOCB_TIMEDOUT.
  9430. * The function waits for the iocb completion using an
  9431. * non-interruptible wait.
  9432. * This function will sleep while waiting for iocb completion.
  9433. * So, this function should not be called from any context which
  9434. * does not allow sleeping. Due to the same reason, this function
  9435. * cannot be called with interrupt disabled.
  9436. * This function assumes that the iocb completions occur while
  9437. * this function sleep. So, this function cannot be called from
  9438. * the thread which process iocb completion for this ring.
  9439. * This function clears the iocb_flag of the iocb object before
  9440. * issuing the iocb and the iocb completion handler sets this
  9441. * flag and wakes this thread when the iocb completes.
  9442. * The contents of the response iocb will be copied to prspiocbq
  9443. * by the completion handler when the command completes.
  9444. * This function returns IOCB_SUCCESS when success.
  9445. * This function is called with no lock held.
  9446. **/
  9447. int
  9448. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  9449. uint32_t ring_number,
  9450. struct lpfc_iocbq *piocb,
  9451. struct lpfc_iocbq *prspiocbq,
  9452. uint32_t timeout)
  9453. {
  9454. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9455. long timeleft, timeout_req = 0;
  9456. int retval = IOCB_SUCCESS;
  9457. uint32_t creg_val;
  9458. struct lpfc_iocbq *iocb;
  9459. int txq_cnt = 0;
  9460. int txcmplq_cnt = 0;
  9461. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9462. unsigned long iflags;
  9463. bool iocb_completed = true;
  9464. /*
  9465. * If the caller has provided a response iocbq buffer, then context2
  9466. * is NULL or its an error.
  9467. */
  9468. if (prspiocbq) {
  9469. if (piocb->context2)
  9470. return IOCB_ERROR;
  9471. piocb->context2 = prspiocbq;
  9472. }
  9473. piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
  9474. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  9475. piocb->context_un.wait_queue = &done_q;
  9476. piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
  9477. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9478. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9479. return IOCB_ERROR;
  9480. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  9481. writel(creg_val, phba->HCregaddr);
  9482. readl(phba->HCregaddr); /* flush */
  9483. }
  9484. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  9485. SLI_IOCB_RET_IOCB);
  9486. if (retval == IOCB_SUCCESS) {
  9487. timeout_req = msecs_to_jiffies(timeout * 1000);
  9488. timeleft = wait_event_timeout(done_q,
  9489. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  9490. timeout_req);
  9491. spin_lock_irqsave(&phba->hbalock, iflags);
  9492. if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
  9493. /*
  9494. * IOCB timed out. Inform the wake iocb wait
  9495. * completion function and set local status
  9496. */
  9497. iocb_completed = false;
  9498. piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
  9499. }
  9500. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9501. if (iocb_completed) {
  9502. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9503. "0331 IOCB wake signaled\n");
  9504. /* Note: we are not indicating if the IOCB has a success
  9505. * status or not - that's for the caller to check.
  9506. * IOCB_SUCCESS means just that the command was sent and
  9507. * completed. Not that it completed successfully.
  9508. * */
  9509. } else if (timeleft == 0) {
  9510. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9511. "0338 IOCB wait timeout error - no "
  9512. "wake response Data x%x\n", timeout);
  9513. retval = IOCB_TIMEDOUT;
  9514. } else {
  9515. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9516. "0330 IOCB wake NOT set, "
  9517. "Data x%x x%lx\n",
  9518. timeout, (timeleft / jiffies));
  9519. retval = IOCB_TIMEDOUT;
  9520. }
  9521. } else if (retval == IOCB_BUSY) {
  9522. if (phba->cfg_log_verbose & LOG_SLI) {
  9523. list_for_each_entry(iocb, &pring->txq, list) {
  9524. txq_cnt++;
  9525. }
  9526. list_for_each_entry(iocb, &pring->txcmplq, list) {
  9527. txcmplq_cnt++;
  9528. }
  9529. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9530. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  9531. phba->iocb_cnt, txq_cnt, txcmplq_cnt);
  9532. }
  9533. return retval;
  9534. } else {
  9535. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9536. "0332 IOCB wait issue failed, Data x%x\n",
  9537. retval);
  9538. retval = IOCB_ERROR;
  9539. }
  9540. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9541. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9542. return IOCB_ERROR;
  9543. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  9544. writel(creg_val, phba->HCregaddr);
  9545. readl(phba->HCregaddr); /* flush */
  9546. }
  9547. if (prspiocbq)
  9548. piocb->context2 = NULL;
  9549. piocb->context_un.wait_queue = NULL;
  9550. piocb->iocb_cmpl = NULL;
  9551. return retval;
  9552. }
  9553. /**
  9554. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  9555. * @phba: Pointer to HBA context object.
  9556. * @pmboxq: Pointer to driver mailbox object.
  9557. * @timeout: Timeout in number of seconds.
  9558. *
  9559. * This function issues the mailbox to firmware and waits for the
  9560. * mailbox command to complete. If the mailbox command is not
  9561. * completed within timeout seconds, it returns MBX_TIMEOUT.
  9562. * The function waits for the mailbox completion using an
  9563. * interruptible wait. If the thread is woken up due to a
  9564. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  9565. * should not free the mailbox resources, if this function returns
  9566. * MBX_TIMEOUT.
  9567. * This function will sleep while waiting for mailbox completion.
  9568. * So, this function should not be called from any context which
  9569. * does not allow sleeping. Due to the same reason, this function
  9570. * cannot be called with interrupt disabled.
  9571. * This function assumes that the mailbox completion occurs while
  9572. * this function sleep. So, this function cannot be called from
  9573. * the worker thread which processes mailbox completion.
  9574. * This function is called in the context of HBA management
  9575. * applications.
  9576. * This function returns MBX_SUCCESS when successful.
  9577. * This function is called with no lock held.
  9578. **/
  9579. int
  9580. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  9581. uint32_t timeout)
  9582. {
  9583. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9584. MAILBOX_t *mb = NULL;
  9585. int retval;
  9586. unsigned long flag;
  9587. /* The caller might set context1 for extended buffer */
  9588. if (pmboxq->context1)
  9589. mb = (MAILBOX_t *)pmboxq->context1;
  9590. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  9591. /* setup wake call as IOCB callback */
  9592. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  9593. /* setup context field to pass wait_queue pointer to wake function */
  9594. pmboxq->context1 = &done_q;
  9595. /* now issue the command */
  9596. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  9597. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  9598. wait_event_interruptible_timeout(done_q,
  9599. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  9600. msecs_to_jiffies(timeout * 1000));
  9601. spin_lock_irqsave(&phba->hbalock, flag);
  9602. /* restore the possible extended buffer for free resource */
  9603. pmboxq->context1 = (uint8_t *)mb;
  9604. /*
  9605. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  9606. * else do not free the resources.
  9607. */
  9608. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  9609. retval = MBX_SUCCESS;
  9610. } else {
  9611. retval = MBX_TIMEOUT;
  9612. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9613. }
  9614. spin_unlock_irqrestore(&phba->hbalock, flag);
  9615. } else {
  9616. /* restore the possible extended buffer for free resource */
  9617. pmboxq->context1 = (uint8_t *)mb;
  9618. }
  9619. return retval;
  9620. }
  9621. /**
  9622. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  9623. * @phba: Pointer to HBA context.
  9624. *
  9625. * This function is called to shutdown the driver's mailbox sub-system.
  9626. * It first marks the mailbox sub-system is in a block state to prevent
  9627. * the asynchronous mailbox command from issued off the pending mailbox
  9628. * command queue. If the mailbox command sub-system shutdown is due to
  9629. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  9630. * the mailbox sub-system flush routine to forcefully bring down the
  9631. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  9632. * as with offline or HBA function reset), this routine will wait for the
  9633. * outstanding mailbox command to complete before invoking the mailbox
  9634. * sub-system flush routine to gracefully bring down mailbox sub-system.
  9635. **/
  9636. void
  9637. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
  9638. {
  9639. struct lpfc_sli *psli = &phba->sli;
  9640. unsigned long timeout;
  9641. if (mbx_action == LPFC_MBX_NO_WAIT) {
  9642. /* delay 100ms for port state */
  9643. msleep(100);
  9644. lpfc_sli_mbox_sys_flush(phba);
  9645. return;
  9646. }
  9647. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  9648. spin_lock_irq(&phba->hbalock);
  9649. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  9650. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  9651. /* Determine how long we might wait for the active mailbox
  9652. * command to be gracefully completed by firmware.
  9653. */
  9654. if (phba->sli.mbox_active)
  9655. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  9656. phba->sli.mbox_active) *
  9657. 1000) + jiffies;
  9658. spin_unlock_irq(&phba->hbalock);
  9659. while (phba->sli.mbox_active) {
  9660. /* Check active mailbox complete status every 2ms */
  9661. msleep(2);
  9662. if (time_after(jiffies, timeout))
  9663. /* Timeout, let the mailbox flush routine to
  9664. * forcefully release active mailbox command
  9665. */
  9666. break;
  9667. }
  9668. } else
  9669. spin_unlock_irq(&phba->hbalock);
  9670. lpfc_sli_mbox_sys_flush(phba);
  9671. }
  9672. /**
  9673. * lpfc_sli_eratt_read - read sli-3 error attention events
  9674. * @phba: Pointer to HBA context.
  9675. *
  9676. * This function is called to read the SLI3 device error attention registers
  9677. * for possible error attention events. The caller must hold the hostlock
  9678. * with spin_lock_irq().
  9679. *
  9680. * This function returns 1 when there is Error Attention in the Host Attention
  9681. * Register and returns 0 otherwise.
  9682. **/
  9683. static int
  9684. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  9685. {
  9686. uint32_t ha_copy;
  9687. /* Read chip Host Attention (HA) register */
  9688. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9689. goto unplug_err;
  9690. if (ha_copy & HA_ERATT) {
  9691. /* Read host status register to retrieve error event */
  9692. if (lpfc_sli_read_hs(phba))
  9693. goto unplug_err;
  9694. /* Check if there is a deferred error condition is active */
  9695. if ((HS_FFER1 & phba->work_hs) &&
  9696. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9697. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9698. phba->hba_flag |= DEFER_ERATT;
  9699. /* Clear all interrupt enable conditions */
  9700. writel(0, phba->HCregaddr);
  9701. readl(phba->HCregaddr);
  9702. }
  9703. /* Set the driver HA work bitmap */
  9704. phba->work_ha |= HA_ERATT;
  9705. /* Indicate polling handles this ERATT */
  9706. phba->hba_flag |= HBA_ERATT_HANDLED;
  9707. return 1;
  9708. }
  9709. return 0;
  9710. unplug_err:
  9711. /* Set the driver HS work bitmap */
  9712. phba->work_hs |= UNPLUG_ERR;
  9713. /* Set the driver HA work bitmap */
  9714. phba->work_ha |= HA_ERATT;
  9715. /* Indicate polling handles this ERATT */
  9716. phba->hba_flag |= HBA_ERATT_HANDLED;
  9717. return 1;
  9718. }
  9719. /**
  9720. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9721. * @phba: Pointer to HBA context.
  9722. *
  9723. * This function is called to read the SLI4 device error attention registers
  9724. * for possible error attention events. The caller must hold the hostlock
  9725. * with spin_lock_irq().
  9726. *
  9727. * This function returns 1 when there is Error Attention in the Host Attention
  9728. * Register and returns 0 otherwise.
  9729. **/
  9730. static int
  9731. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9732. {
  9733. uint32_t uerr_sta_hi, uerr_sta_lo;
  9734. uint32_t if_type, portsmphr;
  9735. struct lpfc_register portstat_reg;
  9736. /*
  9737. * For now, use the SLI4 device internal unrecoverable error
  9738. * registers for error attention. This can be changed later.
  9739. */
  9740. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9741. switch (if_type) {
  9742. case LPFC_SLI_INTF_IF_TYPE_0:
  9743. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9744. &uerr_sta_lo) ||
  9745. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9746. &uerr_sta_hi)) {
  9747. phba->work_hs |= UNPLUG_ERR;
  9748. phba->work_ha |= HA_ERATT;
  9749. phba->hba_flag |= HBA_ERATT_HANDLED;
  9750. return 1;
  9751. }
  9752. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9753. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9754. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9755. "1423 HBA Unrecoverable error: "
  9756. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9757. "ue_mask_lo_reg=0x%x, "
  9758. "ue_mask_hi_reg=0x%x\n",
  9759. uerr_sta_lo, uerr_sta_hi,
  9760. phba->sli4_hba.ue_mask_lo,
  9761. phba->sli4_hba.ue_mask_hi);
  9762. phba->work_status[0] = uerr_sta_lo;
  9763. phba->work_status[1] = uerr_sta_hi;
  9764. phba->work_ha |= HA_ERATT;
  9765. phba->hba_flag |= HBA_ERATT_HANDLED;
  9766. return 1;
  9767. }
  9768. break;
  9769. case LPFC_SLI_INTF_IF_TYPE_2:
  9770. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9771. &portstat_reg.word0) ||
  9772. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9773. &portsmphr)){
  9774. phba->work_hs |= UNPLUG_ERR;
  9775. phba->work_ha |= HA_ERATT;
  9776. phba->hba_flag |= HBA_ERATT_HANDLED;
  9777. return 1;
  9778. }
  9779. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9780. phba->work_status[0] =
  9781. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9782. phba->work_status[1] =
  9783. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9784. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9785. "2885 Port Status Event: "
  9786. "port status reg 0x%x, "
  9787. "port smphr reg 0x%x, "
  9788. "error 1=0x%x, error 2=0x%x\n",
  9789. portstat_reg.word0,
  9790. portsmphr,
  9791. phba->work_status[0],
  9792. phba->work_status[1]);
  9793. phba->work_ha |= HA_ERATT;
  9794. phba->hba_flag |= HBA_ERATT_HANDLED;
  9795. return 1;
  9796. }
  9797. break;
  9798. case LPFC_SLI_INTF_IF_TYPE_1:
  9799. default:
  9800. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9801. "2886 HBA Error Attention on unsupported "
  9802. "if type %d.", if_type);
  9803. return 1;
  9804. }
  9805. return 0;
  9806. }
  9807. /**
  9808. * lpfc_sli_check_eratt - check error attention events
  9809. * @phba: Pointer to HBA context.
  9810. *
  9811. * This function is called from timer soft interrupt context to check HBA's
  9812. * error attention register bit for error attention events.
  9813. *
  9814. * This function returns 1 when there is Error Attention in the Host Attention
  9815. * Register and returns 0 otherwise.
  9816. **/
  9817. int
  9818. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9819. {
  9820. uint32_t ha_copy;
  9821. /* If somebody is waiting to handle an eratt, don't process it
  9822. * here. The brdkill function will do this.
  9823. */
  9824. if (phba->link_flag & LS_IGNORE_ERATT)
  9825. return 0;
  9826. /* Check if interrupt handler handles this ERATT */
  9827. spin_lock_irq(&phba->hbalock);
  9828. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9829. /* Interrupt handler has handled ERATT */
  9830. spin_unlock_irq(&phba->hbalock);
  9831. return 0;
  9832. }
  9833. /*
  9834. * If there is deferred error attention, do not check for error
  9835. * attention
  9836. */
  9837. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9838. spin_unlock_irq(&phba->hbalock);
  9839. return 0;
  9840. }
  9841. /* If PCI channel is offline, don't process it */
  9842. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9843. spin_unlock_irq(&phba->hbalock);
  9844. return 0;
  9845. }
  9846. switch (phba->sli_rev) {
  9847. case LPFC_SLI_REV2:
  9848. case LPFC_SLI_REV3:
  9849. /* Read chip Host Attention (HA) register */
  9850. ha_copy = lpfc_sli_eratt_read(phba);
  9851. break;
  9852. case LPFC_SLI_REV4:
  9853. /* Read device Uncoverable Error (UERR) registers */
  9854. ha_copy = lpfc_sli4_eratt_read(phba);
  9855. break;
  9856. default:
  9857. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9858. "0299 Invalid SLI revision (%d)\n",
  9859. phba->sli_rev);
  9860. ha_copy = 0;
  9861. break;
  9862. }
  9863. spin_unlock_irq(&phba->hbalock);
  9864. return ha_copy;
  9865. }
  9866. /**
  9867. * lpfc_intr_state_check - Check device state for interrupt handling
  9868. * @phba: Pointer to HBA context.
  9869. *
  9870. * This inline routine checks whether a device or its PCI slot is in a state
  9871. * that the interrupt should be handled.
  9872. *
  9873. * This function returns 0 if the device or the PCI slot is in a state that
  9874. * interrupt should be handled, otherwise -EIO.
  9875. */
  9876. static inline int
  9877. lpfc_intr_state_check(struct lpfc_hba *phba)
  9878. {
  9879. /* If the pci channel is offline, ignore all the interrupts */
  9880. if (unlikely(pci_channel_offline(phba->pcidev)))
  9881. return -EIO;
  9882. /* Update device level interrupt statistics */
  9883. phba->sli.slistat.sli_intr++;
  9884. /* Ignore all interrupts during initialization. */
  9885. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9886. return -EIO;
  9887. return 0;
  9888. }
  9889. /**
  9890. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9891. * @irq: Interrupt number.
  9892. * @dev_id: The device context pointer.
  9893. *
  9894. * This function is directly called from the PCI layer as an interrupt
  9895. * service routine when device with SLI-3 interface spec is enabled with
  9896. * MSI-X multi-message interrupt mode and there are slow-path events in
  9897. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9898. * interrupt mode, this function is called as part of the device-level
  9899. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9900. * is undergoing initialization, the interrupt handler will not process
  9901. * the interrupt. The link attention and ELS ring attention events are
  9902. * handled by the worker thread. The interrupt handler signals the worker
  9903. * thread and returns for these events. This function is called without
  9904. * any lock held. It gets the hbalock to access and update SLI data
  9905. * structures.
  9906. *
  9907. * This function returns IRQ_HANDLED when interrupt is handled else it
  9908. * returns IRQ_NONE.
  9909. **/
  9910. irqreturn_t
  9911. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9912. {
  9913. struct lpfc_hba *phba;
  9914. uint32_t ha_copy, hc_copy;
  9915. uint32_t work_ha_copy;
  9916. unsigned long status;
  9917. unsigned long iflag;
  9918. uint32_t control;
  9919. MAILBOX_t *mbox, *pmbox;
  9920. struct lpfc_vport *vport;
  9921. struct lpfc_nodelist *ndlp;
  9922. struct lpfc_dmabuf *mp;
  9923. LPFC_MBOXQ_t *pmb;
  9924. int rc;
  9925. /*
  9926. * Get the driver's phba structure from the dev_id and
  9927. * assume the HBA is not interrupting.
  9928. */
  9929. phba = (struct lpfc_hba *)dev_id;
  9930. if (unlikely(!phba))
  9931. return IRQ_NONE;
  9932. /*
  9933. * Stuff needs to be attented to when this function is invoked as an
  9934. * individual interrupt handler in MSI-X multi-message interrupt mode
  9935. */
  9936. if (phba->intr_type == MSIX) {
  9937. /* Check device state for handling interrupt */
  9938. if (lpfc_intr_state_check(phba))
  9939. return IRQ_NONE;
  9940. /* Need to read HA REG for slow-path events */
  9941. spin_lock_irqsave(&phba->hbalock, iflag);
  9942. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9943. goto unplug_error;
  9944. /* If somebody is waiting to handle an eratt don't process it
  9945. * here. The brdkill function will do this.
  9946. */
  9947. if (phba->link_flag & LS_IGNORE_ERATT)
  9948. ha_copy &= ~HA_ERATT;
  9949. /* Check the need for handling ERATT in interrupt handler */
  9950. if (ha_copy & HA_ERATT) {
  9951. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9952. /* ERATT polling has handled ERATT */
  9953. ha_copy &= ~HA_ERATT;
  9954. else
  9955. /* Indicate interrupt handler handles ERATT */
  9956. phba->hba_flag |= HBA_ERATT_HANDLED;
  9957. }
  9958. /*
  9959. * If there is deferred error attention, do not check for any
  9960. * interrupt.
  9961. */
  9962. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9963. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9964. return IRQ_NONE;
  9965. }
  9966. /* Clear up only attention source related to slow-path */
  9967. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9968. goto unplug_error;
  9969. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9970. HC_LAINT_ENA | HC_ERINT_ENA),
  9971. phba->HCregaddr);
  9972. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9973. phba->HAregaddr);
  9974. writel(hc_copy, phba->HCregaddr);
  9975. readl(phba->HAregaddr); /* flush */
  9976. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9977. } else
  9978. ha_copy = phba->ha_copy;
  9979. work_ha_copy = ha_copy & phba->work_ha_mask;
  9980. if (work_ha_copy) {
  9981. if (work_ha_copy & HA_LATT) {
  9982. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9983. /*
  9984. * Turn off Link Attention interrupts
  9985. * until CLEAR_LA done
  9986. */
  9987. spin_lock_irqsave(&phba->hbalock, iflag);
  9988. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9989. if (lpfc_readl(phba->HCregaddr, &control))
  9990. goto unplug_error;
  9991. control &= ~HC_LAINT_ENA;
  9992. writel(control, phba->HCregaddr);
  9993. readl(phba->HCregaddr); /* flush */
  9994. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9995. }
  9996. else
  9997. work_ha_copy &= ~HA_LATT;
  9998. }
  9999. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  10000. /*
  10001. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  10002. * the only slow ring.
  10003. */
  10004. status = (work_ha_copy &
  10005. (HA_RXMASK << (4*LPFC_ELS_RING)));
  10006. status >>= (4*LPFC_ELS_RING);
  10007. if (status & HA_RXMASK) {
  10008. spin_lock_irqsave(&phba->hbalock, iflag);
  10009. if (lpfc_readl(phba->HCregaddr, &control))
  10010. goto unplug_error;
  10011. lpfc_debugfs_slow_ring_trc(phba,
  10012. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  10013. control, status,
  10014. (uint32_t)phba->sli.slistat.sli_intr);
  10015. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  10016. lpfc_debugfs_slow_ring_trc(phba,
  10017. "ISR Disable ring:"
  10018. "pwork:x%x hawork:x%x wait:x%x",
  10019. phba->work_ha, work_ha_copy,
  10020. (uint32_t)((unsigned long)
  10021. &phba->work_waitq));
  10022. control &=
  10023. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  10024. writel(control, phba->HCregaddr);
  10025. readl(phba->HCregaddr); /* flush */
  10026. }
  10027. else {
  10028. lpfc_debugfs_slow_ring_trc(phba,
  10029. "ISR slow ring: pwork:"
  10030. "x%x hawork:x%x wait:x%x",
  10031. phba->work_ha, work_ha_copy,
  10032. (uint32_t)((unsigned long)
  10033. &phba->work_waitq));
  10034. }
  10035. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10036. }
  10037. }
  10038. spin_lock_irqsave(&phba->hbalock, iflag);
  10039. if (work_ha_copy & HA_ERATT) {
  10040. if (lpfc_sli_read_hs(phba))
  10041. goto unplug_error;
  10042. /*
  10043. * Check if there is a deferred error condition
  10044. * is active
  10045. */
  10046. if ((HS_FFER1 & phba->work_hs) &&
  10047. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  10048. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  10049. phba->work_hs)) {
  10050. phba->hba_flag |= DEFER_ERATT;
  10051. /* Clear all interrupt enable conditions */
  10052. writel(0, phba->HCregaddr);
  10053. readl(phba->HCregaddr);
  10054. }
  10055. }
  10056. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  10057. pmb = phba->sli.mbox_active;
  10058. pmbox = &pmb->u.mb;
  10059. mbox = phba->mbox;
  10060. vport = pmb->vport;
  10061. /* First check out the status word */
  10062. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  10063. if (pmbox->mbxOwner != OWN_HOST) {
  10064. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10065. /*
  10066. * Stray Mailbox Interrupt, mbxCommand <cmd>
  10067. * mbxStatus <status>
  10068. */
  10069. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10070. LOG_SLI,
  10071. "(%d):0304 Stray Mailbox "
  10072. "Interrupt mbxCommand x%x "
  10073. "mbxStatus x%x\n",
  10074. (vport ? vport->vpi : 0),
  10075. pmbox->mbxCommand,
  10076. pmbox->mbxStatus);
  10077. /* clear mailbox attention bit */
  10078. work_ha_copy &= ~HA_MBATT;
  10079. } else {
  10080. phba->sli.mbox_active = NULL;
  10081. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10082. phba->last_completion_time = jiffies;
  10083. del_timer(&phba->sli.mbox_tmo);
  10084. if (pmb->mbox_cmpl) {
  10085. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  10086. MAILBOX_CMD_SIZE);
  10087. if (pmb->out_ext_byte_len &&
  10088. pmb->context2)
  10089. lpfc_sli_pcimem_bcopy(
  10090. phba->mbox_ext,
  10091. pmb->context2,
  10092. pmb->out_ext_byte_len);
  10093. }
  10094. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10095. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10096. lpfc_debugfs_disc_trc(vport,
  10097. LPFC_DISC_TRC_MBOX_VPORT,
  10098. "MBOX dflt rpi: : "
  10099. "status:x%x rpi:x%x",
  10100. (uint32_t)pmbox->mbxStatus,
  10101. pmbox->un.varWords[0], 0);
  10102. if (!pmbox->mbxStatus) {
  10103. mp = (struct lpfc_dmabuf *)
  10104. (pmb->context1);
  10105. ndlp = (struct lpfc_nodelist *)
  10106. pmb->context2;
  10107. /* Reg_LOGIN of dflt RPI was
  10108. * successful. new lets get
  10109. * rid of the RPI using the
  10110. * same mbox buffer.
  10111. */
  10112. lpfc_unreg_login(phba,
  10113. vport->vpi,
  10114. pmbox->un.varWords[0],
  10115. pmb);
  10116. pmb->mbox_cmpl =
  10117. lpfc_mbx_cmpl_dflt_rpi;
  10118. pmb->context1 = mp;
  10119. pmb->context2 = ndlp;
  10120. pmb->vport = vport;
  10121. rc = lpfc_sli_issue_mbox(phba,
  10122. pmb,
  10123. MBX_NOWAIT);
  10124. if (rc != MBX_BUSY)
  10125. lpfc_printf_log(phba,
  10126. KERN_ERR,
  10127. LOG_MBOX | LOG_SLI,
  10128. "0350 rc should have"
  10129. "been MBX_BUSY\n");
  10130. if (rc != MBX_NOT_FINISHED)
  10131. goto send_current_mbox;
  10132. }
  10133. }
  10134. spin_lock_irqsave(
  10135. &phba->pport->work_port_lock,
  10136. iflag);
  10137. phba->pport->work_port_events &=
  10138. ~WORKER_MBOX_TMO;
  10139. spin_unlock_irqrestore(
  10140. &phba->pport->work_port_lock,
  10141. iflag);
  10142. lpfc_mbox_cmpl_put(phba, pmb);
  10143. }
  10144. } else
  10145. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10146. if ((work_ha_copy & HA_MBATT) &&
  10147. (phba->sli.mbox_active == NULL)) {
  10148. send_current_mbox:
  10149. /* Process next mailbox command if there is one */
  10150. do {
  10151. rc = lpfc_sli_issue_mbox(phba, NULL,
  10152. MBX_NOWAIT);
  10153. } while (rc == MBX_NOT_FINISHED);
  10154. if (rc != MBX_SUCCESS)
  10155. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10156. LOG_SLI, "0349 rc should be "
  10157. "MBX_SUCCESS\n");
  10158. }
  10159. spin_lock_irqsave(&phba->hbalock, iflag);
  10160. phba->work_ha |= work_ha_copy;
  10161. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10162. lpfc_worker_wake_up(phba);
  10163. }
  10164. return IRQ_HANDLED;
  10165. unplug_error:
  10166. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10167. return IRQ_HANDLED;
  10168. } /* lpfc_sli_sp_intr_handler */
  10169. /**
  10170. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  10171. * @irq: Interrupt number.
  10172. * @dev_id: The device context pointer.
  10173. *
  10174. * This function is directly called from the PCI layer as an interrupt
  10175. * service routine when device with SLI-3 interface spec is enabled with
  10176. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10177. * ring event in the HBA. However, when the device is enabled with either
  10178. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10179. * device-level interrupt handler. When the PCI slot is in error recovery
  10180. * or the HBA is undergoing initialization, the interrupt handler will not
  10181. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10182. * the intrrupt context. This function is called without any lock held.
  10183. * It gets the hbalock to access and update SLI data structures.
  10184. *
  10185. * This function returns IRQ_HANDLED when interrupt is handled else it
  10186. * returns IRQ_NONE.
  10187. **/
  10188. irqreturn_t
  10189. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  10190. {
  10191. struct lpfc_hba *phba;
  10192. uint32_t ha_copy;
  10193. unsigned long status;
  10194. unsigned long iflag;
  10195. /* Get the driver's phba structure from the dev_id and
  10196. * assume the HBA is not interrupting.
  10197. */
  10198. phba = (struct lpfc_hba *) dev_id;
  10199. if (unlikely(!phba))
  10200. return IRQ_NONE;
  10201. /*
  10202. * Stuff needs to be attented to when this function is invoked as an
  10203. * individual interrupt handler in MSI-X multi-message interrupt mode
  10204. */
  10205. if (phba->intr_type == MSIX) {
  10206. /* Check device state for handling interrupt */
  10207. if (lpfc_intr_state_check(phba))
  10208. return IRQ_NONE;
  10209. /* Need to read HA REG for FCP ring and other ring events */
  10210. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  10211. return IRQ_HANDLED;
  10212. /* Clear up only attention source related to fast-path */
  10213. spin_lock_irqsave(&phba->hbalock, iflag);
  10214. /*
  10215. * If there is deferred error attention, do not check for
  10216. * any interrupt.
  10217. */
  10218. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  10219. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10220. return IRQ_NONE;
  10221. }
  10222. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  10223. phba->HAregaddr);
  10224. readl(phba->HAregaddr); /* flush */
  10225. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10226. } else
  10227. ha_copy = phba->ha_copy;
  10228. /*
  10229. * Process all events on FCP ring. Take the optimized path for FCP IO.
  10230. */
  10231. ha_copy &= ~(phba->work_ha_mask);
  10232. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  10233. status >>= (4*LPFC_FCP_RING);
  10234. if (status & HA_RXMASK)
  10235. lpfc_sli_handle_fast_ring_event(phba,
  10236. &phba->sli.ring[LPFC_FCP_RING],
  10237. status);
  10238. if (phba->cfg_multi_ring_support == 2) {
  10239. /*
  10240. * Process all events on extra ring. Take the optimized path
  10241. * for extra ring IO.
  10242. */
  10243. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  10244. status >>= (4*LPFC_EXTRA_RING);
  10245. if (status & HA_RXMASK) {
  10246. lpfc_sli_handle_fast_ring_event(phba,
  10247. &phba->sli.ring[LPFC_EXTRA_RING],
  10248. status);
  10249. }
  10250. }
  10251. return IRQ_HANDLED;
  10252. } /* lpfc_sli_fp_intr_handler */
  10253. /**
  10254. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  10255. * @irq: Interrupt number.
  10256. * @dev_id: The device context pointer.
  10257. *
  10258. * This function is the HBA device-level interrupt handler to device with
  10259. * SLI-3 interface spec, called from the PCI layer when either MSI or
  10260. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  10261. * requires driver attention. This function invokes the slow-path interrupt
  10262. * attention handling function and fast-path interrupt attention handling
  10263. * function in turn to process the relevant HBA attention events. This
  10264. * function is called without any lock held. It gets the hbalock to access
  10265. * and update SLI data structures.
  10266. *
  10267. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10268. * returns IRQ_NONE.
  10269. **/
  10270. irqreturn_t
  10271. lpfc_sli_intr_handler(int irq, void *dev_id)
  10272. {
  10273. struct lpfc_hba *phba;
  10274. irqreturn_t sp_irq_rc, fp_irq_rc;
  10275. unsigned long status1, status2;
  10276. uint32_t hc_copy;
  10277. /*
  10278. * Get the driver's phba structure from the dev_id and
  10279. * assume the HBA is not interrupting.
  10280. */
  10281. phba = (struct lpfc_hba *) dev_id;
  10282. if (unlikely(!phba))
  10283. return IRQ_NONE;
  10284. /* Check device state for handling interrupt */
  10285. if (lpfc_intr_state_check(phba))
  10286. return IRQ_NONE;
  10287. spin_lock(&phba->hbalock);
  10288. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  10289. spin_unlock(&phba->hbalock);
  10290. return IRQ_HANDLED;
  10291. }
  10292. if (unlikely(!phba->ha_copy)) {
  10293. spin_unlock(&phba->hbalock);
  10294. return IRQ_NONE;
  10295. } else if (phba->ha_copy & HA_ERATT) {
  10296. if (phba->hba_flag & HBA_ERATT_HANDLED)
  10297. /* ERATT polling has handled ERATT */
  10298. phba->ha_copy &= ~HA_ERATT;
  10299. else
  10300. /* Indicate interrupt handler handles ERATT */
  10301. phba->hba_flag |= HBA_ERATT_HANDLED;
  10302. }
  10303. /*
  10304. * If there is deferred error attention, do not check for any interrupt.
  10305. */
  10306. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  10307. spin_unlock(&phba->hbalock);
  10308. return IRQ_NONE;
  10309. }
  10310. /* Clear attention sources except link and error attentions */
  10311. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  10312. spin_unlock(&phba->hbalock);
  10313. return IRQ_HANDLED;
  10314. }
  10315. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  10316. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  10317. phba->HCregaddr);
  10318. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  10319. writel(hc_copy, phba->HCregaddr);
  10320. readl(phba->HAregaddr); /* flush */
  10321. spin_unlock(&phba->hbalock);
  10322. /*
  10323. * Invokes slow-path host attention interrupt handling as appropriate.
  10324. */
  10325. /* status of events with mailbox and link attention */
  10326. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  10327. /* status of events with ELS ring */
  10328. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  10329. status2 >>= (4*LPFC_ELS_RING);
  10330. if (status1 || (status2 & HA_RXMASK))
  10331. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  10332. else
  10333. sp_irq_rc = IRQ_NONE;
  10334. /*
  10335. * Invoke fast-path host attention interrupt handling as appropriate.
  10336. */
  10337. /* status of events with FCP ring */
  10338. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  10339. status1 >>= (4*LPFC_FCP_RING);
  10340. /* status of events with extra ring */
  10341. if (phba->cfg_multi_ring_support == 2) {
  10342. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  10343. status2 >>= (4*LPFC_EXTRA_RING);
  10344. } else
  10345. status2 = 0;
  10346. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  10347. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  10348. else
  10349. fp_irq_rc = IRQ_NONE;
  10350. /* Return device-level interrupt handling status */
  10351. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  10352. } /* lpfc_sli_intr_handler */
  10353. /**
  10354. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  10355. * @phba: pointer to lpfc hba data structure.
  10356. *
  10357. * This routine is invoked by the worker thread to process all the pending
  10358. * SLI4 FCP abort XRI events.
  10359. **/
  10360. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  10361. {
  10362. struct lpfc_cq_event *cq_event;
  10363. /* First, declare the fcp xri abort event has been handled */
  10364. spin_lock_irq(&phba->hbalock);
  10365. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  10366. spin_unlock_irq(&phba->hbalock);
  10367. /* Now, handle all the fcp xri abort events */
  10368. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  10369. /* Get the first event from the head of the event queue */
  10370. spin_lock_irq(&phba->hbalock);
  10371. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  10372. cq_event, struct lpfc_cq_event, list);
  10373. spin_unlock_irq(&phba->hbalock);
  10374. /* Notify aborted XRI for FCP work queue */
  10375. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  10376. /* Free the event processed back to the free pool */
  10377. lpfc_sli4_cq_event_release(phba, cq_event);
  10378. }
  10379. }
  10380. /**
  10381. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  10382. * @phba: pointer to lpfc hba data structure.
  10383. *
  10384. * This routine is invoked by the worker thread to process all the pending
  10385. * SLI4 els abort xri events.
  10386. **/
  10387. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  10388. {
  10389. struct lpfc_cq_event *cq_event;
  10390. /* First, declare the els xri abort event has been handled */
  10391. spin_lock_irq(&phba->hbalock);
  10392. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  10393. spin_unlock_irq(&phba->hbalock);
  10394. /* Now, handle all the els xri abort events */
  10395. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  10396. /* Get the first event from the head of the event queue */
  10397. spin_lock_irq(&phba->hbalock);
  10398. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  10399. cq_event, struct lpfc_cq_event, list);
  10400. spin_unlock_irq(&phba->hbalock);
  10401. /* Notify aborted XRI for ELS work queue */
  10402. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  10403. /* Free the event processed back to the free pool */
  10404. lpfc_sli4_cq_event_release(phba, cq_event);
  10405. }
  10406. }
  10407. /**
  10408. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  10409. * @phba: pointer to lpfc hba data structure
  10410. * @pIocbIn: pointer to the rspiocbq
  10411. * @pIocbOut: pointer to the cmdiocbq
  10412. * @wcqe: pointer to the complete wcqe
  10413. *
  10414. * This routine transfers the fields of a command iocbq to a response iocbq
  10415. * by copying all the IOCB fields from command iocbq and transferring the
  10416. * completion status information from the complete wcqe.
  10417. **/
  10418. static void
  10419. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  10420. struct lpfc_iocbq *pIocbIn,
  10421. struct lpfc_iocbq *pIocbOut,
  10422. struct lpfc_wcqe_complete *wcqe)
  10423. {
  10424. int numBdes, i;
  10425. unsigned long iflags;
  10426. uint32_t status, max_response;
  10427. struct lpfc_dmabuf *dmabuf;
  10428. struct ulp_bde64 *bpl, bde;
  10429. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  10430. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  10431. sizeof(struct lpfc_iocbq) - offset);
  10432. /* Map WCQE parameters into irspiocb parameters */
  10433. status = bf_get(lpfc_wcqe_c_status, wcqe);
  10434. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  10435. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  10436. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  10437. pIocbIn->iocb.un.fcpi.fcpi_parm =
  10438. pIocbOut->iocb.un.fcpi.fcpi_parm -
  10439. wcqe->total_data_placed;
  10440. else
  10441. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  10442. else {
  10443. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  10444. switch (pIocbOut->iocb.ulpCommand) {
  10445. case CMD_ELS_REQUEST64_CR:
  10446. dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
  10447. bpl = (struct ulp_bde64 *)dmabuf->virt;
  10448. bde.tus.w = le32_to_cpu(bpl[1].tus.w);
  10449. max_response = bde.tus.f.bdeSize;
  10450. break;
  10451. case CMD_GEN_REQUEST64_CR:
  10452. max_response = 0;
  10453. if (!pIocbOut->context3)
  10454. break;
  10455. numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
  10456. sizeof(struct ulp_bde64);
  10457. dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
  10458. bpl = (struct ulp_bde64 *)dmabuf->virt;
  10459. for (i = 0; i < numBdes; i++) {
  10460. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  10461. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  10462. max_response += bde.tus.f.bdeSize;
  10463. }
  10464. break;
  10465. default:
  10466. max_response = wcqe->total_data_placed;
  10467. break;
  10468. }
  10469. if (max_response < wcqe->total_data_placed)
  10470. pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
  10471. else
  10472. pIocbIn->iocb.un.genreq64.bdl.bdeSize =
  10473. wcqe->total_data_placed;
  10474. }
  10475. /* Convert BG errors for completion status */
  10476. if (status == CQE_STATUS_DI_ERROR) {
  10477. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  10478. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  10479. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  10480. else
  10481. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  10482. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  10483. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  10484. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10485. BGS_GUARD_ERR_MASK;
  10486. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  10487. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10488. BGS_APPTAG_ERR_MASK;
  10489. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  10490. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10491. BGS_REFTAG_ERR_MASK;
  10492. /* Check to see if there was any good data before the error */
  10493. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  10494. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10495. BGS_HI_WATER_MARK_PRESENT_MASK;
  10496. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  10497. wcqe->total_data_placed;
  10498. }
  10499. /*
  10500. * Set ALL the error bits to indicate we don't know what
  10501. * type of error it is.
  10502. */
  10503. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  10504. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10505. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  10506. BGS_GUARD_ERR_MASK);
  10507. }
  10508. /* Pick up HBA exchange busy condition */
  10509. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  10510. spin_lock_irqsave(&phba->hbalock, iflags);
  10511. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  10512. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10513. }
  10514. }
  10515. /**
  10516. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  10517. * @phba: Pointer to HBA context object.
  10518. * @wcqe: Pointer to work-queue completion queue entry.
  10519. *
  10520. * This routine handles an ELS work-queue completion event and construct
  10521. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  10522. * discovery engine to handle.
  10523. *
  10524. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  10525. **/
  10526. static struct lpfc_iocbq *
  10527. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  10528. struct lpfc_iocbq *irspiocbq)
  10529. {
  10530. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  10531. struct lpfc_iocbq *cmdiocbq;
  10532. struct lpfc_wcqe_complete *wcqe;
  10533. unsigned long iflags;
  10534. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  10535. spin_lock_irqsave(&pring->ring_lock, iflags);
  10536. pring->stats.iocb_event++;
  10537. /* Look up the ELS command IOCB and create pseudo response IOCB */
  10538. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10539. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10540. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10541. if (unlikely(!cmdiocbq)) {
  10542. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10543. "0386 ELS complete with no corresponding "
  10544. "cmdiocb: iotag (%d)\n",
  10545. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10546. lpfc_sli_release_iocbq(phba, irspiocbq);
  10547. return NULL;
  10548. }
  10549. /* Fake the irspiocbq and copy necessary response information */
  10550. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  10551. return irspiocbq;
  10552. }
  10553. /**
  10554. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  10555. * @phba: Pointer to HBA context object.
  10556. * @cqe: Pointer to mailbox completion queue entry.
  10557. *
  10558. * This routine process a mailbox completion queue entry with asynchrous
  10559. * event.
  10560. *
  10561. * Return: true if work posted to worker thread, otherwise false.
  10562. **/
  10563. static bool
  10564. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10565. {
  10566. struct lpfc_cq_event *cq_event;
  10567. unsigned long iflags;
  10568. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10569. "0392 Async Event: word0:x%x, word1:x%x, "
  10570. "word2:x%x, word3:x%x\n", mcqe->word0,
  10571. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  10572. /* Allocate a new internal CQ_EVENT entry */
  10573. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10574. if (!cq_event) {
  10575. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10576. "0394 Failed to allocate CQ_EVENT entry\n");
  10577. return false;
  10578. }
  10579. /* Move the CQE into an asynchronous event entry */
  10580. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  10581. spin_lock_irqsave(&phba->hbalock, iflags);
  10582. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  10583. /* Set the async event flag */
  10584. phba->hba_flag |= ASYNC_EVENT;
  10585. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10586. return true;
  10587. }
  10588. /**
  10589. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  10590. * @phba: Pointer to HBA context object.
  10591. * @cqe: Pointer to mailbox completion queue entry.
  10592. *
  10593. * This routine process a mailbox completion queue entry with mailbox
  10594. * completion event.
  10595. *
  10596. * Return: true if work posted to worker thread, otherwise false.
  10597. **/
  10598. static bool
  10599. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10600. {
  10601. uint32_t mcqe_status;
  10602. MAILBOX_t *mbox, *pmbox;
  10603. struct lpfc_mqe *mqe;
  10604. struct lpfc_vport *vport;
  10605. struct lpfc_nodelist *ndlp;
  10606. struct lpfc_dmabuf *mp;
  10607. unsigned long iflags;
  10608. LPFC_MBOXQ_t *pmb;
  10609. bool workposted = false;
  10610. int rc;
  10611. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  10612. if (!bf_get(lpfc_trailer_completed, mcqe))
  10613. goto out_no_mqe_complete;
  10614. /* Get the reference to the active mbox command */
  10615. spin_lock_irqsave(&phba->hbalock, iflags);
  10616. pmb = phba->sli.mbox_active;
  10617. if (unlikely(!pmb)) {
  10618. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10619. "1832 No pending MBOX command to handle\n");
  10620. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10621. goto out_no_mqe_complete;
  10622. }
  10623. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10624. mqe = &pmb->u.mqe;
  10625. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  10626. mbox = phba->mbox;
  10627. vport = pmb->vport;
  10628. /* Reset heartbeat timer */
  10629. phba->last_completion_time = jiffies;
  10630. del_timer(&phba->sli.mbox_tmo);
  10631. /* Move mbox data to caller's mailbox region, do endian swapping */
  10632. if (pmb->mbox_cmpl && mbox)
  10633. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  10634. /*
  10635. * For mcqe errors, conditionally move a modified error code to
  10636. * the mbox so that the error will not be missed.
  10637. */
  10638. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  10639. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  10640. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  10641. bf_set(lpfc_mqe_status, mqe,
  10642. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  10643. }
  10644. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10645. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10646. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  10647. "MBOX dflt rpi: status:x%x rpi:x%x",
  10648. mcqe_status,
  10649. pmbox->un.varWords[0], 0);
  10650. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  10651. mp = (struct lpfc_dmabuf *)(pmb->context1);
  10652. ndlp = (struct lpfc_nodelist *)pmb->context2;
  10653. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  10654. * RID of the PPI using the same mbox buffer.
  10655. */
  10656. lpfc_unreg_login(phba, vport->vpi,
  10657. pmbox->un.varWords[0], pmb);
  10658. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  10659. pmb->context1 = mp;
  10660. pmb->context2 = ndlp;
  10661. pmb->vport = vport;
  10662. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  10663. if (rc != MBX_BUSY)
  10664. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10665. LOG_SLI, "0385 rc should "
  10666. "have been MBX_BUSY\n");
  10667. if (rc != MBX_NOT_FINISHED)
  10668. goto send_current_mbox;
  10669. }
  10670. }
  10671. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  10672. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  10673. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  10674. /* There is mailbox completion work to do */
  10675. spin_lock_irqsave(&phba->hbalock, iflags);
  10676. __lpfc_mbox_cmpl_put(phba, pmb);
  10677. phba->work_ha |= HA_MBATT;
  10678. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10679. workposted = true;
  10680. send_current_mbox:
  10681. spin_lock_irqsave(&phba->hbalock, iflags);
  10682. /* Release the mailbox command posting token */
  10683. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10684. /* Setting active mailbox pointer need to be in sync to flag clear */
  10685. phba->sli.mbox_active = NULL;
  10686. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10687. /* Wake up worker thread to post the next pending mailbox command */
  10688. lpfc_worker_wake_up(phba);
  10689. out_no_mqe_complete:
  10690. if (bf_get(lpfc_trailer_consumed, mcqe))
  10691. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  10692. return workposted;
  10693. }
  10694. /**
  10695. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  10696. * @phba: Pointer to HBA context object.
  10697. * @cqe: Pointer to mailbox completion queue entry.
  10698. *
  10699. * This routine process a mailbox completion queue entry, it invokes the
  10700. * proper mailbox complete handling or asynchrous event handling routine
  10701. * according to the MCQE's async bit.
  10702. *
  10703. * Return: true if work posted to worker thread, otherwise false.
  10704. **/
  10705. static bool
  10706. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  10707. {
  10708. struct lpfc_mcqe mcqe;
  10709. bool workposted;
  10710. /* Copy the mailbox MCQE and convert endian order as needed */
  10711. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  10712. /* Invoke the proper event handling routine */
  10713. if (!bf_get(lpfc_trailer_async, &mcqe))
  10714. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  10715. else
  10716. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  10717. return workposted;
  10718. }
  10719. /**
  10720. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  10721. * @phba: Pointer to HBA context object.
  10722. * @cq: Pointer to associated CQ
  10723. * @wcqe: Pointer to work-queue completion queue entry.
  10724. *
  10725. * This routine handles an ELS work-queue completion event.
  10726. *
  10727. * Return: true if work posted to worker thread, otherwise false.
  10728. **/
  10729. static bool
  10730. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10731. struct lpfc_wcqe_complete *wcqe)
  10732. {
  10733. struct lpfc_iocbq *irspiocbq;
  10734. unsigned long iflags;
  10735. struct lpfc_sli_ring *pring = cq->pring;
  10736. int txq_cnt = 0;
  10737. int txcmplq_cnt = 0;
  10738. int fcp_txcmplq_cnt = 0;
  10739. /* Get an irspiocbq for later ELS response processing use */
  10740. irspiocbq = lpfc_sli_get_iocbq(phba);
  10741. if (!irspiocbq) {
  10742. if (!list_empty(&pring->txq))
  10743. txq_cnt++;
  10744. if (!list_empty(&pring->txcmplq))
  10745. txcmplq_cnt++;
  10746. if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
  10747. fcp_txcmplq_cnt++;
  10748. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10749. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10750. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10751. txq_cnt, phba->iocb_cnt,
  10752. fcp_txcmplq_cnt,
  10753. txcmplq_cnt);
  10754. return false;
  10755. }
  10756. /* Save off the slow-path queue event for work thread to process */
  10757. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10758. spin_lock_irqsave(&phba->hbalock, iflags);
  10759. list_add_tail(&irspiocbq->cq_event.list,
  10760. &phba->sli4_hba.sp_queue_event);
  10761. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10762. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10763. return true;
  10764. }
  10765. /**
  10766. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10767. * @phba: Pointer to HBA context object.
  10768. * @wcqe: Pointer to work-queue completion queue entry.
  10769. *
  10770. * This routine handles slow-path WQ entry comsumed event by invoking the
  10771. * proper WQ release routine to the slow-path WQ.
  10772. **/
  10773. static void
  10774. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10775. struct lpfc_wcqe_release *wcqe)
  10776. {
  10777. /* sanity check on queue memory */
  10778. if (unlikely(!phba->sli4_hba.els_wq))
  10779. return;
  10780. /* Check for the slow-path ELS work queue */
  10781. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10782. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10783. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10784. else
  10785. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10786. "2579 Slow-path wqe consume event carries "
  10787. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10788. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10789. phba->sli4_hba.els_wq->queue_id);
  10790. }
  10791. /**
  10792. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10793. * @phba: Pointer to HBA context object.
  10794. * @cq: Pointer to a WQ completion queue.
  10795. * @wcqe: Pointer to work-queue completion queue entry.
  10796. *
  10797. * This routine handles an XRI abort event.
  10798. *
  10799. * Return: true if work posted to worker thread, otherwise false.
  10800. **/
  10801. static bool
  10802. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10803. struct lpfc_queue *cq,
  10804. struct sli4_wcqe_xri_aborted *wcqe)
  10805. {
  10806. bool workposted = false;
  10807. struct lpfc_cq_event *cq_event;
  10808. unsigned long iflags;
  10809. /* Allocate a new internal CQ_EVENT entry */
  10810. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10811. if (!cq_event) {
  10812. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10813. "0602 Failed to allocate CQ_EVENT entry\n");
  10814. return false;
  10815. }
  10816. /* Move the CQE into the proper xri abort event list */
  10817. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10818. switch (cq->subtype) {
  10819. case LPFC_FCP:
  10820. spin_lock_irqsave(&phba->hbalock, iflags);
  10821. list_add_tail(&cq_event->list,
  10822. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10823. /* Set the fcp xri abort event flag */
  10824. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10825. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10826. workposted = true;
  10827. break;
  10828. case LPFC_ELS:
  10829. spin_lock_irqsave(&phba->hbalock, iflags);
  10830. list_add_tail(&cq_event->list,
  10831. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10832. /* Set the els xri abort event flag */
  10833. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10834. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10835. workposted = true;
  10836. break;
  10837. default:
  10838. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10839. "0603 Invalid work queue CQE subtype (x%x)\n",
  10840. cq->subtype);
  10841. workposted = false;
  10842. break;
  10843. }
  10844. return workposted;
  10845. }
  10846. /**
  10847. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10848. * @phba: Pointer to HBA context object.
  10849. * @rcqe: Pointer to receive-queue completion queue entry.
  10850. *
  10851. * This routine process a receive-queue completion queue entry.
  10852. *
  10853. * Return: true if work posted to worker thread, otherwise false.
  10854. **/
  10855. static bool
  10856. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10857. {
  10858. bool workposted = false;
  10859. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10860. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10861. struct hbq_dmabuf *dma_buf;
  10862. uint32_t status, rq_id;
  10863. unsigned long iflags;
  10864. /* sanity check on queue memory */
  10865. if (unlikely(!hrq) || unlikely(!drq))
  10866. return workposted;
  10867. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10868. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10869. else
  10870. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10871. if (rq_id != hrq->queue_id)
  10872. goto out;
  10873. status = bf_get(lpfc_rcqe_status, rcqe);
  10874. switch (status) {
  10875. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10876. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10877. "2537 Receive Frame Truncated!!\n");
  10878. hrq->RQ_buf_trunc++;
  10879. case FC_STATUS_RQ_SUCCESS:
  10880. lpfc_sli4_rq_release(hrq, drq);
  10881. spin_lock_irqsave(&phba->hbalock, iflags);
  10882. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10883. if (!dma_buf) {
  10884. hrq->RQ_no_buf_found++;
  10885. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10886. goto out;
  10887. }
  10888. hrq->RQ_rcv_buf++;
  10889. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10890. /* save off the frame for the word thread to process */
  10891. list_add_tail(&dma_buf->cq_event.list,
  10892. &phba->sli4_hba.sp_queue_event);
  10893. /* Frame received */
  10894. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10895. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10896. workposted = true;
  10897. break;
  10898. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10899. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10900. hrq->RQ_no_posted_buf++;
  10901. /* Post more buffers if possible */
  10902. spin_lock_irqsave(&phba->hbalock, iflags);
  10903. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10904. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10905. workposted = true;
  10906. break;
  10907. }
  10908. out:
  10909. return workposted;
  10910. }
  10911. /**
  10912. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10913. * @phba: Pointer to HBA context object.
  10914. * @cq: Pointer to the completion queue.
  10915. * @wcqe: Pointer to a completion queue entry.
  10916. *
  10917. * This routine process a slow-path work-queue or receive queue completion queue
  10918. * entry.
  10919. *
  10920. * Return: true if work posted to worker thread, otherwise false.
  10921. **/
  10922. static bool
  10923. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10924. struct lpfc_cqe *cqe)
  10925. {
  10926. struct lpfc_cqe cqevt;
  10927. bool workposted = false;
  10928. /* Copy the work queue CQE and convert endian order if needed */
  10929. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10930. /* Check and process for different type of WCQE and dispatch */
  10931. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10932. case CQE_CODE_COMPL_WQE:
  10933. /* Process the WQ/RQ complete event */
  10934. phba->last_completion_time = jiffies;
  10935. workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
  10936. (struct lpfc_wcqe_complete *)&cqevt);
  10937. break;
  10938. case CQE_CODE_RELEASE_WQE:
  10939. /* Process the WQ release event */
  10940. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10941. (struct lpfc_wcqe_release *)&cqevt);
  10942. break;
  10943. case CQE_CODE_XRI_ABORTED:
  10944. /* Process the WQ XRI abort event */
  10945. phba->last_completion_time = jiffies;
  10946. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10947. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10948. break;
  10949. case CQE_CODE_RECEIVE:
  10950. case CQE_CODE_RECEIVE_V1:
  10951. /* Process the RQ event */
  10952. phba->last_completion_time = jiffies;
  10953. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10954. (struct lpfc_rcqe *)&cqevt);
  10955. break;
  10956. default:
  10957. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10958. "0388 Not a valid WCQE code: x%x\n",
  10959. bf_get(lpfc_cqe_code, &cqevt));
  10960. break;
  10961. }
  10962. return workposted;
  10963. }
  10964. /**
  10965. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10966. * @phba: Pointer to HBA context object.
  10967. * @eqe: Pointer to fast-path event queue entry.
  10968. *
  10969. * This routine process a event queue entry from the slow-path event queue.
  10970. * It will check the MajorCode and MinorCode to determine this is for a
  10971. * completion event on a completion queue, if not, an error shall be logged
  10972. * and just return. Otherwise, it will get to the corresponding completion
  10973. * queue and process all the entries on that completion queue, rearm the
  10974. * completion queue, and then return.
  10975. *
  10976. **/
  10977. static void
  10978. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10979. struct lpfc_queue *speq)
  10980. {
  10981. struct lpfc_queue *cq = NULL, *childq;
  10982. struct lpfc_cqe *cqe;
  10983. bool workposted = false;
  10984. int ecount = 0;
  10985. uint16_t cqid;
  10986. /* Get the reference to the corresponding CQ */
  10987. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10988. list_for_each_entry(childq, &speq->child_list, list) {
  10989. if (childq->queue_id == cqid) {
  10990. cq = childq;
  10991. break;
  10992. }
  10993. }
  10994. if (unlikely(!cq)) {
  10995. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10996. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10997. "0365 Slow-path CQ identifier "
  10998. "(%d) does not exist\n", cqid);
  10999. return;
  11000. }
  11001. /* Process all the entries to the CQ */
  11002. switch (cq->type) {
  11003. case LPFC_MCQ:
  11004. while ((cqe = lpfc_sli4_cq_get(cq))) {
  11005. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  11006. if (!(++ecount % cq->entry_repost))
  11007. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  11008. cq->CQ_mbox++;
  11009. }
  11010. break;
  11011. case LPFC_WCQ:
  11012. while ((cqe = lpfc_sli4_cq_get(cq))) {
  11013. if (cq->subtype == LPFC_FCP)
  11014. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  11015. cqe);
  11016. else
  11017. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  11018. cqe);
  11019. if (!(++ecount % cq->entry_repost))
  11020. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  11021. }
  11022. /* Track the max number of CQEs processed in 1 EQ */
  11023. if (ecount > cq->CQ_max_cqe)
  11024. cq->CQ_max_cqe = ecount;
  11025. break;
  11026. default:
  11027. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11028. "0370 Invalid completion queue type (%d)\n",
  11029. cq->type);
  11030. return;
  11031. }
  11032. /* Catch the no cq entry condition, log an error */
  11033. if (unlikely(ecount == 0))
  11034. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11035. "0371 No entry from the CQ: identifier "
  11036. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  11037. /* In any case, flash and re-arm the RCQ */
  11038. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  11039. /* wake up worker thread if there are works to be done */
  11040. if (workposted)
  11041. lpfc_worker_wake_up(phba);
  11042. }
  11043. /**
  11044. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  11045. * @phba: Pointer to HBA context object.
  11046. * @cq: Pointer to associated CQ
  11047. * @wcqe: Pointer to work-queue completion queue entry.
  11048. *
  11049. * This routine process a fast-path work queue completion entry from fast-path
  11050. * event queue for FCP command response completion.
  11051. **/
  11052. static void
  11053. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11054. struct lpfc_wcqe_complete *wcqe)
  11055. {
  11056. struct lpfc_sli_ring *pring = cq->pring;
  11057. struct lpfc_iocbq *cmdiocbq;
  11058. struct lpfc_iocbq irspiocbq;
  11059. unsigned long iflags;
  11060. /* Check for response status */
  11061. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  11062. /* If resource errors reported from HBA, reduce queue
  11063. * depth of the SCSI device.
  11064. */
  11065. if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
  11066. IOSTAT_LOCAL_REJECT)) &&
  11067. ((wcqe->parameter & IOERR_PARAM_MASK) ==
  11068. IOERR_NO_RESOURCES))
  11069. phba->lpfc_rampdown_queue_depth(phba);
  11070. /* Log the error status */
  11071. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11072. "0373 FCP complete error: status=x%x, "
  11073. "hw_status=x%x, total_data_specified=%d, "
  11074. "parameter=x%x, word3=x%x\n",
  11075. bf_get(lpfc_wcqe_c_status, wcqe),
  11076. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  11077. wcqe->total_data_placed, wcqe->parameter,
  11078. wcqe->word3);
  11079. }
  11080. /* Look up the FCP command IOCB and create pseudo response IOCB */
  11081. spin_lock_irqsave(&pring->ring_lock, iflags);
  11082. pring->stats.iocb_event++;
  11083. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  11084. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  11085. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  11086. if (unlikely(!cmdiocbq)) {
  11087. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11088. "0374 FCP complete with no corresponding "
  11089. "cmdiocb: iotag (%d)\n",
  11090. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  11091. return;
  11092. }
  11093. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  11094. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11095. "0375 FCP cmdiocb not callback function "
  11096. "iotag: (%d)\n",
  11097. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  11098. return;
  11099. }
  11100. /* Fake the irspiocb and copy necessary response information */
  11101. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  11102. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  11103. spin_lock_irqsave(&phba->hbalock, iflags);
  11104. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  11105. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11106. }
  11107. /* Pass the cmd_iocb and the rsp state to the upper layer */
  11108. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  11109. }
  11110. /**
  11111. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  11112. * @phba: Pointer to HBA context object.
  11113. * @cq: Pointer to completion queue.
  11114. * @wcqe: Pointer to work-queue completion queue entry.
  11115. *
  11116. * This routine handles an fast-path WQ entry comsumed event by invoking the
  11117. * proper WQ release routine to the slow-path WQ.
  11118. **/
  11119. static void
  11120. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11121. struct lpfc_wcqe_release *wcqe)
  11122. {
  11123. struct lpfc_queue *childwq;
  11124. bool wqid_matched = false;
  11125. uint16_t fcp_wqid;
  11126. /* Check for fast-path FCP work queue release */
  11127. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  11128. list_for_each_entry(childwq, &cq->child_list, list) {
  11129. if (childwq->queue_id == fcp_wqid) {
  11130. lpfc_sli4_wq_release(childwq,
  11131. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  11132. wqid_matched = true;
  11133. break;
  11134. }
  11135. }
  11136. /* Report warning log message if no match found */
  11137. if (wqid_matched != true)
  11138. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11139. "2580 Fast-path wqe consume event carries "
  11140. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  11141. }
  11142. /**
  11143. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  11144. * @cq: Pointer to the completion queue.
  11145. * @eqe: Pointer to fast-path completion queue entry.
  11146. *
  11147. * This routine process a fast-path work queue completion entry from fast-path
  11148. * event queue for FCP command response completion.
  11149. **/
  11150. static int
  11151. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11152. struct lpfc_cqe *cqe)
  11153. {
  11154. struct lpfc_wcqe_release wcqe;
  11155. bool workposted = false;
  11156. /* Copy the work queue CQE and convert endian order if needed */
  11157. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  11158. /* Check and process for different type of WCQE and dispatch */
  11159. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  11160. case CQE_CODE_COMPL_WQE:
  11161. cq->CQ_wq++;
  11162. /* Process the WQ complete event */
  11163. phba->last_completion_time = jiffies;
  11164. lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
  11165. (struct lpfc_wcqe_complete *)&wcqe);
  11166. break;
  11167. case CQE_CODE_RELEASE_WQE:
  11168. cq->CQ_release_wqe++;
  11169. /* Process the WQ release event */
  11170. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  11171. (struct lpfc_wcqe_release *)&wcqe);
  11172. break;
  11173. case CQE_CODE_XRI_ABORTED:
  11174. cq->CQ_xri_aborted++;
  11175. /* Process the WQ XRI abort event */
  11176. phba->last_completion_time = jiffies;
  11177. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  11178. (struct sli4_wcqe_xri_aborted *)&wcqe);
  11179. break;
  11180. default:
  11181. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11182. "0144 Not a valid WCQE code: x%x\n",
  11183. bf_get(lpfc_wcqe_c_code, &wcqe));
  11184. break;
  11185. }
  11186. return workposted;
  11187. }
  11188. /**
  11189. * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
  11190. * @phba: Pointer to HBA context object.
  11191. * @eqe: Pointer to fast-path event queue entry.
  11192. *
  11193. * This routine process a event queue entry from the fast-path event queue.
  11194. * It will check the MajorCode and MinorCode to determine this is for a
  11195. * completion event on a completion queue, if not, an error shall be logged
  11196. * and just return. Otherwise, it will get to the corresponding completion
  11197. * queue and process all the entries on the completion queue, rearm the
  11198. * completion queue, and then return.
  11199. **/
  11200. static void
  11201. lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  11202. uint32_t qidx)
  11203. {
  11204. struct lpfc_queue *cq;
  11205. struct lpfc_cqe *cqe;
  11206. bool workposted = false;
  11207. uint16_t cqid;
  11208. int ecount = 0;
  11209. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  11210. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11211. "0366 Not a valid completion "
  11212. "event: majorcode=x%x, minorcode=x%x\n",
  11213. bf_get_le32(lpfc_eqe_major_code, eqe),
  11214. bf_get_le32(lpfc_eqe_minor_code, eqe));
  11215. return;
  11216. }
  11217. /* Get the reference to the corresponding CQ */
  11218. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  11219. /* Check if this is a Slow path event */
  11220. if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
  11221. lpfc_sli4_sp_handle_eqe(phba, eqe,
  11222. phba->sli4_hba.hba_eq[qidx]);
  11223. return;
  11224. }
  11225. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  11226. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11227. "3146 Fast-path completion queues "
  11228. "does not exist\n");
  11229. return;
  11230. }
  11231. cq = phba->sli4_hba.fcp_cq[qidx];
  11232. if (unlikely(!cq)) {
  11233. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  11234. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11235. "0367 Fast-path completion queue "
  11236. "(%d) does not exist\n", qidx);
  11237. return;
  11238. }
  11239. if (unlikely(cqid != cq->queue_id)) {
  11240. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11241. "0368 Miss-matched fast-path completion "
  11242. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  11243. cqid, cq->queue_id);
  11244. return;
  11245. }
  11246. /* Process all the entries to the CQ */
  11247. while ((cqe = lpfc_sli4_cq_get(cq))) {
  11248. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  11249. if (!(++ecount % cq->entry_repost))
  11250. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  11251. }
  11252. /* Track the max number of CQEs processed in 1 EQ */
  11253. if (ecount > cq->CQ_max_cqe)
  11254. cq->CQ_max_cqe = ecount;
  11255. /* Catch the no cq entry condition */
  11256. if (unlikely(ecount == 0))
  11257. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11258. "0369 No entry from fast-path completion "
  11259. "queue fcpcqid=%d\n", cq->queue_id);
  11260. /* In any case, flash and re-arm the CQ */
  11261. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  11262. /* wake up worker thread if there are works to be done */
  11263. if (workposted)
  11264. lpfc_worker_wake_up(phba);
  11265. }
  11266. static void
  11267. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11268. {
  11269. struct lpfc_eqe *eqe;
  11270. /* walk all the EQ entries and drop on the floor */
  11271. while ((eqe = lpfc_sli4_eq_get(eq)))
  11272. ;
  11273. /* Clear and re-arm the EQ */
  11274. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  11275. }
  11276. /**
  11277. * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
  11278. * entry
  11279. * @phba: Pointer to HBA context object.
  11280. * @eqe: Pointer to fast-path event queue entry.
  11281. *
  11282. * This routine process a event queue entry from the Flash Optimized Fabric
  11283. * event queue. It will check the MajorCode and MinorCode to determine this
  11284. * is for a completion event on a completion queue, if not, an error shall be
  11285. * logged and just return. Otherwise, it will get to the corresponding
  11286. * completion queue and process all the entries on the completion queue, rearm
  11287. * the completion queue, and then return.
  11288. **/
  11289. static void
  11290. lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  11291. {
  11292. struct lpfc_queue *cq;
  11293. struct lpfc_cqe *cqe;
  11294. bool workposted = false;
  11295. uint16_t cqid;
  11296. int ecount = 0;
  11297. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  11298. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11299. "9147 Not a valid completion "
  11300. "event: majorcode=x%x, minorcode=x%x\n",
  11301. bf_get_le32(lpfc_eqe_major_code, eqe),
  11302. bf_get_le32(lpfc_eqe_minor_code, eqe));
  11303. return;
  11304. }
  11305. /* Get the reference to the corresponding CQ */
  11306. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  11307. /* Next check for OAS */
  11308. cq = phba->sli4_hba.oas_cq;
  11309. if (unlikely(!cq)) {
  11310. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  11311. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11312. "9148 OAS completion queue "
  11313. "does not exist\n");
  11314. return;
  11315. }
  11316. if (unlikely(cqid != cq->queue_id)) {
  11317. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11318. "9149 Miss-matched fast-path compl "
  11319. "queue id: eqcqid=%d, fcpcqid=%d\n",
  11320. cqid, cq->queue_id);
  11321. return;
  11322. }
  11323. /* Process all the entries to the OAS CQ */
  11324. while ((cqe = lpfc_sli4_cq_get(cq))) {
  11325. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  11326. if (!(++ecount % cq->entry_repost))
  11327. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  11328. }
  11329. /* Track the max number of CQEs processed in 1 EQ */
  11330. if (ecount > cq->CQ_max_cqe)
  11331. cq->CQ_max_cqe = ecount;
  11332. /* Catch the no cq entry condition */
  11333. if (unlikely(ecount == 0))
  11334. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11335. "9153 No entry from fast-path completion "
  11336. "queue fcpcqid=%d\n", cq->queue_id);
  11337. /* In any case, flash and re-arm the CQ */
  11338. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  11339. /* wake up worker thread if there are works to be done */
  11340. if (workposted)
  11341. lpfc_worker_wake_up(phba);
  11342. }
  11343. /**
  11344. * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
  11345. * @irq: Interrupt number.
  11346. * @dev_id: The device context pointer.
  11347. *
  11348. * This function is directly called from the PCI layer as an interrupt
  11349. * service routine when device with SLI-4 interface spec is enabled with
  11350. * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
  11351. * IOCB ring event in the HBA. However, when the device is enabled with either
  11352. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  11353. * device-level interrupt handler. When the PCI slot is in error recovery
  11354. * or the HBA is undergoing initialization, the interrupt handler will not
  11355. * process the interrupt. The Flash Optimized Fabric ring event are handled in
  11356. * the intrrupt context. This function is called without any lock held.
  11357. * It gets the hbalock to access and update SLI data structures. Note that,
  11358. * the EQ to CQ are one-to-one map such that the EQ index is
  11359. * equal to that of CQ index.
  11360. *
  11361. * This function returns IRQ_HANDLED when interrupt is handled else it
  11362. * returns IRQ_NONE.
  11363. **/
  11364. irqreturn_t
  11365. lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
  11366. {
  11367. struct lpfc_hba *phba;
  11368. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  11369. struct lpfc_queue *eq;
  11370. struct lpfc_eqe *eqe;
  11371. unsigned long iflag;
  11372. int ecount = 0;
  11373. uint32_t eqidx;
  11374. /* Get the driver's phba structure from the dev_id */
  11375. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  11376. phba = fcp_eq_hdl->phba;
  11377. eqidx = fcp_eq_hdl->idx;
  11378. if (unlikely(!phba))
  11379. return IRQ_NONE;
  11380. /* Get to the EQ struct associated with this vector */
  11381. eq = phba->sli4_hba.fof_eq;
  11382. if (unlikely(!eq))
  11383. return IRQ_NONE;
  11384. /* Check device state for handling interrupt */
  11385. if (unlikely(lpfc_intr_state_check(phba))) {
  11386. eq->EQ_badstate++;
  11387. /* Check again for link_state with lock held */
  11388. spin_lock_irqsave(&phba->hbalock, iflag);
  11389. if (phba->link_state < LPFC_LINK_DOWN)
  11390. /* Flush, clear interrupt, and rearm the EQ */
  11391. lpfc_sli4_eq_flush(phba, eq);
  11392. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11393. return IRQ_NONE;
  11394. }
  11395. /*
  11396. * Process all the event on FCP fast-path EQ
  11397. */
  11398. while ((eqe = lpfc_sli4_eq_get(eq))) {
  11399. lpfc_sli4_fof_handle_eqe(phba, eqe);
  11400. if (!(++ecount % eq->entry_repost))
  11401. lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
  11402. eq->EQ_processed++;
  11403. }
  11404. /* Track the max number of EQEs processed in 1 intr */
  11405. if (ecount > eq->EQ_max_eqe)
  11406. eq->EQ_max_eqe = ecount;
  11407. if (unlikely(ecount == 0)) {
  11408. eq->EQ_no_entry++;
  11409. if (phba->intr_type == MSIX)
  11410. /* MSI-X treated interrupt served as no EQ share INT */
  11411. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11412. "9145 MSI-X interrupt with no EQE\n");
  11413. else {
  11414. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11415. "9146 ISR interrupt with no EQE\n");
  11416. /* Non MSI-X treated on interrupt as EQ share INT */
  11417. return IRQ_NONE;
  11418. }
  11419. }
  11420. /* Always clear and re-arm the fast-path EQ */
  11421. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  11422. return IRQ_HANDLED;
  11423. }
  11424. /**
  11425. * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
  11426. * @irq: Interrupt number.
  11427. * @dev_id: The device context pointer.
  11428. *
  11429. * This function is directly called from the PCI layer as an interrupt
  11430. * service routine when device with SLI-4 interface spec is enabled with
  11431. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  11432. * ring event in the HBA. However, when the device is enabled with either
  11433. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  11434. * device-level interrupt handler. When the PCI slot is in error recovery
  11435. * or the HBA is undergoing initialization, the interrupt handler will not
  11436. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  11437. * the intrrupt context. This function is called without any lock held.
  11438. * It gets the hbalock to access and update SLI data structures. Note that,
  11439. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  11440. * equal to that of FCP CQ index.
  11441. *
  11442. * The link attention and ELS ring attention events are handled
  11443. * by the worker thread. The interrupt handler signals the worker thread
  11444. * and returns for these events. This function is called without any lock
  11445. * held. It gets the hbalock to access and update SLI data structures.
  11446. *
  11447. * This function returns IRQ_HANDLED when interrupt is handled else it
  11448. * returns IRQ_NONE.
  11449. **/
  11450. irqreturn_t
  11451. lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
  11452. {
  11453. struct lpfc_hba *phba;
  11454. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  11455. struct lpfc_queue *fpeq;
  11456. struct lpfc_eqe *eqe;
  11457. unsigned long iflag;
  11458. int ecount = 0;
  11459. int fcp_eqidx;
  11460. /* Get the driver's phba structure from the dev_id */
  11461. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  11462. phba = fcp_eq_hdl->phba;
  11463. fcp_eqidx = fcp_eq_hdl->idx;
  11464. if (unlikely(!phba))
  11465. return IRQ_NONE;
  11466. if (unlikely(!phba->sli4_hba.hba_eq))
  11467. return IRQ_NONE;
  11468. /* Get to the EQ struct associated with this vector */
  11469. fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
  11470. if (unlikely(!fpeq))
  11471. return IRQ_NONE;
  11472. if (lpfc_fcp_look_ahead) {
  11473. if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
  11474. lpfc_sli4_eq_clr_intr(fpeq);
  11475. else {
  11476. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11477. return IRQ_NONE;
  11478. }
  11479. }
  11480. /* Check device state for handling interrupt */
  11481. if (unlikely(lpfc_intr_state_check(phba))) {
  11482. fpeq->EQ_badstate++;
  11483. /* Check again for link_state with lock held */
  11484. spin_lock_irqsave(&phba->hbalock, iflag);
  11485. if (phba->link_state < LPFC_LINK_DOWN)
  11486. /* Flush, clear interrupt, and rearm the EQ */
  11487. lpfc_sli4_eq_flush(phba, fpeq);
  11488. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11489. if (lpfc_fcp_look_ahead)
  11490. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11491. return IRQ_NONE;
  11492. }
  11493. /*
  11494. * Process all the event on FCP fast-path EQ
  11495. */
  11496. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  11497. if (eqe == NULL)
  11498. break;
  11499. lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
  11500. if (!(++ecount % fpeq->entry_repost))
  11501. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  11502. fpeq->EQ_processed++;
  11503. }
  11504. /* Track the max number of EQEs processed in 1 intr */
  11505. if (ecount > fpeq->EQ_max_eqe)
  11506. fpeq->EQ_max_eqe = ecount;
  11507. /* Always clear and re-arm the fast-path EQ */
  11508. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  11509. if (unlikely(ecount == 0)) {
  11510. fpeq->EQ_no_entry++;
  11511. if (lpfc_fcp_look_ahead) {
  11512. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11513. return IRQ_NONE;
  11514. }
  11515. if (phba->intr_type == MSIX)
  11516. /* MSI-X treated interrupt served as no EQ share INT */
  11517. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11518. "0358 MSI-X interrupt with no EQE\n");
  11519. else
  11520. /* Non MSI-X treated on interrupt as EQ share INT */
  11521. return IRQ_NONE;
  11522. }
  11523. if (lpfc_fcp_look_ahead)
  11524. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11525. return IRQ_HANDLED;
  11526. } /* lpfc_sli4_fp_intr_handler */
  11527. /**
  11528. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  11529. * @irq: Interrupt number.
  11530. * @dev_id: The device context pointer.
  11531. *
  11532. * This function is the device-level interrupt handler to device with SLI-4
  11533. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  11534. * interrupt mode is enabled and there is an event in the HBA which requires
  11535. * driver attention. This function invokes the slow-path interrupt attention
  11536. * handling function and fast-path interrupt attention handling function in
  11537. * turn to process the relevant HBA attention events. This function is called
  11538. * without any lock held. It gets the hbalock to access and update SLI data
  11539. * structures.
  11540. *
  11541. * This function returns IRQ_HANDLED when interrupt is handled, else it
  11542. * returns IRQ_NONE.
  11543. **/
  11544. irqreturn_t
  11545. lpfc_sli4_intr_handler(int irq, void *dev_id)
  11546. {
  11547. struct lpfc_hba *phba;
  11548. irqreturn_t hba_irq_rc;
  11549. bool hba_handled = false;
  11550. int fcp_eqidx;
  11551. /* Get the driver's phba structure from the dev_id */
  11552. phba = (struct lpfc_hba *)dev_id;
  11553. if (unlikely(!phba))
  11554. return IRQ_NONE;
  11555. /*
  11556. * Invoke fast-path host attention interrupt handling as appropriate.
  11557. */
  11558. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
  11559. hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
  11560. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  11561. if (hba_irq_rc == IRQ_HANDLED)
  11562. hba_handled |= true;
  11563. }
  11564. if (phba->cfg_fof) {
  11565. hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
  11566. &phba->sli4_hba.fcp_eq_hdl[0]);
  11567. if (hba_irq_rc == IRQ_HANDLED)
  11568. hba_handled |= true;
  11569. }
  11570. return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
  11571. } /* lpfc_sli4_intr_handler */
  11572. /**
  11573. * lpfc_sli4_queue_free - free a queue structure and associated memory
  11574. * @queue: The queue structure to free.
  11575. *
  11576. * This function frees a queue structure and the DMAable memory used for
  11577. * the host resident queue. This function must be called after destroying the
  11578. * queue on the HBA.
  11579. **/
  11580. void
  11581. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  11582. {
  11583. struct lpfc_dmabuf *dmabuf;
  11584. if (!queue)
  11585. return;
  11586. while (!list_empty(&queue->page_list)) {
  11587. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  11588. list);
  11589. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  11590. dmabuf->virt, dmabuf->phys);
  11591. kfree(dmabuf);
  11592. }
  11593. kfree(queue);
  11594. return;
  11595. }
  11596. /**
  11597. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  11598. * @phba: The HBA that this queue is being created on.
  11599. * @entry_size: The size of each queue entry for this queue.
  11600. * @entry count: The number of entries that this queue will handle.
  11601. *
  11602. * This function allocates a queue structure and the DMAable memory used for
  11603. * the host resident queue. This function must be called before creating the
  11604. * queue on the HBA.
  11605. **/
  11606. struct lpfc_queue *
  11607. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  11608. uint32_t entry_count)
  11609. {
  11610. struct lpfc_queue *queue;
  11611. struct lpfc_dmabuf *dmabuf;
  11612. int x, total_qe_count;
  11613. void *dma_pointer;
  11614. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11615. if (!phba->sli4_hba.pc_sli4_params.supported)
  11616. hw_page_size = SLI4_PAGE_SIZE;
  11617. queue = kzalloc(sizeof(struct lpfc_queue) +
  11618. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  11619. if (!queue)
  11620. return NULL;
  11621. queue->page_count = (ALIGN(entry_size * entry_count,
  11622. hw_page_size))/hw_page_size;
  11623. INIT_LIST_HEAD(&queue->list);
  11624. INIT_LIST_HEAD(&queue->page_list);
  11625. INIT_LIST_HEAD(&queue->child_list);
  11626. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  11627. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  11628. if (!dmabuf)
  11629. goto out_fail;
  11630. dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
  11631. hw_page_size, &dmabuf->phys,
  11632. GFP_KERNEL);
  11633. if (!dmabuf->virt) {
  11634. kfree(dmabuf);
  11635. goto out_fail;
  11636. }
  11637. dmabuf->buffer_tag = x;
  11638. list_add_tail(&dmabuf->list, &queue->page_list);
  11639. /* initialize queue's entry array */
  11640. dma_pointer = dmabuf->virt;
  11641. for (; total_qe_count < entry_count &&
  11642. dma_pointer < (hw_page_size + dmabuf->virt);
  11643. total_qe_count++, dma_pointer += entry_size) {
  11644. queue->qe[total_qe_count].address = dma_pointer;
  11645. }
  11646. }
  11647. queue->entry_size = entry_size;
  11648. queue->entry_count = entry_count;
  11649. /*
  11650. * entry_repost is calculated based on the number of entries in the
  11651. * queue. This works out except for RQs. If buffers are NOT initially
  11652. * posted for every RQE, entry_repost should be adjusted accordingly.
  11653. */
  11654. queue->entry_repost = (entry_count >> 3);
  11655. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  11656. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  11657. queue->phba = phba;
  11658. return queue;
  11659. out_fail:
  11660. lpfc_sli4_queue_free(queue);
  11661. return NULL;
  11662. }
  11663. /**
  11664. * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
  11665. * @phba: HBA structure that indicates port to create a queue on.
  11666. * @pci_barset: PCI BAR set flag.
  11667. *
  11668. * This function shall perform iomap of the specified PCI BAR address to host
  11669. * memory address if not already done so and return it. The returned host
  11670. * memory address can be NULL.
  11671. */
  11672. static void __iomem *
  11673. lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
  11674. {
  11675. struct pci_dev *pdev;
  11676. if (!phba->pcidev)
  11677. return NULL;
  11678. else
  11679. pdev = phba->pcidev;
  11680. switch (pci_barset) {
  11681. case WQ_PCI_BAR_0_AND_1:
  11682. return phba->pci_bar0_memmap_p;
  11683. case WQ_PCI_BAR_2_AND_3:
  11684. return phba->pci_bar2_memmap_p;
  11685. case WQ_PCI_BAR_4_AND_5:
  11686. return phba->pci_bar4_memmap_p;
  11687. default:
  11688. break;
  11689. }
  11690. return NULL;
  11691. }
  11692. /**
  11693. * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
  11694. * @phba: HBA structure that indicates port to create a queue on.
  11695. * @startq: The starting FCP EQ to modify
  11696. *
  11697. * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
  11698. *
  11699. * The @phba struct is used to send mailbox command to HBA. The @startq
  11700. * is used to get the starting FCP EQ to change.
  11701. * This function is asynchronous and will wait for the mailbox
  11702. * command to finish before continuing.
  11703. *
  11704. * On success this function will return a zero. If unable to allocate enough
  11705. * memory this function will return -ENOMEM. If the queue create mailbox command
  11706. * fails this function will return -ENXIO.
  11707. **/
  11708. int
  11709. lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint32_t startq)
  11710. {
  11711. struct lpfc_mbx_modify_eq_delay *eq_delay;
  11712. LPFC_MBOXQ_t *mbox;
  11713. struct lpfc_queue *eq;
  11714. int cnt, rc, length, status = 0;
  11715. uint32_t shdr_status, shdr_add_status;
  11716. uint32_t result;
  11717. int fcp_eqidx;
  11718. union lpfc_sli4_cfg_shdr *shdr;
  11719. uint16_t dmult;
  11720. if (startq >= phba->cfg_fcp_io_channel)
  11721. return 0;
  11722. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11723. if (!mbox)
  11724. return -ENOMEM;
  11725. length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
  11726. sizeof(struct lpfc_sli4_cfg_mhdr));
  11727. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11728. LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
  11729. length, LPFC_SLI4_MBX_EMBED);
  11730. eq_delay = &mbox->u.mqe.un.eq_delay;
  11731. /* Calculate delay multiper from maximum interrupt per second */
  11732. result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
  11733. if (result > LPFC_DMULT_CONST)
  11734. dmult = 0;
  11735. else
  11736. dmult = LPFC_DMULT_CONST/result - 1;
  11737. cnt = 0;
  11738. for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
  11739. fcp_eqidx++) {
  11740. eq = phba->sli4_hba.hba_eq[fcp_eqidx];
  11741. if (!eq)
  11742. continue;
  11743. eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
  11744. eq_delay->u.request.eq[cnt].phase = 0;
  11745. eq_delay->u.request.eq[cnt].delay_multi = dmult;
  11746. cnt++;
  11747. if (cnt >= LPFC_MAX_EQ_DELAY)
  11748. break;
  11749. }
  11750. eq_delay->u.request.num_eq = cnt;
  11751. mbox->vport = phba->pport;
  11752. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11753. mbox->context1 = NULL;
  11754. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11755. shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
  11756. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11757. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11758. if (shdr_status || shdr_add_status || rc) {
  11759. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11760. "2512 MODIFY_EQ_DELAY mailbox failed with "
  11761. "status x%x add_status x%x, mbx status x%x\n",
  11762. shdr_status, shdr_add_status, rc);
  11763. status = -ENXIO;
  11764. }
  11765. mempool_free(mbox, phba->mbox_mem_pool);
  11766. return status;
  11767. }
  11768. /**
  11769. * lpfc_eq_create - Create an Event Queue on the HBA
  11770. * @phba: HBA structure that indicates port to create a queue on.
  11771. * @eq: The queue structure to use to create the event queue.
  11772. * @imax: The maximum interrupt per second limit.
  11773. *
  11774. * This function creates an event queue, as detailed in @eq, on a port,
  11775. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  11776. *
  11777. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  11778. * is used to get the entry count and entry size that are necessary to
  11779. * determine the number of pages to allocate and use for this queue. This
  11780. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  11781. * event queue. This function is asynchronous and will wait for the mailbox
  11782. * command to finish before continuing.
  11783. *
  11784. * On success this function will return a zero. If unable to allocate enough
  11785. * memory this function will return -ENOMEM. If the queue create mailbox command
  11786. * fails this function will return -ENXIO.
  11787. **/
  11788. int
  11789. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
  11790. {
  11791. struct lpfc_mbx_eq_create *eq_create;
  11792. LPFC_MBOXQ_t *mbox;
  11793. int rc, length, status = 0;
  11794. struct lpfc_dmabuf *dmabuf;
  11795. uint32_t shdr_status, shdr_add_status;
  11796. union lpfc_sli4_cfg_shdr *shdr;
  11797. uint16_t dmult;
  11798. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11799. /* sanity check on queue memory */
  11800. if (!eq)
  11801. return -ENODEV;
  11802. if (!phba->sli4_hba.pc_sli4_params.supported)
  11803. hw_page_size = SLI4_PAGE_SIZE;
  11804. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11805. if (!mbox)
  11806. return -ENOMEM;
  11807. length = (sizeof(struct lpfc_mbx_eq_create) -
  11808. sizeof(struct lpfc_sli4_cfg_mhdr));
  11809. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11810. LPFC_MBOX_OPCODE_EQ_CREATE,
  11811. length, LPFC_SLI4_MBX_EMBED);
  11812. eq_create = &mbox->u.mqe.un.eq_create;
  11813. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  11814. eq->page_count);
  11815. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  11816. LPFC_EQE_SIZE);
  11817. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  11818. /* don't setup delay multiplier using EQ_CREATE */
  11819. dmult = 0;
  11820. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  11821. dmult);
  11822. switch (eq->entry_count) {
  11823. default:
  11824. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11825. "0360 Unsupported EQ count. (%d)\n",
  11826. eq->entry_count);
  11827. if (eq->entry_count < 256)
  11828. return -EINVAL;
  11829. /* otherwise default to smallest count (drop through) */
  11830. case 256:
  11831. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11832. LPFC_EQ_CNT_256);
  11833. break;
  11834. case 512:
  11835. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11836. LPFC_EQ_CNT_512);
  11837. break;
  11838. case 1024:
  11839. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11840. LPFC_EQ_CNT_1024);
  11841. break;
  11842. case 2048:
  11843. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11844. LPFC_EQ_CNT_2048);
  11845. break;
  11846. case 4096:
  11847. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11848. LPFC_EQ_CNT_4096);
  11849. break;
  11850. }
  11851. list_for_each_entry(dmabuf, &eq->page_list, list) {
  11852. memset(dmabuf->virt, 0, hw_page_size);
  11853. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11854. putPaddrLow(dmabuf->phys);
  11855. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11856. putPaddrHigh(dmabuf->phys);
  11857. }
  11858. mbox->vport = phba->pport;
  11859. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11860. mbox->context1 = NULL;
  11861. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11862. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  11863. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11864. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11865. if (shdr_status || shdr_add_status || rc) {
  11866. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11867. "2500 EQ_CREATE mailbox failed with "
  11868. "status x%x add_status x%x, mbx status x%x\n",
  11869. shdr_status, shdr_add_status, rc);
  11870. status = -ENXIO;
  11871. }
  11872. eq->type = LPFC_EQ;
  11873. eq->subtype = LPFC_NONE;
  11874. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  11875. if (eq->queue_id == 0xFFFF)
  11876. status = -ENXIO;
  11877. eq->host_index = 0;
  11878. eq->hba_index = 0;
  11879. mempool_free(mbox, phba->mbox_mem_pool);
  11880. return status;
  11881. }
  11882. /**
  11883. * lpfc_cq_create - Create a Completion Queue on the HBA
  11884. * @phba: HBA structure that indicates port to create a queue on.
  11885. * @cq: The queue structure to use to create the completion queue.
  11886. * @eq: The event queue to bind this completion queue to.
  11887. *
  11888. * This function creates a completion queue, as detailed in @wq, on a port,
  11889. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  11890. *
  11891. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11892. * is used to get the entry count and entry size that are necessary to
  11893. * determine the number of pages to allocate and use for this queue. The @eq
  11894. * is used to indicate which event queue to bind this completion queue to. This
  11895. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  11896. * completion queue. This function is asynchronous and will wait for the mailbox
  11897. * command to finish before continuing.
  11898. *
  11899. * On success this function will return a zero. If unable to allocate enough
  11900. * memory this function will return -ENOMEM. If the queue create mailbox command
  11901. * fails this function will return -ENXIO.
  11902. **/
  11903. int
  11904. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11905. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  11906. {
  11907. struct lpfc_mbx_cq_create *cq_create;
  11908. struct lpfc_dmabuf *dmabuf;
  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. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11914. /* sanity check on queue memory */
  11915. if (!cq || !eq)
  11916. return -ENODEV;
  11917. if (!phba->sli4_hba.pc_sli4_params.supported)
  11918. hw_page_size = SLI4_PAGE_SIZE;
  11919. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11920. if (!mbox)
  11921. return -ENOMEM;
  11922. length = (sizeof(struct lpfc_mbx_cq_create) -
  11923. sizeof(struct lpfc_sli4_cfg_mhdr));
  11924. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11925. LPFC_MBOX_OPCODE_CQ_CREATE,
  11926. length, LPFC_SLI4_MBX_EMBED);
  11927. cq_create = &mbox->u.mqe.un.cq_create;
  11928. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  11929. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  11930. cq->page_count);
  11931. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  11932. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  11933. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11934. phba->sli4_hba.pc_sli4_params.cqv);
  11935. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  11936. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  11937. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  11938. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  11939. eq->queue_id);
  11940. } else {
  11941. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  11942. eq->queue_id);
  11943. }
  11944. switch (cq->entry_count) {
  11945. default:
  11946. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11947. "0361 Unsupported CQ count. (%d)\n",
  11948. cq->entry_count);
  11949. if (cq->entry_count < 256) {
  11950. status = -EINVAL;
  11951. goto out;
  11952. }
  11953. /* otherwise default to smallest count (drop through) */
  11954. case 256:
  11955. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11956. LPFC_CQ_CNT_256);
  11957. break;
  11958. case 512:
  11959. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11960. LPFC_CQ_CNT_512);
  11961. break;
  11962. case 1024:
  11963. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11964. LPFC_CQ_CNT_1024);
  11965. break;
  11966. }
  11967. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11968. memset(dmabuf->virt, 0, hw_page_size);
  11969. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11970. putPaddrLow(dmabuf->phys);
  11971. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11972. putPaddrHigh(dmabuf->phys);
  11973. }
  11974. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11975. /* The IOCTL status is embedded in the mailbox subheader. */
  11976. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11977. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11978. if (shdr_status || shdr_add_status || rc) {
  11979. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11980. "2501 CQ_CREATE mailbox failed with "
  11981. "status x%x add_status x%x, mbx status x%x\n",
  11982. shdr_status, shdr_add_status, rc);
  11983. status = -ENXIO;
  11984. goto out;
  11985. }
  11986. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11987. if (cq->queue_id == 0xFFFF) {
  11988. status = -ENXIO;
  11989. goto out;
  11990. }
  11991. /* link the cq onto the parent eq child list */
  11992. list_add_tail(&cq->list, &eq->child_list);
  11993. /* Set up completion queue's type and subtype */
  11994. cq->type = type;
  11995. cq->subtype = subtype;
  11996. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11997. cq->assoc_qid = eq->queue_id;
  11998. cq->host_index = 0;
  11999. cq->hba_index = 0;
  12000. out:
  12001. mempool_free(mbox, phba->mbox_mem_pool);
  12002. return status;
  12003. }
  12004. /**
  12005. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  12006. * @phba: HBA structure that indicates port to create a queue on.
  12007. * @mq: The queue structure to use to create the mailbox queue.
  12008. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  12009. * @cq: The completion queue to associate with this cq.
  12010. *
  12011. * This function provides failback (fb) functionality when the
  12012. * mq_create_ext fails on older FW generations. It's purpose is identical
  12013. * to mq_create_ext otherwise.
  12014. *
  12015. * This routine cannot fail as all attributes were previously accessed and
  12016. * initialized in mq_create_ext.
  12017. **/
  12018. static void
  12019. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  12020. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  12021. {
  12022. struct lpfc_mbx_mq_create *mq_create;
  12023. struct lpfc_dmabuf *dmabuf;
  12024. int length;
  12025. length = (sizeof(struct lpfc_mbx_mq_create) -
  12026. sizeof(struct lpfc_sli4_cfg_mhdr));
  12027. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12028. LPFC_MBOX_OPCODE_MQ_CREATE,
  12029. length, LPFC_SLI4_MBX_EMBED);
  12030. mq_create = &mbox->u.mqe.un.mq_create;
  12031. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  12032. mq->page_count);
  12033. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  12034. cq->queue_id);
  12035. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  12036. switch (mq->entry_count) {
  12037. case 16:
  12038. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  12039. LPFC_MQ_RING_SIZE_16);
  12040. break;
  12041. case 32:
  12042. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  12043. LPFC_MQ_RING_SIZE_32);
  12044. break;
  12045. case 64:
  12046. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  12047. LPFC_MQ_RING_SIZE_64);
  12048. break;
  12049. case 128:
  12050. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  12051. LPFC_MQ_RING_SIZE_128);
  12052. break;
  12053. }
  12054. list_for_each_entry(dmabuf, &mq->page_list, list) {
  12055. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12056. putPaddrLow(dmabuf->phys);
  12057. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12058. putPaddrHigh(dmabuf->phys);
  12059. }
  12060. }
  12061. /**
  12062. * lpfc_mq_create - Create a mailbox Queue on the HBA
  12063. * @phba: HBA structure that indicates port to create a queue on.
  12064. * @mq: The queue structure to use to create the mailbox queue.
  12065. * @cq: The completion queue to associate with this cq.
  12066. * @subtype: The queue's subtype.
  12067. *
  12068. * This function creates a mailbox queue, as detailed in @mq, on a port,
  12069. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  12070. *
  12071. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  12072. * is used to get the entry count and entry size that are necessary to
  12073. * determine the number of pages to allocate and use for this queue. This
  12074. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  12075. * mailbox queue. This function is asynchronous and will wait for the mailbox
  12076. * command to finish before continuing.
  12077. *
  12078. * On success this function will return a zero. If unable to allocate enough
  12079. * memory this function will return -ENOMEM. If the queue create mailbox command
  12080. * fails this function will return -ENXIO.
  12081. **/
  12082. int32_t
  12083. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  12084. struct lpfc_queue *cq, uint32_t subtype)
  12085. {
  12086. struct lpfc_mbx_mq_create *mq_create;
  12087. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  12088. struct lpfc_dmabuf *dmabuf;
  12089. LPFC_MBOXQ_t *mbox;
  12090. int rc, length, status = 0;
  12091. uint32_t shdr_status, shdr_add_status;
  12092. union lpfc_sli4_cfg_shdr *shdr;
  12093. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  12094. /* sanity check on queue memory */
  12095. if (!mq || !cq)
  12096. return -ENODEV;
  12097. if (!phba->sli4_hba.pc_sli4_params.supported)
  12098. hw_page_size = SLI4_PAGE_SIZE;
  12099. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12100. if (!mbox)
  12101. return -ENOMEM;
  12102. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  12103. sizeof(struct lpfc_sli4_cfg_mhdr));
  12104. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12105. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  12106. length, LPFC_SLI4_MBX_EMBED);
  12107. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  12108. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  12109. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  12110. &mq_create_ext->u.request, mq->page_count);
  12111. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  12112. &mq_create_ext->u.request, 1);
  12113. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  12114. &mq_create_ext->u.request, 1);
  12115. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  12116. &mq_create_ext->u.request, 1);
  12117. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  12118. &mq_create_ext->u.request, 1);
  12119. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  12120. &mq_create_ext->u.request, 1);
  12121. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  12122. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12123. phba->sli4_hba.pc_sli4_params.mqv);
  12124. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  12125. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  12126. cq->queue_id);
  12127. else
  12128. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  12129. cq->queue_id);
  12130. switch (mq->entry_count) {
  12131. default:
  12132. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12133. "0362 Unsupported MQ count. (%d)\n",
  12134. mq->entry_count);
  12135. if (mq->entry_count < 16) {
  12136. status = -EINVAL;
  12137. goto out;
  12138. }
  12139. /* otherwise default to smallest count (drop through) */
  12140. case 16:
  12141. bf_set(lpfc_mq_context_ring_size,
  12142. &mq_create_ext->u.request.context,
  12143. LPFC_MQ_RING_SIZE_16);
  12144. break;
  12145. case 32:
  12146. bf_set(lpfc_mq_context_ring_size,
  12147. &mq_create_ext->u.request.context,
  12148. LPFC_MQ_RING_SIZE_32);
  12149. break;
  12150. case 64:
  12151. bf_set(lpfc_mq_context_ring_size,
  12152. &mq_create_ext->u.request.context,
  12153. LPFC_MQ_RING_SIZE_64);
  12154. break;
  12155. case 128:
  12156. bf_set(lpfc_mq_context_ring_size,
  12157. &mq_create_ext->u.request.context,
  12158. LPFC_MQ_RING_SIZE_128);
  12159. break;
  12160. }
  12161. list_for_each_entry(dmabuf, &mq->page_list, list) {
  12162. memset(dmabuf->virt, 0, hw_page_size);
  12163. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  12164. putPaddrLow(dmabuf->phys);
  12165. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  12166. putPaddrHigh(dmabuf->phys);
  12167. }
  12168. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12169. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  12170. &mq_create_ext->u.response);
  12171. if (rc != MBX_SUCCESS) {
  12172. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12173. "2795 MQ_CREATE_EXT failed with "
  12174. "status x%x. Failback to MQ_CREATE.\n",
  12175. rc);
  12176. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  12177. mq_create = &mbox->u.mqe.un.mq_create;
  12178. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12179. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  12180. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  12181. &mq_create->u.response);
  12182. }
  12183. /* The IOCTL status is embedded in the mailbox subheader. */
  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 (shdr_status || shdr_add_status || rc) {
  12187. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12188. "2502 MQ_CREATE mailbox failed with "
  12189. "status x%x add_status x%x, mbx status x%x\n",
  12190. shdr_status, shdr_add_status, rc);
  12191. status = -ENXIO;
  12192. goto out;
  12193. }
  12194. if (mq->queue_id == 0xFFFF) {
  12195. status = -ENXIO;
  12196. goto out;
  12197. }
  12198. mq->type = LPFC_MQ;
  12199. mq->assoc_qid = cq->queue_id;
  12200. mq->subtype = subtype;
  12201. mq->host_index = 0;
  12202. mq->hba_index = 0;
  12203. /* link the mq onto the parent cq child list */
  12204. list_add_tail(&mq->list, &cq->child_list);
  12205. out:
  12206. mempool_free(mbox, phba->mbox_mem_pool);
  12207. return status;
  12208. }
  12209. /**
  12210. * lpfc_wq_create - Create a Work Queue on the HBA
  12211. * @phba: HBA structure that indicates port to create a queue on.
  12212. * @wq: The queue structure to use to create the work queue.
  12213. * @cq: The completion queue to bind this work queue to.
  12214. * @subtype: The subtype of the work queue indicating its functionality.
  12215. *
  12216. * This function creates a work queue, as detailed in @wq, on a port, described
  12217. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  12218. *
  12219. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  12220. * is used to get the entry count and entry size that are necessary to
  12221. * determine the number of pages to allocate and use for this queue. The @cq
  12222. * is used to indicate which completion queue to bind this work queue to. This
  12223. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  12224. * work queue. This function is asynchronous and will wait for the mailbox
  12225. * command to finish before continuing.
  12226. *
  12227. * On success this function will return a zero. If unable to allocate enough
  12228. * memory this function will return -ENOMEM. If the queue create mailbox command
  12229. * fails this function will return -ENXIO.
  12230. **/
  12231. int
  12232. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  12233. struct lpfc_queue *cq, uint32_t subtype)
  12234. {
  12235. struct lpfc_mbx_wq_create *wq_create;
  12236. struct lpfc_dmabuf *dmabuf;
  12237. LPFC_MBOXQ_t *mbox;
  12238. int rc, length, status = 0;
  12239. uint32_t shdr_status, shdr_add_status;
  12240. union lpfc_sli4_cfg_shdr *shdr;
  12241. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  12242. struct dma_address *page;
  12243. void __iomem *bar_memmap_p;
  12244. uint32_t db_offset;
  12245. uint16_t pci_barset;
  12246. /* sanity check on queue memory */
  12247. if (!wq || !cq)
  12248. return -ENODEV;
  12249. if (!phba->sli4_hba.pc_sli4_params.supported)
  12250. hw_page_size = SLI4_PAGE_SIZE;
  12251. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12252. if (!mbox)
  12253. return -ENOMEM;
  12254. length = (sizeof(struct lpfc_mbx_wq_create) -
  12255. sizeof(struct lpfc_sli4_cfg_mhdr));
  12256. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12257. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  12258. length, LPFC_SLI4_MBX_EMBED);
  12259. wq_create = &mbox->u.mqe.un.wq_create;
  12260. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  12261. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  12262. wq->page_count);
  12263. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  12264. cq->queue_id);
  12265. /* wqv is the earliest version supported, NOT the latest */
  12266. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12267. phba->sli4_hba.pc_sli4_params.wqv);
  12268. switch (phba->sli4_hba.pc_sli4_params.wqv) {
  12269. case LPFC_Q_CREATE_VERSION_0:
  12270. switch (wq->entry_size) {
  12271. default:
  12272. case 64:
  12273. /* Nothing to do, version 0 ONLY supports 64 byte */
  12274. page = wq_create->u.request.page;
  12275. break;
  12276. case 128:
  12277. if (!(phba->sli4_hba.pc_sli4_params.wqsize &
  12278. LPFC_WQ_SZ128_SUPPORT)) {
  12279. status = -ERANGE;
  12280. goto out;
  12281. }
  12282. /* If we get here the HBA MUST also support V1 and
  12283. * we MUST use it
  12284. */
  12285. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12286. LPFC_Q_CREATE_VERSION_1);
  12287. bf_set(lpfc_mbx_wq_create_wqe_count,
  12288. &wq_create->u.request_1, wq->entry_count);
  12289. bf_set(lpfc_mbx_wq_create_wqe_size,
  12290. &wq_create->u.request_1,
  12291. LPFC_WQ_WQE_SIZE_128);
  12292. bf_set(lpfc_mbx_wq_create_page_size,
  12293. &wq_create->u.request_1,
  12294. (PAGE_SIZE/SLI4_PAGE_SIZE));
  12295. page = wq_create->u.request_1.page;
  12296. break;
  12297. }
  12298. break;
  12299. case LPFC_Q_CREATE_VERSION_1:
  12300. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  12301. wq->entry_count);
  12302. switch (wq->entry_size) {
  12303. default:
  12304. case 64:
  12305. bf_set(lpfc_mbx_wq_create_wqe_size,
  12306. &wq_create->u.request_1,
  12307. LPFC_WQ_WQE_SIZE_64);
  12308. break;
  12309. case 128:
  12310. if (!(phba->sli4_hba.pc_sli4_params.wqsize &
  12311. LPFC_WQ_SZ128_SUPPORT)) {
  12312. status = -ERANGE;
  12313. goto out;
  12314. }
  12315. bf_set(lpfc_mbx_wq_create_wqe_size,
  12316. &wq_create->u.request_1,
  12317. LPFC_WQ_WQE_SIZE_128);
  12318. break;
  12319. }
  12320. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  12321. (PAGE_SIZE/SLI4_PAGE_SIZE));
  12322. page = wq_create->u.request_1.page;
  12323. break;
  12324. default:
  12325. status = -ERANGE;
  12326. goto out;
  12327. }
  12328. list_for_each_entry(dmabuf, &wq->page_list, list) {
  12329. memset(dmabuf->virt, 0, hw_page_size);
  12330. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  12331. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  12332. }
  12333. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  12334. bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
  12335. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12336. /* The IOCTL status is embedded in the mailbox subheader. */
  12337. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12338. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12339. if (shdr_status || shdr_add_status || rc) {
  12340. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12341. "2503 WQ_CREATE mailbox failed with "
  12342. "status x%x add_status x%x, mbx status x%x\n",
  12343. shdr_status, shdr_add_status, rc);
  12344. status = -ENXIO;
  12345. goto out;
  12346. }
  12347. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  12348. if (wq->queue_id == 0xFFFF) {
  12349. status = -ENXIO;
  12350. goto out;
  12351. }
  12352. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
  12353. wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
  12354. &wq_create->u.response);
  12355. if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
  12356. (wq->db_format != LPFC_DB_RING_FORMAT)) {
  12357. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12358. "3265 WQ[%d] doorbell format not "
  12359. "supported: x%x\n", wq->queue_id,
  12360. wq->db_format);
  12361. status = -EINVAL;
  12362. goto out;
  12363. }
  12364. pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
  12365. &wq_create->u.response);
  12366. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
  12367. if (!bar_memmap_p) {
  12368. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12369. "3263 WQ[%d] failed to memmap pci "
  12370. "barset:x%x\n", wq->queue_id,
  12371. pci_barset);
  12372. status = -ENOMEM;
  12373. goto out;
  12374. }
  12375. db_offset = wq_create->u.response.doorbell_offset;
  12376. if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
  12377. (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
  12378. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12379. "3252 WQ[%d] doorbell offset not "
  12380. "supported: x%x\n", wq->queue_id,
  12381. db_offset);
  12382. status = -EINVAL;
  12383. goto out;
  12384. }
  12385. wq->db_regaddr = bar_memmap_p + db_offset;
  12386. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12387. "3264 WQ[%d]: barset:x%x, offset:x%x, "
  12388. "format:x%x\n", wq->queue_id, pci_barset,
  12389. db_offset, wq->db_format);
  12390. } else {
  12391. wq->db_format = LPFC_DB_LIST_FORMAT;
  12392. wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
  12393. }
  12394. wq->type = LPFC_WQ;
  12395. wq->assoc_qid = cq->queue_id;
  12396. wq->subtype = subtype;
  12397. wq->host_index = 0;
  12398. wq->hba_index = 0;
  12399. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  12400. /* link the wq onto the parent cq child list */
  12401. list_add_tail(&wq->list, &cq->child_list);
  12402. out:
  12403. mempool_free(mbox, phba->mbox_mem_pool);
  12404. return status;
  12405. }
  12406. /**
  12407. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  12408. * @phba: HBA structure that indicates port to create a queue on.
  12409. * @rq: The queue structure to use for the receive queue.
  12410. * @qno: The associated HBQ number
  12411. *
  12412. *
  12413. * For SLI4 we need to adjust the RQ repost value based on
  12414. * the number of buffers that are initially posted to the RQ.
  12415. */
  12416. void
  12417. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  12418. {
  12419. uint32_t cnt;
  12420. /* sanity check on queue memory */
  12421. if (!rq)
  12422. return;
  12423. cnt = lpfc_hbq_defs[qno]->entry_count;
  12424. /* Recalc repost for RQs based on buffers initially posted */
  12425. cnt = (cnt >> 3);
  12426. if (cnt < LPFC_QUEUE_MIN_REPOST)
  12427. cnt = LPFC_QUEUE_MIN_REPOST;
  12428. rq->entry_repost = cnt;
  12429. }
  12430. /**
  12431. * lpfc_rq_create - Create a Receive Queue on the HBA
  12432. * @phba: HBA structure that indicates port to create a queue on.
  12433. * @hrq: The queue structure to use to create the header receive queue.
  12434. * @drq: The queue structure to use to create the data receive queue.
  12435. * @cq: The completion queue to bind this work queue to.
  12436. *
  12437. * This function creates a receive buffer queue pair , as detailed in @hrq and
  12438. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  12439. * to the HBA.
  12440. *
  12441. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  12442. * struct is used to get the entry count that is necessary to determine the
  12443. * number of pages to use for this queue. The @cq is used to indicate which
  12444. * completion queue to bind received buffers that are posted to these queues to.
  12445. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  12446. * receive queue pair. This function is asynchronous and will wait for the
  12447. * mailbox command to finish before continuing.
  12448. *
  12449. * On success this function will return a zero. If unable to allocate enough
  12450. * memory this function will return -ENOMEM. If the queue create mailbox command
  12451. * fails this function will return -ENXIO.
  12452. **/
  12453. int
  12454. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  12455. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  12456. {
  12457. struct lpfc_mbx_rq_create *rq_create;
  12458. struct lpfc_dmabuf *dmabuf;
  12459. LPFC_MBOXQ_t *mbox;
  12460. int rc, length, status = 0;
  12461. uint32_t shdr_status, shdr_add_status;
  12462. union lpfc_sli4_cfg_shdr *shdr;
  12463. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  12464. void __iomem *bar_memmap_p;
  12465. uint32_t db_offset;
  12466. uint16_t pci_barset;
  12467. /* sanity check on queue memory */
  12468. if (!hrq || !drq || !cq)
  12469. return -ENODEV;
  12470. if (!phba->sli4_hba.pc_sli4_params.supported)
  12471. hw_page_size = SLI4_PAGE_SIZE;
  12472. if (hrq->entry_count != drq->entry_count)
  12473. return -EINVAL;
  12474. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12475. if (!mbox)
  12476. return -ENOMEM;
  12477. length = (sizeof(struct lpfc_mbx_rq_create) -
  12478. sizeof(struct lpfc_sli4_cfg_mhdr));
  12479. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12480. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  12481. length, LPFC_SLI4_MBX_EMBED);
  12482. rq_create = &mbox->u.mqe.un.rq_create;
  12483. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  12484. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12485. phba->sli4_hba.pc_sli4_params.rqv);
  12486. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  12487. bf_set(lpfc_rq_context_rqe_count_1,
  12488. &rq_create->u.request.context,
  12489. hrq->entry_count);
  12490. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  12491. bf_set(lpfc_rq_context_rqe_size,
  12492. &rq_create->u.request.context,
  12493. LPFC_RQE_SIZE_8);
  12494. bf_set(lpfc_rq_context_page_size,
  12495. &rq_create->u.request.context,
  12496. (PAGE_SIZE/SLI4_PAGE_SIZE));
  12497. } else {
  12498. switch (hrq->entry_count) {
  12499. default:
  12500. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12501. "2535 Unsupported RQ count. (%d)\n",
  12502. hrq->entry_count);
  12503. if (hrq->entry_count < 512) {
  12504. status = -EINVAL;
  12505. goto out;
  12506. }
  12507. /* otherwise default to smallest count (drop through) */
  12508. case 512:
  12509. bf_set(lpfc_rq_context_rqe_count,
  12510. &rq_create->u.request.context,
  12511. LPFC_RQ_RING_SIZE_512);
  12512. break;
  12513. case 1024:
  12514. bf_set(lpfc_rq_context_rqe_count,
  12515. &rq_create->u.request.context,
  12516. LPFC_RQ_RING_SIZE_1024);
  12517. break;
  12518. case 2048:
  12519. bf_set(lpfc_rq_context_rqe_count,
  12520. &rq_create->u.request.context,
  12521. LPFC_RQ_RING_SIZE_2048);
  12522. break;
  12523. case 4096:
  12524. bf_set(lpfc_rq_context_rqe_count,
  12525. &rq_create->u.request.context,
  12526. LPFC_RQ_RING_SIZE_4096);
  12527. break;
  12528. }
  12529. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  12530. LPFC_HDR_BUF_SIZE);
  12531. }
  12532. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  12533. cq->queue_id);
  12534. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  12535. hrq->page_count);
  12536. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  12537. memset(dmabuf->virt, 0, hw_page_size);
  12538. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12539. putPaddrLow(dmabuf->phys);
  12540. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12541. putPaddrHigh(dmabuf->phys);
  12542. }
  12543. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  12544. bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
  12545. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12546. /* The IOCTL status is embedded in the mailbox subheader. */
  12547. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12548. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12549. if (shdr_status || shdr_add_status || rc) {
  12550. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12551. "2504 RQ_CREATE mailbox failed with "
  12552. "status x%x add_status x%x, mbx status x%x\n",
  12553. shdr_status, shdr_add_status, rc);
  12554. status = -ENXIO;
  12555. goto out;
  12556. }
  12557. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  12558. if (hrq->queue_id == 0xFFFF) {
  12559. status = -ENXIO;
  12560. goto out;
  12561. }
  12562. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
  12563. hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
  12564. &rq_create->u.response);
  12565. if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
  12566. (hrq->db_format != LPFC_DB_RING_FORMAT)) {
  12567. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12568. "3262 RQ [%d] doorbell format not "
  12569. "supported: x%x\n", hrq->queue_id,
  12570. hrq->db_format);
  12571. status = -EINVAL;
  12572. goto out;
  12573. }
  12574. pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
  12575. &rq_create->u.response);
  12576. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
  12577. if (!bar_memmap_p) {
  12578. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12579. "3269 RQ[%d] failed to memmap pci "
  12580. "barset:x%x\n", hrq->queue_id,
  12581. pci_barset);
  12582. status = -ENOMEM;
  12583. goto out;
  12584. }
  12585. db_offset = rq_create->u.response.doorbell_offset;
  12586. if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
  12587. (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
  12588. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12589. "3270 RQ[%d] doorbell offset not "
  12590. "supported: x%x\n", hrq->queue_id,
  12591. db_offset);
  12592. status = -EINVAL;
  12593. goto out;
  12594. }
  12595. hrq->db_regaddr = bar_memmap_p + db_offset;
  12596. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12597. "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
  12598. "format:x%x\n", hrq->queue_id, pci_barset,
  12599. db_offset, hrq->db_format);
  12600. } else {
  12601. hrq->db_format = LPFC_DB_RING_FORMAT;
  12602. hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
  12603. }
  12604. hrq->type = LPFC_HRQ;
  12605. hrq->assoc_qid = cq->queue_id;
  12606. hrq->subtype = subtype;
  12607. hrq->host_index = 0;
  12608. hrq->hba_index = 0;
  12609. /* now create the data queue */
  12610. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12611. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  12612. length, LPFC_SLI4_MBX_EMBED);
  12613. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12614. phba->sli4_hba.pc_sli4_params.rqv);
  12615. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  12616. bf_set(lpfc_rq_context_rqe_count_1,
  12617. &rq_create->u.request.context, hrq->entry_count);
  12618. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  12619. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  12620. LPFC_RQE_SIZE_8);
  12621. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  12622. (PAGE_SIZE/SLI4_PAGE_SIZE));
  12623. } else {
  12624. switch (drq->entry_count) {
  12625. default:
  12626. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12627. "2536 Unsupported RQ count. (%d)\n",
  12628. drq->entry_count);
  12629. if (drq->entry_count < 512) {
  12630. status = -EINVAL;
  12631. goto out;
  12632. }
  12633. /* otherwise default to smallest count (drop through) */
  12634. case 512:
  12635. bf_set(lpfc_rq_context_rqe_count,
  12636. &rq_create->u.request.context,
  12637. LPFC_RQ_RING_SIZE_512);
  12638. break;
  12639. case 1024:
  12640. bf_set(lpfc_rq_context_rqe_count,
  12641. &rq_create->u.request.context,
  12642. LPFC_RQ_RING_SIZE_1024);
  12643. break;
  12644. case 2048:
  12645. bf_set(lpfc_rq_context_rqe_count,
  12646. &rq_create->u.request.context,
  12647. LPFC_RQ_RING_SIZE_2048);
  12648. break;
  12649. case 4096:
  12650. bf_set(lpfc_rq_context_rqe_count,
  12651. &rq_create->u.request.context,
  12652. LPFC_RQ_RING_SIZE_4096);
  12653. break;
  12654. }
  12655. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  12656. LPFC_DATA_BUF_SIZE);
  12657. }
  12658. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  12659. cq->queue_id);
  12660. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  12661. drq->page_count);
  12662. list_for_each_entry(dmabuf, &drq->page_list, list) {
  12663. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12664. putPaddrLow(dmabuf->phys);
  12665. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12666. putPaddrHigh(dmabuf->phys);
  12667. }
  12668. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  12669. bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
  12670. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12671. /* The IOCTL status is embedded in the mailbox subheader. */
  12672. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  12673. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12674. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12675. if (shdr_status || shdr_add_status || rc) {
  12676. status = -ENXIO;
  12677. goto out;
  12678. }
  12679. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  12680. if (drq->queue_id == 0xFFFF) {
  12681. status = -ENXIO;
  12682. goto out;
  12683. }
  12684. drq->type = LPFC_DRQ;
  12685. drq->assoc_qid = cq->queue_id;
  12686. drq->subtype = subtype;
  12687. drq->host_index = 0;
  12688. drq->hba_index = 0;
  12689. /* link the header and data RQs onto the parent cq child list */
  12690. list_add_tail(&hrq->list, &cq->child_list);
  12691. list_add_tail(&drq->list, &cq->child_list);
  12692. out:
  12693. mempool_free(mbox, phba->mbox_mem_pool);
  12694. return status;
  12695. }
  12696. /**
  12697. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  12698. * @eq: The queue structure associated with the queue to destroy.
  12699. *
  12700. * This function destroys a queue, as detailed in @eq by sending an mailbox
  12701. * command, specific to the type of queue, to the HBA.
  12702. *
  12703. * The @eq struct is used to get the queue ID of the queue to destroy.
  12704. *
  12705. * On success this function will return a zero. If the queue destroy mailbox
  12706. * command fails this function will return -ENXIO.
  12707. **/
  12708. int
  12709. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  12710. {
  12711. LPFC_MBOXQ_t *mbox;
  12712. int rc, length, status = 0;
  12713. uint32_t shdr_status, shdr_add_status;
  12714. union lpfc_sli4_cfg_shdr *shdr;
  12715. /* sanity check on queue memory */
  12716. if (!eq)
  12717. return -ENODEV;
  12718. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  12719. if (!mbox)
  12720. return -ENOMEM;
  12721. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  12722. sizeof(struct lpfc_sli4_cfg_mhdr));
  12723. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12724. LPFC_MBOX_OPCODE_EQ_DESTROY,
  12725. length, LPFC_SLI4_MBX_EMBED);
  12726. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  12727. eq->queue_id);
  12728. mbox->vport = eq->phba->pport;
  12729. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12730. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  12731. /* The IOCTL status is embedded in the mailbox subheader. */
  12732. shdr = (union lpfc_sli4_cfg_shdr *)
  12733. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  12734. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12735. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12736. if (shdr_status || shdr_add_status || rc) {
  12737. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12738. "2505 EQ_DESTROY mailbox failed with "
  12739. "status x%x add_status x%x, mbx status x%x\n",
  12740. shdr_status, shdr_add_status, rc);
  12741. status = -ENXIO;
  12742. }
  12743. /* Remove eq from any list */
  12744. list_del_init(&eq->list);
  12745. mempool_free(mbox, eq->phba->mbox_mem_pool);
  12746. return status;
  12747. }
  12748. /**
  12749. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  12750. * @cq: The queue structure associated with the queue to destroy.
  12751. *
  12752. * This function destroys a queue, as detailed in @cq by sending an mailbox
  12753. * command, specific to the type of queue, to the HBA.
  12754. *
  12755. * The @cq struct is used to get the queue ID of the queue to destroy.
  12756. *
  12757. * On success this function will return a zero. If the queue destroy mailbox
  12758. * command fails this function will return -ENXIO.
  12759. **/
  12760. int
  12761. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  12762. {
  12763. LPFC_MBOXQ_t *mbox;
  12764. int rc, length, status = 0;
  12765. uint32_t shdr_status, shdr_add_status;
  12766. union lpfc_sli4_cfg_shdr *shdr;
  12767. /* sanity check on queue memory */
  12768. if (!cq)
  12769. return -ENODEV;
  12770. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  12771. if (!mbox)
  12772. return -ENOMEM;
  12773. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  12774. sizeof(struct lpfc_sli4_cfg_mhdr));
  12775. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12776. LPFC_MBOX_OPCODE_CQ_DESTROY,
  12777. length, LPFC_SLI4_MBX_EMBED);
  12778. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  12779. cq->queue_id);
  12780. mbox->vport = cq->phba->pport;
  12781. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12782. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  12783. /* The IOCTL status is embedded in the mailbox subheader. */
  12784. shdr = (union lpfc_sli4_cfg_shdr *)
  12785. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  12786. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12787. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12788. if (shdr_status || shdr_add_status || rc) {
  12789. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12790. "2506 CQ_DESTROY mailbox failed with "
  12791. "status x%x add_status x%x, mbx status x%x\n",
  12792. shdr_status, shdr_add_status, rc);
  12793. status = -ENXIO;
  12794. }
  12795. /* Remove cq from any list */
  12796. list_del_init(&cq->list);
  12797. mempool_free(mbox, cq->phba->mbox_mem_pool);
  12798. return status;
  12799. }
  12800. /**
  12801. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  12802. * @qm: The queue structure associated with the queue to destroy.
  12803. *
  12804. * This function destroys a queue, as detailed in @mq by sending an mailbox
  12805. * command, specific to the type of queue, to the HBA.
  12806. *
  12807. * The @mq struct is used to get the queue ID of the queue to destroy.
  12808. *
  12809. * On success this function will return a zero. If the queue destroy mailbox
  12810. * command fails this function will return -ENXIO.
  12811. **/
  12812. int
  12813. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  12814. {
  12815. LPFC_MBOXQ_t *mbox;
  12816. int rc, length, status = 0;
  12817. uint32_t shdr_status, shdr_add_status;
  12818. union lpfc_sli4_cfg_shdr *shdr;
  12819. /* sanity check on queue memory */
  12820. if (!mq)
  12821. return -ENODEV;
  12822. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  12823. if (!mbox)
  12824. return -ENOMEM;
  12825. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  12826. sizeof(struct lpfc_sli4_cfg_mhdr));
  12827. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12828. LPFC_MBOX_OPCODE_MQ_DESTROY,
  12829. length, LPFC_SLI4_MBX_EMBED);
  12830. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  12831. mq->queue_id);
  12832. mbox->vport = mq->phba->pport;
  12833. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12834. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  12835. /* The IOCTL status is embedded in the mailbox subheader. */
  12836. shdr = (union lpfc_sli4_cfg_shdr *)
  12837. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  12838. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12839. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12840. if (shdr_status || shdr_add_status || rc) {
  12841. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12842. "2507 MQ_DESTROY mailbox failed with "
  12843. "status x%x add_status x%x, mbx status x%x\n",
  12844. shdr_status, shdr_add_status, rc);
  12845. status = -ENXIO;
  12846. }
  12847. /* Remove mq from any list */
  12848. list_del_init(&mq->list);
  12849. mempool_free(mbox, mq->phba->mbox_mem_pool);
  12850. return status;
  12851. }
  12852. /**
  12853. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  12854. * @wq: The queue structure associated with the queue to destroy.
  12855. *
  12856. * This function destroys a queue, as detailed in @wq by sending an mailbox
  12857. * command, specific to the type of queue, to the HBA.
  12858. *
  12859. * The @wq struct is used to get the queue ID of the queue to destroy.
  12860. *
  12861. * On success this function will return a zero. If the queue destroy mailbox
  12862. * command fails this function will return -ENXIO.
  12863. **/
  12864. int
  12865. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  12866. {
  12867. LPFC_MBOXQ_t *mbox;
  12868. int rc, length, status = 0;
  12869. uint32_t shdr_status, shdr_add_status;
  12870. union lpfc_sli4_cfg_shdr *shdr;
  12871. /* sanity check on queue memory */
  12872. if (!wq)
  12873. return -ENODEV;
  12874. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  12875. if (!mbox)
  12876. return -ENOMEM;
  12877. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  12878. sizeof(struct lpfc_sli4_cfg_mhdr));
  12879. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12880. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  12881. length, LPFC_SLI4_MBX_EMBED);
  12882. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  12883. wq->queue_id);
  12884. mbox->vport = wq->phba->pport;
  12885. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12886. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  12887. shdr = (union lpfc_sli4_cfg_shdr *)
  12888. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  12889. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12890. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12891. if (shdr_status || shdr_add_status || rc) {
  12892. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12893. "2508 WQ_DESTROY mailbox failed with "
  12894. "status x%x add_status x%x, mbx status x%x\n",
  12895. shdr_status, shdr_add_status, rc);
  12896. status = -ENXIO;
  12897. }
  12898. /* Remove wq from any list */
  12899. list_del_init(&wq->list);
  12900. mempool_free(mbox, wq->phba->mbox_mem_pool);
  12901. return status;
  12902. }
  12903. /**
  12904. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  12905. * @rq: The queue structure associated with the queue to destroy.
  12906. *
  12907. * This function destroys a queue, as detailed in @rq by sending an mailbox
  12908. * command, specific to the type of queue, to the HBA.
  12909. *
  12910. * The @rq struct is used to get the queue ID of the queue to destroy.
  12911. *
  12912. * On success this function will return a zero. If the queue destroy mailbox
  12913. * command fails this function will return -ENXIO.
  12914. **/
  12915. int
  12916. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  12917. struct lpfc_queue *drq)
  12918. {
  12919. LPFC_MBOXQ_t *mbox;
  12920. int rc, length, status = 0;
  12921. uint32_t shdr_status, shdr_add_status;
  12922. union lpfc_sli4_cfg_shdr *shdr;
  12923. /* sanity check on queue memory */
  12924. if (!hrq || !drq)
  12925. return -ENODEV;
  12926. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  12927. if (!mbox)
  12928. return -ENOMEM;
  12929. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  12930. sizeof(struct lpfc_sli4_cfg_mhdr));
  12931. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12932. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  12933. length, LPFC_SLI4_MBX_EMBED);
  12934. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12935. hrq->queue_id);
  12936. mbox->vport = hrq->phba->pport;
  12937. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12938. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  12939. /* The IOCTL status is embedded in the mailbox subheader. */
  12940. shdr = (union lpfc_sli4_cfg_shdr *)
  12941. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12942. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12943. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12944. if (shdr_status || shdr_add_status || rc) {
  12945. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12946. "2509 RQ_DESTROY mailbox failed with "
  12947. "status x%x add_status x%x, mbx status x%x\n",
  12948. shdr_status, shdr_add_status, rc);
  12949. if (rc != MBX_TIMEOUT)
  12950. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12951. return -ENXIO;
  12952. }
  12953. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12954. drq->queue_id);
  12955. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  12956. shdr = (union lpfc_sli4_cfg_shdr *)
  12957. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12958. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12959. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12960. if (shdr_status || shdr_add_status || rc) {
  12961. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12962. "2510 RQ_DESTROY mailbox failed with "
  12963. "status x%x add_status x%x, mbx status x%x\n",
  12964. shdr_status, shdr_add_status, rc);
  12965. status = -ENXIO;
  12966. }
  12967. list_del_init(&hrq->list);
  12968. list_del_init(&drq->list);
  12969. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12970. return status;
  12971. }
  12972. /**
  12973. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  12974. * @phba: The virtual port for which this call being executed.
  12975. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  12976. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  12977. * @xritag: the xritag that ties this io to the SGL pages.
  12978. *
  12979. * This routine will post the sgl pages for the IO that has the xritag
  12980. * that is in the iocbq structure. The xritag is assigned during iocbq
  12981. * creation and persists for as long as the driver is loaded.
  12982. * if the caller has fewer than 256 scatter gather segments to map then
  12983. * pdma_phys_addr1 should be 0.
  12984. * If the caller needs to map more than 256 scatter gather segment then
  12985. * pdma_phys_addr1 should be a valid physical address.
  12986. * physical address for SGLs must be 64 byte aligned.
  12987. * If you are going to map 2 SGL's then the first one must have 256 entries
  12988. * the second sgl can have between 1 and 256 entries.
  12989. *
  12990. * Return codes:
  12991. * 0 - Success
  12992. * -ENXIO, -ENOMEM - Failure
  12993. **/
  12994. int
  12995. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  12996. dma_addr_t pdma_phys_addr0,
  12997. dma_addr_t pdma_phys_addr1,
  12998. uint16_t xritag)
  12999. {
  13000. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  13001. LPFC_MBOXQ_t *mbox;
  13002. int rc;
  13003. uint32_t shdr_status, shdr_add_status;
  13004. uint32_t mbox_tmo;
  13005. union lpfc_sli4_cfg_shdr *shdr;
  13006. if (xritag == NO_XRI) {
  13007. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13008. "0364 Invalid param:\n");
  13009. return -EINVAL;
  13010. }
  13011. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13012. if (!mbox)
  13013. return -ENOMEM;
  13014. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  13015. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  13016. sizeof(struct lpfc_mbx_post_sgl_pages) -
  13017. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  13018. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  13019. &mbox->u.mqe.un.post_sgl_pages;
  13020. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  13021. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  13022. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  13023. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  13024. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  13025. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  13026. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  13027. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  13028. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  13029. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  13030. if (!phba->sli4_hba.intr_enable)
  13031. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13032. else {
  13033. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  13034. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  13035. }
  13036. /* The IOCTL status is embedded in the mailbox subheader. */
  13037. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  13038. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13039. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13040. if (rc != MBX_TIMEOUT)
  13041. mempool_free(mbox, phba->mbox_mem_pool);
  13042. if (shdr_status || shdr_add_status || rc) {
  13043. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13044. "2511 POST_SGL mailbox failed with "
  13045. "status x%x add_status x%x, mbx status x%x\n",
  13046. shdr_status, shdr_add_status, rc);
  13047. }
  13048. return 0;
  13049. }
  13050. /**
  13051. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  13052. * @phba: pointer to lpfc hba data structure.
  13053. *
  13054. * This routine is invoked to post rpi header templates to the
  13055. * HBA consistent with the SLI-4 interface spec. This routine
  13056. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13057. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13058. *
  13059. * Returns
  13060. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13061. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13062. **/
  13063. static uint16_t
  13064. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  13065. {
  13066. unsigned long xri;
  13067. /*
  13068. * Fetch the next logical xri. Because this index is logical,
  13069. * the driver starts at 0 each time.
  13070. */
  13071. spin_lock_irq(&phba->hbalock);
  13072. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  13073. phba->sli4_hba.max_cfg_param.max_xri, 0);
  13074. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  13075. spin_unlock_irq(&phba->hbalock);
  13076. return NO_XRI;
  13077. } else {
  13078. set_bit(xri, phba->sli4_hba.xri_bmask);
  13079. phba->sli4_hba.max_cfg_param.xri_used++;
  13080. }
  13081. spin_unlock_irq(&phba->hbalock);
  13082. return xri;
  13083. }
  13084. /**
  13085. * lpfc_sli4_free_xri - Release an xri for reuse.
  13086. * @phba: pointer to lpfc hba data structure.
  13087. *
  13088. * This routine is invoked to release an xri to the pool of
  13089. * available rpis maintained by the driver.
  13090. **/
  13091. static void
  13092. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  13093. {
  13094. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  13095. phba->sli4_hba.max_cfg_param.xri_used--;
  13096. }
  13097. }
  13098. /**
  13099. * lpfc_sli4_free_xri - Release an xri for reuse.
  13100. * @phba: pointer to lpfc hba data structure.
  13101. *
  13102. * This routine is invoked to release an xri to the pool of
  13103. * available rpis maintained by the driver.
  13104. **/
  13105. void
  13106. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  13107. {
  13108. spin_lock_irq(&phba->hbalock);
  13109. __lpfc_sli4_free_xri(phba, xri);
  13110. spin_unlock_irq(&phba->hbalock);
  13111. }
  13112. /**
  13113. * lpfc_sli4_next_xritag - Get an xritag for the io
  13114. * @phba: Pointer to HBA context object.
  13115. *
  13116. * This function gets an xritag for the iocb. If there is no unused xritag
  13117. * it will return 0xffff.
  13118. * The function returns the allocated xritag if successful, else returns zero.
  13119. * Zero is not a valid xritag.
  13120. * The caller is not required to hold any lock.
  13121. **/
  13122. uint16_t
  13123. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  13124. {
  13125. uint16_t xri_index;
  13126. xri_index = lpfc_sli4_alloc_xri(phba);
  13127. if (xri_index == NO_XRI)
  13128. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  13129. "2004 Failed to allocate XRI.last XRITAG is %d"
  13130. " Max XRI is %d, Used XRI is %d\n",
  13131. xri_index,
  13132. phba->sli4_hba.max_cfg_param.max_xri,
  13133. phba->sli4_hba.max_cfg_param.xri_used);
  13134. return xri_index;
  13135. }
  13136. /**
  13137. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  13138. * @phba: pointer to lpfc hba data structure.
  13139. * @post_sgl_list: pointer to els sgl entry list.
  13140. * @count: number of els sgl entries on the list.
  13141. *
  13142. * This routine is invoked to post a block of driver's sgl pages to the
  13143. * HBA using non-embedded mailbox command. No Lock is held. This routine
  13144. * is only called when the driver is loading and after all IO has been
  13145. * stopped.
  13146. **/
  13147. static int
  13148. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
  13149. struct list_head *post_sgl_list,
  13150. int post_cnt)
  13151. {
  13152. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  13153. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  13154. struct sgl_page_pairs *sgl_pg_pairs;
  13155. void *viraddr;
  13156. LPFC_MBOXQ_t *mbox;
  13157. uint32_t reqlen, alloclen, pg_pairs;
  13158. uint32_t mbox_tmo;
  13159. uint16_t xritag_start = 0;
  13160. int rc = 0;
  13161. uint32_t shdr_status, shdr_add_status;
  13162. union lpfc_sli4_cfg_shdr *shdr;
  13163. reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
  13164. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  13165. if (reqlen > SLI4_PAGE_SIZE) {
  13166. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  13167. "2559 Block sgl registration required DMA "
  13168. "size (%d) great than a page\n", reqlen);
  13169. return -ENOMEM;
  13170. }
  13171. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13172. if (!mbox)
  13173. return -ENOMEM;
  13174. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13175. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  13176. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  13177. LPFC_SLI4_MBX_NEMBED);
  13178. if (alloclen < reqlen) {
  13179. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13180. "0285 Allocated DMA memory size (%d) is "
  13181. "less than the requested DMA memory "
  13182. "size (%d)\n", alloclen, reqlen);
  13183. lpfc_sli4_mbox_cmd_free(phba, mbox);
  13184. return -ENOMEM;
  13185. }
  13186. /* Set up the SGL pages in the non-embedded DMA pages */
  13187. viraddr = mbox->sge_array->addr[0];
  13188. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  13189. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  13190. pg_pairs = 0;
  13191. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  13192. /* Set up the sge entry */
  13193. sgl_pg_pairs->sgl_pg0_addr_lo =
  13194. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  13195. sgl_pg_pairs->sgl_pg0_addr_hi =
  13196. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  13197. sgl_pg_pairs->sgl_pg1_addr_lo =
  13198. cpu_to_le32(putPaddrLow(0));
  13199. sgl_pg_pairs->sgl_pg1_addr_hi =
  13200. cpu_to_le32(putPaddrHigh(0));
  13201. /* Keep the first xritag on the list */
  13202. if (pg_pairs == 0)
  13203. xritag_start = sglq_entry->sli4_xritag;
  13204. sgl_pg_pairs++;
  13205. pg_pairs++;
  13206. }
  13207. /* Complete initialization and perform endian conversion. */
  13208. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  13209. bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
  13210. sgl->word0 = cpu_to_le32(sgl->word0);
  13211. if (!phba->sli4_hba.intr_enable)
  13212. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13213. else {
  13214. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  13215. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  13216. }
  13217. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  13218. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13219. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13220. if (rc != MBX_TIMEOUT)
  13221. lpfc_sli4_mbox_cmd_free(phba, mbox);
  13222. if (shdr_status || shdr_add_status || rc) {
  13223. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13224. "2513 POST_SGL_BLOCK mailbox command failed "
  13225. "status x%x add_status x%x mbx status x%x\n",
  13226. shdr_status, shdr_add_status, rc);
  13227. rc = -ENXIO;
  13228. }
  13229. return rc;
  13230. }
  13231. /**
  13232. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  13233. * @phba: pointer to lpfc hba data structure.
  13234. * @sblist: pointer to scsi buffer list.
  13235. * @count: number of scsi buffers on the list.
  13236. *
  13237. * This routine is invoked to post a block of @count scsi sgl pages from a
  13238. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  13239. * No Lock is held.
  13240. *
  13241. **/
  13242. int
  13243. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  13244. struct list_head *sblist,
  13245. int count)
  13246. {
  13247. struct lpfc_scsi_buf *psb;
  13248. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  13249. struct sgl_page_pairs *sgl_pg_pairs;
  13250. void *viraddr;
  13251. LPFC_MBOXQ_t *mbox;
  13252. uint32_t reqlen, alloclen, pg_pairs;
  13253. uint32_t mbox_tmo;
  13254. uint16_t xritag_start = 0;
  13255. int rc = 0;
  13256. uint32_t shdr_status, shdr_add_status;
  13257. dma_addr_t pdma_phys_bpl1;
  13258. union lpfc_sli4_cfg_shdr *shdr;
  13259. /* Calculate the requested length of the dma memory */
  13260. reqlen = count * sizeof(struct sgl_page_pairs) +
  13261. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  13262. if (reqlen > SLI4_PAGE_SIZE) {
  13263. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  13264. "0217 Block sgl registration required DMA "
  13265. "size (%d) great than a page\n", reqlen);
  13266. return -ENOMEM;
  13267. }
  13268. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13269. if (!mbox) {
  13270. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13271. "0283 Failed to allocate mbox cmd memory\n");
  13272. return -ENOMEM;
  13273. }
  13274. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13275. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  13276. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  13277. LPFC_SLI4_MBX_NEMBED);
  13278. if (alloclen < reqlen) {
  13279. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13280. "2561 Allocated DMA memory size (%d) is "
  13281. "less than the requested DMA memory "
  13282. "size (%d)\n", alloclen, reqlen);
  13283. lpfc_sli4_mbox_cmd_free(phba, mbox);
  13284. return -ENOMEM;
  13285. }
  13286. /* Get the first SGE entry from the non-embedded DMA memory */
  13287. viraddr = mbox->sge_array->addr[0];
  13288. /* Set up the SGL pages in the non-embedded DMA pages */
  13289. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  13290. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  13291. pg_pairs = 0;
  13292. list_for_each_entry(psb, sblist, list) {
  13293. /* Set up the sge entry */
  13294. sgl_pg_pairs->sgl_pg0_addr_lo =
  13295. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  13296. sgl_pg_pairs->sgl_pg0_addr_hi =
  13297. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  13298. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  13299. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  13300. else
  13301. pdma_phys_bpl1 = 0;
  13302. sgl_pg_pairs->sgl_pg1_addr_lo =
  13303. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  13304. sgl_pg_pairs->sgl_pg1_addr_hi =
  13305. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  13306. /* Keep the first xritag on the list */
  13307. if (pg_pairs == 0)
  13308. xritag_start = psb->cur_iocbq.sli4_xritag;
  13309. sgl_pg_pairs++;
  13310. pg_pairs++;
  13311. }
  13312. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  13313. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  13314. /* Perform endian conversion if necessary */
  13315. sgl->word0 = cpu_to_le32(sgl->word0);
  13316. if (!phba->sli4_hba.intr_enable)
  13317. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13318. else {
  13319. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  13320. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  13321. }
  13322. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  13323. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13324. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13325. if (rc != MBX_TIMEOUT)
  13326. lpfc_sli4_mbox_cmd_free(phba, mbox);
  13327. if (shdr_status || shdr_add_status || rc) {
  13328. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13329. "2564 POST_SGL_BLOCK mailbox command failed "
  13330. "status x%x add_status x%x mbx status x%x\n",
  13331. shdr_status, shdr_add_status, rc);
  13332. rc = -ENXIO;
  13333. }
  13334. return rc;
  13335. }
  13336. /**
  13337. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  13338. * @phba: pointer to lpfc_hba struct that the frame was received on
  13339. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  13340. *
  13341. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  13342. * valid type of frame that the LPFC driver will handle. This function will
  13343. * return a zero if the frame is a valid frame or a non zero value when the
  13344. * frame does not pass the check.
  13345. **/
  13346. static int
  13347. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  13348. {
  13349. /* make rctl_names static to save stack space */
  13350. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  13351. char *type_names[] = FC_TYPE_NAMES_INIT;
  13352. struct fc_vft_header *fc_vft_hdr;
  13353. uint32_t *header = (uint32_t *) fc_hdr;
  13354. switch (fc_hdr->fh_r_ctl) {
  13355. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  13356. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  13357. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  13358. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  13359. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  13360. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  13361. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  13362. case FC_RCTL_DD_CMD_STATUS: /* command status */
  13363. case FC_RCTL_ELS_REQ: /* extended link services request */
  13364. case FC_RCTL_ELS_REP: /* extended link services reply */
  13365. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  13366. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  13367. case FC_RCTL_BA_NOP: /* basic link service NOP */
  13368. case FC_RCTL_BA_ABTS: /* basic link service abort */
  13369. case FC_RCTL_BA_RMC: /* remove connection */
  13370. case FC_RCTL_BA_ACC: /* basic accept */
  13371. case FC_RCTL_BA_RJT: /* basic reject */
  13372. case FC_RCTL_BA_PRMT:
  13373. case FC_RCTL_ACK_1: /* acknowledge_1 */
  13374. case FC_RCTL_ACK_0: /* acknowledge_0 */
  13375. case FC_RCTL_P_RJT: /* port reject */
  13376. case FC_RCTL_F_RJT: /* fabric reject */
  13377. case FC_RCTL_P_BSY: /* port busy */
  13378. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  13379. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  13380. case FC_RCTL_LCR: /* link credit reset */
  13381. case FC_RCTL_END: /* end */
  13382. break;
  13383. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  13384. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  13385. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  13386. return lpfc_fc_frame_check(phba, fc_hdr);
  13387. default:
  13388. goto drop;
  13389. }
  13390. switch (fc_hdr->fh_type) {
  13391. case FC_TYPE_BLS:
  13392. case FC_TYPE_ELS:
  13393. case FC_TYPE_FCP:
  13394. case FC_TYPE_CT:
  13395. break;
  13396. case FC_TYPE_IP:
  13397. case FC_TYPE_ILS:
  13398. default:
  13399. goto drop;
  13400. }
  13401. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  13402. "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
  13403. "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
  13404. rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
  13405. type_names[fc_hdr->fh_type], fc_hdr->fh_type,
  13406. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  13407. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  13408. be32_to_cpu(header[4]), be32_to_cpu(header[5]),
  13409. be32_to_cpu(header[6]));
  13410. return 0;
  13411. drop:
  13412. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  13413. "2539 Dropped frame rctl:%s type:%s\n",
  13414. rctl_names[fc_hdr->fh_r_ctl],
  13415. type_names[fc_hdr->fh_type]);
  13416. return 1;
  13417. }
  13418. /**
  13419. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  13420. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  13421. *
  13422. * This function processes the FC header to retrieve the VFI from the VF
  13423. * header, if one exists. This function will return the VFI if one exists
  13424. * or 0 if no VSAN Header exists.
  13425. **/
  13426. static uint32_t
  13427. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  13428. {
  13429. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  13430. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  13431. return 0;
  13432. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  13433. }
  13434. /**
  13435. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  13436. * @phba: Pointer to the HBA structure to search for the vport on
  13437. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  13438. * @fcfi: The FC Fabric ID that the frame came from
  13439. *
  13440. * This function searches the @phba for a vport that matches the content of the
  13441. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  13442. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  13443. * returns the matching vport pointer or NULL if unable to match frame to a
  13444. * vport.
  13445. **/
  13446. static struct lpfc_vport *
  13447. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  13448. uint16_t fcfi)
  13449. {
  13450. struct lpfc_vport **vports;
  13451. struct lpfc_vport *vport = NULL;
  13452. int i;
  13453. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  13454. fc_hdr->fh_d_id[1] << 8 |
  13455. fc_hdr->fh_d_id[2]);
  13456. if (did == Fabric_DID)
  13457. return phba->pport;
  13458. if ((phba->pport->fc_flag & FC_PT2PT) &&
  13459. !(phba->link_state == LPFC_HBA_READY))
  13460. return phba->pport;
  13461. vports = lpfc_create_vport_work_array(phba);
  13462. if (vports != NULL)
  13463. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  13464. if (phba->fcf.fcfi == fcfi &&
  13465. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  13466. vports[i]->fc_myDID == did) {
  13467. vport = vports[i];
  13468. break;
  13469. }
  13470. }
  13471. lpfc_destroy_vport_work_array(phba, vports);
  13472. return vport;
  13473. }
  13474. /**
  13475. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  13476. * @vport: The vport to work on.
  13477. *
  13478. * This function updates the receive sequence time stamp for this vport. The
  13479. * receive sequence time stamp indicates the time that the last frame of the
  13480. * the sequence that has been idle for the longest amount of time was received.
  13481. * the driver uses this time stamp to indicate if any received sequences have
  13482. * timed out.
  13483. **/
  13484. static void
  13485. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  13486. {
  13487. struct lpfc_dmabuf *h_buf;
  13488. struct hbq_dmabuf *dmabuf = NULL;
  13489. /* get the oldest sequence on the rcv list */
  13490. h_buf = list_get_first(&vport->rcv_buffer_list,
  13491. struct lpfc_dmabuf, list);
  13492. if (!h_buf)
  13493. return;
  13494. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13495. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  13496. }
  13497. /**
  13498. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  13499. * @vport: The vport that the received sequences were sent to.
  13500. *
  13501. * This function cleans up all outstanding received sequences. This is called
  13502. * by the driver when a link event or user action invalidates all the received
  13503. * sequences.
  13504. **/
  13505. void
  13506. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  13507. {
  13508. struct lpfc_dmabuf *h_buf, *hnext;
  13509. struct lpfc_dmabuf *d_buf, *dnext;
  13510. struct hbq_dmabuf *dmabuf = NULL;
  13511. /* start with the oldest sequence on the rcv list */
  13512. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  13513. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13514. list_del_init(&dmabuf->hbuf.list);
  13515. list_for_each_entry_safe(d_buf, dnext,
  13516. &dmabuf->dbuf.list, list) {
  13517. list_del_init(&d_buf->list);
  13518. lpfc_in_buf_free(vport->phba, d_buf);
  13519. }
  13520. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  13521. }
  13522. }
  13523. /**
  13524. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  13525. * @vport: The vport that the received sequences were sent to.
  13526. *
  13527. * This function determines whether any received sequences have timed out by
  13528. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  13529. * indicates that there is at least one timed out sequence this routine will
  13530. * go through the received sequences one at a time from most inactive to most
  13531. * active to determine which ones need to be cleaned up. Once it has determined
  13532. * that a sequence needs to be cleaned up it will simply free up the resources
  13533. * without sending an abort.
  13534. **/
  13535. void
  13536. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  13537. {
  13538. struct lpfc_dmabuf *h_buf, *hnext;
  13539. struct lpfc_dmabuf *d_buf, *dnext;
  13540. struct hbq_dmabuf *dmabuf = NULL;
  13541. unsigned long timeout;
  13542. int abort_count = 0;
  13543. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  13544. vport->rcv_buffer_time_stamp);
  13545. if (list_empty(&vport->rcv_buffer_list) ||
  13546. time_before(jiffies, timeout))
  13547. return;
  13548. /* start with the oldest sequence on the rcv list */
  13549. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  13550. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13551. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  13552. dmabuf->time_stamp);
  13553. if (time_before(jiffies, timeout))
  13554. break;
  13555. abort_count++;
  13556. list_del_init(&dmabuf->hbuf.list);
  13557. list_for_each_entry_safe(d_buf, dnext,
  13558. &dmabuf->dbuf.list, list) {
  13559. list_del_init(&d_buf->list);
  13560. lpfc_in_buf_free(vport->phba, d_buf);
  13561. }
  13562. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  13563. }
  13564. if (abort_count)
  13565. lpfc_update_rcv_time_stamp(vport);
  13566. }
  13567. /**
  13568. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  13569. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  13570. *
  13571. * This function searches through the existing incomplete sequences that have
  13572. * been sent to this @vport. If the frame matches one of the incomplete
  13573. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  13574. * make up that sequence. If no sequence is found that matches this frame then
  13575. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  13576. * This function returns a pointer to the first dmabuf in the sequence list that
  13577. * the frame was linked to.
  13578. **/
  13579. static struct hbq_dmabuf *
  13580. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  13581. {
  13582. struct fc_frame_header *new_hdr;
  13583. struct fc_frame_header *temp_hdr;
  13584. struct lpfc_dmabuf *d_buf;
  13585. struct lpfc_dmabuf *h_buf;
  13586. struct hbq_dmabuf *seq_dmabuf = NULL;
  13587. struct hbq_dmabuf *temp_dmabuf = NULL;
  13588. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  13589. dmabuf->time_stamp = jiffies;
  13590. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13591. /* Use the hdr_buf to find the sequence that this frame belongs to */
  13592. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  13593. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  13594. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  13595. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  13596. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  13597. continue;
  13598. /* found a pending sequence that matches this frame */
  13599. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13600. break;
  13601. }
  13602. if (!seq_dmabuf) {
  13603. /*
  13604. * This indicates first frame received for this sequence.
  13605. * Queue the buffer on the vport's rcv_buffer_list.
  13606. */
  13607. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  13608. lpfc_update_rcv_time_stamp(vport);
  13609. return dmabuf;
  13610. }
  13611. temp_hdr = seq_dmabuf->hbuf.virt;
  13612. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  13613. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  13614. list_del_init(&seq_dmabuf->hbuf.list);
  13615. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  13616. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  13617. lpfc_update_rcv_time_stamp(vport);
  13618. return dmabuf;
  13619. }
  13620. /* move this sequence to the tail to indicate a young sequence */
  13621. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  13622. seq_dmabuf->time_stamp = jiffies;
  13623. lpfc_update_rcv_time_stamp(vport);
  13624. if (list_empty(&seq_dmabuf->dbuf.list)) {
  13625. temp_hdr = dmabuf->hbuf.virt;
  13626. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  13627. return seq_dmabuf;
  13628. }
  13629. /* find the correct place in the sequence to insert this frame */
  13630. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  13631. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13632. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  13633. /*
  13634. * If the frame's sequence count is greater than the frame on
  13635. * the list then insert the frame right after this frame
  13636. */
  13637. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  13638. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  13639. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  13640. return seq_dmabuf;
  13641. }
  13642. }
  13643. return NULL;
  13644. }
  13645. /**
  13646. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  13647. * @vport: pointer to a vitural port
  13648. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13649. *
  13650. * This function tries to abort from the partially assembed sequence, described
  13651. * by the information from basic abbort @dmabuf. It checks to see whether such
  13652. * partially assembled sequence held by the driver. If so, it shall free up all
  13653. * the frames from the partially assembled sequence.
  13654. *
  13655. * Return
  13656. * true -- if there is matching partially assembled sequence present and all
  13657. * the frames freed with the sequence;
  13658. * false -- if there is no matching partially assembled sequence present so
  13659. * nothing got aborted in the lower layer driver
  13660. **/
  13661. static bool
  13662. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  13663. struct hbq_dmabuf *dmabuf)
  13664. {
  13665. struct fc_frame_header *new_hdr;
  13666. struct fc_frame_header *temp_hdr;
  13667. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  13668. struct hbq_dmabuf *seq_dmabuf = NULL;
  13669. /* Use the hdr_buf to find the sequence that matches this frame */
  13670. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  13671. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  13672. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13673. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  13674. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  13675. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  13676. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  13677. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  13678. continue;
  13679. /* found a pending sequence that matches this frame */
  13680. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13681. break;
  13682. }
  13683. /* Free up all the frames from the partially assembled sequence */
  13684. if (seq_dmabuf) {
  13685. list_for_each_entry_safe(d_buf, n_buf,
  13686. &seq_dmabuf->dbuf.list, list) {
  13687. list_del_init(&d_buf->list);
  13688. lpfc_in_buf_free(vport->phba, d_buf);
  13689. }
  13690. return true;
  13691. }
  13692. return false;
  13693. }
  13694. /**
  13695. * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
  13696. * @vport: pointer to a vitural port
  13697. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13698. *
  13699. * This function tries to abort from the assembed sequence from upper level
  13700. * protocol, described by the information from basic abbort @dmabuf. It
  13701. * checks to see whether such pending context exists at upper level protocol.
  13702. * If so, it shall clean up the pending context.
  13703. *
  13704. * Return
  13705. * true -- if there is matching pending context of the sequence cleaned
  13706. * at ulp;
  13707. * false -- if there is no matching pending context of the sequence present
  13708. * at ulp.
  13709. **/
  13710. static bool
  13711. lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  13712. {
  13713. struct lpfc_hba *phba = vport->phba;
  13714. int handled;
  13715. /* Accepting abort at ulp with SLI4 only */
  13716. if (phba->sli_rev < LPFC_SLI_REV4)
  13717. return false;
  13718. /* Register all caring upper level protocols to attend abort */
  13719. handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
  13720. if (handled)
  13721. return true;
  13722. return false;
  13723. }
  13724. /**
  13725. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  13726. * @phba: Pointer to HBA context object.
  13727. * @cmd_iocbq: pointer to the command iocbq structure.
  13728. * @rsp_iocbq: pointer to the response iocbq structure.
  13729. *
  13730. * This function handles the sequence abort response iocb command complete
  13731. * event. It properly releases the memory allocated to the sequence abort
  13732. * accept iocb.
  13733. **/
  13734. static void
  13735. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  13736. struct lpfc_iocbq *cmd_iocbq,
  13737. struct lpfc_iocbq *rsp_iocbq)
  13738. {
  13739. struct lpfc_nodelist *ndlp;
  13740. if (cmd_iocbq) {
  13741. ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
  13742. lpfc_nlp_put(ndlp);
  13743. lpfc_nlp_not_used(ndlp);
  13744. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  13745. }
  13746. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  13747. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  13748. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13749. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  13750. rsp_iocbq->iocb.ulpStatus,
  13751. rsp_iocbq->iocb.un.ulpWord[4]);
  13752. }
  13753. /**
  13754. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  13755. * @phba: Pointer to HBA context object.
  13756. * @xri: xri id in transaction.
  13757. *
  13758. * This function validates the xri maps to the known range of XRIs allocated an
  13759. * used by the driver.
  13760. **/
  13761. uint16_t
  13762. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  13763. uint16_t xri)
  13764. {
  13765. uint16_t i;
  13766. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  13767. if (xri == phba->sli4_hba.xri_ids[i])
  13768. return i;
  13769. }
  13770. return NO_XRI;
  13771. }
  13772. /**
  13773. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  13774. * @phba: Pointer to HBA context object.
  13775. * @fc_hdr: pointer to a FC frame header.
  13776. *
  13777. * This function sends a basic response to a previous unsol sequence abort
  13778. * event after aborting the sequence handling.
  13779. **/
  13780. static void
  13781. lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
  13782. struct fc_frame_header *fc_hdr, bool aborted)
  13783. {
  13784. struct lpfc_hba *phba = vport->phba;
  13785. struct lpfc_iocbq *ctiocb = NULL;
  13786. struct lpfc_nodelist *ndlp;
  13787. uint16_t oxid, rxid, xri, lxri;
  13788. uint32_t sid, fctl;
  13789. IOCB_t *icmd;
  13790. int rc;
  13791. if (!lpfc_is_link_up(phba))
  13792. return;
  13793. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13794. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  13795. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  13796. ndlp = lpfc_findnode_did(vport, sid);
  13797. if (!ndlp) {
  13798. ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
  13799. if (!ndlp) {
  13800. lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
  13801. "1268 Failed to allocate ndlp for "
  13802. "oxid:x%x SID:x%x\n", oxid, sid);
  13803. return;
  13804. }
  13805. lpfc_nlp_init(vport, ndlp, sid);
  13806. /* Put ndlp onto pport node list */
  13807. lpfc_enqueue_node(vport, ndlp);
  13808. } else if (!NLP_CHK_NODE_ACT(ndlp)) {
  13809. /* re-setup ndlp without removing from node list */
  13810. ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
  13811. if (!ndlp) {
  13812. lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
  13813. "3275 Failed to active ndlp found "
  13814. "for oxid:x%x SID:x%x\n", oxid, sid);
  13815. return;
  13816. }
  13817. }
  13818. /* Allocate buffer for rsp iocb */
  13819. ctiocb = lpfc_sli_get_iocbq(phba);
  13820. if (!ctiocb)
  13821. return;
  13822. /* Extract the F_CTL field from FC_HDR */
  13823. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  13824. icmd = &ctiocb->iocb;
  13825. icmd->un.xseq64.bdl.bdeSize = 0;
  13826. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  13827. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  13828. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  13829. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  13830. /* Fill in the rest of iocb fields */
  13831. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  13832. icmd->ulpBdeCount = 0;
  13833. icmd->ulpLe = 1;
  13834. icmd->ulpClass = CLASS3;
  13835. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  13836. ctiocb->context1 = lpfc_nlp_get(ndlp);
  13837. ctiocb->iocb_cmpl = NULL;
  13838. ctiocb->vport = phba->pport;
  13839. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  13840. ctiocb->sli4_lxritag = NO_XRI;
  13841. ctiocb->sli4_xritag = NO_XRI;
  13842. if (fctl & FC_FC_EX_CTX)
  13843. /* Exchange responder sent the abort so we
  13844. * own the oxid.
  13845. */
  13846. xri = oxid;
  13847. else
  13848. xri = rxid;
  13849. lxri = lpfc_sli4_xri_inrange(phba, xri);
  13850. if (lxri != NO_XRI)
  13851. lpfc_set_rrq_active(phba, ndlp, lxri,
  13852. (xri == oxid) ? rxid : oxid, 0);
  13853. /* For BA_ABTS from exchange responder, if the logical xri with
  13854. * the oxid maps to the FCP XRI range, the port no longer has
  13855. * that exchange context, send a BLS_RJT. Override the IOCB for
  13856. * a BA_RJT.
  13857. */
  13858. if ((fctl & FC_FC_EX_CTX) &&
  13859. (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
  13860. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  13861. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  13862. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  13863. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  13864. }
  13865. /* If BA_ABTS failed to abort a partially assembled receive sequence,
  13866. * the driver no longer has that exchange, send a BLS_RJT. Override
  13867. * the IOCB for a BA_RJT.
  13868. */
  13869. if (aborted == false) {
  13870. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  13871. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  13872. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  13873. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  13874. }
  13875. if (fctl & FC_FC_EX_CTX) {
  13876. /* ABTS sent by responder to CT exchange, construction
  13877. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  13878. * field and RX_ID from ABTS for RX_ID field.
  13879. */
  13880. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  13881. } else {
  13882. /* ABTS sent by initiator to CT exchange, construction
  13883. * of BA_ACC will need to allocate a new XRI as for the
  13884. * XRI_TAG field.
  13885. */
  13886. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  13887. }
  13888. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  13889. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  13890. /* Xmit CT abts response on exchange <xid> */
  13891. lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
  13892. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  13893. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  13894. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  13895. if (rc == IOCB_ERROR) {
  13896. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  13897. "2925 Failed to issue CT ABTS RSP x%x on "
  13898. "xri x%x, Data x%x\n",
  13899. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  13900. phba->link_state);
  13901. lpfc_nlp_put(ndlp);
  13902. ctiocb->context1 = NULL;
  13903. lpfc_sli_release_iocbq(phba, ctiocb);
  13904. }
  13905. }
  13906. /**
  13907. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  13908. * @vport: Pointer to the vport on which this sequence was received
  13909. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13910. *
  13911. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  13912. * receive sequence is only partially assembed by the driver, it shall abort
  13913. * the partially assembled frames for the sequence. Otherwise, if the
  13914. * unsolicited receive sequence has been completely assembled and passed to
  13915. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  13916. * unsolicited sequence has been aborted. After that, it will issue a basic
  13917. * accept to accept the abort.
  13918. **/
  13919. static void
  13920. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  13921. struct hbq_dmabuf *dmabuf)
  13922. {
  13923. struct lpfc_hba *phba = vport->phba;
  13924. struct fc_frame_header fc_hdr;
  13925. uint32_t fctl;
  13926. bool aborted;
  13927. /* Make a copy of fc_hdr before the dmabuf being released */
  13928. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  13929. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  13930. if (fctl & FC_FC_EX_CTX) {
  13931. /* ABTS by responder to exchange, no cleanup needed */
  13932. aborted = true;
  13933. } else {
  13934. /* ABTS by initiator to exchange, need to do cleanup */
  13935. aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  13936. if (aborted == false)
  13937. aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
  13938. }
  13939. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13940. /* Respond with BA_ACC or BA_RJT accordingly */
  13941. lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
  13942. }
  13943. /**
  13944. * lpfc_seq_complete - Indicates if a sequence is complete
  13945. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13946. *
  13947. * This function checks the sequence, starting with the frame described by
  13948. * @dmabuf, to see if all the frames associated with this sequence are present.
  13949. * the frames associated with this sequence are linked to the @dmabuf using the
  13950. * dbuf list. This function looks for two major things. 1) That the first frame
  13951. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  13952. * set. 3) That there are no holes in the sequence count. The function will
  13953. * return 1 when the sequence is complete, otherwise it will return 0.
  13954. **/
  13955. static int
  13956. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  13957. {
  13958. struct fc_frame_header *hdr;
  13959. struct lpfc_dmabuf *d_buf;
  13960. struct hbq_dmabuf *seq_dmabuf;
  13961. uint32_t fctl;
  13962. int seq_count = 0;
  13963. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13964. /* make sure first fame of sequence has a sequence count of zero */
  13965. if (hdr->fh_seq_cnt != seq_count)
  13966. return 0;
  13967. fctl = (hdr->fh_f_ctl[0] << 16 |
  13968. hdr->fh_f_ctl[1] << 8 |
  13969. hdr->fh_f_ctl[2]);
  13970. /* If last frame of sequence we can return success. */
  13971. if (fctl & FC_FC_END_SEQ)
  13972. return 1;
  13973. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  13974. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13975. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13976. /* If there is a hole in the sequence count then fail. */
  13977. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  13978. return 0;
  13979. fctl = (hdr->fh_f_ctl[0] << 16 |
  13980. hdr->fh_f_ctl[1] << 8 |
  13981. hdr->fh_f_ctl[2]);
  13982. /* If last frame of sequence we can return success. */
  13983. if (fctl & FC_FC_END_SEQ)
  13984. return 1;
  13985. }
  13986. return 0;
  13987. }
  13988. /**
  13989. * lpfc_prep_seq - Prep sequence for ULP processing
  13990. * @vport: Pointer to the vport on which this sequence was received
  13991. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13992. *
  13993. * This function takes a sequence, described by a list of frames, and creates
  13994. * a list of iocbq structures to describe the sequence. This iocbq list will be
  13995. * used to issue to the generic unsolicited sequence handler. This routine
  13996. * returns a pointer to the first iocbq in the list. If the function is unable
  13997. * to allocate an iocbq then it throw out the received frames that were not
  13998. * able to be described and return a pointer to the first iocbq. If unable to
  13999. * allocate any iocbqs (including the first) this function will return NULL.
  14000. **/
  14001. static struct lpfc_iocbq *
  14002. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  14003. {
  14004. struct hbq_dmabuf *hbq_buf;
  14005. struct lpfc_dmabuf *d_buf, *n_buf;
  14006. struct lpfc_iocbq *first_iocbq, *iocbq;
  14007. struct fc_frame_header *fc_hdr;
  14008. uint32_t sid;
  14009. uint32_t len, tot_len;
  14010. struct ulp_bde64 *pbde;
  14011. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  14012. /* remove from receive buffer list */
  14013. list_del_init(&seq_dmabuf->hbuf.list);
  14014. lpfc_update_rcv_time_stamp(vport);
  14015. /* get the Remote Port's SID */
  14016. sid = sli4_sid_from_fc_hdr(fc_hdr);
  14017. tot_len = 0;
  14018. /* Get an iocbq struct to fill in. */
  14019. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  14020. if (first_iocbq) {
  14021. /* Initialize the first IOCB. */
  14022. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  14023. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  14024. /* Check FC Header to see what TYPE of frame we are rcv'ing */
  14025. if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
  14026. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
  14027. first_iocbq->iocb.un.rcvels.parmRo =
  14028. sli4_did_from_fc_hdr(fc_hdr);
  14029. first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
  14030. } else
  14031. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  14032. first_iocbq->iocb.ulpContext = NO_XRI;
  14033. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  14034. be16_to_cpu(fc_hdr->fh_ox_id);
  14035. /* iocbq is prepped for internal consumption. Physical vpi. */
  14036. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  14037. vport->phba->vpi_ids[vport->vpi];
  14038. /* put the first buffer into the first IOCBq */
  14039. tot_len = bf_get(lpfc_rcqe_length,
  14040. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  14041. first_iocbq->context2 = &seq_dmabuf->dbuf;
  14042. first_iocbq->context3 = NULL;
  14043. first_iocbq->iocb.ulpBdeCount = 1;
  14044. if (tot_len > LPFC_DATA_BUF_SIZE)
  14045. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  14046. LPFC_DATA_BUF_SIZE;
  14047. else
  14048. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
  14049. first_iocbq->iocb.un.rcvels.remoteID = sid;
  14050. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  14051. }
  14052. iocbq = first_iocbq;
  14053. /*
  14054. * Each IOCBq can have two Buffers assigned, so go through the list
  14055. * of buffers for this sequence and save two buffers in each IOCBq
  14056. */
  14057. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  14058. if (!iocbq) {
  14059. lpfc_in_buf_free(vport->phba, d_buf);
  14060. continue;
  14061. }
  14062. if (!iocbq->context3) {
  14063. iocbq->context3 = d_buf;
  14064. iocbq->iocb.ulpBdeCount++;
  14065. /* We need to get the size out of the right CQE */
  14066. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  14067. len = bf_get(lpfc_rcqe_length,
  14068. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  14069. pbde = (struct ulp_bde64 *)
  14070. &iocbq->iocb.unsli3.sli3Words[4];
  14071. if (len > LPFC_DATA_BUF_SIZE)
  14072. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  14073. else
  14074. pbde->tus.f.bdeSize = len;
  14075. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  14076. tot_len += len;
  14077. } else {
  14078. iocbq = lpfc_sli_get_iocbq(vport->phba);
  14079. if (!iocbq) {
  14080. if (first_iocbq) {
  14081. first_iocbq->iocb.ulpStatus =
  14082. IOSTAT_FCP_RSP_ERROR;
  14083. first_iocbq->iocb.un.ulpWord[4] =
  14084. IOERR_NO_RESOURCES;
  14085. }
  14086. lpfc_in_buf_free(vport->phba, d_buf);
  14087. continue;
  14088. }
  14089. /* We need to get the size out of the right CQE */
  14090. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  14091. len = bf_get(lpfc_rcqe_length,
  14092. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  14093. iocbq->context2 = d_buf;
  14094. iocbq->context3 = NULL;
  14095. iocbq->iocb.ulpBdeCount = 1;
  14096. if (len > LPFC_DATA_BUF_SIZE)
  14097. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  14098. LPFC_DATA_BUF_SIZE;
  14099. else
  14100. iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
  14101. tot_len += len;
  14102. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  14103. iocbq->iocb.un.rcvels.remoteID = sid;
  14104. list_add_tail(&iocbq->list, &first_iocbq->list);
  14105. }
  14106. }
  14107. return first_iocbq;
  14108. }
  14109. static void
  14110. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  14111. struct hbq_dmabuf *seq_dmabuf)
  14112. {
  14113. struct fc_frame_header *fc_hdr;
  14114. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  14115. struct lpfc_hba *phba = vport->phba;
  14116. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  14117. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  14118. if (!iocbq) {
  14119. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14120. "2707 Ring %d handler: Failed to allocate "
  14121. "iocb Rctl x%x Type x%x received\n",
  14122. LPFC_ELS_RING,
  14123. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  14124. return;
  14125. }
  14126. if (!lpfc_complete_unsol_iocb(phba,
  14127. &phba->sli.ring[LPFC_ELS_RING],
  14128. iocbq, fc_hdr->fh_r_ctl,
  14129. fc_hdr->fh_type))
  14130. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14131. "2540 Ring %d handler: unexpected Rctl "
  14132. "x%x Type x%x received\n",
  14133. LPFC_ELS_RING,
  14134. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  14135. /* Free iocb created in lpfc_prep_seq */
  14136. list_for_each_entry_safe(curr_iocb, next_iocb,
  14137. &iocbq->list, list) {
  14138. list_del_init(&curr_iocb->list);
  14139. lpfc_sli_release_iocbq(phba, curr_iocb);
  14140. }
  14141. lpfc_sli_release_iocbq(phba, iocbq);
  14142. }
  14143. /**
  14144. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  14145. * @phba: Pointer to HBA context object.
  14146. *
  14147. * This function is called with no lock held. This function processes all
  14148. * the received buffers and gives it to upper layers when a received buffer
  14149. * indicates that it is the final frame in the sequence. The interrupt
  14150. * service routine processes received buffers at interrupt contexts and adds
  14151. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  14152. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  14153. * appropriate receive function when the final frame in a sequence is received.
  14154. **/
  14155. void
  14156. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  14157. struct hbq_dmabuf *dmabuf)
  14158. {
  14159. struct hbq_dmabuf *seq_dmabuf;
  14160. struct fc_frame_header *fc_hdr;
  14161. struct lpfc_vport *vport;
  14162. uint32_t fcfi;
  14163. uint32_t did;
  14164. /* Process each received buffer */
  14165. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  14166. /* check to see if this a valid type of frame */
  14167. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  14168. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  14169. return;
  14170. }
  14171. if ((bf_get(lpfc_cqe_code,
  14172. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  14173. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  14174. &dmabuf->cq_event.cqe.rcqe_cmpl);
  14175. else
  14176. fcfi = bf_get(lpfc_rcqe_fcf_id,
  14177. &dmabuf->cq_event.cqe.rcqe_cmpl);
  14178. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  14179. if (!vport) {
  14180. /* throw out the frame */
  14181. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  14182. return;
  14183. }
  14184. /* d_id this frame is directed to */
  14185. did = sli4_did_from_fc_hdr(fc_hdr);
  14186. /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
  14187. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
  14188. (did != Fabric_DID)) {
  14189. /*
  14190. * Throw out the frame if we are not pt2pt.
  14191. * The pt2pt protocol allows for discovery frames
  14192. * to be received without a registered VPI.
  14193. */
  14194. if (!(vport->fc_flag & FC_PT2PT) ||
  14195. (phba->link_state == LPFC_HBA_READY)) {
  14196. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  14197. return;
  14198. }
  14199. }
  14200. /* Handle the basic abort sequence (BA_ABTS) event */
  14201. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  14202. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  14203. return;
  14204. }
  14205. /* Link this frame */
  14206. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  14207. if (!seq_dmabuf) {
  14208. /* unable to add frame to vport - throw it out */
  14209. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  14210. return;
  14211. }
  14212. /* If not last frame in sequence continue processing frames. */
  14213. if (!lpfc_seq_complete(seq_dmabuf))
  14214. return;
  14215. /* Send the complete sequence to the upper layer protocol */
  14216. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  14217. }
  14218. /**
  14219. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  14220. * @phba: pointer to lpfc hba data structure.
  14221. *
  14222. * This routine is invoked to post rpi header templates to the
  14223. * HBA consistent with the SLI-4 interface spec. This routine
  14224. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  14225. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  14226. *
  14227. * This routine does not require any locks. It's usage is expected
  14228. * to be driver load or reset recovery when the driver is
  14229. * sequential.
  14230. *
  14231. * Return codes
  14232. * 0 - successful
  14233. * -EIO - The mailbox failed to complete successfully.
  14234. * When this error occurs, the driver is not guaranteed
  14235. * to have any rpi regions posted to the device and
  14236. * must either attempt to repost the regions or take a
  14237. * fatal error.
  14238. **/
  14239. int
  14240. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  14241. {
  14242. struct lpfc_rpi_hdr *rpi_page;
  14243. uint32_t rc = 0;
  14244. uint16_t lrpi = 0;
  14245. /* SLI4 ports that support extents do not require RPI headers. */
  14246. if (!phba->sli4_hba.rpi_hdrs_in_use)
  14247. goto exit;
  14248. if (phba->sli4_hba.extents_in_use)
  14249. return -EIO;
  14250. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  14251. /*
  14252. * Assign the rpi headers a physical rpi only if the driver
  14253. * has not initialized those resources. A port reset only
  14254. * needs the headers posted.
  14255. */
  14256. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  14257. LPFC_RPI_RSRC_RDY)
  14258. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  14259. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  14260. if (rc != MBX_SUCCESS) {
  14261. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14262. "2008 Error %d posting all rpi "
  14263. "headers\n", rc);
  14264. rc = -EIO;
  14265. break;
  14266. }
  14267. }
  14268. exit:
  14269. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  14270. LPFC_RPI_RSRC_RDY);
  14271. return rc;
  14272. }
  14273. /**
  14274. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  14275. * @phba: pointer to lpfc hba data structure.
  14276. * @rpi_page: pointer to the rpi memory region.
  14277. *
  14278. * This routine is invoked to post a single rpi header to the
  14279. * HBA consistent with the SLI-4 interface spec. This memory region
  14280. * maps up to 64 rpi context regions.
  14281. *
  14282. * Return codes
  14283. * 0 - successful
  14284. * -ENOMEM - No available memory
  14285. * -EIO - The mailbox failed to complete successfully.
  14286. **/
  14287. int
  14288. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  14289. {
  14290. LPFC_MBOXQ_t *mboxq;
  14291. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  14292. uint32_t rc = 0;
  14293. uint32_t shdr_status, shdr_add_status;
  14294. union lpfc_sli4_cfg_shdr *shdr;
  14295. /* SLI4 ports that support extents do not require RPI headers. */
  14296. if (!phba->sli4_hba.rpi_hdrs_in_use)
  14297. return rc;
  14298. if (phba->sli4_hba.extents_in_use)
  14299. return -EIO;
  14300. /* The port is notified of the header region via a mailbox command. */
  14301. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14302. if (!mboxq) {
  14303. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14304. "2001 Unable to allocate memory for issuing "
  14305. "SLI_CONFIG_SPECIAL mailbox command\n");
  14306. return -ENOMEM;
  14307. }
  14308. /* Post all rpi memory regions to the port. */
  14309. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  14310. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  14311. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  14312. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  14313. sizeof(struct lpfc_sli4_cfg_mhdr),
  14314. LPFC_SLI4_MBX_EMBED);
  14315. /* Post the physical rpi to the port for this rpi header. */
  14316. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  14317. rpi_page->start_rpi);
  14318. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  14319. hdr_tmpl, rpi_page->page_count);
  14320. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  14321. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  14322. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14323. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  14324. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14325. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14326. if (rc != MBX_TIMEOUT)
  14327. mempool_free(mboxq, phba->mbox_mem_pool);
  14328. if (shdr_status || shdr_add_status || rc) {
  14329. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14330. "2514 POST_RPI_HDR mailbox failed with "
  14331. "status x%x add_status x%x, mbx status x%x\n",
  14332. shdr_status, shdr_add_status, rc);
  14333. rc = -ENXIO;
  14334. }
  14335. return rc;
  14336. }
  14337. /**
  14338. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  14339. * @phba: pointer to lpfc hba data structure.
  14340. *
  14341. * This routine is invoked to post rpi header templates to the
  14342. * HBA consistent with the SLI-4 interface spec. This routine
  14343. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  14344. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  14345. *
  14346. * Returns
  14347. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  14348. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  14349. **/
  14350. int
  14351. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  14352. {
  14353. unsigned long rpi;
  14354. uint16_t max_rpi, rpi_limit;
  14355. uint16_t rpi_remaining, lrpi = 0;
  14356. struct lpfc_rpi_hdr *rpi_hdr;
  14357. unsigned long iflag;
  14358. /*
  14359. * Fetch the next logical rpi. Because this index is logical,
  14360. * the driver starts at 0 each time.
  14361. */
  14362. spin_lock_irqsave(&phba->hbalock, iflag);
  14363. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  14364. rpi_limit = phba->sli4_hba.next_rpi;
  14365. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  14366. if (rpi >= rpi_limit)
  14367. rpi = LPFC_RPI_ALLOC_ERROR;
  14368. else {
  14369. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  14370. phba->sli4_hba.max_cfg_param.rpi_used++;
  14371. phba->sli4_hba.rpi_count++;
  14372. }
  14373. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  14374. "0001 rpi:%x max:%x lim:%x\n",
  14375. (int) rpi, max_rpi, rpi_limit);
  14376. /*
  14377. * Don't try to allocate more rpi header regions if the device limit
  14378. * has been exhausted.
  14379. */
  14380. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  14381. (phba->sli4_hba.rpi_count >= max_rpi)) {
  14382. spin_unlock_irqrestore(&phba->hbalock, iflag);
  14383. return rpi;
  14384. }
  14385. /*
  14386. * RPI header postings are not required for SLI4 ports capable of
  14387. * extents.
  14388. */
  14389. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  14390. spin_unlock_irqrestore(&phba->hbalock, iflag);
  14391. return rpi;
  14392. }
  14393. /*
  14394. * If the driver is running low on rpi resources, allocate another
  14395. * page now. Note that the next_rpi value is used because
  14396. * it represents how many are actually in use whereas max_rpi notes
  14397. * how many are supported max by the device.
  14398. */
  14399. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  14400. spin_unlock_irqrestore(&phba->hbalock, iflag);
  14401. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  14402. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  14403. if (!rpi_hdr) {
  14404. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14405. "2002 Error Could not grow rpi "
  14406. "count\n");
  14407. } else {
  14408. lrpi = rpi_hdr->start_rpi;
  14409. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  14410. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  14411. }
  14412. }
  14413. return rpi;
  14414. }
  14415. /**
  14416. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  14417. * @phba: pointer to lpfc hba data structure.
  14418. *
  14419. * This routine is invoked to release an rpi to the pool of
  14420. * available rpis maintained by the driver.
  14421. **/
  14422. static void
  14423. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  14424. {
  14425. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  14426. phba->sli4_hba.rpi_count--;
  14427. phba->sli4_hba.max_cfg_param.rpi_used--;
  14428. }
  14429. }
  14430. /**
  14431. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  14432. * @phba: pointer to lpfc hba data structure.
  14433. *
  14434. * This routine is invoked to release an rpi to the pool of
  14435. * available rpis maintained by the driver.
  14436. **/
  14437. void
  14438. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  14439. {
  14440. spin_lock_irq(&phba->hbalock);
  14441. __lpfc_sli4_free_rpi(phba, rpi);
  14442. spin_unlock_irq(&phba->hbalock);
  14443. }
  14444. /**
  14445. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  14446. * @phba: pointer to lpfc hba data structure.
  14447. *
  14448. * This routine is invoked to remove the memory region that
  14449. * provided rpi via a bitmask.
  14450. **/
  14451. void
  14452. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  14453. {
  14454. kfree(phba->sli4_hba.rpi_bmask);
  14455. kfree(phba->sli4_hba.rpi_ids);
  14456. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  14457. }
  14458. /**
  14459. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  14460. * @phba: pointer to lpfc hba data structure.
  14461. *
  14462. * This routine is invoked to remove the memory region that
  14463. * provided rpi via a bitmask.
  14464. **/
  14465. int
  14466. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  14467. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  14468. {
  14469. LPFC_MBOXQ_t *mboxq;
  14470. struct lpfc_hba *phba = ndlp->phba;
  14471. int rc;
  14472. /* The port is notified of the header region via a mailbox command. */
  14473. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14474. if (!mboxq)
  14475. return -ENOMEM;
  14476. /* Post all rpi memory regions to the port. */
  14477. lpfc_resume_rpi(mboxq, ndlp);
  14478. if (cmpl) {
  14479. mboxq->mbox_cmpl = cmpl;
  14480. mboxq->context1 = arg;
  14481. mboxq->context2 = ndlp;
  14482. } else
  14483. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14484. mboxq->vport = ndlp->vport;
  14485. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14486. if (rc == MBX_NOT_FINISHED) {
  14487. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14488. "2010 Resume RPI Mailbox failed "
  14489. "status %d, mbxStatus x%x\n", rc,
  14490. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  14491. mempool_free(mboxq, phba->mbox_mem_pool);
  14492. return -EIO;
  14493. }
  14494. return 0;
  14495. }
  14496. /**
  14497. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  14498. * @vport: Pointer to the vport for which the vpi is being initialized
  14499. *
  14500. * This routine is invoked to activate a vpi with the port.
  14501. *
  14502. * Returns:
  14503. * 0 success
  14504. * -Evalue otherwise
  14505. **/
  14506. int
  14507. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  14508. {
  14509. LPFC_MBOXQ_t *mboxq;
  14510. int rc = 0;
  14511. int retval = MBX_SUCCESS;
  14512. uint32_t mbox_tmo;
  14513. struct lpfc_hba *phba = vport->phba;
  14514. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14515. if (!mboxq)
  14516. return -ENOMEM;
  14517. lpfc_init_vpi(phba, mboxq, vport->vpi);
  14518. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  14519. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  14520. if (rc != MBX_SUCCESS) {
  14521. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  14522. "2022 INIT VPI Mailbox failed "
  14523. "status %d, mbxStatus x%x\n", rc,
  14524. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  14525. retval = -EIO;
  14526. }
  14527. if (rc != MBX_TIMEOUT)
  14528. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  14529. return retval;
  14530. }
  14531. /**
  14532. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  14533. * @phba: pointer to lpfc hba data structure.
  14534. * @mboxq: Pointer to mailbox object.
  14535. *
  14536. * This routine is invoked to manually add a single FCF record. The caller
  14537. * must pass a completely initialized FCF_Record. This routine takes
  14538. * care of the nonembedded mailbox operations.
  14539. **/
  14540. static void
  14541. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  14542. {
  14543. void *virt_addr;
  14544. union lpfc_sli4_cfg_shdr *shdr;
  14545. uint32_t shdr_status, shdr_add_status;
  14546. virt_addr = mboxq->sge_array->addr[0];
  14547. /* The IOCTL status is embedded in the mailbox subheader. */
  14548. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  14549. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14550. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14551. if ((shdr_status || shdr_add_status) &&
  14552. (shdr_status != STATUS_FCF_IN_USE))
  14553. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14554. "2558 ADD_FCF_RECORD mailbox failed with "
  14555. "status x%x add_status x%x\n",
  14556. shdr_status, shdr_add_status);
  14557. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14558. }
  14559. /**
  14560. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  14561. * @phba: pointer to lpfc hba data structure.
  14562. * @fcf_record: pointer to the initialized fcf record to add.
  14563. *
  14564. * This routine is invoked to manually add a single FCF record. The caller
  14565. * must pass a completely initialized FCF_Record. This routine takes
  14566. * care of the nonembedded mailbox operations.
  14567. **/
  14568. int
  14569. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  14570. {
  14571. int rc = 0;
  14572. LPFC_MBOXQ_t *mboxq;
  14573. uint8_t *bytep;
  14574. void *virt_addr;
  14575. dma_addr_t phys_addr;
  14576. struct lpfc_mbx_sge sge;
  14577. uint32_t alloc_len, req_len;
  14578. uint32_t fcfindex;
  14579. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14580. if (!mboxq) {
  14581. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14582. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  14583. return -ENOMEM;
  14584. }
  14585. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  14586. sizeof(uint32_t);
  14587. /* Allocate DMA memory and set up the non-embedded mailbox command */
  14588. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  14589. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  14590. req_len, LPFC_SLI4_MBX_NEMBED);
  14591. if (alloc_len < req_len) {
  14592. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14593. "2523 Allocated DMA memory size (x%x) is "
  14594. "less than the requested DMA memory "
  14595. "size (x%x)\n", alloc_len, req_len);
  14596. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14597. return -ENOMEM;
  14598. }
  14599. /*
  14600. * Get the first SGE entry from the non-embedded DMA memory. This
  14601. * routine only uses a single SGE.
  14602. */
  14603. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  14604. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  14605. virt_addr = mboxq->sge_array->addr[0];
  14606. /*
  14607. * Configure the FCF record for FCFI 0. This is the driver's
  14608. * hardcoded default and gets used in nonFIP mode.
  14609. */
  14610. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  14611. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  14612. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  14613. /*
  14614. * Copy the fcf_index and the FCF Record Data. The data starts after
  14615. * the FCoE header plus word10. The data copy needs to be endian
  14616. * correct.
  14617. */
  14618. bytep += sizeof(uint32_t);
  14619. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  14620. mboxq->vport = phba->pport;
  14621. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  14622. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14623. if (rc == MBX_NOT_FINISHED) {
  14624. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14625. "2515 ADD_FCF_RECORD mailbox failed with "
  14626. "status 0x%x\n", rc);
  14627. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14628. rc = -EIO;
  14629. } else
  14630. rc = 0;
  14631. return rc;
  14632. }
  14633. /**
  14634. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  14635. * @phba: pointer to lpfc hba data structure.
  14636. * @fcf_record: pointer to the fcf record to write the default data.
  14637. * @fcf_index: FCF table entry index.
  14638. *
  14639. * This routine is invoked to build the driver's default FCF record. The
  14640. * values used are hardcoded. This routine handles memory initialization.
  14641. *
  14642. **/
  14643. void
  14644. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  14645. struct fcf_record *fcf_record,
  14646. uint16_t fcf_index)
  14647. {
  14648. memset(fcf_record, 0, sizeof(struct fcf_record));
  14649. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  14650. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  14651. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  14652. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  14653. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  14654. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  14655. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  14656. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  14657. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  14658. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  14659. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  14660. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  14661. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  14662. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  14663. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  14664. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  14665. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  14666. /* Set the VLAN bit map */
  14667. if (phba->valid_vlan) {
  14668. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  14669. = 1 << (phba->vlan_id % 8);
  14670. }
  14671. }
  14672. /**
  14673. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  14674. * @phba: pointer to lpfc hba data structure.
  14675. * @fcf_index: FCF table entry offset.
  14676. *
  14677. * This routine is invoked to scan the entire FCF table by reading FCF
  14678. * record and processing it one at a time starting from the @fcf_index
  14679. * for initial FCF discovery or fast FCF failover rediscovery.
  14680. *
  14681. * Return 0 if the mailbox command is submitted successfully, none 0
  14682. * otherwise.
  14683. **/
  14684. int
  14685. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14686. {
  14687. int rc = 0, error;
  14688. LPFC_MBOXQ_t *mboxq;
  14689. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  14690. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  14691. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14692. if (!mboxq) {
  14693. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14694. "2000 Failed to allocate mbox for "
  14695. "READ_FCF cmd\n");
  14696. error = -ENOMEM;
  14697. goto fail_fcf_scan;
  14698. }
  14699. /* Construct the read FCF record mailbox command */
  14700. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14701. if (rc) {
  14702. error = -EINVAL;
  14703. goto fail_fcf_scan;
  14704. }
  14705. /* Issue the mailbox command asynchronously */
  14706. mboxq->vport = phba->pport;
  14707. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  14708. spin_lock_irq(&phba->hbalock);
  14709. phba->hba_flag |= FCF_TS_INPROG;
  14710. spin_unlock_irq(&phba->hbalock);
  14711. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14712. if (rc == MBX_NOT_FINISHED)
  14713. error = -EIO;
  14714. else {
  14715. /* Reset eligible FCF count for new scan */
  14716. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  14717. phba->fcf.eligible_fcf_cnt = 0;
  14718. error = 0;
  14719. }
  14720. fail_fcf_scan:
  14721. if (error) {
  14722. if (mboxq)
  14723. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14724. /* FCF scan failed, clear FCF_TS_INPROG flag */
  14725. spin_lock_irq(&phba->hbalock);
  14726. phba->hba_flag &= ~FCF_TS_INPROG;
  14727. spin_unlock_irq(&phba->hbalock);
  14728. }
  14729. return error;
  14730. }
  14731. /**
  14732. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  14733. * @phba: pointer to lpfc hba data structure.
  14734. * @fcf_index: FCF table entry offset.
  14735. *
  14736. * This routine is invoked to read an FCF record indicated by @fcf_index
  14737. * and to use it for FLOGI roundrobin FCF failover.
  14738. *
  14739. * Return 0 if the mailbox command is submitted successfully, none 0
  14740. * otherwise.
  14741. **/
  14742. int
  14743. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14744. {
  14745. int rc = 0, error;
  14746. LPFC_MBOXQ_t *mboxq;
  14747. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14748. if (!mboxq) {
  14749. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  14750. "2763 Failed to allocate mbox for "
  14751. "READ_FCF cmd\n");
  14752. error = -ENOMEM;
  14753. goto fail_fcf_read;
  14754. }
  14755. /* Construct the read FCF record mailbox command */
  14756. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14757. if (rc) {
  14758. error = -EINVAL;
  14759. goto fail_fcf_read;
  14760. }
  14761. /* Issue the mailbox command asynchronously */
  14762. mboxq->vport = phba->pport;
  14763. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  14764. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14765. if (rc == MBX_NOT_FINISHED)
  14766. error = -EIO;
  14767. else
  14768. error = 0;
  14769. fail_fcf_read:
  14770. if (error && mboxq)
  14771. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14772. return error;
  14773. }
  14774. /**
  14775. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  14776. * @phba: pointer to lpfc hba data structure.
  14777. * @fcf_index: FCF table entry offset.
  14778. *
  14779. * This routine is invoked to read an FCF record indicated by @fcf_index to
  14780. * determine whether it's eligible for FLOGI roundrobin failover list.
  14781. *
  14782. * Return 0 if the mailbox command is submitted successfully, none 0
  14783. * otherwise.
  14784. **/
  14785. int
  14786. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14787. {
  14788. int rc = 0, error;
  14789. LPFC_MBOXQ_t *mboxq;
  14790. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14791. if (!mboxq) {
  14792. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  14793. "2758 Failed to allocate mbox for "
  14794. "READ_FCF cmd\n");
  14795. error = -ENOMEM;
  14796. goto fail_fcf_read;
  14797. }
  14798. /* Construct the read FCF record mailbox command */
  14799. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14800. if (rc) {
  14801. error = -EINVAL;
  14802. goto fail_fcf_read;
  14803. }
  14804. /* Issue the mailbox command asynchronously */
  14805. mboxq->vport = phba->pport;
  14806. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  14807. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14808. if (rc == MBX_NOT_FINISHED)
  14809. error = -EIO;
  14810. else
  14811. error = 0;
  14812. fail_fcf_read:
  14813. if (error && mboxq)
  14814. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14815. return error;
  14816. }
  14817. /**
  14818. * lpfc_check_next_fcf_pri
  14819. * phba pointer to the lpfc_hba struct for this port.
  14820. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  14821. * routine when the rr_bmask is empty. The FCF indecies are put into the
  14822. * rr_bmask based on their priority level. Starting from the highest priority
  14823. * to the lowest. The most likely FCF candidate will be in the highest
  14824. * priority group. When this routine is called it searches the fcf_pri list for
  14825. * next lowest priority group and repopulates the rr_bmask with only those
  14826. * fcf_indexes.
  14827. * returns:
  14828. * 1=success 0=failure
  14829. **/
  14830. static int
  14831. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  14832. {
  14833. uint16_t next_fcf_pri;
  14834. uint16_t last_index;
  14835. struct lpfc_fcf_pri *fcf_pri;
  14836. int rc;
  14837. int ret = 0;
  14838. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  14839. LPFC_SLI4_FCF_TBL_INDX_MAX);
  14840. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14841. "3060 Last IDX %d\n", last_index);
  14842. /* Verify the priority list has 2 or more entries */
  14843. spin_lock_irq(&phba->hbalock);
  14844. if (list_empty(&phba->fcf.fcf_pri_list) ||
  14845. list_is_singular(&phba->fcf.fcf_pri_list)) {
  14846. spin_unlock_irq(&phba->hbalock);
  14847. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14848. "3061 Last IDX %d\n", last_index);
  14849. return 0; /* Empty rr list */
  14850. }
  14851. spin_unlock_irq(&phba->hbalock);
  14852. next_fcf_pri = 0;
  14853. /*
  14854. * Clear the rr_bmask and set all of the bits that are at this
  14855. * priority.
  14856. */
  14857. memset(phba->fcf.fcf_rr_bmask, 0,
  14858. sizeof(*phba->fcf.fcf_rr_bmask));
  14859. spin_lock_irq(&phba->hbalock);
  14860. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14861. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  14862. continue;
  14863. /*
  14864. * the 1st priority that has not FLOGI failed
  14865. * will be the highest.
  14866. */
  14867. if (!next_fcf_pri)
  14868. next_fcf_pri = fcf_pri->fcf_rec.priority;
  14869. spin_unlock_irq(&phba->hbalock);
  14870. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  14871. rc = lpfc_sli4_fcf_rr_index_set(phba,
  14872. fcf_pri->fcf_rec.fcf_index);
  14873. if (rc)
  14874. return 0;
  14875. }
  14876. spin_lock_irq(&phba->hbalock);
  14877. }
  14878. /*
  14879. * if next_fcf_pri was not set above and the list is not empty then
  14880. * we have failed flogis on all of them. So reset flogi failed
  14881. * and start at the beginning.
  14882. */
  14883. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  14884. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14885. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  14886. /*
  14887. * the 1st priority that has not FLOGI failed
  14888. * will be the highest.
  14889. */
  14890. if (!next_fcf_pri)
  14891. next_fcf_pri = fcf_pri->fcf_rec.priority;
  14892. spin_unlock_irq(&phba->hbalock);
  14893. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  14894. rc = lpfc_sli4_fcf_rr_index_set(phba,
  14895. fcf_pri->fcf_rec.fcf_index);
  14896. if (rc)
  14897. return 0;
  14898. }
  14899. spin_lock_irq(&phba->hbalock);
  14900. }
  14901. } else
  14902. ret = 1;
  14903. spin_unlock_irq(&phba->hbalock);
  14904. return ret;
  14905. }
  14906. /**
  14907. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  14908. * @phba: pointer to lpfc hba data structure.
  14909. *
  14910. * This routine is to get the next eligible FCF record index in a round
  14911. * robin fashion. If the next eligible FCF record index equals to the
  14912. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  14913. * shall be returned, otherwise, the next eligible FCF record's index
  14914. * shall be returned.
  14915. **/
  14916. uint16_t
  14917. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  14918. {
  14919. uint16_t next_fcf_index;
  14920. initial_priority:
  14921. /* Search start from next bit of currently registered FCF index */
  14922. next_fcf_index = phba->fcf.current_rec.fcf_indx;
  14923. next_priority:
  14924. /* Determine the next fcf index to check */
  14925. next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
  14926. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14927. LPFC_SLI4_FCF_TBL_INDX_MAX,
  14928. next_fcf_index);
  14929. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  14930. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14931. /*
  14932. * If we have wrapped then we need to clear the bits that
  14933. * have been tested so that we can detect when we should
  14934. * change the priority level.
  14935. */
  14936. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14937. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  14938. }
  14939. /* Check roundrobin failover list empty condition */
  14940. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  14941. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  14942. /*
  14943. * If next fcf index is not found check if there are lower
  14944. * Priority level fcf's in the fcf_priority list.
  14945. * Set up the rr_bmask with all of the avaiable fcf bits
  14946. * at that level and continue the selection process.
  14947. */
  14948. if (lpfc_check_next_fcf_pri_level(phba))
  14949. goto initial_priority;
  14950. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14951. "2844 No roundrobin failover FCF available\n");
  14952. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  14953. return LPFC_FCOE_FCF_NEXT_NONE;
  14954. else {
  14955. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14956. "3063 Only FCF available idx %d, flag %x\n",
  14957. next_fcf_index,
  14958. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  14959. return next_fcf_index;
  14960. }
  14961. }
  14962. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  14963. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  14964. LPFC_FCF_FLOGI_FAILED)
  14965. goto next_priority;
  14966. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14967. "2845 Get next roundrobin failover FCF (x%x)\n",
  14968. next_fcf_index);
  14969. return next_fcf_index;
  14970. }
  14971. /**
  14972. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  14973. * @phba: pointer to lpfc hba data structure.
  14974. *
  14975. * This routine sets the FCF record index in to the eligible bmask for
  14976. * roundrobin failover search. It checks to make sure that the index
  14977. * does not go beyond the range of the driver allocated bmask dimension
  14978. * before setting the bit.
  14979. *
  14980. * Returns 0 if the index bit successfully set, otherwise, it returns
  14981. * -EINVAL.
  14982. **/
  14983. int
  14984. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  14985. {
  14986. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14987. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14988. "2610 FCF (x%x) reached driver's book "
  14989. "keeping dimension:x%x\n",
  14990. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14991. return -EINVAL;
  14992. }
  14993. /* Set the eligible FCF record index bmask */
  14994. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14995. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14996. "2790 Set FCF (x%x) to roundrobin FCF failover "
  14997. "bmask\n", fcf_index);
  14998. return 0;
  14999. }
  15000. /**
  15001. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  15002. * @phba: pointer to lpfc hba data structure.
  15003. *
  15004. * This routine clears the FCF record index from the eligible bmask for
  15005. * roundrobin failover search. It checks to make sure that the index
  15006. * does not go beyond the range of the driver allocated bmask dimension
  15007. * before clearing the bit.
  15008. **/
  15009. void
  15010. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  15011. {
  15012. struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
  15013. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  15014. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  15015. "2762 FCF (x%x) reached driver's book "
  15016. "keeping dimension:x%x\n",
  15017. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  15018. return;
  15019. }
  15020. /* Clear the eligible FCF record index bmask */
  15021. spin_lock_irq(&phba->hbalock);
  15022. list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
  15023. list) {
  15024. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  15025. list_del_init(&fcf_pri->list);
  15026. break;
  15027. }
  15028. }
  15029. spin_unlock_irq(&phba->hbalock);
  15030. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  15031. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  15032. "2791 Clear FCF (x%x) from roundrobin failover "
  15033. "bmask\n", fcf_index);
  15034. }
  15035. /**
  15036. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  15037. * @phba: pointer to lpfc hba data structure.
  15038. *
  15039. * This routine is the completion routine for the rediscover FCF table mailbox
  15040. * command. If the mailbox command returned failure, it will try to stop the
  15041. * FCF rediscover wait timer.
  15042. **/
  15043. static void
  15044. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  15045. {
  15046. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  15047. uint32_t shdr_status, shdr_add_status;
  15048. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  15049. shdr_status = bf_get(lpfc_mbox_hdr_status,
  15050. &redisc_fcf->header.cfg_shdr.response);
  15051. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  15052. &redisc_fcf->header.cfg_shdr.response);
  15053. if (shdr_status || shdr_add_status) {
  15054. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  15055. "2746 Requesting for FCF rediscovery failed "
  15056. "status x%x add_status x%x\n",
  15057. shdr_status, shdr_add_status);
  15058. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  15059. spin_lock_irq(&phba->hbalock);
  15060. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  15061. spin_unlock_irq(&phba->hbalock);
  15062. /*
  15063. * CVL event triggered FCF rediscover request failed,
  15064. * last resort to re-try current registered FCF entry.
  15065. */
  15066. lpfc_retry_pport_discovery(phba);
  15067. } else {
  15068. spin_lock_irq(&phba->hbalock);
  15069. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  15070. spin_unlock_irq(&phba->hbalock);
  15071. /*
  15072. * DEAD FCF event triggered FCF rediscover request
  15073. * failed, last resort to fail over as a link down
  15074. * to FCF registration.
  15075. */
  15076. lpfc_sli4_fcf_dead_failthrough(phba);
  15077. }
  15078. } else {
  15079. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  15080. "2775 Start FCF rediscover quiescent timer\n");
  15081. /*
  15082. * Start FCF rediscovery wait timer for pending FCF
  15083. * before rescan FCF record table.
  15084. */
  15085. lpfc_fcf_redisc_wait_start_timer(phba);
  15086. }
  15087. mempool_free(mbox, phba->mbox_mem_pool);
  15088. }
  15089. /**
  15090. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  15091. * @phba: pointer to lpfc hba data structure.
  15092. *
  15093. * This routine is invoked to request for rediscovery of the entire FCF table
  15094. * by the port.
  15095. **/
  15096. int
  15097. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  15098. {
  15099. LPFC_MBOXQ_t *mbox;
  15100. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  15101. int rc, length;
  15102. /* Cancel retry delay timers to all vports before FCF rediscover */
  15103. lpfc_cancel_all_vport_retry_delay_timer(phba);
  15104. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  15105. if (!mbox) {
  15106. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15107. "2745 Failed to allocate mbox for "
  15108. "requesting FCF rediscover.\n");
  15109. return -ENOMEM;
  15110. }
  15111. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  15112. sizeof(struct lpfc_sli4_cfg_mhdr));
  15113. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  15114. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  15115. length, LPFC_SLI4_MBX_EMBED);
  15116. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  15117. /* Set count to 0 for invalidating the entire FCF database */
  15118. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  15119. /* Issue the mailbox command asynchronously */
  15120. mbox->vport = phba->pport;
  15121. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  15122. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  15123. if (rc == MBX_NOT_FINISHED) {
  15124. mempool_free(mbox, phba->mbox_mem_pool);
  15125. return -EIO;
  15126. }
  15127. return 0;
  15128. }
  15129. /**
  15130. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  15131. * @phba: pointer to lpfc hba data structure.
  15132. *
  15133. * This function is the failover routine as a last resort to the FCF DEAD
  15134. * event when driver failed to perform fast FCF failover.
  15135. **/
  15136. void
  15137. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  15138. {
  15139. uint32_t link_state;
  15140. /*
  15141. * Last resort as FCF DEAD event failover will treat this as
  15142. * a link down, but save the link state because we don't want
  15143. * it to be changed to Link Down unless it is already down.
  15144. */
  15145. link_state = phba->link_state;
  15146. lpfc_linkdown(phba);
  15147. phba->link_state = link_state;
  15148. /* Unregister FCF if no devices connected to it */
  15149. lpfc_unregister_unused_fcf(phba);
  15150. }
  15151. /**
  15152. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  15153. * @phba: pointer to lpfc hba data structure.
  15154. * @rgn23_data: pointer to configure region 23 data.
  15155. *
  15156. * This function gets SLI3 port configure region 23 data through memory dump
  15157. * mailbox command. When it successfully retrieves data, the size of the data
  15158. * will be returned, otherwise, 0 will be returned.
  15159. **/
  15160. static uint32_t
  15161. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  15162. {
  15163. LPFC_MBOXQ_t *pmb = NULL;
  15164. MAILBOX_t *mb;
  15165. uint32_t offset = 0;
  15166. int rc;
  15167. if (!rgn23_data)
  15168. return 0;
  15169. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  15170. if (!pmb) {
  15171. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15172. "2600 failed to allocate mailbox memory\n");
  15173. return 0;
  15174. }
  15175. mb = &pmb->u.mb;
  15176. do {
  15177. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  15178. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  15179. if (rc != MBX_SUCCESS) {
  15180. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  15181. "2601 failed to read config "
  15182. "region 23, rc 0x%x Status 0x%x\n",
  15183. rc, mb->mbxStatus);
  15184. mb->un.varDmp.word_cnt = 0;
  15185. }
  15186. /*
  15187. * dump mem may return a zero when finished or we got a
  15188. * mailbox error, either way we are done.
  15189. */
  15190. if (mb->un.varDmp.word_cnt == 0)
  15191. break;
  15192. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  15193. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  15194. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  15195. rgn23_data + offset,
  15196. mb->un.varDmp.word_cnt);
  15197. offset += mb->un.varDmp.word_cnt;
  15198. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  15199. mempool_free(pmb, phba->mbox_mem_pool);
  15200. return offset;
  15201. }
  15202. /**
  15203. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  15204. * @phba: pointer to lpfc hba data structure.
  15205. * @rgn23_data: pointer to configure region 23 data.
  15206. *
  15207. * This function gets SLI4 port configure region 23 data through memory dump
  15208. * mailbox command. When it successfully retrieves data, the size of the data
  15209. * will be returned, otherwise, 0 will be returned.
  15210. **/
  15211. static uint32_t
  15212. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  15213. {
  15214. LPFC_MBOXQ_t *mboxq = NULL;
  15215. struct lpfc_dmabuf *mp = NULL;
  15216. struct lpfc_mqe *mqe;
  15217. uint32_t data_length = 0;
  15218. int rc;
  15219. if (!rgn23_data)
  15220. return 0;
  15221. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  15222. if (!mboxq) {
  15223. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15224. "3105 failed to allocate mailbox memory\n");
  15225. return 0;
  15226. }
  15227. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  15228. goto out;
  15229. mqe = &mboxq->u.mqe;
  15230. mp = (struct lpfc_dmabuf *) mboxq->context1;
  15231. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  15232. if (rc)
  15233. goto out;
  15234. data_length = mqe->un.mb_words[5];
  15235. if (data_length == 0)
  15236. goto out;
  15237. if (data_length > DMP_RGN23_SIZE) {
  15238. data_length = 0;
  15239. goto out;
  15240. }
  15241. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  15242. out:
  15243. mempool_free(mboxq, phba->mbox_mem_pool);
  15244. if (mp) {
  15245. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  15246. kfree(mp);
  15247. }
  15248. return data_length;
  15249. }
  15250. /**
  15251. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  15252. * @phba: pointer to lpfc hba data structure.
  15253. *
  15254. * This function read region 23 and parse TLV for port status to
  15255. * decide if the user disaled the port. If the TLV indicates the
  15256. * port is disabled, the hba_flag is set accordingly.
  15257. **/
  15258. void
  15259. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  15260. {
  15261. uint8_t *rgn23_data = NULL;
  15262. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  15263. uint32_t offset = 0;
  15264. /* Get adapter Region 23 data */
  15265. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  15266. if (!rgn23_data)
  15267. goto out;
  15268. if (phba->sli_rev < LPFC_SLI_REV4)
  15269. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  15270. else {
  15271. if_type = bf_get(lpfc_sli_intf_if_type,
  15272. &phba->sli4_hba.sli_intf);
  15273. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  15274. goto out;
  15275. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  15276. }
  15277. if (!data_size)
  15278. goto out;
  15279. /* Check the region signature first */
  15280. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  15281. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15282. "2619 Config region 23 has bad signature\n");
  15283. goto out;
  15284. }
  15285. offset += 4;
  15286. /* Check the data structure version */
  15287. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  15288. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15289. "2620 Config region 23 has bad version\n");
  15290. goto out;
  15291. }
  15292. offset += 4;
  15293. /* Parse TLV entries in the region */
  15294. while (offset < data_size) {
  15295. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  15296. break;
  15297. /*
  15298. * If the TLV is not driver specific TLV or driver id is
  15299. * not linux driver id, skip the record.
  15300. */
  15301. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  15302. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  15303. (rgn23_data[offset + 3] != 0)) {
  15304. offset += rgn23_data[offset + 1] * 4 + 4;
  15305. continue;
  15306. }
  15307. /* Driver found a driver specific TLV in the config region */
  15308. sub_tlv_len = rgn23_data[offset + 1] * 4;
  15309. offset += 4;
  15310. tlv_offset = 0;
  15311. /*
  15312. * Search for configured port state sub-TLV.
  15313. */
  15314. while ((offset < data_size) &&
  15315. (tlv_offset < sub_tlv_len)) {
  15316. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  15317. offset += 4;
  15318. tlv_offset += 4;
  15319. break;
  15320. }
  15321. if (rgn23_data[offset] != PORT_STE_TYPE) {
  15322. offset += rgn23_data[offset + 1] * 4 + 4;
  15323. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  15324. continue;
  15325. }
  15326. /* This HBA contains PORT_STE configured */
  15327. if (!rgn23_data[offset + 2])
  15328. phba->hba_flag |= LINK_DISABLED;
  15329. goto out;
  15330. }
  15331. }
  15332. out:
  15333. kfree(rgn23_data);
  15334. return;
  15335. }
  15336. /**
  15337. * lpfc_wr_object - write an object to the firmware
  15338. * @phba: HBA structure that indicates port to create a queue on.
  15339. * @dmabuf_list: list of dmabufs to write to the port.
  15340. * @size: the total byte value of the objects to write to the port.
  15341. * @offset: the current offset to be used to start the transfer.
  15342. *
  15343. * This routine will create a wr_object mailbox command to send to the port.
  15344. * the mailbox command will be constructed using the dma buffers described in
  15345. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  15346. * BDEs that the imbedded mailbox can support. The @offset variable will be
  15347. * used to indicate the starting offset of the transfer and will also return
  15348. * the offset after the write object mailbox has completed. @size is used to
  15349. * determine the end of the object and whether the eof bit should be set.
  15350. *
  15351. * Return 0 is successful and offset will contain the the new offset to use
  15352. * for the next write.
  15353. * Return negative value for error cases.
  15354. **/
  15355. int
  15356. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  15357. uint32_t size, uint32_t *offset)
  15358. {
  15359. struct lpfc_mbx_wr_object *wr_object;
  15360. LPFC_MBOXQ_t *mbox;
  15361. int rc = 0, i = 0;
  15362. uint32_t shdr_status, shdr_add_status;
  15363. uint32_t mbox_tmo;
  15364. union lpfc_sli4_cfg_shdr *shdr;
  15365. struct lpfc_dmabuf *dmabuf;
  15366. uint32_t written = 0;
  15367. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  15368. if (!mbox)
  15369. return -ENOMEM;
  15370. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  15371. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  15372. sizeof(struct lpfc_mbx_wr_object) -
  15373. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  15374. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  15375. wr_object->u.request.write_offset = *offset;
  15376. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  15377. wr_object->u.request.object_name[0] =
  15378. cpu_to_le32(wr_object->u.request.object_name[0]);
  15379. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  15380. list_for_each_entry(dmabuf, dmabuf_list, list) {
  15381. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  15382. break;
  15383. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  15384. wr_object->u.request.bde[i].addrHigh =
  15385. putPaddrHigh(dmabuf->phys);
  15386. if (written + SLI4_PAGE_SIZE >= size) {
  15387. wr_object->u.request.bde[i].tus.f.bdeSize =
  15388. (size - written);
  15389. written += (size - written);
  15390. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  15391. } else {
  15392. wr_object->u.request.bde[i].tus.f.bdeSize =
  15393. SLI4_PAGE_SIZE;
  15394. written += SLI4_PAGE_SIZE;
  15395. }
  15396. i++;
  15397. }
  15398. wr_object->u.request.bde_count = i;
  15399. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  15400. if (!phba->sli4_hba.intr_enable)
  15401. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  15402. else {
  15403. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  15404. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  15405. }
  15406. /* The IOCTL status is embedded in the mailbox subheader. */
  15407. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  15408. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  15409. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  15410. if (rc != MBX_TIMEOUT)
  15411. mempool_free(mbox, phba->mbox_mem_pool);
  15412. if (shdr_status || shdr_add_status || rc) {
  15413. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15414. "3025 Write Object mailbox failed with "
  15415. "status x%x add_status x%x, mbx status x%x\n",
  15416. shdr_status, shdr_add_status, rc);
  15417. rc = -ENXIO;
  15418. } else
  15419. *offset += wr_object->u.response.actual_write_length;
  15420. return rc;
  15421. }
  15422. /**
  15423. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  15424. * @vport: pointer to vport data structure.
  15425. *
  15426. * This function iterate through the mailboxq and clean up all REG_LOGIN
  15427. * and REG_VPI mailbox commands associated with the vport. This function
  15428. * is called when driver want to restart discovery of the vport due to
  15429. * a Clear Virtual Link event.
  15430. **/
  15431. void
  15432. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  15433. {
  15434. struct lpfc_hba *phba = vport->phba;
  15435. LPFC_MBOXQ_t *mb, *nextmb;
  15436. struct lpfc_dmabuf *mp;
  15437. struct lpfc_nodelist *ndlp;
  15438. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  15439. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  15440. LIST_HEAD(mbox_cmd_list);
  15441. uint8_t restart_loop;
  15442. /* Clean up internally queued mailbox commands with the vport */
  15443. spin_lock_irq(&phba->hbalock);
  15444. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  15445. if (mb->vport != vport)
  15446. continue;
  15447. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  15448. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  15449. continue;
  15450. list_del(&mb->list);
  15451. list_add_tail(&mb->list, &mbox_cmd_list);
  15452. }
  15453. /* Clean up active mailbox command with the vport */
  15454. mb = phba->sli.mbox_active;
  15455. if (mb && (mb->vport == vport)) {
  15456. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  15457. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  15458. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  15459. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  15460. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  15461. /* Put reference count for delayed processing */
  15462. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  15463. /* Unregister the RPI when mailbox complete */
  15464. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  15465. }
  15466. }
  15467. /* Cleanup any mailbox completions which are not yet processed */
  15468. do {
  15469. restart_loop = 0;
  15470. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  15471. /*
  15472. * If this mailox is already processed or it is
  15473. * for another vport ignore it.
  15474. */
  15475. if ((mb->vport != vport) ||
  15476. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  15477. continue;
  15478. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  15479. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  15480. continue;
  15481. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  15482. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  15483. ndlp = (struct lpfc_nodelist *)mb->context2;
  15484. /* Unregister the RPI when mailbox complete */
  15485. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  15486. restart_loop = 1;
  15487. spin_unlock_irq(&phba->hbalock);
  15488. spin_lock(shost->host_lock);
  15489. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  15490. spin_unlock(shost->host_lock);
  15491. spin_lock_irq(&phba->hbalock);
  15492. break;
  15493. }
  15494. }
  15495. } while (restart_loop);
  15496. spin_unlock_irq(&phba->hbalock);
  15497. /* Release the cleaned-up mailbox commands */
  15498. while (!list_empty(&mbox_cmd_list)) {
  15499. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  15500. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  15501. mp = (struct lpfc_dmabuf *) (mb->context1);
  15502. if (mp) {
  15503. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  15504. kfree(mp);
  15505. }
  15506. ndlp = (struct lpfc_nodelist *) mb->context2;
  15507. mb->context2 = NULL;
  15508. if (ndlp) {
  15509. spin_lock(shost->host_lock);
  15510. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  15511. spin_unlock(shost->host_lock);
  15512. lpfc_nlp_put(ndlp);
  15513. }
  15514. }
  15515. mempool_free(mb, phba->mbox_mem_pool);
  15516. }
  15517. /* Release the ndlp with the cleaned-up active mailbox command */
  15518. if (act_mbx_ndlp) {
  15519. spin_lock(shost->host_lock);
  15520. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  15521. spin_unlock(shost->host_lock);
  15522. lpfc_nlp_put(act_mbx_ndlp);
  15523. }
  15524. }
  15525. /**
  15526. * lpfc_drain_txq - Drain the txq
  15527. * @phba: Pointer to HBA context object.
  15528. *
  15529. * This function attempt to submit IOCBs on the txq
  15530. * to the adapter. For SLI4 adapters, the txq contains
  15531. * ELS IOCBs that have been deferred because the there
  15532. * are no SGLs. This congestion can occur with large
  15533. * vport counts during node discovery.
  15534. **/
  15535. uint32_t
  15536. lpfc_drain_txq(struct lpfc_hba *phba)
  15537. {
  15538. LIST_HEAD(completions);
  15539. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  15540. struct lpfc_iocbq *piocbq = NULL;
  15541. unsigned long iflags = 0;
  15542. char *fail_msg = NULL;
  15543. struct lpfc_sglq *sglq;
  15544. union lpfc_wqe wqe;
  15545. uint32_t txq_cnt = 0;
  15546. spin_lock_irqsave(&pring->ring_lock, iflags);
  15547. list_for_each_entry(piocbq, &pring->txq, list) {
  15548. txq_cnt++;
  15549. }
  15550. if (txq_cnt > pring->txq_max)
  15551. pring->txq_max = txq_cnt;
  15552. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15553. while (!list_empty(&pring->txq)) {
  15554. spin_lock_irqsave(&pring->ring_lock, iflags);
  15555. piocbq = lpfc_sli_ringtx_get(phba, pring);
  15556. if (!piocbq) {
  15557. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15558. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15559. "2823 txq empty and txq_cnt is %d\n ",
  15560. txq_cnt);
  15561. break;
  15562. }
  15563. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  15564. if (!sglq) {
  15565. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  15566. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15567. break;
  15568. }
  15569. txq_cnt--;
  15570. /* The xri and iocb resources secured,
  15571. * attempt to issue request
  15572. */
  15573. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  15574. piocbq->sli4_xritag = sglq->sli4_xritag;
  15575. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  15576. fail_msg = "to convert bpl to sgl";
  15577. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  15578. fail_msg = "to convert iocb to wqe";
  15579. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  15580. fail_msg = " - Wq is full";
  15581. else
  15582. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  15583. if (fail_msg) {
  15584. /* Failed means we can't issue and need to cancel */
  15585. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15586. "2822 IOCB failed %s iotag 0x%x "
  15587. "xri 0x%x\n",
  15588. fail_msg,
  15589. piocbq->iotag, piocbq->sli4_xritag);
  15590. list_add_tail(&piocbq->list, &completions);
  15591. }
  15592. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15593. }
  15594. /* Cancel all the IOCBs that cannot be issued */
  15595. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  15596. IOERR_SLI_ABORTED);
  15597. return txq_cnt;
  15598. }