lpfc_sli.c 490 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2012 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/fc/fc_fs.h>
  32. #include <linux/aer.h>
  33. #include "lpfc_hw4.h"
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_nl.h"
  38. #include "lpfc_disc.h"
  39. #include "lpfc_scsi.h"
  40. #include "lpfc.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_compat.h"
  44. #include "lpfc_debugfs.h"
  45. #include "lpfc_vport.h"
  46. /* There are only four IOCB completion types. */
  47. typedef enum _lpfc_iocb_type {
  48. LPFC_UNKNOWN_IOCB,
  49. LPFC_UNSOL_IOCB,
  50. LPFC_SOL_IOCB,
  51. LPFC_ABORT_IOCB
  52. } lpfc_iocb_type;
  53. /* Provide function prototypes local to this module. */
  54. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint32_t);
  56. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  57. uint8_t *, uint32_t *);
  58. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  59. struct lpfc_iocbq *);
  60. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  61. struct hbq_dmabuf *);
  62. static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
  63. struct lpfc_cqe *);
  64. static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
  65. int);
  66. static IOCB_t *
  67. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  68. {
  69. return &iocbq->iocb;
  70. }
  71. /**
  72. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  73. * @q: The Work Queue to operate on.
  74. * @wqe: The work Queue Entry to put on the Work queue.
  75. *
  76. * This routine will copy the contents of @wqe to the next available entry on
  77. * the @q. This function will then ring the Work Queue Doorbell to signal the
  78. * HBA to start processing the Work Queue Entry. This function returns 0 if
  79. * successful. If no entries are available on @q then this function will return
  80. * -ENOMEM.
  81. * The caller is expected to hold the hbalock when calling this routine.
  82. **/
  83. static uint32_t
  84. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  85. {
  86. union lpfc_wqe *temp_wqe;
  87. struct lpfc_register doorbell;
  88. uint32_t host_index;
  89. /* sanity check on queue memory */
  90. if (unlikely(!q))
  91. return -ENOMEM;
  92. temp_wqe = q->qe[q->host_index].wqe;
  93. /* If the host has not yet processed the next entry then we are done */
  94. if (((q->host_index + 1) % q->entry_count) == q->hba_index) {
  95. q->WQ_overflow++;
  96. return -ENOMEM;
  97. }
  98. q->WQ_posted++;
  99. /* set consumption flag every once in a while */
  100. if (!((q->host_index + 1) % q->entry_repost))
  101. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  102. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  103. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  104. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  105. /* Update the host index before invoking device */
  106. host_index = q->host_index;
  107. q->host_index = ((q->host_index + 1) % q->entry_count);
  108. /* Ring Doorbell */
  109. doorbell.word0 = 0;
  110. bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
  111. bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
  112. bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
  113. writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
  114. readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
  115. return 0;
  116. }
  117. /**
  118. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  119. * @q: The Work Queue to operate on.
  120. * @index: The index to advance the hba index to.
  121. *
  122. * This routine will update the HBA index of a queue to reflect consumption of
  123. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  124. * an entry the host calls this function to update the queue's internal
  125. * pointers. This routine returns the number of entries that were consumed by
  126. * the HBA.
  127. **/
  128. static uint32_t
  129. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  130. {
  131. uint32_t released = 0;
  132. /* sanity check on queue memory */
  133. if (unlikely(!q))
  134. return 0;
  135. if (q->hba_index == index)
  136. return 0;
  137. do {
  138. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  139. released++;
  140. } while (q->hba_index != index);
  141. return released;
  142. }
  143. /**
  144. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  145. * @q: The Mailbox Queue to operate on.
  146. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  147. *
  148. * This routine will copy the contents of @mqe to the next available entry on
  149. * the @q. This function will then ring the Work Queue Doorbell to signal the
  150. * HBA to start processing the Work Queue Entry. This function returns 0 if
  151. * successful. If no entries are available on @q then this function will return
  152. * -ENOMEM.
  153. * The caller is expected to hold the hbalock when calling this routine.
  154. **/
  155. static uint32_t
  156. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  157. {
  158. struct lpfc_mqe *temp_mqe;
  159. struct lpfc_register doorbell;
  160. uint32_t host_index;
  161. /* sanity check on queue memory */
  162. if (unlikely(!q))
  163. return -ENOMEM;
  164. temp_mqe = q->qe[q->host_index].mqe;
  165. /* If the host has not yet processed the next entry then we are done */
  166. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  167. return -ENOMEM;
  168. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  169. /* Save off the mailbox pointer for completion */
  170. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  171. /* Update the host index before invoking device */
  172. host_index = q->host_index;
  173. q->host_index = ((q->host_index + 1) % q->entry_count);
  174. /* Ring Doorbell */
  175. doorbell.word0 = 0;
  176. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  177. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  178. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  179. readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
  180. return 0;
  181. }
  182. /**
  183. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  184. * @q: The Mailbox Queue to operate on.
  185. *
  186. * This routine will update the HBA index of a queue to reflect consumption of
  187. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  188. * an entry the host calls this function to update the queue's internal
  189. * pointers. This routine returns the number of entries that were consumed by
  190. * the HBA.
  191. **/
  192. static uint32_t
  193. lpfc_sli4_mq_release(struct lpfc_queue *q)
  194. {
  195. /* sanity check on queue memory */
  196. if (unlikely(!q))
  197. return 0;
  198. /* Clear the mailbox pointer for completion */
  199. q->phba->mbox = NULL;
  200. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  201. return 1;
  202. }
  203. /**
  204. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  205. * @q: The Event Queue to get the first valid EQE from
  206. *
  207. * This routine will get the first valid Event Queue Entry from @q, update
  208. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  209. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  210. * processed, but not popped back to the HBA then this routine will return NULL.
  211. **/
  212. static struct lpfc_eqe *
  213. lpfc_sli4_eq_get(struct lpfc_queue *q)
  214. {
  215. struct lpfc_eqe *eqe;
  216. /* sanity check on queue memory */
  217. if (unlikely(!q))
  218. return NULL;
  219. eqe = q->qe[q->hba_index].eqe;
  220. /* If the next EQE is not valid then we are done */
  221. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  222. return NULL;
  223. /* If the host has not yet processed the next entry then we are done */
  224. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  225. return NULL;
  226. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  227. return eqe;
  228. }
  229. /**
  230. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  231. * @q: The Event Queue that the host has completed processing for.
  232. * @arm: Indicates whether the host wants to arms this CQ.
  233. *
  234. * This routine will mark all Event Queue Entries on @q, from the last
  235. * known completed entry to the last entry that was processed, as completed
  236. * by clearing the valid bit for each completion queue entry. Then it will
  237. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  238. * The internal host index in the @q will be updated by this routine to indicate
  239. * that the host has finished processing the entries. The @arm parameter
  240. * indicates that the queue should be rearmed when ringing the doorbell.
  241. *
  242. * This function will return the number of EQEs that were popped.
  243. **/
  244. uint32_t
  245. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  246. {
  247. uint32_t released = 0;
  248. struct lpfc_eqe *temp_eqe;
  249. struct lpfc_register doorbell;
  250. /* sanity check on queue memory */
  251. if (unlikely(!q))
  252. return 0;
  253. /* while there are valid entries */
  254. while (q->hba_index != q->host_index) {
  255. temp_eqe = q->qe[q->host_index].eqe;
  256. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  257. released++;
  258. q->host_index = ((q->host_index + 1) % q->entry_count);
  259. }
  260. if (unlikely(released == 0 && !arm))
  261. return 0;
  262. /* ring doorbell for number popped */
  263. doorbell.word0 = 0;
  264. if (arm) {
  265. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  266. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  267. }
  268. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  269. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  270. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  271. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  272. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  273. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  274. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  275. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  276. readl(q->phba->sli4_hba.EQCQDBregaddr);
  277. return released;
  278. }
  279. /**
  280. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  281. * @q: The Completion Queue to get the first valid CQE from
  282. *
  283. * This routine will get the first valid Completion Queue Entry from @q, update
  284. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  285. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  286. * processed, but not popped back to the HBA then this routine will return NULL.
  287. **/
  288. static struct lpfc_cqe *
  289. lpfc_sli4_cq_get(struct lpfc_queue *q)
  290. {
  291. struct lpfc_cqe *cqe;
  292. /* sanity check on queue memory */
  293. if (unlikely(!q))
  294. return NULL;
  295. /* If the next CQE is not valid then we are done */
  296. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  297. return NULL;
  298. /* If the host has not yet processed the next entry then we are done */
  299. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  300. return NULL;
  301. cqe = q->qe[q->hba_index].cqe;
  302. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  303. return cqe;
  304. }
  305. /**
  306. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  307. * @q: The Completion Queue that the host has completed processing for.
  308. * @arm: Indicates whether the host wants to arms this CQ.
  309. *
  310. * This routine will mark all Completion queue entries on @q, from the last
  311. * known completed entry to the last entry that was processed, as completed
  312. * by clearing the valid bit for each completion queue entry. Then it will
  313. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  314. * The internal host index in the @q will be updated by this routine to indicate
  315. * that the host has finished processing the entries. The @arm parameter
  316. * indicates that the queue should be rearmed when ringing the doorbell.
  317. *
  318. * This function will return the number of CQEs that were released.
  319. **/
  320. uint32_t
  321. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  322. {
  323. uint32_t released = 0;
  324. struct lpfc_cqe *temp_qe;
  325. struct lpfc_register doorbell;
  326. /* sanity check on queue memory */
  327. if (unlikely(!q))
  328. return 0;
  329. /* while there are valid entries */
  330. while (q->hba_index != q->host_index) {
  331. temp_qe = q->qe[q->host_index].cqe;
  332. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  333. released++;
  334. q->host_index = ((q->host_index + 1) % q->entry_count);
  335. }
  336. if (unlikely(released == 0 && !arm))
  337. return 0;
  338. /* ring doorbell for number popped */
  339. doorbell.word0 = 0;
  340. if (arm)
  341. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  342. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  343. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  344. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  345. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  346. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  347. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  348. return released;
  349. }
  350. /**
  351. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  352. * @q: The Header Receive Queue to operate on.
  353. * @wqe: The Receive Queue Entry to put on the Receive queue.
  354. *
  355. * This routine will copy the contents of @wqe to the next available entry on
  356. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  357. * HBA to start processing the Receive Queue Entry. This function returns the
  358. * index that the rqe was copied to if successful. If no entries are available
  359. * on @q then this function will return -ENOMEM.
  360. * The caller is expected to hold the hbalock when calling this routine.
  361. **/
  362. static int
  363. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  364. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  365. {
  366. struct lpfc_rqe *temp_hrqe;
  367. struct lpfc_rqe *temp_drqe;
  368. struct lpfc_register doorbell;
  369. int put_index = hq->host_index;
  370. /* sanity check on queue memory */
  371. if (unlikely(!hq) || unlikely(!dq))
  372. return -ENOMEM;
  373. temp_hrqe = hq->qe[hq->host_index].rqe;
  374. temp_drqe = dq->qe[dq->host_index].rqe;
  375. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  376. return -EINVAL;
  377. if (hq->host_index != dq->host_index)
  378. return -EINVAL;
  379. /* If the host has not yet processed the next entry then we are done */
  380. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  381. return -EBUSY;
  382. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  383. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  384. /* Update the host index to point to the next slot */
  385. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  386. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  387. /* Ring The Header Receive Queue Doorbell */
  388. if (!(hq->host_index % hq->entry_repost)) {
  389. doorbell.word0 = 0;
  390. bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
  391. hq->entry_repost);
  392. bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
  393. writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
  394. }
  395. return put_index;
  396. }
  397. /**
  398. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  399. * @q: The Header Receive Queue to operate on.
  400. *
  401. * This routine will update the HBA index of a queue to reflect consumption of
  402. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  403. * consumed an entry the host calls this function to update the queue's
  404. * internal pointers. This routine returns the number of entries that were
  405. * consumed by the HBA.
  406. **/
  407. static uint32_t
  408. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  409. {
  410. /* sanity check on queue memory */
  411. if (unlikely(!hq) || unlikely(!dq))
  412. return 0;
  413. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  414. return 0;
  415. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  416. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  417. return 1;
  418. }
  419. /**
  420. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  421. * @phba: Pointer to HBA context object.
  422. * @pring: Pointer to driver SLI ring object.
  423. *
  424. * This function returns pointer to next command iocb entry
  425. * in the command ring. The caller must hold hbalock to prevent
  426. * other threads consume the next command iocb.
  427. * SLI-2/SLI-3 provide different sized iocbs.
  428. **/
  429. static inline IOCB_t *
  430. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  431. {
  432. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  433. pring->cmdidx * phba->iocb_cmd_size);
  434. }
  435. /**
  436. * lpfc_resp_iocb - Get next response iocb entry in the ring
  437. * @phba: Pointer to HBA context object.
  438. * @pring: Pointer to driver SLI ring object.
  439. *
  440. * This function returns pointer to next response iocb entry
  441. * in the response ring. The caller must hold hbalock to make sure
  442. * that no other thread consume the next response iocb.
  443. * SLI-2/SLI-3 provide different sized iocbs.
  444. **/
  445. static inline IOCB_t *
  446. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  447. {
  448. return (IOCB_t *) (((char *) pring->rspringaddr) +
  449. pring->rspidx * phba->iocb_rsp_size);
  450. }
  451. /**
  452. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  453. * @phba: Pointer to HBA context object.
  454. *
  455. * This function is called with hbalock held. This function
  456. * allocates a new driver iocb object from the iocb pool. If the
  457. * allocation is successful, it returns pointer to the newly
  458. * allocated iocb object else it returns NULL.
  459. **/
  460. struct lpfc_iocbq *
  461. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  462. {
  463. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  464. struct lpfc_iocbq * iocbq = NULL;
  465. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  466. if (iocbq)
  467. phba->iocb_cnt++;
  468. if (phba->iocb_cnt > phba->iocb_max)
  469. phba->iocb_max = phba->iocb_cnt;
  470. return iocbq;
  471. }
  472. /**
  473. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  474. * @phba: Pointer to HBA context object.
  475. * @xritag: XRI value.
  476. *
  477. * This function clears the sglq pointer from the array of acive
  478. * sglq's. The xritag that is passed in is used to index into the
  479. * array. Before the xritag can be used it needs to be adjusted
  480. * by subtracting the xribase.
  481. *
  482. * Returns sglq ponter = success, NULL = Failure.
  483. **/
  484. static struct lpfc_sglq *
  485. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  486. {
  487. struct lpfc_sglq *sglq;
  488. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  489. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  490. return sglq;
  491. }
  492. /**
  493. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  494. * @phba: Pointer to HBA context object.
  495. * @xritag: XRI value.
  496. *
  497. * This function returns the sglq pointer from the array of acive
  498. * sglq's. The xritag that is passed in is used to index into the
  499. * array. Before the xritag can be used it needs to be adjusted
  500. * by subtracting the xribase.
  501. *
  502. * Returns sglq ponter = success, NULL = Failure.
  503. **/
  504. struct lpfc_sglq *
  505. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  506. {
  507. struct lpfc_sglq *sglq;
  508. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  509. return sglq;
  510. }
  511. /**
  512. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  513. * @phba: Pointer to HBA context object.
  514. * @xritag: xri used in this exchange.
  515. * @rrq: The RRQ to be cleared.
  516. *
  517. **/
  518. void
  519. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  520. uint16_t xritag,
  521. struct lpfc_node_rrq *rrq)
  522. {
  523. struct lpfc_nodelist *ndlp = NULL;
  524. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  525. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  526. /* The target DID could have been swapped (cable swap)
  527. * we should use the ndlp from the findnode if it is
  528. * available.
  529. */
  530. if ((!ndlp) && rrq->ndlp)
  531. ndlp = rrq->ndlp;
  532. if (!ndlp)
  533. goto out;
  534. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  535. rrq->send_rrq = 0;
  536. rrq->xritag = 0;
  537. rrq->rrq_stop_time = 0;
  538. }
  539. out:
  540. mempool_free(rrq, phba->rrq_pool);
  541. }
  542. /**
  543. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  544. * @phba: Pointer to HBA context object.
  545. *
  546. * This function is called with hbalock held. This function
  547. * Checks if stop_time (ratov from setting rrq active) has
  548. * been reached, if it has and the send_rrq flag is set then
  549. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  550. * then it will just call the routine to clear the rrq and
  551. * free the rrq resource.
  552. * The timer is set to the next rrq that is going to expire before
  553. * leaving the routine.
  554. *
  555. **/
  556. void
  557. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  558. {
  559. struct lpfc_node_rrq *rrq;
  560. struct lpfc_node_rrq *nextrrq;
  561. unsigned long next_time;
  562. unsigned long iflags;
  563. LIST_HEAD(send_rrq);
  564. spin_lock_irqsave(&phba->hbalock, iflags);
  565. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  566. next_time = jiffies + HZ * (phba->fc_ratov + 1);
  567. list_for_each_entry_safe(rrq, nextrrq,
  568. &phba->active_rrq_list, list) {
  569. if (time_after(jiffies, rrq->rrq_stop_time))
  570. list_move(&rrq->list, &send_rrq);
  571. else if (time_before(rrq->rrq_stop_time, next_time))
  572. next_time = rrq->rrq_stop_time;
  573. }
  574. spin_unlock_irqrestore(&phba->hbalock, iflags);
  575. if (!list_empty(&phba->active_rrq_list))
  576. mod_timer(&phba->rrq_tmr, next_time);
  577. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  578. list_del(&rrq->list);
  579. if (!rrq->send_rrq)
  580. /* this call will free the rrq */
  581. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  582. else if (lpfc_send_rrq(phba, rrq)) {
  583. /* if we send the rrq then the completion handler
  584. * will clear the bit in the xribitmap.
  585. */
  586. lpfc_clr_rrq_active(phba, rrq->xritag,
  587. rrq);
  588. }
  589. }
  590. }
  591. /**
  592. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  593. * @vport: Pointer to vport context object.
  594. * @xri: The xri used in the exchange.
  595. * @did: The targets DID for this exchange.
  596. *
  597. * returns NULL = rrq not found in the phba->active_rrq_list.
  598. * rrq = rrq for this xri and target.
  599. **/
  600. struct lpfc_node_rrq *
  601. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  602. {
  603. struct lpfc_hba *phba = vport->phba;
  604. struct lpfc_node_rrq *rrq;
  605. struct lpfc_node_rrq *nextrrq;
  606. unsigned long iflags;
  607. if (phba->sli_rev != LPFC_SLI_REV4)
  608. return NULL;
  609. spin_lock_irqsave(&phba->hbalock, iflags);
  610. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  611. if (rrq->vport == vport && rrq->xritag == xri &&
  612. rrq->nlp_DID == did){
  613. list_del(&rrq->list);
  614. spin_unlock_irqrestore(&phba->hbalock, iflags);
  615. return rrq;
  616. }
  617. }
  618. spin_unlock_irqrestore(&phba->hbalock, iflags);
  619. return NULL;
  620. }
  621. /**
  622. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  623. * @vport: Pointer to vport context object.
  624. * @ndlp: Pointer to the lpfc_node_list structure.
  625. * If ndlp is NULL Remove all active RRQs for this vport from the
  626. * phba->active_rrq_list and clear the rrq.
  627. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  628. **/
  629. void
  630. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  631. {
  632. struct lpfc_hba *phba = vport->phba;
  633. struct lpfc_node_rrq *rrq;
  634. struct lpfc_node_rrq *nextrrq;
  635. unsigned long iflags;
  636. LIST_HEAD(rrq_list);
  637. if (phba->sli_rev != LPFC_SLI_REV4)
  638. return;
  639. if (!ndlp) {
  640. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  641. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  642. }
  643. spin_lock_irqsave(&phba->hbalock, iflags);
  644. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  645. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  646. list_move(&rrq->list, &rrq_list);
  647. spin_unlock_irqrestore(&phba->hbalock, iflags);
  648. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  649. list_del(&rrq->list);
  650. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  651. }
  652. }
  653. /**
  654. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  655. * @phba: Pointer to HBA context object.
  656. *
  657. * Remove all rrqs from the phba->active_rrq_list and free them by
  658. * calling __lpfc_clr_active_rrq
  659. *
  660. **/
  661. void
  662. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  663. {
  664. struct lpfc_node_rrq *rrq;
  665. struct lpfc_node_rrq *nextrrq;
  666. unsigned long next_time;
  667. unsigned long iflags;
  668. LIST_HEAD(rrq_list);
  669. if (phba->sli_rev != LPFC_SLI_REV4)
  670. return;
  671. spin_lock_irqsave(&phba->hbalock, iflags);
  672. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  673. next_time = jiffies + HZ * (phba->fc_ratov * 2);
  674. list_splice_init(&phba->active_rrq_list, &rrq_list);
  675. spin_unlock_irqrestore(&phba->hbalock, iflags);
  676. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  677. list_del(&rrq->list);
  678. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  679. }
  680. if (!list_empty(&phba->active_rrq_list))
  681. mod_timer(&phba->rrq_tmr, next_time);
  682. }
  683. /**
  684. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  685. * @phba: Pointer to HBA context object.
  686. * @ndlp: Targets nodelist pointer for this exchange.
  687. * @xritag the xri in the bitmap to test.
  688. *
  689. * This function is called with hbalock held. This function
  690. * returns 0 = rrq not active for this xri
  691. * 1 = rrq is valid for this xri.
  692. **/
  693. int
  694. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  695. uint16_t xritag)
  696. {
  697. if (!ndlp)
  698. return 0;
  699. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  700. return 1;
  701. else
  702. return 0;
  703. }
  704. /**
  705. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  706. * @phba: Pointer to HBA context object.
  707. * @ndlp: nodelist pointer for this target.
  708. * @xritag: xri used in this exchange.
  709. * @rxid: Remote Exchange ID.
  710. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  711. *
  712. * This function takes the hbalock.
  713. * The active bit is always set in the active rrq xri_bitmap even
  714. * if there is no slot avaiable for the other rrq information.
  715. *
  716. * returns 0 rrq actived for this xri
  717. * < 0 No memory or invalid ndlp.
  718. **/
  719. int
  720. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  721. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  722. {
  723. unsigned long iflags;
  724. struct lpfc_node_rrq *rrq;
  725. int empty;
  726. if (!ndlp)
  727. return -EINVAL;
  728. if (!phba->cfg_enable_rrq)
  729. return -EINVAL;
  730. spin_lock_irqsave(&phba->hbalock, iflags);
  731. if (phba->pport->load_flag & FC_UNLOADING) {
  732. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  733. goto out;
  734. }
  735. /*
  736. * set the active bit even if there is no mem available.
  737. */
  738. if (NLP_CHK_FREE_REQ(ndlp))
  739. goto out;
  740. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  741. goto out;
  742. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  743. goto out;
  744. spin_unlock_irqrestore(&phba->hbalock, iflags);
  745. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  746. if (!rrq) {
  747. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  748. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  749. " DID:0x%x Send:%d\n",
  750. xritag, rxid, ndlp->nlp_DID, send_rrq);
  751. return -EINVAL;
  752. }
  753. rrq->send_rrq = send_rrq;
  754. rrq->xritag = xritag;
  755. rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
  756. rrq->ndlp = ndlp;
  757. rrq->nlp_DID = ndlp->nlp_DID;
  758. rrq->vport = ndlp->vport;
  759. rrq->rxid = rxid;
  760. rrq->send_rrq = send_rrq;
  761. spin_lock_irqsave(&phba->hbalock, iflags);
  762. empty = list_empty(&phba->active_rrq_list);
  763. list_add_tail(&rrq->list, &phba->active_rrq_list);
  764. phba->hba_flag |= HBA_RRQ_ACTIVE;
  765. if (empty)
  766. lpfc_worker_wake_up(phba);
  767. spin_unlock_irqrestore(&phba->hbalock, iflags);
  768. return 0;
  769. out:
  770. spin_unlock_irqrestore(&phba->hbalock, iflags);
  771. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  772. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  773. " DID:0x%x Send:%d\n",
  774. xritag, rxid, ndlp->nlp_DID, send_rrq);
  775. return -EINVAL;
  776. }
  777. /**
  778. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  779. * @phba: Pointer to HBA context object.
  780. * @piocb: Pointer to the iocbq.
  781. *
  782. * This function is called with hbalock held. This function
  783. * gets a new driver sglq object from the sglq list. If the
  784. * list is not empty then it is successful, it returns pointer to the newly
  785. * allocated sglq object else it returns NULL.
  786. **/
  787. static struct lpfc_sglq *
  788. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  789. {
  790. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  791. struct lpfc_sglq *sglq = NULL;
  792. struct lpfc_sglq *start_sglq = NULL;
  793. struct lpfc_scsi_buf *lpfc_cmd;
  794. struct lpfc_nodelist *ndlp;
  795. int found = 0;
  796. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  797. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  798. ndlp = lpfc_cmd->rdata->pnode;
  799. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  800. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  801. ndlp = piocbq->context_un.ndlp;
  802. else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
  803. (piocbq->iocb_flag & LPFC_IO_LIBDFC))
  804. ndlp = piocbq->context_un.ndlp;
  805. else
  806. ndlp = piocbq->context1;
  807. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  808. start_sglq = sglq;
  809. while (!found) {
  810. if (!sglq)
  811. return NULL;
  812. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
  813. /* This xri has an rrq outstanding for this DID.
  814. * put it back in the list and get another xri.
  815. */
  816. list_add_tail(&sglq->list, lpfc_sgl_list);
  817. sglq = NULL;
  818. list_remove_head(lpfc_sgl_list, sglq,
  819. struct lpfc_sglq, list);
  820. if (sglq == start_sglq) {
  821. sglq = NULL;
  822. break;
  823. } else
  824. continue;
  825. }
  826. sglq->ndlp = ndlp;
  827. found = 1;
  828. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  829. sglq->state = SGL_ALLOCATED;
  830. }
  831. return sglq;
  832. }
  833. /**
  834. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  835. * @phba: Pointer to HBA context object.
  836. *
  837. * This function is called with no lock held. This function
  838. * allocates a new driver iocb object from the iocb pool. If the
  839. * allocation is successful, it returns pointer to the newly
  840. * allocated iocb object else it returns NULL.
  841. **/
  842. struct lpfc_iocbq *
  843. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  844. {
  845. struct lpfc_iocbq * iocbq = NULL;
  846. unsigned long iflags;
  847. spin_lock_irqsave(&phba->hbalock, iflags);
  848. iocbq = __lpfc_sli_get_iocbq(phba);
  849. spin_unlock_irqrestore(&phba->hbalock, iflags);
  850. return iocbq;
  851. }
  852. /**
  853. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  854. * @phba: Pointer to HBA context object.
  855. * @iocbq: Pointer to driver iocb object.
  856. *
  857. * This function is called with hbalock held to release driver
  858. * iocb object to the iocb pool. The iotag in the iocb object
  859. * does not change for each use of the iocb object. This function
  860. * clears all other fields of the iocb object when it is freed.
  861. * The sqlq structure that holds the xritag and phys and virtual
  862. * mappings for the scatter gather list is retrieved from the
  863. * active array of sglq. The get of the sglq pointer also clears
  864. * the entry in the array. If the status of the IO indiactes that
  865. * this IO was aborted then the sglq entry it put on the
  866. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  867. * IO has good status or fails for any other reason then the sglq
  868. * entry is added to the free list (lpfc_sgl_list).
  869. **/
  870. static void
  871. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  872. {
  873. struct lpfc_sglq *sglq;
  874. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  875. unsigned long iflag = 0;
  876. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  877. if (iocbq->sli4_xritag == NO_XRI)
  878. sglq = NULL;
  879. else
  880. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  881. if (sglq) {
  882. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  883. (sglq->state != SGL_XRI_ABORTED)) {
  884. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  885. iflag);
  886. list_add(&sglq->list,
  887. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  888. spin_unlock_irqrestore(
  889. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  890. } else {
  891. sglq->state = SGL_FREED;
  892. sglq->ndlp = NULL;
  893. list_add_tail(&sglq->list,
  894. &phba->sli4_hba.lpfc_sgl_list);
  895. /* Check if TXQ queue needs to be serviced */
  896. if (pring->txq_cnt)
  897. lpfc_worker_wake_up(phba);
  898. }
  899. }
  900. /*
  901. * Clean all volatile data fields, preserve iotag and node struct.
  902. */
  903. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  904. iocbq->sli4_lxritag = NO_XRI;
  905. iocbq->sli4_xritag = NO_XRI;
  906. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  907. }
  908. /**
  909. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  910. * @phba: Pointer to HBA context object.
  911. * @iocbq: Pointer to driver iocb object.
  912. *
  913. * This function is called with hbalock held to release driver
  914. * iocb object to the iocb pool. The iotag in the iocb object
  915. * does not change for each use of the iocb object. This function
  916. * clears all other fields of the iocb object when it is freed.
  917. **/
  918. static void
  919. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  920. {
  921. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  922. /*
  923. * Clean all volatile data fields, preserve iotag and node struct.
  924. */
  925. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  926. iocbq->sli4_xritag = NO_XRI;
  927. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  928. }
  929. /**
  930. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  931. * @phba: Pointer to HBA context object.
  932. * @iocbq: Pointer to driver iocb object.
  933. *
  934. * This function is called with hbalock held to release driver
  935. * iocb object to the iocb pool. The iotag in the iocb object
  936. * does not change for each use of the iocb object. This function
  937. * clears all other fields of the iocb object when it is freed.
  938. **/
  939. static void
  940. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  941. {
  942. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  943. phba->iocb_cnt--;
  944. }
  945. /**
  946. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  947. * @phba: Pointer to HBA context object.
  948. * @iocbq: Pointer to driver iocb object.
  949. *
  950. * This function is called with no lock held to release the iocb to
  951. * iocb pool.
  952. **/
  953. void
  954. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  955. {
  956. unsigned long iflags;
  957. /*
  958. * Clean all volatile data fields, preserve iotag and node struct.
  959. */
  960. spin_lock_irqsave(&phba->hbalock, iflags);
  961. __lpfc_sli_release_iocbq(phba, iocbq);
  962. spin_unlock_irqrestore(&phba->hbalock, iflags);
  963. }
  964. /**
  965. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  966. * @phba: Pointer to HBA context object.
  967. * @iocblist: List of IOCBs.
  968. * @ulpstatus: ULP status in IOCB command field.
  969. * @ulpWord4: ULP word-4 in IOCB command field.
  970. *
  971. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  972. * on the list by invoking the complete callback function associated with the
  973. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  974. * fields.
  975. **/
  976. void
  977. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  978. uint32_t ulpstatus, uint32_t ulpWord4)
  979. {
  980. struct lpfc_iocbq *piocb;
  981. while (!list_empty(iocblist)) {
  982. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  983. if (!piocb->iocb_cmpl)
  984. lpfc_sli_release_iocbq(phba, piocb);
  985. else {
  986. piocb->iocb.ulpStatus = ulpstatus;
  987. piocb->iocb.un.ulpWord[4] = ulpWord4;
  988. (piocb->iocb_cmpl) (phba, piocb, piocb);
  989. }
  990. }
  991. return;
  992. }
  993. /**
  994. * lpfc_sli_iocb_cmd_type - Get the iocb type
  995. * @iocb_cmnd: iocb command code.
  996. *
  997. * This function is called by ring event handler function to get the iocb type.
  998. * This function translates the iocb command to an iocb command type used to
  999. * decide the final disposition of each completed IOCB.
  1000. * The function returns
  1001. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  1002. * LPFC_SOL_IOCB if it is a solicited iocb completion
  1003. * LPFC_ABORT_IOCB if it is an abort iocb
  1004. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  1005. *
  1006. * The caller is not required to hold any lock.
  1007. **/
  1008. static lpfc_iocb_type
  1009. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1010. {
  1011. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1012. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1013. return 0;
  1014. switch (iocb_cmnd) {
  1015. case CMD_XMIT_SEQUENCE_CR:
  1016. case CMD_XMIT_SEQUENCE_CX:
  1017. case CMD_XMIT_BCAST_CN:
  1018. case CMD_XMIT_BCAST_CX:
  1019. case CMD_ELS_REQUEST_CR:
  1020. case CMD_ELS_REQUEST_CX:
  1021. case CMD_CREATE_XRI_CR:
  1022. case CMD_CREATE_XRI_CX:
  1023. case CMD_GET_RPI_CN:
  1024. case CMD_XMIT_ELS_RSP_CX:
  1025. case CMD_GET_RPI_CR:
  1026. case CMD_FCP_IWRITE_CR:
  1027. case CMD_FCP_IWRITE_CX:
  1028. case CMD_FCP_IREAD_CR:
  1029. case CMD_FCP_IREAD_CX:
  1030. case CMD_FCP_ICMND_CR:
  1031. case CMD_FCP_ICMND_CX:
  1032. case CMD_FCP_TSEND_CX:
  1033. case CMD_FCP_TRSP_CX:
  1034. case CMD_FCP_TRECEIVE_CX:
  1035. case CMD_FCP_AUTO_TRSP_CX:
  1036. case CMD_ADAPTER_MSG:
  1037. case CMD_ADAPTER_DUMP:
  1038. case CMD_XMIT_SEQUENCE64_CR:
  1039. case CMD_XMIT_SEQUENCE64_CX:
  1040. case CMD_XMIT_BCAST64_CN:
  1041. case CMD_XMIT_BCAST64_CX:
  1042. case CMD_ELS_REQUEST64_CR:
  1043. case CMD_ELS_REQUEST64_CX:
  1044. case CMD_FCP_IWRITE64_CR:
  1045. case CMD_FCP_IWRITE64_CX:
  1046. case CMD_FCP_IREAD64_CR:
  1047. case CMD_FCP_IREAD64_CX:
  1048. case CMD_FCP_ICMND64_CR:
  1049. case CMD_FCP_ICMND64_CX:
  1050. case CMD_FCP_TSEND64_CX:
  1051. case CMD_FCP_TRSP64_CX:
  1052. case CMD_FCP_TRECEIVE64_CX:
  1053. case CMD_GEN_REQUEST64_CR:
  1054. case CMD_GEN_REQUEST64_CX:
  1055. case CMD_XMIT_ELS_RSP64_CX:
  1056. case DSSCMD_IWRITE64_CR:
  1057. case DSSCMD_IWRITE64_CX:
  1058. case DSSCMD_IREAD64_CR:
  1059. case DSSCMD_IREAD64_CX:
  1060. type = LPFC_SOL_IOCB;
  1061. break;
  1062. case CMD_ABORT_XRI_CN:
  1063. case CMD_ABORT_XRI_CX:
  1064. case CMD_CLOSE_XRI_CN:
  1065. case CMD_CLOSE_XRI_CX:
  1066. case CMD_XRI_ABORTED_CX:
  1067. case CMD_ABORT_MXRI64_CN:
  1068. case CMD_XMIT_BLS_RSP64_CX:
  1069. type = LPFC_ABORT_IOCB;
  1070. break;
  1071. case CMD_RCV_SEQUENCE_CX:
  1072. case CMD_RCV_ELS_REQ_CX:
  1073. case CMD_RCV_SEQUENCE64_CX:
  1074. case CMD_RCV_ELS_REQ64_CX:
  1075. case CMD_ASYNC_STATUS:
  1076. case CMD_IOCB_RCV_SEQ64_CX:
  1077. case CMD_IOCB_RCV_ELS64_CX:
  1078. case CMD_IOCB_RCV_CONT64_CX:
  1079. case CMD_IOCB_RET_XRI64_CX:
  1080. type = LPFC_UNSOL_IOCB;
  1081. break;
  1082. case CMD_IOCB_XMIT_MSEQ64_CR:
  1083. case CMD_IOCB_XMIT_MSEQ64_CX:
  1084. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1085. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1086. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1087. case CMD_IOCB_ABORT_EXTENDED_CN:
  1088. case CMD_IOCB_RET_HBQE64_CN:
  1089. case CMD_IOCB_FCP_IBIDIR64_CR:
  1090. case CMD_IOCB_FCP_IBIDIR64_CX:
  1091. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1092. case CMD_IOCB_LOGENTRY_CN:
  1093. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1094. printk("%s - Unhandled SLI-3 Command x%x\n",
  1095. __func__, iocb_cmnd);
  1096. type = LPFC_UNKNOWN_IOCB;
  1097. break;
  1098. default:
  1099. type = LPFC_UNKNOWN_IOCB;
  1100. break;
  1101. }
  1102. return type;
  1103. }
  1104. /**
  1105. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1106. * @phba: Pointer to HBA context object.
  1107. *
  1108. * This function is called from SLI initialization code
  1109. * to configure every ring of the HBA's SLI interface. The
  1110. * caller is not required to hold any lock. This function issues
  1111. * a config_ring mailbox command for each ring.
  1112. * This function returns zero if successful else returns a negative
  1113. * error code.
  1114. **/
  1115. static int
  1116. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1117. {
  1118. struct lpfc_sli *psli = &phba->sli;
  1119. LPFC_MBOXQ_t *pmb;
  1120. MAILBOX_t *pmbox;
  1121. int i, rc, ret = 0;
  1122. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1123. if (!pmb)
  1124. return -ENOMEM;
  1125. pmbox = &pmb->u.mb;
  1126. phba->link_state = LPFC_INIT_MBX_CMDS;
  1127. for (i = 0; i < psli->num_rings; i++) {
  1128. lpfc_config_ring(phba, i, pmb);
  1129. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1130. if (rc != MBX_SUCCESS) {
  1131. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1132. "0446 Adapter failed to init (%d), "
  1133. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1134. "ring %d\n",
  1135. rc, pmbox->mbxCommand,
  1136. pmbox->mbxStatus, i);
  1137. phba->link_state = LPFC_HBA_ERROR;
  1138. ret = -ENXIO;
  1139. break;
  1140. }
  1141. }
  1142. mempool_free(pmb, phba->mbox_mem_pool);
  1143. return ret;
  1144. }
  1145. /**
  1146. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1147. * @phba: Pointer to HBA context object.
  1148. * @pring: Pointer to driver SLI ring object.
  1149. * @piocb: Pointer to the driver iocb object.
  1150. *
  1151. * This function is called with hbalock held. The function adds the
  1152. * new iocb to txcmplq of the given ring. This function always returns
  1153. * 0. If this function is called for ELS ring, this function checks if
  1154. * there is a vport associated with the ELS command. This function also
  1155. * starts els_tmofunc timer if this is an ELS command.
  1156. **/
  1157. static int
  1158. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1159. struct lpfc_iocbq *piocb)
  1160. {
  1161. list_add_tail(&piocb->list, &pring->txcmplq);
  1162. piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
  1163. pring->txcmplq_cnt++;
  1164. if (pring->txcmplq_cnt > pring->txcmplq_max)
  1165. pring->txcmplq_max = pring->txcmplq_cnt;
  1166. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1167. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1168. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1169. if (!piocb->vport)
  1170. BUG();
  1171. else
  1172. mod_timer(&piocb->vport->els_tmofunc,
  1173. jiffies + HZ * (phba->fc_ratov << 1));
  1174. }
  1175. return 0;
  1176. }
  1177. /**
  1178. * lpfc_sli_ringtx_get - Get first element of the txq
  1179. * @phba: Pointer to HBA context object.
  1180. * @pring: Pointer to driver SLI ring object.
  1181. *
  1182. * This function is called with hbalock held to get next
  1183. * iocb in txq of the given ring. If there is any iocb in
  1184. * the txq, the function returns first iocb in the list after
  1185. * removing the iocb from the list, else it returns NULL.
  1186. **/
  1187. struct lpfc_iocbq *
  1188. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1189. {
  1190. struct lpfc_iocbq *cmd_iocb;
  1191. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1192. if (cmd_iocb != NULL)
  1193. pring->txq_cnt--;
  1194. return cmd_iocb;
  1195. }
  1196. /**
  1197. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1198. * @phba: Pointer to HBA context object.
  1199. * @pring: Pointer to driver SLI ring object.
  1200. *
  1201. * This function is called with hbalock held and the caller must post the
  1202. * iocb without releasing the lock. If the caller releases the lock,
  1203. * iocb slot returned by the function is not guaranteed to be available.
  1204. * The function returns pointer to the next available iocb slot if there
  1205. * is available slot in the ring, else it returns NULL.
  1206. * If the get index of the ring is ahead of the put index, the function
  1207. * will post an error attention event to the worker thread to take the
  1208. * HBA to offline state.
  1209. **/
  1210. static IOCB_t *
  1211. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1212. {
  1213. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1214. uint32_t max_cmd_idx = pring->numCiocb;
  1215. if ((pring->next_cmdidx == pring->cmdidx) &&
  1216. (++pring->next_cmdidx >= max_cmd_idx))
  1217. pring->next_cmdidx = 0;
  1218. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  1219. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1220. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  1221. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1222. "0315 Ring %d issue: portCmdGet %d "
  1223. "is bigger than cmd ring %d\n",
  1224. pring->ringno,
  1225. pring->local_getidx, max_cmd_idx);
  1226. phba->link_state = LPFC_HBA_ERROR;
  1227. /*
  1228. * All error attention handlers are posted to
  1229. * worker thread
  1230. */
  1231. phba->work_ha |= HA_ERATT;
  1232. phba->work_hs = HS_FFER3;
  1233. lpfc_worker_wake_up(phba);
  1234. return NULL;
  1235. }
  1236. if (pring->local_getidx == pring->next_cmdidx)
  1237. return NULL;
  1238. }
  1239. return lpfc_cmd_iocb(phba, pring);
  1240. }
  1241. /**
  1242. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1243. * @phba: Pointer to HBA context object.
  1244. * @iocbq: Pointer to driver iocb object.
  1245. *
  1246. * This function gets an iotag for the iocb. If there is no unused iotag and
  1247. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1248. * array and assigns a new iotag.
  1249. * The function returns the allocated iotag if successful, else returns zero.
  1250. * Zero is not a valid iotag.
  1251. * The caller is not required to hold any lock.
  1252. **/
  1253. uint16_t
  1254. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1255. {
  1256. struct lpfc_iocbq **new_arr;
  1257. struct lpfc_iocbq **old_arr;
  1258. size_t new_len;
  1259. struct lpfc_sli *psli = &phba->sli;
  1260. uint16_t iotag;
  1261. spin_lock_irq(&phba->hbalock);
  1262. iotag = psli->last_iotag;
  1263. if(++iotag < psli->iocbq_lookup_len) {
  1264. psli->last_iotag = iotag;
  1265. psli->iocbq_lookup[iotag] = iocbq;
  1266. spin_unlock_irq(&phba->hbalock);
  1267. iocbq->iotag = iotag;
  1268. return iotag;
  1269. } else if (psli->iocbq_lookup_len < (0xffff
  1270. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1271. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1272. spin_unlock_irq(&phba->hbalock);
  1273. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1274. GFP_KERNEL);
  1275. if (new_arr) {
  1276. spin_lock_irq(&phba->hbalock);
  1277. old_arr = psli->iocbq_lookup;
  1278. if (new_len <= psli->iocbq_lookup_len) {
  1279. /* highly unprobable case */
  1280. kfree(new_arr);
  1281. iotag = psli->last_iotag;
  1282. if(++iotag < psli->iocbq_lookup_len) {
  1283. psli->last_iotag = iotag;
  1284. psli->iocbq_lookup[iotag] = iocbq;
  1285. spin_unlock_irq(&phba->hbalock);
  1286. iocbq->iotag = iotag;
  1287. return iotag;
  1288. }
  1289. spin_unlock_irq(&phba->hbalock);
  1290. return 0;
  1291. }
  1292. if (psli->iocbq_lookup)
  1293. memcpy(new_arr, old_arr,
  1294. ((psli->last_iotag + 1) *
  1295. sizeof (struct lpfc_iocbq *)));
  1296. psli->iocbq_lookup = new_arr;
  1297. psli->iocbq_lookup_len = new_len;
  1298. psli->last_iotag = iotag;
  1299. psli->iocbq_lookup[iotag] = iocbq;
  1300. spin_unlock_irq(&phba->hbalock);
  1301. iocbq->iotag = iotag;
  1302. kfree(old_arr);
  1303. return iotag;
  1304. }
  1305. } else
  1306. spin_unlock_irq(&phba->hbalock);
  1307. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1308. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1309. psli->last_iotag);
  1310. return 0;
  1311. }
  1312. /**
  1313. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1314. * @phba: Pointer to HBA context object.
  1315. * @pring: Pointer to driver SLI ring object.
  1316. * @iocb: Pointer to iocb slot in the ring.
  1317. * @nextiocb: Pointer to driver iocb object which need to be
  1318. * posted to firmware.
  1319. *
  1320. * This function is called with hbalock held to post a new iocb to
  1321. * the firmware. This function copies the new iocb to ring iocb slot and
  1322. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1323. * a completion call back for this iocb else the function will free the
  1324. * iocb object.
  1325. **/
  1326. static void
  1327. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1328. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1329. {
  1330. /*
  1331. * Set up an iotag
  1332. */
  1333. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1334. if (pring->ringno == LPFC_ELS_RING) {
  1335. lpfc_debugfs_slow_ring_trc(phba,
  1336. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1337. *(((uint32_t *) &nextiocb->iocb) + 4),
  1338. *(((uint32_t *) &nextiocb->iocb) + 6),
  1339. *(((uint32_t *) &nextiocb->iocb) + 7));
  1340. }
  1341. /*
  1342. * Issue iocb command to adapter
  1343. */
  1344. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1345. wmb();
  1346. pring->stats.iocb_cmd++;
  1347. /*
  1348. * If there is no completion routine to call, we can release the
  1349. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1350. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1351. */
  1352. if (nextiocb->iocb_cmpl)
  1353. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1354. else
  1355. __lpfc_sli_release_iocbq(phba, nextiocb);
  1356. /*
  1357. * Let the HBA know what IOCB slot will be the next one the
  1358. * driver will put a command into.
  1359. */
  1360. pring->cmdidx = pring->next_cmdidx;
  1361. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1362. }
  1363. /**
  1364. * lpfc_sli_update_full_ring - Update the chip attention register
  1365. * @phba: Pointer to HBA context object.
  1366. * @pring: Pointer to driver SLI ring object.
  1367. *
  1368. * The caller is not required to hold any lock for calling this function.
  1369. * This function updates the chip attention bits for the ring to inform firmware
  1370. * that there are pending work to be done for this ring and requests an
  1371. * interrupt when there is space available in the ring. This function is
  1372. * called when the driver is unable to post more iocbs to the ring due
  1373. * to unavailability of space in the ring.
  1374. **/
  1375. static void
  1376. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1377. {
  1378. int ringno = pring->ringno;
  1379. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1380. wmb();
  1381. /*
  1382. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1383. * The HBA will tell us when an IOCB entry is available.
  1384. */
  1385. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1386. readl(phba->CAregaddr); /* flush */
  1387. pring->stats.iocb_cmd_full++;
  1388. }
  1389. /**
  1390. * lpfc_sli_update_ring - Update chip attention register
  1391. * @phba: Pointer to HBA context object.
  1392. * @pring: Pointer to driver SLI ring object.
  1393. *
  1394. * This function updates the chip attention register bit for the
  1395. * given ring to inform HBA that there is more work to be done
  1396. * in this ring. The caller is not required to hold any lock.
  1397. **/
  1398. static void
  1399. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1400. {
  1401. int ringno = pring->ringno;
  1402. /*
  1403. * Tell the HBA that there is work to do in this ring.
  1404. */
  1405. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1406. wmb();
  1407. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1408. readl(phba->CAregaddr); /* flush */
  1409. }
  1410. }
  1411. /**
  1412. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1413. * @phba: Pointer to HBA context object.
  1414. * @pring: Pointer to driver SLI ring object.
  1415. *
  1416. * This function is called with hbalock held to post pending iocbs
  1417. * in the txq to the firmware. This function is called when driver
  1418. * detects space available in the ring.
  1419. **/
  1420. static void
  1421. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1422. {
  1423. IOCB_t *iocb;
  1424. struct lpfc_iocbq *nextiocb;
  1425. /*
  1426. * Check to see if:
  1427. * (a) there is anything on the txq to send
  1428. * (b) link is up
  1429. * (c) link attention events can be processed (fcp ring only)
  1430. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1431. */
  1432. if (pring->txq_cnt &&
  1433. lpfc_is_link_up(phba) &&
  1434. (pring->ringno != phba->sli.fcp_ring ||
  1435. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1436. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1437. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1438. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1439. if (iocb)
  1440. lpfc_sli_update_ring(phba, pring);
  1441. else
  1442. lpfc_sli_update_full_ring(phba, pring);
  1443. }
  1444. return;
  1445. }
  1446. /**
  1447. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1448. * @phba: Pointer to HBA context object.
  1449. * @hbqno: HBQ number.
  1450. *
  1451. * This function is called with hbalock held to get the next
  1452. * available slot for the given HBQ. If there is free slot
  1453. * available for the HBQ it will return pointer to the next available
  1454. * HBQ entry else it will return NULL.
  1455. **/
  1456. static struct lpfc_hbq_entry *
  1457. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1458. {
  1459. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1460. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1461. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1462. hbqp->next_hbqPutIdx = 0;
  1463. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1464. uint32_t raw_index = phba->hbq_get[hbqno];
  1465. uint32_t getidx = le32_to_cpu(raw_index);
  1466. hbqp->local_hbqGetIdx = getidx;
  1467. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1468. lpfc_printf_log(phba, KERN_ERR,
  1469. LOG_SLI | LOG_VPORT,
  1470. "1802 HBQ %d: local_hbqGetIdx "
  1471. "%u is > than hbqp->entry_count %u\n",
  1472. hbqno, hbqp->local_hbqGetIdx,
  1473. hbqp->entry_count);
  1474. phba->link_state = LPFC_HBA_ERROR;
  1475. return NULL;
  1476. }
  1477. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1478. return NULL;
  1479. }
  1480. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1481. hbqp->hbqPutIdx;
  1482. }
  1483. /**
  1484. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1485. * @phba: Pointer to HBA context object.
  1486. *
  1487. * This function is called with no lock held to free all the
  1488. * hbq buffers while uninitializing the SLI interface. It also
  1489. * frees the HBQ buffers returned by the firmware but not yet
  1490. * processed by the upper layers.
  1491. **/
  1492. void
  1493. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1494. {
  1495. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1496. struct hbq_dmabuf *hbq_buf;
  1497. unsigned long flags;
  1498. int i, hbq_count;
  1499. uint32_t hbqno;
  1500. hbq_count = lpfc_sli_hbq_count();
  1501. /* Return all memory used by all HBQs */
  1502. spin_lock_irqsave(&phba->hbalock, flags);
  1503. for (i = 0; i < hbq_count; ++i) {
  1504. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1505. &phba->hbqs[i].hbq_buffer_list, list) {
  1506. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1507. list_del(&hbq_buf->dbuf.list);
  1508. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1509. }
  1510. phba->hbqs[i].buffer_count = 0;
  1511. }
  1512. /* Return all HBQ buffer that are in-fly */
  1513. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1514. list) {
  1515. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1516. list_del(&hbq_buf->dbuf.list);
  1517. if (hbq_buf->tag == -1) {
  1518. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1519. (phba, hbq_buf);
  1520. } else {
  1521. hbqno = hbq_buf->tag >> 16;
  1522. if (hbqno >= LPFC_MAX_HBQS)
  1523. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1524. (phba, hbq_buf);
  1525. else
  1526. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1527. hbq_buf);
  1528. }
  1529. }
  1530. /* Mark the HBQs not in use */
  1531. phba->hbq_in_use = 0;
  1532. spin_unlock_irqrestore(&phba->hbalock, flags);
  1533. }
  1534. /**
  1535. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1536. * @phba: Pointer to HBA context object.
  1537. * @hbqno: HBQ number.
  1538. * @hbq_buf: Pointer to HBQ buffer.
  1539. *
  1540. * This function is called with the hbalock held to post a
  1541. * hbq buffer to the firmware. If the function finds an empty
  1542. * slot in the HBQ, it will post the buffer. The function will return
  1543. * pointer to the hbq entry if it successfully post the buffer
  1544. * else it will return NULL.
  1545. **/
  1546. static int
  1547. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1548. struct hbq_dmabuf *hbq_buf)
  1549. {
  1550. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1551. }
  1552. /**
  1553. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1554. * @phba: Pointer to HBA context object.
  1555. * @hbqno: HBQ number.
  1556. * @hbq_buf: Pointer to HBQ buffer.
  1557. *
  1558. * This function is called with the hbalock held to post a hbq buffer to the
  1559. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1560. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1561. * it successfully post the buffer else it will return an error.
  1562. **/
  1563. static int
  1564. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1565. struct hbq_dmabuf *hbq_buf)
  1566. {
  1567. struct lpfc_hbq_entry *hbqe;
  1568. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1569. /* Get next HBQ entry slot to use */
  1570. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1571. if (hbqe) {
  1572. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1573. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1574. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1575. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1576. hbqe->bde.tus.f.bdeFlags = 0;
  1577. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1578. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1579. /* Sync SLIM */
  1580. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1581. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1582. /* flush */
  1583. readl(phba->hbq_put + hbqno);
  1584. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1585. return 0;
  1586. } else
  1587. return -ENOMEM;
  1588. }
  1589. /**
  1590. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1591. * @phba: Pointer to HBA context object.
  1592. * @hbqno: HBQ number.
  1593. * @hbq_buf: Pointer to HBQ buffer.
  1594. *
  1595. * This function is called with the hbalock held to post an RQE to the SLI4
  1596. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1597. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1598. **/
  1599. static int
  1600. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1601. struct hbq_dmabuf *hbq_buf)
  1602. {
  1603. int rc;
  1604. struct lpfc_rqe hrqe;
  1605. struct lpfc_rqe drqe;
  1606. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1607. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1608. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1609. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1610. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1611. &hrqe, &drqe);
  1612. if (rc < 0)
  1613. return rc;
  1614. hbq_buf->tag = rc;
  1615. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1616. return 0;
  1617. }
  1618. /* HBQ for ELS and CT traffic. */
  1619. static struct lpfc_hbq_init lpfc_els_hbq = {
  1620. .rn = 1,
  1621. .entry_count = 256,
  1622. .mask_count = 0,
  1623. .profile = 0,
  1624. .ring_mask = (1 << LPFC_ELS_RING),
  1625. .buffer_count = 0,
  1626. .init_count = 40,
  1627. .add_count = 40,
  1628. };
  1629. /* HBQ for the extra ring if needed */
  1630. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1631. .rn = 1,
  1632. .entry_count = 200,
  1633. .mask_count = 0,
  1634. .profile = 0,
  1635. .ring_mask = (1 << LPFC_EXTRA_RING),
  1636. .buffer_count = 0,
  1637. .init_count = 0,
  1638. .add_count = 5,
  1639. };
  1640. /* Array of HBQs */
  1641. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1642. &lpfc_els_hbq,
  1643. &lpfc_extra_hbq,
  1644. };
  1645. /**
  1646. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1647. * @phba: Pointer to HBA context object.
  1648. * @hbqno: HBQ number.
  1649. * @count: Number of HBQ buffers to be posted.
  1650. *
  1651. * This function is called with no lock held to post more hbq buffers to the
  1652. * given HBQ. The function returns the number of HBQ buffers successfully
  1653. * posted.
  1654. **/
  1655. static int
  1656. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1657. {
  1658. uint32_t i, posted = 0;
  1659. unsigned long flags;
  1660. struct hbq_dmabuf *hbq_buffer;
  1661. LIST_HEAD(hbq_buf_list);
  1662. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1663. return 0;
  1664. if ((phba->hbqs[hbqno].buffer_count + count) >
  1665. lpfc_hbq_defs[hbqno]->entry_count)
  1666. count = lpfc_hbq_defs[hbqno]->entry_count -
  1667. phba->hbqs[hbqno].buffer_count;
  1668. if (!count)
  1669. return 0;
  1670. /* Allocate HBQ entries */
  1671. for (i = 0; i < count; i++) {
  1672. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1673. if (!hbq_buffer)
  1674. break;
  1675. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1676. }
  1677. /* Check whether HBQ is still in use */
  1678. spin_lock_irqsave(&phba->hbalock, flags);
  1679. if (!phba->hbq_in_use)
  1680. goto err;
  1681. while (!list_empty(&hbq_buf_list)) {
  1682. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1683. dbuf.list);
  1684. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1685. (hbqno << 16));
  1686. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1687. phba->hbqs[hbqno].buffer_count++;
  1688. posted++;
  1689. } else
  1690. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1691. }
  1692. spin_unlock_irqrestore(&phba->hbalock, flags);
  1693. return posted;
  1694. err:
  1695. spin_unlock_irqrestore(&phba->hbalock, flags);
  1696. while (!list_empty(&hbq_buf_list)) {
  1697. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1698. dbuf.list);
  1699. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1700. }
  1701. return 0;
  1702. }
  1703. /**
  1704. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1705. * @phba: Pointer to HBA context object.
  1706. * @qno: HBQ number.
  1707. *
  1708. * This function posts more buffers to the HBQ. This function
  1709. * is called with no lock held. The function returns the number of HBQ entries
  1710. * successfully allocated.
  1711. **/
  1712. int
  1713. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1714. {
  1715. if (phba->sli_rev == LPFC_SLI_REV4)
  1716. return 0;
  1717. else
  1718. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1719. lpfc_hbq_defs[qno]->add_count);
  1720. }
  1721. /**
  1722. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1723. * @phba: Pointer to HBA context object.
  1724. * @qno: HBQ queue number.
  1725. *
  1726. * This function is called from SLI initialization code path with
  1727. * no lock held to post initial HBQ buffers to firmware. The
  1728. * function returns the number of HBQ entries successfully allocated.
  1729. **/
  1730. static int
  1731. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1732. {
  1733. if (phba->sli_rev == LPFC_SLI_REV4)
  1734. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1735. lpfc_hbq_defs[qno]->entry_count);
  1736. else
  1737. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1738. lpfc_hbq_defs[qno]->init_count);
  1739. }
  1740. /**
  1741. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1742. * @phba: Pointer to HBA context object.
  1743. * @hbqno: HBQ number.
  1744. *
  1745. * This function removes the first hbq buffer on an hbq list and returns a
  1746. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1747. **/
  1748. static struct hbq_dmabuf *
  1749. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1750. {
  1751. struct lpfc_dmabuf *d_buf;
  1752. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1753. if (!d_buf)
  1754. return NULL;
  1755. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1756. }
  1757. /**
  1758. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1759. * @phba: Pointer to HBA context object.
  1760. * @tag: Tag of the hbq buffer.
  1761. *
  1762. * This function is called with hbalock held. This function searches
  1763. * for the hbq buffer associated with the given tag in the hbq buffer
  1764. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1765. * it returns NULL.
  1766. **/
  1767. static struct hbq_dmabuf *
  1768. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1769. {
  1770. struct lpfc_dmabuf *d_buf;
  1771. struct hbq_dmabuf *hbq_buf;
  1772. uint32_t hbqno;
  1773. hbqno = tag >> 16;
  1774. if (hbqno >= LPFC_MAX_HBQS)
  1775. return NULL;
  1776. spin_lock_irq(&phba->hbalock);
  1777. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1778. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1779. if (hbq_buf->tag == tag) {
  1780. spin_unlock_irq(&phba->hbalock);
  1781. return hbq_buf;
  1782. }
  1783. }
  1784. spin_unlock_irq(&phba->hbalock);
  1785. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1786. "1803 Bad hbq tag. Data: x%x x%x\n",
  1787. tag, phba->hbqs[tag >> 16].buffer_count);
  1788. return NULL;
  1789. }
  1790. /**
  1791. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1792. * @phba: Pointer to HBA context object.
  1793. * @hbq_buffer: Pointer to HBQ buffer.
  1794. *
  1795. * This function is called with hbalock. This function gives back
  1796. * the hbq buffer to firmware. If the HBQ does not have space to
  1797. * post the buffer, it will free the buffer.
  1798. **/
  1799. void
  1800. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1801. {
  1802. uint32_t hbqno;
  1803. if (hbq_buffer) {
  1804. hbqno = hbq_buffer->tag >> 16;
  1805. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1806. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1807. }
  1808. }
  1809. /**
  1810. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1811. * @mbxCommand: mailbox command code.
  1812. *
  1813. * This function is called by the mailbox event handler function to verify
  1814. * that the completed mailbox command is a legitimate mailbox command. If the
  1815. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1816. * and the mailbox event handler will take the HBA offline.
  1817. **/
  1818. static int
  1819. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1820. {
  1821. uint8_t ret;
  1822. switch (mbxCommand) {
  1823. case MBX_LOAD_SM:
  1824. case MBX_READ_NV:
  1825. case MBX_WRITE_NV:
  1826. case MBX_WRITE_VPARMS:
  1827. case MBX_RUN_BIU_DIAG:
  1828. case MBX_INIT_LINK:
  1829. case MBX_DOWN_LINK:
  1830. case MBX_CONFIG_LINK:
  1831. case MBX_CONFIG_RING:
  1832. case MBX_RESET_RING:
  1833. case MBX_READ_CONFIG:
  1834. case MBX_READ_RCONFIG:
  1835. case MBX_READ_SPARM:
  1836. case MBX_READ_STATUS:
  1837. case MBX_READ_RPI:
  1838. case MBX_READ_XRI:
  1839. case MBX_READ_REV:
  1840. case MBX_READ_LNK_STAT:
  1841. case MBX_REG_LOGIN:
  1842. case MBX_UNREG_LOGIN:
  1843. case MBX_CLEAR_LA:
  1844. case MBX_DUMP_MEMORY:
  1845. case MBX_DUMP_CONTEXT:
  1846. case MBX_RUN_DIAGS:
  1847. case MBX_RESTART:
  1848. case MBX_UPDATE_CFG:
  1849. case MBX_DOWN_LOAD:
  1850. case MBX_DEL_LD_ENTRY:
  1851. case MBX_RUN_PROGRAM:
  1852. case MBX_SET_MASK:
  1853. case MBX_SET_VARIABLE:
  1854. case MBX_UNREG_D_ID:
  1855. case MBX_KILL_BOARD:
  1856. case MBX_CONFIG_FARP:
  1857. case MBX_BEACON:
  1858. case MBX_LOAD_AREA:
  1859. case MBX_RUN_BIU_DIAG64:
  1860. case MBX_CONFIG_PORT:
  1861. case MBX_READ_SPARM64:
  1862. case MBX_READ_RPI64:
  1863. case MBX_REG_LOGIN64:
  1864. case MBX_READ_TOPOLOGY:
  1865. case MBX_WRITE_WWN:
  1866. case MBX_SET_DEBUG:
  1867. case MBX_LOAD_EXP_ROM:
  1868. case MBX_ASYNCEVT_ENABLE:
  1869. case MBX_REG_VPI:
  1870. case MBX_UNREG_VPI:
  1871. case MBX_HEARTBEAT:
  1872. case MBX_PORT_CAPABILITIES:
  1873. case MBX_PORT_IOV_CONTROL:
  1874. case MBX_SLI4_CONFIG:
  1875. case MBX_SLI4_REQ_FTRS:
  1876. case MBX_REG_FCFI:
  1877. case MBX_UNREG_FCFI:
  1878. case MBX_REG_VFI:
  1879. case MBX_UNREG_VFI:
  1880. case MBX_INIT_VPI:
  1881. case MBX_INIT_VFI:
  1882. case MBX_RESUME_RPI:
  1883. case MBX_READ_EVENT_LOG_STATUS:
  1884. case MBX_READ_EVENT_LOG:
  1885. case MBX_SECURITY_MGMT:
  1886. case MBX_AUTH_PORT:
  1887. ret = mbxCommand;
  1888. break;
  1889. default:
  1890. ret = MBX_SHUTDOWN;
  1891. break;
  1892. }
  1893. return ret;
  1894. }
  1895. /**
  1896. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1897. * @phba: Pointer to HBA context object.
  1898. * @pmboxq: Pointer to mailbox command.
  1899. *
  1900. * This is completion handler function for mailbox commands issued from
  1901. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1902. * mailbox event handler function with no lock held. This function
  1903. * will wake up thread waiting on the wait queue pointed by context1
  1904. * of the mailbox.
  1905. **/
  1906. void
  1907. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1908. {
  1909. wait_queue_head_t *pdone_q;
  1910. unsigned long drvr_flag;
  1911. /*
  1912. * If pdone_q is empty, the driver thread gave up waiting and
  1913. * continued running.
  1914. */
  1915. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1916. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1917. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1918. if (pdone_q)
  1919. wake_up_interruptible(pdone_q);
  1920. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1921. return;
  1922. }
  1923. /**
  1924. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1925. * @phba: Pointer to HBA context object.
  1926. * @pmb: Pointer to mailbox object.
  1927. *
  1928. * This function is the default mailbox completion handler. It
  1929. * frees the memory resources associated with the completed mailbox
  1930. * command. If the completed command is a REG_LOGIN mailbox command,
  1931. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1932. **/
  1933. void
  1934. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1935. {
  1936. struct lpfc_vport *vport = pmb->vport;
  1937. struct lpfc_dmabuf *mp;
  1938. struct lpfc_nodelist *ndlp;
  1939. struct Scsi_Host *shost;
  1940. uint16_t rpi, vpi;
  1941. int rc;
  1942. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1943. if (mp) {
  1944. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1945. kfree(mp);
  1946. }
  1947. /*
  1948. * If a REG_LOGIN succeeded after node is destroyed or node
  1949. * is in re-discovery driver need to cleanup the RPI.
  1950. */
  1951. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1952. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1953. !pmb->u.mb.mbxStatus) {
  1954. rpi = pmb->u.mb.un.varWords[0];
  1955. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1956. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1957. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1958. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1959. if (rc != MBX_NOT_FINISHED)
  1960. return;
  1961. }
  1962. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  1963. !(phba->pport->load_flag & FC_UNLOADING) &&
  1964. !pmb->u.mb.mbxStatus) {
  1965. shost = lpfc_shost_from_vport(vport);
  1966. spin_lock_irq(shost->host_lock);
  1967. vport->vpi_state |= LPFC_VPI_REGISTERED;
  1968. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  1969. spin_unlock_irq(shost->host_lock);
  1970. }
  1971. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  1972. ndlp = (struct lpfc_nodelist *)pmb->context2;
  1973. lpfc_nlp_put(ndlp);
  1974. pmb->context2 = NULL;
  1975. }
  1976. /* Check security permission status on INIT_LINK mailbox command */
  1977. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  1978. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  1979. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1980. "2860 SLI authentication is required "
  1981. "for INIT_LINK but has not done yet\n");
  1982. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  1983. lpfc_sli4_mbox_cmd_free(phba, pmb);
  1984. else
  1985. mempool_free(pmb, phba->mbox_mem_pool);
  1986. }
  1987. /**
  1988. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  1989. * @phba: Pointer to HBA context object.
  1990. *
  1991. * This function is called with no lock held. This function processes all
  1992. * the completed mailbox commands and gives it to upper layers. The interrupt
  1993. * service routine processes mailbox completion interrupt and adds completed
  1994. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  1995. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  1996. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  1997. * function returns the mailbox commands to the upper layer by calling the
  1998. * completion handler function of each mailbox.
  1999. **/
  2000. int
  2001. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  2002. {
  2003. MAILBOX_t *pmbox;
  2004. LPFC_MBOXQ_t *pmb;
  2005. int rc;
  2006. LIST_HEAD(cmplq);
  2007. phba->sli.slistat.mbox_event++;
  2008. /* Get all completed mailboxe buffers into the cmplq */
  2009. spin_lock_irq(&phba->hbalock);
  2010. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2011. spin_unlock_irq(&phba->hbalock);
  2012. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2013. do {
  2014. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2015. if (pmb == NULL)
  2016. break;
  2017. pmbox = &pmb->u.mb;
  2018. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2019. if (pmb->vport) {
  2020. lpfc_debugfs_disc_trc(pmb->vport,
  2021. LPFC_DISC_TRC_MBOX_VPORT,
  2022. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2023. (uint32_t)pmbox->mbxCommand,
  2024. pmbox->un.varWords[0],
  2025. pmbox->un.varWords[1]);
  2026. }
  2027. else {
  2028. lpfc_debugfs_disc_trc(phba->pport,
  2029. LPFC_DISC_TRC_MBOX,
  2030. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2031. (uint32_t)pmbox->mbxCommand,
  2032. pmbox->un.varWords[0],
  2033. pmbox->un.varWords[1]);
  2034. }
  2035. }
  2036. /*
  2037. * It is a fatal error if unknown mbox command completion.
  2038. */
  2039. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2040. MBX_SHUTDOWN) {
  2041. /* Unknown mailbox command compl */
  2042. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2043. "(%d):0323 Unknown Mailbox command "
  2044. "x%x (x%x/x%x) Cmpl\n",
  2045. pmb->vport ? pmb->vport->vpi : 0,
  2046. pmbox->mbxCommand,
  2047. lpfc_sli_config_mbox_subsys_get(phba,
  2048. pmb),
  2049. lpfc_sli_config_mbox_opcode_get(phba,
  2050. pmb));
  2051. phba->link_state = LPFC_HBA_ERROR;
  2052. phba->work_hs = HS_FFER3;
  2053. lpfc_handle_eratt(phba);
  2054. continue;
  2055. }
  2056. if (pmbox->mbxStatus) {
  2057. phba->sli.slistat.mbox_stat_err++;
  2058. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2059. /* Mbox cmd cmpl error - RETRYing */
  2060. lpfc_printf_log(phba, KERN_INFO,
  2061. LOG_MBOX | LOG_SLI,
  2062. "(%d):0305 Mbox cmd cmpl "
  2063. "error - RETRYing Data: x%x "
  2064. "(x%x/x%x) x%x x%x x%x\n",
  2065. pmb->vport ? pmb->vport->vpi : 0,
  2066. pmbox->mbxCommand,
  2067. lpfc_sli_config_mbox_subsys_get(phba,
  2068. pmb),
  2069. lpfc_sli_config_mbox_opcode_get(phba,
  2070. pmb),
  2071. pmbox->mbxStatus,
  2072. pmbox->un.varWords[0],
  2073. pmb->vport->port_state);
  2074. pmbox->mbxStatus = 0;
  2075. pmbox->mbxOwner = OWN_HOST;
  2076. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2077. if (rc != MBX_NOT_FINISHED)
  2078. continue;
  2079. }
  2080. }
  2081. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2082. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2083. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2084. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2085. pmb->vport ? pmb->vport->vpi : 0,
  2086. pmbox->mbxCommand,
  2087. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2088. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2089. pmb->mbox_cmpl,
  2090. *((uint32_t *) pmbox),
  2091. pmbox->un.varWords[0],
  2092. pmbox->un.varWords[1],
  2093. pmbox->un.varWords[2],
  2094. pmbox->un.varWords[3],
  2095. pmbox->un.varWords[4],
  2096. pmbox->un.varWords[5],
  2097. pmbox->un.varWords[6],
  2098. pmbox->un.varWords[7]);
  2099. if (pmb->mbox_cmpl)
  2100. pmb->mbox_cmpl(phba,pmb);
  2101. } while (1);
  2102. return 0;
  2103. }
  2104. /**
  2105. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2106. * @phba: Pointer to HBA context object.
  2107. * @pring: Pointer to driver SLI ring object.
  2108. * @tag: buffer tag.
  2109. *
  2110. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2111. * is set in the tag the buffer is posted for a particular exchange,
  2112. * the function will return the buffer without replacing the buffer.
  2113. * If the buffer is for unsolicited ELS or CT traffic, this function
  2114. * returns the buffer and also posts another buffer to the firmware.
  2115. **/
  2116. static struct lpfc_dmabuf *
  2117. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2118. struct lpfc_sli_ring *pring,
  2119. uint32_t tag)
  2120. {
  2121. struct hbq_dmabuf *hbq_entry;
  2122. if (tag & QUE_BUFTAG_BIT)
  2123. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2124. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2125. if (!hbq_entry)
  2126. return NULL;
  2127. return &hbq_entry->dbuf;
  2128. }
  2129. /**
  2130. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2131. * @phba: Pointer to HBA context object.
  2132. * @pring: Pointer to driver SLI ring object.
  2133. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2134. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2135. * @fch_type: the type for the first frame of the sequence.
  2136. *
  2137. * This function is called with no lock held. This function uses the r_ctl and
  2138. * type of the received sequence to find the correct callback function to call
  2139. * to process the sequence.
  2140. **/
  2141. static int
  2142. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2143. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2144. uint32_t fch_type)
  2145. {
  2146. int i;
  2147. /* unSolicited Responses */
  2148. if (pring->prt[0].profile) {
  2149. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2150. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2151. saveq);
  2152. return 1;
  2153. }
  2154. /* We must search, based on rctl / type
  2155. for the right routine */
  2156. for (i = 0; i < pring->num_mask; i++) {
  2157. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2158. (pring->prt[i].type == fch_type)) {
  2159. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2160. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2161. (phba, pring, saveq);
  2162. return 1;
  2163. }
  2164. }
  2165. return 0;
  2166. }
  2167. /**
  2168. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2169. * @phba: Pointer to HBA context object.
  2170. * @pring: Pointer to driver SLI ring object.
  2171. * @saveq: Pointer to the unsolicited iocb.
  2172. *
  2173. * This function is called with no lock held by the ring event handler
  2174. * when there is an unsolicited iocb posted to the response ring by the
  2175. * firmware. This function gets the buffer associated with the iocbs
  2176. * and calls the event handler for the ring. This function handles both
  2177. * qring buffers and hbq buffers.
  2178. * When the function returns 1 the caller can free the iocb object otherwise
  2179. * upper layer functions will free the iocb objects.
  2180. **/
  2181. static int
  2182. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2183. struct lpfc_iocbq *saveq)
  2184. {
  2185. IOCB_t * irsp;
  2186. WORD5 * w5p;
  2187. uint32_t Rctl, Type;
  2188. uint32_t match;
  2189. struct lpfc_iocbq *iocbq;
  2190. struct lpfc_dmabuf *dmzbuf;
  2191. match = 0;
  2192. irsp = &(saveq->iocb);
  2193. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2194. if (pring->lpfc_sli_rcv_async_status)
  2195. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2196. else
  2197. lpfc_printf_log(phba,
  2198. KERN_WARNING,
  2199. LOG_SLI,
  2200. "0316 Ring %d handler: unexpected "
  2201. "ASYNC_STATUS iocb received evt_code "
  2202. "0x%x\n",
  2203. pring->ringno,
  2204. irsp->un.asyncstat.evt_code);
  2205. return 1;
  2206. }
  2207. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2208. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2209. if (irsp->ulpBdeCount > 0) {
  2210. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2211. irsp->un.ulpWord[3]);
  2212. lpfc_in_buf_free(phba, dmzbuf);
  2213. }
  2214. if (irsp->ulpBdeCount > 1) {
  2215. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2216. irsp->unsli3.sli3Words[3]);
  2217. lpfc_in_buf_free(phba, dmzbuf);
  2218. }
  2219. if (irsp->ulpBdeCount > 2) {
  2220. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2221. irsp->unsli3.sli3Words[7]);
  2222. lpfc_in_buf_free(phba, dmzbuf);
  2223. }
  2224. return 1;
  2225. }
  2226. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2227. if (irsp->ulpBdeCount != 0) {
  2228. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2229. irsp->un.ulpWord[3]);
  2230. if (!saveq->context2)
  2231. lpfc_printf_log(phba,
  2232. KERN_ERR,
  2233. LOG_SLI,
  2234. "0341 Ring %d Cannot find buffer for "
  2235. "an unsolicited iocb. tag 0x%x\n",
  2236. pring->ringno,
  2237. irsp->un.ulpWord[3]);
  2238. }
  2239. if (irsp->ulpBdeCount == 2) {
  2240. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2241. irsp->unsli3.sli3Words[7]);
  2242. if (!saveq->context3)
  2243. lpfc_printf_log(phba,
  2244. KERN_ERR,
  2245. LOG_SLI,
  2246. "0342 Ring %d Cannot find buffer for an"
  2247. " unsolicited iocb. tag 0x%x\n",
  2248. pring->ringno,
  2249. irsp->unsli3.sli3Words[7]);
  2250. }
  2251. list_for_each_entry(iocbq, &saveq->list, list) {
  2252. irsp = &(iocbq->iocb);
  2253. if (irsp->ulpBdeCount != 0) {
  2254. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2255. irsp->un.ulpWord[3]);
  2256. if (!iocbq->context2)
  2257. lpfc_printf_log(phba,
  2258. KERN_ERR,
  2259. LOG_SLI,
  2260. "0343 Ring %d Cannot find "
  2261. "buffer for an unsolicited iocb"
  2262. ". tag 0x%x\n", pring->ringno,
  2263. irsp->un.ulpWord[3]);
  2264. }
  2265. if (irsp->ulpBdeCount == 2) {
  2266. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2267. irsp->unsli3.sli3Words[7]);
  2268. if (!iocbq->context3)
  2269. lpfc_printf_log(phba,
  2270. KERN_ERR,
  2271. LOG_SLI,
  2272. "0344 Ring %d Cannot find "
  2273. "buffer for an unsolicited "
  2274. "iocb. tag 0x%x\n",
  2275. pring->ringno,
  2276. irsp->unsli3.sli3Words[7]);
  2277. }
  2278. }
  2279. }
  2280. if (irsp->ulpBdeCount != 0 &&
  2281. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2282. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2283. int found = 0;
  2284. /* search continue save q for same XRI */
  2285. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2286. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2287. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2288. list_add_tail(&saveq->list, &iocbq->list);
  2289. found = 1;
  2290. break;
  2291. }
  2292. }
  2293. if (!found)
  2294. list_add_tail(&saveq->clist,
  2295. &pring->iocb_continue_saveq);
  2296. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2297. list_del_init(&iocbq->clist);
  2298. saveq = iocbq;
  2299. irsp = &(saveq->iocb);
  2300. } else
  2301. return 0;
  2302. }
  2303. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2304. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2305. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2306. Rctl = FC_RCTL_ELS_REQ;
  2307. Type = FC_TYPE_ELS;
  2308. } else {
  2309. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2310. Rctl = w5p->hcsw.Rctl;
  2311. Type = w5p->hcsw.Type;
  2312. /* Firmware Workaround */
  2313. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2314. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2315. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2316. Rctl = FC_RCTL_ELS_REQ;
  2317. Type = FC_TYPE_ELS;
  2318. w5p->hcsw.Rctl = Rctl;
  2319. w5p->hcsw.Type = Type;
  2320. }
  2321. }
  2322. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2323. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2324. "0313 Ring %d handler: unexpected Rctl x%x "
  2325. "Type x%x received\n",
  2326. pring->ringno, Rctl, Type);
  2327. return 1;
  2328. }
  2329. /**
  2330. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2331. * @phba: Pointer to HBA context object.
  2332. * @pring: Pointer to driver SLI ring object.
  2333. * @prspiocb: Pointer to response iocb object.
  2334. *
  2335. * This function looks up the iocb_lookup table to get the command iocb
  2336. * corresponding to the given response iocb using the iotag of the
  2337. * response iocb. This function is called with the hbalock held.
  2338. * This function returns the command iocb object if it finds the command
  2339. * iocb else returns NULL.
  2340. **/
  2341. static struct lpfc_iocbq *
  2342. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2343. struct lpfc_sli_ring *pring,
  2344. struct lpfc_iocbq *prspiocb)
  2345. {
  2346. struct lpfc_iocbq *cmd_iocb = NULL;
  2347. uint16_t iotag;
  2348. iotag = prspiocb->iocb.ulpIoTag;
  2349. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2350. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2351. list_del_init(&cmd_iocb->list);
  2352. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2353. pring->txcmplq_cnt--;
  2354. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2355. }
  2356. return cmd_iocb;
  2357. }
  2358. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2359. "0317 iotag x%x is out off "
  2360. "range: max iotag x%x wd0 x%x\n",
  2361. iotag, phba->sli.last_iotag,
  2362. *(((uint32_t *) &prspiocb->iocb) + 7));
  2363. return NULL;
  2364. }
  2365. /**
  2366. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2367. * @phba: Pointer to HBA context object.
  2368. * @pring: Pointer to driver SLI ring object.
  2369. * @iotag: IOCB tag.
  2370. *
  2371. * This function looks up the iocb_lookup table to get the command iocb
  2372. * corresponding to the given iotag. This function is called with the
  2373. * hbalock held.
  2374. * This function returns the command iocb object if it finds the command
  2375. * iocb else returns NULL.
  2376. **/
  2377. static struct lpfc_iocbq *
  2378. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2379. struct lpfc_sli_ring *pring, uint16_t iotag)
  2380. {
  2381. struct lpfc_iocbq *cmd_iocb;
  2382. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2383. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2384. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2385. /* remove from txcmpl queue list */
  2386. list_del_init(&cmd_iocb->list);
  2387. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2388. pring->txcmplq_cnt--;
  2389. return cmd_iocb;
  2390. }
  2391. }
  2392. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2393. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2394. iotag, phba->sli.last_iotag);
  2395. return NULL;
  2396. }
  2397. /**
  2398. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2399. * @phba: Pointer to HBA context object.
  2400. * @pring: Pointer to driver SLI ring object.
  2401. * @saveq: Pointer to the response iocb to be processed.
  2402. *
  2403. * This function is called by the ring event handler for non-fcp
  2404. * rings when there is a new response iocb in the response ring.
  2405. * The caller is not required to hold any locks. This function
  2406. * gets the command iocb associated with the response iocb and
  2407. * calls the completion handler for the command iocb. If there
  2408. * is no completion handler, the function will free the resources
  2409. * associated with command iocb. If the response iocb is for
  2410. * an already aborted command iocb, the status of the completion
  2411. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2412. * This function always returns 1.
  2413. **/
  2414. static int
  2415. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2416. struct lpfc_iocbq *saveq)
  2417. {
  2418. struct lpfc_iocbq *cmdiocbp;
  2419. int rc = 1;
  2420. unsigned long iflag;
  2421. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2422. spin_lock_irqsave(&phba->hbalock, iflag);
  2423. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2424. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2425. if (cmdiocbp) {
  2426. if (cmdiocbp->iocb_cmpl) {
  2427. /*
  2428. * If an ELS command failed send an event to mgmt
  2429. * application.
  2430. */
  2431. if (saveq->iocb.ulpStatus &&
  2432. (pring->ringno == LPFC_ELS_RING) &&
  2433. (cmdiocbp->iocb.ulpCommand ==
  2434. CMD_ELS_REQUEST64_CR))
  2435. lpfc_send_els_failure_event(phba,
  2436. cmdiocbp, saveq);
  2437. /*
  2438. * Post all ELS completions to the worker thread.
  2439. * All other are passed to the completion callback.
  2440. */
  2441. if (pring->ringno == LPFC_ELS_RING) {
  2442. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2443. (cmdiocbp->iocb_flag &
  2444. LPFC_DRIVER_ABORTED)) {
  2445. spin_lock_irqsave(&phba->hbalock,
  2446. iflag);
  2447. cmdiocbp->iocb_flag &=
  2448. ~LPFC_DRIVER_ABORTED;
  2449. spin_unlock_irqrestore(&phba->hbalock,
  2450. iflag);
  2451. saveq->iocb.ulpStatus =
  2452. IOSTAT_LOCAL_REJECT;
  2453. saveq->iocb.un.ulpWord[4] =
  2454. IOERR_SLI_ABORTED;
  2455. /* Firmware could still be in progress
  2456. * of DMAing payload, so don't free data
  2457. * buffer till after a hbeat.
  2458. */
  2459. spin_lock_irqsave(&phba->hbalock,
  2460. iflag);
  2461. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2462. spin_unlock_irqrestore(&phba->hbalock,
  2463. iflag);
  2464. }
  2465. if (phba->sli_rev == LPFC_SLI_REV4) {
  2466. if (saveq->iocb_flag &
  2467. LPFC_EXCHANGE_BUSY) {
  2468. /* Set cmdiocb flag for the
  2469. * exchange busy so sgl (xri)
  2470. * will not be released until
  2471. * the abort xri is received
  2472. * from hba.
  2473. */
  2474. spin_lock_irqsave(
  2475. &phba->hbalock, iflag);
  2476. cmdiocbp->iocb_flag |=
  2477. LPFC_EXCHANGE_BUSY;
  2478. spin_unlock_irqrestore(
  2479. &phba->hbalock, iflag);
  2480. }
  2481. if (cmdiocbp->iocb_flag &
  2482. LPFC_DRIVER_ABORTED) {
  2483. /*
  2484. * Clear LPFC_DRIVER_ABORTED
  2485. * bit in case it was driver
  2486. * initiated abort.
  2487. */
  2488. spin_lock_irqsave(
  2489. &phba->hbalock, iflag);
  2490. cmdiocbp->iocb_flag &=
  2491. ~LPFC_DRIVER_ABORTED;
  2492. spin_unlock_irqrestore(
  2493. &phba->hbalock, iflag);
  2494. cmdiocbp->iocb.ulpStatus =
  2495. IOSTAT_LOCAL_REJECT;
  2496. cmdiocbp->iocb.un.ulpWord[4] =
  2497. IOERR_ABORT_REQUESTED;
  2498. /*
  2499. * For SLI4, irsiocb contains
  2500. * NO_XRI in sli_xritag, it
  2501. * shall not affect releasing
  2502. * sgl (xri) process.
  2503. */
  2504. saveq->iocb.ulpStatus =
  2505. IOSTAT_LOCAL_REJECT;
  2506. saveq->iocb.un.ulpWord[4] =
  2507. IOERR_SLI_ABORTED;
  2508. spin_lock_irqsave(
  2509. &phba->hbalock, iflag);
  2510. saveq->iocb_flag |=
  2511. LPFC_DELAY_MEM_FREE;
  2512. spin_unlock_irqrestore(
  2513. &phba->hbalock, iflag);
  2514. }
  2515. }
  2516. }
  2517. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2518. } else
  2519. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2520. } else {
  2521. /*
  2522. * Unknown initiating command based on the response iotag.
  2523. * This could be the case on the ELS ring because of
  2524. * lpfc_els_abort().
  2525. */
  2526. if (pring->ringno != LPFC_ELS_RING) {
  2527. /*
  2528. * Ring <ringno> handler: unexpected completion IoTag
  2529. * <IoTag>
  2530. */
  2531. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2532. "0322 Ring %d handler: "
  2533. "unexpected completion IoTag x%x "
  2534. "Data: x%x x%x x%x x%x\n",
  2535. pring->ringno,
  2536. saveq->iocb.ulpIoTag,
  2537. saveq->iocb.ulpStatus,
  2538. saveq->iocb.un.ulpWord[4],
  2539. saveq->iocb.ulpCommand,
  2540. saveq->iocb.ulpContext);
  2541. }
  2542. }
  2543. return rc;
  2544. }
  2545. /**
  2546. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2547. * @phba: Pointer to HBA context object.
  2548. * @pring: Pointer to driver SLI ring object.
  2549. *
  2550. * This function is called from the iocb ring event handlers when
  2551. * put pointer is ahead of the get pointer for a ring. This function signal
  2552. * an error attention condition to the worker thread and the worker
  2553. * thread will transition the HBA to offline state.
  2554. **/
  2555. static void
  2556. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2557. {
  2558. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2559. /*
  2560. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2561. * rsp ring <portRspMax>
  2562. */
  2563. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2564. "0312 Ring %d handler: portRspPut %d "
  2565. "is bigger than rsp ring %d\n",
  2566. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2567. pring->numRiocb);
  2568. phba->link_state = LPFC_HBA_ERROR;
  2569. /*
  2570. * All error attention handlers are posted to
  2571. * worker thread
  2572. */
  2573. phba->work_ha |= HA_ERATT;
  2574. phba->work_hs = HS_FFER3;
  2575. lpfc_worker_wake_up(phba);
  2576. return;
  2577. }
  2578. /**
  2579. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2580. * @ptr: Pointer to address of HBA context object.
  2581. *
  2582. * This function is invoked by the Error Attention polling timer when the
  2583. * timer times out. It will check the SLI Error Attention register for
  2584. * possible attention events. If so, it will post an Error Attention event
  2585. * and wake up worker thread to process it. Otherwise, it will set up the
  2586. * Error Attention polling timer for the next poll.
  2587. **/
  2588. void lpfc_poll_eratt(unsigned long ptr)
  2589. {
  2590. struct lpfc_hba *phba;
  2591. uint32_t eratt = 0;
  2592. phba = (struct lpfc_hba *)ptr;
  2593. /* Check chip HA register for error event */
  2594. eratt = lpfc_sli_check_eratt(phba);
  2595. if (eratt)
  2596. /* Tell the worker thread there is work to do */
  2597. lpfc_worker_wake_up(phba);
  2598. else
  2599. /* Restart the timer for next eratt poll */
  2600. mod_timer(&phba->eratt_poll, jiffies +
  2601. HZ * LPFC_ERATT_POLL_INTERVAL);
  2602. return;
  2603. }
  2604. /**
  2605. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2606. * @phba: Pointer to HBA context object.
  2607. * @pring: Pointer to driver SLI ring object.
  2608. * @mask: Host attention register mask for this ring.
  2609. *
  2610. * This function is called from the interrupt context when there is a ring
  2611. * event for the fcp ring. The caller does not hold any lock.
  2612. * The function processes each response iocb in the response ring until it
  2613. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2614. * LE bit set. The function will call the completion handler of the command iocb
  2615. * if the response iocb indicates a completion for a command iocb or it is
  2616. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2617. * function if this is an unsolicited iocb.
  2618. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2619. * to check it explicitly.
  2620. */
  2621. int
  2622. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2623. struct lpfc_sli_ring *pring, uint32_t mask)
  2624. {
  2625. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2626. IOCB_t *irsp = NULL;
  2627. IOCB_t *entry = NULL;
  2628. struct lpfc_iocbq *cmdiocbq = NULL;
  2629. struct lpfc_iocbq rspiocbq;
  2630. uint32_t status;
  2631. uint32_t portRspPut, portRspMax;
  2632. int rc = 1;
  2633. lpfc_iocb_type type;
  2634. unsigned long iflag;
  2635. uint32_t rsp_cmpl = 0;
  2636. spin_lock_irqsave(&phba->hbalock, iflag);
  2637. pring->stats.iocb_event++;
  2638. /*
  2639. * The next available response entry should never exceed the maximum
  2640. * entries. If it does, treat it as an adapter hardware error.
  2641. */
  2642. portRspMax = pring->numRiocb;
  2643. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2644. if (unlikely(portRspPut >= portRspMax)) {
  2645. lpfc_sli_rsp_pointers_error(phba, pring);
  2646. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2647. return 1;
  2648. }
  2649. if (phba->fcp_ring_in_use) {
  2650. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2651. return 1;
  2652. } else
  2653. phba->fcp_ring_in_use = 1;
  2654. rmb();
  2655. while (pring->rspidx != portRspPut) {
  2656. /*
  2657. * Fetch an entry off the ring and copy it into a local data
  2658. * structure. The copy involves a byte-swap since the
  2659. * network byte order and pci byte orders are different.
  2660. */
  2661. entry = lpfc_resp_iocb(phba, pring);
  2662. phba->last_completion_time = jiffies;
  2663. if (++pring->rspidx >= portRspMax)
  2664. pring->rspidx = 0;
  2665. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2666. (uint32_t *) &rspiocbq.iocb,
  2667. phba->iocb_rsp_size);
  2668. INIT_LIST_HEAD(&(rspiocbq.list));
  2669. irsp = &rspiocbq.iocb;
  2670. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2671. pring->stats.iocb_rsp++;
  2672. rsp_cmpl++;
  2673. if (unlikely(irsp->ulpStatus)) {
  2674. /*
  2675. * If resource errors reported from HBA, reduce
  2676. * queuedepths of the SCSI device.
  2677. */
  2678. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2679. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2680. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2681. phba->lpfc_rampdown_queue_depth(phba);
  2682. spin_lock_irqsave(&phba->hbalock, iflag);
  2683. }
  2684. /* Rsp ring <ringno> error: IOCB */
  2685. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2686. "0336 Rsp Ring %d error: IOCB Data: "
  2687. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2688. pring->ringno,
  2689. irsp->un.ulpWord[0],
  2690. irsp->un.ulpWord[1],
  2691. irsp->un.ulpWord[2],
  2692. irsp->un.ulpWord[3],
  2693. irsp->un.ulpWord[4],
  2694. irsp->un.ulpWord[5],
  2695. *(uint32_t *)&irsp->un1,
  2696. *((uint32_t *)&irsp->un1 + 1));
  2697. }
  2698. switch (type) {
  2699. case LPFC_ABORT_IOCB:
  2700. case LPFC_SOL_IOCB:
  2701. /*
  2702. * Idle exchange closed via ABTS from port. No iocb
  2703. * resources need to be recovered.
  2704. */
  2705. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2706. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2707. "0333 IOCB cmd 0x%x"
  2708. " processed. Skipping"
  2709. " completion\n",
  2710. irsp->ulpCommand);
  2711. break;
  2712. }
  2713. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2714. &rspiocbq);
  2715. if (unlikely(!cmdiocbq))
  2716. break;
  2717. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2718. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2719. if (cmdiocbq->iocb_cmpl) {
  2720. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2721. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2722. &rspiocbq);
  2723. spin_lock_irqsave(&phba->hbalock, iflag);
  2724. }
  2725. break;
  2726. case LPFC_UNSOL_IOCB:
  2727. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2728. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2729. spin_lock_irqsave(&phba->hbalock, iflag);
  2730. break;
  2731. default:
  2732. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2733. char adaptermsg[LPFC_MAX_ADPTMSG];
  2734. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2735. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2736. MAX_MSG_DATA);
  2737. dev_warn(&((phba->pcidev)->dev),
  2738. "lpfc%d: %s\n",
  2739. phba->brd_no, adaptermsg);
  2740. } else {
  2741. /* Unknown IOCB command */
  2742. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2743. "0334 Unknown IOCB command "
  2744. "Data: x%x, x%x x%x x%x x%x\n",
  2745. type, irsp->ulpCommand,
  2746. irsp->ulpStatus,
  2747. irsp->ulpIoTag,
  2748. irsp->ulpContext);
  2749. }
  2750. break;
  2751. }
  2752. /*
  2753. * The response IOCB has been processed. Update the ring
  2754. * pointer in SLIM. If the port response put pointer has not
  2755. * been updated, sync the pgp->rspPutInx and fetch the new port
  2756. * response put pointer.
  2757. */
  2758. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2759. if (pring->rspidx == portRspPut)
  2760. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2761. }
  2762. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2763. pring->stats.iocb_rsp_full++;
  2764. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2765. writel(status, phba->CAregaddr);
  2766. readl(phba->CAregaddr);
  2767. }
  2768. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2769. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2770. pring->stats.iocb_cmd_empty++;
  2771. /* Force update of the local copy of cmdGetInx */
  2772. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2773. lpfc_sli_resume_iocb(phba, pring);
  2774. if ((pring->lpfc_sli_cmd_available))
  2775. (pring->lpfc_sli_cmd_available) (phba, pring);
  2776. }
  2777. phba->fcp_ring_in_use = 0;
  2778. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2779. return rc;
  2780. }
  2781. /**
  2782. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2783. * @phba: Pointer to HBA context object.
  2784. * @pring: Pointer to driver SLI ring object.
  2785. * @rspiocbp: Pointer to driver response IOCB object.
  2786. *
  2787. * This function is called from the worker thread when there is a slow-path
  2788. * response IOCB to process. This function chains all the response iocbs until
  2789. * seeing the iocb with the LE bit set. The function will call
  2790. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2791. * completion of a command iocb. The function will call the
  2792. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2793. * The function frees the resources or calls the completion handler if this
  2794. * iocb is an abort completion. The function returns NULL when the response
  2795. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2796. * this function shall chain the iocb on to the iocb_continueq and return the
  2797. * response iocb passed in.
  2798. **/
  2799. static struct lpfc_iocbq *
  2800. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2801. struct lpfc_iocbq *rspiocbp)
  2802. {
  2803. struct lpfc_iocbq *saveq;
  2804. struct lpfc_iocbq *cmdiocbp;
  2805. struct lpfc_iocbq *next_iocb;
  2806. IOCB_t *irsp = NULL;
  2807. uint32_t free_saveq;
  2808. uint8_t iocb_cmd_type;
  2809. lpfc_iocb_type type;
  2810. unsigned long iflag;
  2811. int rc;
  2812. spin_lock_irqsave(&phba->hbalock, iflag);
  2813. /* First add the response iocb to the countinueq list */
  2814. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2815. pring->iocb_continueq_cnt++;
  2816. /* Now, determine whether the list is completed for processing */
  2817. irsp = &rspiocbp->iocb;
  2818. if (irsp->ulpLe) {
  2819. /*
  2820. * By default, the driver expects to free all resources
  2821. * associated with this iocb completion.
  2822. */
  2823. free_saveq = 1;
  2824. saveq = list_get_first(&pring->iocb_continueq,
  2825. struct lpfc_iocbq, list);
  2826. irsp = &(saveq->iocb);
  2827. list_del_init(&pring->iocb_continueq);
  2828. pring->iocb_continueq_cnt = 0;
  2829. pring->stats.iocb_rsp++;
  2830. /*
  2831. * If resource errors reported from HBA, reduce
  2832. * queuedepths of the SCSI device.
  2833. */
  2834. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2835. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2836. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2837. phba->lpfc_rampdown_queue_depth(phba);
  2838. spin_lock_irqsave(&phba->hbalock, iflag);
  2839. }
  2840. if (irsp->ulpStatus) {
  2841. /* Rsp ring <ringno> error: IOCB */
  2842. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2843. "0328 Rsp Ring %d error: "
  2844. "IOCB Data: "
  2845. "x%x x%x x%x x%x "
  2846. "x%x x%x x%x x%x "
  2847. "x%x x%x x%x x%x "
  2848. "x%x x%x x%x x%x\n",
  2849. pring->ringno,
  2850. irsp->un.ulpWord[0],
  2851. irsp->un.ulpWord[1],
  2852. irsp->un.ulpWord[2],
  2853. irsp->un.ulpWord[3],
  2854. irsp->un.ulpWord[4],
  2855. irsp->un.ulpWord[5],
  2856. *(((uint32_t *) irsp) + 6),
  2857. *(((uint32_t *) irsp) + 7),
  2858. *(((uint32_t *) irsp) + 8),
  2859. *(((uint32_t *) irsp) + 9),
  2860. *(((uint32_t *) irsp) + 10),
  2861. *(((uint32_t *) irsp) + 11),
  2862. *(((uint32_t *) irsp) + 12),
  2863. *(((uint32_t *) irsp) + 13),
  2864. *(((uint32_t *) irsp) + 14),
  2865. *(((uint32_t *) irsp) + 15));
  2866. }
  2867. /*
  2868. * Fetch the IOCB command type and call the correct completion
  2869. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2870. * get freed back to the lpfc_iocb_list by the discovery
  2871. * kernel thread.
  2872. */
  2873. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2874. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2875. switch (type) {
  2876. case LPFC_SOL_IOCB:
  2877. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2878. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2879. spin_lock_irqsave(&phba->hbalock, iflag);
  2880. break;
  2881. case LPFC_UNSOL_IOCB:
  2882. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2883. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2884. spin_lock_irqsave(&phba->hbalock, iflag);
  2885. if (!rc)
  2886. free_saveq = 0;
  2887. break;
  2888. case LPFC_ABORT_IOCB:
  2889. cmdiocbp = NULL;
  2890. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2891. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2892. saveq);
  2893. if (cmdiocbp) {
  2894. /* Call the specified completion routine */
  2895. if (cmdiocbp->iocb_cmpl) {
  2896. spin_unlock_irqrestore(&phba->hbalock,
  2897. iflag);
  2898. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2899. saveq);
  2900. spin_lock_irqsave(&phba->hbalock,
  2901. iflag);
  2902. } else
  2903. __lpfc_sli_release_iocbq(phba,
  2904. cmdiocbp);
  2905. }
  2906. break;
  2907. case LPFC_UNKNOWN_IOCB:
  2908. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2909. char adaptermsg[LPFC_MAX_ADPTMSG];
  2910. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2911. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2912. MAX_MSG_DATA);
  2913. dev_warn(&((phba->pcidev)->dev),
  2914. "lpfc%d: %s\n",
  2915. phba->brd_no, adaptermsg);
  2916. } else {
  2917. /* Unknown IOCB command */
  2918. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2919. "0335 Unknown IOCB "
  2920. "command Data: x%x "
  2921. "x%x x%x x%x\n",
  2922. irsp->ulpCommand,
  2923. irsp->ulpStatus,
  2924. irsp->ulpIoTag,
  2925. irsp->ulpContext);
  2926. }
  2927. break;
  2928. }
  2929. if (free_saveq) {
  2930. list_for_each_entry_safe(rspiocbp, next_iocb,
  2931. &saveq->list, list) {
  2932. list_del(&rspiocbp->list);
  2933. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2934. }
  2935. __lpfc_sli_release_iocbq(phba, saveq);
  2936. }
  2937. rspiocbp = NULL;
  2938. }
  2939. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2940. return rspiocbp;
  2941. }
  2942. /**
  2943. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2944. * @phba: Pointer to HBA context object.
  2945. * @pring: Pointer to driver SLI ring object.
  2946. * @mask: Host attention register mask for this ring.
  2947. *
  2948. * This routine wraps the actual slow_ring event process routine from the
  2949. * API jump table function pointer from the lpfc_hba struct.
  2950. **/
  2951. void
  2952. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2953. struct lpfc_sli_ring *pring, uint32_t mask)
  2954. {
  2955. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2956. }
  2957. /**
  2958. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  2959. * @phba: Pointer to HBA context object.
  2960. * @pring: Pointer to driver SLI ring object.
  2961. * @mask: Host attention register mask for this ring.
  2962. *
  2963. * This function is called from the worker thread when there is a ring event
  2964. * for non-fcp rings. The caller does not hold any lock. The function will
  2965. * remove each response iocb in the response ring and calls the handle
  2966. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2967. **/
  2968. static void
  2969. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2970. struct lpfc_sli_ring *pring, uint32_t mask)
  2971. {
  2972. struct lpfc_pgp *pgp;
  2973. IOCB_t *entry;
  2974. IOCB_t *irsp = NULL;
  2975. struct lpfc_iocbq *rspiocbp = NULL;
  2976. uint32_t portRspPut, portRspMax;
  2977. unsigned long iflag;
  2978. uint32_t status;
  2979. pgp = &phba->port_gp[pring->ringno];
  2980. spin_lock_irqsave(&phba->hbalock, iflag);
  2981. pring->stats.iocb_event++;
  2982. /*
  2983. * The next available response entry should never exceed the maximum
  2984. * entries. If it does, treat it as an adapter hardware error.
  2985. */
  2986. portRspMax = pring->numRiocb;
  2987. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2988. if (portRspPut >= portRspMax) {
  2989. /*
  2990. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2991. * rsp ring <portRspMax>
  2992. */
  2993. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2994. "0303 Ring %d handler: portRspPut %d "
  2995. "is bigger than rsp ring %d\n",
  2996. pring->ringno, portRspPut, portRspMax);
  2997. phba->link_state = LPFC_HBA_ERROR;
  2998. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2999. phba->work_hs = HS_FFER3;
  3000. lpfc_handle_eratt(phba);
  3001. return;
  3002. }
  3003. rmb();
  3004. while (pring->rspidx != portRspPut) {
  3005. /*
  3006. * Build a completion list and call the appropriate handler.
  3007. * The process is to get the next available response iocb, get
  3008. * a free iocb from the list, copy the response data into the
  3009. * free iocb, insert to the continuation list, and update the
  3010. * next response index to slim. This process makes response
  3011. * iocb's in the ring available to DMA as fast as possible but
  3012. * pays a penalty for a copy operation. Since the iocb is
  3013. * only 32 bytes, this penalty is considered small relative to
  3014. * the PCI reads for register values and a slim write. When
  3015. * the ulpLe field is set, the entire Command has been
  3016. * received.
  3017. */
  3018. entry = lpfc_resp_iocb(phba, pring);
  3019. phba->last_completion_time = jiffies;
  3020. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3021. if (rspiocbp == NULL) {
  3022. printk(KERN_ERR "%s: out of buffers! Failing "
  3023. "completion.\n", __func__);
  3024. break;
  3025. }
  3026. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3027. phba->iocb_rsp_size);
  3028. irsp = &rspiocbp->iocb;
  3029. if (++pring->rspidx >= portRspMax)
  3030. pring->rspidx = 0;
  3031. if (pring->ringno == LPFC_ELS_RING) {
  3032. lpfc_debugfs_slow_ring_trc(phba,
  3033. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3034. *(((uint32_t *) irsp) + 4),
  3035. *(((uint32_t *) irsp) + 6),
  3036. *(((uint32_t *) irsp) + 7));
  3037. }
  3038. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  3039. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3040. /* Handle the response IOCB */
  3041. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3042. spin_lock_irqsave(&phba->hbalock, iflag);
  3043. /*
  3044. * If the port response put pointer has not been updated, sync
  3045. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3046. * response put pointer.
  3047. */
  3048. if (pring->rspidx == portRspPut) {
  3049. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3050. }
  3051. } /* while (pring->rspidx != portRspPut) */
  3052. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3053. /* At least one response entry has been freed */
  3054. pring->stats.iocb_rsp_full++;
  3055. /* SET RxRE_RSP in Chip Att register */
  3056. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3057. writel(status, phba->CAregaddr);
  3058. readl(phba->CAregaddr); /* flush */
  3059. }
  3060. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3061. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3062. pring->stats.iocb_cmd_empty++;
  3063. /* Force update of the local copy of cmdGetInx */
  3064. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3065. lpfc_sli_resume_iocb(phba, pring);
  3066. if ((pring->lpfc_sli_cmd_available))
  3067. (pring->lpfc_sli_cmd_available) (phba, pring);
  3068. }
  3069. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3070. return;
  3071. }
  3072. /**
  3073. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3074. * @phba: Pointer to HBA context object.
  3075. * @pring: Pointer to driver SLI ring object.
  3076. * @mask: Host attention register mask for this ring.
  3077. *
  3078. * This function is called from the worker thread when there is a pending
  3079. * ELS response iocb on the driver internal slow-path response iocb worker
  3080. * queue. The caller does not hold any lock. The function will remove each
  3081. * response iocb from the response worker queue and calls the handle
  3082. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3083. **/
  3084. static void
  3085. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3086. struct lpfc_sli_ring *pring, uint32_t mask)
  3087. {
  3088. struct lpfc_iocbq *irspiocbq;
  3089. struct hbq_dmabuf *dmabuf;
  3090. struct lpfc_cq_event *cq_event;
  3091. unsigned long iflag;
  3092. spin_lock_irqsave(&phba->hbalock, iflag);
  3093. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3094. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3095. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3096. /* Get the response iocb from the head of work queue */
  3097. spin_lock_irqsave(&phba->hbalock, iflag);
  3098. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3099. cq_event, struct lpfc_cq_event, list);
  3100. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3101. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3102. case CQE_CODE_COMPL_WQE:
  3103. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3104. cq_event);
  3105. /* Translate ELS WCQE to response IOCBQ */
  3106. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3107. irspiocbq);
  3108. if (irspiocbq)
  3109. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3110. irspiocbq);
  3111. break;
  3112. case CQE_CODE_RECEIVE:
  3113. case CQE_CODE_RECEIVE_V1:
  3114. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3115. cq_event);
  3116. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3117. break;
  3118. default:
  3119. break;
  3120. }
  3121. }
  3122. }
  3123. /**
  3124. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3125. * @phba: Pointer to HBA context object.
  3126. * @pring: Pointer to driver SLI ring object.
  3127. *
  3128. * This function aborts all iocbs in the given ring and frees all the iocb
  3129. * objects in txq. This function issues an abort iocb for all the iocb commands
  3130. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3131. * the return of this function. The caller is not required to hold any locks.
  3132. **/
  3133. void
  3134. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3135. {
  3136. LIST_HEAD(completions);
  3137. struct lpfc_iocbq *iocb, *next_iocb;
  3138. if (pring->ringno == LPFC_ELS_RING) {
  3139. lpfc_fabric_abort_hba(phba);
  3140. }
  3141. /* Error everything on txq and txcmplq
  3142. * First do the txq.
  3143. */
  3144. spin_lock_irq(&phba->hbalock);
  3145. list_splice_init(&pring->txq, &completions);
  3146. pring->txq_cnt = 0;
  3147. /* Next issue ABTS for everything on the txcmplq */
  3148. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3149. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3150. spin_unlock_irq(&phba->hbalock);
  3151. /* Cancel all the IOCBs from the completions list */
  3152. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3153. IOERR_SLI_ABORTED);
  3154. }
  3155. /**
  3156. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3157. * @phba: Pointer to HBA context object.
  3158. *
  3159. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3160. * objects in txq and txcmplq. This function will not issue abort iocbs
  3161. * for all the iocb commands in txcmplq, they will just be returned with
  3162. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3163. * slot has been permanently disabled.
  3164. **/
  3165. void
  3166. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3167. {
  3168. LIST_HEAD(txq);
  3169. LIST_HEAD(txcmplq);
  3170. struct lpfc_sli *psli = &phba->sli;
  3171. struct lpfc_sli_ring *pring;
  3172. /* Currently, only one fcp ring */
  3173. pring = &psli->ring[psli->fcp_ring];
  3174. spin_lock_irq(&phba->hbalock);
  3175. /* Retrieve everything on txq */
  3176. list_splice_init(&pring->txq, &txq);
  3177. pring->txq_cnt = 0;
  3178. /* Retrieve everything on the txcmplq */
  3179. list_splice_init(&pring->txcmplq, &txcmplq);
  3180. pring->txcmplq_cnt = 0;
  3181. /* Indicate the I/O queues are flushed */
  3182. phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
  3183. spin_unlock_irq(&phba->hbalock);
  3184. /* Flush the txq */
  3185. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3186. IOERR_SLI_DOWN);
  3187. /* Flush the txcmpq */
  3188. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3189. IOERR_SLI_DOWN);
  3190. }
  3191. /**
  3192. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3193. * @phba: Pointer to HBA context object.
  3194. * @mask: Bit mask to be checked.
  3195. *
  3196. * This function reads the host status register and compares
  3197. * with the provided bit mask to check if HBA completed
  3198. * the restart. This function will wait in a loop for the
  3199. * HBA to complete restart. If the HBA does not restart within
  3200. * 15 iterations, the function will reset the HBA again. The
  3201. * function returns 1 when HBA fail to restart otherwise returns
  3202. * zero.
  3203. **/
  3204. static int
  3205. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3206. {
  3207. uint32_t status;
  3208. int i = 0;
  3209. int retval = 0;
  3210. /* Read the HBA Host Status Register */
  3211. if (lpfc_readl(phba->HSregaddr, &status))
  3212. return 1;
  3213. /*
  3214. * Check status register every 100ms for 5 retries, then every
  3215. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3216. * every 2.5 sec for 4.
  3217. * Break our of the loop if errors occurred during init.
  3218. */
  3219. while (((status & mask) != mask) &&
  3220. !(status & HS_FFERM) &&
  3221. i++ < 20) {
  3222. if (i <= 5)
  3223. msleep(10);
  3224. else if (i <= 10)
  3225. msleep(500);
  3226. else
  3227. msleep(2500);
  3228. if (i == 15) {
  3229. /* Do post */
  3230. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3231. lpfc_sli_brdrestart(phba);
  3232. }
  3233. /* Read the HBA Host Status Register */
  3234. if (lpfc_readl(phba->HSregaddr, &status)) {
  3235. retval = 1;
  3236. break;
  3237. }
  3238. }
  3239. /* Check to see if any errors occurred during init */
  3240. if ((status & HS_FFERM) || (i >= 20)) {
  3241. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3242. "2751 Adapter failed to restart, "
  3243. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3244. status,
  3245. readl(phba->MBslimaddr + 0xa8),
  3246. readl(phba->MBslimaddr + 0xac));
  3247. phba->link_state = LPFC_HBA_ERROR;
  3248. retval = 1;
  3249. }
  3250. return retval;
  3251. }
  3252. /**
  3253. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3254. * @phba: Pointer to HBA context object.
  3255. * @mask: Bit mask to be checked.
  3256. *
  3257. * This function checks the host status register to check if HBA is
  3258. * ready. This function will wait in a loop for the HBA to be ready
  3259. * If the HBA is not ready , the function will will reset the HBA PCI
  3260. * function again. The function returns 1 when HBA fail to be ready
  3261. * otherwise returns zero.
  3262. **/
  3263. static int
  3264. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3265. {
  3266. uint32_t status;
  3267. int retval = 0;
  3268. /* Read the HBA Host Status Register */
  3269. status = lpfc_sli4_post_status_check(phba);
  3270. if (status) {
  3271. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3272. lpfc_sli_brdrestart(phba);
  3273. status = lpfc_sli4_post_status_check(phba);
  3274. }
  3275. /* Check to see if any errors occurred during init */
  3276. if (status) {
  3277. phba->link_state = LPFC_HBA_ERROR;
  3278. retval = 1;
  3279. } else
  3280. phba->sli4_hba.intr_enable = 0;
  3281. return retval;
  3282. }
  3283. /**
  3284. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3285. * @phba: Pointer to HBA context object.
  3286. * @mask: Bit mask to be checked.
  3287. *
  3288. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3289. * from the API jump table function pointer from the lpfc_hba struct.
  3290. **/
  3291. int
  3292. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3293. {
  3294. return phba->lpfc_sli_brdready(phba, mask);
  3295. }
  3296. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3297. /**
  3298. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3299. * @phba: Pointer to HBA context object.
  3300. *
  3301. * This function is called before resetting an HBA. This function is called
  3302. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3303. **/
  3304. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3305. {
  3306. uint32_t __iomem *resp_buf;
  3307. uint32_t __iomem *mbox_buf;
  3308. volatile uint32_t mbox;
  3309. uint32_t hc_copy, ha_copy, resp_data;
  3310. int i;
  3311. uint8_t hdrtype;
  3312. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3313. if (hdrtype != 0x80 ||
  3314. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3315. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3316. return;
  3317. /*
  3318. * Tell the other part of the chip to suspend temporarily all
  3319. * its DMA activity.
  3320. */
  3321. resp_buf = phba->MBslimaddr;
  3322. /* Disable the error attention */
  3323. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3324. return;
  3325. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3326. readl(phba->HCregaddr); /* flush */
  3327. phba->link_flag |= LS_IGNORE_ERATT;
  3328. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3329. return;
  3330. if (ha_copy & HA_ERATT) {
  3331. /* Clear Chip error bit */
  3332. writel(HA_ERATT, phba->HAregaddr);
  3333. phba->pport->stopped = 1;
  3334. }
  3335. mbox = 0;
  3336. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3337. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3338. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3339. mbox_buf = phba->MBslimaddr;
  3340. writel(mbox, mbox_buf);
  3341. for (i = 0; i < 50; i++) {
  3342. if (lpfc_readl((resp_buf + 1), &resp_data))
  3343. return;
  3344. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3345. mdelay(1);
  3346. else
  3347. break;
  3348. }
  3349. resp_data = 0;
  3350. if (lpfc_readl((resp_buf + 1), &resp_data))
  3351. return;
  3352. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3353. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3354. phba->pport->stopped)
  3355. goto restore_hc;
  3356. else
  3357. goto clear_errat;
  3358. }
  3359. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3360. resp_data = 0;
  3361. for (i = 0; i < 500; i++) {
  3362. if (lpfc_readl(resp_buf, &resp_data))
  3363. return;
  3364. if (resp_data != mbox)
  3365. mdelay(1);
  3366. else
  3367. break;
  3368. }
  3369. clear_errat:
  3370. while (++i < 500) {
  3371. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3372. return;
  3373. if (!(ha_copy & HA_ERATT))
  3374. mdelay(1);
  3375. else
  3376. break;
  3377. }
  3378. if (readl(phba->HAregaddr) & HA_ERATT) {
  3379. writel(HA_ERATT, phba->HAregaddr);
  3380. phba->pport->stopped = 1;
  3381. }
  3382. restore_hc:
  3383. phba->link_flag &= ~LS_IGNORE_ERATT;
  3384. writel(hc_copy, phba->HCregaddr);
  3385. readl(phba->HCregaddr); /* flush */
  3386. }
  3387. /**
  3388. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3389. * @phba: Pointer to HBA context object.
  3390. *
  3391. * This function issues a kill_board mailbox command and waits for
  3392. * the error attention interrupt. This function is called for stopping
  3393. * the firmware processing. The caller is not required to hold any
  3394. * locks. This function calls lpfc_hba_down_post function to free
  3395. * any pending commands after the kill. The function will return 1 when it
  3396. * fails to kill the board else will return 0.
  3397. **/
  3398. int
  3399. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3400. {
  3401. struct lpfc_sli *psli;
  3402. LPFC_MBOXQ_t *pmb;
  3403. uint32_t status;
  3404. uint32_t ha_copy;
  3405. int retval;
  3406. int i = 0;
  3407. psli = &phba->sli;
  3408. /* Kill HBA */
  3409. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3410. "0329 Kill HBA Data: x%x x%x\n",
  3411. phba->pport->port_state, psli->sli_flag);
  3412. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3413. if (!pmb)
  3414. return 1;
  3415. /* Disable the error attention */
  3416. spin_lock_irq(&phba->hbalock);
  3417. if (lpfc_readl(phba->HCregaddr, &status)) {
  3418. spin_unlock_irq(&phba->hbalock);
  3419. mempool_free(pmb, phba->mbox_mem_pool);
  3420. return 1;
  3421. }
  3422. status &= ~HC_ERINT_ENA;
  3423. writel(status, phba->HCregaddr);
  3424. readl(phba->HCregaddr); /* flush */
  3425. phba->link_flag |= LS_IGNORE_ERATT;
  3426. spin_unlock_irq(&phba->hbalock);
  3427. lpfc_kill_board(phba, pmb);
  3428. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3429. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3430. if (retval != MBX_SUCCESS) {
  3431. if (retval != MBX_BUSY)
  3432. mempool_free(pmb, phba->mbox_mem_pool);
  3433. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3434. "2752 KILL_BOARD command failed retval %d\n",
  3435. retval);
  3436. spin_lock_irq(&phba->hbalock);
  3437. phba->link_flag &= ~LS_IGNORE_ERATT;
  3438. spin_unlock_irq(&phba->hbalock);
  3439. return 1;
  3440. }
  3441. spin_lock_irq(&phba->hbalock);
  3442. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3443. spin_unlock_irq(&phba->hbalock);
  3444. mempool_free(pmb, phba->mbox_mem_pool);
  3445. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3446. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3447. * 3 seconds we still set HBA_ERROR state because the status of the
  3448. * board is now undefined.
  3449. */
  3450. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3451. return 1;
  3452. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3453. mdelay(100);
  3454. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3455. return 1;
  3456. }
  3457. del_timer_sync(&psli->mbox_tmo);
  3458. if (ha_copy & HA_ERATT) {
  3459. writel(HA_ERATT, phba->HAregaddr);
  3460. phba->pport->stopped = 1;
  3461. }
  3462. spin_lock_irq(&phba->hbalock);
  3463. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3464. psli->mbox_active = NULL;
  3465. phba->link_flag &= ~LS_IGNORE_ERATT;
  3466. spin_unlock_irq(&phba->hbalock);
  3467. lpfc_hba_down_post(phba);
  3468. phba->link_state = LPFC_HBA_ERROR;
  3469. return ha_copy & HA_ERATT ? 0 : 1;
  3470. }
  3471. /**
  3472. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3473. * @phba: Pointer to HBA context object.
  3474. *
  3475. * This function resets the HBA by writing HC_INITFF to the control
  3476. * register. After the HBA resets, this function resets all the iocb ring
  3477. * indices. This function disables PCI layer parity checking during
  3478. * the reset.
  3479. * This function returns 0 always.
  3480. * The caller is not required to hold any locks.
  3481. **/
  3482. int
  3483. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3484. {
  3485. struct lpfc_sli *psli;
  3486. struct lpfc_sli_ring *pring;
  3487. uint16_t cfg_value;
  3488. int i;
  3489. psli = &phba->sli;
  3490. /* Reset HBA */
  3491. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3492. "0325 Reset HBA Data: x%x x%x\n",
  3493. phba->pport->port_state, psli->sli_flag);
  3494. /* perform board reset */
  3495. phba->fc_eventTag = 0;
  3496. phba->link_events = 0;
  3497. phba->pport->fc_myDID = 0;
  3498. phba->pport->fc_prevDID = 0;
  3499. /* Turn off parity checking and serr during the physical reset */
  3500. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3501. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3502. (cfg_value &
  3503. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3504. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3505. /* Now toggle INITFF bit in the Host Control Register */
  3506. writel(HC_INITFF, phba->HCregaddr);
  3507. mdelay(1);
  3508. readl(phba->HCregaddr); /* flush */
  3509. writel(0, phba->HCregaddr);
  3510. readl(phba->HCregaddr); /* flush */
  3511. /* Restore PCI cmd register */
  3512. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3513. /* Initialize relevant SLI info */
  3514. for (i = 0; i < psli->num_rings; i++) {
  3515. pring = &psli->ring[i];
  3516. pring->flag = 0;
  3517. pring->rspidx = 0;
  3518. pring->next_cmdidx = 0;
  3519. pring->local_getidx = 0;
  3520. pring->cmdidx = 0;
  3521. pring->missbufcnt = 0;
  3522. }
  3523. phba->link_state = LPFC_WARM_START;
  3524. return 0;
  3525. }
  3526. /**
  3527. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3528. * @phba: Pointer to HBA context object.
  3529. *
  3530. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3531. * checking during resets the device. The caller is not required to hold
  3532. * any locks.
  3533. *
  3534. * This function returns 0 always.
  3535. **/
  3536. int
  3537. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3538. {
  3539. struct lpfc_sli *psli = &phba->sli;
  3540. uint16_t cfg_value;
  3541. int rc;
  3542. /* Reset HBA */
  3543. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3544. "0295 Reset HBA Data: x%x x%x\n",
  3545. phba->pport->port_state, psli->sli_flag);
  3546. /* perform board reset */
  3547. phba->fc_eventTag = 0;
  3548. phba->link_events = 0;
  3549. phba->pport->fc_myDID = 0;
  3550. phba->pport->fc_prevDID = 0;
  3551. spin_lock_irq(&phba->hbalock);
  3552. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3553. phba->fcf.fcf_flag = 0;
  3554. spin_unlock_irq(&phba->hbalock);
  3555. /* Now physically reset the device */
  3556. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3557. "0389 Performing PCI function reset!\n");
  3558. /* Turn off parity checking and serr during the physical reset */
  3559. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3560. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3561. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3562. /* Perform FCoE PCI function reset */
  3563. lpfc_sli4_queue_destroy(phba);
  3564. rc = lpfc_pci_function_reset(phba);
  3565. /* Restore PCI cmd register */
  3566. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3567. return rc;
  3568. }
  3569. /**
  3570. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3571. * @phba: Pointer to HBA context object.
  3572. *
  3573. * This function is called in the SLI initialization code path to
  3574. * restart the HBA. The caller is not required to hold any lock.
  3575. * This function writes MBX_RESTART mailbox command to the SLIM and
  3576. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3577. * function to free any pending commands. The function enables
  3578. * POST only during the first initialization. The function returns zero.
  3579. * The function does not guarantee completion of MBX_RESTART mailbox
  3580. * command before the return of this function.
  3581. **/
  3582. static int
  3583. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3584. {
  3585. MAILBOX_t *mb;
  3586. struct lpfc_sli *psli;
  3587. volatile uint32_t word0;
  3588. void __iomem *to_slim;
  3589. uint32_t hba_aer_enabled;
  3590. spin_lock_irq(&phba->hbalock);
  3591. /* Take PCIe device Advanced Error Reporting (AER) state */
  3592. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3593. psli = &phba->sli;
  3594. /* Restart HBA */
  3595. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3596. "0337 Restart HBA Data: x%x x%x\n",
  3597. phba->pport->port_state, psli->sli_flag);
  3598. word0 = 0;
  3599. mb = (MAILBOX_t *) &word0;
  3600. mb->mbxCommand = MBX_RESTART;
  3601. mb->mbxHc = 1;
  3602. lpfc_reset_barrier(phba);
  3603. to_slim = phba->MBslimaddr;
  3604. writel(*(uint32_t *) mb, to_slim);
  3605. readl(to_slim); /* flush */
  3606. /* Only skip post after fc_ffinit is completed */
  3607. if (phba->pport->port_state)
  3608. word0 = 1; /* This is really setting up word1 */
  3609. else
  3610. word0 = 0; /* This is really setting up word1 */
  3611. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3612. writel(*(uint32_t *) mb, to_slim);
  3613. readl(to_slim); /* flush */
  3614. lpfc_sli_brdreset(phba);
  3615. phba->pport->stopped = 0;
  3616. phba->link_state = LPFC_INIT_START;
  3617. phba->hba_flag = 0;
  3618. spin_unlock_irq(&phba->hbalock);
  3619. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3620. psli->stats_start = get_seconds();
  3621. /* Give the INITFF and Post time to settle. */
  3622. mdelay(100);
  3623. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3624. if (hba_aer_enabled)
  3625. pci_disable_pcie_error_reporting(phba->pcidev);
  3626. lpfc_hba_down_post(phba);
  3627. return 0;
  3628. }
  3629. /**
  3630. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3631. * @phba: Pointer to HBA context object.
  3632. *
  3633. * This function is called in the SLI initialization code path to restart
  3634. * a SLI4 HBA. The caller is not required to hold any lock.
  3635. * At the end of the function, it calls lpfc_hba_down_post function to
  3636. * free any pending commands.
  3637. **/
  3638. static int
  3639. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3640. {
  3641. struct lpfc_sli *psli = &phba->sli;
  3642. uint32_t hba_aer_enabled;
  3643. int rc;
  3644. /* Restart HBA */
  3645. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3646. "0296 Restart HBA Data: x%x x%x\n",
  3647. phba->pport->port_state, psli->sli_flag);
  3648. /* Take PCIe device Advanced Error Reporting (AER) state */
  3649. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3650. rc = lpfc_sli4_brdreset(phba);
  3651. spin_lock_irq(&phba->hbalock);
  3652. phba->pport->stopped = 0;
  3653. phba->link_state = LPFC_INIT_START;
  3654. phba->hba_flag = 0;
  3655. spin_unlock_irq(&phba->hbalock);
  3656. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3657. psli->stats_start = get_seconds();
  3658. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3659. if (hba_aer_enabled)
  3660. pci_disable_pcie_error_reporting(phba->pcidev);
  3661. lpfc_hba_down_post(phba);
  3662. return rc;
  3663. }
  3664. /**
  3665. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3666. * @phba: Pointer to HBA context object.
  3667. *
  3668. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3669. * API jump table function pointer from the lpfc_hba struct.
  3670. **/
  3671. int
  3672. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3673. {
  3674. return phba->lpfc_sli_brdrestart(phba);
  3675. }
  3676. /**
  3677. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3678. * @phba: Pointer to HBA context object.
  3679. *
  3680. * This function is called after a HBA restart to wait for successful
  3681. * restart of the HBA. Successful restart of the HBA is indicated by
  3682. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3683. * iteration, the function will restart the HBA again. The function returns
  3684. * zero if HBA successfully restarted else returns negative error code.
  3685. **/
  3686. static int
  3687. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3688. {
  3689. uint32_t status, i = 0;
  3690. /* Read the HBA Host Status Register */
  3691. if (lpfc_readl(phba->HSregaddr, &status))
  3692. return -EIO;
  3693. /* Check status register to see what current state is */
  3694. i = 0;
  3695. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3696. /* Check every 10ms for 10 retries, then every 100ms for 90
  3697. * retries, then every 1 sec for 50 retires for a total of
  3698. * ~60 seconds before reset the board again and check every
  3699. * 1 sec for 50 retries. The up to 60 seconds before the
  3700. * board ready is required by the Falcon FIPS zeroization
  3701. * complete, and any reset the board in between shall cause
  3702. * restart of zeroization, further delay the board ready.
  3703. */
  3704. if (i++ >= 200) {
  3705. /* Adapter failed to init, timeout, status reg
  3706. <status> */
  3707. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3708. "0436 Adapter failed to init, "
  3709. "timeout, status reg x%x, "
  3710. "FW Data: A8 x%x AC x%x\n", status,
  3711. readl(phba->MBslimaddr + 0xa8),
  3712. readl(phba->MBslimaddr + 0xac));
  3713. phba->link_state = LPFC_HBA_ERROR;
  3714. return -ETIMEDOUT;
  3715. }
  3716. /* Check to see if any errors occurred during init */
  3717. if (status & HS_FFERM) {
  3718. /* ERROR: During chipset initialization */
  3719. /* Adapter failed to init, chipset, status reg
  3720. <status> */
  3721. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3722. "0437 Adapter failed to init, "
  3723. "chipset, status reg x%x, "
  3724. "FW Data: A8 x%x AC x%x\n", status,
  3725. readl(phba->MBslimaddr + 0xa8),
  3726. readl(phba->MBslimaddr + 0xac));
  3727. phba->link_state = LPFC_HBA_ERROR;
  3728. return -EIO;
  3729. }
  3730. if (i <= 10)
  3731. msleep(10);
  3732. else if (i <= 100)
  3733. msleep(100);
  3734. else
  3735. msleep(1000);
  3736. if (i == 150) {
  3737. /* Do post */
  3738. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3739. lpfc_sli_brdrestart(phba);
  3740. }
  3741. /* Read the HBA Host Status Register */
  3742. if (lpfc_readl(phba->HSregaddr, &status))
  3743. return -EIO;
  3744. }
  3745. /* Check to see if any errors occurred during init */
  3746. if (status & HS_FFERM) {
  3747. /* ERROR: During chipset initialization */
  3748. /* Adapter failed to init, chipset, status reg <status> */
  3749. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3750. "0438 Adapter failed to init, chipset, "
  3751. "status reg x%x, "
  3752. "FW Data: A8 x%x AC x%x\n", status,
  3753. readl(phba->MBslimaddr + 0xa8),
  3754. readl(phba->MBslimaddr + 0xac));
  3755. phba->link_state = LPFC_HBA_ERROR;
  3756. return -EIO;
  3757. }
  3758. /* Clear all interrupt enable conditions */
  3759. writel(0, phba->HCregaddr);
  3760. readl(phba->HCregaddr); /* flush */
  3761. /* setup host attn register */
  3762. writel(0xffffffff, phba->HAregaddr);
  3763. readl(phba->HAregaddr); /* flush */
  3764. return 0;
  3765. }
  3766. /**
  3767. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3768. *
  3769. * This function calculates and returns the number of HBQs required to be
  3770. * configured.
  3771. **/
  3772. int
  3773. lpfc_sli_hbq_count(void)
  3774. {
  3775. return ARRAY_SIZE(lpfc_hbq_defs);
  3776. }
  3777. /**
  3778. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3779. *
  3780. * This function adds the number of hbq entries in every HBQ to get
  3781. * the total number of hbq entries required for the HBA and returns
  3782. * the total count.
  3783. **/
  3784. static int
  3785. lpfc_sli_hbq_entry_count(void)
  3786. {
  3787. int hbq_count = lpfc_sli_hbq_count();
  3788. int count = 0;
  3789. int i;
  3790. for (i = 0; i < hbq_count; ++i)
  3791. count += lpfc_hbq_defs[i]->entry_count;
  3792. return count;
  3793. }
  3794. /**
  3795. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3796. *
  3797. * This function calculates amount of memory required for all hbq entries
  3798. * to be configured and returns the total memory required.
  3799. **/
  3800. int
  3801. lpfc_sli_hbq_size(void)
  3802. {
  3803. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3804. }
  3805. /**
  3806. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3807. * @phba: Pointer to HBA context object.
  3808. *
  3809. * This function is called during the SLI initialization to configure
  3810. * all the HBQs and post buffers to the HBQ. The caller is not
  3811. * required to hold any locks. This function will return zero if successful
  3812. * else it will return negative error code.
  3813. **/
  3814. static int
  3815. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3816. {
  3817. int hbq_count = lpfc_sli_hbq_count();
  3818. LPFC_MBOXQ_t *pmb;
  3819. MAILBOX_t *pmbox;
  3820. uint32_t hbqno;
  3821. uint32_t hbq_entry_index;
  3822. /* Get a Mailbox buffer to setup mailbox
  3823. * commands for HBA initialization
  3824. */
  3825. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3826. if (!pmb)
  3827. return -ENOMEM;
  3828. pmbox = &pmb->u.mb;
  3829. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3830. phba->link_state = LPFC_INIT_MBX_CMDS;
  3831. phba->hbq_in_use = 1;
  3832. hbq_entry_index = 0;
  3833. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3834. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3835. phba->hbqs[hbqno].hbqPutIdx = 0;
  3836. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3837. phba->hbqs[hbqno].entry_count =
  3838. lpfc_hbq_defs[hbqno]->entry_count;
  3839. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3840. hbq_entry_index, pmb);
  3841. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3842. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3843. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3844. mbxStatus <status>, ring <num> */
  3845. lpfc_printf_log(phba, KERN_ERR,
  3846. LOG_SLI | LOG_VPORT,
  3847. "1805 Adapter failed to init. "
  3848. "Data: x%x x%x x%x\n",
  3849. pmbox->mbxCommand,
  3850. pmbox->mbxStatus, hbqno);
  3851. phba->link_state = LPFC_HBA_ERROR;
  3852. mempool_free(pmb, phba->mbox_mem_pool);
  3853. return -ENXIO;
  3854. }
  3855. }
  3856. phba->hbq_count = hbq_count;
  3857. mempool_free(pmb, phba->mbox_mem_pool);
  3858. /* Initially populate or replenish the HBQs */
  3859. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3860. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3861. return 0;
  3862. }
  3863. /**
  3864. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3865. * @phba: Pointer to HBA context object.
  3866. *
  3867. * This function is called during the SLI initialization to configure
  3868. * all the HBQs and post buffers to the HBQ. The caller is not
  3869. * required to hold any locks. This function will return zero if successful
  3870. * else it will return negative error code.
  3871. **/
  3872. static int
  3873. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3874. {
  3875. phba->hbq_in_use = 1;
  3876. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3877. phba->hbq_count = 1;
  3878. /* Initially populate or replenish the HBQs */
  3879. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3880. return 0;
  3881. }
  3882. /**
  3883. * lpfc_sli_config_port - Issue config port mailbox command
  3884. * @phba: Pointer to HBA context object.
  3885. * @sli_mode: sli mode - 2/3
  3886. *
  3887. * This function is called by the sli intialization code path
  3888. * to issue config_port mailbox command. This function restarts the
  3889. * HBA firmware and issues a config_port mailbox command to configure
  3890. * the SLI interface in the sli mode specified by sli_mode
  3891. * variable. The caller is not required to hold any locks.
  3892. * The function returns 0 if successful, else returns negative error
  3893. * code.
  3894. **/
  3895. int
  3896. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3897. {
  3898. LPFC_MBOXQ_t *pmb;
  3899. uint32_t resetcount = 0, rc = 0, done = 0;
  3900. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3901. if (!pmb) {
  3902. phba->link_state = LPFC_HBA_ERROR;
  3903. return -ENOMEM;
  3904. }
  3905. phba->sli_rev = sli_mode;
  3906. while (resetcount < 2 && !done) {
  3907. spin_lock_irq(&phba->hbalock);
  3908. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3909. spin_unlock_irq(&phba->hbalock);
  3910. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3911. lpfc_sli_brdrestart(phba);
  3912. rc = lpfc_sli_chipset_init(phba);
  3913. if (rc)
  3914. break;
  3915. spin_lock_irq(&phba->hbalock);
  3916. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3917. spin_unlock_irq(&phba->hbalock);
  3918. resetcount++;
  3919. /* Call pre CONFIG_PORT mailbox command initialization. A
  3920. * value of 0 means the call was successful. Any other
  3921. * nonzero value is a failure, but if ERESTART is returned,
  3922. * the driver may reset the HBA and try again.
  3923. */
  3924. rc = lpfc_config_port_prep(phba);
  3925. if (rc == -ERESTART) {
  3926. phba->link_state = LPFC_LINK_UNKNOWN;
  3927. continue;
  3928. } else if (rc)
  3929. break;
  3930. phba->link_state = LPFC_INIT_MBX_CMDS;
  3931. lpfc_config_port(phba, pmb);
  3932. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3933. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3934. LPFC_SLI3_HBQ_ENABLED |
  3935. LPFC_SLI3_CRP_ENABLED |
  3936. LPFC_SLI3_BG_ENABLED |
  3937. LPFC_SLI3_DSS_ENABLED);
  3938. if (rc != MBX_SUCCESS) {
  3939. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3940. "0442 Adapter failed to init, mbxCmd x%x "
  3941. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3942. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3943. spin_lock_irq(&phba->hbalock);
  3944. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3945. spin_unlock_irq(&phba->hbalock);
  3946. rc = -ENXIO;
  3947. } else {
  3948. /* Allow asynchronous mailbox command to go through */
  3949. spin_lock_irq(&phba->hbalock);
  3950. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3951. spin_unlock_irq(&phba->hbalock);
  3952. done = 1;
  3953. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  3954. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  3955. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3956. "3110 Port did not grant ASABT\n");
  3957. }
  3958. }
  3959. if (!done) {
  3960. rc = -EINVAL;
  3961. goto do_prep_failed;
  3962. }
  3963. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3964. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3965. rc = -ENXIO;
  3966. goto do_prep_failed;
  3967. }
  3968. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3969. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3970. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3971. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3972. phba->max_vpi : phba->max_vports;
  3973. } else
  3974. phba->max_vpi = 0;
  3975. phba->fips_level = 0;
  3976. phba->fips_spec_rev = 0;
  3977. if (pmb->u.mb.un.varCfgPort.gdss) {
  3978. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3979. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  3980. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  3981. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3982. "2850 Security Crypto Active. FIPS x%d "
  3983. "(Spec Rev: x%d)",
  3984. phba->fips_level, phba->fips_spec_rev);
  3985. }
  3986. if (pmb->u.mb.un.varCfgPort.sec_err) {
  3987. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3988. "2856 Config Port Security Crypto "
  3989. "Error: x%x ",
  3990. pmb->u.mb.un.varCfgPort.sec_err);
  3991. }
  3992. if (pmb->u.mb.un.varCfgPort.gerbm)
  3993. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  3994. if (pmb->u.mb.un.varCfgPort.gcrp)
  3995. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  3996. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  3997. phba->port_gp = phba->mbox->us.s3_pgp.port;
  3998. if (phba->cfg_enable_bg) {
  3999. if (pmb->u.mb.un.varCfgPort.gbg)
  4000. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  4001. else
  4002. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4003. "0443 Adapter did not grant "
  4004. "BlockGuard\n");
  4005. }
  4006. } else {
  4007. phba->hbq_get = NULL;
  4008. phba->port_gp = phba->mbox->us.s2.port;
  4009. phba->max_vpi = 0;
  4010. }
  4011. do_prep_failed:
  4012. mempool_free(pmb, phba->mbox_mem_pool);
  4013. return rc;
  4014. }
  4015. /**
  4016. * lpfc_sli_hba_setup - SLI intialization function
  4017. * @phba: Pointer to HBA context object.
  4018. *
  4019. * This function is the main SLI intialization function. This function
  4020. * is called by the HBA intialization code, HBA reset code and HBA
  4021. * error attention handler code. Caller is not required to hold any
  4022. * locks. This function issues config_port mailbox command to configure
  4023. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4024. * calls the config_port_post function to issue init_link mailbox
  4025. * command and to start the discovery. The function will return zero
  4026. * if successful, else it will return negative error code.
  4027. **/
  4028. int
  4029. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4030. {
  4031. uint32_t rc;
  4032. int mode = 3, i;
  4033. int longs;
  4034. switch (lpfc_sli_mode) {
  4035. case 2:
  4036. if (phba->cfg_enable_npiv) {
  4037. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4038. "1824 NPIV enabled: Override lpfc_sli_mode "
  4039. "parameter (%d) to auto (0).\n",
  4040. lpfc_sli_mode);
  4041. break;
  4042. }
  4043. mode = 2;
  4044. break;
  4045. case 0:
  4046. case 3:
  4047. break;
  4048. default:
  4049. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4050. "1819 Unrecognized lpfc_sli_mode "
  4051. "parameter: %d.\n", lpfc_sli_mode);
  4052. break;
  4053. }
  4054. rc = lpfc_sli_config_port(phba, mode);
  4055. if (rc && lpfc_sli_mode == 3)
  4056. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4057. "1820 Unable to select SLI-3. "
  4058. "Not supported by adapter.\n");
  4059. if (rc && mode != 2)
  4060. rc = lpfc_sli_config_port(phba, 2);
  4061. if (rc)
  4062. goto lpfc_sli_hba_setup_error;
  4063. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4064. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4065. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4066. if (!rc) {
  4067. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4068. "2709 This device supports "
  4069. "Advanced Error Reporting (AER)\n");
  4070. spin_lock_irq(&phba->hbalock);
  4071. phba->hba_flag |= HBA_AER_ENABLED;
  4072. spin_unlock_irq(&phba->hbalock);
  4073. } else {
  4074. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4075. "2708 This device does not support "
  4076. "Advanced Error Reporting (AER)\n");
  4077. phba->cfg_aer_support = 0;
  4078. }
  4079. }
  4080. if (phba->sli_rev == 3) {
  4081. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4082. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4083. } else {
  4084. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4085. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4086. phba->sli3_options = 0;
  4087. }
  4088. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4089. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4090. phba->sli_rev, phba->max_vpi);
  4091. rc = lpfc_sli_ring_map(phba);
  4092. if (rc)
  4093. goto lpfc_sli_hba_setup_error;
  4094. /* Initialize VPIs. */
  4095. if (phba->sli_rev == LPFC_SLI_REV3) {
  4096. /*
  4097. * The VPI bitmask and physical ID array are allocated
  4098. * and initialized once only - at driver load. A port
  4099. * reset doesn't need to reinitialize this memory.
  4100. */
  4101. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4102. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4103. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4104. GFP_KERNEL);
  4105. if (!phba->vpi_bmask) {
  4106. rc = -ENOMEM;
  4107. goto lpfc_sli_hba_setup_error;
  4108. }
  4109. phba->vpi_ids = kzalloc(
  4110. (phba->max_vpi+1) * sizeof(uint16_t),
  4111. GFP_KERNEL);
  4112. if (!phba->vpi_ids) {
  4113. kfree(phba->vpi_bmask);
  4114. rc = -ENOMEM;
  4115. goto lpfc_sli_hba_setup_error;
  4116. }
  4117. for (i = 0; i < phba->max_vpi; i++)
  4118. phba->vpi_ids[i] = i;
  4119. }
  4120. }
  4121. /* Init HBQs */
  4122. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4123. rc = lpfc_sli_hbq_setup(phba);
  4124. if (rc)
  4125. goto lpfc_sli_hba_setup_error;
  4126. }
  4127. spin_lock_irq(&phba->hbalock);
  4128. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4129. spin_unlock_irq(&phba->hbalock);
  4130. rc = lpfc_config_port_post(phba);
  4131. if (rc)
  4132. goto lpfc_sli_hba_setup_error;
  4133. return rc;
  4134. lpfc_sli_hba_setup_error:
  4135. phba->link_state = LPFC_HBA_ERROR;
  4136. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4137. "0445 Firmware initialization failed\n");
  4138. return rc;
  4139. }
  4140. /**
  4141. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4142. * @phba: Pointer to HBA context object.
  4143. * @mboxq: mailbox pointer.
  4144. * This function issue a dump mailbox command to read config region
  4145. * 23 and parse the records in the region and populate driver
  4146. * data structure.
  4147. **/
  4148. static int
  4149. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4150. {
  4151. LPFC_MBOXQ_t *mboxq;
  4152. struct lpfc_dmabuf *mp;
  4153. struct lpfc_mqe *mqe;
  4154. uint32_t data_length;
  4155. int rc;
  4156. /* Program the default value of vlan_id and fc_map */
  4157. phba->valid_vlan = 0;
  4158. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4159. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4160. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4161. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4162. if (!mboxq)
  4163. return -ENOMEM;
  4164. mqe = &mboxq->u.mqe;
  4165. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4166. rc = -ENOMEM;
  4167. goto out_free_mboxq;
  4168. }
  4169. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4170. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4171. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4172. "(%d):2571 Mailbox cmd x%x Status x%x "
  4173. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4174. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4175. "CQ: x%x x%x x%x x%x\n",
  4176. mboxq->vport ? mboxq->vport->vpi : 0,
  4177. bf_get(lpfc_mqe_command, mqe),
  4178. bf_get(lpfc_mqe_status, mqe),
  4179. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4180. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4181. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4182. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4183. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4184. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4185. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4186. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4187. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4188. mboxq->mcqe.word0,
  4189. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4190. mboxq->mcqe.trailer);
  4191. if (rc) {
  4192. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4193. kfree(mp);
  4194. rc = -EIO;
  4195. goto out_free_mboxq;
  4196. }
  4197. data_length = mqe->un.mb_words[5];
  4198. if (data_length > DMP_RGN23_SIZE) {
  4199. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4200. kfree(mp);
  4201. rc = -EIO;
  4202. goto out_free_mboxq;
  4203. }
  4204. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4205. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4206. kfree(mp);
  4207. rc = 0;
  4208. out_free_mboxq:
  4209. mempool_free(mboxq, phba->mbox_mem_pool);
  4210. return rc;
  4211. }
  4212. /**
  4213. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4214. * @phba: pointer to lpfc hba data structure.
  4215. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4216. * @vpd: pointer to the memory to hold resulting port vpd data.
  4217. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4218. * On output, the number of data bytes in @vpd.
  4219. *
  4220. * This routine executes a READ_REV SLI4 mailbox command. In
  4221. * addition, this routine gets the port vpd data.
  4222. *
  4223. * Return codes
  4224. * 0 - successful
  4225. * -ENOMEM - could not allocated memory.
  4226. **/
  4227. static int
  4228. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4229. uint8_t *vpd, uint32_t *vpd_size)
  4230. {
  4231. int rc = 0;
  4232. uint32_t dma_size;
  4233. struct lpfc_dmabuf *dmabuf;
  4234. struct lpfc_mqe *mqe;
  4235. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4236. if (!dmabuf)
  4237. return -ENOMEM;
  4238. /*
  4239. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4240. * mailbox command.
  4241. */
  4242. dma_size = *vpd_size;
  4243. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4244. dma_size,
  4245. &dmabuf->phys,
  4246. GFP_KERNEL);
  4247. if (!dmabuf->virt) {
  4248. kfree(dmabuf);
  4249. return -ENOMEM;
  4250. }
  4251. memset(dmabuf->virt, 0, dma_size);
  4252. /*
  4253. * The SLI4 implementation of READ_REV conflicts at word1,
  4254. * bits 31:16 and SLI4 adds vpd functionality not present
  4255. * in SLI3. This code corrects the conflicts.
  4256. */
  4257. lpfc_read_rev(phba, mboxq);
  4258. mqe = &mboxq->u.mqe;
  4259. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4260. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4261. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4262. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4263. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4264. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4265. if (rc) {
  4266. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4267. dmabuf->virt, dmabuf->phys);
  4268. kfree(dmabuf);
  4269. return -EIO;
  4270. }
  4271. /*
  4272. * The available vpd length cannot be bigger than the
  4273. * DMA buffer passed to the port. Catch the less than
  4274. * case and update the caller's size.
  4275. */
  4276. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4277. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4278. memcpy(vpd, dmabuf->virt, *vpd_size);
  4279. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4280. dmabuf->virt, dmabuf->phys);
  4281. kfree(dmabuf);
  4282. return 0;
  4283. }
  4284. /**
  4285. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4286. * @phba: pointer to lpfc hba data structure.
  4287. *
  4288. * This routine retrieves SLI4 device physical port name this PCI function
  4289. * is attached to.
  4290. *
  4291. * Return codes
  4292. * 0 - sucessful
  4293. * otherwise - failed to retrieve physical port name
  4294. **/
  4295. static int
  4296. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4297. {
  4298. LPFC_MBOXQ_t *mboxq;
  4299. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4300. struct lpfc_controller_attribute *cntl_attr;
  4301. struct lpfc_mbx_get_port_name *get_port_name;
  4302. void *virtaddr = NULL;
  4303. uint32_t alloclen, reqlen;
  4304. uint32_t shdr_status, shdr_add_status;
  4305. union lpfc_sli4_cfg_shdr *shdr;
  4306. char cport_name = 0;
  4307. int rc;
  4308. /* We assume nothing at this point */
  4309. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4310. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4311. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4312. if (!mboxq)
  4313. return -ENOMEM;
  4314. /* obtain link type and link number via READ_CONFIG */
  4315. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4316. lpfc_sli4_read_config(phba);
  4317. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4318. goto retrieve_ppname;
  4319. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4320. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4321. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4322. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4323. LPFC_SLI4_MBX_NEMBED);
  4324. if (alloclen < reqlen) {
  4325. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4326. "3084 Allocated DMA memory size (%d) is "
  4327. "less than the requested DMA memory size "
  4328. "(%d)\n", alloclen, reqlen);
  4329. rc = -ENOMEM;
  4330. goto out_free_mboxq;
  4331. }
  4332. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4333. virtaddr = mboxq->sge_array->addr[0];
  4334. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4335. shdr = &mbx_cntl_attr->cfg_shdr;
  4336. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4337. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4338. if (shdr_status || shdr_add_status || rc) {
  4339. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4340. "3085 Mailbox x%x (x%x/x%x) failed, "
  4341. "rc:x%x, status:x%x, add_status:x%x\n",
  4342. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4343. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4344. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4345. rc, shdr_status, shdr_add_status);
  4346. rc = -ENXIO;
  4347. goto out_free_mboxq;
  4348. }
  4349. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4350. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4351. phba->sli4_hba.lnk_info.lnk_tp =
  4352. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4353. phba->sli4_hba.lnk_info.lnk_no =
  4354. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4355. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4356. "3086 lnk_type:%d, lnk_numb:%d\n",
  4357. phba->sli4_hba.lnk_info.lnk_tp,
  4358. phba->sli4_hba.lnk_info.lnk_no);
  4359. retrieve_ppname:
  4360. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4361. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4362. sizeof(struct lpfc_mbx_get_port_name) -
  4363. sizeof(struct lpfc_sli4_cfg_mhdr),
  4364. LPFC_SLI4_MBX_EMBED);
  4365. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4366. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4367. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4368. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4369. phba->sli4_hba.lnk_info.lnk_tp);
  4370. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4371. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4372. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4373. if (shdr_status || shdr_add_status || rc) {
  4374. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4375. "3087 Mailbox x%x (x%x/x%x) failed: "
  4376. "rc:x%x, status:x%x, add_status:x%x\n",
  4377. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4378. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4379. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4380. rc, shdr_status, shdr_add_status);
  4381. rc = -ENXIO;
  4382. goto out_free_mboxq;
  4383. }
  4384. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4385. case LPFC_LINK_NUMBER_0:
  4386. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4387. &get_port_name->u.response);
  4388. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4389. break;
  4390. case LPFC_LINK_NUMBER_1:
  4391. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4392. &get_port_name->u.response);
  4393. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4394. break;
  4395. case LPFC_LINK_NUMBER_2:
  4396. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4397. &get_port_name->u.response);
  4398. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4399. break;
  4400. case LPFC_LINK_NUMBER_3:
  4401. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4402. &get_port_name->u.response);
  4403. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4404. break;
  4405. default:
  4406. break;
  4407. }
  4408. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4409. phba->Port[0] = cport_name;
  4410. phba->Port[1] = '\0';
  4411. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4412. "3091 SLI get port name: %s\n", phba->Port);
  4413. }
  4414. out_free_mboxq:
  4415. if (rc != MBX_TIMEOUT) {
  4416. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4417. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4418. else
  4419. mempool_free(mboxq, phba->mbox_mem_pool);
  4420. }
  4421. return rc;
  4422. }
  4423. /**
  4424. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4425. * @phba: pointer to lpfc hba data structure.
  4426. *
  4427. * This routine is called to explicitly arm the SLI4 device's completion and
  4428. * event queues
  4429. **/
  4430. static void
  4431. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4432. {
  4433. uint8_t fcp_eqidx;
  4434. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4435. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4436. fcp_eqidx = 0;
  4437. if (phba->sli4_hba.fcp_cq) {
  4438. do
  4439. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4440. LPFC_QUEUE_REARM);
  4441. while (++fcp_eqidx < phba->cfg_fcp_eq_count);
  4442. }
  4443. lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
  4444. if (phba->sli4_hba.fp_eq) {
  4445. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
  4446. fcp_eqidx++)
  4447. lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
  4448. LPFC_QUEUE_REARM);
  4449. }
  4450. }
  4451. /**
  4452. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4453. * @phba: Pointer to HBA context object.
  4454. * @type: The resource extent type.
  4455. * @extnt_count: buffer to hold port available extent count.
  4456. * @extnt_size: buffer to hold element count per extent.
  4457. *
  4458. * This function calls the port and retrievs the number of available
  4459. * extents and their size for a particular extent type.
  4460. *
  4461. * Returns: 0 if successful. Nonzero otherwise.
  4462. **/
  4463. int
  4464. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4465. uint16_t *extnt_count, uint16_t *extnt_size)
  4466. {
  4467. int rc = 0;
  4468. uint32_t length;
  4469. uint32_t mbox_tmo;
  4470. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4471. LPFC_MBOXQ_t *mbox;
  4472. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4473. if (!mbox)
  4474. return -ENOMEM;
  4475. /* Find out how many extents are available for this resource type */
  4476. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4477. sizeof(struct lpfc_sli4_cfg_mhdr));
  4478. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4479. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4480. length, LPFC_SLI4_MBX_EMBED);
  4481. /* Send an extents count of 0 - the GET doesn't use it. */
  4482. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4483. LPFC_SLI4_MBX_EMBED);
  4484. if (unlikely(rc)) {
  4485. rc = -EIO;
  4486. goto err_exit;
  4487. }
  4488. if (!phba->sli4_hba.intr_enable)
  4489. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4490. else {
  4491. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4492. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4493. }
  4494. if (unlikely(rc)) {
  4495. rc = -EIO;
  4496. goto err_exit;
  4497. }
  4498. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4499. if (bf_get(lpfc_mbox_hdr_status,
  4500. &rsrc_info->header.cfg_shdr.response)) {
  4501. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4502. "2930 Failed to get resource extents "
  4503. "Status 0x%x Add'l Status 0x%x\n",
  4504. bf_get(lpfc_mbox_hdr_status,
  4505. &rsrc_info->header.cfg_shdr.response),
  4506. bf_get(lpfc_mbox_hdr_add_status,
  4507. &rsrc_info->header.cfg_shdr.response));
  4508. rc = -EIO;
  4509. goto err_exit;
  4510. }
  4511. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4512. &rsrc_info->u.rsp);
  4513. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4514. &rsrc_info->u.rsp);
  4515. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4516. "3162 Retrieved extents type-%d from port: count:%d, "
  4517. "size:%d\n", type, *extnt_count, *extnt_size);
  4518. err_exit:
  4519. mempool_free(mbox, phba->mbox_mem_pool);
  4520. return rc;
  4521. }
  4522. /**
  4523. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4524. * @phba: Pointer to HBA context object.
  4525. * @type: The extent type to check.
  4526. *
  4527. * This function reads the current available extents from the port and checks
  4528. * if the extent count or extent size has changed since the last access.
  4529. * Callers use this routine post port reset to understand if there is a
  4530. * extent reprovisioning requirement.
  4531. *
  4532. * Returns:
  4533. * -Error: error indicates problem.
  4534. * 1: Extent count or size has changed.
  4535. * 0: No changes.
  4536. **/
  4537. static int
  4538. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4539. {
  4540. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4541. uint16_t size_diff, rsrc_ext_size;
  4542. int rc = 0;
  4543. struct lpfc_rsrc_blks *rsrc_entry;
  4544. struct list_head *rsrc_blk_list = NULL;
  4545. size_diff = 0;
  4546. curr_ext_cnt = 0;
  4547. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4548. &rsrc_ext_cnt,
  4549. &rsrc_ext_size);
  4550. if (unlikely(rc))
  4551. return -EIO;
  4552. switch (type) {
  4553. case LPFC_RSC_TYPE_FCOE_RPI:
  4554. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4555. break;
  4556. case LPFC_RSC_TYPE_FCOE_VPI:
  4557. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4558. break;
  4559. case LPFC_RSC_TYPE_FCOE_XRI:
  4560. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4561. break;
  4562. case LPFC_RSC_TYPE_FCOE_VFI:
  4563. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4564. break;
  4565. default:
  4566. break;
  4567. }
  4568. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4569. curr_ext_cnt++;
  4570. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4571. size_diff++;
  4572. }
  4573. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4574. rc = 1;
  4575. return rc;
  4576. }
  4577. /**
  4578. * lpfc_sli4_cfg_post_extnts -
  4579. * @phba: Pointer to HBA context object.
  4580. * @extnt_cnt - number of available extents.
  4581. * @type - the extent type (rpi, xri, vfi, vpi).
  4582. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4583. * @mbox - pointer to the caller's allocated mailbox structure.
  4584. *
  4585. * This function executes the extents allocation request. It also
  4586. * takes care of the amount of memory needed to allocate or get the
  4587. * allocated extents. It is the caller's responsibility to evaluate
  4588. * the response.
  4589. *
  4590. * Returns:
  4591. * -Error: Error value describes the condition found.
  4592. * 0: if successful
  4593. **/
  4594. static int
  4595. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  4596. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4597. {
  4598. int rc = 0;
  4599. uint32_t req_len;
  4600. uint32_t emb_len;
  4601. uint32_t alloc_len, mbox_tmo;
  4602. /* Calculate the total requested length of the dma memory */
  4603. req_len = extnt_cnt * sizeof(uint16_t);
  4604. /*
  4605. * Calculate the size of an embedded mailbox. The uint32_t
  4606. * accounts for extents-specific word.
  4607. */
  4608. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4609. sizeof(uint32_t);
  4610. /*
  4611. * Presume the allocation and response will fit into an embedded
  4612. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4613. */
  4614. *emb = LPFC_SLI4_MBX_EMBED;
  4615. if (req_len > emb_len) {
  4616. req_len = extnt_cnt * sizeof(uint16_t) +
  4617. sizeof(union lpfc_sli4_cfg_shdr) +
  4618. sizeof(uint32_t);
  4619. *emb = LPFC_SLI4_MBX_NEMBED;
  4620. }
  4621. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4622. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4623. req_len, *emb);
  4624. if (alloc_len < req_len) {
  4625. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4626. "2982 Allocated DMA memory size (x%x) is "
  4627. "less than the requested DMA memory "
  4628. "size (x%x)\n", alloc_len, req_len);
  4629. return -ENOMEM;
  4630. }
  4631. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  4632. if (unlikely(rc))
  4633. return -EIO;
  4634. if (!phba->sli4_hba.intr_enable)
  4635. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4636. else {
  4637. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4638. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4639. }
  4640. if (unlikely(rc))
  4641. rc = -EIO;
  4642. return rc;
  4643. }
  4644. /**
  4645. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4646. * @phba: Pointer to HBA context object.
  4647. * @type: The resource extent type to allocate.
  4648. *
  4649. * This function allocates the number of elements for the specified
  4650. * resource type.
  4651. **/
  4652. static int
  4653. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4654. {
  4655. bool emb = false;
  4656. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4657. uint16_t rsrc_id, rsrc_start, j, k;
  4658. uint16_t *ids;
  4659. int i, rc;
  4660. unsigned long longs;
  4661. unsigned long *bmask;
  4662. struct lpfc_rsrc_blks *rsrc_blks;
  4663. LPFC_MBOXQ_t *mbox;
  4664. uint32_t length;
  4665. struct lpfc_id_range *id_array = NULL;
  4666. void *virtaddr = NULL;
  4667. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4668. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4669. struct list_head *ext_blk_list;
  4670. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4671. &rsrc_cnt,
  4672. &rsrc_size);
  4673. if (unlikely(rc))
  4674. return -EIO;
  4675. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4676. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4677. "3009 No available Resource Extents "
  4678. "for resource type 0x%x: Count: 0x%x, "
  4679. "Size 0x%x\n", type, rsrc_cnt,
  4680. rsrc_size);
  4681. return -ENOMEM;
  4682. }
  4683. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  4684. "2903 Post resource extents type-0x%x: "
  4685. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  4686. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4687. if (!mbox)
  4688. return -ENOMEM;
  4689. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  4690. if (unlikely(rc)) {
  4691. rc = -EIO;
  4692. goto err_exit;
  4693. }
  4694. /*
  4695. * Figure out where the response is located. Then get local pointers
  4696. * to the response data. The port does not guarantee to respond to
  4697. * all extents counts request so update the local variable with the
  4698. * allocated count from the port.
  4699. */
  4700. if (emb == LPFC_SLI4_MBX_EMBED) {
  4701. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4702. id_array = &rsrc_ext->u.rsp.id[0];
  4703. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4704. } else {
  4705. virtaddr = mbox->sge_array->addr[0];
  4706. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4707. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4708. id_array = &n_rsrc->id;
  4709. }
  4710. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4711. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4712. /*
  4713. * Based on the resource size and count, correct the base and max
  4714. * resource values.
  4715. */
  4716. length = sizeof(struct lpfc_rsrc_blks);
  4717. switch (type) {
  4718. case LPFC_RSC_TYPE_FCOE_RPI:
  4719. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4720. sizeof(unsigned long),
  4721. GFP_KERNEL);
  4722. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4723. rc = -ENOMEM;
  4724. goto err_exit;
  4725. }
  4726. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4727. sizeof(uint16_t),
  4728. GFP_KERNEL);
  4729. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4730. kfree(phba->sli4_hba.rpi_bmask);
  4731. rc = -ENOMEM;
  4732. goto err_exit;
  4733. }
  4734. /*
  4735. * The next_rpi was initialized with the maximum available
  4736. * count but the port may allocate a smaller number. Catch
  4737. * that case and update the next_rpi.
  4738. */
  4739. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4740. /* Initialize local ptrs for common extent processing later. */
  4741. bmask = phba->sli4_hba.rpi_bmask;
  4742. ids = phba->sli4_hba.rpi_ids;
  4743. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4744. break;
  4745. case LPFC_RSC_TYPE_FCOE_VPI:
  4746. phba->vpi_bmask = kzalloc(longs *
  4747. sizeof(unsigned long),
  4748. GFP_KERNEL);
  4749. if (unlikely(!phba->vpi_bmask)) {
  4750. rc = -ENOMEM;
  4751. goto err_exit;
  4752. }
  4753. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4754. sizeof(uint16_t),
  4755. GFP_KERNEL);
  4756. if (unlikely(!phba->vpi_ids)) {
  4757. kfree(phba->vpi_bmask);
  4758. rc = -ENOMEM;
  4759. goto err_exit;
  4760. }
  4761. /* Initialize local ptrs for common extent processing later. */
  4762. bmask = phba->vpi_bmask;
  4763. ids = phba->vpi_ids;
  4764. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4765. break;
  4766. case LPFC_RSC_TYPE_FCOE_XRI:
  4767. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4768. sizeof(unsigned long),
  4769. GFP_KERNEL);
  4770. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4771. rc = -ENOMEM;
  4772. goto err_exit;
  4773. }
  4774. phba->sli4_hba.max_cfg_param.xri_used = 0;
  4775. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4776. sizeof(uint16_t),
  4777. GFP_KERNEL);
  4778. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4779. kfree(phba->sli4_hba.xri_bmask);
  4780. rc = -ENOMEM;
  4781. goto err_exit;
  4782. }
  4783. /* Initialize local ptrs for common extent processing later. */
  4784. bmask = phba->sli4_hba.xri_bmask;
  4785. ids = phba->sli4_hba.xri_ids;
  4786. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4787. break;
  4788. case LPFC_RSC_TYPE_FCOE_VFI:
  4789. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4790. sizeof(unsigned long),
  4791. GFP_KERNEL);
  4792. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4793. rc = -ENOMEM;
  4794. goto err_exit;
  4795. }
  4796. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4797. sizeof(uint16_t),
  4798. GFP_KERNEL);
  4799. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4800. kfree(phba->sli4_hba.vfi_bmask);
  4801. rc = -ENOMEM;
  4802. goto err_exit;
  4803. }
  4804. /* Initialize local ptrs for common extent processing later. */
  4805. bmask = phba->sli4_hba.vfi_bmask;
  4806. ids = phba->sli4_hba.vfi_ids;
  4807. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4808. break;
  4809. default:
  4810. /* Unsupported Opcode. Fail call. */
  4811. id_array = NULL;
  4812. bmask = NULL;
  4813. ids = NULL;
  4814. ext_blk_list = NULL;
  4815. goto err_exit;
  4816. }
  4817. /*
  4818. * Complete initializing the extent configuration with the
  4819. * allocated ids assigned to this function. The bitmask serves
  4820. * as an index into the array and manages the available ids. The
  4821. * array just stores the ids communicated to the port via the wqes.
  4822. */
  4823. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4824. if ((i % 2) == 0)
  4825. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4826. &id_array[k]);
  4827. else
  4828. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4829. &id_array[k]);
  4830. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4831. if (unlikely(!rsrc_blks)) {
  4832. rc = -ENOMEM;
  4833. kfree(bmask);
  4834. kfree(ids);
  4835. goto err_exit;
  4836. }
  4837. rsrc_blks->rsrc_start = rsrc_id;
  4838. rsrc_blks->rsrc_size = rsrc_size;
  4839. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4840. rsrc_start = rsrc_id;
  4841. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4842. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4843. lpfc_sli4_get_els_iocb_cnt(phba);
  4844. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4845. ids[j] = rsrc_id;
  4846. rsrc_id++;
  4847. j++;
  4848. }
  4849. /* Entire word processed. Get next word.*/
  4850. if ((i % 2) == 1)
  4851. k++;
  4852. }
  4853. err_exit:
  4854. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4855. return rc;
  4856. }
  4857. /**
  4858. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4859. * @phba: Pointer to HBA context object.
  4860. * @type: the extent's type.
  4861. *
  4862. * This function deallocates all extents of a particular resource type.
  4863. * SLI4 does not allow for deallocating a particular extent range. It
  4864. * is the caller's responsibility to release all kernel memory resources.
  4865. **/
  4866. static int
  4867. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4868. {
  4869. int rc;
  4870. uint32_t length, mbox_tmo = 0;
  4871. LPFC_MBOXQ_t *mbox;
  4872. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4873. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4874. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4875. if (!mbox)
  4876. return -ENOMEM;
  4877. /*
  4878. * This function sends an embedded mailbox because it only sends the
  4879. * the resource type. All extents of this type are released by the
  4880. * port.
  4881. */
  4882. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4883. sizeof(struct lpfc_sli4_cfg_mhdr));
  4884. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4885. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4886. length, LPFC_SLI4_MBX_EMBED);
  4887. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4888. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4889. LPFC_SLI4_MBX_EMBED);
  4890. if (unlikely(rc)) {
  4891. rc = -EIO;
  4892. goto out_free_mbox;
  4893. }
  4894. if (!phba->sli4_hba.intr_enable)
  4895. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4896. else {
  4897. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4898. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4899. }
  4900. if (unlikely(rc)) {
  4901. rc = -EIO;
  4902. goto out_free_mbox;
  4903. }
  4904. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4905. if (bf_get(lpfc_mbox_hdr_status,
  4906. &dealloc_rsrc->header.cfg_shdr.response)) {
  4907. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4908. "2919 Failed to release resource extents "
  4909. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4910. "Resource memory not released.\n",
  4911. type,
  4912. bf_get(lpfc_mbox_hdr_status,
  4913. &dealloc_rsrc->header.cfg_shdr.response),
  4914. bf_get(lpfc_mbox_hdr_add_status,
  4915. &dealloc_rsrc->header.cfg_shdr.response));
  4916. rc = -EIO;
  4917. goto out_free_mbox;
  4918. }
  4919. /* Release kernel memory resources for the specific type. */
  4920. switch (type) {
  4921. case LPFC_RSC_TYPE_FCOE_VPI:
  4922. kfree(phba->vpi_bmask);
  4923. kfree(phba->vpi_ids);
  4924. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4925. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4926. &phba->lpfc_vpi_blk_list, list) {
  4927. list_del_init(&rsrc_blk->list);
  4928. kfree(rsrc_blk);
  4929. }
  4930. break;
  4931. case LPFC_RSC_TYPE_FCOE_XRI:
  4932. kfree(phba->sli4_hba.xri_bmask);
  4933. kfree(phba->sli4_hba.xri_ids);
  4934. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4935. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4936. list_del_init(&rsrc_blk->list);
  4937. kfree(rsrc_blk);
  4938. }
  4939. break;
  4940. case LPFC_RSC_TYPE_FCOE_VFI:
  4941. kfree(phba->sli4_hba.vfi_bmask);
  4942. kfree(phba->sli4_hba.vfi_ids);
  4943. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4944. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4945. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  4946. list_del_init(&rsrc_blk->list);
  4947. kfree(rsrc_blk);
  4948. }
  4949. break;
  4950. case LPFC_RSC_TYPE_FCOE_RPI:
  4951. /* RPI bitmask and physical id array are cleaned up earlier. */
  4952. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4953. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  4954. list_del_init(&rsrc_blk->list);
  4955. kfree(rsrc_blk);
  4956. }
  4957. break;
  4958. default:
  4959. break;
  4960. }
  4961. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4962. out_free_mbox:
  4963. mempool_free(mbox, phba->mbox_mem_pool);
  4964. return rc;
  4965. }
  4966. /**
  4967. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  4968. * @phba: Pointer to HBA context object.
  4969. *
  4970. * This function allocates all SLI4 resource identifiers.
  4971. **/
  4972. int
  4973. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  4974. {
  4975. int i, rc, error = 0;
  4976. uint16_t count, base;
  4977. unsigned long longs;
  4978. if (!phba->sli4_hba.rpi_hdrs_in_use)
  4979. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  4980. if (phba->sli4_hba.extents_in_use) {
  4981. /*
  4982. * The port supports resource extents. The XRI, VPI, VFI, RPI
  4983. * resource extent count must be read and allocated before
  4984. * provisioning the resource id arrays.
  4985. */
  4986. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  4987. LPFC_IDX_RSRC_RDY) {
  4988. /*
  4989. * Extent-based resources are set - the driver could
  4990. * be in a port reset. Figure out if any corrective
  4991. * actions need to be taken.
  4992. */
  4993. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4994. LPFC_RSC_TYPE_FCOE_VFI);
  4995. if (rc != 0)
  4996. error++;
  4997. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4998. LPFC_RSC_TYPE_FCOE_VPI);
  4999. if (rc != 0)
  5000. error++;
  5001. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5002. LPFC_RSC_TYPE_FCOE_XRI);
  5003. if (rc != 0)
  5004. error++;
  5005. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5006. LPFC_RSC_TYPE_FCOE_RPI);
  5007. if (rc != 0)
  5008. error++;
  5009. /*
  5010. * It's possible that the number of resources
  5011. * provided to this port instance changed between
  5012. * resets. Detect this condition and reallocate
  5013. * resources. Otherwise, there is no action.
  5014. */
  5015. if (error) {
  5016. lpfc_printf_log(phba, KERN_INFO,
  5017. LOG_MBOX | LOG_INIT,
  5018. "2931 Detected extent resource "
  5019. "change. Reallocating all "
  5020. "extents.\n");
  5021. rc = lpfc_sli4_dealloc_extent(phba,
  5022. LPFC_RSC_TYPE_FCOE_VFI);
  5023. rc = lpfc_sli4_dealloc_extent(phba,
  5024. LPFC_RSC_TYPE_FCOE_VPI);
  5025. rc = lpfc_sli4_dealloc_extent(phba,
  5026. LPFC_RSC_TYPE_FCOE_XRI);
  5027. rc = lpfc_sli4_dealloc_extent(phba,
  5028. LPFC_RSC_TYPE_FCOE_RPI);
  5029. } else
  5030. return 0;
  5031. }
  5032. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5033. if (unlikely(rc))
  5034. goto err_exit;
  5035. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5036. if (unlikely(rc))
  5037. goto err_exit;
  5038. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5039. if (unlikely(rc))
  5040. goto err_exit;
  5041. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5042. if (unlikely(rc))
  5043. goto err_exit;
  5044. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5045. LPFC_IDX_RSRC_RDY);
  5046. return rc;
  5047. } else {
  5048. /*
  5049. * The port does not support resource extents. The XRI, VPI,
  5050. * VFI, RPI resource ids were determined from READ_CONFIG.
  5051. * Just allocate the bitmasks and provision the resource id
  5052. * arrays. If a port reset is active, the resources don't
  5053. * need any action - just exit.
  5054. */
  5055. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5056. LPFC_IDX_RSRC_RDY) {
  5057. lpfc_sli4_dealloc_resource_identifiers(phba);
  5058. lpfc_sli4_remove_rpis(phba);
  5059. }
  5060. /* RPIs. */
  5061. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5062. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5063. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5064. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5065. sizeof(unsigned long),
  5066. GFP_KERNEL);
  5067. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5068. rc = -ENOMEM;
  5069. goto err_exit;
  5070. }
  5071. phba->sli4_hba.rpi_ids = kzalloc(count *
  5072. sizeof(uint16_t),
  5073. GFP_KERNEL);
  5074. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5075. rc = -ENOMEM;
  5076. goto free_rpi_bmask;
  5077. }
  5078. for (i = 0; i < count; i++)
  5079. phba->sli4_hba.rpi_ids[i] = base + i;
  5080. /* VPIs. */
  5081. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5082. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5083. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5084. phba->vpi_bmask = kzalloc(longs *
  5085. sizeof(unsigned long),
  5086. GFP_KERNEL);
  5087. if (unlikely(!phba->vpi_bmask)) {
  5088. rc = -ENOMEM;
  5089. goto free_rpi_ids;
  5090. }
  5091. phba->vpi_ids = kzalloc(count *
  5092. sizeof(uint16_t),
  5093. GFP_KERNEL);
  5094. if (unlikely(!phba->vpi_ids)) {
  5095. rc = -ENOMEM;
  5096. goto free_vpi_bmask;
  5097. }
  5098. for (i = 0; i < count; i++)
  5099. phba->vpi_ids[i] = base + i;
  5100. /* XRIs. */
  5101. count = phba->sli4_hba.max_cfg_param.max_xri;
  5102. base = phba->sli4_hba.max_cfg_param.xri_base;
  5103. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5104. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5105. sizeof(unsigned long),
  5106. GFP_KERNEL);
  5107. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5108. rc = -ENOMEM;
  5109. goto free_vpi_ids;
  5110. }
  5111. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5112. phba->sli4_hba.xri_ids = kzalloc(count *
  5113. sizeof(uint16_t),
  5114. GFP_KERNEL);
  5115. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5116. rc = -ENOMEM;
  5117. goto free_xri_bmask;
  5118. }
  5119. for (i = 0; i < count; i++)
  5120. phba->sli4_hba.xri_ids[i] = base + i;
  5121. /* VFIs. */
  5122. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5123. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5124. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5125. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5126. sizeof(unsigned long),
  5127. GFP_KERNEL);
  5128. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5129. rc = -ENOMEM;
  5130. goto free_xri_ids;
  5131. }
  5132. phba->sli4_hba.vfi_ids = kzalloc(count *
  5133. sizeof(uint16_t),
  5134. GFP_KERNEL);
  5135. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5136. rc = -ENOMEM;
  5137. goto free_vfi_bmask;
  5138. }
  5139. for (i = 0; i < count; i++)
  5140. phba->sli4_hba.vfi_ids[i] = base + i;
  5141. /*
  5142. * Mark all resources ready. An HBA reset doesn't need
  5143. * to reset the initialization.
  5144. */
  5145. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5146. LPFC_IDX_RSRC_RDY);
  5147. return 0;
  5148. }
  5149. free_vfi_bmask:
  5150. kfree(phba->sli4_hba.vfi_bmask);
  5151. free_xri_ids:
  5152. kfree(phba->sli4_hba.xri_ids);
  5153. free_xri_bmask:
  5154. kfree(phba->sli4_hba.xri_bmask);
  5155. free_vpi_ids:
  5156. kfree(phba->vpi_ids);
  5157. free_vpi_bmask:
  5158. kfree(phba->vpi_bmask);
  5159. free_rpi_ids:
  5160. kfree(phba->sli4_hba.rpi_ids);
  5161. free_rpi_bmask:
  5162. kfree(phba->sli4_hba.rpi_bmask);
  5163. err_exit:
  5164. return rc;
  5165. }
  5166. /**
  5167. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5168. * @phba: Pointer to HBA context object.
  5169. *
  5170. * This function allocates the number of elements for the specified
  5171. * resource type.
  5172. **/
  5173. int
  5174. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5175. {
  5176. if (phba->sli4_hba.extents_in_use) {
  5177. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5178. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5179. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5180. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5181. } else {
  5182. kfree(phba->vpi_bmask);
  5183. kfree(phba->vpi_ids);
  5184. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5185. kfree(phba->sli4_hba.xri_bmask);
  5186. kfree(phba->sli4_hba.xri_ids);
  5187. kfree(phba->sli4_hba.vfi_bmask);
  5188. kfree(phba->sli4_hba.vfi_ids);
  5189. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5190. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5191. }
  5192. return 0;
  5193. }
  5194. /**
  5195. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5196. * @phba: Pointer to HBA context object.
  5197. * @type: The resource extent type.
  5198. * @extnt_count: buffer to hold port extent count response
  5199. * @extnt_size: buffer to hold port extent size response.
  5200. *
  5201. * This function calls the port to read the host allocated extents
  5202. * for a particular type.
  5203. **/
  5204. int
  5205. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5206. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5207. {
  5208. bool emb;
  5209. int rc = 0;
  5210. uint16_t curr_blks = 0;
  5211. uint32_t req_len, emb_len;
  5212. uint32_t alloc_len, mbox_tmo;
  5213. struct list_head *blk_list_head;
  5214. struct lpfc_rsrc_blks *rsrc_blk;
  5215. LPFC_MBOXQ_t *mbox;
  5216. void *virtaddr = NULL;
  5217. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5218. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5219. union lpfc_sli4_cfg_shdr *shdr;
  5220. switch (type) {
  5221. case LPFC_RSC_TYPE_FCOE_VPI:
  5222. blk_list_head = &phba->lpfc_vpi_blk_list;
  5223. break;
  5224. case LPFC_RSC_TYPE_FCOE_XRI:
  5225. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5226. break;
  5227. case LPFC_RSC_TYPE_FCOE_VFI:
  5228. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5229. break;
  5230. case LPFC_RSC_TYPE_FCOE_RPI:
  5231. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5232. break;
  5233. default:
  5234. return -EIO;
  5235. }
  5236. /* Count the number of extents currently allocatd for this type. */
  5237. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5238. if (curr_blks == 0) {
  5239. /*
  5240. * The GET_ALLOCATED mailbox does not return the size,
  5241. * just the count. The size should be just the size
  5242. * stored in the current allocated block and all sizes
  5243. * for an extent type are the same so set the return
  5244. * value now.
  5245. */
  5246. *extnt_size = rsrc_blk->rsrc_size;
  5247. }
  5248. curr_blks++;
  5249. }
  5250. /* Calculate the total requested length of the dma memory. */
  5251. req_len = curr_blks * sizeof(uint16_t);
  5252. /*
  5253. * Calculate the size of an embedded mailbox. The uint32_t
  5254. * accounts for extents-specific word.
  5255. */
  5256. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5257. sizeof(uint32_t);
  5258. /*
  5259. * Presume the allocation and response will fit into an embedded
  5260. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5261. */
  5262. emb = LPFC_SLI4_MBX_EMBED;
  5263. req_len = emb_len;
  5264. if (req_len > emb_len) {
  5265. req_len = curr_blks * sizeof(uint16_t) +
  5266. sizeof(union lpfc_sli4_cfg_shdr) +
  5267. sizeof(uint32_t);
  5268. emb = LPFC_SLI4_MBX_NEMBED;
  5269. }
  5270. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5271. if (!mbox)
  5272. return -ENOMEM;
  5273. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5274. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5275. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5276. req_len, emb);
  5277. if (alloc_len < req_len) {
  5278. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5279. "2983 Allocated DMA memory size (x%x) is "
  5280. "less than the requested DMA memory "
  5281. "size (x%x)\n", alloc_len, req_len);
  5282. rc = -ENOMEM;
  5283. goto err_exit;
  5284. }
  5285. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5286. if (unlikely(rc)) {
  5287. rc = -EIO;
  5288. goto err_exit;
  5289. }
  5290. if (!phba->sli4_hba.intr_enable)
  5291. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5292. else {
  5293. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5294. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5295. }
  5296. if (unlikely(rc)) {
  5297. rc = -EIO;
  5298. goto err_exit;
  5299. }
  5300. /*
  5301. * Figure out where the response is located. Then get local pointers
  5302. * to the response data. The port does not guarantee to respond to
  5303. * all extents counts request so update the local variable with the
  5304. * allocated count from the port.
  5305. */
  5306. if (emb == LPFC_SLI4_MBX_EMBED) {
  5307. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5308. shdr = &rsrc_ext->header.cfg_shdr;
  5309. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5310. } else {
  5311. virtaddr = mbox->sge_array->addr[0];
  5312. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5313. shdr = &n_rsrc->cfg_shdr;
  5314. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5315. }
  5316. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5317. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5318. "2984 Failed to read allocated resources "
  5319. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5320. type,
  5321. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5322. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5323. rc = -EIO;
  5324. goto err_exit;
  5325. }
  5326. err_exit:
  5327. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5328. return rc;
  5329. }
  5330. /**
  5331. * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
  5332. * @phba: pointer to lpfc hba data structure.
  5333. *
  5334. * This routine walks the list of els buffers that have been allocated and
  5335. * repost them to the port by using SGL block post. This is needed after a
  5336. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5337. * of els buffer sgls which contains contiguous xris and uses the non-embedded
  5338. * SGL block post mailbox commands to post them to the port. For single els
  5339. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5340. * mailbox command for posting.
  5341. *
  5342. * Returns: 0 = success, non-zero failure.
  5343. **/
  5344. static int
  5345. lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
  5346. {
  5347. struct lpfc_sglq *sglq_entry = NULL;
  5348. struct lpfc_sglq *sglq_entry_next = NULL;
  5349. struct lpfc_sglq *sglq_entry_first = NULL;
  5350. int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
  5351. int last_xritag = NO_XRI;
  5352. LIST_HEAD(prep_sgl_list);
  5353. LIST_HEAD(blck_sgl_list);
  5354. LIST_HEAD(allc_sgl_list);
  5355. LIST_HEAD(post_sgl_list);
  5356. LIST_HEAD(free_sgl_list);
  5357. spin_lock(&phba->hbalock);
  5358. list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
  5359. spin_unlock(&phba->hbalock);
  5360. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  5361. &allc_sgl_list, list) {
  5362. list_del_init(&sglq_entry->list);
  5363. block_cnt++;
  5364. if ((last_xritag != NO_XRI) &&
  5365. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  5366. /* a hole in xri block, form a sgl posting block */
  5367. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  5368. post_cnt = block_cnt - 1;
  5369. /* prepare list for next posting block */
  5370. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5371. block_cnt = 1;
  5372. } else {
  5373. /* prepare list for next posting block */
  5374. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5375. /* enough sgls for non-embed sgl mbox command */
  5376. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  5377. list_splice_init(&prep_sgl_list,
  5378. &blck_sgl_list);
  5379. post_cnt = block_cnt;
  5380. block_cnt = 0;
  5381. }
  5382. }
  5383. num_posted++;
  5384. /* keep track of last sgl's xritag */
  5385. last_xritag = sglq_entry->sli4_xritag;
  5386. /* end of repost sgl list condition for els buffers */
  5387. if (num_posted == phba->sli4_hba.els_xri_cnt) {
  5388. if (post_cnt == 0) {
  5389. list_splice_init(&prep_sgl_list,
  5390. &blck_sgl_list);
  5391. post_cnt = block_cnt;
  5392. } else if (block_cnt == 1) {
  5393. status = lpfc_sli4_post_sgl(phba,
  5394. sglq_entry->phys, 0,
  5395. sglq_entry->sli4_xritag);
  5396. if (!status) {
  5397. /* successful, put sgl to posted list */
  5398. list_add_tail(&sglq_entry->list,
  5399. &post_sgl_list);
  5400. } else {
  5401. /* Failure, put sgl to free list */
  5402. lpfc_printf_log(phba, KERN_WARNING,
  5403. LOG_SLI,
  5404. "3159 Failed to post els "
  5405. "sgl, xritag:x%x\n",
  5406. sglq_entry->sli4_xritag);
  5407. list_add_tail(&sglq_entry->list,
  5408. &free_sgl_list);
  5409. spin_lock_irq(&phba->hbalock);
  5410. phba->sli4_hba.els_xri_cnt--;
  5411. spin_unlock_irq(&phba->hbalock);
  5412. }
  5413. }
  5414. }
  5415. /* continue until a nembed page worth of sgls */
  5416. if (post_cnt == 0)
  5417. continue;
  5418. /* post the els buffer list sgls as a block */
  5419. status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
  5420. post_cnt);
  5421. if (!status) {
  5422. /* success, put sgl list to posted sgl list */
  5423. list_splice_init(&blck_sgl_list, &post_sgl_list);
  5424. } else {
  5425. /* Failure, put sgl list to free sgl list */
  5426. sglq_entry_first = list_first_entry(&blck_sgl_list,
  5427. struct lpfc_sglq,
  5428. list);
  5429. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5430. "3160 Failed to post els sgl-list, "
  5431. "xritag:x%x-x%x\n",
  5432. sglq_entry_first->sli4_xritag,
  5433. (sglq_entry_first->sli4_xritag +
  5434. post_cnt - 1));
  5435. list_splice_init(&blck_sgl_list, &free_sgl_list);
  5436. spin_lock_irq(&phba->hbalock);
  5437. phba->sli4_hba.els_xri_cnt -= post_cnt;
  5438. spin_unlock_irq(&phba->hbalock);
  5439. }
  5440. /* don't reset xirtag due to hole in xri block */
  5441. if (block_cnt == 0)
  5442. last_xritag = NO_XRI;
  5443. /* reset els sgl post count for next round of posting */
  5444. post_cnt = 0;
  5445. }
  5446. /* free the els sgls failed to post */
  5447. lpfc_free_sgl_list(phba, &free_sgl_list);
  5448. /* push els sgls posted to the availble list */
  5449. if (!list_empty(&post_sgl_list)) {
  5450. spin_lock(&phba->hbalock);
  5451. list_splice_init(&post_sgl_list,
  5452. &phba->sli4_hba.lpfc_sgl_list);
  5453. spin_unlock(&phba->hbalock);
  5454. } else {
  5455. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5456. "3161 Failure to post els sgl to port.\n");
  5457. return -EIO;
  5458. }
  5459. return 0;
  5460. }
  5461. /**
  5462. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5463. * @phba: Pointer to HBA context object.
  5464. *
  5465. * This function is the main SLI4 device intialization PCI function. This
  5466. * function is called by the HBA intialization code, HBA reset code and
  5467. * HBA error attention handler code. Caller is not required to hold any
  5468. * locks.
  5469. **/
  5470. int
  5471. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5472. {
  5473. int rc;
  5474. LPFC_MBOXQ_t *mboxq;
  5475. struct lpfc_mqe *mqe;
  5476. uint8_t *vpd;
  5477. uint32_t vpd_size;
  5478. uint32_t ftr_rsp = 0;
  5479. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5480. struct lpfc_vport *vport = phba->pport;
  5481. struct lpfc_dmabuf *mp;
  5482. /* Perform a PCI function reset to start from clean */
  5483. rc = lpfc_pci_function_reset(phba);
  5484. if (unlikely(rc))
  5485. return -ENODEV;
  5486. /* Check the HBA Host Status Register for readyness */
  5487. rc = lpfc_sli4_post_status_check(phba);
  5488. if (unlikely(rc))
  5489. return -ENODEV;
  5490. else {
  5491. spin_lock_irq(&phba->hbalock);
  5492. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5493. spin_unlock_irq(&phba->hbalock);
  5494. }
  5495. /*
  5496. * Allocate a single mailbox container for initializing the
  5497. * port.
  5498. */
  5499. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5500. if (!mboxq)
  5501. return -ENOMEM;
  5502. /* Issue READ_REV to collect vpd and FW information. */
  5503. vpd_size = SLI4_PAGE_SIZE;
  5504. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5505. if (!vpd) {
  5506. rc = -ENOMEM;
  5507. goto out_free_mbox;
  5508. }
  5509. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5510. if (unlikely(rc)) {
  5511. kfree(vpd);
  5512. goto out_free_mbox;
  5513. }
  5514. mqe = &mboxq->u.mqe;
  5515. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5516. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5517. phba->hba_flag |= HBA_FCOE_MODE;
  5518. else
  5519. phba->hba_flag &= ~HBA_FCOE_MODE;
  5520. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5521. LPFC_DCBX_CEE_MODE)
  5522. phba->hba_flag |= HBA_FIP_SUPPORT;
  5523. else
  5524. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5525. phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
  5526. if (phba->sli_rev != LPFC_SLI_REV4) {
  5527. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5528. "0376 READ_REV Error. SLI Level %d "
  5529. "FCoE enabled %d\n",
  5530. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5531. rc = -EIO;
  5532. kfree(vpd);
  5533. goto out_free_mbox;
  5534. }
  5535. /*
  5536. * Continue initialization with default values even if driver failed
  5537. * to read FCoE param config regions, only read parameters if the
  5538. * board is FCoE
  5539. */
  5540. if (phba->hba_flag & HBA_FCOE_MODE &&
  5541. lpfc_sli4_read_fcoe_params(phba))
  5542. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5543. "2570 Failed to read FCoE parameters\n");
  5544. /*
  5545. * Retrieve sli4 device physical port name, failure of doing it
  5546. * is considered as non-fatal.
  5547. */
  5548. rc = lpfc_sli4_retrieve_pport_name(phba);
  5549. if (!rc)
  5550. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5551. "3080 Successful retrieving SLI4 device "
  5552. "physical port name: %s.\n", phba->Port);
  5553. /*
  5554. * Evaluate the read rev and vpd data. Populate the driver
  5555. * state with the results. If this routine fails, the failure
  5556. * is not fatal as the driver will use generic values.
  5557. */
  5558. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5559. if (unlikely(!rc)) {
  5560. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5561. "0377 Error %d parsing vpd. "
  5562. "Using defaults.\n", rc);
  5563. rc = 0;
  5564. }
  5565. kfree(vpd);
  5566. /* Save information as VPD data */
  5567. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5568. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5569. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5570. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5571. &mqe->un.read_rev);
  5572. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5573. &mqe->un.read_rev);
  5574. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5575. &mqe->un.read_rev);
  5576. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5577. &mqe->un.read_rev);
  5578. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5579. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5580. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5581. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5582. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5583. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5584. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5585. "(%d):0380 READ_REV Status x%x "
  5586. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5587. mboxq->vport ? mboxq->vport->vpi : 0,
  5588. bf_get(lpfc_mqe_status, mqe),
  5589. phba->vpd.rev.opFwName,
  5590. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5591. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5592. /*
  5593. * Discover the port's supported feature set and match it against the
  5594. * hosts requests.
  5595. */
  5596. lpfc_request_features(phba, mboxq);
  5597. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5598. if (unlikely(rc)) {
  5599. rc = -EIO;
  5600. goto out_free_mbox;
  5601. }
  5602. /*
  5603. * The port must support FCP initiator mode as this is the
  5604. * only mode running in the host.
  5605. */
  5606. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5607. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5608. "0378 No support for fcpi mode.\n");
  5609. ftr_rsp++;
  5610. }
  5611. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5612. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5613. else
  5614. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5615. /*
  5616. * If the port cannot support the host's requested features
  5617. * then turn off the global config parameters to disable the
  5618. * feature in the driver. This is not a fatal error.
  5619. */
  5620. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5621. if (phba->cfg_enable_bg) {
  5622. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5623. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5624. else
  5625. ftr_rsp++;
  5626. }
  5627. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5628. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5629. ftr_rsp++;
  5630. if (ftr_rsp) {
  5631. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5632. "0379 Feature Mismatch Data: x%08x %08x "
  5633. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5634. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5635. phba->cfg_enable_npiv, phba->max_vpi);
  5636. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5637. phba->cfg_enable_bg = 0;
  5638. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5639. phba->cfg_enable_npiv = 0;
  5640. }
  5641. /* These SLI3 features are assumed in SLI4 */
  5642. spin_lock_irq(&phba->hbalock);
  5643. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5644. spin_unlock_irq(&phba->hbalock);
  5645. /*
  5646. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5647. * calls depends on these resources to complete port setup.
  5648. */
  5649. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5650. if (rc) {
  5651. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5652. "2920 Failed to alloc Resource IDs "
  5653. "rc = x%x\n", rc);
  5654. goto out_free_mbox;
  5655. }
  5656. /* Read the port's service parameters. */
  5657. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5658. if (rc) {
  5659. phba->link_state = LPFC_HBA_ERROR;
  5660. rc = -ENOMEM;
  5661. goto out_free_mbox;
  5662. }
  5663. mboxq->vport = vport;
  5664. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5665. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5666. if (rc == MBX_SUCCESS) {
  5667. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5668. rc = 0;
  5669. }
  5670. /*
  5671. * This memory was allocated by the lpfc_read_sparam routine. Release
  5672. * it to the mbuf pool.
  5673. */
  5674. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5675. kfree(mp);
  5676. mboxq->context1 = NULL;
  5677. if (unlikely(rc)) {
  5678. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5679. "0382 READ_SPARAM command failed "
  5680. "status %d, mbxStatus x%x\n",
  5681. rc, bf_get(lpfc_mqe_status, mqe));
  5682. phba->link_state = LPFC_HBA_ERROR;
  5683. rc = -EIO;
  5684. goto out_free_mbox;
  5685. }
  5686. lpfc_update_vport_wwn(vport);
  5687. /* Update the fc_host data structures with new wwn. */
  5688. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5689. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5690. /* update host els and scsi xri-sgl sizes and mappings */
  5691. rc = lpfc_sli4_xri_sgl_update(phba);
  5692. if (unlikely(rc)) {
  5693. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5694. "1400 Failed to update xri-sgl size and "
  5695. "mapping: %d\n", rc);
  5696. goto out_free_mbox;
  5697. }
  5698. /* register the els sgl pool to the port */
  5699. rc = lpfc_sli4_repost_els_sgl_list(phba);
  5700. if (unlikely(rc)) {
  5701. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5702. "0582 Error %d during els sgl post "
  5703. "operation\n", rc);
  5704. rc = -ENODEV;
  5705. goto out_free_mbox;
  5706. }
  5707. /* register the allocated scsi sgl pool to the port */
  5708. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5709. if (unlikely(rc)) {
  5710. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5711. "0383 Error %d during scsi sgl post "
  5712. "operation\n", rc);
  5713. /* Some Scsi buffers were moved to the abort scsi list */
  5714. /* A pci function reset will repost them */
  5715. rc = -ENODEV;
  5716. goto out_free_mbox;
  5717. }
  5718. /* Post the rpi header region to the device. */
  5719. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5720. if (unlikely(rc)) {
  5721. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5722. "0393 Error %d during rpi post operation\n",
  5723. rc);
  5724. rc = -ENODEV;
  5725. goto out_free_mbox;
  5726. }
  5727. lpfc_sli4_node_prep(phba);
  5728. /* Create all the SLI4 queues */
  5729. rc = lpfc_sli4_queue_create(phba);
  5730. if (rc) {
  5731. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5732. "3089 Failed to allocate queues\n");
  5733. rc = -ENODEV;
  5734. goto out_stop_timers;
  5735. }
  5736. /* Set up all the queues to the device */
  5737. rc = lpfc_sli4_queue_setup(phba);
  5738. if (unlikely(rc)) {
  5739. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5740. "0381 Error %d during queue setup.\n ", rc);
  5741. goto out_destroy_queue;
  5742. }
  5743. /* Arm the CQs and then EQs on device */
  5744. lpfc_sli4_arm_cqeq_intr(phba);
  5745. /* Indicate device interrupt mode */
  5746. phba->sli4_hba.intr_enable = 1;
  5747. /* Allow asynchronous mailbox command to go through */
  5748. spin_lock_irq(&phba->hbalock);
  5749. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5750. spin_unlock_irq(&phba->hbalock);
  5751. /* Post receive buffers to the device */
  5752. lpfc_sli4_rb_setup(phba);
  5753. /* Reset HBA FCF states after HBA reset */
  5754. phba->fcf.fcf_flag = 0;
  5755. phba->fcf.current_rec.flag = 0;
  5756. /* Start the ELS watchdog timer */
  5757. mod_timer(&vport->els_tmofunc,
  5758. jiffies + HZ * (phba->fc_ratov * 2));
  5759. /* Start heart beat timer */
  5760. mod_timer(&phba->hb_tmofunc,
  5761. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5762. phba->hb_outstanding = 0;
  5763. phba->last_completion_time = jiffies;
  5764. /* Start error attention (ERATT) polling timer */
  5765. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5766. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5767. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5768. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5769. if (!rc) {
  5770. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5771. "2829 This device supports "
  5772. "Advanced Error Reporting (AER)\n");
  5773. spin_lock_irq(&phba->hbalock);
  5774. phba->hba_flag |= HBA_AER_ENABLED;
  5775. spin_unlock_irq(&phba->hbalock);
  5776. } else {
  5777. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5778. "2830 This device does not support "
  5779. "Advanced Error Reporting (AER)\n");
  5780. phba->cfg_aer_support = 0;
  5781. }
  5782. rc = 0;
  5783. }
  5784. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5785. /*
  5786. * The FC Port needs to register FCFI (index 0)
  5787. */
  5788. lpfc_reg_fcfi(phba, mboxq);
  5789. mboxq->vport = phba->pport;
  5790. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5791. if (rc != MBX_SUCCESS)
  5792. goto out_unset_queue;
  5793. rc = 0;
  5794. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5795. &mboxq->u.mqe.un.reg_fcfi);
  5796. /* Check if the port is configured to be disabled */
  5797. lpfc_sli_read_link_ste(phba);
  5798. }
  5799. /*
  5800. * The port is ready, set the host's link state to LINK_DOWN
  5801. * in preparation for link interrupts.
  5802. */
  5803. spin_lock_irq(&phba->hbalock);
  5804. phba->link_state = LPFC_LINK_DOWN;
  5805. spin_unlock_irq(&phba->hbalock);
  5806. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5807. (phba->hba_flag & LINK_DISABLED)) {
  5808. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5809. "3103 Adapter Link is disabled.\n");
  5810. lpfc_down_link(phba, mboxq);
  5811. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5812. if (rc != MBX_SUCCESS) {
  5813. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5814. "3104 Adapter failed to issue "
  5815. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5816. goto out_unset_queue;
  5817. }
  5818. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5819. /* don't perform init_link on SLI4 FC port loopback test */
  5820. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5821. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5822. if (rc)
  5823. goto out_unset_queue;
  5824. }
  5825. }
  5826. mempool_free(mboxq, phba->mbox_mem_pool);
  5827. return rc;
  5828. out_unset_queue:
  5829. /* Unset all the queues set up in this routine when error out */
  5830. lpfc_sli4_queue_unset(phba);
  5831. out_destroy_queue:
  5832. lpfc_sli4_queue_destroy(phba);
  5833. out_stop_timers:
  5834. lpfc_stop_hba_timers(phba);
  5835. out_free_mbox:
  5836. mempool_free(mboxq, phba->mbox_mem_pool);
  5837. return rc;
  5838. }
  5839. /**
  5840. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5841. * @ptr: context object - pointer to hba structure.
  5842. *
  5843. * This is the callback function for mailbox timer. The mailbox
  5844. * timer is armed when a new mailbox command is issued and the timer
  5845. * is deleted when the mailbox complete. The function is called by
  5846. * the kernel timer code when a mailbox does not complete within
  5847. * expected time. This function wakes up the worker thread to
  5848. * process the mailbox timeout and returns. All the processing is
  5849. * done by the worker thread function lpfc_mbox_timeout_handler.
  5850. **/
  5851. void
  5852. lpfc_mbox_timeout(unsigned long ptr)
  5853. {
  5854. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5855. unsigned long iflag;
  5856. uint32_t tmo_posted;
  5857. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5858. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5859. if (!tmo_posted)
  5860. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5861. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5862. if (!tmo_posted)
  5863. lpfc_worker_wake_up(phba);
  5864. return;
  5865. }
  5866. /**
  5867. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5868. * @phba: Pointer to HBA context object.
  5869. *
  5870. * This function is called from worker thread when a mailbox command times out.
  5871. * The caller is not required to hold any locks. This function will reset the
  5872. * HBA and recover all the pending commands.
  5873. **/
  5874. void
  5875. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5876. {
  5877. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5878. MAILBOX_t *mb = &pmbox->u.mb;
  5879. struct lpfc_sli *psli = &phba->sli;
  5880. struct lpfc_sli_ring *pring;
  5881. /* Check the pmbox pointer first. There is a race condition
  5882. * between the mbox timeout handler getting executed in the
  5883. * worklist and the mailbox actually completing. When this
  5884. * race condition occurs, the mbox_active will be NULL.
  5885. */
  5886. spin_lock_irq(&phba->hbalock);
  5887. if (pmbox == NULL) {
  5888. lpfc_printf_log(phba, KERN_WARNING,
  5889. LOG_MBOX | LOG_SLI,
  5890. "0353 Active Mailbox cleared - mailbox timeout "
  5891. "exiting\n");
  5892. spin_unlock_irq(&phba->hbalock);
  5893. return;
  5894. }
  5895. /* Mbox cmd <mbxCommand> timeout */
  5896. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5897. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5898. mb->mbxCommand,
  5899. phba->pport->port_state,
  5900. phba->sli.sli_flag,
  5901. phba->sli.mbox_active);
  5902. spin_unlock_irq(&phba->hbalock);
  5903. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5904. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5905. * it to fail all outstanding SCSI IO.
  5906. */
  5907. spin_lock_irq(&phba->pport->work_port_lock);
  5908. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5909. spin_unlock_irq(&phba->pport->work_port_lock);
  5910. spin_lock_irq(&phba->hbalock);
  5911. phba->link_state = LPFC_LINK_UNKNOWN;
  5912. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5913. spin_unlock_irq(&phba->hbalock);
  5914. pring = &psli->ring[psli->fcp_ring];
  5915. lpfc_sli_abort_iocb_ring(phba, pring);
  5916. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5917. "0345 Resetting board due to mailbox timeout\n");
  5918. /* Reset the HBA device */
  5919. lpfc_reset_hba(phba);
  5920. }
  5921. /**
  5922. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  5923. * @phba: Pointer to HBA context object.
  5924. * @pmbox: Pointer to mailbox object.
  5925. * @flag: Flag indicating how the mailbox need to be processed.
  5926. *
  5927. * This function is called by discovery code and HBA management code
  5928. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  5929. * function gets the hbalock to protect the data structures.
  5930. * The mailbox command can be submitted in polling mode, in which case
  5931. * this function will wait in a polling loop for the completion of the
  5932. * mailbox.
  5933. * If the mailbox is submitted in no_wait mode (not polling) the
  5934. * function will submit the command and returns immediately without waiting
  5935. * for the mailbox completion. The no_wait is supported only when HBA
  5936. * is in SLI2/SLI3 mode - interrupts are enabled.
  5937. * The SLI interface allows only one mailbox pending at a time. If the
  5938. * mailbox is issued in polling mode and there is already a mailbox
  5939. * pending, then the function will return an error. If the mailbox is issued
  5940. * in NO_WAIT mode and there is a mailbox pending already, the function
  5941. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  5942. * The sli layer owns the mailbox object until the completion of mailbox
  5943. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  5944. * return codes the caller owns the mailbox command after the return of
  5945. * the function.
  5946. **/
  5947. static int
  5948. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  5949. uint32_t flag)
  5950. {
  5951. MAILBOX_t *mb;
  5952. struct lpfc_sli *psli = &phba->sli;
  5953. uint32_t status, evtctr;
  5954. uint32_t ha_copy, hc_copy;
  5955. int i;
  5956. unsigned long timeout;
  5957. unsigned long drvr_flag = 0;
  5958. uint32_t word0, ldata;
  5959. void __iomem *to_slim;
  5960. int processing_queue = 0;
  5961. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5962. if (!pmbox) {
  5963. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5964. /* processing mbox queue from intr_handler */
  5965. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5966. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5967. return MBX_SUCCESS;
  5968. }
  5969. processing_queue = 1;
  5970. pmbox = lpfc_mbox_get(phba);
  5971. if (!pmbox) {
  5972. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5973. return MBX_SUCCESS;
  5974. }
  5975. }
  5976. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  5977. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  5978. if(!pmbox->vport) {
  5979. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5980. lpfc_printf_log(phba, KERN_ERR,
  5981. LOG_MBOX | LOG_VPORT,
  5982. "1806 Mbox x%x failed. No vport\n",
  5983. pmbox->u.mb.mbxCommand);
  5984. dump_stack();
  5985. goto out_not_finished;
  5986. }
  5987. }
  5988. /* If the PCI channel is in offline state, do not post mbox. */
  5989. if (unlikely(pci_channel_offline(phba->pcidev))) {
  5990. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5991. goto out_not_finished;
  5992. }
  5993. /* If HBA has a deferred error attention, fail the iocb. */
  5994. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  5995. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5996. goto out_not_finished;
  5997. }
  5998. psli = &phba->sli;
  5999. mb = &pmbox->u.mb;
  6000. status = MBX_SUCCESS;
  6001. if (phba->link_state == LPFC_HBA_ERROR) {
  6002. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6003. /* Mbox command <mbxCommand> cannot issue */
  6004. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6005. "(%d):0311 Mailbox command x%x cannot "
  6006. "issue Data: x%x x%x\n",
  6007. pmbox->vport ? pmbox->vport->vpi : 0,
  6008. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6009. goto out_not_finished;
  6010. }
  6011. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6012. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6013. !(hc_copy & HC_MBINT_ENA)) {
  6014. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6015. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6016. "(%d):2528 Mailbox command x%x cannot "
  6017. "issue Data: x%x x%x\n",
  6018. pmbox->vport ? pmbox->vport->vpi : 0,
  6019. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6020. goto out_not_finished;
  6021. }
  6022. }
  6023. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6024. /* Polling for a mbox command when another one is already active
  6025. * is not allowed in SLI. Also, the driver must have established
  6026. * SLI2 mode to queue and process multiple mbox commands.
  6027. */
  6028. if (flag & MBX_POLL) {
  6029. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6030. /* Mbox command <mbxCommand> cannot issue */
  6031. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6032. "(%d):2529 Mailbox command x%x "
  6033. "cannot issue Data: x%x x%x\n",
  6034. pmbox->vport ? pmbox->vport->vpi : 0,
  6035. pmbox->u.mb.mbxCommand,
  6036. psli->sli_flag, flag);
  6037. goto out_not_finished;
  6038. }
  6039. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  6040. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6041. /* Mbox command <mbxCommand> cannot issue */
  6042. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6043. "(%d):2530 Mailbox command x%x "
  6044. "cannot issue Data: x%x x%x\n",
  6045. pmbox->vport ? pmbox->vport->vpi : 0,
  6046. pmbox->u.mb.mbxCommand,
  6047. psli->sli_flag, flag);
  6048. goto out_not_finished;
  6049. }
  6050. /* Another mailbox command is still being processed, queue this
  6051. * command to be processed later.
  6052. */
  6053. lpfc_mbox_put(phba, pmbox);
  6054. /* Mbox cmd issue - BUSY */
  6055. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6056. "(%d):0308 Mbox cmd issue - BUSY Data: "
  6057. "x%x x%x x%x x%x\n",
  6058. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  6059. mb->mbxCommand, phba->pport->port_state,
  6060. psli->sli_flag, flag);
  6061. psli->slistat.mbox_busy++;
  6062. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6063. if (pmbox->vport) {
  6064. lpfc_debugfs_disc_trc(pmbox->vport,
  6065. LPFC_DISC_TRC_MBOX_VPORT,
  6066. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  6067. (uint32_t)mb->mbxCommand,
  6068. mb->un.varWords[0], mb->un.varWords[1]);
  6069. }
  6070. else {
  6071. lpfc_debugfs_disc_trc(phba->pport,
  6072. LPFC_DISC_TRC_MBOX,
  6073. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  6074. (uint32_t)mb->mbxCommand,
  6075. mb->un.varWords[0], mb->un.varWords[1]);
  6076. }
  6077. return MBX_BUSY;
  6078. }
  6079. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6080. /* If we are not polling, we MUST be in SLI2 mode */
  6081. if (flag != MBX_POLL) {
  6082. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  6083. (mb->mbxCommand != MBX_KILL_BOARD)) {
  6084. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6085. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6086. /* Mbox command <mbxCommand> cannot issue */
  6087. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6088. "(%d):2531 Mailbox command x%x "
  6089. "cannot issue Data: x%x x%x\n",
  6090. pmbox->vport ? pmbox->vport->vpi : 0,
  6091. pmbox->u.mb.mbxCommand,
  6092. psli->sli_flag, flag);
  6093. goto out_not_finished;
  6094. }
  6095. /* timeout active mbox command */
  6096. mod_timer(&psli->mbox_tmo, (jiffies +
  6097. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  6098. }
  6099. /* Mailbox cmd <cmd> issue */
  6100. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6101. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  6102. "x%x\n",
  6103. pmbox->vport ? pmbox->vport->vpi : 0,
  6104. mb->mbxCommand, phba->pport->port_state,
  6105. psli->sli_flag, flag);
  6106. if (mb->mbxCommand != MBX_HEARTBEAT) {
  6107. if (pmbox->vport) {
  6108. lpfc_debugfs_disc_trc(pmbox->vport,
  6109. LPFC_DISC_TRC_MBOX_VPORT,
  6110. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6111. (uint32_t)mb->mbxCommand,
  6112. mb->un.varWords[0], mb->un.varWords[1]);
  6113. }
  6114. else {
  6115. lpfc_debugfs_disc_trc(phba->pport,
  6116. LPFC_DISC_TRC_MBOX,
  6117. "MBOX Send: cmd:x%x mb:x%x x%x",
  6118. (uint32_t)mb->mbxCommand,
  6119. mb->un.varWords[0], mb->un.varWords[1]);
  6120. }
  6121. }
  6122. psli->slistat.mbox_cmd++;
  6123. evtctr = psli->slistat.mbox_event;
  6124. /* next set own bit for the adapter and copy over command word */
  6125. mb->mbxOwner = OWN_CHIP;
  6126. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6127. /* Populate mbox extension offset word. */
  6128. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6129. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6130. = (uint8_t *)phba->mbox_ext
  6131. - (uint8_t *)phba->mbox;
  6132. }
  6133. /* Copy the mailbox extension data */
  6134. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6135. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6136. (uint8_t *)phba->mbox_ext,
  6137. pmbox->in_ext_byte_len);
  6138. }
  6139. /* Copy command data to host SLIM area */
  6140. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6141. } else {
  6142. /* Populate mbox extension offset word. */
  6143. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6144. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6145. = MAILBOX_HBA_EXT_OFFSET;
  6146. /* Copy the mailbox extension data */
  6147. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6148. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6149. MAILBOX_HBA_EXT_OFFSET,
  6150. pmbox->context2, pmbox->in_ext_byte_len);
  6151. }
  6152. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6153. /* copy command data into host mbox for cmpl */
  6154. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6155. }
  6156. /* First copy mbox command data to HBA SLIM, skip past first
  6157. word */
  6158. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6159. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  6160. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6161. /* Next copy over first word, with mbxOwner set */
  6162. ldata = *((uint32_t *)mb);
  6163. to_slim = phba->MBslimaddr;
  6164. writel(ldata, to_slim);
  6165. readl(to_slim); /* flush */
  6166. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6167. /* switch over to host mailbox */
  6168. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6169. }
  6170. }
  6171. wmb();
  6172. switch (flag) {
  6173. case MBX_NOWAIT:
  6174. /* Set up reference to mailbox command */
  6175. psli->mbox_active = pmbox;
  6176. /* Interrupt board to do it */
  6177. writel(CA_MBATT, phba->CAregaddr);
  6178. readl(phba->CAregaddr); /* flush */
  6179. /* Don't wait for it to finish, just return */
  6180. break;
  6181. case MBX_POLL:
  6182. /* Set up null reference to mailbox command */
  6183. psli->mbox_active = NULL;
  6184. /* Interrupt board to do it */
  6185. writel(CA_MBATT, phba->CAregaddr);
  6186. readl(phba->CAregaddr); /* flush */
  6187. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6188. /* First read mbox status word */
  6189. word0 = *((uint32_t *)phba->mbox);
  6190. word0 = le32_to_cpu(word0);
  6191. } else {
  6192. /* First read mbox status word */
  6193. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6194. spin_unlock_irqrestore(&phba->hbalock,
  6195. drvr_flag);
  6196. goto out_not_finished;
  6197. }
  6198. }
  6199. /* Read the HBA Host Attention Register */
  6200. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6201. spin_unlock_irqrestore(&phba->hbalock,
  6202. drvr_flag);
  6203. goto out_not_finished;
  6204. }
  6205. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6206. 1000) + jiffies;
  6207. i = 0;
  6208. /* Wait for command to complete */
  6209. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6210. (!(ha_copy & HA_MBATT) &&
  6211. (phba->link_state > LPFC_WARM_START))) {
  6212. if (time_after(jiffies, timeout)) {
  6213. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6214. spin_unlock_irqrestore(&phba->hbalock,
  6215. drvr_flag);
  6216. goto out_not_finished;
  6217. }
  6218. /* Check if we took a mbox interrupt while we were
  6219. polling */
  6220. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6221. && (evtctr != psli->slistat.mbox_event))
  6222. break;
  6223. if (i++ > 10) {
  6224. spin_unlock_irqrestore(&phba->hbalock,
  6225. drvr_flag);
  6226. msleep(1);
  6227. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6228. }
  6229. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6230. /* First copy command data */
  6231. word0 = *((uint32_t *)phba->mbox);
  6232. word0 = le32_to_cpu(word0);
  6233. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6234. MAILBOX_t *slimmb;
  6235. uint32_t slimword0;
  6236. /* Check real SLIM for any errors */
  6237. slimword0 = readl(phba->MBslimaddr);
  6238. slimmb = (MAILBOX_t *) & slimword0;
  6239. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6240. && slimmb->mbxStatus) {
  6241. psli->sli_flag &=
  6242. ~LPFC_SLI_ACTIVE;
  6243. word0 = slimword0;
  6244. }
  6245. }
  6246. } else {
  6247. /* First copy command data */
  6248. word0 = readl(phba->MBslimaddr);
  6249. }
  6250. /* Read the HBA Host Attention Register */
  6251. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6252. spin_unlock_irqrestore(&phba->hbalock,
  6253. drvr_flag);
  6254. goto out_not_finished;
  6255. }
  6256. }
  6257. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6258. /* copy results back to user */
  6259. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  6260. /* Copy the mailbox extension data */
  6261. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6262. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6263. pmbox->context2,
  6264. pmbox->out_ext_byte_len);
  6265. }
  6266. } else {
  6267. /* First copy command data */
  6268. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  6269. MAILBOX_CMD_SIZE);
  6270. /* Copy the mailbox extension data */
  6271. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6272. lpfc_memcpy_from_slim(pmbox->context2,
  6273. phba->MBslimaddr +
  6274. MAILBOX_HBA_EXT_OFFSET,
  6275. pmbox->out_ext_byte_len);
  6276. }
  6277. }
  6278. writel(HA_MBATT, phba->HAregaddr);
  6279. readl(phba->HAregaddr); /* flush */
  6280. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6281. status = mb->mbxStatus;
  6282. }
  6283. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6284. return status;
  6285. out_not_finished:
  6286. if (processing_queue) {
  6287. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6288. lpfc_mbox_cmpl_put(phba, pmbox);
  6289. }
  6290. return MBX_NOT_FINISHED;
  6291. }
  6292. /**
  6293. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6294. * @phba: Pointer to HBA context object.
  6295. *
  6296. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6297. * the driver internal pending mailbox queue. It will then try to wait out the
  6298. * possible outstanding mailbox command before return.
  6299. *
  6300. * Returns:
  6301. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6302. * the outstanding mailbox command timed out.
  6303. **/
  6304. static int
  6305. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6306. {
  6307. struct lpfc_sli *psli = &phba->sli;
  6308. int rc = 0;
  6309. unsigned long timeout = 0;
  6310. /* Mark the asynchronous mailbox command posting as blocked */
  6311. spin_lock_irq(&phba->hbalock);
  6312. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6313. /* Determine how long we might wait for the active mailbox
  6314. * command to be gracefully completed by firmware.
  6315. */
  6316. if (phba->sli.mbox_active)
  6317. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6318. phba->sli.mbox_active) *
  6319. 1000) + jiffies;
  6320. spin_unlock_irq(&phba->hbalock);
  6321. /* Wait for the outstnading mailbox command to complete */
  6322. while (phba->sli.mbox_active) {
  6323. /* Check active mailbox complete status every 2ms */
  6324. msleep(2);
  6325. if (time_after(jiffies, timeout)) {
  6326. /* Timeout, marked the outstanding cmd not complete */
  6327. rc = 1;
  6328. break;
  6329. }
  6330. }
  6331. /* Can not cleanly block async mailbox command, fails it */
  6332. if (rc) {
  6333. spin_lock_irq(&phba->hbalock);
  6334. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6335. spin_unlock_irq(&phba->hbalock);
  6336. }
  6337. return rc;
  6338. }
  6339. /**
  6340. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6341. * @phba: Pointer to HBA context object.
  6342. *
  6343. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6344. * commands from the driver internal pending mailbox queue. It makes sure
  6345. * that there is no outstanding mailbox command before resuming posting
  6346. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6347. * mailbox command, it will try to wait it out before resuming asynchronous
  6348. * mailbox command posting.
  6349. **/
  6350. static void
  6351. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6352. {
  6353. struct lpfc_sli *psli = &phba->sli;
  6354. spin_lock_irq(&phba->hbalock);
  6355. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6356. /* Asynchronous mailbox posting is not blocked, do nothing */
  6357. spin_unlock_irq(&phba->hbalock);
  6358. return;
  6359. }
  6360. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6361. * successful or timeout, after timing-out the outstanding mailbox
  6362. * command shall always be removed, so just unblock posting async
  6363. * mailbox command and resume
  6364. */
  6365. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6366. spin_unlock_irq(&phba->hbalock);
  6367. /* wake up worker thread to post asynchronlous mailbox command */
  6368. lpfc_worker_wake_up(phba);
  6369. }
  6370. /**
  6371. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6372. * @phba: Pointer to HBA context object.
  6373. * @mboxq: Pointer to mailbox object.
  6374. *
  6375. * The function posts a mailbox to the port. The mailbox is expected
  6376. * to be comletely filled in and ready for the port to operate on it.
  6377. * This routine executes a synchronous completion operation on the
  6378. * mailbox by polling for its completion.
  6379. *
  6380. * The caller must not be holding any locks when calling this routine.
  6381. *
  6382. * Returns:
  6383. * MBX_SUCCESS - mailbox posted successfully
  6384. * Any of the MBX error values.
  6385. **/
  6386. static int
  6387. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6388. {
  6389. int rc = MBX_SUCCESS;
  6390. unsigned long iflag;
  6391. uint32_t db_ready;
  6392. uint32_t mcqe_status;
  6393. uint32_t mbx_cmnd;
  6394. unsigned long timeout;
  6395. struct lpfc_sli *psli = &phba->sli;
  6396. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6397. struct lpfc_bmbx_create *mbox_rgn;
  6398. struct dma_address *dma_address;
  6399. struct lpfc_register bmbx_reg;
  6400. /*
  6401. * Only one mailbox can be active to the bootstrap mailbox region
  6402. * at a time and there is no queueing provided.
  6403. */
  6404. spin_lock_irqsave(&phba->hbalock, iflag);
  6405. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6406. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6407. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6408. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6409. "cannot issue Data: x%x x%x\n",
  6410. mboxq->vport ? mboxq->vport->vpi : 0,
  6411. mboxq->u.mb.mbxCommand,
  6412. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6413. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6414. psli->sli_flag, MBX_POLL);
  6415. return MBXERR_ERROR;
  6416. }
  6417. /* The server grabs the token and owns it until release */
  6418. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6419. phba->sli.mbox_active = mboxq;
  6420. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6421. /*
  6422. * Initialize the bootstrap memory region to avoid stale data areas
  6423. * in the mailbox post. Then copy the caller's mailbox contents to
  6424. * the bmbx mailbox region.
  6425. */
  6426. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6427. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6428. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6429. sizeof(struct lpfc_mqe));
  6430. /* Post the high mailbox dma address to the port and wait for ready. */
  6431. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6432. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6433. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6434. * 1000) + jiffies;
  6435. do {
  6436. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6437. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6438. if (!db_ready)
  6439. msleep(2);
  6440. if (time_after(jiffies, timeout)) {
  6441. rc = MBXERR_ERROR;
  6442. goto exit;
  6443. }
  6444. } while (!db_ready);
  6445. /* Post the low mailbox dma address to the port. */
  6446. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6447. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6448. * 1000) + jiffies;
  6449. do {
  6450. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6451. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6452. if (!db_ready)
  6453. msleep(2);
  6454. if (time_after(jiffies, timeout)) {
  6455. rc = MBXERR_ERROR;
  6456. goto exit;
  6457. }
  6458. } while (!db_ready);
  6459. /*
  6460. * Read the CQ to ensure the mailbox has completed.
  6461. * If so, update the mailbox status so that the upper layers
  6462. * can complete the request normally.
  6463. */
  6464. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6465. sizeof(struct lpfc_mqe));
  6466. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6467. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6468. sizeof(struct lpfc_mcqe));
  6469. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6470. /*
  6471. * When the CQE status indicates a failure and the mailbox status
  6472. * indicates success then copy the CQE status into the mailbox status
  6473. * (and prefix it with x4000).
  6474. */
  6475. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6476. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6477. bf_set(lpfc_mqe_status, mb,
  6478. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6479. rc = MBXERR_ERROR;
  6480. } else
  6481. lpfc_sli4_swap_str(phba, mboxq);
  6482. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6483. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6484. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6485. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6486. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6487. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6488. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6489. bf_get(lpfc_mqe_status, mb),
  6490. mb->un.mb_words[0], mb->un.mb_words[1],
  6491. mb->un.mb_words[2], mb->un.mb_words[3],
  6492. mb->un.mb_words[4], mb->un.mb_words[5],
  6493. mb->un.mb_words[6], mb->un.mb_words[7],
  6494. mb->un.mb_words[8], mb->un.mb_words[9],
  6495. mb->un.mb_words[10], mb->un.mb_words[11],
  6496. mb->un.mb_words[12], mboxq->mcqe.word0,
  6497. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6498. mboxq->mcqe.trailer);
  6499. exit:
  6500. /* We are holding the token, no needed for lock when release */
  6501. spin_lock_irqsave(&phba->hbalock, iflag);
  6502. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6503. phba->sli.mbox_active = NULL;
  6504. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6505. return rc;
  6506. }
  6507. /**
  6508. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6509. * @phba: Pointer to HBA context object.
  6510. * @pmbox: Pointer to mailbox object.
  6511. * @flag: Flag indicating how the mailbox need to be processed.
  6512. *
  6513. * This function is called by discovery code and HBA management code to submit
  6514. * a mailbox command to firmware with SLI-4 interface spec.
  6515. *
  6516. * Return codes the caller owns the mailbox command after the return of the
  6517. * function.
  6518. **/
  6519. static int
  6520. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6521. uint32_t flag)
  6522. {
  6523. struct lpfc_sli *psli = &phba->sli;
  6524. unsigned long iflags;
  6525. int rc;
  6526. /* dump from issue mailbox command if setup */
  6527. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6528. rc = lpfc_mbox_dev_check(phba);
  6529. if (unlikely(rc)) {
  6530. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6531. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6532. "cannot issue Data: x%x x%x\n",
  6533. mboxq->vport ? mboxq->vport->vpi : 0,
  6534. mboxq->u.mb.mbxCommand,
  6535. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6536. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6537. psli->sli_flag, flag);
  6538. goto out_not_finished;
  6539. }
  6540. /* Detect polling mode and jump to a handler */
  6541. if (!phba->sli4_hba.intr_enable) {
  6542. if (flag == MBX_POLL)
  6543. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6544. else
  6545. rc = -EIO;
  6546. if (rc != MBX_SUCCESS)
  6547. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6548. "(%d):2541 Mailbox command x%x "
  6549. "(x%x/x%x) failure: "
  6550. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6551. "Data: x%x x%x\n,",
  6552. mboxq->vport ? mboxq->vport->vpi : 0,
  6553. mboxq->u.mb.mbxCommand,
  6554. lpfc_sli_config_mbox_subsys_get(phba,
  6555. mboxq),
  6556. lpfc_sli_config_mbox_opcode_get(phba,
  6557. mboxq),
  6558. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6559. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6560. bf_get(lpfc_mcqe_ext_status,
  6561. &mboxq->mcqe),
  6562. psli->sli_flag, flag);
  6563. return rc;
  6564. } else if (flag == MBX_POLL) {
  6565. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6566. "(%d):2542 Try to issue mailbox command "
  6567. "x%x (x%x/x%x) synchronously ahead of async"
  6568. "mailbox command queue: x%x x%x\n",
  6569. mboxq->vport ? mboxq->vport->vpi : 0,
  6570. mboxq->u.mb.mbxCommand,
  6571. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6572. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6573. psli->sli_flag, flag);
  6574. /* Try to block the asynchronous mailbox posting */
  6575. rc = lpfc_sli4_async_mbox_block(phba);
  6576. if (!rc) {
  6577. /* Successfully blocked, now issue sync mbox cmd */
  6578. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6579. if (rc != MBX_SUCCESS)
  6580. lpfc_printf_log(phba, KERN_WARNING,
  6581. LOG_MBOX | LOG_SLI,
  6582. "(%d):2597 Sync Mailbox command "
  6583. "x%x (x%x/x%x) failure: "
  6584. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6585. "Data: x%x x%x\n,",
  6586. mboxq->vport ? mboxq->vport->vpi : 0,
  6587. mboxq->u.mb.mbxCommand,
  6588. lpfc_sli_config_mbox_subsys_get(phba,
  6589. mboxq),
  6590. lpfc_sli_config_mbox_opcode_get(phba,
  6591. mboxq),
  6592. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6593. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6594. bf_get(lpfc_mcqe_ext_status,
  6595. &mboxq->mcqe),
  6596. psli->sli_flag, flag);
  6597. /* Unblock the async mailbox posting afterward */
  6598. lpfc_sli4_async_mbox_unblock(phba);
  6599. }
  6600. return rc;
  6601. }
  6602. /* Now, interrupt mode asynchrous mailbox command */
  6603. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6604. if (rc) {
  6605. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6606. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6607. "cannot issue Data: x%x x%x\n",
  6608. mboxq->vport ? mboxq->vport->vpi : 0,
  6609. mboxq->u.mb.mbxCommand,
  6610. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6611. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6612. psli->sli_flag, flag);
  6613. goto out_not_finished;
  6614. }
  6615. /* Put the mailbox command to the driver internal FIFO */
  6616. psli->slistat.mbox_busy++;
  6617. spin_lock_irqsave(&phba->hbalock, iflags);
  6618. lpfc_mbox_put(phba, mboxq);
  6619. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6620. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6621. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6622. "x%x (x%x/x%x) x%x x%x x%x\n",
  6623. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6624. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6625. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6626. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6627. phba->pport->port_state,
  6628. psli->sli_flag, MBX_NOWAIT);
  6629. /* Wake up worker thread to transport mailbox command from head */
  6630. lpfc_worker_wake_up(phba);
  6631. return MBX_BUSY;
  6632. out_not_finished:
  6633. return MBX_NOT_FINISHED;
  6634. }
  6635. /**
  6636. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6637. * @phba: Pointer to HBA context object.
  6638. *
  6639. * This function is called by worker thread to send a mailbox command to
  6640. * SLI4 HBA firmware.
  6641. *
  6642. **/
  6643. int
  6644. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6645. {
  6646. struct lpfc_sli *psli = &phba->sli;
  6647. LPFC_MBOXQ_t *mboxq;
  6648. int rc = MBX_SUCCESS;
  6649. unsigned long iflags;
  6650. struct lpfc_mqe *mqe;
  6651. uint32_t mbx_cmnd;
  6652. /* Check interrupt mode before post async mailbox command */
  6653. if (unlikely(!phba->sli4_hba.intr_enable))
  6654. return MBX_NOT_FINISHED;
  6655. /* Check for mailbox command service token */
  6656. spin_lock_irqsave(&phba->hbalock, iflags);
  6657. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6658. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6659. return MBX_NOT_FINISHED;
  6660. }
  6661. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6662. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6663. return MBX_NOT_FINISHED;
  6664. }
  6665. if (unlikely(phba->sli.mbox_active)) {
  6666. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6667. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6668. "0384 There is pending active mailbox cmd\n");
  6669. return MBX_NOT_FINISHED;
  6670. }
  6671. /* Take the mailbox command service token */
  6672. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6673. /* Get the next mailbox command from head of queue */
  6674. mboxq = lpfc_mbox_get(phba);
  6675. /* If no more mailbox command waiting for post, we're done */
  6676. if (!mboxq) {
  6677. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6678. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6679. return MBX_SUCCESS;
  6680. }
  6681. phba->sli.mbox_active = mboxq;
  6682. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6683. /* Check device readiness for posting mailbox command */
  6684. rc = lpfc_mbox_dev_check(phba);
  6685. if (unlikely(rc))
  6686. /* Driver clean routine will clean up pending mailbox */
  6687. goto out_not_finished;
  6688. /* Prepare the mbox command to be posted */
  6689. mqe = &mboxq->u.mqe;
  6690. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6691. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6692. mod_timer(&psli->mbox_tmo, (jiffies +
  6693. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6694. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6695. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6696. "x%x x%x\n",
  6697. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6698. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6699. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6700. phba->pport->port_state, psli->sli_flag);
  6701. if (mbx_cmnd != MBX_HEARTBEAT) {
  6702. if (mboxq->vport) {
  6703. lpfc_debugfs_disc_trc(mboxq->vport,
  6704. LPFC_DISC_TRC_MBOX_VPORT,
  6705. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6706. mbx_cmnd, mqe->un.mb_words[0],
  6707. mqe->un.mb_words[1]);
  6708. } else {
  6709. lpfc_debugfs_disc_trc(phba->pport,
  6710. LPFC_DISC_TRC_MBOX,
  6711. "MBOX Send: cmd:x%x mb:x%x x%x",
  6712. mbx_cmnd, mqe->un.mb_words[0],
  6713. mqe->un.mb_words[1]);
  6714. }
  6715. }
  6716. psli->slistat.mbox_cmd++;
  6717. /* Post the mailbox command to the port */
  6718. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6719. if (rc != MBX_SUCCESS) {
  6720. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6721. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6722. "cannot issue Data: x%x x%x\n",
  6723. mboxq->vport ? mboxq->vport->vpi : 0,
  6724. mboxq->u.mb.mbxCommand,
  6725. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6726. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6727. psli->sli_flag, MBX_NOWAIT);
  6728. goto out_not_finished;
  6729. }
  6730. return rc;
  6731. out_not_finished:
  6732. spin_lock_irqsave(&phba->hbalock, iflags);
  6733. if (phba->sli.mbox_active) {
  6734. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6735. __lpfc_mbox_cmpl_put(phba, mboxq);
  6736. /* Release the token */
  6737. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6738. phba->sli.mbox_active = NULL;
  6739. }
  6740. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6741. return MBX_NOT_FINISHED;
  6742. }
  6743. /**
  6744. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6745. * @phba: Pointer to HBA context object.
  6746. * @pmbox: Pointer to mailbox object.
  6747. * @flag: Flag indicating how the mailbox need to be processed.
  6748. *
  6749. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6750. * the API jump table function pointer from the lpfc_hba struct.
  6751. *
  6752. * Return codes the caller owns the mailbox command after the return of the
  6753. * function.
  6754. **/
  6755. int
  6756. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6757. {
  6758. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6759. }
  6760. /**
  6761. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6762. * @phba: The hba struct for which this call is being executed.
  6763. * @dev_grp: The HBA PCI-Device group number.
  6764. *
  6765. * This routine sets up the mbox interface API function jump table in @phba
  6766. * struct.
  6767. * Returns: 0 - success, -ENODEV - failure.
  6768. **/
  6769. int
  6770. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6771. {
  6772. switch (dev_grp) {
  6773. case LPFC_PCI_DEV_LP:
  6774. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6775. phba->lpfc_sli_handle_slow_ring_event =
  6776. lpfc_sli_handle_slow_ring_event_s3;
  6777. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6778. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6779. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6780. break;
  6781. case LPFC_PCI_DEV_OC:
  6782. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6783. phba->lpfc_sli_handle_slow_ring_event =
  6784. lpfc_sli_handle_slow_ring_event_s4;
  6785. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6786. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6787. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6788. break;
  6789. default:
  6790. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6791. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6792. dev_grp);
  6793. return -ENODEV;
  6794. break;
  6795. }
  6796. return 0;
  6797. }
  6798. /**
  6799. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6800. * @phba: Pointer to HBA context object.
  6801. * @pring: Pointer to driver SLI ring object.
  6802. * @piocb: Pointer to address of newly added command iocb.
  6803. *
  6804. * This function is called with hbalock held to add a command
  6805. * iocb to the txq when SLI layer cannot submit the command iocb
  6806. * to the ring.
  6807. **/
  6808. void
  6809. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6810. struct lpfc_iocbq *piocb)
  6811. {
  6812. /* Insert the caller's iocb in the txq tail for later processing. */
  6813. list_add_tail(&piocb->list, &pring->txq);
  6814. pring->txq_cnt++;
  6815. }
  6816. /**
  6817. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6818. * @phba: Pointer to HBA context object.
  6819. * @pring: Pointer to driver SLI ring object.
  6820. * @piocb: Pointer to address of newly added command iocb.
  6821. *
  6822. * This function is called with hbalock held before a new
  6823. * iocb is submitted to the firmware. This function checks
  6824. * txq to flush the iocbs in txq to Firmware before
  6825. * submitting new iocbs to the Firmware.
  6826. * If there are iocbs in the txq which need to be submitted
  6827. * to firmware, lpfc_sli_next_iocb returns the first element
  6828. * of the txq after dequeuing it from txq.
  6829. * If there is no iocb in the txq then the function will return
  6830. * *piocb and *piocb is set to NULL. Caller needs to check
  6831. * *piocb to find if there are more commands in the txq.
  6832. **/
  6833. static struct lpfc_iocbq *
  6834. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6835. struct lpfc_iocbq **piocb)
  6836. {
  6837. struct lpfc_iocbq * nextiocb;
  6838. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6839. if (!nextiocb) {
  6840. nextiocb = *piocb;
  6841. *piocb = NULL;
  6842. }
  6843. return nextiocb;
  6844. }
  6845. /**
  6846. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6847. * @phba: Pointer to HBA context object.
  6848. * @ring_number: SLI ring number to issue iocb on.
  6849. * @piocb: Pointer to command iocb.
  6850. * @flag: Flag indicating if this command can be put into txq.
  6851. *
  6852. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6853. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6854. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6855. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6856. * this function allows only iocbs for posting buffers. This function finds
  6857. * next available slot in the command ring and posts the command to the
  6858. * available slot and writes the port attention register to request HBA start
  6859. * processing new iocb. If there is no slot available in the ring and
  6860. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6861. * the function returns IOCB_BUSY.
  6862. *
  6863. * This function is called with hbalock held. The function will return success
  6864. * after it successfully submit the iocb to firmware or after adding to the
  6865. * txq.
  6866. **/
  6867. static int
  6868. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6869. struct lpfc_iocbq *piocb, uint32_t flag)
  6870. {
  6871. struct lpfc_iocbq *nextiocb;
  6872. IOCB_t *iocb;
  6873. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6874. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6875. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6876. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6877. lpfc_printf_log(phba, KERN_ERR,
  6878. LOG_SLI | LOG_VPORT,
  6879. "1807 IOCB x%x failed. No vport\n",
  6880. piocb->iocb.ulpCommand);
  6881. dump_stack();
  6882. return IOCB_ERROR;
  6883. }
  6884. /* If the PCI channel is in offline state, do not post iocbs. */
  6885. if (unlikely(pci_channel_offline(phba->pcidev)))
  6886. return IOCB_ERROR;
  6887. /* If HBA has a deferred error attention, fail the iocb. */
  6888. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6889. return IOCB_ERROR;
  6890. /*
  6891. * We should never get an IOCB if we are in a < LINK_DOWN state
  6892. */
  6893. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6894. return IOCB_ERROR;
  6895. /*
  6896. * Check to see if we are blocking IOCB processing because of a
  6897. * outstanding event.
  6898. */
  6899. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  6900. goto iocb_busy;
  6901. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  6902. /*
  6903. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  6904. * can be issued if the link is not up.
  6905. */
  6906. switch (piocb->iocb.ulpCommand) {
  6907. case CMD_GEN_REQUEST64_CR:
  6908. case CMD_GEN_REQUEST64_CX:
  6909. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  6910. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  6911. FC_RCTL_DD_UNSOL_CMD) ||
  6912. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  6913. MENLO_TRANSPORT_TYPE))
  6914. goto iocb_busy;
  6915. break;
  6916. case CMD_QUE_RING_BUF_CN:
  6917. case CMD_QUE_RING_BUF64_CN:
  6918. /*
  6919. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  6920. * completion, iocb_cmpl MUST be 0.
  6921. */
  6922. if (piocb->iocb_cmpl)
  6923. piocb->iocb_cmpl = NULL;
  6924. /*FALLTHROUGH*/
  6925. case CMD_CREATE_XRI_CR:
  6926. case CMD_CLOSE_XRI_CN:
  6927. case CMD_CLOSE_XRI_CX:
  6928. break;
  6929. default:
  6930. goto iocb_busy;
  6931. }
  6932. /*
  6933. * For FCP commands, we must be in a state where we can process link
  6934. * attention events.
  6935. */
  6936. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  6937. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  6938. goto iocb_busy;
  6939. }
  6940. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  6941. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  6942. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  6943. if (iocb)
  6944. lpfc_sli_update_ring(phba, pring);
  6945. else
  6946. lpfc_sli_update_full_ring(phba, pring);
  6947. if (!piocb)
  6948. return IOCB_SUCCESS;
  6949. goto out_busy;
  6950. iocb_busy:
  6951. pring->stats.iocb_cmd_delay++;
  6952. out_busy:
  6953. if (!(flag & SLI_IOCB_RET_IOCB)) {
  6954. __lpfc_sli_ringtx_put(phba, pring, piocb);
  6955. return IOCB_SUCCESS;
  6956. }
  6957. return IOCB_BUSY;
  6958. }
  6959. /**
  6960. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  6961. * @phba: Pointer to HBA context object.
  6962. * @piocb: Pointer to command iocb.
  6963. * @sglq: Pointer to the scatter gather queue object.
  6964. *
  6965. * This routine converts the bpl or bde that is in the IOCB
  6966. * to a sgl list for the sli4 hardware. The physical address
  6967. * of the bpl/bde is converted back to a virtual address.
  6968. * If the IOCB contains a BPL then the list of BDE's is
  6969. * converted to sli4_sge's. If the IOCB contains a single
  6970. * BDE then it is converted to a single sli_sge.
  6971. * The IOCB is still in cpu endianess so the contents of
  6972. * the bpl can be used without byte swapping.
  6973. *
  6974. * Returns valid XRI = Success, NO_XRI = Failure.
  6975. **/
  6976. static uint16_t
  6977. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  6978. struct lpfc_sglq *sglq)
  6979. {
  6980. uint16_t xritag = NO_XRI;
  6981. struct ulp_bde64 *bpl = NULL;
  6982. struct ulp_bde64 bde;
  6983. struct sli4_sge *sgl = NULL;
  6984. struct lpfc_dmabuf *dmabuf;
  6985. IOCB_t *icmd;
  6986. int numBdes = 0;
  6987. int i = 0;
  6988. uint32_t offset = 0; /* accumulated offset in the sg request list */
  6989. int inbound = 0; /* number of sg reply entries inbound from firmware */
  6990. if (!piocbq || !sglq)
  6991. return xritag;
  6992. sgl = (struct sli4_sge *)sglq->sgl;
  6993. icmd = &piocbq->iocb;
  6994. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  6995. return sglq->sli4_xritag;
  6996. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6997. numBdes = icmd->un.genreq64.bdl.bdeSize /
  6998. sizeof(struct ulp_bde64);
  6999. /* The addrHigh and addrLow fields within the IOCB
  7000. * have not been byteswapped yet so there is no
  7001. * need to swap them back.
  7002. */
  7003. if (piocbq->context3)
  7004. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  7005. else
  7006. return xritag;
  7007. bpl = (struct ulp_bde64 *)dmabuf->virt;
  7008. if (!bpl)
  7009. return xritag;
  7010. for (i = 0; i < numBdes; i++) {
  7011. /* Should already be byte swapped. */
  7012. sgl->addr_hi = bpl->addrHigh;
  7013. sgl->addr_lo = bpl->addrLow;
  7014. sgl->word2 = le32_to_cpu(sgl->word2);
  7015. if ((i+1) == numBdes)
  7016. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7017. else
  7018. bf_set(lpfc_sli4_sge_last, sgl, 0);
  7019. /* swap the size field back to the cpu so we
  7020. * can assign it to the sgl.
  7021. */
  7022. bde.tus.w = le32_to_cpu(bpl->tus.w);
  7023. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  7024. /* The offsets in the sgl need to be accumulated
  7025. * separately for the request and reply lists.
  7026. * The request is always first, the reply follows.
  7027. */
  7028. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  7029. /* add up the reply sg entries */
  7030. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  7031. inbound++;
  7032. /* first inbound? reset the offset */
  7033. if (inbound == 1)
  7034. offset = 0;
  7035. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  7036. bf_set(lpfc_sli4_sge_type, sgl,
  7037. LPFC_SGE_TYPE_DATA);
  7038. offset += bde.tus.f.bdeSize;
  7039. }
  7040. sgl->word2 = cpu_to_le32(sgl->word2);
  7041. bpl++;
  7042. sgl++;
  7043. }
  7044. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  7045. /* The addrHigh and addrLow fields of the BDE have not
  7046. * been byteswapped yet so they need to be swapped
  7047. * before putting them in the sgl.
  7048. */
  7049. sgl->addr_hi =
  7050. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  7051. sgl->addr_lo =
  7052. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  7053. sgl->word2 = le32_to_cpu(sgl->word2);
  7054. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7055. sgl->word2 = cpu_to_le32(sgl->word2);
  7056. sgl->sge_len =
  7057. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  7058. }
  7059. return sglq->sli4_xritag;
  7060. }
  7061. /**
  7062. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  7063. * @phba: Pointer to HBA context object.
  7064. *
  7065. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  7066. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  7067. * held.
  7068. *
  7069. * Return: index into SLI4 fast-path FCP queue index.
  7070. **/
  7071. static uint32_t
  7072. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  7073. {
  7074. ++phba->fcp_qidx;
  7075. if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
  7076. phba->fcp_qidx = 0;
  7077. return phba->fcp_qidx;
  7078. }
  7079. /**
  7080. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  7081. * @phba: Pointer to HBA context object.
  7082. * @piocb: Pointer to command iocb.
  7083. * @wqe: Pointer to the work queue entry.
  7084. *
  7085. * This routine converts the iocb command to its Work Queue Entry
  7086. * equivalent. The wqe pointer should not have any fields set when
  7087. * this routine is called because it will memcpy over them.
  7088. * This routine does not set the CQ_ID or the WQEC bits in the
  7089. * wqe.
  7090. *
  7091. * Returns: 0 = Success, IOCB_ERROR = Failure.
  7092. **/
  7093. static int
  7094. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  7095. union lpfc_wqe *wqe)
  7096. {
  7097. uint32_t xmit_len = 0, total_len = 0;
  7098. uint8_t ct = 0;
  7099. uint32_t fip;
  7100. uint32_t abort_tag;
  7101. uint8_t command_type = ELS_COMMAND_NON_FIP;
  7102. uint8_t cmnd;
  7103. uint16_t xritag;
  7104. uint16_t abrt_iotag;
  7105. struct lpfc_iocbq *abrtiocbq;
  7106. struct ulp_bde64 *bpl = NULL;
  7107. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  7108. int numBdes, i;
  7109. struct ulp_bde64 bde;
  7110. struct lpfc_nodelist *ndlp;
  7111. uint32_t *pcmd;
  7112. uint32_t if_type;
  7113. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  7114. /* The fcp commands will set command type */
  7115. if (iocbq->iocb_flag & LPFC_IO_FCP)
  7116. command_type = FCP_COMMAND;
  7117. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  7118. command_type = ELS_COMMAND_FIP;
  7119. else
  7120. command_type = ELS_COMMAND_NON_FIP;
  7121. /* Some of the fields are in the right position already */
  7122. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  7123. abort_tag = (uint32_t) iocbq->iotag;
  7124. xritag = iocbq->sli4_xritag;
  7125. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  7126. /* words0-2 bpl convert bde */
  7127. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7128. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7129. sizeof(struct ulp_bde64);
  7130. bpl = (struct ulp_bde64 *)
  7131. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  7132. if (!bpl)
  7133. return IOCB_ERROR;
  7134. /* Should already be byte swapped. */
  7135. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7136. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7137. /* swap the size field back to the cpu so we
  7138. * can assign it to the sgl.
  7139. */
  7140. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7141. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7142. total_len = 0;
  7143. for (i = 0; i < numBdes; i++) {
  7144. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7145. total_len += bde.tus.f.bdeSize;
  7146. }
  7147. } else
  7148. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7149. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7150. cmnd = iocbq->iocb.ulpCommand;
  7151. switch (iocbq->iocb.ulpCommand) {
  7152. case CMD_ELS_REQUEST64_CR:
  7153. if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
  7154. ndlp = iocbq->context_un.ndlp;
  7155. else
  7156. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7157. if (!iocbq->iocb.ulpLe) {
  7158. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7159. "2007 Only Limited Edition cmd Format"
  7160. " supported 0x%x\n",
  7161. iocbq->iocb.ulpCommand);
  7162. return IOCB_ERROR;
  7163. }
  7164. wqe->els_req.payload_len = xmit_len;
  7165. /* Els_reguest64 has a TMO */
  7166. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7167. iocbq->iocb.ulpTimeout);
  7168. /* Need a VF for word 4 set the vf bit*/
  7169. bf_set(els_req64_vf, &wqe->els_req, 0);
  7170. /* And a VFID for word 12 */
  7171. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7172. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7173. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7174. iocbq->iocb.ulpContext);
  7175. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7176. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7177. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7178. if (command_type == ELS_COMMAND_FIP)
  7179. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7180. >> LPFC_FIP_ELS_ID_SHIFT);
  7181. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7182. iocbq->context2)->virt);
  7183. if_type = bf_get(lpfc_sli_intf_if_type,
  7184. &phba->sli4_hba.sli_intf);
  7185. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7186. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7187. *pcmd == ELS_CMD_SCR ||
  7188. *pcmd == ELS_CMD_FDISC ||
  7189. *pcmd == ELS_CMD_LOGO ||
  7190. *pcmd == ELS_CMD_PLOGI)) {
  7191. bf_set(els_req64_sp, &wqe->els_req, 1);
  7192. bf_set(els_req64_sid, &wqe->els_req,
  7193. iocbq->vport->fc_myDID);
  7194. if ((*pcmd == ELS_CMD_FLOGI) &&
  7195. !(phba->fc_topology ==
  7196. LPFC_TOPOLOGY_LOOP))
  7197. bf_set(els_req64_sid, &wqe->els_req, 0);
  7198. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7199. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7200. phba->vpi_ids[iocbq->vport->vpi]);
  7201. } else if (pcmd && iocbq->context1) {
  7202. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7203. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7204. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7205. }
  7206. }
  7207. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7208. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7209. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7210. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7211. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7212. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7213. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7214. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7215. break;
  7216. case CMD_XMIT_SEQUENCE64_CX:
  7217. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7218. iocbq->iocb.un.ulpWord[3]);
  7219. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7220. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7221. /* The entire sequence is transmitted for this IOCB */
  7222. xmit_len = total_len;
  7223. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7224. if (phba->link_flag & LS_LOOPBACK_MODE)
  7225. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7226. case CMD_XMIT_SEQUENCE64_CR:
  7227. /* word3 iocb=io_tag32 wqe=reserved */
  7228. wqe->xmit_sequence.rsvd3 = 0;
  7229. /* word4 relative_offset memcpy */
  7230. /* word5 r_ctl/df_ctl memcpy */
  7231. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7232. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7233. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7234. LPFC_WQE_IOD_WRITE);
  7235. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7236. LPFC_WQE_LENLOC_WORD12);
  7237. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7238. wqe->xmit_sequence.xmit_len = xmit_len;
  7239. command_type = OTHER_COMMAND;
  7240. break;
  7241. case CMD_XMIT_BCAST64_CN:
  7242. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7243. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7244. /* word4 iocb=rsvd wqe=rsvd */
  7245. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7246. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7247. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7248. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7249. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7250. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7251. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7252. LPFC_WQE_LENLOC_WORD3);
  7253. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7254. break;
  7255. case CMD_FCP_IWRITE64_CR:
  7256. command_type = FCP_COMMAND_DATA_OUT;
  7257. /* word3 iocb=iotag wqe=payload_offset_len */
  7258. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7259. wqe->fcp_iwrite.payload_offset_len =
  7260. xmit_len + sizeof(struct fcp_rsp);
  7261. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7262. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7263. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7264. iocbq->iocb.ulpFCP2Rcvy);
  7265. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7266. /* Always open the exchange */
  7267. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7268. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7269. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7270. LPFC_WQE_LENLOC_WORD4);
  7271. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7272. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7273. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7274. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7275. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7276. }
  7277. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7278. break;
  7279. case CMD_FCP_IREAD64_CR:
  7280. /* word3 iocb=iotag wqe=payload_offset_len */
  7281. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7282. wqe->fcp_iread.payload_offset_len =
  7283. xmit_len + sizeof(struct fcp_rsp);
  7284. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7285. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7286. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7287. iocbq->iocb.ulpFCP2Rcvy);
  7288. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7289. /* Always open the exchange */
  7290. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7291. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7292. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7293. LPFC_WQE_LENLOC_WORD4);
  7294. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7295. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7296. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7297. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7298. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7299. }
  7300. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7301. break;
  7302. case CMD_FCP_ICMND64_CR:
  7303. /* word3 iocb=IO_TAG wqe=reserved */
  7304. wqe->fcp_icmd.rsrvd3 = 0;
  7305. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7306. /* Always open the exchange */
  7307. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7308. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7309. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7310. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7311. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7312. LPFC_WQE_LENLOC_NONE);
  7313. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7314. break;
  7315. case CMD_GEN_REQUEST64_CR:
  7316. /* For this command calculate the xmit length of the
  7317. * request bde.
  7318. */
  7319. xmit_len = 0;
  7320. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7321. sizeof(struct ulp_bde64);
  7322. for (i = 0; i < numBdes; i++) {
  7323. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7324. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7325. break;
  7326. xmit_len += bde.tus.f.bdeSize;
  7327. }
  7328. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7329. wqe->gen_req.request_payload_len = xmit_len;
  7330. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7331. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7332. /* word6 context tag copied in memcpy */
  7333. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7334. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7335. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7336. "2015 Invalid CT %x command 0x%x\n",
  7337. ct, iocbq->iocb.ulpCommand);
  7338. return IOCB_ERROR;
  7339. }
  7340. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7341. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7342. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7343. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7344. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7345. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7346. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7347. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7348. command_type = OTHER_COMMAND;
  7349. break;
  7350. case CMD_XMIT_ELS_RSP64_CX:
  7351. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7352. /* words0-2 BDE memcpy */
  7353. /* word3 iocb=iotag32 wqe=response_payload_len */
  7354. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7355. /* word4 */
  7356. wqe->xmit_els_rsp.word4 = 0;
  7357. /* word5 iocb=rsvd wge=did */
  7358. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7359. iocbq->iocb.un.xseq64.xmit_els_remoteID);
  7360. if_type = bf_get(lpfc_sli_intf_if_type,
  7361. &phba->sli4_hba.sli_intf);
  7362. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7363. if (iocbq->vport->fc_flag & FC_PT2PT) {
  7364. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7365. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7366. iocbq->vport->fc_myDID);
  7367. if (iocbq->vport->fc_myDID == Fabric_DID) {
  7368. bf_set(wqe_els_did,
  7369. &wqe->xmit_els_rsp.wqe_dest, 0);
  7370. }
  7371. }
  7372. }
  7373. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7374. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7375. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7376. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7377. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7378. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7379. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7380. phba->vpi_ids[iocbq->vport->vpi]);
  7381. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7382. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7383. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7384. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7385. LPFC_WQE_LENLOC_WORD3);
  7386. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7387. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7388. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7389. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7390. iocbq->context2)->virt);
  7391. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7392. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7393. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7394. iocbq->vport->fc_myDID);
  7395. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
  7396. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7397. phba->vpi_ids[phba->pport->vpi]);
  7398. }
  7399. command_type = OTHER_COMMAND;
  7400. break;
  7401. case CMD_CLOSE_XRI_CN:
  7402. case CMD_ABORT_XRI_CN:
  7403. case CMD_ABORT_XRI_CX:
  7404. /* words 0-2 memcpy should be 0 rserved */
  7405. /* port will send abts */
  7406. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7407. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7408. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7409. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7410. } else
  7411. fip = 0;
  7412. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7413. /*
  7414. * The link is down, or the command was ELS_FIP
  7415. * so the fw does not need to send abts
  7416. * on the wire.
  7417. */
  7418. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7419. else
  7420. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7421. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7422. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7423. wqe->abort_cmd.rsrvd5 = 0;
  7424. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7425. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7426. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7427. /*
  7428. * The abort handler will send us CMD_ABORT_XRI_CN or
  7429. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7430. */
  7431. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7432. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7433. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7434. LPFC_WQE_LENLOC_NONE);
  7435. cmnd = CMD_ABORT_XRI_CX;
  7436. command_type = OTHER_COMMAND;
  7437. xritag = 0;
  7438. break;
  7439. case CMD_XMIT_BLS_RSP64_CX:
  7440. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7441. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7442. * we re-construct this WQE here based on information in
  7443. * iocbq from scratch.
  7444. */
  7445. memset(wqe, 0, sizeof(union lpfc_wqe));
  7446. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7447. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7448. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7449. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7450. LPFC_ABTS_UNSOL_INT) {
  7451. /* ABTS sent by initiator to CT exchange, the
  7452. * RX_ID field will be filled with the newly
  7453. * allocated responder XRI.
  7454. */
  7455. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7456. iocbq->sli4_xritag);
  7457. } else {
  7458. /* ABTS sent by responder to CT exchange, the
  7459. * RX_ID field will be filled with the responder
  7460. * RX_ID from ABTS.
  7461. */
  7462. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7463. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7464. }
  7465. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7466. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7467. /* Use CT=VPI */
  7468. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7469. ndlp->nlp_DID);
  7470. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7471. iocbq->iocb.ulpContext);
  7472. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7473. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7474. phba->vpi_ids[phba->pport->vpi]);
  7475. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7476. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7477. LPFC_WQE_LENLOC_NONE);
  7478. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7479. command_type = OTHER_COMMAND;
  7480. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7481. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7482. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7483. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7484. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7485. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7486. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7487. }
  7488. break;
  7489. case CMD_XRI_ABORTED_CX:
  7490. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7491. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7492. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7493. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7494. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7495. default:
  7496. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7497. "2014 Invalid command 0x%x\n",
  7498. iocbq->iocb.ulpCommand);
  7499. return IOCB_ERROR;
  7500. break;
  7501. }
  7502. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7503. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7504. wqe->generic.wqe_com.abort_tag = abort_tag;
  7505. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7506. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7507. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7508. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7509. return 0;
  7510. }
  7511. /**
  7512. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7513. * @phba: Pointer to HBA context object.
  7514. * @ring_number: SLI ring number to issue iocb on.
  7515. * @piocb: Pointer to command iocb.
  7516. * @flag: Flag indicating if this command can be put into txq.
  7517. *
  7518. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7519. * an iocb command to an HBA with SLI-4 interface spec.
  7520. *
  7521. * This function is called with hbalock held. The function will return success
  7522. * after it successfully submit the iocb to firmware or after adding to the
  7523. * txq.
  7524. **/
  7525. static int
  7526. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7527. struct lpfc_iocbq *piocb, uint32_t flag)
  7528. {
  7529. struct lpfc_sglq *sglq;
  7530. union lpfc_wqe wqe;
  7531. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7532. if (piocb->sli4_xritag == NO_XRI) {
  7533. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7534. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7535. sglq = NULL;
  7536. else {
  7537. if (pring->txq_cnt) {
  7538. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7539. __lpfc_sli_ringtx_put(phba,
  7540. pring, piocb);
  7541. return IOCB_SUCCESS;
  7542. } else {
  7543. return IOCB_BUSY;
  7544. }
  7545. } else {
  7546. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7547. if (!sglq) {
  7548. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7549. __lpfc_sli_ringtx_put(phba,
  7550. pring,
  7551. piocb);
  7552. return IOCB_SUCCESS;
  7553. } else
  7554. return IOCB_BUSY;
  7555. }
  7556. }
  7557. }
  7558. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7559. /* These IO's already have an XRI and a mapped sgl. */
  7560. sglq = NULL;
  7561. } else {
  7562. /*
  7563. * This is a continuation of a commandi,(CX) so this
  7564. * sglq is on the active list
  7565. */
  7566. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  7567. if (!sglq)
  7568. return IOCB_ERROR;
  7569. }
  7570. if (sglq) {
  7571. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7572. piocb->sli4_xritag = sglq->sli4_xritag;
  7573. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7574. return IOCB_ERROR;
  7575. }
  7576. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7577. return IOCB_ERROR;
  7578. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7579. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7580. /*
  7581. * For FCP command IOCB, get a new WQ index to distribute
  7582. * WQE across the WQsr. On the other hand, for abort IOCB,
  7583. * it carries the same WQ index to the original command
  7584. * IOCB.
  7585. */
  7586. if (piocb->iocb_flag & LPFC_IO_FCP)
  7587. piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7588. if (unlikely(!phba->sli4_hba.fcp_wq))
  7589. return IOCB_ERROR;
  7590. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7591. &wqe))
  7592. return IOCB_ERROR;
  7593. } else {
  7594. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7595. return IOCB_ERROR;
  7596. }
  7597. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7598. return 0;
  7599. }
  7600. /**
  7601. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7602. *
  7603. * This routine wraps the actual lockless version for issusing IOCB function
  7604. * pointer from the lpfc_hba struct.
  7605. *
  7606. * Return codes:
  7607. * IOCB_ERROR - Error
  7608. * IOCB_SUCCESS - Success
  7609. * IOCB_BUSY - Busy
  7610. **/
  7611. int
  7612. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7613. struct lpfc_iocbq *piocb, uint32_t flag)
  7614. {
  7615. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7616. }
  7617. /**
  7618. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7619. * @phba: The hba struct for which this call is being executed.
  7620. * @dev_grp: The HBA PCI-Device group number.
  7621. *
  7622. * This routine sets up the SLI interface API function jump table in @phba
  7623. * struct.
  7624. * Returns: 0 - success, -ENODEV - failure.
  7625. **/
  7626. int
  7627. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7628. {
  7629. switch (dev_grp) {
  7630. case LPFC_PCI_DEV_LP:
  7631. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7632. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7633. break;
  7634. case LPFC_PCI_DEV_OC:
  7635. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7636. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7637. break;
  7638. default:
  7639. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7640. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7641. dev_grp);
  7642. return -ENODEV;
  7643. break;
  7644. }
  7645. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7646. return 0;
  7647. }
  7648. /**
  7649. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7650. * @phba: Pointer to HBA context object.
  7651. * @pring: Pointer to driver SLI ring object.
  7652. * @piocb: Pointer to command iocb.
  7653. * @flag: Flag indicating if this command can be put into txq.
  7654. *
  7655. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7656. * function. This function gets the hbalock and calls
  7657. * __lpfc_sli_issue_iocb function and will return the error returned
  7658. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7659. * functions which do not hold hbalock.
  7660. **/
  7661. int
  7662. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7663. struct lpfc_iocbq *piocb, uint32_t flag)
  7664. {
  7665. unsigned long iflags;
  7666. int rc;
  7667. spin_lock_irqsave(&phba->hbalock, iflags);
  7668. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7669. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7670. return rc;
  7671. }
  7672. /**
  7673. * lpfc_extra_ring_setup - Extra ring setup function
  7674. * @phba: Pointer to HBA context object.
  7675. *
  7676. * This function is called while driver attaches with the
  7677. * HBA to setup the extra ring. The extra ring is used
  7678. * only when driver needs to support target mode functionality
  7679. * or IP over FC functionalities.
  7680. *
  7681. * This function is called with no lock held.
  7682. **/
  7683. static int
  7684. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7685. {
  7686. struct lpfc_sli *psli;
  7687. struct lpfc_sli_ring *pring;
  7688. psli = &phba->sli;
  7689. /* Adjust cmd/rsp ring iocb entries more evenly */
  7690. /* Take some away from the FCP ring */
  7691. pring = &psli->ring[psli->fcp_ring];
  7692. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7693. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7694. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7695. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7696. /* and give them to the extra ring */
  7697. pring = &psli->ring[psli->extra_ring];
  7698. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7699. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7700. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7701. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7702. /* Setup default profile for this ring */
  7703. pring->iotag_max = 4096;
  7704. pring->num_mask = 1;
  7705. pring->prt[0].profile = 0; /* Mask 0 */
  7706. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7707. pring->prt[0].type = phba->cfg_multi_ring_type;
  7708. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7709. return 0;
  7710. }
  7711. /* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
  7712. * @vport: pointer to virtual port object.
  7713. * @ndlp: nodelist pointer for the impacted rport.
  7714. *
  7715. * The driver calls this routine in response to a XRI ABORT CQE
  7716. * event from the port. In this event, the driver is required to
  7717. * recover its login to the rport even though its login may be valid
  7718. * from the driver's perspective. The failed ABTS notice from the
  7719. * port indicates the rport is not responding.
  7720. */
  7721. static void
  7722. lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
  7723. struct lpfc_nodelist *ndlp)
  7724. {
  7725. struct Scsi_Host *shost;
  7726. struct lpfc_hba *phba;
  7727. unsigned long flags = 0;
  7728. shost = lpfc_shost_from_vport(vport);
  7729. phba = vport->phba;
  7730. if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
  7731. lpfc_printf_log(phba, KERN_INFO,
  7732. LOG_SLI, "3093 No rport recovery needed. "
  7733. "rport in state 0x%x\n",
  7734. ndlp->nlp_state);
  7735. return;
  7736. }
  7737. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7738. "3094 Start rport recovery on shost id 0x%x "
  7739. "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
  7740. "flags 0x%x\n",
  7741. shost->host_no, ndlp->nlp_DID,
  7742. vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
  7743. ndlp->nlp_flag);
  7744. /*
  7745. * The rport is not responding. Don't attempt ADISC recovery.
  7746. * Remove the FCP-2 flag to force a PLOGI.
  7747. */
  7748. spin_lock_irqsave(shost->host_lock, flags);
  7749. ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  7750. spin_unlock_irqrestore(shost->host_lock, flags);
  7751. lpfc_disc_state_machine(vport, ndlp, NULL,
  7752. NLP_EVT_DEVICE_RECOVERY);
  7753. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  7754. spin_lock_irqsave(shost->host_lock, flags);
  7755. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  7756. spin_unlock_irqrestore(shost->host_lock, flags);
  7757. lpfc_disc_start(vport);
  7758. }
  7759. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7760. * @phba: Pointer to HBA context object.
  7761. * @iocbq: Pointer to iocb object.
  7762. *
  7763. * The async_event handler calls this routine when it receives
  7764. * an ASYNC_STATUS_CN event from the port. The port generates
  7765. * this event when an Abort Sequence request to an rport fails
  7766. * twice in succession. The abort could be originated by the
  7767. * driver or by the port. The ABTS could have been for an ELS
  7768. * or FCP IO. The port only generates this event when an ABTS
  7769. * fails to complete after one retry.
  7770. */
  7771. static void
  7772. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7773. struct lpfc_iocbq *iocbq)
  7774. {
  7775. struct lpfc_nodelist *ndlp = NULL;
  7776. uint16_t rpi = 0, vpi = 0;
  7777. struct lpfc_vport *vport = NULL;
  7778. /* The rpi in the ulpContext is vport-sensitive. */
  7779. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7780. rpi = iocbq->iocb.ulpContext;
  7781. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7782. "3092 Port generated ABTS async event "
  7783. "on vpi %d rpi %d status 0x%x\n",
  7784. vpi, rpi, iocbq->iocb.ulpStatus);
  7785. vport = lpfc_find_vport_by_vpid(phba, vpi);
  7786. if (!vport)
  7787. goto err_exit;
  7788. ndlp = lpfc_findnode_rpi(vport, rpi);
  7789. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7790. goto err_exit;
  7791. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  7792. lpfc_sli_abts_recover_port(vport, ndlp);
  7793. return;
  7794. err_exit:
  7795. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7796. "3095 Event Context not found, no "
  7797. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  7798. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  7799. vpi, rpi);
  7800. }
  7801. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  7802. * @phba: pointer to HBA context object.
  7803. * @ndlp: nodelist pointer for the impacted rport.
  7804. * @axri: pointer to the wcqe containing the failed exchange.
  7805. *
  7806. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  7807. * port. The port generates this event when an abort exchange request to an
  7808. * rport fails twice in succession with no reply. The abort could be originated
  7809. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  7810. */
  7811. void
  7812. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  7813. struct lpfc_nodelist *ndlp,
  7814. struct sli4_wcqe_xri_aborted *axri)
  7815. {
  7816. struct lpfc_vport *vport;
  7817. uint32_t ext_status = 0;
  7818. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  7819. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7820. "3115 Node Context not found, driver "
  7821. "ignoring abts err event\n");
  7822. return;
  7823. }
  7824. vport = ndlp->vport;
  7825. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7826. "3116 Port generated FCP XRI ABORT event on "
  7827. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  7828. ndlp->vport->vpi, ndlp->nlp_rpi,
  7829. bf_get(lpfc_wcqe_xa_xri, axri),
  7830. bf_get(lpfc_wcqe_xa_status, axri),
  7831. axri->parameter);
  7832. /*
  7833. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  7834. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  7835. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  7836. */
  7837. ext_status = axri->parameter & WCQE_PARAM_MASK;
  7838. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  7839. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  7840. lpfc_sli_abts_recover_port(vport, ndlp);
  7841. }
  7842. /**
  7843. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7844. * @phba: Pointer to HBA context object.
  7845. * @pring: Pointer to driver SLI ring object.
  7846. * @iocbq: Pointer to iocb object.
  7847. *
  7848. * This function is called by the slow ring event handler
  7849. * function when there is an ASYNC event iocb in the ring.
  7850. * This function is called with no lock held.
  7851. * Currently this function handles only temperature related
  7852. * ASYNC events. The function decodes the temperature sensor
  7853. * event message and posts events for the management applications.
  7854. **/
  7855. static void
  7856. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7857. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7858. {
  7859. IOCB_t *icmd;
  7860. uint16_t evt_code;
  7861. struct temp_event temp_event_data;
  7862. struct Scsi_Host *shost;
  7863. uint32_t *iocb_w;
  7864. icmd = &iocbq->iocb;
  7865. evt_code = icmd->un.asyncstat.evt_code;
  7866. switch (evt_code) {
  7867. case ASYNC_TEMP_WARN:
  7868. case ASYNC_TEMP_SAFE:
  7869. temp_event_data.data = (uint32_t) icmd->ulpContext;
  7870. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7871. if (evt_code == ASYNC_TEMP_WARN) {
  7872. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7873. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7874. "0347 Adapter is very hot, please take "
  7875. "corrective action. temperature : %d Celsius\n",
  7876. (uint32_t) icmd->ulpContext);
  7877. } else {
  7878. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7879. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7880. "0340 Adapter temperature is OK now. "
  7881. "temperature : %d Celsius\n",
  7882. (uint32_t) icmd->ulpContext);
  7883. }
  7884. /* Send temperature change event to applications */
  7885. shost = lpfc_shost_from_vport(phba->pport);
  7886. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7887. sizeof(temp_event_data), (char *) &temp_event_data,
  7888. LPFC_NL_VENDOR_ID);
  7889. break;
  7890. case ASYNC_STATUS_CN:
  7891. lpfc_sli_abts_err_handler(phba, iocbq);
  7892. break;
  7893. default:
  7894. iocb_w = (uint32_t *) icmd;
  7895. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7896. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7897. " evt_code 0x%x\n"
  7898. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7899. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7900. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7901. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7902. pring->ringno, icmd->un.asyncstat.evt_code,
  7903. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7904. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  7905. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  7906. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  7907. break;
  7908. }
  7909. }
  7910. /**
  7911. * lpfc_sli_setup - SLI ring setup function
  7912. * @phba: Pointer to HBA context object.
  7913. *
  7914. * lpfc_sli_setup sets up rings of the SLI interface with
  7915. * number of iocbs per ring and iotags. This function is
  7916. * called while driver attach to the HBA and before the
  7917. * interrupts are enabled. So there is no need for locking.
  7918. *
  7919. * This function always returns 0.
  7920. **/
  7921. int
  7922. lpfc_sli_setup(struct lpfc_hba *phba)
  7923. {
  7924. int i, totiocbsize = 0;
  7925. struct lpfc_sli *psli = &phba->sli;
  7926. struct lpfc_sli_ring *pring;
  7927. psli->num_rings = MAX_CONFIGURED_RINGS;
  7928. psli->sli_flag = 0;
  7929. psli->fcp_ring = LPFC_FCP_RING;
  7930. psli->next_ring = LPFC_FCP_NEXT_RING;
  7931. psli->extra_ring = LPFC_EXTRA_RING;
  7932. psli->iocbq_lookup = NULL;
  7933. psli->iocbq_lookup_len = 0;
  7934. psli->last_iotag = 0;
  7935. for (i = 0; i < psli->num_rings; i++) {
  7936. pring = &psli->ring[i];
  7937. switch (i) {
  7938. case LPFC_FCP_RING: /* ring 0 - FCP */
  7939. /* numCiocb and numRiocb are used in config_port */
  7940. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  7941. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  7942. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7943. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7944. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7945. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7946. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7947. SLI3_IOCB_CMD_SIZE :
  7948. SLI2_IOCB_CMD_SIZE;
  7949. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7950. SLI3_IOCB_RSP_SIZE :
  7951. SLI2_IOCB_RSP_SIZE;
  7952. pring->iotag_ctr = 0;
  7953. pring->iotag_max =
  7954. (phba->cfg_hba_queue_depth * 2);
  7955. pring->fast_iotag = pring->iotag_max;
  7956. pring->num_mask = 0;
  7957. break;
  7958. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  7959. /* numCiocb and numRiocb are used in config_port */
  7960. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  7961. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  7962. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7963. SLI3_IOCB_CMD_SIZE :
  7964. SLI2_IOCB_CMD_SIZE;
  7965. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7966. SLI3_IOCB_RSP_SIZE :
  7967. SLI2_IOCB_RSP_SIZE;
  7968. pring->iotag_max = phba->cfg_hba_queue_depth;
  7969. pring->num_mask = 0;
  7970. break;
  7971. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  7972. /* numCiocb and numRiocb are used in config_port */
  7973. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  7974. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  7975. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7976. SLI3_IOCB_CMD_SIZE :
  7977. SLI2_IOCB_CMD_SIZE;
  7978. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7979. SLI3_IOCB_RSP_SIZE :
  7980. SLI2_IOCB_RSP_SIZE;
  7981. pring->fast_iotag = 0;
  7982. pring->iotag_ctr = 0;
  7983. pring->iotag_max = 4096;
  7984. pring->lpfc_sli_rcv_async_status =
  7985. lpfc_sli_async_event_handler;
  7986. pring->num_mask = LPFC_MAX_RING_MASK;
  7987. pring->prt[0].profile = 0; /* Mask 0 */
  7988. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  7989. pring->prt[0].type = FC_TYPE_ELS;
  7990. pring->prt[0].lpfc_sli_rcv_unsol_event =
  7991. lpfc_els_unsol_event;
  7992. pring->prt[1].profile = 0; /* Mask 1 */
  7993. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  7994. pring->prt[1].type = FC_TYPE_ELS;
  7995. pring->prt[1].lpfc_sli_rcv_unsol_event =
  7996. lpfc_els_unsol_event;
  7997. pring->prt[2].profile = 0; /* Mask 2 */
  7998. /* NameServer Inquiry */
  7999. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  8000. /* NameServer */
  8001. pring->prt[2].type = FC_TYPE_CT;
  8002. pring->prt[2].lpfc_sli_rcv_unsol_event =
  8003. lpfc_ct_unsol_event;
  8004. pring->prt[3].profile = 0; /* Mask 3 */
  8005. /* NameServer response */
  8006. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  8007. /* NameServer */
  8008. pring->prt[3].type = FC_TYPE_CT;
  8009. pring->prt[3].lpfc_sli_rcv_unsol_event =
  8010. lpfc_ct_unsol_event;
  8011. /* abort unsolicited sequence */
  8012. pring->prt[4].profile = 0; /* Mask 4 */
  8013. pring->prt[4].rctl = FC_RCTL_BA_ABTS;
  8014. pring->prt[4].type = FC_TYPE_BLS;
  8015. pring->prt[4].lpfc_sli_rcv_unsol_event =
  8016. lpfc_sli4_ct_abort_unsol_event;
  8017. break;
  8018. }
  8019. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  8020. (pring->numRiocb * pring->sizeRiocb);
  8021. }
  8022. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  8023. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  8024. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  8025. "SLI2 SLIM Data: x%x x%lx\n",
  8026. phba->brd_no, totiocbsize,
  8027. (unsigned long) MAX_SLIM_IOCB_SIZE);
  8028. }
  8029. if (phba->cfg_multi_ring_support == 2)
  8030. lpfc_extra_ring_setup(phba);
  8031. return 0;
  8032. }
  8033. /**
  8034. * lpfc_sli_queue_setup - Queue initialization function
  8035. * @phba: Pointer to HBA context object.
  8036. *
  8037. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  8038. * ring. This function also initializes ring indices of each ring.
  8039. * This function is called during the initialization of the SLI
  8040. * interface of an HBA.
  8041. * This function is called with no lock held and always returns
  8042. * 1.
  8043. **/
  8044. int
  8045. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  8046. {
  8047. struct lpfc_sli *psli;
  8048. struct lpfc_sli_ring *pring;
  8049. int i;
  8050. psli = &phba->sli;
  8051. spin_lock_irq(&phba->hbalock);
  8052. INIT_LIST_HEAD(&psli->mboxq);
  8053. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  8054. /* Initialize list headers for txq and txcmplq as double linked lists */
  8055. for (i = 0; i < psli->num_rings; i++) {
  8056. pring = &psli->ring[i];
  8057. pring->ringno = i;
  8058. pring->next_cmdidx = 0;
  8059. pring->local_getidx = 0;
  8060. pring->cmdidx = 0;
  8061. INIT_LIST_HEAD(&pring->txq);
  8062. INIT_LIST_HEAD(&pring->txcmplq);
  8063. INIT_LIST_HEAD(&pring->iocb_continueq);
  8064. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  8065. INIT_LIST_HEAD(&pring->postbufq);
  8066. }
  8067. spin_unlock_irq(&phba->hbalock);
  8068. return 1;
  8069. }
  8070. /**
  8071. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  8072. * @phba: Pointer to HBA context object.
  8073. *
  8074. * This routine flushes the mailbox command subsystem. It will unconditionally
  8075. * flush all the mailbox commands in the three possible stages in the mailbox
  8076. * command sub-system: pending mailbox command queue; the outstanding mailbox
  8077. * command; and completed mailbox command queue. It is caller's responsibility
  8078. * to make sure that the driver is in the proper state to flush the mailbox
  8079. * command sub-system. Namely, the posting of mailbox commands into the
  8080. * pending mailbox command queue from the various clients must be stopped;
  8081. * either the HBA is in a state that it will never works on the outstanding
  8082. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  8083. * mailbox command has been completed.
  8084. **/
  8085. static void
  8086. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  8087. {
  8088. LIST_HEAD(completions);
  8089. struct lpfc_sli *psli = &phba->sli;
  8090. LPFC_MBOXQ_t *pmb;
  8091. unsigned long iflag;
  8092. /* Flush all the mailbox commands in the mbox system */
  8093. spin_lock_irqsave(&phba->hbalock, iflag);
  8094. /* The pending mailbox command queue */
  8095. list_splice_init(&phba->sli.mboxq, &completions);
  8096. /* The outstanding active mailbox command */
  8097. if (psli->mbox_active) {
  8098. list_add_tail(&psli->mbox_active->list, &completions);
  8099. psli->mbox_active = NULL;
  8100. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  8101. }
  8102. /* The completed mailbox command queue */
  8103. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  8104. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8105. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  8106. while (!list_empty(&completions)) {
  8107. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  8108. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  8109. if (pmb->mbox_cmpl)
  8110. pmb->mbox_cmpl(phba, pmb);
  8111. }
  8112. }
  8113. /**
  8114. * lpfc_sli_host_down - Vport cleanup function
  8115. * @vport: Pointer to virtual port object.
  8116. *
  8117. * lpfc_sli_host_down is called to clean up the resources
  8118. * associated with a vport before destroying virtual
  8119. * port data structures.
  8120. * This function does following operations:
  8121. * - Free discovery resources associated with this virtual
  8122. * port.
  8123. * - Free iocbs associated with this virtual port in
  8124. * the txq.
  8125. * - Send abort for all iocb commands associated with this
  8126. * vport in txcmplq.
  8127. *
  8128. * This function is called with no lock held and always returns 1.
  8129. **/
  8130. int
  8131. lpfc_sli_host_down(struct lpfc_vport *vport)
  8132. {
  8133. LIST_HEAD(completions);
  8134. struct lpfc_hba *phba = vport->phba;
  8135. struct lpfc_sli *psli = &phba->sli;
  8136. struct lpfc_sli_ring *pring;
  8137. struct lpfc_iocbq *iocb, *next_iocb;
  8138. int i;
  8139. unsigned long flags = 0;
  8140. uint16_t prev_pring_flag;
  8141. lpfc_cleanup_discovery_resources(vport);
  8142. spin_lock_irqsave(&phba->hbalock, flags);
  8143. for (i = 0; i < psli->num_rings; i++) {
  8144. pring = &psli->ring[i];
  8145. prev_pring_flag = pring->flag;
  8146. /* Only slow rings */
  8147. if (pring->ringno == LPFC_ELS_RING) {
  8148. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8149. /* Set the lpfc data pending flag */
  8150. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8151. }
  8152. /*
  8153. * Error everything on the txq since these iocbs have not been
  8154. * given to the FW yet.
  8155. */
  8156. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  8157. if (iocb->vport != vport)
  8158. continue;
  8159. list_move_tail(&iocb->list, &completions);
  8160. pring->txq_cnt--;
  8161. }
  8162. /* Next issue ABTS for everything on the txcmplq */
  8163. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8164. list) {
  8165. if (iocb->vport != vport)
  8166. continue;
  8167. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8168. }
  8169. pring->flag = prev_pring_flag;
  8170. }
  8171. spin_unlock_irqrestore(&phba->hbalock, flags);
  8172. /* Cancel all the IOCBs from the completions list */
  8173. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8174. IOERR_SLI_DOWN);
  8175. return 1;
  8176. }
  8177. /**
  8178. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8179. * @phba: Pointer to HBA context object.
  8180. *
  8181. * This function cleans up all iocb, buffers, mailbox commands
  8182. * while shutting down the HBA. This function is called with no
  8183. * lock held and always returns 1.
  8184. * This function does the following to cleanup driver resources:
  8185. * - Free discovery resources for each virtual port
  8186. * - Cleanup any pending fabric iocbs
  8187. * - Iterate through the iocb txq and free each entry
  8188. * in the list.
  8189. * - Free up any buffer posted to the HBA
  8190. * - Free mailbox commands in the mailbox queue.
  8191. **/
  8192. int
  8193. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8194. {
  8195. LIST_HEAD(completions);
  8196. struct lpfc_sli *psli = &phba->sli;
  8197. struct lpfc_sli_ring *pring;
  8198. struct lpfc_dmabuf *buf_ptr;
  8199. unsigned long flags = 0;
  8200. int i;
  8201. /* Shutdown the mailbox command sub-system */
  8202. lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
  8203. lpfc_hba_down_prep(phba);
  8204. lpfc_fabric_abort_hba(phba);
  8205. spin_lock_irqsave(&phba->hbalock, flags);
  8206. for (i = 0; i < psli->num_rings; i++) {
  8207. pring = &psli->ring[i];
  8208. /* Only slow rings */
  8209. if (pring->ringno == LPFC_ELS_RING) {
  8210. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8211. /* Set the lpfc data pending flag */
  8212. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8213. }
  8214. /*
  8215. * Error everything on the txq since these iocbs have not been
  8216. * given to the FW yet.
  8217. */
  8218. list_splice_init(&pring->txq, &completions);
  8219. pring->txq_cnt = 0;
  8220. }
  8221. spin_unlock_irqrestore(&phba->hbalock, flags);
  8222. /* Cancel all the IOCBs from the completions list */
  8223. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8224. IOERR_SLI_DOWN);
  8225. spin_lock_irqsave(&phba->hbalock, flags);
  8226. list_splice_init(&phba->elsbuf, &completions);
  8227. phba->elsbuf_cnt = 0;
  8228. phba->elsbuf_prev_cnt = 0;
  8229. spin_unlock_irqrestore(&phba->hbalock, flags);
  8230. while (!list_empty(&completions)) {
  8231. list_remove_head(&completions, buf_ptr,
  8232. struct lpfc_dmabuf, list);
  8233. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8234. kfree(buf_ptr);
  8235. }
  8236. /* Return any active mbox cmds */
  8237. del_timer_sync(&psli->mbox_tmo);
  8238. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8239. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8240. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8241. return 1;
  8242. }
  8243. /**
  8244. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8245. * @srcp: Source memory pointer.
  8246. * @destp: Destination memory pointer.
  8247. * @cnt: Number of words required to be copied.
  8248. *
  8249. * This function is used for copying data between driver memory
  8250. * and the SLI memory. This function also changes the endianness
  8251. * of each word if native endianness is different from SLI
  8252. * endianness. This function can be called with or without
  8253. * lock.
  8254. **/
  8255. void
  8256. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8257. {
  8258. uint32_t *src = srcp;
  8259. uint32_t *dest = destp;
  8260. uint32_t ldata;
  8261. int i;
  8262. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8263. ldata = *src;
  8264. ldata = le32_to_cpu(ldata);
  8265. *dest = ldata;
  8266. src++;
  8267. dest++;
  8268. }
  8269. }
  8270. /**
  8271. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8272. * @srcp: Source memory pointer.
  8273. * @destp: Destination memory pointer.
  8274. * @cnt: Number of words required to be copied.
  8275. *
  8276. * This function is used for copying data between a data structure
  8277. * with big endian representation to local endianness.
  8278. * This function can be called with or without lock.
  8279. **/
  8280. void
  8281. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8282. {
  8283. uint32_t *src = srcp;
  8284. uint32_t *dest = destp;
  8285. uint32_t ldata;
  8286. int i;
  8287. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8288. ldata = *src;
  8289. ldata = be32_to_cpu(ldata);
  8290. *dest = ldata;
  8291. src++;
  8292. dest++;
  8293. }
  8294. }
  8295. /**
  8296. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8297. * @phba: Pointer to HBA context object.
  8298. * @pring: Pointer to driver SLI ring object.
  8299. * @mp: Pointer to driver buffer object.
  8300. *
  8301. * This function is called with no lock held.
  8302. * It always return zero after adding the buffer to the postbufq
  8303. * buffer list.
  8304. **/
  8305. int
  8306. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8307. struct lpfc_dmabuf *mp)
  8308. {
  8309. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8310. later */
  8311. spin_lock_irq(&phba->hbalock);
  8312. list_add_tail(&mp->list, &pring->postbufq);
  8313. pring->postbufq_cnt++;
  8314. spin_unlock_irq(&phba->hbalock);
  8315. return 0;
  8316. }
  8317. /**
  8318. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8319. * @phba: Pointer to HBA context object.
  8320. *
  8321. * When HBQ is enabled, buffers are searched based on tags. This function
  8322. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8323. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8324. * does not conflict with tags of buffer posted for unsolicited events.
  8325. * The function returns the allocated tag. The function is called with
  8326. * no locks held.
  8327. **/
  8328. uint32_t
  8329. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8330. {
  8331. spin_lock_irq(&phba->hbalock);
  8332. phba->buffer_tag_count++;
  8333. /*
  8334. * Always set the QUE_BUFTAG_BIT to distiguish between
  8335. * a tag assigned by HBQ.
  8336. */
  8337. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8338. spin_unlock_irq(&phba->hbalock);
  8339. return phba->buffer_tag_count;
  8340. }
  8341. /**
  8342. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8343. * @phba: Pointer to HBA context object.
  8344. * @pring: Pointer to driver SLI ring object.
  8345. * @tag: Buffer tag.
  8346. *
  8347. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8348. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8349. * iocb is posted to the response ring with the tag of the buffer.
  8350. * This function searches the pring->postbufq list using the tag
  8351. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8352. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8353. * buffer is returned to the caller else NULL is returned.
  8354. * This function is called with no lock held.
  8355. **/
  8356. struct lpfc_dmabuf *
  8357. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8358. uint32_t tag)
  8359. {
  8360. struct lpfc_dmabuf *mp, *next_mp;
  8361. struct list_head *slp = &pring->postbufq;
  8362. /* Search postbufq, from the beginning, looking for a match on tag */
  8363. spin_lock_irq(&phba->hbalock);
  8364. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8365. if (mp->buffer_tag == tag) {
  8366. list_del_init(&mp->list);
  8367. pring->postbufq_cnt--;
  8368. spin_unlock_irq(&phba->hbalock);
  8369. return mp;
  8370. }
  8371. }
  8372. spin_unlock_irq(&phba->hbalock);
  8373. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8374. "0402 Cannot find virtual addr for buffer tag on "
  8375. "ring %d Data x%lx x%p x%p x%x\n",
  8376. pring->ringno, (unsigned long) tag,
  8377. slp->next, slp->prev, pring->postbufq_cnt);
  8378. return NULL;
  8379. }
  8380. /**
  8381. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8382. * @phba: Pointer to HBA context object.
  8383. * @pring: Pointer to driver SLI ring object.
  8384. * @phys: DMA address of the buffer.
  8385. *
  8386. * This function searches the buffer list using the dma_address
  8387. * of unsolicited event to find the driver's lpfc_dmabuf object
  8388. * corresponding to the dma_address. The function returns the
  8389. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8390. * This function is called by the ct and els unsolicited event
  8391. * handlers to get the buffer associated with the unsolicited
  8392. * event.
  8393. *
  8394. * This function is called with no lock held.
  8395. **/
  8396. struct lpfc_dmabuf *
  8397. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8398. dma_addr_t phys)
  8399. {
  8400. struct lpfc_dmabuf *mp, *next_mp;
  8401. struct list_head *slp = &pring->postbufq;
  8402. /* Search postbufq, from the beginning, looking for a match on phys */
  8403. spin_lock_irq(&phba->hbalock);
  8404. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8405. if (mp->phys == phys) {
  8406. list_del_init(&mp->list);
  8407. pring->postbufq_cnt--;
  8408. spin_unlock_irq(&phba->hbalock);
  8409. return mp;
  8410. }
  8411. }
  8412. spin_unlock_irq(&phba->hbalock);
  8413. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8414. "0410 Cannot find virtual addr for mapped buf on "
  8415. "ring %d Data x%llx x%p x%p x%x\n",
  8416. pring->ringno, (unsigned long long)phys,
  8417. slp->next, slp->prev, pring->postbufq_cnt);
  8418. return NULL;
  8419. }
  8420. /**
  8421. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8422. * @phba: Pointer to HBA context object.
  8423. * @cmdiocb: Pointer to driver command iocb object.
  8424. * @rspiocb: Pointer to driver response iocb object.
  8425. *
  8426. * This function is the completion handler for the abort iocbs for
  8427. * ELS commands. This function is called from the ELS ring event
  8428. * handler with no lock held. This function frees memory resources
  8429. * associated with the abort iocb.
  8430. **/
  8431. static void
  8432. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8433. struct lpfc_iocbq *rspiocb)
  8434. {
  8435. IOCB_t *irsp = &rspiocb->iocb;
  8436. uint16_t abort_iotag, abort_context;
  8437. struct lpfc_iocbq *abort_iocb = NULL;
  8438. if (irsp->ulpStatus) {
  8439. /*
  8440. * Assume that the port already completed and returned, or
  8441. * will return the iocb. Just Log the message.
  8442. */
  8443. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8444. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8445. spin_lock_irq(&phba->hbalock);
  8446. if (phba->sli_rev < LPFC_SLI_REV4) {
  8447. if (abort_iotag != 0 &&
  8448. abort_iotag <= phba->sli.last_iotag)
  8449. abort_iocb =
  8450. phba->sli.iocbq_lookup[abort_iotag];
  8451. } else
  8452. /* For sli4 the abort_tag is the XRI,
  8453. * so the abort routine puts the iotag of the iocb
  8454. * being aborted in the context field of the abort
  8455. * IOCB.
  8456. */
  8457. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8458. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8459. "0327 Cannot abort els iocb %p "
  8460. "with tag %x context %x, abort status %x, "
  8461. "abort code %x\n",
  8462. abort_iocb, abort_iotag, abort_context,
  8463. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8464. spin_unlock_irq(&phba->hbalock);
  8465. }
  8466. lpfc_sli_release_iocbq(phba, cmdiocb);
  8467. return;
  8468. }
  8469. /**
  8470. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8471. * @phba: Pointer to HBA context object.
  8472. * @cmdiocb: Pointer to driver command iocb object.
  8473. * @rspiocb: Pointer to driver response iocb object.
  8474. *
  8475. * The function is called from SLI ring event handler with no
  8476. * lock held. This function is the completion handler for ELS commands
  8477. * which are aborted. The function frees memory resources used for
  8478. * the aborted ELS commands.
  8479. **/
  8480. static void
  8481. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8482. struct lpfc_iocbq *rspiocb)
  8483. {
  8484. IOCB_t *irsp = &rspiocb->iocb;
  8485. /* ELS cmd tag <ulpIoTag> completes */
  8486. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8487. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8488. "x%x x%x x%x\n",
  8489. irsp->ulpIoTag, irsp->ulpStatus,
  8490. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8491. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8492. lpfc_ct_free_iocb(phba, cmdiocb);
  8493. else
  8494. lpfc_els_free_iocb(phba, cmdiocb);
  8495. return;
  8496. }
  8497. /**
  8498. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8499. * @phba: Pointer to HBA context object.
  8500. * @pring: Pointer to driver SLI ring object.
  8501. * @cmdiocb: Pointer to driver command iocb object.
  8502. *
  8503. * This function issues an abort iocb for the provided command iocb down to
  8504. * the port. Other than the case the outstanding command iocb is an abort
  8505. * request, this function issues abort out unconditionally. This function is
  8506. * called with hbalock held. The function returns 0 when it fails due to
  8507. * memory allocation failure or when the command iocb is an abort request.
  8508. **/
  8509. static int
  8510. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8511. struct lpfc_iocbq *cmdiocb)
  8512. {
  8513. struct lpfc_vport *vport = cmdiocb->vport;
  8514. struct lpfc_iocbq *abtsiocbp;
  8515. IOCB_t *icmd = NULL;
  8516. IOCB_t *iabt = NULL;
  8517. int retval;
  8518. /*
  8519. * There are certain command types we don't want to abort. And we
  8520. * don't want to abort commands that are already in the process of
  8521. * being aborted.
  8522. */
  8523. icmd = &cmdiocb->iocb;
  8524. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8525. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8526. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8527. return 0;
  8528. /* issue ABTS for this IOCB based on iotag */
  8529. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8530. if (abtsiocbp == NULL)
  8531. return 0;
  8532. /* This signals the response to set the correct status
  8533. * before calling the completion handler
  8534. */
  8535. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8536. iabt = &abtsiocbp->iocb;
  8537. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8538. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8539. if (phba->sli_rev == LPFC_SLI_REV4) {
  8540. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8541. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8542. }
  8543. else
  8544. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8545. iabt->ulpLe = 1;
  8546. iabt->ulpClass = icmd->ulpClass;
  8547. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8548. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8549. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8550. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8551. if (phba->link_state >= LPFC_LINK_UP)
  8552. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8553. else
  8554. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8555. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8556. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8557. "0339 Abort xri x%x, original iotag x%x, "
  8558. "abort cmd iotag x%x\n",
  8559. iabt->un.acxri.abortIoTag,
  8560. iabt->un.acxri.abortContextTag,
  8561. abtsiocbp->iotag);
  8562. retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
  8563. if (retval)
  8564. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8565. /*
  8566. * Caller to this routine should check for IOCB_ERROR
  8567. * and handle it properly. This routine no longer removes
  8568. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8569. */
  8570. return retval;
  8571. }
  8572. /**
  8573. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8574. * @phba: Pointer to HBA context object.
  8575. * @pring: Pointer to driver SLI ring object.
  8576. * @cmdiocb: Pointer to driver command iocb object.
  8577. *
  8578. * This function issues an abort iocb for the provided command iocb. In case
  8579. * of unloading, the abort iocb will not be issued to commands on the ELS
  8580. * ring. Instead, the callback function shall be changed to those commands
  8581. * so that nothing happens when them finishes. This function is called with
  8582. * hbalock held. The function returns 0 when the command iocb is an abort
  8583. * request.
  8584. **/
  8585. int
  8586. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8587. struct lpfc_iocbq *cmdiocb)
  8588. {
  8589. struct lpfc_vport *vport = cmdiocb->vport;
  8590. int retval = IOCB_ERROR;
  8591. IOCB_t *icmd = NULL;
  8592. /*
  8593. * There are certain command types we don't want to abort. And we
  8594. * don't want to abort commands that are already in the process of
  8595. * being aborted.
  8596. */
  8597. icmd = &cmdiocb->iocb;
  8598. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8599. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8600. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8601. return 0;
  8602. /*
  8603. * If we're unloading, don't abort iocb on the ELS ring, but change
  8604. * the callback so that nothing happens when it finishes.
  8605. */
  8606. if ((vport->load_flag & FC_UNLOADING) &&
  8607. (pring->ringno == LPFC_ELS_RING)) {
  8608. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8609. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8610. else
  8611. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8612. goto abort_iotag_exit;
  8613. }
  8614. /* Now, we try to issue the abort to the cmdiocb out */
  8615. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8616. abort_iotag_exit:
  8617. /*
  8618. * Caller to this routine should check for IOCB_ERROR
  8619. * and handle it properly. This routine no longer removes
  8620. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8621. */
  8622. return retval;
  8623. }
  8624. /**
  8625. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8626. * @phba: Pointer to HBA context object.
  8627. * @pring: Pointer to driver SLI ring object.
  8628. *
  8629. * This function aborts all iocbs in the given ring and frees all the iocb
  8630. * objects in txq. This function issues abort iocbs unconditionally for all
  8631. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8632. * to complete before the return of this function. The caller is not required
  8633. * to hold any locks.
  8634. **/
  8635. static void
  8636. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8637. {
  8638. LIST_HEAD(completions);
  8639. struct lpfc_iocbq *iocb, *next_iocb;
  8640. if (pring->ringno == LPFC_ELS_RING)
  8641. lpfc_fabric_abort_hba(phba);
  8642. spin_lock_irq(&phba->hbalock);
  8643. /* Take off all the iocbs on txq for cancelling */
  8644. list_splice_init(&pring->txq, &completions);
  8645. pring->txq_cnt = 0;
  8646. /* Next issue ABTS for everything on the txcmplq */
  8647. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8648. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8649. spin_unlock_irq(&phba->hbalock);
  8650. /* Cancel all the IOCBs from the completions list */
  8651. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8652. IOERR_SLI_ABORTED);
  8653. }
  8654. /**
  8655. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8656. * @phba: pointer to lpfc HBA data structure.
  8657. *
  8658. * This routine will abort all pending and outstanding iocbs to an HBA.
  8659. **/
  8660. void
  8661. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8662. {
  8663. struct lpfc_sli *psli = &phba->sli;
  8664. struct lpfc_sli_ring *pring;
  8665. int i;
  8666. for (i = 0; i < psli->num_rings; i++) {
  8667. pring = &psli->ring[i];
  8668. lpfc_sli_iocb_ring_abort(phba, pring);
  8669. }
  8670. }
  8671. /**
  8672. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8673. * @iocbq: Pointer to driver iocb object.
  8674. * @vport: Pointer to driver virtual port object.
  8675. * @tgt_id: SCSI ID of the target.
  8676. * @lun_id: LUN ID of the scsi device.
  8677. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8678. *
  8679. * This function acts as an iocb filter for functions which abort or count
  8680. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8681. * 0 if the filtering criteria is met for the given iocb and will return
  8682. * 1 if the filtering criteria is not met.
  8683. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8684. * given iocb is for the SCSI device specified by vport, tgt_id and
  8685. * lun_id parameter.
  8686. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8687. * given iocb is for the SCSI target specified by vport and tgt_id
  8688. * parameters.
  8689. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8690. * given iocb is for the SCSI host associated with the given vport.
  8691. * This function is called with no locks held.
  8692. **/
  8693. static int
  8694. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8695. uint16_t tgt_id, uint64_t lun_id,
  8696. lpfc_ctx_cmd ctx_cmd)
  8697. {
  8698. struct lpfc_scsi_buf *lpfc_cmd;
  8699. int rc = 1;
  8700. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8701. return rc;
  8702. if (iocbq->vport != vport)
  8703. return rc;
  8704. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8705. if (lpfc_cmd->pCmd == NULL)
  8706. return rc;
  8707. switch (ctx_cmd) {
  8708. case LPFC_CTX_LUN:
  8709. if ((lpfc_cmd->rdata->pnode) &&
  8710. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8711. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8712. rc = 0;
  8713. break;
  8714. case LPFC_CTX_TGT:
  8715. if ((lpfc_cmd->rdata->pnode) &&
  8716. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8717. rc = 0;
  8718. break;
  8719. case LPFC_CTX_HOST:
  8720. rc = 0;
  8721. break;
  8722. default:
  8723. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8724. __func__, ctx_cmd);
  8725. break;
  8726. }
  8727. return rc;
  8728. }
  8729. /**
  8730. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8731. * @vport: Pointer to virtual port.
  8732. * @tgt_id: SCSI ID of the target.
  8733. * @lun_id: LUN ID of the scsi device.
  8734. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8735. *
  8736. * This function returns number of FCP commands pending for the vport.
  8737. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8738. * commands pending on the vport associated with SCSI device specified
  8739. * by tgt_id and lun_id parameters.
  8740. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8741. * commands pending on the vport associated with SCSI target specified
  8742. * by tgt_id parameter.
  8743. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8744. * commands pending on the vport.
  8745. * This function returns the number of iocbs which satisfy the filter.
  8746. * This function is called without any lock held.
  8747. **/
  8748. int
  8749. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8750. lpfc_ctx_cmd ctx_cmd)
  8751. {
  8752. struct lpfc_hba *phba = vport->phba;
  8753. struct lpfc_iocbq *iocbq;
  8754. int sum, i;
  8755. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8756. iocbq = phba->sli.iocbq_lookup[i];
  8757. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8758. ctx_cmd) == 0)
  8759. sum++;
  8760. }
  8761. return sum;
  8762. }
  8763. /**
  8764. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8765. * @phba: Pointer to HBA context object
  8766. * @cmdiocb: Pointer to command iocb object.
  8767. * @rspiocb: Pointer to response iocb object.
  8768. *
  8769. * This function is called when an aborted FCP iocb completes. This
  8770. * function is called by the ring event handler with no lock held.
  8771. * This function frees the iocb.
  8772. **/
  8773. void
  8774. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8775. struct lpfc_iocbq *rspiocb)
  8776. {
  8777. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8778. "3096 ABORT_XRI_CN completing on xri x%x "
  8779. "original iotag x%x, abort cmd iotag x%x "
  8780. "status 0x%x, reason 0x%x\n",
  8781. cmdiocb->iocb.un.acxri.abortContextTag,
  8782. cmdiocb->iocb.un.acxri.abortIoTag,
  8783. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  8784. rspiocb->iocb.un.ulpWord[4]);
  8785. lpfc_sli_release_iocbq(phba, cmdiocb);
  8786. return;
  8787. }
  8788. /**
  8789. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8790. * @vport: Pointer to virtual port.
  8791. * @pring: Pointer to driver SLI ring object.
  8792. * @tgt_id: SCSI ID of the target.
  8793. * @lun_id: LUN ID of the scsi device.
  8794. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8795. *
  8796. * This function sends an abort command for every SCSI command
  8797. * associated with the given virtual port pending on the ring
  8798. * filtered by lpfc_sli_validate_fcp_iocb function.
  8799. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8800. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8801. * parameters
  8802. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8803. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8804. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8805. * FCP iocbs associated with virtual port.
  8806. * This function returns number of iocbs it failed to abort.
  8807. * This function is called with no locks held.
  8808. **/
  8809. int
  8810. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8811. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8812. {
  8813. struct lpfc_hba *phba = vport->phba;
  8814. struct lpfc_iocbq *iocbq;
  8815. struct lpfc_iocbq *abtsiocb;
  8816. IOCB_t *cmd = NULL;
  8817. int errcnt = 0, ret_val = 0;
  8818. int i;
  8819. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8820. iocbq = phba->sli.iocbq_lookup[i];
  8821. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8822. abort_cmd) != 0)
  8823. continue;
  8824. /* issue ABTS for this IOCB based on iotag */
  8825. abtsiocb = lpfc_sli_get_iocbq(phba);
  8826. if (abtsiocb == NULL) {
  8827. errcnt++;
  8828. continue;
  8829. }
  8830. cmd = &iocbq->iocb;
  8831. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8832. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8833. if (phba->sli_rev == LPFC_SLI_REV4)
  8834. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8835. else
  8836. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8837. abtsiocb->iocb.ulpLe = 1;
  8838. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8839. abtsiocb->vport = phba->pport;
  8840. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8841. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8842. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8843. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8844. if (lpfc_is_link_up(phba))
  8845. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8846. else
  8847. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8848. /* Setup callback routine and issue the command. */
  8849. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8850. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8851. abtsiocb, 0);
  8852. if (ret_val == IOCB_ERROR) {
  8853. lpfc_sli_release_iocbq(phba, abtsiocb);
  8854. errcnt++;
  8855. continue;
  8856. }
  8857. }
  8858. return errcnt;
  8859. }
  8860. /**
  8861. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8862. * @phba: Pointer to HBA context object.
  8863. * @cmdiocbq: Pointer to command iocb.
  8864. * @rspiocbq: Pointer to response iocb.
  8865. *
  8866. * This function is the completion handler for iocbs issued using
  8867. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8868. * ring event handler function without any lock held. This function
  8869. * can be called from both worker thread context and interrupt
  8870. * context. This function also can be called from other thread which
  8871. * cleans up the SLI layer objects.
  8872. * This function copy the contents of the response iocb to the
  8873. * response iocb memory object provided by the caller of
  8874. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8875. * sleeps for the iocb completion.
  8876. **/
  8877. static void
  8878. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8879. struct lpfc_iocbq *cmdiocbq,
  8880. struct lpfc_iocbq *rspiocbq)
  8881. {
  8882. wait_queue_head_t *pdone_q;
  8883. unsigned long iflags;
  8884. struct lpfc_scsi_buf *lpfc_cmd;
  8885. spin_lock_irqsave(&phba->hbalock, iflags);
  8886. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8887. if (cmdiocbq->context2 && rspiocbq)
  8888. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8889. &rspiocbq->iocb, sizeof(IOCB_t));
  8890. /* Set the exchange busy flag for task management commands */
  8891. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8892. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  8893. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  8894. cur_iocbq);
  8895. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  8896. }
  8897. pdone_q = cmdiocbq->context_un.wait_queue;
  8898. if (pdone_q)
  8899. wake_up(pdone_q);
  8900. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8901. return;
  8902. }
  8903. /**
  8904. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  8905. * @phba: Pointer to HBA context object..
  8906. * @piocbq: Pointer to command iocb.
  8907. * @flag: Flag to test.
  8908. *
  8909. * This routine grabs the hbalock and then test the iocb_flag to
  8910. * see if the passed in flag is set.
  8911. * Returns:
  8912. * 1 if flag is set.
  8913. * 0 if flag is not set.
  8914. **/
  8915. static int
  8916. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  8917. struct lpfc_iocbq *piocbq, uint32_t flag)
  8918. {
  8919. unsigned long iflags;
  8920. int ret;
  8921. spin_lock_irqsave(&phba->hbalock, iflags);
  8922. ret = piocbq->iocb_flag & flag;
  8923. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8924. return ret;
  8925. }
  8926. /**
  8927. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  8928. * @phba: Pointer to HBA context object..
  8929. * @pring: Pointer to sli ring.
  8930. * @piocb: Pointer to command iocb.
  8931. * @prspiocbq: Pointer to response iocb.
  8932. * @timeout: Timeout in number of seconds.
  8933. *
  8934. * This function issues the iocb to firmware and waits for the
  8935. * iocb to complete. If the iocb command is not
  8936. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  8937. * Caller should not free the iocb resources if this function
  8938. * returns IOCB_TIMEDOUT.
  8939. * The function waits for the iocb completion using an
  8940. * non-interruptible wait.
  8941. * This function will sleep while waiting for iocb completion.
  8942. * So, this function should not be called from any context which
  8943. * does not allow sleeping. Due to the same reason, this function
  8944. * cannot be called with interrupt disabled.
  8945. * This function assumes that the iocb completions occur while
  8946. * this function sleep. So, this function cannot be called from
  8947. * the thread which process iocb completion for this ring.
  8948. * This function clears the iocb_flag of the iocb object before
  8949. * issuing the iocb and the iocb completion handler sets this
  8950. * flag and wakes this thread when the iocb completes.
  8951. * The contents of the response iocb will be copied to prspiocbq
  8952. * by the completion handler when the command completes.
  8953. * This function returns IOCB_SUCCESS when success.
  8954. * This function is called with no lock held.
  8955. **/
  8956. int
  8957. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  8958. uint32_t ring_number,
  8959. struct lpfc_iocbq *piocb,
  8960. struct lpfc_iocbq *prspiocbq,
  8961. uint32_t timeout)
  8962. {
  8963. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8964. long timeleft, timeout_req = 0;
  8965. int retval = IOCB_SUCCESS;
  8966. uint32_t creg_val;
  8967. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  8968. /*
  8969. * If the caller has provided a response iocbq buffer, then context2
  8970. * is NULL or its an error.
  8971. */
  8972. if (prspiocbq) {
  8973. if (piocb->context2)
  8974. return IOCB_ERROR;
  8975. piocb->context2 = prspiocbq;
  8976. }
  8977. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  8978. piocb->context_un.wait_queue = &done_q;
  8979. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  8980. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8981. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8982. return IOCB_ERROR;
  8983. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  8984. writel(creg_val, phba->HCregaddr);
  8985. readl(phba->HCregaddr); /* flush */
  8986. }
  8987. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  8988. SLI_IOCB_RET_IOCB);
  8989. if (retval == IOCB_SUCCESS) {
  8990. timeout_req = timeout * HZ;
  8991. timeleft = wait_event_timeout(done_q,
  8992. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  8993. timeout_req);
  8994. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  8995. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8996. "0331 IOCB wake signaled\n");
  8997. } else if (timeleft == 0) {
  8998. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8999. "0338 IOCB wait timeout error - no "
  9000. "wake response Data x%x\n", timeout);
  9001. retval = IOCB_TIMEDOUT;
  9002. } else {
  9003. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9004. "0330 IOCB wake NOT set, "
  9005. "Data x%x x%lx\n",
  9006. timeout, (timeleft / jiffies));
  9007. retval = IOCB_TIMEDOUT;
  9008. }
  9009. } else if (retval == IOCB_BUSY) {
  9010. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9011. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  9012. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  9013. return retval;
  9014. } else {
  9015. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9016. "0332 IOCB wait issue failed, Data x%x\n",
  9017. retval);
  9018. retval = IOCB_ERROR;
  9019. }
  9020. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9021. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9022. return IOCB_ERROR;
  9023. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  9024. writel(creg_val, phba->HCregaddr);
  9025. readl(phba->HCregaddr); /* flush */
  9026. }
  9027. if (prspiocbq)
  9028. piocb->context2 = NULL;
  9029. piocb->context_un.wait_queue = NULL;
  9030. piocb->iocb_cmpl = NULL;
  9031. return retval;
  9032. }
  9033. /**
  9034. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  9035. * @phba: Pointer to HBA context object.
  9036. * @pmboxq: Pointer to driver mailbox object.
  9037. * @timeout: Timeout in number of seconds.
  9038. *
  9039. * This function issues the mailbox to firmware and waits for the
  9040. * mailbox command to complete. If the mailbox command is not
  9041. * completed within timeout seconds, it returns MBX_TIMEOUT.
  9042. * The function waits for the mailbox completion using an
  9043. * interruptible wait. If the thread is woken up due to a
  9044. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  9045. * should not free the mailbox resources, if this function returns
  9046. * MBX_TIMEOUT.
  9047. * This function will sleep while waiting for mailbox completion.
  9048. * So, this function should not be called from any context which
  9049. * does not allow sleeping. Due to the same reason, this function
  9050. * cannot be called with interrupt disabled.
  9051. * This function assumes that the mailbox completion occurs while
  9052. * this function sleep. So, this function cannot be called from
  9053. * the worker thread which processes mailbox completion.
  9054. * This function is called in the context of HBA management
  9055. * applications.
  9056. * This function returns MBX_SUCCESS when successful.
  9057. * This function is called with no lock held.
  9058. **/
  9059. int
  9060. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  9061. uint32_t timeout)
  9062. {
  9063. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9064. int retval;
  9065. unsigned long flag;
  9066. /* The caller must leave context1 empty. */
  9067. if (pmboxq->context1)
  9068. return MBX_NOT_FINISHED;
  9069. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  9070. /* setup wake call as IOCB callback */
  9071. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  9072. /* setup context field to pass wait_queue pointer to wake function */
  9073. pmboxq->context1 = &done_q;
  9074. /* now issue the command */
  9075. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  9076. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  9077. wait_event_interruptible_timeout(done_q,
  9078. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  9079. timeout * HZ);
  9080. spin_lock_irqsave(&phba->hbalock, flag);
  9081. pmboxq->context1 = NULL;
  9082. /*
  9083. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  9084. * else do not free the resources.
  9085. */
  9086. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  9087. retval = MBX_SUCCESS;
  9088. lpfc_sli4_swap_str(phba, pmboxq);
  9089. } else {
  9090. retval = MBX_TIMEOUT;
  9091. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9092. }
  9093. spin_unlock_irqrestore(&phba->hbalock, flag);
  9094. }
  9095. return retval;
  9096. }
  9097. /**
  9098. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  9099. * @phba: Pointer to HBA context.
  9100. *
  9101. * This function is called to shutdown the driver's mailbox sub-system.
  9102. * It first marks the mailbox sub-system is in a block state to prevent
  9103. * the asynchronous mailbox command from issued off the pending mailbox
  9104. * command queue. If the mailbox command sub-system shutdown is due to
  9105. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  9106. * the mailbox sub-system flush routine to forcefully bring down the
  9107. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  9108. * as with offline or HBA function reset), this routine will wait for the
  9109. * outstanding mailbox command to complete before invoking the mailbox
  9110. * sub-system flush routine to gracefully bring down mailbox sub-system.
  9111. **/
  9112. void
  9113. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
  9114. {
  9115. struct lpfc_sli *psli = &phba->sli;
  9116. unsigned long timeout;
  9117. if (mbx_action == LPFC_MBX_NO_WAIT) {
  9118. /* delay 100ms for port state */
  9119. msleep(100);
  9120. lpfc_sli_mbox_sys_flush(phba);
  9121. return;
  9122. }
  9123. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  9124. spin_lock_irq(&phba->hbalock);
  9125. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  9126. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  9127. /* Determine how long we might wait for the active mailbox
  9128. * command to be gracefully completed by firmware.
  9129. */
  9130. if (phba->sli.mbox_active)
  9131. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  9132. phba->sli.mbox_active) *
  9133. 1000) + jiffies;
  9134. spin_unlock_irq(&phba->hbalock);
  9135. while (phba->sli.mbox_active) {
  9136. /* Check active mailbox complete status every 2ms */
  9137. msleep(2);
  9138. if (time_after(jiffies, timeout))
  9139. /* Timeout, let the mailbox flush routine to
  9140. * forcefully release active mailbox command
  9141. */
  9142. break;
  9143. }
  9144. } else
  9145. spin_unlock_irq(&phba->hbalock);
  9146. lpfc_sli_mbox_sys_flush(phba);
  9147. }
  9148. /**
  9149. * lpfc_sli_eratt_read - read sli-3 error attention events
  9150. * @phba: Pointer to HBA context.
  9151. *
  9152. * This function is called to read the SLI3 device error attention registers
  9153. * for possible error attention events. The caller must hold the hostlock
  9154. * with spin_lock_irq().
  9155. *
  9156. * This function returns 1 when there is Error Attention in the Host Attention
  9157. * Register and returns 0 otherwise.
  9158. **/
  9159. static int
  9160. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  9161. {
  9162. uint32_t ha_copy;
  9163. /* Read chip Host Attention (HA) register */
  9164. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9165. goto unplug_err;
  9166. if (ha_copy & HA_ERATT) {
  9167. /* Read host status register to retrieve error event */
  9168. if (lpfc_sli_read_hs(phba))
  9169. goto unplug_err;
  9170. /* Check if there is a deferred error condition is active */
  9171. if ((HS_FFER1 & phba->work_hs) &&
  9172. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9173. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9174. phba->hba_flag |= DEFER_ERATT;
  9175. /* Clear all interrupt enable conditions */
  9176. writel(0, phba->HCregaddr);
  9177. readl(phba->HCregaddr);
  9178. }
  9179. /* Set the driver HA work bitmap */
  9180. phba->work_ha |= HA_ERATT;
  9181. /* Indicate polling handles this ERATT */
  9182. phba->hba_flag |= HBA_ERATT_HANDLED;
  9183. return 1;
  9184. }
  9185. return 0;
  9186. unplug_err:
  9187. /* Set the driver HS work bitmap */
  9188. phba->work_hs |= UNPLUG_ERR;
  9189. /* Set the driver HA work bitmap */
  9190. phba->work_ha |= HA_ERATT;
  9191. /* Indicate polling handles this ERATT */
  9192. phba->hba_flag |= HBA_ERATT_HANDLED;
  9193. return 1;
  9194. }
  9195. /**
  9196. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9197. * @phba: Pointer to HBA context.
  9198. *
  9199. * This function is called to read the SLI4 device error attention registers
  9200. * for possible error attention events. The caller must hold the hostlock
  9201. * with spin_lock_irq().
  9202. *
  9203. * This function returns 1 when there is Error Attention in the Host Attention
  9204. * Register and returns 0 otherwise.
  9205. **/
  9206. static int
  9207. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9208. {
  9209. uint32_t uerr_sta_hi, uerr_sta_lo;
  9210. uint32_t if_type, portsmphr;
  9211. struct lpfc_register portstat_reg;
  9212. /*
  9213. * For now, use the SLI4 device internal unrecoverable error
  9214. * registers for error attention. This can be changed later.
  9215. */
  9216. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9217. switch (if_type) {
  9218. case LPFC_SLI_INTF_IF_TYPE_0:
  9219. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9220. &uerr_sta_lo) ||
  9221. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9222. &uerr_sta_hi)) {
  9223. phba->work_hs |= UNPLUG_ERR;
  9224. phba->work_ha |= HA_ERATT;
  9225. phba->hba_flag |= HBA_ERATT_HANDLED;
  9226. return 1;
  9227. }
  9228. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9229. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9230. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9231. "1423 HBA Unrecoverable error: "
  9232. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9233. "ue_mask_lo_reg=0x%x, "
  9234. "ue_mask_hi_reg=0x%x\n",
  9235. uerr_sta_lo, uerr_sta_hi,
  9236. phba->sli4_hba.ue_mask_lo,
  9237. phba->sli4_hba.ue_mask_hi);
  9238. phba->work_status[0] = uerr_sta_lo;
  9239. phba->work_status[1] = uerr_sta_hi;
  9240. phba->work_ha |= HA_ERATT;
  9241. phba->hba_flag |= HBA_ERATT_HANDLED;
  9242. return 1;
  9243. }
  9244. break;
  9245. case LPFC_SLI_INTF_IF_TYPE_2:
  9246. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9247. &portstat_reg.word0) ||
  9248. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9249. &portsmphr)){
  9250. phba->work_hs |= UNPLUG_ERR;
  9251. phba->work_ha |= HA_ERATT;
  9252. phba->hba_flag |= HBA_ERATT_HANDLED;
  9253. return 1;
  9254. }
  9255. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9256. phba->work_status[0] =
  9257. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9258. phba->work_status[1] =
  9259. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9260. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9261. "2885 Port Status Event: "
  9262. "port status reg 0x%x, "
  9263. "port smphr reg 0x%x, "
  9264. "error 1=0x%x, error 2=0x%x\n",
  9265. portstat_reg.word0,
  9266. portsmphr,
  9267. phba->work_status[0],
  9268. phba->work_status[1]);
  9269. phba->work_ha |= HA_ERATT;
  9270. phba->hba_flag |= HBA_ERATT_HANDLED;
  9271. return 1;
  9272. }
  9273. break;
  9274. case LPFC_SLI_INTF_IF_TYPE_1:
  9275. default:
  9276. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9277. "2886 HBA Error Attention on unsupported "
  9278. "if type %d.", if_type);
  9279. return 1;
  9280. }
  9281. return 0;
  9282. }
  9283. /**
  9284. * lpfc_sli_check_eratt - check error attention events
  9285. * @phba: Pointer to HBA context.
  9286. *
  9287. * This function is called from timer soft interrupt context to check HBA's
  9288. * error attention register bit for error attention events.
  9289. *
  9290. * This function returns 1 when there is Error Attention in the Host Attention
  9291. * Register and returns 0 otherwise.
  9292. **/
  9293. int
  9294. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9295. {
  9296. uint32_t ha_copy;
  9297. /* If somebody is waiting to handle an eratt, don't process it
  9298. * here. The brdkill function will do this.
  9299. */
  9300. if (phba->link_flag & LS_IGNORE_ERATT)
  9301. return 0;
  9302. /* Check if interrupt handler handles this ERATT */
  9303. spin_lock_irq(&phba->hbalock);
  9304. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9305. /* Interrupt handler has handled ERATT */
  9306. spin_unlock_irq(&phba->hbalock);
  9307. return 0;
  9308. }
  9309. /*
  9310. * If there is deferred error attention, do not check for error
  9311. * attention
  9312. */
  9313. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9314. spin_unlock_irq(&phba->hbalock);
  9315. return 0;
  9316. }
  9317. /* If PCI channel is offline, don't process it */
  9318. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9319. spin_unlock_irq(&phba->hbalock);
  9320. return 0;
  9321. }
  9322. switch (phba->sli_rev) {
  9323. case LPFC_SLI_REV2:
  9324. case LPFC_SLI_REV3:
  9325. /* Read chip Host Attention (HA) register */
  9326. ha_copy = lpfc_sli_eratt_read(phba);
  9327. break;
  9328. case LPFC_SLI_REV4:
  9329. /* Read device Uncoverable Error (UERR) registers */
  9330. ha_copy = lpfc_sli4_eratt_read(phba);
  9331. break;
  9332. default:
  9333. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9334. "0299 Invalid SLI revision (%d)\n",
  9335. phba->sli_rev);
  9336. ha_copy = 0;
  9337. break;
  9338. }
  9339. spin_unlock_irq(&phba->hbalock);
  9340. return ha_copy;
  9341. }
  9342. /**
  9343. * lpfc_intr_state_check - Check device state for interrupt handling
  9344. * @phba: Pointer to HBA context.
  9345. *
  9346. * This inline routine checks whether a device or its PCI slot is in a state
  9347. * that the interrupt should be handled.
  9348. *
  9349. * This function returns 0 if the device or the PCI slot is in a state that
  9350. * interrupt should be handled, otherwise -EIO.
  9351. */
  9352. static inline int
  9353. lpfc_intr_state_check(struct lpfc_hba *phba)
  9354. {
  9355. /* If the pci channel is offline, ignore all the interrupts */
  9356. if (unlikely(pci_channel_offline(phba->pcidev)))
  9357. return -EIO;
  9358. /* Update device level interrupt statistics */
  9359. phba->sli.slistat.sli_intr++;
  9360. /* Ignore all interrupts during initialization. */
  9361. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9362. return -EIO;
  9363. return 0;
  9364. }
  9365. /**
  9366. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9367. * @irq: Interrupt number.
  9368. * @dev_id: The device context pointer.
  9369. *
  9370. * This function is directly called from the PCI layer as an interrupt
  9371. * service routine when device with SLI-3 interface spec is enabled with
  9372. * MSI-X multi-message interrupt mode and there are slow-path events in
  9373. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9374. * interrupt mode, this function is called as part of the device-level
  9375. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9376. * is undergoing initialization, the interrupt handler will not process
  9377. * the interrupt. The link attention and ELS ring attention events are
  9378. * handled by the worker thread. The interrupt handler signals the worker
  9379. * thread and returns for these events. This function is called without
  9380. * any lock held. It gets the hbalock to access and update SLI data
  9381. * structures.
  9382. *
  9383. * This function returns IRQ_HANDLED when interrupt is handled else it
  9384. * returns IRQ_NONE.
  9385. **/
  9386. irqreturn_t
  9387. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9388. {
  9389. struct lpfc_hba *phba;
  9390. uint32_t ha_copy, hc_copy;
  9391. uint32_t work_ha_copy;
  9392. unsigned long status;
  9393. unsigned long iflag;
  9394. uint32_t control;
  9395. MAILBOX_t *mbox, *pmbox;
  9396. struct lpfc_vport *vport;
  9397. struct lpfc_nodelist *ndlp;
  9398. struct lpfc_dmabuf *mp;
  9399. LPFC_MBOXQ_t *pmb;
  9400. int rc;
  9401. /*
  9402. * Get the driver's phba structure from the dev_id and
  9403. * assume the HBA is not interrupting.
  9404. */
  9405. phba = (struct lpfc_hba *)dev_id;
  9406. if (unlikely(!phba))
  9407. return IRQ_NONE;
  9408. /*
  9409. * Stuff needs to be attented to when this function is invoked as an
  9410. * individual interrupt handler in MSI-X multi-message interrupt mode
  9411. */
  9412. if (phba->intr_type == MSIX) {
  9413. /* Check device state for handling interrupt */
  9414. if (lpfc_intr_state_check(phba))
  9415. return IRQ_NONE;
  9416. /* Need to read HA REG for slow-path events */
  9417. spin_lock_irqsave(&phba->hbalock, iflag);
  9418. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9419. goto unplug_error;
  9420. /* If somebody is waiting to handle an eratt don't process it
  9421. * here. The brdkill function will do this.
  9422. */
  9423. if (phba->link_flag & LS_IGNORE_ERATT)
  9424. ha_copy &= ~HA_ERATT;
  9425. /* Check the need for handling ERATT in interrupt handler */
  9426. if (ha_copy & HA_ERATT) {
  9427. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9428. /* ERATT polling has handled ERATT */
  9429. ha_copy &= ~HA_ERATT;
  9430. else
  9431. /* Indicate interrupt handler handles ERATT */
  9432. phba->hba_flag |= HBA_ERATT_HANDLED;
  9433. }
  9434. /*
  9435. * If there is deferred error attention, do not check for any
  9436. * interrupt.
  9437. */
  9438. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9439. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9440. return IRQ_NONE;
  9441. }
  9442. /* Clear up only attention source related to slow-path */
  9443. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9444. goto unplug_error;
  9445. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9446. HC_LAINT_ENA | HC_ERINT_ENA),
  9447. phba->HCregaddr);
  9448. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9449. phba->HAregaddr);
  9450. writel(hc_copy, phba->HCregaddr);
  9451. readl(phba->HAregaddr); /* flush */
  9452. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9453. } else
  9454. ha_copy = phba->ha_copy;
  9455. work_ha_copy = ha_copy & phba->work_ha_mask;
  9456. if (work_ha_copy) {
  9457. if (work_ha_copy & HA_LATT) {
  9458. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9459. /*
  9460. * Turn off Link Attention interrupts
  9461. * until CLEAR_LA done
  9462. */
  9463. spin_lock_irqsave(&phba->hbalock, iflag);
  9464. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9465. if (lpfc_readl(phba->HCregaddr, &control))
  9466. goto unplug_error;
  9467. control &= ~HC_LAINT_ENA;
  9468. writel(control, phba->HCregaddr);
  9469. readl(phba->HCregaddr); /* flush */
  9470. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9471. }
  9472. else
  9473. work_ha_copy &= ~HA_LATT;
  9474. }
  9475. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9476. /*
  9477. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9478. * the only slow ring.
  9479. */
  9480. status = (work_ha_copy &
  9481. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9482. status >>= (4*LPFC_ELS_RING);
  9483. if (status & HA_RXMASK) {
  9484. spin_lock_irqsave(&phba->hbalock, iflag);
  9485. if (lpfc_readl(phba->HCregaddr, &control))
  9486. goto unplug_error;
  9487. lpfc_debugfs_slow_ring_trc(phba,
  9488. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9489. control, status,
  9490. (uint32_t)phba->sli.slistat.sli_intr);
  9491. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9492. lpfc_debugfs_slow_ring_trc(phba,
  9493. "ISR Disable ring:"
  9494. "pwork:x%x hawork:x%x wait:x%x",
  9495. phba->work_ha, work_ha_copy,
  9496. (uint32_t)((unsigned long)
  9497. &phba->work_waitq));
  9498. control &=
  9499. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9500. writel(control, phba->HCregaddr);
  9501. readl(phba->HCregaddr); /* flush */
  9502. }
  9503. else {
  9504. lpfc_debugfs_slow_ring_trc(phba,
  9505. "ISR slow ring: pwork:"
  9506. "x%x hawork:x%x wait:x%x",
  9507. phba->work_ha, work_ha_copy,
  9508. (uint32_t)((unsigned long)
  9509. &phba->work_waitq));
  9510. }
  9511. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9512. }
  9513. }
  9514. spin_lock_irqsave(&phba->hbalock, iflag);
  9515. if (work_ha_copy & HA_ERATT) {
  9516. if (lpfc_sli_read_hs(phba))
  9517. goto unplug_error;
  9518. /*
  9519. * Check if there is a deferred error condition
  9520. * is active
  9521. */
  9522. if ((HS_FFER1 & phba->work_hs) &&
  9523. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9524. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9525. phba->work_hs)) {
  9526. phba->hba_flag |= DEFER_ERATT;
  9527. /* Clear all interrupt enable conditions */
  9528. writel(0, phba->HCregaddr);
  9529. readl(phba->HCregaddr);
  9530. }
  9531. }
  9532. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9533. pmb = phba->sli.mbox_active;
  9534. pmbox = &pmb->u.mb;
  9535. mbox = phba->mbox;
  9536. vport = pmb->vport;
  9537. /* First check out the status word */
  9538. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9539. if (pmbox->mbxOwner != OWN_HOST) {
  9540. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9541. /*
  9542. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9543. * mbxStatus <status>
  9544. */
  9545. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9546. LOG_SLI,
  9547. "(%d):0304 Stray Mailbox "
  9548. "Interrupt mbxCommand x%x "
  9549. "mbxStatus x%x\n",
  9550. (vport ? vport->vpi : 0),
  9551. pmbox->mbxCommand,
  9552. pmbox->mbxStatus);
  9553. /* clear mailbox attention bit */
  9554. work_ha_copy &= ~HA_MBATT;
  9555. } else {
  9556. phba->sli.mbox_active = NULL;
  9557. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9558. phba->last_completion_time = jiffies;
  9559. del_timer(&phba->sli.mbox_tmo);
  9560. if (pmb->mbox_cmpl) {
  9561. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9562. MAILBOX_CMD_SIZE);
  9563. if (pmb->out_ext_byte_len &&
  9564. pmb->context2)
  9565. lpfc_sli_pcimem_bcopy(
  9566. phba->mbox_ext,
  9567. pmb->context2,
  9568. pmb->out_ext_byte_len);
  9569. }
  9570. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9571. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9572. lpfc_debugfs_disc_trc(vport,
  9573. LPFC_DISC_TRC_MBOX_VPORT,
  9574. "MBOX dflt rpi: : "
  9575. "status:x%x rpi:x%x",
  9576. (uint32_t)pmbox->mbxStatus,
  9577. pmbox->un.varWords[0], 0);
  9578. if (!pmbox->mbxStatus) {
  9579. mp = (struct lpfc_dmabuf *)
  9580. (pmb->context1);
  9581. ndlp = (struct lpfc_nodelist *)
  9582. pmb->context2;
  9583. /* Reg_LOGIN of dflt RPI was
  9584. * successful. new lets get
  9585. * rid of the RPI using the
  9586. * same mbox buffer.
  9587. */
  9588. lpfc_unreg_login(phba,
  9589. vport->vpi,
  9590. pmbox->un.varWords[0],
  9591. pmb);
  9592. pmb->mbox_cmpl =
  9593. lpfc_mbx_cmpl_dflt_rpi;
  9594. pmb->context1 = mp;
  9595. pmb->context2 = ndlp;
  9596. pmb->vport = vport;
  9597. rc = lpfc_sli_issue_mbox(phba,
  9598. pmb,
  9599. MBX_NOWAIT);
  9600. if (rc != MBX_BUSY)
  9601. lpfc_printf_log(phba,
  9602. KERN_ERR,
  9603. LOG_MBOX | LOG_SLI,
  9604. "0350 rc should have"
  9605. "been MBX_BUSY\n");
  9606. if (rc != MBX_NOT_FINISHED)
  9607. goto send_current_mbox;
  9608. }
  9609. }
  9610. spin_lock_irqsave(
  9611. &phba->pport->work_port_lock,
  9612. iflag);
  9613. phba->pport->work_port_events &=
  9614. ~WORKER_MBOX_TMO;
  9615. spin_unlock_irqrestore(
  9616. &phba->pport->work_port_lock,
  9617. iflag);
  9618. lpfc_mbox_cmpl_put(phba, pmb);
  9619. }
  9620. } else
  9621. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9622. if ((work_ha_copy & HA_MBATT) &&
  9623. (phba->sli.mbox_active == NULL)) {
  9624. send_current_mbox:
  9625. /* Process next mailbox command if there is one */
  9626. do {
  9627. rc = lpfc_sli_issue_mbox(phba, NULL,
  9628. MBX_NOWAIT);
  9629. } while (rc == MBX_NOT_FINISHED);
  9630. if (rc != MBX_SUCCESS)
  9631. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9632. LOG_SLI, "0349 rc should be "
  9633. "MBX_SUCCESS\n");
  9634. }
  9635. spin_lock_irqsave(&phba->hbalock, iflag);
  9636. phba->work_ha |= work_ha_copy;
  9637. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9638. lpfc_worker_wake_up(phba);
  9639. }
  9640. return IRQ_HANDLED;
  9641. unplug_error:
  9642. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9643. return IRQ_HANDLED;
  9644. } /* lpfc_sli_sp_intr_handler */
  9645. /**
  9646. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9647. * @irq: Interrupt number.
  9648. * @dev_id: The device context pointer.
  9649. *
  9650. * This function is directly called from the PCI layer as an interrupt
  9651. * service routine when device with SLI-3 interface spec is enabled with
  9652. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9653. * ring event in the HBA. However, when the device is enabled with either
  9654. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9655. * device-level interrupt handler. When the PCI slot is in error recovery
  9656. * or the HBA is undergoing initialization, the interrupt handler will not
  9657. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9658. * the intrrupt context. This function is called without any lock held.
  9659. * It gets the hbalock to access and update SLI data structures.
  9660. *
  9661. * This function returns IRQ_HANDLED when interrupt is handled else it
  9662. * returns IRQ_NONE.
  9663. **/
  9664. irqreturn_t
  9665. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9666. {
  9667. struct lpfc_hba *phba;
  9668. uint32_t ha_copy;
  9669. unsigned long status;
  9670. unsigned long iflag;
  9671. /* Get the driver's phba structure from the dev_id and
  9672. * assume the HBA is not interrupting.
  9673. */
  9674. phba = (struct lpfc_hba *) dev_id;
  9675. if (unlikely(!phba))
  9676. return IRQ_NONE;
  9677. /*
  9678. * Stuff needs to be attented to when this function is invoked as an
  9679. * individual interrupt handler in MSI-X multi-message interrupt mode
  9680. */
  9681. if (phba->intr_type == MSIX) {
  9682. /* Check device state for handling interrupt */
  9683. if (lpfc_intr_state_check(phba))
  9684. return IRQ_NONE;
  9685. /* Need to read HA REG for FCP ring and other ring events */
  9686. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9687. return IRQ_HANDLED;
  9688. /* Clear up only attention source related to fast-path */
  9689. spin_lock_irqsave(&phba->hbalock, iflag);
  9690. /*
  9691. * If there is deferred error attention, do not check for
  9692. * any interrupt.
  9693. */
  9694. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9695. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9696. return IRQ_NONE;
  9697. }
  9698. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9699. phba->HAregaddr);
  9700. readl(phba->HAregaddr); /* flush */
  9701. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9702. } else
  9703. ha_copy = phba->ha_copy;
  9704. /*
  9705. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9706. */
  9707. ha_copy &= ~(phba->work_ha_mask);
  9708. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9709. status >>= (4*LPFC_FCP_RING);
  9710. if (status & HA_RXMASK)
  9711. lpfc_sli_handle_fast_ring_event(phba,
  9712. &phba->sli.ring[LPFC_FCP_RING],
  9713. status);
  9714. if (phba->cfg_multi_ring_support == 2) {
  9715. /*
  9716. * Process all events on extra ring. Take the optimized path
  9717. * for extra ring IO.
  9718. */
  9719. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9720. status >>= (4*LPFC_EXTRA_RING);
  9721. if (status & HA_RXMASK) {
  9722. lpfc_sli_handle_fast_ring_event(phba,
  9723. &phba->sli.ring[LPFC_EXTRA_RING],
  9724. status);
  9725. }
  9726. }
  9727. return IRQ_HANDLED;
  9728. } /* lpfc_sli_fp_intr_handler */
  9729. /**
  9730. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9731. * @irq: Interrupt number.
  9732. * @dev_id: The device context pointer.
  9733. *
  9734. * This function is the HBA device-level interrupt handler to device with
  9735. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9736. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9737. * requires driver attention. This function invokes the slow-path interrupt
  9738. * attention handling function and fast-path interrupt attention handling
  9739. * function in turn to process the relevant HBA attention events. This
  9740. * function is called without any lock held. It gets the hbalock to access
  9741. * and update SLI data structures.
  9742. *
  9743. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9744. * returns IRQ_NONE.
  9745. **/
  9746. irqreturn_t
  9747. lpfc_sli_intr_handler(int irq, void *dev_id)
  9748. {
  9749. struct lpfc_hba *phba;
  9750. irqreturn_t sp_irq_rc, fp_irq_rc;
  9751. unsigned long status1, status2;
  9752. uint32_t hc_copy;
  9753. /*
  9754. * Get the driver's phba structure from the dev_id and
  9755. * assume the HBA is not interrupting.
  9756. */
  9757. phba = (struct lpfc_hba *) dev_id;
  9758. if (unlikely(!phba))
  9759. return IRQ_NONE;
  9760. /* Check device state for handling interrupt */
  9761. if (lpfc_intr_state_check(phba))
  9762. return IRQ_NONE;
  9763. spin_lock(&phba->hbalock);
  9764. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9765. spin_unlock(&phba->hbalock);
  9766. return IRQ_HANDLED;
  9767. }
  9768. if (unlikely(!phba->ha_copy)) {
  9769. spin_unlock(&phba->hbalock);
  9770. return IRQ_NONE;
  9771. } else if (phba->ha_copy & HA_ERATT) {
  9772. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9773. /* ERATT polling has handled ERATT */
  9774. phba->ha_copy &= ~HA_ERATT;
  9775. else
  9776. /* Indicate interrupt handler handles ERATT */
  9777. phba->hba_flag |= HBA_ERATT_HANDLED;
  9778. }
  9779. /*
  9780. * If there is deferred error attention, do not check for any interrupt.
  9781. */
  9782. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9783. spin_unlock(&phba->hbalock);
  9784. return IRQ_NONE;
  9785. }
  9786. /* Clear attention sources except link and error attentions */
  9787. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9788. spin_unlock(&phba->hbalock);
  9789. return IRQ_HANDLED;
  9790. }
  9791. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9792. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9793. phba->HCregaddr);
  9794. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9795. writel(hc_copy, phba->HCregaddr);
  9796. readl(phba->HAregaddr); /* flush */
  9797. spin_unlock(&phba->hbalock);
  9798. /*
  9799. * Invokes slow-path host attention interrupt handling as appropriate.
  9800. */
  9801. /* status of events with mailbox and link attention */
  9802. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9803. /* status of events with ELS ring */
  9804. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9805. status2 >>= (4*LPFC_ELS_RING);
  9806. if (status1 || (status2 & HA_RXMASK))
  9807. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9808. else
  9809. sp_irq_rc = IRQ_NONE;
  9810. /*
  9811. * Invoke fast-path host attention interrupt handling as appropriate.
  9812. */
  9813. /* status of events with FCP ring */
  9814. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9815. status1 >>= (4*LPFC_FCP_RING);
  9816. /* status of events with extra ring */
  9817. if (phba->cfg_multi_ring_support == 2) {
  9818. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9819. status2 >>= (4*LPFC_EXTRA_RING);
  9820. } else
  9821. status2 = 0;
  9822. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9823. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9824. else
  9825. fp_irq_rc = IRQ_NONE;
  9826. /* Return device-level interrupt handling status */
  9827. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9828. } /* lpfc_sli_intr_handler */
  9829. /**
  9830. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9831. * @phba: pointer to lpfc hba data structure.
  9832. *
  9833. * This routine is invoked by the worker thread to process all the pending
  9834. * SLI4 FCP abort XRI events.
  9835. **/
  9836. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9837. {
  9838. struct lpfc_cq_event *cq_event;
  9839. /* First, declare the fcp xri abort event has been handled */
  9840. spin_lock_irq(&phba->hbalock);
  9841. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9842. spin_unlock_irq(&phba->hbalock);
  9843. /* Now, handle all the fcp xri abort events */
  9844. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9845. /* Get the first event from the head of the event queue */
  9846. spin_lock_irq(&phba->hbalock);
  9847. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9848. cq_event, struct lpfc_cq_event, list);
  9849. spin_unlock_irq(&phba->hbalock);
  9850. /* Notify aborted XRI for FCP work queue */
  9851. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9852. /* Free the event processed back to the free pool */
  9853. lpfc_sli4_cq_event_release(phba, cq_event);
  9854. }
  9855. }
  9856. /**
  9857. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9858. * @phba: pointer to lpfc hba data structure.
  9859. *
  9860. * This routine is invoked by the worker thread to process all the pending
  9861. * SLI4 els abort xri events.
  9862. **/
  9863. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9864. {
  9865. struct lpfc_cq_event *cq_event;
  9866. /* First, declare the els xri abort event has been handled */
  9867. spin_lock_irq(&phba->hbalock);
  9868. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9869. spin_unlock_irq(&phba->hbalock);
  9870. /* Now, handle all the els xri abort events */
  9871. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9872. /* Get the first event from the head of the event queue */
  9873. spin_lock_irq(&phba->hbalock);
  9874. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9875. cq_event, struct lpfc_cq_event, list);
  9876. spin_unlock_irq(&phba->hbalock);
  9877. /* Notify aborted XRI for ELS work queue */
  9878. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9879. /* Free the event processed back to the free pool */
  9880. lpfc_sli4_cq_event_release(phba, cq_event);
  9881. }
  9882. }
  9883. /**
  9884. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9885. * @phba: pointer to lpfc hba data structure
  9886. * @pIocbIn: pointer to the rspiocbq
  9887. * @pIocbOut: pointer to the cmdiocbq
  9888. * @wcqe: pointer to the complete wcqe
  9889. *
  9890. * This routine transfers the fields of a command iocbq to a response iocbq
  9891. * by copying all the IOCB fields from command iocbq and transferring the
  9892. * completion status information from the complete wcqe.
  9893. **/
  9894. static void
  9895. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  9896. struct lpfc_iocbq *pIocbIn,
  9897. struct lpfc_iocbq *pIocbOut,
  9898. struct lpfc_wcqe_complete *wcqe)
  9899. {
  9900. unsigned long iflags;
  9901. uint32_t status;
  9902. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  9903. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  9904. sizeof(struct lpfc_iocbq) - offset);
  9905. /* Map WCQE parameters into irspiocb parameters */
  9906. status = bf_get(lpfc_wcqe_c_status, wcqe);
  9907. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  9908. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  9909. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  9910. pIocbIn->iocb.un.fcpi.fcpi_parm =
  9911. pIocbOut->iocb.un.fcpi.fcpi_parm -
  9912. wcqe->total_data_placed;
  9913. else
  9914. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9915. else {
  9916. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9917. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  9918. }
  9919. /* Convert BG errors for completion status */
  9920. if (status == CQE_STATUS_DI_ERROR) {
  9921. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  9922. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  9923. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  9924. else
  9925. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  9926. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  9927. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  9928. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9929. BGS_GUARD_ERR_MASK;
  9930. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  9931. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9932. BGS_APPTAG_ERR_MASK;
  9933. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  9934. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9935. BGS_REFTAG_ERR_MASK;
  9936. /* Check to see if there was any good data before the error */
  9937. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  9938. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9939. BGS_HI_WATER_MARK_PRESENT_MASK;
  9940. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  9941. wcqe->total_data_placed;
  9942. }
  9943. /*
  9944. * Set ALL the error bits to indicate we don't know what
  9945. * type of error it is.
  9946. */
  9947. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  9948. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9949. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  9950. BGS_GUARD_ERR_MASK);
  9951. }
  9952. /* Pick up HBA exchange busy condition */
  9953. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  9954. spin_lock_irqsave(&phba->hbalock, iflags);
  9955. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  9956. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9957. }
  9958. }
  9959. /**
  9960. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  9961. * @phba: Pointer to HBA context object.
  9962. * @wcqe: Pointer to work-queue completion queue entry.
  9963. *
  9964. * This routine handles an ELS work-queue completion event and construct
  9965. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  9966. * discovery engine to handle.
  9967. *
  9968. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  9969. **/
  9970. static struct lpfc_iocbq *
  9971. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  9972. struct lpfc_iocbq *irspiocbq)
  9973. {
  9974. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9975. struct lpfc_iocbq *cmdiocbq;
  9976. struct lpfc_wcqe_complete *wcqe;
  9977. unsigned long iflags;
  9978. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  9979. spin_lock_irqsave(&phba->hbalock, iflags);
  9980. pring->stats.iocb_event++;
  9981. /* Look up the ELS command IOCB and create pseudo response IOCB */
  9982. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  9983. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9984. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9985. if (unlikely(!cmdiocbq)) {
  9986. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9987. "0386 ELS complete with no corresponding "
  9988. "cmdiocb: iotag (%d)\n",
  9989. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9990. lpfc_sli_release_iocbq(phba, irspiocbq);
  9991. return NULL;
  9992. }
  9993. /* Fake the irspiocbq and copy necessary response information */
  9994. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  9995. return irspiocbq;
  9996. }
  9997. /**
  9998. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  9999. * @phba: Pointer to HBA context object.
  10000. * @cqe: Pointer to mailbox completion queue entry.
  10001. *
  10002. * This routine process a mailbox completion queue entry with asynchrous
  10003. * event.
  10004. *
  10005. * Return: true if work posted to worker thread, otherwise false.
  10006. **/
  10007. static bool
  10008. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10009. {
  10010. struct lpfc_cq_event *cq_event;
  10011. unsigned long iflags;
  10012. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10013. "0392 Async Event: word0:x%x, word1:x%x, "
  10014. "word2:x%x, word3:x%x\n", mcqe->word0,
  10015. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  10016. /* Allocate a new internal CQ_EVENT entry */
  10017. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10018. if (!cq_event) {
  10019. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10020. "0394 Failed to allocate CQ_EVENT entry\n");
  10021. return false;
  10022. }
  10023. /* Move the CQE into an asynchronous event entry */
  10024. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  10025. spin_lock_irqsave(&phba->hbalock, iflags);
  10026. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  10027. /* Set the async event flag */
  10028. phba->hba_flag |= ASYNC_EVENT;
  10029. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10030. return true;
  10031. }
  10032. /**
  10033. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  10034. * @phba: Pointer to HBA context object.
  10035. * @cqe: Pointer to mailbox completion queue entry.
  10036. *
  10037. * This routine process a mailbox completion queue entry with mailbox
  10038. * completion event.
  10039. *
  10040. * Return: true if work posted to worker thread, otherwise false.
  10041. **/
  10042. static bool
  10043. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10044. {
  10045. uint32_t mcqe_status;
  10046. MAILBOX_t *mbox, *pmbox;
  10047. struct lpfc_mqe *mqe;
  10048. struct lpfc_vport *vport;
  10049. struct lpfc_nodelist *ndlp;
  10050. struct lpfc_dmabuf *mp;
  10051. unsigned long iflags;
  10052. LPFC_MBOXQ_t *pmb;
  10053. bool workposted = false;
  10054. int rc;
  10055. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  10056. if (!bf_get(lpfc_trailer_completed, mcqe))
  10057. goto out_no_mqe_complete;
  10058. /* Get the reference to the active mbox command */
  10059. spin_lock_irqsave(&phba->hbalock, iflags);
  10060. pmb = phba->sli.mbox_active;
  10061. if (unlikely(!pmb)) {
  10062. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10063. "1832 No pending MBOX command to handle\n");
  10064. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10065. goto out_no_mqe_complete;
  10066. }
  10067. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10068. mqe = &pmb->u.mqe;
  10069. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  10070. mbox = phba->mbox;
  10071. vport = pmb->vport;
  10072. /* Reset heartbeat timer */
  10073. phba->last_completion_time = jiffies;
  10074. del_timer(&phba->sli.mbox_tmo);
  10075. /* Move mbox data to caller's mailbox region, do endian swapping */
  10076. if (pmb->mbox_cmpl && mbox)
  10077. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  10078. /*
  10079. * For mcqe errors, conditionally move a modified error code to
  10080. * the mbox so that the error will not be missed.
  10081. */
  10082. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  10083. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  10084. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  10085. bf_set(lpfc_mqe_status, mqe,
  10086. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  10087. }
  10088. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10089. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10090. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  10091. "MBOX dflt rpi: status:x%x rpi:x%x",
  10092. mcqe_status,
  10093. pmbox->un.varWords[0], 0);
  10094. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  10095. mp = (struct lpfc_dmabuf *)(pmb->context1);
  10096. ndlp = (struct lpfc_nodelist *)pmb->context2;
  10097. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  10098. * RID of the PPI using the same mbox buffer.
  10099. */
  10100. lpfc_unreg_login(phba, vport->vpi,
  10101. pmbox->un.varWords[0], pmb);
  10102. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  10103. pmb->context1 = mp;
  10104. pmb->context2 = ndlp;
  10105. pmb->vport = vport;
  10106. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  10107. if (rc != MBX_BUSY)
  10108. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10109. LOG_SLI, "0385 rc should "
  10110. "have been MBX_BUSY\n");
  10111. if (rc != MBX_NOT_FINISHED)
  10112. goto send_current_mbox;
  10113. }
  10114. }
  10115. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  10116. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  10117. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  10118. /* There is mailbox completion work to do */
  10119. spin_lock_irqsave(&phba->hbalock, iflags);
  10120. __lpfc_mbox_cmpl_put(phba, pmb);
  10121. phba->work_ha |= HA_MBATT;
  10122. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10123. workposted = true;
  10124. send_current_mbox:
  10125. spin_lock_irqsave(&phba->hbalock, iflags);
  10126. /* Release the mailbox command posting token */
  10127. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10128. /* Setting active mailbox pointer need to be in sync to flag clear */
  10129. phba->sli.mbox_active = NULL;
  10130. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10131. /* Wake up worker thread to post the next pending mailbox command */
  10132. lpfc_worker_wake_up(phba);
  10133. out_no_mqe_complete:
  10134. if (bf_get(lpfc_trailer_consumed, mcqe))
  10135. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  10136. return workposted;
  10137. }
  10138. /**
  10139. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  10140. * @phba: Pointer to HBA context object.
  10141. * @cqe: Pointer to mailbox completion queue entry.
  10142. *
  10143. * This routine process a mailbox completion queue entry, it invokes the
  10144. * proper mailbox complete handling or asynchrous event handling routine
  10145. * according to the MCQE's async bit.
  10146. *
  10147. * Return: true if work posted to worker thread, otherwise false.
  10148. **/
  10149. static bool
  10150. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  10151. {
  10152. struct lpfc_mcqe mcqe;
  10153. bool workposted;
  10154. /* Copy the mailbox MCQE and convert endian order as needed */
  10155. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  10156. /* Invoke the proper event handling routine */
  10157. if (!bf_get(lpfc_trailer_async, &mcqe))
  10158. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  10159. else
  10160. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  10161. return workposted;
  10162. }
  10163. /**
  10164. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  10165. * @phba: Pointer to HBA context object.
  10166. * @wcqe: Pointer to work-queue completion queue entry.
  10167. *
  10168. * This routine handles an ELS work-queue completion event.
  10169. *
  10170. * Return: true if work posted to worker thread, otherwise false.
  10171. **/
  10172. static bool
  10173. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
  10174. struct lpfc_wcqe_complete *wcqe)
  10175. {
  10176. struct lpfc_iocbq *irspiocbq;
  10177. unsigned long iflags;
  10178. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10179. /* Get an irspiocbq for later ELS response processing use */
  10180. irspiocbq = lpfc_sli_get_iocbq(phba);
  10181. if (!irspiocbq) {
  10182. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10183. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10184. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10185. pring->txq_cnt, phba->iocb_cnt,
  10186. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  10187. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  10188. return false;
  10189. }
  10190. /* Save off the slow-path queue event for work thread to process */
  10191. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10192. spin_lock_irqsave(&phba->hbalock, iflags);
  10193. list_add_tail(&irspiocbq->cq_event.list,
  10194. &phba->sli4_hba.sp_queue_event);
  10195. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10196. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10197. return true;
  10198. }
  10199. /**
  10200. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10201. * @phba: Pointer to HBA context object.
  10202. * @wcqe: Pointer to work-queue completion queue entry.
  10203. *
  10204. * This routine handles slow-path WQ entry comsumed event by invoking the
  10205. * proper WQ release routine to the slow-path WQ.
  10206. **/
  10207. static void
  10208. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10209. struct lpfc_wcqe_release *wcqe)
  10210. {
  10211. /* sanity check on queue memory */
  10212. if (unlikely(!phba->sli4_hba.els_wq))
  10213. return;
  10214. /* Check for the slow-path ELS work queue */
  10215. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10216. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10217. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10218. else
  10219. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10220. "2579 Slow-path wqe consume event carries "
  10221. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10222. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10223. phba->sli4_hba.els_wq->queue_id);
  10224. }
  10225. /**
  10226. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10227. * @phba: Pointer to HBA context object.
  10228. * @cq: Pointer to a WQ completion queue.
  10229. * @wcqe: Pointer to work-queue completion queue entry.
  10230. *
  10231. * This routine handles an XRI abort event.
  10232. *
  10233. * Return: true if work posted to worker thread, otherwise false.
  10234. **/
  10235. static bool
  10236. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10237. struct lpfc_queue *cq,
  10238. struct sli4_wcqe_xri_aborted *wcqe)
  10239. {
  10240. bool workposted = false;
  10241. struct lpfc_cq_event *cq_event;
  10242. unsigned long iflags;
  10243. /* Allocate a new internal CQ_EVENT entry */
  10244. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10245. if (!cq_event) {
  10246. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10247. "0602 Failed to allocate CQ_EVENT entry\n");
  10248. return false;
  10249. }
  10250. /* Move the CQE into the proper xri abort event list */
  10251. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10252. switch (cq->subtype) {
  10253. case LPFC_FCP:
  10254. spin_lock_irqsave(&phba->hbalock, iflags);
  10255. list_add_tail(&cq_event->list,
  10256. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10257. /* Set the fcp xri abort event flag */
  10258. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10259. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10260. workposted = true;
  10261. break;
  10262. case LPFC_ELS:
  10263. spin_lock_irqsave(&phba->hbalock, iflags);
  10264. list_add_tail(&cq_event->list,
  10265. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10266. /* Set the els xri abort event flag */
  10267. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10268. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10269. workposted = true;
  10270. break;
  10271. default:
  10272. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10273. "0603 Invalid work queue CQE subtype (x%x)\n",
  10274. cq->subtype);
  10275. workposted = false;
  10276. break;
  10277. }
  10278. return workposted;
  10279. }
  10280. /**
  10281. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10282. * @phba: Pointer to HBA context object.
  10283. * @rcqe: Pointer to receive-queue completion queue entry.
  10284. *
  10285. * This routine process a receive-queue completion queue entry.
  10286. *
  10287. * Return: true if work posted to worker thread, otherwise false.
  10288. **/
  10289. static bool
  10290. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10291. {
  10292. bool workposted = false;
  10293. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10294. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10295. struct hbq_dmabuf *dma_buf;
  10296. uint32_t status, rq_id;
  10297. unsigned long iflags;
  10298. /* sanity check on queue memory */
  10299. if (unlikely(!hrq) || unlikely(!drq))
  10300. return workposted;
  10301. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10302. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10303. else
  10304. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10305. if (rq_id != hrq->queue_id)
  10306. goto out;
  10307. status = bf_get(lpfc_rcqe_status, rcqe);
  10308. switch (status) {
  10309. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10310. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10311. "2537 Receive Frame Truncated!!\n");
  10312. hrq->RQ_buf_trunc++;
  10313. case FC_STATUS_RQ_SUCCESS:
  10314. lpfc_sli4_rq_release(hrq, drq);
  10315. spin_lock_irqsave(&phba->hbalock, iflags);
  10316. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10317. if (!dma_buf) {
  10318. hrq->RQ_no_buf_found++;
  10319. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10320. goto out;
  10321. }
  10322. hrq->RQ_rcv_buf++;
  10323. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10324. /* save off the frame for the word thread to process */
  10325. list_add_tail(&dma_buf->cq_event.list,
  10326. &phba->sli4_hba.sp_queue_event);
  10327. /* Frame received */
  10328. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10329. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10330. workposted = true;
  10331. break;
  10332. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10333. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10334. hrq->RQ_no_posted_buf++;
  10335. /* Post more buffers if possible */
  10336. spin_lock_irqsave(&phba->hbalock, iflags);
  10337. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10338. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10339. workposted = true;
  10340. break;
  10341. }
  10342. out:
  10343. return workposted;
  10344. }
  10345. /**
  10346. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10347. * @phba: Pointer to HBA context object.
  10348. * @cq: Pointer to the completion queue.
  10349. * @wcqe: Pointer to a completion queue entry.
  10350. *
  10351. * This routine process a slow-path work-queue or receive queue completion queue
  10352. * entry.
  10353. *
  10354. * Return: true if work posted to worker thread, otherwise false.
  10355. **/
  10356. static bool
  10357. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10358. struct lpfc_cqe *cqe)
  10359. {
  10360. struct lpfc_cqe cqevt;
  10361. bool workposted = false;
  10362. /* Copy the work queue CQE and convert endian order if needed */
  10363. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10364. /* Check and process for different type of WCQE and dispatch */
  10365. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10366. case CQE_CODE_COMPL_WQE:
  10367. /* Process the WQ/RQ complete event */
  10368. phba->last_completion_time = jiffies;
  10369. workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
  10370. (struct lpfc_wcqe_complete *)&cqevt);
  10371. break;
  10372. case CQE_CODE_RELEASE_WQE:
  10373. /* Process the WQ release event */
  10374. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10375. (struct lpfc_wcqe_release *)&cqevt);
  10376. break;
  10377. case CQE_CODE_XRI_ABORTED:
  10378. /* Process the WQ XRI abort event */
  10379. phba->last_completion_time = jiffies;
  10380. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10381. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10382. break;
  10383. case CQE_CODE_RECEIVE:
  10384. case CQE_CODE_RECEIVE_V1:
  10385. /* Process the RQ event */
  10386. phba->last_completion_time = jiffies;
  10387. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10388. (struct lpfc_rcqe *)&cqevt);
  10389. break;
  10390. default:
  10391. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10392. "0388 Not a valid WCQE code: x%x\n",
  10393. bf_get(lpfc_cqe_code, &cqevt));
  10394. break;
  10395. }
  10396. return workposted;
  10397. }
  10398. /**
  10399. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10400. * @phba: Pointer to HBA context object.
  10401. * @eqe: Pointer to fast-path event queue entry.
  10402. *
  10403. * This routine process a event queue entry from the slow-path event queue.
  10404. * It will check the MajorCode and MinorCode to determine this is for a
  10405. * completion event on a completion queue, if not, an error shall be logged
  10406. * and just return. Otherwise, it will get to the corresponding completion
  10407. * queue and process all the entries on that completion queue, rearm the
  10408. * completion queue, and then return.
  10409. *
  10410. **/
  10411. static void
  10412. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  10413. {
  10414. struct lpfc_queue *cq = NULL, *childq, *speq;
  10415. struct lpfc_cqe *cqe;
  10416. bool workposted = false;
  10417. int ecount = 0;
  10418. uint16_t cqid;
  10419. if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
  10420. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10421. "0359 Not a valid slow-path completion "
  10422. "event: majorcode=x%x, minorcode=x%x\n",
  10423. bf_get_le32(lpfc_eqe_major_code, eqe),
  10424. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10425. return;
  10426. }
  10427. /* Get the reference to the corresponding CQ */
  10428. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10429. /* Search for completion queue pointer matching this cqid */
  10430. speq = phba->sli4_hba.sp_eq;
  10431. /* sanity check on queue memory */
  10432. if (unlikely(!speq))
  10433. return;
  10434. list_for_each_entry(childq, &speq->child_list, list) {
  10435. if (childq->queue_id == cqid) {
  10436. cq = childq;
  10437. break;
  10438. }
  10439. }
  10440. if (unlikely(!cq)) {
  10441. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10442. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10443. "0365 Slow-path CQ identifier "
  10444. "(%d) does not exist\n", cqid);
  10445. return;
  10446. }
  10447. /* Process all the entries to the CQ */
  10448. switch (cq->type) {
  10449. case LPFC_MCQ:
  10450. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10451. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10452. if (!(++ecount % cq->entry_repost))
  10453. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10454. cq->CQ_mbox++;
  10455. }
  10456. break;
  10457. case LPFC_WCQ:
  10458. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10459. if (cq->subtype == LPFC_FCP)
  10460. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10461. cqe);
  10462. else
  10463. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10464. cqe);
  10465. if (!(++ecount % cq->entry_repost))
  10466. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10467. }
  10468. /* Track the max number of CQEs processed in 1 EQ */
  10469. if (ecount > cq->CQ_max_cqe)
  10470. cq->CQ_max_cqe = ecount;
  10471. break;
  10472. default:
  10473. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10474. "0370 Invalid completion queue type (%d)\n",
  10475. cq->type);
  10476. return;
  10477. }
  10478. /* Catch the no cq entry condition, log an error */
  10479. if (unlikely(ecount == 0))
  10480. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10481. "0371 No entry from the CQ: identifier "
  10482. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10483. /* In any case, flash and re-arm the RCQ */
  10484. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10485. /* wake up worker thread if there are works to be done */
  10486. if (workposted)
  10487. lpfc_worker_wake_up(phba);
  10488. }
  10489. /**
  10490. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10491. * @eqe: Pointer to fast-path completion queue entry.
  10492. *
  10493. * This routine process a fast-path work queue completion entry from fast-path
  10494. * event queue for FCP command response completion.
  10495. **/
  10496. static void
  10497. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
  10498. struct lpfc_wcqe_complete *wcqe)
  10499. {
  10500. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10501. struct lpfc_iocbq *cmdiocbq;
  10502. struct lpfc_iocbq irspiocbq;
  10503. unsigned long iflags;
  10504. spin_lock_irqsave(&phba->hbalock, iflags);
  10505. pring->stats.iocb_event++;
  10506. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10507. /* Check for response status */
  10508. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10509. /* If resource errors reported from HBA, reduce queue
  10510. * depth of the SCSI device.
  10511. */
  10512. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10513. IOSTAT_LOCAL_REJECT) &&
  10514. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  10515. phba->lpfc_rampdown_queue_depth(phba);
  10516. }
  10517. /* Log the error status */
  10518. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10519. "0373 FCP complete error: status=x%x, "
  10520. "hw_status=x%x, total_data_specified=%d, "
  10521. "parameter=x%x, word3=x%x\n",
  10522. bf_get(lpfc_wcqe_c_status, wcqe),
  10523. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10524. wcqe->total_data_placed, wcqe->parameter,
  10525. wcqe->word3);
  10526. }
  10527. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10528. spin_lock_irqsave(&phba->hbalock, iflags);
  10529. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10530. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10531. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10532. if (unlikely(!cmdiocbq)) {
  10533. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10534. "0374 FCP complete with no corresponding "
  10535. "cmdiocb: iotag (%d)\n",
  10536. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10537. return;
  10538. }
  10539. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10540. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10541. "0375 FCP cmdiocb not callback function "
  10542. "iotag: (%d)\n",
  10543. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10544. return;
  10545. }
  10546. /* Fake the irspiocb and copy necessary response information */
  10547. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10548. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10549. spin_lock_irqsave(&phba->hbalock, iflags);
  10550. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10551. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10552. }
  10553. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10554. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10555. }
  10556. /**
  10557. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10558. * @phba: Pointer to HBA context object.
  10559. * @cq: Pointer to completion queue.
  10560. * @wcqe: Pointer to work-queue completion queue entry.
  10561. *
  10562. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10563. * proper WQ release routine to the slow-path WQ.
  10564. **/
  10565. static void
  10566. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10567. struct lpfc_wcqe_release *wcqe)
  10568. {
  10569. struct lpfc_queue *childwq;
  10570. bool wqid_matched = false;
  10571. uint16_t fcp_wqid;
  10572. /* Check for fast-path FCP work queue release */
  10573. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10574. list_for_each_entry(childwq, &cq->child_list, list) {
  10575. if (childwq->queue_id == fcp_wqid) {
  10576. lpfc_sli4_wq_release(childwq,
  10577. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10578. wqid_matched = true;
  10579. break;
  10580. }
  10581. }
  10582. /* Report warning log message if no match found */
  10583. if (wqid_matched != true)
  10584. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10585. "2580 Fast-path wqe consume event carries "
  10586. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10587. }
  10588. /**
  10589. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10590. * @cq: Pointer to the completion queue.
  10591. * @eqe: Pointer to fast-path completion queue entry.
  10592. *
  10593. * This routine process a fast-path work queue completion entry from fast-path
  10594. * event queue for FCP command response completion.
  10595. **/
  10596. static int
  10597. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10598. struct lpfc_cqe *cqe)
  10599. {
  10600. struct lpfc_wcqe_release wcqe;
  10601. bool workposted = false;
  10602. /* Copy the work queue CQE and convert endian order if needed */
  10603. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10604. /* Check and process for different type of WCQE and dispatch */
  10605. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10606. case CQE_CODE_COMPL_WQE:
  10607. cq->CQ_wq++;
  10608. /* Process the WQ complete event */
  10609. phba->last_completion_time = jiffies;
  10610. lpfc_sli4_fp_handle_fcp_wcqe(phba,
  10611. (struct lpfc_wcqe_complete *)&wcqe);
  10612. break;
  10613. case CQE_CODE_RELEASE_WQE:
  10614. cq->CQ_release_wqe++;
  10615. /* Process the WQ release event */
  10616. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10617. (struct lpfc_wcqe_release *)&wcqe);
  10618. break;
  10619. case CQE_CODE_XRI_ABORTED:
  10620. cq->CQ_xri_aborted++;
  10621. /* Process the WQ XRI abort event */
  10622. phba->last_completion_time = jiffies;
  10623. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10624. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10625. break;
  10626. default:
  10627. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10628. "0144 Not a valid WCQE code: x%x\n",
  10629. bf_get(lpfc_wcqe_c_code, &wcqe));
  10630. break;
  10631. }
  10632. return workposted;
  10633. }
  10634. /**
  10635. * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
  10636. * @phba: Pointer to HBA context object.
  10637. * @eqe: Pointer to fast-path event queue entry.
  10638. *
  10639. * This routine process a event queue entry from the fast-path event queue.
  10640. * It will check the MajorCode and MinorCode to determine this is for a
  10641. * completion event on a completion queue, if not, an error shall be logged
  10642. * and just return. Otherwise, it will get to the corresponding completion
  10643. * queue and process all the entries on the completion queue, rearm the
  10644. * completion queue, and then return.
  10645. **/
  10646. static void
  10647. lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10648. uint32_t fcp_cqidx)
  10649. {
  10650. struct lpfc_queue *cq;
  10651. struct lpfc_cqe *cqe;
  10652. bool workposted = false;
  10653. uint16_t cqid;
  10654. int ecount = 0;
  10655. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10656. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10657. "0366 Not a valid fast-path completion "
  10658. "event: majorcode=x%x, minorcode=x%x\n",
  10659. bf_get_le32(lpfc_eqe_major_code, eqe),
  10660. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10661. return;
  10662. }
  10663. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10664. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10665. "3146 Fast-path completion queues "
  10666. "does not exist\n");
  10667. return;
  10668. }
  10669. cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
  10670. if (unlikely(!cq)) {
  10671. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10672. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10673. "0367 Fast-path completion queue "
  10674. "(%d) does not exist\n", fcp_cqidx);
  10675. return;
  10676. }
  10677. /* Get the reference to the corresponding CQ */
  10678. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10679. if (unlikely(cqid != cq->queue_id)) {
  10680. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10681. "0368 Miss-matched fast-path completion "
  10682. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10683. cqid, cq->queue_id);
  10684. return;
  10685. }
  10686. /* Process all the entries to the CQ */
  10687. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10688. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10689. if (!(++ecount % cq->entry_repost))
  10690. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10691. }
  10692. /* Track the max number of CQEs processed in 1 EQ */
  10693. if (ecount > cq->CQ_max_cqe)
  10694. cq->CQ_max_cqe = ecount;
  10695. /* Catch the no cq entry condition */
  10696. if (unlikely(ecount == 0))
  10697. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10698. "0369 No entry from fast-path completion "
  10699. "queue fcpcqid=%d\n", cq->queue_id);
  10700. /* In any case, flash and re-arm the CQ */
  10701. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10702. /* wake up worker thread if there are works to be done */
  10703. if (workposted)
  10704. lpfc_worker_wake_up(phba);
  10705. }
  10706. static void
  10707. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10708. {
  10709. struct lpfc_eqe *eqe;
  10710. /* walk all the EQ entries and drop on the floor */
  10711. while ((eqe = lpfc_sli4_eq_get(eq)))
  10712. ;
  10713. /* Clear and re-arm the EQ */
  10714. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10715. }
  10716. /**
  10717. * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
  10718. * @irq: Interrupt number.
  10719. * @dev_id: The device context pointer.
  10720. *
  10721. * This function is directly called from the PCI layer as an interrupt
  10722. * service routine when device with SLI-4 interface spec is enabled with
  10723. * MSI-X multi-message interrupt mode and there are slow-path events in
  10724. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  10725. * interrupt mode, this function is called as part of the device-level
  10726. * interrupt handler. When the PCI slot is in error recovery or the HBA is
  10727. * undergoing initialization, the interrupt handler will not process the
  10728. * interrupt. The link attention and ELS ring attention events are handled
  10729. * by the worker thread. The interrupt handler signals the worker thread
  10730. * and returns for these events. This function is called without any lock
  10731. * held. It gets the hbalock to access and update SLI data structures.
  10732. *
  10733. * This function returns IRQ_HANDLED when interrupt is handled else it
  10734. * returns IRQ_NONE.
  10735. **/
  10736. irqreturn_t
  10737. lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
  10738. {
  10739. struct lpfc_hba *phba;
  10740. struct lpfc_queue *speq;
  10741. struct lpfc_eqe *eqe;
  10742. unsigned long iflag;
  10743. int ecount = 0;
  10744. /*
  10745. * Get the driver's phba structure from the dev_id
  10746. */
  10747. phba = (struct lpfc_hba *)dev_id;
  10748. if (unlikely(!phba))
  10749. return IRQ_NONE;
  10750. /* Get to the EQ struct associated with this vector */
  10751. speq = phba->sli4_hba.sp_eq;
  10752. if (unlikely(!speq))
  10753. return IRQ_NONE;
  10754. /* Check device state for handling interrupt */
  10755. if (unlikely(lpfc_intr_state_check(phba))) {
  10756. speq->EQ_badstate++;
  10757. /* Check again for link_state with lock held */
  10758. spin_lock_irqsave(&phba->hbalock, iflag);
  10759. if (phba->link_state < LPFC_LINK_DOWN)
  10760. /* Flush, clear interrupt, and rearm the EQ */
  10761. lpfc_sli4_eq_flush(phba, speq);
  10762. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10763. return IRQ_NONE;
  10764. }
  10765. /*
  10766. * Process all the event on FCP slow-path EQ
  10767. */
  10768. while ((eqe = lpfc_sli4_eq_get(speq))) {
  10769. lpfc_sli4_sp_handle_eqe(phba, eqe);
  10770. if (!(++ecount % speq->entry_repost))
  10771. lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
  10772. speq->EQ_processed++;
  10773. }
  10774. /* Track the max number of EQEs processed in 1 intr */
  10775. if (ecount > speq->EQ_max_eqe)
  10776. speq->EQ_max_eqe = ecount;
  10777. /* Always clear and re-arm the slow-path EQ */
  10778. lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
  10779. /* Catch the no cq entry condition */
  10780. if (unlikely(ecount == 0)) {
  10781. speq->EQ_no_entry++;
  10782. if (phba->intr_type == MSIX)
  10783. /* MSI-X treated interrupt served as no EQ share INT */
  10784. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10785. "0357 MSI-X interrupt with no EQE\n");
  10786. else
  10787. /* Non MSI-X treated on interrupt as EQ share INT */
  10788. return IRQ_NONE;
  10789. }
  10790. return IRQ_HANDLED;
  10791. } /* lpfc_sli4_sp_intr_handler */
  10792. /**
  10793. * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
  10794. * @irq: Interrupt number.
  10795. * @dev_id: The device context pointer.
  10796. *
  10797. * This function is directly called from the PCI layer as an interrupt
  10798. * service routine when device with SLI-4 interface spec is enabled with
  10799. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10800. * ring event in the HBA. However, when the device is enabled with either
  10801. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10802. * device-level interrupt handler. When the PCI slot is in error recovery
  10803. * or the HBA is undergoing initialization, the interrupt handler will not
  10804. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10805. * the intrrupt context. This function is called without any lock held.
  10806. * It gets the hbalock to access and update SLI data structures. Note that,
  10807. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10808. * equal to that of FCP CQ index.
  10809. *
  10810. * This function returns IRQ_HANDLED when interrupt is handled else it
  10811. * returns IRQ_NONE.
  10812. **/
  10813. irqreturn_t
  10814. lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
  10815. {
  10816. struct lpfc_hba *phba;
  10817. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10818. struct lpfc_queue *fpeq;
  10819. struct lpfc_eqe *eqe;
  10820. unsigned long iflag;
  10821. int ecount = 0;
  10822. uint32_t fcp_eqidx;
  10823. /* Get the driver's phba structure from the dev_id */
  10824. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10825. phba = fcp_eq_hdl->phba;
  10826. fcp_eqidx = fcp_eq_hdl->idx;
  10827. if (unlikely(!phba))
  10828. return IRQ_NONE;
  10829. if (unlikely(!phba->sli4_hba.fp_eq))
  10830. return IRQ_NONE;
  10831. /* Get to the EQ struct associated with this vector */
  10832. fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
  10833. if (unlikely(!fpeq))
  10834. return IRQ_NONE;
  10835. /* Check device state for handling interrupt */
  10836. if (unlikely(lpfc_intr_state_check(phba))) {
  10837. fpeq->EQ_badstate++;
  10838. /* Check again for link_state with lock held */
  10839. spin_lock_irqsave(&phba->hbalock, iflag);
  10840. if (phba->link_state < LPFC_LINK_DOWN)
  10841. /* Flush, clear interrupt, and rearm the EQ */
  10842. lpfc_sli4_eq_flush(phba, fpeq);
  10843. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10844. return IRQ_NONE;
  10845. }
  10846. /*
  10847. * Process all the event on FCP fast-path EQ
  10848. */
  10849. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10850. lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
  10851. if (!(++ecount % fpeq->entry_repost))
  10852. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10853. fpeq->EQ_processed++;
  10854. }
  10855. /* Track the max number of EQEs processed in 1 intr */
  10856. if (ecount > fpeq->EQ_max_eqe)
  10857. fpeq->EQ_max_eqe = ecount;
  10858. /* Always clear and re-arm the fast-path EQ */
  10859. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10860. if (unlikely(ecount == 0)) {
  10861. fpeq->EQ_no_entry++;
  10862. if (phba->intr_type == MSIX)
  10863. /* MSI-X treated interrupt served as no EQ share INT */
  10864. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10865. "0358 MSI-X interrupt with no EQE\n");
  10866. else
  10867. /* Non MSI-X treated on interrupt as EQ share INT */
  10868. return IRQ_NONE;
  10869. }
  10870. return IRQ_HANDLED;
  10871. } /* lpfc_sli4_fp_intr_handler */
  10872. /**
  10873. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10874. * @irq: Interrupt number.
  10875. * @dev_id: The device context pointer.
  10876. *
  10877. * This function is the device-level interrupt handler to device with SLI-4
  10878. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10879. * interrupt mode is enabled and there is an event in the HBA which requires
  10880. * driver attention. This function invokes the slow-path interrupt attention
  10881. * handling function and fast-path interrupt attention handling function in
  10882. * turn to process the relevant HBA attention events. This function is called
  10883. * without any lock held. It gets the hbalock to access and update SLI data
  10884. * structures.
  10885. *
  10886. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10887. * returns IRQ_NONE.
  10888. **/
  10889. irqreturn_t
  10890. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10891. {
  10892. struct lpfc_hba *phba;
  10893. irqreturn_t sp_irq_rc, fp_irq_rc;
  10894. bool fp_handled = false;
  10895. uint32_t fcp_eqidx;
  10896. /* Get the driver's phba structure from the dev_id */
  10897. phba = (struct lpfc_hba *)dev_id;
  10898. if (unlikely(!phba))
  10899. return IRQ_NONE;
  10900. /*
  10901. * Invokes slow-path host attention interrupt handling as appropriate.
  10902. */
  10903. sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
  10904. /*
  10905. * Invoke fast-path host attention interrupt handling as appropriate.
  10906. */
  10907. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
  10908. fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
  10909. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10910. if (fp_irq_rc == IRQ_HANDLED)
  10911. fp_handled |= true;
  10912. }
  10913. return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
  10914. } /* lpfc_sli4_intr_handler */
  10915. /**
  10916. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10917. * @queue: The queue structure to free.
  10918. *
  10919. * This function frees a queue structure and the DMAable memory used for
  10920. * the host resident queue. This function must be called after destroying the
  10921. * queue on the HBA.
  10922. **/
  10923. void
  10924. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10925. {
  10926. struct lpfc_dmabuf *dmabuf;
  10927. if (!queue)
  10928. return;
  10929. while (!list_empty(&queue->page_list)) {
  10930. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10931. list);
  10932. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10933. dmabuf->virt, dmabuf->phys);
  10934. kfree(dmabuf);
  10935. }
  10936. kfree(queue);
  10937. return;
  10938. }
  10939. /**
  10940. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10941. * @phba: The HBA that this queue is being created on.
  10942. * @entry_size: The size of each queue entry for this queue.
  10943. * @entry count: The number of entries that this queue will handle.
  10944. *
  10945. * This function allocates a queue structure and the DMAable memory used for
  10946. * the host resident queue. This function must be called before creating the
  10947. * queue on the HBA.
  10948. **/
  10949. struct lpfc_queue *
  10950. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10951. uint32_t entry_count)
  10952. {
  10953. struct lpfc_queue *queue;
  10954. struct lpfc_dmabuf *dmabuf;
  10955. int x, total_qe_count;
  10956. void *dma_pointer;
  10957. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10958. if (!phba->sli4_hba.pc_sli4_params.supported)
  10959. hw_page_size = SLI4_PAGE_SIZE;
  10960. queue = kzalloc(sizeof(struct lpfc_queue) +
  10961. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  10962. if (!queue)
  10963. return NULL;
  10964. queue->page_count = (ALIGN(entry_size * entry_count,
  10965. hw_page_size))/hw_page_size;
  10966. INIT_LIST_HEAD(&queue->list);
  10967. INIT_LIST_HEAD(&queue->page_list);
  10968. INIT_LIST_HEAD(&queue->child_list);
  10969. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  10970. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  10971. if (!dmabuf)
  10972. goto out_fail;
  10973. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  10974. hw_page_size, &dmabuf->phys,
  10975. GFP_KERNEL);
  10976. if (!dmabuf->virt) {
  10977. kfree(dmabuf);
  10978. goto out_fail;
  10979. }
  10980. memset(dmabuf->virt, 0, hw_page_size);
  10981. dmabuf->buffer_tag = x;
  10982. list_add_tail(&dmabuf->list, &queue->page_list);
  10983. /* initialize queue's entry array */
  10984. dma_pointer = dmabuf->virt;
  10985. for (; total_qe_count < entry_count &&
  10986. dma_pointer < (hw_page_size + dmabuf->virt);
  10987. total_qe_count++, dma_pointer += entry_size) {
  10988. queue->qe[total_qe_count].address = dma_pointer;
  10989. }
  10990. }
  10991. queue->entry_size = entry_size;
  10992. queue->entry_count = entry_count;
  10993. /*
  10994. * entry_repost is calculated based on the number of entries in the
  10995. * queue. This works out except for RQs. If buffers are NOT initially
  10996. * posted for every RQE, entry_repost should be adjusted accordingly.
  10997. */
  10998. queue->entry_repost = (entry_count >> 3);
  10999. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  11000. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  11001. queue->phba = phba;
  11002. return queue;
  11003. out_fail:
  11004. lpfc_sli4_queue_free(queue);
  11005. return NULL;
  11006. }
  11007. /**
  11008. * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
  11009. * @phba: HBA structure that indicates port to create a queue on.
  11010. * @startq: The starting FCP EQ to modify
  11011. *
  11012. * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
  11013. *
  11014. * The @phba struct is used to send mailbox command to HBA. The @startq
  11015. * is used to get the starting FCP EQ to change.
  11016. * This function is asynchronous and will wait for the mailbox
  11017. * command to finish before continuing.
  11018. *
  11019. * On success this function will return a zero. If unable to allocate enough
  11020. * memory this function will return -ENOMEM. If the queue create mailbox command
  11021. * fails this function will return -ENXIO.
  11022. **/
  11023. uint32_t
  11024. lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
  11025. {
  11026. struct lpfc_mbx_modify_eq_delay *eq_delay;
  11027. LPFC_MBOXQ_t *mbox;
  11028. struct lpfc_queue *eq;
  11029. int cnt, rc, length, status = 0;
  11030. uint32_t shdr_status, shdr_add_status;
  11031. int fcp_eqidx;
  11032. union lpfc_sli4_cfg_shdr *shdr;
  11033. uint16_t dmult;
  11034. if (startq >= phba->cfg_fcp_eq_count)
  11035. return 0;
  11036. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11037. if (!mbox)
  11038. return -ENOMEM;
  11039. length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
  11040. sizeof(struct lpfc_sli4_cfg_mhdr));
  11041. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11042. LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
  11043. length, LPFC_SLI4_MBX_EMBED);
  11044. eq_delay = &mbox->u.mqe.un.eq_delay;
  11045. /* Calculate delay multiper from maximum interrupt per second */
  11046. dmult = LPFC_DMULT_CONST/phba->cfg_fcp_imax - 1;
  11047. cnt = 0;
  11048. for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_eq_count;
  11049. fcp_eqidx++) {
  11050. eq = phba->sli4_hba.fp_eq[fcp_eqidx];
  11051. if (!eq)
  11052. continue;
  11053. eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
  11054. eq_delay->u.request.eq[cnt].phase = 0;
  11055. eq_delay->u.request.eq[cnt].delay_multi = dmult;
  11056. cnt++;
  11057. if (cnt >= LPFC_MAX_EQ_DELAY)
  11058. break;
  11059. }
  11060. eq_delay->u.request.num_eq = cnt;
  11061. mbox->vport = phba->pport;
  11062. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11063. mbox->context1 = NULL;
  11064. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11065. shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
  11066. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11067. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11068. if (shdr_status || shdr_add_status || rc) {
  11069. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11070. "2512 MODIFY_EQ_DELAY mailbox failed with "
  11071. "status x%x add_status x%x, mbx status x%x\n",
  11072. shdr_status, shdr_add_status, rc);
  11073. status = -ENXIO;
  11074. }
  11075. mempool_free(mbox, phba->mbox_mem_pool);
  11076. return status;
  11077. }
  11078. /**
  11079. * lpfc_eq_create - Create an Event Queue on the HBA
  11080. * @phba: HBA structure that indicates port to create a queue on.
  11081. * @eq: The queue structure to use to create the event queue.
  11082. * @imax: The maximum interrupt per second limit.
  11083. *
  11084. * This function creates an event queue, as detailed in @eq, on a port,
  11085. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  11086. *
  11087. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  11088. * is used to get the entry count and entry size that are necessary to
  11089. * determine the number of pages to allocate and use for this queue. This
  11090. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  11091. * event queue. This function is asynchronous and will wait for the mailbox
  11092. * command to finish before continuing.
  11093. *
  11094. * On success this function will return a zero. If unable to allocate enough
  11095. * memory this function will return -ENOMEM. If the queue create mailbox command
  11096. * fails this function will return -ENXIO.
  11097. **/
  11098. uint32_t
  11099. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  11100. {
  11101. struct lpfc_mbx_eq_create *eq_create;
  11102. LPFC_MBOXQ_t *mbox;
  11103. int rc, length, status = 0;
  11104. struct lpfc_dmabuf *dmabuf;
  11105. uint32_t shdr_status, shdr_add_status;
  11106. union lpfc_sli4_cfg_shdr *shdr;
  11107. uint16_t dmult;
  11108. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11109. /* sanity check on queue memory */
  11110. if (!eq)
  11111. return -ENODEV;
  11112. if (!phba->sli4_hba.pc_sli4_params.supported)
  11113. hw_page_size = SLI4_PAGE_SIZE;
  11114. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11115. if (!mbox)
  11116. return -ENOMEM;
  11117. length = (sizeof(struct lpfc_mbx_eq_create) -
  11118. sizeof(struct lpfc_sli4_cfg_mhdr));
  11119. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11120. LPFC_MBOX_OPCODE_EQ_CREATE,
  11121. length, LPFC_SLI4_MBX_EMBED);
  11122. eq_create = &mbox->u.mqe.un.eq_create;
  11123. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  11124. eq->page_count);
  11125. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  11126. LPFC_EQE_SIZE);
  11127. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  11128. /* Calculate delay multiper from maximum interrupt per second */
  11129. dmult = LPFC_DMULT_CONST/imax - 1;
  11130. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  11131. dmult);
  11132. switch (eq->entry_count) {
  11133. default:
  11134. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11135. "0360 Unsupported EQ count. (%d)\n",
  11136. eq->entry_count);
  11137. if (eq->entry_count < 256)
  11138. return -EINVAL;
  11139. /* otherwise default to smallest count (drop through) */
  11140. case 256:
  11141. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11142. LPFC_EQ_CNT_256);
  11143. break;
  11144. case 512:
  11145. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11146. LPFC_EQ_CNT_512);
  11147. break;
  11148. case 1024:
  11149. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11150. LPFC_EQ_CNT_1024);
  11151. break;
  11152. case 2048:
  11153. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11154. LPFC_EQ_CNT_2048);
  11155. break;
  11156. case 4096:
  11157. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11158. LPFC_EQ_CNT_4096);
  11159. break;
  11160. }
  11161. list_for_each_entry(dmabuf, &eq->page_list, list) {
  11162. memset(dmabuf->virt, 0, hw_page_size);
  11163. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11164. putPaddrLow(dmabuf->phys);
  11165. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11166. putPaddrHigh(dmabuf->phys);
  11167. }
  11168. mbox->vport = phba->pport;
  11169. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11170. mbox->context1 = NULL;
  11171. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11172. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  11173. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11174. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11175. if (shdr_status || shdr_add_status || rc) {
  11176. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11177. "2500 EQ_CREATE mailbox failed with "
  11178. "status x%x add_status x%x, mbx status x%x\n",
  11179. shdr_status, shdr_add_status, rc);
  11180. status = -ENXIO;
  11181. }
  11182. eq->type = LPFC_EQ;
  11183. eq->subtype = LPFC_NONE;
  11184. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  11185. if (eq->queue_id == 0xFFFF)
  11186. status = -ENXIO;
  11187. eq->host_index = 0;
  11188. eq->hba_index = 0;
  11189. mempool_free(mbox, phba->mbox_mem_pool);
  11190. return status;
  11191. }
  11192. /**
  11193. * lpfc_cq_create - Create a Completion Queue on the HBA
  11194. * @phba: HBA structure that indicates port to create a queue on.
  11195. * @cq: The queue structure to use to create the completion queue.
  11196. * @eq: The event queue to bind this completion queue to.
  11197. *
  11198. * This function creates a completion queue, as detailed in @wq, on a port,
  11199. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  11200. *
  11201. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11202. * is used to get the entry count and entry size that are necessary to
  11203. * determine the number of pages to allocate and use for this queue. The @eq
  11204. * is used to indicate which event queue to bind this completion queue to. This
  11205. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  11206. * completion queue. This function is asynchronous and will wait for the mailbox
  11207. * command to finish before continuing.
  11208. *
  11209. * On success this function will return a zero. If unable to allocate enough
  11210. * memory this function will return -ENOMEM. If the queue create mailbox command
  11211. * fails this function will return -ENXIO.
  11212. **/
  11213. uint32_t
  11214. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11215. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  11216. {
  11217. struct lpfc_mbx_cq_create *cq_create;
  11218. struct lpfc_dmabuf *dmabuf;
  11219. LPFC_MBOXQ_t *mbox;
  11220. int rc, length, status = 0;
  11221. uint32_t shdr_status, shdr_add_status;
  11222. union lpfc_sli4_cfg_shdr *shdr;
  11223. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11224. /* sanity check on queue memory */
  11225. if (!cq || !eq)
  11226. return -ENODEV;
  11227. if (!phba->sli4_hba.pc_sli4_params.supported)
  11228. hw_page_size = SLI4_PAGE_SIZE;
  11229. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11230. if (!mbox)
  11231. return -ENOMEM;
  11232. length = (sizeof(struct lpfc_mbx_cq_create) -
  11233. sizeof(struct lpfc_sli4_cfg_mhdr));
  11234. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11235. LPFC_MBOX_OPCODE_CQ_CREATE,
  11236. length, LPFC_SLI4_MBX_EMBED);
  11237. cq_create = &mbox->u.mqe.un.cq_create;
  11238. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  11239. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  11240. cq->page_count);
  11241. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  11242. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  11243. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11244. phba->sli4_hba.pc_sli4_params.cqv);
  11245. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  11246. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  11247. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  11248. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  11249. eq->queue_id);
  11250. } else {
  11251. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  11252. eq->queue_id);
  11253. }
  11254. switch (cq->entry_count) {
  11255. default:
  11256. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11257. "0361 Unsupported CQ count. (%d)\n",
  11258. cq->entry_count);
  11259. if (cq->entry_count < 256) {
  11260. status = -EINVAL;
  11261. goto out;
  11262. }
  11263. /* otherwise default to smallest count (drop through) */
  11264. case 256:
  11265. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11266. LPFC_CQ_CNT_256);
  11267. break;
  11268. case 512:
  11269. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11270. LPFC_CQ_CNT_512);
  11271. break;
  11272. case 1024:
  11273. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11274. LPFC_CQ_CNT_1024);
  11275. break;
  11276. }
  11277. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11278. memset(dmabuf->virt, 0, hw_page_size);
  11279. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11280. putPaddrLow(dmabuf->phys);
  11281. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11282. putPaddrHigh(dmabuf->phys);
  11283. }
  11284. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11285. /* The IOCTL status is embedded in the mailbox subheader. */
  11286. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11287. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11288. if (shdr_status || shdr_add_status || rc) {
  11289. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11290. "2501 CQ_CREATE mailbox failed with "
  11291. "status x%x add_status x%x, mbx status x%x\n",
  11292. shdr_status, shdr_add_status, rc);
  11293. status = -ENXIO;
  11294. goto out;
  11295. }
  11296. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11297. if (cq->queue_id == 0xFFFF) {
  11298. status = -ENXIO;
  11299. goto out;
  11300. }
  11301. /* link the cq onto the parent eq child list */
  11302. list_add_tail(&cq->list, &eq->child_list);
  11303. /* Set up completion queue's type and subtype */
  11304. cq->type = type;
  11305. cq->subtype = subtype;
  11306. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11307. cq->assoc_qid = eq->queue_id;
  11308. cq->host_index = 0;
  11309. cq->hba_index = 0;
  11310. out:
  11311. mempool_free(mbox, phba->mbox_mem_pool);
  11312. return status;
  11313. }
  11314. /**
  11315. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11316. * @phba: HBA structure that indicates port to create a queue on.
  11317. * @mq: The queue structure to use to create the mailbox queue.
  11318. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11319. * @cq: The completion queue to associate with this cq.
  11320. *
  11321. * This function provides failback (fb) functionality when the
  11322. * mq_create_ext fails on older FW generations. It's purpose is identical
  11323. * to mq_create_ext otherwise.
  11324. *
  11325. * This routine cannot fail as all attributes were previously accessed and
  11326. * initialized in mq_create_ext.
  11327. **/
  11328. static void
  11329. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11330. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11331. {
  11332. struct lpfc_mbx_mq_create *mq_create;
  11333. struct lpfc_dmabuf *dmabuf;
  11334. int length;
  11335. length = (sizeof(struct lpfc_mbx_mq_create) -
  11336. sizeof(struct lpfc_sli4_cfg_mhdr));
  11337. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11338. LPFC_MBOX_OPCODE_MQ_CREATE,
  11339. length, LPFC_SLI4_MBX_EMBED);
  11340. mq_create = &mbox->u.mqe.un.mq_create;
  11341. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11342. mq->page_count);
  11343. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11344. cq->queue_id);
  11345. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11346. switch (mq->entry_count) {
  11347. case 16:
  11348. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11349. LPFC_MQ_RING_SIZE_16);
  11350. break;
  11351. case 32:
  11352. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11353. LPFC_MQ_RING_SIZE_32);
  11354. break;
  11355. case 64:
  11356. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11357. LPFC_MQ_RING_SIZE_64);
  11358. break;
  11359. case 128:
  11360. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11361. LPFC_MQ_RING_SIZE_128);
  11362. break;
  11363. }
  11364. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11365. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11366. putPaddrLow(dmabuf->phys);
  11367. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11368. putPaddrHigh(dmabuf->phys);
  11369. }
  11370. }
  11371. /**
  11372. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11373. * @phba: HBA structure that indicates port to create a queue on.
  11374. * @mq: The queue structure to use to create the mailbox queue.
  11375. * @cq: The completion queue to associate with this cq.
  11376. * @subtype: The queue's subtype.
  11377. *
  11378. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11379. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11380. *
  11381. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11382. * is used to get the entry count and entry size that are necessary to
  11383. * determine the number of pages to allocate and use for this queue. This
  11384. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  11385. * mailbox queue. This function is asynchronous and will wait for the mailbox
  11386. * command to finish before continuing.
  11387. *
  11388. * On success this function will return a zero. If unable to allocate enough
  11389. * memory this function will return -ENOMEM. If the queue create mailbox command
  11390. * fails this function will return -ENXIO.
  11391. **/
  11392. int32_t
  11393. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11394. struct lpfc_queue *cq, uint32_t subtype)
  11395. {
  11396. struct lpfc_mbx_mq_create *mq_create;
  11397. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11398. struct lpfc_dmabuf *dmabuf;
  11399. LPFC_MBOXQ_t *mbox;
  11400. int rc, length, status = 0;
  11401. uint32_t shdr_status, shdr_add_status;
  11402. union lpfc_sli4_cfg_shdr *shdr;
  11403. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11404. /* sanity check on queue memory */
  11405. if (!mq || !cq)
  11406. return -ENODEV;
  11407. if (!phba->sli4_hba.pc_sli4_params.supported)
  11408. hw_page_size = SLI4_PAGE_SIZE;
  11409. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11410. if (!mbox)
  11411. return -ENOMEM;
  11412. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11413. sizeof(struct lpfc_sli4_cfg_mhdr));
  11414. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11415. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11416. length, LPFC_SLI4_MBX_EMBED);
  11417. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11418. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11419. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11420. &mq_create_ext->u.request, mq->page_count);
  11421. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11422. &mq_create_ext->u.request, 1);
  11423. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11424. &mq_create_ext->u.request, 1);
  11425. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11426. &mq_create_ext->u.request, 1);
  11427. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11428. &mq_create_ext->u.request, 1);
  11429. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11430. &mq_create_ext->u.request, 1);
  11431. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11432. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11433. phba->sli4_hba.pc_sli4_params.mqv);
  11434. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11435. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11436. cq->queue_id);
  11437. else
  11438. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11439. cq->queue_id);
  11440. switch (mq->entry_count) {
  11441. default:
  11442. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11443. "0362 Unsupported MQ count. (%d)\n",
  11444. mq->entry_count);
  11445. if (mq->entry_count < 16) {
  11446. status = -EINVAL;
  11447. goto out;
  11448. }
  11449. /* otherwise default to smallest count (drop through) */
  11450. case 16:
  11451. bf_set(lpfc_mq_context_ring_size,
  11452. &mq_create_ext->u.request.context,
  11453. LPFC_MQ_RING_SIZE_16);
  11454. break;
  11455. case 32:
  11456. bf_set(lpfc_mq_context_ring_size,
  11457. &mq_create_ext->u.request.context,
  11458. LPFC_MQ_RING_SIZE_32);
  11459. break;
  11460. case 64:
  11461. bf_set(lpfc_mq_context_ring_size,
  11462. &mq_create_ext->u.request.context,
  11463. LPFC_MQ_RING_SIZE_64);
  11464. break;
  11465. case 128:
  11466. bf_set(lpfc_mq_context_ring_size,
  11467. &mq_create_ext->u.request.context,
  11468. LPFC_MQ_RING_SIZE_128);
  11469. break;
  11470. }
  11471. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11472. memset(dmabuf->virt, 0, hw_page_size);
  11473. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11474. putPaddrLow(dmabuf->phys);
  11475. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11476. putPaddrHigh(dmabuf->phys);
  11477. }
  11478. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11479. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11480. &mq_create_ext->u.response);
  11481. if (rc != MBX_SUCCESS) {
  11482. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11483. "2795 MQ_CREATE_EXT failed with "
  11484. "status x%x. Failback to MQ_CREATE.\n",
  11485. rc);
  11486. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11487. mq_create = &mbox->u.mqe.un.mq_create;
  11488. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11489. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11490. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11491. &mq_create->u.response);
  11492. }
  11493. /* The IOCTL status is embedded in the mailbox subheader. */
  11494. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11495. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11496. if (shdr_status || shdr_add_status || rc) {
  11497. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11498. "2502 MQ_CREATE mailbox failed with "
  11499. "status x%x add_status x%x, mbx status x%x\n",
  11500. shdr_status, shdr_add_status, rc);
  11501. status = -ENXIO;
  11502. goto out;
  11503. }
  11504. if (mq->queue_id == 0xFFFF) {
  11505. status = -ENXIO;
  11506. goto out;
  11507. }
  11508. mq->type = LPFC_MQ;
  11509. mq->assoc_qid = cq->queue_id;
  11510. mq->subtype = subtype;
  11511. mq->host_index = 0;
  11512. mq->hba_index = 0;
  11513. /* link the mq onto the parent cq child list */
  11514. list_add_tail(&mq->list, &cq->child_list);
  11515. out:
  11516. mempool_free(mbox, phba->mbox_mem_pool);
  11517. return status;
  11518. }
  11519. /**
  11520. * lpfc_wq_create - Create a Work Queue on the HBA
  11521. * @phba: HBA structure that indicates port to create a queue on.
  11522. * @wq: The queue structure to use to create the work queue.
  11523. * @cq: The completion queue to bind this work queue to.
  11524. * @subtype: The subtype of the work queue indicating its functionality.
  11525. *
  11526. * This function creates a work queue, as detailed in @wq, on a port, described
  11527. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11528. *
  11529. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11530. * is used to get the entry count and entry size that are necessary to
  11531. * determine the number of pages to allocate and use for this queue. The @cq
  11532. * is used to indicate which completion queue to bind this work queue to. This
  11533. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11534. * work queue. This function is asynchronous and will wait for the mailbox
  11535. * command to finish before continuing.
  11536. *
  11537. * On success this function will return a zero. If unable to allocate enough
  11538. * memory this function will return -ENOMEM. If the queue create mailbox command
  11539. * fails this function will return -ENXIO.
  11540. **/
  11541. uint32_t
  11542. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11543. struct lpfc_queue *cq, uint32_t subtype)
  11544. {
  11545. struct lpfc_mbx_wq_create *wq_create;
  11546. struct lpfc_dmabuf *dmabuf;
  11547. LPFC_MBOXQ_t *mbox;
  11548. int rc, length, status = 0;
  11549. uint32_t shdr_status, shdr_add_status;
  11550. union lpfc_sli4_cfg_shdr *shdr;
  11551. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11552. struct dma_address *page;
  11553. /* sanity check on queue memory */
  11554. if (!wq || !cq)
  11555. return -ENODEV;
  11556. if (!phba->sli4_hba.pc_sli4_params.supported)
  11557. hw_page_size = SLI4_PAGE_SIZE;
  11558. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11559. if (!mbox)
  11560. return -ENOMEM;
  11561. length = (sizeof(struct lpfc_mbx_wq_create) -
  11562. sizeof(struct lpfc_sli4_cfg_mhdr));
  11563. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11564. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11565. length, LPFC_SLI4_MBX_EMBED);
  11566. wq_create = &mbox->u.mqe.un.wq_create;
  11567. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11568. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11569. wq->page_count);
  11570. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11571. cq->queue_id);
  11572. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11573. phba->sli4_hba.pc_sli4_params.wqv);
  11574. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11575. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11576. wq->entry_count);
  11577. switch (wq->entry_size) {
  11578. default:
  11579. case 64:
  11580. bf_set(lpfc_mbx_wq_create_wqe_size,
  11581. &wq_create->u.request_1,
  11582. LPFC_WQ_WQE_SIZE_64);
  11583. break;
  11584. case 128:
  11585. bf_set(lpfc_mbx_wq_create_wqe_size,
  11586. &wq_create->u.request_1,
  11587. LPFC_WQ_WQE_SIZE_128);
  11588. break;
  11589. }
  11590. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11591. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11592. page = wq_create->u.request_1.page;
  11593. } else {
  11594. page = wq_create->u.request.page;
  11595. }
  11596. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11597. memset(dmabuf->virt, 0, hw_page_size);
  11598. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11599. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11600. }
  11601. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11602. /* The IOCTL status is embedded in the mailbox subheader. */
  11603. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11604. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11605. if (shdr_status || shdr_add_status || rc) {
  11606. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11607. "2503 WQ_CREATE mailbox failed with "
  11608. "status x%x add_status x%x, mbx status x%x\n",
  11609. shdr_status, shdr_add_status, rc);
  11610. status = -ENXIO;
  11611. goto out;
  11612. }
  11613. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11614. if (wq->queue_id == 0xFFFF) {
  11615. status = -ENXIO;
  11616. goto out;
  11617. }
  11618. wq->type = LPFC_WQ;
  11619. wq->assoc_qid = cq->queue_id;
  11620. wq->subtype = subtype;
  11621. wq->host_index = 0;
  11622. wq->hba_index = 0;
  11623. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  11624. /* link the wq onto the parent cq child list */
  11625. list_add_tail(&wq->list, &cq->child_list);
  11626. out:
  11627. mempool_free(mbox, phba->mbox_mem_pool);
  11628. return status;
  11629. }
  11630. /**
  11631. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11632. * @phba: HBA structure that indicates port to create a queue on.
  11633. * @rq: The queue structure to use for the receive queue.
  11634. * @qno: The associated HBQ number
  11635. *
  11636. *
  11637. * For SLI4 we need to adjust the RQ repost value based on
  11638. * the number of buffers that are initially posted to the RQ.
  11639. */
  11640. void
  11641. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11642. {
  11643. uint32_t cnt;
  11644. /* sanity check on queue memory */
  11645. if (!rq)
  11646. return;
  11647. cnt = lpfc_hbq_defs[qno]->entry_count;
  11648. /* Recalc repost for RQs based on buffers initially posted */
  11649. cnt = (cnt >> 3);
  11650. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11651. cnt = LPFC_QUEUE_MIN_REPOST;
  11652. rq->entry_repost = cnt;
  11653. }
  11654. /**
  11655. * lpfc_rq_create - Create a Receive Queue on the HBA
  11656. * @phba: HBA structure that indicates port to create a queue on.
  11657. * @hrq: The queue structure to use to create the header receive queue.
  11658. * @drq: The queue structure to use to create the data receive queue.
  11659. * @cq: The completion queue to bind this work queue to.
  11660. *
  11661. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11662. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11663. * to the HBA.
  11664. *
  11665. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11666. * struct is used to get the entry count that is necessary to determine the
  11667. * number of pages to use for this queue. The @cq is used to indicate which
  11668. * completion queue to bind received buffers that are posted to these queues to.
  11669. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11670. * receive queue pair. This function is asynchronous and will wait for the
  11671. * mailbox command to finish before continuing.
  11672. *
  11673. * On success this function will return a zero. If unable to allocate enough
  11674. * memory this function will return -ENOMEM. If the queue create mailbox command
  11675. * fails this function will return -ENXIO.
  11676. **/
  11677. uint32_t
  11678. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11679. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11680. {
  11681. struct lpfc_mbx_rq_create *rq_create;
  11682. struct lpfc_dmabuf *dmabuf;
  11683. LPFC_MBOXQ_t *mbox;
  11684. int rc, length, status = 0;
  11685. uint32_t shdr_status, shdr_add_status;
  11686. union lpfc_sli4_cfg_shdr *shdr;
  11687. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11688. /* sanity check on queue memory */
  11689. if (!hrq || !drq || !cq)
  11690. return -ENODEV;
  11691. if (!phba->sli4_hba.pc_sli4_params.supported)
  11692. hw_page_size = SLI4_PAGE_SIZE;
  11693. if (hrq->entry_count != drq->entry_count)
  11694. return -EINVAL;
  11695. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11696. if (!mbox)
  11697. return -ENOMEM;
  11698. length = (sizeof(struct lpfc_mbx_rq_create) -
  11699. sizeof(struct lpfc_sli4_cfg_mhdr));
  11700. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11701. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11702. length, LPFC_SLI4_MBX_EMBED);
  11703. rq_create = &mbox->u.mqe.un.rq_create;
  11704. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11705. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11706. phba->sli4_hba.pc_sli4_params.rqv);
  11707. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11708. bf_set(lpfc_rq_context_rqe_count_1,
  11709. &rq_create->u.request.context,
  11710. hrq->entry_count);
  11711. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11712. bf_set(lpfc_rq_context_rqe_size,
  11713. &rq_create->u.request.context,
  11714. LPFC_RQE_SIZE_8);
  11715. bf_set(lpfc_rq_context_page_size,
  11716. &rq_create->u.request.context,
  11717. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11718. } else {
  11719. switch (hrq->entry_count) {
  11720. default:
  11721. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11722. "2535 Unsupported RQ count. (%d)\n",
  11723. hrq->entry_count);
  11724. if (hrq->entry_count < 512) {
  11725. status = -EINVAL;
  11726. goto out;
  11727. }
  11728. /* otherwise default to smallest count (drop through) */
  11729. case 512:
  11730. bf_set(lpfc_rq_context_rqe_count,
  11731. &rq_create->u.request.context,
  11732. LPFC_RQ_RING_SIZE_512);
  11733. break;
  11734. case 1024:
  11735. bf_set(lpfc_rq_context_rqe_count,
  11736. &rq_create->u.request.context,
  11737. LPFC_RQ_RING_SIZE_1024);
  11738. break;
  11739. case 2048:
  11740. bf_set(lpfc_rq_context_rqe_count,
  11741. &rq_create->u.request.context,
  11742. LPFC_RQ_RING_SIZE_2048);
  11743. break;
  11744. case 4096:
  11745. bf_set(lpfc_rq_context_rqe_count,
  11746. &rq_create->u.request.context,
  11747. LPFC_RQ_RING_SIZE_4096);
  11748. break;
  11749. }
  11750. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11751. LPFC_HDR_BUF_SIZE);
  11752. }
  11753. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11754. cq->queue_id);
  11755. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11756. hrq->page_count);
  11757. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11758. memset(dmabuf->virt, 0, hw_page_size);
  11759. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11760. putPaddrLow(dmabuf->phys);
  11761. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11762. putPaddrHigh(dmabuf->phys);
  11763. }
  11764. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11765. /* The IOCTL status is embedded in the mailbox subheader. */
  11766. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11767. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11768. if (shdr_status || shdr_add_status || rc) {
  11769. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11770. "2504 RQ_CREATE mailbox failed with "
  11771. "status x%x add_status x%x, mbx status x%x\n",
  11772. shdr_status, shdr_add_status, rc);
  11773. status = -ENXIO;
  11774. goto out;
  11775. }
  11776. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11777. if (hrq->queue_id == 0xFFFF) {
  11778. status = -ENXIO;
  11779. goto out;
  11780. }
  11781. hrq->type = LPFC_HRQ;
  11782. hrq->assoc_qid = cq->queue_id;
  11783. hrq->subtype = subtype;
  11784. hrq->host_index = 0;
  11785. hrq->hba_index = 0;
  11786. /* now create the data queue */
  11787. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11788. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11789. length, LPFC_SLI4_MBX_EMBED);
  11790. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11791. phba->sli4_hba.pc_sli4_params.rqv);
  11792. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11793. bf_set(lpfc_rq_context_rqe_count_1,
  11794. &rq_create->u.request.context, hrq->entry_count);
  11795. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11796. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11797. LPFC_RQE_SIZE_8);
  11798. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11799. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11800. } else {
  11801. switch (drq->entry_count) {
  11802. default:
  11803. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11804. "2536 Unsupported RQ count. (%d)\n",
  11805. drq->entry_count);
  11806. if (drq->entry_count < 512) {
  11807. status = -EINVAL;
  11808. goto out;
  11809. }
  11810. /* otherwise default to smallest count (drop through) */
  11811. case 512:
  11812. bf_set(lpfc_rq_context_rqe_count,
  11813. &rq_create->u.request.context,
  11814. LPFC_RQ_RING_SIZE_512);
  11815. break;
  11816. case 1024:
  11817. bf_set(lpfc_rq_context_rqe_count,
  11818. &rq_create->u.request.context,
  11819. LPFC_RQ_RING_SIZE_1024);
  11820. break;
  11821. case 2048:
  11822. bf_set(lpfc_rq_context_rqe_count,
  11823. &rq_create->u.request.context,
  11824. LPFC_RQ_RING_SIZE_2048);
  11825. break;
  11826. case 4096:
  11827. bf_set(lpfc_rq_context_rqe_count,
  11828. &rq_create->u.request.context,
  11829. LPFC_RQ_RING_SIZE_4096);
  11830. break;
  11831. }
  11832. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11833. LPFC_DATA_BUF_SIZE);
  11834. }
  11835. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11836. cq->queue_id);
  11837. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11838. drq->page_count);
  11839. list_for_each_entry(dmabuf, &drq->page_list, list) {
  11840. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11841. putPaddrLow(dmabuf->phys);
  11842. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11843. putPaddrHigh(dmabuf->phys);
  11844. }
  11845. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11846. /* The IOCTL status is embedded in the mailbox subheader. */
  11847. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11848. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11849. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11850. if (shdr_status || shdr_add_status || rc) {
  11851. status = -ENXIO;
  11852. goto out;
  11853. }
  11854. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11855. if (drq->queue_id == 0xFFFF) {
  11856. status = -ENXIO;
  11857. goto out;
  11858. }
  11859. drq->type = LPFC_DRQ;
  11860. drq->assoc_qid = cq->queue_id;
  11861. drq->subtype = subtype;
  11862. drq->host_index = 0;
  11863. drq->hba_index = 0;
  11864. /* link the header and data RQs onto the parent cq child list */
  11865. list_add_tail(&hrq->list, &cq->child_list);
  11866. list_add_tail(&drq->list, &cq->child_list);
  11867. out:
  11868. mempool_free(mbox, phba->mbox_mem_pool);
  11869. return status;
  11870. }
  11871. /**
  11872. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  11873. * @eq: The queue structure associated with the queue to destroy.
  11874. *
  11875. * This function destroys a queue, as detailed in @eq by sending an mailbox
  11876. * command, specific to the type of queue, to the HBA.
  11877. *
  11878. * The @eq struct is used to get the queue ID of the queue to destroy.
  11879. *
  11880. * On success this function will return a zero. If the queue destroy mailbox
  11881. * command fails this function will return -ENXIO.
  11882. **/
  11883. uint32_t
  11884. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11885. {
  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. /* sanity check on queue memory */
  11891. if (!eq)
  11892. return -ENODEV;
  11893. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  11894. if (!mbox)
  11895. return -ENOMEM;
  11896. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  11897. sizeof(struct lpfc_sli4_cfg_mhdr));
  11898. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11899. LPFC_MBOX_OPCODE_EQ_DESTROY,
  11900. length, LPFC_SLI4_MBX_EMBED);
  11901. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  11902. eq->queue_id);
  11903. mbox->vport = eq->phba->pport;
  11904. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11905. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  11906. /* The IOCTL status is embedded in the mailbox subheader. */
  11907. shdr = (union lpfc_sli4_cfg_shdr *)
  11908. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  11909. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11910. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11911. if (shdr_status || shdr_add_status || rc) {
  11912. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11913. "2505 EQ_DESTROY mailbox failed with "
  11914. "status x%x add_status x%x, mbx status x%x\n",
  11915. shdr_status, shdr_add_status, rc);
  11916. status = -ENXIO;
  11917. }
  11918. /* Remove eq from any list */
  11919. list_del_init(&eq->list);
  11920. mempool_free(mbox, eq->phba->mbox_mem_pool);
  11921. return status;
  11922. }
  11923. /**
  11924. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  11925. * @cq: The queue structure associated with the queue to destroy.
  11926. *
  11927. * This function destroys a queue, as detailed in @cq by sending an mailbox
  11928. * command, specific to the type of queue, to the HBA.
  11929. *
  11930. * The @cq struct is used to get the queue ID of the queue to destroy.
  11931. *
  11932. * On success this function will return a zero. If the queue destroy mailbox
  11933. * command fails this function will return -ENXIO.
  11934. **/
  11935. uint32_t
  11936. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  11937. {
  11938. LPFC_MBOXQ_t *mbox;
  11939. int rc, length, status = 0;
  11940. uint32_t shdr_status, shdr_add_status;
  11941. union lpfc_sli4_cfg_shdr *shdr;
  11942. /* sanity check on queue memory */
  11943. if (!cq)
  11944. return -ENODEV;
  11945. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  11946. if (!mbox)
  11947. return -ENOMEM;
  11948. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  11949. sizeof(struct lpfc_sli4_cfg_mhdr));
  11950. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11951. LPFC_MBOX_OPCODE_CQ_DESTROY,
  11952. length, LPFC_SLI4_MBX_EMBED);
  11953. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  11954. cq->queue_id);
  11955. mbox->vport = cq->phba->pport;
  11956. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11957. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  11958. /* The IOCTL status is embedded in the mailbox subheader. */
  11959. shdr = (union lpfc_sli4_cfg_shdr *)
  11960. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  11961. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11962. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11963. if (shdr_status || shdr_add_status || rc) {
  11964. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11965. "2506 CQ_DESTROY mailbox failed with "
  11966. "status x%x add_status x%x, mbx status x%x\n",
  11967. shdr_status, shdr_add_status, rc);
  11968. status = -ENXIO;
  11969. }
  11970. /* Remove cq from any list */
  11971. list_del_init(&cq->list);
  11972. mempool_free(mbox, cq->phba->mbox_mem_pool);
  11973. return status;
  11974. }
  11975. /**
  11976. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  11977. * @qm: The queue structure associated with the queue to destroy.
  11978. *
  11979. * This function destroys a queue, as detailed in @mq by sending an mailbox
  11980. * command, specific to the type of queue, to the HBA.
  11981. *
  11982. * The @mq struct is used to get the queue ID of the queue to destroy.
  11983. *
  11984. * On success this function will return a zero. If the queue destroy mailbox
  11985. * command fails this function will return -ENXIO.
  11986. **/
  11987. uint32_t
  11988. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  11989. {
  11990. LPFC_MBOXQ_t *mbox;
  11991. int rc, length, status = 0;
  11992. uint32_t shdr_status, shdr_add_status;
  11993. union lpfc_sli4_cfg_shdr *shdr;
  11994. /* sanity check on queue memory */
  11995. if (!mq)
  11996. return -ENODEV;
  11997. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  11998. if (!mbox)
  11999. return -ENOMEM;
  12000. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  12001. sizeof(struct lpfc_sli4_cfg_mhdr));
  12002. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12003. LPFC_MBOX_OPCODE_MQ_DESTROY,
  12004. length, LPFC_SLI4_MBX_EMBED);
  12005. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  12006. mq->queue_id);
  12007. mbox->vport = mq->phba->pport;
  12008. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12009. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  12010. /* The IOCTL status is embedded in the mailbox subheader. */
  12011. shdr = (union lpfc_sli4_cfg_shdr *)
  12012. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  12013. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12014. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12015. if (shdr_status || shdr_add_status || rc) {
  12016. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12017. "2507 MQ_DESTROY mailbox failed with "
  12018. "status x%x add_status x%x, mbx status x%x\n",
  12019. shdr_status, shdr_add_status, rc);
  12020. status = -ENXIO;
  12021. }
  12022. /* Remove mq from any list */
  12023. list_del_init(&mq->list);
  12024. mempool_free(mbox, mq->phba->mbox_mem_pool);
  12025. return status;
  12026. }
  12027. /**
  12028. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  12029. * @wq: The queue structure associated with the queue to destroy.
  12030. *
  12031. * This function destroys a queue, as detailed in @wq by sending an mailbox
  12032. * command, specific to the type of queue, to the HBA.
  12033. *
  12034. * The @wq struct is used to get the queue ID of the queue to destroy.
  12035. *
  12036. * On success this function will return a zero. If the queue destroy mailbox
  12037. * command fails this function will return -ENXIO.
  12038. **/
  12039. uint32_t
  12040. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  12041. {
  12042. LPFC_MBOXQ_t *mbox;
  12043. int rc, length, status = 0;
  12044. uint32_t shdr_status, shdr_add_status;
  12045. union lpfc_sli4_cfg_shdr *shdr;
  12046. /* sanity check on queue memory */
  12047. if (!wq)
  12048. return -ENODEV;
  12049. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  12050. if (!mbox)
  12051. return -ENOMEM;
  12052. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  12053. sizeof(struct lpfc_sli4_cfg_mhdr));
  12054. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12055. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  12056. length, LPFC_SLI4_MBX_EMBED);
  12057. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  12058. wq->queue_id);
  12059. mbox->vport = wq->phba->pport;
  12060. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12061. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  12062. shdr = (union lpfc_sli4_cfg_shdr *)
  12063. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  12064. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12065. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12066. if (shdr_status || shdr_add_status || rc) {
  12067. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12068. "2508 WQ_DESTROY mailbox failed with "
  12069. "status x%x add_status x%x, mbx status x%x\n",
  12070. shdr_status, shdr_add_status, rc);
  12071. status = -ENXIO;
  12072. }
  12073. /* Remove wq from any list */
  12074. list_del_init(&wq->list);
  12075. mempool_free(mbox, wq->phba->mbox_mem_pool);
  12076. return status;
  12077. }
  12078. /**
  12079. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  12080. * @rq: The queue structure associated with the queue to destroy.
  12081. *
  12082. * This function destroys a queue, as detailed in @rq by sending an mailbox
  12083. * command, specific to the type of queue, to the HBA.
  12084. *
  12085. * The @rq struct is used to get the queue ID of the queue to destroy.
  12086. *
  12087. * On success this function will return a zero. If the queue destroy mailbox
  12088. * command fails this function will return -ENXIO.
  12089. **/
  12090. uint32_t
  12091. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  12092. struct lpfc_queue *drq)
  12093. {
  12094. LPFC_MBOXQ_t *mbox;
  12095. int rc, length, status = 0;
  12096. uint32_t shdr_status, shdr_add_status;
  12097. union lpfc_sli4_cfg_shdr *shdr;
  12098. /* sanity check on queue memory */
  12099. if (!hrq || !drq)
  12100. return -ENODEV;
  12101. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  12102. if (!mbox)
  12103. return -ENOMEM;
  12104. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  12105. sizeof(struct lpfc_sli4_cfg_mhdr));
  12106. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12107. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  12108. length, LPFC_SLI4_MBX_EMBED);
  12109. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12110. hrq->queue_id);
  12111. mbox->vport = hrq->phba->pport;
  12112. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12113. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  12114. /* The IOCTL status is embedded in the mailbox subheader. */
  12115. shdr = (union lpfc_sli4_cfg_shdr *)
  12116. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12117. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12118. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12119. if (shdr_status || shdr_add_status || rc) {
  12120. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12121. "2509 RQ_DESTROY mailbox failed with "
  12122. "status x%x add_status x%x, mbx status x%x\n",
  12123. shdr_status, shdr_add_status, rc);
  12124. if (rc != MBX_TIMEOUT)
  12125. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12126. return -ENXIO;
  12127. }
  12128. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12129. drq->queue_id);
  12130. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  12131. shdr = (union lpfc_sli4_cfg_shdr *)
  12132. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12133. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12134. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12135. if (shdr_status || shdr_add_status || rc) {
  12136. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12137. "2510 RQ_DESTROY mailbox failed with "
  12138. "status x%x add_status x%x, mbx status x%x\n",
  12139. shdr_status, shdr_add_status, rc);
  12140. status = -ENXIO;
  12141. }
  12142. list_del_init(&hrq->list);
  12143. list_del_init(&drq->list);
  12144. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12145. return status;
  12146. }
  12147. /**
  12148. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  12149. * @phba: The virtual port for which this call being executed.
  12150. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  12151. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  12152. * @xritag: the xritag that ties this io to the SGL pages.
  12153. *
  12154. * This routine will post the sgl pages for the IO that has the xritag
  12155. * that is in the iocbq structure. The xritag is assigned during iocbq
  12156. * creation and persists for as long as the driver is loaded.
  12157. * if the caller has fewer than 256 scatter gather segments to map then
  12158. * pdma_phys_addr1 should be 0.
  12159. * If the caller needs to map more than 256 scatter gather segment then
  12160. * pdma_phys_addr1 should be a valid physical address.
  12161. * physical address for SGLs must be 64 byte aligned.
  12162. * If you are going to map 2 SGL's then the first one must have 256 entries
  12163. * the second sgl can have between 1 and 256 entries.
  12164. *
  12165. * Return codes:
  12166. * 0 - Success
  12167. * -ENXIO, -ENOMEM - Failure
  12168. **/
  12169. int
  12170. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  12171. dma_addr_t pdma_phys_addr0,
  12172. dma_addr_t pdma_phys_addr1,
  12173. uint16_t xritag)
  12174. {
  12175. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  12176. LPFC_MBOXQ_t *mbox;
  12177. int rc;
  12178. uint32_t shdr_status, shdr_add_status;
  12179. uint32_t mbox_tmo;
  12180. union lpfc_sli4_cfg_shdr *shdr;
  12181. if (xritag == NO_XRI) {
  12182. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12183. "0364 Invalid param:\n");
  12184. return -EINVAL;
  12185. }
  12186. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12187. if (!mbox)
  12188. return -ENOMEM;
  12189. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12190. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12191. sizeof(struct lpfc_mbx_post_sgl_pages) -
  12192. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  12193. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  12194. &mbox->u.mqe.un.post_sgl_pages;
  12195. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  12196. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  12197. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  12198. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  12199. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  12200. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  12201. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  12202. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  12203. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  12204. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  12205. if (!phba->sli4_hba.intr_enable)
  12206. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12207. else {
  12208. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12209. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12210. }
  12211. /* The IOCTL status is embedded in the mailbox subheader. */
  12212. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  12213. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12214. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12215. if (rc != MBX_TIMEOUT)
  12216. mempool_free(mbox, phba->mbox_mem_pool);
  12217. if (shdr_status || shdr_add_status || rc) {
  12218. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12219. "2511 POST_SGL mailbox failed with "
  12220. "status x%x add_status x%x, mbx status x%x\n",
  12221. shdr_status, shdr_add_status, rc);
  12222. rc = -ENXIO;
  12223. }
  12224. return 0;
  12225. }
  12226. /**
  12227. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  12228. * @phba: pointer to lpfc hba data structure.
  12229. *
  12230. * This routine is invoked to post rpi header templates to the
  12231. * HBA consistent with the SLI-4 interface spec. This routine
  12232. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  12233. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  12234. *
  12235. * Returns
  12236. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  12237. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  12238. **/
  12239. uint16_t
  12240. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  12241. {
  12242. unsigned long xri;
  12243. /*
  12244. * Fetch the next logical xri. Because this index is logical,
  12245. * the driver starts at 0 each time.
  12246. */
  12247. spin_lock_irq(&phba->hbalock);
  12248. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  12249. phba->sli4_hba.max_cfg_param.max_xri, 0);
  12250. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  12251. spin_unlock_irq(&phba->hbalock);
  12252. return NO_XRI;
  12253. } else {
  12254. set_bit(xri, phba->sli4_hba.xri_bmask);
  12255. phba->sli4_hba.max_cfg_param.xri_used++;
  12256. }
  12257. spin_unlock_irq(&phba->hbalock);
  12258. return xri;
  12259. }
  12260. /**
  12261. * lpfc_sli4_free_xri - Release an xri for reuse.
  12262. * @phba: pointer to lpfc hba data structure.
  12263. *
  12264. * This routine is invoked to release an xri to the pool of
  12265. * available rpis maintained by the driver.
  12266. **/
  12267. void
  12268. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12269. {
  12270. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  12271. phba->sli4_hba.max_cfg_param.xri_used--;
  12272. }
  12273. }
  12274. /**
  12275. * lpfc_sli4_free_xri - Release an xri for reuse.
  12276. * @phba: pointer to lpfc hba data structure.
  12277. *
  12278. * This routine is invoked to release an xri to the pool of
  12279. * available rpis maintained by the driver.
  12280. **/
  12281. void
  12282. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12283. {
  12284. spin_lock_irq(&phba->hbalock);
  12285. __lpfc_sli4_free_xri(phba, xri);
  12286. spin_unlock_irq(&phba->hbalock);
  12287. }
  12288. /**
  12289. * lpfc_sli4_next_xritag - Get an xritag for the io
  12290. * @phba: Pointer to HBA context object.
  12291. *
  12292. * This function gets an xritag for the iocb. If there is no unused xritag
  12293. * it will return 0xffff.
  12294. * The function returns the allocated xritag if successful, else returns zero.
  12295. * Zero is not a valid xritag.
  12296. * The caller is not required to hold any lock.
  12297. **/
  12298. uint16_t
  12299. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  12300. {
  12301. uint16_t xri_index;
  12302. xri_index = lpfc_sli4_alloc_xri(phba);
  12303. if (xri_index == NO_XRI)
  12304. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  12305. "2004 Failed to allocate XRI.last XRITAG is %d"
  12306. " Max XRI is %d, Used XRI is %d\n",
  12307. xri_index,
  12308. phba->sli4_hba.max_cfg_param.max_xri,
  12309. phba->sli4_hba.max_cfg_param.xri_used);
  12310. return xri_index;
  12311. }
  12312. /**
  12313. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  12314. * @phba: pointer to lpfc hba data structure.
  12315. * @post_sgl_list: pointer to els sgl entry list.
  12316. * @count: number of els sgl entries on the list.
  12317. *
  12318. * This routine is invoked to post a block of driver's sgl pages to the
  12319. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12320. * is only called when the driver is loading and after all IO has been
  12321. * stopped.
  12322. **/
  12323. static int
  12324. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
  12325. struct list_head *post_sgl_list,
  12326. int post_cnt)
  12327. {
  12328. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  12329. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12330. struct sgl_page_pairs *sgl_pg_pairs;
  12331. void *viraddr;
  12332. LPFC_MBOXQ_t *mbox;
  12333. uint32_t reqlen, alloclen, pg_pairs;
  12334. uint32_t mbox_tmo;
  12335. uint16_t xritag_start = 0;
  12336. int rc = 0;
  12337. uint32_t shdr_status, shdr_add_status;
  12338. union lpfc_sli4_cfg_shdr *shdr;
  12339. reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12340. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12341. if (reqlen > SLI4_PAGE_SIZE) {
  12342. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12343. "2559 Block sgl registration required DMA "
  12344. "size (%d) great than a page\n", reqlen);
  12345. return -ENOMEM;
  12346. }
  12347. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12348. if (!mbox)
  12349. return -ENOMEM;
  12350. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12351. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12352. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12353. LPFC_SLI4_MBX_NEMBED);
  12354. if (alloclen < reqlen) {
  12355. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12356. "0285 Allocated DMA memory size (%d) is "
  12357. "less than the requested DMA memory "
  12358. "size (%d)\n", alloclen, reqlen);
  12359. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12360. return -ENOMEM;
  12361. }
  12362. /* Set up the SGL pages in the non-embedded DMA pages */
  12363. viraddr = mbox->sge_array->addr[0];
  12364. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12365. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12366. pg_pairs = 0;
  12367. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  12368. /* Set up the sge entry */
  12369. sgl_pg_pairs->sgl_pg0_addr_lo =
  12370. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12371. sgl_pg_pairs->sgl_pg0_addr_hi =
  12372. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12373. sgl_pg_pairs->sgl_pg1_addr_lo =
  12374. cpu_to_le32(putPaddrLow(0));
  12375. sgl_pg_pairs->sgl_pg1_addr_hi =
  12376. cpu_to_le32(putPaddrHigh(0));
  12377. /* Keep the first xritag on the list */
  12378. if (pg_pairs == 0)
  12379. xritag_start = sglq_entry->sli4_xritag;
  12380. sgl_pg_pairs++;
  12381. pg_pairs++;
  12382. }
  12383. /* Complete initialization and perform endian conversion. */
  12384. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12385. bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
  12386. sgl->word0 = cpu_to_le32(sgl->word0);
  12387. if (!phba->sli4_hba.intr_enable)
  12388. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12389. else {
  12390. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12391. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12392. }
  12393. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12394. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12395. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12396. if (rc != MBX_TIMEOUT)
  12397. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12398. if (shdr_status || shdr_add_status || rc) {
  12399. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12400. "2513 POST_SGL_BLOCK mailbox command failed "
  12401. "status x%x add_status x%x mbx status x%x\n",
  12402. shdr_status, shdr_add_status, rc);
  12403. rc = -ENXIO;
  12404. }
  12405. return rc;
  12406. }
  12407. /**
  12408. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12409. * @phba: pointer to lpfc hba data structure.
  12410. * @sblist: pointer to scsi buffer list.
  12411. * @count: number of scsi buffers on the list.
  12412. *
  12413. * This routine is invoked to post a block of @count scsi sgl pages from a
  12414. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12415. * No Lock is held.
  12416. *
  12417. **/
  12418. int
  12419. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  12420. struct list_head *sblist,
  12421. int count)
  12422. {
  12423. struct lpfc_scsi_buf *psb;
  12424. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12425. struct sgl_page_pairs *sgl_pg_pairs;
  12426. void *viraddr;
  12427. LPFC_MBOXQ_t *mbox;
  12428. uint32_t reqlen, alloclen, pg_pairs;
  12429. uint32_t mbox_tmo;
  12430. uint16_t xritag_start = 0;
  12431. int rc = 0;
  12432. uint32_t shdr_status, shdr_add_status;
  12433. dma_addr_t pdma_phys_bpl1;
  12434. union lpfc_sli4_cfg_shdr *shdr;
  12435. /* Calculate the requested length of the dma memory */
  12436. reqlen = count * sizeof(struct sgl_page_pairs) +
  12437. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12438. if (reqlen > SLI4_PAGE_SIZE) {
  12439. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12440. "0217 Block sgl registration required DMA "
  12441. "size (%d) great than a page\n", reqlen);
  12442. return -ENOMEM;
  12443. }
  12444. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12445. if (!mbox) {
  12446. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12447. "0283 Failed to allocate mbox cmd memory\n");
  12448. return -ENOMEM;
  12449. }
  12450. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12451. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12452. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12453. LPFC_SLI4_MBX_NEMBED);
  12454. if (alloclen < reqlen) {
  12455. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12456. "2561 Allocated DMA memory size (%d) is "
  12457. "less than the requested DMA memory "
  12458. "size (%d)\n", alloclen, reqlen);
  12459. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12460. return -ENOMEM;
  12461. }
  12462. /* Get the first SGE entry from the non-embedded DMA memory */
  12463. viraddr = mbox->sge_array->addr[0];
  12464. /* Set up the SGL pages in the non-embedded DMA pages */
  12465. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12466. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12467. pg_pairs = 0;
  12468. list_for_each_entry(psb, sblist, list) {
  12469. /* Set up the sge entry */
  12470. sgl_pg_pairs->sgl_pg0_addr_lo =
  12471. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12472. sgl_pg_pairs->sgl_pg0_addr_hi =
  12473. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12474. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12475. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12476. else
  12477. pdma_phys_bpl1 = 0;
  12478. sgl_pg_pairs->sgl_pg1_addr_lo =
  12479. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12480. sgl_pg_pairs->sgl_pg1_addr_hi =
  12481. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12482. /* Keep the first xritag on the list */
  12483. if (pg_pairs == 0)
  12484. xritag_start = psb->cur_iocbq.sli4_xritag;
  12485. sgl_pg_pairs++;
  12486. pg_pairs++;
  12487. }
  12488. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12489. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12490. /* Perform endian conversion if necessary */
  12491. sgl->word0 = cpu_to_le32(sgl->word0);
  12492. if (!phba->sli4_hba.intr_enable)
  12493. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12494. else {
  12495. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12496. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12497. }
  12498. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12499. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12500. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12501. if (rc != MBX_TIMEOUT)
  12502. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12503. if (shdr_status || shdr_add_status || rc) {
  12504. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12505. "2564 POST_SGL_BLOCK mailbox command failed "
  12506. "status x%x add_status x%x mbx status x%x\n",
  12507. shdr_status, shdr_add_status, rc);
  12508. rc = -ENXIO;
  12509. }
  12510. return rc;
  12511. }
  12512. /**
  12513. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12514. * @phba: pointer to lpfc_hba struct that the frame was received on
  12515. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12516. *
  12517. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12518. * valid type of frame that the LPFC driver will handle. This function will
  12519. * return a zero if the frame is a valid frame or a non zero value when the
  12520. * frame does not pass the check.
  12521. **/
  12522. static int
  12523. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12524. {
  12525. /* make rctl_names static to save stack space */
  12526. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12527. char *type_names[] = FC_TYPE_NAMES_INIT;
  12528. struct fc_vft_header *fc_vft_hdr;
  12529. uint32_t *header = (uint32_t *) fc_hdr;
  12530. switch (fc_hdr->fh_r_ctl) {
  12531. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12532. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12533. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12534. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12535. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12536. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12537. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12538. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12539. case FC_RCTL_ELS_REQ: /* extended link services request */
  12540. case FC_RCTL_ELS_REP: /* extended link services reply */
  12541. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12542. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12543. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12544. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12545. case FC_RCTL_BA_RMC: /* remove connection */
  12546. case FC_RCTL_BA_ACC: /* basic accept */
  12547. case FC_RCTL_BA_RJT: /* basic reject */
  12548. case FC_RCTL_BA_PRMT:
  12549. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12550. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12551. case FC_RCTL_P_RJT: /* port reject */
  12552. case FC_RCTL_F_RJT: /* fabric reject */
  12553. case FC_RCTL_P_BSY: /* port busy */
  12554. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12555. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12556. case FC_RCTL_LCR: /* link credit reset */
  12557. case FC_RCTL_END: /* end */
  12558. break;
  12559. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12560. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12561. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12562. return lpfc_fc_frame_check(phba, fc_hdr);
  12563. default:
  12564. goto drop;
  12565. }
  12566. switch (fc_hdr->fh_type) {
  12567. case FC_TYPE_BLS:
  12568. case FC_TYPE_ELS:
  12569. case FC_TYPE_FCP:
  12570. case FC_TYPE_CT:
  12571. break;
  12572. case FC_TYPE_IP:
  12573. case FC_TYPE_ILS:
  12574. default:
  12575. goto drop;
  12576. }
  12577. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12578. "2538 Received frame rctl:%s type:%s "
  12579. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12580. rctl_names[fc_hdr->fh_r_ctl],
  12581. type_names[fc_hdr->fh_type],
  12582. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12583. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12584. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12585. return 0;
  12586. drop:
  12587. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12588. "2539 Dropped frame rctl:%s type:%s\n",
  12589. rctl_names[fc_hdr->fh_r_ctl],
  12590. type_names[fc_hdr->fh_type]);
  12591. return 1;
  12592. }
  12593. /**
  12594. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12595. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12596. *
  12597. * This function processes the FC header to retrieve the VFI from the VF
  12598. * header, if one exists. This function will return the VFI if one exists
  12599. * or 0 if no VSAN Header exists.
  12600. **/
  12601. static uint32_t
  12602. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12603. {
  12604. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12605. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12606. return 0;
  12607. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12608. }
  12609. /**
  12610. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12611. * @phba: Pointer to the HBA structure to search for the vport on
  12612. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12613. * @fcfi: The FC Fabric ID that the frame came from
  12614. *
  12615. * This function searches the @phba for a vport that matches the content of the
  12616. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12617. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12618. * returns the matching vport pointer or NULL if unable to match frame to a
  12619. * vport.
  12620. **/
  12621. static struct lpfc_vport *
  12622. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12623. uint16_t fcfi)
  12624. {
  12625. struct lpfc_vport **vports;
  12626. struct lpfc_vport *vport = NULL;
  12627. int i;
  12628. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12629. fc_hdr->fh_d_id[1] << 8 |
  12630. fc_hdr->fh_d_id[2]);
  12631. if (did == Fabric_DID)
  12632. return phba->pport;
  12633. if ((phba->pport->fc_flag & FC_PT2PT) &&
  12634. !(phba->link_state == LPFC_HBA_READY))
  12635. return phba->pport;
  12636. vports = lpfc_create_vport_work_array(phba);
  12637. if (vports != NULL)
  12638. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12639. if (phba->fcf.fcfi == fcfi &&
  12640. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12641. vports[i]->fc_myDID == did) {
  12642. vport = vports[i];
  12643. break;
  12644. }
  12645. }
  12646. lpfc_destroy_vport_work_array(phba, vports);
  12647. return vport;
  12648. }
  12649. /**
  12650. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12651. * @vport: The vport to work on.
  12652. *
  12653. * This function updates the receive sequence time stamp for this vport. The
  12654. * receive sequence time stamp indicates the time that the last frame of the
  12655. * the sequence that has been idle for the longest amount of time was received.
  12656. * the driver uses this time stamp to indicate if any received sequences have
  12657. * timed out.
  12658. **/
  12659. void
  12660. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12661. {
  12662. struct lpfc_dmabuf *h_buf;
  12663. struct hbq_dmabuf *dmabuf = NULL;
  12664. /* get the oldest sequence on the rcv list */
  12665. h_buf = list_get_first(&vport->rcv_buffer_list,
  12666. struct lpfc_dmabuf, list);
  12667. if (!h_buf)
  12668. return;
  12669. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12670. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12671. }
  12672. /**
  12673. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12674. * @vport: The vport that the received sequences were sent to.
  12675. *
  12676. * This function cleans up all outstanding received sequences. This is called
  12677. * by the driver when a link event or user action invalidates all the received
  12678. * sequences.
  12679. **/
  12680. void
  12681. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12682. {
  12683. struct lpfc_dmabuf *h_buf, *hnext;
  12684. struct lpfc_dmabuf *d_buf, *dnext;
  12685. struct hbq_dmabuf *dmabuf = NULL;
  12686. /* start with the oldest sequence on the rcv list */
  12687. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12688. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12689. list_del_init(&dmabuf->hbuf.list);
  12690. list_for_each_entry_safe(d_buf, dnext,
  12691. &dmabuf->dbuf.list, list) {
  12692. list_del_init(&d_buf->list);
  12693. lpfc_in_buf_free(vport->phba, d_buf);
  12694. }
  12695. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12696. }
  12697. }
  12698. /**
  12699. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12700. * @vport: The vport that the received sequences were sent to.
  12701. *
  12702. * This function determines whether any received sequences have timed out by
  12703. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12704. * indicates that there is at least one timed out sequence this routine will
  12705. * go through the received sequences one at a time from most inactive to most
  12706. * active to determine which ones need to be cleaned up. Once it has determined
  12707. * that a sequence needs to be cleaned up it will simply free up the resources
  12708. * without sending an abort.
  12709. **/
  12710. void
  12711. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12712. {
  12713. struct lpfc_dmabuf *h_buf, *hnext;
  12714. struct lpfc_dmabuf *d_buf, *dnext;
  12715. struct hbq_dmabuf *dmabuf = NULL;
  12716. unsigned long timeout;
  12717. int abort_count = 0;
  12718. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12719. vport->rcv_buffer_time_stamp);
  12720. if (list_empty(&vport->rcv_buffer_list) ||
  12721. time_before(jiffies, timeout))
  12722. return;
  12723. /* start with the oldest sequence on the rcv list */
  12724. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12725. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12726. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12727. dmabuf->time_stamp);
  12728. if (time_before(jiffies, timeout))
  12729. break;
  12730. abort_count++;
  12731. list_del_init(&dmabuf->hbuf.list);
  12732. list_for_each_entry_safe(d_buf, dnext,
  12733. &dmabuf->dbuf.list, list) {
  12734. list_del_init(&d_buf->list);
  12735. lpfc_in_buf_free(vport->phba, d_buf);
  12736. }
  12737. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12738. }
  12739. if (abort_count)
  12740. lpfc_update_rcv_time_stamp(vport);
  12741. }
  12742. /**
  12743. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12744. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12745. *
  12746. * This function searches through the existing incomplete sequences that have
  12747. * been sent to this @vport. If the frame matches one of the incomplete
  12748. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12749. * make up that sequence. If no sequence is found that matches this frame then
  12750. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12751. * This function returns a pointer to the first dmabuf in the sequence list that
  12752. * the frame was linked to.
  12753. **/
  12754. static struct hbq_dmabuf *
  12755. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12756. {
  12757. struct fc_frame_header *new_hdr;
  12758. struct fc_frame_header *temp_hdr;
  12759. struct lpfc_dmabuf *d_buf;
  12760. struct lpfc_dmabuf *h_buf;
  12761. struct hbq_dmabuf *seq_dmabuf = NULL;
  12762. struct hbq_dmabuf *temp_dmabuf = NULL;
  12763. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12764. dmabuf->time_stamp = jiffies;
  12765. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12766. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12767. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12768. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12769. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12770. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12771. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12772. continue;
  12773. /* found a pending sequence that matches this frame */
  12774. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12775. break;
  12776. }
  12777. if (!seq_dmabuf) {
  12778. /*
  12779. * This indicates first frame received for this sequence.
  12780. * Queue the buffer on the vport's rcv_buffer_list.
  12781. */
  12782. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12783. lpfc_update_rcv_time_stamp(vport);
  12784. return dmabuf;
  12785. }
  12786. temp_hdr = seq_dmabuf->hbuf.virt;
  12787. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12788. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12789. list_del_init(&seq_dmabuf->hbuf.list);
  12790. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12791. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12792. lpfc_update_rcv_time_stamp(vport);
  12793. return dmabuf;
  12794. }
  12795. /* move this sequence to the tail to indicate a young sequence */
  12796. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12797. seq_dmabuf->time_stamp = jiffies;
  12798. lpfc_update_rcv_time_stamp(vport);
  12799. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12800. temp_hdr = dmabuf->hbuf.virt;
  12801. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12802. return seq_dmabuf;
  12803. }
  12804. /* find the correct place in the sequence to insert this frame */
  12805. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12806. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12807. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12808. /*
  12809. * If the frame's sequence count is greater than the frame on
  12810. * the list then insert the frame right after this frame
  12811. */
  12812. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  12813. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12814. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  12815. return seq_dmabuf;
  12816. }
  12817. }
  12818. return NULL;
  12819. }
  12820. /**
  12821. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  12822. * @vport: pointer to a vitural port
  12823. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12824. *
  12825. * This function tries to abort from the partially assembed sequence, described
  12826. * by the information from basic abbort @dmabuf. It checks to see whether such
  12827. * partially assembled sequence held by the driver. If so, it shall free up all
  12828. * the frames from the partially assembled sequence.
  12829. *
  12830. * Return
  12831. * true -- if there is matching partially assembled sequence present and all
  12832. * the frames freed with the sequence;
  12833. * false -- if there is no matching partially assembled sequence present so
  12834. * nothing got aborted in the lower layer driver
  12835. **/
  12836. static bool
  12837. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  12838. struct hbq_dmabuf *dmabuf)
  12839. {
  12840. struct fc_frame_header *new_hdr;
  12841. struct fc_frame_header *temp_hdr;
  12842. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  12843. struct hbq_dmabuf *seq_dmabuf = NULL;
  12844. /* Use the hdr_buf to find the sequence that matches this frame */
  12845. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12846. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  12847. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12848. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12849. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12850. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12851. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12852. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12853. continue;
  12854. /* found a pending sequence that matches this frame */
  12855. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12856. break;
  12857. }
  12858. /* Free up all the frames from the partially assembled sequence */
  12859. if (seq_dmabuf) {
  12860. list_for_each_entry_safe(d_buf, n_buf,
  12861. &seq_dmabuf->dbuf.list, list) {
  12862. list_del_init(&d_buf->list);
  12863. lpfc_in_buf_free(vport->phba, d_buf);
  12864. }
  12865. return true;
  12866. }
  12867. return false;
  12868. }
  12869. /**
  12870. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  12871. * @phba: Pointer to HBA context object.
  12872. * @cmd_iocbq: pointer to the command iocbq structure.
  12873. * @rsp_iocbq: pointer to the response iocbq structure.
  12874. *
  12875. * This function handles the sequence abort response iocb command complete
  12876. * event. It properly releases the memory allocated to the sequence abort
  12877. * accept iocb.
  12878. **/
  12879. static void
  12880. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  12881. struct lpfc_iocbq *cmd_iocbq,
  12882. struct lpfc_iocbq *rsp_iocbq)
  12883. {
  12884. if (cmd_iocbq)
  12885. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  12886. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  12887. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  12888. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12889. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  12890. rsp_iocbq->iocb.ulpStatus,
  12891. rsp_iocbq->iocb.un.ulpWord[4]);
  12892. }
  12893. /**
  12894. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  12895. * @phba: Pointer to HBA context object.
  12896. * @xri: xri id in transaction.
  12897. *
  12898. * This function validates the xri maps to the known range of XRIs allocated an
  12899. * used by the driver.
  12900. **/
  12901. uint16_t
  12902. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  12903. uint16_t xri)
  12904. {
  12905. int i;
  12906. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  12907. if (xri == phba->sli4_hba.xri_ids[i])
  12908. return i;
  12909. }
  12910. return NO_XRI;
  12911. }
  12912. /**
  12913. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  12914. * @phba: Pointer to HBA context object.
  12915. * @fc_hdr: pointer to a FC frame header.
  12916. *
  12917. * This function sends a basic response to a previous unsol sequence abort
  12918. * event after aborting the sequence handling.
  12919. **/
  12920. static void
  12921. lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
  12922. struct fc_frame_header *fc_hdr)
  12923. {
  12924. struct lpfc_iocbq *ctiocb = NULL;
  12925. struct lpfc_nodelist *ndlp;
  12926. uint16_t oxid, rxid, xri, lxri;
  12927. uint32_t sid, fctl;
  12928. IOCB_t *icmd;
  12929. int rc;
  12930. if (!lpfc_is_link_up(phba))
  12931. return;
  12932. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12933. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  12934. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  12935. ndlp = lpfc_findnode_did(phba->pport, sid);
  12936. if (!ndlp) {
  12937. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12938. "1268 Find ndlp returned NULL for oxid:x%x "
  12939. "SID:x%x\n", oxid, sid);
  12940. return;
  12941. }
  12942. /* Allocate buffer for rsp iocb */
  12943. ctiocb = lpfc_sli_get_iocbq(phba);
  12944. if (!ctiocb)
  12945. return;
  12946. /* Extract the F_CTL field from FC_HDR */
  12947. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  12948. icmd = &ctiocb->iocb;
  12949. icmd->un.xseq64.bdl.bdeSize = 0;
  12950. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  12951. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  12952. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  12953. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  12954. /* Fill in the rest of iocb fields */
  12955. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  12956. icmd->ulpBdeCount = 0;
  12957. icmd->ulpLe = 1;
  12958. icmd->ulpClass = CLASS3;
  12959. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  12960. ctiocb->context1 = ndlp;
  12961. ctiocb->iocb_cmpl = NULL;
  12962. ctiocb->vport = phba->pport;
  12963. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  12964. ctiocb->sli4_lxritag = NO_XRI;
  12965. ctiocb->sli4_xritag = NO_XRI;
  12966. if (fctl & FC_FC_EX_CTX)
  12967. /* Exchange responder sent the abort so we
  12968. * own the oxid.
  12969. */
  12970. xri = oxid;
  12971. else
  12972. xri = rxid;
  12973. lxri = lpfc_sli4_xri_inrange(phba, xri);
  12974. if (lxri != NO_XRI)
  12975. lpfc_set_rrq_active(phba, ndlp, lxri,
  12976. (xri == oxid) ? rxid : oxid, 0);
  12977. /* If the oxid maps to the FCP XRI range or if it is out of range,
  12978. * send a BLS_RJT. The driver no longer has that exchange.
  12979. * Override the IOCB for a BA_RJT.
  12980. */
  12981. if (xri > (phba->sli4_hba.max_cfg_param.max_xri +
  12982. phba->sli4_hba.max_cfg_param.xri_base) ||
  12983. xri > (lpfc_sli4_get_els_iocb_cnt(phba) +
  12984. phba->sli4_hba.max_cfg_param.xri_base)) {
  12985. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  12986. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  12987. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  12988. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  12989. }
  12990. if (fctl & FC_FC_EX_CTX) {
  12991. /* ABTS sent by responder to CT exchange, construction
  12992. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  12993. * field and RX_ID from ABTS for RX_ID field.
  12994. */
  12995. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  12996. } else {
  12997. /* ABTS sent by initiator to CT exchange, construction
  12998. * of BA_ACC will need to allocate a new XRI as for the
  12999. * XRI_TAG field.
  13000. */
  13001. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  13002. }
  13003. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  13004. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  13005. /* Xmit CT abts response on exchange <xid> */
  13006. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  13007. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  13008. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  13009. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  13010. if (rc == IOCB_ERROR) {
  13011. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  13012. "2925 Failed to issue CT ABTS RSP x%x on "
  13013. "xri x%x, Data x%x\n",
  13014. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  13015. phba->link_state);
  13016. lpfc_sli_release_iocbq(phba, ctiocb);
  13017. }
  13018. }
  13019. /**
  13020. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  13021. * @vport: Pointer to the vport on which this sequence was received
  13022. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13023. *
  13024. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  13025. * receive sequence is only partially assembed by the driver, it shall abort
  13026. * the partially assembled frames for the sequence. Otherwise, if the
  13027. * unsolicited receive sequence has been completely assembled and passed to
  13028. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  13029. * unsolicited sequence has been aborted. After that, it will issue a basic
  13030. * accept to accept the abort.
  13031. **/
  13032. void
  13033. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  13034. struct hbq_dmabuf *dmabuf)
  13035. {
  13036. struct lpfc_hba *phba = vport->phba;
  13037. struct fc_frame_header fc_hdr;
  13038. uint32_t fctl;
  13039. bool abts_par;
  13040. /* Make a copy of fc_hdr before the dmabuf being released */
  13041. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  13042. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  13043. if (fctl & FC_FC_EX_CTX) {
  13044. /*
  13045. * ABTS sent by responder to exchange, just free the buffer
  13046. */
  13047. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13048. } else {
  13049. /*
  13050. * ABTS sent by initiator to exchange, need to do cleanup
  13051. */
  13052. /* Try to abort partially assembled seq */
  13053. abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  13054. /* Send abort to ULP if partially seq abort failed */
  13055. if (abts_par == false)
  13056. lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
  13057. else
  13058. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13059. }
  13060. /* Send basic accept (BA_ACC) to the abort requester */
  13061. lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
  13062. }
  13063. /**
  13064. * lpfc_seq_complete - Indicates if a sequence is complete
  13065. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13066. *
  13067. * This function checks the sequence, starting with the frame described by
  13068. * @dmabuf, to see if all the frames associated with this sequence are present.
  13069. * the frames associated with this sequence are linked to the @dmabuf using the
  13070. * dbuf list. This function looks for two major things. 1) That the first frame
  13071. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  13072. * set. 3) That there are no holes in the sequence count. The function will
  13073. * return 1 when the sequence is complete, otherwise it will return 0.
  13074. **/
  13075. static int
  13076. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  13077. {
  13078. struct fc_frame_header *hdr;
  13079. struct lpfc_dmabuf *d_buf;
  13080. struct hbq_dmabuf *seq_dmabuf;
  13081. uint32_t fctl;
  13082. int seq_count = 0;
  13083. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13084. /* make sure first fame of sequence has a sequence count of zero */
  13085. if (hdr->fh_seq_cnt != seq_count)
  13086. return 0;
  13087. fctl = (hdr->fh_f_ctl[0] << 16 |
  13088. hdr->fh_f_ctl[1] << 8 |
  13089. hdr->fh_f_ctl[2]);
  13090. /* If last frame of sequence we can return success. */
  13091. if (fctl & FC_FC_END_SEQ)
  13092. return 1;
  13093. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  13094. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13095. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13096. /* If there is a hole in the sequence count then fail. */
  13097. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  13098. return 0;
  13099. fctl = (hdr->fh_f_ctl[0] << 16 |
  13100. hdr->fh_f_ctl[1] << 8 |
  13101. hdr->fh_f_ctl[2]);
  13102. /* If last frame of sequence we can return success. */
  13103. if (fctl & FC_FC_END_SEQ)
  13104. return 1;
  13105. }
  13106. return 0;
  13107. }
  13108. /**
  13109. * lpfc_prep_seq - Prep sequence for ULP processing
  13110. * @vport: Pointer to the vport on which this sequence was received
  13111. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13112. *
  13113. * This function takes a sequence, described by a list of frames, and creates
  13114. * a list of iocbq structures to describe the sequence. This iocbq list will be
  13115. * used to issue to the generic unsolicited sequence handler. This routine
  13116. * returns a pointer to the first iocbq in the list. If the function is unable
  13117. * to allocate an iocbq then it throw out the received frames that were not
  13118. * able to be described and return a pointer to the first iocbq. If unable to
  13119. * allocate any iocbqs (including the first) this function will return NULL.
  13120. **/
  13121. static struct lpfc_iocbq *
  13122. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  13123. {
  13124. struct hbq_dmabuf *hbq_buf;
  13125. struct lpfc_dmabuf *d_buf, *n_buf;
  13126. struct lpfc_iocbq *first_iocbq, *iocbq;
  13127. struct fc_frame_header *fc_hdr;
  13128. uint32_t sid;
  13129. uint32_t len, tot_len;
  13130. struct ulp_bde64 *pbde;
  13131. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13132. /* remove from receive buffer list */
  13133. list_del_init(&seq_dmabuf->hbuf.list);
  13134. lpfc_update_rcv_time_stamp(vport);
  13135. /* get the Remote Port's SID */
  13136. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13137. tot_len = 0;
  13138. /* Get an iocbq struct to fill in. */
  13139. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  13140. if (first_iocbq) {
  13141. /* Initialize the first IOCB. */
  13142. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  13143. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  13144. /* Check FC Header to see what TYPE of frame we are rcv'ing */
  13145. if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
  13146. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
  13147. first_iocbq->iocb.un.rcvels.parmRo =
  13148. sli4_did_from_fc_hdr(fc_hdr);
  13149. first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
  13150. } else
  13151. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  13152. first_iocbq->iocb.ulpContext = NO_XRI;
  13153. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  13154. be16_to_cpu(fc_hdr->fh_ox_id);
  13155. /* iocbq is prepped for internal consumption. Physical vpi. */
  13156. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  13157. vport->phba->vpi_ids[vport->vpi];
  13158. /* put the first buffer into the first IOCBq */
  13159. first_iocbq->context2 = &seq_dmabuf->dbuf;
  13160. first_iocbq->context3 = NULL;
  13161. first_iocbq->iocb.ulpBdeCount = 1;
  13162. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13163. LPFC_DATA_BUF_SIZE;
  13164. first_iocbq->iocb.un.rcvels.remoteID = sid;
  13165. tot_len = bf_get(lpfc_rcqe_length,
  13166. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  13167. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13168. }
  13169. iocbq = first_iocbq;
  13170. /*
  13171. * Each IOCBq can have two Buffers assigned, so go through the list
  13172. * of buffers for this sequence and save two buffers in each IOCBq
  13173. */
  13174. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13175. if (!iocbq) {
  13176. lpfc_in_buf_free(vport->phba, d_buf);
  13177. continue;
  13178. }
  13179. if (!iocbq->context3) {
  13180. iocbq->context3 = d_buf;
  13181. iocbq->iocb.ulpBdeCount++;
  13182. pbde = (struct ulp_bde64 *)
  13183. &iocbq->iocb.unsli3.sli3Words[4];
  13184. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13185. /* We need to get the size out of the right CQE */
  13186. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13187. len = bf_get(lpfc_rcqe_length,
  13188. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13189. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13190. tot_len += len;
  13191. } else {
  13192. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13193. if (!iocbq) {
  13194. if (first_iocbq) {
  13195. first_iocbq->iocb.ulpStatus =
  13196. IOSTAT_FCP_RSP_ERROR;
  13197. first_iocbq->iocb.un.ulpWord[4] =
  13198. IOERR_NO_RESOURCES;
  13199. }
  13200. lpfc_in_buf_free(vport->phba, d_buf);
  13201. continue;
  13202. }
  13203. iocbq->context2 = d_buf;
  13204. iocbq->context3 = NULL;
  13205. iocbq->iocb.ulpBdeCount = 1;
  13206. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13207. LPFC_DATA_BUF_SIZE;
  13208. /* We need to get the size out of the right CQE */
  13209. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13210. len = bf_get(lpfc_rcqe_length,
  13211. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13212. tot_len += len;
  13213. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13214. iocbq->iocb.un.rcvels.remoteID = sid;
  13215. list_add_tail(&iocbq->list, &first_iocbq->list);
  13216. }
  13217. }
  13218. return first_iocbq;
  13219. }
  13220. static void
  13221. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13222. struct hbq_dmabuf *seq_dmabuf)
  13223. {
  13224. struct fc_frame_header *fc_hdr;
  13225. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13226. struct lpfc_hba *phba = vport->phba;
  13227. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13228. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13229. if (!iocbq) {
  13230. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13231. "2707 Ring %d handler: Failed to allocate "
  13232. "iocb Rctl x%x Type x%x received\n",
  13233. LPFC_ELS_RING,
  13234. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13235. return;
  13236. }
  13237. if (!lpfc_complete_unsol_iocb(phba,
  13238. &phba->sli.ring[LPFC_ELS_RING],
  13239. iocbq, fc_hdr->fh_r_ctl,
  13240. fc_hdr->fh_type))
  13241. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13242. "2540 Ring %d handler: unexpected Rctl "
  13243. "x%x Type x%x received\n",
  13244. LPFC_ELS_RING,
  13245. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13246. /* Free iocb created in lpfc_prep_seq */
  13247. list_for_each_entry_safe(curr_iocb, next_iocb,
  13248. &iocbq->list, list) {
  13249. list_del_init(&curr_iocb->list);
  13250. lpfc_sli_release_iocbq(phba, curr_iocb);
  13251. }
  13252. lpfc_sli_release_iocbq(phba, iocbq);
  13253. }
  13254. /**
  13255. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13256. * @phba: Pointer to HBA context object.
  13257. *
  13258. * This function is called with no lock held. This function processes all
  13259. * the received buffers and gives it to upper layers when a received buffer
  13260. * indicates that it is the final frame in the sequence. The interrupt
  13261. * service routine processes received buffers at interrupt contexts and adds
  13262. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13263. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13264. * appropriate receive function when the final frame in a sequence is received.
  13265. **/
  13266. void
  13267. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13268. struct hbq_dmabuf *dmabuf)
  13269. {
  13270. struct hbq_dmabuf *seq_dmabuf;
  13271. struct fc_frame_header *fc_hdr;
  13272. struct lpfc_vport *vport;
  13273. uint32_t fcfi;
  13274. uint32_t did;
  13275. /* Process each received buffer */
  13276. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13277. /* check to see if this a valid type of frame */
  13278. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13279. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13280. return;
  13281. }
  13282. if ((bf_get(lpfc_cqe_code,
  13283. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13284. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13285. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13286. else
  13287. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13288. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13289. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13290. if (!vport) {
  13291. /* throw out the frame */
  13292. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13293. return;
  13294. }
  13295. /* d_id this frame is directed to */
  13296. did = sli4_did_from_fc_hdr(fc_hdr);
  13297. /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
  13298. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
  13299. (did != Fabric_DID)) {
  13300. /*
  13301. * Throw out the frame if we are not pt2pt.
  13302. * The pt2pt protocol allows for discovery frames
  13303. * to be received without a registered VPI.
  13304. */
  13305. if (!(vport->fc_flag & FC_PT2PT) ||
  13306. (phba->link_state == LPFC_HBA_READY)) {
  13307. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13308. return;
  13309. }
  13310. }
  13311. /* Handle the basic abort sequence (BA_ABTS) event */
  13312. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13313. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13314. return;
  13315. }
  13316. /* Link this frame */
  13317. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13318. if (!seq_dmabuf) {
  13319. /* unable to add frame to vport - throw it out */
  13320. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13321. return;
  13322. }
  13323. /* If not last frame in sequence continue processing frames. */
  13324. if (!lpfc_seq_complete(seq_dmabuf))
  13325. return;
  13326. /* Send the complete sequence to the upper layer protocol */
  13327. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13328. }
  13329. /**
  13330. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13331. * @phba: pointer to lpfc hba data structure.
  13332. *
  13333. * This routine is invoked to post rpi header templates to the
  13334. * HBA consistent with the SLI-4 interface spec. This routine
  13335. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13336. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13337. *
  13338. * This routine does not require any locks. It's usage is expected
  13339. * to be driver load or reset recovery when the driver is
  13340. * sequential.
  13341. *
  13342. * Return codes
  13343. * 0 - successful
  13344. * -EIO - The mailbox failed to complete successfully.
  13345. * When this error occurs, the driver is not guaranteed
  13346. * to have any rpi regions posted to the device and
  13347. * must either attempt to repost the regions or take a
  13348. * fatal error.
  13349. **/
  13350. int
  13351. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13352. {
  13353. struct lpfc_rpi_hdr *rpi_page;
  13354. uint32_t rc = 0;
  13355. uint16_t lrpi = 0;
  13356. /* SLI4 ports that support extents do not require RPI headers. */
  13357. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13358. goto exit;
  13359. if (phba->sli4_hba.extents_in_use)
  13360. return -EIO;
  13361. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13362. /*
  13363. * Assign the rpi headers a physical rpi only if the driver
  13364. * has not initialized those resources. A port reset only
  13365. * needs the headers posted.
  13366. */
  13367. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13368. LPFC_RPI_RSRC_RDY)
  13369. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13370. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13371. if (rc != MBX_SUCCESS) {
  13372. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13373. "2008 Error %d posting all rpi "
  13374. "headers\n", rc);
  13375. rc = -EIO;
  13376. break;
  13377. }
  13378. }
  13379. exit:
  13380. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13381. LPFC_RPI_RSRC_RDY);
  13382. return rc;
  13383. }
  13384. /**
  13385. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13386. * @phba: pointer to lpfc hba data structure.
  13387. * @rpi_page: pointer to the rpi memory region.
  13388. *
  13389. * This routine is invoked to post a single rpi header to the
  13390. * HBA consistent with the SLI-4 interface spec. This memory region
  13391. * maps up to 64 rpi context regions.
  13392. *
  13393. * Return codes
  13394. * 0 - successful
  13395. * -ENOMEM - No available memory
  13396. * -EIO - The mailbox failed to complete successfully.
  13397. **/
  13398. int
  13399. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13400. {
  13401. LPFC_MBOXQ_t *mboxq;
  13402. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13403. uint32_t rc = 0;
  13404. uint32_t shdr_status, shdr_add_status;
  13405. union lpfc_sli4_cfg_shdr *shdr;
  13406. /* SLI4 ports that support extents do not require RPI headers. */
  13407. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13408. return rc;
  13409. if (phba->sli4_hba.extents_in_use)
  13410. return -EIO;
  13411. /* The port is notified of the header region via a mailbox command. */
  13412. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13413. if (!mboxq) {
  13414. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13415. "2001 Unable to allocate memory for issuing "
  13416. "SLI_CONFIG_SPECIAL mailbox command\n");
  13417. return -ENOMEM;
  13418. }
  13419. /* Post all rpi memory regions to the port. */
  13420. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13421. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13422. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13423. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13424. sizeof(struct lpfc_sli4_cfg_mhdr),
  13425. LPFC_SLI4_MBX_EMBED);
  13426. /* Post the physical rpi to the port for this rpi header. */
  13427. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13428. rpi_page->start_rpi);
  13429. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13430. hdr_tmpl, rpi_page->page_count);
  13431. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13432. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13433. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13434. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13435. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13436. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13437. if (rc != MBX_TIMEOUT)
  13438. mempool_free(mboxq, phba->mbox_mem_pool);
  13439. if (shdr_status || shdr_add_status || rc) {
  13440. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13441. "2514 POST_RPI_HDR mailbox failed with "
  13442. "status x%x add_status x%x, mbx status x%x\n",
  13443. shdr_status, shdr_add_status, rc);
  13444. rc = -ENXIO;
  13445. }
  13446. return rc;
  13447. }
  13448. /**
  13449. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13450. * @phba: pointer to lpfc hba data structure.
  13451. *
  13452. * This routine is invoked to post rpi header templates to the
  13453. * HBA consistent with the SLI-4 interface spec. This routine
  13454. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13455. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13456. *
  13457. * Returns
  13458. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13459. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13460. **/
  13461. int
  13462. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13463. {
  13464. unsigned long rpi;
  13465. uint16_t max_rpi, rpi_limit;
  13466. uint16_t rpi_remaining, lrpi = 0;
  13467. struct lpfc_rpi_hdr *rpi_hdr;
  13468. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13469. rpi_limit = phba->sli4_hba.next_rpi;
  13470. /*
  13471. * Fetch the next logical rpi. Because this index is logical,
  13472. * the driver starts at 0 each time.
  13473. */
  13474. spin_lock_irq(&phba->hbalock);
  13475. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13476. if (rpi >= rpi_limit)
  13477. rpi = LPFC_RPI_ALLOC_ERROR;
  13478. else {
  13479. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13480. phba->sli4_hba.max_cfg_param.rpi_used++;
  13481. phba->sli4_hba.rpi_count++;
  13482. }
  13483. /*
  13484. * Don't try to allocate more rpi header regions if the device limit
  13485. * has been exhausted.
  13486. */
  13487. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13488. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13489. spin_unlock_irq(&phba->hbalock);
  13490. return rpi;
  13491. }
  13492. /*
  13493. * RPI header postings are not required for SLI4 ports capable of
  13494. * extents.
  13495. */
  13496. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13497. spin_unlock_irq(&phba->hbalock);
  13498. return rpi;
  13499. }
  13500. /*
  13501. * If the driver is running low on rpi resources, allocate another
  13502. * page now. Note that the next_rpi value is used because
  13503. * it represents how many are actually in use whereas max_rpi notes
  13504. * how many are supported max by the device.
  13505. */
  13506. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13507. spin_unlock_irq(&phba->hbalock);
  13508. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13509. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13510. if (!rpi_hdr) {
  13511. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13512. "2002 Error Could not grow rpi "
  13513. "count\n");
  13514. } else {
  13515. lrpi = rpi_hdr->start_rpi;
  13516. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13517. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13518. }
  13519. }
  13520. return rpi;
  13521. }
  13522. /**
  13523. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13524. * @phba: pointer to lpfc hba data structure.
  13525. *
  13526. * This routine is invoked to release an rpi to the pool of
  13527. * available rpis maintained by the driver.
  13528. **/
  13529. void
  13530. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13531. {
  13532. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13533. phba->sli4_hba.rpi_count--;
  13534. phba->sli4_hba.max_cfg_param.rpi_used--;
  13535. }
  13536. }
  13537. /**
  13538. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13539. * @phba: pointer to lpfc hba data structure.
  13540. *
  13541. * This routine is invoked to release an rpi to the pool of
  13542. * available rpis maintained by the driver.
  13543. **/
  13544. void
  13545. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13546. {
  13547. spin_lock_irq(&phba->hbalock);
  13548. __lpfc_sli4_free_rpi(phba, rpi);
  13549. spin_unlock_irq(&phba->hbalock);
  13550. }
  13551. /**
  13552. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13553. * @phba: pointer to lpfc hba data structure.
  13554. *
  13555. * This routine is invoked to remove the memory region that
  13556. * provided rpi via a bitmask.
  13557. **/
  13558. void
  13559. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13560. {
  13561. kfree(phba->sli4_hba.rpi_bmask);
  13562. kfree(phba->sli4_hba.rpi_ids);
  13563. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13564. }
  13565. /**
  13566. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  13567. * @phba: pointer to lpfc hba data structure.
  13568. *
  13569. * This routine is invoked to remove the memory region that
  13570. * provided rpi via a bitmask.
  13571. **/
  13572. int
  13573. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  13574. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  13575. {
  13576. LPFC_MBOXQ_t *mboxq;
  13577. struct lpfc_hba *phba = ndlp->phba;
  13578. int rc;
  13579. /* The port is notified of the header region via a mailbox command. */
  13580. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13581. if (!mboxq)
  13582. return -ENOMEM;
  13583. /* Post all rpi memory regions to the port. */
  13584. lpfc_resume_rpi(mboxq, ndlp);
  13585. if (cmpl) {
  13586. mboxq->mbox_cmpl = cmpl;
  13587. mboxq->context1 = arg;
  13588. mboxq->context2 = ndlp;
  13589. } else
  13590. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13591. mboxq->vport = ndlp->vport;
  13592. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13593. if (rc == MBX_NOT_FINISHED) {
  13594. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13595. "2010 Resume RPI Mailbox failed "
  13596. "status %d, mbxStatus x%x\n", rc,
  13597. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13598. mempool_free(mboxq, phba->mbox_mem_pool);
  13599. return -EIO;
  13600. }
  13601. return 0;
  13602. }
  13603. /**
  13604. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13605. * @vport: Pointer to the vport for which the vpi is being initialized
  13606. *
  13607. * This routine is invoked to activate a vpi with the port.
  13608. *
  13609. * Returns:
  13610. * 0 success
  13611. * -Evalue otherwise
  13612. **/
  13613. int
  13614. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13615. {
  13616. LPFC_MBOXQ_t *mboxq;
  13617. int rc = 0;
  13618. int retval = MBX_SUCCESS;
  13619. uint32_t mbox_tmo;
  13620. struct lpfc_hba *phba = vport->phba;
  13621. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13622. if (!mboxq)
  13623. return -ENOMEM;
  13624. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13625. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13626. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13627. if (rc != MBX_SUCCESS) {
  13628. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13629. "2022 INIT VPI Mailbox failed "
  13630. "status %d, mbxStatus x%x\n", rc,
  13631. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13632. retval = -EIO;
  13633. }
  13634. if (rc != MBX_TIMEOUT)
  13635. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13636. return retval;
  13637. }
  13638. /**
  13639. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13640. * @phba: pointer to lpfc hba data structure.
  13641. * @mboxq: Pointer to mailbox object.
  13642. *
  13643. * This routine is invoked to manually add a single FCF record. The caller
  13644. * must pass a completely initialized FCF_Record. This routine takes
  13645. * care of the nonembedded mailbox operations.
  13646. **/
  13647. static void
  13648. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13649. {
  13650. void *virt_addr;
  13651. union lpfc_sli4_cfg_shdr *shdr;
  13652. uint32_t shdr_status, shdr_add_status;
  13653. virt_addr = mboxq->sge_array->addr[0];
  13654. /* The IOCTL status is embedded in the mailbox subheader. */
  13655. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13656. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13657. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13658. if ((shdr_status || shdr_add_status) &&
  13659. (shdr_status != STATUS_FCF_IN_USE))
  13660. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13661. "2558 ADD_FCF_RECORD mailbox failed with "
  13662. "status x%x add_status x%x\n",
  13663. shdr_status, shdr_add_status);
  13664. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13665. }
  13666. /**
  13667. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13668. * @phba: pointer to lpfc hba data structure.
  13669. * @fcf_record: pointer to the initialized fcf record to add.
  13670. *
  13671. * This routine is invoked to manually add a single FCF record. The caller
  13672. * must pass a completely initialized FCF_Record. This routine takes
  13673. * care of the nonembedded mailbox operations.
  13674. **/
  13675. int
  13676. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13677. {
  13678. int rc = 0;
  13679. LPFC_MBOXQ_t *mboxq;
  13680. uint8_t *bytep;
  13681. void *virt_addr;
  13682. dma_addr_t phys_addr;
  13683. struct lpfc_mbx_sge sge;
  13684. uint32_t alloc_len, req_len;
  13685. uint32_t fcfindex;
  13686. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13687. if (!mboxq) {
  13688. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13689. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13690. return -ENOMEM;
  13691. }
  13692. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13693. sizeof(uint32_t);
  13694. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13695. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13696. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13697. req_len, LPFC_SLI4_MBX_NEMBED);
  13698. if (alloc_len < req_len) {
  13699. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13700. "2523 Allocated DMA memory size (x%x) is "
  13701. "less than the requested DMA memory "
  13702. "size (x%x)\n", alloc_len, req_len);
  13703. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13704. return -ENOMEM;
  13705. }
  13706. /*
  13707. * Get the first SGE entry from the non-embedded DMA memory. This
  13708. * routine only uses a single SGE.
  13709. */
  13710. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13711. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13712. virt_addr = mboxq->sge_array->addr[0];
  13713. /*
  13714. * Configure the FCF record for FCFI 0. This is the driver's
  13715. * hardcoded default and gets used in nonFIP mode.
  13716. */
  13717. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13718. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13719. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13720. /*
  13721. * Copy the fcf_index and the FCF Record Data. The data starts after
  13722. * the FCoE header plus word10. The data copy needs to be endian
  13723. * correct.
  13724. */
  13725. bytep += sizeof(uint32_t);
  13726. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13727. mboxq->vport = phba->pport;
  13728. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13729. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13730. if (rc == MBX_NOT_FINISHED) {
  13731. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13732. "2515 ADD_FCF_RECORD mailbox failed with "
  13733. "status 0x%x\n", rc);
  13734. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13735. rc = -EIO;
  13736. } else
  13737. rc = 0;
  13738. return rc;
  13739. }
  13740. /**
  13741. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13742. * @phba: pointer to lpfc hba data structure.
  13743. * @fcf_record: pointer to the fcf record to write the default data.
  13744. * @fcf_index: FCF table entry index.
  13745. *
  13746. * This routine is invoked to build the driver's default FCF record. The
  13747. * values used are hardcoded. This routine handles memory initialization.
  13748. *
  13749. **/
  13750. void
  13751. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13752. struct fcf_record *fcf_record,
  13753. uint16_t fcf_index)
  13754. {
  13755. memset(fcf_record, 0, sizeof(struct fcf_record));
  13756. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  13757. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  13758. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  13759. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  13760. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  13761. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  13762. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  13763. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  13764. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  13765. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  13766. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  13767. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  13768. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  13769. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  13770. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  13771. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  13772. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  13773. /* Set the VLAN bit map */
  13774. if (phba->valid_vlan) {
  13775. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  13776. = 1 << (phba->vlan_id % 8);
  13777. }
  13778. }
  13779. /**
  13780. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  13781. * @phba: pointer to lpfc hba data structure.
  13782. * @fcf_index: FCF table entry offset.
  13783. *
  13784. * This routine is invoked to scan the entire FCF table by reading FCF
  13785. * record and processing it one at a time starting from the @fcf_index
  13786. * for initial FCF discovery or fast FCF failover rediscovery.
  13787. *
  13788. * Return 0 if the mailbox command is submitted successfully, none 0
  13789. * otherwise.
  13790. **/
  13791. int
  13792. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13793. {
  13794. int rc = 0, error;
  13795. LPFC_MBOXQ_t *mboxq;
  13796. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  13797. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  13798. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13799. if (!mboxq) {
  13800. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13801. "2000 Failed to allocate mbox for "
  13802. "READ_FCF cmd\n");
  13803. error = -ENOMEM;
  13804. goto fail_fcf_scan;
  13805. }
  13806. /* Construct the read FCF record mailbox command */
  13807. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13808. if (rc) {
  13809. error = -EINVAL;
  13810. goto fail_fcf_scan;
  13811. }
  13812. /* Issue the mailbox command asynchronously */
  13813. mboxq->vport = phba->pport;
  13814. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  13815. spin_lock_irq(&phba->hbalock);
  13816. phba->hba_flag |= FCF_TS_INPROG;
  13817. spin_unlock_irq(&phba->hbalock);
  13818. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13819. if (rc == MBX_NOT_FINISHED)
  13820. error = -EIO;
  13821. else {
  13822. /* Reset eligible FCF count for new scan */
  13823. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  13824. phba->fcf.eligible_fcf_cnt = 0;
  13825. error = 0;
  13826. }
  13827. fail_fcf_scan:
  13828. if (error) {
  13829. if (mboxq)
  13830. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13831. /* FCF scan failed, clear FCF_TS_INPROG flag */
  13832. spin_lock_irq(&phba->hbalock);
  13833. phba->hba_flag &= ~FCF_TS_INPROG;
  13834. spin_unlock_irq(&phba->hbalock);
  13835. }
  13836. return error;
  13837. }
  13838. /**
  13839. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  13840. * @phba: pointer to lpfc hba data structure.
  13841. * @fcf_index: FCF table entry offset.
  13842. *
  13843. * This routine is invoked to read an FCF record indicated by @fcf_index
  13844. * and to use it for FLOGI roundrobin FCF failover.
  13845. *
  13846. * Return 0 if the mailbox command is submitted successfully, none 0
  13847. * otherwise.
  13848. **/
  13849. int
  13850. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13851. {
  13852. int rc = 0, error;
  13853. LPFC_MBOXQ_t *mboxq;
  13854. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13855. if (!mboxq) {
  13856. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13857. "2763 Failed to allocate mbox for "
  13858. "READ_FCF cmd\n");
  13859. error = -ENOMEM;
  13860. goto fail_fcf_read;
  13861. }
  13862. /* Construct the read FCF record mailbox command */
  13863. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13864. if (rc) {
  13865. error = -EINVAL;
  13866. goto fail_fcf_read;
  13867. }
  13868. /* Issue the mailbox command asynchronously */
  13869. mboxq->vport = phba->pport;
  13870. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  13871. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13872. if (rc == MBX_NOT_FINISHED)
  13873. error = -EIO;
  13874. else
  13875. error = 0;
  13876. fail_fcf_read:
  13877. if (error && mboxq)
  13878. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13879. return error;
  13880. }
  13881. /**
  13882. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  13883. * @phba: pointer to lpfc hba data structure.
  13884. * @fcf_index: FCF table entry offset.
  13885. *
  13886. * This routine is invoked to read an FCF record indicated by @fcf_index to
  13887. * determine whether it's eligible for FLOGI roundrobin failover list.
  13888. *
  13889. * Return 0 if the mailbox command is submitted successfully, none 0
  13890. * otherwise.
  13891. **/
  13892. int
  13893. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13894. {
  13895. int rc = 0, error;
  13896. LPFC_MBOXQ_t *mboxq;
  13897. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13898. if (!mboxq) {
  13899. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13900. "2758 Failed to allocate mbox for "
  13901. "READ_FCF cmd\n");
  13902. error = -ENOMEM;
  13903. goto fail_fcf_read;
  13904. }
  13905. /* Construct the read FCF record mailbox command */
  13906. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13907. if (rc) {
  13908. error = -EINVAL;
  13909. goto fail_fcf_read;
  13910. }
  13911. /* Issue the mailbox command asynchronously */
  13912. mboxq->vport = phba->pport;
  13913. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  13914. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13915. if (rc == MBX_NOT_FINISHED)
  13916. error = -EIO;
  13917. else
  13918. error = 0;
  13919. fail_fcf_read:
  13920. if (error && mboxq)
  13921. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13922. return error;
  13923. }
  13924. /**
  13925. * lpfc_check_next_fcf_pri
  13926. * phba pointer to the lpfc_hba struct for this port.
  13927. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  13928. * routine when the rr_bmask is empty. The FCF indecies are put into the
  13929. * rr_bmask based on their priority level. Starting from the highest priority
  13930. * to the lowest. The most likely FCF candidate will be in the highest
  13931. * priority group. When this routine is called it searches the fcf_pri list for
  13932. * next lowest priority group and repopulates the rr_bmask with only those
  13933. * fcf_indexes.
  13934. * returns:
  13935. * 1=success 0=failure
  13936. **/
  13937. int
  13938. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  13939. {
  13940. uint16_t next_fcf_pri;
  13941. uint16_t last_index;
  13942. struct lpfc_fcf_pri *fcf_pri;
  13943. int rc;
  13944. int ret = 0;
  13945. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  13946. LPFC_SLI4_FCF_TBL_INDX_MAX);
  13947. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13948. "3060 Last IDX %d\n", last_index);
  13949. if (list_empty(&phba->fcf.fcf_pri_list)) {
  13950. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13951. "3061 Last IDX %d\n", last_index);
  13952. return 0; /* Empty rr list */
  13953. }
  13954. next_fcf_pri = 0;
  13955. /*
  13956. * Clear the rr_bmask and set all of the bits that are at this
  13957. * priority.
  13958. */
  13959. memset(phba->fcf.fcf_rr_bmask, 0,
  13960. sizeof(*phba->fcf.fcf_rr_bmask));
  13961. spin_lock_irq(&phba->hbalock);
  13962. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13963. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  13964. continue;
  13965. /*
  13966. * the 1st priority that has not FLOGI failed
  13967. * will be the highest.
  13968. */
  13969. if (!next_fcf_pri)
  13970. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13971. spin_unlock_irq(&phba->hbalock);
  13972. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13973. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13974. fcf_pri->fcf_rec.fcf_index);
  13975. if (rc)
  13976. return 0;
  13977. }
  13978. spin_lock_irq(&phba->hbalock);
  13979. }
  13980. /*
  13981. * if next_fcf_pri was not set above and the list is not empty then
  13982. * we have failed flogis on all of them. So reset flogi failed
  13983. * and start at the begining.
  13984. */
  13985. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  13986. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13987. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  13988. /*
  13989. * the 1st priority that has not FLOGI failed
  13990. * will be the highest.
  13991. */
  13992. if (!next_fcf_pri)
  13993. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13994. spin_unlock_irq(&phba->hbalock);
  13995. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13996. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13997. fcf_pri->fcf_rec.fcf_index);
  13998. if (rc)
  13999. return 0;
  14000. }
  14001. spin_lock_irq(&phba->hbalock);
  14002. }
  14003. } else
  14004. ret = 1;
  14005. spin_unlock_irq(&phba->hbalock);
  14006. return ret;
  14007. }
  14008. /**
  14009. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  14010. * @phba: pointer to lpfc hba data structure.
  14011. *
  14012. * This routine is to get the next eligible FCF record index in a round
  14013. * robin fashion. If the next eligible FCF record index equals to the
  14014. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  14015. * shall be returned, otherwise, the next eligible FCF record's index
  14016. * shall be returned.
  14017. **/
  14018. uint16_t
  14019. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  14020. {
  14021. uint16_t next_fcf_index;
  14022. /* Search start from next bit of currently registered FCF index */
  14023. next_priority:
  14024. next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
  14025. LPFC_SLI4_FCF_TBL_INDX_MAX;
  14026. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14027. LPFC_SLI4_FCF_TBL_INDX_MAX,
  14028. next_fcf_index);
  14029. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  14030. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14031. /*
  14032. * If we have wrapped then we need to clear the bits that
  14033. * have been tested so that we can detect when we should
  14034. * change the priority level.
  14035. */
  14036. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14037. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  14038. }
  14039. /* Check roundrobin failover list empty condition */
  14040. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  14041. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  14042. /*
  14043. * If next fcf index is not found check if there are lower
  14044. * Priority level fcf's in the fcf_priority list.
  14045. * Set up the rr_bmask with all of the avaiable fcf bits
  14046. * at that level and continue the selection process.
  14047. */
  14048. if (lpfc_check_next_fcf_pri_level(phba))
  14049. goto next_priority;
  14050. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14051. "2844 No roundrobin failover FCF available\n");
  14052. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  14053. return LPFC_FCOE_FCF_NEXT_NONE;
  14054. else {
  14055. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14056. "3063 Only FCF available idx %d, flag %x\n",
  14057. next_fcf_index,
  14058. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  14059. return next_fcf_index;
  14060. }
  14061. }
  14062. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  14063. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  14064. LPFC_FCF_FLOGI_FAILED)
  14065. goto next_priority;
  14066. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14067. "2845 Get next roundrobin failover FCF (x%x)\n",
  14068. next_fcf_index);
  14069. return next_fcf_index;
  14070. }
  14071. /**
  14072. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  14073. * @phba: pointer to lpfc hba data structure.
  14074. *
  14075. * This routine sets the FCF record index in to the eligible bmask for
  14076. * roundrobin failover search. It checks to make sure that the index
  14077. * does not go beyond the range of the driver allocated bmask dimension
  14078. * before setting the bit.
  14079. *
  14080. * Returns 0 if the index bit successfully set, otherwise, it returns
  14081. * -EINVAL.
  14082. **/
  14083. int
  14084. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  14085. {
  14086. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14087. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14088. "2610 FCF (x%x) reached driver's book "
  14089. "keeping dimension:x%x\n",
  14090. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14091. return -EINVAL;
  14092. }
  14093. /* Set the eligible FCF record index bmask */
  14094. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14095. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14096. "2790 Set FCF (x%x) to roundrobin FCF failover "
  14097. "bmask\n", fcf_index);
  14098. return 0;
  14099. }
  14100. /**
  14101. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  14102. * @phba: pointer to lpfc hba data structure.
  14103. *
  14104. * This routine clears the FCF record index from the eligible bmask for
  14105. * roundrobin failover search. It checks to make sure that the index
  14106. * does not go beyond the range of the driver allocated bmask dimension
  14107. * before clearing the bit.
  14108. **/
  14109. void
  14110. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  14111. {
  14112. struct lpfc_fcf_pri *fcf_pri;
  14113. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14114. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14115. "2762 FCF (x%x) reached driver's book "
  14116. "keeping dimension:x%x\n",
  14117. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14118. return;
  14119. }
  14120. /* Clear the eligible FCF record index bmask */
  14121. spin_lock_irq(&phba->hbalock);
  14122. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14123. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  14124. list_del_init(&fcf_pri->list);
  14125. break;
  14126. }
  14127. }
  14128. spin_unlock_irq(&phba->hbalock);
  14129. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14130. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14131. "2791 Clear FCF (x%x) from roundrobin failover "
  14132. "bmask\n", fcf_index);
  14133. }
  14134. /**
  14135. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  14136. * @phba: pointer to lpfc hba data structure.
  14137. *
  14138. * This routine is the completion routine for the rediscover FCF table mailbox
  14139. * command. If the mailbox command returned failure, it will try to stop the
  14140. * FCF rediscover wait timer.
  14141. **/
  14142. void
  14143. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  14144. {
  14145. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14146. uint32_t shdr_status, shdr_add_status;
  14147. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14148. shdr_status = bf_get(lpfc_mbox_hdr_status,
  14149. &redisc_fcf->header.cfg_shdr.response);
  14150. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  14151. &redisc_fcf->header.cfg_shdr.response);
  14152. if (shdr_status || shdr_add_status) {
  14153. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14154. "2746 Requesting for FCF rediscovery failed "
  14155. "status x%x add_status x%x\n",
  14156. shdr_status, shdr_add_status);
  14157. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  14158. spin_lock_irq(&phba->hbalock);
  14159. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  14160. spin_unlock_irq(&phba->hbalock);
  14161. /*
  14162. * CVL event triggered FCF rediscover request failed,
  14163. * last resort to re-try current registered FCF entry.
  14164. */
  14165. lpfc_retry_pport_discovery(phba);
  14166. } else {
  14167. spin_lock_irq(&phba->hbalock);
  14168. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  14169. spin_unlock_irq(&phba->hbalock);
  14170. /*
  14171. * DEAD FCF event triggered FCF rediscover request
  14172. * failed, last resort to fail over as a link down
  14173. * to FCF registration.
  14174. */
  14175. lpfc_sli4_fcf_dead_failthrough(phba);
  14176. }
  14177. } else {
  14178. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14179. "2775 Start FCF rediscover quiescent timer\n");
  14180. /*
  14181. * Start FCF rediscovery wait timer for pending FCF
  14182. * before rescan FCF record table.
  14183. */
  14184. lpfc_fcf_redisc_wait_start_timer(phba);
  14185. }
  14186. mempool_free(mbox, phba->mbox_mem_pool);
  14187. }
  14188. /**
  14189. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14190. * @phba: pointer to lpfc hba data structure.
  14191. *
  14192. * This routine is invoked to request for rediscovery of the entire FCF table
  14193. * by the port.
  14194. **/
  14195. int
  14196. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14197. {
  14198. LPFC_MBOXQ_t *mbox;
  14199. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14200. int rc, length;
  14201. /* Cancel retry delay timers to all vports before FCF rediscover */
  14202. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14203. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14204. if (!mbox) {
  14205. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14206. "2745 Failed to allocate mbox for "
  14207. "requesting FCF rediscover.\n");
  14208. return -ENOMEM;
  14209. }
  14210. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14211. sizeof(struct lpfc_sli4_cfg_mhdr));
  14212. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14213. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14214. length, LPFC_SLI4_MBX_EMBED);
  14215. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14216. /* Set count to 0 for invalidating the entire FCF database */
  14217. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14218. /* Issue the mailbox command asynchronously */
  14219. mbox->vport = phba->pport;
  14220. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14221. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14222. if (rc == MBX_NOT_FINISHED) {
  14223. mempool_free(mbox, phba->mbox_mem_pool);
  14224. return -EIO;
  14225. }
  14226. return 0;
  14227. }
  14228. /**
  14229. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14230. * @phba: pointer to lpfc hba data structure.
  14231. *
  14232. * This function is the failover routine as a last resort to the FCF DEAD
  14233. * event when driver failed to perform fast FCF failover.
  14234. **/
  14235. void
  14236. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14237. {
  14238. uint32_t link_state;
  14239. /*
  14240. * Last resort as FCF DEAD event failover will treat this as
  14241. * a link down, but save the link state because we don't want
  14242. * it to be changed to Link Down unless it is already down.
  14243. */
  14244. link_state = phba->link_state;
  14245. lpfc_linkdown(phba);
  14246. phba->link_state = link_state;
  14247. /* Unregister FCF if no devices connected to it */
  14248. lpfc_unregister_unused_fcf(phba);
  14249. }
  14250. /**
  14251. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14252. * @phba: pointer to lpfc hba data structure.
  14253. * @rgn23_data: pointer to configure region 23 data.
  14254. *
  14255. * This function gets SLI3 port configure region 23 data through memory dump
  14256. * mailbox command. When it successfully retrieves data, the size of the data
  14257. * will be returned, otherwise, 0 will be returned.
  14258. **/
  14259. static uint32_t
  14260. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14261. {
  14262. LPFC_MBOXQ_t *pmb = NULL;
  14263. MAILBOX_t *mb;
  14264. uint32_t offset = 0;
  14265. int rc;
  14266. if (!rgn23_data)
  14267. return 0;
  14268. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14269. if (!pmb) {
  14270. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14271. "2600 failed to allocate mailbox memory\n");
  14272. return 0;
  14273. }
  14274. mb = &pmb->u.mb;
  14275. do {
  14276. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14277. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14278. if (rc != MBX_SUCCESS) {
  14279. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14280. "2601 failed to read config "
  14281. "region 23, rc 0x%x Status 0x%x\n",
  14282. rc, mb->mbxStatus);
  14283. mb->un.varDmp.word_cnt = 0;
  14284. }
  14285. /*
  14286. * dump mem may return a zero when finished or we got a
  14287. * mailbox error, either way we are done.
  14288. */
  14289. if (mb->un.varDmp.word_cnt == 0)
  14290. break;
  14291. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14292. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14293. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14294. rgn23_data + offset,
  14295. mb->un.varDmp.word_cnt);
  14296. offset += mb->un.varDmp.word_cnt;
  14297. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14298. mempool_free(pmb, phba->mbox_mem_pool);
  14299. return offset;
  14300. }
  14301. /**
  14302. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14303. * @phba: pointer to lpfc hba data structure.
  14304. * @rgn23_data: pointer to configure region 23 data.
  14305. *
  14306. * This function gets SLI4 port configure region 23 data through memory dump
  14307. * mailbox command. When it successfully retrieves data, the size of the data
  14308. * will be returned, otherwise, 0 will be returned.
  14309. **/
  14310. static uint32_t
  14311. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14312. {
  14313. LPFC_MBOXQ_t *mboxq = NULL;
  14314. struct lpfc_dmabuf *mp = NULL;
  14315. struct lpfc_mqe *mqe;
  14316. uint32_t data_length = 0;
  14317. int rc;
  14318. if (!rgn23_data)
  14319. return 0;
  14320. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14321. if (!mboxq) {
  14322. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14323. "3105 failed to allocate mailbox memory\n");
  14324. return 0;
  14325. }
  14326. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14327. goto out;
  14328. mqe = &mboxq->u.mqe;
  14329. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14330. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14331. if (rc)
  14332. goto out;
  14333. data_length = mqe->un.mb_words[5];
  14334. if (data_length == 0)
  14335. goto out;
  14336. if (data_length > DMP_RGN23_SIZE) {
  14337. data_length = 0;
  14338. goto out;
  14339. }
  14340. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14341. out:
  14342. mempool_free(mboxq, phba->mbox_mem_pool);
  14343. if (mp) {
  14344. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14345. kfree(mp);
  14346. }
  14347. return data_length;
  14348. }
  14349. /**
  14350. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14351. * @phba: pointer to lpfc hba data structure.
  14352. *
  14353. * This function read region 23 and parse TLV for port status to
  14354. * decide if the user disaled the port. If the TLV indicates the
  14355. * port is disabled, the hba_flag is set accordingly.
  14356. **/
  14357. void
  14358. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14359. {
  14360. uint8_t *rgn23_data = NULL;
  14361. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14362. uint32_t offset = 0;
  14363. /* Get adapter Region 23 data */
  14364. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14365. if (!rgn23_data)
  14366. goto out;
  14367. if (phba->sli_rev < LPFC_SLI_REV4)
  14368. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14369. else {
  14370. if_type = bf_get(lpfc_sli_intf_if_type,
  14371. &phba->sli4_hba.sli_intf);
  14372. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14373. goto out;
  14374. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14375. }
  14376. if (!data_size)
  14377. goto out;
  14378. /* Check the region signature first */
  14379. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14380. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14381. "2619 Config region 23 has bad signature\n");
  14382. goto out;
  14383. }
  14384. offset += 4;
  14385. /* Check the data structure version */
  14386. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14387. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14388. "2620 Config region 23 has bad version\n");
  14389. goto out;
  14390. }
  14391. offset += 4;
  14392. /* Parse TLV entries in the region */
  14393. while (offset < data_size) {
  14394. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14395. break;
  14396. /*
  14397. * If the TLV is not driver specific TLV or driver id is
  14398. * not linux driver id, skip the record.
  14399. */
  14400. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14401. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14402. (rgn23_data[offset + 3] != 0)) {
  14403. offset += rgn23_data[offset + 1] * 4 + 4;
  14404. continue;
  14405. }
  14406. /* Driver found a driver specific TLV in the config region */
  14407. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14408. offset += 4;
  14409. tlv_offset = 0;
  14410. /*
  14411. * Search for configured port state sub-TLV.
  14412. */
  14413. while ((offset < data_size) &&
  14414. (tlv_offset < sub_tlv_len)) {
  14415. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14416. offset += 4;
  14417. tlv_offset += 4;
  14418. break;
  14419. }
  14420. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14421. offset += rgn23_data[offset + 1] * 4 + 4;
  14422. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14423. continue;
  14424. }
  14425. /* This HBA contains PORT_STE configured */
  14426. if (!rgn23_data[offset + 2])
  14427. phba->hba_flag |= LINK_DISABLED;
  14428. goto out;
  14429. }
  14430. }
  14431. out:
  14432. kfree(rgn23_data);
  14433. return;
  14434. }
  14435. /**
  14436. * lpfc_wr_object - write an object to the firmware
  14437. * @phba: HBA structure that indicates port to create a queue on.
  14438. * @dmabuf_list: list of dmabufs to write to the port.
  14439. * @size: the total byte value of the objects to write to the port.
  14440. * @offset: the current offset to be used to start the transfer.
  14441. *
  14442. * This routine will create a wr_object mailbox command to send to the port.
  14443. * the mailbox command will be constructed using the dma buffers described in
  14444. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14445. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14446. * used to indicate the starting offset of the transfer and will also return
  14447. * the offset after the write object mailbox has completed. @size is used to
  14448. * determine the end of the object and whether the eof bit should be set.
  14449. *
  14450. * Return 0 is successful and offset will contain the the new offset to use
  14451. * for the next write.
  14452. * Return negative value for error cases.
  14453. **/
  14454. int
  14455. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14456. uint32_t size, uint32_t *offset)
  14457. {
  14458. struct lpfc_mbx_wr_object *wr_object;
  14459. LPFC_MBOXQ_t *mbox;
  14460. int rc = 0, i = 0;
  14461. uint32_t shdr_status, shdr_add_status;
  14462. uint32_t mbox_tmo;
  14463. union lpfc_sli4_cfg_shdr *shdr;
  14464. struct lpfc_dmabuf *dmabuf;
  14465. uint32_t written = 0;
  14466. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14467. if (!mbox)
  14468. return -ENOMEM;
  14469. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14470. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14471. sizeof(struct lpfc_mbx_wr_object) -
  14472. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14473. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14474. wr_object->u.request.write_offset = *offset;
  14475. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14476. wr_object->u.request.object_name[0] =
  14477. cpu_to_le32(wr_object->u.request.object_name[0]);
  14478. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14479. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14480. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14481. break;
  14482. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14483. wr_object->u.request.bde[i].addrHigh =
  14484. putPaddrHigh(dmabuf->phys);
  14485. if (written + SLI4_PAGE_SIZE >= size) {
  14486. wr_object->u.request.bde[i].tus.f.bdeSize =
  14487. (size - written);
  14488. written += (size - written);
  14489. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14490. } else {
  14491. wr_object->u.request.bde[i].tus.f.bdeSize =
  14492. SLI4_PAGE_SIZE;
  14493. written += SLI4_PAGE_SIZE;
  14494. }
  14495. i++;
  14496. }
  14497. wr_object->u.request.bde_count = i;
  14498. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14499. if (!phba->sli4_hba.intr_enable)
  14500. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14501. else {
  14502. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14503. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14504. }
  14505. /* The IOCTL status is embedded in the mailbox subheader. */
  14506. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14507. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14508. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14509. if (rc != MBX_TIMEOUT)
  14510. mempool_free(mbox, phba->mbox_mem_pool);
  14511. if (shdr_status || shdr_add_status || rc) {
  14512. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14513. "3025 Write Object mailbox failed with "
  14514. "status x%x add_status x%x, mbx status x%x\n",
  14515. shdr_status, shdr_add_status, rc);
  14516. rc = -ENXIO;
  14517. } else
  14518. *offset += wr_object->u.response.actual_write_length;
  14519. return rc;
  14520. }
  14521. /**
  14522. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14523. * @vport: pointer to vport data structure.
  14524. *
  14525. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14526. * and REG_VPI mailbox commands associated with the vport. This function
  14527. * is called when driver want to restart discovery of the vport due to
  14528. * a Clear Virtual Link event.
  14529. **/
  14530. void
  14531. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14532. {
  14533. struct lpfc_hba *phba = vport->phba;
  14534. LPFC_MBOXQ_t *mb, *nextmb;
  14535. struct lpfc_dmabuf *mp;
  14536. struct lpfc_nodelist *ndlp;
  14537. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14538. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14539. LIST_HEAD(mbox_cmd_list);
  14540. uint8_t restart_loop;
  14541. /* Clean up internally queued mailbox commands with the vport */
  14542. spin_lock_irq(&phba->hbalock);
  14543. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14544. if (mb->vport != vport)
  14545. continue;
  14546. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14547. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14548. continue;
  14549. list_del(&mb->list);
  14550. list_add_tail(&mb->list, &mbox_cmd_list);
  14551. }
  14552. /* Clean up active mailbox command with the vport */
  14553. mb = phba->sli.mbox_active;
  14554. if (mb && (mb->vport == vport)) {
  14555. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14556. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14557. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14558. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14559. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14560. /* Put reference count for delayed processing */
  14561. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14562. /* Unregister the RPI when mailbox complete */
  14563. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14564. }
  14565. }
  14566. /* Cleanup any mailbox completions which are not yet processed */
  14567. do {
  14568. restart_loop = 0;
  14569. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14570. /*
  14571. * If this mailox is already processed or it is
  14572. * for another vport ignore it.
  14573. */
  14574. if ((mb->vport != vport) ||
  14575. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14576. continue;
  14577. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14578. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14579. continue;
  14580. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14581. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14582. ndlp = (struct lpfc_nodelist *)mb->context2;
  14583. /* Unregister the RPI when mailbox complete */
  14584. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14585. restart_loop = 1;
  14586. spin_unlock_irq(&phba->hbalock);
  14587. spin_lock(shost->host_lock);
  14588. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14589. spin_unlock(shost->host_lock);
  14590. spin_lock_irq(&phba->hbalock);
  14591. break;
  14592. }
  14593. }
  14594. } while (restart_loop);
  14595. spin_unlock_irq(&phba->hbalock);
  14596. /* Release the cleaned-up mailbox commands */
  14597. while (!list_empty(&mbox_cmd_list)) {
  14598. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14599. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14600. mp = (struct lpfc_dmabuf *) (mb->context1);
  14601. if (mp) {
  14602. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14603. kfree(mp);
  14604. }
  14605. ndlp = (struct lpfc_nodelist *) mb->context2;
  14606. mb->context2 = NULL;
  14607. if (ndlp) {
  14608. spin_lock(shost->host_lock);
  14609. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14610. spin_unlock(shost->host_lock);
  14611. lpfc_nlp_put(ndlp);
  14612. }
  14613. }
  14614. mempool_free(mb, phba->mbox_mem_pool);
  14615. }
  14616. /* Release the ndlp with the cleaned-up active mailbox command */
  14617. if (act_mbx_ndlp) {
  14618. spin_lock(shost->host_lock);
  14619. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14620. spin_unlock(shost->host_lock);
  14621. lpfc_nlp_put(act_mbx_ndlp);
  14622. }
  14623. }
  14624. /**
  14625. * lpfc_drain_txq - Drain the txq
  14626. * @phba: Pointer to HBA context object.
  14627. *
  14628. * This function attempt to submit IOCBs on the txq
  14629. * to the adapter. For SLI4 adapters, the txq contains
  14630. * ELS IOCBs that have been deferred because the there
  14631. * are no SGLs. This congestion can occur with large
  14632. * vport counts during node discovery.
  14633. **/
  14634. uint32_t
  14635. lpfc_drain_txq(struct lpfc_hba *phba)
  14636. {
  14637. LIST_HEAD(completions);
  14638. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14639. struct lpfc_iocbq *piocbq = 0;
  14640. unsigned long iflags = 0;
  14641. char *fail_msg = NULL;
  14642. struct lpfc_sglq *sglq;
  14643. union lpfc_wqe wqe;
  14644. spin_lock_irqsave(&phba->hbalock, iflags);
  14645. if (pring->txq_cnt > pring->txq_max)
  14646. pring->txq_max = pring->txq_cnt;
  14647. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14648. while (pring->txq_cnt) {
  14649. spin_lock_irqsave(&phba->hbalock, iflags);
  14650. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14651. if (!piocbq) {
  14652. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14653. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14654. "2823 txq empty and txq_cnt is %d\n ",
  14655. pring->txq_cnt);
  14656. break;
  14657. }
  14658. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14659. if (!sglq) {
  14660. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14661. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14662. break;
  14663. }
  14664. /* The xri and iocb resources secured,
  14665. * attempt to issue request
  14666. */
  14667. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14668. piocbq->sli4_xritag = sglq->sli4_xritag;
  14669. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14670. fail_msg = "to convert bpl to sgl";
  14671. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14672. fail_msg = "to convert iocb to wqe";
  14673. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14674. fail_msg = " - Wq is full";
  14675. else
  14676. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14677. if (fail_msg) {
  14678. /* Failed means we can't issue and need to cancel */
  14679. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14680. "2822 IOCB failed %s iotag 0x%x "
  14681. "xri 0x%x\n",
  14682. fail_msg,
  14683. piocbq->iotag, piocbq->sli4_xritag);
  14684. list_add_tail(&piocbq->list, &completions);
  14685. }
  14686. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14687. }
  14688. /* Cancel all the IOCBs that cannot be issued */
  14689. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14690. IOERR_SLI_ABORTED);
  14691. return pring->txq_cnt;
  14692. }