lpfc_sli.c 520 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2014 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/fc/fc_fs.h>
  32. #include <linux/aer.h>
  33. #include "lpfc_hw4.h"
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_nl.h"
  38. #include "lpfc_disc.h"
  39. #include "lpfc_scsi.h"
  40. #include "lpfc.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_compat.h"
  44. #include "lpfc_debugfs.h"
  45. #include "lpfc_vport.h"
  46. /* There are only four IOCB completion types. */
  47. typedef enum _lpfc_iocb_type {
  48. LPFC_UNKNOWN_IOCB,
  49. LPFC_UNSOL_IOCB,
  50. LPFC_SOL_IOCB,
  51. LPFC_ABORT_IOCB
  52. } lpfc_iocb_type;
  53. /* Provide function prototypes local to this module. */
  54. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint32_t);
  56. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  57. uint8_t *, uint32_t *);
  58. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  59. struct lpfc_iocbq *);
  60. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  61. struct hbq_dmabuf *);
  62. static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
  63. struct lpfc_cqe *);
  64. static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
  65. int);
  66. static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
  67. uint32_t);
  68. static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
  69. static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
  70. static IOCB_t *
  71. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  72. {
  73. return &iocbq->iocb;
  74. }
  75. /**
  76. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  77. * @q: The Work Queue to operate on.
  78. * @wqe: The work Queue Entry to put on the Work queue.
  79. *
  80. * This routine will copy the contents of @wqe to the next available entry on
  81. * the @q. This function will then ring the Work Queue Doorbell to signal the
  82. * HBA to start processing the Work Queue Entry. This function returns 0 if
  83. * successful. If no entries are available on @q then this function will return
  84. * -ENOMEM.
  85. * The caller is expected to hold the hbalock when calling this routine.
  86. **/
  87. static uint32_t
  88. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  89. {
  90. union lpfc_wqe *temp_wqe;
  91. struct lpfc_register doorbell;
  92. uint32_t host_index;
  93. uint32_t idx;
  94. /* sanity check on queue memory */
  95. if (unlikely(!q))
  96. return -ENOMEM;
  97. temp_wqe = q->qe[q->host_index].wqe;
  98. /* If the host has not yet processed the next entry then we are done */
  99. idx = ((q->host_index + 1) % q->entry_count);
  100. if (idx == q->hba_index) {
  101. q->WQ_overflow++;
  102. return -ENOMEM;
  103. }
  104. q->WQ_posted++;
  105. /* set consumption flag every once in a while */
  106. if (!((q->host_index + 1) % q->entry_repost))
  107. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  108. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  109. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  110. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  111. /* Update the host index before invoking device */
  112. host_index = q->host_index;
  113. q->host_index = idx;
  114. /* Ring Doorbell */
  115. doorbell.word0 = 0;
  116. if (q->db_format == LPFC_DB_LIST_FORMAT) {
  117. bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
  118. bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
  119. bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
  120. } else if (q->db_format == LPFC_DB_RING_FORMAT) {
  121. bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
  122. bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
  123. } else {
  124. return -EINVAL;
  125. }
  126. writel(doorbell.word0, q->db_regaddr);
  127. return 0;
  128. }
  129. /**
  130. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  131. * @q: The Work Queue to operate on.
  132. * @index: The index to advance the hba index to.
  133. *
  134. * This routine will update the HBA index of a queue to reflect consumption of
  135. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  136. * an entry the host calls this function to update the queue's internal
  137. * pointers. This routine returns the number of entries that were consumed by
  138. * the HBA.
  139. **/
  140. static uint32_t
  141. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  142. {
  143. uint32_t released = 0;
  144. /* sanity check on queue memory */
  145. if (unlikely(!q))
  146. return 0;
  147. if (q->hba_index == index)
  148. return 0;
  149. do {
  150. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  151. released++;
  152. } while (q->hba_index != index);
  153. return released;
  154. }
  155. /**
  156. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  157. * @q: The Mailbox Queue to operate on.
  158. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  159. *
  160. * This routine will copy the contents of @mqe to the next available entry on
  161. * the @q. This function will then ring the Work Queue Doorbell to signal the
  162. * HBA to start processing the Work Queue Entry. This function returns 0 if
  163. * successful. If no entries are available on @q then this function will return
  164. * -ENOMEM.
  165. * The caller is expected to hold the hbalock when calling this routine.
  166. **/
  167. static uint32_t
  168. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  169. {
  170. struct lpfc_mqe *temp_mqe;
  171. struct lpfc_register doorbell;
  172. /* sanity check on queue memory */
  173. if (unlikely(!q))
  174. return -ENOMEM;
  175. temp_mqe = q->qe[q->host_index].mqe;
  176. /* If the host has not yet processed the next entry then we are done */
  177. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  178. return -ENOMEM;
  179. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  180. /* Save off the mailbox pointer for completion */
  181. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  182. /* Update the host index before invoking device */
  183. q->host_index = ((q->host_index + 1) % q->entry_count);
  184. /* Ring Doorbell */
  185. doorbell.word0 = 0;
  186. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  187. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  188. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  189. return 0;
  190. }
  191. /**
  192. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  193. * @q: The Mailbox Queue to operate on.
  194. *
  195. * This routine will update the HBA index of a queue to reflect consumption of
  196. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  197. * an entry the host calls this function to update the queue's internal
  198. * pointers. This routine returns the number of entries that were consumed by
  199. * the HBA.
  200. **/
  201. static uint32_t
  202. lpfc_sli4_mq_release(struct lpfc_queue *q)
  203. {
  204. /* sanity check on queue memory */
  205. if (unlikely(!q))
  206. return 0;
  207. /* Clear the mailbox pointer for completion */
  208. q->phba->mbox = NULL;
  209. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  210. return 1;
  211. }
  212. /**
  213. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  214. * @q: The Event Queue to get the first valid EQE from
  215. *
  216. * This routine will get the first valid Event Queue Entry from @q, update
  217. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  218. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  219. * processed, but not popped back to the HBA then this routine will return NULL.
  220. **/
  221. static struct lpfc_eqe *
  222. lpfc_sli4_eq_get(struct lpfc_queue *q)
  223. {
  224. struct lpfc_eqe *eqe;
  225. uint32_t idx;
  226. /* sanity check on queue memory */
  227. if (unlikely(!q))
  228. return NULL;
  229. eqe = q->qe[q->hba_index].eqe;
  230. /* If the next EQE is not valid then we are done */
  231. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  232. return NULL;
  233. /* If the host has not yet processed the next entry then we are done */
  234. idx = ((q->hba_index + 1) % q->entry_count);
  235. if (idx == q->host_index)
  236. return NULL;
  237. q->hba_index = idx;
  238. /*
  239. * insert barrier for instruction interlock : data from the hardware
  240. * must have the valid bit checked before it can be copied and acted
  241. * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
  242. * instructions allowing action on content before valid bit checked,
  243. * add barrier here as well. May not be needed as "content" is a
  244. * single 32-bit entity here (vs multi word structure for cq's).
  245. */
  246. mb();
  247. return eqe;
  248. }
  249. /**
  250. * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
  251. * @q: The Event Queue to disable interrupts
  252. *
  253. **/
  254. static inline void
  255. lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
  256. {
  257. struct lpfc_register doorbell;
  258. doorbell.word0 = 0;
  259. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  260. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  261. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  262. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  263. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  264. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  265. }
  266. /**
  267. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  268. * @q: The Event Queue that the host has completed processing for.
  269. * @arm: Indicates whether the host wants to arms this CQ.
  270. *
  271. * This routine will mark all Event Queue Entries on @q, from the last
  272. * known completed entry to the last entry that was processed, as completed
  273. * by clearing the valid bit for each completion queue entry. Then it will
  274. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  275. * The internal host index in the @q will be updated by this routine to indicate
  276. * that the host has finished processing the entries. The @arm parameter
  277. * indicates that the queue should be rearmed when ringing the doorbell.
  278. *
  279. * This function will return the number of EQEs that were popped.
  280. **/
  281. uint32_t
  282. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  283. {
  284. uint32_t released = 0;
  285. struct lpfc_eqe *temp_eqe;
  286. struct lpfc_register doorbell;
  287. /* sanity check on queue memory */
  288. if (unlikely(!q))
  289. return 0;
  290. /* while there are valid entries */
  291. while (q->hba_index != q->host_index) {
  292. temp_eqe = q->qe[q->host_index].eqe;
  293. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  294. released++;
  295. q->host_index = ((q->host_index + 1) % q->entry_count);
  296. }
  297. if (unlikely(released == 0 && !arm))
  298. return 0;
  299. /* ring doorbell for number popped */
  300. doorbell.word0 = 0;
  301. if (arm) {
  302. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  303. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  304. }
  305. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  306. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  307. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  308. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  309. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  310. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  311. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  312. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  313. readl(q->phba->sli4_hba.EQCQDBregaddr);
  314. return released;
  315. }
  316. /**
  317. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  318. * @q: The Completion Queue to get the first valid CQE from
  319. *
  320. * This routine will get the first valid Completion Queue Entry from @q, update
  321. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  322. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  323. * processed, but not popped back to the HBA then this routine will return NULL.
  324. **/
  325. static struct lpfc_cqe *
  326. lpfc_sli4_cq_get(struct lpfc_queue *q)
  327. {
  328. struct lpfc_cqe *cqe;
  329. uint32_t idx;
  330. /* sanity check on queue memory */
  331. if (unlikely(!q))
  332. return NULL;
  333. /* If the next CQE is not valid then we are done */
  334. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  335. return NULL;
  336. /* If the host has not yet processed the next entry then we are done */
  337. idx = ((q->hba_index + 1) % q->entry_count);
  338. if (idx == q->host_index)
  339. return NULL;
  340. cqe = q->qe[q->hba_index].cqe;
  341. q->hba_index = idx;
  342. /*
  343. * insert barrier for instruction interlock : data from the hardware
  344. * must have the valid bit checked before it can be copied and acted
  345. * upon. Speculative instructions were allowing a bcopy at the start
  346. * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
  347. * after our return, to copy data before the valid bit check above
  348. * was done. As such, some of the copied data was stale. The barrier
  349. * ensures the check is before any data is copied.
  350. */
  351. mb();
  352. return cqe;
  353. }
  354. /**
  355. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  356. * @q: The Completion Queue that the host has completed processing for.
  357. * @arm: Indicates whether the host wants to arms this CQ.
  358. *
  359. * This routine will mark all Completion queue entries on @q, from the last
  360. * known completed entry to the last entry that was processed, as completed
  361. * by clearing the valid bit for each completion queue entry. Then it will
  362. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  363. * The internal host index in the @q will be updated by this routine to indicate
  364. * that the host has finished processing the entries. The @arm parameter
  365. * indicates that the queue should be rearmed when ringing the doorbell.
  366. *
  367. * This function will return the number of CQEs that were released.
  368. **/
  369. uint32_t
  370. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  371. {
  372. uint32_t released = 0;
  373. struct lpfc_cqe *temp_qe;
  374. struct lpfc_register doorbell;
  375. /* sanity check on queue memory */
  376. if (unlikely(!q))
  377. return 0;
  378. /* while there are valid entries */
  379. while (q->hba_index != q->host_index) {
  380. temp_qe = q->qe[q->host_index].cqe;
  381. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  382. released++;
  383. q->host_index = ((q->host_index + 1) % q->entry_count);
  384. }
  385. if (unlikely(released == 0 && !arm))
  386. return 0;
  387. /* ring doorbell for number popped */
  388. doorbell.word0 = 0;
  389. if (arm)
  390. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  391. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  392. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  393. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  394. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  395. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  396. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  397. return released;
  398. }
  399. /**
  400. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  401. * @q: The Header Receive Queue to operate on.
  402. * @wqe: The Receive Queue Entry to put on the Receive queue.
  403. *
  404. * This routine will copy the contents of @wqe to the next available entry on
  405. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  406. * HBA to start processing the Receive Queue Entry. This function returns the
  407. * index that the rqe was copied to if successful. If no entries are available
  408. * on @q then this function will return -ENOMEM.
  409. * The caller is expected to hold the hbalock when calling this routine.
  410. **/
  411. static int
  412. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  413. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  414. {
  415. struct lpfc_rqe *temp_hrqe;
  416. struct lpfc_rqe *temp_drqe;
  417. struct lpfc_register doorbell;
  418. int put_index;
  419. /* sanity check on queue memory */
  420. if (unlikely(!hq) || unlikely(!dq))
  421. return -ENOMEM;
  422. put_index = hq->host_index;
  423. temp_hrqe = hq->qe[hq->host_index].rqe;
  424. temp_drqe = dq->qe[dq->host_index].rqe;
  425. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  426. return -EINVAL;
  427. if (hq->host_index != dq->host_index)
  428. return -EINVAL;
  429. /* If the host has not yet processed the next entry then we are done */
  430. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  431. return -EBUSY;
  432. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  433. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  434. /* Update the host index to point to the next slot */
  435. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  436. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  437. /* Ring The Header Receive Queue Doorbell */
  438. if (!(hq->host_index % hq->entry_repost)) {
  439. doorbell.word0 = 0;
  440. if (hq->db_format == LPFC_DB_RING_FORMAT) {
  441. bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
  442. hq->entry_repost);
  443. bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
  444. } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
  445. bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
  446. hq->entry_repost);
  447. bf_set(lpfc_rq_db_list_fm_index, &doorbell,
  448. hq->host_index);
  449. bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
  450. } else {
  451. return -EINVAL;
  452. }
  453. writel(doorbell.word0, hq->db_regaddr);
  454. }
  455. return put_index;
  456. }
  457. /**
  458. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  459. * @q: The Header Receive Queue to operate on.
  460. *
  461. * This routine will update the HBA index of a queue to reflect consumption of
  462. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  463. * consumed an entry the host calls this function to update the queue's
  464. * internal pointers. This routine returns the number of entries that were
  465. * consumed by the HBA.
  466. **/
  467. static uint32_t
  468. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  469. {
  470. /* sanity check on queue memory */
  471. if (unlikely(!hq) || unlikely(!dq))
  472. return 0;
  473. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  474. return 0;
  475. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  476. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  477. return 1;
  478. }
  479. /**
  480. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  481. * @phba: Pointer to HBA context object.
  482. * @pring: Pointer to driver SLI ring object.
  483. *
  484. * This function returns pointer to next command iocb entry
  485. * in the command ring. The caller must hold hbalock to prevent
  486. * other threads consume the next command iocb.
  487. * SLI-2/SLI-3 provide different sized iocbs.
  488. **/
  489. static inline IOCB_t *
  490. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  491. {
  492. return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
  493. pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
  494. }
  495. /**
  496. * lpfc_resp_iocb - Get next response iocb entry in the ring
  497. * @phba: Pointer to HBA context object.
  498. * @pring: Pointer to driver SLI ring object.
  499. *
  500. * This function returns pointer to next response iocb entry
  501. * in the response ring. The caller must hold hbalock to make sure
  502. * that no other thread consume the next response iocb.
  503. * SLI-2/SLI-3 provide different sized iocbs.
  504. **/
  505. static inline IOCB_t *
  506. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  507. {
  508. return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
  509. pring->sli.sli3.rspidx * phba->iocb_rsp_size);
  510. }
  511. /**
  512. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  513. * @phba: Pointer to HBA context object.
  514. *
  515. * This function is called with hbalock held. This function
  516. * allocates a new driver iocb object from the iocb pool. If the
  517. * allocation is successful, it returns pointer to the newly
  518. * allocated iocb object else it returns NULL.
  519. **/
  520. struct lpfc_iocbq *
  521. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  522. {
  523. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  524. struct lpfc_iocbq * iocbq = NULL;
  525. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  526. if (iocbq)
  527. phba->iocb_cnt++;
  528. if (phba->iocb_cnt > phba->iocb_max)
  529. phba->iocb_max = phba->iocb_cnt;
  530. return iocbq;
  531. }
  532. /**
  533. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  534. * @phba: Pointer to HBA context object.
  535. * @xritag: XRI value.
  536. *
  537. * This function clears the sglq pointer from the array of acive
  538. * sglq's. The xritag that is passed in is used to index into the
  539. * array. Before the xritag can be used it needs to be adjusted
  540. * by subtracting the xribase.
  541. *
  542. * Returns sglq ponter = success, NULL = Failure.
  543. **/
  544. static struct lpfc_sglq *
  545. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  546. {
  547. struct lpfc_sglq *sglq;
  548. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  549. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  550. return sglq;
  551. }
  552. /**
  553. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  554. * @phba: Pointer to HBA context object.
  555. * @xritag: XRI value.
  556. *
  557. * This function returns the sglq pointer from the array of acive
  558. * sglq's. The xritag that is passed in is used to index into the
  559. * array. Before the xritag can be used it needs to be adjusted
  560. * by subtracting the xribase.
  561. *
  562. * Returns sglq ponter = success, NULL = Failure.
  563. **/
  564. struct lpfc_sglq *
  565. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  566. {
  567. struct lpfc_sglq *sglq;
  568. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  569. return sglq;
  570. }
  571. /**
  572. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  573. * @phba: Pointer to HBA context object.
  574. * @xritag: xri used in this exchange.
  575. * @rrq: The RRQ to be cleared.
  576. *
  577. **/
  578. void
  579. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  580. uint16_t xritag,
  581. struct lpfc_node_rrq *rrq)
  582. {
  583. struct lpfc_nodelist *ndlp = NULL;
  584. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  585. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  586. /* The target DID could have been swapped (cable swap)
  587. * we should use the ndlp from the findnode if it is
  588. * available.
  589. */
  590. if ((!ndlp) && rrq->ndlp)
  591. ndlp = rrq->ndlp;
  592. if (!ndlp)
  593. goto out;
  594. if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
  595. rrq->send_rrq = 0;
  596. rrq->xritag = 0;
  597. rrq->rrq_stop_time = 0;
  598. }
  599. out:
  600. mempool_free(rrq, phba->rrq_pool);
  601. }
  602. /**
  603. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  604. * @phba: Pointer to HBA context object.
  605. *
  606. * This function is called with hbalock held. This function
  607. * Checks if stop_time (ratov from setting rrq active) has
  608. * been reached, if it has and the send_rrq flag is set then
  609. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  610. * then it will just call the routine to clear the rrq and
  611. * free the rrq resource.
  612. * The timer is set to the next rrq that is going to expire before
  613. * leaving the routine.
  614. *
  615. **/
  616. void
  617. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  618. {
  619. struct lpfc_node_rrq *rrq;
  620. struct lpfc_node_rrq *nextrrq;
  621. unsigned long next_time;
  622. unsigned long iflags;
  623. LIST_HEAD(send_rrq);
  624. spin_lock_irqsave(&phba->hbalock, iflags);
  625. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  626. next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
  627. list_for_each_entry_safe(rrq, nextrrq,
  628. &phba->active_rrq_list, list) {
  629. if (time_after(jiffies, rrq->rrq_stop_time))
  630. list_move(&rrq->list, &send_rrq);
  631. else if (time_before(rrq->rrq_stop_time, next_time))
  632. next_time = rrq->rrq_stop_time;
  633. }
  634. spin_unlock_irqrestore(&phba->hbalock, iflags);
  635. if ((!list_empty(&phba->active_rrq_list)) &&
  636. (!(phba->pport->load_flag & FC_UNLOADING)))
  637. mod_timer(&phba->rrq_tmr, next_time);
  638. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  639. list_del(&rrq->list);
  640. if (!rrq->send_rrq)
  641. /* this call will free the rrq */
  642. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  643. else if (lpfc_send_rrq(phba, rrq)) {
  644. /* if we send the rrq then the completion handler
  645. * will clear the bit in the xribitmap.
  646. */
  647. lpfc_clr_rrq_active(phba, rrq->xritag,
  648. rrq);
  649. }
  650. }
  651. }
  652. /**
  653. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  654. * @vport: Pointer to vport context object.
  655. * @xri: The xri used in the exchange.
  656. * @did: The targets DID for this exchange.
  657. *
  658. * returns NULL = rrq not found in the phba->active_rrq_list.
  659. * rrq = rrq for this xri and target.
  660. **/
  661. struct lpfc_node_rrq *
  662. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  663. {
  664. struct lpfc_hba *phba = vport->phba;
  665. struct lpfc_node_rrq *rrq;
  666. struct lpfc_node_rrq *nextrrq;
  667. unsigned long iflags;
  668. if (phba->sli_rev != LPFC_SLI_REV4)
  669. return NULL;
  670. spin_lock_irqsave(&phba->hbalock, iflags);
  671. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  672. if (rrq->vport == vport && rrq->xritag == xri &&
  673. rrq->nlp_DID == did){
  674. list_del(&rrq->list);
  675. spin_unlock_irqrestore(&phba->hbalock, iflags);
  676. return rrq;
  677. }
  678. }
  679. spin_unlock_irqrestore(&phba->hbalock, iflags);
  680. return NULL;
  681. }
  682. /**
  683. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  684. * @vport: Pointer to vport context object.
  685. * @ndlp: Pointer to the lpfc_node_list structure.
  686. * If ndlp is NULL Remove all active RRQs for this vport from the
  687. * phba->active_rrq_list and clear the rrq.
  688. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  689. **/
  690. void
  691. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  692. {
  693. struct lpfc_hba *phba = vport->phba;
  694. struct lpfc_node_rrq *rrq;
  695. struct lpfc_node_rrq *nextrrq;
  696. unsigned long iflags;
  697. LIST_HEAD(rrq_list);
  698. if (phba->sli_rev != LPFC_SLI_REV4)
  699. return;
  700. if (!ndlp) {
  701. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  702. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  703. }
  704. spin_lock_irqsave(&phba->hbalock, iflags);
  705. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  706. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  707. list_move(&rrq->list, &rrq_list);
  708. spin_unlock_irqrestore(&phba->hbalock, iflags);
  709. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  710. list_del(&rrq->list);
  711. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  712. }
  713. }
  714. /**
  715. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  716. * @phba: Pointer to HBA context object.
  717. * @ndlp: Targets nodelist pointer for this exchange.
  718. * @xritag the xri in the bitmap to test.
  719. *
  720. * This function is called with hbalock held. This function
  721. * returns 0 = rrq not active for this xri
  722. * 1 = rrq is valid for this xri.
  723. **/
  724. int
  725. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  726. uint16_t xritag)
  727. {
  728. if (!ndlp)
  729. return 0;
  730. if (!ndlp->active_rrqs_xri_bitmap)
  731. return 0;
  732. if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
  733. return 1;
  734. else
  735. return 0;
  736. }
  737. /**
  738. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  739. * @phba: Pointer to HBA context object.
  740. * @ndlp: nodelist pointer for this target.
  741. * @xritag: xri used in this exchange.
  742. * @rxid: Remote Exchange ID.
  743. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  744. *
  745. * This function takes the hbalock.
  746. * The active bit is always set in the active rrq xri_bitmap even
  747. * if there is no slot avaiable for the other rrq information.
  748. *
  749. * returns 0 rrq actived for this xri
  750. * < 0 No memory or invalid ndlp.
  751. **/
  752. int
  753. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  754. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  755. {
  756. unsigned long iflags;
  757. struct lpfc_node_rrq *rrq;
  758. int empty;
  759. if (!ndlp)
  760. return -EINVAL;
  761. if (!phba->cfg_enable_rrq)
  762. return -EINVAL;
  763. spin_lock_irqsave(&phba->hbalock, iflags);
  764. if (phba->pport->load_flag & FC_UNLOADING) {
  765. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  766. goto out;
  767. }
  768. /*
  769. * set the active bit even if there is no mem available.
  770. */
  771. if (NLP_CHK_FREE_REQ(ndlp))
  772. goto out;
  773. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  774. goto out;
  775. if (!ndlp->active_rrqs_xri_bitmap)
  776. goto out;
  777. if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
  778. goto out;
  779. spin_unlock_irqrestore(&phba->hbalock, iflags);
  780. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  781. if (!rrq) {
  782. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  783. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  784. " DID:0x%x Send:%d\n",
  785. xritag, rxid, ndlp->nlp_DID, send_rrq);
  786. return -EINVAL;
  787. }
  788. if (phba->cfg_enable_rrq == 1)
  789. rrq->send_rrq = send_rrq;
  790. else
  791. rrq->send_rrq = 0;
  792. rrq->xritag = xritag;
  793. rrq->rrq_stop_time = jiffies +
  794. msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
  795. rrq->ndlp = ndlp;
  796. rrq->nlp_DID = ndlp->nlp_DID;
  797. rrq->vport = ndlp->vport;
  798. rrq->rxid = rxid;
  799. spin_lock_irqsave(&phba->hbalock, iflags);
  800. empty = list_empty(&phba->active_rrq_list);
  801. list_add_tail(&rrq->list, &phba->active_rrq_list);
  802. phba->hba_flag |= HBA_RRQ_ACTIVE;
  803. if (empty)
  804. lpfc_worker_wake_up(phba);
  805. spin_unlock_irqrestore(&phba->hbalock, iflags);
  806. return 0;
  807. out:
  808. spin_unlock_irqrestore(&phba->hbalock, iflags);
  809. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  810. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  811. " DID:0x%x Send:%d\n",
  812. xritag, rxid, ndlp->nlp_DID, send_rrq);
  813. return -EINVAL;
  814. }
  815. /**
  816. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  817. * @phba: Pointer to HBA context object.
  818. * @piocb: Pointer to the iocbq.
  819. *
  820. * This function is called with the ring lock held. This function
  821. * gets a new driver sglq object from the sglq list. If the
  822. * list is not empty then it is successful, it returns pointer to the newly
  823. * allocated sglq object else it returns NULL.
  824. **/
  825. static struct lpfc_sglq *
  826. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  827. {
  828. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  829. struct lpfc_sglq *sglq = NULL;
  830. struct lpfc_sglq *start_sglq = NULL;
  831. struct lpfc_scsi_buf *lpfc_cmd;
  832. struct lpfc_nodelist *ndlp;
  833. int found = 0;
  834. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  835. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  836. ndlp = lpfc_cmd->rdata->pnode;
  837. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  838. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  839. ndlp = piocbq->context_un.ndlp;
  840. else if (piocbq->iocb_flag & LPFC_IO_LIBDFC)
  841. ndlp = piocbq->context_un.ndlp;
  842. else
  843. ndlp = piocbq->context1;
  844. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  845. start_sglq = sglq;
  846. while (!found) {
  847. if (!sglq)
  848. return NULL;
  849. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
  850. /* This xri has an rrq outstanding for this DID.
  851. * put it back in the list and get another xri.
  852. */
  853. list_add_tail(&sglq->list, lpfc_sgl_list);
  854. sglq = NULL;
  855. list_remove_head(lpfc_sgl_list, sglq,
  856. struct lpfc_sglq, list);
  857. if (sglq == start_sglq) {
  858. sglq = NULL;
  859. break;
  860. } else
  861. continue;
  862. }
  863. sglq->ndlp = ndlp;
  864. found = 1;
  865. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  866. sglq->state = SGL_ALLOCATED;
  867. }
  868. return sglq;
  869. }
  870. /**
  871. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  872. * @phba: Pointer to HBA context object.
  873. *
  874. * This function is called with no lock held. This function
  875. * allocates a new driver iocb object from the iocb pool. If the
  876. * allocation is successful, it returns pointer to the newly
  877. * allocated iocb object else it returns NULL.
  878. **/
  879. struct lpfc_iocbq *
  880. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  881. {
  882. struct lpfc_iocbq * iocbq = NULL;
  883. unsigned long iflags;
  884. spin_lock_irqsave(&phba->hbalock, iflags);
  885. iocbq = __lpfc_sli_get_iocbq(phba);
  886. spin_unlock_irqrestore(&phba->hbalock, iflags);
  887. return iocbq;
  888. }
  889. /**
  890. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  891. * @phba: Pointer to HBA context object.
  892. * @iocbq: Pointer to driver iocb object.
  893. *
  894. * This function is called with hbalock held to release driver
  895. * iocb object to the iocb pool. The iotag in the iocb object
  896. * does not change for each use of the iocb object. This function
  897. * clears all other fields of the iocb object when it is freed.
  898. * The sqlq structure that holds the xritag and phys and virtual
  899. * mappings for the scatter gather list is retrieved from the
  900. * active array of sglq. The get of the sglq pointer also clears
  901. * the entry in the array. If the status of the IO indiactes that
  902. * this IO was aborted then the sglq entry it put on the
  903. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  904. * IO has good status or fails for any other reason then the sglq
  905. * entry is added to the free list (lpfc_sgl_list).
  906. **/
  907. static void
  908. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  909. {
  910. struct lpfc_sglq *sglq;
  911. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  912. unsigned long iflag = 0;
  913. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  914. if (iocbq->sli4_xritag == NO_XRI)
  915. sglq = NULL;
  916. else
  917. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  918. if (sglq) {
  919. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  920. (sglq->state != SGL_XRI_ABORTED)) {
  921. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  922. iflag);
  923. list_add(&sglq->list,
  924. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  925. spin_unlock_irqrestore(
  926. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  927. } else {
  928. spin_lock_irqsave(&pring->ring_lock, iflag);
  929. sglq->state = SGL_FREED;
  930. sglq->ndlp = NULL;
  931. list_add_tail(&sglq->list,
  932. &phba->sli4_hba.lpfc_sgl_list);
  933. spin_unlock_irqrestore(&pring->ring_lock, iflag);
  934. /* Check if TXQ queue needs to be serviced */
  935. if (!list_empty(&pring->txq))
  936. lpfc_worker_wake_up(phba);
  937. }
  938. }
  939. /*
  940. * Clean all volatile data fields, preserve iotag and node struct.
  941. */
  942. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  943. iocbq->sli4_lxritag = NO_XRI;
  944. iocbq->sli4_xritag = NO_XRI;
  945. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  946. }
  947. /**
  948. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  949. * @phba: Pointer to HBA context object.
  950. * @iocbq: Pointer to driver iocb object.
  951. *
  952. * This function is called with hbalock held to release driver
  953. * iocb object to the iocb pool. The iotag in the iocb object
  954. * does not change for each use of the iocb object. This function
  955. * clears all other fields of the iocb object when it is freed.
  956. **/
  957. static void
  958. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  959. {
  960. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  961. /*
  962. * Clean all volatile data fields, preserve iotag and node struct.
  963. */
  964. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  965. iocbq->sli4_xritag = NO_XRI;
  966. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  967. }
  968. /**
  969. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  970. * @phba: Pointer to HBA context object.
  971. * @iocbq: Pointer to driver iocb object.
  972. *
  973. * This function is called with hbalock held to release driver
  974. * iocb object to the iocb pool. The iotag in the iocb object
  975. * does not change for each use of the iocb object. This function
  976. * clears all other fields of the iocb object when it is freed.
  977. **/
  978. static void
  979. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  980. {
  981. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  982. phba->iocb_cnt--;
  983. }
  984. /**
  985. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  986. * @phba: Pointer to HBA context object.
  987. * @iocbq: Pointer to driver iocb object.
  988. *
  989. * This function is called with no lock held to release the iocb to
  990. * iocb pool.
  991. **/
  992. void
  993. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  994. {
  995. unsigned long iflags;
  996. /*
  997. * Clean all volatile data fields, preserve iotag and node struct.
  998. */
  999. spin_lock_irqsave(&phba->hbalock, iflags);
  1000. __lpfc_sli_release_iocbq(phba, iocbq);
  1001. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1002. }
  1003. /**
  1004. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  1005. * @phba: Pointer to HBA context object.
  1006. * @iocblist: List of IOCBs.
  1007. * @ulpstatus: ULP status in IOCB command field.
  1008. * @ulpWord4: ULP word-4 in IOCB command field.
  1009. *
  1010. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  1011. * on the list by invoking the complete callback function associated with the
  1012. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  1013. * fields.
  1014. **/
  1015. void
  1016. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  1017. uint32_t ulpstatus, uint32_t ulpWord4)
  1018. {
  1019. struct lpfc_iocbq *piocb;
  1020. while (!list_empty(iocblist)) {
  1021. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  1022. if (!piocb->iocb_cmpl)
  1023. lpfc_sli_release_iocbq(phba, piocb);
  1024. else {
  1025. piocb->iocb.ulpStatus = ulpstatus;
  1026. piocb->iocb.un.ulpWord[4] = ulpWord4;
  1027. (piocb->iocb_cmpl) (phba, piocb, piocb);
  1028. }
  1029. }
  1030. return;
  1031. }
  1032. /**
  1033. * lpfc_sli_iocb_cmd_type - Get the iocb type
  1034. * @iocb_cmnd: iocb command code.
  1035. *
  1036. * This function is called by ring event handler function to get the iocb type.
  1037. * This function translates the iocb command to an iocb command type used to
  1038. * decide the final disposition of each completed IOCB.
  1039. * The function returns
  1040. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  1041. * LPFC_SOL_IOCB if it is a solicited iocb completion
  1042. * LPFC_ABORT_IOCB if it is an abort iocb
  1043. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  1044. *
  1045. * The caller is not required to hold any lock.
  1046. **/
  1047. static lpfc_iocb_type
  1048. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1049. {
  1050. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1051. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1052. return 0;
  1053. switch (iocb_cmnd) {
  1054. case CMD_XMIT_SEQUENCE_CR:
  1055. case CMD_XMIT_SEQUENCE_CX:
  1056. case CMD_XMIT_BCAST_CN:
  1057. case CMD_XMIT_BCAST_CX:
  1058. case CMD_ELS_REQUEST_CR:
  1059. case CMD_ELS_REQUEST_CX:
  1060. case CMD_CREATE_XRI_CR:
  1061. case CMD_CREATE_XRI_CX:
  1062. case CMD_GET_RPI_CN:
  1063. case CMD_XMIT_ELS_RSP_CX:
  1064. case CMD_GET_RPI_CR:
  1065. case CMD_FCP_IWRITE_CR:
  1066. case CMD_FCP_IWRITE_CX:
  1067. case CMD_FCP_IREAD_CR:
  1068. case CMD_FCP_IREAD_CX:
  1069. case CMD_FCP_ICMND_CR:
  1070. case CMD_FCP_ICMND_CX:
  1071. case CMD_FCP_TSEND_CX:
  1072. case CMD_FCP_TRSP_CX:
  1073. case CMD_FCP_TRECEIVE_CX:
  1074. case CMD_FCP_AUTO_TRSP_CX:
  1075. case CMD_ADAPTER_MSG:
  1076. case CMD_ADAPTER_DUMP:
  1077. case CMD_XMIT_SEQUENCE64_CR:
  1078. case CMD_XMIT_SEQUENCE64_CX:
  1079. case CMD_XMIT_BCAST64_CN:
  1080. case CMD_XMIT_BCAST64_CX:
  1081. case CMD_ELS_REQUEST64_CR:
  1082. case CMD_ELS_REQUEST64_CX:
  1083. case CMD_FCP_IWRITE64_CR:
  1084. case CMD_FCP_IWRITE64_CX:
  1085. case CMD_FCP_IREAD64_CR:
  1086. case CMD_FCP_IREAD64_CX:
  1087. case CMD_FCP_ICMND64_CR:
  1088. case CMD_FCP_ICMND64_CX:
  1089. case CMD_FCP_TSEND64_CX:
  1090. case CMD_FCP_TRSP64_CX:
  1091. case CMD_FCP_TRECEIVE64_CX:
  1092. case CMD_GEN_REQUEST64_CR:
  1093. case CMD_GEN_REQUEST64_CX:
  1094. case CMD_XMIT_ELS_RSP64_CX:
  1095. case DSSCMD_IWRITE64_CR:
  1096. case DSSCMD_IWRITE64_CX:
  1097. case DSSCMD_IREAD64_CR:
  1098. case DSSCMD_IREAD64_CX:
  1099. type = LPFC_SOL_IOCB;
  1100. break;
  1101. case CMD_ABORT_XRI_CN:
  1102. case CMD_ABORT_XRI_CX:
  1103. case CMD_CLOSE_XRI_CN:
  1104. case CMD_CLOSE_XRI_CX:
  1105. case CMD_XRI_ABORTED_CX:
  1106. case CMD_ABORT_MXRI64_CN:
  1107. case CMD_XMIT_BLS_RSP64_CX:
  1108. type = LPFC_ABORT_IOCB;
  1109. break;
  1110. case CMD_RCV_SEQUENCE_CX:
  1111. case CMD_RCV_ELS_REQ_CX:
  1112. case CMD_RCV_SEQUENCE64_CX:
  1113. case CMD_RCV_ELS_REQ64_CX:
  1114. case CMD_ASYNC_STATUS:
  1115. case CMD_IOCB_RCV_SEQ64_CX:
  1116. case CMD_IOCB_RCV_ELS64_CX:
  1117. case CMD_IOCB_RCV_CONT64_CX:
  1118. case CMD_IOCB_RET_XRI64_CX:
  1119. type = LPFC_UNSOL_IOCB;
  1120. break;
  1121. case CMD_IOCB_XMIT_MSEQ64_CR:
  1122. case CMD_IOCB_XMIT_MSEQ64_CX:
  1123. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1124. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1125. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1126. case CMD_IOCB_ABORT_EXTENDED_CN:
  1127. case CMD_IOCB_RET_HBQE64_CN:
  1128. case CMD_IOCB_FCP_IBIDIR64_CR:
  1129. case CMD_IOCB_FCP_IBIDIR64_CX:
  1130. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1131. case CMD_IOCB_LOGENTRY_CN:
  1132. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1133. printk("%s - Unhandled SLI-3 Command x%x\n",
  1134. __func__, iocb_cmnd);
  1135. type = LPFC_UNKNOWN_IOCB;
  1136. break;
  1137. default:
  1138. type = LPFC_UNKNOWN_IOCB;
  1139. break;
  1140. }
  1141. return type;
  1142. }
  1143. /**
  1144. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1145. * @phba: Pointer to HBA context object.
  1146. *
  1147. * This function is called from SLI initialization code
  1148. * to configure every ring of the HBA's SLI interface. The
  1149. * caller is not required to hold any lock. This function issues
  1150. * a config_ring mailbox command for each ring.
  1151. * This function returns zero if successful else returns a negative
  1152. * error code.
  1153. **/
  1154. static int
  1155. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1156. {
  1157. struct lpfc_sli *psli = &phba->sli;
  1158. LPFC_MBOXQ_t *pmb;
  1159. MAILBOX_t *pmbox;
  1160. int i, rc, ret = 0;
  1161. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1162. if (!pmb)
  1163. return -ENOMEM;
  1164. pmbox = &pmb->u.mb;
  1165. phba->link_state = LPFC_INIT_MBX_CMDS;
  1166. for (i = 0; i < psli->num_rings; i++) {
  1167. lpfc_config_ring(phba, i, pmb);
  1168. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1169. if (rc != MBX_SUCCESS) {
  1170. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1171. "0446 Adapter failed to init (%d), "
  1172. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1173. "ring %d\n",
  1174. rc, pmbox->mbxCommand,
  1175. pmbox->mbxStatus, i);
  1176. phba->link_state = LPFC_HBA_ERROR;
  1177. ret = -ENXIO;
  1178. break;
  1179. }
  1180. }
  1181. mempool_free(pmb, phba->mbox_mem_pool);
  1182. return ret;
  1183. }
  1184. /**
  1185. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1186. * @phba: Pointer to HBA context object.
  1187. * @pring: Pointer to driver SLI ring object.
  1188. * @piocb: Pointer to the driver iocb object.
  1189. *
  1190. * This function is called with hbalock held. The function adds the
  1191. * new iocb to txcmplq of the given ring. This function always returns
  1192. * 0. If this function is called for ELS ring, this function checks if
  1193. * there is a vport associated with the ELS command. This function also
  1194. * starts els_tmofunc timer if this is an ELS command.
  1195. **/
  1196. static int
  1197. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1198. struct lpfc_iocbq *piocb)
  1199. {
  1200. list_add_tail(&piocb->list, &pring->txcmplq);
  1201. piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
  1202. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1203. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1204. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN) &&
  1205. (!(piocb->vport->load_flag & FC_UNLOADING))) {
  1206. if (!piocb->vport)
  1207. BUG();
  1208. else
  1209. mod_timer(&piocb->vport->els_tmofunc,
  1210. jiffies +
  1211. msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
  1212. }
  1213. return 0;
  1214. }
  1215. /**
  1216. * lpfc_sli_ringtx_get - Get first element of the txq
  1217. * @phba: Pointer to HBA context object.
  1218. * @pring: Pointer to driver SLI ring object.
  1219. *
  1220. * This function is called with hbalock held to get next
  1221. * iocb in txq of the given ring. If there is any iocb in
  1222. * the txq, the function returns first iocb in the list after
  1223. * removing the iocb from the list, else it returns NULL.
  1224. **/
  1225. struct lpfc_iocbq *
  1226. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1227. {
  1228. struct lpfc_iocbq *cmd_iocb;
  1229. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1230. return cmd_iocb;
  1231. }
  1232. /**
  1233. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1234. * @phba: Pointer to HBA context object.
  1235. * @pring: Pointer to driver SLI ring object.
  1236. *
  1237. * This function is called with hbalock held and the caller must post the
  1238. * iocb without releasing the lock. If the caller releases the lock,
  1239. * iocb slot returned by the function is not guaranteed to be available.
  1240. * The function returns pointer to the next available iocb slot if there
  1241. * is available slot in the ring, else it returns NULL.
  1242. * If the get index of the ring is ahead of the put index, the function
  1243. * will post an error attention event to the worker thread to take the
  1244. * HBA to offline state.
  1245. **/
  1246. static IOCB_t *
  1247. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1248. {
  1249. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1250. uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
  1251. if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
  1252. (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
  1253. pring->sli.sli3.next_cmdidx = 0;
  1254. if (unlikely(pring->sli.sli3.local_getidx ==
  1255. pring->sli.sli3.next_cmdidx)) {
  1256. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1257. if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
  1258. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1259. "0315 Ring %d issue: portCmdGet %d "
  1260. "is bigger than cmd ring %d\n",
  1261. pring->ringno,
  1262. pring->sli.sli3.local_getidx,
  1263. max_cmd_idx);
  1264. phba->link_state = LPFC_HBA_ERROR;
  1265. /*
  1266. * All error attention handlers are posted to
  1267. * worker thread
  1268. */
  1269. phba->work_ha |= HA_ERATT;
  1270. phba->work_hs = HS_FFER3;
  1271. lpfc_worker_wake_up(phba);
  1272. return NULL;
  1273. }
  1274. if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
  1275. return NULL;
  1276. }
  1277. return lpfc_cmd_iocb(phba, pring);
  1278. }
  1279. /**
  1280. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1281. * @phba: Pointer to HBA context object.
  1282. * @iocbq: Pointer to driver iocb object.
  1283. *
  1284. * This function gets an iotag for the iocb. If there is no unused iotag and
  1285. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1286. * array and assigns a new iotag.
  1287. * The function returns the allocated iotag if successful, else returns zero.
  1288. * Zero is not a valid iotag.
  1289. * The caller is not required to hold any lock.
  1290. **/
  1291. uint16_t
  1292. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1293. {
  1294. struct lpfc_iocbq **new_arr;
  1295. struct lpfc_iocbq **old_arr;
  1296. size_t new_len;
  1297. struct lpfc_sli *psli = &phba->sli;
  1298. uint16_t iotag;
  1299. spin_lock_irq(&phba->hbalock);
  1300. iotag = psli->last_iotag;
  1301. if(++iotag < psli->iocbq_lookup_len) {
  1302. psli->last_iotag = iotag;
  1303. psli->iocbq_lookup[iotag] = iocbq;
  1304. spin_unlock_irq(&phba->hbalock);
  1305. iocbq->iotag = iotag;
  1306. return iotag;
  1307. } else if (psli->iocbq_lookup_len < (0xffff
  1308. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1309. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1310. spin_unlock_irq(&phba->hbalock);
  1311. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1312. GFP_KERNEL);
  1313. if (new_arr) {
  1314. spin_lock_irq(&phba->hbalock);
  1315. old_arr = psli->iocbq_lookup;
  1316. if (new_len <= psli->iocbq_lookup_len) {
  1317. /* highly unprobable case */
  1318. kfree(new_arr);
  1319. iotag = psli->last_iotag;
  1320. if(++iotag < psli->iocbq_lookup_len) {
  1321. psli->last_iotag = iotag;
  1322. psli->iocbq_lookup[iotag] = iocbq;
  1323. spin_unlock_irq(&phba->hbalock);
  1324. iocbq->iotag = iotag;
  1325. return iotag;
  1326. }
  1327. spin_unlock_irq(&phba->hbalock);
  1328. return 0;
  1329. }
  1330. if (psli->iocbq_lookup)
  1331. memcpy(new_arr, old_arr,
  1332. ((psli->last_iotag + 1) *
  1333. sizeof (struct lpfc_iocbq *)));
  1334. psli->iocbq_lookup = new_arr;
  1335. psli->iocbq_lookup_len = new_len;
  1336. psli->last_iotag = iotag;
  1337. psli->iocbq_lookup[iotag] = iocbq;
  1338. spin_unlock_irq(&phba->hbalock);
  1339. iocbq->iotag = iotag;
  1340. kfree(old_arr);
  1341. return iotag;
  1342. }
  1343. } else
  1344. spin_unlock_irq(&phba->hbalock);
  1345. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1346. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1347. psli->last_iotag);
  1348. return 0;
  1349. }
  1350. /**
  1351. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1352. * @phba: Pointer to HBA context object.
  1353. * @pring: Pointer to driver SLI ring object.
  1354. * @iocb: Pointer to iocb slot in the ring.
  1355. * @nextiocb: Pointer to driver iocb object which need to be
  1356. * posted to firmware.
  1357. *
  1358. * This function is called with hbalock held to post a new iocb to
  1359. * the firmware. This function copies the new iocb to ring iocb slot and
  1360. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1361. * a completion call back for this iocb else the function will free the
  1362. * iocb object.
  1363. **/
  1364. static void
  1365. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1366. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1367. {
  1368. /*
  1369. * Set up an iotag
  1370. */
  1371. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1372. if (pring->ringno == LPFC_ELS_RING) {
  1373. lpfc_debugfs_slow_ring_trc(phba,
  1374. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1375. *(((uint32_t *) &nextiocb->iocb) + 4),
  1376. *(((uint32_t *) &nextiocb->iocb) + 6),
  1377. *(((uint32_t *) &nextiocb->iocb) + 7));
  1378. }
  1379. /*
  1380. * Issue iocb command to adapter
  1381. */
  1382. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1383. wmb();
  1384. pring->stats.iocb_cmd++;
  1385. /*
  1386. * If there is no completion routine to call, we can release the
  1387. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1388. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1389. */
  1390. if (nextiocb->iocb_cmpl)
  1391. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1392. else
  1393. __lpfc_sli_release_iocbq(phba, nextiocb);
  1394. /*
  1395. * Let the HBA know what IOCB slot will be the next one the
  1396. * driver will put a command into.
  1397. */
  1398. pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
  1399. writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1400. }
  1401. /**
  1402. * lpfc_sli_update_full_ring - Update the chip attention register
  1403. * @phba: Pointer to HBA context object.
  1404. * @pring: Pointer to driver SLI ring object.
  1405. *
  1406. * The caller is not required to hold any lock for calling this function.
  1407. * This function updates the chip attention bits for the ring to inform firmware
  1408. * that there are pending work to be done for this ring and requests an
  1409. * interrupt when there is space available in the ring. This function is
  1410. * called when the driver is unable to post more iocbs to the ring due
  1411. * to unavailability of space in the ring.
  1412. **/
  1413. static void
  1414. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1415. {
  1416. int ringno = pring->ringno;
  1417. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1418. wmb();
  1419. /*
  1420. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1421. * The HBA will tell us when an IOCB entry is available.
  1422. */
  1423. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1424. readl(phba->CAregaddr); /* flush */
  1425. pring->stats.iocb_cmd_full++;
  1426. }
  1427. /**
  1428. * lpfc_sli_update_ring - Update chip attention register
  1429. * @phba: Pointer to HBA context object.
  1430. * @pring: Pointer to driver SLI ring object.
  1431. *
  1432. * This function updates the chip attention register bit for the
  1433. * given ring to inform HBA that there is more work to be done
  1434. * in this ring. The caller is not required to hold any lock.
  1435. **/
  1436. static void
  1437. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1438. {
  1439. int ringno = pring->ringno;
  1440. /*
  1441. * Tell the HBA that there is work to do in this ring.
  1442. */
  1443. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1444. wmb();
  1445. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1446. readl(phba->CAregaddr); /* flush */
  1447. }
  1448. }
  1449. /**
  1450. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1451. * @phba: Pointer to HBA context object.
  1452. * @pring: Pointer to driver SLI ring object.
  1453. *
  1454. * This function is called with hbalock held to post pending iocbs
  1455. * in the txq to the firmware. This function is called when driver
  1456. * detects space available in the ring.
  1457. **/
  1458. static void
  1459. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1460. {
  1461. IOCB_t *iocb;
  1462. struct lpfc_iocbq *nextiocb;
  1463. /*
  1464. * Check to see if:
  1465. * (a) there is anything on the txq to send
  1466. * (b) link is up
  1467. * (c) link attention events can be processed (fcp ring only)
  1468. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1469. */
  1470. if (lpfc_is_link_up(phba) &&
  1471. (!list_empty(&pring->txq)) &&
  1472. (pring->ringno != phba->sli.fcp_ring ||
  1473. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1474. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1475. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1476. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1477. if (iocb)
  1478. lpfc_sli_update_ring(phba, pring);
  1479. else
  1480. lpfc_sli_update_full_ring(phba, pring);
  1481. }
  1482. return;
  1483. }
  1484. /**
  1485. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1486. * @phba: Pointer to HBA context object.
  1487. * @hbqno: HBQ number.
  1488. *
  1489. * This function is called with hbalock held to get the next
  1490. * available slot for the given HBQ. If there is free slot
  1491. * available for the HBQ it will return pointer to the next available
  1492. * HBQ entry else it will return NULL.
  1493. **/
  1494. static struct lpfc_hbq_entry *
  1495. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1496. {
  1497. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1498. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1499. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1500. hbqp->next_hbqPutIdx = 0;
  1501. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1502. uint32_t raw_index = phba->hbq_get[hbqno];
  1503. uint32_t getidx = le32_to_cpu(raw_index);
  1504. hbqp->local_hbqGetIdx = getidx;
  1505. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1506. lpfc_printf_log(phba, KERN_ERR,
  1507. LOG_SLI | LOG_VPORT,
  1508. "1802 HBQ %d: local_hbqGetIdx "
  1509. "%u is > than hbqp->entry_count %u\n",
  1510. hbqno, hbqp->local_hbqGetIdx,
  1511. hbqp->entry_count);
  1512. phba->link_state = LPFC_HBA_ERROR;
  1513. return NULL;
  1514. }
  1515. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1516. return NULL;
  1517. }
  1518. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1519. hbqp->hbqPutIdx;
  1520. }
  1521. /**
  1522. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1523. * @phba: Pointer to HBA context object.
  1524. *
  1525. * This function is called with no lock held to free all the
  1526. * hbq buffers while uninitializing the SLI interface. It also
  1527. * frees the HBQ buffers returned by the firmware but not yet
  1528. * processed by the upper layers.
  1529. **/
  1530. void
  1531. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1532. {
  1533. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1534. struct hbq_dmabuf *hbq_buf;
  1535. unsigned long flags;
  1536. int i, hbq_count;
  1537. uint32_t hbqno;
  1538. hbq_count = lpfc_sli_hbq_count();
  1539. /* Return all memory used by all HBQs */
  1540. spin_lock_irqsave(&phba->hbalock, flags);
  1541. for (i = 0; i < hbq_count; ++i) {
  1542. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1543. &phba->hbqs[i].hbq_buffer_list, list) {
  1544. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1545. list_del(&hbq_buf->dbuf.list);
  1546. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1547. }
  1548. phba->hbqs[i].buffer_count = 0;
  1549. }
  1550. /* Return all HBQ buffer that are in-fly */
  1551. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1552. list) {
  1553. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1554. list_del(&hbq_buf->dbuf.list);
  1555. if (hbq_buf->tag == -1) {
  1556. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1557. (phba, hbq_buf);
  1558. } else {
  1559. hbqno = hbq_buf->tag >> 16;
  1560. if (hbqno >= LPFC_MAX_HBQS)
  1561. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1562. (phba, hbq_buf);
  1563. else
  1564. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1565. hbq_buf);
  1566. }
  1567. }
  1568. /* Mark the HBQs not in use */
  1569. phba->hbq_in_use = 0;
  1570. spin_unlock_irqrestore(&phba->hbalock, flags);
  1571. }
  1572. /**
  1573. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1574. * @phba: Pointer to HBA context object.
  1575. * @hbqno: HBQ number.
  1576. * @hbq_buf: Pointer to HBQ buffer.
  1577. *
  1578. * This function is called with the hbalock held to post a
  1579. * hbq buffer to the firmware. If the function finds an empty
  1580. * slot in the HBQ, it will post the buffer. The function will return
  1581. * pointer to the hbq entry if it successfully post the buffer
  1582. * else it will return NULL.
  1583. **/
  1584. static int
  1585. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1586. struct hbq_dmabuf *hbq_buf)
  1587. {
  1588. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1589. }
  1590. /**
  1591. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1592. * @phba: Pointer to HBA context object.
  1593. * @hbqno: HBQ number.
  1594. * @hbq_buf: Pointer to HBQ buffer.
  1595. *
  1596. * This function is called with the hbalock held to post a hbq buffer to the
  1597. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1598. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1599. * it successfully post the buffer else it will return an error.
  1600. **/
  1601. static int
  1602. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1603. struct hbq_dmabuf *hbq_buf)
  1604. {
  1605. struct lpfc_hbq_entry *hbqe;
  1606. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1607. /* Get next HBQ entry slot to use */
  1608. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1609. if (hbqe) {
  1610. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1611. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1612. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1613. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1614. hbqe->bde.tus.f.bdeFlags = 0;
  1615. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1616. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1617. /* Sync SLIM */
  1618. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1619. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1620. /* flush */
  1621. readl(phba->hbq_put + hbqno);
  1622. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1623. return 0;
  1624. } else
  1625. return -ENOMEM;
  1626. }
  1627. /**
  1628. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1629. * @phba: Pointer to HBA context object.
  1630. * @hbqno: HBQ number.
  1631. * @hbq_buf: Pointer to HBQ buffer.
  1632. *
  1633. * This function is called with the hbalock held to post an RQE to the SLI4
  1634. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1635. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1636. **/
  1637. static int
  1638. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1639. struct hbq_dmabuf *hbq_buf)
  1640. {
  1641. int rc;
  1642. struct lpfc_rqe hrqe;
  1643. struct lpfc_rqe drqe;
  1644. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1645. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1646. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1647. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1648. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1649. &hrqe, &drqe);
  1650. if (rc < 0)
  1651. return rc;
  1652. hbq_buf->tag = rc;
  1653. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1654. return 0;
  1655. }
  1656. /* HBQ for ELS and CT traffic. */
  1657. static struct lpfc_hbq_init lpfc_els_hbq = {
  1658. .rn = 1,
  1659. .entry_count = 256,
  1660. .mask_count = 0,
  1661. .profile = 0,
  1662. .ring_mask = (1 << LPFC_ELS_RING),
  1663. .buffer_count = 0,
  1664. .init_count = 40,
  1665. .add_count = 40,
  1666. };
  1667. /* HBQ for the extra ring if needed */
  1668. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1669. .rn = 1,
  1670. .entry_count = 200,
  1671. .mask_count = 0,
  1672. .profile = 0,
  1673. .ring_mask = (1 << LPFC_EXTRA_RING),
  1674. .buffer_count = 0,
  1675. .init_count = 0,
  1676. .add_count = 5,
  1677. };
  1678. /* Array of HBQs */
  1679. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1680. &lpfc_els_hbq,
  1681. &lpfc_extra_hbq,
  1682. };
  1683. /**
  1684. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1685. * @phba: Pointer to HBA context object.
  1686. * @hbqno: HBQ number.
  1687. * @count: Number of HBQ buffers to be posted.
  1688. *
  1689. * This function is called with no lock held to post more hbq buffers to the
  1690. * given HBQ. The function returns the number of HBQ buffers successfully
  1691. * posted.
  1692. **/
  1693. static int
  1694. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1695. {
  1696. uint32_t i, posted = 0;
  1697. unsigned long flags;
  1698. struct hbq_dmabuf *hbq_buffer;
  1699. LIST_HEAD(hbq_buf_list);
  1700. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1701. return 0;
  1702. if ((phba->hbqs[hbqno].buffer_count + count) >
  1703. lpfc_hbq_defs[hbqno]->entry_count)
  1704. count = lpfc_hbq_defs[hbqno]->entry_count -
  1705. phba->hbqs[hbqno].buffer_count;
  1706. if (!count)
  1707. return 0;
  1708. /* Allocate HBQ entries */
  1709. for (i = 0; i < count; i++) {
  1710. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1711. if (!hbq_buffer)
  1712. break;
  1713. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1714. }
  1715. /* Check whether HBQ is still in use */
  1716. spin_lock_irqsave(&phba->hbalock, flags);
  1717. if (!phba->hbq_in_use)
  1718. goto err;
  1719. while (!list_empty(&hbq_buf_list)) {
  1720. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1721. dbuf.list);
  1722. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1723. (hbqno << 16));
  1724. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1725. phba->hbqs[hbqno].buffer_count++;
  1726. posted++;
  1727. } else
  1728. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1729. }
  1730. spin_unlock_irqrestore(&phba->hbalock, flags);
  1731. return posted;
  1732. err:
  1733. spin_unlock_irqrestore(&phba->hbalock, flags);
  1734. while (!list_empty(&hbq_buf_list)) {
  1735. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1736. dbuf.list);
  1737. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1738. }
  1739. return 0;
  1740. }
  1741. /**
  1742. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1743. * @phba: Pointer to HBA context object.
  1744. * @qno: HBQ number.
  1745. *
  1746. * This function posts more buffers to the HBQ. This function
  1747. * is called with no lock held. The function returns the number of HBQ entries
  1748. * successfully allocated.
  1749. **/
  1750. int
  1751. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1752. {
  1753. if (phba->sli_rev == LPFC_SLI_REV4)
  1754. return 0;
  1755. else
  1756. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1757. lpfc_hbq_defs[qno]->add_count);
  1758. }
  1759. /**
  1760. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1761. * @phba: Pointer to HBA context object.
  1762. * @qno: HBQ queue number.
  1763. *
  1764. * This function is called from SLI initialization code path with
  1765. * no lock held to post initial HBQ buffers to firmware. The
  1766. * function returns the number of HBQ entries successfully allocated.
  1767. **/
  1768. static int
  1769. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1770. {
  1771. if (phba->sli_rev == LPFC_SLI_REV4)
  1772. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1773. lpfc_hbq_defs[qno]->entry_count);
  1774. else
  1775. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1776. lpfc_hbq_defs[qno]->init_count);
  1777. }
  1778. /**
  1779. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1780. * @phba: Pointer to HBA context object.
  1781. * @hbqno: HBQ number.
  1782. *
  1783. * This function removes the first hbq buffer on an hbq list and returns a
  1784. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1785. **/
  1786. static struct hbq_dmabuf *
  1787. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1788. {
  1789. struct lpfc_dmabuf *d_buf;
  1790. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1791. if (!d_buf)
  1792. return NULL;
  1793. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1794. }
  1795. /**
  1796. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1797. * @phba: Pointer to HBA context object.
  1798. * @tag: Tag of the hbq buffer.
  1799. *
  1800. * This function is called with hbalock held. This function searches
  1801. * for the hbq buffer associated with the given tag in the hbq buffer
  1802. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1803. * it returns NULL.
  1804. **/
  1805. static struct hbq_dmabuf *
  1806. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1807. {
  1808. struct lpfc_dmabuf *d_buf;
  1809. struct hbq_dmabuf *hbq_buf;
  1810. uint32_t hbqno;
  1811. hbqno = tag >> 16;
  1812. if (hbqno >= LPFC_MAX_HBQS)
  1813. return NULL;
  1814. spin_lock_irq(&phba->hbalock);
  1815. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1816. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1817. if (hbq_buf->tag == tag) {
  1818. spin_unlock_irq(&phba->hbalock);
  1819. return hbq_buf;
  1820. }
  1821. }
  1822. spin_unlock_irq(&phba->hbalock);
  1823. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1824. "1803 Bad hbq tag. Data: x%x x%x\n",
  1825. tag, phba->hbqs[tag >> 16].buffer_count);
  1826. return NULL;
  1827. }
  1828. /**
  1829. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1830. * @phba: Pointer to HBA context object.
  1831. * @hbq_buffer: Pointer to HBQ buffer.
  1832. *
  1833. * This function is called with hbalock. This function gives back
  1834. * the hbq buffer to firmware. If the HBQ does not have space to
  1835. * post the buffer, it will free the buffer.
  1836. **/
  1837. void
  1838. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1839. {
  1840. uint32_t hbqno;
  1841. if (hbq_buffer) {
  1842. hbqno = hbq_buffer->tag >> 16;
  1843. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1844. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1845. }
  1846. }
  1847. /**
  1848. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1849. * @mbxCommand: mailbox command code.
  1850. *
  1851. * This function is called by the mailbox event handler function to verify
  1852. * that the completed mailbox command is a legitimate mailbox command. If the
  1853. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1854. * and the mailbox event handler will take the HBA offline.
  1855. **/
  1856. static int
  1857. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1858. {
  1859. uint8_t ret;
  1860. switch (mbxCommand) {
  1861. case MBX_LOAD_SM:
  1862. case MBX_READ_NV:
  1863. case MBX_WRITE_NV:
  1864. case MBX_WRITE_VPARMS:
  1865. case MBX_RUN_BIU_DIAG:
  1866. case MBX_INIT_LINK:
  1867. case MBX_DOWN_LINK:
  1868. case MBX_CONFIG_LINK:
  1869. case MBX_CONFIG_RING:
  1870. case MBX_RESET_RING:
  1871. case MBX_READ_CONFIG:
  1872. case MBX_READ_RCONFIG:
  1873. case MBX_READ_SPARM:
  1874. case MBX_READ_STATUS:
  1875. case MBX_READ_RPI:
  1876. case MBX_READ_XRI:
  1877. case MBX_READ_REV:
  1878. case MBX_READ_LNK_STAT:
  1879. case MBX_REG_LOGIN:
  1880. case MBX_UNREG_LOGIN:
  1881. case MBX_CLEAR_LA:
  1882. case MBX_DUMP_MEMORY:
  1883. case MBX_DUMP_CONTEXT:
  1884. case MBX_RUN_DIAGS:
  1885. case MBX_RESTART:
  1886. case MBX_UPDATE_CFG:
  1887. case MBX_DOWN_LOAD:
  1888. case MBX_DEL_LD_ENTRY:
  1889. case MBX_RUN_PROGRAM:
  1890. case MBX_SET_MASK:
  1891. case MBX_SET_VARIABLE:
  1892. case MBX_UNREG_D_ID:
  1893. case MBX_KILL_BOARD:
  1894. case MBX_CONFIG_FARP:
  1895. case MBX_BEACON:
  1896. case MBX_LOAD_AREA:
  1897. case MBX_RUN_BIU_DIAG64:
  1898. case MBX_CONFIG_PORT:
  1899. case MBX_READ_SPARM64:
  1900. case MBX_READ_RPI64:
  1901. case MBX_REG_LOGIN64:
  1902. case MBX_READ_TOPOLOGY:
  1903. case MBX_WRITE_WWN:
  1904. case MBX_SET_DEBUG:
  1905. case MBX_LOAD_EXP_ROM:
  1906. case MBX_ASYNCEVT_ENABLE:
  1907. case MBX_REG_VPI:
  1908. case MBX_UNREG_VPI:
  1909. case MBX_HEARTBEAT:
  1910. case MBX_PORT_CAPABILITIES:
  1911. case MBX_PORT_IOV_CONTROL:
  1912. case MBX_SLI4_CONFIG:
  1913. case MBX_SLI4_REQ_FTRS:
  1914. case MBX_REG_FCFI:
  1915. case MBX_UNREG_FCFI:
  1916. case MBX_REG_VFI:
  1917. case MBX_UNREG_VFI:
  1918. case MBX_INIT_VPI:
  1919. case MBX_INIT_VFI:
  1920. case MBX_RESUME_RPI:
  1921. case MBX_READ_EVENT_LOG_STATUS:
  1922. case MBX_READ_EVENT_LOG:
  1923. case MBX_SECURITY_MGMT:
  1924. case MBX_AUTH_PORT:
  1925. case MBX_ACCESS_VDATA:
  1926. ret = mbxCommand;
  1927. break;
  1928. default:
  1929. ret = MBX_SHUTDOWN;
  1930. break;
  1931. }
  1932. return ret;
  1933. }
  1934. /**
  1935. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1936. * @phba: Pointer to HBA context object.
  1937. * @pmboxq: Pointer to mailbox command.
  1938. *
  1939. * This is completion handler function for mailbox commands issued from
  1940. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1941. * mailbox event handler function with no lock held. This function
  1942. * will wake up thread waiting on the wait queue pointed by context1
  1943. * of the mailbox.
  1944. **/
  1945. void
  1946. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1947. {
  1948. wait_queue_head_t *pdone_q;
  1949. unsigned long drvr_flag;
  1950. /*
  1951. * If pdone_q is empty, the driver thread gave up waiting and
  1952. * continued running.
  1953. */
  1954. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1955. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1956. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1957. if (pdone_q)
  1958. wake_up_interruptible(pdone_q);
  1959. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1960. return;
  1961. }
  1962. /**
  1963. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1964. * @phba: Pointer to HBA context object.
  1965. * @pmb: Pointer to mailbox object.
  1966. *
  1967. * This function is the default mailbox completion handler. It
  1968. * frees the memory resources associated with the completed mailbox
  1969. * command. If the completed command is a REG_LOGIN mailbox command,
  1970. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1971. **/
  1972. void
  1973. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1974. {
  1975. struct lpfc_vport *vport = pmb->vport;
  1976. struct lpfc_dmabuf *mp;
  1977. struct lpfc_nodelist *ndlp;
  1978. struct Scsi_Host *shost;
  1979. uint16_t rpi, vpi;
  1980. int rc;
  1981. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1982. if (mp) {
  1983. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1984. kfree(mp);
  1985. }
  1986. /*
  1987. * If a REG_LOGIN succeeded after node is destroyed or node
  1988. * is in re-discovery driver need to cleanup the RPI.
  1989. */
  1990. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1991. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1992. !pmb->u.mb.mbxStatus) {
  1993. rpi = pmb->u.mb.un.varWords[0];
  1994. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1995. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1996. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1997. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1998. if (rc != MBX_NOT_FINISHED)
  1999. return;
  2000. }
  2001. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  2002. !(phba->pport->load_flag & FC_UNLOADING) &&
  2003. !pmb->u.mb.mbxStatus) {
  2004. shost = lpfc_shost_from_vport(vport);
  2005. spin_lock_irq(shost->host_lock);
  2006. vport->vpi_state |= LPFC_VPI_REGISTERED;
  2007. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  2008. spin_unlock_irq(shost->host_lock);
  2009. }
  2010. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  2011. ndlp = (struct lpfc_nodelist *)pmb->context2;
  2012. lpfc_nlp_put(ndlp);
  2013. pmb->context2 = NULL;
  2014. }
  2015. /* Check security permission status on INIT_LINK mailbox command */
  2016. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  2017. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  2018. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2019. "2860 SLI authentication is required "
  2020. "for INIT_LINK but has not done yet\n");
  2021. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  2022. lpfc_sli4_mbox_cmd_free(phba, pmb);
  2023. else
  2024. mempool_free(pmb, phba->mbox_mem_pool);
  2025. }
  2026. /**
  2027. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  2028. * @phba: Pointer to HBA context object.
  2029. *
  2030. * This function is called with no lock held. This function processes all
  2031. * the completed mailbox commands and gives it to upper layers. The interrupt
  2032. * service routine processes mailbox completion interrupt and adds completed
  2033. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  2034. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  2035. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  2036. * function returns the mailbox commands to the upper layer by calling the
  2037. * completion handler function of each mailbox.
  2038. **/
  2039. int
  2040. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  2041. {
  2042. MAILBOX_t *pmbox;
  2043. LPFC_MBOXQ_t *pmb;
  2044. int rc;
  2045. LIST_HEAD(cmplq);
  2046. phba->sli.slistat.mbox_event++;
  2047. /* Get all completed mailboxe buffers into the cmplq */
  2048. spin_lock_irq(&phba->hbalock);
  2049. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2050. spin_unlock_irq(&phba->hbalock);
  2051. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2052. do {
  2053. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2054. if (pmb == NULL)
  2055. break;
  2056. pmbox = &pmb->u.mb;
  2057. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2058. if (pmb->vport) {
  2059. lpfc_debugfs_disc_trc(pmb->vport,
  2060. LPFC_DISC_TRC_MBOX_VPORT,
  2061. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2062. (uint32_t)pmbox->mbxCommand,
  2063. pmbox->un.varWords[0],
  2064. pmbox->un.varWords[1]);
  2065. }
  2066. else {
  2067. lpfc_debugfs_disc_trc(phba->pport,
  2068. LPFC_DISC_TRC_MBOX,
  2069. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2070. (uint32_t)pmbox->mbxCommand,
  2071. pmbox->un.varWords[0],
  2072. pmbox->un.varWords[1]);
  2073. }
  2074. }
  2075. /*
  2076. * It is a fatal error if unknown mbox command completion.
  2077. */
  2078. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2079. MBX_SHUTDOWN) {
  2080. /* Unknown mailbox command compl */
  2081. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2082. "(%d):0323 Unknown Mailbox command "
  2083. "x%x (x%x/x%x) Cmpl\n",
  2084. pmb->vport ? pmb->vport->vpi : 0,
  2085. pmbox->mbxCommand,
  2086. lpfc_sli_config_mbox_subsys_get(phba,
  2087. pmb),
  2088. lpfc_sli_config_mbox_opcode_get(phba,
  2089. pmb));
  2090. phba->link_state = LPFC_HBA_ERROR;
  2091. phba->work_hs = HS_FFER3;
  2092. lpfc_handle_eratt(phba);
  2093. continue;
  2094. }
  2095. if (pmbox->mbxStatus) {
  2096. phba->sli.slistat.mbox_stat_err++;
  2097. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2098. /* Mbox cmd cmpl error - RETRYing */
  2099. lpfc_printf_log(phba, KERN_INFO,
  2100. LOG_MBOX | LOG_SLI,
  2101. "(%d):0305 Mbox cmd cmpl "
  2102. "error - RETRYing Data: x%x "
  2103. "(x%x/x%x) x%x x%x x%x\n",
  2104. pmb->vport ? pmb->vport->vpi : 0,
  2105. pmbox->mbxCommand,
  2106. lpfc_sli_config_mbox_subsys_get(phba,
  2107. pmb),
  2108. lpfc_sli_config_mbox_opcode_get(phba,
  2109. pmb),
  2110. pmbox->mbxStatus,
  2111. pmbox->un.varWords[0],
  2112. pmb->vport->port_state);
  2113. pmbox->mbxStatus = 0;
  2114. pmbox->mbxOwner = OWN_HOST;
  2115. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2116. if (rc != MBX_NOT_FINISHED)
  2117. continue;
  2118. }
  2119. }
  2120. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2121. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2122. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2123. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  2124. "x%x x%x x%x\n",
  2125. pmb->vport ? pmb->vport->vpi : 0,
  2126. pmbox->mbxCommand,
  2127. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2128. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2129. pmb->mbox_cmpl,
  2130. *((uint32_t *) pmbox),
  2131. pmbox->un.varWords[0],
  2132. pmbox->un.varWords[1],
  2133. pmbox->un.varWords[2],
  2134. pmbox->un.varWords[3],
  2135. pmbox->un.varWords[4],
  2136. pmbox->un.varWords[5],
  2137. pmbox->un.varWords[6],
  2138. pmbox->un.varWords[7],
  2139. pmbox->un.varWords[8],
  2140. pmbox->un.varWords[9],
  2141. pmbox->un.varWords[10]);
  2142. if (pmb->mbox_cmpl)
  2143. pmb->mbox_cmpl(phba,pmb);
  2144. } while (1);
  2145. return 0;
  2146. }
  2147. /**
  2148. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2149. * @phba: Pointer to HBA context object.
  2150. * @pring: Pointer to driver SLI ring object.
  2151. * @tag: buffer tag.
  2152. *
  2153. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2154. * is set in the tag the buffer is posted for a particular exchange,
  2155. * the function will return the buffer without replacing the buffer.
  2156. * If the buffer is for unsolicited ELS or CT traffic, this function
  2157. * returns the buffer and also posts another buffer to the firmware.
  2158. **/
  2159. static struct lpfc_dmabuf *
  2160. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2161. struct lpfc_sli_ring *pring,
  2162. uint32_t tag)
  2163. {
  2164. struct hbq_dmabuf *hbq_entry;
  2165. if (tag & QUE_BUFTAG_BIT)
  2166. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2167. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2168. if (!hbq_entry)
  2169. return NULL;
  2170. return &hbq_entry->dbuf;
  2171. }
  2172. /**
  2173. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2174. * @phba: Pointer to HBA context object.
  2175. * @pring: Pointer to driver SLI ring object.
  2176. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2177. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2178. * @fch_type: the type for the first frame of the sequence.
  2179. *
  2180. * This function is called with no lock held. This function uses the r_ctl and
  2181. * type of the received sequence to find the correct callback function to call
  2182. * to process the sequence.
  2183. **/
  2184. static int
  2185. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2186. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2187. uint32_t fch_type)
  2188. {
  2189. int i;
  2190. /* unSolicited Responses */
  2191. if (pring->prt[0].profile) {
  2192. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2193. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2194. saveq);
  2195. return 1;
  2196. }
  2197. /* We must search, based on rctl / type
  2198. for the right routine */
  2199. for (i = 0; i < pring->num_mask; i++) {
  2200. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2201. (pring->prt[i].type == fch_type)) {
  2202. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2203. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2204. (phba, pring, saveq);
  2205. return 1;
  2206. }
  2207. }
  2208. return 0;
  2209. }
  2210. /**
  2211. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2212. * @phba: Pointer to HBA context object.
  2213. * @pring: Pointer to driver SLI ring object.
  2214. * @saveq: Pointer to the unsolicited iocb.
  2215. *
  2216. * This function is called with no lock held by the ring event handler
  2217. * when there is an unsolicited iocb posted to the response ring by the
  2218. * firmware. This function gets the buffer associated with the iocbs
  2219. * and calls the event handler for the ring. This function handles both
  2220. * qring buffers and hbq buffers.
  2221. * When the function returns 1 the caller can free the iocb object otherwise
  2222. * upper layer functions will free the iocb objects.
  2223. **/
  2224. static int
  2225. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2226. struct lpfc_iocbq *saveq)
  2227. {
  2228. IOCB_t * irsp;
  2229. WORD5 * w5p;
  2230. uint32_t Rctl, Type;
  2231. struct lpfc_iocbq *iocbq;
  2232. struct lpfc_dmabuf *dmzbuf;
  2233. irsp = &(saveq->iocb);
  2234. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2235. if (pring->lpfc_sli_rcv_async_status)
  2236. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2237. else
  2238. lpfc_printf_log(phba,
  2239. KERN_WARNING,
  2240. LOG_SLI,
  2241. "0316 Ring %d handler: unexpected "
  2242. "ASYNC_STATUS iocb received evt_code "
  2243. "0x%x\n",
  2244. pring->ringno,
  2245. irsp->un.asyncstat.evt_code);
  2246. return 1;
  2247. }
  2248. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2249. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2250. if (irsp->ulpBdeCount > 0) {
  2251. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2252. irsp->un.ulpWord[3]);
  2253. lpfc_in_buf_free(phba, dmzbuf);
  2254. }
  2255. if (irsp->ulpBdeCount > 1) {
  2256. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2257. irsp->unsli3.sli3Words[3]);
  2258. lpfc_in_buf_free(phba, dmzbuf);
  2259. }
  2260. if (irsp->ulpBdeCount > 2) {
  2261. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2262. irsp->unsli3.sli3Words[7]);
  2263. lpfc_in_buf_free(phba, dmzbuf);
  2264. }
  2265. return 1;
  2266. }
  2267. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2268. if (irsp->ulpBdeCount != 0) {
  2269. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2270. irsp->un.ulpWord[3]);
  2271. if (!saveq->context2)
  2272. lpfc_printf_log(phba,
  2273. KERN_ERR,
  2274. LOG_SLI,
  2275. "0341 Ring %d Cannot find buffer for "
  2276. "an unsolicited iocb. tag 0x%x\n",
  2277. pring->ringno,
  2278. irsp->un.ulpWord[3]);
  2279. }
  2280. if (irsp->ulpBdeCount == 2) {
  2281. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2282. irsp->unsli3.sli3Words[7]);
  2283. if (!saveq->context3)
  2284. lpfc_printf_log(phba,
  2285. KERN_ERR,
  2286. LOG_SLI,
  2287. "0342 Ring %d Cannot find buffer for an"
  2288. " unsolicited iocb. tag 0x%x\n",
  2289. pring->ringno,
  2290. irsp->unsli3.sli3Words[7]);
  2291. }
  2292. list_for_each_entry(iocbq, &saveq->list, list) {
  2293. irsp = &(iocbq->iocb);
  2294. if (irsp->ulpBdeCount != 0) {
  2295. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2296. irsp->un.ulpWord[3]);
  2297. if (!iocbq->context2)
  2298. lpfc_printf_log(phba,
  2299. KERN_ERR,
  2300. LOG_SLI,
  2301. "0343 Ring %d Cannot find "
  2302. "buffer for an unsolicited iocb"
  2303. ". tag 0x%x\n", pring->ringno,
  2304. irsp->un.ulpWord[3]);
  2305. }
  2306. if (irsp->ulpBdeCount == 2) {
  2307. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2308. irsp->unsli3.sli3Words[7]);
  2309. if (!iocbq->context3)
  2310. lpfc_printf_log(phba,
  2311. KERN_ERR,
  2312. LOG_SLI,
  2313. "0344 Ring %d Cannot find "
  2314. "buffer for an unsolicited "
  2315. "iocb. tag 0x%x\n",
  2316. pring->ringno,
  2317. irsp->unsli3.sli3Words[7]);
  2318. }
  2319. }
  2320. }
  2321. if (irsp->ulpBdeCount != 0 &&
  2322. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2323. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2324. int found = 0;
  2325. /* search continue save q for same XRI */
  2326. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2327. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2328. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2329. list_add_tail(&saveq->list, &iocbq->list);
  2330. found = 1;
  2331. break;
  2332. }
  2333. }
  2334. if (!found)
  2335. list_add_tail(&saveq->clist,
  2336. &pring->iocb_continue_saveq);
  2337. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2338. list_del_init(&iocbq->clist);
  2339. saveq = iocbq;
  2340. irsp = &(saveq->iocb);
  2341. } else
  2342. return 0;
  2343. }
  2344. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2345. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2346. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2347. Rctl = FC_RCTL_ELS_REQ;
  2348. Type = FC_TYPE_ELS;
  2349. } else {
  2350. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2351. Rctl = w5p->hcsw.Rctl;
  2352. Type = w5p->hcsw.Type;
  2353. /* Firmware Workaround */
  2354. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2355. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2356. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2357. Rctl = FC_RCTL_ELS_REQ;
  2358. Type = FC_TYPE_ELS;
  2359. w5p->hcsw.Rctl = Rctl;
  2360. w5p->hcsw.Type = Type;
  2361. }
  2362. }
  2363. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2364. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2365. "0313 Ring %d handler: unexpected Rctl x%x "
  2366. "Type x%x received\n",
  2367. pring->ringno, Rctl, Type);
  2368. return 1;
  2369. }
  2370. /**
  2371. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2372. * @phba: Pointer to HBA context object.
  2373. * @pring: Pointer to driver SLI ring object.
  2374. * @prspiocb: Pointer to response iocb object.
  2375. *
  2376. * This function looks up the iocb_lookup table to get the command iocb
  2377. * corresponding to the given response iocb using the iotag of the
  2378. * response iocb. This function is called with the hbalock held.
  2379. * This function returns the command iocb object if it finds the command
  2380. * iocb else returns NULL.
  2381. **/
  2382. static struct lpfc_iocbq *
  2383. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2384. struct lpfc_sli_ring *pring,
  2385. struct lpfc_iocbq *prspiocb)
  2386. {
  2387. struct lpfc_iocbq *cmd_iocb = NULL;
  2388. uint16_t iotag;
  2389. iotag = prspiocb->iocb.ulpIoTag;
  2390. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2391. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2392. list_del_init(&cmd_iocb->list);
  2393. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2394. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2395. }
  2396. return cmd_iocb;
  2397. }
  2398. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2399. "0317 iotag x%x is out off "
  2400. "range: max iotag x%x wd0 x%x\n",
  2401. iotag, phba->sli.last_iotag,
  2402. *(((uint32_t *) &prspiocb->iocb) + 7));
  2403. return NULL;
  2404. }
  2405. /**
  2406. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2407. * @phba: Pointer to HBA context object.
  2408. * @pring: Pointer to driver SLI ring object.
  2409. * @iotag: IOCB tag.
  2410. *
  2411. * This function looks up the iocb_lookup table to get the command iocb
  2412. * corresponding to the given iotag. This function is called with the
  2413. * hbalock held.
  2414. * This function returns the command iocb object if it finds the command
  2415. * iocb else returns NULL.
  2416. **/
  2417. static struct lpfc_iocbq *
  2418. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2419. struct lpfc_sli_ring *pring, uint16_t iotag)
  2420. {
  2421. struct lpfc_iocbq *cmd_iocb;
  2422. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2423. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2424. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2425. /* remove from txcmpl queue list */
  2426. list_del_init(&cmd_iocb->list);
  2427. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2428. return cmd_iocb;
  2429. }
  2430. }
  2431. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2432. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2433. iotag, phba->sli.last_iotag);
  2434. return NULL;
  2435. }
  2436. /**
  2437. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2438. * @phba: Pointer to HBA context object.
  2439. * @pring: Pointer to driver SLI ring object.
  2440. * @saveq: Pointer to the response iocb to be processed.
  2441. *
  2442. * This function is called by the ring event handler for non-fcp
  2443. * rings when there is a new response iocb in the response ring.
  2444. * The caller is not required to hold any locks. This function
  2445. * gets the command iocb associated with the response iocb and
  2446. * calls the completion handler for the command iocb. If there
  2447. * is no completion handler, the function will free the resources
  2448. * associated with command iocb. If the response iocb is for
  2449. * an already aborted command iocb, the status of the completion
  2450. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2451. * This function always returns 1.
  2452. **/
  2453. static int
  2454. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2455. struct lpfc_iocbq *saveq)
  2456. {
  2457. struct lpfc_iocbq *cmdiocbp;
  2458. int rc = 1;
  2459. unsigned long iflag;
  2460. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2461. spin_lock_irqsave(&phba->hbalock, iflag);
  2462. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2463. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2464. if (cmdiocbp) {
  2465. if (cmdiocbp->iocb_cmpl) {
  2466. /*
  2467. * If an ELS command failed send an event to mgmt
  2468. * application.
  2469. */
  2470. if (saveq->iocb.ulpStatus &&
  2471. (pring->ringno == LPFC_ELS_RING) &&
  2472. (cmdiocbp->iocb.ulpCommand ==
  2473. CMD_ELS_REQUEST64_CR))
  2474. lpfc_send_els_failure_event(phba,
  2475. cmdiocbp, saveq);
  2476. /*
  2477. * Post all ELS completions to the worker thread.
  2478. * All other are passed to the completion callback.
  2479. */
  2480. if (pring->ringno == LPFC_ELS_RING) {
  2481. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2482. (cmdiocbp->iocb_flag &
  2483. LPFC_DRIVER_ABORTED)) {
  2484. spin_lock_irqsave(&phba->hbalock,
  2485. iflag);
  2486. cmdiocbp->iocb_flag &=
  2487. ~LPFC_DRIVER_ABORTED;
  2488. spin_unlock_irqrestore(&phba->hbalock,
  2489. iflag);
  2490. saveq->iocb.ulpStatus =
  2491. IOSTAT_LOCAL_REJECT;
  2492. saveq->iocb.un.ulpWord[4] =
  2493. IOERR_SLI_ABORTED;
  2494. /* Firmware could still be in progress
  2495. * of DMAing payload, so don't free data
  2496. * buffer till after a hbeat.
  2497. */
  2498. spin_lock_irqsave(&phba->hbalock,
  2499. iflag);
  2500. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2501. spin_unlock_irqrestore(&phba->hbalock,
  2502. iflag);
  2503. }
  2504. if (phba->sli_rev == LPFC_SLI_REV4) {
  2505. if (saveq->iocb_flag &
  2506. LPFC_EXCHANGE_BUSY) {
  2507. /* Set cmdiocb flag for the
  2508. * exchange busy so sgl (xri)
  2509. * will not be released until
  2510. * the abort xri is received
  2511. * from hba.
  2512. */
  2513. spin_lock_irqsave(
  2514. &phba->hbalock, iflag);
  2515. cmdiocbp->iocb_flag |=
  2516. LPFC_EXCHANGE_BUSY;
  2517. spin_unlock_irqrestore(
  2518. &phba->hbalock, iflag);
  2519. }
  2520. if (cmdiocbp->iocb_flag &
  2521. LPFC_DRIVER_ABORTED) {
  2522. /*
  2523. * Clear LPFC_DRIVER_ABORTED
  2524. * bit in case it was driver
  2525. * initiated abort.
  2526. */
  2527. spin_lock_irqsave(
  2528. &phba->hbalock, iflag);
  2529. cmdiocbp->iocb_flag &=
  2530. ~LPFC_DRIVER_ABORTED;
  2531. spin_unlock_irqrestore(
  2532. &phba->hbalock, iflag);
  2533. cmdiocbp->iocb.ulpStatus =
  2534. IOSTAT_LOCAL_REJECT;
  2535. cmdiocbp->iocb.un.ulpWord[4] =
  2536. IOERR_ABORT_REQUESTED;
  2537. /*
  2538. * For SLI4, irsiocb contains
  2539. * NO_XRI in sli_xritag, it
  2540. * shall not affect releasing
  2541. * sgl (xri) process.
  2542. */
  2543. saveq->iocb.ulpStatus =
  2544. IOSTAT_LOCAL_REJECT;
  2545. saveq->iocb.un.ulpWord[4] =
  2546. IOERR_SLI_ABORTED;
  2547. spin_lock_irqsave(
  2548. &phba->hbalock, iflag);
  2549. saveq->iocb_flag |=
  2550. LPFC_DELAY_MEM_FREE;
  2551. spin_unlock_irqrestore(
  2552. &phba->hbalock, iflag);
  2553. }
  2554. }
  2555. }
  2556. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2557. } else
  2558. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2559. } else {
  2560. /*
  2561. * Unknown initiating command based on the response iotag.
  2562. * This could be the case on the ELS ring because of
  2563. * lpfc_els_abort().
  2564. */
  2565. if (pring->ringno != LPFC_ELS_RING) {
  2566. /*
  2567. * Ring <ringno> handler: unexpected completion IoTag
  2568. * <IoTag>
  2569. */
  2570. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2571. "0322 Ring %d handler: "
  2572. "unexpected completion IoTag x%x "
  2573. "Data: x%x x%x x%x x%x\n",
  2574. pring->ringno,
  2575. saveq->iocb.ulpIoTag,
  2576. saveq->iocb.ulpStatus,
  2577. saveq->iocb.un.ulpWord[4],
  2578. saveq->iocb.ulpCommand,
  2579. saveq->iocb.ulpContext);
  2580. }
  2581. }
  2582. return rc;
  2583. }
  2584. /**
  2585. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2586. * @phba: Pointer to HBA context object.
  2587. * @pring: Pointer to driver SLI ring object.
  2588. *
  2589. * This function is called from the iocb ring event handlers when
  2590. * put pointer is ahead of the get pointer for a ring. This function signal
  2591. * an error attention condition to the worker thread and the worker
  2592. * thread will transition the HBA to offline state.
  2593. **/
  2594. static void
  2595. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2596. {
  2597. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2598. /*
  2599. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2600. * rsp ring <portRspMax>
  2601. */
  2602. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2603. "0312 Ring %d handler: portRspPut %d "
  2604. "is bigger than rsp ring %d\n",
  2605. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2606. pring->sli.sli3.numRiocb);
  2607. phba->link_state = LPFC_HBA_ERROR;
  2608. /*
  2609. * All error attention handlers are posted to
  2610. * worker thread
  2611. */
  2612. phba->work_ha |= HA_ERATT;
  2613. phba->work_hs = HS_FFER3;
  2614. lpfc_worker_wake_up(phba);
  2615. return;
  2616. }
  2617. /**
  2618. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2619. * @ptr: Pointer to address of HBA context object.
  2620. *
  2621. * This function is invoked by the Error Attention polling timer when the
  2622. * timer times out. It will check the SLI Error Attention register for
  2623. * possible attention events. If so, it will post an Error Attention event
  2624. * and wake up worker thread to process it. Otherwise, it will set up the
  2625. * Error Attention polling timer for the next poll.
  2626. **/
  2627. void lpfc_poll_eratt(unsigned long ptr)
  2628. {
  2629. struct lpfc_hba *phba;
  2630. uint32_t eratt = 0;
  2631. uint64_t sli_intr, cnt;
  2632. phba = (struct lpfc_hba *)ptr;
  2633. /* Here we will also keep track of interrupts per sec of the hba */
  2634. sli_intr = phba->sli.slistat.sli_intr;
  2635. if (phba->sli.slistat.sli_prev_intr > sli_intr)
  2636. cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
  2637. sli_intr);
  2638. else
  2639. cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
  2640. /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
  2641. do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
  2642. phba->sli.slistat.sli_ips = cnt;
  2643. phba->sli.slistat.sli_prev_intr = sli_intr;
  2644. /* Check chip HA register for error event */
  2645. eratt = lpfc_sli_check_eratt(phba);
  2646. if (eratt)
  2647. /* Tell the worker thread there is work to do */
  2648. lpfc_worker_wake_up(phba);
  2649. else
  2650. /* Restart the timer for next eratt poll */
  2651. mod_timer(&phba->eratt_poll,
  2652. jiffies +
  2653. msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
  2654. return;
  2655. }
  2656. /**
  2657. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2658. * @phba: Pointer to HBA context object.
  2659. * @pring: Pointer to driver SLI ring object.
  2660. * @mask: Host attention register mask for this ring.
  2661. *
  2662. * This function is called from the interrupt context when there is a ring
  2663. * event for the fcp ring. The caller does not hold any lock.
  2664. * The function processes each response iocb in the response ring until it
  2665. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2666. * LE bit set. The function will call the completion handler of the command iocb
  2667. * if the response iocb indicates a completion for a command iocb or it is
  2668. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2669. * function if this is an unsolicited iocb.
  2670. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2671. * to check it explicitly.
  2672. */
  2673. int
  2674. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2675. struct lpfc_sli_ring *pring, uint32_t mask)
  2676. {
  2677. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2678. IOCB_t *irsp = NULL;
  2679. IOCB_t *entry = NULL;
  2680. struct lpfc_iocbq *cmdiocbq = NULL;
  2681. struct lpfc_iocbq rspiocbq;
  2682. uint32_t status;
  2683. uint32_t portRspPut, portRspMax;
  2684. int rc = 1;
  2685. lpfc_iocb_type type;
  2686. unsigned long iflag;
  2687. uint32_t rsp_cmpl = 0;
  2688. spin_lock_irqsave(&phba->hbalock, iflag);
  2689. pring->stats.iocb_event++;
  2690. /*
  2691. * The next available response entry should never exceed the maximum
  2692. * entries. If it does, treat it as an adapter hardware error.
  2693. */
  2694. portRspMax = pring->sli.sli3.numRiocb;
  2695. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2696. if (unlikely(portRspPut >= portRspMax)) {
  2697. lpfc_sli_rsp_pointers_error(phba, pring);
  2698. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2699. return 1;
  2700. }
  2701. if (phba->fcp_ring_in_use) {
  2702. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2703. return 1;
  2704. } else
  2705. phba->fcp_ring_in_use = 1;
  2706. rmb();
  2707. while (pring->sli.sli3.rspidx != portRspPut) {
  2708. /*
  2709. * Fetch an entry off the ring and copy it into a local data
  2710. * structure. The copy involves a byte-swap since the
  2711. * network byte order and pci byte orders are different.
  2712. */
  2713. entry = lpfc_resp_iocb(phba, pring);
  2714. phba->last_completion_time = jiffies;
  2715. if (++pring->sli.sli3.rspidx >= portRspMax)
  2716. pring->sli.sli3.rspidx = 0;
  2717. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2718. (uint32_t *) &rspiocbq.iocb,
  2719. phba->iocb_rsp_size);
  2720. INIT_LIST_HEAD(&(rspiocbq.list));
  2721. irsp = &rspiocbq.iocb;
  2722. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2723. pring->stats.iocb_rsp++;
  2724. rsp_cmpl++;
  2725. if (unlikely(irsp->ulpStatus)) {
  2726. /*
  2727. * If resource errors reported from HBA, reduce
  2728. * queuedepths of the SCSI device.
  2729. */
  2730. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2731. ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
  2732. IOERR_NO_RESOURCES)) {
  2733. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2734. phba->lpfc_rampdown_queue_depth(phba);
  2735. spin_lock_irqsave(&phba->hbalock, iflag);
  2736. }
  2737. /* Rsp ring <ringno> error: IOCB */
  2738. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2739. "0336 Rsp Ring %d error: IOCB Data: "
  2740. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2741. pring->ringno,
  2742. irsp->un.ulpWord[0],
  2743. irsp->un.ulpWord[1],
  2744. irsp->un.ulpWord[2],
  2745. irsp->un.ulpWord[3],
  2746. irsp->un.ulpWord[4],
  2747. irsp->un.ulpWord[5],
  2748. *(uint32_t *)&irsp->un1,
  2749. *((uint32_t *)&irsp->un1 + 1));
  2750. }
  2751. switch (type) {
  2752. case LPFC_ABORT_IOCB:
  2753. case LPFC_SOL_IOCB:
  2754. /*
  2755. * Idle exchange closed via ABTS from port. No iocb
  2756. * resources need to be recovered.
  2757. */
  2758. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2759. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2760. "0333 IOCB cmd 0x%x"
  2761. " processed. Skipping"
  2762. " completion\n",
  2763. irsp->ulpCommand);
  2764. break;
  2765. }
  2766. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2767. &rspiocbq);
  2768. if (unlikely(!cmdiocbq))
  2769. break;
  2770. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2771. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2772. if (cmdiocbq->iocb_cmpl) {
  2773. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2774. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2775. &rspiocbq);
  2776. spin_lock_irqsave(&phba->hbalock, iflag);
  2777. }
  2778. break;
  2779. case LPFC_UNSOL_IOCB:
  2780. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2781. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2782. spin_lock_irqsave(&phba->hbalock, iflag);
  2783. break;
  2784. default:
  2785. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2786. char adaptermsg[LPFC_MAX_ADPTMSG];
  2787. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2788. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2789. MAX_MSG_DATA);
  2790. dev_warn(&((phba->pcidev)->dev),
  2791. "lpfc%d: %s\n",
  2792. phba->brd_no, adaptermsg);
  2793. } else {
  2794. /* Unknown IOCB command */
  2795. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2796. "0334 Unknown IOCB command "
  2797. "Data: x%x, x%x x%x x%x x%x\n",
  2798. type, irsp->ulpCommand,
  2799. irsp->ulpStatus,
  2800. irsp->ulpIoTag,
  2801. irsp->ulpContext);
  2802. }
  2803. break;
  2804. }
  2805. /*
  2806. * The response IOCB has been processed. Update the ring
  2807. * pointer in SLIM. If the port response put pointer has not
  2808. * been updated, sync the pgp->rspPutInx and fetch the new port
  2809. * response put pointer.
  2810. */
  2811. writel(pring->sli.sli3.rspidx,
  2812. &phba->host_gp[pring->ringno].rspGetInx);
  2813. if (pring->sli.sli3.rspidx == portRspPut)
  2814. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2815. }
  2816. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2817. pring->stats.iocb_rsp_full++;
  2818. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2819. writel(status, phba->CAregaddr);
  2820. readl(phba->CAregaddr);
  2821. }
  2822. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2823. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2824. pring->stats.iocb_cmd_empty++;
  2825. /* Force update of the local copy of cmdGetInx */
  2826. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2827. lpfc_sli_resume_iocb(phba, pring);
  2828. if ((pring->lpfc_sli_cmd_available))
  2829. (pring->lpfc_sli_cmd_available) (phba, pring);
  2830. }
  2831. phba->fcp_ring_in_use = 0;
  2832. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2833. return rc;
  2834. }
  2835. /**
  2836. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2837. * @phba: Pointer to HBA context object.
  2838. * @pring: Pointer to driver SLI ring object.
  2839. * @rspiocbp: Pointer to driver response IOCB object.
  2840. *
  2841. * This function is called from the worker thread when there is a slow-path
  2842. * response IOCB to process. This function chains all the response iocbs until
  2843. * seeing the iocb with the LE bit set. The function will call
  2844. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2845. * completion of a command iocb. The function will call the
  2846. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2847. * The function frees the resources or calls the completion handler if this
  2848. * iocb is an abort completion. The function returns NULL when the response
  2849. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2850. * this function shall chain the iocb on to the iocb_continueq and return the
  2851. * response iocb passed in.
  2852. **/
  2853. static struct lpfc_iocbq *
  2854. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2855. struct lpfc_iocbq *rspiocbp)
  2856. {
  2857. struct lpfc_iocbq *saveq;
  2858. struct lpfc_iocbq *cmdiocbp;
  2859. struct lpfc_iocbq *next_iocb;
  2860. IOCB_t *irsp = NULL;
  2861. uint32_t free_saveq;
  2862. uint8_t iocb_cmd_type;
  2863. lpfc_iocb_type type;
  2864. unsigned long iflag;
  2865. int rc;
  2866. spin_lock_irqsave(&phba->hbalock, iflag);
  2867. /* First add the response iocb to the countinueq list */
  2868. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2869. pring->iocb_continueq_cnt++;
  2870. /* Now, determine whether the list is completed for processing */
  2871. irsp = &rspiocbp->iocb;
  2872. if (irsp->ulpLe) {
  2873. /*
  2874. * By default, the driver expects to free all resources
  2875. * associated with this iocb completion.
  2876. */
  2877. free_saveq = 1;
  2878. saveq = list_get_first(&pring->iocb_continueq,
  2879. struct lpfc_iocbq, list);
  2880. irsp = &(saveq->iocb);
  2881. list_del_init(&pring->iocb_continueq);
  2882. pring->iocb_continueq_cnt = 0;
  2883. pring->stats.iocb_rsp++;
  2884. /*
  2885. * If resource errors reported from HBA, reduce
  2886. * queuedepths of the SCSI device.
  2887. */
  2888. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2889. ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
  2890. IOERR_NO_RESOURCES)) {
  2891. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2892. phba->lpfc_rampdown_queue_depth(phba);
  2893. spin_lock_irqsave(&phba->hbalock, iflag);
  2894. }
  2895. if (irsp->ulpStatus) {
  2896. /* Rsp ring <ringno> error: IOCB */
  2897. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2898. "0328 Rsp Ring %d error: "
  2899. "IOCB Data: "
  2900. "x%x x%x x%x x%x "
  2901. "x%x x%x x%x x%x "
  2902. "x%x x%x x%x x%x "
  2903. "x%x x%x x%x x%x\n",
  2904. pring->ringno,
  2905. irsp->un.ulpWord[0],
  2906. irsp->un.ulpWord[1],
  2907. irsp->un.ulpWord[2],
  2908. irsp->un.ulpWord[3],
  2909. irsp->un.ulpWord[4],
  2910. irsp->un.ulpWord[5],
  2911. *(((uint32_t *) irsp) + 6),
  2912. *(((uint32_t *) irsp) + 7),
  2913. *(((uint32_t *) irsp) + 8),
  2914. *(((uint32_t *) irsp) + 9),
  2915. *(((uint32_t *) irsp) + 10),
  2916. *(((uint32_t *) irsp) + 11),
  2917. *(((uint32_t *) irsp) + 12),
  2918. *(((uint32_t *) irsp) + 13),
  2919. *(((uint32_t *) irsp) + 14),
  2920. *(((uint32_t *) irsp) + 15));
  2921. }
  2922. /*
  2923. * Fetch the IOCB command type and call the correct completion
  2924. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2925. * get freed back to the lpfc_iocb_list by the discovery
  2926. * kernel thread.
  2927. */
  2928. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2929. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2930. switch (type) {
  2931. case LPFC_SOL_IOCB:
  2932. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2933. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2934. spin_lock_irqsave(&phba->hbalock, iflag);
  2935. break;
  2936. case LPFC_UNSOL_IOCB:
  2937. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2938. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2939. spin_lock_irqsave(&phba->hbalock, iflag);
  2940. if (!rc)
  2941. free_saveq = 0;
  2942. break;
  2943. case LPFC_ABORT_IOCB:
  2944. cmdiocbp = NULL;
  2945. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2946. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2947. saveq);
  2948. if (cmdiocbp) {
  2949. /* Call the specified completion routine */
  2950. if (cmdiocbp->iocb_cmpl) {
  2951. spin_unlock_irqrestore(&phba->hbalock,
  2952. iflag);
  2953. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2954. saveq);
  2955. spin_lock_irqsave(&phba->hbalock,
  2956. iflag);
  2957. } else
  2958. __lpfc_sli_release_iocbq(phba,
  2959. cmdiocbp);
  2960. }
  2961. break;
  2962. case LPFC_UNKNOWN_IOCB:
  2963. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2964. char adaptermsg[LPFC_MAX_ADPTMSG];
  2965. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2966. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2967. MAX_MSG_DATA);
  2968. dev_warn(&((phba->pcidev)->dev),
  2969. "lpfc%d: %s\n",
  2970. phba->brd_no, adaptermsg);
  2971. } else {
  2972. /* Unknown IOCB command */
  2973. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2974. "0335 Unknown IOCB "
  2975. "command Data: x%x "
  2976. "x%x x%x x%x\n",
  2977. irsp->ulpCommand,
  2978. irsp->ulpStatus,
  2979. irsp->ulpIoTag,
  2980. irsp->ulpContext);
  2981. }
  2982. break;
  2983. }
  2984. if (free_saveq) {
  2985. list_for_each_entry_safe(rspiocbp, next_iocb,
  2986. &saveq->list, list) {
  2987. list_del_init(&rspiocbp->list);
  2988. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2989. }
  2990. __lpfc_sli_release_iocbq(phba, saveq);
  2991. }
  2992. rspiocbp = NULL;
  2993. }
  2994. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2995. return rspiocbp;
  2996. }
  2997. /**
  2998. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2999. * @phba: Pointer to HBA context object.
  3000. * @pring: Pointer to driver SLI ring object.
  3001. * @mask: Host attention register mask for this ring.
  3002. *
  3003. * This routine wraps the actual slow_ring event process routine from the
  3004. * API jump table function pointer from the lpfc_hba struct.
  3005. **/
  3006. void
  3007. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  3008. struct lpfc_sli_ring *pring, uint32_t mask)
  3009. {
  3010. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  3011. }
  3012. /**
  3013. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  3014. * @phba: Pointer to HBA context object.
  3015. * @pring: Pointer to driver SLI ring object.
  3016. * @mask: Host attention register mask for this ring.
  3017. *
  3018. * This function is called from the worker thread when there is a ring event
  3019. * for non-fcp rings. The caller does not hold any lock. The function will
  3020. * remove each response iocb in the response ring and calls the handle
  3021. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3022. **/
  3023. static void
  3024. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  3025. struct lpfc_sli_ring *pring, uint32_t mask)
  3026. {
  3027. struct lpfc_pgp *pgp;
  3028. IOCB_t *entry;
  3029. IOCB_t *irsp = NULL;
  3030. struct lpfc_iocbq *rspiocbp = NULL;
  3031. uint32_t portRspPut, portRspMax;
  3032. unsigned long iflag;
  3033. uint32_t status;
  3034. pgp = &phba->port_gp[pring->ringno];
  3035. spin_lock_irqsave(&phba->hbalock, iflag);
  3036. pring->stats.iocb_event++;
  3037. /*
  3038. * The next available response entry should never exceed the maximum
  3039. * entries. If it does, treat it as an adapter hardware error.
  3040. */
  3041. portRspMax = pring->sli.sli3.numRiocb;
  3042. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3043. if (portRspPut >= portRspMax) {
  3044. /*
  3045. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  3046. * rsp ring <portRspMax>
  3047. */
  3048. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3049. "0303 Ring %d handler: portRspPut %d "
  3050. "is bigger than rsp ring %d\n",
  3051. pring->ringno, portRspPut, portRspMax);
  3052. phba->link_state = LPFC_HBA_ERROR;
  3053. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3054. phba->work_hs = HS_FFER3;
  3055. lpfc_handle_eratt(phba);
  3056. return;
  3057. }
  3058. rmb();
  3059. while (pring->sli.sli3.rspidx != portRspPut) {
  3060. /*
  3061. * Build a completion list and call the appropriate handler.
  3062. * The process is to get the next available response iocb, get
  3063. * a free iocb from the list, copy the response data into the
  3064. * free iocb, insert to the continuation list, and update the
  3065. * next response index to slim. This process makes response
  3066. * iocb's in the ring available to DMA as fast as possible but
  3067. * pays a penalty for a copy operation. Since the iocb is
  3068. * only 32 bytes, this penalty is considered small relative to
  3069. * the PCI reads for register values and a slim write. When
  3070. * the ulpLe field is set, the entire Command has been
  3071. * received.
  3072. */
  3073. entry = lpfc_resp_iocb(phba, pring);
  3074. phba->last_completion_time = jiffies;
  3075. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3076. if (rspiocbp == NULL) {
  3077. printk(KERN_ERR "%s: out of buffers! Failing "
  3078. "completion.\n", __func__);
  3079. break;
  3080. }
  3081. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3082. phba->iocb_rsp_size);
  3083. irsp = &rspiocbp->iocb;
  3084. if (++pring->sli.sli3.rspidx >= portRspMax)
  3085. pring->sli.sli3.rspidx = 0;
  3086. if (pring->ringno == LPFC_ELS_RING) {
  3087. lpfc_debugfs_slow_ring_trc(phba,
  3088. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3089. *(((uint32_t *) irsp) + 4),
  3090. *(((uint32_t *) irsp) + 6),
  3091. *(((uint32_t *) irsp) + 7));
  3092. }
  3093. writel(pring->sli.sli3.rspidx,
  3094. &phba->host_gp[pring->ringno].rspGetInx);
  3095. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3096. /* Handle the response IOCB */
  3097. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3098. spin_lock_irqsave(&phba->hbalock, iflag);
  3099. /*
  3100. * If the port response put pointer has not been updated, sync
  3101. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3102. * response put pointer.
  3103. */
  3104. if (pring->sli.sli3.rspidx == portRspPut) {
  3105. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3106. }
  3107. } /* while (pring->sli.sli3.rspidx != portRspPut) */
  3108. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3109. /* At least one response entry has been freed */
  3110. pring->stats.iocb_rsp_full++;
  3111. /* SET RxRE_RSP in Chip Att register */
  3112. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3113. writel(status, phba->CAregaddr);
  3114. readl(phba->CAregaddr); /* flush */
  3115. }
  3116. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3117. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3118. pring->stats.iocb_cmd_empty++;
  3119. /* Force update of the local copy of cmdGetInx */
  3120. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3121. lpfc_sli_resume_iocb(phba, pring);
  3122. if ((pring->lpfc_sli_cmd_available))
  3123. (pring->lpfc_sli_cmd_available) (phba, pring);
  3124. }
  3125. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3126. return;
  3127. }
  3128. /**
  3129. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3130. * @phba: Pointer to HBA context object.
  3131. * @pring: Pointer to driver SLI ring object.
  3132. * @mask: Host attention register mask for this ring.
  3133. *
  3134. * This function is called from the worker thread when there is a pending
  3135. * ELS response iocb on the driver internal slow-path response iocb worker
  3136. * queue. The caller does not hold any lock. The function will remove each
  3137. * response iocb from the response worker queue and calls the handle
  3138. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3139. **/
  3140. static void
  3141. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3142. struct lpfc_sli_ring *pring, uint32_t mask)
  3143. {
  3144. struct lpfc_iocbq *irspiocbq;
  3145. struct hbq_dmabuf *dmabuf;
  3146. struct lpfc_cq_event *cq_event;
  3147. unsigned long iflag;
  3148. spin_lock_irqsave(&phba->hbalock, iflag);
  3149. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3150. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3151. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3152. /* Get the response iocb from the head of work queue */
  3153. spin_lock_irqsave(&phba->hbalock, iflag);
  3154. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3155. cq_event, struct lpfc_cq_event, list);
  3156. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3157. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3158. case CQE_CODE_COMPL_WQE:
  3159. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3160. cq_event);
  3161. /* Translate ELS WCQE to response IOCBQ */
  3162. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3163. irspiocbq);
  3164. if (irspiocbq)
  3165. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3166. irspiocbq);
  3167. break;
  3168. case CQE_CODE_RECEIVE:
  3169. case CQE_CODE_RECEIVE_V1:
  3170. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3171. cq_event);
  3172. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3173. break;
  3174. default:
  3175. break;
  3176. }
  3177. }
  3178. }
  3179. /**
  3180. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3181. * @phba: Pointer to HBA context object.
  3182. * @pring: Pointer to driver SLI ring object.
  3183. *
  3184. * This function aborts all iocbs in the given ring and frees all the iocb
  3185. * objects in txq. This function issues an abort iocb for all the iocb commands
  3186. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3187. * the return of this function. The caller is not required to hold any locks.
  3188. **/
  3189. void
  3190. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3191. {
  3192. LIST_HEAD(completions);
  3193. struct lpfc_iocbq *iocb, *next_iocb;
  3194. if (pring->ringno == LPFC_ELS_RING) {
  3195. lpfc_fabric_abort_hba(phba);
  3196. }
  3197. /* Error everything on txq and txcmplq
  3198. * First do the txq.
  3199. */
  3200. if (phba->sli_rev >= LPFC_SLI_REV4) {
  3201. spin_lock_irq(&pring->ring_lock);
  3202. list_splice_init(&pring->txq, &completions);
  3203. pring->txq_cnt = 0;
  3204. spin_unlock_irq(&pring->ring_lock);
  3205. spin_lock_irq(&phba->hbalock);
  3206. /* Next issue ABTS for everything on the txcmplq */
  3207. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3208. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3209. spin_unlock_irq(&phba->hbalock);
  3210. } else {
  3211. spin_lock_irq(&phba->hbalock);
  3212. list_splice_init(&pring->txq, &completions);
  3213. pring->txq_cnt = 0;
  3214. /* Next issue ABTS for everything on the txcmplq */
  3215. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3216. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3217. spin_unlock_irq(&phba->hbalock);
  3218. }
  3219. /* Cancel all the IOCBs from the completions list */
  3220. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3221. IOERR_SLI_ABORTED);
  3222. }
  3223. /**
  3224. * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
  3225. * @phba: Pointer to HBA context object.
  3226. * @pring: Pointer to driver SLI ring object.
  3227. *
  3228. * This function aborts all iocbs in FCP rings and frees all the iocb
  3229. * objects in txq. This function issues an abort iocb for all the iocb commands
  3230. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3231. * the return of this function. The caller is not required to hold any locks.
  3232. **/
  3233. void
  3234. lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
  3235. {
  3236. struct lpfc_sli *psli = &phba->sli;
  3237. struct lpfc_sli_ring *pring;
  3238. uint32_t i;
  3239. /* Look on all the FCP Rings for the iotag */
  3240. if (phba->sli_rev >= LPFC_SLI_REV4) {
  3241. for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
  3242. pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
  3243. lpfc_sli_abort_iocb_ring(phba, pring);
  3244. }
  3245. } else {
  3246. pring = &psli->ring[psli->fcp_ring];
  3247. lpfc_sli_abort_iocb_ring(phba, pring);
  3248. }
  3249. }
  3250. /**
  3251. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3252. * @phba: Pointer to HBA context object.
  3253. *
  3254. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3255. * objects in txq and txcmplq. This function will not issue abort iocbs
  3256. * for all the iocb commands in txcmplq, they will just be returned with
  3257. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3258. * slot has been permanently disabled.
  3259. **/
  3260. void
  3261. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3262. {
  3263. LIST_HEAD(txq);
  3264. LIST_HEAD(txcmplq);
  3265. struct lpfc_sli *psli = &phba->sli;
  3266. struct lpfc_sli_ring *pring;
  3267. uint32_t i;
  3268. spin_lock_irq(&phba->hbalock);
  3269. /* Indicate the I/O queues are flushed */
  3270. phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
  3271. spin_unlock_irq(&phba->hbalock);
  3272. /* Look on all the FCP Rings for the iotag */
  3273. if (phba->sli_rev >= LPFC_SLI_REV4) {
  3274. for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
  3275. pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
  3276. spin_lock_irq(&pring->ring_lock);
  3277. /* Retrieve everything on txq */
  3278. list_splice_init(&pring->txq, &txq);
  3279. /* Retrieve everything on the txcmplq */
  3280. list_splice_init(&pring->txcmplq, &txcmplq);
  3281. pring->txq_cnt = 0;
  3282. pring->txcmplq_cnt = 0;
  3283. spin_unlock_irq(&pring->ring_lock);
  3284. /* Flush the txq */
  3285. lpfc_sli_cancel_iocbs(phba, &txq,
  3286. IOSTAT_LOCAL_REJECT,
  3287. IOERR_SLI_DOWN);
  3288. /* Flush the txcmpq */
  3289. lpfc_sli_cancel_iocbs(phba, &txcmplq,
  3290. IOSTAT_LOCAL_REJECT,
  3291. IOERR_SLI_DOWN);
  3292. }
  3293. } else {
  3294. pring = &psli->ring[psli->fcp_ring];
  3295. spin_lock_irq(&phba->hbalock);
  3296. /* Retrieve everything on txq */
  3297. list_splice_init(&pring->txq, &txq);
  3298. /* Retrieve everything on the txcmplq */
  3299. list_splice_init(&pring->txcmplq, &txcmplq);
  3300. pring->txq_cnt = 0;
  3301. pring->txcmplq_cnt = 0;
  3302. spin_unlock_irq(&phba->hbalock);
  3303. /* Flush the txq */
  3304. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3305. IOERR_SLI_DOWN);
  3306. /* Flush the txcmpq */
  3307. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3308. IOERR_SLI_DOWN);
  3309. }
  3310. }
  3311. /**
  3312. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3313. * @phba: Pointer to HBA context object.
  3314. * @mask: Bit mask to be checked.
  3315. *
  3316. * This function reads the host status register and compares
  3317. * with the provided bit mask to check if HBA completed
  3318. * the restart. This function will wait in a loop for the
  3319. * HBA to complete restart. If the HBA does not restart within
  3320. * 15 iterations, the function will reset the HBA again. The
  3321. * function returns 1 when HBA fail to restart otherwise returns
  3322. * zero.
  3323. **/
  3324. static int
  3325. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3326. {
  3327. uint32_t status;
  3328. int i = 0;
  3329. int retval = 0;
  3330. /* Read the HBA Host Status Register */
  3331. if (lpfc_readl(phba->HSregaddr, &status))
  3332. return 1;
  3333. /*
  3334. * Check status register every 100ms for 5 retries, then every
  3335. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3336. * every 2.5 sec for 4.
  3337. * Break our of the loop if errors occurred during init.
  3338. */
  3339. while (((status & mask) != mask) &&
  3340. !(status & HS_FFERM) &&
  3341. i++ < 20) {
  3342. if (i <= 5)
  3343. msleep(10);
  3344. else if (i <= 10)
  3345. msleep(500);
  3346. else
  3347. msleep(2500);
  3348. if (i == 15) {
  3349. /* Do post */
  3350. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3351. lpfc_sli_brdrestart(phba);
  3352. }
  3353. /* Read the HBA Host Status Register */
  3354. if (lpfc_readl(phba->HSregaddr, &status)) {
  3355. retval = 1;
  3356. break;
  3357. }
  3358. }
  3359. /* Check to see if any errors occurred during init */
  3360. if ((status & HS_FFERM) || (i >= 20)) {
  3361. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3362. "2751 Adapter failed to restart, "
  3363. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3364. status,
  3365. readl(phba->MBslimaddr + 0xa8),
  3366. readl(phba->MBslimaddr + 0xac));
  3367. phba->link_state = LPFC_HBA_ERROR;
  3368. retval = 1;
  3369. }
  3370. return retval;
  3371. }
  3372. /**
  3373. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3374. * @phba: Pointer to HBA context object.
  3375. * @mask: Bit mask to be checked.
  3376. *
  3377. * This function checks the host status register to check if HBA is
  3378. * ready. This function will wait in a loop for the HBA to be ready
  3379. * If the HBA is not ready , the function will will reset the HBA PCI
  3380. * function again. The function returns 1 when HBA fail to be ready
  3381. * otherwise returns zero.
  3382. **/
  3383. static int
  3384. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3385. {
  3386. uint32_t status;
  3387. int retval = 0;
  3388. /* Read the HBA Host Status Register */
  3389. status = lpfc_sli4_post_status_check(phba);
  3390. if (status) {
  3391. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3392. lpfc_sli_brdrestart(phba);
  3393. status = lpfc_sli4_post_status_check(phba);
  3394. }
  3395. /* Check to see if any errors occurred during init */
  3396. if (status) {
  3397. phba->link_state = LPFC_HBA_ERROR;
  3398. retval = 1;
  3399. } else
  3400. phba->sli4_hba.intr_enable = 0;
  3401. return retval;
  3402. }
  3403. /**
  3404. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3405. * @phba: Pointer to HBA context object.
  3406. * @mask: Bit mask to be checked.
  3407. *
  3408. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3409. * from the API jump table function pointer from the lpfc_hba struct.
  3410. **/
  3411. int
  3412. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3413. {
  3414. return phba->lpfc_sli_brdready(phba, mask);
  3415. }
  3416. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3417. /**
  3418. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3419. * @phba: Pointer to HBA context object.
  3420. *
  3421. * This function is called before resetting an HBA. This function is called
  3422. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3423. **/
  3424. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3425. {
  3426. uint32_t __iomem *resp_buf;
  3427. uint32_t __iomem *mbox_buf;
  3428. volatile uint32_t mbox;
  3429. uint32_t hc_copy, ha_copy, resp_data;
  3430. int i;
  3431. uint8_t hdrtype;
  3432. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3433. if (hdrtype != 0x80 ||
  3434. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3435. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3436. return;
  3437. /*
  3438. * Tell the other part of the chip to suspend temporarily all
  3439. * its DMA activity.
  3440. */
  3441. resp_buf = phba->MBslimaddr;
  3442. /* Disable the error attention */
  3443. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3444. return;
  3445. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3446. readl(phba->HCregaddr); /* flush */
  3447. phba->link_flag |= LS_IGNORE_ERATT;
  3448. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3449. return;
  3450. if (ha_copy & HA_ERATT) {
  3451. /* Clear Chip error bit */
  3452. writel(HA_ERATT, phba->HAregaddr);
  3453. phba->pport->stopped = 1;
  3454. }
  3455. mbox = 0;
  3456. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3457. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3458. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3459. mbox_buf = phba->MBslimaddr;
  3460. writel(mbox, mbox_buf);
  3461. for (i = 0; i < 50; i++) {
  3462. if (lpfc_readl((resp_buf + 1), &resp_data))
  3463. return;
  3464. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3465. mdelay(1);
  3466. else
  3467. break;
  3468. }
  3469. resp_data = 0;
  3470. if (lpfc_readl((resp_buf + 1), &resp_data))
  3471. return;
  3472. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3473. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3474. phba->pport->stopped)
  3475. goto restore_hc;
  3476. else
  3477. goto clear_errat;
  3478. }
  3479. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3480. resp_data = 0;
  3481. for (i = 0; i < 500; i++) {
  3482. if (lpfc_readl(resp_buf, &resp_data))
  3483. return;
  3484. if (resp_data != mbox)
  3485. mdelay(1);
  3486. else
  3487. break;
  3488. }
  3489. clear_errat:
  3490. while (++i < 500) {
  3491. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3492. return;
  3493. if (!(ha_copy & HA_ERATT))
  3494. mdelay(1);
  3495. else
  3496. break;
  3497. }
  3498. if (readl(phba->HAregaddr) & HA_ERATT) {
  3499. writel(HA_ERATT, phba->HAregaddr);
  3500. phba->pport->stopped = 1;
  3501. }
  3502. restore_hc:
  3503. phba->link_flag &= ~LS_IGNORE_ERATT;
  3504. writel(hc_copy, phba->HCregaddr);
  3505. readl(phba->HCregaddr); /* flush */
  3506. }
  3507. /**
  3508. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3509. * @phba: Pointer to HBA context object.
  3510. *
  3511. * This function issues a kill_board mailbox command and waits for
  3512. * the error attention interrupt. This function is called for stopping
  3513. * the firmware processing. The caller is not required to hold any
  3514. * locks. This function calls lpfc_hba_down_post function to free
  3515. * any pending commands after the kill. The function will return 1 when it
  3516. * fails to kill the board else will return 0.
  3517. **/
  3518. int
  3519. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3520. {
  3521. struct lpfc_sli *psli;
  3522. LPFC_MBOXQ_t *pmb;
  3523. uint32_t status;
  3524. uint32_t ha_copy;
  3525. int retval;
  3526. int i = 0;
  3527. psli = &phba->sli;
  3528. /* Kill HBA */
  3529. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3530. "0329 Kill HBA Data: x%x x%x\n",
  3531. phba->pport->port_state, psli->sli_flag);
  3532. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3533. if (!pmb)
  3534. return 1;
  3535. /* Disable the error attention */
  3536. spin_lock_irq(&phba->hbalock);
  3537. if (lpfc_readl(phba->HCregaddr, &status)) {
  3538. spin_unlock_irq(&phba->hbalock);
  3539. mempool_free(pmb, phba->mbox_mem_pool);
  3540. return 1;
  3541. }
  3542. status &= ~HC_ERINT_ENA;
  3543. writel(status, phba->HCregaddr);
  3544. readl(phba->HCregaddr); /* flush */
  3545. phba->link_flag |= LS_IGNORE_ERATT;
  3546. spin_unlock_irq(&phba->hbalock);
  3547. lpfc_kill_board(phba, pmb);
  3548. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3549. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3550. if (retval != MBX_SUCCESS) {
  3551. if (retval != MBX_BUSY)
  3552. mempool_free(pmb, phba->mbox_mem_pool);
  3553. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3554. "2752 KILL_BOARD command failed retval %d\n",
  3555. retval);
  3556. spin_lock_irq(&phba->hbalock);
  3557. phba->link_flag &= ~LS_IGNORE_ERATT;
  3558. spin_unlock_irq(&phba->hbalock);
  3559. return 1;
  3560. }
  3561. spin_lock_irq(&phba->hbalock);
  3562. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3563. spin_unlock_irq(&phba->hbalock);
  3564. mempool_free(pmb, phba->mbox_mem_pool);
  3565. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3566. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3567. * 3 seconds we still set HBA_ERROR state because the status of the
  3568. * board is now undefined.
  3569. */
  3570. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3571. return 1;
  3572. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3573. mdelay(100);
  3574. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3575. return 1;
  3576. }
  3577. del_timer_sync(&psli->mbox_tmo);
  3578. if (ha_copy & HA_ERATT) {
  3579. writel(HA_ERATT, phba->HAregaddr);
  3580. phba->pport->stopped = 1;
  3581. }
  3582. spin_lock_irq(&phba->hbalock);
  3583. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3584. psli->mbox_active = NULL;
  3585. phba->link_flag &= ~LS_IGNORE_ERATT;
  3586. spin_unlock_irq(&phba->hbalock);
  3587. lpfc_hba_down_post(phba);
  3588. phba->link_state = LPFC_HBA_ERROR;
  3589. return ha_copy & HA_ERATT ? 0 : 1;
  3590. }
  3591. /**
  3592. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3593. * @phba: Pointer to HBA context object.
  3594. *
  3595. * This function resets the HBA by writing HC_INITFF to the control
  3596. * register. After the HBA resets, this function resets all the iocb ring
  3597. * indices. This function disables PCI layer parity checking during
  3598. * the reset.
  3599. * This function returns 0 always.
  3600. * The caller is not required to hold any locks.
  3601. **/
  3602. int
  3603. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3604. {
  3605. struct lpfc_sli *psli;
  3606. struct lpfc_sli_ring *pring;
  3607. uint16_t cfg_value;
  3608. int i;
  3609. psli = &phba->sli;
  3610. /* Reset HBA */
  3611. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3612. "0325 Reset HBA Data: x%x x%x\n",
  3613. phba->pport->port_state, psli->sli_flag);
  3614. /* perform board reset */
  3615. phba->fc_eventTag = 0;
  3616. phba->link_events = 0;
  3617. phba->pport->fc_myDID = 0;
  3618. phba->pport->fc_prevDID = 0;
  3619. /* Turn off parity checking and serr during the physical reset */
  3620. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3621. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3622. (cfg_value &
  3623. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3624. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3625. /* Now toggle INITFF bit in the Host Control Register */
  3626. writel(HC_INITFF, phba->HCregaddr);
  3627. mdelay(1);
  3628. readl(phba->HCregaddr); /* flush */
  3629. writel(0, phba->HCregaddr);
  3630. readl(phba->HCregaddr); /* flush */
  3631. /* Restore PCI cmd register */
  3632. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3633. /* Initialize relevant SLI info */
  3634. for (i = 0; i < psli->num_rings; i++) {
  3635. pring = &psli->ring[i];
  3636. pring->flag = 0;
  3637. pring->sli.sli3.rspidx = 0;
  3638. pring->sli.sli3.next_cmdidx = 0;
  3639. pring->sli.sli3.local_getidx = 0;
  3640. pring->sli.sli3.cmdidx = 0;
  3641. pring->missbufcnt = 0;
  3642. }
  3643. phba->link_state = LPFC_WARM_START;
  3644. return 0;
  3645. }
  3646. /**
  3647. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3648. * @phba: Pointer to HBA context object.
  3649. *
  3650. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3651. * checking during resets the device. The caller is not required to hold
  3652. * any locks.
  3653. *
  3654. * This function returns 0 always.
  3655. **/
  3656. int
  3657. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3658. {
  3659. struct lpfc_sli *psli = &phba->sli;
  3660. uint16_t cfg_value;
  3661. int rc = 0;
  3662. /* Reset HBA */
  3663. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3664. "0295 Reset HBA Data: x%x x%x x%x\n",
  3665. phba->pport->port_state, psli->sli_flag,
  3666. phba->hba_flag);
  3667. /* perform board reset */
  3668. phba->fc_eventTag = 0;
  3669. phba->link_events = 0;
  3670. phba->pport->fc_myDID = 0;
  3671. phba->pport->fc_prevDID = 0;
  3672. spin_lock_irq(&phba->hbalock);
  3673. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3674. phba->fcf.fcf_flag = 0;
  3675. spin_unlock_irq(&phba->hbalock);
  3676. /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
  3677. if (phba->hba_flag & HBA_FW_DUMP_OP) {
  3678. phba->hba_flag &= ~HBA_FW_DUMP_OP;
  3679. return rc;
  3680. }
  3681. /* Now physically reset the device */
  3682. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3683. "0389 Performing PCI function reset!\n");
  3684. /* Turn off parity checking and serr during the physical reset */
  3685. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3686. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3687. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3688. /* Perform FCoE PCI function reset before freeing queue memory */
  3689. rc = lpfc_pci_function_reset(phba);
  3690. lpfc_sli4_queue_destroy(phba);
  3691. /* Restore PCI cmd register */
  3692. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3693. return rc;
  3694. }
  3695. /**
  3696. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3697. * @phba: Pointer to HBA context object.
  3698. *
  3699. * This function is called in the SLI initialization code path to
  3700. * restart the HBA. The caller is not required to hold any lock.
  3701. * This function writes MBX_RESTART mailbox command to the SLIM and
  3702. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3703. * function to free any pending commands. The function enables
  3704. * POST only during the first initialization. The function returns zero.
  3705. * The function does not guarantee completion of MBX_RESTART mailbox
  3706. * command before the return of this function.
  3707. **/
  3708. static int
  3709. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3710. {
  3711. MAILBOX_t *mb;
  3712. struct lpfc_sli *psli;
  3713. volatile uint32_t word0;
  3714. void __iomem *to_slim;
  3715. uint32_t hba_aer_enabled;
  3716. spin_lock_irq(&phba->hbalock);
  3717. /* Take PCIe device Advanced Error Reporting (AER) state */
  3718. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3719. psli = &phba->sli;
  3720. /* Restart HBA */
  3721. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3722. "0337 Restart HBA Data: x%x x%x\n",
  3723. phba->pport->port_state, psli->sli_flag);
  3724. word0 = 0;
  3725. mb = (MAILBOX_t *) &word0;
  3726. mb->mbxCommand = MBX_RESTART;
  3727. mb->mbxHc = 1;
  3728. lpfc_reset_barrier(phba);
  3729. to_slim = phba->MBslimaddr;
  3730. writel(*(uint32_t *) mb, to_slim);
  3731. readl(to_slim); /* flush */
  3732. /* Only skip post after fc_ffinit is completed */
  3733. if (phba->pport->port_state)
  3734. word0 = 1; /* This is really setting up word1 */
  3735. else
  3736. word0 = 0; /* This is really setting up word1 */
  3737. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3738. writel(*(uint32_t *) mb, to_slim);
  3739. readl(to_slim); /* flush */
  3740. lpfc_sli_brdreset(phba);
  3741. phba->pport->stopped = 0;
  3742. phba->link_state = LPFC_INIT_START;
  3743. phba->hba_flag = 0;
  3744. spin_unlock_irq(&phba->hbalock);
  3745. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3746. psli->stats_start = get_seconds();
  3747. /* Give the INITFF and Post time to settle. */
  3748. mdelay(100);
  3749. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3750. if (hba_aer_enabled)
  3751. pci_disable_pcie_error_reporting(phba->pcidev);
  3752. lpfc_hba_down_post(phba);
  3753. return 0;
  3754. }
  3755. /**
  3756. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3757. * @phba: Pointer to HBA context object.
  3758. *
  3759. * This function is called in the SLI initialization code path to restart
  3760. * a SLI4 HBA. The caller is not required to hold any lock.
  3761. * At the end of the function, it calls lpfc_hba_down_post function to
  3762. * free any pending commands.
  3763. **/
  3764. static int
  3765. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3766. {
  3767. struct lpfc_sli *psli = &phba->sli;
  3768. uint32_t hba_aer_enabled;
  3769. int rc;
  3770. /* Restart HBA */
  3771. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3772. "0296 Restart HBA Data: x%x x%x\n",
  3773. phba->pport->port_state, psli->sli_flag);
  3774. /* Take PCIe device Advanced Error Reporting (AER) state */
  3775. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3776. rc = lpfc_sli4_brdreset(phba);
  3777. spin_lock_irq(&phba->hbalock);
  3778. phba->pport->stopped = 0;
  3779. phba->link_state = LPFC_INIT_START;
  3780. phba->hba_flag = 0;
  3781. spin_unlock_irq(&phba->hbalock);
  3782. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3783. psli->stats_start = get_seconds();
  3784. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3785. if (hba_aer_enabled)
  3786. pci_disable_pcie_error_reporting(phba->pcidev);
  3787. lpfc_hba_down_post(phba);
  3788. return rc;
  3789. }
  3790. /**
  3791. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3792. * @phba: Pointer to HBA context object.
  3793. *
  3794. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3795. * API jump table function pointer from the lpfc_hba struct.
  3796. **/
  3797. int
  3798. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3799. {
  3800. return phba->lpfc_sli_brdrestart(phba);
  3801. }
  3802. /**
  3803. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3804. * @phba: Pointer to HBA context object.
  3805. *
  3806. * This function is called after a HBA restart to wait for successful
  3807. * restart of the HBA. Successful restart of the HBA is indicated by
  3808. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3809. * iteration, the function will restart the HBA again. The function returns
  3810. * zero if HBA successfully restarted else returns negative error code.
  3811. **/
  3812. static int
  3813. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3814. {
  3815. uint32_t status, i = 0;
  3816. /* Read the HBA Host Status Register */
  3817. if (lpfc_readl(phba->HSregaddr, &status))
  3818. return -EIO;
  3819. /* Check status register to see what current state is */
  3820. i = 0;
  3821. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3822. /* Check every 10ms for 10 retries, then every 100ms for 90
  3823. * retries, then every 1 sec for 50 retires for a total of
  3824. * ~60 seconds before reset the board again and check every
  3825. * 1 sec for 50 retries. The up to 60 seconds before the
  3826. * board ready is required by the Falcon FIPS zeroization
  3827. * complete, and any reset the board in between shall cause
  3828. * restart of zeroization, further delay the board ready.
  3829. */
  3830. if (i++ >= 200) {
  3831. /* Adapter failed to init, timeout, status reg
  3832. <status> */
  3833. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3834. "0436 Adapter failed to init, "
  3835. "timeout, status reg x%x, "
  3836. "FW Data: A8 x%x AC x%x\n", status,
  3837. readl(phba->MBslimaddr + 0xa8),
  3838. readl(phba->MBslimaddr + 0xac));
  3839. phba->link_state = LPFC_HBA_ERROR;
  3840. return -ETIMEDOUT;
  3841. }
  3842. /* Check to see if any errors occurred during init */
  3843. if (status & HS_FFERM) {
  3844. /* ERROR: During chipset initialization */
  3845. /* Adapter failed to init, chipset, status reg
  3846. <status> */
  3847. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3848. "0437 Adapter failed to init, "
  3849. "chipset, status reg x%x, "
  3850. "FW Data: A8 x%x AC x%x\n", status,
  3851. readl(phba->MBslimaddr + 0xa8),
  3852. readl(phba->MBslimaddr + 0xac));
  3853. phba->link_state = LPFC_HBA_ERROR;
  3854. return -EIO;
  3855. }
  3856. if (i <= 10)
  3857. msleep(10);
  3858. else if (i <= 100)
  3859. msleep(100);
  3860. else
  3861. msleep(1000);
  3862. if (i == 150) {
  3863. /* Do post */
  3864. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3865. lpfc_sli_brdrestart(phba);
  3866. }
  3867. /* Read the HBA Host Status Register */
  3868. if (lpfc_readl(phba->HSregaddr, &status))
  3869. return -EIO;
  3870. }
  3871. /* Check to see if any errors occurred during init */
  3872. if (status & HS_FFERM) {
  3873. /* ERROR: During chipset initialization */
  3874. /* Adapter failed to init, chipset, status reg <status> */
  3875. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3876. "0438 Adapter failed to init, chipset, "
  3877. "status reg x%x, "
  3878. "FW Data: A8 x%x AC x%x\n", status,
  3879. readl(phba->MBslimaddr + 0xa8),
  3880. readl(phba->MBslimaddr + 0xac));
  3881. phba->link_state = LPFC_HBA_ERROR;
  3882. return -EIO;
  3883. }
  3884. /* Clear all interrupt enable conditions */
  3885. writel(0, phba->HCregaddr);
  3886. readl(phba->HCregaddr); /* flush */
  3887. /* setup host attn register */
  3888. writel(0xffffffff, phba->HAregaddr);
  3889. readl(phba->HAregaddr); /* flush */
  3890. return 0;
  3891. }
  3892. /**
  3893. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3894. *
  3895. * This function calculates and returns the number of HBQs required to be
  3896. * configured.
  3897. **/
  3898. int
  3899. lpfc_sli_hbq_count(void)
  3900. {
  3901. return ARRAY_SIZE(lpfc_hbq_defs);
  3902. }
  3903. /**
  3904. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3905. *
  3906. * This function adds the number of hbq entries in every HBQ to get
  3907. * the total number of hbq entries required for the HBA and returns
  3908. * the total count.
  3909. **/
  3910. static int
  3911. lpfc_sli_hbq_entry_count(void)
  3912. {
  3913. int hbq_count = lpfc_sli_hbq_count();
  3914. int count = 0;
  3915. int i;
  3916. for (i = 0; i < hbq_count; ++i)
  3917. count += lpfc_hbq_defs[i]->entry_count;
  3918. return count;
  3919. }
  3920. /**
  3921. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3922. *
  3923. * This function calculates amount of memory required for all hbq entries
  3924. * to be configured and returns the total memory required.
  3925. **/
  3926. int
  3927. lpfc_sli_hbq_size(void)
  3928. {
  3929. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3930. }
  3931. /**
  3932. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3933. * @phba: Pointer to HBA context object.
  3934. *
  3935. * This function is called during the SLI initialization to configure
  3936. * all the HBQs and post buffers to the HBQ. The caller is not
  3937. * required to hold any locks. This function will return zero if successful
  3938. * else it will return negative error code.
  3939. **/
  3940. static int
  3941. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3942. {
  3943. int hbq_count = lpfc_sli_hbq_count();
  3944. LPFC_MBOXQ_t *pmb;
  3945. MAILBOX_t *pmbox;
  3946. uint32_t hbqno;
  3947. uint32_t hbq_entry_index;
  3948. /* Get a Mailbox buffer to setup mailbox
  3949. * commands for HBA initialization
  3950. */
  3951. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3952. if (!pmb)
  3953. return -ENOMEM;
  3954. pmbox = &pmb->u.mb;
  3955. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3956. phba->link_state = LPFC_INIT_MBX_CMDS;
  3957. phba->hbq_in_use = 1;
  3958. hbq_entry_index = 0;
  3959. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3960. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3961. phba->hbqs[hbqno].hbqPutIdx = 0;
  3962. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3963. phba->hbqs[hbqno].entry_count =
  3964. lpfc_hbq_defs[hbqno]->entry_count;
  3965. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3966. hbq_entry_index, pmb);
  3967. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3968. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3969. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3970. mbxStatus <status>, ring <num> */
  3971. lpfc_printf_log(phba, KERN_ERR,
  3972. LOG_SLI | LOG_VPORT,
  3973. "1805 Adapter failed to init. "
  3974. "Data: x%x x%x x%x\n",
  3975. pmbox->mbxCommand,
  3976. pmbox->mbxStatus, hbqno);
  3977. phba->link_state = LPFC_HBA_ERROR;
  3978. mempool_free(pmb, phba->mbox_mem_pool);
  3979. return -ENXIO;
  3980. }
  3981. }
  3982. phba->hbq_count = hbq_count;
  3983. mempool_free(pmb, phba->mbox_mem_pool);
  3984. /* Initially populate or replenish the HBQs */
  3985. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3986. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3987. return 0;
  3988. }
  3989. /**
  3990. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  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_sli4_rb_setup(struct lpfc_hba *phba)
  4000. {
  4001. phba->hbq_in_use = 1;
  4002. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  4003. phba->hbq_count = 1;
  4004. /* Initially populate or replenish the HBQs */
  4005. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  4006. return 0;
  4007. }
  4008. /**
  4009. * lpfc_sli_config_port - Issue config port mailbox command
  4010. * @phba: Pointer to HBA context object.
  4011. * @sli_mode: sli mode - 2/3
  4012. *
  4013. * This function is called by the sli intialization code path
  4014. * to issue config_port mailbox command. This function restarts the
  4015. * HBA firmware and issues a config_port mailbox command to configure
  4016. * the SLI interface in the sli mode specified by sli_mode
  4017. * variable. The caller is not required to hold any locks.
  4018. * The function returns 0 if successful, else returns negative error
  4019. * code.
  4020. **/
  4021. int
  4022. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  4023. {
  4024. LPFC_MBOXQ_t *pmb;
  4025. uint32_t resetcount = 0, rc = 0, done = 0;
  4026. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4027. if (!pmb) {
  4028. phba->link_state = LPFC_HBA_ERROR;
  4029. return -ENOMEM;
  4030. }
  4031. phba->sli_rev = sli_mode;
  4032. while (resetcount < 2 && !done) {
  4033. spin_lock_irq(&phba->hbalock);
  4034. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  4035. spin_unlock_irq(&phba->hbalock);
  4036. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  4037. lpfc_sli_brdrestart(phba);
  4038. rc = lpfc_sli_chipset_init(phba);
  4039. if (rc)
  4040. break;
  4041. spin_lock_irq(&phba->hbalock);
  4042. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4043. spin_unlock_irq(&phba->hbalock);
  4044. resetcount++;
  4045. /* Call pre CONFIG_PORT mailbox command initialization. A
  4046. * value of 0 means the call was successful. Any other
  4047. * nonzero value is a failure, but if ERESTART is returned,
  4048. * the driver may reset the HBA and try again.
  4049. */
  4050. rc = lpfc_config_port_prep(phba);
  4051. if (rc == -ERESTART) {
  4052. phba->link_state = LPFC_LINK_UNKNOWN;
  4053. continue;
  4054. } else if (rc)
  4055. break;
  4056. phba->link_state = LPFC_INIT_MBX_CMDS;
  4057. lpfc_config_port(phba, pmb);
  4058. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  4059. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  4060. LPFC_SLI3_HBQ_ENABLED |
  4061. LPFC_SLI3_CRP_ENABLED |
  4062. LPFC_SLI3_BG_ENABLED |
  4063. LPFC_SLI3_DSS_ENABLED);
  4064. if (rc != MBX_SUCCESS) {
  4065. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4066. "0442 Adapter failed to init, mbxCmd x%x "
  4067. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  4068. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  4069. spin_lock_irq(&phba->hbalock);
  4070. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  4071. spin_unlock_irq(&phba->hbalock);
  4072. rc = -ENXIO;
  4073. } else {
  4074. /* Allow asynchronous mailbox command to go through */
  4075. spin_lock_irq(&phba->hbalock);
  4076. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  4077. spin_unlock_irq(&phba->hbalock);
  4078. done = 1;
  4079. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  4080. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  4081. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  4082. "3110 Port did not grant ASABT\n");
  4083. }
  4084. }
  4085. if (!done) {
  4086. rc = -EINVAL;
  4087. goto do_prep_failed;
  4088. }
  4089. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  4090. if (!pmb->u.mb.un.varCfgPort.cMA) {
  4091. rc = -ENXIO;
  4092. goto do_prep_failed;
  4093. }
  4094. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  4095. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  4096. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  4097. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  4098. phba->max_vpi : phba->max_vports;
  4099. } else
  4100. phba->max_vpi = 0;
  4101. phba->fips_level = 0;
  4102. phba->fips_spec_rev = 0;
  4103. if (pmb->u.mb.un.varCfgPort.gdss) {
  4104. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  4105. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  4106. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  4107. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4108. "2850 Security Crypto Active. FIPS x%d "
  4109. "(Spec Rev: x%d)",
  4110. phba->fips_level, phba->fips_spec_rev);
  4111. }
  4112. if (pmb->u.mb.un.varCfgPort.sec_err) {
  4113. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4114. "2856 Config Port Security Crypto "
  4115. "Error: x%x ",
  4116. pmb->u.mb.un.varCfgPort.sec_err);
  4117. }
  4118. if (pmb->u.mb.un.varCfgPort.gerbm)
  4119. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  4120. if (pmb->u.mb.un.varCfgPort.gcrp)
  4121. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  4122. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  4123. phba->port_gp = phba->mbox->us.s3_pgp.port;
  4124. if (phba->cfg_enable_bg) {
  4125. if (pmb->u.mb.un.varCfgPort.gbg)
  4126. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  4127. else
  4128. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4129. "0443 Adapter did not grant "
  4130. "BlockGuard\n");
  4131. }
  4132. } else {
  4133. phba->hbq_get = NULL;
  4134. phba->port_gp = phba->mbox->us.s2.port;
  4135. phba->max_vpi = 0;
  4136. }
  4137. do_prep_failed:
  4138. mempool_free(pmb, phba->mbox_mem_pool);
  4139. return rc;
  4140. }
  4141. /**
  4142. * lpfc_sli_hba_setup - SLI intialization function
  4143. * @phba: Pointer to HBA context object.
  4144. *
  4145. * This function is the main SLI intialization function. This function
  4146. * is called by the HBA intialization code, HBA reset code and HBA
  4147. * error attention handler code. Caller is not required to hold any
  4148. * locks. This function issues config_port mailbox command to configure
  4149. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4150. * calls the config_port_post function to issue init_link mailbox
  4151. * command and to start the discovery. The function will return zero
  4152. * if successful, else it will return negative error code.
  4153. **/
  4154. int
  4155. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4156. {
  4157. uint32_t rc;
  4158. int mode = 3, i;
  4159. int longs;
  4160. switch (lpfc_sli_mode) {
  4161. case 2:
  4162. if (phba->cfg_enable_npiv) {
  4163. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4164. "1824 NPIV enabled: Override lpfc_sli_mode "
  4165. "parameter (%d) to auto (0).\n",
  4166. lpfc_sli_mode);
  4167. break;
  4168. }
  4169. mode = 2;
  4170. break;
  4171. case 0:
  4172. case 3:
  4173. break;
  4174. default:
  4175. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4176. "1819 Unrecognized lpfc_sli_mode "
  4177. "parameter: %d.\n", lpfc_sli_mode);
  4178. break;
  4179. }
  4180. rc = lpfc_sli_config_port(phba, mode);
  4181. if (rc && lpfc_sli_mode == 3)
  4182. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4183. "1820 Unable to select SLI-3. "
  4184. "Not supported by adapter.\n");
  4185. if (rc && mode != 2)
  4186. rc = lpfc_sli_config_port(phba, 2);
  4187. if (rc)
  4188. goto lpfc_sli_hba_setup_error;
  4189. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4190. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4191. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4192. if (!rc) {
  4193. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4194. "2709 This device supports "
  4195. "Advanced Error Reporting (AER)\n");
  4196. spin_lock_irq(&phba->hbalock);
  4197. phba->hba_flag |= HBA_AER_ENABLED;
  4198. spin_unlock_irq(&phba->hbalock);
  4199. } else {
  4200. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4201. "2708 This device does not support "
  4202. "Advanced Error Reporting (AER): %d\n",
  4203. rc);
  4204. phba->cfg_aer_support = 0;
  4205. }
  4206. }
  4207. if (phba->sli_rev == 3) {
  4208. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4209. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4210. } else {
  4211. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4212. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4213. phba->sli3_options = 0;
  4214. }
  4215. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4216. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4217. phba->sli_rev, phba->max_vpi);
  4218. rc = lpfc_sli_ring_map(phba);
  4219. if (rc)
  4220. goto lpfc_sli_hba_setup_error;
  4221. /* Initialize VPIs. */
  4222. if (phba->sli_rev == LPFC_SLI_REV3) {
  4223. /*
  4224. * The VPI bitmask and physical ID array are allocated
  4225. * and initialized once only - at driver load. A port
  4226. * reset doesn't need to reinitialize this memory.
  4227. */
  4228. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4229. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4230. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4231. GFP_KERNEL);
  4232. if (!phba->vpi_bmask) {
  4233. rc = -ENOMEM;
  4234. goto lpfc_sli_hba_setup_error;
  4235. }
  4236. phba->vpi_ids = kzalloc(
  4237. (phba->max_vpi+1) * sizeof(uint16_t),
  4238. GFP_KERNEL);
  4239. if (!phba->vpi_ids) {
  4240. kfree(phba->vpi_bmask);
  4241. rc = -ENOMEM;
  4242. goto lpfc_sli_hba_setup_error;
  4243. }
  4244. for (i = 0; i < phba->max_vpi; i++)
  4245. phba->vpi_ids[i] = i;
  4246. }
  4247. }
  4248. /* Init HBQs */
  4249. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4250. rc = lpfc_sli_hbq_setup(phba);
  4251. if (rc)
  4252. goto lpfc_sli_hba_setup_error;
  4253. }
  4254. spin_lock_irq(&phba->hbalock);
  4255. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4256. spin_unlock_irq(&phba->hbalock);
  4257. rc = lpfc_config_port_post(phba);
  4258. if (rc)
  4259. goto lpfc_sli_hba_setup_error;
  4260. return rc;
  4261. lpfc_sli_hba_setup_error:
  4262. phba->link_state = LPFC_HBA_ERROR;
  4263. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4264. "0445 Firmware initialization failed\n");
  4265. return rc;
  4266. }
  4267. /**
  4268. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4269. * @phba: Pointer to HBA context object.
  4270. * @mboxq: mailbox pointer.
  4271. * This function issue a dump mailbox command to read config region
  4272. * 23 and parse the records in the region and populate driver
  4273. * data structure.
  4274. **/
  4275. static int
  4276. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4277. {
  4278. LPFC_MBOXQ_t *mboxq;
  4279. struct lpfc_dmabuf *mp;
  4280. struct lpfc_mqe *mqe;
  4281. uint32_t data_length;
  4282. int rc;
  4283. /* Program the default value of vlan_id and fc_map */
  4284. phba->valid_vlan = 0;
  4285. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4286. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4287. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4288. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4289. if (!mboxq)
  4290. return -ENOMEM;
  4291. mqe = &mboxq->u.mqe;
  4292. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4293. rc = -ENOMEM;
  4294. goto out_free_mboxq;
  4295. }
  4296. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4297. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4298. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4299. "(%d):2571 Mailbox cmd x%x Status x%x "
  4300. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4301. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4302. "CQ: x%x x%x x%x x%x\n",
  4303. mboxq->vport ? mboxq->vport->vpi : 0,
  4304. bf_get(lpfc_mqe_command, mqe),
  4305. bf_get(lpfc_mqe_status, mqe),
  4306. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4307. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4308. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4309. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4310. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4311. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4312. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4313. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4314. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4315. mboxq->mcqe.word0,
  4316. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4317. mboxq->mcqe.trailer);
  4318. if (rc) {
  4319. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4320. kfree(mp);
  4321. rc = -EIO;
  4322. goto out_free_mboxq;
  4323. }
  4324. data_length = mqe->un.mb_words[5];
  4325. if (data_length > DMP_RGN23_SIZE) {
  4326. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4327. kfree(mp);
  4328. rc = -EIO;
  4329. goto out_free_mboxq;
  4330. }
  4331. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4332. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4333. kfree(mp);
  4334. rc = 0;
  4335. out_free_mboxq:
  4336. mempool_free(mboxq, phba->mbox_mem_pool);
  4337. return rc;
  4338. }
  4339. /**
  4340. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4341. * @phba: pointer to lpfc hba data structure.
  4342. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4343. * @vpd: pointer to the memory to hold resulting port vpd data.
  4344. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4345. * On output, the number of data bytes in @vpd.
  4346. *
  4347. * This routine executes a READ_REV SLI4 mailbox command. In
  4348. * addition, this routine gets the port vpd data.
  4349. *
  4350. * Return codes
  4351. * 0 - successful
  4352. * -ENOMEM - could not allocated memory.
  4353. **/
  4354. static int
  4355. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4356. uint8_t *vpd, uint32_t *vpd_size)
  4357. {
  4358. int rc = 0;
  4359. uint32_t dma_size;
  4360. struct lpfc_dmabuf *dmabuf;
  4361. struct lpfc_mqe *mqe;
  4362. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4363. if (!dmabuf)
  4364. return -ENOMEM;
  4365. /*
  4366. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4367. * mailbox command.
  4368. */
  4369. dma_size = *vpd_size;
  4370. dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
  4371. &dmabuf->phys, GFP_KERNEL);
  4372. if (!dmabuf->virt) {
  4373. kfree(dmabuf);
  4374. return -ENOMEM;
  4375. }
  4376. /*
  4377. * The SLI4 implementation of READ_REV conflicts at word1,
  4378. * bits 31:16 and SLI4 adds vpd functionality not present
  4379. * in SLI3. This code corrects the conflicts.
  4380. */
  4381. lpfc_read_rev(phba, mboxq);
  4382. mqe = &mboxq->u.mqe;
  4383. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4384. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4385. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4386. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4387. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4388. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4389. if (rc) {
  4390. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4391. dmabuf->virt, dmabuf->phys);
  4392. kfree(dmabuf);
  4393. return -EIO;
  4394. }
  4395. /*
  4396. * The available vpd length cannot be bigger than the
  4397. * DMA buffer passed to the port. Catch the less than
  4398. * case and update the caller's size.
  4399. */
  4400. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4401. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4402. memcpy(vpd, dmabuf->virt, *vpd_size);
  4403. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4404. dmabuf->virt, dmabuf->phys);
  4405. kfree(dmabuf);
  4406. return 0;
  4407. }
  4408. /**
  4409. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4410. * @phba: pointer to lpfc hba data structure.
  4411. *
  4412. * This routine retrieves SLI4 device physical port name this PCI function
  4413. * is attached to.
  4414. *
  4415. * Return codes
  4416. * 0 - successful
  4417. * otherwise - failed to retrieve physical port name
  4418. **/
  4419. static int
  4420. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4421. {
  4422. LPFC_MBOXQ_t *mboxq;
  4423. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4424. struct lpfc_controller_attribute *cntl_attr;
  4425. struct lpfc_mbx_get_port_name *get_port_name;
  4426. void *virtaddr = NULL;
  4427. uint32_t alloclen, reqlen;
  4428. uint32_t shdr_status, shdr_add_status;
  4429. union lpfc_sli4_cfg_shdr *shdr;
  4430. char cport_name = 0;
  4431. int rc;
  4432. /* We assume nothing at this point */
  4433. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4434. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4435. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4436. if (!mboxq)
  4437. return -ENOMEM;
  4438. /* obtain link type and link number via READ_CONFIG */
  4439. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4440. lpfc_sli4_read_config(phba);
  4441. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4442. goto retrieve_ppname;
  4443. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4444. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4445. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4446. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4447. LPFC_SLI4_MBX_NEMBED);
  4448. if (alloclen < reqlen) {
  4449. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4450. "3084 Allocated DMA memory size (%d) is "
  4451. "less than the requested DMA memory size "
  4452. "(%d)\n", alloclen, reqlen);
  4453. rc = -ENOMEM;
  4454. goto out_free_mboxq;
  4455. }
  4456. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4457. virtaddr = mboxq->sge_array->addr[0];
  4458. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4459. shdr = &mbx_cntl_attr->cfg_shdr;
  4460. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4461. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4462. if (shdr_status || shdr_add_status || rc) {
  4463. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4464. "3085 Mailbox x%x (x%x/x%x) failed, "
  4465. "rc:x%x, status:x%x, add_status:x%x\n",
  4466. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4467. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4468. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4469. rc, shdr_status, shdr_add_status);
  4470. rc = -ENXIO;
  4471. goto out_free_mboxq;
  4472. }
  4473. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4474. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4475. phba->sli4_hba.lnk_info.lnk_tp =
  4476. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4477. phba->sli4_hba.lnk_info.lnk_no =
  4478. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4479. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4480. "3086 lnk_type:%d, lnk_numb:%d\n",
  4481. phba->sli4_hba.lnk_info.lnk_tp,
  4482. phba->sli4_hba.lnk_info.lnk_no);
  4483. retrieve_ppname:
  4484. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4485. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4486. sizeof(struct lpfc_mbx_get_port_name) -
  4487. sizeof(struct lpfc_sli4_cfg_mhdr),
  4488. LPFC_SLI4_MBX_EMBED);
  4489. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4490. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4491. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4492. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4493. phba->sli4_hba.lnk_info.lnk_tp);
  4494. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4495. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4496. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4497. if (shdr_status || shdr_add_status || rc) {
  4498. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4499. "3087 Mailbox x%x (x%x/x%x) failed: "
  4500. "rc:x%x, status:x%x, add_status:x%x\n",
  4501. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4502. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4503. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4504. rc, shdr_status, shdr_add_status);
  4505. rc = -ENXIO;
  4506. goto out_free_mboxq;
  4507. }
  4508. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4509. case LPFC_LINK_NUMBER_0:
  4510. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4511. &get_port_name->u.response);
  4512. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4513. break;
  4514. case LPFC_LINK_NUMBER_1:
  4515. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4516. &get_port_name->u.response);
  4517. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4518. break;
  4519. case LPFC_LINK_NUMBER_2:
  4520. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4521. &get_port_name->u.response);
  4522. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4523. break;
  4524. case LPFC_LINK_NUMBER_3:
  4525. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4526. &get_port_name->u.response);
  4527. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4528. break;
  4529. default:
  4530. break;
  4531. }
  4532. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4533. phba->Port[0] = cport_name;
  4534. phba->Port[1] = '\0';
  4535. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4536. "3091 SLI get port name: %s\n", phba->Port);
  4537. }
  4538. out_free_mboxq:
  4539. if (rc != MBX_TIMEOUT) {
  4540. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4541. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4542. else
  4543. mempool_free(mboxq, phba->mbox_mem_pool);
  4544. }
  4545. return rc;
  4546. }
  4547. /**
  4548. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4549. * @phba: pointer to lpfc hba data structure.
  4550. *
  4551. * This routine is called to explicitly arm the SLI4 device's completion and
  4552. * event queues
  4553. **/
  4554. static void
  4555. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4556. {
  4557. int fcp_eqidx;
  4558. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4559. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4560. fcp_eqidx = 0;
  4561. if (phba->sli4_hba.fcp_cq) {
  4562. do {
  4563. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4564. LPFC_QUEUE_REARM);
  4565. } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
  4566. }
  4567. if (phba->cfg_fof)
  4568. lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
  4569. if (phba->sli4_hba.hba_eq) {
  4570. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
  4571. fcp_eqidx++)
  4572. lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
  4573. LPFC_QUEUE_REARM);
  4574. }
  4575. if (phba->cfg_fof)
  4576. lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
  4577. }
  4578. /**
  4579. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4580. * @phba: Pointer to HBA context object.
  4581. * @type: The resource extent type.
  4582. * @extnt_count: buffer to hold port available extent count.
  4583. * @extnt_size: buffer to hold element count per extent.
  4584. *
  4585. * This function calls the port and retrievs the number of available
  4586. * extents and their size for a particular extent type.
  4587. *
  4588. * Returns: 0 if successful. Nonzero otherwise.
  4589. **/
  4590. int
  4591. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4592. uint16_t *extnt_count, uint16_t *extnt_size)
  4593. {
  4594. int rc = 0;
  4595. uint32_t length;
  4596. uint32_t mbox_tmo;
  4597. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4598. LPFC_MBOXQ_t *mbox;
  4599. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4600. if (!mbox)
  4601. return -ENOMEM;
  4602. /* Find out how many extents are available for this resource type */
  4603. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4604. sizeof(struct lpfc_sli4_cfg_mhdr));
  4605. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4606. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4607. length, LPFC_SLI4_MBX_EMBED);
  4608. /* Send an extents count of 0 - the GET doesn't use it. */
  4609. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4610. LPFC_SLI4_MBX_EMBED);
  4611. if (unlikely(rc)) {
  4612. rc = -EIO;
  4613. goto err_exit;
  4614. }
  4615. if (!phba->sli4_hba.intr_enable)
  4616. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4617. else {
  4618. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4619. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4620. }
  4621. if (unlikely(rc)) {
  4622. rc = -EIO;
  4623. goto err_exit;
  4624. }
  4625. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4626. if (bf_get(lpfc_mbox_hdr_status,
  4627. &rsrc_info->header.cfg_shdr.response)) {
  4628. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4629. "2930 Failed to get resource extents "
  4630. "Status 0x%x Add'l Status 0x%x\n",
  4631. bf_get(lpfc_mbox_hdr_status,
  4632. &rsrc_info->header.cfg_shdr.response),
  4633. bf_get(lpfc_mbox_hdr_add_status,
  4634. &rsrc_info->header.cfg_shdr.response));
  4635. rc = -EIO;
  4636. goto err_exit;
  4637. }
  4638. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4639. &rsrc_info->u.rsp);
  4640. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4641. &rsrc_info->u.rsp);
  4642. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4643. "3162 Retrieved extents type-%d from port: count:%d, "
  4644. "size:%d\n", type, *extnt_count, *extnt_size);
  4645. err_exit:
  4646. mempool_free(mbox, phba->mbox_mem_pool);
  4647. return rc;
  4648. }
  4649. /**
  4650. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4651. * @phba: Pointer to HBA context object.
  4652. * @type: The extent type to check.
  4653. *
  4654. * This function reads the current available extents from the port and checks
  4655. * if the extent count or extent size has changed since the last access.
  4656. * Callers use this routine post port reset to understand if there is a
  4657. * extent reprovisioning requirement.
  4658. *
  4659. * Returns:
  4660. * -Error: error indicates problem.
  4661. * 1: Extent count or size has changed.
  4662. * 0: No changes.
  4663. **/
  4664. static int
  4665. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4666. {
  4667. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4668. uint16_t size_diff, rsrc_ext_size;
  4669. int rc = 0;
  4670. struct lpfc_rsrc_blks *rsrc_entry;
  4671. struct list_head *rsrc_blk_list = NULL;
  4672. size_diff = 0;
  4673. curr_ext_cnt = 0;
  4674. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4675. &rsrc_ext_cnt,
  4676. &rsrc_ext_size);
  4677. if (unlikely(rc))
  4678. return -EIO;
  4679. switch (type) {
  4680. case LPFC_RSC_TYPE_FCOE_RPI:
  4681. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4682. break;
  4683. case LPFC_RSC_TYPE_FCOE_VPI:
  4684. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4685. break;
  4686. case LPFC_RSC_TYPE_FCOE_XRI:
  4687. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4688. break;
  4689. case LPFC_RSC_TYPE_FCOE_VFI:
  4690. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4691. break;
  4692. default:
  4693. break;
  4694. }
  4695. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4696. curr_ext_cnt++;
  4697. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4698. size_diff++;
  4699. }
  4700. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4701. rc = 1;
  4702. return rc;
  4703. }
  4704. /**
  4705. * lpfc_sli4_cfg_post_extnts -
  4706. * @phba: Pointer to HBA context object.
  4707. * @extnt_cnt - number of available extents.
  4708. * @type - the extent type (rpi, xri, vfi, vpi).
  4709. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4710. * @mbox - pointer to the caller's allocated mailbox structure.
  4711. *
  4712. * This function executes the extents allocation request. It also
  4713. * takes care of the amount of memory needed to allocate or get the
  4714. * allocated extents. It is the caller's responsibility to evaluate
  4715. * the response.
  4716. *
  4717. * Returns:
  4718. * -Error: Error value describes the condition found.
  4719. * 0: if successful
  4720. **/
  4721. static int
  4722. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  4723. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4724. {
  4725. int rc = 0;
  4726. uint32_t req_len;
  4727. uint32_t emb_len;
  4728. uint32_t alloc_len, mbox_tmo;
  4729. /* Calculate the total requested length of the dma memory */
  4730. req_len = extnt_cnt * sizeof(uint16_t);
  4731. /*
  4732. * Calculate the size of an embedded mailbox. The uint32_t
  4733. * accounts for extents-specific word.
  4734. */
  4735. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4736. sizeof(uint32_t);
  4737. /*
  4738. * Presume the allocation and response will fit into an embedded
  4739. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4740. */
  4741. *emb = LPFC_SLI4_MBX_EMBED;
  4742. if (req_len > emb_len) {
  4743. req_len = extnt_cnt * sizeof(uint16_t) +
  4744. sizeof(union lpfc_sli4_cfg_shdr) +
  4745. sizeof(uint32_t);
  4746. *emb = LPFC_SLI4_MBX_NEMBED;
  4747. }
  4748. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4749. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4750. req_len, *emb);
  4751. if (alloc_len < req_len) {
  4752. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4753. "2982 Allocated DMA memory size (x%x) is "
  4754. "less than the requested DMA memory "
  4755. "size (x%x)\n", alloc_len, req_len);
  4756. return -ENOMEM;
  4757. }
  4758. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  4759. if (unlikely(rc))
  4760. return -EIO;
  4761. if (!phba->sli4_hba.intr_enable)
  4762. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4763. else {
  4764. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4765. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4766. }
  4767. if (unlikely(rc))
  4768. rc = -EIO;
  4769. return rc;
  4770. }
  4771. /**
  4772. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4773. * @phba: Pointer to HBA context object.
  4774. * @type: The resource extent type to allocate.
  4775. *
  4776. * This function allocates the number of elements for the specified
  4777. * resource type.
  4778. **/
  4779. static int
  4780. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4781. {
  4782. bool emb = false;
  4783. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4784. uint16_t rsrc_id, rsrc_start, j, k;
  4785. uint16_t *ids;
  4786. int i, rc;
  4787. unsigned long longs;
  4788. unsigned long *bmask;
  4789. struct lpfc_rsrc_blks *rsrc_blks;
  4790. LPFC_MBOXQ_t *mbox;
  4791. uint32_t length;
  4792. struct lpfc_id_range *id_array = NULL;
  4793. void *virtaddr = NULL;
  4794. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4795. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4796. struct list_head *ext_blk_list;
  4797. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4798. &rsrc_cnt,
  4799. &rsrc_size);
  4800. if (unlikely(rc))
  4801. return -EIO;
  4802. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4803. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4804. "3009 No available Resource Extents "
  4805. "for resource type 0x%x: Count: 0x%x, "
  4806. "Size 0x%x\n", type, rsrc_cnt,
  4807. rsrc_size);
  4808. return -ENOMEM;
  4809. }
  4810. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  4811. "2903 Post resource extents type-0x%x: "
  4812. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  4813. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4814. if (!mbox)
  4815. return -ENOMEM;
  4816. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  4817. if (unlikely(rc)) {
  4818. rc = -EIO;
  4819. goto err_exit;
  4820. }
  4821. /*
  4822. * Figure out where the response is located. Then get local pointers
  4823. * to the response data. The port does not guarantee to respond to
  4824. * all extents counts request so update the local variable with the
  4825. * allocated count from the port.
  4826. */
  4827. if (emb == LPFC_SLI4_MBX_EMBED) {
  4828. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4829. id_array = &rsrc_ext->u.rsp.id[0];
  4830. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4831. } else {
  4832. virtaddr = mbox->sge_array->addr[0];
  4833. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4834. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4835. id_array = &n_rsrc->id;
  4836. }
  4837. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4838. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4839. /*
  4840. * Based on the resource size and count, correct the base and max
  4841. * resource values.
  4842. */
  4843. length = sizeof(struct lpfc_rsrc_blks);
  4844. switch (type) {
  4845. case LPFC_RSC_TYPE_FCOE_RPI:
  4846. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4847. sizeof(unsigned long),
  4848. GFP_KERNEL);
  4849. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4850. rc = -ENOMEM;
  4851. goto err_exit;
  4852. }
  4853. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4854. sizeof(uint16_t),
  4855. GFP_KERNEL);
  4856. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4857. kfree(phba->sli4_hba.rpi_bmask);
  4858. rc = -ENOMEM;
  4859. goto err_exit;
  4860. }
  4861. /*
  4862. * The next_rpi was initialized with the maximum available
  4863. * count but the port may allocate a smaller number. Catch
  4864. * that case and update the next_rpi.
  4865. */
  4866. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4867. /* Initialize local ptrs for common extent processing later. */
  4868. bmask = phba->sli4_hba.rpi_bmask;
  4869. ids = phba->sli4_hba.rpi_ids;
  4870. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4871. break;
  4872. case LPFC_RSC_TYPE_FCOE_VPI:
  4873. phba->vpi_bmask = kzalloc(longs *
  4874. sizeof(unsigned long),
  4875. GFP_KERNEL);
  4876. if (unlikely(!phba->vpi_bmask)) {
  4877. rc = -ENOMEM;
  4878. goto err_exit;
  4879. }
  4880. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4881. sizeof(uint16_t),
  4882. GFP_KERNEL);
  4883. if (unlikely(!phba->vpi_ids)) {
  4884. kfree(phba->vpi_bmask);
  4885. rc = -ENOMEM;
  4886. goto err_exit;
  4887. }
  4888. /* Initialize local ptrs for common extent processing later. */
  4889. bmask = phba->vpi_bmask;
  4890. ids = phba->vpi_ids;
  4891. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4892. break;
  4893. case LPFC_RSC_TYPE_FCOE_XRI:
  4894. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4895. sizeof(unsigned long),
  4896. GFP_KERNEL);
  4897. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4898. rc = -ENOMEM;
  4899. goto err_exit;
  4900. }
  4901. phba->sli4_hba.max_cfg_param.xri_used = 0;
  4902. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4903. sizeof(uint16_t),
  4904. GFP_KERNEL);
  4905. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4906. kfree(phba->sli4_hba.xri_bmask);
  4907. rc = -ENOMEM;
  4908. goto err_exit;
  4909. }
  4910. /* Initialize local ptrs for common extent processing later. */
  4911. bmask = phba->sli4_hba.xri_bmask;
  4912. ids = phba->sli4_hba.xri_ids;
  4913. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4914. break;
  4915. case LPFC_RSC_TYPE_FCOE_VFI:
  4916. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4917. sizeof(unsigned long),
  4918. GFP_KERNEL);
  4919. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4920. rc = -ENOMEM;
  4921. goto err_exit;
  4922. }
  4923. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4924. sizeof(uint16_t),
  4925. GFP_KERNEL);
  4926. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4927. kfree(phba->sli4_hba.vfi_bmask);
  4928. rc = -ENOMEM;
  4929. goto err_exit;
  4930. }
  4931. /* Initialize local ptrs for common extent processing later. */
  4932. bmask = phba->sli4_hba.vfi_bmask;
  4933. ids = phba->sli4_hba.vfi_ids;
  4934. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4935. break;
  4936. default:
  4937. /* Unsupported Opcode. Fail call. */
  4938. id_array = NULL;
  4939. bmask = NULL;
  4940. ids = NULL;
  4941. ext_blk_list = NULL;
  4942. goto err_exit;
  4943. }
  4944. /*
  4945. * Complete initializing the extent configuration with the
  4946. * allocated ids assigned to this function. The bitmask serves
  4947. * as an index into the array and manages the available ids. The
  4948. * array just stores the ids communicated to the port via the wqes.
  4949. */
  4950. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4951. if ((i % 2) == 0)
  4952. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4953. &id_array[k]);
  4954. else
  4955. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4956. &id_array[k]);
  4957. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4958. if (unlikely(!rsrc_blks)) {
  4959. rc = -ENOMEM;
  4960. kfree(bmask);
  4961. kfree(ids);
  4962. goto err_exit;
  4963. }
  4964. rsrc_blks->rsrc_start = rsrc_id;
  4965. rsrc_blks->rsrc_size = rsrc_size;
  4966. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4967. rsrc_start = rsrc_id;
  4968. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4969. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4970. lpfc_sli4_get_els_iocb_cnt(phba);
  4971. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4972. ids[j] = rsrc_id;
  4973. rsrc_id++;
  4974. j++;
  4975. }
  4976. /* Entire word processed. Get next word.*/
  4977. if ((i % 2) == 1)
  4978. k++;
  4979. }
  4980. err_exit:
  4981. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4982. return rc;
  4983. }
  4984. /**
  4985. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4986. * @phba: Pointer to HBA context object.
  4987. * @type: the extent's type.
  4988. *
  4989. * This function deallocates all extents of a particular resource type.
  4990. * SLI4 does not allow for deallocating a particular extent range. It
  4991. * is the caller's responsibility to release all kernel memory resources.
  4992. **/
  4993. static int
  4994. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4995. {
  4996. int rc;
  4997. uint32_t length, mbox_tmo = 0;
  4998. LPFC_MBOXQ_t *mbox;
  4999. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  5000. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  5001. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5002. if (!mbox)
  5003. return -ENOMEM;
  5004. /*
  5005. * This function sends an embedded mailbox because it only sends the
  5006. * the resource type. All extents of this type are released by the
  5007. * port.
  5008. */
  5009. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  5010. sizeof(struct lpfc_sli4_cfg_mhdr));
  5011. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5012. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  5013. length, LPFC_SLI4_MBX_EMBED);
  5014. /* Send an extents count of 0 - the dealloc doesn't use it. */
  5015. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  5016. LPFC_SLI4_MBX_EMBED);
  5017. if (unlikely(rc)) {
  5018. rc = -EIO;
  5019. goto out_free_mbox;
  5020. }
  5021. if (!phba->sli4_hba.intr_enable)
  5022. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5023. else {
  5024. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5025. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5026. }
  5027. if (unlikely(rc)) {
  5028. rc = -EIO;
  5029. goto out_free_mbox;
  5030. }
  5031. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  5032. if (bf_get(lpfc_mbox_hdr_status,
  5033. &dealloc_rsrc->header.cfg_shdr.response)) {
  5034. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5035. "2919 Failed to release resource extents "
  5036. "for type %d - Status 0x%x Add'l Status 0x%x. "
  5037. "Resource memory not released.\n",
  5038. type,
  5039. bf_get(lpfc_mbox_hdr_status,
  5040. &dealloc_rsrc->header.cfg_shdr.response),
  5041. bf_get(lpfc_mbox_hdr_add_status,
  5042. &dealloc_rsrc->header.cfg_shdr.response));
  5043. rc = -EIO;
  5044. goto out_free_mbox;
  5045. }
  5046. /* Release kernel memory resources for the specific type. */
  5047. switch (type) {
  5048. case LPFC_RSC_TYPE_FCOE_VPI:
  5049. kfree(phba->vpi_bmask);
  5050. kfree(phba->vpi_ids);
  5051. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5052. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5053. &phba->lpfc_vpi_blk_list, list) {
  5054. list_del_init(&rsrc_blk->list);
  5055. kfree(rsrc_blk);
  5056. }
  5057. phba->sli4_hba.max_cfg_param.vpi_used = 0;
  5058. break;
  5059. case LPFC_RSC_TYPE_FCOE_XRI:
  5060. kfree(phba->sli4_hba.xri_bmask);
  5061. kfree(phba->sli4_hba.xri_ids);
  5062. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5063. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  5064. list_del_init(&rsrc_blk->list);
  5065. kfree(rsrc_blk);
  5066. }
  5067. break;
  5068. case LPFC_RSC_TYPE_FCOE_VFI:
  5069. kfree(phba->sli4_hba.vfi_bmask);
  5070. kfree(phba->sli4_hba.vfi_ids);
  5071. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5072. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5073. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  5074. list_del_init(&rsrc_blk->list);
  5075. kfree(rsrc_blk);
  5076. }
  5077. break;
  5078. case LPFC_RSC_TYPE_FCOE_RPI:
  5079. /* RPI bitmask and physical id array are cleaned up earlier. */
  5080. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5081. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  5082. list_del_init(&rsrc_blk->list);
  5083. kfree(rsrc_blk);
  5084. }
  5085. break;
  5086. default:
  5087. break;
  5088. }
  5089. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5090. out_free_mbox:
  5091. mempool_free(mbox, phba->mbox_mem_pool);
  5092. return rc;
  5093. }
  5094. /**
  5095. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  5096. * @phba: Pointer to HBA context object.
  5097. *
  5098. * This function allocates all SLI4 resource identifiers.
  5099. **/
  5100. int
  5101. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  5102. {
  5103. int i, rc, error = 0;
  5104. uint16_t count, base;
  5105. unsigned long longs;
  5106. if (!phba->sli4_hba.rpi_hdrs_in_use)
  5107. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  5108. if (phba->sli4_hba.extents_in_use) {
  5109. /*
  5110. * The port supports resource extents. The XRI, VPI, VFI, RPI
  5111. * resource extent count must be read and allocated before
  5112. * provisioning the resource id arrays.
  5113. */
  5114. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5115. LPFC_IDX_RSRC_RDY) {
  5116. /*
  5117. * Extent-based resources are set - the driver could
  5118. * be in a port reset. Figure out if any corrective
  5119. * actions need to be taken.
  5120. */
  5121. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5122. LPFC_RSC_TYPE_FCOE_VFI);
  5123. if (rc != 0)
  5124. error++;
  5125. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5126. LPFC_RSC_TYPE_FCOE_VPI);
  5127. if (rc != 0)
  5128. error++;
  5129. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5130. LPFC_RSC_TYPE_FCOE_XRI);
  5131. if (rc != 0)
  5132. error++;
  5133. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5134. LPFC_RSC_TYPE_FCOE_RPI);
  5135. if (rc != 0)
  5136. error++;
  5137. /*
  5138. * It's possible that the number of resources
  5139. * provided to this port instance changed between
  5140. * resets. Detect this condition and reallocate
  5141. * resources. Otherwise, there is no action.
  5142. */
  5143. if (error) {
  5144. lpfc_printf_log(phba, KERN_INFO,
  5145. LOG_MBOX | LOG_INIT,
  5146. "2931 Detected extent resource "
  5147. "change. Reallocating all "
  5148. "extents.\n");
  5149. rc = lpfc_sli4_dealloc_extent(phba,
  5150. LPFC_RSC_TYPE_FCOE_VFI);
  5151. rc = lpfc_sli4_dealloc_extent(phba,
  5152. LPFC_RSC_TYPE_FCOE_VPI);
  5153. rc = lpfc_sli4_dealloc_extent(phba,
  5154. LPFC_RSC_TYPE_FCOE_XRI);
  5155. rc = lpfc_sli4_dealloc_extent(phba,
  5156. LPFC_RSC_TYPE_FCOE_RPI);
  5157. } else
  5158. return 0;
  5159. }
  5160. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5161. if (unlikely(rc))
  5162. goto err_exit;
  5163. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5164. if (unlikely(rc))
  5165. goto err_exit;
  5166. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5167. if (unlikely(rc))
  5168. goto err_exit;
  5169. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5170. if (unlikely(rc))
  5171. goto err_exit;
  5172. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5173. LPFC_IDX_RSRC_RDY);
  5174. return rc;
  5175. } else {
  5176. /*
  5177. * The port does not support resource extents. The XRI, VPI,
  5178. * VFI, RPI resource ids were determined from READ_CONFIG.
  5179. * Just allocate the bitmasks and provision the resource id
  5180. * arrays. If a port reset is active, the resources don't
  5181. * need any action - just exit.
  5182. */
  5183. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5184. LPFC_IDX_RSRC_RDY) {
  5185. lpfc_sli4_dealloc_resource_identifiers(phba);
  5186. lpfc_sli4_remove_rpis(phba);
  5187. }
  5188. /* RPIs. */
  5189. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5190. if (count <= 0) {
  5191. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5192. "3279 Invalid provisioning of "
  5193. "rpi:%d\n", count);
  5194. rc = -EINVAL;
  5195. goto err_exit;
  5196. }
  5197. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5198. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5199. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5200. sizeof(unsigned long),
  5201. GFP_KERNEL);
  5202. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5203. rc = -ENOMEM;
  5204. goto err_exit;
  5205. }
  5206. phba->sli4_hba.rpi_ids = kzalloc(count *
  5207. sizeof(uint16_t),
  5208. GFP_KERNEL);
  5209. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5210. rc = -ENOMEM;
  5211. goto free_rpi_bmask;
  5212. }
  5213. for (i = 0; i < count; i++)
  5214. phba->sli4_hba.rpi_ids[i] = base + i;
  5215. /* VPIs. */
  5216. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5217. if (count <= 0) {
  5218. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5219. "3280 Invalid provisioning of "
  5220. "vpi:%d\n", count);
  5221. rc = -EINVAL;
  5222. goto free_rpi_ids;
  5223. }
  5224. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5225. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5226. phba->vpi_bmask = kzalloc(longs *
  5227. sizeof(unsigned long),
  5228. GFP_KERNEL);
  5229. if (unlikely(!phba->vpi_bmask)) {
  5230. rc = -ENOMEM;
  5231. goto free_rpi_ids;
  5232. }
  5233. phba->vpi_ids = kzalloc(count *
  5234. sizeof(uint16_t),
  5235. GFP_KERNEL);
  5236. if (unlikely(!phba->vpi_ids)) {
  5237. rc = -ENOMEM;
  5238. goto free_vpi_bmask;
  5239. }
  5240. for (i = 0; i < count; i++)
  5241. phba->vpi_ids[i] = base + i;
  5242. /* XRIs. */
  5243. count = phba->sli4_hba.max_cfg_param.max_xri;
  5244. if (count <= 0) {
  5245. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5246. "3281 Invalid provisioning of "
  5247. "xri:%d\n", count);
  5248. rc = -EINVAL;
  5249. goto free_vpi_ids;
  5250. }
  5251. base = phba->sli4_hba.max_cfg_param.xri_base;
  5252. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5253. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5254. sizeof(unsigned long),
  5255. GFP_KERNEL);
  5256. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5257. rc = -ENOMEM;
  5258. goto free_vpi_ids;
  5259. }
  5260. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5261. phba->sli4_hba.xri_ids = kzalloc(count *
  5262. sizeof(uint16_t),
  5263. GFP_KERNEL);
  5264. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5265. rc = -ENOMEM;
  5266. goto free_xri_bmask;
  5267. }
  5268. for (i = 0; i < count; i++)
  5269. phba->sli4_hba.xri_ids[i] = base + i;
  5270. /* VFIs. */
  5271. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5272. if (count <= 0) {
  5273. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5274. "3282 Invalid provisioning of "
  5275. "vfi:%d\n", count);
  5276. rc = -EINVAL;
  5277. goto free_xri_ids;
  5278. }
  5279. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5280. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5281. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5282. sizeof(unsigned long),
  5283. GFP_KERNEL);
  5284. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5285. rc = -ENOMEM;
  5286. goto free_xri_ids;
  5287. }
  5288. phba->sli4_hba.vfi_ids = kzalloc(count *
  5289. sizeof(uint16_t),
  5290. GFP_KERNEL);
  5291. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5292. rc = -ENOMEM;
  5293. goto free_vfi_bmask;
  5294. }
  5295. for (i = 0; i < count; i++)
  5296. phba->sli4_hba.vfi_ids[i] = base + i;
  5297. /*
  5298. * Mark all resources ready. An HBA reset doesn't need
  5299. * to reset the initialization.
  5300. */
  5301. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5302. LPFC_IDX_RSRC_RDY);
  5303. return 0;
  5304. }
  5305. free_vfi_bmask:
  5306. kfree(phba->sli4_hba.vfi_bmask);
  5307. free_xri_ids:
  5308. kfree(phba->sli4_hba.xri_ids);
  5309. free_xri_bmask:
  5310. kfree(phba->sli4_hba.xri_bmask);
  5311. free_vpi_ids:
  5312. kfree(phba->vpi_ids);
  5313. free_vpi_bmask:
  5314. kfree(phba->vpi_bmask);
  5315. free_rpi_ids:
  5316. kfree(phba->sli4_hba.rpi_ids);
  5317. free_rpi_bmask:
  5318. kfree(phba->sli4_hba.rpi_bmask);
  5319. err_exit:
  5320. return rc;
  5321. }
  5322. /**
  5323. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5324. * @phba: Pointer to HBA context object.
  5325. *
  5326. * This function allocates the number of elements for the specified
  5327. * resource type.
  5328. **/
  5329. int
  5330. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5331. {
  5332. if (phba->sli4_hba.extents_in_use) {
  5333. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5334. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5335. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5336. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5337. } else {
  5338. kfree(phba->vpi_bmask);
  5339. phba->sli4_hba.max_cfg_param.vpi_used = 0;
  5340. kfree(phba->vpi_ids);
  5341. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5342. kfree(phba->sli4_hba.xri_bmask);
  5343. kfree(phba->sli4_hba.xri_ids);
  5344. kfree(phba->sli4_hba.vfi_bmask);
  5345. kfree(phba->sli4_hba.vfi_ids);
  5346. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5347. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5348. }
  5349. return 0;
  5350. }
  5351. /**
  5352. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5353. * @phba: Pointer to HBA context object.
  5354. * @type: The resource extent type.
  5355. * @extnt_count: buffer to hold port extent count response
  5356. * @extnt_size: buffer to hold port extent size response.
  5357. *
  5358. * This function calls the port to read the host allocated extents
  5359. * for a particular type.
  5360. **/
  5361. int
  5362. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5363. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5364. {
  5365. bool emb;
  5366. int rc = 0;
  5367. uint16_t curr_blks = 0;
  5368. uint32_t req_len, emb_len;
  5369. uint32_t alloc_len, mbox_tmo;
  5370. struct list_head *blk_list_head;
  5371. struct lpfc_rsrc_blks *rsrc_blk;
  5372. LPFC_MBOXQ_t *mbox;
  5373. void *virtaddr = NULL;
  5374. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5375. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5376. union lpfc_sli4_cfg_shdr *shdr;
  5377. switch (type) {
  5378. case LPFC_RSC_TYPE_FCOE_VPI:
  5379. blk_list_head = &phba->lpfc_vpi_blk_list;
  5380. break;
  5381. case LPFC_RSC_TYPE_FCOE_XRI:
  5382. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5383. break;
  5384. case LPFC_RSC_TYPE_FCOE_VFI:
  5385. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5386. break;
  5387. case LPFC_RSC_TYPE_FCOE_RPI:
  5388. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5389. break;
  5390. default:
  5391. return -EIO;
  5392. }
  5393. /* Count the number of extents currently allocatd for this type. */
  5394. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5395. if (curr_blks == 0) {
  5396. /*
  5397. * The GET_ALLOCATED mailbox does not return the size,
  5398. * just the count. The size should be just the size
  5399. * stored in the current allocated block and all sizes
  5400. * for an extent type are the same so set the return
  5401. * value now.
  5402. */
  5403. *extnt_size = rsrc_blk->rsrc_size;
  5404. }
  5405. curr_blks++;
  5406. }
  5407. /*
  5408. * Calculate the size of an embedded mailbox. The uint32_t
  5409. * accounts for extents-specific word.
  5410. */
  5411. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5412. sizeof(uint32_t);
  5413. /*
  5414. * Presume the allocation and response will fit into an embedded
  5415. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5416. */
  5417. emb = LPFC_SLI4_MBX_EMBED;
  5418. req_len = emb_len;
  5419. if (req_len > emb_len) {
  5420. req_len = curr_blks * sizeof(uint16_t) +
  5421. sizeof(union lpfc_sli4_cfg_shdr) +
  5422. sizeof(uint32_t);
  5423. emb = LPFC_SLI4_MBX_NEMBED;
  5424. }
  5425. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5426. if (!mbox)
  5427. return -ENOMEM;
  5428. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5429. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5430. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5431. req_len, emb);
  5432. if (alloc_len < req_len) {
  5433. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5434. "2983 Allocated DMA memory size (x%x) is "
  5435. "less than the requested DMA memory "
  5436. "size (x%x)\n", alloc_len, req_len);
  5437. rc = -ENOMEM;
  5438. goto err_exit;
  5439. }
  5440. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5441. if (unlikely(rc)) {
  5442. rc = -EIO;
  5443. goto err_exit;
  5444. }
  5445. if (!phba->sli4_hba.intr_enable)
  5446. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5447. else {
  5448. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5449. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5450. }
  5451. if (unlikely(rc)) {
  5452. rc = -EIO;
  5453. goto err_exit;
  5454. }
  5455. /*
  5456. * Figure out where the response is located. Then get local pointers
  5457. * to the response data. The port does not guarantee to respond to
  5458. * all extents counts request so update the local variable with the
  5459. * allocated count from the port.
  5460. */
  5461. if (emb == LPFC_SLI4_MBX_EMBED) {
  5462. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5463. shdr = &rsrc_ext->header.cfg_shdr;
  5464. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5465. } else {
  5466. virtaddr = mbox->sge_array->addr[0];
  5467. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5468. shdr = &n_rsrc->cfg_shdr;
  5469. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5470. }
  5471. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5472. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5473. "2984 Failed to read allocated resources "
  5474. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5475. type,
  5476. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5477. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5478. rc = -EIO;
  5479. goto err_exit;
  5480. }
  5481. err_exit:
  5482. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5483. return rc;
  5484. }
  5485. /**
  5486. * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
  5487. * @phba: pointer to lpfc hba data structure.
  5488. *
  5489. * This routine walks the list of els buffers that have been allocated and
  5490. * repost them to the port by using SGL block post. This is needed after a
  5491. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5492. * of els buffer sgls which contains contiguous xris and uses the non-embedded
  5493. * SGL block post mailbox commands to post them to the port. For single els
  5494. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5495. * mailbox command for posting.
  5496. *
  5497. * Returns: 0 = success, non-zero failure.
  5498. **/
  5499. static int
  5500. lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
  5501. {
  5502. struct lpfc_sglq *sglq_entry = NULL;
  5503. struct lpfc_sglq *sglq_entry_next = NULL;
  5504. struct lpfc_sglq *sglq_entry_first = NULL;
  5505. int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
  5506. int last_xritag = NO_XRI;
  5507. struct lpfc_sli_ring *pring;
  5508. LIST_HEAD(prep_sgl_list);
  5509. LIST_HEAD(blck_sgl_list);
  5510. LIST_HEAD(allc_sgl_list);
  5511. LIST_HEAD(post_sgl_list);
  5512. LIST_HEAD(free_sgl_list);
  5513. pring = &phba->sli.ring[LPFC_ELS_RING];
  5514. spin_lock_irq(&phba->hbalock);
  5515. spin_lock(&pring->ring_lock);
  5516. list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
  5517. spin_unlock(&pring->ring_lock);
  5518. spin_unlock_irq(&phba->hbalock);
  5519. total_cnt = phba->sli4_hba.els_xri_cnt;
  5520. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  5521. &allc_sgl_list, list) {
  5522. list_del_init(&sglq_entry->list);
  5523. block_cnt++;
  5524. if ((last_xritag != NO_XRI) &&
  5525. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  5526. /* a hole in xri block, form a sgl posting block */
  5527. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  5528. post_cnt = block_cnt - 1;
  5529. /* prepare list for next posting block */
  5530. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5531. block_cnt = 1;
  5532. } else {
  5533. /* prepare list for next posting block */
  5534. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5535. /* enough sgls for non-embed sgl mbox command */
  5536. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  5537. list_splice_init(&prep_sgl_list,
  5538. &blck_sgl_list);
  5539. post_cnt = block_cnt;
  5540. block_cnt = 0;
  5541. }
  5542. }
  5543. num_posted++;
  5544. /* keep track of last sgl's xritag */
  5545. last_xritag = sglq_entry->sli4_xritag;
  5546. /* end of repost sgl list condition for els buffers */
  5547. if (num_posted == phba->sli4_hba.els_xri_cnt) {
  5548. if (post_cnt == 0) {
  5549. list_splice_init(&prep_sgl_list,
  5550. &blck_sgl_list);
  5551. post_cnt = block_cnt;
  5552. } else if (block_cnt == 1) {
  5553. status = lpfc_sli4_post_sgl(phba,
  5554. sglq_entry->phys, 0,
  5555. sglq_entry->sli4_xritag);
  5556. if (!status) {
  5557. /* successful, put sgl to posted list */
  5558. list_add_tail(&sglq_entry->list,
  5559. &post_sgl_list);
  5560. } else {
  5561. /* Failure, put sgl to free list */
  5562. lpfc_printf_log(phba, KERN_WARNING,
  5563. LOG_SLI,
  5564. "3159 Failed to post els "
  5565. "sgl, xritag:x%x\n",
  5566. sglq_entry->sli4_xritag);
  5567. list_add_tail(&sglq_entry->list,
  5568. &free_sgl_list);
  5569. total_cnt--;
  5570. }
  5571. }
  5572. }
  5573. /* continue until a nembed page worth of sgls */
  5574. if (post_cnt == 0)
  5575. continue;
  5576. /* post the els buffer list sgls as a block */
  5577. status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
  5578. post_cnt);
  5579. if (!status) {
  5580. /* success, put sgl list to posted sgl list */
  5581. list_splice_init(&blck_sgl_list, &post_sgl_list);
  5582. } else {
  5583. /* Failure, put sgl list to free sgl list */
  5584. sglq_entry_first = list_first_entry(&blck_sgl_list,
  5585. struct lpfc_sglq,
  5586. list);
  5587. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5588. "3160 Failed to post els sgl-list, "
  5589. "xritag:x%x-x%x\n",
  5590. sglq_entry_first->sli4_xritag,
  5591. (sglq_entry_first->sli4_xritag +
  5592. post_cnt - 1));
  5593. list_splice_init(&blck_sgl_list, &free_sgl_list);
  5594. total_cnt -= post_cnt;
  5595. }
  5596. /* don't reset xirtag due to hole in xri block */
  5597. if (block_cnt == 0)
  5598. last_xritag = NO_XRI;
  5599. /* reset els sgl post count for next round of posting */
  5600. post_cnt = 0;
  5601. }
  5602. /* update the number of XRIs posted for ELS */
  5603. phba->sli4_hba.els_xri_cnt = total_cnt;
  5604. /* free the els sgls failed to post */
  5605. lpfc_free_sgl_list(phba, &free_sgl_list);
  5606. /* push els sgls posted to the availble list */
  5607. if (!list_empty(&post_sgl_list)) {
  5608. spin_lock_irq(&phba->hbalock);
  5609. spin_lock(&pring->ring_lock);
  5610. list_splice_init(&post_sgl_list,
  5611. &phba->sli4_hba.lpfc_sgl_list);
  5612. spin_unlock(&pring->ring_lock);
  5613. spin_unlock_irq(&phba->hbalock);
  5614. } else {
  5615. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5616. "3161 Failure to post els sgl to port.\n");
  5617. return -EIO;
  5618. }
  5619. return 0;
  5620. }
  5621. /**
  5622. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5623. * @phba: Pointer to HBA context object.
  5624. *
  5625. * This function is the main SLI4 device intialization PCI function. This
  5626. * function is called by the HBA intialization code, HBA reset code and
  5627. * HBA error attention handler code. Caller is not required to hold any
  5628. * locks.
  5629. **/
  5630. int
  5631. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5632. {
  5633. int rc;
  5634. LPFC_MBOXQ_t *mboxq;
  5635. struct lpfc_mqe *mqe;
  5636. uint8_t *vpd;
  5637. uint32_t vpd_size;
  5638. uint32_t ftr_rsp = 0;
  5639. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5640. struct lpfc_vport *vport = phba->pport;
  5641. struct lpfc_dmabuf *mp;
  5642. /* Perform a PCI function reset to start from clean */
  5643. rc = lpfc_pci_function_reset(phba);
  5644. if (unlikely(rc))
  5645. return -ENODEV;
  5646. /* Check the HBA Host Status Register for readyness */
  5647. rc = lpfc_sli4_post_status_check(phba);
  5648. if (unlikely(rc))
  5649. return -ENODEV;
  5650. else {
  5651. spin_lock_irq(&phba->hbalock);
  5652. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5653. spin_unlock_irq(&phba->hbalock);
  5654. }
  5655. /*
  5656. * Allocate a single mailbox container for initializing the
  5657. * port.
  5658. */
  5659. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5660. if (!mboxq)
  5661. return -ENOMEM;
  5662. /* Issue READ_REV to collect vpd and FW information. */
  5663. vpd_size = SLI4_PAGE_SIZE;
  5664. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5665. if (!vpd) {
  5666. rc = -ENOMEM;
  5667. goto out_free_mbox;
  5668. }
  5669. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5670. if (unlikely(rc)) {
  5671. kfree(vpd);
  5672. goto out_free_mbox;
  5673. }
  5674. mqe = &mboxq->u.mqe;
  5675. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5676. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5677. phba->hba_flag |= HBA_FCOE_MODE;
  5678. else
  5679. phba->hba_flag &= ~HBA_FCOE_MODE;
  5680. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5681. LPFC_DCBX_CEE_MODE)
  5682. phba->hba_flag |= HBA_FIP_SUPPORT;
  5683. else
  5684. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5685. phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
  5686. if (phba->sli_rev != LPFC_SLI_REV4) {
  5687. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5688. "0376 READ_REV Error. SLI Level %d "
  5689. "FCoE enabled %d\n",
  5690. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5691. rc = -EIO;
  5692. kfree(vpd);
  5693. goto out_free_mbox;
  5694. }
  5695. /*
  5696. * Continue initialization with default values even if driver failed
  5697. * to read FCoE param config regions, only read parameters if the
  5698. * board is FCoE
  5699. */
  5700. if (phba->hba_flag & HBA_FCOE_MODE &&
  5701. lpfc_sli4_read_fcoe_params(phba))
  5702. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5703. "2570 Failed to read FCoE parameters\n");
  5704. /*
  5705. * Retrieve sli4 device physical port name, failure of doing it
  5706. * is considered as non-fatal.
  5707. */
  5708. rc = lpfc_sli4_retrieve_pport_name(phba);
  5709. if (!rc)
  5710. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5711. "3080 Successful retrieving SLI4 device "
  5712. "physical port name: %s.\n", phba->Port);
  5713. /*
  5714. * Evaluate the read rev and vpd data. Populate the driver
  5715. * state with the results. If this routine fails, the failure
  5716. * is not fatal as the driver will use generic values.
  5717. */
  5718. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5719. if (unlikely(!rc)) {
  5720. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5721. "0377 Error %d parsing vpd. "
  5722. "Using defaults.\n", rc);
  5723. rc = 0;
  5724. }
  5725. kfree(vpd);
  5726. /* Save information as VPD data */
  5727. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5728. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5729. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5730. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5731. &mqe->un.read_rev);
  5732. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5733. &mqe->un.read_rev);
  5734. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5735. &mqe->un.read_rev);
  5736. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5737. &mqe->un.read_rev);
  5738. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5739. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5740. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5741. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5742. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5743. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5744. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5745. "(%d):0380 READ_REV Status x%x "
  5746. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5747. mboxq->vport ? mboxq->vport->vpi : 0,
  5748. bf_get(lpfc_mqe_status, mqe),
  5749. phba->vpd.rev.opFwName,
  5750. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5751. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5752. /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
  5753. rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
  5754. if (phba->pport->cfg_lun_queue_depth > rc) {
  5755. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  5756. "3362 LUN queue depth changed from %d to %d\n",
  5757. phba->pport->cfg_lun_queue_depth, rc);
  5758. phba->pport->cfg_lun_queue_depth = rc;
  5759. }
  5760. /*
  5761. * Discover the port's supported feature set and match it against the
  5762. * hosts requests.
  5763. */
  5764. lpfc_request_features(phba, mboxq);
  5765. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5766. if (unlikely(rc)) {
  5767. rc = -EIO;
  5768. goto out_free_mbox;
  5769. }
  5770. /*
  5771. * The port must support FCP initiator mode as this is the
  5772. * only mode running in the host.
  5773. */
  5774. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5775. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5776. "0378 No support for fcpi mode.\n");
  5777. ftr_rsp++;
  5778. }
  5779. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5780. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5781. else
  5782. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5783. /*
  5784. * If the port cannot support the host's requested features
  5785. * then turn off the global config parameters to disable the
  5786. * feature in the driver. This is not a fatal error.
  5787. */
  5788. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5789. if (phba->cfg_enable_bg) {
  5790. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5791. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5792. else
  5793. ftr_rsp++;
  5794. }
  5795. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5796. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5797. ftr_rsp++;
  5798. if (ftr_rsp) {
  5799. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5800. "0379 Feature Mismatch Data: x%08x %08x "
  5801. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5802. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5803. phba->cfg_enable_npiv, phba->max_vpi);
  5804. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5805. phba->cfg_enable_bg = 0;
  5806. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5807. phba->cfg_enable_npiv = 0;
  5808. }
  5809. /* These SLI3 features are assumed in SLI4 */
  5810. spin_lock_irq(&phba->hbalock);
  5811. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5812. spin_unlock_irq(&phba->hbalock);
  5813. /*
  5814. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5815. * calls depends on these resources to complete port setup.
  5816. */
  5817. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5818. if (rc) {
  5819. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5820. "2920 Failed to alloc Resource IDs "
  5821. "rc = x%x\n", rc);
  5822. goto out_free_mbox;
  5823. }
  5824. /* Read the port's service parameters. */
  5825. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5826. if (rc) {
  5827. phba->link_state = LPFC_HBA_ERROR;
  5828. rc = -ENOMEM;
  5829. goto out_free_mbox;
  5830. }
  5831. mboxq->vport = vport;
  5832. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5833. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5834. if (rc == MBX_SUCCESS) {
  5835. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5836. rc = 0;
  5837. }
  5838. /*
  5839. * This memory was allocated by the lpfc_read_sparam routine. Release
  5840. * it to the mbuf pool.
  5841. */
  5842. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5843. kfree(mp);
  5844. mboxq->context1 = NULL;
  5845. if (unlikely(rc)) {
  5846. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5847. "0382 READ_SPARAM command failed "
  5848. "status %d, mbxStatus x%x\n",
  5849. rc, bf_get(lpfc_mqe_status, mqe));
  5850. phba->link_state = LPFC_HBA_ERROR;
  5851. rc = -EIO;
  5852. goto out_free_mbox;
  5853. }
  5854. lpfc_update_vport_wwn(vport);
  5855. /* Update the fc_host data structures with new wwn. */
  5856. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5857. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5858. /* update host els and scsi xri-sgl sizes and mappings */
  5859. rc = lpfc_sli4_xri_sgl_update(phba);
  5860. if (unlikely(rc)) {
  5861. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5862. "1400 Failed to update xri-sgl size and "
  5863. "mapping: %d\n", rc);
  5864. goto out_free_mbox;
  5865. }
  5866. /* register the els sgl pool to the port */
  5867. rc = lpfc_sli4_repost_els_sgl_list(phba);
  5868. if (unlikely(rc)) {
  5869. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5870. "0582 Error %d during els sgl post "
  5871. "operation\n", rc);
  5872. rc = -ENODEV;
  5873. goto out_free_mbox;
  5874. }
  5875. /* register the allocated scsi sgl pool to the port */
  5876. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5877. if (unlikely(rc)) {
  5878. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5879. "0383 Error %d during scsi sgl post "
  5880. "operation\n", rc);
  5881. /* Some Scsi buffers were moved to the abort scsi list */
  5882. /* A pci function reset will repost them */
  5883. rc = -ENODEV;
  5884. goto out_free_mbox;
  5885. }
  5886. /* Post the rpi header region to the device. */
  5887. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5888. if (unlikely(rc)) {
  5889. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5890. "0393 Error %d during rpi post operation\n",
  5891. rc);
  5892. rc = -ENODEV;
  5893. goto out_free_mbox;
  5894. }
  5895. lpfc_sli4_node_prep(phba);
  5896. /* Create all the SLI4 queues */
  5897. rc = lpfc_sli4_queue_create(phba);
  5898. if (rc) {
  5899. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5900. "3089 Failed to allocate queues\n");
  5901. rc = -ENODEV;
  5902. goto out_stop_timers;
  5903. }
  5904. /* Set up all the queues to the device */
  5905. rc = lpfc_sli4_queue_setup(phba);
  5906. if (unlikely(rc)) {
  5907. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5908. "0381 Error %d during queue setup.\n ", rc);
  5909. goto out_destroy_queue;
  5910. }
  5911. /* Arm the CQs and then EQs on device */
  5912. lpfc_sli4_arm_cqeq_intr(phba);
  5913. /* Indicate device interrupt mode */
  5914. phba->sli4_hba.intr_enable = 1;
  5915. /* Allow asynchronous mailbox command to go through */
  5916. spin_lock_irq(&phba->hbalock);
  5917. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5918. spin_unlock_irq(&phba->hbalock);
  5919. /* Post receive buffers to the device */
  5920. lpfc_sli4_rb_setup(phba);
  5921. /* Reset HBA FCF states after HBA reset */
  5922. phba->fcf.fcf_flag = 0;
  5923. phba->fcf.current_rec.flag = 0;
  5924. /* Start the ELS watchdog timer */
  5925. mod_timer(&vport->els_tmofunc,
  5926. jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
  5927. /* Start heart beat timer */
  5928. mod_timer(&phba->hb_tmofunc,
  5929. jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
  5930. phba->hb_outstanding = 0;
  5931. phba->last_completion_time = jiffies;
  5932. /* Start error attention (ERATT) polling timer */
  5933. mod_timer(&phba->eratt_poll,
  5934. jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
  5935. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5936. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5937. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5938. if (!rc) {
  5939. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5940. "2829 This device supports "
  5941. "Advanced Error Reporting (AER)\n");
  5942. spin_lock_irq(&phba->hbalock);
  5943. phba->hba_flag |= HBA_AER_ENABLED;
  5944. spin_unlock_irq(&phba->hbalock);
  5945. } else {
  5946. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5947. "2830 This device does not support "
  5948. "Advanced Error Reporting (AER)\n");
  5949. phba->cfg_aer_support = 0;
  5950. }
  5951. rc = 0;
  5952. }
  5953. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5954. /*
  5955. * The FC Port needs to register FCFI (index 0)
  5956. */
  5957. lpfc_reg_fcfi(phba, mboxq);
  5958. mboxq->vport = phba->pport;
  5959. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5960. if (rc != MBX_SUCCESS)
  5961. goto out_unset_queue;
  5962. rc = 0;
  5963. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5964. &mboxq->u.mqe.un.reg_fcfi);
  5965. /* Check if the port is configured to be disabled */
  5966. lpfc_sli_read_link_ste(phba);
  5967. }
  5968. /*
  5969. * The port is ready, set the host's link state to LINK_DOWN
  5970. * in preparation for link interrupts.
  5971. */
  5972. spin_lock_irq(&phba->hbalock);
  5973. phba->link_state = LPFC_LINK_DOWN;
  5974. spin_unlock_irq(&phba->hbalock);
  5975. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5976. (phba->hba_flag & LINK_DISABLED)) {
  5977. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5978. "3103 Adapter Link is disabled.\n");
  5979. lpfc_down_link(phba, mboxq);
  5980. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5981. if (rc != MBX_SUCCESS) {
  5982. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5983. "3104 Adapter failed to issue "
  5984. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5985. goto out_unset_queue;
  5986. }
  5987. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5988. /* don't perform init_link on SLI4 FC port loopback test */
  5989. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5990. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5991. if (rc)
  5992. goto out_unset_queue;
  5993. }
  5994. }
  5995. mempool_free(mboxq, phba->mbox_mem_pool);
  5996. return rc;
  5997. out_unset_queue:
  5998. /* Unset all the queues set up in this routine when error out */
  5999. lpfc_sli4_queue_unset(phba);
  6000. out_destroy_queue:
  6001. lpfc_sli4_queue_destroy(phba);
  6002. out_stop_timers:
  6003. lpfc_stop_hba_timers(phba);
  6004. out_free_mbox:
  6005. mempool_free(mboxq, phba->mbox_mem_pool);
  6006. return rc;
  6007. }
  6008. /**
  6009. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  6010. * @ptr: context object - pointer to hba structure.
  6011. *
  6012. * This is the callback function for mailbox timer. The mailbox
  6013. * timer is armed when a new mailbox command is issued and the timer
  6014. * is deleted when the mailbox complete. The function is called by
  6015. * the kernel timer code when a mailbox does not complete within
  6016. * expected time. This function wakes up the worker thread to
  6017. * process the mailbox timeout and returns. All the processing is
  6018. * done by the worker thread function lpfc_mbox_timeout_handler.
  6019. **/
  6020. void
  6021. lpfc_mbox_timeout(unsigned long ptr)
  6022. {
  6023. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  6024. unsigned long iflag;
  6025. uint32_t tmo_posted;
  6026. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  6027. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  6028. if (!tmo_posted)
  6029. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  6030. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  6031. if (!tmo_posted)
  6032. lpfc_worker_wake_up(phba);
  6033. return;
  6034. }
  6035. /**
  6036. * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
  6037. * are pending
  6038. * @phba: Pointer to HBA context object.
  6039. *
  6040. * This function checks if any mailbox completions are present on the mailbox
  6041. * completion queue.
  6042. **/
  6043. bool
  6044. lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
  6045. {
  6046. uint32_t idx;
  6047. struct lpfc_queue *mcq;
  6048. struct lpfc_mcqe *mcqe;
  6049. bool pending_completions = false;
  6050. if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
  6051. return false;
  6052. /* Check for completions on mailbox completion queue */
  6053. mcq = phba->sli4_hba.mbx_cq;
  6054. idx = mcq->hba_index;
  6055. while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
  6056. mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
  6057. if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
  6058. (!bf_get_le32(lpfc_trailer_async, mcqe))) {
  6059. pending_completions = true;
  6060. break;
  6061. }
  6062. idx = (idx + 1) % mcq->entry_count;
  6063. if (mcq->hba_index == idx)
  6064. break;
  6065. }
  6066. return pending_completions;
  6067. }
  6068. /**
  6069. * lpfc_sli4_process_missed_mbox_completions - process mbox completions
  6070. * that were missed.
  6071. * @phba: Pointer to HBA context object.
  6072. *
  6073. * For sli4, it is possible to miss an interrupt. As such mbox completions
  6074. * maybe missed causing erroneous mailbox timeouts to occur. This function
  6075. * checks to see if mbox completions are on the mailbox completion queue
  6076. * and will process all the completions associated with the eq for the
  6077. * mailbox completion queue.
  6078. **/
  6079. bool
  6080. lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
  6081. {
  6082. uint32_t eqidx;
  6083. struct lpfc_queue *fpeq = NULL;
  6084. struct lpfc_eqe *eqe;
  6085. bool mbox_pending;
  6086. if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
  6087. return false;
  6088. /* Find the eq associated with the mcq */
  6089. if (phba->sli4_hba.hba_eq)
  6090. for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
  6091. if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
  6092. phba->sli4_hba.mbx_cq->assoc_qid) {
  6093. fpeq = phba->sli4_hba.hba_eq[eqidx];
  6094. break;
  6095. }
  6096. if (!fpeq)
  6097. return false;
  6098. /* Turn off interrupts from this EQ */
  6099. lpfc_sli4_eq_clr_intr(fpeq);
  6100. /* Check to see if a mbox completion is pending */
  6101. mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
  6102. /*
  6103. * If a mbox completion is pending, process all the events on EQ
  6104. * associated with the mbox completion queue (this could include
  6105. * mailbox commands, async events, els commands, receive queue data
  6106. * and fcp commands)
  6107. */
  6108. if (mbox_pending)
  6109. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  6110. lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
  6111. fpeq->EQ_processed++;
  6112. }
  6113. /* Always clear and re-arm the EQ */
  6114. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  6115. return mbox_pending;
  6116. }
  6117. /**
  6118. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  6119. * @phba: Pointer to HBA context object.
  6120. *
  6121. * This function is called from worker thread when a mailbox command times out.
  6122. * The caller is not required to hold any locks. This function will reset the
  6123. * HBA and recover all the pending commands.
  6124. **/
  6125. void
  6126. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  6127. {
  6128. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  6129. MAILBOX_t *mb = NULL;
  6130. struct lpfc_sli *psli = &phba->sli;
  6131. /* If the mailbox completed, process the completion and return */
  6132. if (lpfc_sli4_process_missed_mbox_completions(phba))
  6133. return;
  6134. if (pmbox != NULL)
  6135. mb = &pmbox->u.mb;
  6136. /* Check the pmbox pointer first. There is a race condition
  6137. * between the mbox timeout handler getting executed in the
  6138. * worklist and the mailbox actually completing. When this
  6139. * race condition occurs, the mbox_active will be NULL.
  6140. */
  6141. spin_lock_irq(&phba->hbalock);
  6142. if (pmbox == NULL) {
  6143. lpfc_printf_log(phba, KERN_WARNING,
  6144. LOG_MBOX | LOG_SLI,
  6145. "0353 Active Mailbox cleared - mailbox timeout "
  6146. "exiting\n");
  6147. spin_unlock_irq(&phba->hbalock);
  6148. return;
  6149. }
  6150. /* Mbox cmd <mbxCommand> timeout */
  6151. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6152. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  6153. mb->mbxCommand,
  6154. phba->pport->port_state,
  6155. phba->sli.sli_flag,
  6156. phba->sli.mbox_active);
  6157. spin_unlock_irq(&phba->hbalock);
  6158. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  6159. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  6160. * it to fail all outstanding SCSI IO.
  6161. */
  6162. spin_lock_irq(&phba->pport->work_port_lock);
  6163. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  6164. spin_unlock_irq(&phba->pport->work_port_lock);
  6165. spin_lock_irq(&phba->hbalock);
  6166. phba->link_state = LPFC_LINK_UNKNOWN;
  6167. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  6168. spin_unlock_irq(&phba->hbalock);
  6169. lpfc_sli_abort_fcp_rings(phba);
  6170. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6171. "0345 Resetting board due to mailbox timeout\n");
  6172. /* Reset the HBA device */
  6173. lpfc_reset_hba(phba);
  6174. }
  6175. /**
  6176. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  6177. * @phba: Pointer to HBA context object.
  6178. * @pmbox: Pointer to mailbox object.
  6179. * @flag: Flag indicating how the mailbox need to be processed.
  6180. *
  6181. * This function is called by discovery code and HBA management code
  6182. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  6183. * function gets the hbalock to protect the data structures.
  6184. * The mailbox command can be submitted in polling mode, in which case
  6185. * this function will wait in a polling loop for the completion of the
  6186. * mailbox.
  6187. * If the mailbox is submitted in no_wait mode (not polling) the
  6188. * function will submit the command and returns immediately without waiting
  6189. * for the mailbox completion. The no_wait is supported only when HBA
  6190. * is in SLI2/SLI3 mode - interrupts are enabled.
  6191. * The SLI interface allows only one mailbox pending at a time. If the
  6192. * mailbox is issued in polling mode and there is already a mailbox
  6193. * pending, then the function will return an error. If the mailbox is issued
  6194. * in NO_WAIT mode and there is a mailbox pending already, the function
  6195. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  6196. * The sli layer owns the mailbox object until the completion of mailbox
  6197. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  6198. * return codes the caller owns the mailbox command after the return of
  6199. * the function.
  6200. **/
  6201. static int
  6202. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  6203. uint32_t flag)
  6204. {
  6205. MAILBOX_t *mbx;
  6206. struct lpfc_sli *psli = &phba->sli;
  6207. uint32_t status, evtctr;
  6208. uint32_t ha_copy, hc_copy;
  6209. int i;
  6210. unsigned long timeout;
  6211. unsigned long drvr_flag = 0;
  6212. uint32_t word0, ldata;
  6213. void __iomem *to_slim;
  6214. int processing_queue = 0;
  6215. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6216. if (!pmbox) {
  6217. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6218. /* processing mbox queue from intr_handler */
  6219. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6220. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6221. return MBX_SUCCESS;
  6222. }
  6223. processing_queue = 1;
  6224. pmbox = lpfc_mbox_get(phba);
  6225. if (!pmbox) {
  6226. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6227. return MBX_SUCCESS;
  6228. }
  6229. }
  6230. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  6231. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  6232. if(!pmbox->vport) {
  6233. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6234. lpfc_printf_log(phba, KERN_ERR,
  6235. LOG_MBOX | LOG_VPORT,
  6236. "1806 Mbox x%x failed. No vport\n",
  6237. pmbox->u.mb.mbxCommand);
  6238. dump_stack();
  6239. goto out_not_finished;
  6240. }
  6241. }
  6242. /* If the PCI channel is in offline state, do not post mbox. */
  6243. if (unlikely(pci_channel_offline(phba->pcidev))) {
  6244. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6245. goto out_not_finished;
  6246. }
  6247. /* If HBA has a deferred error attention, fail the iocb. */
  6248. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  6249. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6250. goto out_not_finished;
  6251. }
  6252. psli = &phba->sli;
  6253. mbx = &pmbox->u.mb;
  6254. status = MBX_SUCCESS;
  6255. if (phba->link_state == LPFC_HBA_ERROR) {
  6256. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6257. /* Mbox command <mbxCommand> cannot issue */
  6258. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6259. "(%d):0311 Mailbox command x%x cannot "
  6260. "issue Data: x%x x%x\n",
  6261. pmbox->vport ? pmbox->vport->vpi : 0,
  6262. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6263. goto out_not_finished;
  6264. }
  6265. if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6266. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6267. !(hc_copy & HC_MBINT_ENA)) {
  6268. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6269. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6270. "(%d):2528 Mailbox command x%x cannot "
  6271. "issue Data: x%x x%x\n",
  6272. pmbox->vport ? pmbox->vport->vpi : 0,
  6273. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6274. goto out_not_finished;
  6275. }
  6276. }
  6277. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6278. /* Polling for a mbox command when another one is already active
  6279. * is not allowed in SLI. Also, the driver must have established
  6280. * SLI2 mode to queue and process multiple mbox commands.
  6281. */
  6282. if (flag & MBX_POLL) {
  6283. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6284. /* Mbox command <mbxCommand> cannot issue */
  6285. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6286. "(%d):2529 Mailbox command x%x "
  6287. "cannot issue Data: x%x x%x\n",
  6288. pmbox->vport ? pmbox->vport->vpi : 0,
  6289. pmbox->u.mb.mbxCommand,
  6290. psli->sli_flag, flag);
  6291. goto out_not_finished;
  6292. }
  6293. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  6294. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6295. /* Mbox command <mbxCommand> cannot issue */
  6296. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6297. "(%d):2530 Mailbox command x%x "
  6298. "cannot issue Data: x%x x%x\n",
  6299. pmbox->vport ? pmbox->vport->vpi : 0,
  6300. pmbox->u.mb.mbxCommand,
  6301. psli->sli_flag, flag);
  6302. goto out_not_finished;
  6303. }
  6304. /* Another mailbox command is still being processed, queue this
  6305. * command to be processed later.
  6306. */
  6307. lpfc_mbox_put(phba, pmbox);
  6308. /* Mbox cmd issue - BUSY */
  6309. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6310. "(%d):0308 Mbox cmd issue - BUSY Data: "
  6311. "x%x x%x x%x x%x\n",
  6312. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  6313. mbx->mbxCommand, phba->pport->port_state,
  6314. psli->sli_flag, flag);
  6315. psli->slistat.mbox_busy++;
  6316. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6317. if (pmbox->vport) {
  6318. lpfc_debugfs_disc_trc(pmbox->vport,
  6319. LPFC_DISC_TRC_MBOX_VPORT,
  6320. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  6321. (uint32_t)mbx->mbxCommand,
  6322. mbx->un.varWords[0], mbx->un.varWords[1]);
  6323. }
  6324. else {
  6325. lpfc_debugfs_disc_trc(phba->pport,
  6326. LPFC_DISC_TRC_MBOX,
  6327. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  6328. (uint32_t)mbx->mbxCommand,
  6329. mbx->un.varWords[0], mbx->un.varWords[1]);
  6330. }
  6331. return MBX_BUSY;
  6332. }
  6333. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6334. /* If we are not polling, we MUST be in SLI2 mode */
  6335. if (flag != MBX_POLL) {
  6336. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  6337. (mbx->mbxCommand != MBX_KILL_BOARD)) {
  6338. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6339. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6340. /* Mbox command <mbxCommand> cannot issue */
  6341. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6342. "(%d):2531 Mailbox command x%x "
  6343. "cannot issue Data: x%x x%x\n",
  6344. pmbox->vport ? pmbox->vport->vpi : 0,
  6345. pmbox->u.mb.mbxCommand,
  6346. psli->sli_flag, flag);
  6347. goto out_not_finished;
  6348. }
  6349. /* timeout active mbox command */
  6350. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6351. 1000);
  6352. mod_timer(&psli->mbox_tmo, jiffies + timeout);
  6353. }
  6354. /* Mailbox cmd <cmd> issue */
  6355. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6356. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  6357. "x%x\n",
  6358. pmbox->vport ? pmbox->vport->vpi : 0,
  6359. mbx->mbxCommand, phba->pport->port_state,
  6360. psli->sli_flag, flag);
  6361. if (mbx->mbxCommand != MBX_HEARTBEAT) {
  6362. if (pmbox->vport) {
  6363. lpfc_debugfs_disc_trc(pmbox->vport,
  6364. LPFC_DISC_TRC_MBOX_VPORT,
  6365. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6366. (uint32_t)mbx->mbxCommand,
  6367. mbx->un.varWords[0], mbx->un.varWords[1]);
  6368. }
  6369. else {
  6370. lpfc_debugfs_disc_trc(phba->pport,
  6371. LPFC_DISC_TRC_MBOX,
  6372. "MBOX Send: cmd:x%x mb:x%x x%x",
  6373. (uint32_t)mbx->mbxCommand,
  6374. mbx->un.varWords[0], mbx->un.varWords[1]);
  6375. }
  6376. }
  6377. psli->slistat.mbox_cmd++;
  6378. evtctr = psli->slistat.mbox_event;
  6379. /* next set own bit for the adapter and copy over command word */
  6380. mbx->mbxOwner = OWN_CHIP;
  6381. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6382. /* Populate mbox extension offset word. */
  6383. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6384. *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
  6385. = (uint8_t *)phba->mbox_ext
  6386. - (uint8_t *)phba->mbox;
  6387. }
  6388. /* Copy the mailbox extension data */
  6389. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6390. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6391. (uint8_t *)phba->mbox_ext,
  6392. pmbox->in_ext_byte_len);
  6393. }
  6394. /* Copy command data to host SLIM area */
  6395. lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
  6396. } else {
  6397. /* Populate mbox extension offset word. */
  6398. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6399. *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
  6400. = MAILBOX_HBA_EXT_OFFSET;
  6401. /* Copy the mailbox extension data */
  6402. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6403. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6404. MAILBOX_HBA_EXT_OFFSET,
  6405. pmbox->context2, pmbox->in_ext_byte_len);
  6406. }
  6407. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6408. /* copy command data into host mbox for cmpl */
  6409. lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
  6410. }
  6411. /* First copy mbox command data to HBA SLIM, skip past first
  6412. word */
  6413. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6414. lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
  6415. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6416. /* Next copy over first word, with mbxOwner set */
  6417. ldata = *((uint32_t *)mbx);
  6418. to_slim = phba->MBslimaddr;
  6419. writel(ldata, to_slim);
  6420. readl(to_slim); /* flush */
  6421. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6422. /* switch over to host mailbox */
  6423. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6424. }
  6425. }
  6426. wmb();
  6427. switch (flag) {
  6428. case MBX_NOWAIT:
  6429. /* Set up reference to mailbox command */
  6430. psli->mbox_active = pmbox;
  6431. /* Interrupt board to do it */
  6432. writel(CA_MBATT, phba->CAregaddr);
  6433. readl(phba->CAregaddr); /* flush */
  6434. /* Don't wait for it to finish, just return */
  6435. break;
  6436. case MBX_POLL:
  6437. /* Set up null reference to mailbox command */
  6438. psli->mbox_active = NULL;
  6439. /* Interrupt board to do it */
  6440. writel(CA_MBATT, phba->CAregaddr);
  6441. readl(phba->CAregaddr); /* flush */
  6442. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6443. /* First read mbox status word */
  6444. word0 = *((uint32_t *)phba->mbox);
  6445. word0 = le32_to_cpu(word0);
  6446. } else {
  6447. /* First read mbox status word */
  6448. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6449. spin_unlock_irqrestore(&phba->hbalock,
  6450. drvr_flag);
  6451. goto out_not_finished;
  6452. }
  6453. }
  6454. /* Read the HBA Host Attention Register */
  6455. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6456. spin_unlock_irqrestore(&phba->hbalock,
  6457. drvr_flag);
  6458. goto out_not_finished;
  6459. }
  6460. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6461. 1000) + jiffies;
  6462. i = 0;
  6463. /* Wait for command to complete */
  6464. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6465. (!(ha_copy & HA_MBATT) &&
  6466. (phba->link_state > LPFC_WARM_START))) {
  6467. if (time_after(jiffies, timeout)) {
  6468. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6469. spin_unlock_irqrestore(&phba->hbalock,
  6470. drvr_flag);
  6471. goto out_not_finished;
  6472. }
  6473. /* Check if we took a mbox interrupt while we were
  6474. polling */
  6475. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6476. && (evtctr != psli->slistat.mbox_event))
  6477. break;
  6478. if (i++ > 10) {
  6479. spin_unlock_irqrestore(&phba->hbalock,
  6480. drvr_flag);
  6481. msleep(1);
  6482. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6483. }
  6484. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6485. /* First copy command data */
  6486. word0 = *((uint32_t *)phba->mbox);
  6487. word0 = le32_to_cpu(word0);
  6488. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6489. MAILBOX_t *slimmb;
  6490. uint32_t slimword0;
  6491. /* Check real SLIM for any errors */
  6492. slimword0 = readl(phba->MBslimaddr);
  6493. slimmb = (MAILBOX_t *) & slimword0;
  6494. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6495. && slimmb->mbxStatus) {
  6496. psli->sli_flag &=
  6497. ~LPFC_SLI_ACTIVE;
  6498. word0 = slimword0;
  6499. }
  6500. }
  6501. } else {
  6502. /* First copy command data */
  6503. word0 = readl(phba->MBslimaddr);
  6504. }
  6505. /* Read the HBA Host Attention Register */
  6506. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6507. spin_unlock_irqrestore(&phba->hbalock,
  6508. drvr_flag);
  6509. goto out_not_finished;
  6510. }
  6511. }
  6512. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6513. /* copy results back to user */
  6514. lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
  6515. /* Copy the mailbox extension data */
  6516. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6517. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6518. pmbox->context2,
  6519. pmbox->out_ext_byte_len);
  6520. }
  6521. } else {
  6522. /* First copy command data */
  6523. lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
  6524. MAILBOX_CMD_SIZE);
  6525. /* Copy the mailbox extension data */
  6526. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6527. lpfc_memcpy_from_slim(pmbox->context2,
  6528. phba->MBslimaddr +
  6529. MAILBOX_HBA_EXT_OFFSET,
  6530. pmbox->out_ext_byte_len);
  6531. }
  6532. }
  6533. writel(HA_MBATT, phba->HAregaddr);
  6534. readl(phba->HAregaddr); /* flush */
  6535. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6536. status = mbx->mbxStatus;
  6537. }
  6538. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6539. return status;
  6540. out_not_finished:
  6541. if (processing_queue) {
  6542. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6543. lpfc_mbox_cmpl_put(phba, pmbox);
  6544. }
  6545. return MBX_NOT_FINISHED;
  6546. }
  6547. /**
  6548. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6549. * @phba: Pointer to HBA context object.
  6550. *
  6551. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6552. * the driver internal pending mailbox queue. It will then try to wait out the
  6553. * possible outstanding mailbox command before return.
  6554. *
  6555. * Returns:
  6556. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6557. * the outstanding mailbox command timed out.
  6558. **/
  6559. static int
  6560. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6561. {
  6562. struct lpfc_sli *psli = &phba->sli;
  6563. int rc = 0;
  6564. unsigned long timeout = 0;
  6565. /* Mark the asynchronous mailbox command posting as blocked */
  6566. spin_lock_irq(&phba->hbalock);
  6567. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6568. /* Determine how long we might wait for the active mailbox
  6569. * command to be gracefully completed by firmware.
  6570. */
  6571. if (phba->sli.mbox_active)
  6572. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6573. phba->sli.mbox_active) *
  6574. 1000) + jiffies;
  6575. spin_unlock_irq(&phba->hbalock);
  6576. /* Make sure the mailbox is really active */
  6577. if (timeout)
  6578. lpfc_sli4_process_missed_mbox_completions(phba);
  6579. /* Wait for the outstnading mailbox command to complete */
  6580. while (phba->sli.mbox_active) {
  6581. /* Check active mailbox complete status every 2ms */
  6582. msleep(2);
  6583. if (time_after(jiffies, timeout)) {
  6584. /* Timeout, marked the outstanding cmd not complete */
  6585. rc = 1;
  6586. break;
  6587. }
  6588. }
  6589. /* Can not cleanly block async mailbox command, fails it */
  6590. if (rc) {
  6591. spin_lock_irq(&phba->hbalock);
  6592. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6593. spin_unlock_irq(&phba->hbalock);
  6594. }
  6595. return rc;
  6596. }
  6597. /**
  6598. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6599. * @phba: Pointer to HBA context object.
  6600. *
  6601. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6602. * commands from the driver internal pending mailbox queue. It makes sure
  6603. * that there is no outstanding mailbox command before resuming posting
  6604. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6605. * mailbox command, it will try to wait it out before resuming asynchronous
  6606. * mailbox command posting.
  6607. **/
  6608. static void
  6609. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6610. {
  6611. struct lpfc_sli *psli = &phba->sli;
  6612. spin_lock_irq(&phba->hbalock);
  6613. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6614. /* Asynchronous mailbox posting is not blocked, do nothing */
  6615. spin_unlock_irq(&phba->hbalock);
  6616. return;
  6617. }
  6618. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6619. * successful or timeout, after timing-out the outstanding mailbox
  6620. * command shall always be removed, so just unblock posting async
  6621. * mailbox command and resume
  6622. */
  6623. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6624. spin_unlock_irq(&phba->hbalock);
  6625. /* wake up worker thread to post asynchronlous mailbox command */
  6626. lpfc_worker_wake_up(phba);
  6627. }
  6628. /**
  6629. * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
  6630. * @phba: Pointer to HBA context object.
  6631. * @mboxq: Pointer to mailbox object.
  6632. *
  6633. * The function waits for the bootstrap mailbox register ready bit from
  6634. * port for twice the regular mailbox command timeout value.
  6635. *
  6636. * 0 - no timeout on waiting for bootstrap mailbox register ready.
  6637. * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
  6638. **/
  6639. static int
  6640. lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6641. {
  6642. uint32_t db_ready;
  6643. unsigned long timeout;
  6644. struct lpfc_register bmbx_reg;
  6645. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6646. * 1000) + jiffies;
  6647. do {
  6648. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6649. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6650. if (!db_ready)
  6651. msleep(2);
  6652. if (time_after(jiffies, timeout))
  6653. return MBXERR_ERROR;
  6654. } while (!db_ready);
  6655. return 0;
  6656. }
  6657. /**
  6658. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6659. * @phba: Pointer to HBA context object.
  6660. * @mboxq: Pointer to mailbox object.
  6661. *
  6662. * The function posts a mailbox to the port. The mailbox is expected
  6663. * to be comletely filled in and ready for the port to operate on it.
  6664. * This routine executes a synchronous completion operation on the
  6665. * mailbox by polling for its completion.
  6666. *
  6667. * The caller must not be holding any locks when calling this routine.
  6668. *
  6669. * Returns:
  6670. * MBX_SUCCESS - mailbox posted successfully
  6671. * Any of the MBX error values.
  6672. **/
  6673. static int
  6674. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6675. {
  6676. int rc = MBX_SUCCESS;
  6677. unsigned long iflag;
  6678. uint32_t mcqe_status;
  6679. uint32_t mbx_cmnd;
  6680. struct lpfc_sli *psli = &phba->sli;
  6681. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6682. struct lpfc_bmbx_create *mbox_rgn;
  6683. struct dma_address *dma_address;
  6684. /*
  6685. * Only one mailbox can be active to the bootstrap mailbox region
  6686. * at a time and there is no queueing provided.
  6687. */
  6688. spin_lock_irqsave(&phba->hbalock, iflag);
  6689. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6690. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6691. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6692. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6693. "cannot issue Data: x%x x%x\n",
  6694. mboxq->vport ? mboxq->vport->vpi : 0,
  6695. mboxq->u.mb.mbxCommand,
  6696. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6697. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6698. psli->sli_flag, MBX_POLL);
  6699. return MBXERR_ERROR;
  6700. }
  6701. /* The server grabs the token and owns it until release */
  6702. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6703. phba->sli.mbox_active = mboxq;
  6704. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6705. /* wait for bootstrap mbox register for readyness */
  6706. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6707. if (rc)
  6708. goto exit;
  6709. /*
  6710. * Initialize the bootstrap memory region to avoid stale data areas
  6711. * in the mailbox post. Then copy the caller's mailbox contents to
  6712. * the bmbx mailbox region.
  6713. */
  6714. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6715. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6716. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6717. sizeof(struct lpfc_mqe));
  6718. /* Post the high mailbox dma address to the port and wait for ready. */
  6719. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6720. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6721. /* wait for bootstrap mbox register for hi-address write done */
  6722. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6723. if (rc)
  6724. goto exit;
  6725. /* Post the low mailbox dma address to the port. */
  6726. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6727. /* wait for bootstrap mbox register for low address write done */
  6728. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6729. if (rc)
  6730. goto exit;
  6731. /*
  6732. * Read the CQ to ensure the mailbox has completed.
  6733. * If so, update the mailbox status so that the upper layers
  6734. * can complete the request normally.
  6735. */
  6736. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6737. sizeof(struct lpfc_mqe));
  6738. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6739. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6740. sizeof(struct lpfc_mcqe));
  6741. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6742. /*
  6743. * When the CQE status indicates a failure and the mailbox status
  6744. * indicates success then copy the CQE status into the mailbox status
  6745. * (and prefix it with x4000).
  6746. */
  6747. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6748. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6749. bf_set(lpfc_mqe_status, mb,
  6750. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6751. rc = MBXERR_ERROR;
  6752. } else
  6753. lpfc_sli4_swap_str(phba, mboxq);
  6754. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6755. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6756. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6757. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6758. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6759. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6760. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6761. bf_get(lpfc_mqe_status, mb),
  6762. mb->un.mb_words[0], mb->un.mb_words[1],
  6763. mb->un.mb_words[2], mb->un.mb_words[3],
  6764. mb->un.mb_words[4], mb->un.mb_words[5],
  6765. mb->un.mb_words[6], mb->un.mb_words[7],
  6766. mb->un.mb_words[8], mb->un.mb_words[9],
  6767. mb->un.mb_words[10], mb->un.mb_words[11],
  6768. mb->un.mb_words[12], mboxq->mcqe.word0,
  6769. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6770. mboxq->mcqe.trailer);
  6771. exit:
  6772. /* We are holding the token, no needed for lock when release */
  6773. spin_lock_irqsave(&phba->hbalock, iflag);
  6774. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6775. phba->sli.mbox_active = NULL;
  6776. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6777. return rc;
  6778. }
  6779. /**
  6780. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6781. * @phba: Pointer to HBA context object.
  6782. * @pmbox: Pointer to mailbox object.
  6783. * @flag: Flag indicating how the mailbox need to be processed.
  6784. *
  6785. * This function is called by discovery code and HBA management code to submit
  6786. * a mailbox command to firmware with SLI-4 interface spec.
  6787. *
  6788. * Return codes the caller owns the mailbox command after the return of the
  6789. * function.
  6790. **/
  6791. static int
  6792. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6793. uint32_t flag)
  6794. {
  6795. struct lpfc_sli *psli = &phba->sli;
  6796. unsigned long iflags;
  6797. int rc;
  6798. /* dump from issue mailbox command if setup */
  6799. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6800. rc = lpfc_mbox_dev_check(phba);
  6801. if (unlikely(rc)) {
  6802. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6803. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6804. "cannot issue Data: x%x x%x\n",
  6805. mboxq->vport ? mboxq->vport->vpi : 0,
  6806. mboxq->u.mb.mbxCommand,
  6807. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6808. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6809. psli->sli_flag, flag);
  6810. goto out_not_finished;
  6811. }
  6812. /* Detect polling mode and jump to a handler */
  6813. if (!phba->sli4_hba.intr_enable) {
  6814. if (flag == MBX_POLL)
  6815. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6816. else
  6817. rc = -EIO;
  6818. if (rc != MBX_SUCCESS)
  6819. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6820. "(%d):2541 Mailbox command x%x "
  6821. "(x%x/x%x) failure: "
  6822. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6823. "Data: x%x x%x\n,",
  6824. mboxq->vport ? mboxq->vport->vpi : 0,
  6825. mboxq->u.mb.mbxCommand,
  6826. lpfc_sli_config_mbox_subsys_get(phba,
  6827. mboxq),
  6828. lpfc_sli_config_mbox_opcode_get(phba,
  6829. mboxq),
  6830. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6831. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6832. bf_get(lpfc_mcqe_ext_status,
  6833. &mboxq->mcqe),
  6834. psli->sli_flag, flag);
  6835. return rc;
  6836. } else if (flag == MBX_POLL) {
  6837. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6838. "(%d):2542 Try to issue mailbox command "
  6839. "x%x (x%x/x%x) synchronously ahead of async"
  6840. "mailbox command queue: x%x x%x\n",
  6841. mboxq->vport ? mboxq->vport->vpi : 0,
  6842. mboxq->u.mb.mbxCommand,
  6843. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6844. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6845. psli->sli_flag, flag);
  6846. /* Try to block the asynchronous mailbox posting */
  6847. rc = lpfc_sli4_async_mbox_block(phba);
  6848. if (!rc) {
  6849. /* Successfully blocked, now issue sync mbox cmd */
  6850. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6851. if (rc != MBX_SUCCESS)
  6852. lpfc_printf_log(phba, KERN_WARNING,
  6853. LOG_MBOX | LOG_SLI,
  6854. "(%d):2597 Sync Mailbox command "
  6855. "x%x (x%x/x%x) failure: "
  6856. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6857. "Data: x%x x%x\n,",
  6858. mboxq->vport ? mboxq->vport->vpi : 0,
  6859. mboxq->u.mb.mbxCommand,
  6860. lpfc_sli_config_mbox_subsys_get(phba,
  6861. mboxq),
  6862. lpfc_sli_config_mbox_opcode_get(phba,
  6863. mboxq),
  6864. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6865. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6866. bf_get(lpfc_mcqe_ext_status,
  6867. &mboxq->mcqe),
  6868. psli->sli_flag, flag);
  6869. /* Unblock the async mailbox posting afterward */
  6870. lpfc_sli4_async_mbox_unblock(phba);
  6871. }
  6872. return rc;
  6873. }
  6874. /* Now, interrupt mode asynchrous mailbox command */
  6875. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6876. if (rc) {
  6877. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6878. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6879. "cannot issue Data: x%x x%x\n",
  6880. mboxq->vport ? mboxq->vport->vpi : 0,
  6881. mboxq->u.mb.mbxCommand,
  6882. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6883. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6884. psli->sli_flag, flag);
  6885. goto out_not_finished;
  6886. }
  6887. /* Put the mailbox command to the driver internal FIFO */
  6888. psli->slistat.mbox_busy++;
  6889. spin_lock_irqsave(&phba->hbalock, iflags);
  6890. lpfc_mbox_put(phba, mboxq);
  6891. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6892. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6893. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6894. "x%x (x%x/x%x) x%x x%x x%x\n",
  6895. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6896. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6897. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6898. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6899. phba->pport->port_state,
  6900. psli->sli_flag, MBX_NOWAIT);
  6901. /* Wake up worker thread to transport mailbox command from head */
  6902. lpfc_worker_wake_up(phba);
  6903. return MBX_BUSY;
  6904. out_not_finished:
  6905. return MBX_NOT_FINISHED;
  6906. }
  6907. /**
  6908. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6909. * @phba: Pointer to HBA context object.
  6910. *
  6911. * This function is called by worker thread to send a mailbox command to
  6912. * SLI4 HBA firmware.
  6913. *
  6914. **/
  6915. int
  6916. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6917. {
  6918. struct lpfc_sli *psli = &phba->sli;
  6919. LPFC_MBOXQ_t *mboxq;
  6920. int rc = MBX_SUCCESS;
  6921. unsigned long iflags;
  6922. struct lpfc_mqe *mqe;
  6923. uint32_t mbx_cmnd;
  6924. /* Check interrupt mode before post async mailbox command */
  6925. if (unlikely(!phba->sli4_hba.intr_enable))
  6926. return MBX_NOT_FINISHED;
  6927. /* Check for mailbox command service token */
  6928. spin_lock_irqsave(&phba->hbalock, iflags);
  6929. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6930. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6931. return MBX_NOT_FINISHED;
  6932. }
  6933. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6934. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6935. return MBX_NOT_FINISHED;
  6936. }
  6937. if (unlikely(phba->sli.mbox_active)) {
  6938. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6939. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6940. "0384 There is pending active mailbox cmd\n");
  6941. return MBX_NOT_FINISHED;
  6942. }
  6943. /* Take the mailbox command service token */
  6944. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6945. /* Get the next mailbox command from head of queue */
  6946. mboxq = lpfc_mbox_get(phba);
  6947. /* If no more mailbox command waiting for post, we're done */
  6948. if (!mboxq) {
  6949. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6950. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6951. return MBX_SUCCESS;
  6952. }
  6953. phba->sli.mbox_active = mboxq;
  6954. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6955. /* Check device readiness for posting mailbox command */
  6956. rc = lpfc_mbox_dev_check(phba);
  6957. if (unlikely(rc))
  6958. /* Driver clean routine will clean up pending mailbox */
  6959. goto out_not_finished;
  6960. /* Prepare the mbox command to be posted */
  6961. mqe = &mboxq->u.mqe;
  6962. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6963. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6964. mod_timer(&psli->mbox_tmo, (jiffies +
  6965. msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
  6966. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6967. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6968. "x%x x%x\n",
  6969. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6970. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6971. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6972. phba->pport->port_state, psli->sli_flag);
  6973. if (mbx_cmnd != MBX_HEARTBEAT) {
  6974. if (mboxq->vport) {
  6975. lpfc_debugfs_disc_trc(mboxq->vport,
  6976. LPFC_DISC_TRC_MBOX_VPORT,
  6977. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6978. mbx_cmnd, mqe->un.mb_words[0],
  6979. mqe->un.mb_words[1]);
  6980. } else {
  6981. lpfc_debugfs_disc_trc(phba->pport,
  6982. LPFC_DISC_TRC_MBOX,
  6983. "MBOX Send: cmd:x%x mb:x%x x%x",
  6984. mbx_cmnd, mqe->un.mb_words[0],
  6985. mqe->un.mb_words[1]);
  6986. }
  6987. }
  6988. psli->slistat.mbox_cmd++;
  6989. /* Post the mailbox command to the port */
  6990. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6991. if (rc != MBX_SUCCESS) {
  6992. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6993. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6994. "cannot issue Data: x%x x%x\n",
  6995. mboxq->vport ? mboxq->vport->vpi : 0,
  6996. mboxq->u.mb.mbxCommand,
  6997. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6998. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6999. psli->sli_flag, MBX_NOWAIT);
  7000. goto out_not_finished;
  7001. }
  7002. return rc;
  7003. out_not_finished:
  7004. spin_lock_irqsave(&phba->hbalock, iflags);
  7005. if (phba->sli.mbox_active) {
  7006. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  7007. __lpfc_mbox_cmpl_put(phba, mboxq);
  7008. /* Release the token */
  7009. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7010. phba->sli.mbox_active = NULL;
  7011. }
  7012. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7013. return MBX_NOT_FINISHED;
  7014. }
  7015. /**
  7016. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  7017. * @phba: Pointer to HBA context object.
  7018. * @pmbox: Pointer to mailbox object.
  7019. * @flag: Flag indicating how the mailbox need to be processed.
  7020. *
  7021. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  7022. * the API jump table function pointer from the lpfc_hba struct.
  7023. *
  7024. * Return codes the caller owns the mailbox command after the return of the
  7025. * function.
  7026. **/
  7027. int
  7028. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  7029. {
  7030. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  7031. }
  7032. /**
  7033. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  7034. * @phba: The hba struct for which this call is being executed.
  7035. * @dev_grp: The HBA PCI-Device group number.
  7036. *
  7037. * This routine sets up the mbox interface API function jump table in @phba
  7038. * struct.
  7039. * Returns: 0 - success, -ENODEV - failure.
  7040. **/
  7041. int
  7042. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7043. {
  7044. switch (dev_grp) {
  7045. case LPFC_PCI_DEV_LP:
  7046. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  7047. phba->lpfc_sli_handle_slow_ring_event =
  7048. lpfc_sli_handle_slow_ring_event_s3;
  7049. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  7050. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  7051. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  7052. break;
  7053. case LPFC_PCI_DEV_OC:
  7054. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  7055. phba->lpfc_sli_handle_slow_ring_event =
  7056. lpfc_sli_handle_slow_ring_event_s4;
  7057. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  7058. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  7059. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  7060. break;
  7061. default:
  7062. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7063. "1420 Invalid HBA PCI-device group: 0x%x\n",
  7064. dev_grp);
  7065. return -ENODEV;
  7066. break;
  7067. }
  7068. return 0;
  7069. }
  7070. /**
  7071. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  7072. * @phba: Pointer to HBA context object.
  7073. * @pring: Pointer to driver SLI ring object.
  7074. * @piocb: Pointer to address of newly added command iocb.
  7075. *
  7076. * This function is called with hbalock held to add a command
  7077. * iocb to the txq when SLI layer cannot submit the command iocb
  7078. * to the ring.
  7079. **/
  7080. void
  7081. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7082. struct lpfc_iocbq *piocb)
  7083. {
  7084. /* Insert the caller's iocb in the txq tail for later processing. */
  7085. list_add_tail(&piocb->list, &pring->txq);
  7086. }
  7087. /**
  7088. * lpfc_sli_next_iocb - Get the next iocb in the txq
  7089. * @phba: Pointer to HBA context object.
  7090. * @pring: Pointer to driver SLI ring object.
  7091. * @piocb: Pointer to address of newly added command iocb.
  7092. *
  7093. * This function is called with hbalock held before a new
  7094. * iocb is submitted to the firmware. This function checks
  7095. * txq to flush the iocbs in txq to Firmware before
  7096. * submitting new iocbs to the Firmware.
  7097. * If there are iocbs in the txq which need to be submitted
  7098. * to firmware, lpfc_sli_next_iocb returns the first element
  7099. * of the txq after dequeuing it from txq.
  7100. * If there is no iocb in the txq then the function will return
  7101. * *piocb and *piocb is set to NULL. Caller needs to check
  7102. * *piocb to find if there are more commands in the txq.
  7103. **/
  7104. static struct lpfc_iocbq *
  7105. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7106. struct lpfc_iocbq **piocb)
  7107. {
  7108. struct lpfc_iocbq * nextiocb;
  7109. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  7110. if (!nextiocb) {
  7111. nextiocb = *piocb;
  7112. *piocb = NULL;
  7113. }
  7114. return nextiocb;
  7115. }
  7116. /**
  7117. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  7118. * @phba: Pointer to HBA context object.
  7119. * @ring_number: SLI ring number to issue iocb on.
  7120. * @piocb: Pointer to command iocb.
  7121. * @flag: Flag indicating if this command can be put into txq.
  7122. *
  7123. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  7124. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  7125. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  7126. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  7127. * this function allows only iocbs for posting buffers. This function finds
  7128. * next available slot in the command ring and posts the command to the
  7129. * available slot and writes the port attention register to request HBA start
  7130. * processing new iocb. If there is no slot available in the ring and
  7131. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  7132. * the function returns IOCB_BUSY.
  7133. *
  7134. * This function is called with hbalock held. The function will return success
  7135. * after it successfully submit the iocb to firmware or after adding to the
  7136. * txq.
  7137. **/
  7138. static int
  7139. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  7140. struct lpfc_iocbq *piocb, uint32_t flag)
  7141. {
  7142. struct lpfc_iocbq *nextiocb;
  7143. IOCB_t *iocb;
  7144. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7145. if (piocb->iocb_cmpl && (!piocb->vport) &&
  7146. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  7147. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  7148. lpfc_printf_log(phba, KERN_ERR,
  7149. LOG_SLI | LOG_VPORT,
  7150. "1807 IOCB x%x failed. No vport\n",
  7151. piocb->iocb.ulpCommand);
  7152. dump_stack();
  7153. return IOCB_ERROR;
  7154. }
  7155. /* If the PCI channel is in offline state, do not post iocbs. */
  7156. if (unlikely(pci_channel_offline(phba->pcidev)))
  7157. return IOCB_ERROR;
  7158. /* If HBA has a deferred error attention, fail the iocb. */
  7159. if (unlikely(phba->hba_flag & DEFER_ERATT))
  7160. return IOCB_ERROR;
  7161. /*
  7162. * We should never get an IOCB if we are in a < LINK_DOWN state
  7163. */
  7164. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  7165. return IOCB_ERROR;
  7166. /*
  7167. * Check to see if we are blocking IOCB processing because of a
  7168. * outstanding event.
  7169. */
  7170. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  7171. goto iocb_busy;
  7172. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  7173. /*
  7174. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  7175. * can be issued if the link is not up.
  7176. */
  7177. switch (piocb->iocb.ulpCommand) {
  7178. case CMD_GEN_REQUEST64_CR:
  7179. case CMD_GEN_REQUEST64_CX:
  7180. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  7181. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  7182. FC_RCTL_DD_UNSOL_CMD) ||
  7183. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  7184. MENLO_TRANSPORT_TYPE))
  7185. goto iocb_busy;
  7186. break;
  7187. case CMD_QUE_RING_BUF_CN:
  7188. case CMD_QUE_RING_BUF64_CN:
  7189. /*
  7190. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  7191. * completion, iocb_cmpl MUST be 0.
  7192. */
  7193. if (piocb->iocb_cmpl)
  7194. piocb->iocb_cmpl = NULL;
  7195. /*FALLTHROUGH*/
  7196. case CMD_CREATE_XRI_CR:
  7197. case CMD_CLOSE_XRI_CN:
  7198. case CMD_CLOSE_XRI_CX:
  7199. break;
  7200. default:
  7201. goto iocb_busy;
  7202. }
  7203. /*
  7204. * For FCP commands, we must be in a state where we can process link
  7205. * attention events.
  7206. */
  7207. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  7208. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  7209. goto iocb_busy;
  7210. }
  7211. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  7212. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  7213. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  7214. if (iocb)
  7215. lpfc_sli_update_ring(phba, pring);
  7216. else
  7217. lpfc_sli_update_full_ring(phba, pring);
  7218. if (!piocb)
  7219. return IOCB_SUCCESS;
  7220. goto out_busy;
  7221. iocb_busy:
  7222. pring->stats.iocb_cmd_delay++;
  7223. out_busy:
  7224. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7225. __lpfc_sli_ringtx_put(phba, pring, piocb);
  7226. return IOCB_SUCCESS;
  7227. }
  7228. return IOCB_BUSY;
  7229. }
  7230. /**
  7231. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  7232. * @phba: Pointer to HBA context object.
  7233. * @piocb: Pointer to command iocb.
  7234. * @sglq: Pointer to the scatter gather queue object.
  7235. *
  7236. * This routine converts the bpl or bde that is in the IOCB
  7237. * to a sgl list for the sli4 hardware. The physical address
  7238. * of the bpl/bde is converted back to a virtual address.
  7239. * If the IOCB contains a BPL then the list of BDE's is
  7240. * converted to sli4_sge's. If the IOCB contains a single
  7241. * BDE then it is converted to a single sli_sge.
  7242. * The IOCB is still in cpu endianess so the contents of
  7243. * the bpl can be used without byte swapping.
  7244. *
  7245. * Returns valid XRI = Success, NO_XRI = Failure.
  7246. **/
  7247. static uint16_t
  7248. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  7249. struct lpfc_sglq *sglq)
  7250. {
  7251. uint16_t xritag = NO_XRI;
  7252. struct ulp_bde64 *bpl = NULL;
  7253. struct ulp_bde64 bde;
  7254. struct sli4_sge *sgl = NULL;
  7255. struct lpfc_dmabuf *dmabuf;
  7256. IOCB_t *icmd;
  7257. int numBdes = 0;
  7258. int i = 0;
  7259. uint32_t offset = 0; /* accumulated offset in the sg request list */
  7260. int inbound = 0; /* number of sg reply entries inbound from firmware */
  7261. if (!piocbq || !sglq)
  7262. return xritag;
  7263. sgl = (struct sli4_sge *)sglq->sgl;
  7264. icmd = &piocbq->iocb;
  7265. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  7266. return sglq->sli4_xritag;
  7267. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7268. numBdes = icmd->un.genreq64.bdl.bdeSize /
  7269. sizeof(struct ulp_bde64);
  7270. /* The addrHigh and addrLow fields within the IOCB
  7271. * have not been byteswapped yet so there is no
  7272. * need to swap them back.
  7273. */
  7274. if (piocbq->context3)
  7275. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  7276. else
  7277. return xritag;
  7278. bpl = (struct ulp_bde64 *)dmabuf->virt;
  7279. if (!bpl)
  7280. return xritag;
  7281. for (i = 0; i < numBdes; i++) {
  7282. /* Should already be byte swapped. */
  7283. sgl->addr_hi = bpl->addrHigh;
  7284. sgl->addr_lo = bpl->addrLow;
  7285. sgl->word2 = le32_to_cpu(sgl->word2);
  7286. if ((i+1) == numBdes)
  7287. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7288. else
  7289. bf_set(lpfc_sli4_sge_last, sgl, 0);
  7290. /* swap the size field back to the cpu so we
  7291. * can assign it to the sgl.
  7292. */
  7293. bde.tus.w = le32_to_cpu(bpl->tus.w);
  7294. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  7295. /* The offsets in the sgl need to be accumulated
  7296. * separately for the request and reply lists.
  7297. * The request is always first, the reply follows.
  7298. */
  7299. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  7300. /* add up the reply sg entries */
  7301. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  7302. inbound++;
  7303. /* first inbound? reset the offset */
  7304. if (inbound == 1)
  7305. offset = 0;
  7306. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  7307. bf_set(lpfc_sli4_sge_type, sgl,
  7308. LPFC_SGE_TYPE_DATA);
  7309. offset += bde.tus.f.bdeSize;
  7310. }
  7311. sgl->word2 = cpu_to_le32(sgl->word2);
  7312. bpl++;
  7313. sgl++;
  7314. }
  7315. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  7316. /* The addrHigh and addrLow fields of the BDE have not
  7317. * been byteswapped yet so they need to be swapped
  7318. * before putting them in the sgl.
  7319. */
  7320. sgl->addr_hi =
  7321. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  7322. sgl->addr_lo =
  7323. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  7324. sgl->word2 = le32_to_cpu(sgl->word2);
  7325. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7326. sgl->word2 = cpu_to_le32(sgl->word2);
  7327. sgl->sge_len =
  7328. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  7329. }
  7330. return sglq->sli4_xritag;
  7331. }
  7332. /**
  7333. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  7334. * @phba: Pointer to HBA context object.
  7335. *
  7336. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  7337. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  7338. * held.
  7339. *
  7340. * Return: index into SLI4 fast-path FCP queue index.
  7341. **/
  7342. static inline int
  7343. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  7344. {
  7345. struct lpfc_vector_map_info *cpup;
  7346. int chann, cpu;
  7347. if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU
  7348. && phba->cfg_fcp_io_channel > 1) {
  7349. cpu = smp_processor_id();
  7350. if (cpu < phba->sli4_hba.num_present_cpu) {
  7351. cpup = phba->sli4_hba.cpu_map;
  7352. cpup += cpu;
  7353. return cpup->channel_id;
  7354. }
  7355. }
  7356. chann = atomic_add_return(1, &phba->fcp_qidx);
  7357. chann = (chann % phba->cfg_fcp_io_channel);
  7358. return chann;
  7359. }
  7360. /**
  7361. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  7362. * @phba: Pointer to HBA context object.
  7363. * @piocb: Pointer to command iocb.
  7364. * @wqe: Pointer to the work queue entry.
  7365. *
  7366. * This routine converts the iocb command to its Work Queue Entry
  7367. * equivalent. The wqe pointer should not have any fields set when
  7368. * this routine is called because it will memcpy over them.
  7369. * This routine does not set the CQ_ID or the WQEC bits in the
  7370. * wqe.
  7371. *
  7372. * Returns: 0 = Success, IOCB_ERROR = Failure.
  7373. **/
  7374. static int
  7375. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  7376. union lpfc_wqe *wqe)
  7377. {
  7378. uint32_t xmit_len = 0, total_len = 0;
  7379. uint8_t ct = 0;
  7380. uint32_t fip;
  7381. uint32_t abort_tag;
  7382. uint8_t command_type = ELS_COMMAND_NON_FIP;
  7383. uint8_t cmnd;
  7384. uint16_t xritag;
  7385. uint16_t abrt_iotag;
  7386. struct lpfc_iocbq *abrtiocbq;
  7387. struct ulp_bde64 *bpl = NULL;
  7388. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  7389. int numBdes, i;
  7390. struct ulp_bde64 bde;
  7391. struct lpfc_nodelist *ndlp;
  7392. uint32_t *pcmd;
  7393. uint32_t if_type;
  7394. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  7395. /* The fcp commands will set command type */
  7396. if (iocbq->iocb_flag & LPFC_IO_FCP)
  7397. command_type = FCP_COMMAND;
  7398. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  7399. command_type = ELS_COMMAND_FIP;
  7400. else
  7401. command_type = ELS_COMMAND_NON_FIP;
  7402. /* Some of the fields are in the right position already */
  7403. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  7404. abort_tag = (uint32_t) iocbq->iotag;
  7405. xritag = iocbq->sli4_xritag;
  7406. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  7407. wqe->generic.wqe_com.word10 = 0;
  7408. /* words0-2 bpl convert bde */
  7409. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7410. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7411. sizeof(struct ulp_bde64);
  7412. bpl = (struct ulp_bde64 *)
  7413. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  7414. if (!bpl)
  7415. return IOCB_ERROR;
  7416. /* Should already be byte swapped. */
  7417. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7418. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7419. /* swap the size field back to the cpu so we
  7420. * can assign it to the sgl.
  7421. */
  7422. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7423. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7424. total_len = 0;
  7425. for (i = 0; i < numBdes; i++) {
  7426. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7427. total_len += bde.tus.f.bdeSize;
  7428. }
  7429. } else
  7430. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7431. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7432. cmnd = iocbq->iocb.ulpCommand;
  7433. switch (iocbq->iocb.ulpCommand) {
  7434. case CMD_ELS_REQUEST64_CR:
  7435. if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
  7436. ndlp = iocbq->context_un.ndlp;
  7437. else
  7438. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7439. if (!iocbq->iocb.ulpLe) {
  7440. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7441. "2007 Only Limited Edition cmd Format"
  7442. " supported 0x%x\n",
  7443. iocbq->iocb.ulpCommand);
  7444. return IOCB_ERROR;
  7445. }
  7446. wqe->els_req.payload_len = xmit_len;
  7447. /* Els_reguest64 has a TMO */
  7448. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7449. iocbq->iocb.ulpTimeout);
  7450. /* Need a VF for word 4 set the vf bit*/
  7451. bf_set(els_req64_vf, &wqe->els_req, 0);
  7452. /* And a VFID for word 12 */
  7453. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7454. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7455. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7456. iocbq->iocb.ulpContext);
  7457. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7458. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7459. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7460. if (command_type == ELS_COMMAND_FIP)
  7461. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7462. >> LPFC_FIP_ELS_ID_SHIFT);
  7463. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7464. iocbq->context2)->virt);
  7465. if_type = bf_get(lpfc_sli_intf_if_type,
  7466. &phba->sli4_hba.sli_intf);
  7467. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7468. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7469. *pcmd == ELS_CMD_SCR ||
  7470. *pcmd == ELS_CMD_FDISC ||
  7471. *pcmd == ELS_CMD_LOGO ||
  7472. *pcmd == ELS_CMD_PLOGI)) {
  7473. bf_set(els_req64_sp, &wqe->els_req, 1);
  7474. bf_set(els_req64_sid, &wqe->els_req,
  7475. iocbq->vport->fc_myDID);
  7476. if ((*pcmd == ELS_CMD_FLOGI) &&
  7477. !(phba->fc_topology ==
  7478. LPFC_TOPOLOGY_LOOP))
  7479. bf_set(els_req64_sid, &wqe->els_req, 0);
  7480. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7481. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7482. phba->vpi_ids[iocbq->vport->vpi]);
  7483. } else if (pcmd && iocbq->context1) {
  7484. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7485. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7486. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7487. }
  7488. }
  7489. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7490. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7491. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7492. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7493. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7494. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7495. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7496. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7497. wqe->els_req.max_response_payload_len = total_len - xmit_len;
  7498. break;
  7499. case CMD_XMIT_SEQUENCE64_CX:
  7500. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7501. iocbq->iocb.un.ulpWord[3]);
  7502. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7503. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7504. /* The entire sequence is transmitted for this IOCB */
  7505. xmit_len = total_len;
  7506. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7507. if (phba->link_flag & LS_LOOPBACK_MODE)
  7508. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7509. case CMD_XMIT_SEQUENCE64_CR:
  7510. /* word3 iocb=io_tag32 wqe=reserved */
  7511. wqe->xmit_sequence.rsvd3 = 0;
  7512. /* word4 relative_offset memcpy */
  7513. /* word5 r_ctl/df_ctl memcpy */
  7514. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7515. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7516. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7517. LPFC_WQE_IOD_WRITE);
  7518. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7519. LPFC_WQE_LENLOC_WORD12);
  7520. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7521. wqe->xmit_sequence.xmit_len = xmit_len;
  7522. command_type = OTHER_COMMAND;
  7523. break;
  7524. case CMD_XMIT_BCAST64_CN:
  7525. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7526. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7527. /* word4 iocb=rsvd wqe=rsvd */
  7528. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7529. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7530. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7531. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7532. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7533. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7534. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7535. LPFC_WQE_LENLOC_WORD3);
  7536. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7537. break;
  7538. case CMD_FCP_IWRITE64_CR:
  7539. command_type = FCP_COMMAND_DATA_OUT;
  7540. /* word3 iocb=iotag wqe=payload_offset_len */
  7541. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7542. bf_set(payload_offset_len, &wqe->fcp_iwrite,
  7543. xmit_len + sizeof(struct fcp_rsp));
  7544. bf_set(cmd_buff_len, &wqe->fcp_iwrite,
  7545. 0);
  7546. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7547. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7548. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7549. iocbq->iocb.ulpFCP2Rcvy);
  7550. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7551. /* Always open the exchange */
  7552. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7553. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7554. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7555. LPFC_WQE_LENLOC_WORD4);
  7556. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7557. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7558. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7559. if (iocbq->iocb_flag & LPFC_IO_OAS) {
  7560. bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
  7561. if (phba->cfg_XLanePriority) {
  7562. bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
  7563. bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
  7564. (phba->cfg_XLanePriority << 1));
  7565. }
  7566. }
  7567. break;
  7568. case CMD_FCP_IREAD64_CR:
  7569. /* word3 iocb=iotag wqe=payload_offset_len */
  7570. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7571. bf_set(payload_offset_len, &wqe->fcp_iread,
  7572. xmit_len + sizeof(struct fcp_rsp));
  7573. bf_set(cmd_buff_len, &wqe->fcp_iread,
  7574. 0);
  7575. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7576. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7577. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7578. iocbq->iocb.ulpFCP2Rcvy);
  7579. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7580. /* Always open the exchange */
  7581. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7582. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7583. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7584. LPFC_WQE_LENLOC_WORD4);
  7585. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7586. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7587. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7588. if (iocbq->iocb_flag & LPFC_IO_OAS) {
  7589. bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
  7590. if (phba->cfg_XLanePriority) {
  7591. bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
  7592. bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
  7593. (phba->cfg_XLanePriority << 1));
  7594. }
  7595. }
  7596. break;
  7597. case CMD_FCP_ICMND64_CR:
  7598. /* word3 iocb=iotag wqe=payload_offset_len */
  7599. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7600. bf_set(payload_offset_len, &wqe->fcp_icmd,
  7601. xmit_len + sizeof(struct fcp_rsp));
  7602. bf_set(cmd_buff_len, &wqe->fcp_icmd,
  7603. 0);
  7604. /* word3 iocb=IO_TAG wqe=reserved */
  7605. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7606. /* Always open the exchange */
  7607. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7608. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7609. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7610. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7611. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7612. LPFC_WQE_LENLOC_NONE);
  7613. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7614. bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
  7615. iocbq->iocb.ulpFCP2Rcvy);
  7616. if (iocbq->iocb_flag & LPFC_IO_OAS) {
  7617. bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
  7618. if (phba->cfg_XLanePriority) {
  7619. bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
  7620. bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
  7621. (phba->cfg_XLanePriority << 1));
  7622. }
  7623. }
  7624. break;
  7625. case CMD_GEN_REQUEST64_CR:
  7626. /* For this command calculate the xmit length of the
  7627. * request bde.
  7628. */
  7629. xmit_len = 0;
  7630. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7631. sizeof(struct ulp_bde64);
  7632. for (i = 0; i < numBdes; i++) {
  7633. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7634. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7635. break;
  7636. xmit_len += bde.tus.f.bdeSize;
  7637. }
  7638. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7639. wqe->gen_req.request_payload_len = xmit_len;
  7640. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7641. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7642. /* word6 context tag copied in memcpy */
  7643. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7644. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7645. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7646. "2015 Invalid CT %x command 0x%x\n",
  7647. ct, iocbq->iocb.ulpCommand);
  7648. return IOCB_ERROR;
  7649. }
  7650. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7651. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7652. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7653. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7654. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7655. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7656. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7657. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7658. wqe->gen_req.max_response_payload_len = total_len - xmit_len;
  7659. command_type = OTHER_COMMAND;
  7660. break;
  7661. case CMD_XMIT_ELS_RSP64_CX:
  7662. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7663. /* words0-2 BDE memcpy */
  7664. /* word3 iocb=iotag32 wqe=response_payload_len */
  7665. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7666. /* word4 */
  7667. wqe->xmit_els_rsp.word4 = 0;
  7668. /* word5 iocb=rsvd wge=did */
  7669. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7670. iocbq->iocb.un.xseq64.xmit_els_remoteID);
  7671. if_type = bf_get(lpfc_sli_intf_if_type,
  7672. &phba->sli4_hba.sli_intf);
  7673. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7674. if (iocbq->vport->fc_flag & FC_PT2PT) {
  7675. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7676. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7677. iocbq->vport->fc_myDID);
  7678. if (iocbq->vport->fc_myDID == Fabric_DID) {
  7679. bf_set(wqe_els_did,
  7680. &wqe->xmit_els_rsp.wqe_dest, 0);
  7681. }
  7682. }
  7683. }
  7684. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7685. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7686. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7687. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7688. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7689. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7690. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7691. phba->vpi_ids[iocbq->vport->vpi]);
  7692. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7693. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7694. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7695. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7696. LPFC_WQE_LENLOC_WORD3);
  7697. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7698. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7699. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7700. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7701. iocbq->context2)->virt);
  7702. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7703. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7704. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7705. iocbq->vport->fc_myDID);
  7706. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
  7707. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7708. phba->vpi_ids[phba->pport->vpi]);
  7709. }
  7710. command_type = OTHER_COMMAND;
  7711. break;
  7712. case CMD_CLOSE_XRI_CN:
  7713. case CMD_ABORT_XRI_CN:
  7714. case CMD_ABORT_XRI_CX:
  7715. /* words 0-2 memcpy should be 0 rserved */
  7716. /* port will send abts */
  7717. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7718. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7719. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7720. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7721. } else
  7722. fip = 0;
  7723. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7724. /*
  7725. * The link is down, or the command was ELS_FIP
  7726. * so the fw does not need to send abts
  7727. * on the wire.
  7728. */
  7729. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7730. else
  7731. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7732. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7733. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7734. wqe->abort_cmd.rsrvd5 = 0;
  7735. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7736. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7737. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7738. /*
  7739. * The abort handler will send us CMD_ABORT_XRI_CN or
  7740. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7741. */
  7742. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7743. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7744. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7745. LPFC_WQE_LENLOC_NONE);
  7746. cmnd = CMD_ABORT_XRI_CX;
  7747. command_type = OTHER_COMMAND;
  7748. xritag = 0;
  7749. break;
  7750. case CMD_XMIT_BLS_RSP64_CX:
  7751. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7752. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7753. * we re-construct this WQE here based on information in
  7754. * iocbq from scratch.
  7755. */
  7756. memset(wqe, 0, sizeof(union lpfc_wqe));
  7757. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7758. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7759. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7760. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7761. LPFC_ABTS_UNSOL_INT) {
  7762. /* ABTS sent by initiator to CT exchange, the
  7763. * RX_ID field will be filled with the newly
  7764. * allocated responder XRI.
  7765. */
  7766. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7767. iocbq->sli4_xritag);
  7768. } else {
  7769. /* ABTS sent by responder to CT exchange, the
  7770. * RX_ID field will be filled with the responder
  7771. * RX_ID from ABTS.
  7772. */
  7773. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7774. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7775. }
  7776. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7777. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7778. /* Use CT=VPI */
  7779. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7780. ndlp->nlp_DID);
  7781. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7782. iocbq->iocb.ulpContext);
  7783. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7784. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7785. phba->vpi_ids[phba->pport->vpi]);
  7786. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7787. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7788. LPFC_WQE_LENLOC_NONE);
  7789. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7790. command_type = OTHER_COMMAND;
  7791. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7792. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7793. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7794. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7795. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7796. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7797. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7798. }
  7799. break;
  7800. case CMD_XRI_ABORTED_CX:
  7801. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7802. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7803. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7804. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7805. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7806. default:
  7807. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7808. "2014 Invalid command 0x%x\n",
  7809. iocbq->iocb.ulpCommand);
  7810. return IOCB_ERROR;
  7811. break;
  7812. }
  7813. if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
  7814. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
  7815. else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
  7816. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
  7817. else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
  7818. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
  7819. iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
  7820. LPFC_IO_DIF_INSERT);
  7821. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7822. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7823. wqe->generic.wqe_com.abort_tag = abort_tag;
  7824. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7825. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7826. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7827. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7828. return 0;
  7829. }
  7830. /**
  7831. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7832. * @phba: Pointer to HBA context object.
  7833. * @ring_number: SLI ring number to issue iocb on.
  7834. * @piocb: Pointer to command iocb.
  7835. * @flag: Flag indicating if this command can be put into txq.
  7836. *
  7837. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7838. * an iocb command to an HBA with SLI-4 interface spec.
  7839. *
  7840. * This function is called with hbalock held. The function will return success
  7841. * after it successfully submit the iocb to firmware or after adding to the
  7842. * txq.
  7843. **/
  7844. static int
  7845. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7846. struct lpfc_iocbq *piocb, uint32_t flag)
  7847. {
  7848. struct lpfc_sglq *sglq;
  7849. union lpfc_wqe wqe;
  7850. struct lpfc_queue *wq;
  7851. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7852. if (piocb->sli4_xritag == NO_XRI) {
  7853. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7854. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7855. sglq = NULL;
  7856. else {
  7857. if (!list_empty(&pring->txq)) {
  7858. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7859. __lpfc_sli_ringtx_put(phba,
  7860. pring, piocb);
  7861. return IOCB_SUCCESS;
  7862. } else {
  7863. return IOCB_BUSY;
  7864. }
  7865. } else {
  7866. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7867. if (!sglq) {
  7868. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7869. __lpfc_sli_ringtx_put(phba,
  7870. pring,
  7871. piocb);
  7872. return IOCB_SUCCESS;
  7873. } else
  7874. return IOCB_BUSY;
  7875. }
  7876. }
  7877. }
  7878. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7879. /* These IO's already have an XRI and a mapped sgl. */
  7880. sglq = NULL;
  7881. } else {
  7882. /*
  7883. * This is a continuation of a commandi,(CX) so this
  7884. * sglq is on the active list
  7885. */
  7886. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
  7887. if (!sglq)
  7888. return IOCB_ERROR;
  7889. }
  7890. if (sglq) {
  7891. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7892. piocb->sli4_xritag = sglq->sli4_xritag;
  7893. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7894. return IOCB_ERROR;
  7895. }
  7896. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7897. return IOCB_ERROR;
  7898. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7899. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7900. if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
  7901. wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
  7902. } else {
  7903. wq = phba->sli4_hba.oas_wq;
  7904. }
  7905. if (lpfc_sli4_wq_put(wq, &wqe))
  7906. return IOCB_ERROR;
  7907. } else {
  7908. if (unlikely(!phba->sli4_hba.els_wq))
  7909. return IOCB_ERROR;
  7910. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7911. return IOCB_ERROR;
  7912. }
  7913. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7914. return 0;
  7915. }
  7916. /**
  7917. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7918. *
  7919. * This routine wraps the actual lockless version for issusing IOCB function
  7920. * pointer from the lpfc_hba struct.
  7921. *
  7922. * Return codes:
  7923. * IOCB_ERROR - Error
  7924. * IOCB_SUCCESS - Success
  7925. * IOCB_BUSY - Busy
  7926. **/
  7927. int
  7928. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7929. struct lpfc_iocbq *piocb, uint32_t flag)
  7930. {
  7931. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7932. }
  7933. /**
  7934. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7935. * @phba: The hba struct for which this call is being executed.
  7936. * @dev_grp: The HBA PCI-Device group number.
  7937. *
  7938. * This routine sets up the SLI interface API function jump table in @phba
  7939. * struct.
  7940. * Returns: 0 - success, -ENODEV - failure.
  7941. **/
  7942. int
  7943. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7944. {
  7945. switch (dev_grp) {
  7946. case LPFC_PCI_DEV_LP:
  7947. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7948. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7949. break;
  7950. case LPFC_PCI_DEV_OC:
  7951. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7952. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7953. break;
  7954. default:
  7955. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7956. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7957. dev_grp);
  7958. return -ENODEV;
  7959. break;
  7960. }
  7961. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7962. return 0;
  7963. }
  7964. int
  7965. lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
  7966. struct lpfc_iocbq *piocb)
  7967. {
  7968. uint32_t idx;
  7969. if (phba->sli_rev == LPFC_SLI_REV4) {
  7970. if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
  7971. /*
  7972. * fcp_wqidx should already be setup based on what
  7973. * completion queue we want to use.
  7974. */
  7975. if (!(phba->cfg_fof) ||
  7976. (!(piocb->iocb_flag & LPFC_IO_FOF))) {
  7977. if (unlikely(!phba->sli4_hba.fcp_wq))
  7978. return LPFC_HBA_ERROR;
  7979. idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7980. piocb->fcp_wqidx = idx;
  7981. ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
  7982. } else {
  7983. if (unlikely(!phba->sli4_hba.oas_wq))
  7984. return LPFC_HBA_ERROR;
  7985. idx = 0;
  7986. piocb->fcp_wqidx = idx;
  7987. ring_number = LPFC_FCP_OAS_RING;
  7988. }
  7989. }
  7990. }
  7991. return ring_number;
  7992. }
  7993. /**
  7994. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7995. * @phba: Pointer to HBA context object.
  7996. * @pring: Pointer to driver SLI ring object.
  7997. * @piocb: Pointer to command iocb.
  7998. * @flag: Flag indicating if this command can be put into txq.
  7999. *
  8000. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  8001. * function. This function gets the hbalock and calls
  8002. * __lpfc_sli_issue_iocb function and will return the error returned
  8003. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  8004. * functions which do not hold hbalock.
  8005. **/
  8006. int
  8007. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  8008. struct lpfc_iocbq *piocb, uint32_t flag)
  8009. {
  8010. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  8011. struct lpfc_sli_ring *pring;
  8012. struct lpfc_queue *fpeq;
  8013. struct lpfc_eqe *eqe;
  8014. unsigned long iflags;
  8015. int rc, idx;
  8016. if (phba->sli_rev == LPFC_SLI_REV4) {
  8017. ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
  8018. if (unlikely(ring_number == LPFC_HBA_ERROR))
  8019. return IOCB_ERROR;
  8020. idx = piocb->fcp_wqidx;
  8021. pring = &phba->sli.ring[ring_number];
  8022. spin_lock_irqsave(&pring->ring_lock, iflags);
  8023. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  8024. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  8025. if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
  8026. fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
  8027. if (atomic_dec_and_test(&fcp_eq_hdl->
  8028. fcp_eq_in_use)) {
  8029. /* Get associated EQ with this index */
  8030. fpeq = phba->sli4_hba.hba_eq[idx];
  8031. /* Turn off interrupts from this EQ */
  8032. lpfc_sli4_eq_clr_intr(fpeq);
  8033. /*
  8034. * Process all the events on FCP EQ
  8035. */
  8036. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  8037. lpfc_sli4_hba_handle_eqe(phba,
  8038. eqe, idx);
  8039. fpeq->EQ_processed++;
  8040. }
  8041. /* Always clear and re-arm the EQ */
  8042. lpfc_sli4_eq_release(fpeq,
  8043. LPFC_QUEUE_REARM);
  8044. }
  8045. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  8046. }
  8047. } else {
  8048. /* For now, SLI2/3 will still use hbalock */
  8049. spin_lock_irqsave(&phba->hbalock, iflags);
  8050. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  8051. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8052. }
  8053. return rc;
  8054. }
  8055. /**
  8056. * lpfc_extra_ring_setup - Extra ring setup function
  8057. * @phba: Pointer to HBA context object.
  8058. *
  8059. * This function is called while driver attaches with the
  8060. * HBA to setup the extra ring. The extra ring is used
  8061. * only when driver needs to support target mode functionality
  8062. * or IP over FC functionalities.
  8063. *
  8064. * This function is called with no lock held.
  8065. **/
  8066. static int
  8067. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  8068. {
  8069. struct lpfc_sli *psli;
  8070. struct lpfc_sli_ring *pring;
  8071. psli = &phba->sli;
  8072. /* Adjust cmd/rsp ring iocb entries more evenly */
  8073. /* Take some away from the FCP ring */
  8074. pring = &psli->ring[psli->fcp_ring];
  8075. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  8076. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  8077. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  8078. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  8079. /* and give them to the extra ring */
  8080. pring = &psli->ring[psli->extra_ring];
  8081. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  8082. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  8083. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  8084. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  8085. /* Setup default profile for this ring */
  8086. pring->iotag_max = 4096;
  8087. pring->num_mask = 1;
  8088. pring->prt[0].profile = 0; /* Mask 0 */
  8089. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  8090. pring->prt[0].type = phba->cfg_multi_ring_type;
  8091. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  8092. return 0;
  8093. }
  8094. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  8095. * @phba: Pointer to HBA context object.
  8096. * @iocbq: Pointer to iocb object.
  8097. *
  8098. * The async_event handler calls this routine when it receives
  8099. * an ASYNC_STATUS_CN event from the port. The port generates
  8100. * this event when an Abort Sequence request to an rport fails
  8101. * twice in succession. The abort could be originated by the
  8102. * driver or by the port. The ABTS could have been for an ELS
  8103. * or FCP IO. The port only generates this event when an ABTS
  8104. * fails to complete after one retry.
  8105. */
  8106. static void
  8107. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  8108. struct lpfc_iocbq *iocbq)
  8109. {
  8110. struct lpfc_nodelist *ndlp = NULL;
  8111. uint16_t rpi = 0, vpi = 0;
  8112. struct lpfc_vport *vport = NULL;
  8113. /* The rpi in the ulpContext is vport-sensitive. */
  8114. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  8115. rpi = iocbq->iocb.ulpContext;
  8116. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8117. "3092 Port generated ABTS async event "
  8118. "on vpi %d rpi %d status 0x%x\n",
  8119. vpi, rpi, iocbq->iocb.ulpStatus);
  8120. vport = lpfc_find_vport_by_vpid(phba, vpi);
  8121. if (!vport)
  8122. goto err_exit;
  8123. ndlp = lpfc_findnode_rpi(vport, rpi);
  8124. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  8125. goto err_exit;
  8126. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  8127. lpfc_sli_abts_recover_port(vport, ndlp);
  8128. return;
  8129. err_exit:
  8130. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8131. "3095 Event Context not found, no "
  8132. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  8133. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  8134. vpi, rpi);
  8135. }
  8136. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  8137. * @phba: pointer to HBA context object.
  8138. * @ndlp: nodelist pointer for the impacted rport.
  8139. * @axri: pointer to the wcqe containing the failed exchange.
  8140. *
  8141. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  8142. * port. The port generates this event when an abort exchange request to an
  8143. * rport fails twice in succession with no reply. The abort could be originated
  8144. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  8145. */
  8146. void
  8147. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  8148. struct lpfc_nodelist *ndlp,
  8149. struct sli4_wcqe_xri_aborted *axri)
  8150. {
  8151. struct lpfc_vport *vport;
  8152. uint32_t ext_status = 0;
  8153. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  8154. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8155. "3115 Node Context not found, driver "
  8156. "ignoring abts err event\n");
  8157. return;
  8158. }
  8159. vport = ndlp->vport;
  8160. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8161. "3116 Port generated FCP XRI ABORT event on "
  8162. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  8163. ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
  8164. bf_get(lpfc_wcqe_xa_xri, axri),
  8165. bf_get(lpfc_wcqe_xa_status, axri),
  8166. axri->parameter);
  8167. /*
  8168. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  8169. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  8170. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  8171. */
  8172. ext_status = axri->parameter & IOERR_PARAM_MASK;
  8173. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  8174. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  8175. lpfc_sli_abts_recover_port(vport, ndlp);
  8176. }
  8177. /**
  8178. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  8179. * @phba: Pointer to HBA context object.
  8180. * @pring: Pointer to driver SLI ring object.
  8181. * @iocbq: Pointer to iocb object.
  8182. *
  8183. * This function is called by the slow ring event handler
  8184. * function when there is an ASYNC event iocb in the ring.
  8185. * This function is called with no lock held.
  8186. * Currently this function handles only temperature related
  8187. * ASYNC events. The function decodes the temperature sensor
  8188. * event message and posts events for the management applications.
  8189. **/
  8190. static void
  8191. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  8192. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  8193. {
  8194. IOCB_t *icmd;
  8195. uint16_t evt_code;
  8196. struct temp_event temp_event_data;
  8197. struct Scsi_Host *shost;
  8198. uint32_t *iocb_w;
  8199. icmd = &iocbq->iocb;
  8200. evt_code = icmd->un.asyncstat.evt_code;
  8201. switch (evt_code) {
  8202. case ASYNC_TEMP_WARN:
  8203. case ASYNC_TEMP_SAFE:
  8204. temp_event_data.data = (uint32_t) icmd->ulpContext;
  8205. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  8206. if (evt_code == ASYNC_TEMP_WARN) {
  8207. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  8208. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  8209. "0347 Adapter is very hot, please take "
  8210. "corrective action. temperature : %d Celsius\n",
  8211. (uint32_t) icmd->ulpContext);
  8212. } else {
  8213. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  8214. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  8215. "0340 Adapter temperature is OK now. "
  8216. "temperature : %d Celsius\n",
  8217. (uint32_t) icmd->ulpContext);
  8218. }
  8219. /* Send temperature change event to applications */
  8220. shost = lpfc_shost_from_vport(phba->pport);
  8221. fc_host_post_vendor_event(shost, fc_get_event_number(),
  8222. sizeof(temp_event_data), (char *) &temp_event_data,
  8223. LPFC_NL_VENDOR_ID);
  8224. break;
  8225. case ASYNC_STATUS_CN:
  8226. lpfc_sli_abts_err_handler(phba, iocbq);
  8227. break;
  8228. default:
  8229. iocb_w = (uint32_t *) icmd;
  8230. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8231. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  8232. " evt_code 0x%x\n"
  8233. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  8234. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  8235. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  8236. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  8237. pring->ringno, icmd->un.asyncstat.evt_code,
  8238. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  8239. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  8240. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  8241. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  8242. break;
  8243. }
  8244. }
  8245. /**
  8246. * lpfc_sli_setup - SLI ring setup function
  8247. * @phba: Pointer to HBA context object.
  8248. *
  8249. * lpfc_sli_setup sets up rings of the SLI interface with
  8250. * number of iocbs per ring and iotags. This function is
  8251. * called while driver attach to the HBA and before the
  8252. * interrupts are enabled. So there is no need for locking.
  8253. *
  8254. * This function always returns 0.
  8255. **/
  8256. int
  8257. lpfc_sli_setup(struct lpfc_hba *phba)
  8258. {
  8259. int i, totiocbsize = 0;
  8260. struct lpfc_sli *psli = &phba->sli;
  8261. struct lpfc_sli_ring *pring;
  8262. psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
  8263. if (phba->sli_rev == LPFC_SLI_REV4)
  8264. psli->num_rings += phba->cfg_fcp_io_channel;
  8265. psli->sli_flag = 0;
  8266. psli->fcp_ring = LPFC_FCP_RING;
  8267. psli->next_ring = LPFC_FCP_NEXT_RING;
  8268. psli->extra_ring = LPFC_EXTRA_RING;
  8269. psli->iocbq_lookup = NULL;
  8270. psli->iocbq_lookup_len = 0;
  8271. psli->last_iotag = 0;
  8272. for (i = 0; i < psli->num_rings; i++) {
  8273. pring = &psli->ring[i];
  8274. switch (i) {
  8275. case LPFC_FCP_RING: /* ring 0 - FCP */
  8276. /* numCiocb and numRiocb are used in config_port */
  8277. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  8278. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  8279. pring->sli.sli3.numCiocb +=
  8280. SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  8281. pring->sli.sli3.numRiocb +=
  8282. SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  8283. pring->sli.sli3.numCiocb +=
  8284. SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  8285. pring->sli.sli3.numRiocb +=
  8286. SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  8287. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8288. SLI3_IOCB_CMD_SIZE :
  8289. SLI2_IOCB_CMD_SIZE;
  8290. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8291. SLI3_IOCB_RSP_SIZE :
  8292. SLI2_IOCB_RSP_SIZE;
  8293. pring->iotag_ctr = 0;
  8294. pring->iotag_max =
  8295. (phba->cfg_hba_queue_depth * 2);
  8296. pring->fast_iotag = pring->iotag_max;
  8297. pring->num_mask = 0;
  8298. break;
  8299. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  8300. /* numCiocb and numRiocb are used in config_port */
  8301. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  8302. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  8303. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8304. SLI3_IOCB_CMD_SIZE :
  8305. SLI2_IOCB_CMD_SIZE;
  8306. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8307. SLI3_IOCB_RSP_SIZE :
  8308. SLI2_IOCB_RSP_SIZE;
  8309. pring->iotag_max = phba->cfg_hba_queue_depth;
  8310. pring->num_mask = 0;
  8311. break;
  8312. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  8313. /* numCiocb and numRiocb are used in config_port */
  8314. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  8315. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  8316. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8317. SLI3_IOCB_CMD_SIZE :
  8318. SLI2_IOCB_CMD_SIZE;
  8319. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8320. SLI3_IOCB_RSP_SIZE :
  8321. SLI2_IOCB_RSP_SIZE;
  8322. pring->fast_iotag = 0;
  8323. pring->iotag_ctr = 0;
  8324. pring->iotag_max = 4096;
  8325. pring->lpfc_sli_rcv_async_status =
  8326. lpfc_sli_async_event_handler;
  8327. pring->num_mask = LPFC_MAX_RING_MASK;
  8328. pring->prt[0].profile = 0; /* Mask 0 */
  8329. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  8330. pring->prt[0].type = FC_TYPE_ELS;
  8331. pring->prt[0].lpfc_sli_rcv_unsol_event =
  8332. lpfc_els_unsol_event;
  8333. pring->prt[1].profile = 0; /* Mask 1 */
  8334. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  8335. pring->prt[1].type = FC_TYPE_ELS;
  8336. pring->prt[1].lpfc_sli_rcv_unsol_event =
  8337. lpfc_els_unsol_event;
  8338. pring->prt[2].profile = 0; /* Mask 2 */
  8339. /* NameServer Inquiry */
  8340. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  8341. /* NameServer */
  8342. pring->prt[2].type = FC_TYPE_CT;
  8343. pring->prt[2].lpfc_sli_rcv_unsol_event =
  8344. lpfc_ct_unsol_event;
  8345. pring->prt[3].profile = 0; /* Mask 3 */
  8346. /* NameServer response */
  8347. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  8348. /* NameServer */
  8349. pring->prt[3].type = FC_TYPE_CT;
  8350. pring->prt[3].lpfc_sli_rcv_unsol_event =
  8351. lpfc_ct_unsol_event;
  8352. break;
  8353. }
  8354. totiocbsize += (pring->sli.sli3.numCiocb *
  8355. pring->sli.sli3.sizeCiocb) +
  8356. (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
  8357. }
  8358. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  8359. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  8360. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  8361. "SLI2 SLIM Data: x%x x%lx\n",
  8362. phba->brd_no, totiocbsize,
  8363. (unsigned long) MAX_SLIM_IOCB_SIZE);
  8364. }
  8365. if (phba->cfg_multi_ring_support == 2)
  8366. lpfc_extra_ring_setup(phba);
  8367. return 0;
  8368. }
  8369. /**
  8370. * lpfc_sli_queue_setup - Queue initialization function
  8371. * @phba: Pointer to HBA context object.
  8372. *
  8373. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  8374. * ring. This function also initializes ring indices of each ring.
  8375. * This function is called during the initialization of the SLI
  8376. * interface of an HBA.
  8377. * This function is called with no lock held and always returns
  8378. * 1.
  8379. **/
  8380. int
  8381. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  8382. {
  8383. struct lpfc_sli *psli;
  8384. struct lpfc_sli_ring *pring;
  8385. int i;
  8386. psli = &phba->sli;
  8387. spin_lock_irq(&phba->hbalock);
  8388. INIT_LIST_HEAD(&psli->mboxq);
  8389. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  8390. /* Initialize list headers for txq and txcmplq as double linked lists */
  8391. for (i = 0; i < psli->num_rings; i++) {
  8392. pring = &psli->ring[i];
  8393. pring->ringno = i;
  8394. pring->sli.sli3.next_cmdidx = 0;
  8395. pring->sli.sli3.local_getidx = 0;
  8396. pring->sli.sli3.cmdidx = 0;
  8397. pring->flag = 0;
  8398. INIT_LIST_HEAD(&pring->txq);
  8399. INIT_LIST_HEAD(&pring->txcmplq);
  8400. INIT_LIST_HEAD(&pring->iocb_continueq);
  8401. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  8402. INIT_LIST_HEAD(&pring->postbufq);
  8403. spin_lock_init(&pring->ring_lock);
  8404. }
  8405. spin_unlock_irq(&phba->hbalock);
  8406. return 1;
  8407. }
  8408. /**
  8409. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  8410. * @phba: Pointer to HBA context object.
  8411. *
  8412. * This routine flushes the mailbox command subsystem. It will unconditionally
  8413. * flush all the mailbox commands in the three possible stages in the mailbox
  8414. * command sub-system: pending mailbox command queue; the outstanding mailbox
  8415. * command; and completed mailbox command queue. It is caller's responsibility
  8416. * to make sure that the driver is in the proper state to flush the mailbox
  8417. * command sub-system. Namely, the posting of mailbox commands into the
  8418. * pending mailbox command queue from the various clients must be stopped;
  8419. * either the HBA is in a state that it will never works on the outstanding
  8420. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  8421. * mailbox command has been completed.
  8422. **/
  8423. static void
  8424. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  8425. {
  8426. LIST_HEAD(completions);
  8427. struct lpfc_sli *psli = &phba->sli;
  8428. LPFC_MBOXQ_t *pmb;
  8429. unsigned long iflag;
  8430. /* Flush all the mailbox commands in the mbox system */
  8431. spin_lock_irqsave(&phba->hbalock, iflag);
  8432. /* The pending mailbox command queue */
  8433. list_splice_init(&phba->sli.mboxq, &completions);
  8434. /* The outstanding active mailbox command */
  8435. if (psli->mbox_active) {
  8436. list_add_tail(&psli->mbox_active->list, &completions);
  8437. psli->mbox_active = NULL;
  8438. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  8439. }
  8440. /* The completed mailbox command queue */
  8441. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  8442. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8443. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  8444. while (!list_empty(&completions)) {
  8445. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  8446. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  8447. if (pmb->mbox_cmpl)
  8448. pmb->mbox_cmpl(phba, pmb);
  8449. }
  8450. }
  8451. /**
  8452. * lpfc_sli_host_down - Vport cleanup function
  8453. * @vport: Pointer to virtual port object.
  8454. *
  8455. * lpfc_sli_host_down is called to clean up the resources
  8456. * associated with a vport before destroying virtual
  8457. * port data structures.
  8458. * This function does following operations:
  8459. * - Free discovery resources associated with this virtual
  8460. * port.
  8461. * - Free iocbs associated with this virtual port in
  8462. * the txq.
  8463. * - Send abort for all iocb commands associated with this
  8464. * vport in txcmplq.
  8465. *
  8466. * This function is called with no lock held and always returns 1.
  8467. **/
  8468. int
  8469. lpfc_sli_host_down(struct lpfc_vport *vport)
  8470. {
  8471. LIST_HEAD(completions);
  8472. struct lpfc_hba *phba = vport->phba;
  8473. struct lpfc_sli *psli = &phba->sli;
  8474. struct lpfc_sli_ring *pring;
  8475. struct lpfc_iocbq *iocb, *next_iocb;
  8476. int i;
  8477. unsigned long flags = 0;
  8478. uint16_t prev_pring_flag;
  8479. lpfc_cleanup_discovery_resources(vport);
  8480. spin_lock_irqsave(&phba->hbalock, flags);
  8481. for (i = 0; i < psli->num_rings; i++) {
  8482. pring = &psli->ring[i];
  8483. prev_pring_flag = pring->flag;
  8484. /* Only slow rings */
  8485. if (pring->ringno == LPFC_ELS_RING) {
  8486. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8487. /* Set the lpfc data pending flag */
  8488. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8489. }
  8490. /*
  8491. * Error everything on the txq since these iocbs have not been
  8492. * given to the FW yet.
  8493. */
  8494. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  8495. if (iocb->vport != vport)
  8496. continue;
  8497. list_move_tail(&iocb->list, &completions);
  8498. }
  8499. /* Next issue ABTS for everything on the txcmplq */
  8500. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8501. list) {
  8502. if (iocb->vport != vport)
  8503. continue;
  8504. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8505. }
  8506. pring->flag = prev_pring_flag;
  8507. }
  8508. spin_unlock_irqrestore(&phba->hbalock, flags);
  8509. /* Cancel all the IOCBs from the completions list */
  8510. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8511. IOERR_SLI_DOWN);
  8512. return 1;
  8513. }
  8514. /**
  8515. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8516. * @phba: Pointer to HBA context object.
  8517. *
  8518. * This function cleans up all iocb, buffers, mailbox commands
  8519. * while shutting down the HBA. This function is called with no
  8520. * lock held and always returns 1.
  8521. * This function does the following to cleanup driver resources:
  8522. * - Free discovery resources for each virtual port
  8523. * - Cleanup any pending fabric iocbs
  8524. * - Iterate through the iocb txq and free each entry
  8525. * in the list.
  8526. * - Free up any buffer posted to the HBA
  8527. * - Free mailbox commands in the mailbox queue.
  8528. **/
  8529. int
  8530. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8531. {
  8532. LIST_HEAD(completions);
  8533. struct lpfc_sli *psli = &phba->sli;
  8534. struct lpfc_sli_ring *pring;
  8535. struct lpfc_dmabuf *buf_ptr;
  8536. unsigned long flags = 0;
  8537. int i;
  8538. /* Shutdown the mailbox command sub-system */
  8539. lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
  8540. lpfc_hba_down_prep(phba);
  8541. lpfc_fabric_abort_hba(phba);
  8542. spin_lock_irqsave(&phba->hbalock, flags);
  8543. for (i = 0; i < psli->num_rings; i++) {
  8544. pring = &psli->ring[i];
  8545. /* Only slow rings */
  8546. if (pring->ringno == LPFC_ELS_RING) {
  8547. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8548. /* Set the lpfc data pending flag */
  8549. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8550. }
  8551. /*
  8552. * Error everything on the txq since these iocbs have not been
  8553. * given to the FW yet.
  8554. */
  8555. list_splice_init(&pring->txq, &completions);
  8556. }
  8557. spin_unlock_irqrestore(&phba->hbalock, flags);
  8558. /* Cancel all the IOCBs from the completions list */
  8559. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8560. IOERR_SLI_DOWN);
  8561. spin_lock_irqsave(&phba->hbalock, flags);
  8562. list_splice_init(&phba->elsbuf, &completions);
  8563. phba->elsbuf_cnt = 0;
  8564. phba->elsbuf_prev_cnt = 0;
  8565. spin_unlock_irqrestore(&phba->hbalock, flags);
  8566. while (!list_empty(&completions)) {
  8567. list_remove_head(&completions, buf_ptr,
  8568. struct lpfc_dmabuf, list);
  8569. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8570. kfree(buf_ptr);
  8571. }
  8572. /* Return any active mbox cmds */
  8573. del_timer_sync(&psli->mbox_tmo);
  8574. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8575. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8576. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8577. return 1;
  8578. }
  8579. /**
  8580. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8581. * @srcp: Source memory pointer.
  8582. * @destp: Destination memory pointer.
  8583. * @cnt: Number of words required to be copied.
  8584. *
  8585. * This function is used for copying data between driver memory
  8586. * and the SLI memory. This function also changes the endianness
  8587. * of each word if native endianness is different from SLI
  8588. * endianness. This function can be called with or without
  8589. * lock.
  8590. **/
  8591. void
  8592. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8593. {
  8594. uint32_t *src = srcp;
  8595. uint32_t *dest = destp;
  8596. uint32_t ldata;
  8597. int i;
  8598. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8599. ldata = *src;
  8600. ldata = le32_to_cpu(ldata);
  8601. *dest = ldata;
  8602. src++;
  8603. dest++;
  8604. }
  8605. }
  8606. /**
  8607. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8608. * @srcp: Source memory pointer.
  8609. * @destp: Destination memory pointer.
  8610. * @cnt: Number of words required to be copied.
  8611. *
  8612. * This function is used for copying data between a data structure
  8613. * with big endian representation to local endianness.
  8614. * This function can be called with or without lock.
  8615. **/
  8616. void
  8617. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8618. {
  8619. uint32_t *src = srcp;
  8620. uint32_t *dest = destp;
  8621. uint32_t ldata;
  8622. int i;
  8623. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8624. ldata = *src;
  8625. ldata = be32_to_cpu(ldata);
  8626. *dest = ldata;
  8627. src++;
  8628. dest++;
  8629. }
  8630. }
  8631. /**
  8632. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8633. * @phba: Pointer to HBA context object.
  8634. * @pring: Pointer to driver SLI ring object.
  8635. * @mp: Pointer to driver buffer object.
  8636. *
  8637. * This function is called with no lock held.
  8638. * It always return zero after adding the buffer to the postbufq
  8639. * buffer list.
  8640. **/
  8641. int
  8642. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8643. struct lpfc_dmabuf *mp)
  8644. {
  8645. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8646. later */
  8647. spin_lock_irq(&phba->hbalock);
  8648. list_add_tail(&mp->list, &pring->postbufq);
  8649. pring->postbufq_cnt++;
  8650. spin_unlock_irq(&phba->hbalock);
  8651. return 0;
  8652. }
  8653. /**
  8654. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8655. * @phba: Pointer to HBA context object.
  8656. *
  8657. * When HBQ is enabled, buffers are searched based on tags. This function
  8658. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8659. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8660. * does not conflict with tags of buffer posted for unsolicited events.
  8661. * The function returns the allocated tag. The function is called with
  8662. * no locks held.
  8663. **/
  8664. uint32_t
  8665. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8666. {
  8667. spin_lock_irq(&phba->hbalock);
  8668. phba->buffer_tag_count++;
  8669. /*
  8670. * Always set the QUE_BUFTAG_BIT to distiguish between
  8671. * a tag assigned by HBQ.
  8672. */
  8673. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8674. spin_unlock_irq(&phba->hbalock);
  8675. return phba->buffer_tag_count;
  8676. }
  8677. /**
  8678. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8679. * @phba: Pointer to HBA context object.
  8680. * @pring: Pointer to driver SLI ring object.
  8681. * @tag: Buffer tag.
  8682. *
  8683. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8684. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8685. * iocb is posted to the response ring with the tag of the buffer.
  8686. * This function searches the pring->postbufq list using the tag
  8687. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8688. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8689. * buffer is returned to the caller else NULL is returned.
  8690. * This function is called with no lock held.
  8691. **/
  8692. struct lpfc_dmabuf *
  8693. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8694. uint32_t tag)
  8695. {
  8696. struct lpfc_dmabuf *mp, *next_mp;
  8697. struct list_head *slp = &pring->postbufq;
  8698. /* Search postbufq, from the beginning, looking for a match on tag */
  8699. spin_lock_irq(&phba->hbalock);
  8700. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8701. if (mp->buffer_tag == tag) {
  8702. list_del_init(&mp->list);
  8703. pring->postbufq_cnt--;
  8704. spin_unlock_irq(&phba->hbalock);
  8705. return mp;
  8706. }
  8707. }
  8708. spin_unlock_irq(&phba->hbalock);
  8709. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8710. "0402 Cannot find virtual addr for buffer tag on "
  8711. "ring %d Data x%lx x%p x%p x%x\n",
  8712. pring->ringno, (unsigned long) tag,
  8713. slp->next, slp->prev, pring->postbufq_cnt);
  8714. return NULL;
  8715. }
  8716. /**
  8717. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8718. * @phba: Pointer to HBA context object.
  8719. * @pring: Pointer to driver SLI ring object.
  8720. * @phys: DMA address of the buffer.
  8721. *
  8722. * This function searches the buffer list using the dma_address
  8723. * of unsolicited event to find the driver's lpfc_dmabuf object
  8724. * corresponding to the dma_address. The function returns the
  8725. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8726. * This function is called by the ct and els unsolicited event
  8727. * handlers to get the buffer associated with the unsolicited
  8728. * event.
  8729. *
  8730. * This function is called with no lock held.
  8731. **/
  8732. struct lpfc_dmabuf *
  8733. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8734. dma_addr_t phys)
  8735. {
  8736. struct lpfc_dmabuf *mp, *next_mp;
  8737. struct list_head *slp = &pring->postbufq;
  8738. /* Search postbufq, from the beginning, looking for a match on phys */
  8739. spin_lock_irq(&phba->hbalock);
  8740. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8741. if (mp->phys == phys) {
  8742. list_del_init(&mp->list);
  8743. pring->postbufq_cnt--;
  8744. spin_unlock_irq(&phba->hbalock);
  8745. return mp;
  8746. }
  8747. }
  8748. spin_unlock_irq(&phba->hbalock);
  8749. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8750. "0410 Cannot find virtual addr for mapped buf on "
  8751. "ring %d Data x%llx x%p x%p x%x\n",
  8752. pring->ringno, (unsigned long long)phys,
  8753. slp->next, slp->prev, pring->postbufq_cnt);
  8754. return NULL;
  8755. }
  8756. /**
  8757. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8758. * @phba: Pointer to HBA context object.
  8759. * @cmdiocb: Pointer to driver command iocb object.
  8760. * @rspiocb: Pointer to driver response iocb object.
  8761. *
  8762. * This function is the completion handler for the abort iocbs for
  8763. * ELS commands. This function is called from the ELS ring event
  8764. * handler with no lock held. This function frees memory resources
  8765. * associated with the abort iocb.
  8766. **/
  8767. static void
  8768. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8769. struct lpfc_iocbq *rspiocb)
  8770. {
  8771. IOCB_t *irsp = &rspiocb->iocb;
  8772. uint16_t abort_iotag, abort_context;
  8773. struct lpfc_iocbq *abort_iocb = NULL;
  8774. if (irsp->ulpStatus) {
  8775. /*
  8776. * Assume that the port already completed and returned, or
  8777. * will return the iocb. Just Log the message.
  8778. */
  8779. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8780. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8781. spin_lock_irq(&phba->hbalock);
  8782. if (phba->sli_rev < LPFC_SLI_REV4) {
  8783. if (abort_iotag != 0 &&
  8784. abort_iotag <= phba->sli.last_iotag)
  8785. abort_iocb =
  8786. phba->sli.iocbq_lookup[abort_iotag];
  8787. } else
  8788. /* For sli4 the abort_tag is the XRI,
  8789. * so the abort routine puts the iotag of the iocb
  8790. * being aborted in the context field of the abort
  8791. * IOCB.
  8792. */
  8793. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8794. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8795. "0327 Cannot abort els iocb %p "
  8796. "with tag %x context %x, abort status %x, "
  8797. "abort code %x\n",
  8798. abort_iocb, abort_iotag, abort_context,
  8799. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8800. spin_unlock_irq(&phba->hbalock);
  8801. }
  8802. lpfc_sli_release_iocbq(phba, cmdiocb);
  8803. return;
  8804. }
  8805. /**
  8806. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8807. * @phba: Pointer to HBA context object.
  8808. * @cmdiocb: Pointer to driver command iocb object.
  8809. * @rspiocb: Pointer to driver response iocb object.
  8810. *
  8811. * The function is called from SLI ring event handler with no
  8812. * lock held. This function is the completion handler for ELS commands
  8813. * which are aborted. The function frees memory resources used for
  8814. * the aborted ELS commands.
  8815. **/
  8816. static void
  8817. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8818. struct lpfc_iocbq *rspiocb)
  8819. {
  8820. IOCB_t *irsp = &rspiocb->iocb;
  8821. /* ELS cmd tag <ulpIoTag> completes */
  8822. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8823. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8824. "x%x x%x x%x\n",
  8825. irsp->ulpIoTag, irsp->ulpStatus,
  8826. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8827. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8828. lpfc_ct_free_iocb(phba, cmdiocb);
  8829. else
  8830. lpfc_els_free_iocb(phba, cmdiocb);
  8831. return;
  8832. }
  8833. /**
  8834. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8835. * @phba: Pointer to HBA context object.
  8836. * @pring: Pointer to driver SLI ring object.
  8837. * @cmdiocb: Pointer to driver command iocb object.
  8838. *
  8839. * This function issues an abort iocb for the provided command iocb down to
  8840. * the port. Other than the case the outstanding command iocb is an abort
  8841. * request, this function issues abort out unconditionally. This function is
  8842. * called with hbalock held. The function returns 0 when it fails due to
  8843. * memory allocation failure or when the command iocb is an abort request.
  8844. **/
  8845. static int
  8846. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8847. struct lpfc_iocbq *cmdiocb)
  8848. {
  8849. struct lpfc_vport *vport = cmdiocb->vport;
  8850. struct lpfc_iocbq *abtsiocbp;
  8851. IOCB_t *icmd = NULL;
  8852. IOCB_t *iabt = NULL;
  8853. int ring_number;
  8854. int retval;
  8855. unsigned long iflags;
  8856. /*
  8857. * There are certain command types we don't want to abort. And we
  8858. * don't want to abort commands that are already in the process of
  8859. * being aborted.
  8860. */
  8861. icmd = &cmdiocb->iocb;
  8862. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8863. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8864. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8865. return 0;
  8866. /* issue ABTS for this IOCB based on iotag */
  8867. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8868. if (abtsiocbp == NULL)
  8869. return 0;
  8870. /* This signals the response to set the correct status
  8871. * before calling the completion handler
  8872. */
  8873. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8874. iabt = &abtsiocbp->iocb;
  8875. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8876. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8877. if (phba->sli_rev == LPFC_SLI_REV4) {
  8878. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8879. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8880. }
  8881. else
  8882. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8883. iabt->ulpLe = 1;
  8884. iabt->ulpClass = icmd->ulpClass;
  8885. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8886. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8887. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8888. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8889. if (cmdiocb->iocb_flag & LPFC_IO_FOF)
  8890. abtsiocbp->iocb_flag |= LPFC_IO_FOF;
  8891. if (phba->link_state >= LPFC_LINK_UP)
  8892. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8893. else
  8894. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8895. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8896. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8897. "0339 Abort xri x%x, original iotag x%x, "
  8898. "abort cmd iotag x%x\n",
  8899. iabt->un.acxri.abortIoTag,
  8900. iabt->un.acxri.abortContextTag,
  8901. abtsiocbp->iotag);
  8902. if (phba->sli_rev == LPFC_SLI_REV4) {
  8903. ring_number =
  8904. lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
  8905. if (unlikely(ring_number == LPFC_HBA_ERROR))
  8906. return 0;
  8907. pring = &phba->sli.ring[ring_number];
  8908. /* Note: both hbalock and ring_lock need to be set here */
  8909. spin_lock_irqsave(&pring->ring_lock, iflags);
  8910. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8911. abtsiocbp, 0);
  8912. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  8913. } else {
  8914. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8915. abtsiocbp, 0);
  8916. }
  8917. if (retval)
  8918. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8919. /*
  8920. * Caller to this routine should check for IOCB_ERROR
  8921. * and handle it properly. This routine no longer removes
  8922. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8923. */
  8924. return retval;
  8925. }
  8926. /**
  8927. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8928. * @phba: Pointer to HBA context object.
  8929. * @pring: Pointer to driver SLI ring object.
  8930. * @cmdiocb: Pointer to driver command iocb object.
  8931. *
  8932. * This function issues an abort iocb for the provided command iocb. In case
  8933. * of unloading, the abort iocb will not be issued to commands on the ELS
  8934. * ring. Instead, the callback function shall be changed to those commands
  8935. * so that nothing happens when them finishes. This function is called with
  8936. * hbalock held. The function returns 0 when the command iocb is an abort
  8937. * request.
  8938. **/
  8939. int
  8940. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8941. struct lpfc_iocbq *cmdiocb)
  8942. {
  8943. struct lpfc_vport *vport = cmdiocb->vport;
  8944. int retval = IOCB_ERROR;
  8945. IOCB_t *icmd = NULL;
  8946. /*
  8947. * There are certain command types we don't want to abort. And we
  8948. * don't want to abort commands that are already in the process of
  8949. * being aborted.
  8950. */
  8951. icmd = &cmdiocb->iocb;
  8952. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8953. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8954. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8955. return 0;
  8956. /*
  8957. * If we're unloading, don't abort iocb on the ELS ring, but change
  8958. * the callback so that nothing happens when it finishes.
  8959. */
  8960. if ((vport->load_flag & FC_UNLOADING) &&
  8961. (pring->ringno == LPFC_ELS_RING)) {
  8962. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8963. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8964. else
  8965. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8966. goto abort_iotag_exit;
  8967. }
  8968. /* Now, we try to issue the abort to the cmdiocb out */
  8969. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8970. abort_iotag_exit:
  8971. /*
  8972. * Caller to this routine should check for IOCB_ERROR
  8973. * and handle it properly. This routine no longer removes
  8974. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8975. */
  8976. return retval;
  8977. }
  8978. /**
  8979. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8980. * @phba: pointer to lpfc HBA data structure.
  8981. *
  8982. * This routine will abort all pending and outstanding iocbs to an HBA.
  8983. **/
  8984. void
  8985. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8986. {
  8987. struct lpfc_sli *psli = &phba->sli;
  8988. struct lpfc_sli_ring *pring;
  8989. int i;
  8990. for (i = 0; i < psli->num_rings; i++) {
  8991. pring = &psli->ring[i];
  8992. lpfc_sli_abort_iocb_ring(phba, pring);
  8993. }
  8994. }
  8995. /**
  8996. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8997. * @iocbq: Pointer to driver iocb object.
  8998. * @vport: Pointer to driver virtual port object.
  8999. * @tgt_id: SCSI ID of the target.
  9000. * @lun_id: LUN ID of the scsi device.
  9001. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  9002. *
  9003. * This function acts as an iocb filter for functions which abort or count
  9004. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  9005. * 0 if the filtering criteria is met for the given iocb and will return
  9006. * 1 if the filtering criteria is not met.
  9007. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  9008. * given iocb is for the SCSI device specified by vport, tgt_id and
  9009. * lun_id parameter.
  9010. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  9011. * given iocb is for the SCSI target specified by vport and tgt_id
  9012. * parameters.
  9013. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  9014. * given iocb is for the SCSI host associated with the given vport.
  9015. * This function is called with no locks held.
  9016. **/
  9017. static int
  9018. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  9019. uint16_t tgt_id, uint64_t lun_id,
  9020. lpfc_ctx_cmd ctx_cmd)
  9021. {
  9022. struct lpfc_scsi_buf *lpfc_cmd;
  9023. int rc = 1;
  9024. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  9025. return rc;
  9026. if (iocbq->vport != vport)
  9027. return rc;
  9028. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  9029. if (lpfc_cmd->pCmd == NULL)
  9030. return rc;
  9031. switch (ctx_cmd) {
  9032. case LPFC_CTX_LUN:
  9033. if ((lpfc_cmd->rdata->pnode) &&
  9034. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  9035. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  9036. rc = 0;
  9037. break;
  9038. case LPFC_CTX_TGT:
  9039. if ((lpfc_cmd->rdata->pnode) &&
  9040. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  9041. rc = 0;
  9042. break;
  9043. case LPFC_CTX_HOST:
  9044. rc = 0;
  9045. break;
  9046. default:
  9047. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  9048. __func__, ctx_cmd);
  9049. break;
  9050. }
  9051. return rc;
  9052. }
  9053. /**
  9054. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  9055. * @vport: Pointer to virtual port.
  9056. * @tgt_id: SCSI ID of the target.
  9057. * @lun_id: LUN ID of the scsi device.
  9058. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  9059. *
  9060. * This function returns number of FCP commands pending for the vport.
  9061. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  9062. * commands pending on the vport associated with SCSI device specified
  9063. * by tgt_id and lun_id parameters.
  9064. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  9065. * commands pending on the vport associated with SCSI target specified
  9066. * by tgt_id parameter.
  9067. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  9068. * commands pending on the vport.
  9069. * This function returns the number of iocbs which satisfy the filter.
  9070. * This function is called without any lock held.
  9071. **/
  9072. int
  9073. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  9074. lpfc_ctx_cmd ctx_cmd)
  9075. {
  9076. struct lpfc_hba *phba = vport->phba;
  9077. struct lpfc_iocbq *iocbq;
  9078. int sum, i;
  9079. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  9080. iocbq = phba->sli.iocbq_lookup[i];
  9081. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  9082. ctx_cmd) == 0)
  9083. sum++;
  9084. }
  9085. return sum;
  9086. }
  9087. /**
  9088. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  9089. * @phba: Pointer to HBA context object
  9090. * @cmdiocb: Pointer to command iocb object.
  9091. * @rspiocb: Pointer to response iocb object.
  9092. *
  9093. * This function is called when an aborted FCP iocb completes. This
  9094. * function is called by the ring event handler with no lock held.
  9095. * This function frees the iocb.
  9096. **/
  9097. void
  9098. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  9099. struct lpfc_iocbq *rspiocb)
  9100. {
  9101. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9102. "3096 ABORT_XRI_CN completing on rpi x%x "
  9103. "original iotag x%x, abort cmd iotag x%x "
  9104. "status 0x%x, reason 0x%x\n",
  9105. cmdiocb->iocb.un.acxri.abortContextTag,
  9106. cmdiocb->iocb.un.acxri.abortIoTag,
  9107. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  9108. rspiocb->iocb.un.ulpWord[4]);
  9109. lpfc_sli_release_iocbq(phba, cmdiocb);
  9110. return;
  9111. }
  9112. /**
  9113. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  9114. * @vport: Pointer to virtual port.
  9115. * @pring: Pointer to driver SLI ring object.
  9116. * @tgt_id: SCSI ID of the target.
  9117. * @lun_id: LUN ID of the scsi device.
  9118. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  9119. *
  9120. * This function sends an abort command for every SCSI command
  9121. * associated with the given virtual port pending on the ring
  9122. * filtered by lpfc_sli_validate_fcp_iocb function.
  9123. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  9124. * FCP iocbs associated with lun specified by tgt_id and lun_id
  9125. * parameters
  9126. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  9127. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  9128. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  9129. * FCP iocbs associated with virtual port.
  9130. * This function returns number of iocbs it failed to abort.
  9131. * This function is called with no locks held.
  9132. **/
  9133. int
  9134. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  9135. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  9136. {
  9137. struct lpfc_hba *phba = vport->phba;
  9138. struct lpfc_iocbq *iocbq;
  9139. struct lpfc_iocbq *abtsiocb;
  9140. IOCB_t *cmd = NULL;
  9141. int errcnt = 0, ret_val = 0;
  9142. int i;
  9143. for (i = 1; i <= phba->sli.last_iotag; i++) {
  9144. iocbq = phba->sli.iocbq_lookup[i];
  9145. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  9146. abort_cmd) != 0)
  9147. continue;
  9148. /*
  9149. * If the iocbq is already being aborted, don't take a second
  9150. * action, but do count it.
  9151. */
  9152. if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  9153. continue;
  9154. /* issue ABTS for this IOCB based on iotag */
  9155. abtsiocb = lpfc_sli_get_iocbq(phba);
  9156. if (abtsiocb == NULL) {
  9157. errcnt++;
  9158. continue;
  9159. }
  9160. /* indicate the IO is being aborted by the driver. */
  9161. iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
  9162. cmd = &iocbq->iocb;
  9163. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  9164. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  9165. if (phba->sli_rev == LPFC_SLI_REV4)
  9166. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  9167. else
  9168. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  9169. abtsiocb->iocb.ulpLe = 1;
  9170. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  9171. abtsiocb->vport = vport;
  9172. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  9173. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  9174. if (iocbq->iocb_flag & LPFC_IO_FCP)
  9175. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  9176. if (iocbq->iocb_flag & LPFC_IO_FOF)
  9177. abtsiocb->iocb_flag |= LPFC_IO_FOF;
  9178. if (lpfc_is_link_up(phba))
  9179. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  9180. else
  9181. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  9182. /* Setup callback routine and issue the command. */
  9183. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  9184. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  9185. abtsiocb, 0);
  9186. if (ret_val == IOCB_ERROR) {
  9187. lpfc_sli_release_iocbq(phba, abtsiocb);
  9188. errcnt++;
  9189. continue;
  9190. }
  9191. }
  9192. return errcnt;
  9193. }
  9194. /**
  9195. * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
  9196. * @vport: Pointer to virtual port.
  9197. * @pring: Pointer to driver SLI ring object.
  9198. * @tgt_id: SCSI ID of the target.
  9199. * @lun_id: LUN ID of the scsi device.
  9200. * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  9201. *
  9202. * This function sends an abort command for every SCSI command
  9203. * associated with the given virtual port pending on the ring
  9204. * filtered by lpfc_sli_validate_fcp_iocb function.
  9205. * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
  9206. * FCP iocbs associated with lun specified by tgt_id and lun_id
  9207. * parameters
  9208. * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
  9209. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  9210. * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
  9211. * FCP iocbs associated with virtual port.
  9212. * This function returns number of iocbs it aborted .
  9213. * This function is called with no locks held right after a taskmgmt
  9214. * command is sent.
  9215. **/
  9216. int
  9217. lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  9218. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
  9219. {
  9220. struct lpfc_hba *phba = vport->phba;
  9221. struct lpfc_scsi_buf *lpfc_cmd;
  9222. struct lpfc_iocbq *abtsiocbq;
  9223. struct lpfc_nodelist *ndlp;
  9224. struct lpfc_iocbq *iocbq;
  9225. IOCB_t *icmd;
  9226. int sum, i, ret_val;
  9227. unsigned long iflags;
  9228. struct lpfc_sli_ring *pring_s4;
  9229. uint32_t ring_number;
  9230. spin_lock_irq(&phba->hbalock);
  9231. /* all I/Os are in process of being flushed */
  9232. if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
  9233. spin_unlock_irq(&phba->hbalock);
  9234. return 0;
  9235. }
  9236. sum = 0;
  9237. for (i = 1; i <= phba->sli.last_iotag; i++) {
  9238. iocbq = phba->sli.iocbq_lookup[i];
  9239. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  9240. cmd) != 0)
  9241. continue;
  9242. /*
  9243. * If the iocbq is already being aborted, don't take a second
  9244. * action, but do count it.
  9245. */
  9246. if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  9247. continue;
  9248. /* issue ABTS for this IOCB based on iotag */
  9249. abtsiocbq = __lpfc_sli_get_iocbq(phba);
  9250. if (abtsiocbq == NULL)
  9251. continue;
  9252. icmd = &iocbq->iocb;
  9253. abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  9254. abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
  9255. if (phba->sli_rev == LPFC_SLI_REV4)
  9256. abtsiocbq->iocb.un.acxri.abortIoTag =
  9257. iocbq->sli4_xritag;
  9258. else
  9259. abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
  9260. abtsiocbq->iocb.ulpLe = 1;
  9261. abtsiocbq->iocb.ulpClass = icmd->ulpClass;
  9262. abtsiocbq->vport = vport;
  9263. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  9264. abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
  9265. if (iocbq->iocb_flag & LPFC_IO_FCP)
  9266. abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
  9267. if (iocbq->iocb_flag & LPFC_IO_FOF)
  9268. abtsiocbq->iocb_flag |= LPFC_IO_FOF;
  9269. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  9270. ndlp = lpfc_cmd->rdata->pnode;
  9271. if (lpfc_is_link_up(phba) &&
  9272. (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
  9273. abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  9274. else
  9275. abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  9276. /* Setup callback routine and issue the command. */
  9277. abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  9278. /*
  9279. * Indicate the IO is being aborted by the driver and set
  9280. * the caller's flag into the aborted IO.
  9281. */
  9282. iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
  9283. if (phba->sli_rev == LPFC_SLI_REV4) {
  9284. ring_number = MAX_SLI3_CONFIGURED_RINGS +
  9285. iocbq->fcp_wqidx;
  9286. pring_s4 = &phba->sli.ring[ring_number];
  9287. /* Note: both hbalock and ring_lock must be set here */
  9288. spin_lock_irqsave(&pring_s4->ring_lock, iflags);
  9289. ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
  9290. abtsiocbq, 0);
  9291. spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
  9292. } else {
  9293. ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
  9294. abtsiocbq, 0);
  9295. }
  9296. if (ret_val == IOCB_ERROR)
  9297. __lpfc_sli_release_iocbq(phba, abtsiocbq);
  9298. else
  9299. sum++;
  9300. }
  9301. spin_unlock_irq(&phba->hbalock);
  9302. return sum;
  9303. }
  9304. /**
  9305. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  9306. * @phba: Pointer to HBA context object.
  9307. * @cmdiocbq: Pointer to command iocb.
  9308. * @rspiocbq: Pointer to response iocb.
  9309. *
  9310. * This function is the completion handler for iocbs issued using
  9311. * lpfc_sli_issue_iocb_wait function. This function is called by the
  9312. * ring event handler function without any lock held. This function
  9313. * can be called from both worker thread context and interrupt
  9314. * context. This function also can be called from other thread which
  9315. * cleans up the SLI layer objects.
  9316. * This function copy the contents of the response iocb to the
  9317. * response iocb memory object provided by the caller of
  9318. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  9319. * sleeps for the iocb completion.
  9320. **/
  9321. static void
  9322. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  9323. struct lpfc_iocbq *cmdiocbq,
  9324. struct lpfc_iocbq *rspiocbq)
  9325. {
  9326. wait_queue_head_t *pdone_q;
  9327. unsigned long iflags;
  9328. struct lpfc_scsi_buf *lpfc_cmd;
  9329. spin_lock_irqsave(&phba->hbalock, iflags);
  9330. if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
  9331. /*
  9332. * A time out has occurred for the iocb. If a time out
  9333. * completion handler has been supplied, call it. Otherwise,
  9334. * just free the iocbq.
  9335. */
  9336. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9337. cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
  9338. cmdiocbq->wait_iocb_cmpl = NULL;
  9339. if (cmdiocbq->iocb_cmpl)
  9340. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
  9341. else
  9342. lpfc_sli_release_iocbq(phba, cmdiocbq);
  9343. return;
  9344. }
  9345. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  9346. if (cmdiocbq->context2 && rspiocbq)
  9347. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  9348. &rspiocbq->iocb, sizeof(IOCB_t));
  9349. /* Set the exchange busy flag for task management commands */
  9350. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  9351. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  9352. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  9353. cur_iocbq);
  9354. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  9355. }
  9356. pdone_q = cmdiocbq->context_un.wait_queue;
  9357. if (pdone_q)
  9358. wake_up(pdone_q);
  9359. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9360. return;
  9361. }
  9362. /**
  9363. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  9364. * @phba: Pointer to HBA context object..
  9365. * @piocbq: Pointer to command iocb.
  9366. * @flag: Flag to test.
  9367. *
  9368. * This routine grabs the hbalock and then test the iocb_flag to
  9369. * see if the passed in flag is set.
  9370. * Returns:
  9371. * 1 if flag is set.
  9372. * 0 if flag is not set.
  9373. **/
  9374. static int
  9375. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  9376. struct lpfc_iocbq *piocbq, uint32_t flag)
  9377. {
  9378. unsigned long iflags;
  9379. int ret;
  9380. spin_lock_irqsave(&phba->hbalock, iflags);
  9381. ret = piocbq->iocb_flag & flag;
  9382. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9383. return ret;
  9384. }
  9385. /**
  9386. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  9387. * @phba: Pointer to HBA context object..
  9388. * @pring: Pointer to sli ring.
  9389. * @piocb: Pointer to command iocb.
  9390. * @prspiocbq: Pointer to response iocb.
  9391. * @timeout: Timeout in number of seconds.
  9392. *
  9393. * This function issues the iocb to firmware and waits for the
  9394. * iocb to complete. The iocb_cmpl field of the shall be used
  9395. * to handle iocbs which time out. If the field is NULL, the
  9396. * function shall free the iocbq structure. If more clean up is
  9397. * needed, the caller is expected to provide a completion function
  9398. * that will provide the needed clean up. If the iocb command is
  9399. * not completed within timeout seconds, the function will either
  9400. * free the iocbq structure (if iocb_cmpl == NULL) or execute the
  9401. * completion function set in the iocb_cmpl field and then return
  9402. * a status of IOCB_TIMEDOUT. The caller should not free the iocb
  9403. * resources if this function returns IOCB_TIMEDOUT.
  9404. * The function waits for the iocb completion using an
  9405. * non-interruptible wait.
  9406. * This function will sleep while waiting for iocb completion.
  9407. * So, this function should not be called from any context which
  9408. * does not allow sleeping. Due to the same reason, this function
  9409. * cannot be called with interrupt disabled.
  9410. * This function assumes that the iocb completions occur while
  9411. * this function sleep. So, this function cannot be called from
  9412. * the thread which process iocb completion for this ring.
  9413. * This function clears the iocb_flag of the iocb object before
  9414. * issuing the iocb and the iocb completion handler sets this
  9415. * flag and wakes this thread when the iocb completes.
  9416. * The contents of the response iocb will be copied to prspiocbq
  9417. * by the completion handler when the command completes.
  9418. * This function returns IOCB_SUCCESS when success.
  9419. * This function is called with no lock held.
  9420. **/
  9421. int
  9422. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  9423. uint32_t ring_number,
  9424. struct lpfc_iocbq *piocb,
  9425. struct lpfc_iocbq *prspiocbq,
  9426. uint32_t timeout)
  9427. {
  9428. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9429. long timeleft, timeout_req = 0;
  9430. int retval = IOCB_SUCCESS;
  9431. uint32_t creg_val;
  9432. struct lpfc_iocbq *iocb;
  9433. int txq_cnt = 0;
  9434. int txcmplq_cnt = 0;
  9435. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9436. unsigned long iflags;
  9437. bool iocb_completed = true;
  9438. /*
  9439. * If the caller has provided a response iocbq buffer, then context2
  9440. * is NULL or its an error.
  9441. */
  9442. if (prspiocbq) {
  9443. if (piocb->context2)
  9444. return IOCB_ERROR;
  9445. piocb->context2 = prspiocbq;
  9446. }
  9447. piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
  9448. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  9449. piocb->context_un.wait_queue = &done_q;
  9450. piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
  9451. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9452. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9453. return IOCB_ERROR;
  9454. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  9455. writel(creg_val, phba->HCregaddr);
  9456. readl(phba->HCregaddr); /* flush */
  9457. }
  9458. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  9459. SLI_IOCB_RET_IOCB);
  9460. if (retval == IOCB_SUCCESS) {
  9461. timeout_req = msecs_to_jiffies(timeout * 1000);
  9462. timeleft = wait_event_timeout(done_q,
  9463. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  9464. timeout_req);
  9465. spin_lock_irqsave(&phba->hbalock, iflags);
  9466. if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
  9467. /*
  9468. * IOCB timed out. Inform the wake iocb wait
  9469. * completion function and set local status
  9470. */
  9471. iocb_completed = false;
  9472. piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
  9473. }
  9474. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9475. if (iocb_completed) {
  9476. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9477. "0331 IOCB wake signaled\n");
  9478. /* Note: we are not indicating if the IOCB has a success
  9479. * status or not - that's for the caller to check.
  9480. * IOCB_SUCCESS means just that the command was sent and
  9481. * completed. Not that it completed successfully.
  9482. * */
  9483. } else if (timeleft == 0) {
  9484. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9485. "0338 IOCB wait timeout error - no "
  9486. "wake response Data x%x\n", timeout);
  9487. retval = IOCB_TIMEDOUT;
  9488. } else {
  9489. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9490. "0330 IOCB wake NOT set, "
  9491. "Data x%x x%lx\n",
  9492. timeout, (timeleft / jiffies));
  9493. retval = IOCB_TIMEDOUT;
  9494. }
  9495. } else if (retval == IOCB_BUSY) {
  9496. if (phba->cfg_log_verbose & LOG_SLI) {
  9497. list_for_each_entry(iocb, &pring->txq, list) {
  9498. txq_cnt++;
  9499. }
  9500. list_for_each_entry(iocb, &pring->txcmplq, list) {
  9501. txcmplq_cnt++;
  9502. }
  9503. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9504. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  9505. phba->iocb_cnt, txq_cnt, txcmplq_cnt);
  9506. }
  9507. return retval;
  9508. } else {
  9509. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9510. "0332 IOCB wait issue failed, Data x%x\n",
  9511. retval);
  9512. retval = IOCB_ERROR;
  9513. }
  9514. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9515. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9516. return IOCB_ERROR;
  9517. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  9518. writel(creg_val, phba->HCregaddr);
  9519. readl(phba->HCregaddr); /* flush */
  9520. }
  9521. if (prspiocbq)
  9522. piocb->context2 = NULL;
  9523. piocb->context_un.wait_queue = NULL;
  9524. piocb->iocb_cmpl = NULL;
  9525. return retval;
  9526. }
  9527. /**
  9528. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  9529. * @phba: Pointer to HBA context object.
  9530. * @pmboxq: Pointer to driver mailbox object.
  9531. * @timeout: Timeout in number of seconds.
  9532. *
  9533. * This function issues the mailbox to firmware and waits for the
  9534. * mailbox command to complete. If the mailbox command is not
  9535. * completed within timeout seconds, it returns MBX_TIMEOUT.
  9536. * The function waits for the mailbox completion using an
  9537. * interruptible wait. If the thread is woken up due to a
  9538. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  9539. * should not free the mailbox resources, if this function returns
  9540. * MBX_TIMEOUT.
  9541. * This function will sleep while waiting for mailbox completion.
  9542. * So, this function should not be called from any context which
  9543. * does not allow sleeping. Due to the same reason, this function
  9544. * cannot be called with interrupt disabled.
  9545. * This function assumes that the mailbox completion occurs while
  9546. * this function sleep. So, this function cannot be called from
  9547. * the worker thread which processes mailbox completion.
  9548. * This function is called in the context of HBA management
  9549. * applications.
  9550. * This function returns MBX_SUCCESS when successful.
  9551. * This function is called with no lock held.
  9552. **/
  9553. int
  9554. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  9555. uint32_t timeout)
  9556. {
  9557. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9558. MAILBOX_t *mb = NULL;
  9559. int retval;
  9560. unsigned long flag;
  9561. /* The caller might set context1 for extended buffer */
  9562. if (pmboxq->context1)
  9563. mb = (MAILBOX_t *)pmboxq->context1;
  9564. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  9565. /* setup wake call as IOCB callback */
  9566. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  9567. /* setup context field to pass wait_queue pointer to wake function */
  9568. pmboxq->context1 = &done_q;
  9569. /* now issue the command */
  9570. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  9571. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  9572. wait_event_interruptible_timeout(done_q,
  9573. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  9574. msecs_to_jiffies(timeout * 1000));
  9575. spin_lock_irqsave(&phba->hbalock, flag);
  9576. /* restore the possible extended buffer for free resource */
  9577. pmboxq->context1 = (uint8_t *)mb;
  9578. /*
  9579. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  9580. * else do not free the resources.
  9581. */
  9582. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  9583. retval = MBX_SUCCESS;
  9584. } else {
  9585. retval = MBX_TIMEOUT;
  9586. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9587. }
  9588. spin_unlock_irqrestore(&phba->hbalock, flag);
  9589. } else {
  9590. /* restore the possible extended buffer for free resource */
  9591. pmboxq->context1 = (uint8_t *)mb;
  9592. }
  9593. return retval;
  9594. }
  9595. /**
  9596. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  9597. * @phba: Pointer to HBA context.
  9598. *
  9599. * This function is called to shutdown the driver's mailbox sub-system.
  9600. * It first marks the mailbox sub-system is in a block state to prevent
  9601. * the asynchronous mailbox command from issued off the pending mailbox
  9602. * command queue. If the mailbox command sub-system shutdown is due to
  9603. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  9604. * the mailbox sub-system flush routine to forcefully bring down the
  9605. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  9606. * as with offline or HBA function reset), this routine will wait for the
  9607. * outstanding mailbox command to complete before invoking the mailbox
  9608. * sub-system flush routine to gracefully bring down mailbox sub-system.
  9609. **/
  9610. void
  9611. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
  9612. {
  9613. struct lpfc_sli *psli = &phba->sli;
  9614. unsigned long timeout;
  9615. if (mbx_action == LPFC_MBX_NO_WAIT) {
  9616. /* delay 100ms for port state */
  9617. msleep(100);
  9618. lpfc_sli_mbox_sys_flush(phba);
  9619. return;
  9620. }
  9621. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  9622. spin_lock_irq(&phba->hbalock);
  9623. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  9624. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  9625. /* Determine how long we might wait for the active mailbox
  9626. * command to be gracefully completed by firmware.
  9627. */
  9628. if (phba->sli.mbox_active)
  9629. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  9630. phba->sli.mbox_active) *
  9631. 1000) + jiffies;
  9632. spin_unlock_irq(&phba->hbalock);
  9633. while (phba->sli.mbox_active) {
  9634. /* Check active mailbox complete status every 2ms */
  9635. msleep(2);
  9636. if (time_after(jiffies, timeout))
  9637. /* Timeout, let the mailbox flush routine to
  9638. * forcefully release active mailbox command
  9639. */
  9640. break;
  9641. }
  9642. } else
  9643. spin_unlock_irq(&phba->hbalock);
  9644. lpfc_sli_mbox_sys_flush(phba);
  9645. }
  9646. /**
  9647. * lpfc_sli_eratt_read - read sli-3 error attention events
  9648. * @phba: Pointer to HBA context.
  9649. *
  9650. * This function is called to read the SLI3 device error attention registers
  9651. * for possible error attention events. The caller must hold the hostlock
  9652. * with spin_lock_irq().
  9653. *
  9654. * This function returns 1 when there is Error Attention in the Host Attention
  9655. * Register and returns 0 otherwise.
  9656. **/
  9657. static int
  9658. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  9659. {
  9660. uint32_t ha_copy;
  9661. /* Read chip Host Attention (HA) register */
  9662. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9663. goto unplug_err;
  9664. if (ha_copy & HA_ERATT) {
  9665. /* Read host status register to retrieve error event */
  9666. if (lpfc_sli_read_hs(phba))
  9667. goto unplug_err;
  9668. /* Check if there is a deferred error condition is active */
  9669. if ((HS_FFER1 & phba->work_hs) &&
  9670. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9671. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9672. phba->hba_flag |= DEFER_ERATT;
  9673. /* Clear all interrupt enable conditions */
  9674. writel(0, phba->HCregaddr);
  9675. readl(phba->HCregaddr);
  9676. }
  9677. /* Set the driver HA work bitmap */
  9678. phba->work_ha |= HA_ERATT;
  9679. /* Indicate polling handles this ERATT */
  9680. phba->hba_flag |= HBA_ERATT_HANDLED;
  9681. return 1;
  9682. }
  9683. return 0;
  9684. unplug_err:
  9685. /* Set the driver HS work bitmap */
  9686. phba->work_hs |= UNPLUG_ERR;
  9687. /* Set the driver HA work bitmap */
  9688. phba->work_ha |= HA_ERATT;
  9689. /* Indicate polling handles this ERATT */
  9690. phba->hba_flag |= HBA_ERATT_HANDLED;
  9691. return 1;
  9692. }
  9693. /**
  9694. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9695. * @phba: Pointer to HBA context.
  9696. *
  9697. * This function is called to read the SLI4 device error attention registers
  9698. * for possible error attention events. The caller must hold the hostlock
  9699. * with spin_lock_irq().
  9700. *
  9701. * This function returns 1 when there is Error Attention in the Host Attention
  9702. * Register and returns 0 otherwise.
  9703. **/
  9704. static int
  9705. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9706. {
  9707. uint32_t uerr_sta_hi, uerr_sta_lo;
  9708. uint32_t if_type, portsmphr;
  9709. struct lpfc_register portstat_reg;
  9710. /*
  9711. * For now, use the SLI4 device internal unrecoverable error
  9712. * registers for error attention. This can be changed later.
  9713. */
  9714. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9715. switch (if_type) {
  9716. case LPFC_SLI_INTF_IF_TYPE_0:
  9717. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9718. &uerr_sta_lo) ||
  9719. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9720. &uerr_sta_hi)) {
  9721. phba->work_hs |= UNPLUG_ERR;
  9722. phba->work_ha |= HA_ERATT;
  9723. phba->hba_flag |= HBA_ERATT_HANDLED;
  9724. return 1;
  9725. }
  9726. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9727. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9728. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9729. "1423 HBA Unrecoverable error: "
  9730. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9731. "ue_mask_lo_reg=0x%x, "
  9732. "ue_mask_hi_reg=0x%x\n",
  9733. uerr_sta_lo, uerr_sta_hi,
  9734. phba->sli4_hba.ue_mask_lo,
  9735. phba->sli4_hba.ue_mask_hi);
  9736. phba->work_status[0] = uerr_sta_lo;
  9737. phba->work_status[1] = uerr_sta_hi;
  9738. phba->work_ha |= HA_ERATT;
  9739. phba->hba_flag |= HBA_ERATT_HANDLED;
  9740. return 1;
  9741. }
  9742. break;
  9743. case LPFC_SLI_INTF_IF_TYPE_2:
  9744. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9745. &portstat_reg.word0) ||
  9746. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9747. &portsmphr)){
  9748. phba->work_hs |= UNPLUG_ERR;
  9749. phba->work_ha |= HA_ERATT;
  9750. phba->hba_flag |= HBA_ERATT_HANDLED;
  9751. return 1;
  9752. }
  9753. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9754. phba->work_status[0] =
  9755. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9756. phba->work_status[1] =
  9757. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9758. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9759. "2885 Port Status Event: "
  9760. "port status reg 0x%x, "
  9761. "port smphr reg 0x%x, "
  9762. "error 1=0x%x, error 2=0x%x\n",
  9763. portstat_reg.word0,
  9764. portsmphr,
  9765. phba->work_status[0],
  9766. phba->work_status[1]);
  9767. phba->work_ha |= HA_ERATT;
  9768. phba->hba_flag |= HBA_ERATT_HANDLED;
  9769. return 1;
  9770. }
  9771. break;
  9772. case LPFC_SLI_INTF_IF_TYPE_1:
  9773. default:
  9774. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9775. "2886 HBA Error Attention on unsupported "
  9776. "if type %d.", if_type);
  9777. return 1;
  9778. }
  9779. return 0;
  9780. }
  9781. /**
  9782. * lpfc_sli_check_eratt - check error attention events
  9783. * @phba: Pointer to HBA context.
  9784. *
  9785. * This function is called from timer soft interrupt context to check HBA's
  9786. * error attention register bit for error attention events.
  9787. *
  9788. * This function returns 1 when there is Error Attention in the Host Attention
  9789. * Register and returns 0 otherwise.
  9790. **/
  9791. int
  9792. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9793. {
  9794. uint32_t ha_copy;
  9795. /* If somebody is waiting to handle an eratt, don't process it
  9796. * here. The brdkill function will do this.
  9797. */
  9798. if (phba->link_flag & LS_IGNORE_ERATT)
  9799. return 0;
  9800. /* Check if interrupt handler handles this ERATT */
  9801. spin_lock_irq(&phba->hbalock);
  9802. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9803. /* Interrupt handler has handled ERATT */
  9804. spin_unlock_irq(&phba->hbalock);
  9805. return 0;
  9806. }
  9807. /*
  9808. * If there is deferred error attention, do not check for error
  9809. * attention
  9810. */
  9811. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9812. spin_unlock_irq(&phba->hbalock);
  9813. return 0;
  9814. }
  9815. /* If PCI channel is offline, don't process it */
  9816. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9817. spin_unlock_irq(&phba->hbalock);
  9818. return 0;
  9819. }
  9820. switch (phba->sli_rev) {
  9821. case LPFC_SLI_REV2:
  9822. case LPFC_SLI_REV3:
  9823. /* Read chip Host Attention (HA) register */
  9824. ha_copy = lpfc_sli_eratt_read(phba);
  9825. break;
  9826. case LPFC_SLI_REV4:
  9827. /* Read device Uncoverable Error (UERR) registers */
  9828. ha_copy = lpfc_sli4_eratt_read(phba);
  9829. break;
  9830. default:
  9831. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9832. "0299 Invalid SLI revision (%d)\n",
  9833. phba->sli_rev);
  9834. ha_copy = 0;
  9835. break;
  9836. }
  9837. spin_unlock_irq(&phba->hbalock);
  9838. return ha_copy;
  9839. }
  9840. /**
  9841. * lpfc_intr_state_check - Check device state for interrupt handling
  9842. * @phba: Pointer to HBA context.
  9843. *
  9844. * This inline routine checks whether a device or its PCI slot is in a state
  9845. * that the interrupt should be handled.
  9846. *
  9847. * This function returns 0 if the device or the PCI slot is in a state that
  9848. * interrupt should be handled, otherwise -EIO.
  9849. */
  9850. static inline int
  9851. lpfc_intr_state_check(struct lpfc_hba *phba)
  9852. {
  9853. /* If the pci channel is offline, ignore all the interrupts */
  9854. if (unlikely(pci_channel_offline(phba->pcidev)))
  9855. return -EIO;
  9856. /* Update device level interrupt statistics */
  9857. phba->sli.slistat.sli_intr++;
  9858. /* Ignore all interrupts during initialization. */
  9859. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9860. return -EIO;
  9861. return 0;
  9862. }
  9863. /**
  9864. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9865. * @irq: Interrupt number.
  9866. * @dev_id: The device context pointer.
  9867. *
  9868. * This function is directly called from the PCI layer as an interrupt
  9869. * service routine when device with SLI-3 interface spec is enabled with
  9870. * MSI-X multi-message interrupt mode and there are slow-path events in
  9871. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9872. * interrupt mode, this function is called as part of the device-level
  9873. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9874. * is undergoing initialization, the interrupt handler will not process
  9875. * the interrupt. The link attention and ELS ring attention events are
  9876. * handled by the worker thread. The interrupt handler signals the worker
  9877. * thread and returns for these events. This function is called without
  9878. * any lock held. It gets the hbalock to access and update SLI data
  9879. * structures.
  9880. *
  9881. * This function returns IRQ_HANDLED when interrupt is handled else it
  9882. * returns IRQ_NONE.
  9883. **/
  9884. irqreturn_t
  9885. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9886. {
  9887. struct lpfc_hba *phba;
  9888. uint32_t ha_copy, hc_copy;
  9889. uint32_t work_ha_copy;
  9890. unsigned long status;
  9891. unsigned long iflag;
  9892. uint32_t control;
  9893. MAILBOX_t *mbox, *pmbox;
  9894. struct lpfc_vport *vport;
  9895. struct lpfc_nodelist *ndlp;
  9896. struct lpfc_dmabuf *mp;
  9897. LPFC_MBOXQ_t *pmb;
  9898. int rc;
  9899. /*
  9900. * Get the driver's phba structure from the dev_id and
  9901. * assume the HBA is not interrupting.
  9902. */
  9903. phba = (struct lpfc_hba *)dev_id;
  9904. if (unlikely(!phba))
  9905. return IRQ_NONE;
  9906. /*
  9907. * Stuff needs to be attented to when this function is invoked as an
  9908. * individual interrupt handler in MSI-X multi-message interrupt mode
  9909. */
  9910. if (phba->intr_type == MSIX) {
  9911. /* Check device state for handling interrupt */
  9912. if (lpfc_intr_state_check(phba))
  9913. return IRQ_NONE;
  9914. /* Need to read HA REG for slow-path events */
  9915. spin_lock_irqsave(&phba->hbalock, iflag);
  9916. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9917. goto unplug_error;
  9918. /* If somebody is waiting to handle an eratt don't process it
  9919. * here. The brdkill function will do this.
  9920. */
  9921. if (phba->link_flag & LS_IGNORE_ERATT)
  9922. ha_copy &= ~HA_ERATT;
  9923. /* Check the need for handling ERATT in interrupt handler */
  9924. if (ha_copy & HA_ERATT) {
  9925. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9926. /* ERATT polling has handled ERATT */
  9927. ha_copy &= ~HA_ERATT;
  9928. else
  9929. /* Indicate interrupt handler handles ERATT */
  9930. phba->hba_flag |= HBA_ERATT_HANDLED;
  9931. }
  9932. /*
  9933. * If there is deferred error attention, do not check for any
  9934. * interrupt.
  9935. */
  9936. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9937. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9938. return IRQ_NONE;
  9939. }
  9940. /* Clear up only attention source related to slow-path */
  9941. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9942. goto unplug_error;
  9943. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9944. HC_LAINT_ENA | HC_ERINT_ENA),
  9945. phba->HCregaddr);
  9946. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9947. phba->HAregaddr);
  9948. writel(hc_copy, phba->HCregaddr);
  9949. readl(phba->HAregaddr); /* flush */
  9950. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9951. } else
  9952. ha_copy = phba->ha_copy;
  9953. work_ha_copy = ha_copy & phba->work_ha_mask;
  9954. if (work_ha_copy) {
  9955. if (work_ha_copy & HA_LATT) {
  9956. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9957. /*
  9958. * Turn off Link Attention interrupts
  9959. * until CLEAR_LA done
  9960. */
  9961. spin_lock_irqsave(&phba->hbalock, iflag);
  9962. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9963. if (lpfc_readl(phba->HCregaddr, &control))
  9964. goto unplug_error;
  9965. control &= ~HC_LAINT_ENA;
  9966. writel(control, phba->HCregaddr);
  9967. readl(phba->HCregaddr); /* flush */
  9968. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9969. }
  9970. else
  9971. work_ha_copy &= ~HA_LATT;
  9972. }
  9973. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9974. /*
  9975. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9976. * the only slow ring.
  9977. */
  9978. status = (work_ha_copy &
  9979. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9980. status >>= (4*LPFC_ELS_RING);
  9981. if (status & HA_RXMASK) {
  9982. spin_lock_irqsave(&phba->hbalock, iflag);
  9983. if (lpfc_readl(phba->HCregaddr, &control))
  9984. goto unplug_error;
  9985. lpfc_debugfs_slow_ring_trc(phba,
  9986. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9987. control, status,
  9988. (uint32_t)phba->sli.slistat.sli_intr);
  9989. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9990. lpfc_debugfs_slow_ring_trc(phba,
  9991. "ISR Disable ring:"
  9992. "pwork:x%x hawork:x%x wait:x%x",
  9993. phba->work_ha, work_ha_copy,
  9994. (uint32_t)((unsigned long)
  9995. &phba->work_waitq));
  9996. control &=
  9997. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9998. writel(control, phba->HCregaddr);
  9999. readl(phba->HCregaddr); /* flush */
  10000. }
  10001. else {
  10002. lpfc_debugfs_slow_ring_trc(phba,
  10003. "ISR slow ring: pwork:"
  10004. "x%x hawork:x%x wait:x%x",
  10005. phba->work_ha, work_ha_copy,
  10006. (uint32_t)((unsigned long)
  10007. &phba->work_waitq));
  10008. }
  10009. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10010. }
  10011. }
  10012. spin_lock_irqsave(&phba->hbalock, iflag);
  10013. if (work_ha_copy & HA_ERATT) {
  10014. if (lpfc_sli_read_hs(phba))
  10015. goto unplug_error;
  10016. /*
  10017. * Check if there is a deferred error condition
  10018. * is active
  10019. */
  10020. if ((HS_FFER1 & phba->work_hs) &&
  10021. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  10022. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  10023. phba->work_hs)) {
  10024. phba->hba_flag |= DEFER_ERATT;
  10025. /* Clear all interrupt enable conditions */
  10026. writel(0, phba->HCregaddr);
  10027. readl(phba->HCregaddr);
  10028. }
  10029. }
  10030. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  10031. pmb = phba->sli.mbox_active;
  10032. pmbox = &pmb->u.mb;
  10033. mbox = phba->mbox;
  10034. vport = pmb->vport;
  10035. /* First check out the status word */
  10036. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  10037. if (pmbox->mbxOwner != OWN_HOST) {
  10038. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10039. /*
  10040. * Stray Mailbox Interrupt, mbxCommand <cmd>
  10041. * mbxStatus <status>
  10042. */
  10043. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10044. LOG_SLI,
  10045. "(%d):0304 Stray Mailbox "
  10046. "Interrupt mbxCommand x%x "
  10047. "mbxStatus x%x\n",
  10048. (vport ? vport->vpi : 0),
  10049. pmbox->mbxCommand,
  10050. pmbox->mbxStatus);
  10051. /* clear mailbox attention bit */
  10052. work_ha_copy &= ~HA_MBATT;
  10053. } else {
  10054. phba->sli.mbox_active = NULL;
  10055. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10056. phba->last_completion_time = jiffies;
  10057. del_timer(&phba->sli.mbox_tmo);
  10058. if (pmb->mbox_cmpl) {
  10059. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  10060. MAILBOX_CMD_SIZE);
  10061. if (pmb->out_ext_byte_len &&
  10062. pmb->context2)
  10063. lpfc_sli_pcimem_bcopy(
  10064. phba->mbox_ext,
  10065. pmb->context2,
  10066. pmb->out_ext_byte_len);
  10067. }
  10068. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10069. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10070. lpfc_debugfs_disc_trc(vport,
  10071. LPFC_DISC_TRC_MBOX_VPORT,
  10072. "MBOX dflt rpi: : "
  10073. "status:x%x rpi:x%x",
  10074. (uint32_t)pmbox->mbxStatus,
  10075. pmbox->un.varWords[0], 0);
  10076. if (!pmbox->mbxStatus) {
  10077. mp = (struct lpfc_dmabuf *)
  10078. (pmb->context1);
  10079. ndlp = (struct lpfc_nodelist *)
  10080. pmb->context2;
  10081. /* Reg_LOGIN of dflt RPI was
  10082. * successful. new lets get
  10083. * rid of the RPI using the
  10084. * same mbox buffer.
  10085. */
  10086. lpfc_unreg_login(phba,
  10087. vport->vpi,
  10088. pmbox->un.varWords[0],
  10089. pmb);
  10090. pmb->mbox_cmpl =
  10091. lpfc_mbx_cmpl_dflt_rpi;
  10092. pmb->context1 = mp;
  10093. pmb->context2 = ndlp;
  10094. pmb->vport = vport;
  10095. rc = lpfc_sli_issue_mbox(phba,
  10096. pmb,
  10097. MBX_NOWAIT);
  10098. if (rc != MBX_BUSY)
  10099. lpfc_printf_log(phba,
  10100. KERN_ERR,
  10101. LOG_MBOX | LOG_SLI,
  10102. "0350 rc should have"
  10103. "been MBX_BUSY\n");
  10104. if (rc != MBX_NOT_FINISHED)
  10105. goto send_current_mbox;
  10106. }
  10107. }
  10108. spin_lock_irqsave(
  10109. &phba->pport->work_port_lock,
  10110. iflag);
  10111. phba->pport->work_port_events &=
  10112. ~WORKER_MBOX_TMO;
  10113. spin_unlock_irqrestore(
  10114. &phba->pport->work_port_lock,
  10115. iflag);
  10116. lpfc_mbox_cmpl_put(phba, pmb);
  10117. }
  10118. } else
  10119. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10120. if ((work_ha_copy & HA_MBATT) &&
  10121. (phba->sli.mbox_active == NULL)) {
  10122. send_current_mbox:
  10123. /* Process next mailbox command if there is one */
  10124. do {
  10125. rc = lpfc_sli_issue_mbox(phba, NULL,
  10126. MBX_NOWAIT);
  10127. } while (rc == MBX_NOT_FINISHED);
  10128. if (rc != MBX_SUCCESS)
  10129. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10130. LOG_SLI, "0349 rc should be "
  10131. "MBX_SUCCESS\n");
  10132. }
  10133. spin_lock_irqsave(&phba->hbalock, iflag);
  10134. phba->work_ha |= work_ha_copy;
  10135. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10136. lpfc_worker_wake_up(phba);
  10137. }
  10138. return IRQ_HANDLED;
  10139. unplug_error:
  10140. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10141. return IRQ_HANDLED;
  10142. } /* lpfc_sli_sp_intr_handler */
  10143. /**
  10144. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  10145. * @irq: Interrupt number.
  10146. * @dev_id: The device context pointer.
  10147. *
  10148. * This function is directly called from the PCI layer as an interrupt
  10149. * service routine when device with SLI-3 interface spec is enabled with
  10150. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10151. * ring event in the HBA. However, when the device is enabled with either
  10152. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10153. * device-level interrupt handler. When the PCI slot is in error recovery
  10154. * or the HBA is undergoing initialization, the interrupt handler will not
  10155. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10156. * the intrrupt context. This function is called without any lock held.
  10157. * It gets the hbalock to access and update SLI data structures.
  10158. *
  10159. * This function returns IRQ_HANDLED when interrupt is handled else it
  10160. * returns IRQ_NONE.
  10161. **/
  10162. irqreturn_t
  10163. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  10164. {
  10165. struct lpfc_hba *phba;
  10166. uint32_t ha_copy;
  10167. unsigned long status;
  10168. unsigned long iflag;
  10169. /* Get the driver's phba structure from the dev_id and
  10170. * assume the HBA is not interrupting.
  10171. */
  10172. phba = (struct lpfc_hba *) dev_id;
  10173. if (unlikely(!phba))
  10174. return IRQ_NONE;
  10175. /*
  10176. * Stuff needs to be attented to when this function is invoked as an
  10177. * individual interrupt handler in MSI-X multi-message interrupt mode
  10178. */
  10179. if (phba->intr_type == MSIX) {
  10180. /* Check device state for handling interrupt */
  10181. if (lpfc_intr_state_check(phba))
  10182. return IRQ_NONE;
  10183. /* Need to read HA REG for FCP ring and other ring events */
  10184. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  10185. return IRQ_HANDLED;
  10186. /* Clear up only attention source related to fast-path */
  10187. spin_lock_irqsave(&phba->hbalock, iflag);
  10188. /*
  10189. * If there is deferred error attention, do not check for
  10190. * any interrupt.
  10191. */
  10192. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  10193. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10194. return IRQ_NONE;
  10195. }
  10196. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  10197. phba->HAregaddr);
  10198. readl(phba->HAregaddr); /* flush */
  10199. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10200. } else
  10201. ha_copy = phba->ha_copy;
  10202. /*
  10203. * Process all events on FCP ring. Take the optimized path for FCP IO.
  10204. */
  10205. ha_copy &= ~(phba->work_ha_mask);
  10206. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  10207. status >>= (4*LPFC_FCP_RING);
  10208. if (status & HA_RXMASK)
  10209. lpfc_sli_handle_fast_ring_event(phba,
  10210. &phba->sli.ring[LPFC_FCP_RING],
  10211. status);
  10212. if (phba->cfg_multi_ring_support == 2) {
  10213. /*
  10214. * Process all events on extra ring. Take the optimized path
  10215. * for extra ring IO.
  10216. */
  10217. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  10218. status >>= (4*LPFC_EXTRA_RING);
  10219. if (status & HA_RXMASK) {
  10220. lpfc_sli_handle_fast_ring_event(phba,
  10221. &phba->sli.ring[LPFC_EXTRA_RING],
  10222. status);
  10223. }
  10224. }
  10225. return IRQ_HANDLED;
  10226. } /* lpfc_sli_fp_intr_handler */
  10227. /**
  10228. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  10229. * @irq: Interrupt number.
  10230. * @dev_id: The device context pointer.
  10231. *
  10232. * This function is the HBA device-level interrupt handler to device with
  10233. * SLI-3 interface spec, called from the PCI layer when either MSI or
  10234. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  10235. * requires driver attention. This function invokes the slow-path interrupt
  10236. * attention handling function and fast-path interrupt attention handling
  10237. * function in turn to process the relevant HBA attention events. This
  10238. * function is called without any lock held. It gets the hbalock to access
  10239. * and update SLI data structures.
  10240. *
  10241. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10242. * returns IRQ_NONE.
  10243. **/
  10244. irqreturn_t
  10245. lpfc_sli_intr_handler(int irq, void *dev_id)
  10246. {
  10247. struct lpfc_hba *phba;
  10248. irqreturn_t sp_irq_rc, fp_irq_rc;
  10249. unsigned long status1, status2;
  10250. uint32_t hc_copy;
  10251. /*
  10252. * Get the driver's phba structure from the dev_id and
  10253. * assume the HBA is not interrupting.
  10254. */
  10255. phba = (struct lpfc_hba *) dev_id;
  10256. if (unlikely(!phba))
  10257. return IRQ_NONE;
  10258. /* Check device state for handling interrupt */
  10259. if (lpfc_intr_state_check(phba))
  10260. return IRQ_NONE;
  10261. spin_lock(&phba->hbalock);
  10262. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  10263. spin_unlock(&phba->hbalock);
  10264. return IRQ_HANDLED;
  10265. }
  10266. if (unlikely(!phba->ha_copy)) {
  10267. spin_unlock(&phba->hbalock);
  10268. return IRQ_NONE;
  10269. } else if (phba->ha_copy & HA_ERATT) {
  10270. if (phba->hba_flag & HBA_ERATT_HANDLED)
  10271. /* ERATT polling has handled ERATT */
  10272. phba->ha_copy &= ~HA_ERATT;
  10273. else
  10274. /* Indicate interrupt handler handles ERATT */
  10275. phba->hba_flag |= HBA_ERATT_HANDLED;
  10276. }
  10277. /*
  10278. * If there is deferred error attention, do not check for any interrupt.
  10279. */
  10280. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  10281. spin_unlock(&phba->hbalock);
  10282. return IRQ_NONE;
  10283. }
  10284. /* Clear attention sources except link and error attentions */
  10285. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  10286. spin_unlock(&phba->hbalock);
  10287. return IRQ_HANDLED;
  10288. }
  10289. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  10290. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  10291. phba->HCregaddr);
  10292. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  10293. writel(hc_copy, phba->HCregaddr);
  10294. readl(phba->HAregaddr); /* flush */
  10295. spin_unlock(&phba->hbalock);
  10296. /*
  10297. * Invokes slow-path host attention interrupt handling as appropriate.
  10298. */
  10299. /* status of events with mailbox and link attention */
  10300. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  10301. /* status of events with ELS ring */
  10302. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  10303. status2 >>= (4*LPFC_ELS_RING);
  10304. if (status1 || (status2 & HA_RXMASK))
  10305. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  10306. else
  10307. sp_irq_rc = IRQ_NONE;
  10308. /*
  10309. * Invoke fast-path host attention interrupt handling as appropriate.
  10310. */
  10311. /* status of events with FCP ring */
  10312. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  10313. status1 >>= (4*LPFC_FCP_RING);
  10314. /* status of events with extra ring */
  10315. if (phba->cfg_multi_ring_support == 2) {
  10316. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  10317. status2 >>= (4*LPFC_EXTRA_RING);
  10318. } else
  10319. status2 = 0;
  10320. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  10321. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  10322. else
  10323. fp_irq_rc = IRQ_NONE;
  10324. /* Return device-level interrupt handling status */
  10325. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  10326. } /* lpfc_sli_intr_handler */
  10327. /**
  10328. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  10329. * @phba: pointer to lpfc hba data structure.
  10330. *
  10331. * This routine is invoked by the worker thread to process all the pending
  10332. * SLI4 FCP abort XRI events.
  10333. **/
  10334. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  10335. {
  10336. struct lpfc_cq_event *cq_event;
  10337. /* First, declare the fcp xri abort event has been handled */
  10338. spin_lock_irq(&phba->hbalock);
  10339. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  10340. spin_unlock_irq(&phba->hbalock);
  10341. /* Now, handle all the fcp xri abort events */
  10342. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  10343. /* Get the first event from the head of the event queue */
  10344. spin_lock_irq(&phba->hbalock);
  10345. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  10346. cq_event, struct lpfc_cq_event, list);
  10347. spin_unlock_irq(&phba->hbalock);
  10348. /* Notify aborted XRI for FCP work queue */
  10349. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  10350. /* Free the event processed back to the free pool */
  10351. lpfc_sli4_cq_event_release(phba, cq_event);
  10352. }
  10353. }
  10354. /**
  10355. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  10356. * @phba: pointer to lpfc hba data structure.
  10357. *
  10358. * This routine is invoked by the worker thread to process all the pending
  10359. * SLI4 els abort xri events.
  10360. **/
  10361. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  10362. {
  10363. struct lpfc_cq_event *cq_event;
  10364. /* First, declare the els xri abort event has been handled */
  10365. spin_lock_irq(&phba->hbalock);
  10366. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  10367. spin_unlock_irq(&phba->hbalock);
  10368. /* Now, handle all the els xri abort events */
  10369. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  10370. /* Get the first event from the head of the event queue */
  10371. spin_lock_irq(&phba->hbalock);
  10372. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  10373. cq_event, struct lpfc_cq_event, list);
  10374. spin_unlock_irq(&phba->hbalock);
  10375. /* Notify aborted XRI for ELS work queue */
  10376. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  10377. /* Free the event processed back to the free pool */
  10378. lpfc_sli4_cq_event_release(phba, cq_event);
  10379. }
  10380. }
  10381. /**
  10382. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  10383. * @phba: pointer to lpfc hba data structure
  10384. * @pIocbIn: pointer to the rspiocbq
  10385. * @pIocbOut: pointer to the cmdiocbq
  10386. * @wcqe: pointer to the complete wcqe
  10387. *
  10388. * This routine transfers the fields of a command iocbq to a response iocbq
  10389. * by copying all the IOCB fields from command iocbq and transferring the
  10390. * completion status information from the complete wcqe.
  10391. **/
  10392. static void
  10393. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  10394. struct lpfc_iocbq *pIocbIn,
  10395. struct lpfc_iocbq *pIocbOut,
  10396. struct lpfc_wcqe_complete *wcqe)
  10397. {
  10398. int numBdes, i;
  10399. unsigned long iflags;
  10400. uint32_t status, max_response;
  10401. struct lpfc_dmabuf *dmabuf;
  10402. struct ulp_bde64 *bpl, bde;
  10403. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  10404. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  10405. sizeof(struct lpfc_iocbq) - offset);
  10406. /* Map WCQE parameters into irspiocb parameters */
  10407. status = bf_get(lpfc_wcqe_c_status, wcqe);
  10408. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  10409. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  10410. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  10411. pIocbIn->iocb.un.fcpi.fcpi_parm =
  10412. pIocbOut->iocb.un.fcpi.fcpi_parm -
  10413. wcqe->total_data_placed;
  10414. else
  10415. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  10416. else {
  10417. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  10418. switch (pIocbOut->iocb.ulpCommand) {
  10419. case CMD_ELS_REQUEST64_CR:
  10420. dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
  10421. bpl = (struct ulp_bde64 *)dmabuf->virt;
  10422. bde.tus.w = le32_to_cpu(bpl[1].tus.w);
  10423. max_response = bde.tus.f.bdeSize;
  10424. break;
  10425. case CMD_GEN_REQUEST64_CR:
  10426. max_response = 0;
  10427. if (!pIocbOut->context3)
  10428. break;
  10429. numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
  10430. sizeof(struct ulp_bde64);
  10431. dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
  10432. bpl = (struct ulp_bde64 *)dmabuf->virt;
  10433. for (i = 0; i < numBdes; i++) {
  10434. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  10435. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  10436. max_response += bde.tus.f.bdeSize;
  10437. }
  10438. break;
  10439. default:
  10440. max_response = wcqe->total_data_placed;
  10441. break;
  10442. }
  10443. if (max_response < wcqe->total_data_placed)
  10444. pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
  10445. else
  10446. pIocbIn->iocb.un.genreq64.bdl.bdeSize =
  10447. wcqe->total_data_placed;
  10448. }
  10449. /* Convert BG errors for completion status */
  10450. if (status == CQE_STATUS_DI_ERROR) {
  10451. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  10452. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  10453. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  10454. else
  10455. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  10456. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  10457. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  10458. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10459. BGS_GUARD_ERR_MASK;
  10460. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  10461. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10462. BGS_APPTAG_ERR_MASK;
  10463. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  10464. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10465. BGS_REFTAG_ERR_MASK;
  10466. /* Check to see if there was any good data before the error */
  10467. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  10468. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10469. BGS_HI_WATER_MARK_PRESENT_MASK;
  10470. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  10471. wcqe->total_data_placed;
  10472. }
  10473. /*
  10474. * Set ALL the error bits to indicate we don't know what
  10475. * type of error it is.
  10476. */
  10477. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  10478. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10479. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  10480. BGS_GUARD_ERR_MASK);
  10481. }
  10482. /* Pick up HBA exchange busy condition */
  10483. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  10484. spin_lock_irqsave(&phba->hbalock, iflags);
  10485. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  10486. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10487. }
  10488. }
  10489. /**
  10490. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  10491. * @phba: Pointer to HBA context object.
  10492. * @wcqe: Pointer to work-queue completion queue entry.
  10493. *
  10494. * This routine handles an ELS work-queue completion event and construct
  10495. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  10496. * discovery engine to handle.
  10497. *
  10498. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  10499. **/
  10500. static struct lpfc_iocbq *
  10501. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  10502. struct lpfc_iocbq *irspiocbq)
  10503. {
  10504. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  10505. struct lpfc_iocbq *cmdiocbq;
  10506. struct lpfc_wcqe_complete *wcqe;
  10507. unsigned long iflags;
  10508. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  10509. spin_lock_irqsave(&pring->ring_lock, iflags);
  10510. pring->stats.iocb_event++;
  10511. /* Look up the ELS command IOCB and create pseudo response IOCB */
  10512. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10513. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10514. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10515. if (unlikely(!cmdiocbq)) {
  10516. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10517. "0386 ELS complete with no corresponding "
  10518. "cmdiocb: iotag (%d)\n",
  10519. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10520. lpfc_sli_release_iocbq(phba, irspiocbq);
  10521. return NULL;
  10522. }
  10523. /* Fake the irspiocbq and copy necessary response information */
  10524. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  10525. return irspiocbq;
  10526. }
  10527. /**
  10528. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  10529. * @phba: Pointer to HBA context object.
  10530. * @cqe: Pointer to mailbox completion queue entry.
  10531. *
  10532. * This routine process a mailbox completion queue entry with asynchrous
  10533. * event.
  10534. *
  10535. * Return: true if work posted to worker thread, otherwise false.
  10536. **/
  10537. static bool
  10538. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10539. {
  10540. struct lpfc_cq_event *cq_event;
  10541. unsigned long iflags;
  10542. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10543. "0392 Async Event: word0:x%x, word1:x%x, "
  10544. "word2:x%x, word3:x%x\n", mcqe->word0,
  10545. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  10546. /* Allocate a new internal CQ_EVENT entry */
  10547. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10548. if (!cq_event) {
  10549. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10550. "0394 Failed to allocate CQ_EVENT entry\n");
  10551. return false;
  10552. }
  10553. /* Move the CQE into an asynchronous event entry */
  10554. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  10555. spin_lock_irqsave(&phba->hbalock, iflags);
  10556. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  10557. /* Set the async event flag */
  10558. phba->hba_flag |= ASYNC_EVENT;
  10559. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10560. return true;
  10561. }
  10562. /**
  10563. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  10564. * @phba: Pointer to HBA context object.
  10565. * @cqe: Pointer to mailbox completion queue entry.
  10566. *
  10567. * This routine process a mailbox completion queue entry with mailbox
  10568. * completion event.
  10569. *
  10570. * Return: true if work posted to worker thread, otherwise false.
  10571. **/
  10572. static bool
  10573. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10574. {
  10575. uint32_t mcqe_status;
  10576. MAILBOX_t *mbox, *pmbox;
  10577. struct lpfc_mqe *mqe;
  10578. struct lpfc_vport *vport;
  10579. struct lpfc_nodelist *ndlp;
  10580. struct lpfc_dmabuf *mp;
  10581. unsigned long iflags;
  10582. LPFC_MBOXQ_t *pmb;
  10583. bool workposted = false;
  10584. int rc;
  10585. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  10586. if (!bf_get(lpfc_trailer_completed, mcqe))
  10587. goto out_no_mqe_complete;
  10588. /* Get the reference to the active mbox command */
  10589. spin_lock_irqsave(&phba->hbalock, iflags);
  10590. pmb = phba->sli.mbox_active;
  10591. if (unlikely(!pmb)) {
  10592. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10593. "1832 No pending MBOX command to handle\n");
  10594. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10595. goto out_no_mqe_complete;
  10596. }
  10597. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10598. mqe = &pmb->u.mqe;
  10599. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  10600. mbox = phba->mbox;
  10601. vport = pmb->vport;
  10602. /* Reset heartbeat timer */
  10603. phba->last_completion_time = jiffies;
  10604. del_timer(&phba->sli.mbox_tmo);
  10605. /* Move mbox data to caller's mailbox region, do endian swapping */
  10606. if (pmb->mbox_cmpl && mbox)
  10607. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  10608. /*
  10609. * For mcqe errors, conditionally move a modified error code to
  10610. * the mbox so that the error will not be missed.
  10611. */
  10612. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  10613. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  10614. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  10615. bf_set(lpfc_mqe_status, mqe,
  10616. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  10617. }
  10618. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10619. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10620. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  10621. "MBOX dflt rpi: status:x%x rpi:x%x",
  10622. mcqe_status,
  10623. pmbox->un.varWords[0], 0);
  10624. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  10625. mp = (struct lpfc_dmabuf *)(pmb->context1);
  10626. ndlp = (struct lpfc_nodelist *)pmb->context2;
  10627. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  10628. * RID of the PPI using the same mbox buffer.
  10629. */
  10630. lpfc_unreg_login(phba, vport->vpi,
  10631. pmbox->un.varWords[0], pmb);
  10632. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  10633. pmb->context1 = mp;
  10634. pmb->context2 = ndlp;
  10635. pmb->vport = vport;
  10636. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  10637. if (rc != MBX_BUSY)
  10638. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10639. LOG_SLI, "0385 rc should "
  10640. "have been MBX_BUSY\n");
  10641. if (rc != MBX_NOT_FINISHED)
  10642. goto send_current_mbox;
  10643. }
  10644. }
  10645. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  10646. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  10647. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  10648. /* There is mailbox completion work to do */
  10649. spin_lock_irqsave(&phba->hbalock, iflags);
  10650. __lpfc_mbox_cmpl_put(phba, pmb);
  10651. phba->work_ha |= HA_MBATT;
  10652. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10653. workposted = true;
  10654. send_current_mbox:
  10655. spin_lock_irqsave(&phba->hbalock, iflags);
  10656. /* Release the mailbox command posting token */
  10657. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10658. /* Setting active mailbox pointer need to be in sync to flag clear */
  10659. phba->sli.mbox_active = NULL;
  10660. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10661. /* Wake up worker thread to post the next pending mailbox command */
  10662. lpfc_worker_wake_up(phba);
  10663. out_no_mqe_complete:
  10664. if (bf_get(lpfc_trailer_consumed, mcqe))
  10665. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  10666. return workposted;
  10667. }
  10668. /**
  10669. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  10670. * @phba: Pointer to HBA context object.
  10671. * @cqe: Pointer to mailbox completion queue entry.
  10672. *
  10673. * This routine process a mailbox completion queue entry, it invokes the
  10674. * proper mailbox complete handling or asynchrous event handling routine
  10675. * according to the MCQE's async bit.
  10676. *
  10677. * Return: true if work posted to worker thread, otherwise false.
  10678. **/
  10679. static bool
  10680. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  10681. {
  10682. struct lpfc_mcqe mcqe;
  10683. bool workposted;
  10684. /* Copy the mailbox MCQE and convert endian order as needed */
  10685. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  10686. /* Invoke the proper event handling routine */
  10687. if (!bf_get(lpfc_trailer_async, &mcqe))
  10688. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  10689. else
  10690. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  10691. return workposted;
  10692. }
  10693. /**
  10694. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  10695. * @phba: Pointer to HBA context object.
  10696. * @cq: Pointer to associated CQ
  10697. * @wcqe: Pointer to work-queue completion queue entry.
  10698. *
  10699. * This routine handles an ELS work-queue completion event.
  10700. *
  10701. * Return: true if work posted to worker thread, otherwise false.
  10702. **/
  10703. static bool
  10704. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10705. struct lpfc_wcqe_complete *wcqe)
  10706. {
  10707. struct lpfc_iocbq *irspiocbq;
  10708. unsigned long iflags;
  10709. struct lpfc_sli_ring *pring = cq->pring;
  10710. int txq_cnt = 0;
  10711. int txcmplq_cnt = 0;
  10712. int fcp_txcmplq_cnt = 0;
  10713. /* Get an irspiocbq for later ELS response processing use */
  10714. irspiocbq = lpfc_sli_get_iocbq(phba);
  10715. if (!irspiocbq) {
  10716. if (!list_empty(&pring->txq))
  10717. txq_cnt++;
  10718. if (!list_empty(&pring->txcmplq))
  10719. txcmplq_cnt++;
  10720. if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
  10721. fcp_txcmplq_cnt++;
  10722. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10723. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10724. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10725. txq_cnt, phba->iocb_cnt,
  10726. fcp_txcmplq_cnt,
  10727. txcmplq_cnt);
  10728. return false;
  10729. }
  10730. /* Save off the slow-path queue event for work thread to process */
  10731. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10732. spin_lock_irqsave(&phba->hbalock, iflags);
  10733. list_add_tail(&irspiocbq->cq_event.list,
  10734. &phba->sli4_hba.sp_queue_event);
  10735. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10736. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10737. return true;
  10738. }
  10739. /**
  10740. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10741. * @phba: Pointer to HBA context object.
  10742. * @wcqe: Pointer to work-queue completion queue entry.
  10743. *
  10744. * This routine handles slow-path WQ entry comsumed event by invoking the
  10745. * proper WQ release routine to the slow-path WQ.
  10746. **/
  10747. static void
  10748. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10749. struct lpfc_wcqe_release *wcqe)
  10750. {
  10751. /* sanity check on queue memory */
  10752. if (unlikely(!phba->sli4_hba.els_wq))
  10753. return;
  10754. /* Check for the slow-path ELS work queue */
  10755. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10756. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10757. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10758. else
  10759. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10760. "2579 Slow-path wqe consume event carries "
  10761. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10762. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10763. phba->sli4_hba.els_wq->queue_id);
  10764. }
  10765. /**
  10766. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10767. * @phba: Pointer to HBA context object.
  10768. * @cq: Pointer to a WQ completion queue.
  10769. * @wcqe: Pointer to work-queue completion queue entry.
  10770. *
  10771. * This routine handles an XRI abort event.
  10772. *
  10773. * Return: true if work posted to worker thread, otherwise false.
  10774. **/
  10775. static bool
  10776. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10777. struct lpfc_queue *cq,
  10778. struct sli4_wcqe_xri_aborted *wcqe)
  10779. {
  10780. bool workposted = false;
  10781. struct lpfc_cq_event *cq_event;
  10782. unsigned long iflags;
  10783. /* Allocate a new internal CQ_EVENT entry */
  10784. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10785. if (!cq_event) {
  10786. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10787. "0602 Failed to allocate CQ_EVENT entry\n");
  10788. return false;
  10789. }
  10790. /* Move the CQE into the proper xri abort event list */
  10791. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10792. switch (cq->subtype) {
  10793. case LPFC_FCP:
  10794. spin_lock_irqsave(&phba->hbalock, iflags);
  10795. list_add_tail(&cq_event->list,
  10796. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10797. /* Set the fcp xri abort event flag */
  10798. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10799. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10800. workposted = true;
  10801. break;
  10802. case LPFC_ELS:
  10803. spin_lock_irqsave(&phba->hbalock, iflags);
  10804. list_add_tail(&cq_event->list,
  10805. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10806. /* Set the els xri abort event flag */
  10807. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10808. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10809. workposted = true;
  10810. break;
  10811. default:
  10812. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10813. "0603 Invalid work queue CQE subtype (x%x)\n",
  10814. cq->subtype);
  10815. workposted = false;
  10816. break;
  10817. }
  10818. return workposted;
  10819. }
  10820. /**
  10821. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10822. * @phba: Pointer to HBA context object.
  10823. * @rcqe: Pointer to receive-queue completion queue entry.
  10824. *
  10825. * This routine process a receive-queue completion queue entry.
  10826. *
  10827. * Return: true if work posted to worker thread, otherwise false.
  10828. **/
  10829. static bool
  10830. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10831. {
  10832. bool workposted = false;
  10833. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10834. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10835. struct hbq_dmabuf *dma_buf;
  10836. uint32_t status, rq_id;
  10837. unsigned long iflags;
  10838. /* sanity check on queue memory */
  10839. if (unlikely(!hrq) || unlikely(!drq))
  10840. return workposted;
  10841. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10842. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10843. else
  10844. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10845. if (rq_id != hrq->queue_id)
  10846. goto out;
  10847. status = bf_get(lpfc_rcqe_status, rcqe);
  10848. switch (status) {
  10849. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10850. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10851. "2537 Receive Frame Truncated!!\n");
  10852. hrq->RQ_buf_trunc++;
  10853. case FC_STATUS_RQ_SUCCESS:
  10854. lpfc_sli4_rq_release(hrq, drq);
  10855. spin_lock_irqsave(&phba->hbalock, iflags);
  10856. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10857. if (!dma_buf) {
  10858. hrq->RQ_no_buf_found++;
  10859. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10860. goto out;
  10861. }
  10862. hrq->RQ_rcv_buf++;
  10863. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10864. /* save off the frame for the word thread to process */
  10865. list_add_tail(&dma_buf->cq_event.list,
  10866. &phba->sli4_hba.sp_queue_event);
  10867. /* Frame received */
  10868. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10869. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10870. workposted = true;
  10871. break;
  10872. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10873. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10874. hrq->RQ_no_posted_buf++;
  10875. /* Post more buffers if possible */
  10876. spin_lock_irqsave(&phba->hbalock, iflags);
  10877. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10878. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10879. workposted = true;
  10880. break;
  10881. }
  10882. out:
  10883. return workposted;
  10884. }
  10885. /**
  10886. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10887. * @phba: Pointer to HBA context object.
  10888. * @cq: Pointer to the completion queue.
  10889. * @wcqe: Pointer to a completion queue entry.
  10890. *
  10891. * This routine process a slow-path work-queue or receive queue completion queue
  10892. * entry.
  10893. *
  10894. * Return: true if work posted to worker thread, otherwise false.
  10895. **/
  10896. static bool
  10897. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10898. struct lpfc_cqe *cqe)
  10899. {
  10900. struct lpfc_cqe cqevt;
  10901. bool workposted = false;
  10902. /* Copy the work queue CQE and convert endian order if needed */
  10903. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10904. /* Check and process for different type of WCQE and dispatch */
  10905. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10906. case CQE_CODE_COMPL_WQE:
  10907. /* Process the WQ/RQ complete event */
  10908. phba->last_completion_time = jiffies;
  10909. workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
  10910. (struct lpfc_wcqe_complete *)&cqevt);
  10911. break;
  10912. case CQE_CODE_RELEASE_WQE:
  10913. /* Process the WQ release event */
  10914. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10915. (struct lpfc_wcqe_release *)&cqevt);
  10916. break;
  10917. case CQE_CODE_XRI_ABORTED:
  10918. /* Process the WQ XRI abort event */
  10919. phba->last_completion_time = jiffies;
  10920. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10921. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10922. break;
  10923. case CQE_CODE_RECEIVE:
  10924. case CQE_CODE_RECEIVE_V1:
  10925. /* Process the RQ event */
  10926. phba->last_completion_time = jiffies;
  10927. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10928. (struct lpfc_rcqe *)&cqevt);
  10929. break;
  10930. default:
  10931. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10932. "0388 Not a valid WCQE code: x%x\n",
  10933. bf_get(lpfc_cqe_code, &cqevt));
  10934. break;
  10935. }
  10936. return workposted;
  10937. }
  10938. /**
  10939. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10940. * @phba: Pointer to HBA context object.
  10941. * @eqe: Pointer to fast-path event queue entry.
  10942. *
  10943. * This routine process a event queue entry from the slow-path event queue.
  10944. * It will check the MajorCode and MinorCode to determine this is for a
  10945. * completion event on a completion queue, if not, an error shall be logged
  10946. * and just return. Otherwise, it will get to the corresponding completion
  10947. * queue and process all the entries on that completion queue, rearm the
  10948. * completion queue, and then return.
  10949. *
  10950. **/
  10951. static void
  10952. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10953. struct lpfc_queue *speq)
  10954. {
  10955. struct lpfc_queue *cq = NULL, *childq;
  10956. struct lpfc_cqe *cqe;
  10957. bool workposted = false;
  10958. int ecount = 0;
  10959. uint16_t cqid;
  10960. /* Get the reference to the corresponding CQ */
  10961. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10962. list_for_each_entry(childq, &speq->child_list, list) {
  10963. if (childq->queue_id == cqid) {
  10964. cq = childq;
  10965. break;
  10966. }
  10967. }
  10968. if (unlikely(!cq)) {
  10969. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10970. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10971. "0365 Slow-path CQ identifier "
  10972. "(%d) does not exist\n", cqid);
  10973. return;
  10974. }
  10975. /* Process all the entries to the CQ */
  10976. switch (cq->type) {
  10977. case LPFC_MCQ:
  10978. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10979. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10980. if (!(++ecount % cq->entry_repost))
  10981. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10982. cq->CQ_mbox++;
  10983. }
  10984. break;
  10985. case LPFC_WCQ:
  10986. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10987. if (cq->subtype == LPFC_FCP)
  10988. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10989. cqe);
  10990. else
  10991. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10992. cqe);
  10993. if (!(++ecount % cq->entry_repost))
  10994. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10995. }
  10996. /* Track the max number of CQEs processed in 1 EQ */
  10997. if (ecount > cq->CQ_max_cqe)
  10998. cq->CQ_max_cqe = ecount;
  10999. break;
  11000. default:
  11001. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11002. "0370 Invalid completion queue type (%d)\n",
  11003. cq->type);
  11004. return;
  11005. }
  11006. /* Catch the no cq entry condition, log an error */
  11007. if (unlikely(ecount == 0))
  11008. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11009. "0371 No entry from the CQ: identifier "
  11010. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  11011. /* In any case, flash and re-arm the RCQ */
  11012. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  11013. /* wake up worker thread if there are works to be done */
  11014. if (workposted)
  11015. lpfc_worker_wake_up(phba);
  11016. }
  11017. /**
  11018. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  11019. * @phba: Pointer to HBA context object.
  11020. * @cq: Pointer to associated CQ
  11021. * @wcqe: Pointer to work-queue completion queue entry.
  11022. *
  11023. * This routine process a fast-path work queue completion entry from fast-path
  11024. * event queue for FCP command response completion.
  11025. **/
  11026. static void
  11027. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11028. struct lpfc_wcqe_complete *wcqe)
  11029. {
  11030. struct lpfc_sli_ring *pring = cq->pring;
  11031. struct lpfc_iocbq *cmdiocbq;
  11032. struct lpfc_iocbq irspiocbq;
  11033. unsigned long iflags;
  11034. /* Check for response status */
  11035. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  11036. /* If resource errors reported from HBA, reduce queue
  11037. * depth of the SCSI device.
  11038. */
  11039. if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
  11040. IOSTAT_LOCAL_REJECT)) &&
  11041. ((wcqe->parameter & IOERR_PARAM_MASK) ==
  11042. IOERR_NO_RESOURCES))
  11043. phba->lpfc_rampdown_queue_depth(phba);
  11044. /* Log the error status */
  11045. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11046. "0373 FCP complete error: status=x%x, "
  11047. "hw_status=x%x, total_data_specified=%d, "
  11048. "parameter=x%x, word3=x%x\n",
  11049. bf_get(lpfc_wcqe_c_status, wcqe),
  11050. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  11051. wcqe->total_data_placed, wcqe->parameter,
  11052. wcqe->word3);
  11053. }
  11054. /* Look up the FCP command IOCB and create pseudo response IOCB */
  11055. spin_lock_irqsave(&pring->ring_lock, iflags);
  11056. pring->stats.iocb_event++;
  11057. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  11058. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  11059. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  11060. if (unlikely(!cmdiocbq)) {
  11061. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11062. "0374 FCP complete with no corresponding "
  11063. "cmdiocb: iotag (%d)\n",
  11064. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  11065. return;
  11066. }
  11067. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  11068. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11069. "0375 FCP cmdiocb not callback function "
  11070. "iotag: (%d)\n",
  11071. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  11072. return;
  11073. }
  11074. /* Fake the irspiocb and copy necessary response information */
  11075. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  11076. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  11077. spin_lock_irqsave(&phba->hbalock, iflags);
  11078. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  11079. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11080. }
  11081. /* Pass the cmd_iocb and the rsp state to the upper layer */
  11082. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  11083. }
  11084. /**
  11085. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  11086. * @phba: Pointer to HBA context object.
  11087. * @cq: Pointer to completion queue.
  11088. * @wcqe: Pointer to work-queue completion queue entry.
  11089. *
  11090. * This routine handles an fast-path WQ entry comsumed event by invoking the
  11091. * proper WQ release routine to the slow-path WQ.
  11092. **/
  11093. static void
  11094. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11095. struct lpfc_wcqe_release *wcqe)
  11096. {
  11097. struct lpfc_queue *childwq;
  11098. bool wqid_matched = false;
  11099. uint16_t fcp_wqid;
  11100. /* Check for fast-path FCP work queue release */
  11101. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  11102. list_for_each_entry(childwq, &cq->child_list, list) {
  11103. if (childwq->queue_id == fcp_wqid) {
  11104. lpfc_sli4_wq_release(childwq,
  11105. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  11106. wqid_matched = true;
  11107. break;
  11108. }
  11109. }
  11110. /* Report warning log message if no match found */
  11111. if (wqid_matched != true)
  11112. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11113. "2580 Fast-path wqe consume event carries "
  11114. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  11115. }
  11116. /**
  11117. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  11118. * @cq: Pointer to the completion queue.
  11119. * @eqe: Pointer to fast-path completion queue entry.
  11120. *
  11121. * This routine process a fast-path work queue completion entry from fast-path
  11122. * event queue for FCP command response completion.
  11123. **/
  11124. static int
  11125. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11126. struct lpfc_cqe *cqe)
  11127. {
  11128. struct lpfc_wcqe_release wcqe;
  11129. bool workposted = false;
  11130. /* Copy the work queue CQE and convert endian order if needed */
  11131. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  11132. /* Check and process for different type of WCQE and dispatch */
  11133. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  11134. case CQE_CODE_COMPL_WQE:
  11135. cq->CQ_wq++;
  11136. /* Process the WQ complete event */
  11137. phba->last_completion_time = jiffies;
  11138. lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
  11139. (struct lpfc_wcqe_complete *)&wcqe);
  11140. break;
  11141. case CQE_CODE_RELEASE_WQE:
  11142. cq->CQ_release_wqe++;
  11143. /* Process the WQ release event */
  11144. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  11145. (struct lpfc_wcqe_release *)&wcqe);
  11146. break;
  11147. case CQE_CODE_XRI_ABORTED:
  11148. cq->CQ_xri_aborted++;
  11149. /* Process the WQ XRI abort event */
  11150. phba->last_completion_time = jiffies;
  11151. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  11152. (struct sli4_wcqe_xri_aborted *)&wcqe);
  11153. break;
  11154. default:
  11155. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11156. "0144 Not a valid WCQE code: x%x\n",
  11157. bf_get(lpfc_wcqe_c_code, &wcqe));
  11158. break;
  11159. }
  11160. return workposted;
  11161. }
  11162. /**
  11163. * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
  11164. * @phba: Pointer to HBA context object.
  11165. * @eqe: Pointer to fast-path event queue entry.
  11166. *
  11167. * This routine process a event queue entry from the fast-path event queue.
  11168. * It will check the MajorCode and MinorCode to determine this is for a
  11169. * completion event on a completion queue, if not, an error shall be logged
  11170. * and just return. Otherwise, it will get to the corresponding completion
  11171. * queue and process all the entries on the completion queue, rearm the
  11172. * completion queue, and then return.
  11173. **/
  11174. static void
  11175. lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  11176. uint32_t qidx)
  11177. {
  11178. struct lpfc_queue *cq;
  11179. struct lpfc_cqe *cqe;
  11180. bool workposted = false;
  11181. uint16_t cqid;
  11182. int ecount = 0;
  11183. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  11184. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11185. "0366 Not a valid completion "
  11186. "event: majorcode=x%x, minorcode=x%x\n",
  11187. bf_get_le32(lpfc_eqe_major_code, eqe),
  11188. bf_get_le32(lpfc_eqe_minor_code, eqe));
  11189. return;
  11190. }
  11191. /* Get the reference to the corresponding CQ */
  11192. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  11193. /* Check if this is a Slow path event */
  11194. if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
  11195. lpfc_sli4_sp_handle_eqe(phba, eqe,
  11196. phba->sli4_hba.hba_eq[qidx]);
  11197. return;
  11198. }
  11199. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  11200. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11201. "3146 Fast-path completion queues "
  11202. "does not exist\n");
  11203. return;
  11204. }
  11205. cq = phba->sli4_hba.fcp_cq[qidx];
  11206. if (unlikely(!cq)) {
  11207. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  11208. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11209. "0367 Fast-path completion queue "
  11210. "(%d) does not exist\n", qidx);
  11211. return;
  11212. }
  11213. if (unlikely(cqid != cq->queue_id)) {
  11214. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11215. "0368 Miss-matched fast-path completion "
  11216. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  11217. cqid, cq->queue_id);
  11218. return;
  11219. }
  11220. /* Process all the entries to the CQ */
  11221. while ((cqe = lpfc_sli4_cq_get(cq))) {
  11222. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  11223. if (!(++ecount % cq->entry_repost))
  11224. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  11225. }
  11226. /* Track the max number of CQEs processed in 1 EQ */
  11227. if (ecount > cq->CQ_max_cqe)
  11228. cq->CQ_max_cqe = ecount;
  11229. /* Catch the no cq entry condition */
  11230. if (unlikely(ecount == 0))
  11231. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11232. "0369 No entry from fast-path completion "
  11233. "queue fcpcqid=%d\n", cq->queue_id);
  11234. /* In any case, flash and re-arm the CQ */
  11235. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  11236. /* wake up worker thread if there are works to be done */
  11237. if (workposted)
  11238. lpfc_worker_wake_up(phba);
  11239. }
  11240. static void
  11241. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11242. {
  11243. struct lpfc_eqe *eqe;
  11244. /* walk all the EQ entries and drop on the floor */
  11245. while ((eqe = lpfc_sli4_eq_get(eq)))
  11246. ;
  11247. /* Clear and re-arm the EQ */
  11248. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  11249. }
  11250. /**
  11251. * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
  11252. * entry
  11253. * @phba: Pointer to HBA context object.
  11254. * @eqe: Pointer to fast-path event queue entry.
  11255. *
  11256. * This routine process a event queue entry from the Flash Optimized Fabric
  11257. * event queue. It will check the MajorCode and MinorCode to determine this
  11258. * is for a completion event on a completion queue, if not, an error shall be
  11259. * logged and just return. Otherwise, it will get to the corresponding
  11260. * completion queue and process all the entries on the completion queue, rearm
  11261. * the completion queue, and then return.
  11262. **/
  11263. static void
  11264. lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  11265. {
  11266. struct lpfc_queue *cq;
  11267. struct lpfc_cqe *cqe;
  11268. bool workposted = false;
  11269. uint16_t cqid;
  11270. int ecount = 0;
  11271. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  11272. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11273. "9147 Not a valid completion "
  11274. "event: majorcode=x%x, minorcode=x%x\n",
  11275. bf_get_le32(lpfc_eqe_major_code, eqe),
  11276. bf_get_le32(lpfc_eqe_minor_code, eqe));
  11277. return;
  11278. }
  11279. /* Get the reference to the corresponding CQ */
  11280. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  11281. /* Next check for OAS */
  11282. cq = phba->sli4_hba.oas_cq;
  11283. if (unlikely(!cq)) {
  11284. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  11285. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11286. "9148 OAS completion queue "
  11287. "does not exist\n");
  11288. return;
  11289. }
  11290. if (unlikely(cqid != cq->queue_id)) {
  11291. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11292. "9149 Miss-matched fast-path compl "
  11293. "queue id: eqcqid=%d, fcpcqid=%d\n",
  11294. cqid, cq->queue_id);
  11295. return;
  11296. }
  11297. /* Process all the entries to the OAS CQ */
  11298. while ((cqe = lpfc_sli4_cq_get(cq))) {
  11299. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  11300. if (!(++ecount % cq->entry_repost))
  11301. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  11302. }
  11303. /* Track the max number of CQEs processed in 1 EQ */
  11304. if (ecount > cq->CQ_max_cqe)
  11305. cq->CQ_max_cqe = ecount;
  11306. /* Catch the no cq entry condition */
  11307. if (unlikely(ecount == 0))
  11308. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11309. "9153 No entry from fast-path completion "
  11310. "queue fcpcqid=%d\n", cq->queue_id);
  11311. /* In any case, flash and re-arm the CQ */
  11312. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  11313. /* wake up worker thread if there are works to be done */
  11314. if (workposted)
  11315. lpfc_worker_wake_up(phba);
  11316. }
  11317. /**
  11318. * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
  11319. * @irq: Interrupt number.
  11320. * @dev_id: The device context pointer.
  11321. *
  11322. * This function is directly called from the PCI layer as an interrupt
  11323. * service routine when device with SLI-4 interface spec is enabled with
  11324. * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
  11325. * IOCB ring event in the HBA. However, when the device is enabled with either
  11326. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  11327. * device-level interrupt handler. When the PCI slot is in error recovery
  11328. * or the HBA is undergoing initialization, the interrupt handler will not
  11329. * process the interrupt. The Flash Optimized Fabric ring event are handled in
  11330. * the intrrupt context. This function is called without any lock held.
  11331. * It gets the hbalock to access and update SLI data structures. Note that,
  11332. * the EQ to CQ are one-to-one map such that the EQ index is
  11333. * equal to that of CQ index.
  11334. *
  11335. * This function returns IRQ_HANDLED when interrupt is handled else it
  11336. * returns IRQ_NONE.
  11337. **/
  11338. irqreturn_t
  11339. lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
  11340. {
  11341. struct lpfc_hba *phba;
  11342. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  11343. struct lpfc_queue *eq;
  11344. struct lpfc_eqe *eqe;
  11345. unsigned long iflag;
  11346. int ecount = 0;
  11347. uint32_t eqidx;
  11348. /* Get the driver's phba structure from the dev_id */
  11349. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  11350. phba = fcp_eq_hdl->phba;
  11351. eqidx = fcp_eq_hdl->idx;
  11352. if (unlikely(!phba))
  11353. return IRQ_NONE;
  11354. /* Get to the EQ struct associated with this vector */
  11355. eq = phba->sli4_hba.fof_eq;
  11356. if (unlikely(!eq))
  11357. return IRQ_NONE;
  11358. /* Check device state for handling interrupt */
  11359. if (unlikely(lpfc_intr_state_check(phba))) {
  11360. eq->EQ_badstate++;
  11361. /* Check again for link_state with lock held */
  11362. spin_lock_irqsave(&phba->hbalock, iflag);
  11363. if (phba->link_state < LPFC_LINK_DOWN)
  11364. /* Flush, clear interrupt, and rearm the EQ */
  11365. lpfc_sli4_eq_flush(phba, eq);
  11366. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11367. return IRQ_NONE;
  11368. }
  11369. /*
  11370. * Process all the event on FCP fast-path EQ
  11371. */
  11372. while ((eqe = lpfc_sli4_eq_get(eq))) {
  11373. lpfc_sli4_fof_handle_eqe(phba, eqe);
  11374. if (!(++ecount % eq->entry_repost))
  11375. lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
  11376. eq->EQ_processed++;
  11377. }
  11378. /* Track the max number of EQEs processed in 1 intr */
  11379. if (ecount > eq->EQ_max_eqe)
  11380. eq->EQ_max_eqe = ecount;
  11381. if (unlikely(ecount == 0)) {
  11382. eq->EQ_no_entry++;
  11383. if (phba->intr_type == MSIX)
  11384. /* MSI-X treated interrupt served as no EQ share INT */
  11385. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11386. "9145 MSI-X interrupt with no EQE\n");
  11387. else {
  11388. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11389. "9146 ISR interrupt with no EQE\n");
  11390. /* Non MSI-X treated on interrupt as EQ share INT */
  11391. return IRQ_NONE;
  11392. }
  11393. }
  11394. /* Always clear and re-arm the fast-path EQ */
  11395. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  11396. return IRQ_HANDLED;
  11397. }
  11398. /**
  11399. * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
  11400. * @irq: Interrupt number.
  11401. * @dev_id: The device context pointer.
  11402. *
  11403. * This function is directly called from the PCI layer as an interrupt
  11404. * service routine when device with SLI-4 interface spec is enabled with
  11405. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  11406. * ring event in the HBA. However, when the device is enabled with either
  11407. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  11408. * device-level interrupt handler. When the PCI slot is in error recovery
  11409. * or the HBA is undergoing initialization, the interrupt handler will not
  11410. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  11411. * the intrrupt context. This function is called without any lock held.
  11412. * It gets the hbalock to access and update SLI data structures. Note that,
  11413. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  11414. * equal to that of FCP CQ index.
  11415. *
  11416. * The link attention and ELS ring attention events are handled
  11417. * by the worker thread. The interrupt handler signals the worker thread
  11418. * and returns for these events. This function is called without any lock
  11419. * held. It gets the hbalock to access and update SLI data structures.
  11420. *
  11421. * This function returns IRQ_HANDLED when interrupt is handled else it
  11422. * returns IRQ_NONE.
  11423. **/
  11424. irqreturn_t
  11425. lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
  11426. {
  11427. struct lpfc_hba *phba;
  11428. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  11429. struct lpfc_queue *fpeq;
  11430. struct lpfc_eqe *eqe;
  11431. unsigned long iflag;
  11432. int ecount = 0;
  11433. int fcp_eqidx;
  11434. /* Get the driver's phba structure from the dev_id */
  11435. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  11436. phba = fcp_eq_hdl->phba;
  11437. fcp_eqidx = fcp_eq_hdl->idx;
  11438. if (unlikely(!phba))
  11439. return IRQ_NONE;
  11440. if (unlikely(!phba->sli4_hba.hba_eq))
  11441. return IRQ_NONE;
  11442. /* Get to the EQ struct associated with this vector */
  11443. fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
  11444. if (unlikely(!fpeq))
  11445. return IRQ_NONE;
  11446. if (lpfc_fcp_look_ahead) {
  11447. if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
  11448. lpfc_sli4_eq_clr_intr(fpeq);
  11449. else {
  11450. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11451. return IRQ_NONE;
  11452. }
  11453. }
  11454. /* Check device state for handling interrupt */
  11455. if (unlikely(lpfc_intr_state_check(phba))) {
  11456. fpeq->EQ_badstate++;
  11457. /* Check again for link_state with lock held */
  11458. spin_lock_irqsave(&phba->hbalock, iflag);
  11459. if (phba->link_state < LPFC_LINK_DOWN)
  11460. /* Flush, clear interrupt, and rearm the EQ */
  11461. lpfc_sli4_eq_flush(phba, fpeq);
  11462. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11463. if (lpfc_fcp_look_ahead)
  11464. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11465. return IRQ_NONE;
  11466. }
  11467. /*
  11468. * Process all the event on FCP fast-path EQ
  11469. */
  11470. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  11471. if (eqe == NULL)
  11472. break;
  11473. lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
  11474. if (!(++ecount % fpeq->entry_repost))
  11475. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  11476. fpeq->EQ_processed++;
  11477. }
  11478. /* Track the max number of EQEs processed in 1 intr */
  11479. if (ecount > fpeq->EQ_max_eqe)
  11480. fpeq->EQ_max_eqe = ecount;
  11481. /* Always clear and re-arm the fast-path EQ */
  11482. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  11483. if (unlikely(ecount == 0)) {
  11484. fpeq->EQ_no_entry++;
  11485. if (lpfc_fcp_look_ahead) {
  11486. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11487. return IRQ_NONE;
  11488. }
  11489. if (phba->intr_type == MSIX)
  11490. /* MSI-X treated interrupt served as no EQ share INT */
  11491. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11492. "0358 MSI-X interrupt with no EQE\n");
  11493. else
  11494. /* Non MSI-X treated on interrupt as EQ share INT */
  11495. return IRQ_NONE;
  11496. }
  11497. if (lpfc_fcp_look_ahead)
  11498. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11499. return IRQ_HANDLED;
  11500. } /* lpfc_sli4_fp_intr_handler */
  11501. /**
  11502. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  11503. * @irq: Interrupt number.
  11504. * @dev_id: The device context pointer.
  11505. *
  11506. * This function is the device-level interrupt handler to device with SLI-4
  11507. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  11508. * interrupt mode is enabled and there is an event in the HBA which requires
  11509. * driver attention. This function invokes the slow-path interrupt attention
  11510. * handling function and fast-path interrupt attention handling function in
  11511. * turn to process the relevant HBA attention events. This function is called
  11512. * without any lock held. It gets the hbalock to access and update SLI data
  11513. * structures.
  11514. *
  11515. * This function returns IRQ_HANDLED when interrupt is handled, else it
  11516. * returns IRQ_NONE.
  11517. **/
  11518. irqreturn_t
  11519. lpfc_sli4_intr_handler(int irq, void *dev_id)
  11520. {
  11521. struct lpfc_hba *phba;
  11522. irqreturn_t hba_irq_rc;
  11523. bool hba_handled = false;
  11524. int fcp_eqidx;
  11525. /* Get the driver's phba structure from the dev_id */
  11526. phba = (struct lpfc_hba *)dev_id;
  11527. if (unlikely(!phba))
  11528. return IRQ_NONE;
  11529. /*
  11530. * Invoke fast-path host attention interrupt handling as appropriate.
  11531. */
  11532. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
  11533. hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
  11534. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  11535. if (hba_irq_rc == IRQ_HANDLED)
  11536. hba_handled |= true;
  11537. }
  11538. if (phba->cfg_fof) {
  11539. hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
  11540. &phba->sli4_hba.fcp_eq_hdl[0]);
  11541. if (hba_irq_rc == IRQ_HANDLED)
  11542. hba_handled |= true;
  11543. }
  11544. return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
  11545. } /* lpfc_sli4_intr_handler */
  11546. /**
  11547. * lpfc_sli4_queue_free - free a queue structure and associated memory
  11548. * @queue: The queue structure to free.
  11549. *
  11550. * This function frees a queue structure and the DMAable memory used for
  11551. * the host resident queue. This function must be called after destroying the
  11552. * queue on the HBA.
  11553. **/
  11554. void
  11555. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  11556. {
  11557. struct lpfc_dmabuf *dmabuf;
  11558. if (!queue)
  11559. return;
  11560. while (!list_empty(&queue->page_list)) {
  11561. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  11562. list);
  11563. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  11564. dmabuf->virt, dmabuf->phys);
  11565. kfree(dmabuf);
  11566. }
  11567. kfree(queue);
  11568. return;
  11569. }
  11570. /**
  11571. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  11572. * @phba: The HBA that this queue is being created on.
  11573. * @entry_size: The size of each queue entry for this queue.
  11574. * @entry count: The number of entries that this queue will handle.
  11575. *
  11576. * This function allocates a queue structure and the DMAable memory used for
  11577. * the host resident queue. This function must be called before creating the
  11578. * queue on the HBA.
  11579. **/
  11580. struct lpfc_queue *
  11581. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  11582. uint32_t entry_count)
  11583. {
  11584. struct lpfc_queue *queue;
  11585. struct lpfc_dmabuf *dmabuf;
  11586. int x, total_qe_count;
  11587. void *dma_pointer;
  11588. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11589. if (!phba->sli4_hba.pc_sli4_params.supported)
  11590. hw_page_size = SLI4_PAGE_SIZE;
  11591. queue = kzalloc(sizeof(struct lpfc_queue) +
  11592. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  11593. if (!queue)
  11594. return NULL;
  11595. queue->page_count = (ALIGN(entry_size * entry_count,
  11596. hw_page_size))/hw_page_size;
  11597. INIT_LIST_HEAD(&queue->list);
  11598. INIT_LIST_HEAD(&queue->page_list);
  11599. INIT_LIST_HEAD(&queue->child_list);
  11600. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  11601. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  11602. if (!dmabuf)
  11603. goto out_fail;
  11604. dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
  11605. hw_page_size, &dmabuf->phys,
  11606. GFP_KERNEL);
  11607. if (!dmabuf->virt) {
  11608. kfree(dmabuf);
  11609. goto out_fail;
  11610. }
  11611. dmabuf->buffer_tag = x;
  11612. list_add_tail(&dmabuf->list, &queue->page_list);
  11613. /* initialize queue's entry array */
  11614. dma_pointer = dmabuf->virt;
  11615. for (; total_qe_count < entry_count &&
  11616. dma_pointer < (hw_page_size + dmabuf->virt);
  11617. total_qe_count++, dma_pointer += entry_size) {
  11618. queue->qe[total_qe_count].address = dma_pointer;
  11619. }
  11620. }
  11621. queue->entry_size = entry_size;
  11622. queue->entry_count = entry_count;
  11623. /*
  11624. * entry_repost is calculated based on the number of entries in the
  11625. * queue. This works out except for RQs. If buffers are NOT initially
  11626. * posted for every RQE, entry_repost should be adjusted accordingly.
  11627. */
  11628. queue->entry_repost = (entry_count >> 3);
  11629. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  11630. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  11631. queue->phba = phba;
  11632. return queue;
  11633. out_fail:
  11634. lpfc_sli4_queue_free(queue);
  11635. return NULL;
  11636. }
  11637. /**
  11638. * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
  11639. * @phba: HBA structure that indicates port to create a queue on.
  11640. * @pci_barset: PCI BAR set flag.
  11641. *
  11642. * This function shall perform iomap of the specified PCI BAR address to host
  11643. * memory address if not already done so and return it. The returned host
  11644. * memory address can be NULL.
  11645. */
  11646. static void __iomem *
  11647. lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
  11648. {
  11649. struct pci_dev *pdev;
  11650. if (!phba->pcidev)
  11651. return NULL;
  11652. else
  11653. pdev = phba->pcidev;
  11654. switch (pci_barset) {
  11655. case WQ_PCI_BAR_0_AND_1:
  11656. return phba->pci_bar0_memmap_p;
  11657. case WQ_PCI_BAR_2_AND_3:
  11658. return phba->pci_bar2_memmap_p;
  11659. case WQ_PCI_BAR_4_AND_5:
  11660. return phba->pci_bar4_memmap_p;
  11661. default:
  11662. break;
  11663. }
  11664. return NULL;
  11665. }
  11666. /**
  11667. * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
  11668. * @phba: HBA structure that indicates port to create a queue on.
  11669. * @startq: The starting FCP EQ to modify
  11670. *
  11671. * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
  11672. *
  11673. * The @phba struct is used to send mailbox command to HBA. The @startq
  11674. * is used to get the starting FCP EQ to change.
  11675. * This function is asynchronous and will wait for the mailbox
  11676. * command to finish before continuing.
  11677. *
  11678. * On success this function will return a zero. If unable to allocate enough
  11679. * memory this function will return -ENOMEM. If the queue create mailbox command
  11680. * fails this function will return -ENXIO.
  11681. **/
  11682. int
  11683. lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
  11684. {
  11685. struct lpfc_mbx_modify_eq_delay *eq_delay;
  11686. LPFC_MBOXQ_t *mbox;
  11687. struct lpfc_queue *eq;
  11688. int cnt, rc, length, status = 0;
  11689. uint32_t shdr_status, shdr_add_status;
  11690. uint32_t result;
  11691. int fcp_eqidx;
  11692. union lpfc_sli4_cfg_shdr *shdr;
  11693. uint16_t dmult;
  11694. if (startq >= phba->cfg_fcp_io_channel)
  11695. return 0;
  11696. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11697. if (!mbox)
  11698. return -ENOMEM;
  11699. length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
  11700. sizeof(struct lpfc_sli4_cfg_mhdr));
  11701. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11702. LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
  11703. length, LPFC_SLI4_MBX_EMBED);
  11704. eq_delay = &mbox->u.mqe.un.eq_delay;
  11705. /* Calculate delay multiper from maximum interrupt per second */
  11706. result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
  11707. if (result > LPFC_DMULT_CONST)
  11708. dmult = 0;
  11709. else
  11710. dmult = LPFC_DMULT_CONST/result - 1;
  11711. cnt = 0;
  11712. for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
  11713. fcp_eqidx++) {
  11714. eq = phba->sli4_hba.hba_eq[fcp_eqidx];
  11715. if (!eq)
  11716. continue;
  11717. eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
  11718. eq_delay->u.request.eq[cnt].phase = 0;
  11719. eq_delay->u.request.eq[cnt].delay_multi = dmult;
  11720. cnt++;
  11721. if (cnt >= LPFC_MAX_EQ_DELAY)
  11722. break;
  11723. }
  11724. eq_delay->u.request.num_eq = cnt;
  11725. mbox->vport = phba->pport;
  11726. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11727. mbox->context1 = NULL;
  11728. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11729. shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
  11730. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11731. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11732. if (shdr_status || shdr_add_status || rc) {
  11733. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11734. "2512 MODIFY_EQ_DELAY mailbox failed with "
  11735. "status x%x add_status x%x, mbx status x%x\n",
  11736. shdr_status, shdr_add_status, rc);
  11737. status = -ENXIO;
  11738. }
  11739. mempool_free(mbox, phba->mbox_mem_pool);
  11740. return status;
  11741. }
  11742. /**
  11743. * lpfc_eq_create - Create an Event Queue on the HBA
  11744. * @phba: HBA structure that indicates port to create a queue on.
  11745. * @eq: The queue structure to use to create the event queue.
  11746. * @imax: The maximum interrupt per second limit.
  11747. *
  11748. * This function creates an event queue, as detailed in @eq, on a port,
  11749. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  11750. *
  11751. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  11752. * is used to get the entry count and entry size that are necessary to
  11753. * determine the number of pages to allocate and use for this queue. This
  11754. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  11755. * event queue. This function is asynchronous and will wait for the mailbox
  11756. * command to finish before continuing.
  11757. *
  11758. * On success this function will return a zero. If unable to allocate enough
  11759. * memory this function will return -ENOMEM. If the queue create mailbox command
  11760. * fails this function will return -ENXIO.
  11761. **/
  11762. int
  11763. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
  11764. {
  11765. struct lpfc_mbx_eq_create *eq_create;
  11766. LPFC_MBOXQ_t *mbox;
  11767. int rc, length, status = 0;
  11768. struct lpfc_dmabuf *dmabuf;
  11769. uint32_t shdr_status, shdr_add_status;
  11770. union lpfc_sli4_cfg_shdr *shdr;
  11771. uint16_t dmult;
  11772. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11773. /* sanity check on queue memory */
  11774. if (!eq)
  11775. return -ENODEV;
  11776. if (!phba->sli4_hba.pc_sli4_params.supported)
  11777. hw_page_size = SLI4_PAGE_SIZE;
  11778. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11779. if (!mbox)
  11780. return -ENOMEM;
  11781. length = (sizeof(struct lpfc_mbx_eq_create) -
  11782. sizeof(struct lpfc_sli4_cfg_mhdr));
  11783. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11784. LPFC_MBOX_OPCODE_EQ_CREATE,
  11785. length, LPFC_SLI4_MBX_EMBED);
  11786. eq_create = &mbox->u.mqe.un.eq_create;
  11787. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  11788. eq->page_count);
  11789. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  11790. LPFC_EQE_SIZE);
  11791. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  11792. /* Calculate delay multiper from maximum interrupt per second */
  11793. if (imax > LPFC_DMULT_CONST)
  11794. dmult = 0;
  11795. else
  11796. dmult = LPFC_DMULT_CONST/imax - 1;
  11797. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  11798. dmult);
  11799. switch (eq->entry_count) {
  11800. default:
  11801. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11802. "0360 Unsupported EQ count. (%d)\n",
  11803. eq->entry_count);
  11804. if (eq->entry_count < 256)
  11805. return -EINVAL;
  11806. /* otherwise default to smallest count (drop through) */
  11807. case 256:
  11808. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11809. LPFC_EQ_CNT_256);
  11810. break;
  11811. case 512:
  11812. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11813. LPFC_EQ_CNT_512);
  11814. break;
  11815. case 1024:
  11816. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11817. LPFC_EQ_CNT_1024);
  11818. break;
  11819. case 2048:
  11820. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11821. LPFC_EQ_CNT_2048);
  11822. break;
  11823. case 4096:
  11824. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11825. LPFC_EQ_CNT_4096);
  11826. break;
  11827. }
  11828. list_for_each_entry(dmabuf, &eq->page_list, list) {
  11829. memset(dmabuf->virt, 0, hw_page_size);
  11830. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11831. putPaddrLow(dmabuf->phys);
  11832. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11833. putPaddrHigh(dmabuf->phys);
  11834. }
  11835. mbox->vport = phba->pport;
  11836. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11837. mbox->context1 = NULL;
  11838. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11839. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  11840. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11841. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11842. if (shdr_status || shdr_add_status || rc) {
  11843. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11844. "2500 EQ_CREATE mailbox failed with "
  11845. "status x%x add_status x%x, mbx status x%x\n",
  11846. shdr_status, shdr_add_status, rc);
  11847. status = -ENXIO;
  11848. }
  11849. eq->type = LPFC_EQ;
  11850. eq->subtype = LPFC_NONE;
  11851. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  11852. if (eq->queue_id == 0xFFFF)
  11853. status = -ENXIO;
  11854. eq->host_index = 0;
  11855. eq->hba_index = 0;
  11856. mempool_free(mbox, phba->mbox_mem_pool);
  11857. return status;
  11858. }
  11859. /**
  11860. * lpfc_cq_create - Create a Completion Queue on the HBA
  11861. * @phba: HBA structure that indicates port to create a queue on.
  11862. * @cq: The queue structure to use to create the completion queue.
  11863. * @eq: The event queue to bind this completion queue to.
  11864. *
  11865. * This function creates a completion queue, as detailed in @wq, on a port,
  11866. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  11867. *
  11868. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11869. * is used to get the entry count and entry size that are necessary to
  11870. * determine the number of pages to allocate and use for this queue. The @eq
  11871. * is used to indicate which event queue to bind this completion queue to. This
  11872. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  11873. * completion queue. This function is asynchronous and will wait for the mailbox
  11874. * command to finish before continuing.
  11875. *
  11876. * On success this function will return a zero. If unable to allocate enough
  11877. * memory this function will return -ENOMEM. If the queue create mailbox command
  11878. * fails this function will return -ENXIO.
  11879. **/
  11880. int
  11881. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11882. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  11883. {
  11884. struct lpfc_mbx_cq_create *cq_create;
  11885. struct lpfc_dmabuf *dmabuf;
  11886. LPFC_MBOXQ_t *mbox;
  11887. int rc, length, status = 0;
  11888. uint32_t shdr_status, shdr_add_status;
  11889. union lpfc_sli4_cfg_shdr *shdr;
  11890. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11891. /* sanity check on queue memory */
  11892. if (!cq || !eq)
  11893. return -ENODEV;
  11894. if (!phba->sli4_hba.pc_sli4_params.supported)
  11895. hw_page_size = SLI4_PAGE_SIZE;
  11896. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11897. if (!mbox)
  11898. return -ENOMEM;
  11899. length = (sizeof(struct lpfc_mbx_cq_create) -
  11900. sizeof(struct lpfc_sli4_cfg_mhdr));
  11901. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11902. LPFC_MBOX_OPCODE_CQ_CREATE,
  11903. length, LPFC_SLI4_MBX_EMBED);
  11904. cq_create = &mbox->u.mqe.un.cq_create;
  11905. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  11906. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  11907. cq->page_count);
  11908. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  11909. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  11910. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11911. phba->sli4_hba.pc_sli4_params.cqv);
  11912. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  11913. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  11914. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  11915. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  11916. eq->queue_id);
  11917. } else {
  11918. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  11919. eq->queue_id);
  11920. }
  11921. switch (cq->entry_count) {
  11922. default:
  11923. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11924. "0361 Unsupported CQ count. (%d)\n",
  11925. cq->entry_count);
  11926. if (cq->entry_count < 256) {
  11927. status = -EINVAL;
  11928. goto out;
  11929. }
  11930. /* otherwise default to smallest count (drop through) */
  11931. case 256:
  11932. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11933. LPFC_CQ_CNT_256);
  11934. break;
  11935. case 512:
  11936. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11937. LPFC_CQ_CNT_512);
  11938. break;
  11939. case 1024:
  11940. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11941. LPFC_CQ_CNT_1024);
  11942. break;
  11943. }
  11944. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11945. memset(dmabuf->virt, 0, hw_page_size);
  11946. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11947. putPaddrLow(dmabuf->phys);
  11948. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11949. putPaddrHigh(dmabuf->phys);
  11950. }
  11951. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11952. /* The IOCTL status is embedded in the mailbox subheader. */
  11953. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11954. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11955. if (shdr_status || shdr_add_status || rc) {
  11956. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11957. "2501 CQ_CREATE mailbox failed with "
  11958. "status x%x add_status x%x, mbx status x%x\n",
  11959. shdr_status, shdr_add_status, rc);
  11960. status = -ENXIO;
  11961. goto out;
  11962. }
  11963. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11964. if (cq->queue_id == 0xFFFF) {
  11965. status = -ENXIO;
  11966. goto out;
  11967. }
  11968. /* link the cq onto the parent eq child list */
  11969. list_add_tail(&cq->list, &eq->child_list);
  11970. /* Set up completion queue's type and subtype */
  11971. cq->type = type;
  11972. cq->subtype = subtype;
  11973. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11974. cq->assoc_qid = eq->queue_id;
  11975. cq->host_index = 0;
  11976. cq->hba_index = 0;
  11977. out:
  11978. mempool_free(mbox, phba->mbox_mem_pool);
  11979. return status;
  11980. }
  11981. /**
  11982. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11983. * @phba: HBA structure that indicates port to create a queue on.
  11984. * @mq: The queue structure to use to create the mailbox queue.
  11985. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11986. * @cq: The completion queue to associate with this cq.
  11987. *
  11988. * This function provides failback (fb) functionality when the
  11989. * mq_create_ext fails on older FW generations. It's purpose is identical
  11990. * to mq_create_ext otherwise.
  11991. *
  11992. * This routine cannot fail as all attributes were previously accessed and
  11993. * initialized in mq_create_ext.
  11994. **/
  11995. static void
  11996. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11997. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11998. {
  11999. struct lpfc_mbx_mq_create *mq_create;
  12000. struct lpfc_dmabuf *dmabuf;
  12001. int length;
  12002. length = (sizeof(struct lpfc_mbx_mq_create) -
  12003. sizeof(struct lpfc_sli4_cfg_mhdr));
  12004. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12005. LPFC_MBOX_OPCODE_MQ_CREATE,
  12006. length, LPFC_SLI4_MBX_EMBED);
  12007. mq_create = &mbox->u.mqe.un.mq_create;
  12008. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  12009. mq->page_count);
  12010. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  12011. cq->queue_id);
  12012. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  12013. switch (mq->entry_count) {
  12014. case 16:
  12015. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  12016. LPFC_MQ_RING_SIZE_16);
  12017. break;
  12018. case 32:
  12019. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  12020. LPFC_MQ_RING_SIZE_32);
  12021. break;
  12022. case 64:
  12023. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  12024. LPFC_MQ_RING_SIZE_64);
  12025. break;
  12026. case 128:
  12027. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  12028. LPFC_MQ_RING_SIZE_128);
  12029. break;
  12030. }
  12031. list_for_each_entry(dmabuf, &mq->page_list, list) {
  12032. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12033. putPaddrLow(dmabuf->phys);
  12034. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12035. putPaddrHigh(dmabuf->phys);
  12036. }
  12037. }
  12038. /**
  12039. * lpfc_mq_create - Create a mailbox Queue on the HBA
  12040. * @phba: HBA structure that indicates port to create a queue on.
  12041. * @mq: The queue structure to use to create the mailbox queue.
  12042. * @cq: The completion queue to associate with this cq.
  12043. * @subtype: The queue's subtype.
  12044. *
  12045. * This function creates a mailbox queue, as detailed in @mq, on a port,
  12046. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  12047. *
  12048. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  12049. * is used to get the entry count and entry size that are necessary to
  12050. * determine the number of pages to allocate and use for this queue. This
  12051. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  12052. * mailbox queue. This function is asynchronous and will wait for the mailbox
  12053. * command to finish before continuing.
  12054. *
  12055. * On success this function will return a zero. If unable to allocate enough
  12056. * memory this function will return -ENOMEM. If the queue create mailbox command
  12057. * fails this function will return -ENXIO.
  12058. **/
  12059. int32_t
  12060. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  12061. struct lpfc_queue *cq, uint32_t subtype)
  12062. {
  12063. struct lpfc_mbx_mq_create *mq_create;
  12064. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  12065. struct lpfc_dmabuf *dmabuf;
  12066. LPFC_MBOXQ_t *mbox;
  12067. int rc, length, status = 0;
  12068. uint32_t shdr_status, shdr_add_status;
  12069. union lpfc_sli4_cfg_shdr *shdr;
  12070. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  12071. /* sanity check on queue memory */
  12072. if (!mq || !cq)
  12073. return -ENODEV;
  12074. if (!phba->sli4_hba.pc_sli4_params.supported)
  12075. hw_page_size = SLI4_PAGE_SIZE;
  12076. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12077. if (!mbox)
  12078. return -ENOMEM;
  12079. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  12080. sizeof(struct lpfc_sli4_cfg_mhdr));
  12081. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12082. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  12083. length, LPFC_SLI4_MBX_EMBED);
  12084. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  12085. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  12086. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  12087. &mq_create_ext->u.request, mq->page_count);
  12088. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  12089. &mq_create_ext->u.request, 1);
  12090. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  12091. &mq_create_ext->u.request, 1);
  12092. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  12093. &mq_create_ext->u.request, 1);
  12094. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  12095. &mq_create_ext->u.request, 1);
  12096. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  12097. &mq_create_ext->u.request, 1);
  12098. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  12099. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12100. phba->sli4_hba.pc_sli4_params.mqv);
  12101. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  12102. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  12103. cq->queue_id);
  12104. else
  12105. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  12106. cq->queue_id);
  12107. switch (mq->entry_count) {
  12108. default:
  12109. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12110. "0362 Unsupported MQ count. (%d)\n",
  12111. mq->entry_count);
  12112. if (mq->entry_count < 16) {
  12113. status = -EINVAL;
  12114. goto out;
  12115. }
  12116. /* otherwise default to smallest count (drop through) */
  12117. case 16:
  12118. bf_set(lpfc_mq_context_ring_size,
  12119. &mq_create_ext->u.request.context,
  12120. LPFC_MQ_RING_SIZE_16);
  12121. break;
  12122. case 32:
  12123. bf_set(lpfc_mq_context_ring_size,
  12124. &mq_create_ext->u.request.context,
  12125. LPFC_MQ_RING_SIZE_32);
  12126. break;
  12127. case 64:
  12128. bf_set(lpfc_mq_context_ring_size,
  12129. &mq_create_ext->u.request.context,
  12130. LPFC_MQ_RING_SIZE_64);
  12131. break;
  12132. case 128:
  12133. bf_set(lpfc_mq_context_ring_size,
  12134. &mq_create_ext->u.request.context,
  12135. LPFC_MQ_RING_SIZE_128);
  12136. break;
  12137. }
  12138. list_for_each_entry(dmabuf, &mq->page_list, list) {
  12139. memset(dmabuf->virt, 0, hw_page_size);
  12140. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  12141. putPaddrLow(dmabuf->phys);
  12142. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  12143. putPaddrHigh(dmabuf->phys);
  12144. }
  12145. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12146. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  12147. &mq_create_ext->u.response);
  12148. if (rc != MBX_SUCCESS) {
  12149. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12150. "2795 MQ_CREATE_EXT failed with "
  12151. "status x%x. Failback to MQ_CREATE.\n",
  12152. rc);
  12153. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  12154. mq_create = &mbox->u.mqe.un.mq_create;
  12155. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12156. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  12157. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  12158. &mq_create->u.response);
  12159. }
  12160. /* The IOCTL status is embedded in the mailbox subheader. */
  12161. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12162. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12163. if (shdr_status || shdr_add_status || rc) {
  12164. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12165. "2502 MQ_CREATE mailbox failed with "
  12166. "status x%x add_status x%x, mbx status x%x\n",
  12167. shdr_status, shdr_add_status, rc);
  12168. status = -ENXIO;
  12169. goto out;
  12170. }
  12171. if (mq->queue_id == 0xFFFF) {
  12172. status = -ENXIO;
  12173. goto out;
  12174. }
  12175. mq->type = LPFC_MQ;
  12176. mq->assoc_qid = cq->queue_id;
  12177. mq->subtype = subtype;
  12178. mq->host_index = 0;
  12179. mq->hba_index = 0;
  12180. /* link the mq onto the parent cq child list */
  12181. list_add_tail(&mq->list, &cq->child_list);
  12182. out:
  12183. mempool_free(mbox, phba->mbox_mem_pool);
  12184. return status;
  12185. }
  12186. /**
  12187. * lpfc_wq_create - Create a Work Queue on the HBA
  12188. * @phba: HBA structure that indicates port to create a queue on.
  12189. * @wq: The queue structure to use to create the work queue.
  12190. * @cq: The completion queue to bind this work queue to.
  12191. * @subtype: The subtype of the work queue indicating its functionality.
  12192. *
  12193. * This function creates a work queue, as detailed in @wq, on a port, described
  12194. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  12195. *
  12196. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  12197. * is used to get the entry count and entry size that are necessary to
  12198. * determine the number of pages to allocate and use for this queue. The @cq
  12199. * is used to indicate which completion queue to bind this work queue to. This
  12200. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  12201. * work queue. This function is asynchronous and will wait for the mailbox
  12202. * command to finish before continuing.
  12203. *
  12204. * On success this function will return a zero. If unable to allocate enough
  12205. * memory this function will return -ENOMEM. If the queue create mailbox command
  12206. * fails this function will return -ENXIO.
  12207. **/
  12208. int
  12209. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  12210. struct lpfc_queue *cq, uint32_t subtype)
  12211. {
  12212. struct lpfc_mbx_wq_create *wq_create;
  12213. struct lpfc_dmabuf *dmabuf;
  12214. LPFC_MBOXQ_t *mbox;
  12215. int rc, length, status = 0;
  12216. uint32_t shdr_status, shdr_add_status;
  12217. union lpfc_sli4_cfg_shdr *shdr;
  12218. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  12219. struct dma_address *page;
  12220. void __iomem *bar_memmap_p;
  12221. uint32_t db_offset;
  12222. uint16_t pci_barset;
  12223. /* sanity check on queue memory */
  12224. if (!wq || !cq)
  12225. return -ENODEV;
  12226. if (!phba->sli4_hba.pc_sli4_params.supported)
  12227. hw_page_size = SLI4_PAGE_SIZE;
  12228. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12229. if (!mbox)
  12230. return -ENOMEM;
  12231. length = (sizeof(struct lpfc_mbx_wq_create) -
  12232. sizeof(struct lpfc_sli4_cfg_mhdr));
  12233. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12234. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  12235. length, LPFC_SLI4_MBX_EMBED);
  12236. wq_create = &mbox->u.mqe.un.wq_create;
  12237. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  12238. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  12239. wq->page_count);
  12240. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  12241. cq->queue_id);
  12242. /* wqv is the earliest version supported, NOT the latest */
  12243. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12244. phba->sli4_hba.pc_sli4_params.wqv);
  12245. switch (phba->sli4_hba.pc_sli4_params.wqv) {
  12246. case LPFC_Q_CREATE_VERSION_0:
  12247. switch (wq->entry_size) {
  12248. default:
  12249. case 64:
  12250. /* Nothing to do, version 0 ONLY supports 64 byte */
  12251. page = wq_create->u.request.page;
  12252. break;
  12253. case 128:
  12254. if (!(phba->sli4_hba.pc_sli4_params.wqsize &
  12255. LPFC_WQ_SZ128_SUPPORT)) {
  12256. status = -ERANGE;
  12257. goto out;
  12258. }
  12259. /* If we get here the HBA MUST also support V1 and
  12260. * we MUST use it
  12261. */
  12262. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12263. LPFC_Q_CREATE_VERSION_1);
  12264. bf_set(lpfc_mbx_wq_create_wqe_count,
  12265. &wq_create->u.request_1, wq->entry_count);
  12266. bf_set(lpfc_mbx_wq_create_wqe_size,
  12267. &wq_create->u.request_1,
  12268. LPFC_WQ_WQE_SIZE_128);
  12269. bf_set(lpfc_mbx_wq_create_page_size,
  12270. &wq_create->u.request_1,
  12271. (PAGE_SIZE/SLI4_PAGE_SIZE));
  12272. page = wq_create->u.request_1.page;
  12273. break;
  12274. }
  12275. break;
  12276. case LPFC_Q_CREATE_VERSION_1:
  12277. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  12278. wq->entry_count);
  12279. switch (wq->entry_size) {
  12280. default:
  12281. case 64:
  12282. bf_set(lpfc_mbx_wq_create_wqe_size,
  12283. &wq_create->u.request_1,
  12284. LPFC_WQ_WQE_SIZE_64);
  12285. break;
  12286. case 128:
  12287. if (!(phba->sli4_hba.pc_sli4_params.wqsize &
  12288. LPFC_WQ_SZ128_SUPPORT)) {
  12289. status = -ERANGE;
  12290. goto out;
  12291. }
  12292. bf_set(lpfc_mbx_wq_create_wqe_size,
  12293. &wq_create->u.request_1,
  12294. LPFC_WQ_WQE_SIZE_128);
  12295. break;
  12296. }
  12297. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  12298. (PAGE_SIZE/SLI4_PAGE_SIZE));
  12299. page = wq_create->u.request_1.page;
  12300. break;
  12301. default:
  12302. status = -ERANGE;
  12303. goto out;
  12304. }
  12305. list_for_each_entry(dmabuf, &wq->page_list, list) {
  12306. memset(dmabuf->virt, 0, hw_page_size);
  12307. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  12308. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  12309. }
  12310. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  12311. bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
  12312. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12313. /* The IOCTL status is embedded in the mailbox subheader. */
  12314. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12315. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12316. if (shdr_status || shdr_add_status || rc) {
  12317. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12318. "2503 WQ_CREATE mailbox failed with "
  12319. "status x%x add_status x%x, mbx status x%x\n",
  12320. shdr_status, shdr_add_status, rc);
  12321. status = -ENXIO;
  12322. goto out;
  12323. }
  12324. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  12325. if (wq->queue_id == 0xFFFF) {
  12326. status = -ENXIO;
  12327. goto out;
  12328. }
  12329. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
  12330. wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
  12331. &wq_create->u.response);
  12332. if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
  12333. (wq->db_format != LPFC_DB_RING_FORMAT)) {
  12334. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12335. "3265 WQ[%d] doorbell format not "
  12336. "supported: x%x\n", wq->queue_id,
  12337. wq->db_format);
  12338. status = -EINVAL;
  12339. goto out;
  12340. }
  12341. pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
  12342. &wq_create->u.response);
  12343. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
  12344. if (!bar_memmap_p) {
  12345. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12346. "3263 WQ[%d] failed to memmap pci "
  12347. "barset:x%x\n", wq->queue_id,
  12348. pci_barset);
  12349. status = -ENOMEM;
  12350. goto out;
  12351. }
  12352. db_offset = wq_create->u.response.doorbell_offset;
  12353. if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
  12354. (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
  12355. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12356. "3252 WQ[%d] doorbell offset not "
  12357. "supported: x%x\n", wq->queue_id,
  12358. db_offset);
  12359. status = -EINVAL;
  12360. goto out;
  12361. }
  12362. wq->db_regaddr = bar_memmap_p + db_offset;
  12363. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12364. "3264 WQ[%d]: barset:x%x, offset:x%x, "
  12365. "format:x%x\n", wq->queue_id, pci_barset,
  12366. db_offset, wq->db_format);
  12367. } else {
  12368. wq->db_format = LPFC_DB_LIST_FORMAT;
  12369. wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
  12370. }
  12371. wq->type = LPFC_WQ;
  12372. wq->assoc_qid = cq->queue_id;
  12373. wq->subtype = subtype;
  12374. wq->host_index = 0;
  12375. wq->hba_index = 0;
  12376. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  12377. /* link the wq onto the parent cq child list */
  12378. list_add_tail(&wq->list, &cq->child_list);
  12379. out:
  12380. mempool_free(mbox, phba->mbox_mem_pool);
  12381. return status;
  12382. }
  12383. /**
  12384. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  12385. * @phba: HBA structure that indicates port to create a queue on.
  12386. * @rq: The queue structure to use for the receive queue.
  12387. * @qno: The associated HBQ number
  12388. *
  12389. *
  12390. * For SLI4 we need to adjust the RQ repost value based on
  12391. * the number of buffers that are initially posted to the RQ.
  12392. */
  12393. void
  12394. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  12395. {
  12396. uint32_t cnt;
  12397. /* sanity check on queue memory */
  12398. if (!rq)
  12399. return;
  12400. cnt = lpfc_hbq_defs[qno]->entry_count;
  12401. /* Recalc repost for RQs based on buffers initially posted */
  12402. cnt = (cnt >> 3);
  12403. if (cnt < LPFC_QUEUE_MIN_REPOST)
  12404. cnt = LPFC_QUEUE_MIN_REPOST;
  12405. rq->entry_repost = cnt;
  12406. }
  12407. /**
  12408. * lpfc_rq_create - Create a Receive Queue on the HBA
  12409. * @phba: HBA structure that indicates port to create a queue on.
  12410. * @hrq: The queue structure to use to create the header receive queue.
  12411. * @drq: The queue structure to use to create the data receive queue.
  12412. * @cq: The completion queue to bind this work queue to.
  12413. *
  12414. * This function creates a receive buffer queue pair , as detailed in @hrq and
  12415. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  12416. * to the HBA.
  12417. *
  12418. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  12419. * struct is used to get the entry count that is necessary to determine the
  12420. * number of pages to use for this queue. The @cq is used to indicate which
  12421. * completion queue to bind received buffers that are posted to these queues to.
  12422. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  12423. * receive queue pair. This function is asynchronous and will wait for the
  12424. * mailbox command to finish before continuing.
  12425. *
  12426. * On success this function will return a zero. If unable to allocate enough
  12427. * memory this function will return -ENOMEM. If the queue create mailbox command
  12428. * fails this function will return -ENXIO.
  12429. **/
  12430. int
  12431. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  12432. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  12433. {
  12434. struct lpfc_mbx_rq_create *rq_create;
  12435. struct lpfc_dmabuf *dmabuf;
  12436. LPFC_MBOXQ_t *mbox;
  12437. int rc, length, status = 0;
  12438. uint32_t shdr_status, shdr_add_status;
  12439. union lpfc_sli4_cfg_shdr *shdr;
  12440. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  12441. void __iomem *bar_memmap_p;
  12442. uint32_t db_offset;
  12443. uint16_t pci_barset;
  12444. /* sanity check on queue memory */
  12445. if (!hrq || !drq || !cq)
  12446. return -ENODEV;
  12447. if (!phba->sli4_hba.pc_sli4_params.supported)
  12448. hw_page_size = SLI4_PAGE_SIZE;
  12449. if (hrq->entry_count != drq->entry_count)
  12450. return -EINVAL;
  12451. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12452. if (!mbox)
  12453. return -ENOMEM;
  12454. length = (sizeof(struct lpfc_mbx_rq_create) -
  12455. sizeof(struct lpfc_sli4_cfg_mhdr));
  12456. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12457. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  12458. length, LPFC_SLI4_MBX_EMBED);
  12459. rq_create = &mbox->u.mqe.un.rq_create;
  12460. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  12461. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12462. phba->sli4_hba.pc_sli4_params.rqv);
  12463. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  12464. bf_set(lpfc_rq_context_rqe_count_1,
  12465. &rq_create->u.request.context,
  12466. hrq->entry_count);
  12467. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  12468. bf_set(lpfc_rq_context_rqe_size,
  12469. &rq_create->u.request.context,
  12470. LPFC_RQE_SIZE_8);
  12471. bf_set(lpfc_rq_context_page_size,
  12472. &rq_create->u.request.context,
  12473. (PAGE_SIZE/SLI4_PAGE_SIZE));
  12474. } else {
  12475. switch (hrq->entry_count) {
  12476. default:
  12477. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12478. "2535 Unsupported RQ count. (%d)\n",
  12479. hrq->entry_count);
  12480. if (hrq->entry_count < 512) {
  12481. status = -EINVAL;
  12482. goto out;
  12483. }
  12484. /* otherwise default to smallest count (drop through) */
  12485. case 512:
  12486. bf_set(lpfc_rq_context_rqe_count,
  12487. &rq_create->u.request.context,
  12488. LPFC_RQ_RING_SIZE_512);
  12489. break;
  12490. case 1024:
  12491. bf_set(lpfc_rq_context_rqe_count,
  12492. &rq_create->u.request.context,
  12493. LPFC_RQ_RING_SIZE_1024);
  12494. break;
  12495. case 2048:
  12496. bf_set(lpfc_rq_context_rqe_count,
  12497. &rq_create->u.request.context,
  12498. LPFC_RQ_RING_SIZE_2048);
  12499. break;
  12500. case 4096:
  12501. bf_set(lpfc_rq_context_rqe_count,
  12502. &rq_create->u.request.context,
  12503. LPFC_RQ_RING_SIZE_4096);
  12504. break;
  12505. }
  12506. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  12507. LPFC_HDR_BUF_SIZE);
  12508. }
  12509. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  12510. cq->queue_id);
  12511. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  12512. hrq->page_count);
  12513. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  12514. memset(dmabuf->virt, 0, hw_page_size);
  12515. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12516. putPaddrLow(dmabuf->phys);
  12517. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12518. putPaddrHigh(dmabuf->phys);
  12519. }
  12520. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  12521. bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
  12522. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12523. /* The IOCTL status is embedded in the mailbox subheader. */
  12524. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12525. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12526. if (shdr_status || shdr_add_status || rc) {
  12527. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12528. "2504 RQ_CREATE mailbox failed with "
  12529. "status x%x add_status x%x, mbx status x%x\n",
  12530. shdr_status, shdr_add_status, rc);
  12531. status = -ENXIO;
  12532. goto out;
  12533. }
  12534. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  12535. if (hrq->queue_id == 0xFFFF) {
  12536. status = -ENXIO;
  12537. goto out;
  12538. }
  12539. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
  12540. hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
  12541. &rq_create->u.response);
  12542. if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
  12543. (hrq->db_format != LPFC_DB_RING_FORMAT)) {
  12544. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12545. "3262 RQ [%d] doorbell format not "
  12546. "supported: x%x\n", hrq->queue_id,
  12547. hrq->db_format);
  12548. status = -EINVAL;
  12549. goto out;
  12550. }
  12551. pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
  12552. &rq_create->u.response);
  12553. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
  12554. if (!bar_memmap_p) {
  12555. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12556. "3269 RQ[%d] failed to memmap pci "
  12557. "barset:x%x\n", hrq->queue_id,
  12558. pci_barset);
  12559. status = -ENOMEM;
  12560. goto out;
  12561. }
  12562. db_offset = rq_create->u.response.doorbell_offset;
  12563. if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
  12564. (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
  12565. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12566. "3270 RQ[%d] doorbell offset not "
  12567. "supported: x%x\n", hrq->queue_id,
  12568. db_offset);
  12569. status = -EINVAL;
  12570. goto out;
  12571. }
  12572. hrq->db_regaddr = bar_memmap_p + db_offset;
  12573. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12574. "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
  12575. "format:x%x\n", hrq->queue_id, pci_barset,
  12576. db_offset, hrq->db_format);
  12577. } else {
  12578. hrq->db_format = LPFC_DB_RING_FORMAT;
  12579. hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
  12580. }
  12581. hrq->type = LPFC_HRQ;
  12582. hrq->assoc_qid = cq->queue_id;
  12583. hrq->subtype = subtype;
  12584. hrq->host_index = 0;
  12585. hrq->hba_index = 0;
  12586. /* now create the data queue */
  12587. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12588. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  12589. length, LPFC_SLI4_MBX_EMBED);
  12590. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12591. phba->sli4_hba.pc_sli4_params.rqv);
  12592. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  12593. bf_set(lpfc_rq_context_rqe_count_1,
  12594. &rq_create->u.request.context, hrq->entry_count);
  12595. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  12596. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  12597. LPFC_RQE_SIZE_8);
  12598. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  12599. (PAGE_SIZE/SLI4_PAGE_SIZE));
  12600. } else {
  12601. switch (drq->entry_count) {
  12602. default:
  12603. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12604. "2536 Unsupported RQ count. (%d)\n",
  12605. drq->entry_count);
  12606. if (drq->entry_count < 512) {
  12607. status = -EINVAL;
  12608. goto out;
  12609. }
  12610. /* otherwise default to smallest count (drop through) */
  12611. case 512:
  12612. bf_set(lpfc_rq_context_rqe_count,
  12613. &rq_create->u.request.context,
  12614. LPFC_RQ_RING_SIZE_512);
  12615. break;
  12616. case 1024:
  12617. bf_set(lpfc_rq_context_rqe_count,
  12618. &rq_create->u.request.context,
  12619. LPFC_RQ_RING_SIZE_1024);
  12620. break;
  12621. case 2048:
  12622. bf_set(lpfc_rq_context_rqe_count,
  12623. &rq_create->u.request.context,
  12624. LPFC_RQ_RING_SIZE_2048);
  12625. break;
  12626. case 4096:
  12627. bf_set(lpfc_rq_context_rqe_count,
  12628. &rq_create->u.request.context,
  12629. LPFC_RQ_RING_SIZE_4096);
  12630. break;
  12631. }
  12632. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  12633. LPFC_DATA_BUF_SIZE);
  12634. }
  12635. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  12636. cq->queue_id);
  12637. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  12638. drq->page_count);
  12639. list_for_each_entry(dmabuf, &drq->page_list, list) {
  12640. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12641. putPaddrLow(dmabuf->phys);
  12642. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12643. putPaddrHigh(dmabuf->phys);
  12644. }
  12645. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  12646. bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
  12647. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12648. /* The IOCTL status is embedded in the mailbox subheader. */
  12649. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  12650. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12651. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12652. if (shdr_status || shdr_add_status || rc) {
  12653. status = -ENXIO;
  12654. goto out;
  12655. }
  12656. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  12657. if (drq->queue_id == 0xFFFF) {
  12658. status = -ENXIO;
  12659. goto out;
  12660. }
  12661. drq->type = LPFC_DRQ;
  12662. drq->assoc_qid = cq->queue_id;
  12663. drq->subtype = subtype;
  12664. drq->host_index = 0;
  12665. drq->hba_index = 0;
  12666. /* link the header and data RQs onto the parent cq child list */
  12667. list_add_tail(&hrq->list, &cq->child_list);
  12668. list_add_tail(&drq->list, &cq->child_list);
  12669. out:
  12670. mempool_free(mbox, phba->mbox_mem_pool);
  12671. return status;
  12672. }
  12673. /**
  12674. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  12675. * @eq: The queue structure associated with the queue to destroy.
  12676. *
  12677. * This function destroys a queue, as detailed in @eq by sending an mailbox
  12678. * command, specific to the type of queue, to the HBA.
  12679. *
  12680. * The @eq struct is used to get the queue ID of the queue to destroy.
  12681. *
  12682. * On success this function will return a zero. If the queue destroy mailbox
  12683. * command fails this function will return -ENXIO.
  12684. **/
  12685. int
  12686. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  12687. {
  12688. LPFC_MBOXQ_t *mbox;
  12689. int rc, length, status = 0;
  12690. uint32_t shdr_status, shdr_add_status;
  12691. union lpfc_sli4_cfg_shdr *shdr;
  12692. /* sanity check on queue memory */
  12693. if (!eq)
  12694. return -ENODEV;
  12695. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  12696. if (!mbox)
  12697. return -ENOMEM;
  12698. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  12699. sizeof(struct lpfc_sli4_cfg_mhdr));
  12700. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12701. LPFC_MBOX_OPCODE_EQ_DESTROY,
  12702. length, LPFC_SLI4_MBX_EMBED);
  12703. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  12704. eq->queue_id);
  12705. mbox->vport = eq->phba->pport;
  12706. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12707. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  12708. /* The IOCTL status is embedded in the mailbox subheader. */
  12709. shdr = (union lpfc_sli4_cfg_shdr *)
  12710. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  12711. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12712. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12713. if (shdr_status || shdr_add_status || rc) {
  12714. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12715. "2505 EQ_DESTROY mailbox failed with "
  12716. "status x%x add_status x%x, mbx status x%x\n",
  12717. shdr_status, shdr_add_status, rc);
  12718. status = -ENXIO;
  12719. }
  12720. /* Remove eq from any list */
  12721. list_del_init(&eq->list);
  12722. mempool_free(mbox, eq->phba->mbox_mem_pool);
  12723. return status;
  12724. }
  12725. /**
  12726. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  12727. * @cq: The queue structure associated with the queue to destroy.
  12728. *
  12729. * This function destroys a queue, as detailed in @cq by sending an mailbox
  12730. * command, specific to the type of queue, to the HBA.
  12731. *
  12732. * The @cq struct is used to get the queue ID of the queue to destroy.
  12733. *
  12734. * On success this function will return a zero. If the queue destroy mailbox
  12735. * command fails this function will return -ENXIO.
  12736. **/
  12737. int
  12738. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  12739. {
  12740. LPFC_MBOXQ_t *mbox;
  12741. int rc, length, status = 0;
  12742. uint32_t shdr_status, shdr_add_status;
  12743. union lpfc_sli4_cfg_shdr *shdr;
  12744. /* sanity check on queue memory */
  12745. if (!cq)
  12746. return -ENODEV;
  12747. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  12748. if (!mbox)
  12749. return -ENOMEM;
  12750. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  12751. sizeof(struct lpfc_sli4_cfg_mhdr));
  12752. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12753. LPFC_MBOX_OPCODE_CQ_DESTROY,
  12754. length, LPFC_SLI4_MBX_EMBED);
  12755. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  12756. cq->queue_id);
  12757. mbox->vport = cq->phba->pport;
  12758. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12759. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  12760. /* The IOCTL status is embedded in the mailbox subheader. */
  12761. shdr = (union lpfc_sli4_cfg_shdr *)
  12762. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  12763. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12764. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12765. if (shdr_status || shdr_add_status || rc) {
  12766. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12767. "2506 CQ_DESTROY mailbox failed with "
  12768. "status x%x add_status x%x, mbx status x%x\n",
  12769. shdr_status, shdr_add_status, rc);
  12770. status = -ENXIO;
  12771. }
  12772. /* Remove cq from any list */
  12773. list_del_init(&cq->list);
  12774. mempool_free(mbox, cq->phba->mbox_mem_pool);
  12775. return status;
  12776. }
  12777. /**
  12778. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  12779. * @qm: The queue structure associated with the queue to destroy.
  12780. *
  12781. * This function destroys a queue, as detailed in @mq by sending an mailbox
  12782. * command, specific to the type of queue, to the HBA.
  12783. *
  12784. * The @mq struct is used to get the queue ID of the queue to destroy.
  12785. *
  12786. * On success this function will return a zero. If the queue destroy mailbox
  12787. * command fails this function will return -ENXIO.
  12788. **/
  12789. int
  12790. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  12791. {
  12792. LPFC_MBOXQ_t *mbox;
  12793. int rc, length, status = 0;
  12794. uint32_t shdr_status, shdr_add_status;
  12795. union lpfc_sli4_cfg_shdr *shdr;
  12796. /* sanity check on queue memory */
  12797. if (!mq)
  12798. return -ENODEV;
  12799. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  12800. if (!mbox)
  12801. return -ENOMEM;
  12802. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  12803. sizeof(struct lpfc_sli4_cfg_mhdr));
  12804. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12805. LPFC_MBOX_OPCODE_MQ_DESTROY,
  12806. length, LPFC_SLI4_MBX_EMBED);
  12807. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  12808. mq->queue_id);
  12809. mbox->vport = mq->phba->pport;
  12810. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12811. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  12812. /* The IOCTL status is embedded in the mailbox subheader. */
  12813. shdr = (union lpfc_sli4_cfg_shdr *)
  12814. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  12815. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12816. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12817. if (shdr_status || shdr_add_status || rc) {
  12818. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12819. "2507 MQ_DESTROY mailbox failed with "
  12820. "status x%x add_status x%x, mbx status x%x\n",
  12821. shdr_status, shdr_add_status, rc);
  12822. status = -ENXIO;
  12823. }
  12824. /* Remove mq from any list */
  12825. list_del_init(&mq->list);
  12826. mempool_free(mbox, mq->phba->mbox_mem_pool);
  12827. return status;
  12828. }
  12829. /**
  12830. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  12831. * @wq: The queue structure associated with the queue to destroy.
  12832. *
  12833. * This function destroys a queue, as detailed in @wq by sending an mailbox
  12834. * command, specific to the type of queue, to the HBA.
  12835. *
  12836. * The @wq struct is used to get the queue ID of the queue to destroy.
  12837. *
  12838. * On success this function will return a zero. If the queue destroy mailbox
  12839. * command fails this function will return -ENXIO.
  12840. **/
  12841. int
  12842. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  12843. {
  12844. LPFC_MBOXQ_t *mbox;
  12845. int rc, length, status = 0;
  12846. uint32_t shdr_status, shdr_add_status;
  12847. union lpfc_sli4_cfg_shdr *shdr;
  12848. /* sanity check on queue memory */
  12849. if (!wq)
  12850. return -ENODEV;
  12851. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  12852. if (!mbox)
  12853. return -ENOMEM;
  12854. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  12855. sizeof(struct lpfc_sli4_cfg_mhdr));
  12856. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12857. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  12858. length, LPFC_SLI4_MBX_EMBED);
  12859. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  12860. wq->queue_id);
  12861. mbox->vport = wq->phba->pport;
  12862. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12863. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  12864. shdr = (union lpfc_sli4_cfg_shdr *)
  12865. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  12866. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12867. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12868. if (shdr_status || shdr_add_status || rc) {
  12869. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12870. "2508 WQ_DESTROY mailbox failed with "
  12871. "status x%x add_status x%x, mbx status x%x\n",
  12872. shdr_status, shdr_add_status, rc);
  12873. status = -ENXIO;
  12874. }
  12875. /* Remove wq from any list */
  12876. list_del_init(&wq->list);
  12877. mempool_free(mbox, wq->phba->mbox_mem_pool);
  12878. return status;
  12879. }
  12880. /**
  12881. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  12882. * @rq: The queue structure associated with the queue to destroy.
  12883. *
  12884. * This function destroys a queue, as detailed in @rq by sending an mailbox
  12885. * command, specific to the type of queue, to the HBA.
  12886. *
  12887. * The @rq struct is used to get the queue ID of the queue to destroy.
  12888. *
  12889. * On success this function will return a zero. If the queue destroy mailbox
  12890. * command fails this function will return -ENXIO.
  12891. **/
  12892. int
  12893. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  12894. struct lpfc_queue *drq)
  12895. {
  12896. LPFC_MBOXQ_t *mbox;
  12897. int rc, length, status = 0;
  12898. uint32_t shdr_status, shdr_add_status;
  12899. union lpfc_sli4_cfg_shdr *shdr;
  12900. /* sanity check on queue memory */
  12901. if (!hrq || !drq)
  12902. return -ENODEV;
  12903. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  12904. if (!mbox)
  12905. return -ENOMEM;
  12906. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  12907. sizeof(struct lpfc_sli4_cfg_mhdr));
  12908. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12909. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  12910. length, LPFC_SLI4_MBX_EMBED);
  12911. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12912. hrq->queue_id);
  12913. mbox->vport = hrq->phba->pport;
  12914. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12915. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  12916. /* The IOCTL status is embedded in the mailbox subheader. */
  12917. shdr = (union lpfc_sli4_cfg_shdr *)
  12918. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12919. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12920. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12921. if (shdr_status || shdr_add_status || rc) {
  12922. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12923. "2509 RQ_DESTROY mailbox failed with "
  12924. "status x%x add_status x%x, mbx status x%x\n",
  12925. shdr_status, shdr_add_status, rc);
  12926. if (rc != MBX_TIMEOUT)
  12927. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12928. return -ENXIO;
  12929. }
  12930. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12931. drq->queue_id);
  12932. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  12933. shdr = (union lpfc_sli4_cfg_shdr *)
  12934. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12935. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12936. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12937. if (shdr_status || shdr_add_status || rc) {
  12938. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12939. "2510 RQ_DESTROY mailbox failed with "
  12940. "status x%x add_status x%x, mbx status x%x\n",
  12941. shdr_status, shdr_add_status, rc);
  12942. status = -ENXIO;
  12943. }
  12944. list_del_init(&hrq->list);
  12945. list_del_init(&drq->list);
  12946. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12947. return status;
  12948. }
  12949. /**
  12950. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  12951. * @phba: The virtual port for which this call being executed.
  12952. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  12953. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  12954. * @xritag: the xritag that ties this io to the SGL pages.
  12955. *
  12956. * This routine will post the sgl pages for the IO that has the xritag
  12957. * that is in the iocbq structure. The xritag is assigned during iocbq
  12958. * creation and persists for as long as the driver is loaded.
  12959. * if the caller has fewer than 256 scatter gather segments to map then
  12960. * pdma_phys_addr1 should be 0.
  12961. * If the caller needs to map more than 256 scatter gather segment then
  12962. * pdma_phys_addr1 should be a valid physical address.
  12963. * physical address for SGLs must be 64 byte aligned.
  12964. * If you are going to map 2 SGL's then the first one must have 256 entries
  12965. * the second sgl can have between 1 and 256 entries.
  12966. *
  12967. * Return codes:
  12968. * 0 - Success
  12969. * -ENXIO, -ENOMEM - Failure
  12970. **/
  12971. int
  12972. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  12973. dma_addr_t pdma_phys_addr0,
  12974. dma_addr_t pdma_phys_addr1,
  12975. uint16_t xritag)
  12976. {
  12977. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  12978. LPFC_MBOXQ_t *mbox;
  12979. int rc;
  12980. uint32_t shdr_status, shdr_add_status;
  12981. uint32_t mbox_tmo;
  12982. union lpfc_sli4_cfg_shdr *shdr;
  12983. if (xritag == NO_XRI) {
  12984. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12985. "0364 Invalid param:\n");
  12986. return -EINVAL;
  12987. }
  12988. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12989. if (!mbox)
  12990. return -ENOMEM;
  12991. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12992. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12993. sizeof(struct lpfc_mbx_post_sgl_pages) -
  12994. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  12995. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  12996. &mbox->u.mqe.un.post_sgl_pages;
  12997. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  12998. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  12999. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  13000. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  13001. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  13002. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  13003. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  13004. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  13005. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  13006. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  13007. if (!phba->sli4_hba.intr_enable)
  13008. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13009. else {
  13010. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  13011. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  13012. }
  13013. /* The IOCTL status is embedded in the mailbox subheader. */
  13014. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  13015. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13016. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13017. if (rc != MBX_TIMEOUT)
  13018. mempool_free(mbox, phba->mbox_mem_pool);
  13019. if (shdr_status || shdr_add_status || rc) {
  13020. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13021. "2511 POST_SGL mailbox failed with "
  13022. "status x%x add_status x%x, mbx status x%x\n",
  13023. shdr_status, shdr_add_status, rc);
  13024. }
  13025. return 0;
  13026. }
  13027. /**
  13028. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  13029. * @phba: pointer to lpfc hba data structure.
  13030. *
  13031. * This routine is invoked to post rpi header templates to the
  13032. * HBA consistent with the SLI-4 interface spec. This routine
  13033. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13034. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13035. *
  13036. * Returns
  13037. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13038. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13039. **/
  13040. static uint16_t
  13041. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  13042. {
  13043. unsigned long xri;
  13044. /*
  13045. * Fetch the next logical xri. Because this index is logical,
  13046. * the driver starts at 0 each time.
  13047. */
  13048. spin_lock_irq(&phba->hbalock);
  13049. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  13050. phba->sli4_hba.max_cfg_param.max_xri, 0);
  13051. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  13052. spin_unlock_irq(&phba->hbalock);
  13053. return NO_XRI;
  13054. } else {
  13055. set_bit(xri, phba->sli4_hba.xri_bmask);
  13056. phba->sli4_hba.max_cfg_param.xri_used++;
  13057. }
  13058. spin_unlock_irq(&phba->hbalock);
  13059. return xri;
  13060. }
  13061. /**
  13062. * lpfc_sli4_free_xri - Release an xri for reuse.
  13063. * @phba: pointer to lpfc hba data structure.
  13064. *
  13065. * This routine is invoked to release an xri to the pool of
  13066. * available rpis maintained by the driver.
  13067. **/
  13068. static void
  13069. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  13070. {
  13071. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  13072. phba->sli4_hba.max_cfg_param.xri_used--;
  13073. }
  13074. }
  13075. /**
  13076. * lpfc_sli4_free_xri - Release an xri for reuse.
  13077. * @phba: pointer to lpfc hba data structure.
  13078. *
  13079. * This routine is invoked to release an xri to the pool of
  13080. * available rpis maintained by the driver.
  13081. **/
  13082. void
  13083. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  13084. {
  13085. spin_lock_irq(&phba->hbalock);
  13086. __lpfc_sli4_free_xri(phba, xri);
  13087. spin_unlock_irq(&phba->hbalock);
  13088. }
  13089. /**
  13090. * lpfc_sli4_next_xritag - Get an xritag for the io
  13091. * @phba: Pointer to HBA context object.
  13092. *
  13093. * This function gets an xritag for the iocb. If there is no unused xritag
  13094. * it will return 0xffff.
  13095. * The function returns the allocated xritag if successful, else returns zero.
  13096. * Zero is not a valid xritag.
  13097. * The caller is not required to hold any lock.
  13098. **/
  13099. uint16_t
  13100. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  13101. {
  13102. uint16_t xri_index;
  13103. xri_index = lpfc_sli4_alloc_xri(phba);
  13104. if (xri_index == NO_XRI)
  13105. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  13106. "2004 Failed to allocate XRI.last XRITAG is %d"
  13107. " Max XRI is %d, Used XRI is %d\n",
  13108. xri_index,
  13109. phba->sli4_hba.max_cfg_param.max_xri,
  13110. phba->sli4_hba.max_cfg_param.xri_used);
  13111. return xri_index;
  13112. }
  13113. /**
  13114. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  13115. * @phba: pointer to lpfc hba data structure.
  13116. * @post_sgl_list: pointer to els sgl entry list.
  13117. * @count: number of els sgl entries on the list.
  13118. *
  13119. * This routine is invoked to post a block of driver's sgl pages to the
  13120. * HBA using non-embedded mailbox command. No Lock is held. This routine
  13121. * is only called when the driver is loading and after all IO has been
  13122. * stopped.
  13123. **/
  13124. static int
  13125. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
  13126. struct list_head *post_sgl_list,
  13127. int post_cnt)
  13128. {
  13129. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  13130. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  13131. struct sgl_page_pairs *sgl_pg_pairs;
  13132. void *viraddr;
  13133. LPFC_MBOXQ_t *mbox;
  13134. uint32_t reqlen, alloclen, pg_pairs;
  13135. uint32_t mbox_tmo;
  13136. uint16_t xritag_start = 0;
  13137. int rc = 0;
  13138. uint32_t shdr_status, shdr_add_status;
  13139. union lpfc_sli4_cfg_shdr *shdr;
  13140. reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
  13141. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  13142. if (reqlen > SLI4_PAGE_SIZE) {
  13143. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  13144. "2559 Block sgl registration required DMA "
  13145. "size (%d) great than a page\n", reqlen);
  13146. return -ENOMEM;
  13147. }
  13148. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13149. if (!mbox)
  13150. return -ENOMEM;
  13151. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13152. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  13153. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  13154. LPFC_SLI4_MBX_NEMBED);
  13155. if (alloclen < reqlen) {
  13156. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13157. "0285 Allocated DMA memory size (%d) is "
  13158. "less than the requested DMA memory "
  13159. "size (%d)\n", alloclen, reqlen);
  13160. lpfc_sli4_mbox_cmd_free(phba, mbox);
  13161. return -ENOMEM;
  13162. }
  13163. /* Set up the SGL pages in the non-embedded DMA pages */
  13164. viraddr = mbox->sge_array->addr[0];
  13165. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  13166. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  13167. pg_pairs = 0;
  13168. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  13169. /* Set up the sge entry */
  13170. sgl_pg_pairs->sgl_pg0_addr_lo =
  13171. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  13172. sgl_pg_pairs->sgl_pg0_addr_hi =
  13173. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  13174. sgl_pg_pairs->sgl_pg1_addr_lo =
  13175. cpu_to_le32(putPaddrLow(0));
  13176. sgl_pg_pairs->sgl_pg1_addr_hi =
  13177. cpu_to_le32(putPaddrHigh(0));
  13178. /* Keep the first xritag on the list */
  13179. if (pg_pairs == 0)
  13180. xritag_start = sglq_entry->sli4_xritag;
  13181. sgl_pg_pairs++;
  13182. pg_pairs++;
  13183. }
  13184. /* Complete initialization and perform endian conversion. */
  13185. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  13186. bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
  13187. sgl->word0 = cpu_to_le32(sgl->word0);
  13188. if (!phba->sli4_hba.intr_enable)
  13189. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13190. else {
  13191. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  13192. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  13193. }
  13194. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  13195. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13196. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13197. if (rc != MBX_TIMEOUT)
  13198. lpfc_sli4_mbox_cmd_free(phba, mbox);
  13199. if (shdr_status || shdr_add_status || rc) {
  13200. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13201. "2513 POST_SGL_BLOCK mailbox command failed "
  13202. "status x%x add_status x%x mbx status x%x\n",
  13203. shdr_status, shdr_add_status, rc);
  13204. rc = -ENXIO;
  13205. }
  13206. return rc;
  13207. }
  13208. /**
  13209. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  13210. * @phba: pointer to lpfc hba data structure.
  13211. * @sblist: pointer to scsi buffer list.
  13212. * @count: number of scsi buffers on the list.
  13213. *
  13214. * This routine is invoked to post a block of @count scsi sgl pages from a
  13215. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  13216. * No Lock is held.
  13217. *
  13218. **/
  13219. int
  13220. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  13221. struct list_head *sblist,
  13222. int count)
  13223. {
  13224. struct lpfc_scsi_buf *psb;
  13225. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  13226. struct sgl_page_pairs *sgl_pg_pairs;
  13227. void *viraddr;
  13228. LPFC_MBOXQ_t *mbox;
  13229. uint32_t reqlen, alloclen, pg_pairs;
  13230. uint32_t mbox_tmo;
  13231. uint16_t xritag_start = 0;
  13232. int rc = 0;
  13233. uint32_t shdr_status, shdr_add_status;
  13234. dma_addr_t pdma_phys_bpl1;
  13235. union lpfc_sli4_cfg_shdr *shdr;
  13236. /* Calculate the requested length of the dma memory */
  13237. reqlen = count * sizeof(struct sgl_page_pairs) +
  13238. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  13239. if (reqlen > SLI4_PAGE_SIZE) {
  13240. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  13241. "0217 Block sgl registration required DMA "
  13242. "size (%d) great than a page\n", reqlen);
  13243. return -ENOMEM;
  13244. }
  13245. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13246. if (!mbox) {
  13247. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13248. "0283 Failed to allocate mbox cmd memory\n");
  13249. return -ENOMEM;
  13250. }
  13251. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13252. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  13253. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  13254. LPFC_SLI4_MBX_NEMBED);
  13255. if (alloclen < reqlen) {
  13256. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13257. "2561 Allocated DMA memory size (%d) is "
  13258. "less than the requested DMA memory "
  13259. "size (%d)\n", alloclen, reqlen);
  13260. lpfc_sli4_mbox_cmd_free(phba, mbox);
  13261. return -ENOMEM;
  13262. }
  13263. /* Get the first SGE entry from the non-embedded DMA memory */
  13264. viraddr = mbox->sge_array->addr[0];
  13265. /* Set up the SGL pages in the non-embedded DMA pages */
  13266. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  13267. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  13268. pg_pairs = 0;
  13269. list_for_each_entry(psb, sblist, list) {
  13270. /* Set up the sge entry */
  13271. sgl_pg_pairs->sgl_pg0_addr_lo =
  13272. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  13273. sgl_pg_pairs->sgl_pg0_addr_hi =
  13274. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  13275. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  13276. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  13277. else
  13278. pdma_phys_bpl1 = 0;
  13279. sgl_pg_pairs->sgl_pg1_addr_lo =
  13280. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  13281. sgl_pg_pairs->sgl_pg1_addr_hi =
  13282. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  13283. /* Keep the first xritag on the list */
  13284. if (pg_pairs == 0)
  13285. xritag_start = psb->cur_iocbq.sli4_xritag;
  13286. sgl_pg_pairs++;
  13287. pg_pairs++;
  13288. }
  13289. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  13290. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  13291. /* Perform endian conversion if necessary */
  13292. sgl->word0 = cpu_to_le32(sgl->word0);
  13293. if (!phba->sli4_hba.intr_enable)
  13294. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13295. else {
  13296. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  13297. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  13298. }
  13299. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  13300. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13301. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13302. if (rc != MBX_TIMEOUT)
  13303. lpfc_sli4_mbox_cmd_free(phba, mbox);
  13304. if (shdr_status || shdr_add_status || rc) {
  13305. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13306. "2564 POST_SGL_BLOCK mailbox command failed "
  13307. "status x%x add_status x%x mbx status x%x\n",
  13308. shdr_status, shdr_add_status, rc);
  13309. rc = -ENXIO;
  13310. }
  13311. return rc;
  13312. }
  13313. /**
  13314. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  13315. * @phba: pointer to lpfc_hba struct that the frame was received on
  13316. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  13317. *
  13318. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  13319. * valid type of frame that the LPFC driver will handle. This function will
  13320. * return a zero if the frame is a valid frame or a non zero value when the
  13321. * frame does not pass the check.
  13322. **/
  13323. static int
  13324. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  13325. {
  13326. /* make rctl_names static to save stack space */
  13327. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  13328. char *type_names[] = FC_TYPE_NAMES_INIT;
  13329. struct fc_vft_header *fc_vft_hdr;
  13330. uint32_t *header = (uint32_t *) fc_hdr;
  13331. switch (fc_hdr->fh_r_ctl) {
  13332. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  13333. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  13334. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  13335. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  13336. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  13337. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  13338. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  13339. case FC_RCTL_DD_CMD_STATUS: /* command status */
  13340. case FC_RCTL_ELS_REQ: /* extended link services request */
  13341. case FC_RCTL_ELS_REP: /* extended link services reply */
  13342. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  13343. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  13344. case FC_RCTL_BA_NOP: /* basic link service NOP */
  13345. case FC_RCTL_BA_ABTS: /* basic link service abort */
  13346. case FC_RCTL_BA_RMC: /* remove connection */
  13347. case FC_RCTL_BA_ACC: /* basic accept */
  13348. case FC_RCTL_BA_RJT: /* basic reject */
  13349. case FC_RCTL_BA_PRMT:
  13350. case FC_RCTL_ACK_1: /* acknowledge_1 */
  13351. case FC_RCTL_ACK_0: /* acknowledge_0 */
  13352. case FC_RCTL_P_RJT: /* port reject */
  13353. case FC_RCTL_F_RJT: /* fabric reject */
  13354. case FC_RCTL_P_BSY: /* port busy */
  13355. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  13356. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  13357. case FC_RCTL_LCR: /* link credit reset */
  13358. case FC_RCTL_END: /* end */
  13359. break;
  13360. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  13361. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  13362. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  13363. return lpfc_fc_frame_check(phba, fc_hdr);
  13364. default:
  13365. goto drop;
  13366. }
  13367. switch (fc_hdr->fh_type) {
  13368. case FC_TYPE_BLS:
  13369. case FC_TYPE_ELS:
  13370. case FC_TYPE_FCP:
  13371. case FC_TYPE_CT:
  13372. break;
  13373. case FC_TYPE_IP:
  13374. case FC_TYPE_ILS:
  13375. default:
  13376. goto drop;
  13377. }
  13378. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  13379. "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
  13380. "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
  13381. rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
  13382. type_names[fc_hdr->fh_type], fc_hdr->fh_type,
  13383. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  13384. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  13385. be32_to_cpu(header[4]), be32_to_cpu(header[5]),
  13386. be32_to_cpu(header[6]));
  13387. return 0;
  13388. drop:
  13389. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  13390. "2539 Dropped frame rctl:%s type:%s\n",
  13391. rctl_names[fc_hdr->fh_r_ctl],
  13392. type_names[fc_hdr->fh_type]);
  13393. return 1;
  13394. }
  13395. /**
  13396. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  13397. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  13398. *
  13399. * This function processes the FC header to retrieve the VFI from the VF
  13400. * header, if one exists. This function will return the VFI if one exists
  13401. * or 0 if no VSAN Header exists.
  13402. **/
  13403. static uint32_t
  13404. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  13405. {
  13406. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  13407. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  13408. return 0;
  13409. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  13410. }
  13411. /**
  13412. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  13413. * @phba: Pointer to the HBA structure to search for the vport on
  13414. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  13415. * @fcfi: The FC Fabric ID that the frame came from
  13416. *
  13417. * This function searches the @phba for a vport that matches the content of the
  13418. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  13419. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  13420. * returns the matching vport pointer or NULL if unable to match frame to a
  13421. * vport.
  13422. **/
  13423. static struct lpfc_vport *
  13424. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  13425. uint16_t fcfi)
  13426. {
  13427. struct lpfc_vport **vports;
  13428. struct lpfc_vport *vport = NULL;
  13429. int i;
  13430. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  13431. fc_hdr->fh_d_id[1] << 8 |
  13432. fc_hdr->fh_d_id[2]);
  13433. if (did == Fabric_DID)
  13434. return phba->pport;
  13435. if ((phba->pport->fc_flag & FC_PT2PT) &&
  13436. !(phba->link_state == LPFC_HBA_READY))
  13437. return phba->pport;
  13438. vports = lpfc_create_vport_work_array(phba);
  13439. if (vports != NULL)
  13440. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  13441. if (phba->fcf.fcfi == fcfi &&
  13442. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  13443. vports[i]->fc_myDID == did) {
  13444. vport = vports[i];
  13445. break;
  13446. }
  13447. }
  13448. lpfc_destroy_vport_work_array(phba, vports);
  13449. return vport;
  13450. }
  13451. /**
  13452. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  13453. * @vport: The vport to work on.
  13454. *
  13455. * This function updates the receive sequence time stamp for this vport. The
  13456. * receive sequence time stamp indicates the time that the last frame of the
  13457. * the sequence that has been idle for the longest amount of time was received.
  13458. * the driver uses this time stamp to indicate if any received sequences have
  13459. * timed out.
  13460. **/
  13461. static void
  13462. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  13463. {
  13464. struct lpfc_dmabuf *h_buf;
  13465. struct hbq_dmabuf *dmabuf = NULL;
  13466. /* get the oldest sequence on the rcv list */
  13467. h_buf = list_get_first(&vport->rcv_buffer_list,
  13468. struct lpfc_dmabuf, list);
  13469. if (!h_buf)
  13470. return;
  13471. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13472. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  13473. }
  13474. /**
  13475. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  13476. * @vport: The vport that the received sequences were sent to.
  13477. *
  13478. * This function cleans up all outstanding received sequences. This is called
  13479. * by the driver when a link event or user action invalidates all the received
  13480. * sequences.
  13481. **/
  13482. void
  13483. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  13484. {
  13485. struct lpfc_dmabuf *h_buf, *hnext;
  13486. struct lpfc_dmabuf *d_buf, *dnext;
  13487. struct hbq_dmabuf *dmabuf = NULL;
  13488. /* start with the oldest sequence on the rcv list */
  13489. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  13490. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13491. list_del_init(&dmabuf->hbuf.list);
  13492. list_for_each_entry_safe(d_buf, dnext,
  13493. &dmabuf->dbuf.list, list) {
  13494. list_del_init(&d_buf->list);
  13495. lpfc_in_buf_free(vport->phba, d_buf);
  13496. }
  13497. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  13498. }
  13499. }
  13500. /**
  13501. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  13502. * @vport: The vport that the received sequences were sent to.
  13503. *
  13504. * This function determines whether any received sequences have timed out by
  13505. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  13506. * indicates that there is at least one timed out sequence this routine will
  13507. * go through the received sequences one at a time from most inactive to most
  13508. * active to determine which ones need to be cleaned up. Once it has determined
  13509. * that a sequence needs to be cleaned up it will simply free up the resources
  13510. * without sending an abort.
  13511. **/
  13512. void
  13513. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  13514. {
  13515. struct lpfc_dmabuf *h_buf, *hnext;
  13516. struct lpfc_dmabuf *d_buf, *dnext;
  13517. struct hbq_dmabuf *dmabuf = NULL;
  13518. unsigned long timeout;
  13519. int abort_count = 0;
  13520. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  13521. vport->rcv_buffer_time_stamp);
  13522. if (list_empty(&vport->rcv_buffer_list) ||
  13523. time_before(jiffies, timeout))
  13524. return;
  13525. /* start with the oldest sequence on the rcv list */
  13526. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  13527. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13528. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  13529. dmabuf->time_stamp);
  13530. if (time_before(jiffies, timeout))
  13531. break;
  13532. abort_count++;
  13533. list_del_init(&dmabuf->hbuf.list);
  13534. list_for_each_entry_safe(d_buf, dnext,
  13535. &dmabuf->dbuf.list, list) {
  13536. list_del_init(&d_buf->list);
  13537. lpfc_in_buf_free(vport->phba, d_buf);
  13538. }
  13539. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  13540. }
  13541. if (abort_count)
  13542. lpfc_update_rcv_time_stamp(vport);
  13543. }
  13544. /**
  13545. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  13546. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  13547. *
  13548. * This function searches through the existing incomplete sequences that have
  13549. * been sent to this @vport. If the frame matches one of the incomplete
  13550. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  13551. * make up that sequence. If no sequence is found that matches this frame then
  13552. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  13553. * This function returns a pointer to the first dmabuf in the sequence list that
  13554. * the frame was linked to.
  13555. **/
  13556. static struct hbq_dmabuf *
  13557. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  13558. {
  13559. struct fc_frame_header *new_hdr;
  13560. struct fc_frame_header *temp_hdr;
  13561. struct lpfc_dmabuf *d_buf;
  13562. struct lpfc_dmabuf *h_buf;
  13563. struct hbq_dmabuf *seq_dmabuf = NULL;
  13564. struct hbq_dmabuf *temp_dmabuf = NULL;
  13565. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  13566. dmabuf->time_stamp = jiffies;
  13567. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13568. /* Use the hdr_buf to find the sequence that this frame belongs to */
  13569. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  13570. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  13571. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  13572. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  13573. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  13574. continue;
  13575. /* found a pending sequence that matches this frame */
  13576. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13577. break;
  13578. }
  13579. if (!seq_dmabuf) {
  13580. /*
  13581. * This indicates first frame received for this sequence.
  13582. * Queue the buffer on the vport's rcv_buffer_list.
  13583. */
  13584. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  13585. lpfc_update_rcv_time_stamp(vport);
  13586. return dmabuf;
  13587. }
  13588. temp_hdr = seq_dmabuf->hbuf.virt;
  13589. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  13590. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  13591. list_del_init(&seq_dmabuf->hbuf.list);
  13592. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  13593. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  13594. lpfc_update_rcv_time_stamp(vport);
  13595. return dmabuf;
  13596. }
  13597. /* move this sequence to the tail to indicate a young sequence */
  13598. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  13599. seq_dmabuf->time_stamp = jiffies;
  13600. lpfc_update_rcv_time_stamp(vport);
  13601. if (list_empty(&seq_dmabuf->dbuf.list)) {
  13602. temp_hdr = dmabuf->hbuf.virt;
  13603. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  13604. return seq_dmabuf;
  13605. }
  13606. /* find the correct place in the sequence to insert this frame */
  13607. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  13608. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13609. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  13610. /*
  13611. * If the frame's sequence count is greater than the frame on
  13612. * the list then insert the frame right after this frame
  13613. */
  13614. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  13615. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  13616. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  13617. return seq_dmabuf;
  13618. }
  13619. }
  13620. return NULL;
  13621. }
  13622. /**
  13623. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  13624. * @vport: pointer to a vitural port
  13625. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13626. *
  13627. * This function tries to abort from the partially assembed sequence, described
  13628. * by the information from basic abbort @dmabuf. It checks to see whether such
  13629. * partially assembled sequence held by the driver. If so, it shall free up all
  13630. * the frames from the partially assembled sequence.
  13631. *
  13632. * Return
  13633. * true -- if there is matching partially assembled sequence present and all
  13634. * the frames freed with the sequence;
  13635. * false -- if there is no matching partially assembled sequence present so
  13636. * nothing got aborted in the lower layer driver
  13637. **/
  13638. static bool
  13639. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  13640. struct hbq_dmabuf *dmabuf)
  13641. {
  13642. struct fc_frame_header *new_hdr;
  13643. struct fc_frame_header *temp_hdr;
  13644. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  13645. struct hbq_dmabuf *seq_dmabuf = NULL;
  13646. /* Use the hdr_buf to find the sequence that matches this frame */
  13647. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  13648. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  13649. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13650. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  13651. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  13652. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  13653. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  13654. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  13655. continue;
  13656. /* found a pending sequence that matches this frame */
  13657. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13658. break;
  13659. }
  13660. /* Free up all the frames from the partially assembled sequence */
  13661. if (seq_dmabuf) {
  13662. list_for_each_entry_safe(d_buf, n_buf,
  13663. &seq_dmabuf->dbuf.list, list) {
  13664. list_del_init(&d_buf->list);
  13665. lpfc_in_buf_free(vport->phba, d_buf);
  13666. }
  13667. return true;
  13668. }
  13669. return false;
  13670. }
  13671. /**
  13672. * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
  13673. * @vport: pointer to a vitural port
  13674. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13675. *
  13676. * This function tries to abort from the assembed sequence from upper level
  13677. * protocol, described by the information from basic abbort @dmabuf. It
  13678. * checks to see whether such pending context exists at upper level protocol.
  13679. * If so, it shall clean up the pending context.
  13680. *
  13681. * Return
  13682. * true -- if there is matching pending context of the sequence cleaned
  13683. * at ulp;
  13684. * false -- if there is no matching pending context of the sequence present
  13685. * at ulp.
  13686. **/
  13687. static bool
  13688. lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  13689. {
  13690. struct lpfc_hba *phba = vport->phba;
  13691. int handled;
  13692. /* Accepting abort at ulp with SLI4 only */
  13693. if (phba->sli_rev < LPFC_SLI_REV4)
  13694. return false;
  13695. /* Register all caring upper level protocols to attend abort */
  13696. handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
  13697. if (handled)
  13698. return true;
  13699. return false;
  13700. }
  13701. /**
  13702. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  13703. * @phba: Pointer to HBA context object.
  13704. * @cmd_iocbq: pointer to the command iocbq structure.
  13705. * @rsp_iocbq: pointer to the response iocbq structure.
  13706. *
  13707. * This function handles the sequence abort response iocb command complete
  13708. * event. It properly releases the memory allocated to the sequence abort
  13709. * accept iocb.
  13710. **/
  13711. static void
  13712. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  13713. struct lpfc_iocbq *cmd_iocbq,
  13714. struct lpfc_iocbq *rsp_iocbq)
  13715. {
  13716. struct lpfc_nodelist *ndlp;
  13717. if (cmd_iocbq) {
  13718. ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
  13719. lpfc_nlp_put(ndlp);
  13720. lpfc_nlp_not_used(ndlp);
  13721. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  13722. }
  13723. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  13724. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  13725. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13726. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  13727. rsp_iocbq->iocb.ulpStatus,
  13728. rsp_iocbq->iocb.un.ulpWord[4]);
  13729. }
  13730. /**
  13731. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  13732. * @phba: Pointer to HBA context object.
  13733. * @xri: xri id in transaction.
  13734. *
  13735. * This function validates the xri maps to the known range of XRIs allocated an
  13736. * used by the driver.
  13737. **/
  13738. uint16_t
  13739. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  13740. uint16_t xri)
  13741. {
  13742. uint16_t i;
  13743. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  13744. if (xri == phba->sli4_hba.xri_ids[i])
  13745. return i;
  13746. }
  13747. return NO_XRI;
  13748. }
  13749. /**
  13750. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  13751. * @phba: Pointer to HBA context object.
  13752. * @fc_hdr: pointer to a FC frame header.
  13753. *
  13754. * This function sends a basic response to a previous unsol sequence abort
  13755. * event after aborting the sequence handling.
  13756. **/
  13757. static void
  13758. lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
  13759. struct fc_frame_header *fc_hdr, bool aborted)
  13760. {
  13761. struct lpfc_hba *phba = vport->phba;
  13762. struct lpfc_iocbq *ctiocb = NULL;
  13763. struct lpfc_nodelist *ndlp;
  13764. uint16_t oxid, rxid, xri, lxri;
  13765. uint32_t sid, fctl;
  13766. IOCB_t *icmd;
  13767. int rc;
  13768. if (!lpfc_is_link_up(phba))
  13769. return;
  13770. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13771. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  13772. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  13773. ndlp = lpfc_findnode_did(vport, sid);
  13774. if (!ndlp) {
  13775. ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
  13776. if (!ndlp) {
  13777. lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
  13778. "1268 Failed to allocate ndlp for "
  13779. "oxid:x%x SID:x%x\n", oxid, sid);
  13780. return;
  13781. }
  13782. lpfc_nlp_init(vport, ndlp, sid);
  13783. /* Put ndlp onto pport node list */
  13784. lpfc_enqueue_node(vport, ndlp);
  13785. } else if (!NLP_CHK_NODE_ACT(ndlp)) {
  13786. /* re-setup ndlp without removing from node list */
  13787. ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
  13788. if (!ndlp) {
  13789. lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
  13790. "3275 Failed to active ndlp found "
  13791. "for oxid:x%x SID:x%x\n", oxid, sid);
  13792. return;
  13793. }
  13794. }
  13795. /* Allocate buffer for rsp iocb */
  13796. ctiocb = lpfc_sli_get_iocbq(phba);
  13797. if (!ctiocb)
  13798. return;
  13799. /* Extract the F_CTL field from FC_HDR */
  13800. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  13801. icmd = &ctiocb->iocb;
  13802. icmd->un.xseq64.bdl.bdeSize = 0;
  13803. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  13804. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  13805. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  13806. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  13807. /* Fill in the rest of iocb fields */
  13808. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  13809. icmd->ulpBdeCount = 0;
  13810. icmd->ulpLe = 1;
  13811. icmd->ulpClass = CLASS3;
  13812. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  13813. ctiocb->context1 = lpfc_nlp_get(ndlp);
  13814. ctiocb->iocb_cmpl = NULL;
  13815. ctiocb->vport = phba->pport;
  13816. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  13817. ctiocb->sli4_lxritag = NO_XRI;
  13818. ctiocb->sli4_xritag = NO_XRI;
  13819. if (fctl & FC_FC_EX_CTX)
  13820. /* Exchange responder sent the abort so we
  13821. * own the oxid.
  13822. */
  13823. xri = oxid;
  13824. else
  13825. xri = rxid;
  13826. lxri = lpfc_sli4_xri_inrange(phba, xri);
  13827. if (lxri != NO_XRI)
  13828. lpfc_set_rrq_active(phba, ndlp, lxri,
  13829. (xri == oxid) ? rxid : oxid, 0);
  13830. /* For BA_ABTS from exchange responder, if the logical xri with
  13831. * the oxid maps to the FCP XRI range, the port no longer has
  13832. * that exchange context, send a BLS_RJT. Override the IOCB for
  13833. * a BA_RJT.
  13834. */
  13835. if ((fctl & FC_FC_EX_CTX) &&
  13836. (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
  13837. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  13838. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  13839. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  13840. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  13841. }
  13842. /* If BA_ABTS failed to abort a partially assembled receive sequence,
  13843. * the driver no longer has that exchange, send a BLS_RJT. Override
  13844. * the IOCB for a BA_RJT.
  13845. */
  13846. if (aborted == false) {
  13847. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  13848. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  13849. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  13850. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  13851. }
  13852. if (fctl & FC_FC_EX_CTX) {
  13853. /* ABTS sent by responder to CT exchange, construction
  13854. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  13855. * field and RX_ID from ABTS for RX_ID field.
  13856. */
  13857. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  13858. } else {
  13859. /* ABTS sent by initiator to CT exchange, construction
  13860. * of BA_ACC will need to allocate a new XRI as for the
  13861. * XRI_TAG field.
  13862. */
  13863. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  13864. }
  13865. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  13866. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  13867. /* Xmit CT abts response on exchange <xid> */
  13868. lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
  13869. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  13870. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  13871. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  13872. if (rc == IOCB_ERROR) {
  13873. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  13874. "2925 Failed to issue CT ABTS RSP x%x on "
  13875. "xri x%x, Data x%x\n",
  13876. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  13877. phba->link_state);
  13878. lpfc_nlp_put(ndlp);
  13879. ctiocb->context1 = NULL;
  13880. lpfc_sli_release_iocbq(phba, ctiocb);
  13881. }
  13882. }
  13883. /**
  13884. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  13885. * @vport: Pointer to the vport on which this sequence was received
  13886. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13887. *
  13888. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  13889. * receive sequence is only partially assembed by the driver, it shall abort
  13890. * the partially assembled frames for the sequence. Otherwise, if the
  13891. * unsolicited receive sequence has been completely assembled and passed to
  13892. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  13893. * unsolicited sequence has been aborted. After that, it will issue a basic
  13894. * accept to accept the abort.
  13895. **/
  13896. static void
  13897. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  13898. struct hbq_dmabuf *dmabuf)
  13899. {
  13900. struct lpfc_hba *phba = vport->phba;
  13901. struct fc_frame_header fc_hdr;
  13902. uint32_t fctl;
  13903. bool aborted;
  13904. /* Make a copy of fc_hdr before the dmabuf being released */
  13905. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  13906. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  13907. if (fctl & FC_FC_EX_CTX) {
  13908. /* ABTS by responder to exchange, no cleanup needed */
  13909. aborted = true;
  13910. } else {
  13911. /* ABTS by initiator to exchange, need to do cleanup */
  13912. aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  13913. if (aborted == false)
  13914. aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
  13915. }
  13916. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13917. /* Respond with BA_ACC or BA_RJT accordingly */
  13918. lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
  13919. }
  13920. /**
  13921. * lpfc_seq_complete - Indicates if a sequence is complete
  13922. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13923. *
  13924. * This function checks the sequence, starting with the frame described by
  13925. * @dmabuf, to see if all the frames associated with this sequence are present.
  13926. * the frames associated with this sequence are linked to the @dmabuf using the
  13927. * dbuf list. This function looks for two major things. 1) That the first frame
  13928. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  13929. * set. 3) That there are no holes in the sequence count. The function will
  13930. * return 1 when the sequence is complete, otherwise it will return 0.
  13931. **/
  13932. static int
  13933. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  13934. {
  13935. struct fc_frame_header *hdr;
  13936. struct lpfc_dmabuf *d_buf;
  13937. struct hbq_dmabuf *seq_dmabuf;
  13938. uint32_t fctl;
  13939. int seq_count = 0;
  13940. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13941. /* make sure first fame of sequence has a sequence count of zero */
  13942. if (hdr->fh_seq_cnt != seq_count)
  13943. return 0;
  13944. fctl = (hdr->fh_f_ctl[0] << 16 |
  13945. hdr->fh_f_ctl[1] << 8 |
  13946. hdr->fh_f_ctl[2]);
  13947. /* If last frame of sequence we can return success. */
  13948. if (fctl & FC_FC_END_SEQ)
  13949. return 1;
  13950. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  13951. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13952. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13953. /* If there is a hole in the sequence count then fail. */
  13954. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  13955. return 0;
  13956. fctl = (hdr->fh_f_ctl[0] << 16 |
  13957. hdr->fh_f_ctl[1] << 8 |
  13958. hdr->fh_f_ctl[2]);
  13959. /* If last frame of sequence we can return success. */
  13960. if (fctl & FC_FC_END_SEQ)
  13961. return 1;
  13962. }
  13963. return 0;
  13964. }
  13965. /**
  13966. * lpfc_prep_seq - Prep sequence for ULP processing
  13967. * @vport: Pointer to the vport on which this sequence was received
  13968. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13969. *
  13970. * This function takes a sequence, described by a list of frames, and creates
  13971. * a list of iocbq structures to describe the sequence. This iocbq list will be
  13972. * used to issue to the generic unsolicited sequence handler. This routine
  13973. * returns a pointer to the first iocbq in the list. If the function is unable
  13974. * to allocate an iocbq then it throw out the received frames that were not
  13975. * able to be described and return a pointer to the first iocbq. If unable to
  13976. * allocate any iocbqs (including the first) this function will return NULL.
  13977. **/
  13978. static struct lpfc_iocbq *
  13979. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  13980. {
  13981. struct hbq_dmabuf *hbq_buf;
  13982. struct lpfc_dmabuf *d_buf, *n_buf;
  13983. struct lpfc_iocbq *first_iocbq, *iocbq;
  13984. struct fc_frame_header *fc_hdr;
  13985. uint32_t sid;
  13986. uint32_t len, tot_len;
  13987. struct ulp_bde64 *pbde;
  13988. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13989. /* remove from receive buffer list */
  13990. list_del_init(&seq_dmabuf->hbuf.list);
  13991. lpfc_update_rcv_time_stamp(vport);
  13992. /* get the Remote Port's SID */
  13993. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13994. tot_len = 0;
  13995. /* Get an iocbq struct to fill in. */
  13996. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  13997. if (first_iocbq) {
  13998. /* Initialize the first IOCB. */
  13999. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  14000. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  14001. /* Check FC Header to see what TYPE of frame we are rcv'ing */
  14002. if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
  14003. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
  14004. first_iocbq->iocb.un.rcvels.parmRo =
  14005. sli4_did_from_fc_hdr(fc_hdr);
  14006. first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
  14007. } else
  14008. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  14009. first_iocbq->iocb.ulpContext = NO_XRI;
  14010. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  14011. be16_to_cpu(fc_hdr->fh_ox_id);
  14012. /* iocbq is prepped for internal consumption. Physical vpi. */
  14013. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  14014. vport->phba->vpi_ids[vport->vpi];
  14015. /* put the first buffer into the first IOCBq */
  14016. tot_len = bf_get(lpfc_rcqe_length,
  14017. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  14018. first_iocbq->context2 = &seq_dmabuf->dbuf;
  14019. first_iocbq->context3 = NULL;
  14020. first_iocbq->iocb.ulpBdeCount = 1;
  14021. if (tot_len > LPFC_DATA_BUF_SIZE)
  14022. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  14023. LPFC_DATA_BUF_SIZE;
  14024. else
  14025. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
  14026. first_iocbq->iocb.un.rcvels.remoteID = sid;
  14027. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  14028. }
  14029. iocbq = first_iocbq;
  14030. /*
  14031. * Each IOCBq can have two Buffers assigned, so go through the list
  14032. * of buffers for this sequence and save two buffers in each IOCBq
  14033. */
  14034. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  14035. if (!iocbq) {
  14036. lpfc_in_buf_free(vport->phba, d_buf);
  14037. continue;
  14038. }
  14039. if (!iocbq->context3) {
  14040. iocbq->context3 = d_buf;
  14041. iocbq->iocb.ulpBdeCount++;
  14042. /* We need to get the size out of the right CQE */
  14043. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  14044. len = bf_get(lpfc_rcqe_length,
  14045. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  14046. pbde = (struct ulp_bde64 *)
  14047. &iocbq->iocb.unsli3.sli3Words[4];
  14048. if (len > LPFC_DATA_BUF_SIZE)
  14049. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  14050. else
  14051. pbde->tus.f.bdeSize = len;
  14052. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  14053. tot_len += len;
  14054. } else {
  14055. iocbq = lpfc_sli_get_iocbq(vport->phba);
  14056. if (!iocbq) {
  14057. if (first_iocbq) {
  14058. first_iocbq->iocb.ulpStatus =
  14059. IOSTAT_FCP_RSP_ERROR;
  14060. first_iocbq->iocb.un.ulpWord[4] =
  14061. IOERR_NO_RESOURCES;
  14062. }
  14063. lpfc_in_buf_free(vport->phba, d_buf);
  14064. continue;
  14065. }
  14066. /* We need to get the size out of the right CQE */
  14067. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  14068. len = bf_get(lpfc_rcqe_length,
  14069. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  14070. iocbq->context2 = d_buf;
  14071. iocbq->context3 = NULL;
  14072. iocbq->iocb.ulpBdeCount = 1;
  14073. if (len > LPFC_DATA_BUF_SIZE)
  14074. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  14075. LPFC_DATA_BUF_SIZE;
  14076. else
  14077. iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
  14078. tot_len += len;
  14079. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  14080. iocbq->iocb.un.rcvels.remoteID = sid;
  14081. list_add_tail(&iocbq->list, &first_iocbq->list);
  14082. }
  14083. }
  14084. return first_iocbq;
  14085. }
  14086. static void
  14087. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  14088. struct hbq_dmabuf *seq_dmabuf)
  14089. {
  14090. struct fc_frame_header *fc_hdr;
  14091. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  14092. struct lpfc_hba *phba = vport->phba;
  14093. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  14094. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  14095. if (!iocbq) {
  14096. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14097. "2707 Ring %d handler: Failed to allocate "
  14098. "iocb Rctl x%x Type x%x received\n",
  14099. LPFC_ELS_RING,
  14100. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  14101. return;
  14102. }
  14103. if (!lpfc_complete_unsol_iocb(phba,
  14104. &phba->sli.ring[LPFC_ELS_RING],
  14105. iocbq, fc_hdr->fh_r_ctl,
  14106. fc_hdr->fh_type))
  14107. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14108. "2540 Ring %d handler: unexpected Rctl "
  14109. "x%x Type x%x received\n",
  14110. LPFC_ELS_RING,
  14111. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  14112. /* Free iocb created in lpfc_prep_seq */
  14113. list_for_each_entry_safe(curr_iocb, next_iocb,
  14114. &iocbq->list, list) {
  14115. list_del_init(&curr_iocb->list);
  14116. lpfc_sli_release_iocbq(phba, curr_iocb);
  14117. }
  14118. lpfc_sli_release_iocbq(phba, iocbq);
  14119. }
  14120. /**
  14121. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  14122. * @phba: Pointer to HBA context object.
  14123. *
  14124. * This function is called with no lock held. This function processes all
  14125. * the received buffers and gives it to upper layers when a received buffer
  14126. * indicates that it is the final frame in the sequence. The interrupt
  14127. * service routine processes received buffers at interrupt contexts and adds
  14128. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  14129. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  14130. * appropriate receive function when the final frame in a sequence is received.
  14131. **/
  14132. void
  14133. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  14134. struct hbq_dmabuf *dmabuf)
  14135. {
  14136. struct hbq_dmabuf *seq_dmabuf;
  14137. struct fc_frame_header *fc_hdr;
  14138. struct lpfc_vport *vport;
  14139. uint32_t fcfi;
  14140. uint32_t did;
  14141. /* Process each received buffer */
  14142. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  14143. /* check to see if this a valid type of frame */
  14144. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  14145. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  14146. return;
  14147. }
  14148. if ((bf_get(lpfc_cqe_code,
  14149. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  14150. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  14151. &dmabuf->cq_event.cqe.rcqe_cmpl);
  14152. else
  14153. fcfi = bf_get(lpfc_rcqe_fcf_id,
  14154. &dmabuf->cq_event.cqe.rcqe_cmpl);
  14155. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  14156. if (!vport) {
  14157. /* throw out the frame */
  14158. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  14159. return;
  14160. }
  14161. /* d_id this frame is directed to */
  14162. did = sli4_did_from_fc_hdr(fc_hdr);
  14163. /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
  14164. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
  14165. (did != Fabric_DID)) {
  14166. /*
  14167. * Throw out the frame if we are not pt2pt.
  14168. * The pt2pt protocol allows for discovery frames
  14169. * to be received without a registered VPI.
  14170. */
  14171. if (!(vport->fc_flag & FC_PT2PT) ||
  14172. (phba->link_state == LPFC_HBA_READY)) {
  14173. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  14174. return;
  14175. }
  14176. }
  14177. /* Handle the basic abort sequence (BA_ABTS) event */
  14178. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  14179. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  14180. return;
  14181. }
  14182. /* Link this frame */
  14183. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  14184. if (!seq_dmabuf) {
  14185. /* unable to add frame to vport - throw it out */
  14186. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  14187. return;
  14188. }
  14189. /* If not last frame in sequence continue processing frames. */
  14190. if (!lpfc_seq_complete(seq_dmabuf))
  14191. return;
  14192. /* Send the complete sequence to the upper layer protocol */
  14193. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  14194. }
  14195. /**
  14196. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  14197. * @phba: pointer to lpfc hba data structure.
  14198. *
  14199. * This routine is invoked to post rpi header templates to the
  14200. * HBA consistent with the SLI-4 interface spec. This routine
  14201. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  14202. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  14203. *
  14204. * This routine does not require any locks. It's usage is expected
  14205. * to be driver load or reset recovery when the driver is
  14206. * sequential.
  14207. *
  14208. * Return codes
  14209. * 0 - successful
  14210. * -EIO - The mailbox failed to complete successfully.
  14211. * When this error occurs, the driver is not guaranteed
  14212. * to have any rpi regions posted to the device and
  14213. * must either attempt to repost the regions or take a
  14214. * fatal error.
  14215. **/
  14216. int
  14217. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  14218. {
  14219. struct lpfc_rpi_hdr *rpi_page;
  14220. uint32_t rc = 0;
  14221. uint16_t lrpi = 0;
  14222. /* SLI4 ports that support extents do not require RPI headers. */
  14223. if (!phba->sli4_hba.rpi_hdrs_in_use)
  14224. goto exit;
  14225. if (phba->sli4_hba.extents_in_use)
  14226. return -EIO;
  14227. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  14228. /*
  14229. * Assign the rpi headers a physical rpi only if the driver
  14230. * has not initialized those resources. A port reset only
  14231. * needs the headers posted.
  14232. */
  14233. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  14234. LPFC_RPI_RSRC_RDY)
  14235. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  14236. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  14237. if (rc != MBX_SUCCESS) {
  14238. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14239. "2008 Error %d posting all rpi "
  14240. "headers\n", rc);
  14241. rc = -EIO;
  14242. break;
  14243. }
  14244. }
  14245. exit:
  14246. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  14247. LPFC_RPI_RSRC_RDY);
  14248. return rc;
  14249. }
  14250. /**
  14251. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  14252. * @phba: pointer to lpfc hba data structure.
  14253. * @rpi_page: pointer to the rpi memory region.
  14254. *
  14255. * This routine is invoked to post a single rpi header to the
  14256. * HBA consistent with the SLI-4 interface spec. This memory region
  14257. * maps up to 64 rpi context regions.
  14258. *
  14259. * Return codes
  14260. * 0 - successful
  14261. * -ENOMEM - No available memory
  14262. * -EIO - The mailbox failed to complete successfully.
  14263. **/
  14264. int
  14265. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  14266. {
  14267. LPFC_MBOXQ_t *mboxq;
  14268. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  14269. uint32_t rc = 0;
  14270. uint32_t shdr_status, shdr_add_status;
  14271. union lpfc_sli4_cfg_shdr *shdr;
  14272. /* SLI4 ports that support extents do not require RPI headers. */
  14273. if (!phba->sli4_hba.rpi_hdrs_in_use)
  14274. return rc;
  14275. if (phba->sli4_hba.extents_in_use)
  14276. return -EIO;
  14277. /* The port is notified of the header region via a mailbox command. */
  14278. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14279. if (!mboxq) {
  14280. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14281. "2001 Unable to allocate memory for issuing "
  14282. "SLI_CONFIG_SPECIAL mailbox command\n");
  14283. return -ENOMEM;
  14284. }
  14285. /* Post all rpi memory regions to the port. */
  14286. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  14287. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  14288. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  14289. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  14290. sizeof(struct lpfc_sli4_cfg_mhdr),
  14291. LPFC_SLI4_MBX_EMBED);
  14292. /* Post the physical rpi to the port for this rpi header. */
  14293. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  14294. rpi_page->start_rpi);
  14295. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  14296. hdr_tmpl, rpi_page->page_count);
  14297. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  14298. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  14299. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14300. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  14301. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14302. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14303. if (rc != MBX_TIMEOUT)
  14304. mempool_free(mboxq, phba->mbox_mem_pool);
  14305. if (shdr_status || shdr_add_status || rc) {
  14306. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14307. "2514 POST_RPI_HDR mailbox failed with "
  14308. "status x%x add_status x%x, mbx status x%x\n",
  14309. shdr_status, shdr_add_status, rc);
  14310. rc = -ENXIO;
  14311. }
  14312. return rc;
  14313. }
  14314. /**
  14315. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  14316. * @phba: pointer to lpfc hba data structure.
  14317. *
  14318. * This routine is invoked to post rpi header templates to the
  14319. * HBA consistent with the SLI-4 interface spec. This routine
  14320. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  14321. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  14322. *
  14323. * Returns
  14324. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  14325. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  14326. **/
  14327. int
  14328. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  14329. {
  14330. unsigned long rpi;
  14331. uint16_t max_rpi, rpi_limit;
  14332. uint16_t rpi_remaining, lrpi = 0;
  14333. struct lpfc_rpi_hdr *rpi_hdr;
  14334. unsigned long iflag;
  14335. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  14336. rpi_limit = phba->sli4_hba.next_rpi;
  14337. /*
  14338. * Fetch the next logical rpi. Because this index is logical,
  14339. * the driver starts at 0 each time.
  14340. */
  14341. spin_lock_irqsave(&phba->hbalock, iflag);
  14342. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  14343. if (rpi >= rpi_limit)
  14344. rpi = LPFC_RPI_ALLOC_ERROR;
  14345. else {
  14346. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  14347. phba->sli4_hba.max_cfg_param.rpi_used++;
  14348. phba->sli4_hba.rpi_count++;
  14349. }
  14350. /*
  14351. * Don't try to allocate more rpi header regions if the device limit
  14352. * has been exhausted.
  14353. */
  14354. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  14355. (phba->sli4_hba.rpi_count >= max_rpi)) {
  14356. spin_unlock_irqrestore(&phba->hbalock, iflag);
  14357. return rpi;
  14358. }
  14359. /*
  14360. * RPI header postings are not required for SLI4 ports capable of
  14361. * extents.
  14362. */
  14363. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  14364. spin_unlock_irqrestore(&phba->hbalock, iflag);
  14365. return rpi;
  14366. }
  14367. /*
  14368. * If the driver is running low on rpi resources, allocate another
  14369. * page now. Note that the next_rpi value is used because
  14370. * it represents how many are actually in use whereas max_rpi notes
  14371. * how many are supported max by the device.
  14372. */
  14373. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  14374. spin_unlock_irqrestore(&phba->hbalock, iflag);
  14375. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  14376. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  14377. if (!rpi_hdr) {
  14378. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14379. "2002 Error Could not grow rpi "
  14380. "count\n");
  14381. } else {
  14382. lrpi = rpi_hdr->start_rpi;
  14383. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  14384. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  14385. }
  14386. }
  14387. return rpi;
  14388. }
  14389. /**
  14390. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  14391. * @phba: pointer to lpfc hba data structure.
  14392. *
  14393. * This routine is invoked to release an rpi to the pool of
  14394. * available rpis maintained by the driver.
  14395. **/
  14396. static void
  14397. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  14398. {
  14399. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  14400. phba->sli4_hba.rpi_count--;
  14401. phba->sli4_hba.max_cfg_param.rpi_used--;
  14402. }
  14403. }
  14404. /**
  14405. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  14406. * @phba: pointer to lpfc hba data structure.
  14407. *
  14408. * This routine is invoked to release an rpi to the pool of
  14409. * available rpis maintained by the driver.
  14410. **/
  14411. void
  14412. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  14413. {
  14414. spin_lock_irq(&phba->hbalock);
  14415. __lpfc_sli4_free_rpi(phba, rpi);
  14416. spin_unlock_irq(&phba->hbalock);
  14417. }
  14418. /**
  14419. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  14420. * @phba: pointer to lpfc hba data structure.
  14421. *
  14422. * This routine is invoked to remove the memory region that
  14423. * provided rpi via a bitmask.
  14424. **/
  14425. void
  14426. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  14427. {
  14428. kfree(phba->sli4_hba.rpi_bmask);
  14429. kfree(phba->sli4_hba.rpi_ids);
  14430. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  14431. }
  14432. /**
  14433. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  14434. * @phba: pointer to lpfc hba data structure.
  14435. *
  14436. * This routine is invoked to remove the memory region that
  14437. * provided rpi via a bitmask.
  14438. **/
  14439. int
  14440. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  14441. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  14442. {
  14443. LPFC_MBOXQ_t *mboxq;
  14444. struct lpfc_hba *phba = ndlp->phba;
  14445. int rc;
  14446. /* The port is notified of the header region via a mailbox command. */
  14447. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14448. if (!mboxq)
  14449. return -ENOMEM;
  14450. /* Post all rpi memory regions to the port. */
  14451. lpfc_resume_rpi(mboxq, ndlp);
  14452. if (cmpl) {
  14453. mboxq->mbox_cmpl = cmpl;
  14454. mboxq->context1 = arg;
  14455. mboxq->context2 = ndlp;
  14456. } else
  14457. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14458. mboxq->vport = ndlp->vport;
  14459. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14460. if (rc == MBX_NOT_FINISHED) {
  14461. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14462. "2010 Resume RPI Mailbox failed "
  14463. "status %d, mbxStatus x%x\n", rc,
  14464. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  14465. mempool_free(mboxq, phba->mbox_mem_pool);
  14466. return -EIO;
  14467. }
  14468. return 0;
  14469. }
  14470. /**
  14471. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  14472. * @vport: Pointer to the vport for which the vpi is being initialized
  14473. *
  14474. * This routine is invoked to activate a vpi with the port.
  14475. *
  14476. * Returns:
  14477. * 0 success
  14478. * -Evalue otherwise
  14479. **/
  14480. int
  14481. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  14482. {
  14483. LPFC_MBOXQ_t *mboxq;
  14484. int rc = 0;
  14485. int retval = MBX_SUCCESS;
  14486. uint32_t mbox_tmo;
  14487. struct lpfc_hba *phba = vport->phba;
  14488. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14489. if (!mboxq)
  14490. return -ENOMEM;
  14491. lpfc_init_vpi(phba, mboxq, vport->vpi);
  14492. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  14493. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  14494. if (rc != MBX_SUCCESS) {
  14495. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  14496. "2022 INIT VPI Mailbox failed "
  14497. "status %d, mbxStatus x%x\n", rc,
  14498. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  14499. retval = -EIO;
  14500. }
  14501. if (rc != MBX_TIMEOUT)
  14502. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  14503. return retval;
  14504. }
  14505. /**
  14506. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  14507. * @phba: pointer to lpfc hba data structure.
  14508. * @mboxq: Pointer to mailbox object.
  14509. *
  14510. * This routine is invoked to manually add a single FCF record. The caller
  14511. * must pass a completely initialized FCF_Record. This routine takes
  14512. * care of the nonembedded mailbox operations.
  14513. **/
  14514. static void
  14515. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  14516. {
  14517. void *virt_addr;
  14518. union lpfc_sli4_cfg_shdr *shdr;
  14519. uint32_t shdr_status, shdr_add_status;
  14520. virt_addr = mboxq->sge_array->addr[0];
  14521. /* The IOCTL status is embedded in the mailbox subheader. */
  14522. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  14523. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14524. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14525. if ((shdr_status || shdr_add_status) &&
  14526. (shdr_status != STATUS_FCF_IN_USE))
  14527. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14528. "2558 ADD_FCF_RECORD mailbox failed with "
  14529. "status x%x add_status x%x\n",
  14530. shdr_status, shdr_add_status);
  14531. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14532. }
  14533. /**
  14534. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  14535. * @phba: pointer to lpfc hba data structure.
  14536. * @fcf_record: pointer to the initialized fcf record to add.
  14537. *
  14538. * This routine is invoked to manually add a single FCF record. The caller
  14539. * must pass a completely initialized FCF_Record. This routine takes
  14540. * care of the nonembedded mailbox operations.
  14541. **/
  14542. int
  14543. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  14544. {
  14545. int rc = 0;
  14546. LPFC_MBOXQ_t *mboxq;
  14547. uint8_t *bytep;
  14548. void *virt_addr;
  14549. dma_addr_t phys_addr;
  14550. struct lpfc_mbx_sge sge;
  14551. uint32_t alloc_len, req_len;
  14552. uint32_t fcfindex;
  14553. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14554. if (!mboxq) {
  14555. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14556. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  14557. return -ENOMEM;
  14558. }
  14559. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  14560. sizeof(uint32_t);
  14561. /* Allocate DMA memory and set up the non-embedded mailbox command */
  14562. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  14563. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  14564. req_len, LPFC_SLI4_MBX_NEMBED);
  14565. if (alloc_len < req_len) {
  14566. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14567. "2523 Allocated DMA memory size (x%x) is "
  14568. "less than the requested DMA memory "
  14569. "size (x%x)\n", alloc_len, req_len);
  14570. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14571. return -ENOMEM;
  14572. }
  14573. /*
  14574. * Get the first SGE entry from the non-embedded DMA memory. This
  14575. * routine only uses a single SGE.
  14576. */
  14577. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  14578. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  14579. virt_addr = mboxq->sge_array->addr[0];
  14580. /*
  14581. * Configure the FCF record for FCFI 0. This is the driver's
  14582. * hardcoded default and gets used in nonFIP mode.
  14583. */
  14584. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  14585. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  14586. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  14587. /*
  14588. * Copy the fcf_index and the FCF Record Data. The data starts after
  14589. * the FCoE header plus word10. The data copy needs to be endian
  14590. * correct.
  14591. */
  14592. bytep += sizeof(uint32_t);
  14593. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  14594. mboxq->vport = phba->pport;
  14595. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  14596. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14597. if (rc == MBX_NOT_FINISHED) {
  14598. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14599. "2515 ADD_FCF_RECORD mailbox failed with "
  14600. "status 0x%x\n", rc);
  14601. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14602. rc = -EIO;
  14603. } else
  14604. rc = 0;
  14605. return rc;
  14606. }
  14607. /**
  14608. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  14609. * @phba: pointer to lpfc hba data structure.
  14610. * @fcf_record: pointer to the fcf record to write the default data.
  14611. * @fcf_index: FCF table entry index.
  14612. *
  14613. * This routine is invoked to build the driver's default FCF record. The
  14614. * values used are hardcoded. This routine handles memory initialization.
  14615. *
  14616. **/
  14617. void
  14618. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  14619. struct fcf_record *fcf_record,
  14620. uint16_t fcf_index)
  14621. {
  14622. memset(fcf_record, 0, sizeof(struct fcf_record));
  14623. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  14624. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  14625. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  14626. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  14627. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  14628. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  14629. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  14630. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  14631. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  14632. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  14633. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  14634. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  14635. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  14636. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  14637. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  14638. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  14639. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  14640. /* Set the VLAN bit map */
  14641. if (phba->valid_vlan) {
  14642. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  14643. = 1 << (phba->vlan_id % 8);
  14644. }
  14645. }
  14646. /**
  14647. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  14648. * @phba: pointer to lpfc hba data structure.
  14649. * @fcf_index: FCF table entry offset.
  14650. *
  14651. * This routine is invoked to scan the entire FCF table by reading FCF
  14652. * record and processing it one at a time starting from the @fcf_index
  14653. * for initial FCF discovery or fast FCF failover rediscovery.
  14654. *
  14655. * Return 0 if the mailbox command is submitted successfully, none 0
  14656. * otherwise.
  14657. **/
  14658. int
  14659. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14660. {
  14661. int rc = 0, error;
  14662. LPFC_MBOXQ_t *mboxq;
  14663. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  14664. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  14665. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14666. if (!mboxq) {
  14667. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14668. "2000 Failed to allocate mbox for "
  14669. "READ_FCF cmd\n");
  14670. error = -ENOMEM;
  14671. goto fail_fcf_scan;
  14672. }
  14673. /* Construct the read FCF record mailbox command */
  14674. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14675. if (rc) {
  14676. error = -EINVAL;
  14677. goto fail_fcf_scan;
  14678. }
  14679. /* Issue the mailbox command asynchronously */
  14680. mboxq->vport = phba->pport;
  14681. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  14682. spin_lock_irq(&phba->hbalock);
  14683. phba->hba_flag |= FCF_TS_INPROG;
  14684. spin_unlock_irq(&phba->hbalock);
  14685. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14686. if (rc == MBX_NOT_FINISHED)
  14687. error = -EIO;
  14688. else {
  14689. /* Reset eligible FCF count for new scan */
  14690. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  14691. phba->fcf.eligible_fcf_cnt = 0;
  14692. error = 0;
  14693. }
  14694. fail_fcf_scan:
  14695. if (error) {
  14696. if (mboxq)
  14697. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14698. /* FCF scan failed, clear FCF_TS_INPROG flag */
  14699. spin_lock_irq(&phba->hbalock);
  14700. phba->hba_flag &= ~FCF_TS_INPROG;
  14701. spin_unlock_irq(&phba->hbalock);
  14702. }
  14703. return error;
  14704. }
  14705. /**
  14706. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  14707. * @phba: pointer to lpfc hba data structure.
  14708. * @fcf_index: FCF table entry offset.
  14709. *
  14710. * This routine is invoked to read an FCF record indicated by @fcf_index
  14711. * and to use it for FLOGI roundrobin FCF failover.
  14712. *
  14713. * Return 0 if the mailbox command is submitted successfully, none 0
  14714. * otherwise.
  14715. **/
  14716. int
  14717. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14718. {
  14719. int rc = 0, error;
  14720. LPFC_MBOXQ_t *mboxq;
  14721. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14722. if (!mboxq) {
  14723. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  14724. "2763 Failed to allocate mbox for "
  14725. "READ_FCF cmd\n");
  14726. error = -ENOMEM;
  14727. goto fail_fcf_read;
  14728. }
  14729. /* Construct the read FCF record mailbox command */
  14730. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14731. if (rc) {
  14732. error = -EINVAL;
  14733. goto fail_fcf_read;
  14734. }
  14735. /* Issue the mailbox command asynchronously */
  14736. mboxq->vport = phba->pport;
  14737. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  14738. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14739. if (rc == MBX_NOT_FINISHED)
  14740. error = -EIO;
  14741. else
  14742. error = 0;
  14743. fail_fcf_read:
  14744. if (error && mboxq)
  14745. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14746. return error;
  14747. }
  14748. /**
  14749. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  14750. * @phba: pointer to lpfc hba data structure.
  14751. * @fcf_index: FCF table entry offset.
  14752. *
  14753. * This routine is invoked to read an FCF record indicated by @fcf_index to
  14754. * determine whether it's eligible for FLOGI roundrobin failover list.
  14755. *
  14756. * Return 0 if the mailbox command is submitted successfully, none 0
  14757. * otherwise.
  14758. **/
  14759. int
  14760. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14761. {
  14762. int rc = 0, error;
  14763. LPFC_MBOXQ_t *mboxq;
  14764. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14765. if (!mboxq) {
  14766. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  14767. "2758 Failed to allocate mbox for "
  14768. "READ_FCF cmd\n");
  14769. error = -ENOMEM;
  14770. goto fail_fcf_read;
  14771. }
  14772. /* Construct the read FCF record mailbox command */
  14773. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14774. if (rc) {
  14775. error = -EINVAL;
  14776. goto fail_fcf_read;
  14777. }
  14778. /* Issue the mailbox command asynchronously */
  14779. mboxq->vport = phba->pport;
  14780. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  14781. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14782. if (rc == MBX_NOT_FINISHED)
  14783. error = -EIO;
  14784. else
  14785. error = 0;
  14786. fail_fcf_read:
  14787. if (error && mboxq)
  14788. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14789. return error;
  14790. }
  14791. /**
  14792. * lpfc_check_next_fcf_pri
  14793. * phba pointer to the lpfc_hba struct for this port.
  14794. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  14795. * routine when the rr_bmask is empty. The FCF indecies are put into the
  14796. * rr_bmask based on their priority level. Starting from the highest priority
  14797. * to the lowest. The most likely FCF candidate will be in the highest
  14798. * priority group. When this routine is called it searches the fcf_pri list for
  14799. * next lowest priority group and repopulates the rr_bmask with only those
  14800. * fcf_indexes.
  14801. * returns:
  14802. * 1=success 0=failure
  14803. **/
  14804. static int
  14805. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  14806. {
  14807. uint16_t next_fcf_pri;
  14808. uint16_t last_index;
  14809. struct lpfc_fcf_pri *fcf_pri;
  14810. int rc;
  14811. int ret = 0;
  14812. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  14813. LPFC_SLI4_FCF_TBL_INDX_MAX);
  14814. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14815. "3060 Last IDX %d\n", last_index);
  14816. /* Verify the priority list has 2 or more entries */
  14817. spin_lock_irq(&phba->hbalock);
  14818. if (list_empty(&phba->fcf.fcf_pri_list) ||
  14819. list_is_singular(&phba->fcf.fcf_pri_list)) {
  14820. spin_unlock_irq(&phba->hbalock);
  14821. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14822. "3061 Last IDX %d\n", last_index);
  14823. return 0; /* Empty rr list */
  14824. }
  14825. spin_unlock_irq(&phba->hbalock);
  14826. next_fcf_pri = 0;
  14827. /*
  14828. * Clear the rr_bmask and set all of the bits that are at this
  14829. * priority.
  14830. */
  14831. memset(phba->fcf.fcf_rr_bmask, 0,
  14832. sizeof(*phba->fcf.fcf_rr_bmask));
  14833. spin_lock_irq(&phba->hbalock);
  14834. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14835. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  14836. continue;
  14837. /*
  14838. * the 1st priority that has not FLOGI failed
  14839. * will be the highest.
  14840. */
  14841. if (!next_fcf_pri)
  14842. next_fcf_pri = fcf_pri->fcf_rec.priority;
  14843. spin_unlock_irq(&phba->hbalock);
  14844. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  14845. rc = lpfc_sli4_fcf_rr_index_set(phba,
  14846. fcf_pri->fcf_rec.fcf_index);
  14847. if (rc)
  14848. return 0;
  14849. }
  14850. spin_lock_irq(&phba->hbalock);
  14851. }
  14852. /*
  14853. * if next_fcf_pri was not set above and the list is not empty then
  14854. * we have failed flogis on all of them. So reset flogi failed
  14855. * and start at the beginning.
  14856. */
  14857. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  14858. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14859. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  14860. /*
  14861. * the 1st priority that has not FLOGI failed
  14862. * will be the highest.
  14863. */
  14864. if (!next_fcf_pri)
  14865. next_fcf_pri = fcf_pri->fcf_rec.priority;
  14866. spin_unlock_irq(&phba->hbalock);
  14867. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  14868. rc = lpfc_sli4_fcf_rr_index_set(phba,
  14869. fcf_pri->fcf_rec.fcf_index);
  14870. if (rc)
  14871. return 0;
  14872. }
  14873. spin_lock_irq(&phba->hbalock);
  14874. }
  14875. } else
  14876. ret = 1;
  14877. spin_unlock_irq(&phba->hbalock);
  14878. return ret;
  14879. }
  14880. /**
  14881. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  14882. * @phba: pointer to lpfc hba data structure.
  14883. *
  14884. * This routine is to get the next eligible FCF record index in a round
  14885. * robin fashion. If the next eligible FCF record index equals to the
  14886. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  14887. * shall be returned, otherwise, the next eligible FCF record's index
  14888. * shall be returned.
  14889. **/
  14890. uint16_t
  14891. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  14892. {
  14893. uint16_t next_fcf_index;
  14894. initial_priority:
  14895. /* Search start from next bit of currently registered FCF index */
  14896. next_fcf_index = phba->fcf.current_rec.fcf_indx;
  14897. next_priority:
  14898. /* Determine the next fcf index to check */
  14899. next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
  14900. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14901. LPFC_SLI4_FCF_TBL_INDX_MAX,
  14902. next_fcf_index);
  14903. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  14904. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14905. /*
  14906. * If we have wrapped then we need to clear the bits that
  14907. * have been tested so that we can detect when we should
  14908. * change the priority level.
  14909. */
  14910. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14911. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  14912. }
  14913. /* Check roundrobin failover list empty condition */
  14914. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  14915. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  14916. /*
  14917. * If next fcf index is not found check if there are lower
  14918. * Priority level fcf's in the fcf_priority list.
  14919. * Set up the rr_bmask with all of the avaiable fcf bits
  14920. * at that level and continue the selection process.
  14921. */
  14922. if (lpfc_check_next_fcf_pri_level(phba))
  14923. goto initial_priority;
  14924. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14925. "2844 No roundrobin failover FCF available\n");
  14926. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  14927. return LPFC_FCOE_FCF_NEXT_NONE;
  14928. else {
  14929. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14930. "3063 Only FCF available idx %d, flag %x\n",
  14931. next_fcf_index,
  14932. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  14933. return next_fcf_index;
  14934. }
  14935. }
  14936. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  14937. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  14938. LPFC_FCF_FLOGI_FAILED)
  14939. goto next_priority;
  14940. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14941. "2845 Get next roundrobin failover FCF (x%x)\n",
  14942. next_fcf_index);
  14943. return next_fcf_index;
  14944. }
  14945. /**
  14946. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  14947. * @phba: pointer to lpfc hba data structure.
  14948. *
  14949. * This routine sets the FCF record index in to the eligible bmask for
  14950. * roundrobin failover search. It checks to make sure that the index
  14951. * does not go beyond the range of the driver allocated bmask dimension
  14952. * before setting the bit.
  14953. *
  14954. * Returns 0 if the index bit successfully set, otherwise, it returns
  14955. * -EINVAL.
  14956. **/
  14957. int
  14958. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  14959. {
  14960. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14961. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14962. "2610 FCF (x%x) reached driver's book "
  14963. "keeping dimension:x%x\n",
  14964. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14965. return -EINVAL;
  14966. }
  14967. /* Set the eligible FCF record index bmask */
  14968. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14969. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14970. "2790 Set FCF (x%x) to roundrobin FCF failover "
  14971. "bmask\n", fcf_index);
  14972. return 0;
  14973. }
  14974. /**
  14975. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  14976. * @phba: pointer to lpfc hba data structure.
  14977. *
  14978. * This routine clears the FCF record index from the eligible bmask for
  14979. * roundrobin failover search. It checks to make sure that the index
  14980. * does not go beyond the range of the driver allocated bmask dimension
  14981. * before clearing the bit.
  14982. **/
  14983. void
  14984. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  14985. {
  14986. struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
  14987. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14988. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14989. "2762 FCF (x%x) reached driver's book "
  14990. "keeping dimension:x%x\n",
  14991. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14992. return;
  14993. }
  14994. /* Clear the eligible FCF record index bmask */
  14995. spin_lock_irq(&phba->hbalock);
  14996. list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
  14997. list) {
  14998. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  14999. list_del_init(&fcf_pri->list);
  15000. break;
  15001. }
  15002. }
  15003. spin_unlock_irq(&phba->hbalock);
  15004. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  15005. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  15006. "2791 Clear FCF (x%x) from roundrobin failover "
  15007. "bmask\n", fcf_index);
  15008. }
  15009. /**
  15010. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  15011. * @phba: pointer to lpfc hba data structure.
  15012. *
  15013. * This routine is the completion routine for the rediscover FCF table mailbox
  15014. * command. If the mailbox command returned failure, it will try to stop the
  15015. * FCF rediscover wait timer.
  15016. **/
  15017. static void
  15018. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  15019. {
  15020. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  15021. uint32_t shdr_status, shdr_add_status;
  15022. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  15023. shdr_status = bf_get(lpfc_mbox_hdr_status,
  15024. &redisc_fcf->header.cfg_shdr.response);
  15025. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  15026. &redisc_fcf->header.cfg_shdr.response);
  15027. if (shdr_status || shdr_add_status) {
  15028. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  15029. "2746 Requesting for FCF rediscovery failed "
  15030. "status x%x add_status x%x\n",
  15031. shdr_status, shdr_add_status);
  15032. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  15033. spin_lock_irq(&phba->hbalock);
  15034. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  15035. spin_unlock_irq(&phba->hbalock);
  15036. /*
  15037. * CVL event triggered FCF rediscover request failed,
  15038. * last resort to re-try current registered FCF entry.
  15039. */
  15040. lpfc_retry_pport_discovery(phba);
  15041. } else {
  15042. spin_lock_irq(&phba->hbalock);
  15043. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  15044. spin_unlock_irq(&phba->hbalock);
  15045. /*
  15046. * DEAD FCF event triggered FCF rediscover request
  15047. * failed, last resort to fail over as a link down
  15048. * to FCF registration.
  15049. */
  15050. lpfc_sli4_fcf_dead_failthrough(phba);
  15051. }
  15052. } else {
  15053. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  15054. "2775 Start FCF rediscover quiescent timer\n");
  15055. /*
  15056. * Start FCF rediscovery wait timer for pending FCF
  15057. * before rescan FCF record table.
  15058. */
  15059. lpfc_fcf_redisc_wait_start_timer(phba);
  15060. }
  15061. mempool_free(mbox, phba->mbox_mem_pool);
  15062. }
  15063. /**
  15064. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  15065. * @phba: pointer to lpfc hba data structure.
  15066. *
  15067. * This routine is invoked to request for rediscovery of the entire FCF table
  15068. * by the port.
  15069. **/
  15070. int
  15071. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  15072. {
  15073. LPFC_MBOXQ_t *mbox;
  15074. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  15075. int rc, length;
  15076. /* Cancel retry delay timers to all vports before FCF rediscover */
  15077. lpfc_cancel_all_vport_retry_delay_timer(phba);
  15078. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  15079. if (!mbox) {
  15080. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15081. "2745 Failed to allocate mbox for "
  15082. "requesting FCF rediscover.\n");
  15083. return -ENOMEM;
  15084. }
  15085. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  15086. sizeof(struct lpfc_sli4_cfg_mhdr));
  15087. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  15088. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  15089. length, LPFC_SLI4_MBX_EMBED);
  15090. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  15091. /* Set count to 0 for invalidating the entire FCF database */
  15092. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  15093. /* Issue the mailbox command asynchronously */
  15094. mbox->vport = phba->pport;
  15095. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  15096. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  15097. if (rc == MBX_NOT_FINISHED) {
  15098. mempool_free(mbox, phba->mbox_mem_pool);
  15099. return -EIO;
  15100. }
  15101. return 0;
  15102. }
  15103. /**
  15104. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  15105. * @phba: pointer to lpfc hba data structure.
  15106. *
  15107. * This function is the failover routine as a last resort to the FCF DEAD
  15108. * event when driver failed to perform fast FCF failover.
  15109. **/
  15110. void
  15111. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  15112. {
  15113. uint32_t link_state;
  15114. /*
  15115. * Last resort as FCF DEAD event failover will treat this as
  15116. * a link down, but save the link state because we don't want
  15117. * it to be changed to Link Down unless it is already down.
  15118. */
  15119. link_state = phba->link_state;
  15120. lpfc_linkdown(phba);
  15121. phba->link_state = link_state;
  15122. /* Unregister FCF if no devices connected to it */
  15123. lpfc_unregister_unused_fcf(phba);
  15124. }
  15125. /**
  15126. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  15127. * @phba: pointer to lpfc hba data structure.
  15128. * @rgn23_data: pointer to configure region 23 data.
  15129. *
  15130. * This function gets SLI3 port configure region 23 data through memory dump
  15131. * mailbox command. When it successfully retrieves data, the size of the data
  15132. * will be returned, otherwise, 0 will be returned.
  15133. **/
  15134. static uint32_t
  15135. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  15136. {
  15137. LPFC_MBOXQ_t *pmb = NULL;
  15138. MAILBOX_t *mb;
  15139. uint32_t offset = 0;
  15140. int rc;
  15141. if (!rgn23_data)
  15142. return 0;
  15143. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  15144. if (!pmb) {
  15145. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15146. "2600 failed to allocate mailbox memory\n");
  15147. return 0;
  15148. }
  15149. mb = &pmb->u.mb;
  15150. do {
  15151. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  15152. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  15153. if (rc != MBX_SUCCESS) {
  15154. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  15155. "2601 failed to read config "
  15156. "region 23, rc 0x%x Status 0x%x\n",
  15157. rc, mb->mbxStatus);
  15158. mb->un.varDmp.word_cnt = 0;
  15159. }
  15160. /*
  15161. * dump mem may return a zero when finished or we got a
  15162. * mailbox error, either way we are done.
  15163. */
  15164. if (mb->un.varDmp.word_cnt == 0)
  15165. break;
  15166. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  15167. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  15168. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  15169. rgn23_data + offset,
  15170. mb->un.varDmp.word_cnt);
  15171. offset += mb->un.varDmp.word_cnt;
  15172. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  15173. mempool_free(pmb, phba->mbox_mem_pool);
  15174. return offset;
  15175. }
  15176. /**
  15177. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  15178. * @phba: pointer to lpfc hba data structure.
  15179. * @rgn23_data: pointer to configure region 23 data.
  15180. *
  15181. * This function gets SLI4 port configure region 23 data through memory dump
  15182. * mailbox command. When it successfully retrieves data, the size of the data
  15183. * will be returned, otherwise, 0 will be returned.
  15184. **/
  15185. static uint32_t
  15186. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  15187. {
  15188. LPFC_MBOXQ_t *mboxq = NULL;
  15189. struct lpfc_dmabuf *mp = NULL;
  15190. struct lpfc_mqe *mqe;
  15191. uint32_t data_length = 0;
  15192. int rc;
  15193. if (!rgn23_data)
  15194. return 0;
  15195. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  15196. if (!mboxq) {
  15197. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15198. "3105 failed to allocate mailbox memory\n");
  15199. return 0;
  15200. }
  15201. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  15202. goto out;
  15203. mqe = &mboxq->u.mqe;
  15204. mp = (struct lpfc_dmabuf *) mboxq->context1;
  15205. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  15206. if (rc)
  15207. goto out;
  15208. data_length = mqe->un.mb_words[5];
  15209. if (data_length == 0)
  15210. goto out;
  15211. if (data_length > DMP_RGN23_SIZE) {
  15212. data_length = 0;
  15213. goto out;
  15214. }
  15215. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  15216. out:
  15217. mempool_free(mboxq, phba->mbox_mem_pool);
  15218. if (mp) {
  15219. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  15220. kfree(mp);
  15221. }
  15222. return data_length;
  15223. }
  15224. /**
  15225. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  15226. * @phba: pointer to lpfc hba data structure.
  15227. *
  15228. * This function read region 23 and parse TLV for port status to
  15229. * decide if the user disaled the port. If the TLV indicates the
  15230. * port is disabled, the hba_flag is set accordingly.
  15231. **/
  15232. void
  15233. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  15234. {
  15235. uint8_t *rgn23_data = NULL;
  15236. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  15237. uint32_t offset = 0;
  15238. /* Get adapter Region 23 data */
  15239. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  15240. if (!rgn23_data)
  15241. goto out;
  15242. if (phba->sli_rev < LPFC_SLI_REV4)
  15243. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  15244. else {
  15245. if_type = bf_get(lpfc_sli_intf_if_type,
  15246. &phba->sli4_hba.sli_intf);
  15247. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  15248. goto out;
  15249. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  15250. }
  15251. if (!data_size)
  15252. goto out;
  15253. /* Check the region signature first */
  15254. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  15255. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15256. "2619 Config region 23 has bad signature\n");
  15257. goto out;
  15258. }
  15259. offset += 4;
  15260. /* Check the data structure version */
  15261. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  15262. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15263. "2620 Config region 23 has bad version\n");
  15264. goto out;
  15265. }
  15266. offset += 4;
  15267. /* Parse TLV entries in the region */
  15268. while (offset < data_size) {
  15269. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  15270. break;
  15271. /*
  15272. * If the TLV is not driver specific TLV or driver id is
  15273. * not linux driver id, skip the record.
  15274. */
  15275. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  15276. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  15277. (rgn23_data[offset + 3] != 0)) {
  15278. offset += rgn23_data[offset + 1] * 4 + 4;
  15279. continue;
  15280. }
  15281. /* Driver found a driver specific TLV in the config region */
  15282. sub_tlv_len = rgn23_data[offset + 1] * 4;
  15283. offset += 4;
  15284. tlv_offset = 0;
  15285. /*
  15286. * Search for configured port state sub-TLV.
  15287. */
  15288. while ((offset < data_size) &&
  15289. (tlv_offset < sub_tlv_len)) {
  15290. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  15291. offset += 4;
  15292. tlv_offset += 4;
  15293. break;
  15294. }
  15295. if (rgn23_data[offset] != PORT_STE_TYPE) {
  15296. offset += rgn23_data[offset + 1] * 4 + 4;
  15297. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  15298. continue;
  15299. }
  15300. /* This HBA contains PORT_STE configured */
  15301. if (!rgn23_data[offset + 2])
  15302. phba->hba_flag |= LINK_DISABLED;
  15303. goto out;
  15304. }
  15305. }
  15306. out:
  15307. kfree(rgn23_data);
  15308. return;
  15309. }
  15310. /**
  15311. * lpfc_wr_object - write an object to the firmware
  15312. * @phba: HBA structure that indicates port to create a queue on.
  15313. * @dmabuf_list: list of dmabufs to write to the port.
  15314. * @size: the total byte value of the objects to write to the port.
  15315. * @offset: the current offset to be used to start the transfer.
  15316. *
  15317. * This routine will create a wr_object mailbox command to send to the port.
  15318. * the mailbox command will be constructed using the dma buffers described in
  15319. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  15320. * BDEs that the imbedded mailbox can support. The @offset variable will be
  15321. * used to indicate the starting offset of the transfer and will also return
  15322. * the offset after the write object mailbox has completed. @size is used to
  15323. * determine the end of the object and whether the eof bit should be set.
  15324. *
  15325. * Return 0 is successful and offset will contain the the new offset to use
  15326. * for the next write.
  15327. * Return negative value for error cases.
  15328. **/
  15329. int
  15330. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  15331. uint32_t size, uint32_t *offset)
  15332. {
  15333. struct lpfc_mbx_wr_object *wr_object;
  15334. LPFC_MBOXQ_t *mbox;
  15335. int rc = 0, i = 0;
  15336. uint32_t shdr_status, shdr_add_status;
  15337. uint32_t mbox_tmo;
  15338. union lpfc_sli4_cfg_shdr *shdr;
  15339. struct lpfc_dmabuf *dmabuf;
  15340. uint32_t written = 0;
  15341. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  15342. if (!mbox)
  15343. return -ENOMEM;
  15344. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  15345. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  15346. sizeof(struct lpfc_mbx_wr_object) -
  15347. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  15348. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  15349. wr_object->u.request.write_offset = *offset;
  15350. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  15351. wr_object->u.request.object_name[0] =
  15352. cpu_to_le32(wr_object->u.request.object_name[0]);
  15353. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  15354. list_for_each_entry(dmabuf, dmabuf_list, list) {
  15355. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  15356. break;
  15357. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  15358. wr_object->u.request.bde[i].addrHigh =
  15359. putPaddrHigh(dmabuf->phys);
  15360. if (written + SLI4_PAGE_SIZE >= size) {
  15361. wr_object->u.request.bde[i].tus.f.bdeSize =
  15362. (size - written);
  15363. written += (size - written);
  15364. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  15365. } else {
  15366. wr_object->u.request.bde[i].tus.f.bdeSize =
  15367. SLI4_PAGE_SIZE;
  15368. written += SLI4_PAGE_SIZE;
  15369. }
  15370. i++;
  15371. }
  15372. wr_object->u.request.bde_count = i;
  15373. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  15374. if (!phba->sli4_hba.intr_enable)
  15375. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  15376. else {
  15377. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  15378. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  15379. }
  15380. /* The IOCTL status is embedded in the mailbox subheader. */
  15381. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  15382. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  15383. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  15384. if (rc != MBX_TIMEOUT)
  15385. mempool_free(mbox, phba->mbox_mem_pool);
  15386. if (shdr_status || shdr_add_status || rc) {
  15387. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15388. "3025 Write Object mailbox failed with "
  15389. "status x%x add_status x%x, mbx status x%x\n",
  15390. shdr_status, shdr_add_status, rc);
  15391. rc = -ENXIO;
  15392. } else
  15393. *offset += wr_object->u.response.actual_write_length;
  15394. return rc;
  15395. }
  15396. /**
  15397. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  15398. * @vport: pointer to vport data structure.
  15399. *
  15400. * This function iterate through the mailboxq and clean up all REG_LOGIN
  15401. * and REG_VPI mailbox commands associated with the vport. This function
  15402. * is called when driver want to restart discovery of the vport due to
  15403. * a Clear Virtual Link event.
  15404. **/
  15405. void
  15406. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  15407. {
  15408. struct lpfc_hba *phba = vport->phba;
  15409. LPFC_MBOXQ_t *mb, *nextmb;
  15410. struct lpfc_dmabuf *mp;
  15411. struct lpfc_nodelist *ndlp;
  15412. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  15413. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  15414. LIST_HEAD(mbox_cmd_list);
  15415. uint8_t restart_loop;
  15416. /* Clean up internally queued mailbox commands with the vport */
  15417. spin_lock_irq(&phba->hbalock);
  15418. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  15419. if (mb->vport != vport)
  15420. continue;
  15421. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  15422. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  15423. continue;
  15424. list_del(&mb->list);
  15425. list_add_tail(&mb->list, &mbox_cmd_list);
  15426. }
  15427. /* Clean up active mailbox command with the vport */
  15428. mb = phba->sli.mbox_active;
  15429. if (mb && (mb->vport == vport)) {
  15430. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  15431. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  15432. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  15433. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  15434. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  15435. /* Put reference count for delayed processing */
  15436. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  15437. /* Unregister the RPI when mailbox complete */
  15438. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  15439. }
  15440. }
  15441. /* Cleanup any mailbox completions which are not yet processed */
  15442. do {
  15443. restart_loop = 0;
  15444. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  15445. /*
  15446. * If this mailox is already processed or it is
  15447. * for another vport ignore it.
  15448. */
  15449. if ((mb->vport != vport) ||
  15450. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  15451. continue;
  15452. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  15453. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  15454. continue;
  15455. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  15456. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  15457. ndlp = (struct lpfc_nodelist *)mb->context2;
  15458. /* Unregister the RPI when mailbox complete */
  15459. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  15460. restart_loop = 1;
  15461. spin_unlock_irq(&phba->hbalock);
  15462. spin_lock(shost->host_lock);
  15463. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  15464. spin_unlock(shost->host_lock);
  15465. spin_lock_irq(&phba->hbalock);
  15466. break;
  15467. }
  15468. }
  15469. } while (restart_loop);
  15470. spin_unlock_irq(&phba->hbalock);
  15471. /* Release the cleaned-up mailbox commands */
  15472. while (!list_empty(&mbox_cmd_list)) {
  15473. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  15474. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  15475. mp = (struct lpfc_dmabuf *) (mb->context1);
  15476. if (mp) {
  15477. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  15478. kfree(mp);
  15479. }
  15480. ndlp = (struct lpfc_nodelist *) mb->context2;
  15481. mb->context2 = NULL;
  15482. if (ndlp) {
  15483. spin_lock(shost->host_lock);
  15484. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  15485. spin_unlock(shost->host_lock);
  15486. lpfc_nlp_put(ndlp);
  15487. }
  15488. }
  15489. mempool_free(mb, phba->mbox_mem_pool);
  15490. }
  15491. /* Release the ndlp with the cleaned-up active mailbox command */
  15492. if (act_mbx_ndlp) {
  15493. spin_lock(shost->host_lock);
  15494. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  15495. spin_unlock(shost->host_lock);
  15496. lpfc_nlp_put(act_mbx_ndlp);
  15497. }
  15498. }
  15499. /**
  15500. * lpfc_drain_txq - Drain the txq
  15501. * @phba: Pointer to HBA context object.
  15502. *
  15503. * This function attempt to submit IOCBs on the txq
  15504. * to the adapter. For SLI4 adapters, the txq contains
  15505. * ELS IOCBs that have been deferred because the there
  15506. * are no SGLs. This congestion can occur with large
  15507. * vport counts during node discovery.
  15508. **/
  15509. uint32_t
  15510. lpfc_drain_txq(struct lpfc_hba *phba)
  15511. {
  15512. LIST_HEAD(completions);
  15513. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  15514. struct lpfc_iocbq *piocbq = NULL;
  15515. unsigned long iflags = 0;
  15516. char *fail_msg = NULL;
  15517. struct lpfc_sglq *sglq;
  15518. union lpfc_wqe wqe;
  15519. uint32_t txq_cnt = 0;
  15520. spin_lock_irqsave(&pring->ring_lock, iflags);
  15521. list_for_each_entry(piocbq, &pring->txq, list) {
  15522. txq_cnt++;
  15523. }
  15524. if (txq_cnt > pring->txq_max)
  15525. pring->txq_max = txq_cnt;
  15526. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15527. while (!list_empty(&pring->txq)) {
  15528. spin_lock_irqsave(&pring->ring_lock, iflags);
  15529. piocbq = lpfc_sli_ringtx_get(phba, pring);
  15530. if (!piocbq) {
  15531. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15532. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15533. "2823 txq empty and txq_cnt is %d\n ",
  15534. txq_cnt);
  15535. break;
  15536. }
  15537. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  15538. if (!sglq) {
  15539. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  15540. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15541. break;
  15542. }
  15543. txq_cnt--;
  15544. /* The xri and iocb resources secured,
  15545. * attempt to issue request
  15546. */
  15547. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  15548. piocbq->sli4_xritag = sglq->sli4_xritag;
  15549. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  15550. fail_msg = "to convert bpl to sgl";
  15551. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  15552. fail_msg = "to convert iocb to wqe";
  15553. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  15554. fail_msg = " - Wq is full";
  15555. else
  15556. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  15557. if (fail_msg) {
  15558. /* Failed means we can't issue and need to cancel */
  15559. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15560. "2822 IOCB failed %s iotag 0x%x "
  15561. "xri 0x%x\n",
  15562. fail_msg,
  15563. piocbq->iotag, piocbq->sli4_xritag);
  15564. list_add_tail(&piocbq->list, &completions);
  15565. }
  15566. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15567. }
  15568. /* Cancel all the IOCBs that cannot be issued */
  15569. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  15570. IOERR_SLI_ABORTED);
  15571. return txq_cnt;
  15572. }