lpfc_sli.c 489 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2012 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/fc/fc_fs.h>
  32. #include <linux/aer.h>
  33. #include "lpfc_hw4.h"
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_nl.h"
  38. #include "lpfc_disc.h"
  39. #include "lpfc_scsi.h"
  40. #include "lpfc.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_compat.h"
  44. #include "lpfc_debugfs.h"
  45. #include "lpfc_vport.h"
  46. /* There are only four IOCB completion types. */
  47. typedef enum _lpfc_iocb_type {
  48. LPFC_UNKNOWN_IOCB,
  49. LPFC_UNSOL_IOCB,
  50. LPFC_SOL_IOCB,
  51. LPFC_ABORT_IOCB
  52. } lpfc_iocb_type;
  53. /* Provide function prototypes local to this module. */
  54. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint32_t);
  56. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  57. uint8_t *, uint32_t *);
  58. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  59. struct lpfc_iocbq *);
  60. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  61. struct hbq_dmabuf *);
  62. static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
  63. struct lpfc_cqe *);
  64. static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
  65. int);
  66. static IOCB_t *
  67. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  68. {
  69. return &iocbq->iocb;
  70. }
  71. /**
  72. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  73. * @q: The Work Queue to operate on.
  74. * @wqe: The work Queue Entry to put on the Work queue.
  75. *
  76. * This routine will copy the contents of @wqe to the next available entry on
  77. * the @q. This function will then ring the Work Queue Doorbell to signal the
  78. * HBA to start processing the Work Queue Entry. This function returns 0 if
  79. * successful. If no entries are available on @q then this function will return
  80. * -ENOMEM.
  81. * The caller is expected to hold the hbalock when calling this routine.
  82. **/
  83. static uint32_t
  84. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  85. {
  86. union lpfc_wqe *temp_wqe;
  87. struct lpfc_register doorbell;
  88. uint32_t host_index;
  89. uint32_t idx;
  90. /* sanity check on queue memory */
  91. if (unlikely(!q))
  92. return -ENOMEM;
  93. temp_wqe = q->qe[q->host_index].wqe;
  94. /* If the host has not yet processed the next entry then we are done */
  95. idx = ((q->host_index + 1) % q->entry_count);
  96. if (idx == q->hba_index) {
  97. q->WQ_overflow++;
  98. return -ENOMEM;
  99. }
  100. q->WQ_posted++;
  101. /* set consumption flag every once in a while */
  102. if (!((q->host_index + 1) % q->entry_repost))
  103. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  104. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  105. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  106. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  107. /* Update the host index before invoking device */
  108. host_index = q->host_index;
  109. q->host_index = idx;
  110. /* Ring Doorbell */
  111. doorbell.word0 = 0;
  112. bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
  113. bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
  114. bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
  115. writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
  116. return 0;
  117. }
  118. /**
  119. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  120. * @q: The Work Queue to operate on.
  121. * @index: The index to advance the hba index to.
  122. *
  123. * This routine will update the HBA index of a queue to reflect consumption of
  124. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  125. * an entry the host calls this function to update the queue's internal
  126. * pointers. This routine returns the number of entries that were consumed by
  127. * the HBA.
  128. **/
  129. static uint32_t
  130. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  131. {
  132. uint32_t released = 0;
  133. /* sanity check on queue memory */
  134. if (unlikely(!q))
  135. return 0;
  136. if (q->hba_index == index)
  137. return 0;
  138. do {
  139. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  140. released++;
  141. } while (q->hba_index != index);
  142. return released;
  143. }
  144. /**
  145. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  146. * @q: The Mailbox Queue to operate on.
  147. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  148. *
  149. * This routine will copy the contents of @mqe to the next available entry on
  150. * the @q. This function will then ring the Work Queue Doorbell to signal the
  151. * HBA to start processing the Work Queue Entry. This function returns 0 if
  152. * successful. If no entries are available on @q then this function will return
  153. * -ENOMEM.
  154. * The caller is expected to hold the hbalock when calling this routine.
  155. **/
  156. static uint32_t
  157. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  158. {
  159. struct lpfc_mqe *temp_mqe;
  160. struct lpfc_register doorbell;
  161. uint32_t host_index;
  162. /* sanity check on queue memory */
  163. if (unlikely(!q))
  164. return -ENOMEM;
  165. temp_mqe = q->qe[q->host_index].mqe;
  166. /* If the host has not yet processed the next entry then we are done */
  167. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  168. return -ENOMEM;
  169. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  170. /* Save off the mailbox pointer for completion */
  171. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  172. /* Update the host index before invoking device */
  173. host_index = q->host_index;
  174. q->host_index = ((q->host_index + 1) % q->entry_count);
  175. /* Ring Doorbell */
  176. doorbell.word0 = 0;
  177. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  178. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  179. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  180. return 0;
  181. }
  182. /**
  183. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  184. * @q: The Mailbox Queue to operate on.
  185. *
  186. * This routine will update the HBA index of a queue to reflect consumption of
  187. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  188. * an entry the host calls this function to update the queue's internal
  189. * pointers. This routine returns the number of entries that were consumed by
  190. * the HBA.
  191. **/
  192. static uint32_t
  193. lpfc_sli4_mq_release(struct lpfc_queue *q)
  194. {
  195. /* sanity check on queue memory */
  196. if (unlikely(!q))
  197. return 0;
  198. /* Clear the mailbox pointer for completion */
  199. q->phba->mbox = NULL;
  200. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  201. return 1;
  202. }
  203. /**
  204. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  205. * @q: The Event Queue to get the first valid EQE from
  206. *
  207. * This routine will get the first valid Event Queue Entry from @q, update
  208. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  209. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  210. * processed, but not popped back to the HBA then this routine will return NULL.
  211. **/
  212. static struct lpfc_eqe *
  213. lpfc_sli4_eq_get(struct lpfc_queue *q)
  214. {
  215. struct lpfc_eqe *eqe;
  216. uint32_t idx;
  217. /* sanity check on queue memory */
  218. if (unlikely(!q))
  219. return NULL;
  220. eqe = q->qe[q->hba_index].eqe;
  221. /* If the next EQE is not valid then we are done */
  222. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  223. return NULL;
  224. /* If the host has not yet processed the next entry then we are done */
  225. idx = ((q->hba_index + 1) % q->entry_count);
  226. if (idx == q->host_index)
  227. return NULL;
  228. q->hba_index = idx;
  229. return eqe;
  230. }
  231. /**
  232. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  233. * @q: The Event Queue that the host has completed processing for.
  234. * @arm: Indicates whether the host wants to arms this CQ.
  235. *
  236. * This routine will mark all Event Queue Entries on @q, from the last
  237. * known completed entry to the last entry that was processed, as completed
  238. * by clearing the valid bit for each completion queue entry. Then it will
  239. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  240. * The internal host index in the @q will be updated by this routine to indicate
  241. * that the host has finished processing the entries. The @arm parameter
  242. * indicates that the queue should be rearmed when ringing the doorbell.
  243. *
  244. * This function will return the number of EQEs that were popped.
  245. **/
  246. uint32_t
  247. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  248. {
  249. uint32_t released = 0;
  250. struct lpfc_eqe *temp_eqe;
  251. struct lpfc_register doorbell;
  252. /* sanity check on queue memory */
  253. if (unlikely(!q))
  254. return 0;
  255. /* while there are valid entries */
  256. while (q->hba_index != q->host_index) {
  257. temp_eqe = q->qe[q->host_index].eqe;
  258. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  259. released++;
  260. q->host_index = ((q->host_index + 1) % q->entry_count);
  261. }
  262. if (unlikely(released == 0 && !arm))
  263. return 0;
  264. /* ring doorbell for number popped */
  265. doorbell.word0 = 0;
  266. if (arm) {
  267. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  268. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  269. }
  270. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  271. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  272. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  273. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  274. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  275. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  276. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  277. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  278. readl(q->phba->sli4_hba.EQCQDBregaddr);
  279. return released;
  280. }
  281. /**
  282. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  283. * @q: The Completion Queue to get the first valid CQE from
  284. *
  285. * This routine will get the first valid Completion Queue Entry from @q, update
  286. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  287. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  288. * processed, but not popped back to the HBA then this routine will return NULL.
  289. **/
  290. static struct lpfc_cqe *
  291. lpfc_sli4_cq_get(struct lpfc_queue *q)
  292. {
  293. struct lpfc_cqe *cqe;
  294. uint32_t idx;
  295. /* sanity check on queue memory */
  296. if (unlikely(!q))
  297. return NULL;
  298. /* If the next CQE is not valid then we are done */
  299. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  300. return NULL;
  301. /* If the host has not yet processed the next entry then we are done */
  302. idx = ((q->hba_index + 1) % q->entry_count);
  303. if (idx == q->host_index)
  304. return NULL;
  305. cqe = q->qe[q->hba_index].cqe;
  306. q->hba_index = idx;
  307. return cqe;
  308. }
  309. /**
  310. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  311. * @q: The Completion Queue that the host has completed processing for.
  312. * @arm: Indicates whether the host wants to arms this CQ.
  313. *
  314. * This routine will mark all Completion queue entries on @q, from the last
  315. * known completed entry to the last entry that was processed, as completed
  316. * by clearing the valid bit for each completion queue entry. Then it will
  317. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  318. * The internal host index in the @q will be updated by this routine to indicate
  319. * that the host has finished processing the entries. The @arm parameter
  320. * indicates that the queue should be rearmed when ringing the doorbell.
  321. *
  322. * This function will return the number of CQEs that were released.
  323. **/
  324. uint32_t
  325. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  326. {
  327. uint32_t released = 0;
  328. struct lpfc_cqe *temp_qe;
  329. struct lpfc_register doorbell;
  330. /* sanity check on queue memory */
  331. if (unlikely(!q))
  332. return 0;
  333. /* while there are valid entries */
  334. while (q->hba_index != q->host_index) {
  335. temp_qe = q->qe[q->host_index].cqe;
  336. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  337. released++;
  338. q->host_index = ((q->host_index + 1) % q->entry_count);
  339. }
  340. if (unlikely(released == 0 && !arm))
  341. return 0;
  342. /* ring doorbell for number popped */
  343. doorbell.word0 = 0;
  344. if (arm)
  345. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  346. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  347. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  348. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  349. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  350. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  351. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  352. return released;
  353. }
  354. /**
  355. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  356. * @q: The Header Receive Queue to operate on.
  357. * @wqe: The Receive Queue Entry to put on the Receive queue.
  358. *
  359. * This routine will copy the contents of @wqe to the next available entry on
  360. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  361. * HBA to start processing the Receive Queue Entry. This function returns the
  362. * index that the rqe was copied to if successful. If no entries are available
  363. * on @q then this function will return -ENOMEM.
  364. * The caller is expected to hold the hbalock when calling this routine.
  365. **/
  366. static int
  367. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  368. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  369. {
  370. struct lpfc_rqe *temp_hrqe;
  371. struct lpfc_rqe *temp_drqe;
  372. struct lpfc_register doorbell;
  373. int put_index = hq->host_index;
  374. /* sanity check on queue memory */
  375. if (unlikely(!hq) || unlikely(!dq))
  376. return -ENOMEM;
  377. temp_hrqe = hq->qe[hq->host_index].rqe;
  378. temp_drqe = dq->qe[dq->host_index].rqe;
  379. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  380. return -EINVAL;
  381. if (hq->host_index != dq->host_index)
  382. return -EINVAL;
  383. /* If the host has not yet processed the next entry then we are done */
  384. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  385. return -EBUSY;
  386. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  387. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  388. /* Update the host index to point to the next slot */
  389. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  390. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  391. /* Ring The Header Receive Queue Doorbell */
  392. if (!(hq->host_index % hq->entry_repost)) {
  393. doorbell.word0 = 0;
  394. bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
  395. hq->entry_repost);
  396. bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
  397. writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
  398. }
  399. return put_index;
  400. }
  401. /**
  402. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  403. * @q: The Header Receive Queue to operate on.
  404. *
  405. * This routine will update the HBA index of a queue to reflect consumption of
  406. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  407. * consumed an entry the host calls this function to update the queue's
  408. * internal pointers. This routine returns the number of entries that were
  409. * consumed by the HBA.
  410. **/
  411. static uint32_t
  412. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  413. {
  414. /* sanity check on queue memory */
  415. if (unlikely(!hq) || unlikely(!dq))
  416. return 0;
  417. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  418. return 0;
  419. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  420. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  421. return 1;
  422. }
  423. /**
  424. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  425. * @phba: Pointer to HBA context object.
  426. * @pring: Pointer to driver SLI ring object.
  427. *
  428. * This function returns pointer to next command iocb entry
  429. * in the command ring. The caller must hold hbalock to prevent
  430. * other threads consume the next command iocb.
  431. * SLI-2/SLI-3 provide different sized iocbs.
  432. **/
  433. static inline IOCB_t *
  434. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  435. {
  436. return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
  437. pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
  438. }
  439. /**
  440. * lpfc_resp_iocb - Get next response iocb entry in the ring
  441. * @phba: Pointer to HBA context object.
  442. * @pring: Pointer to driver SLI ring object.
  443. *
  444. * This function returns pointer to next response iocb entry
  445. * in the response ring. The caller must hold hbalock to make sure
  446. * that no other thread consume the next response iocb.
  447. * SLI-2/SLI-3 provide different sized iocbs.
  448. **/
  449. static inline IOCB_t *
  450. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  451. {
  452. return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
  453. pring->sli.sli3.rspidx * phba->iocb_rsp_size);
  454. }
  455. /**
  456. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  457. * @phba: Pointer to HBA context object.
  458. *
  459. * This function is called with hbalock held. This function
  460. * allocates a new driver iocb object from the iocb pool. If the
  461. * allocation is successful, it returns pointer to the newly
  462. * allocated iocb object else it returns NULL.
  463. **/
  464. struct lpfc_iocbq *
  465. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  466. {
  467. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  468. struct lpfc_iocbq * iocbq = NULL;
  469. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  470. if (iocbq)
  471. phba->iocb_cnt++;
  472. if (phba->iocb_cnt > phba->iocb_max)
  473. phba->iocb_max = phba->iocb_cnt;
  474. return iocbq;
  475. }
  476. /**
  477. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  478. * @phba: Pointer to HBA context object.
  479. * @xritag: XRI value.
  480. *
  481. * This function clears the sglq pointer from the array of acive
  482. * sglq's. The xritag that is passed in is used to index into the
  483. * array. Before the xritag can be used it needs to be adjusted
  484. * by subtracting the xribase.
  485. *
  486. * Returns sglq ponter = success, NULL = Failure.
  487. **/
  488. static struct lpfc_sglq *
  489. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  490. {
  491. struct lpfc_sglq *sglq;
  492. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  493. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  494. return sglq;
  495. }
  496. /**
  497. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  498. * @phba: Pointer to HBA context object.
  499. * @xritag: XRI value.
  500. *
  501. * This function returns the sglq pointer from the array of acive
  502. * sglq's. The xritag that is passed in is used to index into the
  503. * array. Before the xritag can be used it needs to be adjusted
  504. * by subtracting the xribase.
  505. *
  506. * Returns sglq ponter = success, NULL = Failure.
  507. **/
  508. struct lpfc_sglq *
  509. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  510. {
  511. struct lpfc_sglq *sglq;
  512. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  513. return sglq;
  514. }
  515. /**
  516. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  517. * @phba: Pointer to HBA context object.
  518. * @xritag: xri used in this exchange.
  519. * @rrq: The RRQ to be cleared.
  520. *
  521. **/
  522. void
  523. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  524. uint16_t xritag,
  525. struct lpfc_node_rrq *rrq)
  526. {
  527. struct lpfc_nodelist *ndlp = NULL;
  528. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  529. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  530. /* The target DID could have been swapped (cable swap)
  531. * we should use the ndlp from the findnode if it is
  532. * available.
  533. */
  534. if ((!ndlp) && rrq->ndlp)
  535. ndlp = rrq->ndlp;
  536. if (!ndlp)
  537. goto out;
  538. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  539. rrq->send_rrq = 0;
  540. rrq->xritag = 0;
  541. rrq->rrq_stop_time = 0;
  542. }
  543. out:
  544. mempool_free(rrq, phba->rrq_pool);
  545. }
  546. /**
  547. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  548. * @phba: Pointer to HBA context object.
  549. *
  550. * This function is called with hbalock held. This function
  551. * Checks if stop_time (ratov from setting rrq active) has
  552. * been reached, if it has and the send_rrq flag is set then
  553. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  554. * then it will just call the routine to clear the rrq and
  555. * free the rrq resource.
  556. * The timer is set to the next rrq that is going to expire before
  557. * leaving the routine.
  558. *
  559. **/
  560. void
  561. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  562. {
  563. struct lpfc_node_rrq *rrq;
  564. struct lpfc_node_rrq *nextrrq;
  565. unsigned long next_time;
  566. unsigned long iflags;
  567. LIST_HEAD(send_rrq);
  568. spin_lock_irqsave(&phba->hbalock, iflags);
  569. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  570. next_time = jiffies + HZ * (phba->fc_ratov + 1);
  571. list_for_each_entry_safe(rrq, nextrrq,
  572. &phba->active_rrq_list, list) {
  573. if (time_after(jiffies, rrq->rrq_stop_time))
  574. list_move(&rrq->list, &send_rrq);
  575. else if (time_before(rrq->rrq_stop_time, next_time))
  576. next_time = rrq->rrq_stop_time;
  577. }
  578. spin_unlock_irqrestore(&phba->hbalock, iflags);
  579. if (!list_empty(&phba->active_rrq_list))
  580. mod_timer(&phba->rrq_tmr, next_time);
  581. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  582. list_del(&rrq->list);
  583. if (!rrq->send_rrq)
  584. /* this call will free the rrq */
  585. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  586. else if (lpfc_send_rrq(phba, rrq)) {
  587. /* if we send the rrq then the completion handler
  588. * will clear the bit in the xribitmap.
  589. */
  590. lpfc_clr_rrq_active(phba, rrq->xritag,
  591. rrq);
  592. }
  593. }
  594. }
  595. /**
  596. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  597. * @vport: Pointer to vport context object.
  598. * @xri: The xri used in the exchange.
  599. * @did: The targets DID for this exchange.
  600. *
  601. * returns NULL = rrq not found in the phba->active_rrq_list.
  602. * rrq = rrq for this xri and target.
  603. **/
  604. struct lpfc_node_rrq *
  605. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  606. {
  607. struct lpfc_hba *phba = vport->phba;
  608. struct lpfc_node_rrq *rrq;
  609. struct lpfc_node_rrq *nextrrq;
  610. unsigned long iflags;
  611. if (phba->sli_rev != LPFC_SLI_REV4)
  612. return NULL;
  613. spin_lock_irqsave(&phba->hbalock, iflags);
  614. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  615. if (rrq->vport == vport && rrq->xritag == xri &&
  616. rrq->nlp_DID == did){
  617. list_del(&rrq->list);
  618. spin_unlock_irqrestore(&phba->hbalock, iflags);
  619. return rrq;
  620. }
  621. }
  622. spin_unlock_irqrestore(&phba->hbalock, iflags);
  623. return NULL;
  624. }
  625. /**
  626. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  627. * @vport: Pointer to vport context object.
  628. * @ndlp: Pointer to the lpfc_node_list structure.
  629. * If ndlp is NULL Remove all active RRQs for this vport from the
  630. * phba->active_rrq_list and clear the rrq.
  631. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  632. **/
  633. void
  634. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  635. {
  636. struct lpfc_hba *phba = vport->phba;
  637. struct lpfc_node_rrq *rrq;
  638. struct lpfc_node_rrq *nextrrq;
  639. unsigned long iflags;
  640. LIST_HEAD(rrq_list);
  641. if (phba->sli_rev != LPFC_SLI_REV4)
  642. return;
  643. if (!ndlp) {
  644. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  645. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  646. }
  647. spin_lock_irqsave(&phba->hbalock, iflags);
  648. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  649. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  650. list_move(&rrq->list, &rrq_list);
  651. spin_unlock_irqrestore(&phba->hbalock, iflags);
  652. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  653. list_del(&rrq->list);
  654. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  655. }
  656. }
  657. /**
  658. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  659. * @phba: Pointer to HBA context object.
  660. *
  661. * Remove all rrqs from the phba->active_rrq_list and free them by
  662. * calling __lpfc_clr_active_rrq
  663. *
  664. **/
  665. void
  666. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  667. {
  668. struct lpfc_node_rrq *rrq;
  669. struct lpfc_node_rrq *nextrrq;
  670. unsigned long next_time;
  671. unsigned long iflags;
  672. LIST_HEAD(rrq_list);
  673. if (phba->sli_rev != LPFC_SLI_REV4)
  674. return;
  675. spin_lock_irqsave(&phba->hbalock, iflags);
  676. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  677. next_time = jiffies + HZ * (phba->fc_ratov * 2);
  678. list_splice_init(&phba->active_rrq_list, &rrq_list);
  679. spin_unlock_irqrestore(&phba->hbalock, iflags);
  680. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  681. list_del(&rrq->list);
  682. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  683. }
  684. if (!list_empty(&phba->active_rrq_list))
  685. mod_timer(&phba->rrq_tmr, next_time);
  686. }
  687. /**
  688. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  689. * @phba: Pointer to HBA context object.
  690. * @ndlp: Targets nodelist pointer for this exchange.
  691. * @xritag the xri in the bitmap to test.
  692. *
  693. * This function is called with hbalock held. This function
  694. * returns 0 = rrq not active for this xri
  695. * 1 = rrq is valid for this xri.
  696. **/
  697. int
  698. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  699. uint16_t xritag)
  700. {
  701. if (!ndlp)
  702. return 0;
  703. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  704. return 1;
  705. else
  706. return 0;
  707. }
  708. /**
  709. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  710. * @phba: Pointer to HBA context object.
  711. * @ndlp: nodelist pointer for this target.
  712. * @xritag: xri used in this exchange.
  713. * @rxid: Remote Exchange ID.
  714. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  715. *
  716. * This function takes the hbalock.
  717. * The active bit is always set in the active rrq xri_bitmap even
  718. * if there is no slot avaiable for the other rrq information.
  719. *
  720. * returns 0 rrq actived for this xri
  721. * < 0 No memory or invalid ndlp.
  722. **/
  723. int
  724. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  725. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  726. {
  727. unsigned long iflags;
  728. struct lpfc_node_rrq *rrq;
  729. int empty;
  730. if (!ndlp)
  731. return -EINVAL;
  732. if (!phba->cfg_enable_rrq)
  733. return -EINVAL;
  734. spin_lock_irqsave(&phba->hbalock, iflags);
  735. if (phba->pport->load_flag & FC_UNLOADING) {
  736. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  737. goto out;
  738. }
  739. /*
  740. * set the active bit even if there is no mem available.
  741. */
  742. if (NLP_CHK_FREE_REQ(ndlp))
  743. goto out;
  744. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  745. goto out;
  746. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  747. goto out;
  748. spin_unlock_irqrestore(&phba->hbalock, iflags);
  749. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  750. if (!rrq) {
  751. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  752. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  753. " DID:0x%x Send:%d\n",
  754. xritag, rxid, ndlp->nlp_DID, send_rrq);
  755. return -EINVAL;
  756. }
  757. rrq->send_rrq = send_rrq;
  758. rrq->xritag = xritag;
  759. rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
  760. rrq->ndlp = ndlp;
  761. rrq->nlp_DID = ndlp->nlp_DID;
  762. rrq->vport = ndlp->vport;
  763. rrq->rxid = rxid;
  764. rrq->send_rrq = send_rrq;
  765. spin_lock_irqsave(&phba->hbalock, iflags);
  766. empty = list_empty(&phba->active_rrq_list);
  767. list_add_tail(&rrq->list, &phba->active_rrq_list);
  768. phba->hba_flag |= HBA_RRQ_ACTIVE;
  769. if (empty)
  770. lpfc_worker_wake_up(phba);
  771. spin_unlock_irqrestore(&phba->hbalock, iflags);
  772. return 0;
  773. out:
  774. spin_unlock_irqrestore(&phba->hbalock, iflags);
  775. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  776. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  777. " DID:0x%x Send:%d\n",
  778. xritag, rxid, ndlp->nlp_DID, send_rrq);
  779. return -EINVAL;
  780. }
  781. /**
  782. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  783. * @phba: Pointer to HBA context object.
  784. * @piocb: Pointer to the iocbq.
  785. *
  786. * This function is called with hbalock held. This function
  787. * gets a new driver sglq object from the sglq list. If the
  788. * list is not empty then it is successful, it returns pointer to the newly
  789. * allocated sglq object else it returns NULL.
  790. **/
  791. static struct lpfc_sglq *
  792. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  793. {
  794. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  795. struct lpfc_sglq *sglq = NULL;
  796. struct lpfc_sglq *start_sglq = NULL;
  797. struct lpfc_scsi_buf *lpfc_cmd;
  798. struct lpfc_nodelist *ndlp;
  799. int found = 0;
  800. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  801. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  802. ndlp = lpfc_cmd->rdata->pnode;
  803. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  804. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  805. ndlp = piocbq->context_un.ndlp;
  806. else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
  807. (piocbq->iocb_flag & LPFC_IO_LIBDFC))
  808. ndlp = piocbq->context_un.ndlp;
  809. else
  810. ndlp = piocbq->context1;
  811. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  812. start_sglq = sglq;
  813. while (!found) {
  814. if (!sglq)
  815. return NULL;
  816. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
  817. /* This xri has an rrq outstanding for this DID.
  818. * put it back in the list and get another xri.
  819. */
  820. list_add_tail(&sglq->list, lpfc_sgl_list);
  821. sglq = NULL;
  822. list_remove_head(lpfc_sgl_list, sglq,
  823. struct lpfc_sglq, list);
  824. if (sglq == start_sglq) {
  825. sglq = NULL;
  826. break;
  827. } else
  828. continue;
  829. }
  830. sglq->ndlp = ndlp;
  831. found = 1;
  832. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  833. sglq->state = SGL_ALLOCATED;
  834. }
  835. return sglq;
  836. }
  837. /**
  838. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  839. * @phba: Pointer to HBA context object.
  840. *
  841. * This function is called with no lock held. This function
  842. * allocates a new driver iocb object from the iocb pool. If the
  843. * allocation is successful, it returns pointer to the newly
  844. * allocated iocb object else it returns NULL.
  845. **/
  846. struct lpfc_iocbq *
  847. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  848. {
  849. struct lpfc_iocbq * iocbq = NULL;
  850. unsigned long iflags;
  851. spin_lock_irqsave(&phba->hbalock, iflags);
  852. iocbq = __lpfc_sli_get_iocbq(phba);
  853. spin_unlock_irqrestore(&phba->hbalock, iflags);
  854. return iocbq;
  855. }
  856. /**
  857. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  858. * @phba: Pointer to HBA context object.
  859. * @iocbq: Pointer to driver iocb object.
  860. *
  861. * This function is called with hbalock held to release driver
  862. * iocb object to the iocb pool. The iotag in the iocb object
  863. * does not change for each use of the iocb object. This function
  864. * clears all other fields of the iocb object when it is freed.
  865. * The sqlq structure that holds the xritag and phys and virtual
  866. * mappings for the scatter gather list is retrieved from the
  867. * active array of sglq. The get of the sglq pointer also clears
  868. * the entry in the array. If the status of the IO indiactes that
  869. * this IO was aborted then the sglq entry it put on the
  870. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  871. * IO has good status or fails for any other reason then the sglq
  872. * entry is added to the free list (lpfc_sgl_list).
  873. **/
  874. static void
  875. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  876. {
  877. struct lpfc_sglq *sglq;
  878. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  879. unsigned long iflag = 0;
  880. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  881. if (iocbq->sli4_xritag == NO_XRI)
  882. sglq = NULL;
  883. else
  884. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  885. if (sglq) {
  886. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  887. (sglq->state != SGL_XRI_ABORTED)) {
  888. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  889. iflag);
  890. list_add(&sglq->list,
  891. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  892. spin_unlock_irqrestore(
  893. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  894. } else {
  895. sglq->state = SGL_FREED;
  896. sglq->ndlp = NULL;
  897. list_add_tail(&sglq->list,
  898. &phba->sli4_hba.lpfc_sgl_list);
  899. /* Check if TXQ queue needs to be serviced */
  900. if (pring->txq_cnt)
  901. lpfc_worker_wake_up(phba);
  902. }
  903. }
  904. /*
  905. * Clean all volatile data fields, preserve iotag and node struct.
  906. */
  907. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  908. iocbq->sli4_lxritag = NO_XRI;
  909. iocbq->sli4_xritag = NO_XRI;
  910. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  911. }
  912. /**
  913. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  914. * @phba: Pointer to HBA context object.
  915. * @iocbq: Pointer to driver iocb object.
  916. *
  917. * This function is called with hbalock held to release driver
  918. * iocb object to the iocb pool. The iotag in the iocb object
  919. * does not change for each use of the iocb object. This function
  920. * clears all other fields of the iocb object when it is freed.
  921. **/
  922. static void
  923. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  924. {
  925. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  926. /*
  927. * Clean all volatile data fields, preserve iotag and node struct.
  928. */
  929. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  930. iocbq->sli4_xritag = NO_XRI;
  931. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  932. }
  933. /**
  934. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  935. * @phba: Pointer to HBA context object.
  936. * @iocbq: Pointer to driver iocb object.
  937. *
  938. * This function is called with hbalock held to release driver
  939. * iocb object to the iocb pool. The iotag in the iocb object
  940. * does not change for each use of the iocb object. This function
  941. * clears all other fields of the iocb object when it is freed.
  942. **/
  943. static void
  944. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  945. {
  946. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  947. phba->iocb_cnt--;
  948. }
  949. /**
  950. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  951. * @phba: Pointer to HBA context object.
  952. * @iocbq: Pointer to driver iocb object.
  953. *
  954. * This function is called with no lock held to release the iocb to
  955. * iocb pool.
  956. **/
  957. void
  958. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  959. {
  960. unsigned long iflags;
  961. /*
  962. * Clean all volatile data fields, preserve iotag and node struct.
  963. */
  964. spin_lock_irqsave(&phba->hbalock, iflags);
  965. __lpfc_sli_release_iocbq(phba, iocbq);
  966. spin_unlock_irqrestore(&phba->hbalock, iflags);
  967. }
  968. /**
  969. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  970. * @phba: Pointer to HBA context object.
  971. * @iocblist: List of IOCBs.
  972. * @ulpstatus: ULP status in IOCB command field.
  973. * @ulpWord4: ULP word-4 in IOCB command field.
  974. *
  975. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  976. * on the list by invoking the complete callback function associated with the
  977. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  978. * fields.
  979. **/
  980. void
  981. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  982. uint32_t ulpstatus, uint32_t ulpWord4)
  983. {
  984. struct lpfc_iocbq *piocb;
  985. while (!list_empty(iocblist)) {
  986. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  987. if (!piocb->iocb_cmpl)
  988. lpfc_sli_release_iocbq(phba, piocb);
  989. else {
  990. piocb->iocb.ulpStatus = ulpstatus;
  991. piocb->iocb.un.ulpWord[4] = ulpWord4;
  992. (piocb->iocb_cmpl) (phba, piocb, piocb);
  993. }
  994. }
  995. return;
  996. }
  997. /**
  998. * lpfc_sli_iocb_cmd_type - Get the iocb type
  999. * @iocb_cmnd: iocb command code.
  1000. *
  1001. * This function is called by ring event handler function to get the iocb type.
  1002. * This function translates the iocb command to an iocb command type used to
  1003. * decide the final disposition of each completed IOCB.
  1004. * The function returns
  1005. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  1006. * LPFC_SOL_IOCB if it is a solicited iocb completion
  1007. * LPFC_ABORT_IOCB if it is an abort iocb
  1008. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  1009. *
  1010. * The caller is not required to hold any lock.
  1011. **/
  1012. static lpfc_iocb_type
  1013. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1014. {
  1015. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1016. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1017. return 0;
  1018. switch (iocb_cmnd) {
  1019. case CMD_XMIT_SEQUENCE_CR:
  1020. case CMD_XMIT_SEQUENCE_CX:
  1021. case CMD_XMIT_BCAST_CN:
  1022. case CMD_XMIT_BCAST_CX:
  1023. case CMD_ELS_REQUEST_CR:
  1024. case CMD_ELS_REQUEST_CX:
  1025. case CMD_CREATE_XRI_CR:
  1026. case CMD_CREATE_XRI_CX:
  1027. case CMD_GET_RPI_CN:
  1028. case CMD_XMIT_ELS_RSP_CX:
  1029. case CMD_GET_RPI_CR:
  1030. case CMD_FCP_IWRITE_CR:
  1031. case CMD_FCP_IWRITE_CX:
  1032. case CMD_FCP_IREAD_CR:
  1033. case CMD_FCP_IREAD_CX:
  1034. case CMD_FCP_ICMND_CR:
  1035. case CMD_FCP_ICMND_CX:
  1036. case CMD_FCP_TSEND_CX:
  1037. case CMD_FCP_TRSP_CX:
  1038. case CMD_FCP_TRECEIVE_CX:
  1039. case CMD_FCP_AUTO_TRSP_CX:
  1040. case CMD_ADAPTER_MSG:
  1041. case CMD_ADAPTER_DUMP:
  1042. case CMD_XMIT_SEQUENCE64_CR:
  1043. case CMD_XMIT_SEQUENCE64_CX:
  1044. case CMD_XMIT_BCAST64_CN:
  1045. case CMD_XMIT_BCAST64_CX:
  1046. case CMD_ELS_REQUEST64_CR:
  1047. case CMD_ELS_REQUEST64_CX:
  1048. case CMD_FCP_IWRITE64_CR:
  1049. case CMD_FCP_IWRITE64_CX:
  1050. case CMD_FCP_IREAD64_CR:
  1051. case CMD_FCP_IREAD64_CX:
  1052. case CMD_FCP_ICMND64_CR:
  1053. case CMD_FCP_ICMND64_CX:
  1054. case CMD_FCP_TSEND64_CX:
  1055. case CMD_FCP_TRSP64_CX:
  1056. case CMD_FCP_TRECEIVE64_CX:
  1057. case CMD_GEN_REQUEST64_CR:
  1058. case CMD_GEN_REQUEST64_CX:
  1059. case CMD_XMIT_ELS_RSP64_CX:
  1060. case DSSCMD_IWRITE64_CR:
  1061. case DSSCMD_IWRITE64_CX:
  1062. case DSSCMD_IREAD64_CR:
  1063. case DSSCMD_IREAD64_CX:
  1064. type = LPFC_SOL_IOCB;
  1065. break;
  1066. case CMD_ABORT_XRI_CN:
  1067. case CMD_ABORT_XRI_CX:
  1068. case CMD_CLOSE_XRI_CN:
  1069. case CMD_CLOSE_XRI_CX:
  1070. case CMD_XRI_ABORTED_CX:
  1071. case CMD_ABORT_MXRI64_CN:
  1072. case CMD_XMIT_BLS_RSP64_CX:
  1073. type = LPFC_ABORT_IOCB;
  1074. break;
  1075. case CMD_RCV_SEQUENCE_CX:
  1076. case CMD_RCV_ELS_REQ_CX:
  1077. case CMD_RCV_SEQUENCE64_CX:
  1078. case CMD_RCV_ELS_REQ64_CX:
  1079. case CMD_ASYNC_STATUS:
  1080. case CMD_IOCB_RCV_SEQ64_CX:
  1081. case CMD_IOCB_RCV_ELS64_CX:
  1082. case CMD_IOCB_RCV_CONT64_CX:
  1083. case CMD_IOCB_RET_XRI64_CX:
  1084. type = LPFC_UNSOL_IOCB;
  1085. break;
  1086. case CMD_IOCB_XMIT_MSEQ64_CR:
  1087. case CMD_IOCB_XMIT_MSEQ64_CX:
  1088. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1089. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1090. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1091. case CMD_IOCB_ABORT_EXTENDED_CN:
  1092. case CMD_IOCB_RET_HBQE64_CN:
  1093. case CMD_IOCB_FCP_IBIDIR64_CR:
  1094. case CMD_IOCB_FCP_IBIDIR64_CX:
  1095. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1096. case CMD_IOCB_LOGENTRY_CN:
  1097. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1098. printk("%s - Unhandled SLI-3 Command x%x\n",
  1099. __func__, iocb_cmnd);
  1100. type = LPFC_UNKNOWN_IOCB;
  1101. break;
  1102. default:
  1103. type = LPFC_UNKNOWN_IOCB;
  1104. break;
  1105. }
  1106. return type;
  1107. }
  1108. /**
  1109. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1110. * @phba: Pointer to HBA context object.
  1111. *
  1112. * This function is called from SLI initialization code
  1113. * to configure every ring of the HBA's SLI interface. The
  1114. * caller is not required to hold any lock. This function issues
  1115. * a config_ring mailbox command for each ring.
  1116. * This function returns zero if successful else returns a negative
  1117. * error code.
  1118. **/
  1119. static int
  1120. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1121. {
  1122. struct lpfc_sli *psli = &phba->sli;
  1123. LPFC_MBOXQ_t *pmb;
  1124. MAILBOX_t *pmbox;
  1125. int i, rc, ret = 0;
  1126. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1127. if (!pmb)
  1128. return -ENOMEM;
  1129. pmbox = &pmb->u.mb;
  1130. phba->link_state = LPFC_INIT_MBX_CMDS;
  1131. for (i = 0; i < psli->num_rings; i++) {
  1132. lpfc_config_ring(phba, i, pmb);
  1133. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1134. if (rc != MBX_SUCCESS) {
  1135. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1136. "0446 Adapter failed to init (%d), "
  1137. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1138. "ring %d\n",
  1139. rc, pmbox->mbxCommand,
  1140. pmbox->mbxStatus, i);
  1141. phba->link_state = LPFC_HBA_ERROR;
  1142. ret = -ENXIO;
  1143. break;
  1144. }
  1145. }
  1146. mempool_free(pmb, phba->mbox_mem_pool);
  1147. return ret;
  1148. }
  1149. /**
  1150. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1151. * @phba: Pointer to HBA context object.
  1152. * @pring: Pointer to driver SLI ring object.
  1153. * @piocb: Pointer to the driver iocb object.
  1154. *
  1155. * This function is called with hbalock held. The function adds the
  1156. * new iocb to txcmplq of the given ring. This function always returns
  1157. * 0. If this function is called for ELS ring, this function checks if
  1158. * there is a vport associated with the ELS command. This function also
  1159. * starts els_tmofunc timer if this is an ELS command.
  1160. **/
  1161. static int
  1162. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1163. struct lpfc_iocbq *piocb)
  1164. {
  1165. list_add_tail(&piocb->list, &pring->txcmplq);
  1166. piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
  1167. pring->txcmplq_cnt++;
  1168. if (pring->txcmplq_cnt > pring->txcmplq_max)
  1169. pring->txcmplq_max = pring->txcmplq_cnt;
  1170. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1171. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1172. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1173. if (!piocb->vport)
  1174. BUG();
  1175. else
  1176. mod_timer(&piocb->vport->els_tmofunc,
  1177. jiffies + HZ * (phba->fc_ratov << 1));
  1178. }
  1179. return 0;
  1180. }
  1181. /**
  1182. * lpfc_sli_ringtx_get - Get first element of the txq
  1183. * @phba: Pointer to HBA context object.
  1184. * @pring: Pointer to driver SLI ring object.
  1185. *
  1186. * This function is called with hbalock held to get next
  1187. * iocb in txq of the given ring. If there is any iocb in
  1188. * the txq, the function returns first iocb in the list after
  1189. * removing the iocb from the list, else it returns NULL.
  1190. **/
  1191. struct lpfc_iocbq *
  1192. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1193. {
  1194. struct lpfc_iocbq *cmd_iocb;
  1195. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1196. if (cmd_iocb != NULL)
  1197. pring->txq_cnt--;
  1198. return cmd_iocb;
  1199. }
  1200. /**
  1201. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1202. * @phba: Pointer to HBA context object.
  1203. * @pring: Pointer to driver SLI ring object.
  1204. *
  1205. * This function is called with hbalock held and the caller must post the
  1206. * iocb without releasing the lock. If the caller releases the lock,
  1207. * iocb slot returned by the function is not guaranteed to be available.
  1208. * The function returns pointer to the next available iocb slot if there
  1209. * is available slot in the ring, else it returns NULL.
  1210. * If the get index of the ring is ahead of the put index, the function
  1211. * will post an error attention event to the worker thread to take the
  1212. * HBA to offline state.
  1213. **/
  1214. static IOCB_t *
  1215. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1216. {
  1217. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1218. uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
  1219. if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
  1220. (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
  1221. pring->sli.sli3.next_cmdidx = 0;
  1222. if (unlikely(pring->sli.sli3.local_getidx ==
  1223. pring->sli.sli3.next_cmdidx)) {
  1224. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1225. if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
  1226. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1227. "0315 Ring %d issue: portCmdGet %d "
  1228. "is bigger than cmd ring %d\n",
  1229. pring->ringno,
  1230. pring->sli.sli3.local_getidx,
  1231. max_cmd_idx);
  1232. phba->link_state = LPFC_HBA_ERROR;
  1233. /*
  1234. * All error attention handlers are posted to
  1235. * worker thread
  1236. */
  1237. phba->work_ha |= HA_ERATT;
  1238. phba->work_hs = HS_FFER3;
  1239. lpfc_worker_wake_up(phba);
  1240. return NULL;
  1241. }
  1242. if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
  1243. return NULL;
  1244. }
  1245. return lpfc_cmd_iocb(phba, pring);
  1246. }
  1247. /**
  1248. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1249. * @phba: Pointer to HBA context object.
  1250. * @iocbq: Pointer to driver iocb object.
  1251. *
  1252. * This function gets an iotag for the iocb. If there is no unused iotag and
  1253. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1254. * array and assigns a new iotag.
  1255. * The function returns the allocated iotag if successful, else returns zero.
  1256. * Zero is not a valid iotag.
  1257. * The caller is not required to hold any lock.
  1258. **/
  1259. uint16_t
  1260. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1261. {
  1262. struct lpfc_iocbq **new_arr;
  1263. struct lpfc_iocbq **old_arr;
  1264. size_t new_len;
  1265. struct lpfc_sli *psli = &phba->sli;
  1266. uint16_t iotag;
  1267. spin_lock_irq(&phba->hbalock);
  1268. iotag = psli->last_iotag;
  1269. if(++iotag < psli->iocbq_lookup_len) {
  1270. psli->last_iotag = iotag;
  1271. psli->iocbq_lookup[iotag] = iocbq;
  1272. spin_unlock_irq(&phba->hbalock);
  1273. iocbq->iotag = iotag;
  1274. return iotag;
  1275. } else if (psli->iocbq_lookup_len < (0xffff
  1276. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1277. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1278. spin_unlock_irq(&phba->hbalock);
  1279. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1280. GFP_KERNEL);
  1281. if (new_arr) {
  1282. spin_lock_irq(&phba->hbalock);
  1283. old_arr = psli->iocbq_lookup;
  1284. if (new_len <= psli->iocbq_lookup_len) {
  1285. /* highly unprobable case */
  1286. kfree(new_arr);
  1287. iotag = psli->last_iotag;
  1288. if(++iotag < psli->iocbq_lookup_len) {
  1289. psli->last_iotag = iotag;
  1290. psli->iocbq_lookup[iotag] = iocbq;
  1291. spin_unlock_irq(&phba->hbalock);
  1292. iocbq->iotag = iotag;
  1293. return iotag;
  1294. }
  1295. spin_unlock_irq(&phba->hbalock);
  1296. return 0;
  1297. }
  1298. if (psli->iocbq_lookup)
  1299. memcpy(new_arr, old_arr,
  1300. ((psli->last_iotag + 1) *
  1301. sizeof (struct lpfc_iocbq *)));
  1302. psli->iocbq_lookup = new_arr;
  1303. psli->iocbq_lookup_len = new_len;
  1304. psli->last_iotag = iotag;
  1305. psli->iocbq_lookup[iotag] = iocbq;
  1306. spin_unlock_irq(&phba->hbalock);
  1307. iocbq->iotag = iotag;
  1308. kfree(old_arr);
  1309. return iotag;
  1310. }
  1311. } else
  1312. spin_unlock_irq(&phba->hbalock);
  1313. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1314. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1315. psli->last_iotag);
  1316. return 0;
  1317. }
  1318. /**
  1319. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1320. * @phba: Pointer to HBA context object.
  1321. * @pring: Pointer to driver SLI ring object.
  1322. * @iocb: Pointer to iocb slot in the ring.
  1323. * @nextiocb: Pointer to driver iocb object which need to be
  1324. * posted to firmware.
  1325. *
  1326. * This function is called with hbalock held to post a new iocb to
  1327. * the firmware. This function copies the new iocb to ring iocb slot and
  1328. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1329. * a completion call back for this iocb else the function will free the
  1330. * iocb object.
  1331. **/
  1332. static void
  1333. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1334. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1335. {
  1336. /*
  1337. * Set up an iotag
  1338. */
  1339. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1340. if (pring->ringno == LPFC_ELS_RING) {
  1341. lpfc_debugfs_slow_ring_trc(phba,
  1342. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1343. *(((uint32_t *) &nextiocb->iocb) + 4),
  1344. *(((uint32_t *) &nextiocb->iocb) + 6),
  1345. *(((uint32_t *) &nextiocb->iocb) + 7));
  1346. }
  1347. /*
  1348. * Issue iocb command to adapter
  1349. */
  1350. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1351. wmb();
  1352. pring->stats.iocb_cmd++;
  1353. /*
  1354. * If there is no completion routine to call, we can release the
  1355. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1356. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1357. */
  1358. if (nextiocb->iocb_cmpl)
  1359. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1360. else
  1361. __lpfc_sli_release_iocbq(phba, nextiocb);
  1362. /*
  1363. * Let the HBA know what IOCB slot will be the next one the
  1364. * driver will put a command into.
  1365. */
  1366. pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
  1367. writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1368. }
  1369. /**
  1370. * lpfc_sli_update_full_ring - Update the chip attention register
  1371. * @phba: Pointer to HBA context object.
  1372. * @pring: Pointer to driver SLI ring object.
  1373. *
  1374. * The caller is not required to hold any lock for calling this function.
  1375. * This function updates the chip attention bits for the ring to inform firmware
  1376. * that there are pending work to be done for this ring and requests an
  1377. * interrupt when there is space available in the ring. This function is
  1378. * called when the driver is unable to post more iocbs to the ring due
  1379. * to unavailability of space in the ring.
  1380. **/
  1381. static void
  1382. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1383. {
  1384. int ringno = pring->ringno;
  1385. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1386. wmb();
  1387. /*
  1388. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1389. * The HBA will tell us when an IOCB entry is available.
  1390. */
  1391. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1392. readl(phba->CAregaddr); /* flush */
  1393. pring->stats.iocb_cmd_full++;
  1394. }
  1395. /**
  1396. * lpfc_sli_update_ring - Update chip attention register
  1397. * @phba: Pointer to HBA context object.
  1398. * @pring: Pointer to driver SLI ring object.
  1399. *
  1400. * This function updates the chip attention register bit for the
  1401. * given ring to inform HBA that there is more work to be done
  1402. * in this ring. The caller is not required to hold any lock.
  1403. **/
  1404. static void
  1405. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1406. {
  1407. int ringno = pring->ringno;
  1408. /*
  1409. * Tell the HBA that there is work to do in this ring.
  1410. */
  1411. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1412. wmb();
  1413. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1414. readl(phba->CAregaddr); /* flush */
  1415. }
  1416. }
  1417. /**
  1418. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1419. * @phba: Pointer to HBA context object.
  1420. * @pring: Pointer to driver SLI ring object.
  1421. *
  1422. * This function is called with hbalock held to post pending iocbs
  1423. * in the txq to the firmware. This function is called when driver
  1424. * detects space available in the ring.
  1425. **/
  1426. static void
  1427. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1428. {
  1429. IOCB_t *iocb;
  1430. struct lpfc_iocbq *nextiocb;
  1431. /*
  1432. * Check to see if:
  1433. * (a) there is anything on the txq to send
  1434. * (b) link is up
  1435. * (c) link attention events can be processed (fcp ring only)
  1436. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1437. */
  1438. if (pring->txq_cnt &&
  1439. lpfc_is_link_up(phba) &&
  1440. (pring->ringno != phba->sli.fcp_ring ||
  1441. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1442. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1443. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1444. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1445. if (iocb)
  1446. lpfc_sli_update_ring(phba, pring);
  1447. else
  1448. lpfc_sli_update_full_ring(phba, pring);
  1449. }
  1450. return;
  1451. }
  1452. /**
  1453. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1454. * @phba: Pointer to HBA context object.
  1455. * @hbqno: HBQ number.
  1456. *
  1457. * This function is called with hbalock held to get the next
  1458. * available slot for the given HBQ. If there is free slot
  1459. * available for the HBQ it will return pointer to the next available
  1460. * HBQ entry else it will return NULL.
  1461. **/
  1462. static struct lpfc_hbq_entry *
  1463. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1464. {
  1465. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1466. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1467. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1468. hbqp->next_hbqPutIdx = 0;
  1469. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1470. uint32_t raw_index = phba->hbq_get[hbqno];
  1471. uint32_t getidx = le32_to_cpu(raw_index);
  1472. hbqp->local_hbqGetIdx = getidx;
  1473. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1474. lpfc_printf_log(phba, KERN_ERR,
  1475. LOG_SLI | LOG_VPORT,
  1476. "1802 HBQ %d: local_hbqGetIdx "
  1477. "%u is > than hbqp->entry_count %u\n",
  1478. hbqno, hbqp->local_hbqGetIdx,
  1479. hbqp->entry_count);
  1480. phba->link_state = LPFC_HBA_ERROR;
  1481. return NULL;
  1482. }
  1483. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1484. return NULL;
  1485. }
  1486. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1487. hbqp->hbqPutIdx;
  1488. }
  1489. /**
  1490. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1491. * @phba: Pointer to HBA context object.
  1492. *
  1493. * This function is called with no lock held to free all the
  1494. * hbq buffers while uninitializing the SLI interface. It also
  1495. * frees the HBQ buffers returned by the firmware but not yet
  1496. * processed by the upper layers.
  1497. **/
  1498. void
  1499. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1500. {
  1501. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1502. struct hbq_dmabuf *hbq_buf;
  1503. unsigned long flags;
  1504. int i, hbq_count;
  1505. uint32_t hbqno;
  1506. hbq_count = lpfc_sli_hbq_count();
  1507. /* Return all memory used by all HBQs */
  1508. spin_lock_irqsave(&phba->hbalock, flags);
  1509. for (i = 0; i < hbq_count; ++i) {
  1510. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1511. &phba->hbqs[i].hbq_buffer_list, list) {
  1512. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1513. list_del(&hbq_buf->dbuf.list);
  1514. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1515. }
  1516. phba->hbqs[i].buffer_count = 0;
  1517. }
  1518. /* Return all HBQ buffer that are in-fly */
  1519. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1520. list) {
  1521. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1522. list_del(&hbq_buf->dbuf.list);
  1523. if (hbq_buf->tag == -1) {
  1524. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1525. (phba, hbq_buf);
  1526. } else {
  1527. hbqno = hbq_buf->tag >> 16;
  1528. if (hbqno >= LPFC_MAX_HBQS)
  1529. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1530. (phba, hbq_buf);
  1531. else
  1532. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1533. hbq_buf);
  1534. }
  1535. }
  1536. /* Mark the HBQs not in use */
  1537. phba->hbq_in_use = 0;
  1538. spin_unlock_irqrestore(&phba->hbalock, flags);
  1539. }
  1540. /**
  1541. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1542. * @phba: Pointer to HBA context object.
  1543. * @hbqno: HBQ number.
  1544. * @hbq_buf: Pointer to HBQ buffer.
  1545. *
  1546. * This function is called with the hbalock held to post a
  1547. * hbq buffer to the firmware. If the function finds an empty
  1548. * slot in the HBQ, it will post the buffer. The function will return
  1549. * pointer to the hbq entry if it successfully post the buffer
  1550. * else it will return NULL.
  1551. **/
  1552. static int
  1553. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1554. struct hbq_dmabuf *hbq_buf)
  1555. {
  1556. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1557. }
  1558. /**
  1559. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1560. * @phba: Pointer to HBA context object.
  1561. * @hbqno: HBQ number.
  1562. * @hbq_buf: Pointer to HBQ buffer.
  1563. *
  1564. * This function is called with the hbalock held to post a hbq buffer to the
  1565. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1566. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1567. * it successfully post the buffer else it will return an error.
  1568. **/
  1569. static int
  1570. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1571. struct hbq_dmabuf *hbq_buf)
  1572. {
  1573. struct lpfc_hbq_entry *hbqe;
  1574. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1575. /* Get next HBQ entry slot to use */
  1576. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1577. if (hbqe) {
  1578. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1579. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1580. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1581. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1582. hbqe->bde.tus.f.bdeFlags = 0;
  1583. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1584. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1585. /* Sync SLIM */
  1586. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1587. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1588. /* flush */
  1589. readl(phba->hbq_put + hbqno);
  1590. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1591. return 0;
  1592. } else
  1593. return -ENOMEM;
  1594. }
  1595. /**
  1596. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1597. * @phba: Pointer to HBA context object.
  1598. * @hbqno: HBQ number.
  1599. * @hbq_buf: Pointer to HBQ buffer.
  1600. *
  1601. * This function is called with the hbalock held to post an RQE to the SLI4
  1602. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1603. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1604. **/
  1605. static int
  1606. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1607. struct hbq_dmabuf *hbq_buf)
  1608. {
  1609. int rc;
  1610. struct lpfc_rqe hrqe;
  1611. struct lpfc_rqe drqe;
  1612. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1613. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1614. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1615. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1616. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1617. &hrqe, &drqe);
  1618. if (rc < 0)
  1619. return rc;
  1620. hbq_buf->tag = rc;
  1621. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1622. return 0;
  1623. }
  1624. /* HBQ for ELS and CT traffic. */
  1625. static struct lpfc_hbq_init lpfc_els_hbq = {
  1626. .rn = 1,
  1627. .entry_count = 256,
  1628. .mask_count = 0,
  1629. .profile = 0,
  1630. .ring_mask = (1 << LPFC_ELS_RING),
  1631. .buffer_count = 0,
  1632. .init_count = 40,
  1633. .add_count = 40,
  1634. };
  1635. /* HBQ for the extra ring if needed */
  1636. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1637. .rn = 1,
  1638. .entry_count = 200,
  1639. .mask_count = 0,
  1640. .profile = 0,
  1641. .ring_mask = (1 << LPFC_EXTRA_RING),
  1642. .buffer_count = 0,
  1643. .init_count = 0,
  1644. .add_count = 5,
  1645. };
  1646. /* Array of HBQs */
  1647. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1648. &lpfc_els_hbq,
  1649. &lpfc_extra_hbq,
  1650. };
  1651. /**
  1652. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1653. * @phba: Pointer to HBA context object.
  1654. * @hbqno: HBQ number.
  1655. * @count: Number of HBQ buffers to be posted.
  1656. *
  1657. * This function is called with no lock held to post more hbq buffers to the
  1658. * given HBQ. The function returns the number of HBQ buffers successfully
  1659. * posted.
  1660. **/
  1661. static int
  1662. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1663. {
  1664. uint32_t i, posted = 0;
  1665. unsigned long flags;
  1666. struct hbq_dmabuf *hbq_buffer;
  1667. LIST_HEAD(hbq_buf_list);
  1668. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1669. return 0;
  1670. if ((phba->hbqs[hbqno].buffer_count + count) >
  1671. lpfc_hbq_defs[hbqno]->entry_count)
  1672. count = lpfc_hbq_defs[hbqno]->entry_count -
  1673. phba->hbqs[hbqno].buffer_count;
  1674. if (!count)
  1675. return 0;
  1676. /* Allocate HBQ entries */
  1677. for (i = 0; i < count; i++) {
  1678. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1679. if (!hbq_buffer)
  1680. break;
  1681. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1682. }
  1683. /* Check whether HBQ is still in use */
  1684. spin_lock_irqsave(&phba->hbalock, flags);
  1685. if (!phba->hbq_in_use)
  1686. goto err;
  1687. while (!list_empty(&hbq_buf_list)) {
  1688. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1689. dbuf.list);
  1690. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1691. (hbqno << 16));
  1692. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1693. phba->hbqs[hbqno].buffer_count++;
  1694. posted++;
  1695. } else
  1696. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1697. }
  1698. spin_unlock_irqrestore(&phba->hbalock, flags);
  1699. return posted;
  1700. err:
  1701. spin_unlock_irqrestore(&phba->hbalock, flags);
  1702. while (!list_empty(&hbq_buf_list)) {
  1703. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1704. dbuf.list);
  1705. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1706. }
  1707. return 0;
  1708. }
  1709. /**
  1710. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1711. * @phba: Pointer to HBA context object.
  1712. * @qno: HBQ number.
  1713. *
  1714. * This function posts more buffers to the HBQ. This function
  1715. * is called with no lock held. The function returns the number of HBQ entries
  1716. * successfully allocated.
  1717. **/
  1718. int
  1719. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1720. {
  1721. if (phba->sli_rev == LPFC_SLI_REV4)
  1722. return 0;
  1723. else
  1724. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1725. lpfc_hbq_defs[qno]->add_count);
  1726. }
  1727. /**
  1728. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1729. * @phba: Pointer to HBA context object.
  1730. * @qno: HBQ queue number.
  1731. *
  1732. * This function is called from SLI initialization code path with
  1733. * no lock held to post initial HBQ buffers to firmware. The
  1734. * function returns the number of HBQ entries successfully allocated.
  1735. **/
  1736. static int
  1737. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1738. {
  1739. if (phba->sli_rev == LPFC_SLI_REV4)
  1740. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1741. lpfc_hbq_defs[qno]->entry_count);
  1742. else
  1743. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1744. lpfc_hbq_defs[qno]->init_count);
  1745. }
  1746. /**
  1747. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1748. * @phba: Pointer to HBA context object.
  1749. * @hbqno: HBQ number.
  1750. *
  1751. * This function removes the first hbq buffer on an hbq list and returns a
  1752. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1753. **/
  1754. static struct hbq_dmabuf *
  1755. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1756. {
  1757. struct lpfc_dmabuf *d_buf;
  1758. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1759. if (!d_buf)
  1760. return NULL;
  1761. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1762. }
  1763. /**
  1764. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1765. * @phba: Pointer to HBA context object.
  1766. * @tag: Tag of the hbq buffer.
  1767. *
  1768. * This function is called with hbalock held. This function searches
  1769. * for the hbq buffer associated with the given tag in the hbq buffer
  1770. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1771. * it returns NULL.
  1772. **/
  1773. static struct hbq_dmabuf *
  1774. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1775. {
  1776. struct lpfc_dmabuf *d_buf;
  1777. struct hbq_dmabuf *hbq_buf;
  1778. uint32_t hbqno;
  1779. hbqno = tag >> 16;
  1780. if (hbqno >= LPFC_MAX_HBQS)
  1781. return NULL;
  1782. spin_lock_irq(&phba->hbalock);
  1783. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1784. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1785. if (hbq_buf->tag == tag) {
  1786. spin_unlock_irq(&phba->hbalock);
  1787. return hbq_buf;
  1788. }
  1789. }
  1790. spin_unlock_irq(&phba->hbalock);
  1791. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1792. "1803 Bad hbq tag. Data: x%x x%x\n",
  1793. tag, phba->hbqs[tag >> 16].buffer_count);
  1794. return NULL;
  1795. }
  1796. /**
  1797. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1798. * @phba: Pointer to HBA context object.
  1799. * @hbq_buffer: Pointer to HBQ buffer.
  1800. *
  1801. * This function is called with hbalock. This function gives back
  1802. * the hbq buffer to firmware. If the HBQ does not have space to
  1803. * post the buffer, it will free the buffer.
  1804. **/
  1805. void
  1806. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1807. {
  1808. uint32_t hbqno;
  1809. if (hbq_buffer) {
  1810. hbqno = hbq_buffer->tag >> 16;
  1811. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1812. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1813. }
  1814. }
  1815. /**
  1816. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1817. * @mbxCommand: mailbox command code.
  1818. *
  1819. * This function is called by the mailbox event handler function to verify
  1820. * that the completed mailbox command is a legitimate mailbox command. If the
  1821. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1822. * and the mailbox event handler will take the HBA offline.
  1823. **/
  1824. static int
  1825. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1826. {
  1827. uint8_t ret;
  1828. switch (mbxCommand) {
  1829. case MBX_LOAD_SM:
  1830. case MBX_READ_NV:
  1831. case MBX_WRITE_NV:
  1832. case MBX_WRITE_VPARMS:
  1833. case MBX_RUN_BIU_DIAG:
  1834. case MBX_INIT_LINK:
  1835. case MBX_DOWN_LINK:
  1836. case MBX_CONFIG_LINK:
  1837. case MBX_CONFIG_RING:
  1838. case MBX_RESET_RING:
  1839. case MBX_READ_CONFIG:
  1840. case MBX_READ_RCONFIG:
  1841. case MBX_READ_SPARM:
  1842. case MBX_READ_STATUS:
  1843. case MBX_READ_RPI:
  1844. case MBX_READ_XRI:
  1845. case MBX_READ_REV:
  1846. case MBX_READ_LNK_STAT:
  1847. case MBX_REG_LOGIN:
  1848. case MBX_UNREG_LOGIN:
  1849. case MBX_CLEAR_LA:
  1850. case MBX_DUMP_MEMORY:
  1851. case MBX_DUMP_CONTEXT:
  1852. case MBX_RUN_DIAGS:
  1853. case MBX_RESTART:
  1854. case MBX_UPDATE_CFG:
  1855. case MBX_DOWN_LOAD:
  1856. case MBX_DEL_LD_ENTRY:
  1857. case MBX_RUN_PROGRAM:
  1858. case MBX_SET_MASK:
  1859. case MBX_SET_VARIABLE:
  1860. case MBX_UNREG_D_ID:
  1861. case MBX_KILL_BOARD:
  1862. case MBX_CONFIG_FARP:
  1863. case MBX_BEACON:
  1864. case MBX_LOAD_AREA:
  1865. case MBX_RUN_BIU_DIAG64:
  1866. case MBX_CONFIG_PORT:
  1867. case MBX_READ_SPARM64:
  1868. case MBX_READ_RPI64:
  1869. case MBX_REG_LOGIN64:
  1870. case MBX_READ_TOPOLOGY:
  1871. case MBX_WRITE_WWN:
  1872. case MBX_SET_DEBUG:
  1873. case MBX_LOAD_EXP_ROM:
  1874. case MBX_ASYNCEVT_ENABLE:
  1875. case MBX_REG_VPI:
  1876. case MBX_UNREG_VPI:
  1877. case MBX_HEARTBEAT:
  1878. case MBX_PORT_CAPABILITIES:
  1879. case MBX_PORT_IOV_CONTROL:
  1880. case MBX_SLI4_CONFIG:
  1881. case MBX_SLI4_REQ_FTRS:
  1882. case MBX_REG_FCFI:
  1883. case MBX_UNREG_FCFI:
  1884. case MBX_REG_VFI:
  1885. case MBX_UNREG_VFI:
  1886. case MBX_INIT_VPI:
  1887. case MBX_INIT_VFI:
  1888. case MBX_RESUME_RPI:
  1889. case MBX_READ_EVENT_LOG_STATUS:
  1890. case MBX_READ_EVENT_LOG:
  1891. case MBX_SECURITY_MGMT:
  1892. case MBX_AUTH_PORT:
  1893. ret = mbxCommand;
  1894. break;
  1895. default:
  1896. ret = MBX_SHUTDOWN;
  1897. break;
  1898. }
  1899. return ret;
  1900. }
  1901. /**
  1902. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1903. * @phba: Pointer to HBA context object.
  1904. * @pmboxq: Pointer to mailbox command.
  1905. *
  1906. * This is completion handler function for mailbox commands issued from
  1907. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1908. * mailbox event handler function with no lock held. This function
  1909. * will wake up thread waiting on the wait queue pointed by context1
  1910. * of the mailbox.
  1911. **/
  1912. void
  1913. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1914. {
  1915. wait_queue_head_t *pdone_q;
  1916. unsigned long drvr_flag;
  1917. /*
  1918. * If pdone_q is empty, the driver thread gave up waiting and
  1919. * continued running.
  1920. */
  1921. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1922. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1923. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1924. if (pdone_q)
  1925. wake_up_interruptible(pdone_q);
  1926. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1927. return;
  1928. }
  1929. /**
  1930. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1931. * @phba: Pointer to HBA context object.
  1932. * @pmb: Pointer to mailbox object.
  1933. *
  1934. * This function is the default mailbox completion handler. It
  1935. * frees the memory resources associated with the completed mailbox
  1936. * command. If the completed command is a REG_LOGIN mailbox command,
  1937. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1938. **/
  1939. void
  1940. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1941. {
  1942. struct lpfc_vport *vport = pmb->vport;
  1943. struct lpfc_dmabuf *mp;
  1944. struct lpfc_nodelist *ndlp;
  1945. struct Scsi_Host *shost;
  1946. uint16_t rpi, vpi;
  1947. int rc;
  1948. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1949. if (mp) {
  1950. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1951. kfree(mp);
  1952. }
  1953. /*
  1954. * If a REG_LOGIN succeeded after node is destroyed or node
  1955. * is in re-discovery driver need to cleanup the RPI.
  1956. */
  1957. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1958. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1959. !pmb->u.mb.mbxStatus) {
  1960. rpi = pmb->u.mb.un.varWords[0];
  1961. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1962. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1963. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1964. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1965. if (rc != MBX_NOT_FINISHED)
  1966. return;
  1967. }
  1968. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  1969. !(phba->pport->load_flag & FC_UNLOADING) &&
  1970. !pmb->u.mb.mbxStatus) {
  1971. shost = lpfc_shost_from_vport(vport);
  1972. spin_lock_irq(shost->host_lock);
  1973. vport->vpi_state |= LPFC_VPI_REGISTERED;
  1974. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  1975. spin_unlock_irq(shost->host_lock);
  1976. }
  1977. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  1978. ndlp = (struct lpfc_nodelist *)pmb->context2;
  1979. lpfc_nlp_put(ndlp);
  1980. pmb->context2 = NULL;
  1981. }
  1982. /* Check security permission status on INIT_LINK mailbox command */
  1983. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  1984. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  1985. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1986. "2860 SLI authentication is required "
  1987. "for INIT_LINK but has not done yet\n");
  1988. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  1989. lpfc_sli4_mbox_cmd_free(phba, pmb);
  1990. else
  1991. mempool_free(pmb, phba->mbox_mem_pool);
  1992. }
  1993. /**
  1994. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  1995. * @phba: Pointer to HBA context object.
  1996. *
  1997. * This function is called with no lock held. This function processes all
  1998. * the completed mailbox commands and gives it to upper layers. The interrupt
  1999. * service routine processes mailbox completion interrupt and adds completed
  2000. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  2001. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  2002. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  2003. * function returns the mailbox commands to the upper layer by calling the
  2004. * completion handler function of each mailbox.
  2005. **/
  2006. int
  2007. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  2008. {
  2009. MAILBOX_t *pmbox;
  2010. LPFC_MBOXQ_t *pmb;
  2011. int rc;
  2012. LIST_HEAD(cmplq);
  2013. phba->sli.slistat.mbox_event++;
  2014. /* Get all completed mailboxe buffers into the cmplq */
  2015. spin_lock_irq(&phba->hbalock);
  2016. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2017. spin_unlock_irq(&phba->hbalock);
  2018. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2019. do {
  2020. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2021. if (pmb == NULL)
  2022. break;
  2023. pmbox = &pmb->u.mb;
  2024. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2025. if (pmb->vport) {
  2026. lpfc_debugfs_disc_trc(pmb->vport,
  2027. LPFC_DISC_TRC_MBOX_VPORT,
  2028. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2029. (uint32_t)pmbox->mbxCommand,
  2030. pmbox->un.varWords[0],
  2031. pmbox->un.varWords[1]);
  2032. }
  2033. else {
  2034. lpfc_debugfs_disc_trc(phba->pport,
  2035. LPFC_DISC_TRC_MBOX,
  2036. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2037. (uint32_t)pmbox->mbxCommand,
  2038. pmbox->un.varWords[0],
  2039. pmbox->un.varWords[1]);
  2040. }
  2041. }
  2042. /*
  2043. * It is a fatal error if unknown mbox command completion.
  2044. */
  2045. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2046. MBX_SHUTDOWN) {
  2047. /* Unknown mailbox command compl */
  2048. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2049. "(%d):0323 Unknown Mailbox command "
  2050. "x%x (x%x/x%x) Cmpl\n",
  2051. pmb->vport ? pmb->vport->vpi : 0,
  2052. pmbox->mbxCommand,
  2053. lpfc_sli_config_mbox_subsys_get(phba,
  2054. pmb),
  2055. lpfc_sli_config_mbox_opcode_get(phba,
  2056. pmb));
  2057. phba->link_state = LPFC_HBA_ERROR;
  2058. phba->work_hs = HS_FFER3;
  2059. lpfc_handle_eratt(phba);
  2060. continue;
  2061. }
  2062. if (pmbox->mbxStatus) {
  2063. phba->sli.slistat.mbox_stat_err++;
  2064. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2065. /* Mbox cmd cmpl error - RETRYing */
  2066. lpfc_printf_log(phba, KERN_INFO,
  2067. LOG_MBOX | LOG_SLI,
  2068. "(%d):0305 Mbox cmd cmpl "
  2069. "error - RETRYing Data: x%x "
  2070. "(x%x/x%x) x%x x%x x%x\n",
  2071. pmb->vport ? pmb->vport->vpi : 0,
  2072. pmbox->mbxCommand,
  2073. lpfc_sli_config_mbox_subsys_get(phba,
  2074. pmb),
  2075. lpfc_sli_config_mbox_opcode_get(phba,
  2076. pmb),
  2077. pmbox->mbxStatus,
  2078. pmbox->un.varWords[0],
  2079. pmb->vport->port_state);
  2080. pmbox->mbxStatus = 0;
  2081. pmbox->mbxOwner = OWN_HOST;
  2082. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2083. if (rc != MBX_NOT_FINISHED)
  2084. continue;
  2085. }
  2086. }
  2087. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2088. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2089. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2090. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2091. pmb->vport ? pmb->vport->vpi : 0,
  2092. pmbox->mbxCommand,
  2093. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2094. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2095. pmb->mbox_cmpl,
  2096. *((uint32_t *) pmbox),
  2097. pmbox->un.varWords[0],
  2098. pmbox->un.varWords[1],
  2099. pmbox->un.varWords[2],
  2100. pmbox->un.varWords[3],
  2101. pmbox->un.varWords[4],
  2102. pmbox->un.varWords[5],
  2103. pmbox->un.varWords[6],
  2104. pmbox->un.varWords[7]);
  2105. if (pmb->mbox_cmpl)
  2106. pmb->mbox_cmpl(phba,pmb);
  2107. } while (1);
  2108. return 0;
  2109. }
  2110. /**
  2111. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2112. * @phba: Pointer to HBA context object.
  2113. * @pring: Pointer to driver SLI ring object.
  2114. * @tag: buffer tag.
  2115. *
  2116. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2117. * is set in the tag the buffer is posted for a particular exchange,
  2118. * the function will return the buffer without replacing the buffer.
  2119. * If the buffer is for unsolicited ELS or CT traffic, this function
  2120. * returns the buffer and also posts another buffer to the firmware.
  2121. **/
  2122. static struct lpfc_dmabuf *
  2123. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2124. struct lpfc_sli_ring *pring,
  2125. uint32_t tag)
  2126. {
  2127. struct hbq_dmabuf *hbq_entry;
  2128. if (tag & QUE_BUFTAG_BIT)
  2129. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2130. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2131. if (!hbq_entry)
  2132. return NULL;
  2133. return &hbq_entry->dbuf;
  2134. }
  2135. /**
  2136. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2137. * @phba: Pointer to HBA context object.
  2138. * @pring: Pointer to driver SLI ring object.
  2139. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2140. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2141. * @fch_type: the type for the first frame of the sequence.
  2142. *
  2143. * This function is called with no lock held. This function uses the r_ctl and
  2144. * type of the received sequence to find the correct callback function to call
  2145. * to process the sequence.
  2146. **/
  2147. static int
  2148. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2149. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2150. uint32_t fch_type)
  2151. {
  2152. int i;
  2153. /* unSolicited Responses */
  2154. if (pring->prt[0].profile) {
  2155. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2156. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2157. saveq);
  2158. return 1;
  2159. }
  2160. /* We must search, based on rctl / type
  2161. for the right routine */
  2162. for (i = 0; i < pring->num_mask; i++) {
  2163. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2164. (pring->prt[i].type == fch_type)) {
  2165. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2166. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2167. (phba, pring, saveq);
  2168. return 1;
  2169. }
  2170. }
  2171. return 0;
  2172. }
  2173. /**
  2174. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2175. * @phba: Pointer to HBA context object.
  2176. * @pring: Pointer to driver SLI ring object.
  2177. * @saveq: Pointer to the unsolicited iocb.
  2178. *
  2179. * This function is called with no lock held by the ring event handler
  2180. * when there is an unsolicited iocb posted to the response ring by the
  2181. * firmware. This function gets the buffer associated with the iocbs
  2182. * and calls the event handler for the ring. This function handles both
  2183. * qring buffers and hbq buffers.
  2184. * When the function returns 1 the caller can free the iocb object otherwise
  2185. * upper layer functions will free the iocb objects.
  2186. **/
  2187. static int
  2188. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2189. struct lpfc_iocbq *saveq)
  2190. {
  2191. IOCB_t * irsp;
  2192. WORD5 * w5p;
  2193. uint32_t Rctl, Type;
  2194. uint32_t match;
  2195. struct lpfc_iocbq *iocbq;
  2196. struct lpfc_dmabuf *dmzbuf;
  2197. match = 0;
  2198. irsp = &(saveq->iocb);
  2199. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2200. if (pring->lpfc_sli_rcv_async_status)
  2201. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2202. else
  2203. lpfc_printf_log(phba,
  2204. KERN_WARNING,
  2205. LOG_SLI,
  2206. "0316 Ring %d handler: unexpected "
  2207. "ASYNC_STATUS iocb received evt_code "
  2208. "0x%x\n",
  2209. pring->ringno,
  2210. irsp->un.asyncstat.evt_code);
  2211. return 1;
  2212. }
  2213. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2214. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2215. if (irsp->ulpBdeCount > 0) {
  2216. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2217. irsp->un.ulpWord[3]);
  2218. lpfc_in_buf_free(phba, dmzbuf);
  2219. }
  2220. if (irsp->ulpBdeCount > 1) {
  2221. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2222. irsp->unsli3.sli3Words[3]);
  2223. lpfc_in_buf_free(phba, dmzbuf);
  2224. }
  2225. if (irsp->ulpBdeCount > 2) {
  2226. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2227. irsp->unsli3.sli3Words[7]);
  2228. lpfc_in_buf_free(phba, dmzbuf);
  2229. }
  2230. return 1;
  2231. }
  2232. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2233. if (irsp->ulpBdeCount != 0) {
  2234. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2235. irsp->un.ulpWord[3]);
  2236. if (!saveq->context2)
  2237. lpfc_printf_log(phba,
  2238. KERN_ERR,
  2239. LOG_SLI,
  2240. "0341 Ring %d Cannot find buffer for "
  2241. "an unsolicited iocb. tag 0x%x\n",
  2242. pring->ringno,
  2243. irsp->un.ulpWord[3]);
  2244. }
  2245. if (irsp->ulpBdeCount == 2) {
  2246. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2247. irsp->unsli3.sli3Words[7]);
  2248. if (!saveq->context3)
  2249. lpfc_printf_log(phba,
  2250. KERN_ERR,
  2251. LOG_SLI,
  2252. "0342 Ring %d Cannot find buffer for an"
  2253. " unsolicited iocb. tag 0x%x\n",
  2254. pring->ringno,
  2255. irsp->unsli3.sli3Words[7]);
  2256. }
  2257. list_for_each_entry(iocbq, &saveq->list, list) {
  2258. irsp = &(iocbq->iocb);
  2259. if (irsp->ulpBdeCount != 0) {
  2260. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2261. irsp->un.ulpWord[3]);
  2262. if (!iocbq->context2)
  2263. lpfc_printf_log(phba,
  2264. KERN_ERR,
  2265. LOG_SLI,
  2266. "0343 Ring %d Cannot find "
  2267. "buffer for an unsolicited iocb"
  2268. ". tag 0x%x\n", pring->ringno,
  2269. irsp->un.ulpWord[3]);
  2270. }
  2271. if (irsp->ulpBdeCount == 2) {
  2272. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2273. irsp->unsli3.sli3Words[7]);
  2274. if (!iocbq->context3)
  2275. lpfc_printf_log(phba,
  2276. KERN_ERR,
  2277. LOG_SLI,
  2278. "0344 Ring %d Cannot find "
  2279. "buffer for an unsolicited "
  2280. "iocb. tag 0x%x\n",
  2281. pring->ringno,
  2282. irsp->unsli3.sli3Words[7]);
  2283. }
  2284. }
  2285. }
  2286. if (irsp->ulpBdeCount != 0 &&
  2287. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2288. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2289. int found = 0;
  2290. /* search continue save q for same XRI */
  2291. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2292. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2293. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2294. list_add_tail(&saveq->list, &iocbq->list);
  2295. found = 1;
  2296. break;
  2297. }
  2298. }
  2299. if (!found)
  2300. list_add_tail(&saveq->clist,
  2301. &pring->iocb_continue_saveq);
  2302. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2303. list_del_init(&iocbq->clist);
  2304. saveq = iocbq;
  2305. irsp = &(saveq->iocb);
  2306. } else
  2307. return 0;
  2308. }
  2309. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2310. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2311. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2312. Rctl = FC_RCTL_ELS_REQ;
  2313. Type = FC_TYPE_ELS;
  2314. } else {
  2315. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2316. Rctl = w5p->hcsw.Rctl;
  2317. Type = w5p->hcsw.Type;
  2318. /* Firmware Workaround */
  2319. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2320. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2321. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2322. Rctl = FC_RCTL_ELS_REQ;
  2323. Type = FC_TYPE_ELS;
  2324. w5p->hcsw.Rctl = Rctl;
  2325. w5p->hcsw.Type = Type;
  2326. }
  2327. }
  2328. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2329. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2330. "0313 Ring %d handler: unexpected Rctl x%x "
  2331. "Type x%x received\n",
  2332. pring->ringno, Rctl, Type);
  2333. return 1;
  2334. }
  2335. /**
  2336. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2337. * @phba: Pointer to HBA context object.
  2338. * @pring: Pointer to driver SLI ring object.
  2339. * @prspiocb: Pointer to response iocb object.
  2340. *
  2341. * This function looks up the iocb_lookup table to get the command iocb
  2342. * corresponding to the given response iocb using the iotag of the
  2343. * response iocb. This function is called with the hbalock held.
  2344. * This function returns the command iocb object if it finds the command
  2345. * iocb else returns NULL.
  2346. **/
  2347. static struct lpfc_iocbq *
  2348. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2349. struct lpfc_sli_ring *pring,
  2350. struct lpfc_iocbq *prspiocb)
  2351. {
  2352. struct lpfc_iocbq *cmd_iocb = NULL;
  2353. uint16_t iotag;
  2354. iotag = prspiocb->iocb.ulpIoTag;
  2355. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2356. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2357. list_del_init(&cmd_iocb->list);
  2358. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2359. pring->txcmplq_cnt--;
  2360. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2361. }
  2362. return cmd_iocb;
  2363. }
  2364. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2365. "0317 iotag x%x is out off "
  2366. "range: max iotag x%x wd0 x%x\n",
  2367. iotag, phba->sli.last_iotag,
  2368. *(((uint32_t *) &prspiocb->iocb) + 7));
  2369. return NULL;
  2370. }
  2371. /**
  2372. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2373. * @phba: Pointer to HBA context object.
  2374. * @pring: Pointer to driver SLI ring object.
  2375. * @iotag: IOCB tag.
  2376. *
  2377. * This function looks up the iocb_lookup table to get the command iocb
  2378. * corresponding to the given iotag. This function is called with the
  2379. * hbalock held.
  2380. * This function returns the command iocb object if it finds the command
  2381. * iocb else returns NULL.
  2382. **/
  2383. static struct lpfc_iocbq *
  2384. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2385. struct lpfc_sli_ring *pring, uint16_t iotag)
  2386. {
  2387. struct lpfc_iocbq *cmd_iocb;
  2388. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2389. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2390. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2391. /* remove from txcmpl queue list */
  2392. list_del_init(&cmd_iocb->list);
  2393. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2394. pring->txcmplq_cnt--;
  2395. return cmd_iocb;
  2396. }
  2397. }
  2398. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2399. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2400. iotag, phba->sli.last_iotag);
  2401. return NULL;
  2402. }
  2403. /**
  2404. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2405. * @phba: Pointer to HBA context object.
  2406. * @pring: Pointer to driver SLI ring object.
  2407. * @saveq: Pointer to the response iocb to be processed.
  2408. *
  2409. * This function is called by the ring event handler for non-fcp
  2410. * rings when there is a new response iocb in the response ring.
  2411. * The caller is not required to hold any locks. This function
  2412. * gets the command iocb associated with the response iocb and
  2413. * calls the completion handler for the command iocb. If there
  2414. * is no completion handler, the function will free the resources
  2415. * associated with command iocb. If the response iocb is for
  2416. * an already aborted command iocb, the status of the completion
  2417. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2418. * This function always returns 1.
  2419. **/
  2420. static int
  2421. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2422. struct lpfc_iocbq *saveq)
  2423. {
  2424. struct lpfc_iocbq *cmdiocbp;
  2425. int rc = 1;
  2426. unsigned long iflag;
  2427. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2428. spin_lock_irqsave(&phba->hbalock, iflag);
  2429. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2430. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2431. if (cmdiocbp) {
  2432. if (cmdiocbp->iocb_cmpl) {
  2433. /*
  2434. * If an ELS command failed send an event to mgmt
  2435. * application.
  2436. */
  2437. if (saveq->iocb.ulpStatus &&
  2438. (pring->ringno == LPFC_ELS_RING) &&
  2439. (cmdiocbp->iocb.ulpCommand ==
  2440. CMD_ELS_REQUEST64_CR))
  2441. lpfc_send_els_failure_event(phba,
  2442. cmdiocbp, saveq);
  2443. /*
  2444. * Post all ELS completions to the worker thread.
  2445. * All other are passed to the completion callback.
  2446. */
  2447. if (pring->ringno == LPFC_ELS_RING) {
  2448. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2449. (cmdiocbp->iocb_flag &
  2450. LPFC_DRIVER_ABORTED)) {
  2451. spin_lock_irqsave(&phba->hbalock,
  2452. iflag);
  2453. cmdiocbp->iocb_flag &=
  2454. ~LPFC_DRIVER_ABORTED;
  2455. spin_unlock_irqrestore(&phba->hbalock,
  2456. iflag);
  2457. saveq->iocb.ulpStatus =
  2458. IOSTAT_LOCAL_REJECT;
  2459. saveq->iocb.un.ulpWord[4] =
  2460. IOERR_SLI_ABORTED;
  2461. /* Firmware could still be in progress
  2462. * of DMAing payload, so don't free data
  2463. * buffer till after a hbeat.
  2464. */
  2465. spin_lock_irqsave(&phba->hbalock,
  2466. iflag);
  2467. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2468. spin_unlock_irqrestore(&phba->hbalock,
  2469. iflag);
  2470. }
  2471. if (phba->sli_rev == LPFC_SLI_REV4) {
  2472. if (saveq->iocb_flag &
  2473. LPFC_EXCHANGE_BUSY) {
  2474. /* Set cmdiocb flag for the
  2475. * exchange busy so sgl (xri)
  2476. * will not be released until
  2477. * the abort xri is received
  2478. * from hba.
  2479. */
  2480. spin_lock_irqsave(
  2481. &phba->hbalock, iflag);
  2482. cmdiocbp->iocb_flag |=
  2483. LPFC_EXCHANGE_BUSY;
  2484. spin_unlock_irqrestore(
  2485. &phba->hbalock, iflag);
  2486. }
  2487. if (cmdiocbp->iocb_flag &
  2488. LPFC_DRIVER_ABORTED) {
  2489. /*
  2490. * Clear LPFC_DRIVER_ABORTED
  2491. * bit in case it was driver
  2492. * initiated abort.
  2493. */
  2494. spin_lock_irqsave(
  2495. &phba->hbalock, iflag);
  2496. cmdiocbp->iocb_flag &=
  2497. ~LPFC_DRIVER_ABORTED;
  2498. spin_unlock_irqrestore(
  2499. &phba->hbalock, iflag);
  2500. cmdiocbp->iocb.ulpStatus =
  2501. IOSTAT_LOCAL_REJECT;
  2502. cmdiocbp->iocb.un.ulpWord[4] =
  2503. IOERR_ABORT_REQUESTED;
  2504. /*
  2505. * For SLI4, irsiocb contains
  2506. * NO_XRI in sli_xritag, it
  2507. * shall not affect releasing
  2508. * sgl (xri) process.
  2509. */
  2510. saveq->iocb.ulpStatus =
  2511. IOSTAT_LOCAL_REJECT;
  2512. saveq->iocb.un.ulpWord[4] =
  2513. IOERR_SLI_ABORTED;
  2514. spin_lock_irqsave(
  2515. &phba->hbalock, iflag);
  2516. saveq->iocb_flag |=
  2517. LPFC_DELAY_MEM_FREE;
  2518. spin_unlock_irqrestore(
  2519. &phba->hbalock, iflag);
  2520. }
  2521. }
  2522. }
  2523. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2524. } else
  2525. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2526. } else {
  2527. /*
  2528. * Unknown initiating command based on the response iotag.
  2529. * This could be the case on the ELS ring because of
  2530. * lpfc_els_abort().
  2531. */
  2532. if (pring->ringno != LPFC_ELS_RING) {
  2533. /*
  2534. * Ring <ringno> handler: unexpected completion IoTag
  2535. * <IoTag>
  2536. */
  2537. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2538. "0322 Ring %d handler: "
  2539. "unexpected completion IoTag x%x "
  2540. "Data: x%x x%x x%x x%x\n",
  2541. pring->ringno,
  2542. saveq->iocb.ulpIoTag,
  2543. saveq->iocb.ulpStatus,
  2544. saveq->iocb.un.ulpWord[4],
  2545. saveq->iocb.ulpCommand,
  2546. saveq->iocb.ulpContext);
  2547. }
  2548. }
  2549. return rc;
  2550. }
  2551. /**
  2552. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2553. * @phba: Pointer to HBA context object.
  2554. * @pring: Pointer to driver SLI ring object.
  2555. *
  2556. * This function is called from the iocb ring event handlers when
  2557. * put pointer is ahead of the get pointer for a ring. This function signal
  2558. * an error attention condition to the worker thread and the worker
  2559. * thread will transition the HBA to offline state.
  2560. **/
  2561. static void
  2562. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2563. {
  2564. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2565. /*
  2566. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2567. * rsp ring <portRspMax>
  2568. */
  2569. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2570. "0312 Ring %d handler: portRspPut %d "
  2571. "is bigger than rsp ring %d\n",
  2572. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2573. pring->sli.sli3.numRiocb);
  2574. phba->link_state = LPFC_HBA_ERROR;
  2575. /*
  2576. * All error attention handlers are posted to
  2577. * worker thread
  2578. */
  2579. phba->work_ha |= HA_ERATT;
  2580. phba->work_hs = HS_FFER3;
  2581. lpfc_worker_wake_up(phba);
  2582. return;
  2583. }
  2584. /**
  2585. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2586. * @ptr: Pointer to address of HBA context object.
  2587. *
  2588. * This function is invoked by the Error Attention polling timer when the
  2589. * timer times out. It will check the SLI Error Attention register for
  2590. * possible attention events. If so, it will post an Error Attention event
  2591. * and wake up worker thread to process it. Otherwise, it will set up the
  2592. * Error Attention polling timer for the next poll.
  2593. **/
  2594. void lpfc_poll_eratt(unsigned long ptr)
  2595. {
  2596. struct lpfc_hba *phba;
  2597. uint32_t eratt = 0, rem;
  2598. uint64_t sli_intr, cnt;
  2599. phba = (struct lpfc_hba *)ptr;
  2600. /* Here we will also keep track of interrupts per sec of the hba */
  2601. sli_intr = phba->sli.slistat.sli_intr;
  2602. if (phba->sli.slistat.sli_prev_intr > sli_intr)
  2603. cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
  2604. sli_intr);
  2605. else
  2606. cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
  2607. /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
  2608. rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
  2609. phba->sli.slistat.sli_ips = cnt;
  2610. phba->sli.slistat.sli_prev_intr = sli_intr;
  2611. /* Check chip HA register for error event */
  2612. eratt = lpfc_sli_check_eratt(phba);
  2613. if (eratt)
  2614. /* Tell the worker thread there is work to do */
  2615. lpfc_worker_wake_up(phba);
  2616. else
  2617. /* Restart the timer for next eratt poll */
  2618. mod_timer(&phba->eratt_poll, jiffies +
  2619. HZ * LPFC_ERATT_POLL_INTERVAL);
  2620. return;
  2621. }
  2622. /**
  2623. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2624. * @phba: Pointer to HBA context object.
  2625. * @pring: Pointer to driver SLI ring object.
  2626. * @mask: Host attention register mask for this ring.
  2627. *
  2628. * This function is called from the interrupt context when there is a ring
  2629. * event for the fcp ring. The caller does not hold any lock.
  2630. * The function processes each response iocb in the response ring until it
  2631. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2632. * LE bit set. The function will call the completion handler of the command iocb
  2633. * if the response iocb indicates a completion for a command iocb or it is
  2634. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2635. * function if this is an unsolicited iocb.
  2636. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2637. * to check it explicitly.
  2638. */
  2639. int
  2640. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2641. struct lpfc_sli_ring *pring, uint32_t mask)
  2642. {
  2643. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2644. IOCB_t *irsp = NULL;
  2645. IOCB_t *entry = NULL;
  2646. struct lpfc_iocbq *cmdiocbq = NULL;
  2647. struct lpfc_iocbq rspiocbq;
  2648. uint32_t status;
  2649. uint32_t portRspPut, portRspMax;
  2650. int rc = 1;
  2651. lpfc_iocb_type type;
  2652. unsigned long iflag;
  2653. uint32_t rsp_cmpl = 0;
  2654. spin_lock_irqsave(&phba->hbalock, iflag);
  2655. pring->stats.iocb_event++;
  2656. /*
  2657. * The next available response entry should never exceed the maximum
  2658. * entries. If it does, treat it as an adapter hardware error.
  2659. */
  2660. portRspMax = pring->sli.sli3.numRiocb;
  2661. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2662. if (unlikely(portRspPut >= portRspMax)) {
  2663. lpfc_sli_rsp_pointers_error(phba, pring);
  2664. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2665. return 1;
  2666. }
  2667. if (phba->fcp_ring_in_use) {
  2668. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2669. return 1;
  2670. } else
  2671. phba->fcp_ring_in_use = 1;
  2672. rmb();
  2673. while (pring->sli.sli3.rspidx != portRspPut) {
  2674. /*
  2675. * Fetch an entry off the ring and copy it into a local data
  2676. * structure. The copy involves a byte-swap since the
  2677. * network byte order and pci byte orders are different.
  2678. */
  2679. entry = lpfc_resp_iocb(phba, pring);
  2680. phba->last_completion_time = jiffies;
  2681. if (++pring->sli.sli3.rspidx >= portRspMax)
  2682. pring->sli.sli3.rspidx = 0;
  2683. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2684. (uint32_t *) &rspiocbq.iocb,
  2685. phba->iocb_rsp_size);
  2686. INIT_LIST_HEAD(&(rspiocbq.list));
  2687. irsp = &rspiocbq.iocb;
  2688. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2689. pring->stats.iocb_rsp++;
  2690. rsp_cmpl++;
  2691. if (unlikely(irsp->ulpStatus)) {
  2692. /*
  2693. * If resource errors reported from HBA, reduce
  2694. * queuedepths of the SCSI device.
  2695. */
  2696. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2697. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2698. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2699. phba->lpfc_rampdown_queue_depth(phba);
  2700. spin_lock_irqsave(&phba->hbalock, iflag);
  2701. }
  2702. /* Rsp ring <ringno> error: IOCB */
  2703. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2704. "0336 Rsp Ring %d error: IOCB Data: "
  2705. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2706. pring->ringno,
  2707. irsp->un.ulpWord[0],
  2708. irsp->un.ulpWord[1],
  2709. irsp->un.ulpWord[2],
  2710. irsp->un.ulpWord[3],
  2711. irsp->un.ulpWord[4],
  2712. irsp->un.ulpWord[5],
  2713. *(uint32_t *)&irsp->un1,
  2714. *((uint32_t *)&irsp->un1 + 1));
  2715. }
  2716. switch (type) {
  2717. case LPFC_ABORT_IOCB:
  2718. case LPFC_SOL_IOCB:
  2719. /*
  2720. * Idle exchange closed via ABTS from port. No iocb
  2721. * resources need to be recovered.
  2722. */
  2723. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2724. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2725. "0333 IOCB cmd 0x%x"
  2726. " processed. Skipping"
  2727. " completion\n",
  2728. irsp->ulpCommand);
  2729. break;
  2730. }
  2731. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2732. &rspiocbq);
  2733. if (unlikely(!cmdiocbq))
  2734. break;
  2735. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2736. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2737. if (cmdiocbq->iocb_cmpl) {
  2738. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2739. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2740. &rspiocbq);
  2741. spin_lock_irqsave(&phba->hbalock, iflag);
  2742. }
  2743. break;
  2744. case LPFC_UNSOL_IOCB:
  2745. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2746. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2747. spin_lock_irqsave(&phba->hbalock, iflag);
  2748. break;
  2749. default:
  2750. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2751. char adaptermsg[LPFC_MAX_ADPTMSG];
  2752. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2753. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2754. MAX_MSG_DATA);
  2755. dev_warn(&((phba->pcidev)->dev),
  2756. "lpfc%d: %s\n",
  2757. phba->brd_no, adaptermsg);
  2758. } else {
  2759. /* Unknown IOCB command */
  2760. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2761. "0334 Unknown IOCB command "
  2762. "Data: x%x, x%x x%x x%x x%x\n",
  2763. type, irsp->ulpCommand,
  2764. irsp->ulpStatus,
  2765. irsp->ulpIoTag,
  2766. irsp->ulpContext);
  2767. }
  2768. break;
  2769. }
  2770. /*
  2771. * The response IOCB has been processed. Update the ring
  2772. * pointer in SLIM. If the port response put pointer has not
  2773. * been updated, sync the pgp->rspPutInx and fetch the new port
  2774. * response put pointer.
  2775. */
  2776. writel(pring->sli.sli3.rspidx,
  2777. &phba->host_gp[pring->ringno].rspGetInx);
  2778. if (pring->sli.sli3.rspidx == portRspPut)
  2779. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2780. }
  2781. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2782. pring->stats.iocb_rsp_full++;
  2783. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2784. writel(status, phba->CAregaddr);
  2785. readl(phba->CAregaddr);
  2786. }
  2787. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2788. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2789. pring->stats.iocb_cmd_empty++;
  2790. /* Force update of the local copy of cmdGetInx */
  2791. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2792. lpfc_sli_resume_iocb(phba, pring);
  2793. if ((pring->lpfc_sli_cmd_available))
  2794. (pring->lpfc_sli_cmd_available) (phba, pring);
  2795. }
  2796. phba->fcp_ring_in_use = 0;
  2797. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2798. return rc;
  2799. }
  2800. /**
  2801. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2802. * @phba: Pointer to HBA context object.
  2803. * @pring: Pointer to driver SLI ring object.
  2804. * @rspiocbp: Pointer to driver response IOCB object.
  2805. *
  2806. * This function is called from the worker thread when there is a slow-path
  2807. * response IOCB to process. This function chains all the response iocbs until
  2808. * seeing the iocb with the LE bit set. The function will call
  2809. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2810. * completion of a command iocb. The function will call the
  2811. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2812. * The function frees the resources or calls the completion handler if this
  2813. * iocb is an abort completion. The function returns NULL when the response
  2814. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2815. * this function shall chain the iocb on to the iocb_continueq and return the
  2816. * response iocb passed in.
  2817. **/
  2818. static struct lpfc_iocbq *
  2819. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2820. struct lpfc_iocbq *rspiocbp)
  2821. {
  2822. struct lpfc_iocbq *saveq;
  2823. struct lpfc_iocbq *cmdiocbp;
  2824. struct lpfc_iocbq *next_iocb;
  2825. IOCB_t *irsp = NULL;
  2826. uint32_t free_saveq;
  2827. uint8_t iocb_cmd_type;
  2828. lpfc_iocb_type type;
  2829. unsigned long iflag;
  2830. int rc;
  2831. spin_lock_irqsave(&phba->hbalock, iflag);
  2832. /* First add the response iocb to the countinueq list */
  2833. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2834. pring->iocb_continueq_cnt++;
  2835. /* Now, determine whether the list is completed for processing */
  2836. irsp = &rspiocbp->iocb;
  2837. if (irsp->ulpLe) {
  2838. /*
  2839. * By default, the driver expects to free all resources
  2840. * associated with this iocb completion.
  2841. */
  2842. free_saveq = 1;
  2843. saveq = list_get_first(&pring->iocb_continueq,
  2844. struct lpfc_iocbq, list);
  2845. irsp = &(saveq->iocb);
  2846. list_del_init(&pring->iocb_continueq);
  2847. pring->iocb_continueq_cnt = 0;
  2848. pring->stats.iocb_rsp++;
  2849. /*
  2850. * If resource errors reported from HBA, reduce
  2851. * queuedepths of the SCSI device.
  2852. */
  2853. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2854. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2855. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2856. phba->lpfc_rampdown_queue_depth(phba);
  2857. spin_lock_irqsave(&phba->hbalock, iflag);
  2858. }
  2859. if (irsp->ulpStatus) {
  2860. /* Rsp ring <ringno> error: IOCB */
  2861. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2862. "0328 Rsp Ring %d error: "
  2863. "IOCB Data: "
  2864. "x%x x%x x%x x%x "
  2865. "x%x x%x x%x x%x "
  2866. "x%x x%x x%x x%x "
  2867. "x%x x%x x%x x%x\n",
  2868. pring->ringno,
  2869. irsp->un.ulpWord[0],
  2870. irsp->un.ulpWord[1],
  2871. irsp->un.ulpWord[2],
  2872. irsp->un.ulpWord[3],
  2873. irsp->un.ulpWord[4],
  2874. irsp->un.ulpWord[5],
  2875. *(((uint32_t *) irsp) + 6),
  2876. *(((uint32_t *) irsp) + 7),
  2877. *(((uint32_t *) irsp) + 8),
  2878. *(((uint32_t *) irsp) + 9),
  2879. *(((uint32_t *) irsp) + 10),
  2880. *(((uint32_t *) irsp) + 11),
  2881. *(((uint32_t *) irsp) + 12),
  2882. *(((uint32_t *) irsp) + 13),
  2883. *(((uint32_t *) irsp) + 14),
  2884. *(((uint32_t *) irsp) + 15));
  2885. }
  2886. /*
  2887. * Fetch the IOCB command type and call the correct completion
  2888. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2889. * get freed back to the lpfc_iocb_list by the discovery
  2890. * kernel thread.
  2891. */
  2892. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2893. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2894. switch (type) {
  2895. case LPFC_SOL_IOCB:
  2896. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2897. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2898. spin_lock_irqsave(&phba->hbalock, iflag);
  2899. break;
  2900. case LPFC_UNSOL_IOCB:
  2901. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2902. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2903. spin_lock_irqsave(&phba->hbalock, iflag);
  2904. if (!rc)
  2905. free_saveq = 0;
  2906. break;
  2907. case LPFC_ABORT_IOCB:
  2908. cmdiocbp = NULL;
  2909. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2910. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2911. saveq);
  2912. if (cmdiocbp) {
  2913. /* Call the specified completion routine */
  2914. if (cmdiocbp->iocb_cmpl) {
  2915. spin_unlock_irqrestore(&phba->hbalock,
  2916. iflag);
  2917. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2918. saveq);
  2919. spin_lock_irqsave(&phba->hbalock,
  2920. iflag);
  2921. } else
  2922. __lpfc_sli_release_iocbq(phba,
  2923. cmdiocbp);
  2924. }
  2925. break;
  2926. case LPFC_UNKNOWN_IOCB:
  2927. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2928. char adaptermsg[LPFC_MAX_ADPTMSG];
  2929. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2930. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2931. MAX_MSG_DATA);
  2932. dev_warn(&((phba->pcidev)->dev),
  2933. "lpfc%d: %s\n",
  2934. phba->brd_no, adaptermsg);
  2935. } else {
  2936. /* Unknown IOCB command */
  2937. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2938. "0335 Unknown IOCB "
  2939. "command Data: x%x "
  2940. "x%x x%x x%x\n",
  2941. irsp->ulpCommand,
  2942. irsp->ulpStatus,
  2943. irsp->ulpIoTag,
  2944. irsp->ulpContext);
  2945. }
  2946. break;
  2947. }
  2948. if (free_saveq) {
  2949. list_for_each_entry_safe(rspiocbp, next_iocb,
  2950. &saveq->list, list) {
  2951. list_del(&rspiocbp->list);
  2952. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2953. }
  2954. __lpfc_sli_release_iocbq(phba, saveq);
  2955. }
  2956. rspiocbp = NULL;
  2957. }
  2958. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2959. return rspiocbp;
  2960. }
  2961. /**
  2962. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2963. * @phba: Pointer to HBA context object.
  2964. * @pring: Pointer to driver SLI ring object.
  2965. * @mask: Host attention register mask for this ring.
  2966. *
  2967. * This routine wraps the actual slow_ring event process routine from the
  2968. * API jump table function pointer from the lpfc_hba struct.
  2969. **/
  2970. void
  2971. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2972. struct lpfc_sli_ring *pring, uint32_t mask)
  2973. {
  2974. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2975. }
  2976. /**
  2977. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  2978. * @phba: Pointer to HBA context object.
  2979. * @pring: Pointer to driver SLI ring object.
  2980. * @mask: Host attention register mask for this ring.
  2981. *
  2982. * This function is called from the worker thread when there is a ring event
  2983. * for non-fcp rings. The caller does not hold any lock. The function will
  2984. * remove each response iocb in the response ring and calls the handle
  2985. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2986. **/
  2987. static void
  2988. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2989. struct lpfc_sli_ring *pring, uint32_t mask)
  2990. {
  2991. struct lpfc_pgp *pgp;
  2992. IOCB_t *entry;
  2993. IOCB_t *irsp = NULL;
  2994. struct lpfc_iocbq *rspiocbp = NULL;
  2995. uint32_t portRspPut, portRspMax;
  2996. unsigned long iflag;
  2997. uint32_t status;
  2998. pgp = &phba->port_gp[pring->ringno];
  2999. spin_lock_irqsave(&phba->hbalock, iflag);
  3000. pring->stats.iocb_event++;
  3001. /*
  3002. * The next available response entry should never exceed the maximum
  3003. * entries. If it does, treat it as an adapter hardware error.
  3004. */
  3005. portRspMax = pring->sli.sli3.numRiocb;
  3006. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3007. if (portRspPut >= portRspMax) {
  3008. /*
  3009. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  3010. * rsp ring <portRspMax>
  3011. */
  3012. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3013. "0303 Ring %d handler: portRspPut %d "
  3014. "is bigger than rsp ring %d\n",
  3015. pring->ringno, portRspPut, portRspMax);
  3016. phba->link_state = LPFC_HBA_ERROR;
  3017. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3018. phba->work_hs = HS_FFER3;
  3019. lpfc_handle_eratt(phba);
  3020. return;
  3021. }
  3022. rmb();
  3023. while (pring->sli.sli3.rspidx != portRspPut) {
  3024. /*
  3025. * Build a completion list and call the appropriate handler.
  3026. * The process is to get the next available response iocb, get
  3027. * a free iocb from the list, copy the response data into the
  3028. * free iocb, insert to the continuation list, and update the
  3029. * next response index to slim. This process makes response
  3030. * iocb's in the ring available to DMA as fast as possible but
  3031. * pays a penalty for a copy operation. Since the iocb is
  3032. * only 32 bytes, this penalty is considered small relative to
  3033. * the PCI reads for register values and a slim write. When
  3034. * the ulpLe field is set, the entire Command has been
  3035. * received.
  3036. */
  3037. entry = lpfc_resp_iocb(phba, pring);
  3038. phba->last_completion_time = jiffies;
  3039. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3040. if (rspiocbp == NULL) {
  3041. printk(KERN_ERR "%s: out of buffers! Failing "
  3042. "completion.\n", __func__);
  3043. break;
  3044. }
  3045. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3046. phba->iocb_rsp_size);
  3047. irsp = &rspiocbp->iocb;
  3048. if (++pring->sli.sli3.rspidx >= portRspMax)
  3049. pring->sli.sli3.rspidx = 0;
  3050. if (pring->ringno == LPFC_ELS_RING) {
  3051. lpfc_debugfs_slow_ring_trc(phba,
  3052. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3053. *(((uint32_t *) irsp) + 4),
  3054. *(((uint32_t *) irsp) + 6),
  3055. *(((uint32_t *) irsp) + 7));
  3056. }
  3057. writel(pring->sli.sli3.rspidx,
  3058. &phba->host_gp[pring->ringno].rspGetInx);
  3059. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3060. /* Handle the response IOCB */
  3061. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3062. spin_lock_irqsave(&phba->hbalock, iflag);
  3063. /*
  3064. * If the port response put pointer has not been updated, sync
  3065. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3066. * response put pointer.
  3067. */
  3068. if (pring->sli.sli3.rspidx == portRspPut) {
  3069. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3070. }
  3071. } /* while (pring->sli.sli3.rspidx != portRspPut) */
  3072. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3073. /* At least one response entry has been freed */
  3074. pring->stats.iocb_rsp_full++;
  3075. /* SET RxRE_RSP in Chip Att register */
  3076. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3077. writel(status, phba->CAregaddr);
  3078. readl(phba->CAregaddr); /* flush */
  3079. }
  3080. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3081. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3082. pring->stats.iocb_cmd_empty++;
  3083. /* Force update of the local copy of cmdGetInx */
  3084. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3085. lpfc_sli_resume_iocb(phba, pring);
  3086. if ((pring->lpfc_sli_cmd_available))
  3087. (pring->lpfc_sli_cmd_available) (phba, pring);
  3088. }
  3089. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3090. return;
  3091. }
  3092. /**
  3093. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3094. * @phba: Pointer to HBA context object.
  3095. * @pring: Pointer to driver SLI ring object.
  3096. * @mask: Host attention register mask for this ring.
  3097. *
  3098. * This function is called from the worker thread when there is a pending
  3099. * ELS response iocb on the driver internal slow-path response iocb worker
  3100. * queue. The caller does not hold any lock. The function will remove each
  3101. * response iocb from the response worker queue and calls the handle
  3102. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3103. **/
  3104. static void
  3105. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3106. struct lpfc_sli_ring *pring, uint32_t mask)
  3107. {
  3108. struct lpfc_iocbq *irspiocbq;
  3109. struct hbq_dmabuf *dmabuf;
  3110. struct lpfc_cq_event *cq_event;
  3111. unsigned long iflag;
  3112. spin_lock_irqsave(&phba->hbalock, iflag);
  3113. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3114. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3115. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3116. /* Get the response iocb from the head of work queue */
  3117. spin_lock_irqsave(&phba->hbalock, iflag);
  3118. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3119. cq_event, struct lpfc_cq_event, list);
  3120. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3121. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3122. case CQE_CODE_COMPL_WQE:
  3123. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3124. cq_event);
  3125. /* Translate ELS WCQE to response IOCBQ */
  3126. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3127. irspiocbq);
  3128. if (irspiocbq)
  3129. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3130. irspiocbq);
  3131. break;
  3132. case CQE_CODE_RECEIVE:
  3133. case CQE_CODE_RECEIVE_V1:
  3134. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3135. cq_event);
  3136. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3137. break;
  3138. default:
  3139. break;
  3140. }
  3141. }
  3142. }
  3143. /**
  3144. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3145. * @phba: Pointer to HBA context object.
  3146. * @pring: Pointer to driver SLI ring object.
  3147. *
  3148. * This function aborts all iocbs in the given ring and frees all the iocb
  3149. * objects in txq. This function issues an abort iocb for all the iocb commands
  3150. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3151. * the return of this function. The caller is not required to hold any locks.
  3152. **/
  3153. void
  3154. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3155. {
  3156. LIST_HEAD(completions);
  3157. struct lpfc_iocbq *iocb, *next_iocb;
  3158. if (pring->ringno == LPFC_ELS_RING) {
  3159. lpfc_fabric_abort_hba(phba);
  3160. }
  3161. /* Error everything on txq and txcmplq
  3162. * First do the txq.
  3163. */
  3164. spin_lock_irq(&phba->hbalock);
  3165. list_splice_init(&pring->txq, &completions);
  3166. pring->txq_cnt = 0;
  3167. /* Next issue ABTS for everything on the txcmplq */
  3168. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3169. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3170. spin_unlock_irq(&phba->hbalock);
  3171. /* Cancel all the IOCBs from the completions list */
  3172. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3173. IOERR_SLI_ABORTED);
  3174. }
  3175. /**
  3176. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3177. * @phba: Pointer to HBA context object.
  3178. *
  3179. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3180. * objects in txq and txcmplq. This function will not issue abort iocbs
  3181. * for all the iocb commands in txcmplq, they will just be returned with
  3182. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3183. * slot has been permanently disabled.
  3184. **/
  3185. void
  3186. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3187. {
  3188. LIST_HEAD(txq);
  3189. LIST_HEAD(txcmplq);
  3190. struct lpfc_sli *psli = &phba->sli;
  3191. struct lpfc_sli_ring *pring;
  3192. /* Currently, only one fcp ring */
  3193. pring = &psli->ring[psli->fcp_ring];
  3194. spin_lock_irq(&phba->hbalock);
  3195. /* Retrieve everything on txq */
  3196. list_splice_init(&pring->txq, &txq);
  3197. pring->txq_cnt = 0;
  3198. /* Retrieve everything on the txcmplq */
  3199. list_splice_init(&pring->txcmplq, &txcmplq);
  3200. pring->txcmplq_cnt = 0;
  3201. /* Indicate the I/O queues are flushed */
  3202. phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
  3203. spin_unlock_irq(&phba->hbalock);
  3204. /* Flush the txq */
  3205. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3206. IOERR_SLI_DOWN);
  3207. /* Flush the txcmpq */
  3208. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3209. IOERR_SLI_DOWN);
  3210. }
  3211. /**
  3212. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3213. * @phba: Pointer to HBA context object.
  3214. * @mask: Bit mask to be checked.
  3215. *
  3216. * This function reads the host status register and compares
  3217. * with the provided bit mask to check if HBA completed
  3218. * the restart. This function will wait in a loop for the
  3219. * HBA to complete restart. If the HBA does not restart within
  3220. * 15 iterations, the function will reset the HBA again. The
  3221. * function returns 1 when HBA fail to restart otherwise returns
  3222. * zero.
  3223. **/
  3224. static int
  3225. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3226. {
  3227. uint32_t status;
  3228. int i = 0;
  3229. int retval = 0;
  3230. /* Read the HBA Host Status Register */
  3231. if (lpfc_readl(phba->HSregaddr, &status))
  3232. return 1;
  3233. /*
  3234. * Check status register every 100ms for 5 retries, then every
  3235. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3236. * every 2.5 sec for 4.
  3237. * Break our of the loop if errors occurred during init.
  3238. */
  3239. while (((status & mask) != mask) &&
  3240. !(status & HS_FFERM) &&
  3241. i++ < 20) {
  3242. if (i <= 5)
  3243. msleep(10);
  3244. else if (i <= 10)
  3245. msleep(500);
  3246. else
  3247. msleep(2500);
  3248. if (i == 15) {
  3249. /* Do post */
  3250. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3251. lpfc_sli_brdrestart(phba);
  3252. }
  3253. /* Read the HBA Host Status Register */
  3254. if (lpfc_readl(phba->HSregaddr, &status)) {
  3255. retval = 1;
  3256. break;
  3257. }
  3258. }
  3259. /* Check to see if any errors occurred during init */
  3260. if ((status & HS_FFERM) || (i >= 20)) {
  3261. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3262. "2751 Adapter failed to restart, "
  3263. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3264. status,
  3265. readl(phba->MBslimaddr + 0xa8),
  3266. readl(phba->MBslimaddr + 0xac));
  3267. phba->link_state = LPFC_HBA_ERROR;
  3268. retval = 1;
  3269. }
  3270. return retval;
  3271. }
  3272. /**
  3273. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3274. * @phba: Pointer to HBA context object.
  3275. * @mask: Bit mask to be checked.
  3276. *
  3277. * This function checks the host status register to check if HBA is
  3278. * ready. This function will wait in a loop for the HBA to be ready
  3279. * If the HBA is not ready , the function will will reset the HBA PCI
  3280. * function again. The function returns 1 when HBA fail to be ready
  3281. * otherwise returns zero.
  3282. **/
  3283. static int
  3284. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3285. {
  3286. uint32_t status;
  3287. int retval = 0;
  3288. /* Read the HBA Host Status Register */
  3289. status = lpfc_sli4_post_status_check(phba);
  3290. if (status) {
  3291. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3292. lpfc_sli_brdrestart(phba);
  3293. status = lpfc_sli4_post_status_check(phba);
  3294. }
  3295. /* Check to see if any errors occurred during init */
  3296. if (status) {
  3297. phba->link_state = LPFC_HBA_ERROR;
  3298. retval = 1;
  3299. } else
  3300. phba->sli4_hba.intr_enable = 0;
  3301. return retval;
  3302. }
  3303. /**
  3304. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3305. * @phba: Pointer to HBA context object.
  3306. * @mask: Bit mask to be checked.
  3307. *
  3308. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3309. * from the API jump table function pointer from the lpfc_hba struct.
  3310. **/
  3311. int
  3312. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3313. {
  3314. return phba->lpfc_sli_brdready(phba, mask);
  3315. }
  3316. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3317. /**
  3318. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3319. * @phba: Pointer to HBA context object.
  3320. *
  3321. * This function is called before resetting an HBA. This function is called
  3322. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3323. **/
  3324. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3325. {
  3326. uint32_t __iomem *resp_buf;
  3327. uint32_t __iomem *mbox_buf;
  3328. volatile uint32_t mbox;
  3329. uint32_t hc_copy, ha_copy, resp_data;
  3330. int i;
  3331. uint8_t hdrtype;
  3332. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3333. if (hdrtype != 0x80 ||
  3334. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3335. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3336. return;
  3337. /*
  3338. * Tell the other part of the chip to suspend temporarily all
  3339. * its DMA activity.
  3340. */
  3341. resp_buf = phba->MBslimaddr;
  3342. /* Disable the error attention */
  3343. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3344. return;
  3345. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3346. readl(phba->HCregaddr); /* flush */
  3347. phba->link_flag |= LS_IGNORE_ERATT;
  3348. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3349. return;
  3350. if (ha_copy & HA_ERATT) {
  3351. /* Clear Chip error bit */
  3352. writel(HA_ERATT, phba->HAregaddr);
  3353. phba->pport->stopped = 1;
  3354. }
  3355. mbox = 0;
  3356. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3357. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3358. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3359. mbox_buf = phba->MBslimaddr;
  3360. writel(mbox, mbox_buf);
  3361. for (i = 0; i < 50; i++) {
  3362. if (lpfc_readl((resp_buf + 1), &resp_data))
  3363. return;
  3364. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3365. mdelay(1);
  3366. else
  3367. break;
  3368. }
  3369. resp_data = 0;
  3370. if (lpfc_readl((resp_buf + 1), &resp_data))
  3371. return;
  3372. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3373. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3374. phba->pport->stopped)
  3375. goto restore_hc;
  3376. else
  3377. goto clear_errat;
  3378. }
  3379. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3380. resp_data = 0;
  3381. for (i = 0; i < 500; i++) {
  3382. if (lpfc_readl(resp_buf, &resp_data))
  3383. return;
  3384. if (resp_data != mbox)
  3385. mdelay(1);
  3386. else
  3387. break;
  3388. }
  3389. clear_errat:
  3390. while (++i < 500) {
  3391. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3392. return;
  3393. if (!(ha_copy & HA_ERATT))
  3394. mdelay(1);
  3395. else
  3396. break;
  3397. }
  3398. if (readl(phba->HAregaddr) & HA_ERATT) {
  3399. writel(HA_ERATT, phba->HAregaddr);
  3400. phba->pport->stopped = 1;
  3401. }
  3402. restore_hc:
  3403. phba->link_flag &= ~LS_IGNORE_ERATT;
  3404. writel(hc_copy, phba->HCregaddr);
  3405. readl(phba->HCregaddr); /* flush */
  3406. }
  3407. /**
  3408. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3409. * @phba: Pointer to HBA context object.
  3410. *
  3411. * This function issues a kill_board mailbox command and waits for
  3412. * the error attention interrupt. This function is called for stopping
  3413. * the firmware processing. The caller is not required to hold any
  3414. * locks. This function calls lpfc_hba_down_post function to free
  3415. * any pending commands after the kill. The function will return 1 when it
  3416. * fails to kill the board else will return 0.
  3417. **/
  3418. int
  3419. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3420. {
  3421. struct lpfc_sli *psli;
  3422. LPFC_MBOXQ_t *pmb;
  3423. uint32_t status;
  3424. uint32_t ha_copy;
  3425. int retval;
  3426. int i = 0;
  3427. psli = &phba->sli;
  3428. /* Kill HBA */
  3429. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3430. "0329 Kill HBA Data: x%x x%x\n",
  3431. phba->pport->port_state, psli->sli_flag);
  3432. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3433. if (!pmb)
  3434. return 1;
  3435. /* Disable the error attention */
  3436. spin_lock_irq(&phba->hbalock);
  3437. if (lpfc_readl(phba->HCregaddr, &status)) {
  3438. spin_unlock_irq(&phba->hbalock);
  3439. mempool_free(pmb, phba->mbox_mem_pool);
  3440. return 1;
  3441. }
  3442. status &= ~HC_ERINT_ENA;
  3443. writel(status, phba->HCregaddr);
  3444. readl(phba->HCregaddr); /* flush */
  3445. phba->link_flag |= LS_IGNORE_ERATT;
  3446. spin_unlock_irq(&phba->hbalock);
  3447. lpfc_kill_board(phba, pmb);
  3448. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3449. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3450. if (retval != MBX_SUCCESS) {
  3451. if (retval != MBX_BUSY)
  3452. mempool_free(pmb, phba->mbox_mem_pool);
  3453. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3454. "2752 KILL_BOARD command failed retval %d\n",
  3455. retval);
  3456. spin_lock_irq(&phba->hbalock);
  3457. phba->link_flag &= ~LS_IGNORE_ERATT;
  3458. spin_unlock_irq(&phba->hbalock);
  3459. return 1;
  3460. }
  3461. spin_lock_irq(&phba->hbalock);
  3462. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3463. spin_unlock_irq(&phba->hbalock);
  3464. mempool_free(pmb, phba->mbox_mem_pool);
  3465. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3466. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3467. * 3 seconds we still set HBA_ERROR state because the status of the
  3468. * board is now undefined.
  3469. */
  3470. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3471. return 1;
  3472. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3473. mdelay(100);
  3474. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3475. return 1;
  3476. }
  3477. del_timer_sync(&psli->mbox_tmo);
  3478. if (ha_copy & HA_ERATT) {
  3479. writel(HA_ERATT, phba->HAregaddr);
  3480. phba->pport->stopped = 1;
  3481. }
  3482. spin_lock_irq(&phba->hbalock);
  3483. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3484. psli->mbox_active = NULL;
  3485. phba->link_flag &= ~LS_IGNORE_ERATT;
  3486. spin_unlock_irq(&phba->hbalock);
  3487. lpfc_hba_down_post(phba);
  3488. phba->link_state = LPFC_HBA_ERROR;
  3489. return ha_copy & HA_ERATT ? 0 : 1;
  3490. }
  3491. /**
  3492. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3493. * @phba: Pointer to HBA context object.
  3494. *
  3495. * This function resets the HBA by writing HC_INITFF to the control
  3496. * register. After the HBA resets, this function resets all the iocb ring
  3497. * indices. This function disables PCI layer parity checking during
  3498. * the reset.
  3499. * This function returns 0 always.
  3500. * The caller is not required to hold any locks.
  3501. **/
  3502. int
  3503. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3504. {
  3505. struct lpfc_sli *psli;
  3506. struct lpfc_sli_ring *pring;
  3507. uint16_t cfg_value;
  3508. int i;
  3509. psli = &phba->sli;
  3510. /* Reset HBA */
  3511. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3512. "0325 Reset HBA Data: x%x x%x\n",
  3513. phba->pport->port_state, psli->sli_flag);
  3514. /* perform board reset */
  3515. phba->fc_eventTag = 0;
  3516. phba->link_events = 0;
  3517. phba->pport->fc_myDID = 0;
  3518. phba->pport->fc_prevDID = 0;
  3519. /* Turn off parity checking and serr during the physical reset */
  3520. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3521. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3522. (cfg_value &
  3523. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3524. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3525. /* Now toggle INITFF bit in the Host Control Register */
  3526. writel(HC_INITFF, phba->HCregaddr);
  3527. mdelay(1);
  3528. readl(phba->HCregaddr); /* flush */
  3529. writel(0, phba->HCregaddr);
  3530. readl(phba->HCregaddr); /* flush */
  3531. /* Restore PCI cmd register */
  3532. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3533. /* Initialize relevant SLI info */
  3534. for (i = 0; i < psli->num_rings; i++) {
  3535. pring = &psli->ring[i];
  3536. pring->flag = 0;
  3537. pring->sli.sli3.rspidx = 0;
  3538. pring->sli.sli3.next_cmdidx = 0;
  3539. pring->sli.sli3.local_getidx = 0;
  3540. pring->sli.sli3.cmdidx = 0;
  3541. pring->missbufcnt = 0;
  3542. }
  3543. phba->link_state = LPFC_WARM_START;
  3544. return 0;
  3545. }
  3546. /**
  3547. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3548. * @phba: Pointer to HBA context object.
  3549. *
  3550. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3551. * checking during resets the device. The caller is not required to hold
  3552. * any locks.
  3553. *
  3554. * This function returns 0 always.
  3555. **/
  3556. int
  3557. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3558. {
  3559. struct lpfc_sli *psli = &phba->sli;
  3560. uint16_t cfg_value;
  3561. int rc;
  3562. /* Reset HBA */
  3563. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3564. "0295 Reset HBA Data: x%x x%x\n",
  3565. phba->pport->port_state, psli->sli_flag);
  3566. /* perform board reset */
  3567. phba->fc_eventTag = 0;
  3568. phba->link_events = 0;
  3569. phba->pport->fc_myDID = 0;
  3570. phba->pport->fc_prevDID = 0;
  3571. spin_lock_irq(&phba->hbalock);
  3572. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3573. phba->fcf.fcf_flag = 0;
  3574. spin_unlock_irq(&phba->hbalock);
  3575. /* Now physically reset the device */
  3576. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3577. "0389 Performing PCI function reset!\n");
  3578. /* Turn off parity checking and serr during the physical reset */
  3579. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3580. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3581. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3582. /* Perform FCoE PCI function reset */
  3583. lpfc_sli4_queue_destroy(phba);
  3584. rc = lpfc_pci_function_reset(phba);
  3585. /* Restore PCI cmd register */
  3586. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3587. return rc;
  3588. }
  3589. /**
  3590. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3591. * @phba: Pointer to HBA context object.
  3592. *
  3593. * This function is called in the SLI initialization code path to
  3594. * restart the HBA. The caller is not required to hold any lock.
  3595. * This function writes MBX_RESTART mailbox command to the SLIM and
  3596. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3597. * function to free any pending commands. The function enables
  3598. * POST only during the first initialization. The function returns zero.
  3599. * The function does not guarantee completion of MBX_RESTART mailbox
  3600. * command before the return of this function.
  3601. **/
  3602. static int
  3603. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3604. {
  3605. MAILBOX_t *mb;
  3606. struct lpfc_sli *psli;
  3607. volatile uint32_t word0;
  3608. void __iomem *to_slim;
  3609. uint32_t hba_aer_enabled;
  3610. spin_lock_irq(&phba->hbalock);
  3611. /* Take PCIe device Advanced Error Reporting (AER) state */
  3612. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3613. psli = &phba->sli;
  3614. /* Restart HBA */
  3615. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3616. "0337 Restart HBA Data: x%x x%x\n",
  3617. phba->pport->port_state, psli->sli_flag);
  3618. word0 = 0;
  3619. mb = (MAILBOX_t *) &word0;
  3620. mb->mbxCommand = MBX_RESTART;
  3621. mb->mbxHc = 1;
  3622. lpfc_reset_barrier(phba);
  3623. to_slim = phba->MBslimaddr;
  3624. writel(*(uint32_t *) mb, to_slim);
  3625. readl(to_slim); /* flush */
  3626. /* Only skip post after fc_ffinit is completed */
  3627. if (phba->pport->port_state)
  3628. word0 = 1; /* This is really setting up word1 */
  3629. else
  3630. word0 = 0; /* This is really setting up word1 */
  3631. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3632. writel(*(uint32_t *) mb, to_slim);
  3633. readl(to_slim); /* flush */
  3634. lpfc_sli_brdreset(phba);
  3635. phba->pport->stopped = 0;
  3636. phba->link_state = LPFC_INIT_START;
  3637. phba->hba_flag = 0;
  3638. spin_unlock_irq(&phba->hbalock);
  3639. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3640. psli->stats_start = get_seconds();
  3641. /* Give the INITFF and Post time to settle. */
  3642. mdelay(100);
  3643. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3644. if (hba_aer_enabled)
  3645. pci_disable_pcie_error_reporting(phba->pcidev);
  3646. lpfc_hba_down_post(phba);
  3647. return 0;
  3648. }
  3649. /**
  3650. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3651. * @phba: Pointer to HBA context object.
  3652. *
  3653. * This function is called in the SLI initialization code path to restart
  3654. * a SLI4 HBA. The caller is not required to hold any lock.
  3655. * At the end of the function, it calls lpfc_hba_down_post function to
  3656. * free any pending commands.
  3657. **/
  3658. static int
  3659. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3660. {
  3661. struct lpfc_sli *psli = &phba->sli;
  3662. uint32_t hba_aer_enabled;
  3663. int rc;
  3664. /* Restart HBA */
  3665. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3666. "0296 Restart HBA Data: x%x x%x\n",
  3667. phba->pport->port_state, psli->sli_flag);
  3668. /* Take PCIe device Advanced Error Reporting (AER) state */
  3669. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3670. rc = lpfc_sli4_brdreset(phba);
  3671. spin_lock_irq(&phba->hbalock);
  3672. phba->pport->stopped = 0;
  3673. phba->link_state = LPFC_INIT_START;
  3674. phba->hba_flag = 0;
  3675. spin_unlock_irq(&phba->hbalock);
  3676. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3677. psli->stats_start = get_seconds();
  3678. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3679. if (hba_aer_enabled)
  3680. pci_disable_pcie_error_reporting(phba->pcidev);
  3681. lpfc_hba_down_post(phba);
  3682. return rc;
  3683. }
  3684. /**
  3685. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3686. * @phba: Pointer to HBA context object.
  3687. *
  3688. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3689. * API jump table function pointer from the lpfc_hba struct.
  3690. **/
  3691. int
  3692. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3693. {
  3694. return phba->lpfc_sli_brdrestart(phba);
  3695. }
  3696. /**
  3697. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3698. * @phba: Pointer to HBA context object.
  3699. *
  3700. * This function is called after a HBA restart to wait for successful
  3701. * restart of the HBA. Successful restart of the HBA is indicated by
  3702. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3703. * iteration, the function will restart the HBA again. The function returns
  3704. * zero if HBA successfully restarted else returns negative error code.
  3705. **/
  3706. static int
  3707. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3708. {
  3709. uint32_t status, i = 0;
  3710. /* Read the HBA Host Status Register */
  3711. if (lpfc_readl(phba->HSregaddr, &status))
  3712. return -EIO;
  3713. /* Check status register to see what current state is */
  3714. i = 0;
  3715. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3716. /* Check every 10ms for 10 retries, then every 100ms for 90
  3717. * retries, then every 1 sec for 50 retires for a total of
  3718. * ~60 seconds before reset the board again and check every
  3719. * 1 sec for 50 retries. The up to 60 seconds before the
  3720. * board ready is required by the Falcon FIPS zeroization
  3721. * complete, and any reset the board in between shall cause
  3722. * restart of zeroization, further delay the board ready.
  3723. */
  3724. if (i++ >= 200) {
  3725. /* Adapter failed to init, timeout, status reg
  3726. <status> */
  3727. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3728. "0436 Adapter failed to init, "
  3729. "timeout, status reg x%x, "
  3730. "FW Data: A8 x%x AC x%x\n", status,
  3731. readl(phba->MBslimaddr + 0xa8),
  3732. readl(phba->MBslimaddr + 0xac));
  3733. phba->link_state = LPFC_HBA_ERROR;
  3734. return -ETIMEDOUT;
  3735. }
  3736. /* Check to see if any errors occurred during init */
  3737. if (status & HS_FFERM) {
  3738. /* ERROR: During chipset initialization */
  3739. /* Adapter failed to init, chipset, status reg
  3740. <status> */
  3741. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3742. "0437 Adapter failed to init, "
  3743. "chipset, status reg x%x, "
  3744. "FW Data: A8 x%x AC x%x\n", status,
  3745. readl(phba->MBslimaddr + 0xa8),
  3746. readl(phba->MBslimaddr + 0xac));
  3747. phba->link_state = LPFC_HBA_ERROR;
  3748. return -EIO;
  3749. }
  3750. if (i <= 10)
  3751. msleep(10);
  3752. else if (i <= 100)
  3753. msleep(100);
  3754. else
  3755. msleep(1000);
  3756. if (i == 150) {
  3757. /* Do post */
  3758. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3759. lpfc_sli_brdrestart(phba);
  3760. }
  3761. /* Read the HBA Host Status Register */
  3762. if (lpfc_readl(phba->HSregaddr, &status))
  3763. return -EIO;
  3764. }
  3765. /* Check to see if any errors occurred during init */
  3766. if (status & HS_FFERM) {
  3767. /* ERROR: During chipset initialization */
  3768. /* Adapter failed to init, chipset, status reg <status> */
  3769. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3770. "0438 Adapter failed to init, chipset, "
  3771. "status reg x%x, "
  3772. "FW Data: A8 x%x AC x%x\n", status,
  3773. readl(phba->MBslimaddr + 0xa8),
  3774. readl(phba->MBslimaddr + 0xac));
  3775. phba->link_state = LPFC_HBA_ERROR;
  3776. return -EIO;
  3777. }
  3778. /* Clear all interrupt enable conditions */
  3779. writel(0, phba->HCregaddr);
  3780. readl(phba->HCregaddr); /* flush */
  3781. /* setup host attn register */
  3782. writel(0xffffffff, phba->HAregaddr);
  3783. readl(phba->HAregaddr); /* flush */
  3784. return 0;
  3785. }
  3786. /**
  3787. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3788. *
  3789. * This function calculates and returns the number of HBQs required to be
  3790. * configured.
  3791. **/
  3792. int
  3793. lpfc_sli_hbq_count(void)
  3794. {
  3795. return ARRAY_SIZE(lpfc_hbq_defs);
  3796. }
  3797. /**
  3798. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3799. *
  3800. * This function adds the number of hbq entries in every HBQ to get
  3801. * the total number of hbq entries required for the HBA and returns
  3802. * the total count.
  3803. **/
  3804. static int
  3805. lpfc_sli_hbq_entry_count(void)
  3806. {
  3807. int hbq_count = lpfc_sli_hbq_count();
  3808. int count = 0;
  3809. int i;
  3810. for (i = 0; i < hbq_count; ++i)
  3811. count += lpfc_hbq_defs[i]->entry_count;
  3812. return count;
  3813. }
  3814. /**
  3815. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3816. *
  3817. * This function calculates amount of memory required for all hbq entries
  3818. * to be configured and returns the total memory required.
  3819. **/
  3820. int
  3821. lpfc_sli_hbq_size(void)
  3822. {
  3823. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3824. }
  3825. /**
  3826. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3827. * @phba: Pointer to HBA context object.
  3828. *
  3829. * This function is called during the SLI initialization to configure
  3830. * all the HBQs and post buffers to the HBQ. The caller is not
  3831. * required to hold any locks. This function will return zero if successful
  3832. * else it will return negative error code.
  3833. **/
  3834. static int
  3835. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3836. {
  3837. int hbq_count = lpfc_sli_hbq_count();
  3838. LPFC_MBOXQ_t *pmb;
  3839. MAILBOX_t *pmbox;
  3840. uint32_t hbqno;
  3841. uint32_t hbq_entry_index;
  3842. /* Get a Mailbox buffer to setup mailbox
  3843. * commands for HBA initialization
  3844. */
  3845. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3846. if (!pmb)
  3847. return -ENOMEM;
  3848. pmbox = &pmb->u.mb;
  3849. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3850. phba->link_state = LPFC_INIT_MBX_CMDS;
  3851. phba->hbq_in_use = 1;
  3852. hbq_entry_index = 0;
  3853. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3854. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3855. phba->hbqs[hbqno].hbqPutIdx = 0;
  3856. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3857. phba->hbqs[hbqno].entry_count =
  3858. lpfc_hbq_defs[hbqno]->entry_count;
  3859. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3860. hbq_entry_index, pmb);
  3861. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3862. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3863. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3864. mbxStatus <status>, ring <num> */
  3865. lpfc_printf_log(phba, KERN_ERR,
  3866. LOG_SLI | LOG_VPORT,
  3867. "1805 Adapter failed to init. "
  3868. "Data: x%x x%x x%x\n",
  3869. pmbox->mbxCommand,
  3870. pmbox->mbxStatus, hbqno);
  3871. phba->link_state = LPFC_HBA_ERROR;
  3872. mempool_free(pmb, phba->mbox_mem_pool);
  3873. return -ENXIO;
  3874. }
  3875. }
  3876. phba->hbq_count = hbq_count;
  3877. mempool_free(pmb, phba->mbox_mem_pool);
  3878. /* Initially populate or replenish the HBQs */
  3879. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3880. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3881. return 0;
  3882. }
  3883. /**
  3884. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3885. * @phba: Pointer to HBA context object.
  3886. *
  3887. * This function is called during the SLI initialization to configure
  3888. * all the HBQs and post buffers to the HBQ. The caller is not
  3889. * required to hold any locks. This function will return zero if successful
  3890. * else it will return negative error code.
  3891. **/
  3892. static int
  3893. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3894. {
  3895. phba->hbq_in_use = 1;
  3896. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3897. phba->hbq_count = 1;
  3898. /* Initially populate or replenish the HBQs */
  3899. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3900. return 0;
  3901. }
  3902. /**
  3903. * lpfc_sli_config_port - Issue config port mailbox command
  3904. * @phba: Pointer to HBA context object.
  3905. * @sli_mode: sli mode - 2/3
  3906. *
  3907. * This function is called by the sli intialization code path
  3908. * to issue config_port mailbox command. This function restarts the
  3909. * HBA firmware and issues a config_port mailbox command to configure
  3910. * the SLI interface in the sli mode specified by sli_mode
  3911. * variable. The caller is not required to hold any locks.
  3912. * The function returns 0 if successful, else returns negative error
  3913. * code.
  3914. **/
  3915. int
  3916. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3917. {
  3918. LPFC_MBOXQ_t *pmb;
  3919. uint32_t resetcount = 0, rc = 0, done = 0;
  3920. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3921. if (!pmb) {
  3922. phba->link_state = LPFC_HBA_ERROR;
  3923. return -ENOMEM;
  3924. }
  3925. phba->sli_rev = sli_mode;
  3926. while (resetcount < 2 && !done) {
  3927. spin_lock_irq(&phba->hbalock);
  3928. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3929. spin_unlock_irq(&phba->hbalock);
  3930. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3931. lpfc_sli_brdrestart(phba);
  3932. rc = lpfc_sli_chipset_init(phba);
  3933. if (rc)
  3934. break;
  3935. spin_lock_irq(&phba->hbalock);
  3936. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3937. spin_unlock_irq(&phba->hbalock);
  3938. resetcount++;
  3939. /* Call pre CONFIG_PORT mailbox command initialization. A
  3940. * value of 0 means the call was successful. Any other
  3941. * nonzero value is a failure, but if ERESTART is returned,
  3942. * the driver may reset the HBA and try again.
  3943. */
  3944. rc = lpfc_config_port_prep(phba);
  3945. if (rc == -ERESTART) {
  3946. phba->link_state = LPFC_LINK_UNKNOWN;
  3947. continue;
  3948. } else if (rc)
  3949. break;
  3950. phba->link_state = LPFC_INIT_MBX_CMDS;
  3951. lpfc_config_port(phba, pmb);
  3952. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3953. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3954. LPFC_SLI3_HBQ_ENABLED |
  3955. LPFC_SLI3_CRP_ENABLED |
  3956. LPFC_SLI3_BG_ENABLED |
  3957. LPFC_SLI3_DSS_ENABLED);
  3958. if (rc != MBX_SUCCESS) {
  3959. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3960. "0442 Adapter failed to init, mbxCmd x%x "
  3961. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3962. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3963. spin_lock_irq(&phba->hbalock);
  3964. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3965. spin_unlock_irq(&phba->hbalock);
  3966. rc = -ENXIO;
  3967. } else {
  3968. /* Allow asynchronous mailbox command to go through */
  3969. spin_lock_irq(&phba->hbalock);
  3970. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3971. spin_unlock_irq(&phba->hbalock);
  3972. done = 1;
  3973. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  3974. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  3975. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3976. "3110 Port did not grant ASABT\n");
  3977. }
  3978. }
  3979. if (!done) {
  3980. rc = -EINVAL;
  3981. goto do_prep_failed;
  3982. }
  3983. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3984. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3985. rc = -ENXIO;
  3986. goto do_prep_failed;
  3987. }
  3988. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3989. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3990. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3991. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3992. phba->max_vpi : phba->max_vports;
  3993. } else
  3994. phba->max_vpi = 0;
  3995. phba->fips_level = 0;
  3996. phba->fips_spec_rev = 0;
  3997. if (pmb->u.mb.un.varCfgPort.gdss) {
  3998. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3999. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  4000. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  4001. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4002. "2850 Security Crypto Active. FIPS x%d "
  4003. "(Spec Rev: x%d)",
  4004. phba->fips_level, phba->fips_spec_rev);
  4005. }
  4006. if (pmb->u.mb.un.varCfgPort.sec_err) {
  4007. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4008. "2856 Config Port Security Crypto "
  4009. "Error: x%x ",
  4010. pmb->u.mb.un.varCfgPort.sec_err);
  4011. }
  4012. if (pmb->u.mb.un.varCfgPort.gerbm)
  4013. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  4014. if (pmb->u.mb.un.varCfgPort.gcrp)
  4015. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  4016. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  4017. phba->port_gp = phba->mbox->us.s3_pgp.port;
  4018. if (phba->cfg_enable_bg) {
  4019. if (pmb->u.mb.un.varCfgPort.gbg)
  4020. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  4021. else
  4022. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4023. "0443 Adapter did not grant "
  4024. "BlockGuard\n");
  4025. }
  4026. } else {
  4027. phba->hbq_get = NULL;
  4028. phba->port_gp = phba->mbox->us.s2.port;
  4029. phba->max_vpi = 0;
  4030. }
  4031. do_prep_failed:
  4032. mempool_free(pmb, phba->mbox_mem_pool);
  4033. return rc;
  4034. }
  4035. /**
  4036. * lpfc_sli_hba_setup - SLI intialization function
  4037. * @phba: Pointer to HBA context object.
  4038. *
  4039. * This function is the main SLI intialization function. This function
  4040. * is called by the HBA intialization code, HBA reset code and HBA
  4041. * error attention handler code. Caller is not required to hold any
  4042. * locks. This function issues config_port mailbox command to configure
  4043. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4044. * calls the config_port_post function to issue init_link mailbox
  4045. * command and to start the discovery. The function will return zero
  4046. * if successful, else it will return negative error code.
  4047. **/
  4048. int
  4049. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4050. {
  4051. uint32_t rc;
  4052. int mode = 3, i;
  4053. int longs;
  4054. switch (lpfc_sli_mode) {
  4055. case 2:
  4056. if (phba->cfg_enable_npiv) {
  4057. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4058. "1824 NPIV enabled: Override lpfc_sli_mode "
  4059. "parameter (%d) to auto (0).\n",
  4060. lpfc_sli_mode);
  4061. break;
  4062. }
  4063. mode = 2;
  4064. break;
  4065. case 0:
  4066. case 3:
  4067. break;
  4068. default:
  4069. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4070. "1819 Unrecognized lpfc_sli_mode "
  4071. "parameter: %d.\n", lpfc_sli_mode);
  4072. break;
  4073. }
  4074. rc = lpfc_sli_config_port(phba, mode);
  4075. if (rc && lpfc_sli_mode == 3)
  4076. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4077. "1820 Unable to select SLI-3. "
  4078. "Not supported by adapter.\n");
  4079. if (rc && mode != 2)
  4080. rc = lpfc_sli_config_port(phba, 2);
  4081. if (rc)
  4082. goto lpfc_sli_hba_setup_error;
  4083. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4084. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4085. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4086. if (!rc) {
  4087. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4088. "2709 This device supports "
  4089. "Advanced Error Reporting (AER)\n");
  4090. spin_lock_irq(&phba->hbalock);
  4091. phba->hba_flag |= HBA_AER_ENABLED;
  4092. spin_unlock_irq(&phba->hbalock);
  4093. } else {
  4094. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4095. "2708 This device does not support "
  4096. "Advanced Error Reporting (AER)\n");
  4097. phba->cfg_aer_support = 0;
  4098. }
  4099. }
  4100. if (phba->sli_rev == 3) {
  4101. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4102. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4103. } else {
  4104. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4105. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4106. phba->sli3_options = 0;
  4107. }
  4108. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4109. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4110. phba->sli_rev, phba->max_vpi);
  4111. rc = lpfc_sli_ring_map(phba);
  4112. if (rc)
  4113. goto lpfc_sli_hba_setup_error;
  4114. /* Initialize VPIs. */
  4115. if (phba->sli_rev == LPFC_SLI_REV3) {
  4116. /*
  4117. * The VPI bitmask and physical ID array are allocated
  4118. * and initialized once only - at driver load. A port
  4119. * reset doesn't need to reinitialize this memory.
  4120. */
  4121. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4122. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4123. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4124. GFP_KERNEL);
  4125. if (!phba->vpi_bmask) {
  4126. rc = -ENOMEM;
  4127. goto lpfc_sli_hba_setup_error;
  4128. }
  4129. phba->vpi_ids = kzalloc(
  4130. (phba->max_vpi+1) * sizeof(uint16_t),
  4131. GFP_KERNEL);
  4132. if (!phba->vpi_ids) {
  4133. kfree(phba->vpi_bmask);
  4134. rc = -ENOMEM;
  4135. goto lpfc_sli_hba_setup_error;
  4136. }
  4137. for (i = 0; i < phba->max_vpi; i++)
  4138. phba->vpi_ids[i] = i;
  4139. }
  4140. }
  4141. /* Init HBQs */
  4142. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4143. rc = lpfc_sli_hbq_setup(phba);
  4144. if (rc)
  4145. goto lpfc_sli_hba_setup_error;
  4146. }
  4147. spin_lock_irq(&phba->hbalock);
  4148. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4149. spin_unlock_irq(&phba->hbalock);
  4150. rc = lpfc_config_port_post(phba);
  4151. if (rc)
  4152. goto lpfc_sli_hba_setup_error;
  4153. return rc;
  4154. lpfc_sli_hba_setup_error:
  4155. phba->link_state = LPFC_HBA_ERROR;
  4156. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4157. "0445 Firmware initialization failed\n");
  4158. return rc;
  4159. }
  4160. /**
  4161. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4162. * @phba: Pointer to HBA context object.
  4163. * @mboxq: mailbox pointer.
  4164. * This function issue a dump mailbox command to read config region
  4165. * 23 and parse the records in the region and populate driver
  4166. * data structure.
  4167. **/
  4168. static int
  4169. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4170. {
  4171. LPFC_MBOXQ_t *mboxq;
  4172. struct lpfc_dmabuf *mp;
  4173. struct lpfc_mqe *mqe;
  4174. uint32_t data_length;
  4175. int rc;
  4176. /* Program the default value of vlan_id and fc_map */
  4177. phba->valid_vlan = 0;
  4178. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4179. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4180. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4181. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4182. if (!mboxq)
  4183. return -ENOMEM;
  4184. mqe = &mboxq->u.mqe;
  4185. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4186. rc = -ENOMEM;
  4187. goto out_free_mboxq;
  4188. }
  4189. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4190. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4191. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4192. "(%d):2571 Mailbox cmd x%x Status x%x "
  4193. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4194. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4195. "CQ: x%x x%x x%x x%x\n",
  4196. mboxq->vport ? mboxq->vport->vpi : 0,
  4197. bf_get(lpfc_mqe_command, mqe),
  4198. bf_get(lpfc_mqe_status, mqe),
  4199. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4200. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4201. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4202. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4203. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4204. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4205. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4206. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4207. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4208. mboxq->mcqe.word0,
  4209. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4210. mboxq->mcqe.trailer);
  4211. if (rc) {
  4212. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4213. kfree(mp);
  4214. rc = -EIO;
  4215. goto out_free_mboxq;
  4216. }
  4217. data_length = mqe->un.mb_words[5];
  4218. if (data_length > DMP_RGN23_SIZE) {
  4219. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4220. kfree(mp);
  4221. rc = -EIO;
  4222. goto out_free_mboxq;
  4223. }
  4224. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4225. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4226. kfree(mp);
  4227. rc = 0;
  4228. out_free_mboxq:
  4229. mempool_free(mboxq, phba->mbox_mem_pool);
  4230. return rc;
  4231. }
  4232. /**
  4233. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4234. * @phba: pointer to lpfc hba data structure.
  4235. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4236. * @vpd: pointer to the memory to hold resulting port vpd data.
  4237. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4238. * On output, the number of data bytes in @vpd.
  4239. *
  4240. * This routine executes a READ_REV SLI4 mailbox command. In
  4241. * addition, this routine gets the port vpd data.
  4242. *
  4243. * Return codes
  4244. * 0 - successful
  4245. * -ENOMEM - could not allocated memory.
  4246. **/
  4247. static int
  4248. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4249. uint8_t *vpd, uint32_t *vpd_size)
  4250. {
  4251. int rc = 0;
  4252. uint32_t dma_size;
  4253. struct lpfc_dmabuf *dmabuf;
  4254. struct lpfc_mqe *mqe;
  4255. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4256. if (!dmabuf)
  4257. return -ENOMEM;
  4258. /*
  4259. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4260. * mailbox command.
  4261. */
  4262. dma_size = *vpd_size;
  4263. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4264. dma_size,
  4265. &dmabuf->phys,
  4266. GFP_KERNEL);
  4267. if (!dmabuf->virt) {
  4268. kfree(dmabuf);
  4269. return -ENOMEM;
  4270. }
  4271. memset(dmabuf->virt, 0, dma_size);
  4272. /*
  4273. * The SLI4 implementation of READ_REV conflicts at word1,
  4274. * bits 31:16 and SLI4 adds vpd functionality not present
  4275. * in SLI3. This code corrects the conflicts.
  4276. */
  4277. lpfc_read_rev(phba, mboxq);
  4278. mqe = &mboxq->u.mqe;
  4279. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4280. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4281. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4282. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4283. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4284. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4285. if (rc) {
  4286. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4287. dmabuf->virt, dmabuf->phys);
  4288. kfree(dmabuf);
  4289. return -EIO;
  4290. }
  4291. /*
  4292. * The available vpd length cannot be bigger than the
  4293. * DMA buffer passed to the port. Catch the less than
  4294. * case and update the caller's size.
  4295. */
  4296. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4297. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4298. memcpy(vpd, dmabuf->virt, *vpd_size);
  4299. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4300. dmabuf->virt, dmabuf->phys);
  4301. kfree(dmabuf);
  4302. return 0;
  4303. }
  4304. /**
  4305. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4306. * @phba: pointer to lpfc hba data structure.
  4307. *
  4308. * This routine retrieves SLI4 device physical port name this PCI function
  4309. * is attached to.
  4310. *
  4311. * Return codes
  4312. * 0 - sucessful
  4313. * otherwise - failed to retrieve physical port name
  4314. **/
  4315. static int
  4316. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4317. {
  4318. LPFC_MBOXQ_t *mboxq;
  4319. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4320. struct lpfc_controller_attribute *cntl_attr;
  4321. struct lpfc_mbx_get_port_name *get_port_name;
  4322. void *virtaddr = NULL;
  4323. uint32_t alloclen, reqlen;
  4324. uint32_t shdr_status, shdr_add_status;
  4325. union lpfc_sli4_cfg_shdr *shdr;
  4326. char cport_name = 0;
  4327. int rc;
  4328. /* We assume nothing at this point */
  4329. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4330. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4331. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4332. if (!mboxq)
  4333. return -ENOMEM;
  4334. /* obtain link type and link number via READ_CONFIG */
  4335. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4336. lpfc_sli4_read_config(phba);
  4337. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4338. goto retrieve_ppname;
  4339. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4340. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4341. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4342. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4343. LPFC_SLI4_MBX_NEMBED);
  4344. if (alloclen < reqlen) {
  4345. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4346. "3084 Allocated DMA memory size (%d) is "
  4347. "less than the requested DMA memory size "
  4348. "(%d)\n", alloclen, reqlen);
  4349. rc = -ENOMEM;
  4350. goto out_free_mboxq;
  4351. }
  4352. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4353. virtaddr = mboxq->sge_array->addr[0];
  4354. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4355. shdr = &mbx_cntl_attr->cfg_shdr;
  4356. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4357. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4358. if (shdr_status || shdr_add_status || rc) {
  4359. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4360. "3085 Mailbox x%x (x%x/x%x) failed, "
  4361. "rc:x%x, status:x%x, add_status:x%x\n",
  4362. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4363. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4364. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4365. rc, shdr_status, shdr_add_status);
  4366. rc = -ENXIO;
  4367. goto out_free_mboxq;
  4368. }
  4369. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4370. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4371. phba->sli4_hba.lnk_info.lnk_tp =
  4372. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4373. phba->sli4_hba.lnk_info.lnk_no =
  4374. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4375. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4376. "3086 lnk_type:%d, lnk_numb:%d\n",
  4377. phba->sli4_hba.lnk_info.lnk_tp,
  4378. phba->sli4_hba.lnk_info.lnk_no);
  4379. retrieve_ppname:
  4380. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4381. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4382. sizeof(struct lpfc_mbx_get_port_name) -
  4383. sizeof(struct lpfc_sli4_cfg_mhdr),
  4384. LPFC_SLI4_MBX_EMBED);
  4385. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4386. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4387. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4388. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4389. phba->sli4_hba.lnk_info.lnk_tp);
  4390. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4391. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4392. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4393. if (shdr_status || shdr_add_status || rc) {
  4394. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4395. "3087 Mailbox x%x (x%x/x%x) failed: "
  4396. "rc:x%x, status:x%x, add_status:x%x\n",
  4397. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4398. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4399. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4400. rc, shdr_status, shdr_add_status);
  4401. rc = -ENXIO;
  4402. goto out_free_mboxq;
  4403. }
  4404. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4405. case LPFC_LINK_NUMBER_0:
  4406. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4407. &get_port_name->u.response);
  4408. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4409. break;
  4410. case LPFC_LINK_NUMBER_1:
  4411. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4412. &get_port_name->u.response);
  4413. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4414. break;
  4415. case LPFC_LINK_NUMBER_2:
  4416. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4417. &get_port_name->u.response);
  4418. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4419. break;
  4420. case LPFC_LINK_NUMBER_3:
  4421. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4422. &get_port_name->u.response);
  4423. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4424. break;
  4425. default:
  4426. break;
  4427. }
  4428. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4429. phba->Port[0] = cport_name;
  4430. phba->Port[1] = '\0';
  4431. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4432. "3091 SLI get port name: %s\n", phba->Port);
  4433. }
  4434. out_free_mboxq:
  4435. if (rc != MBX_TIMEOUT) {
  4436. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4437. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4438. else
  4439. mempool_free(mboxq, phba->mbox_mem_pool);
  4440. }
  4441. return rc;
  4442. }
  4443. /**
  4444. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4445. * @phba: pointer to lpfc hba data structure.
  4446. *
  4447. * This routine is called to explicitly arm the SLI4 device's completion and
  4448. * event queues
  4449. **/
  4450. static void
  4451. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4452. {
  4453. uint8_t fcp_eqidx;
  4454. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4455. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4456. fcp_eqidx = 0;
  4457. if (phba->sli4_hba.fcp_cq) {
  4458. do {
  4459. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4460. LPFC_QUEUE_REARM);
  4461. } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
  4462. }
  4463. if (phba->sli4_hba.hba_eq) {
  4464. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
  4465. fcp_eqidx++)
  4466. lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
  4467. LPFC_QUEUE_REARM);
  4468. }
  4469. }
  4470. /**
  4471. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4472. * @phba: Pointer to HBA context object.
  4473. * @type: The resource extent type.
  4474. * @extnt_count: buffer to hold port available extent count.
  4475. * @extnt_size: buffer to hold element count per extent.
  4476. *
  4477. * This function calls the port and retrievs the number of available
  4478. * extents and their size for a particular extent type.
  4479. *
  4480. * Returns: 0 if successful. Nonzero otherwise.
  4481. **/
  4482. int
  4483. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4484. uint16_t *extnt_count, uint16_t *extnt_size)
  4485. {
  4486. int rc = 0;
  4487. uint32_t length;
  4488. uint32_t mbox_tmo;
  4489. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4490. LPFC_MBOXQ_t *mbox;
  4491. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4492. if (!mbox)
  4493. return -ENOMEM;
  4494. /* Find out how many extents are available for this resource type */
  4495. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4496. sizeof(struct lpfc_sli4_cfg_mhdr));
  4497. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4498. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4499. length, LPFC_SLI4_MBX_EMBED);
  4500. /* Send an extents count of 0 - the GET doesn't use it. */
  4501. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4502. LPFC_SLI4_MBX_EMBED);
  4503. if (unlikely(rc)) {
  4504. rc = -EIO;
  4505. goto err_exit;
  4506. }
  4507. if (!phba->sli4_hba.intr_enable)
  4508. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4509. else {
  4510. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4511. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4512. }
  4513. if (unlikely(rc)) {
  4514. rc = -EIO;
  4515. goto err_exit;
  4516. }
  4517. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4518. if (bf_get(lpfc_mbox_hdr_status,
  4519. &rsrc_info->header.cfg_shdr.response)) {
  4520. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4521. "2930 Failed to get resource extents "
  4522. "Status 0x%x Add'l Status 0x%x\n",
  4523. bf_get(lpfc_mbox_hdr_status,
  4524. &rsrc_info->header.cfg_shdr.response),
  4525. bf_get(lpfc_mbox_hdr_add_status,
  4526. &rsrc_info->header.cfg_shdr.response));
  4527. rc = -EIO;
  4528. goto err_exit;
  4529. }
  4530. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4531. &rsrc_info->u.rsp);
  4532. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4533. &rsrc_info->u.rsp);
  4534. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4535. "3162 Retrieved extents type-%d from port: count:%d, "
  4536. "size:%d\n", type, *extnt_count, *extnt_size);
  4537. err_exit:
  4538. mempool_free(mbox, phba->mbox_mem_pool);
  4539. return rc;
  4540. }
  4541. /**
  4542. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4543. * @phba: Pointer to HBA context object.
  4544. * @type: The extent type to check.
  4545. *
  4546. * This function reads the current available extents from the port and checks
  4547. * if the extent count or extent size has changed since the last access.
  4548. * Callers use this routine post port reset to understand if there is a
  4549. * extent reprovisioning requirement.
  4550. *
  4551. * Returns:
  4552. * -Error: error indicates problem.
  4553. * 1: Extent count or size has changed.
  4554. * 0: No changes.
  4555. **/
  4556. static int
  4557. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4558. {
  4559. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4560. uint16_t size_diff, rsrc_ext_size;
  4561. int rc = 0;
  4562. struct lpfc_rsrc_blks *rsrc_entry;
  4563. struct list_head *rsrc_blk_list = NULL;
  4564. size_diff = 0;
  4565. curr_ext_cnt = 0;
  4566. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4567. &rsrc_ext_cnt,
  4568. &rsrc_ext_size);
  4569. if (unlikely(rc))
  4570. return -EIO;
  4571. switch (type) {
  4572. case LPFC_RSC_TYPE_FCOE_RPI:
  4573. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4574. break;
  4575. case LPFC_RSC_TYPE_FCOE_VPI:
  4576. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4577. break;
  4578. case LPFC_RSC_TYPE_FCOE_XRI:
  4579. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4580. break;
  4581. case LPFC_RSC_TYPE_FCOE_VFI:
  4582. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4583. break;
  4584. default:
  4585. break;
  4586. }
  4587. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4588. curr_ext_cnt++;
  4589. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4590. size_diff++;
  4591. }
  4592. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4593. rc = 1;
  4594. return rc;
  4595. }
  4596. /**
  4597. * lpfc_sli4_cfg_post_extnts -
  4598. * @phba: Pointer to HBA context object.
  4599. * @extnt_cnt - number of available extents.
  4600. * @type - the extent type (rpi, xri, vfi, vpi).
  4601. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4602. * @mbox - pointer to the caller's allocated mailbox structure.
  4603. *
  4604. * This function executes the extents allocation request. It also
  4605. * takes care of the amount of memory needed to allocate or get the
  4606. * allocated extents. It is the caller's responsibility to evaluate
  4607. * the response.
  4608. *
  4609. * Returns:
  4610. * -Error: Error value describes the condition found.
  4611. * 0: if successful
  4612. **/
  4613. static int
  4614. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  4615. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4616. {
  4617. int rc = 0;
  4618. uint32_t req_len;
  4619. uint32_t emb_len;
  4620. uint32_t alloc_len, mbox_tmo;
  4621. /* Calculate the total requested length of the dma memory */
  4622. req_len = extnt_cnt * sizeof(uint16_t);
  4623. /*
  4624. * Calculate the size of an embedded mailbox. The uint32_t
  4625. * accounts for extents-specific word.
  4626. */
  4627. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4628. sizeof(uint32_t);
  4629. /*
  4630. * Presume the allocation and response will fit into an embedded
  4631. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4632. */
  4633. *emb = LPFC_SLI4_MBX_EMBED;
  4634. if (req_len > emb_len) {
  4635. req_len = extnt_cnt * sizeof(uint16_t) +
  4636. sizeof(union lpfc_sli4_cfg_shdr) +
  4637. sizeof(uint32_t);
  4638. *emb = LPFC_SLI4_MBX_NEMBED;
  4639. }
  4640. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4641. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4642. req_len, *emb);
  4643. if (alloc_len < req_len) {
  4644. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4645. "2982 Allocated DMA memory size (x%x) is "
  4646. "less than the requested DMA memory "
  4647. "size (x%x)\n", alloc_len, req_len);
  4648. return -ENOMEM;
  4649. }
  4650. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  4651. if (unlikely(rc))
  4652. return -EIO;
  4653. if (!phba->sli4_hba.intr_enable)
  4654. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4655. else {
  4656. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4657. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4658. }
  4659. if (unlikely(rc))
  4660. rc = -EIO;
  4661. return rc;
  4662. }
  4663. /**
  4664. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4665. * @phba: Pointer to HBA context object.
  4666. * @type: The resource extent type to allocate.
  4667. *
  4668. * This function allocates the number of elements for the specified
  4669. * resource type.
  4670. **/
  4671. static int
  4672. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4673. {
  4674. bool emb = false;
  4675. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4676. uint16_t rsrc_id, rsrc_start, j, k;
  4677. uint16_t *ids;
  4678. int i, rc;
  4679. unsigned long longs;
  4680. unsigned long *bmask;
  4681. struct lpfc_rsrc_blks *rsrc_blks;
  4682. LPFC_MBOXQ_t *mbox;
  4683. uint32_t length;
  4684. struct lpfc_id_range *id_array = NULL;
  4685. void *virtaddr = NULL;
  4686. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4687. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4688. struct list_head *ext_blk_list;
  4689. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4690. &rsrc_cnt,
  4691. &rsrc_size);
  4692. if (unlikely(rc))
  4693. return -EIO;
  4694. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4695. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4696. "3009 No available Resource Extents "
  4697. "for resource type 0x%x: Count: 0x%x, "
  4698. "Size 0x%x\n", type, rsrc_cnt,
  4699. rsrc_size);
  4700. return -ENOMEM;
  4701. }
  4702. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  4703. "2903 Post resource extents type-0x%x: "
  4704. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  4705. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4706. if (!mbox)
  4707. return -ENOMEM;
  4708. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  4709. if (unlikely(rc)) {
  4710. rc = -EIO;
  4711. goto err_exit;
  4712. }
  4713. /*
  4714. * Figure out where the response is located. Then get local pointers
  4715. * to the response data. The port does not guarantee to respond to
  4716. * all extents counts request so update the local variable with the
  4717. * allocated count from the port.
  4718. */
  4719. if (emb == LPFC_SLI4_MBX_EMBED) {
  4720. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4721. id_array = &rsrc_ext->u.rsp.id[0];
  4722. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4723. } else {
  4724. virtaddr = mbox->sge_array->addr[0];
  4725. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4726. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4727. id_array = &n_rsrc->id;
  4728. }
  4729. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4730. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4731. /*
  4732. * Based on the resource size and count, correct the base and max
  4733. * resource values.
  4734. */
  4735. length = sizeof(struct lpfc_rsrc_blks);
  4736. switch (type) {
  4737. case LPFC_RSC_TYPE_FCOE_RPI:
  4738. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4739. sizeof(unsigned long),
  4740. GFP_KERNEL);
  4741. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4742. rc = -ENOMEM;
  4743. goto err_exit;
  4744. }
  4745. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4746. sizeof(uint16_t),
  4747. GFP_KERNEL);
  4748. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4749. kfree(phba->sli4_hba.rpi_bmask);
  4750. rc = -ENOMEM;
  4751. goto err_exit;
  4752. }
  4753. /*
  4754. * The next_rpi was initialized with the maximum available
  4755. * count but the port may allocate a smaller number. Catch
  4756. * that case and update the next_rpi.
  4757. */
  4758. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4759. /* Initialize local ptrs for common extent processing later. */
  4760. bmask = phba->sli4_hba.rpi_bmask;
  4761. ids = phba->sli4_hba.rpi_ids;
  4762. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4763. break;
  4764. case LPFC_RSC_TYPE_FCOE_VPI:
  4765. phba->vpi_bmask = kzalloc(longs *
  4766. sizeof(unsigned long),
  4767. GFP_KERNEL);
  4768. if (unlikely(!phba->vpi_bmask)) {
  4769. rc = -ENOMEM;
  4770. goto err_exit;
  4771. }
  4772. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4773. sizeof(uint16_t),
  4774. GFP_KERNEL);
  4775. if (unlikely(!phba->vpi_ids)) {
  4776. kfree(phba->vpi_bmask);
  4777. rc = -ENOMEM;
  4778. goto err_exit;
  4779. }
  4780. /* Initialize local ptrs for common extent processing later. */
  4781. bmask = phba->vpi_bmask;
  4782. ids = phba->vpi_ids;
  4783. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4784. break;
  4785. case LPFC_RSC_TYPE_FCOE_XRI:
  4786. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4787. sizeof(unsigned long),
  4788. GFP_KERNEL);
  4789. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4790. rc = -ENOMEM;
  4791. goto err_exit;
  4792. }
  4793. phba->sli4_hba.max_cfg_param.xri_used = 0;
  4794. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4795. sizeof(uint16_t),
  4796. GFP_KERNEL);
  4797. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4798. kfree(phba->sli4_hba.xri_bmask);
  4799. rc = -ENOMEM;
  4800. goto err_exit;
  4801. }
  4802. /* Initialize local ptrs for common extent processing later. */
  4803. bmask = phba->sli4_hba.xri_bmask;
  4804. ids = phba->sli4_hba.xri_ids;
  4805. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4806. break;
  4807. case LPFC_RSC_TYPE_FCOE_VFI:
  4808. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4809. sizeof(unsigned long),
  4810. GFP_KERNEL);
  4811. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4812. rc = -ENOMEM;
  4813. goto err_exit;
  4814. }
  4815. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4816. sizeof(uint16_t),
  4817. GFP_KERNEL);
  4818. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4819. kfree(phba->sli4_hba.vfi_bmask);
  4820. rc = -ENOMEM;
  4821. goto err_exit;
  4822. }
  4823. /* Initialize local ptrs for common extent processing later. */
  4824. bmask = phba->sli4_hba.vfi_bmask;
  4825. ids = phba->sli4_hba.vfi_ids;
  4826. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4827. break;
  4828. default:
  4829. /* Unsupported Opcode. Fail call. */
  4830. id_array = NULL;
  4831. bmask = NULL;
  4832. ids = NULL;
  4833. ext_blk_list = NULL;
  4834. goto err_exit;
  4835. }
  4836. /*
  4837. * Complete initializing the extent configuration with the
  4838. * allocated ids assigned to this function. The bitmask serves
  4839. * as an index into the array and manages the available ids. The
  4840. * array just stores the ids communicated to the port via the wqes.
  4841. */
  4842. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4843. if ((i % 2) == 0)
  4844. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4845. &id_array[k]);
  4846. else
  4847. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4848. &id_array[k]);
  4849. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4850. if (unlikely(!rsrc_blks)) {
  4851. rc = -ENOMEM;
  4852. kfree(bmask);
  4853. kfree(ids);
  4854. goto err_exit;
  4855. }
  4856. rsrc_blks->rsrc_start = rsrc_id;
  4857. rsrc_blks->rsrc_size = rsrc_size;
  4858. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4859. rsrc_start = rsrc_id;
  4860. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4861. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4862. lpfc_sli4_get_els_iocb_cnt(phba);
  4863. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4864. ids[j] = rsrc_id;
  4865. rsrc_id++;
  4866. j++;
  4867. }
  4868. /* Entire word processed. Get next word.*/
  4869. if ((i % 2) == 1)
  4870. k++;
  4871. }
  4872. err_exit:
  4873. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4874. return rc;
  4875. }
  4876. /**
  4877. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4878. * @phba: Pointer to HBA context object.
  4879. * @type: the extent's type.
  4880. *
  4881. * This function deallocates all extents of a particular resource type.
  4882. * SLI4 does not allow for deallocating a particular extent range. It
  4883. * is the caller's responsibility to release all kernel memory resources.
  4884. **/
  4885. static int
  4886. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4887. {
  4888. int rc;
  4889. uint32_t length, mbox_tmo = 0;
  4890. LPFC_MBOXQ_t *mbox;
  4891. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4892. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4893. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4894. if (!mbox)
  4895. return -ENOMEM;
  4896. /*
  4897. * This function sends an embedded mailbox because it only sends the
  4898. * the resource type. All extents of this type are released by the
  4899. * port.
  4900. */
  4901. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4902. sizeof(struct lpfc_sli4_cfg_mhdr));
  4903. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4904. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4905. length, LPFC_SLI4_MBX_EMBED);
  4906. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4907. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4908. LPFC_SLI4_MBX_EMBED);
  4909. if (unlikely(rc)) {
  4910. rc = -EIO;
  4911. goto out_free_mbox;
  4912. }
  4913. if (!phba->sli4_hba.intr_enable)
  4914. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4915. else {
  4916. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4917. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4918. }
  4919. if (unlikely(rc)) {
  4920. rc = -EIO;
  4921. goto out_free_mbox;
  4922. }
  4923. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4924. if (bf_get(lpfc_mbox_hdr_status,
  4925. &dealloc_rsrc->header.cfg_shdr.response)) {
  4926. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4927. "2919 Failed to release resource extents "
  4928. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4929. "Resource memory not released.\n",
  4930. type,
  4931. bf_get(lpfc_mbox_hdr_status,
  4932. &dealloc_rsrc->header.cfg_shdr.response),
  4933. bf_get(lpfc_mbox_hdr_add_status,
  4934. &dealloc_rsrc->header.cfg_shdr.response));
  4935. rc = -EIO;
  4936. goto out_free_mbox;
  4937. }
  4938. /* Release kernel memory resources for the specific type. */
  4939. switch (type) {
  4940. case LPFC_RSC_TYPE_FCOE_VPI:
  4941. kfree(phba->vpi_bmask);
  4942. kfree(phba->vpi_ids);
  4943. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4944. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4945. &phba->lpfc_vpi_blk_list, list) {
  4946. list_del_init(&rsrc_blk->list);
  4947. kfree(rsrc_blk);
  4948. }
  4949. break;
  4950. case LPFC_RSC_TYPE_FCOE_XRI:
  4951. kfree(phba->sli4_hba.xri_bmask);
  4952. kfree(phba->sli4_hba.xri_ids);
  4953. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4954. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4955. list_del_init(&rsrc_blk->list);
  4956. kfree(rsrc_blk);
  4957. }
  4958. break;
  4959. case LPFC_RSC_TYPE_FCOE_VFI:
  4960. kfree(phba->sli4_hba.vfi_bmask);
  4961. kfree(phba->sli4_hba.vfi_ids);
  4962. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4963. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4964. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  4965. list_del_init(&rsrc_blk->list);
  4966. kfree(rsrc_blk);
  4967. }
  4968. break;
  4969. case LPFC_RSC_TYPE_FCOE_RPI:
  4970. /* RPI bitmask and physical id array are cleaned up earlier. */
  4971. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4972. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  4973. list_del_init(&rsrc_blk->list);
  4974. kfree(rsrc_blk);
  4975. }
  4976. break;
  4977. default:
  4978. break;
  4979. }
  4980. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4981. out_free_mbox:
  4982. mempool_free(mbox, phba->mbox_mem_pool);
  4983. return rc;
  4984. }
  4985. /**
  4986. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  4987. * @phba: Pointer to HBA context object.
  4988. *
  4989. * This function allocates all SLI4 resource identifiers.
  4990. **/
  4991. int
  4992. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  4993. {
  4994. int i, rc, error = 0;
  4995. uint16_t count, base;
  4996. unsigned long longs;
  4997. if (!phba->sli4_hba.rpi_hdrs_in_use)
  4998. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  4999. if (phba->sli4_hba.extents_in_use) {
  5000. /*
  5001. * The port supports resource extents. The XRI, VPI, VFI, RPI
  5002. * resource extent count must be read and allocated before
  5003. * provisioning the resource id arrays.
  5004. */
  5005. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5006. LPFC_IDX_RSRC_RDY) {
  5007. /*
  5008. * Extent-based resources are set - the driver could
  5009. * be in a port reset. Figure out if any corrective
  5010. * actions need to be taken.
  5011. */
  5012. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5013. LPFC_RSC_TYPE_FCOE_VFI);
  5014. if (rc != 0)
  5015. error++;
  5016. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5017. LPFC_RSC_TYPE_FCOE_VPI);
  5018. if (rc != 0)
  5019. error++;
  5020. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5021. LPFC_RSC_TYPE_FCOE_XRI);
  5022. if (rc != 0)
  5023. error++;
  5024. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5025. LPFC_RSC_TYPE_FCOE_RPI);
  5026. if (rc != 0)
  5027. error++;
  5028. /*
  5029. * It's possible that the number of resources
  5030. * provided to this port instance changed between
  5031. * resets. Detect this condition and reallocate
  5032. * resources. Otherwise, there is no action.
  5033. */
  5034. if (error) {
  5035. lpfc_printf_log(phba, KERN_INFO,
  5036. LOG_MBOX | LOG_INIT,
  5037. "2931 Detected extent resource "
  5038. "change. Reallocating all "
  5039. "extents.\n");
  5040. rc = lpfc_sli4_dealloc_extent(phba,
  5041. LPFC_RSC_TYPE_FCOE_VFI);
  5042. rc = lpfc_sli4_dealloc_extent(phba,
  5043. LPFC_RSC_TYPE_FCOE_VPI);
  5044. rc = lpfc_sli4_dealloc_extent(phba,
  5045. LPFC_RSC_TYPE_FCOE_XRI);
  5046. rc = lpfc_sli4_dealloc_extent(phba,
  5047. LPFC_RSC_TYPE_FCOE_RPI);
  5048. } else
  5049. return 0;
  5050. }
  5051. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5052. if (unlikely(rc))
  5053. goto err_exit;
  5054. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5055. if (unlikely(rc))
  5056. goto err_exit;
  5057. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5058. if (unlikely(rc))
  5059. goto err_exit;
  5060. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5061. if (unlikely(rc))
  5062. goto err_exit;
  5063. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5064. LPFC_IDX_RSRC_RDY);
  5065. return rc;
  5066. } else {
  5067. /*
  5068. * The port does not support resource extents. The XRI, VPI,
  5069. * VFI, RPI resource ids were determined from READ_CONFIG.
  5070. * Just allocate the bitmasks and provision the resource id
  5071. * arrays. If a port reset is active, the resources don't
  5072. * need any action - just exit.
  5073. */
  5074. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5075. LPFC_IDX_RSRC_RDY) {
  5076. lpfc_sli4_dealloc_resource_identifiers(phba);
  5077. lpfc_sli4_remove_rpis(phba);
  5078. }
  5079. /* RPIs. */
  5080. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5081. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5082. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5083. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5084. sizeof(unsigned long),
  5085. GFP_KERNEL);
  5086. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5087. rc = -ENOMEM;
  5088. goto err_exit;
  5089. }
  5090. phba->sli4_hba.rpi_ids = kzalloc(count *
  5091. sizeof(uint16_t),
  5092. GFP_KERNEL);
  5093. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5094. rc = -ENOMEM;
  5095. goto free_rpi_bmask;
  5096. }
  5097. for (i = 0; i < count; i++)
  5098. phba->sli4_hba.rpi_ids[i] = base + i;
  5099. /* VPIs. */
  5100. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5101. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5102. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5103. phba->vpi_bmask = kzalloc(longs *
  5104. sizeof(unsigned long),
  5105. GFP_KERNEL);
  5106. if (unlikely(!phba->vpi_bmask)) {
  5107. rc = -ENOMEM;
  5108. goto free_rpi_ids;
  5109. }
  5110. phba->vpi_ids = kzalloc(count *
  5111. sizeof(uint16_t),
  5112. GFP_KERNEL);
  5113. if (unlikely(!phba->vpi_ids)) {
  5114. rc = -ENOMEM;
  5115. goto free_vpi_bmask;
  5116. }
  5117. for (i = 0; i < count; i++)
  5118. phba->vpi_ids[i] = base + i;
  5119. /* XRIs. */
  5120. count = phba->sli4_hba.max_cfg_param.max_xri;
  5121. base = phba->sli4_hba.max_cfg_param.xri_base;
  5122. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5123. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5124. sizeof(unsigned long),
  5125. GFP_KERNEL);
  5126. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5127. rc = -ENOMEM;
  5128. goto free_vpi_ids;
  5129. }
  5130. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5131. phba->sli4_hba.xri_ids = kzalloc(count *
  5132. sizeof(uint16_t),
  5133. GFP_KERNEL);
  5134. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5135. rc = -ENOMEM;
  5136. goto free_xri_bmask;
  5137. }
  5138. for (i = 0; i < count; i++)
  5139. phba->sli4_hba.xri_ids[i] = base + i;
  5140. /* VFIs. */
  5141. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5142. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5143. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5144. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5145. sizeof(unsigned long),
  5146. GFP_KERNEL);
  5147. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5148. rc = -ENOMEM;
  5149. goto free_xri_ids;
  5150. }
  5151. phba->sli4_hba.vfi_ids = kzalloc(count *
  5152. sizeof(uint16_t),
  5153. GFP_KERNEL);
  5154. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5155. rc = -ENOMEM;
  5156. goto free_vfi_bmask;
  5157. }
  5158. for (i = 0; i < count; i++)
  5159. phba->sli4_hba.vfi_ids[i] = base + i;
  5160. /*
  5161. * Mark all resources ready. An HBA reset doesn't need
  5162. * to reset the initialization.
  5163. */
  5164. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5165. LPFC_IDX_RSRC_RDY);
  5166. return 0;
  5167. }
  5168. free_vfi_bmask:
  5169. kfree(phba->sli4_hba.vfi_bmask);
  5170. free_xri_ids:
  5171. kfree(phba->sli4_hba.xri_ids);
  5172. free_xri_bmask:
  5173. kfree(phba->sli4_hba.xri_bmask);
  5174. free_vpi_ids:
  5175. kfree(phba->vpi_ids);
  5176. free_vpi_bmask:
  5177. kfree(phba->vpi_bmask);
  5178. free_rpi_ids:
  5179. kfree(phba->sli4_hba.rpi_ids);
  5180. free_rpi_bmask:
  5181. kfree(phba->sli4_hba.rpi_bmask);
  5182. err_exit:
  5183. return rc;
  5184. }
  5185. /**
  5186. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5187. * @phba: Pointer to HBA context object.
  5188. *
  5189. * This function allocates the number of elements for the specified
  5190. * resource type.
  5191. **/
  5192. int
  5193. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5194. {
  5195. if (phba->sli4_hba.extents_in_use) {
  5196. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5197. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5198. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5199. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5200. } else {
  5201. kfree(phba->vpi_bmask);
  5202. kfree(phba->vpi_ids);
  5203. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5204. kfree(phba->sli4_hba.xri_bmask);
  5205. kfree(phba->sli4_hba.xri_ids);
  5206. kfree(phba->sli4_hba.vfi_bmask);
  5207. kfree(phba->sli4_hba.vfi_ids);
  5208. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5209. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5210. }
  5211. return 0;
  5212. }
  5213. /**
  5214. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5215. * @phba: Pointer to HBA context object.
  5216. * @type: The resource extent type.
  5217. * @extnt_count: buffer to hold port extent count response
  5218. * @extnt_size: buffer to hold port extent size response.
  5219. *
  5220. * This function calls the port to read the host allocated extents
  5221. * for a particular type.
  5222. **/
  5223. int
  5224. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5225. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5226. {
  5227. bool emb;
  5228. int rc = 0;
  5229. uint16_t curr_blks = 0;
  5230. uint32_t req_len, emb_len;
  5231. uint32_t alloc_len, mbox_tmo;
  5232. struct list_head *blk_list_head;
  5233. struct lpfc_rsrc_blks *rsrc_blk;
  5234. LPFC_MBOXQ_t *mbox;
  5235. void *virtaddr = NULL;
  5236. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5237. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5238. union lpfc_sli4_cfg_shdr *shdr;
  5239. switch (type) {
  5240. case LPFC_RSC_TYPE_FCOE_VPI:
  5241. blk_list_head = &phba->lpfc_vpi_blk_list;
  5242. break;
  5243. case LPFC_RSC_TYPE_FCOE_XRI:
  5244. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5245. break;
  5246. case LPFC_RSC_TYPE_FCOE_VFI:
  5247. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5248. break;
  5249. case LPFC_RSC_TYPE_FCOE_RPI:
  5250. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5251. break;
  5252. default:
  5253. return -EIO;
  5254. }
  5255. /* Count the number of extents currently allocatd for this type. */
  5256. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5257. if (curr_blks == 0) {
  5258. /*
  5259. * The GET_ALLOCATED mailbox does not return the size,
  5260. * just the count. The size should be just the size
  5261. * stored in the current allocated block and all sizes
  5262. * for an extent type are the same so set the return
  5263. * value now.
  5264. */
  5265. *extnt_size = rsrc_blk->rsrc_size;
  5266. }
  5267. curr_blks++;
  5268. }
  5269. /* Calculate the total requested length of the dma memory. */
  5270. req_len = curr_blks * sizeof(uint16_t);
  5271. /*
  5272. * Calculate the size of an embedded mailbox. The uint32_t
  5273. * accounts for extents-specific word.
  5274. */
  5275. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5276. sizeof(uint32_t);
  5277. /*
  5278. * Presume the allocation and response will fit into an embedded
  5279. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5280. */
  5281. emb = LPFC_SLI4_MBX_EMBED;
  5282. req_len = emb_len;
  5283. if (req_len > emb_len) {
  5284. req_len = curr_blks * sizeof(uint16_t) +
  5285. sizeof(union lpfc_sli4_cfg_shdr) +
  5286. sizeof(uint32_t);
  5287. emb = LPFC_SLI4_MBX_NEMBED;
  5288. }
  5289. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5290. if (!mbox)
  5291. return -ENOMEM;
  5292. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5293. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5294. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5295. req_len, emb);
  5296. if (alloc_len < req_len) {
  5297. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5298. "2983 Allocated DMA memory size (x%x) is "
  5299. "less than the requested DMA memory "
  5300. "size (x%x)\n", alloc_len, req_len);
  5301. rc = -ENOMEM;
  5302. goto err_exit;
  5303. }
  5304. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5305. if (unlikely(rc)) {
  5306. rc = -EIO;
  5307. goto err_exit;
  5308. }
  5309. if (!phba->sli4_hba.intr_enable)
  5310. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5311. else {
  5312. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5313. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5314. }
  5315. if (unlikely(rc)) {
  5316. rc = -EIO;
  5317. goto err_exit;
  5318. }
  5319. /*
  5320. * Figure out where the response is located. Then get local pointers
  5321. * to the response data. The port does not guarantee to respond to
  5322. * all extents counts request so update the local variable with the
  5323. * allocated count from the port.
  5324. */
  5325. if (emb == LPFC_SLI4_MBX_EMBED) {
  5326. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5327. shdr = &rsrc_ext->header.cfg_shdr;
  5328. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5329. } else {
  5330. virtaddr = mbox->sge_array->addr[0];
  5331. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5332. shdr = &n_rsrc->cfg_shdr;
  5333. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5334. }
  5335. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5336. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5337. "2984 Failed to read allocated resources "
  5338. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5339. type,
  5340. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5341. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5342. rc = -EIO;
  5343. goto err_exit;
  5344. }
  5345. err_exit:
  5346. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5347. return rc;
  5348. }
  5349. /**
  5350. * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
  5351. * @phba: pointer to lpfc hba data structure.
  5352. *
  5353. * This routine walks the list of els buffers that have been allocated and
  5354. * repost them to the port by using SGL block post. This is needed after a
  5355. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5356. * of els buffer sgls which contains contiguous xris and uses the non-embedded
  5357. * SGL block post mailbox commands to post them to the port. For single els
  5358. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5359. * mailbox command for posting.
  5360. *
  5361. * Returns: 0 = success, non-zero failure.
  5362. **/
  5363. static int
  5364. lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
  5365. {
  5366. struct lpfc_sglq *sglq_entry = NULL;
  5367. struct lpfc_sglq *sglq_entry_next = NULL;
  5368. struct lpfc_sglq *sglq_entry_first = NULL;
  5369. int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
  5370. int last_xritag = NO_XRI;
  5371. LIST_HEAD(prep_sgl_list);
  5372. LIST_HEAD(blck_sgl_list);
  5373. LIST_HEAD(allc_sgl_list);
  5374. LIST_HEAD(post_sgl_list);
  5375. LIST_HEAD(free_sgl_list);
  5376. spin_lock(&phba->hbalock);
  5377. list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
  5378. spin_unlock(&phba->hbalock);
  5379. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  5380. &allc_sgl_list, list) {
  5381. list_del_init(&sglq_entry->list);
  5382. block_cnt++;
  5383. if ((last_xritag != NO_XRI) &&
  5384. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  5385. /* a hole in xri block, form a sgl posting block */
  5386. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  5387. post_cnt = block_cnt - 1;
  5388. /* prepare list for next posting block */
  5389. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5390. block_cnt = 1;
  5391. } else {
  5392. /* prepare list for next posting block */
  5393. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5394. /* enough sgls for non-embed sgl mbox command */
  5395. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  5396. list_splice_init(&prep_sgl_list,
  5397. &blck_sgl_list);
  5398. post_cnt = block_cnt;
  5399. block_cnt = 0;
  5400. }
  5401. }
  5402. num_posted++;
  5403. /* keep track of last sgl's xritag */
  5404. last_xritag = sglq_entry->sli4_xritag;
  5405. /* end of repost sgl list condition for els buffers */
  5406. if (num_posted == phba->sli4_hba.els_xri_cnt) {
  5407. if (post_cnt == 0) {
  5408. list_splice_init(&prep_sgl_list,
  5409. &blck_sgl_list);
  5410. post_cnt = block_cnt;
  5411. } else if (block_cnt == 1) {
  5412. status = lpfc_sli4_post_sgl(phba,
  5413. sglq_entry->phys, 0,
  5414. sglq_entry->sli4_xritag);
  5415. if (!status) {
  5416. /* successful, put sgl to posted list */
  5417. list_add_tail(&sglq_entry->list,
  5418. &post_sgl_list);
  5419. } else {
  5420. /* Failure, put sgl to free list */
  5421. lpfc_printf_log(phba, KERN_WARNING,
  5422. LOG_SLI,
  5423. "3159 Failed to post els "
  5424. "sgl, xritag:x%x\n",
  5425. sglq_entry->sli4_xritag);
  5426. list_add_tail(&sglq_entry->list,
  5427. &free_sgl_list);
  5428. spin_lock_irq(&phba->hbalock);
  5429. phba->sli4_hba.els_xri_cnt--;
  5430. spin_unlock_irq(&phba->hbalock);
  5431. }
  5432. }
  5433. }
  5434. /* continue until a nembed page worth of sgls */
  5435. if (post_cnt == 0)
  5436. continue;
  5437. /* post the els buffer list sgls as a block */
  5438. status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
  5439. post_cnt);
  5440. if (!status) {
  5441. /* success, put sgl list to posted sgl list */
  5442. list_splice_init(&blck_sgl_list, &post_sgl_list);
  5443. } else {
  5444. /* Failure, put sgl list to free sgl list */
  5445. sglq_entry_first = list_first_entry(&blck_sgl_list,
  5446. struct lpfc_sglq,
  5447. list);
  5448. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5449. "3160 Failed to post els sgl-list, "
  5450. "xritag:x%x-x%x\n",
  5451. sglq_entry_first->sli4_xritag,
  5452. (sglq_entry_first->sli4_xritag +
  5453. post_cnt - 1));
  5454. list_splice_init(&blck_sgl_list, &free_sgl_list);
  5455. spin_lock_irq(&phba->hbalock);
  5456. phba->sli4_hba.els_xri_cnt -= post_cnt;
  5457. spin_unlock_irq(&phba->hbalock);
  5458. }
  5459. /* don't reset xirtag due to hole in xri block */
  5460. if (block_cnt == 0)
  5461. last_xritag = NO_XRI;
  5462. /* reset els sgl post count for next round of posting */
  5463. post_cnt = 0;
  5464. }
  5465. /* free the els sgls failed to post */
  5466. lpfc_free_sgl_list(phba, &free_sgl_list);
  5467. /* push els sgls posted to the availble list */
  5468. if (!list_empty(&post_sgl_list)) {
  5469. spin_lock(&phba->hbalock);
  5470. list_splice_init(&post_sgl_list,
  5471. &phba->sli4_hba.lpfc_sgl_list);
  5472. spin_unlock(&phba->hbalock);
  5473. } else {
  5474. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5475. "3161 Failure to post els sgl to port.\n");
  5476. return -EIO;
  5477. }
  5478. return 0;
  5479. }
  5480. /**
  5481. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5482. * @phba: Pointer to HBA context object.
  5483. *
  5484. * This function is the main SLI4 device intialization PCI function. This
  5485. * function is called by the HBA intialization code, HBA reset code and
  5486. * HBA error attention handler code. Caller is not required to hold any
  5487. * locks.
  5488. **/
  5489. int
  5490. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5491. {
  5492. int rc;
  5493. LPFC_MBOXQ_t *mboxq;
  5494. struct lpfc_mqe *mqe;
  5495. uint8_t *vpd;
  5496. uint32_t vpd_size;
  5497. uint32_t ftr_rsp = 0;
  5498. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5499. struct lpfc_vport *vport = phba->pport;
  5500. struct lpfc_dmabuf *mp;
  5501. /* Perform a PCI function reset to start from clean */
  5502. rc = lpfc_pci_function_reset(phba);
  5503. if (unlikely(rc))
  5504. return -ENODEV;
  5505. /* Check the HBA Host Status Register for readyness */
  5506. rc = lpfc_sli4_post_status_check(phba);
  5507. if (unlikely(rc))
  5508. return -ENODEV;
  5509. else {
  5510. spin_lock_irq(&phba->hbalock);
  5511. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5512. spin_unlock_irq(&phba->hbalock);
  5513. }
  5514. /*
  5515. * Allocate a single mailbox container for initializing the
  5516. * port.
  5517. */
  5518. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5519. if (!mboxq)
  5520. return -ENOMEM;
  5521. /* Issue READ_REV to collect vpd and FW information. */
  5522. vpd_size = SLI4_PAGE_SIZE;
  5523. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5524. if (!vpd) {
  5525. rc = -ENOMEM;
  5526. goto out_free_mbox;
  5527. }
  5528. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5529. if (unlikely(rc)) {
  5530. kfree(vpd);
  5531. goto out_free_mbox;
  5532. }
  5533. mqe = &mboxq->u.mqe;
  5534. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5535. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5536. phba->hba_flag |= HBA_FCOE_MODE;
  5537. else
  5538. phba->hba_flag &= ~HBA_FCOE_MODE;
  5539. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5540. LPFC_DCBX_CEE_MODE)
  5541. phba->hba_flag |= HBA_FIP_SUPPORT;
  5542. else
  5543. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5544. phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
  5545. if (phba->sli_rev != LPFC_SLI_REV4) {
  5546. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5547. "0376 READ_REV Error. SLI Level %d "
  5548. "FCoE enabled %d\n",
  5549. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5550. rc = -EIO;
  5551. kfree(vpd);
  5552. goto out_free_mbox;
  5553. }
  5554. /*
  5555. * Continue initialization with default values even if driver failed
  5556. * to read FCoE param config regions, only read parameters if the
  5557. * board is FCoE
  5558. */
  5559. if (phba->hba_flag & HBA_FCOE_MODE &&
  5560. lpfc_sli4_read_fcoe_params(phba))
  5561. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5562. "2570 Failed to read FCoE parameters\n");
  5563. /*
  5564. * Retrieve sli4 device physical port name, failure of doing it
  5565. * is considered as non-fatal.
  5566. */
  5567. rc = lpfc_sli4_retrieve_pport_name(phba);
  5568. if (!rc)
  5569. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5570. "3080 Successful retrieving SLI4 device "
  5571. "physical port name: %s.\n", phba->Port);
  5572. /*
  5573. * Evaluate the read rev and vpd data. Populate the driver
  5574. * state with the results. If this routine fails, the failure
  5575. * is not fatal as the driver will use generic values.
  5576. */
  5577. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5578. if (unlikely(!rc)) {
  5579. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5580. "0377 Error %d parsing vpd. "
  5581. "Using defaults.\n", rc);
  5582. rc = 0;
  5583. }
  5584. kfree(vpd);
  5585. /* Save information as VPD data */
  5586. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5587. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5588. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5589. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5590. &mqe->un.read_rev);
  5591. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5592. &mqe->un.read_rev);
  5593. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5594. &mqe->un.read_rev);
  5595. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5596. &mqe->un.read_rev);
  5597. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5598. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5599. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5600. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5601. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5602. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5603. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5604. "(%d):0380 READ_REV Status x%x "
  5605. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5606. mboxq->vport ? mboxq->vport->vpi : 0,
  5607. bf_get(lpfc_mqe_status, mqe),
  5608. phba->vpd.rev.opFwName,
  5609. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5610. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5611. /*
  5612. * Discover the port's supported feature set and match it against the
  5613. * hosts requests.
  5614. */
  5615. lpfc_request_features(phba, mboxq);
  5616. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5617. if (unlikely(rc)) {
  5618. rc = -EIO;
  5619. goto out_free_mbox;
  5620. }
  5621. /*
  5622. * The port must support FCP initiator mode as this is the
  5623. * only mode running in the host.
  5624. */
  5625. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5626. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5627. "0378 No support for fcpi mode.\n");
  5628. ftr_rsp++;
  5629. }
  5630. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5631. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5632. else
  5633. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5634. /*
  5635. * If the port cannot support the host's requested features
  5636. * then turn off the global config parameters to disable the
  5637. * feature in the driver. This is not a fatal error.
  5638. */
  5639. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5640. if (phba->cfg_enable_bg) {
  5641. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5642. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5643. else
  5644. ftr_rsp++;
  5645. }
  5646. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5647. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5648. ftr_rsp++;
  5649. if (ftr_rsp) {
  5650. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5651. "0379 Feature Mismatch Data: x%08x %08x "
  5652. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5653. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5654. phba->cfg_enable_npiv, phba->max_vpi);
  5655. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5656. phba->cfg_enable_bg = 0;
  5657. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5658. phba->cfg_enable_npiv = 0;
  5659. }
  5660. /* These SLI3 features are assumed in SLI4 */
  5661. spin_lock_irq(&phba->hbalock);
  5662. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5663. spin_unlock_irq(&phba->hbalock);
  5664. /*
  5665. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5666. * calls depends on these resources to complete port setup.
  5667. */
  5668. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5669. if (rc) {
  5670. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5671. "2920 Failed to alloc Resource IDs "
  5672. "rc = x%x\n", rc);
  5673. goto out_free_mbox;
  5674. }
  5675. /* Read the port's service parameters. */
  5676. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5677. if (rc) {
  5678. phba->link_state = LPFC_HBA_ERROR;
  5679. rc = -ENOMEM;
  5680. goto out_free_mbox;
  5681. }
  5682. mboxq->vport = vport;
  5683. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5684. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5685. if (rc == MBX_SUCCESS) {
  5686. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5687. rc = 0;
  5688. }
  5689. /*
  5690. * This memory was allocated by the lpfc_read_sparam routine. Release
  5691. * it to the mbuf pool.
  5692. */
  5693. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5694. kfree(mp);
  5695. mboxq->context1 = NULL;
  5696. if (unlikely(rc)) {
  5697. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5698. "0382 READ_SPARAM command failed "
  5699. "status %d, mbxStatus x%x\n",
  5700. rc, bf_get(lpfc_mqe_status, mqe));
  5701. phba->link_state = LPFC_HBA_ERROR;
  5702. rc = -EIO;
  5703. goto out_free_mbox;
  5704. }
  5705. lpfc_update_vport_wwn(vport);
  5706. /* Update the fc_host data structures with new wwn. */
  5707. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5708. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5709. /* update host els and scsi xri-sgl sizes and mappings */
  5710. rc = lpfc_sli4_xri_sgl_update(phba);
  5711. if (unlikely(rc)) {
  5712. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5713. "1400 Failed to update xri-sgl size and "
  5714. "mapping: %d\n", rc);
  5715. goto out_free_mbox;
  5716. }
  5717. /* register the els sgl pool to the port */
  5718. rc = lpfc_sli4_repost_els_sgl_list(phba);
  5719. if (unlikely(rc)) {
  5720. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5721. "0582 Error %d during els sgl post "
  5722. "operation\n", rc);
  5723. rc = -ENODEV;
  5724. goto out_free_mbox;
  5725. }
  5726. /* register the allocated scsi sgl pool to the port */
  5727. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5728. if (unlikely(rc)) {
  5729. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5730. "0383 Error %d during scsi sgl post "
  5731. "operation\n", rc);
  5732. /* Some Scsi buffers were moved to the abort scsi list */
  5733. /* A pci function reset will repost them */
  5734. rc = -ENODEV;
  5735. goto out_free_mbox;
  5736. }
  5737. /* Post the rpi header region to the device. */
  5738. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5739. if (unlikely(rc)) {
  5740. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5741. "0393 Error %d during rpi post operation\n",
  5742. rc);
  5743. rc = -ENODEV;
  5744. goto out_free_mbox;
  5745. }
  5746. lpfc_sli4_node_prep(phba);
  5747. /* Create all the SLI4 queues */
  5748. rc = lpfc_sli4_queue_create(phba);
  5749. if (rc) {
  5750. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5751. "3089 Failed to allocate queues\n");
  5752. rc = -ENODEV;
  5753. goto out_stop_timers;
  5754. }
  5755. /* Set up all the queues to the device */
  5756. rc = lpfc_sli4_queue_setup(phba);
  5757. if (unlikely(rc)) {
  5758. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5759. "0381 Error %d during queue setup.\n ", rc);
  5760. goto out_destroy_queue;
  5761. }
  5762. /* Arm the CQs and then EQs on device */
  5763. lpfc_sli4_arm_cqeq_intr(phba);
  5764. /* Indicate device interrupt mode */
  5765. phba->sli4_hba.intr_enable = 1;
  5766. /* Allow asynchronous mailbox command to go through */
  5767. spin_lock_irq(&phba->hbalock);
  5768. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5769. spin_unlock_irq(&phba->hbalock);
  5770. /* Post receive buffers to the device */
  5771. lpfc_sli4_rb_setup(phba);
  5772. /* Reset HBA FCF states after HBA reset */
  5773. phba->fcf.fcf_flag = 0;
  5774. phba->fcf.current_rec.flag = 0;
  5775. /* Start the ELS watchdog timer */
  5776. mod_timer(&vport->els_tmofunc,
  5777. jiffies + HZ * (phba->fc_ratov * 2));
  5778. /* Start heart beat timer */
  5779. mod_timer(&phba->hb_tmofunc,
  5780. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5781. phba->hb_outstanding = 0;
  5782. phba->last_completion_time = jiffies;
  5783. /* Start error attention (ERATT) polling timer */
  5784. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5785. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5786. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5787. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5788. if (!rc) {
  5789. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5790. "2829 This device supports "
  5791. "Advanced Error Reporting (AER)\n");
  5792. spin_lock_irq(&phba->hbalock);
  5793. phba->hba_flag |= HBA_AER_ENABLED;
  5794. spin_unlock_irq(&phba->hbalock);
  5795. } else {
  5796. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5797. "2830 This device does not support "
  5798. "Advanced Error Reporting (AER)\n");
  5799. phba->cfg_aer_support = 0;
  5800. }
  5801. rc = 0;
  5802. }
  5803. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5804. /*
  5805. * The FC Port needs to register FCFI (index 0)
  5806. */
  5807. lpfc_reg_fcfi(phba, mboxq);
  5808. mboxq->vport = phba->pport;
  5809. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5810. if (rc != MBX_SUCCESS)
  5811. goto out_unset_queue;
  5812. rc = 0;
  5813. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5814. &mboxq->u.mqe.un.reg_fcfi);
  5815. /* Check if the port is configured to be disabled */
  5816. lpfc_sli_read_link_ste(phba);
  5817. }
  5818. /*
  5819. * The port is ready, set the host's link state to LINK_DOWN
  5820. * in preparation for link interrupts.
  5821. */
  5822. spin_lock_irq(&phba->hbalock);
  5823. phba->link_state = LPFC_LINK_DOWN;
  5824. spin_unlock_irq(&phba->hbalock);
  5825. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5826. (phba->hba_flag & LINK_DISABLED)) {
  5827. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5828. "3103 Adapter Link is disabled.\n");
  5829. lpfc_down_link(phba, mboxq);
  5830. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5831. if (rc != MBX_SUCCESS) {
  5832. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5833. "3104 Adapter failed to issue "
  5834. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5835. goto out_unset_queue;
  5836. }
  5837. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5838. /* don't perform init_link on SLI4 FC port loopback test */
  5839. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5840. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5841. if (rc)
  5842. goto out_unset_queue;
  5843. }
  5844. }
  5845. mempool_free(mboxq, phba->mbox_mem_pool);
  5846. return rc;
  5847. out_unset_queue:
  5848. /* Unset all the queues set up in this routine when error out */
  5849. lpfc_sli4_queue_unset(phba);
  5850. out_destroy_queue:
  5851. lpfc_sli4_queue_destroy(phba);
  5852. out_stop_timers:
  5853. lpfc_stop_hba_timers(phba);
  5854. out_free_mbox:
  5855. mempool_free(mboxq, phba->mbox_mem_pool);
  5856. return rc;
  5857. }
  5858. /**
  5859. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5860. * @ptr: context object - pointer to hba structure.
  5861. *
  5862. * This is the callback function for mailbox timer. The mailbox
  5863. * timer is armed when a new mailbox command is issued and the timer
  5864. * is deleted when the mailbox complete. The function is called by
  5865. * the kernel timer code when a mailbox does not complete within
  5866. * expected time. This function wakes up the worker thread to
  5867. * process the mailbox timeout and returns. All the processing is
  5868. * done by the worker thread function lpfc_mbox_timeout_handler.
  5869. **/
  5870. void
  5871. lpfc_mbox_timeout(unsigned long ptr)
  5872. {
  5873. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5874. unsigned long iflag;
  5875. uint32_t tmo_posted;
  5876. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5877. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5878. if (!tmo_posted)
  5879. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5880. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5881. if (!tmo_posted)
  5882. lpfc_worker_wake_up(phba);
  5883. return;
  5884. }
  5885. /**
  5886. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5887. * @phba: Pointer to HBA context object.
  5888. *
  5889. * This function is called from worker thread when a mailbox command times out.
  5890. * The caller is not required to hold any locks. This function will reset the
  5891. * HBA and recover all the pending commands.
  5892. **/
  5893. void
  5894. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5895. {
  5896. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5897. MAILBOX_t *mb = &pmbox->u.mb;
  5898. struct lpfc_sli *psli = &phba->sli;
  5899. struct lpfc_sli_ring *pring;
  5900. /* Check the pmbox pointer first. There is a race condition
  5901. * between the mbox timeout handler getting executed in the
  5902. * worklist and the mailbox actually completing. When this
  5903. * race condition occurs, the mbox_active will be NULL.
  5904. */
  5905. spin_lock_irq(&phba->hbalock);
  5906. if (pmbox == NULL) {
  5907. lpfc_printf_log(phba, KERN_WARNING,
  5908. LOG_MBOX | LOG_SLI,
  5909. "0353 Active Mailbox cleared - mailbox timeout "
  5910. "exiting\n");
  5911. spin_unlock_irq(&phba->hbalock);
  5912. return;
  5913. }
  5914. /* Mbox cmd <mbxCommand> timeout */
  5915. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5916. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5917. mb->mbxCommand,
  5918. phba->pport->port_state,
  5919. phba->sli.sli_flag,
  5920. phba->sli.mbox_active);
  5921. spin_unlock_irq(&phba->hbalock);
  5922. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5923. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5924. * it to fail all outstanding SCSI IO.
  5925. */
  5926. spin_lock_irq(&phba->pport->work_port_lock);
  5927. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5928. spin_unlock_irq(&phba->pport->work_port_lock);
  5929. spin_lock_irq(&phba->hbalock);
  5930. phba->link_state = LPFC_LINK_UNKNOWN;
  5931. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5932. spin_unlock_irq(&phba->hbalock);
  5933. pring = &psli->ring[psli->fcp_ring];
  5934. lpfc_sli_abort_iocb_ring(phba, pring);
  5935. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5936. "0345 Resetting board due to mailbox timeout\n");
  5937. /* Reset the HBA device */
  5938. lpfc_reset_hba(phba);
  5939. }
  5940. /**
  5941. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  5942. * @phba: Pointer to HBA context object.
  5943. * @pmbox: Pointer to mailbox object.
  5944. * @flag: Flag indicating how the mailbox need to be processed.
  5945. *
  5946. * This function is called by discovery code and HBA management code
  5947. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  5948. * function gets the hbalock to protect the data structures.
  5949. * The mailbox command can be submitted in polling mode, in which case
  5950. * this function will wait in a polling loop for the completion of the
  5951. * mailbox.
  5952. * If the mailbox is submitted in no_wait mode (not polling) the
  5953. * function will submit the command and returns immediately without waiting
  5954. * for the mailbox completion. The no_wait is supported only when HBA
  5955. * is in SLI2/SLI3 mode - interrupts are enabled.
  5956. * The SLI interface allows only one mailbox pending at a time. If the
  5957. * mailbox is issued in polling mode and there is already a mailbox
  5958. * pending, then the function will return an error. If the mailbox is issued
  5959. * in NO_WAIT mode and there is a mailbox pending already, the function
  5960. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  5961. * The sli layer owns the mailbox object until the completion of mailbox
  5962. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  5963. * return codes the caller owns the mailbox command after the return of
  5964. * the function.
  5965. **/
  5966. static int
  5967. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  5968. uint32_t flag)
  5969. {
  5970. MAILBOX_t *mb;
  5971. struct lpfc_sli *psli = &phba->sli;
  5972. uint32_t status, evtctr;
  5973. uint32_t ha_copy, hc_copy;
  5974. int i;
  5975. unsigned long timeout;
  5976. unsigned long drvr_flag = 0;
  5977. uint32_t word0, ldata;
  5978. void __iomem *to_slim;
  5979. int processing_queue = 0;
  5980. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5981. if (!pmbox) {
  5982. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5983. /* processing mbox queue from intr_handler */
  5984. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5985. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5986. return MBX_SUCCESS;
  5987. }
  5988. processing_queue = 1;
  5989. pmbox = lpfc_mbox_get(phba);
  5990. if (!pmbox) {
  5991. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5992. return MBX_SUCCESS;
  5993. }
  5994. }
  5995. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  5996. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  5997. if(!pmbox->vport) {
  5998. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5999. lpfc_printf_log(phba, KERN_ERR,
  6000. LOG_MBOX | LOG_VPORT,
  6001. "1806 Mbox x%x failed. No vport\n",
  6002. pmbox->u.mb.mbxCommand);
  6003. dump_stack();
  6004. goto out_not_finished;
  6005. }
  6006. }
  6007. /* If the PCI channel is in offline state, do not post mbox. */
  6008. if (unlikely(pci_channel_offline(phba->pcidev))) {
  6009. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6010. goto out_not_finished;
  6011. }
  6012. /* If HBA has a deferred error attention, fail the iocb. */
  6013. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  6014. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6015. goto out_not_finished;
  6016. }
  6017. psli = &phba->sli;
  6018. mb = &pmbox->u.mb;
  6019. status = MBX_SUCCESS;
  6020. if (phba->link_state == LPFC_HBA_ERROR) {
  6021. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6022. /* Mbox command <mbxCommand> cannot issue */
  6023. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6024. "(%d):0311 Mailbox command x%x cannot "
  6025. "issue Data: x%x x%x\n",
  6026. pmbox->vport ? pmbox->vport->vpi : 0,
  6027. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6028. goto out_not_finished;
  6029. }
  6030. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6031. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6032. !(hc_copy & HC_MBINT_ENA)) {
  6033. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6034. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6035. "(%d):2528 Mailbox command x%x cannot "
  6036. "issue Data: x%x x%x\n",
  6037. pmbox->vport ? pmbox->vport->vpi : 0,
  6038. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6039. goto out_not_finished;
  6040. }
  6041. }
  6042. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6043. /* Polling for a mbox command when another one is already active
  6044. * is not allowed in SLI. Also, the driver must have established
  6045. * SLI2 mode to queue and process multiple mbox commands.
  6046. */
  6047. if (flag & MBX_POLL) {
  6048. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6049. /* Mbox command <mbxCommand> cannot issue */
  6050. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6051. "(%d):2529 Mailbox command x%x "
  6052. "cannot issue Data: x%x x%x\n",
  6053. pmbox->vport ? pmbox->vport->vpi : 0,
  6054. pmbox->u.mb.mbxCommand,
  6055. psli->sli_flag, flag);
  6056. goto out_not_finished;
  6057. }
  6058. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  6059. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6060. /* Mbox command <mbxCommand> cannot issue */
  6061. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6062. "(%d):2530 Mailbox command x%x "
  6063. "cannot issue Data: x%x x%x\n",
  6064. pmbox->vport ? pmbox->vport->vpi : 0,
  6065. pmbox->u.mb.mbxCommand,
  6066. psli->sli_flag, flag);
  6067. goto out_not_finished;
  6068. }
  6069. /* Another mailbox command is still being processed, queue this
  6070. * command to be processed later.
  6071. */
  6072. lpfc_mbox_put(phba, pmbox);
  6073. /* Mbox cmd issue - BUSY */
  6074. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6075. "(%d):0308 Mbox cmd issue - BUSY Data: "
  6076. "x%x x%x x%x x%x\n",
  6077. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  6078. mb->mbxCommand, phba->pport->port_state,
  6079. psli->sli_flag, flag);
  6080. psli->slistat.mbox_busy++;
  6081. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6082. if (pmbox->vport) {
  6083. lpfc_debugfs_disc_trc(pmbox->vport,
  6084. LPFC_DISC_TRC_MBOX_VPORT,
  6085. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  6086. (uint32_t)mb->mbxCommand,
  6087. mb->un.varWords[0], mb->un.varWords[1]);
  6088. }
  6089. else {
  6090. lpfc_debugfs_disc_trc(phba->pport,
  6091. LPFC_DISC_TRC_MBOX,
  6092. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  6093. (uint32_t)mb->mbxCommand,
  6094. mb->un.varWords[0], mb->un.varWords[1]);
  6095. }
  6096. return MBX_BUSY;
  6097. }
  6098. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6099. /* If we are not polling, we MUST be in SLI2 mode */
  6100. if (flag != MBX_POLL) {
  6101. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  6102. (mb->mbxCommand != MBX_KILL_BOARD)) {
  6103. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6104. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6105. /* Mbox command <mbxCommand> cannot issue */
  6106. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6107. "(%d):2531 Mailbox command x%x "
  6108. "cannot issue Data: x%x x%x\n",
  6109. pmbox->vport ? pmbox->vport->vpi : 0,
  6110. pmbox->u.mb.mbxCommand,
  6111. psli->sli_flag, flag);
  6112. goto out_not_finished;
  6113. }
  6114. /* timeout active mbox command */
  6115. mod_timer(&psli->mbox_tmo, (jiffies +
  6116. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  6117. }
  6118. /* Mailbox cmd <cmd> issue */
  6119. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6120. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  6121. "x%x\n",
  6122. pmbox->vport ? pmbox->vport->vpi : 0,
  6123. mb->mbxCommand, phba->pport->port_state,
  6124. psli->sli_flag, flag);
  6125. if (mb->mbxCommand != MBX_HEARTBEAT) {
  6126. if (pmbox->vport) {
  6127. lpfc_debugfs_disc_trc(pmbox->vport,
  6128. LPFC_DISC_TRC_MBOX_VPORT,
  6129. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6130. (uint32_t)mb->mbxCommand,
  6131. mb->un.varWords[0], mb->un.varWords[1]);
  6132. }
  6133. else {
  6134. lpfc_debugfs_disc_trc(phba->pport,
  6135. LPFC_DISC_TRC_MBOX,
  6136. "MBOX Send: cmd:x%x mb:x%x x%x",
  6137. (uint32_t)mb->mbxCommand,
  6138. mb->un.varWords[0], mb->un.varWords[1]);
  6139. }
  6140. }
  6141. psli->slistat.mbox_cmd++;
  6142. evtctr = psli->slistat.mbox_event;
  6143. /* next set own bit for the adapter and copy over command word */
  6144. mb->mbxOwner = OWN_CHIP;
  6145. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6146. /* Populate mbox extension offset word. */
  6147. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6148. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6149. = (uint8_t *)phba->mbox_ext
  6150. - (uint8_t *)phba->mbox;
  6151. }
  6152. /* Copy the mailbox extension data */
  6153. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6154. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6155. (uint8_t *)phba->mbox_ext,
  6156. pmbox->in_ext_byte_len);
  6157. }
  6158. /* Copy command data to host SLIM area */
  6159. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6160. } else {
  6161. /* Populate mbox extension offset word. */
  6162. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6163. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6164. = MAILBOX_HBA_EXT_OFFSET;
  6165. /* Copy the mailbox extension data */
  6166. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6167. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6168. MAILBOX_HBA_EXT_OFFSET,
  6169. pmbox->context2, pmbox->in_ext_byte_len);
  6170. }
  6171. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6172. /* copy command data into host mbox for cmpl */
  6173. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6174. }
  6175. /* First copy mbox command data to HBA SLIM, skip past first
  6176. word */
  6177. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6178. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  6179. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6180. /* Next copy over first word, with mbxOwner set */
  6181. ldata = *((uint32_t *)mb);
  6182. to_slim = phba->MBslimaddr;
  6183. writel(ldata, to_slim);
  6184. readl(to_slim); /* flush */
  6185. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6186. /* switch over to host mailbox */
  6187. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6188. }
  6189. }
  6190. wmb();
  6191. switch (flag) {
  6192. case MBX_NOWAIT:
  6193. /* Set up reference to mailbox command */
  6194. psli->mbox_active = pmbox;
  6195. /* Interrupt board to do it */
  6196. writel(CA_MBATT, phba->CAregaddr);
  6197. readl(phba->CAregaddr); /* flush */
  6198. /* Don't wait for it to finish, just return */
  6199. break;
  6200. case MBX_POLL:
  6201. /* Set up null reference to mailbox command */
  6202. psli->mbox_active = NULL;
  6203. /* Interrupt board to do it */
  6204. writel(CA_MBATT, phba->CAregaddr);
  6205. readl(phba->CAregaddr); /* flush */
  6206. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6207. /* First read mbox status word */
  6208. word0 = *((uint32_t *)phba->mbox);
  6209. word0 = le32_to_cpu(word0);
  6210. } else {
  6211. /* First read mbox status word */
  6212. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6213. spin_unlock_irqrestore(&phba->hbalock,
  6214. drvr_flag);
  6215. goto out_not_finished;
  6216. }
  6217. }
  6218. /* Read the HBA Host Attention Register */
  6219. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6220. spin_unlock_irqrestore(&phba->hbalock,
  6221. drvr_flag);
  6222. goto out_not_finished;
  6223. }
  6224. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6225. 1000) + jiffies;
  6226. i = 0;
  6227. /* Wait for command to complete */
  6228. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6229. (!(ha_copy & HA_MBATT) &&
  6230. (phba->link_state > LPFC_WARM_START))) {
  6231. if (time_after(jiffies, timeout)) {
  6232. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6233. spin_unlock_irqrestore(&phba->hbalock,
  6234. drvr_flag);
  6235. goto out_not_finished;
  6236. }
  6237. /* Check if we took a mbox interrupt while we were
  6238. polling */
  6239. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6240. && (evtctr != psli->slistat.mbox_event))
  6241. break;
  6242. if (i++ > 10) {
  6243. spin_unlock_irqrestore(&phba->hbalock,
  6244. drvr_flag);
  6245. msleep(1);
  6246. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6247. }
  6248. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6249. /* First copy command data */
  6250. word0 = *((uint32_t *)phba->mbox);
  6251. word0 = le32_to_cpu(word0);
  6252. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6253. MAILBOX_t *slimmb;
  6254. uint32_t slimword0;
  6255. /* Check real SLIM for any errors */
  6256. slimword0 = readl(phba->MBslimaddr);
  6257. slimmb = (MAILBOX_t *) & slimword0;
  6258. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6259. && slimmb->mbxStatus) {
  6260. psli->sli_flag &=
  6261. ~LPFC_SLI_ACTIVE;
  6262. word0 = slimword0;
  6263. }
  6264. }
  6265. } else {
  6266. /* First copy command data */
  6267. word0 = readl(phba->MBslimaddr);
  6268. }
  6269. /* Read the HBA Host Attention Register */
  6270. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6271. spin_unlock_irqrestore(&phba->hbalock,
  6272. drvr_flag);
  6273. goto out_not_finished;
  6274. }
  6275. }
  6276. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6277. /* copy results back to user */
  6278. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  6279. /* Copy the mailbox extension data */
  6280. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6281. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6282. pmbox->context2,
  6283. pmbox->out_ext_byte_len);
  6284. }
  6285. } else {
  6286. /* First copy command data */
  6287. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  6288. MAILBOX_CMD_SIZE);
  6289. /* Copy the mailbox extension data */
  6290. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6291. lpfc_memcpy_from_slim(pmbox->context2,
  6292. phba->MBslimaddr +
  6293. MAILBOX_HBA_EXT_OFFSET,
  6294. pmbox->out_ext_byte_len);
  6295. }
  6296. }
  6297. writel(HA_MBATT, phba->HAregaddr);
  6298. readl(phba->HAregaddr); /* flush */
  6299. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6300. status = mb->mbxStatus;
  6301. }
  6302. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6303. return status;
  6304. out_not_finished:
  6305. if (processing_queue) {
  6306. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6307. lpfc_mbox_cmpl_put(phba, pmbox);
  6308. }
  6309. return MBX_NOT_FINISHED;
  6310. }
  6311. /**
  6312. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6313. * @phba: Pointer to HBA context object.
  6314. *
  6315. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6316. * the driver internal pending mailbox queue. It will then try to wait out the
  6317. * possible outstanding mailbox command before return.
  6318. *
  6319. * Returns:
  6320. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6321. * the outstanding mailbox command timed out.
  6322. **/
  6323. static int
  6324. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6325. {
  6326. struct lpfc_sli *psli = &phba->sli;
  6327. int rc = 0;
  6328. unsigned long timeout = 0;
  6329. /* Mark the asynchronous mailbox command posting as blocked */
  6330. spin_lock_irq(&phba->hbalock);
  6331. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6332. /* Determine how long we might wait for the active mailbox
  6333. * command to be gracefully completed by firmware.
  6334. */
  6335. if (phba->sli.mbox_active)
  6336. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6337. phba->sli.mbox_active) *
  6338. 1000) + jiffies;
  6339. spin_unlock_irq(&phba->hbalock);
  6340. /* Wait for the outstnading mailbox command to complete */
  6341. while (phba->sli.mbox_active) {
  6342. /* Check active mailbox complete status every 2ms */
  6343. msleep(2);
  6344. if (time_after(jiffies, timeout)) {
  6345. /* Timeout, marked the outstanding cmd not complete */
  6346. rc = 1;
  6347. break;
  6348. }
  6349. }
  6350. /* Can not cleanly block async mailbox command, fails it */
  6351. if (rc) {
  6352. spin_lock_irq(&phba->hbalock);
  6353. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6354. spin_unlock_irq(&phba->hbalock);
  6355. }
  6356. return rc;
  6357. }
  6358. /**
  6359. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6360. * @phba: Pointer to HBA context object.
  6361. *
  6362. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6363. * commands from the driver internal pending mailbox queue. It makes sure
  6364. * that there is no outstanding mailbox command before resuming posting
  6365. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6366. * mailbox command, it will try to wait it out before resuming asynchronous
  6367. * mailbox command posting.
  6368. **/
  6369. static void
  6370. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6371. {
  6372. struct lpfc_sli *psli = &phba->sli;
  6373. spin_lock_irq(&phba->hbalock);
  6374. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6375. /* Asynchronous mailbox posting is not blocked, do nothing */
  6376. spin_unlock_irq(&phba->hbalock);
  6377. return;
  6378. }
  6379. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6380. * successful or timeout, after timing-out the outstanding mailbox
  6381. * command shall always be removed, so just unblock posting async
  6382. * mailbox command and resume
  6383. */
  6384. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6385. spin_unlock_irq(&phba->hbalock);
  6386. /* wake up worker thread to post asynchronlous mailbox command */
  6387. lpfc_worker_wake_up(phba);
  6388. }
  6389. /**
  6390. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6391. * @phba: Pointer to HBA context object.
  6392. * @mboxq: Pointer to mailbox object.
  6393. *
  6394. * The function posts a mailbox to the port. The mailbox is expected
  6395. * to be comletely filled in and ready for the port to operate on it.
  6396. * This routine executes a synchronous completion operation on the
  6397. * mailbox by polling for its completion.
  6398. *
  6399. * The caller must not be holding any locks when calling this routine.
  6400. *
  6401. * Returns:
  6402. * MBX_SUCCESS - mailbox posted successfully
  6403. * Any of the MBX error values.
  6404. **/
  6405. static int
  6406. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6407. {
  6408. int rc = MBX_SUCCESS;
  6409. unsigned long iflag;
  6410. uint32_t db_ready;
  6411. uint32_t mcqe_status;
  6412. uint32_t mbx_cmnd;
  6413. unsigned long timeout;
  6414. struct lpfc_sli *psli = &phba->sli;
  6415. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6416. struct lpfc_bmbx_create *mbox_rgn;
  6417. struct dma_address *dma_address;
  6418. struct lpfc_register bmbx_reg;
  6419. /*
  6420. * Only one mailbox can be active to the bootstrap mailbox region
  6421. * at a time and there is no queueing provided.
  6422. */
  6423. spin_lock_irqsave(&phba->hbalock, iflag);
  6424. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6425. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6426. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6427. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6428. "cannot issue Data: x%x x%x\n",
  6429. mboxq->vport ? mboxq->vport->vpi : 0,
  6430. mboxq->u.mb.mbxCommand,
  6431. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6432. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6433. psli->sli_flag, MBX_POLL);
  6434. return MBXERR_ERROR;
  6435. }
  6436. /* The server grabs the token and owns it until release */
  6437. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6438. phba->sli.mbox_active = mboxq;
  6439. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6440. /*
  6441. * Initialize the bootstrap memory region to avoid stale data areas
  6442. * in the mailbox post. Then copy the caller's mailbox contents to
  6443. * the bmbx mailbox region.
  6444. */
  6445. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6446. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6447. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6448. sizeof(struct lpfc_mqe));
  6449. /* Post the high mailbox dma address to the port and wait for ready. */
  6450. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6451. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6452. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6453. * 1000) + jiffies;
  6454. do {
  6455. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6456. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6457. if (!db_ready)
  6458. msleep(2);
  6459. if (time_after(jiffies, timeout)) {
  6460. rc = MBXERR_ERROR;
  6461. goto exit;
  6462. }
  6463. } while (!db_ready);
  6464. /* Post the low mailbox dma address to the port. */
  6465. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6466. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6467. * 1000) + jiffies;
  6468. do {
  6469. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6470. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6471. if (!db_ready)
  6472. msleep(2);
  6473. if (time_after(jiffies, timeout)) {
  6474. rc = MBXERR_ERROR;
  6475. goto exit;
  6476. }
  6477. } while (!db_ready);
  6478. /*
  6479. * Read the CQ to ensure the mailbox has completed.
  6480. * If so, update the mailbox status so that the upper layers
  6481. * can complete the request normally.
  6482. */
  6483. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6484. sizeof(struct lpfc_mqe));
  6485. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6486. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6487. sizeof(struct lpfc_mcqe));
  6488. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6489. /*
  6490. * When the CQE status indicates a failure and the mailbox status
  6491. * indicates success then copy the CQE status into the mailbox status
  6492. * (and prefix it with x4000).
  6493. */
  6494. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6495. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6496. bf_set(lpfc_mqe_status, mb,
  6497. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6498. rc = MBXERR_ERROR;
  6499. } else
  6500. lpfc_sli4_swap_str(phba, mboxq);
  6501. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6502. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6503. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6504. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6505. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6506. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6507. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6508. bf_get(lpfc_mqe_status, mb),
  6509. mb->un.mb_words[0], mb->un.mb_words[1],
  6510. mb->un.mb_words[2], mb->un.mb_words[3],
  6511. mb->un.mb_words[4], mb->un.mb_words[5],
  6512. mb->un.mb_words[6], mb->un.mb_words[7],
  6513. mb->un.mb_words[8], mb->un.mb_words[9],
  6514. mb->un.mb_words[10], mb->un.mb_words[11],
  6515. mb->un.mb_words[12], mboxq->mcqe.word0,
  6516. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6517. mboxq->mcqe.trailer);
  6518. exit:
  6519. /* We are holding the token, no needed for lock when release */
  6520. spin_lock_irqsave(&phba->hbalock, iflag);
  6521. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6522. phba->sli.mbox_active = NULL;
  6523. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6524. return rc;
  6525. }
  6526. /**
  6527. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6528. * @phba: Pointer to HBA context object.
  6529. * @pmbox: Pointer to mailbox object.
  6530. * @flag: Flag indicating how the mailbox need to be processed.
  6531. *
  6532. * This function is called by discovery code and HBA management code to submit
  6533. * a mailbox command to firmware with SLI-4 interface spec.
  6534. *
  6535. * Return codes the caller owns the mailbox command after the return of the
  6536. * function.
  6537. **/
  6538. static int
  6539. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6540. uint32_t flag)
  6541. {
  6542. struct lpfc_sli *psli = &phba->sli;
  6543. unsigned long iflags;
  6544. int rc;
  6545. /* dump from issue mailbox command if setup */
  6546. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6547. rc = lpfc_mbox_dev_check(phba);
  6548. if (unlikely(rc)) {
  6549. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6550. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6551. "cannot issue Data: x%x x%x\n",
  6552. mboxq->vport ? mboxq->vport->vpi : 0,
  6553. mboxq->u.mb.mbxCommand,
  6554. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6555. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6556. psli->sli_flag, flag);
  6557. goto out_not_finished;
  6558. }
  6559. /* Detect polling mode and jump to a handler */
  6560. if (!phba->sli4_hba.intr_enable) {
  6561. if (flag == MBX_POLL)
  6562. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6563. else
  6564. rc = -EIO;
  6565. if (rc != MBX_SUCCESS)
  6566. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6567. "(%d):2541 Mailbox command x%x "
  6568. "(x%x/x%x) failure: "
  6569. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6570. "Data: x%x x%x\n,",
  6571. mboxq->vport ? mboxq->vport->vpi : 0,
  6572. mboxq->u.mb.mbxCommand,
  6573. lpfc_sli_config_mbox_subsys_get(phba,
  6574. mboxq),
  6575. lpfc_sli_config_mbox_opcode_get(phba,
  6576. mboxq),
  6577. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6578. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6579. bf_get(lpfc_mcqe_ext_status,
  6580. &mboxq->mcqe),
  6581. psli->sli_flag, flag);
  6582. return rc;
  6583. } else if (flag == MBX_POLL) {
  6584. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6585. "(%d):2542 Try to issue mailbox command "
  6586. "x%x (x%x/x%x) synchronously ahead of async"
  6587. "mailbox command queue: x%x x%x\n",
  6588. mboxq->vport ? mboxq->vport->vpi : 0,
  6589. mboxq->u.mb.mbxCommand,
  6590. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6591. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6592. psli->sli_flag, flag);
  6593. /* Try to block the asynchronous mailbox posting */
  6594. rc = lpfc_sli4_async_mbox_block(phba);
  6595. if (!rc) {
  6596. /* Successfully blocked, now issue sync mbox cmd */
  6597. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6598. if (rc != MBX_SUCCESS)
  6599. lpfc_printf_log(phba, KERN_WARNING,
  6600. LOG_MBOX | LOG_SLI,
  6601. "(%d):2597 Sync Mailbox command "
  6602. "x%x (x%x/x%x) failure: "
  6603. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6604. "Data: x%x x%x\n,",
  6605. mboxq->vport ? mboxq->vport->vpi : 0,
  6606. mboxq->u.mb.mbxCommand,
  6607. lpfc_sli_config_mbox_subsys_get(phba,
  6608. mboxq),
  6609. lpfc_sli_config_mbox_opcode_get(phba,
  6610. mboxq),
  6611. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6612. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6613. bf_get(lpfc_mcqe_ext_status,
  6614. &mboxq->mcqe),
  6615. psli->sli_flag, flag);
  6616. /* Unblock the async mailbox posting afterward */
  6617. lpfc_sli4_async_mbox_unblock(phba);
  6618. }
  6619. return rc;
  6620. }
  6621. /* Now, interrupt mode asynchrous mailbox command */
  6622. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6623. if (rc) {
  6624. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6625. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6626. "cannot issue Data: x%x x%x\n",
  6627. mboxq->vport ? mboxq->vport->vpi : 0,
  6628. mboxq->u.mb.mbxCommand,
  6629. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6630. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6631. psli->sli_flag, flag);
  6632. goto out_not_finished;
  6633. }
  6634. /* Put the mailbox command to the driver internal FIFO */
  6635. psli->slistat.mbox_busy++;
  6636. spin_lock_irqsave(&phba->hbalock, iflags);
  6637. lpfc_mbox_put(phba, mboxq);
  6638. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6639. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6640. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6641. "x%x (x%x/x%x) x%x x%x x%x\n",
  6642. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6643. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6644. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6645. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6646. phba->pport->port_state,
  6647. psli->sli_flag, MBX_NOWAIT);
  6648. /* Wake up worker thread to transport mailbox command from head */
  6649. lpfc_worker_wake_up(phba);
  6650. return MBX_BUSY;
  6651. out_not_finished:
  6652. return MBX_NOT_FINISHED;
  6653. }
  6654. /**
  6655. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6656. * @phba: Pointer to HBA context object.
  6657. *
  6658. * This function is called by worker thread to send a mailbox command to
  6659. * SLI4 HBA firmware.
  6660. *
  6661. **/
  6662. int
  6663. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6664. {
  6665. struct lpfc_sli *psli = &phba->sli;
  6666. LPFC_MBOXQ_t *mboxq;
  6667. int rc = MBX_SUCCESS;
  6668. unsigned long iflags;
  6669. struct lpfc_mqe *mqe;
  6670. uint32_t mbx_cmnd;
  6671. /* Check interrupt mode before post async mailbox command */
  6672. if (unlikely(!phba->sli4_hba.intr_enable))
  6673. return MBX_NOT_FINISHED;
  6674. /* Check for mailbox command service token */
  6675. spin_lock_irqsave(&phba->hbalock, iflags);
  6676. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6677. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6678. return MBX_NOT_FINISHED;
  6679. }
  6680. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6681. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6682. return MBX_NOT_FINISHED;
  6683. }
  6684. if (unlikely(phba->sli.mbox_active)) {
  6685. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6686. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6687. "0384 There is pending active mailbox cmd\n");
  6688. return MBX_NOT_FINISHED;
  6689. }
  6690. /* Take the mailbox command service token */
  6691. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6692. /* Get the next mailbox command from head of queue */
  6693. mboxq = lpfc_mbox_get(phba);
  6694. /* If no more mailbox command waiting for post, we're done */
  6695. if (!mboxq) {
  6696. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6697. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6698. return MBX_SUCCESS;
  6699. }
  6700. phba->sli.mbox_active = mboxq;
  6701. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6702. /* Check device readiness for posting mailbox command */
  6703. rc = lpfc_mbox_dev_check(phba);
  6704. if (unlikely(rc))
  6705. /* Driver clean routine will clean up pending mailbox */
  6706. goto out_not_finished;
  6707. /* Prepare the mbox command to be posted */
  6708. mqe = &mboxq->u.mqe;
  6709. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6710. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6711. mod_timer(&psli->mbox_tmo, (jiffies +
  6712. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6713. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6714. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6715. "x%x x%x\n",
  6716. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6717. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6718. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6719. phba->pport->port_state, psli->sli_flag);
  6720. if (mbx_cmnd != MBX_HEARTBEAT) {
  6721. if (mboxq->vport) {
  6722. lpfc_debugfs_disc_trc(mboxq->vport,
  6723. LPFC_DISC_TRC_MBOX_VPORT,
  6724. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6725. mbx_cmnd, mqe->un.mb_words[0],
  6726. mqe->un.mb_words[1]);
  6727. } else {
  6728. lpfc_debugfs_disc_trc(phba->pport,
  6729. LPFC_DISC_TRC_MBOX,
  6730. "MBOX Send: cmd:x%x mb:x%x x%x",
  6731. mbx_cmnd, mqe->un.mb_words[0],
  6732. mqe->un.mb_words[1]);
  6733. }
  6734. }
  6735. psli->slistat.mbox_cmd++;
  6736. /* Post the mailbox command to the port */
  6737. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6738. if (rc != MBX_SUCCESS) {
  6739. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6740. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6741. "cannot issue Data: x%x x%x\n",
  6742. mboxq->vport ? mboxq->vport->vpi : 0,
  6743. mboxq->u.mb.mbxCommand,
  6744. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6745. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6746. psli->sli_flag, MBX_NOWAIT);
  6747. goto out_not_finished;
  6748. }
  6749. return rc;
  6750. out_not_finished:
  6751. spin_lock_irqsave(&phba->hbalock, iflags);
  6752. if (phba->sli.mbox_active) {
  6753. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6754. __lpfc_mbox_cmpl_put(phba, mboxq);
  6755. /* Release the token */
  6756. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6757. phba->sli.mbox_active = NULL;
  6758. }
  6759. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6760. return MBX_NOT_FINISHED;
  6761. }
  6762. /**
  6763. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6764. * @phba: Pointer to HBA context object.
  6765. * @pmbox: Pointer to mailbox object.
  6766. * @flag: Flag indicating how the mailbox need to be processed.
  6767. *
  6768. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6769. * the API jump table function pointer from the lpfc_hba struct.
  6770. *
  6771. * Return codes the caller owns the mailbox command after the return of the
  6772. * function.
  6773. **/
  6774. int
  6775. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6776. {
  6777. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6778. }
  6779. /**
  6780. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6781. * @phba: The hba struct for which this call is being executed.
  6782. * @dev_grp: The HBA PCI-Device group number.
  6783. *
  6784. * This routine sets up the mbox interface API function jump table in @phba
  6785. * struct.
  6786. * Returns: 0 - success, -ENODEV - failure.
  6787. **/
  6788. int
  6789. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6790. {
  6791. switch (dev_grp) {
  6792. case LPFC_PCI_DEV_LP:
  6793. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6794. phba->lpfc_sli_handle_slow_ring_event =
  6795. lpfc_sli_handle_slow_ring_event_s3;
  6796. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6797. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6798. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6799. break;
  6800. case LPFC_PCI_DEV_OC:
  6801. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6802. phba->lpfc_sli_handle_slow_ring_event =
  6803. lpfc_sli_handle_slow_ring_event_s4;
  6804. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6805. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6806. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6807. break;
  6808. default:
  6809. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6810. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6811. dev_grp);
  6812. return -ENODEV;
  6813. break;
  6814. }
  6815. return 0;
  6816. }
  6817. /**
  6818. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6819. * @phba: Pointer to HBA context object.
  6820. * @pring: Pointer to driver SLI ring object.
  6821. * @piocb: Pointer to address of newly added command iocb.
  6822. *
  6823. * This function is called with hbalock held to add a command
  6824. * iocb to the txq when SLI layer cannot submit the command iocb
  6825. * to the ring.
  6826. **/
  6827. void
  6828. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6829. struct lpfc_iocbq *piocb)
  6830. {
  6831. /* Insert the caller's iocb in the txq tail for later processing. */
  6832. list_add_tail(&piocb->list, &pring->txq);
  6833. pring->txq_cnt++;
  6834. }
  6835. /**
  6836. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6837. * @phba: Pointer to HBA context object.
  6838. * @pring: Pointer to driver SLI ring object.
  6839. * @piocb: Pointer to address of newly added command iocb.
  6840. *
  6841. * This function is called with hbalock held before a new
  6842. * iocb is submitted to the firmware. This function checks
  6843. * txq to flush the iocbs in txq to Firmware before
  6844. * submitting new iocbs to the Firmware.
  6845. * If there are iocbs in the txq which need to be submitted
  6846. * to firmware, lpfc_sli_next_iocb returns the first element
  6847. * of the txq after dequeuing it from txq.
  6848. * If there is no iocb in the txq then the function will return
  6849. * *piocb and *piocb is set to NULL. Caller needs to check
  6850. * *piocb to find if there are more commands in the txq.
  6851. **/
  6852. static struct lpfc_iocbq *
  6853. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6854. struct lpfc_iocbq **piocb)
  6855. {
  6856. struct lpfc_iocbq * nextiocb;
  6857. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6858. if (!nextiocb) {
  6859. nextiocb = *piocb;
  6860. *piocb = NULL;
  6861. }
  6862. return nextiocb;
  6863. }
  6864. /**
  6865. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6866. * @phba: Pointer to HBA context object.
  6867. * @ring_number: SLI ring number to issue iocb on.
  6868. * @piocb: Pointer to command iocb.
  6869. * @flag: Flag indicating if this command can be put into txq.
  6870. *
  6871. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6872. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6873. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6874. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6875. * this function allows only iocbs for posting buffers. This function finds
  6876. * next available slot in the command ring and posts the command to the
  6877. * available slot and writes the port attention register to request HBA start
  6878. * processing new iocb. If there is no slot available in the ring and
  6879. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6880. * the function returns IOCB_BUSY.
  6881. *
  6882. * This function is called with hbalock held. The function will return success
  6883. * after it successfully submit the iocb to firmware or after adding to the
  6884. * txq.
  6885. **/
  6886. static int
  6887. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6888. struct lpfc_iocbq *piocb, uint32_t flag)
  6889. {
  6890. struct lpfc_iocbq *nextiocb;
  6891. IOCB_t *iocb;
  6892. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6893. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6894. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6895. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6896. lpfc_printf_log(phba, KERN_ERR,
  6897. LOG_SLI | LOG_VPORT,
  6898. "1807 IOCB x%x failed. No vport\n",
  6899. piocb->iocb.ulpCommand);
  6900. dump_stack();
  6901. return IOCB_ERROR;
  6902. }
  6903. /* If the PCI channel is in offline state, do not post iocbs. */
  6904. if (unlikely(pci_channel_offline(phba->pcidev)))
  6905. return IOCB_ERROR;
  6906. /* If HBA has a deferred error attention, fail the iocb. */
  6907. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6908. return IOCB_ERROR;
  6909. /*
  6910. * We should never get an IOCB if we are in a < LINK_DOWN state
  6911. */
  6912. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6913. return IOCB_ERROR;
  6914. /*
  6915. * Check to see if we are blocking IOCB processing because of a
  6916. * outstanding event.
  6917. */
  6918. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  6919. goto iocb_busy;
  6920. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  6921. /*
  6922. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  6923. * can be issued if the link is not up.
  6924. */
  6925. switch (piocb->iocb.ulpCommand) {
  6926. case CMD_GEN_REQUEST64_CR:
  6927. case CMD_GEN_REQUEST64_CX:
  6928. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  6929. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  6930. FC_RCTL_DD_UNSOL_CMD) ||
  6931. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  6932. MENLO_TRANSPORT_TYPE))
  6933. goto iocb_busy;
  6934. break;
  6935. case CMD_QUE_RING_BUF_CN:
  6936. case CMD_QUE_RING_BUF64_CN:
  6937. /*
  6938. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  6939. * completion, iocb_cmpl MUST be 0.
  6940. */
  6941. if (piocb->iocb_cmpl)
  6942. piocb->iocb_cmpl = NULL;
  6943. /*FALLTHROUGH*/
  6944. case CMD_CREATE_XRI_CR:
  6945. case CMD_CLOSE_XRI_CN:
  6946. case CMD_CLOSE_XRI_CX:
  6947. break;
  6948. default:
  6949. goto iocb_busy;
  6950. }
  6951. /*
  6952. * For FCP commands, we must be in a state where we can process link
  6953. * attention events.
  6954. */
  6955. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  6956. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  6957. goto iocb_busy;
  6958. }
  6959. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  6960. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  6961. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  6962. if (iocb)
  6963. lpfc_sli_update_ring(phba, pring);
  6964. else
  6965. lpfc_sli_update_full_ring(phba, pring);
  6966. if (!piocb)
  6967. return IOCB_SUCCESS;
  6968. goto out_busy;
  6969. iocb_busy:
  6970. pring->stats.iocb_cmd_delay++;
  6971. out_busy:
  6972. if (!(flag & SLI_IOCB_RET_IOCB)) {
  6973. __lpfc_sli_ringtx_put(phba, pring, piocb);
  6974. return IOCB_SUCCESS;
  6975. }
  6976. return IOCB_BUSY;
  6977. }
  6978. /**
  6979. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  6980. * @phba: Pointer to HBA context object.
  6981. * @piocb: Pointer to command iocb.
  6982. * @sglq: Pointer to the scatter gather queue object.
  6983. *
  6984. * This routine converts the bpl or bde that is in the IOCB
  6985. * to a sgl list for the sli4 hardware. The physical address
  6986. * of the bpl/bde is converted back to a virtual address.
  6987. * If the IOCB contains a BPL then the list of BDE's is
  6988. * converted to sli4_sge's. If the IOCB contains a single
  6989. * BDE then it is converted to a single sli_sge.
  6990. * The IOCB is still in cpu endianess so the contents of
  6991. * the bpl can be used without byte swapping.
  6992. *
  6993. * Returns valid XRI = Success, NO_XRI = Failure.
  6994. **/
  6995. static uint16_t
  6996. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  6997. struct lpfc_sglq *sglq)
  6998. {
  6999. uint16_t xritag = NO_XRI;
  7000. struct ulp_bde64 *bpl = NULL;
  7001. struct ulp_bde64 bde;
  7002. struct sli4_sge *sgl = NULL;
  7003. struct lpfc_dmabuf *dmabuf;
  7004. IOCB_t *icmd;
  7005. int numBdes = 0;
  7006. int i = 0;
  7007. uint32_t offset = 0; /* accumulated offset in the sg request list */
  7008. int inbound = 0; /* number of sg reply entries inbound from firmware */
  7009. if (!piocbq || !sglq)
  7010. return xritag;
  7011. sgl = (struct sli4_sge *)sglq->sgl;
  7012. icmd = &piocbq->iocb;
  7013. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  7014. return sglq->sli4_xritag;
  7015. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7016. numBdes = icmd->un.genreq64.bdl.bdeSize /
  7017. sizeof(struct ulp_bde64);
  7018. /* The addrHigh and addrLow fields within the IOCB
  7019. * have not been byteswapped yet so there is no
  7020. * need to swap them back.
  7021. */
  7022. if (piocbq->context3)
  7023. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  7024. else
  7025. return xritag;
  7026. bpl = (struct ulp_bde64 *)dmabuf->virt;
  7027. if (!bpl)
  7028. return xritag;
  7029. for (i = 0; i < numBdes; i++) {
  7030. /* Should already be byte swapped. */
  7031. sgl->addr_hi = bpl->addrHigh;
  7032. sgl->addr_lo = bpl->addrLow;
  7033. sgl->word2 = le32_to_cpu(sgl->word2);
  7034. if ((i+1) == numBdes)
  7035. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7036. else
  7037. bf_set(lpfc_sli4_sge_last, sgl, 0);
  7038. /* swap the size field back to the cpu so we
  7039. * can assign it to the sgl.
  7040. */
  7041. bde.tus.w = le32_to_cpu(bpl->tus.w);
  7042. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  7043. /* The offsets in the sgl need to be accumulated
  7044. * separately for the request and reply lists.
  7045. * The request is always first, the reply follows.
  7046. */
  7047. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  7048. /* add up the reply sg entries */
  7049. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  7050. inbound++;
  7051. /* first inbound? reset the offset */
  7052. if (inbound == 1)
  7053. offset = 0;
  7054. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  7055. bf_set(lpfc_sli4_sge_type, sgl,
  7056. LPFC_SGE_TYPE_DATA);
  7057. offset += bde.tus.f.bdeSize;
  7058. }
  7059. sgl->word2 = cpu_to_le32(sgl->word2);
  7060. bpl++;
  7061. sgl++;
  7062. }
  7063. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  7064. /* The addrHigh and addrLow fields of the BDE have not
  7065. * been byteswapped yet so they need to be swapped
  7066. * before putting them in the sgl.
  7067. */
  7068. sgl->addr_hi =
  7069. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  7070. sgl->addr_lo =
  7071. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  7072. sgl->word2 = le32_to_cpu(sgl->word2);
  7073. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7074. sgl->word2 = cpu_to_le32(sgl->word2);
  7075. sgl->sge_len =
  7076. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  7077. }
  7078. return sglq->sli4_xritag;
  7079. }
  7080. /**
  7081. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  7082. * @phba: Pointer to HBA context object.
  7083. *
  7084. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  7085. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  7086. * held.
  7087. *
  7088. * Return: index into SLI4 fast-path FCP queue index.
  7089. **/
  7090. static inline uint32_t
  7091. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  7092. {
  7093. int i;
  7094. i = atomic_add_return(1, &phba->fcp_qidx);
  7095. i = (i % phba->cfg_fcp_io_channel);
  7096. return i;
  7097. }
  7098. /**
  7099. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  7100. * @phba: Pointer to HBA context object.
  7101. * @piocb: Pointer to command iocb.
  7102. * @wqe: Pointer to the work queue entry.
  7103. *
  7104. * This routine converts the iocb command to its Work Queue Entry
  7105. * equivalent. The wqe pointer should not have any fields set when
  7106. * this routine is called because it will memcpy over them.
  7107. * This routine does not set the CQ_ID or the WQEC bits in the
  7108. * wqe.
  7109. *
  7110. * Returns: 0 = Success, IOCB_ERROR = Failure.
  7111. **/
  7112. static int
  7113. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  7114. union lpfc_wqe *wqe)
  7115. {
  7116. uint32_t xmit_len = 0, total_len = 0;
  7117. uint8_t ct = 0;
  7118. uint32_t fip;
  7119. uint32_t abort_tag;
  7120. uint8_t command_type = ELS_COMMAND_NON_FIP;
  7121. uint8_t cmnd;
  7122. uint16_t xritag;
  7123. uint16_t abrt_iotag;
  7124. struct lpfc_iocbq *abrtiocbq;
  7125. struct ulp_bde64 *bpl = NULL;
  7126. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  7127. int numBdes, i;
  7128. struct ulp_bde64 bde;
  7129. struct lpfc_nodelist *ndlp;
  7130. uint32_t *pcmd;
  7131. uint32_t if_type;
  7132. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  7133. /* The fcp commands will set command type */
  7134. if (iocbq->iocb_flag & LPFC_IO_FCP)
  7135. command_type = FCP_COMMAND;
  7136. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  7137. command_type = ELS_COMMAND_FIP;
  7138. else
  7139. command_type = ELS_COMMAND_NON_FIP;
  7140. /* Some of the fields are in the right position already */
  7141. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  7142. abort_tag = (uint32_t) iocbq->iotag;
  7143. xritag = iocbq->sli4_xritag;
  7144. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  7145. /* words0-2 bpl convert bde */
  7146. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7147. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7148. sizeof(struct ulp_bde64);
  7149. bpl = (struct ulp_bde64 *)
  7150. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  7151. if (!bpl)
  7152. return IOCB_ERROR;
  7153. /* Should already be byte swapped. */
  7154. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7155. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7156. /* swap the size field back to the cpu so we
  7157. * can assign it to the sgl.
  7158. */
  7159. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7160. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7161. total_len = 0;
  7162. for (i = 0; i < numBdes; i++) {
  7163. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7164. total_len += bde.tus.f.bdeSize;
  7165. }
  7166. } else
  7167. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7168. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7169. cmnd = iocbq->iocb.ulpCommand;
  7170. switch (iocbq->iocb.ulpCommand) {
  7171. case CMD_ELS_REQUEST64_CR:
  7172. if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
  7173. ndlp = iocbq->context_un.ndlp;
  7174. else
  7175. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7176. if (!iocbq->iocb.ulpLe) {
  7177. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7178. "2007 Only Limited Edition cmd Format"
  7179. " supported 0x%x\n",
  7180. iocbq->iocb.ulpCommand);
  7181. return IOCB_ERROR;
  7182. }
  7183. wqe->els_req.payload_len = xmit_len;
  7184. /* Els_reguest64 has a TMO */
  7185. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7186. iocbq->iocb.ulpTimeout);
  7187. /* Need a VF for word 4 set the vf bit*/
  7188. bf_set(els_req64_vf, &wqe->els_req, 0);
  7189. /* And a VFID for word 12 */
  7190. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7191. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7192. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7193. iocbq->iocb.ulpContext);
  7194. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7195. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7196. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7197. if (command_type == ELS_COMMAND_FIP)
  7198. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7199. >> LPFC_FIP_ELS_ID_SHIFT);
  7200. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7201. iocbq->context2)->virt);
  7202. if_type = bf_get(lpfc_sli_intf_if_type,
  7203. &phba->sli4_hba.sli_intf);
  7204. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7205. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7206. *pcmd == ELS_CMD_SCR ||
  7207. *pcmd == ELS_CMD_FDISC ||
  7208. *pcmd == ELS_CMD_LOGO ||
  7209. *pcmd == ELS_CMD_PLOGI)) {
  7210. bf_set(els_req64_sp, &wqe->els_req, 1);
  7211. bf_set(els_req64_sid, &wqe->els_req,
  7212. iocbq->vport->fc_myDID);
  7213. if ((*pcmd == ELS_CMD_FLOGI) &&
  7214. !(phba->fc_topology ==
  7215. LPFC_TOPOLOGY_LOOP))
  7216. bf_set(els_req64_sid, &wqe->els_req, 0);
  7217. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7218. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7219. phba->vpi_ids[iocbq->vport->vpi]);
  7220. } else if (pcmd && iocbq->context1) {
  7221. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7222. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7223. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7224. }
  7225. }
  7226. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7227. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7228. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7229. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7230. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7231. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7232. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7233. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7234. break;
  7235. case CMD_XMIT_SEQUENCE64_CX:
  7236. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7237. iocbq->iocb.un.ulpWord[3]);
  7238. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7239. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7240. /* The entire sequence is transmitted for this IOCB */
  7241. xmit_len = total_len;
  7242. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7243. if (phba->link_flag & LS_LOOPBACK_MODE)
  7244. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7245. case CMD_XMIT_SEQUENCE64_CR:
  7246. /* word3 iocb=io_tag32 wqe=reserved */
  7247. wqe->xmit_sequence.rsvd3 = 0;
  7248. /* word4 relative_offset memcpy */
  7249. /* word5 r_ctl/df_ctl memcpy */
  7250. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7251. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7252. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7253. LPFC_WQE_IOD_WRITE);
  7254. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7255. LPFC_WQE_LENLOC_WORD12);
  7256. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7257. wqe->xmit_sequence.xmit_len = xmit_len;
  7258. command_type = OTHER_COMMAND;
  7259. break;
  7260. case CMD_XMIT_BCAST64_CN:
  7261. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7262. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7263. /* word4 iocb=rsvd wqe=rsvd */
  7264. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7265. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7266. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7267. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7268. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7269. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7270. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7271. LPFC_WQE_LENLOC_WORD3);
  7272. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7273. break;
  7274. case CMD_FCP_IWRITE64_CR:
  7275. command_type = FCP_COMMAND_DATA_OUT;
  7276. /* word3 iocb=iotag wqe=payload_offset_len */
  7277. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7278. wqe->fcp_iwrite.payload_offset_len =
  7279. xmit_len + sizeof(struct fcp_rsp);
  7280. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7281. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7282. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7283. iocbq->iocb.ulpFCP2Rcvy);
  7284. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7285. /* Always open the exchange */
  7286. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7287. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7288. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7289. LPFC_WQE_LENLOC_WORD4);
  7290. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7291. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7292. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7293. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7294. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7295. }
  7296. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7297. break;
  7298. case CMD_FCP_IREAD64_CR:
  7299. /* word3 iocb=iotag wqe=payload_offset_len */
  7300. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7301. wqe->fcp_iread.payload_offset_len =
  7302. xmit_len + sizeof(struct fcp_rsp);
  7303. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7304. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7305. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7306. iocbq->iocb.ulpFCP2Rcvy);
  7307. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7308. /* Always open the exchange */
  7309. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7310. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7311. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7312. LPFC_WQE_LENLOC_WORD4);
  7313. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7314. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7315. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7316. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7317. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7318. }
  7319. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7320. break;
  7321. case CMD_FCP_ICMND64_CR:
  7322. /* word3 iocb=IO_TAG wqe=reserved */
  7323. wqe->fcp_icmd.rsrvd3 = 0;
  7324. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7325. /* Always open the exchange */
  7326. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7327. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7328. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7329. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7330. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7331. LPFC_WQE_LENLOC_NONE);
  7332. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7333. break;
  7334. case CMD_GEN_REQUEST64_CR:
  7335. /* For this command calculate the xmit length of the
  7336. * request bde.
  7337. */
  7338. xmit_len = 0;
  7339. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7340. sizeof(struct ulp_bde64);
  7341. for (i = 0; i < numBdes; i++) {
  7342. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7343. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7344. break;
  7345. xmit_len += bde.tus.f.bdeSize;
  7346. }
  7347. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7348. wqe->gen_req.request_payload_len = xmit_len;
  7349. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7350. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7351. /* word6 context tag copied in memcpy */
  7352. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7353. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7354. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7355. "2015 Invalid CT %x command 0x%x\n",
  7356. ct, iocbq->iocb.ulpCommand);
  7357. return IOCB_ERROR;
  7358. }
  7359. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7360. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7361. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7362. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7363. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7364. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7365. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7366. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7367. command_type = OTHER_COMMAND;
  7368. break;
  7369. case CMD_XMIT_ELS_RSP64_CX:
  7370. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7371. /* words0-2 BDE memcpy */
  7372. /* word3 iocb=iotag32 wqe=response_payload_len */
  7373. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7374. /* word4 */
  7375. wqe->xmit_els_rsp.word4 = 0;
  7376. /* word5 iocb=rsvd wge=did */
  7377. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7378. iocbq->iocb.un.xseq64.xmit_els_remoteID);
  7379. if_type = bf_get(lpfc_sli_intf_if_type,
  7380. &phba->sli4_hba.sli_intf);
  7381. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7382. if (iocbq->vport->fc_flag & FC_PT2PT) {
  7383. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7384. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7385. iocbq->vport->fc_myDID);
  7386. if (iocbq->vport->fc_myDID == Fabric_DID) {
  7387. bf_set(wqe_els_did,
  7388. &wqe->xmit_els_rsp.wqe_dest, 0);
  7389. }
  7390. }
  7391. }
  7392. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7393. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7394. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7395. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7396. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7397. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7398. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7399. phba->vpi_ids[iocbq->vport->vpi]);
  7400. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7401. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7402. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7403. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7404. LPFC_WQE_LENLOC_WORD3);
  7405. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7406. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7407. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7408. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7409. iocbq->context2)->virt);
  7410. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7411. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7412. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7413. iocbq->vport->fc_myDID);
  7414. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
  7415. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7416. phba->vpi_ids[phba->pport->vpi]);
  7417. }
  7418. command_type = OTHER_COMMAND;
  7419. break;
  7420. case CMD_CLOSE_XRI_CN:
  7421. case CMD_ABORT_XRI_CN:
  7422. case CMD_ABORT_XRI_CX:
  7423. /* words 0-2 memcpy should be 0 rserved */
  7424. /* port will send abts */
  7425. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7426. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7427. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7428. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7429. } else
  7430. fip = 0;
  7431. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7432. /*
  7433. * The link is down, or the command was ELS_FIP
  7434. * so the fw does not need to send abts
  7435. * on the wire.
  7436. */
  7437. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7438. else
  7439. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7440. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7441. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7442. wqe->abort_cmd.rsrvd5 = 0;
  7443. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7444. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7445. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7446. /*
  7447. * The abort handler will send us CMD_ABORT_XRI_CN or
  7448. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7449. */
  7450. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7451. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7452. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7453. LPFC_WQE_LENLOC_NONE);
  7454. cmnd = CMD_ABORT_XRI_CX;
  7455. command_type = OTHER_COMMAND;
  7456. xritag = 0;
  7457. break;
  7458. case CMD_XMIT_BLS_RSP64_CX:
  7459. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7460. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7461. * we re-construct this WQE here based on information in
  7462. * iocbq from scratch.
  7463. */
  7464. memset(wqe, 0, sizeof(union lpfc_wqe));
  7465. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7466. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7467. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7468. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7469. LPFC_ABTS_UNSOL_INT) {
  7470. /* ABTS sent by initiator to CT exchange, the
  7471. * RX_ID field will be filled with the newly
  7472. * allocated responder XRI.
  7473. */
  7474. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7475. iocbq->sli4_xritag);
  7476. } else {
  7477. /* ABTS sent by responder to CT exchange, the
  7478. * RX_ID field will be filled with the responder
  7479. * RX_ID from ABTS.
  7480. */
  7481. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7482. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7483. }
  7484. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7485. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7486. /* Use CT=VPI */
  7487. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7488. ndlp->nlp_DID);
  7489. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7490. iocbq->iocb.ulpContext);
  7491. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7492. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7493. phba->vpi_ids[phba->pport->vpi]);
  7494. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7495. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7496. LPFC_WQE_LENLOC_NONE);
  7497. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7498. command_type = OTHER_COMMAND;
  7499. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7500. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7501. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7502. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7503. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7504. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7505. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7506. }
  7507. break;
  7508. case CMD_XRI_ABORTED_CX:
  7509. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7510. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7511. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7512. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7513. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7514. default:
  7515. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7516. "2014 Invalid command 0x%x\n",
  7517. iocbq->iocb.ulpCommand);
  7518. return IOCB_ERROR;
  7519. break;
  7520. }
  7521. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7522. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7523. wqe->generic.wqe_com.abort_tag = abort_tag;
  7524. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7525. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7526. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7527. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7528. return 0;
  7529. }
  7530. /**
  7531. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7532. * @phba: Pointer to HBA context object.
  7533. * @ring_number: SLI ring number to issue iocb on.
  7534. * @piocb: Pointer to command iocb.
  7535. * @flag: Flag indicating if this command can be put into txq.
  7536. *
  7537. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7538. * an iocb command to an HBA with SLI-4 interface spec.
  7539. *
  7540. * This function is called with hbalock held. The function will return success
  7541. * after it successfully submit the iocb to firmware or after adding to the
  7542. * txq.
  7543. **/
  7544. static int
  7545. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7546. struct lpfc_iocbq *piocb, uint32_t flag)
  7547. {
  7548. struct lpfc_sglq *sglq;
  7549. union lpfc_wqe wqe;
  7550. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7551. if (piocb->sli4_xritag == NO_XRI) {
  7552. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7553. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7554. sglq = NULL;
  7555. else {
  7556. if (pring->txq_cnt) {
  7557. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7558. __lpfc_sli_ringtx_put(phba,
  7559. pring, piocb);
  7560. return IOCB_SUCCESS;
  7561. } else {
  7562. return IOCB_BUSY;
  7563. }
  7564. } else {
  7565. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7566. if (!sglq) {
  7567. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7568. __lpfc_sli_ringtx_put(phba,
  7569. pring,
  7570. piocb);
  7571. return IOCB_SUCCESS;
  7572. } else
  7573. return IOCB_BUSY;
  7574. }
  7575. }
  7576. }
  7577. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7578. /* These IO's already have an XRI and a mapped sgl. */
  7579. sglq = NULL;
  7580. } else {
  7581. /*
  7582. * This is a continuation of a commandi,(CX) so this
  7583. * sglq is on the active list
  7584. */
  7585. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  7586. if (!sglq)
  7587. return IOCB_ERROR;
  7588. }
  7589. if (sglq) {
  7590. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7591. piocb->sli4_xritag = sglq->sli4_xritag;
  7592. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7593. return IOCB_ERROR;
  7594. }
  7595. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7596. return IOCB_ERROR;
  7597. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7598. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7599. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7600. &wqe))
  7601. return IOCB_ERROR;
  7602. } else {
  7603. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7604. return IOCB_ERROR;
  7605. }
  7606. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7607. return 0;
  7608. }
  7609. /**
  7610. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7611. *
  7612. * This routine wraps the actual lockless version for issusing IOCB function
  7613. * pointer from the lpfc_hba struct.
  7614. *
  7615. * Return codes:
  7616. * IOCB_ERROR - Error
  7617. * IOCB_SUCCESS - Success
  7618. * IOCB_BUSY - Busy
  7619. **/
  7620. int
  7621. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7622. struct lpfc_iocbq *piocb, uint32_t flag)
  7623. {
  7624. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7625. }
  7626. /**
  7627. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7628. * @phba: The hba struct for which this call is being executed.
  7629. * @dev_grp: The HBA PCI-Device group number.
  7630. *
  7631. * This routine sets up the SLI interface API function jump table in @phba
  7632. * struct.
  7633. * Returns: 0 - success, -ENODEV - failure.
  7634. **/
  7635. int
  7636. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7637. {
  7638. switch (dev_grp) {
  7639. case LPFC_PCI_DEV_LP:
  7640. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7641. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7642. break;
  7643. case LPFC_PCI_DEV_OC:
  7644. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7645. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7646. break;
  7647. default:
  7648. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7649. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7650. dev_grp);
  7651. return -ENODEV;
  7652. break;
  7653. }
  7654. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7655. return 0;
  7656. }
  7657. /**
  7658. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7659. * @phba: Pointer to HBA context object.
  7660. * @pring: Pointer to driver SLI ring object.
  7661. * @piocb: Pointer to command iocb.
  7662. * @flag: Flag indicating if this command can be put into txq.
  7663. *
  7664. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7665. * function. This function gets the hbalock and calls
  7666. * __lpfc_sli_issue_iocb function and will return the error returned
  7667. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7668. * functions which do not hold hbalock.
  7669. **/
  7670. int
  7671. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7672. struct lpfc_iocbq *piocb, uint32_t flag)
  7673. {
  7674. struct lpfc_sli_ring *pring;
  7675. unsigned long iflags;
  7676. int rc, idx;
  7677. if (phba->sli_rev == LPFC_SLI_REV4) {
  7678. if (piocb->iocb_flag & LPFC_IO_FCP) {
  7679. if (unlikely(!phba->sli4_hba.fcp_wq))
  7680. return IOCB_ERROR;
  7681. idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7682. piocb->fcp_wqidx = idx;
  7683. ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
  7684. }
  7685. pring = &phba->sli.ring[ring_number];
  7686. spin_lock_irqsave(&pring->ring_lock, iflags);
  7687. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7688. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  7689. } else {
  7690. /* For now, SLI2/3 will still use hbalock */
  7691. spin_lock_irqsave(&phba->hbalock, iflags);
  7692. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7693. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7694. }
  7695. return rc;
  7696. }
  7697. /**
  7698. * lpfc_extra_ring_setup - Extra ring setup function
  7699. * @phba: Pointer to HBA context object.
  7700. *
  7701. * This function is called while driver attaches with the
  7702. * HBA to setup the extra ring. The extra ring is used
  7703. * only when driver needs to support target mode functionality
  7704. * or IP over FC functionalities.
  7705. *
  7706. * This function is called with no lock held.
  7707. **/
  7708. static int
  7709. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7710. {
  7711. struct lpfc_sli *psli;
  7712. struct lpfc_sli_ring *pring;
  7713. psli = &phba->sli;
  7714. /* Adjust cmd/rsp ring iocb entries more evenly */
  7715. /* Take some away from the FCP ring */
  7716. pring = &psli->ring[psli->fcp_ring];
  7717. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7718. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7719. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7720. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7721. /* and give them to the extra ring */
  7722. pring = &psli->ring[psli->extra_ring];
  7723. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7724. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7725. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7726. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7727. /* Setup default profile for this ring */
  7728. pring->iotag_max = 4096;
  7729. pring->num_mask = 1;
  7730. pring->prt[0].profile = 0; /* Mask 0 */
  7731. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7732. pring->prt[0].type = phba->cfg_multi_ring_type;
  7733. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7734. return 0;
  7735. }
  7736. /* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
  7737. * @vport: pointer to virtual port object.
  7738. * @ndlp: nodelist pointer for the impacted rport.
  7739. *
  7740. * The driver calls this routine in response to a XRI ABORT CQE
  7741. * event from the port. In this event, the driver is required to
  7742. * recover its login to the rport even though its login may be valid
  7743. * from the driver's perspective. The failed ABTS notice from the
  7744. * port indicates the rport is not responding.
  7745. */
  7746. static void
  7747. lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
  7748. struct lpfc_nodelist *ndlp)
  7749. {
  7750. struct Scsi_Host *shost;
  7751. struct lpfc_hba *phba;
  7752. unsigned long flags = 0;
  7753. shost = lpfc_shost_from_vport(vport);
  7754. phba = vport->phba;
  7755. if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
  7756. lpfc_printf_log(phba, KERN_INFO,
  7757. LOG_SLI, "3093 No rport recovery needed. "
  7758. "rport in state 0x%x\n",
  7759. ndlp->nlp_state);
  7760. return;
  7761. }
  7762. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7763. "3094 Start rport recovery on shost id 0x%x "
  7764. "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
  7765. "flags 0x%x\n",
  7766. shost->host_no, ndlp->nlp_DID,
  7767. vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
  7768. ndlp->nlp_flag);
  7769. /*
  7770. * The rport is not responding. Don't attempt ADISC recovery.
  7771. * Remove the FCP-2 flag to force a PLOGI.
  7772. */
  7773. spin_lock_irqsave(shost->host_lock, flags);
  7774. ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  7775. spin_unlock_irqrestore(shost->host_lock, flags);
  7776. lpfc_disc_state_machine(vport, ndlp, NULL,
  7777. NLP_EVT_DEVICE_RECOVERY);
  7778. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  7779. spin_lock_irqsave(shost->host_lock, flags);
  7780. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  7781. spin_unlock_irqrestore(shost->host_lock, flags);
  7782. lpfc_disc_start(vport);
  7783. }
  7784. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7785. * @phba: Pointer to HBA context object.
  7786. * @iocbq: Pointer to iocb object.
  7787. *
  7788. * The async_event handler calls this routine when it receives
  7789. * an ASYNC_STATUS_CN event from the port. The port generates
  7790. * this event when an Abort Sequence request to an rport fails
  7791. * twice in succession. The abort could be originated by the
  7792. * driver or by the port. The ABTS could have been for an ELS
  7793. * or FCP IO. The port only generates this event when an ABTS
  7794. * fails to complete after one retry.
  7795. */
  7796. static void
  7797. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7798. struct lpfc_iocbq *iocbq)
  7799. {
  7800. struct lpfc_nodelist *ndlp = NULL;
  7801. uint16_t rpi = 0, vpi = 0;
  7802. struct lpfc_vport *vport = NULL;
  7803. /* The rpi in the ulpContext is vport-sensitive. */
  7804. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7805. rpi = iocbq->iocb.ulpContext;
  7806. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7807. "3092 Port generated ABTS async event "
  7808. "on vpi %d rpi %d status 0x%x\n",
  7809. vpi, rpi, iocbq->iocb.ulpStatus);
  7810. vport = lpfc_find_vport_by_vpid(phba, vpi);
  7811. if (!vport)
  7812. goto err_exit;
  7813. ndlp = lpfc_findnode_rpi(vport, rpi);
  7814. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7815. goto err_exit;
  7816. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  7817. lpfc_sli_abts_recover_port(vport, ndlp);
  7818. return;
  7819. err_exit:
  7820. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7821. "3095 Event Context not found, no "
  7822. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  7823. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  7824. vpi, rpi);
  7825. }
  7826. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  7827. * @phba: pointer to HBA context object.
  7828. * @ndlp: nodelist pointer for the impacted rport.
  7829. * @axri: pointer to the wcqe containing the failed exchange.
  7830. *
  7831. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  7832. * port. The port generates this event when an abort exchange request to an
  7833. * rport fails twice in succession with no reply. The abort could be originated
  7834. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  7835. */
  7836. void
  7837. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  7838. struct lpfc_nodelist *ndlp,
  7839. struct sli4_wcqe_xri_aborted *axri)
  7840. {
  7841. struct lpfc_vport *vport;
  7842. uint32_t ext_status = 0;
  7843. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  7844. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7845. "3115 Node Context not found, driver "
  7846. "ignoring abts err event\n");
  7847. return;
  7848. }
  7849. vport = ndlp->vport;
  7850. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7851. "3116 Port generated FCP XRI ABORT event on "
  7852. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  7853. ndlp->vport->vpi, ndlp->nlp_rpi,
  7854. bf_get(lpfc_wcqe_xa_xri, axri),
  7855. bf_get(lpfc_wcqe_xa_status, axri),
  7856. axri->parameter);
  7857. /*
  7858. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  7859. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  7860. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  7861. */
  7862. ext_status = axri->parameter & WCQE_PARAM_MASK;
  7863. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  7864. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  7865. lpfc_sli_abts_recover_port(vport, ndlp);
  7866. }
  7867. /**
  7868. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7869. * @phba: Pointer to HBA context object.
  7870. * @pring: Pointer to driver SLI ring object.
  7871. * @iocbq: Pointer to iocb object.
  7872. *
  7873. * This function is called by the slow ring event handler
  7874. * function when there is an ASYNC event iocb in the ring.
  7875. * This function is called with no lock held.
  7876. * Currently this function handles only temperature related
  7877. * ASYNC events. The function decodes the temperature sensor
  7878. * event message and posts events for the management applications.
  7879. **/
  7880. static void
  7881. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7882. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7883. {
  7884. IOCB_t *icmd;
  7885. uint16_t evt_code;
  7886. struct temp_event temp_event_data;
  7887. struct Scsi_Host *shost;
  7888. uint32_t *iocb_w;
  7889. icmd = &iocbq->iocb;
  7890. evt_code = icmd->un.asyncstat.evt_code;
  7891. switch (evt_code) {
  7892. case ASYNC_TEMP_WARN:
  7893. case ASYNC_TEMP_SAFE:
  7894. temp_event_data.data = (uint32_t) icmd->ulpContext;
  7895. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7896. if (evt_code == ASYNC_TEMP_WARN) {
  7897. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7898. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7899. "0347 Adapter is very hot, please take "
  7900. "corrective action. temperature : %d Celsius\n",
  7901. (uint32_t) icmd->ulpContext);
  7902. } else {
  7903. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7904. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7905. "0340 Adapter temperature is OK now. "
  7906. "temperature : %d Celsius\n",
  7907. (uint32_t) icmd->ulpContext);
  7908. }
  7909. /* Send temperature change event to applications */
  7910. shost = lpfc_shost_from_vport(phba->pport);
  7911. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7912. sizeof(temp_event_data), (char *) &temp_event_data,
  7913. LPFC_NL_VENDOR_ID);
  7914. break;
  7915. case ASYNC_STATUS_CN:
  7916. lpfc_sli_abts_err_handler(phba, iocbq);
  7917. break;
  7918. default:
  7919. iocb_w = (uint32_t *) icmd;
  7920. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7921. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7922. " evt_code 0x%x\n"
  7923. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7924. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7925. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7926. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7927. pring->ringno, icmd->un.asyncstat.evt_code,
  7928. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7929. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  7930. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  7931. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  7932. break;
  7933. }
  7934. }
  7935. /**
  7936. * lpfc_sli_setup - SLI ring setup function
  7937. * @phba: Pointer to HBA context object.
  7938. *
  7939. * lpfc_sli_setup sets up rings of the SLI interface with
  7940. * number of iocbs per ring and iotags. This function is
  7941. * called while driver attach to the HBA and before the
  7942. * interrupts are enabled. So there is no need for locking.
  7943. *
  7944. * This function always returns 0.
  7945. **/
  7946. int
  7947. lpfc_sli_setup(struct lpfc_hba *phba)
  7948. {
  7949. int i, totiocbsize = 0;
  7950. struct lpfc_sli *psli = &phba->sli;
  7951. struct lpfc_sli_ring *pring;
  7952. psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
  7953. if (phba->sli_rev == LPFC_SLI_REV4)
  7954. psli->num_rings += phba->cfg_fcp_io_channel;
  7955. psli->sli_flag = 0;
  7956. psli->fcp_ring = LPFC_FCP_RING;
  7957. psli->next_ring = LPFC_FCP_NEXT_RING;
  7958. psli->extra_ring = LPFC_EXTRA_RING;
  7959. psli->iocbq_lookup = NULL;
  7960. psli->iocbq_lookup_len = 0;
  7961. psli->last_iotag = 0;
  7962. for (i = 0; i < psli->num_rings; i++) {
  7963. pring = &psli->ring[i];
  7964. switch (i) {
  7965. case LPFC_FCP_RING: /* ring 0 - FCP */
  7966. /* numCiocb and numRiocb are used in config_port */
  7967. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  7968. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  7969. pring->sli.sli3.numCiocb +=
  7970. SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7971. pring->sli.sli3.numRiocb +=
  7972. SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7973. pring->sli.sli3.numCiocb +=
  7974. SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7975. pring->sli.sli3.numRiocb +=
  7976. SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7977. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  7978. SLI3_IOCB_CMD_SIZE :
  7979. SLI2_IOCB_CMD_SIZE;
  7980. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  7981. SLI3_IOCB_RSP_SIZE :
  7982. SLI2_IOCB_RSP_SIZE;
  7983. pring->iotag_ctr = 0;
  7984. pring->iotag_max =
  7985. (phba->cfg_hba_queue_depth * 2);
  7986. pring->fast_iotag = pring->iotag_max;
  7987. pring->num_mask = 0;
  7988. break;
  7989. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  7990. /* numCiocb and numRiocb are used in config_port */
  7991. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  7992. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  7993. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  7994. SLI3_IOCB_CMD_SIZE :
  7995. SLI2_IOCB_CMD_SIZE;
  7996. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  7997. SLI3_IOCB_RSP_SIZE :
  7998. SLI2_IOCB_RSP_SIZE;
  7999. pring->iotag_max = phba->cfg_hba_queue_depth;
  8000. pring->num_mask = 0;
  8001. break;
  8002. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  8003. /* numCiocb and numRiocb are used in config_port */
  8004. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  8005. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  8006. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8007. SLI3_IOCB_CMD_SIZE :
  8008. SLI2_IOCB_CMD_SIZE;
  8009. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8010. SLI3_IOCB_RSP_SIZE :
  8011. SLI2_IOCB_RSP_SIZE;
  8012. pring->fast_iotag = 0;
  8013. pring->iotag_ctr = 0;
  8014. pring->iotag_max = 4096;
  8015. pring->lpfc_sli_rcv_async_status =
  8016. lpfc_sli_async_event_handler;
  8017. pring->num_mask = LPFC_MAX_RING_MASK;
  8018. pring->prt[0].profile = 0; /* Mask 0 */
  8019. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  8020. pring->prt[0].type = FC_TYPE_ELS;
  8021. pring->prt[0].lpfc_sli_rcv_unsol_event =
  8022. lpfc_els_unsol_event;
  8023. pring->prt[1].profile = 0; /* Mask 1 */
  8024. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  8025. pring->prt[1].type = FC_TYPE_ELS;
  8026. pring->prt[1].lpfc_sli_rcv_unsol_event =
  8027. lpfc_els_unsol_event;
  8028. pring->prt[2].profile = 0; /* Mask 2 */
  8029. /* NameServer Inquiry */
  8030. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  8031. /* NameServer */
  8032. pring->prt[2].type = FC_TYPE_CT;
  8033. pring->prt[2].lpfc_sli_rcv_unsol_event =
  8034. lpfc_ct_unsol_event;
  8035. pring->prt[3].profile = 0; /* Mask 3 */
  8036. /* NameServer response */
  8037. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  8038. /* NameServer */
  8039. pring->prt[3].type = FC_TYPE_CT;
  8040. pring->prt[3].lpfc_sli_rcv_unsol_event =
  8041. lpfc_ct_unsol_event;
  8042. /* abort unsolicited sequence */
  8043. pring->prt[4].profile = 0; /* Mask 4 */
  8044. pring->prt[4].rctl = FC_RCTL_BA_ABTS;
  8045. pring->prt[4].type = FC_TYPE_BLS;
  8046. pring->prt[4].lpfc_sli_rcv_unsol_event =
  8047. lpfc_sli4_ct_abort_unsol_event;
  8048. break;
  8049. }
  8050. totiocbsize += (pring->sli.sli3.numCiocb *
  8051. pring->sli.sli3.sizeCiocb) +
  8052. (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
  8053. }
  8054. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  8055. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  8056. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  8057. "SLI2 SLIM Data: x%x x%lx\n",
  8058. phba->brd_no, totiocbsize,
  8059. (unsigned long) MAX_SLIM_IOCB_SIZE);
  8060. }
  8061. if (phba->cfg_multi_ring_support == 2)
  8062. lpfc_extra_ring_setup(phba);
  8063. return 0;
  8064. }
  8065. /**
  8066. * lpfc_sli_queue_setup - Queue initialization function
  8067. * @phba: Pointer to HBA context object.
  8068. *
  8069. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  8070. * ring. This function also initializes ring indices of each ring.
  8071. * This function is called during the initialization of the SLI
  8072. * interface of an HBA.
  8073. * This function is called with no lock held and always returns
  8074. * 1.
  8075. **/
  8076. int
  8077. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  8078. {
  8079. struct lpfc_sli *psli;
  8080. struct lpfc_sli_ring *pring;
  8081. int i;
  8082. psli = &phba->sli;
  8083. spin_lock_irq(&phba->hbalock);
  8084. INIT_LIST_HEAD(&psli->mboxq);
  8085. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  8086. /* Initialize list headers for txq and txcmplq as double linked lists */
  8087. for (i = 0; i < psli->num_rings; i++) {
  8088. pring = &psli->ring[i];
  8089. pring->ringno = i;
  8090. pring->sli.sli3.next_cmdidx = 0;
  8091. pring->sli.sli3.local_getidx = 0;
  8092. pring->sli.sli3.cmdidx = 0;
  8093. INIT_LIST_HEAD(&pring->txq);
  8094. INIT_LIST_HEAD(&pring->txcmplq);
  8095. INIT_LIST_HEAD(&pring->iocb_continueq);
  8096. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  8097. INIT_LIST_HEAD(&pring->postbufq);
  8098. spin_lock_init(&pring->ring_lock);
  8099. }
  8100. spin_unlock_irq(&phba->hbalock);
  8101. return 1;
  8102. }
  8103. /**
  8104. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  8105. * @phba: Pointer to HBA context object.
  8106. *
  8107. * This routine flushes the mailbox command subsystem. It will unconditionally
  8108. * flush all the mailbox commands in the three possible stages in the mailbox
  8109. * command sub-system: pending mailbox command queue; the outstanding mailbox
  8110. * command; and completed mailbox command queue. It is caller's responsibility
  8111. * to make sure that the driver is in the proper state to flush the mailbox
  8112. * command sub-system. Namely, the posting of mailbox commands into the
  8113. * pending mailbox command queue from the various clients must be stopped;
  8114. * either the HBA is in a state that it will never works on the outstanding
  8115. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  8116. * mailbox command has been completed.
  8117. **/
  8118. static void
  8119. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  8120. {
  8121. LIST_HEAD(completions);
  8122. struct lpfc_sli *psli = &phba->sli;
  8123. LPFC_MBOXQ_t *pmb;
  8124. unsigned long iflag;
  8125. /* Flush all the mailbox commands in the mbox system */
  8126. spin_lock_irqsave(&phba->hbalock, iflag);
  8127. /* The pending mailbox command queue */
  8128. list_splice_init(&phba->sli.mboxq, &completions);
  8129. /* The outstanding active mailbox command */
  8130. if (psli->mbox_active) {
  8131. list_add_tail(&psli->mbox_active->list, &completions);
  8132. psli->mbox_active = NULL;
  8133. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  8134. }
  8135. /* The completed mailbox command queue */
  8136. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  8137. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8138. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  8139. while (!list_empty(&completions)) {
  8140. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  8141. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  8142. if (pmb->mbox_cmpl)
  8143. pmb->mbox_cmpl(phba, pmb);
  8144. }
  8145. }
  8146. /**
  8147. * lpfc_sli_host_down - Vport cleanup function
  8148. * @vport: Pointer to virtual port object.
  8149. *
  8150. * lpfc_sli_host_down is called to clean up the resources
  8151. * associated with a vport before destroying virtual
  8152. * port data structures.
  8153. * This function does following operations:
  8154. * - Free discovery resources associated with this virtual
  8155. * port.
  8156. * - Free iocbs associated with this virtual port in
  8157. * the txq.
  8158. * - Send abort for all iocb commands associated with this
  8159. * vport in txcmplq.
  8160. *
  8161. * This function is called with no lock held and always returns 1.
  8162. **/
  8163. int
  8164. lpfc_sli_host_down(struct lpfc_vport *vport)
  8165. {
  8166. LIST_HEAD(completions);
  8167. struct lpfc_hba *phba = vport->phba;
  8168. struct lpfc_sli *psli = &phba->sli;
  8169. struct lpfc_sli_ring *pring;
  8170. struct lpfc_iocbq *iocb, *next_iocb;
  8171. int i;
  8172. unsigned long flags = 0;
  8173. uint16_t prev_pring_flag;
  8174. lpfc_cleanup_discovery_resources(vport);
  8175. spin_lock_irqsave(&phba->hbalock, flags);
  8176. for (i = 0; i < psli->num_rings; i++) {
  8177. pring = &psli->ring[i];
  8178. prev_pring_flag = pring->flag;
  8179. /* Only slow rings */
  8180. if (pring->ringno == LPFC_ELS_RING) {
  8181. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8182. /* Set the lpfc data pending flag */
  8183. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8184. }
  8185. /*
  8186. * Error everything on the txq since these iocbs have not been
  8187. * given to the FW yet.
  8188. */
  8189. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  8190. if (iocb->vport != vport)
  8191. continue;
  8192. list_move_tail(&iocb->list, &completions);
  8193. pring->txq_cnt--;
  8194. }
  8195. /* Next issue ABTS for everything on the txcmplq */
  8196. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8197. list) {
  8198. if (iocb->vport != vport)
  8199. continue;
  8200. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8201. }
  8202. pring->flag = prev_pring_flag;
  8203. }
  8204. spin_unlock_irqrestore(&phba->hbalock, flags);
  8205. /* Cancel all the IOCBs from the completions list */
  8206. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8207. IOERR_SLI_DOWN);
  8208. return 1;
  8209. }
  8210. /**
  8211. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8212. * @phba: Pointer to HBA context object.
  8213. *
  8214. * This function cleans up all iocb, buffers, mailbox commands
  8215. * while shutting down the HBA. This function is called with no
  8216. * lock held and always returns 1.
  8217. * This function does the following to cleanup driver resources:
  8218. * - Free discovery resources for each virtual port
  8219. * - Cleanup any pending fabric iocbs
  8220. * - Iterate through the iocb txq and free each entry
  8221. * in the list.
  8222. * - Free up any buffer posted to the HBA
  8223. * - Free mailbox commands in the mailbox queue.
  8224. **/
  8225. int
  8226. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8227. {
  8228. LIST_HEAD(completions);
  8229. struct lpfc_sli *psli = &phba->sli;
  8230. struct lpfc_sli_ring *pring;
  8231. struct lpfc_dmabuf *buf_ptr;
  8232. unsigned long flags = 0;
  8233. int i;
  8234. /* Shutdown the mailbox command sub-system */
  8235. lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
  8236. lpfc_hba_down_prep(phba);
  8237. lpfc_fabric_abort_hba(phba);
  8238. spin_lock_irqsave(&phba->hbalock, flags);
  8239. for (i = 0; i < psli->num_rings; i++) {
  8240. pring = &psli->ring[i];
  8241. /* Only slow rings */
  8242. if (pring->ringno == LPFC_ELS_RING) {
  8243. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8244. /* Set the lpfc data pending flag */
  8245. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8246. }
  8247. /*
  8248. * Error everything on the txq since these iocbs have not been
  8249. * given to the FW yet.
  8250. */
  8251. list_splice_init(&pring->txq, &completions);
  8252. pring->txq_cnt = 0;
  8253. }
  8254. spin_unlock_irqrestore(&phba->hbalock, flags);
  8255. /* Cancel all the IOCBs from the completions list */
  8256. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8257. IOERR_SLI_DOWN);
  8258. spin_lock_irqsave(&phba->hbalock, flags);
  8259. list_splice_init(&phba->elsbuf, &completions);
  8260. phba->elsbuf_cnt = 0;
  8261. phba->elsbuf_prev_cnt = 0;
  8262. spin_unlock_irqrestore(&phba->hbalock, flags);
  8263. while (!list_empty(&completions)) {
  8264. list_remove_head(&completions, buf_ptr,
  8265. struct lpfc_dmabuf, list);
  8266. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8267. kfree(buf_ptr);
  8268. }
  8269. /* Return any active mbox cmds */
  8270. del_timer_sync(&psli->mbox_tmo);
  8271. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8272. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8273. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8274. return 1;
  8275. }
  8276. /**
  8277. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8278. * @srcp: Source memory pointer.
  8279. * @destp: Destination memory pointer.
  8280. * @cnt: Number of words required to be copied.
  8281. *
  8282. * This function is used for copying data between driver memory
  8283. * and the SLI memory. This function also changes the endianness
  8284. * of each word if native endianness is different from SLI
  8285. * endianness. This function can be called with or without
  8286. * lock.
  8287. **/
  8288. void
  8289. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8290. {
  8291. uint32_t *src = srcp;
  8292. uint32_t *dest = destp;
  8293. uint32_t ldata;
  8294. int i;
  8295. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8296. ldata = *src;
  8297. ldata = le32_to_cpu(ldata);
  8298. *dest = ldata;
  8299. src++;
  8300. dest++;
  8301. }
  8302. }
  8303. /**
  8304. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8305. * @srcp: Source memory pointer.
  8306. * @destp: Destination memory pointer.
  8307. * @cnt: Number of words required to be copied.
  8308. *
  8309. * This function is used for copying data between a data structure
  8310. * with big endian representation to local endianness.
  8311. * This function can be called with or without lock.
  8312. **/
  8313. void
  8314. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8315. {
  8316. uint32_t *src = srcp;
  8317. uint32_t *dest = destp;
  8318. uint32_t ldata;
  8319. int i;
  8320. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8321. ldata = *src;
  8322. ldata = be32_to_cpu(ldata);
  8323. *dest = ldata;
  8324. src++;
  8325. dest++;
  8326. }
  8327. }
  8328. /**
  8329. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8330. * @phba: Pointer to HBA context object.
  8331. * @pring: Pointer to driver SLI ring object.
  8332. * @mp: Pointer to driver buffer object.
  8333. *
  8334. * This function is called with no lock held.
  8335. * It always return zero after adding the buffer to the postbufq
  8336. * buffer list.
  8337. **/
  8338. int
  8339. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8340. struct lpfc_dmabuf *mp)
  8341. {
  8342. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8343. later */
  8344. spin_lock_irq(&phba->hbalock);
  8345. list_add_tail(&mp->list, &pring->postbufq);
  8346. pring->postbufq_cnt++;
  8347. spin_unlock_irq(&phba->hbalock);
  8348. return 0;
  8349. }
  8350. /**
  8351. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8352. * @phba: Pointer to HBA context object.
  8353. *
  8354. * When HBQ is enabled, buffers are searched based on tags. This function
  8355. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8356. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8357. * does not conflict with tags of buffer posted for unsolicited events.
  8358. * The function returns the allocated tag. The function is called with
  8359. * no locks held.
  8360. **/
  8361. uint32_t
  8362. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8363. {
  8364. spin_lock_irq(&phba->hbalock);
  8365. phba->buffer_tag_count++;
  8366. /*
  8367. * Always set the QUE_BUFTAG_BIT to distiguish between
  8368. * a tag assigned by HBQ.
  8369. */
  8370. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8371. spin_unlock_irq(&phba->hbalock);
  8372. return phba->buffer_tag_count;
  8373. }
  8374. /**
  8375. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8376. * @phba: Pointer to HBA context object.
  8377. * @pring: Pointer to driver SLI ring object.
  8378. * @tag: Buffer tag.
  8379. *
  8380. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8381. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8382. * iocb is posted to the response ring with the tag of the buffer.
  8383. * This function searches the pring->postbufq list using the tag
  8384. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8385. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8386. * buffer is returned to the caller else NULL is returned.
  8387. * This function is called with no lock held.
  8388. **/
  8389. struct lpfc_dmabuf *
  8390. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8391. uint32_t tag)
  8392. {
  8393. struct lpfc_dmabuf *mp, *next_mp;
  8394. struct list_head *slp = &pring->postbufq;
  8395. /* Search postbufq, from the beginning, looking for a match on tag */
  8396. spin_lock_irq(&phba->hbalock);
  8397. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8398. if (mp->buffer_tag == tag) {
  8399. list_del_init(&mp->list);
  8400. pring->postbufq_cnt--;
  8401. spin_unlock_irq(&phba->hbalock);
  8402. return mp;
  8403. }
  8404. }
  8405. spin_unlock_irq(&phba->hbalock);
  8406. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8407. "0402 Cannot find virtual addr for buffer tag on "
  8408. "ring %d Data x%lx x%p x%p x%x\n",
  8409. pring->ringno, (unsigned long) tag,
  8410. slp->next, slp->prev, pring->postbufq_cnt);
  8411. return NULL;
  8412. }
  8413. /**
  8414. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8415. * @phba: Pointer to HBA context object.
  8416. * @pring: Pointer to driver SLI ring object.
  8417. * @phys: DMA address of the buffer.
  8418. *
  8419. * This function searches the buffer list using the dma_address
  8420. * of unsolicited event to find the driver's lpfc_dmabuf object
  8421. * corresponding to the dma_address. The function returns the
  8422. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8423. * This function is called by the ct and els unsolicited event
  8424. * handlers to get the buffer associated with the unsolicited
  8425. * event.
  8426. *
  8427. * This function is called with no lock held.
  8428. **/
  8429. struct lpfc_dmabuf *
  8430. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8431. dma_addr_t phys)
  8432. {
  8433. struct lpfc_dmabuf *mp, *next_mp;
  8434. struct list_head *slp = &pring->postbufq;
  8435. /* Search postbufq, from the beginning, looking for a match on phys */
  8436. spin_lock_irq(&phba->hbalock);
  8437. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8438. if (mp->phys == phys) {
  8439. list_del_init(&mp->list);
  8440. pring->postbufq_cnt--;
  8441. spin_unlock_irq(&phba->hbalock);
  8442. return mp;
  8443. }
  8444. }
  8445. spin_unlock_irq(&phba->hbalock);
  8446. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8447. "0410 Cannot find virtual addr for mapped buf on "
  8448. "ring %d Data x%llx x%p x%p x%x\n",
  8449. pring->ringno, (unsigned long long)phys,
  8450. slp->next, slp->prev, pring->postbufq_cnt);
  8451. return NULL;
  8452. }
  8453. /**
  8454. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8455. * @phba: Pointer to HBA context object.
  8456. * @cmdiocb: Pointer to driver command iocb object.
  8457. * @rspiocb: Pointer to driver response iocb object.
  8458. *
  8459. * This function is the completion handler for the abort iocbs for
  8460. * ELS commands. This function is called from the ELS ring event
  8461. * handler with no lock held. This function frees memory resources
  8462. * associated with the abort iocb.
  8463. **/
  8464. static void
  8465. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8466. struct lpfc_iocbq *rspiocb)
  8467. {
  8468. IOCB_t *irsp = &rspiocb->iocb;
  8469. uint16_t abort_iotag, abort_context;
  8470. struct lpfc_iocbq *abort_iocb = NULL;
  8471. if (irsp->ulpStatus) {
  8472. /*
  8473. * Assume that the port already completed and returned, or
  8474. * will return the iocb. Just Log the message.
  8475. */
  8476. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8477. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8478. spin_lock_irq(&phba->hbalock);
  8479. if (phba->sli_rev < LPFC_SLI_REV4) {
  8480. if (abort_iotag != 0 &&
  8481. abort_iotag <= phba->sli.last_iotag)
  8482. abort_iocb =
  8483. phba->sli.iocbq_lookup[abort_iotag];
  8484. } else
  8485. /* For sli4 the abort_tag is the XRI,
  8486. * so the abort routine puts the iotag of the iocb
  8487. * being aborted in the context field of the abort
  8488. * IOCB.
  8489. */
  8490. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8491. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8492. "0327 Cannot abort els iocb %p "
  8493. "with tag %x context %x, abort status %x, "
  8494. "abort code %x\n",
  8495. abort_iocb, abort_iotag, abort_context,
  8496. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8497. spin_unlock_irq(&phba->hbalock);
  8498. }
  8499. lpfc_sli_release_iocbq(phba, cmdiocb);
  8500. return;
  8501. }
  8502. /**
  8503. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8504. * @phba: Pointer to HBA context object.
  8505. * @cmdiocb: Pointer to driver command iocb object.
  8506. * @rspiocb: Pointer to driver response iocb object.
  8507. *
  8508. * The function is called from SLI ring event handler with no
  8509. * lock held. This function is the completion handler for ELS commands
  8510. * which are aborted. The function frees memory resources used for
  8511. * the aborted ELS commands.
  8512. **/
  8513. static void
  8514. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8515. struct lpfc_iocbq *rspiocb)
  8516. {
  8517. IOCB_t *irsp = &rspiocb->iocb;
  8518. /* ELS cmd tag <ulpIoTag> completes */
  8519. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8520. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8521. "x%x x%x x%x\n",
  8522. irsp->ulpIoTag, irsp->ulpStatus,
  8523. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8524. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8525. lpfc_ct_free_iocb(phba, cmdiocb);
  8526. else
  8527. lpfc_els_free_iocb(phba, cmdiocb);
  8528. return;
  8529. }
  8530. /**
  8531. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8532. * @phba: Pointer to HBA context object.
  8533. * @pring: Pointer to driver SLI ring object.
  8534. * @cmdiocb: Pointer to driver command iocb object.
  8535. *
  8536. * This function issues an abort iocb for the provided command iocb down to
  8537. * the port. Other than the case the outstanding command iocb is an abort
  8538. * request, this function issues abort out unconditionally. This function is
  8539. * called with hbalock held. The function returns 0 when it fails due to
  8540. * memory allocation failure or when the command iocb is an abort request.
  8541. **/
  8542. static int
  8543. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8544. struct lpfc_iocbq *cmdiocb)
  8545. {
  8546. struct lpfc_vport *vport = cmdiocb->vport;
  8547. struct lpfc_iocbq *abtsiocbp;
  8548. IOCB_t *icmd = NULL;
  8549. IOCB_t *iabt = NULL;
  8550. int retval;
  8551. unsigned long iflags;
  8552. /*
  8553. * There are certain command types we don't want to abort. And we
  8554. * don't want to abort commands that are already in the process of
  8555. * being aborted.
  8556. */
  8557. icmd = &cmdiocb->iocb;
  8558. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8559. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8560. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8561. return 0;
  8562. /* issue ABTS for this IOCB based on iotag */
  8563. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8564. if (abtsiocbp == NULL)
  8565. return 0;
  8566. /* This signals the response to set the correct status
  8567. * before calling the completion handler
  8568. */
  8569. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8570. iabt = &abtsiocbp->iocb;
  8571. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8572. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8573. if (phba->sli_rev == LPFC_SLI_REV4) {
  8574. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8575. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8576. }
  8577. else
  8578. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8579. iabt->ulpLe = 1;
  8580. iabt->ulpClass = icmd->ulpClass;
  8581. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8582. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8583. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8584. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8585. if (phba->link_state >= LPFC_LINK_UP)
  8586. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8587. else
  8588. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8589. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8590. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8591. "0339 Abort xri x%x, original iotag x%x, "
  8592. "abort cmd iotag x%x\n",
  8593. iabt->un.acxri.abortIoTag,
  8594. iabt->un.acxri.abortContextTag,
  8595. abtsiocbp->iotag);
  8596. if (phba->sli_rev == LPFC_SLI_REV4) {
  8597. /* Note: both hbalock and ring_lock need to be set here */
  8598. spin_lock_irqsave(&pring->ring_lock, iflags);
  8599. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8600. abtsiocbp, 0);
  8601. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  8602. } else {
  8603. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8604. abtsiocbp, 0);
  8605. }
  8606. if (retval)
  8607. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8608. /*
  8609. * Caller to this routine should check for IOCB_ERROR
  8610. * and handle it properly. This routine no longer removes
  8611. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8612. */
  8613. return retval;
  8614. }
  8615. /**
  8616. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8617. * @phba: Pointer to HBA context object.
  8618. * @pring: Pointer to driver SLI ring object.
  8619. * @cmdiocb: Pointer to driver command iocb object.
  8620. *
  8621. * This function issues an abort iocb for the provided command iocb. In case
  8622. * of unloading, the abort iocb will not be issued to commands on the ELS
  8623. * ring. Instead, the callback function shall be changed to those commands
  8624. * so that nothing happens when them finishes. This function is called with
  8625. * hbalock held. The function returns 0 when the command iocb is an abort
  8626. * request.
  8627. **/
  8628. int
  8629. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8630. struct lpfc_iocbq *cmdiocb)
  8631. {
  8632. struct lpfc_vport *vport = cmdiocb->vport;
  8633. int retval = IOCB_ERROR;
  8634. IOCB_t *icmd = NULL;
  8635. /*
  8636. * There are certain command types we don't want to abort. And we
  8637. * don't want to abort commands that are already in the process of
  8638. * being aborted.
  8639. */
  8640. icmd = &cmdiocb->iocb;
  8641. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8642. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8643. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8644. return 0;
  8645. /*
  8646. * If we're unloading, don't abort iocb on the ELS ring, but change
  8647. * the callback so that nothing happens when it finishes.
  8648. */
  8649. if ((vport->load_flag & FC_UNLOADING) &&
  8650. (pring->ringno == LPFC_ELS_RING)) {
  8651. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8652. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8653. else
  8654. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8655. goto abort_iotag_exit;
  8656. }
  8657. /* Now, we try to issue the abort to the cmdiocb out */
  8658. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8659. abort_iotag_exit:
  8660. /*
  8661. * Caller to this routine should check for IOCB_ERROR
  8662. * and handle it properly. This routine no longer removes
  8663. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8664. */
  8665. return retval;
  8666. }
  8667. /**
  8668. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8669. * @phba: Pointer to HBA context object.
  8670. * @pring: Pointer to driver SLI ring object.
  8671. *
  8672. * This function aborts all iocbs in the given ring and frees all the iocb
  8673. * objects in txq. This function issues abort iocbs unconditionally for all
  8674. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8675. * to complete before the return of this function. The caller is not required
  8676. * to hold any locks.
  8677. **/
  8678. static void
  8679. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8680. {
  8681. LIST_HEAD(completions);
  8682. struct lpfc_iocbq *iocb, *next_iocb;
  8683. if (pring->ringno == LPFC_ELS_RING)
  8684. lpfc_fabric_abort_hba(phba);
  8685. spin_lock_irq(&phba->hbalock);
  8686. /* Take off all the iocbs on txq for cancelling */
  8687. list_splice_init(&pring->txq, &completions);
  8688. pring->txq_cnt = 0;
  8689. /* Next issue ABTS for everything on the txcmplq */
  8690. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8691. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8692. spin_unlock_irq(&phba->hbalock);
  8693. /* Cancel all the IOCBs from the completions list */
  8694. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8695. IOERR_SLI_ABORTED);
  8696. }
  8697. /**
  8698. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8699. * @phba: pointer to lpfc HBA data structure.
  8700. *
  8701. * This routine will abort all pending and outstanding iocbs to an HBA.
  8702. **/
  8703. void
  8704. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8705. {
  8706. struct lpfc_sli *psli = &phba->sli;
  8707. struct lpfc_sli_ring *pring;
  8708. int i;
  8709. for (i = 0; i < psli->num_rings; i++) {
  8710. pring = &psli->ring[i];
  8711. lpfc_sli_iocb_ring_abort(phba, pring);
  8712. }
  8713. }
  8714. /**
  8715. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8716. * @iocbq: Pointer to driver iocb object.
  8717. * @vport: Pointer to driver virtual port object.
  8718. * @tgt_id: SCSI ID of the target.
  8719. * @lun_id: LUN ID of the scsi device.
  8720. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8721. *
  8722. * This function acts as an iocb filter for functions which abort or count
  8723. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8724. * 0 if the filtering criteria is met for the given iocb and will return
  8725. * 1 if the filtering criteria is not met.
  8726. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8727. * given iocb is for the SCSI device specified by vport, tgt_id and
  8728. * lun_id parameter.
  8729. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8730. * given iocb is for the SCSI target specified by vport and tgt_id
  8731. * parameters.
  8732. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8733. * given iocb is for the SCSI host associated with the given vport.
  8734. * This function is called with no locks held.
  8735. **/
  8736. static int
  8737. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8738. uint16_t tgt_id, uint64_t lun_id,
  8739. lpfc_ctx_cmd ctx_cmd)
  8740. {
  8741. struct lpfc_scsi_buf *lpfc_cmd;
  8742. int rc = 1;
  8743. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8744. return rc;
  8745. if (iocbq->vport != vport)
  8746. return rc;
  8747. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8748. if (lpfc_cmd->pCmd == NULL)
  8749. return rc;
  8750. switch (ctx_cmd) {
  8751. case LPFC_CTX_LUN:
  8752. if ((lpfc_cmd->rdata->pnode) &&
  8753. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8754. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8755. rc = 0;
  8756. break;
  8757. case LPFC_CTX_TGT:
  8758. if ((lpfc_cmd->rdata->pnode) &&
  8759. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8760. rc = 0;
  8761. break;
  8762. case LPFC_CTX_HOST:
  8763. rc = 0;
  8764. break;
  8765. default:
  8766. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8767. __func__, ctx_cmd);
  8768. break;
  8769. }
  8770. return rc;
  8771. }
  8772. /**
  8773. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8774. * @vport: Pointer to virtual port.
  8775. * @tgt_id: SCSI ID of the target.
  8776. * @lun_id: LUN ID of the scsi device.
  8777. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8778. *
  8779. * This function returns number of FCP commands pending for the vport.
  8780. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8781. * commands pending on the vport associated with SCSI device specified
  8782. * by tgt_id and lun_id parameters.
  8783. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8784. * commands pending on the vport associated with SCSI target specified
  8785. * by tgt_id parameter.
  8786. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8787. * commands pending on the vport.
  8788. * This function returns the number of iocbs which satisfy the filter.
  8789. * This function is called without any lock held.
  8790. **/
  8791. int
  8792. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8793. lpfc_ctx_cmd ctx_cmd)
  8794. {
  8795. struct lpfc_hba *phba = vport->phba;
  8796. struct lpfc_iocbq *iocbq;
  8797. int sum, i;
  8798. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8799. iocbq = phba->sli.iocbq_lookup[i];
  8800. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8801. ctx_cmd) == 0)
  8802. sum++;
  8803. }
  8804. return sum;
  8805. }
  8806. /**
  8807. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8808. * @phba: Pointer to HBA context object
  8809. * @cmdiocb: Pointer to command iocb object.
  8810. * @rspiocb: Pointer to response iocb object.
  8811. *
  8812. * This function is called when an aborted FCP iocb completes. This
  8813. * function is called by the ring event handler with no lock held.
  8814. * This function frees the iocb.
  8815. **/
  8816. void
  8817. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8818. struct lpfc_iocbq *rspiocb)
  8819. {
  8820. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8821. "3096 ABORT_XRI_CN completing on xri x%x "
  8822. "original iotag x%x, abort cmd iotag x%x "
  8823. "status 0x%x, reason 0x%x\n",
  8824. cmdiocb->iocb.un.acxri.abortContextTag,
  8825. cmdiocb->iocb.un.acxri.abortIoTag,
  8826. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  8827. rspiocb->iocb.un.ulpWord[4]);
  8828. lpfc_sli_release_iocbq(phba, cmdiocb);
  8829. return;
  8830. }
  8831. /**
  8832. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8833. * @vport: Pointer to virtual port.
  8834. * @pring: Pointer to driver SLI ring object.
  8835. * @tgt_id: SCSI ID of the target.
  8836. * @lun_id: LUN ID of the scsi device.
  8837. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8838. *
  8839. * This function sends an abort command for every SCSI command
  8840. * associated with the given virtual port pending on the ring
  8841. * filtered by lpfc_sli_validate_fcp_iocb function.
  8842. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8843. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8844. * parameters
  8845. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8846. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8847. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8848. * FCP iocbs associated with virtual port.
  8849. * This function returns number of iocbs it failed to abort.
  8850. * This function is called with no locks held.
  8851. **/
  8852. int
  8853. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8854. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8855. {
  8856. struct lpfc_hba *phba = vport->phba;
  8857. struct lpfc_iocbq *iocbq;
  8858. struct lpfc_iocbq *abtsiocb;
  8859. IOCB_t *cmd = NULL;
  8860. int errcnt = 0, ret_val = 0;
  8861. int i;
  8862. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8863. iocbq = phba->sli.iocbq_lookup[i];
  8864. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8865. abort_cmd) != 0)
  8866. continue;
  8867. /* issue ABTS for this IOCB based on iotag */
  8868. abtsiocb = lpfc_sli_get_iocbq(phba);
  8869. if (abtsiocb == NULL) {
  8870. errcnt++;
  8871. continue;
  8872. }
  8873. cmd = &iocbq->iocb;
  8874. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8875. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8876. if (phba->sli_rev == LPFC_SLI_REV4)
  8877. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8878. else
  8879. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8880. abtsiocb->iocb.ulpLe = 1;
  8881. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8882. abtsiocb->vport = phba->pport;
  8883. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8884. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8885. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8886. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8887. if (lpfc_is_link_up(phba))
  8888. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8889. else
  8890. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8891. /* Setup callback routine and issue the command. */
  8892. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8893. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8894. abtsiocb, 0);
  8895. if (ret_val == IOCB_ERROR) {
  8896. lpfc_sli_release_iocbq(phba, abtsiocb);
  8897. errcnt++;
  8898. continue;
  8899. }
  8900. }
  8901. return errcnt;
  8902. }
  8903. /**
  8904. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8905. * @phba: Pointer to HBA context object.
  8906. * @cmdiocbq: Pointer to command iocb.
  8907. * @rspiocbq: Pointer to response iocb.
  8908. *
  8909. * This function is the completion handler for iocbs issued using
  8910. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8911. * ring event handler function without any lock held. This function
  8912. * can be called from both worker thread context and interrupt
  8913. * context. This function also can be called from other thread which
  8914. * cleans up the SLI layer objects.
  8915. * This function copy the contents of the response iocb to the
  8916. * response iocb memory object provided by the caller of
  8917. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8918. * sleeps for the iocb completion.
  8919. **/
  8920. static void
  8921. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8922. struct lpfc_iocbq *cmdiocbq,
  8923. struct lpfc_iocbq *rspiocbq)
  8924. {
  8925. wait_queue_head_t *pdone_q;
  8926. unsigned long iflags;
  8927. struct lpfc_scsi_buf *lpfc_cmd;
  8928. spin_lock_irqsave(&phba->hbalock, iflags);
  8929. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8930. if (cmdiocbq->context2 && rspiocbq)
  8931. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8932. &rspiocbq->iocb, sizeof(IOCB_t));
  8933. /* Set the exchange busy flag for task management commands */
  8934. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8935. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  8936. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  8937. cur_iocbq);
  8938. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  8939. }
  8940. pdone_q = cmdiocbq->context_un.wait_queue;
  8941. if (pdone_q)
  8942. wake_up(pdone_q);
  8943. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8944. return;
  8945. }
  8946. /**
  8947. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  8948. * @phba: Pointer to HBA context object..
  8949. * @piocbq: Pointer to command iocb.
  8950. * @flag: Flag to test.
  8951. *
  8952. * This routine grabs the hbalock and then test the iocb_flag to
  8953. * see if the passed in flag is set.
  8954. * Returns:
  8955. * 1 if flag is set.
  8956. * 0 if flag is not set.
  8957. **/
  8958. static int
  8959. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  8960. struct lpfc_iocbq *piocbq, uint32_t flag)
  8961. {
  8962. unsigned long iflags;
  8963. int ret;
  8964. spin_lock_irqsave(&phba->hbalock, iflags);
  8965. ret = piocbq->iocb_flag & flag;
  8966. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8967. return ret;
  8968. }
  8969. /**
  8970. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  8971. * @phba: Pointer to HBA context object..
  8972. * @pring: Pointer to sli ring.
  8973. * @piocb: Pointer to command iocb.
  8974. * @prspiocbq: Pointer to response iocb.
  8975. * @timeout: Timeout in number of seconds.
  8976. *
  8977. * This function issues the iocb to firmware and waits for the
  8978. * iocb to complete. If the iocb command is not
  8979. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  8980. * Caller should not free the iocb resources if this function
  8981. * returns IOCB_TIMEDOUT.
  8982. * The function waits for the iocb completion using an
  8983. * non-interruptible wait.
  8984. * This function will sleep while waiting for iocb completion.
  8985. * So, this function should not be called from any context which
  8986. * does not allow sleeping. Due to the same reason, this function
  8987. * cannot be called with interrupt disabled.
  8988. * This function assumes that the iocb completions occur while
  8989. * this function sleep. So, this function cannot be called from
  8990. * the thread which process iocb completion for this ring.
  8991. * This function clears the iocb_flag of the iocb object before
  8992. * issuing the iocb and the iocb completion handler sets this
  8993. * flag and wakes this thread when the iocb completes.
  8994. * The contents of the response iocb will be copied to prspiocbq
  8995. * by the completion handler when the command completes.
  8996. * This function returns IOCB_SUCCESS when success.
  8997. * This function is called with no lock held.
  8998. **/
  8999. int
  9000. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  9001. uint32_t ring_number,
  9002. struct lpfc_iocbq *piocb,
  9003. struct lpfc_iocbq *prspiocbq,
  9004. uint32_t timeout)
  9005. {
  9006. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9007. long timeleft, timeout_req = 0;
  9008. int retval = IOCB_SUCCESS;
  9009. uint32_t creg_val;
  9010. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9011. /*
  9012. * If the caller has provided a response iocbq buffer, then context2
  9013. * is NULL or its an error.
  9014. */
  9015. if (prspiocbq) {
  9016. if (piocb->context2)
  9017. return IOCB_ERROR;
  9018. piocb->context2 = prspiocbq;
  9019. }
  9020. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  9021. piocb->context_un.wait_queue = &done_q;
  9022. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  9023. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9024. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9025. return IOCB_ERROR;
  9026. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  9027. writel(creg_val, phba->HCregaddr);
  9028. readl(phba->HCregaddr); /* flush */
  9029. }
  9030. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  9031. SLI_IOCB_RET_IOCB);
  9032. if (retval == IOCB_SUCCESS) {
  9033. timeout_req = timeout * HZ;
  9034. timeleft = wait_event_timeout(done_q,
  9035. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  9036. timeout_req);
  9037. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  9038. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9039. "0331 IOCB wake signaled\n");
  9040. } else if (timeleft == 0) {
  9041. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9042. "0338 IOCB wait timeout error - no "
  9043. "wake response Data x%x\n", timeout);
  9044. retval = IOCB_TIMEDOUT;
  9045. } else {
  9046. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9047. "0330 IOCB wake NOT set, "
  9048. "Data x%x x%lx\n",
  9049. timeout, (timeleft / jiffies));
  9050. retval = IOCB_TIMEDOUT;
  9051. }
  9052. } else if (retval == IOCB_BUSY) {
  9053. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9054. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  9055. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  9056. return retval;
  9057. } else {
  9058. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9059. "0332 IOCB wait issue failed, Data x%x\n",
  9060. retval);
  9061. retval = IOCB_ERROR;
  9062. }
  9063. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9064. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9065. return IOCB_ERROR;
  9066. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  9067. writel(creg_val, phba->HCregaddr);
  9068. readl(phba->HCregaddr); /* flush */
  9069. }
  9070. if (prspiocbq)
  9071. piocb->context2 = NULL;
  9072. piocb->context_un.wait_queue = NULL;
  9073. piocb->iocb_cmpl = NULL;
  9074. return retval;
  9075. }
  9076. /**
  9077. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  9078. * @phba: Pointer to HBA context object.
  9079. * @pmboxq: Pointer to driver mailbox object.
  9080. * @timeout: Timeout in number of seconds.
  9081. *
  9082. * This function issues the mailbox to firmware and waits for the
  9083. * mailbox command to complete. If the mailbox command is not
  9084. * completed within timeout seconds, it returns MBX_TIMEOUT.
  9085. * The function waits for the mailbox completion using an
  9086. * interruptible wait. If the thread is woken up due to a
  9087. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  9088. * should not free the mailbox resources, if this function returns
  9089. * MBX_TIMEOUT.
  9090. * This function will sleep while waiting for mailbox completion.
  9091. * So, this function should not be called from any context which
  9092. * does not allow sleeping. Due to the same reason, this function
  9093. * cannot be called with interrupt disabled.
  9094. * This function assumes that the mailbox completion occurs while
  9095. * this function sleep. So, this function cannot be called from
  9096. * the worker thread which processes mailbox completion.
  9097. * This function is called in the context of HBA management
  9098. * applications.
  9099. * This function returns MBX_SUCCESS when successful.
  9100. * This function is called with no lock held.
  9101. **/
  9102. int
  9103. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  9104. uint32_t timeout)
  9105. {
  9106. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9107. int retval;
  9108. unsigned long flag;
  9109. /* The caller must leave context1 empty. */
  9110. if (pmboxq->context1)
  9111. return MBX_NOT_FINISHED;
  9112. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  9113. /* setup wake call as IOCB callback */
  9114. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  9115. /* setup context field to pass wait_queue pointer to wake function */
  9116. pmboxq->context1 = &done_q;
  9117. /* now issue the command */
  9118. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  9119. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  9120. wait_event_interruptible_timeout(done_q,
  9121. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  9122. timeout * HZ);
  9123. spin_lock_irqsave(&phba->hbalock, flag);
  9124. pmboxq->context1 = NULL;
  9125. /*
  9126. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  9127. * else do not free the resources.
  9128. */
  9129. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  9130. retval = MBX_SUCCESS;
  9131. lpfc_sli4_swap_str(phba, pmboxq);
  9132. } else {
  9133. retval = MBX_TIMEOUT;
  9134. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9135. }
  9136. spin_unlock_irqrestore(&phba->hbalock, flag);
  9137. }
  9138. return retval;
  9139. }
  9140. /**
  9141. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  9142. * @phba: Pointer to HBA context.
  9143. *
  9144. * This function is called to shutdown the driver's mailbox sub-system.
  9145. * It first marks the mailbox sub-system is in a block state to prevent
  9146. * the asynchronous mailbox command from issued off the pending mailbox
  9147. * command queue. If the mailbox command sub-system shutdown is due to
  9148. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  9149. * the mailbox sub-system flush routine to forcefully bring down the
  9150. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  9151. * as with offline or HBA function reset), this routine will wait for the
  9152. * outstanding mailbox command to complete before invoking the mailbox
  9153. * sub-system flush routine to gracefully bring down mailbox sub-system.
  9154. **/
  9155. void
  9156. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
  9157. {
  9158. struct lpfc_sli *psli = &phba->sli;
  9159. unsigned long timeout;
  9160. if (mbx_action == LPFC_MBX_NO_WAIT) {
  9161. /* delay 100ms for port state */
  9162. msleep(100);
  9163. lpfc_sli_mbox_sys_flush(phba);
  9164. return;
  9165. }
  9166. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  9167. spin_lock_irq(&phba->hbalock);
  9168. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  9169. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  9170. /* Determine how long we might wait for the active mailbox
  9171. * command to be gracefully completed by firmware.
  9172. */
  9173. if (phba->sli.mbox_active)
  9174. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  9175. phba->sli.mbox_active) *
  9176. 1000) + jiffies;
  9177. spin_unlock_irq(&phba->hbalock);
  9178. while (phba->sli.mbox_active) {
  9179. /* Check active mailbox complete status every 2ms */
  9180. msleep(2);
  9181. if (time_after(jiffies, timeout))
  9182. /* Timeout, let the mailbox flush routine to
  9183. * forcefully release active mailbox command
  9184. */
  9185. break;
  9186. }
  9187. } else
  9188. spin_unlock_irq(&phba->hbalock);
  9189. lpfc_sli_mbox_sys_flush(phba);
  9190. }
  9191. /**
  9192. * lpfc_sli_eratt_read - read sli-3 error attention events
  9193. * @phba: Pointer to HBA context.
  9194. *
  9195. * This function is called to read the SLI3 device error attention registers
  9196. * for possible error attention events. The caller must hold the hostlock
  9197. * with spin_lock_irq().
  9198. *
  9199. * This function returns 1 when there is Error Attention in the Host Attention
  9200. * Register and returns 0 otherwise.
  9201. **/
  9202. static int
  9203. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  9204. {
  9205. uint32_t ha_copy;
  9206. /* Read chip Host Attention (HA) register */
  9207. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9208. goto unplug_err;
  9209. if (ha_copy & HA_ERATT) {
  9210. /* Read host status register to retrieve error event */
  9211. if (lpfc_sli_read_hs(phba))
  9212. goto unplug_err;
  9213. /* Check if there is a deferred error condition is active */
  9214. if ((HS_FFER1 & phba->work_hs) &&
  9215. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9216. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9217. phba->hba_flag |= DEFER_ERATT;
  9218. /* Clear all interrupt enable conditions */
  9219. writel(0, phba->HCregaddr);
  9220. readl(phba->HCregaddr);
  9221. }
  9222. /* Set the driver HA work bitmap */
  9223. phba->work_ha |= HA_ERATT;
  9224. /* Indicate polling handles this ERATT */
  9225. phba->hba_flag |= HBA_ERATT_HANDLED;
  9226. return 1;
  9227. }
  9228. return 0;
  9229. unplug_err:
  9230. /* Set the driver HS work bitmap */
  9231. phba->work_hs |= UNPLUG_ERR;
  9232. /* Set the driver HA work bitmap */
  9233. phba->work_ha |= HA_ERATT;
  9234. /* Indicate polling handles this ERATT */
  9235. phba->hba_flag |= HBA_ERATT_HANDLED;
  9236. return 1;
  9237. }
  9238. /**
  9239. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9240. * @phba: Pointer to HBA context.
  9241. *
  9242. * This function is called to read the SLI4 device error attention registers
  9243. * for possible error attention events. The caller must hold the hostlock
  9244. * with spin_lock_irq().
  9245. *
  9246. * This function returns 1 when there is Error Attention in the Host Attention
  9247. * Register and returns 0 otherwise.
  9248. **/
  9249. static int
  9250. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9251. {
  9252. uint32_t uerr_sta_hi, uerr_sta_lo;
  9253. uint32_t if_type, portsmphr;
  9254. struct lpfc_register portstat_reg;
  9255. /*
  9256. * For now, use the SLI4 device internal unrecoverable error
  9257. * registers for error attention. This can be changed later.
  9258. */
  9259. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9260. switch (if_type) {
  9261. case LPFC_SLI_INTF_IF_TYPE_0:
  9262. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9263. &uerr_sta_lo) ||
  9264. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9265. &uerr_sta_hi)) {
  9266. phba->work_hs |= UNPLUG_ERR;
  9267. phba->work_ha |= HA_ERATT;
  9268. phba->hba_flag |= HBA_ERATT_HANDLED;
  9269. return 1;
  9270. }
  9271. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9272. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9273. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9274. "1423 HBA Unrecoverable error: "
  9275. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9276. "ue_mask_lo_reg=0x%x, "
  9277. "ue_mask_hi_reg=0x%x\n",
  9278. uerr_sta_lo, uerr_sta_hi,
  9279. phba->sli4_hba.ue_mask_lo,
  9280. phba->sli4_hba.ue_mask_hi);
  9281. phba->work_status[0] = uerr_sta_lo;
  9282. phba->work_status[1] = uerr_sta_hi;
  9283. phba->work_ha |= HA_ERATT;
  9284. phba->hba_flag |= HBA_ERATT_HANDLED;
  9285. return 1;
  9286. }
  9287. break;
  9288. case LPFC_SLI_INTF_IF_TYPE_2:
  9289. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9290. &portstat_reg.word0) ||
  9291. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9292. &portsmphr)){
  9293. phba->work_hs |= UNPLUG_ERR;
  9294. phba->work_ha |= HA_ERATT;
  9295. phba->hba_flag |= HBA_ERATT_HANDLED;
  9296. return 1;
  9297. }
  9298. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9299. phba->work_status[0] =
  9300. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9301. phba->work_status[1] =
  9302. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9303. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9304. "2885 Port Status Event: "
  9305. "port status reg 0x%x, "
  9306. "port smphr reg 0x%x, "
  9307. "error 1=0x%x, error 2=0x%x\n",
  9308. portstat_reg.word0,
  9309. portsmphr,
  9310. phba->work_status[0],
  9311. phba->work_status[1]);
  9312. phba->work_ha |= HA_ERATT;
  9313. phba->hba_flag |= HBA_ERATT_HANDLED;
  9314. return 1;
  9315. }
  9316. break;
  9317. case LPFC_SLI_INTF_IF_TYPE_1:
  9318. default:
  9319. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9320. "2886 HBA Error Attention on unsupported "
  9321. "if type %d.", if_type);
  9322. return 1;
  9323. }
  9324. return 0;
  9325. }
  9326. /**
  9327. * lpfc_sli_check_eratt - check error attention events
  9328. * @phba: Pointer to HBA context.
  9329. *
  9330. * This function is called from timer soft interrupt context to check HBA's
  9331. * error attention register bit for error attention events.
  9332. *
  9333. * This function returns 1 when there is Error Attention in the Host Attention
  9334. * Register and returns 0 otherwise.
  9335. **/
  9336. int
  9337. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9338. {
  9339. uint32_t ha_copy;
  9340. /* If somebody is waiting to handle an eratt, don't process it
  9341. * here. The brdkill function will do this.
  9342. */
  9343. if (phba->link_flag & LS_IGNORE_ERATT)
  9344. return 0;
  9345. /* Check if interrupt handler handles this ERATT */
  9346. spin_lock_irq(&phba->hbalock);
  9347. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9348. /* Interrupt handler has handled ERATT */
  9349. spin_unlock_irq(&phba->hbalock);
  9350. return 0;
  9351. }
  9352. /*
  9353. * If there is deferred error attention, do not check for error
  9354. * attention
  9355. */
  9356. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9357. spin_unlock_irq(&phba->hbalock);
  9358. return 0;
  9359. }
  9360. /* If PCI channel is offline, don't process it */
  9361. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9362. spin_unlock_irq(&phba->hbalock);
  9363. return 0;
  9364. }
  9365. switch (phba->sli_rev) {
  9366. case LPFC_SLI_REV2:
  9367. case LPFC_SLI_REV3:
  9368. /* Read chip Host Attention (HA) register */
  9369. ha_copy = lpfc_sli_eratt_read(phba);
  9370. break;
  9371. case LPFC_SLI_REV4:
  9372. /* Read device Uncoverable Error (UERR) registers */
  9373. ha_copy = lpfc_sli4_eratt_read(phba);
  9374. break;
  9375. default:
  9376. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9377. "0299 Invalid SLI revision (%d)\n",
  9378. phba->sli_rev);
  9379. ha_copy = 0;
  9380. break;
  9381. }
  9382. spin_unlock_irq(&phba->hbalock);
  9383. return ha_copy;
  9384. }
  9385. /**
  9386. * lpfc_intr_state_check - Check device state for interrupt handling
  9387. * @phba: Pointer to HBA context.
  9388. *
  9389. * This inline routine checks whether a device or its PCI slot is in a state
  9390. * that the interrupt should be handled.
  9391. *
  9392. * This function returns 0 if the device or the PCI slot is in a state that
  9393. * interrupt should be handled, otherwise -EIO.
  9394. */
  9395. static inline int
  9396. lpfc_intr_state_check(struct lpfc_hba *phba)
  9397. {
  9398. /* If the pci channel is offline, ignore all the interrupts */
  9399. if (unlikely(pci_channel_offline(phba->pcidev)))
  9400. return -EIO;
  9401. /* Update device level interrupt statistics */
  9402. phba->sli.slistat.sli_intr++;
  9403. /* Ignore all interrupts during initialization. */
  9404. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9405. return -EIO;
  9406. return 0;
  9407. }
  9408. /**
  9409. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9410. * @irq: Interrupt number.
  9411. * @dev_id: The device context pointer.
  9412. *
  9413. * This function is directly called from the PCI layer as an interrupt
  9414. * service routine when device with SLI-3 interface spec is enabled with
  9415. * MSI-X multi-message interrupt mode and there are slow-path events in
  9416. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9417. * interrupt mode, this function is called as part of the device-level
  9418. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9419. * is undergoing initialization, the interrupt handler will not process
  9420. * the interrupt. The link attention and ELS ring attention events are
  9421. * handled by the worker thread. The interrupt handler signals the worker
  9422. * thread and returns for these events. This function is called without
  9423. * any lock held. It gets the hbalock to access and update SLI data
  9424. * structures.
  9425. *
  9426. * This function returns IRQ_HANDLED when interrupt is handled else it
  9427. * returns IRQ_NONE.
  9428. **/
  9429. irqreturn_t
  9430. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9431. {
  9432. struct lpfc_hba *phba;
  9433. uint32_t ha_copy, hc_copy;
  9434. uint32_t work_ha_copy;
  9435. unsigned long status;
  9436. unsigned long iflag;
  9437. uint32_t control;
  9438. MAILBOX_t *mbox, *pmbox;
  9439. struct lpfc_vport *vport;
  9440. struct lpfc_nodelist *ndlp;
  9441. struct lpfc_dmabuf *mp;
  9442. LPFC_MBOXQ_t *pmb;
  9443. int rc;
  9444. /*
  9445. * Get the driver's phba structure from the dev_id and
  9446. * assume the HBA is not interrupting.
  9447. */
  9448. phba = (struct lpfc_hba *)dev_id;
  9449. if (unlikely(!phba))
  9450. return IRQ_NONE;
  9451. /*
  9452. * Stuff needs to be attented to when this function is invoked as an
  9453. * individual interrupt handler in MSI-X multi-message interrupt mode
  9454. */
  9455. if (phba->intr_type == MSIX) {
  9456. /* Check device state for handling interrupt */
  9457. if (lpfc_intr_state_check(phba))
  9458. return IRQ_NONE;
  9459. /* Need to read HA REG for slow-path events */
  9460. spin_lock_irqsave(&phba->hbalock, iflag);
  9461. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9462. goto unplug_error;
  9463. /* If somebody is waiting to handle an eratt don't process it
  9464. * here. The brdkill function will do this.
  9465. */
  9466. if (phba->link_flag & LS_IGNORE_ERATT)
  9467. ha_copy &= ~HA_ERATT;
  9468. /* Check the need for handling ERATT in interrupt handler */
  9469. if (ha_copy & HA_ERATT) {
  9470. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9471. /* ERATT polling has handled ERATT */
  9472. ha_copy &= ~HA_ERATT;
  9473. else
  9474. /* Indicate interrupt handler handles ERATT */
  9475. phba->hba_flag |= HBA_ERATT_HANDLED;
  9476. }
  9477. /*
  9478. * If there is deferred error attention, do not check for any
  9479. * interrupt.
  9480. */
  9481. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9482. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9483. return IRQ_NONE;
  9484. }
  9485. /* Clear up only attention source related to slow-path */
  9486. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9487. goto unplug_error;
  9488. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9489. HC_LAINT_ENA | HC_ERINT_ENA),
  9490. phba->HCregaddr);
  9491. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9492. phba->HAregaddr);
  9493. writel(hc_copy, phba->HCregaddr);
  9494. readl(phba->HAregaddr); /* flush */
  9495. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9496. } else
  9497. ha_copy = phba->ha_copy;
  9498. work_ha_copy = ha_copy & phba->work_ha_mask;
  9499. if (work_ha_copy) {
  9500. if (work_ha_copy & HA_LATT) {
  9501. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9502. /*
  9503. * Turn off Link Attention interrupts
  9504. * until CLEAR_LA done
  9505. */
  9506. spin_lock_irqsave(&phba->hbalock, iflag);
  9507. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9508. if (lpfc_readl(phba->HCregaddr, &control))
  9509. goto unplug_error;
  9510. control &= ~HC_LAINT_ENA;
  9511. writel(control, phba->HCregaddr);
  9512. readl(phba->HCregaddr); /* flush */
  9513. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9514. }
  9515. else
  9516. work_ha_copy &= ~HA_LATT;
  9517. }
  9518. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9519. /*
  9520. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9521. * the only slow ring.
  9522. */
  9523. status = (work_ha_copy &
  9524. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9525. status >>= (4*LPFC_ELS_RING);
  9526. if (status & HA_RXMASK) {
  9527. spin_lock_irqsave(&phba->hbalock, iflag);
  9528. if (lpfc_readl(phba->HCregaddr, &control))
  9529. goto unplug_error;
  9530. lpfc_debugfs_slow_ring_trc(phba,
  9531. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9532. control, status,
  9533. (uint32_t)phba->sli.slistat.sli_intr);
  9534. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9535. lpfc_debugfs_slow_ring_trc(phba,
  9536. "ISR Disable ring:"
  9537. "pwork:x%x hawork:x%x wait:x%x",
  9538. phba->work_ha, work_ha_copy,
  9539. (uint32_t)((unsigned long)
  9540. &phba->work_waitq));
  9541. control &=
  9542. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9543. writel(control, phba->HCregaddr);
  9544. readl(phba->HCregaddr); /* flush */
  9545. }
  9546. else {
  9547. lpfc_debugfs_slow_ring_trc(phba,
  9548. "ISR slow ring: pwork:"
  9549. "x%x hawork:x%x wait:x%x",
  9550. phba->work_ha, work_ha_copy,
  9551. (uint32_t)((unsigned long)
  9552. &phba->work_waitq));
  9553. }
  9554. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9555. }
  9556. }
  9557. spin_lock_irqsave(&phba->hbalock, iflag);
  9558. if (work_ha_copy & HA_ERATT) {
  9559. if (lpfc_sli_read_hs(phba))
  9560. goto unplug_error;
  9561. /*
  9562. * Check if there is a deferred error condition
  9563. * is active
  9564. */
  9565. if ((HS_FFER1 & phba->work_hs) &&
  9566. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9567. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9568. phba->work_hs)) {
  9569. phba->hba_flag |= DEFER_ERATT;
  9570. /* Clear all interrupt enable conditions */
  9571. writel(0, phba->HCregaddr);
  9572. readl(phba->HCregaddr);
  9573. }
  9574. }
  9575. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9576. pmb = phba->sli.mbox_active;
  9577. pmbox = &pmb->u.mb;
  9578. mbox = phba->mbox;
  9579. vport = pmb->vport;
  9580. /* First check out the status word */
  9581. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9582. if (pmbox->mbxOwner != OWN_HOST) {
  9583. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9584. /*
  9585. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9586. * mbxStatus <status>
  9587. */
  9588. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9589. LOG_SLI,
  9590. "(%d):0304 Stray Mailbox "
  9591. "Interrupt mbxCommand x%x "
  9592. "mbxStatus x%x\n",
  9593. (vport ? vport->vpi : 0),
  9594. pmbox->mbxCommand,
  9595. pmbox->mbxStatus);
  9596. /* clear mailbox attention bit */
  9597. work_ha_copy &= ~HA_MBATT;
  9598. } else {
  9599. phba->sli.mbox_active = NULL;
  9600. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9601. phba->last_completion_time = jiffies;
  9602. del_timer(&phba->sli.mbox_tmo);
  9603. if (pmb->mbox_cmpl) {
  9604. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9605. MAILBOX_CMD_SIZE);
  9606. if (pmb->out_ext_byte_len &&
  9607. pmb->context2)
  9608. lpfc_sli_pcimem_bcopy(
  9609. phba->mbox_ext,
  9610. pmb->context2,
  9611. pmb->out_ext_byte_len);
  9612. }
  9613. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9614. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9615. lpfc_debugfs_disc_trc(vport,
  9616. LPFC_DISC_TRC_MBOX_VPORT,
  9617. "MBOX dflt rpi: : "
  9618. "status:x%x rpi:x%x",
  9619. (uint32_t)pmbox->mbxStatus,
  9620. pmbox->un.varWords[0], 0);
  9621. if (!pmbox->mbxStatus) {
  9622. mp = (struct lpfc_dmabuf *)
  9623. (pmb->context1);
  9624. ndlp = (struct lpfc_nodelist *)
  9625. pmb->context2;
  9626. /* Reg_LOGIN of dflt RPI was
  9627. * successful. new lets get
  9628. * rid of the RPI using the
  9629. * same mbox buffer.
  9630. */
  9631. lpfc_unreg_login(phba,
  9632. vport->vpi,
  9633. pmbox->un.varWords[0],
  9634. pmb);
  9635. pmb->mbox_cmpl =
  9636. lpfc_mbx_cmpl_dflt_rpi;
  9637. pmb->context1 = mp;
  9638. pmb->context2 = ndlp;
  9639. pmb->vport = vport;
  9640. rc = lpfc_sli_issue_mbox(phba,
  9641. pmb,
  9642. MBX_NOWAIT);
  9643. if (rc != MBX_BUSY)
  9644. lpfc_printf_log(phba,
  9645. KERN_ERR,
  9646. LOG_MBOX | LOG_SLI,
  9647. "0350 rc should have"
  9648. "been MBX_BUSY\n");
  9649. if (rc != MBX_NOT_FINISHED)
  9650. goto send_current_mbox;
  9651. }
  9652. }
  9653. spin_lock_irqsave(
  9654. &phba->pport->work_port_lock,
  9655. iflag);
  9656. phba->pport->work_port_events &=
  9657. ~WORKER_MBOX_TMO;
  9658. spin_unlock_irqrestore(
  9659. &phba->pport->work_port_lock,
  9660. iflag);
  9661. lpfc_mbox_cmpl_put(phba, pmb);
  9662. }
  9663. } else
  9664. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9665. if ((work_ha_copy & HA_MBATT) &&
  9666. (phba->sli.mbox_active == NULL)) {
  9667. send_current_mbox:
  9668. /* Process next mailbox command if there is one */
  9669. do {
  9670. rc = lpfc_sli_issue_mbox(phba, NULL,
  9671. MBX_NOWAIT);
  9672. } while (rc == MBX_NOT_FINISHED);
  9673. if (rc != MBX_SUCCESS)
  9674. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9675. LOG_SLI, "0349 rc should be "
  9676. "MBX_SUCCESS\n");
  9677. }
  9678. spin_lock_irqsave(&phba->hbalock, iflag);
  9679. phba->work_ha |= work_ha_copy;
  9680. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9681. lpfc_worker_wake_up(phba);
  9682. }
  9683. return IRQ_HANDLED;
  9684. unplug_error:
  9685. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9686. return IRQ_HANDLED;
  9687. } /* lpfc_sli_sp_intr_handler */
  9688. /**
  9689. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9690. * @irq: Interrupt number.
  9691. * @dev_id: The device context pointer.
  9692. *
  9693. * This function is directly called from the PCI layer as an interrupt
  9694. * service routine when device with SLI-3 interface spec is enabled with
  9695. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9696. * ring event in the HBA. However, when the device is enabled with either
  9697. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9698. * device-level interrupt handler. When the PCI slot is in error recovery
  9699. * or the HBA is undergoing initialization, the interrupt handler will not
  9700. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9701. * the intrrupt context. This function is called without any lock held.
  9702. * It gets the hbalock to access and update SLI data structures.
  9703. *
  9704. * This function returns IRQ_HANDLED when interrupt is handled else it
  9705. * returns IRQ_NONE.
  9706. **/
  9707. irqreturn_t
  9708. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9709. {
  9710. struct lpfc_hba *phba;
  9711. uint32_t ha_copy;
  9712. unsigned long status;
  9713. unsigned long iflag;
  9714. /* Get the driver's phba structure from the dev_id and
  9715. * assume the HBA is not interrupting.
  9716. */
  9717. phba = (struct lpfc_hba *) dev_id;
  9718. if (unlikely(!phba))
  9719. return IRQ_NONE;
  9720. /*
  9721. * Stuff needs to be attented to when this function is invoked as an
  9722. * individual interrupt handler in MSI-X multi-message interrupt mode
  9723. */
  9724. if (phba->intr_type == MSIX) {
  9725. /* Check device state for handling interrupt */
  9726. if (lpfc_intr_state_check(phba))
  9727. return IRQ_NONE;
  9728. /* Need to read HA REG for FCP ring and other ring events */
  9729. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9730. return IRQ_HANDLED;
  9731. /* Clear up only attention source related to fast-path */
  9732. spin_lock_irqsave(&phba->hbalock, iflag);
  9733. /*
  9734. * If there is deferred error attention, do not check for
  9735. * any interrupt.
  9736. */
  9737. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9738. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9739. return IRQ_NONE;
  9740. }
  9741. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9742. phba->HAregaddr);
  9743. readl(phba->HAregaddr); /* flush */
  9744. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9745. } else
  9746. ha_copy = phba->ha_copy;
  9747. /*
  9748. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9749. */
  9750. ha_copy &= ~(phba->work_ha_mask);
  9751. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9752. status >>= (4*LPFC_FCP_RING);
  9753. if (status & HA_RXMASK)
  9754. lpfc_sli_handle_fast_ring_event(phba,
  9755. &phba->sli.ring[LPFC_FCP_RING],
  9756. status);
  9757. if (phba->cfg_multi_ring_support == 2) {
  9758. /*
  9759. * Process all events on extra ring. Take the optimized path
  9760. * for extra ring IO.
  9761. */
  9762. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9763. status >>= (4*LPFC_EXTRA_RING);
  9764. if (status & HA_RXMASK) {
  9765. lpfc_sli_handle_fast_ring_event(phba,
  9766. &phba->sli.ring[LPFC_EXTRA_RING],
  9767. status);
  9768. }
  9769. }
  9770. return IRQ_HANDLED;
  9771. } /* lpfc_sli_fp_intr_handler */
  9772. /**
  9773. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9774. * @irq: Interrupt number.
  9775. * @dev_id: The device context pointer.
  9776. *
  9777. * This function is the HBA device-level interrupt handler to device with
  9778. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9779. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9780. * requires driver attention. This function invokes the slow-path interrupt
  9781. * attention handling function and fast-path interrupt attention handling
  9782. * function in turn to process the relevant HBA attention events. This
  9783. * function is called without any lock held. It gets the hbalock to access
  9784. * and update SLI data structures.
  9785. *
  9786. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9787. * returns IRQ_NONE.
  9788. **/
  9789. irqreturn_t
  9790. lpfc_sli_intr_handler(int irq, void *dev_id)
  9791. {
  9792. struct lpfc_hba *phba;
  9793. irqreturn_t sp_irq_rc, fp_irq_rc;
  9794. unsigned long status1, status2;
  9795. uint32_t hc_copy;
  9796. /*
  9797. * Get the driver's phba structure from the dev_id and
  9798. * assume the HBA is not interrupting.
  9799. */
  9800. phba = (struct lpfc_hba *) dev_id;
  9801. if (unlikely(!phba))
  9802. return IRQ_NONE;
  9803. /* Check device state for handling interrupt */
  9804. if (lpfc_intr_state_check(phba))
  9805. return IRQ_NONE;
  9806. spin_lock(&phba->hbalock);
  9807. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9808. spin_unlock(&phba->hbalock);
  9809. return IRQ_HANDLED;
  9810. }
  9811. if (unlikely(!phba->ha_copy)) {
  9812. spin_unlock(&phba->hbalock);
  9813. return IRQ_NONE;
  9814. } else if (phba->ha_copy & HA_ERATT) {
  9815. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9816. /* ERATT polling has handled ERATT */
  9817. phba->ha_copy &= ~HA_ERATT;
  9818. else
  9819. /* Indicate interrupt handler handles ERATT */
  9820. phba->hba_flag |= HBA_ERATT_HANDLED;
  9821. }
  9822. /*
  9823. * If there is deferred error attention, do not check for any interrupt.
  9824. */
  9825. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9826. spin_unlock(&phba->hbalock);
  9827. return IRQ_NONE;
  9828. }
  9829. /* Clear attention sources except link and error attentions */
  9830. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9831. spin_unlock(&phba->hbalock);
  9832. return IRQ_HANDLED;
  9833. }
  9834. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9835. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9836. phba->HCregaddr);
  9837. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9838. writel(hc_copy, phba->HCregaddr);
  9839. readl(phba->HAregaddr); /* flush */
  9840. spin_unlock(&phba->hbalock);
  9841. /*
  9842. * Invokes slow-path host attention interrupt handling as appropriate.
  9843. */
  9844. /* status of events with mailbox and link attention */
  9845. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9846. /* status of events with ELS ring */
  9847. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9848. status2 >>= (4*LPFC_ELS_RING);
  9849. if (status1 || (status2 & HA_RXMASK))
  9850. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9851. else
  9852. sp_irq_rc = IRQ_NONE;
  9853. /*
  9854. * Invoke fast-path host attention interrupt handling as appropriate.
  9855. */
  9856. /* status of events with FCP ring */
  9857. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9858. status1 >>= (4*LPFC_FCP_RING);
  9859. /* status of events with extra ring */
  9860. if (phba->cfg_multi_ring_support == 2) {
  9861. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9862. status2 >>= (4*LPFC_EXTRA_RING);
  9863. } else
  9864. status2 = 0;
  9865. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9866. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9867. else
  9868. fp_irq_rc = IRQ_NONE;
  9869. /* Return device-level interrupt handling status */
  9870. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9871. } /* lpfc_sli_intr_handler */
  9872. /**
  9873. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9874. * @phba: pointer to lpfc hba data structure.
  9875. *
  9876. * This routine is invoked by the worker thread to process all the pending
  9877. * SLI4 FCP abort XRI events.
  9878. **/
  9879. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9880. {
  9881. struct lpfc_cq_event *cq_event;
  9882. /* First, declare the fcp xri abort event has been handled */
  9883. spin_lock_irq(&phba->hbalock);
  9884. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9885. spin_unlock_irq(&phba->hbalock);
  9886. /* Now, handle all the fcp xri abort events */
  9887. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9888. /* Get the first event from the head of the event queue */
  9889. spin_lock_irq(&phba->hbalock);
  9890. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9891. cq_event, struct lpfc_cq_event, list);
  9892. spin_unlock_irq(&phba->hbalock);
  9893. /* Notify aborted XRI for FCP work queue */
  9894. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9895. /* Free the event processed back to the free pool */
  9896. lpfc_sli4_cq_event_release(phba, cq_event);
  9897. }
  9898. }
  9899. /**
  9900. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9901. * @phba: pointer to lpfc hba data structure.
  9902. *
  9903. * This routine is invoked by the worker thread to process all the pending
  9904. * SLI4 els abort xri events.
  9905. **/
  9906. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9907. {
  9908. struct lpfc_cq_event *cq_event;
  9909. /* First, declare the els xri abort event has been handled */
  9910. spin_lock_irq(&phba->hbalock);
  9911. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9912. spin_unlock_irq(&phba->hbalock);
  9913. /* Now, handle all the els xri abort events */
  9914. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9915. /* Get the first event from the head of the event queue */
  9916. spin_lock_irq(&phba->hbalock);
  9917. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9918. cq_event, struct lpfc_cq_event, list);
  9919. spin_unlock_irq(&phba->hbalock);
  9920. /* Notify aborted XRI for ELS work queue */
  9921. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9922. /* Free the event processed back to the free pool */
  9923. lpfc_sli4_cq_event_release(phba, cq_event);
  9924. }
  9925. }
  9926. /**
  9927. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9928. * @phba: pointer to lpfc hba data structure
  9929. * @pIocbIn: pointer to the rspiocbq
  9930. * @pIocbOut: pointer to the cmdiocbq
  9931. * @wcqe: pointer to the complete wcqe
  9932. *
  9933. * This routine transfers the fields of a command iocbq to a response iocbq
  9934. * by copying all the IOCB fields from command iocbq and transferring the
  9935. * completion status information from the complete wcqe.
  9936. **/
  9937. static void
  9938. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  9939. struct lpfc_iocbq *pIocbIn,
  9940. struct lpfc_iocbq *pIocbOut,
  9941. struct lpfc_wcqe_complete *wcqe)
  9942. {
  9943. unsigned long iflags;
  9944. uint32_t status;
  9945. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  9946. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  9947. sizeof(struct lpfc_iocbq) - offset);
  9948. /* Map WCQE parameters into irspiocb parameters */
  9949. status = bf_get(lpfc_wcqe_c_status, wcqe);
  9950. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  9951. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  9952. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  9953. pIocbIn->iocb.un.fcpi.fcpi_parm =
  9954. pIocbOut->iocb.un.fcpi.fcpi_parm -
  9955. wcqe->total_data_placed;
  9956. else
  9957. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9958. else {
  9959. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9960. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  9961. }
  9962. /* Convert BG errors for completion status */
  9963. if (status == CQE_STATUS_DI_ERROR) {
  9964. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  9965. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  9966. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  9967. else
  9968. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  9969. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  9970. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  9971. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9972. BGS_GUARD_ERR_MASK;
  9973. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  9974. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9975. BGS_APPTAG_ERR_MASK;
  9976. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  9977. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9978. BGS_REFTAG_ERR_MASK;
  9979. /* Check to see if there was any good data before the error */
  9980. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  9981. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9982. BGS_HI_WATER_MARK_PRESENT_MASK;
  9983. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  9984. wcqe->total_data_placed;
  9985. }
  9986. /*
  9987. * Set ALL the error bits to indicate we don't know what
  9988. * type of error it is.
  9989. */
  9990. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  9991. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9992. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  9993. BGS_GUARD_ERR_MASK);
  9994. }
  9995. /* Pick up HBA exchange busy condition */
  9996. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  9997. spin_lock_irqsave(&phba->hbalock, iflags);
  9998. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  9999. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10000. }
  10001. }
  10002. /**
  10003. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  10004. * @phba: Pointer to HBA context object.
  10005. * @wcqe: Pointer to work-queue completion queue entry.
  10006. *
  10007. * This routine handles an ELS work-queue completion event and construct
  10008. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  10009. * discovery engine to handle.
  10010. *
  10011. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  10012. **/
  10013. static struct lpfc_iocbq *
  10014. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  10015. struct lpfc_iocbq *irspiocbq)
  10016. {
  10017. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  10018. struct lpfc_iocbq *cmdiocbq;
  10019. struct lpfc_wcqe_complete *wcqe;
  10020. unsigned long iflags;
  10021. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  10022. spin_lock_irqsave(&pring->ring_lock, iflags);
  10023. pring->stats.iocb_event++;
  10024. /* Look up the ELS command IOCB and create pseudo response IOCB */
  10025. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10026. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10027. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10028. if (unlikely(!cmdiocbq)) {
  10029. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10030. "0386 ELS complete with no corresponding "
  10031. "cmdiocb: iotag (%d)\n",
  10032. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10033. lpfc_sli_release_iocbq(phba, irspiocbq);
  10034. return NULL;
  10035. }
  10036. /* Fake the irspiocbq and copy necessary response information */
  10037. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  10038. return irspiocbq;
  10039. }
  10040. /**
  10041. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  10042. * @phba: Pointer to HBA context object.
  10043. * @cqe: Pointer to mailbox completion queue entry.
  10044. *
  10045. * This routine process a mailbox completion queue entry with asynchrous
  10046. * event.
  10047. *
  10048. * Return: true if work posted to worker thread, otherwise false.
  10049. **/
  10050. static bool
  10051. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10052. {
  10053. struct lpfc_cq_event *cq_event;
  10054. unsigned long iflags;
  10055. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10056. "0392 Async Event: word0:x%x, word1:x%x, "
  10057. "word2:x%x, word3:x%x\n", mcqe->word0,
  10058. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  10059. /* Allocate a new internal CQ_EVENT entry */
  10060. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10061. if (!cq_event) {
  10062. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10063. "0394 Failed to allocate CQ_EVENT entry\n");
  10064. return false;
  10065. }
  10066. /* Move the CQE into an asynchronous event entry */
  10067. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  10068. spin_lock_irqsave(&phba->hbalock, iflags);
  10069. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  10070. /* Set the async event flag */
  10071. phba->hba_flag |= ASYNC_EVENT;
  10072. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10073. return true;
  10074. }
  10075. /**
  10076. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  10077. * @phba: Pointer to HBA context object.
  10078. * @cqe: Pointer to mailbox completion queue entry.
  10079. *
  10080. * This routine process a mailbox completion queue entry with mailbox
  10081. * completion event.
  10082. *
  10083. * Return: true if work posted to worker thread, otherwise false.
  10084. **/
  10085. static bool
  10086. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10087. {
  10088. uint32_t mcqe_status;
  10089. MAILBOX_t *mbox, *pmbox;
  10090. struct lpfc_mqe *mqe;
  10091. struct lpfc_vport *vport;
  10092. struct lpfc_nodelist *ndlp;
  10093. struct lpfc_dmabuf *mp;
  10094. unsigned long iflags;
  10095. LPFC_MBOXQ_t *pmb;
  10096. bool workposted = false;
  10097. int rc;
  10098. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  10099. if (!bf_get(lpfc_trailer_completed, mcqe))
  10100. goto out_no_mqe_complete;
  10101. /* Get the reference to the active mbox command */
  10102. spin_lock_irqsave(&phba->hbalock, iflags);
  10103. pmb = phba->sli.mbox_active;
  10104. if (unlikely(!pmb)) {
  10105. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10106. "1832 No pending MBOX command to handle\n");
  10107. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10108. goto out_no_mqe_complete;
  10109. }
  10110. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10111. mqe = &pmb->u.mqe;
  10112. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  10113. mbox = phba->mbox;
  10114. vport = pmb->vport;
  10115. /* Reset heartbeat timer */
  10116. phba->last_completion_time = jiffies;
  10117. del_timer(&phba->sli.mbox_tmo);
  10118. /* Move mbox data to caller's mailbox region, do endian swapping */
  10119. if (pmb->mbox_cmpl && mbox)
  10120. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  10121. /*
  10122. * For mcqe errors, conditionally move a modified error code to
  10123. * the mbox so that the error will not be missed.
  10124. */
  10125. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  10126. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  10127. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  10128. bf_set(lpfc_mqe_status, mqe,
  10129. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  10130. }
  10131. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10132. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10133. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  10134. "MBOX dflt rpi: status:x%x rpi:x%x",
  10135. mcqe_status,
  10136. pmbox->un.varWords[0], 0);
  10137. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  10138. mp = (struct lpfc_dmabuf *)(pmb->context1);
  10139. ndlp = (struct lpfc_nodelist *)pmb->context2;
  10140. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  10141. * RID of the PPI using the same mbox buffer.
  10142. */
  10143. lpfc_unreg_login(phba, vport->vpi,
  10144. pmbox->un.varWords[0], pmb);
  10145. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  10146. pmb->context1 = mp;
  10147. pmb->context2 = ndlp;
  10148. pmb->vport = vport;
  10149. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  10150. if (rc != MBX_BUSY)
  10151. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10152. LOG_SLI, "0385 rc should "
  10153. "have been MBX_BUSY\n");
  10154. if (rc != MBX_NOT_FINISHED)
  10155. goto send_current_mbox;
  10156. }
  10157. }
  10158. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  10159. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  10160. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  10161. /* There is mailbox completion work to do */
  10162. spin_lock_irqsave(&phba->hbalock, iflags);
  10163. __lpfc_mbox_cmpl_put(phba, pmb);
  10164. phba->work_ha |= HA_MBATT;
  10165. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10166. workposted = true;
  10167. send_current_mbox:
  10168. spin_lock_irqsave(&phba->hbalock, iflags);
  10169. /* Release the mailbox command posting token */
  10170. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10171. /* Setting active mailbox pointer need to be in sync to flag clear */
  10172. phba->sli.mbox_active = NULL;
  10173. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10174. /* Wake up worker thread to post the next pending mailbox command */
  10175. lpfc_worker_wake_up(phba);
  10176. out_no_mqe_complete:
  10177. if (bf_get(lpfc_trailer_consumed, mcqe))
  10178. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  10179. return workposted;
  10180. }
  10181. /**
  10182. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  10183. * @phba: Pointer to HBA context object.
  10184. * @cqe: Pointer to mailbox completion queue entry.
  10185. *
  10186. * This routine process a mailbox completion queue entry, it invokes the
  10187. * proper mailbox complete handling or asynchrous event handling routine
  10188. * according to the MCQE's async bit.
  10189. *
  10190. * Return: true if work posted to worker thread, otherwise false.
  10191. **/
  10192. static bool
  10193. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  10194. {
  10195. struct lpfc_mcqe mcqe;
  10196. bool workposted;
  10197. /* Copy the mailbox MCQE and convert endian order as needed */
  10198. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  10199. /* Invoke the proper event handling routine */
  10200. if (!bf_get(lpfc_trailer_async, &mcqe))
  10201. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  10202. else
  10203. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  10204. return workposted;
  10205. }
  10206. /**
  10207. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  10208. * @phba: Pointer to HBA context object.
  10209. * @cq: Pointer to associated CQ
  10210. * @wcqe: Pointer to work-queue completion queue entry.
  10211. *
  10212. * This routine handles an ELS work-queue completion event.
  10213. *
  10214. * Return: true if work posted to worker thread, otherwise false.
  10215. **/
  10216. static bool
  10217. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10218. struct lpfc_wcqe_complete *wcqe)
  10219. {
  10220. struct lpfc_iocbq *irspiocbq;
  10221. unsigned long iflags;
  10222. struct lpfc_sli_ring *pring = cq->pring;
  10223. /* Get an irspiocbq for later ELS response processing use */
  10224. irspiocbq = lpfc_sli_get_iocbq(phba);
  10225. if (!irspiocbq) {
  10226. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10227. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10228. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10229. pring->txq_cnt, phba->iocb_cnt,
  10230. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  10231. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  10232. return false;
  10233. }
  10234. /* Save off the slow-path queue event for work thread to process */
  10235. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10236. spin_lock_irqsave(&phba->hbalock, iflags);
  10237. list_add_tail(&irspiocbq->cq_event.list,
  10238. &phba->sli4_hba.sp_queue_event);
  10239. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10240. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10241. return true;
  10242. }
  10243. /**
  10244. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10245. * @phba: Pointer to HBA context object.
  10246. * @wcqe: Pointer to work-queue completion queue entry.
  10247. *
  10248. * This routine handles slow-path WQ entry comsumed event by invoking the
  10249. * proper WQ release routine to the slow-path WQ.
  10250. **/
  10251. static void
  10252. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10253. struct lpfc_wcqe_release *wcqe)
  10254. {
  10255. /* sanity check on queue memory */
  10256. if (unlikely(!phba->sli4_hba.els_wq))
  10257. return;
  10258. /* Check for the slow-path ELS work queue */
  10259. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10260. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10261. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10262. else
  10263. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10264. "2579 Slow-path wqe consume event carries "
  10265. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10266. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10267. phba->sli4_hba.els_wq->queue_id);
  10268. }
  10269. /**
  10270. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10271. * @phba: Pointer to HBA context object.
  10272. * @cq: Pointer to a WQ completion queue.
  10273. * @wcqe: Pointer to work-queue completion queue entry.
  10274. *
  10275. * This routine handles an XRI abort event.
  10276. *
  10277. * Return: true if work posted to worker thread, otherwise false.
  10278. **/
  10279. static bool
  10280. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10281. struct lpfc_queue *cq,
  10282. struct sli4_wcqe_xri_aborted *wcqe)
  10283. {
  10284. bool workposted = false;
  10285. struct lpfc_cq_event *cq_event;
  10286. unsigned long iflags;
  10287. /* Allocate a new internal CQ_EVENT entry */
  10288. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10289. if (!cq_event) {
  10290. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10291. "0602 Failed to allocate CQ_EVENT entry\n");
  10292. return false;
  10293. }
  10294. /* Move the CQE into the proper xri abort event list */
  10295. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10296. switch (cq->subtype) {
  10297. case LPFC_FCP:
  10298. spin_lock_irqsave(&phba->hbalock, iflags);
  10299. list_add_tail(&cq_event->list,
  10300. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10301. /* Set the fcp xri abort event flag */
  10302. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10303. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10304. workposted = true;
  10305. break;
  10306. case LPFC_ELS:
  10307. spin_lock_irqsave(&phba->hbalock, iflags);
  10308. list_add_tail(&cq_event->list,
  10309. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10310. /* Set the els xri abort event flag */
  10311. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10312. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10313. workposted = true;
  10314. break;
  10315. default:
  10316. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10317. "0603 Invalid work queue CQE subtype (x%x)\n",
  10318. cq->subtype);
  10319. workposted = false;
  10320. break;
  10321. }
  10322. return workposted;
  10323. }
  10324. /**
  10325. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10326. * @phba: Pointer to HBA context object.
  10327. * @rcqe: Pointer to receive-queue completion queue entry.
  10328. *
  10329. * This routine process a receive-queue completion queue entry.
  10330. *
  10331. * Return: true if work posted to worker thread, otherwise false.
  10332. **/
  10333. static bool
  10334. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10335. {
  10336. bool workposted = false;
  10337. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10338. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10339. struct hbq_dmabuf *dma_buf;
  10340. uint32_t status, rq_id;
  10341. unsigned long iflags;
  10342. /* sanity check on queue memory */
  10343. if (unlikely(!hrq) || unlikely(!drq))
  10344. return workposted;
  10345. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10346. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10347. else
  10348. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10349. if (rq_id != hrq->queue_id)
  10350. goto out;
  10351. status = bf_get(lpfc_rcqe_status, rcqe);
  10352. switch (status) {
  10353. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10354. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10355. "2537 Receive Frame Truncated!!\n");
  10356. hrq->RQ_buf_trunc++;
  10357. case FC_STATUS_RQ_SUCCESS:
  10358. lpfc_sli4_rq_release(hrq, drq);
  10359. spin_lock_irqsave(&phba->hbalock, iflags);
  10360. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10361. if (!dma_buf) {
  10362. hrq->RQ_no_buf_found++;
  10363. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10364. goto out;
  10365. }
  10366. hrq->RQ_rcv_buf++;
  10367. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10368. /* save off the frame for the word thread to process */
  10369. list_add_tail(&dma_buf->cq_event.list,
  10370. &phba->sli4_hba.sp_queue_event);
  10371. /* Frame received */
  10372. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10373. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10374. workposted = true;
  10375. break;
  10376. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10377. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10378. hrq->RQ_no_posted_buf++;
  10379. /* Post more buffers if possible */
  10380. spin_lock_irqsave(&phba->hbalock, iflags);
  10381. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10382. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10383. workposted = true;
  10384. break;
  10385. }
  10386. out:
  10387. return workposted;
  10388. }
  10389. /**
  10390. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10391. * @phba: Pointer to HBA context object.
  10392. * @cq: Pointer to the completion queue.
  10393. * @wcqe: Pointer to a completion queue entry.
  10394. *
  10395. * This routine process a slow-path work-queue or receive queue completion queue
  10396. * entry.
  10397. *
  10398. * Return: true if work posted to worker thread, otherwise false.
  10399. **/
  10400. static bool
  10401. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10402. struct lpfc_cqe *cqe)
  10403. {
  10404. struct lpfc_cqe cqevt;
  10405. bool workposted = false;
  10406. /* Copy the work queue CQE and convert endian order if needed */
  10407. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10408. /* Check and process for different type of WCQE and dispatch */
  10409. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10410. case CQE_CODE_COMPL_WQE:
  10411. /* Process the WQ/RQ complete event */
  10412. phba->last_completion_time = jiffies;
  10413. workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
  10414. (struct lpfc_wcqe_complete *)&cqevt);
  10415. break;
  10416. case CQE_CODE_RELEASE_WQE:
  10417. /* Process the WQ release event */
  10418. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10419. (struct lpfc_wcqe_release *)&cqevt);
  10420. break;
  10421. case CQE_CODE_XRI_ABORTED:
  10422. /* Process the WQ XRI abort event */
  10423. phba->last_completion_time = jiffies;
  10424. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10425. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10426. break;
  10427. case CQE_CODE_RECEIVE:
  10428. case CQE_CODE_RECEIVE_V1:
  10429. /* Process the RQ event */
  10430. phba->last_completion_time = jiffies;
  10431. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10432. (struct lpfc_rcqe *)&cqevt);
  10433. break;
  10434. default:
  10435. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10436. "0388 Not a valid WCQE code: x%x\n",
  10437. bf_get(lpfc_cqe_code, &cqevt));
  10438. break;
  10439. }
  10440. return workposted;
  10441. }
  10442. /**
  10443. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10444. * @phba: Pointer to HBA context object.
  10445. * @eqe: Pointer to fast-path event queue entry.
  10446. *
  10447. * This routine process a event queue entry from the slow-path event queue.
  10448. * It will check the MajorCode and MinorCode to determine this is for a
  10449. * completion event on a completion queue, if not, an error shall be logged
  10450. * and just return. Otherwise, it will get to the corresponding completion
  10451. * queue and process all the entries on that completion queue, rearm the
  10452. * completion queue, and then return.
  10453. *
  10454. **/
  10455. static void
  10456. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10457. struct lpfc_queue *speq)
  10458. {
  10459. struct lpfc_queue *cq = NULL, *childq;
  10460. struct lpfc_cqe *cqe;
  10461. bool workposted = false;
  10462. int ecount = 0;
  10463. uint16_t cqid;
  10464. /* Get the reference to the corresponding CQ */
  10465. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10466. list_for_each_entry(childq, &speq->child_list, list) {
  10467. if (childq->queue_id == cqid) {
  10468. cq = childq;
  10469. break;
  10470. }
  10471. }
  10472. if (unlikely(!cq)) {
  10473. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10474. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10475. "0365 Slow-path CQ identifier "
  10476. "(%d) does not exist\n", cqid);
  10477. return;
  10478. }
  10479. /* Process all the entries to the CQ */
  10480. switch (cq->type) {
  10481. case LPFC_MCQ:
  10482. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10483. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10484. if (!(++ecount % cq->entry_repost))
  10485. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10486. cq->CQ_mbox++;
  10487. }
  10488. break;
  10489. case LPFC_WCQ:
  10490. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10491. if (cq->subtype == LPFC_FCP)
  10492. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10493. cqe);
  10494. else
  10495. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10496. cqe);
  10497. if (!(++ecount % cq->entry_repost))
  10498. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10499. }
  10500. /* Track the max number of CQEs processed in 1 EQ */
  10501. if (ecount > cq->CQ_max_cqe)
  10502. cq->CQ_max_cqe = ecount;
  10503. break;
  10504. default:
  10505. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10506. "0370 Invalid completion queue type (%d)\n",
  10507. cq->type);
  10508. return;
  10509. }
  10510. /* Catch the no cq entry condition, log an error */
  10511. if (unlikely(ecount == 0))
  10512. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10513. "0371 No entry from the CQ: identifier "
  10514. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10515. /* In any case, flash and re-arm the RCQ */
  10516. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10517. /* wake up worker thread if there are works to be done */
  10518. if (workposted)
  10519. lpfc_worker_wake_up(phba);
  10520. }
  10521. /**
  10522. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10523. * @phba: Pointer to HBA context object.
  10524. * @cq: Pointer to associated CQ
  10525. * @wcqe: Pointer to work-queue completion queue entry.
  10526. *
  10527. * This routine process a fast-path work queue completion entry from fast-path
  10528. * event queue for FCP command response completion.
  10529. **/
  10530. static void
  10531. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10532. struct lpfc_wcqe_complete *wcqe)
  10533. {
  10534. struct lpfc_sli_ring *pring = cq->pring;
  10535. struct lpfc_iocbq *cmdiocbq;
  10536. struct lpfc_iocbq irspiocbq;
  10537. unsigned long iflags;
  10538. /* Check for response status */
  10539. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10540. /* If resource errors reported from HBA, reduce queue
  10541. * depth of the SCSI device.
  10542. */
  10543. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10544. IOSTAT_LOCAL_REJECT) &&
  10545. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  10546. phba->lpfc_rampdown_queue_depth(phba);
  10547. }
  10548. /* Log the error status */
  10549. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10550. "0373 FCP complete error: status=x%x, "
  10551. "hw_status=x%x, total_data_specified=%d, "
  10552. "parameter=x%x, word3=x%x\n",
  10553. bf_get(lpfc_wcqe_c_status, wcqe),
  10554. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10555. wcqe->total_data_placed, wcqe->parameter,
  10556. wcqe->word3);
  10557. }
  10558. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10559. spin_lock_irqsave(&pring->ring_lock, iflags);
  10560. pring->stats.iocb_event++;
  10561. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10562. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10563. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10564. if (unlikely(!cmdiocbq)) {
  10565. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10566. "0374 FCP complete with no corresponding "
  10567. "cmdiocb: iotag (%d)\n",
  10568. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10569. return;
  10570. }
  10571. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10572. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10573. "0375 FCP cmdiocb not callback function "
  10574. "iotag: (%d)\n",
  10575. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10576. return;
  10577. }
  10578. /* Fake the irspiocb and copy necessary response information */
  10579. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10580. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10581. spin_lock_irqsave(&phba->hbalock, iflags);
  10582. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10583. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10584. }
  10585. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10586. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10587. }
  10588. /**
  10589. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10590. * @phba: Pointer to HBA context object.
  10591. * @cq: Pointer to completion queue.
  10592. * @wcqe: Pointer to work-queue completion queue entry.
  10593. *
  10594. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10595. * proper WQ release routine to the slow-path WQ.
  10596. **/
  10597. static void
  10598. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10599. struct lpfc_wcqe_release *wcqe)
  10600. {
  10601. struct lpfc_queue *childwq;
  10602. bool wqid_matched = false;
  10603. uint16_t fcp_wqid;
  10604. /* Check for fast-path FCP work queue release */
  10605. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10606. list_for_each_entry(childwq, &cq->child_list, list) {
  10607. if (childwq->queue_id == fcp_wqid) {
  10608. lpfc_sli4_wq_release(childwq,
  10609. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10610. wqid_matched = true;
  10611. break;
  10612. }
  10613. }
  10614. /* Report warning log message if no match found */
  10615. if (wqid_matched != true)
  10616. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10617. "2580 Fast-path wqe consume event carries "
  10618. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10619. }
  10620. /**
  10621. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10622. * @cq: Pointer to the completion queue.
  10623. * @eqe: Pointer to fast-path completion queue entry.
  10624. *
  10625. * This routine process a fast-path work queue completion entry from fast-path
  10626. * event queue for FCP command response completion.
  10627. **/
  10628. static int
  10629. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10630. struct lpfc_cqe *cqe)
  10631. {
  10632. struct lpfc_wcqe_release wcqe;
  10633. bool workposted = false;
  10634. /* Copy the work queue CQE and convert endian order if needed */
  10635. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10636. /* Check and process for different type of WCQE and dispatch */
  10637. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10638. case CQE_CODE_COMPL_WQE:
  10639. cq->CQ_wq++;
  10640. /* Process the WQ complete event */
  10641. phba->last_completion_time = jiffies;
  10642. lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
  10643. (struct lpfc_wcqe_complete *)&wcqe);
  10644. break;
  10645. case CQE_CODE_RELEASE_WQE:
  10646. cq->CQ_release_wqe++;
  10647. /* Process the WQ release event */
  10648. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10649. (struct lpfc_wcqe_release *)&wcqe);
  10650. break;
  10651. case CQE_CODE_XRI_ABORTED:
  10652. cq->CQ_xri_aborted++;
  10653. /* Process the WQ XRI abort event */
  10654. phba->last_completion_time = jiffies;
  10655. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10656. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10657. break;
  10658. default:
  10659. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10660. "0144 Not a valid WCQE code: x%x\n",
  10661. bf_get(lpfc_wcqe_c_code, &wcqe));
  10662. break;
  10663. }
  10664. return workposted;
  10665. }
  10666. /**
  10667. * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
  10668. * @phba: Pointer to HBA context object.
  10669. * @eqe: Pointer to fast-path event queue entry.
  10670. *
  10671. * This routine process a event queue entry from the fast-path event queue.
  10672. * It will check the MajorCode and MinorCode to determine this is for a
  10673. * completion event on a completion queue, if not, an error shall be logged
  10674. * and just return. Otherwise, it will get to the corresponding completion
  10675. * queue and process all the entries on the completion queue, rearm the
  10676. * completion queue, and then return.
  10677. **/
  10678. static void
  10679. lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10680. uint32_t qidx)
  10681. {
  10682. struct lpfc_queue *cq;
  10683. struct lpfc_cqe *cqe;
  10684. bool workposted = false;
  10685. uint16_t cqid;
  10686. int ecount = 0;
  10687. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10688. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10689. "0366 Not a valid completion "
  10690. "event: majorcode=x%x, minorcode=x%x\n",
  10691. bf_get_le32(lpfc_eqe_major_code, eqe),
  10692. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10693. return;
  10694. }
  10695. /* Get the reference to the corresponding CQ */
  10696. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10697. /* Check if this is a Slow path event */
  10698. if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
  10699. lpfc_sli4_sp_handle_eqe(phba, eqe,
  10700. phba->sli4_hba.hba_eq[qidx]);
  10701. return;
  10702. }
  10703. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10704. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10705. "3146 Fast-path completion queues "
  10706. "does not exist\n");
  10707. return;
  10708. }
  10709. cq = phba->sli4_hba.fcp_cq[qidx];
  10710. if (unlikely(!cq)) {
  10711. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10712. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10713. "0367 Fast-path completion queue "
  10714. "(%d) does not exist\n", qidx);
  10715. return;
  10716. }
  10717. if (unlikely(cqid != cq->queue_id)) {
  10718. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10719. "0368 Miss-matched fast-path completion "
  10720. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10721. cqid, cq->queue_id);
  10722. return;
  10723. }
  10724. /* Process all the entries to the CQ */
  10725. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10726. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10727. if (!(++ecount % cq->entry_repost))
  10728. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10729. }
  10730. /* Track the max number of CQEs processed in 1 EQ */
  10731. if (ecount > cq->CQ_max_cqe)
  10732. cq->CQ_max_cqe = ecount;
  10733. /* Catch the no cq entry condition */
  10734. if (unlikely(ecount == 0))
  10735. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10736. "0369 No entry from fast-path completion "
  10737. "queue fcpcqid=%d\n", cq->queue_id);
  10738. /* In any case, flash and re-arm the CQ */
  10739. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10740. /* wake up worker thread if there are works to be done */
  10741. if (workposted)
  10742. lpfc_worker_wake_up(phba);
  10743. }
  10744. static void
  10745. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10746. {
  10747. struct lpfc_eqe *eqe;
  10748. /* walk all the EQ entries and drop on the floor */
  10749. while ((eqe = lpfc_sli4_eq_get(eq)))
  10750. ;
  10751. /* Clear and re-arm the EQ */
  10752. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10753. }
  10754. /**
  10755. * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
  10756. * @irq: Interrupt number.
  10757. * @dev_id: The device context pointer.
  10758. *
  10759. * This function is directly called from the PCI layer as an interrupt
  10760. * service routine when device with SLI-4 interface spec is enabled with
  10761. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10762. * ring event in the HBA. However, when the device is enabled with either
  10763. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10764. * device-level interrupt handler. When the PCI slot is in error recovery
  10765. * or the HBA is undergoing initialization, the interrupt handler will not
  10766. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10767. * the intrrupt context. This function is called without any lock held.
  10768. * It gets the hbalock to access and update SLI data structures. Note that,
  10769. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10770. * equal to that of FCP CQ index.
  10771. *
  10772. * The link attention and ELS ring attention events are handled
  10773. * by the worker thread. The interrupt handler signals the worker thread
  10774. * and returns for these events. This function is called without any lock
  10775. * held. It gets the hbalock to access and update SLI data structures.
  10776. *
  10777. * This function returns IRQ_HANDLED when interrupt is handled else it
  10778. * returns IRQ_NONE.
  10779. **/
  10780. irqreturn_t
  10781. lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
  10782. {
  10783. struct lpfc_hba *phba;
  10784. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10785. struct lpfc_queue *fpeq;
  10786. struct lpfc_eqe *eqe;
  10787. unsigned long iflag;
  10788. int ecount = 0;
  10789. uint32_t fcp_eqidx;
  10790. /* Get the driver's phba structure from the dev_id */
  10791. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10792. phba = fcp_eq_hdl->phba;
  10793. fcp_eqidx = fcp_eq_hdl->idx;
  10794. if (unlikely(!phba))
  10795. return IRQ_NONE;
  10796. if (unlikely(!phba->sli4_hba.hba_eq))
  10797. return IRQ_NONE;
  10798. /* Get to the EQ struct associated with this vector */
  10799. fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
  10800. if (unlikely(!fpeq))
  10801. return IRQ_NONE;
  10802. /* Check device state for handling interrupt */
  10803. if (unlikely(lpfc_intr_state_check(phba))) {
  10804. fpeq->EQ_badstate++;
  10805. /* Check again for link_state with lock held */
  10806. spin_lock_irqsave(&phba->hbalock, iflag);
  10807. if (phba->link_state < LPFC_LINK_DOWN)
  10808. /* Flush, clear interrupt, and rearm the EQ */
  10809. lpfc_sli4_eq_flush(phba, fpeq);
  10810. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10811. return IRQ_NONE;
  10812. }
  10813. /*
  10814. * Process all the event on FCP fast-path EQ
  10815. */
  10816. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10817. lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
  10818. if (!(++ecount % fpeq->entry_repost))
  10819. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10820. fpeq->EQ_processed++;
  10821. }
  10822. /* Track the max number of EQEs processed in 1 intr */
  10823. if (ecount > fpeq->EQ_max_eqe)
  10824. fpeq->EQ_max_eqe = ecount;
  10825. /* Always clear and re-arm the fast-path EQ */
  10826. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10827. if (unlikely(ecount == 0)) {
  10828. fpeq->EQ_no_entry++;
  10829. if (phba->intr_type == MSIX)
  10830. /* MSI-X treated interrupt served as no EQ share INT */
  10831. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10832. "0358 MSI-X interrupt with no EQE\n");
  10833. else
  10834. /* Non MSI-X treated on interrupt as EQ share INT */
  10835. return IRQ_NONE;
  10836. }
  10837. return IRQ_HANDLED;
  10838. } /* lpfc_sli4_fp_intr_handler */
  10839. /**
  10840. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10841. * @irq: Interrupt number.
  10842. * @dev_id: The device context pointer.
  10843. *
  10844. * This function is the device-level interrupt handler to device with SLI-4
  10845. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10846. * interrupt mode is enabled and there is an event in the HBA which requires
  10847. * driver attention. This function invokes the slow-path interrupt attention
  10848. * handling function and fast-path interrupt attention handling function in
  10849. * turn to process the relevant HBA attention events. This function is called
  10850. * without any lock held. It gets the hbalock to access and update SLI data
  10851. * structures.
  10852. *
  10853. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10854. * returns IRQ_NONE.
  10855. **/
  10856. irqreturn_t
  10857. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10858. {
  10859. struct lpfc_hba *phba;
  10860. irqreturn_t hba_irq_rc;
  10861. bool hba_handled = false;
  10862. uint32_t fcp_eqidx;
  10863. /* Get the driver's phba structure from the dev_id */
  10864. phba = (struct lpfc_hba *)dev_id;
  10865. if (unlikely(!phba))
  10866. return IRQ_NONE;
  10867. /*
  10868. * Invoke fast-path host attention interrupt handling as appropriate.
  10869. */
  10870. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
  10871. hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
  10872. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10873. if (hba_irq_rc == IRQ_HANDLED)
  10874. hba_handled |= true;
  10875. }
  10876. return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
  10877. } /* lpfc_sli4_intr_handler */
  10878. /**
  10879. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10880. * @queue: The queue structure to free.
  10881. *
  10882. * This function frees a queue structure and the DMAable memory used for
  10883. * the host resident queue. This function must be called after destroying the
  10884. * queue on the HBA.
  10885. **/
  10886. void
  10887. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10888. {
  10889. struct lpfc_dmabuf *dmabuf;
  10890. if (!queue)
  10891. return;
  10892. while (!list_empty(&queue->page_list)) {
  10893. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10894. list);
  10895. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10896. dmabuf->virt, dmabuf->phys);
  10897. kfree(dmabuf);
  10898. }
  10899. kfree(queue);
  10900. return;
  10901. }
  10902. /**
  10903. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10904. * @phba: The HBA that this queue is being created on.
  10905. * @entry_size: The size of each queue entry for this queue.
  10906. * @entry count: The number of entries that this queue will handle.
  10907. *
  10908. * This function allocates a queue structure and the DMAable memory used for
  10909. * the host resident queue. This function must be called before creating the
  10910. * queue on the HBA.
  10911. **/
  10912. struct lpfc_queue *
  10913. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10914. uint32_t entry_count)
  10915. {
  10916. struct lpfc_queue *queue;
  10917. struct lpfc_dmabuf *dmabuf;
  10918. int x, total_qe_count;
  10919. void *dma_pointer;
  10920. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10921. if (!phba->sli4_hba.pc_sli4_params.supported)
  10922. hw_page_size = SLI4_PAGE_SIZE;
  10923. queue = kzalloc(sizeof(struct lpfc_queue) +
  10924. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  10925. if (!queue)
  10926. return NULL;
  10927. queue->page_count = (ALIGN(entry_size * entry_count,
  10928. hw_page_size))/hw_page_size;
  10929. INIT_LIST_HEAD(&queue->list);
  10930. INIT_LIST_HEAD(&queue->page_list);
  10931. INIT_LIST_HEAD(&queue->child_list);
  10932. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  10933. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  10934. if (!dmabuf)
  10935. goto out_fail;
  10936. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  10937. hw_page_size, &dmabuf->phys,
  10938. GFP_KERNEL);
  10939. if (!dmabuf->virt) {
  10940. kfree(dmabuf);
  10941. goto out_fail;
  10942. }
  10943. memset(dmabuf->virt, 0, hw_page_size);
  10944. dmabuf->buffer_tag = x;
  10945. list_add_tail(&dmabuf->list, &queue->page_list);
  10946. /* initialize queue's entry array */
  10947. dma_pointer = dmabuf->virt;
  10948. for (; total_qe_count < entry_count &&
  10949. dma_pointer < (hw_page_size + dmabuf->virt);
  10950. total_qe_count++, dma_pointer += entry_size) {
  10951. queue->qe[total_qe_count].address = dma_pointer;
  10952. }
  10953. }
  10954. queue->entry_size = entry_size;
  10955. queue->entry_count = entry_count;
  10956. /*
  10957. * entry_repost is calculated based on the number of entries in the
  10958. * queue. This works out except for RQs. If buffers are NOT initially
  10959. * posted for every RQE, entry_repost should be adjusted accordingly.
  10960. */
  10961. queue->entry_repost = (entry_count >> 3);
  10962. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  10963. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  10964. queue->phba = phba;
  10965. return queue;
  10966. out_fail:
  10967. lpfc_sli4_queue_free(queue);
  10968. return NULL;
  10969. }
  10970. /**
  10971. * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
  10972. * @phba: HBA structure that indicates port to create a queue on.
  10973. * @startq: The starting FCP EQ to modify
  10974. *
  10975. * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
  10976. *
  10977. * The @phba struct is used to send mailbox command to HBA. The @startq
  10978. * is used to get the starting FCP EQ to change.
  10979. * This function is asynchronous and will wait for the mailbox
  10980. * command to finish before continuing.
  10981. *
  10982. * On success this function will return a zero. If unable to allocate enough
  10983. * memory this function will return -ENOMEM. If the queue create mailbox command
  10984. * fails this function will return -ENXIO.
  10985. **/
  10986. uint32_t
  10987. lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
  10988. {
  10989. struct lpfc_mbx_modify_eq_delay *eq_delay;
  10990. LPFC_MBOXQ_t *mbox;
  10991. struct lpfc_queue *eq;
  10992. int cnt, rc, length, status = 0;
  10993. uint32_t shdr_status, shdr_add_status;
  10994. int fcp_eqidx;
  10995. union lpfc_sli4_cfg_shdr *shdr;
  10996. uint16_t dmult;
  10997. if (startq >= phba->cfg_fcp_io_channel)
  10998. return 0;
  10999. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11000. if (!mbox)
  11001. return -ENOMEM;
  11002. length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
  11003. sizeof(struct lpfc_sli4_cfg_mhdr));
  11004. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11005. LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
  11006. length, LPFC_SLI4_MBX_EMBED);
  11007. eq_delay = &mbox->u.mqe.un.eq_delay;
  11008. /* Calculate delay multiper from maximum interrupt per second */
  11009. dmult = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
  11010. dmult = LPFC_DMULT_CONST/dmult - 1;
  11011. cnt = 0;
  11012. for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
  11013. fcp_eqidx++) {
  11014. eq = phba->sli4_hba.hba_eq[fcp_eqidx];
  11015. if (!eq)
  11016. continue;
  11017. eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
  11018. eq_delay->u.request.eq[cnt].phase = 0;
  11019. eq_delay->u.request.eq[cnt].delay_multi = dmult;
  11020. cnt++;
  11021. if (cnt >= LPFC_MAX_EQ_DELAY)
  11022. break;
  11023. }
  11024. eq_delay->u.request.num_eq = cnt;
  11025. mbox->vport = phba->pport;
  11026. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11027. mbox->context1 = NULL;
  11028. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11029. shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
  11030. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11031. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11032. if (shdr_status || shdr_add_status || rc) {
  11033. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11034. "2512 MODIFY_EQ_DELAY mailbox failed with "
  11035. "status x%x add_status x%x, mbx status x%x\n",
  11036. shdr_status, shdr_add_status, rc);
  11037. status = -ENXIO;
  11038. }
  11039. mempool_free(mbox, phba->mbox_mem_pool);
  11040. return status;
  11041. }
  11042. /**
  11043. * lpfc_eq_create - Create an Event Queue on the HBA
  11044. * @phba: HBA structure that indicates port to create a queue on.
  11045. * @eq: The queue structure to use to create the event queue.
  11046. * @imax: The maximum interrupt per second limit.
  11047. *
  11048. * This function creates an event queue, as detailed in @eq, on a port,
  11049. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  11050. *
  11051. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  11052. * is used to get the entry count and entry size that are necessary to
  11053. * determine the number of pages to allocate and use for this queue. This
  11054. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  11055. * event queue. This function is asynchronous and will wait for the mailbox
  11056. * command to finish before continuing.
  11057. *
  11058. * On success this function will return a zero. If unable to allocate enough
  11059. * memory this function will return -ENOMEM. If the queue create mailbox command
  11060. * fails this function will return -ENXIO.
  11061. **/
  11062. uint32_t
  11063. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  11064. {
  11065. struct lpfc_mbx_eq_create *eq_create;
  11066. LPFC_MBOXQ_t *mbox;
  11067. int rc, length, status = 0;
  11068. struct lpfc_dmabuf *dmabuf;
  11069. uint32_t shdr_status, shdr_add_status;
  11070. union lpfc_sli4_cfg_shdr *shdr;
  11071. uint16_t dmult;
  11072. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11073. /* sanity check on queue memory */
  11074. if (!eq)
  11075. return -ENODEV;
  11076. if (!phba->sli4_hba.pc_sli4_params.supported)
  11077. hw_page_size = SLI4_PAGE_SIZE;
  11078. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11079. if (!mbox)
  11080. return -ENOMEM;
  11081. length = (sizeof(struct lpfc_mbx_eq_create) -
  11082. sizeof(struct lpfc_sli4_cfg_mhdr));
  11083. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11084. LPFC_MBOX_OPCODE_EQ_CREATE,
  11085. length, LPFC_SLI4_MBX_EMBED);
  11086. eq_create = &mbox->u.mqe.un.eq_create;
  11087. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  11088. eq->page_count);
  11089. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  11090. LPFC_EQE_SIZE);
  11091. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  11092. /* Calculate delay multiper from maximum interrupt per second */
  11093. dmult = LPFC_DMULT_CONST/imax - 1;
  11094. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  11095. dmult);
  11096. switch (eq->entry_count) {
  11097. default:
  11098. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11099. "0360 Unsupported EQ count. (%d)\n",
  11100. eq->entry_count);
  11101. if (eq->entry_count < 256)
  11102. return -EINVAL;
  11103. /* otherwise default to smallest count (drop through) */
  11104. case 256:
  11105. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11106. LPFC_EQ_CNT_256);
  11107. break;
  11108. case 512:
  11109. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11110. LPFC_EQ_CNT_512);
  11111. break;
  11112. case 1024:
  11113. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11114. LPFC_EQ_CNT_1024);
  11115. break;
  11116. case 2048:
  11117. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11118. LPFC_EQ_CNT_2048);
  11119. break;
  11120. case 4096:
  11121. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11122. LPFC_EQ_CNT_4096);
  11123. break;
  11124. }
  11125. list_for_each_entry(dmabuf, &eq->page_list, list) {
  11126. memset(dmabuf->virt, 0, hw_page_size);
  11127. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11128. putPaddrLow(dmabuf->phys);
  11129. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11130. putPaddrHigh(dmabuf->phys);
  11131. }
  11132. mbox->vport = phba->pport;
  11133. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11134. mbox->context1 = NULL;
  11135. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11136. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  11137. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11138. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11139. if (shdr_status || shdr_add_status || rc) {
  11140. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11141. "2500 EQ_CREATE mailbox failed with "
  11142. "status x%x add_status x%x, mbx status x%x\n",
  11143. shdr_status, shdr_add_status, rc);
  11144. status = -ENXIO;
  11145. }
  11146. eq->type = LPFC_EQ;
  11147. eq->subtype = LPFC_NONE;
  11148. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  11149. if (eq->queue_id == 0xFFFF)
  11150. status = -ENXIO;
  11151. eq->host_index = 0;
  11152. eq->hba_index = 0;
  11153. mempool_free(mbox, phba->mbox_mem_pool);
  11154. return status;
  11155. }
  11156. /**
  11157. * lpfc_cq_create - Create a Completion Queue on the HBA
  11158. * @phba: HBA structure that indicates port to create a queue on.
  11159. * @cq: The queue structure to use to create the completion queue.
  11160. * @eq: The event queue to bind this completion queue to.
  11161. *
  11162. * This function creates a completion queue, as detailed in @wq, on a port,
  11163. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  11164. *
  11165. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11166. * is used to get the entry count and entry size that are necessary to
  11167. * determine the number of pages to allocate and use for this queue. The @eq
  11168. * is used to indicate which event queue to bind this completion queue to. This
  11169. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  11170. * completion queue. This function is asynchronous and will wait for the mailbox
  11171. * command to finish before continuing.
  11172. *
  11173. * On success this function will return a zero. If unable to allocate enough
  11174. * memory this function will return -ENOMEM. If the queue create mailbox command
  11175. * fails this function will return -ENXIO.
  11176. **/
  11177. uint32_t
  11178. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11179. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  11180. {
  11181. struct lpfc_mbx_cq_create *cq_create;
  11182. struct lpfc_dmabuf *dmabuf;
  11183. LPFC_MBOXQ_t *mbox;
  11184. int rc, length, status = 0;
  11185. uint32_t shdr_status, shdr_add_status;
  11186. union lpfc_sli4_cfg_shdr *shdr;
  11187. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11188. /* sanity check on queue memory */
  11189. if (!cq || !eq)
  11190. return -ENODEV;
  11191. if (!phba->sli4_hba.pc_sli4_params.supported)
  11192. hw_page_size = SLI4_PAGE_SIZE;
  11193. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11194. if (!mbox)
  11195. return -ENOMEM;
  11196. length = (sizeof(struct lpfc_mbx_cq_create) -
  11197. sizeof(struct lpfc_sli4_cfg_mhdr));
  11198. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11199. LPFC_MBOX_OPCODE_CQ_CREATE,
  11200. length, LPFC_SLI4_MBX_EMBED);
  11201. cq_create = &mbox->u.mqe.un.cq_create;
  11202. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  11203. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  11204. cq->page_count);
  11205. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  11206. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  11207. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11208. phba->sli4_hba.pc_sli4_params.cqv);
  11209. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  11210. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  11211. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  11212. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  11213. eq->queue_id);
  11214. } else {
  11215. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  11216. eq->queue_id);
  11217. }
  11218. switch (cq->entry_count) {
  11219. default:
  11220. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11221. "0361 Unsupported CQ count. (%d)\n",
  11222. cq->entry_count);
  11223. if (cq->entry_count < 256) {
  11224. status = -EINVAL;
  11225. goto out;
  11226. }
  11227. /* otherwise default to smallest count (drop through) */
  11228. case 256:
  11229. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11230. LPFC_CQ_CNT_256);
  11231. break;
  11232. case 512:
  11233. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11234. LPFC_CQ_CNT_512);
  11235. break;
  11236. case 1024:
  11237. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11238. LPFC_CQ_CNT_1024);
  11239. break;
  11240. }
  11241. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11242. memset(dmabuf->virt, 0, hw_page_size);
  11243. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11244. putPaddrLow(dmabuf->phys);
  11245. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11246. putPaddrHigh(dmabuf->phys);
  11247. }
  11248. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11249. /* The IOCTL status is embedded in the mailbox subheader. */
  11250. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11251. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11252. if (shdr_status || shdr_add_status || rc) {
  11253. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11254. "2501 CQ_CREATE mailbox failed with "
  11255. "status x%x add_status x%x, mbx status x%x\n",
  11256. shdr_status, shdr_add_status, rc);
  11257. status = -ENXIO;
  11258. goto out;
  11259. }
  11260. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11261. if (cq->queue_id == 0xFFFF) {
  11262. status = -ENXIO;
  11263. goto out;
  11264. }
  11265. /* link the cq onto the parent eq child list */
  11266. list_add_tail(&cq->list, &eq->child_list);
  11267. /* Set up completion queue's type and subtype */
  11268. cq->type = type;
  11269. cq->subtype = subtype;
  11270. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11271. cq->assoc_qid = eq->queue_id;
  11272. cq->host_index = 0;
  11273. cq->hba_index = 0;
  11274. out:
  11275. mempool_free(mbox, phba->mbox_mem_pool);
  11276. return status;
  11277. }
  11278. /**
  11279. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11280. * @phba: HBA structure that indicates port to create a queue on.
  11281. * @mq: The queue structure to use to create the mailbox queue.
  11282. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11283. * @cq: The completion queue to associate with this cq.
  11284. *
  11285. * This function provides failback (fb) functionality when the
  11286. * mq_create_ext fails on older FW generations. It's purpose is identical
  11287. * to mq_create_ext otherwise.
  11288. *
  11289. * This routine cannot fail as all attributes were previously accessed and
  11290. * initialized in mq_create_ext.
  11291. **/
  11292. static void
  11293. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11294. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11295. {
  11296. struct lpfc_mbx_mq_create *mq_create;
  11297. struct lpfc_dmabuf *dmabuf;
  11298. int length;
  11299. length = (sizeof(struct lpfc_mbx_mq_create) -
  11300. sizeof(struct lpfc_sli4_cfg_mhdr));
  11301. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11302. LPFC_MBOX_OPCODE_MQ_CREATE,
  11303. length, LPFC_SLI4_MBX_EMBED);
  11304. mq_create = &mbox->u.mqe.un.mq_create;
  11305. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11306. mq->page_count);
  11307. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11308. cq->queue_id);
  11309. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11310. switch (mq->entry_count) {
  11311. case 16:
  11312. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11313. LPFC_MQ_RING_SIZE_16);
  11314. break;
  11315. case 32:
  11316. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11317. LPFC_MQ_RING_SIZE_32);
  11318. break;
  11319. case 64:
  11320. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11321. LPFC_MQ_RING_SIZE_64);
  11322. break;
  11323. case 128:
  11324. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11325. LPFC_MQ_RING_SIZE_128);
  11326. break;
  11327. }
  11328. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11329. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11330. putPaddrLow(dmabuf->phys);
  11331. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11332. putPaddrHigh(dmabuf->phys);
  11333. }
  11334. }
  11335. /**
  11336. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11337. * @phba: HBA structure that indicates port to create a queue on.
  11338. * @mq: The queue structure to use to create the mailbox queue.
  11339. * @cq: The completion queue to associate with this cq.
  11340. * @subtype: The queue's subtype.
  11341. *
  11342. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11343. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11344. *
  11345. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11346. * is used to get the entry count and entry size that are necessary to
  11347. * determine the number of pages to allocate and use for this queue. This
  11348. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  11349. * mailbox queue. This function is asynchronous and will wait for the mailbox
  11350. * command to finish before continuing.
  11351. *
  11352. * On success this function will return a zero. If unable to allocate enough
  11353. * memory this function will return -ENOMEM. If the queue create mailbox command
  11354. * fails this function will return -ENXIO.
  11355. **/
  11356. int32_t
  11357. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11358. struct lpfc_queue *cq, uint32_t subtype)
  11359. {
  11360. struct lpfc_mbx_mq_create *mq_create;
  11361. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11362. struct lpfc_dmabuf *dmabuf;
  11363. LPFC_MBOXQ_t *mbox;
  11364. int rc, length, status = 0;
  11365. uint32_t shdr_status, shdr_add_status;
  11366. union lpfc_sli4_cfg_shdr *shdr;
  11367. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11368. /* sanity check on queue memory */
  11369. if (!mq || !cq)
  11370. return -ENODEV;
  11371. if (!phba->sli4_hba.pc_sli4_params.supported)
  11372. hw_page_size = SLI4_PAGE_SIZE;
  11373. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11374. if (!mbox)
  11375. return -ENOMEM;
  11376. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11377. sizeof(struct lpfc_sli4_cfg_mhdr));
  11378. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11379. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11380. length, LPFC_SLI4_MBX_EMBED);
  11381. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11382. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11383. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11384. &mq_create_ext->u.request, mq->page_count);
  11385. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11386. &mq_create_ext->u.request, 1);
  11387. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11388. &mq_create_ext->u.request, 1);
  11389. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11390. &mq_create_ext->u.request, 1);
  11391. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11392. &mq_create_ext->u.request, 1);
  11393. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11394. &mq_create_ext->u.request, 1);
  11395. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11396. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11397. phba->sli4_hba.pc_sli4_params.mqv);
  11398. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11399. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11400. cq->queue_id);
  11401. else
  11402. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11403. cq->queue_id);
  11404. switch (mq->entry_count) {
  11405. default:
  11406. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11407. "0362 Unsupported MQ count. (%d)\n",
  11408. mq->entry_count);
  11409. if (mq->entry_count < 16) {
  11410. status = -EINVAL;
  11411. goto out;
  11412. }
  11413. /* otherwise default to smallest count (drop through) */
  11414. case 16:
  11415. bf_set(lpfc_mq_context_ring_size,
  11416. &mq_create_ext->u.request.context,
  11417. LPFC_MQ_RING_SIZE_16);
  11418. break;
  11419. case 32:
  11420. bf_set(lpfc_mq_context_ring_size,
  11421. &mq_create_ext->u.request.context,
  11422. LPFC_MQ_RING_SIZE_32);
  11423. break;
  11424. case 64:
  11425. bf_set(lpfc_mq_context_ring_size,
  11426. &mq_create_ext->u.request.context,
  11427. LPFC_MQ_RING_SIZE_64);
  11428. break;
  11429. case 128:
  11430. bf_set(lpfc_mq_context_ring_size,
  11431. &mq_create_ext->u.request.context,
  11432. LPFC_MQ_RING_SIZE_128);
  11433. break;
  11434. }
  11435. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11436. memset(dmabuf->virt, 0, hw_page_size);
  11437. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11438. putPaddrLow(dmabuf->phys);
  11439. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11440. putPaddrHigh(dmabuf->phys);
  11441. }
  11442. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11443. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11444. &mq_create_ext->u.response);
  11445. if (rc != MBX_SUCCESS) {
  11446. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11447. "2795 MQ_CREATE_EXT failed with "
  11448. "status x%x. Failback to MQ_CREATE.\n",
  11449. rc);
  11450. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11451. mq_create = &mbox->u.mqe.un.mq_create;
  11452. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11453. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11454. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11455. &mq_create->u.response);
  11456. }
  11457. /* The IOCTL status is embedded in the mailbox subheader. */
  11458. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11459. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11460. if (shdr_status || shdr_add_status || rc) {
  11461. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11462. "2502 MQ_CREATE mailbox failed with "
  11463. "status x%x add_status x%x, mbx status x%x\n",
  11464. shdr_status, shdr_add_status, rc);
  11465. status = -ENXIO;
  11466. goto out;
  11467. }
  11468. if (mq->queue_id == 0xFFFF) {
  11469. status = -ENXIO;
  11470. goto out;
  11471. }
  11472. mq->type = LPFC_MQ;
  11473. mq->assoc_qid = cq->queue_id;
  11474. mq->subtype = subtype;
  11475. mq->host_index = 0;
  11476. mq->hba_index = 0;
  11477. /* link the mq onto the parent cq child list */
  11478. list_add_tail(&mq->list, &cq->child_list);
  11479. out:
  11480. mempool_free(mbox, phba->mbox_mem_pool);
  11481. return status;
  11482. }
  11483. /**
  11484. * lpfc_wq_create - Create a Work Queue on the HBA
  11485. * @phba: HBA structure that indicates port to create a queue on.
  11486. * @wq: The queue structure to use to create the work queue.
  11487. * @cq: The completion queue to bind this work queue to.
  11488. * @subtype: The subtype of the work queue indicating its functionality.
  11489. *
  11490. * This function creates a work queue, as detailed in @wq, on a port, described
  11491. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11492. *
  11493. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11494. * is used to get the entry count and entry size that are necessary to
  11495. * determine the number of pages to allocate and use for this queue. The @cq
  11496. * is used to indicate which completion queue to bind this work queue to. This
  11497. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11498. * work queue. This function is asynchronous and will wait for the mailbox
  11499. * command to finish before continuing.
  11500. *
  11501. * On success this function will return a zero. If unable to allocate enough
  11502. * memory this function will return -ENOMEM. If the queue create mailbox command
  11503. * fails this function will return -ENXIO.
  11504. **/
  11505. uint32_t
  11506. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11507. struct lpfc_queue *cq, uint32_t subtype)
  11508. {
  11509. struct lpfc_mbx_wq_create *wq_create;
  11510. struct lpfc_dmabuf *dmabuf;
  11511. LPFC_MBOXQ_t *mbox;
  11512. int rc, length, status = 0;
  11513. uint32_t shdr_status, shdr_add_status;
  11514. union lpfc_sli4_cfg_shdr *shdr;
  11515. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11516. struct dma_address *page;
  11517. /* sanity check on queue memory */
  11518. if (!wq || !cq)
  11519. return -ENODEV;
  11520. if (!phba->sli4_hba.pc_sli4_params.supported)
  11521. hw_page_size = SLI4_PAGE_SIZE;
  11522. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11523. if (!mbox)
  11524. return -ENOMEM;
  11525. length = (sizeof(struct lpfc_mbx_wq_create) -
  11526. sizeof(struct lpfc_sli4_cfg_mhdr));
  11527. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11528. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11529. length, LPFC_SLI4_MBX_EMBED);
  11530. wq_create = &mbox->u.mqe.un.wq_create;
  11531. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11532. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11533. wq->page_count);
  11534. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11535. cq->queue_id);
  11536. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11537. phba->sli4_hba.pc_sli4_params.wqv);
  11538. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11539. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11540. wq->entry_count);
  11541. switch (wq->entry_size) {
  11542. default:
  11543. case 64:
  11544. bf_set(lpfc_mbx_wq_create_wqe_size,
  11545. &wq_create->u.request_1,
  11546. LPFC_WQ_WQE_SIZE_64);
  11547. break;
  11548. case 128:
  11549. bf_set(lpfc_mbx_wq_create_wqe_size,
  11550. &wq_create->u.request_1,
  11551. LPFC_WQ_WQE_SIZE_128);
  11552. break;
  11553. }
  11554. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11555. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11556. page = wq_create->u.request_1.page;
  11557. } else {
  11558. page = wq_create->u.request.page;
  11559. }
  11560. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11561. memset(dmabuf->virt, 0, hw_page_size);
  11562. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11563. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11564. }
  11565. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11566. /* The IOCTL status is embedded in the mailbox subheader. */
  11567. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11568. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11569. if (shdr_status || shdr_add_status || rc) {
  11570. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11571. "2503 WQ_CREATE mailbox failed with "
  11572. "status x%x add_status x%x, mbx status x%x\n",
  11573. shdr_status, shdr_add_status, rc);
  11574. status = -ENXIO;
  11575. goto out;
  11576. }
  11577. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11578. if (wq->queue_id == 0xFFFF) {
  11579. status = -ENXIO;
  11580. goto out;
  11581. }
  11582. wq->type = LPFC_WQ;
  11583. wq->assoc_qid = cq->queue_id;
  11584. wq->subtype = subtype;
  11585. wq->host_index = 0;
  11586. wq->hba_index = 0;
  11587. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  11588. /* link the wq onto the parent cq child list */
  11589. list_add_tail(&wq->list, &cq->child_list);
  11590. out:
  11591. mempool_free(mbox, phba->mbox_mem_pool);
  11592. return status;
  11593. }
  11594. /**
  11595. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11596. * @phba: HBA structure that indicates port to create a queue on.
  11597. * @rq: The queue structure to use for the receive queue.
  11598. * @qno: The associated HBQ number
  11599. *
  11600. *
  11601. * For SLI4 we need to adjust the RQ repost value based on
  11602. * the number of buffers that are initially posted to the RQ.
  11603. */
  11604. void
  11605. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11606. {
  11607. uint32_t cnt;
  11608. /* sanity check on queue memory */
  11609. if (!rq)
  11610. return;
  11611. cnt = lpfc_hbq_defs[qno]->entry_count;
  11612. /* Recalc repost for RQs based on buffers initially posted */
  11613. cnt = (cnt >> 3);
  11614. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11615. cnt = LPFC_QUEUE_MIN_REPOST;
  11616. rq->entry_repost = cnt;
  11617. }
  11618. /**
  11619. * lpfc_rq_create - Create a Receive Queue on the HBA
  11620. * @phba: HBA structure that indicates port to create a queue on.
  11621. * @hrq: The queue structure to use to create the header receive queue.
  11622. * @drq: The queue structure to use to create the data receive queue.
  11623. * @cq: The completion queue to bind this work queue to.
  11624. *
  11625. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11626. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11627. * to the HBA.
  11628. *
  11629. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11630. * struct is used to get the entry count that is necessary to determine the
  11631. * number of pages to use for this queue. The @cq is used to indicate which
  11632. * completion queue to bind received buffers that are posted to these queues to.
  11633. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11634. * receive queue pair. This function is asynchronous and will wait for the
  11635. * mailbox command to finish before continuing.
  11636. *
  11637. * On success this function will return a zero. If unable to allocate enough
  11638. * memory this function will return -ENOMEM. If the queue create mailbox command
  11639. * fails this function will return -ENXIO.
  11640. **/
  11641. uint32_t
  11642. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11643. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11644. {
  11645. struct lpfc_mbx_rq_create *rq_create;
  11646. struct lpfc_dmabuf *dmabuf;
  11647. LPFC_MBOXQ_t *mbox;
  11648. int rc, length, status = 0;
  11649. uint32_t shdr_status, shdr_add_status;
  11650. union lpfc_sli4_cfg_shdr *shdr;
  11651. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11652. /* sanity check on queue memory */
  11653. if (!hrq || !drq || !cq)
  11654. return -ENODEV;
  11655. if (!phba->sli4_hba.pc_sli4_params.supported)
  11656. hw_page_size = SLI4_PAGE_SIZE;
  11657. if (hrq->entry_count != drq->entry_count)
  11658. return -EINVAL;
  11659. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11660. if (!mbox)
  11661. return -ENOMEM;
  11662. length = (sizeof(struct lpfc_mbx_rq_create) -
  11663. sizeof(struct lpfc_sli4_cfg_mhdr));
  11664. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11665. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11666. length, LPFC_SLI4_MBX_EMBED);
  11667. rq_create = &mbox->u.mqe.un.rq_create;
  11668. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11669. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11670. phba->sli4_hba.pc_sli4_params.rqv);
  11671. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11672. bf_set(lpfc_rq_context_rqe_count_1,
  11673. &rq_create->u.request.context,
  11674. hrq->entry_count);
  11675. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11676. bf_set(lpfc_rq_context_rqe_size,
  11677. &rq_create->u.request.context,
  11678. LPFC_RQE_SIZE_8);
  11679. bf_set(lpfc_rq_context_page_size,
  11680. &rq_create->u.request.context,
  11681. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11682. } else {
  11683. switch (hrq->entry_count) {
  11684. default:
  11685. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11686. "2535 Unsupported RQ count. (%d)\n",
  11687. hrq->entry_count);
  11688. if (hrq->entry_count < 512) {
  11689. status = -EINVAL;
  11690. goto out;
  11691. }
  11692. /* otherwise default to smallest count (drop through) */
  11693. case 512:
  11694. bf_set(lpfc_rq_context_rqe_count,
  11695. &rq_create->u.request.context,
  11696. LPFC_RQ_RING_SIZE_512);
  11697. break;
  11698. case 1024:
  11699. bf_set(lpfc_rq_context_rqe_count,
  11700. &rq_create->u.request.context,
  11701. LPFC_RQ_RING_SIZE_1024);
  11702. break;
  11703. case 2048:
  11704. bf_set(lpfc_rq_context_rqe_count,
  11705. &rq_create->u.request.context,
  11706. LPFC_RQ_RING_SIZE_2048);
  11707. break;
  11708. case 4096:
  11709. bf_set(lpfc_rq_context_rqe_count,
  11710. &rq_create->u.request.context,
  11711. LPFC_RQ_RING_SIZE_4096);
  11712. break;
  11713. }
  11714. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11715. LPFC_HDR_BUF_SIZE);
  11716. }
  11717. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11718. cq->queue_id);
  11719. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11720. hrq->page_count);
  11721. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11722. memset(dmabuf->virt, 0, hw_page_size);
  11723. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11724. putPaddrLow(dmabuf->phys);
  11725. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11726. putPaddrHigh(dmabuf->phys);
  11727. }
  11728. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11729. /* The IOCTL status is embedded in the mailbox subheader. */
  11730. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11731. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11732. if (shdr_status || shdr_add_status || rc) {
  11733. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11734. "2504 RQ_CREATE mailbox failed with "
  11735. "status x%x add_status x%x, mbx status x%x\n",
  11736. shdr_status, shdr_add_status, rc);
  11737. status = -ENXIO;
  11738. goto out;
  11739. }
  11740. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11741. if (hrq->queue_id == 0xFFFF) {
  11742. status = -ENXIO;
  11743. goto out;
  11744. }
  11745. hrq->type = LPFC_HRQ;
  11746. hrq->assoc_qid = cq->queue_id;
  11747. hrq->subtype = subtype;
  11748. hrq->host_index = 0;
  11749. hrq->hba_index = 0;
  11750. /* now create the data queue */
  11751. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11752. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11753. length, LPFC_SLI4_MBX_EMBED);
  11754. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11755. phba->sli4_hba.pc_sli4_params.rqv);
  11756. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11757. bf_set(lpfc_rq_context_rqe_count_1,
  11758. &rq_create->u.request.context, hrq->entry_count);
  11759. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11760. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11761. LPFC_RQE_SIZE_8);
  11762. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11763. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11764. } else {
  11765. switch (drq->entry_count) {
  11766. default:
  11767. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11768. "2536 Unsupported RQ count. (%d)\n",
  11769. drq->entry_count);
  11770. if (drq->entry_count < 512) {
  11771. status = -EINVAL;
  11772. goto out;
  11773. }
  11774. /* otherwise default to smallest count (drop through) */
  11775. case 512:
  11776. bf_set(lpfc_rq_context_rqe_count,
  11777. &rq_create->u.request.context,
  11778. LPFC_RQ_RING_SIZE_512);
  11779. break;
  11780. case 1024:
  11781. bf_set(lpfc_rq_context_rqe_count,
  11782. &rq_create->u.request.context,
  11783. LPFC_RQ_RING_SIZE_1024);
  11784. break;
  11785. case 2048:
  11786. bf_set(lpfc_rq_context_rqe_count,
  11787. &rq_create->u.request.context,
  11788. LPFC_RQ_RING_SIZE_2048);
  11789. break;
  11790. case 4096:
  11791. bf_set(lpfc_rq_context_rqe_count,
  11792. &rq_create->u.request.context,
  11793. LPFC_RQ_RING_SIZE_4096);
  11794. break;
  11795. }
  11796. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11797. LPFC_DATA_BUF_SIZE);
  11798. }
  11799. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11800. cq->queue_id);
  11801. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11802. drq->page_count);
  11803. list_for_each_entry(dmabuf, &drq->page_list, list) {
  11804. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11805. putPaddrLow(dmabuf->phys);
  11806. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11807. putPaddrHigh(dmabuf->phys);
  11808. }
  11809. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11810. /* The IOCTL status is embedded in the mailbox subheader. */
  11811. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11812. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11813. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11814. if (shdr_status || shdr_add_status || rc) {
  11815. status = -ENXIO;
  11816. goto out;
  11817. }
  11818. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11819. if (drq->queue_id == 0xFFFF) {
  11820. status = -ENXIO;
  11821. goto out;
  11822. }
  11823. drq->type = LPFC_DRQ;
  11824. drq->assoc_qid = cq->queue_id;
  11825. drq->subtype = subtype;
  11826. drq->host_index = 0;
  11827. drq->hba_index = 0;
  11828. /* link the header and data RQs onto the parent cq child list */
  11829. list_add_tail(&hrq->list, &cq->child_list);
  11830. list_add_tail(&drq->list, &cq->child_list);
  11831. out:
  11832. mempool_free(mbox, phba->mbox_mem_pool);
  11833. return status;
  11834. }
  11835. /**
  11836. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  11837. * @eq: The queue structure associated with the queue to destroy.
  11838. *
  11839. * This function destroys a queue, as detailed in @eq by sending an mailbox
  11840. * command, specific to the type of queue, to the HBA.
  11841. *
  11842. * The @eq struct is used to get the queue ID of the queue to destroy.
  11843. *
  11844. * On success this function will return a zero. If the queue destroy mailbox
  11845. * command fails this function will return -ENXIO.
  11846. **/
  11847. uint32_t
  11848. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11849. {
  11850. LPFC_MBOXQ_t *mbox;
  11851. int rc, length, status = 0;
  11852. uint32_t shdr_status, shdr_add_status;
  11853. union lpfc_sli4_cfg_shdr *shdr;
  11854. /* sanity check on queue memory */
  11855. if (!eq)
  11856. return -ENODEV;
  11857. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  11858. if (!mbox)
  11859. return -ENOMEM;
  11860. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  11861. sizeof(struct lpfc_sli4_cfg_mhdr));
  11862. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11863. LPFC_MBOX_OPCODE_EQ_DESTROY,
  11864. length, LPFC_SLI4_MBX_EMBED);
  11865. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  11866. eq->queue_id);
  11867. mbox->vport = eq->phba->pport;
  11868. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11869. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  11870. /* The IOCTL status is embedded in the mailbox subheader. */
  11871. shdr = (union lpfc_sli4_cfg_shdr *)
  11872. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  11873. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11874. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11875. if (shdr_status || shdr_add_status || rc) {
  11876. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11877. "2505 EQ_DESTROY mailbox failed with "
  11878. "status x%x add_status x%x, mbx status x%x\n",
  11879. shdr_status, shdr_add_status, rc);
  11880. status = -ENXIO;
  11881. }
  11882. /* Remove eq from any list */
  11883. list_del_init(&eq->list);
  11884. mempool_free(mbox, eq->phba->mbox_mem_pool);
  11885. return status;
  11886. }
  11887. /**
  11888. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  11889. * @cq: The queue structure associated with the queue to destroy.
  11890. *
  11891. * This function destroys a queue, as detailed in @cq by sending an mailbox
  11892. * command, specific to the type of queue, to the HBA.
  11893. *
  11894. * The @cq struct is used to get the queue ID of the queue to destroy.
  11895. *
  11896. * On success this function will return a zero. If the queue destroy mailbox
  11897. * command fails this function will return -ENXIO.
  11898. **/
  11899. uint32_t
  11900. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  11901. {
  11902. LPFC_MBOXQ_t *mbox;
  11903. int rc, length, status = 0;
  11904. uint32_t shdr_status, shdr_add_status;
  11905. union lpfc_sli4_cfg_shdr *shdr;
  11906. /* sanity check on queue memory */
  11907. if (!cq)
  11908. return -ENODEV;
  11909. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  11910. if (!mbox)
  11911. return -ENOMEM;
  11912. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  11913. sizeof(struct lpfc_sli4_cfg_mhdr));
  11914. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11915. LPFC_MBOX_OPCODE_CQ_DESTROY,
  11916. length, LPFC_SLI4_MBX_EMBED);
  11917. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  11918. cq->queue_id);
  11919. mbox->vport = cq->phba->pport;
  11920. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11921. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  11922. /* The IOCTL status is embedded in the mailbox subheader. */
  11923. shdr = (union lpfc_sli4_cfg_shdr *)
  11924. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  11925. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11926. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11927. if (shdr_status || shdr_add_status || rc) {
  11928. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11929. "2506 CQ_DESTROY mailbox failed with "
  11930. "status x%x add_status x%x, mbx status x%x\n",
  11931. shdr_status, shdr_add_status, rc);
  11932. status = -ENXIO;
  11933. }
  11934. /* Remove cq from any list */
  11935. list_del_init(&cq->list);
  11936. mempool_free(mbox, cq->phba->mbox_mem_pool);
  11937. return status;
  11938. }
  11939. /**
  11940. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  11941. * @qm: The queue structure associated with the queue to destroy.
  11942. *
  11943. * This function destroys a queue, as detailed in @mq by sending an mailbox
  11944. * command, specific to the type of queue, to the HBA.
  11945. *
  11946. * The @mq struct is used to get the queue ID of the queue to destroy.
  11947. *
  11948. * On success this function will return a zero. If the queue destroy mailbox
  11949. * command fails this function will return -ENXIO.
  11950. **/
  11951. uint32_t
  11952. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  11953. {
  11954. LPFC_MBOXQ_t *mbox;
  11955. int rc, length, status = 0;
  11956. uint32_t shdr_status, shdr_add_status;
  11957. union lpfc_sli4_cfg_shdr *shdr;
  11958. /* sanity check on queue memory */
  11959. if (!mq)
  11960. return -ENODEV;
  11961. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  11962. if (!mbox)
  11963. return -ENOMEM;
  11964. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  11965. sizeof(struct lpfc_sli4_cfg_mhdr));
  11966. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11967. LPFC_MBOX_OPCODE_MQ_DESTROY,
  11968. length, LPFC_SLI4_MBX_EMBED);
  11969. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  11970. mq->queue_id);
  11971. mbox->vport = mq->phba->pport;
  11972. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11973. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  11974. /* The IOCTL status is embedded in the mailbox subheader. */
  11975. shdr = (union lpfc_sli4_cfg_shdr *)
  11976. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  11977. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11978. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11979. if (shdr_status || shdr_add_status || rc) {
  11980. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11981. "2507 MQ_DESTROY mailbox failed with "
  11982. "status x%x add_status x%x, mbx status x%x\n",
  11983. shdr_status, shdr_add_status, rc);
  11984. status = -ENXIO;
  11985. }
  11986. /* Remove mq from any list */
  11987. list_del_init(&mq->list);
  11988. mempool_free(mbox, mq->phba->mbox_mem_pool);
  11989. return status;
  11990. }
  11991. /**
  11992. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  11993. * @wq: The queue structure associated with the queue to destroy.
  11994. *
  11995. * This function destroys a queue, as detailed in @wq by sending an mailbox
  11996. * command, specific to the type of queue, to the HBA.
  11997. *
  11998. * The @wq struct is used to get the queue ID of the queue to destroy.
  11999. *
  12000. * On success this function will return a zero. If the queue destroy mailbox
  12001. * command fails this function will return -ENXIO.
  12002. **/
  12003. uint32_t
  12004. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  12005. {
  12006. LPFC_MBOXQ_t *mbox;
  12007. int rc, length, status = 0;
  12008. uint32_t shdr_status, shdr_add_status;
  12009. union lpfc_sli4_cfg_shdr *shdr;
  12010. /* sanity check on queue memory */
  12011. if (!wq)
  12012. return -ENODEV;
  12013. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  12014. if (!mbox)
  12015. return -ENOMEM;
  12016. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  12017. sizeof(struct lpfc_sli4_cfg_mhdr));
  12018. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12019. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  12020. length, LPFC_SLI4_MBX_EMBED);
  12021. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  12022. wq->queue_id);
  12023. mbox->vport = wq->phba->pport;
  12024. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12025. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  12026. shdr = (union lpfc_sli4_cfg_shdr *)
  12027. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  12028. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12029. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12030. if (shdr_status || shdr_add_status || rc) {
  12031. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12032. "2508 WQ_DESTROY mailbox failed with "
  12033. "status x%x add_status x%x, mbx status x%x\n",
  12034. shdr_status, shdr_add_status, rc);
  12035. status = -ENXIO;
  12036. }
  12037. /* Remove wq from any list */
  12038. list_del_init(&wq->list);
  12039. mempool_free(mbox, wq->phba->mbox_mem_pool);
  12040. return status;
  12041. }
  12042. /**
  12043. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  12044. * @rq: The queue structure associated with the queue to destroy.
  12045. *
  12046. * This function destroys a queue, as detailed in @rq by sending an mailbox
  12047. * command, specific to the type of queue, to the HBA.
  12048. *
  12049. * The @rq struct is used to get the queue ID of the queue to destroy.
  12050. *
  12051. * On success this function will return a zero. If the queue destroy mailbox
  12052. * command fails this function will return -ENXIO.
  12053. **/
  12054. uint32_t
  12055. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  12056. struct lpfc_queue *drq)
  12057. {
  12058. LPFC_MBOXQ_t *mbox;
  12059. int rc, length, status = 0;
  12060. uint32_t shdr_status, shdr_add_status;
  12061. union lpfc_sli4_cfg_shdr *shdr;
  12062. /* sanity check on queue memory */
  12063. if (!hrq || !drq)
  12064. return -ENODEV;
  12065. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  12066. if (!mbox)
  12067. return -ENOMEM;
  12068. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  12069. sizeof(struct lpfc_sli4_cfg_mhdr));
  12070. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12071. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  12072. length, LPFC_SLI4_MBX_EMBED);
  12073. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12074. hrq->queue_id);
  12075. mbox->vport = hrq->phba->pport;
  12076. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12077. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  12078. /* The IOCTL status is embedded in the mailbox subheader. */
  12079. shdr = (union lpfc_sli4_cfg_shdr *)
  12080. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12081. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12082. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12083. if (shdr_status || shdr_add_status || rc) {
  12084. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12085. "2509 RQ_DESTROY mailbox failed with "
  12086. "status x%x add_status x%x, mbx status x%x\n",
  12087. shdr_status, shdr_add_status, rc);
  12088. if (rc != MBX_TIMEOUT)
  12089. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12090. return -ENXIO;
  12091. }
  12092. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12093. drq->queue_id);
  12094. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  12095. shdr = (union lpfc_sli4_cfg_shdr *)
  12096. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12097. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12098. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12099. if (shdr_status || shdr_add_status || rc) {
  12100. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12101. "2510 RQ_DESTROY mailbox failed with "
  12102. "status x%x add_status x%x, mbx status x%x\n",
  12103. shdr_status, shdr_add_status, rc);
  12104. status = -ENXIO;
  12105. }
  12106. list_del_init(&hrq->list);
  12107. list_del_init(&drq->list);
  12108. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12109. return status;
  12110. }
  12111. /**
  12112. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  12113. * @phba: The virtual port for which this call being executed.
  12114. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  12115. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  12116. * @xritag: the xritag that ties this io to the SGL pages.
  12117. *
  12118. * This routine will post the sgl pages for the IO that has the xritag
  12119. * that is in the iocbq structure. The xritag is assigned during iocbq
  12120. * creation and persists for as long as the driver is loaded.
  12121. * if the caller has fewer than 256 scatter gather segments to map then
  12122. * pdma_phys_addr1 should be 0.
  12123. * If the caller needs to map more than 256 scatter gather segment then
  12124. * pdma_phys_addr1 should be a valid physical address.
  12125. * physical address for SGLs must be 64 byte aligned.
  12126. * If you are going to map 2 SGL's then the first one must have 256 entries
  12127. * the second sgl can have between 1 and 256 entries.
  12128. *
  12129. * Return codes:
  12130. * 0 - Success
  12131. * -ENXIO, -ENOMEM - Failure
  12132. **/
  12133. int
  12134. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  12135. dma_addr_t pdma_phys_addr0,
  12136. dma_addr_t pdma_phys_addr1,
  12137. uint16_t xritag)
  12138. {
  12139. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  12140. LPFC_MBOXQ_t *mbox;
  12141. int rc;
  12142. uint32_t shdr_status, shdr_add_status;
  12143. uint32_t mbox_tmo;
  12144. union lpfc_sli4_cfg_shdr *shdr;
  12145. if (xritag == NO_XRI) {
  12146. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12147. "0364 Invalid param:\n");
  12148. return -EINVAL;
  12149. }
  12150. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12151. if (!mbox)
  12152. return -ENOMEM;
  12153. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12154. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12155. sizeof(struct lpfc_mbx_post_sgl_pages) -
  12156. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  12157. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  12158. &mbox->u.mqe.un.post_sgl_pages;
  12159. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  12160. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  12161. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  12162. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  12163. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  12164. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  12165. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  12166. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  12167. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  12168. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  12169. if (!phba->sli4_hba.intr_enable)
  12170. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12171. else {
  12172. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12173. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12174. }
  12175. /* The IOCTL status is embedded in the mailbox subheader. */
  12176. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  12177. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12178. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12179. if (rc != MBX_TIMEOUT)
  12180. mempool_free(mbox, phba->mbox_mem_pool);
  12181. if (shdr_status || shdr_add_status || rc) {
  12182. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12183. "2511 POST_SGL mailbox failed with "
  12184. "status x%x add_status x%x, mbx status x%x\n",
  12185. shdr_status, shdr_add_status, rc);
  12186. rc = -ENXIO;
  12187. }
  12188. return 0;
  12189. }
  12190. /**
  12191. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  12192. * @phba: pointer to lpfc hba data structure.
  12193. *
  12194. * This routine is invoked to post rpi header templates to the
  12195. * HBA consistent with the SLI-4 interface spec. This routine
  12196. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  12197. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  12198. *
  12199. * Returns
  12200. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  12201. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  12202. **/
  12203. uint16_t
  12204. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  12205. {
  12206. unsigned long xri;
  12207. /*
  12208. * Fetch the next logical xri. Because this index is logical,
  12209. * the driver starts at 0 each time.
  12210. */
  12211. spin_lock_irq(&phba->hbalock);
  12212. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  12213. phba->sli4_hba.max_cfg_param.max_xri, 0);
  12214. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  12215. spin_unlock_irq(&phba->hbalock);
  12216. return NO_XRI;
  12217. } else {
  12218. set_bit(xri, phba->sli4_hba.xri_bmask);
  12219. phba->sli4_hba.max_cfg_param.xri_used++;
  12220. }
  12221. spin_unlock_irq(&phba->hbalock);
  12222. return xri;
  12223. }
  12224. /**
  12225. * lpfc_sli4_free_xri - Release an xri for reuse.
  12226. * @phba: pointer to lpfc hba data structure.
  12227. *
  12228. * This routine is invoked to release an xri to the pool of
  12229. * available rpis maintained by the driver.
  12230. **/
  12231. void
  12232. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12233. {
  12234. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  12235. phba->sli4_hba.max_cfg_param.xri_used--;
  12236. }
  12237. }
  12238. /**
  12239. * lpfc_sli4_free_xri - Release an xri for reuse.
  12240. * @phba: pointer to lpfc hba data structure.
  12241. *
  12242. * This routine is invoked to release an xri to the pool of
  12243. * available rpis maintained by the driver.
  12244. **/
  12245. void
  12246. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12247. {
  12248. spin_lock_irq(&phba->hbalock);
  12249. __lpfc_sli4_free_xri(phba, xri);
  12250. spin_unlock_irq(&phba->hbalock);
  12251. }
  12252. /**
  12253. * lpfc_sli4_next_xritag - Get an xritag for the io
  12254. * @phba: Pointer to HBA context object.
  12255. *
  12256. * This function gets an xritag for the iocb. If there is no unused xritag
  12257. * it will return 0xffff.
  12258. * The function returns the allocated xritag if successful, else returns zero.
  12259. * Zero is not a valid xritag.
  12260. * The caller is not required to hold any lock.
  12261. **/
  12262. uint16_t
  12263. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  12264. {
  12265. uint16_t xri_index;
  12266. xri_index = lpfc_sli4_alloc_xri(phba);
  12267. if (xri_index == NO_XRI)
  12268. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  12269. "2004 Failed to allocate XRI.last XRITAG is %d"
  12270. " Max XRI is %d, Used XRI is %d\n",
  12271. xri_index,
  12272. phba->sli4_hba.max_cfg_param.max_xri,
  12273. phba->sli4_hba.max_cfg_param.xri_used);
  12274. return xri_index;
  12275. }
  12276. /**
  12277. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  12278. * @phba: pointer to lpfc hba data structure.
  12279. * @post_sgl_list: pointer to els sgl entry list.
  12280. * @count: number of els sgl entries on the list.
  12281. *
  12282. * This routine is invoked to post a block of driver's sgl pages to the
  12283. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12284. * is only called when the driver is loading and after all IO has been
  12285. * stopped.
  12286. **/
  12287. static int
  12288. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
  12289. struct list_head *post_sgl_list,
  12290. int post_cnt)
  12291. {
  12292. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  12293. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12294. struct sgl_page_pairs *sgl_pg_pairs;
  12295. void *viraddr;
  12296. LPFC_MBOXQ_t *mbox;
  12297. uint32_t reqlen, alloclen, pg_pairs;
  12298. uint32_t mbox_tmo;
  12299. uint16_t xritag_start = 0;
  12300. int rc = 0;
  12301. uint32_t shdr_status, shdr_add_status;
  12302. union lpfc_sli4_cfg_shdr *shdr;
  12303. reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12304. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12305. if (reqlen > SLI4_PAGE_SIZE) {
  12306. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12307. "2559 Block sgl registration required DMA "
  12308. "size (%d) great than a page\n", reqlen);
  12309. return -ENOMEM;
  12310. }
  12311. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12312. if (!mbox)
  12313. return -ENOMEM;
  12314. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12315. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12316. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12317. LPFC_SLI4_MBX_NEMBED);
  12318. if (alloclen < reqlen) {
  12319. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12320. "0285 Allocated DMA memory size (%d) is "
  12321. "less than the requested DMA memory "
  12322. "size (%d)\n", alloclen, reqlen);
  12323. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12324. return -ENOMEM;
  12325. }
  12326. /* Set up the SGL pages in the non-embedded DMA pages */
  12327. viraddr = mbox->sge_array->addr[0];
  12328. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12329. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12330. pg_pairs = 0;
  12331. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  12332. /* Set up the sge entry */
  12333. sgl_pg_pairs->sgl_pg0_addr_lo =
  12334. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12335. sgl_pg_pairs->sgl_pg0_addr_hi =
  12336. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12337. sgl_pg_pairs->sgl_pg1_addr_lo =
  12338. cpu_to_le32(putPaddrLow(0));
  12339. sgl_pg_pairs->sgl_pg1_addr_hi =
  12340. cpu_to_le32(putPaddrHigh(0));
  12341. /* Keep the first xritag on the list */
  12342. if (pg_pairs == 0)
  12343. xritag_start = sglq_entry->sli4_xritag;
  12344. sgl_pg_pairs++;
  12345. pg_pairs++;
  12346. }
  12347. /* Complete initialization and perform endian conversion. */
  12348. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12349. bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
  12350. sgl->word0 = cpu_to_le32(sgl->word0);
  12351. if (!phba->sli4_hba.intr_enable)
  12352. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12353. else {
  12354. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12355. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12356. }
  12357. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12358. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12359. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12360. if (rc != MBX_TIMEOUT)
  12361. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12362. if (shdr_status || shdr_add_status || rc) {
  12363. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12364. "2513 POST_SGL_BLOCK mailbox command failed "
  12365. "status x%x add_status x%x mbx status x%x\n",
  12366. shdr_status, shdr_add_status, rc);
  12367. rc = -ENXIO;
  12368. }
  12369. return rc;
  12370. }
  12371. /**
  12372. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12373. * @phba: pointer to lpfc hba data structure.
  12374. * @sblist: pointer to scsi buffer list.
  12375. * @count: number of scsi buffers on the list.
  12376. *
  12377. * This routine is invoked to post a block of @count scsi sgl pages from a
  12378. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12379. * No Lock is held.
  12380. *
  12381. **/
  12382. int
  12383. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  12384. struct list_head *sblist,
  12385. int count)
  12386. {
  12387. struct lpfc_scsi_buf *psb;
  12388. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12389. struct sgl_page_pairs *sgl_pg_pairs;
  12390. void *viraddr;
  12391. LPFC_MBOXQ_t *mbox;
  12392. uint32_t reqlen, alloclen, pg_pairs;
  12393. uint32_t mbox_tmo;
  12394. uint16_t xritag_start = 0;
  12395. int rc = 0;
  12396. uint32_t shdr_status, shdr_add_status;
  12397. dma_addr_t pdma_phys_bpl1;
  12398. union lpfc_sli4_cfg_shdr *shdr;
  12399. /* Calculate the requested length of the dma memory */
  12400. reqlen = count * sizeof(struct sgl_page_pairs) +
  12401. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12402. if (reqlen > SLI4_PAGE_SIZE) {
  12403. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12404. "0217 Block sgl registration required DMA "
  12405. "size (%d) great than a page\n", reqlen);
  12406. return -ENOMEM;
  12407. }
  12408. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12409. if (!mbox) {
  12410. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12411. "0283 Failed to allocate mbox cmd memory\n");
  12412. return -ENOMEM;
  12413. }
  12414. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12415. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12416. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12417. LPFC_SLI4_MBX_NEMBED);
  12418. if (alloclen < reqlen) {
  12419. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12420. "2561 Allocated DMA memory size (%d) is "
  12421. "less than the requested DMA memory "
  12422. "size (%d)\n", alloclen, reqlen);
  12423. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12424. return -ENOMEM;
  12425. }
  12426. /* Get the first SGE entry from the non-embedded DMA memory */
  12427. viraddr = mbox->sge_array->addr[0];
  12428. /* Set up the SGL pages in the non-embedded DMA pages */
  12429. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12430. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12431. pg_pairs = 0;
  12432. list_for_each_entry(psb, sblist, list) {
  12433. /* Set up the sge entry */
  12434. sgl_pg_pairs->sgl_pg0_addr_lo =
  12435. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12436. sgl_pg_pairs->sgl_pg0_addr_hi =
  12437. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12438. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12439. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12440. else
  12441. pdma_phys_bpl1 = 0;
  12442. sgl_pg_pairs->sgl_pg1_addr_lo =
  12443. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12444. sgl_pg_pairs->sgl_pg1_addr_hi =
  12445. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12446. /* Keep the first xritag on the list */
  12447. if (pg_pairs == 0)
  12448. xritag_start = psb->cur_iocbq.sli4_xritag;
  12449. sgl_pg_pairs++;
  12450. pg_pairs++;
  12451. }
  12452. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12453. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12454. /* Perform endian conversion if necessary */
  12455. sgl->word0 = cpu_to_le32(sgl->word0);
  12456. if (!phba->sli4_hba.intr_enable)
  12457. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12458. else {
  12459. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12460. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12461. }
  12462. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12463. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12464. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12465. if (rc != MBX_TIMEOUT)
  12466. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12467. if (shdr_status || shdr_add_status || rc) {
  12468. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12469. "2564 POST_SGL_BLOCK mailbox command failed "
  12470. "status x%x add_status x%x mbx status x%x\n",
  12471. shdr_status, shdr_add_status, rc);
  12472. rc = -ENXIO;
  12473. }
  12474. return rc;
  12475. }
  12476. /**
  12477. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12478. * @phba: pointer to lpfc_hba struct that the frame was received on
  12479. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12480. *
  12481. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12482. * valid type of frame that the LPFC driver will handle. This function will
  12483. * return a zero if the frame is a valid frame or a non zero value when the
  12484. * frame does not pass the check.
  12485. **/
  12486. static int
  12487. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12488. {
  12489. /* make rctl_names static to save stack space */
  12490. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12491. char *type_names[] = FC_TYPE_NAMES_INIT;
  12492. struct fc_vft_header *fc_vft_hdr;
  12493. uint32_t *header = (uint32_t *) fc_hdr;
  12494. switch (fc_hdr->fh_r_ctl) {
  12495. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12496. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12497. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12498. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12499. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12500. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12501. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12502. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12503. case FC_RCTL_ELS_REQ: /* extended link services request */
  12504. case FC_RCTL_ELS_REP: /* extended link services reply */
  12505. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12506. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12507. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12508. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12509. case FC_RCTL_BA_RMC: /* remove connection */
  12510. case FC_RCTL_BA_ACC: /* basic accept */
  12511. case FC_RCTL_BA_RJT: /* basic reject */
  12512. case FC_RCTL_BA_PRMT:
  12513. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12514. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12515. case FC_RCTL_P_RJT: /* port reject */
  12516. case FC_RCTL_F_RJT: /* fabric reject */
  12517. case FC_RCTL_P_BSY: /* port busy */
  12518. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12519. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12520. case FC_RCTL_LCR: /* link credit reset */
  12521. case FC_RCTL_END: /* end */
  12522. break;
  12523. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12524. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12525. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12526. return lpfc_fc_frame_check(phba, fc_hdr);
  12527. default:
  12528. goto drop;
  12529. }
  12530. switch (fc_hdr->fh_type) {
  12531. case FC_TYPE_BLS:
  12532. case FC_TYPE_ELS:
  12533. case FC_TYPE_FCP:
  12534. case FC_TYPE_CT:
  12535. break;
  12536. case FC_TYPE_IP:
  12537. case FC_TYPE_ILS:
  12538. default:
  12539. goto drop;
  12540. }
  12541. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12542. "2538 Received frame rctl:%s type:%s "
  12543. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12544. rctl_names[fc_hdr->fh_r_ctl],
  12545. type_names[fc_hdr->fh_type],
  12546. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12547. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12548. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12549. return 0;
  12550. drop:
  12551. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12552. "2539 Dropped frame rctl:%s type:%s\n",
  12553. rctl_names[fc_hdr->fh_r_ctl],
  12554. type_names[fc_hdr->fh_type]);
  12555. return 1;
  12556. }
  12557. /**
  12558. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12559. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12560. *
  12561. * This function processes the FC header to retrieve the VFI from the VF
  12562. * header, if one exists. This function will return the VFI if one exists
  12563. * or 0 if no VSAN Header exists.
  12564. **/
  12565. static uint32_t
  12566. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12567. {
  12568. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12569. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12570. return 0;
  12571. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12572. }
  12573. /**
  12574. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12575. * @phba: Pointer to the HBA structure to search for the vport on
  12576. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12577. * @fcfi: The FC Fabric ID that the frame came from
  12578. *
  12579. * This function searches the @phba for a vport that matches the content of the
  12580. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12581. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12582. * returns the matching vport pointer or NULL if unable to match frame to a
  12583. * vport.
  12584. **/
  12585. static struct lpfc_vport *
  12586. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12587. uint16_t fcfi)
  12588. {
  12589. struct lpfc_vport **vports;
  12590. struct lpfc_vport *vport = NULL;
  12591. int i;
  12592. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12593. fc_hdr->fh_d_id[1] << 8 |
  12594. fc_hdr->fh_d_id[2]);
  12595. if (did == Fabric_DID)
  12596. return phba->pport;
  12597. if ((phba->pport->fc_flag & FC_PT2PT) &&
  12598. !(phba->link_state == LPFC_HBA_READY))
  12599. return phba->pport;
  12600. vports = lpfc_create_vport_work_array(phba);
  12601. if (vports != NULL)
  12602. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12603. if (phba->fcf.fcfi == fcfi &&
  12604. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12605. vports[i]->fc_myDID == did) {
  12606. vport = vports[i];
  12607. break;
  12608. }
  12609. }
  12610. lpfc_destroy_vport_work_array(phba, vports);
  12611. return vport;
  12612. }
  12613. /**
  12614. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12615. * @vport: The vport to work on.
  12616. *
  12617. * This function updates the receive sequence time stamp for this vport. The
  12618. * receive sequence time stamp indicates the time that the last frame of the
  12619. * the sequence that has been idle for the longest amount of time was received.
  12620. * the driver uses this time stamp to indicate if any received sequences have
  12621. * timed out.
  12622. **/
  12623. void
  12624. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12625. {
  12626. struct lpfc_dmabuf *h_buf;
  12627. struct hbq_dmabuf *dmabuf = NULL;
  12628. /* get the oldest sequence on the rcv list */
  12629. h_buf = list_get_first(&vport->rcv_buffer_list,
  12630. struct lpfc_dmabuf, list);
  12631. if (!h_buf)
  12632. return;
  12633. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12634. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12635. }
  12636. /**
  12637. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12638. * @vport: The vport that the received sequences were sent to.
  12639. *
  12640. * This function cleans up all outstanding received sequences. This is called
  12641. * by the driver when a link event or user action invalidates all the received
  12642. * sequences.
  12643. **/
  12644. void
  12645. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12646. {
  12647. struct lpfc_dmabuf *h_buf, *hnext;
  12648. struct lpfc_dmabuf *d_buf, *dnext;
  12649. struct hbq_dmabuf *dmabuf = NULL;
  12650. /* start with the oldest sequence on the rcv list */
  12651. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12652. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12653. list_del_init(&dmabuf->hbuf.list);
  12654. list_for_each_entry_safe(d_buf, dnext,
  12655. &dmabuf->dbuf.list, list) {
  12656. list_del_init(&d_buf->list);
  12657. lpfc_in_buf_free(vport->phba, d_buf);
  12658. }
  12659. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12660. }
  12661. }
  12662. /**
  12663. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12664. * @vport: The vport that the received sequences were sent to.
  12665. *
  12666. * This function determines whether any received sequences have timed out by
  12667. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12668. * indicates that there is at least one timed out sequence this routine will
  12669. * go through the received sequences one at a time from most inactive to most
  12670. * active to determine which ones need to be cleaned up. Once it has determined
  12671. * that a sequence needs to be cleaned up it will simply free up the resources
  12672. * without sending an abort.
  12673. **/
  12674. void
  12675. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12676. {
  12677. struct lpfc_dmabuf *h_buf, *hnext;
  12678. struct lpfc_dmabuf *d_buf, *dnext;
  12679. struct hbq_dmabuf *dmabuf = NULL;
  12680. unsigned long timeout;
  12681. int abort_count = 0;
  12682. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12683. vport->rcv_buffer_time_stamp);
  12684. if (list_empty(&vport->rcv_buffer_list) ||
  12685. time_before(jiffies, timeout))
  12686. return;
  12687. /* start with the oldest sequence on the rcv list */
  12688. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12689. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12690. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12691. dmabuf->time_stamp);
  12692. if (time_before(jiffies, timeout))
  12693. break;
  12694. abort_count++;
  12695. list_del_init(&dmabuf->hbuf.list);
  12696. list_for_each_entry_safe(d_buf, dnext,
  12697. &dmabuf->dbuf.list, list) {
  12698. list_del_init(&d_buf->list);
  12699. lpfc_in_buf_free(vport->phba, d_buf);
  12700. }
  12701. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12702. }
  12703. if (abort_count)
  12704. lpfc_update_rcv_time_stamp(vport);
  12705. }
  12706. /**
  12707. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12708. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12709. *
  12710. * This function searches through the existing incomplete sequences that have
  12711. * been sent to this @vport. If the frame matches one of the incomplete
  12712. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12713. * make up that sequence. If no sequence is found that matches this frame then
  12714. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12715. * This function returns a pointer to the first dmabuf in the sequence list that
  12716. * the frame was linked to.
  12717. **/
  12718. static struct hbq_dmabuf *
  12719. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12720. {
  12721. struct fc_frame_header *new_hdr;
  12722. struct fc_frame_header *temp_hdr;
  12723. struct lpfc_dmabuf *d_buf;
  12724. struct lpfc_dmabuf *h_buf;
  12725. struct hbq_dmabuf *seq_dmabuf = NULL;
  12726. struct hbq_dmabuf *temp_dmabuf = NULL;
  12727. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12728. dmabuf->time_stamp = jiffies;
  12729. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12730. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12731. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12732. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12733. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12734. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12735. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12736. continue;
  12737. /* found a pending sequence that matches this frame */
  12738. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12739. break;
  12740. }
  12741. if (!seq_dmabuf) {
  12742. /*
  12743. * This indicates first frame received for this sequence.
  12744. * Queue the buffer on the vport's rcv_buffer_list.
  12745. */
  12746. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12747. lpfc_update_rcv_time_stamp(vport);
  12748. return dmabuf;
  12749. }
  12750. temp_hdr = seq_dmabuf->hbuf.virt;
  12751. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12752. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12753. list_del_init(&seq_dmabuf->hbuf.list);
  12754. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12755. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12756. lpfc_update_rcv_time_stamp(vport);
  12757. return dmabuf;
  12758. }
  12759. /* move this sequence to the tail to indicate a young sequence */
  12760. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12761. seq_dmabuf->time_stamp = jiffies;
  12762. lpfc_update_rcv_time_stamp(vport);
  12763. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12764. temp_hdr = dmabuf->hbuf.virt;
  12765. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12766. return seq_dmabuf;
  12767. }
  12768. /* find the correct place in the sequence to insert this frame */
  12769. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12770. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12771. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12772. /*
  12773. * If the frame's sequence count is greater than the frame on
  12774. * the list then insert the frame right after this frame
  12775. */
  12776. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  12777. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12778. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  12779. return seq_dmabuf;
  12780. }
  12781. }
  12782. return NULL;
  12783. }
  12784. /**
  12785. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  12786. * @vport: pointer to a vitural port
  12787. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12788. *
  12789. * This function tries to abort from the partially assembed sequence, described
  12790. * by the information from basic abbort @dmabuf. It checks to see whether such
  12791. * partially assembled sequence held by the driver. If so, it shall free up all
  12792. * the frames from the partially assembled sequence.
  12793. *
  12794. * Return
  12795. * true -- if there is matching partially assembled sequence present and all
  12796. * the frames freed with the sequence;
  12797. * false -- if there is no matching partially assembled sequence present so
  12798. * nothing got aborted in the lower layer driver
  12799. **/
  12800. static bool
  12801. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  12802. struct hbq_dmabuf *dmabuf)
  12803. {
  12804. struct fc_frame_header *new_hdr;
  12805. struct fc_frame_header *temp_hdr;
  12806. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  12807. struct hbq_dmabuf *seq_dmabuf = NULL;
  12808. /* Use the hdr_buf to find the sequence that matches this frame */
  12809. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12810. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  12811. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12812. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12813. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12814. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12815. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12816. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12817. continue;
  12818. /* found a pending sequence that matches this frame */
  12819. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12820. break;
  12821. }
  12822. /* Free up all the frames from the partially assembled sequence */
  12823. if (seq_dmabuf) {
  12824. list_for_each_entry_safe(d_buf, n_buf,
  12825. &seq_dmabuf->dbuf.list, list) {
  12826. list_del_init(&d_buf->list);
  12827. lpfc_in_buf_free(vport->phba, d_buf);
  12828. }
  12829. return true;
  12830. }
  12831. return false;
  12832. }
  12833. /**
  12834. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  12835. * @phba: Pointer to HBA context object.
  12836. * @cmd_iocbq: pointer to the command iocbq structure.
  12837. * @rsp_iocbq: pointer to the response iocbq structure.
  12838. *
  12839. * This function handles the sequence abort response iocb command complete
  12840. * event. It properly releases the memory allocated to the sequence abort
  12841. * accept iocb.
  12842. **/
  12843. static void
  12844. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  12845. struct lpfc_iocbq *cmd_iocbq,
  12846. struct lpfc_iocbq *rsp_iocbq)
  12847. {
  12848. if (cmd_iocbq)
  12849. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  12850. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  12851. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  12852. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12853. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  12854. rsp_iocbq->iocb.ulpStatus,
  12855. rsp_iocbq->iocb.un.ulpWord[4]);
  12856. }
  12857. /**
  12858. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  12859. * @phba: Pointer to HBA context object.
  12860. * @xri: xri id in transaction.
  12861. *
  12862. * This function validates the xri maps to the known range of XRIs allocated an
  12863. * used by the driver.
  12864. **/
  12865. uint16_t
  12866. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  12867. uint16_t xri)
  12868. {
  12869. int i;
  12870. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  12871. if (xri == phba->sli4_hba.xri_ids[i])
  12872. return i;
  12873. }
  12874. return NO_XRI;
  12875. }
  12876. /**
  12877. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  12878. * @phba: Pointer to HBA context object.
  12879. * @fc_hdr: pointer to a FC frame header.
  12880. *
  12881. * This function sends a basic response to a previous unsol sequence abort
  12882. * event after aborting the sequence handling.
  12883. **/
  12884. static void
  12885. lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
  12886. struct fc_frame_header *fc_hdr)
  12887. {
  12888. struct lpfc_iocbq *ctiocb = NULL;
  12889. struct lpfc_nodelist *ndlp;
  12890. uint16_t oxid, rxid, xri, lxri;
  12891. uint32_t sid, fctl;
  12892. IOCB_t *icmd;
  12893. int rc;
  12894. if (!lpfc_is_link_up(phba))
  12895. return;
  12896. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12897. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  12898. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  12899. ndlp = lpfc_findnode_did(phba->pport, sid);
  12900. if (!ndlp) {
  12901. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12902. "1268 Find ndlp returned NULL for oxid:x%x "
  12903. "SID:x%x\n", oxid, sid);
  12904. return;
  12905. }
  12906. /* Allocate buffer for rsp iocb */
  12907. ctiocb = lpfc_sli_get_iocbq(phba);
  12908. if (!ctiocb)
  12909. return;
  12910. /* Extract the F_CTL field from FC_HDR */
  12911. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  12912. icmd = &ctiocb->iocb;
  12913. icmd->un.xseq64.bdl.bdeSize = 0;
  12914. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  12915. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  12916. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  12917. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  12918. /* Fill in the rest of iocb fields */
  12919. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  12920. icmd->ulpBdeCount = 0;
  12921. icmd->ulpLe = 1;
  12922. icmd->ulpClass = CLASS3;
  12923. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  12924. ctiocb->context1 = ndlp;
  12925. ctiocb->iocb_cmpl = NULL;
  12926. ctiocb->vport = phba->pport;
  12927. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  12928. ctiocb->sli4_lxritag = NO_XRI;
  12929. ctiocb->sli4_xritag = NO_XRI;
  12930. if (fctl & FC_FC_EX_CTX)
  12931. /* Exchange responder sent the abort so we
  12932. * own the oxid.
  12933. */
  12934. xri = oxid;
  12935. else
  12936. xri = rxid;
  12937. lxri = lpfc_sli4_xri_inrange(phba, xri);
  12938. if (lxri != NO_XRI)
  12939. lpfc_set_rrq_active(phba, ndlp, lxri,
  12940. (xri == oxid) ? rxid : oxid, 0);
  12941. /* If the oxid maps to the FCP XRI range or if it is out of range,
  12942. * send a BLS_RJT. The driver no longer has that exchange.
  12943. * Override the IOCB for a BA_RJT.
  12944. */
  12945. if (xri > (phba->sli4_hba.max_cfg_param.max_xri +
  12946. phba->sli4_hba.max_cfg_param.xri_base) ||
  12947. xri > (lpfc_sli4_get_els_iocb_cnt(phba) +
  12948. phba->sli4_hba.max_cfg_param.xri_base)) {
  12949. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  12950. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  12951. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  12952. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  12953. }
  12954. if (fctl & FC_FC_EX_CTX) {
  12955. /* ABTS sent by responder to CT exchange, construction
  12956. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  12957. * field and RX_ID from ABTS for RX_ID field.
  12958. */
  12959. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  12960. } else {
  12961. /* ABTS sent by initiator to CT exchange, construction
  12962. * of BA_ACC will need to allocate a new XRI as for the
  12963. * XRI_TAG field.
  12964. */
  12965. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  12966. }
  12967. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  12968. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  12969. /* Xmit CT abts response on exchange <xid> */
  12970. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12971. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  12972. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  12973. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  12974. if (rc == IOCB_ERROR) {
  12975. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  12976. "2925 Failed to issue CT ABTS RSP x%x on "
  12977. "xri x%x, Data x%x\n",
  12978. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  12979. phba->link_state);
  12980. lpfc_sli_release_iocbq(phba, ctiocb);
  12981. }
  12982. }
  12983. /**
  12984. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  12985. * @vport: Pointer to the vport on which this sequence was received
  12986. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12987. *
  12988. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  12989. * receive sequence is only partially assembed by the driver, it shall abort
  12990. * the partially assembled frames for the sequence. Otherwise, if the
  12991. * unsolicited receive sequence has been completely assembled and passed to
  12992. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  12993. * unsolicited sequence has been aborted. After that, it will issue a basic
  12994. * accept to accept the abort.
  12995. **/
  12996. void
  12997. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  12998. struct hbq_dmabuf *dmabuf)
  12999. {
  13000. struct lpfc_hba *phba = vport->phba;
  13001. struct fc_frame_header fc_hdr;
  13002. uint32_t fctl;
  13003. bool abts_par;
  13004. /* Make a copy of fc_hdr before the dmabuf being released */
  13005. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  13006. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  13007. if (fctl & FC_FC_EX_CTX) {
  13008. /*
  13009. * ABTS sent by responder to exchange, just free the buffer
  13010. */
  13011. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13012. } else {
  13013. /*
  13014. * ABTS sent by initiator to exchange, need to do cleanup
  13015. */
  13016. /* Try to abort partially assembled seq */
  13017. abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  13018. /* Send abort to ULP if partially seq abort failed */
  13019. if (abts_par == false)
  13020. lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
  13021. else
  13022. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13023. }
  13024. /* Send basic accept (BA_ACC) to the abort requester */
  13025. lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
  13026. }
  13027. /**
  13028. * lpfc_seq_complete - Indicates if a sequence is complete
  13029. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13030. *
  13031. * This function checks the sequence, starting with the frame described by
  13032. * @dmabuf, to see if all the frames associated with this sequence are present.
  13033. * the frames associated with this sequence are linked to the @dmabuf using the
  13034. * dbuf list. This function looks for two major things. 1) That the first frame
  13035. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  13036. * set. 3) That there are no holes in the sequence count. The function will
  13037. * return 1 when the sequence is complete, otherwise it will return 0.
  13038. **/
  13039. static int
  13040. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  13041. {
  13042. struct fc_frame_header *hdr;
  13043. struct lpfc_dmabuf *d_buf;
  13044. struct hbq_dmabuf *seq_dmabuf;
  13045. uint32_t fctl;
  13046. int seq_count = 0;
  13047. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13048. /* make sure first fame of sequence has a sequence count of zero */
  13049. if (hdr->fh_seq_cnt != seq_count)
  13050. return 0;
  13051. fctl = (hdr->fh_f_ctl[0] << 16 |
  13052. hdr->fh_f_ctl[1] << 8 |
  13053. hdr->fh_f_ctl[2]);
  13054. /* If last frame of sequence we can return success. */
  13055. if (fctl & FC_FC_END_SEQ)
  13056. return 1;
  13057. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  13058. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13059. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13060. /* If there is a hole in the sequence count then fail. */
  13061. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  13062. return 0;
  13063. fctl = (hdr->fh_f_ctl[0] << 16 |
  13064. hdr->fh_f_ctl[1] << 8 |
  13065. hdr->fh_f_ctl[2]);
  13066. /* If last frame of sequence we can return success. */
  13067. if (fctl & FC_FC_END_SEQ)
  13068. return 1;
  13069. }
  13070. return 0;
  13071. }
  13072. /**
  13073. * lpfc_prep_seq - Prep sequence for ULP processing
  13074. * @vport: Pointer to the vport on which this sequence was received
  13075. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13076. *
  13077. * This function takes a sequence, described by a list of frames, and creates
  13078. * a list of iocbq structures to describe the sequence. This iocbq list will be
  13079. * used to issue to the generic unsolicited sequence handler. This routine
  13080. * returns a pointer to the first iocbq in the list. If the function is unable
  13081. * to allocate an iocbq then it throw out the received frames that were not
  13082. * able to be described and return a pointer to the first iocbq. If unable to
  13083. * allocate any iocbqs (including the first) this function will return NULL.
  13084. **/
  13085. static struct lpfc_iocbq *
  13086. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  13087. {
  13088. struct hbq_dmabuf *hbq_buf;
  13089. struct lpfc_dmabuf *d_buf, *n_buf;
  13090. struct lpfc_iocbq *first_iocbq, *iocbq;
  13091. struct fc_frame_header *fc_hdr;
  13092. uint32_t sid;
  13093. uint32_t len, tot_len;
  13094. struct ulp_bde64 *pbde;
  13095. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13096. /* remove from receive buffer list */
  13097. list_del_init(&seq_dmabuf->hbuf.list);
  13098. lpfc_update_rcv_time_stamp(vport);
  13099. /* get the Remote Port's SID */
  13100. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13101. tot_len = 0;
  13102. /* Get an iocbq struct to fill in. */
  13103. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  13104. if (first_iocbq) {
  13105. /* Initialize the first IOCB. */
  13106. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  13107. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  13108. /* Check FC Header to see what TYPE of frame we are rcv'ing */
  13109. if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
  13110. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
  13111. first_iocbq->iocb.un.rcvels.parmRo =
  13112. sli4_did_from_fc_hdr(fc_hdr);
  13113. first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
  13114. } else
  13115. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  13116. first_iocbq->iocb.ulpContext = NO_XRI;
  13117. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  13118. be16_to_cpu(fc_hdr->fh_ox_id);
  13119. /* iocbq is prepped for internal consumption. Physical vpi. */
  13120. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  13121. vport->phba->vpi_ids[vport->vpi];
  13122. /* put the first buffer into the first IOCBq */
  13123. first_iocbq->context2 = &seq_dmabuf->dbuf;
  13124. first_iocbq->context3 = NULL;
  13125. first_iocbq->iocb.ulpBdeCount = 1;
  13126. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13127. LPFC_DATA_BUF_SIZE;
  13128. first_iocbq->iocb.un.rcvels.remoteID = sid;
  13129. tot_len = bf_get(lpfc_rcqe_length,
  13130. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  13131. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13132. }
  13133. iocbq = first_iocbq;
  13134. /*
  13135. * Each IOCBq can have two Buffers assigned, so go through the list
  13136. * of buffers for this sequence and save two buffers in each IOCBq
  13137. */
  13138. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13139. if (!iocbq) {
  13140. lpfc_in_buf_free(vport->phba, d_buf);
  13141. continue;
  13142. }
  13143. if (!iocbq->context3) {
  13144. iocbq->context3 = d_buf;
  13145. iocbq->iocb.ulpBdeCount++;
  13146. pbde = (struct ulp_bde64 *)
  13147. &iocbq->iocb.unsli3.sli3Words[4];
  13148. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13149. /* We need to get the size out of the right CQE */
  13150. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13151. len = bf_get(lpfc_rcqe_length,
  13152. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13153. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13154. tot_len += len;
  13155. } else {
  13156. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13157. if (!iocbq) {
  13158. if (first_iocbq) {
  13159. first_iocbq->iocb.ulpStatus =
  13160. IOSTAT_FCP_RSP_ERROR;
  13161. first_iocbq->iocb.un.ulpWord[4] =
  13162. IOERR_NO_RESOURCES;
  13163. }
  13164. lpfc_in_buf_free(vport->phba, d_buf);
  13165. continue;
  13166. }
  13167. iocbq->context2 = d_buf;
  13168. iocbq->context3 = NULL;
  13169. iocbq->iocb.ulpBdeCount = 1;
  13170. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13171. LPFC_DATA_BUF_SIZE;
  13172. /* We need to get the size out of the right CQE */
  13173. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13174. len = bf_get(lpfc_rcqe_length,
  13175. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13176. tot_len += len;
  13177. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13178. iocbq->iocb.un.rcvels.remoteID = sid;
  13179. list_add_tail(&iocbq->list, &first_iocbq->list);
  13180. }
  13181. }
  13182. return first_iocbq;
  13183. }
  13184. static void
  13185. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13186. struct hbq_dmabuf *seq_dmabuf)
  13187. {
  13188. struct fc_frame_header *fc_hdr;
  13189. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13190. struct lpfc_hba *phba = vport->phba;
  13191. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13192. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13193. if (!iocbq) {
  13194. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13195. "2707 Ring %d handler: Failed to allocate "
  13196. "iocb Rctl x%x Type x%x received\n",
  13197. LPFC_ELS_RING,
  13198. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13199. return;
  13200. }
  13201. if (!lpfc_complete_unsol_iocb(phba,
  13202. &phba->sli.ring[LPFC_ELS_RING],
  13203. iocbq, fc_hdr->fh_r_ctl,
  13204. fc_hdr->fh_type))
  13205. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13206. "2540 Ring %d handler: unexpected Rctl "
  13207. "x%x Type x%x received\n",
  13208. LPFC_ELS_RING,
  13209. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13210. /* Free iocb created in lpfc_prep_seq */
  13211. list_for_each_entry_safe(curr_iocb, next_iocb,
  13212. &iocbq->list, list) {
  13213. list_del_init(&curr_iocb->list);
  13214. lpfc_sli_release_iocbq(phba, curr_iocb);
  13215. }
  13216. lpfc_sli_release_iocbq(phba, iocbq);
  13217. }
  13218. /**
  13219. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13220. * @phba: Pointer to HBA context object.
  13221. *
  13222. * This function is called with no lock held. This function processes all
  13223. * the received buffers and gives it to upper layers when a received buffer
  13224. * indicates that it is the final frame in the sequence. The interrupt
  13225. * service routine processes received buffers at interrupt contexts and adds
  13226. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13227. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13228. * appropriate receive function when the final frame in a sequence is received.
  13229. **/
  13230. void
  13231. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13232. struct hbq_dmabuf *dmabuf)
  13233. {
  13234. struct hbq_dmabuf *seq_dmabuf;
  13235. struct fc_frame_header *fc_hdr;
  13236. struct lpfc_vport *vport;
  13237. uint32_t fcfi;
  13238. uint32_t did;
  13239. /* Process each received buffer */
  13240. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13241. /* check to see if this a valid type of frame */
  13242. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13243. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13244. return;
  13245. }
  13246. if ((bf_get(lpfc_cqe_code,
  13247. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13248. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13249. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13250. else
  13251. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13252. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13253. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13254. if (!vport) {
  13255. /* throw out the frame */
  13256. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13257. return;
  13258. }
  13259. /* d_id this frame is directed to */
  13260. did = sli4_did_from_fc_hdr(fc_hdr);
  13261. /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
  13262. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
  13263. (did != Fabric_DID)) {
  13264. /*
  13265. * Throw out the frame if we are not pt2pt.
  13266. * The pt2pt protocol allows for discovery frames
  13267. * to be received without a registered VPI.
  13268. */
  13269. if (!(vport->fc_flag & FC_PT2PT) ||
  13270. (phba->link_state == LPFC_HBA_READY)) {
  13271. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13272. return;
  13273. }
  13274. }
  13275. /* Handle the basic abort sequence (BA_ABTS) event */
  13276. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13277. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13278. return;
  13279. }
  13280. /* Link this frame */
  13281. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13282. if (!seq_dmabuf) {
  13283. /* unable to add frame to vport - throw it out */
  13284. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13285. return;
  13286. }
  13287. /* If not last frame in sequence continue processing frames. */
  13288. if (!lpfc_seq_complete(seq_dmabuf))
  13289. return;
  13290. /* Send the complete sequence to the upper layer protocol */
  13291. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13292. }
  13293. /**
  13294. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13295. * @phba: pointer to lpfc hba data structure.
  13296. *
  13297. * This routine is invoked to post rpi header templates to the
  13298. * HBA consistent with the SLI-4 interface spec. This routine
  13299. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13300. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13301. *
  13302. * This routine does not require any locks. It's usage is expected
  13303. * to be driver load or reset recovery when the driver is
  13304. * sequential.
  13305. *
  13306. * Return codes
  13307. * 0 - successful
  13308. * -EIO - The mailbox failed to complete successfully.
  13309. * When this error occurs, the driver is not guaranteed
  13310. * to have any rpi regions posted to the device and
  13311. * must either attempt to repost the regions or take a
  13312. * fatal error.
  13313. **/
  13314. int
  13315. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13316. {
  13317. struct lpfc_rpi_hdr *rpi_page;
  13318. uint32_t rc = 0;
  13319. uint16_t lrpi = 0;
  13320. /* SLI4 ports that support extents do not require RPI headers. */
  13321. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13322. goto exit;
  13323. if (phba->sli4_hba.extents_in_use)
  13324. return -EIO;
  13325. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13326. /*
  13327. * Assign the rpi headers a physical rpi only if the driver
  13328. * has not initialized those resources. A port reset only
  13329. * needs the headers posted.
  13330. */
  13331. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13332. LPFC_RPI_RSRC_RDY)
  13333. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13334. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13335. if (rc != MBX_SUCCESS) {
  13336. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13337. "2008 Error %d posting all rpi "
  13338. "headers\n", rc);
  13339. rc = -EIO;
  13340. break;
  13341. }
  13342. }
  13343. exit:
  13344. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13345. LPFC_RPI_RSRC_RDY);
  13346. return rc;
  13347. }
  13348. /**
  13349. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13350. * @phba: pointer to lpfc hba data structure.
  13351. * @rpi_page: pointer to the rpi memory region.
  13352. *
  13353. * This routine is invoked to post a single rpi header to the
  13354. * HBA consistent with the SLI-4 interface spec. This memory region
  13355. * maps up to 64 rpi context regions.
  13356. *
  13357. * Return codes
  13358. * 0 - successful
  13359. * -ENOMEM - No available memory
  13360. * -EIO - The mailbox failed to complete successfully.
  13361. **/
  13362. int
  13363. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13364. {
  13365. LPFC_MBOXQ_t *mboxq;
  13366. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13367. uint32_t rc = 0;
  13368. uint32_t shdr_status, shdr_add_status;
  13369. union lpfc_sli4_cfg_shdr *shdr;
  13370. /* SLI4 ports that support extents do not require RPI headers. */
  13371. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13372. return rc;
  13373. if (phba->sli4_hba.extents_in_use)
  13374. return -EIO;
  13375. /* The port is notified of the header region via a mailbox command. */
  13376. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13377. if (!mboxq) {
  13378. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13379. "2001 Unable to allocate memory for issuing "
  13380. "SLI_CONFIG_SPECIAL mailbox command\n");
  13381. return -ENOMEM;
  13382. }
  13383. /* Post all rpi memory regions to the port. */
  13384. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13385. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13386. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13387. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13388. sizeof(struct lpfc_sli4_cfg_mhdr),
  13389. LPFC_SLI4_MBX_EMBED);
  13390. /* Post the physical rpi to the port for this rpi header. */
  13391. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13392. rpi_page->start_rpi);
  13393. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13394. hdr_tmpl, rpi_page->page_count);
  13395. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13396. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13397. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13398. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13399. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13400. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13401. if (rc != MBX_TIMEOUT)
  13402. mempool_free(mboxq, phba->mbox_mem_pool);
  13403. if (shdr_status || shdr_add_status || rc) {
  13404. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13405. "2514 POST_RPI_HDR mailbox failed with "
  13406. "status x%x add_status x%x, mbx status x%x\n",
  13407. shdr_status, shdr_add_status, rc);
  13408. rc = -ENXIO;
  13409. }
  13410. return rc;
  13411. }
  13412. /**
  13413. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13414. * @phba: pointer to lpfc hba data structure.
  13415. *
  13416. * This routine is invoked to post rpi header templates to the
  13417. * HBA consistent with the SLI-4 interface spec. This routine
  13418. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13419. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13420. *
  13421. * Returns
  13422. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13423. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13424. **/
  13425. int
  13426. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13427. {
  13428. unsigned long rpi;
  13429. uint16_t max_rpi, rpi_limit;
  13430. uint16_t rpi_remaining, lrpi = 0;
  13431. struct lpfc_rpi_hdr *rpi_hdr;
  13432. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13433. rpi_limit = phba->sli4_hba.next_rpi;
  13434. /*
  13435. * Fetch the next logical rpi. Because this index is logical,
  13436. * the driver starts at 0 each time.
  13437. */
  13438. spin_lock_irq(&phba->hbalock);
  13439. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13440. if (rpi >= rpi_limit)
  13441. rpi = LPFC_RPI_ALLOC_ERROR;
  13442. else {
  13443. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13444. phba->sli4_hba.max_cfg_param.rpi_used++;
  13445. phba->sli4_hba.rpi_count++;
  13446. }
  13447. /*
  13448. * Don't try to allocate more rpi header regions if the device limit
  13449. * has been exhausted.
  13450. */
  13451. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13452. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13453. spin_unlock_irq(&phba->hbalock);
  13454. return rpi;
  13455. }
  13456. /*
  13457. * RPI header postings are not required for SLI4 ports capable of
  13458. * extents.
  13459. */
  13460. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13461. spin_unlock_irq(&phba->hbalock);
  13462. return rpi;
  13463. }
  13464. /*
  13465. * If the driver is running low on rpi resources, allocate another
  13466. * page now. Note that the next_rpi value is used because
  13467. * it represents how many are actually in use whereas max_rpi notes
  13468. * how many are supported max by the device.
  13469. */
  13470. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13471. spin_unlock_irq(&phba->hbalock);
  13472. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13473. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13474. if (!rpi_hdr) {
  13475. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13476. "2002 Error Could not grow rpi "
  13477. "count\n");
  13478. } else {
  13479. lrpi = rpi_hdr->start_rpi;
  13480. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13481. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13482. }
  13483. }
  13484. return rpi;
  13485. }
  13486. /**
  13487. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13488. * @phba: pointer to lpfc hba data structure.
  13489. *
  13490. * This routine is invoked to release an rpi to the pool of
  13491. * available rpis maintained by the driver.
  13492. **/
  13493. void
  13494. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13495. {
  13496. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13497. phba->sli4_hba.rpi_count--;
  13498. phba->sli4_hba.max_cfg_param.rpi_used--;
  13499. }
  13500. }
  13501. /**
  13502. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13503. * @phba: pointer to lpfc hba data structure.
  13504. *
  13505. * This routine is invoked to release an rpi to the pool of
  13506. * available rpis maintained by the driver.
  13507. **/
  13508. void
  13509. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13510. {
  13511. spin_lock_irq(&phba->hbalock);
  13512. __lpfc_sli4_free_rpi(phba, rpi);
  13513. spin_unlock_irq(&phba->hbalock);
  13514. }
  13515. /**
  13516. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13517. * @phba: pointer to lpfc hba data structure.
  13518. *
  13519. * This routine is invoked to remove the memory region that
  13520. * provided rpi via a bitmask.
  13521. **/
  13522. void
  13523. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13524. {
  13525. kfree(phba->sli4_hba.rpi_bmask);
  13526. kfree(phba->sli4_hba.rpi_ids);
  13527. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13528. }
  13529. /**
  13530. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  13531. * @phba: pointer to lpfc hba data structure.
  13532. *
  13533. * This routine is invoked to remove the memory region that
  13534. * provided rpi via a bitmask.
  13535. **/
  13536. int
  13537. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  13538. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  13539. {
  13540. LPFC_MBOXQ_t *mboxq;
  13541. struct lpfc_hba *phba = ndlp->phba;
  13542. int rc;
  13543. /* The port is notified of the header region via a mailbox command. */
  13544. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13545. if (!mboxq)
  13546. return -ENOMEM;
  13547. /* Post all rpi memory regions to the port. */
  13548. lpfc_resume_rpi(mboxq, ndlp);
  13549. if (cmpl) {
  13550. mboxq->mbox_cmpl = cmpl;
  13551. mboxq->context1 = arg;
  13552. mboxq->context2 = ndlp;
  13553. } else
  13554. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13555. mboxq->vport = ndlp->vport;
  13556. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13557. if (rc == MBX_NOT_FINISHED) {
  13558. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13559. "2010 Resume RPI Mailbox failed "
  13560. "status %d, mbxStatus x%x\n", rc,
  13561. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13562. mempool_free(mboxq, phba->mbox_mem_pool);
  13563. return -EIO;
  13564. }
  13565. return 0;
  13566. }
  13567. /**
  13568. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13569. * @vport: Pointer to the vport for which the vpi is being initialized
  13570. *
  13571. * This routine is invoked to activate a vpi with the port.
  13572. *
  13573. * Returns:
  13574. * 0 success
  13575. * -Evalue otherwise
  13576. **/
  13577. int
  13578. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13579. {
  13580. LPFC_MBOXQ_t *mboxq;
  13581. int rc = 0;
  13582. int retval = MBX_SUCCESS;
  13583. uint32_t mbox_tmo;
  13584. struct lpfc_hba *phba = vport->phba;
  13585. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13586. if (!mboxq)
  13587. return -ENOMEM;
  13588. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13589. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13590. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13591. if (rc != MBX_SUCCESS) {
  13592. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13593. "2022 INIT VPI Mailbox failed "
  13594. "status %d, mbxStatus x%x\n", rc,
  13595. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13596. retval = -EIO;
  13597. }
  13598. if (rc != MBX_TIMEOUT)
  13599. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13600. return retval;
  13601. }
  13602. /**
  13603. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13604. * @phba: pointer to lpfc hba data structure.
  13605. * @mboxq: Pointer to mailbox object.
  13606. *
  13607. * This routine is invoked to manually add a single FCF record. The caller
  13608. * must pass a completely initialized FCF_Record. This routine takes
  13609. * care of the nonembedded mailbox operations.
  13610. **/
  13611. static void
  13612. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13613. {
  13614. void *virt_addr;
  13615. union lpfc_sli4_cfg_shdr *shdr;
  13616. uint32_t shdr_status, shdr_add_status;
  13617. virt_addr = mboxq->sge_array->addr[0];
  13618. /* The IOCTL status is embedded in the mailbox subheader. */
  13619. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13620. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13621. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13622. if ((shdr_status || shdr_add_status) &&
  13623. (shdr_status != STATUS_FCF_IN_USE))
  13624. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13625. "2558 ADD_FCF_RECORD mailbox failed with "
  13626. "status x%x add_status x%x\n",
  13627. shdr_status, shdr_add_status);
  13628. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13629. }
  13630. /**
  13631. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13632. * @phba: pointer to lpfc hba data structure.
  13633. * @fcf_record: pointer to the initialized fcf record to add.
  13634. *
  13635. * This routine is invoked to manually add a single FCF record. The caller
  13636. * must pass a completely initialized FCF_Record. This routine takes
  13637. * care of the nonembedded mailbox operations.
  13638. **/
  13639. int
  13640. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13641. {
  13642. int rc = 0;
  13643. LPFC_MBOXQ_t *mboxq;
  13644. uint8_t *bytep;
  13645. void *virt_addr;
  13646. dma_addr_t phys_addr;
  13647. struct lpfc_mbx_sge sge;
  13648. uint32_t alloc_len, req_len;
  13649. uint32_t fcfindex;
  13650. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13651. if (!mboxq) {
  13652. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13653. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13654. return -ENOMEM;
  13655. }
  13656. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13657. sizeof(uint32_t);
  13658. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13659. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13660. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13661. req_len, LPFC_SLI4_MBX_NEMBED);
  13662. if (alloc_len < req_len) {
  13663. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13664. "2523 Allocated DMA memory size (x%x) is "
  13665. "less than the requested DMA memory "
  13666. "size (x%x)\n", alloc_len, req_len);
  13667. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13668. return -ENOMEM;
  13669. }
  13670. /*
  13671. * Get the first SGE entry from the non-embedded DMA memory. This
  13672. * routine only uses a single SGE.
  13673. */
  13674. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13675. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13676. virt_addr = mboxq->sge_array->addr[0];
  13677. /*
  13678. * Configure the FCF record for FCFI 0. This is the driver's
  13679. * hardcoded default and gets used in nonFIP mode.
  13680. */
  13681. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13682. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13683. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13684. /*
  13685. * Copy the fcf_index and the FCF Record Data. The data starts after
  13686. * the FCoE header plus word10. The data copy needs to be endian
  13687. * correct.
  13688. */
  13689. bytep += sizeof(uint32_t);
  13690. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13691. mboxq->vport = phba->pport;
  13692. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13693. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13694. if (rc == MBX_NOT_FINISHED) {
  13695. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13696. "2515 ADD_FCF_RECORD mailbox failed with "
  13697. "status 0x%x\n", rc);
  13698. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13699. rc = -EIO;
  13700. } else
  13701. rc = 0;
  13702. return rc;
  13703. }
  13704. /**
  13705. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13706. * @phba: pointer to lpfc hba data structure.
  13707. * @fcf_record: pointer to the fcf record to write the default data.
  13708. * @fcf_index: FCF table entry index.
  13709. *
  13710. * This routine is invoked to build the driver's default FCF record. The
  13711. * values used are hardcoded. This routine handles memory initialization.
  13712. *
  13713. **/
  13714. void
  13715. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13716. struct fcf_record *fcf_record,
  13717. uint16_t fcf_index)
  13718. {
  13719. memset(fcf_record, 0, sizeof(struct fcf_record));
  13720. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  13721. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  13722. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  13723. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  13724. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  13725. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  13726. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  13727. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  13728. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  13729. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  13730. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  13731. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  13732. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  13733. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  13734. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  13735. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  13736. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  13737. /* Set the VLAN bit map */
  13738. if (phba->valid_vlan) {
  13739. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  13740. = 1 << (phba->vlan_id % 8);
  13741. }
  13742. }
  13743. /**
  13744. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  13745. * @phba: pointer to lpfc hba data structure.
  13746. * @fcf_index: FCF table entry offset.
  13747. *
  13748. * This routine is invoked to scan the entire FCF table by reading FCF
  13749. * record and processing it one at a time starting from the @fcf_index
  13750. * for initial FCF discovery or fast FCF failover rediscovery.
  13751. *
  13752. * Return 0 if the mailbox command is submitted successfully, none 0
  13753. * otherwise.
  13754. **/
  13755. int
  13756. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13757. {
  13758. int rc = 0, error;
  13759. LPFC_MBOXQ_t *mboxq;
  13760. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  13761. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  13762. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13763. if (!mboxq) {
  13764. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13765. "2000 Failed to allocate mbox for "
  13766. "READ_FCF cmd\n");
  13767. error = -ENOMEM;
  13768. goto fail_fcf_scan;
  13769. }
  13770. /* Construct the read FCF record mailbox command */
  13771. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13772. if (rc) {
  13773. error = -EINVAL;
  13774. goto fail_fcf_scan;
  13775. }
  13776. /* Issue the mailbox command asynchronously */
  13777. mboxq->vport = phba->pport;
  13778. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  13779. spin_lock_irq(&phba->hbalock);
  13780. phba->hba_flag |= FCF_TS_INPROG;
  13781. spin_unlock_irq(&phba->hbalock);
  13782. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13783. if (rc == MBX_NOT_FINISHED)
  13784. error = -EIO;
  13785. else {
  13786. /* Reset eligible FCF count for new scan */
  13787. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  13788. phba->fcf.eligible_fcf_cnt = 0;
  13789. error = 0;
  13790. }
  13791. fail_fcf_scan:
  13792. if (error) {
  13793. if (mboxq)
  13794. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13795. /* FCF scan failed, clear FCF_TS_INPROG flag */
  13796. spin_lock_irq(&phba->hbalock);
  13797. phba->hba_flag &= ~FCF_TS_INPROG;
  13798. spin_unlock_irq(&phba->hbalock);
  13799. }
  13800. return error;
  13801. }
  13802. /**
  13803. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  13804. * @phba: pointer to lpfc hba data structure.
  13805. * @fcf_index: FCF table entry offset.
  13806. *
  13807. * This routine is invoked to read an FCF record indicated by @fcf_index
  13808. * and to use it for FLOGI roundrobin FCF failover.
  13809. *
  13810. * Return 0 if the mailbox command is submitted successfully, none 0
  13811. * otherwise.
  13812. **/
  13813. int
  13814. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13815. {
  13816. int rc = 0, error;
  13817. LPFC_MBOXQ_t *mboxq;
  13818. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13819. if (!mboxq) {
  13820. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13821. "2763 Failed to allocate mbox for "
  13822. "READ_FCF cmd\n");
  13823. error = -ENOMEM;
  13824. goto fail_fcf_read;
  13825. }
  13826. /* Construct the read FCF record mailbox command */
  13827. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13828. if (rc) {
  13829. error = -EINVAL;
  13830. goto fail_fcf_read;
  13831. }
  13832. /* Issue the mailbox command asynchronously */
  13833. mboxq->vport = phba->pport;
  13834. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  13835. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13836. if (rc == MBX_NOT_FINISHED)
  13837. error = -EIO;
  13838. else
  13839. error = 0;
  13840. fail_fcf_read:
  13841. if (error && mboxq)
  13842. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13843. return error;
  13844. }
  13845. /**
  13846. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  13847. * @phba: pointer to lpfc hba data structure.
  13848. * @fcf_index: FCF table entry offset.
  13849. *
  13850. * This routine is invoked to read an FCF record indicated by @fcf_index to
  13851. * determine whether it's eligible for FLOGI roundrobin failover list.
  13852. *
  13853. * Return 0 if the mailbox command is submitted successfully, none 0
  13854. * otherwise.
  13855. **/
  13856. int
  13857. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13858. {
  13859. int rc = 0, error;
  13860. LPFC_MBOXQ_t *mboxq;
  13861. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13862. if (!mboxq) {
  13863. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13864. "2758 Failed to allocate mbox for "
  13865. "READ_FCF cmd\n");
  13866. error = -ENOMEM;
  13867. goto fail_fcf_read;
  13868. }
  13869. /* Construct the read FCF record mailbox command */
  13870. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13871. if (rc) {
  13872. error = -EINVAL;
  13873. goto fail_fcf_read;
  13874. }
  13875. /* Issue the mailbox command asynchronously */
  13876. mboxq->vport = phba->pport;
  13877. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  13878. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13879. if (rc == MBX_NOT_FINISHED)
  13880. error = -EIO;
  13881. else
  13882. error = 0;
  13883. fail_fcf_read:
  13884. if (error && mboxq)
  13885. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13886. return error;
  13887. }
  13888. /**
  13889. * lpfc_check_next_fcf_pri
  13890. * phba pointer to the lpfc_hba struct for this port.
  13891. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  13892. * routine when the rr_bmask is empty. The FCF indecies are put into the
  13893. * rr_bmask based on their priority level. Starting from the highest priority
  13894. * to the lowest. The most likely FCF candidate will be in the highest
  13895. * priority group. When this routine is called it searches the fcf_pri list for
  13896. * next lowest priority group and repopulates the rr_bmask with only those
  13897. * fcf_indexes.
  13898. * returns:
  13899. * 1=success 0=failure
  13900. **/
  13901. int
  13902. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  13903. {
  13904. uint16_t next_fcf_pri;
  13905. uint16_t last_index;
  13906. struct lpfc_fcf_pri *fcf_pri;
  13907. int rc;
  13908. int ret = 0;
  13909. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  13910. LPFC_SLI4_FCF_TBL_INDX_MAX);
  13911. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13912. "3060 Last IDX %d\n", last_index);
  13913. if (list_empty(&phba->fcf.fcf_pri_list)) {
  13914. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13915. "3061 Last IDX %d\n", last_index);
  13916. return 0; /* Empty rr list */
  13917. }
  13918. next_fcf_pri = 0;
  13919. /*
  13920. * Clear the rr_bmask and set all of the bits that are at this
  13921. * priority.
  13922. */
  13923. memset(phba->fcf.fcf_rr_bmask, 0,
  13924. sizeof(*phba->fcf.fcf_rr_bmask));
  13925. spin_lock_irq(&phba->hbalock);
  13926. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13927. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  13928. continue;
  13929. /*
  13930. * the 1st priority that has not FLOGI failed
  13931. * will be the highest.
  13932. */
  13933. if (!next_fcf_pri)
  13934. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13935. spin_unlock_irq(&phba->hbalock);
  13936. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13937. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13938. fcf_pri->fcf_rec.fcf_index);
  13939. if (rc)
  13940. return 0;
  13941. }
  13942. spin_lock_irq(&phba->hbalock);
  13943. }
  13944. /*
  13945. * if next_fcf_pri was not set above and the list is not empty then
  13946. * we have failed flogis on all of them. So reset flogi failed
  13947. * and start at the begining.
  13948. */
  13949. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  13950. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13951. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  13952. /*
  13953. * the 1st priority that has not FLOGI failed
  13954. * will be the highest.
  13955. */
  13956. if (!next_fcf_pri)
  13957. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13958. spin_unlock_irq(&phba->hbalock);
  13959. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13960. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13961. fcf_pri->fcf_rec.fcf_index);
  13962. if (rc)
  13963. return 0;
  13964. }
  13965. spin_lock_irq(&phba->hbalock);
  13966. }
  13967. } else
  13968. ret = 1;
  13969. spin_unlock_irq(&phba->hbalock);
  13970. return ret;
  13971. }
  13972. /**
  13973. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  13974. * @phba: pointer to lpfc hba data structure.
  13975. *
  13976. * This routine is to get the next eligible FCF record index in a round
  13977. * robin fashion. If the next eligible FCF record index equals to the
  13978. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  13979. * shall be returned, otherwise, the next eligible FCF record's index
  13980. * shall be returned.
  13981. **/
  13982. uint16_t
  13983. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  13984. {
  13985. uint16_t next_fcf_index;
  13986. /* Search start from next bit of currently registered FCF index */
  13987. next_priority:
  13988. next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
  13989. LPFC_SLI4_FCF_TBL_INDX_MAX;
  13990. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13991. LPFC_SLI4_FCF_TBL_INDX_MAX,
  13992. next_fcf_index);
  13993. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  13994. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13995. /*
  13996. * If we have wrapped then we need to clear the bits that
  13997. * have been tested so that we can detect when we should
  13998. * change the priority level.
  13999. */
  14000. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14001. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  14002. }
  14003. /* Check roundrobin failover list empty condition */
  14004. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  14005. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  14006. /*
  14007. * If next fcf index is not found check if there are lower
  14008. * Priority level fcf's in the fcf_priority list.
  14009. * Set up the rr_bmask with all of the avaiable fcf bits
  14010. * at that level and continue the selection process.
  14011. */
  14012. if (lpfc_check_next_fcf_pri_level(phba))
  14013. goto next_priority;
  14014. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14015. "2844 No roundrobin failover FCF available\n");
  14016. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  14017. return LPFC_FCOE_FCF_NEXT_NONE;
  14018. else {
  14019. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14020. "3063 Only FCF available idx %d, flag %x\n",
  14021. next_fcf_index,
  14022. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  14023. return next_fcf_index;
  14024. }
  14025. }
  14026. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  14027. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  14028. LPFC_FCF_FLOGI_FAILED)
  14029. goto next_priority;
  14030. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14031. "2845 Get next roundrobin failover FCF (x%x)\n",
  14032. next_fcf_index);
  14033. return next_fcf_index;
  14034. }
  14035. /**
  14036. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  14037. * @phba: pointer to lpfc hba data structure.
  14038. *
  14039. * This routine sets the FCF record index in to the eligible bmask for
  14040. * roundrobin failover search. It checks to make sure that the index
  14041. * does not go beyond the range of the driver allocated bmask dimension
  14042. * before setting the bit.
  14043. *
  14044. * Returns 0 if the index bit successfully set, otherwise, it returns
  14045. * -EINVAL.
  14046. **/
  14047. int
  14048. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  14049. {
  14050. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14051. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14052. "2610 FCF (x%x) reached driver's book "
  14053. "keeping dimension:x%x\n",
  14054. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14055. return -EINVAL;
  14056. }
  14057. /* Set the eligible FCF record index bmask */
  14058. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14059. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14060. "2790 Set FCF (x%x) to roundrobin FCF failover "
  14061. "bmask\n", fcf_index);
  14062. return 0;
  14063. }
  14064. /**
  14065. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  14066. * @phba: pointer to lpfc hba data structure.
  14067. *
  14068. * This routine clears the FCF record index from the eligible bmask for
  14069. * roundrobin failover search. It checks to make sure that the index
  14070. * does not go beyond the range of the driver allocated bmask dimension
  14071. * before clearing the bit.
  14072. **/
  14073. void
  14074. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  14075. {
  14076. struct lpfc_fcf_pri *fcf_pri;
  14077. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14078. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14079. "2762 FCF (x%x) reached driver's book "
  14080. "keeping dimension:x%x\n",
  14081. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14082. return;
  14083. }
  14084. /* Clear the eligible FCF record index bmask */
  14085. spin_lock_irq(&phba->hbalock);
  14086. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14087. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  14088. list_del_init(&fcf_pri->list);
  14089. break;
  14090. }
  14091. }
  14092. spin_unlock_irq(&phba->hbalock);
  14093. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14094. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14095. "2791 Clear FCF (x%x) from roundrobin failover "
  14096. "bmask\n", fcf_index);
  14097. }
  14098. /**
  14099. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  14100. * @phba: pointer to lpfc hba data structure.
  14101. *
  14102. * This routine is the completion routine for the rediscover FCF table mailbox
  14103. * command. If the mailbox command returned failure, it will try to stop the
  14104. * FCF rediscover wait timer.
  14105. **/
  14106. void
  14107. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  14108. {
  14109. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14110. uint32_t shdr_status, shdr_add_status;
  14111. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14112. shdr_status = bf_get(lpfc_mbox_hdr_status,
  14113. &redisc_fcf->header.cfg_shdr.response);
  14114. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  14115. &redisc_fcf->header.cfg_shdr.response);
  14116. if (shdr_status || shdr_add_status) {
  14117. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14118. "2746 Requesting for FCF rediscovery failed "
  14119. "status x%x add_status x%x\n",
  14120. shdr_status, shdr_add_status);
  14121. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  14122. spin_lock_irq(&phba->hbalock);
  14123. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  14124. spin_unlock_irq(&phba->hbalock);
  14125. /*
  14126. * CVL event triggered FCF rediscover request failed,
  14127. * last resort to re-try current registered FCF entry.
  14128. */
  14129. lpfc_retry_pport_discovery(phba);
  14130. } else {
  14131. spin_lock_irq(&phba->hbalock);
  14132. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  14133. spin_unlock_irq(&phba->hbalock);
  14134. /*
  14135. * DEAD FCF event triggered FCF rediscover request
  14136. * failed, last resort to fail over as a link down
  14137. * to FCF registration.
  14138. */
  14139. lpfc_sli4_fcf_dead_failthrough(phba);
  14140. }
  14141. } else {
  14142. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14143. "2775 Start FCF rediscover quiescent timer\n");
  14144. /*
  14145. * Start FCF rediscovery wait timer for pending FCF
  14146. * before rescan FCF record table.
  14147. */
  14148. lpfc_fcf_redisc_wait_start_timer(phba);
  14149. }
  14150. mempool_free(mbox, phba->mbox_mem_pool);
  14151. }
  14152. /**
  14153. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14154. * @phba: pointer to lpfc hba data structure.
  14155. *
  14156. * This routine is invoked to request for rediscovery of the entire FCF table
  14157. * by the port.
  14158. **/
  14159. int
  14160. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14161. {
  14162. LPFC_MBOXQ_t *mbox;
  14163. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14164. int rc, length;
  14165. /* Cancel retry delay timers to all vports before FCF rediscover */
  14166. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14167. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14168. if (!mbox) {
  14169. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14170. "2745 Failed to allocate mbox for "
  14171. "requesting FCF rediscover.\n");
  14172. return -ENOMEM;
  14173. }
  14174. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14175. sizeof(struct lpfc_sli4_cfg_mhdr));
  14176. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14177. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14178. length, LPFC_SLI4_MBX_EMBED);
  14179. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14180. /* Set count to 0 for invalidating the entire FCF database */
  14181. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14182. /* Issue the mailbox command asynchronously */
  14183. mbox->vport = phba->pport;
  14184. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14185. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14186. if (rc == MBX_NOT_FINISHED) {
  14187. mempool_free(mbox, phba->mbox_mem_pool);
  14188. return -EIO;
  14189. }
  14190. return 0;
  14191. }
  14192. /**
  14193. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14194. * @phba: pointer to lpfc hba data structure.
  14195. *
  14196. * This function is the failover routine as a last resort to the FCF DEAD
  14197. * event when driver failed to perform fast FCF failover.
  14198. **/
  14199. void
  14200. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14201. {
  14202. uint32_t link_state;
  14203. /*
  14204. * Last resort as FCF DEAD event failover will treat this as
  14205. * a link down, but save the link state because we don't want
  14206. * it to be changed to Link Down unless it is already down.
  14207. */
  14208. link_state = phba->link_state;
  14209. lpfc_linkdown(phba);
  14210. phba->link_state = link_state;
  14211. /* Unregister FCF if no devices connected to it */
  14212. lpfc_unregister_unused_fcf(phba);
  14213. }
  14214. /**
  14215. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14216. * @phba: pointer to lpfc hba data structure.
  14217. * @rgn23_data: pointer to configure region 23 data.
  14218. *
  14219. * This function gets SLI3 port configure region 23 data through memory dump
  14220. * mailbox command. When it successfully retrieves data, the size of the data
  14221. * will be returned, otherwise, 0 will be returned.
  14222. **/
  14223. static uint32_t
  14224. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14225. {
  14226. LPFC_MBOXQ_t *pmb = NULL;
  14227. MAILBOX_t *mb;
  14228. uint32_t offset = 0;
  14229. int rc;
  14230. if (!rgn23_data)
  14231. return 0;
  14232. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14233. if (!pmb) {
  14234. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14235. "2600 failed to allocate mailbox memory\n");
  14236. return 0;
  14237. }
  14238. mb = &pmb->u.mb;
  14239. do {
  14240. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14241. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14242. if (rc != MBX_SUCCESS) {
  14243. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14244. "2601 failed to read config "
  14245. "region 23, rc 0x%x Status 0x%x\n",
  14246. rc, mb->mbxStatus);
  14247. mb->un.varDmp.word_cnt = 0;
  14248. }
  14249. /*
  14250. * dump mem may return a zero when finished or we got a
  14251. * mailbox error, either way we are done.
  14252. */
  14253. if (mb->un.varDmp.word_cnt == 0)
  14254. break;
  14255. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14256. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14257. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14258. rgn23_data + offset,
  14259. mb->un.varDmp.word_cnt);
  14260. offset += mb->un.varDmp.word_cnt;
  14261. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14262. mempool_free(pmb, phba->mbox_mem_pool);
  14263. return offset;
  14264. }
  14265. /**
  14266. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14267. * @phba: pointer to lpfc hba data structure.
  14268. * @rgn23_data: pointer to configure region 23 data.
  14269. *
  14270. * This function gets SLI4 port configure region 23 data through memory dump
  14271. * mailbox command. When it successfully retrieves data, the size of the data
  14272. * will be returned, otherwise, 0 will be returned.
  14273. **/
  14274. static uint32_t
  14275. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14276. {
  14277. LPFC_MBOXQ_t *mboxq = NULL;
  14278. struct lpfc_dmabuf *mp = NULL;
  14279. struct lpfc_mqe *mqe;
  14280. uint32_t data_length = 0;
  14281. int rc;
  14282. if (!rgn23_data)
  14283. return 0;
  14284. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14285. if (!mboxq) {
  14286. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14287. "3105 failed to allocate mailbox memory\n");
  14288. return 0;
  14289. }
  14290. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14291. goto out;
  14292. mqe = &mboxq->u.mqe;
  14293. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14294. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14295. if (rc)
  14296. goto out;
  14297. data_length = mqe->un.mb_words[5];
  14298. if (data_length == 0)
  14299. goto out;
  14300. if (data_length > DMP_RGN23_SIZE) {
  14301. data_length = 0;
  14302. goto out;
  14303. }
  14304. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14305. out:
  14306. mempool_free(mboxq, phba->mbox_mem_pool);
  14307. if (mp) {
  14308. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14309. kfree(mp);
  14310. }
  14311. return data_length;
  14312. }
  14313. /**
  14314. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14315. * @phba: pointer to lpfc hba data structure.
  14316. *
  14317. * This function read region 23 and parse TLV for port status to
  14318. * decide if the user disaled the port. If the TLV indicates the
  14319. * port is disabled, the hba_flag is set accordingly.
  14320. **/
  14321. void
  14322. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14323. {
  14324. uint8_t *rgn23_data = NULL;
  14325. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14326. uint32_t offset = 0;
  14327. /* Get adapter Region 23 data */
  14328. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14329. if (!rgn23_data)
  14330. goto out;
  14331. if (phba->sli_rev < LPFC_SLI_REV4)
  14332. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14333. else {
  14334. if_type = bf_get(lpfc_sli_intf_if_type,
  14335. &phba->sli4_hba.sli_intf);
  14336. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14337. goto out;
  14338. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14339. }
  14340. if (!data_size)
  14341. goto out;
  14342. /* Check the region signature first */
  14343. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14344. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14345. "2619 Config region 23 has bad signature\n");
  14346. goto out;
  14347. }
  14348. offset += 4;
  14349. /* Check the data structure version */
  14350. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14351. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14352. "2620 Config region 23 has bad version\n");
  14353. goto out;
  14354. }
  14355. offset += 4;
  14356. /* Parse TLV entries in the region */
  14357. while (offset < data_size) {
  14358. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14359. break;
  14360. /*
  14361. * If the TLV is not driver specific TLV or driver id is
  14362. * not linux driver id, skip the record.
  14363. */
  14364. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14365. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14366. (rgn23_data[offset + 3] != 0)) {
  14367. offset += rgn23_data[offset + 1] * 4 + 4;
  14368. continue;
  14369. }
  14370. /* Driver found a driver specific TLV in the config region */
  14371. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14372. offset += 4;
  14373. tlv_offset = 0;
  14374. /*
  14375. * Search for configured port state sub-TLV.
  14376. */
  14377. while ((offset < data_size) &&
  14378. (tlv_offset < sub_tlv_len)) {
  14379. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14380. offset += 4;
  14381. tlv_offset += 4;
  14382. break;
  14383. }
  14384. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14385. offset += rgn23_data[offset + 1] * 4 + 4;
  14386. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14387. continue;
  14388. }
  14389. /* This HBA contains PORT_STE configured */
  14390. if (!rgn23_data[offset + 2])
  14391. phba->hba_flag |= LINK_DISABLED;
  14392. goto out;
  14393. }
  14394. }
  14395. out:
  14396. kfree(rgn23_data);
  14397. return;
  14398. }
  14399. /**
  14400. * lpfc_wr_object - write an object to the firmware
  14401. * @phba: HBA structure that indicates port to create a queue on.
  14402. * @dmabuf_list: list of dmabufs to write to the port.
  14403. * @size: the total byte value of the objects to write to the port.
  14404. * @offset: the current offset to be used to start the transfer.
  14405. *
  14406. * This routine will create a wr_object mailbox command to send to the port.
  14407. * the mailbox command will be constructed using the dma buffers described in
  14408. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14409. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14410. * used to indicate the starting offset of the transfer and will also return
  14411. * the offset after the write object mailbox has completed. @size is used to
  14412. * determine the end of the object and whether the eof bit should be set.
  14413. *
  14414. * Return 0 is successful and offset will contain the the new offset to use
  14415. * for the next write.
  14416. * Return negative value for error cases.
  14417. **/
  14418. int
  14419. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14420. uint32_t size, uint32_t *offset)
  14421. {
  14422. struct lpfc_mbx_wr_object *wr_object;
  14423. LPFC_MBOXQ_t *mbox;
  14424. int rc = 0, i = 0;
  14425. uint32_t shdr_status, shdr_add_status;
  14426. uint32_t mbox_tmo;
  14427. union lpfc_sli4_cfg_shdr *shdr;
  14428. struct lpfc_dmabuf *dmabuf;
  14429. uint32_t written = 0;
  14430. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14431. if (!mbox)
  14432. return -ENOMEM;
  14433. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14434. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14435. sizeof(struct lpfc_mbx_wr_object) -
  14436. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14437. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14438. wr_object->u.request.write_offset = *offset;
  14439. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14440. wr_object->u.request.object_name[0] =
  14441. cpu_to_le32(wr_object->u.request.object_name[0]);
  14442. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14443. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14444. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14445. break;
  14446. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14447. wr_object->u.request.bde[i].addrHigh =
  14448. putPaddrHigh(dmabuf->phys);
  14449. if (written + SLI4_PAGE_SIZE >= size) {
  14450. wr_object->u.request.bde[i].tus.f.bdeSize =
  14451. (size - written);
  14452. written += (size - written);
  14453. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14454. } else {
  14455. wr_object->u.request.bde[i].tus.f.bdeSize =
  14456. SLI4_PAGE_SIZE;
  14457. written += SLI4_PAGE_SIZE;
  14458. }
  14459. i++;
  14460. }
  14461. wr_object->u.request.bde_count = i;
  14462. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14463. if (!phba->sli4_hba.intr_enable)
  14464. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14465. else {
  14466. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14467. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14468. }
  14469. /* The IOCTL status is embedded in the mailbox subheader. */
  14470. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14471. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14472. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14473. if (rc != MBX_TIMEOUT)
  14474. mempool_free(mbox, phba->mbox_mem_pool);
  14475. if (shdr_status || shdr_add_status || rc) {
  14476. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14477. "3025 Write Object mailbox failed with "
  14478. "status x%x add_status x%x, mbx status x%x\n",
  14479. shdr_status, shdr_add_status, rc);
  14480. rc = -ENXIO;
  14481. } else
  14482. *offset += wr_object->u.response.actual_write_length;
  14483. return rc;
  14484. }
  14485. /**
  14486. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14487. * @vport: pointer to vport data structure.
  14488. *
  14489. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14490. * and REG_VPI mailbox commands associated with the vport. This function
  14491. * is called when driver want to restart discovery of the vport due to
  14492. * a Clear Virtual Link event.
  14493. **/
  14494. void
  14495. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14496. {
  14497. struct lpfc_hba *phba = vport->phba;
  14498. LPFC_MBOXQ_t *mb, *nextmb;
  14499. struct lpfc_dmabuf *mp;
  14500. struct lpfc_nodelist *ndlp;
  14501. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14502. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14503. LIST_HEAD(mbox_cmd_list);
  14504. uint8_t restart_loop;
  14505. /* Clean up internally queued mailbox commands with the vport */
  14506. spin_lock_irq(&phba->hbalock);
  14507. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14508. if (mb->vport != vport)
  14509. continue;
  14510. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14511. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14512. continue;
  14513. list_del(&mb->list);
  14514. list_add_tail(&mb->list, &mbox_cmd_list);
  14515. }
  14516. /* Clean up active mailbox command with the vport */
  14517. mb = phba->sli.mbox_active;
  14518. if (mb && (mb->vport == vport)) {
  14519. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14520. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14521. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14522. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14523. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14524. /* Put reference count for delayed processing */
  14525. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14526. /* Unregister the RPI when mailbox complete */
  14527. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14528. }
  14529. }
  14530. /* Cleanup any mailbox completions which are not yet processed */
  14531. do {
  14532. restart_loop = 0;
  14533. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14534. /*
  14535. * If this mailox is already processed or it is
  14536. * for another vport ignore it.
  14537. */
  14538. if ((mb->vport != vport) ||
  14539. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14540. continue;
  14541. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14542. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14543. continue;
  14544. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14545. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14546. ndlp = (struct lpfc_nodelist *)mb->context2;
  14547. /* Unregister the RPI when mailbox complete */
  14548. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14549. restart_loop = 1;
  14550. spin_unlock_irq(&phba->hbalock);
  14551. spin_lock(shost->host_lock);
  14552. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14553. spin_unlock(shost->host_lock);
  14554. spin_lock_irq(&phba->hbalock);
  14555. break;
  14556. }
  14557. }
  14558. } while (restart_loop);
  14559. spin_unlock_irq(&phba->hbalock);
  14560. /* Release the cleaned-up mailbox commands */
  14561. while (!list_empty(&mbox_cmd_list)) {
  14562. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14563. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14564. mp = (struct lpfc_dmabuf *) (mb->context1);
  14565. if (mp) {
  14566. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14567. kfree(mp);
  14568. }
  14569. ndlp = (struct lpfc_nodelist *) mb->context2;
  14570. mb->context2 = NULL;
  14571. if (ndlp) {
  14572. spin_lock(shost->host_lock);
  14573. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14574. spin_unlock(shost->host_lock);
  14575. lpfc_nlp_put(ndlp);
  14576. }
  14577. }
  14578. mempool_free(mb, phba->mbox_mem_pool);
  14579. }
  14580. /* Release the ndlp with the cleaned-up active mailbox command */
  14581. if (act_mbx_ndlp) {
  14582. spin_lock(shost->host_lock);
  14583. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14584. spin_unlock(shost->host_lock);
  14585. lpfc_nlp_put(act_mbx_ndlp);
  14586. }
  14587. }
  14588. /**
  14589. * lpfc_drain_txq - Drain the txq
  14590. * @phba: Pointer to HBA context object.
  14591. *
  14592. * This function attempt to submit IOCBs on the txq
  14593. * to the adapter. For SLI4 adapters, the txq contains
  14594. * ELS IOCBs that have been deferred because the there
  14595. * are no SGLs. This congestion can occur with large
  14596. * vport counts during node discovery.
  14597. **/
  14598. uint32_t
  14599. lpfc_drain_txq(struct lpfc_hba *phba)
  14600. {
  14601. LIST_HEAD(completions);
  14602. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14603. struct lpfc_iocbq *piocbq = 0;
  14604. unsigned long iflags = 0;
  14605. char *fail_msg = NULL;
  14606. struct lpfc_sglq *sglq;
  14607. union lpfc_wqe wqe;
  14608. spin_lock_irqsave(&phba->hbalock, iflags);
  14609. if (pring->txq_cnt > pring->txq_max)
  14610. pring->txq_max = pring->txq_cnt;
  14611. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14612. while (pring->txq_cnt) {
  14613. spin_lock_irqsave(&phba->hbalock, iflags);
  14614. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14615. if (!piocbq) {
  14616. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14617. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14618. "2823 txq empty and txq_cnt is %d\n ",
  14619. pring->txq_cnt);
  14620. break;
  14621. }
  14622. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14623. if (!sglq) {
  14624. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14625. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14626. break;
  14627. }
  14628. /* The xri and iocb resources secured,
  14629. * attempt to issue request
  14630. */
  14631. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14632. piocbq->sli4_xritag = sglq->sli4_xritag;
  14633. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14634. fail_msg = "to convert bpl to sgl";
  14635. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14636. fail_msg = "to convert iocb to wqe";
  14637. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14638. fail_msg = " - Wq is full";
  14639. else
  14640. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14641. if (fail_msg) {
  14642. /* Failed means we can't issue and need to cancel */
  14643. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14644. "2822 IOCB failed %s iotag 0x%x "
  14645. "xri 0x%x\n",
  14646. fail_msg,
  14647. piocbq->iotag, piocbq->sli4_xritag);
  14648. list_add_tail(&piocbq->list, &completions);
  14649. }
  14650. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14651. }
  14652. /* Cancel all the IOCBs that cannot be issued */
  14653. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14654. IOERR_SLI_ABORTED);
  14655. return pring->txq_cnt;
  14656. }