lpfc_sli.c 488 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2011 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 IOCB_t *
  65. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  66. {
  67. return &iocbq->iocb;
  68. }
  69. /**
  70. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  71. * @q: The Work Queue to operate on.
  72. * @wqe: The work Queue Entry to put on the Work queue.
  73. *
  74. * This routine will copy the contents of @wqe to the next available entry on
  75. * the @q. This function will then ring the Work Queue Doorbell to signal the
  76. * HBA to start processing the Work Queue Entry. This function returns 0 if
  77. * successful. If no entries are available on @q then this function will return
  78. * -ENOMEM.
  79. * The caller is expected to hold the hbalock when calling this routine.
  80. **/
  81. static uint32_t
  82. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  83. {
  84. union lpfc_wqe *temp_wqe;
  85. struct lpfc_register doorbell;
  86. uint32_t host_index;
  87. /* sanity check on queue memory */
  88. if (unlikely(!q))
  89. return -ENOMEM;
  90. temp_wqe = q->qe[q->host_index].wqe;
  91. /* If the host has not yet processed the next entry then we are done */
  92. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  93. return -ENOMEM;
  94. /* set consumption flag every once in a while */
  95. if (!((q->host_index + 1) % q->entry_repost))
  96. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  97. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  98. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  99. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  100. /* Update the host index before invoking device */
  101. host_index = q->host_index;
  102. q->host_index = ((q->host_index + 1) % q->entry_count);
  103. /* Ring Doorbell */
  104. doorbell.word0 = 0;
  105. bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
  106. bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
  107. bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
  108. writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
  109. readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
  110. return 0;
  111. }
  112. /**
  113. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  114. * @q: The Work Queue to operate on.
  115. * @index: The index to advance the hba index to.
  116. *
  117. * This routine will update the HBA index of a queue to reflect consumption of
  118. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  119. * an entry the host calls this function to update the queue's internal
  120. * pointers. This routine returns the number of entries that were consumed by
  121. * the HBA.
  122. **/
  123. static uint32_t
  124. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  125. {
  126. uint32_t released = 0;
  127. /* sanity check on queue memory */
  128. if (unlikely(!q))
  129. return 0;
  130. if (q->hba_index == index)
  131. return 0;
  132. do {
  133. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  134. released++;
  135. } while (q->hba_index != index);
  136. return released;
  137. }
  138. /**
  139. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  140. * @q: The Mailbox Queue to operate on.
  141. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  142. *
  143. * This routine will copy the contents of @mqe to the next available entry on
  144. * the @q. This function will then ring the Work Queue Doorbell to signal the
  145. * HBA to start processing the Work Queue Entry. This function returns 0 if
  146. * successful. If no entries are available on @q then this function will return
  147. * -ENOMEM.
  148. * The caller is expected to hold the hbalock when calling this routine.
  149. **/
  150. static uint32_t
  151. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  152. {
  153. struct lpfc_mqe *temp_mqe;
  154. struct lpfc_register doorbell;
  155. uint32_t host_index;
  156. /* sanity check on queue memory */
  157. if (unlikely(!q))
  158. return -ENOMEM;
  159. temp_mqe = q->qe[q->host_index].mqe;
  160. /* If the host has not yet processed the next entry then we are done */
  161. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  162. return -ENOMEM;
  163. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  164. /* Save off the mailbox pointer for completion */
  165. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  166. /* Update the host index before invoking device */
  167. host_index = q->host_index;
  168. q->host_index = ((q->host_index + 1) % q->entry_count);
  169. /* Ring Doorbell */
  170. doorbell.word0 = 0;
  171. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  172. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  173. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  174. readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
  175. return 0;
  176. }
  177. /**
  178. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  179. * @q: The Mailbox Queue to operate on.
  180. *
  181. * This routine will update the HBA index of a queue to reflect consumption of
  182. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  183. * an entry the host calls this function to update the queue's internal
  184. * pointers. This routine returns the number of entries that were consumed by
  185. * the HBA.
  186. **/
  187. static uint32_t
  188. lpfc_sli4_mq_release(struct lpfc_queue *q)
  189. {
  190. /* sanity check on queue memory */
  191. if (unlikely(!q))
  192. return 0;
  193. /* Clear the mailbox pointer for completion */
  194. q->phba->mbox = NULL;
  195. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  196. return 1;
  197. }
  198. /**
  199. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  200. * @q: The Event Queue to get the first valid EQE from
  201. *
  202. * This routine will get the first valid Event Queue Entry from @q, update
  203. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  204. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  205. * processed, but not popped back to the HBA then this routine will return NULL.
  206. **/
  207. static struct lpfc_eqe *
  208. lpfc_sli4_eq_get(struct lpfc_queue *q)
  209. {
  210. struct lpfc_eqe *eqe;
  211. /* sanity check on queue memory */
  212. if (unlikely(!q))
  213. return NULL;
  214. eqe = q->qe[q->hba_index].eqe;
  215. /* If the next EQE is not valid then we are done */
  216. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  217. return NULL;
  218. /* If the host has not yet processed the next entry then we are done */
  219. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  220. return NULL;
  221. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  222. return eqe;
  223. }
  224. /**
  225. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  226. * @q: The Event Queue that the host has completed processing for.
  227. * @arm: Indicates whether the host wants to arms this CQ.
  228. *
  229. * This routine will mark all Event Queue Entries on @q, from the last
  230. * known completed entry to the last entry that was processed, as completed
  231. * by clearing the valid bit for each completion queue entry. Then it will
  232. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  233. * The internal host index in the @q will be updated by this routine to indicate
  234. * that the host has finished processing the entries. The @arm parameter
  235. * indicates that the queue should be rearmed when ringing the doorbell.
  236. *
  237. * This function will return the number of EQEs that were popped.
  238. **/
  239. uint32_t
  240. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  241. {
  242. uint32_t released = 0;
  243. struct lpfc_eqe *temp_eqe;
  244. struct lpfc_register doorbell;
  245. /* sanity check on queue memory */
  246. if (unlikely(!q))
  247. return 0;
  248. /* while there are valid entries */
  249. while (q->hba_index != q->host_index) {
  250. temp_eqe = q->qe[q->host_index].eqe;
  251. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  252. released++;
  253. q->host_index = ((q->host_index + 1) % q->entry_count);
  254. }
  255. if (unlikely(released == 0 && !arm))
  256. return 0;
  257. /* ring doorbell for number popped */
  258. doorbell.word0 = 0;
  259. if (arm) {
  260. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  261. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  262. }
  263. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  264. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  265. bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
  266. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  267. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  268. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  269. readl(q->phba->sli4_hba.EQCQDBregaddr);
  270. return released;
  271. }
  272. /**
  273. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  274. * @q: The Completion Queue to get the first valid CQE from
  275. *
  276. * This routine will get the first valid Completion Queue Entry from @q, update
  277. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  278. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  279. * processed, but not popped back to the HBA then this routine will return NULL.
  280. **/
  281. static struct lpfc_cqe *
  282. lpfc_sli4_cq_get(struct lpfc_queue *q)
  283. {
  284. struct lpfc_cqe *cqe;
  285. /* sanity check on queue memory */
  286. if (unlikely(!q))
  287. return NULL;
  288. /* If the next CQE is not valid then we are done */
  289. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  290. return NULL;
  291. /* If the host has not yet processed the next entry then we are done */
  292. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  293. return NULL;
  294. cqe = q->qe[q->hba_index].cqe;
  295. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  296. return cqe;
  297. }
  298. /**
  299. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  300. * @q: The Completion Queue that the host has completed processing for.
  301. * @arm: Indicates whether the host wants to arms this CQ.
  302. *
  303. * This routine will mark all Completion queue entries on @q, from the last
  304. * known completed entry to the last entry that was processed, as completed
  305. * by clearing the valid bit for each completion queue entry. Then it will
  306. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  307. * The internal host index in the @q will be updated by this routine to indicate
  308. * that the host has finished processing the entries. The @arm parameter
  309. * indicates that the queue should be rearmed when ringing the doorbell.
  310. *
  311. * This function will return the number of CQEs that were released.
  312. **/
  313. uint32_t
  314. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  315. {
  316. uint32_t released = 0;
  317. struct lpfc_cqe *temp_qe;
  318. struct lpfc_register doorbell;
  319. /* sanity check on queue memory */
  320. if (unlikely(!q))
  321. return 0;
  322. /* while there are valid entries */
  323. while (q->hba_index != q->host_index) {
  324. temp_qe = q->qe[q->host_index].cqe;
  325. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  326. released++;
  327. q->host_index = ((q->host_index + 1) % q->entry_count);
  328. }
  329. if (unlikely(released == 0 && !arm))
  330. return 0;
  331. /* ring doorbell for number popped */
  332. doorbell.word0 = 0;
  333. if (arm)
  334. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  335. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  336. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  337. bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
  338. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  339. return released;
  340. }
  341. /**
  342. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  343. * @q: The Header Receive Queue to operate on.
  344. * @wqe: The Receive Queue Entry to put on the Receive queue.
  345. *
  346. * This routine will copy the contents of @wqe to the next available entry on
  347. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  348. * HBA to start processing the Receive Queue Entry. This function returns the
  349. * index that the rqe was copied to if successful. If no entries are available
  350. * on @q then this function will return -ENOMEM.
  351. * The caller is expected to hold the hbalock when calling this routine.
  352. **/
  353. static int
  354. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  355. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  356. {
  357. struct lpfc_rqe *temp_hrqe;
  358. struct lpfc_rqe *temp_drqe;
  359. struct lpfc_register doorbell;
  360. int put_index = hq->host_index;
  361. /* sanity check on queue memory */
  362. if (unlikely(!hq) || unlikely(!dq))
  363. return -ENOMEM;
  364. temp_hrqe = hq->qe[hq->host_index].rqe;
  365. temp_drqe = dq->qe[dq->host_index].rqe;
  366. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  367. return -EINVAL;
  368. if (hq->host_index != dq->host_index)
  369. return -EINVAL;
  370. /* If the host has not yet processed the next entry then we are done */
  371. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  372. return -EBUSY;
  373. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  374. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  375. /* Update the host index to point to the next slot */
  376. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  377. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  378. /* Ring The Header Receive Queue Doorbell */
  379. if (!(hq->host_index % hq->entry_repost)) {
  380. doorbell.word0 = 0;
  381. bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
  382. hq->entry_repost);
  383. bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
  384. writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
  385. }
  386. return put_index;
  387. }
  388. /**
  389. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  390. * @q: The Header Receive Queue to operate on.
  391. *
  392. * This routine will update the HBA index of a queue to reflect consumption of
  393. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  394. * consumed an entry the host calls this function to update the queue's
  395. * internal pointers. This routine returns the number of entries that were
  396. * consumed by the HBA.
  397. **/
  398. static uint32_t
  399. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  400. {
  401. /* sanity check on queue memory */
  402. if (unlikely(!hq) || unlikely(!dq))
  403. return 0;
  404. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  405. return 0;
  406. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  407. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  408. return 1;
  409. }
  410. /**
  411. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  412. * @phba: Pointer to HBA context object.
  413. * @pring: Pointer to driver SLI ring object.
  414. *
  415. * This function returns pointer to next command iocb entry
  416. * in the command ring. The caller must hold hbalock to prevent
  417. * other threads consume the next command iocb.
  418. * SLI-2/SLI-3 provide different sized iocbs.
  419. **/
  420. static inline IOCB_t *
  421. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  422. {
  423. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  424. pring->cmdidx * phba->iocb_cmd_size);
  425. }
  426. /**
  427. * lpfc_resp_iocb - Get next response iocb entry in the ring
  428. * @phba: Pointer to HBA context object.
  429. * @pring: Pointer to driver SLI ring object.
  430. *
  431. * This function returns pointer to next response iocb entry
  432. * in the response ring. The caller must hold hbalock to make sure
  433. * that no other thread consume the next response iocb.
  434. * SLI-2/SLI-3 provide different sized iocbs.
  435. **/
  436. static inline IOCB_t *
  437. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  438. {
  439. return (IOCB_t *) (((char *) pring->rspringaddr) +
  440. pring->rspidx * phba->iocb_rsp_size);
  441. }
  442. /**
  443. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  444. * @phba: Pointer to HBA context object.
  445. *
  446. * This function is called with hbalock held. This function
  447. * allocates a new driver iocb object from the iocb pool. If the
  448. * allocation is successful, it returns pointer to the newly
  449. * allocated iocb object else it returns NULL.
  450. **/
  451. static struct lpfc_iocbq *
  452. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  453. {
  454. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  455. struct lpfc_iocbq * iocbq = NULL;
  456. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  457. if (iocbq)
  458. phba->iocb_cnt++;
  459. if (phba->iocb_cnt > phba->iocb_max)
  460. phba->iocb_max = phba->iocb_cnt;
  461. return iocbq;
  462. }
  463. /**
  464. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  465. * @phba: Pointer to HBA context object.
  466. * @xritag: XRI value.
  467. *
  468. * This function clears the sglq pointer from the array of acive
  469. * sglq's. The xritag that is passed in is used to index into the
  470. * array. Before the xritag can be used it needs to be adjusted
  471. * by subtracting the xribase.
  472. *
  473. * Returns sglq ponter = success, NULL = Failure.
  474. **/
  475. static struct lpfc_sglq *
  476. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  477. {
  478. struct lpfc_sglq *sglq;
  479. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  480. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  481. return sglq;
  482. }
  483. /**
  484. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  485. * @phba: Pointer to HBA context object.
  486. * @xritag: XRI value.
  487. *
  488. * This function returns the sglq pointer from the array of acive
  489. * sglq's. The xritag that is passed in is used to index into the
  490. * array. Before the xritag can be used it needs to be adjusted
  491. * by subtracting the xribase.
  492. *
  493. * Returns sglq ponter = success, NULL = Failure.
  494. **/
  495. struct lpfc_sglq *
  496. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  497. {
  498. struct lpfc_sglq *sglq;
  499. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  500. return sglq;
  501. }
  502. /**
  503. * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
  504. * @phba: Pointer to HBA context object.
  505. * @ndlp: nodelist pointer for this target.
  506. * @xritag: xri used in this exchange.
  507. * @rxid: Remote Exchange ID.
  508. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  509. *
  510. * This function is called with hbalock held.
  511. * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
  512. * rrq struct and adds it to the active_rrq_list.
  513. *
  514. * returns 0 for rrq slot for this xri
  515. * < 0 Were not able to get rrq mem or invalid parameter.
  516. **/
  517. static int
  518. __lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  519. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  520. {
  521. struct lpfc_node_rrq *rrq;
  522. int empty;
  523. uint32_t did = 0;
  524. if (!ndlp)
  525. return -EINVAL;
  526. if (!phba->cfg_enable_rrq)
  527. return -EINVAL;
  528. if (phba->pport->load_flag & FC_UNLOADING) {
  529. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  530. goto out;
  531. }
  532. did = ndlp->nlp_DID;
  533. /*
  534. * set the active bit even if there is no mem available.
  535. */
  536. if (NLP_CHK_FREE_REQ(ndlp))
  537. goto out;
  538. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  539. goto out;
  540. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  541. goto out;
  542. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  543. if (rrq) {
  544. rrq->send_rrq = send_rrq;
  545. rrq->xritag = xritag;
  546. rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
  547. rrq->ndlp = ndlp;
  548. rrq->nlp_DID = ndlp->nlp_DID;
  549. rrq->vport = ndlp->vport;
  550. rrq->rxid = rxid;
  551. empty = list_empty(&phba->active_rrq_list);
  552. rrq->send_rrq = send_rrq;
  553. list_add_tail(&rrq->list, &phba->active_rrq_list);
  554. if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
  555. phba->hba_flag |= HBA_RRQ_ACTIVE;
  556. if (empty)
  557. lpfc_worker_wake_up(phba);
  558. }
  559. return 0;
  560. }
  561. out:
  562. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  563. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  564. " DID:0x%x Send:%d\n",
  565. xritag, rxid, did, send_rrq);
  566. return -EINVAL;
  567. }
  568. /**
  569. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  570. * @phba: Pointer to HBA context object.
  571. * @xritag: xri used in this exchange.
  572. * @rrq: The RRQ to be cleared.
  573. *
  574. **/
  575. void
  576. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  577. uint16_t xritag,
  578. struct lpfc_node_rrq *rrq)
  579. {
  580. struct lpfc_nodelist *ndlp = NULL;
  581. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  582. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  583. /* The target DID could have been swapped (cable swap)
  584. * we should use the ndlp from the findnode if it is
  585. * available.
  586. */
  587. if ((!ndlp) && rrq->ndlp)
  588. ndlp = rrq->ndlp;
  589. if (!ndlp)
  590. goto out;
  591. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  592. rrq->send_rrq = 0;
  593. rrq->xritag = 0;
  594. rrq->rrq_stop_time = 0;
  595. }
  596. out:
  597. mempool_free(rrq, phba->rrq_pool);
  598. }
  599. /**
  600. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  601. * @phba: Pointer to HBA context object.
  602. *
  603. * This function is called with hbalock held. This function
  604. * Checks if stop_time (ratov from setting rrq active) has
  605. * been reached, if it has and the send_rrq flag is set then
  606. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  607. * then it will just call the routine to clear the rrq and
  608. * free the rrq resource.
  609. * The timer is set to the next rrq that is going to expire before
  610. * leaving the routine.
  611. *
  612. **/
  613. void
  614. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  615. {
  616. struct lpfc_node_rrq *rrq;
  617. struct lpfc_node_rrq *nextrrq;
  618. unsigned long next_time;
  619. unsigned long iflags;
  620. LIST_HEAD(send_rrq);
  621. spin_lock_irqsave(&phba->hbalock, iflags);
  622. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  623. next_time = jiffies + HZ * (phba->fc_ratov + 1);
  624. list_for_each_entry_safe(rrq, nextrrq,
  625. &phba->active_rrq_list, list) {
  626. if (time_after(jiffies, rrq->rrq_stop_time))
  627. list_move(&rrq->list, &send_rrq);
  628. else if (time_before(rrq->rrq_stop_time, next_time))
  629. next_time = rrq->rrq_stop_time;
  630. }
  631. spin_unlock_irqrestore(&phba->hbalock, iflags);
  632. if (!list_empty(&phba->active_rrq_list))
  633. mod_timer(&phba->rrq_tmr, next_time);
  634. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  635. list_del(&rrq->list);
  636. if (!rrq->send_rrq)
  637. /* this call will free the rrq */
  638. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  639. else if (lpfc_send_rrq(phba, rrq)) {
  640. /* if we send the rrq then the completion handler
  641. * will clear the bit in the xribitmap.
  642. */
  643. lpfc_clr_rrq_active(phba, rrq->xritag,
  644. rrq);
  645. }
  646. }
  647. }
  648. /**
  649. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  650. * @vport: Pointer to vport context object.
  651. * @xri: The xri used in the exchange.
  652. * @did: The targets DID for this exchange.
  653. *
  654. * returns NULL = rrq not found in the phba->active_rrq_list.
  655. * rrq = rrq for this xri and target.
  656. **/
  657. struct lpfc_node_rrq *
  658. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  659. {
  660. struct lpfc_hba *phba = vport->phba;
  661. struct lpfc_node_rrq *rrq;
  662. struct lpfc_node_rrq *nextrrq;
  663. unsigned long iflags;
  664. if (phba->sli_rev != LPFC_SLI_REV4)
  665. return NULL;
  666. spin_lock_irqsave(&phba->hbalock, iflags);
  667. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  668. if (rrq->vport == vport && rrq->xritag == xri &&
  669. rrq->nlp_DID == did){
  670. list_del(&rrq->list);
  671. spin_unlock_irqrestore(&phba->hbalock, iflags);
  672. return rrq;
  673. }
  674. }
  675. spin_unlock_irqrestore(&phba->hbalock, iflags);
  676. return NULL;
  677. }
  678. /**
  679. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  680. * @vport: Pointer to vport context object.
  681. * @ndlp: Pointer to the lpfc_node_list structure.
  682. * If ndlp is NULL Remove all active RRQs for this vport from the
  683. * phba->active_rrq_list and clear the rrq.
  684. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  685. **/
  686. void
  687. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  688. {
  689. struct lpfc_hba *phba = vport->phba;
  690. struct lpfc_node_rrq *rrq;
  691. struct lpfc_node_rrq *nextrrq;
  692. unsigned long iflags;
  693. LIST_HEAD(rrq_list);
  694. if (phba->sli_rev != LPFC_SLI_REV4)
  695. return;
  696. if (!ndlp) {
  697. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  698. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  699. }
  700. spin_lock_irqsave(&phba->hbalock, iflags);
  701. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  702. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  703. list_move(&rrq->list, &rrq_list);
  704. spin_unlock_irqrestore(&phba->hbalock, iflags);
  705. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  706. list_del(&rrq->list);
  707. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  708. }
  709. }
  710. /**
  711. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  712. * @phba: Pointer to HBA context object.
  713. *
  714. * Remove all rrqs from the phba->active_rrq_list and free them by
  715. * calling __lpfc_clr_active_rrq
  716. *
  717. **/
  718. void
  719. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  720. {
  721. struct lpfc_node_rrq *rrq;
  722. struct lpfc_node_rrq *nextrrq;
  723. unsigned long next_time;
  724. unsigned long iflags;
  725. LIST_HEAD(rrq_list);
  726. if (phba->sli_rev != LPFC_SLI_REV4)
  727. return;
  728. spin_lock_irqsave(&phba->hbalock, iflags);
  729. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  730. next_time = jiffies + HZ * (phba->fc_ratov * 2);
  731. list_splice_init(&phba->active_rrq_list, &rrq_list);
  732. spin_unlock_irqrestore(&phba->hbalock, iflags);
  733. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  734. list_del(&rrq->list);
  735. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  736. }
  737. if (!list_empty(&phba->active_rrq_list))
  738. mod_timer(&phba->rrq_tmr, next_time);
  739. }
  740. /**
  741. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  742. * @phba: Pointer to HBA context object.
  743. * @ndlp: Targets nodelist pointer for this exchange.
  744. * @xritag the xri in the bitmap to test.
  745. *
  746. * This function is called with hbalock held. This function
  747. * returns 0 = rrq not active for this xri
  748. * 1 = rrq is valid for this xri.
  749. **/
  750. int
  751. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  752. uint16_t xritag)
  753. {
  754. if (!ndlp)
  755. return 0;
  756. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  757. return 1;
  758. else
  759. return 0;
  760. }
  761. /**
  762. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  763. * @phba: Pointer to HBA context object.
  764. * @ndlp: nodelist pointer for this target.
  765. * @xritag: xri used in this exchange.
  766. * @rxid: Remote Exchange ID.
  767. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  768. *
  769. * This function takes the hbalock.
  770. * The active bit is always set in the active rrq xri_bitmap even
  771. * if there is no slot avaiable for the other rrq information.
  772. *
  773. * returns 0 rrq actived for this xri
  774. * < 0 No memory or invalid ndlp.
  775. **/
  776. int
  777. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  778. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  779. {
  780. int ret;
  781. unsigned long iflags;
  782. spin_lock_irqsave(&phba->hbalock, iflags);
  783. ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
  784. spin_unlock_irqrestore(&phba->hbalock, iflags);
  785. return ret;
  786. }
  787. /**
  788. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  789. * @phba: Pointer to HBA context object.
  790. * @piocb: Pointer to the iocbq.
  791. *
  792. * This function is called with hbalock held. This function
  793. * gets a new driver sglq object from the sglq list. If the
  794. * list is not empty then it is successful, it returns pointer to the newly
  795. * allocated sglq object else it returns NULL.
  796. **/
  797. static struct lpfc_sglq *
  798. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  799. {
  800. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  801. struct lpfc_sglq *sglq = NULL;
  802. struct lpfc_sglq *start_sglq = NULL;
  803. struct lpfc_scsi_buf *lpfc_cmd;
  804. struct lpfc_nodelist *ndlp;
  805. int found = 0;
  806. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  807. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  808. ndlp = lpfc_cmd->rdata->pnode;
  809. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  810. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  811. ndlp = piocbq->context_un.ndlp;
  812. else
  813. ndlp = piocbq->context1;
  814. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  815. start_sglq = sglq;
  816. while (!found) {
  817. if (!sglq)
  818. return NULL;
  819. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
  820. /* This xri has an rrq outstanding for this DID.
  821. * put it back in the list and get another xri.
  822. */
  823. list_add_tail(&sglq->list, lpfc_sgl_list);
  824. sglq = NULL;
  825. list_remove_head(lpfc_sgl_list, sglq,
  826. struct lpfc_sglq, list);
  827. if (sglq == start_sglq) {
  828. sglq = NULL;
  829. break;
  830. } else
  831. continue;
  832. }
  833. sglq->ndlp = ndlp;
  834. found = 1;
  835. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  836. sglq->state = SGL_ALLOCATED;
  837. }
  838. return sglq;
  839. }
  840. /**
  841. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  842. * @phba: Pointer to HBA context object.
  843. *
  844. * This function is called with no lock held. This function
  845. * allocates a new driver iocb object from the iocb pool. If the
  846. * allocation is successful, it returns pointer to the newly
  847. * allocated iocb object else it returns NULL.
  848. **/
  849. struct lpfc_iocbq *
  850. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  851. {
  852. struct lpfc_iocbq * iocbq = NULL;
  853. unsigned long iflags;
  854. spin_lock_irqsave(&phba->hbalock, iflags);
  855. iocbq = __lpfc_sli_get_iocbq(phba);
  856. spin_unlock_irqrestore(&phba->hbalock, iflags);
  857. return iocbq;
  858. }
  859. /**
  860. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  861. * @phba: Pointer to HBA context object.
  862. * @iocbq: Pointer to driver iocb object.
  863. *
  864. * This function is called with hbalock held to release driver
  865. * iocb object to the iocb pool. The iotag in the iocb object
  866. * does not change for each use of the iocb object. This function
  867. * clears all other fields of the iocb object when it is freed.
  868. * The sqlq structure that holds the xritag and phys and virtual
  869. * mappings for the scatter gather list is retrieved from the
  870. * active array of sglq. The get of the sglq pointer also clears
  871. * the entry in the array. If the status of the IO indiactes that
  872. * this IO was aborted then the sglq entry it put on the
  873. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  874. * IO has good status or fails for any other reason then the sglq
  875. * entry is added to the free list (lpfc_sgl_list).
  876. **/
  877. static void
  878. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  879. {
  880. struct lpfc_sglq *sglq;
  881. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  882. unsigned long iflag = 0;
  883. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  884. if (iocbq->sli4_xritag == NO_XRI)
  885. sglq = NULL;
  886. else
  887. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  888. if (sglq) {
  889. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  890. (sglq->state != SGL_XRI_ABORTED)) {
  891. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  892. iflag);
  893. list_add(&sglq->list,
  894. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  895. spin_unlock_irqrestore(
  896. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  897. } else {
  898. sglq->state = SGL_FREED;
  899. sglq->ndlp = NULL;
  900. list_add_tail(&sglq->list,
  901. &phba->sli4_hba.lpfc_sgl_list);
  902. /* Check if TXQ queue needs to be serviced */
  903. if (pring->txq_cnt)
  904. lpfc_worker_wake_up(phba);
  905. }
  906. }
  907. /*
  908. * Clean all volatile data fields, preserve iotag and node struct.
  909. */
  910. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  911. iocbq->sli4_lxritag = NO_XRI;
  912. iocbq->sli4_xritag = NO_XRI;
  913. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  914. }
  915. /**
  916. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  917. * @phba: Pointer to HBA context object.
  918. * @iocbq: Pointer to driver iocb object.
  919. *
  920. * This function is called with hbalock held to release driver
  921. * iocb object to the iocb pool. The iotag in the iocb object
  922. * does not change for each use of the iocb object. This function
  923. * clears all other fields of the iocb object when it is freed.
  924. **/
  925. static void
  926. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  927. {
  928. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  929. /*
  930. * Clean all volatile data fields, preserve iotag and node struct.
  931. */
  932. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  933. iocbq->sli4_xritag = NO_XRI;
  934. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  935. }
  936. /**
  937. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  938. * @phba: Pointer to HBA context object.
  939. * @iocbq: Pointer to driver iocb object.
  940. *
  941. * This function is called with hbalock held to release driver
  942. * iocb object to the iocb pool. The iotag in the iocb object
  943. * does not change for each use of the iocb object. This function
  944. * clears all other fields of the iocb object when it is freed.
  945. **/
  946. static void
  947. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  948. {
  949. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  950. phba->iocb_cnt--;
  951. }
  952. /**
  953. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  954. * @phba: Pointer to HBA context object.
  955. * @iocbq: Pointer to driver iocb object.
  956. *
  957. * This function is called with no lock held to release the iocb to
  958. * iocb pool.
  959. **/
  960. void
  961. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  962. {
  963. unsigned long iflags;
  964. /*
  965. * Clean all volatile data fields, preserve iotag and node struct.
  966. */
  967. spin_lock_irqsave(&phba->hbalock, iflags);
  968. __lpfc_sli_release_iocbq(phba, iocbq);
  969. spin_unlock_irqrestore(&phba->hbalock, iflags);
  970. }
  971. /**
  972. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  973. * @phba: Pointer to HBA context object.
  974. * @iocblist: List of IOCBs.
  975. * @ulpstatus: ULP status in IOCB command field.
  976. * @ulpWord4: ULP word-4 in IOCB command field.
  977. *
  978. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  979. * on the list by invoking the complete callback function associated with the
  980. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  981. * fields.
  982. **/
  983. void
  984. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  985. uint32_t ulpstatus, uint32_t ulpWord4)
  986. {
  987. struct lpfc_iocbq *piocb;
  988. while (!list_empty(iocblist)) {
  989. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  990. if (!piocb->iocb_cmpl)
  991. lpfc_sli_release_iocbq(phba, piocb);
  992. else {
  993. piocb->iocb.ulpStatus = ulpstatus;
  994. piocb->iocb.un.ulpWord[4] = ulpWord4;
  995. (piocb->iocb_cmpl) (phba, piocb, piocb);
  996. }
  997. }
  998. return;
  999. }
  1000. /**
  1001. * lpfc_sli_iocb_cmd_type - Get the iocb type
  1002. * @iocb_cmnd: iocb command code.
  1003. *
  1004. * This function is called by ring event handler function to get the iocb type.
  1005. * This function translates the iocb command to an iocb command type used to
  1006. * decide the final disposition of each completed IOCB.
  1007. * The function returns
  1008. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  1009. * LPFC_SOL_IOCB if it is a solicited iocb completion
  1010. * LPFC_ABORT_IOCB if it is an abort iocb
  1011. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  1012. *
  1013. * The caller is not required to hold any lock.
  1014. **/
  1015. static lpfc_iocb_type
  1016. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1017. {
  1018. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1019. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1020. return 0;
  1021. switch (iocb_cmnd) {
  1022. case CMD_XMIT_SEQUENCE_CR:
  1023. case CMD_XMIT_SEQUENCE_CX:
  1024. case CMD_XMIT_BCAST_CN:
  1025. case CMD_XMIT_BCAST_CX:
  1026. case CMD_ELS_REQUEST_CR:
  1027. case CMD_ELS_REQUEST_CX:
  1028. case CMD_CREATE_XRI_CR:
  1029. case CMD_CREATE_XRI_CX:
  1030. case CMD_GET_RPI_CN:
  1031. case CMD_XMIT_ELS_RSP_CX:
  1032. case CMD_GET_RPI_CR:
  1033. case CMD_FCP_IWRITE_CR:
  1034. case CMD_FCP_IWRITE_CX:
  1035. case CMD_FCP_IREAD_CR:
  1036. case CMD_FCP_IREAD_CX:
  1037. case CMD_FCP_ICMND_CR:
  1038. case CMD_FCP_ICMND_CX:
  1039. case CMD_FCP_TSEND_CX:
  1040. case CMD_FCP_TRSP_CX:
  1041. case CMD_FCP_TRECEIVE_CX:
  1042. case CMD_FCP_AUTO_TRSP_CX:
  1043. case CMD_ADAPTER_MSG:
  1044. case CMD_ADAPTER_DUMP:
  1045. case CMD_XMIT_SEQUENCE64_CR:
  1046. case CMD_XMIT_SEQUENCE64_CX:
  1047. case CMD_XMIT_BCAST64_CN:
  1048. case CMD_XMIT_BCAST64_CX:
  1049. case CMD_ELS_REQUEST64_CR:
  1050. case CMD_ELS_REQUEST64_CX:
  1051. case CMD_FCP_IWRITE64_CR:
  1052. case CMD_FCP_IWRITE64_CX:
  1053. case CMD_FCP_IREAD64_CR:
  1054. case CMD_FCP_IREAD64_CX:
  1055. case CMD_FCP_ICMND64_CR:
  1056. case CMD_FCP_ICMND64_CX:
  1057. case CMD_FCP_TSEND64_CX:
  1058. case CMD_FCP_TRSP64_CX:
  1059. case CMD_FCP_TRECEIVE64_CX:
  1060. case CMD_GEN_REQUEST64_CR:
  1061. case CMD_GEN_REQUEST64_CX:
  1062. case CMD_XMIT_ELS_RSP64_CX:
  1063. case DSSCMD_IWRITE64_CR:
  1064. case DSSCMD_IWRITE64_CX:
  1065. case DSSCMD_IREAD64_CR:
  1066. case DSSCMD_IREAD64_CX:
  1067. type = LPFC_SOL_IOCB;
  1068. break;
  1069. case CMD_ABORT_XRI_CN:
  1070. case CMD_ABORT_XRI_CX:
  1071. case CMD_CLOSE_XRI_CN:
  1072. case CMD_CLOSE_XRI_CX:
  1073. case CMD_XRI_ABORTED_CX:
  1074. case CMD_ABORT_MXRI64_CN:
  1075. case CMD_XMIT_BLS_RSP64_CX:
  1076. type = LPFC_ABORT_IOCB;
  1077. break;
  1078. case CMD_RCV_SEQUENCE_CX:
  1079. case CMD_RCV_ELS_REQ_CX:
  1080. case CMD_RCV_SEQUENCE64_CX:
  1081. case CMD_RCV_ELS_REQ64_CX:
  1082. case CMD_ASYNC_STATUS:
  1083. case CMD_IOCB_RCV_SEQ64_CX:
  1084. case CMD_IOCB_RCV_ELS64_CX:
  1085. case CMD_IOCB_RCV_CONT64_CX:
  1086. case CMD_IOCB_RET_XRI64_CX:
  1087. type = LPFC_UNSOL_IOCB;
  1088. break;
  1089. case CMD_IOCB_XMIT_MSEQ64_CR:
  1090. case CMD_IOCB_XMIT_MSEQ64_CX:
  1091. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1092. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1093. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1094. case CMD_IOCB_ABORT_EXTENDED_CN:
  1095. case CMD_IOCB_RET_HBQE64_CN:
  1096. case CMD_IOCB_FCP_IBIDIR64_CR:
  1097. case CMD_IOCB_FCP_IBIDIR64_CX:
  1098. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1099. case CMD_IOCB_LOGENTRY_CN:
  1100. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1101. printk("%s - Unhandled SLI-3 Command x%x\n",
  1102. __func__, iocb_cmnd);
  1103. type = LPFC_UNKNOWN_IOCB;
  1104. break;
  1105. default:
  1106. type = LPFC_UNKNOWN_IOCB;
  1107. break;
  1108. }
  1109. return type;
  1110. }
  1111. /**
  1112. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1113. * @phba: Pointer to HBA context object.
  1114. *
  1115. * This function is called from SLI initialization code
  1116. * to configure every ring of the HBA's SLI interface. The
  1117. * caller is not required to hold any lock. This function issues
  1118. * a config_ring mailbox command for each ring.
  1119. * This function returns zero if successful else returns a negative
  1120. * error code.
  1121. **/
  1122. static int
  1123. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1124. {
  1125. struct lpfc_sli *psli = &phba->sli;
  1126. LPFC_MBOXQ_t *pmb;
  1127. MAILBOX_t *pmbox;
  1128. int i, rc, ret = 0;
  1129. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1130. if (!pmb)
  1131. return -ENOMEM;
  1132. pmbox = &pmb->u.mb;
  1133. phba->link_state = LPFC_INIT_MBX_CMDS;
  1134. for (i = 0; i < psli->num_rings; i++) {
  1135. lpfc_config_ring(phba, i, pmb);
  1136. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1137. if (rc != MBX_SUCCESS) {
  1138. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1139. "0446 Adapter failed to init (%d), "
  1140. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1141. "ring %d\n",
  1142. rc, pmbox->mbxCommand,
  1143. pmbox->mbxStatus, i);
  1144. phba->link_state = LPFC_HBA_ERROR;
  1145. ret = -ENXIO;
  1146. break;
  1147. }
  1148. }
  1149. mempool_free(pmb, phba->mbox_mem_pool);
  1150. return ret;
  1151. }
  1152. /**
  1153. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1154. * @phba: Pointer to HBA context object.
  1155. * @pring: Pointer to driver SLI ring object.
  1156. * @piocb: Pointer to the driver iocb object.
  1157. *
  1158. * This function is called with hbalock held. The function adds the
  1159. * new iocb to txcmplq of the given ring. This function always returns
  1160. * 0. If this function is called for ELS ring, this function checks if
  1161. * there is a vport associated with the ELS command. This function also
  1162. * starts els_tmofunc timer if this is an ELS command.
  1163. **/
  1164. static int
  1165. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1166. struct lpfc_iocbq *piocb)
  1167. {
  1168. list_add_tail(&piocb->list, &pring->txcmplq);
  1169. piocb->iocb_flag |= LPFC_IO_ON_Q;
  1170. pring->txcmplq_cnt++;
  1171. if (pring->txcmplq_cnt > pring->txcmplq_max)
  1172. pring->txcmplq_max = pring->txcmplq_cnt;
  1173. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1174. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1175. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1176. if (!piocb->vport)
  1177. BUG();
  1178. else
  1179. mod_timer(&piocb->vport->els_tmofunc,
  1180. jiffies + HZ * (phba->fc_ratov << 1));
  1181. }
  1182. return 0;
  1183. }
  1184. /**
  1185. * lpfc_sli_ringtx_get - Get first element of the txq
  1186. * @phba: Pointer to HBA context object.
  1187. * @pring: Pointer to driver SLI ring object.
  1188. *
  1189. * This function is called with hbalock held to get next
  1190. * iocb in txq of the given ring. If there is any iocb in
  1191. * the txq, the function returns first iocb in the list after
  1192. * removing the iocb from the list, else it returns NULL.
  1193. **/
  1194. struct lpfc_iocbq *
  1195. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1196. {
  1197. struct lpfc_iocbq *cmd_iocb;
  1198. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1199. if (cmd_iocb != NULL)
  1200. pring->txq_cnt--;
  1201. return cmd_iocb;
  1202. }
  1203. /**
  1204. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1205. * @phba: Pointer to HBA context object.
  1206. * @pring: Pointer to driver SLI ring object.
  1207. *
  1208. * This function is called with hbalock held and the caller must post the
  1209. * iocb without releasing the lock. If the caller releases the lock,
  1210. * iocb slot returned by the function is not guaranteed to be available.
  1211. * The function returns pointer to the next available iocb slot if there
  1212. * is available slot in the ring, else it returns NULL.
  1213. * If the get index of the ring is ahead of the put index, the function
  1214. * will post an error attention event to the worker thread to take the
  1215. * HBA to offline state.
  1216. **/
  1217. static IOCB_t *
  1218. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1219. {
  1220. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1221. uint32_t max_cmd_idx = pring->numCiocb;
  1222. if ((pring->next_cmdidx == pring->cmdidx) &&
  1223. (++pring->next_cmdidx >= max_cmd_idx))
  1224. pring->next_cmdidx = 0;
  1225. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  1226. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1227. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  1228. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1229. "0315 Ring %d issue: portCmdGet %d "
  1230. "is bigger than cmd ring %d\n",
  1231. pring->ringno,
  1232. pring->local_getidx, max_cmd_idx);
  1233. phba->link_state = LPFC_HBA_ERROR;
  1234. /*
  1235. * All error attention handlers are posted to
  1236. * worker thread
  1237. */
  1238. phba->work_ha |= HA_ERATT;
  1239. phba->work_hs = HS_FFER3;
  1240. lpfc_worker_wake_up(phba);
  1241. return NULL;
  1242. }
  1243. if (pring->local_getidx == pring->next_cmdidx)
  1244. return NULL;
  1245. }
  1246. return lpfc_cmd_iocb(phba, pring);
  1247. }
  1248. /**
  1249. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1250. * @phba: Pointer to HBA context object.
  1251. * @iocbq: Pointer to driver iocb object.
  1252. *
  1253. * This function gets an iotag for the iocb. If there is no unused iotag and
  1254. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1255. * array and assigns a new iotag.
  1256. * The function returns the allocated iotag if successful, else returns zero.
  1257. * Zero is not a valid iotag.
  1258. * The caller is not required to hold any lock.
  1259. **/
  1260. uint16_t
  1261. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1262. {
  1263. struct lpfc_iocbq **new_arr;
  1264. struct lpfc_iocbq **old_arr;
  1265. size_t new_len;
  1266. struct lpfc_sli *psli = &phba->sli;
  1267. uint16_t iotag;
  1268. spin_lock_irq(&phba->hbalock);
  1269. iotag = psli->last_iotag;
  1270. if(++iotag < psli->iocbq_lookup_len) {
  1271. psli->last_iotag = iotag;
  1272. psli->iocbq_lookup[iotag] = iocbq;
  1273. spin_unlock_irq(&phba->hbalock);
  1274. iocbq->iotag = iotag;
  1275. return iotag;
  1276. } else if (psli->iocbq_lookup_len < (0xffff
  1277. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1278. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1279. spin_unlock_irq(&phba->hbalock);
  1280. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1281. GFP_KERNEL);
  1282. if (new_arr) {
  1283. spin_lock_irq(&phba->hbalock);
  1284. old_arr = psli->iocbq_lookup;
  1285. if (new_len <= psli->iocbq_lookup_len) {
  1286. /* highly unprobable case */
  1287. kfree(new_arr);
  1288. iotag = psli->last_iotag;
  1289. if(++iotag < psli->iocbq_lookup_len) {
  1290. psli->last_iotag = iotag;
  1291. psli->iocbq_lookup[iotag] = iocbq;
  1292. spin_unlock_irq(&phba->hbalock);
  1293. iocbq->iotag = iotag;
  1294. return iotag;
  1295. }
  1296. spin_unlock_irq(&phba->hbalock);
  1297. return 0;
  1298. }
  1299. if (psli->iocbq_lookup)
  1300. memcpy(new_arr, old_arr,
  1301. ((psli->last_iotag + 1) *
  1302. sizeof (struct lpfc_iocbq *)));
  1303. psli->iocbq_lookup = new_arr;
  1304. psli->iocbq_lookup_len = new_len;
  1305. psli->last_iotag = iotag;
  1306. psli->iocbq_lookup[iotag] = iocbq;
  1307. spin_unlock_irq(&phba->hbalock);
  1308. iocbq->iotag = iotag;
  1309. kfree(old_arr);
  1310. return iotag;
  1311. }
  1312. } else
  1313. spin_unlock_irq(&phba->hbalock);
  1314. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1315. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1316. psli->last_iotag);
  1317. return 0;
  1318. }
  1319. /**
  1320. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1321. * @phba: Pointer to HBA context object.
  1322. * @pring: Pointer to driver SLI ring object.
  1323. * @iocb: Pointer to iocb slot in the ring.
  1324. * @nextiocb: Pointer to driver iocb object which need to be
  1325. * posted to firmware.
  1326. *
  1327. * This function is called with hbalock held to post a new iocb to
  1328. * the firmware. This function copies the new iocb to ring iocb slot and
  1329. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1330. * a completion call back for this iocb else the function will free the
  1331. * iocb object.
  1332. **/
  1333. static void
  1334. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1335. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1336. {
  1337. /*
  1338. * Set up an iotag
  1339. */
  1340. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1341. if (pring->ringno == LPFC_ELS_RING) {
  1342. lpfc_debugfs_slow_ring_trc(phba,
  1343. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1344. *(((uint32_t *) &nextiocb->iocb) + 4),
  1345. *(((uint32_t *) &nextiocb->iocb) + 6),
  1346. *(((uint32_t *) &nextiocb->iocb) + 7));
  1347. }
  1348. /*
  1349. * Issue iocb command to adapter
  1350. */
  1351. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1352. wmb();
  1353. pring->stats.iocb_cmd++;
  1354. /*
  1355. * If there is no completion routine to call, we can release the
  1356. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1357. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1358. */
  1359. if (nextiocb->iocb_cmpl)
  1360. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1361. else
  1362. __lpfc_sli_release_iocbq(phba, nextiocb);
  1363. /*
  1364. * Let the HBA know what IOCB slot will be the next one the
  1365. * driver will put a command into.
  1366. */
  1367. pring->cmdidx = pring->next_cmdidx;
  1368. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1369. }
  1370. /**
  1371. * lpfc_sli_update_full_ring - Update the chip attention register
  1372. * @phba: Pointer to HBA context object.
  1373. * @pring: Pointer to driver SLI ring object.
  1374. *
  1375. * The caller is not required to hold any lock for calling this function.
  1376. * This function updates the chip attention bits for the ring to inform firmware
  1377. * that there are pending work to be done for this ring and requests an
  1378. * interrupt when there is space available in the ring. This function is
  1379. * called when the driver is unable to post more iocbs to the ring due
  1380. * to unavailability of space in the ring.
  1381. **/
  1382. static void
  1383. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1384. {
  1385. int ringno = pring->ringno;
  1386. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1387. wmb();
  1388. /*
  1389. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1390. * The HBA will tell us when an IOCB entry is available.
  1391. */
  1392. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1393. readl(phba->CAregaddr); /* flush */
  1394. pring->stats.iocb_cmd_full++;
  1395. }
  1396. /**
  1397. * lpfc_sli_update_ring - Update chip attention register
  1398. * @phba: Pointer to HBA context object.
  1399. * @pring: Pointer to driver SLI ring object.
  1400. *
  1401. * This function updates the chip attention register bit for the
  1402. * given ring to inform HBA that there is more work to be done
  1403. * in this ring. The caller is not required to hold any lock.
  1404. **/
  1405. static void
  1406. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1407. {
  1408. int ringno = pring->ringno;
  1409. /*
  1410. * Tell the HBA that there is work to do in this ring.
  1411. */
  1412. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1413. wmb();
  1414. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1415. readl(phba->CAregaddr); /* flush */
  1416. }
  1417. }
  1418. /**
  1419. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1420. * @phba: Pointer to HBA context object.
  1421. * @pring: Pointer to driver SLI ring object.
  1422. *
  1423. * This function is called with hbalock held to post pending iocbs
  1424. * in the txq to the firmware. This function is called when driver
  1425. * detects space available in the ring.
  1426. **/
  1427. static void
  1428. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1429. {
  1430. IOCB_t *iocb;
  1431. struct lpfc_iocbq *nextiocb;
  1432. /*
  1433. * Check to see if:
  1434. * (a) there is anything on the txq to send
  1435. * (b) link is up
  1436. * (c) link attention events can be processed (fcp ring only)
  1437. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1438. */
  1439. if (pring->txq_cnt &&
  1440. lpfc_is_link_up(phba) &&
  1441. (pring->ringno != phba->sli.fcp_ring ||
  1442. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1443. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1444. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1445. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1446. if (iocb)
  1447. lpfc_sli_update_ring(phba, pring);
  1448. else
  1449. lpfc_sli_update_full_ring(phba, pring);
  1450. }
  1451. return;
  1452. }
  1453. /**
  1454. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1455. * @phba: Pointer to HBA context object.
  1456. * @hbqno: HBQ number.
  1457. *
  1458. * This function is called with hbalock held to get the next
  1459. * available slot for the given HBQ. If there is free slot
  1460. * available for the HBQ it will return pointer to the next available
  1461. * HBQ entry else it will return NULL.
  1462. **/
  1463. static struct lpfc_hbq_entry *
  1464. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1465. {
  1466. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1467. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1468. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1469. hbqp->next_hbqPutIdx = 0;
  1470. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1471. uint32_t raw_index = phba->hbq_get[hbqno];
  1472. uint32_t getidx = le32_to_cpu(raw_index);
  1473. hbqp->local_hbqGetIdx = getidx;
  1474. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1475. lpfc_printf_log(phba, KERN_ERR,
  1476. LOG_SLI | LOG_VPORT,
  1477. "1802 HBQ %d: local_hbqGetIdx "
  1478. "%u is > than hbqp->entry_count %u\n",
  1479. hbqno, hbqp->local_hbqGetIdx,
  1480. hbqp->entry_count);
  1481. phba->link_state = LPFC_HBA_ERROR;
  1482. return NULL;
  1483. }
  1484. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1485. return NULL;
  1486. }
  1487. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1488. hbqp->hbqPutIdx;
  1489. }
  1490. /**
  1491. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1492. * @phba: Pointer to HBA context object.
  1493. *
  1494. * This function is called with no lock held to free all the
  1495. * hbq buffers while uninitializing the SLI interface. It also
  1496. * frees the HBQ buffers returned by the firmware but not yet
  1497. * processed by the upper layers.
  1498. **/
  1499. void
  1500. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1501. {
  1502. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1503. struct hbq_dmabuf *hbq_buf;
  1504. unsigned long flags;
  1505. int i, hbq_count;
  1506. uint32_t hbqno;
  1507. hbq_count = lpfc_sli_hbq_count();
  1508. /* Return all memory used by all HBQs */
  1509. spin_lock_irqsave(&phba->hbalock, flags);
  1510. for (i = 0; i < hbq_count; ++i) {
  1511. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1512. &phba->hbqs[i].hbq_buffer_list, list) {
  1513. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1514. list_del(&hbq_buf->dbuf.list);
  1515. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1516. }
  1517. phba->hbqs[i].buffer_count = 0;
  1518. }
  1519. /* Return all HBQ buffer that are in-fly */
  1520. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1521. list) {
  1522. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1523. list_del(&hbq_buf->dbuf.list);
  1524. if (hbq_buf->tag == -1) {
  1525. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1526. (phba, hbq_buf);
  1527. } else {
  1528. hbqno = hbq_buf->tag >> 16;
  1529. if (hbqno >= LPFC_MAX_HBQS)
  1530. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1531. (phba, hbq_buf);
  1532. else
  1533. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1534. hbq_buf);
  1535. }
  1536. }
  1537. /* Mark the HBQs not in use */
  1538. phba->hbq_in_use = 0;
  1539. spin_unlock_irqrestore(&phba->hbalock, flags);
  1540. }
  1541. /**
  1542. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1543. * @phba: Pointer to HBA context object.
  1544. * @hbqno: HBQ number.
  1545. * @hbq_buf: Pointer to HBQ buffer.
  1546. *
  1547. * This function is called with the hbalock held to post a
  1548. * hbq buffer to the firmware. If the function finds an empty
  1549. * slot in the HBQ, it will post the buffer. The function will return
  1550. * pointer to the hbq entry if it successfully post the buffer
  1551. * else it will return NULL.
  1552. **/
  1553. static int
  1554. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1555. struct hbq_dmabuf *hbq_buf)
  1556. {
  1557. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1558. }
  1559. /**
  1560. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1561. * @phba: Pointer to HBA context object.
  1562. * @hbqno: HBQ number.
  1563. * @hbq_buf: Pointer to HBQ buffer.
  1564. *
  1565. * This function is called with the hbalock held to post a hbq buffer to the
  1566. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1567. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1568. * it successfully post the buffer else it will return an error.
  1569. **/
  1570. static int
  1571. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1572. struct hbq_dmabuf *hbq_buf)
  1573. {
  1574. struct lpfc_hbq_entry *hbqe;
  1575. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1576. /* Get next HBQ entry slot to use */
  1577. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1578. if (hbqe) {
  1579. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1580. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1581. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1582. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1583. hbqe->bde.tus.f.bdeFlags = 0;
  1584. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1585. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1586. /* Sync SLIM */
  1587. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1588. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1589. /* flush */
  1590. readl(phba->hbq_put + hbqno);
  1591. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1592. return 0;
  1593. } else
  1594. return -ENOMEM;
  1595. }
  1596. /**
  1597. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1598. * @phba: Pointer to HBA context object.
  1599. * @hbqno: HBQ number.
  1600. * @hbq_buf: Pointer to HBQ buffer.
  1601. *
  1602. * This function is called with the hbalock held to post an RQE to the SLI4
  1603. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1604. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1605. **/
  1606. static int
  1607. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1608. struct hbq_dmabuf *hbq_buf)
  1609. {
  1610. int rc;
  1611. struct lpfc_rqe hrqe;
  1612. struct lpfc_rqe drqe;
  1613. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1614. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1615. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1616. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1617. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1618. &hrqe, &drqe);
  1619. if (rc < 0)
  1620. return rc;
  1621. hbq_buf->tag = rc;
  1622. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1623. return 0;
  1624. }
  1625. /* HBQ for ELS and CT traffic. */
  1626. static struct lpfc_hbq_init lpfc_els_hbq = {
  1627. .rn = 1,
  1628. .entry_count = 256,
  1629. .mask_count = 0,
  1630. .profile = 0,
  1631. .ring_mask = (1 << LPFC_ELS_RING),
  1632. .buffer_count = 0,
  1633. .init_count = 40,
  1634. .add_count = 40,
  1635. };
  1636. /* HBQ for the extra ring if needed */
  1637. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1638. .rn = 1,
  1639. .entry_count = 200,
  1640. .mask_count = 0,
  1641. .profile = 0,
  1642. .ring_mask = (1 << LPFC_EXTRA_RING),
  1643. .buffer_count = 0,
  1644. .init_count = 0,
  1645. .add_count = 5,
  1646. };
  1647. /* Array of HBQs */
  1648. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1649. &lpfc_els_hbq,
  1650. &lpfc_extra_hbq,
  1651. };
  1652. /**
  1653. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1654. * @phba: Pointer to HBA context object.
  1655. * @hbqno: HBQ number.
  1656. * @count: Number of HBQ buffers to be posted.
  1657. *
  1658. * This function is called with no lock held to post more hbq buffers to the
  1659. * given HBQ. The function returns the number of HBQ buffers successfully
  1660. * posted.
  1661. **/
  1662. static int
  1663. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1664. {
  1665. uint32_t i, posted = 0;
  1666. unsigned long flags;
  1667. struct hbq_dmabuf *hbq_buffer;
  1668. LIST_HEAD(hbq_buf_list);
  1669. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1670. return 0;
  1671. if ((phba->hbqs[hbqno].buffer_count + count) >
  1672. lpfc_hbq_defs[hbqno]->entry_count)
  1673. count = lpfc_hbq_defs[hbqno]->entry_count -
  1674. phba->hbqs[hbqno].buffer_count;
  1675. if (!count)
  1676. return 0;
  1677. /* Allocate HBQ entries */
  1678. for (i = 0; i < count; i++) {
  1679. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1680. if (!hbq_buffer)
  1681. break;
  1682. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1683. }
  1684. /* Check whether HBQ is still in use */
  1685. spin_lock_irqsave(&phba->hbalock, flags);
  1686. if (!phba->hbq_in_use)
  1687. goto err;
  1688. while (!list_empty(&hbq_buf_list)) {
  1689. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1690. dbuf.list);
  1691. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1692. (hbqno << 16));
  1693. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1694. phba->hbqs[hbqno].buffer_count++;
  1695. posted++;
  1696. } else
  1697. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1698. }
  1699. spin_unlock_irqrestore(&phba->hbalock, flags);
  1700. return posted;
  1701. err:
  1702. spin_unlock_irqrestore(&phba->hbalock, flags);
  1703. while (!list_empty(&hbq_buf_list)) {
  1704. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1705. dbuf.list);
  1706. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1707. }
  1708. return 0;
  1709. }
  1710. /**
  1711. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1712. * @phba: Pointer to HBA context object.
  1713. * @qno: HBQ number.
  1714. *
  1715. * This function posts more buffers to the HBQ. This function
  1716. * is called with no lock held. The function returns the number of HBQ entries
  1717. * successfully allocated.
  1718. **/
  1719. int
  1720. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1721. {
  1722. if (phba->sli_rev == LPFC_SLI_REV4)
  1723. return 0;
  1724. else
  1725. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1726. lpfc_hbq_defs[qno]->add_count);
  1727. }
  1728. /**
  1729. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1730. * @phba: Pointer to HBA context object.
  1731. * @qno: HBQ queue number.
  1732. *
  1733. * This function is called from SLI initialization code path with
  1734. * no lock held to post initial HBQ buffers to firmware. The
  1735. * function returns the number of HBQ entries successfully allocated.
  1736. **/
  1737. static int
  1738. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1739. {
  1740. if (phba->sli_rev == LPFC_SLI_REV4)
  1741. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1742. lpfc_hbq_defs[qno]->entry_count);
  1743. else
  1744. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1745. lpfc_hbq_defs[qno]->init_count);
  1746. }
  1747. /**
  1748. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1749. * @phba: Pointer to HBA context object.
  1750. * @hbqno: HBQ number.
  1751. *
  1752. * This function removes the first hbq buffer on an hbq list and returns a
  1753. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1754. **/
  1755. static struct hbq_dmabuf *
  1756. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1757. {
  1758. struct lpfc_dmabuf *d_buf;
  1759. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1760. if (!d_buf)
  1761. return NULL;
  1762. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1763. }
  1764. /**
  1765. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1766. * @phba: Pointer to HBA context object.
  1767. * @tag: Tag of the hbq buffer.
  1768. *
  1769. * This function is called with hbalock held. This function searches
  1770. * for the hbq buffer associated with the given tag in the hbq buffer
  1771. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1772. * it returns NULL.
  1773. **/
  1774. static struct hbq_dmabuf *
  1775. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1776. {
  1777. struct lpfc_dmabuf *d_buf;
  1778. struct hbq_dmabuf *hbq_buf;
  1779. uint32_t hbqno;
  1780. hbqno = tag >> 16;
  1781. if (hbqno >= LPFC_MAX_HBQS)
  1782. return NULL;
  1783. spin_lock_irq(&phba->hbalock);
  1784. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1785. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1786. if (hbq_buf->tag == tag) {
  1787. spin_unlock_irq(&phba->hbalock);
  1788. return hbq_buf;
  1789. }
  1790. }
  1791. spin_unlock_irq(&phba->hbalock);
  1792. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1793. "1803 Bad hbq tag. Data: x%x x%x\n",
  1794. tag, phba->hbqs[tag >> 16].buffer_count);
  1795. return NULL;
  1796. }
  1797. /**
  1798. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1799. * @phba: Pointer to HBA context object.
  1800. * @hbq_buffer: Pointer to HBQ buffer.
  1801. *
  1802. * This function is called with hbalock. This function gives back
  1803. * the hbq buffer to firmware. If the HBQ does not have space to
  1804. * post the buffer, it will free the buffer.
  1805. **/
  1806. void
  1807. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1808. {
  1809. uint32_t hbqno;
  1810. if (hbq_buffer) {
  1811. hbqno = hbq_buffer->tag >> 16;
  1812. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1813. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1814. }
  1815. }
  1816. /**
  1817. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1818. * @mbxCommand: mailbox command code.
  1819. *
  1820. * This function is called by the mailbox event handler function to verify
  1821. * that the completed mailbox command is a legitimate mailbox command. If the
  1822. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1823. * and the mailbox event handler will take the HBA offline.
  1824. **/
  1825. static int
  1826. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1827. {
  1828. uint8_t ret;
  1829. switch (mbxCommand) {
  1830. case MBX_LOAD_SM:
  1831. case MBX_READ_NV:
  1832. case MBX_WRITE_NV:
  1833. case MBX_WRITE_VPARMS:
  1834. case MBX_RUN_BIU_DIAG:
  1835. case MBX_INIT_LINK:
  1836. case MBX_DOWN_LINK:
  1837. case MBX_CONFIG_LINK:
  1838. case MBX_CONFIG_RING:
  1839. case MBX_RESET_RING:
  1840. case MBX_READ_CONFIG:
  1841. case MBX_READ_RCONFIG:
  1842. case MBX_READ_SPARM:
  1843. case MBX_READ_STATUS:
  1844. case MBX_READ_RPI:
  1845. case MBX_READ_XRI:
  1846. case MBX_READ_REV:
  1847. case MBX_READ_LNK_STAT:
  1848. case MBX_REG_LOGIN:
  1849. case MBX_UNREG_LOGIN:
  1850. case MBX_CLEAR_LA:
  1851. case MBX_DUMP_MEMORY:
  1852. case MBX_DUMP_CONTEXT:
  1853. case MBX_RUN_DIAGS:
  1854. case MBX_RESTART:
  1855. case MBX_UPDATE_CFG:
  1856. case MBX_DOWN_LOAD:
  1857. case MBX_DEL_LD_ENTRY:
  1858. case MBX_RUN_PROGRAM:
  1859. case MBX_SET_MASK:
  1860. case MBX_SET_VARIABLE:
  1861. case MBX_UNREG_D_ID:
  1862. case MBX_KILL_BOARD:
  1863. case MBX_CONFIG_FARP:
  1864. case MBX_BEACON:
  1865. case MBX_LOAD_AREA:
  1866. case MBX_RUN_BIU_DIAG64:
  1867. case MBX_CONFIG_PORT:
  1868. case MBX_READ_SPARM64:
  1869. case MBX_READ_RPI64:
  1870. case MBX_REG_LOGIN64:
  1871. case MBX_READ_TOPOLOGY:
  1872. case MBX_WRITE_WWN:
  1873. case MBX_SET_DEBUG:
  1874. case MBX_LOAD_EXP_ROM:
  1875. case MBX_ASYNCEVT_ENABLE:
  1876. case MBX_REG_VPI:
  1877. case MBX_UNREG_VPI:
  1878. case MBX_HEARTBEAT:
  1879. case MBX_PORT_CAPABILITIES:
  1880. case MBX_PORT_IOV_CONTROL:
  1881. case MBX_SLI4_CONFIG:
  1882. case MBX_SLI4_REQ_FTRS:
  1883. case MBX_REG_FCFI:
  1884. case MBX_UNREG_FCFI:
  1885. case MBX_REG_VFI:
  1886. case MBX_UNREG_VFI:
  1887. case MBX_INIT_VPI:
  1888. case MBX_INIT_VFI:
  1889. case MBX_RESUME_RPI:
  1890. case MBX_READ_EVENT_LOG_STATUS:
  1891. case MBX_READ_EVENT_LOG:
  1892. case MBX_SECURITY_MGMT:
  1893. case MBX_AUTH_PORT:
  1894. ret = mbxCommand;
  1895. break;
  1896. default:
  1897. ret = MBX_SHUTDOWN;
  1898. break;
  1899. }
  1900. return ret;
  1901. }
  1902. /**
  1903. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1904. * @phba: Pointer to HBA context object.
  1905. * @pmboxq: Pointer to mailbox command.
  1906. *
  1907. * This is completion handler function for mailbox commands issued from
  1908. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1909. * mailbox event handler function with no lock held. This function
  1910. * will wake up thread waiting on the wait queue pointed by context1
  1911. * of the mailbox.
  1912. **/
  1913. void
  1914. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1915. {
  1916. wait_queue_head_t *pdone_q;
  1917. unsigned long drvr_flag;
  1918. /*
  1919. * If pdone_q is empty, the driver thread gave up waiting and
  1920. * continued running.
  1921. */
  1922. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1923. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1924. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1925. if (pdone_q)
  1926. wake_up_interruptible(pdone_q);
  1927. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1928. return;
  1929. }
  1930. /**
  1931. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1932. * @phba: Pointer to HBA context object.
  1933. * @pmb: Pointer to mailbox object.
  1934. *
  1935. * This function is the default mailbox completion handler. It
  1936. * frees the memory resources associated with the completed mailbox
  1937. * command. If the completed command is a REG_LOGIN mailbox command,
  1938. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1939. **/
  1940. void
  1941. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1942. {
  1943. struct lpfc_vport *vport = pmb->vport;
  1944. struct lpfc_dmabuf *mp;
  1945. struct lpfc_nodelist *ndlp;
  1946. struct Scsi_Host *shost;
  1947. uint16_t rpi, vpi;
  1948. int rc;
  1949. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1950. if (mp) {
  1951. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1952. kfree(mp);
  1953. }
  1954. /*
  1955. * If a REG_LOGIN succeeded after node is destroyed or node
  1956. * is in re-discovery driver need to cleanup the RPI.
  1957. */
  1958. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1959. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1960. !pmb->u.mb.mbxStatus) {
  1961. rpi = pmb->u.mb.un.varWords[0];
  1962. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1963. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1964. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1965. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1966. if (rc != MBX_NOT_FINISHED)
  1967. return;
  1968. }
  1969. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  1970. !(phba->pport->load_flag & FC_UNLOADING) &&
  1971. !pmb->u.mb.mbxStatus) {
  1972. shost = lpfc_shost_from_vport(vport);
  1973. spin_lock_irq(shost->host_lock);
  1974. vport->vpi_state |= LPFC_VPI_REGISTERED;
  1975. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  1976. spin_unlock_irq(shost->host_lock);
  1977. }
  1978. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  1979. ndlp = (struct lpfc_nodelist *)pmb->context2;
  1980. lpfc_nlp_put(ndlp);
  1981. pmb->context2 = NULL;
  1982. }
  1983. /* Check security permission status on INIT_LINK mailbox command */
  1984. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  1985. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  1986. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1987. "2860 SLI authentication is required "
  1988. "for INIT_LINK but has not done yet\n");
  1989. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  1990. lpfc_sli4_mbox_cmd_free(phba, pmb);
  1991. else
  1992. mempool_free(pmb, phba->mbox_mem_pool);
  1993. }
  1994. /**
  1995. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  1996. * @phba: Pointer to HBA context object.
  1997. *
  1998. * This function is called with no lock held. This function processes all
  1999. * the completed mailbox commands and gives it to upper layers. The interrupt
  2000. * service routine processes mailbox completion interrupt and adds completed
  2001. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  2002. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  2003. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  2004. * function returns the mailbox commands to the upper layer by calling the
  2005. * completion handler function of each mailbox.
  2006. **/
  2007. int
  2008. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  2009. {
  2010. MAILBOX_t *pmbox;
  2011. LPFC_MBOXQ_t *pmb;
  2012. int rc;
  2013. LIST_HEAD(cmplq);
  2014. phba->sli.slistat.mbox_event++;
  2015. /* Get all completed mailboxe buffers into the cmplq */
  2016. spin_lock_irq(&phba->hbalock);
  2017. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2018. spin_unlock_irq(&phba->hbalock);
  2019. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2020. do {
  2021. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2022. if (pmb == NULL)
  2023. break;
  2024. pmbox = &pmb->u.mb;
  2025. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2026. if (pmb->vport) {
  2027. lpfc_debugfs_disc_trc(pmb->vport,
  2028. LPFC_DISC_TRC_MBOX_VPORT,
  2029. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2030. (uint32_t)pmbox->mbxCommand,
  2031. pmbox->un.varWords[0],
  2032. pmbox->un.varWords[1]);
  2033. }
  2034. else {
  2035. lpfc_debugfs_disc_trc(phba->pport,
  2036. LPFC_DISC_TRC_MBOX,
  2037. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2038. (uint32_t)pmbox->mbxCommand,
  2039. pmbox->un.varWords[0],
  2040. pmbox->un.varWords[1]);
  2041. }
  2042. }
  2043. /*
  2044. * It is a fatal error if unknown mbox command completion.
  2045. */
  2046. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2047. MBX_SHUTDOWN) {
  2048. /* Unknown mailbox command compl */
  2049. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2050. "(%d):0323 Unknown Mailbox command "
  2051. "x%x (x%x/x%x) Cmpl\n",
  2052. pmb->vport ? pmb->vport->vpi : 0,
  2053. pmbox->mbxCommand,
  2054. lpfc_sli_config_mbox_subsys_get(phba,
  2055. pmb),
  2056. lpfc_sli_config_mbox_opcode_get(phba,
  2057. pmb));
  2058. phba->link_state = LPFC_HBA_ERROR;
  2059. phba->work_hs = HS_FFER3;
  2060. lpfc_handle_eratt(phba);
  2061. continue;
  2062. }
  2063. if (pmbox->mbxStatus) {
  2064. phba->sli.slistat.mbox_stat_err++;
  2065. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2066. /* Mbox cmd cmpl error - RETRYing */
  2067. lpfc_printf_log(phba, KERN_INFO,
  2068. LOG_MBOX | LOG_SLI,
  2069. "(%d):0305 Mbox cmd cmpl "
  2070. "error - RETRYing Data: x%x "
  2071. "(x%x/x%x) x%x x%x x%x\n",
  2072. pmb->vport ? pmb->vport->vpi : 0,
  2073. pmbox->mbxCommand,
  2074. lpfc_sli_config_mbox_subsys_get(phba,
  2075. pmb),
  2076. lpfc_sli_config_mbox_opcode_get(phba,
  2077. pmb),
  2078. pmbox->mbxStatus,
  2079. pmbox->un.varWords[0],
  2080. pmb->vport->port_state);
  2081. pmbox->mbxStatus = 0;
  2082. pmbox->mbxOwner = OWN_HOST;
  2083. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2084. if (rc != MBX_NOT_FINISHED)
  2085. continue;
  2086. }
  2087. }
  2088. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2089. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2090. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2091. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2092. pmb->vport ? pmb->vport->vpi : 0,
  2093. pmbox->mbxCommand,
  2094. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2095. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2096. pmb->mbox_cmpl,
  2097. *((uint32_t *) pmbox),
  2098. pmbox->un.varWords[0],
  2099. pmbox->un.varWords[1],
  2100. pmbox->un.varWords[2],
  2101. pmbox->un.varWords[3],
  2102. pmbox->un.varWords[4],
  2103. pmbox->un.varWords[5],
  2104. pmbox->un.varWords[6],
  2105. pmbox->un.varWords[7]);
  2106. if (pmb->mbox_cmpl)
  2107. pmb->mbox_cmpl(phba,pmb);
  2108. } while (1);
  2109. return 0;
  2110. }
  2111. /**
  2112. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2113. * @phba: Pointer to HBA context object.
  2114. * @pring: Pointer to driver SLI ring object.
  2115. * @tag: buffer tag.
  2116. *
  2117. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2118. * is set in the tag the buffer is posted for a particular exchange,
  2119. * the function will return the buffer without replacing the buffer.
  2120. * If the buffer is for unsolicited ELS or CT traffic, this function
  2121. * returns the buffer and also posts another buffer to the firmware.
  2122. **/
  2123. static struct lpfc_dmabuf *
  2124. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2125. struct lpfc_sli_ring *pring,
  2126. uint32_t tag)
  2127. {
  2128. struct hbq_dmabuf *hbq_entry;
  2129. if (tag & QUE_BUFTAG_BIT)
  2130. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2131. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2132. if (!hbq_entry)
  2133. return NULL;
  2134. return &hbq_entry->dbuf;
  2135. }
  2136. /**
  2137. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2138. * @phba: Pointer to HBA context object.
  2139. * @pring: Pointer to driver SLI ring object.
  2140. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2141. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2142. * @fch_type: the type for the first frame of the sequence.
  2143. *
  2144. * This function is called with no lock held. This function uses the r_ctl and
  2145. * type of the received sequence to find the correct callback function to call
  2146. * to process the sequence.
  2147. **/
  2148. static int
  2149. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2150. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2151. uint32_t fch_type)
  2152. {
  2153. int i;
  2154. /* unSolicited Responses */
  2155. if (pring->prt[0].profile) {
  2156. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2157. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2158. saveq);
  2159. return 1;
  2160. }
  2161. /* We must search, based on rctl / type
  2162. for the right routine */
  2163. for (i = 0; i < pring->num_mask; i++) {
  2164. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2165. (pring->prt[i].type == fch_type)) {
  2166. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2167. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2168. (phba, pring, saveq);
  2169. return 1;
  2170. }
  2171. }
  2172. return 0;
  2173. }
  2174. /**
  2175. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2176. * @phba: Pointer to HBA context object.
  2177. * @pring: Pointer to driver SLI ring object.
  2178. * @saveq: Pointer to the unsolicited iocb.
  2179. *
  2180. * This function is called with no lock held by the ring event handler
  2181. * when there is an unsolicited iocb posted to the response ring by the
  2182. * firmware. This function gets the buffer associated with the iocbs
  2183. * and calls the event handler for the ring. This function handles both
  2184. * qring buffers and hbq buffers.
  2185. * When the function returns 1 the caller can free the iocb object otherwise
  2186. * upper layer functions will free the iocb objects.
  2187. **/
  2188. static int
  2189. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2190. struct lpfc_iocbq *saveq)
  2191. {
  2192. IOCB_t * irsp;
  2193. WORD5 * w5p;
  2194. uint32_t Rctl, Type;
  2195. uint32_t match;
  2196. struct lpfc_iocbq *iocbq;
  2197. struct lpfc_dmabuf *dmzbuf;
  2198. match = 0;
  2199. irsp = &(saveq->iocb);
  2200. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2201. if (pring->lpfc_sli_rcv_async_status)
  2202. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2203. else
  2204. lpfc_printf_log(phba,
  2205. KERN_WARNING,
  2206. LOG_SLI,
  2207. "0316 Ring %d handler: unexpected "
  2208. "ASYNC_STATUS iocb received evt_code "
  2209. "0x%x\n",
  2210. pring->ringno,
  2211. irsp->un.asyncstat.evt_code);
  2212. return 1;
  2213. }
  2214. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2215. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2216. if (irsp->ulpBdeCount > 0) {
  2217. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2218. irsp->un.ulpWord[3]);
  2219. lpfc_in_buf_free(phba, dmzbuf);
  2220. }
  2221. if (irsp->ulpBdeCount > 1) {
  2222. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2223. irsp->unsli3.sli3Words[3]);
  2224. lpfc_in_buf_free(phba, dmzbuf);
  2225. }
  2226. if (irsp->ulpBdeCount > 2) {
  2227. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2228. irsp->unsli3.sli3Words[7]);
  2229. lpfc_in_buf_free(phba, dmzbuf);
  2230. }
  2231. return 1;
  2232. }
  2233. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2234. if (irsp->ulpBdeCount != 0) {
  2235. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2236. irsp->un.ulpWord[3]);
  2237. if (!saveq->context2)
  2238. lpfc_printf_log(phba,
  2239. KERN_ERR,
  2240. LOG_SLI,
  2241. "0341 Ring %d Cannot find buffer for "
  2242. "an unsolicited iocb. tag 0x%x\n",
  2243. pring->ringno,
  2244. irsp->un.ulpWord[3]);
  2245. }
  2246. if (irsp->ulpBdeCount == 2) {
  2247. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2248. irsp->unsli3.sli3Words[7]);
  2249. if (!saveq->context3)
  2250. lpfc_printf_log(phba,
  2251. KERN_ERR,
  2252. LOG_SLI,
  2253. "0342 Ring %d Cannot find buffer for an"
  2254. " unsolicited iocb. tag 0x%x\n",
  2255. pring->ringno,
  2256. irsp->unsli3.sli3Words[7]);
  2257. }
  2258. list_for_each_entry(iocbq, &saveq->list, list) {
  2259. irsp = &(iocbq->iocb);
  2260. if (irsp->ulpBdeCount != 0) {
  2261. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2262. irsp->un.ulpWord[3]);
  2263. if (!iocbq->context2)
  2264. lpfc_printf_log(phba,
  2265. KERN_ERR,
  2266. LOG_SLI,
  2267. "0343 Ring %d Cannot find "
  2268. "buffer for an unsolicited iocb"
  2269. ". tag 0x%x\n", pring->ringno,
  2270. irsp->un.ulpWord[3]);
  2271. }
  2272. if (irsp->ulpBdeCount == 2) {
  2273. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2274. irsp->unsli3.sli3Words[7]);
  2275. if (!iocbq->context3)
  2276. lpfc_printf_log(phba,
  2277. KERN_ERR,
  2278. LOG_SLI,
  2279. "0344 Ring %d Cannot find "
  2280. "buffer for an unsolicited "
  2281. "iocb. tag 0x%x\n",
  2282. pring->ringno,
  2283. irsp->unsli3.sli3Words[7]);
  2284. }
  2285. }
  2286. }
  2287. if (irsp->ulpBdeCount != 0 &&
  2288. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2289. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2290. int found = 0;
  2291. /* search continue save q for same XRI */
  2292. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2293. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2294. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2295. list_add_tail(&saveq->list, &iocbq->list);
  2296. found = 1;
  2297. break;
  2298. }
  2299. }
  2300. if (!found)
  2301. list_add_tail(&saveq->clist,
  2302. &pring->iocb_continue_saveq);
  2303. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2304. list_del_init(&iocbq->clist);
  2305. saveq = iocbq;
  2306. irsp = &(saveq->iocb);
  2307. } else
  2308. return 0;
  2309. }
  2310. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2311. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2312. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2313. Rctl = FC_RCTL_ELS_REQ;
  2314. Type = FC_TYPE_ELS;
  2315. } else {
  2316. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2317. Rctl = w5p->hcsw.Rctl;
  2318. Type = w5p->hcsw.Type;
  2319. /* Firmware Workaround */
  2320. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2321. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2322. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2323. Rctl = FC_RCTL_ELS_REQ;
  2324. Type = FC_TYPE_ELS;
  2325. w5p->hcsw.Rctl = Rctl;
  2326. w5p->hcsw.Type = Type;
  2327. }
  2328. }
  2329. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2330. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2331. "0313 Ring %d handler: unexpected Rctl x%x "
  2332. "Type x%x received\n",
  2333. pring->ringno, Rctl, Type);
  2334. return 1;
  2335. }
  2336. /**
  2337. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2338. * @phba: Pointer to HBA context object.
  2339. * @pring: Pointer to driver SLI ring object.
  2340. * @prspiocb: Pointer to response iocb object.
  2341. *
  2342. * This function looks up the iocb_lookup table to get the command iocb
  2343. * corresponding to the given response iocb using the iotag of the
  2344. * response iocb. This function is called with the hbalock held.
  2345. * This function returns the command iocb object if it finds the command
  2346. * iocb else returns NULL.
  2347. **/
  2348. static struct lpfc_iocbq *
  2349. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2350. struct lpfc_sli_ring *pring,
  2351. struct lpfc_iocbq *prspiocb)
  2352. {
  2353. struct lpfc_iocbq *cmd_iocb = NULL;
  2354. uint16_t iotag;
  2355. iotag = prspiocb->iocb.ulpIoTag;
  2356. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2357. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2358. list_del_init(&cmd_iocb->list);
  2359. if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
  2360. pring->txcmplq_cnt--;
  2361. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
  2362. }
  2363. return cmd_iocb;
  2364. }
  2365. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2366. "0317 iotag x%x is out off "
  2367. "range: max iotag x%x wd0 x%x\n",
  2368. iotag, phba->sli.last_iotag,
  2369. *(((uint32_t *) &prspiocb->iocb) + 7));
  2370. return NULL;
  2371. }
  2372. /**
  2373. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2374. * @phba: Pointer to HBA context object.
  2375. * @pring: Pointer to driver SLI ring object.
  2376. * @iotag: IOCB tag.
  2377. *
  2378. * This function looks up the iocb_lookup table to get the command iocb
  2379. * corresponding to the given iotag. This function is called with the
  2380. * hbalock held.
  2381. * This function returns the command iocb object if it finds the command
  2382. * iocb else returns NULL.
  2383. **/
  2384. static struct lpfc_iocbq *
  2385. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2386. struct lpfc_sli_ring *pring, uint16_t iotag)
  2387. {
  2388. struct lpfc_iocbq *cmd_iocb;
  2389. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2390. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2391. list_del_init(&cmd_iocb->list);
  2392. if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
  2393. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
  2394. pring->txcmplq_cnt--;
  2395. }
  2396. return cmd_iocb;
  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->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;
  2598. phba = (struct lpfc_hba *)ptr;
  2599. /* Check chip HA register for error event */
  2600. eratt = lpfc_sli_check_eratt(phba);
  2601. if (eratt)
  2602. /* Tell the worker thread there is work to do */
  2603. lpfc_worker_wake_up(phba);
  2604. else
  2605. /* Restart the timer for next eratt poll */
  2606. mod_timer(&phba->eratt_poll, jiffies +
  2607. HZ * LPFC_ERATT_POLL_INTERVAL);
  2608. return;
  2609. }
  2610. /**
  2611. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2612. * @phba: Pointer to HBA context object.
  2613. * @pring: Pointer to driver SLI ring object.
  2614. * @mask: Host attention register mask for this ring.
  2615. *
  2616. * This function is called from the interrupt context when there is a ring
  2617. * event for the fcp ring. The caller does not hold any lock.
  2618. * The function processes each response iocb in the response ring until it
  2619. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2620. * LE bit set. The function will call the completion handler of the command iocb
  2621. * if the response iocb indicates a completion for a command iocb or it is
  2622. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2623. * function if this is an unsolicited iocb.
  2624. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2625. * to check it explicitly.
  2626. */
  2627. int
  2628. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2629. struct lpfc_sli_ring *pring, uint32_t mask)
  2630. {
  2631. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2632. IOCB_t *irsp = NULL;
  2633. IOCB_t *entry = NULL;
  2634. struct lpfc_iocbq *cmdiocbq = NULL;
  2635. struct lpfc_iocbq rspiocbq;
  2636. uint32_t status;
  2637. uint32_t portRspPut, portRspMax;
  2638. int rc = 1;
  2639. lpfc_iocb_type type;
  2640. unsigned long iflag;
  2641. uint32_t rsp_cmpl = 0;
  2642. spin_lock_irqsave(&phba->hbalock, iflag);
  2643. pring->stats.iocb_event++;
  2644. /*
  2645. * The next available response entry should never exceed the maximum
  2646. * entries. If it does, treat it as an adapter hardware error.
  2647. */
  2648. portRspMax = pring->numRiocb;
  2649. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2650. if (unlikely(portRspPut >= portRspMax)) {
  2651. lpfc_sli_rsp_pointers_error(phba, pring);
  2652. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2653. return 1;
  2654. }
  2655. if (phba->fcp_ring_in_use) {
  2656. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2657. return 1;
  2658. } else
  2659. phba->fcp_ring_in_use = 1;
  2660. rmb();
  2661. while (pring->rspidx != portRspPut) {
  2662. /*
  2663. * Fetch an entry off the ring and copy it into a local data
  2664. * structure. The copy involves a byte-swap since the
  2665. * network byte order and pci byte orders are different.
  2666. */
  2667. entry = lpfc_resp_iocb(phba, pring);
  2668. phba->last_completion_time = jiffies;
  2669. if (++pring->rspidx >= portRspMax)
  2670. pring->rspidx = 0;
  2671. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2672. (uint32_t *) &rspiocbq.iocb,
  2673. phba->iocb_rsp_size);
  2674. INIT_LIST_HEAD(&(rspiocbq.list));
  2675. irsp = &rspiocbq.iocb;
  2676. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2677. pring->stats.iocb_rsp++;
  2678. rsp_cmpl++;
  2679. if (unlikely(irsp->ulpStatus)) {
  2680. /*
  2681. * If resource errors reported from HBA, reduce
  2682. * queuedepths of the SCSI device.
  2683. */
  2684. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2685. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2686. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2687. phba->lpfc_rampdown_queue_depth(phba);
  2688. spin_lock_irqsave(&phba->hbalock, iflag);
  2689. }
  2690. /* Rsp ring <ringno> error: IOCB */
  2691. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2692. "0336 Rsp Ring %d error: IOCB Data: "
  2693. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2694. pring->ringno,
  2695. irsp->un.ulpWord[0],
  2696. irsp->un.ulpWord[1],
  2697. irsp->un.ulpWord[2],
  2698. irsp->un.ulpWord[3],
  2699. irsp->un.ulpWord[4],
  2700. irsp->un.ulpWord[5],
  2701. *(uint32_t *)&irsp->un1,
  2702. *((uint32_t *)&irsp->un1 + 1));
  2703. }
  2704. switch (type) {
  2705. case LPFC_ABORT_IOCB:
  2706. case LPFC_SOL_IOCB:
  2707. /*
  2708. * Idle exchange closed via ABTS from port. No iocb
  2709. * resources need to be recovered.
  2710. */
  2711. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2712. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2713. "0333 IOCB cmd 0x%x"
  2714. " processed. Skipping"
  2715. " completion\n",
  2716. irsp->ulpCommand);
  2717. break;
  2718. }
  2719. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2720. &rspiocbq);
  2721. if (unlikely(!cmdiocbq))
  2722. break;
  2723. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2724. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2725. if (cmdiocbq->iocb_cmpl) {
  2726. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2727. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2728. &rspiocbq);
  2729. spin_lock_irqsave(&phba->hbalock, iflag);
  2730. }
  2731. break;
  2732. case LPFC_UNSOL_IOCB:
  2733. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2734. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2735. spin_lock_irqsave(&phba->hbalock, iflag);
  2736. break;
  2737. default:
  2738. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2739. char adaptermsg[LPFC_MAX_ADPTMSG];
  2740. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2741. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2742. MAX_MSG_DATA);
  2743. dev_warn(&((phba->pcidev)->dev),
  2744. "lpfc%d: %s\n",
  2745. phba->brd_no, adaptermsg);
  2746. } else {
  2747. /* Unknown IOCB command */
  2748. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2749. "0334 Unknown IOCB command "
  2750. "Data: x%x, x%x x%x x%x x%x\n",
  2751. type, irsp->ulpCommand,
  2752. irsp->ulpStatus,
  2753. irsp->ulpIoTag,
  2754. irsp->ulpContext);
  2755. }
  2756. break;
  2757. }
  2758. /*
  2759. * The response IOCB has been processed. Update the ring
  2760. * pointer in SLIM. If the port response put pointer has not
  2761. * been updated, sync the pgp->rspPutInx and fetch the new port
  2762. * response put pointer.
  2763. */
  2764. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2765. if (pring->rspidx == portRspPut)
  2766. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2767. }
  2768. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2769. pring->stats.iocb_rsp_full++;
  2770. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2771. writel(status, phba->CAregaddr);
  2772. readl(phba->CAregaddr);
  2773. }
  2774. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2775. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2776. pring->stats.iocb_cmd_empty++;
  2777. /* Force update of the local copy of cmdGetInx */
  2778. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2779. lpfc_sli_resume_iocb(phba, pring);
  2780. if ((pring->lpfc_sli_cmd_available))
  2781. (pring->lpfc_sli_cmd_available) (phba, pring);
  2782. }
  2783. phba->fcp_ring_in_use = 0;
  2784. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2785. return rc;
  2786. }
  2787. /**
  2788. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2789. * @phba: Pointer to HBA context object.
  2790. * @pring: Pointer to driver SLI ring object.
  2791. * @rspiocbp: Pointer to driver response IOCB object.
  2792. *
  2793. * This function is called from the worker thread when there is a slow-path
  2794. * response IOCB to process. This function chains all the response iocbs until
  2795. * seeing the iocb with the LE bit set. The function will call
  2796. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2797. * completion of a command iocb. The function will call the
  2798. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2799. * The function frees the resources or calls the completion handler if this
  2800. * iocb is an abort completion. The function returns NULL when the response
  2801. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2802. * this function shall chain the iocb on to the iocb_continueq and return the
  2803. * response iocb passed in.
  2804. **/
  2805. static struct lpfc_iocbq *
  2806. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2807. struct lpfc_iocbq *rspiocbp)
  2808. {
  2809. struct lpfc_iocbq *saveq;
  2810. struct lpfc_iocbq *cmdiocbp;
  2811. struct lpfc_iocbq *next_iocb;
  2812. IOCB_t *irsp = NULL;
  2813. uint32_t free_saveq;
  2814. uint8_t iocb_cmd_type;
  2815. lpfc_iocb_type type;
  2816. unsigned long iflag;
  2817. int rc;
  2818. spin_lock_irqsave(&phba->hbalock, iflag);
  2819. /* First add the response iocb to the countinueq list */
  2820. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2821. pring->iocb_continueq_cnt++;
  2822. /* Now, determine whether the list is completed for processing */
  2823. irsp = &rspiocbp->iocb;
  2824. if (irsp->ulpLe) {
  2825. /*
  2826. * By default, the driver expects to free all resources
  2827. * associated with this iocb completion.
  2828. */
  2829. free_saveq = 1;
  2830. saveq = list_get_first(&pring->iocb_continueq,
  2831. struct lpfc_iocbq, list);
  2832. irsp = &(saveq->iocb);
  2833. list_del_init(&pring->iocb_continueq);
  2834. pring->iocb_continueq_cnt = 0;
  2835. pring->stats.iocb_rsp++;
  2836. /*
  2837. * If resource errors reported from HBA, reduce
  2838. * queuedepths of the SCSI device.
  2839. */
  2840. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2841. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2842. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2843. phba->lpfc_rampdown_queue_depth(phba);
  2844. spin_lock_irqsave(&phba->hbalock, iflag);
  2845. }
  2846. if (irsp->ulpStatus) {
  2847. /* Rsp ring <ringno> error: IOCB */
  2848. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2849. "0328 Rsp Ring %d error: "
  2850. "IOCB Data: "
  2851. "x%x x%x x%x x%x "
  2852. "x%x x%x x%x x%x "
  2853. "x%x x%x x%x x%x "
  2854. "x%x x%x x%x x%x\n",
  2855. pring->ringno,
  2856. irsp->un.ulpWord[0],
  2857. irsp->un.ulpWord[1],
  2858. irsp->un.ulpWord[2],
  2859. irsp->un.ulpWord[3],
  2860. irsp->un.ulpWord[4],
  2861. irsp->un.ulpWord[5],
  2862. *(((uint32_t *) irsp) + 6),
  2863. *(((uint32_t *) irsp) + 7),
  2864. *(((uint32_t *) irsp) + 8),
  2865. *(((uint32_t *) irsp) + 9),
  2866. *(((uint32_t *) irsp) + 10),
  2867. *(((uint32_t *) irsp) + 11),
  2868. *(((uint32_t *) irsp) + 12),
  2869. *(((uint32_t *) irsp) + 13),
  2870. *(((uint32_t *) irsp) + 14),
  2871. *(((uint32_t *) irsp) + 15));
  2872. }
  2873. /*
  2874. * Fetch the IOCB command type and call the correct completion
  2875. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2876. * get freed back to the lpfc_iocb_list by the discovery
  2877. * kernel thread.
  2878. */
  2879. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2880. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2881. switch (type) {
  2882. case LPFC_SOL_IOCB:
  2883. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2884. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2885. spin_lock_irqsave(&phba->hbalock, iflag);
  2886. break;
  2887. case LPFC_UNSOL_IOCB:
  2888. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2889. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2890. spin_lock_irqsave(&phba->hbalock, iflag);
  2891. if (!rc)
  2892. free_saveq = 0;
  2893. break;
  2894. case LPFC_ABORT_IOCB:
  2895. cmdiocbp = NULL;
  2896. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2897. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2898. saveq);
  2899. if (cmdiocbp) {
  2900. /* Call the specified completion routine */
  2901. if (cmdiocbp->iocb_cmpl) {
  2902. spin_unlock_irqrestore(&phba->hbalock,
  2903. iflag);
  2904. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2905. saveq);
  2906. spin_lock_irqsave(&phba->hbalock,
  2907. iflag);
  2908. } else
  2909. __lpfc_sli_release_iocbq(phba,
  2910. cmdiocbp);
  2911. }
  2912. break;
  2913. case LPFC_UNKNOWN_IOCB:
  2914. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2915. char adaptermsg[LPFC_MAX_ADPTMSG];
  2916. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2917. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2918. MAX_MSG_DATA);
  2919. dev_warn(&((phba->pcidev)->dev),
  2920. "lpfc%d: %s\n",
  2921. phba->brd_no, adaptermsg);
  2922. } else {
  2923. /* Unknown IOCB command */
  2924. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2925. "0335 Unknown IOCB "
  2926. "command Data: x%x "
  2927. "x%x x%x x%x\n",
  2928. irsp->ulpCommand,
  2929. irsp->ulpStatus,
  2930. irsp->ulpIoTag,
  2931. irsp->ulpContext);
  2932. }
  2933. break;
  2934. }
  2935. if (free_saveq) {
  2936. list_for_each_entry_safe(rspiocbp, next_iocb,
  2937. &saveq->list, list) {
  2938. list_del(&rspiocbp->list);
  2939. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2940. }
  2941. __lpfc_sli_release_iocbq(phba, saveq);
  2942. }
  2943. rspiocbp = NULL;
  2944. }
  2945. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2946. return rspiocbp;
  2947. }
  2948. /**
  2949. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2950. * @phba: Pointer to HBA context object.
  2951. * @pring: Pointer to driver SLI ring object.
  2952. * @mask: Host attention register mask for this ring.
  2953. *
  2954. * This routine wraps the actual slow_ring event process routine from the
  2955. * API jump table function pointer from the lpfc_hba struct.
  2956. **/
  2957. void
  2958. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2959. struct lpfc_sli_ring *pring, uint32_t mask)
  2960. {
  2961. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2962. }
  2963. /**
  2964. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  2965. * @phba: Pointer to HBA context object.
  2966. * @pring: Pointer to driver SLI ring object.
  2967. * @mask: Host attention register mask for this ring.
  2968. *
  2969. * This function is called from the worker thread when there is a ring event
  2970. * for non-fcp rings. The caller does not hold any lock. The function will
  2971. * remove each response iocb in the response ring and calls the handle
  2972. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2973. **/
  2974. static void
  2975. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2976. struct lpfc_sli_ring *pring, uint32_t mask)
  2977. {
  2978. struct lpfc_pgp *pgp;
  2979. IOCB_t *entry;
  2980. IOCB_t *irsp = NULL;
  2981. struct lpfc_iocbq *rspiocbp = NULL;
  2982. uint32_t portRspPut, portRspMax;
  2983. unsigned long iflag;
  2984. uint32_t status;
  2985. pgp = &phba->port_gp[pring->ringno];
  2986. spin_lock_irqsave(&phba->hbalock, iflag);
  2987. pring->stats.iocb_event++;
  2988. /*
  2989. * The next available response entry should never exceed the maximum
  2990. * entries. If it does, treat it as an adapter hardware error.
  2991. */
  2992. portRspMax = pring->numRiocb;
  2993. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2994. if (portRspPut >= portRspMax) {
  2995. /*
  2996. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2997. * rsp ring <portRspMax>
  2998. */
  2999. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3000. "0303 Ring %d handler: portRspPut %d "
  3001. "is bigger than rsp ring %d\n",
  3002. pring->ringno, portRspPut, portRspMax);
  3003. phba->link_state = LPFC_HBA_ERROR;
  3004. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3005. phba->work_hs = HS_FFER3;
  3006. lpfc_handle_eratt(phba);
  3007. return;
  3008. }
  3009. rmb();
  3010. while (pring->rspidx != portRspPut) {
  3011. /*
  3012. * Build a completion list and call the appropriate handler.
  3013. * The process is to get the next available response iocb, get
  3014. * a free iocb from the list, copy the response data into the
  3015. * free iocb, insert to the continuation list, and update the
  3016. * next response index to slim. This process makes response
  3017. * iocb's in the ring available to DMA as fast as possible but
  3018. * pays a penalty for a copy operation. Since the iocb is
  3019. * only 32 bytes, this penalty is considered small relative to
  3020. * the PCI reads for register values and a slim write. When
  3021. * the ulpLe field is set, the entire Command has been
  3022. * received.
  3023. */
  3024. entry = lpfc_resp_iocb(phba, pring);
  3025. phba->last_completion_time = jiffies;
  3026. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3027. if (rspiocbp == NULL) {
  3028. printk(KERN_ERR "%s: out of buffers! Failing "
  3029. "completion.\n", __func__);
  3030. break;
  3031. }
  3032. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3033. phba->iocb_rsp_size);
  3034. irsp = &rspiocbp->iocb;
  3035. if (++pring->rspidx >= portRspMax)
  3036. pring->rspidx = 0;
  3037. if (pring->ringno == LPFC_ELS_RING) {
  3038. lpfc_debugfs_slow_ring_trc(phba,
  3039. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3040. *(((uint32_t *) irsp) + 4),
  3041. *(((uint32_t *) irsp) + 6),
  3042. *(((uint32_t *) irsp) + 7));
  3043. }
  3044. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  3045. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3046. /* Handle the response IOCB */
  3047. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3048. spin_lock_irqsave(&phba->hbalock, iflag);
  3049. /*
  3050. * If the port response put pointer has not been updated, sync
  3051. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3052. * response put pointer.
  3053. */
  3054. if (pring->rspidx == portRspPut) {
  3055. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3056. }
  3057. } /* while (pring->rspidx != portRspPut) */
  3058. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3059. /* At least one response entry has been freed */
  3060. pring->stats.iocb_rsp_full++;
  3061. /* SET RxRE_RSP in Chip Att register */
  3062. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3063. writel(status, phba->CAregaddr);
  3064. readl(phba->CAregaddr); /* flush */
  3065. }
  3066. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3067. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3068. pring->stats.iocb_cmd_empty++;
  3069. /* Force update of the local copy of cmdGetInx */
  3070. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3071. lpfc_sli_resume_iocb(phba, pring);
  3072. if ((pring->lpfc_sli_cmd_available))
  3073. (pring->lpfc_sli_cmd_available) (phba, pring);
  3074. }
  3075. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3076. return;
  3077. }
  3078. /**
  3079. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3080. * @phba: Pointer to HBA context object.
  3081. * @pring: Pointer to driver SLI ring object.
  3082. * @mask: Host attention register mask for this ring.
  3083. *
  3084. * This function is called from the worker thread when there is a pending
  3085. * ELS response iocb on the driver internal slow-path response iocb worker
  3086. * queue. The caller does not hold any lock. The function will remove each
  3087. * response iocb from the response worker queue and calls the handle
  3088. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3089. **/
  3090. static void
  3091. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3092. struct lpfc_sli_ring *pring, uint32_t mask)
  3093. {
  3094. struct lpfc_iocbq *irspiocbq;
  3095. struct hbq_dmabuf *dmabuf;
  3096. struct lpfc_cq_event *cq_event;
  3097. unsigned long iflag;
  3098. spin_lock_irqsave(&phba->hbalock, iflag);
  3099. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3100. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3101. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3102. /* Get the response iocb from the head of work queue */
  3103. spin_lock_irqsave(&phba->hbalock, iflag);
  3104. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3105. cq_event, struct lpfc_cq_event, list);
  3106. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3107. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3108. case CQE_CODE_COMPL_WQE:
  3109. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3110. cq_event);
  3111. /* Translate ELS WCQE to response IOCBQ */
  3112. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3113. irspiocbq);
  3114. if (irspiocbq)
  3115. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3116. irspiocbq);
  3117. break;
  3118. case CQE_CODE_RECEIVE:
  3119. case CQE_CODE_RECEIVE_V1:
  3120. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3121. cq_event);
  3122. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3123. break;
  3124. default:
  3125. break;
  3126. }
  3127. }
  3128. }
  3129. /**
  3130. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3131. * @phba: Pointer to HBA context object.
  3132. * @pring: Pointer to driver SLI ring object.
  3133. *
  3134. * This function aborts all iocbs in the given ring and frees all the iocb
  3135. * objects in txq. This function issues an abort iocb for all the iocb commands
  3136. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3137. * the return of this function. The caller is not required to hold any locks.
  3138. **/
  3139. void
  3140. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3141. {
  3142. LIST_HEAD(completions);
  3143. struct lpfc_iocbq *iocb, *next_iocb;
  3144. if (pring->ringno == LPFC_ELS_RING) {
  3145. lpfc_fabric_abort_hba(phba);
  3146. }
  3147. /* Error everything on txq and txcmplq
  3148. * First do the txq.
  3149. */
  3150. spin_lock_irq(&phba->hbalock);
  3151. list_splice_init(&pring->txq, &completions);
  3152. pring->txq_cnt = 0;
  3153. /* Next issue ABTS for everything on the txcmplq */
  3154. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3155. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3156. spin_unlock_irq(&phba->hbalock);
  3157. /* Cancel all the IOCBs from the completions list */
  3158. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3159. IOERR_SLI_ABORTED);
  3160. }
  3161. /**
  3162. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3163. * @phba: Pointer to HBA context object.
  3164. *
  3165. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3166. * objects in txq and txcmplq. This function will not issue abort iocbs
  3167. * for all the iocb commands in txcmplq, they will just be returned with
  3168. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3169. * slot has been permanently disabled.
  3170. **/
  3171. void
  3172. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3173. {
  3174. LIST_HEAD(txq);
  3175. LIST_HEAD(txcmplq);
  3176. struct lpfc_sli *psli = &phba->sli;
  3177. struct lpfc_sli_ring *pring;
  3178. /* Currently, only one fcp ring */
  3179. pring = &psli->ring[psli->fcp_ring];
  3180. spin_lock_irq(&phba->hbalock);
  3181. /* Retrieve everything on txq */
  3182. list_splice_init(&pring->txq, &txq);
  3183. pring->txq_cnt = 0;
  3184. /* Retrieve everything on the txcmplq */
  3185. list_splice_init(&pring->txcmplq, &txcmplq);
  3186. pring->txcmplq_cnt = 0;
  3187. spin_unlock_irq(&phba->hbalock);
  3188. /* Flush the txq */
  3189. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3190. IOERR_SLI_DOWN);
  3191. /* Flush the txcmpq */
  3192. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3193. IOERR_SLI_DOWN);
  3194. }
  3195. /**
  3196. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3197. * @phba: Pointer to HBA context object.
  3198. * @mask: Bit mask to be checked.
  3199. *
  3200. * This function reads the host status register and compares
  3201. * with the provided bit mask to check if HBA completed
  3202. * the restart. This function will wait in a loop for the
  3203. * HBA to complete restart. If the HBA does not restart within
  3204. * 15 iterations, the function will reset the HBA again. The
  3205. * function returns 1 when HBA fail to restart otherwise returns
  3206. * zero.
  3207. **/
  3208. static int
  3209. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3210. {
  3211. uint32_t status;
  3212. int i = 0;
  3213. int retval = 0;
  3214. /* Read the HBA Host Status Register */
  3215. if (lpfc_readl(phba->HSregaddr, &status))
  3216. return 1;
  3217. /*
  3218. * Check status register every 100ms for 5 retries, then every
  3219. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3220. * every 2.5 sec for 4.
  3221. * Break our of the loop if errors occurred during init.
  3222. */
  3223. while (((status & mask) != mask) &&
  3224. !(status & HS_FFERM) &&
  3225. i++ < 20) {
  3226. if (i <= 5)
  3227. msleep(10);
  3228. else if (i <= 10)
  3229. msleep(500);
  3230. else
  3231. msleep(2500);
  3232. if (i == 15) {
  3233. /* Do post */
  3234. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3235. lpfc_sli_brdrestart(phba);
  3236. }
  3237. /* Read the HBA Host Status Register */
  3238. if (lpfc_readl(phba->HSregaddr, &status)) {
  3239. retval = 1;
  3240. break;
  3241. }
  3242. }
  3243. /* Check to see if any errors occurred during init */
  3244. if ((status & HS_FFERM) || (i >= 20)) {
  3245. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3246. "2751 Adapter failed to restart, "
  3247. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3248. status,
  3249. readl(phba->MBslimaddr + 0xa8),
  3250. readl(phba->MBslimaddr + 0xac));
  3251. phba->link_state = LPFC_HBA_ERROR;
  3252. retval = 1;
  3253. }
  3254. return retval;
  3255. }
  3256. /**
  3257. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3258. * @phba: Pointer to HBA context object.
  3259. * @mask: Bit mask to be checked.
  3260. *
  3261. * This function checks the host status register to check if HBA is
  3262. * ready. This function will wait in a loop for the HBA to be ready
  3263. * If the HBA is not ready , the function will will reset the HBA PCI
  3264. * function again. The function returns 1 when HBA fail to be ready
  3265. * otherwise returns zero.
  3266. **/
  3267. static int
  3268. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3269. {
  3270. uint32_t status;
  3271. int retval = 0;
  3272. /* Read the HBA Host Status Register */
  3273. status = lpfc_sli4_post_status_check(phba);
  3274. if (status) {
  3275. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3276. lpfc_sli_brdrestart(phba);
  3277. status = lpfc_sli4_post_status_check(phba);
  3278. }
  3279. /* Check to see if any errors occurred during init */
  3280. if (status) {
  3281. phba->link_state = LPFC_HBA_ERROR;
  3282. retval = 1;
  3283. } else
  3284. phba->sli4_hba.intr_enable = 0;
  3285. return retval;
  3286. }
  3287. /**
  3288. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3289. * @phba: Pointer to HBA context object.
  3290. * @mask: Bit mask to be checked.
  3291. *
  3292. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3293. * from the API jump table function pointer from the lpfc_hba struct.
  3294. **/
  3295. int
  3296. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3297. {
  3298. return phba->lpfc_sli_brdready(phba, mask);
  3299. }
  3300. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3301. /**
  3302. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3303. * @phba: Pointer to HBA context object.
  3304. *
  3305. * This function is called before resetting an HBA. This function is called
  3306. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3307. **/
  3308. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3309. {
  3310. uint32_t __iomem *resp_buf;
  3311. uint32_t __iomem *mbox_buf;
  3312. volatile uint32_t mbox;
  3313. uint32_t hc_copy, ha_copy, resp_data;
  3314. int i;
  3315. uint8_t hdrtype;
  3316. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3317. if (hdrtype != 0x80 ||
  3318. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3319. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3320. return;
  3321. /*
  3322. * Tell the other part of the chip to suspend temporarily all
  3323. * its DMA activity.
  3324. */
  3325. resp_buf = phba->MBslimaddr;
  3326. /* Disable the error attention */
  3327. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3328. return;
  3329. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3330. readl(phba->HCregaddr); /* flush */
  3331. phba->link_flag |= LS_IGNORE_ERATT;
  3332. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3333. return;
  3334. if (ha_copy & HA_ERATT) {
  3335. /* Clear Chip error bit */
  3336. writel(HA_ERATT, phba->HAregaddr);
  3337. phba->pport->stopped = 1;
  3338. }
  3339. mbox = 0;
  3340. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3341. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3342. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3343. mbox_buf = phba->MBslimaddr;
  3344. writel(mbox, mbox_buf);
  3345. for (i = 0; i < 50; i++) {
  3346. if (lpfc_readl((resp_buf + 1), &resp_data))
  3347. return;
  3348. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3349. mdelay(1);
  3350. else
  3351. break;
  3352. }
  3353. resp_data = 0;
  3354. if (lpfc_readl((resp_buf + 1), &resp_data))
  3355. return;
  3356. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3357. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3358. phba->pport->stopped)
  3359. goto restore_hc;
  3360. else
  3361. goto clear_errat;
  3362. }
  3363. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3364. resp_data = 0;
  3365. for (i = 0; i < 500; i++) {
  3366. if (lpfc_readl(resp_buf, &resp_data))
  3367. return;
  3368. if (resp_data != mbox)
  3369. mdelay(1);
  3370. else
  3371. break;
  3372. }
  3373. clear_errat:
  3374. while (++i < 500) {
  3375. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3376. return;
  3377. if (!(ha_copy & HA_ERATT))
  3378. mdelay(1);
  3379. else
  3380. break;
  3381. }
  3382. if (readl(phba->HAregaddr) & HA_ERATT) {
  3383. writel(HA_ERATT, phba->HAregaddr);
  3384. phba->pport->stopped = 1;
  3385. }
  3386. restore_hc:
  3387. phba->link_flag &= ~LS_IGNORE_ERATT;
  3388. writel(hc_copy, phba->HCregaddr);
  3389. readl(phba->HCregaddr); /* flush */
  3390. }
  3391. /**
  3392. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3393. * @phba: Pointer to HBA context object.
  3394. *
  3395. * This function issues a kill_board mailbox command and waits for
  3396. * the error attention interrupt. This function is called for stopping
  3397. * the firmware processing. The caller is not required to hold any
  3398. * locks. This function calls lpfc_hba_down_post function to free
  3399. * any pending commands after the kill. The function will return 1 when it
  3400. * fails to kill the board else will return 0.
  3401. **/
  3402. int
  3403. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3404. {
  3405. struct lpfc_sli *psli;
  3406. LPFC_MBOXQ_t *pmb;
  3407. uint32_t status;
  3408. uint32_t ha_copy;
  3409. int retval;
  3410. int i = 0;
  3411. psli = &phba->sli;
  3412. /* Kill HBA */
  3413. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3414. "0329 Kill HBA Data: x%x x%x\n",
  3415. phba->pport->port_state, psli->sli_flag);
  3416. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3417. if (!pmb)
  3418. return 1;
  3419. /* Disable the error attention */
  3420. spin_lock_irq(&phba->hbalock);
  3421. if (lpfc_readl(phba->HCregaddr, &status)) {
  3422. spin_unlock_irq(&phba->hbalock);
  3423. mempool_free(pmb, phba->mbox_mem_pool);
  3424. return 1;
  3425. }
  3426. status &= ~HC_ERINT_ENA;
  3427. writel(status, phba->HCregaddr);
  3428. readl(phba->HCregaddr); /* flush */
  3429. phba->link_flag |= LS_IGNORE_ERATT;
  3430. spin_unlock_irq(&phba->hbalock);
  3431. lpfc_kill_board(phba, pmb);
  3432. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3433. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3434. if (retval != MBX_SUCCESS) {
  3435. if (retval != MBX_BUSY)
  3436. mempool_free(pmb, phba->mbox_mem_pool);
  3437. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3438. "2752 KILL_BOARD command failed retval %d\n",
  3439. retval);
  3440. spin_lock_irq(&phba->hbalock);
  3441. phba->link_flag &= ~LS_IGNORE_ERATT;
  3442. spin_unlock_irq(&phba->hbalock);
  3443. return 1;
  3444. }
  3445. spin_lock_irq(&phba->hbalock);
  3446. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3447. spin_unlock_irq(&phba->hbalock);
  3448. mempool_free(pmb, phba->mbox_mem_pool);
  3449. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3450. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3451. * 3 seconds we still set HBA_ERROR state because the status of the
  3452. * board is now undefined.
  3453. */
  3454. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3455. return 1;
  3456. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3457. mdelay(100);
  3458. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3459. return 1;
  3460. }
  3461. del_timer_sync(&psli->mbox_tmo);
  3462. if (ha_copy & HA_ERATT) {
  3463. writel(HA_ERATT, phba->HAregaddr);
  3464. phba->pport->stopped = 1;
  3465. }
  3466. spin_lock_irq(&phba->hbalock);
  3467. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3468. psli->mbox_active = NULL;
  3469. phba->link_flag &= ~LS_IGNORE_ERATT;
  3470. spin_unlock_irq(&phba->hbalock);
  3471. lpfc_hba_down_post(phba);
  3472. phba->link_state = LPFC_HBA_ERROR;
  3473. return ha_copy & HA_ERATT ? 0 : 1;
  3474. }
  3475. /**
  3476. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3477. * @phba: Pointer to HBA context object.
  3478. *
  3479. * This function resets the HBA by writing HC_INITFF to the control
  3480. * register. After the HBA resets, this function resets all the iocb ring
  3481. * indices. This function disables PCI layer parity checking during
  3482. * the reset.
  3483. * This function returns 0 always.
  3484. * The caller is not required to hold any locks.
  3485. **/
  3486. int
  3487. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3488. {
  3489. struct lpfc_sli *psli;
  3490. struct lpfc_sli_ring *pring;
  3491. uint16_t cfg_value;
  3492. int i;
  3493. psli = &phba->sli;
  3494. /* Reset HBA */
  3495. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3496. "0325 Reset HBA Data: x%x x%x\n",
  3497. phba->pport->port_state, psli->sli_flag);
  3498. /* perform board reset */
  3499. phba->fc_eventTag = 0;
  3500. phba->link_events = 0;
  3501. phba->pport->fc_myDID = 0;
  3502. phba->pport->fc_prevDID = 0;
  3503. /* Turn off parity checking and serr during the physical reset */
  3504. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3505. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3506. (cfg_value &
  3507. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3508. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3509. /* Now toggle INITFF bit in the Host Control Register */
  3510. writel(HC_INITFF, phba->HCregaddr);
  3511. mdelay(1);
  3512. readl(phba->HCregaddr); /* flush */
  3513. writel(0, phba->HCregaddr);
  3514. readl(phba->HCregaddr); /* flush */
  3515. /* Restore PCI cmd register */
  3516. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3517. /* Initialize relevant SLI info */
  3518. for (i = 0; i < psli->num_rings; i++) {
  3519. pring = &psli->ring[i];
  3520. pring->flag = 0;
  3521. pring->rspidx = 0;
  3522. pring->next_cmdidx = 0;
  3523. pring->local_getidx = 0;
  3524. pring->cmdidx = 0;
  3525. pring->missbufcnt = 0;
  3526. }
  3527. phba->link_state = LPFC_WARM_START;
  3528. return 0;
  3529. }
  3530. /**
  3531. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3532. * @phba: Pointer to HBA context object.
  3533. *
  3534. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3535. * checking during resets the device. The caller is not required to hold
  3536. * any locks.
  3537. *
  3538. * This function returns 0 always.
  3539. **/
  3540. int
  3541. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3542. {
  3543. struct lpfc_sli *psli = &phba->sli;
  3544. uint16_t cfg_value;
  3545. /* Reset HBA */
  3546. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3547. "0295 Reset HBA Data: x%x x%x\n",
  3548. phba->pport->port_state, psli->sli_flag);
  3549. /* perform board reset */
  3550. phba->fc_eventTag = 0;
  3551. phba->link_events = 0;
  3552. phba->pport->fc_myDID = 0;
  3553. phba->pport->fc_prevDID = 0;
  3554. spin_lock_irq(&phba->hbalock);
  3555. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3556. phba->fcf.fcf_flag = 0;
  3557. spin_unlock_irq(&phba->hbalock);
  3558. /* Now physically reset the device */
  3559. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3560. "0389 Performing PCI function reset!\n");
  3561. /* Turn off parity checking and serr during the physical reset */
  3562. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3563. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3564. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3565. /* Perform FCoE PCI function reset */
  3566. lpfc_sli4_queue_destroy(phba);
  3567. lpfc_pci_function_reset(phba);
  3568. /* Restore PCI cmd register */
  3569. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3570. return 0;
  3571. }
  3572. /**
  3573. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3574. * @phba: Pointer to HBA context object.
  3575. *
  3576. * This function is called in the SLI initialization code path to
  3577. * restart the HBA. The caller is not required to hold any lock.
  3578. * This function writes MBX_RESTART mailbox command to the SLIM and
  3579. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3580. * function to free any pending commands. The function enables
  3581. * POST only during the first initialization. The function returns zero.
  3582. * The function does not guarantee completion of MBX_RESTART mailbox
  3583. * command before the return of this function.
  3584. **/
  3585. static int
  3586. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3587. {
  3588. MAILBOX_t *mb;
  3589. struct lpfc_sli *psli;
  3590. volatile uint32_t word0;
  3591. void __iomem *to_slim;
  3592. uint32_t hba_aer_enabled;
  3593. spin_lock_irq(&phba->hbalock);
  3594. /* Take PCIe device Advanced Error Reporting (AER) state */
  3595. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3596. psli = &phba->sli;
  3597. /* Restart HBA */
  3598. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3599. "0337 Restart HBA Data: x%x x%x\n",
  3600. phba->pport->port_state, psli->sli_flag);
  3601. word0 = 0;
  3602. mb = (MAILBOX_t *) &word0;
  3603. mb->mbxCommand = MBX_RESTART;
  3604. mb->mbxHc = 1;
  3605. lpfc_reset_barrier(phba);
  3606. to_slim = phba->MBslimaddr;
  3607. writel(*(uint32_t *) mb, to_slim);
  3608. readl(to_slim); /* flush */
  3609. /* Only skip post after fc_ffinit is completed */
  3610. if (phba->pport->port_state)
  3611. word0 = 1; /* This is really setting up word1 */
  3612. else
  3613. word0 = 0; /* This is really setting up word1 */
  3614. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3615. writel(*(uint32_t *) mb, to_slim);
  3616. readl(to_slim); /* flush */
  3617. lpfc_sli_brdreset(phba);
  3618. phba->pport->stopped = 0;
  3619. phba->link_state = LPFC_INIT_START;
  3620. phba->hba_flag = 0;
  3621. spin_unlock_irq(&phba->hbalock);
  3622. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3623. psli->stats_start = get_seconds();
  3624. /* Give the INITFF and Post time to settle. */
  3625. mdelay(100);
  3626. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3627. if (hba_aer_enabled)
  3628. pci_disable_pcie_error_reporting(phba->pcidev);
  3629. lpfc_hba_down_post(phba);
  3630. return 0;
  3631. }
  3632. /**
  3633. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3634. * @phba: Pointer to HBA context object.
  3635. *
  3636. * This function is called in the SLI initialization code path to restart
  3637. * a SLI4 HBA. The caller is not required to hold any lock.
  3638. * At the end of the function, it calls lpfc_hba_down_post function to
  3639. * free any pending commands.
  3640. **/
  3641. static int
  3642. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3643. {
  3644. struct lpfc_sli *psli = &phba->sli;
  3645. uint32_t hba_aer_enabled;
  3646. /* Restart HBA */
  3647. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3648. "0296 Restart HBA Data: x%x x%x\n",
  3649. phba->pport->port_state, psli->sli_flag);
  3650. /* Take PCIe device Advanced Error Reporting (AER) state */
  3651. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3652. lpfc_sli4_brdreset(phba);
  3653. spin_lock_irq(&phba->hbalock);
  3654. phba->pport->stopped = 0;
  3655. phba->link_state = LPFC_INIT_START;
  3656. phba->hba_flag = 0;
  3657. spin_unlock_irq(&phba->hbalock);
  3658. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3659. psli->stats_start = get_seconds();
  3660. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3661. if (hba_aer_enabled)
  3662. pci_disable_pcie_error_reporting(phba->pcidev);
  3663. lpfc_hba_down_post(phba);
  3664. return 0;
  3665. }
  3666. /**
  3667. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3668. * @phba: Pointer to HBA context object.
  3669. *
  3670. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3671. * API jump table function pointer from the lpfc_hba struct.
  3672. **/
  3673. int
  3674. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3675. {
  3676. return phba->lpfc_sli_brdrestart(phba);
  3677. }
  3678. /**
  3679. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3680. * @phba: Pointer to HBA context object.
  3681. *
  3682. * This function is called after a HBA restart to wait for successful
  3683. * restart of the HBA. Successful restart of the HBA is indicated by
  3684. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3685. * iteration, the function will restart the HBA again. The function returns
  3686. * zero if HBA successfully restarted else returns negative error code.
  3687. **/
  3688. static int
  3689. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3690. {
  3691. uint32_t status, i = 0;
  3692. /* Read the HBA Host Status Register */
  3693. if (lpfc_readl(phba->HSregaddr, &status))
  3694. return -EIO;
  3695. /* Check status register to see what current state is */
  3696. i = 0;
  3697. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3698. /* Check every 10ms for 10 retries, then every 100ms for 90
  3699. * retries, then every 1 sec for 50 retires for a total of
  3700. * ~60 seconds before reset the board again and check every
  3701. * 1 sec for 50 retries. The up to 60 seconds before the
  3702. * board ready is required by the Falcon FIPS zeroization
  3703. * complete, and any reset the board in between shall cause
  3704. * restart of zeroization, further delay the board ready.
  3705. */
  3706. if (i++ >= 200) {
  3707. /* Adapter failed to init, timeout, status reg
  3708. <status> */
  3709. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3710. "0436 Adapter failed to init, "
  3711. "timeout, status reg x%x, "
  3712. "FW Data: A8 x%x AC x%x\n", status,
  3713. readl(phba->MBslimaddr + 0xa8),
  3714. readl(phba->MBslimaddr + 0xac));
  3715. phba->link_state = LPFC_HBA_ERROR;
  3716. return -ETIMEDOUT;
  3717. }
  3718. /* Check to see if any errors occurred during init */
  3719. if (status & HS_FFERM) {
  3720. /* ERROR: During chipset initialization */
  3721. /* Adapter failed to init, chipset, status reg
  3722. <status> */
  3723. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3724. "0437 Adapter failed to init, "
  3725. "chipset, status reg x%x, "
  3726. "FW Data: A8 x%x AC x%x\n", status,
  3727. readl(phba->MBslimaddr + 0xa8),
  3728. readl(phba->MBslimaddr + 0xac));
  3729. phba->link_state = LPFC_HBA_ERROR;
  3730. return -EIO;
  3731. }
  3732. if (i <= 10)
  3733. msleep(10);
  3734. else if (i <= 100)
  3735. msleep(100);
  3736. else
  3737. msleep(1000);
  3738. if (i == 150) {
  3739. /* Do post */
  3740. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3741. lpfc_sli_brdrestart(phba);
  3742. }
  3743. /* Read the HBA Host Status Register */
  3744. if (lpfc_readl(phba->HSregaddr, &status))
  3745. return -EIO;
  3746. }
  3747. /* Check to see if any errors occurred during init */
  3748. if (status & HS_FFERM) {
  3749. /* ERROR: During chipset initialization */
  3750. /* Adapter failed to init, chipset, status reg <status> */
  3751. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3752. "0438 Adapter failed to init, chipset, "
  3753. "status reg x%x, "
  3754. "FW Data: A8 x%x AC x%x\n", status,
  3755. readl(phba->MBslimaddr + 0xa8),
  3756. readl(phba->MBslimaddr + 0xac));
  3757. phba->link_state = LPFC_HBA_ERROR;
  3758. return -EIO;
  3759. }
  3760. /* Clear all interrupt enable conditions */
  3761. writel(0, phba->HCregaddr);
  3762. readl(phba->HCregaddr); /* flush */
  3763. /* setup host attn register */
  3764. writel(0xffffffff, phba->HAregaddr);
  3765. readl(phba->HAregaddr); /* flush */
  3766. return 0;
  3767. }
  3768. /**
  3769. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3770. *
  3771. * This function calculates and returns the number of HBQs required to be
  3772. * configured.
  3773. **/
  3774. int
  3775. lpfc_sli_hbq_count(void)
  3776. {
  3777. return ARRAY_SIZE(lpfc_hbq_defs);
  3778. }
  3779. /**
  3780. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3781. *
  3782. * This function adds the number of hbq entries in every HBQ to get
  3783. * the total number of hbq entries required for the HBA and returns
  3784. * the total count.
  3785. **/
  3786. static int
  3787. lpfc_sli_hbq_entry_count(void)
  3788. {
  3789. int hbq_count = lpfc_sli_hbq_count();
  3790. int count = 0;
  3791. int i;
  3792. for (i = 0; i < hbq_count; ++i)
  3793. count += lpfc_hbq_defs[i]->entry_count;
  3794. return count;
  3795. }
  3796. /**
  3797. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3798. *
  3799. * This function calculates amount of memory required for all hbq entries
  3800. * to be configured and returns the total memory required.
  3801. **/
  3802. int
  3803. lpfc_sli_hbq_size(void)
  3804. {
  3805. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3806. }
  3807. /**
  3808. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3809. * @phba: Pointer to HBA context object.
  3810. *
  3811. * This function is called during the SLI initialization to configure
  3812. * all the HBQs and post buffers to the HBQ. The caller is not
  3813. * required to hold any locks. This function will return zero if successful
  3814. * else it will return negative error code.
  3815. **/
  3816. static int
  3817. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3818. {
  3819. int hbq_count = lpfc_sli_hbq_count();
  3820. LPFC_MBOXQ_t *pmb;
  3821. MAILBOX_t *pmbox;
  3822. uint32_t hbqno;
  3823. uint32_t hbq_entry_index;
  3824. /* Get a Mailbox buffer to setup mailbox
  3825. * commands for HBA initialization
  3826. */
  3827. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3828. if (!pmb)
  3829. return -ENOMEM;
  3830. pmbox = &pmb->u.mb;
  3831. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3832. phba->link_state = LPFC_INIT_MBX_CMDS;
  3833. phba->hbq_in_use = 1;
  3834. hbq_entry_index = 0;
  3835. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3836. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3837. phba->hbqs[hbqno].hbqPutIdx = 0;
  3838. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3839. phba->hbqs[hbqno].entry_count =
  3840. lpfc_hbq_defs[hbqno]->entry_count;
  3841. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3842. hbq_entry_index, pmb);
  3843. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3844. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3845. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3846. mbxStatus <status>, ring <num> */
  3847. lpfc_printf_log(phba, KERN_ERR,
  3848. LOG_SLI | LOG_VPORT,
  3849. "1805 Adapter failed to init. "
  3850. "Data: x%x x%x x%x\n",
  3851. pmbox->mbxCommand,
  3852. pmbox->mbxStatus, hbqno);
  3853. phba->link_state = LPFC_HBA_ERROR;
  3854. mempool_free(pmb, phba->mbox_mem_pool);
  3855. return -ENXIO;
  3856. }
  3857. }
  3858. phba->hbq_count = hbq_count;
  3859. mempool_free(pmb, phba->mbox_mem_pool);
  3860. /* Initially populate or replenish the HBQs */
  3861. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3862. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3863. return 0;
  3864. }
  3865. /**
  3866. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3867. * @phba: Pointer to HBA context object.
  3868. *
  3869. * This function is called during the SLI initialization to configure
  3870. * all the HBQs and post buffers to the HBQ. The caller is not
  3871. * required to hold any locks. This function will return zero if successful
  3872. * else it will return negative error code.
  3873. **/
  3874. static int
  3875. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3876. {
  3877. phba->hbq_in_use = 1;
  3878. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3879. phba->hbq_count = 1;
  3880. /* Initially populate or replenish the HBQs */
  3881. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3882. return 0;
  3883. }
  3884. /**
  3885. * lpfc_sli_config_port - Issue config port mailbox command
  3886. * @phba: Pointer to HBA context object.
  3887. * @sli_mode: sli mode - 2/3
  3888. *
  3889. * This function is called by the sli intialization code path
  3890. * to issue config_port mailbox command. This function restarts the
  3891. * HBA firmware and issues a config_port mailbox command to configure
  3892. * the SLI interface in the sli mode specified by sli_mode
  3893. * variable. The caller is not required to hold any locks.
  3894. * The function returns 0 if successful, else returns negative error
  3895. * code.
  3896. **/
  3897. int
  3898. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3899. {
  3900. LPFC_MBOXQ_t *pmb;
  3901. uint32_t resetcount = 0, rc = 0, done = 0;
  3902. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3903. if (!pmb) {
  3904. phba->link_state = LPFC_HBA_ERROR;
  3905. return -ENOMEM;
  3906. }
  3907. phba->sli_rev = sli_mode;
  3908. while (resetcount < 2 && !done) {
  3909. spin_lock_irq(&phba->hbalock);
  3910. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3911. spin_unlock_irq(&phba->hbalock);
  3912. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3913. lpfc_sli_brdrestart(phba);
  3914. rc = lpfc_sli_chipset_init(phba);
  3915. if (rc)
  3916. break;
  3917. spin_lock_irq(&phba->hbalock);
  3918. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3919. spin_unlock_irq(&phba->hbalock);
  3920. resetcount++;
  3921. /* Call pre CONFIG_PORT mailbox command initialization. A
  3922. * value of 0 means the call was successful. Any other
  3923. * nonzero value is a failure, but if ERESTART is returned,
  3924. * the driver may reset the HBA and try again.
  3925. */
  3926. rc = lpfc_config_port_prep(phba);
  3927. if (rc == -ERESTART) {
  3928. phba->link_state = LPFC_LINK_UNKNOWN;
  3929. continue;
  3930. } else if (rc)
  3931. break;
  3932. phba->link_state = LPFC_INIT_MBX_CMDS;
  3933. lpfc_config_port(phba, pmb);
  3934. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3935. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3936. LPFC_SLI3_HBQ_ENABLED |
  3937. LPFC_SLI3_CRP_ENABLED |
  3938. LPFC_SLI3_BG_ENABLED |
  3939. LPFC_SLI3_DSS_ENABLED);
  3940. if (rc != MBX_SUCCESS) {
  3941. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3942. "0442 Adapter failed to init, mbxCmd x%x "
  3943. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3944. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3945. spin_lock_irq(&phba->hbalock);
  3946. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3947. spin_unlock_irq(&phba->hbalock);
  3948. rc = -ENXIO;
  3949. } else {
  3950. /* Allow asynchronous mailbox command to go through */
  3951. spin_lock_irq(&phba->hbalock);
  3952. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3953. spin_unlock_irq(&phba->hbalock);
  3954. done = 1;
  3955. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  3956. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  3957. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3958. "3110 Port did not grant ASABT\n");
  3959. }
  3960. }
  3961. if (!done) {
  3962. rc = -EINVAL;
  3963. goto do_prep_failed;
  3964. }
  3965. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3966. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3967. rc = -ENXIO;
  3968. goto do_prep_failed;
  3969. }
  3970. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3971. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3972. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3973. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3974. phba->max_vpi : phba->max_vports;
  3975. } else
  3976. phba->max_vpi = 0;
  3977. phba->fips_level = 0;
  3978. phba->fips_spec_rev = 0;
  3979. if (pmb->u.mb.un.varCfgPort.gdss) {
  3980. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3981. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  3982. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  3983. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3984. "2850 Security Crypto Active. FIPS x%d "
  3985. "(Spec Rev: x%d)",
  3986. phba->fips_level, phba->fips_spec_rev);
  3987. }
  3988. if (pmb->u.mb.un.varCfgPort.sec_err) {
  3989. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3990. "2856 Config Port Security Crypto "
  3991. "Error: x%x ",
  3992. pmb->u.mb.un.varCfgPort.sec_err);
  3993. }
  3994. if (pmb->u.mb.un.varCfgPort.gerbm)
  3995. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  3996. if (pmb->u.mb.un.varCfgPort.gcrp)
  3997. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  3998. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  3999. phba->port_gp = phba->mbox->us.s3_pgp.port;
  4000. if (phba->cfg_enable_bg) {
  4001. if (pmb->u.mb.un.varCfgPort.gbg)
  4002. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  4003. else
  4004. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4005. "0443 Adapter did not grant "
  4006. "BlockGuard\n");
  4007. }
  4008. } else {
  4009. phba->hbq_get = NULL;
  4010. phba->port_gp = phba->mbox->us.s2.port;
  4011. phba->max_vpi = 0;
  4012. }
  4013. do_prep_failed:
  4014. mempool_free(pmb, phba->mbox_mem_pool);
  4015. return rc;
  4016. }
  4017. /**
  4018. * lpfc_sli_hba_setup - SLI intialization function
  4019. * @phba: Pointer to HBA context object.
  4020. *
  4021. * This function is the main SLI intialization function. This function
  4022. * is called by the HBA intialization code, HBA reset code and HBA
  4023. * error attention handler code. Caller is not required to hold any
  4024. * locks. This function issues config_port mailbox command to configure
  4025. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4026. * calls the config_port_post function to issue init_link mailbox
  4027. * command and to start the discovery. The function will return zero
  4028. * if successful, else it will return negative error code.
  4029. **/
  4030. int
  4031. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4032. {
  4033. uint32_t rc;
  4034. int mode = 3, i;
  4035. int longs;
  4036. switch (lpfc_sli_mode) {
  4037. case 2:
  4038. if (phba->cfg_enable_npiv) {
  4039. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4040. "1824 NPIV enabled: Override lpfc_sli_mode "
  4041. "parameter (%d) to auto (0).\n",
  4042. lpfc_sli_mode);
  4043. break;
  4044. }
  4045. mode = 2;
  4046. break;
  4047. case 0:
  4048. case 3:
  4049. break;
  4050. default:
  4051. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4052. "1819 Unrecognized lpfc_sli_mode "
  4053. "parameter: %d.\n", lpfc_sli_mode);
  4054. break;
  4055. }
  4056. rc = lpfc_sli_config_port(phba, mode);
  4057. if (rc && lpfc_sli_mode == 3)
  4058. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4059. "1820 Unable to select SLI-3. "
  4060. "Not supported by adapter.\n");
  4061. if (rc && mode != 2)
  4062. rc = lpfc_sli_config_port(phba, 2);
  4063. if (rc)
  4064. goto lpfc_sli_hba_setup_error;
  4065. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4066. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4067. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4068. if (!rc) {
  4069. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4070. "2709 This device supports "
  4071. "Advanced Error Reporting (AER)\n");
  4072. spin_lock_irq(&phba->hbalock);
  4073. phba->hba_flag |= HBA_AER_ENABLED;
  4074. spin_unlock_irq(&phba->hbalock);
  4075. } else {
  4076. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4077. "2708 This device does not support "
  4078. "Advanced Error Reporting (AER)\n");
  4079. phba->cfg_aer_support = 0;
  4080. }
  4081. }
  4082. if (phba->sli_rev == 3) {
  4083. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4084. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4085. } else {
  4086. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4087. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4088. phba->sli3_options = 0;
  4089. }
  4090. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4091. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4092. phba->sli_rev, phba->max_vpi);
  4093. rc = lpfc_sli_ring_map(phba);
  4094. if (rc)
  4095. goto lpfc_sli_hba_setup_error;
  4096. /* Initialize VPIs. */
  4097. if (phba->sli_rev == LPFC_SLI_REV3) {
  4098. /*
  4099. * The VPI bitmask and physical ID array are allocated
  4100. * and initialized once only - at driver load. A port
  4101. * reset doesn't need to reinitialize this memory.
  4102. */
  4103. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4104. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4105. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4106. GFP_KERNEL);
  4107. if (!phba->vpi_bmask) {
  4108. rc = -ENOMEM;
  4109. goto lpfc_sli_hba_setup_error;
  4110. }
  4111. phba->vpi_ids = kzalloc(
  4112. (phba->max_vpi+1) * sizeof(uint16_t),
  4113. GFP_KERNEL);
  4114. if (!phba->vpi_ids) {
  4115. kfree(phba->vpi_bmask);
  4116. rc = -ENOMEM;
  4117. goto lpfc_sli_hba_setup_error;
  4118. }
  4119. for (i = 0; i < phba->max_vpi; i++)
  4120. phba->vpi_ids[i] = i;
  4121. }
  4122. }
  4123. /* Init HBQs */
  4124. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4125. rc = lpfc_sli_hbq_setup(phba);
  4126. if (rc)
  4127. goto lpfc_sli_hba_setup_error;
  4128. }
  4129. spin_lock_irq(&phba->hbalock);
  4130. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4131. spin_unlock_irq(&phba->hbalock);
  4132. rc = lpfc_config_port_post(phba);
  4133. if (rc)
  4134. goto lpfc_sli_hba_setup_error;
  4135. return rc;
  4136. lpfc_sli_hba_setup_error:
  4137. phba->link_state = LPFC_HBA_ERROR;
  4138. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4139. "0445 Firmware initialization failed\n");
  4140. return rc;
  4141. }
  4142. /**
  4143. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4144. * @phba: Pointer to HBA context object.
  4145. * @mboxq: mailbox pointer.
  4146. * This function issue a dump mailbox command to read config region
  4147. * 23 and parse the records in the region and populate driver
  4148. * data structure.
  4149. **/
  4150. static int
  4151. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4152. {
  4153. LPFC_MBOXQ_t *mboxq;
  4154. struct lpfc_dmabuf *mp;
  4155. struct lpfc_mqe *mqe;
  4156. uint32_t data_length;
  4157. int rc;
  4158. /* Program the default value of vlan_id and fc_map */
  4159. phba->valid_vlan = 0;
  4160. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4161. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4162. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4163. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4164. if (!mboxq)
  4165. return -ENOMEM;
  4166. mqe = &mboxq->u.mqe;
  4167. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4168. rc = -ENOMEM;
  4169. goto out_free_mboxq;
  4170. }
  4171. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4172. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4173. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4174. "(%d):2571 Mailbox cmd x%x Status x%x "
  4175. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4176. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4177. "CQ: x%x x%x x%x x%x\n",
  4178. mboxq->vport ? mboxq->vport->vpi : 0,
  4179. bf_get(lpfc_mqe_command, mqe),
  4180. bf_get(lpfc_mqe_status, mqe),
  4181. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4182. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4183. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4184. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4185. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4186. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4187. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4188. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4189. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4190. mboxq->mcqe.word0,
  4191. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4192. mboxq->mcqe.trailer);
  4193. if (rc) {
  4194. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4195. kfree(mp);
  4196. rc = -EIO;
  4197. goto out_free_mboxq;
  4198. }
  4199. data_length = mqe->un.mb_words[5];
  4200. if (data_length > DMP_RGN23_SIZE) {
  4201. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4202. kfree(mp);
  4203. rc = -EIO;
  4204. goto out_free_mboxq;
  4205. }
  4206. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4207. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4208. kfree(mp);
  4209. rc = 0;
  4210. out_free_mboxq:
  4211. mempool_free(mboxq, phba->mbox_mem_pool);
  4212. return rc;
  4213. }
  4214. /**
  4215. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4216. * @phba: pointer to lpfc hba data structure.
  4217. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4218. * @vpd: pointer to the memory to hold resulting port vpd data.
  4219. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4220. * On output, the number of data bytes in @vpd.
  4221. *
  4222. * This routine executes a READ_REV SLI4 mailbox command. In
  4223. * addition, this routine gets the port vpd data.
  4224. *
  4225. * Return codes
  4226. * 0 - successful
  4227. * -ENOMEM - could not allocated memory.
  4228. **/
  4229. static int
  4230. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4231. uint8_t *vpd, uint32_t *vpd_size)
  4232. {
  4233. int rc = 0;
  4234. uint32_t dma_size;
  4235. struct lpfc_dmabuf *dmabuf;
  4236. struct lpfc_mqe *mqe;
  4237. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4238. if (!dmabuf)
  4239. return -ENOMEM;
  4240. /*
  4241. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4242. * mailbox command.
  4243. */
  4244. dma_size = *vpd_size;
  4245. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4246. dma_size,
  4247. &dmabuf->phys,
  4248. GFP_KERNEL);
  4249. if (!dmabuf->virt) {
  4250. kfree(dmabuf);
  4251. return -ENOMEM;
  4252. }
  4253. memset(dmabuf->virt, 0, dma_size);
  4254. /*
  4255. * The SLI4 implementation of READ_REV conflicts at word1,
  4256. * bits 31:16 and SLI4 adds vpd functionality not present
  4257. * in SLI3. This code corrects the conflicts.
  4258. */
  4259. lpfc_read_rev(phba, mboxq);
  4260. mqe = &mboxq->u.mqe;
  4261. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4262. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4263. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4264. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4265. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4266. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4267. if (rc) {
  4268. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4269. dmabuf->virt, dmabuf->phys);
  4270. kfree(dmabuf);
  4271. return -EIO;
  4272. }
  4273. /*
  4274. * The available vpd length cannot be bigger than the
  4275. * DMA buffer passed to the port. Catch the less than
  4276. * case and update the caller's size.
  4277. */
  4278. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4279. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4280. memcpy(vpd, dmabuf->virt, *vpd_size);
  4281. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4282. dmabuf->virt, dmabuf->phys);
  4283. kfree(dmabuf);
  4284. return 0;
  4285. }
  4286. /**
  4287. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4288. * @phba: pointer to lpfc hba data structure.
  4289. *
  4290. * This routine retrieves SLI4 device physical port name this PCI function
  4291. * is attached to.
  4292. *
  4293. * Return codes
  4294. * 0 - sucessful
  4295. * otherwise - failed to retrieve physical port name
  4296. **/
  4297. static int
  4298. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4299. {
  4300. LPFC_MBOXQ_t *mboxq;
  4301. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4302. struct lpfc_controller_attribute *cntl_attr;
  4303. struct lpfc_mbx_get_port_name *get_port_name;
  4304. void *virtaddr = NULL;
  4305. uint32_t alloclen, reqlen;
  4306. uint32_t shdr_status, shdr_add_status;
  4307. union lpfc_sli4_cfg_shdr *shdr;
  4308. char cport_name = 0;
  4309. int rc;
  4310. /* We assume nothing at this point */
  4311. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4312. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4313. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4314. if (!mboxq)
  4315. return -ENOMEM;
  4316. /* obtain link type and link number via READ_CONFIG */
  4317. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4318. lpfc_sli4_read_config(phba);
  4319. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4320. goto retrieve_ppname;
  4321. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4322. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4323. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4324. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4325. LPFC_SLI4_MBX_NEMBED);
  4326. if (alloclen < reqlen) {
  4327. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4328. "3084 Allocated DMA memory size (%d) is "
  4329. "less than the requested DMA memory size "
  4330. "(%d)\n", alloclen, reqlen);
  4331. rc = -ENOMEM;
  4332. goto out_free_mboxq;
  4333. }
  4334. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4335. virtaddr = mboxq->sge_array->addr[0];
  4336. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4337. shdr = &mbx_cntl_attr->cfg_shdr;
  4338. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4339. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4340. if (shdr_status || shdr_add_status || rc) {
  4341. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4342. "3085 Mailbox x%x (x%x/x%x) failed, "
  4343. "rc:x%x, status:x%x, add_status:x%x\n",
  4344. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4345. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4346. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4347. rc, shdr_status, shdr_add_status);
  4348. rc = -ENXIO;
  4349. goto out_free_mboxq;
  4350. }
  4351. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4352. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4353. phba->sli4_hba.lnk_info.lnk_tp =
  4354. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4355. phba->sli4_hba.lnk_info.lnk_no =
  4356. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4357. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4358. "3086 lnk_type:%d, lnk_numb:%d\n",
  4359. phba->sli4_hba.lnk_info.lnk_tp,
  4360. phba->sli4_hba.lnk_info.lnk_no);
  4361. retrieve_ppname:
  4362. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4363. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4364. sizeof(struct lpfc_mbx_get_port_name) -
  4365. sizeof(struct lpfc_sli4_cfg_mhdr),
  4366. LPFC_SLI4_MBX_EMBED);
  4367. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4368. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4369. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4370. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4371. phba->sli4_hba.lnk_info.lnk_tp);
  4372. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4373. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4374. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4375. if (shdr_status || shdr_add_status || rc) {
  4376. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4377. "3087 Mailbox x%x (x%x/x%x) failed: "
  4378. "rc:x%x, status:x%x, add_status:x%x\n",
  4379. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4380. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4381. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4382. rc, shdr_status, shdr_add_status);
  4383. rc = -ENXIO;
  4384. goto out_free_mboxq;
  4385. }
  4386. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4387. case LPFC_LINK_NUMBER_0:
  4388. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4389. &get_port_name->u.response);
  4390. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4391. break;
  4392. case LPFC_LINK_NUMBER_1:
  4393. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4394. &get_port_name->u.response);
  4395. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4396. break;
  4397. case LPFC_LINK_NUMBER_2:
  4398. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4399. &get_port_name->u.response);
  4400. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4401. break;
  4402. case LPFC_LINK_NUMBER_3:
  4403. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4404. &get_port_name->u.response);
  4405. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4406. break;
  4407. default:
  4408. break;
  4409. }
  4410. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4411. phba->Port[0] = cport_name;
  4412. phba->Port[1] = '\0';
  4413. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4414. "3091 SLI get port name: %s\n", phba->Port);
  4415. }
  4416. out_free_mboxq:
  4417. if (rc != MBX_TIMEOUT) {
  4418. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4419. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4420. else
  4421. mempool_free(mboxq, phba->mbox_mem_pool);
  4422. }
  4423. return rc;
  4424. }
  4425. /**
  4426. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4427. * @phba: pointer to lpfc hba data structure.
  4428. *
  4429. * This routine is called to explicitly arm the SLI4 device's completion and
  4430. * event queues
  4431. **/
  4432. static void
  4433. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4434. {
  4435. uint8_t fcp_eqidx;
  4436. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4437. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4438. fcp_eqidx = 0;
  4439. if (phba->sli4_hba.fcp_cq) {
  4440. do
  4441. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4442. LPFC_QUEUE_REARM);
  4443. while (++fcp_eqidx < phba->cfg_fcp_eq_count);
  4444. }
  4445. lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
  4446. if (phba->sli4_hba.fp_eq) {
  4447. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
  4448. fcp_eqidx++)
  4449. lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
  4450. LPFC_QUEUE_REARM);
  4451. }
  4452. }
  4453. /**
  4454. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4455. * @phba: Pointer to HBA context object.
  4456. * @type: The resource extent type.
  4457. * @extnt_count: buffer to hold port available extent count.
  4458. * @extnt_size: buffer to hold element count per extent.
  4459. *
  4460. * This function calls the port and retrievs the number of available
  4461. * extents and their size for a particular extent type.
  4462. *
  4463. * Returns: 0 if successful. Nonzero otherwise.
  4464. **/
  4465. int
  4466. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4467. uint16_t *extnt_count, uint16_t *extnt_size)
  4468. {
  4469. int rc = 0;
  4470. uint32_t length;
  4471. uint32_t mbox_tmo;
  4472. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4473. LPFC_MBOXQ_t *mbox;
  4474. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4475. if (!mbox)
  4476. return -ENOMEM;
  4477. /* Find out how many extents are available for this resource type */
  4478. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4479. sizeof(struct lpfc_sli4_cfg_mhdr));
  4480. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4481. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4482. length, LPFC_SLI4_MBX_EMBED);
  4483. /* Send an extents count of 0 - the GET doesn't use it. */
  4484. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4485. LPFC_SLI4_MBX_EMBED);
  4486. if (unlikely(rc)) {
  4487. rc = -EIO;
  4488. goto err_exit;
  4489. }
  4490. if (!phba->sli4_hba.intr_enable)
  4491. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4492. else {
  4493. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4494. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4495. }
  4496. if (unlikely(rc)) {
  4497. rc = -EIO;
  4498. goto err_exit;
  4499. }
  4500. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4501. if (bf_get(lpfc_mbox_hdr_status,
  4502. &rsrc_info->header.cfg_shdr.response)) {
  4503. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4504. "2930 Failed to get resource extents "
  4505. "Status 0x%x Add'l Status 0x%x\n",
  4506. bf_get(lpfc_mbox_hdr_status,
  4507. &rsrc_info->header.cfg_shdr.response),
  4508. bf_get(lpfc_mbox_hdr_add_status,
  4509. &rsrc_info->header.cfg_shdr.response));
  4510. rc = -EIO;
  4511. goto err_exit;
  4512. }
  4513. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4514. &rsrc_info->u.rsp);
  4515. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4516. &rsrc_info->u.rsp);
  4517. err_exit:
  4518. mempool_free(mbox, phba->mbox_mem_pool);
  4519. return rc;
  4520. }
  4521. /**
  4522. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4523. * @phba: Pointer to HBA context object.
  4524. * @type: The extent type to check.
  4525. *
  4526. * This function reads the current available extents from the port and checks
  4527. * if the extent count or extent size has changed since the last access.
  4528. * Callers use this routine post port reset to understand if there is a
  4529. * extent reprovisioning requirement.
  4530. *
  4531. * Returns:
  4532. * -Error: error indicates problem.
  4533. * 1: Extent count or size has changed.
  4534. * 0: No changes.
  4535. **/
  4536. static int
  4537. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4538. {
  4539. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4540. uint16_t size_diff, rsrc_ext_size;
  4541. int rc = 0;
  4542. struct lpfc_rsrc_blks *rsrc_entry;
  4543. struct list_head *rsrc_blk_list = NULL;
  4544. size_diff = 0;
  4545. curr_ext_cnt = 0;
  4546. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4547. &rsrc_ext_cnt,
  4548. &rsrc_ext_size);
  4549. if (unlikely(rc))
  4550. return -EIO;
  4551. switch (type) {
  4552. case LPFC_RSC_TYPE_FCOE_RPI:
  4553. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4554. break;
  4555. case LPFC_RSC_TYPE_FCOE_VPI:
  4556. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4557. break;
  4558. case LPFC_RSC_TYPE_FCOE_XRI:
  4559. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4560. break;
  4561. case LPFC_RSC_TYPE_FCOE_VFI:
  4562. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4563. break;
  4564. default:
  4565. break;
  4566. }
  4567. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4568. curr_ext_cnt++;
  4569. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4570. size_diff++;
  4571. }
  4572. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4573. rc = 1;
  4574. return rc;
  4575. }
  4576. /**
  4577. * lpfc_sli4_cfg_post_extnts -
  4578. * @phba: Pointer to HBA context object.
  4579. * @extnt_cnt - number of available extents.
  4580. * @type - the extent type (rpi, xri, vfi, vpi).
  4581. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4582. * @mbox - pointer to the caller's allocated mailbox structure.
  4583. *
  4584. * This function executes the extents allocation request. It also
  4585. * takes care of the amount of memory needed to allocate or get the
  4586. * allocated extents. It is the caller's responsibility to evaluate
  4587. * the response.
  4588. *
  4589. * Returns:
  4590. * -Error: Error value describes the condition found.
  4591. * 0: if successful
  4592. **/
  4593. static int
  4594. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
  4595. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4596. {
  4597. int rc = 0;
  4598. uint32_t req_len;
  4599. uint32_t emb_len;
  4600. uint32_t alloc_len, mbox_tmo;
  4601. /* Calculate the total requested length of the dma memory */
  4602. req_len = *extnt_cnt * sizeof(uint16_t);
  4603. /*
  4604. * Calculate the size of an embedded mailbox. The uint32_t
  4605. * accounts for extents-specific word.
  4606. */
  4607. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4608. sizeof(uint32_t);
  4609. /*
  4610. * Presume the allocation and response will fit into an embedded
  4611. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4612. */
  4613. *emb = LPFC_SLI4_MBX_EMBED;
  4614. if (req_len > emb_len) {
  4615. req_len = *extnt_cnt * sizeof(uint16_t) +
  4616. sizeof(union lpfc_sli4_cfg_shdr) +
  4617. sizeof(uint32_t);
  4618. *emb = LPFC_SLI4_MBX_NEMBED;
  4619. }
  4620. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4621. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4622. req_len, *emb);
  4623. if (alloc_len < req_len) {
  4624. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4625. "2982 Allocated DMA memory size (x%x) is "
  4626. "less than the requested DMA memory "
  4627. "size (x%x)\n", alloc_len, req_len);
  4628. return -ENOMEM;
  4629. }
  4630. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
  4631. if (unlikely(rc))
  4632. return -EIO;
  4633. if (!phba->sli4_hba.intr_enable)
  4634. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4635. else {
  4636. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4637. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4638. }
  4639. if (unlikely(rc))
  4640. rc = -EIO;
  4641. return rc;
  4642. }
  4643. /**
  4644. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4645. * @phba: Pointer to HBA context object.
  4646. * @type: The resource extent type to allocate.
  4647. *
  4648. * This function allocates the number of elements for the specified
  4649. * resource type.
  4650. **/
  4651. static int
  4652. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4653. {
  4654. bool emb = false;
  4655. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4656. uint16_t rsrc_id, rsrc_start, j, k;
  4657. uint16_t *ids;
  4658. int i, rc;
  4659. unsigned long longs;
  4660. unsigned long *bmask;
  4661. struct lpfc_rsrc_blks *rsrc_blks;
  4662. LPFC_MBOXQ_t *mbox;
  4663. uint32_t length;
  4664. struct lpfc_id_range *id_array = NULL;
  4665. void *virtaddr = NULL;
  4666. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4667. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4668. struct list_head *ext_blk_list;
  4669. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4670. &rsrc_cnt,
  4671. &rsrc_size);
  4672. if (unlikely(rc))
  4673. return -EIO;
  4674. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4675. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4676. "3009 No available Resource Extents "
  4677. "for resource type 0x%x: Count: 0x%x, "
  4678. "Size 0x%x\n", type, rsrc_cnt,
  4679. rsrc_size);
  4680. return -ENOMEM;
  4681. }
  4682. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
  4683. "2903 Available Resource Extents "
  4684. "for resource type 0x%x: Count: 0x%x, "
  4685. "Size 0x%x\n", type, rsrc_cnt,
  4686. rsrc_size);
  4687. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4688. if (!mbox)
  4689. return -ENOMEM;
  4690. rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
  4691. if (unlikely(rc)) {
  4692. rc = -EIO;
  4693. goto err_exit;
  4694. }
  4695. /*
  4696. * Figure out where the response is located. Then get local pointers
  4697. * to the response data. The port does not guarantee to respond to
  4698. * all extents counts request so update the local variable with the
  4699. * allocated count from the port.
  4700. */
  4701. if (emb == LPFC_SLI4_MBX_EMBED) {
  4702. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4703. id_array = &rsrc_ext->u.rsp.id[0];
  4704. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4705. } else {
  4706. virtaddr = mbox->sge_array->addr[0];
  4707. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4708. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4709. id_array = &n_rsrc->id;
  4710. }
  4711. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4712. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4713. /*
  4714. * Based on the resource size and count, correct the base and max
  4715. * resource values.
  4716. */
  4717. length = sizeof(struct lpfc_rsrc_blks);
  4718. switch (type) {
  4719. case LPFC_RSC_TYPE_FCOE_RPI:
  4720. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4721. sizeof(unsigned long),
  4722. GFP_KERNEL);
  4723. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4724. rc = -ENOMEM;
  4725. goto err_exit;
  4726. }
  4727. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4728. sizeof(uint16_t),
  4729. GFP_KERNEL);
  4730. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4731. kfree(phba->sli4_hba.rpi_bmask);
  4732. rc = -ENOMEM;
  4733. goto err_exit;
  4734. }
  4735. /*
  4736. * The next_rpi was initialized with the maximum available
  4737. * count but the port may allocate a smaller number. Catch
  4738. * that case and update the next_rpi.
  4739. */
  4740. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4741. /* Initialize local ptrs for common extent processing later. */
  4742. bmask = phba->sli4_hba.rpi_bmask;
  4743. ids = phba->sli4_hba.rpi_ids;
  4744. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4745. break;
  4746. case LPFC_RSC_TYPE_FCOE_VPI:
  4747. phba->vpi_bmask = kzalloc(longs *
  4748. sizeof(unsigned long),
  4749. GFP_KERNEL);
  4750. if (unlikely(!phba->vpi_bmask)) {
  4751. rc = -ENOMEM;
  4752. goto err_exit;
  4753. }
  4754. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4755. sizeof(uint16_t),
  4756. GFP_KERNEL);
  4757. if (unlikely(!phba->vpi_ids)) {
  4758. kfree(phba->vpi_bmask);
  4759. rc = -ENOMEM;
  4760. goto err_exit;
  4761. }
  4762. /* Initialize local ptrs for common extent processing later. */
  4763. bmask = phba->vpi_bmask;
  4764. ids = phba->vpi_ids;
  4765. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4766. break;
  4767. case LPFC_RSC_TYPE_FCOE_XRI:
  4768. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4769. sizeof(unsigned long),
  4770. GFP_KERNEL);
  4771. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4772. rc = -ENOMEM;
  4773. goto err_exit;
  4774. }
  4775. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4776. sizeof(uint16_t),
  4777. GFP_KERNEL);
  4778. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4779. kfree(phba->sli4_hba.xri_bmask);
  4780. rc = -ENOMEM;
  4781. goto err_exit;
  4782. }
  4783. /* Initialize local ptrs for common extent processing later. */
  4784. bmask = phba->sli4_hba.xri_bmask;
  4785. ids = phba->sli4_hba.xri_ids;
  4786. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4787. break;
  4788. case LPFC_RSC_TYPE_FCOE_VFI:
  4789. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4790. sizeof(unsigned long),
  4791. GFP_KERNEL);
  4792. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4793. rc = -ENOMEM;
  4794. goto err_exit;
  4795. }
  4796. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4797. sizeof(uint16_t),
  4798. GFP_KERNEL);
  4799. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4800. kfree(phba->sli4_hba.vfi_bmask);
  4801. rc = -ENOMEM;
  4802. goto err_exit;
  4803. }
  4804. /* Initialize local ptrs for common extent processing later. */
  4805. bmask = phba->sli4_hba.vfi_bmask;
  4806. ids = phba->sli4_hba.vfi_ids;
  4807. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4808. break;
  4809. default:
  4810. /* Unsupported Opcode. Fail call. */
  4811. id_array = NULL;
  4812. bmask = NULL;
  4813. ids = NULL;
  4814. ext_blk_list = NULL;
  4815. goto err_exit;
  4816. }
  4817. /*
  4818. * Complete initializing the extent configuration with the
  4819. * allocated ids assigned to this function. The bitmask serves
  4820. * as an index into the array and manages the available ids. The
  4821. * array just stores the ids communicated to the port via the wqes.
  4822. */
  4823. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4824. if ((i % 2) == 0)
  4825. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4826. &id_array[k]);
  4827. else
  4828. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4829. &id_array[k]);
  4830. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4831. if (unlikely(!rsrc_blks)) {
  4832. rc = -ENOMEM;
  4833. kfree(bmask);
  4834. kfree(ids);
  4835. goto err_exit;
  4836. }
  4837. rsrc_blks->rsrc_start = rsrc_id;
  4838. rsrc_blks->rsrc_size = rsrc_size;
  4839. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4840. rsrc_start = rsrc_id;
  4841. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4842. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4843. lpfc_sli4_get_els_iocb_cnt(phba);
  4844. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4845. ids[j] = rsrc_id;
  4846. rsrc_id++;
  4847. j++;
  4848. }
  4849. /* Entire word processed. Get next word.*/
  4850. if ((i % 2) == 1)
  4851. k++;
  4852. }
  4853. err_exit:
  4854. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4855. return rc;
  4856. }
  4857. /**
  4858. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4859. * @phba: Pointer to HBA context object.
  4860. * @type: the extent's type.
  4861. *
  4862. * This function deallocates all extents of a particular resource type.
  4863. * SLI4 does not allow for deallocating a particular extent range. It
  4864. * is the caller's responsibility to release all kernel memory resources.
  4865. **/
  4866. static int
  4867. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4868. {
  4869. int rc;
  4870. uint32_t length, mbox_tmo = 0;
  4871. LPFC_MBOXQ_t *mbox;
  4872. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4873. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4874. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4875. if (!mbox)
  4876. return -ENOMEM;
  4877. /*
  4878. * This function sends an embedded mailbox because it only sends the
  4879. * the resource type. All extents of this type are released by the
  4880. * port.
  4881. */
  4882. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4883. sizeof(struct lpfc_sli4_cfg_mhdr));
  4884. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4885. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4886. length, LPFC_SLI4_MBX_EMBED);
  4887. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4888. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4889. LPFC_SLI4_MBX_EMBED);
  4890. if (unlikely(rc)) {
  4891. rc = -EIO;
  4892. goto out_free_mbox;
  4893. }
  4894. if (!phba->sli4_hba.intr_enable)
  4895. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4896. else {
  4897. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4898. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4899. }
  4900. if (unlikely(rc)) {
  4901. rc = -EIO;
  4902. goto out_free_mbox;
  4903. }
  4904. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4905. if (bf_get(lpfc_mbox_hdr_status,
  4906. &dealloc_rsrc->header.cfg_shdr.response)) {
  4907. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4908. "2919 Failed to release resource extents "
  4909. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4910. "Resource memory not released.\n",
  4911. type,
  4912. bf_get(lpfc_mbox_hdr_status,
  4913. &dealloc_rsrc->header.cfg_shdr.response),
  4914. bf_get(lpfc_mbox_hdr_add_status,
  4915. &dealloc_rsrc->header.cfg_shdr.response));
  4916. rc = -EIO;
  4917. goto out_free_mbox;
  4918. }
  4919. /* Release kernel memory resources for the specific type. */
  4920. switch (type) {
  4921. case LPFC_RSC_TYPE_FCOE_VPI:
  4922. kfree(phba->vpi_bmask);
  4923. kfree(phba->vpi_ids);
  4924. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4925. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4926. &phba->lpfc_vpi_blk_list, list) {
  4927. list_del_init(&rsrc_blk->list);
  4928. kfree(rsrc_blk);
  4929. }
  4930. break;
  4931. case LPFC_RSC_TYPE_FCOE_XRI:
  4932. kfree(phba->sli4_hba.xri_bmask);
  4933. kfree(phba->sli4_hba.xri_ids);
  4934. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4935. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4936. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4937. list_del_init(&rsrc_blk->list);
  4938. kfree(rsrc_blk);
  4939. }
  4940. break;
  4941. case LPFC_RSC_TYPE_FCOE_VFI:
  4942. kfree(phba->sli4_hba.vfi_bmask);
  4943. kfree(phba->sli4_hba.vfi_ids);
  4944. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4945. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4946. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  4947. list_del_init(&rsrc_blk->list);
  4948. kfree(rsrc_blk);
  4949. }
  4950. break;
  4951. case LPFC_RSC_TYPE_FCOE_RPI:
  4952. /* RPI bitmask and physical id array are cleaned up earlier. */
  4953. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4954. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  4955. list_del_init(&rsrc_blk->list);
  4956. kfree(rsrc_blk);
  4957. }
  4958. break;
  4959. default:
  4960. break;
  4961. }
  4962. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4963. out_free_mbox:
  4964. mempool_free(mbox, phba->mbox_mem_pool);
  4965. return rc;
  4966. }
  4967. /**
  4968. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  4969. * @phba: Pointer to HBA context object.
  4970. *
  4971. * This function allocates all SLI4 resource identifiers.
  4972. **/
  4973. int
  4974. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  4975. {
  4976. int i, rc, error = 0;
  4977. uint16_t count, base;
  4978. unsigned long longs;
  4979. if (!phba->sli4_hba.rpi_hdrs_in_use)
  4980. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  4981. if (phba->sli4_hba.extents_in_use) {
  4982. /*
  4983. * The port supports resource extents. The XRI, VPI, VFI, RPI
  4984. * resource extent count must be read and allocated before
  4985. * provisioning the resource id arrays.
  4986. */
  4987. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  4988. LPFC_IDX_RSRC_RDY) {
  4989. /*
  4990. * Extent-based resources are set - the driver could
  4991. * be in a port reset. Figure out if any corrective
  4992. * actions need to be taken.
  4993. */
  4994. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4995. LPFC_RSC_TYPE_FCOE_VFI);
  4996. if (rc != 0)
  4997. error++;
  4998. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4999. LPFC_RSC_TYPE_FCOE_VPI);
  5000. if (rc != 0)
  5001. error++;
  5002. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5003. LPFC_RSC_TYPE_FCOE_XRI);
  5004. if (rc != 0)
  5005. error++;
  5006. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5007. LPFC_RSC_TYPE_FCOE_RPI);
  5008. if (rc != 0)
  5009. error++;
  5010. /*
  5011. * It's possible that the number of resources
  5012. * provided to this port instance changed between
  5013. * resets. Detect this condition and reallocate
  5014. * resources. Otherwise, there is no action.
  5015. */
  5016. if (error) {
  5017. lpfc_printf_log(phba, KERN_INFO,
  5018. LOG_MBOX | LOG_INIT,
  5019. "2931 Detected extent resource "
  5020. "change. Reallocating all "
  5021. "extents.\n");
  5022. rc = lpfc_sli4_dealloc_extent(phba,
  5023. LPFC_RSC_TYPE_FCOE_VFI);
  5024. rc = lpfc_sli4_dealloc_extent(phba,
  5025. LPFC_RSC_TYPE_FCOE_VPI);
  5026. rc = lpfc_sli4_dealloc_extent(phba,
  5027. LPFC_RSC_TYPE_FCOE_XRI);
  5028. rc = lpfc_sli4_dealloc_extent(phba,
  5029. LPFC_RSC_TYPE_FCOE_RPI);
  5030. } else
  5031. return 0;
  5032. }
  5033. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5034. if (unlikely(rc))
  5035. goto err_exit;
  5036. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5037. if (unlikely(rc))
  5038. goto err_exit;
  5039. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5040. if (unlikely(rc))
  5041. goto err_exit;
  5042. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5043. if (unlikely(rc))
  5044. goto err_exit;
  5045. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5046. LPFC_IDX_RSRC_RDY);
  5047. return rc;
  5048. } else {
  5049. /*
  5050. * The port does not support resource extents. The XRI, VPI,
  5051. * VFI, RPI resource ids were determined from READ_CONFIG.
  5052. * Just allocate the bitmasks and provision the resource id
  5053. * arrays. If a port reset is active, the resources don't
  5054. * need any action - just exit.
  5055. */
  5056. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5057. LPFC_IDX_RSRC_RDY) {
  5058. lpfc_sli4_dealloc_resource_identifiers(phba);
  5059. lpfc_sli4_remove_rpis(phba);
  5060. }
  5061. /* RPIs. */
  5062. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5063. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5064. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5065. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5066. sizeof(unsigned long),
  5067. GFP_KERNEL);
  5068. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5069. rc = -ENOMEM;
  5070. goto err_exit;
  5071. }
  5072. phba->sli4_hba.rpi_ids = kzalloc(count *
  5073. sizeof(uint16_t),
  5074. GFP_KERNEL);
  5075. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5076. rc = -ENOMEM;
  5077. goto free_rpi_bmask;
  5078. }
  5079. for (i = 0; i < count; i++)
  5080. phba->sli4_hba.rpi_ids[i] = base + i;
  5081. /* VPIs. */
  5082. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5083. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5084. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5085. phba->vpi_bmask = kzalloc(longs *
  5086. sizeof(unsigned long),
  5087. GFP_KERNEL);
  5088. if (unlikely(!phba->vpi_bmask)) {
  5089. rc = -ENOMEM;
  5090. goto free_rpi_ids;
  5091. }
  5092. phba->vpi_ids = kzalloc(count *
  5093. sizeof(uint16_t),
  5094. GFP_KERNEL);
  5095. if (unlikely(!phba->vpi_ids)) {
  5096. rc = -ENOMEM;
  5097. goto free_vpi_bmask;
  5098. }
  5099. for (i = 0; i < count; i++)
  5100. phba->vpi_ids[i] = base + i;
  5101. /* XRIs. */
  5102. count = phba->sli4_hba.max_cfg_param.max_xri;
  5103. base = phba->sli4_hba.max_cfg_param.xri_base;
  5104. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5105. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5106. sizeof(unsigned long),
  5107. GFP_KERNEL);
  5108. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5109. rc = -ENOMEM;
  5110. goto free_vpi_ids;
  5111. }
  5112. phba->sli4_hba.xri_ids = kzalloc(count *
  5113. sizeof(uint16_t),
  5114. GFP_KERNEL);
  5115. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5116. rc = -ENOMEM;
  5117. goto free_xri_bmask;
  5118. }
  5119. for (i = 0; i < count; i++)
  5120. phba->sli4_hba.xri_ids[i] = base + i;
  5121. /* VFIs. */
  5122. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5123. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5124. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5125. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5126. sizeof(unsigned long),
  5127. GFP_KERNEL);
  5128. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5129. rc = -ENOMEM;
  5130. goto free_xri_ids;
  5131. }
  5132. phba->sli4_hba.vfi_ids = kzalloc(count *
  5133. sizeof(uint16_t),
  5134. GFP_KERNEL);
  5135. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5136. rc = -ENOMEM;
  5137. goto free_vfi_bmask;
  5138. }
  5139. for (i = 0; i < count; i++)
  5140. phba->sli4_hba.vfi_ids[i] = base + i;
  5141. /*
  5142. * Mark all resources ready. An HBA reset doesn't need
  5143. * to reset the initialization.
  5144. */
  5145. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5146. LPFC_IDX_RSRC_RDY);
  5147. return 0;
  5148. }
  5149. free_vfi_bmask:
  5150. kfree(phba->sli4_hba.vfi_bmask);
  5151. free_xri_ids:
  5152. kfree(phba->sli4_hba.xri_ids);
  5153. free_xri_bmask:
  5154. kfree(phba->sli4_hba.xri_bmask);
  5155. free_vpi_ids:
  5156. kfree(phba->vpi_ids);
  5157. free_vpi_bmask:
  5158. kfree(phba->vpi_bmask);
  5159. free_rpi_ids:
  5160. kfree(phba->sli4_hba.rpi_ids);
  5161. free_rpi_bmask:
  5162. kfree(phba->sli4_hba.rpi_bmask);
  5163. err_exit:
  5164. return rc;
  5165. }
  5166. /**
  5167. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5168. * @phba: Pointer to HBA context object.
  5169. *
  5170. * This function allocates the number of elements for the specified
  5171. * resource type.
  5172. **/
  5173. int
  5174. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5175. {
  5176. if (phba->sli4_hba.extents_in_use) {
  5177. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5178. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5179. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5180. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5181. } else {
  5182. kfree(phba->vpi_bmask);
  5183. kfree(phba->vpi_ids);
  5184. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5185. kfree(phba->sli4_hba.xri_bmask);
  5186. kfree(phba->sli4_hba.xri_ids);
  5187. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5188. kfree(phba->sli4_hba.vfi_bmask);
  5189. kfree(phba->sli4_hba.vfi_ids);
  5190. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5191. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5192. }
  5193. return 0;
  5194. }
  5195. /**
  5196. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5197. * @phba: Pointer to HBA context object.
  5198. * @type: The resource extent type.
  5199. * @extnt_count: buffer to hold port extent count response
  5200. * @extnt_size: buffer to hold port extent size response.
  5201. *
  5202. * This function calls the port to read the host allocated extents
  5203. * for a particular type.
  5204. **/
  5205. int
  5206. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5207. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5208. {
  5209. bool emb;
  5210. int rc = 0;
  5211. uint16_t curr_blks = 0;
  5212. uint32_t req_len, emb_len;
  5213. uint32_t alloc_len, mbox_tmo;
  5214. struct list_head *blk_list_head;
  5215. struct lpfc_rsrc_blks *rsrc_blk;
  5216. LPFC_MBOXQ_t *mbox;
  5217. void *virtaddr = NULL;
  5218. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5219. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5220. union lpfc_sli4_cfg_shdr *shdr;
  5221. switch (type) {
  5222. case LPFC_RSC_TYPE_FCOE_VPI:
  5223. blk_list_head = &phba->lpfc_vpi_blk_list;
  5224. break;
  5225. case LPFC_RSC_TYPE_FCOE_XRI:
  5226. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5227. break;
  5228. case LPFC_RSC_TYPE_FCOE_VFI:
  5229. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5230. break;
  5231. case LPFC_RSC_TYPE_FCOE_RPI:
  5232. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5233. break;
  5234. default:
  5235. return -EIO;
  5236. }
  5237. /* Count the number of extents currently allocatd for this type. */
  5238. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5239. if (curr_blks == 0) {
  5240. /*
  5241. * The GET_ALLOCATED mailbox does not return the size,
  5242. * just the count. The size should be just the size
  5243. * stored in the current allocated block and all sizes
  5244. * for an extent type are the same so set the return
  5245. * value now.
  5246. */
  5247. *extnt_size = rsrc_blk->rsrc_size;
  5248. }
  5249. curr_blks++;
  5250. }
  5251. /* Calculate the total requested length of the dma memory. */
  5252. req_len = curr_blks * sizeof(uint16_t);
  5253. /*
  5254. * Calculate the size of an embedded mailbox. The uint32_t
  5255. * accounts for extents-specific word.
  5256. */
  5257. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5258. sizeof(uint32_t);
  5259. /*
  5260. * Presume the allocation and response will fit into an embedded
  5261. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5262. */
  5263. emb = LPFC_SLI4_MBX_EMBED;
  5264. req_len = emb_len;
  5265. if (req_len > emb_len) {
  5266. req_len = curr_blks * sizeof(uint16_t) +
  5267. sizeof(union lpfc_sli4_cfg_shdr) +
  5268. sizeof(uint32_t);
  5269. emb = LPFC_SLI4_MBX_NEMBED;
  5270. }
  5271. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5272. if (!mbox)
  5273. return -ENOMEM;
  5274. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5275. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5276. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5277. req_len, emb);
  5278. if (alloc_len < req_len) {
  5279. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5280. "2983 Allocated DMA memory size (x%x) is "
  5281. "less than the requested DMA memory "
  5282. "size (x%x)\n", alloc_len, req_len);
  5283. rc = -ENOMEM;
  5284. goto err_exit;
  5285. }
  5286. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5287. if (unlikely(rc)) {
  5288. rc = -EIO;
  5289. goto err_exit;
  5290. }
  5291. if (!phba->sli4_hba.intr_enable)
  5292. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5293. else {
  5294. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5295. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5296. }
  5297. if (unlikely(rc)) {
  5298. rc = -EIO;
  5299. goto err_exit;
  5300. }
  5301. /*
  5302. * Figure out where the response is located. Then get local pointers
  5303. * to the response data. The port does not guarantee to respond to
  5304. * all extents counts request so update the local variable with the
  5305. * allocated count from the port.
  5306. */
  5307. if (emb == LPFC_SLI4_MBX_EMBED) {
  5308. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5309. shdr = &rsrc_ext->header.cfg_shdr;
  5310. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5311. } else {
  5312. virtaddr = mbox->sge_array->addr[0];
  5313. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5314. shdr = &n_rsrc->cfg_shdr;
  5315. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5316. }
  5317. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5318. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5319. "2984 Failed to read allocated resources "
  5320. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5321. type,
  5322. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5323. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5324. rc = -EIO;
  5325. goto err_exit;
  5326. }
  5327. err_exit:
  5328. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5329. return rc;
  5330. }
  5331. /**
  5332. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5333. * @phba: Pointer to HBA context object.
  5334. *
  5335. * This function is the main SLI4 device intialization PCI function. This
  5336. * function is called by the HBA intialization code, HBA reset code and
  5337. * HBA error attention handler code. Caller is not required to hold any
  5338. * locks.
  5339. **/
  5340. int
  5341. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5342. {
  5343. int rc;
  5344. LPFC_MBOXQ_t *mboxq;
  5345. struct lpfc_mqe *mqe;
  5346. uint8_t *vpd;
  5347. uint32_t vpd_size;
  5348. uint32_t ftr_rsp = 0;
  5349. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5350. struct lpfc_vport *vport = phba->pport;
  5351. struct lpfc_dmabuf *mp;
  5352. /* Perform a PCI function reset to start from clean */
  5353. rc = lpfc_pci_function_reset(phba);
  5354. if (unlikely(rc))
  5355. return -ENODEV;
  5356. /* Check the HBA Host Status Register for readyness */
  5357. rc = lpfc_sli4_post_status_check(phba);
  5358. if (unlikely(rc))
  5359. return -ENODEV;
  5360. else {
  5361. spin_lock_irq(&phba->hbalock);
  5362. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5363. spin_unlock_irq(&phba->hbalock);
  5364. }
  5365. /*
  5366. * Allocate a single mailbox container for initializing the
  5367. * port.
  5368. */
  5369. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5370. if (!mboxq)
  5371. return -ENOMEM;
  5372. /* Issue READ_REV to collect vpd and FW information. */
  5373. vpd_size = SLI4_PAGE_SIZE;
  5374. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5375. if (!vpd) {
  5376. rc = -ENOMEM;
  5377. goto out_free_mbox;
  5378. }
  5379. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5380. if (unlikely(rc)) {
  5381. kfree(vpd);
  5382. goto out_free_mbox;
  5383. }
  5384. mqe = &mboxq->u.mqe;
  5385. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5386. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5387. phba->hba_flag |= HBA_FCOE_MODE;
  5388. else
  5389. phba->hba_flag &= ~HBA_FCOE_MODE;
  5390. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5391. LPFC_DCBX_CEE_MODE)
  5392. phba->hba_flag |= HBA_FIP_SUPPORT;
  5393. else
  5394. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5395. if (phba->sli_rev != LPFC_SLI_REV4) {
  5396. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5397. "0376 READ_REV Error. SLI Level %d "
  5398. "FCoE enabled %d\n",
  5399. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5400. rc = -EIO;
  5401. kfree(vpd);
  5402. goto out_free_mbox;
  5403. }
  5404. /*
  5405. * Continue initialization with default values even if driver failed
  5406. * to read FCoE param config regions, only read parameters if the
  5407. * board is FCoE
  5408. */
  5409. if (phba->hba_flag & HBA_FCOE_MODE &&
  5410. lpfc_sli4_read_fcoe_params(phba))
  5411. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5412. "2570 Failed to read FCoE parameters\n");
  5413. /*
  5414. * Retrieve sli4 device physical port name, failure of doing it
  5415. * is considered as non-fatal.
  5416. */
  5417. rc = lpfc_sli4_retrieve_pport_name(phba);
  5418. if (!rc)
  5419. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5420. "3080 Successful retrieving SLI4 device "
  5421. "physical port name: %s.\n", phba->Port);
  5422. /*
  5423. * Evaluate the read rev and vpd data. Populate the driver
  5424. * state with the results. If this routine fails, the failure
  5425. * is not fatal as the driver will use generic values.
  5426. */
  5427. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5428. if (unlikely(!rc)) {
  5429. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5430. "0377 Error %d parsing vpd. "
  5431. "Using defaults.\n", rc);
  5432. rc = 0;
  5433. }
  5434. kfree(vpd);
  5435. /* Save information as VPD data */
  5436. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5437. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5438. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5439. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5440. &mqe->un.read_rev);
  5441. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5442. &mqe->un.read_rev);
  5443. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5444. &mqe->un.read_rev);
  5445. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5446. &mqe->un.read_rev);
  5447. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5448. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5449. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5450. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5451. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5452. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5453. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5454. "(%d):0380 READ_REV Status x%x "
  5455. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5456. mboxq->vport ? mboxq->vport->vpi : 0,
  5457. bf_get(lpfc_mqe_status, mqe),
  5458. phba->vpd.rev.opFwName,
  5459. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5460. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5461. /*
  5462. * Discover the port's supported feature set and match it against the
  5463. * hosts requests.
  5464. */
  5465. lpfc_request_features(phba, mboxq);
  5466. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5467. if (unlikely(rc)) {
  5468. rc = -EIO;
  5469. goto out_free_mbox;
  5470. }
  5471. /*
  5472. * The port must support FCP initiator mode as this is the
  5473. * only mode running in the host.
  5474. */
  5475. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5476. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5477. "0378 No support for fcpi mode.\n");
  5478. ftr_rsp++;
  5479. }
  5480. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5481. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5482. else
  5483. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5484. /*
  5485. * If the port cannot support the host's requested features
  5486. * then turn off the global config parameters to disable the
  5487. * feature in the driver. This is not a fatal error.
  5488. */
  5489. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5490. if (phba->cfg_enable_bg) {
  5491. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5492. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5493. else
  5494. ftr_rsp++;
  5495. }
  5496. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5497. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5498. ftr_rsp++;
  5499. if (ftr_rsp) {
  5500. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5501. "0379 Feature Mismatch Data: x%08x %08x "
  5502. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5503. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5504. phba->cfg_enable_npiv, phba->max_vpi);
  5505. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5506. phba->cfg_enable_bg = 0;
  5507. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5508. phba->cfg_enable_npiv = 0;
  5509. }
  5510. /* These SLI3 features are assumed in SLI4 */
  5511. spin_lock_irq(&phba->hbalock);
  5512. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5513. spin_unlock_irq(&phba->hbalock);
  5514. /*
  5515. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5516. * calls depends on these resources to complete port setup.
  5517. */
  5518. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5519. if (rc) {
  5520. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5521. "2920 Failed to alloc Resource IDs "
  5522. "rc = x%x\n", rc);
  5523. goto out_free_mbox;
  5524. }
  5525. /* update physical xri mappings in the scsi buffers */
  5526. lpfc_scsi_buf_update(phba);
  5527. /* Read the port's service parameters. */
  5528. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5529. if (rc) {
  5530. phba->link_state = LPFC_HBA_ERROR;
  5531. rc = -ENOMEM;
  5532. goto out_free_mbox;
  5533. }
  5534. mboxq->vport = vport;
  5535. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5536. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5537. if (rc == MBX_SUCCESS) {
  5538. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5539. rc = 0;
  5540. }
  5541. /*
  5542. * This memory was allocated by the lpfc_read_sparam routine. Release
  5543. * it to the mbuf pool.
  5544. */
  5545. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5546. kfree(mp);
  5547. mboxq->context1 = NULL;
  5548. if (unlikely(rc)) {
  5549. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5550. "0382 READ_SPARAM command failed "
  5551. "status %d, mbxStatus x%x\n",
  5552. rc, bf_get(lpfc_mqe_status, mqe));
  5553. phba->link_state = LPFC_HBA_ERROR;
  5554. rc = -EIO;
  5555. goto out_free_mbox;
  5556. }
  5557. lpfc_update_vport_wwn(vport);
  5558. /* Update the fc_host data structures with new wwn. */
  5559. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5560. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5561. /* Register SGL pool to the device using non-embedded mailbox command */
  5562. if (!phba->sli4_hba.extents_in_use) {
  5563. rc = lpfc_sli4_post_els_sgl_list(phba);
  5564. if (unlikely(rc)) {
  5565. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5566. "0582 Error %d during els sgl post "
  5567. "operation\n", rc);
  5568. rc = -ENODEV;
  5569. goto out_free_mbox;
  5570. }
  5571. } else {
  5572. rc = lpfc_sli4_post_els_sgl_list_ext(phba);
  5573. if (unlikely(rc)) {
  5574. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5575. "2560 Error %d during els sgl post "
  5576. "operation\n", rc);
  5577. rc = -ENODEV;
  5578. goto out_free_mbox;
  5579. }
  5580. }
  5581. /* Register SCSI SGL pool to the device */
  5582. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5583. if (unlikely(rc)) {
  5584. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5585. "0383 Error %d during scsi sgl post "
  5586. "operation\n", rc);
  5587. /* Some Scsi buffers were moved to the abort scsi list */
  5588. /* A pci function reset will repost them */
  5589. rc = -ENODEV;
  5590. goto out_free_mbox;
  5591. }
  5592. /* Post the rpi header region to the device. */
  5593. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5594. if (unlikely(rc)) {
  5595. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5596. "0393 Error %d during rpi post operation\n",
  5597. rc);
  5598. rc = -ENODEV;
  5599. goto out_free_mbox;
  5600. }
  5601. /* Create all the SLI4 queues */
  5602. rc = lpfc_sli4_queue_create(phba);
  5603. if (rc) {
  5604. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5605. "3089 Failed to allocate queues\n");
  5606. rc = -ENODEV;
  5607. goto out_stop_timers;
  5608. }
  5609. /* Set up all the queues to the device */
  5610. rc = lpfc_sli4_queue_setup(phba);
  5611. if (unlikely(rc)) {
  5612. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5613. "0381 Error %d during queue setup.\n ", rc);
  5614. goto out_destroy_queue;
  5615. }
  5616. /* Arm the CQs and then EQs on device */
  5617. lpfc_sli4_arm_cqeq_intr(phba);
  5618. /* Indicate device interrupt mode */
  5619. phba->sli4_hba.intr_enable = 1;
  5620. /* Allow asynchronous mailbox command to go through */
  5621. spin_lock_irq(&phba->hbalock);
  5622. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5623. spin_unlock_irq(&phba->hbalock);
  5624. /* Post receive buffers to the device */
  5625. lpfc_sli4_rb_setup(phba);
  5626. /* Reset HBA FCF states after HBA reset */
  5627. phba->fcf.fcf_flag = 0;
  5628. phba->fcf.current_rec.flag = 0;
  5629. /* Start the ELS watchdog timer */
  5630. mod_timer(&vport->els_tmofunc,
  5631. jiffies + HZ * (phba->fc_ratov * 2));
  5632. /* Start heart beat timer */
  5633. mod_timer(&phba->hb_tmofunc,
  5634. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5635. phba->hb_outstanding = 0;
  5636. phba->last_completion_time = jiffies;
  5637. /* Start error attention (ERATT) polling timer */
  5638. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5639. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5640. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5641. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5642. if (!rc) {
  5643. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5644. "2829 This device supports "
  5645. "Advanced Error Reporting (AER)\n");
  5646. spin_lock_irq(&phba->hbalock);
  5647. phba->hba_flag |= HBA_AER_ENABLED;
  5648. spin_unlock_irq(&phba->hbalock);
  5649. } else {
  5650. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5651. "2830 This device does not support "
  5652. "Advanced Error Reporting (AER)\n");
  5653. phba->cfg_aer_support = 0;
  5654. }
  5655. rc = 0;
  5656. }
  5657. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5658. /*
  5659. * The FC Port needs to register FCFI (index 0)
  5660. */
  5661. lpfc_reg_fcfi(phba, mboxq);
  5662. mboxq->vport = phba->pport;
  5663. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5664. if (rc != MBX_SUCCESS)
  5665. goto out_unset_queue;
  5666. rc = 0;
  5667. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5668. &mboxq->u.mqe.un.reg_fcfi);
  5669. /* Check if the port is configured to be disabled */
  5670. lpfc_sli_read_link_ste(phba);
  5671. }
  5672. /*
  5673. * The port is ready, set the host's link state to LINK_DOWN
  5674. * in preparation for link interrupts.
  5675. */
  5676. spin_lock_irq(&phba->hbalock);
  5677. phba->link_state = LPFC_LINK_DOWN;
  5678. spin_unlock_irq(&phba->hbalock);
  5679. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5680. (phba->hba_flag & LINK_DISABLED)) {
  5681. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5682. "3103 Adapter Link is disabled.\n");
  5683. lpfc_down_link(phba, mboxq);
  5684. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5685. if (rc != MBX_SUCCESS) {
  5686. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5687. "3104 Adapter failed to issue "
  5688. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5689. goto out_unset_queue;
  5690. }
  5691. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5692. /* don't perform init_link on SLI4 FC port loopback test */
  5693. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5694. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5695. if (rc)
  5696. goto out_unset_queue;
  5697. }
  5698. }
  5699. mempool_free(mboxq, phba->mbox_mem_pool);
  5700. return rc;
  5701. out_unset_queue:
  5702. /* Unset all the queues set up in this routine when error out */
  5703. lpfc_sli4_queue_unset(phba);
  5704. out_destroy_queue:
  5705. lpfc_sli4_queue_destroy(phba);
  5706. out_stop_timers:
  5707. lpfc_stop_hba_timers(phba);
  5708. out_free_mbox:
  5709. mempool_free(mboxq, phba->mbox_mem_pool);
  5710. return rc;
  5711. }
  5712. /**
  5713. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5714. * @ptr: context object - pointer to hba structure.
  5715. *
  5716. * This is the callback function for mailbox timer. The mailbox
  5717. * timer is armed when a new mailbox command is issued and the timer
  5718. * is deleted when the mailbox complete. The function is called by
  5719. * the kernel timer code when a mailbox does not complete within
  5720. * expected time. This function wakes up the worker thread to
  5721. * process the mailbox timeout and returns. All the processing is
  5722. * done by the worker thread function lpfc_mbox_timeout_handler.
  5723. **/
  5724. void
  5725. lpfc_mbox_timeout(unsigned long ptr)
  5726. {
  5727. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5728. unsigned long iflag;
  5729. uint32_t tmo_posted;
  5730. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5731. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5732. if (!tmo_posted)
  5733. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5734. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5735. if (!tmo_posted)
  5736. lpfc_worker_wake_up(phba);
  5737. return;
  5738. }
  5739. /**
  5740. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5741. * @phba: Pointer to HBA context object.
  5742. *
  5743. * This function is called from worker thread when a mailbox command times out.
  5744. * The caller is not required to hold any locks. This function will reset the
  5745. * HBA and recover all the pending commands.
  5746. **/
  5747. void
  5748. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5749. {
  5750. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5751. MAILBOX_t *mb = &pmbox->u.mb;
  5752. struct lpfc_sli *psli = &phba->sli;
  5753. struct lpfc_sli_ring *pring;
  5754. /* Check the pmbox pointer first. There is a race condition
  5755. * between the mbox timeout handler getting executed in the
  5756. * worklist and the mailbox actually completing. When this
  5757. * race condition occurs, the mbox_active will be NULL.
  5758. */
  5759. spin_lock_irq(&phba->hbalock);
  5760. if (pmbox == NULL) {
  5761. lpfc_printf_log(phba, KERN_WARNING,
  5762. LOG_MBOX | LOG_SLI,
  5763. "0353 Active Mailbox cleared - mailbox timeout "
  5764. "exiting\n");
  5765. spin_unlock_irq(&phba->hbalock);
  5766. return;
  5767. }
  5768. /* Mbox cmd <mbxCommand> timeout */
  5769. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5770. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5771. mb->mbxCommand,
  5772. phba->pport->port_state,
  5773. phba->sli.sli_flag,
  5774. phba->sli.mbox_active);
  5775. spin_unlock_irq(&phba->hbalock);
  5776. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5777. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5778. * it to fail all outstanding SCSI IO.
  5779. */
  5780. spin_lock_irq(&phba->pport->work_port_lock);
  5781. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5782. spin_unlock_irq(&phba->pport->work_port_lock);
  5783. spin_lock_irq(&phba->hbalock);
  5784. phba->link_state = LPFC_LINK_UNKNOWN;
  5785. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5786. spin_unlock_irq(&phba->hbalock);
  5787. pring = &psli->ring[psli->fcp_ring];
  5788. lpfc_sli_abort_iocb_ring(phba, pring);
  5789. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5790. "0345 Resetting board due to mailbox timeout\n");
  5791. /* Reset the HBA device */
  5792. lpfc_reset_hba(phba);
  5793. }
  5794. /**
  5795. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  5796. * @phba: Pointer to HBA context object.
  5797. * @pmbox: Pointer to mailbox object.
  5798. * @flag: Flag indicating how the mailbox need to be processed.
  5799. *
  5800. * This function is called by discovery code and HBA management code
  5801. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  5802. * function gets the hbalock to protect the data structures.
  5803. * The mailbox command can be submitted in polling mode, in which case
  5804. * this function will wait in a polling loop for the completion of the
  5805. * mailbox.
  5806. * If the mailbox is submitted in no_wait mode (not polling) the
  5807. * function will submit the command and returns immediately without waiting
  5808. * for the mailbox completion. The no_wait is supported only when HBA
  5809. * is in SLI2/SLI3 mode - interrupts are enabled.
  5810. * The SLI interface allows only one mailbox pending at a time. If the
  5811. * mailbox is issued in polling mode and there is already a mailbox
  5812. * pending, then the function will return an error. If the mailbox is issued
  5813. * in NO_WAIT mode and there is a mailbox pending already, the function
  5814. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  5815. * The sli layer owns the mailbox object until the completion of mailbox
  5816. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  5817. * return codes the caller owns the mailbox command after the return of
  5818. * the function.
  5819. **/
  5820. static int
  5821. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  5822. uint32_t flag)
  5823. {
  5824. MAILBOX_t *mb;
  5825. struct lpfc_sli *psli = &phba->sli;
  5826. uint32_t status, evtctr;
  5827. uint32_t ha_copy, hc_copy;
  5828. int i;
  5829. unsigned long timeout;
  5830. unsigned long drvr_flag = 0;
  5831. uint32_t word0, ldata;
  5832. void __iomem *to_slim;
  5833. int processing_queue = 0;
  5834. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5835. if (!pmbox) {
  5836. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5837. /* processing mbox queue from intr_handler */
  5838. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5839. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5840. return MBX_SUCCESS;
  5841. }
  5842. processing_queue = 1;
  5843. pmbox = lpfc_mbox_get(phba);
  5844. if (!pmbox) {
  5845. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5846. return MBX_SUCCESS;
  5847. }
  5848. }
  5849. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  5850. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  5851. if(!pmbox->vport) {
  5852. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5853. lpfc_printf_log(phba, KERN_ERR,
  5854. LOG_MBOX | LOG_VPORT,
  5855. "1806 Mbox x%x failed. No vport\n",
  5856. pmbox->u.mb.mbxCommand);
  5857. dump_stack();
  5858. goto out_not_finished;
  5859. }
  5860. }
  5861. /* If the PCI channel is in offline state, do not post mbox. */
  5862. if (unlikely(pci_channel_offline(phba->pcidev))) {
  5863. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5864. goto out_not_finished;
  5865. }
  5866. /* If HBA has a deferred error attention, fail the iocb. */
  5867. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  5868. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5869. goto out_not_finished;
  5870. }
  5871. psli = &phba->sli;
  5872. mb = &pmbox->u.mb;
  5873. status = MBX_SUCCESS;
  5874. if (phba->link_state == LPFC_HBA_ERROR) {
  5875. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5876. /* Mbox command <mbxCommand> cannot issue */
  5877. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5878. "(%d):0311 Mailbox command x%x cannot "
  5879. "issue Data: x%x x%x\n",
  5880. pmbox->vport ? pmbox->vport->vpi : 0,
  5881. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  5882. goto out_not_finished;
  5883. }
  5884. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  5885. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  5886. !(hc_copy & HC_MBINT_ENA)) {
  5887. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5888. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5889. "(%d):2528 Mailbox command x%x cannot "
  5890. "issue Data: x%x x%x\n",
  5891. pmbox->vport ? pmbox->vport->vpi : 0,
  5892. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  5893. goto out_not_finished;
  5894. }
  5895. }
  5896. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  5897. /* Polling for a mbox command when another one is already active
  5898. * is not allowed in SLI. Also, the driver must have established
  5899. * SLI2 mode to queue and process multiple mbox commands.
  5900. */
  5901. if (flag & MBX_POLL) {
  5902. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5903. /* Mbox command <mbxCommand> cannot issue */
  5904. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5905. "(%d):2529 Mailbox command x%x "
  5906. "cannot issue Data: x%x x%x\n",
  5907. pmbox->vport ? pmbox->vport->vpi : 0,
  5908. pmbox->u.mb.mbxCommand,
  5909. psli->sli_flag, flag);
  5910. goto out_not_finished;
  5911. }
  5912. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  5913. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5914. /* Mbox command <mbxCommand> cannot issue */
  5915. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5916. "(%d):2530 Mailbox command x%x "
  5917. "cannot issue Data: x%x x%x\n",
  5918. pmbox->vport ? pmbox->vport->vpi : 0,
  5919. pmbox->u.mb.mbxCommand,
  5920. psli->sli_flag, flag);
  5921. goto out_not_finished;
  5922. }
  5923. /* Another mailbox command is still being processed, queue this
  5924. * command to be processed later.
  5925. */
  5926. lpfc_mbox_put(phba, pmbox);
  5927. /* Mbox cmd issue - BUSY */
  5928. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5929. "(%d):0308 Mbox cmd issue - BUSY Data: "
  5930. "x%x x%x x%x x%x\n",
  5931. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  5932. mb->mbxCommand, phba->pport->port_state,
  5933. psli->sli_flag, flag);
  5934. psli->slistat.mbox_busy++;
  5935. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5936. if (pmbox->vport) {
  5937. lpfc_debugfs_disc_trc(pmbox->vport,
  5938. LPFC_DISC_TRC_MBOX_VPORT,
  5939. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  5940. (uint32_t)mb->mbxCommand,
  5941. mb->un.varWords[0], mb->un.varWords[1]);
  5942. }
  5943. else {
  5944. lpfc_debugfs_disc_trc(phba->pport,
  5945. LPFC_DISC_TRC_MBOX,
  5946. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  5947. (uint32_t)mb->mbxCommand,
  5948. mb->un.varWords[0], mb->un.varWords[1]);
  5949. }
  5950. return MBX_BUSY;
  5951. }
  5952. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  5953. /* If we are not polling, we MUST be in SLI2 mode */
  5954. if (flag != MBX_POLL) {
  5955. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  5956. (mb->mbxCommand != MBX_KILL_BOARD)) {
  5957. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5958. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5959. /* Mbox command <mbxCommand> cannot issue */
  5960. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5961. "(%d):2531 Mailbox command x%x "
  5962. "cannot issue Data: x%x x%x\n",
  5963. pmbox->vport ? pmbox->vport->vpi : 0,
  5964. pmbox->u.mb.mbxCommand,
  5965. psli->sli_flag, flag);
  5966. goto out_not_finished;
  5967. }
  5968. /* timeout active mbox command */
  5969. mod_timer(&psli->mbox_tmo, (jiffies +
  5970. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  5971. }
  5972. /* Mailbox cmd <cmd> issue */
  5973. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5974. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  5975. "x%x\n",
  5976. pmbox->vport ? pmbox->vport->vpi : 0,
  5977. mb->mbxCommand, phba->pport->port_state,
  5978. psli->sli_flag, flag);
  5979. if (mb->mbxCommand != MBX_HEARTBEAT) {
  5980. if (pmbox->vport) {
  5981. lpfc_debugfs_disc_trc(pmbox->vport,
  5982. LPFC_DISC_TRC_MBOX_VPORT,
  5983. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  5984. (uint32_t)mb->mbxCommand,
  5985. mb->un.varWords[0], mb->un.varWords[1]);
  5986. }
  5987. else {
  5988. lpfc_debugfs_disc_trc(phba->pport,
  5989. LPFC_DISC_TRC_MBOX,
  5990. "MBOX Send: cmd:x%x mb:x%x x%x",
  5991. (uint32_t)mb->mbxCommand,
  5992. mb->un.varWords[0], mb->un.varWords[1]);
  5993. }
  5994. }
  5995. psli->slistat.mbox_cmd++;
  5996. evtctr = psli->slistat.mbox_event;
  5997. /* next set own bit for the adapter and copy over command word */
  5998. mb->mbxOwner = OWN_CHIP;
  5999. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6000. /* Populate mbox extension offset word. */
  6001. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6002. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6003. = (uint8_t *)phba->mbox_ext
  6004. - (uint8_t *)phba->mbox;
  6005. }
  6006. /* Copy the mailbox extension data */
  6007. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6008. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6009. (uint8_t *)phba->mbox_ext,
  6010. pmbox->in_ext_byte_len);
  6011. }
  6012. /* Copy command data to host SLIM area */
  6013. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6014. } else {
  6015. /* Populate mbox extension offset word. */
  6016. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6017. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6018. = MAILBOX_HBA_EXT_OFFSET;
  6019. /* Copy the mailbox extension data */
  6020. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6021. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6022. MAILBOX_HBA_EXT_OFFSET,
  6023. pmbox->context2, pmbox->in_ext_byte_len);
  6024. }
  6025. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6026. /* copy command data into host mbox for cmpl */
  6027. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6028. }
  6029. /* First copy mbox command data to HBA SLIM, skip past first
  6030. word */
  6031. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6032. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  6033. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6034. /* Next copy over first word, with mbxOwner set */
  6035. ldata = *((uint32_t *)mb);
  6036. to_slim = phba->MBslimaddr;
  6037. writel(ldata, to_slim);
  6038. readl(to_slim); /* flush */
  6039. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6040. /* switch over to host mailbox */
  6041. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6042. }
  6043. }
  6044. wmb();
  6045. switch (flag) {
  6046. case MBX_NOWAIT:
  6047. /* Set up reference to mailbox command */
  6048. psli->mbox_active = pmbox;
  6049. /* Interrupt board to do it */
  6050. writel(CA_MBATT, phba->CAregaddr);
  6051. readl(phba->CAregaddr); /* flush */
  6052. /* Don't wait for it to finish, just return */
  6053. break;
  6054. case MBX_POLL:
  6055. /* Set up null reference to mailbox command */
  6056. psli->mbox_active = NULL;
  6057. /* Interrupt board to do it */
  6058. writel(CA_MBATT, phba->CAregaddr);
  6059. readl(phba->CAregaddr); /* flush */
  6060. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6061. /* First read mbox status word */
  6062. word0 = *((uint32_t *)phba->mbox);
  6063. word0 = le32_to_cpu(word0);
  6064. } else {
  6065. /* First read mbox status word */
  6066. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6067. spin_unlock_irqrestore(&phba->hbalock,
  6068. drvr_flag);
  6069. goto out_not_finished;
  6070. }
  6071. }
  6072. /* Read the HBA Host Attention Register */
  6073. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6074. spin_unlock_irqrestore(&phba->hbalock,
  6075. drvr_flag);
  6076. goto out_not_finished;
  6077. }
  6078. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6079. 1000) + jiffies;
  6080. i = 0;
  6081. /* Wait for command to complete */
  6082. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6083. (!(ha_copy & HA_MBATT) &&
  6084. (phba->link_state > LPFC_WARM_START))) {
  6085. if (time_after(jiffies, timeout)) {
  6086. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6087. spin_unlock_irqrestore(&phba->hbalock,
  6088. drvr_flag);
  6089. goto out_not_finished;
  6090. }
  6091. /* Check if we took a mbox interrupt while we were
  6092. polling */
  6093. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6094. && (evtctr != psli->slistat.mbox_event))
  6095. break;
  6096. if (i++ > 10) {
  6097. spin_unlock_irqrestore(&phba->hbalock,
  6098. drvr_flag);
  6099. msleep(1);
  6100. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6101. }
  6102. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6103. /* First copy command data */
  6104. word0 = *((uint32_t *)phba->mbox);
  6105. word0 = le32_to_cpu(word0);
  6106. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6107. MAILBOX_t *slimmb;
  6108. uint32_t slimword0;
  6109. /* Check real SLIM for any errors */
  6110. slimword0 = readl(phba->MBslimaddr);
  6111. slimmb = (MAILBOX_t *) & slimword0;
  6112. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6113. && slimmb->mbxStatus) {
  6114. psli->sli_flag &=
  6115. ~LPFC_SLI_ACTIVE;
  6116. word0 = slimword0;
  6117. }
  6118. }
  6119. } else {
  6120. /* First copy command data */
  6121. word0 = readl(phba->MBslimaddr);
  6122. }
  6123. /* Read the HBA Host Attention Register */
  6124. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6125. spin_unlock_irqrestore(&phba->hbalock,
  6126. drvr_flag);
  6127. goto out_not_finished;
  6128. }
  6129. }
  6130. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6131. /* copy results back to user */
  6132. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  6133. /* Copy the mailbox extension data */
  6134. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6135. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6136. pmbox->context2,
  6137. pmbox->out_ext_byte_len);
  6138. }
  6139. } else {
  6140. /* First copy command data */
  6141. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  6142. MAILBOX_CMD_SIZE);
  6143. /* Copy the mailbox extension data */
  6144. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6145. lpfc_memcpy_from_slim(pmbox->context2,
  6146. phba->MBslimaddr +
  6147. MAILBOX_HBA_EXT_OFFSET,
  6148. pmbox->out_ext_byte_len);
  6149. }
  6150. }
  6151. writel(HA_MBATT, phba->HAregaddr);
  6152. readl(phba->HAregaddr); /* flush */
  6153. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6154. status = mb->mbxStatus;
  6155. }
  6156. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6157. return status;
  6158. out_not_finished:
  6159. if (processing_queue) {
  6160. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6161. lpfc_mbox_cmpl_put(phba, pmbox);
  6162. }
  6163. return MBX_NOT_FINISHED;
  6164. }
  6165. /**
  6166. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6167. * @phba: Pointer to HBA context object.
  6168. *
  6169. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6170. * the driver internal pending mailbox queue. It will then try to wait out the
  6171. * possible outstanding mailbox command before return.
  6172. *
  6173. * Returns:
  6174. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6175. * the outstanding mailbox command timed out.
  6176. **/
  6177. static int
  6178. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6179. {
  6180. struct lpfc_sli *psli = &phba->sli;
  6181. int rc = 0;
  6182. unsigned long timeout = 0;
  6183. /* Mark the asynchronous mailbox command posting as blocked */
  6184. spin_lock_irq(&phba->hbalock);
  6185. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6186. /* Determine how long we might wait for the active mailbox
  6187. * command to be gracefully completed by firmware.
  6188. */
  6189. if (phba->sli.mbox_active)
  6190. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6191. phba->sli.mbox_active) *
  6192. 1000) + jiffies;
  6193. spin_unlock_irq(&phba->hbalock);
  6194. /* Wait for the outstnading mailbox command to complete */
  6195. while (phba->sli.mbox_active) {
  6196. /* Check active mailbox complete status every 2ms */
  6197. msleep(2);
  6198. if (time_after(jiffies, timeout)) {
  6199. /* Timeout, marked the outstanding cmd not complete */
  6200. rc = 1;
  6201. break;
  6202. }
  6203. }
  6204. /* Can not cleanly block async mailbox command, fails it */
  6205. if (rc) {
  6206. spin_lock_irq(&phba->hbalock);
  6207. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6208. spin_unlock_irq(&phba->hbalock);
  6209. }
  6210. return rc;
  6211. }
  6212. /**
  6213. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6214. * @phba: Pointer to HBA context object.
  6215. *
  6216. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6217. * commands from the driver internal pending mailbox queue. It makes sure
  6218. * that there is no outstanding mailbox command before resuming posting
  6219. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6220. * mailbox command, it will try to wait it out before resuming asynchronous
  6221. * mailbox command posting.
  6222. **/
  6223. static void
  6224. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6225. {
  6226. struct lpfc_sli *psli = &phba->sli;
  6227. spin_lock_irq(&phba->hbalock);
  6228. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6229. /* Asynchronous mailbox posting is not blocked, do nothing */
  6230. spin_unlock_irq(&phba->hbalock);
  6231. return;
  6232. }
  6233. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6234. * successful or timeout, after timing-out the outstanding mailbox
  6235. * command shall always be removed, so just unblock posting async
  6236. * mailbox command and resume
  6237. */
  6238. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6239. spin_unlock_irq(&phba->hbalock);
  6240. /* wake up worker thread to post asynchronlous mailbox command */
  6241. lpfc_worker_wake_up(phba);
  6242. }
  6243. /**
  6244. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6245. * @phba: Pointer to HBA context object.
  6246. * @mboxq: Pointer to mailbox object.
  6247. *
  6248. * The function posts a mailbox to the port. The mailbox is expected
  6249. * to be comletely filled in and ready for the port to operate on it.
  6250. * This routine executes a synchronous completion operation on the
  6251. * mailbox by polling for its completion.
  6252. *
  6253. * The caller must not be holding any locks when calling this routine.
  6254. *
  6255. * Returns:
  6256. * MBX_SUCCESS - mailbox posted successfully
  6257. * Any of the MBX error values.
  6258. **/
  6259. static int
  6260. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6261. {
  6262. int rc = MBX_SUCCESS;
  6263. unsigned long iflag;
  6264. uint32_t db_ready;
  6265. uint32_t mcqe_status;
  6266. uint32_t mbx_cmnd;
  6267. unsigned long timeout;
  6268. struct lpfc_sli *psli = &phba->sli;
  6269. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6270. struct lpfc_bmbx_create *mbox_rgn;
  6271. struct dma_address *dma_address;
  6272. struct lpfc_register bmbx_reg;
  6273. /*
  6274. * Only one mailbox can be active to the bootstrap mailbox region
  6275. * at a time and there is no queueing provided.
  6276. */
  6277. spin_lock_irqsave(&phba->hbalock, iflag);
  6278. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6279. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6280. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6281. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6282. "cannot issue Data: x%x x%x\n",
  6283. mboxq->vport ? mboxq->vport->vpi : 0,
  6284. mboxq->u.mb.mbxCommand,
  6285. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6286. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6287. psli->sli_flag, MBX_POLL);
  6288. return MBXERR_ERROR;
  6289. }
  6290. /* The server grabs the token and owns it until release */
  6291. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6292. phba->sli.mbox_active = mboxq;
  6293. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6294. /*
  6295. * Initialize the bootstrap memory region to avoid stale data areas
  6296. * in the mailbox post. Then copy the caller's mailbox contents to
  6297. * the bmbx mailbox region.
  6298. */
  6299. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6300. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6301. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6302. sizeof(struct lpfc_mqe));
  6303. /* Post the high mailbox dma address to the port and wait for ready. */
  6304. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6305. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6306. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6307. * 1000) + jiffies;
  6308. do {
  6309. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6310. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6311. if (!db_ready)
  6312. msleep(2);
  6313. if (time_after(jiffies, timeout)) {
  6314. rc = MBXERR_ERROR;
  6315. goto exit;
  6316. }
  6317. } while (!db_ready);
  6318. /* Post the low mailbox dma address to the port. */
  6319. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6320. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6321. * 1000) + jiffies;
  6322. do {
  6323. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6324. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6325. if (!db_ready)
  6326. msleep(2);
  6327. if (time_after(jiffies, timeout)) {
  6328. rc = MBXERR_ERROR;
  6329. goto exit;
  6330. }
  6331. } while (!db_ready);
  6332. /*
  6333. * Read the CQ to ensure the mailbox has completed.
  6334. * If so, update the mailbox status so that the upper layers
  6335. * can complete the request normally.
  6336. */
  6337. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6338. sizeof(struct lpfc_mqe));
  6339. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6340. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6341. sizeof(struct lpfc_mcqe));
  6342. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6343. /*
  6344. * When the CQE status indicates a failure and the mailbox status
  6345. * indicates success then copy the CQE status into the mailbox status
  6346. * (and prefix it with x4000).
  6347. */
  6348. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6349. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6350. bf_set(lpfc_mqe_status, mb,
  6351. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6352. rc = MBXERR_ERROR;
  6353. } else
  6354. lpfc_sli4_swap_str(phba, mboxq);
  6355. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6356. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6357. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6358. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6359. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6360. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6361. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6362. bf_get(lpfc_mqe_status, mb),
  6363. mb->un.mb_words[0], mb->un.mb_words[1],
  6364. mb->un.mb_words[2], mb->un.mb_words[3],
  6365. mb->un.mb_words[4], mb->un.mb_words[5],
  6366. mb->un.mb_words[6], mb->un.mb_words[7],
  6367. mb->un.mb_words[8], mb->un.mb_words[9],
  6368. mb->un.mb_words[10], mb->un.mb_words[11],
  6369. mb->un.mb_words[12], mboxq->mcqe.word0,
  6370. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6371. mboxq->mcqe.trailer);
  6372. exit:
  6373. /* We are holding the token, no needed for lock when release */
  6374. spin_lock_irqsave(&phba->hbalock, iflag);
  6375. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6376. phba->sli.mbox_active = NULL;
  6377. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6378. return rc;
  6379. }
  6380. /**
  6381. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6382. * @phba: Pointer to HBA context object.
  6383. * @pmbox: Pointer to mailbox object.
  6384. * @flag: Flag indicating how the mailbox need to be processed.
  6385. *
  6386. * This function is called by discovery code and HBA management code to submit
  6387. * a mailbox command to firmware with SLI-4 interface spec.
  6388. *
  6389. * Return codes the caller owns the mailbox command after the return of the
  6390. * function.
  6391. **/
  6392. static int
  6393. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6394. uint32_t flag)
  6395. {
  6396. struct lpfc_sli *psli = &phba->sli;
  6397. unsigned long iflags;
  6398. int rc;
  6399. /* dump from issue mailbox command if setup */
  6400. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6401. rc = lpfc_mbox_dev_check(phba);
  6402. if (unlikely(rc)) {
  6403. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6404. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6405. "cannot issue Data: x%x x%x\n",
  6406. mboxq->vport ? mboxq->vport->vpi : 0,
  6407. mboxq->u.mb.mbxCommand,
  6408. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6409. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6410. psli->sli_flag, flag);
  6411. goto out_not_finished;
  6412. }
  6413. /* Detect polling mode and jump to a handler */
  6414. if (!phba->sli4_hba.intr_enable) {
  6415. if (flag == MBX_POLL)
  6416. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6417. else
  6418. rc = -EIO;
  6419. if (rc != MBX_SUCCESS)
  6420. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6421. "(%d):2541 Mailbox command x%x "
  6422. "(x%x/x%x) cannot issue Data: "
  6423. "x%x x%x\n",
  6424. mboxq->vport ? mboxq->vport->vpi : 0,
  6425. mboxq->u.mb.mbxCommand,
  6426. lpfc_sli_config_mbox_subsys_get(phba,
  6427. mboxq),
  6428. lpfc_sli_config_mbox_opcode_get(phba,
  6429. mboxq),
  6430. psli->sli_flag, flag);
  6431. return rc;
  6432. } else if (flag == MBX_POLL) {
  6433. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6434. "(%d):2542 Try to issue mailbox command "
  6435. "x%x (x%x/x%x) synchronously ahead of async"
  6436. "mailbox command queue: x%x x%x\n",
  6437. mboxq->vport ? mboxq->vport->vpi : 0,
  6438. mboxq->u.mb.mbxCommand,
  6439. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6440. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6441. psli->sli_flag, flag);
  6442. /* Try to block the asynchronous mailbox posting */
  6443. rc = lpfc_sli4_async_mbox_block(phba);
  6444. if (!rc) {
  6445. /* Successfully blocked, now issue sync mbox cmd */
  6446. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6447. if (rc != MBX_SUCCESS)
  6448. lpfc_printf_log(phba, KERN_ERR,
  6449. LOG_MBOX | LOG_SLI,
  6450. "(%d):2597 Mailbox command "
  6451. "x%x (x%x/x%x) cannot issue "
  6452. "Data: x%x x%x\n",
  6453. mboxq->vport ?
  6454. mboxq->vport->vpi : 0,
  6455. mboxq->u.mb.mbxCommand,
  6456. lpfc_sli_config_mbox_subsys_get(phba,
  6457. mboxq),
  6458. lpfc_sli_config_mbox_opcode_get(phba,
  6459. mboxq),
  6460. psli->sli_flag, flag);
  6461. /* Unblock the async mailbox posting afterward */
  6462. lpfc_sli4_async_mbox_unblock(phba);
  6463. }
  6464. return rc;
  6465. }
  6466. /* Now, interrupt mode asynchrous mailbox command */
  6467. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6468. if (rc) {
  6469. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6470. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6471. "cannot issue Data: x%x x%x\n",
  6472. mboxq->vport ? mboxq->vport->vpi : 0,
  6473. mboxq->u.mb.mbxCommand,
  6474. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6475. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6476. psli->sli_flag, flag);
  6477. goto out_not_finished;
  6478. }
  6479. /* Put the mailbox command to the driver internal FIFO */
  6480. psli->slistat.mbox_busy++;
  6481. spin_lock_irqsave(&phba->hbalock, iflags);
  6482. lpfc_mbox_put(phba, mboxq);
  6483. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6484. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6485. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6486. "x%x (x%x/x%x) x%x x%x x%x\n",
  6487. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6488. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6489. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6490. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6491. phba->pport->port_state,
  6492. psli->sli_flag, MBX_NOWAIT);
  6493. /* Wake up worker thread to transport mailbox command from head */
  6494. lpfc_worker_wake_up(phba);
  6495. return MBX_BUSY;
  6496. out_not_finished:
  6497. return MBX_NOT_FINISHED;
  6498. }
  6499. /**
  6500. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6501. * @phba: Pointer to HBA context object.
  6502. *
  6503. * This function is called by worker thread to send a mailbox command to
  6504. * SLI4 HBA firmware.
  6505. *
  6506. **/
  6507. int
  6508. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6509. {
  6510. struct lpfc_sli *psli = &phba->sli;
  6511. LPFC_MBOXQ_t *mboxq;
  6512. int rc = MBX_SUCCESS;
  6513. unsigned long iflags;
  6514. struct lpfc_mqe *mqe;
  6515. uint32_t mbx_cmnd;
  6516. /* Check interrupt mode before post async mailbox command */
  6517. if (unlikely(!phba->sli4_hba.intr_enable))
  6518. return MBX_NOT_FINISHED;
  6519. /* Check for mailbox command service token */
  6520. spin_lock_irqsave(&phba->hbalock, iflags);
  6521. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6522. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6523. return MBX_NOT_FINISHED;
  6524. }
  6525. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6526. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6527. return MBX_NOT_FINISHED;
  6528. }
  6529. if (unlikely(phba->sli.mbox_active)) {
  6530. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6531. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6532. "0384 There is pending active mailbox cmd\n");
  6533. return MBX_NOT_FINISHED;
  6534. }
  6535. /* Take the mailbox command service token */
  6536. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6537. /* Get the next mailbox command from head of queue */
  6538. mboxq = lpfc_mbox_get(phba);
  6539. /* If no more mailbox command waiting for post, we're done */
  6540. if (!mboxq) {
  6541. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6542. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6543. return MBX_SUCCESS;
  6544. }
  6545. phba->sli.mbox_active = mboxq;
  6546. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6547. /* Check device readiness for posting mailbox command */
  6548. rc = lpfc_mbox_dev_check(phba);
  6549. if (unlikely(rc))
  6550. /* Driver clean routine will clean up pending mailbox */
  6551. goto out_not_finished;
  6552. /* Prepare the mbox command to be posted */
  6553. mqe = &mboxq->u.mqe;
  6554. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6555. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6556. mod_timer(&psli->mbox_tmo, (jiffies +
  6557. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6558. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6559. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6560. "x%x x%x\n",
  6561. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6562. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6563. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6564. phba->pport->port_state, psli->sli_flag);
  6565. if (mbx_cmnd != MBX_HEARTBEAT) {
  6566. if (mboxq->vport) {
  6567. lpfc_debugfs_disc_trc(mboxq->vport,
  6568. LPFC_DISC_TRC_MBOX_VPORT,
  6569. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6570. mbx_cmnd, mqe->un.mb_words[0],
  6571. mqe->un.mb_words[1]);
  6572. } else {
  6573. lpfc_debugfs_disc_trc(phba->pport,
  6574. LPFC_DISC_TRC_MBOX,
  6575. "MBOX Send: cmd:x%x mb:x%x x%x",
  6576. mbx_cmnd, mqe->un.mb_words[0],
  6577. mqe->un.mb_words[1]);
  6578. }
  6579. }
  6580. psli->slistat.mbox_cmd++;
  6581. /* Post the mailbox command to the port */
  6582. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6583. if (rc != MBX_SUCCESS) {
  6584. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6585. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6586. "cannot issue Data: x%x x%x\n",
  6587. mboxq->vport ? mboxq->vport->vpi : 0,
  6588. mboxq->u.mb.mbxCommand,
  6589. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6590. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6591. psli->sli_flag, MBX_NOWAIT);
  6592. goto out_not_finished;
  6593. }
  6594. return rc;
  6595. out_not_finished:
  6596. spin_lock_irqsave(&phba->hbalock, iflags);
  6597. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6598. __lpfc_mbox_cmpl_put(phba, mboxq);
  6599. /* Release the token */
  6600. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6601. phba->sli.mbox_active = NULL;
  6602. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6603. return MBX_NOT_FINISHED;
  6604. }
  6605. /**
  6606. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6607. * @phba: Pointer to HBA context object.
  6608. * @pmbox: Pointer to mailbox object.
  6609. * @flag: Flag indicating how the mailbox need to be processed.
  6610. *
  6611. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6612. * the API jump table function pointer from the lpfc_hba struct.
  6613. *
  6614. * Return codes the caller owns the mailbox command after the return of the
  6615. * function.
  6616. **/
  6617. int
  6618. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6619. {
  6620. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6621. }
  6622. /**
  6623. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6624. * @phba: The hba struct for which this call is being executed.
  6625. * @dev_grp: The HBA PCI-Device group number.
  6626. *
  6627. * This routine sets up the mbox interface API function jump table in @phba
  6628. * struct.
  6629. * Returns: 0 - success, -ENODEV - failure.
  6630. **/
  6631. int
  6632. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6633. {
  6634. switch (dev_grp) {
  6635. case LPFC_PCI_DEV_LP:
  6636. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6637. phba->lpfc_sli_handle_slow_ring_event =
  6638. lpfc_sli_handle_slow_ring_event_s3;
  6639. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6640. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6641. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6642. break;
  6643. case LPFC_PCI_DEV_OC:
  6644. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6645. phba->lpfc_sli_handle_slow_ring_event =
  6646. lpfc_sli_handle_slow_ring_event_s4;
  6647. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6648. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6649. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6650. break;
  6651. default:
  6652. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6653. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6654. dev_grp);
  6655. return -ENODEV;
  6656. break;
  6657. }
  6658. return 0;
  6659. }
  6660. /**
  6661. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6662. * @phba: Pointer to HBA context object.
  6663. * @pring: Pointer to driver SLI ring object.
  6664. * @piocb: Pointer to address of newly added command iocb.
  6665. *
  6666. * This function is called with hbalock held to add a command
  6667. * iocb to the txq when SLI layer cannot submit the command iocb
  6668. * to the ring.
  6669. **/
  6670. void
  6671. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6672. struct lpfc_iocbq *piocb)
  6673. {
  6674. /* Insert the caller's iocb in the txq tail for later processing. */
  6675. list_add_tail(&piocb->list, &pring->txq);
  6676. pring->txq_cnt++;
  6677. }
  6678. /**
  6679. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6680. * @phba: Pointer to HBA context object.
  6681. * @pring: Pointer to driver SLI ring object.
  6682. * @piocb: Pointer to address of newly added command iocb.
  6683. *
  6684. * This function is called with hbalock held before a new
  6685. * iocb is submitted to the firmware. This function checks
  6686. * txq to flush the iocbs in txq to Firmware before
  6687. * submitting new iocbs to the Firmware.
  6688. * If there are iocbs in the txq which need to be submitted
  6689. * to firmware, lpfc_sli_next_iocb returns the first element
  6690. * of the txq after dequeuing it from txq.
  6691. * If there is no iocb in the txq then the function will return
  6692. * *piocb and *piocb is set to NULL. Caller needs to check
  6693. * *piocb to find if there are more commands in the txq.
  6694. **/
  6695. static struct lpfc_iocbq *
  6696. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6697. struct lpfc_iocbq **piocb)
  6698. {
  6699. struct lpfc_iocbq * nextiocb;
  6700. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6701. if (!nextiocb) {
  6702. nextiocb = *piocb;
  6703. *piocb = NULL;
  6704. }
  6705. return nextiocb;
  6706. }
  6707. /**
  6708. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6709. * @phba: Pointer to HBA context object.
  6710. * @ring_number: SLI ring number to issue iocb on.
  6711. * @piocb: Pointer to command iocb.
  6712. * @flag: Flag indicating if this command can be put into txq.
  6713. *
  6714. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6715. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6716. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6717. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6718. * this function allows only iocbs for posting buffers. This function finds
  6719. * next available slot in the command ring and posts the command to the
  6720. * available slot and writes the port attention register to request HBA start
  6721. * processing new iocb. If there is no slot available in the ring and
  6722. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6723. * the function returns IOCB_BUSY.
  6724. *
  6725. * This function is called with hbalock held. The function will return success
  6726. * after it successfully submit the iocb to firmware or after adding to the
  6727. * txq.
  6728. **/
  6729. static int
  6730. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6731. struct lpfc_iocbq *piocb, uint32_t flag)
  6732. {
  6733. struct lpfc_iocbq *nextiocb;
  6734. IOCB_t *iocb;
  6735. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6736. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6737. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6738. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6739. lpfc_printf_log(phba, KERN_ERR,
  6740. LOG_SLI | LOG_VPORT,
  6741. "1807 IOCB x%x failed. No vport\n",
  6742. piocb->iocb.ulpCommand);
  6743. dump_stack();
  6744. return IOCB_ERROR;
  6745. }
  6746. /* If the PCI channel is in offline state, do not post iocbs. */
  6747. if (unlikely(pci_channel_offline(phba->pcidev)))
  6748. return IOCB_ERROR;
  6749. /* If HBA has a deferred error attention, fail the iocb. */
  6750. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6751. return IOCB_ERROR;
  6752. /*
  6753. * We should never get an IOCB if we are in a < LINK_DOWN state
  6754. */
  6755. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6756. return IOCB_ERROR;
  6757. /*
  6758. * Check to see if we are blocking IOCB processing because of a
  6759. * outstanding event.
  6760. */
  6761. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  6762. goto iocb_busy;
  6763. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  6764. /*
  6765. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  6766. * can be issued if the link is not up.
  6767. */
  6768. switch (piocb->iocb.ulpCommand) {
  6769. case CMD_GEN_REQUEST64_CR:
  6770. case CMD_GEN_REQUEST64_CX:
  6771. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  6772. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  6773. FC_RCTL_DD_UNSOL_CMD) ||
  6774. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  6775. MENLO_TRANSPORT_TYPE))
  6776. goto iocb_busy;
  6777. break;
  6778. case CMD_QUE_RING_BUF_CN:
  6779. case CMD_QUE_RING_BUF64_CN:
  6780. /*
  6781. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  6782. * completion, iocb_cmpl MUST be 0.
  6783. */
  6784. if (piocb->iocb_cmpl)
  6785. piocb->iocb_cmpl = NULL;
  6786. /*FALLTHROUGH*/
  6787. case CMD_CREATE_XRI_CR:
  6788. case CMD_CLOSE_XRI_CN:
  6789. case CMD_CLOSE_XRI_CX:
  6790. break;
  6791. default:
  6792. goto iocb_busy;
  6793. }
  6794. /*
  6795. * For FCP commands, we must be in a state where we can process link
  6796. * attention events.
  6797. */
  6798. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  6799. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  6800. goto iocb_busy;
  6801. }
  6802. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  6803. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  6804. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  6805. if (iocb)
  6806. lpfc_sli_update_ring(phba, pring);
  6807. else
  6808. lpfc_sli_update_full_ring(phba, pring);
  6809. if (!piocb)
  6810. return IOCB_SUCCESS;
  6811. goto out_busy;
  6812. iocb_busy:
  6813. pring->stats.iocb_cmd_delay++;
  6814. out_busy:
  6815. if (!(flag & SLI_IOCB_RET_IOCB)) {
  6816. __lpfc_sli_ringtx_put(phba, pring, piocb);
  6817. return IOCB_SUCCESS;
  6818. }
  6819. return IOCB_BUSY;
  6820. }
  6821. /**
  6822. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  6823. * @phba: Pointer to HBA context object.
  6824. * @piocb: Pointer to command iocb.
  6825. * @sglq: Pointer to the scatter gather queue object.
  6826. *
  6827. * This routine converts the bpl or bde that is in the IOCB
  6828. * to a sgl list for the sli4 hardware. The physical address
  6829. * of the bpl/bde is converted back to a virtual address.
  6830. * If the IOCB contains a BPL then the list of BDE's is
  6831. * converted to sli4_sge's. If the IOCB contains a single
  6832. * BDE then it is converted to a single sli_sge.
  6833. * The IOCB is still in cpu endianess so the contents of
  6834. * the bpl can be used without byte swapping.
  6835. *
  6836. * Returns valid XRI = Success, NO_XRI = Failure.
  6837. **/
  6838. static uint16_t
  6839. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  6840. struct lpfc_sglq *sglq)
  6841. {
  6842. uint16_t xritag = NO_XRI;
  6843. struct ulp_bde64 *bpl = NULL;
  6844. struct ulp_bde64 bde;
  6845. struct sli4_sge *sgl = NULL;
  6846. struct lpfc_dmabuf *dmabuf;
  6847. IOCB_t *icmd;
  6848. int numBdes = 0;
  6849. int i = 0;
  6850. uint32_t offset = 0; /* accumulated offset in the sg request list */
  6851. int inbound = 0; /* number of sg reply entries inbound from firmware */
  6852. if (!piocbq || !sglq)
  6853. return xritag;
  6854. sgl = (struct sli4_sge *)sglq->sgl;
  6855. icmd = &piocbq->iocb;
  6856. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6857. numBdes = icmd->un.genreq64.bdl.bdeSize /
  6858. sizeof(struct ulp_bde64);
  6859. /* The addrHigh and addrLow fields within the IOCB
  6860. * have not been byteswapped yet so there is no
  6861. * need to swap them back.
  6862. */
  6863. if (piocbq->context3)
  6864. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  6865. else
  6866. return xritag;
  6867. bpl = (struct ulp_bde64 *)dmabuf->virt;
  6868. if (!bpl)
  6869. return xritag;
  6870. for (i = 0; i < numBdes; i++) {
  6871. /* Should already be byte swapped. */
  6872. sgl->addr_hi = bpl->addrHigh;
  6873. sgl->addr_lo = bpl->addrLow;
  6874. sgl->word2 = le32_to_cpu(sgl->word2);
  6875. if ((i+1) == numBdes)
  6876. bf_set(lpfc_sli4_sge_last, sgl, 1);
  6877. else
  6878. bf_set(lpfc_sli4_sge_last, sgl, 0);
  6879. /* swap the size field back to the cpu so we
  6880. * can assign it to the sgl.
  6881. */
  6882. bde.tus.w = le32_to_cpu(bpl->tus.w);
  6883. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  6884. /* The offsets in the sgl need to be accumulated
  6885. * separately for the request and reply lists.
  6886. * The request is always first, the reply follows.
  6887. */
  6888. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  6889. /* add up the reply sg entries */
  6890. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  6891. inbound++;
  6892. /* first inbound? reset the offset */
  6893. if (inbound == 1)
  6894. offset = 0;
  6895. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  6896. bf_set(lpfc_sli4_sge_type, sgl,
  6897. LPFC_SGE_TYPE_DATA);
  6898. offset += bde.tus.f.bdeSize;
  6899. }
  6900. sgl->word2 = cpu_to_le32(sgl->word2);
  6901. bpl++;
  6902. sgl++;
  6903. }
  6904. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  6905. /* The addrHigh and addrLow fields of the BDE have not
  6906. * been byteswapped yet so they need to be swapped
  6907. * before putting them in the sgl.
  6908. */
  6909. sgl->addr_hi =
  6910. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  6911. sgl->addr_lo =
  6912. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  6913. sgl->word2 = le32_to_cpu(sgl->word2);
  6914. bf_set(lpfc_sli4_sge_last, sgl, 1);
  6915. sgl->word2 = cpu_to_le32(sgl->word2);
  6916. sgl->sge_len =
  6917. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  6918. }
  6919. return sglq->sli4_xritag;
  6920. }
  6921. /**
  6922. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  6923. * @phba: Pointer to HBA context object.
  6924. *
  6925. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  6926. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  6927. * held.
  6928. *
  6929. * Return: index into SLI4 fast-path FCP queue index.
  6930. **/
  6931. static uint32_t
  6932. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  6933. {
  6934. ++phba->fcp_qidx;
  6935. if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
  6936. phba->fcp_qidx = 0;
  6937. return phba->fcp_qidx;
  6938. }
  6939. /**
  6940. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  6941. * @phba: Pointer to HBA context object.
  6942. * @piocb: Pointer to command iocb.
  6943. * @wqe: Pointer to the work queue entry.
  6944. *
  6945. * This routine converts the iocb command to its Work Queue Entry
  6946. * equivalent. The wqe pointer should not have any fields set when
  6947. * this routine is called because it will memcpy over them.
  6948. * This routine does not set the CQ_ID or the WQEC bits in the
  6949. * wqe.
  6950. *
  6951. * Returns: 0 = Success, IOCB_ERROR = Failure.
  6952. **/
  6953. static int
  6954. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  6955. union lpfc_wqe *wqe)
  6956. {
  6957. uint32_t xmit_len = 0, total_len = 0;
  6958. uint8_t ct = 0;
  6959. uint32_t fip;
  6960. uint32_t abort_tag;
  6961. uint8_t command_type = ELS_COMMAND_NON_FIP;
  6962. uint8_t cmnd;
  6963. uint16_t xritag;
  6964. uint16_t abrt_iotag;
  6965. struct lpfc_iocbq *abrtiocbq;
  6966. struct ulp_bde64 *bpl = NULL;
  6967. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  6968. int numBdes, i;
  6969. struct ulp_bde64 bde;
  6970. struct lpfc_nodelist *ndlp;
  6971. uint32_t *pcmd;
  6972. uint32_t if_type;
  6973. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  6974. /* The fcp commands will set command type */
  6975. if (iocbq->iocb_flag & LPFC_IO_FCP)
  6976. command_type = FCP_COMMAND;
  6977. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  6978. command_type = ELS_COMMAND_FIP;
  6979. else
  6980. command_type = ELS_COMMAND_NON_FIP;
  6981. /* Some of the fields are in the right position already */
  6982. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  6983. abort_tag = (uint32_t) iocbq->iotag;
  6984. xritag = iocbq->sli4_xritag;
  6985. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  6986. /* words0-2 bpl convert bde */
  6987. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6988. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  6989. sizeof(struct ulp_bde64);
  6990. bpl = (struct ulp_bde64 *)
  6991. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  6992. if (!bpl)
  6993. return IOCB_ERROR;
  6994. /* Should already be byte swapped. */
  6995. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  6996. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  6997. /* swap the size field back to the cpu so we
  6998. * can assign it to the sgl.
  6999. */
  7000. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7001. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7002. total_len = 0;
  7003. for (i = 0; i < numBdes; i++) {
  7004. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7005. total_len += bde.tus.f.bdeSize;
  7006. }
  7007. } else
  7008. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7009. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7010. cmnd = iocbq->iocb.ulpCommand;
  7011. switch (iocbq->iocb.ulpCommand) {
  7012. case CMD_ELS_REQUEST64_CR:
  7013. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7014. if (!iocbq->iocb.ulpLe) {
  7015. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7016. "2007 Only Limited Edition cmd Format"
  7017. " supported 0x%x\n",
  7018. iocbq->iocb.ulpCommand);
  7019. return IOCB_ERROR;
  7020. }
  7021. wqe->els_req.payload_len = xmit_len;
  7022. /* Els_reguest64 has a TMO */
  7023. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7024. iocbq->iocb.ulpTimeout);
  7025. /* Need a VF for word 4 set the vf bit*/
  7026. bf_set(els_req64_vf, &wqe->els_req, 0);
  7027. /* And a VFID for word 12 */
  7028. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7029. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7030. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7031. iocbq->iocb.ulpContext);
  7032. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7033. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7034. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7035. if (command_type == ELS_COMMAND_FIP)
  7036. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7037. >> LPFC_FIP_ELS_ID_SHIFT);
  7038. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7039. iocbq->context2)->virt);
  7040. if_type = bf_get(lpfc_sli_intf_if_type,
  7041. &phba->sli4_hba.sli_intf);
  7042. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7043. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7044. *pcmd == ELS_CMD_SCR ||
  7045. *pcmd == ELS_CMD_PLOGI)) {
  7046. bf_set(els_req64_sp, &wqe->els_req, 1);
  7047. bf_set(els_req64_sid, &wqe->els_req,
  7048. iocbq->vport->fc_myDID);
  7049. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7050. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7051. phba->vpi_ids[phba->pport->vpi]);
  7052. } else if (iocbq->context1) {
  7053. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7054. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7055. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7056. }
  7057. }
  7058. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7059. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7060. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7061. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7062. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7063. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7064. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7065. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7066. break;
  7067. case CMD_XMIT_SEQUENCE64_CX:
  7068. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7069. iocbq->iocb.un.ulpWord[3]);
  7070. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7071. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7072. /* The entire sequence is transmitted for this IOCB */
  7073. xmit_len = total_len;
  7074. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7075. if (phba->link_flag & LS_LOOPBACK_MODE)
  7076. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7077. case CMD_XMIT_SEQUENCE64_CR:
  7078. /* word3 iocb=io_tag32 wqe=reserved */
  7079. wqe->xmit_sequence.rsvd3 = 0;
  7080. /* word4 relative_offset memcpy */
  7081. /* word5 r_ctl/df_ctl memcpy */
  7082. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7083. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7084. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7085. LPFC_WQE_IOD_WRITE);
  7086. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7087. LPFC_WQE_LENLOC_WORD12);
  7088. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7089. wqe->xmit_sequence.xmit_len = xmit_len;
  7090. command_type = OTHER_COMMAND;
  7091. break;
  7092. case CMD_XMIT_BCAST64_CN:
  7093. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7094. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7095. /* word4 iocb=rsvd wqe=rsvd */
  7096. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7097. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7098. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7099. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7100. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7101. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7102. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7103. LPFC_WQE_LENLOC_WORD3);
  7104. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7105. break;
  7106. case CMD_FCP_IWRITE64_CR:
  7107. command_type = FCP_COMMAND_DATA_OUT;
  7108. /* word3 iocb=iotag wqe=payload_offset_len */
  7109. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7110. wqe->fcp_iwrite.payload_offset_len =
  7111. xmit_len + sizeof(struct fcp_rsp);
  7112. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7113. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7114. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7115. iocbq->iocb.ulpFCP2Rcvy);
  7116. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7117. /* Always open the exchange */
  7118. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7119. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7120. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7121. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7122. LPFC_WQE_LENLOC_WORD4);
  7123. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7124. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7125. break;
  7126. case CMD_FCP_IREAD64_CR:
  7127. /* word3 iocb=iotag wqe=payload_offset_len */
  7128. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7129. wqe->fcp_iread.payload_offset_len =
  7130. xmit_len + sizeof(struct fcp_rsp);
  7131. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7132. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7133. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7134. iocbq->iocb.ulpFCP2Rcvy);
  7135. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7136. /* Always open the exchange */
  7137. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7138. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7139. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7140. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7141. LPFC_WQE_LENLOC_WORD4);
  7142. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7143. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7144. break;
  7145. case CMD_FCP_ICMND64_CR:
  7146. /* word3 iocb=IO_TAG wqe=reserved */
  7147. wqe->fcp_icmd.rsrvd3 = 0;
  7148. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7149. /* Always open the exchange */
  7150. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7151. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7152. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7153. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7154. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7155. LPFC_WQE_LENLOC_NONE);
  7156. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7157. break;
  7158. case CMD_GEN_REQUEST64_CR:
  7159. /* For this command calculate the xmit length of the
  7160. * request bde.
  7161. */
  7162. xmit_len = 0;
  7163. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7164. sizeof(struct ulp_bde64);
  7165. for (i = 0; i < numBdes; i++) {
  7166. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7167. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7168. break;
  7169. xmit_len += bde.tus.f.bdeSize;
  7170. }
  7171. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7172. wqe->gen_req.request_payload_len = xmit_len;
  7173. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7174. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7175. /* word6 context tag copied in memcpy */
  7176. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7177. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7178. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7179. "2015 Invalid CT %x command 0x%x\n",
  7180. ct, iocbq->iocb.ulpCommand);
  7181. return IOCB_ERROR;
  7182. }
  7183. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7184. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7185. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7186. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7187. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7188. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7189. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7190. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7191. command_type = OTHER_COMMAND;
  7192. break;
  7193. case CMD_XMIT_ELS_RSP64_CX:
  7194. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7195. /* words0-2 BDE memcpy */
  7196. /* word3 iocb=iotag32 wqe=response_payload_len */
  7197. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7198. /* word4 iocb=did wge=rsvd. */
  7199. wqe->xmit_els_rsp.rsvd4 = 0;
  7200. /* word5 iocb=rsvd wge=did */
  7201. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7202. iocbq->iocb.un.elsreq64.remoteID);
  7203. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7204. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7205. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7206. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7207. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7208. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7209. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7210. phba->vpi_ids[iocbq->vport->vpi]);
  7211. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7212. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7213. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7214. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7215. LPFC_WQE_LENLOC_WORD3);
  7216. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7217. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7218. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7219. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7220. iocbq->context2)->virt);
  7221. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7222. bf_set(els_req64_sp, &wqe->els_req, 1);
  7223. bf_set(els_req64_sid, &wqe->els_req,
  7224. iocbq->vport->fc_myDID);
  7225. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7226. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7227. phba->vpi_ids[phba->pport->vpi]);
  7228. }
  7229. command_type = OTHER_COMMAND;
  7230. break;
  7231. case CMD_CLOSE_XRI_CN:
  7232. case CMD_ABORT_XRI_CN:
  7233. case CMD_ABORT_XRI_CX:
  7234. /* words 0-2 memcpy should be 0 rserved */
  7235. /* port will send abts */
  7236. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7237. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7238. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7239. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7240. } else
  7241. fip = 0;
  7242. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7243. /*
  7244. * The link is down, or the command was ELS_FIP
  7245. * so the fw does not need to send abts
  7246. * on the wire.
  7247. */
  7248. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7249. else
  7250. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7251. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7252. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7253. wqe->abort_cmd.rsrvd5 = 0;
  7254. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7255. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7256. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7257. /*
  7258. * The abort handler will send us CMD_ABORT_XRI_CN or
  7259. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7260. */
  7261. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7262. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7263. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7264. LPFC_WQE_LENLOC_NONE);
  7265. cmnd = CMD_ABORT_XRI_CX;
  7266. command_type = OTHER_COMMAND;
  7267. xritag = 0;
  7268. break;
  7269. case CMD_XMIT_BLS_RSP64_CX:
  7270. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7271. * we re-construct this WQE here based on information in
  7272. * iocbq from scratch.
  7273. */
  7274. memset(wqe, 0, sizeof(union lpfc_wqe));
  7275. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7276. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7277. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7278. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7279. LPFC_ABTS_UNSOL_INT) {
  7280. /* ABTS sent by initiator to CT exchange, the
  7281. * RX_ID field will be filled with the newly
  7282. * allocated responder XRI.
  7283. */
  7284. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7285. iocbq->sli4_xritag);
  7286. } else {
  7287. /* ABTS sent by responder to CT exchange, the
  7288. * RX_ID field will be filled with the responder
  7289. * RX_ID from ABTS.
  7290. */
  7291. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7292. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7293. }
  7294. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7295. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7296. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7297. iocbq->iocb.ulpContext);
  7298. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7299. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7300. LPFC_WQE_LENLOC_NONE);
  7301. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7302. command_type = OTHER_COMMAND;
  7303. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7304. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7305. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7306. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7307. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7308. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7309. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7310. }
  7311. break;
  7312. case CMD_XRI_ABORTED_CX:
  7313. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7314. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7315. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7316. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7317. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7318. default:
  7319. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7320. "2014 Invalid command 0x%x\n",
  7321. iocbq->iocb.ulpCommand);
  7322. return IOCB_ERROR;
  7323. break;
  7324. }
  7325. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7326. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7327. wqe->generic.wqe_com.abort_tag = abort_tag;
  7328. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7329. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7330. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7331. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7332. return 0;
  7333. }
  7334. /**
  7335. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7336. * @phba: Pointer to HBA context object.
  7337. * @ring_number: SLI ring number to issue iocb on.
  7338. * @piocb: Pointer to command iocb.
  7339. * @flag: Flag indicating if this command can be put into txq.
  7340. *
  7341. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7342. * an iocb command to an HBA with SLI-4 interface spec.
  7343. *
  7344. * This function is called with hbalock held. The function will return success
  7345. * after it successfully submit the iocb to firmware or after adding to the
  7346. * txq.
  7347. **/
  7348. static int
  7349. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7350. struct lpfc_iocbq *piocb, uint32_t flag)
  7351. {
  7352. struct lpfc_sglq *sglq;
  7353. union lpfc_wqe wqe;
  7354. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7355. if (piocb->sli4_xritag == NO_XRI) {
  7356. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7357. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
  7358. piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  7359. sglq = NULL;
  7360. else {
  7361. if (pring->txq_cnt) {
  7362. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7363. __lpfc_sli_ringtx_put(phba,
  7364. pring, piocb);
  7365. return IOCB_SUCCESS;
  7366. } else {
  7367. return IOCB_BUSY;
  7368. }
  7369. } else {
  7370. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7371. if (!sglq) {
  7372. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7373. __lpfc_sli_ringtx_put(phba,
  7374. pring,
  7375. piocb);
  7376. return IOCB_SUCCESS;
  7377. } else
  7378. return IOCB_BUSY;
  7379. }
  7380. }
  7381. }
  7382. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7383. /* These IO's already have an XRI and a mapped sgl. */
  7384. sglq = NULL;
  7385. } else {
  7386. /*
  7387. * This is a continuation of a commandi,(CX) so this
  7388. * sglq is on the active list
  7389. */
  7390. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  7391. if (!sglq)
  7392. return IOCB_ERROR;
  7393. }
  7394. if (sglq) {
  7395. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7396. piocb->sli4_xritag = sglq->sli4_xritag;
  7397. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7398. return IOCB_ERROR;
  7399. }
  7400. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7401. return IOCB_ERROR;
  7402. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7403. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7404. /*
  7405. * For FCP command IOCB, get a new WQ index to distribute
  7406. * WQE across the WQsr. On the other hand, for abort IOCB,
  7407. * it carries the same WQ index to the original command
  7408. * IOCB.
  7409. */
  7410. if (piocb->iocb_flag & LPFC_IO_FCP)
  7411. piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7412. if (unlikely(!phba->sli4_hba.fcp_wq))
  7413. return IOCB_ERROR;
  7414. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7415. &wqe))
  7416. return IOCB_ERROR;
  7417. } else {
  7418. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7419. return IOCB_ERROR;
  7420. }
  7421. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7422. return 0;
  7423. }
  7424. /**
  7425. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7426. *
  7427. * This routine wraps the actual lockless version for issusing IOCB function
  7428. * pointer from the lpfc_hba struct.
  7429. *
  7430. * Return codes:
  7431. * IOCB_ERROR - Error
  7432. * IOCB_SUCCESS - Success
  7433. * IOCB_BUSY - Busy
  7434. **/
  7435. int
  7436. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7437. struct lpfc_iocbq *piocb, uint32_t flag)
  7438. {
  7439. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7440. }
  7441. /**
  7442. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7443. * @phba: The hba struct for which this call is being executed.
  7444. * @dev_grp: The HBA PCI-Device group number.
  7445. *
  7446. * This routine sets up the SLI interface API function jump table in @phba
  7447. * struct.
  7448. * Returns: 0 - success, -ENODEV - failure.
  7449. **/
  7450. int
  7451. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7452. {
  7453. switch (dev_grp) {
  7454. case LPFC_PCI_DEV_LP:
  7455. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7456. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7457. break;
  7458. case LPFC_PCI_DEV_OC:
  7459. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7460. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7461. break;
  7462. default:
  7463. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7464. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7465. dev_grp);
  7466. return -ENODEV;
  7467. break;
  7468. }
  7469. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7470. return 0;
  7471. }
  7472. /**
  7473. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7474. * @phba: Pointer to HBA context object.
  7475. * @pring: Pointer to driver SLI ring object.
  7476. * @piocb: Pointer to command iocb.
  7477. * @flag: Flag indicating if this command can be put into txq.
  7478. *
  7479. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7480. * function. This function gets the hbalock and calls
  7481. * __lpfc_sli_issue_iocb function and will return the error returned
  7482. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7483. * functions which do not hold hbalock.
  7484. **/
  7485. int
  7486. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7487. struct lpfc_iocbq *piocb, uint32_t flag)
  7488. {
  7489. unsigned long iflags;
  7490. int rc;
  7491. spin_lock_irqsave(&phba->hbalock, iflags);
  7492. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7493. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7494. return rc;
  7495. }
  7496. /**
  7497. * lpfc_extra_ring_setup - Extra ring setup function
  7498. * @phba: Pointer to HBA context object.
  7499. *
  7500. * This function is called while driver attaches with the
  7501. * HBA to setup the extra ring. The extra ring is used
  7502. * only when driver needs to support target mode functionality
  7503. * or IP over FC functionalities.
  7504. *
  7505. * This function is called with no lock held.
  7506. **/
  7507. static int
  7508. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7509. {
  7510. struct lpfc_sli *psli;
  7511. struct lpfc_sli_ring *pring;
  7512. psli = &phba->sli;
  7513. /* Adjust cmd/rsp ring iocb entries more evenly */
  7514. /* Take some away from the FCP ring */
  7515. pring = &psli->ring[psli->fcp_ring];
  7516. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7517. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7518. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7519. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7520. /* and give them to the extra ring */
  7521. pring = &psli->ring[psli->extra_ring];
  7522. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7523. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7524. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7525. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7526. /* Setup default profile for this ring */
  7527. pring->iotag_max = 4096;
  7528. pring->num_mask = 1;
  7529. pring->prt[0].profile = 0; /* Mask 0 */
  7530. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7531. pring->prt[0].type = phba->cfg_multi_ring_type;
  7532. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7533. return 0;
  7534. }
  7535. /* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
  7536. * @vport: pointer to virtual port object.
  7537. * @ndlp: nodelist pointer for the impacted rport.
  7538. *
  7539. * The driver calls this routine in response to a XRI ABORT CQE
  7540. * event from the port. In this event, the driver is required to
  7541. * recover its login to the rport even though its login may be valid
  7542. * from the driver's perspective. The failed ABTS notice from the
  7543. * port indicates the rport is not responding.
  7544. */
  7545. static void
  7546. lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
  7547. struct lpfc_nodelist *ndlp)
  7548. {
  7549. struct Scsi_Host *shost;
  7550. struct lpfc_hba *phba;
  7551. unsigned long flags = 0;
  7552. shost = lpfc_shost_from_vport(vport);
  7553. phba = vport->phba;
  7554. if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
  7555. lpfc_printf_log(phba, KERN_INFO,
  7556. LOG_SLI, "3093 No rport recovery needed. "
  7557. "rport in state 0x%x\n",
  7558. ndlp->nlp_state);
  7559. return;
  7560. }
  7561. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7562. "3094 Start rport recovery on shost id 0x%x "
  7563. "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
  7564. "flags 0x%x\n",
  7565. shost->host_no, ndlp->nlp_DID,
  7566. vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
  7567. ndlp->nlp_flag);
  7568. /*
  7569. * The rport is not responding. Don't attempt ADISC recovery.
  7570. * Remove the FCP-2 flag to force a PLOGI.
  7571. */
  7572. spin_lock_irqsave(shost->host_lock, flags);
  7573. ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  7574. spin_unlock_irqrestore(shost->host_lock, flags);
  7575. lpfc_disc_state_machine(vport, ndlp, NULL,
  7576. NLP_EVT_DEVICE_RECOVERY);
  7577. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  7578. spin_lock_irqsave(shost->host_lock, flags);
  7579. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  7580. spin_unlock_irqrestore(shost->host_lock, flags);
  7581. lpfc_disc_start(vport);
  7582. }
  7583. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7584. * @phba: Pointer to HBA context object.
  7585. * @iocbq: Pointer to iocb object.
  7586. *
  7587. * The async_event handler calls this routine when it receives
  7588. * an ASYNC_STATUS_CN event from the port. The port generates
  7589. * this event when an Abort Sequence request to an rport fails
  7590. * twice in succession. The abort could be originated by the
  7591. * driver or by the port. The ABTS could have been for an ELS
  7592. * or FCP IO. The port only generates this event when an ABTS
  7593. * fails to complete after one retry.
  7594. */
  7595. static void
  7596. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7597. struct lpfc_iocbq *iocbq)
  7598. {
  7599. struct lpfc_nodelist *ndlp = NULL;
  7600. uint16_t rpi = 0, vpi = 0;
  7601. struct lpfc_vport *vport = NULL;
  7602. /* The rpi in the ulpContext is vport-sensitive. */
  7603. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7604. rpi = iocbq->iocb.ulpContext;
  7605. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7606. "3092 Port generated ABTS async event "
  7607. "on vpi %d rpi %d status 0x%x\n",
  7608. vpi, rpi, iocbq->iocb.ulpStatus);
  7609. vport = lpfc_find_vport_by_vpid(phba, vpi);
  7610. if (!vport)
  7611. goto err_exit;
  7612. ndlp = lpfc_findnode_rpi(vport, rpi);
  7613. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7614. goto err_exit;
  7615. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  7616. lpfc_sli_abts_recover_port(vport, ndlp);
  7617. return;
  7618. err_exit:
  7619. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7620. "3095 Event Context not found, no "
  7621. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  7622. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  7623. vpi, rpi);
  7624. }
  7625. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  7626. * @phba: pointer to HBA context object.
  7627. * @ndlp: nodelist pointer for the impacted rport.
  7628. * @axri: pointer to the wcqe containing the failed exchange.
  7629. *
  7630. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  7631. * port. The port generates this event when an abort exchange request to an
  7632. * rport fails twice in succession with no reply. The abort could be originated
  7633. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  7634. */
  7635. void
  7636. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  7637. struct lpfc_nodelist *ndlp,
  7638. struct sli4_wcqe_xri_aborted *axri)
  7639. {
  7640. struct lpfc_vport *vport;
  7641. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7642. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7643. "3115 Node Context not found, driver "
  7644. "ignoring abts err event\n");
  7645. vport = ndlp->vport;
  7646. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7647. "3116 Port generated FCP XRI ABORT event on "
  7648. "vpi %d rpi %d xri x%x status 0x%x\n",
  7649. ndlp->vport->vpi, ndlp->nlp_rpi,
  7650. bf_get(lpfc_wcqe_xa_xri, axri),
  7651. bf_get(lpfc_wcqe_xa_status, axri));
  7652. if (bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT)
  7653. lpfc_sli_abts_recover_port(vport, ndlp);
  7654. }
  7655. /**
  7656. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7657. * @phba: Pointer to HBA context object.
  7658. * @pring: Pointer to driver SLI ring object.
  7659. * @iocbq: Pointer to iocb object.
  7660. *
  7661. * This function is called by the slow ring event handler
  7662. * function when there is an ASYNC event iocb in the ring.
  7663. * This function is called with no lock held.
  7664. * Currently this function handles only temperature related
  7665. * ASYNC events. The function decodes the temperature sensor
  7666. * event message and posts events for the management applications.
  7667. **/
  7668. static void
  7669. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7670. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7671. {
  7672. IOCB_t *icmd;
  7673. uint16_t evt_code;
  7674. struct temp_event temp_event_data;
  7675. struct Scsi_Host *shost;
  7676. uint32_t *iocb_w;
  7677. icmd = &iocbq->iocb;
  7678. evt_code = icmd->un.asyncstat.evt_code;
  7679. switch (evt_code) {
  7680. case ASYNC_TEMP_WARN:
  7681. case ASYNC_TEMP_SAFE:
  7682. temp_event_data.data = (uint32_t) icmd->ulpContext;
  7683. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7684. if (evt_code == ASYNC_TEMP_WARN) {
  7685. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7686. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7687. "0347 Adapter is very hot, please take "
  7688. "corrective action. temperature : %d Celsius\n",
  7689. (uint32_t) icmd->ulpContext);
  7690. } else {
  7691. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7692. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7693. "0340 Adapter temperature is OK now. "
  7694. "temperature : %d Celsius\n",
  7695. (uint32_t) icmd->ulpContext);
  7696. }
  7697. /* Send temperature change event to applications */
  7698. shost = lpfc_shost_from_vport(phba->pport);
  7699. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7700. sizeof(temp_event_data), (char *) &temp_event_data,
  7701. LPFC_NL_VENDOR_ID);
  7702. break;
  7703. case ASYNC_STATUS_CN:
  7704. lpfc_sli_abts_err_handler(phba, iocbq);
  7705. break;
  7706. default:
  7707. iocb_w = (uint32_t *) icmd;
  7708. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7709. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7710. " evt_code 0x%x\n"
  7711. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7712. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7713. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7714. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7715. pring->ringno, icmd->un.asyncstat.evt_code,
  7716. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7717. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  7718. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  7719. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  7720. break;
  7721. }
  7722. }
  7723. /**
  7724. * lpfc_sli_setup - SLI ring setup function
  7725. * @phba: Pointer to HBA context object.
  7726. *
  7727. * lpfc_sli_setup sets up rings of the SLI interface with
  7728. * number of iocbs per ring and iotags. This function is
  7729. * called while driver attach to the HBA and before the
  7730. * interrupts are enabled. So there is no need for locking.
  7731. *
  7732. * This function always returns 0.
  7733. **/
  7734. int
  7735. lpfc_sli_setup(struct lpfc_hba *phba)
  7736. {
  7737. int i, totiocbsize = 0;
  7738. struct lpfc_sli *psli = &phba->sli;
  7739. struct lpfc_sli_ring *pring;
  7740. psli->num_rings = MAX_CONFIGURED_RINGS;
  7741. psli->sli_flag = 0;
  7742. psli->fcp_ring = LPFC_FCP_RING;
  7743. psli->next_ring = LPFC_FCP_NEXT_RING;
  7744. psli->extra_ring = LPFC_EXTRA_RING;
  7745. psli->iocbq_lookup = NULL;
  7746. psli->iocbq_lookup_len = 0;
  7747. psli->last_iotag = 0;
  7748. for (i = 0; i < psli->num_rings; i++) {
  7749. pring = &psli->ring[i];
  7750. switch (i) {
  7751. case LPFC_FCP_RING: /* ring 0 - FCP */
  7752. /* numCiocb and numRiocb are used in config_port */
  7753. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  7754. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  7755. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7756. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7757. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7758. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7759. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7760. SLI3_IOCB_CMD_SIZE :
  7761. SLI2_IOCB_CMD_SIZE;
  7762. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7763. SLI3_IOCB_RSP_SIZE :
  7764. SLI2_IOCB_RSP_SIZE;
  7765. pring->iotag_ctr = 0;
  7766. pring->iotag_max =
  7767. (phba->cfg_hba_queue_depth * 2);
  7768. pring->fast_iotag = pring->iotag_max;
  7769. pring->num_mask = 0;
  7770. break;
  7771. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  7772. /* numCiocb and numRiocb are used in config_port */
  7773. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  7774. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  7775. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7776. SLI3_IOCB_CMD_SIZE :
  7777. SLI2_IOCB_CMD_SIZE;
  7778. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7779. SLI3_IOCB_RSP_SIZE :
  7780. SLI2_IOCB_RSP_SIZE;
  7781. pring->iotag_max = phba->cfg_hba_queue_depth;
  7782. pring->num_mask = 0;
  7783. break;
  7784. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  7785. /* numCiocb and numRiocb are used in config_port */
  7786. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  7787. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  7788. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7789. SLI3_IOCB_CMD_SIZE :
  7790. SLI2_IOCB_CMD_SIZE;
  7791. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7792. SLI3_IOCB_RSP_SIZE :
  7793. SLI2_IOCB_RSP_SIZE;
  7794. pring->fast_iotag = 0;
  7795. pring->iotag_ctr = 0;
  7796. pring->iotag_max = 4096;
  7797. pring->lpfc_sli_rcv_async_status =
  7798. lpfc_sli_async_event_handler;
  7799. pring->num_mask = LPFC_MAX_RING_MASK;
  7800. pring->prt[0].profile = 0; /* Mask 0 */
  7801. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  7802. pring->prt[0].type = FC_TYPE_ELS;
  7803. pring->prt[0].lpfc_sli_rcv_unsol_event =
  7804. lpfc_els_unsol_event;
  7805. pring->prt[1].profile = 0; /* Mask 1 */
  7806. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  7807. pring->prt[1].type = FC_TYPE_ELS;
  7808. pring->prt[1].lpfc_sli_rcv_unsol_event =
  7809. lpfc_els_unsol_event;
  7810. pring->prt[2].profile = 0; /* Mask 2 */
  7811. /* NameServer Inquiry */
  7812. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  7813. /* NameServer */
  7814. pring->prt[2].type = FC_TYPE_CT;
  7815. pring->prt[2].lpfc_sli_rcv_unsol_event =
  7816. lpfc_ct_unsol_event;
  7817. pring->prt[3].profile = 0; /* Mask 3 */
  7818. /* NameServer response */
  7819. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  7820. /* NameServer */
  7821. pring->prt[3].type = FC_TYPE_CT;
  7822. pring->prt[3].lpfc_sli_rcv_unsol_event =
  7823. lpfc_ct_unsol_event;
  7824. /* abort unsolicited sequence */
  7825. pring->prt[4].profile = 0; /* Mask 4 */
  7826. pring->prt[4].rctl = FC_RCTL_BA_ABTS;
  7827. pring->prt[4].type = FC_TYPE_BLS;
  7828. pring->prt[4].lpfc_sli_rcv_unsol_event =
  7829. lpfc_sli4_ct_abort_unsol_event;
  7830. break;
  7831. }
  7832. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  7833. (pring->numRiocb * pring->sizeRiocb);
  7834. }
  7835. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  7836. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  7837. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  7838. "SLI2 SLIM Data: x%x x%lx\n",
  7839. phba->brd_no, totiocbsize,
  7840. (unsigned long) MAX_SLIM_IOCB_SIZE);
  7841. }
  7842. if (phba->cfg_multi_ring_support == 2)
  7843. lpfc_extra_ring_setup(phba);
  7844. return 0;
  7845. }
  7846. /**
  7847. * lpfc_sli_queue_setup - Queue initialization function
  7848. * @phba: Pointer to HBA context object.
  7849. *
  7850. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  7851. * ring. This function also initializes ring indices of each ring.
  7852. * This function is called during the initialization of the SLI
  7853. * interface of an HBA.
  7854. * This function is called with no lock held and always returns
  7855. * 1.
  7856. **/
  7857. int
  7858. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  7859. {
  7860. struct lpfc_sli *psli;
  7861. struct lpfc_sli_ring *pring;
  7862. int i;
  7863. psli = &phba->sli;
  7864. spin_lock_irq(&phba->hbalock);
  7865. INIT_LIST_HEAD(&psli->mboxq);
  7866. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  7867. /* Initialize list headers for txq and txcmplq as double linked lists */
  7868. for (i = 0; i < psli->num_rings; i++) {
  7869. pring = &psli->ring[i];
  7870. pring->ringno = i;
  7871. pring->next_cmdidx = 0;
  7872. pring->local_getidx = 0;
  7873. pring->cmdidx = 0;
  7874. INIT_LIST_HEAD(&pring->txq);
  7875. INIT_LIST_HEAD(&pring->txcmplq);
  7876. INIT_LIST_HEAD(&pring->iocb_continueq);
  7877. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  7878. INIT_LIST_HEAD(&pring->postbufq);
  7879. }
  7880. spin_unlock_irq(&phba->hbalock);
  7881. return 1;
  7882. }
  7883. /**
  7884. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  7885. * @phba: Pointer to HBA context object.
  7886. *
  7887. * This routine flushes the mailbox command subsystem. It will unconditionally
  7888. * flush all the mailbox commands in the three possible stages in the mailbox
  7889. * command sub-system: pending mailbox command queue; the outstanding mailbox
  7890. * command; and completed mailbox command queue. It is caller's responsibility
  7891. * to make sure that the driver is in the proper state to flush the mailbox
  7892. * command sub-system. Namely, the posting of mailbox commands into the
  7893. * pending mailbox command queue from the various clients must be stopped;
  7894. * either the HBA is in a state that it will never works on the outstanding
  7895. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  7896. * mailbox command has been completed.
  7897. **/
  7898. static void
  7899. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  7900. {
  7901. LIST_HEAD(completions);
  7902. struct lpfc_sli *psli = &phba->sli;
  7903. LPFC_MBOXQ_t *pmb;
  7904. unsigned long iflag;
  7905. /* Flush all the mailbox commands in the mbox system */
  7906. spin_lock_irqsave(&phba->hbalock, iflag);
  7907. /* The pending mailbox command queue */
  7908. list_splice_init(&phba->sli.mboxq, &completions);
  7909. /* The outstanding active mailbox command */
  7910. if (psli->mbox_active) {
  7911. list_add_tail(&psli->mbox_active->list, &completions);
  7912. psli->mbox_active = NULL;
  7913. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7914. }
  7915. /* The completed mailbox command queue */
  7916. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  7917. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7918. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  7919. while (!list_empty(&completions)) {
  7920. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  7921. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  7922. if (pmb->mbox_cmpl)
  7923. pmb->mbox_cmpl(phba, pmb);
  7924. }
  7925. }
  7926. /**
  7927. * lpfc_sli_host_down - Vport cleanup function
  7928. * @vport: Pointer to virtual port object.
  7929. *
  7930. * lpfc_sli_host_down is called to clean up the resources
  7931. * associated with a vport before destroying virtual
  7932. * port data structures.
  7933. * This function does following operations:
  7934. * - Free discovery resources associated with this virtual
  7935. * port.
  7936. * - Free iocbs associated with this virtual port in
  7937. * the txq.
  7938. * - Send abort for all iocb commands associated with this
  7939. * vport in txcmplq.
  7940. *
  7941. * This function is called with no lock held and always returns 1.
  7942. **/
  7943. int
  7944. lpfc_sli_host_down(struct lpfc_vport *vport)
  7945. {
  7946. LIST_HEAD(completions);
  7947. struct lpfc_hba *phba = vport->phba;
  7948. struct lpfc_sli *psli = &phba->sli;
  7949. struct lpfc_sli_ring *pring;
  7950. struct lpfc_iocbq *iocb, *next_iocb;
  7951. int i;
  7952. unsigned long flags = 0;
  7953. uint16_t prev_pring_flag;
  7954. lpfc_cleanup_discovery_resources(vport);
  7955. spin_lock_irqsave(&phba->hbalock, flags);
  7956. for (i = 0; i < psli->num_rings; i++) {
  7957. pring = &psli->ring[i];
  7958. prev_pring_flag = pring->flag;
  7959. /* Only slow rings */
  7960. if (pring->ringno == LPFC_ELS_RING) {
  7961. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  7962. /* Set the lpfc data pending flag */
  7963. set_bit(LPFC_DATA_READY, &phba->data_flags);
  7964. }
  7965. /*
  7966. * Error everything on the txq since these iocbs have not been
  7967. * given to the FW yet.
  7968. */
  7969. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  7970. if (iocb->vport != vport)
  7971. continue;
  7972. list_move_tail(&iocb->list, &completions);
  7973. pring->txq_cnt--;
  7974. }
  7975. /* Next issue ABTS for everything on the txcmplq */
  7976. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  7977. list) {
  7978. if (iocb->vport != vport)
  7979. continue;
  7980. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  7981. }
  7982. pring->flag = prev_pring_flag;
  7983. }
  7984. spin_unlock_irqrestore(&phba->hbalock, flags);
  7985. /* Cancel all the IOCBs from the completions list */
  7986. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  7987. IOERR_SLI_DOWN);
  7988. return 1;
  7989. }
  7990. /**
  7991. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  7992. * @phba: Pointer to HBA context object.
  7993. *
  7994. * This function cleans up all iocb, buffers, mailbox commands
  7995. * while shutting down the HBA. This function is called with no
  7996. * lock held and always returns 1.
  7997. * This function does the following to cleanup driver resources:
  7998. * - Free discovery resources for each virtual port
  7999. * - Cleanup any pending fabric iocbs
  8000. * - Iterate through the iocb txq and free each entry
  8001. * in the list.
  8002. * - Free up any buffer posted to the HBA
  8003. * - Free mailbox commands in the mailbox queue.
  8004. **/
  8005. int
  8006. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8007. {
  8008. LIST_HEAD(completions);
  8009. struct lpfc_sli *psli = &phba->sli;
  8010. struct lpfc_sli_ring *pring;
  8011. struct lpfc_dmabuf *buf_ptr;
  8012. unsigned long flags = 0;
  8013. int i;
  8014. /* Shutdown the mailbox command sub-system */
  8015. lpfc_sli_mbox_sys_shutdown(phba);
  8016. lpfc_hba_down_prep(phba);
  8017. lpfc_fabric_abort_hba(phba);
  8018. spin_lock_irqsave(&phba->hbalock, flags);
  8019. for (i = 0; i < psli->num_rings; i++) {
  8020. pring = &psli->ring[i];
  8021. /* Only slow rings */
  8022. if (pring->ringno == LPFC_ELS_RING) {
  8023. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8024. /* Set the lpfc data pending flag */
  8025. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8026. }
  8027. /*
  8028. * Error everything on the txq since these iocbs have not been
  8029. * given to the FW yet.
  8030. */
  8031. list_splice_init(&pring->txq, &completions);
  8032. pring->txq_cnt = 0;
  8033. }
  8034. spin_unlock_irqrestore(&phba->hbalock, flags);
  8035. /* Cancel all the IOCBs from the completions list */
  8036. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8037. IOERR_SLI_DOWN);
  8038. spin_lock_irqsave(&phba->hbalock, flags);
  8039. list_splice_init(&phba->elsbuf, &completions);
  8040. phba->elsbuf_cnt = 0;
  8041. phba->elsbuf_prev_cnt = 0;
  8042. spin_unlock_irqrestore(&phba->hbalock, flags);
  8043. while (!list_empty(&completions)) {
  8044. list_remove_head(&completions, buf_ptr,
  8045. struct lpfc_dmabuf, list);
  8046. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8047. kfree(buf_ptr);
  8048. }
  8049. /* Return any active mbox cmds */
  8050. del_timer_sync(&psli->mbox_tmo);
  8051. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8052. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8053. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8054. return 1;
  8055. }
  8056. /**
  8057. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8058. * @srcp: Source memory pointer.
  8059. * @destp: Destination memory pointer.
  8060. * @cnt: Number of words required to be copied.
  8061. *
  8062. * This function is used for copying data between driver memory
  8063. * and the SLI memory. This function also changes the endianness
  8064. * of each word if native endianness is different from SLI
  8065. * endianness. This function can be called with or without
  8066. * lock.
  8067. **/
  8068. void
  8069. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8070. {
  8071. uint32_t *src = srcp;
  8072. uint32_t *dest = destp;
  8073. uint32_t ldata;
  8074. int i;
  8075. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8076. ldata = *src;
  8077. ldata = le32_to_cpu(ldata);
  8078. *dest = ldata;
  8079. src++;
  8080. dest++;
  8081. }
  8082. }
  8083. /**
  8084. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8085. * @srcp: Source memory pointer.
  8086. * @destp: Destination memory pointer.
  8087. * @cnt: Number of words required to be copied.
  8088. *
  8089. * This function is used for copying data between a data structure
  8090. * with big endian representation to local endianness.
  8091. * This function can be called with or without lock.
  8092. **/
  8093. void
  8094. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8095. {
  8096. uint32_t *src = srcp;
  8097. uint32_t *dest = destp;
  8098. uint32_t ldata;
  8099. int i;
  8100. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8101. ldata = *src;
  8102. ldata = be32_to_cpu(ldata);
  8103. *dest = ldata;
  8104. src++;
  8105. dest++;
  8106. }
  8107. }
  8108. /**
  8109. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8110. * @phba: Pointer to HBA context object.
  8111. * @pring: Pointer to driver SLI ring object.
  8112. * @mp: Pointer to driver buffer object.
  8113. *
  8114. * This function is called with no lock held.
  8115. * It always return zero after adding the buffer to the postbufq
  8116. * buffer list.
  8117. **/
  8118. int
  8119. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8120. struct lpfc_dmabuf *mp)
  8121. {
  8122. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8123. later */
  8124. spin_lock_irq(&phba->hbalock);
  8125. list_add_tail(&mp->list, &pring->postbufq);
  8126. pring->postbufq_cnt++;
  8127. spin_unlock_irq(&phba->hbalock);
  8128. return 0;
  8129. }
  8130. /**
  8131. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8132. * @phba: Pointer to HBA context object.
  8133. *
  8134. * When HBQ is enabled, buffers are searched based on tags. This function
  8135. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8136. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8137. * does not conflict with tags of buffer posted for unsolicited events.
  8138. * The function returns the allocated tag. The function is called with
  8139. * no locks held.
  8140. **/
  8141. uint32_t
  8142. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8143. {
  8144. spin_lock_irq(&phba->hbalock);
  8145. phba->buffer_tag_count++;
  8146. /*
  8147. * Always set the QUE_BUFTAG_BIT to distiguish between
  8148. * a tag assigned by HBQ.
  8149. */
  8150. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8151. spin_unlock_irq(&phba->hbalock);
  8152. return phba->buffer_tag_count;
  8153. }
  8154. /**
  8155. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8156. * @phba: Pointer to HBA context object.
  8157. * @pring: Pointer to driver SLI ring object.
  8158. * @tag: Buffer tag.
  8159. *
  8160. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8161. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8162. * iocb is posted to the response ring with the tag of the buffer.
  8163. * This function searches the pring->postbufq list using the tag
  8164. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8165. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8166. * buffer is returned to the caller else NULL is returned.
  8167. * This function is called with no lock held.
  8168. **/
  8169. struct lpfc_dmabuf *
  8170. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8171. uint32_t tag)
  8172. {
  8173. struct lpfc_dmabuf *mp, *next_mp;
  8174. struct list_head *slp = &pring->postbufq;
  8175. /* Search postbufq, from the beginning, looking for a match on tag */
  8176. spin_lock_irq(&phba->hbalock);
  8177. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8178. if (mp->buffer_tag == tag) {
  8179. list_del_init(&mp->list);
  8180. pring->postbufq_cnt--;
  8181. spin_unlock_irq(&phba->hbalock);
  8182. return mp;
  8183. }
  8184. }
  8185. spin_unlock_irq(&phba->hbalock);
  8186. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8187. "0402 Cannot find virtual addr for buffer tag on "
  8188. "ring %d Data x%lx x%p x%p x%x\n",
  8189. pring->ringno, (unsigned long) tag,
  8190. slp->next, slp->prev, pring->postbufq_cnt);
  8191. return NULL;
  8192. }
  8193. /**
  8194. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8195. * @phba: Pointer to HBA context object.
  8196. * @pring: Pointer to driver SLI ring object.
  8197. * @phys: DMA address of the buffer.
  8198. *
  8199. * This function searches the buffer list using the dma_address
  8200. * of unsolicited event to find the driver's lpfc_dmabuf object
  8201. * corresponding to the dma_address. The function returns the
  8202. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8203. * This function is called by the ct and els unsolicited event
  8204. * handlers to get the buffer associated with the unsolicited
  8205. * event.
  8206. *
  8207. * This function is called with no lock held.
  8208. **/
  8209. struct lpfc_dmabuf *
  8210. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8211. dma_addr_t phys)
  8212. {
  8213. struct lpfc_dmabuf *mp, *next_mp;
  8214. struct list_head *slp = &pring->postbufq;
  8215. /* Search postbufq, from the beginning, looking for a match on phys */
  8216. spin_lock_irq(&phba->hbalock);
  8217. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8218. if (mp->phys == phys) {
  8219. list_del_init(&mp->list);
  8220. pring->postbufq_cnt--;
  8221. spin_unlock_irq(&phba->hbalock);
  8222. return mp;
  8223. }
  8224. }
  8225. spin_unlock_irq(&phba->hbalock);
  8226. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8227. "0410 Cannot find virtual addr for mapped buf on "
  8228. "ring %d Data x%llx x%p x%p x%x\n",
  8229. pring->ringno, (unsigned long long)phys,
  8230. slp->next, slp->prev, pring->postbufq_cnt);
  8231. return NULL;
  8232. }
  8233. /**
  8234. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8235. * @phba: Pointer to HBA context object.
  8236. * @cmdiocb: Pointer to driver command iocb object.
  8237. * @rspiocb: Pointer to driver response iocb object.
  8238. *
  8239. * This function is the completion handler for the abort iocbs for
  8240. * ELS commands. This function is called from the ELS ring event
  8241. * handler with no lock held. This function frees memory resources
  8242. * associated with the abort iocb.
  8243. **/
  8244. static void
  8245. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8246. struct lpfc_iocbq *rspiocb)
  8247. {
  8248. IOCB_t *irsp = &rspiocb->iocb;
  8249. uint16_t abort_iotag, abort_context;
  8250. struct lpfc_iocbq *abort_iocb = NULL;
  8251. if (irsp->ulpStatus) {
  8252. /*
  8253. * Assume that the port already completed and returned, or
  8254. * will return the iocb. Just Log the message.
  8255. */
  8256. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8257. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8258. spin_lock_irq(&phba->hbalock);
  8259. if (phba->sli_rev < LPFC_SLI_REV4) {
  8260. if (abort_iotag != 0 &&
  8261. abort_iotag <= phba->sli.last_iotag)
  8262. abort_iocb =
  8263. phba->sli.iocbq_lookup[abort_iotag];
  8264. } else
  8265. /* For sli4 the abort_tag is the XRI,
  8266. * so the abort routine puts the iotag of the iocb
  8267. * being aborted in the context field of the abort
  8268. * IOCB.
  8269. */
  8270. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8271. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8272. "0327 Cannot abort els iocb %p "
  8273. "with tag %x context %x, abort status %x, "
  8274. "abort code %x\n",
  8275. abort_iocb, abort_iotag, abort_context,
  8276. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8277. spin_unlock_irq(&phba->hbalock);
  8278. }
  8279. lpfc_sli_release_iocbq(phba, cmdiocb);
  8280. return;
  8281. }
  8282. /**
  8283. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8284. * @phba: Pointer to HBA context object.
  8285. * @cmdiocb: Pointer to driver command iocb object.
  8286. * @rspiocb: Pointer to driver response iocb object.
  8287. *
  8288. * The function is called from SLI ring event handler with no
  8289. * lock held. This function is the completion handler for ELS commands
  8290. * which are aborted. The function frees memory resources used for
  8291. * the aborted ELS commands.
  8292. **/
  8293. static void
  8294. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8295. struct lpfc_iocbq *rspiocb)
  8296. {
  8297. IOCB_t *irsp = &rspiocb->iocb;
  8298. /* ELS cmd tag <ulpIoTag> completes */
  8299. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8300. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8301. "x%x x%x x%x\n",
  8302. irsp->ulpIoTag, irsp->ulpStatus,
  8303. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8304. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8305. lpfc_ct_free_iocb(phba, cmdiocb);
  8306. else
  8307. lpfc_els_free_iocb(phba, cmdiocb);
  8308. return;
  8309. }
  8310. /**
  8311. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8312. * @phba: Pointer to HBA context object.
  8313. * @pring: Pointer to driver SLI ring object.
  8314. * @cmdiocb: Pointer to driver command iocb object.
  8315. *
  8316. * This function issues an abort iocb for the provided command iocb down to
  8317. * the port. Other than the case the outstanding command iocb is an abort
  8318. * request, this function issues abort out unconditionally. This function is
  8319. * called with hbalock held. The function returns 0 when it fails due to
  8320. * memory allocation failure or when the command iocb is an abort request.
  8321. **/
  8322. static int
  8323. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8324. struct lpfc_iocbq *cmdiocb)
  8325. {
  8326. struct lpfc_vport *vport = cmdiocb->vport;
  8327. struct lpfc_iocbq *abtsiocbp;
  8328. IOCB_t *icmd = NULL;
  8329. IOCB_t *iabt = NULL;
  8330. int retval;
  8331. /*
  8332. * There are certain command types we don't want to abort. And we
  8333. * don't want to abort commands that are already in the process of
  8334. * being aborted.
  8335. */
  8336. icmd = &cmdiocb->iocb;
  8337. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8338. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8339. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8340. return 0;
  8341. /* issue ABTS for this IOCB based on iotag */
  8342. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8343. if (abtsiocbp == NULL)
  8344. return 0;
  8345. /* This signals the response to set the correct status
  8346. * before calling the completion handler
  8347. */
  8348. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8349. iabt = &abtsiocbp->iocb;
  8350. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8351. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8352. if (phba->sli_rev == LPFC_SLI_REV4) {
  8353. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8354. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8355. }
  8356. else
  8357. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8358. iabt->ulpLe = 1;
  8359. iabt->ulpClass = icmd->ulpClass;
  8360. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8361. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8362. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8363. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8364. if (phba->link_state >= LPFC_LINK_UP)
  8365. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8366. else
  8367. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8368. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8369. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8370. "0339 Abort xri x%x, original iotag x%x, "
  8371. "abort cmd iotag x%x\n",
  8372. iabt->un.acxri.abortIoTag,
  8373. iabt->un.acxri.abortContextTag,
  8374. abtsiocbp->iotag);
  8375. retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
  8376. if (retval)
  8377. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8378. /*
  8379. * Caller to this routine should check for IOCB_ERROR
  8380. * and handle it properly. This routine no longer removes
  8381. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8382. */
  8383. return retval;
  8384. }
  8385. /**
  8386. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8387. * @phba: Pointer to HBA context object.
  8388. * @pring: Pointer to driver SLI ring object.
  8389. * @cmdiocb: Pointer to driver command iocb object.
  8390. *
  8391. * This function issues an abort iocb for the provided command iocb. In case
  8392. * of unloading, the abort iocb will not be issued to commands on the ELS
  8393. * ring. Instead, the callback function shall be changed to those commands
  8394. * so that nothing happens when them finishes. This function is called with
  8395. * hbalock held. The function returns 0 when the command iocb is an abort
  8396. * request.
  8397. **/
  8398. int
  8399. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8400. struct lpfc_iocbq *cmdiocb)
  8401. {
  8402. struct lpfc_vport *vport = cmdiocb->vport;
  8403. int retval = IOCB_ERROR;
  8404. IOCB_t *icmd = NULL;
  8405. /*
  8406. * There are certain command types we don't want to abort. And we
  8407. * don't want to abort commands that are already in the process of
  8408. * being aborted.
  8409. */
  8410. icmd = &cmdiocb->iocb;
  8411. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8412. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8413. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8414. return 0;
  8415. /*
  8416. * If we're unloading, don't abort iocb on the ELS ring, but change
  8417. * the callback so that nothing happens when it finishes.
  8418. */
  8419. if ((vport->load_flag & FC_UNLOADING) &&
  8420. (pring->ringno == LPFC_ELS_RING)) {
  8421. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8422. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8423. else
  8424. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8425. goto abort_iotag_exit;
  8426. }
  8427. /* Now, we try to issue the abort to the cmdiocb out */
  8428. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8429. abort_iotag_exit:
  8430. /*
  8431. * Caller to this routine should check for IOCB_ERROR
  8432. * and handle it properly. This routine no longer removes
  8433. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8434. */
  8435. return retval;
  8436. }
  8437. /**
  8438. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8439. * @phba: Pointer to HBA context object.
  8440. * @pring: Pointer to driver SLI ring object.
  8441. *
  8442. * This function aborts all iocbs in the given ring and frees all the iocb
  8443. * objects in txq. This function issues abort iocbs unconditionally for all
  8444. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8445. * to complete before the return of this function. The caller is not required
  8446. * to hold any locks.
  8447. **/
  8448. static void
  8449. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8450. {
  8451. LIST_HEAD(completions);
  8452. struct lpfc_iocbq *iocb, *next_iocb;
  8453. if (pring->ringno == LPFC_ELS_RING)
  8454. lpfc_fabric_abort_hba(phba);
  8455. spin_lock_irq(&phba->hbalock);
  8456. /* Take off all the iocbs on txq for cancelling */
  8457. list_splice_init(&pring->txq, &completions);
  8458. pring->txq_cnt = 0;
  8459. /* Next issue ABTS for everything on the txcmplq */
  8460. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8461. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8462. spin_unlock_irq(&phba->hbalock);
  8463. /* Cancel all the IOCBs from the completions list */
  8464. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8465. IOERR_SLI_ABORTED);
  8466. }
  8467. /**
  8468. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8469. * @phba: pointer to lpfc HBA data structure.
  8470. *
  8471. * This routine will abort all pending and outstanding iocbs to an HBA.
  8472. **/
  8473. void
  8474. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8475. {
  8476. struct lpfc_sli *psli = &phba->sli;
  8477. struct lpfc_sli_ring *pring;
  8478. int i;
  8479. for (i = 0; i < psli->num_rings; i++) {
  8480. pring = &psli->ring[i];
  8481. lpfc_sli_iocb_ring_abort(phba, pring);
  8482. }
  8483. }
  8484. /**
  8485. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8486. * @iocbq: Pointer to driver iocb object.
  8487. * @vport: Pointer to driver virtual port object.
  8488. * @tgt_id: SCSI ID of the target.
  8489. * @lun_id: LUN ID of the scsi device.
  8490. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8491. *
  8492. * This function acts as an iocb filter for functions which abort or count
  8493. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8494. * 0 if the filtering criteria is met for the given iocb and will return
  8495. * 1 if the filtering criteria is not met.
  8496. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8497. * given iocb is for the SCSI device specified by vport, tgt_id and
  8498. * lun_id parameter.
  8499. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8500. * given iocb is for the SCSI target specified by vport and tgt_id
  8501. * parameters.
  8502. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8503. * given iocb is for the SCSI host associated with the given vport.
  8504. * This function is called with no locks held.
  8505. **/
  8506. static int
  8507. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8508. uint16_t tgt_id, uint64_t lun_id,
  8509. lpfc_ctx_cmd ctx_cmd)
  8510. {
  8511. struct lpfc_scsi_buf *lpfc_cmd;
  8512. int rc = 1;
  8513. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8514. return rc;
  8515. if (iocbq->vport != vport)
  8516. return rc;
  8517. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8518. if (lpfc_cmd->pCmd == NULL)
  8519. return rc;
  8520. switch (ctx_cmd) {
  8521. case LPFC_CTX_LUN:
  8522. if ((lpfc_cmd->rdata->pnode) &&
  8523. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8524. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8525. rc = 0;
  8526. break;
  8527. case LPFC_CTX_TGT:
  8528. if ((lpfc_cmd->rdata->pnode) &&
  8529. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8530. rc = 0;
  8531. break;
  8532. case LPFC_CTX_HOST:
  8533. rc = 0;
  8534. break;
  8535. default:
  8536. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8537. __func__, ctx_cmd);
  8538. break;
  8539. }
  8540. return rc;
  8541. }
  8542. /**
  8543. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8544. * @vport: Pointer to virtual port.
  8545. * @tgt_id: SCSI ID of the target.
  8546. * @lun_id: LUN ID of the scsi device.
  8547. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8548. *
  8549. * This function returns number of FCP commands pending for the vport.
  8550. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8551. * commands pending on the vport associated with SCSI device specified
  8552. * by tgt_id and lun_id parameters.
  8553. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8554. * commands pending on the vport associated with SCSI target specified
  8555. * by tgt_id parameter.
  8556. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8557. * commands pending on the vport.
  8558. * This function returns the number of iocbs which satisfy the filter.
  8559. * This function is called without any lock held.
  8560. **/
  8561. int
  8562. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8563. lpfc_ctx_cmd ctx_cmd)
  8564. {
  8565. struct lpfc_hba *phba = vport->phba;
  8566. struct lpfc_iocbq *iocbq;
  8567. int sum, i;
  8568. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8569. iocbq = phba->sli.iocbq_lookup[i];
  8570. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8571. ctx_cmd) == 0)
  8572. sum++;
  8573. }
  8574. return sum;
  8575. }
  8576. /**
  8577. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8578. * @phba: Pointer to HBA context object
  8579. * @cmdiocb: Pointer to command iocb object.
  8580. * @rspiocb: Pointer to response iocb object.
  8581. *
  8582. * This function is called when an aborted FCP iocb completes. This
  8583. * function is called by the ring event handler with no lock held.
  8584. * This function frees the iocb.
  8585. **/
  8586. void
  8587. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8588. struct lpfc_iocbq *rspiocb)
  8589. {
  8590. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8591. "3096 ABORT_XRI_CN completing on xri x%x "
  8592. "original iotag x%x, abort cmd iotag x%x "
  8593. "status 0x%x, reason 0x%x\n",
  8594. cmdiocb->iocb.un.acxri.abortContextTag,
  8595. cmdiocb->iocb.un.acxri.abortIoTag,
  8596. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  8597. rspiocb->iocb.un.ulpWord[4]);
  8598. lpfc_sli_release_iocbq(phba, cmdiocb);
  8599. return;
  8600. }
  8601. /**
  8602. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8603. * @vport: Pointer to virtual port.
  8604. * @pring: Pointer to driver SLI ring object.
  8605. * @tgt_id: SCSI ID of the target.
  8606. * @lun_id: LUN ID of the scsi device.
  8607. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8608. *
  8609. * This function sends an abort command for every SCSI command
  8610. * associated with the given virtual port pending on the ring
  8611. * filtered by lpfc_sli_validate_fcp_iocb function.
  8612. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8613. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8614. * parameters
  8615. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8616. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8617. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8618. * FCP iocbs associated with virtual port.
  8619. * This function returns number of iocbs it failed to abort.
  8620. * This function is called with no locks held.
  8621. **/
  8622. int
  8623. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8624. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8625. {
  8626. struct lpfc_hba *phba = vport->phba;
  8627. struct lpfc_iocbq *iocbq;
  8628. struct lpfc_iocbq *abtsiocb;
  8629. IOCB_t *cmd = NULL;
  8630. int errcnt = 0, ret_val = 0;
  8631. int i;
  8632. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8633. iocbq = phba->sli.iocbq_lookup[i];
  8634. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8635. abort_cmd) != 0)
  8636. continue;
  8637. /* issue ABTS for this IOCB based on iotag */
  8638. abtsiocb = lpfc_sli_get_iocbq(phba);
  8639. if (abtsiocb == NULL) {
  8640. errcnt++;
  8641. continue;
  8642. }
  8643. cmd = &iocbq->iocb;
  8644. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8645. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8646. if (phba->sli_rev == LPFC_SLI_REV4)
  8647. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8648. else
  8649. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8650. abtsiocb->iocb.ulpLe = 1;
  8651. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8652. abtsiocb->vport = phba->pport;
  8653. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8654. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8655. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8656. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8657. if (lpfc_is_link_up(phba))
  8658. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8659. else
  8660. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8661. /* Setup callback routine and issue the command. */
  8662. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8663. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8664. abtsiocb, 0);
  8665. if (ret_val == IOCB_ERROR) {
  8666. lpfc_sli_release_iocbq(phba, abtsiocb);
  8667. errcnt++;
  8668. continue;
  8669. }
  8670. }
  8671. return errcnt;
  8672. }
  8673. /**
  8674. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8675. * @phba: Pointer to HBA context object.
  8676. * @cmdiocbq: Pointer to command iocb.
  8677. * @rspiocbq: Pointer to response iocb.
  8678. *
  8679. * This function is the completion handler for iocbs issued using
  8680. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8681. * ring event handler function without any lock held. This function
  8682. * can be called from both worker thread context and interrupt
  8683. * context. This function also can be called from other thread which
  8684. * cleans up the SLI layer objects.
  8685. * This function copy the contents of the response iocb to the
  8686. * response iocb memory object provided by the caller of
  8687. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8688. * sleeps for the iocb completion.
  8689. **/
  8690. static void
  8691. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8692. struct lpfc_iocbq *cmdiocbq,
  8693. struct lpfc_iocbq *rspiocbq)
  8694. {
  8695. wait_queue_head_t *pdone_q;
  8696. unsigned long iflags;
  8697. struct lpfc_scsi_buf *lpfc_cmd;
  8698. spin_lock_irqsave(&phba->hbalock, iflags);
  8699. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8700. if (cmdiocbq->context2 && rspiocbq)
  8701. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8702. &rspiocbq->iocb, sizeof(IOCB_t));
  8703. /* Set the exchange busy flag for task management commands */
  8704. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8705. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  8706. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  8707. cur_iocbq);
  8708. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  8709. }
  8710. pdone_q = cmdiocbq->context_un.wait_queue;
  8711. if (pdone_q)
  8712. wake_up(pdone_q);
  8713. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8714. return;
  8715. }
  8716. /**
  8717. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  8718. * @phba: Pointer to HBA context object..
  8719. * @piocbq: Pointer to command iocb.
  8720. * @flag: Flag to test.
  8721. *
  8722. * This routine grabs the hbalock and then test the iocb_flag to
  8723. * see if the passed in flag is set.
  8724. * Returns:
  8725. * 1 if flag is set.
  8726. * 0 if flag is not set.
  8727. **/
  8728. static int
  8729. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  8730. struct lpfc_iocbq *piocbq, uint32_t flag)
  8731. {
  8732. unsigned long iflags;
  8733. int ret;
  8734. spin_lock_irqsave(&phba->hbalock, iflags);
  8735. ret = piocbq->iocb_flag & flag;
  8736. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8737. return ret;
  8738. }
  8739. /**
  8740. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  8741. * @phba: Pointer to HBA context object..
  8742. * @pring: Pointer to sli ring.
  8743. * @piocb: Pointer to command iocb.
  8744. * @prspiocbq: Pointer to response iocb.
  8745. * @timeout: Timeout in number of seconds.
  8746. *
  8747. * This function issues the iocb to firmware and waits for the
  8748. * iocb to complete. If the iocb command is not
  8749. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  8750. * Caller should not free the iocb resources if this function
  8751. * returns IOCB_TIMEDOUT.
  8752. * The function waits for the iocb completion using an
  8753. * non-interruptible wait.
  8754. * This function will sleep while waiting for iocb completion.
  8755. * So, this function should not be called from any context which
  8756. * does not allow sleeping. Due to the same reason, this function
  8757. * cannot be called with interrupt disabled.
  8758. * This function assumes that the iocb completions occur while
  8759. * this function sleep. So, this function cannot be called from
  8760. * the thread which process iocb completion for this ring.
  8761. * This function clears the iocb_flag of the iocb object before
  8762. * issuing the iocb and the iocb completion handler sets this
  8763. * flag and wakes this thread when the iocb completes.
  8764. * The contents of the response iocb will be copied to prspiocbq
  8765. * by the completion handler when the command completes.
  8766. * This function returns IOCB_SUCCESS when success.
  8767. * This function is called with no lock held.
  8768. **/
  8769. int
  8770. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  8771. uint32_t ring_number,
  8772. struct lpfc_iocbq *piocb,
  8773. struct lpfc_iocbq *prspiocbq,
  8774. uint32_t timeout)
  8775. {
  8776. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8777. long timeleft, timeout_req = 0;
  8778. int retval = IOCB_SUCCESS;
  8779. uint32_t creg_val;
  8780. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  8781. /*
  8782. * If the caller has provided a response iocbq buffer, then context2
  8783. * is NULL or its an error.
  8784. */
  8785. if (prspiocbq) {
  8786. if (piocb->context2)
  8787. return IOCB_ERROR;
  8788. piocb->context2 = prspiocbq;
  8789. }
  8790. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  8791. piocb->context_un.wait_queue = &done_q;
  8792. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  8793. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8794. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8795. return IOCB_ERROR;
  8796. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  8797. writel(creg_val, phba->HCregaddr);
  8798. readl(phba->HCregaddr); /* flush */
  8799. }
  8800. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  8801. SLI_IOCB_RET_IOCB);
  8802. if (retval == IOCB_SUCCESS) {
  8803. timeout_req = timeout * HZ;
  8804. timeleft = wait_event_timeout(done_q,
  8805. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  8806. timeout_req);
  8807. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  8808. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8809. "0331 IOCB wake signaled\n");
  8810. } else if (timeleft == 0) {
  8811. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8812. "0338 IOCB wait timeout error - no "
  8813. "wake response Data x%x\n", timeout);
  8814. retval = IOCB_TIMEDOUT;
  8815. } else {
  8816. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8817. "0330 IOCB wake NOT set, "
  8818. "Data x%x x%lx\n",
  8819. timeout, (timeleft / jiffies));
  8820. retval = IOCB_TIMEDOUT;
  8821. }
  8822. } else if (retval == IOCB_BUSY) {
  8823. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8824. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  8825. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  8826. return retval;
  8827. } else {
  8828. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8829. "0332 IOCB wait issue failed, Data x%x\n",
  8830. retval);
  8831. retval = IOCB_ERROR;
  8832. }
  8833. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8834. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8835. return IOCB_ERROR;
  8836. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  8837. writel(creg_val, phba->HCregaddr);
  8838. readl(phba->HCregaddr); /* flush */
  8839. }
  8840. if (prspiocbq)
  8841. piocb->context2 = NULL;
  8842. piocb->context_un.wait_queue = NULL;
  8843. piocb->iocb_cmpl = NULL;
  8844. return retval;
  8845. }
  8846. /**
  8847. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  8848. * @phba: Pointer to HBA context object.
  8849. * @pmboxq: Pointer to driver mailbox object.
  8850. * @timeout: Timeout in number of seconds.
  8851. *
  8852. * This function issues the mailbox to firmware and waits for the
  8853. * mailbox command to complete. If the mailbox command is not
  8854. * completed within timeout seconds, it returns MBX_TIMEOUT.
  8855. * The function waits for the mailbox completion using an
  8856. * interruptible wait. If the thread is woken up due to a
  8857. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  8858. * should not free the mailbox resources, if this function returns
  8859. * MBX_TIMEOUT.
  8860. * This function will sleep while waiting for mailbox completion.
  8861. * So, this function should not be called from any context which
  8862. * does not allow sleeping. Due to the same reason, this function
  8863. * cannot be called with interrupt disabled.
  8864. * This function assumes that the mailbox completion occurs while
  8865. * this function sleep. So, this function cannot be called from
  8866. * the worker thread which processes mailbox completion.
  8867. * This function is called in the context of HBA management
  8868. * applications.
  8869. * This function returns MBX_SUCCESS when successful.
  8870. * This function is called with no lock held.
  8871. **/
  8872. int
  8873. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  8874. uint32_t timeout)
  8875. {
  8876. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8877. int retval;
  8878. unsigned long flag;
  8879. /* The caller must leave context1 empty. */
  8880. if (pmboxq->context1)
  8881. return MBX_NOT_FINISHED;
  8882. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  8883. /* setup wake call as IOCB callback */
  8884. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  8885. /* setup context field to pass wait_queue pointer to wake function */
  8886. pmboxq->context1 = &done_q;
  8887. /* now issue the command */
  8888. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  8889. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  8890. wait_event_interruptible_timeout(done_q,
  8891. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  8892. timeout * HZ);
  8893. spin_lock_irqsave(&phba->hbalock, flag);
  8894. pmboxq->context1 = NULL;
  8895. /*
  8896. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  8897. * else do not free the resources.
  8898. */
  8899. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  8900. retval = MBX_SUCCESS;
  8901. lpfc_sli4_swap_str(phba, pmboxq);
  8902. } else {
  8903. retval = MBX_TIMEOUT;
  8904. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  8905. }
  8906. spin_unlock_irqrestore(&phba->hbalock, flag);
  8907. }
  8908. return retval;
  8909. }
  8910. /**
  8911. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  8912. * @phba: Pointer to HBA context.
  8913. *
  8914. * This function is called to shutdown the driver's mailbox sub-system.
  8915. * It first marks the mailbox sub-system is in a block state to prevent
  8916. * the asynchronous mailbox command from issued off the pending mailbox
  8917. * command queue. If the mailbox command sub-system shutdown is due to
  8918. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  8919. * the mailbox sub-system flush routine to forcefully bring down the
  8920. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  8921. * as with offline or HBA function reset), this routine will wait for the
  8922. * outstanding mailbox command to complete before invoking the mailbox
  8923. * sub-system flush routine to gracefully bring down mailbox sub-system.
  8924. **/
  8925. void
  8926. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
  8927. {
  8928. struct lpfc_sli *psli = &phba->sli;
  8929. unsigned long timeout;
  8930. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  8931. spin_lock_irq(&phba->hbalock);
  8932. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  8933. spin_unlock_irq(&phba->hbalock);
  8934. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  8935. spin_lock_irq(&phba->hbalock);
  8936. /* Determine how long we might wait for the active mailbox
  8937. * command to be gracefully completed by firmware.
  8938. */
  8939. if (phba->sli.mbox_active)
  8940. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  8941. phba->sli.mbox_active) *
  8942. 1000) + jiffies;
  8943. spin_unlock_irq(&phba->hbalock);
  8944. while (phba->sli.mbox_active) {
  8945. /* Check active mailbox complete status every 2ms */
  8946. msleep(2);
  8947. if (time_after(jiffies, timeout))
  8948. /* Timeout, let the mailbox flush routine to
  8949. * forcefully release active mailbox command
  8950. */
  8951. break;
  8952. }
  8953. }
  8954. lpfc_sli_mbox_sys_flush(phba);
  8955. }
  8956. /**
  8957. * lpfc_sli_eratt_read - read sli-3 error attention events
  8958. * @phba: Pointer to HBA context.
  8959. *
  8960. * This function is called to read the SLI3 device error attention registers
  8961. * for possible error attention events. The caller must hold the hostlock
  8962. * with spin_lock_irq().
  8963. *
  8964. * This function returns 1 when there is Error Attention in the Host Attention
  8965. * Register and returns 0 otherwise.
  8966. **/
  8967. static int
  8968. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  8969. {
  8970. uint32_t ha_copy;
  8971. /* Read chip Host Attention (HA) register */
  8972. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  8973. goto unplug_err;
  8974. if (ha_copy & HA_ERATT) {
  8975. /* Read host status register to retrieve error event */
  8976. if (lpfc_sli_read_hs(phba))
  8977. goto unplug_err;
  8978. /* Check if there is a deferred error condition is active */
  8979. if ((HS_FFER1 & phba->work_hs) &&
  8980. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  8981. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  8982. phba->hba_flag |= DEFER_ERATT;
  8983. /* Clear all interrupt enable conditions */
  8984. writel(0, phba->HCregaddr);
  8985. readl(phba->HCregaddr);
  8986. }
  8987. /* Set the driver HA work bitmap */
  8988. phba->work_ha |= HA_ERATT;
  8989. /* Indicate polling handles this ERATT */
  8990. phba->hba_flag |= HBA_ERATT_HANDLED;
  8991. return 1;
  8992. }
  8993. return 0;
  8994. unplug_err:
  8995. /* Set the driver HS work bitmap */
  8996. phba->work_hs |= UNPLUG_ERR;
  8997. /* Set the driver HA work bitmap */
  8998. phba->work_ha |= HA_ERATT;
  8999. /* Indicate polling handles this ERATT */
  9000. phba->hba_flag |= HBA_ERATT_HANDLED;
  9001. return 1;
  9002. }
  9003. /**
  9004. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9005. * @phba: Pointer to HBA context.
  9006. *
  9007. * This function is called to read the SLI4 device error attention registers
  9008. * for possible error attention events. The caller must hold the hostlock
  9009. * with spin_lock_irq().
  9010. *
  9011. * This function returns 1 when there is Error Attention in the Host Attention
  9012. * Register and returns 0 otherwise.
  9013. **/
  9014. static int
  9015. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9016. {
  9017. uint32_t uerr_sta_hi, uerr_sta_lo;
  9018. uint32_t if_type, portsmphr;
  9019. struct lpfc_register portstat_reg;
  9020. /*
  9021. * For now, use the SLI4 device internal unrecoverable error
  9022. * registers for error attention. This can be changed later.
  9023. */
  9024. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9025. switch (if_type) {
  9026. case LPFC_SLI_INTF_IF_TYPE_0:
  9027. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9028. &uerr_sta_lo) ||
  9029. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9030. &uerr_sta_hi)) {
  9031. phba->work_hs |= UNPLUG_ERR;
  9032. phba->work_ha |= HA_ERATT;
  9033. phba->hba_flag |= HBA_ERATT_HANDLED;
  9034. return 1;
  9035. }
  9036. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9037. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9038. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9039. "1423 HBA Unrecoverable error: "
  9040. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9041. "ue_mask_lo_reg=0x%x, "
  9042. "ue_mask_hi_reg=0x%x\n",
  9043. uerr_sta_lo, uerr_sta_hi,
  9044. phba->sli4_hba.ue_mask_lo,
  9045. phba->sli4_hba.ue_mask_hi);
  9046. phba->work_status[0] = uerr_sta_lo;
  9047. phba->work_status[1] = uerr_sta_hi;
  9048. phba->work_ha |= HA_ERATT;
  9049. phba->hba_flag |= HBA_ERATT_HANDLED;
  9050. return 1;
  9051. }
  9052. break;
  9053. case LPFC_SLI_INTF_IF_TYPE_2:
  9054. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9055. &portstat_reg.word0) ||
  9056. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9057. &portsmphr)){
  9058. phba->work_hs |= UNPLUG_ERR;
  9059. phba->work_ha |= HA_ERATT;
  9060. phba->hba_flag |= HBA_ERATT_HANDLED;
  9061. return 1;
  9062. }
  9063. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9064. phba->work_status[0] =
  9065. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9066. phba->work_status[1] =
  9067. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9068. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9069. "2885 Port Status Event: "
  9070. "port status reg 0x%x, "
  9071. "port smphr reg 0x%x, "
  9072. "error 1=0x%x, error 2=0x%x\n",
  9073. portstat_reg.word0,
  9074. portsmphr,
  9075. phba->work_status[0],
  9076. phba->work_status[1]);
  9077. phba->work_ha |= HA_ERATT;
  9078. phba->hba_flag |= HBA_ERATT_HANDLED;
  9079. return 1;
  9080. }
  9081. break;
  9082. case LPFC_SLI_INTF_IF_TYPE_1:
  9083. default:
  9084. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9085. "2886 HBA Error Attention on unsupported "
  9086. "if type %d.", if_type);
  9087. return 1;
  9088. }
  9089. return 0;
  9090. }
  9091. /**
  9092. * lpfc_sli_check_eratt - check error attention events
  9093. * @phba: Pointer to HBA context.
  9094. *
  9095. * This function is called from timer soft interrupt context to check HBA's
  9096. * error attention register bit for error attention events.
  9097. *
  9098. * This function returns 1 when there is Error Attention in the Host Attention
  9099. * Register and returns 0 otherwise.
  9100. **/
  9101. int
  9102. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9103. {
  9104. uint32_t ha_copy;
  9105. /* If somebody is waiting to handle an eratt, don't process it
  9106. * here. The brdkill function will do this.
  9107. */
  9108. if (phba->link_flag & LS_IGNORE_ERATT)
  9109. return 0;
  9110. /* Check if interrupt handler handles this ERATT */
  9111. spin_lock_irq(&phba->hbalock);
  9112. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9113. /* Interrupt handler has handled ERATT */
  9114. spin_unlock_irq(&phba->hbalock);
  9115. return 0;
  9116. }
  9117. /*
  9118. * If there is deferred error attention, do not check for error
  9119. * attention
  9120. */
  9121. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9122. spin_unlock_irq(&phba->hbalock);
  9123. return 0;
  9124. }
  9125. /* If PCI channel is offline, don't process it */
  9126. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9127. spin_unlock_irq(&phba->hbalock);
  9128. return 0;
  9129. }
  9130. switch (phba->sli_rev) {
  9131. case LPFC_SLI_REV2:
  9132. case LPFC_SLI_REV3:
  9133. /* Read chip Host Attention (HA) register */
  9134. ha_copy = lpfc_sli_eratt_read(phba);
  9135. break;
  9136. case LPFC_SLI_REV4:
  9137. /* Read device Uncoverable Error (UERR) registers */
  9138. ha_copy = lpfc_sli4_eratt_read(phba);
  9139. break;
  9140. default:
  9141. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9142. "0299 Invalid SLI revision (%d)\n",
  9143. phba->sli_rev);
  9144. ha_copy = 0;
  9145. break;
  9146. }
  9147. spin_unlock_irq(&phba->hbalock);
  9148. return ha_copy;
  9149. }
  9150. /**
  9151. * lpfc_intr_state_check - Check device state for interrupt handling
  9152. * @phba: Pointer to HBA context.
  9153. *
  9154. * This inline routine checks whether a device or its PCI slot is in a state
  9155. * that the interrupt should be handled.
  9156. *
  9157. * This function returns 0 if the device or the PCI slot is in a state that
  9158. * interrupt should be handled, otherwise -EIO.
  9159. */
  9160. static inline int
  9161. lpfc_intr_state_check(struct lpfc_hba *phba)
  9162. {
  9163. /* If the pci channel is offline, ignore all the interrupts */
  9164. if (unlikely(pci_channel_offline(phba->pcidev)))
  9165. return -EIO;
  9166. /* Update device level interrupt statistics */
  9167. phba->sli.slistat.sli_intr++;
  9168. /* Ignore all interrupts during initialization. */
  9169. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9170. return -EIO;
  9171. return 0;
  9172. }
  9173. /**
  9174. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9175. * @irq: Interrupt number.
  9176. * @dev_id: The device context pointer.
  9177. *
  9178. * This function is directly called from the PCI layer as an interrupt
  9179. * service routine when device with SLI-3 interface spec is enabled with
  9180. * MSI-X multi-message interrupt mode and there are slow-path events in
  9181. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9182. * interrupt mode, this function is called as part of the device-level
  9183. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9184. * is undergoing initialization, the interrupt handler will not process
  9185. * the interrupt. The link attention and ELS ring attention events are
  9186. * handled by the worker thread. The interrupt handler signals the worker
  9187. * thread and returns for these events. This function is called without
  9188. * any lock held. It gets the hbalock to access and update SLI data
  9189. * structures.
  9190. *
  9191. * This function returns IRQ_HANDLED when interrupt is handled else it
  9192. * returns IRQ_NONE.
  9193. **/
  9194. irqreturn_t
  9195. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9196. {
  9197. struct lpfc_hba *phba;
  9198. uint32_t ha_copy, hc_copy;
  9199. uint32_t work_ha_copy;
  9200. unsigned long status;
  9201. unsigned long iflag;
  9202. uint32_t control;
  9203. MAILBOX_t *mbox, *pmbox;
  9204. struct lpfc_vport *vport;
  9205. struct lpfc_nodelist *ndlp;
  9206. struct lpfc_dmabuf *mp;
  9207. LPFC_MBOXQ_t *pmb;
  9208. int rc;
  9209. /*
  9210. * Get the driver's phba structure from the dev_id and
  9211. * assume the HBA is not interrupting.
  9212. */
  9213. phba = (struct lpfc_hba *)dev_id;
  9214. if (unlikely(!phba))
  9215. return IRQ_NONE;
  9216. /*
  9217. * Stuff needs to be attented to when this function is invoked as an
  9218. * individual interrupt handler in MSI-X multi-message interrupt mode
  9219. */
  9220. if (phba->intr_type == MSIX) {
  9221. /* Check device state for handling interrupt */
  9222. if (lpfc_intr_state_check(phba))
  9223. return IRQ_NONE;
  9224. /* Need to read HA REG for slow-path events */
  9225. spin_lock_irqsave(&phba->hbalock, iflag);
  9226. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9227. goto unplug_error;
  9228. /* If somebody is waiting to handle an eratt don't process it
  9229. * here. The brdkill function will do this.
  9230. */
  9231. if (phba->link_flag & LS_IGNORE_ERATT)
  9232. ha_copy &= ~HA_ERATT;
  9233. /* Check the need for handling ERATT in interrupt handler */
  9234. if (ha_copy & HA_ERATT) {
  9235. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9236. /* ERATT polling has handled ERATT */
  9237. ha_copy &= ~HA_ERATT;
  9238. else
  9239. /* Indicate interrupt handler handles ERATT */
  9240. phba->hba_flag |= HBA_ERATT_HANDLED;
  9241. }
  9242. /*
  9243. * If there is deferred error attention, do not check for any
  9244. * interrupt.
  9245. */
  9246. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9247. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9248. return IRQ_NONE;
  9249. }
  9250. /* Clear up only attention source related to slow-path */
  9251. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9252. goto unplug_error;
  9253. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9254. HC_LAINT_ENA | HC_ERINT_ENA),
  9255. phba->HCregaddr);
  9256. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9257. phba->HAregaddr);
  9258. writel(hc_copy, phba->HCregaddr);
  9259. readl(phba->HAregaddr); /* flush */
  9260. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9261. } else
  9262. ha_copy = phba->ha_copy;
  9263. work_ha_copy = ha_copy & phba->work_ha_mask;
  9264. if (work_ha_copy) {
  9265. if (work_ha_copy & HA_LATT) {
  9266. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9267. /*
  9268. * Turn off Link Attention interrupts
  9269. * until CLEAR_LA done
  9270. */
  9271. spin_lock_irqsave(&phba->hbalock, iflag);
  9272. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9273. if (lpfc_readl(phba->HCregaddr, &control))
  9274. goto unplug_error;
  9275. control &= ~HC_LAINT_ENA;
  9276. writel(control, phba->HCregaddr);
  9277. readl(phba->HCregaddr); /* flush */
  9278. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9279. }
  9280. else
  9281. work_ha_copy &= ~HA_LATT;
  9282. }
  9283. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9284. /*
  9285. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9286. * the only slow ring.
  9287. */
  9288. status = (work_ha_copy &
  9289. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9290. status >>= (4*LPFC_ELS_RING);
  9291. if (status & HA_RXMASK) {
  9292. spin_lock_irqsave(&phba->hbalock, iflag);
  9293. if (lpfc_readl(phba->HCregaddr, &control))
  9294. goto unplug_error;
  9295. lpfc_debugfs_slow_ring_trc(phba,
  9296. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9297. control, status,
  9298. (uint32_t)phba->sli.slistat.sli_intr);
  9299. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9300. lpfc_debugfs_slow_ring_trc(phba,
  9301. "ISR Disable ring:"
  9302. "pwork:x%x hawork:x%x wait:x%x",
  9303. phba->work_ha, work_ha_copy,
  9304. (uint32_t)((unsigned long)
  9305. &phba->work_waitq));
  9306. control &=
  9307. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9308. writel(control, phba->HCregaddr);
  9309. readl(phba->HCregaddr); /* flush */
  9310. }
  9311. else {
  9312. lpfc_debugfs_slow_ring_trc(phba,
  9313. "ISR slow ring: pwork:"
  9314. "x%x hawork:x%x wait:x%x",
  9315. phba->work_ha, work_ha_copy,
  9316. (uint32_t)((unsigned long)
  9317. &phba->work_waitq));
  9318. }
  9319. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9320. }
  9321. }
  9322. spin_lock_irqsave(&phba->hbalock, iflag);
  9323. if (work_ha_copy & HA_ERATT) {
  9324. if (lpfc_sli_read_hs(phba))
  9325. goto unplug_error;
  9326. /*
  9327. * Check if there is a deferred error condition
  9328. * is active
  9329. */
  9330. if ((HS_FFER1 & phba->work_hs) &&
  9331. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9332. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9333. phba->work_hs)) {
  9334. phba->hba_flag |= DEFER_ERATT;
  9335. /* Clear all interrupt enable conditions */
  9336. writel(0, phba->HCregaddr);
  9337. readl(phba->HCregaddr);
  9338. }
  9339. }
  9340. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9341. pmb = phba->sli.mbox_active;
  9342. pmbox = &pmb->u.mb;
  9343. mbox = phba->mbox;
  9344. vport = pmb->vport;
  9345. /* First check out the status word */
  9346. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9347. if (pmbox->mbxOwner != OWN_HOST) {
  9348. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9349. /*
  9350. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9351. * mbxStatus <status>
  9352. */
  9353. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9354. LOG_SLI,
  9355. "(%d):0304 Stray Mailbox "
  9356. "Interrupt mbxCommand x%x "
  9357. "mbxStatus x%x\n",
  9358. (vport ? vport->vpi : 0),
  9359. pmbox->mbxCommand,
  9360. pmbox->mbxStatus);
  9361. /* clear mailbox attention bit */
  9362. work_ha_copy &= ~HA_MBATT;
  9363. } else {
  9364. phba->sli.mbox_active = NULL;
  9365. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9366. phba->last_completion_time = jiffies;
  9367. del_timer(&phba->sli.mbox_tmo);
  9368. if (pmb->mbox_cmpl) {
  9369. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9370. MAILBOX_CMD_SIZE);
  9371. if (pmb->out_ext_byte_len &&
  9372. pmb->context2)
  9373. lpfc_sli_pcimem_bcopy(
  9374. phba->mbox_ext,
  9375. pmb->context2,
  9376. pmb->out_ext_byte_len);
  9377. }
  9378. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9379. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9380. lpfc_debugfs_disc_trc(vport,
  9381. LPFC_DISC_TRC_MBOX_VPORT,
  9382. "MBOX dflt rpi: : "
  9383. "status:x%x rpi:x%x",
  9384. (uint32_t)pmbox->mbxStatus,
  9385. pmbox->un.varWords[0], 0);
  9386. if (!pmbox->mbxStatus) {
  9387. mp = (struct lpfc_dmabuf *)
  9388. (pmb->context1);
  9389. ndlp = (struct lpfc_nodelist *)
  9390. pmb->context2;
  9391. /* Reg_LOGIN of dflt RPI was
  9392. * successful. new lets get
  9393. * rid of the RPI using the
  9394. * same mbox buffer.
  9395. */
  9396. lpfc_unreg_login(phba,
  9397. vport->vpi,
  9398. pmbox->un.varWords[0],
  9399. pmb);
  9400. pmb->mbox_cmpl =
  9401. lpfc_mbx_cmpl_dflt_rpi;
  9402. pmb->context1 = mp;
  9403. pmb->context2 = ndlp;
  9404. pmb->vport = vport;
  9405. rc = lpfc_sli_issue_mbox(phba,
  9406. pmb,
  9407. MBX_NOWAIT);
  9408. if (rc != MBX_BUSY)
  9409. lpfc_printf_log(phba,
  9410. KERN_ERR,
  9411. LOG_MBOX | LOG_SLI,
  9412. "0350 rc should have"
  9413. "been MBX_BUSY\n");
  9414. if (rc != MBX_NOT_FINISHED)
  9415. goto send_current_mbox;
  9416. }
  9417. }
  9418. spin_lock_irqsave(
  9419. &phba->pport->work_port_lock,
  9420. iflag);
  9421. phba->pport->work_port_events &=
  9422. ~WORKER_MBOX_TMO;
  9423. spin_unlock_irqrestore(
  9424. &phba->pport->work_port_lock,
  9425. iflag);
  9426. lpfc_mbox_cmpl_put(phba, pmb);
  9427. }
  9428. } else
  9429. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9430. if ((work_ha_copy & HA_MBATT) &&
  9431. (phba->sli.mbox_active == NULL)) {
  9432. send_current_mbox:
  9433. /* Process next mailbox command if there is one */
  9434. do {
  9435. rc = lpfc_sli_issue_mbox(phba, NULL,
  9436. MBX_NOWAIT);
  9437. } while (rc == MBX_NOT_FINISHED);
  9438. if (rc != MBX_SUCCESS)
  9439. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9440. LOG_SLI, "0349 rc should be "
  9441. "MBX_SUCCESS\n");
  9442. }
  9443. spin_lock_irqsave(&phba->hbalock, iflag);
  9444. phba->work_ha |= work_ha_copy;
  9445. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9446. lpfc_worker_wake_up(phba);
  9447. }
  9448. return IRQ_HANDLED;
  9449. unplug_error:
  9450. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9451. return IRQ_HANDLED;
  9452. } /* lpfc_sli_sp_intr_handler */
  9453. /**
  9454. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9455. * @irq: Interrupt number.
  9456. * @dev_id: The device context pointer.
  9457. *
  9458. * This function is directly called from the PCI layer as an interrupt
  9459. * service routine when device with SLI-3 interface spec is enabled with
  9460. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9461. * ring event in the HBA. However, when the device is enabled with either
  9462. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9463. * device-level interrupt handler. When the PCI slot is in error recovery
  9464. * or the HBA is undergoing initialization, the interrupt handler will not
  9465. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9466. * the intrrupt context. This function is called without any lock held.
  9467. * It gets the hbalock to access and update SLI data structures.
  9468. *
  9469. * This function returns IRQ_HANDLED when interrupt is handled else it
  9470. * returns IRQ_NONE.
  9471. **/
  9472. irqreturn_t
  9473. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9474. {
  9475. struct lpfc_hba *phba;
  9476. uint32_t ha_copy;
  9477. unsigned long status;
  9478. unsigned long iflag;
  9479. /* Get the driver's phba structure from the dev_id and
  9480. * assume the HBA is not interrupting.
  9481. */
  9482. phba = (struct lpfc_hba *) dev_id;
  9483. if (unlikely(!phba))
  9484. return IRQ_NONE;
  9485. /*
  9486. * Stuff needs to be attented to when this function is invoked as an
  9487. * individual interrupt handler in MSI-X multi-message interrupt mode
  9488. */
  9489. if (phba->intr_type == MSIX) {
  9490. /* Check device state for handling interrupt */
  9491. if (lpfc_intr_state_check(phba))
  9492. return IRQ_NONE;
  9493. /* Need to read HA REG for FCP ring and other ring events */
  9494. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9495. return IRQ_HANDLED;
  9496. /* Clear up only attention source related to fast-path */
  9497. spin_lock_irqsave(&phba->hbalock, iflag);
  9498. /*
  9499. * If there is deferred error attention, do not check for
  9500. * any interrupt.
  9501. */
  9502. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9503. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9504. return IRQ_NONE;
  9505. }
  9506. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9507. phba->HAregaddr);
  9508. readl(phba->HAregaddr); /* flush */
  9509. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9510. } else
  9511. ha_copy = phba->ha_copy;
  9512. /*
  9513. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9514. */
  9515. ha_copy &= ~(phba->work_ha_mask);
  9516. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9517. status >>= (4*LPFC_FCP_RING);
  9518. if (status & HA_RXMASK)
  9519. lpfc_sli_handle_fast_ring_event(phba,
  9520. &phba->sli.ring[LPFC_FCP_RING],
  9521. status);
  9522. if (phba->cfg_multi_ring_support == 2) {
  9523. /*
  9524. * Process all events on extra ring. Take the optimized path
  9525. * for extra ring IO.
  9526. */
  9527. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9528. status >>= (4*LPFC_EXTRA_RING);
  9529. if (status & HA_RXMASK) {
  9530. lpfc_sli_handle_fast_ring_event(phba,
  9531. &phba->sli.ring[LPFC_EXTRA_RING],
  9532. status);
  9533. }
  9534. }
  9535. return IRQ_HANDLED;
  9536. } /* lpfc_sli_fp_intr_handler */
  9537. /**
  9538. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9539. * @irq: Interrupt number.
  9540. * @dev_id: The device context pointer.
  9541. *
  9542. * This function is the HBA device-level interrupt handler to device with
  9543. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9544. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9545. * requires driver attention. This function invokes the slow-path interrupt
  9546. * attention handling function and fast-path interrupt attention handling
  9547. * function in turn to process the relevant HBA attention events. This
  9548. * function is called without any lock held. It gets the hbalock to access
  9549. * and update SLI data structures.
  9550. *
  9551. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9552. * returns IRQ_NONE.
  9553. **/
  9554. irqreturn_t
  9555. lpfc_sli_intr_handler(int irq, void *dev_id)
  9556. {
  9557. struct lpfc_hba *phba;
  9558. irqreturn_t sp_irq_rc, fp_irq_rc;
  9559. unsigned long status1, status2;
  9560. uint32_t hc_copy;
  9561. /*
  9562. * Get the driver's phba structure from the dev_id and
  9563. * assume the HBA is not interrupting.
  9564. */
  9565. phba = (struct lpfc_hba *) dev_id;
  9566. if (unlikely(!phba))
  9567. return IRQ_NONE;
  9568. /* Check device state for handling interrupt */
  9569. if (lpfc_intr_state_check(phba))
  9570. return IRQ_NONE;
  9571. spin_lock(&phba->hbalock);
  9572. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9573. spin_unlock(&phba->hbalock);
  9574. return IRQ_HANDLED;
  9575. }
  9576. if (unlikely(!phba->ha_copy)) {
  9577. spin_unlock(&phba->hbalock);
  9578. return IRQ_NONE;
  9579. } else if (phba->ha_copy & HA_ERATT) {
  9580. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9581. /* ERATT polling has handled ERATT */
  9582. phba->ha_copy &= ~HA_ERATT;
  9583. else
  9584. /* Indicate interrupt handler handles ERATT */
  9585. phba->hba_flag |= HBA_ERATT_HANDLED;
  9586. }
  9587. /*
  9588. * If there is deferred error attention, do not check for any interrupt.
  9589. */
  9590. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9591. spin_unlock(&phba->hbalock);
  9592. return IRQ_NONE;
  9593. }
  9594. /* Clear attention sources except link and error attentions */
  9595. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9596. spin_unlock(&phba->hbalock);
  9597. return IRQ_HANDLED;
  9598. }
  9599. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9600. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9601. phba->HCregaddr);
  9602. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9603. writel(hc_copy, phba->HCregaddr);
  9604. readl(phba->HAregaddr); /* flush */
  9605. spin_unlock(&phba->hbalock);
  9606. /*
  9607. * Invokes slow-path host attention interrupt handling as appropriate.
  9608. */
  9609. /* status of events with mailbox and link attention */
  9610. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9611. /* status of events with ELS ring */
  9612. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9613. status2 >>= (4*LPFC_ELS_RING);
  9614. if (status1 || (status2 & HA_RXMASK))
  9615. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9616. else
  9617. sp_irq_rc = IRQ_NONE;
  9618. /*
  9619. * Invoke fast-path host attention interrupt handling as appropriate.
  9620. */
  9621. /* status of events with FCP ring */
  9622. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9623. status1 >>= (4*LPFC_FCP_RING);
  9624. /* status of events with extra ring */
  9625. if (phba->cfg_multi_ring_support == 2) {
  9626. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9627. status2 >>= (4*LPFC_EXTRA_RING);
  9628. } else
  9629. status2 = 0;
  9630. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9631. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9632. else
  9633. fp_irq_rc = IRQ_NONE;
  9634. /* Return device-level interrupt handling status */
  9635. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9636. } /* lpfc_sli_intr_handler */
  9637. /**
  9638. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9639. * @phba: pointer to lpfc hba data structure.
  9640. *
  9641. * This routine is invoked by the worker thread to process all the pending
  9642. * SLI4 FCP abort XRI events.
  9643. **/
  9644. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9645. {
  9646. struct lpfc_cq_event *cq_event;
  9647. /* First, declare the fcp xri abort event has been handled */
  9648. spin_lock_irq(&phba->hbalock);
  9649. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9650. spin_unlock_irq(&phba->hbalock);
  9651. /* Now, handle all the fcp xri abort events */
  9652. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9653. /* Get the first event from the head of the event queue */
  9654. spin_lock_irq(&phba->hbalock);
  9655. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9656. cq_event, struct lpfc_cq_event, list);
  9657. spin_unlock_irq(&phba->hbalock);
  9658. /* Notify aborted XRI for FCP work queue */
  9659. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9660. /* Free the event processed back to the free pool */
  9661. lpfc_sli4_cq_event_release(phba, cq_event);
  9662. }
  9663. }
  9664. /**
  9665. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9666. * @phba: pointer to lpfc hba data structure.
  9667. *
  9668. * This routine is invoked by the worker thread to process all the pending
  9669. * SLI4 els abort xri events.
  9670. **/
  9671. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9672. {
  9673. struct lpfc_cq_event *cq_event;
  9674. /* First, declare the els xri abort event has been handled */
  9675. spin_lock_irq(&phba->hbalock);
  9676. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9677. spin_unlock_irq(&phba->hbalock);
  9678. /* Now, handle all the els xri abort events */
  9679. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9680. /* Get the first event from the head of the event queue */
  9681. spin_lock_irq(&phba->hbalock);
  9682. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9683. cq_event, struct lpfc_cq_event, list);
  9684. spin_unlock_irq(&phba->hbalock);
  9685. /* Notify aborted XRI for ELS work queue */
  9686. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9687. /* Free the event processed back to the free pool */
  9688. lpfc_sli4_cq_event_release(phba, cq_event);
  9689. }
  9690. }
  9691. /**
  9692. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9693. * @phba: pointer to lpfc hba data structure
  9694. * @pIocbIn: pointer to the rspiocbq
  9695. * @pIocbOut: pointer to the cmdiocbq
  9696. * @wcqe: pointer to the complete wcqe
  9697. *
  9698. * This routine transfers the fields of a command iocbq to a response iocbq
  9699. * by copying all the IOCB fields from command iocbq and transferring the
  9700. * completion status information from the complete wcqe.
  9701. **/
  9702. static void
  9703. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  9704. struct lpfc_iocbq *pIocbIn,
  9705. struct lpfc_iocbq *pIocbOut,
  9706. struct lpfc_wcqe_complete *wcqe)
  9707. {
  9708. unsigned long iflags;
  9709. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  9710. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  9711. sizeof(struct lpfc_iocbq) - offset);
  9712. /* Map WCQE parameters into irspiocb parameters */
  9713. pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
  9714. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  9715. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  9716. pIocbIn->iocb.un.fcpi.fcpi_parm =
  9717. pIocbOut->iocb.un.fcpi.fcpi_parm -
  9718. wcqe->total_data_placed;
  9719. else
  9720. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9721. else {
  9722. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9723. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  9724. }
  9725. /* Pick up HBA exchange busy condition */
  9726. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  9727. spin_lock_irqsave(&phba->hbalock, iflags);
  9728. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  9729. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9730. }
  9731. }
  9732. /**
  9733. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  9734. * @phba: Pointer to HBA context object.
  9735. * @wcqe: Pointer to work-queue completion queue entry.
  9736. *
  9737. * This routine handles an ELS work-queue completion event and construct
  9738. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  9739. * discovery engine to handle.
  9740. *
  9741. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  9742. **/
  9743. static struct lpfc_iocbq *
  9744. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  9745. struct lpfc_iocbq *irspiocbq)
  9746. {
  9747. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9748. struct lpfc_iocbq *cmdiocbq;
  9749. struct lpfc_wcqe_complete *wcqe;
  9750. unsigned long iflags;
  9751. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  9752. spin_lock_irqsave(&phba->hbalock, iflags);
  9753. pring->stats.iocb_event++;
  9754. /* Look up the ELS command IOCB and create pseudo response IOCB */
  9755. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  9756. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9757. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9758. if (unlikely(!cmdiocbq)) {
  9759. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9760. "0386 ELS complete with no corresponding "
  9761. "cmdiocb: iotag (%d)\n",
  9762. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9763. lpfc_sli_release_iocbq(phba, irspiocbq);
  9764. return NULL;
  9765. }
  9766. /* Fake the irspiocbq and copy necessary response information */
  9767. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  9768. return irspiocbq;
  9769. }
  9770. /**
  9771. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  9772. * @phba: Pointer to HBA context object.
  9773. * @cqe: Pointer to mailbox completion queue entry.
  9774. *
  9775. * This routine process a mailbox completion queue entry with asynchrous
  9776. * event.
  9777. *
  9778. * Return: true if work posted to worker thread, otherwise false.
  9779. **/
  9780. static bool
  9781. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9782. {
  9783. struct lpfc_cq_event *cq_event;
  9784. unsigned long iflags;
  9785. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9786. "0392 Async Event: word0:x%x, word1:x%x, "
  9787. "word2:x%x, word3:x%x\n", mcqe->word0,
  9788. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  9789. /* Allocate a new internal CQ_EVENT entry */
  9790. cq_event = lpfc_sli4_cq_event_alloc(phba);
  9791. if (!cq_event) {
  9792. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9793. "0394 Failed to allocate CQ_EVENT entry\n");
  9794. return false;
  9795. }
  9796. /* Move the CQE into an asynchronous event entry */
  9797. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  9798. spin_lock_irqsave(&phba->hbalock, iflags);
  9799. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  9800. /* Set the async event flag */
  9801. phba->hba_flag |= ASYNC_EVENT;
  9802. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9803. return true;
  9804. }
  9805. /**
  9806. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  9807. * @phba: Pointer to HBA context object.
  9808. * @cqe: Pointer to mailbox completion queue entry.
  9809. *
  9810. * This routine process a mailbox completion queue entry with mailbox
  9811. * completion event.
  9812. *
  9813. * Return: true if work posted to worker thread, otherwise false.
  9814. **/
  9815. static bool
  9816. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9817. {
  9818. uint32_t mcqe_status;
  9819. MAILBOX_t *mbox, *pmbox;
  9820. struct lpfc_mqe *mqe;
  9821. struct lpfc_vport *vport;
  9822. struct lpfc_nodelist *ndlp;
  9823. struct lpfc_dmabuf *mp;
  9824. unsigned long iflags;
  9825. LPFC_MBOXQ_t *pmb;
  9826. bool workposted = false;
  9827. int rc;
  9828. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  9829. if (!bf_get(lpfc_trailer_completed, mcqe))
  9830. goto out_no_mqe_complete;
  9831. /* Get the reference to the active mbox command */
  9832. spin_lock_irqsave(&phba->hbalock, iflags);
  9833. pmb = phba->sli.mbox_active;
  9834. if (unlikely(!pmb)) {
  9835. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  9836. "1832 No pending MBOX command to handle\n");
  9837. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9838. goto out_no_mqe_complete;
  9839. }
  9840. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9841. mqe = &pmb->u.mqe;
  9842. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  9843. mbox = phba->mbox;
  9844. vport = pmb->vport;
  9845. /* Reset heartbeat timer */
  9846. phba->last_completion_time = jiffies;
  9847. del_timer(&phba->sli.mbox_tmo);
  9848. /* Move mbox data to caller's mailbox region, do endian swapping */
  9849. if (pmb->mbox_cmpl && mbox)
  9850. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  9851. /*
  9852. * For mcqe errors, conditionally move a modified error code to
  9853. * the mbox so that the error will not be missed.
  9854. */
  9855. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  9856. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  9857. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  9858. bf_set(lpfc_mqe_status, mqe,
  9859. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  9860. }
  9861. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9862. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9863. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  9864. "MBOX dflt rpi: status:x%x rpi:x%x",
  9865. mcqe_status,
  9866. pmbox->un.varWords[0], 0);
  9867. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  9868. mp = (struct lpfc_dmabuf *)(pmb->context1);
  9869. ndlp = (struct lpfc_nodelist *)pmb->context2;
  9870. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  9871. * RID of the PPI using the same mbox buffer.
  9872. */
  9873. lpfc_unreg_login(phba, vport->vpi,
  9874. pmbox->un.varWords[0], pmb);
  9875. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  9876. pmb->context1 = mp;
  9877. pmb->context2 = ndlp;
  9878. pmb->vport = vport;
  9879. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  9880. if (rc != MBX_BUSY)
  9881. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9882. LOG_SLI, "0385 rc should "
  9883. "have been MBX_BUSY\n");
  9884. if (rc != MBX_NOT_FINISHED)
  9885. goto send_current_mbox;
  9886. }
  9887. }
  9888. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  9889. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  9890. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  9891. /* There is mailbox completion work to do */
  9892. spin_lock_irqsave(&phba->hbalock, iflags);
  9893. __lpfc_mbox_cmpl_put(phba, pmb);
  9894. phba->work_ha |= HA_MBATT;
  9895. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9896. workposted = true;
  9897. send_current_mbox:
  9898. spin_lock_irqsave(&phba->hbalock, iflags);
  9899. /* Release the mailbox command posting token */
  9900. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  9901. /* Setting active mailbox pointer need to be in sync to flag clear */
  9902. phba->sli.mbox_active = NULL;
  9903. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9904. /* Wake up worker thread to post the next pending mailbox command */
  9905. lpfc_worker_wake_up(phba);
  9906. out_no_mqe_complete:
  9907. if (bf_get(lpfc_trailer_consumed, mcqe))
  9908. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  9909. return workposted;
  9910. }
  9911. /**
  9912. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  9913. * @phba: Pointer to HBA context object.
  9914. * @cqe: Pointer to mailbox completion queue entry.
  9915. *
  9916. * This routine process a mailbox completion queue entry, it invokes the
  9917. * proper mailbox complete handling or asynchrous event handling routine
  9918. * according to the MCQE's async bit.
  9919. *
  9920. * Return: true if work posted to worker thread, otherwise false.
  9921. **/
  9922. static bool
  9923. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  9924. {
  9925. struct lpfc_mcqe mcqe;
  9926. bool workposted;
  9927. /* Copy the mailbox MCQE and convert endian order as needed */
  9928. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  9929. /* Invoke the proper event handling routine */
  9930. if (!bf_get(lpfc_trailer_async, &mcqe))
  9931. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  9932. else
  9933. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  9934. return workposted;
  9935. }
  9936. /**
  9937. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  9938. * @phba: Pointer to HBA context object.
  9939. * @wcqe: Pointer to work-queue completion queue entry.
  9940. *
  9941. * This routine handles an ELS work-queue completion event.
  9942. *
  9943. * Return: true if work posted to worker thread, otherwise false.
  9944. **/
  9945. static bool
  9946. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
  9947. struct lpfc_wcqe_complete *wcqe)
  9948. {
  9949. struct lpfc_iocbq *irspiocbq;
  9950. unsigned long iflags;
  9951. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  9952. /* Get an irspiocbq for later ELS response processing use */
  9953. irspiocbq = lpfc_sli_get_iocbq(phba);
  9954. if (!irspiocbq) {
  9955. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9956. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  9957. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  9958. pring->txq_cnt, phba->iocb_cnt,
  9959. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  9960. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  9961. return false;
  9962. }
  9963. /* Save off the slow-path queue event for work thread to process */
  9964. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  9965. spin_lock_irqsave(&phba->hbalock, iflags);
  9966. list_add_tail(&irspiocbq->cq_event.list,
  9967. &phba->sli4_hba.sp_queue_event);
  9968. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  9969. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9970. return true;
  9971. }
  9972. /**
  9973. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  9974. * @phba: Pointer to HBA context object.
  9975. * @wcqe: Pointer to work-queue completion queue entry.
  9976. *
  9977. * This routine handles slow-path WQ entry comsumed event by invoking the
  9978. * proper WQ release routine to the slow-path WQ.
  9979. **/
  9980. static void
  9981. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  9982. struct lpfc_wcqe_release *wcqe)
  9983. {
  9984. /* sanity check on queue memory */
  9985. if (unlikely(!phba->sli4_hba.els_wq))
  9986. return;
  9987. /* Check for the slow-path ELS work queue */
  9988. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  9989. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  9990. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  9991. else
  9992. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9993. "2579 Slow-path wqe consume event carries "
  9994. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  9995. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  9996. phba->sli4_hba.els_wq->queue_id);
  9997. }
  9998. /**
  9999. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10000. * @phba: Pointer to HBA context object.
  10001. * @cq: Pointer to a WQ completion queue.
  10002. * @wcqe: Pointer to work-queue completion queue entry.
  10003. *
  10004. * This routine handles an XRI abort event.
  10005. *
  10006. * Return: true if work posted to worker thread, otherwise false.
  10007. **/
  10008. static bool
  10009. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10010. struct lpfc_queue *cq,
  10011. struct sli4_wcqe_xri_aborted *wcqe)
  10012. {
  10013. bool workposted = false;
  10014. struct lpfc_cq_event *cq_event;
  10015. unsigned long iflags;
  10016. /* Allocate a new internal CQ_EVENT entry */
  10017. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10018. if (!cq_event) {
  10019. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10020. "0602 Failed to allocate CQ_EVENT entry\n");
  10021. return false;
  10022. }
  10023. /* Move the CQE into the proper xri abort event list */
  10024. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10025. switch (cq->subtype) {
  10026. case LPFC_FCP:
  10027. spin_lock_irqsave(&phba->hbalock, iflags);
  10028. list_add_tail(&cq_event->list,
  10029. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10030. /* Set the fcp xri abort event flag */
  10031. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10032. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10033. workposted = true;
  10034. break;
  10035. case LPFC_ELS:
  10036. spin_lock_irqsave(&phba->hbalock, iflags);
  10037. list_add_tail(&cq_event->list,
  10038. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10039. /* Set the els xri abort event flag */
  10040. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10041. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10042. workposted = true;
  10043. break;
  10044. default:
  10045. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10046. "0603 Invalid work queue CQE subtype (x%x)\n",
  10047. cq->subtype);
  10048. workposted = false;
  10049. break;
  10050. }
  10051. return workposted;
  10052. }
  10053. /**
  10054. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10055. * @phba: Pointer to HBA context object.
  10056. * @rcqe: Pointer to receive-queue completion queue entry.
  10057. *
  10058. * This routine process a receive-queue completion queue entry.
  10059. *
  10060. * Return: true if work posted to worker thread, otherwise false.
  10061. **/
  10062. static bool
  10063. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10064. {
  10065. bool workposted = false;
  10066. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10067. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10068. struct hbq_dmabuf *dma_buf;
  10069. uint32_t status, rq_id;
  10070. unsigned long iflags;
  10071. /* sanity check on queue memory */
  10072. if (unlikely(!hrq) || unlikely(!drq))
  10073. return workposted;
  10074. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10075. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10076. else
  10077. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10078. if (rq_id != hrq->queue_id)
  10079. goto out;
  10080. status = bf_get(lpfc_rcqe_status, rcqe);
  10081. switch (status) {
  10082. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10083. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10084. "2537 Receive Frame Truncated!!\n");
  10085. case FC_STATUS_RQ_SUCCESS:
  10086. lpfc_sli4_rq_release(hrq, drq);
  10087. spin_lock_irqsave(&phba->hbalock, iflags);
  10088. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10089. if (!dma_buf) {
  10090. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10091. goto out;
  10092. }
  10093. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10094. /* save off the frame for the word thread to process */
  10095. list_add_tail(&dma_buf->cq_event.list,
  10096. &phba->sli4_hba.sp_queue_event);
  10097. /* Frame received */
  10098. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10099. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10100. workposted = true;
  10101. break;
  10102. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10103. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10104. /* Post more buffers if possible */
  10105. spin_lock_irqsave(&phba->hbalock, iflags);
  10106. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10107. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10108. workposted = true;
  10109. break;
  10110. }
  10111. out:
  10112. return workposted;
  10113. }
  10114. /**
  10115. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10116. * @phba: Pointer to HBA context object.
  10117. * @cq: Pointer to the completion queue.
  10118. * @wcqe: Pointer to a completion queue entry.
  10119. *
  10120. * This routine process a slow-path work-queue or receive queue completion queue
  10121. * entry.
  10122. *
  10123. * Return: true if work posted to worker thread, otherwise false.
  10124. **/
  10125. static bool
  10126. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10127. struct lpfc_cqe *cqe)
  10128. {
  10129. struct lpfc_cqe cqevt;
  10130. bool workposted = false;
  10131. /* Copy the work queue CQE and convert endian order if needed */
  10132. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10133. /* Check and process for different type of WCQE and dispatch */
  10134. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10135. case CQE_CODE_COMPL_WQE:
  10136. /* Process the WQ/RQ complete event */
  10137. phba->last_completion_time = jiffies;
  10138. workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
  10139. (struct lpfc_wcqe_complete *)&cqevt);
  10140. break;
  10141. case CQE_CODE_RELEASE_WQE:
  10142. /* Process the WQ release event */
  10143. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10144. (struct lpfc_wcqe_release *)&cqevt);
  10145. break;
  10146. case CQE_CODE_XRI_ABORTED:
  10147. /* Process the WQ XRI abort event */
  10148. phba->last_completion_time = jiffies;
  10149. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10150. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10151. break;
  10152. case CQE_CODE_RECEIVE:
  10153. case CQE_CODE_RECEIVE_V1:
  10154. /* Process the RQ event */
  10155. phba->last_completion_time = jiffies;
  10156. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10157. (struct lpfc_rcqe *)&cqevt);
  10158. break;
  10159. default:
  10160. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10161. "0388 Not a valid WCQE code: x%x\n",
  10162. bf_get(lpfc_cqe_code, &cqevt));
  10163. break;
  10164. }
  10165. return workposted;
  10166. }
  10167. /**
  10168. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10169. * @phba: Pointer to HBA context object.
  10170. * @eqe: Pointer to fast-path event queue entry.
  10171. *
  10172. * This routine process a event queue entry from the slow-path event queue.
  10173. * It will check the MajorCode and MinorCode to determine this is for a
  10174. * completion event on a completion queue, if not, an error shall be logged
  10175. * and just return. Otherwise, it will get to the corresponding completion
  10176. * queue and process all the entries on that completion queue, rearm the
  10177. * completion queue, and then return.
  10178. *
  10179. **/
  10180. static void
  10181. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  10182. {
  10183. struct lpfc_queue *cq = NULL, *childq, *speq;
  10184. struct lpfc_cqe *cqe;
  10185. bool workposted = false;
  10186. int ecount = 0;
  10187. uint16_t cqid;
  10188. if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
  10189. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10190. "0359 Not a valid slow-path completion "
  10191. "event: majorcode=x%x, minorcode=x%x\n",
  10192. bf_get_le32(lpfc_eqe_major_code, eqe),
  10193. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10194. return;
  10195. }
  10196. /* Get the reference to the corresponding CQ */
  10197. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10198. /* Search for completion queue pointer matching this cqid */
  10199. speq = phba->sli4_hba.sp_eq;
  10200. /* sanity check on queue memory */
  10201. if (unlikely(!speq))
  10202. return;
  10203. list_for_each_entry(childq, &speq->child_list, list) {
  10204. if (childq->queue_id == cqid) {
  10205. cq = childq;
  10206. break;
  10207. }
  10208. }
  10209. if (unlikely(!cq)) {
  10210. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10211. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10212. "0365 Slow-path CQ identifier "
  10213. "(%d) does not exist\n", cqid);
  10214. return;
  10215. }
  10216. /* Process all the entries to the CQ */
  10217. switch (cq->type) {
  10218. case LPFC_MCQ:
  10219. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10220. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10221. if (!(++ecount % cq->entry_repost))
  10222. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10223. }
  10224. break;
  10225. case LPFC_WCQ:
  10226. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10227. if (cq->subtype == LPFC_FCP)
  10228. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10229. cqe);
  10230. else
  10231. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10232. cqe);
  10233. if (!(++ecount % cq->entry_repost))
  10234. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10235. }
  10236. break;
  10237. default:
  10238. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10239. "0370 Invalid completion queue type (%d)\n",
  10240. cq->type);
  10241. return;
  10242. }
  10243. /* Catch the no cq entry condition, log an error */
  10244. if (unlikely(ecount == 0))
  10245. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10246. "0371 No entry from the CQ: identifier "
  10247. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10248. /* In any case, flash and re-arm the RCQ */
  10249. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10250. /* wake up worker thread if there are works to be done */
  10251. if (workposted)
  10252. lpfc_worker_wake_up(phba);
  10253. }
  10254. /**
  10255. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10256. * @eqe: Pointer to fast-path completion queue entry.
  10257. *
  10258. * This routine process a fast-path work queue completion entry from fast-path
  10259. * event queue for FCP command response completion.
  10260. **/
  10261. static void
  10262. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
  10263. struct lpfc_wcqe_complete *wcqe)
  10264. {
  10265. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10266. struct lpfc_iocbq *cmdiocbq;
  10267. struct lpfc_iocbq irspiocbq;
  10268. unsigned long iflags;
  10269. spin_lock_irqsave(&phba->hbalock, iflags);
  10270. pring->stats.iocb_event++;
  10271. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10272. /* Check for response status */
  10273. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10274. /* If resource errors reported from HBA, reduce queue
  10275. * depth of the SCSI device.
  10276. */
  10277. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10278. IOSTAT_LOCAL_REJECT) &&
  10279. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  10280. phba->lpfc_rampdown_queue_depth(phba);
  10281. }
  10282. /* Log the error status */
  10283. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10284. "0373 FCP complete error: status=x%x, "
  10285. "hw_status=x%x, total_data_specified=%d, "
  10286. "parameter=x%x, word3=x%x\n",
  10287. bf_get(lpfc_wcqe_c_status, wcqe),
  10288. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10289. wcqe->total_data_placed, wcqe->parameter,
  10290. wcqe->word3);
  10291. }
  10292. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10293. spin_lock_irqsave(&phba->hbalock, iflags);
  10294. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10295. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10296. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10297. if (unlikely(!cmdiocbq)) {
  10298. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10299. "0374 FCP complete with no corresponding "
  10300. "cmdiocb: iotag (%d)\n",
  10301. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10302. return;
  10303. }
  10304. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10305. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10306. "0375 FCP cmdiocb not callback function "
  10307. "iotag: (%d)\n",
  10308. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10309. return;
  10310. }
  10311. /* Fake the irspiocb and copy necessary response information */
  10312. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10313. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10314. spin_lock_irqsave(&phba->hbalock, iflags);
  10315. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10316. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10317. }
  10318. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10319. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10320. }
  10321. /**
  10322. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10323. * @phba: Pointer to HBA context object.
  10324. * @cq: Pointer to completion queue.
  10325. * @wcqe: Pointer to work-queue completion queue entry.
  10326. *
  10327. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10328. * proper WQ release routine to the slow-path WQ.
  10329. **/
  10330. static void
  10331. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10332. struct lpfc_wcqe_release *wcqe)
  10333. {
  10334. struct lpfc_queue *childwq;
  10335. bool wqid_matched = false;
  10336. uint16_t fcp_wqid;
  10337. /* Check for fast-path FCP work queue release */
  10338. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10339. list_for_each_entry(childwq, &cq->child_list, list) {
  10340. if (childwq->queue_id == fcp_wqid) {
  10341. lpfc_sli4_wq_release(childwq,
  10342. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10343. wqid_matched = true;
  10344. break;
  10345. }
  10346. }
  10347. /* Report warning log message if no match found */
  10348. if (wqid_matched != true)
  10349. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10350. "2580 Fast-path wqe consume event carries "
  10351. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10352. }
  10353. /**
  10354. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10355. * @cq: Pointer to the completion queue.
  10356. * @eqe: Pointer to fast-path completion queue entry.
  10357. *
  10358. * This routine process a fast-path work queue completion entry from fast-path
  10359. * event queue for FCP command response completion.
  10360. **/
  10361. static int
  10362. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10363. struct lpfc_cqe *cqe)
  10364. {
  10365. struct lpfc_wcqe_release wcqe;
  10366. bool workposted = false;
  10367. /* Copy the work queue CQE and convert endian order if needed */
  10368. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10369. /* Check and process for different type of WCQE and dispatch */
  10370. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10371. case CQE_CODE_COMPL_WQE:
  10372. /* Process the WQ complete event */
  10373. phba->last_completion_time = jiffies;
  10374. lpfc_sli4_fp_handle_fcp_wcqe(phba,
  10375. (struct lpfc_wcqe_complete *)&wcqe);
  10376. break;
  10377. case CQE_CODE_RELEASE_WQE:
  10378. /* Process the WQ release event */
  10379. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10380. (struct lpfc_wcqe_release *)&wcqe);
  10381. break;
  10382. case CQE_CODE_XRI_ABORTED:
  10383. /* Process the WQ XRI abort event */
  10384. phba->last_completion_time = jiffies;
  10385. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10386. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10387. break;
  10388. default:
  10389. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10390. "0144 Not a valid WCQE code: x%x\n",
  10391. bf_get(lpfc_wcqe_c_code, &wcqe));
  10392. break;
  10393. }
  10394. return workposted;
  10395. }
  10396. /**
  10397. * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
  10398. * @phba: Pointer to HBA context object.
  10399. * @eqe: Pointer to fast-path event queue entry.
  10400. *
  10401. * This routine process a event queue entry from the fast-path event queue.
  10402. * It will check the MajorCode and MinorCode to determine this is for a
  10403. * completion event on a completion queue, if not, an error shall be logged
  10404. * and just return. Otherwise, it will get to the corresponding completion
  10405. * queue and process all the entries on the completion queue, rearm the
  10406. * completion queue, and then return.
  10407. **/
  10408. static void
  10409. lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10410. uint32_t fcp_cqidx)
  10411. {
  10412. struct lpfc_queue *cq;
  10413. struct lpfc_cqe *cqe;
  10414. bool workposted = false;
  10415. uint16_t cqid;
  10416. int ecount = 0;
  10417. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10418. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10419. "0366 Not a valid fast-path completion "
  10420. "event: majorcode=x%x, minorcode=x%x\n",
  10421. bf_get_le32(lpfc_eqe_major_code, eqe),
  10422. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10423. return;
  10424. }
  10425. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10426. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10427. "3146 Fast-path completion queues "
  10428. "does not exist\n");
  10429. return;
  10430. }
  10431. cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
  10432. if (unlikely(!cq)) {
  10433. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10434. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10435. "0367 Fast-path completion queue "
  10436. "(%d) does not exist\n", fcp_cqidx);
  10437. return;
  10438. }
  10439. /* Get the reference to the corresponding CQ */
  10440. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10441. if (unlikely(cqid != cq->queue_id)) {
  10442. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10443. "0368 Miss-matched fast-path completion "
  10444. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10445. cqid, cq->queue_id);
  10446. return;
  10447. }
  10448. /* Process all the entries to the CQ */
  10449. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10450. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10451. if (!(++ecount % cq->entry_repost))
  10452. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10453. }
  10454. /* Catch the no cq entry condition */
  10455. if (unlikely(ecount == 0))
  10456. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10457. "0369 No entry from fast-path completion "
  10458. "queue fcpcqid=%d\n", cq->queue_id);
  10459. /* In any case, flash and re-arm the CQ */
  10460. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10461. /* wake up worker thread if there are works to be done */
  10462. if (workposted)
  10463. lpfc_worker_wake_up(phba);
  10464. }
  10465. static void
  10466. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10467. {
  10468. struct lpfc_eqe *eqe;
  10469. /* walk all the EQ entries and drop on the floor */
  10470. while ((eqe = lpfc_sli4_eq_get(eq)))
  10471. ;
  10472. /* Clear and re-arm the EQ */
  10473. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10474. }
  10475. /**
  10476. * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
  10477. * @irq: Interrupt number.
  10478. * @dev_id: The device context pointer.
  10479. *
  10480. * This function is directly called from the PCI layer as an interrupt
  10481. * service routine when device with SLI-4 interface spec is enabled with
  10482. * MSI-X multi-message interrupt mode and there are slow-path events in
  10483. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  10484. * interrupt mode, this function is called as part of the device-level
  10485. * interrupt handler. When the PCI slot is in error recovery or the HBA is
  10486. * undergoing initialization, the interrupt handler will not process the
  10487. * interrupt. The link attention and ELS ring attention events are handled
  10488. * by the worker thread. The interrupt handler signals the worker thread
  10489. * and returns for these events. This function is called without any lock
  10490. * held. It gets the hbalock to access and update SLI data structures.
  10491. *
  10492. * This function returns IRQ_HANDLED when interrupt is handled else it
  10493. * returns IRQ_NONE.
  10494. **/
  10495. irqreturn_t
  10496. lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
  10497. {
  10498. struct lpfc_hba *phba;
  10499. struct lpfc_queue *speq;
  10500. struct lpfc_eqe *eqe;
  10501. unsigned long iflag;
  10502. int ecount = 0;
  10503. /*
  10504. * Get the driver's phba structure from the dev_id
  10505. */
  10506. phba = (struct lpfc_hba *)dev_id;
  10507. if (unlikely(!phba))
  10508. return IRQ_NONE;
  10509. /* Get to the EQ struct associated with this vector */
  10510. speq = phba->sli4_hba.sp_eq;
  10511. if (unlikely(!speq))
  10512. return IRQ_NONE;
  10513. /* Check device state for handling interrupt */
  10514. if (unlikely(lpfc_intr_state_check(phba))) {
  10515. /* Check again for link_state with lock held */
  10516. spin_lock_irqsave(&phba->hbalock, iflag);
  10517. if (phba->link_state < LPFC_LINK_DOWN)
  10518. /* Flush, clear interrupt, and rearm the EQ */
  10519. lpfc_sli4_eq_flush(phba, speq);
  10520. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10521. return IRQ_NONE;
  10522. }
  10523. /*
  10524. * Process all the event on FCP slow-path EQ
  10525. */
  10526. while ((eqe = lpfc_sli4_eq_get(speq))) {
  10527. lpfc_sli4_sp_handle_eqe(phba, eqe);
  10528. if (!(++ecount % speq->entry_repost))
  10529. lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
  10530. }
  10531. /* Always clear and re-arm the slow-path EQ */
  10532. lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
  10533. /* Catch the no cq entry condition */
  10534. if (unlikely(ecount == 0)) {
  10535. if (phba->intr_type == MSIX)
  10536. /* MSI-X treated interrupt served as no EQ share INT */
  10537. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10538. "0357 MSI-X interrupt with no EQE\n");
  10539. else
  10540. /* Non MSI-X treated on interrupt as EQ share INT */
  10541. return IRQ_NONE;
  10542. }
  10543. return IRQ_HANDLED;
  10544. } /* lpfc_sli4_sp_intr_handler */
  10545. /**
  10546. * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
  10547. * @irq: Interrupt number.
  10548. * @dev_id: The device context pointer.
  10549. *
  10550. * This function is directly called from the PCI layer as an interrupt
  10551. * service routine when device with SLI-4 interface spec is enabled with
  10552. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10553. * ring event in the HBA. However, when the device is enabled with either
  10554. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10555. * device-level interrupt handler. When the PCI slot is in error recovery
  10556. * or the HBA is undergoing initialization, the interrupt handler will not
  10557. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10558. * the intrrupt context. This function is called without any lock held.
  10559. * It gets the hbalock to access and update SLI data structures. Note that,
  10560. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10561. * equal to that of FCP CQ index.
  10562. *
  10563. * This function returns IRQ_HANDLED when interrupt is handled else it
  10564. * returns IRQ_NONE.
  10565. **/
  10566. irqreturn_t
  10567. lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
  10568. {
  10569. struct lpfc_hba *phba;
  10570. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10571. struct lpfc_queue *fpeq;
  10572. struct lpfc_eqe *eqe;
  10573. unsigned long iflag;
  10574. int ecount = 0;
  10575. uint32_t fcp_eqidx;
  10576. /* Get the driver's phba structure from the dev_id */
  10577. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10578. phba = fcp_eq_hdl->phba;
  10579. fcp_eqidx = fcp_eq_hdl->idx;
  10580. if (unlikely(!phba))
  10581. return IRQ_NONE;
  10582. if (unlikely(!phba->sli4_hba.fp_eq))
  10583. return IRQ_NONE;
  10584. /* Get to the EQ struct associated with this vector */
  10585. fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
  10586. if (unlikely(!fpeq))
  10587. return IRQ_NONE;
  10588. /* Check device state for handling interrupt */
  10589. if (unlikely(lpfc_intr_state_check(phba))) {
  10590. /* Check again for link_state with lock held */
  10591. spin_lock_irqsave(&phba->hbalock, iflag);
  10592. if (phba->link_state < LPFC_LINK_DOWN)
  10593. /* Flush, clear interrupt, and rearm the EQ */
  10594. lpfc_sli4_eq_flush(phba, fpeq);
  10595. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10596. return IRQ_NONE;
  10597. }
  10598. /*
  10599. * Process all the event on FCP fast-path EQ
  10600. */
  10601. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10602. lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
  10603. if (!(++ecount % fpeq->entry_repost))
  10604. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10605. }
  10606. /* Always clear and re-arm the fast-path EQ */
  10607. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10608. if (unlikely(ecount == 0)) {
  10609. if (phba->intr_type == MSIX)
  10610. /* MSI-X treated interrupt served as no EQ share INT */
  10611. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10612. "0358 MSI-X interrupt with no EQE\n");
  10613. else
  10614. /* Non MSI-X treated on interrupt as EQ share INT */
  10615. return IRQ_NONE;
  10616. }
  10617. return IRQ_HANDLED;
  10618. } /* lpfc_sli4_fp_intr_handler */
  10619. /**
  10620. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10621. * @irq: Interrupt number.
  10622. * @dev_id: The device context pointer.
  10623. *
  10624. * This function is the device-level interrupt handler to device with SLI-4
  10625. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10626. * interrupt mode is enabled and there is an event in the HBA which requires
  10627. * driver attention. This function invokes the slow-path interrupt attention
  10628. * handling function and fast-path interrupt attention handling function in
  10629. * turn to process the relevant HBA attention events. This function is called
  10630. * without any lock held. It gets the hbalock to access and update SLI data
  10631. * structures.
  10632. *
  10633. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10634. * returns IRQ_NONE.
  10635. **/
  10636. irqreturn_t
  10637. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10638. {
  10639. struct lpfc_hba *phba;
  10640. irqreturn_t sp_irq_rc, fp_irq_rc;
  10641. bool fp_handled = false;
  10642. uint32_t fcp_eqidx;
  10643. /* Get the driver's phba structure from the dev_id */
  10644. phba = (struct lpfc_hba *)dev_id;
  10645. if (unlikely(!phba))
  10646. return IRQ_NONE;
  10647. /*
  10648. * Invokes slow-path host attention interrupt handling as appropriate.
  10649. */
  10650. sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
  10651. /*
  10652. * Invoke fast-path host attention interrupt handling as appropriate.
  10653. */
  10654. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
  10655. fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
  10656. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10657. if (fp_irq_rc == IRQ_HANDLED)
  10658. fp_handled |= true;
  10659. }
  10660. return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
  10661. } /* lpfc_sli4_intr_handler */
  10662. /**
  10663. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10664. * @queue: The queue structure to free.
  10665. *
  10666. * This function frees a queue structure and the DMAable memory used for
  10667. * the host resident queue. This function must be called after destroying the
  10668. * queue on the HBA.
  10669. **/
  10670. void
  10671. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10672. {
  10673. struct lpfc_dmabuf *dmabuf;
  10674. if (!queue)
  10675. return;
  10676. while (!list_empty(&queue->page_list)) {
  10677. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10678. list);
  10679. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10680. dmabuf->virt, dmabuf->phys);
  10681. kfree(dmabuf);
  10682. }
  10683. kfree(queue);
  10684. return;
  10685. }
  10686. /**
  10687. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10688. * @phba: The HBA that this queue is being created on.
  10689. * @entry_size: The size of each queue entry for this queue.
  10690. * @entry count: The number of entries that this queue will handle.
  10691. *
  10692. * This function allocates a queue structure and the DMAable memory used for
  10693. * the host resident queue. This function must be called before creating the
  10694. * queue on the HBA.
  10695. **/
  10696. struct lpfc_queue *
  10697. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10698. uint32_t entry_count)
  10699. {
  10700. struct lpfc_queue *queue;
  10701. struct lpfc_dmabuf *dmabuf;
  10702. int x, total_qe_count;
  10703. void *dma_pointer;
  10704. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10705. if (!phba->sli4_hba.pc_sli4_params.supported)
  10706. hw_page_size = SLI4_PAGE_SIZE;
  10707. queue = kzalloc(sizeof(struct lpfc_queue) +
  10708. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  10709. if (!queue)
  10710. return NULL;
  10711. queue->page_count = (ALIGN(entry_size * entry_count,
  10712. hw_page_size))/hw_page_size;
  10713. INIT_LIST_HEAD(&queue->list);
  10714. INIT_LIST_HEAD(&queue->page_list);
  10715. INIT_LIST_HEAD(&queue->child_list);
  10716. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  10717. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  10718. if (!dmabuf)
  10719. goto out_fail;
  10720. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  10721. hw_page_size, &dmabuf->phys,
  10722. GFP_KERNEL);
  10723. if (!dmabuf->virt) {
  10724. kfree(dmabuf);
  10725. goto out_fail;
  10726. }
  10727. memset(dmabuf->virt, 0, hw_page_size);
  10728. dmabuf->buffer_tag = x;
  10729. list_add_tail(&dmabuf->list, &queue->page_list);
  10730. /* initialize queue's entry array */
  10731. dma_pointer = dmabuf->virt;
  10732. for (; total_qe_count < entry_count &&
  10733. dma_pointer < (hw_page_size + dmabuf->virt);
  10734. total_qe_count++, dma_pointer += entry_size) {
  10735. queue->qe[total_qe_count].address = dma_pointer;
  10736. }
  10737. }
  10738. queue->entry_size = entry_size;
  10739. queue->entry_count = entry_count;
  10740. /*
  10741. * entry_repost is calculated based on the number of entries in the
  10742. * queue. This works out except for RQs. If buffers are NOT initially
  10743. * posted for every RQE, entry_repost should be adjusted accordingly.
  10744. */
  10745. queue->entry_repost = (entry_count >> 3);
  10746. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  10747. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  10748. queue->phba = phba;
  10749. return queue;
  10750. out_fail:
  10751. lpfc_sli4_queue_free(queue);
  10752. return NULL;
  10753. }
  10754. /**
  10755. * lpfc_eq_create - Create an Event Queue on the HBA
  10756. * @phba: HBA structure that indicates port to create a queue on.
  10757. * @eq: The queue structure to use to create the event queue.
  10758. * @imax: The maximum interrupt per second limit.
  10759. *
  10760. * This function creates an event queue, as detailed in @eq, on a port,
  10761. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  10762. *
  10763. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  10764. * is used to get the entry count and entry size that are necessary to
  10765. * determine the number of pages to allocate and use for this queue. This
  10766. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  10767. * event queue. This function is asynchronous and will wait for the mailbox
  10768. * command to finish before continuing.
  10769. *
  10770. * On success this function will return a zero. If unable to allocate enough
  10771. * memory this function will return -ENOMEM. If the queue create mailbox command
  10772. * fails this function will return -ENXIO.
  10773. **/
  10774. uint32_t
  10775. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  10776. {
  10777. struct lpfc_mbx_eq_create *eq_create;
  10778. LPFC_MBOXQ_t *mbox;
  10779. int rc, length, status = 0;
  10780. struct lpfc_dmabuf *dmabuf;
  10781. uint32_t shdr_status, shdr_add_status;
  10782. union lpfc_sli4_cfg_shdr *shdr;
  10783. uint16_t dmult;
  10784. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10785. /* sanity check on queue memory */
  10786. if (!eq)
  10787. return -ENODEV;
  10788. if (!phba->sli4_hba.pc_sli4_params.supported)
  10789. hw_page_size = SLI4_PAGE_SIZE;
  10790. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10791. if (!mbox)
  10792. return -ENOMEM;
  10793. length = (sizeof(struct lpfc_mbx_eq_create) -
  10794. sizeof(struct lpfc_sli4_cfg_mhdr));
  10795. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10796. LPFC_MBOX_OPCODE_EQ_CREATE,
  10797. length, LPFC_SLI4_MBX_EMBED);
  10798. eq_create = &mbox->u.mqe.un.eq_create;
  10799. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  10800. eq->page_count);
  10801. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  10802. LPFC_EQE_SIZE);
  10803. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  10804. /* Calculate delay multiper from maximum interrupt per second */
  10805. dmult = LPFC_DMULT_CONST/imax - 1;
  10806. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  10807. dmult);
  10808. switch (eq->entry_count) {
  10809. default:
  10810. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10811. "0360 Unsupported EQ count. (%d)\n",
  10812. eq->entry_count);
  10813. if (eq->entry_count < 256)
  10814. return -EINVAL;
  10815. /* otherwise default to smallest count (drop through) */
  10816. case 256:
  10817. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10818. LPFC_EQ_CNT_256);
  10819. break;
  10820. case 512:
  10821. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10822. LPFC_EQ_CNT_512);
  10823. break;
  10824. case 1024:
  10825. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10826. LPFC_EQ_CNT_1024);
  10827. break;
  10828. case 2048:
  10829. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10830. LPFC_EQ_CNT_2048);
  10831. break;
  10832. case 4096:
  10833. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10834. LPFC_EQ_CNT_4096);
  10835. break;
  10836. }
  10837. list_for_each_entry(dmabuf, &eq->page_list, list) {
  10838. memset(dmabuf->virt, 0, hw_page_size);
  10839. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  10840. putPaddrLow(dmabuf->phys);
  10841. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  10842. putPaddrHigh(dmabuf->phys);
  10843. }
  10844. mbox->vport = phba->pport;
  10845. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  10846. mbox->context1 = NULL;
  10847. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10848. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  10849. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10850. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10851. if (shdr_status || shdr_add_status || rc) {
  10852. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10853. "2500 EQ_CREATE mailbox failed with "
  10854. "status x%x add_status x%x, mbx status x%x\n",
  10855. shdr_status, shdr_add_status, rc);
  10856. status = -ENXIO;
  10857. }
  10858. eq->type = LPFC_EQ;
  10859. eq->subtype = LPFC_NONE;
  10860. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  10861. if (eq->queue_id == 0xFFFF)
  10862. status = -ENXIO;
  10863. eq->host_index = 0;
  10864. eq->hba_index = 0;
  10865. mempool_free(mbox, phba->mbox_mem_pool);
  10866. return status;
  10867. }
  10868. /**
  10869. * lpfc_cq_create - Create a Completion Queue on the HBA
  10870. * @phba: HBA structure that indicates port to create a queue on.
  10871. * @cq: The queue structure to use to create the completion queue.
  10872. * @eq: The event queue to bind this completion queue to.
  10873. *
  10874. * This function creates a completion queue, as detailed in @wq, on a port,
  10875. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  10876. *
  10877. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  10878. * is used to get the entry count and entry size that are necessary to
  10879. * determine the number of pages to allocate and use for this queue. The @eq
  10880. * is used to indicate which event queue to bind this completion queue to. This
  10881. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  10882. * completion queue. This function is asynchronous and will wait for the mailbox
  10883. * command to finish before continuing.
  10884. *
  10885. * On success this function will return a zero. If unable to allocate enough
  10886. * memory this function will return -ENOMEM. If the queue create mailbox command
  10887. * fails this function will return -ENXIO.
  10888. **/
  10889. uint32_t
  10890. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10891. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  10892. {
  10893. struct lpfc_mbx_cq_create *cq_create;
  10894. struct lpfc_dmabuf *dmabuf;
  10895. LPFC_MBOXQ_t *mbox;
  10896. int rc, length, status = 0;
  10897. uint32_t shdr_status, shdr_add_status;
  10898. union lpfc_sli4_cfg_shdr *shdr;
  10899. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10900. /* sanity check on queue memory */
  10901. if (!cq || !eq)
  10902. return -ENODEV;
  10903. if (!phba->sli4_hba.pc_sli4_params.supported)
  10904. hw_page_size = SLI4_PAGE_SIZE;
  10905. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10906. if (!mbox)
  10907. return -ENOMEM;
  10908. length = (sizeof(struct lpfc_mbx_cq_create) -
  10909. sizeof(struct lpfc_sli4_cfg_mhdr));
  10910. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10911. LPFC_MBOX_OPCODE_CQ_CREATE,
  10912. length, LPFC_SLI4_MBX_EMBED);
  10913. cq_create = &mbox->u.mqe.un.cq_create;
  10914. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  10915. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  10916. cq->page_count);
  10917. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  10918. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  10919. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  10920. phba->sli4_hba.pc_sli4_params.cqv);
  10921. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  10922. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  10923. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  10924. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  10925. eq->queue_id);
  10926. } else {
  10927. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  10928. eq->queue_id);
  10929. }
  10930. switch (cq->entry_count) {
  10931. default:
  10932. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10933. "0361 Unsupported CQ count. (%d)\n",
  10934. cq->entry_count);
  10935. if (cq->entry_count < 256)
  10936. return -EINVAL;
  10937. /* otherwise default to smallest count (drop through) */
  10938. case 256:
  10939. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10940. LPFC_CQ_CNT_256);
  10941. break;
  10942. case 512:
  10943. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10944. LPFC_CQ_CNT_512);
  10945. break;
  10946. case 1024:
  10947. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10948. LPFC_CQ_CNT_1024);
  10949. break;
  10950. }
  10951. list_for_each_entry(dmabuf, &cq->page_list, list) {
  10952. memset(dmabuf->virt, 0, hw_page_size);
  10953. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  10954. putPaddrLow(dmabuf->phys);
  10955. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  10956. putPaddrHigh(dmabuf->phys);
  10957. }
  10958. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10959. /* The IOCTL status is embedded in the mailbox subheader. */
  10960. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10961. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10962. if (shdr_status || shdr_add_status || rc) {
  10963. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10964. "2501 CQ_CREATE mailbox failed with "
  10965. "status x%x add_status x%x, mbx status x%x\n",
  10966. shdr_status, shdr_add_status, rc);
  10967. status = -ENXIO;
  10968. goto out;
  10969. }
  10970. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  10971. if (cq->queue_id == 0xFFFF) {
  10972. status = -ENXIO;
  10973. goto out;
  10974. }
  10975. /* link the cq onto the parent eq child list */
  10976. list_add_tail(&cq->list, &eq->child_list);
  10977. /* Set up completion queue's type and subtype */
  10978. cq->type = type;
  10979. cq->subtype = subtype;
  10980. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  10981. cq->assoc_qid = eq->queue_id;
  10982. cq->host_index = 0;
  10983. cq->hba_index = 0;
  10984. out:
  10985. mempool_free(mbox, phba->mbox_mem_pool);
  10986. return status;
  10987. }
  10988. /**
  10989. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  10990. * @phba: HBA structure that indicates port to create a queue on.
  10991. * @mq: The queue structure to use to create the mailbox queue.
  10992. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  10993. * @cq: The completion queue to associate with this cq.
  10994. *
  10995. * This function provides failback (fb) functionality when the
  10996. * mq_create_ext fails on older FW generations. It's purpose is identical
  10997. * to mq_create_ext otherwise.
  10998. *
  10999. * This routine cannot fail as all attributes were previously accessed and
  11000. * initialized in mq_create_ext.
  11001. **/
  11002. static void
  11003. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11004. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11005. {
  11006. struct lpfc_mbx_mq_create *mq_create;
  11007. struct lpfc_dmabuf *dmabuf;
  11008. int length;
  11009. length = (sizeof(struct lpfc_mbx_mq_create) -
  11010. sizeof(struct lpfc_sli4_cfg_mhdr));
  11011. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11012. LPFC_MBOX_OPCODE_MQ_CREATE,
  11013. length, LPFC_SLI4_MBX_EMBED);
  11014. mq_create = &mbox->u.mqe.un.mq_create;
  11015. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11016. mq->page_count);
  11017. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11018. cq->queue_id);
  11019. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11020. switch (mq->entry_count) {
  11021. case 16:
  11022. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11023. LPFC_MQ_RING_SIZE_16);
  11024. break;
  11025. case 32:
  11026. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11027. LPFC_MQ_RING_SIZE_32);
  11028. break;
  11029. case 64:
  11030. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11031. LPFC_MQ_RING_SIZE_64);
  11032. break;
  11033. case 128:
  11034. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11035. LPFC_MQ_RING_SIZE_128);
  11036. break;
  11037. }
  11038. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11039. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11040. putPaddrLow(dmabuf->phys);
  11041. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11042. putPaddrHigh(dmabuf->phys);
  11043. }
  11044. }
  11045. /**
  11046. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11047. * @phba: HBA structure that indicates port to create a queue on.
  11048. * @mq: The queue structure to use to create the mailbox queue.
  11049. * @cq: The completion queue to associate with this cq.
  11050. * @subtype: The queue's subtype.
  11051. *
  11052. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11053. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11054. *
  11055. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11056. * is used to get the entry count and entry size that are necessary to
  11057. * determine the number of pages to allocate and use for this queue. This
  11058. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  11059. * mailbox queue. This function is asynchronous and will wait for the mailbox
  11060. * command to finish before continuing.
  11061. *
  11062. * On success this function will return a zero. If unable to allocate enough
  11063. * memory this function will return -ENOMEM. If the queue create mailbox command
  11064. * fails this function will return -ENXIO.
  11065. **/
  11066. int32_t
  11067. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11068. struct lpfc_queue *cq, uint32_t subtype)
  11069. {
  11070. struct lpfc_mbx_mq_create *mq_create;
  11071. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11072. struct lpfc_dmabuf *dmabuf;
  11073. LPFC_MBOXQ_t *mbox;
  11074. int rc, length, status = 0;
  11075. uint32_t shdr_status, shdr_add_status;
  11076. union lpfc_sli4_cfg_shdr *shdr;
  11077. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11078. /* sanity check on queue memory */
  11079. if (!mq || !cq)
  11080. return -ENODEV;
  11081. if (!phba->sli4_hba.pc_sli4_params.supported)
  11082. hw_page_size = SLI4_PAGE_SIZE;
  11083. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11084. if (!mbox)
  11085. return -ENOMEM;
  11086. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11087. sizeof(struct lpfc_sli4_cfg_mhdr));
  11088. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11089. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11090. length, LPFC_SLI4_MBX_EMBED);
  11091. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11092. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11093. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11094. &mq_create_ext->u.request, mq->page_count);
  11095. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11096. &mq_create_ext->u.request, 1);
  11097. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11098. &mq_create_ext->u.request, 1);
  11099. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11100. &mq_create_ext->u.request, 1);
  11101. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11102. &mq_create_ext->u.request, 1);
  11103. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11104. &mq_create_ext->u.request, 1);
  11105. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11106. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11107. phba->sli4_hba.pc_sli4_params.mqv);
  11108. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11109. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11110. cq->queue_id);
  11111. else
  11112. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11113. cq->queue_id);
  11114. switch (mq->entry_count) {
  11115. default:
  11116. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11117. "0362 Unsupported MQ count. (%d)\n",
  11118. mq->entry_count);
  11119. if (mq->entry_count < 16)
  11120. return -EINVAL;
  11121. /* otherwise default to smallest count (drop through) */
  11122. case 16:
  11123. bf_set(lpfc_mq_context_ring_size,
  11124. &mq_create_ext->u.request.context,
  11125. LPFC_MQ_RING_SIZE_16);
  11126. break;
  11127. case 32:
  11128. bf_set(lpfc_mq_context_ring_size,
  11129. &mq_create_ext->u.request.context,
  11130. LPFC_MQ_RING_SIZE_32);
  11131. break;
  11132. case 64:
  11133. bf_set(lpfc_mq_context_ring_size,
  11134. &mq_create_ext->u.request.context,
  11135. LPFC_MQ_RING_SIZE_64);
  11136. break;
  11137. case 128:
  11138. bf_set(lpfc_mq_context_ring_size,
  11139. &mq_create_ext->u.request.context,
  11140. LPFC_MQ_RING_SIZE_128);
  11141. break;
  11142. }
  11143. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11144. memset(dmabuf->virt, 0, hw_page_size);
  11145. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11146. putPaddrLow(dmabuf->phys);
  11147. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11148. putPaddrHigh(dmabuf->phys);
  11149. }
  11150. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11151. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11152. &mq_create_ext->u.response);
  11153. if (rc != MBX_SUCCESS) {
  11154. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11155. "2795 MQ_CREATE_EXT failed with "
  11156. "status x%x. Failback to MQ_CREATE.\n",
  11157. rc);
  11158. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11159. mq_create = &mbox->u.mqe.un.mq_create;
  11160. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11161. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11162. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11163. &mq_create->u.response);
  11164. }
  11165. /* The IOCTL status is embedded in the mailbox subheader. */
  11166. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11167. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11168. if (shdr_status || shdr_add_status || rc) {
  11169. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11170. "2502 MQ_CREATE mailbox failed with "
  11171. "status x%x add_status x%x, mbx status x%x\n",
  11172. shdr_status, shdr_add_status, rc);
  11173. status = -ENXIO;
  11174. goto out;
  11175. }
  11176. if (mq->queue_id == 0xFFFF) {
  11177. status = -ENXIO;
  11178. goto out;
  11179. }
  11180. mq->type = LPFC_MQ;
  11181. mq->assoc_qid = cq->queue_id;
  11182. mq->subtype = subtype;
  11183. mq->host_index = 0;
  11184. mq->hba_index = 0;
  11185. /* link the mq onto the parent cq child list */
  11186. list_add_tail(&mq->list, &cq->child_list);
  11187. out:
  11188. mempool_free(mbox, phba->mbox_mem_pool);
  11189. return status;
  11190. }
  11191. /**
  11192. * lpfc_wq_create - Create a Work Queue on the HBA
  11193. * @phba: HBA structure that indicates port to create a queue on.
  11194. * @wq: The queue structure to use to create the work queue.
  11195. * @cq: The completion queue to bind this work queue to.
  11196. * @subtype: The subtype of the work queue indicating its functionality.
  11197. *
  11198. * This function creates a work queue, as detailed in @wq, on a port, described
  11199. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11200. *
  11201. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11202. * is used to get the entry count and entry size that are necessary to
  11203. * determine the number of pages to allocate and use for this queue. The @cq
  11204. * is used to indicate which completion queue to bind this work queue to. This
  11205. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11206. * work queue. This function is asynchronous and will wait for the mailbox
  11207. * command to finish before continuing.
  11208. *
  11209. * On success this function will return a zero. If unable to allocate enough
  11210. * memory this function will return -ENOMEM. If the queue create mailbox command
  11211. * fails this function will return -ENXIO.
  11212. **/
  11213. uint32_t
  11214. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11215. struct lpfc_queue *cq, uint32_t subtype)
  11216. {
  11217. struct lpfc_mbx_wq_create *wq_create;
  11218. struct lpfc_dmabuf *dmabuf;
  11219. LPFC_MBOXQ_t *mbox;
  11220. int rc, length, status = 0;
  11221. uint32_t shdr_status, shdr_add_status;
  11222. union lpfc_sli4_cfg_shdr *shdr;
  11223. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11224. struct dma_address *page;
  11225. /* sanity check on queue memory */
  11226. if (!wq || !cq)
  11227. return -ENODEV;
  11228. if (!phba->sli4_hba.pc_sli4_params.supported)
  11229. hw_page_size = SLI4_PAGE_SIZE;
  11230. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11231. if (!mbox)
  11232. return -ENOMEM;
  11233. length = (sizeof(struct lpfc_mbx_wq_create) -
  11234. sizeof(struct lpfc_sli4_cfg_mhdr));
  11235. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11236. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11237. length, LPFC_SLI4_MBX_EMBED);
  11238. wq_create = &mbox->u.mqe.un.wq_create;
  11239. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11240. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11241. wq->page_count);
  11242. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11243. cq->queue_id);
  11244. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11245. phba->sli4_hba.pc_sli4_params.wqv);
  11246. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11247. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11248. wq->entry_count);
  11249. switch (wq->entry_size) {
  11250. default:
  11251. case 64:
  11252. bf_set(lpfc_mbx_wq_create_wqe_size,
  11253. &wq_create->u.request_1,
  11254. LPFC_WQ_WQE_SIZE_64);
  11255. break;
  11256. case 128:
  11257. bf_set(lpfc_mbx_wq_create_wqe_size,
  11258. &wq_create->u.request_1,
  11259. LPFC_WQ_WQE_SIZE_128);
  11260. break;
  11261. }
  11262. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11263. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11264. page = wq_create->u.request_1.page;
  11265. } else {
  11266. page = wq_create->u.request.page;
  11267. }
  11268. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11269. memset(dmabuf->virt, 0, hw_page_size);
  11270. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11271. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11272. }
  11273. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11274. /* The IOCTL status is embedded in the mailbox subheader. */
  11275. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11276. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11277. if (shdr_status || shdr_add_status || rc) {
  11278. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11279. "2503 WQ_CREATE mailbox failed with "
  11280. "status x%x add_status x%x, mbx status x%x\n",
  11281. shdr_status, shdr_add_status, rc);
  11282. status = -ENXIO;
  11283. goto out;
  11284. }
  11285. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11286. if (wq->queue_id == 0xFFFF) {
  11287. status = -ENXIO;
  11288. goto out;
  11289. }
  11290. wq->type = LPFC_WQ;
  11291. wq->assoc_qid = cq->queue_id;
  11292. wq->subtype = subtype;
  11293. wq->host_index = 0;
  11294. wq->hba_index = 0;
  11295. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  11296. /* link the wq onto the parent cq child list */
  11297. list_add_tail(&wq->list, &cq->child_list);
  11298. out:
  11299. mempool_free(mbox, phba->mbox_mem_pool);
  11300. return status;
  11301. }
  11302. /**
  11303. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11304. * @phba: HBA structure that indicates port to create a queue on.
  11305. * @rq: The queue structure to use for the receive queue.
  11306. * @qno: The associated HBQ number
  11307. *
  11308. *
  11309. * For SLI4 we need to adjust the RQ repost value based on
  11310. * the number of buffers that are initially posted to the RQ.
  11311. */
  11312. void
  11313. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11314. {
  11315. uint32_t cnt;
  11316. /* sanity check on queue memory */
  11317. if (!rq)
  11318. return;
  11319. cnt = lpfc_hbq_defs[qno]->entry_count;
  11320. /* Recalc repost for RQs based on buffers initially posted */
  11321. cnt = (cnt >> 3);
  11322. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11323. cnt = LPFC_QUEUE_MIN_REPOST;
  11324. rq->entry_repost = cnt;
  11325. }
  11326. /**
  11327. * lpfc_rq_create - Create a Receive Queue on the HBA
  11328. * @phba: HBA structure that indicates port to create a queue on.
  11329. * @hrq: The queue structure to use to create the header receive queue.
  11330. * @drq: The queue structure to use to create the data receive queue.
  11331. * @cq: The completion queue to bind this work queue to.
  11332. *
  11333. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11334. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11335. * to the HBA.
  11336. *
  11337. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11338. * struct is used to get the entry count that is necessary to determine the
  11339. * number of pages to use for this queue. The @cq is used to indicate which
  11340. * completion queue to bind received buffers that are posted to these queues to.
  11341. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11342. * receive queue pair. This function is asynchronous and will wait for the
  11343. * mailbox command to finish before continuing.
  11344. *
  11345. * On success this function will return a zero. If unable to allocate enough
  11346. * memory this function will return -ENOMEM. If the queue create mailbox command
  11347. * fails this function will return -ENXIO.
  11348. **/
  11349. uint32_t
  11350. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11351. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11352. {
  11353. struct lpfc_mbx_rq_create *rq_create;
  11354. struct lpfc_dmabuf *dmabuf;
  11355. LPFC_MBOXQ_t *mbox;
  11356. int rc, length, status = 0;
  11357. uint32_t shdr_status, shdr_add_status;
  11358. union lpfc_sli4_cfg_shdr *shdr;
  11359. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11360. /* sanity check on queue memory */
  11361. if (!hrq || !drq || !cq)
  11362. return -ENODEV;
  11363. if (!phba->sli4_hba.pc_sli4_params.supported)
  11364. hw_page_size = SLI4_PAGE_SIZE;
  11365. if (hrq->entry_count != drq->entry_count)
  11366. return -EINVAL;
  11367. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11368. if (!mbox)
  11369. return -ENOMEM;
  11370. length = (sizeof(struct lpfc_mbx_rq_create) -
  11371. sizeof(struct lpfc_sli4_cfg_mhdr));
  11372. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11373. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11374. length, LPFC_SLI4_MBX_EMBED);
  11375. rq_create = &mbox->u.mqe.un.rq_create;
  11376. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11377. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11378. phba->sli4_hba.pc_sli4_params.rqv);
  11379. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11380. bf_set(lpfc_rq_context_rqe_count_1,
  11381. &rq_create->u.request.context,
  11382. hrq->entry_count);
  11383. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11384. bf_set(lpfc_rq_context_rqe_size,
  11385. &rq_create->u.request.context,
  11386. LPFC_RQE_SIZE_8);
  11387. bf_set(lpfc_rq_context_page_size,
  11388. &rq_create->u.request.context,
  11389. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11390. } else {
  11391. switch (hrq->entry_count) {
  11392. default:
  11393. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11394. "2535 Unsupported RQ count. (%d)\n",
  11395. hrq->entry_count);
  11396. if (hrq->entry_count < 512)
  11397. return -EINVAL;
  11398. /* otherwise default to smallest count (drop through) */
  11399. case 512:
  11400. bf_set(lpfc_rq_context_rqe_count,
  11401. &rq_create->u.request.context,
  11402. LPFC_RQ_RING_SIZE_512);
  11403. break;
  11404. case 1024:
  11405. bf_set(lpfc_rq_context_rqe_count,
  11406. &rq_create->u.request.context,
  11407. LPFC_RQ_RING_SIZE_1024);
  11408. break;
  11409. case 2048:
  11410. bf_set(lpfc_rq_context_rqe_count,
  11411. &rq_create->u.request.context,
  11412. LPFC_RQ_RING_SIZE_2048);
  11413. break;
  11414. case 4096:
  11415. bf_set(lpfc_rq_context_rqe_count,
  11416. &rq_create->u.request.context,
  11417. LPFC_RQ_RING_SIZE_4096);
  11418. break;
  11419. }
  11420. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11421. LPFC_HDR_BUF_SIZE);
  11422. }
  11423. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11424. cq->queue_id);
  11425. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11426. hrq->page_count);
  11427. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11428. memset(dmabuf->virt, 0, hw_page_size);
  11429. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11430. putPaddrLow(dmabuf->phys);
  11431. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11432. putPaddrHigh(dmabuf->phys);
  11433. }
  11434. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11435. /* The IOCTL status is embedded in the mailbox subheader. */
  11436. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11437. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11438. if (shdr_status || shdr_add_status || rc) {
  11439. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11440. "2504 RQ_CREATE mailbox failed with "
  11441. "status x%x add_status x%x, mbx status x%x\n",
  11442. shdr_status, shdr_add_status, rc);
  11443. status = -ENXIO;
  11444. goto out;
  11445. }
  11446. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11447. if (hrq->queue_id == 0xFFFF) {
  11448. status = -ENXIO;
  11449. goto out;
  11450. }
  11451. hrq->type = LPFC_HRQ;
  11452. hrq->assoc_qid = cq->queue_id;
  11453. hrq->subtype = subtype;
  11454. hrq->host_index = 0;
  11455. hrq->hba_index = 0;
  11456. /* now create the data queue */
  11457. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11458. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11459. length, LPFC_SLI4_MBX_EMBED);
  11460. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11461. phba->sli4_hba.pc_sli4_params.rqv);
  11462. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11463. bf_set(lpfc_rq_context_rqe_count_1,
  11464. &rq_create->u.request.context, hrq->entry_count);
  11465. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11466. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11467. LPFC_RQE_SIZE_8);
  11468. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11469. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11470. } else {
  11471. switch (drq->entry_count) {
  11472. default:
  11473. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11474. "2536 Unsupported RQ count. (%d)\n",
  11475. drq->entry_count);
  11476. if (drq->entry_count < 512)
  11477. return -EINVAL;
  11478. /* otherwise default to smallest count (drop through) */
  11479. case 512:
  11480. bf_set(lpfc_rq_context_rqe_count,
  11481. &rq_create->u.request.context,
  11482. LPFC_RQ_RING_SIZE_512);
  11483. break;
  11484. case 1024:
  11485. bf_set(lpfc_rq_context_rqe_count,
  11486. &rq_create->u.request.context,
  11487. LPFC_RQ_RING_SIZE_1024);
  11488. break;
  11489. case 2048:
  11490. bf_set(lpfc_rq_context_rqe_count,
  11491. &rq_create->u.request.context,
  11492. LPFC_RQ_RING_SIZE_2048);
  11493. break;
  11494. case 4096:
  11495. bf_set(lpfc_rq_context_rqe_count,
  11496. &rq_create->u.request.context,
  11497. LPFC_RQ_RING_SIZE_4096);
  11498. break;
  11499. }
  11500. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11501. LPFC_DATA_BUF_SIZE);
  11502. }
  11503. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11504. cq->queue_id);
  11505. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11506. drq->page_count);
  11507. list_for_each_entry(dmabuf, &drq->page_list, list) {
  11508. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11509. putPaddrLow(dmabuf->phys);
  11510. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11511. putPaddrHigh(dmabuf->phys);
  11512. }
  11513. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11514. /* The IOCTL status is embedded in the mailbox subheader. */
  11515. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11516. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11517. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11518. if (shdr_status || shdr_add_status || rc) {
  11519. status = -ENXIO;
  11520. goto out;
  11521. }
  11522. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11523. if (drq->queue_id == 0xFFFF) {
  11524. status = -ENXIO;
  11525. goto out;
  11526. }
  11527. drq->type = LPFC_DRQ;
  11528. drq->assoc_qid = cq->queue_id;
  11529. drq->subtype = subtype;
  11530. drq->host_index = 0;
  11531. drq->hba_index = 0;
  11532. /* link the header and data RQs onto the parent cq child list */
  11533. list_add_tail(&hrq->list, &cq->child_list);
  11534. list_add_tail(&drq->list, &cq->child_list);
  11535. out:
  11536. mempool_free(mbox, phba->mbox_mem_pool);
  11537. return status;
  11538. }
  11539. /**
  11540. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  11541. * @eq: The queue structure associated with the queue to destroy.
  11542. *
  11543. * This function destroys a queue, as detailed in @eq by sending an mailbox
  11544. * command, specific to the type of queue, to the HBA.
  11545. *
  11546. * The @eq struct is used to get the queue ID of the queue to destroy.
  11547. *
  11548. * On success this function will return a zero. If the queue destroy mailbox
  11549. * command fails this function will return -ENXIO.
  11550. **/
  11551. uint32_t
  11552. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11553. {
  11554. LPFC_MBOXQ_t *mbox;
  11555. int rc, length, status = 0;
  11556. uint32_t shdr_status, shdr_add_status;
  11557. union lpfc_sli4_cfg_shdr *shdr;
  11558. /* sanity check on queue memory */
  11559. if (!eq)
  11560. return -ENODEV;
  11561. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  11562. if (!mbox)
  11563. return -ENOMEM;
  11564. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  11565. sizeof(struct lpfc_sli4_cfg_mhdr));
  11566. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11567. LPFC_MBOX_OPCODE_EQ_DESTROY,
  11568. length, LPFC_SLI4_MBX_EMBED);
  11569. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  11570. eq->queue_id);
  11571. mbox->vport = eq->phba->pport;
  11572. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11573. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  11574. /* The IOCTL status is embedded in the mailbox subheader. */
  11575. shdr = (union lpfc_sli4_cfg_shdr *)
  11576. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  11577. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11578. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11579. if (shdr_status || shdr_add_status || rc) {
  11580. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11581. "2505 EQ_DESTROY mailbox failed with "
  11582. "status x%x add_status x%x, mbx status x%x\n",
  11583. shdr_status, shdr_add_status, rc);
  11584. status = -ENXIO;
  11585. }
  11586. /* Remove eq from any list */
  11587. list_del_init(&eq->list);
  11588. mempool_free(mbox, eq->phba->mbox_mem_pool);
  11589. return status;
  11590. }
  11591. /**
  11592. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  11593. * @cq: The queue structure associated with the queue to destroy.
  11594. *
  11595. * This function destroys a queue, as detailed in @cq by sending an mailbox
  11596. * command, specific to the type of queue, to the HBA.
  11597. *
  11598. * The @cq struct is used to get the queue ID of the queue to destroy.
  11599. *
  11600. * On success this function will return a zero. If the queue destroy mailbox
  11601. * command fails this function will return -ENXIO.
  11602. **/
  11603. uint32_t
  11604. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  11605. {
  11606. LPFC_MBOXQ_t *mbox;
  11607. int rc, length, status = 0;
  11608. uint32_t shdr_status, shdr_add_status;
  11609. union lpfc_sli4_cfg_shdr *shdr;
  11610. /* sanity check on queue memory */
  11611. if (!cq)
  11612. return -ENODEV;
  11613. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  11614. if (!mbox)
  11615. return -ENOMEM;
  11616. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  11617. sizeof(struct lpfc_sli4_cfg_mhdr));
  11618. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11619. LPFC_MBOX_OPCODE_CQ_DESTROY,
  11620. length, LPFC_SLI4_MBX_EMBED);
  11621. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  11622. cq->queue_id);
  11623. mbox->vport = cq->phba->pport;
  11624. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11625. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  11626. /* The IOCTL status is embedded in the mailbox subheader. */
  11627. shdr = (union lpfc_sli4_cfg_shdr *)
  11628. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  11629. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11630. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11631. if (shdr_status || shdr_add_status || rc) {
  11632. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11633. "2506 CQ_DESTROY mailbox failed with "
  11634. "status x%x add_status x%x, mbx status x%x\n",
  11635. shdr_status, shdr_add_status, rc);
  11636. status = -ENXIO;
  11637. }
  11638. /* Remove cq from any list */
  11639. list_del_init(&cq->list);
  11640. mempool_free(mbox, cq->phba->mbox_mem_pool);
  11641. return status;
  11642. }
  11643. /**
  11644. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  11645. * @qm: The queue structure associated with the queue to destroy.
  11646. *
  11647. * This function destroys a queue, as detailed in @mq by sending an mailbox
  11648. * command, specific to the type of queue, to the HBA.
  11649. *
  11650. * The @mq struct is used to get the queue ID of the queue to destroy.
  11651. *
  11652. * On success this function will return a zero. If the queue destroy mailbox
  11653. * command fails this function will return -ENXIO.
  11654. **/
  11655. uint32_t
  11656. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  11657. {
  11658. LPFC_MBOXQ_t *mbox;
  11659. int rc, length, status = 0;
  11660. uint32_t shdr_status, shdr_add_status;
  11661. union lpfc_sli4_cfg_shdr *shdr;
  11662. /* sanity check on queue memory */
  11663. if (!mq)
  11664. return -ENODEV;
  11665. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  11666. if (!mbox)
  11667. return -ENOMEM;
  11668. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  11669. sizeof(struct lpfc_sli4_cfg_mhdr));
  11670. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11671. LPFC_MBOX_OPCODE_MQ_DESTROY,
  11672. length, LPFC_SLI4_MBX_EMBED);
  11673. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  11674. mq->queue_id);
  11675. mbox->vport = mq->phba->pport;
  11676. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11677. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  11678. /* The IOCTL status is embedded in the mailbox subheader. */
  11679. shdr = (union lpfc_sli4_cfg_shdr *)
  11680. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  11681. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11682. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11683. if (shdr_status || shdr_add_status || rc) {
  11684. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11685. "2507 MQ_DESTROY mailbox failed with "
  11686. "status x%x add_status x%x, mbx status x%x\n",
  11687. shdr_status, shdr_add_status, rc);
  11688. status = -ENXIO;
  11689. }
  11690. /* Remove mq from any list */
  11691. list_del_init(&mq->list);
  11692. mempool_free(mbox, mq->phba->mbox_mem_pool);
  11693. return status;
  11694. }
  11695. /**
  11696. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  11697. * @wq: The queue structure associated with the queue to destroy.
  11698. *
  11699. * This function destroys a queue, as detailed in @wq by sending an mailbox
  11700. * command, specific to the type of queue, to the HBA.
  11701. *
  11702. * The @wq struct is used to get the queue ID of the queue to destroy.
  11703. *
  11704. * On success this function will return a zero. If the queue destroy mailbox
  11705. * command fails this function will return -ENXIO.
  11706. **/
  11707. uint32_t
  11708. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  11709. {
  11710. LPFC_MBOXQ_t *mbox;
  11711. int rc, length, status = 0;
  11712. uint32_t shdr_status, shdr_add_status;
  11713. union lpfc_sli4_cfg_shdr *shdr;
  11714. /* sanity check on queue memory */
  11715. if (!wq)
  11716. return -ENODEV;
  11717. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  11718. if (!mbox)
  11719. return -ENOMEM;
  11720. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  11721. sizeof(struct lpfc_sli4_cfg_mhdr));
  11722. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11723. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  11724. length, LPFC_SLI4_MBX_EMBED);
  11725. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  11726. wq->queue_id);
  11727. mbox->vport = wq->phba->pport;
  11728. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11729. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  11730. shdr = (union lpfc_sli4_cfg_shdr *)
  11731. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  11732. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11733. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11734. if (shdr_status || shdr_add_status || rc) {
  11735. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11736. "2508 WQ_DESTROY mailbox failed with "
  11737. "status x%x add_status x%x, mbx status x%x\n",
  11738. shdr_status, shdr_add_status, rc);
  11739. status = -ENXIO;
  11740. }
  11741. /* Remove wq from any list */
  11742. list_del_init(&wq->list);
  11743. mempool_free(mbox, wq->phba->mbox_mem_pool);
  11744. return status;
  11745. }
  11746. /**
  11747. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  11748. * @rq: The queue structure associated with the queue to destroy.
  11749. *
  11750. * This function destroys a queue, as detailed in @rq by sending an mailbox
  11751. * command, specific to the type of queue, to the HBA.
  11752. *
  11753. * The @rq struct is used to get the queue ID of the queue to destroy.
  11754. *
  11755. * On success this function will return a zero. If the queue destroy mailbox
  11756. * command fails this function will return -ENXIO.
  11757. **/
  11758. uint32_t
  11759. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11760. struct lpfc_queue *drq)
  11761. {
  11762. LPFC_MBOXQ_t *mbox;
  11763. int rc, length, status = 0;
  11764. uint32_t shdr_status, shdr_add_status;
  11765. union lpfc_sli4_cfg_shdr *shdr;
  11766. /* sanity check on queue memory */
  11767. if (!hrq || !drq)
  11768. return -ENODEV;
  11769. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  11770. if (!mbox)
  11771. return -ENOMEM;
  11772. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  11773. sizeof(struct lpfc_sli4_cfg_mhdr));
  11774. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11775. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  11776. length, LPFC_SLI4_MBX_EMBED);
  11777. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11778. hrq->queue_id);
  11779. mbox->vport = hrq->phba->pport;
  11780. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11781. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  11782. /* The IOCTL status is embedded in the mailbox subheader. */
  11783. shdr = (union lpfc_sli4_cfg_shdr *)
  11784. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11785. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11786. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11787. if (shdr_status || shdr_add_status || rc) {
  11788. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11789. "2509 RQ_DESTROY mailbox failed with "
  11790. "status x%x add_status x%x, mbx status x%x\n",
  11791. shdr_status, shdr_add_status, rc);
  11792. if (rc != MBX_TIMEOUT)
  11793. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11794. return -ENXIO;
  11795. }
  11796. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11797. drq->queue_id);
  11798. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  11799. shdr = (union lpfc_sli4_cfg_shdr *)
  11800. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11801. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11802. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11803. if (shdr_status || shdr_add_status || rc) {
  11804. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11805. "2510 RQ_DESTROY mailbox failed with "
  11806. "status x%x add_status x%x, mbx status x%x\n",
  11807. shdr_status, shdr_add_status, rc);
  11808. status = -ENXIO;
  11809. }
  11810. list_del_init(&hrq->list);
  11811. list_del_init(&drq->list);
  11812. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11813. return status;
  11814. }
  11815. /**
  11816. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  11817. * @phba: The virtual port for which this call being executed.
  11818. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  11819. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  11820. * @xritag: the xritag that ties this io to the SGL pages.
  11821. *
  11822. * This routine will post the sgl pages for the IO that has the xritag
  11823. * that is in the iocbq structure. The xritag is assigned during iocbq
  11824. * creation and persists for as long as the driver is loaded.
  11825. * if the caller has fewer than 256 scatter gather segments to map then
  11826. * pdma_phys_addr1 should be 0.
  11827. * If the caller needs to map more than 256 scatter gather segment then
  11828. * pdma_phys_addr1 should be a valid physical address.
  11829. * physical address for SGLs must be 64 byte aligned.
  11830. * If you are going to map 2 SGL's then the first one must have 256 entries
  11831. * the second sgl can have between 1 and 256 entries.
  11832. *
  11833. * Return codes:
  11834. * 0 - Success
  11835. * -ENXIO, -ENOMEM - Failure
  11836. **/
  11837. int
  11838. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  11839. dma_addr_t pdma_phys_addr0,
  11840. dma_addr_t pdma_phys_addr1,
  11841. uint16_t xritag)
  11842. {
  11843. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  11844. LPFC_MBOXQ_t *mbox;
  11845. int rc;
  11846. uint32_t shdr_status, shdr_add_status;
  11847. uint32_t mbox_tmo;
  11848. union lpfc_sli4_cfg_shdr *shdr;
  11849. if (xritag == NO_XRI) {
  11850. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11851. "0364 Invalid param:\n");
  11852. return -EINVAL;
  11853. }
  11854. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11855. if (!mbox)
  11856. return -ENOMEM;
  11857. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11858. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  11859. sizeof(struct lpfc_mbx_post_sgl_pages) -
  11860. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  11861. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  11862. &mbox->u.mqe.un.post_sgl_pages;
  11863. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  11864. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  11865. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  11866. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  11867. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  11868. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  11869. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  11870. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  11871. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  11872. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  11873. if (!phba->sli4_hba.intr_enable)
  11874. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11875. else {
  11876. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  11877. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  11878. }
  11879. /* The IOCTL status is embedded in the mailbox subheader. */
  11880. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  11881. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11882. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11883. if (rc != MBX_TIMEOUT)
  11884. mempool_free(mbox, phba->mbox_mem_pool);
  11885. if (shdr_status || shdr_add_status || rc) {
  11886. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11887. "2511 POST_SGL mailbox failed with "
  11888. "status x%x add_status x%x, mbx status x%x\n",
  11889. shdr_status, shdr_add_status, rc);
  11890. rc = -ENXIO;
  11891. }
  11892. return 0;
  11893. }
  11894. /**
  11895. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  11896. * @phba: pointer to lpfc hba data structure.
  11897. *
  11898. * This routine is invoked to post rpi header templates to the
  11899. * HBA consistent with the SLI-4 interface spec. This routine
  11900. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  11901. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  11902. *
  11903. * Returns
  11904. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  11905. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  11906. **/
  11907. uint16_t
  11908. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  11909. {
  11910. unsigned long xri;
  11911. /*
  11912. * Fetch the next logical xri. Because this index is logical,
  11913. * the driver starts at 0 each time.
  11914. */
  11915. spin_lock_irq(&phba->hbalock);
  11916. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  11917. phba->sli4_hba.max_cfg_param.max_xri, 0);
  11918. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  11919. spin_unlock_irq(&phba->hbalock);
  11920. return NO_XRI;
  11921. } else {
  11922. set_bit(xri, phba->sli4_hba.xri_bmask);
  11923. phba->sli4_hba.max_cfg_param.xri_used++;
  11924. phba->sli4_hba.xri_count++;
  11925. }
  11926. spin_unlock_irq(&phba->hbalock);
  11927. return xri;
  11928. }
  11929. /**
  11930. * lpfc_sli4_free_xri - Release an xri for reuse.
  11931. * @phba: pointer to lpfc hba data structure.
  11932. *
  11933. * This routine is invoked to release an xri to the pool of
  11934. * available rpis maintained by the driver.
  11935. **/
  11936. void
  11937. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  11938. {
  11939. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  11940. phba->sli4_hba.xri_count--;
  11941. phba->sli4_hba.max_cfg_param.xri_used--;
  11942. }
  11943. }
  11944. /**
  11945. * lpfc_sli4_free_xri - Release an xri for reuse.
  11946. * @phba: pointer to lpfc hba data structure.
  11947. *
  11948. * This routine is invoked to release an xri to the pool of
  11949. * available rpis maintained by the driver.
  11950. **/
  11951. void
  11952. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  11953. {
  11954. spin_lock_irq(&phba->hbalock);
  11955. __lpfc_sli4_free_xri(phba, xri);
  11956. spin_unlock_irq(&phba->hbalock);
  11957. }
  11958. /**
  11959. * lpfc_sli4_next_xritag - Get an xritag for the io
  11960. * @phba: Pointer to HBA context object.
  11961. *
  11962. * This function gets an xritag for the iocb. If there is no unused xritag
  11963. * it will return 0xffff.
  11964. * The function returns the allocated xritag if successful, else returns zero.
  11965. * Zero is not a valid xritag.
  11966. * The caller is not required to hold any lock.
  11967. **/
  11968. uint16_t
  11969. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  11970. {
  11971. uint16_t xri_index;
  11972. xri_index = lpfc_sli4_alloc_xri(phba);
  11973. if (xri_index != NO_XRI)
  11974. return xri_index;
  11975. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11976. "2004 Failed to allocate XRI.last XRITAG is %d"
  11977. " Max XRI is %d, Used XRI is %d\n",
  11978. xri_index,
  11979. phba->sli4_hba.max_cfg_param.max_xri,
  11980. phba->sli4_hba.max_cfg_param.xri_used);
  11981. return NO_XRI;
  11982. }
  11983. /**
  11984. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  11985. * @phba: pointer to lpfc hba data structure.
  11986. *
  11987. * This routine is invoked to post a block of driver's sgl pages to the
  11988. * HBA using non-embedded mailbox command. No Lock is held. This routine
  11989. * is only called when the driver is loading and after all IO has been
  11990. * stopped.
  11991. **/
  11992. int
  11993. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
  11994. {
  11995. struct lpfc_sglq *sglq_entry;
  11996. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  11997. struct sgl_page_pairs *sgl_pg_pairs;
  11998. void *viraddr;
  11999. LPFC_MBOXQ_t *mbox;
  12000. uint32_t reqlen, alloclen, pg_pairs;
  12001. uint32_t mbox_tmo;
  12002. uint16_t xritag_start = 0, lxri = 0;
  12003. int els_xri_cnt, rc = 0;
  12004. uint32_t shdr_status, shdr_add_status;
  12005. union lpfc_sli4_cfg_shdr *shdr;
  12006. /* The number of sgls to be posted */
  12007. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  12008. reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12009. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12010. if (reqlen > SLI4_PAGE_SIZE) {
  12011. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12012. "2559 Block sgl registration required DMA "
  12013. "size (%d) great than a page\n", reqlen);
  12014. return -ENOMEM;
  12015. }
  12016. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12017. if (!mbox)
  12018. return -ENOMEM;
  12019. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12020. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12021. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12022. LPFC_SLI4_MBX_NEMBED);
  12023. if (alloclen < reqlen) {
  12024. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12025. "0285 Allocated DMA memory size (%d) is "
  12026. "less than the requested DMA memory "
  12027. "size (%d)\n", alloclen, reqlen);
  12028. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12029. return -ENOMEM;
  12030. }
  12031. /* Set up the SGL pages in the non-embedded DMA pages */
  12032. viraddr = mbox->sge_array->addr[0];
  12033. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12034. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12035. for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
  12036. sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
  12037. /*
  12038. * Assign the sglq a physical xri only if the driver has not
  12039. * initialized those resources. A port reset only needs
  12040. * the sglq's posted.
  12041. */
  12042. if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  12043. LPFC_XRI_RSRC_RDY) {
  12044. lxri = lpfc_sli4_next_xritag(phba);
  12045. if (lxri == NO_XRI) {
  12046. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12047. return -ENOMEM;
  12048. }
  12049. sglq_entry->sli4_lxritag = lxri;
  12050. sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  12051. }
  12052. /* Set up the sge entry */
  12053. sgl_pg_pairs->sgl_pg0_addr_lo =
  12054. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12055. sgl_pg_pairs->sgl_pg0_addr_hi =
  12056. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12057. sgl_pg_pairs->sgl_pg1_addr_lo =
  12058. cpu_to_le32(putPaddrLow(0));
  12059. sgl_pg_pairs->sgl_pg1_addr_hi =
  12060. cpu_to_le32(putPaddrHigh(0));
  12061. /* Keep the first xritag on the list */
  12062. if (pg_pairs == 0)
  12063. xritag_start = sglq_entry->sli4_xritag;
  12064. sgl_pg_pairs++;
  12065. }
  12066. /* Complete initialization and perform endian conversion. */
  12067. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12068. bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
  12069. sgl->word0 = cpu_to_le32(sgl->word0);
  12070. if (!phba->sli4_hba.intr_enable)
  12071. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12072. else {
  12073. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12074. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12075. }
  12076. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12077. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12078. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12079. if (rc != MBX_TIMEOUT)
  12080. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12081. if (shdr_status || shdr_add_status || rc) {
  12082. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12083. "2513 POST_SGL_BLOCK mailbox command failed "
  12084. "status x%x add_status x%x mbx status x%x\n",
  12085. shdr_status, shdr_add_status, rc);
  12086. rc = -ENXIO;
  12087. }
  12088. if (rc == 0)
  12089. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  12090. LPFC_XRI_RSRC_RDY);
  12091. return rc;
  12092. }
  12093. /**
  12094. * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
  12095. * @phba: pointer to lpfc hba data structure.
  12096. *
  12097. * This routine is invoked to post a block of driver's sgl pages to the
  12098. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12099. * is only called when the driver is loading and after all IO has been
  12100. * stopped.
  12101. **/
  12102. int
  12103. lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
  12104. {
  12105. struct lpfc_sglq *sglq_entry;
  12106. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12107. struct sgl_page_pairs *sgl_pg_pairs;
  12108. void *viraddr;
  12109. LPFC_MBOXQ_t *mbox;
  12110. uint32_t reqlen, alloclen, index;
  12111. uint32_t mbox_tmo;
  12112. uint16_t rsrc_start, rsrc_size, els_xri_cnt;
  12113. uint16_t xritag_start = 0, lxri = 0;
  12114. struct lpfc_rsrc_blks *rsrc_blk;
  12115. int cnt, ttl_cnt, rc = 0;
  12116. int loop_cnt;
  12117. uint32_t shdr_status, shdr_add_status;
  12118. union lpfc_sli4_cfg_shdr *shdr;
  12119. /* The number of sgls to be posted */
  12120. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  12121. reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12122. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12123. if (reqlen > SLI4_PAGE_SIZE) {
  12124. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12125. "2989 Block sgl registration required DMA "
  12126. "size (%d) great than a page\n", reqlen);
  12127. return -ENOMEM;
  12128. }
  12129. cnt = 0;
  12130. ttl_cnt = 0;
  12131. list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
  12132. list) {
  12133. rsrc_start = rsrc_blk->rsrc_start;
  12134. rsrc_size = rsrc_blk->rsrc_size;
  12135. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12136. "3014 Working ELS Extent start %d, cnt %d\n",
  12137. rsrc_start, rsrc_size);
  12138. loop_cnt = min(els_xri_cnt, rsrc_size);
  12139. if (ttl_cnt + loop_cnt >= els_xri_cnt) {
  12140. loop_cnt = els_xri_cnt - ttl_cnt;
  12141. ttl_cnt = els_xri_cnt;
  12142. }
  12143. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12144. if (!mbox)
  12145. return -ENOMEM;
  12146. /*
  12147. * Allocate DMA memory and set up the non-embedded mailbox
  12148. * command.
  12149. */
  12150. alloclen = lpfc_sli4_config(phba, mbox,
  12151. LPFC_MBOX_SUBSYSTEM_FCOE,
  12152. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12153. reqlen, LPFC_SLI4_MBX_NEMBED);
  12154. if (alloclen < reqlen) {
  12155. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12156. "2987 Allocated DMA memory size (%d) "
  12157. "is less than the requested DMA memory "
  12158. "size (%d)\n", alloclen, reqlen);
  12159. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12160. return -ENOMEM;
  12161. }
  12162. /* Set up the SGL pages in the non-embedded DMA pages */
  12163. viraddr = mbox->sge_array->addr[0];
  12164. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12165. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12166. /*
  12167. * The starting resource may not begin at zero. Control
  12168. * the loop variants via the block resource parameters,
  12169. * but handle the sge pointers with a zero-based index
  12170. * that doesn't get reset per loop pass.
  12171. */
  12172. for (index = rsrc_start;
  12173. index < rsrc_start + loop_cnt;
  12174. index++) {
  12175. sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
  12176. /*
  12177. * Assign the sglq a physical xri only if the driver
  12178. * has not initialized those resources. A port reset
  12179. * only needs the sglq's posted.
  12180. */
  12181. if (bf_get(lpfc_xri_rsrc_rdy,
  12182. &phba->sli4_hba.sli4_flags) !=
  12183. LPFC_XRI_RSRC_RDY) {
  12184. lxri = lpfc_sli4_next_xritag(phba);
  12185. if (lxri == NO_XRI) {
  12186. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12187. rc = -ENOMEM;
  12188. goto err_exit;
  12189. }
  12190. sglq_entry->sli4_lxritag = lxri;
  12191. sglq_entry->sli4_xritag =
  12192. phba->sli4_hba.xri_ids[lxri];
  12193. }
  12194. /* Set up the sge entry */
  12195. sgl_pg_pairs->sgl_pg0_addr_lo =
  12196. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12197. sgl_pg_pairs->sgl_pg0_addr_hi =
  12198. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12199. sgl_pg_pairs->sgl_pg1_addr_lo =
  12200. cpu_to_le32(putPaddrLow(0));
  12201. sgl_pg_pairs->sgl_pg1_addr_hi =
  12202. cpu_to_le32(putPaddrHigh(0));
  12203. /* Track the starting physical XRI for the mailbox. */
  12204. if (index == rsrc_start)
  12205. xritag_start = sglq_entry->sli4_xritag;
  12206. sgl_pg_pairs++;
  12207. cnt++;
  12208. }
  12209. /* Complete initialization and perform endian conversion. */
  12210. rsrc_blk->rsrc_used += loop_cnt;
  12211. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12212. bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
  12213. sgl->word0 = cpu_to_le32(sgl->word0);
  12214. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12215. "3015 Post ELS Extent SGL, start %d, "
  12216. "cnt %d, used %d\n",
  12217. xritag_start, loop_cnt, rsrc_blk->rsrc_used);
  12218. if (!phba->sli4_hba.intr_enable)
  12219. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12220. else {
  12221. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12222. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12223. }
  12224. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12225. shdr_status = bf_get(lpfc_mbox_hdr_status,
  12226. &shdr->response);
  12227. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  12228. &shdr->response);
  12229. if (rc != MBX_TIMEOUT)
  12230. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12231. if (shdr_status || shdr_add_status || rc) {
  12232. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12233. "2988 POST_SGL_BLOCK mailbox "
  12234. "command failed status x%x "
  12235. "add_status x%x mbx status x%x\n",
  12236. shdr_status, shdr_add_status, rc);
  12237. rc = -ENXIO;
  12238. goto err_exit;
  12239. }
  12240. if (ttl_cnt >= els_xri_cnt)
  12241. break;
  12242. }
  12243. err_exit:
  12244. if (rc == 0)
  12245. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  12246. LPFC_XRI_RSRC_RDY);
  12247. return rc;
  12248. }
  12249. /**
  12250. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12251. * @phba: pointer to lpfc hba data structure.
  12252. * @sblist: pointer to scsi buffer list.
  12253. * @count: number of scsi buffers on the list.
  12254. *
  12255. * This routine is invoked to post a block of @count scsi sgl pages from a
  12256. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12257. * No Lock is held.
  12258. *
  12259. **/
  12260. int
  12261. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
  12262. int cnt)
  12263. {
  12264. struct lpfc_scsi_buf *psb;
  12265. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12266. struct sgl_page_pairs *sgl_pg_pairs;
  12267. void *viraddr;
  12268. LPFC_MBOXQ_t *mbox;
  12269. uint32_t reqlen, alloclen, pg_pairs;
  12270. uint32_t mbox_tmo;
  12271. uint16_t xritag_start = 0;
  12272. int rc = 0;
  12273. uint32_t shdr_status, shdr_add_status;
  12274. dma_addr_t pdma_phys_bpl1;
  12275. union lpfc_sli4_cfg_shdr *shdr;
  12276. /* Calculate the requested length of the dma memory */
  12277. reqlen = cnt * sizeof(struct sgl_page_pairs) +
  12278. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12279. if (reqlen > SLI4_PAGE_SIZE) {
  12280. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12281. "0217 Block sgl registration required DMA "
  12282. "size (%d) great than a page\n", reqlen);
  12283. return -ENOMEM;
  12284. }
  12285. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12286. if (!mbox) {
  12287. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12288. "0283 Failed to allocate mbox cmd memory\n");
  12289. return -ENOMEM;
  12290. }
  12291. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12292. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12293. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12294. LPFC_SLI4_MBX_NEMBED);
  12295. if (alloclen < reqlen) {
  12296. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12297. "2561 Allocated DMA memory size (%d) is "
  12298. "less than the requested DMA memory "
  12299. "size (%d)\n", alloclen, reqlen);
  12300. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12301. return -ENOMEM;
  12302. }
  12303. /* Get the first SGE entry from the non-embedded DMA memory */
  12304. viraddr = mbox->sge_array->addr[0];
  12305. /* Set up the SGL pages in the non-embedded DMA pages */
  12306. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12307. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12308. pg_pairs = 0;
  12309. list_for_each_entry(psb, sblist, list) {
  12310. /* Set up the sge entry */
  12311. sgl_pg_pairs->sgl_pg0_addr_lo =
  12312. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12313. sgl_pg_pairs->sgl_pg0_addr_hi =
  12314. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12315. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12316. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12317. else
  12318. pdma_phys_bpl1 = 0;
  12319. sgl_pg_pairs->sgl_pg1_addr_lo =
  12320. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12321. sgl_pg_pairs->sgl_pg1_addr_hi =
  12322. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12323. /* Keep the first xritag on the list */
  12324. if (pg_pairs == 0)
  12325. xritag_start = psb->cur_iocbq.sli4_xritag;
  12326. sgl_pg_pairs++;
  12327. pg_pairs++;
  12328. }
  12329. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12330. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12331. /* Perform endian conversion if necessary */
  12332. sgl->word0 = cpu_to_le32(sgl->word0);
  12333. if (!phba->sli4_hba.intr_enable)
  12334. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12335. else {
  12336. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12337. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12338. }
  12339. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12340. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12341. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12342. if (rc != MBX_TIMEOUT)
  12343. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12344. if (shdr_status || shdr_add_status || rc) {
  12345. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12346. "2564 POST_SGL_BLOCK mailbox command failed "
  12347. "status x%x add_status x%x mbx status x%x\n",
  12348. shdr_status, shdr_add_status, rc);
  12349. rc = -ENXIO;
  12350. }
  12351. return rc;
  12352. }
  12353. /**
  12354. * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
  12355. * @phba: pointer to lpfc hba data structure.
  12356. * @sblist: pointer to scsi buffer list.
  12357. * @count: number of scsi buffers on the list.
  12358. *
  12359. * This routine is invoked to post a block of @count scsi sgl pages from a
  12360. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12361. * No Lock is held.
  12362. *
  12363. **/
  12364. int
  12365. lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
  12366. int cnt)
  12367. {
  12368. struct lpfc_scsi_buf *psb = NULL;
  12369. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12370. struct sgl_page_pairs *sgl_pg_pairs;
  12371. void *viraddr;
  12372. LPFC_MBOXQ_t *mbox;
  12373. uint32_t reqlen, alloclen, pg_pairs;
  12374. uint32_t mbox_tmo;
  12375. uint16_t xri_start = 0, scsi_xri_start;
  12376. uint16_t rsrc_range;
  12377. int rc = 0, avail_cnt;
  12378. uint32_t shdr_status, shdr_add_status;
  12379. dma_addr_t pdma_phys_bpl1;
  12380. union lpfc_sli4_cfg_shdr *shdr;
  12381. struct lpfc_rsrc_blks *rsrc_blk;
  12382. uint32_t xri_cnt = 0;
  12383. /* Calculate the total requested length of the dma memory */
  12384. reqlen = cnt * sizeof(struct sgl_page_pairs) +
  12385. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12386. if (reqlen > SLI4_PAGE_SIZE) {
  12387. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12388. "2932 Block sgl registration required DMA "
  12389. "size (%d) great than a page\n", reqlen);
  12390. return -ENOMEM;
  12391. }
  12392. /*
  12393. * The use of extents requires the driver to post the sgl headers
  12394. * in multiple postings to meet the contiguous resource assignment.
  12395. */
  12396. psb = list_prepare_entry(psb, sblist, list);
  12397. scsi_xri_start = phba->sli4_hba.scsi_xri_start;
  12398. list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
  12399. list) {
  12400. rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
  12401. if (rsrc_range < scsi_xri_start)
  12402. continue;
  12403. else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
  12404. continue;
  12405. else
  12406. avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
  12407. reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
  12408. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12409. /*
  12410. * Allocate DMA memory and set up the non-embedded mailbox
  12411. * command. The mbox is used to post an SGL page per loop
  12412. * but the DMA memory has a use-once semantic so the mailbox
  12413. * is used and freed per loop pass.
  12414. */
  12415. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12416. if (!mbox) {
  12417. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12418. "2933 Failed to allocate mbox cmd "
  12419. "memory\n");
  12420. return -ENOMEM;
  12421. }
  12422. alloclen = lpfc_sli4_config(phba, mbox,
  12423. LPFC_MBOX_SUBSYSTEM_FCOE,
  12424. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12425. reqlen,
  12426. LPFC_SLI4_MBX_NEMBED);
  12427. if (alloclen < reqlen) {
  12428. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12429. "2934 Allocated DMA memory size (%d) "
  12430. "is less than the requested DMA memory "
  12431. "size (%d)\n", alloclen, reqlen);
  12432. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12433. return -ENOMEM;
  12434. }
  12435. /* Get the first SGE entry from the non-embedded DMA memory */
  12436. viraddr = mbox->sge_array->addr[0];
  12437. /* Set up the SGL pages in the non-embedded DMA pages */
  12438. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12439. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12440. /* pg_pairs tracks posted SGEs per loop iteration. */
  12441. pg_pairs = 0;
  12442. list_for_each_entry_continue(psb, sblist, list) {
  12443. /* Set up the sge entry */
  12444. sgl_pg_pairs->sgl_pg0_addr_lo =
  12445. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12446. sgl_pg_pairs->sgl_pg0_addr_hi =
  12447. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12448. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12449. pdma_phys_bpl1 = psb->dma_phys_bpl +
  12450. SGL_PAGE_SIZE;
  12451. else
  12452. pdma_phys_bpl1 = 0;
  12453. sgl_pg_pairs->sgl_pg1_addr_lo =
  12454. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12455. sgl_pg_pairs->sgl_pg1_addr_hi =
  12456. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12457. /* Keep the first xri for this extent. */
  12458. if (pg_pairs == 0)
  12459. xri_start = psb->cur_iocbq.sli4_xritag;
  12460. sgl_pg_pairs++;
  12461. pg_pairs++;
  12462. xri_cnt++;
  12463. /*
  12464. * Track two exit conditions - the loop has constructed
  12465. * all of the caller's SGE pairs or all available
  12466. * resource IDs in this extent are consumed.
  12467. */
  12468. if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
  12469. break;
  12470. }
  12471. rsrc_blk->rsrc_used += pg_pairs;
  12472. bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
  12473. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12474. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12475. "3016 Post SCSI Extent SGL, start %d, cnt %d "
  12476. "blk use %d\n",
  12477. xri_start, pg_pairs, rsrc_blk->rsrc_used);
  12478. /* Perform endian conversion if necessary */
  12479. sgl->word0 = cpu_to_le32(sgl->word0);
  12480. if (!phba->sli4_hba.intr_enable)
  12481. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12482. else {
  12483. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12484. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12485. }
  12486. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12487. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12488. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  12489. &shdr->response);
  12490. if (rc != MBX_TIMEOUT)
  12491. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12492. if (shdr_status || shdr_add_status || rc) {
  12493. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12494. "2935 POST_SGL_BLOCK mailbox command "
  12495. "failed status x%x add_status x%x "
  12496. "mbx status x%x\n",
  12497. shdr_status, shdr_add_status, rc);
  12498. return -ENXIO;
  12499. }
  12500. /* Post only what is requested. */
  12501. if (xri_cnt >= cnt)
  12502. break;
  12503. }
  12504. return rc;
  12505. }
  12506. /**
  12507. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12508. * @phba: pointer to lpfc_hba struct that the frame was received on
  12509. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12510. *
  12511. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12512. * valid type of frame that the LPFC driver will handle. This function will
  12513. * return a zero if the frame is a valid frame or a non zero value when the
  12514. * frame does not pass the check.
  12515. **/
  12516. static int
  12517. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12518. {
  12519. /* make rctl_names static to save stack space */
  12520. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12521. char *type_names[] = FC_TYPE_NAMES_INIT;
  12522. struct fc_vft_header *fc_vft_hdr;
  12523. uint32_t *header = (uint32_t *) fc_hdr;
  12524. switch (fc_hdr->fh_r_ctl) {
  12525. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12526. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12527. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12528. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12529. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12530. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12531. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12532. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12533. case FC_RCTL_ELS_REQ: /* extended link services request */
  12534. case FC_RCTL_ELS_REP: /* extended link services reply */
  12535. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12536. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12537. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12538. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12539. case FC_RCTL_BA_RMC: /* remove connection */
  12540. case FC_RCTL_BA_ACC: /* basic accept */
  12541. case FC_RCTL_BA_RJT: /* basic reject */
  12542. case FC_RCTL_BA_PRMT:
  12543. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12544. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12545. case FC_RCTL_P_RJT: /* port reject */
  12546. case FC_RCTL_F_RJT: /* fabric reject */
  12547. case FC_RCTL_P_BSY: /* port busy */
  12548. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12549. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12550. case FC_RCTL_LCR: /* link credit reset */
  12551. case FC_RCTL_END: /* end */
  12552. break;
  12553. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12554. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12555. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12556. return lpfc_fc_frame_check(phba, fc_hdr);
  12557. default:
  12558. goto drop;
  12559. }
  12560. switch (fc_hdr->fh_type) {
  12561. case FC_TYPE_BLS:
  12562. case FC_TYPE_ELS:
  12563. case FC_TYPE_FCP:
  12564. case FC_TYPE_CT:
  12565. break;
  12566. case FC_TYPE_IP:
  12567. case FC_TYPE_ILS:
  12568. default:
  12569. goto drop;
  12570. }
  12571. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12572. "2538 Received frame rctl:%s type:%s "
  12573. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12574. rctl_names[fc_hdr->fh_r_ctl],
  12575. type_names[fc_hdr->fh_type],
  12576. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12577. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12578. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12579. return 0;
  12580. drop:
  12581. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12582. "2539 Dropped frame rctl:%s type:%s\n",
  12583. rctl_names[fc_hdr->fh_r_ctl],
  12584. type_names[fc_hdr->fh_type]);
  12585. return 1;
  12586. }
  12587. /**
  12588. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12589. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12590. *
  12591. * This function processes the FC header to retrieve the VFI from the VF
  12592. * header, if one exists. This function will return the VFI if one exists
  12593. * or 0 if no VSAN Header exists.
  12594. **/
  12595. static uint32_t
  12596. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12597. {
  12598. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12599. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12600. return 0;
  12601. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12602. }
  12603. /**
  12604. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12605. * @phba: Pointer to the HBA structure to search for the vport on
  12606. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12607. * @fcfi: The FC Fabric ID that the frame came from
  12608. *
  12609. * This function searches the @phba for a vport that matches the content of the
  12610. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12611. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12612. * returns the matching vport pointer or NULL if unable to match frame to a
  12613. * vport.
  12614. **/
  12615. static struct lpfc_vport *
  12616. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12617. uint16_t fcfi)
  12618. {
  12619. struct lpfc_vport **vports;
  12620. struct lpfc_vport *vport = NULL;
  12621. int i;
  12622. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12623. fc_hdr->fh_d_id[1] << 8 |
  12624. fc_hdr->fh_d_id[2]);
  12625. if (did == Fabric_DID)
  12626. return phba->pport;
  12627. vports = lpfc_create_vport_work_array(phba);
  12628. if (vports != NULL)
  12629. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12630. if (phba->fcf.fcfi == fcfi &&
  12631. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12632. vports[i]->fc_myDID == did) {
  12633. vport = vports[i];
  12634. break;
  12635. }
  12636. }
  12637. lpfc_destroy_vport_work_array(phba, vports);
  12638. return vport;
  12639. }
  12640. /**
  12641. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12642. * @vport: The vport to work on.
  12643. *
  12644. * This function updates the receive sequence time stamp for this vport. The
  12645. * receive sequence time stamp indicates the time that the last frame of the
  12646. * the sequence that has been idle for the longest amount of time was received.
  12647. * the driver uses this time stamp to indicate if any received sequences have
  12648. * timed out.
  12649. **/
  12650. void
  12651. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12652. {
  12653. struct lpfc_dmabuf *h_buf;
  12654. struct hbq_dmabuf *dmabuf = NULL;
  12655. /* get the oldest sequence on the rcv list */
  12656. h_buf = list_get_first(&vport->rcv_buffer_list,
  12657. struct lpfc_dmabuf, list);
  12658. if (!h_buf)
  12659. return;
  12660. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12661. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12662. }
  12663. /**
  12664. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12665. * @vport: The vport that the received sequences were sent to.
  12666. *
  12667. * This function cleans up all outstanding received sequences. This is called
  12668. * by the driver when a link event or user action invalidates all the received
  12669. * sequences.
  12670. **/
  12671. void
  12672. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12673. {
  12674. struct lpfc_dmabuf *h_buf, *hnext;
  12675. struct lpfc_dmabuf *d_buf, *dnext;
  12676. struct hbq_dmabuf *dmabuf = NULL;
  12677. /* start with the oldest sequence on the rcv list */
  12678. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12679. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12680. list_del_init(&dmabuf->hbuf.list);
  12681. list_for_each_entry_safe(d_buf, dnext,
  12682. &dmabuf->dbuf.list, list) {
  12683. list_del_init(&d_buf->list);
  12684. lpfc_in_buf_free(vport->phba, d_buf);
  12685. }
  12686. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12687. }
  12688. }
  12689. /**
  12690. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12691. * @vport: The vport that the received sequences were sent to.
  12692. *
  12693. * This function determines whether any received sequences have timed out by
  12694. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12695. * indicates that there is at least one timed out sequence this routine will
  12696. * go through the received sequences one at a time from most inactive to most
  12697. * active to determine which ones need to be cleaned up. Once it has determined
  12698. * that a sequence needs to be cleaned up it will simply free up the resources
  12699. * without sending an abort.
  12700. **/
  12701. void
  12702. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12703. {
  12704. struct lpfc_dmabuf *h_buf, *hnext;
  12705. struct lpfc_dmabuf *d_buf, *dnext;
  12706. struct hbq_dmabuf *dmabuf = NULL;
  12707. unsigned long timeout;
  12708. int abort_count = 0;
  12709. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12710. vport->rcv_buffer_time_stamp);
  12711. if (list_empty(&vport->rcv_buffer_list) ||
  12712. time_before(jiffies, timeout))
  12713. return;
  12714. /* start with the oldest sequence on the rcv list */
  12715. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12716. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12717. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12718. dmabuf->time_stamp);
  12719. if (time_before(jiffies, timeout))
  12720. break;
  12721. abort_count++;
  12722. list_del_init(&dmabuf->hbuf.list);
  12723. list_for_each_entry_safe(d_buf, dnext,
  12724. &dmabuf->dbuf.list, list) {
  12725. list_del_init(&d_buf->list);
  12726. lpfc_in_buf_free(vport->phba, d_buf);
  12727. }
  12728. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12729. }
  12730. if (abort_count)
  12731. lpfc_update_rcv_time_stamp(vport);
  12732. }
  12733. /**
  12734. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12735. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12736. *
  12737. * This function searches through the existing incomplete sequences that have
  12738. * been sent to this @vport. If the frame matches one of the incomplete
  12739. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12740. * make up that sequence. If no sequence is found that matches this frame then
  12741. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12742. * This function returns a pointer to the first dmabuf in the sequence list that
  12743. * the frame was linked to.
  12744. **/
  12745. static struct hbq_dmabuf *
  12746. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12747. {
  12748. struct fc_frame_header *new_hdr;
  12749. struct fc_frame_header *temp_hdr;
  12750. struct lpfc_dmabuf *d_buf;
  12751. struct lpfc_dmabuf *h_buf;
  12752. struct hbq_dmabuf *seq_dmabuf = NULL;
  12753. struct hbq_dmabuf *temp_dmabuf = NULL;
  12754. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12755. dmabuf->time_stamp = jiffies;
  12756. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12757. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12758. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12759. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12760. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12761. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12762. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12763. continue;
  12764. /* found a pending sequence that matches this frame */
  12765. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12766. break;
  12767. }
  12768. if (!seq_dmabuf) {
  12769. /*
  12770. * This indicates first frame received for this sequence.
  12771. * Queue the buffer on the vport's rcv_buffer_list.
  12772. */
  12773. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12774. lpfc_update_rcv_time_stamp(vport);
  12775. return dmabuf;
  12776. }
  12777. temp_hdr = seq_dmabuf->hbuf.virt;
  12778. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12779. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12780. list_del_init(&seq_dmabuf->hbuf.list);
  12781. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12782. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12783. lpfc_update_rcv_time_stamp(vport);
  12784. return dmabuf;
  12785. }
  12786. /* move this sequence to the tail to indicate a young sequence */
  12787. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12788. seq_dmabuf->time_stamp = jiffies;
  12789. lpfc_update_rcv_time_stamp(vport);
  12790. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12791. temp_hdr = dmabuf->hbuf.virt;
  12792. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12793. return seq_dmabuf;
  12794. }
  12795. /* find the correct place in the sequence to insert this frame */
  12796. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12797. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12798. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12799. /*
  12800. * If the frame's sequence count is greater than the frame on
  12801. * the list then insert the frame right after this frame
  12802. */
  12803. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  12804. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12805. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  12806. return seq_dmabuf;
  12807. }
  12808. }
  12809. return NULL;
  12810. }
  12811. /**
  12812. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  12813. * @vport: pointer to a vitural port
  12814. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12815. *
  12816. * This function tries to abort from the partially assembed sequence, described
  12817. * by the information from basic abbort @dmabuf. It checks to see whether such
  12818. * partially assembled sequence held by the driver. If so, it shall free up all
  12819. * the frames from the partially assembled sequence.
  12820. *
  12821. * Return
  12822. * true -- if there is matching partially assembled sequence present and all
  12823. * the frames freed with the sequence;
  12824. * false -- if there is no matching partially assembled sequence present so
  12825. * nothing got aborted in the lower layer driver
  12826. **/
  12827. static bool
  12828. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  12829. struct hbq_dmabuf *dmabuf)
  12830. {
  12831. struct fc_frame_header *new_hdr;
  12832. struct fc_frame_header *temp_hdr;
  12833. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  12834. struct hbq_dmabuf *seq_dmabuf = NULL;
  12835. /* Use the hdr_buf to find the sequence that matches this frame */
  12836. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12837. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  12838. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12839. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12840. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12841. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12842. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12843. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12844. continue;
  12845. /* found a pending sequence that matches this frame */
  12846. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12847. break;
  12848. }
  12849. /* Free up all the frames from the partially assembled sequence */
  12850. if (seq_dmabuf) {
  12851. list_for_each_entry_safe(d_buf, n_buf,
  12852. &seq_dmabuf->dbuf.list, list) {
  12853. list_del_init(&d_buf->list);
  12854. lpfc_in_buf_free(vport->phba, d_buf);
  12855. }
  12856. return true;
  12857. }
  12858. return false;
  12859. }
  12860. /**
  12861. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  12862. * @phba: Pointer to HBA context object.
  12863. * @cmd_iocbq: pointer to the command iocbq structure.
  12864. * @rsp_iocbq: pointer to the response iocbq structure.
  12865. *
  12866. * This function handles the sequence abort response iocb command complete
  12867. * event. It properly releases the memory allocated to the sequence abort
  12868. * accept iocb.
  12869. **/
  12870. static void
  12871. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  12872. struct lpfc_iocbq *cmd_iocbq,
  12873. struct lpfc_iocbq *rsp_iocbq)
  12874. {
  12875. if (cmd_iocbq)
  12876. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  12877. }
  12878. /**
  12879. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  12880. * @phba: Pointer to HBA context object.
  12881. * @xri: xri id in transaction.
  12882. *
  12883. * This function validates the xri maps to the known range of XRIs allocated an
  12884. * used by the driver.
  12885. **/
  12886. uint16_t
  12887. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  12888. uint16_t xri)
  12889. {
  12890. int i;
  12891. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  12892. if (xri == phba->sli4_hba.xri_ids[i])
  12893. return i;
  12894. }
  12895. return NO_XRI;
  12896. }
  12897. /**
  12898. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  12899. * @phba: Pointer to HBA context object.
  12900. * @fc_hdr: pointer to a FC frame header.
  12901. *
  12902. * This function sends a basic response to a previous unsol sequence abort
  12903. * event after aborting the sequence handling.
  12904. **/
  12905. static void
  12906. lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
  12907. struct fc_frame_header *fc_hdr)
  12908. {
  12909. struct lpfc_iocbq *ctiocb = NULL;
  12910. struct lpfc_nodelist *ndlp;
  12911. uint16_t oxid, rxid;
  12912. uint32_t sid, fctl;
  12913. IOCB_t *icmd;
  12914. int rc;
  12915. if (!lpfc_is_link_up(phba))
  12916. return;
  12917. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12918. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  12919. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  12920. ndlp = lpfc_findnode_did(phba->pport, sid);
  12921. if (!ndlp) {
  12922. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12923. "1268 Find ndlp returned NULL for oxid:x%x "
  12924. "SID:x%x\n", oxid, sid);
  12925. return;
  12926. }
  12927. if (lpfc_sli4_xri_inrange(phba, rxid))
  12928. lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
  12929. /* Allocate buffer for rsp iocb */
  12930. ctiocb = lpfc_sli_get_iocbq(phba);
  12931. if (!ctiocb)
  12932. return;
  12933. /* Extract the F_CTL field from FC_HDR */
  12934. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  12935. icmd = &ctiocb->iocb;
  12936. icmd->un.xseq64.bdl.bdeSize = 0;
  12937. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  12938. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  12939. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  12940. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  12941. /* Fill in the rest of iocb fields */
  12942. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  12943. icmd->ulpBdeCount = 0;
  12944. icmd->ulpLe = 1;
  12945. icmd->ulpClass = CLASS3;
  12946. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  12947. ctiocb->context1 = ndlp;
  12948. ctiocb->iocb_cmpl = NULL;
  12949. ctiocb->vport = phba->pport;
  12950. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  12951. ctiocb->sli4_lxritag = NO_XRI;
  12952. ctiocb->sli4_xritag = NO_XRI;
  12953. /* If the oxid maps to the FCP XRI range or if it is out of range,
  12954. * send a BLS_RJT. The driver no longer has that exchange.
  12955. * Override the IOCB for a BA_RJT.
  12956. */
  12957. if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
  12958. phba->sli4_hba.max_cfg_param.xri_base) ||
  12959. oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
  12960. phba->sli4_hba.max_cfg_param.xri_base)) {
  12961. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  12962. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  12963. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  12964. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  12965. }
  12966. if (fctl & FC_FC_EX_CTX) {
  12967. /* ABTS sent by responder to CT exchange, construction
  12968. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  12969. * field and RX_ID from ABTS for RX_ID field.
  12970. */
  12971. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  12972. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  12973. } else {
  12974. /* ABTS sent by initiator to CT exchange, construction
  12975. * of BA_ACC will need to allocate a new XRI as for the
  12976. * XRI_TAG and RX_ID fields.
  12977. */
  12978. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  12979. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
  12980. }
  12981. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  12982. /* Xmit CT abts response on exchange <xid> */
  12983. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12984. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  12985. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  12986. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  12987. if (rc == IOCB_ERROR) {
  12988. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  12989. "2925 Failed to issue CT ABTS RSP x%x on "
  12990. "xri x%x, Data x%x\n",
  12991. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  12992. phba->link_state);
  12993. lpfc_sli_release_iocbq(phba, ctiocb);
  12994. }
  12995. }
  12996. /**
  12997. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  12998. * @vport: Pointer to the vport on which this sequence was received
  12999. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13000. *
  13001. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  13002. * receive sequence is only partially assembed by the driver, it shall abort
  13003. * the partially assembled frames for the sequence. Otherwise, if the
  13004. * unsolicited receive sequence has been completely assembled and passed to
  13005. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  13006. * unsolicited sequence has been aborted. After that, it will issue a basic
  13007. * accept to accept the abort.
  13008. **/
  13009. void
  13010. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  13011. struct hbq_dmabuf *dmabuf)
  13012. {
  13013. struct lpfc_hba *phba = vport->phba;
  13014. struct fc_frame_header fc_hdr;
  13015. uint32_t fctl;
  13016. bool abts_par;
  13017. /* Make a copy of fc_hdr before the dmabuf being released */
  13018. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  13019. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  13020. if (fctl & FC_FC_EX_CTX) {
  13021. /*
  13022. * ABTS sent by responder to exchange, just free the buffer
  13023. */
  13024. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13025. } else {
  13026. /*
  13027. * ABTS sent by initiator to exchange, need to do cleanup
  13028. */
  13029. /* Try to abort partially assembled seq */
  13030. abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  13031. /* Send abort to ULP if partially seq abort failed */
  13032. if (abts_par == false)
  13033. lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
  13034. else
  13035. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13036. }
  13037. /* Send basic accept (BA_ACC) to the abort requester */
  13038. lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
  13039. }
  13040. /**
  13041. * lpfc_seq_complete - Indicates if a sequence is complete
  13042. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13043. *
  13044. * This function checks the sequence, starting with the frame described by
  13045. * @dmabuf, to see if all the frames associated with this sequence are present.
  13046. * the frames associated with this sequence are linked to the @dmabuf using the
  13047. * dbuf list. This function looks for two major things. 1) That the first frame
  13048. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  13049. * set. 3) That there are no holes in the sequence count. The function will
  13050. * return 1 when the sequence is complete, otherwise it will return 0.
  13051. **/
  13052. static int
  13053. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  13054. {
  13055. struct fc_frame_header *hdr;
  13056. struct lpfc_dmabuf *d_buf;
  13057. struct hbq_dmabuf *seq_dmabuf;
  13058. uint32_t fctl;
  13059. int seq_count = 0;
  13060. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13061. /* make sure first fame of sequence has a sequence count of zero */
  13062. if (hdr->fh_seq_cnt != seq_count)
  13063. return 0;
  13064. fctl = (hdr->fh_f_ctl[0] << 16 |
  13065. hdr->fh_f_ctl[1] << 8 |
  13066. hdr->fh_f_ctl[2]);
  13067. /* If last frame of sequence we can return success. */
  13068. if (fctl & FC_FC_END_SEQ)
  13069. return 1;
  13070. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  13071. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13072. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13073. /* If there is a hole in the sequence count then fail. */
  13074. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  13075. return 0;
  13076. fctl = (hdr->fh_f_ctl[0] << 16 |
  13077. hdr->fh_f_ctl[1] << 8 |
  13078. hdr->fh_f_ctl[2]);
  13079. /* If last frame of sequence we can return success. */
  13080. if (fctl & FC_FC_END_SEQ)
  13081. return 1;
  13082. }
  13083. return 0;
  13084. }
  13085. /**
  13086. * lpfc_prep_seq - Prep sequence for ULP processing
  13087. * @vport: Pointer to the vport on which this sequence was received
  13088. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13089. *
  13090. * This function takes a sequence, described by a list of frames, and creates
  13091. * a list of iocbq structures to describe the sequence. This iocbq list will be
  13092. * used to issue to the generic unsolicited sequence handler. This routine
  13093. * returns a pointer to the first iocbq in the list. If the function is unable
  13094. * to allocate an iocbq then it throw out the received frames that were not
  13095. * able to be described and return a pointer to the first iocbq. If unable to
  13096. * allocate any iocbqs (including the first) this function will return NULL.
  13097. **/
  13098. static struct lpfc_iocbq *
  13099. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  13100. {
  13101. struct hbq_dmabuf *hbq_buf;
  13102. struct lpfc_dmabuf *d_buf, *n_buf;
  13103. struct lpfc_iocbq *first_iocbq, *iocbq;
  13104. struct fc_frame_header *fc_hdr;
  13105. uint32_t sid;
  13106. uint32_t len, tot_len;
  13107. struct ulp_bde64 *pbde;
  13108. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13109. /* remove from receive buffer list */
  13110. list_del_init(&seq_dmabuf->hbuf.list);
  13111. lpfc_update_rcv_time_stamp(vport);
  13112. /* get the Remote Port's SID */
  13113. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13114. tot_len = 0;
  13115. /* Get an iocbq struct to fill in. */
  13116. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  13117. if (first_iocbq) {
  13118. /* Initialize the first IOCB. */
  13119. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  13120. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  13121. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  13122. first_iocbq->iocb.ulpContext = NO_XRI;
  13123. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  13124. be16_to_cpu(fc_hdr->fh_ox_id);
  13125. /* iocbq is prepped for internal consumption. Physical vpi. */
  13126. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  13127. vport->phba->vpi_ids[vport->vpi];
  13128. /* put the first buffer into the first IOCBq */
  13129. first_iocbq->context2 = &seq_dmabuf->dbuf;
  13130. first_iocbq->context3 = NULL;
  13131. first_iocbq->iocb.ulpBdeCount = 1;
  13132. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13133. LPFC_DATA_BUF_SIZE;
  13134. first_iocbq->iocb.un.rcvels.remoteID = sid;
  13135. tot_len = bf_get(lpfc_rcqe_length,
  13136. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  13137. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13138. }
  13139. iocbq = first_iocbq;
  13140. /*
  13141. * Each IOCBq can have two Buffers assigned, so go through the list
  13142. * of buffers for this sequence and save two buffers in each IOCBq
  13143. */
  13144. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13145. if (!iocbq) {
  13146. lpfc_in_buf_free(vport->phba, d_buf);
  13147. continue;
  13148. }
  13149. if (!iocbq->context3) {
  13150. iocbq->context3 = d_buf;
  13151. iocbq->iocb.ulpBdeCount++;
  13152. pbde = (struct ulp_bde64 *)
  13153. &iocbq->iocb.unsli3.sli3Words[4];
  13154. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13155. /* We need to get the size out of the right CQE */
  13156. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13157. len = bf_get(lpfc_rcqe_length,
  13158. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13159. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13160. tot_len += len;
  13161. } else {
  13162. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13163. if (!iocbq) {
  13164. if (first_iocbq) {
  13165. first_iocbq->iocb.ulpStatus =
  13166. IOSTAT_FCP_RSP_ERROR;
  13167. first_iocbq->iocb.un.ulpWord[4] =
  13168. IOERR_NO_RESOURCES;
  13169. }
  13170. lpfc_in_buf_free(vport->phba, d_buf);
  13171. continue;
  13172. }
  13173. iocbq->context2 = d_buf;
  13174. iocbq->context3 = NULL;
  13175. iocbq->iocb.ulpBdeCount = 1;
  13176. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13177. LPFC_DATA_BUF_SIZE;
  13178. /* We need to get the size out of the right CQE */
  13179. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13180. len = bf_get(lpfc_rcqe_length,
  13181. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13182. tot_len += len;
  13183. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13184. iocbq->iocb.un.rcvels.remoteID = sid;
  13185. list_add_tail(&iocbq->list, &first_iocbq->list);
  13186. }
  13187. }
  13188. return first_iocbq;
  13189. }
  13190. static void
  13191. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13192. struct hbq_dmabuf *seq_dmabuf)
  13193. {
  13194. struct fc_frame_header *fc_hdr;
  13195. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13196. struct lpfc_hba *phba = vport->phba;
  13197. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13198. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13199. if (!iocbq) {
  13200. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13201. "2707 Ring %d handler: Failed to allocate "
  13202. "iocb Rctl x%x Type x%x received\n",
  13203. LPFC_ELS_RING,
  13204. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13205. return;
  13206. }
  13207. if (!lpfc_complete_unsol_iocb(phba,
  13208. &phba->sli.ring[LPFC_ELS_RING],
  13209. iocbq, fc_hdr->fh_r_ctl,
  13210. fc_hdr->fh_type))
  13211. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13212. "2540 Ring %d handler: unexpected Rctl "
  13213. "x%x Type x%x received\n",
  13214. LPFC_ELS_RING,
  13215. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13216. /* Free iocb created in lpfc_prep_seq */
  13217. list_for_each_entry_safe(curr_iocb, next_iocb,
  13218. &iocbq->list, list) {
  13219. list_del_init(&curr_iocb->list);
  13220. lpfc_sli_release_iocbq(phba, curr_iocb);
  13221. }
  13222. lpfc_sli_release_iocbq(phba, iocbq);
  13223. }
  13224. /**
  13225. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13226. * @phba: Pointer to HBA context object.
  13227. *
  13228. * This function is called with no lock held. This function processes all
  13229. * the received buffers and gives it to upper layers when a received buffer
  13230. * indicates that it is the final frame in the sequence. The interrupt
  13231. * service routine processes received buffers at interrupt contexts and adds
  13232. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13233. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13234. * appropriate receive function when the final frame in a sequence is received.
  13235. **/
  13236. void
  13237. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13238. struct hbq_dmabuf *dmabuf)
  13239. {
  13240. struct hbq_dmabuf *seq_dmabuf;
  13241. struct fc_frame_header *fc_hdr;
  13242. struct lpfc_vport *vport;
  13243. uint32_t fcfi;
  13244. /* Process each received buffer */
  13245. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13246. /* check to see if this a valid type of frame */
  13247. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13248. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13249. return;
  13250. }
  13251. if ((bf_get(lpfc_cqe_code,
  13252. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13253. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13254. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13255. else
  13256. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13257. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13258. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13259. if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
  13260. /* throw out the frame */
  13261. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13262. return;
  13263. }
  13264. /* Handle the basic abort sequence (BA_ABTS) event */
  13265. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13266. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13267. return;
  13268. }
  13269. /* Link this frame */
  13270. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13271. if (!seq_dmabuf) {
  13272. /* unable to add frame to vport - throw it out */
  13273. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13274. return;
  13275. }
  13276. /* If not last frame in sequence continue processing frames. */
  13277. if (!lpfc_seq_complete(seq_dmabuf))
  13278. return;
  13279. /* Send the complete sequence to the upper layer protocol */
  13280. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13281. }
  13282. /**
  13283. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13284. * @phba: pointer to lpfc hba data structure.
  13285. *
  13286. * This routine is invoked to post rpi header templates to the
  13287. * HBA consistent with the SLI-4 interface spec. This routine
  13288. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13289. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13290. *
  13291. * This routine does not require any locks. It's usage is expected
  13292. * to be driver load or reset recovery when the driver is
  13293. * sequential.
  13294. *
  13295. * Return codes
  13296. * 0 - successful
  13297. * -EIO - The mailbox failed to complete successfully.
  13298. * When this error occurs, the driver is not guaranteed
  13299. * to have any rpi regions posted to the device and
  13300. * must either attempt to repost the regions or take a
  13301. * fatal error.
  13302. **/
  13303. int
  13304. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13305. {
  13306. struct lpfc_rpi_hdr *rpi_page;
  13307. uint32_t rc = 0;
  13308. uint16_t lrpi = 0;
  13309. /* SLI4 ports that support extents do not require RPI headers. */
  13310. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13311. goto exit;
  13312. if (phba->sli4_hba.extents_in_use)
  13313. return -EIO;
  13314. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13315. /*
  13316. * Assign the rpi headers a physical rpi only if the driver
  13317. * has not initialized those resources. A port reset only
  13318. * needs the headers posted.
  13319. */
  13320. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13321. LPFC_RPI_RSRC_RDY)
  13322. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13323. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13324. if (rc != MBX_SUCCESS) {
  13325. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13326. "2008 Error %d posting all rpi "
  13327. "headers\n", rc);
  13328. rc = -EIO;
  13329. break;
  13330. }
  13331. }
  13332. exit:
  13333. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13334. LPFC_RPI_RSRC_RDY);
  13335. return rc;
  13336. }
  13337. /**
  13338. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13339. * @phba: pointer to lpfc hba data structure.
  13340. * @rpi_page: pointer to the rpi memory region.
  13341. *
  13342. * This routine is invoked to post a single rpi header to the
  13343. * HBA consistent with the SLI-4 interface spec. This memory region
  13344. * maps up to 64 rpi context regions.
  13345. *
  13346. * Return codes
  13347. * 0 - successful
  13348. * -ENOMEM - No available memory
  13349. * -EIO - The mailbox failed to complete successfully.
  13350. **/
  13351. int
  13352. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13353. {
  13354. LPFC_MBOXQ_t *mboxq;
  13355. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13356. uint32_t rc = 0;
  13357. uint32_t shdr_status, shdr_add_status;
  13358. union lpfc_sli4_cfg_shdr *shdr;
  13359. /* SLI4 ports that support extents do not require RPI headers. */
  13360. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13361. return rc;
  13362. if (phba->sli4_hba.extents_in_use)
  13363. return -EIO;
  13364. /* The port is notified of the header region via a mailbox command. */
  13365. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13366. if (!mboxq) {
  13367. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13368. "2001 Unable to allocate memory for issuing "
  13369. "SLI_CONFIG_SPECIAL mailbox command\n");
  13370. return -ENOMEM;
  13371. }
  13372. /* Post all rpi memory regions to the port. */
  13373. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13374. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13375. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13376. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13377. sizeof(struct lpfc_sli4_cfg_mhdr),
  13378. LPFC_SLI4_MBX_EMBED);
  13379. /* Post the physical rpi to the port for this rpi header. */
  13380. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13381. rpi_page->start_rpi);
  13382. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13383. hdr_tmpl, rpi_page->page_count);
  13384. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13385. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13386. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13387. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13388. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13389. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13390. if (rc != MBX_TIMEOUT)
  13391. mempool_free(mboxq, phba->mbox_mem_pool);
  13392. if (shdr_status || shdr_add_status || rc) {
  13393. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13394. "2514 POST_RPI_HDR mailbox failed with "
  13395. "status x%x add_status x%x, mbx status x%x\n",
  13396. shdr_status, shdr_add_status, rc);
  13397. rc = -ENXIO;
  13398. }
  13399. return rc;
  13400. }
  13401. /**
  13402. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13403. * @phba: pointer to lpfc hba data structure.
  13404. *
  13405. * This routine is invoked to post rpi header templates to the
  13406. * HBA consistent with the SLI-4 interface spec. This routine
  13407. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13408. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13409. *
  13410. * Returns
  13411. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13412. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13413. **/
  13414. int
  13415. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13416. {
  13417. unsigned long rpi;
  13418. uint16_t max_rpi, rpi_limit;
  13419. uint16_t rpi_remaining, lrpi = 0;
  13420. struct lpfc_rpi_hdr *rpi_hdr;
  13421. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13422. rpi_limit = phba->sli4_hba.next_rpi;
  13423. /*
  13424. * Fetch the next logical rpi. Because this index is logical,
  13425. * the driver starts at 0 each time.
  13426. */
  13427. spin_lock_irq(&phba->hbalock);
  13428. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13429. if (rpi >= rpi_limit)
  13430. rpi = LPFC_RPI_ALLOC_ERROR;
  13431. else {
  13432. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13433. phba->sli4_hba.max_cfg_param.rpi_used++;
  13434. phba->sli4_hba.rpi_count++;
  13435. }
  13436. /*
  13437. * Don't try to allocate more rpi header regions if the device limit
  13438. * has been exhausted.
  13439. */
  13440. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13441. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13442. spin_unlock_irq(&phba->hbalock);
  13443. return rpi;
  13444. }
  13445. /*
  13446. * RPI header postings are not required for SLI4 ports capable of
  13447. * extents.
  13448. */
  13449. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13450. spin_unlock_irq(&phba->hbalock);
  13451. return rpi;
  13452. }
  13453. /*
  13454. * If the driver is running low on rpi resources, allocate another
  13455. * page now. Note that the next_rpi value is used because
  13456. * it represents how many are actually in use whereas max_rpi notes
  13457. * how many are supported max by the device.
  13458. */
  13459. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13460. spin_unlock_irq(&phba->hbalock);
  13461. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13462. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13463. if (!rpi_hdr) {
  13464. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13465. "2002 Error Could not grow rpi "
  13466. "count\n");
  13467. } else {
  13468. lrpi = rpi_hdr->start_rpi;
  13469. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13470. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13471. }
  13472. }
  13473. return rpi;
  13474. }
  13475. /**
  13476. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13477. * @phba: pointer to lpfc hba data structure.
  13478. *
  13479. * This routine is invoked to release an rpi to the pool of
  13480. * available rpis maintained by the driver.
  13481. **/
  13482. void
  13483. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13484. {
  13485. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13486. phba->sli4_hba.rpi_count--;
  13487. phba->sli4_hba.max_cfg_param.rpi_used--;
  13488. }
  13489. }
  13490. /**
  13491. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13492. * @phba: pointer to lpfc hba data structure.
  13493. *
  13494. * This routine is invoked to release an rpi to the pool of
  13495. * available rpis maintained by the driver.
  13496. **/
  13497. void
  13498. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13499. {
  13500. spin_lock_irq(&phba->hbalock);
  13501. __lpfc_sli4_free_rpi(phba, rpi);
  13502. spin_unlock_irq(&phba->hbalock);
  13503. }
  13504. /**
  13505. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13506. * @phba: pointer to lpfc hba data structure.
  13507. *
  13508. * This routine is invoked to remove the memory region that
  13509. * provided rpi via a bitmask.
  13510. **/
  13511. void
  13512. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13513. {
  13514. kfree(phba->sli4_hba.rpi_bmask);
  13515. kfree(phba->sli4_hba.rpi_ids);
  13516. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13517. }
  13518. /**
  13519. * lpfc_sli4_resume_rpi - 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. int
  13526. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
  13527. {
  13528. LPFC_MBOXQ_t *mboxq;
  13529. struct lpfc_hba *phba = ndlp->phba;
  13530. int rc;
  13531. /* The port is notified of the header region via a mailbox command. */
  13532. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13533. if (!mboxq)
  13534. return -ENOMEM;
  13535. /* Post all rpi memory regions to the port. */
  13536. lpfc_resume_rpi(mboxq, ndlp);
  13537. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13538. if (rc == MBX_NOT_FINISHED) {
  13539. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13540. "2010 Resume RPI Mailbox failed "
  13541. "status %d, mbxStatus x%x\n", rc,
  13542. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13543. mempool_free(mboxq, phba->mbox_mem_pool);
  13544. return -EIO;
  13545. }
  13546. return 0;
  13547. }
  13548. /**
  13549. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13550. * @vport: Pointer to the vport for which the vpi is being initialized
  13551. *
  13552. * This routine is invoked to activate a vpi with the port.
  13553. *
  13554. * Returns:
  13555. * 0 success
  13556. * -Evalue otherwise
  13557. **/
  13558. int
  13559. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13560. {
  13561. LPFC_MBOXQ_t *mboxq;
  13562. int rc = 0;
  13563. int retval = MBX_SUCCESS;
  13564. uint32_t mbox_tmo;
  13565. struct lpfc_hba *phba = vport->phba;
  13566. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13567. if (!mboxq)
  13568. return -ENOMEM;
  13569. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13570. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13571. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13572. if (rc != MBX_SUCCESS) {
  13573. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13574. "2022 INIT VPI Mailbox failed "
  13575. "status %d, mbxStatus x%x\n", rc,
  13576. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13577. retval = -EIO;
  13578. }
  13579. if (rc != MBX_TIMEOUT)
  13580. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13581. return retval;
  13582. }
  13583. /**
  13584. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13585. * @phba: pointer to lpfc hba data structure.
  13586. * @mboxq: Pointer to mailbox object.
  13587. *
  13588. * This routine is invoked to manually add a single FCF record. The caller
  13589. * must pass a completely initialized FCF_Record. This routine takes
  13590. * care of the nonembedded mailbox operations.
  13591. **/
  13592. static void
  13593. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13594. {
  13595. void *virt_addr;
  13596. union lpfc_sli4_cfg_shdr *shdr;
  13597. uint32_t shdr_status, shdr_add_status;
  13598. virt_addr = mboxq->sge_array->addr[0];
  13599. /* The IOCTL status is embedded in the mailbox subheader. */
  13600. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13601. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13602. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13603. if ((shdr_status || shdr_add_status) &&
  13604. (shdr_status != STATUS_FCF_IN_USE))
  13605. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13606. "2558 ADD_FCF_RECORD mailbox failed with "
  13607. "status x%x add_status x%x\n",
  13608. shdr_status, shdr_add_status);
  13609. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13610. }
  13611. /**
  13612. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13613. * @phba: pointer to lpfc hba data structure.
  13614. * @fcf_record: pointer to the initialized fcf record to add.
  13615. *
  13616. * This routine is invoked to manually add a single FCF record. The caller
  13617. * must pass a completely initialized FCF_Record. This routine takes
  13618. * care of the nonembedded mailbox operations.
  13619. **/
  13620. int
  13621. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13622. {
  13623. int rc = 0;
  13624. LPFC_MBOXQ_t *mboxq;
  13625. uint8_t *bytep;
  13626. void *virt_addr;
  13627. dma_addr_t phys_addr;
  13628. struct lpfc_mbx_sge sge;
  13629. uint32_t alloc_len, req_len;
  13630. uint32_t fcfindex;
  13631. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13632. if (!mboxq) {
  13633. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13634. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13635. return -ENOMEM;
  13636. }
  13637. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13638. sizeof(uint32_t);
  13639. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13640. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13641. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13642. req_len, LPFC_SLI4_MBX_NEMBED);
  13643. if (alloc_len < req_len) {
  13644. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13645. "2523 Allocated DMA memory size (x%x) is "
  13646. "less than the requested DMA memory "
  13647. "size (x%x)\n", alloc_len, req_len);
  13648. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13649. return -ENOMEM;
  13650. }
  13651. /*
  13652. * Get the first SGE entry from the non-embedded DMA memory. This
  13653. * routine only uses a single SGE.
  13654. */
  13655. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13656. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13657. virt_addr = mboxq->sge_array->addr[0];
  13658. /*
  13659. * Configure the FCF record for FCFI 0. This is the driver's
  13660. * hardcoded default and gets used in nonFIP mode.
  13661. */
  13662. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13663. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13664. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13665. /*
  13666. * Copy the fcf_index and the FCF Record Data. The data starts after
  13667. * the FCoE header plus word10. The data copy needs to be endian
  13668. * correct.
  13669. */
  13670. bytep += sizeof(uint32_t);
  13671. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13672. mboxq->vport = phba->pport;
  13673. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13674. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13675. if (rc == MBX_NOT_FINISHED) {
  13676. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13677. "2515 ADD_FCF_RECORD mailbox failed with "
  13678. "status 0x%x\n", rc);
  13679. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13680. rc = -EIO;
  13681. } else
  13682. rc = 0;
  13683. return rc;
  13684. }
  13685. /**
  13686. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13687. * @phba: pointer to lpfc hba data structure.
  13688. * @fcf_record: pointer to the fcf record to write the default data.
  13689. * @fcf_index: FCF table entry index.
  13690. *
  13691. * This routine is invoked to build the driver's default FCF record. The
  13692. * values used are hardcoded. This routine handles memory initialization.
  13693. *
  13694. **/
  13695. void
  13696. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13697. struct fcf_record *fcf_record,
  13698. uint16_t fcf_index)
  13699. {
  13700. memset(fcf_record, 0, sizeof(struct fcf_record));
  13701. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  13702. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  13703. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  13704. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  13705. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  13706. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  13707. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  13708. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  13709. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  13710. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  13711. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  13712. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  13713. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  13714. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  13715. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  13716. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  13717. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  13718. /* Set the VLAN bit map */
  13719. if (phba->valid_vlan) {
  13720. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  13721. = 1 << (phba->vlan_id % 8);
  13722. }
  13723. }
  13724. /**
  13725. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  13726. * @phba: pointer to lpfc hba data structure.
  13727. * @fcf_index: FCF table entry offset.
  13728. *
  13729. * This routine is invoked to scan the entire FCF table by reading FCF
  13730. * record and processing it one at a time starting from the @fcf_index
  13731. * for initial FCF discovery or fast FCF failover rediscovery.
  13732. *
  13733. * Return 0 if the mailbox command is submitted successfully, none 0
  13734. * otherwise.
  13735. **/
  13736. int
  13737. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13738. {
  13739. int rc = 0, error;
  13740. LPFC_MBOXQ_t *mboxq;
  13741. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  13742. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13743. if (!mboxq) {
  13744. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13745. "2000 Failed to allocate mbox for "
  13746. "READ_FCF cmd\n");
  13747. error = -ENOMEM;
  13748. goto fail_fcf_scan;
  13749. }
  13750. /* Construct the read FCF record mailbox command */
  13751. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13752. if (rc) {
  13753. error = -EINVAL;
  13754. goto fail_fcf_scan;
  13755. }
  13756. /* Issue the mailbox command asynchronously */
  13757. mboxq->vport = phba->pport;
  13758. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  13759. spin_lock_irq(&phba->hbalock);
  13760. phba->hba_flag |= FCF_TS_INPROG;
  13761. spin_unlock_irq(&phba->hbalock);
  13762. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13763. if (rc == MBX_NOT_FINISHED)
  13764. error = -EIO;
  13765. else {
  13766. /* Reset eligible FCF count for new scan */
  13767. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  13768. phba->fcf.eligible_fcf_cnt = 0;
  13769. error = 0;
  13770. }
  13771. fail_fcf_scan:
  13772. if (error) {
  13773. if (mboxq)
  13774. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13775. /* FCF scan failed, clear FCF_TS_INPROG flag */
  13776. spin_lock_irq(&phba->hbalock);
  13777. phba->hba_flag &= ~FCF_TS_INPROG;
  13778. spin_unlock_irq(&phba->hbalock);
  13779. }
  13780. return error;
  13781. }
  13782. /**
  13783. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  13784. * @phba: pointer to lpfc hba data structure.
  13785. * @fcf_index: FCF table entry offset.
  13786. *
  13787. * This routine is invoked to read an FCF record indicated by @fcf_index
  13788. * and to use it for FLOGI roundrobin FCF failover.
  13789. *
  13790. * Return 0 if the mailbox command is submitted successfully, none 0
  13791. * otherwise.
  13792. **/
  13793. int
  13794. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13795. {
  13796. int rc = 0, error;
  13797. LPFC_MBOXQ_t *mboxq;
  13798. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13799. if (!mboxq) {
  13800. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13801. "2763 Failed to allocate mbox for "
  13802. "READ_FCF cmd\n");
  13803. error = -ENOMEM;
  13804. goto fail_fcf_read;
  13805. }
  13806. /* Construct the read FCF record mailbox command */
  13807. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13808. if (rc) {
  13809. error = -EINVAL;
  13810. goto fail_fcf_read;
  13811. }
  13812. /* Issue the mailbox command asynchronously */
  13813. mboxq->vport = phba->pport;
  13814. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  13815. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13816. if (rc == MBX_NOT_FINISHED)
  13817. error = -EIO;
  13818. else
  13819. error = 0;
  13820. fail_fcf_read:
  13821. if (error && mboxq)
  13822. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13823. return error;
  13824. }
  13825. /**
  13826. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  13827. * @phba: pointer to lpfc hba data structure.
  13828. * @fcf_index: FCF table entry offset.
  13829. *
  13830. * This routine is invoked to read an FCF record indicated by @fcf_index to
  13831. * determine whether it's eligible for FLOGI roundrobin failover list.
  13832. *
  13833. * Return 0 if the mailbox command is submitted successfully, none 0
  13834. * otherwise.
  13835. **/
  13836. int
  13837. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13838. {
  13839. int rc = 0, error;
  13840. LPFC_MBOXQ_t *mboxq;
  13841. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13842. if (!mboxq) {
  13843. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13844. "2758 Failed to allocate mbox for "
  13845. "READ_FCF cmd\n");
  13846. error = -ENOMEM;
  13847. goto fail_fcf_read;
  13848. }
  13849. /* Construct the read FCF record mailbox command */
  13850. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13851. if (rc) {
  13852. error = -EINVAL;
  13853. goto fail_fcf_read;
  13854. }
  13855. /* Issue the mailbox command asynchronously */
  13856. mboxq->vport = phba->pport;
  13857. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  13858. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13859. if (rc == MBX_NOT_FINISHED)
  13860. error = -EIO;
  13861. else
  13862. error = 0;
  13863. fail_fcf_read:
  13864. if (error && mboxq)
  13865. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13866. return error;
  13867. }
  13868. /**
  13869. * lpfc_check_next_fcf_pri
  13870. * phba pointer to the lpfc_hba struct for this port.
  13871. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  13872. * routine when the rr_bmask is empty. The FCF indecies are put into the
  13873. * rr_bmask based on their priority level. Starting from the highest priority
  13874. * to the lowest. The most likely FCF candidate will be in the highest
  13875. * priority group. When this routine is called it searches the fcf_pri list for
  13876. * next lowest priority group and repopulates the rr_bmask with only those
  13877. * fcf_indexes.
  13878. * returns:
  13879. * 1=success 0=failure
  13880. **/
  13881. int
  13882. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  13883. {
  13884. uint16_t next_fcf_pri;
  13885. uint16_t last_index;
  13886. struct lpfc_fcf_pri *fcf_pri;
  13887. int rc;
  13888. int ret = 0;
  13889. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  13890. LPFC_SLI4_FCF_TBL_INDX_MAX);
  13891. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13892. "3060 Last IDX %d\n", last_index);
  13893. if (list_empty(&phba->fcf.fcf_pri_list)) {
  13894. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13895. "3061 Last IDX %d\n", last_index);
  13896. return 0; /* Empty rr list */
  13897. }
  13898. next_fcf_pri = 0;
  13899. /*
  13900. * Clear the rr_bmask and set all of the bits that are at this
  13901. * priority.
  13902. */
  13903. memset(phba->fcf.fcf_rr_bmask, 0,
  13904. sizeof(*phba->fcf.fcf_rr_bmask));
  13905. spin_lock_irq(&phba->hbalock);
  13906. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13907. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  13908. continue;
  13909. /*
  13910. * the 1st priority that has not FLOGI failed
  13911. * will be the highest.
  13912. */
  13913. if (!next_fcf_pri)
  13914. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13915. spin_unlock_irq(&phba->hbalock);
  13916. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13917. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13918. fcf_pri->fcf_rec.fcf_index);
  13919. if (rc)
  13920. return 0;
  13921. }
  13922. spin_lock_irq(&phba->hbalock);
  13923. }
  13924. /*
  13925. * if next_fcf_pri was not set above and the list is not empty then
  13926. * we have failed flogis on all of them. So reset flogi failed
  13927. * and start at the begining.
  13928. */
  13929. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  13930. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13931. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  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. } else
  13948. ret = 1;
  13949. spin_unlock_irq(&phba->hbalock);
  13950. return ret;
  13951. }
  13952. /**
  13953. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  13954. * @phba: pointer to lpfc hba data structure.
  13955. *
  13956. * This routine is to get the next eligible FCF record index in a round
  13957. * robin fashion. If the next eligible FCF record index equals to the
  13958. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  13959. * shall be returned, otherwise, the next eligible FCF record's index
  13960. * shall be returned.
  13961. **/
  13962. uint16_t
  13963. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  13964. {
  13965. uint16_t next_fcf_index;
  13966. /* Search start from next bit of currently registered FCF index */
  13967. next_priority:
  13968. next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
  13969. LPFC_SLI4_FCF_TBL_INDX_MAX;
  13970. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13971. LPFC_SLI4_FCF_TBL_INDX_MAX,
  13972. next_fcf_index);
  13973. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  13974. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13975. /*
  13976. * If we have wrapped then we need to clear the bits that
  13977. * have been tested so that we can detect when we should
  13978. * change the priority level.
  13979. */
  13980. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13981. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  13982. }
  13983. /* Check roundrobin failover list empty condition */
  13984. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  13985. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  13986. /*
  13987. * If next fcf index is not found check if there are lower
  13988. * Priority level fcf's in the fcf_priority list.
  13989. * Set up the rr_bmask with all of the avaiable fcf bits
  13990. * at that level and continue the selection process.
  13991. */
  13992. if (lpfc_check_next_fcf_pri_level(phba))
  13993. goto next_priority;
  13994. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  13995. "2844 No roundrobin failover FCF available\n");
  13996. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  13997. return LPFC_FCOE_FCF_NEXT_NONE;
  13998. else {
  13999. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14000. "3063 Only FCF available idx %d, flag %x\n",
  14001. next_fcf_index,
  14002. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  14003. return next_fcf_index;
  14004. }
  14005. }
  14006. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  14007. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  14008. LPFC_FCF_FLOGI_FAILED)
  14009. goto next_priority;
  14010. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14011. "2845 Get next roundrobin failover FCF (x%x)\n",
  14012. next_fcf_index);
  14013. return next_fcf_index;
  14014. }
  14015. /**
  14016. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  14017. * @phba: pointer to lpfc hba data structure.
  14018. *
  14019. * This routine sets the FCF record index in to the eligible bmask for
  14020. * roundrobin failover search. It checks to make sure that the index
  14021. * does not go beyond the range of the driver allocated bmask dimension
  14022. * before setting the bit.
  14023. *
  14024. * Returns 0 if the index bit successfully set, otherwise, it returns
  14025. * -EINVAL.
  14026. **/
  14027. int
  14028. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  14029. {
  14030. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14031. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14032. "2610 FCF (x%x) reached driver's book "
  14033. "keeping dimension:x%x\n",
  14034. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14035. return -EINVAL;
  14036. }
  14037. /* Set the eligible FCF record index bmask */
  14038. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14039. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14040. "2790 Set FCF (x%x) to roundrobin FCF failover "
  14041. "bmask\n", fcf_index);
  14042. return 0;
  14043. }
  14044. /**
  14045. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  14046. * @phba: pointer to lpfc hba data structure.
  14047. *
  14048. * This routine clears the FCF record index from the eligible bmask for
  14049. * roundrobin failover search. It checks to make sure that the index
  14050. * does not go beyond the range of the driver allocated bmask dimension
  14051. * before clearing the bit.
  14052. **/
  14053. void
  14054. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  14055. {
  14056. struct lpfc_fcf_pri *fcf_pri;
  14057. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14058. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14059. "2762 FCF (x%x) reached driver's book "
  14060. "keeping dimension:x%x\n",
  14061. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14062. return;
  14063. }
  14064. /* Clear the eligible FCF record index bmask */
  14065. spin_lock_irq(&phba->hbalock);
  14066. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14067. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  14068. list_del_init(&fcf_pri->list);
  14069. break;
  14070. }
  14071. }
  14072. spin_unlock_irq(&phba->hbalock);
  14073. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14074. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14075. "2791 Clear FCF (x%x) from roundrobin failover "
  14076. "bmask\n", fcf_index);
  14077. }
  14078. /**
  14079. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  14080. * @phba: pointer to lpfc hba data structure.
  14081. *
  14082. * This routine is the completion routine for the rediscover FCF table mailbox
  14083. * command. If the mailbox command returned failure, it will try to stop the
  14084. * FCF rediscover wait timer.
  14085. **/
  14086. void
  14087. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  14088. {
  14089. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14090. uint32_t shdr_status, shdr_add_status;
  14091. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14092. shdr_status = bf_get(lpfc_mbox_hdr_status,
  14093. &redisc_fcf->header.cfg_shdr.response);
  14094. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  14095. &redisc_fcf->header.cfg_shdr.response);
  14096. if (shdr_status || shdr_add_status) {
  14097. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14098. "2746 Requesting for FCF rediscovery failed "
  14099. "status x%x add_status x%x\n",
  14100. shdr_status, shdr_add_status);
  14101. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  14102. spin_lock_irq(&phba->hbalock);
  14103. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  14104. spin_unlock_irq(&phba->hbalock);
  14105. /*
  14106. * CVL event triggered FCF rediscover request failed,
  14107. * last resort to re-try current registered FCF entry.
  14108. */
  14109. lpfc_retry_pport_discovery(phba);
  14110. } else {
  14111. spin_lock_irq(&phba->hbalock);
  14112. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  14113. spin_unlock_irq(&phba->hbalock);
  14114. /*
  14115. * DEAD FCF event triggered FCF rediscover request
  14116. * failed, last resort to fail over as a link down
  14117. * to FCF registration.
  14118. */
  14119. lpfc_sli4_fcf_dead_failthrough(phba);
  14120. }
  14121. } else {
  14122. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14123. "2775 Start FCF rediscover quiescent timer\n");
  14124. /*
  14125. * Start FCF rediscovery wait timer for pending FCF
  14126. * before rescan FCF record table.
  14127. */
  14128. lpfc_fcf_redisc_wait_start_timer(phba);
  14129. }
  14130. mempool_free(mbox, phba->mbox_mem_pool);
  14131. }
  14132. /**
  14133. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14134. * @phba: pointer to lpfc hba data structure.
  14135. *
  14136. * This routine is invoked to request for rediscovery of the entire FCF table
  14137. * by the port.
  14138. **/
  14139. int
  14140. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14141. {
  14142. LPFC_MBOXQ_t *mbox;
  14143. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14144. int rc, length;
  14145. /* Cancel retry delay timers to all vports before FCF rediscover */
  14146. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14147. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14148. if (!mbox) {
  14149. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14150. "2745 Failed to allocate mbox for "
  14151. "requesting FCF rediscover.\n");
  14152. return -ENOMEM;
  14153. }
  14154. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14155. sizeof(struct lpfc_sli4_cfg_mhdr));
  14156. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14157. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14158. length, LPFC_SLI4_MBX_EMBED);
  14159. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14160. /* Set count to 0 for invalidating the entire FCF database */
  14161. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14162. /* Issue the mailbox command asynchronously */
  14163. mbox->vport = phba->pport;
  14164. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14165. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14166. if (rc == MBX_NOT_FINISHED) {
  14167. mempool_free(mbox, phba->mbox_mem_pool);
  14168. return -EIO;
  14169. }
  14170. return 0;
  14171. }
  14172. /**
  14173. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14174. * @phba: pointer to lpfc hba data structure.
  14175. *
  14176. * This function is the failover routine as a last resort to the FCF DEAD
  14177. * event when driver failed to perform fast FCF failover.
  14178. **/
  14179. void
  14180. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14181. {
  14182. uint32_t link_state;
  14183. /*
  14184. * Last resort as FCF DEAD event failover will treat this as
  14185. * a link down, but save the link state because we don't want
  14186. * it to be changed to Link Down unless it is already down.
  14187. */
  14188. link_state = phba->link_state;
  14189. lpfc_linkdown(phba);
  14190. phba->link_state = link_state;
  14191. /* Unregister FCF if no devices connected to it */
  14192. lpfc_unregister_unused_fcf(phba);
  14193. }
  14194. /**
  14195. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14196. * @phba: pointer to lpfc hba data structure.
  14197. * @rgn23_data: pointer to configure region 23 data.
  14198. *
  14199. * This function gets SLI3 port configure region 23 data through memory dump
  14200. * mailbox command. When it successfully retrieves data, the size of the data
  14201. * will be returned, otherwise, 0 will be returned.
  14202. **/
  14203. static uint32_t
  14204. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14205. {
  14206. LPFC_MBOXQ_t *pmb = NULL;
  14207. MAILBOX_t *mb;
  14208. uint32_t offset = 0;
  14209. int rc;
  14210. if (!rgn23_data)
  14211. return 0;
  14212. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14213. if (!pmb) {
  14214. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14215. "2600 failed to allocate mailbox memory\n");
  14216. return 0;
  14217. }
  14218. mb = &pmb->u.mb;
  14219. do {
  14220. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14221. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14222. if (rc != MBX_SUCCESS) {
  14223. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14224. "2601 failed to read config "
  14225. "region 23, rc 0x%x Status 0x%x\n",
  14226. rc, mb->mbxStatus);
  14227. mb->un.varDmp.word_cnt = 0;
  14228. }
  14229. /*
  14230. * dump mem may return a zero when finished or we got a
  14231. * mailbox error, either way we are done.
  14232. */
  14233. if (mb->un.varDmp.word_cnt == 0)
  14234. break;
  14235. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14236. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14237. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14238. rgn23_data + offset,
  14239. mb->un.varDmp.word_cnt);
  14240. offset += mb->un.varDmp.word_cnt;
  14241. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14242. mempool_free(pmb, phba->mbox_mem_pool);
  14243. return offset;
  14244. }
  14245. /**
  14246. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14247. * @phba: pointer to lpfc hba data structure.
  14248. * @rgn23_data: pointer to configure region 23 data.
  14249. *
  14250. * This function gets SLI4 port configure region 23 data through memory dump
  14251. * mailbox command. When it successfully retrieves data, the size of the data
  14252. * will be returned, otherwise, 0 will be returned.
  14253. **/
  14254. static uint32_t
  14255. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14256. {
  14257. LPFC_MBOXQ_t *mboxq = NULL;
  14258. struct lpfc_dmabuf *mp = NULL;
  14259. struct lpfc_mqe *mqe;
  14260. uint32_t data_length = 0;
  14261. int rc;
  14262. if (!rgn23_data)
  14263. return 0;
  14264. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14265. if (!mboxq) {
  14266. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14267. "3105 failed to allocate mailbox memory\n");
  14268. return 0;
  14269. }
  14270. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14271. goto out;
  14272. mqe = &mboxq->u.mqe;
  14273. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14274. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14275. if (rc)
  14276. goto out;
  14277. data_length = mqe->un.mb_words[5];
  14278. if (data_length == 0)
  14279. goto out;
  14280. if (data_length > DMP_RGN23_SIZE) {
  14281. data_length = 0;
  14282. goto out;
  14283. }
  14284. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14285. out:
  14286. mempool_free(mboxq, phba->mbox_mem_pool);
  14287. if (mp) {
  14288. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14289. kfree(mp);
  14290. }
  14291. return data_length;
  14292. }
  14293. /**
  14294. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14295. * @phba: pointer to lpfc hba data structure.
  14296. *
  14297. * This function read region 23 and parse TLV for port status to
  14298. * decide if the user disaled the port. If the TLV indicates the
  14299. * port is disabled, the hba_flag is set accordingly.
  14300. **/
  14301. void
  14302. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14303. {
  14304. uint8_t *rgn23_data = NULL;
  14305. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14306. uint32_t offset = 0;
  14307. /* Get adapter Region 23 data */
  14308. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14309. if (!rgn23_data)
  14310. goto out;
  14311. if (phba->sli_rev < LPFC_SLI_REV4)
  14312. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14313. else {
  14314. if_type = bf_get(lpfc_sli_intf_if_type,
  14315. &phba->sli4_hba.sli_intf);
  14316. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14317. goto out;
  14318. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14319. }
  14320. if (!data_size)
  14321. goto out;
  14322. /* Check the region signature first */
  14323. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14324. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14325. "2619 Config region 23 has bad signature\n");
  14326. goto out;
  14327. }
  14328. offset += 4;
  14329. /* Check the data structure version */
  14330. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14331. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14332. "2620 Config region 23 has bad version\n");
  14333. goto out;
  14334. }
  14335. offset += 4;
  14336. /* Parse TLV entries in the region */
  14337. while (offset < data_size) {
  14338. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14339. break;
  14340. /*
  14341. * If the TLV is not driver specific TLV or driver id is
  14342. * not linux driver id, skip the record.
  14343. */
  14344. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14345. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14346. (rgn23_data[offset + 3] != 0)) {
  14347. offset += rgn23_data[offset + 1] * 4 + 4;
  14348. continue;
  14349. }
  14350. /* Driver found a driver specific TLV in the config region */
  14351. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14352. offset += 4;
  14353. tlv_offset = 0;
  14354. /*
  14355. * Search for configured port state sub-TLV.
  14356. */
  14357. while ((offset < data_size) &&
  14358. (tlv_offset < sub_tlv_len)) {
  14359. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14360. offset += 4;
  14361. tlv_offset += 4;
  14362. break;
  14363. }
  14364. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14365. offset += rgn23_data[offset + 1] * 4 + 4;
  14366. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14367. continue;
  14368. }
  14369. /* This HBA contains PORT_STE configured */
  14370. if (!rgn23_data[offset + 2])
  14371. phba->hba_flag |= LINK_DISABLED;
  14372. goto out;
  14373. }
  14374. }
  14375. out:
  14376. kfree(rgn23_data);
  14377. return;
  14378. }
  14379. /**
  14380. * lpfc_wr_object - write an object to the firmware
  14381. * @phba: HBA structure that indicates port to create a queue on.
  14382. * @dmabuf_list: list of dmabufs to write to the port.
  14383. * @size: the total byte value of the objects to write to the port.
  14384. * @offset: the current offset to be used to start the transfer.
  14385. *
  14386. * This routine will create a wr_object mailbox command to send to the port.
  14387. * the mailbox command will be constructed using the dma buffers described in
  14388. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14389. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14390. * used to indicate the starting offset of the transfer and will also return
  14391. * the offset after the write object mailbox has completed. @size is used to
  14392. * determine the end of the object and whether the eof bit should be set.
  14393. *
  14394. * Return 0 is successful and offset will contain the the new offset to use
  14395. * for the next write.
  14396. * Return negative value for error cases.
  14397. **/
  14398. int
  14399. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14400. uint32_t size, uint32_t *offset)
  14401. {
  14402. struct lpfc_mbx_wr_object *wr_object;
  14403. LPFC_MBOXQ_t *mbox;
  14404. int rc = 0, i = 0;
  14405. uint32_t shdr_status, shdr_add_status;
  14406. uint32_t mbox_tmo;
  14407. union lpfc_sli4_cfg_shdr *shdr;
  14408. struct lpfc_dmabuf *dmabuf;
  14409. uint32_t written = 0;
  14410. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14411. if (!mbox)
  14412. return -ENOMEM;
  14413. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14414. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14415. sizeof(struct lpfc_mbx_wr_object) -
  14416. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14417. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14418. wr_object->u.request.write_offset = *offset;
  14419. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14420. wr_object->u.request.object_name[0] =
  14421. cpu_to_le32(wr_object->u.request.object_name[0]);
  14422. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14423. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14424. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14425. break;
  14426. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14427. wr_object->u.request.bde[i].addrHigh =
  14428. putPaddrHigh(dmabuf->phys);
  14429. if (written + SLI4_PAGE_SIZE >= size) {
  14430. wr_object->u.request.bde[i].tus.f.bdeSize =
  14431. (size - written);
  14432. written += (size - written);
  14433. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14434. } else {
  14435. wr_object->u.request.bde[i].tus.f.bdeSize =
  14436. SLI4_PAGE_SIZE;
  14437. written += SLI4_PAGE_SIZE;
  14438. }
  14439. i++;
  14440. }
  14441. wr_object->u.request.bde_count = i;
  14442. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14443. if (!phba->sli4_hba.intr_enable)
  14444. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14445. else {
  14446. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14447. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14448. }
  14449. /* The IOCTL status is embedded in the mailbox subheader. */
  14450. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14451. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14452. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14453. if (rc != MBX_TIMEOUT)
  14454. mempool_free(mbox, phba->mbox_mem_pool);
  14455. if (shdr_status || shdr_add_status || rc) {
  14456. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14457. "3025 Write Object mailbox failed with "
  14458. "status x%x add_status x%x, mbx status x%x\n",
  14459. shdr_status, shdr_add_status, rc);
  14460. rc = -ENXIO;
  14461. } else
  14462. *offset += wr_object->u.response.actual_write_length;
  14463. return rc;
  14464. }
  14465. /**
  14466. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14467. * @vport: pointer to vport data structure.
  14468. *
  14469. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14470. * and REG_VPI mailbox commands associated with the vport. This function
  14471. * is called when driver want to restart discovery of the vport due to
  14472. * a Clear Virtual Link event.
  14473. **/
  14474. void
  14475. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14476. {
  14477. struct lpfc_hba *phba = vport->phba;
  14478. LPFC_MBOXQ_t *mb, *nextmb;
  14479. struct lpfc_dmabuf *mp;
  14480. struct lpfc_nodelist *ndlp;
  14481. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14482. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14483. LIST_HEAD(mbox_cmd_list);
  14484. uint8_t restart_loop;
  14485. /* Clean up internally queued mailbox commands with the vport */
  14486. spin_lock_irq(&phba->hbalock);
  14487. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14488. if (mb->vport != vport)
  14489. continue;
  14490. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14491. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14492. continue;
  14493. list_del(&mb->list);
  14494. list_add_tail(&mb->list, &mbox_cmd_list);
  14495. }
  14496. /* Clean up active mailbox command with the vport */
  14497. mb = phba->sli.mbox_active;
  14498. if (mb && (mb->vport == vport)) {
  14499. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14500. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14501. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14502. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14503. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14504. /* Put reference count for delayed processing */
  14505. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14506. /* Unregister the RPI when mailbox complete */
  14507. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14508. }
  14509. }
  14510. /* Cleanup any mailbox completions which are not yet processed */
  14511. do {
  14512. restart_loop = 0;
  14513. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14514. /*
  14515. * If this mailox is already processed or it is
  14516. * for another vport ignore it.
  14517. */
  14518. if ((mb->vport != vport) ||
  14519. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14520. continue;
  14521. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14522. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14523. continue;
  14524. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14525. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14526. ndlp = (struct lpfc_nodelist *)mb->context2;
  14527. /* Unregister the RPI when mailbox complete */
  14528. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14529. restart_loop = 1;
  14530. spin_unlock_irq(&phba->hbalock);
  14531. spin_lock(shost->host_lock);
  14532. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14533. spin_unlock(shost->host_lock);
  14534. spin_lock_irq(&phba->hbalock);
  14535. break;
  14536. }
  14537. }
  14538. } while (restart_loop);
  14539. spin_unlock_irq(&phba->hbalock);
  14540. /* Release the cleaned-up mailbox commands */
  14541. while (!list_empty(&mbox_cmd_list)) {
  14542. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14543. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14544. mp = (struct lpfc_dmabuf *) (mb->context1);
  14545. if (mp) {
  14546. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14547. kfree(mp);
  14548. }
  14549. ndlp = (struct lpfc_nodelist *) mb->context2;
  14550. mb->context2 = NULL;
  14551. if (ndlp) {
  14552. spin_lock(shost->host_lock);
  14553. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14554. spin_unlock(shost->host_lock);
  14555. lpfc_nlp_put(ndlp);
  14556. }
  14557. }
  14558. mempool_free(mb, phba->mbox_mem_pool);
  14559. }
  14560. /* Release the ndlp with the cleaned-up active mailbox command */
  14561. if (act_mbx_ndlp) {
  14562. spin_lock(shost->host_lock);
  14563. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14564. spin_unlock(shost->host_lock);
  14565. lpfc_nlp_put(act_mbx_ndlp);
  14566. }
  14567. }
  14568. /**
  14569. * lpfc_drain_txq - Drain the txq
  14570. * @phba: Pointer to HBA context object.
  14571. *
  14572. * This function attempt to submit IOCBs on the txq
  14573. * to the adapter. For SLI4 adapters, the txq contains
  14574. * ELS IOCBs that have been deferred because the there
  14575. * are no SGLs. This congestion can occur with large
  14576. * vport counts during node discovery.
  14577. **/
  14578. uint32_t
  14579. lpfc_drain_txq(struct lpfc_hba *phba)
  14580. {
  14581. LIST_HEAD(completions);
  14582. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14583. struct lpfc_iocbq *piocbq = 0;
  14584. unsigned long iflags = 0;
  14585. char *fail_msg = NULL;
  14586. struct lpfc_sglq *sglq;
  14587. union lpfc_wqe wqe;
  14588. spin_lock_irqsave(&phba->hbalock, iflags);
  14589. if (pring->txq_cnt > pring->txq_max)
  14590. pring->txq_max = pring->txq_cnt;
  14591. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14592. while (pring->txq_cnt) {
  14593. spin_lock_irqsave(&phba->hbalock, iflags);
  14594. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14595. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14596. if (!sglq) {
  14597. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14598. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14599. break;
  14600. } else {
  14601. if (!piocbq) {
  14602. /* The txq_cnt out of sync. This should
  14603. * never happen
  14604. */
  14605. sglq = __lpfc_clear_active_sglq(phba,
  14606. sglq->sli4_lxritag);
  14607. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14608. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14609. "2823 txq empty and txq_cnt is %d\n ",
  14610. pring->txq_cnt);
  14611. break;
  14612. }
  14613. }
  14614. /* The xri and iocb resources secured,
  14615. * attempt to issue request
  14616. */
  14617. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14618. piocbq->sli4_xritag = sglq->sli4_xritag;
  14619. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14620. fail_msg = "to convert bpl to sgl";
  14621. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14622. fail_msg = "to convert iocb to wqe";
  14623. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14624. fail_msg = " - Wq is full";
  14625. else
  14626. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14627. if (fail_msg) {
  14628. /* Failed means we can't issue and need to cancel */
  14629. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14630. "2822 IOCB failed %s iotag 0x%x "
  14631. "xri 0x%x\n",
  14632. fail_msg,
  14633. piocbq->iotag, piocbq->sli4_xritag);
  14634. list_add_tail(&piocbq->list, &completions);
  14635. }
  14636. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14637. }
  14638. /* Cancel all the IOCBs that cannot be issued */
  14639. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14640. IOERR_SLI_ABORTED);
  14641. return pring->txq_cnt;
  14642. }