lpfc_sli.c 526 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2016 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 <linux/lockdep.h>
  27. #include <scsi/scsi.h>
  28. #include <scsi/scsi_cmnd.h>
  29. #include <scsi/scsi_device.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_transport_fc.h>
  32. #include <scsi/fc/fc_fs.h>
  33. #include <linux/aer.h>
  34. #include "lpfc_hw4.h"
  35. #include "lpfc_hw.h"
  36. #include "lpfc_sli.h"
  37. #include "lpfc_sli4.h"
  38. #include "lpfc_nl.h"
  39. #include "lpfc_disc.h"
  40. #include "lpfc_scsi.h"
  41. #include "lpfc.h"
  42. #include "lpfc_crtn.h"
  43. #include "lpfc_logmsg.h"
  44. #include "lpfc_compat.h"
  45. #include "lpfc_debugfs.h"
  46. #include "lpfc_vport.h"
  47. /* There are only four IOCB completion types. */
  48. typedef enum _lpfc_iocb_type {
  49. LPFC_UNKNOWN_IOCB,
  50. LPFC_UNSOL_IOCB,
  51. LPFC_SOL_IOCB,
  52. LPFC_ABORT_IOCB
  53. } lpfc_iocb_type;
  54. /* Provide function prototypes local to this module. */
  55. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  56. uint32_t);
  57. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  58. uint8_t *, uint32_t *);
  59. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  60. struct lpfc_iocbq *);
  61. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  62. struct hbq_dmabuf *);
  63. static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
  64. struct lpfc_cqe *);
  65. static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
  66. int);
  67. static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
  68. uint32_t);
  69. static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
  70. static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
  71. static IOCB_t *
  72. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  73. {
  74. return &iocbq->iocb;
  75. }
  76. /**
  77. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  78. * @q: The Work Queue to operate on.
  79. * @wqe: The work Queue Entry to put on the Work queue.
  80. *
  81. * This routine will copy the contents of @wqe to the next available entry on
  82. * the @q. This function will then ring the Work Queue Doorbell to signal the
  83. * HBA to start processing the Work Queue Entry. This function returns 0 if
  84. * successful. If no entries are available on @q then this function will return
  85. * -ENOMEM.
  86. * The caller is expected to hold the hbalock when calling this routine.
  87. **/
  88. static uint32_t
  89. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  90. {
  91. union lpfc_wqe *temp_wqe;
  92. struct lpfc_register doorbell;
  93. uint32_t host_index;
  94. uint32_t idx;
  95. /* sanity check on queue memory */
  96. if (unlikely(!q))
  97. return -ENOMEM;
  98. temp_wqe = q->qe[q->host_index].wqe;
  99. /* If the host has not yet processed the next entry then we are done */
  100. idx = ((q->host_index + 1) % q->entry_count);
  101. if (idx == q->hba_index) {
  102. q->WQ_overflow++;
  103. return -ENOMEM;
  104. }
  105. q->WQ_posted++;
  106. /* set consumption flag every once in a while */
  107. if (!((q->host_index + 1) % q->entry_repost))
  108. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  109. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  110. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  111. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  112. /* Update the host index before invoking device */
  113. host_index = q->host_index;
  114. q->host_index = idx;
  115. /* Ring Doorbell */
  116. doorbell.word0 = 0;
  117. if (q->db_format == LPFC_DB_LIST_FORMAT) {
  118. bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
  119. bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
  120. bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
  121. } else if (q->db_format == LPFC_DB_RING_FORMAT) {
  122. bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
  123. bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
  124. } else {
  125. return -EINVAL;
  126. }
  127. writel(doorbell.word0, q->db_regaddr);
  128. return 0;
  129. }
  130. /**
  131. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  132. * @q: The Work Queue to operate on.
  133. * @index: The index to advance the hba index to.
  134. *
  135. * This routine will update the HBA index of a queue to reflect consumption of
  136. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  137. * an entry the host calls this function to update the queue's internal
  138. * pointers. This routine returns the number of entries that were consumed by
  139. * the HBA.
  140. **/
  141. static uint32_t
  142. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  143. {
  144. uint32_t released = 0;
  145. /* sanity check on queue memory */
  146. if (unlikely(!q))
  147. return 0;
  148. if (q->hba_index == index)
  149. return 0;
  150. do {
  151. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  152. released++;
  153. } while (q->hba_index != index);
  154. return released;
  155. }
  156. /**
  157. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  158. * @q: The Mailbox Queue to operate on.
  159. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  160. *
  161. * This routine will copy the contents of @mqe to the next available entry on
  162. * the @q. This function will then ring the Work Queue Doorbell to signal the
  163. * HBA to start processing the Work Queue Entry. This function returns 0 if
  164. * successful. If no entries are available on @q then this function will return
  165. * -ENOMEM.
  166. * The caller is expected to hold the hbalock when calling this routine.
  167. **/
  168. static uint32_t
  169. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  170. {
  171. struct lpfc_mqe *temp_mqe;
  172. struct lpfc_register doorbell;
  173. /* sanity check on queue memory */
  174. if (unlikely(!q))
  175. return -ENOMEM;
  176. temp_mqe = q->qe[q->host_index].mqe;
  177. /* If the host has not yet processed the next entry then we are done */
  178. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  179. return -ENOMEM;
  180. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  181. /* Save off the mailbox pointer for completion */
  182. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  183. /* Update the host index before invoking device */
  184. q->host_index = ((q->host_index + 1) % q->entry_count);
  185. /* Ring Doorbell */
  186. doorbell.word0 = 0;
  187. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  188. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  189. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  190. return 0;
  191. }
  192. /**
  193. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  194. * @q: The Mailbox Queue to operate on.
  195. *
  196. * This routine will update the HBA index of a queue to reflect consumption of
  197. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  198. * an entry the host calls this function to update the queue's internal
  199. * pointers. This routine returns the number of entries that were consumed by
  200. * the HBA.
  201. **/
  202. static uint32_t
  203. lpfc_sli4_mq_release(struct lpfc_queue *q)
  204. {
  205. /* sanity check on queue memory */
  206. if (unlikely(!q))
  207. return 0;
  208. /* Clear the mailbox pointer for completion */
  209. q->phba->mbox = NULL;
  210. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  211. return 1;
  212. }
  213. /**
  214. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  215. * @q: The Event Queue to get the first valid EQE from
  216. *
  217. * This routine will get the first valid Event Queue Entry from @q, update
  218. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  219. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  220. * processed, but not popped back to the HBA then this routine will return NULL.
  221. **/
  222. static struct lpfc_eqe *
  223. lpfc_sli4_eq_get(struct lpfc_queue *q)
  224. {
  225. struct lpfc_eqe *eqe;
  226. uint32_t idx;
  227. /* sanity check on queue memory */
  228. if (unlikely(!q))
  229. return NULL;
  230. eqe = q->qe[q->hba_index].eqe;
  231. /* If the next EQE is not valid then we are done */
  232. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  233. return NULL;
  234. /* If the host has not yet processed the next entry then we are done */
  235. idx = ((q->hba_index + 1) % q->entry_count);
  236. if (idx == q->host_index)
  237. return NULL;
  238. q->hba_index = idx;
  239. /*
  240. * insert barrier for instruction interlock : data from the hardware
  241. * must have the valid bit checked before it can be copied and acted
  242. * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
  243. * instructions allowing action on content before valid bit checked,
  244. * add barrier here as well. May not be needed as "content" is a
  245. * single 32-bit entity here (vs multi word structure for cq's).
  246. */
  247. mb();
  248. return eqe;
  249. }
  250. /**
  251. * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
  252. * @q: The Event Queue to disable interrupts
  253. *
  254. **/
  255. static inline void
  256. lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
  257. {
  258. struct lpfc_register doorbell;
  259. doorbell.word0 = 0;
  260. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  261. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  262. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  263. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  264. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  265. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  266. }
  267. /**
  268. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  269. * @q: The Event Queue that the host has completed processing for.
  270. * @arm: Indicates whether the host wants to arms this CQ.
  271. *
  272. * This routine will mark all Event Queue Entries on @q, from the last
  273. * known completed entry to the last entry that was processed, as completed
  274. * by clearing the valid bit for each completion queue entry. Then it will
  275. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  276. * The internal host index in the @q will be updated by this routine to indicate
  277. * that the host has finished processing the entries. The @arm parameter
  278. * indicates that the queue should be rearmed when ringing the doorbell.
  279. *
  280. * This function will return the number of EQEs that were popped.
  281. **/
  282. uint32_t
  283. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  284. {
  285. uint32_t released = 0;
  286. struct lpfc_eqe *temp_eqe;
  287. struct lpfc_register doorbell;
  288. /* sanity check on queue memory */
  289. if (unlikely(!q))
  290. return 0;
  291. /* while there are valid entries */
  292. while (q->hba_index != q->host_index) {
  293. temp_eqe = q->qe[q->host_index].eqe;
  294. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  295. released++;
  296. q->host_index = ((q->host_index + 1) % q->entry_count);
  297. }
  298. if (unlikely(released == 0 && !arm))
  299. return 0;
  300. /* ring doorbell for number popped */
  301. doorbell.word0 = 0;
  302. if (arm) {
  303. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  304. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  305. }
  306. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  307. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  308. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  309. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  310. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  311. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  312. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  313. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  314. readl(q->phba->sli4_hba.EQCQDBregaddr);
  315. return released;
  316. }
  317. /**
  318. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  319. * @q: The Completion Queue to get the first valid CQE from
  320. *
  321. * This routine will get the first valid Completion Queue Entry from @q, update
  322. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  323. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  324. * processed, but not popped back to the HBA then this routine will return NULL.
  325. **/
  326. static struct lpfc_cqe *
  327. lpfc_sli4_cq_get(struct lpfc_queue *q)
  328. {
  329. struct lpfc_cqe *cqe;
  330. uint32_t idx;
  331. /* sanity check on queue memory */
  332. if (unlikely(!q))
  333. return NULL;
  334. /* If the next CQE is not valid then we are done */
  335. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  336. return NULL;
  337. /* If the host has not yet processed the next entry then we are done */
  338. idx = ((q->hba_index + 1) % q->entry_count);
  339. if (idx == q->host_index)
  340. return NULL;
  341. cqe = q->qe[q->hba_index].cqe;
  342. q->hba_index = idx;
  343. /*
  344. * insert barrier for instruction interlock : data from the hardware
  345. * must have the valid bit checked before it can be copied and acted
  346. * upon. Speculative instructions were allowing a bcopy at the start
  347. * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
  348. * after our return, to copy data before the valid bit check above
  349. * was done. As such, some of the copied data was stale. The barrier
  350. * ensures the check is before any data is copied.
  351. */
  352. mb();
  353. return cqe;
  354. }
  355. /**
  356. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  357. * @q: The Completion Queue that the host has completed processing for.
  358. * @arm: Indicates whether the host wants to arms this CQ.
  359. *
  360. * This routine will mark all Completion queue entries on @q, from the last
  361. * known completed entry to the last entry that was processed, as completed
  362. * by clearing the valid bit for each completion queue entry. Then it will
  363. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  364. * The internal host index in the @q will be updated by this routine to indicate
  365. * that the host has finished processing the entries. The @arm parameter
  366. * indicates that the queue should be rearmed when ringing the doorbell.
  367. *
  368. * This function will return the number of CQEs that were released.
  369. **/
  370. uint32_t
  371. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  372. {
  373. uint32_t released = 0;
  374. struct lpfc_cqe *temp_qe;
  375. struct lpfc_register doorbell;
  376. /* sanity check on queue memory */
  377. if (unlikely(!q))
  378. return 0;
  379. /* while there are valid entries */
  380. while (q->hba_index != q->host_index) {
  381. temp_qe = q->qe[q->host_index].cqe;
  382. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  383. released++;
  384. q->host_index = ((q->host_index + 1) % q->entry_count);
  385. }
  386. if (unlikely(released == 0 && !arm))
  387. return 0;
  388. /* ring doorbell for number popped */
  389. doorbell.word0 = 0;
  390. if (arm)
  391. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  392. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  393. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  394. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  395. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  396. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  397. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  398. return released;
  399. }
  400. /**
  401. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  402. * @q: The Header Receive Queue to operate on.
  403. * @wqe: The Receive Queue Entry to put on the Receive queue.
  404. *
  405. * This routine will copy the contents of @wqe to the next available entry on
  406. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  407. * HBA to start processing the Receive Queue Entry. This function returns the
  408. * index that the rqe was copied to if successful. If no entries are available
  409. * on @q then this function will return -ENOMEM.
  410. * The caller is expected to hold the hbalock when calling this routine.
  411. **/
  412. static int
  413. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  414. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  415. {
  416. struct lpfc_rqe *temp_hrqe;
  417. struct lpfc_rqe *temp_drqe;
  418. struct lpfc_register doorbell;
  419. int put_index;
  420. /* sanity check on queue memory */
  421. if (unlikely(!hq) || unlikely(!dq))
  422. return -ENOMEM;
  423. put_index = hq->host_index;
  424. temp_hrqe = hq->qe[hq->host_index].rqe;
  425. temp_drqe = dq->qe[dq->host_index].rqe;
  426. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  427. return -EINVAL;
  428. if (hq->host_index != dq->host_index)
  429. return -EINVAL;
  430. /* If the host has not yet processed the next entry then we are done */
  431. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  432. return -EBUSY;
  433. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  434. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  435. /* Update the host index to point to the next slot */
  436. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  437. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  438. /* Ring The Header Receive Queue Doorbell */
  439. if (!(hq->host_index % hq->entry_repost)) {
  440. doorbell.word0 = 0;
  441. if (hq->db_format == LPFC_DB_RING_FORMAT) {
  442. bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
  443. hq->entry_repost);
  444. bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
  445. } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
  446. bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
  447. hq->entry_repost);
  448. bf_set(lpfc_rq_db_list_fm_index, &doorbell,
  449. hq->host_index);
  450. bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
  451. } else {
  452. return -EINVAL;
  453. }
  454. writel(doorbell.word0, hq->db_regaddr);
  455. }
  456. return put_index;
  457. }
  458. /**
  459. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  460. * @q: The Header Receive Queue to operate on.
  461. *
  462. * This routine will update the HBA index of a queue to reflect consumption of
  463. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  464. * consumed an entry the host calls this function to update the queue's
  465. * internal pointers. This routine returns the number of entries that were
  466. * consumed by the HBA.
  467. **/
  468. static uint32_t
  469. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  470. {
  471. /* sanity check on queue memory */
  472. if (unlikely(!hq) || unlikely(!dq))
  473. return 0;
  474. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  475. return 0;
  476. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  477. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  478. return 1;
  479. }
  480. /**
  481. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  482. * @phba: Pointer to HBA context object.
  483. * @pring: Pointer to driver SLI ring object.
  484. *
  485. * This function returns pointer to next command iocb entry
  486. * in the command ring. The caller must hold hbalock to prevent
  487. * other threads consume the next command iocb.
  488. * SLI-2/SLI-3 provide different sized iocbs.
  489. **/
  490. static inline IOCB_t *
  491. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  492. {
  493. return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
  494. pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
  495. }
  496. /**
  497. * lpfc_resp_iocb - Get next response iocb entry in the ring
  498. * @phba: Pointer to HBA context object.
  499. * @pring: Pointer to driver SLI ring object.
  500. *
  501. * This function returns pointer to next response iocb entry
  502. * in the response ring. The caller must hold hbalock to make sure
  503. * that no other thread consume the next response iocb.
  504. * SLI-2/SLI-3 provide different sized iocbs.
  505. **/
  506. static inline IOCB_t *
  507. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  508. {
  509. return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
  510. pring->sli.sli3.rspidx * phba->iocb_rsp_size);
  511. }
  512. /**
  513. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  514. * @phba: Pointer to HBA context object.
  515. *
  516. * This function is called with hbalock held. This function
  517. * allocates a new driver iocb object from the iocb pool. If the
  518. * allocation is successful, it returns pointer to the newly
  519. * allocated iocb object else it returns NULL.
  520. **/
  521. struct lpfc_iocbq *
  522. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  523. {
  524. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  525. struct lpfc_iocbq * iocbq = NULL;
  526. lockdep_assert_held(&phba->hbalock);
  527. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  528. if (iocbq)
  529. phba->iocb_cnt++;
  530. if (phba->iocb_cnt > phba->iocb_max)
  531. phba->iocb_max = phba->iocb_cnt;
  532. return iocbq;
  533. }
  534. /**
  535. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  536. * @phba: Pointer to HBA context object.
  537. * @xritag: XRI value.
  538. *
  539. * This function clears the sglq pointer from the array of acive
  540. * sglq's. The xritag that is passed in is used to index into the
  541. * array. Before the xritag can be used it needs to be adjusted
  542. * by subtracting the xribase.
  543. *
  544. * Returns sglq ponter = success, NULL = Failure.
  545. **/
  546. static struct lpfc_sglq *
  547. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  548. {
  549. struct lpfc_sglq *sglq;
  550. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  551. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  552. return sglq;
  553. }
  554. /**
  555. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  556. * @phba: Pointer to HBA context object.
  557. * @xritag: XRI value.
  558. *
  559. * This function returns the sglq pointer from the array of acive
  560. * sglq's. The xritag that is passed in is used to index into the
  561. * array. Before the xritag can be used it needs to be adjusted
  562. * by subtracting the xribase.
  563. *
  564. * Returns sglq ponter = success, NULL = Failure.
  565. **/
  566. struct lpfc_sglq *
  567. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  568. {
  569. struct lpfc_sglq *sglq;
  570. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  571. return sglq;
  572. }
  573. /**
  574. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  575. * @phba: Pointer to HBA context object.
  576. * @xritag: xri used in this exchange.
  577. * @rrq: The RRQ to be cleared.
  578. *
  579. **/
  580. void
  581. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  582. uint16_t xritag,
  583. struct lpfc_node_rrq *rrq)
  584. {
  585. struct lpfc_nodelist *ndlp = NULL;
  586. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  587. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  588. /* The target DID could have been swapped (cable swap)
  589. * we should use the ndlp from the findnode if it is
  590. * available.
  591. */
  592. if ((!ndlp) && rrq->ndlp)
  593. ndlp = rrq->ndlp;
  594. if (!ndlp)
  595. goto out;
  596. if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
  597. rrq->send_rrq = 0;
  598. rrq->xritag = 0;
  599. rrq->rrq_stop_time = 0;
  600. }
  601. out:
  602. mempool_free(rrq, phba->rrq_pool);
  603. }
  604. /**
  605. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  606. * @phba: Pointer to HBA context object.
  607. *
  608. * This function is called with hbalock held. This function
  609. * Checks if stop_time (ratov from setting rrq active) has
  610. * been reached, if it has and the send_rrq flag is set then
  611. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  612. * then it will just call the routine to clear the rrq and
  613. * free the rrq resource.
  614. * The timer is set to the next rrq that is going to expire before
  615. * leaving the routine.
  616. *
  617. **/
  618. void
  619. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  620. {
  621. struct lpfc_node_rrq *rrq;
  622. struct lpfc_node_rrq *nextrrq;
  623. unsigned long next_time;
  624. unsigned long iflags;
  625. LIST_HEAD(send_rrq);
  626. spin_lock_irqsave(&phba->hbalock, iflags);
  627. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  628. next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
  629. list_for_each_entry_safe(rrq, nextrrq,
  630. &phba->active_rrq_list, list) {
  631. if (time_after(jiffies, rrq->rrq_stop_time))
  632. list_move(&rrq->list, &send_rrq);
  633. else if (time_before(rrq->rrq_stop_time, next_time))
  634. next_time = rrq->rrq_stop_time;
  635. }
  636. spin_unlock_irqrestore(&phba->hbalock, iflags);
  637. if ((!list_empty(&phba->active_rrq_list)) &&
  638. (!(phba->pport->load_flag & FC_UNLOADING)))
  639. mod_timer(&phba->rrq_tmr, next_time);
  640. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  641. list_del(&rrq->list);
  642. if (!rrq->send_rrq)
  643. /* this call will free the rrq */
  644. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  645. else if (lpfc_send_rrq(phba, rrq)) {
  646. /* if we send the rrq then the completion handler
  647. * will clear the bit in the xribitmap.
  648. */
  649. lpfc_clr_rrq_active(phba, rrq->xritag,
  650. rrq);
  651. }
  652. }
  653. }
  654. /**
  655. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  656. * @vport: Pointer to vport context object.
  657. * @xri: The xri used in the exchange.
  658. * @did: The targets DID for this exchange.
  659. *
  660. * returns NULL = rrq not found in the phba->active_rrq_list.
  661. * rrq = rrq for this xri and target.
  662. **/
  663. struct lpfc_node_rrq *
  664. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  665. {
  666. struct lpfc_hba *phba = vport->phba;
  667. struct lpfc_node_rrq *rrq;
  668. struct lpfc_node_rrq *nextrrq;
  669. unsigned long iflags;
  670. if (phba->sli_rev != LPFC_SLI_REV4)
  671. return NULL;
  672. spin_lock_irqsave(&phba->hbalock, iflags);
  673. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  674. if (rrq->vport == vport && rrq->xritag == xri &&
  675. rrq->nlp_DID == did){
  676. list_del(&rrq->list);
  677. spin_unlock_irqrestore(&phba->hbalock, iflags);
  678. return rrq;
  679. }
  680. }
  681. spin_unlock_irqrestore(&phba->hbalock, iflags);
  682. return NULL;
  683. }
  684. /**
  685. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  686. * @vport: Pointer to vport context object.
  687. * @ndlp: Pointer to the lpfc_node_list structure.
  688. * If ndlp is NULL Remove all active RRQs for this vport from the
  689. * phba->active_rrq_list and clear the rrq.
  690. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  691. **/
  692. void
  693. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  694. {
  695. struct lpfc_hba *phba = vport->phba;
  696. struct lpfc_node_rrq *rrq;
  697. struct lpfc_node_rrq *nextrrq;
  698. unsigned long iflags;
  699. LIST_HEAD(rrq_list);
  700. if (phba->sli_rev != LPFC_SLI_REV4)
  701. return;
  702. if (!ndlp) {
  703. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  704. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  705. }
  706. spin_lock_irqsave(&phba->hbalock, iflags);
  707. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  708. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  709. list_move(&rrq->list, &rrq_list);
  710. spin_unlock_irqrestore(&phba->hbalock, iflags);
  711. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  712. list_del(&rrq->list);
  713. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  714. }
  715. }
  716. /**
  717. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  718. * @phba: Pointer to HBA context object.
  719. * @ndlp: Targets nodelist pointer for this exchange.
  720. * @xritag the xri in the bitmap to test.
  721. *
  722. * This function is called with hbalock held. This function
  723. * returns 0 = rrq not active for this xri
  724. * 1 = rrq is valid for this xri.
  725. **/
  726. int
  727. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  728. uint16_t xritag)
  729. {
  730. lockdep_assert_held(&phba->hbalock);
  731. if (!ndlp)
  732. return 0;
  733. if (!ndlp->active_rrqs_xri_bitmap)
  734. return 0;
  735. if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
  736. return 1;
  737. else
  738. return 0;
  739. }
  740. /**
  741. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  742. * @phba: Pointer to HBA context object.
  743. * @ndlp: nodelist pointer for this target.
  744. * @xritag: xri used in this exchange.
  745. * @rxid: Remote Exchange ID.
  746. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  747. *
  748. * This function takes the hbalock.
  749. * The active bit is always set in the active rrq xri_bitmap even
  750. * if there is no slot avaiable for the other rrq information.
  751. *
  752. * returns 0 rrq actived for this xri
  753. * < 0 No memory or invalid ndlp.
  754. **/
  755. int
  756. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  757. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  758. {
  759. unsigned long iflags;
  760. struct lpfc_node_rrq *rrq;
  761. int empty;
  762. if (!ndlp)
  763. return -EINVAL;
  764. if (!phba->cfg_enable_rrq)
  765. return -EINVAL;
  766. spin_lock_irqsave(&phba->hbalock, iflags);
  767. if (phba->pport->load_flag & FC_UNLOADING) {
  768. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  769. goto out;
  770. }
  771. /*
  772. * set the active bit even if there is no mem available.
  773. */
  774. if (NLP_CHK_FREE_REQ(ndlp))
  775. goto out;
  776. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  777. goto out;
  778. if (!ndlp->active_rrqs_xri_bitmap)
  779. goto out;
  780. if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
  781. goto out;
  782. spin_unlock_irqrestore(&phba->hbalock, iflags);
  783. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  784. if (!rrq) {
  785. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  786. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  787. " DID:0x%x Send:%d\n",
  788. xritag, rxid, ndlp->nlp_DID, send_rrq);
  789. return -EINVAL;
  790. }
  791. if (phba->cfg_enable_rrq == 1)
  792. rrq->send_rrq = send_rrq;
  793. else
  794. rrq->send_rrq = 0;
  795. rrq->xritag = xritag;
  796. rrq->rrq_stop_time = jiffies +
  797. msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
  798. rrq->ndlp = ndlp;
  799. rrq->nlp_DID = ndlp->nlp_DID;
  800. rrq->vport = ndlp->vport;
  801. rrq->rxid = rxid;
  802. spin_lock_irqsave(&phba->hbalock, iflags);
  803. empty = list_empty(&phba->active_rrq_list);
  804. list_add_tail(&rrq->list, &phba->active_rrq_list);
  805. phba->hba_flag |= HBA_RRQ_ACTIVE;
  806. if (empty)
  807. lpfc_worker_wake_up(phba);
  808. spin_unlock_irqrestore(&phba->hbalock, iflags);
  809. return 0;
  810. out:
  811. spin_unlock_irqrestore(&phba->hbalock, iflags);
  812. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  813. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  814. " DID:0x%x Send:%d\n",
  815. xritag, rxid, ndlp->nlp_DID, send_rrq);
  816. return -EINVAL;
  817. }
  818. /**
  819. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  820. * @phba: Pointer to HBA context object.
  821. * @piocb: Pointer to the iocbq.
  822. *
  823. * This function is called with the ring lock held. This function
  824. * gets a new driver sglq object from the sglq list. If the
  825. * list is not empty then it is successful, it returns pointer to the newly
  826. * allocated sglq object else it returns NULL.
  827. **/
  828. static struct lpfc_sglq *
  829. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  830. {
  831. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  832. struct lpfc_sglq *sglq = NULL;
  833. struct lpfc_sglq *start_sglq = NULL;
  834. struct lpfc_scsi_buf *lpfc_cmd;
  835. struct lpfc_nodelist *ndlp;
  836. int found = 0;
  837. lockdep_assert_held(&phba->hbalock);
  838. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  839. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  840. ndlp = lpfc_cmd->rdata->pnode;
  841. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  842. !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  843. ndlp = piocbq->context_un.ndlp;
  844. } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
  845. if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
  846. ndlp = NULL;
  847. else
  848. ndlp = piocbq->context_un.ndlp;
  849. } else {
  850. ndlp = piocbq->context1;
  851. }
  852. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  853. start_sglq = sglq;
  854. while (!found) {
  855. if (!sglq)
  856. return NULL;
  857. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
  858. /* This xri has an rrq outstanding for this DID.
  859. * put it back in the list and get another xri.
  860. */
  861. list_add_tail(&sglq->list, lpfc_sgl_list);
  862. sglq = NULL;
  863. list_remove_head(lpfc_sgl_list, sglq,
  864. struct lpfc_sglq, list);
  865. if (sglq == start_sglq) {
  866. sglq = NULL;
  867. break;
  868. } else
  869. continue;
  870. }
  871. sglq->ndlp = ndlp;
  872. found = 1;
  873. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  874. sglq->state = SGL_ALLOCATED;
  875. }
  876. return sglq;
  877. }
  878. /**
  879. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  880. * @phba: Pointer to HBA context object.
  881. *
  882. * This function is called with no lock held. This function
  883. * allocates a new driver iocb object from the iocb pool. If the
  884. * allocation is successful, it returns pointer to the newly
  885. * allocated iocb object else it returns NULL.
  886. **/
  887. struct lpfc_iocbq *
  888. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  889. {
  890. struct lpfc_iocbq * iocbq = NULL;
  891. unsigned long iflags;
  892. spin_lock_irqsave(&phba->hbalock, iflags);
  893. iocbq = __lpfc_sli_get_iocbq(phba);
  894. spin_unlock_irqrestore(&phba->hbalock, iflags);
  895. return iocbq;
  896. }
  897. /**
  898. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  899. * @phba: Pointer to HBA context object.
  900. * @iocbq: Pointer to driver iocb object.
  901. *
  902. * This function is called with hbalock held to release driver
  903. * iocb object to the iocb pool. The iotag in the iocb object
  904. * does not change for each use of the iocb object. This function
  905. * clears all other fields of the iocb object when it is freed.
  906. * The sqlq structure that holds the xritag and phys and virtual
  907. * mappings for the scatter gather list is retrieved from the
  908. * active array of sglq. The get of the sglq pointer also clears
  909. * the entry in the array. If the status of the IO indiactes that
  910. * this IO was aborted then the sglq entry it put on the
  911. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  912. * IO has good status or fails for any other reason then the sglq
  913. * entry is added to the free list (lpfc_sgl_list).
  914. **/
  915. static void
  916. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  917. {
  918. struct lpfc_sglq *sglq;
  919. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  920. unsigned long iflag = 0;
  921. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  922. lockdep_assert_held(&phba->hbalock);
  923. if (iocbq->sli4_xritag == NO_XRI)
  924. sglq = NULL;
  925. else
  926. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  927. if (sglq) {
  928. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  929. (sglq->state != SGL_XRI_ABORTED)) {
  930. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  931. iflag);
  932. list_add(&sglq->list,
  933. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  934. spin_unlock_irqrestore(
  935. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  936. } else {
  937. spin_lock_irqsave(&pring->ring_lock, iflag);
  938. sglq->state = SGL_FREED;
  939. sglq->ndlp = NULL;
  940. list_add_tail(&sglq->list,
  941. &phba->sli4_hba.lpfc_sgl_list);
  942. spin_unlock_irqrestore(&pring->ring_lock, iflag);
  943. /* Check if TXQ queue needs to be serviced */
  944. if (!list_empty(&pring->txq))
  945. lpfc_worker_wake_up(phba);
  946. }
  947. }
  948. /*
  949. * Clean all volatile data fields, preserve iotag and node struct.
  950. */
  951. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  952. iocbq->sli4_lxritag = NO_XRI;
  953. iocbq->sli4_xritag = NO_XRI;
  954. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  955. }
  956. /**
  957. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  958. * @phba: Pointer to HBA context object.
  959. * @iocbq: Pointer to driver iocb object.
  960. *
  961. * This function is called with hbalock held to release driver
  962. * iocb object to the iocb pool. The iotag in the iocb object
  963. * does not change for each use of the iocb object. This function
  964. * clears all other fields of the iocb object when it is freed.
  965. **/
  966. static void
  967. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  968. {
  969. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  970. lockdep_assert_held(&phba->hbalock);
  971. /*
  972. * Clean all volatile data fields, preserve iotag and node struct.
  973. */
  974. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  975. iocbq->sli4_xritag = NO_XRI;
  976. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  977. }
  978. /**
  979. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  980. * @phba: Pointer to HBA context object.
  981. * @iocbq: Pointer to driver iocb object.
  982. *
  983. * This function is called with hbalock held to release driver
  984. * iocb object to the iocb pool. The iotag in the iocb object
  985. * does not change for each use of the iocb object. This function
  986. * clears all other fields of the iocb object when it is freed.
  987. **/
  988. static void
  989. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  990. {
  991. lockdep_assert_held(&phba->hbalock);
  992. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  993. phba->iocb_cnt--;
  994. }
  995. /**
  996. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  997. * @phba: Pointer to HBA context object.
  998. * @iocbq: Pointer to driver iocb object.
  999. *
  1000. * This function is called with no lock held to release the iocb to
  1001. * iocb pool.
  1002. **/
  1003. void
  1004. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1005. {
  1006. unsigned long iflags;
  1007. /*
  1008. * Clean all volatile data fields, preserve iotag and node struct.
  1009. */
  1010. spin_lock_irqsave(&phba->hbalock, iflags);
  1011. __lpfc_sli_release_iocbq(phba, iocbq);
  1012. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1013. }
  1014. /**
  1015. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  1016. * @phba: Pointer to HBA context object.
  1017. * @iocblist: List of IOCBs.
  1018. * @ulpstatus: ULP status in IOCB command field.
  1019. * @ulpWord4: ULP word-4 in IOCB command field.
  1020. *
  1021. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  1022. * on the list by invoking the complete callback function associated with the
  1023. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  1024. * fields.
  1025. **/
  1026. void
  1027. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  1028. uint32_t ulpstatus, uint32_t ulpWord4)
  1029. {
  1030. struct lpfc_iocbq *piocb;
  1031. while (!list_empty(iocblist)) {
  1032. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  1033. if (!piocb->iocb_cmpl)
  1034. lpfc_sli_release_iocbq(phba, piocb);
  1035. else {
  1036. piocb->iocb.ulpStatus = ulpstatus;
  1037. piocb->iocb.un.ulpWord[4] = ulpWord4;
  1038. (piocb->iocb_cmpl) (phba, piocb, piocb);
  1039. }
  1040. }
  1041. return;
  1042. }
  1043. /**
  1044. * lpfc_sli_iocb_cmd_type - Get the iocb type
  1045. * @iocb_cmnd: iocb command code.
  1046. *
  1047. * This function is called by ring event handler function to get the iocb type.
  1048. * This function translates the iocb command to an iocb command type used to
  1049. * decide the final disposition of each completed IOCB.
  1050. * The function returns
  1051. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  1052. * LPFC_SOL_IOCB if it is a solicited iocb completion
  1053. * LPFC_ABORT_IOCB if it is an abort iocb
  1054. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  1055. *
  1056. * The caller is not required to hold any lock.
  1057. **/
  1058. static lpfc_iocb_type
  1059. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1060. {
  1061. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1062. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1063. return 0;
  1064. switch (iocb_cmnd) {
  1065. case CMD_XMIT_SEQUENCE_CR:
  1066. case CMD_XMIT_SEQUENCE_CX:
  1067. case CMD_XMIT_BCAST_CN:
  1068. case CMD_XMIT_BCAST_CX:
  1069. case CMD_ELS_REQUEST_CR:
  1070. case CMD_ELS_REQUEST_CX:
  1071. case CMD_CREATE_XRI_CR:
  1072. case CMD_CREATE_XRI_CX:
  1073. case CMD_GET_RPI_CN:
  1074. case CMD_XMIT_ELS_RSP_CX:
  1075. case CMD_GET_RPI_CR:
  1076. case CMD_FCP_IWRITE_CR:
  1077. case CMD_FCP_IWRITE_CX:
  1078. case CMD_FCP_IREAD_CR:
  1079. case CMD_FCP_IREAD_CX:
  1080. case CMD_FCP_ICMND_CR:
  1081. case CMD_FCP_ICMND_CX:
  1082. case CMD_FCP_TSEND_CX:
  1083. case CMD_FCP_TRSP_CX:
  1084. case CMD_FCP_TRECEIVE_CX:
  1085. case CMD_FCP_AUTO_TRSP_CX:
  1086. case CMD_ADAPTER_MSG:
  1087. case CMD_ADAPTER_DUMP:
  1088. case CMD_XMIT_SEQUENCE64_CR:
  1089. case CMD_XMIT_SEQUENCE64_CX:
  1090. case CMD_XMIT_BCAST64_CN:
  1091. case CMD_XMIT_BCAST64_CX:
  1092. case CMD_ELS_REQUEST64_CR:
  1093. case CMD_ELS_REQUEST64_CX:
  1094. case CMD_FCP_IWRITE64_CR:
  1095. case CMD_FCP_IWRITE64_CX:
  1096. case CMD_FCP_IREAD64_CR:
  1097. case CMD_FCP_IREAD64_CX:
  1098. case CMD_FCP_ICMND64_CR:
  1099. case CMD_FCP_ICMND64_CX:
  1100. case CMD_FCP_TSEND64_CX:
  1101. case CMD_FCP_TRSP64_CX:
  1102. case CMD_FCP_TRECEIVE64_CX:
  1103. case CMD_GEN_REQUEST64_CR:
  1104. case CMD_GEN_REQUEST64_CX:
  1105. case CMD_XMIT_ELS_RSP64_CX:
  1106. case DSSCMD_IWRITE64_CR:
  1107. case DSSCMD_IWRITE64_CX:
  1108. case DSSCMD_IREAD64_CR:
  1109. case DSSCMD_IREAD64_CX:
  1110. type = LPFC_SOL_IOCB;
  1111. break;
  1112. case CMD_ABORT_XRI_CN:
  1113. case CMD_ABORT_XRI_CX:
  1114. case CMD_CLOSE_XRI_CN:
  1115. case CMD_CLOSE_XRI_CX:
  1116. case CMD_XRI_ABORTED_CX:
  1117. case CMD_ABORT_MXRI64_CN:
  1118. case CMD_XMIT_BLS_RSP64_CX:
  1119. type = LPFC_ABORT_IOCB;
  1120. break;
  1121. case CMD_RCV_SEQUENCE_CX:
  1122. case CMD_RCV_ELS_REQ_CX:
  1123. case CMD_RCV_SEQUENCE64_CX:
  1124. case CMD_RCV_ELS_REQ64_CX:
  1125. case CMD_ASYNC_STATUS:
  1126. case CMD_IOCB_RCV_SEQ64_CX:
  1127. case CMD_IOCB_RCV_ELS64_CX:
  1128. case CMD_IOCB_RCV_CONT64_CX:
  1129. case CMD_IOCB_RET_XRI64_CX:
  1130. type = LPFC_UNSOL_IOCB;
  1131. break;
  1132. case CMD_IOCB_XMIT_MSEQ64_CR:
  1133. case CMD_IOCB_XMIT_MSEQ64_CX:
  1134. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1135. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1136. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1137. case CMD_IOCB_ABORT_EXTENDED_CN:
  1138. case CMD_IOCB_RET_HBQE64_CN:
  1139. case CMD_IOCB_FCP_IBIDIR64_CR:
  1140. case CMD_IOCB_FCP_IBIDIR64_CX:
  1141. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1142. case CMD_IOCB_LOGENTRY_CN:
  1143. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1144. printk("%s - Unhandled SLI-3 Command x%x\n",
  1145. __func__, iocb_cmnd);
  1146. type = LPFC_UNKNOWN_IOCB;
  1147. break;
  1148. default:
  1149. type = LPFC_UNKNOWN_IOCB;
  1150. break;
  1151. }
  1152. return type;
  1153. }
  1154. /**
  1155. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1156. * @phba: Pointer to HBA context object.
  1157. *
  1158. * This function is called from SLI initialization code
  1159. * to configure every ring of the HBA's SLI interface. The
  1160. * caller is not required to hold any lock. This function issues
  1161. * a config_ring mailbox command for each ring.
  1162. * This function returns zero if successful else returns a negative
  1163. * error code.
  1164. **/
  1165. static int
  1166. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1167. {
  1168. struct lpfc_sli *psli = &phba->sli;
  1169. LPFC_MBOXQ_t *pmb;
  1170. MAILBOX_t *pmbox;
  1171. int i, rc, ret = 0;
  1172. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1173. if (!pmb)
  1174. return -ENOMEM;
  1175. pmbox = &pmb->u.mb;
  1176. phba->link_state = LPFC_INIT_MBX_CMDS;
  1177. for (i = 0; i < psli->num_rings; i++) {
  1178. lpfc_config_ring(phba, i, pmb);
  1179. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1180. if (rc != MBX_SUCCESS) {
  1181. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1182. "0446 Adapter failed to init (%d), "
  1183. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1184. "ring %d\n",
  1185. rc, pmbox->mbxCommand,
  1186. pmbox->mbxStatus, i);
  1187. phba->link_state = LPFC_HBA_ERROR;
  1188. ret = -ENXIO;
  1189. break;
  1190. }
  1191. }
  1192. mempool_free(pmb, phba->mbox_mem_pool);
  1193. return ret;
  1194. }
  1195. /**
  1196. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1197. * @phba: Pointer to HBA context object.
  1198. * @pring: Pointer to driver SLI ring object.
  1199. * @piocb: Pointer to the driver iocb object.
  1200. *
  1201. * This function is called with hbalock held. The function adds the
  1202. * new iocb to txcmplq of the given ring. This function always returns
  1203. * 0. If this function is called for ELS ring, this function checks if
  1204. * there is a vport associated with the ELS command. This function also
  1205. * starts els_tmofunc timer if this is an ELS command.
  1206. **/
  1207. static int
  1208. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1209. struct lpfc_iocbq *piocb)
  1210. {
  1211. lockdep_assert_held(&phba->hbalock);
  1212. BUG_ON(!piocb || !piocb->vport);
  1213. list_add_tail(&piocb->list, &pring->txcmplq);
  1214. piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
  1215. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1216. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1217. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN) &&
  1218. (!(piocb->vport->load_flag & FC_UNLOADING)))
  1219. mod_timer(&piocb->vport->els_tmofunc,
  1220. jiffies +
  1221. msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
  1222. return 0;
  1223. }
  1224. /**
  1225. * lpfc_sli_ringtx_get - Get first element of the txq
  1226. * @phba: Pointer to HBA context object.
  1227. * @pring: Pointer to driver SLI ring object.
  1228. *
  1229. * This function is called with hbalock held to get next
  1230. * iocb in txq of the given ring. If there is any iocb in
  1231. * the txq, the function returns first iocb in the list after
  1232. * removing the iocb from the list, else it returns NULL.
  1233. **/
  1234. struct lpfc_iocbq *
  1235. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1236. {
  1237. struct lpfc_iocbq *cmd_iocb;
  1238. lockdep_assert_held(&phba->hbalock);
  1239. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1240. return cmd_iocb;
  1241. }
  1242. /**
  1243. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1244. * @phba: Pointer to HBA context object.
  1245. * @pring: Pointer to driver SLI ring object.
  1246. *
  1247. * This function is called with hbalock held and the caller must post the
  1248. * iocb without releasing the lock. If the caller releases the lock,
  1249. * iocb slot returned by the function is not guaranteed to be available.
  1250. * The function returns pointer to the next available iocb slot if there
  1251. * is available slot in the ring, else it returns NULL.
  1252. * If the get index of the ring is ahead of the put index, the function
  1253. * will post an error attention event to the worker thread to take the
  1254. * HBA to offline state.
  1255. **/
  1256. static IOCB_t *
  1257. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1258. {
  1259. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1260. uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
  1261. lockdep_assert_held(&phba->hbalock);
  1262. if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
  1263. (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
  1264. pring->sli.sli3.next_cmdidx = 0;
  1265. if (unlikely(pring->sli.sli3.local_getidx ==
  1266. pring->sli.sli3.next_cmdidx)) {
  1267. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1268. if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
  1269. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1270. "0315 Ring %d issue: portCmdGet %d "
  1271. "is bigger than cmd ring %d\n",
  1272. pring->ringno,
  1273. pring->sli.sli3.local_getidx,
  1274. max_cmd_idx);
  1275. phba->link_state = LPFC_HBA_ERROR;
  1276. /*
  1277. * All error attention handlers are posted to
  1278. * worker thread
  1279. */
  1280. phba->work_ha |= HA_ERATT;
  1281. phba->work_hs = HS_FFER3;
  1282. lpfc_worker_wake_up(phba);
  1283. return NULL;
  1284. }
  1285. if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
  1286. return NULL;
  1287. }
  1288. return lpfc_cmd_iocb(phba, pring);
  1289. }
  1290. /**
  1291. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1292. * @phba: Pointer to HBA context object.
  1293. * @iocbq: Pointer to driver iocb object.
  1294. *
  1295. * This function gets an iotag for the iocb. If there is no unused iotag and
  1296. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1297. * array and assigns a new iotag.
  1298. * The function returns the allocated iotag if successful, else returns zero.
  1299. * Zero is not a valid iotag.
  1300. * The caller is not required to hold any lock.
  1301. **/
  1302. uint16_t
  1303. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1304. {
  1305. struct lpfc_iocbq **new_arr;
  1306. struct lpfc_iocbq **old_arr;
  1307. size_t new_len;
  1308. struct lpfc_sli *psli = &phba->sli;
  1309. uint16_t iotag;
  1310. spin_lock_irq(&phba->hbalock);
  1311. iotag = psli->last_iotag;
  1312. if(++iotag < psli->iocbq_lookup_len) {
  1313. psli->last_iotag = iotag;
  1314. psli->iocbq_lookup[iotag] = iocbq;
  1315. spin_unlock_irq(&phba->hbalock);
  1316. iocbq->iotag = iotag;
  1317. return iotag;
  1318. } else if (psli->iocbq_lookup_len < (0xffff
  1319. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1320. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1321. spin_unlock_irq(&phba->hbalock);
  1322. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1323. GFP_KERNEL);
  1324. if (new_arr) {
  1325. spin_lock_irq(&phba->hbalock);
  1326. old_arr = psli->iocbq_lookup;
  1327. if (new_len <= psli->iocbq_lookup_len) {
  1328. /* highly unprobable case */
  1329. kfree(new_arr);
  1330. iotag = psli->last_iotag;
  1331. if(++iotag < psli->iocbq_lookup_len) {
  1332. psli->last_iotag = iotag;
  1333. psli->iocbq_lookup[iotag] = iocbq;
  1334. spin_unlock_irq(&phba->hbalock);
  1335. iocbq->iotag = iotag;
  1336. return iotag;
  1337. }
  1338. spin_unlock_irq(&phba->hbalock);
  1339. return 0;
  1340. }
  1341. if (psli->iocbq_lookup)
  1342. memcpy(new_arr, old_arr,
  1343. ((psli->last_iotag + 1) *
  1344. sizeof (struct lpfc_iocbq *)));
  1345. psli->iocbq_lookup = new_arr;
  1346. psli->iocbq_lookup_len = new_len;
  1347. psli->last_iotag = iotag;
  1348. psli->iocbq_lookup[iotag] = iocbq;
  1349. spin_unlock_irq(&phba->hbalock);
  1350. iocbq->iotag = iotag;
  1351. kfree(old_arr);
  1352. return iotag;
  1353. }
  1354. } else
  1355. spin_unlock_irq(&phba->hbalock);
  1356. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1357. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1358. psli->last_iotag);
  1359. return 0;
  1360. }
  1361. /**
  1362. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1363. * @phba: Pointer to HBA context object.
  1364. * @pring: Pointer to driver SLI ring object.
  1365. * @iocb: Pointer to iocb slot in the ring.
  1366. * @nextiocb: Pointer to driver iocb object which need to be
  1367. * posted to firmware.
  1368. *
  1369. * This function is called with hbalock held to post a new iocb to
  1370. * the firmware. This function copies the new iocb to ring iocb slot and
  1371. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1372. * a completion call back for this iocb else the function will free the
  1373. * iocb object.
  1374. **/
  1375. static void
  1376. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1377. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1378. {
  1379. lockdep_assert_held(&phba->hbalock);
  1380. /*
  1381. * Set up an iotag
  1382. */
  1383. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1384. if (pring->ringno == LPFC_ELS_RING) {
  1385. lpfc_debugfs_slow_ring_trc(phba,
  1386. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1387. *(((uint32_t *) &nextiocb->iocb) + 4),
  1388. *(((uint32_t *) &nextiocb->iocb) + 6),
  1389. *(((uint32_t *) &nextiocb->iocb) + 7));
  1390. }
  1391. /*
  1392. * Issue iocb command to adapter
  1393. */
  1394. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1395. wmb();
  1396. pring->stats.iocb_cmd++;
  1397. /*
  1398. * If there is no completion routine to call, we can release the
  1399. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1400. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1401. */
  1402. if (nextiocb->iocb_cmpl)
  1403. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1404. else
  1405. __lpfc_sli_release_iocbq(phba, nextiocb);
  1406. /*
  1407. * Let the HBA know what IOCB slot will be the next one the
  1408. * driver will put a command into.
  1409. */
  1410. pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
  1411. writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1412. }
  1413. /**
  1414. * lpfc_sli_update_full_ring - Update the chip attention register
  1415. * @phba: Pointer to HBA context object.
  1416. * @pring: Pointer to driver SLI ring object.
  1417. *
  1418. * The caller is not required to hold any lock for calling this function.
  1419. * This function updates the chip attention bits for the ring to inform firmware
  1420. * that there are pending work to be done for this ring and requests an
  1421. * interrupt when there is space available in the ring. This function is
  1422. * called when the driver is unable to post more iocbs to the ring due
  1423. * to unavailability of space in the ring.
  1424. **/
  1425. static void
  1426. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1427. {
  1428. int ringno = pring->ringno;
  1429. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1430. wmb();
  1431. /*
  1432. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1433. * The HBA will tell us when an IOCB entry is available.
  1434. */
  1435. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1436. readl(phba->CAregaddr); /* flush */
  1437. pring->stats.iocb_cmd_full++;
  1438. }
  1439. /**
  1440. * lpfc_sli_update_ring - Update chip attention register
  1441. * @phba: Pointer to HBA context object.
  1442. * @pring: Pointer to driver SLI ring object.
  1443. *
  1444. * This function updates the chip attention register bit for the
  1445. * given ring to inform HBA that there is more work to be done
  1446. * in this ring. The caller is not required to hold any lock.
  1447. **/
  1448. static void
  1449. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1450. {
  1451. int ringno = pring->ringno;
  1452. /*
  1453. * Tell the HBA that there is work to do in this ring.
  1454. */
  1455. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1456. wmb();
  1457. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1458. readl(phba->CAregaddr); /* flush */
  1459. }
  1460. }
  1461. /**
  1462. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1463. * @phba: Pointer to HBA context object.
  1464. * @pring: Pointer to driver SLI ring object.
  1465. *
  1466. * This function is called with hbalock held to post pending iocbs
  1467. * in the txq to the firmware. This function is called when driver
  1468. * detects space available in the ring.
  1469. **/
  1470. static void
  1471. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1472. {
  1473. IOCB_t *iocb;
  1474. struct lpfc_iocbq *nextiocb;
  1475. lockdep_assert_held(&phba->hbalock);
  1476. /*
  1477. * Check to see if:
  1478. * (a) there is anything on the txq to send
  1479. * (b) link is up
  1480. * (c) link attention events can be processed (fcp ring only)
  1481. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1482. */
  1483. if (lpfc_is_link_up(phba) &&
  1484. (!list_empty(&pring->txq)) &&
  1485. (pring->ringno != phba->sli.fcp_ring ||
  1486. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1487. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1488. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1489. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1490. if (iocb)
  1491. lpfc_sli_update_ring(phba, pring);
  1492. else
  1493. lpfc_sli_update_full_ring(phba, pring);
  1494. }
  1495. return;
  1496. }
  1497. /**
  1498. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1499. * @phba: Pointer to HBA context object.
  1500. * @hbqno: HBQ number.
  1501. *
  1502. * This function is called with hbalock held to get the next
  1503. * available slot for the given HBQ. If there is free slot
  1504. * available for the HBQ it will return pointer to the next available
  1505. * HBQ entry else it will return NULL.
  1506. **/
  1507. static struct lpfc_hbq_entry *
  1508. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1509. {
  1510. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1511. lockdep_assert_held(&phba->hbalock);
  1512. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1513. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1514. hbqp->next_hbqPutIdx = 0;
  1515. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1516. uint32_t raw_index = phba->hbq_get[hbqno];
  1517. uint32_t getidx = le32_to_cpu(raw_index);
  1518. hbqp->local_hbqGetIdx = getidx;
  1519. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1520. lpfc_printf_log(phba, KERN_ERR,
  1521. LOG_SLI | LOG_VPORT,
  1522. "1802 HBQ %d: local_hbqGetIdx "
  1523. "%u is > than hbqp->entry_count %u\n",
  1524. hbqno, hbqp->local_hbqGetIdx,
  1525. hbqp->entry_count);
  1526. phba->link_state = LPFC_HBA_ERROR;
  1527. return NULL;
  1528. }
  1529. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1530. return NULL;
  1531. }
  1532. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1533. hbqp->hbqPutIdx;
  1534. }
  1535. /**
  1536. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1537. * @phba: Pointer to HBA context object.
  1538. *
  1539. * This function is called with no lock held to free all the
  1540. * hbq buffers while uninitializing the SLI interface. It also
  1541. * frees the HBQ buffers returned by the firmware but not yet
  1542. * processed by the upper layers.
  1543. **/
  1544. void
  1545. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1546. {
  1547. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1548. struct hbq_dmabuf *hbq_buf;
  1549. unsigned long flags;
  1550. int i, hbq_count;
  1551. uint32_t hbqno;
  1552. hbq_count = lpfc_sli_hbq_count();
  1553. /* Return all memory used by all HBQs */
  1554. spin_lock_irqsave(&phba->hbalock, flags);
  1555. for (i = 0; i < hbq_count; ++i) {
  1556. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1557. &phba->hbqs[i].hbq_buffer_list, list) {
  1558. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1559. list_del(&hbq_buf->dbuf.list);
  1560. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1561. }
  1562. phba->hbqs[i].buffer_count = 0;
  1563. }
  1564. /* Return all HBQ buffer that are in-fly */
  1565. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1566. list) {
  1567. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1568. list_del(&hbq_buf->dbuf.list);
  1569. if (hbq_buf->tag == -1) {
  1570. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1571. (phba, hbq_buf);
  1572. } else {
  1573. hbqno = hbq_buf->tag >> 16;
  1574. if (hbqno >= LPFC_MAX_HBQS)
  1575. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1576. (phba, hbq_buf);
  1577. else
  1578. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1579. hbq_buf);
  1580. }
  1581. }
  1582. /* Mark the HBQs not in use */
  1583. phba->hbq_in_use = 0;
  1584. spin_unlock_irqrestore(&phba->hbalock, flags);
  1585. }
  1586. /**
  1587. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1588. * @phba: Pointer to HBA context object.
  1589. * @hbqno: HBQ number.
  1590. * @hbq_buf: Pointer to HBQ buffer.
  1591. *
  1592. * This function is called with the hbalock held to post a
  1593. * hbq buffer to the firmware. If the function finds an empty
  1594. * slot in the HBQ, it will post the buffer. The function will return
  1595. * pointer to the hbq entry if it successfully post the buffer
  1596. * else it will return NULL.
  1597. **/
  1598. static int
  1599. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1600. struct hbq_dmabuf *hbq_buf)
  1601. {
  1602. lockdep_assert_held(&phba->hbalock);
  1603. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1604. }
  1605. /**
  1606. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1607. * @phba: Pointer to HBA context object.
  1608. * @hbqno: HBQ number.
  1609. * @hbq_buf: Pointer to HBQ buffer.
  1610. *
  1611. * This function is called with the hbalock held to post a hbq buffer to the
  1612. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1613. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1614. * it successfully post the buffer else it will return an error.
  1615. **/
  1616. static int
  1617. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1618. struct hbq_dmabuf *hbq_buf)
  1619. {
  1620. struct lpfc_hbq_entry *hbqe;
  1621. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1622. lockdep_assert_held(&phba->hbalock);
  1623. /* Get next HBQ entry slot to use */
  1624. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1625. if (hbqe) {
  1626. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1627. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1628. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1629. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1630. hbqe->bde.tus.f.bdeFlags = 0;
  1631. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1632. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1633. /* Sync SLIM */
  1634. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1635. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1636. /* flush */
  1637. readl(phba->hbq_put + hbqno);
  1638. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1639. return 0;
  1640. } else
  1641. return -ENOMEM;
  1642. }
  1643. /**
  1644. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1645. * @phba: Pointer to HBA context object.
  1646. * @hbqno: HBQ number.
  1647. * @hbq_buf: Pointer to HBQ buffer.
  1648. *
  1649. * This function is called with the hbalock held to post an RQE to the SLI4
  1650. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1651. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1652. **/
  1653. static int
  1654. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1655. struct hbq_dmabuf *hbq_buf)
  1656. {
  1657. int rc;
  1658. struct lpfc_rqe hrqe;
  1659. struct lpfc_rqe drqe;
  1660. lockdep_assert_held(&phba->hbalock);
  1661. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1662. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1663. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1664. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1665. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1666. &hrqe, &drqe);
  1667. if (rc < 0)
  1668. return rc;
  1669. hbq_buf->tag = rc;
  1670. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1671. return 0;
  1672. }
  1673. /* HBQ for ELS and CT traffic. */
  1674. static struct lpfc_hbq_init lpfc_els_hbq = {
  1675. .rn = 1,
  1676. .entry_count = 256,
  1677. .mask_count = 0,
  1678. .profile = 0,
  1679. .ring_mask = (1 << LPFC_ELS_RING),
  1680. .buffer_count = 0,
  1681. .init_count = 40,
  1682. .add_count = 40,
  1683. };
  1684. /* HBQ for the extra ring if needed */
  1685. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1686. .rn = 1,
  1687. .entry_count = 200,
  1688. .mask_count = 0,
  1689. .profile = 0,
  1690. .ring_mask = (1 << LPFC_EXTRA_RING),
  1691. .buffer_count = 0,
  1692. .init_count = 0,
  1693. .add_count = 5,
  1694. };
  1695. /* Array of HBQs */
  1696. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1697. &lpfc_els_hbq,
  1698. &lpfc_extra_hbq,
  1699. };
  1700. /**
  1701. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1702. * @phba: Pointer to HBA context object.
  1703. * @hbqno: HBQ number.
  1704. * @count: Number of HBQ buffers to be posted.
  1705. *
  1706. * This function is called with no lock held to post more hbq buffers to the
  1707. * given HBQ. The function returns the number of HBQ buffers successfully
  1708. * posted.
  1709. **/
  1710. static int
  1711. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1712. {
  1713. uint32_t i, posted = 0;
  1714. unsigned long flags;
  1715. struct hbq_dmabuf *hbq_buffer;
  1716. LIST_HEAD(hbq_buf_list);
  1717. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1718. return 0;
  1719. if ((phba->hbqs[hbqno].buffer_count + count) >
  1720. lpfc_hbq_defs[hbqno]->entry_count)
  1721. count = lpfc_hbq_defs[hbqno]->entry_count -
  1722. phba->hbqs[hbqno].buffer_count;
  1723. if (!count)
  1724. return 0;
  1725. /* Allocate HBQ entries */
  1726. for (i = 0; i < count; i++) {
  1727. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1728. if (!hbq_buffer)
  1729. break;
  1730. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1731. }
  1732. /* Check whether HBQ is still in use */
  1733. spin_lock_irqsave(&phba->hbalock, flags);
  1734. if (!phba->hbq_in_use)
  1735. goto err;
  1736. while (!list_empty(&hbq_buf_list)) {
  1737. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1738. dbuf.list);
  1739. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1740. (hbqno << 16));
  1741. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1742. phba->hbqs[hbqno].buffer_count++;
  1743. posted++;
  1744. } else
  1745. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1746. }
  1747. spin_unlock_irqrestore(&phba->hbalock, flags);
  1748. return posted;
  1749. err:
  1750. spin_unlock_irqrestore(&phba->hbalock, flags);
  1751. while (!list_empty(&hbq_buf_list)) {
  1752. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1753. dbuf.list);
  1754. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1755. }
  1756. return 0;
  1757. }
  1758. /**
  1759. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1760. * @phba: Pointer to HBA context object.
  1761. * @qno: HBQ number.
  1762. *
  1763. * This function posts more buffers to the HBQ. This function
  1764. * is called with no lock held. The function returns the number of HBQ entries
  1765. * successfully allocated.
  1766. **/
  1767. int
  1768. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1769. {
  1770. if (phba->sli_rev == LPFC_SLI_REV4)
  1771. return 0;
  1772. else
  1773. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1774. lpfc_hbq_defs[qno]->add_count);
  1775. }
  1776. /**
  1777. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1778. * @phba: Pointer to HBA context object.
  1779. * @qno: HBQ queue number.
  1780. *
  1781. * This function is called from SLI initialization code path with
  1782. * no lock held to post initial HBQ buffers to firmware. The
  1783. * function returns the number of HBQ entries successfully allocated.
  1784. **/
  1785. static int
  1786. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1787. {
  1788. if (phba->sli_rev == LPFC_SLI_REV4)
  1789. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1790. lpfc_hbq_defs[qno]->entry_count);
  1791. else
  1792. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1793. lpfc_hbq_defs[qno]->init_count);
  1794. }
  1795. /**
  1796. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1797. * @phba: Pointer to HBA context object.
  1798. * @hbqno: HBQ number.
  1799. *
  1800. * This function removes the first hbq buffer on an hbq list and returns a
  1801. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1802. **/
  1803. static struct hbq_dmabuf *
  1804. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1805. {
  1806. struct lpfc_dmabuf *d_buf;
  1807. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1808. if (!d_buf)
  1809. return NULL;
  1810. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1811. }
  1812. /**
  1813. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1814. * @phba: Pointer to HBA context object.
  1815. * @tag: Tag of the hbq buffer.
  1816. *
  1817. * This function searches for the hbq buffer associated with the given tag in
  1818. * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
  1819. * otherwise it returns NULL.
  1820. **/
  1821. static struct hbq_dmabuf *
  1822. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1823. {
  1824. struct lpfc_dmabuf *d_buf;
  1825. struct hbq_dmabuf *hbq_buf;
  1826. uint32_t hbqno;
  1827. hbqno = tag >> 16;
  1828. if (hbqno >= LPFC_MAX_HBQS)
  1829. return NULL;
  1830. spin_lock_irq(&phba->hbalock);
  1831. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1832. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1833. if (hbq_buf->tag == tag) {
  1834. spin_unlock_irq(&phba->hbalock);
  1835. return hbq_buf;
  1836. }
  1837. }
  1838. spin_unlock_irq(&phba->hbalock);
  1839. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1840. "1803 Bad hbq tag. Data: x%x x%x\n",
  1841. tag, phba->hbqs[tag >> 16].buffer_count);
  1842. return NULL;
  1843. }
  1844. /**
  1845. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1846. * @phba: Pointer to HBA context object.
  1847. * @hbq_buffer: Pointer to HBQ buffer.
  1848. *
  1849. * This function is called with hbalock. This function gives back
  1850. * the hbq buffer to firmware. If the HBQ does not have space to
  1851. * post the buffer, it will free the buffer.
  1852. **/
  1853. void
  1854. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1855. {
  1856. uint32_t hbqno;
  1857. if (hbq_buffer) {
  1858. hbqno = hbq_buffer->tag >> 16;
  1859. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1860. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1861. }
  1862. }
  1863. /**
  1864. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1865. * @mbxCommand: mailbox command code.
  1866. *
  1867. * This function is called by the mailbox event handler function to verify
  1868. * that the completed mailbox command is a legitimate mailbox command. If the
  1869. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1870. * and the mailbox event handler will take the HBA offline.
  1871. **/
  1872. static int
  1873. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1874. {
  1875. uint8_t ret;
  1876. switch (mbxCommand) {
  1877. case MBX_LOAD_SM:
  1878. case MBX_READ_NV:
  1879. case MBX_WRITE_NV:
  1880. case MBX_WRITE_VPARMS:
  1881. case MBX_RUN_BIU_DIAG:
  1882. case MBX_INIT_LINK:
  1883. case MBX_DOWN_LINK:
  1884. case MBX_CONFIG_LINK:
  1885. case MBX_CONFIG_RING:
  1886. case MBX_RESET_RING:
  1887. case MBX_READ_CONFIG:
  1888. case MBX_READ_RCONFIG:
  1889. case MBX_READ_SPARM:
  1890. case MBX_READ_STATUS:
  1891. case MBX_READ_RPI:
  1892. case MBX_READ_XRI:
  1893. case MBX_READ_REV:
  1894. case MBX_READ_LNK_STAT:
  1895. case MBX_REG_LOGIN:
  1896. case MBX_UNREG_LOGIN:
  1897. case MBX_CLEAR_LA:
  1898. case MBX_DUMP_MEMORY:
  1899. case MBX_DUMP_CONTEXT:
  1900. case MBX_RUN_DIAGS:
  1901. case MBX_RESTART:
  1902. case MBX_UPDATE_CFG:
  1903. case MBX_DOWN_LOAD:
  1904. case MBX_DEL_LD_ENTRY:
  1905. case MBX_RUN_PROGRAM:
  1906. case MBX_SET_MASK:
  1907. case MBX_SET_VARIABLE:
  1908. case MBX_UNREG_D_ID:
  1909. case MBX_KILL_BOARD:
  1910. case MBX_CONFIG_FARP:
  1911. case MBX_BEACON:
  1912. case MBX_LOAD_AREA:
  1913. case MBX_RUN_BIU_DIAG64:
  1914. case MBX_CONFIG_PORT:
  1915. case MBX_READ_SPARM64:
  1916. case MBX_READ_RPI64:
  1917. case MBX_REG_LOGIN64:
  1918. case MBX_READ_TOPOLOGY:
  1919. case MBX_WRITE_WWN:
  1920. case MBX_SET_DEBUG:
  1921. case MBX_LOAD_EXP_ROM:
  1922. case MBX_ASYNCEVT_ENABLE:
  1923. case MBX_REG_VPI:
  1924. case MBX_UNREG_VPI:
  1925. case MBX_HEARTBEAT:
  1926. case MBX_PORT_CAPABILITIES:
  1927. case MBX_PORT_IOV_CONTROL:
  1928. case MBX_SLI4_CONFIG:
  1929. case MBX_SLI4_REQ_FTRS:
  1930. case MBX_REG_FCFI:
  1931. case MBX_UNREG_FCFI:
  1932. case MBX_REG_VFI:
  1933. case MBX_UNREG_VFI:
  1934. case MBX_INIT_VPI:
  1935. case MBX_INIT_VFI:
  1936. case MBX_RESUME_RPI:
  1937. case MBX_READ_EVENT_LOG_STATUS:
  1938. case MBX_READ_EVENT_LOG:
  1939. case MBX_SECURITY_MGMT:
  1940. case MBX_AUTH_PORT:
  1941. case MBX_ACCESS_VDATA:
  1942. ret = mbxCommand;
  1943. break;
  1944. default:
  1945. ret = MBX_SHUTDOWN;
  1946. break;
  1947. }
  1948. return ret;
  1949. }
  1950. /**
  1951. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1952. * @phba: Pointer to HBA context object.
  1953. * @pmboxq: Pointer to mailbox command.
  1954. *
  1955. * This is completion handler function for mailbox commands issued from
  1956. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1957. * mailbox event handler function with no lock held. This function
  1958. * will wake up thread waiting on the wait queue pointed by context1
  1959. * of the mailbox.
  1960. **/
  1961. void
  1962. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1963. {
  1964. wait_queue_head_t *pdone_q;
  1965. unsigned long drvr_flag;
  1966. /*
  1967. * If pdone_q is empty, the driver thread gave up waiting and
  1968. * continued running.
  1969. */
  1970. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1971. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1972. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1973. if (pdone_q)
  1974. wake_up_interruptible(pdone_q);
  1975. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1976. return;
  1977. }
  1978. /**
  1979. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1980. * @phba: Pointer to HBA context object.
  1981. * @pmb: Pointer to mailbox object.
  1982. *
  1983. * This function is the default mailbox completion handler. It
  1984. * frees the memory resources associated with the completed mailbox
  1985. * command. If the completed command is a REG_LOGIN mailbox command,
  1986. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1987. **/
  1988. void
  1989. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1990. {
  1991. struct lpfc_vport *vport = pmb->vport;
  1992. struct lpfc_dmabuf *mp;
  1993. struct lpfc_nodelist *ndlp;
  1994. struct Scsi_Host *shost;
  1995. uint16_t rpi, vpi;
  1996. int rc;
  1997. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1998. if (mp) {
  1999. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2000. kfree(mp);
  2001. }
  2002. /*
  2003. * If a REG_LOGIN succeeded after node is destroyed or node
  2004. * is in re-discovery driver need to cleanup the RPI.
  2005. */
  2006. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  2007. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  2008. !pmb->u.mb.mbxStatus) {
  2009. rpi = pmb->u.mb.un.varWords[0];
  2010. vpi = pmb->u.mb.un.varRegLogin.vpi;
  2011. lpfc_unreg_login(phba, vpi, rpi, pmb);
  2012. pmb->vport = vport;
  2013. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  2014. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2015. if (rc != MBX_NOT_FINISHED)
  2016. return;
  2017. }
  2018. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  2019. !(phba->pport->load_flag & FC_UNLOADING) &&
  2020. !pmb->u.mb.mbxStatus) {
  2021. shost = lpfc_shost_from_vport(vport);
  2022. spin_lock_irq(shost->host_lock);
  2023. vport->vpi_state |= LPFC_VPI_REGISTERED;
  2024. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  2025. spin_unlock_irq(shost->host_lock);
  2026. }
  2027. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  2028. ndlp = (struct lpfc_nodelist *)pmb->context2;
  2029. lpfc_nlp_put(ndlp);
  2030. pmb->context2 = NULL;
  2031. }
  2032. /* Check security permission status on INIT_LINK mailbox command */
  2033. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  2034. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  2035. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2036. "2860 SLI authentication is required "
  2037. "for INIT_LINK but has not done yet\n");
  2038. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  2039. lpfc_sli4_mbox_cmd_free(phba, pmb);
  2040. else
  2041. mempool_free(pmb, phba->mbox_mem_pool);
  2042. }
  2043. /**
  2044. * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
  2045. * @phba: Pointer to HBA context object.
  2046. * @pmb: Pointer to mailbox object.
  2047. *
  2048. * This function is the unreg rpi mailbox completion handler. It
  2049. * frees the memory resources associated with the completed mailbox
  2050. * command. An additional refrenece is put on the ndlp to prevent
  2051. * lpfc_nlp_release from freeing the rpi bit in the bitmask before
  2052. * the unreg mailbox command completes, this routine puts the
  2053. * reference back.
  2054. *
  2055. **/
  2056. void
  2057. lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2058. {
  2059. struct lpfc_vport *vport = pmb->vport;
  2060. struct lpfc_nodelist *ndlp;
  2061. ndlp = pmb->context1;
  2062. if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
  2063. if (phba->sli_rev == LPFC_SLI_REV4 &&
  2064. (bf_get(lpfc_sli_intf_if_type,
  2065. &phba->sli4_hba.sli_intf) ==
  2066. LPFC_SLI_INTF_IF_TYPE_2)) {
  2067. if (ndlp) {
  2068. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  2069. "0010 UNREG_LOGIN vpi:%x "
  2070. "rpi:%x DID:%x map:%x %p\n",
  2071. vport->vpi, ndlp->nlp_rpi,
  2072. ndlp->nlp_DID,
  2073. ndlp->nlp_usg_map, ndlp);
  2074. ndlp->nlp_flag &= ~NLP_LOGO_ACC;
  2075. lpfc_nlp_put(ndlp);
  2076. }
  2077. }
  2078. }
  2079. mempool_free(pmb, phba->mbox_mem_pool);
  2080. }
  2081. /**
  2082. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  2083. * @phba: Pointer to HBA context object.
  2084. *
  2085. * This function is called with no lock held. This function processes all
  2086. * the completed mailbox commands and gives it to upper layers. The interrupt
  2087. * service routine processes mailbox completion interrupt and adds completed
  2088. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  2089. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  2090. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  2091. * function returns the mailbox commands to the upper layer by calling the
  2092. * completion handler function of each mailbox.
  2093. **/
  2094. int
  2095. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  2096. {
  2097. MAILBOX_t *pmbox;
  2098. LPFC_MBOXQ_t *pmb;
  2099. int rc;
  2100. LIST_HEAD(cmplq);
  2101. phba->sli.slistat.mbox_event++;
  2102. /* Get all completed mailboxe buffers into the cmplq */
  2103. spin_lock_irq(&phba->hbalock);
  2104. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2105. spin_unlock_irq(&phba->hbalock);
  2106. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2107. do {
  2108. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2109. if (pmb == NULL)
  2110. break;
  2111. pmbox = &pmb->u.mb;
  2112. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2113. if (pmb->vport) {
  2114. lpfc_debugfs_disc_trc(pmb->vport,
  2115. LPFC_DISC_TRC_MBOX_VPORT,
  2116. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2117. (uint32_t)pmbox->mbxCommand,
  2118. pmbox->un.varWords[0],
  2119. pmbox->un.varWords[1]);
  2120. }
  2121. else {
  2122. lpfc_debugfs_disc_trc(phba->pport,
  2123. LPFC_DISC_TRC_MBOX,
  2124. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2125. (uint32_t)pmbox->mbxCommand,
  2126. pmbox->un.varWords[0],
  2127. pmbox->un.varWords[1]);
  2128. }
  2129. }
  2130. /*
  2131. * It is a fatal error if unknown mbox command completion.
  2132. */
  2133. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2134. MBX_SHUTDOWN) {
  2135. /* Unknown mailbox command compl */
  2136. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2137. "(%d):0323 Unknown Mailbox command "
  2138. "x%x (x%x/x%x) Cmpl\n",
  2139. pmb->vport ? pmb->vport->vpi : 0,
  2140. pmbox->mbxCommand,
  2141. lpfc_sli_config_mbox_subsys_get(phba,
  2142. pmb),
  2143. lpfc_sli_config_mbox_opcode_get(phba,
  2144. pmb));
  2145. phba->link_state = LPFC_HBA_ERROR;
  2146. phba->work_hs = HS_FFER3;
  2147. lpfc_handle_eratt(phba);
  2148. continue;
  2149. }
  2150. if (pmbox->mbxStatus) {
  2151. phba->sli.slistat.mbox_stat_err++;
  2152. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2153. /* Mbox cmd cmpl error - RETRYing */
  2154. lpfc_printf_log(phba, KERN_INFO,
  2155. LOG_MBOX | LOG_SLI,
  2156. "(%d):0305 Mbox cmd cmpl "
  2157. "error - RETRYing Data: x%x "
  2158. "(x%x/x%x) x%x x%x x%x\n",
  2159. pmb->vport ? pmb->vport->vpi : 0,
  2160. pmbox->mbxCommand,
  2161. lpfc_sli_config_mbox_subsys_get(phba,
  2162. pmb),
  2163. lpfc_sli_config_mbox_opcode_get(phba,
  2164. pmb),
  2165. pmbox->mbxStatus,
  2166. pmbox->un.varWords[0],
  2167. pmb->vport->port_state);
  2168. pmbox->mbxStatus = 0;
  2169. pmbox->mbxOwner = OWN_HOST;
  2170. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2171. if (rc != MBX_NOT_FINISHED)
  2172. continue;
  2173. }
  2174. }
  2175. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2176. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2177. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2178. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  2179. "x%x x%x x%x\n",
  2180. pmb->vport ? pmb->vport->vpi : 0,
  2181. pmbox->mbxCommand,
  2182. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2183. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2184. pmb->mbox_cmpl,
  2185. *((uint32_t *) pmbox),
  2186. pmbox->un.varWords[0],
  2187. pmbox->un.varWords[1],
  2188. pmbox->un.varWords[2],
  2189. pmbox->un.varWords[3],
  2190. pmbox->un.varWords[4],
  2191. pmbox->un.varWords[5],
  2192. pmbox->un.varWords[6],
  2193. pmbox->un.varWords[7],
  2194. pmbox->un.varWords[8],
  2195. pmbox->un.varWords[9],
  2196. pmbox->un.varWords[10]);
  2197. if (pmb->mbox_cmpl)
  2198. pmb->mbox_cmpl(phba,pmb);
  2199. } while (1);
  2200. return 0;
  2201. }
  2202. /**
  2203. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2204. * @phba: Pointer to HBA context object.
  2205. * @pring: Pointer to driver SLI ring object.
  2206. * @tag: buffer tag.
  2207. *
  2208. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2209. * is set in the tag the buffer is posted for a particular exchange,
  2210. * the function will return the buffer without replacing the buffer.
  2211. * If the buffer is for unsolicited ELS or CT traffic, this function
  2212. * returns the buffer and also posts another buffer to the firmware.
  2213. **/
  2214. static struct lpfc_dmabuf *
  2215. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2216. struct lpfc_sli_ring *pring,
  2217. uint32_t tag)
  2218. {
  2219. struct hbq_dmabuf *hbq_entry;
  2220. if (tag & QUE_BUFTAG_BIT)
  2221. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2222. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2223. if (!hbq_entry)
  2224. return NULL;
  2225. return &hbq_entry->dbuf;
  2226. }
  2227. /**
  2228. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2229. * @phba: Pointer to HBA context object.
  2230. * @pring: Pointer to driver SLI ring object.
  2231. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2232. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2233. * @fch_type: the type for the first frame of the sequence.
  2234. *
  2235. * This function is called with no lock held. This function uses the r_ctl and
  2236. * type of the received sequence to find the correct callback function to call
  2237. * to process the sequence.
  2238. **/
  2239. static int
  2240. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2241. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2242. uint32_t fch_type)
  2243. {
  2244. int i;
  2245. /* unSolicited Responses */
  2246. if (pring->prt[0].profile) {
  2247. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2248. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2249. saveq);
  2250. return 1;
  2251. }
  2252. /* We must search, based on rctl / type
  2253. for the right routine */
  2254. for (i = 0; i < pring->num_mask; i++) {
  2255. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2256. (pring->prt[i].type == fch_type)) {
  2257. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2258. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2259. (phba, pring, saveq);
  2260. return 1;
  2261. }
  2262. }
  2263. return 0;
  2264. }
  2265. /**
  2266. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2267. * @phba: Pointer to HBA context object.
  2268. * @pring: Pointer to driver SLI ring object.
  2269. * @saveq: Pointer to the unsolicited iocb.
  2270. *
  2271. * This function is called with no lock held by the ring event handler
  2272. * when there is an unsolicited iocb posted to the response ring by the
  2273. * firmware. This function gets the buffer associated with the iocbs
  2274. * and calls the event handler for the ring. This function handles both
  2275. * qring buffers and hbq buffers.
  2276. * When the function returns 1 the caller can free the iocb object otherwise
  2277. * upper layer functions will free the iocb objects.
  2278. **/
  2279. static int
  2280. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2281. struct lpfc_iocbq *saveq)
  2282. {
  2283. IOCB_t * irsp;
  2284. WORD5 * w5p;
  2285. uint32_t Rctl, Type;
  2286. struct lpfc_iocbq *iocbq;
  2287. struct lpfc_dmabuf *dmzbuf;
  2288. irsp = &(saveq->iocb);
  2289. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2290. if (pring->lpfc_sli_rcv_async_status)
  2291. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2292. else
  2293. lpfc_printf_log(phba,
  2294. KERN_WARNING,
  2295. LOG_SLI,
  2296. "0316 Ring %d handler: unexpected "
  2297. "ASYNC_STATUS iocb received evt_code "
  2298. "0x%x\n",
  2299. pring->ringno,
  2300. irsp->un.asyncstat.evt_code);
  2301. return 1;
  2302. }
  2303. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2304. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2305. if (irsp->ulpBdeCount > 0) {
  2306. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2307. irsp->un.ulpWord[3]);
  2308. lpfc_in_buf_free(phba, dmzbuf);
  2309. }
  2310. if (irsp->ulpBdeCount > 1) {
  2311. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2312. irsp->unsli3.sli3Words[3]);
  2313. lpfc_in_buf_free(phba, dmzbuf);
  2314. }
  2315. if (irsp->ulpBdeCount > 2) {
  2316. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2317. irsp->unsli3.sli3Words[7]);
  2318. lpfc_in_buf_free(phba, dmzbuf);
  2319. }
  2320. return 1;
  2321. }
  2322. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2323. if (irsp->ulpBdeCount != 0) {
  2324. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2325. irsp->un.ulpWord[3]);
  2326. if (!saveq->context2)
  2327. lpfc_printf_log(phba,
  2328. KERN_ERR,
  2329. LOG_SLI,
  2330. "0341 Ring %d Cannot find buffer for "
  2331. "an unsolicited iocb. tag 0x%x\n",
  2332. pring->ringno,
  2333. irsp->un.ulpWord[3]);
  2334. }
  2335. if (irsp->ulpBdeCount == 2) {
  2336. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2337. irsp->unsli3.sli3Words[7]);
  2338. if (!saveq->context3)
  2339. lpfc_printf_log(phba,
  2340. KERN_ERR,
  2341. LOG_SLI,
  2342. "0342 Ring %d Cannot find buffer for an"
  2343. " unsolicited iocb. tag 0x%x\n",
  2344. pring->ringno,
  2345. irsp->unsli3.sli3Words[7]);
  2346. }
  2347. list_for_each_entry(iocbq, &saveq->list, list) {
  2348. irsp = &(iocbq->iocb);
  2349. if (irsp->ulpBdeCount != 0) {
  2350. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2351. irsp->un.ulpWord[3]);
  2352. if (!iocbq->context2)
  2353. lpfc_printf_log(phba,
  2354. KERN_ERR,
  2355. LOG_SLI,
  2356. "0343 Ring %d Cannot find "
  2357. "buffer for an unsolicited iocb"
  2358. ". tag 0x%x\n", pring->ringno,
  2359. irsp->un.ulpWord[3]);
  2360. }
  2361. if (irsp->ulpBdeCount == 2) {
  2362. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2363. irsp->unsli3.sli3Words[7]);
  2364. if (!iocbq->context3)
  2365. lpfc_printf_log(phba,
  2366. KERN_ERR,
  2367. LOG_SLI,
  2368. "0344 Ring %d Cannot find "
  2369. "buffer for an unsolicited "
  2370. "iocb. tag 0x%x\n",
  2371. pring->ringno,
  2372. irsp->unsli3.sli3Words[7]);
  2373. }
  2374. }
  2375. }
  2376. if (irsp->ulpBdeCount != 0 &&
  2377. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2378. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2379. int found = 0;
  2380. /* search continue save q for same XRI */
  2381. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2382. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2383. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2384. list_add_tail(&saveq->list, &iocbq->list);
  2385. found = 1;
  2386. break;
  2387. }
  2388. }
  2389. if (!found)
  2390. list_add_tail(&saveq->clist,
  2391. &pring->iocb_continue_saveq);
  2392. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2393. list_del_init(&iocbq->clist);
  2394. saveq = iocbq;
  2395. irsp = &(saveq->iocb);
  2396. } else
  2397. return 0;
  2398. }
  2399. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2400. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2401. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2402. Rctl = FC_RCTL_ELS_REQ;
  2403. Type = FC_TYPE_ELS;
  2404. } else {
  2405. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2406. Rctl = w5p->hcsw.Rctl;
  2407. Type = w5p->hcsw.Type;
  2408. /* Firmware Workaround */
  2409. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2410. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2411. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2412. Rctl = FC_RCTL_ELS_REQ;
  2413. Type = FC_TYPE_ELS;
  2414. w5p->hcsw.Rctl = Rctl;
  2415. w5p->hcsw.Type = Type;
  2416. }
  2417. }
  2418. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2419. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2420. "0313 Ring %d handler: unexpected Rctl x%x "
  2421. "Type x%x received\n",
  2422. pring->ringno, Rctl, Type);
  2423. return 1;
  2424. }
  2425. /**
  2426. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2427. * @phba: Pointer to HBA context object.
  2428. * @pring: Pointer to driver SLI ring object.
  2429. * @prspiocb: Pointer to response iocb object.
  2430. *
  2431. * This function looks up the iocb_lookup table to get the command iocb
  2432. * corresponding to the given response iocb using the iotag of the
  2433. * response iocb. This function is called with the hbalock held.
  2434. * This function returns the command iocb object if it finds the command
  2435. * iocb else returns NULL.
  2436. **/
  2437. static struct lpfc_iocbq *
  2438. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2439. struct lpfc_sli_ring *pring,
  2440. struct lpfc_iocbq *prspiocb)
  2441. {
  2442. struct lpfc_iocbq *cmd_iocb = NULL;
  2443. uint16_t iotag;
  2444. lockdep_assert_held(&phba->hbalock);
  2445. iotag = prspiocb->iocb.ulpIoTag;
  2446. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2447. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2448. list_del_init(&cmd_iocb->list);
  2449. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2450. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2451. }
  2452. return cmd_iocb;
  2453. }
  2454. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2455. "0317 iotag x%x is out off "
  2456. "range: max iotag x%x wd0 x%x\n",
  2457. iotag, phba->sli.last_iotag,
  2458. *(((uint32_t *) &prspiocb->iocb) + 7));
  2459. return NULL;
  2460. }
  2461. /**
  2462. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2463. * @phba: Pointer to HBA context object.
  2464. * @pring: Pointer to driver SLI ring object.
  2465. * @iotag: IOCB tag.
  2466. *
  2467. * This function looks up the iocb_lookup table to get the command iocb
  2468. * corresponding to the given iotag. This function is called with the
  2469. * hbalock held.
  2470. * This function returns the command iocb object if it finds the command
  2471. * iocb else returns NULL.
  2472. **/
  2473. static struct lpfc_iocbq *
  2474. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2475. struct lpfc_sli_ring *pring, uint16_t iotag)
  2476. {
  2477. struct lpfc_iocbq *cmd_iocb;
  2478. lockdep_assert_held(&phba->hbalock);
  2479. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2480. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2481. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2482. /* remove from txcmpl queue list */
  2483. list_del_init(&cmd_iocb->list);
  2484. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2485. return cmd_iocb;
  2486. }
  2487. }
  2488. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2489. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2490. iotag, phba->sli.last_iotag);
  2491. return NULL;
  2492. }
  2493. /**
  2494. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2495. * @phba: Pointer to HBA context object.
  2496. * @pring: Pointer to driver SLI ring object.
  2497. * @saveq: Pointer to the response iocb to be processed.
  2498. *
  2499. * This function is called by the ring event handler for non-fcp
  2500. * rings when there is a new response iocb in the response ring.
  2501. * The caller is not required to hold any locks. This function
  2502. * gets the command iocb associated with the response iocb and
  2503. * calls the completion handler for the command iocb. If there
  2504. * is no completion handler, the function will free the resources
  2505. * associated with command iocb. If the response iocb is for
  2506. * an already aborted command iocb, the status of the completion
  2507. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2508. * This function always returns 1.
  2509. **/
  2510. static int
  2511. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2512. struct lpfc_iocbq *saveq)
  2513. {
  2514. struct lpfc_iocbq *cmdiocbp;
  2515. int rc = 1;
  2516. unsigned long iflag;
  2517. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2518. spin_lock_irqsave(&phba->hbalock, iflag);
  2519. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2520. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2521. if (cmdiocbp) {
  2522. if (cmdiocbp->iocb_cmpl) {
  2523. /*
  2524. * If an ELS command failed send an event to mgmt
  2525. * application.
  2526. */
  2527. if (saveq->iocb.ulpStatus &&
  2528. (pring->ringno == LPFC_ELS_RING) &&
  2529. (cmdiocbp->iocb.ulpCommand ==
  2530. CMD_ELS_REQUEST64_CR))
  2531. lpfc_send_els_failure_event(phba,
  2532. cmdiocbp, saveq);
  2533. /*
  2534. * Post all ELS completions to the worker thread.
  2535. * All other are passed to the completion callback.
  2536. */
  2537. if (pring->ringno == LPFC_ELS_RING) {
  2538. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2539. (cmdiocbp->iocb_flag &
  2540. LPFC_DRIVER_ABORTED)) {
  2541. spin_lock_irqsave(&phba->hbalock,
  2542. iflag);
  2543. cmdiocbp->iocb_flag &=
  2544. ~LPFC_DRIVER_ABORTED;
  2545. spin_unlock_irqrestore(&phba->hbalock,
  2546. iflag);
  2547. saveq->iocb.ulpStatus =
  2548. IOSTAT_LOCAL_REJECT;
  2549. saveq->iocb.un.ulpWord[4] =
  2550. IOERR_SLI_ABORTED;
  2551. /* Firmware could still be in progress
  2552. * of DMAing payload, so don't free data
  2553. * buffer till after a hbeat.
  2554. */
  2555. spin_lock_irqsave(&phba->hbalock,
  2556. iflag);
  2557. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2558. spin_unlock_irqrestore(&phba->hbalock,
  2559. iflag);
  2560. }
  2561. if (phba->sli_rev == LPFC_SLI_REV4) {
  2562. if (saveq->iocb_flag &
  2563. LPFC_EXCHANGE_BUSY) {
  2564. /* Set cmdiocb flag for the
  2565. * exchange busy so sgl (xri)
  2566. * will not be released until
  2567. * the abort xri is received
  2568. * from hba.
  2569. */
  2570. spin_lock_irqsave(
  2571. &phba->hbalock, iflag);
  2572. cmdiocbp->iocb_flag |=
  2573. LPFC_EXCHANGE_BUSY;
  2574. spin_unlock_irqrestore(
  2575. &phba->hbalock, iflag);
  2576. }
  2577. if (cmdiocbp->iocb_flag &
  2578. LPFC_DRIVER_ABORTED) {
  2579. /*
  2580. * Clear LPFC_DRIVER_ABORTED
  2581. * bit in case it was driver
  2582. * initiated abort.
  2583. */
  2584. spin_lock_irqsave(
  2585. &phba->hbalock, iflag);
  2586. cmdiocbp->iocb_flag &=
  2587. ~LPFC_DRIVER_ABORTED;
  2588. spin_unlock_irqrestore(
  2589. &phba->hbalock, iflag);
  2590. cmdiocbp->iocb.ulpStatus =
  2591. IOSTAT_LOCAL_REJECT;
  2592. cmdiocbp->iocb.un.ulpWord[4] =
  2593. IOERR_ABORT_REQUESTED;
  2594. /*
  2595. * For SLI4, irsiocb contains
  2596. * NO_XRI in sli_xritag, it
  2597. * shall not affect releasing
  2598. * sgl (xri) process.
  2599. */
  2600. saveq->iocb.ulpStatus =
  2601. IOSTAT_LOCAL_REJECT;
  2602. saveq->iocb.un.ulpWord[4] =
  2603. IOERR_SLI_ABORTED;
  2604. spin_lock_irqsave(
  2605. &phba->hbalock, iflag);
  2606. saveq->iocb_flag |=
  2607. LPFC_DELAY_MEM_FREE;
  2608. spin_unlock_irqrestore(
  2609. &phba->hbalock, iflag);
  2610. }
  2611. }
  2612. }
  2613. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2614. } else
  2615. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2616. } else {
  2617. /*
  2618. * Unknown initiating command based on the response iotag.
  2619. * This could be the case on the ELS ring because of
  2620. * lpfc_els_abort().
  2621. */
  2622. if (pring->ringno != LPFC_ELS_RING) {
  2623. /*
  2624. * Ring <ringno> handler: unexpected completion IoTag
  2625. * <IoTag>
  2626. */
  2627. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2628. "0322 Ring %d handler: "
  2629. "unexpected completion IoTag x%x "
  2630. "Data: x%x x%x x%x x%x\n",
  2631. pring->ringno,
  2632. saveq->iocb.ulpIoTag,
  2633. saveq->iocb.ulpStatus,
  2634. saveq->iocb.un.ulpWord[4],
  2635. saveq->iocb.ulpCommand,
  2636. saveq->iocb.ulpContext);
  2637. }
  2638. }
  2639. return rc;
  2640. }
  2641. /**
  2642. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2643. * @phba: Pointer to HBA context object.
  2644. * @pring: Pointer to driver SLI ring object.
  2645. *
  2646. * This function is called from the iocb ring event handlers when
  2647. * put pointer is ahead of the get pointer for a ring. This function signal
  2648. * an error attention condition to the worker thread and the worker
  2649. * thread will transition the HBA to offline state.
  2650. **/
  2651. static void
  2652. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2653. {
  2654. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2655. /*
  2656. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2657. * rsp ring <portRspMax>
  2658. */
  2659. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2660. "0312 Ring %d handler: portRspPut %d "
  2661. "is bigger than rsp ring %d\n",
  2662. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2663. pring->sli.sli3.numRiocb);
  2664. phba->link_state = LPFC_HBA_ERROR;
  2665. /*
  2666. * All error attention handlers are posted to
  2667. * worker thread
  2668. */
  2669. phba->work_ha |= HA_ERATT;
  2670. phba->work_hs = HS_FFER3;
  2671. lpfc_worker_wake_up(phba);
  2672. return;
  2673. }
  2674. /**
  2675. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2676. * @ptr: Pointer to address of HBA context object.
  2677. *
  2678. * This function is invoked by the Error Attention polling timer when the
  2679. * timer times out. It will check the SLI Error Attention register for
  2680. * possible attention events. If so, it will post an Error Attention event
  2681. * and wake up worker thread to process it. Otherwise, it will set up the
  2682. * Error Attention polling timer for the next poll.
  2683. **/
  2684. void lpfc_poll_eratt(unsigned long ptr)
  2685. {
  2686. struct lpfc_hba *phba;
  2687. uint32_t eratt = 0;
  2688. uint64_t sli_intr, cnt;
  2689. phba = (struct lpfc_hba *)ptr;
  2690. /* Here we will also keep track of interrupts per sec of the hba */
  2691. sli_intr = phba->sli.slistat.sli_intr;
  2692. if (phba->sli.slistat.sli_prev_intr > sli_intr)
  2693. cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
  2694. sli_intr);
  2695. else
  2696. cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
  2697. /* 64-bit integer division not supported on 32-bit x86 - use do_div */
  2698. do_div(cnt, phba->eratt_poll_interval);
  2699. phba->sli.slistat.sli_ips = cnt;
  2700. phba->sli.slistat.sli_prev_intr = sli_intr;
  2701. /* Check chip HA register for error event */
  2702. eratt = lpfc_sli_check_eratt(phba);
  2703. if (eratt)
  2704. /* Tell the worker thread there is work to do */
  2705. lpfc_worker_wake_up(phba);
  2706. else
  2707. /* Restart the timer for next eratt poll */
  2708. mod_timer(&phba->eratt_poll,
  2709. jiffies +
  2710. msecs_to_jiffies(1000 * phba->eratt_poll_interval));
  2711. return;
  2712. }
  2713. /**
  2714. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2715. * @phba: Pointer to HBA context object.
  2716. * @pring: Pointer to driver SLI ring object.
  2717. * @mask: Host attention register mask for this ring.
  2718. *
  2719. * This function is called from the interrupt context when there is a ring
  2720. * event for the fcp ring. The caller does not hold any lock.
  2721. * The function processes each response iocb in the response ring until it
  2722. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2723. * LE bit set. The function will call the completion handler of the command iocb
  2724. * if the response iocb indicates a completion for a command iocb or it is
  2725. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2726. * function if this is an unsolicited iocb.
  2727. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2728. * to check it explicitly.
  2729. */
  2730. int
  2731. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2732. struct lpfc_sli_ring *pring, uint32_t mask)
  2733. {
  2734. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2735. IOCB_t *irsp = NULL;
  2736. IOCB_t *entry = NULL;
  2737. struct lpfc_iocbq *cmdiocbq = NULL;
  2738. struct lpfc_iocbq rspiocbq;
  2739. uint32_t status;
  2740. uint32_t portRspPut, portRspMax;
  2741. int rc = 1;
  2742. lpfc_iocb_type type;
  2743. unsigned long iflag;
  2744. uint32_t rsp_cmpl = 0;
  2745. spin_lock_irqsave(&phba->hbalock, iflag);
  2746. pring->stats.iocb_event++;
  2747. /*
  2748. * The next available response entry should never exceed the maximum
  2749. * entries. If it does, treat it as an adapter hardware error.
  2750. */
  2751. portRspMax = pring->sli.sli3.numRiocb;
  2752. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2753. if (unlikely(portRspPut >= portRspMax)) {
  2754. lpfc_sli_rsp_pointers_error(phba, pring);
  2755. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2756. return 1;
  2757. }
  2758. if (phba->fcp_ring_in_use) {
  2759. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2760. return 1;
  2761. } else
  2762. phba->fcp_ring_in_use = 1;
  2763. rmb();
  2764. while (pring->sli.sli3.rspidx != portRspPut) {
  2765. /*
  2766. * Fetch an entry off the ring and copy it into a local data
  2767. * structure. The copy involves a byte-swap since the
  2768. * network byte order and pci byte orders are different.
  2769. */
  2770. entry = lpfc_resp_iocb(phba, pring);
  2771. phba->last_completion_time = jiffies;
  2772. if (++pring->sli.sli3.rspidx >= portRspMax)
  2773. pring->sli.sli3.rspidx = 0;
  2774. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2775. (uint32_t *) &rspiocbq.iocb,
  2776. phba->iocb_rsp_size);
  2777. INIT_LIST_HEAD(&(rspiocbq.list));
  2778. irsp = &rspiocbq.iocb;
  2779. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2780. pring->stats.iocb_rsp++;
  2781. rsp_cmpl++;
  2782. if (unlikely(irsp->ulpStatus)) {
  2783. /*
  2784. * If resource errors reported from HBA, reduce
  2785. * queuedepths of the SCSI device.
  2786. */
  2787. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2788. ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
  2789. IOERR_NO_RESOURCES)) {
  2790. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2791. phba->lpfc_rampdown_queue_depth(phba);
  2792. spin_lock_irqsave(&phba->hbalock, iflag);
  2793. }
  2794. /* Rsp ring <ringno> error: IOCB */
  2795. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2796. "0336 Rsp Ring %d error: IOCB Data: "
  2797. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2798. pring->ringno,
  2799. irsp->un.ulpWord[0],
  2800. irsp->un.ulpWord[1],
  2801. irsp->un.ulpWord[2],
  2802. irsp->un.ulpWord[3],
  2803. irsp->un.ulpWord[4],
  2804. irsp->un.ulpWord[5],
  2805. *(uint32_t *)&irsp->un1,
  2806. *((uint32_t *)&irsp->un1 + 1));
  2807. }
  2808. switch (type) {
  2809. case LPFC_ABORT_IOCB:
  2810. case LPFC_SOL_IOCB:
  2811. /*
  2812. * Idle exchange closed via ABTS from port. No iocb
  2813. * resources need to be recovered.
  2814. */
  2815. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2816. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2817. "0333 IOCB cmd 0x%x"
  2818. " processed. Skipping"
  2819. " completion\n",
  2820. irsp->ulpCommand);
  2821. break;
  2822. }
  2823. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2824. &rspiocbq);
  2825. if (unlikely(!cmdiocbq))
  2826. break;
  2827. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2828. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2829. if (cmdiocbq->iocb_cmpl) {
  2830. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2831. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2832. &rspiocbq);
  2833. spin_lock_irqsave(&phba->hbalock, iflag);
  2834. }
  2835. break;
  2836. case LPFC_UNSOL_IOCB:
  2837. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2838. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2839. spin_lock_irqsave(&phba->hbalock, iflag);
  2840. break;
  2841. default:
  2842. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2843. char adaptermsg[LPFC_MAX_ADPTMSG];
  2844. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2845. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2846. MAX_MSG_DATA);
  2847. dev_warn(&((phba->pcidev)->dev),
  2848. "lpfc%d: %s\n",
  2849. phba->brd_no, adaptermsg);
  2850. } else {
  2851. /* Unknown IOCB command */
  2852. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2853. "0334 Unknown IOCB command "
  2854. "Data: x%x, x%x x%x x%x x%x\n",
  2855. type, irsp->ulpCommand,
  2856. irsp->ulpStatus,
  2857. irsp->ulpIoTag,
  2858. irsp->ulpContext);
  2859. }
  2860. break;
  2861. }
  2862. /*
  2863. * The response IOCB has been processed. Update the ring
  2864. * pointer in SLIM. If the port response put pointer has not
  2865. * been updated, sync the pgp->rspPutInx and fetch the new port
  2866. * response put pointer.
  2867. */
  2868. writel(pring->sli.sli3.rspidx,
  2869. &phba->host_gp[pring->ringno].rspGetInx);
  2870. if (pring->sli.sli3.rspidx == portRspPut)
  2871. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2872. }
  2873. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2874. pring->stats.iocb_rsp_full++;
  2875. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2876. writel(status, phba->CAregaddr);
  2877. readl(phba->CAregaddr);
  2878. }
  2879. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2880. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2881. pring->stats.iocb_cmd_empty++;
  2882. /* Force update of the local copy of cmdGetInx */
  2883. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2884. lpfc_sli_resume_iocb(phba, pring);
  2885. if ((pring->lpfc_sli_cmd_available))
  2886. (pring->lpfc_sli_cmd_available) (phba, pring);
  2887. }
  2888. phba->fcp_ring_in_use = 0;
  2889. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2890. return rc;
  2891. }
  2892. /**
  2893. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2894. * @phba: Pointer to HBA context object.
  2895. * @pring: Pointer to driver SLI ring object.
  2896. * @rspiocbp: Pointer to driver response IOCB object.
  2897. *
  2898. * This function is called from the worker thread when there is a slow-path
  2899. * response IOCB to process. This function chains all the response iocbs until
  2900. * seeing the iocb with the LE bit set. The function will call
  2901. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2902. * completion of a command iocb. The function will call the
  2903. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2904. * The function frees the resources or calls the completion handler if this
  2905. * iocb is an abort completion. The function returns NULL when the response
  2906. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2907. * this function shall chain the iocb on to the iocb_continueq and return the
  2908. * response iocb passed in.
  2909. **/
  2910. static struct lpfc_iocbq *
  2911. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2912. struct lpfc_iocbq *rspiocbp)
  2913. {
  2914. struct lpfc_iocbq *saveq;
  2915. struct lpfc_iocbq *cmdiocbp;
  2916. struct lpfc_iocbq *next_iocb;
  2917. IOCB_t *irsp = NULL;
  2918. uint32_t free_saveq;
  2919. uint8_t iocb_cmd_type;
  2920. lpfc_iocb_type type;
  2921. unsigned long iflag;
  2922. int rc;
  2923. spin_lock_irqsave(&phba->hbalock, iflag);
  2924. /* First add the response iocb to the countinueq list */
  2925. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2926. pring->iocb_continueq_cnt++;
  2927. /* Now, determine whether the list is completed for processing */
  2928. irsp = &rspiocbp->iocb;
  2929. if (irsp->ulpLe) {
  2930. /*
  2931. * By default, the driver expects to free all resources
  2932. * associated with this iocb completion.
  2933. */
  2934. free_saveq = 1;
  2935. saveq = list_get_first(&pring->iocb_continueq,
  2936. struct lpfc_iocbq, list);
  2937. irsp = &(saveq->iocb);
  2938. list_del_init(&pring->iocb_continueq);
  2939. pring->iocb_continueq_cnt = 0;
  2940. pring->stats.iocb_rsp++;
  2941. /*
  2942. * If resource errors reported from HBA, reduce
  2943. * queuedepths of the SCSI device.
  2944. */
  2945. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2946. ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
  2947. IOERR_NO_RESOURCES)) {
  2948. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2949. phba->lpfc_rampdown_queue_depth(phba);
  2950. spin_lock_irqsave(&phba->hbalock, iflag);
  2951. }
  2952. if (irsp->ulpStatus) {
  2953. /* Rsp ring <ringno> error: IOCB */
  2954. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2955. "0328 Rsp Ring %d error: "
  2956. "IOCB Data: "
  2957. "x%x x%x x%x x%x "
  2958. "x%x x%x x%x x%x "
  2959. "x%x x%x x%x x%x "
  2960. "x%x x%x x%x x%x\n",
  2961. pring->ringno,
  2962. irsp->un.ulpWord[0],
  2963. irsp->un.ulpWord[1],
  2964. irsp->un.ulpWord[2],
  2965. irsp->un.ulpWord[3],
  2966. irsp->un.ulpWord[4],
  2967. irsp->un.ulpWord[5],
  2968. *(((uint32_t *) irsp) + 6),
  2969. *(((uint32_t *) irsp) + 7),
  2970. *(((uint32_t *) irsp) + 8),
  2971. *(((uint32_t *) irsp) + 9),
  2972. *(((uint32_t *) irsp) + 10),
  2973. *(((uint32_t *) irsp) + 11),
  2974. *(((uint32_t *) irsp) + 12),
  2975. *(((uint32_t *) irsp) + 13),
  2976. *(((uint32_t *) irsp) + 14),
  2977. *(((uint32_t *) irsp) + 15));
  2978. }
  2979. /*
  2980. * Fetch the IOCB command type and call the correct completion
  2981. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2982. * get freed back to the lpfc_iocb_list by the discovery
  2983. * kernel thread.
  2984. */
  2985. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2986. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2987. switch (type) {
  2988. case LPFC_SOL_IOCB:
  2989. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2990. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2991. spin_lock_irqsave(&phba->hbalock, iflag);
  2992. break;
  2993. case LPFC_UNSOL_IOCB:
  2994. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2995. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2996. spin_lock_irqsave(&phba->hbalock, iflag);
  2997. if (!rc)
  2998. free_saveq = 0;
  2999. break;
  3000. case LPFC_ABORT_IOCB:
  3001. cmdiocbp = NULL;
  3002. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  3003. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  3004. saveq);
  3005. if (cmdiocbp) {
  3006. /* Call the specified completion routine */
  3007. if (cmdiocbp->iocb_cmpl) {
  3008. spin_unlock_irqrestore(&phba->hbalock,
  3009. iflag);
  3010. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  3011. saveq);
  3012. spin_lock_irqsave(&phba->hbalock,
  3013. iflag);
  3014. } else
  3015. __lpfc_sli_release_iocbq(phba,
  3016. cmdiocbp);
  3017. }
  3018. break;
  3019. case LPFC_UNKNOWN_IOCB:
  3020. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  3021. char adaptermsg[LPFC_MAX_ADPTMSG];
  3022. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  3023. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  3024. MAX_MSG_DATA);
  3025. dev_warn(&((phba->pcidev)->dev),
  3026. "lpfc%d: %s\n",
  3027. phba->brd_no, adaptermsg);
  3028. } else {
  3029. /* Unknown IOCB command */
  3030. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3031. "0335 Unknown IOCB "
  3032. "command Data: x%x "
  3033. "x%x x%x x%x\n",
  3034. irsp->ulpCommand,
  3035. irsp->ulpStatus,
  3036. irsp->ulpIoTag,
  3037. irsp->ulpContext);
  3038. }
  3039. break;
  3040. }
  3041. if (free_saveq) {
  3042. list_for_each_entry_safe(rspiocbp, next_iocb,
  3043. &saveq->list, list) {
  3044. list_del_init(&rspiocbp->list);
  3045. __lpfc_sli_release_iocbq(phba, rspiocbp);
  3046. }
  3047. __lpfc_sli_release_iocbq(phba, saveq);
  3048. }
  3049. rspiocbp = NULL;
  3050. }
  3051. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3052. return rspiocbp;
  3053. }
  3054. /**
  3055. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  3056. * @phba: Pointer to HBA context object.
  3057. * @pring: Pointer to driver SLI ring object.
  3058. * @mask: Host attention register mask for this ring.
  3059. *
  3060. * This routine wraps the actual slow_ring event process routine from the
  3061. * API jump table function pointer from the lpfc_hba struct.
  3062. **/
  3063. void
  3064. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  3065. struct lpfc_sli_ring *pring, uint32_t mask)
  3066. {
  3067. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  3068. }
  3069. /**
  3070. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  3071. * @phba: Pointer to HBA context object.
  3072. * @pring: Pointer to driver SLI ring object.
  3073. * @mask: Host attention register mask for this ring.
  3074. *
  3075. * This function is called from the worker thread when there is a ring event
  3076. * for non-fcp rings. The caller does not hold any lock. The function will
  3077. * remove each response iocb in the response ring and calls the handle
  3078. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3079. **/
  3080. static void
  3081. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  3082. struct lpfc_sli_ring *pring, uint32_t mask)
  3083. {
  3084. struct lpfc_pgp *pgp;
  3085. IOCB_t *entry;
  3086. IOCB_t *irsp = NULL;
  3087. struct lpfc_iocbq *rspiocbp = NULL;
  3088. uint32_t portRspPut, portRspMax;
  3089. unsigned long iflag;
  3090. uint32_t status;
  3091. pgp = &phba->port_gp[pring->ringno];
  3092. spin_lock_irqsave(&phba->hbalock, iflag);
  3093. pring->stats.iocb_event++;
  3094. /*
  3095. * The next available response entry should never exceed the maximum
  3096. * entries. If it does, treat it as an adapter hardware error.
  3097. */
  3098. portRspMax = pring->sli.sli3.numRiocb;
  3099. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3100. if (portRspPut >= portRspMax) {
  3101. /*
  3102. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  3103. * rsp ring <portRspMax>
  3104. */
  3105. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3106. "0303 Ring %d handler: portRspPut %d "
  3107. "is bigger than rsp ring %d\n",
  3108. pring->ringno, portRspPut, portRspMax);
  3109. phba->link_state = LPFC_HBA_ERROR;
  3110. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3111. phba->work_hs = HS_FFER3;
  3112. lpfc_handle_eratt(phba);
  3113. return;
  3114. }
  3115. rmb();
  3116. while (pring->sli.sli3.rspidx != portRspPut) {
  3117. /*
  3118. * Build a completion list and call the appropriate handler.
  3119. * The process is to get the next available response iocb, get
  3120. * a free iocb from the list, copy the response data into the
  3121. * free iocb, insert to the continuation list, and update the
  3122. * next response index to slim. This process makes response
  3123. * iocb's in the ring available to DMA as fast as possible but
  3124. * pays a penalty for a copy operation. Since the iocb is
  3125. * only 32 bytes, this penalty is considered small relative to
  3126. * the PCI reads for register values and a slim write. When
  3127. * the ulpLe field is set, the entire Command has been
  3128. * received.
  3129. */
  3130. entry = lpfc_resp_iocb(phba, pring);
  3131. phba->last_completion_time = jiffies;
  3132. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3133. if (rspiocbp == NULL) {
  3134. printk(KERN_ERR "%s: out of buffers! Failing "
  3135. "completion.\n", __func__);
  3136. break;
  3137. }
  3138. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3139. phba->iocb_rsp_size);
  3140. irsp = &rspiocbp->iocb;
  3141. if (++pring->sli.sli3.rspidx >= portRspMax)
  3142. pring->sli.sli3.rspidx = 0;
  3143. if (pring->ringno == LPFC_ELS_RING) {
  3144. lpfc_debugfs_slow_ring_trc(phba,
  3145. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3146. *(((uint32_t *) irsp) + 4),
  3147. *(((uint32_t *) irsp) + 6),
  3148. *(((uint32_t *) irsp) + 7));
  3149. }
  3150. writel(pring->sli.sli3.rspidx,
  3151. &phba->host_gp[pring->ringno].rspGetInx);
  3152. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3153. /* Handle the response IOCB */
  3154. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3155. spin_lock_irqsave(&phba->hbalock, iflag);
  3156. /*
  3157. * If the port response put pointer has not been updated, sync
  3158. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3159. * response put pointer.
  3160. */
  3161. if (pring->sli.sli3.rspidx == portRspPut) {
  3162. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3163. }
  3164. } /* while (pring->sli.sli3.rspidx != portRspPut) */
  3165. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3166. /* At least one response entry has been freed */
  3167. pring->stats.iocb_rsp_full++;
  3168. /* SET RxRE_RSP in Chip Att register */
  3169. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3170. writel(status, phba->CAregaddr);
  3171. readl(phba->CAregaddr); /* flush */
  3172. }
  3173. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3174. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3175. pring->stats.iocb_cmd_empty++;
  3176. /* Force update of the local copy of cmdGetInx */
  3177. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3178. lpfc_sli_resume_iocb(phba, pring);
  3179. if ((pring->lpfc_sli_cmd_available))
  3180. (pring->lpfc_sli_cmd_available) (phba, pring);
  3181. }
  3182. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3183. return;
  3184. }
  3185. /**
  3186. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3187. * @phba: Pointer to HBA context object.
  3188. * @pring: Pointer to driver SLI ring object.
  3189. * @mask: Host attention register mask for this ring.
  3190. *
  3191. * This function is called from the worker thread when there is a pending
  3192. * ELS response iocb on the driver internal slow-path response iocb worker
  3193. * queue. The caller does not hold any lock. The function will remove each
  3194. * response iocb from the response worker queue and calls the handle
  3195. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3196. **/
  3197. static void
  3198. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3199. struct lpfc_sli_ring *pring, uint32_t mask)
  3200. {
  3201. struct lpfc_iocbq *irspiocbq;
  3202. struct hbq_dmabuf *dmabuf;
  3203. struct lpfc_cq_event *cq_event;
  3204. unsigned long iflag;
  3205. spin_lock_irqsave(&phba->hbalock, iflag);
  3206. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3207. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3208. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3209. /* Get the response iocb from the head of work queue */
  3210. spin_lock_irqsave(&phba->hbalock, iflag);
  3211. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3212. cq_event, struct lpfc_cq_event, list);
  3213. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3214. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3215. case CQE_CODE_COMPL_WQE:
  3216. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3217. cq_event);
  3218. /* Translate ELS WCQE to response IOCBQ */
  3219. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3220. irspiocbq);
  3221. if (irspiocbq)
  3222. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3223. irspiocbq);
  3224. break;
  3225. case CQE_CODE_RECEIVE:
  3226. case CQE_CODE_RECEIVE_V1:
  3227. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3228. cq_event);
  3229. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3230. break;
  3231. default:
  3232. break;
  3233. }
  3234. }
  3235. }
  3236. /**
  3237. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3238. * @phba: Pointer to HBA context object.
  3239. * @pring: Pointer to driver SLI ring object.
  3240. *
  3241. * This function aborts all iocbs in the given ring and frees all the iocb
  3242. * objects in txq. This function issues an abort iocb for all the iocb commands
  3243. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3244. * the return of this function. The caller is not required to hold any locks.
  3245. **/
  3246. void
  3247. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3248. {
  3249. LIST_HEAD(completions);
  3250. struct lpfc_iocbq *iocb, *next_iocb;
  3251. if (pring->ringno == LPFC_ELS_RING) {
  3252. lpfc_fabric_abort_hba(phba);
  3253. }
  3254. /* Error everything on txq and txcmplq
  3255. * First do the txq.
  3256. */
  3257. if (phba->sli_rev >= LPFC_SLI_REV4) {
  3258. spin_lock_irq(&pring->ring_lock);
  3259. list_splice_init(&pring->txq, &completions);
  3260. pring->txq_cnt = 0;
  3261. spin_unlock_irq(&pring->ring_lock);
  3262. spin_lock_irq(&phba->hbalock);
  3263. /* Next issue ABTS for everything on the txcmplq */
  3264. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3265. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3266. spin_unlock_irq(&phba->hbalock);
  3267. } else {
  3268. spin_lock_irq(&phba->hbalock);
  3269. list_splice_init(&pring->txq, &completions);
  3270. pring->txq_cnt = 0;
  3271. /* Next issue ABTS for everything on the txcmplq */
  3272. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3273. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3274. spin_unlock_irq(&phba->hbalock);
  3275. }
  3276. /* Cancel all the IOCBs from the completions list */
  3277. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3278. IOERR_SLI_ABORTED);
  3279. }
  3280. /**
  3281. * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
  3282. * @phba: Pointer to HBA context object.
  3283. * @pring: Pointer to driver SLI ring object.
  3284. *
  3285. * This function aborts all iocbs in FCP rings and frees all the iocb
  3286. * objects in txq. This function issues an abort iocb for all the iocb commands
  3287. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3288. * the return of this function. The caller is not required to hold any locks.
  3289. **/
  3290. void
  3291. lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
  3292. {
  3293. struct lpfc_sli *psli = &phba->sli;
  3294. struct lpfc_sli_ring *pring;
  3295. uint32_t i;
  3296. /* Look on all the FCP Rings for the iotag */
  3297. if (phba->sli_rev >= LPFC_SLI_REV4) {
  3298. for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
  3299. pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
  3300. lpfc_sli_abort_iocb_ring(phba, pring);
  3301. }
  3302. } else {
  3303. pring = &psli->ring[psli->fcp_ring];
  3304. lpfc_sli_abort_iocb_ring(phba, pring);
  3305. }
  3306. }
  3307. /**
  3308. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3309. * @phba: Pointer to HBA context object.
  3310. *
  3311. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3312. * objects in txq and txcmplq. This function will not issue abort iocbs
  3313. * for all the iocb commands in txcmplq, they will just be returned with
  3314. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3315. * slot has been permanently disabled.
  3316. **/
  3317. void
  3318. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3319. {
  3320. LIST_HEAD(txq);
  3321. LIST_HEAD(txcmplq);
  3322. struct lpfc_sli *psli = &phba->sli;
  3323. struct lpfc_sli_ring *pring;
  3324. uint32_t i;
  3325. spin_lock_irq(&phba->hbalock);
  3326. /* Indicate the I/O queues are flushed */
  3327. phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
  3328. spin_unlock_irq(&phba->hbalock);
  3329. /* Look on all the FCP Rings for the iotag */
  3330. if (phba->sli_rev >= LPFC_SLI_REV4) {
  3331. for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
  3332. pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
  3333. spin_lock_irq(&pring->ring_lock);
  3334. /* Retrieve everything on txq */
  3335. list_splice_init(&pring->txq, &txq);
  3336. /* Retrieve everything on the txcmplq */
  3337. list_splice_init(&pring->txcmplq, &txcmplq);
  3338. pring->txq_cnt = 0;
  3339. pring->txcmplq_cnt = 0;
  3340. spin_unlock_irq(&pring->ring_lock);
  3341. /* Flush the txq */
  3342. lpfc_sli_cancel_iocbs(phba, &txq,
  3343. IOSTAT_LOCAL_REJECT,
  3344. IOERR_SLI_DOWN);
  3345. /* Flush the txcmpq */
  3346. lpfc_sli_cancel_iocbs(phba, &txcmplq,
  3347. IOSTAT_LOCAL_REJECT,
  3348. IOERR_SLI_DOWN);
  3349. }
  3350. } else {
  3351. pring = &psli->ring[psli->fcp_ring];
  3352. spin_lock_irq(&phba->hbalock);
  3353. /* Retrieve everything on txq */
  3354. list_splice_init(&pring->txq, &txq);
  3355. /* Retrieve everything on the txcmplq */
  3356. list_splice_init(&pring->txcmplq, &txcmplq);
  3357. pring->txq_cnt = 0;
  3358. pring->txcmplq_cnt = 0;
  3359. spin_unlock_irq(&phba->hbalock);
  3360. /* Flush the txq */
  3361. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3362. IOERR_SLI_DOWN);
  3363. /* Flush the txcmpq */
  3364. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3365. IOERR_SLI_DOWN);
  3366. }
  3367. }
  3368. /**
  3369. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3370. * @phba: Pointer to HBA context object.
  3371. * @mask: Bit mask to be checked.
  3372. *
  3373. * This function reads the host status register and compares
  3374. * with the provided bit mask to check if HBA completed
  3375. * the restart. This function will wait in a loop for the
  3376. * HBA to complete restart. If the HBA does not restart within
  3377. * 15 iterations, the function will reset the HBA again. The
  3378. * function returns 1 when HBA fail to restart otherwise returns
  3379. * zero.
  3380. **/
  3381. static int
  3382. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3383. {
  3384. uint32_t status;
  3385. int i = 0;
  3386. int retval = 0;
  3387. /* Read the HBA Host Status Register */
  3388. if (lpfc_readl(phba->HSregaddr, &status))
  3389. return 1;
  3390. /*
  3391. * Check status register every 100ms for 5 retries, then every
  3392. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3393. * every 2.5 sec for 4.
  3394. * Break our of the loop if errors occurred during init.
  3395. */
  3396. while (((status & mask) != mask) &&
  3397. !(status & HS_FFERM) &&
  3398. i++ < 20) {
  3399. if (i <= 5)
  3400. msleep(10);
  3401. else if (i <= 10)
  3402. msleep(500);
  3403. else
  3404. msleep(2500);
  3405. if (i == 15) {
  3406. /* Do post */
  3407. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3408. lpfc_sli_brdrestart(phba);
  3409. }
  3410. /* Read the HBA Host Status Register */
  3411. if (lpfc_readl(phba->HSregaddr, &status)) {
  3412. retval = 1;
  3413. break;
  3414. }
  3415. }
  3416. /* Check to see if any errors occurred during init */
  3417. if ((status & HS_FFERM) || (i >= 20)) {
  3418. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3419. "2751 Adapter failed to restart, "
  3420. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3421. status,
  3422. readl(phba->MBslimaddr + 0xa8),
  3423. readl(phba->MBslimaddr + 0xac));
  3424. phba->link_state = LPFC_HBA_ERROR;
  3425. retval = 1;
  3426. }
  3427. return retval;
  3428. }
  3429. /**
  3430. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3431. * @phba: Pointer to HBA context object.
  3432. * @mask: Bit mask to be checked.
  3433. *
  3434. * This function checks the host status register to check if HBA is
  3435. * ready. This function will wait in a loop for the HBA to be ready
  3436. * If the HBA is not ready , the function will will reset the HBA PCI
  3437. * function again. The function returns 1 when HBA fail to be ready
  3438. * otherwise returns zero.
  3439. **/
  3440. static int
  3441. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3442. {
  3443. uint32_t status;
  3444. int retval = 0;
  3445. /* Read the HBA Host Status Register */
  3446. status = lpfc_sli4_post_status_check(phba);
  3447. if (status) {
  3448. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3449. lpfc_sli_brdrestart(phba);
  3450. status = lpfc_sli4_post_status_check(phba);
  3451. }
  3452. /* Check to see if any errors occurred during init */
  3453. if (status) {
  3454. phba->link_state = LPFC_HBA_ERROR;
  3455. retval = 1;
  3456. } else
  3457. phba->sli4_hba.intr_enable = 0;
  3458. return retval;
  3459. }
  3460. /**
  3461. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3462. * @phba: Pointer to HBA context object.
  3463. * @mask: Bit mask to be checked.
  3464. *
  3465. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3466. * from the API jump table function pointer from the lpfc_hba struct.
  3467. **/
  3468. int
  3469. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3470. {
  3471. return phba->lpfc_sli_brdready(phba, mask);
  3472. }
  3473. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3474. /**
  3475. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3476. * @phba: Pointer to HBA context object.
  3477. *
  3478. * This function is called before resetting an HBA. This function is called
  3479. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3480. **/
  3481. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3482. {
  3483. uint32_t __iomem *resp_buf;
  3484. uint32_t __iomem *mbox_buf;
  3485. volatile uint32_t mbox;
  3486. uint32_t hc_copy, ha_copy, resp_data;
  3487. int i;
  3488. uint8_t hdrtype;
  3489. lockdep_assert_held(&phba->hbalock);
  3490. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3491. if (hdrtype != 0x80 ||
  3492. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3493. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3494. return;
  3495. /*
  3496. * Tell the other part of the chip to suspend temporarily all
  3497. * its DMA activity.
  3498. */
  3499. resp_buf = phba->MBslimaddr;
  3500. /* Disable the error attention */
  3501. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3502. return;
  3503. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3504. readl(phba->HCregaddr); /* flush */
  3505. phba->link_flag |= LS_IGNORE_ERATT;
  3506. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3507. return;
  3508. if (ha_copy & HA_ERATT) {
  3509. /* Clear Chip error bit */
  3510. writel(HA_ERATT, phba->HAregaddr);
  3511. phba->pport->stopped = 1;
  3512. }
  3513. mbox = 0;
  3514. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3515. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3516. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3517. mbox_buf = phba->MBslimaddr;
  3518. writel(mbox, mbox_buf);
  3519. for (i = 0; i < 50; i++) {
  3520. if (lpfc_readl((resp_buf + 1), &resp_data))
  3521. return;
  3522. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3523. mdelay(1);
  3524. else
  3525. break;
  3526. }
  3527. resp_data = 0;
  3528. if (lpfc_readl((resp_buf + 1), &resp_data))
  3529. return;
  3530. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3531. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3532. phba->pport->stopped)
  3533. goto restore_hc;
  3534. else
  3535. goto clear_errat;
  3536. }
  3537. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3538. resp_data = 0;
  3539. for (i = 0; i < 500; i++) {
  3540. if (lpfc_readl(resp_buf, &resp_data))
  3541. return;
  3542. if (resp_data != mbox)
  3543. mdelay(1);
  3544. else
  3545. break;
  3546. }
  3547. clear_errat:
  3548. while (++i < 500) {
  3549. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3550. return;
  3551. if (!(ha_copy & HA_ERATT))
  3552. mdelay(1);
  3553. else
  3554. break;
  3555. }
  3556. if (readl(phba->HAregaddr) & HA_ERATT) {
  3557. writel(HA_ERATT, phba->HAregaddr);
  3558. phba->pport->stopped = 1;
  3559. }
  3560. restore_hc:
  3561. phba->link_flag &= ~LS_IGNORE_ERATT;
  3562. writel(hc_copy, phba->HCregaddr);
  3563. readl(phba->HCregaddr); /* flush */
  3564. }
  3565. /**
  3566. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3567. * @phba: Pointer to HBA context object.
  3568. *
  3569. * This function issues a kill_board mailbox command and waits for
  3570. * the error attention interrupt. This function is called for stopping
  3571. * the firmware processing. The caller is not required to hold any
  3572. * locks. This function calls lpfc_hba_down_post function to free
  3573. * any pending commands after the kill. The function will return 1 when it
  3574. * fails to kill the board else will return 0.
  3575. **/
  3576. int
  3577. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3578. {
  3579. struct lpfc_sli *psli;
  3580. LPFC_MBOXQ_t *pmb;
  3581. uint32_t status;
  3582. uint32_t ha_copy;
  3583. int retval;
  3584. int i = 0;
  3585. psli = &phba->sli;
  3586. /* Kill HBA */
  3587. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3588. "0329 Kill HBA Data: x%x x%x\n",
  3589. phba->pport->port_state, psli->sli_flag);
  3590. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3591. if (!pmb)
  3592. return 1;
  3593. /* Disable the error attention */
  3594. spin_lock_irq(&phba->hbalock);
  3595. if (lpfc_readl(phba->HCregaddr, &status)) {
  3596. spin_unlock_irq(&phba->hbalock);
  3597. mempool_free(pmb, phba->mbox_mem_pool);
  3598. return 1;
  3599. }
  3600. status &= ~HC_ERINT_ENA;
  3601. writel(status, phba->HCregaddr);
  3602. readl(phba->HCregaddr); /* flush */
  3603. phba->link_flag |= LS_IGNORE_ERATT;
  3604. spin_unlock_irq(&phba->hbalock);
  3605. lpfc_kill_board(phba, pmb);
  3606. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3607. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3608. if (retval != MBX_SUCCESS) {
  3609. if (retval != MBX_BUSY)
  3610. mempool_free(pmb, phba->mbox_mem_pool);
  3611. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3612. "2752 KILL_BOARD command failed retval %d\n",
  3613. retval);
  3614. spin_lock_irq(&phba->hbalock);
  3615. phba->link_flag &= ~LS_IGNORE_ERATT;
  3616. spin_unlock_irq(&phba->hbalock);
  3617. return 1;
  3618. }
  3619. spin_lock_irq(&phba->hbalock);
  3620. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3621. spin_unlock_irq(&phba->hbalock);
  3622. mempool_free(pmb, phba->mbox_mem_pool);
  3623. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3624. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3625. * 3 seconds we still set HBA_ERROR state because the status of the
  3626. * board is now undefined.
  3627. */
  3628. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3629. return 1;
  3630. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3631. mdelay(100);
  3632. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3633. return 1;
  3634. }
  3635. del_timer_sync(&psli->mbox_tmo);
  3636. if (ha_copy & HA_ERATT) {
  3637. writel(HA_ERATT, phba->HAregaddr);
  3638. phba->pport->stopped = 1;
  3639. }
  3640. spin_lock_irq(&phba->hbalock);
  3641. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3642. psli->mbox_active = NULL;
  3643. phba->link_flag &= ~LS_IGNORE_ERATT;
  3644. spin_unlock_irq(&phba->hbalock);
  3645. lpfc_hba_down_post(phba);
  3646. phba->link_state = LPFC_HBA_ERROR;
  3647. return ha_copy & HA_ERATT ? 0 : 1;
  3648. }
  3649. /**
  3650. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3651. * @phba: Pointer to HBA context object.
  3652. *
  3653. * This function resets the HBA by writing HC_INITFF to the control
  3654. * register. After the HBA resets, this function resets all the iocb ring
  3655. * indices. This function disables PCI layer parity checking during
  3656. * the reset.
  3657. * This function returns 0 always.
  3658. * The caller is not required to hold any locks.
  3659. **/
  3660. int
  3661. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3662. {
  3663. struct lpfc_sli *psli;
  3664. struct lpfc_sli_ring *pring;
  3665. uint16_t cfg_value;
  3666. int i;
  3667. psli = &phba->sli;
  3668. /* Reset HBA */
  3669. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3670. "0325 Reset HBA Data: x%x x%x\n",
  3671. phba->pport->port_state, psli->sli_flag);
  3672. /* perform board reset */
  3673. phba->fc_eventTag = 0;
  3674. phba->link_events = 0;
  3675. phba->pport->fc_myDID = 0;
  3676. phba->pport->fc_prevDID = 0;
  3677. /* Turn off parity checking and serr during the physical reset */
  3678. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3679. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3680. (cfg_value &
  3681. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3682. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3683. /* Now toggle INITFF bit in the Host Control Register */
  3684. writel(HC_INITFF, phba->HCregaddr);
  3685. mdelay(1);
  3686. readl(phba->HCregaddr); /* flush */
  3687. writel(0, phba->HCregaddr);
  3688. readl(phba->HCregaddr); /* flush */
  3689. /* Restore PCI cmd register */
  3690. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3691. /* Initialize relevant SLI info */
  3692. for (i = 0; i < psli->num_rings; i++) {
  3693. pring = &psli->ring[i];
  3694. pring->flag = 0;
  3695. pring->sli.sli3.rspidx = 0;
  3696. pring->sli.sli3.next_cmdidx = 0;
  3697. pring->sli.sli3.local_getidx = 0;
  3698. pring->sli.sli3.cmdidx = 0;
  3699. pring->missbufcnt = 0;
  3700. }
  3701. phba->link_state = LPFC_WARM_START;
  3702. return 0;
  3703. }
  3704. /**
  3705. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3706. * @phba: Pointer to HBA context object.
  3707. *
  3708. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3709. * checking during resets the device. The caller is not required to hold
  3710. * any locks.
  3711. *
  3712. * This function returns 0 always.
  3713. **/
  3714. int
  3715. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3716. {
  3717. struct lpfc_sli *psli = &phba->sli;
  3718. uint16_t cfg_value;
  3719. int rc = 0;
  3720. /* Reset HBA */
  3721. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3722. "0295 Reset HBA Data: x%x x%x x%x\n",
  3723. phba->pport->port_state, psli->sli_flag,
  3724. phba->hba_flag);
  3725. /* perform board reset */
  3726. phba->fc_eventTag = 0;
  3727. phba->link_events = 0;
  3728. phba->pport->fc_myDID = 0;
  3729. phba->pport->fc_prevDID = 0;
  3730. spin_lock_irq(&phba->hbalock);
  3731. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3732. phba->fcf.fcf_flag = 0;
  3733. spin_unlock_irq(&phba->hbalock);
  3734. /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
  3735. if (phba->hba_flag & HBA_FW_DUMP_OP) {
  3736. phba->hba_flag &= ~HBA_FW_DUMP_OP;
  3737. return rc;
  3738. }
  3739. /* Now physically reset the device */
  3740. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3741. "0389 Performing PCI function reset!\n");
  3742. /* Turn off parity checking and serr during the physical reset */
  3743. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3744. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3745. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3746. /* Perform FCoE PCI function reset before freeing queue memory */
  3747. rc = lpfc_pci_function_reset(phba);
  3748. lpfc_sli4_queue_destroy(phba);
  3749. /* Restore PCI cmd register */
  3750. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3751. return rc;
  3752. }
  3753. /**
  3754. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3755. * @phba: Pointer to HBA context object.
  3756. *
  3757. * This function is called in the SLI initialization code path to
  3758. * restart the HBA. The caller is not required to hold any lock.
  3759. * This function writes MBX_RESTART mailbox command to the SLIM and
  3760. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3761. * function to free any pending commands. The function enables
  3762. * POST only during the first initialization. The function returns zero.
  3763. * The function does not guarantee completion of MBX_RESTART mailbox
  3764. * command before the return of this function.
  3765. **/
  3766. static int
  3767. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3768. {
  3769. MAILBOX_t *mb;
  3770. struct lpfc_sli *psli;
  3771. volatile uint32_t word0;
  3772. void __iomem *to_slim;
  3773. uint32_t hba_aer_enabled;
  3774. spin_lock_irq(&phba->hbalock);
  3775. /* Take PCIe device Advanced Error Reporting (AER) state */
  3776. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3777. psli = &phba->sli;
  3778. /* Restart HBA */
  3779. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3780. "0337 Restart HBA Data: x%x x%x\n",
  3781. phba->pport->port_state, psli->sli_flag);
  3782. word0 = 0;
  3783. mb = (MAILBOX_t *) &word0;
  3784. mb->mbxCommand = MBX_RESTART;
  3785. mb->mbxHc = 1;
  3786. lpfc_reset_barrier(phba);
  3787. to_slim = phba->MBslimaddr;
  3788. writel(*(uint32_t *) mb, to_slim);
  3789. readl(to_slim); /* flush */
  3790. /* Only skip post after fc_ffinit is completed */
  3791. if (phba->pport->port_state)
  3792. word0 = 1; /* This is really setting up word1 */
  3793. else
  3794. word0 = 0; /* This is really setting up word1 */
  3795. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3796. writel(*(uint32_t *) mb, to_slim);
  3797. readl(to_slim); /* flush */
  3798. lpfc_sli_brdreset(phba);
  3799. phba->pport->stopped = 0;
  3800. phba->link_state = LPFC_INIT_START;
  3801. phba->hba_flag = 0;
  3802. spin_unlock_irq(&phba->hbalock);
  3803. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3804. psli->stats_start = get_seconds();
  3805. /* Give the INITFF and Post time to settle. */
  3806. mdelay(100);
  3807. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3808. if (hba_aer_enabled)
  3809. pci_disable_pcie_error_reporting(phba->pcidev);
  3810. lpfc_hba_down_post(phba);
  3811. return 0;
  3812. }
  3813. /**
  3814. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3815. * @phba: Pointer to HBA context object.
  3816. *
  3817. * This function is called in the SLI initialization code path to restart
  3818. * a SLI4 HBA. The caller is not required to hold any lock.
  3819. * At the end of the function, it calls lpfc_hba_down_post function to
  3820. * free any pending commands.
  3821. **/
  3822. static int
  3823. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3824. {
  3825. struct lpfc_sli *psli = &phba->sli;
  3826. uint32_t hba_aer_enabled;
  3827. int rc;
  3828. /* Restart HBA */
  3829. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3830. "0296 Restart HBA Data: x%x x%x\n",
  3831. phba->pport->port_state, psli->sli_flag);
  3832. /* Take PCIe device Advanced Error Reporting (AER) state */
  3833. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3834. rc = lpfc_sli4_brdreset(phba);
  3835. spin_lock_irq(&phba->hbalock);
  3836. phba->pport->stopped = 0;
  3837. phba->link_state = LPFC_INIT_START;
  3838. phba->hba_flag = 0;
  3839. spin_unlock_irq(&phba->hbalock);
  3840. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3841. psli->stats_start = get_seconds();
  3842. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3843. if (hba_aer_enabled)
  3844. pci_disable_pcie_error_reporting(phba->pcidev);
  3845. lpfc_hba_down_post(phba);
  3846. return rc;
  3847. }
  3848. /**
  3849. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3850. * @phba: Pointer to HBA context object.
  3851. *
  3852. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3853. * API jump table function pointer from the lpfc_hba struct.
  3854. **/
  3855. int
  3856. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3857. {
  3858. return phba->lpfc_sli_brdrestart(phba);
  3859. }
  3860. /**
  3861. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3862. * @phba: Pointer to HBA context object.
  3863. *
  3864. * This function is called after a HBA restart to wait for successful
  3865. * restart of the HBA. Successful restart of the HBA is indicated by
  3866. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3867. * iteration, the function will restart the HBA again. The function returns
  3868. * zero if HBA successfully restarted else returns negative error code.
  3869. **/
  3870. static int
  3871. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3872. {
  3873. uint32_t status, i = 0;
  3874. /* Read the HBA Host Status Register */
  3875. if (lpfc_readl(phba->HSregaddr, &status))
  3876. return -EIO;
  3877. /* Check status register to see what current state is */
  3878. i = 0;
  3879. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3880. /* Check every 10ms for 10 retries, then every 100ms for 90
  3881. * retries, then every 1 sec for 50 retires for a total of
  3882. * ~60 seconds before reset the board again and check every
  3883. * 1 sec for 50 retries. The up to 60 seconds before the
  3884. * board ready is required by the Falcon FIPS zeroization
  3885. * complete, and any reset the board in between shall cause
  3886. * restart of zeroization, further delay the board ready.
  3887. */
  3888. if (i++ >= 200) {
  3889. /* Adapter failed to init, timeout, status reg
  3890. <status> */
  3891. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3892. "0436 Adapter failed to init, "
  3893. "timeout, status reg x%x, "
  3894. "FW Data: A8 x%x AC x%x\n", status,
  3895. readl(phba->MBslimaddr + 0xa8),
  3896. readl(phba->MBslimaddr + 0xac));
  3897. phba->link_state = LPFC_HBA_ERROR;
  3898. return -ETIMEDOUT;
  3899. }
  3900. /* Check to see if any errors occurred during init */
  3901. if (status & HS_FFERM) {
  3902. /* ERROR: During chipset initialization */
  3903. /* Adapter failed to init, chipset, status reg
  3904. <status> */
  3905. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3906. "0437 Adapter failed to init, "
  3907. "chipset, status reg x%x, "
  3908. "FW Data: A8 x%x AC x%x\n", status,
  3909. readl(phba->MBslimaddr + 0xa8),
  3910. readl(phba->MBslimaddr + 0xac));
  3911. phba->link_state = LPFC_HBA_ERROR;
  3912. return -EIO;
  3913. }
  3914. if (i <= 10)
  3915. msleep(10);
  3916. else if (i <= 100)
  3917. msleep(100);
  3918. else
  3919. msleep(1000);
  3920. if (i == 150) {
  3921. /* Do post */
  3922. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3923. lpfc_sli_brdrestart(phba);
  3924. }
  3925. /* Read the HBA Host Status Register */
  3926. if (lpfc_readl(phba->HSregaddr, &status))
  3927. return -EIO;
  3928. }
  3929. /* Check to see if any errors occurred during init */
  3930. if (status & HS_FFERM) {
  3931. /* ERROR: During chipset initialization */
  3932. /* Adapter failed to init, chipset, status reg <status> */
  3933. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3934. "0438 Adapter failed to init, chipset, "
  3935. "status reg x%x, "
  3936. "FW Data: A8 x%x AC x%x\n", status,
  3937. readl(phba->MBslimaddr + 0xa8),
  3938. readl(phba->MBslimaddr + 0xac));
  3939. phba->link_state = LPFC_HBA_ERROR;
  3940. return -EIO;
  3941. }
  3942. /* Clear all interrupt enable conditions */
  3943. writel(0, phba->HCregaddr);
  3944. readl(phba->HCregaddr); /* flush */
  3945. /* setup host attn register */
  3946. writel(0xffffffff, phba->HAregaddr);
  3947. readl(phba->HAregaddr); /* flush */
  3948. return 0;
  3949. }
  3950. /**
  3951. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3952. *
  3953. * This function calculates and returns the number of HBQs required to be
  3954. * configured.
  3955. **/
  3956. int
  3957. lpfc_sli_hbq_count(void)
  3958. {
  3959. return ARRAY_SIZE(lpfc_hbq_defs);
  3960. }
  3961. /**
  3962. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3963. *
  3964. * This function adds the number of hbq entries in every HBQ to get
  3965. * the total number of hbq entries required for the HBA and returns
  3966. * the total count.
  3967. **/
  3968. static int
  3969. lpfc_sli_hbq_entry_count(void)
  3970. {
  3971. int hbq_count = lpfc_sli_hbq_count();
  3972. int count = 0;
  3973. int i;
  3974. for (i = 0; i < hbq_count; ++i)
  3975. count += lpfc_hbq_defs[i]->entry_count;
  3976. return count;
  3977. }
  3978. /**
  3979. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3980. *
  3981. * This function calculates amount of memory required for all hbq entries
  3982. * to be configured and returns the total memory required.
  3983. **/
  3984. int
  3985. lpfc_sli_hbq_size(void)
  3986. {
  3987. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3988. }
  3989. /**
  3990. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3991. * @phba: Pointer to HBA context object.
  3992. *
  3993. * This function is called during the SLI initialization to configure
  3994. * all the HBQs and post buffers to the HBQ. The caller is not
  3995. * required to hold any locks. This function will return zero if successful
  3996. * else it will return negative error code.
  3997. **/
  3998. static int
  3999. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  4000. {
  4001. int hbq_count = lpfc_sli_hbq_count();
  4002. LPFC_MBOXQ_t *pmb;
  4003. MAILBOX_t *pmbox;
  4004. uint32_t hbqno;
  4005. uint32_t hbq_entry_index;
  4006. /* Get a Mailbox buffer to setup mailbox
  4007. * commands for HBA initialization
  4008. */
  4009. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4010. if (!pmb)
  4011. return -ENOMEM;
  4012. pmbox = &pmb->u.mb;
  4013. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  4014. phba->link_state = LPFC_INIT_MBX_CMDS;
  4015. phba->hbq_in_use = 1;
  4016. hbq_entry_index = 0;
  4017. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  4018. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  4019. phba->hbqs[hbqno].hbqPutIdx = 0;
  4020. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  4021. phba->hbqs[hbqno].entry_count =
  4022. lpfc_hbq_defs[hbqno]->entry_count;
  4023. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  4024. hbq_entry_index, pmb);
  4025. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  4026. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  4027. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  4028. mbxStatus <status>, ring <num> */
  4029. lpfc_printf_log(phba, KERN_ERR,
  4030. LOG_SLI | LOG_VPORT,
  4031. "1805 Adapter failed to init. "
  4032. "Data: x%x x%x x%x\n",
  4033. pmbox->mbxCommand,
  4034. pmbox->mbxStatus, hbqno);
  4035. phba->link_state = LPFC_HBA_ERROR;
  4036. mempool_free(pmb, phba->mbox_mem_pool);
  4037. return -ENXIO;
  4038. }
  4039. }
  4040. phba->hbq_count = hbq_count;
  4041. mempool_free(pmb, phba->mbox_mem_pool);
  4042. /* Initially populate or replenish the HBQs */
  4043. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  4044. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  4045. return 0;
  4046. }
  4047. /**
  4048. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  4049. * @phba: Pointer to HBA context object.
  4050. *
  4051. * This function is called during the SLI initialization to configure
  4052. * all the HBQs and post buffers to the HBQ. The caller is not
  4053. * required to hold any locks. This function will return zero if successful
  4054. * else it will return negative error code.
  4055. **/
  4056. static int
  4057. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  4058. {
  4059. phba->hbq_in_use = 1;
  4060. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  4061. phba->hbq_count = 1;
  4062. /* Initially populate or replenish the HBQs */
  4063. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  4064. return 0;
  4065. }
  4066. /**
  4067. * lpfc_sli_config_port - Issue config port mailbox command
  4068. * @phba: Pointer to HBA context object.
  4069. * @sli_mode: sli mode - 2/3
  4070. *
  4071. * This function is called by the sli intialization code path
  4072. * to issue config_port mailbox command. This function restarts the
  4073. * HBA firmware and issues a config_port mailbox command to configure
  4074. * the SLI interface in the sli mode specified by sli_mode
  4075. * variable. The caller is not required to hold any locks.
  4076. * The function returns 0 if successful, else returns negative error
  4077. * code.
  4078. **/
  4079. int
  4080. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  4081. {
  4082. LPFC_MBOXQ_t *pmb;
  4083. uint32_t resetcount = 0, rc = 0, done = 0;
  4084. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4085. if (!pmb) {
  4086. phba->link_state = LPFC_HBA_ERROR;
  4087. return -ENOMEM;
  4088. }
  4089. phba->sli_rev = sli_mode;
  4090. while (resetcount < 2 && !done) {
  4091. spin_lock_irq(&phba->hbalock);
  4092. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  4093. spin_unlock_irq(&phba->hbalock);
  4094. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  4095. lpfc_sli_brdrestart(phba);
  4096. rc = lpfc_sli_chipset_init(phba);
  4097. if (rc)
  4098. break;
  4099. spin_lock_irq(&phba->hbalock);
  4100. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4101. spin_unlock_irq(&phba->hbalock);
  4102. resetcount++;
  4103. /* Call pre CONFIG_PORT mailbox command initialization. A
  4104. * value of 0 means the call was successful. Any other
  4105. * nonzero value is a failure, but if ERESTART is returned,
  4106. * the driver may reset the HBA and try again.
  4107. */
  4108. rc = lpfc_config_port_prep(phba);
  4109. if (rc == -ERESTART) {
  4110. phba->link_state = LPFC_LINK_UNKNOWN;
  4111. continue;
  4112. } else if (rc)
  4113. break;
  4114. phba->link_state = LPFC_INIT_MBX_CMDS;
  4115. lpfc_config_port(phba, pmb);
  4116. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  4117. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  4118. LPFC_SLI3_HBQ_ENABLED |
  4119. LPFC_SLI3_CRP_ENABLED |
  4120. LPFC_SLI3_BG_ENABLED |
  4121. LPFC_SLI3_DSS_ENABLED);
  4122. if (rc != MBX_SUCCESS) {
  4123. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4124. "0442 Adapter failed to init, mbxCmd x%x "
  4125. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  4126. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  4127. spin_lock_irq(&phba->hbalock);
  4128. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  4129. spin_unlock_irq(&phba->hbalock);
  4130. rc = -ENXIO;
  4131. } else {
  4132. /* Allow asynchronous mailbox command to go through */
  4133. spin_lock_irq(&phba->hbalock);
  4134. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  4135. spin_unlock_irq(&phba->hbalock);
  4136. done = 1;
  4137. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  4138. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  4139. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  4140. "3110 Port did not grant ASABT\n");
  4141. }
  4142. }
  4143. if (!done) {
  4144. rc = -EINVAL;
  4145. goto do_prep_failed;
  4146. }
  4147. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  4148. if (!pmb->u.mb.un.varCfgPort.cMA) {
  4149. rc = -ENXIO;
  4150. goto do_prep_failed;
  4151. }
  4152. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  4153. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  4154. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  4155. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  4156. phba->max_vpi : phba->max_vports;
  4157. } else
  4158. phba->max_vpi = 0;
  4159. phba->fips_level = 0;
  4160. phba->fips_spec_rev = 0;
  4161. if (pmb->u.mb.un.varCfgPort.gdss) {
  4162. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  4163. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  4164. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  4165. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4166. "2850 Security Crypto Active. FIPS x%d "
  4167. "(Spec Rev: x%d)",
  4168. phba->fips_level, phba->fips_spec_rev);
  4169. }
  4170. if (pmb->u.mb.un.varCfgPort.sec_err) {
  4171. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4172. "2856 Config Port Security Crypto "
  4173. "Error: x%x ",
  4174. pmb->u.mb.un.varCfgPort.sec_err);
  4175. }
  4176. if (pmb->u.mb.un.varCfgPort.gerbm)
  4177. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  4178. if (pmb->u.mb.un.varCfgPort.gcrp)
  4179. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  4180. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  4181. phba->port_gp = phba->mbox->us.s3_pgp.port;
  4182. if (phba->cfg_enable_bg) {
  4183. if (pmb->u.mb.un.varCfgPort.gbg)
  4184. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  4185. else
  4186. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4187. "0443 Adapter did not grant "
  4188. "BlockGuard\n");
  4189. }
  4190. } else {
  4191. phba->hbq_get = NULL;
  4192. phba->port_gp = phba->mbox->us.s2.port;
  4193. phba->max_vpi = 0;
  4194. }
  4195. do_prep_failed:
  4196. mempool_free(pmb, phba->mbox_mem_pool);
  4197. return rc;
  4198. }
  4199. /**
  4200. * lpfc_sli_hba_setup - SLI intialization function
  4201. * @phba: Pointer to HBA context object.
  4202. *
  4203. * This function is the main SLI intialization function. This function
  4204. * is called by the HBA intialization code, HBA reset code and HBA
  4205. * error attention handler code. Caller is not required to hold any
  4206. * locks. This function issues config_port mailbox command to configure
  4207. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4208. * calls the config_port_post function to issue init_link mailbox
  4209. * command and to start the discovery. The function will return zero
  4210. * if successful, else it will return negative error code.
  4211. **/
  4212. int
  4213. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4214. {
  4215. uint32_t rc;
  4216. int mode = 3, i;
  4217. int longs;
  4218. switch (phba->cfg_sli_mode) {
  4219. case 2:
  4220. if (phba->cfg_enable_npiv) {
  4221. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4222. "1824 NPIV enabled: Override sli_mode "
  4223. "parameter (%d) to auto (0).\n",
  4224. phba->cfg_sli_mode);
  4225. break;
  4226. }
  4227. mode = 2;
  4228. break;
  4229. case 0:
  4230. case 3:
  4231. break;
  4232. default:
  4233. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4234. "1819 Unrecognized sli_mode parameter: %d.\n",
  4235. phba->cfg_sli_mode);
  4236. break;
  4237. }
  4238. phba->fcp_embed_io = 0; /* SLI4 FC support only */
  4239. rc = lpfc_sli_config_port(phba, mode);
  4240. if (rc && phba->cfg_sli_mode == 3)
  4241. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4242. "1820 Unable to select SLI-3. "
  4243. "Not supported by adapter.\n");
  4244. if (rc && mode != 2)
  4245. rc = lpfc_sli_config_port(phba, 2);
  4246. else if (rc && mode == 2)
  4247. rc = lpfc_sli_config_port(phba, 3);
  4248. if (rc)
  4249. goto lpfc_sli_hba_setup_error;
  4250. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4251. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4252. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4253. if (!rc) {
  4254. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4255. "2709 This device supports "
  4256. "Advanced Error Reporting (AER)\n");
  4257. spin_lock_irq(&phba->hbalock);
  4258. phba->hba_flag |= HBA_AER_ENABLED;
  4259. spin_unlock_irq(&phba->hbalock);
  4260. } else {
  4261. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4262. "2708 This device does not support "
  4263. "Advanced Error Reporting (AER): %d\n",
  4264. rc);
  4265. phba->cfg_aer_support = 0;
  4266. }
  4267. }
  4268. if (phba->sli_rev == 3) {
  4269. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4270. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4271. } else {
  4272. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4273. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4274. phba->sli3_options = 0;
  4275. }
  4276. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4277. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4278. phba->sli_rev, phba->max_vpi);
  4279. rc = lpfc_sli_ring_map(phba);
  4280. if (rc)
  4281. goto lpfc_sli_hba_setup_error;
  4282. /* Initialize VPIs. */
  4283. if (phba->sli_rev == LPFC_SLI_REV3) {
  4284. /*
  4285. * The VPI bitmask and physical ID array are allocated
  4286. * and initialized once only - at driver load. A port
  4287. * reset doesn't need to reinitialize this memory.
  4288. */
  4289. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4290. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4291. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4292. GFP_KERNEL);
  4293. if (!phba->vpi_bmask) {
  4294. rc = -ENOMEM;
  4295. goto lpfc_sli_hba_setup_error;
  4296. }
  4297. phba->vpi_ids = kzalloc(
  4298. (phba->max_vpi+1) * sizeof(uint16_t),
  4299. GFP_KERNEL);
  4300. if (!phba->vpi_ids) {
  4301. kfree(phba->vpi_bmask);
  4302. rc = -ENOMEM;
  4303. goto lpfc_sli_hba_setup_error;
  4304. }
  4305. for (i = 0; i < phba->max_vpi; i++)
  4306. phba->vpi_ids[i] = i;
  4307. }
  4308. }
  4309. /* Init HBQs */
  4310. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4311. rc = lpfc_sli_hbq_setup(phba);
  4312. if (rc)
  4313. goto lpfc_sli_hba_setup_error;
  4314. }
  4315. spin_lock_irq(&phba->hbalock);
  4316. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4317. spin_unlock_irq(&phba->hbalock);
  4318. rc = lpfc_config_port_post(phba);
  4319. if (rc)
  4320. goto lpfc_sli_hba_setup_error;
  4321. return rc;
  4322. lpfc_sli_hba_setup_error:
  4323. phba->link_state = LPFC_HBA_ERROR;
  4324. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4325. "0445 Firmware initialization failed\n");
  4326. return rc;
  4327. }
  4328. /**
  4329. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4330. * @phba: Pointer to HBA context object.
  4331. * @mboxq: mailbox pointer.
  4332. * This function issue a dump mailbox command to read config region
  4333. * 23 and parse the records in the region and populate driver
  4334. * data structure.
  4335. **/
  4336. static int
  4337. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4338. {
  4339. LPFC_MBOXQ_t *mboxq;
  4340. struct lpfc_dmabuf *mp;
  4341. struct lpfc_mqe *mqe;
  4342. uint32_t data_length;
  4343. int rc;
  4344. /* Program the default value of vlan_id and fc_map */
  4345. phba->valid_vlan = 0;
  4346. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4347. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4348. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4349. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4350. if (!mboxq)
  4351. return -ENOMEM;
  4352. mqe = &mboxq->u.mqe;
  4353. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4354. rc = -ENOMEM;
  4355. goto out_free_mboxq;
  4356. }
  4357. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4358. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4359. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4360. "(%d):2571 Mailbox cmd x%x Status x%x "
  4361. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4362. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4363. "CQ: x%x x%x x%x x%x\n",
  4364. mboxq->vport ? mboxq->vport->vpi : 0,
  4365. bf_get(lpfc_mqe_command, mqe),
  4366. bf_get(lpfc_mqe_status, mqe),
  4367. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4368. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4369. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4370. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4371. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4372. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4373. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4374. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4375. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4376. mboxq->mcqe.word0,
  4377. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4378. mboxq->mcqe.trailer);
  4379. if (rc) {
  4380. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4381. kfree(mp);
  4382. rc = -EIO;
  4383. goto out_free_mboxq;
  4384. }
  4385. data_length = mqe->un.mb_words[5];
  4386. if (data_length > DMP_RGN23_SIZE) {
  4387. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4388. kfree(mp);
  4389. rc = -EIO;
  4390. goto out_free_mboxq;
  4391. }
  4392. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4393. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4394. kfree(mp);
  4395. rc = 0;
  4396. out_free_mboxq:
  4397. mempool_free(mboxq, phba->mbox_mem_pool);
  4398. return rc;
  4399. }
  4400. /**
  4401. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4402. * @phba: pointer to lpfc hba data structure.
  4403. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4404. * @vpd: pointer to the memory to hold resulting port vpd data.
  4405. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4406. * On output, the number of data bytes in @vpd.
  4407. *
  4408. * This routine executes a READ_REV SLI4 mailbox command. In
  4409. * addition, this routine gets the port vpd data.
  4410. *
  4411. * Return codes
  4412. * 0 - successful
  4413. * -ENOMEM - could not allocated memory.
  4414. **/
  4415. static int
  4416. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4417. uint8_t *vpd, uint32_t *vpd_size)
  4418. {
  4419. int rc = 0;
  4420. uint32_t dma_size;
  4421. struct lpfc_dmabuf *dmabuf;
  4422. struct lpfc_mqe *mqe;
  4423. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4424. if (!dmabuf)
  4425. return -ENOMEM;
  4426. /*
  4427. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4428. * mailbox command.
  4429. */
  4430. dma_size = *vpd_size;
  4431. dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
  4432. &dmabuf->phys, GFP_KERNEL);
  4433. if (!dmabuf->virt) {
  4434. kfree(dmabuf);
  4435. return -ENOMEM;
  4436. }
  4437. /*
  4438. * The SLI4 implementation of READ_REV conflicts at word1,
  4439. * bits 31:16 and SLI4 adds vpd functionality not present
  4440. * in SLI3. This code corrects the conflicts.
  4441. */
  4442. lpfc_read_rev(phba, mboxq);
  4443. mqe = &mboxq->u.mqe;
  4444. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4445. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4446. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4447. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4448. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4449. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4450. if (rc) {
  4451. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4452. dmabuf->virt, dmabuf->phys);
  4453. kfree(dmabuf);
  4454. return -EIO;
  4455. }
  4456. /*
  4457. * The available vpd length cannot be bigger than the
  4458. * DMA buffer passed to the port. Catch the less than
  4459. * case and update the caller's size.
  4460. */
  4461. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4462. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4463. memcpy(vpd, dmabuf->virt, *vpd_size);
  4464. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4465. dmabuf->virt, dmabuf->phys);
  4466. kfree(dmabuf);
  4467. return 0;
  4468. }
  4469. /**
  4470. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4471. * @phba: pointer to lpfc hba data structure.
  4472. *
  4473. * This routine retrieves SLI4 device physical port name this PCI function
  4474. * is attached to.
  4475. *
  4476. * Return codes
  4477. * 0 - successful
  4478. * otherwise - failed to retrieve physical port name
  4479. **/
  4480. static int
  4481. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4482. {
  4483. LPFC_MBOXQ_t *mboxq;
  4484. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4485. struct lpfc_controller_attribute *cntl_attr;
  4486. struct lpfc_mbx_get_port_name *get_port_name;
  4487. void *virtaddr = NULL;
  4488. uint32_t alloclen, reqlen;
  4489. uint32_t shdr_status, shdr_add_status;
  4490. union lpfc_sli4_cfg_shdr *shdr;
  4491. char cport_name = 0;
  4492. int rc;
  4493. /* We assume nothing at this point */
  4494. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4495. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4496. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4497. if (!mboxq)
  4498. return -ENOMEM;
  4499. /* obtain link type and link number via READ_CONFIG */
  4500. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4501. lpfc_sli4_read_config(phba);
  4502. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4503. goto retrieve_ppname;
  4504. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4505. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4506. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4507. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4508. LPFC_SLI4_MBX_NEMBED);
  4509. if (alloclen < reqlen) {
  4510. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4511. "3084 Allocated DMA memory size (%d) is "
  4512. "less than the requested DMA memory size "
  4513. "(%d)\n", alloclen, reqlen);
  4514. rc = -ENOMEM;
  4515. goto out_free_mboxq;
  4516. }
  4517. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4518. virtaddr = mboxq->sge_array->addr[0];
  4519. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4520. shdr = &mbx_cntl_attr->cfg_shdr;
  4521. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4522. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4523. if (shdr_status || shdr_add_status || rc) {
  4524. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4525. "3085 Mailbox x%x (x%x/x%x) failed, "
  4526. "rc:x%x, status:x%x, add_status:x%x\n",
  4527. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4528. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4529. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4530. rc, shdr_status, shdr_add_status);
  4531. rc = -ENXIO;
  4532. goto out_free_mboxq;
  4533. }
  4534. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4535. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4536. phba->sli4_hba.lnk_info.lnk_tp =
  4537. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4538. phba->sli4_hba.lnk_info.lnk_no =
  4539. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4540. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4541. "3086 lnk_type:%d, lnk_numb:%d\n",
  4542. phba->sli4_hba.lnk_info.lnk_tp,
  4543. phba->sli4_hba.lnk_info.lnk_no);
  4544. retrieve_ppname:
  4545. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4546. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4547. sizeof(struct lpfc_mbx_get_port_name) -
  4548. sizeof(struct lpfc_sli4_cfg_mhdr),
  4549. LPFC_SLI4_MBX_EMBED);
  4550. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4551. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4552. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4553. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4554. phba->sli4_hba.lnk_info.lnk_tp);
  4555. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4556. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4557. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4558. if (shdr_status || shdr_add_status || rc) {
  4559. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4560. "3087 Mailbox x%x (x%x/x%x) failed: "
  4561. "rc:x%x, status:x%x, add_status:x%x\n",
  4562. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4563. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4564. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4565. rc, shdr_status, shdr_add_status);
  4566. rc = -ENXIO;
  4567. goto out_free_mboxq;
  4568. }
  4569. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4570. case LPFC_LINK_NUMBER_0:
  4571. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4572. &get_port_name->u.response);
  4573. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4574. break;
  4575. case LPFC_LINK_NUMBER_1:
  4576. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4577. &get_port_name->u.response);
  4578. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4579. break;
  4580. case LPFC_LINK_NUMBER_2:
  4581. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4582. &get_port_name->u.response);
  4583. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4584. break;
  4585. case LPFC_LINK_NUMBER_3:
  4586. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4587. &get_port_name->u.response);
  4588. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4589. break;
  4590. default:
  4591. break;
  4592. }
  4593. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4594. phba->Port[0] = cport_name;
  4595. phba->Port[1] = '\0';
  4596. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4597. "3091 SLI get port name: %s\n", phba->Port);
  4598. }
  4599. out_free_mboxq:
  4600. if (rc != MBX_TIMEOUT) {
  4601. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4602. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4603. else
  4604. mempool_free(mboxq, phba->mbox_mem_pool);
  4605. }
  4606. return rc;
  4607. }
  4608. /**
  4609. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4610. * @phba: pointer to lpfc hba data structure.
  4611. *
  4612. * This routine is called to explicitly arm the SLI4 device's completion and
  4613. * event queues
  4614. **/
  4615. static void
  4616. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4617. {
  4618. int fcp_eqidx;
  4619. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4620. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4621. fcp_eqidx = 0;
  4622. if (phba->sli4_hba.fcp_cq) {
  4623. do {
  4624. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4625. LPFC_QUEUE_REARM);
  4626. } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
  4627. }
  4628. if (phba->cfg_fof)
  4629. lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
  4630. if (phba->sli4_hba.hba_eq) {
  4631. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
  4632. fcp_eqidx++)
  4633. lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
  4634. LPFC_QUEUE_REARM);
  4635. }
  4636. if (phba->cfg_fof)
  4637. lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
  4638. }
  4639. /**
  4640. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4641. * @phba: Pointer to HBA context object.
  4642. * @type: The resource extent type.
  4643. * @extnt_count: buffer to hold port available extent count.
  4644. * @extnt_size: buffer to hold element count per extent.
  4645. *
  4646. * This function calls the port and retrievs the number of available
  4647. * extents and their size for a particular extent type.
  4648. *
  4649. * Returns: 0 if successful. Nonzero otherwise.
  4650. **/
  4651. int
  4652. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4653. uint16_t *extnt_count, uint16_t *extnt_size)
  4654. {
  4655. int rc = 0;
  4656. uint32_t length;
  4657. uint32_t mbox_tmo;
  4658. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4659. LPFC_MBOXQ_t *mbox;
  4660. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4661. if (!mbox)
  4662. return -ENOMEM;
  4663. /* Find out how many extents are available for this resource type */
  4664. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4665. sizeof(struct lpfc_sli4_cfg_mhdr));
  4666. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4667. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4668. length, LPFC_SLI4_MBX_EMBED);
  4669. /* Send an extents count of 0 - the GET doesn't use it. */
  4670. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4671. LPFC_SLI4_MBX_EMBED);
  4672. if (unlikely(rc)) {
  4673. rc = -EIO;
  4674. goto err_exit;
  4675. }
  4676. if (!phba->sli4_hba.intr_enable)
  4677. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4678. else {
  4679. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4680. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4681. }
  4682. if (unlikely(rc)) {
  4683. rc = -EIO;
  4684. goto err_exit;
  4685. }
  4686. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4687. if (bf_get(lpfc_mbox_hdr_status,
  4688. &rsrc_info->header.cfg_shdr.response)) {
  4689. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4690. "2930 Failed to get resource extents "
  4691. "Status 0x%x Add'l Status 0x%x\n",
  4692. bf_get(lpfc_mbox_hdr_status,
  4693. &rsrc_info->header.cfg_shdr.response),
  4694. bf_get(lpfc_mbox_hdr_add_status,
  4695. &rsrc_info->header.cfg_shdr.response));
  4696. rc = -EIO;
  4697. goto err_exit;
  4698. }
  4699. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4700. &rsrc_info->u.rsp);
  4701. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4702. &rsrc_info->u.rsp);
  4703. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4704. "3162 Retrieved extents type-%d from port: count:%d, "
  4705. "size:%d\n", type, *extnt_count, *extnt_size);
  4706. err_exit:
  4707. mempool_free(mbox, phba->mbox_mem_pool);
  4708. return rc;
  4709. }
  4710. /**
  4711. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4712. * @phba: Pointer to HBA context object.
  4713. * @type: The extent type to check.
  4714. *
  4715. * This function reads the current available extents from the port and checks
  4716. * if the extent count or extent size has changed since the last access.
  4717. * Callers use this routine post port reset to understand if there is a
  4718. * extent reprovisioning requirement.
  4719. *
  4720. * Returns:
  4721. * -Error: error indicates problem.
  4722. * 1: Extent count or size has changed.
  4723. * 0: No changes.
  4724. **/
  4725. static int
  4726. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4727. {
  4728. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4729. uint16_t size_diff, rsrc_ext_size;
  4730. int rc = 0;
  4731. struct lpfc_rsrc_blks *rsrc_entry;
  4732. struct list_head *rsrc_blk_list = NULL;
  4733. size_diff = 0;
  4734. curr_ext_cnt = 0;
  4735. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4736. &rsrc_ext_cnt,
  4737. &rsrc_ext_size);
  4738. if (unlikely(rc))
  4739. return -EIO;
  4740. switch (type) {
  4741. case LPFC_RSC_TYPE_FCOE_RPI:
  4742. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4743. break;
  4744. case LPFC_RSC_TYPE_FCOE_VPI:
  4745. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4746. break;
  4747. case LPFC_RSC_TYPE_FCOE_XRI:
  4748. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4749. break;
  4750. case LPFC_RSC_TYPE_FCOE_VFI:
  4751. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4752. break;
  4753. default:
  4754. break;
  4755. }
  4756. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4757. curr_ext_cnt++;
  4758. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4759. size_diff++;
  4760. }
  4761. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4762. rc = 1;
  4763. return rc;
  4764. }
  4765. /**
  4766. * lpfc_sli4_cfg_post_extnts -
  4767. * @phba: Pointer to HBA context object.
  4768. * @extnt_cnt - number of available extents.
  4769. * @type - the extent type (rpi, xri, vfi, vpi).
  4770. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4771. * @mbox - pointer to the caller's allocated mailbox structure.
  4772. *
  4773. * This function executes the extents allocation request. It also
  4774. * takes care of the amount of memory needed to allocate or get the
  4775. * allocated extents. It is the caller's responsibility to evaluate
  4776. * the response.
  4777. *
  4778. * Returns:
  4779. * -Error: Error value describes the condition found.
  4780. * 0: if successful
  4781. **/
  4782. static int
  4783. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  4784. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4785. {
  4786. int rc = 0;
  4787. uint32_t req_len;
  4788. uint32_t emb_len;
  4789. uint32_t alloc_len, mbox_tmo;
  4790. /* Calculate the total requested length of the dma memory */
  4791. req_len = extnt_cnt * sizeof(uint16_t);
  4792. /*
  4793. * Calculate the size of an embedded mailbox. The uint32_t
  4794. * accounts for extents-specific word.
  4795. */
  4796. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4797. sizeof(uint32_t);
  4798. /*
  4799. * Presume the allocation and response will fit into an embedded
  4800. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4801. */
  4802. *emb = LPFC_SLI4_MBX_EMBED;
  4803. if (req_len > emb_len) {
  4804. req_len = extnt_cnt * sizeof(uint16_t) +
  4805. sizeof(union lpfc_sli4_cfg_shdr) +
  4806. sizeof(uint32_t);
  4807. *emb = LPFC_SLI4_MBX_NEMBED;
  4808. }
  4809. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4810. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4811. req_len, *emb);
  4812. if (alloc_len < req_len) {
  4813. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4814. "2982 Allocated DMA memory size (x%x) is "
  4815. "less than the requested DMA memory "
  4816. "size (x%x)\n", alloc_len, req_len);
  4817. return -ENOMEM;
  4818. }
  4819. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  4820. if (unlikely(rc))
  4821. return -EIO;
  4822. if (!phba->sli4_hba.intr_enable)
  4823. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4824. else {
  4825. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4826. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4827. }
  4828. if (unlikely(rc))
  4829. rc = -EIO;
  4830. return rc;
  4831. }
  4832. /**
  4833. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4834. * @phba: Pointer to HBA context object.
  4835. * @type: The resource extent type to allocate.
  4836. *
  4837. * This function allocates the number of elements for the specified
  4838. * resource type.
  4839. **/
  4840. static int
  4841. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4842. {
  4843. bool emb = false;
  4844. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4845. uint16_t rsrc_id, rsrc_start, j, k;
  4846. uint16_t *ids;
  4847. int i, rc;
  4848. unsigned long longs;
  4849. unsigned long *bmask;
  4850. struct lpfc_rsrc_blks *rsrc_blks;
  4851. LPFC_MBOXQ_t *mbox;
  4852. uint32_t length;
  4853. struct lpfc_id_range *id_array = NULL;
  4854. void *virtaddr = NULL;
  4855. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4856. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4857. struct list_head *ext_blk_list;
  4858. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4859. &rsrc_cnt,
  4860. &rsrc_size);
  4861. if (unlikely(rc))
  4862. return -EIO;
  4863. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4864. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4865. "3009 No available Resource Extents "
  4866. "for resource type 0x%x: Count: 0x%x, "
  4867. "Size 0x%x\n", type, rsrc_cnt,
  4868. rsrc_size);
  4869. return -ENOMEM;
  4870. }
  4871. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  4872. "2903 Post resource extents type-0x%x: "
  4873. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  4874. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4875. if (!mbox)
  4876. return -ENOMEM;
  4877. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  4878. if (unlikely(rc)) {
  4879. rc = -EIO;
  4880. goto err_exit;
  4881. }
  4882. /*
  4883. * Figure out where the response is located. Then get local pointers
  4884. * to the response data. The port does not guarantee to respond to
  4885. * all extents counts request so update the local variable with the
  4886. * allocated count from the port.
  4887. */
  4888. if (emb == LPFC_SLI4_MBX_EMBED) {
  4889. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4890. id_array = &rsrc_ext->u.rsp.id[0];
  4891. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4892. } else {
  4893. virtaddr = mbox->sge_array->addr[0];
  4894. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4895. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4896. id_array = &n_rsrc->id;
  4897. }
  4898. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4899. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4900. /*
  4901. * Based on the resource size and count, correct the base and max
  4902. * resource values.
  4903. */
  4904. length = sizeof(struct lpfc_rsrc_blks);
  4905. switch (type) {
  4906. case LPFC_RSC_TYPE_FCOE_RPI:
  4907. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4908. sizeof(unsigned long),
  4909. GFP_KERNEL);
  4910. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4911. rc = -ENOMEM;
  4912. goto err_exit;
  4913. }
  4914. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4915. sizeof(uint16_t),
  4916. GFP_KERNEL);
  4917. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4918. kfree(phba->sli4_hba.rpi_bmask);
  4919. rc = -ENOMEM;
  4920. goto err_exit;
  4921. }
  4922. /*
  4923. * The next_rpi was initialized with the maximum available
  4924. * count but the port may allocate a smaller number. Catch
  4925. * that case and update the next_rpi.
  4926. */
  4927. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4928. /* Initialize local ptrs for common extent processing later. */
  4929. bmask = phba->sli4_hba.rpi_bmask;
  4930. ids = phba->sli4_hba.rpi_ids;
  4931. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4932. break;
  4933. case LPFC_RSC_TYPE_FCOE_VPI:
  4934. phba->vpi_bmask = kzalloc(longs *
  4935. sizeof(unsigned long),
  4936. GFP_KERNEL);
  4937. if (unlikely(!phba->vpi_bmask)) {
  4938. rc = -ENOMEM;
  4939. goto err_exit;
  4940. }
  4941. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4942. sizeof(uint16_t),
  4943. GFP_KERNEL);
  4944. if (unlikely(!phba->vpi_ids)) {
  4945. kfree(phba->vpi_bmask);
  4946. rc = -ENOMEM;
  4947. goto err_exit;
  4948. }
  4949. /* Initialize local ptrs for common extent processing later. */
  4950. bmask = phba->vpi_bmask;
  4951. ids = phba->vpi_ids;
  4952. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4953. break;
  4954. case LPFC_RSC_TYPE_FCOE_XRI:
  4955. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4956. sizeof(unsigned long),
  4957. GFP_KERNEL);
  4958. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4959. rc = -ENOMEM;
  4960. goto err_exit;
  4961. }
  4962. phba->sli4_hba.max_cfg_param.xri_used = 0;
  4963. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4964. sizeof(uint16_t),
  4965. GFP_KERNEL);
  4966. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4967. kfree(phba->sli4_hba.xri_bmask);
  4968. rc = -ENOMEM;
  4969. goto err_exit;
  4970. }
  4971. /* Initialize local ptrs for common extent processing later. */
  4972. bmask = phba->sli4_hba.xri_bmask;
  4973. ids = phba->sli4_hba.xri_ids;
  4974. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4975. break;
  4976. case LPFC_RSC_TYPE_FCOE_VFI:
  4977. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4978. sizeof(unsigned long),
  4979. GFP_KERNEL);
  4980. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4981. rc = -ENOMEM;
  4982. goto err_exit;
  4983. }
  4984. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4985. sizeof(uint16_t),
  4986. GFP_KERNEL);
  4987. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4988. kfree(phba->sli4_hba.vfi_bmask);
  4989. rc = -ENOMEM;
  4990. goto err_exit;
  4991. }
  4992. /* Initialize local ptrs for common extent processing later. */
  4993. bmask = phba->sli4_hba.vfi_bmask;
  4994. ids = phba->sli4_hba.vfi_ids;
  4995. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4996. break;
  4997. default:
  4998. /* Unsupported Opcode. Fail call. */
  4999. id_array = NULL;
  5000. bmask = NULL;
  5001. ids = NULL;
  5002. ext_blk_list = NULL;
  5003. goto err_exit;
  5004. }
  5005. /*
  5006. * Complete initializing the extent configuration with the
  5007. * allocated ids assigned to this function. The bitmask serves
  5008. * as an index into the array and manages the available ids. The
  5009. * array just stores the ids communicated to the port via the wqes.
  5010. */
  5011. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  5012. if ((i % 2) == 0)
  5013. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  5014. &id_array[k]);
  5015. else
  5016. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  5017. &id_array[k]);
  5018. rsrc_blks = kzalloc(length, GFP_KERNEL);
  5019. if (unlikely(!rsrc_blks)) {
  5020. rc = -ENOMEM;
  5021. kfree(bmask);
  5022. kfree(ids);
  5023. goto err_exit;
  5024. }
  5025. rsrc_blks->rsrc_start = rsrc_id;
  5026. rsrc_blks->rsrc_size = rsrc_size;
  5027. list_add_tail(&rsrc_blks->list, ext_blk_list);
  5028. rsrc_start = rsrc_id;
  5029. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  5030. phba->sli4_hba.scsi_xri_start = rsrc_start +
  5031. lpfc_sli4_get_els_iocb_cnt(phba);
  5032. while (rsrc_id < (rsrc_start + rsrc_size)) {
  5033. ids[j] = rsrc_id;
  5034. rsrc_id++;
  5035. j++;
  5036. }
  5037. /* Entire word processed. Get next word.*/
  5038. if ((i % 2) == 1)
  5039. k++;
  5040. }
  5041. err_exit:
  5042. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5043. return rc;
  5044. }
  5045. /**
  5046. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  5047. * @phba: Pointer to HBA context object.
  5048. * @type: the extent's type.
  5049. *
  5050. * This function deallocates all extents of a particular resource type.
  5051. * SLI4 does not allow for deallocating a particular extent range. It
  5052. * is the caller's responsibility to release all kernel memory resources.
  5053. **/
  5054. static int
  5055. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  5056. {
  5057. int rc;
  5058. uint32_t length, mbox_tmo = 0;
  5059. LPFC_MBOXQ_t *mbox;
  5060. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  5061. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  5062. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5063. if (!mbox)
  5064. return -ENOMEM;
  5065. /*
  5066. * This function sends an embedded mailbox because it only sends the
  5067. * the resource type. All extents of this type are released by the
  5068. * port.
  5069. */
  5070. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  5071. sizeof(struct lpfc_sli4_cfg_mhdr));
  5072. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5073. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  5074. length, LPFC_SLI4_MBX_EMBED);
  5075. /* Send an extents count of 0 - the dealloc doesn't use it. */
  5076. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  5077. LPFC_SLI4_MBX_EMBED);
  5078. if (unlikely(rc)) {
  5079. rc = -EIO;
  5080. goto out_free_mbox;
  5081. }
  5082. if (!phba->sli4_hba.intr_enable)
  5083. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5084. else {
  5085. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5086. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5087. }
  5088. if (unlikely(rc)) {
  5089. rc = -EIO;
  5090. goto out_free_mbox;
  5091. }
  5092. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  5093. if (bf_get(lpfc_mbox_hdr_status,
  5094. &dealloc_rsrc->header.cfg_shdr.response)) {
  5095. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5096. "2919 Failed to release resource extents "
  5097. "for type %d - Status 0x%x Add'l Status 0x%x. "
  5098. "Resource memory not released.\n",
  5099. type,
  5100. bf_get(lpfc_mbox_hdr_status,
  5101. &dealloc_rsrc->header.cfg_shdr.response),
  5102. bf_get(lpfc_mbox_hdr_add_status,
  5103. &dealloc_rsrc->header.cfg_shdr.response));
  5104. rc = -EIO;
  5105. goto out_free_mbox;
  5106. }
  5107. /* Release kernel memory resources for the specific type. */
  5108. switch (type) {
  5109. case LPFC_RSC_TYPE_FCOE_VPI:
  5110. kfree(phba->vpi_bmask);
  5111. kfree(phba->vpi_ids);
  5112. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5113. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5114. &phba->lpfc_vpi_blk_list, list) {
  5115. list_del_init(&rsrc_blk->list);
  5116. kfree(rsrc_blk);
  5117. }
  5118. phba->sli4_hba.max_cfg_param.vpi_used = 0;
  5119. break;
  5120. case LPFC_RSC_TYPE_FCOE_XRI:
  5121. kfree(phba->sli4_hba.xri_bmask);
  5122. kfree(phba->sli4_hba.xri_ids);
  5123. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5124. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  5125. list_del_init(&rsrc_blk->list);
  5126. kfree(rsrc_blk);
  5127. }
  5128. break;
  5129. case LPFC_RSC_TYPE_FCOE_VFI:
  5130. kfree(phba->sli4_hba.vfi_bmask);
  5131. kfree(phba->sli4_hba.vfi_ids);
  5132. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5133. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5134. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  5135. list_del_init(&rsrc_blk->list);
  5136. kfree(rsrc_blk);
  5137. }
  5138. break;
  5139. case LPFC_RSC_TYPE_FCOE_RPI:
  5140. /* RPI bitmask and physical id array are cleaned up earlier. */
  5141. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5142. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  5143. list_del_init(&rsrc_blk->list);
  5144. kfree(rsrc_blk);
  5145. }
  5146. break;
  5147. default:
  5148. break;
  5149. }
  5150. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5151. out_free_mbox:
  5152. mempool_free(mbox, phba->mbox_mem_pool);
  5153. return rc;
  5154. }
  5155. static void
  5156. lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
  5157. uint32_t feature)
  5158. {
  5159. uint32_t len;
  5160. len = sizeof(struct lpfc_mbx_set_feature) -
  5161. sizeof(struct lpfc_sli4_cfg_mhdr);
  5162. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5163. LPFC_MBOX_OPCODE_SET_FEATURES, len,
  5164. LPFC_SLI4_MBX_EMBED);
  5165. switch (feature) {
  5166. case LPFC_SET_UE_RECOVERY:
  5167. bf_set(lpfc_mbx_set_feature_UER,
  5168. &mbox->u.mqe.un.set_feature, 1);
  5169. mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
  5170. mbox->u.mqe.un.set_feature.param_len = 8;
  5171. break;
  5172. case LPFC_SET_MDS_DIAGS:
  5173. bf_set(lpfc_mbx_set_feature_mds,
  5174. &mbox->u.mqe.un.set_feature, 1);
  5175. bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
  5176. &mbox->u.mqe.un.set_feature, 0);
  5177. mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
  5178. mbox->u.mqe.un.set_feature.param_len = 8;
  5179. break;
  5180. }
  5181. return;
  5182. }
  5183. /**
  5184. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  5185. * @phba: Pointer to HBA context object.
  5186. *
  5187. * This function allocates all SLI4 resource identifiers.
  5188. **/
  5189. int
  5190. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  5191. {
  5192. int i, rc, error = 0;
  5193. uint16_t count, base;
  5194. unsigned long longs;
  5195. if (!phba->sli4_hba.rpi_hdrs_in_use)
  5196. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  5197. if (phba->sli4_hba.extents_in_use) {
  5198. /*
  5199. * The port supports resource extents. The XRI, VPI, VFI, RPI
  5200. * resource extent count must be read and allocated before
  5201. * provisioning the resource id arrays.
  5202. */
  5203. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5204. LPFC_IDX_RSRC_RDY) {
  5205. /*
  5206. * Extent-based resources are set - the driver could
  5207. * be in a port reset. Figure out if any corrective
  5208. * actions need to be taken.
  5209. */
  5210. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5211. LPFC_RSC_TYPE_FCOE_VFI);
  5212. if (rc != 0)
  5213. error++;
  5214. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5215. LPFC_RSC_TYPE_FCOE_VPI);
  5216. if (rc != 0)
  5217. error++;
  5218. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5219. LPFC_RSC_TYPE_FCOE_XRI);
  5220. if (rc != 0)
  5221. error++;
  5222. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5223. LPFC_RSC_TYPE_FCOE_RPI);
  5224. if (rc != 0)
  5225. error++;
  5226. /*
  5227. * It's possible that the number of resources
  5228. * provided to this port instance changed between
  5229. * resets. Detect this condition and reallocate
  5230. * resources. Otherwise, there is no action.
  5231. */
  5232. if (error) {
  5233. lpfc_printf_log(phba, KERN_INFO,
  5234. LOG_MBOX | LOG_INIT,
  5235. "2931 Detected extent resource "
  5236. "change. Reallocating all "
  5237. "extents.\n");
  5238. rc = lpfc_sli4_dealloc_extent(phba,
  5239. LPFC_RSC_TYPE_FCOE_VFI);
  5240. rc = lpfc_sli4_dealloc_extent(phba,
  5241. LPFC_RSC_TYPE_FCOE_VPI);
  5242. rc = lpfc_sli4_dealloc_extent(phba,
  5243. LPFC_RSC_TYPE_FCOE_XRI);
  5244. rc = lpfc_sli4_dealloc_extent(phba,
  5245. LPFC_RSC_TYPE_FCOE_RPI);
  5246. } else
  5247. return 0;
  5248. }
  5249. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5250. if (unlikely(rc))
  5251. goto err_exit;
  5252. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5253. if (unlikely(rc))
  5254. goto err_exit;
  5255. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5256. if (unlikely(rc))
  5257. goto err_exit;
  5258. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5259. if (unlikely(rc))
  5260. goto err_exit;
  5261. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5262. LPFC_IDX_RSRC_RDY);
  5263. return rc;
  5264. } else {
  5265. /*
  5266. * The port does not support resource extents. The XRI, VPI,
  5267. * VFI, RPI resource ids were determined from READ_CONFIG.
  5268. * Just allocate the bitmasks and provision the resource id
  5269. * arrays. If a port reset is active, the resources don't
  5270. * need any action - just exit.
  5271. */
  5272. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5273. LPFC_IDX_RSRC_RDY) {
  5274. lpfc_sli4_dealloc_resource_identifiers(phba);
  5275. lpfc_sli4_remove_rpis(phba);
  5276. }
  5277. /* RPIs. */
  5278. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5279. if (count <= 0) {
  5280. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5281. "3279 Invalid provisioning of "
  5282. "rpi:%d\n", count);
  5283. rc = -EINVAL;
  5284. goto err_exit;
  5285. }
  5286. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5287. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5288. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5289. sizeof(unsigned long),
  5290. GFP_KERNEL);
  5291. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5292. rc = -ENOMEM;
  5293. goto err_exit;
  5294. }
  5295. phba->sli4_hba.rpi_ids = kzalloc(count *
  5296. sizeof(uint16_t),
  5297. GFP_KERNEL);
  5298. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5299. rc = -ENOMEM;
  5300. goto free_rpi_bmask;
  5301. }
  5302. for (i = 0; i < count; i++)
  5303. phba->sli4_hba.rpi_ids[i] = base + i;
  5304. /* VPIs. */
  5305. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5306. if (count <= 0) {
  5307. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5308. "3280 Invalid provisioning of "
  5309. "vpi:%d\n", count);
  5310. rc = -EINVAL;
  5311. goto free_rpi_ids;
  5312. }
  5313. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5314. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5315. phba->vpi_bmask = kzalloc(longs *
  5316. sizeof(unsigned long),
  5317. GFP_KERNEL);
  5318. if (unlikely(!phba->vpi_bmask)) {
  5319. rc = -ENOMEM;
  5320. goto free_rpi_ids;
  5321. }
  5322. phba->vpi_ids = kzalloc(count *
  5323. sizeof(uint16_t),
  5324. GFP_KERNEL);
  5325. if (unlikely(!phba->vpi_ids)) {
  5326. rc = -ENOMEM;
  5327. goto free_vpi_bmask;
  5328. }
  5329. for (i = 0; i < count; i++)
  5330. phba->vpi_ids[i] = base + i;
  5331. /* XRIs. */
  5332. count = phba->sli4_hba.max_cfg_param.max_xri;
  5333. if (count <= 0) {
  5334. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5335. "3281 Invalid provisioning of "
  5336. "xri:%d\n", count);
  5337. rc = -EINVAL;
  5338. goto free_vpi_ids;
  5339. }
  5340. base = phba->sli4_hba.max_cfg_param.xri_base;
  5341. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5342. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5343. sizeof(unsigned long),
  5344. GFP_KERNEL);
  5345. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5346. rc = -ENOMEM;
  5347. goto free_vpi_ids;
  5348. }
  5349. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5350. phba->sli4_hba.xri_ids = kzalloc(count *
  5351. sizeof(uint16_t),
  5352. GFP_KERNEL);
  5353. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5354. rc = -ENOMEM;
  5355. goto free_xri_bmask;
  5356. }
  5357. for (i = 0; i < count; i++)
  5358. phba->sli4_hba.xri_ids[i] = base + i;
  5359. /* VFIs. */
  5360. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5361. if (count <= 0) {
  5362. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5363. "3282 Invalid provisioning of "
  5364. "vfi:%d\n", count);
  5365. rc = -EINVAL;
  5366. goto free_xri_ids;
  5367. }
  5368. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5369. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5370. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5371. sizeof(unsigned long),
  5372. GFP_KERNEL);
  5373. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5374. rc = -ENOMEM;
  5375. goto free_xri_ids;
  5376. }
  5377. phba->sli4_hba.vfi_ids = kzalloc(count *
  5378. sizeof(uint16_t),
  5379. GFP_KERNEL);
  5380. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5381. rc = -ENOMEM;
  5382. goto free_vfi_bmask;
  5383. }
  5384. for (i = 0; i < count; i++)
  5385. phba->sli4_hba.vfi_ids[i] = base + i;
  5386. /*
  5387. * Mark all resources ready. An HBA reset doesn't need
  5388. * to reset the initialization.
  5389. */
  5390. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5391. LPFC_IDX_RSRC_RDY);
  5392. return 0;
  5393. }
  5394. free_vfi_bmask:
  5395. kfree(phba->sli4_hba.vfi_bmask);
  5396. free_xri_ids:
  5397. kfree(phba->sli4_hba.xri_ids);
  5398. free_xri_bmask:
  5399. kfree(phba->sli4_hba.xri_bmask);
  5400. free_vpi_ids:
  5401. kfree(phba->vpi_ids);
  5402. free_vpi_bmask:
  5403. kfree(phba->vpi_bmask);
  5404. free_rpi_ids:
  5405. kfree(phba->sli4_hba.rpi_ids);
  5406. free_rpi_bmask:
  5407. kfree(phba->sli4_hba.rpi_bmask);
  5408. err_exit:
  5409. return rc;
  5410. }
  5411. /**
  5412. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5413. * @phba: Pointer to HBA context object.
  5414. *
  5415. * This function allocates the number of elements for the specified
  5416. * resource type.
  5417. **/
  5418. int
  5419. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5420. {
  5421. if (phba->sli4_hba.extents_in_use) {
  5422. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5423. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5424. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5425. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5426. } else {
  5427. kfree(phba->vpi_bmask);
  5428. phba->sli4_hba.max_cfg_param.vpi_used = 0;
  5429. kfree(phba->vpi_ids);
  5430. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5431. kfree(phba->sli4_hba.xri_bmask);
  5432. kfree(phba->sli4_hba.xri_ids);
  5433. kfree(phba->sli4_hba.vfi_bmask);
  5434. kfree(phba->sli4_hba.vfi_ids);
  5435. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5436. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5437. }
  5438. return 0;
  5439. }
  5440. /**
  5441. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5442. * @phba: Pointer to HBA context object.
  5443. * @type: The resource extent type.
  5444. * @extnt_count: buffer to hold port extent count response
  5445. * @extnt_size: buffer to hold port extent size response.
  5446. *
  5447. * This function calls the port to read the host allocated extents
  5448. * for a particular type.
  5449. **/
  5450. int
  5451. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5452. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5453. {
  5454. bool emb;
  5455. int rc = 0;
  5456. uint16_t curr_blks = 0;
  5457. uint32_t req_len, emb_len;
  5458. uint32_t alloc_len, mbox_tmo;
  5459. struct list_head *blk_list_head;
  5460. struct lpfc_rsrc_blks *rsrc_blk;
  5461. LPFC_MBOXQ_t *mbox;
  5462. void *virtaddr = NULL;
  5463. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5464. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5465. union lpfc_sli4_cfg_shdr *shdr;
  5466. switch (type) {
  5467. case LPFC_RSC_TYPE_FCOE_VPI:
  5468. blk_list_head = &phba->lpfc_vpi_blk_list;
  5469. break;
  5470. case LPFC_RSC_TYPE_FCOE_XRI:
  5471. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5472. break;
  5473. case LPFC_RSC_TYPE_FCOE_VFI:
  5474. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5475. break;
  5476. case LPFC_RSC_TYPE_FCOE_RPI:
  5477. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5478. break;
  5479. default:
  5480. return -EIO;
  5481. }
  5482. /* Count the number of extents currently allocatd for this type. */
  5483. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5484. if (curr_blks == 0) {
  5485. /*
  5486. * The GET_ALLOCATED mailbox does not return the size,
  5487. * just the count. The size should be just the size
  5488. * stored in the current allocated block and all sizes
  5489. * for an extent type are the same so set the return
  5490. * value now.
  5491. */
  5492. *extnt_size = rsrc_blk->rsrc_size;
  5493. }
  5494. curr_blks++;
  5495. }
  5496. /*
  5497. * Calculate the size of an embedded mailbox. The uint32_t
  5498. * accounts for extents-specific word.
  5499. */
  5500. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5501. sizeof(uint32_t);
  5502. /*
  5503. * Presume the allocation and response will fit into an embedded
  5504. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5505. */
  5506. emb = LPFC_SLI4_MBX_EMBED;
  5507. req_len = emb_len;
  5508. if (req_len > emb_len) {
  5509. req_len = curr_blks * sizeof(uint16_t) +
  5510. sizeof(union lpfc_sli4_cfg_shdr) +
  5511. sizeof(uint32_t);
  5512. emb = LPFC_SLI4_MBX_NEMBED;
  5513. }
  5514. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5515. if (!mbox)
  5516. return -ENOMEM;
  5517. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5518. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5519. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5520. req_len, emb);
  5521. if (alloc_len < req_len) {
  5522. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5523. "2983 Allocated DMA memory size (x%x) is "
  5524. "less than the requested DMA memory "
  5525. "size (x%x)\n", alloc_len, req_len);
  5526. rc = -ENOMEM;
  5527. goto err_exit;
  5528. }
  5529. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5530. if (unlikely(rc)) {
  5531. rc = -EIO;
  5532. goto err_exit;
  5533. }
  5534. if (!phba->sli4_hba.intr_enable)
  5535. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5536. else {
  5537. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5538. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5539. }
  5540. if (unlikely(rc)) {
  5541. rc = -EIO;
  5542. goto err_exit;
  5543. }
  5544. /*
  5545. * Figure out where the response is located. Then get local pointers
  5546. * to the response data. The port does not guarantee to respond to
  5547. * all extents counts request so update the local variable with the
  5548. * allocated count from the port.
  5549. */
  5550. if (emb == LPFC_SLI4_MBX_EMBED) {
  5551. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5552. shdr = &rsrc_ext->header.cfg_shdr;
  5553. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5554. } else {
  5555. virtaddr = mbox->sge_array->addr[0];
  5556. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5557. shdr = &n_rsrc->cfg_shdr;
  5558. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5559. }
  5560. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5561. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5562. "2984 Failed to read allocated resources "
  5563. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5564. type,
  5565. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5566. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5567. rc = -EIO;
  5568. goto err_exit;
  5569. }
  5570. err_exit:
  5571. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5572. return rc;
  5573. }
  5574. /**
  5575. * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
  5576. * @phba: pointer to lpfc hba data structure.
  5577. *
  5578. * This routine walks the list of els buffers that have been allocated and
  5579. * repost them to the port by using SGL block post. This is needed after a
  5580. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5581. * of els buffer sgls which contains contiguous xris and uses the non-embedded
  5582. * SGL block post mailbox commands to post them to the port. For single els
  5583. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5584. * mailbox command for posting.
  5585. *
  5586. * Returns: 0 = success, non-zero failure.
  5587. **/
  5588. static int
  5589. lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
  5590. {
  5591. struct lpfc_sglq *sglq_entry = NULL;
  5592. struct lpfc_sglq *sglq_entry_next = NULL;
  5593. struct lpfc_sglq *sglq_entry_first = NULL;
  5594. int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
  5595. int last_xritag = NO_XRI;
  5596. struct lpfc_sli_ring *pring;
  5597. LIST_HEAD(prep_sgl_list);
  5598. LIST_HEAD(blck_sgl_list);
  5599. LIST_HEAD(allc_sgl_list);
  5600. LIST_HEAD(post_sgl_list);
  5601. LIST_HEAD(free_sgl_list);
  5602. pring = &phba->sli.ring[LPFC_ELS_RING];
  5603. spin_lock_irq(&phba->hbalock);
  5604. spin_lock(&pring->ring_lock);
  5605. list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
  5606. spin_unlock(&pring->ring_lock);
  5607. spin_unlock_irq(&phba->hbalock);
  5608. total_cnt = phba->sli4_hba.els_xri_cnt;
  5609. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  5610. &allc_sgl_list, list) {
  5611. list_del_init(&sglq_entry->list);
  5612. block_cnt++;
  5613. if ((last_xritag != NO_XRI) &&
  5614. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  5615. /* a hole in xri block, form a sgl posting block */
  5616. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  5617. post_cnt = block_cnt - 1;
  5618. /* prepare list for next posting block */
  5619. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5620. block_cnt = 1;
  5621. } else {
  5622. /* prepare list for next posting block */
  5623. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5624. /* enough sgls for non-embed sgl mbox command */
  5625. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  5626. list_splice_init(&prep_sgl_list,
  5627. &blck_sgl_list);
  5628. post_cnt = block_cnt;
  5629. block_cnt = 0;
  5630. }
  5631. }
  5632. num_posted++;
  5633. /* keep track of last sgl's xritag */
  5634. last_xritag = sglq_entry->sli4_xritag;
  5635. /* end of repost sgl list condition for els buffers */
  5636. if (num_posted == phba->sli4_hba.els_xri_cnt) {
  5637. if (post_cnt == 0) {
  5638. list_splice_init(&prep_sgl_list,
  5639. &blck_sgl_list);
  5640. post_cnt = block_cnt;
  5641. } else if (block_cnt == 1) {
  5642. status = lpfc_sli4_post_sgl(phba,
  5643. sglq_entry->phys, 0,
  5644. sglq_entry->sli4_xritag);
  5645. if (!status) {
  5646. /* successful, put sgl to posted list */
  5647. list_add_tail(&sglq_entry->list,
  5648. &post_sgl_list);
  5649. } else {
  5650. /* Failure, put sgl to free list */
  5651. lpfc_printf_log(phba, KERN_WARNING,
  5652. LOG_SLI,
  5653. "3159 Failed to post els "
  5654. "sgl, xritag:x%x\n",
  5655. sglq_entry->sli4_xritag);
  5656. list_add_tail(&sglq_entry->list,
  5657. &free_sgl_list);
  5658. total_cnt--;
  5659. }
  5660. }
  5661. }
  5662. /* continue until a nembed page worth of sgls */
  5663. if (post_cnt == 0)
  5664. continue;
  5665. /* post the els buffer list sgls as a block */
  5666. status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
  5667. post_cnt);
  5668. if (!status) {
  5669. /* success, put sgl list to posted sgl list */
  5670. list_splice_init(&blck_sgl_list, &post_sgl_list);
  5671. } else {
  5672. /* Failure, put sgl list to free sgl list */
  5673. sglq_entry_first = list_first_entry(&blck_sgl_list,
  5674. struct lpfc_sglq,
  5675. list);
  5676. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5677. "3160 Failed to post els sgl-list, "
  5678. "xritag:x%x-x%x\n",
  5679. sglq_entry_first->sli4_xritag,
  5680. (sglq_entry_first->sli4_xritag +
  5681. post_cnt - 1));
  5682. list_splice_init(&blck_sgl_list, &free_sgl_list);
  5683. total_cnt -= post_cnt;
  5684. }
  5685. /* don't reset xirtag due to hole in xri block */
  5686. if (block_cnt == 0)
  5687. last_xritag = NO_XRI;
  5688. /* reset els sgl post count for next round of posting */
  5689. post_cnt = 0;
  5690. }
  5691. /* update the number of XRIs posted for ELS */
  5692. phba->sli4_hba.els_xri_cnt = total_cnt;
  5693. /* free the els sgls failed to post */
  5694. lpfc_free_sgl_list(phba, &free_sgl_list);
  5695. /* push els sgls posted to the availble list */
  5696. if (!list_empty(&post_sgl_list)) {
  5697. spin_lock_irq(&phba->hbalock);
  5698. spin_lock(&pring->ring_lock);
  5699. list_splice_init(&post_sgl_list,
  5700. &phba->sli4_hba.lpfc_sgl_list);
  5701. spin_unlock(&pring->ring_lock);
  5702. spin_unlock_irq(&phba->hbalock);
  5703. } else {
  5704. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5705. "3161 Failure to post els sgl to port.\n");
  5706. return -EIO;
  5707. }
  5708. return 0;
  5709. }
  5710. /**
  5711. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5712. * @phba: Pointer to HBA context object.
  5713. *
  5714. * This function is the main SLI4 device intialization PCI function. This
  5715. * function is called by the HBA intialization code, HBA reset code and
  5716. * HBA error attention handler code. Caller is not required to hold any
  5717. * locks.
  5718. **/
  5719. int
  5720. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5721. {
  5722. int rc;
  5723. LPFC_MBOXQ_t *mboxq;
  5724. struct lpfc_mqe *mqe;
  5725. uint8_t *vpd;
  5726. uint32_t vpd_size;
  5727. uint32_t ftr_rsp = 0;
  5728. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5729. struct lpfc_vport *vport = phba->pport;
  5730. struct lpfc_dmabuf *mp;
  5731. /* Perform a PCI function reset to start from clean */
  5732. rc = lpfc_pci_function_reset(phba);
  5733. if (unlikely(rc))
  5734. return -ENODEV;
  5735. /* Check the HBA Host Status Register for readyness */
  5736. rc = lpfc_sli4_post_status_check(phba);
  5737. if (unlikely(rc))
  5738. return -ENODEV;
  5739. else {
  5740. spin_lock_irq(&phba->hbalock);
  5741. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5742. spin_unlock_irq(&phba->hbalock);
  5743. }
  5744. /*
  5745. * Allocate a single mailbox container for initializing the
  5746. * port.
  5747. */
  5748. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5749. if (!mboxq)
  5750. return -ENOMEM;
  5751. /* Issue READ_REV to collect vpd and FW information. */
  5752. vpd_size = SLI4_PAGE_SIZE;
  5753. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5754. if (!vpd) {
  5755. rc = -ENOMEM;
  5756. goto out_free_mbox;
  5757. }
  5758. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5759. if (unlikely(rc)) {
  5760. kfree(vpd);
  5761. goto out_free_mbox;
  5762. }
  5763. mqe = &mboxq->u.mqe;
  5764. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5765. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
  5766. phba->hba_flag |= HBA_FCOE_MODE;
  5767. phba->fcp_embed_io = 0; /* SLI4 FC support only */
  5768. } else {
  5769. phba->hba_flag &= ~HBA_FCOE_MODE;
  5770. }
  5771. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5772. LPFC_DCBX_CEE_MODE)
  5773. phba->hba_flag |= HBA_FIP_SUPPORT;
  5774. else
  5775. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5776. phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
  5777. if (phba->sli_rev != LPFC_SLI_REV4) {
  5778. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5779. "0376 READ_REV Error. SLI Level %d "
  5780. "FCoE enabled %d\n",
  5781. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5782. rc = -EIO;
  5783. kfree(vpd);
  5784. goto out_free_mbox;
  5785. }
  5786. /*
  5787. * Continue initialization with default values even if driver failed
  5788. * to read FCoE param config regions, only read parameters if the
  5789. * board is FCoE
  5790. */
  5791. if (phba->hba_flag & HBA_FCOE_MODE &&
  5792. lpfc_sli4_read_fcoe_params(phba))
  5793. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5794. "2570 Failed to read FCoE parameters\n");
  5795. /*
  5796. * Retrieve sli4 device physical port name, failure of doing it
  5797. * is considered as non-fatal.
  5798. */
  5799. rc = lpfc_sli4_retrieve_pport_name(phba);
  5800. if (!rc)
  5801. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5802. "3080 Successful retrieving SLI4 device "
  5803. "physical port name: %s.\n", phba->Port);
  5804. /*
  5805. * Evaluate the read rev and vpd data. Populate the driver
  5806. * state with the results. If this routine fails, the failure
  5807. * is not fatal as the driver will use generic values.
  5808. */
  5809. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5810. if (unlikely(!rc)) {
  5811. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5812. "0377 Error %d parsing vpd. "
  5813. "Using defaults.\n", rc);
  5814. rc = 0;
  5815. }
  5816. kfree(vpd);
  5817. /* Save information as VPD data */
  5818. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5819. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5820. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5821. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5822. &mqe->un.read_rev);
  5823. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5824. &mqe->un.read_rev);
  5825. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5826. &mqe->un.read_rev);
  5827. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5828. &mqe->un.read_rev);
  5829. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5830. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5831. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5832. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5833. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5834. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5835. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5836. "(%d):0380 READ_REV Status x%x "
  5837. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5838. mboxq->vport ? mboxq->vport->vpi : 0,
  5839. bf_get(lpfc_mqe_status, mqe),
  5840. phba->vpd.rev.opFwName,
  5841. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5842. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5843. /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
  5844. rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
  5845. if (phba->pport->cfg_lun_queue_depth > rc) {
  5846. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  5847. "3362 LUN queue depth changed from %d to %d\n",
  5848. phba->pport->cfg_lun_queue_depth, rc);
  5849. phba->pport->cfg_lun_queue_depth = rc;
  5850. }
  5851. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
  5852. LPFC_SLI_INTF_IF_TYPE_0) {
  5853. lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
  5854. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5855. if (rc == MBX_SUCCESS) {
  5856. phba->hba_flag |= HBA_RECOVERABLE_UE;
  5857. /* Set 1Sec interval to detect UE */
  5858. phba->eratt_poll_interval = 1;
  5859. phba->sli4_hba.ue_to_sr = bf_get(
  5860. lpfc_mbx_set_feature_UESR,
  5861. &mboxq->u.mqe.un.set_feature);
  5862. phba->sli4_hba.ue_to_rp = bf_get(
  5863. lpfc_mbx_set_feature_UERP,
  5864. &mboxq->u.mqe.un.set_feature);
  5865. }
  5866. }
  5867. if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
  5868. /* Enable MDS Diagnostics only if the SLI Port supports it */
  5869. lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
  5870. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5871. if (rc != MBX_SUCCESS)
  5872. phba->mds_diags_support = 0;
  5873. }
  5874. /*
  5875. * Discover the port's supported feature set and match it against the
  5876. * hosts requests.
  5877. */
  5878. lpfc_request_features(phba, mboxq);
  5879. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5880. if (unlikely(rc)) {
  5881. rc = -EIO;
  5882. goto out_free_mbox;
  5883. }
  5884. /*
  5885. * The port must support FCP initiator mode as this is the
  5886. * only mode running in the host.
  5887. */
  5888. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5889. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5890. "0378 No support for fcpi mode.\n");
  5891. ftr_rsp++;
  5892. }
  5893. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5894. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5895. else
  5896. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5897. /*
  5898. * If the port cannot support the host's requested features
  5899. * then turn off the global config parameters to disable the
  5900. * feature in the driver. This is not a fatal error.
  5901. */
  5902. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5903. if (phba->cfg_enable_bg) {
  5904. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5905. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5906. else
  5907. ftr_rsp++;
  5908. }
  5909. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5910. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5911. ftr_rsp++;
  5912. if (ftr_rsp) {
  5913. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5914. "0379 Feature Mismatch Data: x%08x %08x "
  5915. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5916. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5917. phba->cfg_enable_npiv, phba->max_vpi);
  5918. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5919. phba->cfg_enable_bg = 0;
  5920. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5921. phba->cfg_enable_npiv = 0;
  5922. }
  5923. /* These SLI3 features are assumed in SLI4 */
  5924. spin_lock_irq(&phba->hbalock);
  5925. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5926. spin_unlock_irq(&phba->hbalock);
  5927. /*
  5928. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5929. * calls depends on these resources to complete port setup.
  5930. */
  5931. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5932. if (rc) {
  5933. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5934. "2920 Failed to alloc Resource IDs "
  5935. "rc = x%x\n", rc);
  5936. goto out_free_mbox;
  5937. }
  5938. /* Read the port's service parameters. */
  5939. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5940. if (rc) {
  5941. phba->link_state = LPFC_HBA_ERROR;
  5942. rc = -ENOMEM;
  5943. goto out_free_mbox;
  5944. }
  5945. mboxq->vport = vport;
  5946. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5947. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5948. if (rc == MBX_SUCCESS) {
  5949. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5950. rc = 0;
  5951. }
  5952. /*
  5953. * This memory was allocated by the lpfc_read_sparam routine. Release
  5954. * it to the mbuf pool.
  5955. */
  5956. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5957. kfree(mp);
  5958. mboxq->context1 = NULL;
  5959. if (unlikely(rc)) {
  5960. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5961. "0382 READ_SPARAM command failed "
  5962. "status %d, mbxStatus x%x\n",
  5963. rc, bf_get(lpfc_mqe_status, mqe));
  5964. phba->link_state = LPFC_HBA_ERROR;
  5965. rc = -EIO;
  5966. goto out_free_mbox;
  5967. }
  5968. lpfc_update_vport_wwn(vport);
  5969. /* Update the fc_host data structures with new wwn. */
  5970. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5971. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5972. /* update host els and scsi xri-sgl sizes and mappings */
  5973. rc = lpfc_sli4_xri_sgl_update(phba);
  5974. if (unlikely(rc)) {
  5975. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5976. "1400 Failed to update xri-sgl size and "
  5977. "mapping: %d\n", rc);
  5978. goto out_free_mbox;
  5979. }
  5980. /* register the els sgl pool to the port */
  5981. rc = lpfc_sli4_repost_els_sgl_list(phba);
  5982. if (unlikely(rc)) {
  5983. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5984. "0582 Error %d during els sgl post "
  5985. "operation\n", rc);
  5986. rc = -ENODEV;
  5987. goto out_free_mbox;
  5988. }
  5989. /* register the allocated scsi sgl pool to the port */
  5990. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5991. if (unlikely(rc)) {
  5992. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5993. "0383 Error %d during scsi sgl post "
  5994. "operation\n", rc);
  5995. /* Some Scsi buffers were moved to the abort scsi list */
  5996. /* A pci function reset will repost them */
  5997. rc = -ENODEV;
  5998. goto out_free_mbox;
  5999. }
  6000. /* Post the rpi header region to the device. */
  6001. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  6002. if (unlikely(rc)) {
  6003. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6004. "0393 Error %d during rpi post operation\n",
  6005. rc);
  6006. rc = -ENODEV;
  6007. goto out_free_mbox;
  6008. }
  6009. lpfc_sli4_node_prep(phba);
  6010. /* Create all the SLI4 queues */
  6011. rc = lpfc_sli4_queue_create(phba);
  6012. if (rc) {
  6013. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6014. "3089 Failed to allocate queues\n");
  6015. rc = -ENODEV;
  6016. goto out_stop_timers;
  6017. }
  6018. /* Set up all the queues to the device */
  6019. rc = lpfc_sli4_queue_setup(phba);
  6020. if (unlikely(rc)) {
  6021. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6022. "0381 Error %d during queue setup.\n ", rc);
  6023. goto out_destroy_queue;
  6024. }
  6025. /* Arm the CQs and then EQs on device */
  6026. lpfc_sli4_arm_cqeq_intr(phba);
  6027. /* Indicate device interrupt mode */
  6028. phba->sli4_hba.intr_enable = 1;
  6029. /* Allow asynchronous mailbox command to go through */
  6030. spin_lock_irq(&phba->hbalock);
  6031. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6032. spin_unlock_irq(&phba->hbalock);
  6033. /* Post receive buffers to the device */
  6034. lpfc_sli4_rb_setup(phba);
  6035. /* Reset HBA FCF states after HBA reset */
  6036. phba->fcf.fcf_flag = 0;
  6037. phba->fcf.current_rec.flag = 0;
  6038. /* Start the ELS watchdog timer */
  6039. mod_timer(&vport->els_tmofunc,
  6040. jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
  6041. /* Start heart beat timer */
  6042. mod_timer(&phba->hb_tmofunc,
  6043. jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
  6044. phba->hb_outstanding = 0;
  6045. phba->last_completion_time = jiffies;
  6046. /* Start error attention (ERATT) polling timer */
  6047. mod_timer(&phba->eratt_poll,
  6048. jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
  6049. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  6050. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  6051. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  6052. if (!rc) {
  6053. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  6054. "2829 This device supports "
  6055. "Advanced Error Reporting (AER)\n");
  6056. spin_lock_irq(&phba->hbalock);
  6057. phba->hba_flag |= HBA_AER_ENABLED;
  6058. spin_unlock_irq(&phba->hbalock);
  6059. } else {
  6060. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  6061. "2830 This device does not support "
  6062. "Advanced Error Reporting (AER)\n");
  6063. phba->cfg_aer_support = 0;
  6064. }
  6065. rc = 0;
  6066. }
  6067. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  6068. /*
  6069. * The FC Port needs to register FCFI (index 0)
  6070. */
  6071. lpfc_reg_fcfi(phba, mboxq);
  6072. mboxq->vport = phba->pport;
  6073. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6074. if (rc != MBX_SUCCESS)
  6075. goto out_unset_queue;
  6076. rc = 0;
  6077. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  6078. &mboxq->u.mqe.un.reg_fcfi);
  6079. /* Check if the port is configured to be disabled */
  6080. lpfc_sli_read_link_ste(phba);
  6081. }
  6082. /*
  6083. * The port is ready, set the host's link state to LINK_DOWN
  6084. * in preparation for link interrupts.
  6085. */
  6086. spin_lock_irq(&phba->hbalock);
  6087. phba->link_state = LPFC_LINK_DOWN;
  6088. spin_unlock_irq(&phba->hbalock);
  6089. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  6090. (phba->hba_flag & LINK_DISABLED)) {
  6091. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  6092. "3103 Adapter Link is disabled.\n");
  6093. lpfc_down_link(phba, mboxq);
  6094. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6095. if (rc != MBX_SUCCESS) {
  6096. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  6097. "3104 Adapter failed to issue "
  6098. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  6099. goto out_unset_queue;
  6100. }
  6101. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  6102. /* don't perform init_link on SLI4 FC port loopback test */
  6103. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  6104. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  6105. if (rc)
  6106. goto out_unset_queue;
  6107. }
  6108. }
  6109. mempool_free(mboxq, phba->mbox_mem_pool);
  6110. return rc;
  6111. out_unset_queue:
  6112. /* Unset all the queues set up in this routine when error out */
  6113. lpfc_sli4_queue_unset(phba);
  6114. out_destroy_queue:
  6115. lpfc_sli4_queue_destroy(phba);
  6116. out_stop_timers:
  6117. lpfc_stop_hba_timers(phba);
  6118. out_free_mbox:
  6119. mempool_free(mboxq, phba->mbox_mem_pool);
  6120. return rc;
  6121. }
  6122. /**
  6123. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  6124. * @ptr: context object - pointer to hba structure.
  6125. *
  6126. * This is the callback function for mailbox timer. The mailbox
  6127. * timer is armed when a new mailbox command is issued and the timer
  6128. * is deleted when the mailbox complete. The function is called by
  6129. * the kernel timer code when a mailbox does not complete within
  6130. * expected time. This function wakes up the worker thread to
  6131. * process the mailbox timeout and returns. All the processing is
  6132. * done by the worker thread function lpfc_mbox_timeout_handler.
  6133. **/
  6134. void
  6135. lpfc_mbox_timeout(unsigned long ptr)
  6136. {
  6137. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  6138. unsigned long iflag;
  6139. uint32_t tmo_posted;
  6140. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  6141. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  6142. if (!tmo_posted)
  6143. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  6144. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  6145. if (!tmo_posted)
  6146. lpfc_worker_wake_up(phba);
  6147. return;
  6148. }
  6149. /**
  6150. * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
  6151. * are pending
  6152. * @phba: Pointer to HBA context object.
  6153. *
  6154. * This function checks if any mailbox completions are present on the mailbox
  6155. * completion queue.
  6156. **/
  6157. static bool
  6158. lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
  6159. {
  6160. uint32_t idx;
  6161. struct lpfc_queue *mcq;
  6162. struct lpfc_mcqe *mcqe;
  6163. bool pending_completions = false;
  6164. if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
  6165. return false;
  6166. /* Check for completions on mailbox completion queue */
  6167. mcq = phba->sli4_hba.mbx_cq;
  6168. idx = mcq->hba_index;
  6169. while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
  6170. mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
  6171. if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
  6172. (!bf_get_le32(lpfc_trailer_async, mcqe))) {
  6173. pending_completions = true;
  6174. break;
  6175. }
  6176. idx = (idx + 1) % mcq->entry_count;
  6177. if (mcq->hba_index == idx)
  6178. break;
  6179. }
  6180. return pending_completions;
  6181. }
  6182. /**
  6183. * lpfc_sli4_process_missed_mbox_completions - process mbox completions
  6184. * that were missed.
  6185. * @phba: Pointer to HBA context object.
  6186. *
  6187. * For sli4, it is possible to miss an interrupt. As such mbox completions
  6188. * maybe missed causing erroneous mailbox timeouts to occur. This function
  6189. * checks to see if mbox completions are on the mailbox completion queue
  6190. * and will process all the completions associated with the eq for the
  6191. * mailbox completion queue.
  6192. **/
  6193. bool
  6194. lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
  6195. {
  6196. uint32_t eqidx;
  6197. struct lpfc_queue *fpeq = NULL;
  6198. struct lpfc_eqe *eqe;
  6199. bool mbox_pending;
  6200. if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
  6201. return false;
  6202. /* Find the eq associated with the mcq */
  6203. if (phba->sli4_hba.hba_eq)
  6204. for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
  6205. if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
  6206. phba->sli4_hba.mbx_cq->assoc_qid) {
  6207. fpeq = phba->sli4_hba.hba_eq[eqidx];
  6208. break;
  6209. }
  6210. if (!fpeq)
  6211. return false;
  6212. /* Turn off interrupts from this EQ */
  6213. lpfc_sli4_eq_clr_intr(fpeq);
  6214. /* Check to see if a mbox completion is pending */
  6215. mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
  6216. /*
  6217. * If a mbox completion is pending, process all the events on EQ
  6218. * associated with the mbox completion queue (this could include
  6219. * mailbox commands, async events, els commands, receive queue data
  6220. * and fcp commands)
  6221. */
  6222. if (mbox_pending)
  6223. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  6224. lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
  6225. fpeq->EQ_processed++;
  6226. }
  6227. /* Always clear and re-arm the EQ */
  6228. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  6229. return mbox_pending;
  6230. }
  6231. /**
  6232. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  6233. * @phba: Pointer to HBA context object.
  6234. *
  6235. * This function is called from worker thread when a mailbox command times out.
  6236. * The caller is not required to hold any locks. This function will reset the
  6237. * HBA and recover all the pending commands.
  6238. **/
  6239. void
  6240. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  6241. {
  6242. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  6243. MAILBOX_t *mb = NULL;
  6244. struct lpfc_sli *psli = &phba->sli;
  6245. /* If the mailbox completed, process the completion and return */
  6246. if (lpfc_sli4_process_missed_mbox_completions(phba))
  6247. return;
  6248. if (pmbox != NULL)
  6249. mb = &pmbox->u.mb;
  6250. /* Check the pmbox pointer first. There is a race condition
  6251. * between the mbox timeout handler getting executed in the
  6252. * worklist and the mailbox actually completing. When this
  6253. * race condition occurs, the mbox_active will be NULL.
  6254. */
  6255. spin_lock_irq(&phba->hbalock);
  6256. if (pmbox == NULL) {
  6257. lpfc_printf_log(phba, KERN_WARNING,
  6258. LOG_MBOX | LOG_SLI,
  6259. "0353 Active Mailbox cleared - mailbox timeout "
  6260. "exiting\n");
  6261. spin_unlock_irq(&phba->hbalock);
  6262. return;
  6263. }
  6264. /* Mbox cmd <mbxCommand> timeout */
  6265. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6266. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  6267. mb->mbxCommand,
  6268. phba->pport->port_state,
  6269. phba->sli.sli_flag,
  6270. phba->sli.mbox_active);
  6271. spin_unlock_irq(&phba->hbalock);
  6272. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  6273. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  6274. * it to fail all outstanding SCSI IO.
  6275. */
  6276. spin_lock_irq(&phba->pport->work_port_lock);
  6277. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  6278. spin_unlock_irq(&phba->pport->work_port_lock);
  6279. spin_lock_irq(&phba->hbalock);
  6280. phba->link_state = LPFC_LINK_UNKNOWN;
  6281. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  6282. spin_unlock_irq(&phba->hbalock);
  6283. lpfc_sli_abort_fcp_rings(phba);
  6284. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6285. "0345 Resetting board due to mailbox timeout\n");
  6286. /* Reset the HBA device */
  6287. lpfc_reset_hba(phba);
  6288. }
  6289. /**
  6290. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  6291. * @phba: Pointer to HBA context object.
  6292. * @pmbox: Pointer to mailbox object.
  6293. * @flag: Flag indicating how the mailbox need to be processed.
  6294. *
  6295. * This function is called by discovery code and HBA management code
  6296. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  6297. * function gets the hbalock to protect the data structures.
  6298. * The mailbox command can be submitted in polling mode, in which case
  6299. * this function will wait in a polling loop for the completion of the
  6300. * mailbox.
  6301. * If the mailbox is submitted in no_wait mode (not polling) the
  6302. * function will submit the command and returns immediately without waiting
  6303. * for the mailbox completion. The no_wait is supported only when HBA
  6304. * is in SLI2/SLI3 mode - interrupts are enabled.
  6305. * The SLI interface allows only one mailbox pending at a time. If the
  6306. * mailbox is issued in polling mode and there is already a mailbox
  6307. * pending, then the function will return an error. If the mailbox is issued
  6308. * in NO_WAIT mode and there is a mailbox pending already, the function
  6309. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  6310. * The sli layer owns the mailbox object until the completion of mailbox
  6311. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  6312. * return codes the caller owns the mailbox command after the return of
  6313. * the function.
  6314. **/
  6315. static int
  6316. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  6317. uint32_t flag)
  6318. {
  6319. MAILBOX_t *mbx;
  6320. struct lpfc_sli *psli = &phba->sli;
  6321. uint32_t status, evtctr;
  6322. uint32_t ha_copy, hc_copy;
  6323. int i;
  6324. unsigned long timeout;
  6325. unsigned long drvr_flag = 0;
  6326. uint32_t word0, ldata;
  6327. void __iomem *to_slim;
  6328. int processing_queue = 0;
  6329. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6330. if (!pmbox) {
  6331. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6332. /* processing mbox queue from intr_handler */
  6333. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6334. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6335. return MBX_SUCCESS;
  6336. }
  6337. processing_queue = 1;
  6338. pmbox = lpfc_mbox_get(phba);
  6339. if (!pmbox) {
  6340. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6341. return MBX_SUCCESS;
  6342. }
  6343. }
  6344. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  6345. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  6346. if(!pmbox->vport) {
  6347. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6348. lpfc_printf_log(phba, KERN_ERR,
  6349. LOG_MBOX | LOG_VPORT,
  6350. "1806 Mbox x%x failed. No vport\n",
  6351. pmbox->u.mb.mbxCommand);
  6352. dump_stack();
  6353. goto out_not_finished;
  6354. }
  6355. }
  6356. /* If the PCI channel is in offline state, do not post mbox. */
  6357. if (unlikely(pci_channel_offline(phba->pcidev))) {
  6358. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6359. goto out_not_finished;
  6360. }
  6361. /* If HBA has a deferred error attention, fail the iocb. */
  6362. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  6363. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6364. goto out_not_finished;
  6365. }
  6366. psli = &phba->sli;
  6367. mbx = &pmbox->u.mb;
  6368. status = MBX_SUCCESS;
  6369. if (phba->link_state == LPFC_HBA_ERROR) {
  6370. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6371. /* Mbox command <mbxCommand> cannot issue */
  6372. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6373. "(%d):0311 Mailbox command x%x cannot "
  6374. "issue Data: x%x x%x\n",
  6375. pmbox->vport ? pmbox->vport->vpi : 0,
  6376. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6377. goto out_not_finished;
  6378. }
  6379. if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6380. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6381. !(hc_copy & HC_MBINT_ENA)) {
  6382. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6383. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6384. "(%d):2528 Mailbox command x%x cannot "
  6385. "issue Data: x%x x%x\n",
  6386. pmbox->vport ? pmbox->vport->vpi : 0,
  6387. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6388. goto out_not_finished;
  6389. }
  6390. }
  6391. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6392. /* Polling for a mbox command when another one is already active
  6393. * is not allowed in SLI. Also, the driver must have established
  6394. * SLI2 mode to queue and process multiple mbox commands.
  6395. */
  6396. if (flag & MBX_POLL) {
  6397. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6398. /* Mbox command <mbxCommand> cannot issue */
  6399. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6400. "(%d):2529 Mailbox command x%x "
  6401. "cannot issue Data: x%x x%x\n",
  6402. pmbox->vport ? pmbox->vport->vpi : 0,
  6403. pmbox->u.mb.mbxCommand,
  6404. psli->sli_flag, flag);
  6405. goto out_not_finished;
  6406. }
  6407. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  6408. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6409. /* Mbox command <mbxCommand> cannot issue */
  6410. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6411. "(%d):2530 Mailbox command x%x "
  6412. "cannot issue Data: x%x x%x\n",
  6413. pmbox->vport ? pmbox->vport->vpi : 0,
  6414. pmbox->u.mb.mbxCommand,
  6415. psli->sli_flag, flag);
  6416. goto out_not_finished;
  6417. }
  6418. /* Another mailbox command is still being processed, queue this
  6419. * command to be processed later.
  6420. */
  6421. lpfc_mbox_put(phba, pmbox);
  6422. /* Mbox cmd issue - BUSY */
  6423. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6424. "(%d):0308 Mbox cmd issue - BUSY Data: "
  6425. "x%x x%x x%x x%x\n",
  6426. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  6427. mbx->mbxCommand, phba->pport->port_state,
  6428. psli->sli_flag, flag);
  6429. psli->slistat.mbox_busy++;
  6430. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6431. if (pmbox->vport) {
  6432. lpfc_debugfs_disc_trc(pmbox->vport,
  6433. LPFC_DISC_TRC_MBOX_VPORT,
  6434. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  6435. (uint32_t)mbx->mbxCommand,
  6436. mbx->un.varWords[0], mbx->un.varWords[1]);
  6437. }
  6438. else {
  6439. lpfc_debugfs_disc_trc(phba->pport,
  6440. LPFC_DISC_TRC_MBOX,
  6441. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  6442. (uint32_t)mbx->mbxCommand,
  6443. mbx->un.varWords[0], mbx->un.varWords[1]);
  6444. }
  6445. return MBX_BUSY;
  6446. }
  6447. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6448. /* If we are not polling, we MUST be in SLI2 mode */
  6449. if (flag != MBX_POLL) {
  6450. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  6451. (mbx->mbxCommand != MBX_KILL_BOARD)) {
  6452. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6453. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6454. /* Mbox command <mbxCommand> cannot issue */
  6455. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6456. "(%d):2531 Mailbox command x%x "
  6457. "cannot issue Data: x%x x%x\n",
  6458. pmbox->vport ? pmbox->vport->vpi : 0,
  6459. pmbox->u.mb.mbxCommand,
  6460. psli->sli_flag, flag);
  6461. goto out_not_finished;
  6462. }
  6463. /* timeout active mbox command */
  6464. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6465. 1000);
  6466. mod_timer(&psli->mbox_tmo, jiffies + timeout);
  6467. }
  6468. /* Mailbox cmd <cmd> issue */
  6469. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6470. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  6471. "x%x\n",
  6472. pmbox->vport ? pmbox->vport->vpi : 0,
  6473. mbx->mbxCommand, phba->pport->port_state,
  6474. psli->sli_flag, flag);
  6475. if (mbx->mbxCommand != MBX_HEARTBEAT) {
  6476. if (pmbox->vport) {
  6477. lpfc_debugfs_disc_trc(pmbox->vport,
  6478. LPFC_DISC_TRC_MBOX_VPORT,
  6479. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6480. (uint32_t)mbx->mbxCommand,
  6481. mbx->un.varWords[0], mbx->un.varWords[1]);
  6482. }
  6483. else {
  6484. lpfc_debugfs_disc_trc(phba->pport,
  6485. LPFC_DISC_TRC_MBOX,
  6486. "MBOX Send: cmd:x%x mb:x%x x%x",
  6487. (uint32_t)mbx->mbxCommand,
  6488. mbx->un.varWords[0], mbx->un.varWords[1]);
  6489. }
  6490. }
  6491. psli->slistat.mbox_cmd++;
  6492. evtctr = psli->slistat.mbox_event;
  6493. /* next set own bit for the adapter and copy over command word */
  6494. mbx->mbxOwner = OWN_CHIP;
  6495. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6496. /* Populate mbox extension offset word. */
  6497. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6498. *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
  6499. = (uint8_t *)phba->mbox_ext
  6500. - (uint8_t *)phba->mbox;
  6501. }
  6502. /* Copy the mailbox extension data */
  6503. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6504. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6505. (uint8_t *)phba->mbox_ext,
  6506. pmbox->in_ext_byte_len);
  6507. }
  6508. /* Copy command data to host SLIM area */
  6509. lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
  6510. } else {
  6511. /* Populate mbox extension offset word. */
  6512. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6513. *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
  6514. = MAILBOX_HBA_EXT_OFFSET;
  6515. /* Copy the mailbox extension data */
  6516. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6517. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6518. MAILBOX_HBA_EXT_OFFSET,
  6519. pmbox->context2, pmbox->in_ext_byte_len);
  6520. }
  6521. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6522. /* copy command data into host mbox for cmpl */
  6523. lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
  6524. }
  6525. /* First copy mbox command data to HBA SLIM, skip past first
  6526. word */
  6527. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6528. lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
  6529. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6530. /* Next copy over first word, with mbxOwner set */
  6531. ldata = *((uint32_t *)mbx);
  6532. to_slim = phba->MBslimaddr;
  6533. writel(ldata, to_slim);
  6534. readl(to_slim); /* flush */
  6535. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6536. /* switch over to host mailbox */
  6537. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6538. }
  6539. }
  6540. wmb();
  6541. switch (flag) {
  6542. case MBX_NOWAIT:
  6543. /* Set up reference to mailbox command */
  6544. psli->mbox_active = pmbox;
  6545. /* Interrupt board to do it */
  6546. writel(CA_MBATT, phba->CAregaddr);
  6547. readl(phba->CAregaddr); /* flush */
  6548. /* Don't wait for it to finish, just return */
  6549. break;
  6550. case MBX_POLL:
  6551. /* Set up null reference to mailbox command */
  6552. psli->mbox_active = NULL;
  6553. /* Interrupt board to do it */
  6554. writel(CA_MBATT, phba->CAregaddr);
  6555. readl(phba->CAregaddr); /* flush */
  6556. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6557. /* First read mbox status word */
  6558. word0 = *((uint32_t *)phba->mbox);
  6559. word0 = le32_to_cpu(word0);
  6560. } else {
  6561. /* First read mbox status word */
  6562. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6563. spin_unlock_irqrestore(&phba->hbalock,
  6564. drvr_flag);
  6565. goto out_not_finished;
  6566. }
  6567. }
  6568. /* Read the HBA Host Attention Register */
  6569. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6570. spin_unlock_irqrestore(&phba->hbalock,
  6571. drvr_flag);
  6572. goto out_not_finished;
  6573. }
  6574. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6575. 1000) + jiffies;
  6576. i = 0;
  6577. /* Wait for command to complete */
  6578. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6579. (!(ha_copy & HA_MBATT) &&
  6580. (phba->link_state > LPFC_WARM_START))) {
  6581. if (time_after(jiffies, timeout)) {
  6582. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6583. spin_unlock_irqrestore(&phba->hbalock,
  6584. drvr_flag);
  6585. goto out_not_finished;
  6586. }
  6587. /* Check if we took a mbox interrupt while we were
  6588. polling */
  6589. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6590. && (evtctr != psli->slistat.mbox_event))
  6591. break;
  6592. if (i++ > 10) {
  6593. spin_unlock_irqrestore(&phba->hbalock,
  6594. drvr_flag);
  6595. msleep(1);
  6596. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6597. }
  6598. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6599. /* First copy command data */
  6600. word0 = *((uint32_t *)phba->mbox);
  6601. word0 = le32_to_cpu(word0);
  6602. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6603. MAILBOX_t *slimmb;
  6604. uint32_t slimword0;
  6605. /* Check real SLIM for any errors */
  6606. slimword0 = readl(phba->MBslimaddr);
  6607. slimmb = (MAILBOX_t *) & slimword0;
  6608. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6609. && slimmb->mbxStatus) {
  6610. psli->sli_flag &=
  6611. ~LPFC_SLI_ACTIVE;
  6612. word0 = slimword0;
  6613. }
  6614. }
  6615. } else {
  6616. /* First copy command data */
  6617. word0 = readl(phba->MBslimaddr);
  6618. }
  6619. /* Read the HBA Host Attention Register */
  6620. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6621. spin_unlock_irqrestore(&phba->hbalock,
  6622. drvr_flag);
  6623. goto out_not_finished;
  6624. }
  6625. }
  6626. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6627. /* copy results back to user */
  6628. lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
  6629. /* Copy the mailbox extension data */
  6630. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6631. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6632. pmbox->context2,
  6633. pmbox->out_ext_byte_len);
  6634. }
  6635. } else {
  6636. /* First copy command data */
  6637. lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
  6638. MAILBOX_CMD_SIZE);
  6639. /* Copy the mailbox extension data */
  6640. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6641. lpfc_memcpy_from_slim(pmbox->context2,
  6642. phba->MBslimaddr +
  6643. MAILBOX_HBA_EXT_OFFSET,
  6644. pmbox->out_ext_byte_len);
  6645. }
  6646. }
  6647. writel(HA_MBATT, phba->HAregaddr);
  6648. readl(phba->HAregaddr); /* flush */
  6649. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6650. status = mbx->mbxStatus;
  6651. }
  6652. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6653. return status;
  6654. out_not_finished:
  6655. if (processing_queue) {
  6656. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6657. lpfc_mbox_cmpl_put(phba, pmbox);
  6658. }
  6659. return MBX_NOT_FINISHED;
  6660. }
  6661. /**
  6662. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6663. * @phba: Pointer to HBA context object.
  6664. *
  6665. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6666. * the driver internal pending mailbox queue. It will then try to wait out the
  6667. * possible outstanding mailbox command before return.
  6668. *
  6669. * Returns:
  6670. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6671. * the outstanding mailbox command timed out.
  6672. **/
  6673. static int
  6674. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6675. {
  6676. struct lpfc_sli *psli = &phba->sli;
  6677. int rc = 0;
  6678. unsigned long timeout = 0;
  6679. /* Mark the asynchronous mailbox command posting as blocked */
  6680. spin_lock_irq(&phba->hbalock);
  6681. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6682. /* Determine how long we might wait for the active mailbox
  6683. * command to be gracefully completed by firmware.
  6684. */
  6685. if (phba->sli.mbox_active)
  6686. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6687. phba->sli.mbox_active) *
  6688. 1000) + jiffies;
  6689. spin_unlock_irq(&phba->hbalock);
  6690. /* Make sure the mailbox is really active */
  6691. if (timeout)
  6692. lpfc_sli4_process_missed_mbox_completions(phba);
  6693. /* Wait for the outstnading mailbox command to complete */
  6694. while (phba->sli.mbox_active) {
  6695. /* Check active mailbox complete status every 2ms */
  6696. msleep(2);
  6697. if (time_after(jiffies, timeout)) {
  6698. /* Timeout, marked the outstanding cmd not complete */
  6699. rc = 1;
  6700. break;
  6701. }
  6702. }
  6703. /* Can not cleanly block async mailbox command, fails it */
  6704. if (rc) {
  6705. spin_lock_irq(&phba->hbalock);
  6706. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6707. spin_unlock_irq(&phba->hbalock);
  6708. }
  6709. return rc;
  6710. }
  6711. /**
  6712. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6713. * @phba: Pointer to HBA context object.
  6714. *
  6715. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6716. * commands from the driver internal pending mailbox queue. It makes sure
  6717. * that there is no outstanding mailbox command before resuming posting
  6718. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6719. * mailbox command, it will try to wait it out before resuming asynchronous
  6720. * mailbox command posting.
  6721. **/
  6722. static void
  6723. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6724. {
  6725. struct lpfc_sli *psli = &phba->sli;
  6726. spin_lock_irq(&phba->hbalock);
  6727. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6728. /* Asynchronous mailbox posting is not blocked, do nothing */
  6729. spin_unlock_irq(&phba->hbalock);
  6730. return;
  6731. }
  6732. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6733. * successful or timeout, after timing-out the outstanding mailbox
  6734. * command shall always be removed, so just unblock posting async
  6735. * mailbox command and resume
  6736. */
  6737. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6738. spin_unlock_irq(&phba->hbalock);
  6739. /* wake up worker thread to post asynchronlous mailbox command */
  6740. lpfc_worker_wake_up(phba);
  6741. }
  6742. /**
  6743. * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
  6744. * @phba: Pointer to HBA context object.
  6745. * @mboxq: Pointer to mailbox object.
  6746. *
  6747. * The function waits for the bootstrap mailbox register ready bit from
  6748. * port for twice the regular mailbox command timeout value.
  6749. *
  6750. * 0 - no timeout on waiting for bootstrap mailbox register ready.
  6751. * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
  6752. **/
  6753. static int
  6754. lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6755. {
  6756. uint32_t db_ready;
  6757. unsigned long timeout;
  6758. struct lpfc_register bmbx_reg;
  6759. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6760. * 1000) + jiffies;
  6761. do {
  6762. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6763. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6764. if (!db_ready)
  6765. msleep(2);
  6766. if (time_after(jiffies, timeout))
  6767. return MBXERR_ERROR;
  6768. } while (!db_ready);
  6769. return 0;
  6770. }
  6771. /**
  6772. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6773. * @phba: Pointer to HBA context object.
  6774. * @mboxq: Pointer to mailbox object.
  6775. *
  6776. * The function posts a mailbox to the port. The mailbox is expected
  6777. * to be comletely filled in and ready for the port to operate on it.
  6778. * This routine executes a synchronous completion operation on the
  6779. * mailbox by polling for its completion.
  6780. *
  6781. * The caller must not be holding any locks when calling this routine.
  6782. *
  6783. * Returns:
  6784. * MBX_SUCCESS - mailbox posted successfully
  6785. * Any of the MBX error values.
  6786. **/
  6787. static int
  6788. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6789. {
  6790. int rc = MBX_SUCCESS;
  6791. unsigned long iflag;
  6792. uint32_t mcqe_status;
  6793. uint32_t mbx_cmnd;
  6794. struct lpfc_sli *psli = &phba->sli;
  6795. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6796. struct lpfc_bmbx_create *mbox_rgn;
  6797. struct dma_address *dma_address;
  6798. /*
  6799. * Only one mailbox can be active to the bootstrap mailbox region
  6800. * at a time and there is no queueing provided.
  6801. */
  6802. spin_lock_irqsave(&phba->hbalock, iflag);
  6803. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6804. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6805. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6806. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6807. "cannot issue Data: x%x x%x\n",
  6808. mboxq->vport ? mboxq->vport->vpi : 0,
  6809. mboxq->u.mb.mbxCommand,
  6810. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6811. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6812. psli->sli_flag, MBX_POLL);
  6813. return MBXERR_ERROR;
  6814. }
  6815. /* The server grabs the token and owns it until release */
  6816. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6817. phba->sli.mbox_active = mboxq;
  6818. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6819. /* wait for bootstrap mbox register for readyness */
  6820. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6821. if (rc)
  6822. goto exit;
  6823. /*
  6824. * Initialize the bootstrap memory region to avoid stale data areas
  6825. * in the mailbox post. Then copy the caller's mailbox contents to
  6826. * the bmbx mailbox region.
  6827. */
  6828. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6829. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6830. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6831. sizeof(struct lpfc_mqe));
  6832. /* Post the high mailbox dma address to the port and wait for ready. */
  6833. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6834. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6835. /* wait for bootstrap mbox register for hi-address write done */
  6836. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6837. if (rc)
  6838. goto exit;
  6839. /* Post the low mailbox dma address to the port. */
  6840. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6841. /* wait for bootstrap mbox register for low address write done */
  6842. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6843. if (rc)
  6844. goto exit;
  6845. /*
  6846. * Read the CQ to ensure the mailbox has completed.
  6847. * If so, update the mailbox status so that the upper layers
  6848. * can complete the request normally.
  6849. */
  6850. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6851. sizeof(struct lpfc_mqe));
  6852. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6853. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6854. sizeof(struct lpfc_mcqe));
  6855. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6856. /*
  6857. * When the CQE status indicates a failure and the mailbox status
  6858. * indicates success then copy the CQE status into the mailbox status
  6859. * (and prefix it with x4000).
  6860. */
  6861. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6862. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6863. bf_set(lpfc_mqe_status, mb,
  6864. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6865. rc = MBXERR_ERROR;
  6866. } else
  6867. lpfc_sli4_swap_str(phba, mboxq);
  6868. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6869. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6870. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6871. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6872. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6873. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6874. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6875. bf_get(lpfc_mqe_status, mb),
  6876. mb->un.mb_words[0], mb->un.mb_words[1],
  6877. mb->un.mb_words[2], mb->un.mb_words[3],
  6878. mb->un.mb_words[4], mb->un.mb_words[5],
  6879. mb->un.mb_words[6], mb->un.mb_words[7],
  6880. mb->un.mb_words[8], mb->un.mb_words[9],
  6881. mb->un.mb_words[10], mb->un.mb_words[11],
  6882. mb->un.mb_words[12], mboxq->mcqe.word0,
  6883. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6884. mboxq->mcqe.trailer);
  6885. exit:
  6886. /* We are holding the token, no needed for lock when release */
  6887. spin_lock_irqsave(&phba->hbalock, iflag);
  6888. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6889. phba->sli.mbox_active = NULL;
  6890. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6891. return rc;
  6892. }
  6893. /**
  6894. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6895. * @phba: Pointer to HBA context object.
  6896. * @pmbox: Pointer to mailbox object.
  6897. * @flag: Flag indicating how the mailbox need to be processed.
  6898. *
  6899. * This function is called by discovery code and HBA management code to submit
  6900. * a mailbox command to firmware with SLI-4 interface spec.
  6901. *
  6902. * Return codes the caller owns the mailbox command after the return of the
  6903. * function.
  6904. **/
  6905. static int
  6906. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6907. uint32_t flag)
  6908. {
  6909. struct lpfc_sli *psli = &phba->sli;
  6910. unsigned long iflags;
  6911. int rc;
  6912. /* dump from issue mailbox command if setup */
  6913. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6914. rc = lpfc_mbox_dev_check(phba);
  6915. if (unlikely(rc)) {
  6916. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6917. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6918. "cannot issue Data: x%x x%x\n",
  6919. mboxq->vport ? mboxq->vport->vpi : 0,
  6920. mboxq->u.mb.mbxCommand,
  6921. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6922. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6923. psli->sli_flag, flag);
  6924. goto out_not_finished;
  6925. }
  6926. /* Detect polling mode and jump to a handler */
  6927. if (!phba->sli4_hba.intr_enable) {
  6928. if (flag == MBX_POLL)
  6929. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6930. else
  6931. rc = -EIO;
  6932. if (rc != MBX_SUCCESS)
  6933. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6934. "(%d):2541 Mailbox command x%x "
  6935. "(x%x/x%x) failure: "
  6936. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6937. "Data: x%x x%x\n,",
  6938. mboxq->vport ? mboxq->vport->vpi : 0,
  6939. mboxq->u.mb.mbxCommand,
  6940. lpfc_sli_config_mbox_subsys_get(phba,
  6941. mboxq),
  6942. lpfc_sli_config_mbox_opcode_get(phba,
  6943. mboxq),
  6944. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6945. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6946. bf_get(lpfc_mcqe_ext_status,
  6947. &mboxq->mcqe),
  6948. psli->sli_flag, flag);
  6949. return rc;
  6950. } else if (flag == MBX_POLL) {
  6951. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6952. "(%d):2542 Try to issue mailbox command "
  6953. "x%x (x%x/x%x) synchronously ahead of async"
  6954. "mailbox command queue: x%x x%x\n",
  6955. mboxq->vport ? mboxq->vport->vpi : 0,
  6956. mboxq->u.mb.mbxCommand,
  6957. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6958. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6959. psli->sli_flag, flag);
  6960. /* Try to block the asynchronous mailbox posting */
  6961. rc = lpfc_sli4_async_mbox_block(phba);
  6962. if (!rc) {
  6963. /* Successfully blocked, now issue sync mbox cmd */
  6964. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6965. if (rc != MBX_SUCCESS)
  6966. lpfc_printf_log(phba, KERN_WARNING,
  6967. LOG_MBOX | LOG_SLI,
  6968. "(%d):2597 Sync Mailbox command "
  6969. "x%x (x%x/x%x) failure: "
  6970. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6971. "Data: x%x x%x\n,",
  6972. mboxq->vport ? mboxq->vport->vpi : 0,
  6973. mboxq->u.mb.mbxCommand,
  6974. lpfc_sli_config_mbox_subsys_get(phba,
  6975. mboxq),
  6976. lpfc_sli_config_mbox_opcode_get(phba,
  6977. mboxq),
  6978. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6979. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6980. bf_get(lpfc_mcqe_ext_status,
  6981. &mboxq->mcqe),
  6982. psli->sli_flag, flag);
  6983. /* Unblock the async mailbox posting afterward */
  6984. lpfc_sli4_async_mbox_unblock(phba);
  6985. }
  6986. return rc;
  6987. }
  6988. /* Now, interrupt mode asynchrous mailbox command */
  6989. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6990. if (rc) {
  6991. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6992. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6993. "cannot issue Data: x%x x%x\n",
  6994. mboxq->vport ? mboxq->vport->vpi : 0,
  6995. mboxq->u.mb.mbxCommand,
  6996. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6997. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6998. psli->sli_flag, flag);
  6999. goto out_not_finished;
  7000. }
  7001. /* Put the mailbox command to the driver internal FIFO */
  7002. psli->slistat.mbox_busy++;
  7003. spin_lock_irqsave(&phba->hbalock, iflags);
  7004. lpfc_mbox_put(phba, mboxq);
  7005. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7006. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  7007. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  7008. "x%x (x%x/x%x) x%x x%x x%x\n",
  7009. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  7010. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  7011. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  7012. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  7013. phba->pport->port_state,
  7014. psli->sli_flag, MBX_NOWAIT);
  7015. /* Wake up worker thread to transport mailbox command from head */
  7016. lpfc_worker_wake_up(phba);
  7017. return MBX_BUSY;
  7018. out_not_finished:
  7019. return MBX_NOT_FINISHED;
  7020. }
  7021. /**
  7022. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  7023. * @phba: Pointer to HBA context object.
  7024. *
  7025. * This function is called by worker thread to send a mailbox command to
  7026. * SLI4 HBA firmware.
  7027. *
  7028. **/
  7029. int
  7030. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  7031. {
  7032. struct lpfc_sli *psli = &phba->sli;
  7033. LPFC_MBOXQ_t *mboxq;
  7034. int rc = MBX_SUCCESS;
  7035. unsigned long iflags;
  7036. struct lpfc_mqe *mqe;
  7037. uint32_t mbx_cmnd;
  7038. /* Check interrupt mode before post async mailbox command */
  7039. if (unlikely(!phba->sli4_hba.intr_enable))
  7040. return MBX_NOT_FINISHED;
  7041. /* Check for mailbox command service token */
  7042. spin_lock_irqsave(&phba->hbalock, iflags);
  7043. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  7044. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7045. return MBX_NOT_FINISHED;
  7046. }
  7047. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  7048. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7049. return MBX_NOT_FINISHED;
  7050. }
  7051. if (unlikely(phba->sli.mbox_active)) {
  7052. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7053. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  7054. "0384 There is pending active mailbox cmd\n");
  7055. return MBX_NOT_FINISHED;
  7056. }
  7057. /* Take the mailbox command service token */
  7058. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  7059. /* Get the next mailbox command from head of queue */
  7060. mboxq = lpfc_mbox_get(phba);
  7061. /* If no more mailbox command waiting for post, we're done */
  7062. if (!mboxq) {
  7063. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7064. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7065. return MBX_SUCCESS;
  7066. }
  7067. phba->sli.mbox_active = mboxq;
  7068. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7069. /* Check device readiness for posting mailbox command */
  7070. rc = lpfc_mbox_dev_check(phba);
  7071. if (unlikely(rc))
  7072. /* Driver clean routine will clean up pending mailbox */
  7073. goto out_not_finished;
  7074. /* Prepare the mbox command to be posted */
  7075. mqe = &mboxq->u.mqe;
  7076. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  7077. /* Start timer for the mbox_tmo and log some mailbox post messages */
  7078. mod_timer(&psli->mbox_tmo, (jiffies +
  7079. msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
  7080. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  7081. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  7082. "x%x x%x\n",
  7083. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  7084. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  7085. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  7086. phba->pport->port_state, psli->sli_flag);
  7087. if (mbx_cmnd != MBX_HEARTBEAT) {
  7088. if (mboxq->vport) {
  7089. lpfc_debugfs_disc_trc(mboxq->vport,
  7090. LPFC_DISC_TRC_MBOX_VPORT,
  7091. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  7092. mbx_cmnd, mqe->un.mb_words[0],
  7093. mqe->un.mb_words[1]);
  7094. } else {
  7095. lpfc_debugfs_disc_trc(phba->pport,
  7096. LPFC_DISC_TRC_MBOX,
  7097. "MBOX Send: cmd:x%x mb:x%x x%x",
  7098. mbx_cmnd, mqe->un.mb_words[0],
  7099. mqe->un.mb_words[1]);
  7100. }
  7101. }
  7102. psli->slistat.mbox_cmd++;
  7103. /* Post the mailbox command to the port */
  7104. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  7105. if (rc != MBX_SUCCESS) {
  7106. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  7107. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  7108. "cannot issue Data: x%x x%x\n",
  7109. mboxq->vport ? mboxq->vport->vpi : 0,
  7110. mboxq->u.mb.mbxCommand,
  7111. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  7112. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  7113. psli->sli_flag, MBX_NOWAIT);
  7114. goto out_not_finished;
  7115. }
  7116. return rc;
  7117. out_not_finished:
  7118. spin_lock_irqsave(&phba->hbalock, iflags);
  7119. if (phba->sli.mbox_active) {
  7120. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  7121. __lpfc_mbox_cmpl_put(phba, mboxq);
  7122. /* Release the token */
  7123. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7124. phba->sli.mbox_active = NULL;
  7125. }
  7126. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7127. return MBX_NOT_FINISHED;
  7128. }
  7129. /**
  7130. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  7131. * @phba: Pointer to HBA context object.
  7132. * @pmbox: Pointer to mailbox object.
  7133. * @flag: Flag indicating how the mailbox need to be processed.
  7134. *
  7135. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  7136. * the API jump table function pointer from the lpfc_hba struct.
  7137. *
  7138. * Return codes the caller owns the mailbox command after the return of the
  7139. * function.
  7140. **/
  7141. int
  7142. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  7143. {
  7144. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  7145. }
  7146. /**
  7147. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  7148. * @phba: The hba struct for which this call is being executed.
  7149. * @dev_grp: The HBA PCI-Device group number.
  7150. *
  7151. * This routine sets up the mbox interface API function jump table in @phba
  7152. * struct.
  7153. * Returns: 0 - success, -ENODEV - failure.
  7154. **/
  7155. int
  7156. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7157. {
  7158. switch (dev_grp) {
  7159. case LPFC_PCI_DEV_LP:
  7160. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  7161. phba->lpfc_sli_handle_slow_ring_event =
  7162. lpfc_sli_handle_slow_ring_event_s3;
  7163. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  7164. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  7165. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  7166. break;
  7167. case LPFC_PCI_DEV_OC:
  7168. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  7169. phba->lpfc_sli_handle_slow_ring_event =
  7170. lpfc_sli_handle_slow_ring_event_s4;
  7171. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  7172. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  7173. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  7174. break;
  7175. default:
  7176. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7177. "1420 Invalid HBA PCI-device group: 0x%x\n",
  7178. dev_grp);
  7179. return -ENODEV;
  7180. break;
  7181. }
  7182. return 0;
  7183. }
  7184. /**
  7185. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  7186. * @phba: Pointer to HBA context object.
  7187. * @pring: Pointer to driver SLI ring object.
  7188. * @piocb: Pointer to address of newly added command iocb.
  7189. *
  7190. * This function is called with hbalock held to add a command
  7191. * iocb to the txq when SLI layer cannot submit the command iocb
  7192. * to the ring.
  7193. **/
  7194. void
  7195. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7196. struct lpfc_iocbq *piocb)
  7197. {
  7198. lockdep_assert_held(&phba->hbalock);
  7199. /* Insert the caller's iocb in the txq tail for later processing. */
  7200. list_add_tail(&piocb->list, &pring->txq);
  7201. }
  7202. /**
  7203. * lpfc_sli_next_iocb - Get the next iocb in the txq
  7204. * @phba: Pointer to HBA context object.
  7205. * @pring: Pointer to driver SLI ring object.
  7206. * @piocb: Pointer to address of newly added command iocb.
  7207. *
  7208. * This function is called with hbalock held before a new
  7209. * iocb is submitted to the firmware. This function checks
  7210. * txq to flush the iocbs in txq to Firmware before
  7211. * submitting new iocbs to the Firmware.
  7212. * If there are iocbs in the txq which need to be submitted
  7213. * to firmware, lpfc_sli_next_iocb returns the first element
  7214. * of the txq after dequeuing it from txq.
  7215. * If there is no iocb in the txq then the function will return
  7216. * *piocb and *piocb is set to NULL. Caller needs to check
  7217. * *piocb to find if there are more commands in the txq.
  7218. **/
  7219. static struct lpfc_iocbq *
  7220. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7221. struct lpfc_iocbq **piocb)
  7222. {
  7223. struct lpfc_iocbq * nextiocb;
  7224. lockdep_assert_held(&phba->hbalock);
  7225. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  7226. if (!nextiocb) {
  7227. nextiocb = *piocb;
  7228. *piocb = NULL;
  7229. }
  7230. return nextiocb;
  7231. }
  7232. /**
  7233. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  7234. * @phba: Pointer to HBA context object.
  7235. * @ring_number: SLI ring number to issue iocb on.
  7236. * @piocb: Pointer to command iocb.
  7237. * @flag: Flag indicating if this command can be put into txq.
  7238. *
  7239. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  7240. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  7241. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  7242. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  7243. * this function allows only iocbs for posting buffers. This function finds
  7244. * next available slot in the command ring and posts the command to the
  7245. * available slot and writes the port attention register to request HBA start
  7246. * processing new iocb. If there is no slot available in the ring and
  7247. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  7248. * the function returns IOCB_BUSY.
  7249. *
  7250. * This function is called with hbalock held. The function will return success
  7251. * after it successfully submit the iocb to firmware or after adding to the
  7252. * txq.
  7253. **/
  7254. static int
  7255. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  7256. struct lpfc_iocbq *piocb, uint32_t flag)
  7257. {
  7258. struct lpfc_iocbq *nextiocb;
  7259. IOCB_t *iocb;
  7260. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7261. lockdep_assert_held(&phba->hbalock);
  7262. if (piocb->iocb_cmpl && (!piocb->vport) &&
  7263. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  7264. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  7265. lpfc_printf_log(phba, KERN_ERR,
  7266. LOG_SLI | LOG_VPORT,
  7267. "1807 IOCB x%x failed. No vport\n",
  7268. piocb->iocb.ulpCommand);
  7269. dump_stack();
  7270. return IOCB_ERROR;
  7271. }
  7272. /* If the PCI channel is in offline state, do not post iocbs. */
  7273. if (unlikely(pci_channel_offline(phba->pcidev)))
  7274. return IOCB_ERROR;
  7275. /* If HBA has a deferred error attention, fail the iocb. */
  7276. if (unlikely(phba->hba_flag & DEFER_ERATT))
  7277. return IOCB_ERROR;
  7278. /*
  7279. * We should never get an IOCB if we are in a < LINK_DOWN state
  7280. */
  7281. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  7282. return IOCB_ERROR;
  7283. /*
  7284. * Check to see if we are blocking IOCB processing because of a
  7285. * outstanding event.
  7286. */
  7287. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  7288. goto iocb_busy;
  7289. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  7290. /*
  7291. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  7292. * can be issued if the link is not up.
  7293. */
  7294. switch (piocb->iocb.ulpCommand) {
  7295. case CMD_GEN_REQUEST64_CR:
  7296. case CMD_GEN_REQUEST64_CX:
  7297. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  7298. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  7299. FC_RCTL_DD_UNSOL_CMD) ||
  7300. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  7301. MENLO_TRANSPORT_TYPE))
  7302. goto iocb_busy;
  7303. break;
  7304. case CMD_QUE_RING_BUF_CN:
  7305. case CMD_QUE_RING_BUF64_CN:
  7306. /*
  7307. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  7308. * completion, iocb_cmpl MUST be 0.
  7309. */
  7310. if (piocb->iocb_cmpl)
  7311. piocb->iocb_cmpl = NULL;
  7312. /*FALLTHROUGH*/
  7313. case CMD_CREATE_XRI_CR:
  7314. case CMD_CLOSE_XRI_CN:
  7315. case CMD_CLOSE_XRI_CX:
  7316. break;
  7317. default:
  7318. goto iocb_busy;
  7319. }
  7320. /*
  7321. * For FCP commands, we must be in a state where we can process link
  7322. * attention events.
  7323. */
  7324. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  7325. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  7326. goto iocb_busy;
  7327. }
  7328. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  7329. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  7330. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  7331. if (iocb)
  7332. lpfc_sli_update_ring(phba, pring);
  7333. else
  7334. lpfc_sli_update_full_ring(phba, pring);
  7335. if (!piocb)
  7336. return IOCB_SUCCESS;
  7337. goto out_busy;
  7338. iocb_busy:
  7339. pring->stats.iocb_cmd_delay++;
  7340. out_busy:
  7341. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7342. __lpfc_sli_ringtx_put(phba, pring, piocb);
  7343. return IOCB_SUCCESS;
  7344. }
  7345. return IOCB_BUSY;
  7346. }
  7347. /**
  7348. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  7349. * @phba: Pointer to HBA context object.
  7350. * @piocb: Pointer to command iocb.
  7351. * @sglq: Pointer to the scatter gather queue object.
  7352. *
  7353. * This routine converts the bpl or bde that is in the IOCB
  7354. * to a sgl list for the sli4 hardware. The physical address
  7355. * of the bpl/bde is converted back to a virtual address.
  7356. * If the IOCB contains a BPL then the list of BDE's is
  7357. * converted to sli4_sge's. If the IOCB contains a single
  7358. * BDE then it is converted to a single sli_sge.
  7359. * The IOCB is still in cpu endianess so the contents of
  7360. * the bpl can be used without byte swapping.
  7361. *
  7362. * Returns valid XRI = Success, NO_XRI = Failure.
  7363. **/
  7364. static uint16_t
  7365. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  7366. struct lpfc_sglq *sglq)
  7367. {
  7368. uint16_t xritag = NO_XRI;
  7369. struct ulp_bde64 *bpl = NULL;
  7370. struct ulp_bde64 bde;
  7371. struct sli4_sge *sgl = NULL;
  7372. struct lpfc_dmabuf *dmabuf;
  7373. IOCB_t *icmd;
  7374. int numBdes = 0;
  7375. int i = 0;
  7376. uint32_t offset = 0; /* accumulated offset in the sg request list */
  7377. int inbound = 0; /* number of sg reply entries inbound from firmware */
  7378. if (!piocbq || !sglq)
  7379. return xritag;
  7380. sgl = (struct sli4_sge *)sglq->sgl;
  7381. icmd = &piocbq->iocb;
  7382. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  7383. return sglq->sli4_xritag;
  7384. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7385. numBdes = icmd->un.genreq64.bdl.bdeSize /
  7386. sizeof(struct ulp_bde64);
  7387. /* The addrHigh and addrLow fields within the IOCB
  7388. * have not been byteswapped yet so there is no
  7389. * need to swap them back.
  7390. */
  7391. if (piocbq->context3)
  7392. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  7393. else
  7394. return xritag;
  7395. bpl = (struct ulp_bde64 *)dmabuf->virt;
  7396. if (!bpl)
  7397. return xritag;
  7398. for (i = 0; i < numBdes; i++) {
  7399. /* Should already be byte swapped. */
  7400. sgl->addr_hi = bpl->addrHigh;
  7401. sgl->addr_lo = bpl->addrLow;
  7402. sgl->word2 = le32_to_cpu(sgl->word2);
  7403. if ((i+1) == numBdes)
  7404. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7405. else
  7406. bf_set(lpfc_sli4_sge_last, sgl, 0);
  7407. /* swap the size field back to the cpu so we
  7408. * can assign it to the sgl.
  7409. */
  7410. bde.tus.w = le32_to_cpu(bpl->tus.w);
  7411. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  7412. /* The offsets in the sgl need to be accumulated
  7413. * separately for the request and reply lists.
  7414. * The request is always first, the reply follows.
  7415. */
  7416. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  7417. /* add up the reply sg entries */
  7418. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  7419. inbound++;
  7420. /* first inbound? reset the offset */
  7421. if (inbound == 1)
  7422. offset = 0;
  7423. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  7424. bf_set(lpfc_sli4_sge_type, sgl,
  7425. LPFC_SGE_TYPE_DATA);
  7426. offset += bde.tus.f.bdeSize;
  7427. }
  7428. sgl->word2 = cpu_to_le32(sgl->word2);
  7429. bpl++;
  7430. sgl++;
  7431. }
  7432. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  7433. /* The addrHigh and addrLow fields of the BDE have not
  7434. * been byteswapped yet so they need to be swapped
  7435. * before putting them in the sgl.
  7436. */
  7437. sgl->addr_hi =
  7438. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  7439. sgl->addr_lo =
  7440. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  7441. sgl->word2 = le32_to_cpu(sgl->word2);
  7442. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7443. sgl->word2 = cpu_to_le32(sgl->word2);
  7444. sgl->sge_len =
  7445. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  7446. }
  7447. return sglq->sli4_xritag;
  7448. }
  7449. /**
  7450. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  7451. * @phba: Pointer to HBA context object.
  7452. * @piocb: Pointer to command iocb.
  7453. * @wqe: Pointer to the work queue entry.
  7454. *
  7455. * This routine converts the iocb command to its Work Queue Entry
  7456. * equivalent. The wqe pointer should not have any fields set when
  7457. * this routine is called because it will memcpy over them.
  7458. * This routine does not set the CQ_ID or the WQEC bits in the
  7459. * wqe.
  7460. *
  7461. * Returns: 0 = Success, IOCB_ERROR = Failure.
  7462. **/
  7463. static int
  7464. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  7465. union lpfc_wqe *wqe)
  7466. {
  7467. uint32_t xmit_len = 0, total_len = 0;
  7468. uint8_t ct = 0;
  7469. uint32_t fip;
  7470. uint32_t abort_tag;
  7471. uint8_t command_type = ELS_COMMAND_NON_FIP;
  7472. uint8_t cmnd;
  7473. uint16_t xritag;
  7474. uint16_t abrt_iotag;
  7475. struct lpfc_iocbq *abrtiocbq;
  7476. struct ulp_bde64 *bpl = NULL;
  7477. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  7478. int numBdes, i;
  7479. struct ulp_bde64 bde;
  7480. struct lpfc_nodelist *ndlp;
  7481. uint32_t *pcmd;
  7482. uint32_t if_type;
  7483. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  7484. /* The fcp commands will set command type */
  7485. if (iocbq->iocb_flag & LPFC_IO_FCP)
  7486. command_type = FCP_COMMAND;
  7487. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  7488. command_type = ELS_COMMAND_FIP;
  7489. else
  7490. command_type = ELS_COMMAND_NON_FIP;
  7491. if (phba->fcp_embed_io)
  7492. memset(wqe, 0, sizeof(union lpfc_wqe128));
  7493. /* Some of the fields are in the right position already */
  7494. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  7495. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  7496. wqe->generic.wqe_com.word10 = 0;
  7497. abort_tag = (uint32_t) iocbq->iotag;
  7498. xritag = iocbq->sli4_xritag;
  7499. /* words0-2 bpl convert bde */
  7500. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7501. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7502. sizeof(struct ulp_bde64);
  7503. bpl = (struct ulp_bde64 *)
  7504. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  7505. if (!bpl)
  7506. return IOCB_ERROR;
  7507. /* Should already be byte swapped. */
  7508. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7509. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7510. /* swap the size field back to the cpu so we
  7511. * can assign it to the sgl.
  7512. */
  7513. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7514. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7515. total_len = 0;
  7516. for (i = 0; i < numBdes; i++) {
  7517. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7518. total_len += bde.tus.f.bdeSize;
  7519. }
  7520. } else
  7521. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7522. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7523. cmnd = iocbq->iocb.ulpCommand;
  7524. switch (iocbq->iocb.ulpCommand) {
  7525. case CMD_ELS_REQUEST64_CR:
  7526. if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
  7527. ndlp = iocbq->context_un.ndlp;
  7528. else
  7529. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7530. if (!iocbq->iocb.ulpLe) {
  7531. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7532. "2007 Only Limited Edition cmd Format"
  7533. " supported 0x%x\n",
  7534. iocbq->iocb.ulpCommand);
  7535. return IOCB_ERROR;
  7536. }
  7537. wqe->els_req.payload_len = xmit_len;
  7538. /* Els_reguest64 has a TMO */
  7539. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7540. iocbq->iocb.ulpTimeout);
  7541. /* Need a VF for word 4 set the vf bit*/
  7542. bf_set(els_req64_vf, &wqe->els_req, 0);
  7543. /* And a VFID for word 12 */
  7544. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7545. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7546. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7547. iocbq->iocb.ulpContext);
  7548. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7549. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7550. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7551. if (command_type == ELS_COMMAND_FIP)
  7552. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7553. >> LPFC_FIP_ELS_ID_SHIFT);
  7554. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7555. iocbq->context2)->virt);
  7556. if_type = bf_get(lpfc_sli_intf_if_type,
  7557. &phba->sli4_hba.sli_intf);
  7558. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7559. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7560. *pcmd == ELS_CMD_SCR ||
  7561. *pcmd == ELS_CMD_FDISC ||
  7562. *pcmd == ELS_CMD_LOGO ||
  7563. *pcmd == ELS_CMD_PLOGI)) {
  7564. bf_set(els_req64_sp, &wqe->els_req, 1);
  7565. bf_set(els_req64_sid, &wqe->els_req,
  7566. iocbq->vport->fc_myDID);
  7567. if ((*pcmd == ELS_CMD_FLOGI) &&
  7568. !(phba->fc_topology ==
  7569. LPFC_TOPOLOGY_LOOP))
  7570. bf_set(els_req64_sid, &wqe->els_req, 0);
  7571. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7572. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7573. phba->vpi_ids[iocbq->vport->vpi]);
  7574. } else if (pcmd && iocbq->context1) {
  7575. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7576. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7577. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7578. }
  7579. }
  7580. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7581. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7582. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7583. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7584. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7585. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7586. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7587. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7588. wqe->els_req.max_response_payload_len = total_len - xmit_len;
  7589. break;
  7590. case CMD_XMIT_SEQUENCE64_CX:
  7591. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7592. iocbq->iocb.un.ulpWord[3]);
  7593. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7594. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7595. /* The entire sequence is transmitted for this IOCB */
  7596. xmit_len = total_len;
  7597. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7598. if (phba->link_flag & LS_LOOPBACK_MODE)
  7599. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7600. case CMD_XMIT_SEQUENCE64_CR:
  7601. /* word3 iocb=io_tag32 wqe=reserved */
  7602. wqe->xmit_sequence.rsvd3 = 0;
  7603. /* word4 relative_offset memcpy */
  7604. /* word5 r_ctl/df_ctl memcpy */
  7605. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7606. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7607. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7608. LPFC_WQE_IOD_WRITE);
  7609. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7610. LPFC_WQE_LENLOC_WORD12);
  7611. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7612. wqe->xmit_sequence.xmit_len = xmit_len;
  7613. command_type = OTHER_COMMAND;
  7614. break;
  7615. case CMD_XMIT_BCAST64_CN:
  7616. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7617. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7618. /* word4 iocb=rsvd wqe=rsvd */
  7619. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7620. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7621. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7622. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7623. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7624. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7625. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7626. LPFC_WQE_LENLOC_WORD3);
  7627. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7628. break;
  7629. case CMD_FCP_IWRITE64_CR:
  7630. command_type = FCP_COMMAND_DATA_OUT;
  7631. /* word3 iocb=iotag wqe=payload_offset_len */
  7632. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7633. bf_set(payload_offset_len, &wqe->fcp_iwrite,
  7634. xmit_len + sizeof(struct fcp_rsp));
  7635. bf_set(cmd_buff_len, &wqe->fcp_iwrite,
  7636. 0);
  7637. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7638. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7639. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7640. iocbq->iocb.ulpFCP2Rcvy);
  7641. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7642. /* Always open the exchange */
  7643. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7644. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7645. LPFC_WQE_LENLOC_WORD4);
  7646. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7647. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7648. if (iocbq->iocb_flag & LPFC_IO_OAS) {
  7649. bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
  7650. bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
  7651. if (iocbq->priority) {
  7652. bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
  7653. (iocbq->priority << 1));
  7654. } else {
  7655. bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
  7656. (phba->cfg_XLanePriority << 1));
  7657. }
  7658. }
  7659. /* Note, word 10 is already initialized to 0 */
  7660. if (phba->fcp_embed_io) {
  7661. struct lpfc_scsi_buf *lpfc_cmd;
  7662. struct sli4_sge *sgl;
  7663. union lpfc_wqe128 *wqe128;
  7664. struct fcp_cmnd *fcp_cmnd;
  7665. uint32_t *ptr;
  7666. /* 128 byte wqe support here */
  7667. wqe128 = (union lpfc_wqe128 *)wqe;
  7668. lpfc_cmd = iocbq->context1;
  7669. sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  7670. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  7671. /* Word 0-2 - FCP_CMND */
  7672. wqe128->generic.bde.tus.f.bdeFlags =
  7673. BUFF_TYPE_BDE_IMMED;
  7674. wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
  7675. wqe128->generic.bde.addrHigh = 0;
  7676. wqe128->generic.bde.addrLow = 88; /* Word 22 */
  7677. bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
  7678. /* Word 22-29 FCP CMND Payload */
  7679. ptr = &wqe128->words[22];
  7680. memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
  7681. }
  7682. break;
  7683. case CMD_FCP_IREAD64_CR:
  7684. /* word3 iocb=iotag wqe=payload_offset_len */
  7685. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7686. bf_set(payload_offset_len, &wqe->fcp_iread,
  7687. xmit_len + sizeof(struct fcp_rsp));
  7688. bf_set(cmd_buff_len, &wqe->fcp_iread,
  7689. 0);
  7690. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7691. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7692. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7693. iocbq->iocb.ulpFCP2Rcvy);
  7694. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7695. /* Always open the exchange */
  7696. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7697. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7698. LPFC_WQE_LENLOC_WORD4);
  7699. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7700. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7701. if (iocbq->iocb_flag & LPFC_IO_OAS) {
  7702. bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
  7703. bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
  7704. if (iocbq->priority) {
  7705. bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
  7706. (iocbq->priority << 1));
  7707. } else {
  7708. bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
  7709. (phba->cfg_XLanePriority << 1));
  7710. }
  7711. }
  7712. /* Note, word 10 is already initialized to 0 */
  7713. if (phba->fcp_embed_io) {
  7714. struct lpfc_scsi_buf *lpfc_cmd;
  7715. struct sli4_sge *sgl;
  7716. union lpfc_wqe128 *wqe128;
  7717. struct fcp_cmnd *fcp_cmnd;
  7718. uint32_t *ptr;
  7719. /* 128 byte wqe support here */
  7720. wqe128 = (union lpfc_wqe128 *)wqe;
  7721. lpfc_cmd = iocbq->context1;
  7722. sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  7723. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  7724. /* Word 0-2 - FCP_CMND */
  7725. wqe128->generic.bde.tus.f.bdeFlags =
  7726. BUFF_TYPE_BDE_IMMED;
  7727. wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
  7728. wqe128->generic.bde.addrHigh = 0;
  7729. wqe128->generic.bde.addrLow = 88; /* Word 22 */
  7730. bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
  7731. /* Word 22-29 FCP CMND Payload */
  7732. ptr = &wqe128->words[22];
  7733. memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
  7734. }
  7735. break;
  7736. case CMD_FCP_ICMND64_CR:
  7737. /* word3 iocb=iotag wqe=payload_offset_len */
  7738. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7739. bf_set(payload_offset_len, &wqe->fcp_icmd,
  7740. xmit_len + sizeof(struct fcp_rsp));
  7741. bf_set(cmd_buff_len, &wqe->fcp_icmd,
  7742. 0);
  7743. /* word3 iocb=IO_TAG wqe=reserved */
  7744. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7745. /* Always open the exchange */
  7746. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7747. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7748. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7749. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7750. LPFC_WQE_LENLOC_NONE);
  7751. bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
  7752. iocbq->iocb.ulpFCP2Rcvy);
  7753. if (iocbq->iocb_flag & LPFC_IO_OAS) {
  7754. bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
  7755. bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
  7756. if (iocbq->priority) {
  7757. bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
  7758. (iocbq->priority << 1));
  7759. } else {
  7760. bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
  7761. (phba->cfg_XLanePriority << 1));
  7762. }
  7763. }
  7764. /* Note, word 10 is already initialized to 0 */
  7765. if (phba->fcp_embed_io) {
  7766. struct lpfc_scsi_buf *lpfc_cmd;
  7767. struct sli4_sge *sgl;
  7768. union lpfc_wqe128 *wqe128;
  7769. struct fcp_cmnd *fcp_cmnd;
  7770. uint32_t *ptr;
  7771. /* 128 byte wqe support here */
  7772. wqe128 = (union lpfc_wqe128 *)wqe;
  7773. lpfc_cmd = iocbq->context1;
  7774. sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  7775. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  7776. /* Word 0-2 - FCP_CMND */
  7777. wqe128->generic.bde.tus.f.bdeFlags =
  7778. BUFF_TYPE_BDE_IMMED;
  7779. wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
  7780. wqe128->generic.bde.addrHigh = 0;
  7781. wqe128->generic.bde.addrLow = 88; /* Word 22 */
  7782. bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
  7783. /* Word 22-29 FCP CMND Payload */
  7784. ptr = &wqe128->words[22];
  7785. memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
  7786. }
  7787. break;
  7788. case CMD_GEN_REQUEST64_CR:
  7789. /* For this command calculate the xmit length of the
  7790. * request bde.
  7791. */
  7792. xmit_len = 0;
  7793. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7794. sizeof(struct ulp_bde64);
  7795. for (i = 0; i < numBdes; i++) {
  7796. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7797. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7798. break;
  7799. xmit_len += bde.tus.f.bdeSize;
  7800. }
  7801. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7802. wqe->gen_req.request_payload_len = xmit_len;
  7803. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7804. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7805. /* word6 context tag copied in memcpy */
  7806. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7807. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7808. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7809. "2015 Invalid CT %x command 0x%x\n",
  7810. ct, iocbq->iocb.ulpCommand);
  7811. return IOCB_ERROR;
  7812. }
  7813. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7814. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7815. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7816. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7817. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7818. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7819. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7820. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7821. wqe->gen_req.max_response_payload_len = total_len - xmit_len;
  7822. command_type = OTHER_COMMAND;
  7823. break;
  7824. case CMD_XMIT_ELS_RSP64_CX:
  7825. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7826. /* words0-2 BDE memcpy */
  7827. /* word3 iocb=iotag32 wqe=response_payload_len */
  7828. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7829. /* word4 */
  7830. wqe->xmit_els_rsp.word4 = 0;
  7831. /* word5 iocb=rsvd wge=did */
  7832. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7833. iocbq->iocb.un.xseq64.xmit_els_remoteID);
  7834. if_type = bf_get(lpfc_sli_intf_if_type,
  7835. &phba->sli4_hba.sli_intf);
  7836. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7837. if (iocbq->vport->fc_flag & FC_PT2PT) {
  7838. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7839. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7840. iocbq->vport->fc_myDID);
  7841. if (iocbq->vport->fc_myDID == Fabric_DID) {
  7842. bf_set(wqe_els_did,
  7843. &wqe->xmit_els_rsp.wqe_dest, 0);
  7844. }
  7845. }
  7846. }
  7847. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7848. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7849. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7850. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7851. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7852. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7853. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7854. phba->vpi_ids[iocbq->vport->vpi]);
  7855. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7856. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7857. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7858. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7859. LPFC_WQE_LENLOC_WORD3);
  7860. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7861. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7862. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7863. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7864. iocbq->context2)->virt);
  7865. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7866. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7867. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7868. iocbq->vport->fc_myDID);
  7869. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
  7870. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7871. phba->vpi_ids[phba->pport->vpi]);
  7872. }
  7873. command_type = OTHER_COMMAND;
  7874. break;
  7875. case CMD_CLOSE_XRI_CN:
  7876. case CMD_ABORT_XRI_CN:
  7877. case CMD_ABORT_XRI_CX:
  7878. /* words 0-2 memcpy should be 0 rserved */
  7879. /* port will send abts */
  7880. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7881. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7882. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7883. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7884. } else
  7885. fip = 0;
  7886. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7887. /*
  7888. * The link is down, or the command was ELS_FIP
  7889. * so the fw does not need to send abts
  7890. * on the wire.
  7891. */
  7892. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7893. else
  7894. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7895. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7896. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7897. wqe->abort_cmd.rsrvd5 = 0;
  7898. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7899. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7900. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7901. /*
  7902. * The abort handler will send us CMD_ABORT_XRI_CN or
  7903. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7904. */
  7905. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7906. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7907. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7908. LPFC_WQE_LENLOC_NONE);
  7909. cmnd = CMD_ABORT_XRI_CX;
  7910. command_type = OTHER_COMMAND;
  7911. xritag = 0;
  7912. break;
  7913. case CMD_XMIT_BLS_RSP64_CX:
  7914. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7915. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7916. * we re-construct this WQE here based on information in
  7917. * iocbq from scratch.
  7918. */
  7919. memset(wqe, 0, sizeof(union lpfc_wqe));
  7920. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7921. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7922. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7923. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7924. LPFC_ABTS_UNSOL_INT) {
  7925. /* ABTS sent by initiator to CT exchange, the
  7926. * RX_ID field will be filled with the newly
  7927. * allocated responder XRI.
  7928. */
  7929. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7930. iocbq->sli4_xritag);
  7931. } else {
  7932. /* ABTS sent by responder to CT exchange, the
  7933. * RX_ID field will be filled with the responder
  7934. * RX_ID from ABTS.
  7935. */
  7936. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7937. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7938. }
  7939. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7940. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7941. /* Use CT=VPI */
  7942. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7943. ndlp->nlp_DID);
  7944. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7945. iocbq->iocb.ulpContext);
  7946. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7947. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7948. phba->vpi_ids[phba->pport->vpi]);
  7949. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7950. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7951. LPFC_WQE_LENLOC_NONE);
  7952. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7953. command_type = OTHER_COMMAND;
  7954. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7955. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7956. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7957. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7958. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7959. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7960. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7961. }
  7962. break;
  7963. case CMD_XRI_ABORTED_CX:
  7964. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7965. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7966. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7967. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7968. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7969. default:
  7970. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7971. "2014 Invalid command 0x%x\n",
  7972. iocbq->iocb.ulpCommand);
  7973. return IOCB_ERROR;
  7974. break;
  7975. }
  7976. if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
  7977. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
  7978. else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
  7979. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
  7980. else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
  7981. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
  7982. iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
  7983. LPFC_IO_DIF_INSERT);
  7984. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7985. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7986. wqe->generic.wqe_com.abort_tag = abort_tag;
  7987. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7988. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7989. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7990. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7991. return 0;
  7992. }
  7993. /**
  7994. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7995. * @phba: Pointer to HBA context object.
  7996. * @ring_number: SLI ring number to issue iocb on.
  7997. * @piocb: Pointer to command iocb.
  7998. * @flag: Flag indicating if this command can be put into txq.
  7999. *
  8000. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  8001. * an iocb command to an HBA with SLI-4 interface spec.
  8002. *
  8003. * This function is called with hbalock held. The function will return success
  8004. * after it successfully submit the iocb to firmware or after adding to the
  8005. * txq.
  8006. **/
  8007. static int
  8008. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  8009. struct lpfc_iocbq *piocb, uint32_t flag)
  8010. {
  8011. struct lpfc_sglq *sglq;
  8012. union lpfc_wqe *wqe;
  8013. union lpfc_wqe128 wqe128;
  8014. struct lpfc_queue *wq;
  8015. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  8016. lockdep_assert_held(&phba->hbalock);
  8017. /*
  8018. * The WQE can be either 64 or 128 bytes,
  8019. * so allocate space on the stack assuming the largest.
  8020. */
  8021. wqe = (union lpfc_wqe *)&wqe128;
  8022. if (piocb->sli4_xritag == NO_XRI) {
  8023. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  8024. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  8025. sglq = NULL;
  8026. else {
  8027. if (!list_empty(&pring->txq)) {
  8028. if (!(flag & SLI_IOCB_RET_IOCB)) {
  8029. __lpfc_sli_ringtx_put(phba,
  8030. pring, piocb);
  8031. return IOCB_SUCCESS;
  8032. } else {
  8033. return IOCB_BUSY;
  8034. }
  8035. } else {
  8036. sglq = __lpfc_sli_get_sglq(phba, piocb);
  8037. if (!sglq) {
  8038. if (!(flag & SLI_IOCB_RET_IOCB)) {
  8039. __lpfc_sli_ringtx_put(phba,
  8040. pring,
  8041. piocb);
  8042. return IOCB_SUCCESS;
  8043. } else
  8044. return IOCB_BUSY;
  8045. }
  8046. }
  8047. }
  8048. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  8049. /* These IO's already have an XRI and a mapped sgl. */
  8050. sglq = NULL;
  8051. } else {
  8052. /*
  8053. * This is a continuation of a commandi,(CX) so this
  8054. * sglq is on the active list
  8055. */
  8056. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
  8057. if (!sglq)
  8058. return IOCB_ERROR;
  8059. }
  8060. if (sglq) {
  8061. piocb->sli4_lxritag = sglq->sli4_lxritag;
  8062. piocb->sli4_xritag = sglq->sli4_xritag;
  8063. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  8064. return IOCB_ERROR;
  8065. }
  8066. if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
  8067. return IOCB_ERROR;
  8068. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  8069. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  8070. if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
  8071. wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
  8072. } else {
  8073. wq = phba->sli4_hba.oas_wq;
  8074. }
  8075. if (lpfc_sli4_wq_put(wq, wqe))
  8076. return IOCB_ERROR;
  8077. } else {
  8078. if (unlikely(!phba->sli4_hba.els_wq))
  8079. return IOCB_ERROR;
  8080. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
  8081. return IOCB_ERROR;
  8082. }
  8083. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  8084. return 0;
  8085. }
  8086. /**
  8087. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  8088. *
  8089. * This routine wraps the actual lockless version for issusing IOCB function
  8090. * pointer from the lpfc_hba struct.
  8091. *
  8092. * Return codes:
  8093. * IOCB_ERROR - Error
  8094. * IOCB_SUCCESS - Success
  8095. * IOCB_BUSY - Busy
  8096. **/
  8097. int
  8098. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  8099. struct lpfc_iocbq *piocb, uint32_t flag)
  8100. {
  8101. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  8102. }
  8103. /**
  8104. * lpfc_sli_api_table_setup - Set up sli api function jump table
  8105. * @phba: The hba struct for which this call is being executed.
  8106. * @dev_grp: The HBA PCI-Device group number.
  8107. *
  8108. * This routine sets up the SLI interface API function jump table in @phba
  8109. * struct.
  8110. * Returns: 0 - success, -ENODEV - failure.
  8111. **/
  8112. int
  8113. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  8114. {
  8115. switch (dev_grp) {
  8116. case LPFC_PCI_DEV_LP:
  8117. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  8118. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  8119. break;
  8120. case LPFC_PCI_DEV_OC:
  8121. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  8122. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  8123. break;
  8124. default:
  8125. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8126. "1419 Invalid HBA PCI-device group: 0x%x\n",
  8127. dev_grp);
  8128. return -ENODEV;
  8129. break;
  8130. }
  8131. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  8132. return 0;
  8133. }
  8134. /**
  8135. * lpfc_sli_calc_ring - Calculates which ring to use
  8136. * @phba: Pointer to HBA context object.
  8137. * @ring_number: Initial ring
  8138. * @piocb: Pointer to command iocb.
  8139. *
  8140. * For SLI4, FCP IO can deferred to one fo many WQs, based on
  8141. * fcp_wqidx, thus we need to calculate the corresponding ring.
  8142. * Since ABORTS must go on the same WQ of the command they are
  8143. * aborting, we use command's fcp_wqidx.
  8144. */
  8145. static int
  8146. lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
  8147. struct lpfc_iocbq *piocb)
  8148. {
  8149. if (phba->sli_rev < LPFC_SLI_REV4)
  8150. return ring_number;
  8151. if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
  8152. if (!(phba->cfg_fof) ||
  8153. (!(piocb->iocb_flag & LPFC_IO_FOF))) {
  8154. if (unlikely(!phba->sli4_hba.fcp_wq))
  8155. return LPFC_HBA_ERROR;
  8156. /*
  8157. * for abort iocb fcp_wqidx should already
  8158. * be setup based on what work queue we used.
  8159. */
  8160. if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
  8161. piocb->fcp_wqidx =
  8162. lpfc_sli4_scmd_to_wqidx_distr(phba,
  8163. piocb->context1);
  8164. ring_number = MAX_SLI3_CONFIGURED_RINGS +
  8165. piocb->fcp_wqidx;
  8166. } else {
  8167. if (unlikely(!phba->sli4_hba.oas_wq))
  8168. return LPFC_HBA_ERROR;
  8169. piocb->fcp_wqidx = 0;
  8170. ring_number = LPFC_FCP_OAS_RING;
  8171. }
  8172. }
  8173. return ring_number;
  8174. }
  8175. /**
  8176. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  8177. * @phba: Pointer to HBA context object.
  8178. * @pring: Pointer to driver SLI ring object.
  8179. * @piocb: Pointer to command iocb.
  8180. * @flag: Flag indicating if this command can be put into txq.
  8181. *
  8182. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  8183. * function. This function gets the hbalock and calls
  8184. * __lpfc_sli_issue_iocb function and will return the error returned
  8185. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  8186. * functions which do not hold hbalock.
  8187. **/
  8188. int
  8189. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  8190. struct lpfc_iocbq *piocb, uint32_t flag)
  8191. {
  8192. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  8193. struct lpfc_sli_ring *pring;
  8194. struct lpfc_queue *fpeq;
  8195. struct lpfc_eqe *eqe;
  8196. unsigned long iflags;
  8197. int rc, idx;
  8198. if (phba->sli_rev == LPFC_SLI_REV4) {
  8199. ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
  8200. if (unlikely(ring_number == LPFC_HBA_ERROR))
  8201. return IOCB_ERROR;
  8202. idx = piocb->fcp_wqidx;
  8203. pring = &phba->sli.ring[ring_number];
  8204. spin_lock_irqsave(&pring->ring_lock, iflags);
  8205. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  8206. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  8207. if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
  8208. fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
  8209. if (atomic_dec_and_test(&fcp_eq_hdl->
  8210. fcp_eq_in_use)) {
  8211. /* Get associated EQ with this index */
  8212. fpeq = phba->sli4_hba.hba_eq[idx];
  8213. /* Turn off interrupts from this EQ */
  8214. lpfc_sli4_eq_clr_intr(fpeq);
  8215. /*
  8216. * Process all the events on FCP EQ
  8217. */
  8218. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  8219. lpfc_sli4_hba_handle_eqe(phba,
  8220. eqe, idx);
  8221. fpeq->EQ_processed++;
  8222. }
  8223. /* Always clear and re-arm the EQ */
  8224. lpfc_sli4_eq_release(fpeq,
  8225. LPFC_QUEUE_REARM);
  8226. }
  8227. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  8228. }
  8229. } else {
  8230. /* For now, SLI2/3 will still use hbalock */
  8231. spin_lock_irqsave(&phba->hbalock, iflags);
  8232. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  8233. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8234. }
  8235. return rc;
  8236. }
  8237. /**
  8238. * lpfc_extra_ring_setup - Extra ring setup function
  8239. * @phba: Pointer to HBA context object.
  8240. *
  8241. * This function is called while driver attaches with the
  8242. * HBA to setup the extra ring. The extra ring is used
  8243. * only when driver needs to support target mode functionality
  8244. * or IP over FC functionalities.
  8245. *
  8246. * This function is called with no lock held.
  8247. **/
  8248. static int
  8249. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  8250. {
  8251. struct lpfc_sli *psli;
  8252. struct lpfc_sli_ring *pring;
  8253. psli = &phba->sli;
  8254. /* Adjust cmd/rsp ring iocb entries more evenly */
  8255. /* Take some away from the FCP ring */
  8256. pring = &psli->ring[psli->fcp_ring];
  8257. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  8258. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  8259. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  8260. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  8261. /* and give them to the extra ring */
  8262. pring = &psli->ring[psli->extra_ring];
  8263. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  8264. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  8265. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  8266. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  8267. /* Setup default profile for this ring */
  8268. pring->iotag_max = 4096;
  8269. pring->num_mask = 1;
  8270. pring->prt[0].profile = 0; /* Mask 0 */
  8271. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  8272. pring->prt[0].type = phba->cfg_multi_ring_type;
  8273. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  8274. return 0;
  8275. }
  8276. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  8277. * @phba: Pointer to HBA context object.
  8278. * @iocbq: Pointer to iocb object.
  8279. *
  8280. * The async_event handler calls this routine when it receives
  8281. * an ASYNC_STATUS_CN event from the port. The port generates
  8282. * this event when an Abort Sequence request to an rport fails
  8283. * twice in succession. The abort could be originated by the
  8284. * driver or by the port. The ABTS could have been for an ELS
  8285. * or FCP IO. The port only generates this event when an ABTS
  8286. * fails to complete after one retry.
  8287. */
  8288. static void
  8289. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  8290. struct lpfc_iocbq *iocbq)
  8291. {
  8292. struct lpfc_nodelist *ndlp = NULL;
  8293. uint16_t rpi = 0, vpi = 0;
  8294. struct lpfc_vport *vport = NULL;
  8295. /* The rpi in the ulpContext is vport-sensitive. */
  8296. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  8297. rpi = iocbq->iocb.ulpContext;
  8298. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8299. "3092 Port generated ABTS async event "
  8300. "on vpi %d rpi %d status 0x%x\n",
  8301. vpi, rpi, iocbq->iocb.ulpStatus);
  8302. vport = lpfc_find_vport_by_vpid(phba, vpi);
  8303. if (!vport)
  8304. goto err_exit;
  8305. ndlp = lpfc_findnode_rpi(vport, rpi);
  8306. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  8307. goto err_exit;
  8308. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  8309. lpfc_sli_abts_recover_port(vport, ndlp);
  8310. return;
  8311. err_exit:
  8312. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8313. "3095 Event Context not found, no "
  8314. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  8315. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  8316. vpi, rpi);
  8317. }
  8318. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  8319. * @phba: pointer to HBA context object.
  8320. * @ndlp: nodelist pointer for the impacted rport.
  8321. * @axri: pointer to the wcqe containing the failed exchange.
  8322. *
  8323. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  8324. * port. The port generates this event when an abort exchange request to an
  8325. * rport fails twice in succession with no reply. The abort could be originated
  8326. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  8327. */
  8328. void
  8329. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  8330. struct lpfc_nodelist *ndlp,
  8331. struct sli4_wcqe_xri_aborted *axri)
  8332. {
  8333. struct lpfc_vport *vport;
  8334. uint32_t ext_status = 0;
  8335. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  8336. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8337. "3115 Node Context not found, driver "
  8338. "ignoring abts err event\n");
  8339. return;
  8340. }
  8341. vport = ndlp->vport;
  8342. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8343. "3116 Port generated FCP XRI ABORT event on "
  8344. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  8345. ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
  8346. bf_get(lpfc_wcqe_xa_xri, axri),
  8347. bf_get(lpfc_wcqe_xa_status, axri),
  8348. axri->parameter);
  8349. /*
  8350. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  8351. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  8352. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  8353. */
  8354. ext_status = axri->parameter & IOERR_PARAM_MASK;
  8355. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  8356. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  8357. lpfc_sli_abts_recover_port(vport, ndlp);
  8358. }
  8359. /**
  8360. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  8361. * @phba: Pointer to HBA context object.
  8362. * @pring: Pointer to driver SLI ring object.
  8363. * @iocbq: Pointer to iocb object.
  8364. *
  8365. * This function is called by the slow ring event handler
  8366. * function when there is an ASYNC event iocb in the ring.
  8367. * This function is called with no lock held.
  8368. * Currently this function handles only temperature related
  8369. * ASYNC events. The function decodes the temperature sensor
  8370. * event message and posts events for the management applications.
  8371. **/
  8372. static void
  8373. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  8374. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  8375. {
  8376. IOCB_t *icmd;
  8377. uint16_t evt_code;
  8378. struct temp_event temp_event_data;
  8379. struct Scsi_Host *shost;
  8380. uint32_t *iocb_w;
  8381. icmd = &iocbq->iocb;
  8382. evt_code = icmd->un.asyncstat.evt_code;
  8383. switch (evt_code) {
  8384. case ASYNC_TEMP_WARN:
  8385. case ASYNC_TEMP_SAFE:
  8386. temp_event_data.data = (uint32_t) icmd->ulpContext;
  8387. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  8388. if (evt_code == ASYNC_TEMP_WARN) {
  8389. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  8390. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  8391. "0347 Adapter is very hot, please take "
  8392. "corrective action. temperature : %d Celsius\n",
  8393. (uint32_t) icmd->ulpContext);
  8394. } else {
  8395. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  8396. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  8397. "0340 Adapter temperature is OK now. "
  8398. "temperature : %d Celsius\n",
  8399. (uint32_t) icmd->ulpContext);
  8400. }
  8401. /* Send temperature change event to applications */
  8402. shost = lpfc_shost_from_vport(phba->pport);
  8403. fc_host_post_vendor_event(shost, fc_get_event_number(),
  8404. sizeof(temp_event_data), (char *) &temp_event_data,
  8405. LPFC_NL_VENDOR_ID);
  8406. break;
  8407. case ASYNC_STATUS_CN:
  8408. lpfc_sli_abts_err_handler(phba, iocbq);
  8409. break;
  8410. default:
  8411. iocb_w = (uint32_t *) icmd;
  8412. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8413. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  8414. " evt_code 0x%x\n"
  8415. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  8416. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  8417. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  8418. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  8419. pring->ringno, icmd->un.asyncstat.evt_code,
  8420. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  8421. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  8422. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  8423. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  8424. break;
  8425. }
  8426. }
  8427. /**
  8428. * lpfc_sli_setup - SLI ring setup function
  8429. * @phba: Pointer to HBA context object.
  8430. *
  8431. * lpfc_sli_setup sets up rings of the SLI interface with
  8432. * number of iocbs per ring and iotags. This function is
  8433. * called while driver attach to the HBA and before the
  8434. * interrupts are enabled. So there is no need for locking.
  8435. *
  8436. * This function always returns 0.
  8437. **/
  8438. int
  8439. lpfc_sli_setup(struct lpfc_hba *phba)
  8440. {
  8441. int i, totiocbsize = 0;
  8442. struct lpfc_sli *psli = &phba->sli;
  8443. struct lpfc_sli_ring *pring;
  8444. psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
  8445. if (phba->sli_rev == LPFC_SLI_REV4)
  8446. psli->num_rings += phba->cfg_fcp_io_channel;
  8447. psli->sli_flag = 0;
  8448. psli->fcp_ring = LPFC_FCP_RING;
  8449. psli->next_ring = LPFC_FCP_NEXT_RING;
  8450. psli->extra_ring = LPFC_EXTRA_RING;
  8451. psli->iocbq_lookup = NULL;
  8452. psli->iocbq_lookup_len = 0;
  8453. psli->last_iotag = 0;
  8454. for (i = 0; i < psli->num_rings; i++) {
  8455. pring = &psli->ring[i];
  8456. switch (i) {
  8457. case LPFC_FCP_RING: /* ring 0 - FCP */
  8458. /* numCiocb and numRiocb are used in config_port */
  8459. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  8460. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  8461. pring->sli.sli3.numCiocb +=
  8462. SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  8463. pring->sli.sli3.numRiocb +=
  8464. SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  8465. pring->sli.sli3.numCiocb +=
  8466. SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  8467. pring->sli.sli3.numRiocb +=
  8468. SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  8469. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8470. SLI3_IOCB_CMD_SIZE :
  8471. SLI2_IOCB_CMD_SIZE;
  8472. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8473. SLI3_IOCB_RSP_SIZE :
  8474. SLI2_IOCB_RSP_SIZE;
  8475. pring->iotag_ctr = 0;
  8476. pring->iotag_max =
  8477. (phba->cfg_hba_queue_depth * 2);
  8478. pring->fast_iotag = pring->iotag_max;
  8479. pring->num_mask = 0;
  8480. break;
  8481. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  8482. /* numCiocb and numRiocb are used in config_port */
  8483. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  8484. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  8485. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8486. SLI3_IOCB_CMD_SIZE :
  8487. SLI2_IOCB_CMD_SIZE;
  8488. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8489. SLI3_IOCB_RSP_SIZE :
  8490. SLI2_IOCB_RSP_SIZE;
  8491. pring->iotag_max = phba->cfg_hba_queue_depth;
  8492. pring->num_mask = 0;
  8493. break;
  8494. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  8495. /* numCiocb and numRiocb are used in config_port */
  8496. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  8497. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  8498. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8499. SLI3_IOCB_CMD_SIZE :
  8500. SLI2_IOCB_CMD_SIZE;
  8501. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8502. SLI3_IOCB_RSP_SIZE :
  8503. SLI2_IOCB_RSP_SIZE;
  8504. pring->fast_iotag = 0;
  8505. pring->iotag_ctr = 0;
  8506. pring->iotag_max = 4096;
  8507. pring->lpfc_sli_rcv_async_status =
  8508. lpfc_sli_async_event_handler;
  8509. pring->num_mask = LPFC_MAX_RING_MASK;
  8510. pring->prt[0].profile = 0; /* Mask 0 */
  8511. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  8512. pring->prt[0].type = FC_TYPE_ELS;
  8513. pring->prt[0].lpfc_sli_rcv_unsol_event =
  8514. lpfc_els_unsol_event;
  8515. pring->prt[1].profile = 0; /* Mask 1 */
  8516. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  8517. pring->prt[1].type = FC_TYPE_ELS;
  8518. pring->prt[1].lpfc_sli_rcv_unsol_event =
  8519. lpfc_els_unsol_event;
  8520. pring->prt[2].profile = 0; /* Mask 2 */
  8521. /* NameServer Inquiry */
  8522. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  8523. /* NameServer */
  8524. pring->prt[2].type = FC_TYPE_CT;
  8525. pring->prt[2].lpfc_sli_rcv_unsol_event =
  8526. lpfc_ct_unsol_event;
  8527. pring->prt[3].profile = 0; /* Mask 3 */
  8528. /* NameServer response */
  8529. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  8530. /* NameServer */
  8531. pring->prt[3].type = FC_TYPE_CT;
  8532. pring->prt[3].lpfc_sli_rcv_unsol_event =
  8533. lpfc_ct_unsol_event;
  8534. break;
  8535. }
  8536. totiocbsize += (pring->sli.sli3.numCiocb *
  8537. pring->sli.sli3.sizeCiocb) +
  8538. (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
  8539. }
  8540. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  8541. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  8542. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  8543. "SLI2 SLIM Data: x%x x%lx\n",
  8544. phba->brd_no, totiocbsize,
  8545. (unsigned long) MAX_SLIM_IOCB_SIZE);
  8546. }
  8547. if (phba->cfg_multi_ring_support == 2)
  8548. lpfc_extra_ring_setup(phba);
  8549. return 0;
  8550. }
  8551. /**
  8552. * lpfc_sli_queue_setup - Queue initialization function
  8553. * @phba: Pointer to HBA context object.
  8554. *
  8555. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  8556. * ring. This function also initializes ring indices of each ring.
  8557. * This function is called during the initialization of the SLI
  8558. * interface of an HBA.
  8559. * This function is called with no lock held and always returns
  8560. * 1.
  8561. **/
  8562. int
  8563. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  8564. {
  8565. struct lpfc_sli *psli;
  8566. struct lpfc_sli_ring *pring;
  8567. int i;
  8568. psli = &phba->sli;
  8569. spin_lock_irq(&phba->hbalock);
  8570. INIT_LIST_HEAD(&psli->mboxq);
  8571. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  8572. /* Initialize list headers for txq and txcmplq as double linked lists */
  8573. for (i = 0; i < psli->num_rings; i++) {
  8574. pring = &psli->ring[i];
  8575. pring->ringno = i;
  8576. pring->sli.sli3.next_cmdidx = 0;
  8577. pring->sli.sli3.local_getidx = 0;
  8578. pring->sli.sli3.cmdidx = 0;
  8579. pring->flag = 0;
  8580. INIT_LIST_HEAD(&pring->txq);
  8581. INIT_LIST_HEAD(&pring->txcmplq);
  8582. INIT_LIST_HEAD(&pring->iocb_continueq);
  8583. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  8584. INIT_LIST_HEAD(&pring->postbufq);
  8585. spin_lock_init(&pring->ring_lock);
  8586. }
  8587. spin_unlock_irq(&phba->hbalock);
  8588. return 1;
  8589. }
  8590. /**
  8591. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  8592. * @phba: Pointer to HBA context object.
  8593. *
  8594. * This routine flushes the mailbox command subsystem. It will unconditionally
  8595. * flush all the mailbox commands in the three possible stages in the mailbox
  8596. * command sub-system: pending mailbox command queue; the outstanding mailbox
  8597. * command; and completed mailbox command queue. It is caller's responsibility
  8598. * to make sure that the driver is in the proper state to flush the mailbox
  8599. * command sub-system. Namely, the posting of mailbox commands into the
  8600. * pending mailbox command queue from the various clients must be stopped;
  8601. * either the HBA is in a state that it will never works on the outstanding
  8602. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  8603. * mailbox command has been completed.
  8604. **/
  8605. static void
  8606. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  8607. {
  8608. LIST_HEAD(completions);
  8609. struct lpfc_sli *psli = &phba->sli;
  8610. LPFC_MBOXQ_t *pmb;
  8611. unsigned long iflag;
  8612. /* Flush all the mailbox commands in the mbox system */
  8613. spin_lock_irqsave(&phba->hbalock, iflag);
  8614. /* The pending mailbox command queue */
  8615. list_splice_init(&phba->sli.mboxq, &completions);
  8616. /* The outstanding active mailbox command */
  8617. if (psli->mbox_active) {
  8618. list_add_tail(&psli->mbox_active->list, &completions);
  8619. psli->mbox_active = NULL;
  8620. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  8621. }
  8622. /* The completed mailbox command queue */
  8623. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  8624. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8625. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  8626. while (!list_empty(&completions)) {
  8627. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  8628. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  8629. if (pmb->mbox_cmpl)
  8630. pmb->mbox_cmpl(phba, pmb);
  8631. }
  8632. }
  8633. /**
  8634. * lpfc_sli_host_down - Vport cleanup function
  8635. * @vport: Pointer to virtual port object.
  8636. *
  8637. * lpfc_sli_host_down is called to clean up the resources
  8638. * associated with a vport before destroying virtual
  8639. * port data structures.
  8640. * This function does following operations:
  8641. * - Free discovery resources associated with this virtual
  8642. * port.
  8643. * - Free iocbs associated with this virtual port in
  8644. * the txq.
  8645. * - Send abort for all iocb commands associated with this
  8646. * vport in txcmplq.
  8647. *
  8648. * This function is called with no lock held and always returns 1.
  8649. **/
  8650. int
  8651. lpfc_sli_host_down(struct lpfc_vport *vport)
  8652. {
  8653. LIST_HEAD(completions);
  8654. struct lpfc_hba *phba = vport->phba;
  8655. struct lpfc_sli *psli = &phba->sli;
  8656. struct lpfc_sli_ring *pring;
  8657. struct lpfc_iocbq *iocb, *next_iocb;
  8658. int i;
  8659. unsigned long flags = 0;
  8660. uint16_t prev_pring_flag;
  8661. lpfc_cleanup_discovery_resources(vport);
  8662. spin_lock_irqsave(&phba->hbalock, flags);
  8663. for (i = 0; i < psli->num_rings; i++) {
  8664. pring = &psli->ring[i];
  8665. prev_pring_flag = pring->flag;
  8666. /* Only slow rings */
  8667. if (pring->ringno == LPFC_ELS_RING) {
  8668. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8669. /* Set the lpfc data pending flag */
  8670. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8671. }
  8672. /*
  8673. * Error everything on the txq since these iocbs have not been
  8674. * given to the FW yet.
  8675. */
  8676. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  8677. if (iocb->vport != vport)
  8678. continue;
  8679. list_move_tail(&iocb->list, &completions);
  8680. }
  8681. /* Next issue ABTS for everything on the txcmplq */
  8682. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8683. list) {
  8684. if (iocb->vport != vport)
  8685. continue;
  8686. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8687. }
  8688. pring->flag = prev_pring_flag;
  8689. }
  8690. spin_unlock_irqrestore(&phba->hbalock, flags);
  8691. /* Cancel all the IOCBs from the completions list */
  8692. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8693. IOERR_SLI_DOWN);
  8694. return 1;
  8695. }
  8696. /**
  8697. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8698. * @phba: Pointer to HBA context object.
  8699. *
  8700. * This function cleans up all iocb, buffers, mailbox commands
  8701. * while shutting down the HBA. This function is called with no
  8702. * lock held and always returns 1.
  8703. * This function does the following to cleanup driver resources:
  8704. * - Free discovery resources for each virtual port
  8705. * - Cleanup any pending fabric iocbs
  8706. * - Iterate through the iocb txq and free each entry
  8707. * in the list.
  8708. * - Free up any buffer posted to the HBA
  8709. * - Free mailbox commands in the mailbox queue.
  8710. **/
  8711. int
  8712. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8713. {
  8714. LIST_HEAD(completions);
  8715. struct lpfc_sli *psli = &phba->sli;
  8716. struct lpfc_sli_ring *pring;
  8717. struct lpfc_dmabuf *buf_ptr;
  8718. unsigned long flags = 0;
  8719. int i;
  8720. /* Shutdown the mailbox command sub-system */
  8721. lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
  8722. lpfc_hba_down_prep(phba);
  8723. lpfc_fabric_abort_hba(phba);
  8724. spin_lock_irqsave(&phba->hbalock, flags);
  8725. for (i = 0; i < psli->num_rings; i++) {
  8726. pring = &psli->ring[i];
  8727. /* Only slow rings */
  8728. if (pring->ringno == LPFC_ELS_RING) {
  8729. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8730. /* Set the lpfc data pending flag */
  8731. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8732. }
  8733. /*
  8734. * Error everything on the txq since these iocbs have not been
  8735. * given to the FW yet.
  8736. */
  8737. list_splice_init(&pring->txq, &completions);
  8738. }
  8739. spin_unlock_irqrestore(&phba->hbalock, flags);
  8740. /* Cancel all the IOCBs from the completions list */
  8741. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8742. IOERR_SLI_DOWN);
  8743. spin_lock_irqsave(&phba->hbalock, flags);
  8744. list_splice_init(&phba->elsbuf, &completions);
  8745. phba->elsbuf_cnt = 0;
  8746. phba->elsbuf_prev_cnt = 0;
  8747. spin_unlock_irqrestore(&phba->hbalock, flags);
  8748. while (!list_empty(&completions)) {
  8749. list_remove_head(&completions, buf_ptr,
  8750. struct lpfc_dmabuf, list);
  8751. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8752. kfree(buf_ptr);
  8753. }
  8754. /* Return any active mbox cmds */
  8755. del_timer_sync(&psli->mbox_tmo);
  8756. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8757. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8758. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8759. return 1;
  8760. }
  8761. /**
  8762. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8763. * @srcp: Source memory pointer.
  8764. * @destp: Destination memory pointer.
  8765. * @cnt: Number of words required to be copied.
  8766. *
  8767. * This function is used for copying data between driver memory
  8768. * and the SLI memory. This function also changes the endianness
  8769. * of each word if native endianness is different from SLI
  8770. * endianness. This function can be called with or without
  8771. * lock.
  8772. **/
  8773. void
  8774. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8775. {
  8776. uint32_t *src = srcp;
  8777. uint32_t *dest = destp;
  8778. uint32_t ldata;
  8779. int i;
  8780. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8781. ldata = *src;
  8782. ldata = le32_to_cpu(ldata);
  8783. *dest = ldata;
  8784. src++;
  8785. dest++;
  8786. }
  8787. }
  8788. /**
  8789. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8790. * @srcp: Source memory pointer.
  8791. * @destp: Destination memory pointer.
  8792. * @cnt: Number of words required to be copied.
  8793. *
  8794. * This function is used for copying data between a data structure
  8795. * with big endian representation to local endianness.
  8796. * This function can be called with or without lock.
  8797. **/
  8798. void
  8799. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8800. {
  8801. uint32_t *src = srcp;
  8802. uint32_t *dest = destp;
  8803. uint32_t ldata;
  8804. int i;
  8805. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8806. ldata = *src;
  8807. ldata = be32_to_cpu(ldata);
  8808. *dest = ldata;
  8809. src++;
  8810. dest++;
  8811. }
  8812. }
  8813. /**
  8814. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8815. * @phba: Pointer to HBA context object.
  8816. * @pring: Pointer to driver SLI ring object.
  8817. * @mp: Pointer to driver buffer object.
  8818. *
  8819. * This function is called with no lock held.
  8820. * It always return zero after adding the buffer to the postbufq
  8821. * buffer list.
  8822. **/
  8823. int
  8824. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8825. struct lpfc_dmabuf *mp)
  8826. {
  8827. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8828. later */
  8829. spin_lock_irq(&phba->hbalock);
  8830. list_add_tail(&mp->list, &pring->postbufq);
  8831. pring->postbufq_cnt++;
  8832. spin_unlock_irq(&phba->hbalock);
  8833. return 0;
  8834. }
  8835. /**
  8836. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8837. * @phba: Pointer to HBA context object.
  8838. *
  8839. * When HBQ is enabled, buffers are searched based on tags. This function
  8840. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8841. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8842. * does not conflict with tags of buffer posted for unsolicited events.
  8843. * The function returns the allocated tag. The function is called with
  8844. * no locks held.
  8845. **/
  8846. uint32_t
  8847. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8848. {
  8849. spin_lock_irq(&phba->hbalock);
  8850. phba->buffer_tag_count++;
  8851. /*
  8852. * Always set the QUE_BUFTAG_BIT to distiguish between
  8853. * a tag assigned by HBQ.
  8854. */
  8855. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8856. spin_unlock_irq(&phba->hbalock);
  8857. return phba->buffer_tag_count;
  8858. }
  8859. /**
  8860. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8861. * @phba: Pointer to HBA context object.
  8862. * @pring: Pointer to driver SLI ring object.
  8863. * @tag: Buffer tag.
  8864. *
  8865. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8866. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8867. * iocb is posted to the response ring with the tag of the buffer.
  8868. * This function searches the pring->postbufq list using the tag
  8869. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8870. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8871. * buffer is returned to the caller else NULL is returned.
  8872. * This function is called with no lock held.
  8873. **/
  8874. struct lpfc_dmabuf *
  8875. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8876. uint32_t tag)
  8877. {
  8878. struct lpfc_dmabuf *mp, *next_mp;
  8879. struct list_head *slp = &pring->postbufq;
  8880. /* Search postbufq, from the beginning, looking for a match on tag */
  8881. spin_lock_irq(&phba->hbalock);
  8882. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8883. if (mp->buffer_tag == tag) {
  8884. list_del_init(&mp->list);
  8885. pring->postbufq_cnt--;
  8886. spin_unlock_irq(&phba->hbalock);
  8887. return mp;
  8888. }
  8889. }
  8890. spin_unlock_irq(&phba->hbalock);
  8891. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8892. "0402 Cannot find virtual addr for buffer tag on "
  8893. "ring %d Data x%lx x%p x%p x%x\n",
  8894. pring->ringno, (unsigned long) tag,
  8895. slp->next, slp->prev, pring->postbufq_cnt);
  8896. return NULL;
  8897. }
  8898. /**
  8899. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8900. * @phba: Pointer to HBA context object.
  8901. * @pring: Pointer to driver SLI ring object.
  8902. * @phys: DMA address of the buffer.
  8903. *
  8904. * This function searches the buffer list using the dma_address
  8905. * of unsolicited event to find the driver's lpfc_dmabuf object
  8906. * corresponding to the dma_address. The function returns the
  8907. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8908. * This function is called by the ct and els unsolicited event
  8909. * handlers to get the buffer associated with the unsolicited
  8910. * event.
  8911. *
  8912. * This function is called with no lock held.
  8913. **/
  8914. struct lpfc_dmabuf *
  8915. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8916. dma_addr_t phys)
  8917. {
  8918. struct lpfc_dmabuf *mp, *next_mp;
  8919. struct list_head *slp = &pring->postbufq;
  8920. /* Search postbufq, from the beginning, looking for a match on phys */
  8921. spin_lock_irq(&phba->hbalock);
  8922. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8923. if (mp->phys == phys) {
  8924. list_del_init(&mp->list);
  8925. pring->postbufq_cnt--;
  8926. spin_unlock_irq(&phba->hbalock);
  8927. return mp;
  8928. }
  8929. }
  8930. spin_unlock_irq(&phba->hbalock);
  8931. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8932. "0410 Cannot find virtual addr for mapped buf on "
  8933. "ring %d Data x%llx x%p x%p x%x\n",
  8934. pring->ringno, (unsigned long long)phys,
  8935. slp->next, slp->prev, pring->postbufq_cnt);
  8936. return NULL;
  8937. }
  8938. /**
  8939. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8940. * @phba: Pointer to HBA context object.
  8941. * @cmdiocb: Pointer to driver command iocb object.
  8942. * @rspiocb: Pointer to driver response iocb object.
  8943. *
  8944. * This function is the completion handler for the abort iocbs for
  8945. * ELS commands. This function is called from the ELS ring event
  8946. * handler with no lock held. This function frees memory resources
  8947. * associated with the abort iocb.
  8948. **/
  8949. static void
  8950. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8951. struct lpfc_iocbq *rspiocb)
  8952. {
  8953. IOCB_t *irsp = &rspiocb->iocb;
  8954. uint16_t abort_iotag, abort_context;
  8955. struct lpfc_iocbq *abort_iocb = NULL;
  8956. if (irsp->ulpStatus) {
  8957. /*
  8958. * Assume that the port already completed and returned, or
  8959. * will return the iocb. Just Log the message.
  8960. */
  8961. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8962. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8963. spin_lock_irq(&phba->hbalock);
  8964. if (phba->sli_rev < LPFC_SLI_REV4) {
  8965. if (abort_iotag != 0 &&
  8966. abort_iotag <= phba->sli.last_iotag)
  8967. abort_iocb =
  8968. phba->sli.iocbq_lookup[abort_iotag];
  8969. } else
  8970. /* For sli4 the abort_tag is the XRI,
  8971. * so the abort routine puts the iotag of the iocb
  8972. * being aborted in the context field of the abort
  8973. * IOCB.
  8974. */
  8975. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8976. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8977. "0327 Cannot abort els iocb %p "
  8978. "with tag %x context %x, abort status %x, "
  8979. "abort code %x\n",
  8980. abort_iocb, abort_iotag, abort_context,
  8981. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8982. spin_unlock_irq(&phba->hbalock);
  8983. }
  8984. lpfc_sli_release_iocbq(phba, cmdiocb);
  8985. return;
  8986. }
  8987. /**
  8988. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8989. * @phba: Pointer to HBA context object.
  8990. * @cmdiocb: Pointer to driver command iocb object.
  8991. * @rspiocb: Pointer to driver response iocb object.
  8992. *
  8993. * The function is called from SLI ring event handler with no
  8994. * lock held. This function is the completion handler for ELS commands
  8995. * which are aborted. The function frees memory resources used for
  8996. * the aborted ELS commands.
  8997. **/
  8998. static void
  8999. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  9000. struct lpfc_iocbq *rspiocb)
  9001. {
  9002. IOCB_t *irsp = &rspiocb->iocb;
  9003. /* ELS cmd tag <ulpIoTag> completes */
  9004. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  9005. "0139 Ignoring ELS cmd tag x%x completion Data: "
  9006. "x%x x%x x%x\n",
  9007. irsp->ulpIoTag, irsp->ulpStatus,
  9008. irsp->un.ulpWord[4], irsp->ulpTimeout);
  9009. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  9010. lpfc_ct_free_iocb(phba, cmdiocb);
  9011. else
  9012. lpfc_els_free_iocb(phba, cmdiocb);
  9013. return;
  9014. }
  9015. /**
  9016. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  9017. * @phba: Pointer to HBA context object.
  9018. * @pring: Pointer to driver SLI ring object.
  9019. * @cmdiocb: Pointer to driver command iocb object.
  9020. *
  9021. * This function issues an abort iocb for the provided command iocb down to
  9022. * the port. Other than the case the outstanding command iocb is an abort
  9023. * request, this function issues abort out unconditionally. This function is
  9024. * called with hbalock held. The function returns 0 when it fails due to
  9025. * memory allocation failure or when the command iocb is an abort request.
  9026. **/
  9027. static int
  9028. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  9029. struct lpfc_iocbq *cmdiocb)
  9030. {
  9031. struct lpfc_vport *vport = cmdiocb->vport;
  9032. struct lpfc_iocbq *abtsiocbp;
  9033. IOCB_t *icmd = NULL;
  9034. IOCB_t *iabt = NULL;
  9035. int ring_number;
  9036. int retval;
  9037. unsigned long iflags;
  9038. lockdep_assert_held(&phba->hbalock);
  9039. /*
  9040. * There are certain command types we don't want to abort. And we
  9041. * don't want to abort commands that are already in the process of
  9042. * being aborted.
  9043. */
  9044. icmd = &cmdiocb->iocb;
  9045. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  9046. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  9047. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  9048. return 0;
  9049. /* issue ABTS for this IOCB based on iotag */
  9050. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  9051. if (abtsiocbp == NULL)
  9052. return 0;
  9053. /* This signals the response to set the correct status
  9054. * before calling the completion handler
  9055. */
  9056. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  9057. iabt = &abtsiocbp->iocb;
  9058. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  9059. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  9060. if (phba->sli_rev == LPFC_SLI_REV4) {
  9061. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  9062. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  9063. }
  9064. else
  9065. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  9066. iabt->ulpLe = 1;
  9067. iabt->ulpClass = icmd->ulpClass;
  9068. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  9069. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  9070. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  9071. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  9072. if (cmdiocb->iocb_flag & LPFC_IO_FOF)
  9073. abtsiocbp->iocb_flag |= LPFC_IO_FOF;
  9074. if (phba->link_state >= LPFC_LINK_UP)
  9075. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  9076. else
  9077. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  9078. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  9079. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  9080. "0339 Abort xri x%x, original iotag x%x, "
  9081. "abort cmd iotag x%x\n",
  9082. iabt->un.acxri.abortIoTag,
  9083. iabt->un.acxri.abortContextTag,
  9084. abtsiocbp->iotag);
  9085. if (phba->sli_rev == LPFC_SLI_REV4) {
  9086. ring_number =
  9087. lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
  9088. if (unlikely(ring_number == LPFC_HBA_ERROR))
  9089. return 0;
  9090. pring = &phba->sli.ring[ring_number];
  9091. /* Note: both hbalock and ring_lock need to be set here */
  9092. spin_lock_irqsave(&pring->ring_lock, iflags);
  9093. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  9094. abtsiocbp, 0);
  9095. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  9096. } else {
  9097. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  9098. abtsiocbp, 0);
  9099. }
  9100. if (retval)
  9101. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  9102. /*
  9103. * Caller to this routine should check for IOCB_ERROR
  9104. * and handle it properly. This routine no longer removes
  9105. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  9106. */
  9107. return retval;
  9108. }
  9109. /**
  9110. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  9111. * @phba: Pointer to HBA context object.
  9112. * @pring: Pointer to driver SLI ring object.
  9113. * @cmdiocb: Pointer to driver command iocb object.
  9114. *
  9115. * This function issues an abort iocb for the provided command iocb. In case
  9116. * of unloading, the abort iocb will not be issued to commands on the ELS
  9117. * ring. Instead, the callback function shall be changed to those commands
  9118. * so that nothing happens when them finishes. This function is called with
  9119. * hbalock held. The function returns 0 when the command iocb is an abort
  9120. * request.
  9121. **/
  9122. int
  9123. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  9124. struct lpfc_iocbq *cmdiocb)
  9125. {
  9126. struct lpfc_vport *vport = cmdiocb->vport;
  9127. int retval = IOCB_ERROR;
  9128. IOCB_t *icmd = NULL;
  9129. lockdep_assert_held(&phba->hbalock);
  9130. /*
  9131. * There are certain command types we don't want to abort. And we
  9132. * don't want to abort commands that are already in the process of
  9133. * being aborted.
  9134. */
  9135. icmd = &cmdiocb->iocb;
  9136. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  9137. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  9138. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  9139. return 0;
  9140. /*
  9141. * If we're unloading, don't abort iocb on the ELS ring, but change
  9142. * the callback so that nothing happens when it finishes.
  9143. */
  9144. if ((vport->load_flag & FC_UNLOADING) &&
  9145. (pring->ringno == LPFC_ELS_RING)) {
  9146. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  9147. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  9148. else
  9149. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  9150. goto abort_iotag_exit;
  9151. }
  9152. /* Now, we try to issue the abort to the cmdiocb out */
  9153. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  9154. abort_iotag_exit:
  9155. /*
  9156. * Caller to this routine should check for IOCB_ERROR
  9157. * and handle it properly. This routine no longer removes
  9158. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  9159. */
  9160. return retval;
  9161. }
  9162. /**
  9163. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  9164. * @phba: pointer to lpfc HBA data structure.
  9165. *
  9166. * This routine will abort all pending and outstanding iocbs to an HBA.
  9167. **/
  9168. void
  9169. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  9170. {
  9171. struct lpfc_sli *psli = &phba->sli;
  9172. struct lpfc_sli_ring *pring;
  9173. int i;
  9174. for (i = 0; i < psli->num_rings; i++) {
  9175. pring = &psli->ring[i];
  9176. lpfc_sli_abort_iocb_ring(phba, pring);
  9177. }
  9178. }
  9179. /**
  9180. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  9181. * @iocbq: Pointer to driver iocb object.
  9182. * @vport: Pointer to driver virtual port object.
  9183. * @tgt_id: SCSI ID of the target.
  9184. * @lun_id: LUN ID of the scsi device.
  9185. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  9186. *
  9187. * This function acts as an iocb filter for functions which abort or count
  9188. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  9189. * 0 if the filtering criteria is met for the given iocb and will return
  9190. * 1 if the filtering criteria is not met.
  9191. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  9192. * given iocb is for the SCSI device specified by vport, tgt_id and
  9193. * lun_id parameter.
  9194. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  9195. * given iocb is for the SCSI target specified by vport and tgt_id
  9196. * parameters.
  9197. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  9198. * given iocb is for the SCSI host associated with the given vport.
  9199. * This function is called with no locks held.
  9200. **/
  9201. static int
  9202. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  9203. uint16_t tgt_id, uint64_t lun_id,
  9204. lpfc_ctx_cmd ctx_cmd)
  9205. {
  9206. struct lpfc_scsi_buf *lpfc_cmd;
  9207. int rc = 1;
  9208. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  9209. return rc;
  9210. if (iocbq->vport != vport)
  9211. return rc;
  9212. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  9213. if (lpfc_cmd->pCmd == NULL)
  9214. return rc;
  9215. switch (ctx_cmd) {
  9216. case LPFC_CTX_LUN:
  9217. if ((lpfc_cmd->rdata->pnode) &&
  9218. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  9219. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  9220. rc = 0;
  9221. break;
  9222. case LPFC_CTX_TGT:
  9223. if ((lpfc_cmd->rdata->pnode) &&
  9224. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  9225. rc = 0;
  9226. break;
  9227. case LPFC_CTX_HOST:
  9228. rc = 0;
  9229. break;
  9230. default:
  9231. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  9232. __func__, ctx_cmd);
  9233. break;
  9234. }
  9235. return rc;
  9236. }
  9237. /**
  9238. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  9239. * @vport: Pointer to virtual port.
  9240. * @tgt_id: SCSI ID of the target.
  9241. * @lun_id: LUN ID of the scsi device.
  9242. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  9243. *
  9244. * This function returns number of FCP commands pending for the vport.
  9245. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  9246. * commands pending on the vport associated with SCSI device specified
  9247. * by tgt_id and lun_id parameters.
  9248. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  9249. * commands pending on the vport associated with SCSI target specified
  9250. * by tgt_id parameter.
  9251. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  9252. * commands pending on the vport.
  9253. * This function returns the number of iocbs which satisfy the filter.
  9254. * This function is called without any lock held.
  9255. **/
  9256. int
  9257. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  9258. lpfc_ctx_cmd ctx_cmd)
  9259. {
  9260. struct lpfc_hba *phba = vport->phba;
  9261. struct lpfc_iocbq *iocbq;
  9262. int sum, i;
  9263. spin_lock_irq(&phba->hbalock);
  9264. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  9265. iocbq = phba->sli.iocbq_lookup[i];
  9266. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  9267. ctx_cmd) == 0)
  9268. sum++;
  9269. }
  9270. spin_unlock_irq(&phba->hbalock);
  9271. return sum;
  9272. }
  9273. /**
  9274. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  9275. * @phba: Pointer to HBA context object
  9276. * @cmdiocb: Pointer to command iocb object.
  9277. * @rspiocb: Pointer to response iocb object.
  9278. *
  9279. * This function is called when an aborted FCP iocb completes. This
  9280. * function is called by the ring event handler with no lock held.
  9281. * This function frees the iocb.
  9282. **/
  9283. void
  9284. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  9285. struct lpfc_iocbq *rspiocb)
  9286. {
  9287. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9288. "3096 ABORT_XRI_CN completing on rpi x%x "
  9289. "original iotag x%x, abort cmd iotag x%x "
  9290. "status 0x%x, reason 0x%x\n",
  9291. cmdiocb->iocb.un.acxri.abortContextTag,
  9292. cmdiocb->iocb.un.acxri.abortIoTag,
  9293. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  9294. rspiocb->iocb.un.ulpWord[4]);
  9295. lpfc_sli_release_iocbq(phba, cmdiocb);
  9296. return;
  9297. }
  9298. /**
  9299. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  9300. * @vport: Pointer to virtual port.
  9301. * @pring: Pointer to driver SLI ring object.
  9302. * @tgt_id: SCSI ID of the target.
  9303. * @lun_id: LUN ID of the scsi device.
  9304. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  9305. *
  9306. * This function sends an abort command for every SCSI command
  9307. * associated with the given virtual port pending on the ring
  9308. * filtered by lpfc_sli_validate_fcp_iocb function.
  9309. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  9310. * FCP iocbs associated with lun specified by tgt_id and lun_id
  9311. * parameters
  9312. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  9313. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  9314. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  9315. * FCP iocbs associated with virtual port.
  9316. * This function returns number of iocbs it failed to abort.
  9317. * This function is called with no locks held.
  9318. **/
  9319. int
  9320. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  9321. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  9322. {
  9323. struct lpfc_hba *phba = vport->phba;
  9324. struct lpfc_iocbq *iocbq;
  9325. struct lpfc_iocbq *abtsiocb;
  9326. IOCB_t *cmd = NULL;
  9327. int errcnt = 0, ret_val = 0;
  9328. int i;
  9329. for (i = 1; i <= phba->sli.last_iotag; i++) {
  9330. iocbq = phba->sli.iocbq_lookup[i];
  9331. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  9332. abort_cmd) != 0)
  9333. continue;
  9334. /*
  9335. * If the iocbq is already being aborted, don't take a second
  9336. * action, but do count it.
  9337. */
  9338. if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  9339. continue;
  9340. /* issue ABTS for this IOCB based on iotag */
  9341. abtsiocb = lpfc_sli_get_iocbq(phba);
  9342. if (abtsiocb == NULL) {
  9343. errcnt++;
  9344. continue;
  9345. }
  9346. /* indicate the IO is being aborted by the driver. */
  9347. iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
  9348. cmd = &iocbq->iocb;
  9349. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  9350. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  9351. if (phba->sli_rev == LPFC_SLI_REV4)
  9352. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  9353. else
  9354. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  9355. abtsiocb->iocb.ulpLe = 1;
  9356. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  9357. abtsiocb->vport = vport;
  9358. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  9359. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  9360. if (iocbq->iocb_flag & LPFC_IO_FCP)
  9361. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  9362. if (iocbq->iocb_flag & LPFC_IO_FOF)
  9363. abtsiocb->iocb_flag |= LPFC_IO_FOF;
  9364. if (lpfc_is_link_up(phba))
  9365. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  9366. else
  9367. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  9368. /* Setup callback routine and issue the command. */
  9369. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  9370. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  9371. abtsiocb, 0);
  9372. if (ret_val == IOCB_ERROR) {
  9373. lpfc_sli_release_iocbq(phba, abtsiocb);
  9374. errcnt++;
  9375. continue;
  9376. }
  9377. }
  9378. return errcnt;
  9379. }
  9380. /**
  9381. * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
  9382. * @vport: Pointer to virtual port.
  9383. * @pring: Pointer to driver SLI ring object.
  9384. * @tgt_id: SCSI ID of the target.
  9385. * @lun_id: LUN ID of the scsi device.
  9386. * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  9387. *
  9388. * This function sends an abort command for every SCSI command
  9389. * associated with the given virtual port pending on the ring
  9390. * filtered by lpfc_sli_validate_fcp_iocb function.
  9391. * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
  9392. * FCP iocbs associated with lun specified by tgt_id and lun_id
  9393. * parameters
  9394. * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
  9395. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  9396. * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
  9397. * FCP iocbs associated with virtual port.
  9398. * This function returns number of iocbs it aborted .
  9399. * This function is called with no locks held right after a taskmgmt
  9400. * command is sent.
  9401. **/
  9402. int
  9403. lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  9404. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
  9405. {
  9406. struct lpfc_hba *phba = vport->phba;
  9407. struct lpfc_scsi_buf *lpfc_cmd;
  9408. struct lpfc_iocbq *abtsiocbq;
  9409. struct lpfc_nodelist *ndlp;
  9410. struct lpfc_iocbq *iocbq;
  9411. IOCB_t *icmd;
  9412. int sum, i, ret_val;
  9413. unsigned long iflags;
  9414. struct lpfc_sli_ring *pring_s4;
  9415. uint32_t ring_number;
  9416. spin_lock_irq(&phba->hbalock);
  9417. /* all I/Os are in process of being flushed */
  9418. if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
  9419. spin_unlock_irq(&phba->hbalock);
  9420. return 0;
  9421. }
  9422. sum = 0;
  9423. for (i = 1; i <= phba->sli.last_iotag; i++) {
  9424. iocbq = phba->sli.iocbq_lookup[i];
  9425. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  9426. cmd) != 0)
  9427. continue;
  9428. /*
  9429. * If the iocbq is already being aborted, don't take a second
  9430. * action, but do count it.
  9431. */
  9432. if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  9433. continue;
  9434. /* issue ABTS for this IOCB based on iotag */
  9435. abtsiocbq = __lpfc_sli_get_iocbq(phba);
  9436. if (abtsiocbq == NULL)
  9437. continue;
  9438. icmd = &iocbq->iocb;
  9439. abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  9440. abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
  9441. if (phba->sli_rev == LPFC_SLI_REV4)
  9442. abtsiocbq->iocb.un.acxri.abortIoTag =
  9443. iocbq->sli4_xritag;
  9444. else
  9445. abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
  9446. abtsiocbq->iocb.ulpLe = 1;
  9447. abtsiocbq->iocb.ulpClass = icmd->ulpClass;
  9448. abtsiocbq->vport = vport;
  9449. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  9450. abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
  9451. if (iocbq->iocb_flag & LPFC_IO_FCP)
  9452. abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
  9453. if (iocbq->iocb_flag & LPFC_IO_FOF)
  9454. abtsiocbq->iocb_flag |= LPFC_IO_FOF;
  9455. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  9456. ndlp = lpfc_cmd->rdata->pnode;
  9457. if (lpfc_is_link_up(phba) &&
  9458. (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
  9459. abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  9460. else
  9461. abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  9462. /* Setup callback routine and issue the command. */
  9463. abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  9464. /*
  9465. * Indicate the IO is being aborted by the driver and set
  9466. * the caller's flag into the aborted IO.
  9467. */
  9468. iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
  9469. if (phba->sli_rev == LPFC_SLI_REV4) {
  9470. ring_number = MAX_SLI3_CONFIGURED_RINGS +
  9471. iocbq->fcp_wqidx;
  9472. pring_s4 = &phba->sli.ring[ring_number];
  9473. /* Note: both hbalock and ring_lock must be set here */
  9474. spin_lock_irqsave(&pring_s4->ring_lock, iflags);
  9475. ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
  9476. abtsiocbq, 0);
  9477. spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
  9478. } else {
  9479. ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
  9480. abtsiocbq, 0);
  9481. }
  9482. if (ret_val == IOCB_ERROR)
  9483. __lpfc_sli_release_iocbq(phba, abtsiocbq);
  9484. else
  9485. sum++;
  9486. }
  9487. spin_unlock_irq(&phba->hbalock);
  9488. return sum;
  9489. }
  9490. /**
  9491. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  9492. * @phba: Pointer to HBA context object.
  9493. * @cmdiocbq: Pointer to command iocb.
  9494. * @rspiocbq: Pointer to response iocb.
  9495. *
  9496. * This function is the completion handler for iocbs issued using
  9497. * lpfc_sli_issue_iocb_wait function. This function is called by the
  9498. * ring event handler function without any lock held. This function
  9499. * can be called from both worker thread context and interrupt
  9500. * context. This function also can be called from other thread which
  9501. * cleans up the SLI layer objects.
  9502. * This function copy the contents of the response iocb to the
  9503. * response iocb memory object provided by the caller of
  9504. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  9505. * sleeps for the iocb completion.
  9506. **/
  9507. static void
  9508. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  9509. struct lpfc_iocbq *cmdiocbq,
  9510. struct lpfc_iocbq *rspiocbq)
  9511. {
  9512. wait_queue_head_t *pdone_q;
  9513. unsigned long iflags;
  9514. struct lpfc_scsi_buf *lpfc_cmd;
  9515. spin_lock_irqsave(&phba->hbalock, iflags);
  9516. if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
  9517. /*
  9518. * A time out has occurred for the iocb. If a time out
  9519. * completion handler has been supplied, call it. Otherwise,
  9520. * just free the iocbq.
  9521. */
  9522. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9523. cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
  9524. cmdiocbq->wait_iocb_cmpl = NULL;
  9525. if (cmdiocbq->iocb_cmpl)
  9526. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
  9527. else
  9528. lpfc_sli_release_iocbq(phba, cmdiocbq);
  9529. return;
  9530. }
  9531. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  9532. if (cmdiocbq->context2 && rspiocbq)
  9533. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  9534. &rspiocbq->iocb, sizeof(IOCB_t));
  9535. /* Set the exchange busy flag for task management commands */
  9536. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  9537. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  9538. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  9539. cur_iocbq);
  9540. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  9541. }
  9542. pdone_q = cmdiocbq->context_un.wait_queue;
  9543. if (pdone_q)
  9544. wake_up(pdone_q);
  9545. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9546. return;
  9547. }
  9548. /**
  9549. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  9550. * @phba: Pointer to HBA context object..
  9551. * @piocbq: Pointer to command iocb.
  9552. * @flag: Flag to test.
  9553. *
  9554. * This routine grabs the hbalock and then test the iocb_flag to
  9555. * see if the passed in flag is set.
  9556. * Returns:
  9557. * 1 if flag is set.
  9558. * 0 if flag is not set.
  9559. **/
  9560. static int
  9561. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  9562. struct lpfc_iocbq *piocbq, uint32_t flag)
  9563. {
  9564. unsigned long iflags;
  9565. int ret;
  9566. spin_lock_irqsave(&phba->hbalock, iflags);
  9567. ret = piocbq->iocb_flag & flag;
  9568. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9569. return ret;
  9570. }
  9571. /**
  9572. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  9573. * @phba: Pointer to HBA context object..
  9574. * @pring: Pointer to sli ring.
  9575. * @piocb: Pointer to command iocb.
  9576. * @prspiocbq: Pointer to response iocb.
  9577. * @timeout: Timeout in number of seconds.
  9578. *
  9579. * This function issues the iocb to firmware and waits for the
  9580. * iocb to complete. The iocb_cmpl field of the shall be used
  9581. * to handle iocbs which time out. If the field is NULL, the
  9582. * function shall free the iocbq structure. If more clean up is
  9583. * needed, the caller is expected to provide a completion function
  9584. * that will provide the needed clean up. If the iocb command is
  9585. * not completed within timeout seconds, the function will either
  9586. * free the iocbq structure (if iocb_cmpl == NULL) or execute the
  9587. * completion function set in the iocb_cmpl field and then return
  9588. * a status of IOCB_TIMEDOUT. The caller should not free the iocb
  9589. * resources if this function returns IOCB_TIMEDOUT.
  9590. * The function waits for the iocb completion using an
  9591. * non-interruptible wait.
  9592. * This function will sleep while waiting for iocb completion.
  9593. * So, this function should not be called from any context which
  9594. * does not allow sleeping. Due to the same reason, this function
  9595. * cannot be called with interrupt disabled.
  9596. * This function assumes that the iocb completions occur while
  9597. * this function sleep. So, this function cannot be called from
  9598. * the thread which process iocb completion for this ring.
  9599. * This function clears the iocb_flag of the iocb object before
  9600. * issuing the iocb and the iocb completion handler sets this
  9601. * flag and wakes this thread when the iocb completes.
  9602. * The contents of the response iocb will be copied to prspiocbq
  9603. * by the completion handler when the command completes.
  9604. * This function returns IOCB_SUCCESS when success.
  9605. * This function is called with no lock held.
  9606. **/
  9607. int
  9608. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  9609. uint32_t ring_number,
  9610. struct lpfc_iocbq *piocb,
  9611. struct lpfc_iocbq *prspiocbq,
  9612. uint32_t timeout)
  9613. {
  9614. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9615. long timeleft, timeout_req = 0;
  9616. int retval = IOCB_SUCCESS;
  9617. uint32_t creg_val;
  9618. struct lpfc_iocbq *iocb;
  9619. int txq_cnt = 0;
  9620. int txcmplq_cnt = 0;
  9621. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9622. unsigned long iflags;
  9623. bool iocb_completed = true;
  9624. /*
  9625. * If the caller has provided a response iocbq buffer, then context2
  9626. * is NULL or its an error.
  9627. */
  9628. if (prspiocbq) {
  9629. if (piocb->context2)
  9630. return IOCB_ERROR;
  9631. piocb->context2 = prspiocbq;
  9632. }
  9633. piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
  9634. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  9635. piocb->context_un.wait_queue = &done_q;
  9636. piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
  9637. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9638. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9639. return IOCB_ERROR;
  9640. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  9641. writel(creg_val, phba->HCregaddr);
  9642. readl(phba->HCregaddr); /* flush */
  9643. }
  9644. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  9645. SLI_IOCB_RET_IOCB);
  9646. if (retval == IOCB_SUCCESS) {
  9647. timeout_req = msecs_to_jiffies(timeout * 1000);
  9648. timeleft = wait_event_timeout(done_q,
  9649. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  9650. timeout_req);
  9651. spin_lock_irqsave(&phba->hbalock, iflags);
  9652. if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
  9653. /*
  9654. * IOCB timed out. Inform the wake iocb wait
  9655. * completion function and set local status
  9656. */
  9657. iocb_completed = false;
  9658. piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
  9659. }
  9660. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9661. if (iocb_completed) {
  9662. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9663. "0331 IOCB wake signaled\n");
  9664. /* Note: we are not indicating if the IOCB has a success
  9665. * status or not - that's for the caller to check.
  9666. * IOCB_SUCCESS means just that the command was sent and
  9667. * completed. Not that it completed successfully.
  9668. * */
  9669. } else if (timeleft == 0) {
  9670. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9671. "0338 IOCB wait timeout error - no "
  9672. "wake response Data x%x\n", timeout);
  9673. retval = IOCB_TIMEDOUT;
  9674. } else {
  9675. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9676. "0330 IOCB wake NOT set, "
  9677. "Data x%x x%lx\n",
  9678. timeout, (timeleft / jiffies));
  9679. retval = IOCB_TIMEDOUT;
  9680. }
  9681. } else if (retval == IOCB_BUSY) {
  9682. if (phba->cfg_log_verbose & LOG_SLI) {
  9683. list_for_each_entry(iocb, &pring->txq, list) {
  9684. txq_cnt++;
  9685. }
  9686. list_for_each_entry(iocb, &pring->txcmplq, list) {
  9687. txcmplq_cnt++;
  9688. }
  9689. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9690. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  9691. phba->iocb_cnt, txq_cnt, txcmplq_cnt);
  9692. }
  9693. return retval;
  9694. } else {
  9695. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9696. "0332 IOCB wait issue failed, Data x%x\n",
  9697. retval);
  9698. retval = IOCB_ERROR;
  9699. }
  9700. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9701. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9702. return IOCB_ERROR;
  9703. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  9704. writel(creg_val, phba->HCregaddr);
  9705. readl(phba->HCregaddr); /* flush */
  9706. }
  9707. if (prspiocbq)
  9708. piocb->context2 = NULL;
  9709. piocb->context_un.wait_queue = NULL;
  9710. piocb->iocb_cmpl = NULL;
  9711. return retval;
  9712. }
  9713. /**
  9714. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  9715. * @phba: Pointer to HBA context object.
  9716. * @pmboxq: Pointer to driver mailbox object.
  9717. * @timeout: Timeout in number of seconds.
  9718. *
  9719. * This function issues the mailbox to firmware and waits for the
  9720. * mailbox command to complete. If the mailbox command is not
  9721. * completed within timeout seconds, it returns MBX_TIMEOUT.
  9722. * The function waits for the mailbox completion using an
  9723. * interruptible wait. If the thread is woken up due to a
  9724. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  9725. * should not free the mailbox resources, if this function returns
  9726. * MBX_TIMEOUT.
  9727. * This function will sleep while waiting for mailbox completion.
  9728. * So, this function should not be called from any context which
  9729. * does not allow sleeping. Due to the same reason, this function
  9730. * cannot be called with interrupt disabled.
  9731. * This function assumes that the mailbox completion occurs while
  9732. * this function sleep. So, this function cannot be called from
  9733. * the worker thread which processes mailbox completion.
  9734. * This function is called in the context of HBA management
  9735. * applications.
  9736. * This function returns MBX_SUCCESS when successful.
  9737. * This function is called with no lock held.
  9738. **/
  9739. int
  9740. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  9741. uint32_t timeout)
  9742. {
  9743. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9744. MAILBOX_t *mb = NULL;
  9745. int retval;
  9746. unsigned long flag;
  9747. /* The caller might set context1 for extended buffer */
  9748. if (pmboxq->context1)
  9749. mb = (MAILBOX_t *)pmboxq->context1;
  9750. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  9751. /* setup wake call as IOCB callback */
  9752. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  9753. /* setup context field to pass wait_queue pointer to wake function */
  9754. pmboxq->context1 = &done_q;
  9755. /* now issue the command */
  9756. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  9757. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  9758. wait_event_interruptible_timeout(done_q,
  9759. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  9760. msecs_to_jiffies(timeout * 1000));
  9761. spin_lock_irqsave(&phba->hbalock, flag);
  9762. /* restore the possible extended buffer for free resource */
  9763. pmboxq->context1 = (uint8_t *)mb;
  9764. /*
  9765. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  9766. * else do not free the resources.
  9767. */
  9768. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  9769. retval = MBX_SUCCESS;
  9770. } else {
  9771. retval = MBX_TIMEOUT;
  9772. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9773. }
  9774. spin_unlock_irqrestore(&phba->hbalock, flag);
  9775. } else {
  9776. /* restore the possible extended buffer for free resource */
  9777. pmboxq->context1 = (uint8_t *)mb;
  9778. }
  9779. return retval;
  9780. }
  9781. /**
  9782. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  9783. * @phba: Pointer to HBA context.
  9784. *
  9785. * This function is called to shutdown the driver's mailbox sub-system.
  9786. * It first marks the mailbox sub-system is in a block state to prevent
  9787. * the asynchronous mailbox command from issued off the pending mailbox
  9788. * command queue. If the mailbox command sub-system shutdown is due to
  9789. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  9790. * the mailbox sub-system flush routine to forcefully bring down the
  9791. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  9792. * as with offline or HBA function reset), this routine will wait for the
  9793. * outstanding mailbox command to complete before invoking the mailbox
  9794. * sub-system flush routine to gracefully bring down mailbox sub-system.
  9795. **/
  9796. void
  9797. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
  9798. {
  9799. struct lpfc_sli *psli = &phba->sli;
  9800. unsigned long timeout;
  9801. if (mbx_action == LPFC_MBX_NO_WAIT) {
  9802. /* delay 100ms for port state */
  9803. msleep(100);
  9804. lpfc_sli_mbox_sys_flush(phba);
  9805. return;
  9806. }
  9807. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  9808. spin_lock_irq(&phba->hbalock);
  9809. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  9810. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  9811. /* Determine how long we might wait for the active mailbox
  9812. * command to be gracefully completed by firmware.
  9813. */
  9814. if (phba->sli.mbox_active)
  9815. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  9816. phba->sli.mbox_active) *
  9817. 1000) + jiffies;
  9818. spin_unlock_irq(&phba->hbalock);
  9819. while (phba->sli.mbox_active) {
  9820. /* Check active mailbox complete status every 2ms */
  9821. msleep(2);
  9822. if (time_after(jiffies, timeout))
  9823. /* Timeout, let the mailbox flush routine to
  9824. * forcefully release active mailbox command
  9825. */
  9826. break;
  9827. }
  9828. } else
  9829. spin_unlock_irq(&phba->hbalock);
  9830. lpfc_sli_mbox_sys_flush(phba);
  9831. }
  9832. /**
  9833. * lpfc_sli_eratt_read - read sli-3 error attention events
  9834. * @phba: Pointer to HBA context.
  9835. *
  9836. * This function is called to read the SLI3 device error attention registers
  9837. * for possible error attention events. The caller must hold the hostlock
  9838. * with spin_lock_irq().
  9839. *
  9840. * This function returns 1 when there is Error Attention in the Host Attention
  9841. * Register and returns 0 otherwise.
  9842. **/
  9843. static int
  9844. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  9845. {
  9846. uint32_t ha_copy;
  9847. /* Read chip Host Attention (HA) register */
  9848. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9849. goto unplug_err;
  9850. if (ha_copy & HA_ERATT) {
  9851. /* Read host status register to retrieve error event */
  9852. if (lpfc_sli_read_hs(phba))
  9853. goto unplug_err;
  9854. /* Check if there is a deferred error condition is active */
  9855. if ((HS_FFER1 & phba->work_hs) &&
  9856. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9857. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9858. phba->hba_flag |= DEFER_ERATT;
  9859. /* Clear all interrupt enable conditions */
  9860. writel(0, phba->HCregaddr);
  9861. readl(phba->HCregaddr);
  9862. }
  9863. /* Set the driver HA work bitmap */
  9864. phba->work_ha |= HA_ERATT;
  9865. /* Indicate polling handles this ERATT */
  9866. phba->hba_flag |= HBA_ERATT_HANDLED;
  9867. return 1;
  9868. }
  9869. return 0;
  9870. unplug_err:
  9871. /* Set the driver HS work bitmap */
  9872. phba->work_hs |= UNPLUG_ERR;
  9873. /* Set the driver HA work bitmap */
  9874. phba->work_ha |= HA_ERATT;
  9875. /* Indicate polling handles this ERATT */
  9876. phba->hba_flag |= HBA_ERATT_HANDLED;
  9877. return 1;
  9878. }
  9879. /**
  9880. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9881. * @phba: Pointer to HBA context.
  9882. *
  9883. * This function is called to read the SLI4 device error attention registers
  9884. * for possible error attention events. The caller must hold the hostlock
  9885. * with spin_lock_irq().
  9886. *
  9887. * This function returns 1 when there is Error Attention in the Host Attention
  9888. * Register and returns 0 otherwise.
  9889. **/
  9890. static int
  9891. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9892. {
  9893. uint32_t uerr_sta_hi, uerr_sta_lo;
  9894. uint32_t if_type, portsmphr;
  9895. struct lpfc_register portstat_reg;
  9896. /*
  9897. * For now, use the SLI4 device internal unrecoverable error
  9898. * registers for error attention. This can be changed later.
  9899. */
  9900. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9901. switch (if_type) {
  9902. case LPFC_SLI_INTF_IF_TYPE_0:
  9903. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9904. &uerr_sta_lo) ||
  9905. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9906. &uerr_sta_hi)) {
  9907. phba->work_hs |= UNPLUG_ERR;
  9908. phba->work_ha |= HA_ERATT;
  9909. phba->hba_flag |= HBA_ERATT_HANDLED;
  9910. return 1;
  9911. }
  9912. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9913. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9914. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9915. "1423 HBA Unrecoverable error: "
  9916. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9917. "ue_mask_lo_reg=0x%x, "
  9918. "ue_mask_hi_reg=0x%x\n",
  9919. uerr_sta_lo, uerr_sta_hi,
  9920. phba->sli4_hba.ue_mask_lo,
  9921. phba->sli4_hba.ue_mask_hi);
  9922. phba->work_status[0] = uerr_sta_lo;
  9923. phba->work_status[1] = uerr_sta_hi;
  9924. phba->work_ha |= HA_ERATT;
  9925. phba->hba_flag |= HBA_ERATT_HANDLED;
  9926. return 1;
  9927. }
  9928. break;
  9929. case LPFC_SLI_INTF_IF_TYPE_2:
  9930. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9931. &portstat_reg.word0) ||
  9932. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9933. &portsmphr)){
  9934. phba->work_hs |= UNPLUG_ERR;
  9935. phba->work_ha |= HA_ERATT;
  9936. phba->hba_flag |= HBA_ERATT_HANDLED;
  9937. return 1;
  9938. }
  9939. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9940. phba->work_status[0] =
  9941. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9942. phba->work_status[1] =
  9943. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9944. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9945. "2885 Port Status Event: "
  9946. "port status reg 0x%x, "
  9947. "port smphr reg 0x%x, "
  9948. "error 1=0x%x, error 2=0x%x\n",
  9949. portstat_reg.word0,
  9950. portsmphr,
  9951. phba->work_status[0],
  9952. phba->work_status[1]);
  9953. phba->work_ha |= HA_ERATT;
  9954. phba->hba_flag |= HBA_ERATT_HANDLED;
  9955. return 1;
  9956. }
  9957. break;
  9958. case LPFC_SLI_INTF_IF_TYPE_1:
  9959. default:
  9960. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9961. "2886 HBA Error Attention on unsupported "
  9962. "if type %d.", if_type);
  9963. return 1;
  9964. }
  9965. return 0;
  9966. }
  9967. /**
  9968. * lpfc_sli_check_eratt - check error attention events
  9969. * @phba: Pointer to HBA context.
  9970. *
  9971. * This function is called from timer soft interrupt context to check HBA's
  9972. * error attention register bit for error attention events.
  9973. *
  9974. * This function returns 1 when there is Error Attention in the Host Attention
  9975. * Register and returns 0 otherwise.
  9976. **/
  9977. int
  9978. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9979. {
  9980. uint32_t ha_copy;
  9981. /* If somebody is waiting to handle an eratt, don't process it
  9982. * here. The brdkill function will do this.
  9983. */
  9984. if (phba->link_flag & LS_IGNORE_ERATT)
  9985. return 0;
  9986. /* Check if interrupt handler handles this ERATT */
  9987. spin_lock_irq(&phba->hbalock);
  9988. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9989. /* Interrupt handler has handled ERATT */
  9990. spin_unlock_irq(&phba->hbalock);
  9991. return 0;
  9992. }
  9993. /*
  9994. * If there is deferred error attention, do not check for error
  9995. * attention
  9996. */
  9997. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9998. spin_unlock_irq(&phba->hbalock);
  9999. return 0;
  10000. }
  10001. /* If PCI channel is offline, don't process it */
  10002. if (unlikely(pci_channel_offline(phba->pcidev))) {
  10003. spin_unlock_irq(&phba->hbalock);
  10004. return 0;
  10005. }
  10006. switch (phba->sli_rev) {
  10007. case LPFC_SLI_REV2:
  10008. case LPFC_SLI_REV3:
  10009. /* Read chip Host Attention (HA) register */
  10010. ha_copy = lpfc_sli_eratt_read(phba);
  10011. break;
  10012. case LPFC_SLI_REV4:
  10013. /* Read device Uncoverable Error (UERR) registers */
  10014. ha_copy = lpfc_sli4_eratt_read(phba);
  10015. break;
  10016. default:
  10017. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10018. "0299 Invalid SLI revision (%d)\n",
  10019. phba->sli_rev);
  10020. ha_copy = 0;
  10021. break;
  10022. }
  10023. spin_unlock_irq(&phba->hbalock);
  10024. return ha_copy;
  10025. }
  10026. /**
  10027. * lpfc_intr_state_check - Check device state for interrupt handling
  10028. * @phba: Pointer to HBA context.
  10029. *
  10030. * This inline routine checks whether a device or its PCI slot is in a state
  10031. * that the interrupt should be handled.
  10032. *
  10033. * This function returns 0 if the device or the PCI slot is in a state that
  10034. * interrupt should be handled, otherwise -EIO.
  10035. */
  10036. static inline int
  10037. lpfc_intr_state_check(struct lpfc_hba *phba)
  10038. {
  10039. /* If the pci channel is offline, ignore all the interrupts */
  10040. if (unlikely(pci_channel_offline(phba->pcidev)))
  10041. return -EIO;
  10042. /* Update device level interrupt statistics */
  10043. phba->sli.slistat.sli_intr++;
  10044. /* Ignore all interrupts during initialization. */
  10045. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  10046. return -EIO;
  10047. return 0;
  10048. }
  10049. /**
  10050. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  10051. * @irq: Interrupt number.
  10052. * @dev_id: The device context pointer.
  10053. *
  10054. * This function is directly called from the PCI layer as an interrupt
  10055. * service routine when device with SLI-3 interface spec is enabled with
  10056. * MSI-X multi-message interrupt mode and there are slow-path events in
  10057. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  10058. * interrupt mode, this function is called as part of the device-level
  10059. * interrupt handler. When the PCI slot is in error recovery or the HBA
  10060. * is undergoing initialization, the interrupt handler will not process
  10061. * the interrupt. The link attention and ELS ring attention events are
  10062. * handled by the worker thread. The interrupt handler signals the worker
  10063. * thread and returns for these events. This function is called without
  10064. * any lock held. It gets the hbalock to access and update SLI data
  10065. * structures.
  10066. *
  10067. * This function returns IRQ_HANDLED when interrupt is handled else it
  10068. * returns IRQ_NONE.
  10069. **/
  10070. irqreturn_t
  10071. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  10072. {
  10073. struct lpfc_hba *phba;
  10074. uint32_t ha_copy, hc_copy;
  10075. uint32_t work_ha_copy;
  10076. unsigned long status;
  10077. unsigned long iflag;
  10078. uint32_t control;
  10079. MAILBOX_t *mbox, *pmbox;
  10080. struct lpfc_vport *vport;
  10081. struct lpfc_nodelist *ndlp;
  10082. struct lpfc_dmabuf *mp;
  10083. LPFC_MBOXQ_t *pmb;
  10084. int rc;
  10085. /*
  10086. * Get the driver's phba structure from the dev_id and
  10087. * assume the HBA is not interrupting.
  10088. */
  10089. phba = (struct lpfc_hba *)dev_id;
  10090. if (unlikely(!phba))
  10091. return IRQ_NONE;
  10092. /*
  10093. * Stuff needs to be attented to when this function is invoked as an
  10094. * individual interrupt handler in MSI-X multi-message interrupt mode
  10095. */
  10096. if (phba->intr_type == MSIX) {
  10097. /* Check device state for handling interrupt */
  10098. if (lpfc_intr_state_check(phba))
  10099. return IRQ_NONE;
  10100. /* Need to read HA REG for slow-path events */
  10101. spin_lock_irqsave(&phba->hbalock, iflag);
  10102. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  10103. goto unplug_error;
  10104. /* If somebody is waiting to handle an eratt don't process it
  10105. * here. The brdkill function will do this.
  10106. */
  10107. if (phba->link_flag & LS_IGNORE_ERATT)
  10108. ha_copy &= ~HA_ERATT;
  10109. /* Check the need for handling ERATT in interrupt handler */
  10110. if (ha_copy & HA_ERATT) {
  10111. if (phba->hba_flag & HBA_ERATT_HANDLED)
  10112. /* ERATT polling has handled ERATT */
  10113. ha_copy &= ~HA_ERATT;
  10114. else
  10115. /* Indicate interrupt handler handles ERATT */
  10116. phba->hba_flag |= HBA_ERATT_HANDLED;
  10117. }
  10118. /*
  10119. * If there is deferred error attention, do not check for any
  10120. * interrupt.
  10121. */
  10122. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  10123. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10124. return IRQ_NONE;
  10125. }
  10126. /* Clear up only attention source related to slow-path */
  10127. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  10128. goto unplug_error;
  10129. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  10130. HC_LAINT_ENA | HC_ERINT_ENA),
  10131. phba->HCregaddr);
  10132. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  10133. phba->HAregaddr);
  10134. writel(hc_copy, phba->HCregaddr);
  10135. readl(phba->HAregaddr); /* flush */
  10136. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10137. } else
  10138. ha_copy = phba->ha_copy;
  10139. work_ha_copy = ha_copy & phba->work_ha_mask;
  10140. if (work_ha_copy) {
  10141. if (work_ha_copy & HA_LATT) {
  10142. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  10143. /*
  10144. * Turn off Link Attention interrupts
  10145. * until CLEAR_LA done
  10146. */
  10147. spin_lock_irqsave(&phba->hbalock, iflag);
  10148. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  10149. if (lpfc_readl(phba->HCregaddr, &control))
  10150. goto unplug_error;
  10151. control &= ~HC_LAINT_ENA;
  10152. writel(control, phba->HCregaddr);
  10153. readl(phba->HCregaddr); /* flush */
  10154. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10155. }
  10156. else
  10157. work_ha_copy &= ~HA_LATT;
  10158. }
  10159. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  10160. /*
  10161. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  10162. * the only slow ring.
  10163. */
  10164. status = (work_ha_copy &
  10165. (HA_RXMASK << (4*LPFC_ELS_RING)));
  10166. status >>= (4*LPFC_ELS_RING);
  10167. if (status & HA_RXMASK) {
  10168. spin_lock_irqsave(&phba->hbalock, iflag);
  10169. if (lpfc_readl(phba->HCregaddr, &control))
  10170. goto unplug_error;
  10171. lpfc_debugfs_slow_ring_trc(phba,
  10172. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  10173. control, status,
  10174. (uint32_t)phba->sli.slistat.sli_intr);
  10175. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  10176. lpfc_debugfs_slow_ring_trc(phba,
  10177. "ISR Disable ring:"
  10178. "pwork:x%x hawork:x%x wait:x%x",
  10179. phba->work_ha, work_ha_copy,
  10180. (uint32_t)((unsigned long)
  10181. &phba->work_waitq));
  10182. control &=
  10183. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  10184. writel(control, phba->HCregaddr);
  10185. readl(phba->HCregaddr); /* flush */
  10186. }
  10187. else {
  10188. lpfc_debugfs_slow_ring_trc(phba,
  10189. "ISR slow ring: pwork:"
  10190. "x%x hawork:x%x wait:x%x",
  10191. phba->work_ha, work_ha_copy,
  10192. (uint32_t)((unsigned long)
  10193. &phba->work_waitq));
  10194. }
  10195. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10196. }
  10197. }
  10198. spin_lock_irqsave(&phba->hbalock, iflag);
  10199. if (work_ha_copy & HA_ERATT) {
  10200. if (lpfc_sli_read_hs(phba))
  10201. goto unplug_error;
  10202. /*
  10203. * Check if there is a deferred error condition
  10204. * is active
  10205. */
  10206. if ((HS_FFER1 & phba->work_hs) &&
  10207. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  10208. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  10209. phba->work_hs)) {
  10210. phba->hba_flag |= DEFER_ERATT;
  10211. /* Clear all interrupt enable conditions */
  10212. writel(0, phba->HCregaddr);
  10213. readl(phba->HCregaddr);
  10214. }
  10215. }
  10216. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  10217. pmb = phba->sli.mbox_active;
  10218. pmbox = &pmb->u.mb;
  10219. mbox = phba->mbox;
  10220. vport = pmb->vport;
  10221. /* First check out the status word */
  10222. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  10223. if (pmbox->mbxOwner != OWN_HOST) {
  10224. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10225. /*
  10226. * Stray Mailbox Interrupt, mbxCommand <cmd>
  10227. * mbxStatus <status>
  10228. */
  10229. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10230. LOG_SLI,
  10231. "(%d):0304 Stray Mailbox "
  10232. "Interrupt mbxCommand x%x "
  10233. "mbxStatus x%x\n",
  10234. (vport ? vport->vpi : 0),
  10235. pmbox->mbxCommand,
  10236. pmbox->mbxStatus);
  10237. /* clear mailbox attention bit */
  10238. work_ha_copy &= ~HA_MBATT;
  10239. } else {
  10240. phba->sli.mbox_active = NULL;
  10241. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10242. phba->last_completion_time = jiffies;
  10243. del_timer(&phba->sli.mbox_tmo);
  10244. if (pmb->mbox_cmpl) {
  10245. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  10246. MAILBOX_CMD_SIZE);
  10247. if (pmb->out_ext_byte_len &&
  10248. pmb->context2)
  10249. lpfc_sli_pcimem_bcopy(
  10250. phba->mbox_ext,
  10251. pmb->context2,
  10252. pmb->out_ext_byte_len);
  10253. }
  10254. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10255. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10256. lpfc_debugfs_disc_trc(vport,
  10257. LPFC_DISC_TRC_MBOX_VPORT,
  10258. "MBOX dflt rpi: : "
  10259. "status:x%x rpi:x%x",
  10260. (uint32_t)pmbox->mbxStatus,
  10261. pmbox->un.varWords[0], 0);
  10262. if (!pmbox->mbxStatus) {
  10263. mp = (struct lpfc_dmabuf *)
  10264. (pmb->context1);
  10265. ndlp = (struct lpfc_nodelist *)
  10266. pmb->context2;
  10267. /* Reg_LOGIN of dflt RPI was
  10268. * successful. new lets get
  10269. * rid of the RPI using the
  10270. * same mbox buffer.
  10271. */
  10272. lpfc_unreg_login(phba,
  10273. vport->vpi,
  10274. pmbox->un.varWords[0],
  10275. pmb);
  10276. pmb->mbox_cmpl =
  10277. lpfc_mbx_cmpl_dflt_rpi;
  10278. pmb->context1 = mp;
  10279. pmb->context2 = ndlp;
  10280. pmb->vport = vport;
  10281. rc = lpfc_sli_issue_mbox(phba,
  10282. pmb,
  10283. MBX_NOWAIT);
  10284. if (rc != MBX_BUSY)
  10285. lpfc_printf_log(phba,
  10286. KERN_ERR,
  10287. LOG_MBOX | LOG_SLI,
  10288. "0350 rc should have"
  10289. "been MBX_BUSY\n");
  10290. if (rc != MBX_NOT_FINISHED)
  10291. goto send_current_mbox;
  10292. }
  10293. }
  10294. spin_lock_irqsave(
  10295. &phba->pport->work_port_lock,
  10296. iflag);
  10297. phba->pport->work_port_events &=
  10298. ~WORKER_MBOX_TMO;
  10299. spin_unlock_irqrestore(
  10300. &phba->pport->work_port_lock,
  10301. iflag);
  10302. lpfc_mbox_cmpl_put(phba, pmb);
  10303. }
  10304. } else
  10305. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10306. if ((work_ha_copy & HA_MBATT) &&
  10307. (phba->sli.mbox_active == NULL)) {
  10308. send_current_mbox:
  10309. /* Process next mailbox command if there is one */
  10310. do {
  10311. rc = lpfc_sli_issue_mbox(phba, NULL,
  10312. MBX_NOWAIT);
  10313. } while (rc == MBX_NOT_FINISHED);
  10314. if (rc != MBX_SUCCESS)
  10315. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10316. LOG_SLI, "0349 rc should be "
  10317. "MBX_SUCCESS\n");
  10318. }
  10319. spin_lock_irqsave(&phba->hbalock, iflag);
  10320. phba->work_ha |= work_ha_copy;
  10321. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10322. lpfc_worker_wake_up(phba);
  10323. }
  10324. return IRQ_HANDLED;
  10325. unplug_error:
  10326. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10327. return IRQ_HANDLED;
  10328. } /* lpfc_sli_sp_intr_handler */
  10329. /**
  10330. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  10331. * @irq: Interrupt number.
  10332. * @dev_id: The device context pointer.
  10333. *
  10334. * This function is directly called from the PCI layer as an interrupt
  10335. * service routine when device with SLI-3 interface spec is enabled with
  10336. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10337. * ring event in the HBA. However, when the device is enabled with either
  10338. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10339. * device-level interrupt handler. When the PCI slot is in error recovery
  10340. * or the HBA is undergoing initialization, the interrupt handler will not
  10341. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10342. * the intrrupt context. This function is called without any lock held.
  10343. * It gets the hbalock to access and update SLI data structures.
  10344. *
  10345. * This function returns IRQ_HANDLED when interrupt is handled else it
  10346. * returns IRQ_NONE.
  10347. **/
  10348. irqreturn_t
  10349. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  10350. {
  10351. struct lpfc_hba *phba;
  10352. uint32_t ha_copy;
  10353. unsigned long status;
  10354. unsigned long iflag;
  10355. /* Get the driver's phba structure from the dev_id and
  10356. * assume the HBA is not interrupting.
  10357. */
  10358. phba = (struct lpfc_hba *) dev_id;
  10359. if (unlikely(!phba))
  10360. return IRQ_NONE;
  10361. /*
  10362. * Stuff needs to be attented to when this function is invoked as an
  10363. * individual interrupt handler in MSI-X multi-message interrupt mode
  10364. */
  10365. if (phba->intr_type == MSIX) {
  10366. /* Check device state for handling interrupt */
  10367. if (lpfc_intr_state_check(phba))
  10368. return IRQ_NONE;
  10369. /* Need to read HA REG for FCP ring and other ring events */
  10370. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  10371. return IRQ_HANDLED;
  10372. /* Clear up only attention source related to fast-path */
  10373. spin_lock_irqsave(&phba->hbalock, iflag);
  10374. /*
  10375. * If there is deferred error attention, do not check for
  10376. * any interrupt.
  10377. */
  10378. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  10379. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10380. return IRQ_NONE;
  10381. }
  10382. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  10383. phba->HAregaddr);
  10384. readl(phba->HAregaddr); /* flush */
  10385. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10386. } else
  10387. ha_copy = phba->ha_copy;
  10388. /*
  10389. * Process all events on FCP ring. Take the optimized path for FCP IO.
  10390. */
  10391. ha_copy &= ~(phba->work_ha_mask);
  10392. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  10393. status >>= (4*LPFC_FCP_RING);
  10394. if (status & HA_RXMASK)
  10395. lpfc_sli_handle_fast_ring_event(phba,
  10396. &phba->sli.ring[LPFC_FCP_RING],
  10397. status);
  10398. if (phba->cfg_multi_ring_support == 2) {
  10399. /*
  10400. * Process all events on extra ring. Take the optimized path
  10401. * for extra ring IO.
  10402. */
  10403. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  10404. status >>= (4*LPFC_EXTRA_RING);
  10405. if (status & HA_RXMASK) {
  10406. lpfc_sli_handle_fast_ring_event(phba,
  10407. &phba->sli.ring[LPFC_EXTRA_RING],
  10408. status);
  10409. }
  10410. }
  10411. return IRQ_HANDLED;
  10412. } /* lpfc_sli_fp_intr_handler */
  10413. /**
  10414. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  10415. * @irq: Interrupt number.
  10416. * @dev_id: The device context pointer.
  10417. *
  10418. * This function is the HBA device-level interrupt handler to device with
  10419. * SLI-3 interface spec, called from the PCI layer when either MSI or
  10420. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  10421. * requires driver attention. This function invokes the slow-path interrupt
  10422. * attention handling function and fast-path interrupt attention handling
  10423. * function in turn to process the relevant HBA attention events. This
  10424. * function is called without any lock held. It gets the hbalock to access
  10425. * and update SLI data structures.
  10426. *
  10427. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10428. * returns IRQ_NONE.
  10429. **/
  10430. irqreturn_t
  10431. lpfc_sli_intr_handler(int irq, void *dev_id)
  10432. {
  10433. struct lpfc_hba *phba;
  10434. irqreturn_t sp_irq_rc, fp_irq_rc;
  10435. unsigned long status1, status2;
  10436. uint32_t hc_copy;
  10437. /*
  10438. * Get the driver's phba structure from the dev_id and
  10439. * assume the HBA is not interrupting.
  10440. */
  10441. phba = (struct lpfc_hba *) dev_id;
  10442. if (unlikely(!phba))
  10443. return IRQ_NONE;
  10444. /* Check device state for handling interrupt */
  10445. if (lpfc_intr_state_check(phba))
  10446. return IRQ_NONE;
  10447. spin_lock(&phba->hbalock);
  10448. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  10449. spin_unlock(&phba->hbalock);
  10450. return IRQ_HANDLED;
  10451. }
  10452. if (unlikely(!phba->ha_copy)) {
  10453. spin_unlock(&phba->hbalock);
  10454. return IRQ_NONE;
  10455. } else if (phba->ha_copy & HA_ERATT) {
  10456. if (phba->hba_flag & HBA_ERATT_HANDLED)
  10457. /* ERATT polling has handled ERATT */
  10458. phba->ha_copy &= ~HA_ERATT;
  10459. else
  10460. /* Indicate interrupt handler handles ERATT */
  10461. phba->hba_flag |= HBA_ERATT_HANDLED;
  10462. }
  10463. /*
  10464. * If there is deferred error attention, do not check for any interrupt.
  10465. */
  10466. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  10467. spin_unlock(&phba->hbalock);
  10468. return IRQ_NONE;
  10469. }
  10470. /* Clear attention sources except link and error attentions */
  10471. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  10472. spin_unlock(&phba->hbalock);
  10473. return IRQ_HANDLED;
  10474. }
  10475. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  10476. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  10477. phba->HCregaddr);
  10478. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  10479. writel(hc_copy, phba->HCregaddr);
  10480. readl(phba->HAregaddr); /* flush */
  10481. spin_unlock(&phba->hbalock);
  10482. /*
  10483. * Invokes slow-path host attention interrupt handling as appropriate.
  10484. */
  10485. /* status of events with mailbox and link attention */
  10486. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  10487. /* status of events with ELS ring */
  10488. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  10489. status2 >>= (4*LPFC_ELS_RING);
  10490. if (status1 || (status2 & HA_RXMASK))
  10491. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  10492. else
  10493. sp_irq_rc = IRQ_NONE;
  10494. /*
  10495. * Invoke fast-path host attention interrupt handling as appropriate.
  10496. */
  10497. /* status of events with FCP ring */
  10498. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  10499. status1 >>= (4*LPFC_FCP_RING);
  10500. /* status of events with extra ring */
  10501. if (phba->cfg_multi_ring_support == 2) {
  10502. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  10503. status2 >>= (4*LPFC_EXTRA_RING);
  10504. } else
  10505. status2 = 0;
  10506. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  10507. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  10508. else
  10509. fp_irq_rc = IRQ_NONE;
  10510. /* Return device-level interrupt handling status */
  10511. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  10512. } /* lpfc_sli_intr_handler */
  10513. /**
  10514. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  10515. * @phba: pointer to lpfc hba data structure.
  10516. *
  10517. * This routine is invoked by the worker thread to process all the pending
  10518. * SLI4 FCP abort XRI events.
  10519. **/
  10520. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  10521. {
  10522. struct lpfc_cq_event *cq_event;
  10523. /* First, declare the fcp xri abort event has been handled */
  10524. spin_lock_irq(&phba->hbalock);
  10525. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  10526. spin_unlock_irq(&phba->hbalock);
  10527. /* Now, handle all the fcp xri abort events */
  10528. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  10529. /* Get the first event from the head of the event queue */
  10530. spin_lock_irq(&phba->hbalock);
  10531. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  10532. cq_event, struct lpfc_cq_event, list);
  10533. spin_unlock_irq(&phba->hbalock);
  10534. /* Notify aborted XRI for FCP work queue */
  10535. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  10536. /* Free the event processed back to the free pool */
  10537. lpfc_sli4_cq_event_release(phba, cq_event);
  10538. }
  10539. }
  10540. /**
  10541. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  10542. * @phba: pointer to lpfc hba data structure.
  10543. *
  10544. * This routine is invoked by the worker thread to process all the pending
  10545. * SLI4 els abort xri events.
  10546. **/
  10547. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  10548. {
  10549. struct lpfc_cq_event *cq_event;
  10550. /* First, declare the els xri abort event has been handled */
  10551. spin_lock_irq(&phba->hbalock);
  10552. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  10553. spin_unlock_irq(&phba->hbalock);
  10554. /* Now, handle all the els xri abort events */
  10555. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  10556. /* Get the first event from the head of the event queue */
  10557. spin_lock_irq(&phba->hbalock);
  10558. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  10559. cq_event, struct lpfc_cq_event, list);
  10560. spin_unlock_irq(&phba->hbalock);
  10561. /* Notify aborted XRI for ELS work queue */
  10562. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  10563. /* Free the event processed back to the free pool */
  10564. lpfc_sli4_cq_event_release(phba, cq_event);
  10565. }
  10566. }
  10567. /**
  10568. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  10569. * @phba: pointer to lpfc hba data structure
  10570. * @pIocbIn: pointer to the rspiocbq
  10571. * @pIocbOut: pointer to the cmdiocbq
  10572. * @wcqe: pointer to the complete wcqe
  10573. *
  10574. * This routine transfers the fields of a command iocbq to a response iocbq
  10575. * by copying all the IOCB fields from command iocbq and transferring the
  10576. * completion status information from the complete wcqe.
  10577. **/
  10578. static void
  10579. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  10580. struct lpfc_iocbq *pIocbIn,
  10581. struct lpfc_iocbq *pIocbOut,
  10582. struct lpfc_wcqe_complete *wcqe)
  10583. {
  10584. int numBdes, i;
  10585. unsigned long iflags;
  10586. uint32_t status, max_response;
  10587. struct lpfc_dmabuf *dmabuf;
  10588. struct ulp_bde64 *bpl, bde;
  10589. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  10590. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  10591. sizeof(struct lpfc_iocbq) - offset);
  10592. /* Map WCQE parameters into irspiocb parameters */
  10593. status = bf_get(lpfc_wcqe_c_status, wcqe);
  10594. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  10595. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  10596. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  10597. pIocbIn->iocb.un.fcpi.fcpi_parm =
  10598. pIocbOut->iocb.un.fcpi.fcpi_parm -
  10599. wcqe->total_data_placed;
  10600. else
  10601. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  10602. else {
  10603. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  10604. switch (pIocbOut->iocb.ulpCommand) {
  10605. case CMD_ELS_REQUEST64_CR:
  10606. dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
  10607. bpl = (struct ulp_bde64 *)dmabuf->virt;
  10608. bde.tus.w = le32_to_cpu(bpl[1].tus.w);
  10609. max_response = bde.tus.f.bdeSize;
  10610. break;
  10611. case CMD_GEN_REQUEST64_CR:
  10612. max_response = 0;
  10613. if (!pIocbOut->context3)
  10614. break;
  10615. numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
  10616. sizeof(struct ulp_bde64);
  10617. dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
  10618. bpl = (struct ulp_bde64 *)dmabuf->virt;
  10619. for (i = 0; i < numBdes; i++) {
  10620. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  10621. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  10622. max_response += bde.tus.f.bdeSize;
  10623. }
  10624. break;
  10625. default:
  10626. max_response = wcqe->total_data_placed;
  10627. break;
  10628. }
  10629. if (max_response < wcqe->total_data_placed)
  10630. pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
  10631. else
  10632. pIocbIn->iocb.un.genreq64.bdl.bdeSize =
  10633. wcqe->total_data_placed;
  10634. }
  10635. /* Convert BG errors for completion status */
  10636. if (status == CQE_STATUS_DI_ERROR) {
  10637. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  10638. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  10639. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  10640. else
  10641. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  10642. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  10643. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  10644. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10645. BGS_GUARD_ERR_MASK;
  10646. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  10647. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10648. BGS_APPTAG_ERR_MASK;
  10649. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  10650. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10651. BGS_REFTAG_ERR_MASK;
  10652. /* Check to see if there was any good data before the error */
  10653. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  10654. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10655. BGS_HI_WATER_MARK_PRESENT_MASK;
  10656. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  10657. wcqe->total_data_placed;
  10658. }
  10659. /*
  10660. * Set ALL the error bits to indicate we don't know what
  10661. * type of error it is.
  10662. */
  10663. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  10664. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10665. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  10666. BGS_GUARD_ERR_MASK);
  10667. }
  10668. /* Pick up HBA exchange busy condition */
  10669. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  10670. spin_lock_irqsave(&phba->hbalock, iflags);
  10671. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  10672. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10673. }
  10674. }
  10675. /**
  10676. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  10677. * @phba: Pointer to HBA context object.
  10678. * @wcqe: Pointer to work-queue completion queue entry.
  10679. *
  10680. * This routine handles an ELS work-queue completion event and construct
  10681. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  10682. * discovery engine to handle.
  10683. *
  10684. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  10685. **/
  10686. static struct lpfc_iocbq *
  10687. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  10688. struct lpfc_iocbq *irspiocbq)
  10689. {
  10690. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  10691. struct lpfc_iocbq *cmdiocbq;
  10692. struct lpfc_wcqe_complete *wcqe;
  10693. unsigned long iflags;
  10694. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  10695. spin_lock_irqsave(&pring->ring_lock, iflags);
  10696. pring->stats.iocb_event++;
  10697. /* Look up the ELS command IOCB and create pseudo response IOCB */
  10698. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10699. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10700. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10701. if (unlikely(!cmdiocbq)) {
  10702. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10703. "0386 ELS complete with no corresponding "
  10704. "cmdiocb: iotag (%d)\n",
  10705. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10706. lpfc_sli_release_iocbq(phba, irspiocbq);
  10707. return NULL;
  10708. }
  10709. /* Fake the irspiocbq and copy necessary response information */
  10710. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  10711. return irspiocbq;
  10712. }
  10713. /**
  10714. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  10715. * @phba: Pointer to HBA context object.
  10716. * @cqe: Pointer to mailbox completion queue entry.
  10717. *
  10718. * This routine process a mailbox completion queue entry with asynchrous
  10719. * event.
  10720. *
  10721. * Return: true if work posted to worker thread, otherwise false.
  10722. **/
  10723. static bool
  10724. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10725. {
  10726. struct lpfc_cq_event *cq_event;
  10727. unsigned long iflags;
  10728. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10729. "0392 Async Event: word0:x%x, word1:x%x, "
  10730. "word2:x%x, word3:x%x\n", mcqe->word0,
  10731. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  10732. /* Allocate a new internal CQ_EVENT entry */
  10733. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10734. if (!cq_event) {
  10735. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10736. "0394 Failed to allocate CQ_EVENT entry\n");
  10737. return false;
  10738. }
  10739. /* Move the CQE into an asynchronous event entry */
  10740. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  10741. spin_lock_irqsave(&phba->hbalock, iflags);
  10742. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  10743. /* Set the async event flag */
  10744. phba->hba_flag |= ASYNC_EVENT;
  10745. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10746. return true;
  10747. }
  10748. /**
  10749. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  10750. * @phba: Pointer to HBA context object.
  10751. * @cqe: Pointer to mailbox completion queue entry.
  10752. *
  10753. * This routine process a mailbox completion queue entry with mailbox
  10754. * completion event.
  10755. *
  10756. * Return: true if work posted to worker thread, otherwise false.
  10757. **/
  10758. static bool
  10759. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10760. {
  10761. uint32_t mcqe_status;
  10762. MAILBOX_t *mbox, *pmbox;
  10763. struct lpfc_mqe *mqe;
  10764. struct lpfc_vport *vport;
  10765. struct lpfc_nodelist *ndlp;
  10766. struct lpfc_dmabuf *mp;
  10767. unsigned long iflags;
  10768. LPFC_MBOXQ_t *pmb;
  10769. bool workposted = false;
  10770. int rc;
  10771. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  10772. if (!bf_get(lpfc_trailer_completed, mcqe))
  10773. goto out_no_mqe_complete;
  10774. /* Get the reference to the active mbox command */
  10775. spin_lock_irqsave(&phba->hbalock, iflags);
  10776. pmb = phba->sli.mbox_active;
  10777. if (unlikely(!pmb)) {
  10778. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10779. "1832 No pending MBOX command to handle\n");
  10780. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10781. goto out_no_mqe_complete;
  10782. }
  10783. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10784. mqe = &pmb->u.mqe;
  10785. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  10786. mbox = phba->mbox;
  10787. vport = pmb->vport;
  10788. /* Reset heartbeat timer */
  10789. phba->last_completion_time = jiffies;
  10790. del_timer(&phba->sli.mbox_tmo);
  10791. /* Move mbox data to caller's mailbox region, do endian swapping */
  10792. if (pmb->mbox_cmpl && mbox)
  10793. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  10794. /*
  10795. * For mcqe errors, conditionally move a modified error code to
  10796. * the mbox so that the error will not be missed.
  10797. */
  10798. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  10799. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  10800. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  10801. bf_set(lpfc_mqe_status, mqe,
  10802. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  10803. }
  10804. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10805. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10806. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  10807. "MBOX dflt rpi: status:x%x rpi:x%x",
  10808. mcqe_status,
  10809. pmbox->un.varWords[0], 0);
  10810. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  10811. mp = (struct lpfc_dmabuf *)(pmb->context1);
  10812. ndlp = (struct lpfc_nodelist *)pmb->context2;
  10813. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  10814. * RID of the PPI using the same mbox buffer.
  10815. */
  10816. lpfc_unreg_login(phba, vport->vpi,
  10817. pmbox->un.varWords[0], pmb);
  10818. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  10819. pmb->context1 = mp;
  10820. pmb->context2 = ndlp;
  10821. pmb->vport = vport;
  10822. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  10823. if (rc != MBX_BUSY)
  10824. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10825. LOG_SLI, "0385 rc should "
  10826. "have been MBX_BUSY\n");
  10827. if (rc != MBX_NOT_FINISHED)
  10828. goto send_current_mbox;
  10829. }
  10830. }
  10831. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  10832. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  10833. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  10834. /* There is mailbox completion work to do */
  10835. spin_lock_irqsave(&phba->hbalock, iflags);
  10836. __lpfc_mbox_cmpl_put(phba, pmb);
  10837. phba->work_ha |= HA_MBATT;
  10838. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10839. workposted = true;
  10840. send_current_mbox:
  10841. spin_lock_irqsave(&phba->hbalock, iflags);
  10842. /* Release the mailbox command posting token */
  10843. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10844. /* Setting active mailbox pointer need to be in sync to flag clear */
  10845. phba->sli.mbox_active = NULL;
  10846. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10847. /* Wake up worker thread to post the next pending mailbox command */
  10848. lpfc_worker_wake_up(phba);
  10849. out_no_mqe_complete:
  10850. if (bf_get(lpfc_trailer_consumed, mcqe))
  10851. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  10852. return workposted;
  10853. }
  10854. /**
  10855. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  10856. * @phba: Pointer to HBA context object.
  10857. * @cqe: Pointer to mailbox completion queue entry.
  10858. *
  10859. * This routine process a mailbox completion queue entry, it invokes the
  10860. * proper mailbox complete handling or asynchrous event handling routine
  10861. * according to the MCQE's async bit.
  10862. *
  10863. * Return: true if work posted to worker thread, otherwise false.
  10864. **/
  10865. static bool
  10866. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  10867. {
  10868. struct lpfc_mcqe mcqe;
  10869. bool workposted;
  10870. /* Copy the mailbox MCQE and convert endian order as needed */
  10871. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  10872. /* Invoke the proper event handling routine */
  10873. if (!bf_get(lpfc_trailer_async, &mcqe))
  10874. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  10875. else
  10876. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  10877. return workposted;
  10878. }
  10879. /**
  10880. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  10881. * @phba: Pointer to HBA context object.
  10882. * @cq: Pointer to associated CQ
  10883. * @wcqe: Pointer to work-queue completion queue entry.
  10884. *
  10885. * This routine handles an ELS work-queue completion event.
  10886. *
  10887. * Return: true if work posted to worker thread, otherwise false.
  10888. **/
  10889. static bool
  10890. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10891. struct lpfc_wcqe_complete *wcqe)
  10892. {
  10893. struct lpfc_iocbq *irspiocbq;
  10894. unsigned long iflags;
  10895. struct lpfc_sli_ring *pring = cq->pring;
  10896. int txq_cnt = 0;
  10897. int txcmplq_cnt = 0;
  10898. int fcp_txcmplq_cnt = 0;
  10899. /* Get an irspiocbq for later ELS response processing use */
  10900. irspiocbq = lpfc_sli_get_iocbq(phba);
  10901. if (!irspiocbq) {
  10902. if (!list_empty(&pring->txq))
  10903. txq_cnt++;
  10904. if (!list_empty(&pring->txcmplq))
  10905. txcmplq_cnt++;
  10906. if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
  10907. fcp_txcmplq_cnt++;
  10908. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10909. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10910. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10911. txq_cnt, phba->iocb_cnt,
  10912. fcp_txcmplq_cnt,
  10913. txcmplq_cnt);
  10914. return false;
  10915. }
  10916. /* Save off the slow-path queue event for work thread to process */
  10917. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10918. spin_lock_irqsave(&phba->hbalock, iflags);
  10919. list_add_tail(&irspiocbq->cq_event.list,
  10920. &phba->sli4_hba.sp_queue_event);
  10921. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10922. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10923. return true;
  10924. }
  10925. /**
  10926. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10927. * @phba: Pointer to HBA context object.
  10928. * @wcqe: Pointer to work-queue completion queue entry.
  10929. *
  10930. * This routine handles slow-path WQ entry comsumed event by invoking the
  10931. * proper WQ release routine to the slow-path WQ.
  10932. **/
  10933. static void
  10934. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10935. struct lpfc_wcqe_release *wcqe)
  10936. {
  10937. /* sanity check on queue memory */
  10938. if (unlikely(!phba->sli4_hba.els_wq))
  10939. return;
  10940. /* Check for the slow-path ELS work queue */
  10941. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10942. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10943. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10944. else
  10945. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10946. "2579 Slow-path wqe consume event carries "
  10947. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10948. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10949. phba->sli4_hba.els_wq->queue_id);
  10950. }
  10951. /**
  10952. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10953. * @phba: Pointer to HBA context object.
  10954. * @cq: Pointer to a WQ completion queue.
  10955. * @wcqe: Pointer to work-queue completion queue entry.
  10956. *
  10957. * This routine handles an XRI abort event.
  10958. *
  10959. * Return: true if work posted to worker thread, otherwise false.
  10960. **/
  10961. static bool
  10962. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10963. struct lpfc_queue *cq,
  10964. struct sli4_wcqe_xri_aborted *wcqe)
  10965. {
  10966. bool workposted = false;
  10967. struct lpfc_cq_event *cq_event;
  10968. unsigned long iflags;
  10969. /* Allocate a new internal CQ_EVENT entry */
  10970. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10971. if (!cq_event) {
  10972. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10973. "0602 Failed to allocate CQ_EVENT entry\n");
  10974. return false;
  10975. }
  10976. /* Move the CQE into the proper xri abort event list */
  10977. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10978. switch (cq->subtype) {
  10979. case LPFC_FCP:
  10980. spin_lock_irqsave(&phba->hbalock, iflags);
  10981. list_add_tail(&cq_event->list,
  10982. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10983. /* Set the fcp xri abort event flag */
  10984. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10985. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10986. workposted = true;
  10987. break;
  10988. case LPFC_ELS:
  10989. spin_lock_irqsave(&phba->hbalock, iflags);
  10990. list_add_tail(&cq_event->list,
  10991. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10992. /* Set the els xri abort event flag */
  10993. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10994. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10995. workposted = true;
  10996. break;
  10997. default:
  10998. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10999. "0603 Invalid work queue CQE subtype (x%x)\n",
  11000. cq->subtype);
  11001. workposted = false;
  11002. break;
  11003. }
  11004. return workposted;
  11005. }
  11006. /**
  11007. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  11008. * @phba: Pointer to HBA context object.
  11009. * @rcqe: Pointer to receive-queue completion queue entry.
  11010. *
  11011. * This routine process a receive-queue completion queue entry.
  11012. *
  11013. * Return: true if work posted to worker thread, otherwise false.
  11014. **/
  11015. static bool
  11016. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  11017. {
  11018. bool workposted = false;
  11019. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  11020. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  11021. struct hbq_dmabuf *dma_buf;
  11022. uint32_t status, rq_id;
  11023. unsigned long iflags;
  11024. /* sanity check on queue memory */
  11025. if (unlikely(!hrq) || unlikely(!drq))
  11026. return workposted;
  11027. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  11028. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  11029. else
  11030. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  11031. if (rq_id != hrq->queue_id)
  11032. goto out;
  11033. status = bf_get(lpfc_rcqe_status, rcqe);
  11034. switch (status) {
  11035. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  11036. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11037. "2537 Receive Frame Truncated!!\n");
  11038. hrq->RQ_buf_trunc++;
  11039. case FC_STATUS_RQ_SUCCESS:
  11040. lpfc_sli4_rq_release(hrq, drq);
  11041. spin_lock_irqsave(&phba->hbalock, iflags);
  11042. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  11043. if (!dma_buf) {
  11044. hrq->RQ_no_buf_found++;
  11045. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11046. goto out;
  11047. }
  11048. hrq->RQ_rcv_buf++;
  11049. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  11050. /* save off the frame for the word thread to process */
  11051. list_add_tail(&dma_buf->cq_event.list,
  11052. &phba->sli4_hba.sp_queue_event);
  11053. /* Frame received */
  11054. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  11055. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11056. workposted = true;
  11057. break;
  11058. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  11059. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  11060. hrq->RQ_no_posted_buf++;
  11061. /* Post more buffers if possible */
  11062. spin_lock_irqsave(&phba->hbalock, iflags);
  11063. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  11064. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11065. workposted = true;
  11066. break;
  11067. }
  11068. out:
  11069. return workposted;
  11070. }
  11071. /**
  11072. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  11073. * @phba: Pointer to HBA context object.
  11074. * @cq: Pointer to the completion queue.
  11075. * @wcqe: Pointer to a completion queue entry.
  11076. *
  11077. * This routine process a slow-path work-queue or receive queue completion queue
  11078. * entry.
  11079. *
  11080. * Return: true if work posted to worker thread, otherwise false.
  11081. **/
  11082. static bool
  11083. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11084. struct lpfc_cqe *cqe)
  11085. {
  11086. struct lpfc_cqe cqevt;
  11087. bool workposted = false;
  11088. /* Copy the work queue CQE and convert endian order if needed */
  11089. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  11090. /* Check and process for different type of WCQE and dispatch */
  11091. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  11092. case CQE_CODE_COMPL_WQE:
  11093. /* Process the WQ/RQ complete event */
  11094. phba->last_completion_time = jiffies;
  11095. workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
  11096. (struct lpfc_wcqe_complete *)&cqevt);
  11097. break;
  11098. case CQE_CODE_RELEASE_WQE:
  11099. /* Process the WQ release event */
  11100. lpfc_sli4_sp_handle_rel_wcqe(phba,
  11101. (struct lpfc_wcqe_release *)&cqevt);
  11102. break;
  11103. case CQE_CODE_XRI_ABORTED:
  11104. /* Process the WQ XRI abort event */
  11105. phba->last_completion_time = jiffies;
  11106. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  11107. (struct sli4_wcqe_xri_aborted *)&cqevt);
  11108. break;
  11109. case CQE_CODE_RECEIVE:
  11110. case CQE_CODE_RECEIVE_V1:
  11111. /* Process the RQ event */
  11112. phba->last_completion_time = jiffies;
  11113. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  11114. (struct lpfc_rcqe *)&cqevt);
  11115. break;
  11116. default:
  11117. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11118. "0388 Not a valid WCQE code: x%x\n",
  11119. bf_get(lpfc_cqe_code, &cqevt));
  11120. break;
  11121. }
  11122. return workposted;
  11123. }
  11124. /**
  11125. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  11126. * @phba: Pointer to HBA context object.
  11127. * @eqe: Pointer to fast-path event queue entry.
  11128. *
  11129. * This routine process a event queue entry from the slow-path event queue.
  11130. * It will check the MajorCode and MinorCode to determine this is for a
  11131. * completion event on a completion queue, if not, an error shall be logged
  11132. * and just return. Otherwise, it will get to the corresponding completion
  11133. * queue and process all the entries on that completion queue, rearm the
  11134. * completion queue, and then return.
  11135. *
  11136. **/
  11137. static void
  11138. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  11139. struct lpfc_queue *speq)
  11140. {
  11141. struct lpfc_queue *cq = NULL, *childq;
  11142. struct lpfc_cqe *cqe;
  11143. bool workposted = false;
  11144. int ecount = 0;
  11145. uint16_t cqid;
  11146. /* Get the reference to the corresponding CQ */
  11147. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  11148. list_for_each_entry(childq, &speq->child_list, list) {
  11149. if (childq->queue_id == cqid) {
  11150. cq = childq;
  11151. break;
  11152. }
  11153. }
  11154. if (unlikely(!cq)) {
  11155. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  11156. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11157. "0365 Slow-path CQ identifier "
  11158. "(%d) does not exist\n", cqid);
  11159. return;
  11160. }
  11161. /* Process all the entries to the CQ */
  11162. switch (cq->type) {
  11163. case LPFC_MCQ:
  11164. while ((cqe = lpfc_sli4_cq_get(cq))) {
  11165. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  11166. if (!(++ecount % cq->entry_repost))
  11167. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  11168. cq->CQ_mbox++;
  11169. }
  11170. break;
  11171. case LPFC_WCQ:
  11172. while ((cqe = lpfc_sli4_cq_get(cq))) {
  11173. if (cq->subtype == LPFC_FCP)
  11174. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  11175. cqe);
  11176. else
  11177. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  11178. cqe);
  11179. if (!(++ecount % cq->entry_repost))
  11180. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  11181. }
  11182. /* Track the max number of CQEs processed in 1 EQ */
  11183. if (ecount > cq->CQ_max_cqe)
  11184. cq->CQ_max_cqe = ecount;
  11185. break;
  11186. default:
  11187. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11188. "0370 Invalid completion queue type (%d)\n",
  11189. cq->type);
  11190. return;
  11191. }
  11192. /* Catch the no cq entry condition, log an error */
  11193. if (unlikely(ecount == 0))
  11194. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11195. "0371 No entry from the CQ: identifier "
  11196. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  11197. /* In any case, flash and re-arm the RCQ */
  11198. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  11199. /* wake up worker thread if there are works to be done */
  11200. if (workposted)
  11201. lpfc_worker_wake_up(phba);
  11202. }
  11203. /**
  11204. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  11205. * @phba: Pointer to HBA context object.
  11206. * @cq: Pointer to associated CQ
  11207. * @wcqe: Pointer to work-queue completion queue entry.
  11208. *
  11209. * This routine process a fast-path work queue completion entry from fast-path
  11210. * event queue for FCP command response completion.
  11211. **/
  11212. static void
  11213. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11214. struct lpfc_wcqe_complete *wcqe)
  11215. {
  11216. struct lpfc_sli_ring *pring = cq->pring;
  11217. struct lpfc_iocbq *cmdiocbq;
  11218. struct lpfc_iocbq irspiocbq;
  11219. unsigned long iflags;
  11220. /* Check for response status */
  11221. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  11222. /* If resource errors reported from HBA, reduce queue
  11223. * depth of the SCSI device.
  11224. */
  11225. if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
  11226. IOSTAT_LOCAL_REJECT)) &&
  11227. ((wcqe->parameter & IOERR_PARAM_MASK) ==
  11228. IOERR_NO_RESOURCES))
  11229. phba->lpfc_rampdown_queue_depth(phba);
  11230. /* Log the error status */
  11231. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11232. "0373 FCP complete error: status=x%x, "
  11233. "hw_status=x%x, total_data_specified=%d, "
  11234. "parameter=x%x, word3=x%x\n",
  11235. bf_get(lpfc_wcqe_c_status, wcqe),
  11236. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  11237. wcqe->total_data_placed, wcqe->parameter,
  11238. wcqe->word3);
  11239. }
  11240. /* Look up the FCP command IOCB and create pseudo response IOCB */
  11241. spin_lock_irqsave(&pring->ring_lock, iflags);
  11242. pring->stats.iocb_event++;
  11243. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  11244. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  11245. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  11246. if (unlikely(!cmdiocbq)) {
  11247. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11248. "0374 FCP complete with no corresponding "
  11249. "cmdiocb: iotag (%d)\n",
  11250. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  11251. return;
  11252. }
  11253. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  11254. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11255. "0375 FCP cmdiocb not callback function "
  11256. "iotag: (%d)\n",
  11257. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  11258. return;
  11259. }
  11260. /* Fake the irspiocb and copy necessary response information */
  11261. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  11262. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  11263. spin_lock_irqsave(&phba->hbalock, iflags);
  11264. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  11265. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11266. }
  11267. /* Pass the cmd_iocb and the rsp state to the upper layer */
  11268. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  11269. }
  11270. /**
  11271. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  11272. * @phba: Pointer to HBA context object.
  11273. * @cq: Pointer to completion queue.
  11274. * @wcqe: Pointer to work-queue completion queue entry.
  11275. *
  11276. * This routine handles an fast-path WQ entry comsumed event by invoking the
  11277. * proper WQ release routine to the slow-path WQ.
  11278. **/
  11279. static void
  11280. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11281. struct lpfc_wcqe_release *wcqe)
  11282. {
  11283. struct lpfc_queue *childwq;
  11284. bool wqid_matched = false;
  11285. uint16_t fcp_wqid;
  11286. /* Check for fast-path FCP work queue release */
  11287. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  11288. list_for_each_entry(childwq, &cq->child_list, list) {
  11289. if (childwq->queue_id == fcp_wqid) {
  11290. lpfc_sli4_wq_release(childwq,
  11291. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  11292. wqid_matched = true;
  11293. break;
  11294. }
  11295. }
  11296. /* Report warning log message if no match found */
  11297. if (wqid_matched != true)
  11298. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11299. "2580 Fast-path wqe consume event carries "
  11300. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  11301. }
  11302. /**
  11303. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  11304. * @cq: Pointer to the completion queue.
  11305. * @eqe: Pointer to fast-path completion queue entry.
  11306. *
  11307. * This routine process a fast-path work queue completion entry from fast-path
  11308. * event queue for FCP command response completion.
  11309. **/
  11310. static int
  11311. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11312. struct lpfc_cqe *cqe)
  11313. {
  11314. struct lpfc_wcqe_release wcqe;
  11315. bool workposted = false;
  11316. /* Copy the work queue CQE and convert endian order if needed */
  11317. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  11318. /* Check and process for different type of WCQE and dispatch */
  11319. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  11320. case CQE_CODE_COMPL_WQE:
  11321. cq->CQ_wq++;
  11322. /* Process the WQ complete event */
  11323. phba->last_completion_time = jiffies;
  11324. lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
  11325. (struct lpfc_wcqe_complete *)&wcqe);
  11326. break;
  11327. case CQE_CODE_RELEASE_WQE:
  11328. cq->CQ_release_wqe++;
  11329. /* Process the WQ release event */
  11330. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  11331. (struct lpfc_wcqe_release *)&wcqe);
  11332. break;
  11333. case CQE_CODE_XRI_ABORTED:
  11334. cq->CQ_xri_aborted++;
  11335. /* Process the WQ XRI abort event */
  11336. phba->last_completion_time = jiffies;
  11337. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  11338. (struct sli4_wcqe_xri_aborted *)&wcqe);
  11339. break;
  11340. default:
  11341. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11342. "0144 Not a valid WCQE code: x%x\n",
  11343. bf_get(lpfc_wcqe_c_code, &wcqe));
  11344. break;
  11345. }
  11346. return workposted;
  11347. }
  11348. /**
  11349. * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
  11350. * @phba: Pointer to HBA context object.
  11351. * @eqe: Pointer to fast-path event queue entry.
  11352. *
  11353. * This routine process a event queue entry from the fast-path event queue.
  11354. * It will check the MajorCode and MinorCode to determine this is for a
  11355. * completion event on a completion queue, if not, an error shall be logged
  11356. * and just return. Otherwise, it will get to the corresponding completion
  11357. * queue and process all the entries on the completion queue, rearm the
  11358. * completion queue, and then return.
  11359. **/
  11360. static void
  11361. lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  11362. uint32_t qidx)
  11363. {
  11364. struct lpfc_queue *cq;
  11365. struct lpfc_cqe *cqe;
  11366. bool workposted = false;
  11367. uint16_t cqid;
  11368. int ecount = 0;
  11369. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  11370. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11371. "0366 Not a valid completion "
  11372. "event: majorcode=x%x, minorcode=x%x\n",
  11373. bf_get_le32(lpfc_eqe_major_code, eqe),
  11374. bf_get_le32(lpfc_eqe_minor_code, eqe));
  11375. return;
  11376. }
  11377. /* Get the reference to the corresponding CQ */
  11378. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  11379. /* Check if this is a Slow path event */
  11380. if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
  11381. lpfc_sli4_sp_handle_eqe(phba, eqe,
  11382. phba->sli4_hba.hba_eq[qidx]);
  11383. return;
  11384. }
  11385. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  11386. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11387. "3146 Fast-path completion queues "
  11388. "does not exist\n");
  11389. return;
  11390. }
  11391. cq = phba->sli4_hba.fcp_cq[qidx];
  11392. if (unlikely(!cq)) {
  11393. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  11394. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11395. "0367 Fast-path completion queue "
  11396. "(%d) does not exist\n", qidx);
  11397. return;
  11398. }
  11399. if (unlikely(cqid != cq->queue_id)) {
  11400. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11401. "0368 Miss-matched fast-path completion "
  11402. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  11403. cqid, cq->queue_id);
  11404. return;
  11405. }
  11406. /* Process all the entries to the CQ */
  11407. while ((cqe = lpfc_sli4_cq_get(cq))) {
  11408. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  11409. if (!(++ecount % cq->entry_repost))
  11410. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  11411. }
  11412. /* Track the max number of CQEs processed in 1 EQ */
  11413. if (ecount > cq->CQ_max_cqe)
  11414. cq->CQ_max_cqe = ecount;
  11415. /* Catch the no cq entry condition */
  11416. if (unlikely(ecount == 0))
  11417. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11418. "0369 No entry from fast-path completion "
  11419. "queue fcpcqid=%d\n", cq->queue_id);
  11420. /* In any case, flash and re-arm the CQ */
  11421. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  11422. /* wake up worker thread if there are works to be done */
  11423. if (workposted)
  11424. lpfc_worker_wake_up(phba);
  11425. }
  11426. static void
  11427. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11428. {
  11429. struct lpfc_eqe *eqe;
  11430. /* walk all the EQ entries and drop on the floor */
  11431. while ((eqe = lpfc_sli4_eq_get(eq)))
  11432. ;
  11433. /* Clear and re-arm the EQ */
  11434. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  11435. }
  11436. /**
  11437. * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
  11438. * entry
  11439. * @phba: Pointer to HBA context object.
  11440. * @eqe: Pointer to fast-path event queue entry.
  11441. *
  11442. * This routine process a event queue entry from the Flash Optimized Fabric
  11443. * event queue. It will check the MajorCode and MinorCode to determine this
  11444. * is for a completion event on a completion queue, if not, an error shall be
  11445. * logged and just return. Otherwise, it will get to the corresponding
  11446. * completion queue and process all the entries on the completion queue, rearm
  11447. * the completion queue, and then return.
  11448. **/
  11449. static void
  11450. lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  11451. {
  11452. struct lpfc_queue *cq;
  11453. struct lpfc_cqe *cqe;
  11454. bool workposted = false;
  11455. uint16_t cqid;
  11456. int ecount = 0;
  11457. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  11458. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11459. "9147 Not a valid completion "
  11460. "event: majorcode=x%x, minorcode=x%x\n",
  11461. bf_get_le32(lpfc_eqe_major_code, eqe),
  11462. bf_get_le32(lpfc_eqe_minor_code, eqe));
  11463. return;
  11464. }
  11465. /* Get the reference to the corresponding CQ */
  11466. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  11467. /* Next check for OAS */
  11468. cq = phba->sli4_hba.oas_cq;
  11469. if (unlikely(!cq)) {
  11470. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  11471. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11472. "9148 OAS completion queue "
  11473. "does not exist\n");
  11474. return;
  11475. }
  11476. if (unlikely(cqid != cq->queue_id)) {
  11477. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11478. "9149 Miss-matched fast-path compl "
  11479. "queue id: eqcqid=%d, fcpcqid=%d\n",
  11480. cqid, cq->queue_id);
  11481. return;
  11482. }
  11483. /* Process all the entries to the OAS CQ */
  11484. while ((cqe = lpfc_sli4_cq_get(cq))) {
  11485. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  11486. if (!(++ecount % cq->entry_repost))
  11487. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  11488. }
  11489. /* Track the max number of CQEs processed in 1 EQ */
  11490. if (ecount > cq->CQ_max_cqe)
  11491. cq->CQ_max_cqe = ecount;
  11492. /* Catch the no cq entry condition */
  11493. if (unlikely(ecount == 0))
  11494. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11495. "9153 No entry from fast-path completion "
  11496. "queue fcpcqid=%d\n", cq->queue_id);
  11497. /* In any case, flash and re-arm the CQ */
  11498. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  11499. /* wake up worker thread if there are works to be done */
  11500. if (workposted)
  11501. lpfc_worker_wake_up(phba);
  11502. }
  11503. /**
  11504. * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
  11505. * @irq: Interrupt number.
  11506. * @dev_id: The device context pointer.
  11507. *
  11508. * This function is directly called from the PCI layer as an interrupt
  11509. * service routine when device with SLI-4 interface spec is enabled with
  11510. * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
  11511. * IOCB ring event in the HBA. However, when the device is enabled with either
  11512. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  11513. * device-level interrupt handler. When the PCI slot is in error recovery
  11514. * or the HBA is undergoing initialization, the interrupt handler will not
  11515. * process the interrupt. The Flash Optimized Fabric ring event are handled in
  11516. * the intrrupt context. This function is called without any lock held.
  11517. * It gets the hbalock to access and update SLI data structures. Note that,
  11518. * the EQ to CQ are one-to-one map such that the EQ index is
  11519. * equal to that of CQ index.
  11520. *
  11521. * This function returns IRQ_HANDLED when interrupt is handled else it
  11522. * returns IRQ_NONE.
  11523. **/
  11524. irqreturn_t
  11525. lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
  11526. {
  11527. struct lpfc_hba *phba;
  11528. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  11529. struct lpfc_queue *eq;
  11530. struct lpfc_eqe *eqe;
  11531. unsigned long iflag;
  11532. int ecount = 0;
  11533. /* Get the driver's phba structure from the dev_id */
  11534. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  11535. phba = fcp_eq_hdl->phba;
  11536. if (unlikely(!phba))
  11537. return IRQ_NONE;
  11538. /* Get to the EQ struct associated with this vector */
  11539. eq = phba->sli4_hba.fof_eq;
  11540. if (unlikely(!eq))
  11541. return IRQ_NONE;
  11542. /* Check device state for handling interrupt */
  11543. if (unlikely(lpfc_intr_state_check(phba))) {
  11544. eq->EQ_badstate++;
  11545. /* Check again for link_state with lock held */
  11546. spin_lock_irqsave(&phba->hbalock, iflag);
  11547. if (phba->link_state < LPFC_LINK_DOWN)
  11548. /* Flush, clear interrupt, and rearm the EQ */
  11549. lpfc_sli4_eq_flush(phba, eq);
  11550. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11551. return IRQ_NONE;
  11552. }
  11553. /*
  11554. * Process all the event on FCP fast-path EQ
  11555. */
  11556. while ((eqe = lpfc_sli4_eq_get(eq))) {
  11557. lpfc_sli4_fof_handle_eqe(phba, eqe);
  11558. if (!(++ecount % eq->entry_repost))
  11559. lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
  11560. eq->EQ_processed++;
  11561. }
  11562. /* Track the max number of EQEs processed in 1 intr */
  11563. if (ecount > eq->EQ_max_eqe)
  11564. eq->EQ_max_eqe = ecount;
  11565. if (unlikely(ecount == 0)) {
  11566. eq->EQ_no_entry++;
  11567. if (phba->intr_type == MSIX)
  11568. /* MSI-X treated interrupt served as no EQ share INT */
  11569. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11570. "9145 MSI-X interrupt with no EQE\n");
  11571. else {
  11572. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11573. "9146 ISR interrupt with no EQE\n");
  11574. /* Non MSI-X treated on interrupt as EQ share INT */
  11575. return IRQ_NONE;
  11576. }
  11577. }
  11578. /* Always clear and re-arm the fast-path EQ */
  11579. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  11580. return IRQ_HANDLED;
  11581. }
  11582. /**
  11583. * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
  11584. * @irq: Interrupt number.
  11585. * @dev_id: The device context pointer.
  11586. *
  11587. * This function is directly called from the PCI layer as an interrupt
  11588. * service routine when device with SLI-4 interface spec is enabled with
  11589. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  11590. * ring event in the HBA. However, when the device is enabled with either
  11591. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  11592. * device-level interrupt handler. When the PCI slot is in error recovery
  11593. * or the HBA is undergoing initialization, the interrupt handler will not
  11594. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  11595. * the intrrupt context. This function is called without any lock held.
  11596. * It gets the hbalock to access and update SLI data structures. Note that,
  11597. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  11598. * equal to that of FCP CQ index.
  11599. *
  11600. * The link attention and ELS ring attention events are handled
  11601. * by the worker thread. The interrupt handler signals the worker thread
  11602. * and returns for these events. This function is called without any lock
  11603. * held. It gets the hbalock to access and update SLI data structures.
  11604. *
  11605. * This function returns IRQ_HANDLED when interrupt is handled else it
  11606. * returns IRQ_NONE.
  11607. **/
  11608. irqreturn_t
  11609. lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
  11610. {
  11611. struct lpfc_hba *phba;
  11612. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  11613. struct lpfc_queue *fpeq;
  11614. struct lpfc_eqe *eqe;
  11615. unsigned long iflag;
  11616. int ecount = 0;
  11617. int fcp_eqidx;
  11618. /* Get the driver's phba structure from the dev_id */
  11619. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  11620. phba = fcp_eq_hdl->phba;
  11621. fcp_eqidx = fcp_eq_hdl->idx;
  11622. if (unlikely(!phba))
  11623. return IRQ_NONE;
  11624. if (unlikely(!phba->sli4_hba.hba_eq))
  11625. return IRQ_NONE;
  11626. /* Get to the EQ struct associated with this vector */
  11627. fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
  11628. if (unlikely(!fpeq))
  11629. return IRQ_NONE;
  11630. if (lpfc_fcp_look_ahead) {
  11631. if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
  11632. lpfc_sli4_eq_clr_intr(fpeq);
  11633. else {
  11634. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11635. return IRQ_NONE;
  11636. }
  11637. }
  11638. /* Check device state for handling interrupt */
  11639. if (unlikely(lpfc_intr_state_check(phba))) {
  11640. fpeq->EQ_badstate++;
  11641. /* Check again for link_state with lock held */
  11642. spin_lock_irqsave(&phba->hbalock, iflag);
  11643. if (phba->link_state < LPFC_LINK_DOWN)
  11644. /* Flush, clear interrupt, and rearm the EQ */
  11645. lpfc_sli4_eq_flush(phba, fpeq);
  11646. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11647. if (lpfc_fcp_look_ahead)
  11648. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11649. return IRQ_NONE;
  11650. }
  11651. /*
  11652. * Process all the event on FCP fast-path EQ
  11653. */
  11654. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  11655. if (eqe == NULL)
  11656. break;
  11657. lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
  11658. if (!(++ecount % fpeq->entry_repost))
  11659. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  11660. fpeq->EQ_processed++;
  11661. }
  11662. /* Track the max number of EQEs processed in 1 intr */
  11663. if (ecount > fpeq->EQ_max_eqe)
  11664. fpeq->EQ_max_eqe = ecount;
  11665. /* Always clear and re-arm the fast-path EQ */
  11666. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  11667. if (unlikely(ecount == 0)) {
  11668. fpeq->EQ_no_entry++;
  11669. if (lpfc_fcp_look_ahead) {
  11670. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11671. return IRQ_NONE;
  11672. }
  11673. if (phba->intr_type == MSIX)
  11674. /* MSI-X treated interrupt served as no EQ share INT */
  11675. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11676. "0358 MSI-X interrupt with no EQE\n");
  11677. else
  11678. /* Non MSI-X treated on interrupt as EQ share INT */
  11679. return IRQ_NONE;
  11680. }
  11681. if (lpfc_fcp_look_ahead)
  11682. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11683. return IRQ_HANDLED;
  11684. } /* lpfc_sli4_fp_intr_handler */
  11685. /**
  11686. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  11687. * @irq: Interrupt number.
  11688. * @dev_id: The device context pointer.
  11689. *
  11690. * This function is the device-level interrupt handler to device with SLI-4
  11691. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  11692. * interrupt mode is enabled and there is an event in the HBA which requires
  11693. * driver attention. This function invokes the slow-path interrupt attention
  11694. * handling function and fast-path interrupt attention handling function in
  11695. * turn to process the relevant HBA attention events. This function is called
  11696. * without any lock held. It gets the hbalock to access and update SLI data
  11697. * structures.
  11698. *
  11699. * This function returns IRQ_HANDLED when interrupt is handled, else it
  11700. * returns IRQ_NONE.
  11701. **/
  11702. irqreturn_t
  11703. lpfc_sli4_intr_handler(int irq, void *dev_id)
  11704. {
  11705. struct lpfc_hba *phba;
  11706. irqreturn_t hba_irq_rc;
  11707. bool hba_handled = false;
  11708. int fcp_eqidx;
  11709. /* Get the driver's phba structure from the dev_id */
  11710. phba = (struct lpfc_hba *)dev_id;
  11711. if (unlikely(!phba))
  11712. return IRQ_NONE;
  11713. /*
  11714. * Invoke fast-path host attention interrupt handling as appropriate.
  11715. */
  11716. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
  11717. hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
  11718. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  11719. if (hba_irq_rc == IRQ_HANDLED)
  11720. hba_handled |= true;
  11721. }
  11722. if (phba->cfg_fof) {
  11723. hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
  11724. &phba->sli4_hba.fcp_eq_hdl[0]);
  11725. if (hba_irq_rc == IRQ_HANDLED)
  11726. hba_handled |= true;
  11727. }
  11728. return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
  11729. } /* lpfc_sli4_intr_handler */
  11730. /**
  11731. * lpfc_sli4_queue_free - free a queue structure and associated memory
  11732. * @queue: The queue structure to free.
  11733. *
  11734. * This function frees a queue structure and the DMAable memory used for
  11735. * the host resident queue. This function must be called after destroying the
  11736. * queue on the HBA.
  11737. **/
  11738. void
  11739. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  11740. {
  11741. struct lpfc_dmabuf *dmabuf;
  11742. if (!queue)
  11743. return;
  11744. while (!list_empty(&queue->page_list)) {
  11745. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  11746. list);
  11747. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  11748. dmabuf->virt, dmabuf->phys);
  11749. kfree(dmabuf);
  11750. }
  11751. kfree(queue);
  11752. return;
  11753. }
  11754. /**
  11755. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  11756. * @phba: The HBA that this queue is being created on.
  11757. * @entry_size: The size of each queue entry for this queue.
  11758. * @entry count: The number of entries that this queue will handle.
  11759. *
  11760. * This function allocates a queue structure and the DMAable memory used for
  11761. * the host resident queue. This function must be called before creating the
  11762. * queue on the HBA.
  11763. **/
  11764. struct lpfc_queue *
  11765. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  11766. uint32_t entry_count)
  11767. {
  11768. struct lpfc_queue *queue;
  11769. struct lpfc_dmabuf *dmabuf;
  11770. int x, total_qe_count;
  11771. void *dma_pointer;
  11772. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11773. if (!phba->sli4_hba.pc_sli4_params.supported)
  11774. hw_page_size = SLI4_PAGE_SIZE;
  11775. queue = kzalloc(sizeof(struct lpfc_queue) +
  11776. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  11777. if (!queue)
  11778. return NULL;
  11779. queue->page_count = (ALIGN(entry_size * entry_count,
  11780. hw_page_size))/hw_page_size;
  11781. INIT_LIST_HEAD(&queue->list);
  11782. INIT_LIST_HEAD(&queue->page_list);
  11783. INIT_LIST_HEAD(&queue->child_list);
  11784. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  11785. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  11786. if (!dmabuf)
  11787. goto out_fail;
  11788. dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
  11789. hw_page_size, &dmabuf->phys,
  11790. GFP_KERNEL);
  11791. if (!dmabuf->virt) {
  11792. kfree(dmabuf);
  11793. goto out_fail;
  11794. }
  11795. dmabuf->buffer_tag = x;
  11796. list_add_tail(&dmabuf->list, &queue->page_list);
  11797. /* initialize queue's entry array */
  11798. dma_pointer = dmabuf->virt;
  11799. for (; total_qe_count < entry_count &&
  11800. dma_pointer < (hw_page_size + dmabuf->virt);
  11801. total_qe_count++, dma_pointer += entry_size) {
  11802. queue->qe[total_qe_count].address = dma_pointer;
  11803. }
  11804. }
  11805. queue->entry_size = entry_size;
  11806. queue->entry_count = entry_count;
  11807. /*
  11808. * entry_repost is calculated based on the number of entries in the
  11809. * queue. This works out except for RQs. If buffers are NOT initially
  11810. * posted for every RQE, entry_repost should be adjusted accordingly.
  11811. */
  11812. queue->entry_repost = (entry_count >> 3);
  11813. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  11814. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  11815. queue->phba = phba;
  11816. return queue;
  11817. out_fail:
  11818. lpfc_sli4_queue_free(queue);
  11819. return NULL;
  11820. }
  11821. /**
  11822. * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
  11823. * @phba: HBA structure that indicates port to create a queue on.
  11824. * @pci_barset: PCI BAR set flag.
  11825. *
  11826. * This function shall perform iomap of the specified PCI BAR address to host
  11827. * memory address if not already done so and return it. The returned host
  11828. * memory address can be NULL.
  11829. */
  11830. static void __iomem *
  11831. lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
  11832. {
  11833. if (!phba->pcidev)
  11834. return NULL;
  11835. switch (pci_barset) {
  11836. case WQ_PCI_BAR_0_AND_1:
  11837. return phba->pci_bar0_memmap_p;
  11838. case WQ_PCI_BAR_2_AND_3:
  11839. return phba->pci_bar2_memmap_p;
  11840. case WQ_PCI_BAR_4_AND_5:
  11841. return phba->pci_bar4_memmap_p;
  11842. default:
  11843. break;
  11844. }
  11845. return NULL;
  11846. }
  11847. /**
  11848. * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
  11849. * @phba: HBA structure that indicates port to create a queue on.
  11850. * @startq: The starting FCP EQ to modify
  11851. *
  11852. * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
  11853. *
  11854. * The @phba struct is used to send mailbox command to HBA. The @startq
  11855. * is used to get the starting FCP EQ to change.
  11856. * This function is asynchronous and will wait for the mailbox
  11857. * command to finish before continuing.
  11858. *
  11859. * On success this function will return a zero. If unable to allocate enough
  11860. * memory this function will return -ENOMEM. If the queue create mailbox command
  11861. * fails this function will return -ENXIO.
  11862. **/
  11863. int
  11864. lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint32_t startq)
  11865. {
  11866. struct lpfc_mbx_modify_eq_delay *eq_delay;
  11867. LPFC_MBOXQ_t *mbox;
  11868. struct lpfc_queue *eq;
  11869. int cnt, rc, length, status = 0;
  11870. uint32_t shdr_status, shdr_add_status;
  11871. uint32_t result;
  11872. int fcp_eqidx;
  11873. union lpfc_sli4_cfg_shdr *shdr;
  11874. uint16_t dmult;
  11875. if (startq >= phba->cfg_fcp_io_channel)
  11876. return 0;
  11877. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11878. if (!mbox)
  11879. return -ENOMEM;
  11880. length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
  11881. sizeof(struct lpfc_sli4_cfg_mhdr));
  11882. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11883. LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
  11884. length, LPFC_SLI4_MBX_EMBED);
  11885. eq_delay = &mbox->u.mqe.un.eq_delay;
  11886. /* Calculate delay multiper from maximum interrupt per second */
  11887. result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
  11888. if (result > LPFC_DMULT_CONST)
  11889. dmult = 0;
  11890. else
  11891. dmult = LPFC_DMULT_CONST/result - 1;
  11892. cnt = 0;
  11893. for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
  11894. fcp_eqidx++) {
  11895. eq = phba->sli4_hba.hba_eq[fcp_eqidx];
  11896. if (!eq)
  11897. continue;
  11898. eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
  11899. eq_delay->u.request.eq[cnt].phase = 0;
  11900. eq_delay->u.request.eq[cnt].delay_multi = dmult;
  11901. cnt++;
  11902. if (cnt >= LPFC_MAX_EQ_DELAY)
  11903. break;
  11904. }
  11905. eq_delay->u.request.num_eq = cnt;
  11906. mbox->vport = phba->pport;
  11907. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11908. mbox->context1 = NULL;
  11909. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11910. shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
  11911. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11912. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11913. if (shdr_status || shdr_add_status || rc) {
  11914. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11915. "2512 MODIFY_EQ_DELAY mailbox failed with "
  11916. "status x%x add_status x%x, mbx status x%x\n",
  11917. shdr_status, shdr_add_status, rc);
  11918. status = -ENXIO;
  11919. }
  11920. mempool_free(mbox, phba->mbox_mem_pool);
  11921. return status;
  11922. }
  11923. /**
  11924. * lpfc_eq_create - Create an Event Queue on the HBA
  11925. * @phba: HBA structure that indicates port to create a queue on.
  11926. * @eq: The queue structure to use to create the event queue.
  11927. * @imax: The maximum interrupt per second limit.
  11928. *
  11929. * This function creates an event queue, as detailed in @eq, on a port,
  11930. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  11931. *
  11932. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  11933. * is used to get the entry count and entry size that are necessary to
  11934. * determine the number of pages to allocate and use for this queue. This
  11935. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  11936. * event queue. This function is asynchronous and will wait for the mailbox
  11937. * command to finish before continuing.
  11938. *
  11939. * On success this function will return a zero. If unable to allocate enough
  11940. * memory this function will return -ENOMEM. If the queue create mailbox command
  11941. * fails this function will return -ENXIO.
  11942. **/
  11943. int
  11944. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
  11945. {
  11946. struct lpfc_mbx_eq_create *eq_create;
  11947. LPFC_MBOXQ_t *mbox;
  11948. int rc, length, status = 0;
  11949. struct lpfc_dmabuf *dmabuf;
  11950. uint32_t shdr_status, shdr_add_status;
  11951. union lpfc_sli4_cfg_shdr *shdr;
  11952. uint16_t dmult;
  11953. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11954. /* sanity check on queue memory */
  11955. if (!eq)
  11956. return -ENODEV;
  11957. if (!phba->sli4_hba.pc_sli4_params.supported)
  11958. hw_page_size = SLI4_PAGE_SIZE;
  11959. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11960. if (!mbox)
  11961. return -ENOMEM;
  11962. length = (sizeof(struct lpfc_mbx_eq_create) -
  11963. sizeof(struct lpfc_sli4_cfg_mhdr));
  11964. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11965. LPFC_MBOX_OPCODE_EQ_CREATE,
  11966. length, LPFC_SLI4_MBX_EMBED);
  11967. eq_create = &mbox->u.mqe.un.eq_create;
  11968. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  11969. eq->page_count);
  11970. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  11971. LPFC_EQE_SIZE);
  11972. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  11973. /* don't setup delay multiplier using EQ_CREATE */
  11974. dmult = 0;
  11975. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  11976. dmult);
  11977. switch (eq->entry_count) {
  11978. default:
  11979. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11980. "0360 Unsupported EQ count. (%d)\n",
  11981. eq->entry_count);
  11982. if (eq->entry_count < 256)
  11983. return -EINVAL;
  11984. /* otherwise default to smallest count (drop through) */
  11985. case 256:
  11986. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11987. LPFC_EQ_CNT_256);
  11988. break;
  11989. case 512:
  11990. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11991. LPFC_EQ_CNT_512);
  11992. break;
  11993. case 1024:
  11994. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11995. LPFC_EQ_CNT_1024);
  11996. break;
  11997. case 2048:
  11998. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11999. LPFC_EQ_CNT_2048);
  12000. break;
  12001. case 4096:
  12002. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  12003. LPFC_EQ_CNT_4096);
  12004. break;
  12005. }
  12006. list_for_each_entry(dmabuf, &eq->page_list, list) {
  12007. memset(dmabuf->virt, 0, hw_page_size);
  12008. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12009. putPaddrLow(dmabuf->phys);
  12010. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12011. putPaddrHigh(dmabuf->phys);
  12012. }
  12013. mbox->vport = phba->pport;
  12014. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12015. mbox->context1 = NULL;
  12016. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12017. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  12018. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12019. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12020. if (shdr_status || shdr_add_status || rc) {
  12021. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12022. "2500 EQ_CREATE mailbox failed with "
  12023. "status x%x add_status x%x, mbx status x%x\n",
  12024. shdr_status, shdr_add_status, rc);
  12025. status = -ENXIO;
  12026. }
  12027. eq->type = LPFC_EQ;
  12028. eq->subtype = LPFC_NONE;
  12029. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  12030. if (eq->queue_id == 0xFFFF)
  12031. status = -ENXIO;
  12032. eq->host_index = 0;
  12033. eq->hba_index = 0;
  12034. mempool_free(mbox, phba->mbox_mem_pool);
  12035. return status;
  12036. }
  12037. /**
  12038. * lpfc_cq_create - Create a Completion Queue on the HBA
  12039. * @phba: HBA structure that indicates port to create a queue on.
  12040. * @cq: The queue structure to use to create the completion queue.
  12041. * @eq: The event queue to bind this completion queue to.
  12042. *
  12043. * This function creates a completion queue, as detailed in @wq, on a port,
  12044. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  12045. *
  12046. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  12047. * is used to get the entry count and entry size that are necessary to
  12048. * determine the number of pages to allocate and use for this queue. The @eq
  12049. * is used to indicate which event queue to bind this completion queue to. This
  12050. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  12051. * completion queue. This function is asynchronous and will wait for the mailbox
  12052. * command to finish before continuing.
  12053. *
  12054. * On success this function will return a zero. If unable to allocate enough
  12055. * memory this function will return -ENOMEM. If the queue create mailbox command
  12056. * fails this function will return -ENXIO.
  12057. **/
  12058. int
  12059. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  12060. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  12061. {
  12062. struct lpfc_mbx_cq_create *cq_create;
  12063. struct lpfc_dmabuf *dmabuf;
  12064. LPFC_MBOXQ_t *mbox;
  12065. int rc, length, status = 0;
  12066. uint32_t shdr_status, shdr_add_status;
  12067. union lpfc_sli4_cfg_shdr *shdr;
  12068. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  12069. /* sanity check on queue memory */
  12070. if (!cq || !eq)
  12071. return -ENODEV;
  12072. if (!phba->sli4_hba.pc_sli4_params.supported)
  12073. hw_page_size = SLI4_PAGE_SIZE;
  12074. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12075. if (!mbox)
  12076. return -ENOMEM;
  12077. length = (sizeof(struct lpfc_mbx_cq_create) -
  12078. sizeof(struct lpfc_sli4_cfg_mhdr));
  12079. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12080. LPFC_MBOX_OPCODE_CQ_CREATE,
  12081. length, LPFC_SLI4_MBX_EMBED);
  12082. cq_create = &mbox->u.mqe.un.cq_create;
  12083. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  12084. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  12085. cq->page_count);
  12086. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  12087. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  12088. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12089. phba->sli4_hba.pc_sli4_params.cqv);
  12090. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  12091. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  12092. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  12093. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  12094. eq->queue_id);
  12095. } else {
  12096. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  12097. eq->queue_id);
  12098. }
  12099. switch (cq->entry_count) {
  12100. default:
  12101. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12102. "0361 Unsupported CQ count. (%d)\n",
  12103. cq->entry_count);
  12104. if (cq->entry_count < 256) {
  12105. status = -EINVAL;
  12106. goto out;
  12107. }
  12108. /* otherwise default to smallest count (drop through) */
  12109. case 256:
  12110. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  12111. LPFC_CQ_CNT_256);
  12112. break;
  12113. case 512:
  12114. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  12115. LPFC_CQ_CNT_512);
  12116. break;
  12117. case 1024:
  12118. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  12119. LPFC_CQ_CNT_1024);
  12120. break;
  12121. }
  12122. list_for_each_entry(dmabuf, &cq->page_list, list) {
  12123. memset(dmabuf->virt, 0, hw_page_size);
  12124. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12125. putPaddrLow(dmabuf->phys);
  12126. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12127. putPaddrHigh(dmabuf->phys);
  12128. }
  12129. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12130. /* The IOCTL status is embedded in the mailbox subheader. */
  12131. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12132. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12133. if (shdr_status || shdr_add_status || rc) {
  12134. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12135. "2501 CQ_CREATE mailbox failed with "
  12136. "status x%x add_status x%x, mbx status x%x\n",
  12137. shdr_status, shdr_add_status, rc);
  12138. status = -ENXIO;
  12139. goto out;
  12140. }
  12141. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  12142. if (cq->queue_id == 0xFFFF) {
  12143. status = -ENXIO;
  12144. goto out;
  12145. }
  12146. /* link the cq onto the parent eq child list */
  12147. list_add_tail(&cq->list, &eq->child_list);
  12148. /* Set up completion queue's type and subtype */
  12149. cq->type = type;
  12150. cq->subtype = subtype;
  12151. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  12152. cq->assoc_qid = eq->queue_id;
  12153. cq->host_index = 0;
  12154. cq->hba_index = 0;
  12155. out:
  12156. mempool_free(mbox, phba->mbox_mem_pool);
  12157. return status;
  12158. }
  12159. /**
  12160. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  12161. * @phba: HBA structure that indicates port to create a queue on.
  12162. * @mq: The queue structure to use to create the mailbox queue.
  12163. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  12164. * @cq: The completion queue to associate with this cq.
  12165. *
  12166. * This function provides failback (fb) functionality when the
  12167. * mq_create_ext fails on older FW generations. It's purpose is identical
  12168. * to mq_create_ext otherwise.
  12169. *
  12170. * This routine cannot fail as all attributes were previously accessed and
  12171. * initialized in mq_create_ext.
  12172. **/
  12173. static void
  12174. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  12175. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  12176. {
  12177. struct lpfc_mbx_mq_create *mq_create;
  12178. struct lpfc_dmabuf *dmabuf;
  12179. int length;
  12180. length = (sizeof(struct lpfc_mbx_mq_create) -
  12181. sizeof(struct lpfc_sli4_cfg_mhdr));
  12182. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12183. LPFC_MBOX_OPCODE_MQ_CREATE,
  12184. length, LPFC_SLI4_MBX_EMBED);
  12185. mq_create = &mbox->u.mqe.un.mq_create;
  12186. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  12187. mq->page_count);
  12188. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  12189. cq->queue_id);
  12190. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  12191. switch (mq->entry_count) {
  12192. case 16:
  12193. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  12194. LPFC_MQ_RING_SIZE_16);
  12195. break;
  12196. case 32:
  12197. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  12198. LPFC_MQ_RING_SIZE_32);
  12199. break;
  12200. case 64:
  12201. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  12202. LPFC_MQ_RING_SIZE_64);
  12203. break;
  12204. case 128:
  12205. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  12206. LPFC_MQ_RING_SIZE_128);
  12207. break;
  12208. }
  12209. list_for_each_entry(dmabuf, &mq->page_list, list) {
  12210. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12211. putPaddrLow(dmabuf->phys);
  12212. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12213. putPaddrHigh(dmabuf->phys);
  12214. }
  12215. }
  12216. /**
  12217. * lpfc_mq_create - Create a mailbox Queue on the HBA
  12218. * @phba: HBA structure that indicates port to create a queue on.
  12219. * @mq: The queue structure to use to create the mailbox queue.
  12220. * @cq: The completion queue to associate with this cq.
  12221. * @subtype: The queue's subtype.
  12222. *
  12223. * This function creates a mailbox queue, as detailed in @mq, on a port,
  12224. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  12225. *
  12226. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  12227. * is used to get the entry count and entry size that are necessary to
  12228. * determine the number of pages to allocate and use for this queue. This
  12229. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  12230. * mailbox queue. This function is asynchronous and will wait for the mailbox
  12231. * command to finish before continuing.
  12232. *
  12233. * On success this function will return a zero. If unable to allocate enough
  12234. * memory this function will return -ENOMEM. If the queue create mailbox command
  12235. * fails this function will return -ENXIO.
  12236. **/
  12237. int32_t
  12238. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  12239. struct lpfc_queue *cq, uint32_t subtype)
  12240. {
  12241. struct lpfc_mbx_mq_create *mq_create;
  12242. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  12243. struct lpfc_dmabuf *dmabuf;
  12244. LPFC_MBOXQ_t *mbox;
  12245. int rc, length, status = 0;
  12246. uint32_t shdr_status, shdr_add_status;
  12247. union lpfc_sli4_cfg_shdr *shdr;
  12248. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  12249. /* sanity check on queue memory */
  12250. if (!mq || !cq)
  12251. return -ENODEV;
  12252. if (!phba->sli4_hba.pc_sli4_params.supported)
  12253. hw_page_size = SLI4_PAGE_SIZE;
  12254. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12255. if (!mbox)
  12256. return -ENOMEM;
  12257. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  12258. sizeof(struct lpfc_sli4_cfg_mhdr));
  12259. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12260. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  12261. length, LPFC_SLI4_MBX_EMBED);
  12262. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  12263. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  12264. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  12265. &mq_create_ext->u.request, mq->page_count);
  12266. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  12267. &mq_create_ext->u.request, 1);
  12268. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  12269. &mq_create_ext->u.request, 1);
  12270. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  12271. &mq_create_ext->u.request, 1);
  12272. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  12273. &mq_create_ext->u.request, 1);
  12274. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  12275. &mq_create_ext->u.request, 1);
  12276. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  12277. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12278. phba->sli4_hba.pc_sli4_params.mqv);
  12279. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  12280. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  12281. cq->queue_id);
  12282. else
  12283. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  12284. cq->queue_id);
  12285. switch (mq->entry_count) {
  12286. default:
  12287. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12288. "0362 Unsupported MQ count. (%d)\n",
  12289. mq->entry_count);
  12290. if (mq->entry_count < 16) {
  12291. status = -EINVAL;
  12292. goto out;
  12293. }
  12294. /* otherwise default to smallest count (drop through) */
  12295. case 16:
  12296. bf_set(lpfc_mq_context_ring_size,
  12297. &mq_create_ext->u.request.context,
  12298. LPFC_MQ_RING_SIZE_16);
  12299. break;
  12300. case 32:
  12301. bf_set(lpfc_mq_context_ring_size,
  12302. &mq_create_ext->u.request.context,
  12303. LPFC_MQ_RING_SIZE_32);
  12304. break;
  12305. case 64:
  12306. bf_set(lpfc_mq_context_ring_size,
  12307. &mq_create_ext->u.request.context,
  12308. LPFC_MQ_RING_SIZE_64);
  12309. break;
  12310. case 128:
  12311. bf_set(lpfc_mq_context_ring_size,
  12312. &mq_create_ext->u.request.context,
  12313. LPFC_MQ_RING_SIZE_128);
  12314. break;
  12315. }
  12316. list_for_each_entry(dmabuf, &mq->page_list, list) {
  12317. memset(dmabuf->virt, 0, hw_page_size);
  12318. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  12319. putPaddrLow(dmabuf->phys);
  12320. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  12321. putPaddrHigh(dmabuf->phys);
  12322. }
  12323. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12324. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  12325. &mq_create_ext->u.response);
  12326. if (rc != MBX_SUCCESS) {
  12327. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12328. "2795 MQ_CREATE_EXT failed with "
  12329. "status x%x. Failback to MQ_CREATE.\n",
  12330. rc);
  12331. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  12332. mq_create = &mbox->u.mqe.un.mq_create;
  12333. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12334. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  12335. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  12336. &mq_create->u.response);
  12337. }
  12338. /* The IOCTL status is embedded in the mailbox subheader. */
  12339. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12340. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12341. if (shdr_status || shdr_add_status || rc) {
  12342. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12343. "2502 MQ_CREATE mailbox failed with "
  12344. "status x%x add_status x%x, mbx status x%x\n",
  12345. shdr_status, shdr_add_status, rc);
  12346. status = -ENXIO;
  12347. goto out;
  12348. }
  12349. if (mq->queue_id == 0xFFFF) {
  12350. status = -ENXIO;
  12351. goto out;
  12352. }
  12353. mq->type = LPFC_MQ;
  12354. mq->assoc_qid = cq->queue_id;
  12355. mq->subtype = subtype;
  12356. mq->host_index = 0;
  12357. mq->hba_index = 0;
  12358. /* link the mq onto the parent cq child list */
  12359. list_add_tail(&mq->list, &cq->child_list);
  12360. out:
  12361. mempool_free(mbox, phba->mbox_mem_pool);
  12362. return status;
  12363. }
  12364. /**
  12365. * lpfc_wq_create - Create a Work Queue on the HBA
  12366. * @phba: HBA structure that indicates port to create a queue on.
  12367. * @wq: The queue structure to use to create the work queue.
  12368. * @cq: The completion queue to bind this work queue to.
  12369. * @subtype: The subtype of the work queue indicating its functionality.
  12370. *
  12371. * This function creates a work queue, as detailed in @wq, on a port, described
  12372. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  12373. *
  12374. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  12375. * is used to get the entry count and entry size that are necessary to
  12376. * determine the number of pages to allocate and use for this queue. The @cq
  12377. * is used to indicate which completion queue to bind this work queue to. This
  12378. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  12379. * work queue. This function is asynchronous and will wait for the mailbox
  12380. * command to finish before continuing.
  12381. *
  12382. * On success this function will return a zero. If unable to allocate enough
  12383. * memory this function will return -ENOMEM. If the queue create mailbox command
  12384. * fails this function will return -ENXIO.
  12385. **/
  12386. int
  12387. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  12388. struct lpfc_queue *cq, uint32_t subtype)
  12389. {
  12390. struct lpfc_mbx_wq_create *wq_create;
  12391. struct lpfc_dmabuf *dmabuf;
  12392. LPFC_MBOXQ_t *mbox;
  12393. int rc, length, status = 0;
  12394. uint32_t shdr_status, shdr_add_status;
  12395. union lpfc_sli4_cfg_shdr *shdr;
  12396. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  12397. struct dma_address *page;
  12398. void __iomem *bar_memmap_p;
  12399. uint32_t db_offset;
  12400. uint16_t pci_barset;
  12401. /* sanity check on queue memory */
  12402. if (!wq || !cq)
  12403. return -ENODEV;
  12404. if (!phba->sli4_hba.pc_sli4_params.supported)
  12405. hw_page_size = SLI4_PAGE_SIZE;
  12406. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12407. if (!mbox)
  12408. return -ENOMEM;
  12409. length = (sizeof(struct lpfc_mbx_wq_create) -
  12410. sizeof(struct lpfc_sli4_cfg_mhdr));
  12411. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12412. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  12413. length, LPFC_SLI4_MBX_EMBED);
  12414. wq_create = &mbox->u.mqe.un.wq_create;
  12415. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  12416. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  12417. wq->page_count);
  12418. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  12419. cq->queue_id);
  12420. /* wqv is the earliest version supported, NOT the latest */
  12421. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12422. phba->sli4_hba.pc_sli4_params.wqv);
  12423. switch (phba->sli4_hba.pc_sli4_params.wqv) {
  12424. case LPFC_Q_CREATE_VERSION_0:
  12425. switch (wq->entry_size) {
  12426. default:
  12427. case 64:
  12428. /* Nothing to do, version 0 ONLY supports 64 byte */
  12429. page = wq_create->u.request.page;
  12430. break;
  12431. case 128:
  12432. if (!(phba->sli4_hba.pc_sli4_params.wqsize &
  12433. LPFC_WQ_SZ128_SUPPORT)) {
  12434. status = -ERANGE;
  12435. goto out;
  12436. }
  12437. /* If we get here the HBA MUST also support V1 and
  12438. * we MUST use it
  12439. */
  12440. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12441. LPFC_Q_CREATE_VERSION_1);
  12442. bf_set(lpfc_mbx_wq_create_wqe_count,
  12443. &wq_create->u.request_1, wq->entry_count);
  12444. bf_set(lpfc_mbx_wq_create_wqe_size,
  12445. &wq_create->u.request_1,
  12446. LPFC_WQ_WQE_SIZE_128);
  12447. bf_set(lpfc_mbx_wq_create_page_size,
  12448. &wq_create->u.request_1,
  12449. (PAGE_SIZE/SLI4_PAGE_SIZE));
  12450. page = wq_create->u.request_1.page;
  12451. break;
  12452. }
  12453. break;
  12454. case LPFC_Q_CREATE_VERSION_1:
  12455. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  12456. wq->entry_count);
  12457. switch (wq->entry_size) {
  12458. default:
  12459. case 64:
  12460. bf_set(lpfc_mbx_wq_create_wqe_size,
  12461. &wq_create->u.request_1,
  12462. LPFC_WQ_WQE_SIZE_64);
  12463. break;
  12464. case 128:
  12465. if (!(phba->sli4_hba.pc_sli4_params.wqsize &
  12466. LPFC_WQ_SZ128_SUPPORT)) {
  12467. status = -ERANGE;
  12468. goto out;
  12469. }
  12470. bf_set(lpfc_mbx_wq_create_wqe_size,
  12471. &wq_create->u.request_1,
  12472. LPFC_WQ_WQE_SIZE_128);
  12473. break;
  12474. }
  12475. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  12476. (PAGE_SIZE/SLI4_PAGE_SIZE));
  12477. page = wq_create->u.request_1.page;
  12478. break;
  12479. default:
  12480. status = -ERANGE;
  12481. goto out;
  12482. }
  12483. list_for_each_entry(dmabuf, &wq->page_list, list) {
  12484. memset(dmabuf->virt, 0, hw_page_size);
  12485. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  12486. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  12487. }
  12488. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  12489. bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
  12490. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12491. /* The IOCTL status is embedded in the mailbox subheader. */
  12492. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12493. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12494. if (shdr_status || shdr_add_status || rc) {
  12495. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12496. "2503 WQ_CREATE mailbox failed with "
  12497. "status x%x add_status x%x, mbx status x%x\n",
  12498. shdr_status, shdr_add_status, rc);
  12499. status = -ENXIO;
  12500. goto out;
  12501. }
  12502. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  12503. if (wq->queue_id == 0xFFFF) {
  12504. status = -ENXIO;
  12505. goto out;
  12506. }
  12507. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
  12508. wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
  12509. &wq_create->u.response);
  12510. if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
  12511. (wq->db_format != LPFC_DB_RING_FORMAT)) {
  12512. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12513. "3265 WQ[%d] doorbell format not "
  12514. "supported: x%x\n", wq->queue_id,
  12515. wq->db_format);
  12516. status = -EINVAL;
  12517. goto out;
  12518. }
  12519. pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
  12520. &wq_create->u.response);
  12521. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
  12522. if (!bar_memmap_p) {
  12523. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12524. "3263 WQ[%d] failed to memmap pci "
  12525. "barset:x%x\n", wq->queue_id,
  12526. pci_barset);
  12527. status = -ENOMEM;
  12528. goto out;
  12529. }
  12530. db_offset = wq_create->u.response.doorbell_offset;
  12531. if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
  12532. (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
  12533. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12534. "3252 WQ[%d] doorbell offset not "
  12535. "supported: x%x\n", wq->queue_id,
  12536. db_offset);
  12537. status = -EINVAL;
  12538. goto out;
  12539. }
  12540. wq->db_regaddr = bar_memmap_p + db_offset;
  12541. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12542. "3264 WQ[%d]: barset:x%x, offset:x%x, "
  12543. "format:x%x\n", wq->queue_id, pci_barset,
  12544. db_offset, wq->db_format);
  12545. } else {
  12546. wq->db_format = LPFC_DB_LIST_FORMAT;
  12547. wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
  12548. }
  12549. wq->type = LPFC_WQ;
  12550. wq->assoc_qid = cq->queue_id;
  12551. wq->subtype = subtype;
  12552. wq->host_index = 0;
  12553. wq->hba_index = 0;
  12554. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  12555. /* link the wq onto the parent cq child list */
  12556. list_add_tail(&wq->list, &cq->child_list);
  12557. out:
  12558. mempool_free(mbox, phba->mbox_mem_pool);
  12559. return status;
  12560. }
  12561. /**
  12562. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  12563. * @phba: HBA structure that indicates port to create a queue on.
  12564. * @rq: The queue structure to use for the receive queue.
  12565. * @qno: The associated HBQ number
  12566. *
  12567. *
  12568. * For SLI4 we need to adjust the RQ repost value based on
  12569. * the number of buffers that are initially posted to the RQ.
  12570. */
  12571. void
  12572. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  12573. {
  12574. uint32_t cnt;
  12575. /* sanity check on queue memory */
  12576. if (!rq)
  12577. return;
  12578. cnt = lpfc_hbq_defs[qno]->entry_count;
  12579. /* Recalc repost for RQs based on buffers initially posted */
  12580. cnt = (cnt >> 3);
  12581. if (cnt < LPFC_QUEUE_MIN_REPOST)
  12582. cnt = LPFC_QUEUE_MIN_REPOST;
  12583. rq->entry_repost = cnt;
  12584. }
  12585. /**
  12586. * lpfc_rq_create - Create a Receive Queue on the HBA
  12587. * @phba: HBA structure that indicates port to create a queue on.
  12588. * @hrq: The queue structure to use to create the header receive queue.
  12589. * @drq: The queue structure to use to create the data receive queue.
  12590. * @cq: The completion queue to bind this work queue to.
  12591. *
  12592. * This function creates a receive buffer queue pair , as detailed in @hrq and
  12593. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  12594. * to the HBA.
  12595. *
  12596. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  12597. * struct is used to get the entry count that is necessary to determine the
  12598. * number of pages to use for this queue. The @cq is used to indicate which
  12599. * completion queue to bind received buffers that are posted to these queues to.
  12600. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  12601. * receive queue pair. This function is asynchronous and will wait for the
  12602. * mailbox command to finish before continuing.
  12603. *
  12604. * On success this function will return a zero. If unable to allocate enough
  12605. * memory this function will return -ENOMEM. If the queue create mailbox command
  12606. * fails this function will return -ENXIO.
  12607. **/
  12608. int
  12609. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  12610. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  12611. {
  12612. struct lpfc_mbx_rq_create *rq_create;
  12613. struct lpfc_dmabuf *dmabuf;
  12614. LPFC_MBOXQ_t *mbox;
  12615. int rc, length, status = 0;
  12616. uint32_t shdr_status, shdr_add_status;
  12617. union lpfc_sli4_cfg_shdr *shdr;
  12618. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  12619. void __iomem *bar_memmap_p;
  12620. uint32_t db_offset;
  12621. uint16_t pci_barset;
  12622. /* sanity check on queue memory */
  12623. if (!hrq || !drq || !cq)
  12624. return -ENODEV;
  12625. if (!phba->sli4_hba.pc_sli4_params.supported)
  12626. hw_page_size = SLI4_PAGE_SIZE;
  12627. if (hrq->entry_count != drq->entry_count)
  12628. return -EINVAL;
  12629. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12630. if (!mbox)
  12631. return -ENOMEM;
  12632. length = (sizeof(struct lpfc_mbx_rq_create) -
  12633. sizeof(struct lpfc_sli4_cfg_mhdr));
  12634. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12635. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  12636. length, LPFC_SLI4_MBX_EMBED);
  12637. rq_create = &mbox->u.mqe.un.rq_create;
  12638. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  12639. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12640. phba->sli4_hba.pc_sli4_params.rqv);
  12641. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  12642. bf_set(lpfc_rq_context_rqe_count_1,
  12643. &rq_create->u.request.context,
  12644. hrq->entry_count);
  12645. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  12646. bf_set(lpfc_rq_context_rqe_size,
  12647. &rq_create->u.request.context,
  12648. LPFC_RQE_SIZE_8);
  12649. bf_set(lpfc_rq_context_page_size,
  12650. &rq_create->u.request.context,
  12651. (PAGE_SIZE/SLI4_PAGE_SIZE));
  12652. } else {
  12653. switch (hrq->entry_count) {
  12654. default:
  12655. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12656. "2535 Unsupported RQ count. (%d)\n",
  12657. hrq->entry_count);
  12658. if (hrq->entry_count < 512) {
  12659. status = -EINVAL;
  12660. goto out;
  12661. }
  12662. /* otherwise default to smallest count (drop through) */
  12663. case 512:
  12664. bf_set(lpfc_rq_context_rqe_count,
  12665. &rq_create->u.request.context,
  12666. LPFC_RQ_RING_SIZE_512);
  12667. break;
  12668. case 1024:
  12669. bf_set(lpfc_rq_context_rqe_count,
  12670. &rq_create->u.request.context,
  12671. LPFC_RQ_RING_SIZE_1024);
  12672. break;
  12673. case 2048:
  12674. bf_set(lpfc_rq_context_rqe_count,
  12675. &rq_create->u.request.context,
  12676. LPFC_RQ_RING_SIZE_2048);
  12677. break;
  12678. case 4096:
  12679. bf_set(lpfc_rq_context_rqe_count,
  12680. &rq_create->u.request.context,
  12681. LPFC_RQ_RING_SIZE_4096);
  12682. break;
  12683. }
  12684. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  12685. LPFC_HDR_BUF_SIZE);
  12686. }
  12687. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  12688. cq->queue_id);
  12689. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  12690. hrq->page_count);
  12691. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  12692. memset(dmabuf->virt, 0, hw_page_size);
  12693. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12694. putPaddrLow(dmabuf->phys);
  12695. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12696. putPaddrHigh(dmabuf->phys);
  12697. }
  12698. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  12699. bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
  12700. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12701. /* The IOCTL status is embedded in the mailbox subheader. */
  12702. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12703. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12704. if (shdr_status || shdr_add_status || rc) {
  12705. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12706. "2504 RQ_CREATE mailbox failed with "
  12707. "status x%x add_status x%x, mbx status x%x\n",
  12708. shdr_status, shdr_add_status, rc);
  12709. status = -ENXIO;
  12710. goto out;
  12711. }
  12712. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  12713. if (hrq->queue_id == 0xFFFF) {
  12714. status = -ENXIO;
  12715. goto out;
  12716. }
  12717. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
  12718. hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
  12719. &rq_create->u.response);
  12720. if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
  12721. (hrq->db_format != LPFC_DB_RING_FORMAT)) {
  12722. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12723. "3262 RQ [%d] doorbell format not "
  12724. "supported: x%x\n", hrq->queue_id,
  12725. hrq->db_format);
  12726. status = -EINVAL;
  12727. goto out;
  12728. }
  12729. pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
  12730. &rq_create->u.response);
  12731. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
  12732. if (!bar_memmap_p) {
  12733. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12734. "3269 RQ[%d] failed to memmap pci "
  12735. "barset:x%x\n", hrq->queue_id,
  12736. pci_barset);
  12737. status = -ENOMEM;
  12738. goto out;
  12739. }
  12740. db_offset = rq_create->u.response.doorbell_offset;
  12741. if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
  12742. (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
  12743. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12744. "3270 RQ[%d] doorbell offset not "
  12745. "supported: x%x\n", hrq->queue_id,
  12746. db_offset);
  12747. status = -EINVAL;
  12748. goto out;
  12749. }
  12750. hrq->db_regaddr = bar_memmap_p + db_offset;
  12751. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12752. "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
  12753. "format:x%x\n", hrq->queue_id, pci_barset,
  12754. db_offset, hrq->db_format);
  12755. } else {
  12756. hrq->db_format = LPFC_DB_RING_FORMAT;
  12757. hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
  12758. }
  12759. hrq->type = LPFC_HRQ;
  12760. hrq->assoc_qid = cq->queue_id;
  12761. hrq->subtype = subtype;
  12762. hrq->host_index = 0;
  12763. hrq->hba_index = 0;
  12764. /* now create the data queue */
  12765. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12766. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  12767. length, LPFC_SLI4_MBX_EMBED);
  12768. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12769. phba->sli4_hba.pc_sli4_params.rqv);
  12770. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  12771. bf_set(lpfc_rq_context_rqe_count_1,
  12772. &rq_create->u.request.context, hrq->entry_count);
  12773. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  12774. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  12775. LPFC_RQE_SIZE_8);
  12776. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  12777. (PAGE_SIZE/SLI4_PAGE_SIZE));
  12778. } else {
  12779. switch (drq->entry_count) {
  12780. default:
  12781. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12782. "2536 Unsupported RQ count. (%d)\n",
  12783. drq->entry_count);
  12784. if (drq->entry_count < 512) {
  12785. status = -EINVAL;
  12786. goto out;
  12787. }
  12788. /* otherwise default to smallest count (drop through) */
  12789. case 512:
  12790. bf_set(lpfc_rq_context_rqe_count,
  12791. &rq_create->u.request.context,
  12792. LPFC_RQ_RING_SIZE_512);
  12793. break;
  12794. case 1024:
  12795. bf_set(lpfc_rq_context_rqe_count,
  12796. &rq_create->u.request.context,
  12797. LPFC_RQ_RING_SIZE_1024);
  12798. break;
  12799. case 2048:
  12800. bf_set(lpfc_rq_context_rqe_count,
  12801. &rq_create->u.request.context,
  12802. LPFC_RQ_RING_SIZE_2048);
  12803. break;
  12804. case 4096:
  12805. bf_set(lpfc_rq_context_rqe_count,
  12806. &rq_create->u.request.context,
  12807. LPFC_RQ_RING_SIZE_4096);
  12808. break;
  12809. }
  12810. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  12811. LPFC_DATA_BUF_SIZE);
  12812. }
  12813. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  12814. cq->queue_id);
  12815. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  12816. drq->page_count);
  12817. list_for_each_entry(dmabuf, &drq->page_list, list) {
  12818. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12819. putPaddrLow(dmabuf->phys);
  12820. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12821. putPaddrHigh(dmabuf->phys);
  12822. }
  12823. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  12824. bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
  12825. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12826. /* The IOCTL status is embedded in the mailbox subheader. */
  12827. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  12828. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12829. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12830. if (shdr_status || shdr_add_status || rc) {
  12831. status = -ENXIO;
  12832. goto out;
  12833. }
  12834. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  12835. if (drq->queue_id == 0xFFFF) {
  12836. status = -ENXIO;
  12837. goto out;
  12838. }
  12839. drq->type = LPFC_DRQ;
  12840. drq->assoc_qid = cq->queue_id;
  12841. drq->subtype = subtype;
  12842. drq->host_index = 0;
  12843. drq->hba_index = 0;
  12844. /* link the header and data RQs onto the parent cq child list */
  12845. list_add_tail(&hrq->list, &cq->child_list);
  12846. list_add_tail(&drq->list, &cq->child_list);
  12847. out:
  12848. mempool_free(mbox, phba->mbox_mem_pool);
  12849. return status;
  12850. }
  12851. /**
  12852. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  12853. * @eq: The queue structure associated with the queue to destroy.
  12854. *
  12855. * This function destroys a queue, as detailed in @eq by sending an mailbox
  12856. * command, specific to the type of queue, to the HBA.
  12857. *
  12858. * The @eq struct is used to get the queue ID of the queue to destroy.
  12859. *
  12860. * On success this function will return a zero. If the queue destroy mailbox
  12861. * command fails this function will return -ENXIO.
  12862. **/
  12863. int
  12864. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  12865. {
  12866. LPFC_MBOXQ_t *mbox;
  12867. int rc, length, status = 0;
  12868. uint32_t shdr_status, shdr_add_status;
  12869. union lpfc_sli4_cfg_shdr *shdr;
  12870. /* sanity check on queue memory */
  12871. if (!eq)
  12872. return -ENODEV;
  12873. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  12874. if (!mbox)
  12875. return -ENOMEM;
  12876. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  12877. sizeof(struct lpfc_sli4_cfg_mhdr));
  12878. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12879. LPFC_MBOX_OPCODE_EQ_DESTROY,
  12880. length, LPFC_SLI4_MBX_EMBED);
  12881. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  12882. eq->queue_id);
  12883. mbox->vport = eq->phba->pport;
  12884. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12885. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  12886. /* The IOCTL status is embedded in the mailbox subheader. */
  12887. shdr = (union lpfc_sli4_cfg_shdr *)
  12888. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  12889. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12890. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12891. if (shdr_status || shdr_add_status || rc) {
  12892. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12893. "2505 EQ_DESTROY mailbox failed with "
  12894. "status x%x add_status x%x, mbx status x%x\n",
  12895. shdr_status, shdr_add_status, rc);
  12896. status = -ENXIO;
  12897. }
  12898. /* Remove eq from any list */
  12899. list_del_init(&eq->list);
  12900. mempool_free(mbox, eq->phba->mbox_mem_pool);
  12901. return status;
  12902. }
  12903. /**
  12904. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  12905. * @cq: The queue structure associated with the queue to destroy.
  12906. *
  12907. * This function destroys a queue, as detailed in @cq by sending an mailbox
  12908. * command, specific to the type of queue, to the HBA.
  12909. *
  12910. * The @cq struct is used to get the queue ID of the queue to destroy.
  12911. *
  12912. * On success this function will return a zero. If the queue destroy mailbox
  12913. * command fails this function will return -ENXIO.
  12914. **/
  12915. int
  12916. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  12917. {
  12918. LPFC_MBOXQ_t *mbox;
  12919. int rc, length, status = 0;
  12920. uint32_t shdr_status, shdr_add_status;
  12921. union lpfc_sli4_cfg_shdr *shdr;
  12922. /* sanity check on queue memory */
  12923. if (!cq)
  12924. return -ENODEV;
  12925. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  12926. if (!mbox)
  12927. return -ENOMEM;
  12928. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  12929. sizeof(struct lpfc_sli4_cfg_mhdr));
  12930. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12931. LPFC_MBOX_OPCODE_CQ_DESTROY,
  12932. length, LPFC_SLI4_MBX_EMBED);
  12933. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  12934. cq->queue_id);
  12935. mbox->vport = cq->phba->pport;
  12936. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12937. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  12938. /* The IOCTL status is embedded in the mailbox subheader. */
  12939. shdr = (union lpfc_sli4_cfg_shdr *)
  12940. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  12941. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12942. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12943. if (shdr_status || shdr_add_status || rc) {
  12944. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12945. "2506 CQ_DESTROY mailbox failed with "
  12946. "status x%x add_status x%x, mbx status x%x\n",
  12947. shdr_status, shdr_add_status, rc);
  12948. status = -ENXIO;
  12949. }
  12950. /* Remove cq from any list */
  12951. list_del_init(&cq->list);
  12952. mempool_free(mbox, cq->phba->mbox_mem_pool);
  12953. return status;
  12954. }
  12955. /**
  12956. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  12957. * @qm: The queue structure associated with the queue to destroy.
  12958. *
  12959. * This function destroys a queue, as detailed in @mq by sending an mailbox
  12960. * command, specific to the type of queue, to the HBA.
  12961. *
  12962. * The @mq struct is used to get the queue ID of the queue to destroy.
  12963. *
  12964. * On success this function will return a zero. If the queue destroy mailbox
  12965. * command fails this function will return -ENXIO.
  12966. **/
  12967. int
  12968. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  12969. {
  12970. LPFC_MBOXQ_t *mbox;
  12971. int rc, length, status = 0;
  12972. uint32_t shdr_status, shdr_add_status;
  12973. union lpfc_sli4_cfg_shdr *shdr;
  12974. /* sanity check on queue memory */
  12975. if (!mq)
  12976. return -ENODEV;
  12977. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  12978. if (!mbox)
  12979. return -ENOMEM;
  12980. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  12981. sizeof(struct lpfc_sli4_cfg_mhdr));
  12982. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12983. LPFC_MBOX_OPCODE_MQ_DESTROY,
  12984. length, LPFC_SLI4_MBX_EMBED);
  12985. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  12986. mq->queue_id);
  12987. mbox->vport = mq->phba->pport;
  12988. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12989. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  12990. /* The IOCTL status is embedded in the mailbox subheader. */
  12991. shdr = (union lpfc_sli4_cfg_shdr *)
  12992. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  12993. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12994. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12995. if (shdr_status || shdr_add_status || rc) {
  12996. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12997. "2507 MQ_DESTROY mailbox failed with "
  12998. "status x%x add_status x%x, mbx status x%x\n",
  12999. shdr_status, shdr_add_status, rc);
  13000. status = -ENXIO;
  13001. }
  13002. /* Remove mq from any list */
  13003. list_del_init(&mq->list);
  13004. mempool_free(mbox, mq->phba->mbox_mem_pool);
  13005. return status;
  13006. }
  13007. /**
  13008. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  13009. * @wq: The queue structure associated with the queue to destroy.
  13010. *
  13011. * This function destroys a queue, as detailed in @wq by sending an mailbox
  13012. * command, specific to the type of queue, to the HBA.
  13013. *
  13014. * The @wq struct is used to get the queue ID of the queue to destroy.
  13015. *
  13016. * On success this function will return a zero. If the queue destroy mailbox
  13017. * command fails this function will return -ENXIO.
  13018. **/
  13019. int
  13020. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  13021. {
  13022. LPFC_MBOXQ_t *mbox;
  13023. int rc, length, status = 0;
  13024. uint32_t shdr_status, shdr_add_status;
  13025. union lpfc_sli4_cfg_shdr *shdr;
  13026. /* sanity check on queue memory */
  13027. if (!wq)
  13028. return -ENODEV;
  13029. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  13030. if (!mbox)
  13031. return -ENOMEM;
  13032. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  13033. sizeof(struct lpfc_sli4_cfg_mhdr));
  13034. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  13035. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  13036. length, LPFC_SLI4_MBX_EMBED);
  13037. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  13038. wq->queue_id);
  13039. mbox->vport = wq->phba->pport;
  13040. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13041. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  13042. shdr = (union lpfc_sli4_cfg_shdr *)
  13043. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  13044. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13045. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13046. if (shdr_status || shdr_add_status || rc) {
  13047. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13048. "2508 WQ_DESTROY mailbox failed with "
  13049. "status x%x add_status x%x, mbx status x%x\n",
  13050. shdr_status, shdr_add_status, rc);
  13051. status = -ENXIO;
  13052. }
  13053. /* Remove wq from any list */
  13054. list_del_init(&wq->list);
  13055. mempool_free(mbox, wq->phba->mbox_mem_pool);
  13056. return status;
  13057. }
  13058. /**
  13059. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  13060. * @rq: The queue structure associated with the queue to destroy.
  13061. *
  13062. * This function destroys a queue, as detailed in @rq by sending an mailbox
  13063. * command, specific to the type of queue, to the HBA.
  13064. *
  13065. * The @rq struct is used to get the queue ID of the queue to destroy.
  13066. *
  13067. * On success this function will return a zero. If the queue destroy mailbox
  13068. * command fails this function will return -ENXIO.
  13069. **/
  13070. int
  13071. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  13072. struct lpfc_queue *drq)
  13073. {
  13074. LPFC_MBOXQ_t *mbox;
  13075. int rc, length, status = 0;
  13076. uint32_t shdr_status, shdr_add_status;
  13077. union lpfc_sli4_cfg_shdr *shdr;
  13078. /* sanity check on queue memory */
  13079. if (!hrq || !drq)
  13080. return -ENODEV;
  13081. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  13082. if (!mbox)
  13083. return -ENOMEM;
  13084. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  13085. sizeof(struct lpfc_sli4_cfg_mhdr));
  13086. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  13087. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  13088. length, LPFC_SLI4_MBX_EMBED);
  13089. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  13090. hrq->queue_id);
  13091. mbox->vport = hrq->phba->pport;
  13092. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13093. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  13094. /* The IOCTL status is embedded in the mailbox subheader. */
  13095. shdr = (union lpfc_sli4_cfg_shdr *)
  13096. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  13097. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13098. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13099. if (shdr_status || shdr_add_status || rc) {
  13100. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13101. "2509 RQ_DESTROY mailbox failed with "
  13102. "status x%x add_status x%x, mbx status x%x\n",
  13103. shdr_status, shdr_add_status, rc);
  13104. if (rc != MBX_TIMEOUT)
  13105. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  13106. return -ENXIO;
  13107. }
  13108. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  13109. drq->queue_id);
  13110. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  13111. shdr = (union lpfc_sli4_cfg_shdr *)
  13112. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  13113. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13114. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13115. if (shdr_status || shdr_add_status || rc) {
  13116. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13117. "2510 RQ_DESTROY mailbox failed with "
  13118. "status x%x add_status x%x, mbx status x%x\n",
  13119. shdr_status, shdr_add_status, rc);
  13120. status = -ENXIO;
  13121. }
  13122. list_del_init(&hrq->list);
  13123. list_del_init(&drq->list);
  13124. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  13125. return status;
  13126. }
  13127. /**
  13128. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  13129. * @phba: The virtual port for which this call being executed.
  13130. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  13131. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  13132. * @xritag: the xritag that ties this io to the SGL pages.
  13133. *
  13134. * This routine will post the sgl pages for the IO that has the xritag
  13135. * that is in the iocbq structure. The xritag is assigned during iocbq
  13136. * creation and persists for as long as the driver is loaded.
  13137. * if the caller has fewer than 256 scatter gather segments to map then
  13138. * pdma_phys_addr1 should be 0.
  13139. * If the caller needs to map more than 256 scatter gather segment then
  13140. * pdma_phys_addr1 should be a valid physical address.
  13141. * physical address for SGLs must be 64 byte aligned.
  13142. * If you are going to map 2 SGL's then the first one must have 256 entries
  13143. * the second sgl can have between 1 and 256 entries.
  13144. *
  13145. * Return codes:
  13146. * 0 - Success
  13147. * -ENXIO, -ENOMEM - Failure
  13148. **/
  13149. int
  13150. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  13151. dma_addr_t pdma_phys_addr0,
  13152. dma_addr_t pdma_phys_addr1,
  13153. uint16_t xritag)
  13154. {
  13155. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  13156. LPFC_MBOXQ_t *mbox;
  13157. int rc;
  13158. uint32_t shdr_status, shdr_add_status;
  13159. uint32_t mbox_tmo;
  13160. union lpfc_sli4_cfg_shdr *shdr;
  13161. if (xritag == NO_XRI) {
  13162. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13163. "0364 Invalid param:\n");
  13164. return -EINVAL;
  13165. }
  13166. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13167. if (!mbox)
  13168. return -ENOMEM;
  13169. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  13170. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  13171. sizeof(struct lpfc_mbx_post_sgl_pages) -
  13172. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  13173. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  13174. &mbox->u.mqe.un.post_sgl_pages;
  13175. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  13176. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  13177. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  13178. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  13179. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  13180. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  13181. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  13182. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  13183. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  13184. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  13185. if (!phba->sli4_hba.intr_enable)
  13186. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13187. else {
  13188. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  13189. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  13190. }
  13191. /* The IOCTL status is embedded in the mailbox subheader. */
  13192. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  13193. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13194. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13195. if (rc != MBX_TIMEOUT)
  13196. mempool_free(mbox, phba->mbox_mem_pool);
  13197. if (shdr_status || shdr_add_status || rc) {
  13198. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13199. "2511 POST_SGL mailbox failed with "
  13200. "status x%x add_status x%x, mbx status x%x\n",
  13201. shdr_status, shdr_add_status, rc);
  13202. }
  13203. return 0;
  13204. }
  13205. /**
  13206. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  13207. * @phba: pointer to lpfc hba data structure.
  13208. *
  13209. * This routine is invoked to post rpi header templates to the
  13210. * HBA consistent with the SLI-4 interface spec. This routine
  13211. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13212. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13213. *
  13214. * Returns
  13215. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13216. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13217. **/
  13218. static uint16_t
  13219. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  13220. {
  13221. unsigned long xri;
  13222. /*
  13223. * Fetch the next logical xri. Because this index is logical,
  13224. * the driver starts at 0 each time.
  13225. */
  13226. spin_lock_irq(&phba->hbalock);
  13227. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  13228. phba->sli4_hba.max_cfg_param.max_xri, 0);
  13229. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  13230. spin_unlock_irq(&phba->hbalock);
  13231. return NO_XRI;
  13232. } else {
  13233. set_bit(xri, phba->sli4_hba.xri_bmask);
  13234. phba->sli4_hba.max_cfg_param.xri_used++;
  13235. }
  13236. spin_unlock_irq(&phba->hbalock);
  13237. return xri;
  13238. }
  13239. /**
  13240. * lpfc_sli4_free_xri - Release an xri for reuse.
  13241. * @phba: pointer to lpfc hba data structure.
  13242. *
  13243. * This routine is invoked to release an xri to the pool of
  13244. * available rpis maintained by the driver.
  13245. **/
  13246. static void
  13247. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  13248. {
  13249. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  13250. phba->sli4_hba.max_cfg_param.xri_used--;
  13251. }
  13252. }
  13253. /**
  13254. * lpfc_sli4_free_xri - Release an xri for reuse.
  13255. * @phba: pointer to lpfc hba data structure.
  13256. *
  13257. * This routine is invoked to release an xri to the pool of
  13258. * available rpis maintained by the driver.
  13259. **/
  13260. void
  13261. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  13262. {
  13263. spin_lock_irq(&phba->hbalock);
  13264. __lpfc_sli4_free_xri(phba, xri);
  13265. spin_unlock_irq(&phba->hbalock);
  13266. }
  13267. /**
  13268. * lpfc_sli4_next_xritag - Get an xritag for the io
  13269. * @phba: Pointer to HBA context object.
  13270. *
  13271. * This function gets an xritag for the iocb. If there is no unused xritag
  13272. * it will return 0xffff.
  13273. * The function returns the allocated xritag if successful, else returns zero.
  13274. * Zero is not a valid xritag.
  13275. * The caller is not required to hold any lock.
  13276. **/
  13277. uint16_t
  13278. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  13279. {
  13280. uint16_t xri_index;
  13281. xri_index = lpfc_sli4_alloc_xri(phba);
  13282. if (xri_index == NO_XRI)
  13283. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  13284. "2004 Failed to allocate XRI.last XRITAG is %d"
  13285. " Max XRI is %d, Used XRI is %d\n",
  13286. xri_index,
  13287. phba->sli4_hba.max_cfg_param.max_xri,
  13288. phba->sli4_hba.max_cfg_param.xri_used);
  13289. return xri_index;
  13290. }
  13291. /**
  13292. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  13293. * @phba: pointer to lpfc hba data structure.
  13294. * @post_sgl_list: pointer to els sgl entry list.
  13295. * @count: number of els sgl entries on the list.
  13296. *
  13297. * This routine is invoked to post a block of driver's sgl pages to the
  13298. * HBA using non-embedded mailbox command. No Lock is held. This routine
  13299. * is only called when the driver is loading and after all IO has been
  13300. * stopped.
  13301. **/
  13302. static int
  13303. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
  13304. struct list_head *post_sgl_list,
  13305. int post_cnt)
  13306. {
  13307. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  13308. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  13309. struct sgl_page_pairs *sgl_pg_pairs;
  13310. void *viraddr;
  13311. LPFC_MBOXQ_t *mbox;
  13312. uint32_t reqlen, alloclen, pg_pairs;
  13313. uint32_t mbox_tmo;
  13314. uint16_t xritag_start = 0;
  13315. int rc = 0;
  13316. uint32_t shdr_status, shdr_add_status;
  13317. union lpfc_sli4_cfg_shdr *shdr;
  13318. reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
  13319. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  13320. if (reqlen > SLI4_PAGE_SIZE) {
  13321. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  13322. "2559 Block sgl registration required DMA "
  13323. "size (%d) great than a page\n", reqlen);
  13324. return -ENOMEM;
  13325. }
  13326. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13327. if (!mbox)
  13328. return -ENOMEM;
  13329. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13330. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  13331. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  13332. LPFC_SLI4_MBX_NEMBED);
  13333. if (alloclen < reqlen) {
  13334. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13335. "0285 Allocated DMA memory size (%d) is "
  13336. "less than the requested DMA memory "
  13337. "size (%d)\n", alloclen, reqlen);
  13338. lpfc_sli4_mbox_cmd_free(phba, mbox);
  13339. return -ENOMEM;
  13340. }
  13341. /* Set up the SGL pages in the non-embedded DMA pages */
  13342. viraddr = mbox->sge_array->addr[0];
  13343. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  13344. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  13345. pg_pairs = 0;
  13346. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  13347. /* Set up the sge entry */
  13348. sgl_pg_pairs->sgl_pg0_addr_lo =
  13349. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  13350. sgl_pg_pairs->sgl_pg0_addr_hi =
  13351. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  13352. sgl_pg_pairs->sgl_pg1_addr_lo =
  13353. cpu_to_le32(putPaddrLow(0));
  13354. sgl_pg_pairs->sgl_pg1_addr_hi =
  13355. cpu_to_le32(putPaddrHigh(0));
  13356. /* Keep the first xritag on the list */
  13357. if (pg_pairs == 0)
  13358. xritag_start = sglq_entry->sli4_xritag;
  13359. sgl_pg_pairs++;
  13360. pg_pairs++;
  13361. }
  13362. /* Complete initialization and perform endian conversion. */
  13363. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  13364. bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
  13365. sgl->word0 = cpu_to_le32(sgl->word0);
  13366. if (!phba->sli4_hba.intr_enable)
  13367. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13368. else {
  13369. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  13370. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  13371. }
  13372. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  13373. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13374. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13375. if (rc != MBX_TIMEOUT)
  13376. lpfc_sli4_mbox_cmd_free(phba, mbox);
  13377. if (shdr_status || shdr_add_status || rc) {
  13378. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13379. "2513 POST_SGL_BLOCK mailbox command failed "
  13380. "status x%x add_status x%x mbx status x%x\n",
  13381. shdr_status, shdr_add_status, rc);
  13382. rc = -ENXIO;
  13383. }
  13384. return rc;
  13385. }
  13386. /**
  13387. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  13388. * @phba: pointer to lpfc hba data structure.
  13389. * @sblist: pointer to scsi buffer list.
  13390. * @count: number of scsi buffers on the list.
  13391. *
  13392. * This routine is invoked to post a block of @count scsi sgl pages from a
  13393. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  13394. * No Lock is held.
  13395. *
  13396. **/
  13397. int
  13398. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  13399. struct list_head *sblist,
  13400. int count)
  13401. {
  13402. struct lpfc_scsi_buf *psb;
  13403. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  13404. struct sgl_page_pairs *sgl_pg_pairs;
  13405. void *viraddr;
  13406. LPFC_MBOXQ_t *mbox;
  13407. uint32_t reqlen, alloclen, pg_pairs;
  13408. uint32_t mbox_tmo;
  13409. uint16_t xritag_start = 0;
  13410. int rc = 0;
  13411. uint32_t shdr_status, shdr_add_status;
  13412. dma_addr_t pdma_phys_bpl1;
  13413. union lpfc_sli4_cfg_shdr *shdr;
  13414. /* Calculate the requested length of the dma memory */
  13415. reqlen = count * sizeof(struct sgl_page_pairs) +
  13416. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  13417. if (reqlen > SLI4_PAGE_SIZE) {
  13418. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  13419. "0217 Block sgl registration required DMA "
  13420. "size (%d) great than a page\n", reqlen);
  13421. return -ENOMEM;
  13422. }
  13423. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13424. if (!mbox) {
  13425. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13426. "0283 Failed to allocate mbox cmd memory\n");
  13427. return -ENOMEM;
  13428. }
  13429. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13430. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  13431. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  13432. LPFC_SLI4_MBX_NEMBED);
  13433. if (alloclen < reqlen) {
  13434. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13435. "2561 Allocated DMA memory size (%d) is "
  13436. "less than the requested DMA memory "
  13437. "size (%d)\n", alloclen, reqlen);
  13438. lpfc_sli4_mbox_cmd_free(phba, mbox);
  13439. return -ENOMEM;
  13440. }
  13441. /* Get the first SGE entry from the non-embedded DMA memory */
  13442. viraddr = mbox->sge_array->addr[0];
  13443. /* Set up the SGL pages in the non-embedded DMA pages */
  13444. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  13445. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  13446. pg_pairs = 0;
  13447. list_for_each_entry(psb, sblist, list) {
  13448. /* Set up the sge entry */
  13449. sgl_pg_pairs->sgl_pg0_addr_lo =
  13450. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  13451. sgl_pg_pairs->sgl_pg0_addr_hi =
  13452. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  13453. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  13454. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  13455. else
  13456. pdma_phys_bpl1 = 0;
  13457. sgl_pg_pairs->sgl_pg1_addr_lo =
  13458. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  13459. sgl_pg_pairs->sgl_pg1_addr_hi =
  13460. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  13461. /* Keep the first xritag on the list */
  13462. if (pg_pairs == 0)
  13463. xritag_start = psb->cur_iocbq.sli4_xritag;
  13464. sgl_pg_pairs++;
  13465. pg_pairs++;
  13466. }
  13467. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  13468. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  13469. /* Perform endian conversion if necessary */
  13470. sgl->word0 = cpu_to_le32(sgl->word0);
  13471. if (!phba->sli4_hba.intr_enable)
  13472. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13473. else {
  13474. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  13475. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  13476. }
  13477. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  13478. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13479. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13480. if (rc != MBX_TIMEOUT)
  13481. lpfc_sli4_mbox_cmd_free(phba, mbox);
  13482. if (shdr_status || shdr_add_status || rc) {
  13483. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13484. "2564 POST_SGL_BLOCK mailbox command failed "
  13485. "status x%x add_status x%x mbx status x%x\n",
  13486. shdr_status, shdr_add_status, rc);
  13487. rc = -ENXIO;
  13488. }
  13489. return rc;
  13490. }
  13491. /**
  13492. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  13493. * @phba: pointer to lpfc_hba struct that the frame was received on
  13494. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  13495. *
  13496. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  13497. * valid type of frame that the LPFC driver will handle. This function will
  13498. * return a zero if the frame is a valid frame or a non zero value when the
  13499. * frame does not pass the check.
  13500. **/
  13501. static int
  13502. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  13503. {
  13504. /* make rctl_names static to save stack space */
  13505. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  13506. char *type_names[] = FC_TYPE_NAMES_INIT;
  13507. struct fc_vft_header *fc_vft_hdr;
  13508. uint32_t *header = (uint32_t *) fc_hdr;
  13509. switch (fc_hdr->fh_r_ctl) {
  13510. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  13511. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  13512. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  13513. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  13514. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  13515. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  13516. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  13517. case FC_RCTL_DD_CMD_STATUS: /* command status */
  13518. case FC_RCTL_ELS_REQ: /* extended link services request */
  13519. case FC_RCTL_ELS_REP: /* extended link services reply */
  13520. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  13521. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  13522. case FC_RCTL_BA_NOP: /* basic link service NOP */
  13523. case FC_RCTL_BA_ABTS: /* basic link service abort */
  13524. case FC_RCTL_BA_RMC: /* remove connection */
  13525. case FC_RCTL_BA_ACC: /* basic accept */
  13526. case FC_RCTL_BA_RJT: /* basic reject */
  13527. case FC_RCTL_BA_PRMT:
  13528. case FC_RCTL_ACK_1: /* acknowledge_1 */
  13529. case FC_RCTL_ACK_0: /* acknowledge_0 */
  13530. case FC_RCTL_P_RJT: /* port reject */
  13531. case FC_RCTL_F_RJT: /* fabric reject */
  13532. case FC_RCTL_P_BSY: /* port busy */
  13533. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  13534. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  13535. case FC_RCTL_LCR: /* link credit reset */
  13536. case FC_RCTL_END: /* end */
  13537. break;
  13538. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  13539. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  13540. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  13541. return lpfc_fc_frame_check(phba, fc_hdr);
  13542. default:
  13543. goto drop;
  13544. }
  13545. switch (fc_hdr->fh_type) {
  13546. case FC_TYPE_BLS:
  13547. case FC_TYPE_ELS:
  13548. case FC_TYPE_FCP:
  13549. case FC_TYPE_CT:
  13550. break;
  13551. case FC_TYPE_IP:
  13552. case FC_TYPE_ILS:
  13553. default:
  13554. goto drop;
  13555. }
  13556. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  13557. "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
  13558. "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
  13559. rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
  13560. type_names[fc_hdr->fh_type], fc_hdr->fh_type,
  13561. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  13562. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  13563. be32_to_cpu(header[4]), be32_to_cpu(header[5]),
  13564. be32_to_cpu(header[6]));
  13565. return 0;
  13566. drop:
  13567. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  13568. "2539 Dropped frame rctl:%s type:%s\n",
  13569. rctl_names[fc_hdr->fh_r_ctl],
  13570. type_names[fc_hdr->fh_type]);
  13571. return 1;
  13572. }
  13573. /**
  13574. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  13575. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  13576. *
  13577. * This function processes the FC header to retrieve the VFI from the VF
  13578. * header, if one exists. This function will return the VFI if one exists
  13579. * or 0 if no VSAN Header exists.
  13580. **/
  13581. static uint32_t
  13582. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  13583. {
  13584. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  13585. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  13586. return 0;
  13587. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  13588. }
  13589. /**
  13590. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  13591. * @phba: Pointer to the HBA structure to search for the vport on
  13592. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  13593. * @fcfi: The FC Fabric ID that the frame came from
  13594. *
  13595. * This function searches the @phba for a vport that matches the content of the
  13596. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  13597. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  13598. * returns the matching vport pointer or NULL if unable to match frame to a
  13599. * vport.
  13600. **/
  13601. static struct lpfc_vport *
  13602. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  13603. uint16_t fcfi)
  13604. {
  13605. struct lpfc_vport **vports;
  13606. struct lpfc_vport *vport = NULL;
  13607. int i;
  13608. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  13609. fc_hdr->fh_d_id[1] << 8 |
  13610. fc_hdr->fh_d_id[2]);
  13611. if (did == Fabric_DID)
  13612. return phba->pport;
  13613. if ((phba->pport->fc_flag & FC_PT2PT) &&
  13614. !(phba->link_state == LPFC_HBA_READY))
  13615. return phba->pport;
  13616. vports = lpfc_create_vport_work_array(phba);
  13617. if (vports != NULL)
  13618. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  13619. if (phba->fcf.fcfi == fcfi &&
  13620. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  13621. vports[i]->fc_myDID == did) {
  13622. vport = vports[i];
  13623. break;
  13624. }
  13625. }
  13626. lpfc_destroy_vport_work_array(phba, vports);
  13627. return vport;
  13628. }
  13629. /**
  13630. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  13631. * @vport: The vport to work on.
  13632. *
  13633. * This function updates the receive sequence time stamp for this vport. The
  13634. * receive sequence time stamp indicates the time that the last frame of the
  13635. * the sequence that has been idle for the longest amount of time was received.
  13636. * the driver uses this time stamp to indicate if any received sequences have
  13637. * timed out.
  13638. **/
  13639. static void
  13640. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  13641. {
  13642. struct lpfc_dmabuf *h_buf;
  13643. struct hbq_dmabuf *dmabuf = NULL;
  13644. /* get the oldest sequence on the rcv list */
  13645. h_buf = list_get_first(&vport->rcv_buffer_list,
  13646. struct lpfc_dmabuf, list);
  13647. if (!h_buf)
  13648. return;
  13649. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13650. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  13651. }
  13652. /**
  13653. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  13654. * @vport: The vport that the received sequences were sent to.
  13655. *
  13656. * This function cleans up all outstanding received sequences. This is called
  13657. * by the driver when a link event or user action invalidates all the received
  13658. * sequences.
  13659. **/
  13660. void
  13661. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  13662. {
  13663. struct lpfc_dmabuf *h_buf, *hnext;
  13664. struct lpfc_dmabuf *d_buf, *dnext;
  13665. struct hbq_dmabuf *dmabuf = NULL;
  13666. /* start with the oldest sequence on the rcv list */
  13667. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  13668. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13669. list_del_init(&dmabuf->hbuf.list);
  13670. list_for_each_entry_safe(d_buf, dnext,
  13671. &dmabuf->dbuf.list, list) {
  13672. list_del_init(&d_buf->list);
  13673. lpfc_in_buf_free(vport->phba, d_buf);
  13674. }
  13675. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  13676. }
  13677. }
  13678. /**
  13679. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  13680. * @vport: The vport that the received sequences were sent to.
  13681. *
  13682. * This function determines whether any received sequences have timed out by
  13683. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  13684. * indicates that there is at least one timed out sequence this routine will
  13685. * go through the received sequences one at a time from most inactive to most
  13686. * active to determine which ones need to be cleaned up. Once it has determined
  13687. * that a sequence needs to be cleaned up it will simply free up the resources
  13688. * without sending an abort.
  13689. **/
  13690. void
  13691. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  13692. {
  13693. struct lpfc_dmabuf *h_buf, *hnext;
  13694. struct lpfc_dmabuf *d_buf, *dnext;
  13695. struct hbq_dmabuf *dmabuf = NULL;
  13696. unsigned long timeout;
  13697. int abort_count = 0;
  13698. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  13699. vport->rcv_buffer_time_stamp);
  13700. if (list_empty(&vport->rcv_buffer_list) ||
  13701. time_before(jiffies, timeout))
  13702. return;
  13703. /* start with the oldest sequence on the rcv list */
  13704. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  13705. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13706. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  13707. dmabuf->time_stamp);
  13708. if (time_before(jiffies, timeout))
  13709. break;
  13710. abort_count++;
  13711. list_del_init(&dmabuf->hbuf.list);
  13712. list_for_each_entry_safe(d_buf, dnext,
  13713. &dmabuf->dbuf.list, list) {
  13714. list_del_init(&d_buf->list);
  13715. lpfc_in_buf_free(vport->phba, d_buf);
  13716. }
  13717. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  13718. }
  13719. if (abort_count)
  13720. lpfc_update_rcv_time_stamp(vport);
  13721. }
  13722. /**
  13723. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  13724. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  13725. *
  13726. * This function searches through the existing incomplete sequences that have
  13727. * been sent to this @vport. If the frame matches one of the incomplete
  13728. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  13729. * make up that sequence. If no sequence is found that matches this frame then
  13730. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  13731. * This function returns a pointer to the first dmabuf in the sequence list that
  13732. * the frame was linked to.
  13733. **/
  13734. static struct hbq_dmabuf *
  13735. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  13736. {
  13737. struct fc_frame_header *new_hdr;
  13738. struct fc_frame_header *temp_hdr;
  13739. struct lpfc_dmabuf *d_buf;
  13740. struct lpfc_dmabuf *h_buf;
  13741. struct hbq_dmabuf *seq_dmabuf = NULL;
  13742. struct hbq_dmabuf *temp_dmabuf = NULL;
  13743. uint8_t found = 0;
  13744. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  13745. dmabuf->time_stamp = jiffies;
  13746. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13747. /* Use the hdr_buf to find the sequence that this frame belongs to */
  13748. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  13749. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  13750. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  13751. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  13752. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  13753. continue;
  13754. /* found a pending sequence that matches this frame */
  13755. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13756. break;
  13757. }
  13758. if (!seq_dmabuf) {
  13759. /*
  13760. * This indicates first frame received for this sequence.
  13761. * Queue the buffer on the vport's rcv_buffer_list.
  13762. */
  13763. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  13764. lpfc_update_rcv_time_stamp(vport);
  13765. return dmabuf;
  13766. }
  13767. temp_hdr = seq_dmabuf->hbuf.virt;
  13768. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  13769. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  13770. list_del_init(&seq_dmabuf->hbuf.list);
  13771. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  13772. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  13773. lpfc_update_rcv_time_stamp(vport);
  13774. return dmabuf;
  13775. }
  13776. /* move this sequence to the tail to indicate a young sequence */
  13777. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  13778. seq_dmabuf->time_stamp = jiffies;
  13779. lpfc_update_rcv_time_stamp(vport);
  13780. if (list_empty(&seq_dmabuf->dbuf.list)) {
  13781. temp_hdr = dmabuf->hbuf.virt;
  13782. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  13783. return seq_dmabuf;
  13784. }
  13785. /* find the correct place in the sequence to insert this frame */
  13786. d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
  13787. while (!found) {
  13788. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13789. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  13790. /*
  13791. * If the frame's sequence count is greater than the frame on
  13792. * the list then insert the frame right after this frame
  13793. */
  13794. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  13795. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  13796. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  13797. found = 1;
  13798. break;
  13799. }
  13800. if (&d_buf->list == &seq_dmabuf->dbuf.list)
  13801. break;
  13802. d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
  13803. }
  13804. if (found)
  13805. return seq_dmabuf;
  13806. return NULL;
  13807. }
  13808. /**
  13809. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  13810. * @vport: pointer to a vitural port
  13811. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13812. *
  13813. * This function tries to abort from the partially assembed sequence, described
  13814. * by the information from basic abbort @dmabuf. It checks to see whether such
  13815. * partially assembled sequence held by the driver. If so, it shall free up all
  13816. * the frames from the partially assembled sequence.
  13817. *
  13818. * Return
  13819. * true -- if there is matching partially assembled sequence present and all
  13820. * the frames freed with the sequence;
  13821. * false -- if there is no matching partially assembled sequence present so
  13822. * nothing got aborted in the lower layer driver
  13823. **/
  13824. static bool
  13825. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  13826. struct hbq_dmabuf *dmabuf)
  13827. {
  13828. struct fc_frame_header *new_hdr;
  13829. struct fc_frame_header *temp_hdr;
  13830. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  13831. struct hbq_dmabuf *seq_dmabuf = NULL;
  13832. /* Use the hdr_buf to find the sequence that matches this frame */
  13833. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  13834. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  13835. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13836. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  13837. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  13838. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  13839. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  13840. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  13841. continue;
  13842. /* found a pending sequence that matches this frame */
  13843. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13844. break;
  13845. }
  13846. /* Free up all the frames from the partially assembled sequence */
  13847. if (seq_dmabuf) {
  13848. list_for_each_entry_safe(d_buf, n_buf,
  13849. &seq_dmabuf->dbuf.list, list) {
  13850. list_del_init(&d_buf->list);
  13851. lpfc_in_buf_free(vport->phba, d_buf);
  13852. }
  13853. return true;
  13854. }
  13855. return false;
  13856. }
  13857. /**
  13858. * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
  13859. * @vport: pointer to a vitural port
  13860. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13861. *
  13862. * This function tries to abort from the assembed sequence from upper level
  13863. * protocol, described by the information from basic abbort @dmabuf. It
  13864. * checks to see whether such pending context exists at upper level protocol.
  13865. * If so, it shall clean up the pending context.
  13866. *
  13867. * Return
  13868. * true -- if there is matching pending context of the sequence cleaned
  13869. * at ulp;
  13870. * false -- if there is no matching pending context of the sequence present
  13871. * at ulp.
  13872. **/
  13873. static bool
  13874. lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  13875. {
  13876. struct lpfc_hba *phba = vport->phba;
  13877. int handled;
  13878. /* Accepting abort at ulp with SLI4 only */
  13879. if (phba->sli_rev < LPFC_SLI_REV4)
  13880. return false;
  13881. /* Register all caring upper level protocols to attend abort */
  13882. handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
  13883. if (handled)
  13884. return true;
  13885. return false;
  13886. }
  13887. /**
  13888. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  13889. * @phba: Pointer to HBA context object.
  13890. * @cmd_iocbq: pointer to the command iocbq structure.
  13891. * @rsp_iocbq: pointer to the response iocbq structure.
  13892. *
  13893. * This function handles the sequence abort response iocb command complete
  13894. * event. It properly releases the memory allocated to the sequence abort
  13895. * accept iocb.
  13896. **/
  13897. static void
  13898. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  13899. struct lpfc_iocbq *cmd_iocbq,
  13900. struct lpfc_iocbq *rsp_iocbq)
  13901. {
  13902. struct lpfc_nodelist *ndlp;
  13903. if (cmd_iocbq) {
  13904. ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
  13905. lpfc_nlp_put(ndlp);
  13906. lpfc_nlp_not_used(ndlp);
  13907. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  13908. }
  13909. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  13910. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  13911. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13912. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  13913. rsp_iocbq->iocb.ulpStatus,
  13914. rsp_iocbq->iocb.un.ulpWord[4]);
  13915. }
  13916. /**
  13917. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  13918. * @phba: Pointer to HBA context object.
  13919. * @xri: xri id in transaction.
  13920. *
  13921. * This function validates the xri maps to the known range of XRIs allocated an
  13922. * used by the driver.
  13923. **/
  13924. uint16_t
  13925. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  13926. uint16_t xri)
  13927. {
  13928. uint16_t i;
  13929. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  13930. if (xri == phba->sli4_hba.xri_ids[i])
  13931. return i;
  13932. }
  13933. return NO_XRI;
  13934. }
  13935. /**
  13936. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  13937. * @phba: Pointer to HBA context object.
  13938. * @fc_hdr: pointer to a FC frame header.
  13939. *
  13940. * This function sends a basic response to a previous unsol sequence abort
  13941. * event after aborting the sequence handling.
  13942. **/
  13943. static void
  13944. lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
  13945. struct fc_frame_header *fc_hdr, bool aborted)
  13946. {
  13947. struct lpfc_hba *phba = vport->phba;
  13948. struct lpfc_iocbq *ctiocb = NULL;
  13949. struct lpfc_nodelist *ndlp;
  13950. uint16_t oxid, rxid, xri, lxri;
  13951. uint32_t sid, fctl;
  13952. IOCB_t *icmd;
  13953. int rc;
  13954. if (!lpfc_is_link_up(phba))
  13955. return;
  13956. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13957. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  13958. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  13959. ndlp = lpfc_findnode_did(vport, sid);
  13960. if (!ndlp) {
  13961. ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
  13962. if (!ndlp) {
  13963. lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
  13964. "1268 Failed to allocate ndlp for "
  13965. "oxid:x%x SID:x%x\n", oxid, sid);
  13966. return;
  13967. }
  13968. lpfc_nlp_init(vport, ndlp, sid);
  13969. /* Put ndlp onto pport node list */
  13970. lpfc_enqueue_node(vport, ndlp);
  13971. } else if (!NLP_CHK_NODE_ACT(ndlp)) {
  13972. /* re-setup ndlp without removing from node list */
  13973. ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
  13974. if (!ndlp) {
  13975. lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
  13976. "3275 Failed to active ndlp found "
  13977. "for oxid:x%x SID:x%x\n", oxid, sid);
  13978. return;
  13979. }
  13980. }
  13981. /* Allocate buffer for rsp iocb */
  13982. ctiocb = lpfc_sli_get_iocbq(phba);
  13983. if (!ctiocb)
  13984. return;
  13985. /* Extract the F_CTL field from FC_HDR */
  13986. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  13987. icmd = &ctiocb->iocb;
  13988. icmd->un.xseq64.bdl.bdeSize = 0;
  13989. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  13990. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  13991. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  13992. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  13993. /* Fill in the rest of iocb fields */
  13994. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  13995. icmd->ulpBdeCount = 0;
  13996. icmd->ulpLe = 1;
  13997. icmd->ulpClass = CLASS3;
  13998. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  13999. ctiocb->context1 = lpfc_nlp_get(ndlp);
  14000. ctiocb->iocb_cmpl = NULL;
  14001. ctiocb->vport = phba->pport;
  14002. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  14003. ctiocb->sli4_lxritag = NO_XRI;
  14004. ctiocb->sli4_xritag = NO_XRI;
  14005. if (fctl & FC_FC_EX_CTX)
  14006. /* Exchange responder sent the abort so we
  14007. * own the oxid.
  14008. */
  14009. xri = oxid;
  14010. else
  14011. xri = rxid;
  14012. lxri = lpfc_sli4_xri_inrange(phba, xri);
  14013. if (lxri != NO_XRI)
  14014. lpfc_set_rrq_active(phba, ndlp, lxri,
  14015. (xri == oxid) ? rxid : oxid, 0);
  14016. /* For BA_ABTS from exchange responder, if the logical xri with
  14017. * the oxid maps to the FCP XRI range, the port no longer has
  14018. * that exchange context, send a BLS_RJT. Override the IOCB for
  14019. * a BA_RJT.
  14020. */
  14021. if ((fctl & FC_FC_EX_CTX) &&
  14022. (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
  14023. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  14024. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  14025. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  14026. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  14027. }
  14028. /* If BA_ABTS failed to abort a partially assembled receive sequence,
  14029. * the driver no longer has that exchange, send a BLS_RJT. Override
  14030. * the IOCB for a BA_RJT.
  14031. */
  14032. if (aborted == false) {
  14033. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  14034. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  14035. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  14036. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  14037. }
  14038. if (fctl & FC_FC_EX_CTX) {
  14039. /* ABTS sent by responder to CT exchange, construction
  14040. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  14041. * field and RX_ID from ABTS for RX_ID field.
  14042. */
  14043. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  14044. } else {
  14045. /* ABTS sent by initiator to CT exchange, construction
  14046. * of BA_ACC will need to allocate a new XRI as for the
  14047. * XRI_TAG field.
  14048. */
  14049. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  14050. }
  14051. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  14052. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  14053. /* Xmit CT abts response on exchange <xid> */
  14054. lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
  14055. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  14056. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  14057. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  14058. if (rc == IOCB_ERROR) {
  14059. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  14060. "2925 Failed to issue CT ABTS RSP x%x on "
  14061. "xri x%x, Data x%x\n",
  14062. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  14063. phba->link_state);
  14064. lpfc_nlp_put(ndlp);
  14065. ctiocb->context1 = NULL;
  14066. lpfc_sli_release_iocbq(phba, ctiocb);
  14067. }
  14068. }
  14069. /**
  14070. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  14071. * @vport: Pointer to the vport on which this sequence was received
  14072. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  14073. *
  14074. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  14075. * receive sequence is only partially assembed by the driver, it shall abort
  14076. * the partially assembled frames for the sequence. Otherwise, if the
  14077. * unsolicited receive sequence has been completely assembled and passed to
  14078. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  14079. * unsolicited sequence has been aborted. After that, it will issue a basic
  14080. * accept to accept the abort.
  14081. **/
  14082. static void
  14083. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  14084. struct hbq_dmabuf *dmabuf)
  14085. {
  14086. struct lpfc_hba *phba = vport->phba;
  14087. struct fc_frame_header fc_hdr;
  14088. uint32_t fctl;
  14089. bool aborted;
  14090. /* Make a copy of fc_hdr before the dmabuf being released */
  14091. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  14092. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  14093. if (fctl & FC_FC_EX_CTX) {
  14094. /* ABTS by responder to exchange, no cleanup needed */
  14095. aborted = true;
  14096. } else {
  14097. /* ABTS by initiator to exchange, need to do cleanup */
  14098. aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  14099. if (aborted == false)
  14100. aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
  14101. }
  14102. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  14103. /* Respond with BA_ACC or BA_RJT accordingly */
  14104. lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
  14105. }
  14106. /**
  14107. * lpfc_seq_complete - Indicates if a sequence is complete
  14108. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  14109. *
  14110. * This function checks the sequence, starting with the frame described by
  14111. * @dmabuf, to see if all the frames associated with this sequence are present.
  14112. * the frames associated with this sequence are linked to the @dmabuf using the
  14113. * dbuf list. This function looks for two major things. 1) That the first frame
  14114. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  14115. * set. 3) That there are no holes in the sequence count. The function will
  14116. * return 1 when the sequence is complete, otherwise it will return 0.
  14117. **/
  14118. static int
  14119. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  14120. {
  14121. struct fc_frame_header *hdr;
  14122. struct lpfc_dmabuf *d_buf;
  14123. struct hbq_dmabuf *seq_dmabuf;
  14124. uint32_t fctl;
  14125. int seq_count = 0;
  14126. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  14127. /* make sure first fame of sequence has a sequence count of zero */
  14128. if (hdr->fh_seq_cnt != seq_count)
  14129. return 0;
  14130. fctl = (hdr->fh_f_ctl[0] << 16 |
  14131. hdr->fh_f_ctl[1] << 8 |
  14132. hdr->fh_f_ctl[2]);
  14133. /* If last frame of sequence we can return success. */
  14134. if (fctl & FC_FC_END_SEQ)
  14135. return 1;
  14136. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  14137. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  14138. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  14139. /* If there is a hole in the sequence count then fail. */
  14140. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  14141. return 0;
  14142. fctl = (hdr->fh_f_ctl[0] << 16 |
  14143. hdr->fh_f_ctl[1] << 8 |
  14144. hdr->fh_f_ctl[2]);
  14145. /* If last frame of sequence we can return success. */
  14146. if (fctl & FC_FC_END_SEQ)
  14147. return 1;
  14148. }
  14149. return 0;
  14150. }
  14151. /**
  14152. * lpfc_prep_seq - Prep sequence for ULP processing
  14153. * @vport: Pointer to the vport on which this sequence was received
  14154. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  14155. *
  14156. * This function takes a sequence, described by a list of frames, and creates
  14157. * a list of iocbq structures to describe the sequence. This iocbq list will be
  14158. * used to issue to the generic unsolicited sequence handler. This routine
  14159. * returns a pointer to the first iocbq in the list. If the function is unable
  14160. * to allocate an iocbq then it throw out the received frames that were not
  14161. * able to be described and return a pointer to the first iocbq. If unable to
  14162. * allocate any iocbqs (including the first) this function will return NULL.
  14163. **/
  14164. static struct lpfc_iocbq *
  14165. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  14166. {
  14167. struct hbq_dmabuf *hbq_buf;
  14168. struct lpfc_dmabuf *d_buf, *n_buf;
  14169. struct lpfc_iocbq *first_iocbq, *iocbq;
  14170. struct fc_frame_header *fc_hdr;
  14171. uint32_t sid;
  14172. uint32_t len, tot_len;
  14173. struct ulp_bde64 *pbde;
  14174. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  14175. /* remove from receive buffer list */
  14176. list_del_init(&seq_dmabuf->hbuf.list);
  14177. lpfc_update_rcv_time_stamp(vport);
  14178. /* get the Remote Port's SID */
  14179. sid = sli4_sid_from_fc_hdr(fc_hdr);
  14180. tot_len = 0;
  14181. /* Get an iocbq struct to fill in. */
  14182. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  14183. if (first_iocbq) {
  14184. /* Initialize the first IOCB. */
  14185. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  14186. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  14187. /* Check FC Header to see what TYPE of frame we are rcv'ing */
  14188. if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
  14189. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
  14190. first_iocbq->iocb.un.rcvels.parmRo =
  14191. sli4_did_from_fc_hdr(fc_hdr);
  14192. first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
  14193. } else
  14194. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  14195. first_iocbq->iocb.ulpContext = NO_XRI;
  14196. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  14197. be16_to_cpu(fc_hdr->fh_ox_id);
  14198. /* iocbq is prepped for internal consumption. Physical vpi. */
  14199. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  14200. vport->phba->vpi_ids[vport->vpi];
  14201. /* put the first buffer into the first IOCBq */
  14202. tot_len = bf_get(lpfc_rcqe_length,
  14203. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  14204. first_iocbq->context2 = &seq_dmabuf->dbuf;
  14205. first_iocbq->context3 = NULL;
  14206. first_iocbq->iocb.ulpBdeCount = 1;
  14207. if (tot_len > LPFC_DATA_BUF_SIZE)
  14208. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  14209. LPFC_DATA_BUF_SIZE;
  14210. else
  14211. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
  14212. first_iocbq->iocb.un.rcvels.remoteID = sid;
  14213. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  14214. }
  14215. iocbq = first_iocbq;
  14216. /*
  14217. * Each IOCBq can have two Buffers assigned, so go through the list
  14218. * of buffers for this sequence and save two buffers in each IOCBq
  14219. */
  14220. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  14221. if (!iocbq) {
  14222. lpfc_in_buf_free(vport->phba, d_buf);
  14223. continue;
  14224. }
  14225. if (!iocbq->context3) {
  14226. iocbq->context3 = d_buf;
  14227. iocbq->iocb.ulpBdeCount++;
  14228. /* We need to get the size out of the right CQE */
  14229. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  14230. len = bf_get(lpfc_rcqe_length,
  14231. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  14232. pbde = (struct ulp_bde64 *)
  14233. &iocbq->iocb.unsli3.sli3Words[4];
  14234. if (len > LPFC_DATA_BUF_SIZE)
  14235. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  14236. else
  14237. pbde->tus.f.bdeSize = len;
  14238. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  14239. tot_len += len;
  14240. } else {
  14241. iocbq = lpfc_sli_get_iocbq(vport->phba);
  14242. if (!iocbq) {
  14243. if (first_iocbq) {
  14244. first_iocbq->iocb.ulpStatus =
  14245. IOSTAT_FCP_RSP_ERROR;
  14246. first_iocbq->iocb.un.ulpWord[4] =
  14247. IOERR_NO_RESOURCES;
  14248. }
  14249. lpfc_in_buf_free(vport->phba, d_buf);
  14250. continue;
  14251. }
  14252. /* We need to get the size out of the right CQE */
  14253. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  14254. len = bf_get(lpfc_rcqe_length,
  14255. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  14256. iocbq->context2 = d_buf;
  14257. iocbq->context3 = NULL;
  14258. iocbq->iocb.ulpBdeCount = 1;
  14259. if (len > LPFC_DATA_BUF_SIZE)
  14260. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  14261. LPFC_DATA_BUF_SIZE;
  14262. else
  14263. iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
  14264. tot_len += len;
  14265. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  14266. iocbq->iocb.un.rcvels.remoteID = sid;
  14267. list_add_tail(&iocbq->list, &first_iocbq->list);
  14268. }
  14269. }
  14270. return first_iocbq;
  14271. }
  14272. static void
  14273. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  14274. struct hbq_dmabuf *seq_dmabuf)
  14275. {
  14276. struct fc_frame_header *fc_hdr;
  14277. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  14278. struct lpfc_hba *phba = vport->phba;
  14279. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  14280. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  14281. if (!iocbq) {
  14282. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14283. "2707 Ring %d handler: Failed to allocate "
  14284. "iocb Rctl x%x Type x%x received\n",
  14285. LPFC_ELS_RING,
  14286. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  14287. return;
  14288. }
  14289. if (!lpfc_complete_unsol_iocb(phba,
  14290. &phba->sli.ring[LPFC_ELS_RING],
  14291. iocbq, fc_hdr->fh_r_ctl,
  14292. fc_hdr->fh_type))
  14293. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14294. "2540 Ring %d handler: unexpected Rctl "
  14295. "x%x Type x%x received\n",
  14296. LPFC_ELS_RING,
  14297. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  14298. /* Free iocb created in lpfc_prep_seq */
  14299. list_for_each_entry_safe(curr_iocb, next_iocb,
  14300. &iocbq->list, list) {
  14301. list_del_init(&curr_iocb->list);
  14302. lpfc_sli_release_iocbq(phba, curr_iocb);
  14303. }
  14304. lpfc_sli_release_iocbq(phba, iocbq);
  14305. }
  14306. /**
  14307. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  14308. * @phba: Pointer to HBA context object.
  14309. *
  14310. * This function is called with no lock held. This function processes all
  14311. * the received buffers and gives it to upper layers when a received buffer
  14312. * indicates that it is the final frame in the sequence. The interrupt
  14313. * service routine processes received buffers at interrupt contexts and adds
  14314. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  14315. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  14316. * appropriate receive function when the final frame in a sequence is received.
  14317. **/
  14318. void
  14319. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  14320. struct hbq_dmabuf *dmabuf)
  14321. {
  14322. struct hbq_dmabuf *seq_dmabuf;
  14323. struct fc_frame_header *fc_hdr;
  14324. struct lpfc_vport *vport;
  14325. uint32_t fcfi;
  14326. uint32_t did;
  14327. /* Process each received buffer */
  14328. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  14329. /* check to see if this a valid type of frame */
  14330. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  14331. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  14332. return;
  14333. }
  14334. if ((bf_get(lpfc_cqe_code,
  14335. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  14336. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  14337. &dmabuf->cq_event.cqe.rcqe_cmpl);
  14338. else
  14339. fcfi = bf_get(lpfc_rcqe_fcf_id,
  14340. &dmabuf->cq_event.cqe.rcqe_cmpl);
  14341. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  14342. if (!vport) {
  14343. /* throw out the frame */
  14344. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  14345. return;
  14346. }
  14347. /* d_id this frame is directed to */
  14348. did = sli4_did_from_fc_hdr(fc_hdr);
  14349. /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
  14350. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
  14351. (did != Fabric_DID)) {
  14352. /*
  14353. * Throw out the frame if we are not pt2pt.
  14354. * The pt2pt protocol allows for discovery frames
  14355. * to be received without a registered VPI.
  14356. */
  14357. if (!(vport->fc_flag & FC_PT2PT) ||
  14358. (phba->link_state == LPFC_HBA_READY)) {
  14359. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  14360. return;
  14361. }
  14362. }
  14363. /* Handle the basic abort sequence (BA_ABTS) event */
  14364. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  14365. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  14366. return;
  14367. }
  14368. /* Link this frame */
  14369. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  14370. if (!seq_dmabuf) {
  14371. /* unable to add frame to vport - throw it out */
  14372. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  14373. return;
  14374. }
  14375. /* If not last frame in sequence continue processing frames. */
  14376. if (!lpfc_seq_complete(seq_dmabuf))
  14377. return;
  14378. /* Send the complete sequence to the upper layer protocol */
  14379. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  14380. }
  14381. /**
  14382. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  14383. * @phba: pointer to lpfc hba data structure.
  14384. *
  14385. * This routine is invoked to post rpi header templates to the
  14386. * HBA consistent with the SLI-4 interface spec. This routine
  14387. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  14388. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  14389. *
  14390. * This routine does not require any locks. It's usage is expected
  14391. * to be driver load or reset recovery when the driver is
  14392. * sequential.
  14393. *
  14394. * Return codes
  14395. * 0 - successful
  14396. * -EIO - The mailbox failed to complete successfully.
  14397. * When this error occurs, the driver is not guaranteed
  14398. * to have any rpi regions posted to the device and
  14399. * must either attempt to repost the regions or take a
  14400. * fatal error.
  14401. **/
  14402. int
  14403. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  14404. {
  14405. struct lpfc_rpi_hdr *rpi_page;
  14406. uint32_t rc = 0;
  14407. uint16_t lrpi = 0;
  14408. /* SLI4 ports that support extents do not require RPI headers. */
  14409. if (!phba->sli4_hba.rpi_hdrs_in_use)
  14410. goto exit;
  14411. if (phba->sli4_hba.extents_in_use)
  14412. return -EIO;
  14413. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  14414. /*
  14415. * Assign the rpi headers a physical rpi only if the driver
  14416. * has not initialized those resources. A port reset only
  14417. * needs the headers posted.
  14418. */
  14419. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  14420. LPFC_RPI_RSRC_RDY)
  14421. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  14422. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  14423. if (rc != MBX_SUCCESS) {
  14424. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14425. "2008 Error %d posting all rpi "
  14426. "headers\n", rc);
  14427. rc = -EIO;
  14428. break;
  14429. }
  14430. }
  14431. exit:
  14432. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  14433. LPFC_RPI_RSRC_RDY);
  14434. return rc;
  14435. }
  14436. /**
  14437. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  14438. * @phba: pointer to lpfc hba data structure.
  14439. * @rpi_page: pointer to the rpi memory region.
  14440. *
  14441. * This routine is invoked to post a single rpi header to the
  14442. * HBA consistent with the SLI-4 interface spec. This memory region
  14443. * maps up to 64 rpi context regions.
  14444. *
  14445. * Return codes
  14446. * 0 - successful
  14447. * -ENOMEM - No available memory
  14448. * -EIO - The mailbox failed to complete successfully.
  14449. **/
  14450. int
  14451. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  14452. {
  14453. LPFC_MBOXQ_t *mboxq;
  14454. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  14455. uint32_t rc = 0;
  14456. uint32_t shdr_status, shdr_add_status;
  14457. union lpfc_sli4_cfg_shdr *shdr;
  14458. /* SLI4 ports that support extents do not require RPI headers. */
  14459. if (!phba->sli4_hba.rpi_hdrs_in_use)
  14460. return rc;
  14461. if (phba->sli4_hba.extents_in_use)
  14462. return -EIO;
  14463. /* The port is notified of the header region via a mailbox command. */
  14464. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14465. if (!mboxq) {
  14466. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14467. "2001 Unable to allocate memory for issuing "
  14468. "SLI_CONFIG_SPECIAL mailbox command\n");
  14469. return -ENOMEM;
  14470. }
  14471. /* Post all rpi memory regions to the port. */
  14472. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  14473. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  14474. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  14475. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  14476. sizeof(struct lpfc_sli4_cfg_mhdr),
  14477. LPFC_SLI4_MBX_EMBED);
  14478. /* Post the physical rpi to the port for this rpi header. */
  14479. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  14480. rpi_page->start_rpi);
  14481. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  14482. hdr_tmpl, rpi_page->page_count);
  14483. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  14484. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  14485. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14486. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  14487. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14488. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14489. if (rc != MBX_TIMEOUT)
  14490. mempool_free(mboxq, phba->mbox_mem_pool);
  14491. if (shdr_status || shdr_add_status || rc) {
  14492. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14493. "2514 POST_RPI_HDR mailbox failed with "
  14494. "status x%x add_status x%x, mbx status x%x\n",
  14495. shdr_status, shdr_add_status, rc);
  14496. rc = -ENXIO;
  14497. }
  14498. return rc;
  14499. }
  14500. /**
  14501. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  14502. * @phba: pointer to lpfc hba data structure.
  14503. *
  14504. * This routine is invoked to post rpi header templates to the
  14505. * HBA consistent with the SLI-4 interface spec. This routine
  14506. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  14507. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  14508. *
  14509. * Returns
  14510. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  14511. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  14512. **/
  14513. int
  14514. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  14515. {
  14516. unsigned long rpi;
  14517. uint16_t max_rpi, rpi_limit;
  14518. uint16_t rpi_remaining, lrpi = 0;
  14519. struct lpfc_rpi_hdr *rpi_hdr;
  14520. unsigned long iflag;
  14521. /*
  14522. * Fetch the next logical rpi. Because this index is logical,
  14523. * the driver starts at 0 each time.
  14524. */
  14525. spin_lock_irqsave(&phba->hbalock, iflag);
  14526. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  14527. rpi_limit = phba->sli4_hba.next_rpi;
  14528. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  14529. if (rpi >= rpi_limit)
  14530. rpi = LPFC_RPI_ALLOC_ERROR;
  14531. else {
  14532. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  14533. phba->sli4_hba.max_cfg_param.rpi_used++;
  14534. phba->sli4_hba.rpi_count++;
  14535. }
  14536. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  14537. "0001 rpi:%x max:%x lim:%x\n",
  14538. (int) rpi, max_rpi, rpi_limit);
  14539. /*
  14540. * Don't try to allocate more rpi header regions if the device limit
  14541. * has been exhausted.
  14542. */
  14543. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  14544. (phba->sli4_hba.rpi_count >= max_rpi)) {
  14545. spin_unlock_irqrestore(&phba->hbalock, iflag);
  14546. return rpi;
  14547. }
  14548. /*
  14549. * RPI header postings are not required for SLI4 ports capable of
  14550. * extents.
  14551. */
  14552. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  14553. spin_unlock_irqrestore(&phba->hbalock, iflag);
  14554. return rpi;
  14555. }
  14556. /*
  14557. * If the driver is running low on rpi resources, allocate another
  14558. * page now. Note that the next_rpi value is used because
  14559. * it represents how many are actually in use whereas max_rpi notes
  14560. * how many are supported max by the device.
  14561. */
  14562. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  14563. spin_unlock_irqrestore(&phba->hbalock, iflag);
  14564. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  14565. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  14566. if (!rpi_hdr) {
  14567. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14568. "2002 Error Could not grow rpi "
  14569. "count\n");
  14570. } else {
  14571. lrpi = rpi_hdr->start_rpi;
  14572. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  14573. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  14574. }
  14575. }
  14576. return rpi;
  14577. }
  14578. /**
  14579. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  14580. * @phba: pointer to lpfc hba data structure.
  14581. *
  14582. * This routine is invoked to release an rpi to the pool of
  14583. * available rpis maintained by the driver.
  14584. **/
  14585. static void
  14586. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  14587. {
  14588. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  14589. phba->sli4_hba.rpi_count--;
  14590. phba->sli4_hba.max_cfg_param.rpi_used--;
  14591. }
  14592. }
  14593. /**
  14594. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  14595. * @phba: pointer to lpfc hba data structure.
  14596. *
  14597. * This routine is invoked to release an rpi to the pool of
  14598. * available rpis maintained by the driver.
  14599. **/
  14600. void
  14601. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  14602. {
  14603. spin_lock_irq(&phba->hbalock);
  14604. __lpfc_sli4_free_rpi(phba, rpi);
  14605. spin_unlock_irq(&phba->hbalock);
  14606. }
  14607. /**
  14608. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  14609. * @phba: pointer to lpfc hba data structure.
  14610. *
  14611. * This routine is invoked to remove the memory region that
  14612. * provided rpi via a bitmask.
  14613. **/
  14614. void
  14615. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  14616. {
  14617. kfree(phba->sli4_hba.rpi_bmask);
  14618. kfree(phba->sli4_hba.rpi_ids);
  14619. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  14620. }
  14621. /**
  14622. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  14623. * @phba: pointer to lpfc hba data structure.
  14624. *
  14625. * This routine is invoked to remove the memory region that
  14626. * provided rpi via a bitmask.
  14627. **/
  14628. int
  14629. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  14630. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  14631. {
  14632. LPFC_MBOXQ_t *mboxq;
  14633. struct lpfc_hba *phba = ndlp->phba;
  14634. int rc;
  14635. /* The port is notified of the header region via a mailbox command. */
  14636. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14637. if (!mboxq)
  14638. return -ENOMEM;
  14639. /* Post all rpi memory regions to the port. */
  14640. lpfc_resume_rpi(mboxq, ndlp);
  14641. if (cmpl) {
  14642. mboxq->mbox_cmpl = cmpl;
  14643. mboxq->context1 = arg;
  14644. mboxq->context2 = ndlp;
  14645. } else
  14646. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14647. mboxq->vport = ndlp->vport;
  14648. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14649. if (rc == MBX_NOT_FINISHED) {
  14650. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14651. "2010 Resume RPI Mailbox failed "
  14652. "status %d, mbxStatus x%x\n", rc,
  14653. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  14654. mempool_free(mboxq, phba->mbox_mem_pool);
  14655. return -EIO;
  14656. }
  14657. return 0;
  14658. }
  14659. /**
  14660. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  14661. * @vport: Pointer to the vport for which the vpi is being initialized
  14662. *
  14663. * This routine is invoked to activate a vpi with the port.
  14664. *
  14665. * Returns:
  14666. * 0 success
  14667. * -Evalue otherwise
  14668. **/
  14669. int
  14670. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  14671. {
  14672. LPFC_MBOXQ_t *mboxq;
  14673. int rc = 0;
  14674. int retval = MBX_SUCCESS;
  14675. uint32_t mbox_tmo;
  14676. struct lpfc_hba *phba = vport->phba;
  14677. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14678. if (!mboxq)
  14679. return -ENOMEM;
  14680. lpfc_init_vpi(phba, mboxq, vport->vpi);
  14681. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  14682. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  14683. if (rc != MBX_SUCCESS) {
  14684. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  14685. "2022 INIT VPI Mailbox failed "
  14686. "status %d, mbxStatus x%x\n", rc,
  14687. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  14688. retval = -EIO;
  14689. }
  14690. if (rc != MBX_TIMEOUT)
  14691. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  14692. return retval;
  14693. }
  14694. /**
  14695. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  14696. * @phba: pointer to lpfc hba data structure.
  14697. * @mboxq: Pointer to mailbox object.
  14698. *
  14699. * This routine is invoked to manually add a single FCF record. The caller
  14700. * must pass a completely initialized FCF_Record. This routine takes
  14701. * care of the nonembedded mailbox operations.
  14702. **/
  14703. static void
  14704. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  14705. {
  14706. void *virt_addr;
  14707. union lpfc_sli4_cfg_shdr *shdr;
  14708. uint32_t shdr_status, shdr_add_status;
  14709. virt_addr = mboxq->sge_array->addr[0];
  14710. /* The IOCTL status is embedded in the mailbox subheader. */
  14711. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  14712. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14713. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14714. if ((shdr_status || shdr_add_status) &&
  14715. (shdr_status != STATUS_FCF_IN_USE))
  14716. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14717. "2558 ADD_FCF_RECORD mailbox failed with "
  14718. "status x%x add_status x%x\n",
  14719. shdr_status, shdr_add_status);
  14720. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14721. }
  14722. /**
  14723. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  14724. * @phba: pointer to lpfc hba data structure.
  14725. * @fcf_record: pointer to the initialized fcf record to add.
  14726. *
  14727. * This routine is invoked to manually add a single FCF record. The caller
  14728. * must pass a completely initialized FCF_Record. This routine takes
  14729. * care of the nonembedded mailbox operations.
  14730. **/
  14731. int
  14732. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  14733. {
  14734. int rc = 0;
  14735. LPFC_MBOXQ_t *mboxq;
  14736. uint8_t *bytep;
  14737. void *virt_addr;
  14738. struct lpfc_mbx_sge sge;
  14739. uint32_t alloc_len, req_len;
  14740. uint32_t fcfindex;
  14741. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14742. if (!mboxq) {
  14743. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14744. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  14745. return -ENOMEM;
  14746. }
  14747. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  14748. sizeof(uint32_t);
  14749. /* Allocate DMA memory and set up the non-embedded mailbox command */
  14750. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  14751. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  14752. req_len, LPFC_SLI4_MBX_NEMBED);
  14753. if (alloc_len < req_len) {
  14754. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14755. "2523 Allocated DMA memory size (x%x) is "
  14756. "less than the requested DMA memory "
  14757. "size (x%x)\n", alloc_len, req_len);
  14758. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14759. return -ENOMEM;
  14760. }
  14761. /*
  14762. * Get the first SGE entry from the non-embedded DMA memory. This
  14763. * routine only uses a single SGE.
  14764. */
  14765. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  14766. virt_addr = mboxq->sge_array->addr[0];
  14767. /*
  14768. * Configure the FCF record for FCFI 0. This is the driver's
  14769. * hardcoded default and gets used in nonFIP mode.
  14770. */
  14771. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  14772. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  14773. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  14774. /*
  14775. * Copy the fcf_index and the FCF Record Data. The data starts after
  14776. * the FCoE header plus word10. The data copy needs to be endian
  14777. * correct.
  14778. */
  14779. bytep += sizeof(uint32_t);
  14780. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  14781. mboxq->vport = phba->pport;
  14782. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  14783. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14784. if (rc == MBX_NOT_FINISHED) {
  14785. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14786. "2515 ADD_FCF_RECORD mailbox failed with "
  14787. "status 0x%x\n", rc);
  14788. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14789. rc = -EIO;
  14790. } else
  14791. rc = 0;
  14792. return rc;
  14793. }
  14794. /**
  14795. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  14796. * @phba: pointer to lpfc hba data structure.
  14797. * @fcf_record: pointer to the fcf record to write the default data.
  14798. * @fcf_index: FCF table entry index.
  14799. *
  14800. * This routine is invoked to build the driver's default FCF record. The
  14801. * values used are hardcoded. This routine handles memory initialization.
  14802. *
  14803. **/
  14804. void
  14805. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  14806. struct fcf_record *fcf_record,
  14807. uint16_t fcf_index)
  14808. {
  14809. memset(fcf_record, 0, sizeof(struct fcf_record));
  14810. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  14811. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  14812. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  14813. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  14814. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  14815. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  14816. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  14817. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  14818. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  14819. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  14820. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  14821. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  14822. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  14823. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  14824. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  14825. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  14826. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  14827. /* Set the VLAN bit map */
  14828. if (phba->valid_vlan) {
  14829. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  14830. = 1 << (phba->vlan_id % 8);
  14831. }
  14832. }
  14833. /**
  14834. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  14835. * @phba: pointer to lpfc hba data structure.
  14836. * @fcf_index: FCF table entry offset.
  14837. *
  14838. * This routine is invoked to scan the entire FCF table by reading FCF
  14839. * record and processing it one at a time starting from the @fcf_index
  14840. * for initial FCF discovery or fast FCF failover rediscovery.
  14841. *
  14842. * Return 0 if the mailbox command is submitted successfully, none 0
  14843. * otherwise.
  14844. **/
  14845. int
  14846. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14847. {
  14848. int rc = 0, error;
  14849. LPFC_MBOXQ_t *mboxq;
  14850. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  14851. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  14852. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14853. if (!mboxq) {
  14854. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14855. "2000 Failed to allocate mbox for "
  14856. "READ_FCF cmd\n");
  14857. error = -ENOMEM;
  14858. goto fail_fcf_scan;
  14859. }
  14860. /* Construct the read FCF record mailbox command */
  14861. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14862. if (rc) {
  14863. error = -EINVAL;
  14864. goto fail_fcf_scan;
  14865. }
  14866. /* Issue the mailbox command asynchronously */
  14867. mboxq->vport = phba->pport;
  14868. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  14869. spin_lock_irq(&phba->hbalock);
  14870. phba->hba_flag |= FCF_TS_INPROG;
  14871. spin_unlock_irq(&phba->hbalock);
  14872. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14873. if (rc == MBX_NOT_FINISHED)
  14874. error = -EIO;
  14875. else {
  14876. /* Reset eligible FCF count for new scan */
  14877. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  14878. phba->fcf.eligible_fcf_cnt = 0;
  14879. error = 0;
  14880. }
  14881. fail_fcf_scan:
  14882. if (error) {
  14883. if (mboxq)
  14884. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14885. /* FCF scan failed, clear FCF_TS_INPROG flag */
  14886. spin_lock_irq(&phba->hbalock);
  14887. phba->hba_flag &= ~FCF_TS_INPROG;
  14888. spin_unlock_irq(&phba->hbalock);
  14889. }
  14890. return error;
  14891. }
  14892. /**
  14893. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  14894. * @phba: pointer to lpfc hba data structure.
  14895. * @fcf_index: FCF table entry offset.
  14896. *
  14897. * This routine is invoked to read an FCF record indicated by @fcf_index
  14898. * and to use it for FLOGI roundrobin FCF failover.
  14899. *
  14900. * Return 0 if the mailbox command is submitted successfully, none 0
  14901. * otherwise.
  14902. **/
  14903. int
  14904. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14905. {
  14906. int rc = 0, error;
  14907. LPFC_MBOXQ_t *mboxq;
  14908. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14909. if (!mboxq) {
  14910. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  14911. "2763 Failed to allocate mbox for "
  14912. "READ_FCF cmd\n");
  14913. error = -ENOMEM;
  14914. goto fail_fcf_read;
  14915. }
  14916. /* Construct the read FCF record mailbox command */
  14917. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14918. if (rc) {
  14919. error = -EINVAL;
  14920. goto fail_fcf_read;
  14921. }
  14922. /* Issue the mailbox command asynchronously */
  14923. mboxq->vport = phba->pport;
  14924. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  14925. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14926. if (rc == MBX_NOT_FINISHED)
  14927. error = -EIO;
  14928. else
  14929. error = 0;
  14930. fail_fcf_read:
  14931. if (error && mboxq)
  14932. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14933. return error;
  14934. }
  14935. /**
  14936. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  14937. * @phba: pointer to lpfc hba data structure.
  14938. * @fcf_index: FCF table entry offset.
  14939. *
  14940. * This routine is invoked to read an FCF record indicated by @fcf_index to
  14941. * determine whether it's eligible for FLOGI roundrobin failover list.
  14942. *
  14943. * Return 0 if the mailbox command is submitted successfully, none 0
  14944. * otherwise.
  14945. **/
  14946. int
  14947. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14948. {
  14949. int rc = 0, error;
  14950. LPFC_MBOXQ_t *mboxq;
  14951. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14952. if (!mboxq) {
  14953. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  14954. "2758 Failed to allocate mbox for "
  14955. "READ_FCF cmd\n");
  14956. error = -ENOMEM;
  14957. goto fail_fcf_read;
  14958. }
  14959. /* Construct the read FCF record mailbox command */
  14960. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14961. if (rc) {
  14962. error = -EINVAL;
  14963. goto fail_fcf_read;
  14964. }
  14965. /* Issue the mailbox command asynchronously */
  14966. mboxq->vport = phba->pport;
  14967. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  14968. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14969. if (rc == MBX_NOT_FINISHED)
  14970. error = -EIO;
  14971. else
  14972. error = 0;
  14973. fail_fcf_read:
  14974. if (error && mboxq)
  14975. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14976. return error;
  14977. }
  14978. /**
  14979. * lpfc_check_next_fcf_pri_level
  14980. * phba pointer to the lpfc_hba struct for this port.
  14981. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  14982. * routine when the rr_bmask is empty. The FCF indecies are put into the
  14983. * rr_bmask based on their priority level. Starting from the highest priority
  14984. * to the lowest. The most likely FCF candidate will be in the highest
  14985. * priority group. When this routine is called it searches the fcf_pri list for
  14986. * next lowest priority group and repopulates the rr_bmask with only those
  14987. * fcf_indexes.
  14988. * returns:
  14989. * 1=success 0=failure
  14990. **/
  14991. static int
  14992. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  14993. {
  14994. uint16_t next_fcf_pri;
  14995. uint16_t last_index;
  14996. struct lpfc_fcf_pri *fcf_pri;
  14997. int rc;
  14998. int ret = 0;
  14999. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  15000. LPFC_SLI4_FCF_TBL_INDX_MAX);
  15001. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  15002. "3060 Last IDX %d\n", last_index);
  15003. /* Verify the priority list has 2 or more entries */
  15004. spin_lock_irq(&phba->hbalock);
  15005. if (list_empty(&phba->fcf.fcf_pri_list) ||
  15006. list_is_singular(&phba->fcf.fcf_pri_list)) {
  15007. spin_unlock_irq(&phba->hbalock);
  15008. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  15009. "3061 Last IDX %d\n", last_index);
  15010. return 0; /* Empty rr list */
  15011. }
  15012. spin_unlock_irq(&phba->hbalock);
  15013. next_fcf_pri = 0;
  15014. /*
  15015. * Clear the rr_bmask and set all of the bits that are at this
  15016. * priority.
  15017. */
  15018. memset(phba->fcf.fcf_rr_bmask, 0,
  15019. sizeof(*phba->fcf.fcf_rr_bmask));
  15020. spin_lock_irq(&phba->hbalock);
  15021. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  15022. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  15023. continue;
  15024. /*
  15025. * the 1st priority that has not FLOGI failed
  15026. * will be the highest.
  15027. */
  15028. if (!next_fcf_pri)
  15029. next_fcf_pri = fcf_pri->fcf_rec.priority;
  15030. spin_unlock_irq(&phba->hbalock);
  15031. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  15032. rc = lpfc_sli4_fcf_rr_index_set(phba,
  15033. fcf_pri->fcf_rec.fcf_index);
  15034. if (rc)
  15035. return 0;
  15036. }
  15037. spin_lock_irq(&phba->hbalock);
  15038. }
  15039. /*
  15040. * if next_fcf_pri was not set above and the list is not empty then
  15041. * we have failed flogis on all of them. So reset flogi failed
  15042. * and start at the beginning.
  15043. */
  15044. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  15045. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  15046. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  15047. /*
  15048. * the 1st priority that has not FLOGI failed
  15049. * will be the highest.
  15050. */
  15051. if (!next_fcf_pri)
  15052. next_fcf_pri = fcf_pri->fcf_rec.priority;
  15053. spin_unlock_irq(&phba->hbalock);
  15054. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  15055. rc = lpfc_sli4_fcf_rr_index_set(phba,
  15056. fcf_pri->fcf_rec.fcf_index);
  15057. if (rc)
  15058. return 0;
  15059. }
  15060. spin_lock_irq(&phba->hbalock);
  15061. }
  15062. } else
  15063. ret = 1;
  15064. spin_unlock_irq(&phba->hbalock);
  15065. return ret;
  15066. }
  15067. /**
  15068. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  15069. * @phba: pointer to lpfc hba data structure.
  15070. *
  15071. * This routine is to get the next eligible FCF record index in a round
  15072. * robin fashion. If the next eligible FCF record index equals to the
  15073. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  15074. * shall be returned, otherwise, the next eligible FCF record's index
  15075. * shall be returned.
  15076. **/
  15077. uint16_t
  15078. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  15079. {
  15080. uint16_t next_fcf_index;
  15081. initial_priority:
  15082. /* Search start from next bit of currently registered FCF index */
  15083. next_fcf_index = phba->fcf.current_rec.fcf_indx;
  15084. next_priority:
  15085. /* Determine the next fcf index to check */
  15086. next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
  15087. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  15088. LPFC_SLI4_FCF_TBL_INDX_MAX,
  15089. next_fcf_index);
  15090. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  15091. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  15092. /*
  15093. * If we have wrapped then we need to clear the bits that
  15094. * have been tested so that we can detect when we should
  15095. * change the priority level.
  15096. */
  15097. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  15098. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  15099. }
  15100. /* Check roundrobin failover list empty condition */
  15101. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  15102. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  15103. /*
  15104. * If next fcf index is not found check if there are lower
  15105. * Priority level fcf's in the fcf_priority list.
  15106. * Set up the rr_bmask with all of the avaiable fcf bits
  15107. * at that level and continue the selection process.
  15108. */
  15109. if (lpfc_check_next_fcf_pri_level(phba))
  15110. goto initial_priority;
  15111. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  15112. "2844 No roundrobin failover FCF available\n");
  15113. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  15114. return LPFC_FCOE_FCF_NEXT_NONE;
  15115. else {
  15116. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  15117. "3063 Only FCF available idx %d, flag %x\n",
  15118. next_fcf_index,
  15119. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  15120. return next_fcf_index;
  15121. }
  15122. }
  15123. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  15124. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  15125. LPFC_FCF_FLOGI_FAILED) {
  15126. if (list_is_singular(&phba->fcf.fcf_pri_list))
  15127. return LPFC_FCOE_FCF_NEXT_NONE;
  15128. goto next_priority;
  15129. }
  15130. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  15131. "2845 Get next roundrobin failover FCF (x%x)\n",
  15132. next_fcf_index);
  15133. return next_fcf_index;
  15134. }
  15135. /**
  15136. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  15137. * @phba: pointer to lpfc hba data structure.
  15138. *
  15139. * This routine sets the FCF record index in to the eligible bmask for
  15140. * roundrobin failover search. It checks to make sure that the index
  15141. * does not go beyond the range of the driver allocated bmask dimension
  15142. * before setting the bit.
  15143. *
  15144. * Returns 0 if the index bit successfully set, otherwise, it returns
  15145. * -EINVAL.
  15146. **/
  15147. int
  15148. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  15149. {
  15150. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  15151. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  15152. "2610 FCF (x%x) reached driver's book "
  15153. "keeping dimension:x%x\n",
  15154. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  15155. return -EINVAL;
  15156. }
  15157. /* Set the eligible FCF record index bmask */
  15158. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  15159. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  15160. "2790 Set FCF (x%x) to roundrobin FCF failover "
  15161. "bmask\n", fcf_index);
  15162. return 0;
  15163. }
  15164. /**
  15165. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  15166. * @phba: pointer to lpfc hba data structure.
  15167. *
  15168. * This routine clears the FCF record index from the eligible bmask for
  15169. * roundrobin failover search. It checks to make sure that the index
  15170. * does not go beyond the range of the driver allocated bmask dimension
  15171. * before clearing the bit.
  15172. **/
  15173. void
  15174. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  15175. {
  15176. struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
  15177. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  15178. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  15179. "2762 FCF (x%x) reached driver's book "
  15180. "keeping dimension:x%x\n",
  15181. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  15182. return;
  15183. }
  15184. /* Clear the eligible FCF record index bmask */
  15185. spin_lock_irq(&phba->hbalock);
  15186. list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
  15187. list) {
  15188. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  15189. list_del_init(&fcf_pri->list);
  15190. break;
  15191. }
  15192. }
  15193. spin_unlock_irq(&phba->hbalock);
  15194. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  15195. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  15196. "2791 Clear FCF (x%x) from roundrobin failover "
  15197. "bmask\n", fcf_index);
  15198. }
  15199. /**
  15200. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  15201. * @phba: pointer to lpfc hba data structure.
  15202. *
  15203. * This routine is the completion routine for the rediscover FCF table mailbox
  15204. * command. If the mailbox command returned failure, it will try to stop the
  15205. * FCF rediscover wait timer.
  15206. **/
  15207. static void
  15208. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  15209. {
  15210. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  15211. uint32_t shdr_status, shdr_add_status;
  15212. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  15213. shdr_status = bf_get(lpfc_mbox_hdr_status,
  15214. &redisc_fcf->header.cfg_shdr.response);
  15215. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  15216. &redisc_fcf->header.cfg_shdr.response);
  15217. if (shdr_status || shdr_add_status) {
  15218. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  15219. "2746 Requesting for FCF rediscovery failed "
  15220. "status x%x add_status x%x\n",
  15221. shdr_status, shdr_add_status);
  15222. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  15223. spin_lock_irq(&phba->hbalock);
  15224. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  15225. spin_unlock_irq(&phba->hbalock);
  15226. /*
  15227. * CVL event triggered FCF rediscover request failed,
  15228. * last resort to re-try current registered FCF entry.
  15229. */
  15230. lpfc_retry_pport_discovery(phba);
  15231. } else {
  15232. spin_lock_irq(&phba->hbalock);
  15233. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  15234. spin_unlock_irq(&phba->hbalock);
  15235. /*
  15236. * DEAD FCF event triggered FCF rediscover request
  15237. * failed, last resort to fail over as a link down
  15238. * to FCF registration.
  15239. */
  15240. lpfc_sli4_fcf_dead_failthrough(phba);
  15241. }
  15242. } else {
  15243. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  15244. "2775 Start FCF rediscover quiescent timer\n");
  15245. /*
  15246. * Start FCF rediscovery wait timer for pending FCF
  15247. * before rescan FCF record table.
  15248. */
  15249. lpfc_fcf_redisc_wait_start_timer(phba);
  15250. }
  15251. mempool_free(mbox, phba->mbox_mem_pool);
  15252. }
  15253. /**
  15254. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  15255. * @phba: pointer to lpfc hba data structure.
  15256. *
  15257. * This routine is invoked to request for rediscovery of the entire FCF table
  15258. * by the port.
  15259. **/
  15260. int
  15261. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  15262. {
  15263. LPFC_MBOXQ_t *mbox;
  15264. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  15265. int rc, length;
  15266. /* Cancel retry delay timers to all vports before FCF rediscover */
  15267. lpfc_cancel_all_vport_retry_delay_timer(phba);
  15268. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  15269. if (!mbox) {
  15270. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15271. "2745 Failed to allocate mbox for "
  15272. "requesting FCF rediscover.\n");
  15273. return -ENOMEM;
  15274. }
  15275. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  15276. sizeof(struct lpfc_sli4_cfg_mhdr));
  15277. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  15278. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  15279. length, LPFC_SLI4_MBX_EMBED);
  15280. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  15281. /* Set count to 0 for invalidating the entire FCF database */
  15282. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  15283. /* Issue the mailbox command asynchronously */
  15284. mbox->vport = phba->pport;
  15285. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  15286. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  15287. if (rc == MBX_NOT_FINISHED) {
  15288. mempool_free(mbox, phba->mbox_mem_pool);
  15289. return -EIO;
  15290. }
  15291. return 0;
  15292. }
  15293. /**
  15294. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  15295. * @phba: pointer to lpfc hba data structure.
  15296. *
  15297. * This function is the failover routine as a last resort to the FCF DEAD
  15298. * event when driver failed to perform fast FCF failover.
  15299. **/
  15300. void
  15301. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  15302. {
  15303. uint32_t link_state;
  15304. /*
  15305. * Last resort as FCF DEAD event failover will treat this as
  15306. * a link down, but save the link state because we don't want
  15307. * it to be changed to Link Down unless it is already down.
  15308. */
  15309. link_state = phba->link_state;
  15310. lpfc_linkdown(phba);
  15311. phba->link_state = link_state;
  15312. /* Unregister FCF if no devices connected to it */
  15313. lpfc_unregister_unused_fcf(phba);
  15314. }
  15315. /**
  15316. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  15317. * @phba: pointer to lpfc hba data structure.
  15318. * @rgn23_data: pointer to configure region 23 data.
  15319. *
  15320. * This function gets SLI3 port configure region 23 data through memory dump
  15321. * mailbox command. When it successfully retrieves data, the size of the data
  15322. * will be returned, otherwise, 0 will be returned.
  15323. **/
  15324. static uint32_t
  15325. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  15326. {
  15327. LPFC_MBOXQ_t *pmb = NULL;
  15328. MAILBOX_t *mb;
  15329. uint32_t offset = 0;
  15330. int rc;
  15331. if (!rgn23_data)
  15332. return 0;
  15333. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  15334. if (!pmb) {
  15335. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15336. "2600 failed to allocate mailbox memory\n");
  15337. return 0;
  15338. }
  15339. mb = &pmb->u.mb;
  15340. do {
  15341. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  15342. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  15343. if (rc != MBX_SUCCESS) {
  15344. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  15345. "2601 failed to read config "
  15346. "region 23, rc 0x%x Status 0x%x\n",
  15347. rc, mb->mbxStatus);
  15348. mb->un.varDmp.word_cnt = 0;
  15349. }
  15350. /*
  15351. * dump mem may return a zero when finished or we got a
  15352. * mailbox error, either way we are done.
  15353. */
  15354. if (mb->un.varDmp.word_cnt == 0)
  15355. break;
  15356. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  15357. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  15358. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  15359. rgn23_data + offset,
  15360. mb->un.varDmp.word_cnt);
  15361. offset += mb->un.varDmp.word_cnt;
  15362. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  15363. mempool_free(pmb, phba->mbox_mem_pool);
  15364. return offset;
  15365. }
  15366. /**
  15367. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  15368. * @phba: pointer to lpfc hba data structure.
  15369. * @rgn23_data: pointer to configure region 23 data.
  15370. *
  15371. * This function gets SLI4 port configure region 23 data through memory dump
  15372. * mailbox command. When it successfully retrieves data, the size of the data
  15373. * will be returned, otherwise, 0 will be returned.
  15374. **/
  15375. static uint32_t
  15376. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  15377. {
  15378. LPFC_MBOXQ_t *mboxq = NULL;
  15379. struct lpfc_dmabuf *mp = NULL;
  15380. struct lpfc_mqe *mqe;
  15381. uint32_t data_length = 0;
  15382. int rc;
  15383. if (!rgn23_data)
  15384. return 0;
  15385. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  15386. if (!mboxq) {
  15387. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15388. "3105 failed to allocate mailbox memory\n");
  15389. return 0;
  15390. }
  15391. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  15392. goto out;
  15393. mqe = &mboxq->u.mqe;
  15394. mp = (struct lpfc_dmabuf *) mboxq->context1;
  15395. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  15396. if (rc)
  15397. goto out;
  15398. data_length = mqe->un.mb_words[5];
  15399. if (data_length == 0)
  15400. goto out;
  15401. if (data_length > DMP_RGN23_SIZE) {
  15402. data_length = 0;
  15403. goto out;
  15404. }
  15405. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  15406. out:
  15407. mempool_free(mboxq, phba->mbox_mem_pool);
  15408. if (mp) {
  15409. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  15410. kfree(mp);
  15411. }
  15412. return data_length;
  15413. }
  15414. /**
  15415. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  15416. * @phba: pointer to lpfc hba data structure.
  15417. *
  15418. * This function read region 23 and parse TLV for port status to
  15419. * decide if the user disaled the port. If the TLV indicates the
  15420. * port is disabled, the hba_flag is set accordingly.
  15421. **/
  15422. void
  15423. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  15424. {
  15425. uint8_t *rgn23_data = NULL;
  15426. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  15427. uint32_t offset = 0;
  15428. /* Get adapter Region 23 data */
  15429. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  15430. if (!rgn23_data)
  15431. goto out;
  15432. if (phba->sli_rev < LPFC_SLI_REV4)
  15433. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  15434. else {
  15435. if_type = bf_get(lpfc_sli_intf_if_type,
  15436. &phba->sli4_hba.sli_intf);
  15437. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  15438. goto out;
  15439. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  15440. }
  15441. if (!data_size)
  15442. goto out;
  15443. /* Check the region signature first */
  15444. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  15445. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15446. "2619 Config region 23 has bad signature\n");
  15447. goto out;
  15448. }
  15449. offset += 4;
  15450. /* Check the data structure version */
  15451. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  15452. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15453. "2620 Config region 23 has bad version\n");
  15454. goto out;
  15455. }
  15456. offset += 4;
  15457. /* Parse TLV entries in the region */
  15458. while (offset < data_size) {
  15459. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  15460. break;
  15461. /*
  15462. * If the TLV is not driver specific TLV or driver id is
  15463. * not linux driver id, skip the record.
  15464. */
  15465. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  15466. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  15467. (rgn23_data[offset + 3] != 0)) {
  15468. offset += rgn23_data[offset + 1] * 4 + 4;
  15469. continue;
  15470. }
  15471. /* Driver found a driver specific TLV in the config region */
  15472. sub_tlv_len = rgn23_data[offset + 1] * 4;
  15473. offset += 4;
  15474. tlv_offset = 0;
  15475. /*
  15476. * Search for configured port state sub-TLV.
  15477. */
  15478. while ((offset < data_size) &&
  15479. (tlv_offset < sub_tlv_len)) {
  15480. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  15481. offset += 4;
  15482. tlv_offset += 4;
  15483. break;
  15484. }
  15485. if (rgn23_data[offset] != PORT_STE_TYPE) {
  15486. offset += rgn23_data[offset + 1] * 4 + 4;
  15487. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  15488. continue;
  15489. }
  15490. /* This HBA contains PORT_STE configured */
  15491. if (!rgn23_data[offset + 2])
  15492. phba->hba_flag |= LINK_DISABLED;
  15493. goto out;
  15494. }
  15495. }
  15496. out:
  15497. kfree(rgn23_data);
  15498. return;
  15499. }
  15500. /**
  15501. * lpfc_wr_object - write an object to the firmware
  15502. * @phba: HBA structure that indicates port to create a queue on.
  15503. * @dmabuf_list: list of dmabufs to write to the port.
  15504. * @size: the total byte value of the objects to write to the port.
  15505. * @offset: the current offset to be used to start the transfer.
  15506. *
  15507. * This routine will create a wr_object mailbox command to send to the port.
  15508. * the mailbox command will be constructed using the dma buffers described in
  15509. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  15510. * BDEs that the imbedded mailbox can support. The @offset variable will be
  15511. * used to indicate the starting offset of the transfer and will also return
  15512. * the offset after the write object mailbox has completed. @size is used to
  15513. * determine the end of the object and whether the eof bit should be set.
  15514. *
  15515. * Return 0 is successful and offset will contain the the new offset to use
  15516. * for the next write.
  15517. * Return negative value for error cases.
  15518. **/
  15519. int
  15520. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  15521. uint32_t size, uint32_t *offset)
  15522. {
  15523. struct lpfc_mbx_wr_object *wr_object;
  15524. LPFC_MBOXQ_t *mbox;
  15525. int rc = 0, i = 0;
  15526. uint32_t shdr_status, shdr_add_status;
  15527. uint32_t mbox_tmo;
  15528. union lpfc_sli4_cfg_shdr *shdr;
  15529. struct lpfc_dmabuf *dmabuf;
  15530. uint32_t written = 0;
  15531. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  15532. if (!mbox)
  15533. return -ENOMEM;
  15534. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  15535. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  15536. sizeof(struct lpfc_mbx_wr_object) -
  15537. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  15538. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  15539. wr_object->u.request.write_offset = *offset;
  15540. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  15541. wr_object->u.request.object_name[0] =
  15542. cpu_to_le32(wr_object->u.request.object_name[0]);
  15543. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  15544. list_for_each_entry(dmabuf, dmabuf_list, list) {
  15545. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  15546. break;
  15547. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  15548. wr_object->u.request.bde[i].addrHigh =
  15549. putPaddrHigh(dmabuf->phys);
  15550. if (written + SLI4_PAGE_SIZE >= size) {
  15551. wr_object->u.request.bde[i].tus.f.bdeSize =
  15552. (size - written);
  15553. written += (size - written);
  15554. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  15555. } else {
  15556. wr_object->u.request.bde[i].tus.f.bdeSize =
  15557. SLI4_PAGE_SIZE;
  15558. written += SLI4_PAGE_SIZE;
  15559. }
  15560. i++;
  15561. }
  15562. wr_object->u.request.bde_count = i;
  15563. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  15564. if (!phba->sli4_hba.intr_enable)
  15565. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  15566. else {
  15567. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  15568. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  15569. }
  15570. /* The IOCTL status is embedded in the mailbox subheader. */
  15571. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  15572. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  15573. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  15574. if (rc != MBX_TIMEOUT)
  15575. mempool_free(mbox, phba->mbox_mem_pool);
  15576. if (shdr_status || shdr_add_status || rc) {
  15577. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15578. "3025 Write Object mailbox failed with "
  15579. "status x%x add_status x%x, mbx status x%x\n",
  15580. shdr_status, shdr_add_status, rc);
  15581. rc = -ENXIO;
  15582. } else
  15583. *offset += wr_object->u.response.actual_write_length;
  15584. return rc;
  15585. }
  15586. /**
  15587. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  15588. * @vport: pointer to vport data structure.
  15589. *
  15590. * This function iterate through the mailboxq and clean up all REG_LOGIN
  15591. * and REG_VPI mailbox commands associated with the vport. This function
  15592. * is called when driver want to restart discovery of the vport due to
  15593. * a Clear Virtual Link event.
  15594. **/
  15595. void
  15596. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  15597. {
  15598. struct lpfc_hba *phba = vport->phba;
  15599. LPFC_MBOXQ_t *mb, *nextmb;
  15600. struct lpfc_dmabuf *mp;
  15601. struct lpfc_nodelist *ndlp;
  15602. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  15603. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  15604. LIST_HEAD(mbox_cmd_list);
  15605. uint8_t restart_loop;
  15606. /* Clean up internally queued mailbox commands with the vport */
  15607. spin_lock_irq(&phba->hbalock);
  15608. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  15609. if (mb->vport != vport)
  15610. continue;
  15611. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  15612. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  15613. continue;
  15614. list_del(&mb->list);
  15615. list_add_tail(&mb->list, &mbox_cmd_list);
  15616. }
  15617. /* Clean up active mailbox command with the vport */
  15618. mb = phba->sli.mbox_active;
  15619. if (mb && (mb->vport == vport)) {
  15620. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  15621. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  15622. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  15623. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  15624. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  15625. /* Put reference count for delayed processing */
  15626. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  15627. /* Unregister the RPI when mailbox complete */
  15628. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  15629. }
  15630. }
  15631. /* Cleanup any mailbox completions which are not yet processed */
  15632. do {
  15633. restart_loop = 0;
  15634. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  15635. /*
  15636. * If this mailox is already processed or it is
  15637. * for another vport ignore it.
  15638. */
  15639. if ((mb->vport != vport) ||
  15640. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  15641. continue;
  15642. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  15643. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  15644. continue;
  15645. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  15646. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  15647. ndlp = (struct lpfc_nodelist *)mb->context2;
  15648. /* Unregister the RPI when mailbox complete */
  15649. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  15650. restart_loop = 1;
  15651. spin_unlock_irq(&phba->hbalock);
  15652. spin_lock(shost->host_lock);
  15653. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  15654. spin_unlock(shost->host_lock);
  15655. spin_lock_irq(&phba->hbalock);
  15656. break;
  15657. }
  15658. }
  15659. } while (restart_loop);
  15660. spin_unlock_irq(&phba->hbalock);
  15661. /* Release the cleaned-up mailbox commands */
  15662. while (!list_empty(&mbox_cmd_list)) {
  15663. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  15664. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  15665. mp = (struct lpfc_dmabuf *) (mb->context1);
  15666. if (mp) {
  15667. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  15668. kfree(mp);
  15669. }
  15670. ndlp = (struct lpfc_nodelist *) mb->context2;
  15671. mb->context2 = NULL;
  15672. if (ndlp) {
  15673. spin_lock(shost->host_lock);
  15674. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  15675. spin_unlock(shost->host_lock);
  15676. lpfc_nlp_put(ndlp);
  15677. }
  15678. }
  15679. mempool_free(mb, phba->mbox_mem_pool);
  15680. }
  15681. /* Release the ndlp with the cleaned-up active mailbox command */
  15682. if (act_mbx_ndlp) {
  15683. spin_lock(shost->host_lock);
  15684. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  15685. spin_unlock(shost->host_lock);
  15686. lpfc_nlp_put(act_mbx_ndlp);
  15687. }
  15688. }
  15689. /**
  15690. * lpfc_drain_txq - Drain the txq
  15691. * @phba: Pointer to HBA context object.
  15692. *
  15693. * This function attempt to submit IOCBs on the txq
  15694. * to the adapter. For SLI4 adapters, the txq contains
  15695. * ELS IOCBs that have been deferred because the there
  15696. * are no SGLs. This congestion can occur with large
  15697. * vport counts during node discovery.
  15698. **/
  15699. uint32_t
  15700. lpfc_drain_txq(struct lpfc_hba *phba)
  15701. {
  15702. LIST_HEAD(completions);
  15703. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  15704. struct lpfc_iocbq *piocbq = NULL;
  15705. unsigned long iflags = 0;
  15706. char *fail_msg = NULL;
  15707. struct lpfc_sglq *sglq;
  15708. union lpfc_wqe wqe;
  15709. uint32_t txq_cnt = 0;
  15710. spin_lock_irqsave(&pring->ring_lock, iflags);
  15711. list_for_each_entry(piocbq, &pring->txq, list) {
  15712. txq_cnt++;
  15713. }
  15714. if (txq_cnt > pring->txq_max)
  15715. pring->txq_max = txq_cnt;
  15716. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15717. while (!list_empty(&pring->txq)) {
  15718. spin_lock_irqsave(&pring->ring_lock, iflags);
  15719. piocbq = lpfc_sli_ringtx_get(phba, pring);
  15720. if (!piocbq) {
  15721. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15722. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15723. "2823 txq empty and txq_cnt is %d\n ",
  15724. txq_cnt);
  15725. break;
  15726. }
  15727. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  15728. if (!sglq) {
  15729. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  15730. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15731. break;
  15732. }
  15733. txq_cnt--;
  15734. /* The xri and iocb resources secured,
  15735. * attempt to issue request
  15736. */
  15737. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  15738. piocbq->sli4_xritag = sglq->sli4_xritag;
  15739. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  15740. fail_msg = "to convert bpl to sgl";
  15741. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  15742. fail_msg = "to convert iocb to wqe";
  15743. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  15744. fail_msg = " - Wq is full";
  15745. else
  15746. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  15747. if (fail_msg) {
  15748. /* Failed means we can't issue and need to cancel */
  15749. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15750. "2822 IOCB failed %s iotag 0x%x "
  15751. "xri 0x%x\n",
  15752. fail_msg,
  15753. piocbq->iotag, piocbq->sli4_xritag);
  15754. list_add_tail(&piocbq->list, &completions);
  15755. }
  15756. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15757. }
  15758. /* Cancel all the IOCBs that cannot be issued */
  15759. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  15760. IOERR_SLI_ABORTED);
  15761. return txq_cnt;
  15762. }