lpfc_sli.c 489 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2012 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/fc/fc_fs.h>
  32. #include <linux/aer.h>
  33. #include "lpfc_hw4.h"
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_nl.h"
  38. #include "lpfc_disc.h"
  39. #include "lpfc_scsi.h"
  40. #include "lpfc.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_compat.h"
  44. #include "lpfc_debugfs.h"
  45. #include "lpfc_vport.h"
  46. /* There are only four IOCB completion types. */
  47. typedef enum _lpfc_iocb_type {
  48. LPFC_UNKNOWN_IOCB,
  49. LPFC_UNSOL_IOCB,
  50. LPFC_SOL_IOCB,
  51. LPFC_ABORT_IOCB
  52. } lpfc_iocb_type;
  53. /* Provide function prototypes local to this module. */
  54. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint32_t);
  56. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  57. uint8_t *, uint32_t *);
  58. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  59. struct lpfc_iocbq *);
  60. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  61. struct hbq_dmabuf *);
  62. static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
  63. struct lpfc_cqe *);
  64. static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
  65. int);
  66. static IOCB_t *
  67. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  68. {
  69. return &iocbq->iocb;
  70. }
  71. /**
  72. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  73. * @q: The Work Queue to operate on.
  74. * @wqe: The work Queue Entry to put on the Work queue.
  75. *
  76. * This routine will copy the contents of @wqe to the next available entry on
  77. * the @q. This function will then ring the Work Queue Doorbell to signal the
  78. * HBA to start processing the Work Queue Entry. This function returns 0 if
  79. * successful. If no entries are available on @q then this function will return
  80. * -ENOMEM.
  81. * The caller is expected to hold the hbalock when calling this routine.
  82. **/
  83. static uint32_t
  84. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  85. {
  86. union lpfc_wqe *temp_wqe;
  87. struct lpfc_register doorbell;
  88. uint32_t host_index;
  89. uint32_t idx;
  90. /* sanity check on queue memory */
  91. if (unlikely(!q))
  92. return -ENOMEM;
  93. temp_wqe = q->qe[q->host_index].wqe;
  94. /* If the host has not yet processed the next entry then we are done */
  95. idx = ((q->host_index + 1) % q->entry_count);
  96. if (idx == q->hba_index) {
  97. q->WQ_overflow++;
  98. return -ENOMEM;
  99. }
  100. q->WQ_posted++;
  101. /* set consumption flag every once in a while */
  102. if (!((q->host_index + 1) % q->entry_repost))
  103. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  104. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  105. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  106. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  107. /* Update the host index before invoking device */
  108. host_index = q->host_index;
  109. q->host_index = idx;
  110. /* Ring Doorbell */
  111. doorbell.word0 = 0;
  112. bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
  113. bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
  114. bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
  115. writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
  116. return 0;
  117. }
  118. /**
  119. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  120. * @q: The Work Queue to operate on.
  121. * @index: The index to advance the hba index to.
  122. *
  123. * This routine will update the HBA index of a queue to reflect consumption of
  124. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  125. * an entry the host calls this function to update the queue's internal
  126. * pointers. This routine returns the number of entries that were consumed by
  127. * the HBA.
  128. **/
  129. static uint32_t
  130. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  131. {
  132. uint32_t released = 0;
  133. /* sanity check on queue memory */
  134. if (unlikely(!q))
  135. return 0;
  136. if (q->hba_index == index)
  137. return 0;
  138. do {
  139. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  140. released++;
  141. } while (q->hba_index != index);
  142. return released;
  143. }
  144. /**
  145. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  146. * @q: The Mailbox Queue to operate on.
  147. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  148. *
  149. * This routine will copy the contents of @mqe to the next available entry on
  150. * the @q. This function will then ring the Work Queue Doorbell to signal the
  151. * HBA to start processing the Work Queue Entry. This function returns 0 if
  152. * successful. If no entries are available on @q then this function will return
  153. * -ENOMEM.
  154. * The caller is expected to hold the hbalock when calling this routine.
  155. **/
  156. static uint32_t
  157. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  158. {
  159. struct lpfc_mqe *temp_mqe;
  160. struct lpfc_register doorbell;
  161. uint32_t host_index;
  162. /* sanity check on queue memory */
  163. if (unlikely(!q))
  164. return -ENOMEM;
  165. temp_mqe = q->qe[q->host_index].mqe;
  166. /* If the host has not yet processed the next entry then we are done */
  167. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  168. return -ENOMEM;
  169. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  170. /* Save off the mailbox pointer for completion */
  171. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  172. /* Update the host index before invoking device */
  173. host_index = q->host_index;
  174. q->host_index = ((q->host_index + 1) % q->entry_count);
  175. /* Ring Doorbell */
  176. doorbell.word0 = 0;
  177. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  178. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  179. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  180. return 0;
  181. }
  182. /**
  183. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  184. * @q: The Mailbox Queue to operate on.
  185. *
  186. * This routine will update the HBA index of a queue to reflect consumption of
  187. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  188. * an entry the host calls this function to update the queue's internal
  189. * pointers. This routine returns the number of entries that were consumed by
  190. * the HBA.
  191. **/
  192. static uint32_t
  193. lpfc_sli4_mq_release(struct lpfc_queue *q)
  194. {
  195. /* sanity check on queue memory */
  196. if (unlikely(!q))
  197. return 0;
  198. /* Clear the mailbox pointer for completion */
  199. q->phba->mbox = NULL;
  200. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  201. return 1;
  202. }
  203. /**
  204. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  205. * @q: The Event Queue to get the first valid EQE from
  206. *
  207. * This routine will get the first valid Event Queue Entry from @q, update
  208. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  209. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  210. * processed, but not popped back to the HBA then this routine will return NULL.
  211. **/
  212. static struct lpfc_eqe *
  213. lpfc_sli4_eq_get(struct lpfc_queue *q)
  214. {
  215. struct lpfc_eqe *eqe;
  216. uint32_t idx;
  217. /* sanity check on queue memory */
  218. if (unlikely(!q))
  219. return NULL;
  220. eqe = q->qe[q->hba_index].eqe;
  221. /* If the next EQE is not valid then we are done */
  222. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  223. return NULL;
  224. /* If the host has not yet processed the next entry then we are done */
  225. idx = ((q->hba_index + 1) % q->entry_count);
  226. if (idx == q->host_index)
  227. return NULL;
  228. q->hba_index = idx;
  229. return eqe;
  230. }
  231. /**
  232. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  233. * @q: The Event Queue that the host has completed processing for.
  234. * @arm: Indicates whether the host wants to arms this CQ.
  235. *
  236. * This routine will mark all Event Queue Entries on @q, from the last
  237. * known completed entry to the last entry that was processed, as completed
  238. * by clearing the valid bit for each completion queue entry. Then it will
  239. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  240. * The internal host index in the @q will be updated by this routine to indicate
  241. * that the host has finished processing the entries. The @arm parameter
  242. * indicates that the queue should be rearmed when ringing the doorbell.
  243. *
  244. * This function will return the number of EQEs that were popped.
  245. **/
  246. uint32_t
  247. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  248. {
  249. uint32_t released = 0;
  250. struct lpfc_eqe *temp_eqe;
  251. struct lpfc_register doorbell;
  252. /* sanity check on queue memory */
  253. if (unlikely(!q))
  254. return 0;
  255. /* while there are valid entries */
  256. while (q->hba_index != q->host_index) {
  257. temp_eqe = q->qe[q->host_index].eqe;
  258. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  259. released++;
  260. q->host_index = ((q->host_index + 1) % q->entry_count);
  261. }
  262. if (unlikely(released == 0 && !arm))
  263. return 0;
  264. /* ring doorbell for number popped */
  265. doorbell.word0 = 0;
  266. if (arm) {
  267. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  268. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  269. }
  270. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  271. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  272. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  273. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  274. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  275. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  276. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  277. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  278. readl(q->phba->sli4_hba.EQCQDBregaddr);
  279. return released;
  280. }
  281. /**
  282. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  283. * @q: The Completion Queue to get the first valid CQE from
  284. *
  285. * This routine will get the first valid Completion Queue Entry from @q, update
  286. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  287. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  288. * processed, but not popped back to the HBA then this routine will return NULL.
  289. **/
  290. static struct lpfc_cqe *
  291. lpfc_sli4_cq_get(struct lpfc_queue *q)
  292. {
  293. struct lpfc_cqe *cqe;
  294. uint32_t idx;
  295. /* sanity check on queue memory */
  296. if (unlikely(!q))
  297. return NULL;
  298. /* If the next CQE is not valid then we are done */
  299. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  300. return NULL;
  301. /* If the host has not yet processed the next entry then we are done */
  302. idx = ((q->hba_index + 1) % q->entry_count);
  303. if (idx == q->host_index)
  304. return NULL;
  305. cqe = q->qe[q->hba_index].cqe;
  306. q->hba_index = idx;
  307. return cqe;
  308. }
  309. /**
  310. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  311. * @q: The Completion Queue that the host has completed processing for.
  312. * @arm: Indicates whether the host wants to arms this CQ.
  313. *
  314. * This routine will mark all Completion queue entries on @q, from the last
  315. * known completed entry to the last entry that was processed, as completed
  316. * by clearing the valid bit for each completion queue entry. Then it will
  317. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  318. * The internal host index in the @q will be updated by this routine to indicate
  319. * that the host has finished processing the entries. The @arm parameter
  320. * indicates that the queue should be rearmed when ringing the doorbell.
  321. *
  322. * This function will return the number of CQEs that were released.
  323. **/
  324. uint32_t
  325. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  326. {
  327. uint32_t released = 0;
  328. struct lpfc_cqe *temp_qe;
  329. struct lpfc_register doorbell;
  330. /* sanity check on queue memory */
  331. if (unlikely(!q))
  332. return 0;
  333. /* while there are valid entries */
  334. while (q->hba_index != q->host_index) {
  335. temp_qe = q->qe[q->host_index].cqe;
  336. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  337. released++;
  338. q->host_index = ((q->host_index + 1) % q->entry_count);
  339. }
  340. if (unlikely(released == 0 && !arm))
  341. return 0;
  342. /* ring doorbell for number popped */
  343. doorbell.word0 = 0;
  344. if (arm)
  345. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  346. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  347. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  348. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  349. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  350. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  351. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  352. return released;
  353. }
  354. /**
  355. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  356. * @q: The Header Receive Queue to operate on.
  357. * @wqe: The Receive Queue Entry to put on the Receive queue.
  358. *
  359. * This routine will copy the contents of @wqe to the next available entry on
  360. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  361. * HBA to start processing the Receive Queue Entry. This function returns the
  362. * index that the rqe was copied to if successful. If no entries are available
  363. * on @q then this function will return -ENOMEM.
  364. * The caller is expected to hold the hbalock when calling this routine.
  365. **/
  366. static int
  367. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  368. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  369. {
  370. struct lpfc_rqe *temp_hrqe;
  371. struct lpfc_rqe *temp_drqe;
  372. struct lpfc_register doorbell;
  373. int put_index = hq->host_index;
  374. /* sanity check on queue memory */
  375. if (unlikely(!hq) || unlikely(!dq))
  376. return -ENOMEM;
  377. temp_hrqe = hq->qe[hq->host_index].rqe;
  378. temp_drqe = dq->qe[dq->host_index].rqe;
  379. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  380. return -EINVAL;
  381. if (hq->host_index != dq->host_index)
  382. return -EINVAL;
  383. /* If the host has not yet processed the next entry then we are done */
  384. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  385. return -EBUSY;
  386. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  387. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  388. /* Update the host index to point to the next slot */
  389. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  390. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  391. /* Ring The Header Receive Queue Doorbell */
  392. if (!(hq->host_index % hq->entry_repost)) {
  393. doorbell.word0 = 0;
  394. bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
  395. hq->entry_repost);
  396. bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
  397. writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
  398. }
  399. return put_index;
  400. }
  401. /**
  402. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  403. * @q: The Header Receive Queue to operate on.
  404. *
  405. * This routine will update the HBA index of a queue to reflect consumption of
  406. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  407. * consumed an entry the host calls this function to update the queue's
  408. * internal pointers. This routine returns the number of entries that were
  409. * consumed by the HBA.
  410. **/
  411. static uint32_t
  412. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  413. {
  414. /* sanity check on queue memory */
  415. if (unlikely(!hq) || unlikely(!dq))
  416. return 0;
  417. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  418. return 0;
  419. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  420. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  421. return 1;
  422. }
  423. /**
  424. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  425. * @phba: Pointer to HBA context object.
  426. * @pring: Pointer to driver SLI ring object.
  427. *
  428. * This function returns pointer to next command iocb entry
  429. * in the command ring. The caller must hold hbalock to prevent
  430. * other threads consume the next command iocb.
  431. * SLI-2/SLI-3 provide different sized iocbs.
  432. **/
  433. static inline IOCB_t *
  434. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  435. {
  436. return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
  437. pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
  438. }
  439. /**
  440. * lpfc_resp_iocb - Get next response iocb entry in the ring
  441. * @phba: Pointer to HBA context object.
  442. * @pring: Pointer to driver SLI ring object.
  443. *
  444. * This function returns pointer to next response iocb entry
  445. * in the response ring. The caller must hold hbalock to make sure
  446. * that no other thread consume the next response iocb.
  447. * SLI-2/SLI-3 provide different sized iocbs.
  448. **/
  449. static inline IOCB_t *
  450. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  451. {
  452. return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
  453. pring->sli.sli3.rspidx * phba->iocb_rsp_size);
  454. }
  455. /**
  456. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  457. * @phba: Pointer to HBA context object.
  458. *
  459. * This function is called with hbalock held. This function
  460. * allocates a new driver iocb object from the iocb pool. If the
  461. * allocation is successful, it returns pointer to the newly
  462. * allocated iocb object else it returns NULL.
  463. **/
  464. struct lpfc_iocbq *
  465. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  466. {
  467. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  468. struct lpfc_iocbq * iocbq = NULL;
  469. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  470. if (iocbq)
  471. phba->iocb_cnt++;
  472. if (phba->iocb_cnt > phba->iocb_max)
  473. phba->iocb_max = phba->iocb_cnt;
  474. return iocbq;
  475. }
  476. /**
  477. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  478. * @phba: Pointer to HBA context object.
  479. * @xritag: XRI value.
  480. *
  481. * This function clears the sglq pointer from the array of acive
  482. * sglq's. The xritag that is passed in is used to index into the
  483. * array. Before the xritag can be used it needs to be adjusted
  484. * by subtracting the xribase.
  485. *
  486. * Returns sglq ponter = success, NULL = Failure.
  487. **/
  488. static struct lpfc_sglq *
  489. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  490. {
  491. struct lpfc_sglq *sglq;
  492. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  493. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  494. return sglq;
  495. }
  496. /**
  497. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  498. * @phba: Pointer to HBA context object.
  499. * @xritag: XRI value.
  500. *
  501. * This function returns the sglq pointer from the array of acive
  502. * sglq's. The xritag that is passed in is used to index into the
  503. * array. Before the xritag can be used it needs to be adjusted
  504. * by subtracting the xribase.
  505. *
  506. * Returns sglq ponter = success, NULL = Failure.
  507. **/
  508. struct lpfc_sglq *
  509. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  510. {
  511. struct lpfc_sglq *sglq;
  512. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  513. return sglq;
  514. }
  515. /**
  516. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  517. * @phba: Pointer to HBA context object.
  518. * @xritag: xri used in this exchange.
  519. * @rrq: The RRQ to be cleared.
  520. *
  521. **/
  522. void
  523. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  524. uint16_t xritag,
  525. struct lpfc_node_rrq *rrq)
  526. {
  527. struct lpfc_nodelist *ndlp = NULL;
  528. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  529. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  530. /* The target DID could have been swapped (cable swap)
  531. * we should use the ndlp from the findnode if it is
  532. * available.
  533. */
  534. if ((!ndlp) && rrq->ndlp)
  535. ndlp = rrq->ndlp;
  536. if (!ndlp)
  537. goto out;
  538. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  539. rrq->send_rrq = 0;
  540. rrq->xritag = 0;
  541. rrq->rrq_stop_time = 0;
  542. }
  543. out:
  544. mempool_free(rrq, phba->rrq_pool);
  545. }
  546. /**
  547. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  548. * @phba: Pointer to HBA context object.
  549. *
  550. * This function is called with hbalock held. This function
  551. * Checks if stop_time (ratov from setting rrq active) has
  552. * been reached, if it has and the send_rrq flag is set then
  553. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  554. * then it will just call the routine to clear the rrq and
  555. * free the rrq resource.
  556. * The timer is set to the next rrq that is going to expire before
  557. * leaving the routine.
  558. *
  559. **/
  560. void
  561. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  562. {
  563. struct lpfc_node_rrq *rrq;
  564. struct lpfc_node_rrq *nextrrq;
  565. unsigned long next_time;
  566. unsigned long iflags;
  567. LIST_HEAD(send_rrq);
  568. spin_lock_irqsave(&phba->hbalock, iflags);
  569. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  570. next_time = jiffies + HZ * (phba->fc_ratov + 1);
  571. list_for_each_entry_safe(rrq, nextrrq,
  572. &phba->active_rrq_list, list) {
  573. if (time_after(jiffies, rrq->rrq_stop_time))
  574. list_move(&rrq->list, &send_rrq);
  575. else if (time_before(rrq->rrq_stop_time, next_time))
  576. next_time = rrq->rrq_stop_time;
  577. }
  578. spin_unlock_irqrestore(&phba->hbalock, iflags);
  579. if (!list_empty(&phba->active_rrq_list))
  580. mod_timer(&phba->rrq_tmr, next_time);
  581. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  582. list_del(&rrq->list);
  583. if (!rrq->send_rrq)
  584. /* this call will free the rrq */
  585. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  586. else if (lpfc_send_rrq(phba, rrq)) {
  587. /* if we send the rrq then the completion handler
  588. * will clear the bit in the xribitmap.
  589. */
  590. lpfc_clr_rrq_active(phba, rrq->xritag,
  591. rrq);
  592. }
  593. }
  594. }
  595. /**
  596. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  597. * @vport: Pointer to vport context object.
  598. * @xri: The xri used in the exchange.
  599. * @did: The targets DID for this exchange.
  600. *
  601. * returns NULL = rrq not found in the phba->active_rrq_list.
  602. * rrq = rrq for this xri and target.
  603. **/
  604. struct lpfc_node_rrq *
  605. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  606. {
  607. struct lpfc_hba *phba = vport->phba;
  608. struct lpfc_node_rrq *rrq;
  609. struct lpfc_node_rrq *nextrrq;
  610. unsigned long iflags;
  611. if (phba->sli_rev != LPFC_SLI_REV4)
  612. return NULL;
  613. spin_lock_irqsave(&phba->hbalock, iflags);
  614. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  615. if (rrq->vport == vport && rrq->xritag == xri &&
  616. rrq->nlp_DID == did){
  617. list_del(&rrq->list);
  618. spin_unlock_irqrestore(&phba->hbalock, iflags);
  619. return rrq;
  620. }
  621. }
  622. spin_unlock_irqrestore(&phba->hbalock, iflags);
  623. return NULL;
  624. }
  625. /**
  626. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  627. * @vport: Pointer to vport context object.
  628. * @ndlp: Pointer to the lpfc_node_list structure.
  629. * If ndlp is NULL Remove all active RRQs for this vport from the
  630. * phba->active_rrq_list and clear the rrq.
  631. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  632. **/
  633. void
  634. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  635. {
  636. struct lpfc_hba *phba = vport->phba;
  637. struct lpfc_node_rrq *rrq;
  638. struct lpfc_node_rrq *nextrrq;
  639. unsigned long iflags;
  640. LIST_HEAD(rrq_list);
  641. if (phba->sli_rev != LPFC_SLI_REV4)
  642. return;
  643. if (!ndlp) {
  644. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  645. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  646. }
  647. spin_lock_irqsave(&phba->hbalock, iflags);
  648. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  649. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  650. list_move(&rrq->list, &rrq_list);
  651. spin_unlock_irqrestore(&phba->hbalock, iflags);
  652. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  653. list_del(&rrq->list);
  654. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  655. }
  656. }
  657. /**
  658. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  659. * @phba: Pointer to HBA context object.
  660. *
  661. * Remove all rrqs from the phba->active_rrq_list and free them by
  662. * calling __lpfc_clr_active_rrq
  663. *
  664. **/
  665. void
  666. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  667. {
  668. struct lpfc_node_rrq *rrq;
  669. struct lpfc_node_rrq *nextrrq;
  670. unsigned long next_time;
  671. unsigned long iflags;
  672. LIST_HEAD(rrq_list);
  673. if (phba->sli_rev != LPFC_SLI_REV4)
  674. return;
  675. spin_lock_irqsave(&phba->hbalock, iflags);
  676. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  677. next_time = jiffies + HZ * (phba->fc_ratov * 2);
  678. list_splice_init(&phba->active_rrq_list, &rrq_list);
  679. spin_unlock_irqrestore(&phba->hbalock, iflags);
  680. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  681. list_del(&rrq->list);
  682. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  683. }
  684. if (!list_empty(&phba->active_rrq_list))
  685. mod_timer(&phba->rrq_tmr, next_time);
  686. }
  687. /**
  688. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  689. * @phba: Pointer to HBA context object.
  690. * @ndlp: Targets nodelist pointer for this exchange.
  691. * @xritag the xri in the bitmap to test.
  692. *
  693. * This function is called with hbalock held. This function
  694. * returns 0 = rrq not active for this xri
  695. * 1 = rrq is valid for this xri.
  696. **/
  697. int
  698. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  699. uint16_t xritag)
  700. {
  701. if (!ndlp)
  702. return 0;
  703. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  704. return 1;
  705. else
  706. return 0;
  707. }
  708. /**
  709. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  710. * @phba: Pointer to HBA context object.
  711. * @ndlp: nodelist pointer for this target.
  712. * @xritag: xri used in this exchange.
  713. * @rxid: Remote Exchange ID.
  714. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  715. *
  716. * This function takes the hbalock.
  717. * The active bit is always set in the active rrq xri_bitmap even
  718. * if there is no slot avaiable for the other rrq information.
  719. *
  720. * returns 0 rrq actived for this xri
  721. * < 0 No memory or invalid ndlp.
  722. **/
  723. int
  724. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  725. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  726. {
  727. unsigned long iflags;
  728. struct lpfc_node_rrq *rrq;
  729. int empty;
  730. if (!ndlp)
  731. return -EINVAL;
  732. if (!phba->cfg_enable_rrq)
  733. return -EINVAL;
  734. spin_lock_irqsave(&phba->hbalock, iflags);
  735. if (phba->pport->load_flag & FC_UNLOADING) {
  736. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  737. goto out;
  738. }
  739. /*
  740. * set the active bit even if there is no mem available.
  741. */
  742. if (NLP_CHK_FREE_REQ(ndlp))
  743. goto out;
  744. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  745. goto out;
  746. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  747. goto out;
  748. spin_unlock_irqrestore(&phba->hbalock, iflags);
  749. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  750. if (!rrq) {
  751. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  752. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  753. " DID:0x%x Send:%d\n",
  754. xritag, rxid, ndlp->nlp_DID, send_rrq);
  755. return -EINVAL;
  756. }
  757. rrq->send_rrq = send_rrq;
  758. rrq->xritag = xritag;
  759. rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
  760. rrq->ndlp = ndlp;
  761. rrq->nlp_DID = ndlp->nlp_DID;
  762. rrq->vport = ndlp->vport;
  763. rrq->rxid = rxid;
  764. rrq->send_rrq = send_rrq;
  765. spin_lock_irqsave(&phba->hbalock, iflags);
  766. empty = list_empty(&phba->active_rrq_list);
  767. list_add_tail(&rrq->list, &phba->active_rrq_list);
  768. phba->hba_flag |= HBA_RRQ_ACTIVE;
  769. if (empty)
  770. lpfc_worker_wake_up(phba);
  771. spin_unlock_irqrestore(&phba->hbalock, iflags);
  772. return 0;
  773. out:
  774. spin_unlock_irqrestore(&phba->hbalock, iflags);
  775. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  776. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  777. " DID:0x%x Send:%d\n",
  778. xritag, rxid, ndlp->nlp_DID, send_rrq);
  779. return -EINVAL;
  780. }
  781. /**
  782. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  783. * @phba: Pointer to HBA context object.
  784. * @piocb: Pointer to the iocbq.
  785. *
  786. * This function is called with hbalock held. This function
  787. * gets a new driver sglq object from the sglq list. If the
  788. * list is not empty then it is successful, it returns pointer to the newly
  789. * allocated sglq object else it returns NULL.
  790. **/
  791. static struct lpfc_sglq *
  792. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  793. {
  794. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  795. struct lpfc_sglq *sglq = NULL;
  796. struct lpfc_sglq *start_sglq = NULL;
  797. struct lpfc_scsi_buf *lpfc_cmd;
  798. struct lpfc_nodelist *ndlp;
  799. int found = 0;
  800. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  801. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  802. ndlp = lpfc_cmd->rdata->pnode;
  803. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  804. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  805. ndlp = piocbq->context_un.ndlp;
  806. else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
  807. (piocbq->iocb_flag & LPFC_IO_LIBDFC))
  808. ndlp = piocbq->context_un.ndlp;
  809. else
  810. ndlp = piocbq->context1;
  811. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  812. start_sglq = sglq;
  813. while (!found) {
  814. if (!sglq)
  815. return NULL;
  816. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
  817. /* This xri has an rrq outstanding for this DID.
  818. * put it back in the list and get another xri.
  819. */
  820. list_add_tail(&sglq->list, lpfc_sgl_list);
  821. sglq = NULL;
  822. list_remove_head(lpfc_sgl_list, sglq,
  823. struct lpfc_sglq, list);
  824. if (sglq == start_sglq) {
  825. sglq = NULL;
  826. break;
  827. } else
  828. continue;
  829. }
  830. sglq->ndlp = ndlp;
  831. found = 1;
  832. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  833. sglq->state = SGL_ALLOCATED;
  834. }
  835. return sglq;
  836. }
  837. /**
  838. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  839. * @phba: Pointer to HBA context object.
  840. *
  841. * This function is called with no lock held. This function
  842. * allocates a new driver iocb object from the iocb pool. If the
  843. * allocation is successful, it returns pointer to the newly
  844. * allocated iocb object else it returns NULL.
  845. **/
  846. struct lpfc_iocbq *
  847. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  848. {
  849. struct lpfc_iocbq * iocbq = NULL;
  850. unsigned long iflags;
  851. spin_lock_irqsave(&phba->hbalock, iflags);
  852. iocbq = __lpfc_sli_get_iocbq(phba);
  853. spin_unlock_irqrestore(&phba->hbalock, iflags);
  854. return iocbq;
  855. }
  856. /**
  857. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  858. * @phba: Pointer to HBA context object.
  859. * @iocbq: Pointer to driver iocb object.
  860. *
  861. * This function is called with hbalock held to release driver
  862. * iocb object to the iocb pool. The iotag in the iocb object
  863. * does not change for each use of the iocb object. This function
  864. * clears all other fields of the iocb object when it is freed.
  865. * The sqlq structure that holds the xritag and phys and virtual
  866. * mappings for the scatter gather list is retrieved from the
  867. * active array of sglq. The get of the sglq pointer also clears
  868. * the entry in the array. If the status of the IO indiactes that
  869. * this IO was aborted then the sglq entry it put on the
  870. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  871. * IO has good status or fails for any other reason then the sglq
  872. * entry is added to the free list (lpfc_sgl_list).
  873. **/
  874. static void
  875. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  876. {
  877. struct lpfc_sglq *sglq;
  878. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  879. unsigned long iflag = 0;
  880. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  881. if (iocbq->sli4_xritag == NO_XRI)
  882. sglq = NULL;
  883. else
  884. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  885. if (sglq) {
  886. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  887. (sglq->state != SGL_XRI_ABORTED)) {
  888. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  889. iflag);
  890. list_add(&sglq->list,
  891. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  892. spin_unlock_irqrestore(
  893. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  894. } else {
  895. sglq->state = SGL_FREED;
  896. sglq->ndlp = NULL;
  897. list_add_tail(&sglq->list,
  898. &phba->sli4_hba.lpfc_sgl_list);
  899. /* Check if TXQ queue needs to be serviced */
  900. if (pring->txq_cnt)
  901. lpfc_worker_wake_up(phba);
  902. }
  903. }
  904. /*
  905. * Clean all volatile data fields, preserve iotag and node struct.
  906. */
  907. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  908. iocbq->sli4_lxritag = NO_XRI;
  909. iocbq->sli4_xritag = NO_XRI;
  910. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  911. }
  912. /**
  913. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  914. * @phba: Pointer to HBA context object.
  915. * @iocbq: Pointer to driver iocb object.
  916. *
  917. * This function is called with hbalock held to release driver
  918. * iocb object to the iocb pool. The iotag in the iocb object
  919. * does not change for each use of the iocb object. This function
  920. * clears all other fields of the iocb object when it is freed.
  921. **/
  922. static void
  923. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  924. {
  925. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  926. /*
  927. * Clean all volatile data fields, preserve iotag and node struct.
  928. */
  929. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  930. iocbq->sli4_xritag = NO_XRI;
  931. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  932. }
  933. /**
  934. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  935. * @phba: Pointer to HBA context object.
  936. * @iocbq: Pointer to driver iocb object.
  937. *
  938. * This function is called with hbalock held to release driver
  939. * iocb object to the iocb pool. The iotag in the iocb object
  940. * does not change for each use of the iocb object. This function
  941. * clears all other fields of the iocb object when it is freed.
  942. **/
  943. static void
  944. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  945. {
  946. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  947. phba->iocb_cnt--;
  948. }
  949. /**
  950. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  951. * @phba: Pointer to HBA context object.
  952. * @iocbq: Pointer to driver iocb object.
  953. *
  954. * This function is called with no lock held to release the iocb to
  955. * iocb pool.
  956. **/
  957. void
  958. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  959. {
  960. unsigned long iflags;
  961. /*
  962. * Clean all volatile data fields, preserve iotag and node struct.
  963. */
  964. spin_lock_irqsave(&phba->hbalock, iflags);
  965. __lpfc_sli_release_iocbq(phba, iocbq);
  966. spin_unlock_irqrestore(&phba->hbalock, iflags);
  967. }
  968. /**
  969. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  970. * @phba: Pointer to HBA context object.
  971. * @iocblist: List of IOCBs.
  972. * @ulpstatus: ULP status in IOCB command field.
  973. * @ulpWord4: ULP word-4 in IOCB command field.
  974. *
  975. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  976. * on the list by invoking the complete callback function associated with the
  977. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  978. * fields.
  979. **/
  980. void
  981. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  982. uint32_t ulpstatus, uint32_t ulpWord4)
  983. {
  984. struct lpfc_iocbq *piocb;
  985. while (!list_empty(iocblist)) {
  986. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  987. if (!piocb->iocb_cmpl)
  988. lpfc_sli_release_iocbq(phba, piocb);
  989. else {
  990. piocb->iocb.ulpStatus = ulpstatus;
  991. piocb->iocb.un.ulpWord[4] = ulpWord4;
  992. (piocb->iocb_cmpl) (phba, piocb, piocb);
  993. }
  994. }
  995. return;
  996. }
  997. /**
  998. * lpfc_sli_iocb_cmd_type - Get the iocb type
  999. * @iocb_cmnd: iocb command code.
  1000. *
  1001. * This function is called by ring event handler function to get the iocb type.
  1002. * This function translates the iocb command to an iocb command type used to
  1003. * decide the final disposition of each completed IOCB.
  1004. * The function returns
  1005. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  1006. * LPFC_SOL_IOCB if it is a solicited iocb completion
  1007. * LPFC_ABORT_IOCB if it is an abort iocb
  1008. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  1009. *
  1010. * The caller is not required to hold any lock.
  1011. **/
  1012. static lpfc_iocb_type
  1013. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1014. {
  1015. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1016. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1017. return 0;
  1018. switch (iocb_cmnd) {
  1019. case CMD_XMIT_SEQUENCE_CR:
  1020. case CMD_XMIT_SEQUENCE_CX:
  1021. case CMD_XMIT_BCAST_CN:
  1022. case CMD_XMIT_BCAST_CX:
  1023. case CMD_ELS_REQUEST_CR:
  1024. case CMD_ELS_REQUEST_CX:
  1025. case CMD_CREATE_XRI_CR:
  1026. case CMD_CREATE_XRI_CX:
  1027. case CMD_GET_RPI_CN:
  1028. case CMD_XMIT_ELS_RSP_CX:
  1029. case CMD_GET_RPI_CR:
  1030. case CMD_FCP_IWRITE_CR:
  1031. case CMD_FCP_IWRITE_CX:
  1032. case CMD_FCP_IREAD_CR:
  1033. case CMD_FCP_IREAD_CX:
  1034. case CMD_FCP_ICMND_CR:
  1035. case CMD_FCP_ICMND_CX:
  1036. case CMD_FCP_TSEND_CX:
  1037. case CMD_FCP_TRSP_CX:
  1038. case CMD_FCP_TRECEIVE_CX:
  1039. case CMD_FCP_AUTO_TRSP_CX:
  1040. case CMD_ADAPTER_MSG:
  1041. case CMD_ADAPTER_DUMP:
  1042. case CMD_XMIT_SEQUENCE64_CR:
  1043. case CMD_XMIT_SEQUENCE64_CX:
  1044. case CMD_XMIT_BCAST64_CN:
  1045. case CMD_XMIT_BCAST64_CX:
  1046. case CMD_ELS_REQUEST64_CR:
  1047. case CMD_ELS_REQUEST64_CX:
  1048. case CMD_FCP_IWRITE64_CR:
  1049. case CMD_FCP_IWRITE64_CX:
  1050. case CMD_FCP_IREAD64_CR:
  1051. case CMD_FCP_IREAD64_CX:
  1052. case CMD_FCP_ICMND64_CR:
  1053. case CMD_FCP_ICMND64_CX:
  1054. case CMD_FCP_TSEND64_CX:
  1055. case CMD_FCP_TRSP64_CX:
  1056. case CMD_FCP_TRECEIVE64_CX:
  1057. case CMD_GEN_REQUEST64_CR:
  1058. case CMD_GEN_REQUEST64_CX:
  1059. case CMD_XMIT_ELS_RSP64_CX:
  1060. case DSSCMD_IWRITE64_CR:
  1061. case DSSCMD_IWRITE64_CX:
  1062. case DSSCMD_IREAD64_CR:
  1063. case DSSCMD_IREAD64_CX:
  1064. type = LPFC_SOL_IOCB;
  1065. break;
  1066. case CMD_ABORT_XRI_CN:
  1067. case CMD_ABORT_XRI_CX:
  1068. case CMD_CLOSE_XRI_CN:
  1069. case CMD_CLOSE_XRI_CX:
  1070. case CMD_XRI_ABORTED_CX:
  1071. case CMD_ABORT_MXRI64_CN:
  1072. case CMD_XMIT_BLS_RSP64_CX:
  1073. type = LPFC_ABORT_IOCB;
  1074. break;
  1075. case CMD_RCV_SEQUENCE_CX:
  1076. case CMD_RCV_ELS_REQ_CX:
  1077. case CMD_RCV_SEQUENCE64_CX:
  1078. case CMD_RCV_ELS_REQ64_CX:
  1079. case CMD_ASYNC_STATUS:
  1080. case CMD_IOCB_RCV_SEQ64_CX:
  1081. case CMD_IOCB_RCV_ELS64_CX:
  1082. case CMD_IOCB_RCV_CONT64_CX:
  1083. case CMD_IOCB_RET_XRI64_CX:
  1084. type = LPFC_UNSOL_IOCB;
  1085. break;
  1086. case CMD_IOCB_XMIT_MSEQ64_CR:
  1087. case CMD_IOCB_XMIT_MSEQ64_CX:
  1088. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1089. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1090. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1091. case CMD_IOCB_ABORT_EXTENDED_CN:
  1092. case CMD_IOCB_RET_HBQE64_CN:
  1093. case CMD_IOCB_FCP_IBIDIR64_CR:
  1094. case CMD_IOCB_FCP_IBIDIR64_CX:
  1095. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1096. case CMD_IOCB_LOGENTRY_CN:
  1097. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1098. printk("%s - Unhandled SLI-3 Command x%x\n",
  1099. __func__, iocb_cmnd);
  1100. type = LPFC_UNKNOWN_IOCB;
  1101. break;
  1102. default:
  1103. type = LPFC_UNKNOWN_IOCB;
  1104. break;
  1105. }
  1106. return type;
  1107. }
  1108. /**
  1109. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1110. * @phba: Pointer to HBA context object.
  1111. *
  1112. * This function is called from SLI initialization code
  1113. * to configure every ring of the HBA's SLI interface. The
  1114. * caller is not required to hold any lock. This function issues
  1115. * a config_ring mailbox command for each ring.
  1116. * This function returns zero if successful else returns a negative
  1117. * error code.
  1118. **/
  1119. static int
  1120. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1121. {
  1122. struct lpfc_sli *psli = &phba->sli;
  1123. LPFC_MBOXQ_t *pmb;
  1124. MAILBOX_t *pmbox;
  1125. int i, rc, ret = 0;
  1126. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1127. if (!pmb)
  1128. return -ENOMEM;
  1129. pmbox = &pmb->u.mb;
  1130. phba->link_state = LPFC_INIT_MBX_CMDS;
  1131. for (i = 0; i < psli->num_rings; i++) {
  1132. lpfc_config_ring(phba, i, pmb);
  1133. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1134. if (rc != MBX_SUCCESS) {
  1135. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1136. "0446 Adapter failed to init (%d), "
  1137. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1138. "ring %d\n",
  1139. rc, pmbox->mbxCommand,
  1140. pmbox->mbxStatus, i);
  1141. phba->link_state = LPFC_HBA_ERROR;
  1142. ret = -ENXIO;
  1143. break;
  1144. }
  1145. }
  1146. mempool_free(pmb, phba->mbox_mem_pool);
  1147. return ret;
  1148. }
  1149. /**
  1150. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1151. * @phba: Pointer to HBA context object.
  1152. * @pring: Pointer to driver SLI ring object.
  1153. * @piocb: Pointer to the driver iocb object.
  1154. *
  1155. * This function is called with hbalock held. The function adds the
  1156. * new iocb to txcmplq of the given ring. This function always returns
  1157. * 0. If this function is called for ELS ring, this function checks if
  1158. * there is a vport associated with the ELS command. This function also
  1159. * starts els_tmofunc timer if this is an ELS command.
  1160. **/
  1161. static int
  1162. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1163. struct lpfc_iocbq *piocb)
  1164. {
  1165. list_add_tail(&piocb->list, &pring->txcmplq);
  1166. piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
  1167. pring->txcmplq_cnt++;
  1168. if (pring->txcmplq_cnt > pring->txcmplq_max)
  1169. pring->txcmplq_max = pring->txcmplq_cnt;
  1170. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1171. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1172. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1173. if (!piocb->vport)
  1174. BUG();
  1175. else
  1176. mod_timer(&piocb->vport->els_tmofunc,
  1177. jiffies + HZ * (phba->fc_ratov << 1));
  1178. }
  1179. return 0;
  1180. }
  1181. /**
  1182. * lpfc_sli_ringtx_get - Get first element of the txq
  1183. * @phba: Pointer to HBA context object.
  1184. * @pring: Pointer to driver SLI ring object.
  1185. *
  1186. * This function is called with hbalock held to get next
  1187. * iocb in txq of the given ring. If there is any iocb in
  1188. * the txq, the function returns first iocb in the list after
  1189. * removing the iocb from the list, else it returns NULL.
  1190. **/
  1191. struct lpfc_iocbq *
  1192. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1193. {
  1194. struct lpfc_iocbq *cmd_iocb;
  1195. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1196. if (cmd_iocb != NULL)
  1197. pring->txq_cnt--;
  1198. return cmd_iocb;
  1199. }
  1200. /**
  1201. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1202. * @phba: Pointer to HBA context object.
  1203. * @pring: Pointer to driver SLI ring object.
  1204. *
  1205. * This function is called with hbalock held and the caller must post the
  1206. * iocb without releasing the lock. If the caller releases the lock,
  1207. * iocb slot returned by the function is not guaranteed to be available.
  1208. * The function returns pointer to the next available iocb slot if there
  1209. * is available slot in the ring, else it returns NULL.
  1210. * If the get index of the ring is ahead of the put index, the function
  1211. * will post an error attention event to the worker thread to take the
  1212. * HBA to offline state.
  1213. **/
  1214. static IOCB_t *
  1215. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1216. {
  1217. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1218. uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
  1219. if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
  1220. (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
  1221. pring->sli.sli3.next_cmdidx = 0;
  1222. if (unlikely(pring->sli.sli3.local_getidx ==
  1223. pring->sli.sli3.next_cmdidx)) {
  1224. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1225. if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
  1226. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1227. "0315 Ring %d issue: portCmdGet %d "
  1228. "is bigger than cmd ring %d\n",
  1229. pring->ringno,
  1230. pring->sli.sli3.local_getidx,
  1231. max_cmd_idx);
  1232. phba->link_state = LPFC_HBA_ERROR;
  1233. /*
  1234. * All error attention handlers are posted to
  1235. * worker thread
  1236. */
  1237. phba->work_ha |= HA_ERATT;
  1238. phba->work_hs = HS_FFER3;
  1239. lpfc_worker_wake_up(phba);
  1240. return NULL;
  1241. }
  1242. if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
  1243. return NULL;
  1244. }
  1245. return lpfc_cmd_iocb(phba, pring);
  1246. }
  1247. /**
  1248. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1249. * @phba: Pointer to HBA context object.
  1250. * @iocbq: Pointer to driver iocb object.
  1251. *
  1252. * This function gets an iotag for the iocb. If there is no unused iotag and
  1253. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1254. * array and assigns a new iotag.
  1255. * The function returns the allocated iotag if successful, else returns zero.
  1256. * Zero is not a valid iotag.
  1257. * The caller is not required to hold any lock.
  1258. **/
  1259. uint16_t
  1260. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1261. {
  1262. struct lpfc_iocbq **new_arr;
  1263. struct lpfc_iocbq **old_arr;
  1264. size_t new_len;
  1265. struct lpfc_sli *psli = &phba->sli;
  1266. uint16_t iotag;
  1267. spin_lock_irq(&phba->hbalock);
  1268. iotag = psli->last_iotag;
  1269. if(++iotag < psli->iocbq_lookup_len) {
  1270. psli->last_iotag = iotag;
  1271. psli->iocbq_lookup[iotag] = iocbq;
  1272. spin_unlock_irq(&phba->hbalock);
  1273. iocbq->iotag = iotag;
  1274. return iotag;
  1275. } else if (psli->iocbq_lookup_len < (0xffff
  1276. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1277. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1278. spin_unlock_irq(&phba->hbalock);
  1279. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1280. GFP_KERNEL);
  1281. if (new_arr) {
  1282. spin_lock_irq(&phba->hbalock);
  1283. old_arr = psli->iocbq_lookup;
  1284. if (new_len <= psli->iocbq_lookup_len) {
  1285. /* highly unprobable case */
  1286. kfree(new_arr);
  1287. iotag = psli->last_iotag;
  1288. if(++iotag < psli->iocbq_lookup_len) {
  1289. psli->last_iotag = iotag;
  1290. psli->iocbq_lookup[iotag] = iocbq;
  1291. spin_unlock_irq(&phba->hbalock);
  1292. iocbq->iotag = iotag;
  1293. return iotag;
  1294. }
  1295. spin_unlock_irq(&phba->hbalock);
  1296. return 0;
  1297. }
  1298. if (psli->iocbq_lookup)
  1299. memcpy(new_arr, old_arr,
  1300. ((psli->last_iotag + 1) *
  1301. sizeof (struct lpfc_iocbq *)));
  1302. psli->iocbq_lookup = new_arr;
  1303. psli->iocbq_lookup_len = new_len;
  1304. psli->last_iotag = iotag;
  1305. psli->iocbq_lookup[iotag] = iocbq;
  1306. spin_unlock_irq(&phba->hbalock);
  1307. iocbq->iotag = iotag;
  1308. kfree(old_arr);
  1309. return iotag;
  1310. }
  1311. } else
  1312. spin_unlock_irq(&phba->hbalock);
  1313. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1314. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1315. psli->last_iotag);
  1316. return 0;
  1317. }
  1318. /**
  1319. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1320. * @phba: Pointer to HBA context object.
  1321. * @pring: Pointer to driver SLI ring object.
  1322. * @iocb: Pointer to iocb slot in the ring.
  1323. * @nextiocb: Pointer to driver iocb object which need to be
  1324. * posted to firmware.
  1325. *
  1326. * This function is called with hbalock held to post a new iocb to
  1327. * the firmware. This function copies the new iocb to ring iocb slot and
  1328. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1329. * a completion call back for this iocb else the function will free the
  1330. * iocb object.
  1331. **/
  1332. static void
  1333. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1334. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1335. {
  1336. /*
  1337. * Set up an iotag
  1338. */
  1339. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1340. if (pring->ringno == LPFC_ELS_RING) {
  1341. lpfc_debugfs_slow_ring_trc(phba,
  1342. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1343. *(((uint32_t *) &nextiocb->iocb) + 4),
  1344. *(((uint32_t *) &nextiocb->iocb) + 6),
  1345. *(((uint32_t *) &nextiocb->iocb) + 7));
  1346. }
  1347. /*
  1348. * Issue iocb command to adapter
  1349. */
  1350. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1351. wmb();
  1352. pring->stats.iocb_cmd++;
  1353. /*
  1354. * If there is no completion routine to call, we can release the
  1355. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1356. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1357. */
  1358. if (nextiocb->iocb_cmpl)
  1359. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1360. else
  1361. __lpfc_sli_release_iocbq(phba, nextiocb);
  1362. /*
  1363. * Let the HBA know what IOCB slot will be the next one the
  1364. * driver will put a command into.
  1365. */
  1366. pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
  1367. writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1368. }
  1369. /**
  1370. * lpfc_sli_update_full_ring - Update the chip attention register
  1371. * @phba: Pointer to HBA context object.
  1372. * @pring: Pointer to driver SLI ring object.
  1373. *
  1374. * The caller is not required to hold any lock for calling this function.
  1375. * This function updates the chip attention bits for the ring to inform firmware
  1376. * that there are pending work to be done for this ring and requests an
  1377. * interrupt when there is space available in the ring. This function is
  1378. * called when the driver is unable to post more iocbs to the ring due
  1379. * to unavailability of space in the ring.
  1380. **/
  1381. static void
  1382. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1383. {
  1384. int ringno = pring->ringno;
  1385. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1386. wmb();
  1387. /*
  1388. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1389. * The HBA will tell us when an IOCB entry is available.
  1390. */
  1391. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1392. readl(phba->CAregaddr); /* flush */
  1393. pring->stats.iocb_cmd_full++;
  1394. }
  1395. /**
  1396. * lpfc_sli_update_ring - Update chip attention register
  1397. * @phba: Pointer to HBA context object.
  1398. * @pring: Pointer to driver SLI ring object.
  1399. *
  1400. * This function updates the chip attention register bit for the
  1401. * given ring to inform HBA that there is more work to be done
  1402. * in this ring. The caller is not required to hold any lock.
  1403. **/
  1404. static void
  1405. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1406. {
  1407. int ringno = pring->ringno;
  1408. /*
  1409. * Tell the HBA that there is work to do in this ring.
  1410. */
  1411. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1412. wmb();
  1413. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1414. readl(phba->CAregaddr); /* flush */
  1415. }
  1416. }
  1417. /**
  1418. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1419. * @phba: Pointer to HBA context object.
  1420. * @pring: Pointer to driver SLI ring object.
  1421. *
  1422. * This function is called with hbalock held to post pending iocbs
  1423. * in the txq to the firmware. This function is called when driver
  1424. * detects space available in the ring.
  1425. **/
  1426. static void
  1427. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1428. {
  1429. IOCB_t *iocb;
  1430. struct lpfc_iocbq *nextiocb;
  1431. /*
  1432. * Check to see if:
  1433. * (a) there is anything on the txq to send
  1434. * (b) link is up
  1435. * (c) link attention events can be processed (fcp ring only)
  1436. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1437. */
  1438. if (pring->txq_cnt &&
  1439. lpfc_is_link_up(phba) &&
  1440. (pring->ringno != phba->sli.fcp_ring ||
  1441. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1442. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1443. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1444. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1445. if (iocb)
  1446. lpfc_sli_update_ring(phba, pring);
  1447. else
  1448. lpfc_sli_update_full_ring(phba, pring);
  1449. }
  1450. return;
  1451. }
  1452. /**
  1453. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1454. * @phba: Pointer to HBA context object.
  1455. * @hbqno: HBQ number.
  1456. *
  1457. * This function is called with hbalock held to get the next
  1458. * available slot for the given HBQ. If there is free slot
  1459. * available for the HBQ it will return pointer to the next available
  1460. * HBQ entry else it will return NULL.
  1461. **/
  1462. static struct lpfc_hbq_entry *
  1463. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1464. {
  1465. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1466. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1467. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1468. hbqp->next_hbqPutIdx = 0;
  1469. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1470. uint32_t raw_index = phba->hbq_get[hbqno];
  1471. uint32_t getidx = le32_to_cpu(raw_index);
  1472. hbqp->local_hbqGetIdx = getidx;
  1473. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1474. lpfc_printf_log(phba, KERN_ERR,
  1475. LOG_SLI | LOG_VPORT,
  1476. "1802 HBQ %d: local_hbqGetIdx "
  1477. "%u is > than hbqp->entry_count %u\n",
  1478. hbqno, hbqp->local_hbqGetIdx,
  1479. hbqp->entry_count);
  1480. phba->link_state = LPFC_HBA_ERROR;
  1481. return NULL;
  1482. }
  1483. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1484. return NULL;
  1485. }
  1486. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1487. hbqp->hbqPutIdx;
  1488. }
  1489. /**
  1490. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1491. * @phba: Pointer to HBA context object.
  1492. *
  1493. * This function is called with no lock held to free all the
  1494. * hbq buffers while uninitializing the SLI interface. It also
  1495. * frees the HBQ buffers returned by the firmware but not yet
  1496. * processed by the upper layers.
  1497. **/
  1498. void
  1499. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1500. {
  1501. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1502. struct hbq_dmabuf *hbq_buf;
  1503. unsigned long flags;
  1504. int i, hbq_count;
  1505. uint32_t hbqno;
  1506. hbq_count = lpfc_sli_hbq_count();
  1507. /* Return all memory used by all HBQs */
  1508. spin_lock_irqsave(&phba->hbalock, flags);
  1509. for (i = 0; i < hbq_count; ++i) {
  1510. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1511. &phba->hbqs[i].hbq_buffer_list, list) {
  1512. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1513. list_del(&hbq_buf->dbuf.list);
  1514. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1515. }
  1516. phba->hbqs[i].buffer_count = 0;
  1517. }
  1518. /* Return all HBQ buffer that are in-fly */
  1519. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1520. list) {
  1521. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1522. list_del(&hbq_buf->dbuf.list);
  1523. if (hbq_buf->tag == -1) {
  1524. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1525. (phba, hbq_buf);
  1526. } else {
  1527. hbqno = hbq_buf->tag >> 16;
  1528. if (hbqno >= LPFC_MAX_HBQS)
  1529. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1530. (phba, hbq_buf);
  1531. else
  1532. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1533. hbq_buf);
  1534. }
  1535. }
  1536. /* Mark the HBQs not in use */
  1537. phba->hbq_in_use = 0;
  1538. spin_unlock_irqrestore(&phba->hbalock, flags);
  1539. }
  1540. /**
  1541. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1542. * @phba: Pointer to HBA context object.
  1543. * @hbqno: HBQ number.
  1544. * @hbq_buf: Pointer to HBQ buffer.
  1545. *
  1546. * This function is called with the hbalock held to post a
  1547. * hbq buffer to the firmware. If the function finds an empty
  1548. * slot in the HBQ, it will post the buffer. The function will return
  1549. * pointer to the hbq entry if it successfully post the buffer
  1550. * else it will return NULL.
  1551. **/
  1552. static int
  1553. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1554. struct hbq_dmabuf *hbq_buf)
  1555. {
  1556. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1557. }
  1558. /**
  1559. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1560. * @phba: Pointer to HBA context object.
  1561. * @hbqno: HBQ number.
  1562. * @hbq_buf: Pointer to HBQ buffer.
  1563. *
  1564. * This function is called with the hbalock held to post a hbq buffer to the
  1565. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1566. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1567. * it successfully post the buffer else it will return an error.
  1568. **/
  1569. static int
  1570. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1571. struct hbq_dmabuf *hbq_buf)
  1572. {
  1573. struct lpfc_hbq_entry *hbqe;
  1574. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1575. /* Get next HBQ entry slot to use */
  1576. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1577. if (hbqe) {
  1578. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1579. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1580. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1581. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1582. hbqe->bde.tus.f.bdeFlags = 0;
  1583. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1584. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1585. /* Sync SLIM */
  1586. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1587. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1588. /* flush */
  1589. readl(phba->hbq_put + hbqno);
  1590. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1591. return 0;
  1592. } else
  1593. return -ENOMEM;
  1594. }
  1595. /**
  1596. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1597. * @phba: Pointer to HBA context object.
  1598. * @hbqno: HBQ number.
  1599. * @hbq_buf: Pointer to HBQ buffer.
  1600. *
  1601. * This function is called with the hbalock held to post an RQE to the SLI4
  1602. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1603. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1604. **/
  1605. static int
  1606. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1607. struct hbq_dmabuf *hbq_buf)
  1608. {
  1609. int rc;
  1610. struct lpfc_rqe hrqe;
  1611. struct lpfc_rqe drqe;
  1612. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1613. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1614. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1615. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1616. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1617. &hrqe, &drqe);
  1618. if (rc < 0)
  1619. return rc;
  1620. hbq_buf->tag = rc;
  1621. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1622. return 0;
  1623. }
  1624. /* HBQ for ELS and CT traffic. */
  1625. static struct lpfc_hbq_init lpfc_els_hbq = {
  1626. .rn = 1,
  1627. .entry_count = 256,
  1628. .mask_count = 0,
  1629. .profile = 0,
  1630. .ring_mask = (1 << LPFC_ELS_RING),
  1631. .buffer_count = 0,
  1632. .init_count = 40,
  1633. .add_count = 40,
  1634. };
  1635. /* HBQ for the extra ring if needed */
  1636. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1637. .rn = 1,
  1638. .entry_count = 200,
  1639. .mask_count = 0,
  1640. .profile = 0,
  1641. .ring_mask = (1 << LPFC_EXTRA_RING),
  1642. .buffer_count = 0,
  1643. .init_count = 0,
  1644. .add_count = 5,
  1645. };
  1646. /* Array of HBQs */
  1647. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1648. &lpfc_els_hbq,
  1649. &lpfc_extra_hbq,
  1650. };
  1651. /**
  1652. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1653. * @phba: Pointer to HBA context object.
  1654. * @hbqno: HBQ number.
  1655. * @count: Number of HBQ buffers to be posted.
  1656. *
  1657. * This function is called with no lock held to post more hbq buffers to the
  1658. * given HBQ. The function returns the number of HBQ buffers successfully
  1659. * posted.
  1660. **/
  1661. static int
  1662. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1663. {
  1664. uint32_t i, posted = 0;
  1665. unsigned long flags;
  1666. struct hbq_dmabuf *hbq_buffer;
  1667. LIST_HEAD(hbq_buf_list);
  1668. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1669. return 0;
  1670. if ((phba->hbqs[hbqno].buffer_count + count) >
  1671. lpfc_hbq_defs[hbqno]->entry_count)
  1672. count = lpfc_hbq_defs[hbqno]->entry_count -
  1673. phba->hbqs[hbqno].buffer_count;
  1674. if (!count)
  1675. return 0;
  1676. /* Allocate HBQ entries */
  1677. for (i = 0; i < count; i++) {
  1678. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1679. if (!hbq_buffer)
  1680. break;
  1681. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1682. }
  1683. /* Check whether HBQ is still in use */
  1684. spin_lock_irqsave(&phba->hbalock, flags);
  1685. if (!phba->hbq_in_use)
  1686. goto err;
  1687. while (!list_empty(&hbq_buf_list)) {
  1688. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1689. dbuf.list);
  1690. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1691. (hbqno << 16));
  1692. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1693. phba->hbqs[hbqno].buffer_count++;
  1694. posted++;
  1695. } else
  1696. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1697. }
  1698. spin_unlock_irqrestore(&phba->hbalock, flags);
  1699. return posted;
  1700. err:
  1701. spin_unlock_irqrestore(&phba->hbalock, flags);
  1702. while (!list_empty(&hbq_buf_list)) {
  1703. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1704. dbuf.list);
  1705. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1706. }
  1707. return 0;
  1708. }
  1709. /**
  1710. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1711. * @phba: Pointer to HBA context object.
  1712. * @qno: HBQ number.
  1713. *
  1714. * This function posts more buffers to the HBQ. This function
  1715. * is called with no lock held. The function returns the number of HBQ entries
  1716. * successfully allocated.
  1717. **/
  1718. int
  1719. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1720. {
  1721. if (phba->sli_rev == LPFC_SLI_REV4)
  1722. return 0;
  1723. else
  1724. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1725. lpfc_hbq_defs[qno]->add_count);
  1726. }
  1727. /**
  1728. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1729. * @phba: Pointer to HBA context object.
  1730. * @qno: HBQ queue number.
  1731. *
  1732. * This function is called from SLI initialization code path with
  1733. * no lock held to post initial HBQ buffers to firmware. The
  1734. * function returns the number of HBQ entries successfully allocated.
  1735. **/
  1736. static int
  1737. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1738. {
  1739. if (phba->sli_rev == LPFC_SLI_REV4)
  1740. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1741. lpfc_hbq_defs[qno]->entry_count);
  1742. else
  1743. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1744. lpfc_hbq_defs[qno]->init_count);
  1745. }
  1746. /**
  1747. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1748. * @phba: Pointer to HBA context object.
  1749. * @hbqno: HBQ number.
  1750. *
  1751. * This function removes the first hbq buffer on an hbq list and returns a
  1752. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1753. **/
  1754. static struct hbq_dmabuf *
  1755. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1756. {
  1757. struct lpfc_dmabuf *d_buf;
  1758. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1759. if (!d_buf)
  1760. return NULL;
  1761. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1762. }
  1763. /**
  1764. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1765. * @phba: Pointer to HBA context object.
  1766. * @tag: Tag of the hbq buffer.
  1767. *
  1768. * This function is called with hbalock held. This function searches
  1769. * for the hbq buffer associated with the given tag in the hbq buffer
  1770. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1771. * it returns NULL.
  1772. **/
  1773. static struct hbq_dmabuf *
  1774. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1775. {
  1776. struct lpfc_dmabuf *d_buf;
  1777. struct hbq_dmabuf *hbq_buf;
  1778. uint32_t hbqno;
  1779. hbqno = tag >> 16;
  1780. if (hbqno >= LPFC_MAX_HBQS)
  1781. return NULL;
  1782. spin_lock_irq(&phba->hbalock);
  1783. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1784. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1785. if (hbq_buf->tag == tag) {
  1786. spin_unlock_irq(&phba->hbalock);
  1787. return hbq_buf;
  1788. }
  1789. }
  1790. spin_unlock_irq(&phba->hbalock);
  1791. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1792. "1803 Bad hbq tag. Data: x%x x%x\n",
  1793. tag, phba->hbqs[tag >> 16].buffer_count);
  1794. return NULL;
  1795. }
  1796. /**
  1797. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1798. * @phba: Pointer to HBA context object.
  1799. * @hbq_buffer: Pointer to HBQ buffer.
  1800. *
  1801. * This function is called with hbalock. This function gives back
  1802. * the hbq buffer to firmware. If the HBQ does not have space to
  1803. * post the buffer, it will free the buffer.
  1804. **/
  1805. void
  1806. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1807. {
  1808. uint32_t hbqno;
  1809. if (hbq_buffer) {
  1810. hbqno = hbq_buffer->tag >> 16;
  1811. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1812. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1813. }
  1814. }
  1815. /**
  1816. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1817. * @mbxCommand: mailbox command code.
  1818. *
  1819. * This function is called by the mailbox event handler function to verify
  1820. * that the completed mailbox command is a legitimate mailbox command. If the
  1821. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1822. * and the mailbox event handler will take the HBA offline.
  1823. **/
  1824. static int
  1825. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1826. {
  1827. uint8_t ret;
  1828. switch (mbxCommand) {
  1829. case MBX_LOAD_SM:
  1830. case MBX_READ_NV:
  1831. case MBX_WRITE_NV:
  1832. case MBX_WRITE_VPARMS:
  1833. case MBX_RUN_BIU_DIAG:
  1834. case MBX_INIT_LINK:
  1835. case MBX_DOWN_LINK:
  1836. case MBX_CONFIG_LINK:
  1837. case MBX_CONFIG_RING:
  1838. case MBX_RESET_RING:
  1839. case MBX_READ_CONFIG:
  1840. case MBX_READ_RCONFIG:
  1841. case MBX_READ_SPARM:
  1842. case MBX_READ_STATUS:
  1843. case MBX_READ_RPI:
  1844. case MBX_READ_XRI:
  1845. case MBX_READ_REV:
  1846. case MBX_READ_LNK_STAT:
  1847. case MBX_REG_LOGIN:
  1848. case MBX_UNREG_LOGIN:
  1849. case MBX_CLEAR_LA:
  1850. case MBX_DUMP_MEMORY:
  1851. case MBX_DUMP_CONTEXT:
  1852. case MBX_RUN_DIAGS:
  1853. case MBX_RESTART:
  1854. case MBX_UPDATE_CFG:
  1855. case MBX_DOWN_LOAD:
  1856. case MBX_DEL_LD_ENTRY:
  1857. case MBX_RUN_PROGRAM:
  1858. case MBX_SET_MASK:
  1859. case MBX_SET_VARIABLE:
  1860. case MBX_UNREG_D_ID:
  1861. case MBX_KILL_BOARD:
  1862. case MBX_CONFIG_FARP:
  1863. case MBX_BEACON:
  1864. case MBX_LOAD_AREA:
  1865. case MBX_RUN_BIU_DIAG64:
  1866. case MBX_CONFIG_PORT:
  1867. case MBX_READ_SPARM64:
  1868. case MBX_READ_RPI64:
  1869. case MBX_REG_LOGIN64:
  1870. case MBX_READ_TOPOLOGY:
  1871. case MBX_WRITE_WWN:
  1872. case MBX_SET_DEBUG:
  1873. case MBX_LOAD_EXP_ROM:
  1874. case MBX_ASYNCEVT_ENABLE:
  1875. case MBX_REG_VPI:
  1876. case MBX_UNREG_VPI:
  1877. case MBX_HEARTBEAT:
  1878. case MBX_PORT_CAPABILITIES:
  1879. case MBX_PORT_IOV_CONTROL:
  1880. case MBX_SLI4_CONFIG:
  1881. case MBX_SLI4_REQ_FTRS:
  1882. case MBX_REG_FCFI:
  1883. case MBX_UNREG_FCFI:
  1884. case MBX_REG_VFI:
  1885. case MBX_UNREG_VFI:
  1886. case MBX_INIT_VPI:
  1887. case MBX_INIT_VFI:
  1888. case MBX_RESUME_RPI:
  1889. case MBX_READ_EVENT_LOG_STATUS:
  1890. case MBX_READ_EVENT_LOG:
  1891. case MBX_SECURITY_MGMT:
  1892. case MBX_AUTH_PORT:
  1893. ret = mbxCommand;
  1894. break;
  1895. default:
  1896. ret = MBX_SHUTDOWN;
  1897. break;
  1898. }
  1899. return ret;
  1900. }
  1901. /**
  1902. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1903. * @phba: Pointer to HBA context object.
  1904. * @pmboxq: Pointer to mailbox command.
  1905. *
  1906. * This is completion handler function for mailbox commands issued from
  1907. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1908. * mailbox event handler function with no lock held. This function
  1909. * will wake up thread waiting on the wait queue pointed by context1
  1910. * of the mailbox.
  1911. **/
  1912. void
  1913. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1914. {
  1915. wait_queue_head_t *pdone_q;
  1916. unsigned long drvr_flag;
  1917. /*
  1918. * If pdone_q is empty, the driver thread gave up waiting and
  1919. * continued running.
  1920. */
  1921. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1922. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1923. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1924. if (pdone_q)
  1925. wake_up_interruptible(pdone_q);
  1926. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1927. return;
  1928. }
  1929. /**
  1930. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1931. * @phba: Pointer to HBA context object.
  1932. * @pmb: Pointer to mailbox object.
  1933. *
  1934. * This function is the default mailbox completion handler. It
  1935. * frees the memory resources associated with the completed mailbox
  1936. * command. If the completed command is a REG_LOGIN mailbox command,
  1937. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1938. **/
  1939. void
  1940. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1941. {
  1942. struct lpfc_vport *vport = pmb->vport;
  1943. struct lpfc_dmabuf *mp;
  1944. struct lpfc_nodelist *ndlp;
  1945. struct Scsi_Host *shost;
  1946. uint16_t rpi, vpi;
  1947. int rc;
  1948. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1949. if (mp) {
  1950. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1951. kfree(mp);
  1952. }
  1953. /*
  1954. * If a REG_LOGIN succeeded after node is destroyed or node
  1955. * is in re-discovery driver need to cleanup the RPI.
  1956. */
  1957. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1958. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1959. !pmb->u.mb.mbxStatus) {
  1960. rpi = pmb->u.mb.un.varWords[0];
  1961. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1962. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1963. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1964. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1965. if (rc != MBX_NOT_FINISHED)
  1966. return;
  1967. }
  1968. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  1969. !(phba->pport->load_flag & FC_UNLOADING) &&
  1970. !pmb->u.mb.mbxStatus) {
  1971. shost = lpfc_shost_from_vport(vport);
  1972. spin_lock_irq(shost->host_lock);
  1973. vport->vpi_state |= LPFC_VPI_REGISTERED;
  1974. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  1975. spin_unlock_irq(shost->host_lock);
  1976. }
  1977. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  1978. ndlp = (struct lpfc_nodelist *)pmb->context2;
  1979. lpfc_nlp_put(ndlp);
  1980. pmb->context2 = NULL;
  1981. }
  1982. /* Check security permission status on INIT_LINK mailbox command */
  1983. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  1984. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  1985. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1986. "2860 SLI authentication is required "
  1987. "for INIT_LINK but has not done yet\n");
  1988. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  1989. lpfc_sli4_mbox_cmd_free(phba, pmb);
  1990. else
  1991. mempool_free(pmb, phba->mbox_mem_pool);
  1992. }
  1993. /**
  1994. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  1995. * @phba: Pointer to HBA context object.
  1996. *
  1997. * This function is called with no lock held. This function processes all
  1998. * the completed mailbox commands and gives it to upper layers. The interrupt
  1999. * service routine processes mailbox completion interrupt and adds completed
  2000. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  2001. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  2002. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  2003. * function returns the mailbox commands to the upper layer by calling the
  2004. * completion handler function of each mailbox.
  2005. **/
  2006. int
  2007. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  2008. {
  2009. MAILBOX_t *pmbox;
  2010. LPFC_MBOXQ_t *pmb;
  2011. int rc;
  2012. LIST_HEAD(cmplq);
  2013. phba->sli.slistat.mbox_event++;
  2014. /* Get all completed mailboxe buffers into the cmplq */
  2015. spin_lock_irq(&phba->hbalock);
  2016. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2017. spin_unlock_irq(&phba->hbalock);
  2018. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2019. do {
  2020. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2021. if (pmb == NULL)
  2022. break;
  2023. pmbox = &pmb->u.mb;
  2024. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2025. if (pmb->vport) {
  2026. lpfc_debugfs_disc_trc(pmb->vport,
  2027. LPFC_DISC_TRC_MBOX_VPORT,
  2028. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2029. (uint32_t)pmbox->mbxCommand,
  2030. pmbox->un.varWords[0],
  2031. pmbox->un.varWords[1]);
  2032. }
  2033. else {
  2034. lpfc_debugfs_disc_trc(phba->pport,
  2035. LPFC_DISC_TRC_MBOX,
  2036. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2037. (uint32_t)pmbox->mbxCommand,
  2038. pmbox->un.varWords[0],
  2039. pmbox->un.varWords[1]);
  2040. }
  2041. }
  2042. /*
  2043. * It is a fatal error if unknown mbox command completion.
  2044. */
  2045. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2046. MBX_SHUTDOWN) {
  2047. /* Unknown mailbox command compl */
  2048. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2049. "(%d):0323 Unknown Mailbox command "
  2050. "x%x (x%x/x%x) Cmpl\n",
  2051. pmb->vport ? pmb->vport->vpi : 0,
  2052. pmbox->mbxCommand,
  2053. lpfc_sli_config_mbox_subsys_get(phba,
  2054. pmb),
  2055. lpfc_sli_config_mbox_opcode_get(phba,
  2056. pmb));
  2057. phba->link_state = LPFC_HBA_ERROR;
  2058. phba->work_hs = HS_FFER3;
  2059. lpfc_handle_eratt(phba);
  2060. continue;
  2061. }
  2062. if (pmbox->mbxStatus) {
  2063. phba->sli.slistat.mbox_stat_err++;
  2064. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2065. /* Mbox cmd cmpl error - RETRYing */
  2066. lpfc_printf_log(phba, KERN_INFO,
  2067. LOG_MBOX | LOG_SLI,
  2068. "(%d):0305 Mbox cmd cmpl "
  2069. "error - RETRYing Data: x%x "
  2070. "(x%x/x%x) x%x x%x x%x\n",
  2071. pmb->vport ? pmb->vport->vpi : 0,
  2072. pmbox->mbxCommand,
  2073. lpfc_sli_config_mbox_subsys_get(phba,
  2074. pmb),
  2075. lpfc_sli_config_mbox_opcode_get(phba,
  2076. pmb),
  2077. pmbox->mbxStatus,
  2078. pmbox->un.varWords[0],
  2079. pmb->vport->port_state);
  2080. pmbox->mbxStatus = 0;
  2081. pmbox->mbxOwner = OWN_HOST;
  2082. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2083. if (rc != MBX_NOT_FINISHED)
  2084. continue;
  2085. }
  2086. }
  2087. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2088. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2089. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2090. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2091. pmb->vport ? pmb->vport->vpi : 0,
  2092. pmbox->mbxCommand,
  2093. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2094. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2095. pmb->mbox_cmpl,
  2096. *((uint32_t *) pmbox),
  2097. pmbox->un.varWords[0],
  2098. pmbox->un.varWords[1],
  2099. pmbox->un.varWords[2],
  2100. pmbox->un.varWords[3],
  2101. pmbox->un.varWords[4],
  2102. pmbox->un.varWords[5],
  2103. pmbox->un.varWords[6],
  2104. pmbox->un.varWords[7]);
  2105. if (pmb->mbox_cmpl)
  2106. pmb->mbox_cmpl(phba,pmb);
  2107. } while (1);
  2108. return 0;
  2109. }
  2110. /**
  2111. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2112. * @phba: Pointer to HBA context object.
  2113. * @pring: Pointer to driver SLI ring object.
  2114. * @tag: buffer tag.
  2115. *
  2116. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2117. * is set in the tag the buffer is posted for a particular exchange,
  2118. * the function will return the buffer without replacing the buffer.
  2119. * If the buffer is for unsolicited ELS or CT traffic, this function
  2120. * returns the buffer and also posts another buffer to the firmware.
  2121. **/
  2122. static struct lpfc_dmabuf *
  2123. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2124. struct lpfc_sli_ring *pring,
  2125. uint32_t tag)
  2126. {
  2127. struct hbq_dmabuf *hbq_entry;
  2128. if (tag & QUE_BUFTAG_BIT)
  2129. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2130. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2131. if (!hbq_entry)
  2132. return NULL;
  2133. return &hbq_entry->dbuf;
  2134. }
  2135. /**
  2136. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2137. * @phba: Pointer to HBA context object.
  2138. * @pring: Pointer to driver SLI ring object.
  2139. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2140. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2141. * @fch_type: the type for the first frame of the sequence.
  2142. *
  2143. * This function is called with no lock held. This function uses the r_ctl and
  2144. * type of the received sequence to find the correct callback function to call
  2145. * to process the sequence.
  2146. **/
  2147. static int
  2148. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2149. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2150. uint32_t fch_type)
  2151. {
  2152. int i;
  2153. /* unSolicited Responses */
  2154. if (pring->prt[0].profile) {
  2155. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2156. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2157. saveq);
  2158. return 1;
  2159. }
  2160. /* We must search, based on rctl / type
  2161. for the right routine */
  2162. for (i = 0; i < pring->num_mask; i++) {
  2163. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2164. (pring->prt[i].type == fch_type)) {
  2165. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2166. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2167. (phba, pring, saveq);
  2168. return 1;
  2169. }
  2170. }
  2171. return 0;
  2172. }
  2173. /**
  2174. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2175. * @phba: Pointer to HBA context object.
  2176. * @pring: Pointer to driver SLI ring object.
  2177. * @saveq: Pointer to the unsolicited iocb.
  2178. *
  2179. * This function is called with no lock held by the ring event handler
  2180. * when there is an unsolicited iocb posted to the response ring by the
  2181. * firmware. This function gets the buffer associated with the iocbs
  2182. * and calls the event handler for the ring. This function handles both
  2183. * qring buffers and hbq buffers.
  2184. * When the function returns 1 the caller can free the iocb object otherwise
  2185. * upper layer functions will free the iocb objects.
  2186. **/
  2187. static int
  2188. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2189. struct lpfc_iocbq *saveq)
  2190. {
  2191. IOCB_t * irsp;
  2192. WORD5 * w5p;
  2193. uint32_t Rctl, Type;
  2194. uint32_t match;
  2195. struct lpfc_iocbq *iocbq;
  2196. struct lpfc_dmabuf *dmzbuf;
  2197. match = 0;
  2198. irsp = &(saveq->iocb);
  2199. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2200. if (pring->lpfc_sli_rcv_async_status)
  2201. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2202. else
  2203. lpfc_printf_log(phba,
  2204. KERN_WARNING,
  2205. LOG_SLI,
  2206. "0316 Ring %d handler: unexpected "
  2207. "ASYNC_STATUS iocb received evt_code "
  2208. "0x%x\n",
  2209. pring->ringno,
  2210. irsp->un.asyncstat.evt_code);
  2211. return 1;
  2212. }
  2213. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2214. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2215. if (irsp->ulpBdeCount > 0) {
  2216. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2217. irsp->un.ulpWord[3]);
  2218. lpfc_in_buf_free(phba, dmzbuf);
  2219. }
  2220. if (irsp->ulpBdeCount > 1) {
  2221. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2222. irsp->unsli3.sli3Words[3]);
  2223. lpfc_in_buf_free(phba, dmzbuf);
  2224. }
  2225. if (irsp->ulpBdeCount > 2) {
  2226. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2227. irsp->unsli3.sli3Words[7]);
  2228. lpfc_in_buf_free(phba, dmzbuf);
  2229. }
  2230. return 1;
  2231. }
  2232. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2233. if (irsp->ulpBdeCount != 0) {
  2234. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2235. irsp->un.ulpWord[3]);
  2236. if (!saveq->context2)
  2237. lpfc_printf_log(phba,
  2238. KERN_ERR,
  2239. LOG_SLI,
  2240. "0341 Ring %d Cannot find buffer for "
  2241. "an unsolicited iocb. tag 0x%x\n",
  2242. pring->ringno,
  2243. irsp->un.ulpWord[3]);
  2244. }
  2245. if (irsp->ulpBdeCount == 2) {
  2246. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2247. irsp->unsli3.sli3Words[7]);
  2248. if (!saveq->context3)
  2249. lpfc_printf_log(phba,
  2250. KERN_ERR,
  2251. LOG_SLI,
  2252. "0342 Ring %d Cannot find buffer for an"
  2253. " unsolicited iocb. tag 0x%x\n",
  2254. pring->ringno,
  2255. irsp->unsli3.sli3Words[7]);
  2256. }
  2257. list_for_each_entry(iocbq, &saveq->list, list) {
  2258. irsp = &(iocbq->iocb);
  2259. if (irsp->ulpBdeCount != 0) {
  2260. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2261. irsp->un.ulpWord[3]);
  2262. if (!iocbq->context2)
  2263. lpfc_printf_log(phba,
  2264. KERN_ERR,
  2265. LOG_SLI,
  2266. "0343 Ring %d Cannot find "
  2267. "buffer for an unsolicited iocb"
  2268. ". tag 0x%x\n", pring->ringno,
  2269. irsp->un.ulpWord[3]);
  2270. }
  2271. if (irsp->ulpBdeCount == 2) {
  2272. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2273. irsp->unsli3.sli3Words[7]);
  2274. if (!iocbq->context3)
  2275. lpfc_printf_log(phba,
  2276. KERN_ERR,
  2277. LOG_SLI,
  2278. "0344 Ring %d Cannot find "
  2279. "buffer for an unsolicited "
  2280. "iocb. tag 0x%x\n",
  2281. pring->ringno,
  2282. irsp->unsli3.sli3Words[7]);
  2283. }
  2284. }
  2285. }
  2286. if (irsp->ulpBdeCount != 0 &&
  2287. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2288. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2289. int found = 0;
  2290. /* search continue save q for same XRI */
  2291. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2292. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2293. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2294. list_add_tail(&saveq->list, &iocbq->list);
  2295. found = 1;
  2296. break;
  2297. }
  2298. }
  2299. if (!found)
  2300. list_add_tail(&saveq->clist,
  2301. &pring->iocb_continue_saveq);
  2302. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2303. list_del_init(&iocbq->clist);
  2304. saveq = iocbq;
  2305. irsp = &(saveq->iocb);
  2306. } else
  2307. return 0;
  2308. }
  2309. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2310. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2311. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2312. Rctl = FC_RCTL_ELS_REQ;
  2313. Type = FC_TYPE_ELS;
  2314. } else {
  2315. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2316. Rctl = w5p->hcsw.Rctl;
  2317. Type = w5p->hcsw.Type;
  2318. /* Firmware Workaround */
  2319. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2320. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2321. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2322. Rctl = FC_RCTL_ELS_REQ;
  2323. Type = FC_TYPE_ELS;
  2324. w5p->hcsw.Rctl = Rctl;
  2325. w5p->hcsw.Type = Type;
  2326. }
  2327. }
  2328. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2329. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2330. "0313 Ring %d handler: unexpected Rctl x%x "
  2331. "Type x%x received\n",
  2332. pring->ringno, Rctl, Type);
  2333. return 1;
  2334. }
  2335. /**
  2336. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2337. * @phba: Pointer to HBA context object.
  2338. * @pring: Pointer to driver SLI ring object.
  2339. * @prspiocb: Pointer to response iocb object.
  2340. *
  2341. * This function looks up the iocb_lookup table to get the command iocb
  2342. * corresponding to the given response iocb using the iotag of the
  2343. * response iocb. This function is called with the hbalock held.
  2344. * This function returns the command iocb object if it finds the command
  2345. * iocb else returns NULL.
  2346. **/
  2347. static struct lpfc_iocbq *
  2348. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2349. struct lpfc_sli_ring *pring,
  2350. struct lpfc_iocbq *prspiocb)
  2351. {
  2352. struct lpfc_iocbq *cmd_iocb = NULL;
  2353. uint16_t iotag;
  2354. iotag = prspiocb->iocb.ulpIoTag;
  2355. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2356. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2357. list_del_init(&cmd_iocb->list);
  2358. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2359. pring->txcmplq_cnt--;
  2360. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2361. }
  2362. return cmd_iocb;
  2363. }
  2364. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2365. "0317 iotag x%x is out off "
  2366. "range: max iotag x%x wd0 x%x\n",
  2367. iotag, phba->sli.last_iotag,
  2368. *(((uint32_t *) &prspiocb->iocb) + 7));
  2369. return NULL;
  2370. }
  2371. /**
  2372. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2373. * @phba: Pointer to HBA context object.
  2374. * @pring: Pointer to driver SLI ring object.
  2375. * @iotag: IOCB tag.
  2376. *
  2377. * This function looks up the iocb_lookup table to get the command iocb
  2378. * corresponding to the given iotag. This function is called with the
  2379. * hbalock held.
  2380. * This function returns the command iocb object if it finds the command
  2381. * iocb else returns NULL.
  2382. **/
  2383. static struct lpfc_iocbq *
  2384. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2385. struct lpfc_sli_ring *pring, uint16_t iotag)
  2386. {
  2387. struct lpfc_iocbq *cmd_iocb;
  2388. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2389. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2390. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2391. /* remove from txcmpl queue list */
  2392. list_del_init(&cmd_iocb->list);
  2393. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2394. pring->txcmplq_cnt--;
  2395. return cmd_iocb;
  2396. }
  2397. }
  2398. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2399. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2400. iotag, phba->sli.last_iotag);
  2401. return NULL;
  2402. }
  2403. /**
  2404. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2405. * @phba: Pointer to HBA context object.
  2406. * @pring: Pointer to driver SLI ring object.
  2407. * @saveq: Pointer to the response iocb to be processed.
  2408. *
  2409. * This function is called by the ring event handler for non-fcp
  2410. * rings when there is a new response iocb in the response ring.
  2411. * The caller is not required to hold any locks. This function
  2412. * gets the command iocb associated with the response iocb and
  2413. * calls the completion handler for the command iocb. If there
  2414. * is no completion handler, the function will free the resources
  2415. * associated with command iocb. If the response iocb is for
  2416. * an already aborted command iocb, the status of the completion
  2417. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2418. * This function always returns 1.
  2419. **/
  2420. static int
  2421. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2422. struct lpfc_iocbq *saveq)
  2423. {
  2424. struct lpfc_iocbq *cmdiocbp;
  2425. int rc = 1;
  2426. unsigned long iflag;
  2427. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2428. spin_lock_irqsave(&phba->hbalock, iflag);
  2429. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2430. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2431. if (cmdiocbp) {
  2432. if (cmdiocbp->iocb_cmpl) {
  2433. /*
  2434. * If an ELS command failed send an event to mgmt
  2435. * application.
  2436. */
  2437. if (saveq->iocb.ulpStatus &&
  2438. (pring->ringno == LPFC_ELS_RING) &&
  2439. (cmdiocbp->iocb.ulpCommand ==
  2440. CMD_ELS_REQUEST64_CR))
  2441. lpfc_send_els_failure_event(phba,
  2442. cmdiocbp, saveq);
  2443. /*
  2444. * Post all ELS completions to the worker thread.
  2445. * All other are passed to the completion callback.
  2446. */
  2447. if (pring->ringno == LPFC_ELS_RING) {
  2448. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2449. (cmdiocbp->iocb_flag &
  2450. LPFC_DRIVER_ABORTED)) {
  2451. spin_lock_irqsave(&phba->hbalock,
  2452. iflag);
  2453. cmdiocbp->iocb_flag &=
  2454. ~LPFC_DRIVER_ABORTED;
  2455. spin_unlock_irqrestore(&phba->hbalock,
  2456. iflag);
  2457. saveq->iocb.ulpStatus =
  2458. IOSTAT_LOCAL_REJECT;
  2459. saveq->iocb.un.ulpWord[4] =
  2460. IOERR_SLI_ABORTED;
  2461. /* Firmware could still be in progress
  2462. * of DMAing payload, so don't free data
  2463. * buffer till after a hbeat.
  2464. */
  2465. spin_lock_irqsave(&phba->hbalock,
  2466. iflag);
  2467. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2468. spin_unlock_irqrestore(&phba->hbalock,
  2469. iflag);
  2470. }
  2471. if (phba->sli_rev == LPFC_SLI_REV4) {
  2472. if (saveq->iocb_flag &
  2473. LPFC_EXCHANGE_BUSY) {
  2474. /* Set cmdiocb flag for the
  2475. * exchange busy so sgl (xri)
  2476. * will not be released until
  2477. * the abort xri is received
  2478. * from hba.
  2479. */
  2480. spin_lock_irqsave(
  2481. &phba->hbalock, iflag);
  2482. cmdiocbp->iocb_flag |=
  2483. LPFC_EXCHANGE_BUSY;
  2484. spin_unlock_irqrestore(
  2485. &phba->hbalock, iflag);
  2486. }
  2487. if (cmdiocbp->iocb_flag &
  2488. LPFC_DRIVER_ABORTED) {
  2489. /*
  2490. * Clear LPFC_DRIVER_ABORTED
  2491. * bit in case it was driver
  2492. * initiated abort.
  2493. */
  2494. spin_lock_irqsave(
  2495. &phba->hbalock, iflag);
  2496. cmdiocbp->iocb_flag &=
  2497. ~LPFC_DRIVER_ABORTED;
  2498. spin_unlock_irqrestore(
  2499. &phba->hbalock, iflag);
  2500. cmdiocbp->iocb.ulpStatus =
  2501. IOSTAT_LOCAL_REJECT;
  2502. cmdiocbp->iocb.un.ulpWord[4] =
  2503. IOERR_ABORT_REQUESTED;
  2504. /*
  2505. * For SLI4, irsiocb contains
  2506. * NO_XRI in sli_xritag, it
  2507. * shall not affect releasing
  2508. * sgl (xri) process.
  2509. */
  2510. saveq->iocb.ulpStatus =
  2511. IOSTAT_LOCAL_REJECT;
  2512. saveq->iocb.un.ulpWord[4] =
  2513. IOERR_SLI_ABORTED;
  2514. spin_lock_irqsave(
  2515. &phba->hbalock, iflag);
  2516. saveq->iocb_flag |=
  2517. LPFC_DELAY_MEM_FREE;
  2518. spin_unlock_irqrestore(
  2519. &phba->hbalock, iflag);
  2520. }
  2521. }
  2522. }
  2523. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2524. } else
  2525. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2526. } else {
  2527. /*
  2528. * Unknown initiating command based on the response iotag.
  2529. * This could be the case on the ELS ring because of
  2530. * lpfc_els_abort().
  2531. */
  2532. if (pring->ringno != LPFC_ELS_RING) {
  2533. /*
  2534. * Ring <ringno> handler: unexpected completion IoTag
  2535. * <IoTag>
  2536. */
  2537. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2538. "0322 Ring %d handler: "
  2539. "unexpected completion IoTag x%x "
  2540. "Data: x%x x%x x%x x%x\n",
  2541. pring->ringno,
  2542. saveq->iocb.ulpIoTag,
  2543. saveq->iocb.ulpStatus,
  2544. saveq->iocb.un.ulpWord[4],
  2545. saveq->iocb.ulpCommand,
  2546. saveq->iocb.ulpContext);
  2547. }
  2548. }
  2549. return rc;
  2550. }
  2551. /**
  2552. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2553. * @phba: Pointer to HBA context object.
  2554. * @pring: Pointer to driver SLI ring object.
  2555. *
  2556. * This function is called from the iocb ring event handlers when
  2557. * put pointer is ahead of the get pointer for a ring. This function signal
  2558. * an error attention condition to the worker thread and the worker
  2559. * thread will transition the HBA to offline state.
  2560. **/
  2561. static void
  2562. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2563. {
  2564. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2565. /*
  2566. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2567. * rsp ring <portRspMax>
  2568. */
  2569. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2570. "0312 Ring %d handler: portRspPut %d "
  2571. "is bigger than rsp ring %d\n",
  2572. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2573. pring->sli.sli3.numRiocb);
  2574. phba->link_state = LPFC_HBA_ERROR;
  2575. /*
  2576. * All error attention handlers are posted to
  2577. * worker thread
  2578. */
  2579. phba->work_ha |= HA_ERATT;
  2580. phba->work_hs = HS_FFER3;
  2581. lpfc_worker_wake_up(phba);
  2582. return;
  2583. }
  2584. /**
  2585. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2586. * @ptr: Pointer to address of HBA context object.
  2587. *
  2588. * This function is invoked by the Error Attention polling timer when the
  2589. * timer times out. It will check the SLI Error Attention register for
  2590. * possible attention events. If so, it will post an Error Attention event
  2591. * and wake up worker thread to process it. Otherwise, it will set up the
  2592. * Error Attention polling timer for the next poll.
  2593. **/
  2594. void lpfc_poll_eratt(unsigned long ptr)
  2595. {
  2596. struct lpfc_hba *phba;
  2597. uint32_t eratt = 0, rem;
  2598. uint64_t sli_intr, cnt;
  2599. phba = (struct lpfc_hba *)ptr;
  2600. /* Here we will also keep track of interrupts per sec of the hba */
  2601. sli_intr = phba->sli.slistat.sli_intr;
  2602. if (phba->sli.slistat.sli_prev_intr > sli_intr)
  2603. cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
  2604. sli_intr);
  2605. else
  2606. cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
  2607. /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
  2608. rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
  2609. phba->sli.slistat.sli_ips = cnt;
  2610. phba->sli.slistat.sli_prev_intr = sli_intr;
  2611. /* Check chip HA register for error event */
  2612. eratt = lpfc_sli_check_eratt(phba);
  2613. if (eratt)
  2614. /* Tell the worker thread there is work to do */
  2615. lpfc_worker_wake_up(phba);
  2616. else
  2617. /* Restart the timer for next eratt poll */
  2618. mod_timer(&phba->eratt_poll, jiffies +
  2619. HZ * LPFC_ERATT_POLL_INTERVAL);
  2620. return;
  2621. }
  2622. /**
  2623. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2624. * @phba: Pointer to HBA context object.
  2625. * @pring: Pointer to driver SLI ring object.
  2626. * @mask: Host attention register mask for this ring.
  2627. *
  2628. * This function is called from the interrupt context when there is a ring
  2629. * event for the fcp ring. The caller does not hold any lock.
  2630. * The function processes each response iocb in the response ring until it
  2631. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2632. * LE bit set. The function will call the completion handler of the command iocb
  2633. * if the response iocb indicates a completion for a command iocb or it is
  2634. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2635. * function if this is an unsolicited iocb.
  2636. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2637. * to check it explicitly.
  2638. */
  2639. int
  2640. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2641. struct lpfc_sli_ring *pring, uint32_t mask)
  2642. {
  2643. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2644. IOCB_t *irsp = NULL;
  2645. IOCB_t *entry = NULL;
  2646. struct lpfc_iocbq *cmdiocbq = NULL;
  2647. struct lpfc_iocbq rspiocbq;
  2648. uint32_t status;
  2649. uint32_t portRspPut, portRspMax;
  2650. int rc = 1;
  2651. lpfc_iocb_type type;
  2652. unsigned long iflag;
  2653. uint32_t rsp_cmpl = 0;
  2654. spin_lock_irqsave(&phba->hbalock, iflag);
  2655. pring->stats.iocb_event++;
  2656. /*
  2657. * The next available response entry should never exceed the maximum
  2658. * entries. If it does, treat it as an adapter hardware error.
  2659. */
  2660. portRspMax = pring->sli.sli3.numRiocb;
  2661. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2662. if (unlikely(portRspPut >= portRspMax)) {
  2663. lpfc_sli_rsp_pointers_error(phba, pring);
  2664. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2665. return 1;
  2666. }
  2667. if (phba->fcp_ring_in_use) {
  2668. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2669. return 1;
  2670. } else
  2671. phba->fcp_ring_in_use = 1;
  2672. rmb();
  2673. while (pring->sli.sli3.rspidx != portRspPut) {
  2674. /*
  2675. * Fetch an entry off the ring and copy it into a local data
  2676. * structure. The copy involves a byte-swap since the
  2677. * network byte order and pci byte orders are different.
  2678. */
  2679. entry = lpfc_resp_iocb(phba, pring);
  2680. phba->last_completion_time = jiffies;
  2681. if (++pring->sli.sli3.rspidx >= portRspMax)
  2682. pring->sli.sli3.rspidx = 0;
  2683. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2684. (uint32_t *) &rspiocbq.iocb,
  2685. phba->iocb_rsp_size);
  2686. INIT_LIST_HEAD(&(rspiocbq.list));
  2687. irsp = &rspiocbq.iocb;
  2688. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2689. pring->stats.iocb_rsp++;
  2690. rsp_cmpl++;
  2691. if (unlikely(irsp->ulpStatus)) {
  2692. /*
  2693. * If resource errors reported from HBA, reduce
  2694. * queuedepths of the SCSI device.
  2695. */
  2696. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2697. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2698. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2699. phba->lpfc_rampdown_queue_depth(phba);
  2700. spin_lock_irqsave(&phba->hbalock, iflag);
  2701. }
  2702. /* Rsp ring <ringno> error: IOCB */
  2703. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2704. "0336 Rsp Ring %d error: IOCB Data: "
  2705. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2706. pring->ringno,
  2707. irsp->un.ulpWord[0],
  2708. irsp->un.ulpWord[1],
  2709. irsp->un.ulpWord[2],
  2710. irsp->un.ulpWord[3],
  2711. irsp->un.ulpWord[4],
  2712. irsp->un.ulpWord[5],
  2713. *(uint32_t *)&irsp->un1,
  2714. *((uint32_t *)&irsp->un1 + 1));
  2715. }
  2716. switch (type) {
  2717. case LPFC_ABORT_IOCB:
  2718. case LPFC_SOL_IOCB:
  2719. /*
  2720. * Idle exchange closed via ABTS from port. No iocb
  2721. * resources need to be recovered.
  2722. */
  2723. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2724. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2725. "0333 IOCB cmd 0x%x"
  2726. " processed. Skipping"
  2727. " completion\n",
  2728. irsp->ulpCommand);
  2729. break;
  2730. }
  2731. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2732. &rspiocbq);
  2733. if (unlikely(!cmdiocbq))
  2734. break;
  2735. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2736. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2737. if (cmdiocbq->iocb_cmpl) {
  2738. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2739. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2740. &rspiocbq);
  2741. spin_lock_irqsave(&phba->hbalock, iflag);
  2742. }
  2743. break;
  2744. case LPFC_UNSOL_IOCB:
  2745. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2746. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2747. spin_lock_irqsave(&phba->hbalock, iflag);
  2748. break;
  2749. default:
  2750. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2751. char adaptermsg[LPFC_MAX_ADPTMSG];
  2752. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2753. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2754. MAX_MSG_DATA);
  2755. dev_warn(&((phba->pcidev)->dev),
  2756. "lpfc%d: %s\n",
  2757. phba->brd_no, adaptermsg);
  2758. } else {
  2759. /* Unknown IOCB command */
  2760. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2761. "0334 Unknown IOCB command "
  2762. "Data: x%x, x%x x%x x%x x%x\n",
  2763. type, irsp->ulpCommand,
  2764. irsp->ulpStatus,
  2765. irsp->ulpIoTag,
  2766. irsp->ulpContext);
  2767. }
  2768. break;
  2769. }
  2770. /*
  2771. * The response IOCB has been processed. Update the ring
  2772. * pointer in SLIM. If the port response put pointer has not
  2773. * been updated, sync the pgp->rspPutInx and fetch the new port
  2774. * response put pointer.
  2775. */
  2776. writel(pring->sli.sli3.rspidx,
  2777. &phba->host_gp[pring->ringno].rspGetInx);
  2778. if (pring->sli.sli3.rspidx == portRspPut)
  2779. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2780. }
  2781. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2782. pring->stats.iocb_rsp_full++;
  2783. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2784. writel(status, phba->CAregaddr);
  2785. readl(phba->CAregaddr);
  2786. }
  2787. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2788. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2789. pring->stats.iocb_cmd_empty++;
  2790. /* Force update of the local copy of cmdGetInx */
  2791. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2792. lpfc_sli_resume_iocb(phba, pring);
  2793. if ((pring->lpfc_sli_cmd_available))
  2794. (pring->lpfc_sli_cmd_available) (phba, pring);
  2795. }
  2796. phba->fcp_ring_in_use = 0;
  2797. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2798. return rc;
  2799. }
  2800. /**
  2801. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2802. * @phba: Pointer to HBA context object.
  2803. * @pring: Pointer to driver SLI ring object.
  2804. * @rspiocbp: Pointer to driver response IOCB object.
  2805. *
  2806. * This function is called from the worker thread when there is a slow-path
  2807. * response IOCB to process. This function chains all the response iocbs until
  2808. * seeing the iocb with the LE bit set. The function will call
  2809. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2810. * completion of a command iocb. The function will call the
  2811. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2812. * The function frees the resources or calls the completion handler if this
  2813. * iocb is an abort completion. The function returns NULL when the response
  2814. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2815. * this function shall chain the iocb on to the iocb_continueq and return the
  2816. * response iocb passed in.
  2817. **/
  2818. static struct lpfc_iocbq *
  2819. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2820. struct lpfc_iocbq *rspiocbp)
  2821. {
  2822. struct lpfc_iocbq *saveq;
  2823. struct lpfc_iocbq *cmdiocbp;
  2824. struct lpfc_iocbq *next_iocb;
  2825. IOCB_t *irsp = NULL;
  2826. uint32_t free_saveq;
  2827. uint8_t iocb_cmd_type;
  2828. lpfc_iocb_type type;
  2829. unsigned long iflag;
  2830. int rc;
  2831. spin_lock_irqsave(&phba->hbalock, iflag);
  2832. /* First add the response iocb to the countinueq list */
  2833. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2834. pring->iocb_continueq_cnt++;
  2835. /* Now, determine whether the list is completed for processing */
  2836. irsp = &rspiocbp->iocb;
  2837. if (irsp->ulpLe) {
  2838. /*
  2839. * By default, the driver expects to free all resources
  2840. * associated with this iocb completion.
  2841. */
  2842. free_saveq = 1;
  2843. saveq = list_get_first(&pring->iocb_continueq,
  2844. struct lpfc_iocbq, list);
  2845. irsp = &(saveq->iocb);
  2846. list_del_init(&pring->iocb_continueq);
  2847. pring->iocb_continueq_cnt = 0;
  2848. pring->stats.iocb_rsp++;
  2849. /*
  2850. * If resource errors reported from HBA, reduce
  2851. * queuedepths of the SCSI device.
  2852. */
  2853. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2854. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2855. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2856. phba->lpfc_rampdown_queue_depth(phba);
  2857. spin_lock_irqsave(&phba->hbalock, iflag);
  2858. }
  2859. if (irsp->ulpStatus) {
  2860. /* Rsp ring <ringno> error: IOCB */
  2861. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2862. "0328 Rsp Ring %d error: "
  2863. "IOCB Data: "
  2864. "x%x x%x x%x x%x "
  2865. "x%x x%x x%x x%x "
  2866. "x%x x%x x%x x%x "
  2867. "x%x x%x x%x x%x\n",
  2868. pring->ringno,
  2869. irsp->un.ulpWord[0],
  2870. irsp->un.ulpWord[1],
  2871. irsp->un.ulpWord[2],
  2872. irsp->un.ulpWord[3],
  2873. irsp->un.ulpWord[4],
  2874. irsp->un.ulpWord[5],
  2875. *(((uint32_t *) irsp) + 6),
  2876. *(((uint32_t *) irsp) + 7),
  2877. *(((uint32_t *) irsp) + 8),
  2878. *(((uint32_t *) irsp) + 9),
  2879. *(((uint32_t *) irsp) + 10),
  2880. *(((uint32_t *) irsp) + 11),
  2881. *(((uint32_t *) irsp) + 12),
  2882. *(((uint32_t *) irsp) + 13),
  2883. *(((uint32_t *) irsp) + 14),
  2884. *(((uint32_t *) irsp) + 15));
  2885. }
  2886. /*
  2887. * Fetch the IOCB command type and call the correct completion
  2888. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2889. * get freed back to the lpfc_iocb_list by the discovery
  2890. * kernel thread.
  2891. */
  2892. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2893. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2894. switch (type) {
  2895. case LPFC_SOL_IOCB:
  2896. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2897. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2898. spin_lock_irqsave(&phba->hbalock, iflag);
  2899. break;
  2900. case LPFC_UNSOL_IOCB:
  2901. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2902. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2903. spin_lock_irqsave(&phba->hbalock, iflag);
  2904. if (!rc)
  2905. free_saveq = 0;
  2906. break;
  2907. case LPFC_ABORT_IOCB:
  2908. cmdiocbp = NULL;
  2909. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2910. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2911. saveq);
  2912. if (cmdiocbp) {
  2913. /* Call the specified completion routine */
  2914. if (cmdiocbp->iocb_cmpl) {
  2915. spin_unlock_irqrestore(&phba->hbalock,
  2916. iflag);
  2917. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2918. saveq);
  2919. spin_lock_irqsave(&phba->hbalock,
  2920. iflag);
  2921. } else
  2922. __lpfc_sli_release_iocbq(phba,
  2923. cmdiocbp);
  2924. }
  2925. break;
  2926. case LPFC_UNKNOWN_IOCB:
  2927. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2928. char adaptermsg[LPFC_MAX_ADPTMSG];
  2929. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2930. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2931. MAX_MSG_DATA);
  2932. dev_warn(&((phba->pcidev)->dev),
  2933. "lpfc%d: %s\n",
  2934. phba->brd_no, adaptermsg);
  2935. } else {
  2936. /* Unknown IOCB command */
  2937. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2938. "0335 Unknown IOCB "
  2939. "command Data: x%x "
  2940. "x%x x%x x%x\n",
  2941. irsp->ulpCommand,
  2942. irsp->ulpStatus,
  2943. irsp->ulpIoTag,
  2944. irsp->ulpContext);
  2945. }
  2946. break;
  2947. }
  2948. if (free_saveq) {
  2949. list_for_each_entry_safe(rspiocbp, next_iocb,
  2950. &saveq->list, list) {
  2951. list_del(&rspiocbp->list);
  2952. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2953. }
  2954. __lpfc_sli_release_iocbq(phba, saveq);
  2955. }
  2956. rspiocbp = NULL;
  2957. }
  2958. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2959. return rspiocbp;
  2960. }
  2961. /**
  2962. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2963. * @phba: Pointer to HBA context object.
  2964. * @pring: Pointer to driver SLI ring object.
  2965. * @mask: Host attention register mask for this ring.
  2966. *
  2967. * This routine wraps the actual slow_ring event process routine from the
  2968. * API jump table function pointer from the lpfc_hba struct.
  2969. **/
  2970. void
  2971. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2972. struct lpfc_sli_ring *pring, uint32_t mask)
  2973. {
  2974. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2975. }
  2976. /**
  2977. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  2978. * @phba: Pointer to HBA context object.
  2979. * @pring: Pointer to driver SLI ring object.
  2980. * @mask: Host attention register mask for this ring.
  2981. *
  2982. * This function is called from the worker thread when there is a ring event
  2983. * for non-fcp rings. The caller does not hold any lock. The function will
  2984. * remove each response iocb in the response ring and calls the handle
  2985. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2986. **/
  2987. static void
  2988. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2989. struct lpfc_sli_ring *pring, uint32_t mask)
  2990. {
  2991. struct lpfc_pgp *pgp;
  2992. IOCB_t *entry;
  2993. IOCB_t *irsp = NULL;
  2994. struct lpfc_iocbq *rspiocbp = NULL;
  2995. uint32_t portRspPut, portRspMax;
  2996. unsigned long iflag;
  2997. uint32_t status;
  2998. pgp = &phba->port_gp[pring->ringno];
  2999. spin_lock_irqsave(&phba->hbalock, iflag);
  3000. pring->stats.iocb_event++;
  3001. /*
  3002. * The next available response entry should never exceed the maximum
  3003. * entries. If it does, treat it as an adapter hardware error.
  3004. */
  3005. portRspMax = pring->sli.sli3.numRiocb;
  3006. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3007. if (portRspPut >= portRspMax) {
  3008. /*
  3009. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  3010. * rsp ring <portRspMax>
  3011. */
  3012. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3013. "0303 Ring %d handler: portRspPut %d "
  3014. "is bigger than rsp ring %d\n",
  3015. pring->ringno, portRspPut, portRspMax);
  3016. phba->link_state = LPFC_HBA_ERROR;
  3017. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3018. phba->work_hs = HS_FFER3;
  3019. lpfc_handle_eratt(phba);
  3020. return;
  3021. }
  3022. rmb();
  3023. while (pring->sli.sli3.rspidx != portRspPut) {
  3024. /*
  3025. * Build a completion list and call the appropriate handler.
  3026. * The process is to get the next available response iocb, get
  3027. * a free iocb from the list, copy the response data into the
  3028. * free iocb, insert to the continuation list, and update the
  3029. * next response index to slim. This process makes response
  3030. * iocb's in the ring available to DMA as fast as possible but
  3031. * pays a penalty for a copy operation. Since the iocb is
  3032. * only 32 bytes, this penalty is considered small relative to
  3033. * the PCI reads for register values and a slim write. When
  3034. * the ulpLe field is set, the entire Command has been
  3035. * received.
  3036. */
  3037. entry = lpfc_resp_iocb(phba, pring);
  3038. phba->last_completion_time = jiffies;
  3039. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3040. if (rspiocbp == NULL) {
  3041. printk(KERN_ERR "%s: out of buffers! Failing "
  3042. "completion.\n", __func__);
  3043. break;
  3044. }
  3045. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3046. phba->iocb_rsp_size);
  3047. irsp = &rspiocbp->iocb;
  3048. if (++pring->sli.sli3.rspidx >= portRspMax)
  3049. pring->sli.sli3.rspidx = 0;
  3050. if (pring->ringno == LPFC_ELS_RING) {
  3051. lpfc_debugfs_slow_ring_trc(phba,
  3052. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3053. *(((uint32_t *) irsp) + 4),
  3054. *(((uint32_t *) irsp) + 6),
  3055. *(((uint32_t *) irsp) + 7));
  3056. }
  3057. writel(pring->sli.sli3.rspidx,
  3058. &phba->host_gp[pring->ringno].rspGetInx);
  3059. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3060. /* Handle the response IOCB */
  3061. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3062. spin_lock_irqsave(&phba->hbalock, iflag);
  3063. /*
  3064. * If the port response put pointer has not been updated, sync
  3065. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3066. * response put pointer.
  3067. */
  3068. if (pring->sli.sli3.rspidx == portRspPut) {
  3069. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3070. }
  3071. } /* while (pring->sli.sli3.rspidx != portRspPut) */
  3072. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3073. /* At least one response entry has been freed */
  3074. pring->stats.iocb_rsp_full++;
  3075. /* SET RxRE_RSP in Chip Att register */
  3076. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3077. writel(status, phba->CAregaddr);
  3078. readl(phba->CAregaddr); /* flush */
  3079. }
  3080. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3081. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3082. pring->stats.iocb_cmd_empty++;
  3083. /* Force update of the local copy of cmdGetInx */
  3084. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3085. lpfc_sli_resume_iocb(phba, pring);
  3086. if ((pring->lpfc_sli_cmd_available))
  3087. (pring->lpfc_sli_cmd_available) (phba, pring);
  3088. }
  3089. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3090. return;
  3091. }
  3092. /**
  3093. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3094. * @phba: Pointer to HBA context object.
  3095. * @pring: Pointer to driver SLI ring object.
  3096. * @mask: Host attention register mask for this ring.
  3097. *
  3098. * This function is called from the worker thread when there is a pending
  3099. * ELS response iocb on the driver internal slow-path response iocb worker
  3100. * queue. The caller does not hold any lock. The function will remove each
  3101. * response iocb from the response worker queue and calls the handle
  3102. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3103. **/
  3104. static void
  3105. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3106. struct lpfc_sli_ring *pring, uint32_t mask)
  3107. {
  3108. struct lpfc_iocbq *irspiocbq;
  3109. struct hbq_dmabuf *dmabuf;
  3110. struct lpfc_cq_event *cq_event;
  3111. unsigned long iflag;
  3112. spin_lock_irqsave(&phba->hbalock, iflag);
  3113. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3114. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3115. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3116. /* Get the response iocb from the head of work queue */
  3117. spin_lock_irqsave(&phba->hbalock, iflag);
  3118. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3119. cq_event, struct lpfc_cq_event, list);
  3120. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3121. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3122. case CQE_CODE_COMPL_WQE:
  3123. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3124. cq_event);
  3125. /* Translate ELS WCQE to response IOCBQ */
  3126. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3127. irspiocbq);
  3128. if (irspiocbq)
  3129. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3130. irspiocbq);
  3131. break;
  3132. case CQE_CODE_RECEIVE:
  3133. case CQE_CODE_RECEIVE_V1:
  3134. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3135. cq_event);
  3136. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3137. break;
  3138. default:
  3139. break;
  3140. }
  3141. }
  3142. }
  3143. /**
  3144. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3145. * @phba: Pointer to HBA context object.
  3146. * @pring: Pointer to driver SLI ring object.
  3147. *
  3148. * This function aborts all iocbs in the given ring and frees all the iocb
  3149. * objects in txq. This function issues an abort iocb for all the iocb commands
  3150. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3151. * the return of this function. The caller is not required to hold any locks.
  3152. **/
  3153. void
  3154. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3155. {
  3156. LIST_HEAD(completions);
  3157. struct lpfc_iocbq *iocb, *next_iocb;
  3158. if (pring->ringno == LPFC_ELS_RING) {
  3159. lpfc_fabric_abort_hba(phba);
  3160. }
  3161. /* Error everything on txq and txcmplq
  3162. * First do the txq.
  3163. */
  3164. spin_lock_irq(&phba->hbalock);
  3165. list_splice_init(&pring->txq, &completions);
  3166. pring->txq_cnt = 0;
  3167. /* Next issue ABTS for everything on the txcmplq */
  3168. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3169. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3170. spin_unlock_irq(&phba->hbalock);
  3171. /* Cancel all the IOCBs from the completions list */
  3172. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3173. IOERR_SLI_ABORTED);
  3174. }
  3175. /**
  3176. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3177. * @phba: Pointer to HBA context object.
  3178. *
  3179. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3180. * objects in txq and txcmplq. This function will not issue abort iocbs
  3181. * for all the iocb commands in txcmplq, they will just be returned with
  3182. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3183. * slot has been permanently disabled.
  3184. **/
  3185. void
  3186. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3187. {
  3188. LIST_HEAD(txq);
  3189. LIST_HEAD(txcmplq);
  3190. struct lpfc_sli *psli = &phba->sli;
  3191. struct lpfc_sli_ring *pring;
  3192. /* Currently, only one fcp ring */
  3193. pring = &psli->ring[psli->fcp_ring];
  3194. spin_lock_irq(&phba->hbalock);
  3195. /* Retrieve everything on txq */
  3196. list_splice_init(&pring->txq, &txq);
  3197. pring->txq_cnt = 0;
  3198. /* Retrieve everything on the txcmplq */
  3199. list_splice_init(&pring->txcmplq, &txcmplq);
  3200. pring->txcmplq_cnt = 0;
  3201. /* Indicate the I/O queues are flushed */
  3202. phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
  3203. spin_unlock_irq(&phba->hbalock);
  3204. /* Flush the txq */
  3205. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3206. IOERR_SLI_DOWN);
  3207. /* Flush the txcmpq */
  3208. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3209. IOERR_SLI_DOWN);
  3210. }
  3211. /**
  3212. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3213. * @phba: Pointer to HBA context object.
  3214. * @mask: Bit mask to be checked.
  3215. *
  3216. * This function reads the host status register and compares
  3217. * with the provided bit mask to check if HBA completed
  3218. * the restart. This function will wait in a loop for the
  3219. * HBA to complete restart. If the HBA does not restart within
  3220. * 15 iterations, the function will reset the HBA again. The
  3221. * function returns 1 when HBA fail to restart otherwise returns
  3222. * zero.
  3223. **/
  3224. static int
  3225. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3226. {
  3227. uint32_t status;
  3228. int i = 0;
  3229. int retval = 0;
  3230. /* Read the HBA Host Status Register */
  3231. if (lpfc_readl(phba->HSregaddr, &status))
  3232. return 1;
  3233. /*
  3234. * Check status register every 100ms for 5 retries, then every
  3235. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3236. * every 2.5 sec for 4.
  3237. * Break our of the loop if errors occurred during init.
  3238. */
  3239. while (((status & mask) != mask) &&
  3240. !(status & HS_FFERM) &&
  3241. i++ < 20) {
  3242. if (i <= 5)
  3243. msleep(10);
  3244. else if (i <= 10)
  3245. msleep(500);
  3246. else
  3247. msleep(2500);
  3248. if (i == 15) {
  3249. /* Do post */
  3250. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3251. lpfc_sli_brdrestart(phba);
  3252. }
  3253. /* Read the HBA Host Status Register */
  3254. if (lpfc_readl(phba->HSregaddr, &status)) {
  3255. retval = 1;
  3256. break;
  3257. }
  3258. }
  3259. /* Check to see if any errors occurred during init */
  3260. if ((status & HS_FFERM) || (i >= 20)) {
  3261. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3262. "2751 Adapter failed to restart, "
  3263. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3264. status,
  3265. readl(phba->MBslimaddr + 0xa8),
  3266. readl(phba->MBslimaddr + 0xac));
  3267. phba->link_state = LPFC_HBA_ERROR;
  3268. retval = 1;
  3269. }
  3270. return retval;
  3271. }
  3272. /**
  3273. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3274. * @phba: Pointer to HBA context object.
  3275. * @mask: Bit mask to be checked.
  3276. *
  3277. * This function checks the host status register to check if HBA is
  3278. * ready. This function will wait in a loop for the HBA to be ready
  3279. * If the HBA is not ready , the function will will reset the HBA PCI
  3280. * function again. The function returns 1 when HBA fail to be ready
  3281. * otherwise returns zero.
  3282. **/
  3283. static int
  3284. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3285. {
  3286. uint32_t status;
  3287. int retval = 0;
  3288. /* Read the HBA Host Status Register */
  3289. status = lpfc_sli4_post_status_check(phba);
  3290. if (status) {
  3291. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3292. lpfc_sli_brdrestart(phba);
  3293. status = lpfc_sli4_post_status_check(phba);
  3294. }
  3295. /* Check to see if any errors occurred during init */
  3296. if (status) {
  3297. phba->link_state = LPFC_HBA_ERROR;
  3298. retval = 1;
  3299. } else
  3300. phba->sli4_hba.intr_enable = 0;
  3301. return retval;
  3302. }
  3303. /**
  3304. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3305. * @phba: Pointer to HBA context object.
  3306. * @mask: Bit mask to be checked.
  3307. *
  3308. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3309. * from the API jump table function pointer from the lpfc_hba struct.
  3310. **/
  3311. int
  3312. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3313. {
  3314. return phba->lpfc_sli_brdready(phba, mask);
  3315. }
  3316. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3317. /**
  3318. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3319. * @phba: Pointer to HBA context object.
  3320. *
  3321. * This function is called before resetting an HBA. This function is called
  3322. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3323. **/
  3324. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3325. {
  3326. uint32_t __iomem *resp_buf;
  3327. uint32_t __iomem *mbox_buf;
  3328. volatile uint32_t mbox;
  3329. uint32_t hc_copy, ha_copy, resp_data;
  3330. int i;
  3331. uint8_t hdrtype;
  3332. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3333. if (hdrtype != 0x80 ||
  3334. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3335. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3336. return;
  3337. /*
  3338. * Tell the other part of the chip to suspend temporarily all
  3339. * its DMA activity.
  3340. */
  3341. resp_buf = phba->MBslimaddr;
  3342. /* Disable the error attention */
  3343. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3344. return;
  3345. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3346. readl(phba->HCregaddr); /* flush */
  3347. phba->link_flag |= LS_IGNORE_ERATT;
  3348. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3349. return;
  3350. if (ha_copy & HA_ERATT) {
  3351. /* Clear Chip error bit */
  3352. writel(HA_ERATT, phba->HAregaddr);
  3353. phba->pport->stopped = 1;
  3354. }
  3355. mbox = 0;
  3356. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3357. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3358. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3359. mbox_buf = phba->MBslimaddr;
  3360. writel(mbox, mbox_buf);
  3361. for (i = 0; i < 50; i++) {
  3362. if (lpfc_readl((resp_buf + 1), &resp_data))
  3363. return;
  3364. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3365. mdelay(1);
  3366. else
  3367. break;
  3368. }
  3369. resp_data = 0;
  3370. if (lpfc_readl((resp_buf + 1), &resp_data))
  3371. return;
  3372. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3373. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3374. phba->pport->stopped)
  3375. goto restore_hc;
  3376. else
  3377. goto clear_errat;
  3378. }
  3379. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3380. resp_data = 0;
  3381. for (i = 0; i < 500; i++) {
  3382. if (lpfc_readl(resp_buf, &resp_data))
  3383. return;
  3384. if (resp_data != mbox)
  3385. mdelay(1);
  3386. else
  3387. break;
  3388. }
  3389. clear_errat:
  3390. while (++i < 500) {
  3391. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3392. return;
  3393. if (!(ha_copy & HA_ERATT))
  3394. mdelay(1);
  3395. else
  3396. break;
  3397. }
  3398. if (readl(phba->HAregaddr) & HA_ERATT) {
  3399. writel(HA_ERATT, phba->HAregaddr);
  3400. phba->pport->stopped = 1;
  3401. }
  3402. restore_hc:
  3403. phba->link_flag &= ~LS_IGNORE_ERATT;
  3404. writel(hc_copy, phba->HCregaddr);
  3405. readl(phba->HCregaddr); /* flush */
  3406. }
  3407. /**
  3408. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3409. * @phba: Pointer to HBA context object.
  3410. *
  3411. * This function issues a kill_board mailbox command and waits for
  3412. * the error attention interrupt. This function is called for stopping
  3413. * the firmware processing. The caller is not required to hold any
  3414. * locks. This function calls lpfc_hba_down_post function to free
  3415. * any pending commands after the kill. The function will return 1 when it
  3416. * fails to kill the board else will return 0.
  3417. **/
  3418. int
  3419. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3420. {
  3421. struct lpfc_sli *psli;
  3422. LPFC_MBOXQ_t *pmb;
  3423. uint32_t status;
  3424. uint32_t ha_copy;
  3425. int retval;
  3426. int i = 0;
  3427. psli = &phba->sli;
  3428. /* Kill HBA */
  3429. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3430. "0329 Kill HBA Data: x%x x%x\n",
  3431. phba->pport->port_state, psli->sli_flag);
  3432. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3433. if (!pmb)
  3434. return 1;
  3435. /* Disable the error attention */
  3436. spin_lock_irq(&phba->hbalock);
  3437. if (lpfc_readl(phba->HCregaddr, &status)) {
  3438. spin_unlock_irq(&phba->hbalock);
  3439. mempool_free(pmb, phba->mbox_mem_pool);
  3440. return 1;
  3441. }
  3442. status &= ~HC_ERINT_ENA;
  3443. writel(status, phba->HCregaddr);
  3444. readl(phba->HCregaddr); /* flush */
  3445. phba->link_flag |= LS_IGNORE_ERATT;
  3446. spin_unlock_irq(&phba->hbalock);
  3447. lpfc_kill_board(phba, pmb);
  3448. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3449. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3450. if (retval != MBX_SUCCESS) {
  3451. if (retval != MBX_BUSY)
  3452. mempool_free(pmb, phba->mbox_mem_pool);
  3453. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3454. "2752 KILL_BOARD command failed retval %d\n",
  3455. retval);
  3456. spin_lock_irq(&phba->hbalock);
  3457. phba->link_flag &= ~LS_IGNORE_ERATT;
  3458. spin_unlock_irq(&phba->hbalock);
  3459. return 1;
  3460. }
  3461. spin_lock_irq(&phba->hbalock);
  3462. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3463. spin_unlock_irq(&phba->hbalock);
  3464. mempool_free(pmb, phba->mbox_mem_pool);
  3465. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3466. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3467. * 3 seconds we still set HBA_ERROR state because the status of the
  3468. * board is now undefined.
  3469. */
  3470. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3471. return 1;
  3472. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3473. mdelay(100);
  3474. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3475. return 1;
  3476. }
  3477. del_timer_sync(&psli->mbox_tmo);
  3478. if (ha_copy & HA_ERATT) {
  3479. writel(HA_ERATT, phba->HAregaddr);
  3480. phba->pport->stopped = 1;
  3481. }
  3482. spin_lock_irq(&phba->hbalock);
  3483. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3484. psli->mbox_active = NULL;
  3485. phba->link_flag &= ~LS_IGNORE_ERATT;
  3486. spin_unlock_irq(&phba->hbalock);
  3487. lpfc_hba_down_post(phba);
  3488. phba->link_state = LPFC_HBA_ERROR;
  3489. return ha_copy & HA_ERATT ? 0 : 1;
  3490. }
  3491. /**
  3492. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3493. * @phba: Pointer to HBA context object.
  3494. *
  3495. * This function resets the HBA by writing HC_INITFF to the control
  3496. * register. After the HBA resets, this function resets all the iocb ring
  3497. * indices. This function disables PCI layer parity checking during
  3498. * the reset.
  3499. * This function returns 0 always.
  3500. * The caller is not required to hold any locks.
  3501. **/
  3502. int
  3503. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3504. {
  3505. struct lpfc_sli *psli;
  3506. struct lpfc_sli_ring *pring;
  3507. uint16_t cfg_value;
  3508. int i;
  3509. psli = &phba->sli;
  3510. /* Reset HBA */
  3511. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3512. "0325 Reset HBA Data: x%x x%x\n",
  3513. phba->pport->port_state, psli->sli_flag);
  3514. /* perform board reset */
  3515. phba->fc_eventTag = 0;
  3516. phba->link_events = 0;
  3517. phba->pport->fc_myDID = 0;
  3518. phba->pport->fc_prevDID = 0;
  3519. /* Turn off parity checking and serr during the physical reset */
  3520. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3521. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3522. (cfg_value &
  3523. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3524. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3525. /* Now toggle INITFF bit in the Host Control Register */
  3526. writel(HC_INITFF, phba->HCregaddr);
  3527. mdelay(1);
  3528. readl(phba->HCregaddr); /* flush */
  3529. writel(0, phba->HCregaddr);
  3530. readl(phba->HCregaddr); /* flush */
  3531. /* Restore PCI cmd register */
  3532. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3533. /* Initialize relevant SLI info */
  3534. for (i = 0; i < psli->num_rings; i++) {
  3535. pring = &psli->ring[i];
  3536. pring->flag = 0;
  3537. pring->sli.sli3.rspidx = 0;
  3538. pring->sli.sli3.next_cmdidx = 0;
  3539. pring->sli.sli3.local_getidx = 0;
  3540. pring->sli.sli3.cmdidx = 0;
  3541. pring->missbufcnt = 0;
  3542. }
  3543. phba->link_state = LPFC_WARM_START;
  3544. return 0;
  3545. }
  3546. /**
  3547. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3548. * @phba: Pointer to HBA context object.
  3549. *
  3550. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3551. * checking during resets the device. The caller is not required to hold
  3552. * any locks.
  3553. *
  3554. * This function returns 0 always.
  3555. **/
  3556. int
  3557. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3558. {
  3559. struct lpfc_sli *psli = &phba->sli;
  3560. uint16_t cfg_value;
  3561. int rc;
  3562. /* Reset HBA */
  3563. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3564. "0295 Reset HBA Data: x%x x%x\n",
  3565. phba->pport->port_state, psli->sli_flag);
  3566. /* perform board reset */
  3567. phba->fc_eventTag = 0;
  3568. phba->link_events = 0;
  3569. phba->pport->fc_myDID = 0;
  3570. phba->pport->fc_prevDID = 0;
  3571. spin_lock_irq(&phba->hbalock);
  3572. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3573. phba->fcf.fcf_flag = 0;
  3574. spin_unlock_irq(&phba->hbalock);
  3575. /* Now physically reset the device */
  3576. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3577. "0389 Performing PCI function reset!\n");
  3578. /* Turn off parity checking and serr during the physical reset */
  3579. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3580. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3581. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3582. /* Perform FCoE PCI function reset */
  3583. lpfc_sli4_queue_destroy(phba);
  3584. rc = lpfc_pci_function_reset(phba);
  3585. /* Restore PCI cmd register */
  3586. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3587. return rc;
  3588. }
  3589. /**
  3590. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3591. * @phba: Pointer to HBA context object.
  3592. *
  3593. * This function is called in the SLI initialization code path to
  3594. * restart the HBA. The caller is not required to hold any lock.
  3595. * This function writes MBX_RESTART mailbox command to the SLIM and
  3596. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3597. * function to free any pending commands. The function enables
  3598. * POST only during the first initialization. The function returns zero.
  3599. * The function does not guarantee completion of MBX_RESTART mailbox
  3600. * command before the return of this function.
  3601. **/
  3602. static int
  3603. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3604. {
  3605. MAILBOX_t *mb;
  3606. struct lpfc_sli *psli;
  3607. volatile uint32_t word0;
  3608. void __iomem *to_slim;
  3609. uint32_t hba_aer_enabled;
  3610. spin_lock_irq(&phba->hbalock);
  3611. /* Take PCIe device Advanced Error Reporting (AER) state */
  3612. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3613. psli = &phba->sli;
  3614. /* Restart HBA */
  3615. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3616. "0337 Restart HBA Data: x%x x%x\n",
  3617. phba->pport->port_state, psli->sli_flag);
  3618. word0 = 0;
  3619. mb = (MAILBOX_t *) &word0;
  3620. mb->mbxCommand = MBX_RESTART;
  3621. mb->mbxHc = 1;
  3622. lpfc_reset_barrier(phba);
  3623. to_slim = phba->MBslimaddr;
  3624. writel(*(uint32_t *) mb, to_slim);
  3625. readl(to_slim); /* flush */
  3626. /* Only skip post after fc_ffinit is completed */
  3627. if (phba->pport->port_state)
  3628. word0 = 1; /* This is really setting up word1 */
  3629. else
  3630. word0 = 0; /* This is really setting up word1 */
  3631. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3632. writel(*(uint32_t *) mb, to_slim);
  3633. readl(to_slim); /* flush */
  3634. lpfc_sli_brdreset(phba);
  3635. phba->pport->stopped = 0;
  3636. phba->link_state = LPFC_INIT_START;
  3637. phba->hba_flag = 0;
  3638. spin_unlock_irq(&phba->hbalock);
  3639. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3640. psli->stats_start = get_seconds();
  3641. /* Give the INITFF and Post time to settle. */
  3642. mdelay(100);
  3643. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3644. if (hba_aer_enabled)
  3645. pci_disable_pcie_error_reporting(phba->pcidev);
  3646. lpfc_hba_down_post(phba);
  3647. return 0;
  3648. }
  3649. /**
  3650. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3651. * @phba: Pointer to HBA context object.
  3652. *
  3653. * This function is called in the SLI initialization code path to restart
  3654. * a SLI4 HBA. The caller is not required to hold any lock.
  3655. * At the end of the function, it calls lpfc_hba_down_post function to
  3656. * free any pending commands.
  3657. **/
  3658. static int
  3659. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3660. {
  3661. struct lpfc_sli *psli = &phba->sli;
  3662. uint32_t hba_aer_enabled;
  3663. int rc;
  3664. /* Restart HBA */
  3665. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3666. "0296 Restart HBA Data: x%x x%x\n",
  3667. phba->pport->port_state, psli->sli_flag);
  3668. /* Take PCIe device Advanced Error Reporting (AER) state */
  3669. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3670. rc = lpfc_sli4_brdreset(phba);
  3671. spin_lock_irq(&phba->hbalock);
  3672. phba->pport->stopped = 0;
  3673. phba->link_state = LPFC_INIT_START;
  3674. phba->hba_flag = 0;
  3675. spin_unlock_irq(&phba->hbalock);
  3676. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3677. psli->stats_start = get_seconds();
  3678. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3679. if (hba_aer_enabled)
  3680. pci_disable_pcie_error_reporting(phba->pcidev);
  3681. lpfc_hba_down_post(phba);
  3682. return rc;
  3683. }
  3684. /**
  3685. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3686. * @phba: Pointer to HBA context object.
  3687. *
  3688. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3689. * API jump table function pointer from the lpfc_hba struct.
  3690. **/
  3691. int
  3692. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3693. {
  3694. return phba->lpfc_sli_brdrestart(phba);
  3695. }
  3696. /**
  3697. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3698. * @phba: Pointer to HBA context object.
  3699. *
  3700. * This function is called after a HBA restart to wait for successful
  3701. * restart of the HBA. Successful restart of the HBA is indicated by
  3702. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3703. * iteration, the function will restart the HBA again. The function returns
  3704. * zero if HBA successfully restarted else returns negative error code.
  3705. **/
  3706. static int
  3707. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3708. {
  3709. uint32_t status, i = 0;
  3710. /* Read the HBA Host Status Register */
  3711. if (lpfc_readl(phba->HSregaddr, &status))
  3712. return -EIO;
  3713. /* Check status register to see what current state is */
  3714. i = 0;
  3715. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3716. /* Check every 10ms for 10 retries, then every 100ms for 90
  3717. * retries, then every 1 sec for 50 retires for a total of
  3718. * ~60 seconds before reset the board again and check every
  3719. * 1 sec for 50 retries. The up to 60 seconds before the
  3720. * board ready is required by the Falcon FIPS zeroization
  3721. * complete, and any reset the board in between shall cause
  3722. * restart of zeroization, further delay the board ready.
  3723. */
  3724. if (i++ >= 200) {
  3725. /* Adapter failed to init, timeout, status reg
  3726. <status> */
  3727. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3728. "0436 Adapter failed to init, "
  3729. "timeout, status reg x%x, "
  3730. "FW Data: A8 x%x AC x%x\n", status,
  3731. readl(phba->MBslimaddr + 0xa8),
  3732. readl(phba->MBslimaddr + 0xac));
  3733. phba->link_state = LPFC_HBA_ERROR;
  3734. return -ETIMEDOUT;
  3735. }
  3736. /* Check to see if any errors occurred during init */
  3737. if (status & HS_FFERM) {
  3738. /* ERROR: During chipset initialization */
  3739. /* Adapter failed to init, chipset, status reg
  3740. <status> */
  3741. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3742. "0437 Adapter failed to init, "
  3743. "chipset, status reg x%x, "
  3744. "FW Data: A8 x%x AC x%x\n", status,
  3745. readl(phba->MBslimaddr + 0xa8),
  3746. readl(phba->MBslimaddr + 0xac));
  3747. phba->link_state = LPFC_HBA_ERROR;
  3748. return -EIO;
  3749. }
  3750. if (i <= 10)
  3751. msleep(10);
  3752. else if (i <= 100)
  3753. msleep(100);
  3754. else
  3755. msleep(1000);
  3756. if (i == 150) {
  3757. /* Do post */
  3758. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3759. lpfc_sli_brdrestart(phba);
  3760. }
  3761. /* Read the HBA Host Status Register */
  3762. if (lpfc_readl(phba->HSregaddr, &status))
  3763. return -EIO;
  3764. }
  3765. /* Check to see if any errors occurred during init */
  3766. if (status & HS_FFERM) {
  3767. /* ERROR: During chipset initialization */
  3768. /* Adapter failed to init, chipset, status reg <status> */
  3769. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3770. "0438 Adapter failed to init, chipset, "
  3771. "status reg x%x, "
  3772. "FW Data: A8 x%x AC x%x\n", status,
  3773. readl(phba->MBslimaddr + 0xa8),
  3774. readl(phba->MBslimaddr + 0xac));
  3775. phba->link_state = LPFC_HBA_ERROR;
  3776. return -EIO;
  3777. }
  3778. /* Clear all interrupt enable conditions */
  3779. writel(0, phba->HCregaddr);
  3780. readl(phba->HCregaddr); /* flush */
  3781. /* setup host attn register */
  3782. writel(0xffffffff, phba->HAregaddr);
  3783. readl(phba->HAregaddr); /* flush */
  3784. return 0;
  3785. }
  3786. /**
  3787. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3788. *
  3789. * This function calculates and returns the number of HBQs required to be
  3790. * configured.
  3791. **/
  3792. int
  3793. lpfc_sli_hbq_count(void)
  3794. {
  3795. return ARRAY_SIZE(lpfc_hbq_defs);
  3796. }
  3797. /**
  3798. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3799. *
  3800. * This function adds the number of hbq entries in every HBQ to get
  3801. * the total number of hbq entries required for the HBA and returns
  3802. * the total count.
  3803. **/
  3804. static int
  3805. lpfc_sli_hbq_entry_count(void)
  3806. {
  3807. int hbq_count = lpfc_sli_hbq_count();
  3808. int count = 0;
  3809. int i;
  3810. for (i = 0; i < hbq_count; ++i)
  3811. count += lpfc_hbq_defs[i]->entry_count;
  3812. return count;
  3813. }
  3814. /**
  3815. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3816. *
  3817. * This function calculates amount of memory required for all hbq entries
  3818. * to be configured and returns the total memory required.
  3819. **/
  3820. int
  3821. lpfc_sli_hbq_size(void)
  3822. {
  3823. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3824. }
  3825. /**
  3826. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3827. * @phba: Pointer to HBA context object.
  3828. *
  3829. * This function is called during the SLI initialization to configure
  3830. * all the HBQs and post buffers to the HBQ. The caller is not
  3831. * required to hold any locks. This function will return zero if successful
  3832. * else it will return negative error code.
  3833. **/
  3834. static int
  3835. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3836. {
  3837. int hbq_count = lpfc_sli_hbq_count();
  3838. LPFC_MBOXQ_t *pmb;
  3839. MAILBOX_t *pmbox;
  3840. uint32_t hbqno;
  3841. uint32_t hbq_entry_index;
  3842. /* Get a Mailbox buffer to setup mailbox
  3843. * commands for HBA initialization
  3844. */
  3845. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3846. if (!pmb)
  3847. return -ENOMEM;
  3848. pmbox = &pmb->u.mb;
  3849. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3850. phba->link_state = LPFC_INIT_MBX_CMDS;
  3851. phba->hbq_in_use = 1;
  3852. hbq_entry_index = 0;
  3853. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3854. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3855. phba->hbqs[hbqno].hbqPutIdx = 0;
  3856. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3857. phba->hbqs[hbqno].entry_count =
  3858. lpfc_hbq_defs[hbqno]->entry_count;
  3859. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3860. hbq_entry_index, pmb);
  3861. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3862. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3863. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3864. mbxStatus <status>, ring <num> */
  3865. lpfc_printf_log(phba, KERN_ERR,
  3866. LOG_SLI | LOG_VPORT,
  3867. "1805 Adapter failed to init. "
  3868. "Data: x%x x%x x%x\n",
  3869. pmbox->mbxCommand,
  3870. pmbox->mbxStatus, hbqno);
  3871. phba->link_state = LPFC_HBA_ERROR;
  3872. mempool_free(pmb, phba->mbox_mem_pool);
  3873. return -ENXIO;
  3874. }
  3875. }
  3876. phba->hbq_count = hbq_count;
  3877. mempool_free(pmb, phba->mbox_mem_pool);
  3878. /* Initially populate or replenish the HBQs */
  3879. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3880. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3881. return 0;
  3882. }
  3883. /**
  3884. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3885. * @phba: Pointer to HBA context object.
  3886. *
  3887. * This function is called during the SLI initialization to configure
  3888. * all the HBQs and post buffers to the HBQ. The caller is not
  3889. * required to hold any locks. This function will return zero if successful
  3890. * else it will return negative error code.
  3891. **/
  3892. static int
  3893. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3894. {
  3895. phba->hbq_in_use = 1;
  3896. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3897. phba->hbq_count = 1;
  3898. /* Initially populate or replenish the HBQs */
  3899. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3900. return 0;
  3901. }
  3902. /**
  3903. * lpfc_sli_config_port - Issue config port mailbox command
  3904. * @phba: Pointer to HBA context object.
  3905. * @sli_mode: sli mode - 2/3
  3906. *
  3907. * This function is called by the sli intialization code path
  3908. * to issue config_port mailbox command. This function restarts the
  3909. * HBA firmware and issues a config_port mailbox command to configure
  3910. * the SLI interface in the sli mode specified by sli_mode
  3911. * variable. The caller is not required to hold any locks.
  3912. * The function returns 0 if successful, else returns negative error
  3913. * code.
  3914. **/
  3915. int
  3916. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3917. {
  3918. LPFC_MBOXQ_t *pmb;
  3919. uint32_t resetcount = 0, rc = 0, done = 0;
  3920. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3921. if (!pmb) {
  3922. phba->link_state = LPFC_HBA_ERROR;
  3923. return -ENOMEM;
  3924. }
  3925. phba->sli_rev = sli_mode;
  3926. while (resetcount < 2 && !done) {
  3927. spin_lock_irq(&phba->hbalock);
  3928. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3929. spin_unlock_irq(&phba->hbalock);
  3930. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3931. lpfc_sli_brdrestart(phba);
  3932. rc = lpfc_sli_chipset_init(phba);
  3933. if (rc)
  3934. break;
  3935. spin_lock_irq(&phba->hbalock);
  3936. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3937. spin_unlock_irq(&phba->hbalock);
  3938. resetcount++;
  3939. /* Call pre CONFIG_PORT mailbox command initialization. A
  3940. * value of 0 means the call was successful. Any other
  3941. * nonzero value is a failure, but if ERESTART is returned,
  3942. * the driver may reset the HBA and try again.
  3943. */
  3944. rc = lpfc_config_port_prep(phba);
  3945. if (rc == -ERESTART) {
  3946. phba->link_state = LPFC_LINK_UNKNOWN;
  3947. continue;
  3948. } else if (rc)
  3949. break;
  3950. phba->link_state = LPFC_INIT_MBX_CMDS;
  3951. lpfc_config_port(phba, pmb);
  3952. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3953. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3954. LPFC_SLI3_HBQ_ENABLED |
  3955. LPFC_SLI3_CRP_ENABLED |
  3956. LPFC_SLI3_BG_ENABLED |
  3957. LPFC_SLI3_DSS_ENABLED);
  3958. if (rc != MBX_SUCCESS) {
  3959. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3960. "0442 Adapter failed to init, mbxCmd x%x "
  3961. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3962. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3963. spin_lock_irq(&phba->hbalock);
  3964. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3965. spin_unlock_irq(&phba->hbalock);
  3966. rc = -ENXIO;
  3967. } else {
  3968. /* Allow asynchronous mailbox command to go through */
  3969. spin_lock_irq(&phba->hbalock);
  3970. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3971. spin_unlock_irq(&phba->hbalock);
  3972. done = 1;
  3973. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  3974. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  3975. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3976. "3110 Port did not grant ASABT\n");
  3977. }
  3978. }
  3979. if (!done) {
  3980. rc = -EINVAL;
  3981. goto do_prep_failed;
  3982. }
  3983. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3984. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3985. rc = -ENXIO;
  3986. goto do_prep_failed;
  3987. }
  3988. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3989. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3990. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3991. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3992. phba->max_vpi : phba->max_vports;
  3993. } else
  3994. phba->max_vpi = 0;
  3995. phba->fips_level = 0;
  3996. phba->fips_spec_rev = 0;
  3997. if (pmb->u.mb.un.varCfgPort.gdss) {
  3998. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3999. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  4000. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  4001. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4002. "2850 Security Crypto Active. FIPS x%d "
  4003. "(Spec Rev: x%d)",
  4004. phba->fips_level, phba->fips_spec_rev);
  4005. }
  4006. if (pmb->u.mb.un.varCfgPort.sec_err) {
  4007. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4008. "2856 Config Port Security Crypto "
  4009. "Error: x%x ",
  4010. pmb->u.mb.un.varCfgPort.sec_err);
  4011. }
  4012. if (pmb->u.mb.un.varCfgPort.gerbm)
  4013. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  4014. if (pmb->u.mb.un.varCfgPort.gcrp)
  4015. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  4016. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  4017. phba->port_gp = phba->mbox->us.s3_pgp.port;
  4018. if (phba->cfg_enable_bg) {
  4019. if (pmb->u.mb.un.varCfgPort.gbg)
  4020. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  4021. else
  4022. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4023. "0443 Adapter did not grant "
  4024. "BlockGuard\n");
  4025. }
  4026. } else {
  4027. phba->hbq_get = NULL;
  4028. phba->port_gp = phba->mbox->us.s2.port;
  4029. phba->max_vpi = 0;
  4030. }
  4031. do_prep_failed:
  4032. mempool_free(pmb, phba->mbox_mem_pool);
  4033. return rc;
  4034. }
  4035. /**
  4036. * lpfc_sli_hba_setup - SLI intialization function
  4037. * @phba: Pointer to HBA context object.
  4038. *
  4039. * This function is the main SLI intialization function. This function
  4040. * is called by the HBA intialization code, HBA reset code and HBA
  4041. * error attention handler code. Caller is not required to hold any
  4042. * locks. This function issues config_port mailbox command to configure
  4043. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4044. * calls the config_port_post function to issue init_link mailbox
  4045. * command and to start the discovery. The function will return zero
  4046. * if successful, else it will return negative error code.
  4047. **/
  4048. int
  4049. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4050. {
  4051. uint32_t rc;
  4052. int mode = 3, i;
  4053. int longs;
  4054. switch (lpfc_sli_mode) {
  4055. case 2:
  4056. if (phba->cfg_enable_npiv) {
  4057. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4058. "1824 NPIV enabled: Override lpfc_sli_mode "
  4059. "parameter (%d) to auto (0).\n",
  4060. lpfc_sli_mode);
  4061. break;
  4062. }
  4063. mode = 2;
  4064. break;
  4065. case 0:
  4066. case 3:
  4067. break;
  4068. default:
  4069. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4070. "1819 Unrecognized lpfc_sli_mode "
  4071. "parameter: %d.\n", lpfc_sli_mode);
  4072. break;
  4073. }
  4074. rc = lpfc_sli_config_port(phba, mode);
  4075. if (rc && lpfc_sli_mode == 3)
  4076. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4077. "1820 Unable to select SLI-3. "
  4078. "Not supported by adapter.\n");
  4079. if (rc && mode != 2)
  4080. rc = lpfc_sli_config_port(phba, 2);
  4081. if (rc)
  4082. goto lpfc_sli_hba_setup_error;
  4083. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4084. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4085. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4086. if (!rc) {
  4087. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4088. "2709 This device supports "
  4089. "Advanced Error Reporting (AER)\n");
  4090. spin_lock_irq(&phba->hbalock);
  4091. phba->hba_flag |= HBA_AER_ENABLED;
  4092. spin_unlock_irq(&phba->hbalock);
  4093. } else {
  4094. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4095. "2708 This device does not support "
  4096. "Advanced Error Reporting (AER)\n");
  4097. phba->cfg_aer_support = 0;
  4098. }
  4099. }
  4100. if (phba->sli_rev == 3) {
  4101. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4102. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4103. } else {
  4104. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4105. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4106. phba->sli3_options = 0;
  4107. }
  4108. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4109. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4110. phba->sli_rev, phba->max_vpi);
  4111. rc = lpfc_sli_ring_map(phba);
  4112. if (rc)
  4113. goto lpfc_sli_hba_setup_error;
  4114. /* Initialize VPIs. */
  4115. if (phba->sli_rev == LPFC_SLI_REV3) {
  4116. /*
  4117. * The VPI bitmask and physical ID array are allocated
  4118. * and initialized once only - at driver load. A port
  4119. * reset doesn't need to reinitialize this memory.
  4120. */
  4121. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4122. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4123. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4124. GFP_KERNEL);
  4125. if (!phba->vpi_bmask) {
  4126. rc = -ENOMEM;
  4127. goto lpfc_sli_hba_setup_error;
  4128. }
  4129. phba->vpi_ids = kzalloc(
  4130. (phba->max_vpi+1) * sizeof(uint16_t),
  4131. GFP_KERNEL);
  4132. if (!phba->vpi_ids) {
  4133. kfree(phba->vpi_bmask);
  4134. rc = -ENOMEM;
  4135. goto lpfc_sli_hba_setup_error;
  4136. }
  4137. for (i = 0; i < phba->max_vpi; i++)
  4138. phba->vpi_ids[i] = i;
  4139. }
  4140. }
  4141. /* Init HBQs */
  4142. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4143. rc = lpfc_sli_hbq_setup(phba);
  4144. if (rc)
  4145. goto lpfc_sli_hba_setup_error;
  4146. }
  4147. spin_lock_irq(&phba->hbalock);
  4148. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4149. spin_unlock_irq(&phba->hbalock);
  4150. rc = lpfc_config_port_post(phba);
  4151. if (rc)
  4152. goto lpfc_sli_hba_setup_error;
  4153. return rc;
  4154. lpfc_sli_hba_setup_error:
  4155. phba->link_state = LPFC_HBA_ERROR;
  4156. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4157. "0445 Firmware initialization failed\n");
  4158. return rc;
  4159. }
  4160. /**
  4161. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4162. * @phba: Pointer to HBA context object.
  4163. * @mboxq: mailbox pointer.
  4164. * This function issue a dump mailbox command to read config region
  4165. * 23 and parse the records in the region and populate driver
  4166. * data structure.
  4167. **/
  4168. static int
  4169. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4170. {
  4171. LPFC_MBOXQ_t *mboxq;
  4172. struct lpfc_dmabuf *mp;
  4173. struct lpfc_mqe *mqe;
  4174. uint32_t data_length;
  4175. int rc;
  4176. /* Program the default value of vlan_id and fc_map */
  4177. phba->valid_vlan = 0;
  4178. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4179. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4180. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4181. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4182. if (!mboxq)
  4183. return -ENOMEM;
  4184. mqe = &mboxq->u.mqe;
  4185. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4186. rc = -ENOMEM;
  4187. goto out_free_mboxq;
  4188. }
  4189. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4190. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4191. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4192. "(%d):2571 Mailbox cmd x%x Status x%x "
  4193. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4194. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4195. "CQ: x%x x%x x%x x%x\n",
  4196. mboxq->vport ? mboxq->vport->vpi : 0,
  4197. bf_get(lpfc_mqe_command, mqe),
  4198. bf_get(lpfc_mqe_status, mqe),
  4199. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4200. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4201. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4202. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4203. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4204. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4205. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4206. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4207. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4208. mboxq->mcqe.word0,
  4209. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4210. mboxq->mcqe.trailer);
  4211. if (rc) {
  4212. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4213. kfree(mp);
  4214. rc = -EIO;
  4215. goto out_free_mboxq;
  4216. }
  4217. data_length = mqe->un.mb_words[5];
  4218. if (data_length > DMP_RGN23_SIZE) {
  4219. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4220. kfree(mp);
  4221. rc = -EIO;
  4222. goto out_free_mboxq;
  4223. }
  4224. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4225. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4226. kfree(mp);
  4227. rc = 0;
  4228. out_free_mboxq:
  4229. mempool_free(mboxq, phba->mbox_mem_pool);
  4230. return rc;
  4231. }
  4232. /**
  4233. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4234. * @phba: pointer to lpfc hba data structure.
  4235. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4236. * @vpd: pointer to the memory to hold resulting port vpd data.
  4237. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4238. * On output, the number of data bytes in @vpd.
  4239. *
  4240. * This routine executes a READ_REV SLI4 mailbox command. In
  4241. * addition, this routine gets the port vpd data.
  4242. *
  4243. * Return codes
  4244. * 0 - successful
  4245. * -ENOMEM - could not allocated memory.
  4246. **/
  4247. static int
  4248. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4249. uint8_t *vpd, uint32_t *vpd_size)
  4250. {
  4251. int rc = 0;
  4252. uint32_t dma_size;
  4253. struct lpfc_dmabuf *dmabuf;
  4254. struct lpfc_mqe *mqe;
  4255. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4256. if (!dmabuf)
  4257. return -ENOMEM;
  4258. /*
  4259. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4260. * mailbox command.
  4261. */
  4262. dma_size = *vpd_size;
  4263. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4264. dma_size,
  4265. &dmabuf->phys,
  4266. GFP_KERNEL);
  4267. if (!dmabuf->virt) {
  4268. kfree(dmabuf);
  4269. return -ENOMEM;
  4270. }
  4271. memset(dmabuf->virt, 0, dma_size);
  4272. /*
  4273. * The SLI4 implementation of READ_REV conflicts at word1,
  4274. * bits 31:16 and SLI4 adds vpd functionality not present
  4275. * in SLI3. This code corrects the conflicts.
  4276. */
  4277. lpfc_read_rev(phba, mboxq);
  4278. mqe = &mboxq->u.mqe;
  4279. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4280. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4281. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4282. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4283. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4284. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4285. if (rc) {
  4286. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4287. dmabuf->virt, dmabuf->phys);
  4288. kfree(dmabuf);
  4289. return -EIO;
  4290. }
  4291. /*
  4292. * The available vpd length cannot be bigger than the
  4293. * DMA buffer passed to the port. Catch the less than
  4294. * case and update the caller's size.
  4295. */
  4296. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4297. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4298. memcpy(vpd, dmabuf->virt, *vpd_size);
  4299. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4300. dmabuf->virt, dmabuf->phys);
  4301. kfree(dmabuf);
  4302. return 0;
  4303. }
  4304. /**
  4305. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4306. * @phba: pointer to lpfc hba data structure.
  4307. *
  4308. * This routine retrieves SLI4 device physical port name this PCI function
  4309. * is attached to.
  4310. *
  4311. * Return codes
  4312. * 0 - sucessful
  4313. * otherwise - failed to retrieve physical port name
  4314. **/
  4315. static int
  4316. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4317. {
  4318. LPFC_MBOXQ_t *mboxq;
  4319. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4320. struct lpfc_controller_attribute *cntl_attr;
  4321. struct lpfc_mbx_get_port_name *get_port_name;
  4322. void *virtaddr = NULL;
  4323. uint32_t alloclen, reqlen;
  4324. uint32_t shdr_status, shdr_add_status;
  4325. union lpfc_sli4_cfg_shdr *shdr;
  4326. char cport_name = 0;
  4327. int rc;
  4328. /* We assume nothing at this point */
  4329. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4330. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4331. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4332. if (!mboxq)
  4333. return -ENOMEM;
  4334. /* obtain link type and link number via READ_CONFIG */
  4335. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4336. lpfc_sli4_read_config(phba);
  4337. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4338. goto retrieve_ppname;
  4339. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4340. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4341. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4342. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4343. LPFC_SLI4_MBX_NEMBED);
  4344. if (alloclen < reqlen) {
  4345. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4346. "3084 Allocated DMA memory size (%d) is "
  4347. "less than the requested DMA memory size "
  4348. "(%d)\n", alloclen, reqlen);
  4349. rc = -ENOMEM;
  4350. goto out_free_mboxq;
  4351. }
  4352. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4353. virtaddr = mboxq->sge_array->addr[0];
  4354. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4355. shdr = &mbx_cntl_attr->cfg_shdr;
  4356. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4357. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4358. if (shdr_status || shdr_add_status || rc) {
  4359. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4360. "3085 Mailbox x%x (x%x/x%x) failed, "
  4361. "rc:x%x, status:x%x, add_status:x%x\n",
  4362. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4363. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4364. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4365. rc, shdr_status, shdr_add_status);
  4366. rc = -ENXIO;
  4367. goto out_free_mboxq;
  4368. }
  4369. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4370. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4371. phba->sli4_hba.lnk_info.lnk_tp =
  4372. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4373. phba->sli4_hba.lnk_info.lnk_no =
  4374. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4375. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4376. "3086 lnk_type:%d, lnk_numb:%d\n",
  4377. phba->sli4_hba.lnk_info.lnk_tp,
  4378. phba->sli4_hba.lnk_info.lnk_no);
  4379. retrieve_ppname:
  4380. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4381. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4382. sizeof(struct lpfc_mbx_get_port_name) -
  4383. sizeof(struct lpfc_sli4_cfg_mhdr),
  4384. LPFC_SLI4_MBX_EMBED);
  4385. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4386. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4387. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4388. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4389. phba->sli4_hba.lnk_info.lnk_tp);
  4390. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4391. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4392. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4393. if (shdr_status || shdr_add_status || rc) {
  4394. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4395. "3087 Mailbox x%x (x%x/x%x) failed: "
  4396. "rc:x%x, status:x%x, add_status:x%x\n",
  4397. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4398. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4399. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4400. rc, shdr_status, shdr_add_status);
  4401. rc = -ENXIO;
  4402. goto out_free_mboxq;
  4403. }
  4404. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4405. case LPFC_LINK_NUMBER_0:
  4406. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4407. &get_port_name->u.response);
  4408. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4409. break;
  4410. case LPFC_LINK_NUMBER_1:
  4411. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4412. &get_port_name->u.response);
  4413. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4414. break;
  4415. case LPFC_LINK_NUMBER_2:
  4416. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4417. &get_port_name->u.response);
  4418. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4419. break;
  4420. case LPFC_LINK_NUMBER_3:
  4421. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4422. &get_port_name->u.response);
  4423. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4424. break;
  4425. default:
  4426. break;
  4427. }
  4428. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4429. phba->Port[0] = cport_name;
  4430. phba->Port[1] = '\0';
  4431. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4432. "3091 SLI get port name: %s\n", phba->Port);
  4433. }
  4434. out_free_mboxq:
  4435. if (rc != MBX_TIMEOUT) {
  4436. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4437. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4438. else
  4439. mempool_free(mboxq, phba->mbox_mem_pool);
  4440. }
  4441. return rc;
  4442. }
  4443. /**
  4444. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4445. * @phba: pointer to lpfc hba data structure.
  4446. *
  4447. * This routine is called to explicitly arm the SLI4 device's completion and
  4448. * event queues
  4449. **/
  4450. static void
  4451. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4452. {
  4453. uint8_t fcp_eqidx;
  4454. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4455. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4456. fcp_eqidx = 0;
  4457. if (phba->sli4_hba.fcp_cq) {
  4458. do {
  4459. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4460. LPFC_QUEUE_REARM);
  4461. } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
  4462. }
  4463. if (phba->sli4_hba.hba_eq) {
  4464. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
  4465. fcp_eqidx++)
  4466. lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
  4467. LPFC_QUEUE_REARM);
  4468. }
  4469. }
  4470. /**
  4471. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4472. * @phba: Pointer to HBA context object.
  4473. * @type: The resource extent type.
  4474. * @extnt_count: buffer to hold port available extent count.
  4475. * @extnt_size: buffer to hold element count per extent.
  4476. *
  4477. * This function calls the port and retrievs the number of available
  4478. * extents and their size for a particular extent type.
  4479. *
  4480. * Returns: 0 if successful. Nonzero otherwise.
  4481. **/
  4482. int
  4483. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4484. uint16_t *extnt_count, uint16_t *extnt_size)
  4485. {
  4486. int rc = 0;
  4487. uint32_t length;
  4488. uint32_t mbox_tmo;
  4489. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4490. LPFC_MBOXQ_t *mbox;
  4491. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4492. if (!mbox)
  4493. return -ENOMEM;
  4494. /* Find out how many extents are available for this resource type */
  4495. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4496. sizeof(struct lpfc_sli4_cfg_mhdr));
  4497. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4498. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4499. length, LPFC_SLI4_MBX_EMBED);
  4500. /* Send an extents count of 0 - the GET doesn't use it. */
  4501. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4502. LPFC_SLI4_MBX_EMBED);
  4503. if (unlikely(rc)) {
  4504. rc = -EIO;
  4505. goto err_exit;
  4506. }
  4507. if (!phba->sli4_hba.intr_enable)
  4508. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4509. else {
  4510. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4511. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4512. }
  4513. if (unlikely(rc)) {
  4514. rc = -EIO;
  4515. goto err_exit;
  4516. }
  4517. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4518. if (bf_get(lpfc_mbox_hdr_status,
  4519. &rsrc_info->header.cfg_shdr.response)) {
  4520. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4521. "2930 Failed to get resource extents "
  4522. "Status 0x%x Add'l Status 0x%x\n",
  4523. bf_get(lpfc_mbox_hdr_status,
  4524. &rsrc_info->header.cfg_shdr.response),
  4525. bf_get(lpfc_mbox_hdr_add_status,
  4526. &rsrc_info->header.cfg_shdr.response));
  4527. rc = -EIO;
  4528. goto err_exit;
  4529. }
  4530. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4531. &rsrc_info->u.rsp);
  4532. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4533. &rsrc_info->u.rsp);
  4534. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4535. "3162 Retrieved extents type-%d from port: count:%d, "
  4536. "size:%d\n", type, *extnt_count, *extnt_size);
  4537. err_exit:
  4538. mempool_free(mbox, phba->mbox_mem_pool);
  4539. return rc;
  4540. }
  4541. /**
  4542. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4543. * @phba: Pointer to HBA context object.
  4544. * @type: The extent type to check.
  4545. *
  4546. * This function reads the current available extents from the port and checks
  4547. * if the extent count or extent size has changed since the last access.
  4548. * Callers use this routine post port reset to understand if there is a
  4549. * extent reprovisioning requirement.
  4550. *
  4551. * Returns:
  4552. * -Error: error indicates problem.
  4553. * 1: Extent count or size has changed.
  4554. * 0: No changes.
  4555. **/
  4556. static int
  4557. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4558. {
  4559. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4560. uint16_t size_diff, rsrc_ext_size;
  4561. int rc = 0;
  4562. struct lpfc_rsrc_blks *rsrc_entry;
  4563. struct list_head *rsrc_blk_list = NULL;
  4564. size_diff = 0;
  4565. curr_ext_cnt = 0;
  4566. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4567. &rsrc_ext_cnt,
  4568. &rsrc_ext_size);
  4569. if (unlikely(rc))
  4570. return -EIO;
  4571. switch (type) {
  4572. case LPFC_RSC_TYPE_FCOE_RPI:
  4573. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4574. break;
  4575. case LPFC_RSC_TYPE_FCOE_VPI:
  4576. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4577. break;
  4578. case LPFC_RSC_TYPE_FCOE_XRI:
  4579. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4580. break;
  4581. case LPFC_RSC_TYPE_FCOE_VFI:
  4582. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4583. break;
  4584. default:
  4585. break;
  4586. }
  4587. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4588. curr_ext_cnt++;
  4589. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4590. size_diff++;
  4591. }
  4592. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4593. rc = 1;
  4594. return rc;
  4595. }
  4596. /**
  4597. * lpfc_sli4_cfg_post_extnts -
  4598. * @phba: Pointer to HBA context object.
  4599. * @extnt_cnt - number of available extents.
  4600. * @type - the extent type (rpi, xri, vfi, vpi).
  4601. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4602. * @mbox - pointer to the caller's allocated mailbox structure.
  4603. *
  4604. * This function executes the extents allocation request. It also
  4605. * takes care of the amount of memory needed to allocate or get the
  4606. * allocated extents. It is the caller's responsibility to evaluate
  4607. * the response.
  4608. *
  4609. * Returns:
  4610. * -Error: Error value describes the condition found.
  4611. * 0: if successful
  4612. **/
  4613. static int
  4614. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  4615. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4616. {
  4617. int rc = 0;
  4618. uint32_t req_len;
  4619. uint32_t emb_len;
  4620. uint32_t alloc_len, mbox_tmo;
  4621. /* Calculate the total requested length of the dma memory */
  4622. req_len = extnt_cnt * sizeof(uint16_t);
  4623. /*
  4624. * Calculate the size of an embedded mailbox. The uint32_t
  4625. * accounts for extents-specific word.
  4626. */
  4627. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4628. sizeof(uint32_t);
  4629. /*
  4630. * Presume the allocation and response will fit into an embedded
  4631. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4632. */
  4633. *emb = LPFC_SLI4_MBX_EMBED;
  4634. if (req_len > emb_len) {
  4635. req_len = extnt_cnt * sizeof(uint16_t) +
  4636. sizeof(union lpfc_sli4_cfg_shdr) +
  4637. sizeof(uint32_t);
  4638. *emb = LPFC_SLI4_MBX_NEMBED;
  4639. }
  4640. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4641. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4642. req_len, *emb);
  4643. if (alloc_len < req_len) {
  4644. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4645. "2982 Allocated DMA memory size (x%x) is "
  4646. "less than the requested DMA memory "
  4647. "size (x%x)\n", alloc_len, req_len);
  4648. return -ENOMEM;
  4649. }
  4650. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  4651. if (unlikely(rc))
  4652. return -EIO;
  4653. if (!phba->sli4_hba.intr_enable)
  4654. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4655. else {
  4656. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4657. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4658. }
  4659. if (unlikely(rc))
  4660. rc = -EIO;
  4661. return rc;
  4662. }
  4663. /**
  4664. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4665. * @phba: Pointer to HBA context object.
  4666. * @type: The resource extent type to allocate.
  4667. *
  4668. * This function allocates the number of elements for the specified
  4669. * resource type.
  4670. **/
  4671. static int
  4672. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4673. {
  4674. bool emb = false;
  4675. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4676. uint16_t rsrc_id, rsrc_start, j, k;
  4677. uint16_t *ids;
  4678. int i, rc;
  4679. unsigned long longs;
  4680. unsigned long *bmask;
  4681. struct lpfc_rsrc_blks *rsrc_blks;
  4682. LPFC_MBOXQ_t *mbox;
  4683. uint32_t length;
  4684. struct lpfc_id_range *id_array = NULL;
  4685. void *virtaddr = NULL;
  4686. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4687. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4688. struct list_head *ext_blk_list;
  4689. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4690. &rsrc_cnt,
  4691. &rsrc_size);
  4692. if (unlikely(rc))
  4693. return -EIO;
  4694. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4695. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4696. "3009 No available Resource Extents "
  4697. "for resource type 0x%x: Count: 0x%x, "
  4698. "Size 0x%x\n", type, rsrc_cnt,
  4699. rsrc_size);
  4700. return -ENOMEM;
  4701. }
  4702. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  4703. "2903 Post resource extents type-0x%x: "
  4704. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  4705. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4706. if (!mbox)
  4707. return -ENOMEM;
  4708. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  4709. if (unlikely(rc)) {
  4710. rc = -EIO;
  4711. goto err_exit;
  4712. }
  4713. /*
  4714. * Figure out where the response is located. Then get local pointers
  4715. * to the response data. The port does not guarantee to respond to
  4716. * all extents counts request so update the local variable with the
  4717. * allocated count from the port.
  4718. */
  4719. if (emb == LPFC_SLI4_MBX_EMBED) {
  4720. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4721. id_array = &rsrc_ext->u.rsp.id[0];
  4722. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4723. } else {
  4724. virtaddr = mbox->sge_array->addr[0];
  4725. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4726. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4727. id_array = &n_rsrc->id;
  4728. }
  4729. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4730. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4731. /*
  4732. * Based on the resource size and count, correct the base and max
  4733. * resource values.
  4734. */
  4735. length = sizeof(struct lpfc_rsrc_blks);
  4736. switch (type) {
  4737. case LPFC_RSC_TYPE_FCOE_RPI:
  4738. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4739. sizeof(unsigned long),
  4740. GFP_KERNEL);
  4741. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4742. rc = -ENOMEM;
  4743. goto err_exit;
  4744. }
  4745. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4746. sizeof(uint16_t),
  4747. GFP_KERNEL);
  4748. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4749. kfree(phba->sli4_hba.rpi_bmask);
  4750. rc = -ENOMEM;
  4751. goto err_exit;
  4752. }
  4753. /*
  4754. * The next_rpi was initialized with the maximum available
  4755. * count but the port may allocate a smaller number. Catch
  4756. * that case and update the next_rpi.
  4757. */
  4758. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4759. /* Initialize local ptrs for common extent processing later. */
  4760. bmask = phba->sli4_hba.rpi_bmask;
  4761. ids = phba->sli4_hba.rpi_ids;
  4762. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4763. break;
  4764. case LPFC_RSC_TYPE_FCOE_VPI:
  4765. phba->vpi_bmask = kzalloc(longs *
  4766. sizeof(unsigned long),
  4767. GFP_KERNEL);
  4768. if (unlikely(!phba->vpi_bmask)) {
  4769. rc = -ENOMEM;
  4770. goto err_exit;
  4771. }
  4772. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4773. sizeof(uint16_t),
  4774. GFP_KERNEL);
  4775. if (unlikely(!phba->vpi_ids)) {
  4776. kfree(phba->vpi_bmask);
  4777. rc = -ENOMEM;
  4778. goto err_exit;
  4779. }
  4780. /* Initialize local ptrs for common extent processing later. */
  4781. bmask = phba->vpi_bmask;
  4782. ids = phba->vpi_ids;
  4783. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4784. break;
  4785. case LPFC_RSC_TYPE_FCOE_XRI:
  4786. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4787. sizeof(unsigned long),
  4788. GFP_KERNEL);
  4789. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4790. rc = -ENOMEM;
  4791. goto err_exit;
  4792. }
  4793. phba->sli4_hba.max_cfg_param.xri_used = 0;
  4794. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4795. sizeof(uint16_t),
  4796. GFP_KERNEL);
  4797. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4798. kfree(phba->sli4_hba.xri_bmask);
  4799. rc = -ENOMEM;
  4800. goto err_exit;
  4801. }
  4802. /* Initialize local ptrs for common extent processing later. */
  4803. bmask = phba->sli4_hba.xri_bmask;
  4804. ids = phba->sli4_hba.xri_ids;
  4805. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4806. break;
  4807. case LPFC_RSC_TYPE_FCOE_VFI:
  4808. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4809. sizeof(unsigned long),
  4810. GFP_KERNEL);
  4811. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4812. rc = -ENOMEM;
  4813. goto err_exit;
  4814. }
  4815. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4816. sizeof(uint16_t),
  4817. GFP_KERNEL);
  4818. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4819. kfree(phba->sli4_hba.vfi_bmask);
  4820. rc = -ENOMEM;
  4821. goto err_exit;
  4822. }
  4823. /* Initialize local ptrs for common extent processing later. */
  4824. bmask = phba->sli4_hba.vfi_bmask;
  4825. ids = phba->sli4_hba.vfi_ids;
  4826. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4827. break;
  4828. default:
  4829. /* Unsupported Opcode. Fail call. */
  4830. id_array = NULL;
  4831. bmask = NULL;
  4832. ids = NULL;
  4833. ext_blk_list = NULL;
  4834. goto err_exit;
  4835. }
  4836. /*
  4837. * Complete initializing the extent configuration with the
  4838. * allocated ids assigned to this function. The bitmask serves
  4839. * as an index into the array and manages the available ids. The
  4840. * array just stores the ids communicated to the port via the wqes.
  4841. */
  4842. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4843. if ((i % 2) == 0)
  4844. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4845. &id_array[k]);
  4846. else
  4847. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4848. &id_array[k]);
  4849. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4850. if (unlikely(!rsrc_blks)) {
  4851. rc = -ENOMEM;
  4852. kfree(bmask);
  4853. kfree(ids);
  4854. goto err_exit;
  4855. }
  4856. rsrc_blks->rsrc_start = rsrc_id;
  4857. rsrc_blks->rsrc_size = rsrc_size;
  4858. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4859. rsrc_start = rsrc_id;
  4860. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4861. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4862. lpfc_sli4_get_els_iocb_cnt(phba);
  4863. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4864. ids[j] = rsrc_id;
  4865. rsrc_id++;
  4866. j++;
  4867. }
  4868. /* Entire word processed. Get next word.*/
  4869. if ((i % 2) == 1)
  4870. k++;
  4871. }
  4872. err_exit:
  4873. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4874. return rc;
  4875. }
  4876. /**
  4877. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4878. * @phba: Pointer to HBA context object.
  4879. * @type: the extent's type.
  4880. *
  4881. * This function deallocates all extents of a particular resource type.
  4882. * SLI4 does not allow for deallocating a particular extent range. It
  4883. * is the caller's responsibility to release all kernel memory resources.
  4884. **/
  4885. static int
  4886. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4887. {
  4888. int rc;
  4889. uint32_t length, mbox_tmo = 0;
  4890. LPFC_MBOXQ_t *mbox;
  4891. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4892. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4893. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4894. if (!mbox)
  4895. return -ENOMEM;
  4896. /*
  4897. * This function sends an embedded mailbox because it only sends the
  4898. * the resource type. All extents of this type are released by the
  4899. * port.
  4900. */
  4901. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4902. sizeof(struct lpfc_sli4_cfg_mhdr));
  4903. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4904. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4905. length, LPFC_SLI4_MBX_EMBED);
  4906. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4907. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4908. LPFC_SLI4_MBX_EMBED);
  4909. if (unlikely(rc)) {
  4910. rc = -EIO;
  4911. goto out_free_mbox;
  4912. }
  4913. if (!phba->sli4_hba.intr_enable)
  4914. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4915. else {
  4916. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4917. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4918. }
  4919. if (unlikely(rc)) {
  4920. rc = -EIO;
  4921. goto out_free_mbox;
  4922. }
  4923. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4924. if (bf_get(lpfc_mbox_hdr_status,
  4925. &dealloc_rsrc->header.cfg_shdr.response)) {
  4926. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4927. "2919 Failed to release resource extents "
  4928. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4929. "Resource memory not released.\n",
  4930. type,
  4931. bf_get(lpfc_mbox_hdr_status,
  4932. &dealloc_rsrc->header.cfg_shdr.response),
  4933. bf_get(lpfc_mbox_hdr_add_status,
  4934. &dealloc_rsrc->header.cfg_shdr.response));
  4935. rc = -EIO;
  4936. goto out_free_mbox;
  4937. }
  4938. /* Release kernel memory resources for the specific type. */
  4939. switch (type) {
  4940. case LPFC_RSC_TYPE_FCOE_VPI:
  4941. kfree(phba->vpi_bmask);
  4942. kfree(phba->vpi_ids);
  4943. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4944. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4945. &phba->lpfc_vpi_blk_list, list) {
  4946. list_del_init(&rsrc_blk->list);
  4947. kfree(rsrc_blk);
  4948. }
  4949. break;
  4950. case LPFC_RSC_TYPE_FCOE_XRI:
  4951. kfree(phba->sli4_hba.xri_bmask);
  4952. kfree(phba->sli4_hba.xri_ids);
  4953. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4954. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4955. list_del_init(&rsrc_blk->list);
  4956. kfree(rsrc_blk);
  4957. }
  4958. break;
  4959. case LPFC_RSC_TYPE_FCOE_VFI:
  4960. kfree(phba->sli4_hba.vfi_bmask);
  4961. kfree(phba->sli4_hba.vfi_ids);
  4962. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4963. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4964. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  4965. list_del_init(&rsrc_blk->list);
  4966. kfree(rsrc_blk);
  4967. }
  4968. break;
  4969. case LPFC_RSC_TYPE_FCOE_RPI:
  4970. /* RPI bitmask and physical id array are cleaned up earlier. */
  4971. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4972. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  4973. list_del_init(&rsrc_blk->list);
  4974. kfree(rsrc_blk);
  4975. }
  4976. break;
  4977. default:
  4978. break;
  4979. }
  4980. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4981. out_free_mbox:
  4982. mempool_free(mbox, phba->mbox_mem_pool);
  4983. return rc;
  4984. }
  4985. /**
  4986. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  4987. * @phba: Pointer to HBA context object.
  4988. *
  4989. * This function allocates all SLI4 resource identifiers.
  4990. **/
  4991. int
  4992. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  4993. {
  4994. int i, rc, error = 0;
  4995. uint16_t count, base;
  4996. unsigned long longs;
  4997. if (!phba->sli4_hba.rpi_hdrs_in_use)
  4998. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  4999. if (phba->sli4_hba.extents_in_use) {
  5000. /*
  5001. * The port supports resource extents. The XRI, VPI, VFI, RPI
  5002. * resource extent count must be read and allocated before
  5003. * provisioning the resource id arrays.
  5004. */
  5005. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5006. LPFC_IDX_RSRC_RDY) {
  5007. /*
  5008. * Extent-based resources are set - the driver could
  5009. * be in a port reset. Figure out if any corrective
  5010. * actions need to be taken.
  5011. */
  5012. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5013. LPFC_RSC_TYPE_FCOE_VFI);
  5014. if (rc != 0)
  5015. error++;
  5016. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5017. LPFC_RSC_TYPE_FCOE_VPI);
  5018. if (rc != 0)
  5019. error++;
  5020. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5021. LPFC_RSC_TYPE_FCOE_XRI);
  5022. if (rc != 0)
  5023. error++;
  5024. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5025. LPFC_RSC_TYPE_FCOE_RPI);
  5026. if (rc != 0)
  5027. error++;
  5028. /*
  5029. * It's possible that the number of resources
  5030. * provided to this port instance changed between
  5031. * resets. Detect this condition and reallocate
  5032. * resources. Otherwise, there is no action.
  5033. */
  5034. if (error) {
  5035. lpfc_printf_log(phba, KERN_INFO,
  5036. LOG_MBOX | LOG_INIT,
  5037. "2931 Detected extent resource "
  5038. "change. Reallocating all "
  5039. "extents.\n");
  5040. rc = lpfc_sli4_dealloc_extent(phba,
  5041. LPFC_RSC_TYPE_FCOE_VFI);
  5042. rc = lpfc_sli4_dealloc_extent(phba,
  5043. LPFC_RSC_TYPE_FCOE_VPI);
  5044. rc = lpfc_sli4_dealloc_extent(phba,
  5045. LPFC_RSC_TYPE_FCOE_XRI);
  5046. rc = lpfc_sli4_dealloc_extent(phba,
  5047. LPFC_RSC_TYPE_FCOE_RPI);
  5048. } else
  5049. return 0;
  5050. }
  5051. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5052. if (unlikely(rc))
  5053. goto err_exit;
  5054. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5055. if (unlikely(rc))
  5056. goto err_exit;
  5057. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5058. if (unlikely(rc))
  5059. goto err_exit;
  5060. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5061. if (unlikely(rc))
  5062. goto err_exit;
  5063. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5064. LPFC_IDX_RSRC_RDY);
  5065. return rc;
  5066. } else {
  5067. /*
  5068. * The port does not support resource extents. The XRI, VPI,
  5069. * VFI, RPI resource ids were determined from READ_CONFIG.
  5070. * Just allocate the bitmasks and provision the resource id
  5071. * arrays. If a port reset is active, the resources don't
  5072. * need any action - just exit.
  5073. */
  5074. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5075. LPFC_IDX_RSRC_RDY) {
  5076. lpfc_sli4_dealloc_resource_identifiers(phba);
  5077. lpfc_sli4_remove_rpis(phba);
  5078. }
  5079. /* RPIs. */
  5080. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5081. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5082. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5083. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5084. sizeof(unsigned long),
  5085. GFP_KERNEL);
  5086. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5087. rc = -ENOMEM;
  5088. goto err_exit;
  5089. }
  5090. phba->sli4_hba.rpi_ids = kzalloc(count *
  5091. sizeof(uint16_t),
  5092. GFP_KERNEL);
  5093. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5094. rc = -ENOMEM;
  5095. goto free_rpi_bmask;
  5096. }
  5097. for (i = 0; i < count; i++)
  5098. phba->sli4_hba.rpi_ids[i] = base + i;
  5099. /* VPIs. */
  5100. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5101. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5102. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5103. phba->vpi_bmask = kzalloc(longs *
  5104. sizeof(unsigned long),
  5105. GFP_KERNEL);
  5106. if (unlikely(!phba->vpi_bmask)) {
  5107. rc = -ENOMEM;
  5108. goto free_rpi_ids;
  5109. }
  5110. phba->vpi_ids = kzalloc(count *
  5111. sizeof(uint16_t),
  5112. GFP_KERNEL);
  5113. if (unlikely(!phba->vpi_ids)) {
  5114. rc = -ENOMEM;
  5115. goto free_vpi_bmask;
  5116. }
  5117. for (i = 0; i < count; i++)
  5118. phba->vpi_ids[i] = base + i;
  5119. /* XRIs. */
  5120. count = phba->sli4_hba.max_cfg_param.max_xri;
  5121. base = phba->sli4_hba.max_cfg_param.xri_base;
  5122. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5123. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5124. sizeof(unsigned long),
  5125. GFP_KERNEL);
  5126. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5127. rc = -ENOMEM;
  5128. goto free_vpi_ids;
  5129. }
  5130. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5131. phba->sli4_hba.xri_ids = kzalloc(count *
  5132. sizeof(uint16_t),
  5133. GFP_KERNEL);
  5134. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5135. rc = -ENOMEM;
  5136. goto free_xri_bmask;
  5137. }
  5138. for (i = 0; i < count; i++)
  5139. phba->sli4_hba.xri_ids[i] = base + i;
  5140. /* VFIs. */
  5141. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5142. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5143. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5144. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5145. sizeof(unsigned long),
  5146. GFP_KERNEL);
  5147. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5148. rc = -ENOMEM;
  5149. goto free_xri_ids;
  5150. }
  5151. phba->sli4_hba.vfi_ids = kzalloc(count *
  5152. sizeof(uint16_t),
  5153. GFP_KERNEL);
  5154. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5155. rc = -ENOMEM;
  5156. goto free_vfi_bmask;
  5157. }
  5158. for (i = 0; i < count; i++)
  5159. phba->sli4_hba.vfi_ids[i] = base + i;
  5160. /*
  5161. * Mark all resources ready. An HBA reset doesn't need
  5162. * to reset the initialization.
  5163. */
  5164. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5165. LPFC_IDX_RSRC_RDY);
  5166. return 0;
  5167. }
  5168. free_vfi_bmask:
  5169. kfree(phba->sli4_hba.vfi_bmask);
  5170. free_xri_ids:
  5171. kfree(phba->sli4_hba.xri_ids);
  5172. free_xri_bmask:
  5173. kfree(phba->sli4_hba.xri_bmask);
  5174. free_vpi_ids:
  5175. kfree(phba->vpi_ids);
  5176. free_vpi_bmask:
  5177. kfree(phba->vpi_bmask);
  5178. free_rpi_ids:
  5179. kfree(phba->sli4_hba.rpi_ids);
  5180. free_rpi_bmask:
  5181. kfree(phba->sli4_hba.rpi_bmask);
  5182. err_exit:
  5183. return rc;
  5184. }
  5185. /**
  5186. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5187. * @phba: Pointer to HBA context object.
  5188. *
  5189. * This function allocates the number of elements for the specified
  5190. * resource type.
  5191. **/
  5192. int
  5193. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5194. {
  5195. if (phba->sli4_hba.extents_in_use) {
  5196. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5197. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5198. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5199. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5200. } else {
  5201. kfree(phba->vpi_bmask);
  5202. kfree(phba->vpi_ids);
  5203. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5204. kfree(phba->sli4_hba.xri_bmask);
  5205. kfree(phba->sli4_hba.xri_ids);
  5206. kfree(phba->sli4_hba.vfi_bmask);
  5207. kfree(phba->sli4_hba.vfi_ids);
  5208. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5209. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5210. }
  5211. return 0;
  5212. }
  5213. /**
  5214. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5215. * @phba: Pointer to HBA context object.
  5216. * @type: The resource extent type.
  5217. * @extnt_count: buffer to hold port extent count response
  5218. * @extnt_size: buffer to hold port extent size response.
  5219. *
  5220. * This function calls the port to read the host allocated extents
  5221. * for a particular type.
  5222. **/
  5223. int
  5224. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5225. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5226. {
  5227. bool emb;
  5228. int rc = 0;
  5229. uint16_t curr_blks = 0;
  5230. uint32_t req_len, emb_len;
  5231. uint32_t alloc_len, mbox_tmo;
  5232. struct list_head *blk_list_head;
  5233. struct lpfc_rsrc_blks *rsrc_blk;
  5234. LPFC_MBOXQ_t *mbox;
  5235. void *virtaddr = NULL;
  5236. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5237. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5238. union lpfc_sli4_cfg_shdr *shdr;
  5239. switch (type) {
  5240. case LPFC_RSC_TYPE_FCOE_VPI:
  5241. blk_list_head = &phba->lpfc_vpi_blk_list;
  5242. break;
  5243. case LPFC_RSC_TYPE_FCOE_XRI:
  5244. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5245. break;
  5246. case LPFC_RSC_TYPE_FCOE_VFI:
  5247. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5248. break;
  5249. case LPFC_RSC_TYPE_FCOE_RPI:
  5250. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5251. break;
  5252. default:
  5253. return -EIO;
  5254. }
  5255. /* Count the number of extents currently allocatd for this type. */
  5256. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5257. if (curr_blks == 0) {
  5258. /*
  5259. * The GET_ALLOCATED mailbox does not return the size,
  5260. * just the count. The size should be just the size
  5261. * stored in the current allocated block and all sizes
  5262. * for an extent type are the same so set the return
  5263. * value now.
  5264. */
  5265. *extnt_size = rsrc_blk->rsrc_size;
  5266. }
  5267. curr_blks++;
  5268. }
  5269. /* Calculate the total requested length of the dma memory. */
  5270. req_len = curr_blks * sizeof(uint16_t);
  5271. /*
  5272. * Calculate the size of an embedded mailbox. The uint32_t
  5273. * accounts for extents-specific word.
  5274. */
  5275. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5276. sizeof(uint32_t);
  5277. /*
  5278. * Presume the allocation and response will fit into an embedded
  5279. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5280. */
  5281. emb = LPFC_SLI4_MBX_EMBED;
  5282. req_len = emb_len;
  5283. if (req_len > emb_len) {
  5284. req_len = curr_blks * sizeof(uint16_t) +
  5285. sizeof(union lpfc_sli4_cfg_shdr) +
  5286. sizeof(uint32_t);
  5287. emb = LPFC_SLI4_MBX_NEMBED;
  5288. }
  5289. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5290. if (!mbox)
  5291. return -ENOMEM;
  5292. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5293. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5294. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5295. req_len, emb);
  5296. if (alloc_len < req_len) {
  5297. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5298. "2983 Allocated DMA memory size (x%x) is "
  5299. "less than the requested DMA memory "
  5300. "size (x%x)\n", alloc_len, req_len);
  5301. rc = -ENOMEM;
  5302. goto err_exit;
  5303. }
  5304. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5305. if (unlikely(rc)) {
  5306. rc = -EIO;
  5307. goto err_exit;
  5308. }
  5309. if (!phba->sli4_hba.intr_enable)
  5310. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5311. else {
  5312. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5313. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5314. }
  5315. if (unlikely(rc)) {
  5316. rc = -EIO;
  5317. goto err_exit;
  5318. }
  5319. /*
  5320. * Figure out where the response is located. Then get local pointers
  5321. * to the response data. The port does not guarantee to respond to
  5322. * all extents counts request so update the local variable with the
  5323. * allocated count from the port.
  5324. */
  5325. if (emb == LPFC_SLI4_MBX_EMBED) {
  5326. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5327. shdr = &rsrc_ext->header.cfg_shdr;
  5328. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5329. } else {
  5330. virtaddr = mbox->sge_array->addr[0];
  5331. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5332. shdr = &n_rsrc->cfg_shdr;
  5333. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5334. }
  5335. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5336. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5337. "2984 Failed to read allocated resources "
  5338. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5339. type,
  5340. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5341. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5342. rc = -EIO;
  5343. goto err_exit;
  5344. }
  5345. err_exit:
  5346. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5347. return rc;
  5348. }
  5349. /**
  5350. * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
  5351. * @phba: pointer to lpfc hba data structure.
  5352. *
  5353. * This routine walks the list of els buffers that have been allocated and
  5354. * repost them to the port by using SGL block post. This is needed after a
  5355. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5356. * of els buffer sgls which contains contiguous xris and uses the non-embedded
  5357. * SGL block post mailbox commands to post them to the port. For single els
  5358. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5359. * mailbox command for posting.
  5360. *
  5361. * Returns: 0 = success, non-zero failure.
  5362. **/
  5363. static int
  5364. lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
  5365. {
  5366. struct lpfc_sglq *sglq_entry = NULL;
  5367. struct lpfc_sglq *sglq_entry_next = NULL;
  5368. struct lpfc_sglq *sglq_entry_first = NULL;
  5369. int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
  5370. int last_xritag = NO_XRI;
  5371. LIST_HEAD(prep_sgl_list);
  5372. LIST_HEAD(blck_sgl_list);
  5373. LIST_HEAD(allc_sgl_list);
  5374. LIST_HEAD(post_sgl_list);
  5375. LIST_HEAD(free_sgl_list);
  5376. spin_lock(&phba->hbalock);
  5377. list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
  5378. spin_unlock(&phba->hbalock);
  5379. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  5380. &allc_sgl_list, list) {
  5381. list_del_init(&sglq_entry->list);
  5382. block_cnt++;
  5383. if ((last_xritag != NO_XRI) &&
  5384. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  5385. /* a hole in xri block, form a sgl posting block */
  5386. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  5387. post_cnt = block_cnt - 1;
  5388. /* prepare list for next posting block */
  5389. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5390. block_cnt = 1;
  5391. } else {
  5392. /* prepare list for next posting block */
  5393. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5394. /* enough sgls for non-embed sgl mbox command */
  5395. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  5396. list_splice_init(&prep_sgl_list,
  5397. &blck_sgl_list);
  5398. post_cnt = block_cnt;
  5399. block_cnt = 0;
  5400. }
  5401. }
  5402. num_posted++;
  5403. /* keep track of last sgl's xritag */
  5404. last_xritag = sglq_entry->sli4_xritag;
  5405. /* end of repost sgl list condition for els buffers */
  5406. if (num_posted == phba->sli4_hba.els_xri_cnt) {
  5407. if (post_cnt == 0) {
  5408. list_splice_init(&prep_sgl_list,
  5409. &blck_sgl_list);
  5410. post_cnt = block_cnt;
  5411. } else if (block_cnt == 1) {
  5412. status = lpfc_sli4_post_sgl(phba,
  5413. sglq_entry->phys, 0,
  5414. sglq_entry->sli4_xritag);
  5415. if (!status) {
  5416. /* successful, put sgl to posted list */
  5417. list_add_tail(&sglq_entry->list,
  5418. &post_sgl_list);
  5419. } else {
  5420. /* Failure, put sgl to free list */
  5421. lpfc_printf_log(phba, KERN_WARNING,
  5422. LOG_SLI,
  5423. "3159 Failed to post els "
  5424. "sgl, xritag:x%x\n",
  5425. sglq_entry->sli4_xritag);
  5426. list_add_tail(&sglq_entry->list,
  5427. &free_sgl_list);
  5428. spin_lock_irq(&phba->hbalock);
  5429. phba->sli4_hba.els_xri_cnt--;
  5430. spin_unlock_irq(&phba->hbalock);
  5431. }
  5432. }
  5433. }
  5434. /* continue until a nembed page worth of sgls */
  5435. if (post_cnt == 0)
  5436. continue;
  5437. /* post the els buffer list sgls as a block */
  5438. status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
  5439. post_cnt);
  5440. if (!status) {
  5441. /* success, put sgl list to posted sgl list */
  5442. list_splice_init(&blck_sgl_list, &post_sgl_list);
  5443. } else {
  5444. /* Failure, put sgl list to free sgl list */
  5445. sglq_entry_first = list_first_entry(&blck_sgl_list,
  5446. struct lpfc_sglq,
  5447. list);
  5448. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5449. "3160 Failed to post els sgl-list, "
  5450. "xritag:x%x-x%x\n",
  5451. sglq_entry_first->sli4_xritag,
  5452. (sglq_entry_first->sli4_xritag +
  5453. post_cnt - 1));
  5454. list_splice_init(&blck_sgl_list, &free_sgl_list);
  5455. spin_lock_irq(&phba->hbalock);
  5456. phba->sli4_hba.els_xri_cnt -= post_cnt;
  5457. spin_unlock_irq(&phba->hbalock);
  5458. }
  5459. /* don't reset xirtag due to hole in xri block */
  5460. if (block_cnt == 0)
  5461. last_xritag = NO_XRI;
  5462. /* reset els sgl post count for next round of posting */
  5463. post_cnt = 0;
  5464. }
  5465. /* free the els sgls failed to post */
  5466. lpfc_free_sgl_list(phba, &free_sgl_list);
  5467. /* push els sgls posted to the availble list */
  5468. if (!list_empty(&post_sgl_list)) {
  5469. spin_lock(&phba->hbalock);
  5470. list_splice_init(&post_sgl_list,
  5471. &phba->sli4_hba.lpfc_sgl_list);
  5472. spin_unlock(&phba->hbalock);
  5473. } else {
  5474. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5475. "3161 Failure to post els sgl to port.\n");
  5476. return -EIO;
  5477. }
  5478. return 0;
  5479. }
  5480. /**
  5481. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5482. * @phba: Pointer to HBA context object.
  5483. *
  5484. * This function is the main SLI4 device intialization PCI function. This
  5485. * function is called by the HBA intialization code, HBA reset code and
  5486. * HBA error attention handler code. Caller is not required to hold any
  5487. * locks.
  5488. **/
  5489. int
  5490. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5491. {
  5492. int rc;
  5493. LPFC_MBOXQ_t *mboxq;
  5494. struct lpfc_mqe *mqe;
  5495. uint8_t *vpd;
  5496. uint32_t vpd_size;
  5497. uint32_t ftr_rsp = 0;
  5498. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5499. struct lpfc_vport *vport = phba->pport;
  5500. struct lpfc_dmabuf *mp;
  5501. /* Perform a PCI function reset to start from clean */
  5502. rc = lpfc_pci_function_reset(phba);
  5503. if (unlikely(rc))
  5504. return -ENODEV;
  5505. /* Check the HBA Host Status Register for readyness */
  5506. rc = lpfc_sli4_post_status_check(phba);
  5507. if (unlikely(rc))
  5508. return -ENODEV;
  5509. else {
  5510. spin_lock_irq(&phba->hbalock);
  5511. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5512. spin_unlock_irq(&phba->hbalock);
  5513. }
  5514. /*
  5515. * Allocate a single mailbox container for initializing the
  5516. * port.
  5517. */
  5518. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5519. if (!mboxq)
  5520. return -ENOMEM;
  5521. /* Issue READ_REV to collect vpd and FW information. */
  5522. vpd_size = SLI4_PAGE_SIZE;
  5523. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5524. if (!vpd) {
  5525. rc = -ENOMEM;
  5526. goto out_free_mbox;
  5527. }
  5528. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5529. if (unlikely(rc)) {
  5530. kfree(vpd);
  5531. goto out_free_mbox;
  5532. }
  5533. mqe = &mboxq->u.mqe;
  5534. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5535. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5536. phba->hba_flag |= HBA_FCOE_MODE;
  5537. else
  5538. phba->hba_flag &= ~HBA_FCOE_MODE;
  5539. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5540. LPFC_DCBX_CEE_MODE)
  5541. phba->hba_flag |= HBA_FIP_SUPPORT;
  5542. else
  5543. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5544. phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
  5545. if (phba->sli_rev != LPFC_SLI_REV4) {
  5546. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5547. "0376 READ_REV Error. SLI Level %d "
  5548. "FCoE enabled %d\n",
  5549. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5550. rc = -EIO;
  5551. kfree(vpd);
  5552. goto out_free_mbox;
  5553. }
  5554. /*
  5555. * Continue initialization with default values even if driver failed
  5556. * to read FCoE param config regions, only read parameters if the
  5557. * board is FCoE
  5558. */
  5559. if (phba->hba_flag & HBA_FCOE_MODE &&
  5560. lpfc_sli4_read_fcoe_params(phba))
  5561. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5562. "2570 Failed to read FCoE parameters\n");
  5563. /*
  5564. * Retrieve sli4 device physical port name, failure of doing it
  5565. * is considered as non-fatal.
  5566. */
  5567. rc = lpfc_sli4_retrieve_pport_name(phba);
  5568. if (!rc)
  5569. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5570. "3080 Successful retrieving SLI4 device "
  5571. "physical port name: %s.\n", phba->Port);
  5572. /*
  5573. * Evaluate the read rev and vpd data. Populate the driver
  5574. * state with the results. If this routine fails, the failure
  5575. * is not fatal as the driver will use generic values.
  5576. */
  5577. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5578. if (unlikely(!rc)) {
  5579. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5580. "0377 Error %d parsing vpd. "
  5581. "Using defaults.\n", rc);
  5582. rc = 0;
  5583. }
  5584. kfree(vpd);
  5585. /* Save information as VPD data */
  5586. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5587. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5588. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5589. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5590. &mqe->un.read_rev);
  5591. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5592. &mqe->un.read_rev);
  5593. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5594. &mqe->un.read_rev);
  5595. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5596. &mqe->un.read_rev);
  5597. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5598. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5599. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5600. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5601. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5602. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5603. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5604. "(%d):0380 READ_REV Status x%x "
  5605. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5606. mboxq->vport ? mboxq->vport->vpi : 0,
  5607. bf_get(lpfc_mqe_status, mqe),
  5608. phba->vpd.rev.opFwName,
  5609. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5610. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5611. /*
  5612. * Discover the port's supported feature set and match it against the
  5613. * hosts requests.
  5614. */
  5615. lpfc_request_features(phba, mboxq);
  5616. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5617. if (unlikely(rc)) {
  5618. rc = -EIO;
  5619. goto out_free_mbox;
  5620. }
  5621. /*
  5622. * The port must support FCP initiator mode as this is the
  5623. * only mode running in the host.
  5624. */
  5625. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5626. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5627. "0378 No support for fcpi mode.\n");
  5628. ftr_rsp++;
  5629. }
  5630. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5631. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5632. else
  5633. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5634. /*
  5635. * If the port cannot support the host's requested features
  5636. * then turn off the global config parameters to disable the
  5637. * feature in the driver. This is not a fatal error.
  5638. */
  5639. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5640. if (phba->cfg_enable_bg) {
  5641. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5642. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5643. else
  5644. ftr_rsp++;
  5645. }
  5646. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5647. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5648. ftr_rsp++;
  5649. if (ftr_rsp) {
  5650. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5651. "0379 Feature Mismatch Data: x%08x %08x "
  5652. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5653. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5654. phba->cfg_enable_npiv, phba->max_vpi);
  5655. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5656. phba->cfg_enable_bg = 0;
  5657. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5658. phba->cfg_enable_npiv = 0;
  5659. }
  5660. /* These SLI3 features are assumed in SLI4 */
  5661. spin_lock_irq(&phba->hbalock);
  5662. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5663. spin_unlock_irq(&phba->hbalock);
  5664. /*
  5665. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5666. * calls depends on these resources to complete port setup.
  5667. */
  5668. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5669. if (rc) {
  5670. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5671. "2920 Failed to alloc Resource IDs "
  5672. "rc = x%x\n", rc);
  5673. goto out_free_mbox;
  5674. }
  5675. /* Read the port's service parameters. */
  5676. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5677. if (rc) {
  5678. phba->link_state = LPFC_HBA_ERROR;
  5679. rc = -ENOMEM;
  5680. goto out_free_mbox;
  5681. }
  5682. mboxq->vport = vport;
  5683. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5684. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5685. if (rc == MBX_SUCCESS) {
  5686. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5687. rc = 0;
  5688. }
  5689. /*
  5690. * This memory was allocated by the lpfc_read_sparam routine. Release
  5691. * it to the mbuf pool.
  5692. */
  5693. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5694. kfree(mp);
  5695. mboxq->context1 = NULL;
  5696. if (unlikely(rc)) {
  5697. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5698. "0382 READ_SPARAM command failed "
  5699. "status %d, mbxStatus x%x\n",
  5700. rc, bf_get(lpfc_mqe_status, mqe));
  5701. phba->link_state = LPFC_HBA_ERROR;
  5702. rc = -EIO;
  5703. goto out_free_mbox;
  5704. }
  5705. lpfc_update_vport_wwn(vport);
  5706. /* Update the fc_host data structures with new wwn. */
  5707. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5708. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5709. /* update host els and scsi xri-sgl sizes and mappings */
  5710. rc = lpfc_sli4_xri_sgl_update(phba);
  5711. if (unlikely(rc)) {
  5712. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5713. "1400 Failed to update xri-sgl size and "
  5714. "mapping: %d\n", rc);
  5715. goto out_free_mbox;
  5716. }
  5717. /* register the els sgl pool to the port */
  5718. rc = lpfc_sli4_repost_els_sgl_list(phba);
  5719. if (unlikely(rc)) {
  5720. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5721. "0582 Error %d during els sgl post "
  5722. "operation\n", rc);
  5723. rc = -ENODEV;
  5724. goto out_free_mbox;
  5725. }
  5726. /* register the allocated scsi sgl pool to the port */
  5727. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5728. if (unlikely(rc)) {
  5729. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5730. "0383 Error %d during scsi sgl post "
  5731. "operation\n", rc);
  5732. /* Some Scsi buffers were moved to the abort scsi list */
  5733. /* A pci function reset will repost them */
  5734. rc = -ENODEV;
  5735. goto out_free_mbox;
  5736. }
  5737. /* Post the rpi header region to the device. */
  5738. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5739. if (unlikely(rc)) {
  5740. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5741. "0393 Error %d during rpi post operation\n",
  5742. rc);
  5743. rc = -ENODEV;
  5744. goto out_free_mbox;
  5745. }
  5746. lpfc_sli4_node_prep(phba);
  5747. /* Create all the SLI4 queues */
  5748. rc = lpfc_sli4_queue_create(phba);
  5749. if (rc) {
  5750. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5751. "3089 Failed to allocate queues\n");
  5752. rc = -ENODEV;
  5753. goto out_stop_timers;
  5754. }
  5755. /* Set up all the queues to the device */
  5756. rc = lpfc_sli4_queue_setup(phba);
  5757. if (unlikely(rc)) {
  5758. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5759. "0381 Error %d during queue setup.\n ", rc);
  5760. goto out_destroy_queue;
  5761. }
  5762. /* Arm the CQs and then EQs on device */
  5763. lpfc_sli4_arm_cqeq_intr(phba);
  5764. /* Indicate device interrupt mode */
  5765. phba->sli4_hba.intr_enable = 1;
  5766. /* Allow asynchronous mailbox command to go through */
  5767. spin_lock_irq(&phba->hbalock);
  5768. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5769. spin_unlock_irq(&phba->hbalock);
  5770. /* Post receive buffers to the device */
  5771. lpfc_sli4_rb_setup(phba);
  5772. /* Reset HBA FCF states after HBA reset */
  5773. phba->fcf.fcf_flag = 0;
  5774. phba->fcf.current_rec.flag = 0;
  5775. /* Start the ELS watchdog timer */
  5776. mod_timer(&vport->els_tmofunc,
  5777. jiffies + HZ * (phba->fc_ratov * 2));
  5778. /* Start heart beat timer */
  5779. mod_timer(&phba->hb_tmofunc,
  5780. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5781. phba->hb_outstanding = 0;
  5782. phba->last_completion_time = jiffies;
  5783. /* Start error attention (ERATT) polling timer */
  5784. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5785. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5786. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5787. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5788. if (!rc) {
  5789. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5790. "2829 This device supports "
  5791. "Advanced Error Reporting (AER)\n");
  5792. spin_lock_irq(&phba->hbalock);
  5793. phba->hba_flag |= HBA_AER_ENABLED;
  5794. spin_unlock_irq(&phba->hbalock);
  5795. } else {
  5796. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5797. "2830 This device does not support "
  5798. "Advanced Error Reporting (AER)\n");
  5799. phba->cfg_aer_support = 0;
  5800. }
  5801. rc = 0;
  5802. }
  5803. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5804. /*
  5805. * The FC Port needs to register FCFI (index 0)
  5806. */
  5807. lpfc_reg_fcfi(phba, mboxq);
  5808. mboxq->vport = phba->pport;
  5809. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5810. if (rc != MBX_SUCCESS)
  5811. goto out_unset_queue;
  5812. rc = 0;
  5813. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5814. &mboxq->u.mqe.un.reg_fcfi);
  5815. /* Check if the port is configured to be disabled */
  5816. lpfc_sli_read_link_ste(phba);
  5817. }
  5818. /*
  5819. * The port is ready, set the host's link state to LINK_DOWN
  5820. * in preparation for link interrupts.
  5821. */
  5822. spin_lock_irq(&phba->hbalock);
  5823. phba->link_state = LPFC_LINK_DOWN;
  5824. spin_unlock_irq(&phba->hbalock);
  5825. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5826. (phba->hba_flag & LINK_DISABLED)) {
  5827. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5828. "3103 Adapter Link is disabled.\n");
  5829. lpfc_down_link(phba, mboxq);
  5830. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5831. if (rc != MBX_SUCCESS) {
  5832. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5833. "3104 Adapter failed to issue "
  5834. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5835. goto out_unset_queue;
  5836. }
  5837. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5838. /* don't perform init_link on SLI4 FC port loopback test */
  5839. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5840. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5841. if (rc)
  5842. goto out_unset_queue;
  5843. }
  5844. }
  5845. mempool_free(mboxq, phba->mbox_mem_pool);
  5846. return rc;
  5847. out_unset_queue:
  5848. /* Unset all the queues set up in this routine when error out */
  5849. lpfc_sli4_queue_unset(phba);
  5850. out_destroy_queue:
  5851. lpfc_sli4_queue_destroy(phba);
  5852. out_stop_timers:
  5853. lpfc_stop_hba_timers(phba);
  5854. out_free_mbox:
  5855. mempool_free(mboxq, phba->mbox_mem_pool);
  5856. return rc;
  5857. }
  5858. /**
  5859. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5860. * @ptr: context object - pointer to hba structure.
  5861. *
  5862. * This is the callback function for mailbox timer. The mailbox
  5863. * timer is armed when a new mailbox command is issued and the timer
  5864. * is deleted when the mailbox complete. The function is called by
  5865. * the kernel timer code when a mailbox does not complete within
  5866. * expected time. This function wakes up the worker thread to
  5867. * process the mailbox timeout and returns. All the processing is
  5868. * done by the worker thread function lpfc_mbox_timeout_handler.
  5869. **/
  5870. void
  5871. lpfc_mbox_timeout(unsigned long ptr)
  5872. {
  5873. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5874. unsigned long iflag;
  5875. uint32_t tmo_posted;
  5876. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5877. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5878. if (!tmo_posted)
  5879. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5880. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5881. if (!tmo_posted)
  5882. lpfc_worker_wake_up(phba);
  5883. return;
  5884. }
  5885. /**
  5886. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5887. * @phba: Pointer to HBA context object.
  5888. *
  5889. * This function is called from worker thread when a mailbox command times out.
  5890. * The caller is not required to hold any locks. This function will reset the
  5891. * HBA and recover all the pending commands.
  5892. **/
  5893. void
  5894. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5895. {
  5896. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5897. MAILBOX_t *mb = &pmbox->u.mb;
  5898. struct lpfc_sli *psli = &phba->sli;
  5899. struct lpfc_sli_ring *pring;
  5900. /* Check the pmbox pointer first. There is a race condition
  5901. * between the mbox timeout handler getting executed in the
  5902. * worklist and the mailbox actually completing. When this
  5903. * race condition occurs, the mbox_active will be NULL.
  5904. */
  5905. spin_lock_irq(&phba->hbalock);
  5906. if (pmbox == NULL) {
  5907. lpfc_printf_log(phba, KERN_WARNING,
  5908. LOG_MBOX | LOG_SLI,
  5909. "0353 Active Mailbox cleared - mailbox timeout "
  5910. "exiting\n");
  5911. spin_unlock_irq(&phba->hbalock);
  5912. return;
  5913. }
  5914. /* Mbox cmd <mbxCommand> timeout */
  5915. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5916. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5917. mb->mbxCommand,
  5918. phba->pport->port_state,
  5919. phba->sli.sli_flag,
  5920. phba->sli.mbox_active);
  5921. spin_unlock_irq(&phba->hbalock);
  5922. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5923. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5924. * it to fail all outstanding SCSI IO.
  5925. */
  5926. spin_lock_irq(&phba->pport->work_port_lock);
  5927. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5928. spin_unlock_irq(&phba->pport->work_port_lock);
  5929. spin_lock_irq(&phba->hbalock);
  5930. phba->link_state = LPFC_LINK_UNKNOWN;
  5931. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5932. spin_unlock_irq(&phba->hbalock);
  5933. pring = &psli->ring[psli->fcp_ring];
  5934. lpfc_sli_abort_iocb_ring(phba, pring);
  5935. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5936. "0345 Resetting board due to mailbox timeout\n");
  5937. /* Reset the HBA device */
  5938. lpfc_reset_hba(phba);
  5939. }
  5940. /**
  5941. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  5942. * @phba: Pointer to HBA context object.
  5943. * @pmbox: Pointer to mailbox object.
  5944. * @flag: Flag indicating how the mailbox need to be processed.
  5945. *
  5946. * This function is called by discovery code and HBA management code
  5947. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  5948. * function gets the hbalock to protect the data structures.
  5949. * The mailbox command can be submitted in polling mode, in which case
  5950. * this function will wait in a polling loop for the completion of the
  5951. * mailbox.
  5952. * If the mailbox is submitted in no_wait mode (not polling) the
  5953. * function will submit the command and returns immediately without waiting
  5954. * for the mailbox completion. The no_wait is supported only when HBA
  5955. * is in SLI2/SLI3 mode - interrupts are enabled.
  5956. * The SLI interface allows only one mailbox pending at a time. If the
  5957. * mailbox is issued in polling mode and there is already a mailbox
  5958. * pending, then the function will return an error. If the mailbox is issued
  5959. * in NO_WAIT mode and there is a mailbox pending already, the function
  5960. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  5961. * The sli layer owns the mailbox object until the completion of mailbox
  5962. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  5963. * return codes the caller owns the mailbox command after the return of
  5964. * the function.
  5965. **/
  5966. static int
  5967. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  5968. uint32_t flag)
  5969. {
  5970. MAILBOX_t *mb;
  5971. struct lpfc_sli *psli = &phba->sli;
  5972. uint32_t status, evtctr;
  5973. uint32_t ha_copy, hc_copy;
  5974. int i;
  5975. unsigned long timeout;
  5976. unsigned long drvr_flag = 0;
  5977. uint32_t word0, ldata;
  5978. void __iomem *to_slim;
  5979. int processing_queue = 0;
  5980. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5981. if (!pmbox) {
  5982. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5983. /* processing mbox queue from intr_handler */
  5984. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5985. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5986. return MBX_SUCCESS;
  5987. }
  5988. processing_queue = 1;
  5989. pmbox = lpfc_mbox_get(phba);
  5990. if (!pmbox) {
  5991. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5992. return MBX_SUCCESS;
  5993. }
  5994. }
  5995. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  5996. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  5997. if(!pmbox->vport) {
  5998. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5999. lpfc_printf_log(phba, KERN_ERR,
  6000. LOG_MBOX | LOG_VPORT,
  6001. "1806 Mbox x%x failed. No vport\n",
  6002. pmbox->u.mb.mbxCommand);
  6003. dump_stack();
  6004. goto out_not_finished;
  6005. }
  6006. }
  6007. /* If the PCI channel is in offline state, do not post mbox. */
  6008. if (unlikely(pci_channel_offline(phba->pcidev))) {
  6009. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6010. goto out_not_finished;
  6011. }
  6012. /* If HBA has a deferred error attention, fail the iocb. */
  6013. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  6014. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6015. goto out_not_finished;
  6016. }
  6017. psli = &phba->sli;
  6018. mb = &pmbox->u.mb;
  6019. status = MBX_SUCCESS;
  6020. if (phba->link_state == LPFC_HBA_ERROR) {
  6021. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6022. /* Mbox command <mbxCommand> cannot issue */
  6023. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6024. "(%d):0311 Mailbox command x%x cannot "
  6025. "issue Data: x%x x%x\n",
  6026. pmbox->vport ? pmbox->vport->vpi : 0,
  6027. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6028. goto out_not_finished;
  6029. }
  6030. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6031. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6032. !(hc_copy & HC_MBINT_ENA)) {
  6033. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6034. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6035. "(%d):2528 Mailbox command x%x cannot "
  6036. "issue Data: x%x x%x\n",
  6037. pmbox->vport ? pmbox->vport->vpi : 0,
  6038. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6039. goto out_not_finished;
  6040. }
  6041. }
  6042. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6043. /* Polling for a mbox command when another one is already active
  6044. * is not allowed in SLI. Also, the driver must have established
  6045. * SLI2 mode to queue and process multiple mbox commands.
  6046. */
  6047. if (flag & MBX_POLL) {
  6048. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6049. /* Mbox command <mbxCommand> cannot issue */
  6050. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6051. "(%d):2529 Mailbox command x%x "
  6052. "cannot issue Data: x%x x%x\n",
  6053. pmbox->vport ? pmbox->vport->vpi : 0,
  6054. pmbox->u.mb.mbxCommand,
  6055. psli->sli_flag, flag);
  6056. goto out_not_finished;
  6057. }
  6058. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  6059. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6060. /* Mbox command <mbxCommand> cannot issue */
  6061. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6062. "(%d):2530 Mailbox command x%x "
  6063. "cannot issue Data: x%x x%x\n",
  6064. pmbox->vport ? pmbox->vport->vpi : 0,
  6065. pmbox->u.mb.mbxCommand,
  6066. psli->sli_flag, flag);
  6067. goto out_not_finished;
  6068. }
  6069. /* Another mailbox command is still being processed, queue this
  6070. * command to be processed later.
  6071. */
  6072. lpfc_mbox_put(phba, pmbox);
  6073. /* Mbox cmd issue - BUSY */
  6074. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6075. "(%d):0308 Mbox cmd issue - BUSY Data: "
  6076. "x%x x%x x%x x%x\n",
  6077. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  6078. mb->mbxCommand, phba->pport->port_state,
  6079. psli->sli_flag, flag);
  6080. psli->slistat.mbox_busy++;
  6081. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6082. if (pmbox->vport) {
  6083. lpfc_debugfs_disc_trc(pmbox->vport,
  6084. LPFC_DISC_TRC_MBOX_VPORT,
  6085. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  6086. (uint32_t)mb->mbxCommand,
  6087. mb->un.varWords[0], mb->un.varWords[1]);
  6088. }
  6089. else {
  6090. lpfc_debugfs_disc_trc(phba->pport,
  6091. LPFC_DISC_TRC_MBOX,
  6092. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  6093. (uint32_t)mb->mbxCommand,
  6094. mb->un.varWords[0], mb->un.varWords[1]);
  6095. }
  6096. return MBX_BUSY;
  6097. }
  6098. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6099. /* If we are not polling, we MUST be in SLI2 mode */
  6100. if (flag != MBX_POLL) {
  6101. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  6102. (mb->mbxCommand != MBX_KILL_BOARD)) {
  6103. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6104. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6105. /* Mbox command <mbxCommand> cannot issue */
  6106. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6107. "(%d):2531 Mailbox command x%x "
  6108. "cannot issue Data: x%x x%x\n",
  6109. pmbox->vport ? pmbox->vport->vpi : 0,
  6110. pmbox->u.mb.mbxCommand,
  6111. psli->sli_flag, flag);
  6112. goto out_not_finished;
  6113. }
  6114. /* timeout active mbox command */
  6115. mod_timer(&psli->mbox_tmo, (jiffies +
  6116. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  6117. }
  6118. /* Mailbox cmd <cmd> issue */
  6119. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6120. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  6121. "x%x\n",
  6122. pmbox->vport ? pmbox->vport->vpi : 0,
  6123. mb->mbxCommand, phba->pport->port_state,
  6124. psli->sli_flag, flag);
  6125. if (mb->mbxCommand != MBX_HEARTBEAT) {
  6126. if (pmbox->vport) {
  6127. lpfc_debugfs_disc_trc(pmbox->vport,
  6128. LPFC_DISC_TRC_MBOX_VPORT,
  6129. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6130. (uint32_t)mb->mbxCommand,
  6131. mb->un.varWords[0], mb->un.varWords[1]);
  6132. }
  6133. else {
  6134. lpfc_debugfs_disc_trc(phba->pport,
  6135. LPFC_DISC_TRC_MBOX,
  6136. "MBOX Send: cmd:x%x mb:x%x x%x",
  6137. (uint32_t)mb->mbxCommand,
  6138. mb->un.varWords[0], mb->un.varWords[1]);
  6139. }
  6140. }
  6141. psli->slistat.mbox_cmd++;
  6142. evtctr = psli->slistat.mbox_event;
  6143. /* next set own bit for the adapter and copy over command word */
  6144. mb->mbxOwner = OWN_CHIP;
  6145. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6146. /* Populate mbox extension offset word. */
  6147. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6148. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6149. = (uint8_t *)phba->mbox_ext
  6150. - (uint8_t *)phba->mbox;
  6151. }
  6152. /* Copy the mailbox extension data */
  6153. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6154. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6155. (uint8_t *)phba->mbox_ext,
  6156. pmbox->in_ext_byte_len);
  6157. }
  6158. /* Copy command data to host SLIM area */
  6159. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6160. } else {
  6161. /* Populate mbox extension offset word. */
  6162. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6163. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6164. = MAILBOX_HBA_EXT_OFFSET;
  6165. /* Copy the mailbox extension data */
  6166. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6167. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6168. MAILBOX_HBA_EXT_OFFSET,
  6169. pmbox->context2, pmbox->in_ext_byte_len);
  6170. }
  6171. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6172. /* copy command data into host mbox for cmpl */
  6173. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6174. }
  6175. /* First copy mbox command data to HBA SLIM, skip past first
  6176. word */
  6177. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6178. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  6179. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6180. /* Next copy over first word, with mbxOwner set */
  6181. ldata = *((uint32_t *)mb);
  6182. to_slim = phba->MBslimaddr;
  6183. writel(ldata, to_slim);
  6184. readl(to_slim); /* flush */
  6185. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6186. /* switch over to host mailbox */
  6187. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6188. }
  6189. }
  6190. wmb();
  6191. switch (flag) {
  6192. case MBX_NOWAIT:
  6193. /* Set up reference to mailbox command */
  6194. psli->mbox_active = pmbox;
  6195. /* Interrupt board to do it */
  6196. writel(CA_MBATT, phba->CAregaddr);
  6197. readl(phba->CAregaddr); /* flush */
  6198. /* Don't wait for it to finish, just return */
  6199. break;
  6200. case MBX_POLL:
  6201. /* Set up null reference to mailbox command */
  6202. psli->mbox_active = NULL;
  6203. /* Interrupt board to do it */
  6204. writel(CA_MBATT, phba->CAregaddr);
  6205. readl(phba->CAregaddr); /* flush */
  6206. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6207. /* First read mbox status word */
  6208. word0 = *((uint32_t *)phba->mbox);
  6209. word0 = le32_to_cpu(word0);
  6210. } else {
  6211. /* First read mbox status word */
  6212. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6213. spin_unlock_irqrestore(&phba->hbalock,
  6214. drvr_flag);
  6215. goto out_not_finished;
  6216. }
  6217. }
  6218. /* Read the HBA Host Attention Register */
  6219. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6220. spin_unlock_irqrestore(&phba->hbalock,
  6221. drvr_flag);
  6222. goto out_not_finished;
  6223. }
  6224. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6225. 1000) + jiffies;
  6226. i = 0;
  6227. /* Wait for command to complete */
  6228. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6229. (!(ha_copy & HA_MBATT) &&
  6230. (phba->link_state > LPFC_WARM_START))) {
  6231. if (time_after(jiffies, timeout)) {
  6232. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6233. spin_unlock_irqrestore(&phba->hbalock,
  6234. drvr_flag);
  6235. goto out_not_finished;
  6236. }
  6237. /* Check if we took a mbox interrupt while we were
  6238. polling */
  6239. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6240. && (evtctr != psli->slistat.mbox_event))
  6241. break;
  6242. if (i++ > 10) {
  6243. spin_unlock_irqrestore(&phba->hbalock,
  6244. drvr_flag);
  6245. msleep(1);
  6246. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6247. }
  6248. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6249. /* First copy command data */
  6250. word0 = *((uint32_t *)phba->mbox);
  6251. word0 = le32_to_cpu(word0);
  6252. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6253. MAILBOX_t *slimmb;
  6254. uint32_t slimword0;
  6255. /* Check real SLIM for any errors */
  6256. slimword0 = readl(phba->MBslimaddr);
  6257. slimmb = (MAILBOX_t *) & slimword0;
  6258. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6259. && slimmb->mbxStatus) {
  6260. psli->sli_flag &=
  6261. ~LPFC_SLI_ACTIVE;
  6262. word0 = slimword0;
  6263. }
  6264. }
  6265. } else {
  6266. /* First copy command data */
  6267. word0 = readl(phba->MBslimaddr);
  6268. }
  6269. /* Read the HBA Host Attention Register */
  6270. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6271. spin_unlock_irqrestore(&phba->hbalock,
  6272. drvr_flag);
  6273. goto out_not_finished;
  6274. }
  6275. }
  6276. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6277. /* copy results back to user */
  6278. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  6279. /* Copy the mailbox extension data */
  6280. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6281. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6282. pmbox->context2,
  6283. pmbox->out_ext_byte_len);
  6284. }
  6285. } else {
  6286. /* First copy command data */
  6287. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  6288. MAILBOX_CMD_SIZE);
  6289. /* Copy the mailbox extension data */
  6290. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6291. lpfc_memcpy_from_slim(pmbox->context2,
  6292. phba->MBslimaddr +
  6293. MAILBOX_HBA_EXT_OFFSET,
  6294. pmbox->out_ext_byte_len);
  6295. }
  6296. }
  6297. writel(HA_MBATT, phba->HAregaddr);
  6298. readl(phba->HAregaddr); /* flush */
  6299. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6300. status = mb->mbxStatus;
  6301. }
  6302. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6303. return status;
  6304. out_not_finished:
  6305. if (processing_queue) {
  6306. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6307. lpfc_mbox_cmpl_put(phba, pmbox);
  6308. }
  6309. return MBX_NOT_FINISHED;
  6310. }
  6311. /**
  6312. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6313. * @phba: Pointer to HBA context object.
  6314. *
  6315. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6316. * the driver internal pending mailbox queue. It will then try to wait out the
  6317. * possible outstanding mailbox command before return.
  6318. *
  6319. * Returns:
  6320. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6321. * the outstanding mailbox command timed out.
  6322. **/
  6323. static int
  6324. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6325. {
  6326. struct lpfc_sli *psli = &phba->sli;
  6327. int rc = 0;
  6328. unsigned long timeout = 0;
  6329. /* Mark the asynchronous mailbox command posting as blocked */
  6330. spin_lock_irq(&phba->hbalock);
  6331. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6332. /* Determine how long we might wait for the active mailbox
  6333. * command to be gracefully completed by firmware.
  6334. */
  6335. if (phba->sli.mbox_active)
  6336. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6337. phba->sli.mbox_active) *
  6338. 1000) + jiffies;
  6339. spin_unlock_irq(&phba->hbalock);
  6340. /* Wait for the outstnading mailbox command to complete */
  6341. while (phba->sli.mbox_active) {
  6342. /* Check active mailbox complete status every 2ms */
  6343. msleep(2);
  6344. if (time_after(jiffies, timeout)) {
  6345. /* Timeout, marked the outstanding cmd not complete */
  6346. rc = 1;
  6347. break;
  6348. }
  6349. }
  6350. /* Can not cleanly block async mailbox command, fails it */
  6351. if (rc) {
  6352. spin_lock_irq(&phba->hbalock);
  6353. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6354. spin_unlock_irq(&phba->hbalock);
  6355. }
  6356. return rc;
  6357. }
  6358. /**
  6359. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6360. * @phba: Pointer to HBA context object.
  6361. *
  6362. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6363. * commands from the driver internal pending mailbox queue. It makes sure
  6364. * that there is no outstanding mailbox command before resuming posting
  6365. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6366. * mailbox command, it will try to wait it out before resuming asynchronous
  6367. * mailbox command posting.
  6368. **/
  6369. static void
  6370. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6371. {
  6372. struct lpfc_sli *psli = &phba->sli;
  6373. spin_lock_irq(&phba->hbalock);
  6374. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6375. /* Asynchronous mailbox posting is not blocked, do nothing */
  6376. spin_unlock_irq(&phba->hbalock);
  6377. return;
  6378. }
  6379. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6380. * successful or timeout, after timing-out the outstanding mailbox
  6381. * command shall always be removed, so just unblock posting async
  6382. * mailbox command and resume
  6383. */
  6384. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6385. spin_unlock_irq(&phba->hbalock);
  6386. /* wake up worker thread to post asynchronlous mailbox command */
  6387. lpfc_worker_wake_up(phba);
  6388. }
  6389. /**
  6390. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6391. * @phba: Pointer to HBA context object.
  6392. * @mboxq: Pointer to mailbox object.
  6393. *
  6394. * The function posts a mailbox to the port. The mailbox is expected
  6395. * to be comletely filled in and ready for the port to operate on it.
  6396. * This routine executes a synchronous completion operation on the
  6397. * mailbox by polling for its completion.
  6398. *
  6399. * The caller must not be holding any locks when calling this routine.
  6400. *
  6401. * Returns:
  6402. * MBX_SUCCESS - mailbox posted successfully
  6403. * Any of the MBX error values.
  6404. **/
  6405. static int
  6406. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6407. {
  6408. int rc = MBX_SUCCESS;
  6409. unsigned long iflag;
  6410. uint32_t db_ready;
  6411. uint32_t mcqe_status;
  6412. uint32_t mbx_cmnd;
  6413. unsigned long timeout;
  6414. struct lpfc_sli *psli = &phba->sli;
  6415. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6416. struct lpfc_bmbx_create *mbox_rgn;
  6417. struct dma_address *dma_address;
  6418. struct lpfc_register bmbx_reg;
  6419. /*
  6420. * Only one mailbox can be active to the bootstrap mailbox region
  6421. * at a time and there is no queueing provided.
  6422. */
  6423. spin_lock_irqsave(&phba->hbalock, iflag);
  6424. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6425. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6426. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6427. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6428. "cannot issue Data: x%x x%x\n",
  6429. mboxq->vport ? mboxq->vport->vpi : 0,
  6430. mboxq->u.mb.mbxCommand,
  6431. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6432. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6433. psli->sli_flag, MBX_POLL);
  6434. return MBXERR_ERROR;
  6435. }
  6436. /* The server grabs the token and owns it until release */
  6437. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6438. phba->sli.mbox_active = mboxq;
  6439. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6440. /*
  6441. * Initialize the bootstrap memory region to avoid stale data areas
  6442. * in the mailbox post. Then copy the caller's mailbox contents to
  6443. * the bmbx mailbox region.
  6444. */
  6445. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6446. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6447. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6448. sizeof(struct lpfc_mqe));
  6449. /* Post the high mailbox dma address to the port and wait for ready. */
  6450. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6451. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6452. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6453. * 1000) + jiffies;
  6454. do {
  6455. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6456. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6457. if (!db_ready)
  6458. msleep(2);
  6459. if (time_after(jiffies, timeout)) {
  6460. rc = MBXERR_ERROR;
  6461. goto exit;
  6462. }
  6463. } while (!db_ready);
  6464. /* Post the low mailbox dma address to the port. */
  6465. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6466. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6467. * 1000) + jiffies;
  6468. do {
  6469. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6470. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6471. if (!db_ready)
  6472. msleep(2);
  6473. if (time_after(jiffies, timeout)) {
  6474. rc = MBXERR_ERROR;
  6475. goto exit;
  6476. }
  6477. } while (!db_ready);
  6478. /*
  6479. * Read the CQ to ensure the mailbox has completed.
  6480. * If so, update the mailbox status so that the upper layers
  6481. * can complete the request normally.
  6482. */
  6483. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6484. sizeof(struct lpfc_mqe));
  6485. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6486. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6487. sizeof(struct lpfc_mcqe));
  6488. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6489. /*
  6490. * When the CQE status indicates a failure and the mailbox status
  6491. * indicates success then copy the CQE status into the mailbox status
  6492. * (and prefix it with x4000).
  6493. */
  6494. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6495. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6496. bf_set(lpfc_mqe_status, mb,
  6497. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6498. rc = MBXERR_ERROR;
  6499. } else
  6500. lpfc_sli4_swap_str(phba, mboxq);
  6501. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6502. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6503. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6504. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6505. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6506. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6507. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6508. bf_get(lpfc_mqe_status, mb),
  6509. mb->un.mb_words[0], mb->un.mb_words[1],
  6510. mb->un.mb_words[2], mb->un.mb_words[3],
  6511. mb->un.mb_words[4], mb->un.mb_words[5],
  6512. mb->un.mb_words[6], mb->un.mb_words[7],
  6513. mb->un.mb_words[8], mb->un.mb_words[9],
  6514. mb->un.mb_words[10], mb->un.mb_words[11],
  6515. mb->un.mb_words[12], mboxq->mcqe.word0,
  6516. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6517. mboxq->mcqe.trailer);
  6518. exit:
  6519. /* We are holding the token, no needed for lock when release */
  6520. spin_lock_irqsave(&phba->hbalock, iflag);
  6521. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6522. phba->sli.mbox_active = NULL;
  6523. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6524. return rc;
  6525. }
  6526. /**
  6527. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6528. * @phba: Pointer to HBA context object.
  6529. * @pmbox: Pointer to mailbox object.
  6530. * @flag: Flag indicating how the mailbox need to be processed.
  6531. *
  6532. * This function is called by discovery code and HBA management code to submit
  6533. * a mailbox command to firmware with SLI-4 interface spec.
  6534. *
  6535. * Return codes the caller owns the mailbox command after the return of the
  6536. * function.
  6537. **/
  6538. static int
  6539. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6540. uint32_t flag)
  6541. {
  6542. struct lpfc_sli *psli = &phba->sli;
  6543. unsigned long iflags;
  6544. int rc;
  6545. /* dump from issue mailbox command if setup */
  6546. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6547. rc = lpfc_mbox_dev_check(phba);
  6548. if (unlikely(rc)) {
  6549. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6550. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6551. "cannot issue Data: x%x x%x\n",
  6552. mboxq->vport ? mboxq->vport->vpi : 0,
  6553. mboxq->u.mb.mbxCommand,
  6554. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6555. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6556. psli->sli_flag, flag);
  6557. goto out_not_finished;
  6558. }
  6559. /* Detect polling mode and jump to a handler */
  6560. if (!phba->sli4_hba.intr_enable) {
  6561. if (flag == MBX_POLL)
  6562. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6563. else
  6564. rc = -EIO;
  6565. if (rc != MBX_SUCCESS)
  6566. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6567. "(%d):2541 Mailbox command x%x "
  6568. "(x%x/x%x) failure: "
  6569. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6570. "Data: x%x x%x\n,",
  6571. mboxq->vport ? mboxq->vport->vpi : 0,
  6572. mboxq->u.mb.mbxCommand,
  6573. lpfc_sli_config_mbox_subsys_get(phba,
  6574. mboxq),
  6575. lpfc_sli_config_mbox_opcode_get(phba,
  6576. mboxq),
  6577. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6578. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6579. bf_get(lpfc_mcqe_ext_status,
  6580. &mboxq->mcqe),
  6581. psli->sli_flag, flag);
  6582. return rc;
  6583. } else if (flag == MBX_POLL) {
  6584. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6585. "(%d):2542 Try to issue mailbox command "
  6586. "x%x (x%x/x%x) synchronously ahead of async"
  6587. "mailbox command queue: x%x x%x\n",
  6588. mboxq->vport ? mboxq->vport->vpi : 0,
  6589. mboxq->u.mb.mbxCommand,
  6590. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6591. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6592. psli->sli_flag, flag);
  6593. /* Try to block the asynchronous mailbox posting */
  6594. rc = lpfc_sli4_async_mbox_block(phba);
  6595. if (!rc) {
  6596. /* Successfully blocked, now issue sync mbox cmd */
  6597. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6598. if (rc != MBX_SUCCESS)
  6599. lpfc_printf_log(phba, KERN_WARNING,
  6600. LOG_MBOX | LOG_SLI,
  6601. "(%d):2597 Sync Mailbox command "
  6602. "x%x (x%x/x%x) failure: "
  6603. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6604. "Data: x%x x%x\n,",
  6605. mboxq->vport ? mboxq->vport->vpi : 0,
  6606. mboxq->u.mb.mbxCommand,
  6607. lpfc_sli_config_mbox_subsys_get(phba,
  6608. mboxq),
  6609. lpfc_sli_config_mbox_opcode_get(phba,
  6610. mboxq),
  6611. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6612. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6613. bf_get(lpfc_mcqe_ext_status,
  6614. &mboxq->mcqe),
  6615. psli->sli_flag, flag);
  6616. /* Unblock the async mailbox posting afterward */
  6617. lpfc_sli4_async_mbox_unblock(phba);
  6618. }
  6619. return rc;
  6620. }
  6621. /* Now, interrupt mode asynchrous mailbox command */
  6622. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6623. if (rc) {
  6624. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6625. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6626. "cannot issue Data: x%x x%x\n",
  6627. mboxq->vport ? mboxq->vport->vpi : 0,
  6628. mboxq->u.mb.mbxCommand,
  6629. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6630. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6631. psli->sli_flag, flag);
  6632. goto out_not_finished;
  6633. }
  6634. /* Put the mailbox command to the driver internal FIFO */
  6635. psli->slistat.mbox_busy++;
  6636. spin_lock_irqsave(&phba->hbalock, iflags);
  6637. lpfc_mbox_put(phba, mboxq);
  6638. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6639. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6640. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6641. "x%x (x%x/x%x) x%x x%x x%x\n",
  6642. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6643. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6644. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6645. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6646. phba->pport->port_state,
  6647. psli->sli_flag, MBX_NOWAIT);
  6648. /* Wake up worker thread to transport mailbox command from head */
  6649. lpfc_worker_wake_up(phba);
  6650. return MBX_BUSY;
  6651. out_not_finished:
  6652. return MBX_NOT_FINISHED;
  6653. }
  6654. /**
  6655. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6656. * @phba: Pointer to HBA context object.
  6657. *
  6658. * This function is called by worker thread to send a mailbox command to
  6659. * SLI4 HBA firmware.
  6660. *
  6661. **/
  6662. int
  6663. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6664. {
  6665. struct lpfc_sli *psli = &phba->sli;
  6666. LPFC_MBOXQ_t *mboxq;
  6667. int rc = MBX_SUCCESS;
  6668. unsigned long iflags;
  6669. struct lpfc_mqe *mqe;
  6670. uint32_t mbx_cmnd;
  6671. /* Check interrupt mode before post async mailbox command */
  6672. if (unlikely(!phba->sli4_hba.intr_enable))
  6673. return MBX_NOT_FINISHED;
  6674. /* Check for mailbox command service token */
  6675. spin_lock_irqsave(&phba->hbalock, iflags);
  6676. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6677. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6678. return MBX_NOT_FINISHED;
  6679. }
  6680. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6681. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6682. return MBX_NOT_FINISHED;
  6683. }
  6684. if (unlikely(phba->sli.mbox_active)) {
  6685. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6686. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6687. "0384 There is pending active mailbox cmd\n");
  6688. return MBX_NOT_FINISHED;
  6689. }
  6690. /* Take the mailbox command service token */
  6691. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6692. /* Get the next mailbox command from head of queue */
  6693. mboxq = lpfc_mbox_get(phba);
  6694. /* If no more mailbox command waiting for post, we're done */
  6695. if (!mboxq) {
  6696. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6697. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6698. return MBX_SUCCESS;
  6699. }
  6700. phba->sli.mbox_active = mboxq;
  6701. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6702. /* Check device readiness for posting mailbox command */
  6703. rc = lpfc_mbox_dev_check(phba);
  6704. if (unlikely(rc))
  6705. /* Driver clean routine will clean up pending mailbox */
  6706. goto out_not_finished;
  6707. /* Prepare the mbox command to be posted */
  6708. mqe = &mboxq->u.mqe;
  6709. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6710. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6711. mod_timer(&psli->mbox_tmo, (jiffies +
  6712. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6713. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6714. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6715. "x%x x%x\n",
  6716. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6717. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6718. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6719. phba->pport->port_state, psli->sli_flag);
  6720. if (mbx_cmnd != MBX_HEARTBEAT) {
  6721. if (mboxq->vport) {
  6722. lpfc_debugfs_disc_trc(mboxq->vport,
  6723. LPFC_DISC_TRC_MBOX_VPORT,
  6724. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6725. mbx_cmnd, mqe->un.mb_words[0],
  6726. mqe->un.mb_words[1]);
  6727. } else {
  6728. lpfc_debugfs_disc_trc(phba->pport,
  6729. LPFC_DISC_TRC_MBOX,
  6730. "MBOX Send: cmd:x%x mb:x%x x%x",
  6731. mbx_cmnd, mqe->un.mb_words[0],
  6732. mqe->un.mb_words[1]);
  6733. }
  6734. }
  6735. psli->slistat.mbox_cmd++;
  6736. /* Post the mailbox command to the port */
  6737. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6738. if (rc != MBX_SUCCESS) {
  6739. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6740. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6741. "cannot issue Data: x%x x%x\n",
  6742. mboxq->vport ? mboxq->vport->vpi : 0,
  6743. mboxq->u.mb.mbxCommand,
  6744. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6745. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6746. psli->sli_flag, MBX_NOWAIT);
  6747. goto out_not_finished;
  6748. }
  6749. return rc;
  6750. out_not_finished:
  6751. spin_lock_irqsave(&phba->hbalock, iflags);
  6752. if (phba->sli.mbox_active) {
  6753. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6754. __lpfc_mbox_cmpl_put(phba, mboxq);
  6755. /* Release the token */
  6756. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6757. phba->sli.mbox_active = NULL;
  6758. }
  6759. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6760. return MBX_NOT_FINISHED;
  6761. }
  6762. /**
  6763. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6764. * @phba: Pointer to HBA context object.
  6765. * @pmbox: Pointer to mailbox object.
  6766. * @flag: Flag indicating how the mailbox need to be processed.
  6767. *
  6768. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6769. * the API jump table function pointer from the lpfc_hba struct.
  6770. *
  6771. * Return codes the caller owns the mailbox command after the return of the
  6772. * function.
  6773. **/
  6774. int
  6775. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6776. {
  6777. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6778. }
  6779. /**
  6780. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6781. * @phba: The hba struct for which this call is being executed.
  6782. * @dev_grp: The HBA PCI-Device group number.
  6783. *
  6784. * This routine sets up the mbox interface API function jump table in @phba
  6785. * struct.
  6786. * Returns: 0 - success, -ENODEV - failure.
  6787. **/
  6788. int
  6789. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6790. {
  6791. switch (dev_grp) {
  6792. case LPFC_PCI_DEV_LP:
  6793. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6794. phba->lpfc_sli_handle_slow_ring_event =
  6795. lpfc_sli_handle_slow_ring_event_s3;
  6796. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6797. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6798. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6799. break;
  6800. case LPFC_PCI_DEV_OC:
  6801. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6802. phba->lpfc_sli_handle_slow_ring_event =
  6803. lpfc_sli_handle_slow_ring_event_s4;
  6804. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6805. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6806. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6807. break;
  6808. default:
  6809. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6810. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6811. dev_grp);
  6812. return -ENODEV;
  6813. break;
  6814. }
  6815. return 0;
  6816. }
  6817. /**
  6818. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6819. * @phba: Pointer to HBA context object.
  6820. * @pring: Pointer to driver SLI ring object.
  6821. * @piocb: Pointer to address of newly added command iocb.
  6822. *
  6823. * This function is called with hbalock held to add a command
  6824. * iocb to the txq when SLI layer cannot submit the command iocb
  6825. * to the ring.
  6826. **/
  6827. void
  6828. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6829. struct lpfc_iocbq *piocb)
  6830. {
  6831. /* Insert the caller's iocb in the txq tail for later processing. */
  6832. list_add_tail(&piocb->list, &pring->txq);
  6833. pring->txq_cnt++;
  6834. }
  6835. /**
  6836. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6837. * @phba: Pointer to HBA context object.
  6838. * @pring: Pointer to driver SLI ring object.
  6839. * @piocb: Pointer to address of newly added command iocb.
  6840. *
  6841. * This function is called with hbalock held before a new
  6842. * iocb is submitted to the firmware. This function checks
  6843. * txq to flush the iocbs in txq to Firmware before
  6844. * submitting new iocbs to the Firmware.
  6845. * If there are iocbs in the txq which need to be submitted
  6846. * to firmware, lpfc_sli_next_iocb returns the first element
  6847. * of the txq after dequeuing it from txq.
  6848. * If there is no iocb in the txq then the function will return
  6849. * *piocb and *piocb is set to NULL. Caller needs to check
  6850. * *piocb to find if there are more commands in the txq.
  6851. **/
  6852. static struct lpfc_iocbq *
  6853. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6854. struct lpfc_iocbq **piocb)
  6855. {
  6856. struct lpfc_iocbq * nextiocb;
  6857. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6858. if (!nextiocb) {
  6859. nextiocb = *piocb;
  6860. *piocb = NULL;
  6861. }
  6862. return nextiocb;
  6863. }
  6864. /**
  6865. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6866. * @phba: Pointer to HBA context object.
  6867. * @ring_number: SLI ring number to issue iocb on.
  6868. * @piocb: Pointer to command iocb.
  6869. * @flag: Flag indicating if this command can be put into txq.
  6870. *
  6871. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6872. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6873. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6874. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6875. * this function allows only iocbs for posting buffers. This function finds
  6876. * next available slot in the command ring and posts the command to the
  6877. * available slot and writes the port attention register to request HBA start
  6878. * processing new iocb. If there is no slot available in the ring and
  6879. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6880. * the function returns IOCB_BUSY.
  6881. *
  6882. * This function is called with hbalock held. The function will return success
  6883. * after it successfully submit the iocb to firmware or after adding to the
  6884. * txq.
  6885. **/
  6886. static int
  6887. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6888. struct lpfc_iocbq *piocb, uint32_t flag)
  6889. {
  6890. struct lpfc_iocbq *nextiocb;
  6891. IOCB_t *iocb;
  6892. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6893. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6894. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6895. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6896. lpfc_printf_log(phba, KERN_ERR,
  6897. LOG_SLI | LOG_VPORT,
  6898. "1807 IOCB x%x failed. No vport\n",
  6899. piocb->iocb.ulpCommand);
  6900. dump_stack();
  6901. return IOCB_ERROR;
  6902. }
  6903. /* If the PCI channel is in offline state, do not post iocbs. */
  6904. if (unlikely(pci_channel_offline(phba->pcidev)))
  6905. return IOCB_ERROR;
  6906. /* If HBA has a deferred error attention, fail the iocb. */
  6907. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6908. return IOCB_ERROR;
  6909. /*
  6910. * We should never get an IOCB if we are in a < LINK_DOWN state
  6911. */
  6912. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6913. return IOCB_ERROR;
  6914. /*
  6915. * Check to see if we are blocking IOCB processing because of a
  6916. * outstanding event.
  6917. */
  6918. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  6919. goto iocb_busy;
  6920. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  6921. /*
  6922. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  6923. * can be issued if the link is not up.
  6924. */
  6925. switch (piocb->iocb.ulpCommand) {
  6926. case CMD_GEN_REQUEST64_CR:
  6927. case CMD_GEN_REQUEST64_CX:
  6928. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  6929. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  6930. FC_RCTL_DD_UNSOL_CMD) ||
  6931. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  6932. MENLO_TRANSPORT_TYPE))
  6933. goto iocb_busy;
  6934. break;
  6935. case CMD_QUE_RING_BUF_CN:
  6936. case CMD_QUE_RING_BUF64_CN:
  6937. /*
  6938. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  6939. * completion, iocb_cmpl MUST be 0.
  6940. */
  6941. if (piocb->iocb_cmpl)
  6942. piocb->iocb_cmpl = NULL;
  6943. /*FALLTHROUGH*/
  6944. case CMD_CREATE_XRI_CR:
  6945. case CMD_CLOSE_XRI_CN:
  6946. case CMD_CLOSE_XRI_CX:
  6947. break;
  6948. default:
  6949. goto iocb_busy;
  6950. }
  6951. /*
  6952. * For FCP commands, we must be in a state where we can process link
  6953. * attention events.
  6954. */
  6955. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  6956. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  6957. goto iocb_busy;
  6958. }
  6959. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  6960. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  6961. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  6962. if (iocb)
  6963. lpfc_sli_update_ring(phba, pring);
  6964. else
  6965. lpfc_sli_update_full_ring(phba, pring);
  6966. if (!piocb)
  6967. return IOCB_SUCCESS;
  6968. goto out_busy;
  6969. iocb_busy:
  6970. pring->stats.iocb_cmd_delay++;
  6971. out_busy:
  6972. if (!(flag & SLI_IOCB_RET_IOCB)) {
  6973. __lpfc_sli_ringtx_put(phba, pring, piocb);
  6974. return IOCB_SUCCESS;
  6975. }
  6976. return IOCB_BUSY;
  6977. }
  6978. /**
  6979. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  6980. * @phba: Pointer to HBA context object.
  6981. * @piocb: Pointer to command iocb.
  6982. * @sglq: Pointer to the scatter gather queue object.
  6983. *
  6984. * This routine converts the bpl or bde that is in the IOCB
  6985. * to a sgl list for the sli4 hardware. The physical address
  6986. * of the bpl/bde is converted back to a virtual address.
  6987. * If the IOCB contains a BPL then the list of BDE's is
  6988. * converted to sli4_sge's. If the IOCB contains a single
  6989. * BDE then it is converted to a single sli_sge.
  6990. * The IOCB is still in cpu endianess so the contents of
  6991. * the bpl can be used without byte swapping.
  6992. *
  6993. * Returns valid XRI = Success, NO_XRI = Failure.
  6994. **/
  6995. static uint16_t
  6996. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  6997. struct lpfc_sglq *sglq)
  6998. {
  6999. uint16_t xritag = NO_XRI;
  7000. struct ulp_bde64 *bpl = NULL;
  7001. struct ulp_bde64 bde;
  7002. struct sli4_sge *sgl = NULL;
  7003. struct lpfc_dmabuf *dmabuf;
  7004. IOCB_t *icmd;
  7005. int numBdes = 0;
  7006. int i = 0;
  7007. uint32_t offset = 0; /* accumulated offset in the sg request list */
  7008. int inbound = 0; /* number of sg reply entries inbound from firmware */
  7009. if (!piocbq || !sglq)
  7010. return xritag;
  7011. sgl = (struct sli4_sge *)sglq->sgl;
  7012. icmd = &piocbq->iocb;
  7013. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  7014. return sglq->sli4_xritag;
  7015. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7016. numBdes = icmd->un.genreq64.bdl.bdeSize /
  7017. sizeof(struct ulp_bde64);
  7018. /* The addrHigh and addrLow fields within the IOCB
  7019. * have not been byteswapped yet so there is no
  7020. * need to swap them back.
  7021. */
  7022. if (piocbq->context3)
  7023. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  7024. else
  7025. return xritag;
  7026. bpl = (struct ulp_bde64 *)dmabuf->virt;
  7027. if (!bpl)
  7028. return xritag;
  7029. for (i = 0; i < numBdes; i++) {
  7030. /* Should already be byte swapped. */
  7031. sgl->addr_hi = bpl->addrHigh;
  7032. sgl->addr_lo = bpl->addrLow;
  7033. sgl->word2 = le32_to_cpu(sgl->word2);
  7034. if ((i+1) == numBdes)
  7035. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7036. else
  7037. bf_set(lpfc_sli4_sge_last, sgl, 0);
  7038. /* swap the size field back to the cpu so we
  7039. * can assign it to the sgl.
  7040. */
  7041. bde.tus.w = le32_to_cpu(bpl->tus.w);
  7042. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  7043. /* The offsets in the sgl need to be accumulated
  7044. * separately for the request and reply lists.
  7045. * The request is always first, the reply follows.
  7046. */
  7047. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  7048. /* add up the reply sg entries */
  7049. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  7050. inbound++;
  7051. /* first inbound? reset the offset */
  7052. if (inbound == 1)
  7053. offset = 0;
  7054. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  7055. bf_set(lpfc_sli4_sge_type, sgl,
  7056. LPFC_SGE_TYPE_DATA);
  7057. offset += bde.tus.f.bdeSize;
  7058. }
  7059. sgl->word2 = cpu_to_le32(sgl->word2);
  7060. bpl++;
  7061. sgl++;
  7062. }
  7063. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  7064. /* The addrHigh and addrLow fields of the BDE have not
  7065. * been byteswapped yet so they need to be swapped
  7066. * before putting them in the sgl.
  7067. */
  7068. sgl->addr_hi =
  7069. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  7070. sgl->addr_lo =
  7071. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  7072. sgl->word2 = le32_to_cpu(sgl->word2);
  7073. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7074. sgl->word2 = cpu_to_le32(sgl->word2);
  7075. sgl->sge_len =
  7076. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  7077. }
  7078. return sglq->sli4_xritag;
  7079. }
  7080. /**
  7081. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  7082. * @phba: Pointer to HBA context object.
  7083. *
  7084. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  7085. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  7086. * held.
  7087. *
  7088. * Return: index into SLI4 fast-path FCP queue index.
  7089. **/
  7090. static inline uint32_t
  7091. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  7092. {
  7093. int i;
  7094. if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU)
  7095. i = smp_processor_id();
  7096. else
  7097. i = atomic_add_return(1, &phba->fcp_qidx);
  7098. i = (i % phba->cfg_fcp_io_channel);
  7099. return i;
  7100. }
  7101. /**
  7102. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  7103. * @phba: Pointer to HBA context object.
  7104. * @piocb: Pointer to command iocb.
  7105. * @wqe: Pointer to the work queue entry.
  7106. *
  7107. * This routine converts the iocb command to its Work Queue Entry
  7108. * equivalent. The wqe pointer should not have any fields set when
  7109. * this routine is called because it will memcpy over them.
  7110. * This routine does not set the CQ_ID or the WQEC bits in the
  7111. * wqe.
  7112. *
  7113. * Returns: 0 = Success, IOCB_ERROR = Failure.
  7114. **/
  7115. static int
  7116. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  7117. union lpfc_wqe *wqe)
  7118. {
  7119. uint32_t xmit_len = 0, total_len = 0;
  7120. uint8_t ct = 0;
  7121. uint32_t fip;
  7122. uint32_t abort_tag;
  7123. uint8_t command_type = ELS_COMMAND_NON_FIP;
  7124. uint8_t cmnd;
  7125. uint16_t xritag;
  7126. uint16_t abrt_iotag;
  7127. struct lpfc_iocbq *abrtiocbq;
  7128. struct ulp_bde64 *bpl = NULL;
  7129. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  7130. int numBdes, i;
  7131. struct ulp_bde64 bde;
  7132. struct lpfc_nodelist *ndlp;
  7133. uint32_t *pcmd;
  7134. uint32_t if_type;
  7135. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  7136. /* The fcp commands will set command type */
  7137. if (iocbq->iocb_flag & LPFC_IO_FCP)
  7138. command_type = FCP_COMMAND;
  7139. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  7140. command_type = ELS_COMMAND_FIP;
  7141. else
  7142. command_type = ELS_COMMAND_NON_FIP;
  7143. /* Some of the fields are in the right position already */
  7144. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  7145. abort_tag = (uint32_t) iocbq->iotag;
  7146. xritag = iocbq->sli4_xritag;
  7147. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  7148. /* words0-2 bpl convert bde */
  7149. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7150. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7151. sizeof(struct ulp_bde64);
  7152. bpl = (struct ulp_bde64 *)
  7153. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  7154. if (!bpl)
  7155. return IOCB_ERROR;
  7156. /* Should already be byte swapped. */
  7157. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7158. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7159. /* swap the size field back to the cpu so we
  7160. * can assign it to the sgl.
  7161. */
  7162. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7163. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7164. total_len = 0;
  7165. for (i = 0; i < numBdes; i++) {
  7166. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7167. total_len += bde.tus.f.bdeSize;
  7168. }
  7169. } else
  7170. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7171. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7172. cmnd = iocbq->iocb.ulpCommand;
  7173. switch (iocbq->iocb.ulpCommand) {
  7174. case CMD_ELS_REQUEST64_CR:
  7175. if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
  7176. ndlp = iocbq->context_un.ndlp;
  7177. else
  7178. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7179. if (!iocbq->iocb.ulpLe) {
  7180. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7181. "2007 Only Limited Edition cmd Format"
  7182. " supported 0x%x\n",
  7183. iocbq->iocb.ulpCommand);
  7184. return IOCB_ERROR;
  7185. }
  7186. wqe->els_req.payload_len = xmit_len;
  7187. /* Els_reguest64 has a TMO */
  7188. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7189. iocbq->iocb.ulpTimeout);
  7190. /* Need a VF for word 4 set the vf bit*/
  7191. bf_set(els_req64_vf, &wqe->els_req, 0);
  7192. /* And a VFID for word 12 */
  7193. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7194. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7195. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7196. iocbq->iocb.ulpContext);
  7197. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7198. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7199. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7200. if (command_type == ELS_COMMAND_FIP)
  7201. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7202. >> LPFC_FIP_ELS_ID_SHIFT);
  7203. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7204. iocbq->context2)->virt);
  7205. if_type = bf_get(lpfc_sli_intf_if_type,
  7206. &phba->sli4_hba.sli_intf);
  7207. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7208. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7209. *pcmd == ELS_CMD_SCR ||
  7210. *pcmd == ELS_CMD_FDISC ||
  7211. *pcmd == ELS_CMD_LOGO ||
  7212. *pcmd == ELS_CMD_PLOGI)) {
  7213. bf_set(els_req64_sp, &wqe->els_req, 1);
  7214. bf_set(els_req64_sid, &wqe->els_req,
  7215. iocbq->vport->fc_myDID);
  7216. if ((*pcmd == ELS_CMD_FLOGI) &&
  7217. !(phba->fc_topology ==
  7218. LPFC_TOPOLOGY_LOOP))
  7219. bf_set(els_req64_sid, &wqe->els_req, 0);
  7220. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7221. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7222. phba->vpi_ids[iocbq->vport->vpi]);
  7223. } else if (pcmd && iocbq->context1) {
  7224. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7225. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7226. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7227. }
  7228. }
  7229. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7230. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7231. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7232. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7233. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7234. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7235. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7236. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7237. break;
  7238. case CMD_XMIT_SEQUENCE64_CX:
  7239. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7240. iocbq->iocb.un.ulpWord[3]);
  7241. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7242. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7243. /* The entire sequence is transmitted for this IOCB */
  7244. xmit_len = total_len;
  7245. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7246. if (phba->link_flag & LS_LOOPBACK_MODE)
  7247. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7248. case CMD_XMIT_SEQUENCE64_CR:
  7249. /* word3 iocb=io_tag32 wqe=reserved */
  7250. wqe->xmit_sequence.rsvd3 = 0;
  7251. /* word4 relative_offset memcpy */
  7252. /* word5 r_ctl/df_ctl memcpy */
  7253. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7254. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7255. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7256. LPFC_WQE_IOD_WRITE);
  7257. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7258. LPFC_WQE_LENLOC_WORD12);
  7259. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7260. wqe->xmit_sequence.xmit_len = xmit_len;
  7261. command_type = OTHER_COMMAND;
  7262. break;
  7263. case CMD_XMIT_BCAST64_CN:
  7264. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7265. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7266. /* word4 iocb=rsvd wqe=rsvd */
  7267. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7268. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7269. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7270. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7271. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7272. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7273. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7274. LPFC_WQE_LENLOC_WORD3);
  7275. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7276. break;
  7277. case CMD_FCP_IWRITE64_CR:
  7278. command_type = FCP_COMMAND_DATA_OUT;
  7279. /* word3 iocb=iotag wqe=payload_offset_len */
  7280. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7281. wqe->fcp_iwrite.payload_offset_len =
  7282. xmit_len + sizeof(struct fcp_rsp);
  7283. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7284. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7285. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7286. iocbq->iocb.ulpFCP2Rcvy);
  7287. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7288. /* Always open the exchange */
  7289. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7290. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7291. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7292. LPFC_WQE_LENLOC_WORD4);
  7293. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7294. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7295. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7296. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7297. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7298. }
  7299. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7300. break;
  7301. case CMD_FCP_IREAD64_CR:
  7302. /* word3 iocb=iotag wqe=payload_offset_len */
  7303. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7304. wqe->fcp_iread.payload_offset_len =
  7305. xmit_len + sizeof(struct fcp_rsp);
  7306. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7307. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7308. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7309. iocbq->iocb.ulpFCP2Rcvy);
  7310. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7311. /* Always open the exchange */
  7312. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7313. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7314. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7315. LPFC_WQE_LENLOC_WORD4);
  7316. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7317. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7318. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7319. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7320. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7321. }
  7322. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7323. break;
  7324. case CMD_FCP_ICMND64_CR:
  7325. /* word3 iocb=IO_TAG wqe=reserved */
  7326. wqe->fcp_icmd.rsrvd3 = 0;
  7327. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7328. /* Always open the exchange */
  7329. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7330. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7331. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7332. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7333. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7334. LPFC_WQE_LENLOC_NONE);
  7335. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7336. break;
  7337. case CMD_GEN_REQUEST64_CR:
  7338. /* For this command calculate the xmit length of the
  7339. * request bde.
  7340. */
  7341. xmit_len = 0;
  7342. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7343. sizeof(struct ulp_bde64);
  7344. for (i = 0; i < numBdes; i++) {
  7345. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7346. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7347. break;
  7348. xmit_len += bde.tus.f.bdeSize;
  7349. }
  7350. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7351. wqe->gen_req.request_payload_len = xmit_len;
  7352. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7353. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7354. /* word6 context tag copied in memcpy */
  7355. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7356. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7357. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7358. "2015 Invalid CT %x command 0x%x\n",
  7359. ct, iocbq->iocb.ulpCommand);
  7360. return IOCB_ERROR;
  7361. }
  7362. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7363. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7364. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7365. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7366. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7367. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7368. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7369. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7370. command_type = OTHER_COMMAND;
  7371. break;
  7372. case CMD_XMIT_ELS_RSP64_CX:
  7373. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7374. /* words0-2 BDE memcpy */
  7375. /* word3 iocb=iotag32 wqe=response_payload_len */
  7376. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7377. /* word4 */
  7378. wqe->xmit_els_rsp.word4 = 0;
  7379. /* word5 iocb=rsvd wge=did */
  7380. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7381. iocbq->iocb.un.xseq64.xmit_els_remoteID);
  7382. if_type = bf_get(lpfc_sli_intf_if_type,
  7383. &phba->sli4_hba.sli_intf);
  7384. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7385. if (iocbq->vport->fc_flag & FC_PT2PT) {
  7386. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7387. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7388. iocbq->vport->fc_myDID);
  7389. if (iocbq->vport->fc_myDID == Fabric_DID) {
  7390. bf_set(wqe_els_did,
  7391. &wqe->xmit_els_rsp.wqe_dest, 0);
  7392. }
  7393. }
  7394. }
  7395. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7396. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7397. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7398. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7399. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7400. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7401. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7402. phba->vpi_ids[iocbq->vport->vpi]);
  7403. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7404. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7405. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7406. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7407. LPFC_WQE_LENLOC_WORD3);
  7408. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7409. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7410. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7411. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7412. iocbq->context2)->virt);
  7413. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7414. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7415. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7416. iocbq->vport->fc_myDID);
  7417. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
  7418. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7419. phba->vpi_ids[phba->pport->vpi]);
  7420. }
  7421. command_type = OTHER_COMMAND;
  7422. break;
  7423. case CMD_CLOSE_XRI_CN:
  7424. case CMD_ABORT_XRI_CN:
  7425. case CMD_ABORT_XRI_CX:
  7426. /* words 0-2 memcpy should be 0 rserved */
  7427. /* port will send abts */
  7428. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7429. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7430. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7431. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7432. } else
  7433. fip = 0;
  7434. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7435. /*
  7436. * The link is down, or the command was ELS_FIP
  7437. * so the fw does not need to send abts
  7438. * on the wire.
  7439. */
  7440. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7441. else
  7442. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7443. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7444. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7445. wqe->abort_cmd.rsrvd5 = 0;
  7446. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7447. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7448. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7449. /*
  7450. * The abort handler will send us CMD_ABORT_XRI_CN or
  7451. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7452. */
  7453. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7454. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7455. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7456. LPFC_WQE_LENLOC_NONE);
  7457. cmnd = CMD_ABORT_XRI_CX;
  7458. command_type = OTHER_COMMAND;
  7459. xritag = 0;
  7460. break;
  7461. case CMD_XMIT_BLS_RSP64_CX:
  7462. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7463. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7464. * we re-construct this WQE here based on information in
  7465. * iocbq from scratch.
  7466. */
  7467. memset(wqe, 0, sizeof(union lpfc_wqe));
  7468. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7469. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7470. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7471. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7472. LPFC_ABTS_UNSOL_INT) {
  7473. /* ABTS sent by initiator to CT exchange, the
  7474. * RX_ID field will be filled with the newly
  7475. * allocated responder XRI.
  7476. */
  7477. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7478. iocbq->sli4_xritag);
  7479. } else {
  7480. /* ABTS sent by responder to CT exchange, the
  7481. * RX_ID field will be filled with the responder
  7482. * RX_ID from ABTS.
  7483. */
  7484. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7485. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7486. }
  7487. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7488. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7489. /* Use CT=VPI */
  7490. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7491. ndlp->nlp_DID);
  7492. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7493. iocbq->iocb.ulpContext);
  7494. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7495. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7496. phba->vpi_ids[phba->pport->vpi]);
  7497. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7498. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7499. LPFC_WQE_LENLOC_NONE);
  7500. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7501. command_type = OTHER_COMMAND;
  7502. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7503. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7504. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7505. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7506. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7507. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7508. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7509. }
  7510. break;
  7511. case CMD_XRI_ABORTED_CX:
  7512. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7513. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7514. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7515. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7516. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7517. default:
  7518. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7519. "2014 Invalid command 0x%x\n",
  7520. iocbq->iocb.ulpCommand);
  7521. return IOCB_ERROR;
  7522. break;
  7523. }
  7524. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7525. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7526. wqe->generic.wqe_com.abort_tag = abort_tag;
  7527. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7528. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7529. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7530. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7531. return 0;
  7532. }
  7533. /**
  7534. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7535. * @phba: Pointer to HBA context object.
  7536. * @ring_number: SLI ring number to issue iocb on.
  7537. * @piocb: Pointer to command iocb.
  7538. * @flag: Flag indicating if this command can be put into txq.
  7539. *
  7540. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7541. * an iocb command to an HBA with SLI-4 interface spec.
  7542. *
  7543. * This function is called with hbalock held. The function will return success
  7544. * after it successfully submit the iocb to firmware or after adding to the
  7545. * txq.
  7546. **/
  7547. static int
  7548. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7549. struct lpfc_iocbq *piocb, uint32_t flag)
  7550. {
  7551. struct lpfc_sglq *sglq;
  7552. union lpfc_wqe wqe;
  7553. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7554. if (piocb->sli4_xritag == NO_XRI) {
  7555. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7556. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7557. sglq = NULL;
  7558. else {
  7559. if (pring->txq_cnt) {
  7560. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7561. __lpfc_sli_ringtx_put(phba,
  7562. pring, piocb);
  7563. return IOCB_SUCCESS;
  7564. } else {
  7565. return IOCB_BUSY;
  7566. }
  7567. } else {
  7568. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7569. if (!sglq) {
  7570. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7571. __lpfc_sli_ringtx_put(phba,
  7572. pring,
  7573. piocb);
  7574. return IOCB_SUCCESS;
  7575. } else
  7576. return IOCB_BUSY;
  7577. }
  7578. }
  7579. }
  7580. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7581. /* These IO's already have an XRI and a mapped sgl. */
  7582. sglq = NULL;
  7583. } else {
  7584. /*
  7585. * This is a continuation of a commandi,(CX) so this
  7586. * sglq is on the active list
  7587. */
  7588. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  7589. if (!sglq)
  7590. return IOCB_ERROR;
  7591. }
  7592. if (sglq) {
  7593. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7594. piocb->sli4_xritag = sglq->sli4_xritag;
  7595. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7596. return IOCB_ERROR;
  7597. }
  7598. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7599. return IOCB_ERROR;
  7600. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7601. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7602. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7603. &wqe))
  7604. return IOCB_ERROR;
  7605. } else {
  7606. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7607. return IOCB_ERROR;
  7608. }
  7609. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7610. return 0;
  7611. }
  7612. /**
  7613. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7614. *
  7615. * This routine wraps the actual lockless version for issusing IOCB function
  7616. * pointer from the lpfc_hba struct.
  7617. *
  7618. * Return codes:
  7619. * IOCB_ERROR - Error
  7620. * IOCB_SUCCESS - Success
  7621. * IOCB_BUSY - Busy
  7622. **/
  7623. int
  7624. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7625. struct lpfc_iocbq *piocb, uint32_t flag)
  7626. {
  7627. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7628. }
  7629. /**
  7630. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7631. * @phba: The hba struct for which this call is being executed.
  7632. * @dev_grp: The HBA PCI-Device group number.
  7633. *
  7634. * This routine sets up the SLI interface API function jump table in @phba
  7635. * struct.
  7636. * Returns: 0 - success, -ENODEV - failure.
  7637. **/
  7638. int
  7639. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7640. {
  7641. switch (dev_grp) {
  7642. case LPFC_PCI_DEV_LP:
  7643. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7644. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7645. break;
  7646. case LPFC_PCI_DEV_OC:
  7647. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7648. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7649. break;
  7650. default:
  7651. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7652. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7653. dev_grp);
  7654. return -ENODEV;
  7655. break;
  7656. }
  7657. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7658. return 0;
  7659. }
  7660. /**
  7661. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7662. * @phba: Pointer to HBA context object.
  7663. * @pring: Pointer to driver SLI ring object.
  7664. * @piocb: Pointer to command iocb.
  7665. * @flag: Flag indicating if this command can be put into txq.
  7666. *
  7667. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7668. * function. This function gets the hbalock and calls
  7669. * __lpfc_sli_issue_iocb function and will return the error returned
  7670. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7671. * functions which do not hold hbalock.
  7672. **/
  7673. int
  7674. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7675. struct lpfc_iocbq *piocb, uint32_t flag)
  7676. {
  7677. struct lpfc_sli_ring *pring;
  7678. unsigned long iflags;
  7679. int rc, idx;
  7680. if (phba->sli_rev == LPFC_SLI_REV4) {
  7681. if (piocb->iocb_flag & LPFC_IO_FCP) {
  7682. if (unlikely(!phba->sli4_hba.fcp_wq))
  7683. return IOCB_ERROR;
  7684. idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7685. piocb->fcp_wqidx = idx;
  7686. ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
  7687. }
  7688. pring = &phba->sli.ring[ring_number];
  7689. spin_lock_irqsave(&pring->ring_lock, iflags);
  7690. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7691. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  7692. } else {
  7693. /* For now, SLI2/3 will still use hbalock */
  7694. spin_lock_irqsave(&phba->hbalock, iflags);
  7695. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7696. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7697. }
  7698. return rc;
  7699. }
  7700. /**
  7701. * lpfc_extra_ring_setup - Extra ring setup function
  7702. * @phba: Pointer to HBA context object.
  7703. *
  7704. * This function is called while driver attaches with the
  7705. * HBA to setup the extra ring. The extra ring is used
  7706. * only when driver needs to support target mode functionality
  7707. * or IP over FC functionalities.
  7708. *
  7709. * This function is called with no lock held.
  7710. **/
  7711. static int
  7712. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7713. {
  7714. struct lpfc_sli *psli;
  7715. struct lpfc_sli_ring *pring;
  7716. psli = &phba->sli;
  7717. /* Adjust cmd/rsp ring iocb entries more evenly */
  7718. /* Take some away from the FCP ring */
  7719. pring = &psli->ring[psli->fcp_ring];
  7720. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7721. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7722. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7723. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7724. /* and give them to the extra ring */
  7725. pring = &psli->ring[psli->extra_ring];
  7726. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7727. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7728. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7729. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7730. /* Setup default profile for this ring */
  7731. pring->iotag_max = 4096;
  7732. pring->num_mask = 1;
  7733. pring->prt[0].profile = 0; /* Mask 0 */
  7734. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7735. pring->prt[0].type = phba->cfg_multi_ring_type;
  7736. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7737. return 0;
  7738. }
  7739. /* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
  7740. * @vport: pointer to virtual port object.
  7741. * @ndlp: nodelist pointer for the impacted rport.
  7742. *
  7743. * The driver calls this routine in response to a XRI ABORT CQE
  7744. * event from the port. In this event, the driver is required to
  7745. * recover its login to the rport even though its login may be valid
  7746. * from the driver's perspective. The failed ABTS notice from the
  7747. * port indicates the rport is not responding.
  7748. */
  7749. static void
  7750. lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
  7751. struct lpfc_nodelist *ndlp)
  7752. {
  7753. struct Scsi_Host *shost;
  7754. struct lpfc_hba *phba;
  7755. unsigned long flags = 0;
  7756. shost = lpfc_shost_from_vport(vport);
  7757. phba = vport->phba;
  7758. if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
  7759. lpfc_printf_log(phba, KERN_INFO,
  7760. LOG_SLI, "3093 No rport recovery needed. "
  7761. "rport in state 0x%x\n",
  7762. ndlp->nlp_state);
  7763. return;
  7764. }
  7765. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7766. "3094 Start rport recovery on shost id 0x%x "
  7767. "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
  7768. "flags 0x%x\n",
  7769. shost->host_no, ndlp->nlp_DID,
  7770. vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
  7771. ndlp->nlp_flag);
  7772. /*
  7773. * The rport is not responding. Don't attempt ADISC recovery.
  7774. * Remove the FCP-2 flag to force a PLOGI.
  7775. */
  7776. spin_lock_irqsave(shost->host_lock, flags);
  7777. ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  7778. spin_unlock_irqrestore(shost->host_lock, flags);
  7779. lpfc_disc_state_machine(vport, ndlp, NULL,
  7780. NLP_EVT_DEVICE_RECOVERY);
  7781. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  7782. spin_lock_irqsave(shost->host_lock, flags);
  7783. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  7784. spin_unlock_irqrestore(shost->host_lock, flags);
  7785. lpfc_disc_start(vport);
  7786. }
  7787. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7788. * @phba: Pointer to HBA context object.
  7789. * @iocbq: Pointer to iocb object.
  7790. *
  7791. * The async_event handler calls this routine when it receives
  7792. * an ASYNC_STATUS_CN event from the port. The port generates
  7793. * this event when an Abort Sequence request to an rport fails
  7794. * twice in succession. The abort could be originated by the
  7795. * driver or by the port. The ABTS could have been for an ELS
  7796. * or FCP IO. The port only generates this event when an ABTS
  7797. * fails to complete after one retry.
  7798. */
  7799. static void
  7800. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7801. struct lpfc_iocbq *iocbq)
  7802. {
  7803. struct lpfc_nodelist *ndlp = NULL;
  7804. uint16_t rpi = 0, vpi = 0;
  7805. struct lpfc_vport *vport = NULL;
  7806. /* The rpi in the ulpContext is vport-sensitive. */
  7807. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7808. rpi = iocbq->iocb.ulpContext;
  7809. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7810. "3092 Port generated ABTS async event "
  7811. "on vpi %d rpi %d status 0x%x\n",
  7812. vpi, rpi, iocbq->iocb.ulpStatus);
  7813. vport = lpfc_find_vport_by_vpid(phba, vpi);
  7814. if (!vport)
  7815. goto err_exit;
  7816. ndlp = lpfc_findnode_rpi(vport, rpi);
  7817. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7818. goto err_exit;
  7819. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  7820. lpfc_sli_abts_recover_port(vport, ndlp);
  7821. return;
  7822. err_exit:
  7823. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7824. "3095 Event Context not found, no "
  7825. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  7826. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  7827. vpi, rpi);
  7828. }
  7829. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  7830. * @phba: pointer to HBA context object.
  7831. * @ndlp: nodelist pointer for the impacted rport.
  7832. * @axri: pointer to the wcqe containing the failed exchange.
  7833. *
  7834. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  7835. * port. The port generates this event when an abort exchange request to an
  7836. * rport fails twice in succession with no reply. The abort could be originated
  7837. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  7838. */
  7839. void
  7840. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  7841. struct lpfc_nodelist *ndlp,
  7842. struct sli4_wcqe_xri_aborted *axri)
  7843. {
  7844. struct lpfc_vport *vport;
  7845. uint32_t ext_status = 0;
  7846. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  7847. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7848. "3115 Node Context not found, driver "
  7849. "ignoring abts err event\n");
  7850. return;
  7851. }
  7852. vport = ndlp->vport;
  7853. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7854. "3116 Port generated FCP XRI ABORT event on "
  7855. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  7856. ndlp->vport->vpi, ndlp->nlp_rpi,
  7857. bf_get(lpfc_wcqe_xa_xri, axri),
  7858. bf_get(lpfc_wcqe_xa_status, axri),
  7859. axri->parameter);
  7860. /*
  7861. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  7862. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  7863. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  7864. */
  7865. ext_status = axri->parameter & WCQE_PARAM_MASK;
  7866. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  7867. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  7868. lpfc_sli_abts_recover_port(vport, ndlp);
  7869. }
  7870. /**
  7871. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7872. * @phba: Pointer to HBA context object.
  7873. * @pring: Pointer to driver SLI ring object.
  7874. * @iocbq: Pointer to iocb object.
  7875. *
  7876. * This function is called by the slow ring event handler
  7877. * function when there is an ASYNC event iocb in the ring.
  7878. * This function is called with no lock held.
  7879. * Currently this function handles only temperature related
  7880. * ASYNC events. The function decodes the temperature sensor
  7881. * event message and posts events for the management applications.
  7882. **/
  7883. static void
  7884. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7885. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7886. {
  7887. IOCB_t *icmd;
  7888. uint16_t evt_code;
  7889. struct temp_event temp_event_data;
  7890. struct Scsi_Host *shost;
  7891. uint32_t *iocb_w;
  7892. icmd = &iocbq->iocb;
  7893. evt_code = icmd->un.asyncstat.evt_code;
  7894. switch (evt_code) {
  7895. case ASYNC_TEMP_WARN:
  7896. case ASYNC_TEMP_SAFE:
  7897. temp_event_data.data = (uint32_t) icmd->ulpContext;
  7898. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7899. if (evt_code == ASYNC_TEMP_WARN) {
  7900. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7901. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7902. "0347 Adapter is very hot, please take "
  7903. "corrective action. temperature : %d Celsius\n",
  7904. (uint32_t) icmd->ulpContext);
  7905. } else {
  7906. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7907. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7908. "0340 Adapter temperature is OK now. "
  7909. "temperature : %d Celsius\n",
  7910. (uint32_t) icmd->ulpContext);
  7911. }
  7912. /* Send temperature change event to applications */
  7913. shost = lpfc_shost_from_vport(phba->pport);
  7914. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7915. sizeof(temp_event_data), (char *) &temp_event_data,
  7916. LPFC_NL_VENDOR_ID);
  7917. break;
  7918. case ASYNC_STATUS_CN:
  7919. lpfc_sli_abts_err_handler(phba, iocbq);
  7920. break;
  7921. default:
  7922. iocb_w = (uint32_t *) icmd;
  7923. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7924. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7925. " evt_code 0x%x\n"
  7926. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7927. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7928. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7929. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7930. pring->ringno, icmd->un.asyncstat.evt_code,
  7931. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7932. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  7933. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  7934. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  7935. break;
  7936. }
  7937. }
  7938. /**
  7939. * lpfc_sli_setup - SLI ring setup function
  7940. * @phba: Pointer to HBA context object.
  7941. *
  7942. * lpfc_sli_setup sets up rings of the SLI interface with
  7943. * number of iocbs per ring and iotags. This function is
  7944. * called while driver attach to the HBA and before the
  7945. * interrupts are enabled. So there is no need for locking.
  7946. *
  7947. * This function always returns 0.
  7948. **/
  7949. int
  7950. lpfc_sli_setup(struct lpfc_hba *phba)
  7951. {
  7952. int i, totiocbsize = 0;
  7953. struct lpfc_sli *psli = &phba->sli;
  7954. struct lpfc_sli_ring *pring;
  7955. psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
  7956. if (phba->sli_rev == LPFC_SLI_REV4)
  7957. psli->num_rings += phba->cfg_fcp_io_channel;
  7958. psli->sli_flag = 0;
  7959. psli->fcp_ring = LPFC_FCP_RING;
  7960. psli->next_ring = LPFC_FCP_NEXT_RING;
  7961. psli->extra_ring = LPFC_EXTRA_RING;
  7962. psli->iocbq_lookup = NULL;
  7963. psli->iocbq_lookup_len = 0;
  7964. psli->last_iotag = 0;
  7965. for (i = 0; i < psli->num_rings; i++) {
  7966. pring = &psli->ring[i];
  7967. switch (i) {
  7968. case LPFC_FCP_RING: /* ring 0 - FCP */
  7969. /* numCiocb and numRiocb are used in config_port */
  7970. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  7971. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  7972. pring->sli.sli3.numCiocb +=
  7973. SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7974. pring->sli.sli3.numRiocb +=
  7975. SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7976. pring->sli.sli3.numCiocb +=
  7977. SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7978. pring->sli.sli3.numRiocb +=
  7979. SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7980. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  7981. SLI3_IOCB_CMD_SIZE :
  7982. SLI2_IOCB_CMD_SIZE;
  7983. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  7984. SLI3_IOCB_RSP_SIZE :
  7985. SLI2_IOCB_RSP_SIZE;
  7986. pring->iotag_ctr = 0;
  7987. pring->iotag_max =
  7988. (phba->cfg_hba_queue_depth * 2);
  7989. pring->fast_iotag = pring->iotag_max;
  7990. pring->num_mask = 0;
  7991. break;
  7992. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  7993. /* numCiocb and numRiocb are used in config_port */
  7994. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  7995. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  7996. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  7997. SLI3_IOCB_CMD_SIZE :
  7998. SLI2_IOCB_CMD_SIZE;
  7999. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8000. SLI3_IOCB_RSP_SIZE :
  8001. SLI2_IOCB_RSP_SIZE;
  8002. pring->iotag_max = phba->cfg_hba_queue_depth;
  8003. pring->num_mask = 0;
  8004. break;
  8005. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  8006. /* numCiocb and numRiocb are used in config_port */
  8007. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  8008. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  8009. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8010. SLI3_IOCB_CMD_SIZE :
  8011. SLI2_IOCB_CMD_SIZE;
  8012. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8013. SLI3_IOCB_RSP_SIZE :
  8014. SLI2_IOCB_RSP_SIZE;
  8015. pring->fast_iotag = 0;
  8016. pring->iotag_ctr = 0;
  8017. pring->iotag_max = 4096;
  8018. pring->lpfc_sli_rcv_async_status =
  8019. lpfc_sli_async_event_handler;
  8020. pring->num_mask = LPFC_MAX_RING_MASK;
  8021. pring->prt[0].profile = 0; /* Mask 0 */
  8022. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  8023. pring->prt[0].type = FC_TYPE_ELS;
  8024. pring->prt[0].lpfc_sli_rcv_unsol_event =
  8025. lpfc_els_unsol_event;
  8026. pring->prt[1].profile = 0; /* Mask 1 */
  8027. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  8028. pring->prt[1].type = FC_TYPE_ELS;
  8029. pring->prt[1].lpfc_sli_rcv_unsol_event =
  8030. lpfc_els_unsol_event;
  8031. pring->prt[2].profile = 0; /* Mask 2 */
  8032. /* NameServer Inquiry */
  8033. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  8034. /* NameServer */
  8035. pring->prt[2].type = FC_TYPE_CT;
  8036. pring->prt[2].lpfc_sli_rcv_unsol_event =
  8037. lpfc_ct_unsol_event;
  8038. pring->prt[3].profile = 0; /* Mask 3 */
  8039. /* NameServer response */
  8040. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  8041. /* NameServer */
  8042. pring->prt[3].type = FC_TYPE_CT;
  8043. pring->prt[3].lpfc_sli_rcv_unsol_event =
  8044. lpfc_ct_unsol_event;
  8045. /* abort unsolicited sequence */
  8046. pring->prt[4].profile = 0; /* Mask 4 */
  8047. pring->prt[4].rctl = FC_RCTL_BA_ABTS;
  8048. pring->prt[4].type = FC_TYPE_BLS;
  8049. pring->prt[4].lpfc_sli_rcv_unsol_event =
  8050. lpfc_sli4_ct_abort_unsol_event;
  8051. break;
  8052. }
  8053. totiocbsize += (pring->sli.sli3.numCiocb *
  8054. pring->sli.sli3.sizeCiocb) +
  8055. (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
  8056. }
  8057. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  8058. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  8059. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  8060. "SLI2 SLIM Data: x%x x%lx\n",
  8061. phba->brd_no, totiocbsize,
  8062. (unsigned long) MAX_SLIM_IOCB_SIZE);
  8063. }
  8064. if (phba->cfg_multi_ring_support == 2)
  8065. lpfc_extra_ring_setup(phba);
  8066. return 0;
  8067. }
  8068. /**
  8069. * lpfc_sli_queue_setup - Queue initialization function
  8070. * @phba: Pointer to HBA context object.
  8071. *
  8072. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  8073. * ring. This function also initializes ring indices of each ring.
  8074. * This function is called during the initialization of the SLI
  8075. * interface of an HBA.
  8076. * This function is called with no lock held and always returns
  8077. * 1.
  8078. **/
  8079. int
  8080. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  8081. {
  8082. struct lpfc_sli *psli;
  8083. struct lpfc_sli_ring *pring;
  8084. int i;
  8085. psli = &phba->sli;
  8086. spin_lock_irq(&phba->hbalock);
  8087. INIT_LIST_HEAD(&psli->mboxq);
  8088. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  8089. /* Initialize list headers for txq and txcmplq as double linked lists */
  8090. for (i = 0; i < psli->num_rings; i++) {
  8091. pring = &psli->ring[i];
  8092. pring->ringno = i;
  8093. pring->sli.sli3.next_cmdidx = 0;
  8094. pring->sli.sli3.local_getidx = 0;
  8095. pring->sli.sli3.cmdidx = 0;
  8096. INIT_LIST_HEAD(&pring->txq);
  8097. INIT_LIST_HEAD(&pring->txcmplq);
  8098. INIT_LIST_HEAD(&pring->iocb_continueq);
  8099. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  8100. INIT_LIST_HEAD(&pring->postbufq);
  8101. spin_lock_init(&pring->ring_lock);
  8102. }
  8103. spin_unlock_irq(&phba->hbalock);
  8104. return 1;
  8105. }
  8106. /**
  8107. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  8108. * @phba: Pointer to HBA context object.
  8109. *
  8110. * This routine flushes the mailbox command subsystem. It will unconditionally
  8111. * flush all the mailbox commands in the three possible stages in the mailbox
  8112. * command sub-system: pending mailbox command queue; the outstanding mailbox
  8113. * command; and completed mailbox command queue. It is caller's responsibility
  8114. * to make sure that the driver is in the proper state to flush the mailbox
  8115. * command sub-system. Namely, the posting of mailbox commands into the
  8116. * pending mailbox command queue from the various clients must be stopped;
  8117. * either the HBA is in a state that it will never works on the outstanding
  8118. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  8119. * mailbox command has been completed.
  8120. **/
  8121. static void
  8122. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  8123. {
  8124. LIST_HEAD(completions);
  8125. struct lpfc_sli *psli = &phba->sli;
  8126. LPFC_MBOXQ_t *pmb;
  8127. unsigned long iflag;
  8128. /* Flush all the mailbox commands in the mbox system */
  8129. spin_lock_irqsave(&phba->hbalock, iflag);
  8130. /* The pending mailbox command queue */
  8131. list_splice_init(&phba->sli.mboxq, &completions);
  8132. /* The outstanding active mailbox command */
  8133. if (psli->mbox_active) {
  8134. list_add_tail(&psli->mbox_active->list, &completions);
  8135. psli->mbox_active = NULL;
  8136. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  8137. }
  8138. /* The completed mailbox command queue */
  8139. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  8140. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8141. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  8142. while (!list_empty(&completions)) {
  8143. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  8144. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  8145. if (pmb->mbox_cmpl)
  8146. pmb->mbox_cmpl(phba, pmb);
  8147. }
  8148. }
  8149. /**
  8150. * lpfc_sli_host_down - Vport cleanup function
  8151. * @vport: Pointer to virtual port object.
  8152. *
  8153. * lpfc_sli_host_down is called to clean up the resources
  8154. * associated with a vport before destroying virtual
  8155. * port data structures.
  8156. * This function does following operations:
  8157. * - Free discovery resources associated with this virtual
  8158. * port.
  8159. * - Free iocbs associated with this virtual port in
  8160. * the txq.
  8161. * - Send abort for all iocb commands associated with this
  8162. * vport in txcmplq.
  8163. *
  8164. * This function is called with no lock held and always returns 1.
  8165. **/
  8166. int
  8167. lpfc_sli_host_down(struct lpfc_vport *vport)
  8168. {
  8169. LIST_HEAD(completions);
  8170. struct lpfc_hba *phba = vport->phba;
  8171. struct lpfc_sli *psli = &phba->sli;
  8172. struct lpfc_sli_ring *pring;
  8173. struct lpfc_iocbq *iocb, *next_iocb;
  8174. int i;
  8175. unsigned long flags = 0;
  8176. uint16_t prev_pring_flag;
  8177. lpfc_cleanup_discovery_resources(vport);
  8178. spin_lock_irqsave(&phba->hbalock, flags);
  8179. for (i = 0; i < psli->num_rings; i++) {
  8180. pring = &psli->ring[i];
  8181. prev_pring_flag = pring->flag;
  8182. /* Only slow rings */
  8183. if (pring->ringno == LPFC_ELS_RING) {
  8184. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8185. /* Set the lpfc data pending flag */
  8186. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8187. }
  8188. /*
  8189. * Error everything on the txq since these iocbs have not been
  8190. * given to the FW yet.
  8191. */
  8192. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  8193. if (iocb->vport != vport)
  8194. continue;
  8195. list_move_tail(&iocb->list, &completions);
  8196. pring->txq_cnt--;
  8197. }
  8198. /* Next issue ABTS for everything on the txcmplq */
  8199. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8200. list) {
  8201. if (iocb->vport != vport)
  8202. continue;
  8203. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8204. }
  8205. pring->flag = prev_pring_flag;
  8206. }
  8207. spin_unlock_irqrestore(&phba->hbalock, flags);
  8208. /* Cancel all the IOCBs from the completions list */
  8209. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8210. IOERR_SLI_DOWN);
  8211. return 1;
  8212. }
  8213. /**
  8214. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8215. * @phba: Pointer to HBA context object.
  8216. *
  8217. * This function cleans up all iocb, buffers, mailbox commands
  8218. * while shutting down the HBA. This function is called with no
  8219. * lock held and always returns 1.
  8220. * This function does the following to cleanup driver resources:
  8221. * - Free discovery resources for each virtual port
  8222. * - Cleanup any pending fabric iocbs
  8223. * - Iterate through the iocb txq and free each entry
  8224. * in the list.
  8225. * - Free up any buffer posted to the HBA
  8226. * - Free mailbox commands in the mailbox queue.
  8227. **/
  8228. int
  8229. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8230. {
  8231. LIST_HEAD(completions);
  8232. struct lpfc_sli *psli = &phba->sli;
  8233. struct lpfc_sli_ring *pring;
  8234. struct lpfc_dmabuf *buf_ptr;
  8235. unsigned long flags = 0;
  8236. int i;
  8237. /* Shutdown the mailbox command sub-system */
  8238. lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
  8239. lpfc_hba_down_prep(phba);
  8240. lpfc_fabric_abort_hba(phba);
  8241. spin_lock_irqsave(&phba->hbalock, flags);
  8242. for (i = 0; i < psli->num_rings; i++) {
  8243. pring = &psli->ring[i];
  8244. /* Only slow rings */
  8245. if (pring->ringno == LPFC_ELS_RING) {
  8246. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8247. /* Set the lpfc data pending flag */
  8248. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8249. }
  8250. /*
  8251. * Error everything on the txq since these iocbs have not been
  8252. * given to the FW yet.
  8253. */
  8254. list_splice_init(&pring->txq, &completions);
  8255. pring->txq_cnt = 0;
  8256. }
  8257. spin_unlock_irqrestore(&phba->hbalock, flags);
  8258. /* Cancel all the IOCBs from the completions list */
  8259. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8260. IOERR_SLI_DOWN);
  8261. spin_lock_irqsave(&phba->hbalock, flags);
  8262. list_splice_init(&phba->elsbuf, &completions);
  8263. phba->elsbuf_cnt = 0;
  8264. phba->elsbuf_prev_cnt = 0;
  8265. spin_unlock_irqrestore(&phba->hbalock, flags);
  8266. while (!list_empty(&completions)) {
  8267. list_remove_head(&completions, buf_ptr,
  8268. struct lpfc_dmabuf, list);
  8269. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8270. kfree(buf_ptr);
  8271. }
  8272. /* Return any active mbox cmds */
  8273. del_timer_sync(&psli->mbox_tmo);
  8274. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8275. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8276. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8277. return 1;
  8278. }
  8279. /**
  8280. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8281. * @srcp: Source memory pointer.
  8282. * @destp: Destination memory pointer.
  8283. * @cnt: Number of words required to be copied.
  8284. *
  8285. * This function is used for copying data between driver memory
  8286. * and the SLI memory. This function also changes the endianness
  8287. * of each word if native endianness is different from SLI
  8288. * endianness. This function can be called with or without
  8289. * lock.
  8290. **/
  8291. void
  8292. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8293. {
  8294. uint32_t *src = srcp;
  8295. uint32_t *dest = destp;
  8296. uint32_t ldata;
  8297. int i;
  8298. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8299. ldata = *src;
  8300. ldata = le32_to_cpu(ldata);
  8301. *dest = ldata;
  8302. src++;
  8303. dest++;
  8304. }
  8305. }
  8306. /**
  8307. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8308. * @srcp: Source memory pointer.
  8309. * @destp: Destination memory pointer.
  8310. * @cnt: Number of words required to be copied.
  8311. *
  8312. * This function is used for copying data between a data structure
  8313. * with big endian representation to local endianness.
  8314. * This function can be called with or without lock.
  8315. **/
  8316. void
  8317. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8318. {
  8319. uint32_t *src = srcp;
  8320. uint32_t *dest = destp;
  8321. uint32_t ldata;
  8322. int i;
  8323. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8324. ldata = *src;
  8325. ldata = be32_to_cpu(ldata);
  8326. *dest = ldata;
  8327. src++;
  8328. dest++;
  8329. }
  8330. }
  8331. /**
  8332. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8333. * @phba: Pointer to HBA context object.
  8334. * @pring: Pointer to driver SLI ring object.
  8335. * @mp: Pointer to driver buffer object.
  8336. *
  8337. * This function is called with no lock held.
  8338. * It always return zero after adding the buffer to the postbufq
  8339. * buffer list.
  8340. **/
  8341. int
  8342. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8343. struct lpfc_dmabuf *mp)
  8344. {
  8345. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8346. later */
  8347. spin_lock_irq(&phba->hbalock);
  8348. list_add_tail(&mp->list, &pring->postbufq);
  8349. pring->postbufq_cnt++;
  8350. spin_unlock_irq(&phba->hbalock);
  8351. return 0;
  8352. }
  8353. /**
  8354. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8355. * @phba: Pointer to HBA context object.
  8356. *
  8357. * When HBQ is enabled, buffers are searched based on tags. This function
  8358. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8359. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8360. * does not conflict with tags of buffer posted for unsolicited events.
  8361. * The function returns the allocated tag. The function is called with
  8362. * no locks held.
  8363. **/
  8364. uint32_t
  8365. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8366. {
  8367. spin_lock_irq(&phba->hbalock);
  8368. phba->buffer_tag_count++;
  8369. /*
  8370. * Always set the QUE_BUFTAG_BIT to distiguish between
  8371. * a tag assigned by HBQ.
  8372. */
  8373. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8374. spin_unlock_irq(&phba->hbalock);
  8375. return phba->buffer_tag_count;
  8376. }
  8377. /**
  8378. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8379. * @phba: Pointer to HBA context object.
  8380. * @pring: Pointer to driver SLI ring object.
  8381. * @tag: Buffer tag.
  8382. *
  8383. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8384. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8385. * iocb is posted to the response ring with the tag of the buffer.
  8386. * This function searches the pring->postbufq list using the tag
  8387. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8388. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8389. * buffer is returned to the caller else NULL is returned.
  8390. * This function is called with no lock held.
  8391. **/
  8392. struct lpfc_dmabuf *
  8393. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8394. uint32_t tag)
  8395. {
  8396. struct lpfc_dmabuf *mp, *next_mp;
  8397. struct list_head *slp = &pring->postbufq;
  8398. /* Search postbufq, from the beginning, looking for a match on tag */
  8399. spin_lock_irq(&phba->hbalock);
  8400. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8401. if (mp->buffer_tag == tag) {
  8402. list_del_init(&mp->list);
  8403. pring->postbufq_cnt--;
  8404. spin_unlock_irq(&phba->hbalock);
  8405. return mp;
  8406. }
  8407. }
  8408. spin_unlock_irq(&phba->hbalock);
  8409. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8410. "0402 Cannot find virtual addr for buffer tag on "
  8411. "ring %d Data x%lx x%p x%p x%x\n",
  8412. pring->ringno, (unsigned long) tag,
  8413. slp->next, slp->prev, pring->postbufq_cnt);
  8414. return NULL;
  8415. }
  8416. /**
  8417. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8418. * @phba: Pointer to HBA context object.
  8419. * @pring: Pointer to driver SLI ring object.
  8420. * @phys: DMA address of the buffer.
  8421. *
  8422. * This function searches the buffer list using the dma_address
  8423. * of unsolicited event to find the driver's lpfc_dmabuf object
  8424. * corresponding to the dma_address. The function returns the
  8425. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8426. * This function is called by the ct and els unsolicited event
  8427. * handlers to get the buffer associated with the unsolicited
  8428. * event.
  8429. *
  8430. * This function is called with no lock held.
  8431. **/
  8432. struct lpfc_dmabuf *
  8433. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8434. dma_addr_t phys)
  8435. {
  8436. struct lpfc_dmabuf *mp, *next_mp;
  8437. struct list_head *slp = &pring->postbufq;
  8438. /* Search postbufq, from the beginning, looking for a match on phys */
  8439. spin_lock_irq(&phba->hbalock);
  8440. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8441. if (mp->phys == phys) {
  8442. list_del_init(&mp->list);
  8443. pring->postbufq_cnt--;
  8444. spin_unlock_irq(&phba->hbalock);
  8445. return mp;
  8446. }
  8447. }
  8448. spin_unlock_irq(&phba->hbalock);
  8449. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8450. "0410 Cannot find virtual addr for mapped buf on "
  8451. "ring %d Data x%llx x%p x%p x%x\n",
  8452. pring->ringno, (unsigned long long)phys,
  8453. slp->next, slp->prev, pring->postbufq_cnt);
  8454. return NULL;
  8455. }
  8456. /**
  8457. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8458. * @phba: Pointer to HBA context object.
  8459. * @cmdiocb: Pointer to driver command iocb object.
  8460. * @rspiocb: Pointer to driver response iocb object.
  8461. *
  8462. * This function is the completion handler for the abort iocbs for
  8463. * ELS commands. This function is called from the ELS ring event
  8464. * handler with no lock held. This function frees memory resources
  8465. * associated with the abort iocb.
  8466. **/
  8467. static void
  8468. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8469. struct lpfc_iocbq *rspiocb)
  8470. {
  8471. IOCB_t *irsp = &rspiocb->iocb;
  8472. uint16_t abort_iotag, abort_context;
  8473. struct lpfc_iocbq *abort_iocb = NULL;
  8474. if (irsp->ulpStatus) {
  8475. /*
  8476. * Assume that the port already completed and returned, or
  8477. * will return the iocb. Just Log the message.
  8478. */
  8479. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8480. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8481. spin_lock_irq(&phba->hbalock);
  8482. if (phba->sli_rev < LPFC_SLI_REV4) {
  8483. if (abort_iotag != 0 &&
  8484. abort_iotag <= phba->sli.last_iotag)
  8485. abort_iocb =
  8486. phba->sli.iocbq_lookup[abort_iotag];
  8487. } else
  8488. /* For sli4 the abort_tag is the XRI,
  8489. * so the abort routine puts the iotag of the iocb
  8490. * being aborted in the context field of the abort
  8491. * IOCB.
  8492. */
  8493. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8494. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8495. "0327 Cannot abort els iocb %p "
  8496. "with tag %x context %x, abort status %x, "
  8497. "abort code %x\n",
  8498. abort_iocb, abort_iotag, abort_context,
  8499. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8500. spin_unlock_irq(&phba->hbalock);
  8501. }
  8502. lpfc_sli_release_iocbq(phba, cmdiocb);
  8503. return;
  8504. }
  8505. /**
  8506. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8507. * @phba: Pointer to HBA context object.
  8508. * @cmdiocb: Pointer to driver command iocb object.
  8509. * @rspiocb: Pointer to driver response iocb object.
  8510. *
  8511. * The function is called from SLI ring event handler with no
  8512. * lock held. This function is the completion handler for ELS commands
  8513. * which are aborted. The function frees memory resources used for
  8514. * the aborted ELS commands.
  8515. **/
  8516. static void
  8517. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8518. struct lpfc_iocbq *rspiocb)
  8519. {
  8520. IOCB_t *irsp = &rspiocb->iocb;
  8521. /* ELS cmd tag <ulpIoTag> completes */
  8522. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8523. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8524. "x%x x%x x%x\n",
  8525. irsp->ulpIoTag, irsp->ulpStatus,
  8526. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8527. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8528. lpfc_ct_free_iocb(phba, cmdiocb);
  8529. else
  8530. lpfc_els_free_iocb(phba, cmdiocb);
  8531. return;
  8532. }
  8533. /**
  8534. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8535. * @phba: Pointer to HBA context object.
  8536. * @pring: Pointer to driver SLI ring object.
  8537. * @cmdiocb: Pointer to driver command iocb object.
  8538. *
  8539. * This function issues an abort iocb for the provided command iocb down to
  8540. * the port. Other than the case the outstanding command iocb is an abort
  8541. * request, this function issues abort out unconditionally. This function is
  8542. * called with hbalock held. The function returns 0 when it fails due to
  8543. * memory allocation failure or when the command iocb is an abort request.
  8544. **/
  8545. static int
  8546. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8547. struct lpfc_iocbq *cmdiocb)
  8548. {
  8549. struct lpfc_vport *vport = cmdiocb->vport;
  8550. struct lpfc_iocbq *abtsiocbp;
  8551. IOCB_t *icmd = NULL;
  8552. IOCB_t *iabt = NULL;
  8553. int retval;
  8554. unsigned long iflags;
  8555. /*
  8556. * There are certain command types we don't want to abort. And we
  8557. * don't want to abort commands that are already in the process of
  8558. * being aborted.
  8559. */
  8560. icmd = &cmdiocb->iocb;
  8561. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8562. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8563. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8564. return 0;
  8565. /* issue ABTS for this IOCB based on iotag */
  8566. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8567. if (abtsiocbp == NULL)
  8568. return 0;
  8569. /* This signals the response to set the correct status
  8570. * before calling the completion handler
  8571. */
  8572. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8573. iabt = &abtsiocbp->iocb;
  8574. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8575. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8576. if (phba->sli_rev == LPFC_SLI_REV4) {
  8577. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8578. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8579. }
  8580. else
  8581. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8582. iabt->ulpLe = 1;
  8583. iabt->ulpClass = icmd->ulpClass;
  8584. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8585. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8586. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8587. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8588. if (phba->link_state >= LPFC_LINK_UP)
  8589. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8590. else
  8591. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8592. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8593. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8594. "0339 Abort xri x%x, original iotag x%x, "
  8595. "abort cmd iotag x%x\n",
  8596. iabt->un.acxri.abortIoTag,
  8597. iabt->un.acxri.abortContextTag,
  8598. abtsiocbp->iotag);
  8599. if (phba->sli_rev == LPFC_SLI_REV4) {
  8600. /* Note: both hbalock and ring_lock need to be set here */
  8601. spin_lock_irqsave(&pring->ring_lock, iflags);
  8602. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8603. abtsiocbp, 0);
  8604. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  8605. } else {
  8606. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8607. abtsiocbp, 0);
  8608. }
  8609. if (retval)
  8610. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8611. /*
  8612. * Caller to this routine should check for IOCB_ERROR
  8613. * and handle it properly. This routine no longer removes
  8614. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8615. */
  8616. return retval;
  8617. }
  8618. /**
  8619. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8620. * @phba: Pointer to HBA context object.
  8621. * @pring: Pointer to driver SLI ring object.
  8622. * @cmdiocb: Pointer to driver command iocb object.
  8623. *
  8624. * This function issues an abort iocb for the provided command iocb. In case
  8625. * of unloading, the abort iocb will not be issued to commands on the ELS
  8626. * ring. Instead, the callback function shall be changed to those commands
  8627. * so that nothing happens when them finishes. This function is called with
  8628. * hbalock held. The function returns 0 when the command iocb is an abort
  8629. * request.
  8630. **/
  8631. int
  8632. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8633. struct lpfc_iocbq *cmdiocb)
  8634. {
  8635. struct lpfc_vport *vport = cmdiocb->vport;
  8636. int retval = IOCB_ERROR;
  8637. IOCB_t *icmd = NULL;
  8638. /*
  8639. * There are certain command types we don't want to abort. And we
  8640. * don't want to abort commands that are already in the process of
  8641. * being aborted.
  8642. */
  8643. icmd = &cmdiocb->iocb;
  8644. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8645. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8646. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8647. return 0;
  8648. /*
  8649. * If we're unloading, don't abort iocb on the ELS ring, but change
  8650. * the callback so that nothing happens when it finishes.
  8651. */
  8652. if ((vport->load_flag & FC_UNLOADING) &&
  8653. (pring->ringno == LPFC_ELS_RING)) {
  8654. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8655. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8656. else
  8657. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8658. goto abort_iotag_exit;
  8659. }
  8660. /* Now, we try to issue the abort to the cmdiocb out */
  8661. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8662. abort_iotag_exit:
  8663. /*
  8664. * Caller to this routine should check for IOCB_ERROR
  8665. * and handle it properly. This routine no longer removes
  8666. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8667. */
  8668. return retval;
  8669. }
  8670. /**
  8671. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8672. * @phba: Pointer to HBA context object.
  8673. * @pring: Pointer to driver SLI ring object.
  8674. *
  8675. * This function aborts all iocbs in the given ring and frees all the iocb
  8676. * objects in txq. This function issues abort iocbs unconditionally for all
  8677. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8678. * to complete before the return of this function. The caller is not required
  8679. * to hold any locks.
  8680. **/
  8681. static void
  8682. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8683. {
  8684. LIST_HEAD(completions);
  8685. struct lpfc_iocbq *iocb, *next_iocb;
  8686. if (pring->ringno == LPFC_ELS_RING)
  8687. lpfc_fabric_abort_hba(phba);
  8688. spin_lock_irq(&phba->hbalock);
  8689. /* Take off all the iocbs on txq for cancelling */
  8690. list_splice_init(&pring->txq, &completions);
  8691. pring->txq_cnt = 0;
  8692. /* Next issue ABTS for everything on the txcmplq */
  8693. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8694. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8695. spin_unlock_irq(&phba->hbalock);
  8696. /* Cancel all the IOCBs from the completions list */
  8697. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8698. IOERR_SLI_ABORTED);
  8699. }
  8700. /**
  8701. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8702. * @phba: pointer to lpfc HBA data structure.
  8703. *
  8704. * This routine will abort all pending and outstanding iocbs to an HBA.
  8705. **/
  8706. void
  8707. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8708. {
  8709. struct lpfc_sli *psli = &phba->sli;
  8710. struct lpfc_sli_ring *pring;
  8711. int i;
  8712. for (i = 0; i < psli->num_rings; i++) {
  8713. pring = &psli->ring[i];
  8714. lpfc_sli_iocb_ring_abort(phba, pring);
  8715. }
  8716. }
  8717. /**
  8718. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8719. * @iocbq: Pointer to driver iocb object.
  8720. * @vport: Pointer to driver virtual port object.
  8721. * @tgt_id: SCSI ID of the target.
  8722. * @lun_id: LUN ID of the scsi device.
  8723. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8724. *
  8725. * This function acts as an iocb filter for functions which abort or count
  8726. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8727. * 0 if the filtering criteria is met for the given iocb and will return
  8728. * 1 if the filtering criteria is not met.
  8729. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8730. * given iocb is for the SCSI device specified by vport, tgt_id and
  8731. * lun_id parameter.
  8732. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8733. * given iocb is for the SCSI target specified by vport and tgt_id
  8734. * parameters.
  8735. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8736. * given iocb is for the SCSI host associated with the given vport.
  8737. * This function is called with no locks held.
  8738. **/
  8739. static int
  8740. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8741. uint16_t tgt_id, uint64_t lun_id,
  8742. lpfc_ctx_cmd ctx_cmd)
  8743. {
  8744. struct lpfc_scsi_buf *lpfc_cmd;
  8745. int rc = 1;
  8746. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8747. return rc;
  8748. if (iocbq->vport != vport)
  8749. return rc;
  8750. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8751. if (lpfc_cmd->pCmd == NULL)
  8752. return rc;
  8753. switch (ctx_cmd) {
  8754. case LPFC_CTX_LUN:
  8755. if ((lpfc_cmd->rdata->pnode) &&
  8756. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8757. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8758. rc = 0;
  8759. break;
  8760. case LPFC_CTX_TGT:
  8761. if ((lpfc_cmd->rdata->pnode) &&
  8762. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8763. rc = 0;
  8764. break;
  8765. case LPFC_CTX_HOST:
  8766. rc = 0;
  8767. break;
  8768. default:
  8769. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8770. __func__, ctx_cmd);
  8771. break;
  8772. }
  8773. return rc;
  8774. }
  8775. /**
  8776. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8777. * @vport: Pointer to virtual port.
  8778. * @tgt_id: SCSI ID of the target.
  8779. * @lun_id: LUN ID of the scsi device.
  8780. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8781. *
  8782. * This function returns number of FCP commands pending for the vport.
  8783. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8784. * commands pending on the vport associated with SCSI device specified
  8785. * by tgt_id and lun_id parameters.
  8786. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8787. * commands pending on the vport associated with SCSI target specified
  8788. * by tgt_id parameter.
  8789. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8790. * commands pending on the vport.
  8791. * This function returns the number of iocbs which satisfy the filter.
  8792. * This function is called without any lock held.
  8793. **/
  8794. int
  8795. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8796. lpfc_ctx_cmd ctx_cmd)
  8797. {
  8798. struct lpfc_hba *phba = vport->phba;
  8799. struct lpfc_iocbq *iocbq;
  8800. int sum, i;
  8801. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8802. iocbq = phba->sli.iocbq_lookup[i];
  8803. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8804. ctx_cmd) == 0)
  8805. sum++;
  8806. }
  8807. return sum;
  8808. }
  8809. /**
  8810. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8811. * @phba: Pointer to HBA context object
  8812. * @cmdiocb: Pointer to command iocb object.
  8813. * @rspiocb: Pointer to response iocb object.
  8814. *
  8815. * This function is called when an aborted FCP iocb completes. This
  8816. * function is called by the ring event handler with no lock held.
  8817. * This function frees the iocb.
  8818. **/
  8819. void
  8820. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8821. struct lpfc_iocbq *rspiocb)
  8822. {
  8823. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8824. "3096 ABORT_XRI_CN completing on xri x%x "
  8825. "original iotag x%x, abort cmd iotag x%x "
  8826. "status 0x%x, reason 0x%x\n",
  8827. cmdiocb->iocb.un.acxri.abortContextTag,
  8828. cmdiocb->iocb.un.acxri.abortIoTag,
  8829. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  8830. rspiocb->iocb.un.ulpWord[4]);
  8831. lpfc_sli_release_iocbq(phba, cmdiocb);
  8832. return;
  8833. }
  8834. /**
  8835. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8836. * @vport: Pointer to virtual port.
  8837. * @pring: Pointer to driver SLI ring object.
  8838. * @tgt_id: SCSI ID of the target.
  8839. * @lun_id: LUN ID of the scsi device.
  8840. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8841. *
  8842. * This function sends an abort command for every SCSI command
  8843. * associated with the given virtual port pending on the ring
  8844. * filtered by lpfc_sli_validate_fcp_iocb function.
  8845. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8846. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8847. * parameters
  8848. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8849. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8850. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8851. * FCP iocbs associated with virtual port.
  8852. * This function returns number of iocbs it failed to abort.
  8853. * This function is called with no locks held.
  8854. **/
  8855. int
  8856. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8857. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8858. {
  8859. struct lpfc_hba *phba = vport->phba;
  8860. struct lpfc_iocbq *iocbq;
  8861. struct lpfc_iocbq *abtsiocb;
  8862. IOCB_t *cmd = NULL;
  8863. int errcnt = 0, ret_val = 0;
  8864. int i;
  8865. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8866. iocbq = phba->sli.iocbq_lookup[i];
  8867. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8868. abort_cmd) != 0)
  8869. continue;
  8870. /* issue ABTS for this IOCB based on iotag */
  8871. abtsiocb = lpfc_sli_get_iocbq(phba);
  8872. if (abtsiocb == NULL) {
  8873. errcnt++;
  8874. continue;
  8875. }
  8876. cmd = &iocbq->iocb;
  8877. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8878. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8879. if (phba->sli_rev == LPFC_SLI_REV4)
  8880. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8881. else
  8882. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8883. abtsiocb->iocb.ulpLe = 1;
  8884. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8885. abtsiocb->vport = phba->pport;
  8886. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8887. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8888. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8889. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8890. if (lpfc_is_link_up(phba))
  8891. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8892. else
  8893. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8894. /* Setup callback routine and issue the command. */
  8895. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8896. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8897. abtsiocb, 0);
  8898. if (ret_val == IOCB_ERROR) {
  8899. lpfc_sli_release_iocbq(phba, abtsiocb);
  8900. errcnt++;
  8901. continue;
  8902. }
  8903. }
  8904. return errcnt;
  8905. }
  8906. /**
  8907. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8908. * @phba: Pointer to HBA context object.
  8909. * @cmdiocbq: Pointer to command iocb.
  8910. * @rspiocbq: Pointer to response iocb.
  8911. *
  8912. * This function is the completion handler for iocbs issued using
  8913. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8914. * ring event handler function without any lock held. This function
  8915. * can be called from both worker thread context and interrupt
  8916. * context. This function also can be called from other thread which
  8917. * cleans up the SLI layer objects.
  8918. * This function copy the contents of the response iocb to the
  8919. * response iocb memory object provided by the caller of
  8920. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8921. * sleeps for the iocb completion.
  8922. **/
  8923. static void
  8924. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8925. struct lpfc_iocbq *cmdiocbq,
  8926. struct lpfc_iocbq *rspiocbq)
  8927. {
  8928. wait_queue_head_t *pdone_q;
  8929. unsigned long iflags;
  8930. struct lpfc_scsi_buf *lpfc_cmd;
  8931. spin_lock_irqsave(&phba->hbalock, iflags);
  8932. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8933. if (cmdiocbq->context2 && rspiocbq)
  8934. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8935. &rspiocbq->iocb, sizeof(IOCB_t));
  8936. /* Set the exchange busy flag for task management commands */
  8937. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8938. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  8939. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  8940. cur_iocbq);
  8941. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  8942. }
  8943. pdone_q = cmdiocbq->context_un.wait_queue;
  8944. if (pdone_q)
  8945. wake_up(pdone_q);
  8946. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8947. return;
  8948. }
  8949. /**
  8950. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  8951. * @phba: Pointer to HBA context object..
  8952. * @piocbq: Pointer to command iocb.
  8953. * @flag: Flag to test.
  8954. *
  8955. * This routine grabs the hbalock and then test the iocb_flag to
  8956. * see if the passed in flag is set.
  8957. * Returns:
  8958. * 1 if flag is set.
  8959. * 0 if flag is not set.
  8960. **/
  8961. static int
  8962. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  8963. struct lpfc_iocbq *piocbq, uint32_t flag)
  8964. {
  8965. unsigned long iflags;
  8966. int ret;
  8967. spin_lock_irqsave(&phba->hbalock, iflags);
  8968. ret = piocbq->iocb_flag & flag;
  8969. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8970. return ret;
  8971. }
  8972. /**
  8973. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  8974. * @phba: Pointer to HBA context object..
  8975. * @pring: Pointer to sli ring.
  8976. * @piocb: Pointer to command iocb.
  8977. * @prspiocbq: Pointer to response iocb.
  8978. * @timeout: Timeout in number of seconds.
  8979. *
  8980. * This function issues the iocb to firmware and waits for the
  8981. * iocb to complete. If the iocb command is not
  8982. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  8983. * Caller should not free the iocb resources if this function
  8984. * returns IOCB_TIMEDOUT.
  8985. * The function waits for the iocb completion using an
  8986. * non-interruptible wait.
  8987. * This function will sleep while waiting for iocb completion.
  8988. * So, this function should not be called from any context which
  8989. * does not allow sleeping. Due to the same reason, this function
  8990. * cannot be called with interrupt disabled.
  8991. * This function assumes that the iocb completions occur while
  8992. * this function sleep. So, this function cannot be called from
  8993. * the thread which process iocb completion for this ring.
  8994. * This function clears the iocb_flag of the iocb object before
  8995. * issuing the iocb and the iocb completion handler sets this
  8996. * flag and wakes this thread when the iocb completes.
  8997. * The contents of the response iocb will be copied to prspiocbq
  8998. * by the completion handler when the command completes.
  8999. * This function returns IOCB_SUCCESS when success.
  9000. * This function is called with no lock held.
  9001. **/
  9002. int
  9003. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  9004. uint32_t ring_number,
  9005. struct lpfc_iocbq *piocb,
  9006. struct lpfc_iocbq *prspiocbq,
  9007. uint32_t timeout)
  9008. {
  9009. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9010. long timeleft, timeout_req = 0;
  9011. int retval = IOCB_SUCCESS;
  9012. uint32_t creg_val;
  9013. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9014. /*
  9015. * If the caller has provided a response iocbq buffer, then context2
  9016. * is NULL or its an error.
  9017. */
  9018. if (prspiocbq) {
  9019. if (piocb->context2)
  9020. return IOCB_ERROR;
  9021. piocb->context2 = prspiocbq;
  9022. }
  9023. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  9024. piocb->context_un.wait_queue = &done_q;
  9025. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  9026. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9027. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9028. return IOCB_ERROR;
  9029. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  9030. writel(creg_val, phba->HCregaddr);
  9031. readl(phba->HCregaddr); /* flush */
  9032. }
  9033. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  9034. SLI_IOCB_RET_IOCB);
  9035. if (retval == IOCB_SUCCESS) {
  9036. timeout_req = timeout * HZ;
  9037. timeleft = wait_event_timeout(done_q,
  9038. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  9039. timeout_req);
  9040. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  9041. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9042. "0331 IOCB wake signaled\n");
  9043. } else if (timeleft == 0) {
  9044. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9045. "0338 IOCB wait timeout error - no "
  9046. "wake response Data x%x\n", timeout);
  9047. retval = IOCB_TIMEDOUT;
  9048. } else {
  9049. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9050. "0330 IOCB wake NOT set, "
  9051. "Data x%x x%lx\n",
  9052. timeout, (timeleft / jiffies));
  9053. retval = IOCB_TIMEDOUT;
  9054. }
  9055. } else if (retval == IOCB_BUSY) {
  9056. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9057. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  9058. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  9059. return retval;
  9060. } else {
  9061. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9062. "0332 IOCB wait issue failed, Data x%x\n",
  9063. retval);
  9064. retval = IOCB_ERROR;
  9065. }
  9066. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9067. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9068. return IOCB_ERROR;
  9069. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  9070. writel(creg_val, phba->HCregaddr);
  9071. readl(phba->HCregaddr); /* flush */
  9072. }
  9073. if (prspiocbq)
  9074. piocb->context2 = NULL;
  9075. piocb->context_un.wait_queue = NULL;
  9076. piocb->iocb_cmpl = NULL;
  9077. return retval;
  9078. }
  9079. /**
  9080. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  9081. * @phba: Pointer to HBA context object.
  9082. * @pmboxq: Pointer to driver mailbox object.
  9083. * @timeout: Timeout in number of seconds.
  9084. *
  9085. * This function issues the mailbox to firmware and waits for the
  9086. * mailbox command to complete. If the mailbox command is not
  9087. * completed within timeout seconds, it returns MBX_TIMEOUT.
  9088. * The function waits for the mailbox completion using an
  9089. * interruptible wait. If the thread is woken up due to a
  9090. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  9091. * should not free the mailbox resources, if this function returns
  9092. * MBX_TIMEOUT.
  9093. * This function will sleep while waiting for mailbox completion.
  9094. * So, this function should not be called from any context which
  9095. * does not allow sleeping. Due to the same reason, this function
  9096. * cannot be called with interrupt disabled.
  9097. * This function assumes that the mailbox completion occurs while
  9098. * this function sleep. So, this function cannot be called from
  9099. * the worker thread which processes mailbox completion.
  9100. * This function is called in the context of HBA management
  9101. * applications.
  9102. * This function returns MBX_SUCCESS when successful.
  9103. * This function is called with no lock held.
  9104. **/
  9105. int
  9106. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  9107. uint32_t timeout)
  9108. {
  9109. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9110. int retval;
  9111. unsigned long flag;
  9112. /* The caller must leave context1 empty. */
  9113. if (pmboxq->context1)
  9114. return MBX_NOT_FINISHED;
  9115. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  9116. /* setup wake call as IOCB callback */
  9117. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  9118. /* setup context field to pass wait_queue pointer to wake function */
  9119. pmboxq->context1 = &done_q;
  9120. /* now issue the command */
  9121. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  9122. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  9123. wait_event_interruptible_timeout(done_q,
  9124. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  9125. timeout * HZ);
  9126. spin_lock_irqsave(&phba->hbalock, flag);
  9127. pmboxq->context1 = NULL;
  9128. /*
  9129. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  9130. * else do not free the resources.
  9131. */
  9132. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  9133. retval = MBX_SUCCESS;
  9134. lpfc_sli4_swap_str(phba, pmboxq);
  9135. } else {
  9136. retval = MBX_TIMEOUT;
  9137. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9138. }
  9139. spin_unlock_irqrestore(&phba->hbalock, flag);
  9140. }
  9141. return retval;
  9142. }
  9143. /**
  9144. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  9145. * @phba: Pointer to HBA context.
  9146. *
  9147. * This function is called to shutdown the driver's mailbox sub-system.
  9148. * It first marks the mailbox sub-system is in a block state to prevent
  9149. * the asynchronous mailbox command from issued off the pending mailbox
  9150. * command queue. If the mailbox command sub-system shutdown is due to
  9151. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  9152. * the mailbox sub-system flush routine to forcefully bring down the
  9153. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  9154. * as with offline or HBA function reset), this routine will wait for the
  9155. * outstanding mailbox command to complete before invoking the mailbox
  9156. * sub-system flush routine to gracefully bring down mailbox sub-system.
  9157. **/
  9158. void
  9159. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
  9160. {
  9161. struct lpfc_sli *psli = &phba->sli;
  9162. unsigned long timeout;
  9163. if (mbx_action == LPFC_MBX_NO_WAIT) {
  9164. /* delay 100ms for port state */
  9165. msleep(100);
  9166. lpfc_sli_mbox_sys_flush(phba);
  9167. return;
  9168. }
  9169. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  9170. spin_lock_irq(&phba->hbalock);
  9171. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  9172. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  9173. /* Determine how long we might wait for the active mailbox
  9174. * command to be gracefully completed by firmware.
  9175. */
  9176. if (phba->sli.mbox_active)
  9177. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  9178. phba->sli.mbox_active) *
  9179. 1000) + jiffies;
  9180. spin_unlock_irq(&phba->hbalock);
  9181. while (phba->sli.mbox_active) {
  9182. /* Check active mailbox complete status every 2ms */
  9183. msleep(2);
  9184. if (time_after(jiffies, timeout))
  9185. /* Timeout, let the mailbox flush routine to
  9186. * forcefully release active mailbox command
  9187. */
  9188. break;
  9189. }
  9190. } else
  9191. spin_unlock_irq(&phba->hbalock);
  9192. lpfc_sli_mbox_sys_flush(phba);
  9193. }
  9194. /**
  9195. * lpfc_sli_eratt_read - read sli-3 error attention events
  9196. * @phba: Pointer to HBA context.
  9197. *
  9198. * This function is called to read the SLI3 device error attention registers
  9199. * for possible error attention events. The caller must hold the hostlock
  9200. * with spin_lock_irq().
  9201. *
  9202. * This function returns 1 when there is Error Attention in the Host Attention
  9203. * Register and returns 0 otherwise.
  9204. **/
  9205. static int
  9206. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  9207. {
  9208. uint32_t ha_copy;
  9209. /* Read chip Host Attention (HA) register */
  9210. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9211. goto unplug_err;
  9212. if (ha_copy & HA_ERATT) {
  9213. /* Read host status register to retrieve error event */
  9214. if (lpfc_sli_read_hs(phba))
  9215. goto unplug_err;
  9216. /* Check if there is a deferred error condition is active */
  9217. if ((HS_FFER1 & phba->work_hs) &&
  9218. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9219. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9220. phba->hba_flag |= DEFER_ERATT;
  9221. /* Clear all interrupt enable conditions */
  9222. writel(0, phba->HCregaddr);
  9223. readl(phba->HCregaddr);
  9224. }
  9225. /* Set the driver HA work bitmap */
  9226. phba->work_ha |= HA_ERATT;
  9227. /* Indicate polling handles this ERATT */
  9228. phba->hba_flag |= HBA_ERATT_HANDLED;
  9229. return 1;
  9230. }
  9231. return 0;
  9232. unplug_err:
  9233. /* Set the driver HS work bitmap */
  9234. phba->work_hs |= UNPLUG_ERR;
  9235. /* Set the driver HA work bitmap */
  9236. phba->work_ha |= HA_ERATT;
  9237. /* Indicate polling handles this ERATT */
  9238. phba->hba_flag |= HBA_ERATT_HANDLED;
  9239. return 1;
  9240. }
  9241. /**
  9242. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9243. * @phba: Pointer to HBA context.
  9244. *
  9245. * This function is called to read the SLI4 device error attention registers
  9246. * for possible error attention events. The caller must hold the hostlock
  9247. * with spin_lock_irq().
  9248. *
  9249. * This function returns 1 when there is Error Attention in the Host Attention
  9250. * Register and returns 0 otherwise.
  9251. **/
  9252. static int
  9253. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9254. {
  9255. uint32_t uerr_sta_hi, uerr_sta_lo;
  9256. uint32_t if_type, portsmphr;
  9257. struct lpfc_register portstat_reg;
  9258. /*
  9259. * For now, use the SLI4 device internal unrecoverable error
  9260. * registers for error attention. This can be changed later.
  9261. */
  9262. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9263. switch (if_type) {
  9264. case LPFC_SLI_INTF_IF_TYPE_0:
  9265. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9266. &uerr_sta_lo) ||
  9267. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9268. &uerr_sta_hi)) {
  9269. phba->work_hs |= UNPLUG_ERR;
  9270. phba->work_ha |= HA_ERATT;
  9271. phba->hba_flag |= HBA_ERATT_HANDLED;
  9272. return 1;
  9273. }
  9274. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9275. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9276. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9277. "1423 HBA Unrecoverable error: "
  9278. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9279. "ue_mask_lo_reg=0x%x, "
  9280. "ue_mask_hi_reg=0x%x\n",
  9281. uerr_sta_lo, uerr_sta_hi,
  9282. phba->sli4_hba.ue_mask_lo,
  9283. phba->sli4_hba.ue_mask_hi);
  9284. phba->work_status[0] = uerr_sta_lo;
  9285. phba->work_status[1] = uerr_sta_hi;
  9286. phba->work_ha |= HA_ERATT;
  9287. phba->hba_flag |= HBA_ERATT_HANDLED;
  9288. return 1;
  9289. }
  9290. break;
  9291. case LPFC_SLI_INTF_IF_TYPE_2:
  9292. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9293. &portstat_reg.word0) ||
  9294. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9295. &portsmphr)){
  9296. phba->work_hs |= UNPLUG_ERR;
  9297. phba->work_ha |= HA_ERATT;
  9298. phba->hba_flag |= HBA_ERATT_HANDLED;
  9299. return 1;
  9300. }
  9301. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9302. phba->work_status[0] =
  9303. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9304. phba->work_status[1] =
  9305. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9306. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9307. "2885 Port Status Event: "
  9308. "port status reg 0x%x, "
  9309. "port smphr reg 0x%x, "
  9310. "error 1=0x%x, error 2=0x%x\n",
  9311. portstat_reg.word0,
  9312. portsmphr,
  9313. phba->work_status[0],
  9314. phba->work_status[1]);
  9315. phba->work_ha |= HA_ERATT;
  9316. phba->hba_flag |= HBA_ERATT_HANDLED;
  9317. return 1;
  9318. }
  9319. break;
  9320. case LPFC_SLI_INTF_IF_TYPE_1:
  9321. default:
  9322. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9323. "2886 HBA Error Attention on unsupported "
  9324. "if type %d.", if_type);
  9325. return 1;
  9326. }
  9327. return 0;
  9328. }
  9329. /**
  9330. * lpfc_sli_check_eratt - check error attention events
  9331. * @phba: Pointer to HBA context.
  9332. *
  9333. * This function is called from timer soft interrupt context to check HBA's
  9334. * error attention register bit for error attention events.
  9335. *
  9336. * This function returns 1 when there is Error Attention in the Host Attention
  9337. * Register and returns 0 otherwise.
  9338. **/
  9339. int
  9340. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9341. {
  9342. uint32_t ha_copy;
  9343. /* If somebody is waiting to handle an eratt, don't process it
  9344. * here. The brdkill function will do this.
  9345. */
  9346. if (phba->link_flag & LS_IGNORE_ERATT)
  9347. return 0;
  9348. /* Check if interrupt handler handles this ERATT */
  9349. spin_lock_irq(&phba->hbalock);
  9350. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9351. /* Interrupt handler has handled ERATT */
  9352. spin_unlock_irq(&phba->hbalock);
  9353. return 0;
  9354. }
  9355. /*
  9356. * If there is deferred error attention, do not check for error
  9357. * attention
  9358. */
  9359. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9360. spin_unlock_irq(&phba->hbalock);
  9361. return 0;
  9362. }
  9363. /* If PCI channel is offline, don't process it */
  9364. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9365. spin_unlock_irq(&phba->hbalock);
  9366. return 0;
  9367. }
  9368. switch (phba->sli_rev) {
  9369. case LPFC_SLI_REV2:
  9370. case LPFC_SLI_REV3:
  9371. /* Read chip Host Attention (HA) register */
  9372. ha_copy = lpfc_sli_eratt_read(phba);
  9373. break;
  9374. case LPFC_SLI_REV4:
  9375. /* Read device Uncoverable Error (UERR) registers */
  9376. ha_copy = lpfc_sli4_eratt_read(phba);
  9377. break;
  9378. default:
  9379. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9380. "0299 Invalid SLI revision (%d)\n",
  9381. phba->sli_rev);
  9382. ha_copy = 0;
  9383. break;
  9384. }
  9385. spin_unlock_irq(&phba->hbalock);
  9386. return ha_copy;
  9387. }
  9388. /**
  9389. * lpfc_intr_state_check - Check device state for interrupt handling
  9390. * @phba: Pointer to HBA context.
  9391. *
  9392. * This inline routine checks whether a device or its PCI slot is in a state
  9393. * that the interrupt should be handled.
  9394. *
  9395. * This function returns 0 if the device or the PCI slot is in a state that
  9396. * interrupt should be handled, otherwise -EIO.
  9397. */
  9398. static inline int
  9399. lpfc_intr_state_check(struct lpfc_hba *phba)
  9400. {
  9401. /* If the pci channel is offline, ignore all the interrupts */
  9402. if (unlikely(pci_channel_offline(phba->pcidev)))
  9403. return -EIO;
  9404. /* Update device level interrupt statistics */
  9405. phba->sli.slistat.sli_intr++;
  9406. /* Ignore all interrupts during initialization. */
  9407. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9408. return -EIO;
  9409. return 0;
  9410. }
  9411. /**
  9412. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9413. * @irq: Interrupt number.
  9414. * @dev_id: The device context pointer.
  9415. *
  9416. * This function is directly called from the PCI layer as an interrupt
  9417. * service routine when device with SLI-3 interface spec is enabled with
  9418. * MSI-X multi-message interrupt mode and there are slow-path events in
  9419. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9420. * interrupt mode, this function is called as part of the device-level
  9421. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9422. * is undergoing initialization, the interrupt handler will not process
  9423. * the interrupt. The link attention and ELS ring attention events are
  9424. * handled by the worker thread. The interrupt handler signals the worker
  9425. * thread and returns for these events. This function is called without
  9426. * any lock held. It gets the hbalock to access and update SLI data
  9427. * structures.
  9428. *
  9429. * This function returns IRQ_HANDLED when interrupt is handled else it
  9430. * returns IRQ_NONE.
  9431. **/
  9432. irqreturn_t
  9433. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9434. {
  9435. struct lpfc_hba *phba;
  9436. uint32_t ha_copy, hc_copy;
  9437. uint32_t work_ha_copy;
  9438. unsigned long status;
  9439. unsigned long iflag;
  9440. uint32_t control;
  9441. MAILBOX_t *mbox, *pmbox;
  9442. struct lpfc_vport *vport;
  9443. struct lpfc_nodelist *ndlp;
  9444. struct lpfc_dmabuf *mp;
  9445. LPFC_MBOXQ_t *pmb;
  9446. int rc;
  9447. /*
  9448. * Get the driver's phba structure from the dev_id and
  9449. * assume the HBA is not interrupting.
  9450. */
  9451. phba = (struct lpfc_hba *)dev_id;
  9452. if (unlikely(!phba))
  9453. return IRQ_NONE;
  9454. /*
  9455. * Stuff needs to be attented to when this function is invoked as an
  9456. * individual interrupt handler in MSI-X multi-message interrupt mode
  9457. */
  9458. if (phba->intr_type == MSIX) {
  9459. /* Check device state for handling interrupt */
  9460. if (lpfc_intr_state_check(phba))
  9461. return IRQ_NONE;
  9462. /* Need to read HA REG for slow-path events */
  9463. spin_lock_irqsave(&phba->hbalock, iflag);
  9464. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9465. goto unplug_error;
  9466. /* If somebody is waiting to handle an eratt don't process it
  9467. * here. The brdkill function will do this.
  9468. */
  9469. if (phba->link_flag & LS_IGNORE_ERATT)
  9470. ha_copy &= ~HA_ERATT;
  9471. /* Check the need for handling ERATT in interrupt handler */
  9472. if (ha_copy & HA_ERATT) {
  9473. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9474. /* ERATT polling has handled ERATT */
  9475. ha_copy &= ~HA_ERATT;
  9476. else
  9477. /* Indicate interrupt handler handles ERATT */
  9478. phba->hba_flag |= HBA_ERATT_HANDLED;
  9479. }
  9480. /*
  9481. * If there is deferred error attention, do not check for any
  9482. * interrupt.
  9483. */
  9484. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9485. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9486. return IRQ_NONE;
  9487. }
  9488. /* Clear up only attention source related to slow-path */
  9489. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9490. goto unplug_error;
  9491. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9492. HC_LAINT_ENA | HC_ERINT_ENA),
  9493. phba->HCregaddr);
  9494. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9495. phba->HAregaddr);
  9496. writel(hc_copy, phba->HCregaddr);
  9497. readl(phba->HAregaddr); /* flush */
  9498. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9499. } else
  9500. ha_copy = phba->ha_copy;
  9501. work_ha_copy = ha_copy & phba->work_ha_mask;
  9502. if (work_ha_copy) {
  9503. if (work_ha_copy & HA_LATT) {
  9504. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9505. /*
  9506. * Turn off Link Attention interrupts
  9507. * until CLEAR_LA done
  9508. */
  9509. spin_lock_irqsave(&phba->hbalock, iflag);
  9510. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9511. if (lpfc_readl(phba->HCregaddr, &control))
  9512. goto unplug_error;
  9513. control &= ~HC_LAINT_ENA;
  9514. writel(control, phba->HCregaddr);
  9515. readl(phba->HCregaddr); /* flush */
  9516. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9517. }
  9518. else
  9519. work_ha_copy &= ~HA_LATT;
  9520. }
  9521. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9522. /*
  9523. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9524. * the only slow ring.
  9525. */
  9526. status = (work_ha_copy &
  9527. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9528. status >>= (4*LPFC_ELS_RING);
  9529. if (status & HA_RXMASK) {
  9530. spin_lock_irqsave(&phba->hbalock, iflag);
  9531. if (lpfc_readl(phba->HCregaddr, &control))
  9532. goto unplug_error;
  9533. lpfc_debugfs_slow_ring_trc(phba,
  9534. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9535. control, status,
  9536. (uint32_t)phba->sli.slistat.sli_intr);
  9537. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9538. lpfc_debugfs_slow_ring_trc(phba,
  9539. "ISR Disable ring:"
  9540. "pwork:x%x hawork:x%x wait:x%x",
  9541. phba->work_ha, work_ha_copy,
  9542. (uint32_t)((unsigned long)
  9543. &phba->work_waitq));
  9544. control &=
  9545. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9546. writel(control, phba->HCregaddr);
  9547. readl(phba->HCregaddr); /* flush */
  9548. }
  9549. else {
  9550. lpfc_debugfs_slow_ring_trc(phba,
  9551. "ISR slow ring: pwork:"
  9552. "x%x hawork:x%x wait:x%x",
  9553. phba->work_ha, work_ha_copy,
  9554. (uint32_t)((unsigned long)
  9555. &phba->work_waitq));
  9556. }
  9557. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9558. }
  9559. }
  9560. spin_lock_irqsave(&phba->hbalock, iflag);
  9561. if (work_ha_copy & HA_ERATT) {
  9562. if (lpfc_sli_read_hs(phba))
  9563. goto unplug_error;
  9564. /*
  9565. * Check if there is a deferred error condition
  9566. * is active
  9567. */
  9568. if ((HS_FFER1 & phba->work_hs) &&
  9569. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9570. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9571. phba->work_hs)) {
  9572. phba->hba_flag |= DEFER_ERATT;
  9573. /* Clear all interrupt enable conditions */
  9574. writel(0, phba->HCregaddr);
  9575. readl(phba->HCregaddr);
  9576. }
  9577. }
  9578. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9579. pmb = phba->sli.mbox_active;
  9580. pmbox = &pmb->u.mb;
  9581. mbox = phba->mbox;
  9582. vport = pmb->vport;
  9583. /* First check out the status word */
  9584. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9585. if (pmbox->mbxOwner != OWN_HOST) {
  9586. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9587. /*
  9588. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9589. * mbxStatus <status>
  9590. */
  9591. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9592. LOG_SLI,
  9593. "(%d):0304 Stray Mailbox "
  9594. "Interrupt mbxCommand x%x "
  9595. "mbxStatus x%x\n",
  9596. (vport ? vport->vpi : 0),
  9597. pmbox->mbxCommand,
  9598. pmbox->mbxStatus);
  9599. /* clear mailbox attention bit */
  9600. work_ha_copy &= ~HA_MBATT;
  9601. } else {
  9602. phba->sli.mbox_active = NULL;
  9603. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9604. phba->last_completion_time = jiffies;
  9605. del_timer(&phba->sli.mbox_tmo);
  9606. if (pmb->mbox_cmpl) {
  9607. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9608. MAILBOX_CMD_SIZE);
  9609. if (pmb->out_ext_byte_len &&
  9610. pmb->context2)
  9611. lpfc_sli_pcimem_bcopy(
  9612. phba->mbox_ext,
  9613. pmb->context2,
  9614. pmb->out_ext_byte_len);
  9615. }
  9616. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9617. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9618. lpfc_debugfs_disc_trc(vport,
  9619. LPFC_DISC_TRC_MBOX_VPORT,
  9620. "MBOX dflt rpi: : "
  9621. "status:x%x rpi:x%x",
  9622. (uint32_t)pmbox->mbxStatus,
  9623. pmbox->un.varWords[0], 0);
  9624. if (!pmbox->mbxStatus) {
  9625. mp = (struct lpfc_dmabuf *)
  9626. (pmb->context1);
  9627. ndlp = (struct lpfc_nodelist *)
  9628. pmb->context2;
  9629. /* Reg_LOGIN of dflt RPI was
  9630. * successful. new lets get
  9631. * rid of the RPI using the
  9632. * same mbox buffer.
  9633. */
  9634. lpfc_unreg_login(phba,
  9635. vport->vpi,
  9636. pmbox->un.varWords[0],
  9637. pmb);
  9638. pmb->mbox_cmpl =
  9639. lpfc_mbx_cmpl_dflt_rpi;
  9640. pmb->context1 = mp;
  9641. pmb->context2 = ndlp;
  9642. pmb->vport = vport;
  9643. rc = lpfc_sli_issue_mbox(phba,
  9644. pmb,
  9645. MBX_NOWAIT);
  9646. if (rc != MBX_BUSY)
  9647. lpfc_printf_log(phba,
  9648. KERN_ERR,
  9649. LOG_MBOX | LOG_SLI,
  9650. "0350 rc should have"
  9651. "been MBX_BUSY\n");
  9652. if (rc != MBX_NOT_FINISHED)
  9653. goto send_current_mbox;
  9654. }
  9655. }
  9656. spin_lock_irqsave(
  9657. &phba->pport->work_port_lock,
  9658. iflag);
  9659. phba->pport->work_port_events &=
  9660. ~WORKER_MBOX_TMO;
  9661. spin_unlock_irqrestore(
  9662. &phba->pport->work_port_lock,
  9663. iflag);
  9664. lpfc_mbox_cmpl_put(phba, pmb);
  9665. }
  9666. } else
  9667. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9668. if ((work_ha_copy & HA_MBATT) &&
  9669. (phba->sli.mbox_active == NULL)) {
  9670. send_current_mbox:
  9671. /* Process next mailbox command if there is one */
  9672. do {
  9673. rc = lpfc_sli_issue_mbox(phba, NULL,
  9674. MBX_NOWAIT);
  9675. } while (rc == MBX_NOT_FINISHED);
  9676. if (rc != MBX_SUCCESS)
  9677. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9678. LOG_SLI, "0349 rc should be "
  9679. "MBX_SUCCESS\n");
  9680. }
  9681. spin_lock_irqsave(&phba->hbalock, iflag);
  9682. phba->work_ha |= work_ha_copy;
  9683. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9684. lpfc_worker_wake_up(phba);
  9685. }
  9686. return IRQ_HANDLED;
  9687. unplug_error:
  9688. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9689. return IRQ_HANDLED;
  9690. } /* lpfc_sli_sp_intr_handler */
  9691. /**
  9692. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9693. * @irq: Interrupt number.
  9694. * @dev_id: The device context pointer.
  9695. *
  9696. * This function is directly called from the PCI layer as an interrupt
  9697. * service routine when device with SLI-3 interface spec is enabled with
  9698. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9699. * ring event in the HBA. However, when the device is enabled with either
  9700. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9701. * device-level interrupt handler. When the PCI slot is in error recovery
  9702. * or the HBA is undergoing initialization, the interrupt handler will not
  9703. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9704. * the intrrupt context. This function is called without any lock held.
  9705. * It gets the hbalock to access and update SLI data structures.
  9706. *
  9707. * This function returns IRQ_HANDLED when interrupt is handled else it
  9708. * returns IRQ_NONE.
  9709. **/
  9710. irqreturn_t
  9711. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9712. {
  9713. struct lpfc_hba *phba;
  9714. uint32_t ha_copy;
  9715. unsigned long status;
  9716. unsigned long iflag;
  9717. /* Get the driver's phba structure from the dev_id and
  9718. * assume the HBA is not interrupting.
  9719. */
  9720. phba = (struct lpfc_hba *) dev_id;
  9721. if (unlikely(!phba))
  9722. return IRQ_NONE;
  9723. /*
  9724. * Stuff needs to be attented to when this function is invoked as an
  9725. * individual interrupt handler in MSI-X multi-message interrupt mode
  9726. */
  9727. if (phba->intr_type == MSIX) {
  9728. /* Check device state for handling interrupt */
  9729. if (lpfc_intr_state_check(phba))
  9730. return IRQ_NONE;
  9731. /* Need to read HA REG for FCP ring and other ring events */
  9732. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9733. return IRQ_HANDLED;
  9734. /* Clear up only attention source related to fast-path */
  9735. spin_lock_irqsave(&phba->hbalock, iflag);
  9736. /*
  9737. * If there is deferred error attention, do not check for
  9738. * any interrupt.
  9739. */
  9740. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9741. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9742. return IRQ_NONE;
  9743. }
  9744. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9745. phba->HAregaddr);
  9746. readl(phba->HAregaddr); /* flush */
  9747. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9748. } else
  9749. ha_copy = phba->ha_copy;
  9750. /*
  9751. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9752. */
  9753. ha_copy &= ~(phba->work_ha_mask);
  9754. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9755. status >>= (4*LPFC_FCP_RING);
  9756. if (status & HA_RXMASK)
  9757. lpfc_sli_handle_fast_ring_event(phba,
  9758. &phba->sli.ring[LPFC_FCP_RING],
  9759. status);
  9760. if (phba->cfg_multi_ring_support == 2) {
  9761. /*
  9762. * Process all events on extra ring. Take the optimized path
  9763. * for extra ring IO.
  9764. */
  9765. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9766. status >>= (4*LPFC_EXTRA_RING);
  9767. if (status & HA_RXMASK) {
  9768. lpfc_sli_handle_fast_ring_event(phba,
  9769. &phba->sli.ring[LPFC_EXTRA_RING],
  9770. status);
  9771. }
  9772. }
  9773. return IRQ_HANDLED;
  9774. } /* lpfc_sli_fp_intr_handler */
  9775. /**
  9776. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9777. * @irq: Interrupt number.
  9778. * @dev_id: The device context pointer.
  9779. *
  9780. * This function is the HBA device-level interrupt handler to device with
  9781. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9782. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9783. * requires driver attention. This function invokes the slow-path interrupt
  9784. * attention handling function and fast-path interrupt attention handling
  9785. * function in turn to process the relevant HBA attention events. This
  9786. * function is called without any lock held. It gets the hbalock to access
  9787. * and update SLI data structures.
  9788. *
  9789. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9790. * returns IRQ_NONE.
  9791. **/
  9792. irqreturn_t
  9793. lpfc_sli_intr_handler(int irq, void *dev_id)
  9794. {
  9795. struct lpfc_hba *phba;
  9796. irqreturn_t sp_irq_rc, fp_irq_rc;
  9797. unsigned long status1, status2;
  9798. uint32_t hc_copy;
  9799. /*
  9800. * Get the driver's phba structure from the dev_id and
  9801. * assume the HBA is not interrupting.
  9802. */
  9803. phba = (struct lpfc_hba *) dev_id;
  9804. if (unlikely(!phba))
  9805. return IRQ_NONE;
  9806. /* Check device state for handling interrupt */
  9807. if (lpfc_intr_state_check(phba))
  9808. return IRQ_NONE;
  9809. spin_lock(&phba->hbalock);
  9810. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9811. spin_unlock(&phba->hbalock);
  9812. return IRQ_HANDLED;
  9813. }
  9814. if (unlikely(!phba->ha_copy)) {
  9815. spin_unlock(&phba->hbalock);
  9816. return IRQ_NONE;
  9817. } else if (phba->ha_copy & HA_ERATT) {
  9818. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9819. /* ERATT polling has handled ERATT */
  9820. phba->ha_copy &= ~HA_ERATT;
  9821. else
  9822. /* Indicate interrupt handler handles ERATT */
  9823. phba->hba_flag |= HBA_ERATT_HANDLED;
  9824. }
  9825. /*
  9826. * If there is deferred error attention, do not check for any interrupt.
  9827. */
  9828. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9829. spin_unlock(&phba->hbalock);
  9830. return IRQ_NONE;
  9831. }
  9832. /* Clear attention sources except link and error attentions */
  9833. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9834. spin_unlock(&phba->hbalock);
  9835. return IRQ_HANDLED;
  9836. }
  9837. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9838. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9839. phba->HCregaddr);
  9840. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9841. writel(hc_copy, phba->HCregaddr);
  9842. readl(phba->HAregaddr); /* flush */
  9843. spin_unlock(&phba->hbalock);
  9844. /*
  9845. * Invokes slow-path host attention interrupt handling as appropriate.
  9846. */
  9847. /* status of events with mailbox and link attention */
  9848. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9849. /* status of events with ELS ring */
  9850. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9851. status2 >>= (4*LPFC_ELS_RING);
  9852. if (status1 || (status2 & HA_RXMASK))
  9853. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9854. else
  9855. sp_irq_rc = IRQ_NONE;
  9856. /*
  9857. * Invoke fast-path host attention interrupt handling as appropriate.
  9858. */
  9859. /* status of events with FCP ring */
  9860. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9861. status1 >>= (4*LPFC_FCP_RING);
  9862. /* status of events with extra ring */
  9863. if (phba->cfg_multi_ring_support == 2) {
  9864. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9865. status2 >>= (4*LPFC_EXTRA_RING);
  9866. } else
  9867. status2 = 0;
  9868. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9869. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9870. else
  9871. fp_irq_rc = IRQ_NONE;
  9872. /* Return device-level interrupt handling status */
  9873. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9874. } /* lpfc_sli_intr_handler */
  9875. /**
  9876. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9877. * @phba: pointer to lpfc hba data structure.
  9878. *
  9879. * This routine is invoked by the worker thread to process all the pending
  9880. * SLI4 FCP abort XRI events.
  9881. **/
  9882. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9883. {
  9884. struct lpfc_cq_event *cq_event;
  9885. /* First, declare the fcp xri abort event has been handled */
  9886. spin_lock_irq(&phba->hbalock);
  9887. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9888. spin_unlock_irq(&phba->hbalock);
  9889. /* Now, handle all the fcp xri abort events */
  9890. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9891. /* Get the first event from the head of the event queue */
  9892. spin_lock_irq(&phba->hbalock);
  9893. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9894. cq_event, struct lpfc_cq_event, list);
  9895. spin_unlock_irq(&phba->hbalock);
  9896. /* Notify aborted XRI for FCP work queue */
  9897. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9898. /* Free the event processed back to the free pool */
  9899. lpfc_sli4_cq_event_release(phba, cq_event);
  9900. }
  9901. }
  9902. /**
  9903. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9904. * @phba: pointer to lpfc hba data structure.
  9905. *
  9906. * This routine is invoked by the worker thread to process all the pending
  9907. * SLI4 els abort xri events.
  9908. **/
  9909. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9910. {
  9911. struct lpfc_cq_event *cq_event;
  9912. /* First, declare the els xri abort event has been handled */
  9913. spin_lock_irq(&phba->hbalock);
  9914. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9915. spin_unlock_irq(&phba->hbalock);
  9916. /* Now, handle all the els xri abort events */
  9917. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9918. /* Get the first event from the head of the event queue */
  9919. spin_lock_irq(&phba->hbalock);
  9920. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9921. cq_event, struct lpfc_cq_event, list);
  9922. spin_unlock_irq(&phba->hbalock);
  9923. /* Notify aborted XRI for ELS work queue */
  9924. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9925. /* Free the event processed back to the free pool */
  9926. lpfc_sli4_cq_event_release(phba, cq_event);
  9927. }
  9928. }
  9929. /**
  9930. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9931. * @phba: pointer to lpfc hba data structure
  9932. * @pIocbIn: pointer to the rspiocbq
  9933. * @pIocbOut: pointer to the cmdiocbq
  9934. * @wcqe: pointer to the complete wcqe
  9935. *
  9936. * This routine transfers the fields of a command iocbq to a response iocbq
  9937. * by copying all the IOCB fields from command iocbq and transferring the
  9938. * completion status information from the complete wcqe.
  9939. **/
  9940. static void
  9941. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  9942. struct lpfc_iocbq *pIocbIn,
  9943. struct lpfc_iocbq *pIocbOut,
  9944. struct lpfc_wcqe_complete *wcqe)
  9945. {
  9946. unsigned long iflags;
  9947. uint32_t status;
  9948. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  9949. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  9950. sizeof(struct lpfc_iocbq) - offset);
  9951. /* Map WCQE parameters into irspiocb parameters */
  9952. status = bf_get(lpfc_wcqe_c_status, wcqe);
  9953. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  9954. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  9955. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  9956. pIocbIn->iocb.un.fcpi.fcpi_parm =
  9957. pIocbOut->iocb.un.fcpi.fcpi_parm -
  9958. wcqe->total_data_placed;
  9959. else
  9960. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9961. else {
  9962. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9963. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  9964. }
  9965. /* Convert BG errors for completion status */
  9966. if (status == CQE_STATUS_DI_ERROR) {
  9967. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  9968. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  9969. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  9970. else
  9971. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  9972. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  9973. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  9974. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9975. BGS_GUARD_ERR_MASK;
  9976. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  9977. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9978. BGS_APPTAG_ERR_MASK;
  9979. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  9980. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9981. BGS_REFTAG_ERR_MASK;
  9982. /* Check to see if there was any good data before the error */
  9983. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  9984. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9985. BGS_HI_WATER_MARK_PRESENT_MASK;
  9986. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  9987. wcqe->total_data_placed;
  9988. }
  9989. /*
  9990. * Set ALL the error bits to indicate we don't know what
  9991. * type of error it is.
  9992. */
  9993. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  9994. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9995. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  9996. BGS_GUARD_ERR_MASK);
  9997. }
  9998. /* Pick up HBA exchange busy condition */
  9999. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  10000. spin_lock_irqsave(&phba->hbalock, iflags);
  10001. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  10002. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10003. }
  10004. }
  10005. /**
  10006. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  10007. * @phba: Pointer to HBA context object.
  10008. * @wcqe: Pointer to work-queue completion queue entry.
  10009. *
  10010. * This routine handles an ELS work-queue completion event and construct
  10011. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  10012. * discovery engine to handle.
  10013. *
  10014. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  10015. **/
  10016. static struct lpfc_iocbq *
  10017. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  10018. struct lpfc_iocbq *irspiocbq)
  10019. {
  10020. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  10021. struct lpfc_iocbq *cmdiocbq;
  10022. struct lpfc_wcqe_complete *wcqe;
  10023. unsigned long iflags;
  10024. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  10025. spin_lock_irqsave(&pring->ring_lock, iflags);
  10026. pring->stats.iocb_event++;
  10027. /* Look up the ELS command IOCB and create pseudo response IOCB */
  10028. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10029. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10030. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10031. if (unlikely(!cmdiocbq)) {
  10032. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10033. "0386 ELS complete with no corresponding "
  10034. "cmdiocb: iotag (%d)\n",
  10035. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10036. lpfc_sli_release_iocbq(phba, irspiocbq);
  10037. return NULL;
  10038. }
  10039. /* Fake the irspiocbq and copy necessary response information */
  10040. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  10041. return irspiocbq;
  10042. }
  10043. /**
  10044. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  10045. * @phba: Pointer to HBA context object.
  10046. * @cqe: Pointer to mailbox completion queue entry.
  10047. *
  10048. * This routine process a mailbox completion queue entry with asynchrous
  10049. * event.
  10050. *
  10051. * Return: true if work posted to worker thread, otherwise false.
  10052. **/
  10053. static bool
  10054. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10055. {
  10056. struct lpfc_cq_event *cq_event;
  10057. unsigned long iflags;
  10058. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10059. "0392 Async Event: word0:x%x, word1:x%x, "
  10060. "word2:x%x, word3:x%x\n", mcqe->word0,
  10061. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  10062. /* Allocate a new internal CQ_EVENT entry */
  10063. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10064. if (!cq_event) {
  10065. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10066. "0394 Failed to allocate CQ_EVENT entry\n");
  10067. return false;
  10068. }
  10069. /* Move the CQE into an asynchronous event entry */
  10070. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  10071. spin_lock_irqsave(&phba->hbalock, iflags);
  10072. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  10073. /* Set the async event flag */
  10074. phba->hba_flag |= ASYNC_EVENT;
  10075. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10076. return true;
  10077. }
  10078. /**
  10079. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  10080. * @phba: Pointer to HBA context object.
  10081. * @cqe: Pointer to mailbox completion queue entry.
  10082. *
  10083. * This routine process a mailbox completion queue entry with mailbox
  10084. * completion event.
  10085. *
  10086. * Return: true if work posted to worker thread, otherwise false.
  10087. **/
  10088. static bool
  10089. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10090. {
  10091. uint32_t mcqe_status;
  10092. MAILBOX_t *mbox, *pmbox;
  10093. struct lpfc_mqe *mqe;
  10094. struct lpfc_vport *vport;
  10095. struct lpfc_nodelist *ndlp;
  10096. struct lpfc_dmabuf *mp;
  10097. unsigned long iflags;
  10098. LPFC_MBOXQ_t *pmb;
  10099. bool workposted = false;
  10100. int rc;
  10101. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  10102. if (!bf_get(lpfc_trailer_completed, mcqe))
  10103. goto out_no_mqe_complete;
  10104. /* Get the reference to the active mbox command */
  10105. spin_lock_irqsave(&phba->hbalock, iflags);
  10106. pmb = phba->sli.mbox_active;
  10107. if (unlikely(!pmb)) {
  10108. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10109. "1832 No pending MBOX command to handle\n");
  10110. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10111. goto out_no_mqe_complete;
  10112. }
  10113. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10114. mqe = &pmb->u.mqe;
  10115. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  10116. mbox = phba->mbox;
  10117. vport = pmb->vport;
  10118. /* Reset heartbeat timer */
  10119. phba->last_completion_time = jiffies;
  10120. del_timer(&phba->sli.mbox_tmo);
  10121. /* Move mbox data to caller's mailbox region, do endian swapping */
  10122. if (pmb->mbox_cmpl && mbox)
  10123. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  10124. /*
  10125. * For mcqe errors, conditionally move a modified error code to
  10126. * the mbox so that the error will not be missed.
  10127. */
  10128. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  10129. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  10130. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  10131. bf_set(lpfc_mqe_status, mqe,
  10132. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  10133. }
  10134. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10135. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10136. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  10137. "MBOX dflt rpi: status:x%x rpi:x%x",
  10138. mcqe_status,
  10139. pmbox->un.varWords[0], 0);
  10140. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  10141. mp = (struct lpfc_dmabuf *)(pmb->context1);
  10142. ndlp = (struct lpfc_nodelist *)pmb->context2;
  10143. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  10144. * RID of the PPI using the same mbox buffer.
  10145. */
  10146. lpfc_unreg_login(phba, vport->vpi,
  10147. pmbox->un.varWords[0], pmb);
  10148. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  10149. pmb->context1 = mp;
  10150. pmb->context2 = ndlp;
  10151. pmb->vport = vport;
  10152. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  10153. if (rc != MBX_BUSY)
  10154. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10155. LOG_SLI, "0385 rc should "
  10156. "have been MBX_BUSY\n");
  10157. if (rc != MBX_NOT_FINISHED)
  10158. goto send_current_mbox;
  10159. }
  10160. }
  10161. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  10162. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  10163. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  10164. /* There is mailbox completion work to do */
  10165. spin_lock_irqsave(&phba->hbalock, iflags);
  10166. __lpfc_mbox_cmpl_put(phba, pmb);
  10167. phba->work_ha |= HA_MBATT;
  10168. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10169. workposted = true;
  10170. send_current_mbox:
  10171. spin_lock_irqsave(&phba->hbalock, iflags);
  10172. /* Release the mailbox command posting token */
  10173. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10174. /* Setting active mailbox pointer need to be in sync to flag clear */
  10175. phba->sli.mbox_active = NULL;
  10176. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10177. /* Wake up worker thread to post the next pending mailbox command */
  10178. lpfc_worker_wake_up(phba);
  10179. out_no_mqe_complete:
  10180. if (bf_get(lpfc_trailer_consumed, mcqe))
  10181. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  10182. return workposted;
  10183. }
  10184. /**
  10185. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  10186. * @phba: Pointer to HBA context object.
  10187. * @cqe: Pointer to mailbox completion queue entry.
  10188. *
  10189. * This routine process a mailbox completion queue entry, it invokes the
  10190. * proper mailbox complete handling or asynchrous event handling routine
  10191. * according to the MCQE's async bit.
  10192. *
  10193. * Return: true if work posted to worker thread, otherwise false.
  10194. **/
  10195. static bool
  10196. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  10197. {
  10198. struct lpfc_mcqe mcqe;
  10199. bool workposted;
  10200. /* Copy the mailbox MCQE and convert endian order as needed */
  10201. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  10202. /* Invoke the proper event handling routine */
  10203. if (!bf_get(lpfc_trailer_async, &mcqe))
  10204. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  10205. else
  10206. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  10207. return workposted;
  10208. }
  10209. /**
  10210. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  10211. * @phba: Pointer to HBA context object.
  10212. * @cq: Pointer to associated CQ
  10213. * @wcqe: Pointer to work-queue completion queue entry.
  10214. *
  10215. * This routine handles an ELS work-queue completion event.
  10216. *
  10217. * Return: true if work posted to worker thread, otherwise false.
  10218. **/
  10219. static bool
  10220. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10221. struct lpfc_wcqe_complete *wcqe)
  10222. {
  10223. struct lpfc_iocbq *irspiocbq;
  10224. unsigned long iflags;
  10225. struct lpfc_sli_ring *pring = cq->pring;
  10226. /* Get an irspiocbq for later ELS response processing use */
  10227. irspiocbq = lpfc_sli_get_iocbq(phba);
  10228. if (!irspiocbq) {
  10229. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10230. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10231. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10232. pring->txq_cnt, phba->iocb_cnt,
  10233. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  10234. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  10235. return false;
  10236. }
  10237. /* Save off the slow-path queue event for work thread to process */
  10238. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10239. spin_lock_irqsave(&phba->hbalock, iflags);
  10240. list_add_tail(&irspiocbq->cq_event.list,
  10241. &phba->sli4_hba.sp_queue_event);
  10242. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10243. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10244. return true;
  10245. }
  10246. /**
  10247. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10248. * @phba: Pointer to HBA context object.
  10249. * @wcqe: Pointer to work-queue completion queue entry.
  10250. *
  10251. * This routine handles slow-path WQ entry comsumed event by invoking the
  10252. * proper WQ release routine to the slow-path WQ.
  10253. **/
  10254. static void
  10255. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10256. struct lpfc_wcqe_release *wcqe)
  10257. {
  10258. /* sanity check on queue memory */
  10259. if (unlikely(!phba->sli4_hba.els_wq))
  10260. return;
  10261. /* Check for the slow-path ELS work queue */
  10262. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10263. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10264. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10265. else
  10266. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10267. "2579 Slow-path wqe consume event carries "
  10268. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10269. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10270. phba->sli4_hba.els_wq->queue_id);
  10271. }
  10272. /**
  10273. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10274. * @phba: Pointer to HBA context object.
  10275. * @cq: Pointer to a WQ completion queue.
  10276. * @wcqe: Pointer to work-queue completion queue entry.
  10277. *
  10278. * This routine handles an XRI abort event.
  10279. *
  10280. * Return: true if work posted to worker thread, otherwise false.
  10281. **/
  10282. static bool
  10283. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10284. struct lpfc_queue *cq,
  10285. struct sli4_wcqe_xri_aborted *wcqe)
  10286. {
  10287. bool workposted = false;
  10288. struct lpfc_cq_event *cq_event;
  10289. unsigned long iflags;
  10290. /* Allocate a new internal CQ_EVENT entry */
  10291. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10292. if (!cq_event) {
  10293. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10294. "0602 Failed to allocate CQ_EVENT entry\n");
  10295. return false;
  10296. }
  10297. /* Move the CQE into the proper xri abort event list */
  10298. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10299. switch (cq->subtype) {
  10300. case LPFC_FCP:
  10301. spin_lock_irqsave(&phba->hbalock, iflags);
  10302. list_add_tail(&cq_event->list,
  10303. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10304. /* Set the fcp xri abort event flag */
  10305. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10306. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10307. workposted = true;
  10308. break;
  10309. case LPFC_ELS:
  10310. spin_lock_irqsave(&phba->hbalock, iflags);
  10311. list_add_tail(&cq_event->list,
  10312. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10313. /* Set the els xri abort event flag */
  10314. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10315. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10316. workposted = true;
  10317. break;
  10318. default:
  10319. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10320. "0603 Invalid work queue CQE subtype (x%x)\n",
  10321. cq->subtype);
  10322. workposted = false;
  10323. break;
  10324. }
  10325. return workposted;
  10326. }
  10327. /**
  10328. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10329. * @phba: Pointer to HBA context object.
  10330. * @rcqe: Pointer to receive-queue completion queue entry.
  10331. *
  10332. * This routine process a receive-queue completion queue entry.
  10333. *
  10334. * Return: true if work posted to worker thread, otherwise false.
  10335. **/
  10336. static bool
  10337. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10338. {
  10339. bool workposted = false;
  10340. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10341. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10342. struct hbq_dmabuf *dma_buf;
  10343. uint32_t status, rq_id;
  10344. unsigned long iflags;
  10345. /* sanity check on queue memory */
  10346. if (unlikely(!hrq) || unlikely(!drq))
  10347. return workposted;
  10348. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10349. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10350. else
  10351. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10352. if (rq_id != hrq->queue_id)
  10353. goto out;
  10354. status = bf_get(lpfc_rcqe_status, rcqe);
  10355. switch (status) {
  10356. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10357. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10358. "2537 Receive Frame Truncated!!\n");
  10359. hrq->RQ_buf_trunc++;
  10360. case FC_STATUS_RQ_SUCCESS:
  10361. lpfc_sli4_rq_release(hrq, drq);
  10362. spin_lock_irqsave(&phba->hbalock, iflags);
  10363. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10364. if (!dma_buf) {
  10365. hrq->RQ_no_buf_found++;
  10366. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10367. goto out;
  10368. }
  10369. hrq->RQ_rcv_buf++;
  10370. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10371. /* save off the frame for the word thread to process */
  10372. list_add_tail(&dma_buf->cq_event.list,
  10373. &phba->sli4_hba.sp_queue_event);
  10374. /* Frame received */
  10375. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10376. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10377. workposted = true;
  10378. break;
  10379. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10380. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10381. hrq->RQ_no_posted_buf++;
  10382. /* Post more buffers if possible */
  10383. spin_lock_irqsave(&phba->hbalock, iflags);
  10384. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10385. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10386. workposted = true;
  10387. break;
  10388. }
  10389. out:
  10390. return workposted;
  10391. }
  10392. /**
  10393. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10394. * @phba: Pointer to HBA context object.
  10395. * @cq: Pointer to the completion queue.
  10396. * @wcqe: Pointer to a completion queue entry.
  10397. *
  10398. * This routine process a slow-path work-queue or receive queue completion queue
  10399. * entry.
  10400. *
  10401. * Return: true if work posted to worker thread, otherwise false.
  10402. **/
  10403. static bool
  10404. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10405. struct lpfc_cqe *cqe)
  10406. {
  10407. struct lpfc_cqe cqevt;
  10408. bool workposted = false;
  10409. /* Copy the work queue CQE and convert endian order if needed */
  10410. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10411. /* Check and process for different type of WCQE and dispatch */
  10412. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10413. case CQE_CODE_COMPL_WQE:
  10414. /* Process the WQ/RQ complete event */
  10415. phba->last_completion_time = jiffies;
  10416. workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
  10417. (struct lpfc_wcqe_complete *)&cqevt);
  10418. break;
  10419. case CQE_CODE_RELEASE_WQE:
  10420. /* Process the WQ release event */
  10421. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10422. (struct lpfc_wcqe_release *)&cqevt);
  10423. break;
  10424. case CQE_CODE_XRI_ABORTED:
  10425. /* Process the WQ XRI abort event */
  10426. phba->last_completion_time = jiffies;
  10427. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10428. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10429. break;
  10430. case CQE_CODE_RECEIVE:
  10431. case CQE_CODE_RECEIVE_V1:
  10432. /* Process the RQ event */
  10433. phba->last_completion_time = jiffies;
  10434. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10435. (struct lpfc_rcqe *)&cqevt);
  10436. break;
  10437. default:
  10438. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10439. "0388 Not a valid WCQE code: x%x\n",
  10440. bf_get(lpfc_cqe_code, &cqevt));
  10441. break;
  10442. }
  10443. return workposted;
  10444. }
  10445. /**
  10446. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10447. * @phba: Pointer to HBA context object.
  10448. * @eqe: Pointer to fast-path event queue entry.
  10449. *
  10450. * This routine process a event queue entry from the slow-path event queue.
  10451. * It will check the MajorCode and MinorCode to determine this is for a
  10452. * completion event on a completion queue, if not, an error shall be logged
  10453. * and just return. Otherwise, it will get to the corresponding completion
  10454. * queue and process all the entries on that completion queue, rearm the
  10455. * completion queue, and then return.
  10456. *
  10457. **/
  10458. static void
  10459. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10460. struct lpfc_queue *speq)
  10461. {
  10462. struct lpfc_queue *cq = NULL, *childq;
  10463. struct lpfc_cqe *cqe;
  10464. bool workposted = false;
  10465. int ecount = 0;
  10466. uint16_t cqid;
  10467. /* Get the reference to the corresponding CQ */
  10468. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10469. list_for_each_entry(childq, &speq->child_list, list) {
  10470. if (childq->queue_id == cqid) {
  10471. cq = childq;
  10472. break;
  10473. }
  10474. }
  10475. if (unlikely(!cq)) {
  10476. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10477. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10478. "0365 Slow-path CQ identifier "
  10479. "(%d) does not exist\n", cqid);
  10480. return;
  10481. }
  10482. /* Process all the entries to the CQ */
  10483. switch (cq->type) {
  10484. case LPFC_MCQ:
  10485. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10486. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10487. if (!(++ecount % cq->entry_repost))
  10488. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10489. cq->CQ_mbox++;
  10490. }
  10491. break;
  10492. case LPFC_WCQ:
  10493. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10494. if (cq->subtype == LPFC_FCP)
  10495. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10496. cqe);
  10497. else
  10498. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10499. cqe);
  10500. if (!(++ecount % cq->entry_repost))
  10501. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10502. }
  10503. /* Track the max number of CQEs processed in 1 EQ */
  10504. if (ecount > cq->CQ_max_cqe)
  10505. cq->CQ_max_cqe = ecount;
  10506. break;
  10507. default:
  10508. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10509. "0370 Invalid completion queue type (%d)\n",
  10510. cq->type);
  10511. return;
  10512. }
  10513. /* Catch the no cq entry condition, log an error */
  10514. if (unlikely(ecount == 0))
  10515. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10516. "0371 No entry from the CQ: identifier "
  10517. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10518. /* In any case, flash and re-arm the RCQ */
  10519. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10520. /* wake up worker thread if there are works to be done */
  10521. if (workposted)
  10522. lpfc_worker_wake_up(phba);
  10523. }
  10524. /**
  10525. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10526. * @phba: Pointer to HBA context object.
  10527. * @cq: Pointer to associated CQ
  10528. * @wcqe: Pointer to work-queue completion queue entry.
  10529. *
  10530. * This routine process a fast-path work queue completion entry from fast-path
  10531. * event queue for FCP command response completion.
  10532. **/
  10533. static void
  10534. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10535. struct lpfc_wcqe_complete *wcqe)
  10536. {
  10537. struct lpfc_sli_ring *pring = cq->pring;
  10538. struct lpfc_iocbq *cmdiocbq;
  10539. struct lpfc_iocbq irspiocbq;
  10540. unsigned long iflags;
  10541. /* Check for response status */
  10542. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10543. /* If resource errors reported from HBA, reduce queue
  10544. * depth of the SCSI device.
  10545. */
  10546. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10547. IOSTAT_LOCAL_REJECT) &&
  10548. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  10549. phba->lpfc_rampdown_queue_depth(phba);
  10550. }
  10551. /* Log the error status */
  10552. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10553. "0373 FCP complete error: status=x%x, "
  10554. "hw_status=x%x, total_data_specified=%d, "
  10555. "parameter=x%x, word3=x%x\n",
  10556. bf_get(lpfc_wcqe_c_status, wcqe),
  10557. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10558. wcqe->total_data_placed, wcqe->parameter,
  10559. wcqe->word3);
  10560. }
  10561. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10562. spin_lock_irqsave(&pring->ring_lock, iflags);
  10563. pring->stats.iocb_event++;
  10564. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10565. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10566. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10567. if (unlikely(!cmdiocbq)) {
  10568. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10569. "0374 FCP complete with no corresponding "
  10570. "cmdiocb: iotag (%d)\n",
  10571. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10572. return;
  10573. }
  10574. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10575. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10576. "0375 FCP cmdiocb not callback function "
  10577. "iotag: (%d)\n",
  10578. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10579. return;
  10580. }
  10581. /* Fake the irspiocb and copy necessary response information */
  10582. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10583. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10584. spin_lock_irqsave(&phba->hbalock, iflags);
  10585. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10586. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10587. }
  10588. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10589. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10590. }
  10591. /**
  10592. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10593. * @phba: Pointer to HBA context object.
  10594. * @cq: Pointer to completion queue.
  10595. * @wcqe: Pointer to work-queue completion queue entry.
  10596. *
  10597. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10598. * proper WQ release routine to the slow-path WQ.
  10599. **/
  10600. static void
  10601. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10602. struct lpfc_wcqe_release *wcqe)
  10603. {
  10604. struct lpfc_queue *childwq;
  10605. bool wqid_matched = false;
  10606. uint16_t fcp_wqid;
  10607. /* Check for fast-path FCP work queue release */
  10608. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10609. list_for_each_entry(childwq, &cq->child_list, list) {
  10610. if (childwq->queue_id == fcp_wqid) {
  10611. lpfc_sli4_wq_release(childwq,
  10612. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10613. wqid_matched = true;
  10614. break;
  10615. }
  10616. }
  10617. /* Report warning log message if no match found */
  10618. if (wqid_matched != true)
  10619. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10620. "2580 Fast-path wqe consume event carries "
  10621. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10622. }
  10623. /**
  10624. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10625. * @cq: Pointer to the completion queue.
  10626. * @eqe: Pointer to fast-path completion queue entry.
  10627. *
  10628. * This routine process a fast-path work queue completion entry from fast-path
  10629. * event queue for FCP command response completion.
  10630. **/
  10631. static int
  10632. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10633. struct lpfc_cqe *cqe)
  10634. {
  10635. struct lpfc_wcqe_release wcqe;
  10636. bool workposted = false;
  10637. /* Copy the work queue CQE and convert endian order if needed */
  10638. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10639. /* Check and process for different type of WCQE and dispatch */
  10640. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10641. case CQE_CODE_COMPL_WQE:
  10642. cq->CQ_wq++;
  10643. /* Process the WQ complete event */
  10644. phba->last_completion_time = jiffies;
  10645. lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
  10646. (struct lpfc_wcqe_complete *)&wcqe);
  10647. break;
  10648. case CQE_CODE_RELEASE_WQE:
  10649. cq->CQ_release_wqe++;
  10650. /* Process the WQ release event */
  10651. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10652. (struct lpfc_wcqe_release *)&wcqe);
  10653. break;
  10654. case CQE_CODE_XRI_ABORTED:
  10655. cq->CQ_xri_aborted++;
  10656. /* Process the WQ XRI abort event */
  10657. phba->last_completion_time = jiffies;
  10658. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10659. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10660. break;
  10661. default:
  10662. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10663. "0144 Not a valid WCQE code: x%x\n",
  10664. bf_get(lpfc_wcqe_c_code, &wcqe));
  10665. break;
  10666. }
  10667. return workposted;
  10668. }
  10669. /**
  10670. * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
  10671. * @phba: Pointer to HBA context object.
  10672. * @eqe: Pointer to fast-path event queue entry.
  10673. *
  10674. * This routine process a event queue entry from the fast-path event queue.
  10675. * It will check the MajorCode and MinorCode to determine this is for a
  10676. * completion event on a completion queue, if not, an error shall be logged
  10677. * and just return. Otherwise, it will get to the corresponding completion
  10678. * queue and process all the entries on the completion queue, rearm the
  10679. * completion queue, and then return.
  10680. **/
  10681. static void
  10682. lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10683. uint32_t qidx)
  10684. {
  10685. struct lpfc_queue *cq;
  10686. struct lpfc_cqe *cqe;
  10687. bool workposted = false;
  10688. uint16_t cqid;
  10689. int ecount = 0;
  10690. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10691. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10692. "0366 Not a valid completion "
  10693. "event: majorcode=x%x, minorcode=x%x\n",
  10694. bf_get_le32(lpfc_eqe_major_code, eqe),
  10695. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10696. return;
  10697. }
  10698. /* Get the reference to the corresponding CQ */
  10699. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10700. /* Check if this is a Slow path event */
  10701. if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
  10702. lpfc_sli4_sp_handle_eqe(phba, eqe,
  10703. phba->sli4_hba.hba_eq[qidx]);
  10704. return;
  10705. }
  10706. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10707. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10708. "3146 Fast-path completion queues "
  10709. "does not exist\n");
  10710. return;
  10711. }
  10712. cq = phba->sli4_hba.fcp_cq[qidx];
  10713. if (unlikely(!cq)) {
  10714. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10715. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10716. "0367 Fast-path completion queue "
  10717. "(%d) does not exist\n", qidx);
  10718. return;
  10719. }
  10720. if (unlikely(cqid != cq->queue_id)) {
  10721. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10722. "0368 Miss-matched fast-path completion "
  10723. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10724. cqid, cq->queue_id);
  10725. return;
  10726. }
  10727. /* Process all the entries to the CQ */
  10728. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10729. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10730. if (!(++ecount % cq->entry_repost))
  10731. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10732. }
  10733. /* Track the max number of CQEs processed in 1 EQ */
  10734. if (ecount > cq->CQ_max_cqe)
  10735. cq->CQ_max_cqe = ecount;
  10736. /* Catch the no cq entry condition */
  10737. if (unlikely(ecount == 0))
  10738. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10739. "0369 No entry from fast-path completion "
  10740. "queue fcpcqid=%d\n", cq->queue_id);
  10741. /* In any case, flash and re-arm the CQ */
  10742. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10743. /* wake up worker thread if there are works to be done */
  10744. if (workposted)
  10745. lpfc_worker_wake_up(phba);
  10746. }
  10747. static void
  10748. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10749. {
  10750. struct lpfc_eqe *eqe;
  10751. /* walk all the EQ entries and drop on the floor */
  10752. while ((eqe = lpfc_sli4_eq_get(eq)))
  10753. ;
  10754. /* Clear and re-arm the EQ */
  10755. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10756. }
  10757. /**
  10758. * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
  10759. * @irq: Interrupt number.
  10760. * @dev_id: The device context pointer.
  10761. *
  10762. * This function is directly called from the PCI layer as an interrupt
  10763. * service routine when device with SLI-4 interface spec is enabled with
  10764. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10765. * ring event in the HBA. However, when the device is enabled with either
  10766. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10767. * device-level interrupt handler. When the PCI slot is in error recovery
  10768. * or the HBA is undergoing initialization, the interrupt handler will not
  10769. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10770. * the intrrupt context. This function is called without any lock held.
  10771. * It gets the hbalock to access and update SLI data structures. Note that,
  10772. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10773. * equal to that of FCP CQ index.
  10774. *
  10775. * The link attention and ELS ring attention events are handled
  10776. * by the worker thread. The interrupt handler signals the worker thread
  10777. * and returns for these events. This function is called without any lock
  10778. * held. It gets the hbalock to access and update SLI data structures.
  10779. *
  10780. * This function returns IRQ_HANDLED when interrupt is handled else it
  10781. * returns IRQ_NONE.
  10782. **/
  10783. irqreturn_t
  10784. lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
  10785. {
  10786. struct lpfc_hba *phba;
  10787. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10788. struct lpfc_queue *fpeq;
  10789. struct lpfc_eqe *eqe;
  10790. unsigned long iflag;
  10791. int ecount = 0;
  10792. uint32_t fcp_eqidx;
  10793. /* Get the driver's phba structure from the dev_id */
  10794. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10795. phba = fcp_eq_hdl->phba;
  10796. fcp_eqidx = fcp_eq_hdl->idx;
  10797. if (unlikely(!phba))
  10798. return IRQ_NONE;
  10799. if (unlikely(!phba->sli4_hba.hba_eq))
  10800. return IRQ_NONE;
  10801. /* Get to the EQ struct associated with this vector */
  10802. fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
  10803. if (unlikely(!fpeq))
  10804. return IRQ_NONE;
  10805. /* Check device state for handling interrupt */
  10806. if (unlikely(lpfc_intr_state_check(phba))) {
  10807. fpeq->EQ_badstate++;
  10808. /* Check again for link_state with lock held */
  10809. spin_lock_irqsave(&phba->hbalock, iflag);
  10810. if (phba->link_state < LPFC_LINK_DOWN)
  10811. /* Flush, clear interrupt, and rearm the EQ */
  10812. lpfc_sli4_eq_flush(phba, fpeq);
  10813. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10814. return IRQ_NONE;
  10815. }
  10816. /*
  10817. * Process all the event on FCP fast-path EQ
  10818. */
  10819. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10820. lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
  10821. if (!(++ecount % fpeq->entry_repost))
  10822. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10823. fpeq->EQ_processed++;
  10824. }
  10825. /* Track the max number of EQEs processed in 1 intr */
  10826. if (ecount > fpeq->EQ_max_eqe)
  10827. fpeq->EQ_max_eqe = ecount;
  10828. /* Always clear and re-arm the fast-path EQ */
  10829. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10830. if (unlikely(ecount == 0)) {
  10831. fpeq->EQ_no_entry++;
  10832. if (phba->intr_type == MSIX)
  10833. /* MSI-X treated interrupt served as no EQ share INT */
  10834. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10835. "0358 MSI-X interrupt with no EQE\n");
  10836. else
  10837. /* Non MSI-X treated on interrupt as EQ share INT */
  10838. return IRQ_NONE;
  10839. }
  10840. return IRQ_HANDLED;
  10841. } /* lpfc_sli4_fp_intr_handler */
  10842. /**
  10843. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10844. * @irq: Interrupt number.
  10845. * @dev_id: The device context pointer.
  10846. *
  10847. * This function is the device-level interrupt handler to device with SLI-4
  10848. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10849. * interrupt mode is enabled and there is an event in the HBA which requires
  10850. * driver attention. This function invokes the slow-path interrupt attention
  10851. * handling function and fast-path interrupt attention handling function in
  10852. * turn to process the relevant HBA attention events. This function is called
  10853. * without any lock held. It gets the hbalock to access and update SLI data
  10854. * structures.
  10855. *
  10856. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10857. * returns IRQ_NONE.
  10858. **/
  10859. irqreturn_t
  10860. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10861. {
  10862. struct lpfc_hba *phba;
  10863. irqreturn_t hba_irq_rc;
  10864. bool hba_handled = false;
  10865. uint32_t fcp_eqidx;
  10866. /* Get the driver's phba structure from the dev_id */
  10867. phba = (struct lpfc_hba *)dev_id;
  10868. if (unlikely(!phba))
  10869. return IRQ_NONE;
  10870. /*
  10871. * Invoke fast-path host attention interrupt handling as appropriate.
  10872. */
  10873. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
  10874. hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
  10875. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10876. if (hba_irq_rc == IRQ_HANDLED)
  10877. hba_handled |= true;
  10878. }
  10879. return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
  10880. } /* lpfc_sli4_intr_handler */
  10881. /**
  10882. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10883. * @queue: The queue structure to free.
  10884. *
  10885. * This function frees a queue structure and the DMAable memory used for
  10886. * the host resident queue. This function must be called after destroying the
  10887. * queue on the HBA.
  10888. **/
  10889. void
  10890. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10891. {
  10892. struct lpfc_dmabuf *dmabuf;
  10893. if (!queue)
  10894. return;
  10895. while (!list_empty(&queue->page_list)) {
  10896. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10897. list);
  10898. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10899. dmabuf->virt, dmabuf->phys);
  10900. kfree(dmabuf);
  10901. }
  10902. kfree(queue);
  10903. return;
  10904. }
  10905. /**
  10906. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10907. * @phba: The HBA that this queue is being created on.
  10908. * @entry_size: The size of each queue entry for this queue.
  10909. * @entry count: The number of entries that this queue will handle.
  10910. *
  10911. * This function allocates a queue structure and the DMAable memory used for
  10912. * the host resident queue. This function must be called before creating the
  10913. * queue on the HBA.
  10914. **/
  10915. struct lpfc_queue *
  10916. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10917. uint32_t entry_count)
  10918. {
  10919. struct lpfc_queue *queue;
  10920. struct lpfc_dmabuf *dmabuf;
  10921. int x, total_qe_count;
  10922. void *dma_pointer;
  10923. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10924. if (!phba->sli4_hba.pc_sli4_params.supported)
  10925. hw_page_size = SLI4_PAGE_SIZE;
  10926. queue = kzalloc(sizeof(struct lpfc_queue) +
  10927. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  10928. if (!queue)
  10929. return NULL;
  10930. queue->page_count = (ALIGN(entry_size * entry_count,
  10931. hw_page_size))/hw_page_size;
  10932. INIT_LIST_HEAD(&queue->list);
  10933. INIT_LIST_HEAD(&queue->page_list);
  10934. INIT_LIST_HEAD(&queue->child_list);
  10935. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  10936. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  10937. if (!dmabuf)
  10938. goto out_fail;
  10939. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  10940. hw_page_size, &dmabuf->phys,
  10941. GFP_KERNEL);
  10942. if (!dmabuf->virt) {
  10943. kfree(dmabuf);
  10944. goto out_fail;
  10945. }
  10946. memset(dmabuf->virt, 0, hw_page_size);
  10947. dmabuf->buffer_tag = x;
  10948. list_add_tail(&dmabuf->list, &queue->page_list);
  10949. /* initialize queue's entry array */
  10950. dma_pointer = dmabuf->virt;
  10951. for (; total_qe_count < entry_count &&
  10952. dma_pointer < (hw_page_size + dmabuf->virt);
  10953. total_qe_count++, dma_pointer += entry_size) {
  10954. queue->qe[total_qe_count].address = dma_pointer;
  10955. }
  10956. }
  10957. queue->entry_size = entry_size;
  10958. queue->entry_count = entry_count;
  10959. /*
  10960. * entry_repost is calculated based on the number of entries in the
  10961. * queue. This works out except for RQs. If buffers are NOT initially
  10962. * posted for every RQE, entry_repost should be adjusted accordingly.
  10963. */
  10964. queue->entry_repost = (entry_count >> 3);
  10965. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  10966. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  10967. queue->phba = phba;
  10968. return queue;
  10969. out_fail:
  10970. lpfc_sli4_queue_free(queue);
  10971. return NULL;
  10972. }
  10973. /**
  10974. * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
  10975. * @phba: HBA structure that indicates port to create a queue on.
  10976. * @startq: The starting FCP EQ to modify
  10977. *
  10978. * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
  10979. *
  10980. * The @phba struct is used to send mailbox command to HBA. The @startq
  10981. * is used to get the starting FCP EQ to change.
  10982. * This function is asynchronous and will wait for the mailbox
  10983. * command to finish before continuing.
  10984. *
  10985. * On success this function will return a zero. If unable to allocate enough
  10986. * memory this function will return -ENOMEM. If the queue create mailbox command
  10987. * fails this function will return -ENXIO.
  10988. **/
  10989. uint32_t
  10990. lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
  10991. {
  10992. struct lpfc_mbx_modify_eq_delay *eq_delay;
  10993. LPFC_MBOXQ_t *mbox;
  10994. struct lpfc_queue *eq;
  10995. int cnt, rc, length, status = 0;
  10996. uint32_t shdr_status, shdr_add_status;
  10997. int fcp_eqidx;
  10998. union lpfc_sli4_cfg_shdr *shdr;
  10999. uint16_t dmult;
  11000. if (startq >= phba->cfg_fcp_io_channel)
  11001. return 0;
  11002. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11003. if (!mbox)
  11004. return -ENOMEM;
  11005. length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
  11006. sizeof(struct lpfc_sli4_cfg_mhdr));
  11007. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11008. LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
  11009. length, LPFC_SLI4_MBX_EMBED);
  11010. eq_delay = &mbox->u.mqe.un.eq_delay;
  11011. /* Calculate delay multiper from maximum interrupt per second */
  11012. dmult = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
  11013. dmult = LPFC_DMULT_CONST/dmult - 1;
  11014. cnt = 0;
  11015. for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
  11016. fcp_eqidx++) {
  11017. eq = phba->sli4_hba.hba_eq[fcp_eqidx];
  11018. if (!eq)
  11019. continue;
  11020. eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
  11021. eq_delay->u.request.eq[cnt].phase = 0;
  11022. eq_delay->u.request.eq[cnt].delay_multi = dmult;
  11023. cnt++;
  11024. if (cnt >= LPFC_MAX_EQ_DELAY)
  11025. break;
  11026. }
  11027. eq_delay->u.request.num_eq = cnt;
  11028. mbox->vport = phba->pport;
  11029. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11030. mbox->context1 = NULL;
  11031. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11032. shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
  11033. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11034. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11035. if (shdr_status || shdr_add_status || rc) {
  11036. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11037. "2512 MODIFY_EQ_DELAY mailbox failed with "
  11038. "status x%x add_status x%x, mbx status x%x\n",
  11039. shdr_status, shdr_add_status, rc);
  11040. status = -ENXIO;
  11041. }
  11042. mempool_free(mbox, phba->mbox_mem_pool);
  11043. return status;
  11044. }
  11045. /**
  11046. * lpfc_eq_create - Create an Event Queue on the HBA
  11047. * @phba: HBA structure that indicates port to create a queue on.
  11048. * @eq: The queue structure to use to create the event queue.
  11049. * @imax: The maximum interrupt per second limit.
  11050. *
  11051. * This function creates an event queue, as detailed in @eq, on a port,
  11052. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  11053. *
  11054. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  11055. * is used to get the entry count and entry size that are necessary to
  11056. * determine the number of pages to allocate and use for this queue. This
  11057. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  11058. * event queue. This function is asynchronous and will wait for the mailbox
  11059. * command to finish before continuing.
  11060. *
  11061. * On success this function will return a zero. If unable to allocate enough
  11062. * memory this function will return -ENOMEM. If the queue create mailbox command
  11063. * fails this function will return -ENXIO.
  11064. **/
  11065. uint32_t
  11066. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  11067. {
  11068. struct lpfc_mbx_eq_create *eq_create;
  11069. LPFC_MBOXQ_t *mbox;
  11070. int rc, length, status = 0;
  11071. struct lpfc_dmabuf *dmabuf;
  11072. uint32_t shdr_status, shdr_add_status;
  11073. union lpfc_sli4_cfg_shdr *shdr;
  11074. uint16_t dmult;
  11075. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11076. /* sanity check on queue memory */
  11077. if (!eq)
  11078. return -ENODEV;
  11079. if (!phba->sli4_hba.pc_sli4_params.supported)
  11080. hw_page_size = SLI4_PAGE_SIZE;
  11081. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11082. if (!mbox)
  11083. return -ENOMEM;
  11084. length = (sizeof(struct lpfc_mbx_eq_create) -
  11085. sizeof(struct lpfc_sli4_cfg_mhdr));
  11086. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11087. LPFC_MBOX_OPCODE_EQ_CREATE,
  11088. length, LPFC_SLI4_MBX_EMBED);
  11089. eq_create = &mbox->u.mqe.un.eq_create;
  11090. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  11091. eq->page_count);
  11092. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  11093. LPFC_EQE_SIZE);
  11094. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  11095. /* Calculate delay multiper from maximum interrupt per second */
  11096. dmult = LPFC_DMULT_CONST/imax - 1;
  11097. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  11098. dmult);
  11099. switch (eq->entry_count) {
  11100. default:
  11101. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11102. "0360 Unsupported EQ count. (%d)\n",
  11103. eq->entry_count);
  11104. if (eq->entry_count < 256)
  11105. return -EINVAL;
  11106. /* otherwise default to smallest count (drop through) */
  11107. case 256:
  11108. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11109. LPFC_EQ_CNT_256);
  11110. break;
  11111. case 512:
  11112. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11113. LPFC_EQ_CNT_512);
  11114. break;
  11115. case 1024:
  11116. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11117. LPFC_EQ_CNT_1024);
  11118. break;
  11119. case 2048:
  11120. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11121. LPFC_EQ_CNT_2048);
  11122. break;
  11123. case 4096:
  11124. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11125. LPFC_EQ_CNT_4096);
  11126. break;
  11127. }
  11128. list_for_each_entry(dmabuf, &eq->page_list, list) {
  11129. memset(dmabuf->virt, 0, hw_page_size);
  11130. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11131. putPaddrLow(dmabuf->phys);
  11132. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11133. putPaddrHigh(dmabuf->phys);
  11134. }
  11135. mbox->vport = phba->pport;
  11136. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11137. mbox->context1 = NULL;
  11138. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11139. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  11140. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11141. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11142. if (shdr_status || shdr_add_status || rc) {
  11143. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11144. "2500 EQ_CREATE mailbox failed with "
  11145. "status x%x add_status x%x, mbx status x%x\n",
  11146. shdr_status, shdr_add_status, rc);
  11147. status = -ENXIO;
  11148. }
  11149. eq->type = LPFC_EQ;
  11150. eq->subtype = LPFC_NONE;
  11151. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  11152. if (eq->queue_id == 0xFFFF)
  11153. status = -ENXIO;
  11154. eq->host_index = 0;
  11155. eq->hba_index = 0;
  11156. mempool_free(mbox, phba->mbox_mem_pool);
  11157. return status;
  11158. }
  11159. /**
  11160. * lpfc_cq_create - Create a Completion Queue on the HBA
  11161. * @phba: HBA structure that indicates port to create a queue on.
  11162. * @cq: The queue structure to use to create the completion queue.
  11163. * @eq: The event queue to bind this completion queue to.
  11164. *
  11165. * This function creates a completion queue, as detailed in @wq, on a port,
  11166. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  11167. *
  11168. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11169. * is used to get the entry count and entry size that are necessary to
  11170. * determine the number of pages to allocate and use for this queue. The @eq
  11171. * is used to indicate which event queue to bind this completion queue to. This
  11172. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  11173. * completion queue. This function is asynchronous and will wait for the mailbox
  11174. * command to finish before continuing.
  11175. *
  11176. * On success this function will return a zero. If unable to allocate enough
  11177. * memory this function will return -ENOMEM. If the queue create mailbox command
  11178. * fails this function will return -ENXIO.
  11179. **/
  11180. uint32_t
  11181. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11182. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  11183. {
  11184. struct lpfc_mbx_cq_create *cq_create;
  11185. struct lpfc_dmabuf *dmabuf;
  11186. LPFC_MBOXQ_t *mbox;
  11187. int rc, length, status = 0;
  11188. uint32_t shdr_status, shdr_add_status;
  11189. union lpfc_sli4_cfg_shdr *shdr;
  11190. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11191. /* sanity check on queue memory */
  11192. if (!cq || !eq)
  11193. return -ENODEV;
  11194. if (!phba->sli4_hba.pc_sli4_params.supported)
  11195. hw_page_size = SLI4_PAGE_SIZE;
  11196. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11197. if (!mbox)
  11198. return -ENOMEM;
  11199. length = (sizeof(struct lpfc_mbx_cq_create) -
  11200. sizeof(struct lpfc_sli4_cfg_mhdr));
  11201. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11202. LPFC_MBOX_OPCODE_CQ_CREATE,
  11203. length, LPFC_SLI4_MBX_EMBED);
  11204. cq_create = &mbox->u.mqe.un.cq_create;
  11205. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  11206. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  11207. cq->page_count);
  11208. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  11209. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  11210. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11211. phba->sli4_hba.pc_sli4_params.cqv);
  11212. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  11213. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  11214. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  11215. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  11216. eq->queue_id);
  11217. } else {
  11218. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  11219. eq->queue_id);
  11220. }
  11221. switch (cq->entry_count) {
  11222. default:
  11223. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11224. "0361 Unsupported CQ count. (%d)\n",
  11225. cq->entry_count);
  11226. if (cq->entry_count < 256) {
  11227. status = -EINVAL;
  11228. goto out;
  11229. }
  11230. /* otherwise default to smallest count (drop through) */
  11231. case 256:
  11232. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11233. LPFC_CQ_CNT_256);
  11234. break;
  11235. case 512:
  11236. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11237. LPFC_CQ_CNT_512);
  11238. break;
  11239. case 1024:
  11240. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11241. LPFC_CQ_CNT_1024);
  11242. break;
  11243. }
  11244. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11245. memset(dmabuf->virt, 0, hw_page_size);
  11246. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11247. putPaddrLow(dmabuf->phys);
  11248. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11249. putPaddrHigh(dmabuf->phys);
  11250. }
  11251. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11252. /* The IOCTL status is embedded in the mailbox subheader. */
  11253. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11254. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11255. if (shdr_status || shdr_add_status || rc) {
  11256. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11257. "2501 CQ_CREATE mailbox failed with "
  11258. "status x%x add_status x%x, mbx status x%x\n",
  11259. shdr_status, shdr_add_status, rc);
  11260. status = -ENXIO;
  11261. goto out;
  11262. }
  11263. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11264. if (cq->queue_id == 0xFFFF) {
  11265. status = -ENXIO;
  11266. goto out;
  11267. }
  11268. /* link the cq onto the parent eq child list */
  11269. list_add_tail(&cq->list, &eq->child_list);
  11270. /* Set up completion queue's type and subtype */
  11271. cq->type = type;
  11272. cq->subtype = subtype;
  11273. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11274. cq->assoc_qid = eq->queue_id;
  11275. cq->host_index = 0;
  11276. cq->hba_index = 0;
  11277. out:
  11278. mempool_free(mbox, phba->mbox_mem_pool);
  11279. return status;
  11280. }
  11281. /**
  11282. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11283. * @phba: HBA structure that indicates port to create a queue on.
  11284. * @mq: The queue structure to use to create the mailbox queue.
  11285. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11286. * @cq: The completion queue to associate with this cq.
  11287. *
  11288. * This function provides failback (fb) functionality when the
  11289. * mq_create_ext fails on older FW generations. It's purpose is identical
  11290. * to mq_create_ext otherwise.
  11291. *
  11292. * This routine cannot fail as all attributes were previously accessed and
  11293. * initialized in mq_create_ext.
  11294. **/
  11295. static void
  11296. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11297. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11298. {
  11299. struct lpfc_mbx_mq_create *mq_create;
  11300. struct lpfc_dmabuf *dmabuf;
  11301. int length;
  11302. length = (sizeof(struct lpfc_mbx_mq_create) -
  11303. sizeof(struct lpfc_sli4_cfg_mhdr));
  11304. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11305. LPFC_MBOX_OPCODE_MQ_CREATE,
  11306. length, LPFC_SLI4_MBX_EMBED);
  11307. mq_create = &mbox->u.mqe.un.mq_create;
  11308. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11309. mq->page_count);
  11310. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11311. cq->queue_id);
  11312. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11313. switch (mq->entry_count) {
  11314. case 16:
  11315. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11316. LPFC_MQ_RING_SIZE_16);
  11317. break;
  11318. case 32:
  11319. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11320. LPFC_MQ_RING_SIZE_32);
  11321. break;
  11322. case 64:
  11323. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11324. LPFC_MQ_RING_SIZE_64);
  11325. break;
  11326. case 128:
  11327. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11328. LPFC_MQ_RING_SIZE_128);
  11329. break;
  11330. }
  11331. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11332. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11333. putPaddrLow(dmabuf->phys);
  11334. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11335. putPaddrHigh(dmabuf->phys);
  11336. }
  11337. }
  11338. /**
  11339. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11340. * @phba: HBA structure that indicates port to create a queue on.
  11341. * @mq: The queue structure to use to create the mailbox queue.
  11342. * @cq: The completion queue to associate with this cq.
  11343. * @subtype: The queue's subtype.
  11344. *
  11345. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11346. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11347. *
  11348. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11349. * is used to get the entry count and entry size that are necessary to
  11350. * determine the number of pages to allocate and use for this queue. This
  11351. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  11352. * mailbox queue. This function is asynchronous and will wait for the mailbox
  11353. * command to finish before continuing.
  11354. *
  11355. * On success this function will return a zero. If unable to allocate enough
  11356. * memory this function will return -ENOMEM. If the queue create mailbox command
  11357. * fails this function will return -ENXIO.
  11358. **/
  11359. int32_t
  11360. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11361. struct lpfc_queue *cq, uint32_t subtype)
  11362. {
  11363. struct lpfc_mbx_mq_create *mq_create;
  11364. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11365. struct lpfc_dmabuf *dmabuf;
  11366. LPFC_MBOXQ_t *mbox;
  11367. int rc, length, status = 0;
  11368. uint32_t shdr_status, shdr_add_status;
  11369. union lpfc_sli4_cfg_shdr *shdr;
  11370. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11371. /* sanity check on queue memory */
  11372. if (!mq || !cq)
  11373. return -ENODEV;
  11374. if (!phba->sli4_hba.pc_sli4_params.supported)
  11375. hw_page_size = SLI4_PAGE_SIZE;
  11376. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11377. if (!mbox)
  11378. return -ENOMEM;
  11379. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11380. sizeof(struct lpfc_sli4_cfg_mhdr));
  11381. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11382. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11383. length, LPFC_SLI4_MBX_EMBED);
  11384. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11385. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11386. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11387. &mq_create_ext->u.request, mq->page_count);
  11388. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11389. &mq_create_ext->u.request, 1);
  11390. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11391. &mq_create_ext->u.request, 1);
  11392. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11393. &mq_create_ext->u.request, 1);
  11394. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11395. &mq_create_ext->u.request, 1);
  11396. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11397. &mq_create_ext->u.request, 1);
  11398. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11399. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11400. phba->sli4_hba.pc_sli4_params.mqv);
  11401. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11402. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11403. cq->queue_id);
  11404. else
  11405. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11406. cq->queue_id);
  11407. switch (mq->entry_count) {
  11408. default:
  11409. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11410. "0362 Unsupported MQ count. (%d)\n",
  11411. mq->entry_count);
  11412. if (mq->entry_count < 16) {
  11413. status = -EINVAL;
  11414. goto out;
  11415. }
  11416. /* otherwise default to smallest count (drop through) */
  11417. case 16:
  11418. bf_set(lpfc_mq_context_ring_size,
  11419. &mq_create_ext->u.request.context,
  11420. LPFC_MQ_RING_SIZE_16);
  11421. break;
  11422. case 32:
  11423. bf_set(lpfc_mq_context_ring_size,
  11424. &mq_create_ext->u.request.context,
  11425. LPFC_MQ_RING_SIZE_32);
  11426. break;
  11427. case 64:
  11428. bf_set(lpfc_mq_context_ring_size,
  11429. &mq_create_ext->u.request.context,
  11430. LPFC_MQ_RING_SIZE_64);
  11431. break;
  11432. case 128:
  11433. bf_set(lpfc_mq_context_ring_size,
  11434. &mq_create_ext->u.request.context,
  11435. LPFC_MQ_RING_SIZE_128);
  11436. break;
  11437. }
  11438. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11439. memset(dmabuf->virt, 0, hw_page_size);
  11440. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11441. putPaddrLow(dmabuf->phys);
  11442. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11443. putPaddrHigh(dmabuf->phys);
  11444. }
  11445. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11446. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11447. &mq_create_ext->u.response);
  11448. if (rc != MBX_SUCCESS) {
  11449. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11450. "2795 MQ_CREATE_EXT failed with "
  11451. "status x%x. Failback to MQ_CREATE.\n",
  11452. rc);
  11453. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11454. mq_create = &mbox->u.mqe.un.mq_create;
  11455. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11456. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11457. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11458. &mq_create->u.response);
  11459. }
  11460. /* The IOCTL status is embedded in the mailbox subheader. */
  11461. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11462. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11463. if (shdr_status || shdr_add_status || rc) {
  11464. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11465. "2502 MQ_CREATE mailbox failed with "
  11466. "status x%x add_status x%x, mbx status x%x\n",
  11467. shdr_status, shdr_add_status, rc);
  11468. status = -ENXIO;
  11469. goto out;
  11470. }
  11471. if (mq->queue_id == 0xFFFF) {
  11472. status = -ENXIO;
  11473. goto out;
  11474. }
  11475. mq->type = LPFC_MQ;
  11476. mq->assoc_qid = cq->queue_id;
  11477. mq->subtype = subtype;
  11478. mq->host_index = 0;
  11479. mq->hba_index = 0;
  11480. /* link the mq onto the parent cq child list */
  11481. list_add_tail(&mq->list, &cq->child_list);
  11482. out:
  11483. mempool_free(mbox, phba->mbox_mem_pool);
  11484. return status;
  11485. }
  11486. /**
  11487. * lpfc_wq_create - Create a Work Queue on the HBA
  11488. * @phba: HBA structure that indicates port to create a queue on.
  11489. * @wq: The queue structure to use to create the work queue.
  11490. * @cq: The completion queue to bind this work queue to.
  11491. * @subtype: The subtype of the work queue indicating its functionality.
  11492. *
  11493. * This function creates a work queue, as detailed in @wq, on a port, described
  11494. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11495. *
  11496. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11497. * is used to get the entry count and entry size that are necessary to
  11498. * determine the number of pages to allocate and use for this queue. The @cq
  11499. * is used to indicate which completion queue to bind this work queue to. This
  11500. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11501. * work queue. This function is asynchronous and will wait for the mailbox
  11502. * command to finish before continuing.
  11503. *
  11504. * On success this function will return a zero. If unable to allocate enough
  11505. * memory this function will return -ENOMEM. If the queue create mailbox command
  11506. * fails this function will return -ENXIO.
  11507. **/
  11508. uint32_t
  11509. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11510. struct lpfc_queue *cq, uint32_t subtype)
  11511. {
  11512. struct lpfc_mbx_wq_create *wq_create;
  11513. struct lpfc_dmabuf *dmabuf;
  11514. LPFC_MBOXQ_t *mbox;
  11515. int rc, length, status = 0;
  11516. uint32_t shdr_status, shdr_add_status;
  11517. union lpfc_sli4_cfg_shdr *shdr;
  11518. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11519. struct dma_address *page;
  11520. /* sanity check on queue memory */
  11521. if (!wq || !cq)
  11522. return -ENODEV;
  11523. if (!phba->sli4_hba.pc_sli4_params.supported)
  11524. hw_page_size = SLI4_PAGE_SIZE;
  11525. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11526. if (!mbox)
  11527. return -ENOMEM;
  11528. length = (sizeof(struct lpfc_mbx_wq_create) -
  11529. sizeof(struct lpfc_sli4_cfg_mhdr));
  11530. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11531. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11532. length, LPFC_SLI4_MBX_EMBED);
  11533. wq_create = &mbox->u.mqe.un.wq_create;
  11534. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11535. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11536. wq->page_count);
  11537. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11538. cq->queue_id);
  11539. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11540. phba->sli4_hba.pc_sli4_params.wqv);
  11541. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11542. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11543. wq->entry_count);
  11544. switch (wq->entry_size) {
  11545. default:
  11546. case 64:
  11547. bf_set(lpfc_mbx_wq_create_wqe_size,
  11548. &wq_create->u.request_1,
  11549. LPFC_WQ_WQE_SIZE_64);
  11550. break;
  11551. case 128:
  11552. bf_set(lpfc_mbx_wq_create_wqe_size,
  11553. &wq_create->u.request_1,
  11554. LPFC_WQ_WQE_SIZE_128);
  11555. break;
  11556. }
  11557. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11558. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11559. page = wq_create->u.request_1.page;
  11560. } else {
  11561. page = wq_create->u.request.page;
  11562. }
  11563. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11564. memset(dmabuf->virt, 0, hw_page_size);
  11565. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11566. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11567. }
  11568. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11569. /* The IOCTL status is embedded in the mailbox subheader. */
  11570. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11571. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11572. if (shdr_status || shdr_add_status || rc) {
  11573. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11574. "2503 WQ_CREATE mailbox failed with "
  11575. "status x%x add_status x%x, mbx status x%x\n",
  11576. shdr_status, shdr_add_status, rc);
  11577. status = -ENXIO;
  11578. goto out;
  11579. }
  11580. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11581. if (wq->queue_id == 0xFFFF) {
  11582. status = -ENXIO;
  11583. goto out;
  11584. }
  11585. wq->type = LPFC_WQ;
  11586. wq->assoc_qid = cq->queue_id;
  11587. wq->subtype = subtype;
  11588. wq->host_index = 0;
  11589. wq->hba_index = 0;
  11590. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  11591. /* link the wq onto the parent cq child list */
  11592. list_add_tail(&wq->list, &cq->child_list);
  11593. out:
  11594. mempool_free(mbox, phba->mbox_mem_pool);
  11595. return status;
  11596. }
  11597. /**
  11598. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11599. * @phba: HBA structure that indicates port to create a queue on.
  11600. * @rq: The queue structure to use for the receive queue.
  11601. * @qno: The associated HBQ number
  11602. *
  11603. *
  11604. * For SLI4 we need to adjust the RQ repost value based on
  11605. * the number of buffers that are initially posted to the RQ.
  11606. */
  11607. void
  11608. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11609. {
  11610. uint32_t cnt;
  11611. /* sanity check on queue memory */
  11612. if (!rq)
  11613. return;
  11614. cnt = lpfc_hbq_defs[qno]->entry_count;
  11615. /* Recalc repost for RQs based on buffers initially posted */
  11616. cnt = (cnt >> 3);
  11617. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11618. cnt = LPFC_QUEUE_MIN_REPOST;
  11619. rq->entry_repost = cnt;
  11620. }
  11621. /**
  11622. * lpfc_rq_create - Create a Receive Queue on the HBA
  11623. * @phba: HBA structure that indicates port to create a queue on.
  11624. * @hrq: The queue structure to use to create the header receive queue.
  11625. * @drq: The queue structure to use to create the data receive queue.
  11626. * @cq: The completion queue to bind this work queue to.
  11627. *
  11628. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11629. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11630. * to the HBA.
  11631. *
  11632. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11633. * struct is used to get the entry count that is necessary to determine the
  11634. * number of pages to use for this queue. The @cq is used to indicate which
  11635. * completion queue to bind received buffers that are posted to these queues to.
  11636. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11637. * receive queue pair. This function is asynchronous and will wait for the
  11638. * mailbox command to finish before continuing.
  11639. *
  11640. * On success this function will return a zero. If unable to allocate enough
  11641. * memory this function will return -ENOMEM. If the queue create mailbox command
  11642. * fails this function will return -ENXIO.
  11643. **/
  11644. uint32_t
  11645. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11646. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11647. {
  11648. struct lpfc_mbx_rq_create *rq_create;
  11649. struct lpfc_dmabuf *dmabuf;
  11650. LPFC_MBOXQ_t *mbox;
  11651. int rc, length, status = 0;
  11652. uint32_t shdr_status, shdr_add_status;
  11653. union lpfc_sli4_cfg_shdr *shdr;
  11654. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11655. /* sanity check on queue memory */
  11656. if (!hrq || !drq || !cq)
  11657. return -ENODEV;
  11658. if (!phba->sli4_hba.pc_sli4_params.supported)
  11659. hw_page_size = SLI4_PAGE_SIZE;
  11660. if (hrq->entry_count != drq->entry_count)
  11661. return -EINVAL;
  11662. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11663. if (!mbox)
  11664. return -ENOMEM;
  11665. length = (sizeof(struct lpfc_mbx_rq_create) -
  11666. sizeof(struct lpfc_sli4_cfg_mhdr));
  11667. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11668. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11669. length, LPFC_SLI4_MBX_EMBED);
  11670. rq_create = &mbox->u.mqe.un.rq_create;
  11671. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11672. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11673. phba->sli4_hba.pc_sli4_params.rqv);
  11674. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11675. bf_set(lpfc_rq_context_rqe_count_1,
  11676. &rq_create->u.request.context,
  11677. hrq->entry_count);
  11678. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11679. bf_set(lpfc_rq_context_rqe_size,
  11680. &rq_create->u.request.context,
  11681. LPFC_RQE_SIZE_8);
  11682. bf_set(lpfc_rq_context_page_size,
  11683. &rq_create->u.request.context,
  11684. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11685. } else {
  11686. switch (hrq->entry_count) {
  11687. default:
  11688. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11689. "2535 Unsupported RQ count. (%d)\n",
  11690. hrq->entry_count);
  11691. if (hrq->entry_count < 512) {
  11692. status = -EINVAL;
  11693. goto out;
  11694. }
  11695. /* otherwise default to smallest count (drop through) */
  11696. case 512:
  11697. bf_set(lpfc_rq_context_rqe_count,
  11698. &rq_create->u.request.context,
  11699. LPFC_RQ_RING_SIZE_512);
  11700. break;
  11701. case 1024:
  11702. bf_set(lpfc_rq_context_rqe_count,
  11703. &rq_create->u.request.context,
  11704. LPFC_RQ_RING_SIZE_1024);
  11705. break;
  11706. case 2048:
  11707. bf_set(lpfc_rq_context_rqe_count,
  11708. &rq_create->u.request.context,
  11709. LPFC_RQ_RING_SIZE_2048);
  11710. break;
  11711. case 4096:
  11712. bf_set(lpfc_rq_context_rqe_count,
  11713. &rq_create->u.request.context,
  11714. LPFC_RQ_RING_SIZE_4096);
  11715. break;
  11716. }
  11717. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11718. LPFC_HDR_BUF_SIZE);
  11719. }
  11720. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11721. cq->queue_id);
  11722. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11723. hrq->page_count);
  11724. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11725. memset(dmabuf->virt, 0, hw_page_size);
  11726. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11727. putPaddrLow(dmabuf->phys);
  11728. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11729. putPaddrHigh(dmabuf->phys);
  11730. }
  11731. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11732. /* The IOCTL status is embedded in the mailbox subheader. */
  11733. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11734. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11735. if (shdr_status || shdr_add_status || rc) {
  11736. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11737. "2504 RQ_CREATE mailbox failed with "
  11738. "status x%x add_status x%x, mbx status x%x\n",
  11739. shdr_status, shdr_add_status, rc);
  11740. status = -ENXIO;
  11741. goto out;
  11742. }
  11743. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11744. if (hrq->queue_id == 0xFFFF) {
  11745. status = -ENXIO;
  11746. goto out;
  11747. }
  11748. hrq->type = LPFC_HRQ;
  11749. hrq->assoc_qid = cq->queue_id;
  11750. hrq->subtype = subtype;
  11751. hrq->host_index = 0;
  11752. hrq->hba_index = 0;
  11753. /* now create the data queue */
  11754. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11755. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11756. length, LPFC_SLI4_MBX_EMBED);
  11757. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11758. phba->sli4_hba.pc_sli4_params.rqv);
  11759. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11760. bf_set(lpfc_rq_context_rqe_count_1,
  11761. &rq_create->u.request.context, hrq->entry_count);
  11762. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11763. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11764. LPFC_RQE_SIZE_8);
  11765. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11766. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11767. } else {
  11768. switch (drq->entry_count) {
  11769. default:
  11770. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11771. "2536 Unsupported RQ count. (%d)\n",
  11772. drq->entry_count);
  11773. if (drq->entry_count < 512) {
  11774. status = -EINVAL;
  11775. goto out;
  11776. }
  11777. /* otherwise default to smallest count (drop through) */
  11778. case 512:
  11779. bf_set(lpfc_rq_context_rqe_count,
  11780. &rq_create->u.request.context,
  11781. LPFC_RQ_RING_SIZE_512);
  11782. break;
  11783. case 1024:
  11784. bf_set(lpfc_rq_context_rqe_count,
  11785. &rq_create->u.request.context,
  11786. LPFC_RQ_RING_SIZE_1024);
  11787. break;
  11788. case 2048:
  11789. bf_set(lpfc_rq_context_rqe_count,
  11790. &rq_create->u.request.context,
  11791. LPFC_RQ_RING_SIZE_2048);
  11792. break;
  11793. case 4096:
  11794. bf_set(lpfc_rq_context_rqe_count,
  11795. &rq_create->u.request.context,
  11796. LPFC_RQ_RING_SIZE_4096);
  11797. break;
  11798. }
  11799. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11800. LPFC_DATA_BUF_SIZE);
  11801. }
  11802. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11803. cq->queue_id);
  11804. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11805. drq->page_count);
  11806. list_for_each_entry(dmabuf, &drq->page_list, list) {
  11807. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11808. putPaddrLow(dmabuf->phys);
  11809. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11810. putPaddrHigh(dmabuf->phys);
  11811. }
  11812. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11813. /* The IOCTL status is embedded in the mailbox subheader. */
  11814. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11815. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11816. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11817. if (shdr_status || shdr_add_status || rc) {
  11818. status = -ENXIO;
  11819. goto out;
  11820. }
  11821. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11822. if (drq->queue_id == 0xFFFF) {
  11823. status = -ENXIO;
  11824. goto out;
  11825. }
  11826. drq->type = LPFC_DRQ;
  11827. drq->assoc_qid = cq->queue_id;
  11828. drq->subtype = subtype;
  11829. drq->host_index = 0;
  11830. drq->hba_index = 0;
  11831. /* link the header and data RQs onto the parent cq child list */
  11832. list_add_tail(&hrq->list, &cq->child_list);
  11833. list_add_tail(&drq->list, &cq->child_list);
  11834. out:
  11835. mempool_free(mbox, phba->mbox_mem_pool);
  11836. return status;
  11837. }
  11838. /**
  11839. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  11840. * @eq: The queue structure associated with the queue to destroy.
  11841. *
  11842. * This function destroys a queue, as detailed in @eq by sending an mailbox
  11843. * command, specific to the type of queue, to the HBA.
  11844. *
  11845. * The @eq struct is used to get the queue ID of the queue to destroy.
  11846. *
  11847. * On success this function will return a zero. If the queue destroy mailbox
  11848. * command fails this function will return -ENXIO.
  11849. **/
  11850. uint32_t
  11851. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11852. {
  11853. LPFC_MBOXQ_t *mbox;
  11854. int rc, length, status = 0;
  11855. uint32_t shdr_status, shdr_add_status;
  11856. union lpfc_sli4_cfg_shdr *shdr;
  11857. /* sanity check on queue memory */
  11858. if (!eq)
  11859. return -ENODEV;
  11860. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  11861. if (!mbox)
  11862. return -ENOMEM;
  11863. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  11864. sizeof(struct lpfc_sli4_cfg_mhdr));
  11865. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11866. LPFC_MBOX_OPCODE_EQ_DESTROY,
  11867. length, LPFC_SLI4_MBX_EMBED);
  11868. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  11869. eq->queue_id);
  11870. mbox->vport = eq->phba->pport;
  11871. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11872. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  11873. /* The IOCTL status is embedded in the mailbox subheader. */
  11874. shdr = (union lpfc_sli4_cfg_shdr *)
  11875. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  11876. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11877. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11878. if (shdr_status || shdr_add_status || rc) {
  11879. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11880. "2505 EQ_DESTROY mailbox failed with "
  11881. "status x%x add_status x%x, mbx status x%x\n",
  11882. shdr_status, shdr_add_status, rc);
  11883. status = -ENXIO;
  11884. }
  11885. /* Remove eq from any list */
  11886. list_del_init(&eq->list);
  11887. mempool_free(mbox, eq->phba->mbox_mem_pool);
  11888. return status;
  11889. }
  11890. /**
  11891. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  11892. * @cq: The queue structure associated with the queue to destroy.
  11893. *
  11894. * This function destroys a queue, as detailed in @cq by sending an mailbox
  11895. * command, specific to the type of queue, to the HBA.
  11896. *
  11897. * The @cq struct is used to get the queue ID of the queue to destroy.
  11898. *
  11899. * On success this function will return a zero. If the queue destroy mailbox
  11900. * command fails this function will return -ENXIO.
  11901. **/
  11902. uint32_t
  11903. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  11904. {
  11905. LPFC_MBOXQ_t *mbox;
  11906. int rc, length, status = 0;
  11907. uint32_t shdr_status, shdr_add_status;
  11908. union lpfc_sli4_cfg_shdr *shdr;
  11909. /* sanity check on queue memory */
  11910. if (!cq)
  11911. return -ENODEV;
  11912. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  11913. if (!mbox)
  11914. return -ENOMEM;
  11915. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  11916. sizeof(struct lpfc_sli4_cfg_mhdr));
  11917. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11918. LPFC_MBOX_OPCODE_CQ_DESTROY,
  11919. length, LPFC_SLI4_MBX_EMBED);
  11920. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  11921. cq->queue_id);
  11922. mbox->vport = cq->phba->pport;
  11923. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11924. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  11925. /* The IOCTL status is embedded in the mailbox subheader. */
  11926. shdr = (union lpfc_sli4_cfg_shdr *)
  11927. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  11928. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11929. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11930. if (shdr_status || shdr_add_status || rc) {
  11931. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11932. "2506 CQ_DESTROY mailbox failed with "
  11933. "status x%x add_status x%x, mbx status x%x\n",
  11934. shdr_status, shdr_add_status, rc);
  11935. status = -ENXIO;
  11936. }
  11937. /* Remove cq from any list */
  11938. list_del_init(&cq->list);
  11939. mempool_free(mbox, cq->phba->mbox_mem_pool);
  11940. return status;
  11941. }
  11942. /**
  11943. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  11944. * @qm: The queue structure associated with the queue to destroy.
  11945. *
  11946. * This function destroys a queue, as detailed in @mq by sending an mailbox
  11947. * command, specific to the type of queue, to the HBA.
  11948. *
  11949. * The @mq struct is used to get the queue ID of the queue to destroy.
  11950. *
  11951. * On success this function will return a zero. If the queue destroy mailbox
  11952. * command fails this function will return -ENXIO.
  11953. **/
  11954. uint32_t
  11955. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  11956. {
  11957. LPFC_MBOXQ_t *mbox;
  11958. int rc, length, status = 0;
  11959. uint32_t shdr_status, shdr_add_status;
  11960. union lpfc_sli4_cfg_shdr *shdr;
  11961. /* sanity check on queue memory */
  11962. if (!mq)
  11963. return -ENODEV;
  11964. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  11965. if (!mbox)
  11966. return -ENOMEM;
  11967. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  11968. sizeof(struct lpfc_sli4_cfg_mhdr));
  11969. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11970. LPFC_MBOX_OPCODE_MQ_DESTROY,
  11971. length, LPFC_SLI4_MBX_EMBED);
  11972. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  11973. mq->queue_id);
  11974. mbox->vport = mq->phba->pport;
  11975. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11976. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  11977. /* The IOCTL status is embedded in the mailbox subheader. */
  11978. shdr = (union lpfc_sli4_cfg_shdr *)
  11979. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  11980. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11981. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11982. if (shdr_status || shdr_add_status || rc) {
  11983. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11984. "2507 MQ_DESTROY mailbox failed with "
  11985. "status x%x add_status x%x, mbx status x%x\n",
  11986. shdr_status, shdr_add_status, rc);
  11987. status = -ENXIO;
  11988. }
  11989. /* Remove mq from any list */
  11990. list_del_init(&mq->list);
  11991. mempool_free(mbox, mq->phba->mbox_mem_pool);
  11992. return status;
  11993. }
  11994. /**
  11995. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  11996. * @wq: The queue structure associated with the queue to destroy.
  11997. *
  11998. * This function destroys a queue, as detailed in @wq by sending an mailbox
  11999. * command, specific to the type of queue, to the HBA.
  12000. *
  12001. * The @wq struct is used to get the queue ID of the queue to destroy.
  12002. *
  12003. * On success this function will return a zero. If the queue destroy mailbox
  12004. * command fails this function will return -ENXIO.
  12005. **/
  12006. uint32_t
  12007. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  12008. {
  12009. LPFC_MBOXQ_t *mbox;
  12010. int rc, length, status = 0;
  12011. uint32_t shdr_status, shdr_add_status;
  12012. union lpfc_sli4_cfg_shdr *shdr;
  12013. /* sanity check on queue memory */
  12014. if (!wq)
  12015. return -ENODEV;
  12016. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  12017. if (!mbox)
  12018. return -ENOMEM;
  12019. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  12020. sizeof(struct lpfc_sli4_cfg_mhdr));
  12021. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12022. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  12023. length, LPFC_SLI4_MBX_EMBED);
  12024. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  12025. wq->queue_id);
  12026. mbox->vport = wq->phba->pport;
  12027. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12028. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  12029. shdr = (union lpfc_sli4_cfg_shdr *)
  12030. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  12031. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12032. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12033. if (shdr_status || shdr_add_status || rc) {
  12034. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12035. "2508 WQ_DESTROY mailbox failed with "
  12036. "status x%x add_status x%x, mbx status x%x\n",
  12037. shdr_status, shdr_add_status, rc);
  12038. status = -ENXIO;
  12039. }
  12040. /* Remove wq from any list */
  12041. list_del_init(&wq->list);
  12042. mempool_free(mbox, wq->phba->mbox_mem_pool);
  12043. return status;
  12044. }
  12045. /**
  12046. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  12047. * @rq: The queue structure associated with the queue to destroy.
  12048. *
  12049. * This function destroys a queue, as detailed in @rq by sending an mailbox
  12050. * command, specific to the type of queue, to the HBA.
  12051. *
  12052. * The @rq struct is used to get the queue ID of the queue to destroy.
  12053. *
  12054. * On success this function will return a zero. If the queue destroy mailbox
  12055. * command fails this function will return -ENXIO.
  12056. **/
  12057. uint32_t
  12058. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  12059. struct lpfc_queue *drq)
  12060. {
  12061. LPFC_MBOXQ_t *mbox;
  12062. int rc, length, status = 0;
  12063. uint32_t shdr_status, shdr_add_status;
  12064. union lpfc_sli4_cfg_shdr *shdr;
  12065. /* sanity check on queue memory */
  12066. if (!hrq || !drq)
  12067. return -ENODEV;
  12068. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  12069. if (!mbox)
  12070. return -ENOMEM;
  12071. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  12072. sizeof(struct lpfc_sli4_cfg_mhdr));
  12073. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12074. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  12075. length, LPFC_SLI4_MBX_EMBED);
  12076. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12077. hrq->queue_id);
  12078. mbox->vport = hrq->phba->pport;
  12079. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12080. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  12081. /* The IOCTL status is embedded in the mailbox subheader. */
  12082. shdr = (union lpfc_sli4_cfg_shdr *)
  12083. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12084. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12085. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12086. if (shdr_status || shdr_add_status || rc) {
  12087. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12088. "2509 RQ_DESTROY mailbox failed with "
  12089. "status x%x add_status x%x, mbx status x%x\n",
  12090. shdr_status, shdr_add_status, rc);
  12091. if (rc != MBX_TIMEOUT)
  12092. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12093. return -ENXIO;
  12094. }
  12095. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12096. drq->queue_id);
  12097. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  12098. shdr = (union lpfc_sli4_cfg_shdr *)
  12099. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12100. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12101. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12102. if (shdr_status || shdr_add_status || rc) {
  12103. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12104. "2510 RQ_DESTROY mailbox failed with "
  12105. "status x%x add_status x%x, mbx status x%x\n",
  12106. shdr_status, shdr_add_status, rc);
  12107. status = -ENXIO;
  12108. }
  12109. list_del_init(&hrq->list);
  12110. list_del_init(&drq->list);
  12111. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12112. return status;
  12113. }
  12114. /**
  12115. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  12116. * @phba: The virtual port for which this call being executed.
  12117. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  12118. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  12119. * @xritag: the xritag that ties this io to the SGL pages.
  12120. *
  12121. * This routine will post the sgl pages for the IO that has the xritag
  12122. * that is in the iocbq structure. The xritag is assigned during iocbq
  12123. * creation and persists for as long as the driver is loaded.
  12124. * if the caller has fewer than 256 scatter gather segments to map then
  12125. * pdma_phys_addr1 should be 0.
  12126. * If the caller needs to map more than 256 scatter gather segment then
  12127. * pdma_phys_addr1 should be a valid physical address.
  12128. * physical address for SGLs must be 64 byte aligned.
  12129. * If you are going to map 2 SGL's then the first one must have 256 entries
  12130. * the second sgl can have between 1 and 256 entries.
  12131. *
  12132. * Return codes:
  12133. * 0 - Success
  12134. * -ENXIO, -ENOMEM - Failure
  12135. **/
  12136. int
  12137. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  12138. dma_addr_t pdma_phys_addr0,
  12139. dma_addr_t pdma_phys_addr1,
  12140. uint16_t xritag)
  12141. {
  12142. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  12143. LPFC_MBOXQ_t *mbox;
  12144. int rc;
  12145. uint32_t shdr_status, shdr_add_status;
  12146. uint32_t mbox_tmo;
  12147. union lpfc_sli4_cfg_shdr *shdr;
  12148. if (xritag == NO_XRI) {
  12149. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12150. "0364 Invalid param:\n");
  12151. return -EINVAL;
  12152. }
  12153. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12154. if (!mbox)
  12155. return -ENOMEM;
  12156. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12157. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12158. sizeof(struct lpfc_mbx_post_sgl_pages) -
  12159. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  12160. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  12161. &mbox->u.mqe.un.post_sgl_pages;
  12162. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  12163. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  12164. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  12165. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  12166. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  12167. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  12168. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  12169. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  12170. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  12171. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  12172. if (!phba->sli4_hba.intr_enable)
  12173. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12174. else {
  12175. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12176. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12177. }
  12178. /* The IOCTL status is embedded in the mailbox subheader. */
  12179. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  12180. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12181. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12182. if (rc != MBX_TIMEOUT)
  12183. mempool_free(mbox, phba->mbox_mem_pool);
  12184. if (shdr_status || shdr_add_status || rc) {
  12185. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12186. "2511 POST_SGL mailbox failed with "
  12187. "status x%x add_status x%x, mbx status x%x\n",
  12188. shdr_status, shdr_add_status, rc);
  12189. rc = -ENXIO;
  12190. }
  12191. return 0;
  12192. }
  12193. /**
  12194. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  12195. * @phba: pointer to lpfc hba data structure.
  12196. *
  12197. * This routine is invoked to post rpi header templates to the
  12198. * HBA consistent with the SLI-4 interface spec. This routine
  12199. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  12200. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  12201. *
  12202. * Returns
  12203. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  12204. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  12205. **/
  12206. uint16_t
  12207. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  12208. {
  12209. unsigned long xri;
  12210. /*
  12211. * Fetch the next logical xri. Because this index is logical,
  12212. * the driver starts at 0 each time.
  12213. */
  12214. spin_lock_irq(&phba->hbalock);
  12215. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  12216. phba->sli4_hba.max_cfg_param.max_xri, 0);
  12217. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  12218. spin_unlock_irq(&phba->hbalock);
  12219. return NO_XRI;
  12220. } else {
  12221. set_bit(xri, phba->sli4_hba.xri_bmask);
  12222. phba->sli4_hba.max_cfg_param.xri_used++;
  12223. }
  12224. spin_unlock_irq(&phba->hbalock);
  12225. return xri;
  12226. }
  12227. /**
  12228. * lpfc_sli4_free_xri - Release an xri for reuse.
  12229. * @phba: pointer to lpfc hba data structure.
  12230. *
  12231. * This routine is invoked to release an xri to the pool of
  12232. * available rpis maintained by the driver.
  12233. **/
  12234. void
  12235. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12236. {
  12237. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  12238. phba->sli4_hba.max_cfg_param.xri_used--;
  12239. }
  12240. }
  12241. /**
  12242. * lpfc_sli4_free_xri - Release an xri for reuse.
  12243. * @phba: pointer to lpfc hba data structure.
  12244. *
  12245. * This routine is invoked to release an xri to the pool of
  12246. * available rpis maintained by the driver.
  12247. **/
  12248. void
  12249. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12250. {
  12251. spin_lock_irq(&phba->hbalock);
  12252. __lpfc_sli4_free_xri(phba, xri);
  12253. spin_unlock_irq(&phba->hbalock);
  12254. }
  12255. /**
  12256. * lpfc_sli4_next_xritag - Get an xritag for the io
  12257. * @phba: Pointer to HBA context object.
  12258. *
  12259. * This function gets an xritag for the iocb. If there is no unused xritag
  12260. * it will return 0xffff.
  12261. * The function returns the allocated xritag if successful, else returns zero.
  12262. * Zero is not a valid xritag.
  12263. * The caller is not required to hold any lock.
  12264. **/
  12265. uint16_t
  12266. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  12267. {
  12268. uint16_t xri_index;
  12269. xri_index = lpfc_sli4_alloc_xri(phba);
  12270. if (xri_index == NO_XRI)
  12271. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  12272. "2004 Failed to allocate XRI.last XRITAG is %d"
  12273. " Max XRI is %d, Used XRI is %d\n",
  12274. xri_index,
  12275. phba->sli4_hba.max_cfg_param.max_xri,
  12276. phba->sli4_hba.max_cfg_param.xri_used);
  12277. return xri_index;
  12278. }
  12279. /**
  12280. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  12281. * @phba: pointer to lpfc hba data structure.
  12282. * @post_sgl_list: pointer to els sgl entry list.
  12283. * @count: number of els sgl entries on the list.
  12284. *
  12285. * This routine is invoked to post a block of driver's sgl pages to the
  12286. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12287. * is only called when the driver is loading and after all IO has been
  12288. * stopped.
  12289. **/
  12290. static int
  12291. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
  12292. struct list_head *post_sgl_list,
  12293. int post_cnt)
  12294. {
  12295. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  12296. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12297. struct sgl_page_pairs *sgl_pg_pairs;
  12298. void *viraddr;
  12299. LPFC_MBOXQ_t *mbox;
  12300. uint32_t reqlen, alloclen, pg_pairs;
  12301. uint32_t mbox_tmo;
  12302. uint16_t xritag_start = 0;
  12303. int rc = 0;
  12304. uint32_t shdr_status, shdr_add_status;
  12305. union lpfc_sli4_cfg_shdr *shdr;
  12306. reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12307. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12308. if (reqlen > SLI4_PAGE_SIZE) {
  12309. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12310. "2559 Block sgl registration required DMA "
  12311. "size (%d) great than a page\n", reqlen);
  12312. return -ENOMEM;
  12313. }
  12314. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12315. if (!mbox)
  12316. return -ENOMEM;
  12317. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12318. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12319. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12320. LPFC_SLI4_MBX_NEMBED);
  12321. if (alloclen < reqlen) {
  12322. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12323. "0285 Allocated DMA memory size (%d) is "
  12324. "less than the requested DMA memory "
  12325. "size (%d)\n", alloclen, reqlen);
  12326. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12327. return -ENOMEM;
  12328. }
  12329. /* Set up the SGL pages in the non-embedded DMA pages */
  12330. viraddr = mbox->sge_array->addr[0];
  12331. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12332. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12333. pg_pairs = 0;
  12334. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  12335. /* Set up the sge entry */
  12336. sgl_pg_pairs->sgl_pg0_addr_lo =
  12337. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12338. sgl_pg_pairs->sgl_pg0_addr_hi =
  12339. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12340. sgl_pg_pairs->sgl_pg1_addr_lo =
  12341. cpu_to_le32(putPaddrLow(0));
  12342. sgl_pg_pairs->sgl_pg1_addr_hi =
  12343. cpu_to_le32(putPaddrHigh(0));
  12344. /* Keep the first xritag on the list */
  12345. if (pg_pairs == 0)
  12346. xritag_start = sglq_entry->sli4_xritag;
  12347. sgl_pg_pairs++;
  12348. pg_pairs++;
  12349. }
  12350. /* Complete initialization and perform endian conversion. */
  12351. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12352. bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
  12353. sgl->word0 = cpu_to_le32(sgl->word0);
  12354. if (!phba->sli4_hba.intr_enable)
  12355. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12356. else {
  12357. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12358. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12359. }
  12360. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12361. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12362. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12363. if (rc != MBX_TIMEOUT)
  12364. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12365. if (shdr_status || shdr_add_status || rc) {
  12366. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12367. "2513 POST_SGL_BLOCK mailbox command failed "
  12368. "status x%x add_status x%x mbx status x%x\n",
  12369. shdr_status, shdr_add_status, rc);
  12370. rc = -ENXIO;
  12371. }
  12372. return rc;
  12373. }
  12374. /**
  12375. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12376. * @phba: pointer to lpfc hba data structure.
  12377. * @sblist: pointer to scsi buffer list.
  12378. * @count: number of scsi buffers on the list.
  12379. *
  12380. * This routine is invoked to post a block of @count scsi sgl pages from a
  12381. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12382. * No Lock is held.
  12383. *
  12384. **/
  12385. int
  12386. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  12387. struct list_head *sblist,
  12388. int count)
  12389. {
  12390. struct lpfc_scsi_buf *psb;
  12391. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12392. struct sgl_page_pairs *sgl_pg_pairs;
  12393. void *viraddr;
  12394. LPFC_MBOXQ_t *mbox;
  12395. uint32_t reqlen, alloclen, pg_pairs;
  12396. uint32_t mbox_tmo;
  12397. uint16_t xritag_start = 0;
  12398. int rc = 0;
  12399. uint32_t shdr_status, shdr_add_status;
  12400. dma_addr_t pdma_phys_bpl1;
  12401. union lpfc_sli4_cfg_shdr *shdr;
  12402. /* Calculate the requested length of the dma memory */
  12403. reqlen = count * sizeof(struct sgl_page_pairs) +
  12404. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12405. if (reqlen > SLI4_PAGE_SIZE) {
  12406. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12407. "0217 Block sgl registration required DMA "
  12408. "size (%d) great than a page\n", reqlen);
  12409. return -ENOMEM;
  12410. }
  12411. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12412. if (!mbox) {
  12413. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12414. "0283 Failed to allocate mbox cmd memory\n");
  12415. return -ENOMEM;
  12416. }
  12417. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12418. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12419. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12420. LPFC_SLI4_MBX_NEMBED);
  12421. if (alloclen < reqlen) {
  12422. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12423. "2561 Allocated DMA memory size (%d) is "
  12424. "less than the requested DMA memory "
  12425. "size (%d)\n", alloclen, reqlen);
  12426. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12427. return -ENOMEM;
  12428. }
  12429. /* Get the first SGE entry from the non-embedded DMA memory */
  12430. viraddr = mbox->sge_array->addr[0];
  12431. /* Set up the SGL pages in the non-embedded DMA pages */
  12432. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12433. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12434. pg_pairs = 0;
  12435. list_for_each_entry(psb, sblist, list) {
  12436. /* Set up the sge entry */
  12437. sgl_pg_pairs->sgl_pg0_addr_lo =
  12438. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12439. sgl_pg_pairs->sgl_pg0_addr_hi =
  12440. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12441. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12442. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12443. else
  12444. pdma_phys_bpl1 = 0;
  12445. sgl_pg_pairs->sgl_pg1_addr_lo =
  12446. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12447. sgl_pg_pairs->sgl_pg1_addr_hi =
  12448. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12449. /* Keep the first xritag on the list */
  12450. if (pg_pairs == 0)
  12451. xritag_start = psb->cur_iocbq.sli4_xritag;
  12452. sgl_pg_pairs++;
  12453. pg_pairs++;
  12454. }
  12455. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12456. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12457. /* Perform endian conversion if necessary */
  12458. sgl->word0 = cpu_to_le32(sgl->word0);
  12459. if (!phba->sli4_hba.intr_enable)
  12460. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12461. else {
  12462. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12463. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12464. }
  12465. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12466. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12467. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12468. if (rc != MBX_TIMEOUT)
  12469. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12470. if (shdr_status || shdr_add_status || rc) {
  12471. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12472. "2564 POST_SGL_BLOCK mailbox command failed "
  12473. "status x%x add_status x%x mbx status x%x\n",
  12474. shdr_status, shdr_add_status, rc);
  12475. rc = -ENXIO;
  12476. }
  12477. return rc;
  12478. }
  12479. /**
  12480. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12481. * @phba: pointer to lpfc_hba struct that the frame was received on
  12482. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12483. *
  12484. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12485. * valid type of frame that the LPFC driver will handle. This function will
  12486. * return a zero if the frame is a valid frame or a non zero value when the
  12487. * frame does not pass the check.
  12488. **/
  12489. static int
  12490. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12491. {
  12492. /* make rctl_names static to save stack space */
  12493. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12494. char *type_names[] = FC_TYPE_NAMES_INIT;
  12495. struct fc_vft_header *fc_vft_hdr;
  12496. uint32_t *header = (uint32_t *) fc_hdr;
  12497. switch (fc_hdr->fh_r_ctl) {
  12498. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12499. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12500. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12501. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12502. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12503. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12504. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12505. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12506. case FC_RCTL_ELS_REQ: /* extended link services request */
  12507. case FC_RCTL_ELS_REP: /* extended link services reply */
  12508. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12509. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12510. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12511. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12512. case FC_RCTL_BA_RMC: /* remove connection */
  12513. case FC_RCTL_BA_ACC: /* basic accept */
  12514. case FC_RCTL_BA_RJT: /* basic reject */
  12515. case FC_RCTL_BA_PRMT:
  12516. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12517. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12518. case FC_RCTL_P_RJT: /* port reject */
  12519. case FC_RCTL_F_RJT: /* fabric reject */
  12520. case FC_RCTL_P_BSY: /* port busy */
  12521. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12522. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12523. case FC_RCTL_LCR: /* link credit reset */
  12524. case FC_RCTL_END: /* end */
  12525. break;
  12526. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12527. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12528. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12529. return lpfc_fc_frame_check(phba, fc_hdr);
  12530. default:
  12531. goto drop;
  12532. }
  12533. switch (fc_hdr->fh_type) {
  12534. case FC_TYPE_BLS:
  12535. case FC_TYPE_ELS:
  12536. case FC_TYPE_FCP:
  12537. case FC_TYPE_CT:
  12538. break;
  12539. case FC_TYPE_IP:
  12540. case FC_TYPE_ILS:
  12541. default:
  12542. goto drop;
  12543. }
  12544. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12545. "2538 Received frame rctl:%s type:%s "
  12546. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12547. rctl_names[fc_hdr->fh_r_ctl],
  12548. type_names[fc_hdr->fh_type],
  12549. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12550. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12551. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12552. return 0;
  12553. drop:
  12554. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12555. "2539 Dropped frame rctl:%s type:%s\n",
  12556. rctl_names[fc_hdr->fh_r_ctl],
  12557. type_names[fc_hdr->fh_type]);
  12558. return 1;
  12559. }
  12560. /**
  12561. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12562. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12563. *
  12564. * This function processes the FC header to retrieve the VFI from the VF
  12565. * header, if one exists. This function will return the VFI if one exists
  12566. * or 0 if no VSAN Header exists.
  12567. **/
  12568. static uint32_t
  12569. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12570. {
  12571. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12572. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12573. return 0;
  12574. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12575. }
  12576. /**
  12577. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12578. * @phba: Pointer to the HBA structure to search for the vport on
  12579. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12580. * @fcfi: The FC Fabric ID that the frame came from
  12581. *
  12582. * This function searches the @phba for a vport that matches the content of the
  12583. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12584. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12585. * returns the matching vport pointer or NULL if unable to match frame to a
  12586. * vport.
  12587. **/
  12588. static struct lpfc_vport *
  12589. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12590. uint16_t fcfi)
  12591. {
  12592. struct lpfc_vport **vports;
  12593. struct lpfc_vport *vport = NULL;
  12594. int i;
  12595. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12596. fc_hdr->fh_d_id[1] << 8 |
  12597. fc_hdr->fh_d_id[2]);
  12598. if (did == Fabric_DID)
  12599. return phba->pport;
  12600. if ((phba->pport->fc_flag & FC_PT2PT) &&
  12601. !(phba->link_state == LPFC_HBA_READY))
  12602. return phba->pport;
  12603. vports = lpfc_create_vport_work_array(phba);
  12604. if (vports != NULL)
  12605. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12606. if (phba->fcf.fcfi == fcfi &&
  12607. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12608. vports[i]->fc_myDID == did) {
  12609. vport = vports[i];
  12610. break;
  12611. }
  12612. }
  12613. lpfc_destroy_vport_work_array(phba, vports);
  12614. return vport;
  12615. }
  12616. /**
  12617. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12618. * @vport: The vport to work on.
  12619. *
  12620. * This function updates the receive sequence time stamp for this vport. The
  12621. * receive sequence time stamp indicates the time that the last frame of the
  12622. * the sequence that has been idle for the longest amount of time was received.
  12623. * the driver uses this time stamp to indicate if any received sequences have
  12624. * timed out.
  12625. **/
  12626. void
  12627. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12628. {
  12629. struct lpfc_dmabuf *h_buf;
  12630. struct hbq_dmabuf *dmabuf = NULL;
  12631. /* get the oldest sequence on the rcv list */
  12632. h_buf = list_get_first(&vport->rcv_buffer_list,
  12633. struct lpfc_dmabuf, list);
  12634. if (!h_buf)
  12635. return;
  12636. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12637. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12638. }
  12639. /**
  12640. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12641. * @vport: The vport that the received sequences were sent to.
  12642. *
  12643. * This function cleans up all outstanding received sequences. This is called
  12644. * by the driver when a link event or user action invalidates all the received
  12645. * sequences.
  12646. **/
  12647. void
  12648. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12649. {
  12650. struct lpfc_dmabuf *h_buf, *hnext;
  12651. struct lpfc_dmabuf *d_buf, *dnext;
  12652. struct hbq_dmabuf *dmabuf = NULL;
  12653. /* start with the oldest sequence on the rcv list */
  12654. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12655. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12656. list_del_init(&dmabuf->hbuf.list);
  12657. list_for_each_entry_safe(d_buf, dnext,
  12658. &dmabuf->dbuf.list, list) {
  12659. list_del_init(&d_buf->list);
  12660. lpfc_in_buf_free(vport->phba, d_buf);
  12661. }
  12662. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12663. }
  12664. }
  12665. /**
  12666. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12667. * @vport: The vport that the received sequences were sent to.
  12668. *
  12669. * This function determines whether any received sequences have timed out by
  12670. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12671. * indicates that there is at least one timed out sequence this routine will
  12672. * go through the received sequences one at a time from most inactive to most
  12673. * active to determine which ones need to be cleaned up. Once it has determined
  12674. * that a sequence needs to be cleaned up it will simply free up the resources
  12675. * without sending an abort.
  12676. **/
  12677. void
  12678. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12679. {
  12680. struct lpfc_dmabuf *h_buf, *hnext;
  12681. struct lpfc_dmabuf *d_buf, *dnext;
  12682. struct hbq_dmabuf *dmabuf = NULL;
  12683. unsigned long timeout;
  12684. int abort_count = 0;
  12685. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12686. vport->rcv_buffer_time_stamp);
  12687. if (list_empty(&vport->rcv_buffer_list) ||
  12688. time_before(jiffies, timeout))
  12689. return;
  12690. /* start with the oldest sequence on the rcv list */
  12691. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12692. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12693. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12694. dmabuf->time_stamp);
  12695. if (time_before(jiffies, timeout))
  12696. break;
  12697. abort_count++;
  12698. list_del_init(&dmabuf->hbuf.list);
  12699. list_for_each_entry_safe(d_buf, dnext,
  12700. &dmabuf->dbuf.list, list) {
  12701. list_del_init(&d_buf->list);
  12702. lpfc_in_buf_free(vport->phba, d_buf);
  12703. }
  12704. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12705. }
  12706. if (abort_count)
  12707. lpfc_update_rcv_time_stamp(vport);
  12708. }
  12709. /**
  12710. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12711. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12712. *
  12713. * This function searches through the existing incomplete sequences that have
  12714. * been sent to this @vport. If the frame matches one of the incomplete
  12715. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12716. * make up that sequence. If no sequence is found that matches this frame then
  12717. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12718. * This function returns a pointer to the first dmabuf in the sequence list that
  12719. * the frame was linked to.
  12720. **/
  12721. static struct hbq_dmabuf *
  12722. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12723. {
  12724. struct fc_frame_header *new_hdr;
  12725. struct fc_frame_header *temp_hdr;
  12726. struct lpfc_dmabuf *d_buf;
  12727. struct lpfc_dmabuf *h_buf;
  12728. struct hbq_dmabuf *seq_dmabuf = NULL;
  12729. struct hbq_dmabuf *temp_dmabuf = NULL;
  12730. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12731. dmabuf->time_stamp = jiffies;
  12732. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12733. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12734. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12735. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12736. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12737. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12738. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12739. continue;
  12740. /* found a pending sequence that matches this frame */
  12741. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12742. break;
  12743. }
  12744. if (!seq_dmabuf) {
  12745. /*
  12746. * This indicates first frame received for this sequence.
  12747. * Queue the buffer on the vport's rcv_buffer_list.
  12748. */
  12749. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12750. lpfc_update_rcv_time_stamp(vport);
  12751. return dmabuf;
  12752. }
  12753. temp_hdr = seq_dmabuf->hbuf.virt;
  12754. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12755. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12756. list_del_init(&seq_dmabuf->hbuf.list);
  12757. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12758. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12759. lpfc_update_rcv_time_stamp(vport);
  12760. return dmabuf;
  12761. }
  12762. /* move this sequence to the tail to indicate a young sequence */
  12763. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12764. seq_dmabuf->time_stamp = jiffies;
  12765. lpfc_update_rcv_time_stamp(vport);
  12766. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12767. temp_hdr = dmabuf->hbuf.virt;
  12768. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12769. return seq_dmabuf;
  12770. }
  12771. /* find the correct place in the sequence to insert this frame */
  12772. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12773. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12774. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12775. /*
  12776. * If the frame's sequence count is greater than the frame on
  12777. * the list then insert the frame right after this frame
  12778. */
  12779. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  12780. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12781. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  12782. return seq_dmabuf;
  12783. }
  12784. }
  12785. return NULL;
  12786. }
  12787. /**
  12788. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  12789. * @vport: pointer to a vitural port
  12790. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12791. *
  12792. * This function tries to abort from the partially assembed sequence, described
  12793. * by the information from basic abbort @dmabuf. It checks to see whether such
  12794. * partially assembled sequence held by the driver. If so, it shall free up all
  12795. * the frames from the partially assembled sequence.
  12796. *
  12797. * Return
  12798. * true -- if there is matching partially assembled sequence present and all
  12799. * the frames freed with the sequence;
  12800. * false -- if there is no matching partially assembled sequence present so
  12801. * nothing got aborted in the lower layer driver
  12802. **/
  12803. static bool
  12804. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  12805. struct hbq_dmabuf *dmabuf)
  12806. {
  12807. struct fc_frame_header *new_hdr;
  12808. struct fc_frame_header *temp_hdr;
  12809. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  12810. struct hbq_dmabuf *seq_dmabuf = NULL;
  12811. /* Use the hdr_buf to find the sequence that matches this frame */
  12812. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12813. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  12814. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12815. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12816. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12817. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12818. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12819. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12820. continue;
  12821. /* found a pending sequence that matches this frame */
  12822. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12823. break;
  12824. }
  12825. /* Free up all the frames from the partially assembled sequence */
  12826. if (seq_dmabuf) {
  12827. list_for_each_entry_safe(d_buf, n_buf,
  12828. &seq_dmabuf->dbuf.list, list) {
  12829. list_del_init(&d_buf->list);
  12830. lpfc_in_buf_free(vport->phba, d_buf);
  12831. }
  12832. return true;
  12833. }
  12834. return false;
  12835. }
  12836. /**
  12837. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  12838. * @phba: Pointer to HBA context object.
  12839. * @cmd_iocbq: pointer to the command iocbq structure.
  12840. * @rsp_iocbq: pointer to the response iocbq structure.
  12841. *
  12842. * This function handles the sequence abort response iocb command complete
  12843. * event. It properly releases the memory allocated to the sequence abort
  12844. * accept iocb.
  12845. **/
  12846. static void
  12847. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  12848. struct lpfc_iocbq *cmd_iocbq,
  12849. struct lpfc_iocbq *rsp_iocbq)
  12850. {
  12851. if (cmd_iocbq)
  12852. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  12853. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  12854. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  12855. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12856. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  12857. rsp_iocbq->iocb.ulpStatus,
  12858. rsp_iocbq->iocb.un.ulpWord[4]);
  12859. }
  12860. /**
  12861. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  12862. * @phba: Pointer to HBA context object.
  12863. * @xri: xri id in transaction.
  12864. *
  12865. * This function validates the xri maps to the known range of XRIs allocated an
  12866. * used by the driver.
  12867. **/
  12868. uint16_t
  12869. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  12870. uint16_t xri)
  12871. {
  12872. int i;
  12873. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  12874. if (xri == phba->sli4_hba.xri_ids[i])
  12875. return i;
  12876. }
  12877. return NO_XRI;
  12878. }
  12879. /**
  12880. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  12881. * @phba: Pointer to HBA context object.
  12882. * @fc_hdr: pointer to a FC frame header.
  12883. *
  12884. * This function sends a basic response to a previous unsol sequence abort
  12885. * event after aborting the sequence handling.
  12886. **/
  12887. static void
  12888. lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
  12889. struct fc_frame_header *fc_hdr)
  12890. {
  12891. struct lpfc_iocbq *ctiocb = NULL;
  12892. struct lpfc_nodelist *ndlp;
  12893. uint16_t oxid, rxid, xri, lxri;
  12894. uint32_t sid, fctl;
  12895. IOCB_t *icmd;
  12896. int rc;
  12897. if (!lpfc_is_link_up(phba))
  12898. return;
  12899. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12900. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  12901. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  12902. ndlp = lpfc_findnode_did(phba->pport, sid);
  12903. if (!ndlp) {
  12904. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12905. "1268 Find ndlp returned NULL for oxid:x%x "
  12906. "SID:x%x\n", oxid, sid);
  12907. return;
  12908. }
  12909. /* Allocate buffer for rsp iocb */
  12910. ctiocb = lpfc_sli_get_iocbq(phba);
  12911. if (!ctiocb)
  12912. return;
  12913. /* Extract the F_CTL field from FC_HDR */
  12914. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  12915. icmd = &ctiocb->iocb;
  12916. icmd->un.xseq64.bdl.bdeSize = 0;
  12917. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  12918. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  12919. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  12920. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  12921. /* Fill in the rest of iocb fields */
  12922. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  12923. icmd->ulpBdeCount = 0;
  12924. icmd->ulpLe = 1;
  12925. icmd->ulpClass = CLASS3;
  12926. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  12927. ctiocb->context1 = ndlp;
  12928. ctiocb->iocb_cmpl = NULL;
  12929. ctiocb->vport = phba->pport;
  12930. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  12931. ctiocb->sli4_lxritag = NO_XRI;
  12932. ctiocb->sli4_xritag = NO_XRI;
  12933. if (fctl & FC_FC_EX_CTX)
  12934. /* Exchange responder sent the abort so we
  12935. * own the oxid.
  12936. */
  12937. xri = oxid;
  12938. else
  12939. xri = rxid;
  12940. lxri = lpfc_sli4_xri_inrange(phba, xri);
  12941. if (lxri != NO_XRI)
  12942. lpfc_set_rrq_active(phba, ndlp, lxri,
  12943. (xri == oxid) ? rxid : oxid, 0);
  12944. /* If the oxid maps to the FCP XRI range or if it is out of range,
  12945. * send a BLS_RJT. The driver no longer has that exchange.
  12946. * Override the IOCB for a BA_RJT.
  12947. */
  12948. if (xri > (phba->sli4_hba.max_cfg_param.max_xri +
  12949. phba->sli4_hba.max_cfg_param.xri_base) ||
  12950. xri > (lpfc_sli4_get_els_iocb_cnt(phba) +
  12951. phba->sli4_hba.max_cfg_param.xri_base)) {
  12952. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  12953. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  12954. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  12955. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  12956. }
  12957. if (fctl & FC_FC_EX_CTX) {
  12958. /* ABTS sent by responder to CT exchange, construction
  12959. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  12960. * field and RX_ID from ABTS for RX_ID field.
  12961. */
  12962. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  12963. } else {
  12964. /* ABTS sent by initiator to CT exchange, construction
  12965. * of BA_ACC will need to allocate a new XRI as for the
  12966. * XRI_TAG field.
  12967. */
  12968. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  12969. }
  12970. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  12971. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  12972. /* Xmit CT abts response on exchange <xid> */
  12973. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12974. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  12975. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  12976. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  12977. if (rc == IOCB_ERROR) {
  12978. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  12979. "2925 Failed to issue CT ABTS RSP x%x on "
  12980. "xri x%x, Data x%x\n",
  12981. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  12982. phba->link_state);
  12983. lpfc_sli_release_iocbq(phba, ctiocb);
  12984. }
  12985. }
  12986. /**
  12987. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  12988. * @vport: Pointer to the vport on which this sequence was received
  12989. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12990. *
  12991. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  12992. * receive sequence is only partially assembed by the driver, it shall abort
  12993. * the partially assembled frames for the sequence. Otherwise, if the
  12994. * unsolicited receive sequence has been completely assembled and passed to
  12995. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  12996. * unsolicited sequence has been aborted. After that, it will issue a basic
  12997. * accept to accept the abort.
  12998. **/
  12999. void
  13000. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  13001. struct hbq_dmabuf *dmabuf)
  13002. {
  13003. struct lpfc_hba *phba = vport->phba;
  13004. struct fc_frame_header fc_hdr;
  13005. uint32_t fctl;
  13006. bool abts_par;
  13007. /* Make a copy of fc_hdr before the dmabuf being released */
  13008. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  13009. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  13010. if (fctl & FC_FC_EX_CTX) {
  13011. /*
  13012. * ABTS sent by responder to exchange, just free the buffer
  13013. */
  13014. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13015. } else {
  13016. /*
  13017. * ABTS sent by initiator to exchange, need to do cleanup
  13018. */
  13019. /* Try to abort partially assembled seq */
  13020. abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  13021. /* Send abort to ULP if partially seq abort failed */
  13022. if (abts_par == false)
  13023. lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
  13024. else
  13025. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13026. }
  13027. /* Send basic accept (BA_ACC) to the abort requester */
  13028. lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
  13029. }
  13030. /**
  13031. * lpfc_seq_complete - Indicates if a sequence is complete
  13032. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13033. *
  13034. * This function checks the sequence, starting with the frame described by
  13035. * @dmabuf, to see if all the frames associated with this sequence are present.
  13036. * the frames associated with this sequence are linked to the @dmabuf using the
  13037. * dbuf list. This function looks for two major things. 1) That the first frame
  13038. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  13039. * set. 3) That there are no holes in the sequence count. The function will
  13040. * return 1 when the sequence is complete, otherwise it will return 0.
  13041. **/
  13042. static int
  13043. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  13044. {
  13045. struct fc_frame_header *hdr;
  13046. struct lpfc_dmabuf *d_buf;
  13047. struct hbq_dmabuf *seq_dmabuf;
  13048. uint32_t fctl;
  13049. int seq_count = 0;
  13050. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13051. /* make sure first fame of sequence has a sequence count of zero */
  13052. if (hdr->fh_seq_cnt != seq_count)
  13053. return 0;
  13054. fctl = (hdr->fh_f_ctl[0] << 16 |
  13055. hdr->fh_f_ctl[1] << 8 |
  13056. hdr->fh_f_ctl[2]);
  13057. /* If last frame of sequence we can return success. */
  13058. if (fctl & FC_FC_END_SEQ)
  13059. return 1;
  13060. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  13061. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13062. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13063. /* If there is a hole in the sequence count then fail. */
  13064. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  13065. return 0;
  13066. fctl = (hdr->fh_f_ctl[0] << 16 |
  13067. hdr->fh_f_ctl[1] << 8 |
  13068. hdr->fh_f_ctl[2]);
  13069. /* If last frame of sequence we can return success. */
  13070. if (fctl & FC_FC_END_SEQ)
  13071. return 1;
  13072. }
  13073. return 0;
  13074. }
  13075. /**
  13076. * lpfc_prep_seq - Prep sequence for ULP processing
  13077. * @vport: Pointer to the vport on which this sequence was received
  13078. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13079. *
  13080. * This function takes a sequence, described by a list of frames, and creates
  13081. * a list of iocbq structures to describe the sequence. This iocbq list will be
  13082. * used to issue to the generic unsolicited sequence handler. This routine
  13083. * returns a pointer to the first iocbq in the list. If the function is unable
  13084. * to allocate an iocbq then it throw out the received frames that were not
  13085. * able to be described and return a pointer to the first iocbq. If unable to
  13086. * allocate any iocbqs (including the first) this function will return NULL.
  13087. **/
  13088. static struct lpfc_iocbq *
  13089. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  13090. {
  13091. struct hbq_dmabuf *hbq_buf;
  13092. struct lpfc_dmabuf *d_buf, *n_buf;
  13093. struct lpfc_iocbq *first_iocbq, *iocbq;
  13094. struct fc_frame_header *fc_hdr;
  13095. uint32_t sid;
  13096. uint32_t len, tot_len;
  13097. struct ulp_bde64 *pbde;
  13098. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13099. /* remove from receive buffer list */
  13100. list_del_init(&seq_dmabuf->hbuf.list);
  13101. lpfc_update_rcv_time_stamp(vport);
  13102. /* get the Remote Port's SID */
  13103. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13104. tot_len = 0;
  13105. /* Get an iocbq struct to fill in. */
  13106. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  13107. if (first_iocbq) {
  13108. /* Initialize the first IOCB. */
  13109. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  13110. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  13111. /* Check FC Header to see what TYPE of frame we are rcv'ing */
  13112. if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
  13113. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
  13114. first_iocbq->iocb.un.rcvels.parmRo =
  13115. sli4_did_from_fc_hdr(fc_hdr);
  13116. first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
  13117. } else
  13118. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  13119. first_iocbq->iocb.ulpContext = NO_XRI;
  13120. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  13121. be16_to_cpu(fc_hdr->fh_ox_id);
  13122. /* iocbq is prepped for internal consumption. Physical vpi. */
  13123. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  13124. vport->phba->vpi_ids[vport->vpi];
  13125. /* put the first buffer into the first IOCBq */
  13126. first_iocbq->context2 = &seq_dmabuf->dbuf;
  13127. first_iocbq->context3 = NULL;
  13128. first_iocbq->iocb.ulpBdeCount = 1;
  13129. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13130. LPFC_DATA_BUF_SIZE;
  13131. first_iocbq->iocb.un.rcvels.remoteID = sid;
  13132. tot_len = bf_get(lpfc_rcqe_length,
  13133. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  13134. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13135. }
  13136. iocbq = first_iocbq;
  13137. /*
  13138. * Each IOCBq can have two Buffers assigned, so go through the list
  13139. * of buffers for this sequence and save two buffers in each IOCBq
  13140. */
  13141. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13142. if (!iocbq) {
  13143. lpfc_in_buf_free(vport->phba, d_buf);
  13144. continue;
  13145. }
  13146. if (!iocbq->context3) {
  13147. iocbq->context3 = d_buf;
  13148. iocbq->iocb.ulpBdeCount++;
  13149. pbde = (struct ulp_bde64 *)
  13150. &iocbq->iocb.unsli3.sli3Words[4];
  13151. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13152. /* We need to get the size out of the right CQE */
  13153. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13154. len = bf_get(lpfc_rcqe_length,
  13155. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13156. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13157. tot_len += len;
  13158. } else {
  13159. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13160. if (!iocbq) {
  13161. if (first_iocbq) {
  13162. first_iocbq->iocb.ulpStatus =
  13163. IOSTAT_FCP_RSP_ERROR;
  13164. first_iocbq->iocb.un.ulpWord[4] =
  13165. IOERR_NO_RESOURCES;
  13166. }
  13167. lpfc_in_buf_free(vport->phba, d_buf);
  13168. continue;
  13169. }
  13170. iocbq->context2 = d_buf;
  13171. iocbq->context3 = NULL;
  13172. iocbq->iocb.ulpBdeCount = 1;
  13173. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13174. LPFC_DATA_BUF_SIZE;
  13175. /* We need to get the size out of the right CQE */
  13176. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13177. len = bf_get(lpfc_rcqe_length,
  13178. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13179. tot_len += len;
  13180. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13181. iocbq->iocb.un.rcvels.remoteID = sid;
  13182. list_add_tail(&iocbq->list, &first_iocbq->list);
  13183. }
  13184. }
  13185. return first_iocbq;
  13186. }
  13187. static void
  13188. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13189. struct hbq_dmabuf *seq_dmabuf)
  13190. {
  13191. struct fc_frame_header *fc_hdr;
  13192. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13193. struct lpfc_hba *phba = vport->phba;
  13194. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13195. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13196. if (!iocbq) {
  13197. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13198. "2707 Ring %d handler: Failed to allocate "
  13199. "iocb Rctl x%x Type x%x received\n",
  13200. LPFC_ELS_RING,
  13201. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13202. return;
  13203. }
  13204. if (!lpfc_complete_unsol_iocb(phba,
  13205. &phba->sli.ring[LPFC_ELS_RING],
  13206. iocbq, fc_hdr->fh_r_ctl,
  13207. fc_hdr->fh_type))
  13208. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13209. "2540 Ring %d handler: unexpected Rctl "
  13210. "x%x Type x%x received\n",
  13211. LPFC_ELS_RING,
  13212. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13213. /* Free iocb created in lpfc_prep_seq */
  13214. list_for_each_entry_safe(curr_iocb, next_iocb,
  13215. &iocbq->list, list) {
  13216. list_del_init(&curr_iocb->list);
  13217. lpfc_sli_release_iocbq(phba, curr_iocb);
  13218. }
  13219. lpfc_sli_release_iocbq(phba, iocbq);
  13220. }
  13221. /**
  13222. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13223. * @phba: Pointer to HBA context object.
  13224. *
  13225. * This function is called with no lock held. This function processes all
  13226. * the received buffers and gives it to upper layers when a received buffer
  13227. * indicates that it is the final frame in the sequence. The interrupt
  13228. * service routine processes received buffers at interrupt contexts and adds
  13229. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13230. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13231. * appropriate receive function when the final frame in a sequence is received.
  13232. **/
  13233. void
  13234. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13235. struct hbq_dmabuf *dmabuf)
  13236. {
  13237. struct hbq_dmabuf *seq_dmabuf;
  13238. struct fc_frame_header *fc_hdr;
  13239. struct lpfc_vport *vport;
  13240. uint32_t fcfi;
  13241. uint32_t did;
  13242. /* Process each received buffer */
  13243. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13244. /* check to see if this a valid type of frame */
  13245. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13246. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13247. return;
  13248. }
  13249. if ((bf_get(lpfc_cqe_code,
  13250. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13251. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13252. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13253. else
  13254. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13255. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13256. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13257. if (!vport) {
  13258. /* throw out the frame */
  13259. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13260. return;
  13261. }
  13262. /* d_id this frame is directed to */
  13263. did = sli4_did_from_fc_hdr(fc_hdr);
  13264. /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
  13265. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
  13266. (did != Fabric_DID)) {
  13267. /*
  13268. * Throw out the frame if we are not pt2pt.
  13269. * The pt2pt protocol allows for discovery frames
  13270. * to be received without a registered VPI.
  13271. */
  13272. if (!(vport->fc_flag & FC_PT2PT) ||
  13273. (phba->link_state == LPFC_HBA_READY)) {
  13274. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13275. return;
  13276. }
  13277. }
  13278. /* Handle the basic abort sequence (BA_ABTS) event */
  13279. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13280. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13281. return;
  13282. }
  13283. /* Link this frame */
  13284. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13285. if (!seq_dmabuf) {
  13286. /* unable to add frame to vport - throw it out */
  13287. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13288. return;
  13289. }
  13290. /* If not last frame in sequence continue processing frames. */
  13291. if (!lpfc_seq_complete(seq_dmabuf))
  13292. return;
  13293. /* Send the complete sequence to the upper layer protocol */
  13294. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13295. }
  13296. /**
  13297. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13298. * @phba: pointer to lpfc hba data structure.
  13299. *
  13300. * This routine is invoked to post rpi header templates to the
  13301. * HBA consistent with the SLI-4 interface spec. This routine
  13302. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13303. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13304. *
  13305. * This routine does not require any locks. It's usage is expected
  13306. * to be driver load or reset recovery when the driver is
  13307. * sequential.
  13308. *
  13309. * Return codes
  13310. * 0 - successful
  13311. * -EIO - The mailbox failed to complete successfully.
  13312. * When this error occurs, the driver is not guaranteed
  13313. * to have any rpi regions posted to the device and
  13314. * must either attempt to repost the regions or take a
  13315. * fatal error.
  13316. **/
  13317. int
  13318. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13319. {
  13320. struct lpfc_rpi_hdr *rpi_page;
  13321. uint32_t rc = 0;
  13322. uint16_t lrpi = 0;
  13323. /* SLI4 ports that support extents do not require RPI headers. */
  13324. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13325. goto exit;
  13326. if (phba->sli4_hba.extents_in_use)
  13327. return -EIO;
  13328. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13329. /*
  13330. * Assign the rpi headers a physical rpi only if the driver
  13331. * has not initialized those resources. A port reset only
  13332. * needs the headers posted.
  13333. */
  13334. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13335. LPFC_RPI_RSRC_RDY)
  13336. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13337. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13338. if (rc != MBX_SUCCESS) {
  13339. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13340. "2008 Error %d posting all rpi "
  13341. "headers\n", rc);
  13342. rc = -EIO;
  13343. break;
  13344. }
  13345. }
  13346. exit:
  13347. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13348. LPFC_RPI_RSRC_RDY);
  13349. return rc;
  13350. }
  13351. /**
  13352. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13353. * @phba: pointer to lpfc hba data structure.
  13354. * @rpi_page: pointer to the rpi memory region.
  13355. *
  13356. * This routine is invoked to post a single rpi header to the
  13357. * HBA consistent with the SLI-4 interface spec. This memory region
  13358. * maps up to 64 rpi context regions.
  13359. *
  13360. * Return codes
  13361. * 0 - successful
  13362. * -ENOMEM - No available memory
  13363. * -EIO - The mailbox failed to complete successfully.
  13364. **/
  13365. int
  13366. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13367. {
  13368. LPFC_MBOXQ_t *mboxq;
  13369. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13370. uint32_t rc = 0;
  13371. uint32_t shdr_status, shdr_add_status;
  13372. union lpfc_sli4_cfg_shdr *shdr;
  13373. /* SLI4 ports that support extents do not require RPI headers. */
  13374. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13375. return rc;
  13376. if (phba->sli4_hba.extents_in_use)
  13377. return -EIO;
  13378. /* The port is notified of the header region via a mailbox command. */
  13379. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13380. if (!mboxq) {
  13381. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13382. "2001 Unable to allocate memory for issuing "
  13383. "SLI_CONFIG_SPECIAL mailbox command\n");
  13384. return -ENOMEM;
  13385. }
  13386. /* Post all rpi memory regions to the port. */
  13387. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13388. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13389. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13390. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13391. sizeof(struct lpfc_sli4_cfg_mhdr),
  13392. LPFC_SLI4_MBX_EMBED);
  13393. /* Post the physical rpi to the port for this rpi header. */
  13394. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13395. rpi_page->start_rpi);
  13396. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13397. hdr_tmpl, rpi_page->page_count);
  13398. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13399. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13400. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13401. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13402. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13403. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13404. if (rc != MBX_TIMEOUT)
  13405. mempool_free(mboxq, phba->mbox_mem_pool);
  13406. if (shdr_status || shdr_add_status || rc) {
  13407. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13408. "2514 POST_RPI_HDR mailbox failed with "
  13409. "status x%x add_status x%x, mbx status x%x\n",
  13410. shdr_status, shdr_add_status, rc);
  13411. rc = -ENXIO;
  13412. }
  13413. return rc;
  13414. }
  13415. /**
  13416. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13417. * @phba: pointer to lpfc hba data structure.
  13418. *
  13419. * This routine is invoked to post rpi header templates to the
  13420. * HBA consistent with the SLI-4 interface spec. This routine
  13421. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13422. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13423. *
  13424. * Returns
  13425. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13426. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13427. **/
  13428. int
  13429. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13430. {
  13431. unsigned long rpi;
  13432. uint16_t max_rpi, rpi_limit;
  13433. uint16_t rpi_remaining, lrpi = 0;
  13434. struct lpfc_rpi_hdr *rpi_hdr;
  13435. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13436. rpi_limit = phba->sli4_hba.next_rpi;
  13437. /*
  13438. * Fetch the next logical rpi. Because this index is logical,
  13439. * the driver starts at 0 each time.
  13440. */
  13441. spin_lock_irq(&phba->hbalock);
  13442. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13443. if (rpi >= rpi_limit)
  13444. rpi = LPFC_RPI_ALLOC_ERROR;
  13445. else {
  13446. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13447. phba->sli4_hba.max_cfg_param.rpi_used++;
  13448. phba->sli4_hba.rpi_count++;
  13449. }
  13450. /*
  13451. * Don't try to allocate more rpi header regions if the device limit
  13452. * has been exhausted.
  13453. */
  13454. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13455. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13456. spin_unlock_irq(&phba->hbalock);
  13457. return rpi;
  13458. }
  13459. /*
  13460. * RPI header postings are not required for SLI4 ports capable of
  13461. * extents.
  13462. */
  13463. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13464. spin_unlock_irq(&phba->hbalock);
  13465. return rpi;
  13466. }
  13467. /*
  13468. * If the driver is running low on rpi resources, allocate another
  13469. * page now. Note that the next_rpi value is used because
  13470. * it represents how many are actually in use whereas max_rpi notes
  13471. * how many are supported max by the device.
  13472. */
  13473. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13474. spin_unlock_irq(&phba->hbalock);
  13475. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13476. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13477. if (!rpi_hdr) {
  13478. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13479. "2002 Error Could not grow rpi "
  13480. "count\n");
  13481. } else {
  13482. lrpi = rpi_hdr->start_rpi;
  13483. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13484. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13485. }
  13486. }
  13487. return rpi;
  13488. }
  13489. /**
  13490. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13491. * @phba: pointer to lpfc hba data structure.
  13492. *
  13493. * This routine is invoked to release an rpi to the pool of
  13494. * available rpis maintained by the driver.
  13495. **/
  13496. void
  13497. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13498. {
  13499. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13500. phba->sli4_hba.rpi_count--;
  13501. phba->sli4_hba.max_cfg_param.rpi_used--;
  13502. }
  13503. }
  13504. /**
  13505. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13506. * @phba: pointer to lpfc hba data structure.
  13507. *
  13508. * This routine is invoked to release an rpi to the pool of
  13509. * available rpis maintained by the driver.
  13510. **/
  13511. void
  13512. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13513. {
  13514. spin_lock_irq(&phba->hbalock);
  13515. __lpfc_sli4_free_rpi(phba, rpi);
  13516. spin_unlock_irq(&phba->hbalock);
  13517. }
  13518. /**
  13519. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13520. * @phba: pointer to lpfc hba data structure.
  13521. *
  13522. * This routine is invoked to remove the memory region that
  13523. * provided rpi via a bitmask.
  13524. **/
  13525. void
  13526. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13527. {
  13528. kfree(phba->sli4_hba.rpi_bmask);
  13529. kfree(phba->sli4_hba.rpi_ids);
  13530. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13531. }
  13532. /**
  13533. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  13534. * @phba: pointer to lpfc hba data structure.
  13535. *
  13536. * This routine is invoked to remove the memory region that
  13537. * provided rpi via a bitmask.
  13538. **/
  13539. int
  13540. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  13541. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  13542. {
  13543. LPFC_MBOXQ_t *mboxq;
  13544. struct lpfc_hba *phba = ndlp->phba;
  13545. int rc;
  13546. /* The port is notified of the header region via a mailbox command. */
  13547. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13548. if (!mboxq)
  13549. return -ENOMEM;
  13550. /* Post all rpi memory regions to the port. */
  13551. lpfc_resume_rpi(mboxq, ndlp);
  13552. if (cmpl) {
  13553. mboxq->mbox_cmpl = cmpl;
  13554. mboxq->context1 = arg;
  13555. mboxq->context2 = ndlp;
  13556. } else
  13557. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13558. mboxq->vport = ndlp->vport;
  13559. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13560. if (rc == MBX_NOT_FINISHED) {
  13561. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13562. "2010 Resume RPI Mailbox failed "
  13563. "status %d, mbxStatus x%x\n", rc,
  13564. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13565. mempool_free(mboxq, phba->mbox_mem_pool);
  13566. return -EIO;
  13567. }
  13568. return 0;
  13569. }
  13570. /**
  13571. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13572. * @vport: Pointer to the vport for which the vpi is being initialized
  13573. *
  13574. * This routine is invoked to activate a vpi with the port.
  13575. *
  13576. * Returns:
  13577. * 0 success
  13578. * -Evalue otherwise
  13579. **/
  13580. int
  13581. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13582. {
  13583. LPFC_MBOXQ_t *mboxq;
  13584. int rc = 0;
  13585. int retval = MBX_SUCCESS;
  13586. uint32_t mbox_tmo;
  13587. struct lpfc_hba *phba = vport->phba;
  13588. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13589. if (!mboxq)
  13590. return -ENOMEM;
  13591. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13592. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13593. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13594. if (rc != MBX_SUCCESS) {
  13595. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13596. "2022 INIT VPI Mailbox failed "
  13597. "status %d, mbxStatus x%x\n", rc,
  13598. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13599. retval = -EIO;
  13600. }
  13601. if (rc != MBX_TIMEOUT)
  13602. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13603. return retval;
  13604. }
  13605. /**
  13606. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13607. * @phba: pointer to lpfc hba data structure.
  13608. * @mboxq: Pointer to mailbox object.
  13609. *
  13610. * This routine is invoked to manually add a single FCF record. The caller
  13611. * must pass a completely initialized FCF_Record. This routine takes
  13612. * care of the nonembedded mailbox operations.
  13613. **/
  13614. static void
  13615. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13616. {
  13617. void *virt_addr;
  13618. union lpfc_sli4_cfg_shdr *shdr;
  13619. uint32_t shdr_status, shdr_add_status;
  13620. virt_addr = mboxq->sge_array->addr[0];
  13621. /* The IOCTL status is embedded in the mailbox subheader. */
  13622. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13623. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13624. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13625. if ((shdr_status || shdr_add_status) &&
  13626. (shdr_status != STATUS_FCF_IN_USE))
  13627. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13628. "2558 ADD_FCF_RECORD mailbox failed with "
  13629. "status x%x add_status x%x\n",
  13630. shdr_status, shdr_add_status);
  13631. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13632. }
  13633. /**
  13634. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13635. * @phba: pointer to lpfc hba data structure.
  13636. * @fcf_record: pointer to the initialized fcf record to add.
  13637. *
  13638. * This routine is invoked to manually add a single FCF record. The caller
  13639. * must pass a completely initialized FCF_Record. This routine takes
  13640. * care of the nonembedded mailbox operations.
  13641. **/
  13642. int
  13643. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13644. {
  13645. int rc = 0;
  13646. LPFC_MBOXQ_t *mboxq;
  13647. uint8_t *bytep;
  13648. void *virt_addr;
  13649. dma_addr_t phys_addr;
  13650. struct lpfc_mbx_sge sge;
  13651. uint32_t alloc_len, req_len;
  13652. uint32_t fcfindex;
  13653. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13654. if (!mboxq) {
  13655. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13656. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13657. return -ENOMEM;
  13658. }
  13659. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13660. sizeof(uint32_t);
  13661. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13662. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13663. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13664. req_len, LPFC_SLI4_MBX_NEMBED);
  13665. if (alloc_len < req_len) {
  13666. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13667. "2523 Allocated DMA memory size (x%x) is "
  13668. "less than the requested DMA memory "
  13669. "size (x%x)\n", alloc_len, req_len);
  13670. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13671. return -ENOMEM;
  13672. }
  13673. /*
  13674. * Get the first SGE entry from the non-embedded DMA memory. This
  13675. * routine only uses a single SGE.
  13676. */
  13677. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13678. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13679. virt_addr = mboxq->sge_array->addr[0];
  13680. /*
  13681. * Configure the FCF record for FCFI 0. This is the driver's
  13682. * hardcoded default and gets used in nonFIP mode.
  13683. */
  13684. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13685. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13686. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13687. /*
  13688. * Copy the fcf_index and the FCF Record Data. The data starts after
  13689. * the FCoE header plus word10. The data copy needs to be endian
  13690. * correct.
  13691. */
  13692. bytep += sizeof(uint32_t);
  13693. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13694. mboxq->vport = phba->pport;
  13695. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13696. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13697. if (rc == MBX_NOT_FINISHED) {
  13698. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13699. "2515 ADD_FCF_RECORD mailbox failed with "
  13700. "status 0x%x\n", rc);
  13701. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13702. rc = -EIO;
  13703. } else
  13704. rc = 0;
  13705. return rc;
  13706. }
  13707. /**
  13708. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13709. * @phba: pointer to lpfc hba data structure.
  13710. * @fcf_record: pointer to the fcf record to write the default data.
  13711. * @fcf_index: FCF table entry index.
  13712. *
  13713. * This routine is invoked to build the driver's default FCF record. The
  13714. * values used are hardcoded. This routine handles memory initialization.
  13715. *
  13716. **/
  13717. void
  13718. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13719. struct fcf_record *fcf_record,
  13720. uint16_t fcf_index)
  13721. {
  13722. memset(fcf_record, 0, sizeof(struct fcf_record));
  13723. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  13724. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  13725. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  13726. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  13727. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  13728. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  13729. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  13730. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  13731. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  13732. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  13733. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  13734. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  13735. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  13736. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  13737. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  13738. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  13739. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  13740. /* Set the VLAN bit map */
  13741. if (phba->valid_vlan) {
  13742. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  13743. = 1 << (phba->vlan_id % 8);
  13744. }
  13745. }
  13746. /**
  13747. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  13748. * @phba: pointer to lpfc hba data structure.
  13749. * @fcf_index: FCF table entry offset.
  13750. *
  13751. * This routine is invoked to scan the entire FCF table by reading FCF
  13752. * record and processing it one at a time starting from the @fcf_index
  13753. * for initial FCF discovery or fast FCF failover rediscovery.
  13754. *
  13755. * Return 0 if the mailbox command is submitted successfully, none 0
  13756. * otherwise.
  13757. **/
  13758. int
  13759. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13760. {
  13761. int rc = 0, error;
  13762. LPFC_MBOXQ_t *mboxq;
  13763. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  13764. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  13765. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13766. if (!mboxq) {
  13767. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13768. "2000 Failed to allocate mbox for "
  13769. "READ_FCF cmd\n");
  13770. error = -ENOMEM;
  13771. goto fail_fcf_scan;
  13772. }
  13773. /* Construct the read FCF record mailbox command */
  13774. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13775. if (rc) {
  13776. error = -EINVAL;
  13777. goto fail_fcf_scan;
  13778. }
  13779. /* Issue the mailbox command asynchronously */
  13780. mboxq->vport = phba->pport;
  13781. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  13782. spin_lock_irq(&phba->hbalock);
  13783. phba->hba_flag |= FCF_TS_INPROG;
  13784. spin_unlock_irq(&phba->hbalock);
  13785. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13786. if (rc == MBX_NOT_FINISHED)
  13787. error = -EIO;
  13788. else {
  13789. /* Reset eligible FCF count for new scan */
  13790. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  13791. phba->fcf.eligible_fcf_cnt = 0;
  13792. error = 0;
  13793. }
  13794. fail_fcf_scan:
  13795. if (error) {
  13796. if (mboxq)
  13797. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13798. /* FCF scan failed, clear FCF_TS_INPROG flag */
  13799. spin_lock_irq(&phba->hbalock);
  13800. phba->hba_flag &= ~FCF_TS_INPROG;
  13801. spin_unlock_irq(&phba->hbalock);
  13802. }
  13803. return error;
  13804. }
  13805. /**
  13806. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  13807. * @phba: pointer to lpfc hba data structure.
  13808. * @fcf_index: FCF table entry offset.
  13809. *
  13810. * This routine is invoked to read an FCF record indicated by @fcf_index
  13811. * and to use it for FLOGI roundrobin FCF failover.
  13812. *
  13813. * Return 0 if the mailbox command is submitted successfully, none 0
  13814. * otherwise.
  13815. **/
  13816. int
  13817. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13818. {
  13819. int rc = 0, error;
  13820. LPFC_MBOXQ_t *mboxq;
  13821. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13822. if (!mboxq) {
  13823. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13824. "2763 Failed to allocate mbox for "
  13825. "READ_FCF cmd\n");
  13826. error = -ENOMEM;
  13827. goto fail_fcf_read;
  13828. }
  13829. /* Construct the read FCF record mailbox command */
  13830. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13831. if (rc) {
  13832. error = -EINVAL;
  13833. goto fail_fcf_read;
  13834. }
  13835. /* Issue the mailbox command asynchronously */
  13836. mboxq->vport = phba->pport;
  13837. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  13838. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13839. if (rc == MBX_NOT_FINISHED)
  13840. error = -EIO;
  13841. else
  13842. error = 0;
  13843. fail_fcf_read:
  13844. if (error && mboxq)
  13845. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13846. return error;
  13847. }
  13848. /**
  13849. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  13850. * @phba: pointer to lpfc hba data structure.
  13851. * @fcf_index: FCF table entry offset.
  13852. *
  13853. * This routine is invoked to read an FCF record indicated by @fcf_index to
  13854. * determine whether it's eligible for FLOGI roundrobin failover list.
  13855. *
  13856. * Return 0 if the mailbox command is submitted successfully, none 0
  13857. * otherwise.
  13858. **/
  13859. int
  13860. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13861. {
  13862. int rc = 0, error;
  13863. LPFC_MBOXQ_t *mboxq;
  13864. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13865. if (!mboxq) {
  13866. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13867. "2758 Failed to allocate mbox for "
  13868. "READ_FCF cmd\n");
  13869. error = -ENOMEM;
  13870. goto fail_fcf_read;
  13871. }
  13872. /* Construct the read FCF record mailbox command */
  13873. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13874. if (rc) {
  13875. error = -EINVAL;
  13876. goto fail_fcf_read;
  13877. }
  13878. /* Issue the mailbox command asynchronously */
  13879. mboxq->vport = phba->pport;
  13880. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  13881. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13882. if (rc == MBX_NOT_FINISHED)
  13883. error = -EIO;
  13884. else
  13885. error = 0;
  13886. fail_fcf_read:
  13887. if (error && mboxq)
  13888. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13889. return error;
  13890. }
  13891. /**
  13892. * lpfc_check_next_fcf_pri
  13893. * phba pointer to the lpfc_hba struct for this port.
  13894. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  13895. * routine when the rr_bmask is empty. The FCF indecies are put into the
  13896. * rr_bmask based on their priority level. Starting from the highest priority
  13897. * to the lowest. The most likely FCF candidate will be in the highest
  13898. * priority group. When this routine is called it searches the fcf_pri list for
  13899. * next lowest priority group and repopulates the rr_bmask with only those
  13900. * fcf_indexes.
  13901. * returns:
  13902. * 1=success 0=failure
  13903. **/
  13904. int
  13905. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  13906. {
  13907. uint16_t next_fcf_pri;
  13908. uint16_t last_index;
  13909. struct lpfc_fcf_pri *fcf_pri;
  13910. int rc;
  13911. int ret = 0;
  13912. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  13913. LPFC_SLI4_FCF_TBL_INDX_MAX);
  13914. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13915. "3060 Last IDX %d\n", last_index);
  13916. if (list_empty(&phba->fcf.fcf_pri_list)) {
  13917. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13918. "3061 Last IDX %d\n", last_index);
  13919. return 0; /* Empty rr list */
  13920. }
  13921. next_fcf_pri = 0;
  13922. /*
  13923. * Clear the rr_bmask and set all of the bits that are at this
  13924. * priority.
  13925. */
  13926. memset(phba->fcf.fcf_rr_bmask, 0,
  13927. sizeof(*phba->fcf.fcf_rr_bmask));
  13928. spin_lock_irq(&phba->hbalock);
  13929. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13930. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  13931. continue;
  13932. /*
  13933. * the 1st priority that has not FLOGI failed
  13934. * will be the highest.
  13935. */
  13936. if (!next_fcf_pri)
  13937. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13938. spin_unlock_irq(&phba->hbalock);
  13939. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13940. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13941. fcf_pri->fcf_rec.fcf_index);
  13942. if (rc)
  13943. return 0;
  13944. }
  13945. spin_lock_irq(&phba->hbalock);
  13946. }
  13947. /*
  13948. * if next_fcf_pri was not set above and the list is not empty then
  13949. * we have failed flogis on all of them. So reset flogi failed
  13950. * and start at the begining.
  13951. */
  13952. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  13953. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13954. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  13955. /*
  13956. * the 1st priority that has not FLOGI failed
  13957. * will be the highest.
  13958. */
  13959. if (!next_fcf_pri)
  13960. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13961. spin_unlock_irq(&phba->hbalock);
  13962. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13963. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13964. fcf_pri->fcf_rec.fcf_index);
  13965. if (rc)
  13966. return 0;
  13967. }
  13968. spin_lock_irq(&phba->hbalock);
  13969. }
  13970. } else
  13971. ret = 1;
  13972. spin_unlock_irq(&phba->hbalock);
  13973. return ret;
  13974. }
  13975. /**
  13976. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  13977. * @phba: pointer to lpfc hba data structure.
  13978. *
  13979. * This routine is to get the next eligible FCF record index in a round
  13980. * robin fashion. If the next eligible FCF record index equals to the
  13981. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  13982. * shall be returned, otherwise, the next eligible FCF record's index
  13983. * shall be returned.
  13984. **/
  13985. uint16_t
  13986. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  13987. {
  13988. uint16_t next_fcf_index;
  13989. /* Search start from next bit of currently registered FCF index */
  13990. next_priority:
  13991. next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
  13992. LPFC_SLI4_FCF_TBL_INDX_MAX;
  13993. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13994. LPFC_SLI4_FCF_TBL_INDX_MAX,
  13995. next_fcf_index);
  13996. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  13997. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13998. /*
  13999. * If we have wrapped then we need to clear the bits that
  14000. * have been tested so that we can detect when we should
  14001. * change the priority level.
  14002. */
  14003. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14004. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  14005. }
  14006. /* Check roundrobin failover list empty condition */
  14007. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  14008. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  14009. /*
  14010. * If next fcf index is not found check if there are lower
  14011. * Priority level fcf's in the fcf_priority list.
  14012. * Set up the rr_bmask with all of the avaiable fcf bits
  14013. * at that level and continue the selection process.
  14014. */
  14015. if (lpfc_check_next_fcf_pri_level(phba))
  14016. goto next_priority;
  14017. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14018. "2844 No roundrobin failover FCF available\n");
  14019. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  14020. return LPFC_FCOE_FCF_NEXT_NONE;
  14021. else {
  14022. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14023. "3063 Only FCF available idx %d, flag %x\n",
  14024. next_fcf_index,
  14025. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  14026. return next_fcf_index;
  14027. }
  14028. }
  14029. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  14030. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  14031. LPFC_FCF_FLOGI_FAILED)
  14032. goto next_priority;
  14033. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14034. "2845 Get next roundrobin failover FCF (x%x)\n",
  14035. next_fcf_index);
  14036. return next_fcf_index;
  14037. }
  14038. /**
  14039. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  14040. * @phba: pointer to lpfc hba data structure.
  14041. *
  14042. * This routine sets the FCF record index in to the eligible bmask for
  14043. * roundrobin failover search. It checks to make sure that the index
  14044. * does not go beyond the range of the driver allocated bmask dimension
  14045. * before setting the bit.
  14046. *
  14047. * Returns 0 if the index bit successfully set, otherwise, it returns
  14048. * -EINVAL.
  14049. **/
  14050. int
  14051. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  14052. {
  14053. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14054. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14055. "2610 FCF (x%x) reached driver's book "
  14056. "keeping dimension:x%x\n",
  14057. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14058. return -EINVAL;
  14059. }
  14060. /* Set the eligible FCF record index bmask */
  14061. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14062. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14063. "2790 Set FCF (x%x) to roundrobin FCF failover "
  14064. "bmask\n", fcf_index);
  14065. return 0;
  14066. }
  14067. /**
  14068. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  14069. * @phba: pointer to lpfc hba data structure.
  14070. *
  14071. * This routine clears the FCF record index from the eligible bmask for
  14072. * roundrobin failover search. It checks to make sure that the index
  14073. * does not go beyond the range of the driver allocated bmask dimension
  14074. * before clearing the bit.
  14075. **/
  14076. void
  14077. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  14078. {
  14079. struct lpfc_fcf_pri *fcf_pri;
  14080. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14081. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14082. "2762 FCF (x%x) reached driver's book "
  14083. "keeping dimension:x%x\n",
  14084. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14085. return;
  14086. }
  14087. /* Clear the eligible FCF record index bmask */
  14088. spin_lock_irq(&phba->hbalock);
  14089. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14090. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  14091. list_del_init(&fcf_pri->list);
  14092. break;
  14093. }
  14094. }
  14095. spin_unlock_irq(&phba->hbalock);
  14096. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14097. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14098. "2791 Clear FCF (x%x) from roundrobin failover "
  14099. "bmask\n", fcf_index);
  14100. }
  14101. /**
  14102. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  14103. * @phba: pointer to lpfc hba data structure.
  14104. *
  14105. * This routine is the completion routine for the rediscover FCF table mailbox
  14106. * command. If the mailbox command returned failure, it will try to stop the
  14107. * FCF rediscover wait timer.
  14108. **/
  14109. void
  14110. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  14111. {
  14112. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14113. uint32_t shdr_status, shdr_add_status;
  14114. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14115. shdr_status = bf_get(lpfc_mbox_hdr_status,
  14116. &redisc_fcf->header.cfg_shdr.response);
  14117. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  14118. &redisc_fcf->header.cfg_shdr.response);
  14119. if (shdr_status || shdr_add_status) {
  14120. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14121. "2746 Requesting for FCF rediscovery failed "
  14122. "status x%x add_status x%x\n",
  14123. shdr_status, shdr_add_status);
  14124. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  14125. spin_lock_irq(&phba->hbalock);
  14126. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  14127. spin_unlock_irq(&phba->hbalock);
  14128. /*
  14129. * CVL event triggered FCF rediscover request failed,
  14130. * last resort to re-try current registered FCF entry.
  14131. */
  14132. lpfc_retry_pport_discovery(phba);
  14133. } else {
  14134. spin_lock_irq(&phba->hbalock);
  14135. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  14136. spin_unlock_irq(&phba->hbalock);
  14137. /*
  14138. * DEAD FCF event triggered FCF rediscover request
  14139. * failed, last resort to fail over as a link down
  14140. * to FCF registration.
  14141. */
  14142. lpfc_sli4_fcf_dead_failthrough(phba);
  14143. }
  14144. } else {
  14145. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14146. "2775 Start FCF rediscover quiescent timer\n");
  14147. /*
  14148. * Start FCF rediscovery wait timer for pending FCF
  14149. * before rescan FCF record table.
  14150. */
  14151. lpfc_fcf_redisc_wait_start_timer(phba);
  14152. }
  14153. mempool_free(mbox, phba->mbox_mem_pool);
  14154. }
  14155. /**
  14156. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14157. * @phba: pointer to lpfc hba data structure.
  14158. *
  14159. * This routine is invoked to request for rediscovery of the entire FCF table
  14160. * by the port.
  14161. **/
  14162. int
  14163. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14164. {
  14165. LPFC_MBOXQ_t *mbox;
  14166. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14167. int rc, length;
  14168. /* Cancel retry delay timers to all vports before FCF rediscover */
  14169. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14170. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14171. if (!mbox) {
  14172. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14173. "2745 Failed to allocate mbox for "
  14174. "requesting FCF rediscover.\n");
  14175. return -ENOMEM;
  14176. }
  14177. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14178. sizeof(struct lpfc_sli4_cfg_mhdr));
  14179. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14180. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14181. length, LPFC_SLI4_MBX_EMBED);
  14182. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14183. /* Set count to 0 for invalidating the entire FCF database */
  14184. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14185. /* Issue the mailbox command asynchronously */
  14186. mbox->vport = phba->pport;
  14187. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14188. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14189. if (rc == MBX_NOT_FINISHED) {
  14190. mempool_free(mbox, phba->mbox_mem_pool);
  14191. return -EIO;
  14192. }
  14193. return 0;
  14194. }
  14195. /**
  14196. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14197. * @phba: pointer to lpfc hba data structure.
  14198. *
  14199. * This function is the failover routine as a last resort to the FCF DEAD
  14200. * event when driver failed to perform fast FCF failover.
  14201. **/
  14202. void
  14203. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14204. {
  14205. uint32_t link_state;
  14206. /*
  14207. * Last resort as FCF DEAD event failover will treat this as
  14208. * a link down, but save the link state because we don't want
  14209. * it to be changed to Link Down unless it is already down.
  14210. */
  14211. link_state = phba->link_state;
  14212. lpfc_linkdown(phba);
  14213. phba->link_state = link_state;
  14214. /* Unregister FCF if no devices connected to it */
  14215. lpfc_unregister_unused_fcf(phba);
  14216. }
  14217. /**
  14218. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14219. * @phba: pointer to lpfc hba data structure.
  14220. * @rgn23_data: pointer to configure region 23 data.
  14221. *
  14222. * This function gets SLI3 port configure region 23 data through memory dump
  14223. * mailbox command. When it successfully retrieves data, the size of the data
  14224. * will be returned, otherwise, 0 will be returned.
  14225. **/
  14226. static uint32_t
  14227. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14228. {
  14229. LPFC_MBOXQ_t *pmb = NULL;
  14230. MAILBOX_t *mb;
  14231. uint32_t offset = 0;
  14232. int rc;
  14233. if (!rgn23_data)
  14234. return 0;
  14235. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14236. if (!pmb) {
  14237. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14238. "2600 failed to allocate mailbox memory\n");
  14239. return 0;
  14240. }
  14241. mb = &pmb->u.mb;
  14242. do {
  14243. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14244. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14245. if (rc != MBX_SUCCESS) {
  14246. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14247. "2601 failed to read config "
  14248. "region 23, rc 0x%x Status 0x%x\n",
  14249. rc, mb->mbxStatus);
  14250. mb->un.varDmp.word_cnt = 0;
  14251. }
  14252. /*
  14253. * dump mem may return a zero when finished or we got a
  14254. * mailbox error, either way we are done.
  14255. */
  14256. if (mb->un.varDmp.word_cnt == 0)
  14257. break;
  14258. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14259. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14260. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14261. rgn23_data + offset,
  14262. mb->un.varDmp.word_cnt);
  14263. offset += mb->un.varDmp.word_cnt;
  14264. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14265. mempool_free(pmb, phba->mbox_mem_pool);
  14266. return offset;
  14267. }
  14268. /**
  14269. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14270. * @phba: pointer to lpfc hba data structure.
  14271. * @rgn23_data: pointer to configure region 23 data.
  14272. *
  14273. * This function gets SLI4 port configure region 23 data through memory dump
  14274. * mailbox command. When it successfully retrieves data, the size of the data
  14275. * will be returned, otherwise, 0 will be returned.
  14276. **/
  14277. static uint32_t
  14278. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14279. {
  14280. LPFC_MBOXQ_t *mboxq = NULL;
  14281. struct lpfc_dmabuf *mp = NULL;
  14282. struct lpfc_mqe *mqe;
  14283. uint32_t data_length = 0;
  14284. int rc;
  14285. if (!rgn23_data)
  14286. return 0;
  14287. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14288. if (!mboxq) {
  14289. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14290. "3105 failed to allocate mailbox memory\n");
  14291. return 0;
  14292. }
  14293. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14294. goto out;
  14295. mqe = &mboxq->u.mqe;
  14296. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14297. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14298. if (rc)
  14299. goto out;
  14300. data_length = mqe->un.mb_words[5];
  14301. if (data_length == 0)
  14302. goto out;
  14303. if (data_length > DMP_RGN23_SIZE) {
  14304. data_length = 0;
  14305. goto out;
  14306. }
  14307. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14308. out:
  14309. mempool_free(mboxq, phba->mbox_mem_pool);
  14310. if (mp) {
  14311. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14312. kfree(mp);
  14313. }
  14314. return data_length;
  14315. }
  14316. /**
  14317. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14318. * @phba: pointer to lpfc hba data structure.
  14319. *
  14320. * This function read region 23 and parse TLV for port status to
  14321. * decide if the user disaled the port. If the TLV indicates the
  14322. * port is disabled, the hba_flag is set accordingly.
  14323. **/
  14324. void
  14325. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14326. {
  14327. uint8_t *rgn23_data = NULL;
  14328. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14329. uint32_t offset = 0;
  14330. /* Get adapter Region 23 data */
  14331. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14332. if (!rgn23_data)
  14333. goto out;
  14334. if (phba->sli_rev < LPFC_SLI_REV4)
  14335. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14336. else {
  14337. if_type = bf_get(lpfc_sli_intf_if_type,
  14338. &phba->sli4_hba.sli_intf);
  14339. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14340. goto out;
  14341. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14342. }
  14343. if (!data_size)
  14344. goto out;
  14345. /* Check the region signature first */
  14346. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14347. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14348. "2619 Config region 23 has bad signature\n");
  14349. goto out;
  14350. }
  14351. offset += 4;
  14352. /* Check the data structure version */
  14353. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14354. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14355. "2620 Config region 23 has bad version\n");
  14356. goto out;
  14357. }
  14358. offset += 4;
  14359. /* Parse TLV entries in the region */
  14360. while (offset < data_size) {
  14361. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14362. break;
  14363. /*
  14364. * If the TLV is not driver specific TLV or driver id is
  14365. * not linux driver id, skip the record.
  14366. */
  14367. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14368. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14369. (rgn23_data[offset + 3] != 0)) {
  14370. offset += rgn23_data[offset + 1] * 4 + 4;
  14371. continue;
  14372. }
  14373. /* Driver found a driver specific TLV in the config region */
  14374. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14375. offset += 4;
  14376. tlv_offset = 0;
  14377. /*
  14378. * Search for configured port state sub-TLV.
  14379. */
  14380. while ((offset < data_size) &&
  14381. (tlv_offset < sub_tlv_len)) {
  14382. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14383. offset += 4;
  14384. tlv_offset += 4;
  14385. break;
  14386. }
  14387. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14388. offset += rgn23_data[offset + 1] * 4 + 4;
  14389. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14390. continue;
  14391. }
  14392. /* This HBA contains PORT_STE configured */
  14393. if (!rgn23_data[offset + 2])
  14394. phba->hba_flag |= LINK_DISABLED;
  14395. goto out;
  14396. }
  14397. }
  14398. out:
  14399. kfree(rgn23_data);
  14400. return;
  14401. }
  14402. /**
  14403. * lpfc_wr_object - write an object to the firmware
  14404. * @phba: HBA structure that indicates port to create a queue on.
  14405. * @dmabuf_list: list of dmabufs to write to the port.
  14406. * @size: the total byte value of the objects to write to the port.
  14407. * @offset: the current offset to be used to start the transfer.
  14408. *
  14409. * This routine will create a wr_object mailbox command to send to the port.
  14410. * the mailbox command will be constructed using the dma buffers described in
  14411. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14412. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14413. * used to indicate the starting offset of the transfer and will also return
  14414. * the offset after the write object mailbox has completed. @size is used to
  14415. * determine the end of the object and whether the eof bit should be set.
  14416. *
  14417. * Return 0 is successful and offset will contain the the new offset to use
  14418. * for the next write.
  14419. * Return negative value for error cases.
  14420. **/
  14421. int
  14422. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14423. uint32_t size, uint32_t *offset)
  14424. {
  14425. struct lpfc_mbx_wr_object *wr_object;
  14426. LPFC_MBOXQ_t *mbox;
  14427. int rc = 0, i = 0;
  14428. uint32_t shdr_status, shdr_add_status;
  14429. uint32_t mbox_tmo;
  14430. union lpfc_sli4_cfg_shdr *shdr;
  14431. struct lpfc_dmabuf *dmabuf;
  14432. uint32_t written = 0;
  14433. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14434. if (!mbox)
  14435. return -ENOMEM;
  14436. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14437. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14438. sizeof(struct lpfc_mbx_wr_object) -
  14439. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14440. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14441. wr_object->u.request.write_offset = *offset;
  14442. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14443. wr_object->u.request.object_name[0] =
  14444. cpu_to_le32(wr_object->u.request.object_name[0]);
  14445. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14446. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14447. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14448. break;
  14449. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14450. wr_object->u.request.bde[i].addrHigh =
  14451. putPaddrHigh(dmabuf->phys);
  14452. if (written + SLI4_PAGE_SIZE >= size) {
  14453. wr_object->u.request.bde[i].tus.f.bdeSize =
  14454. (size - written);
  14455. written += (size - written);
  14456. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14457. } else {
  14458. wr_object->u.request.bde[i].tus.f.bdeSize =
  14459. SLI4_PAGE_SIZE;
  14460. written += SLI4_PAGE_SIZE;
  14461. }
  14462. i++;
  14463. }
  14464. wr_object->u.request.bde_count = i;
  14465. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14466. if (!phba->sli4_hba.intr_enable)
  14467. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14468. else {
  14469. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14470. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14471. }
  14472. /* The IOCTL status is embedded in the mailbox subheader. */
  14473. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14474. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14475. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14476. if (rc != MBX_TIMEOUT)
  14477. mempool_free(mbox, phba->mbox_mem_pool);
  14478. if (shdr_status || shdr_add_status || rc) {
  14479. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14480. "3025 Write Object mailbox failed with "
  14481. "status x%x add_status x%x, mbx status x%x\n",
  14482. shdr_status, shdr_add_status, rc);
  14483. rc = -ENXIO;
  14484. } else
  14485. *offset += wr_object->u.response.actual_write_length;
  14486. return rc;
  14487. }
  14488. /**
  14489. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14490. * @vport: pointer to vport data structure.
  14491. *
  14492. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14493. * and REG_VPI mailbox commands associated with the vport. This function
  14494. * is called when driver want to restart discovery of the vport due to
  14495. * a Clear Virtual Link event.
  14496. **/
  14497. void
  14498. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14499. {
  14500. struct lpfc_hba *phba = vport->phba;
  14501. LPFC_MBOXQ_t *mb, *nextmb;
  14502. struct lpfc_dmabuf *mp;
  14503. struct lpfc_nodelist *ndlp;
  14504. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14505. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14506. LIST_HEAD(mbox_cmd_list);
  14507. uint8_t restart_loop;
  14508. /* Clean up internally queued mailbox commands with the vport */
  14509. spin_lock_irq(&phba->hbalock);
  14510. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14511. if (mb->vport != vport)
  14512. continue;
  14513. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14514. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14515. continue;
  14516. list_del(&mb->list);
  14517. list_add_tail(&mb->list, &mbox_cmd_list);
  14518. }
  14519. /* Clean up active mailbox command with the vport */
  14520. mb = phba->sli.mbox_active;
  14521. if (mb && (mb->vport == vport)) {
  14522. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14523. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14524. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14525. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14526. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14527. /* Put reference count for delayed processing */
  14528. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14529. /* Unregister the RPI when mailbox complete */
  14530. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14531. }
  14532. }
  14533. /* Cleanup any mailbox completions which are not yet processed */
  14534. do {
  14535. restart_loop = 0;
  14536. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14537. /*
  14538. * If this mailox is already processed or it is
  14539. * for another vport ignore it.
  14540. */
  14541. if ((mb->vport != vport) ||
  14542. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14543. continue;
  14544. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14545. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14546. continue;
  14547. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14548. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14549. ndlp = (struct lpfc_nodelist *)mb->context2;
  14550. /* Unregister the RPI when mailbox complete */
  14551. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14552. restart_loop = 1;
  14553. spin_unlock_irq(&phba->hbalock);
  14554. spin_lock(shost->host_lock);
  14555. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14556. spin_unlock(shost->host_lock);
  14557. spin_lock_irq(&phba->hbalock);
  14558. break;
  14559. }
  14560. }
  14561. } while (restart_loop);
  14562. spin_unlock_irq(&phba->hbalock);
  14563. /* Release the cleaned-up mailbox commands */
  14564. while (!list_empty(&mbox_cmd_list)) {
  14565. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14566. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14567. mp = (struct lpfc_dmabuf *) (mb->context1);
  14568. if (mp) {
  14569. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14570. kfree(mp);
  14571. }
  14572. ndlp = (struct lpfc_nodelist *) mb->context2;
  14573. mb->context2 = NULL;
  14574. if (ndlp) {
  14575. spin_lock(shost->host_lock);
  14576. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14577. spin_unlock(shost->host_lock);
  14578. lpfc_nlp_put(ndlp);
  14579. }
  14580. }
  14581. mempool_free(mb, phba->mbox_mem_pool);
  14582. }
  14583. /* Release the ndlp with the cleaned-up active mailbox command */
  14584. if (act_mbx_ndlp) {
  14585. spin_lock(shost->host_lock);
  14586. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14587. spin_unlock(shost->host_lock);
  14588. lpfc_nlp_put(act_mbx_ndlp);
  14589. }
  14590. }
  14591. /**
  14592. * lpfc_drain_txq - Drain the txq
  14593. * @phba: Pointer to HBA context object.
  14594. *
  14595. * This function attempt to submit IOCBs on the txq
  14596. * to the adapter. For SLI4 adapters, the txq contains
  14597. * ELS IOCBs that have been deferred because the there
  14598. * are no SGLs. This congestion can occur with large
  14599. * vport counts during node discovery.
  14600. **/
  14601. uint32_t
  14602. lpfc_drain_txq(struct lpfc_hba *phba)
  14603. {
  14604. LIST_HEAD(completions);
  14605. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14606. struct lpfc_iocbq *piocbq = 0;
  14607. unsigned long iflags = 0;
  14608. char *fail_msg = NULL;
  14609. struct lpfc_sglq *sglq;
  14610. union lpfc_wqe wqe;
  14611. spin_lock_irqsave(&phba->hbalock, iflags);
  14612. if (pring->txq_cnt > pring->txq_max)
  14613. pring->txq_max = pring->txq_cnt;
  14614. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14615. while (pring->txq_cnt) {
  14616. spin_lock_irqsave(&phba->hbalock, iflags);
  14617. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14618. if (!piocbq) {
  14619. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14620. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14621. "2823 txq empty and txq_cnt is %d\n ",
  14622. pring->txq_cnt);
  14623. break;
  14624. }
  14625. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14626. if (!sglq) {
  14627. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14628. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14629. break;
  14630. }
  14631. /* The xri and iocb resources secured,
  14632. * attempt to issue request
  14633. */
  14634. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14635. piocbq->sli4_xritag = sglq->sli4_xritag;
  14636. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14637. fail_msg = "to convert bpl to sgl";
  14638. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14639. fail_msg = "to convert iocb to wqe";
  14640. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14641. fail_msg = " - Wq is full";
  14642. else
  14643. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14644. if (fail_msg) {
  14645. /* Failed means we can't issue and need to cancel */
  14646. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14647. "2822 IOCB failed %s iotag 0x%x "
  14648. "xri 0x%x\n",
  14649. fail_msg,
  14650. piocbq->iotag, piocbq->sli4_xritag);
  14651. list_add_tail(&piocbq->list, &completions);
  14652. }
  14653. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14654. }
  14655. /* Cancel all the IOCBs that cannot be issued */
  14656. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14657. IOERR_SLI_ABORTED);
  14658. return pring->txq_cnt;
  14659. }