lpfc_sli.c 491 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2012 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/fc/fc_fs.h>
  32. #include <linux/aer.h>
  33. #include "lpfc_hw4.h"
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_nl.h"
  38. #include "lpfc_disc.h"
  39. #include "lpfc_scsi.h"
  40. #include "lpfc.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_compat.h"
  44. #include "lpfc_debugfs.h"
  45. #include "lpfc_vport.h"
  46. /* There are only four IOCB completion types. */
  47. typedef enum _lpfc_iocb_type {
  48. LPFC_UNKNOWN_IOCB,
  49. LPFC_UNSOL_IOCB,
  50. LPFC_SOL_IOCB,
  51. LPFC_ABORT_IOCB
  52. } lpfc_iocb_type;
  53. /* Provide function prototypes local to this module. */
  54. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint32_t);
  56. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  57. uint8_t *, uint32_t *);
  58. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  59. struct lpfc_iocbq *);
  60. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  61. struct hbq_dmabuf *);
  62. static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
  63. struct lpfc_cqe *);
  64. static 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_hi, &doorbell,
  266. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  267. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  268. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  269. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  270. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  271. readl(q->phba->sli4_hba.EQCQDBregaddr);
  272. return released;
  273. }
  274. /**
  275. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  276. * @q: The Completion Queue to get the first valid CQE from
  277. *
  278. * This routine will get the first valid Completion Queue Entry from @q, update
  279. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  280. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  281. * processed, but not popped back to the HBA then this routine will return NULL.
  282. **/
  283. static struct lpfc_cqe *
  284. lpfc_sli4_cq_get(struct lpfc_queue *q)
  285. {
  286. struct lpfc_cqe *cqe;
  287. /* sanity check on queue memory */
  288. if (unlikely(!q))
  289. return NULL;
  290. /* If the next CQE is not valid then we are done */
  291. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  292. return NULL;
  293. /* If the host has not yet processed the next entry then we are done */
  294. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  295. return NULL;
  296. cqe = q->qe[q->hba_index].cqe;
  297. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  298. return cqe;
  299. }
  300. /**
  301. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  302. * @q: The Completion Queue that the host has completed processing for.
  303. * @arm: Indicates whether the host wants to arms this CQ.
  304. *
  305. * This routine will mark all Completion queue entries on @q, from the last
  306. * known completed entry to the last entry that was processed, as completed
  307. * by clearing the valid bit for each completion queue entry. Then it will
  308. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  309. * The internal host index in the @q will be updated by this routine to indicate
  310. * that the host has finished processing the entries. The @arm parameter
  311. * indicates that the queue should be rearmed when ringing the doorbell.
  312. *
  313. * This function will return the number of CQEs that were released.
  314. **/
  315. uint32_t
  316. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  317. {
  318. uint32_t released = 0;
  319. struct lpfc_cqe *temp_qe;
  320. struct lpfc_register doorbell;
  321. /* sanity check on queue memory */
  322. if (unlikely(!q))
  323. return 0;
  324. /* while there are valid entries */
  325. while (q->hba_index != q->host_index) {
  326. temp_qe = q->qe[q->host_index].cqe;
  327. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  328. released++;
  329. q->host_index = ((q->host_index + 1) % q->entry_count);
  330. }
  331. if (unlikely(released == 0 && !arm))
  332. return 0;
  333. /* ring doorbell for number popped */
  334. doorbell.word0 = 0;
  335. if (arm)
  336. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  337. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  338. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  339. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  340. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  341. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  342. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  343. return released;
  344. }
  345. /**
  346. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  347. * @q: The Header Receive Queue to operate on.
  348. * @wqe: The Receive Queue Entry to put on the Receive queue.
  349. *
  350. * This routine will copy the contents of @wqe to the next available entry on
  351. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  352. * HBA to start processing the Receive Queue Entry. This function returns the
  353. * index that the rqe was copied to if successful. If no entries are available
  354. * on @q then this function will return -ENOMEM.
  355. * The caller is expected to hold the hbalock when calling this routine.
  356. **/
  357. static int
  358. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  359. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  360. {
  361. struct lpfc_rqe *temp_hrqe;
  362. struct lpfc_rqe *temp_drqe;
  363. struct lpfc_register doorbell;
  364. int put_index = hq->host_index;
  365. /* sanity check on queue memory */
  366. if (unlikely(!hq) || unlikely(!dq))
  367. return -ENOMEM;
  368. temp_hrqe = hq->qe[hq->host_index].rqe;
  369. temp_drqe = dq->qe[dq->host_index].rqe;
  370. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  371. return -EINVAL;
  372. if (hq->host_index != dq->host_index)
  373. return -EINVAL;
  374. /* If the host has not yet processed the next entry then we are done */
  375. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  376. return -EBUSY;
  377. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  378. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  379. /* Update the host index to point to the next slot */
  380. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  381. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  382. /* Ring The Header Receive Queue Doorbell */
  383. if (!(hq->host_index % hq->entry_repost)) {
  384. doorbell.word0 = 0;
  385. bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
  386. hq->entry_repost);
  387. bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
  388. writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
  389. }
  390. return put_index;
  391. }
  392. /**
  393. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  394. * @q: The Header Receive Queue to operate on.
  395. *
  396. * This routine will update the HBA index of a queue to reflect consumption of
  397. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  398. * consumed an entry the host calls this function to update the queue's
  399. * internal pointers. This routine returns the number of entries that were
  400. * consumed by the HBA.
  401. **/
  402. static uint32_t
  403. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  404. {
  405. /* sanity check on queue memory */
  406. if (unlikely(!hq) || unlikely(!dq))
  407. return 0;
  408. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  409. return 0;
  410. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  411. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  412. return 1;
  413. }
  414. /**
  415. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  416. * @phba: Pointer to HBA context object.
  417. * @pring: Pointer to driver SLI ring object.
  418. *
  419. * This function returns pointer to next command iocb entry
  420. * in the command ring. The caller must hold hbalock to prevent
  421. * other threads consume the next command iocb.
  422. * SLI-2/SLI-3 provide different sized iocbs.
  423. **/
  424. static inline IOCB_t *
  425. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  426. {
  427. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  428. pring->cmdidx * phba->iocb_cmd_size);
  429. }
  430. /**
  431. * lpfc_resp_iocb - Get next response iocb entry in the ring
  432. * @phba: Pointer to HBA context object.
  433. * @pring: Pointer to driver SLI ring object.
  434. *
  435. * This function returns pointer to next response iocb entry
  436. * in the response ring. The caller must hold hbalock to make sure
  437. * that no other thread consume the next response iocb.
  438. * SLI-2/SLI-3 provide different sized iocbs.
  439. **/
  440. static inline IOCB_t *
  441. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  442. {
  443. return (IOCB_t *) (((char *) pring->rspringaddr) +
  444. pring->rspidx * phba->iocb_rsp_size);
  445. }
  446. /**
  447. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  448. * @phba: Pointer to HBA context object.
  449. *
  450. * This function is called with hbalock held. This function
  451. * allocates a new driver iocb object from the iocb pool. If the
  452. * allocation is successful, it returns pointer to the newly
  453. * allocated iocb object else it returns NULL.
  454. **/
  455. static struct lpfc_iocbq *
  456. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  457. {
  458. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  459. struct lpfc_iocbq * iocbq = NULL;
  460. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  461. if (iocbq)
  462. phba->iocb_cnt++;
  463. if (phba->iocb_cnt > phba->iocb_max)
  464. phba->iocb_max = phba->iocb_cnt;
  465. return iocbq;
  466. }
  467. /**
  468. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  469. * @phba: Pointer to HBA context object.
  470. * @xritag: XRI value.
  471. *
  472. * This function clears the sglq pointer from the array of acive
  473. * sglq's. The xritag that is passed in is used to index into the
  474. * array. Before the xritag can be used it needs to be adjusted
  475. * by subtracting the xribase.
  476. *
  477. * Returns sglq ponter = success, NULL = Failure.
  478. **/
  479. static struct lpfc_sglq *
  480. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  481. {
  482. struct lpfc_sglq *sglq;
  483. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  484. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  485. return sglq;
  486. }
  487. /**
  488. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  489. * @phba: Pointer to HBA context object.
  490. * @xritag: XRI value.
  491. *
  492. * This function returns the sglq pointer from the array of acive
  493. * sglq's. The xritag that is passed in is used to index into the
  494. * array. Before the xritag can be used it needs to be adjusted
  495. * by subtracting the xribase.
  496. *
  497. * Returns sglq ponter = success, NULL = Failure.
  498. **/
  499. struct lpfc_sglq *
  500. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  501. {
  502. struct lpfc_sglq *sglq;
  503. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  504. return sglq;
  505. }
  506. /**
  507. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  508. * @phba: Pointer to HBA context object.
  509. * @xritag: xri used in this exchange.
  510. * @rrq: The RRQ to be cleared.
  511. *
  512. **/
  513. void
  514. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  515. uint16_t xritag,
  516. struct lpfc_node_rrq *rrq)
  517. {
  518. struct lpfc_nodelist *ndlp = NULL;
  519. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  520. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  521. /* The target DID could have been swapped (cable swap)
  522. * we should use the ndlp from the findnode if it is
  523. * available.
  524. */
  525. if ((!ndlp) && rrq->ndlp)
  526. ndlp = rrq->ndlp;
  527. if (!ndlp)
  528. goto out;
  529. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  530. rrq->send_rrq = 0;
  531. rrq->xritag = 0;
  532. rrq->rrq_stop_time = 0;
  533. }
  534. out:
  535. mempool_free(rrq, phba->rrq_pool);
  536. }
  537. /**
  538. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  539. * @phba: Pointer to HBA context object.
  540. *
  541. * This function is called with hbalock held. This function
  542. * Checks if stop_time (ratov from setting rrq active) has
  543. * been reached, if it has and the send_rrq flag is set then
  544. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  545. * then it will just call the routine to clear the rrq and
  546. * free the rrq resource.
  547. * The timer is set to the next rrq that is going to expire before
  548. * leaving the routine.
  549. *
  550. **/
  551. void
  552. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  553. {
  554. struct lpfc_node_rrq *rrq;
  555. struct lpfc_node_rrq *nextrrq;
  556. unsigned long next_time;
  557. unsigned long iflags;
  558. LIST_HEAD(send_rrq);
  559. spin_lock_irqsave(&phba->hbalock, iflags);
  560. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  561. next_time = jiffies + HZ * (phba->fc_ratov + 1);
  562. list_for_each_entry_safe(rrq, nextrrq,
  563. &phba->active_rrq_list, list) {
  564. if (time_after(jiffies, rrq->rrq_stop_time))
  565. list_move(&rrq->list, &send_rrq);
  566. else if (time_before(rrq->rrq_stop_time, next_time))
  567. next_time = rrq->rrq_stop_time;
  568. }
  569. spin_unlock_irqrestore(&phba->hbalock, iflags);
  570. if (!list_empty(&phba->active_rrq_list))
  571. mod_timer(&phba->rrq_tmr, next_time);
  572. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  573. list_del(&rrq->list);
  574. if (!rrq->send_rrq)
  575. /* this call will free the rrq */
  576. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  577. else if (lpfc_send_rrq(phba, rrq)) {
  578. /* if we send the rrq then the completion handler
  579. * will clear the bit in the xribitmap.
  580. */
  581. lpfc_clr_rrq_active(phba, rrq->xritag,
  582. rrq);
  583. }
  584. }
  585. }
  586. /**
  587. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  588. * @vport: Pointer to vport context object.
  589. * @xri: The xri used in the exchange.
  590. * @did: The targets DID for this exchange.
  591. *
  592. * returns NULL = rrq not found in the phba->active_rrq_list.
  593. * rrq = rrq for this xri and target.
  594. **/
  595. struct lpfc_node_rrq *
  596. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  597. {
  598. struct lpfc_hba *phba = vport->phba;
  599. struct lpfc_node_rrq *rrq;
  600. struct lpfc_node_rrq *nextrrq;
  601. unsigned long iflags;
  602. if (phba->sli_rev != LPFC_SLI_REV4)
  603. return NULL;
  604. spin_lock_irqsave(&phba->hbalock, iflags);
  605. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  606. if (rrq->vport == vport && rrq->xritag == xri &&
  607. rrq->nlp_DID == did){
  608. list_del(&rrq->list);
  609. spin_unlock_irqrestore(&phba->hbalock, iflags);
  610. return rrq;
  611. }
  612. }
  613. spin_unlock_irqrestore(&phba->hbalock, iflags);
  614. return NULL;
  615. }
  616. /**
  617. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  618. * @vport: Pointer to vport context object.
  619. * @ndlp: Pointer to the lpfc_node_list structure.
  620. * If ndlp is NULL Remove all active RRQs for this vport from the
  621. * phba->active_rrq_list and clear the rrq.
  622. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  623. **/
  624. void
  625. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  626. {
  627. struct lpfc_hba *phba = vport->phba;
  628. struct lpfc_node_rrq *rrq;
  629. struct lpfc_node_rrq *nextrrq;
  630. unsigned long iflags;
  631. LIST_HEAD(rrq_list);
  632. if (phba->sli_rev != LPFC_SLI_REV4)
  633. return;
  634. if (!ndlp) {
  635. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  636. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  637. }
  638. spin_lock_irqsave(&phba->hbalock, iflags);
  639. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  640. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  641. list_move(&rrq->list, &rrq_list);
  642. spin_unlock_irqrestore(&phba->hbalock, iflags);
  643. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  644. list_del(&rrq->list);
  645. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  646. }
  647. }
  648. /**
  649. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  650. * @phba: Pointer to HBA context object.
  651. *
  652. * Remove all rrqs from the phba->active_rrq_list and free them by
  653. * calling __lpfc_clr_active_rrq
  654. *
  655. **/
  656. void
  657. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  658. {
  659. struct lpfc_node_rrq *rrq;
  660. struct lpfc_node_rrq *nextrrq;
  661. unsigned long next_time;
  662. unsigned long iflags;
  663. LIST_HEAD(rrq_list);
  664. if (phba->sli_rev != LPFC_SLI_REV4)
  665. return;
  666. spin_lock_irqsave(&phba->hbalock, iflags);
  667. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  668. next_time = jiffies + HZ * (phba->fc_ratov * 2);
  669. list_splice_init(&phba->active_rrq_list, &rrq_list);
  670. spin_unlock_irqrestore(&phba->hbalock, iflags);
  671. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  672. list_del(&rrq->list);
  673. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  674. }
  675. if (!list_empty(&phba->active_rrq_list))
  676. mod_timer(&phba->rrq_tmr, next_time);
  677. }
  678. /**
  679. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  680. * @phba: Pointer to HBA context object.
  681. * @ndlp: Targets nodelist pointer for this exchange.
  682. * @xritag the xri in the bitmap to test.
  683. *
  684. * This function is called with hbalock held. This function
  685. * returns 0 = rrq not active for this xri
  686. * 1 = rrq is valid for this xri.
  687. **/
  688. int
  689. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  690. uint16_t xritag)
  691. {
  692. if (!ndlp)
  693. return 0;
  694. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  695. return 1;
  696. else
  697. return 0;
  698. }
  699. /**
  700. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  701. * @phba: Pointer to HBA context object.
  702. * @ndlp: nodelist pointer for this target.
  703. * @xritag: xri used in this exchange.
  704. * @rxid: Remote Exchange ID.
  705. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  706. *
  707. * This function takes the hbalock.
  708. * The active bit is always set in the active rrq xri_bitmap even
  709. * if there is no slot avaiable for the other rrq information.
  710. *
  711. * returns 0 rrq actived for this xri
  712. * < 0 No memory or invalid ndlp.
  713. **/
  714. int
  715. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  716. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  717. {
  718. unsigned long iflags;
  719. struct lpfc_node_rrq *rrq;
  720. int empty;
  721. if (!ndlp)
  722. return -EINVAL;
  723. if (!phba->cfg_enable_rrq)
  724. return -EINVAL;
  725. spin_lock_irqsave(&phba->hbalock, iflags);
  726. if (phba->pport->load_flag & FC_UNLOADING) {
  727. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  728. goto out;
  729. }
  730. /*
  731. * set the active bit even if there is no mem available.
  732. */
  733. if (NLP_CHK_FREE_REQ(ndlp))
  734. goto out;
  735. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  736. goto out;
  737. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  738. goto out;
  739. spin_unlock_irqrestore(&phba->hbalock, iflags);
  740. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  741. if (!rrq) {
  742. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  743. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  744. " DID:0x%x Send:%d\n",
  745. xritag, rxid, ndlp->nlp_DID, send_rrq);
  746. return -EINVAL;
  747. }
  748. rrq->send_rrq = send_rrq;
  749. rrq->xritag = xritag;
  750. rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
  751. rrq->ndlp = ndlp;
  752. rrq->nlp_DID = ndlp->nlp_DID;
  753. rrq->vport = ndlp->vport;
  754. rrq->rxid = rxid;
  755. rrq->send_rrq = send_rrq;
  756. spin_lock_irqsave(&phba->hbalock, iflags);
  757. empty = list_empty(&phba->active_rrq_list);
  758. list_add_tail(&rrq->list, &phba->active_rrq_list);
  759. phba->hba_flag |= HBA_RRQ_ACTIVE;
  760. if (empty)
  761. lpfc_worker_wake_up(phba);
  762. spin_unlock_irqrestore(&phba->hbalock, iflags);
  763. return 0;
  764. out:
  765. spin_unlock_irqrestore(&phba->hbalock, iflags);
  766. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  767. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  768. " DID:0x%x Send:%d\n",
  769. xritag, rxid, ndlp->nlp_DID, send_rrq);
  770. return -EINVAL;
  771. }
  772. /**
  773. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  774. * @phba: Pointer to HBA context object.
  775. * @piocb: Pointer to the iocbq.
  776. *
  777. * This function is called with hbalock held. This function
  778. * gets a new driver sglq object from the sglq list. If the
  779. * list is not empty then it is successful, it returns pointer to the newly
  780. * allocated sglq object else it returns NULL.
  781. **/
  782. static struct lpfc_sglq *
  783. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  784. {
  785. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  786. struct lpfc_sglq *sglq = NULL;
  787. struct lpfc_sglq *start_sglq = NULL;
  788. struct lpfc_scsi_buf *lpfc_cmd;
  789. struct lpfc_nodelist *ndlp;
  790. int found = 0;
  791. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  792. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  793. ndlp = lpfc_cmd->rdata->pnode;
  794. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  795. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  796. ndlp = piocbq->context_un.ndlp;
  797. else
  798. ndlp = piocbq->context1;
  799. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  800. start_sglq = sglq;
  801. while (!found) {
  802. if (!sglq)
  803. return NULL;
  804. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
  805. /* This xri has an rrq outstanding for this DID.
  806. * put it back in the list and get another xri.
  807. */
  808. list_add_tail(&sglq->list, lpfc_sgl_list);
  809. sglq = NULL;
  810. list_remove_head(lpfc_sgl_list, sglq,
  811. struct lpfc_sglq, list);
  812. if (sglq == start_sglq) {
  813. sglq = NULL;
  814. break;
  815. } else
  816. continue;
  817. }
  818. sglq->ndlp = ndlp;
  819. found = 1;
  820. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  821. sglq->state = SGL_ALLOCATED;
  822. }
  823. return sglq;
  824. }
  825. /**
  826. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  827. * @phba: Pointer to HBA context object.
  828. *
  829. * This function is called with no lock held. This function
  830. * allocates a new driver iocb object from the iocb pool. If the
  831. * allocation is successful, it returns pointer to the newly
  832. * allocated iocb object else it returns NULL.
  833. **/
  834. struct lpfc_iocbq *
  835. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  836. {
  837. struct lpfc_iocbq * iocbq = NULL;
  838. unsigned long iflags;
  839. spin_lock_irqsave(&phba->hbalock, iflags);
  840. iocbq = __lpfc_sli_get_iocbq(phba);
  841. spin_unlock_irqrestore(&phba->hbalock, iflags);
  842. return iocbq;
  843. }
  844. /**
  845. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  846. * @phba: Pointer to HBA context object.
  847. * @iocbq: Pointer to driver iocb object.
  848. *
  849. * This function is called with hbalock held to release driver
  850. * iocb object to the iocb pool. The iotag in the iocb object
  851. * does not change for each use of the iocb object. This function
  852. * clears all other fields of the iocb object when it is freed.
  853. * The sqlq structure that holds the xritag and phys and virtual
  854. * mappings for the scatter gather list is retrieved from the
  855. * active array of sglq. The get of the sglq pointer also clears
  856. * the entry in the array. If the status of the IO indiactes that
  857. * this IO was aborted then the sglq entry it put on the
  858. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  859. * IO has good status or fails for any other reason then the sglq
  860. * entry is added to the free list (lpfc_sgl_list).
  861. **/
  862. static void
  863. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  864. {
  865. struct lpfc_sglq *sglq;
  866. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  867. unsigned long iflag = 0;
  868. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  869. if (iocbq->sli4_xritag == NO_XRI)
  870. sglq = NULL;
  871. else
  872. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  873. if (sglq) {
  874. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  875. (sglq->state != SGL_XRI_ABORTED)) {
  876. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  877. iflag);
  878. list_add(&sglq->list,
  879. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  880. spin_unlock_irqrestore(
  881. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  882. } else {
  883. sglq->state = SGL_FREED;
  884. sglq->ndlp = NULL;
  885. list_add_tail(&sglq->list,
  886. &phba->sli4_hba.lpfc_sgl_list);
  887. /* Check if TXQ queue needs to be serviced */
  888. if (pring->txq_cnt)
  889. lpfc_worker_wake_up(phba);
  890. }
  891. }
  892. /*
  893. * Clean all volatile data fields, preserve iotag and node struct.
  894. */
  895. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  896. iocbq->sli4_lxritag = NO_XRI;
  897. iocbq->sli4_xritag = NO_XRI;
  898. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  899. }
  900. /**
  901. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  902. * @phba: Pointer to HBA context object.
  903. * @iocbq: Pointer to driver iocb object.
  904. *
  905. * This function is called with hbalock held to release driver
  906. * iocb object to the iocb pool. The iotag in the iocb object
  907. * does not change for each use of the iocb object. This function
  908. * clears all other fields of the iocb object when it is freed.
  909. **/
  910. static void
  911. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  912. {
  913. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  914. /*
  915. * Clean all volatile data fields, preserve iotag and node struct.
  916. */
  917. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  918. iocbq->sli4_xritag = NO_XRI;
  919. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  920. }
  921. /**
  922. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  923. * @phba: Pointer to HBA context object.
  924. * @iocbq: Pointer to driver iocb object.
  925. *
  926. * This function is called with hbalock held to release driver
  927. * iocb object to the iocb pool. The iotag in the iocb object
  928. * does not change for each use of the iocb object. This function
  929. * clears all other fields of the iocb object when it is freed.
  930. **/
  931. static void
  932. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  933. {
  934. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  935. phba->iocb_cnt--;
  936. }
  937. /**
  938. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  939. * @phba: Pointer to HBA context object.
  940. * @iocbq: Pointer to driver iocb object.
  941. *
  942. * This function is called with no lock held to release the iocb to
  943. * iocb pool.
  944. **/
  945. void
  946. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  947. {
  948. unsigned long iflags;
  949. /*
  950. * Clean all volatile data fields, preserve iotag and node struct.
  951. */
  952. spin_lock_irqsave(&phba->hbalock, iflags);
  953. __lpfc_sli_release_iocbq(phba, iocbq);
  954. spin_unlock_irqrestore(&phba->hbalock, iflags);
  955. }
  956. /**
  957. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  958. * @phba: Pointer to HBA context object.
  959. * @iocblist: List of IOCBs.
  960. * @ulpstatus: ULP status in IOCB command field.
  961. * @ulpWord4: ULP word-4 in IOCB command field.
  962. *
  963. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  964. * on the list by invoking the complete callback function associated with the
  965. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  966. * fields.
  967. **/
  968. void
  969. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  970. uint32_t ulpstatus, uint32_t ulpWord4)
  971. {
  972. struct lpfc_iocbq *piocb;
  973. while (!list_empty(iocblist)) {
  974. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  975. if (!piocb->iocb_cmpl)
  976. lpfc_sli_release_iocbq(phba, piocb);
  977. else {
  978. piocb->iocb.ulpStatus = ulpstatus;
  979. piocb->iocb.un.ulpWord[4] = ulpWord4;
  980. (piocb->iocb_cmpl) (phba, piocb, piocb);
  981. }
  982. }
  983. return;
  984. }
  985. /**
  986. * lpfc_sli_iocb_cmd_type - Get the iocb type
  987. * @iocb_cmnd: iocb command code.
  988. *
  989. * This function is called by ring event handler function to get the iocb type.
  990. * This function translates the iocb command to an iocb command type used to
  991. * decide the final disposition of each completed IOCB.
  992. * The function returns
  993. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  994. * LPFC_SOL_IOCB if it is a solicited iocb completion
  995. * LPFC_ABORT_IOCB if it is an abort iocb
  996. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  997. *
  998. * The caller is not required to hold any lock.
  999. **/
  1000. static lpfc_iocb_type
  1001. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1002. {
  1003. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1004. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1005. return 0;
  1006. switch (iocb_cmnd) {
  1007. case CMD_XMIT_SEQUENCE_CR:
  1008. case CMD_XMIT_SEQUENCE_CX:
  1009. case CMD_XMIT_BCAST_CN:
  1010. case CMD_XMIT_BCAST_CX:
  1011. case CMD_ELS_REQUEST_CR:
  1012. case CMD_ELS_REQUEST_CX:
  1013. case CMD_CREATE_XRI_CR:
  1014. case CMD_CREATE_XRI_CX:
  1015. case CMD_GET_RPI_CN:
  1016. case CMD_XMIT_ELS_RSP_CX:
  1017. case CMD_GET_RPI_CR:
  1018. case CMD_FCP_IWRITE_CR:
  1019. case CMD_FCP_IWRITE_CX:
  1020. case CMD_FCP_IREAD_CR:
  1021. case CMD_FCP_IREAD_CX:
  1022. case CMD_FCP_ICMND_CR:
  1023. case CMD_FCP_ICMND_CX:
  1024. case CMD_FCP_TSEND_CX:
  1025. case CMD_FCP_TRSP_CX:
  1026. case CMD_FCP_TRECEIVE_CX:
  1027. case CMD_FCP_AUTO_TRSP_CX:
  1028. case CMD_ADAPTER_MSG:
  1029. case CMD_ADAPTER_DUMP:
  1030. case CMD_XMIT_SEQUENCE64_CR:
  1031. case CMD_XMIT_SEQUENCE64_CX:
  1032. case CMD_XMIT_BCAST64_CN:
  1033. case CMD_XMIT_BCAST64_CX:
  1034. case CMD_ELS_REQUEST64_CR:
  1035. case CMD_ELS_REQUEST64_CX:
  1036. case CMD_FCP_IWRITE64_CR:
  1037. case CMD_FCP_IWRITE64_CX:
  1038. case CMD_FCP_IREAD64_CR:
  1039. case CMD_FCP_IREAD64_CX:
  1040. case CMD_FCP_ICMND64_CR:
  1041. case CMD_FCP_ICMND64_CX:
  1042. case CMD_FCP_TSEND64_CX:
  1043. case CMD_FCP_TRSP64_CX:
  1044. case CMD_FCP_TRECEIVE64_CX:
  1045. case CMD_GEN_REQUEST64_CR:
  1046. case CMD_GEN_REQUEST64_CX:
  1047. case CMD_XMIT_ELS_RSP64_CX:
  1048. case DSSCMD_IWRITE64_CR:
  1049. case DSSCMD_IWRITE64_CX:
  1050. case DSSCMD_IREAD64_CR:
  1051. case DSSCMD_IREAD64_CX:
  1052. type = LPFC_SOL_IOCB;
  1053. break;
  1054. case CMD_ABORT_XRI_CN:
  1055. case CMD_ABORT_XRI_CX:
  1056. case CMD_CLOSE_XRI_CN:
  1057. case CMD_CLOSE_XRI_CX:
  1058. case CMD_XRI_ABORTED_CX:
  1059. case CMD_ABORT_MXRI64_CN:
  1060. case CMD_XMIT_BLS_RSP64_CX:
  1061. type = LPFC_ABORT_IOCB;
  1062. break;
  1063. case CMD_RCV_SEQUENCE_CX:
  1064. case CMD_RCV_ELS_REQ_CX:
  1065. case CMD_RCV_SEQUENCE64_CX:
  1066. case CMD_RCV_ELS_REQ64_CX:
  1067. case CMD_ASYNC_STATUS:
  1068. case CMD_IOCB_RCV_SEQ64_CX:
  1069. case CMD_IOCB_RCV_ELS64_CX:
  1070. case CMD_IOCB_RCV_CONT64_CX:
  1071. case CMD_IOCB_RET_XRI64_CX:
  1072. type = LPFC_UNSOL_IOCB;
  1073. break;
  1074. case CMD_IOCB_XMIT_MSEQ64_CR:
  1075. case CMD_IOCB_XMIT_MSEQ64_CX:
  1076. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1077. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1078. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1079. case CMD_IOCB_ABORT_EXTENDED_CN:
  1080. case CMD_IOCB_RET_HBQE64_CN:
  1081. case CMD_IOCB_FCP_IBIDIR64_CR:
  1082. case CMD_IOCB_FCP_IBIDIR64_CX:
  1083. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1084. case CMD_IOCB_LOGENTRY_CN:
  1085. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1086. printk("%s - Unhandled SLI-3 Command x%x\n",
  1087. __func__, iocb_cmnd);
  1088. type = LPFC_UNKNOWN_IOCB;
  1089. break;
  1090. default:
  1091. type = LPFC_UNKNOWN_IOCB;
  1092. break;
  1093. }
  1094. return type;
  1095. }
  1096. /**
  1097. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1098. * @phba: Pointer to HBA context object.
  1099. *
  1100. * This function is called from SLI initialization code
  1101. * to configure every ring of the HBA's SLI interface. The
  1102. * caller is not required to hold any lock. This function issues
  1103. * a config_ring mailbox command for each ring.
  1104. * This function returns zero if successful else returns a negative
  1105. * error code.
  1106. **/
  1107. static int
  1108. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1109. {
  1110. struct lpfc_sli *psli = &phba->sli;
  1111. LPFC_MBOXQ_t *pmb;
  1112. MAILBOX_t *pmbox;
  1113. int i, rc, ret = 0;
  1114. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1115. if (!pmb)
  1116. return -ENOMEM;
  1117. pmbox = &pmb->u.mb;
  1118. phba->link_state = LPFC_INIT_MBX_CMDS;
  1119. for (i = 0; i < psli->num_rings; i++) {
  1120. lpfc_config_ring(phba, i, pmb);
  1121. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1122. if (rc != MBX_SUCCESS) {
  1123. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1124. "0446 Adapter failed to init (%d), "
  1125. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1126. "ring %d\n",
  1127. rc, pmbox->mbxCommand,
  1128. pmbox->mbxStatus, i);
  1129. phba->link_state = LPFC_HBA_ERROR;
  1130. ret = -ENXIO;
  1131. break;
  1132. }
  1133. }
  1134. mempool_free(pmb, phba->mbox_mem_pool);
  1135. return ret;
  1136. }
  1137. /**
  1138. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1139. * @phba: Pointer to HBA context object.
  1140. * @pring: Pointer to driver SLI ring object.
  1141. * @piocb: Pointer to the driver iocb object.
  1142. *
  1143. * This function is called with hbalock held. The function adds the
  1144. * new iocb to txcmplq of the given ring. This function always returns
  1145. * 0. If this function is called for ELS ring, this function checks if
  1146. * there is a vport associated with the ELS command. This function also
  1147. * starts els_tmofunc timer if this is an ELS command.
  1148. **/
  1149. static int
  1150. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1151. struct lpfc_iocbq *piocb)
  1152. {
  1153. list_add_tail(&piocb->list, &pring->txcmplq);
  1154. piocb->iocb_flag |= LPFC_IO_ON_Q;
  1155. pring->txcmplq_cnt++;
  1156. if (pring->txcmplq_cnt > pring->txcmplq_max)
  1157. pring->txcmplq_max = pring->txcmplq_cnt;
  1158. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1159. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1160. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1161. if (!piocb->vport)
  1162. BUG();
  1163. else
  1164. mod_timer(&piocb->vport->els_tmofunc,
  1165. jiffies + HZ * (phba->fc_ratov << 1));
  1166. }
  1167. return 0;
  1168. }
  1169. /**
  1170. * lpfc_sli_ringtx_get - Get first element of the txq
  1171. * @phba: Pointer to HBA context object.
  1172. * @pring: Pointer to driver SLI ring object.
  1173. *
  1174. * This function is called with hbalock held to get next
  1175. * iocb in txq of the given ring. If there is any iocb in
  1176. * the txq, the function returns first iocb in the list after
  1177. * removing the iocb from the list, else it returns NULL.
  1178. **/
  1179. struct lpfc_iocbq *
  1180. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1181. {
  1182. struct lpfc_iocbq *cmd_iocb;
  1183. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1184. if (cmd_iocb != NULL)
  1185. pring->txq_cnt--;
  1186. return cmd_iocb;
  1187. }
  1188. /**
  1189. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1190. * @phba: Pointer to HBA context object.
  1191. * @pring: Pointer to driver SLI ring object.
  1192. *
  1193. * This function is called with hbalock held and the caller must post the
  1194. * iocb without releasing the lock. If the caller releases the lock,
  1195. * iocb slot returned by the function is not guaranteed to be available.
  1196. * The function returns pointer to the next available iocb slot if there
  1197. * is available slot in the ring, else it returns NULL.
  1198. * If the get index of the ring is ahead of the put index, the function
  1199. * will post an error attention event to the worker thread to take the
  1200. * HBA to offline state.
  1201. **/
  1202. static IOCB_t *
  1203. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1204. {
  1205. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1206. uint32_t max_cmd_idx = pring->numCiocb;
  1207. if ((pring->next_cmdidx == pring->cmdidx) &&
  1208. (++pring->next_cmdidx >= max_cmd_idx))
  1209. pring->next_cmdidx = 0;
  1210. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  1211. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1212. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  1213. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1214. "0315 Ring %d issue: portCmdGet %d "
  1215. "is bigger than cmd ring %d\n",
  1216. pring->ringno,
  1217. pring->local_getidx, max_cmd_idx);
  1218. phba->link_state = LPFC_HBA_ERROR;
  1219. /*
  1220. * All error attention handlers are posted to
  1221. * worker thread
  1222. */
  1223. phba->work_ha |= HA_ERATT;
  1224. phba->work_hs = HS_FFER3;
  1225. lpfc_worker_wake_up(phba);
  1226. return NULL;
  1227. }
  1228. if (pring->local_getidx == pring->next_cmdidx)
  1229. return NULL;
  1230. }
  1231. return lpfc_cmd_iocb(phba, pring);
  1232. }
  1233. /**
  1234. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1235. * @phba: Pointer to HBA context object.
  1236. * @iocbq: Pointer to driver iocb object.
  1237. *
  1238. * This function gets an iotag for the iocb. If there is no unused iotag and
  1239. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1240. * array and assigns a new iotag.
  1241. * The function returns the allocated iotag if successful, else returns zero.
  1242. * Zero is not a valid iotag.
  1243. * The caller is not required to hold any lock.
  1244. **/
  1245. uint16_t
  1246. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1247. {
  1248. struct lpfc_iocbq **new_arr;
  1249. struct lpfc_iocbq **old_arr;
  1250. size_t new_len;
  1251. struct lpfc_sli *psli = &phba->sli;
  1252. uint16_t iotag;
  1253. spin_lock_irq(&phba->hbalock);
  1254. iotag = psli->last_iotag;
  1255. if(++iotag < psli->iocbq_lookup_len) {
  1256. psli->last_iotag = iotag;
  1257. psli->iocbq_lookup[iotag] = iocbq;
  1258. spin_unlock_irq(&phba->hbalock);
  1259. iocbq->iotag = iotag;
  1260. return iotag;
  1261. } else if (psli->iocbq_lookup_len < (0xffff
  1262. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1263. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1264. spin_unlock_irq(&phba->hbalock);
  1265. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1266. GFP_KERNEL);
  1267. if (new_arr) {
  1268. spin_lock_irq(&phba->hbalock);
  1269. old_arr = psli->iocbq_lookup;
  1270. if (new_len <= psli->iocbq_lookup_len) {
  1271. /* highly unprobable case */
  1272. kfree(new_arr);
  1273. iotag = psli->last_iotag;
  1274. if(++iotag < psli->iocbq_lookup_len) {
  1275. psli->last_iotag = iotag;
  1276. psli->iocbq_lookup[iotag] = iocbq;
  1277. spin_unlock_irq(&phba->hbalock);
  1278. iocbq->iotag = iotag;
  1279. return iotag;
  1280. }
  1281. spin_unlock_irq(&phba->hbalock);
  1282. return 0;
  1283. }
  1284. if (psli->iocbq_lookup)
  1285. memcpy(new_arr, old_arr,
  1286. ((psli->last_iotag + 1) *
  1287. sizeof (struct lpfc_iocbq *)));
  1288. psli->iocbq_lookup = new_arr;
  1289. psli->iocbq_lookup_len = new_len;
  1290. psli->last_iotag = iotag;
  1291. psli->iocbq_lookup[iotag] = iocbq;
  1292. spin_unlock_irq(&phba->hbalock);
  1293. iocbq->iotag = iotag;
  1294. kfree(old_arr);
  1295. return iotag;
  1296. }
  1297. } else
  1298. spin_unlock_irq(&phba->hbalock);
  1299. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1300. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1301. psli->last_iotag);
  1302. return 0;
  1303. }
  1304. /**
  1305. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1306. * @phba: Pointer to HBA context object.
  1307. * @pring: Pointer to driver SLI ring object.
  1308. * @iocb: Pointer to iocb slot in the ring.
  1309. * @nextiocb: Pointer to driver iocb object which need to be
  1310. * posted to firmware.
  1311. *
  1312. * This function is called with hbalock held to post a new iocb to
  1313. * the firmware. This function copies the new iocb to ring iocb slot and
  1314. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1315. * a completion call back for this iocb else the function will free the
  1316. * iocb object.
  1317. **/
  1318. static void
  1319. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1320. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1321. {
  1322. /*
  1323. * Set up an iotag
  1324. */
  1325. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1326. if (pring->ringno == LPFC_ELS_RING) {
  1327. lpfc_debugfs_slow_ring_trc(phba,
  1328. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1329. *(((uint32_t *) &nextiocb->iocb) + 4),
  1330. *(((uint32_t *) &nextiocb->iocb) + 6),
  1331. *(((uint32_t *) &nextiocb->iocb) + 7));
  1332. }
  1333. /*
  1334. * Issue iocb command to adapter
  1335. */
  1336. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1337. wmb();
  1338. pring->stats.iocb_cmd++;
  1339. /*
  1340. * If there is no completion routine to call, we can release the
  1341. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1342. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1343. */
  1344. if (nextiocb->iocb_cmpl)
  1345. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1346. else
  1347. __lpfc_sli_release_iocbq(phba, nextiocb);
  1348. /*
  1349. * Let the HBA know what IOCB slot will be the next one the
  1350. * driver will put a command into.
  1351. */
  1352. pring->cmdidx = pring->next_cmdidx;
  1353. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1354. }
  1355. /**
  1356. * lpfc_sli_update_full_ring - Update the chip attention register
  1357. * @phba: Pointer to HBA context object.
  1358. * @pring: Pointer to driver SLI ring object.
  1359. *
  1360. * The caller is not required to hold any lock for calling this function.
  1361. * This function updates the chip attention bits for the ring to inform firmware
  1362. * that there are pending work to be done for this ring and requests an
  1363. * interrupt when there is space available in the ring. This function is
  1364. * called when the driver is unable to post more iocbs to the ring due
  1365. * to unavailability of space in the ring.
  1366. **/
  1367. static void
  1368. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1369. {
  1370. int ringno = pring->ringno;
  1371. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1372. wmb();
  1373. /*
  1374. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1375. * The HBA will tell us when an IOCB entry is available.
  1376. */
  1377. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1378. readl(phba->CAregaddr); /* flush */
  1379. pring->stats.iocb_cmd_full++;
  1380. }
  1381. /**
  1382. * lpfc_sli_update_ring - Update chip attention register
  1383. * @phba: Pointer to HBA context object.
  1384. * @pring: Pointer to driver SLI ring object.
  1385. *
  1386. * This function updates the chip attention register bit for the
  1387. * given ring to inform HBA that there is more work to be done
  1388. * in this ring. The caller is not required to hold any lock.
  1389. **/
  1390. static void
  1391. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1392. {
  1393. int ringno = pring->ringno;
  1394. /*
  1395. * Tell the HBA that there is work to do in this ring.
  1396. */
  1397. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1398. wmb();
  1399. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1400. readl(phba->CAregaddr); /* flush */
  1401. }
  1402. }
  1403. /**
  1404. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1405. * @phba: Pointer to HBA context object.
  1406. * @pring: Pointer to driver SLI ring object.
  1407. *
  1408. * This function is called with hbalock held to post pending iocbs
  1409. * in the txq to the firmware. This function is called when driver
  1410. * detects space available in the ring.
  1411. **/
  1412. static void
  1413. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1414. {
  1415. IOCB_t *iocb;
  1416. struct lpfc_iocbq *nextiocb;
  1417. /*
  1418. * Check to see if:
  1419. * (a) there is anything on the txq to send
  1420. * (b) link is up
  1421. * (c) link attention events can be processed (fcp ring only)
  1422. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1423. */
  1424. if (pring->txq_cnt &&
  1425. lpfc_is_link_up(phba) &&
  1426. (pring->ringno != phba->sli.fcp_ring ||
  1427. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1428. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1429. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1430. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1431. if (iocb)
  1432. lpfc_sli_update_ring(phba, pring);
  1433. else
  1434. lpfc_sli_update_full_ring(phba, pring);
  1435. }
  1436. return;
  1437. }
  1438. /**
  1439. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1440. * @phba: Pointer to HBA context object.
  1441. * @hbqno: HBQ number.
  1442. *
  1443. * This function is called with hbalock held to get the next
  1444. * available slot for the given HBQ. If there is free slot
  1445. * available for the HBQ it will return pointer to the next available
  1446. * HBQ entry else it will return NULL.
  1447. **/
  1448. static struct lpfc_hbq_entry *
  1449. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1450. {
  1451. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1452. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1453. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1454. hbqp->next_hbqPutIdx = 0;
  1455. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1456. uint32_t raw_index = phba->hbq_get[hbqno];
  1457. uint32_t getidx = le32_to_cpu(raw_index);
  1458. hbqp->local_hbqGetIdx = getidx;
  1459. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1460. lpfc_printf_log(phba, KERN_ERR,
  1461. LOG_SLI | LOG_VPORT,
  1462. "1802 HBQ %d: local_hbqGetIdx "
  1463. "%u is > than hbqp->entry_count %u\n",
  1464. hbqno, hbqp->local_hbqGetIdx,
  1465. hbqp->entry_count);
  1466. phba->link_state = LPFC_HBA_ERROR;
  1467. return NULL;
  1468. }
  1469. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1470. return NULL;
  1471. }
  1472. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1473. hbqp->hbqPutIdx;
  1474. }
  1475. /**
  1476. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1477. * @phba: Pointer to HBA context object.
  1478. *
  1479. * This function is called with no lock held to free all the
  1480. * hbq buffers while uninitializing the SLI interface. It also
  1481. * frees the HBQ buffers returned by the firmware but not yet
  1482. * processed by the upper layers.
  1483. **/
  1484. void
  1485. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1486. {
  1487. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1488. struct hbq_dmabuf *hbq_buf;
  1489. unsigned long flags;
  1490. int i, hbq_count;
  1491. uint32_t hbqno;
  1492. hbq_count = lpfc_sli_hbq_count();
  1493. /* Return all memory used by all HBQs */
  1494. spin_lock_irqsave(&phba->hbalock, flags);
  1495. for (i = 0; i < hbq_count; ++i) {
  1496. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1497. &phba->hbqs[i].hbq_buffer_list, list) {
  1498. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1499. list_del(&hbq_buf->dbuf.list);
  1500. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1501. }
  1502. phba->hbqs[i].buffer_count = 0;
  1503. }
  1504. /* Return all HBQ buffer that are in-fly */
  1505. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1506. list) {
  1507. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1508. list_del(&hbq_buf->dbuf.list);
  1509. if (hbq_buf->tag == -1) {
  1510. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1511. (phba, hbq_buf);
  1512. } else {
  1513. hbqno = hbq_buf->tag >> 16;
  1514. if (hbqno >= LPFC_MAX_HBQS)
  1515. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1516. (phba, hbq_buf);
  1517. else
  1518. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1519. hbq_buf);
  1520. }
  1521. }
  1522. /* Mark the HBQs not in use */
  1523. phba->hbq_in_use = 0;
  1524. spin_unlock_irqrestore(&phba->hbalock, flags);
  1525. }
  1526. /**
  1527. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1528. * @phba: Pointer to HBA context object.
  1529. * @hbqno: HBQ number.
  1530. * @hbq_buf: Pointer to HBQ buffer.
  1531. *
  1532. * This function is called with the hbalock held to post a
  1533. * hbq buffer to the firmware. If the function finds an empty
  1534. * slot in the HBQ, it will post the buffer. The function will return
  1535. * pointer to the hbq entry if it successfully post the buffer
  1536. * else it will return NULL.
  1537. **/
  1538. static int
  1539. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1540. struct hbq_dmabuf *hbq_buf)
  1541. {
  1542. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1543. }
  1544. /**
  1545. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1546. * @phba: Pointer to HBA context object.
  1547. * @hbqno: HBQ number.
  1548. * @hbq_buf: Pointer to HBQ buffer.
  1549. *
  1550. * This function is called with the hbalock held to post a hbq buffer to the
  1551. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1552. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1553. * it successfully post the buffer else it will return an error.
  1554. **/
  1555. static int
  1556. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1557. struct hbq_dmabuf *hbq_buf)
  1558. {
  1559. struct lpfc_hbq_entry *hbqe;
  1560. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1561. /* Get next HBQ entry slot to use */
  1562. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1563. if (hbqe) {
  1564. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1565. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1566. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1567. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1568. hbqe->bde.tus.f.bdeFlags = 0;
  1569. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1570. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1571. /* Sync SLIM */
  1572. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1573. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1574. /* flush */
  1575. readl(phba->hbq_put + hbqno);
  1576. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1577. return 0;
  1578. } else
  1579. return -ENOMEM;
  1580. }
  1581. /**
  1582. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1583. * @phba: Pointer to HBA context object.
  1584. * @hbqno: HBQ number.
  1585. * @hbq_buf: Pointer to HBQ buffer.
  1586. *
  1587. * This function is called with the hbalock held to post an RQE to the SLI4
  1588. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1589. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1590. **/
  1591. static int
  1592. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1593. struct hbq_dmabuf *hbq_buf)
  1594. {
  1595. int rc;
  1596. struct lpfc_rqe hrqe;
  1597. struct lpfc_rqe drqe;
  1598. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1599. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1600. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1601. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1602. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1603. &hrqe, &drqe);
  1604. if (rc < 0)
  1605. return rc;
  1606. hbq_buf->tag = rc;
  1607. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1608. return 0;
  1609. }
  1610. /* HBQ for ELS and CT traffic. */
  1611. static struct lpfc_hbq_init lpfc_els_hbq = {
  1612. .rn = 1,
  1613. .entry_count = 256,
  1614. .mask_count = 0,
  1615. .profile = 0,
  1616. .ring_mask = (1 << LPFC_ELS_RING),
  1617. .buffer_count = 0,
  1618. .init_count = 40,
  1619. .add_count = 40,
  1620. };
  1621. /* HBQ for the extra ring if needed */
  1622. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1623. .rn = 1,
  1624. .entry_count = 200,
  1625. .mask_count = 0,
  1626. .profile = 0,
  1627. .ring_mask = (1 << LPFC_EXTRA_RING),
  1628. .buffer_count = 0,
  1629. .init_count = 0,
  1630. .add_count = 5,
  1631. };
  1632. /* Array of HBQs */
  1633. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1634. &lpfc_els_hbq,
  1635. &lpfc_extra_hbq,
  1636. };
  1637. /**
  1638. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1639. * @phba: Pointer to HBA context object.
  1640. * @hbqno: HBQ number.
  1641. * @count: Number of HBQ buffers to be posted.
  1642. *
  1643. * This function is called with no lock held to post more hbq buffers to the
  1644. * given HBQ. The function returns the number of HBQ buffers successfully
  1645. * posted.
  1646. **/
  1647. static int
  1648. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1649. {
  1650. uint32_t i, posted = 0;
  1651. unsigned long flags;
  1652. struct hbq_dmabuf *hbq_buffer;
  1653. LIST_HEAD(hbq_buf_list);
  1654. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1655. return 0;
  1656. if ((phba->hbqs[hbqno].buffer_count + count) >
  1657. lpfc_hbq_defs[hbqno]->entry_count)
  1658. count = lpfc_hbq_defs[hbqno]->entry_count -
  1659. phba->hbqs[hbqno].buffer_count;
  1660. if (!count)
  1661. return 0;
  1662. /* Allocate HBQ entries */
  1663. for (i = 0; i < count; i++) {
  1664. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1665. if (!hbq_buffer)
  1666. break;
  1667. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1668. }
  1669. /* Check whether HBQ is still in use */
  1670. spin_lock_irqsave(&phba->hbalock, flags);
  1671. if (!phba->hbq_in_use)
  1672. goto err;
  1673. while (!list_empty(&hbq_buf_list)) {
  1674. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1675. dbuf.list);
  1676. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1677. (hbqno << 16));
  1678. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1679. phba->hbqs[hbqno].buffer_count++;
  1680. posted++;
  1681. } else
  1682. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1683. }
  1684. spin_unlock_irqrestore(&phba->hbalock, flags);
  1685. return posted;
  1686. err:
  1687. spin_unlock_irqrestore(&phba->hbalock, flags);
  1688. while (!list_empty(&hbq_buf_list)) {
  1689. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1690. dbuf.list);
  1691. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1692. }
  1693. return 0;
  1694. }
  1695. /**
  1696. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1697. * @phba: Pointer to HBA context object.
  1698. * @qno: HBQ number.
  1699. *
  1700. * This function posts more buffers to the HBQ. This function
  1701. * is called with no lock held. The function returns the number of HBQ entries
  1702. * successfully allocated.
  1703. **/
  1704. int
  1705. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1706. {
  1707. if (phba->sli_rev == LPFC_SLI_REV4)
  1708. return 0;
  1709. else
  1710. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1711. lpfc_hbq_defs[qno]->add_count);
  1712. }
  1713. /**
  1714. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1715. * @phba: Pointer to HBA context object.
  1716. * @qno: HBQ queue number.
  1717. *
  1718. * This function is called from SLI initialization code path with
  1719. * no lock held to post initial HBQ buffers to firmware. The
  1720. * function returns the number of HBQ entries successfully allocated.
  1721. **/
  1722. static int
  1723. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1724. {
  1725. if (phba->sli_rev == LPFC_SLI_REV4)
  1726. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1727. lpfc_hbq_defs[qno]->entry_count);
  1728. else
  1729. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1730. lpfc_hbq_defs[qno]->init_count);
  1731. }
  1732. /**
  1733. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1734. * @phba: Pointer to HBA context object.
  1735. * @hbqno: HBQ number.
  1736. *
  1737. * This function removes the first hbq buffer on an hbq list and returns a
  1738. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1739. **/
  1740. static struct hbq_dmabuf *
  1741. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1742. {
  1743. struct lpfc_dmabuf *d_buf;
  1744. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1745. if (!d_buf)
  1746. return NULL;
  1747. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1748. }
  1749. /**
  1750. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1751. * @phba: Pointer to HBA context object.
  1752. * @tag: Tag of the hbq buffer.
  1753. *
  1754. * This function is called with hbalock held. This function searches
  1755. * for the hbq buffer associated with the given tag in the hbq buffer
  1756. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1757. * it returns NULL.
  1758. **/
  1759. static struct hbq_dmabuf *
  1760. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1761. {
  1762. struct lpfc_dmabuf *d_buf;
  1763. struct hbq_dmabuf *hbq_buf;
  1764. uint32_t hbqno;
  1765. hbqno = tag >> 16;
  1766. if (hbqno >= LPFC_MAX_HBQS)
  1767. return NULL;
  1768. spin_lock_irq(&phba->hbalock);
  1769. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1770. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1771. if (hbq_buf->tag == tag) {
  1772. spin_unlock_irq(&phba->hbalock);
  1773. return hbq_buf;
  1774. }
  1775. }
  1776. spin_unlock_irq(&phba->hbalock);
  1777. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1778. "1803 Bad hbq tag. Data: x%x x%x\n",
  1779. tag, phba->hbqs[tag >> 16].buffer_count);
  1780. return NULL;
  1781. }
  1782. /**
  1783. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1784. * @phba: Pointer to HBA context object.
  1785. * @hbq_buffer: Pointer to HBQ buffer.
  1786. *
  1787. * This function is called with hbalock. This function gives back
  1788. * the hbq buffer to firmware. If the HBQ does not have space to
  1789. * post the buffer, it will free the buffer.
  1790. **/
  1791. void
  1792. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1793. {
  1794. uint32_t hbqno;
  1795. if (hbq_buffer) {
  1796. hbqno = hbq_buffer->tag >> 16;
  1797. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1798. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1799. }
  1800. }
  1801. /**
  1802. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1803. * @mbxCommand: mailbox command code.
  1804. *
  1805. * This function is called by the mailbox event handler function to verify
  1806. * that the completed mailbox command is a legitimate mailbox command. If the
  1807. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1808. * and the mailbox event handler will take the HBA offline.
  1809. **/
  1810. static int
  1811. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1812. {
  1813. uint8_t ret;
  1814. switch (mbxCommand) {
  1815. case MBX_LOAD_SM:
  1816. case MBX_READ_NV:
  1817. case MBX_WRITE_NV:
  1818. case MBX_WRITE_VPARMS:
  1819. case MBX_RUN_BIU_DIAG:
  1820. case MBX_INIT_LINK:
  1821. case MBX_DOWN_LINK:
  1822. case MBX_CONFIG_LINK:
  1823. case MBX_CONFIG_RING:
  1824. case MBX_RESET_RING:
  1825. case MBX_READ_CONFIG:
  1826. case MBX_READ_RCONFIG:
  1827. case MBX_READ_SPARM:
  1828. case MBX_READ_STATUS:
  1829. case MBX_READ_RPI:
  1830. case MBX_READ_XRI:
  1831. case MBX_READ_REV:
  1832. case MBX_READ_LNK_STAT:
  1833. case MBX_REG_LOGIN:
  1834. case MBX_UNREG_LOGIN:
  1835. case MBX_CLEAR_LA:
  1836. case MBX_DUMP_MEMORY:
  1837. case MBX_DUMP_CONTEXT:
  1838. case MBX_RUN_DIAGS:
  1839. case MBX_RESTART:
  1840. case MBX_UPDATE_CFG:
  1841. case MBX_DOWN_LOAD:
  1842. case MBX_DEL_LD_ENTRY:
  1843. case MBX_RUN_PROGRAM:
  1844. case MBX_SET_MASK:
  1845. case MBX_SET_VARIABLE:
  1846. case MBX_UNREG_D_ID:
  1847. case MBX_KILL_BOARD:
  1848. case MBX_CONFIG_FARP:
  1849. case MBX_BEACON:
  1850. case MBX_LOAD_AREA:
  1851. case MBX_RUN_BIU_DIAG64:
  1852. case MBX_CONFIG_PORT:
  1853. case MBX_READ_SPARM64:
  1854. case MBX_READ_RPI64:
  1855. case MBX_REG_LOGIN64:
  1856. case MBX_READ_TOPOLOGY:
  1857. case MBX_WRITE_WWN:
  1858. case MBX_SET_DEBUG:
  1859. case MBX_LOAD_EXP_ROM:
  1860. case MBX_ASYNCEVT_ENABLE:
  1861. case MBX_REG_VPI:
  1862. case MBX_UNREG_VPI:
  1863. case MBX_HEARTBEAT:
  1864. case MBX_PORT_CAPABILITIES:
  1865. case MBX_PORT_IOV_CONTROL:
  1866. case MBX_SLI4_CONFIG:
  1867. case MBX_SLI4_REQ_FTRS:
  1868. case MBX_REG_FCFI:
  1869. case MBX_UNREG_FCFI:
  1870. case MBX_REG_VFI:
  1871. case MBX_UNREG_VFI:
  1872. case MBX_INIT_VPI:
  1873. case MBX_INIT_VFI:
  1874. case MBX_RESUME_RPI:
  1875. case MBX_READ_EVENT_LOG_STATUS:
  1876. case MBX_READ_EVENT_LOG:
  1877. case MBX_SECURITY_MGMT:
  1878. case MBX_AUTH_PORT:
  1879. ret = mbxCommand;
  1880. break;
  1881. default:
  1882. ret = MBX_SHUTDOWN;
  1883. break;
  1884. }
  1885. return ret;
  1886. }
  1887. /**
  1888. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1889. * @phba: Pointer to HBA context object.
  1890. * @pmboxq: Pointer to mailbox command.
  1891. *
  1892. * This is completion handler function for mailbox commands issued from
  1893. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1894. * mailbox event handler function with no lock held. This function
  1895. * will wake up thread waiting on the wait queue pointed by context1
  1896. * of the mailbox.
  1897. **/
  1898. void
  1899. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1900. {
  1901. wait_queue_head_t *pdone_q;
  1902. unsigned long drvr_flag;
  1903. /*
  1904. * If pdone_q is empty, the driver thread gave up waiting and
  1905. * continued running.
  1906. */
  1907. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1908. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1909. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1910. if (pdone_q)
  1911. wake_up_interruptible(pdone_q);
  1912. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1913. return;
  1914. }
  1915. /**
  1916. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1917. * @phba: Pointer to HBA context object.
  1918. * @pmb: Pointer to mailbox object.
  1919. *
  1920. * This function is the default mailbox completion handler. It
  1921. * frees the memory resources associated with the completed mailbox
  1922. * command. If the completed command is a REG_LOGIN mailbox command,
  1923. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1924. **/
  1925. void
  1926. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1927. {
  1928. struct lpfc_vport *vport = pmb->vport;
  1929. struct lpfc_dmabuf *mp;
  1930. struct lpfc_nodelist *ndlp;
  1931. struct Scsi_Host *shost;
  1932. uint16_t rpi, vpi;
  1933. int rc;
  1934. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1935. if (mp) {
  1936. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1937. kfree(mp);
  1938. }
  1939. /*
  1940. * If a REG_LOGIN succeeded after node is destroyed or node
  1941. * is in re-discovery driver need to cleanup the RPI.
  1942. */
  1943. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1944. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1945. !pmb->u.mb.mbxStatus) {
  1946. rpi = pmb->u.mb.un.varWords[0];
  1947. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1948. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1949. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1950. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1951. if (rc != MBX_NOT_FINISHED)
  1952. return;
  1953. }
  1954. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  1955. !(phba->pport->load_flag & FC_UNLOADING) &&
  1956. !pmb->u.mb.mbxStatus) {
  1957. shost = lpfc_shost_from_vport(vport);
  1958. spin_lock_irq(shost->host_lock);
  1959. vport->vpi_state |= LPFC_VPI_REGISTERED;
  1960. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  1961. spin_unlock_irq(shost->host_lock);
  1962. }
  1963. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  1964. ndlp = (struct lpfc_nodelist *)pmb->context2;
  1965. lpfc_nlp_put(ndlp);
  1966. pmb->context2 = NULL;
  1967. }
  1968. /* Check security permission status on INIT_LINK mailbox command */
  1969. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  1970. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  1971. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1972. "2860 SLI authentication is required "
  1973. "for INIT_LINK but has not done yet\n");
  1974. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  1975. lpfc_sli4_mbox_cmd_free(phba, pmb);
  1976. else
  1977. mempool_free(pmb, phba->mbox_mem_pool);
  1978. }
  1979. /**
  1980. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  1981. * @phba: Pointer to HBA context object.
  1982. *
  1983. * This function is called with no lock held. This function processes all
  1984. * the completed mailbox commands and gives it to upper layers. The interrupt
  1985. * service routine processes mailbox completion interrupt and adds completed
  1986. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  1987. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  1988. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  1989. * function returns the mailbox commands to the upper layer by calling the
  1990. * completion handler function of each mailbox.
  1991. **/
  1992. int
  1993. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  1994. {
  1995. MAILBOX_t *pmbox;
  1996. LPFC_MBOXQ_t *pmb;
  1997. int rc;
  1998. LIST_HEAD(cmplq);
  1999. phba->sli.slistat.mbox_event++;
  2000. /* Get all completed mailboxe buffers into the cmplq */
  2001. spin_lock_irq(&phba->hbalock);
  2002. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2003. spin_unlock_irq(&phba->hbalock);
  2004. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2005. do {
  2006. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2007. if (pmb == NULL)
  2008. break;
  2009. pmbox = &pmb->u.mb;
  2010. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2011. if (pmb->vport) {
  2012. lpfc_debugfs_disc_trc(pmb->vport,
  2013. LPFC_DISC_TRC_MBOX_VPORT,
  2014. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2015. (uint32_t)pmbox->mbxCommand,
  2016. pmbox->un.varWords[0],
  2017. pmbox->un.varWords[1]);
  2018. }
  2019. else {
  2020. lpfc_debugfs_disc_trc(phba->pport,
  2021. LPFC_DISC_TRC_MBOX,
  2022. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2023. (uint32_t)pmbox->mbxCommand,
  2024. pmbox->un.varWords[0],
  2025. pmbox->un.varWords[1]);
  2026. }
  2027. }
  2028. /*
  2029. * It is a fatal error if unknown mbox command completion.
  2030. */
  2031. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2032. MBX_SHUTDOWN) {
  2033. /* Unknown mailbox command compl */
  2034. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2035. "(%d):0323 Unknown Mailbox command "
  2036. "x%x (x%x/x%x) Cmpl\n",
  2037. pmb->vport ? pmb->vport->vpi : 0,
  2038. pmbox->mbxCommand,
  2039. lpfc_sli_config_mbox_subsys_get(phba,
  2040. pmb),
  2041. lpfc_sli_config_mbox_opcode_get(phba,
  2042. pmb));
  2043. phba->link_state = LPFC_HBA_ERROR;
  2044. phba->work_hs = HS_FFER3;
  2045. lpfc_handle_eratt(phba);
  2046. continue;
  2047. }
  2048. if (pmbox->mbxStatus) {
  2049. phba->sli.slistat.mbox_stat_err++;
  2050. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2051. /* Mbox cmd cmpl error - RETRYing */
  2052. lpfc_printf_log(phba, KERN_INFO,
  2053. LOG_MBOX | LOG_SLI,
  2054. "(%d):0305 Mbox cmd cmpl "
  2055. "error - RETRYing Data: x%x "
  2056. "(x%x/x%x) x%x x%x x%x\n",
  2057. pmb->vport ? pmb->vport->vpi : 0,
  2058. pmbox->mbxCommand,
  2059. lpfc_sli_config_mbox_subsys_get(phba,
  2060. pmb),
  2061. lpfc_sli_config_mbox_opcode_get(phba,
  2062. pmb),
  2063. pmbox->mbxStatus,
  2064. pmbox->un.varWords[0],
  2065. pmb->vport->port_state);
  2066. pmbox->mbxStatus = 0;
  2067. pmbox->mbxOwner = OWN_HOST;
  2068. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2069. if (rc != MBX_NOT_FINISHED)
  2070. continue;
  2071. }
  2072. }
  2073. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2074. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2075. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2076. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2077. pmb->vport ? pmb->vport->vpi : 0,
  2078. pmbox->mbxCommand,
  2079. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2080. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2081. pmb->mbox_cmpl,
  2082. *((uint32_t *) pmbox),
  2083. pmbox->un.varWords[0],
  2084. pmbox->un.varWords[1],
  2085. pmbox->un.varWords[2],
  2086. pmbox->un.varWords[3],
  2087. pmbox->un.varWords[4],
  2088. pmbox->un.varWords[5],
  2089. pmbox->un.varWords[6],
  2090. pmbox->un.varWords[7]);
  2091. if (pmb->mbox_cmpl)
  2092. pmb->mbox_cmpl(phba,pmb);
  2093. } while (1);
  2094. return 0;
  2095. }
  2096. /**
  2097. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2098. * @phba: Pointer to HBA context object.
  2099. * @pring: Pointer to driver SLI ring object.
  2100. * @tag: buffer tag.
  2101. *
  2102. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2103. * is set in the tag the buffer is posted for a particular exchange,
  2104. * the function will return the buffer without replacing the buffer.
  2105. * If the buffer is for unsolicited ELS or CT traffic, this function
  2106. * returns the buffer and also posts another buffer to the firmware.
  2107. **/
  2108. static struct lpfc_dmabuf *
  2109. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2110. struct lpfc_sli_ring *pring,
  2111. uint32_t tag)
  2112. {
  2113. struct hbq_dmabuf *hbq_entry;
  2114. if (tag & QUE_BUFTAG_BIT)
  2115. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2116. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2117. if (!hbq_entry)
  2118. return NULL;
  2119. return &hbq_entry->dbuf;
  2120. }
  2121. /**
  2122. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2123. * @phba: Pointer to HBA context object.
  2124. * @pring: Pointer to driver SLI ring object.
  2125. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2126. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2127. * @fch_type: the type for the first frame of the sequence.
  2128. *
  2129. * This function is called with no lock held. This function uses the r_ctl and
  2130. * type of the received sequence to find the correct callback function to call
  2131. * to process the sequence.
  2132. **/
  2133. static int
  2134. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2135. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2136. uint32_t fch_type)
  2137. {
  2138. int i;
  2139. /* unSolicited Responses */
  2140. if (pring->prt[0].profile) {
  2141. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2142. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2143. saveq);
  2144. return 1;
  2145. }
  2146. /* We must search, based on rctl / type
  2147. for the right routine */
  2148. for (i = 0; i < pring->num_mask; i++) {
  2149. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2150. (pring->prt[i].type == fch_type)) {
  2151. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2152. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2153. (phba, pring, saveq);
  2154. return 1;
  2155. }
  2156. }
  2157. return 0;
  2158. }
  2159. /**
  2160. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2161. * @phba: Pointer to HBA context object.
  2162. * @pring: Pointer to driver SLI ring object.
  2163. * @saveq: Pointer to the unsolicited iocb.
  2164. *
  2165. * This function is called with no lock held by the ring event handler
  2166. * when there is an unsolicited iocb posted to the response ring by the
  2167. * firmware. This function gets the buffer associated with the iocbs
  2168. * and calls the event handler for the ring. This function handles both
  2169. * qring buffers and hbq buffers.
  2170. * When the function returns 1 the caller can free the iocb object otherwise
  2171. * upper layer functions will free the iocb objects.
  2172. **/
  2173. static int
  2174. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2175. struct lpfc_iocbq *saveq)
  2176. {
  2177. IOCB_t * irsp;
  2178. WORD5 * w5p;
  2179. uint32_t Rctl, Type;
  2180. uint32_t match;
  2181. struct lpfc_iocbq *iocbq;
  2182. struct lpfc_dmabuf *dmzbuf;
  2183. match = 0;
  2184. irsp = &(saveq->iocb);
  2185. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2186. if (pring->lpfc_sli_rcv_async_status)
  2187. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2188. else
  2189. lpfc_printf_log(phba,
  2190. KERN_WARNING,
  2191. LOG_SLI,
  2192. "0316 Ring %d handler: unexpected "
  2193. "ASYNC_STATUS iocb received evt_code "
  2194. "0x%x\n",
  2195. pring->ringno,
  2196. irsp->un.asyncstat.evt_code);
  2197. return 1;
  2198. }
  2199. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2200. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2201. if (irsp->ulpBdeCount > 0) {
  2202. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2203. irsp->un.ulpWord[3]);
  2204. lpfc_in_buf_free(phba, dmzbuf);
  2205. }
  2206. if (irsp->ulpBdeCount > 1) {
  2207. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2208. irsp->unsli3.sli3Words[3]);
  2209. lpfc_in_buf_free(phba, dmzbuf);
  2210. }
  2211. if (irsp->ulpBdeCount > 2) {
  2212. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2213. irsp->unsli3.sli3Words[7]);
  2214. lpfc_in_buf_free(phba, dmzbuf);
  2215. }
  2216. return 1;
  2217. }
  2218. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2219. if (irsp->ulpBdeCount != 0) {
  2220. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2221. irsp->un.ulpWord[3]);
  2222. if (!saveq->context2)
  2223. lpfc_printf_log(phba,
  2224. KERN_ERR,
  2225. LOG_SLI,
  2226. "0341 Ring %d Cannot find buffer for "
  2227. "an unsolicited iocb. tag 0x%x\n",
  2228. pring->ringno,
  2229. irsp->un.ulpWord[3]);
  2230. }
  2231. if (irsp->ulpBdeCount == 2) {
  2232. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2233. irsp->unsli3.sli3Words[7]);
  2234. if (!saveq->context3)
  2235. lpfc_printf_log(phba,
  2236. KERN_ERR,
  2237. LOG_SLI,
  2238. "0342 Ring %d Cannot find buffer for an"
  2239. " unsolicited iocb. tag 0x%x\n",
  2240. pring->ringno,
  2241. irsp->unsli3.sli3Words[7]);
  2242. }
  2243. list_for_each_entry(iocbq, &saveq->list, list) {
  2244. irsp = &(iocbq->iocb);
  2245. if (irsp->ulpBdeCount != 0) {
  2246. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2247. irsp->un.ulpWord[3]);
  2248. if (!iocbq->context2)
  2249. lpfc_printf_log(phba,
  2250. KERN_ERR,
  2251. LOG_SLI,
  2252. "0343 Ring %d Cannot find "
  2253. "buffer for an unsolicited iocb"
  2254. ". tag 0x%x\n", pring->ringno,
  2255. irsp->un.ulpWord[3]);
  2256. }
  2257. if (irsp->ulpBdeCount == 2) {
  2258. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2259. irsp->unsli3.sli3Words[7]);
  2260. if (!iocbq->context3)
  2261. lpfc_printf_log(phba,
  2262. KERN_ERR,
  2263. LOG_SLI,
  2264. "0344 Ring %d Cannot find "
  2265. "buffer for an unsolicited "
  2266. "iocb. tag 0x%x\n",
  2267. pring->ringno,
  2268. irsp->unsli3.sli3Words[7]);
  2269. }
  2270. }
  2271. }
  2272. if (irsp->ulpBdeCount != 0 &&
  2273. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2274. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2275. int found = 0;
  2276. /* search continue save q for same XRI */
  2277. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2278. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2279. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2280. list_add_tail(&saveq->list, &iocbq->list);
  2281. found = 1;
  2282. break;
  2283. }
  2284. }
  2285. if (!found)
  2286. list_add_tail(&saveq->clist,
  2287. &pring->iocb_continue_saveq);
  2288. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2289. list_del_init(&iocbq->clist);
  2290. saveq = iocbq;
  2291. irsp = &(saveq->iocb);
  2292. } else
  2293. return 0;
  2294. }
  2295. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2296. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2297. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2298. Rctl = FC_RCTL_ELS_REQ;
  2299. Type = FC_TYPE_ELS;
  2300. } else {
  2301. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2302. Rctl = w5p->hcsw.Rctl;
  2303. Type = w5p->hcsw.Type;
  2304. /* Firmware Workaround */
  2305. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2306. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2307. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2308. Rctl = FC_RCTL_ELS_REQ;
  2309. Type = FC_TYPE_ELS;
  2310. w5p->hcsw.Rctl = Rctl;
  2311. w5p->hcsw.Type = Type;
  2312. }
  2313. }
  2314. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2315. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2316. "0313 Ring %d handler: unexpected Rctl x%x "
  2317. "Type x%x received\n",
  2318. pring->ringno, Rctl, Type);
  2319. return 1;
  2320. }
  2321. /**
  2322. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2323. * @phba: Pointer to HBA context object.
  2324. * @pring: Pointer to driver SLI ring object.
  2325. * @prspiocb: Pointer to response iocb object.
  2326. *
  2327. * This function looks up the iocb_lookup table to get the command iocb
  2328. * corresponding to the given response iocb using the iotag of the
  2329. * response iocb. This function is called with the hbalock held.
  2330. * This function returns the command iocb object if it finds the command
  2331. * iocb else returns NULL.
  2332. **/
  2333. static struct lpfc_iocbq *
  2334. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2335. struct lpfc_sli_ring *pring,
  2336. struct lpfc_iocbq *prspiocb)
  2337. {
  2338. struct lpfc_iocbq *cmd_iocb = NULL;
  2339. uint16_t iotag;
  2340. iotag = prspiocb->iocb.ulpIoTag;
  2341. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2342. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2343. list_del_init(&cmd_iocb->list);
  2344. if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
  2345. pring->txcmplq_cnt--;
  2346. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
  2347. }
  2348. return cmd_iocb;
  2349. }
  2350. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2351. "0317 iotag x%x is out off "
  2352. "range: max iotag x%x wd0 x%x\n",
  2353. iotag, phba->sli.last_iotag,
  2354. *(((uint32_t *) &prspiocb->iocb) + 7));
  2355. return NULL;
  2356. }
  2357. /**
  2358. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2359. * @phba: Pointer to HBA context object.
  2360. * @pring: Pointer to driver SLI ring object.
  2361. * @iotag: IOCB tag.
  2362. *
  2363. * This function looks up the iocb_lookup table to get the command iocb
  2364. * corresponding to the given iotag. This function is called with the
  2365. * hbalock held.
  2366. * This function returns the command iocb object if it finds the command
  2367. * iocb else returns NULL.
  2368. **/
  2369. static struct lpfc_iocbq *
  2370. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2371. struct lpfc_sli_ring *pring, uint16_t iotag)
  2372. {
  2373. struct lpfc_iocbq *cmd_iocb;
  2374. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2375. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2376. list_del_init(&cmd_iocb->list);
  2377. if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
  2378. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
  2379. pring->txcmplq_cnt--;
  2380. }
  2381. return cmd_iocb;
  2382. }
  2383. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2384. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2385. iotag, phba->sli.last_iotag);
  2386. return NULL;
  2387. }
  2388. /**
  2389. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2390. * @phba: Pointer to HBA context object.
  2391. * @pring: Pointer to driver SLI ring object.
  2392. * @saveq: Pointer to the response iocb to be processed.
  2393. *
  2394. * This function is called by the ring event handler for non-fcp
  2395. * rings when there is a new response iocb in the response ring.
  2396. * The caller is not required to hold any locks. This function
  2397. * gets the command iocb associated with the response iocb and
  2398. * calls the completion handler for the command iocb. If there
  2399. * is no completion handler, the function will free the resources
  2400. * associated with command iocb. If the response iocb is for
  2401. * an already aborted command iocb, the status of the completion
  2402. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2403. * This function always returns 1.
  2404. **/
  2405. static int
  2406. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2407. struct lpfc_iocbq *saveq)
  2408. {
  2409. struct lpfc_iocbq *cmdiocbp;
  2410. int rc = 1;
  2411. unsigned long iflag;
  2412. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2413. spin_lock_irqsave(&phba->hbalock, iflag);
  2414. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2415. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2416. if (cmdiocbp) {
  2417. if (cmdiocbp->iocb_cmpl) {
  2418. /*
  2419. * If an ELS command failed send an event to mgmt
  2420. * application.
  2421. */
  2422. if (saveq->iocb.ulpStatus &&
  2423. (pring->ringno == LPFC_ELS_RING) &&
  2424. (cmdiocbp->iocb.ulpCommand ==
  2425. CMD_ELS_REQUEST64_CR))
  2426. lpfc_send_els_failure_event(phba,
  2427. cmdiocbp, saveq);
  2428. /*
  2429. * Post all ELS completions to the worker thread.
  2430. * All other are passed to the completion callback.
  2431. */
  2432. if (pring->ringno == LPFC_ELS_RING) {
  2433. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2434. (cmdiocbp->iocb_flag &
  2435. LPFC_DRIVER_ABORTED)) {
  2436. spin_lock_irqsave(&phba->hbalock,
  2437. iflag);
  2438. cmdiocbp->iocb_flag &=
  2439. ~LPFC_DRIVER_ABORTED;
  2440. spin_unlock_irqrestore(&phba->hbalock,
  2441. iflag);
  2442. saveq->iocb.ulpStatus =
  2443. IOSTAT_LOCAL_REJECT;
  2444. saveq->iocb.un.ulpWord[4] =
  2445. IOERR_SLI_ABORTED;
  2446. /* Firmware could still be in progress
  2447. * of DMAing payload, so don't free data
  2448. * buffer till after a hbeat.
  2449. */
  2450. spin_lock_irqsave(&phba->hbalock,
  2451. iflag);
  2452. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2453. spin_unlock_irqrestore(&phba->hbalock,
  2454. iflag);
  2455. }
  2456. if (phba->sli_rev == LPFC_SLI_REV4) {
  2457. if (saveq->iocb_flag &
  2458. LPFC_EXCHANGE_BUSY) {
  2459. /* Set cmdiocb flag for the
  2460. * exchange busy so sgl (xri)
  2461. * will not be released until
  2462. * the abort xri is received
  2463. * from hba.
  2464. */
  2465. spin_lock_irqsave(
  2466. &phba->hbalock, iflag);
  2467. cmdiocbp->iocb_flag |=
  2468. LPFC_EXCHANGE_BUSY;
  2469. spin_unlock_irqrestore(
  2470. &phba->hbalock, iflag);
  2471. }
  2472. if (cmdiocbp->iocb_flag &
  2473. LPFC_DRIVER_ABORTED) {
  2474. /*
  2475. * Clear LPFC_DRIVER_ABORTED
  2476. * bit in case it was driver
  2477. * initiated abort.
  2478. */
  2479. spin_lock_irqsave(
  2480. &phba->hbalock, iflag);
  2481. cmdiocbp->iocb_flag &=
  2482. ~LPFC_DRIVER_ABORTED;
  2483. spin_unlock_irqrestore(
  2484. &phba->hbalock, iflag);
  2485. cmdiocbp->iocb.ulpStatus =
  2486. IOSTAT_LOCAL_REJECT;
  2487. cmdiocbp->iocb.un.ulpWord[4] =
  2488. IOERR_ABORT_REQUESTED;
  2489. /*
  2490. * For SLI4, irsiocb contains
  2491. * NO_XRI in sli_xritag, it
  2492. * shall not affect releasing
  2493. * sgl (xri) process.
  2494. */
  2495. saveq->iocb.ulpStatus =
  2496. IOSTAT_LOCAL_REJECT;
  2497. saveq->iocb.un.ulpWord[4] =
  2498. IOERR_SLI_ABORTED;
  2499. spin_lock_irqsave(
  2500. &phba->hbalock, iflag);
  2501. saveq->iocb_flag |=
  2502. LPFC_DELAY_MEM_FREE;
  2503. spin_unlock_irqrestore(
  2504. &phba->hbalock, iflag);
  2505. }
  2506. }
  2507. }
  2508. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2509. } else
  2510. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2511. } else {
  2512. /*
  2513. * Unknown initiating command based on the response iotag.
  2514. * This could be the case on the ELS ring because of
  2515. * lpfc_els_abort().
  2516. */
  2517. if (pring->ringno != LPFC_ELS_RING) {
  2518. /*
  2519. * Ring <ringno> handler: unexpected completion IoTag
  2520. * <IoTag>
  2521. */
  2522. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2523. "0322 Ring %d handler: "
  2524. "unexpected completion IoTag x%x "
  2525. "Data: x%x x%x x%x x%x\n",
  2526. pring->ringno,
  2527. saveq->iocb.ulpIoTag,
  2528. saveq->iocb.ulpStatus,
  2529. saveq->iocb.un.ulpWord[4],
  2530. saveq->iocb.ulpCommand,
  2531. saveq->iocb.ulpContext);
  2532. }
  2533. }
  2534. return rc;
  2535. }
  2536. /**
  2537. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2538. * @phba: Pointer to HBA context object.
  2539. * @pring: Pointer to driver SLI ring object.
  2540. *
  2541. * This function is called from the iocb ring event handlers when
  2542. * put pointer is ahead of the get pointer for a ring. This function signal
  2543. * an error attention condition to the worker thread and the worker
  2544. * thread will transition the HBA to offline state.
  2545. **/
  2546. static void
  2547. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2548. {
  2549. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2550. /*
  2551. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2552. * rsp ring <portRspMax>
  2553. */
  2554. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2555. "0312 Ring %d handler: portRspPut %d "
  2556. "is bigger than rsp ring %d\n",
  2557. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2558. pring->numRiocb);
  2559. phba->link_state = LPFC_HBA_ERROR;
  2560. /*
  2561. * All error attention handlers are posted to
  2562. * worker thread
  2563. */
  2564. phba->work_ha |= HA_ERATT;
  2565. phba->work_hs = HS_FFER3;
  2566. lpfc_worker_wake_up(phba);
  2567. return;
  2568. }
  2569. /**
  2570. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2571. * @ptr: Pointer to address of HBA context object.
  2572. *
  2573. * This function is invoked by the Error Attention polling timer when the
  2574. * timer times out. It will check the SLI Error Attention register for
  2575. * possible attention events. If so, it will post an Error Attention event
  2576. * and wake up worker thread to process it. Otherwise, it will set up the
  2577. * Error Attention polling timer for the next poll.
  2578. **/
  2579. void lpfc_poll_eratt(unsigned long ptr)
  2580. {
  2581. struct lpfc_hba *phba;
  2582. uint32_t eratt = 0;
  2583. phba = (struct lpfc_hba *)ptr;
  2584. /* Check chip HA register for error event */
  2585. eratt = lpfc_sli_check_eratt(phba);
  2586. if (eratt)
  2587. /* Tell the worker thread there is work to do */
  2588. lpfc_worker_wake_up(phba);
  2589. else
  2590. /* Restart the timer for next eratt poll */
  2591. mod_timer(&phba->eratt_poll, jiffies +
  2592. HZ * LPFC_ERATT_POLL_INTERVAL);
  2593. return;
  2594. }
  2595. /**
  2596. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2597. * @phba: Pointer to HBA context object.
  2598. * @pring: Pointer to driver SLI ring object.
  2599. * @mask: Host attention register mask for this ring.
  2600. *
  2601. * This function is called from the interrupt context when there is a ring
  2602. * event for the fcp ring. The caller does not hold any lock.
  2603. * The function processes each response iocb in the response ring until it
  2604. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2605. * LE bit set. The function will call the completion handler of the command iocb
  2606. * if the response iocb indicates a completion for a command iocb or it is
  2607. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2608. * function if this is an unsolicited iocb.
  2609. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2610. * to check it explicitly.
  2611. */
  2612. int
  2613. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2614. struct lpfc_sli_ring *pring, uint32_t mask)
  2615. {
  2616. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2617. IOCB_t *irsp = NULL;
  2618. IOCB_t *entry = NULL;
  2619. struct lpfc_iocbq *cmdiocbq = NULL;
  2620. struct lpfc_iocbq rspiocbq;
  2621. uint32_t status;
  2622. uint32_t portRspPut, portRspMax;
  2623. int rc = 1;
  2624. lpfc_iocb_type type;
  2625. unsigned long iflag;
  2626. uint32_t rsp_cmpl = 0;
  2627. spin_lock_irqsave(&phba->hbalock, iflag);
  2628. pring->stats.iocb_event++;
  2629. /*
  2630. * The next available response entry should never exceed the maximum
  2631. * entries. If it does, treat it as an adapter hardware error.
  2632. */
  2633. portRspMax = pring->numRiocb;
  2634. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2635. if (unlikely(portRspPut >= portRspMax)) {
  2636. lpfc_sli_rsp_pointers_error(phba, pring);
  2637. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2638. return 1;
  2639. }
  2640. if (phba->fcp_ring_in_use) {
  2641. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2642. return 1;
  2643. } else
  2644. phba->fcp_ring_in_use = 1;
  2645. rmb();
  2646. while (pring->rspidx != portRspPut) {
  2647. /*
  2648. * Fetch an entry off the ring and copy it into a local data
  2649. * structure. The copy involves a byte-swap since the
  2650. * network byte order and pci byte orders are different.
  2651. */
  2652. entry = lpfc_resp_iocb(phba, pring);
  2653. phba->last_completion_time = jiffies;
  2654. if (++pring->rspidx >= portRspMax)
  2655. pring->rspidx = 0;
  2656. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2657. (uint32_t *) &rspiocbq.iocb,
  2658. phba->iocb_rsp_size);
  2659. INIT_LIST_HEAD(&(rspiocbq.list));
  2660. irsp = &rspiocbq.iocb;
  2661. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2662. pring->stats.iocb_rsp++;
  2663. rsp_cmpl++;
  2664. if (unlikely(irsp->ulpStatus)) {
  2665. /*
  2666. * If resource errors reported from HBA, reduce
  2667. * queuedepths of the SCSI device.
  2668. */
  2669. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2670. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2671. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2672. phba->lpfc_rampdown_queue_depth(phba);
  2673. spin_lock_irqsave(&phba->hbalock, iflag);
  2674. }
  2675. /* Rsp ring <ringno> error: IOCB */
  2676. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2677. "0336 Rsp Ring %d error: IOCB Data: "
  2678. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2679. pring->ringno,
  2680. irsp->un.ulpWord[0],
  2681. irsp->un.ulpWord[1],
  2682. irsp->un.ulpWord[2],
  2683. irsp->un.ulpWord[3],
  2684. irsp->un.ulpWord[4],
  2685. irsp->un.ulpWord[5],
  2686. *(uint32_t *)&irsp->un1,
  2687. *((uint32_t *)&irsp->un1 + 1));
  2688. }
  2689. switch (type) {
  2690. case LPFC_ABORT_IOCB:
  2691. case LPFC_SOL_IOCB:
  2692. /*
  2693. * Idle exchange closed via ABTS from port. No iocb
  2694. * resources need to be recovered.
  2695. */
  2696. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2697. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2698. "0333 IOCB cmd 0x%x"
  2699. " processed. Skipping"
  2700. " completion\n",
  2701. irsp->ulpCommand);
  2702. break;
  2703. }
  2704. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2705. &rspiocbq);
  2706. if (unlikely(!cmdiocbq))
  2707. break;
  2708. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2709. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2710. if (cmdiocbq->iocb_cmpl) {
  2711. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2712. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2713. &rspiocbq);
  2714. spin_lock_irqsave(&phba->hbalock, iflag);
  2715. }
  2716. break;
  2717. case LPFC_UNSOL_IOCB:
  2718. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2719. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2720. spin_lock_irqsave(&phba->hbalock, iflag);
  2721. break;
  2722. default:
  2723. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2724. char adaptermsg[LPFC_MAX_ADPTMSG];
  2725. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2726. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2727. MAX_MSG_DATA);
  2728. dev_warn(&((phba->pcidev)->dev),
  2729. "lpfc%d: %s\n",
  2730. phba->brd_no, adaptermsg);
  2731. } else {
  2732. /* Unknown IOCB command */
  2733. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2734. "0334 Unknown IOCB command "
  2735. "Data: x%x, x%x x%x x%x x%x\n",
  2736. type, irsp->ulpCommand,
  2737. irsp->ulpStatus,
  2738. irsp->ulpIoTag,
  2739. irsp->ulpContext);
  2740. }
  2741. break;
  2742. }
  2743. /*
  2744. * The response IOCB has been processed. Update the ring
  2745. * pointer in SLIM. If the port response put pointer has not
  2746. * been updated, sync the pgp->rspPutInx and fetch the new port
  2747. * response put pointer.
  2748. */
  2749. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2750. if (pring->rspidx == portRspPut)
  2751. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2752. }
  2753. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2754. pring->stats.iocb_rsp_full++;
  2755. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2756. writel(status, phba->CAregaddr);
  2757. readl(phba->CAregaddr);
  2758. }
  2759. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2760. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2761. pring->stats.iocb_cmd_empty++;
  2762. /* Force update of the local copy of cmdGetInx */
  2763. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2764. lpfc_sli_resume_iocb(phba, pring);
  2765. if ((pring->lpfc_sli_cmd_available))
  2766. (pring->lpfc_sli_cmd_available) (phba, pring);
  2767. }
  2768. phba->fcp_ring_in_use = 0;
  2769. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2770. return rc;
  2771. }
  2772. /**
  2773. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2774. * @phba: Pointer to HBA context object.
  2775. * @pring: Pointer to driver SLI ring object.
  2776. * @rspiocbp: Pointer to driver response IOCB object.
  2777. *
  2778. * This function is called from the worker thread when there is a slow-path
  2779. * response IOCB to process. This function chains all the response iocbs until
  2780. * seeing the iocb with the LE bit set. The function will call
  2781. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2782. * completion of a command iocb. The function will call the
  2783. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2784. * The function frees the resources or calls the completion handler if this
  2785. * iocb is an abort completion. The function returns NULL when the response
  2786. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2787. * this function shall chain the iocb on to the iocb_continueq and return the
  2788. * response iocb passed in.
  2789. **/
  2790. static struct lpfc_iocbq *
  2791. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2792. struct lpfc_iocbq *rspiocbp)
  2793. {
  2794. struct lpfc_iocbq *saveq;
  2795. struct lpfc_iocbq *cmdiocbp;
  2796. struct lpfc_iocbq *next_iocb;
  2797. IOCB_t *irsp = NULL;
  2798. uint32_t free_saveq;
  2799. uint8_t iocb_cmd_type;
  2800. lpfc_iocb_type type;
  2801. unsigned long iflag;
  2802. int rc;
  2803. spin_lock_irqsave(&phba->hbalock, iflag);
  2804. /* First add the response iocb to the countinueq list */
  2805. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2806. pring->iocb_continueq_cnt++;
  2807. /* Now, determine whether the list is completed for processing */
  2808. irsp = &rspiocbp->iocb;
  2809. if (irsp->ulpLe) {
  2810. /*
  2811. * By default, the driver expects to free all resources
  2812. * associated with this iocb completion.
  2813. */
  2814. free_saveq = 1;
  2815. saveq = list_get_first(&pring->iocb_continueq,
  2816. struct lpfc_iocbq, list);
  2817. irsp = &(saveq->iocb);
  2818. list_del_init(&pring->iocb_continueq);
  2819. pring->iocb_continueq_cnt = 0;
  2820. pring->stats.iocb_rsp++;
  2821. /*
  2822. * If resource errors reported from HBA, reduce
  2823. * queuedepths of the SCSI device.
  2824. */
  2825. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2826. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2827. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2828. phba->lpfc_rampdown_queue_depth(phba);
  2829. spin_lock_irqsave(&phba->hbalock, iflag);
  2830. }
  2831. if (irsp->ulpStatus) {
  2832. /* Rsp ring <ringno> error: IOCB */
  2833. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2834. "0328 Rsp Ring %d error: "
  2835. "IOCB Data: "
  2836. "x%x x%x x%x x%x "
  2837. "x%x x%x x%x x%x "
  2838. "x%x x%x x%x x%x "
  2839. "x%x x%x x%x x%x\n",
  2840. pring->ringno,
  2841. irsp->un.ulpWord[0],
  2842. irsp->un.ulpWord[1],
  2843. irsp->un.ulpWord[2],
  2844. irsp->un.ulpWord[3],
  2845. irsp->un.ulpWord[4],
  2846. irsp->un.ulpWord[5],
  2847. *(((uint32_t *) irsp) + 6),
  2848. *(((uint32_t *) irsp) + 7),
  2849. *(((uint32_t *) irsp) + 8),
  2850. *(((uint32_t *) irsp) + 9),
  2851. *(((uint32_t *) irsp) + 10),
  2852. *(((uint32_t *) irsp) + 11),
  2853. *(((uint32_t *) irsp) + 12),
  2854. *(((uint32_t *) irsp) + 13),
  2855. *(((uint32_t *) irsp) + 14),
  2856. *(((uint32_t *) irsp) + 15));
  2857. }
  2858. /*
  2859. * Fetch the IOCB command type and call the correct completion
  2860. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2861. * get freed back to the lpfc_iocb_list by the discovery
  2862. * kernel thread.
  2863. */
  2864. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2865. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2866. switch (type) {
  2867. case LPFC_SOL_IOCB:
  2868. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2869. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2870. spin_lock_irqsave(&phba->hbalock, iflag);
  2871. break;
  2872. case LPFC_UNSOL_IOCB:
  2873. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2874. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2875. spin_lock_irqsave(&phba->hbalock, iflag);
  2876. if (!rc)
  2877. free_saveq = 0;
  2878. break;
  2879. case LPFC_ABORT_IOCB:
  2880. cmdiocbp = NULL;
  2881. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2882. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2883. saveq);
  2884. if (cmdiocbp) {
  2885. /* Call the specified completion routine */
  2886. if (cmdiocbp->iocb_cmpl) {
  2887. spin_unlock_irqrestore(&phba->hbalock,
  2888. iflag);
  2889. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2890. saveq);
  2891. spin_lock_irqsave(&phba->hbalock,
  2892. iflag);
  2893. } else
  2894. __lpfc_sli_release_iocbq(phba,
  2895. cmdiocbp);
  2896. }
  2897. break;
  2898. case LPFC_UNKNOWN_IOCB:
  2899. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2900. char adaptermsg[LPFC_MAX_ADPTMSG];
  2901. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2902. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2903. MAX_MSG_DATA);
  2904. dev_warn(&((phba->pcidev)->dev),
  2905. "lpfc%d: %s\n",
  2906. phba->brd_no, adaptermsg);
  2907. } else {
  2908. /* Unknown IOCB command */
  2909. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2910. "0335 Unknown IOCB "
  2911. "command Data: x%x "
  2912. "x%x x%x x%x\n",
  2913. irsp->ulpCommand,
  2914. irsp->ulpStatus,
  2915. irsp->ulpIoTag,
  2916. irsp->ulpContext);
  2917. }
  2918. break;
  2919. }
  2920. if (free_saveq) {
  2921. list_for_each_entry_safe(rspiocbp, next_iocb,
  2922. &saveq->list, list) {
  2923. list_del(&rspiocbp->list);
  2924. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2925. }
  2926. __lpfc_sli_release_iocbq(phba, saveq);
  2927. }
  2928. rspiocbp = NULL;
  2929. }
  2930. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2931. return rspiocbp;
  2932. }
  2933. /**
  2934. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2935. * @phba: Pointer to HBA context object.
  2936. * @pring: Pointer to driver SLI ring object.
  2937. * @mask: Host attention register mask for this ring.
  2938. *
  2939. * This routine wraps the actual slow_ring event process routine from the
  2940. * API jump table function pointer from the lpfc_hba struct.
  2941. **/
  2942. void
  2943. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2944. struct lpfc_sli_ring *pring, uint32_t mask)
  2945. {
  2946. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2947. }
  2948. /**
  2949. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  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 function is called from the worker thread when there is a ring event
  2955. * for non-fcp rings. The caller does not hold any lock. The function will
  2956. * remove each response iocb in the response ring and calls the handle
  2957. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2958. **/
  2959. static void
  2960. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2961. struct lpfc_sli_ring *pring, uint32_t mask)
  2962. {
  2963. struct lpfc_pgp *pgp;
  2964. IOCB_t *entry;
  2965. IOCB_t *irsp = NULL;
  2966. struct lpfc_iocbq *rspiocbp = NULL;
  2967. uint32_t portRspPut, portRspMax;
  2968. unsigned long iflag;
  2969. uint32_t status;
  2970. pgp = &phba->port_gp[pring->ringno];
  2971. spin_lock_irqsave(&phba->hbalock, iflag);
  2972. pring->stats.iocb_event++;
  2973. /*
  2974. * The next available response entry should never exceed the maximum
  2975. * entries. If it does, treat it as an adapter hardware error.
  2976. */
  2977. portRspMax = pring->numRiocb;
  2978. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2979. if (portRspPut >= portRspMax) {
  2980. /*
  2981. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2982. * rsp ring <portRspMax>
  2983. */
  2984. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2985. "0303 Ring %d handler: portRspPut %d "
  2986. "is bigger than rsp ring %d\n",
  2987. pring->ringno, portRspPut, portRspMax);
  2988. phba->link_state = LPFC_HBA_ERROR;
  2989. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2990. phba->work_hs = HS_FFER3;
  2991. lpfc_handle_eratt(phba);
  2992. return;
  2993. }
  2994. rmb();
  2995. while (pring->rspidx != portRspPut) {
  2996. /*
  2997. * Build a completion list and call the appropriate handler.
  2998. * The process is to get the next available response iocb, get
  2999. * a free iocb from the list, copy the response data into the
  3000. * free iocb, insert to the continuation list, and update the
  3001. * next response index to slim. This process makes response
  3002. * iocb's in the ring available to DMA as fast as possible but
  3003. * pays a penalty for a copy operation. Since the iocb is
  3004. * only 32 bytes, this penalty is considered small relative to
  3005. * the PCI reads for register values and a slim write. When
  3006. * the ulpLe field is set, the entire Command has been
  3007. * received.
  3008. */
  3009. entry = lpfc_resp_iocb(phba, pring);
  3010. phba->last_completion_time = jiffies;
  3011. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3012. if (rspiocbp == NULL) {
  3013. printk(KERN_ERR "%s: out of buffers! Failing "
  3014. "completion.\n", __func__);
  3015. break;
  3016. }
  3017. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3018. phba->iocb_rsp_size);
  3019. irsp = &rspiocbp->iocb;
  3020. if (++pring->rspidx >= portRspMax)
  3021. pring->rspidx = 0;
  3022. if (pring->ringno == LPFC_ELS_RING) {
  3023. lpfc_debugfs_slow_ring_trc(phba,
  3024. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3025. *(((uint32_t *) irsp) + 4),
  3026. *(((uint32_t *) irsp) + 6),
  3027. *(((uint32_t *) irsp) + 7));
  3028. }
  3029. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  3030. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3031. /* Handle the response IOCB */
  3032. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3033. spin_lock_irqsave(&phba->hbalock, iflag);
  3034. /*
  3035. * If the port response put pointer has not been updated, sync
  3036. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3037. * response put pointer.
  3038. */
  3039. if (pring->rspidx == portRspPut) {
  3040. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3041. }
  3042. } /* while (pring->rspidx != portRspPut) */
  3043. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3044. /* At least one response entry has been freed */
  3045. pring->stats.iocb_rsp_full++;
  3046. /* SET RxRE_RSP in Chip Att register */
  3047. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3048. writel(status, phba->CAregaddr);
  3049. readl(phba->CAregaddr); /* flush */
  3050. }
  3051. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3052. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3053. pring->stats.iocb_cmd_empty++;
  3054. /* Force update of the local copy of cmdGetInx */
  3055. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3056. lpfc_sli_resume_iocb(phba, pring);
  3057. if ((pring->lpfc_sli_cmd_available))
  3058. (pring->lpfc_sli_cmd_available) (phba, pring);
  3059. }
  3060. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3061. return;
  3062. }
  3063. /**
  3064. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3065. * @phba: Pointer to HBA context object.
  3066. * @pring: Pointer to driver SLI ring object.
  3067. * @mask: Host attention register mask for this ring.
  3068. *
  3069. * This function is called from the worker thread when there is a pending
  3070. * ELS response iocb on the driver internal slow-path response iocb worker
  3071. * queue. The caller does not hold any lock. The function will remove each
  3072. * response iocb from the response worker queue and calls the handle
  3073. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3074. **/
  3075. static void
  3076. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3077. struct lpfc_sli_ring *pring, uint32_t mask)
  3078. {
  3079. struct lpfc_iocbq *irspiocbq;
  3080. struct hbq_dmabuf *dmabuf;
  3081. struct lpfc_cq_event *cq_event;
  3082. unsigned long iflag;
  3083. spin_lock_irqsave(&phba->hbalock, iflag);
  3084. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3085. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3086. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3087. /* Get the response iocb from the head of work queue */
  3088. spin_lock_irqsave(&phba->hbalock, iflag);
  3089. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3090. cq_event, struct lpfc_cq_event, list);
  3091. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3092. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3093. case CQE_CODE_COMPL_WQE:
  3094. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3095. cq_event);
  3096. /* Translate ELS WCQE to response IOCBQ */
  3097. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3098. irspiocbq);
  3099. if (irspiocbq)
  3100. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3101. irspiocbq);
  3102. break;
  3103. case CQE_CODE_RECEIVE:
  3104. case CQE_CODE_RECEIVE_V1:
  3105. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3106. cq_event);
  3107. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3108. break;
  3109. default:
  3110. break;
  3111. }
  3112. }
  3113. }
  3114. /**
  3115. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3116. * @phba: Pointer to HBA context object.
  3117. * @pring: Pointer to driver SLI ring object.
  3118. *
  3119. * This function aborts all iocbs in the given ring and frees all the iocb
  3120. * objects in txq. This function issues an abort iocb for all the iocb commands
  3121. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3122. * the return of this function. The caller is not required to hold any locks.
  3123. **/
  3124. void
  3125. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3126. {
  3127. LIST_HEAD(completions);
  3128. struct lpfc_iocbq *iocb, *next_iocb;
  3129. if (pring->ringno == LPFC_ELS_RING) {
  3130. lpfc_fabric_abort_hba(phba);
  3131. }
  3132. /* Error everything on txq and txcmplq
  3133. * First do the txq.
  3134. */
  3135. spin_lock_irq(&phba->hbalock);
  3136. list_splice_init(&pring->txq, &completions);
  3137. pring->txq_cnt = 0;
  3138. /* Next issue ABTS for everything on the txcmplq */
  3139. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3140. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3141. spin_unlock_irq(&phba->hbalock);
  3142. /* Cancel all the IOCBs from the completions list */
  3143. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3144. IOERR_SLI_ABORTED);
  3145. }
  3146. /**
  3147. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3148. * @phba: Pointer to HBA context object.
  3149. *
  3150. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3151. * objects in txq and txcmplq. This function will not issue abort iocbs
  3152. * for all the iocb commands in txcmplq, they will just be returned with
  3153. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3154. * slot has been permanently disabled.
  3155. **/
  3156. void
  3157. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3158. {
  3159. LIST_HEAD(txq);
  3160. LIST_HEAD(txcmplq);
  3161. struct lpfc_sli *psli = &phba->sli;
  3162. struct lpfc_sli_ring *pring;
  3163. /* Currently, only one fcp ring */
  3164. pring = &psli->ring[psli->fcp_ring];
  3165. spin_lock_irq(&phba->hbalock);
  3166. /* Retrieve everything on txq */
  3167. list_splice_init(&pring->txq, &txq);
  3168. pring->txq_cnt = 0;
  3169. /* Retrieve everything on the txcmplq */
  3170. list_splice_init(&pring->txcmplq, &txcmplq);
  3171. pring->txcmplq_cnt = 0;
  3172. spin_unlock_irq(&phba->hbalock);
  3173. /* Flush the txq */
  3174. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3175. IOERR_SLI_DOWN);
  3176. /* Flush the txcmpq */
  3177. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3178. IOERR_SLI_DOWN);
  3179. }
  3180. /**
  3181. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3182. * @phba: Pointer to HBA context object.
  3183. * @mask: Bit mask to be checked.
  3184. *
  3185. * This function reads the host status register and compares
  3186. * with the provided bit mask to check if HBA completed
  3187. * the restart. This function will wait in a loop for the
  3188. * HBA to complete restart. If the HBA does not restart within
  3189. * 15 iterations, the function will reset the HBA again. The
  3190. * function returns 1 when HBA fail to restart otherwise returns
  3191. * zero.
  3192. **/
  3193. static int
  3194. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3195. {
  3196. uint32_t status;
  3197. int i = 0;
  3198. int retval = 0;
  3199. /* Read the HBA Host Status Register */
  3200. if (lpfc_readl(phba->HSregaddr, &status))
  3201. return 1;
  3202. /*
  3203. * Check status register every 100ms for 5 retries, then every
  3204. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3205. * every 2.5 sec for 4.
  3206. * Break our of the loop if errors occurred during init.
  3207. */
  3208. while (((status & mask) != mask) &&
  3209. !(status & HS_FFERM) &&
  3210. i++ < 20) {
  3211. if (i <= 5)
  3212. msleep(10);
  3213. else if (i <= 10)
  3214. msleep(500);
  3215. else
  3216. msleep(2500);
  3217. if (i == 15) {
  3218. /* Do post */
  3219. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3220. lpfc_sli_brdrestart(phba);
  3221. }
  3222. /* Read the HBA Host Status Register */
  3223. if (lpfc_readl(phba->HSregaddr, &status)) {
  3224. retval = 1;
  3225. break;
  3226. }
  3227. }
  3228. /* Check to see if any errors occurred during init */
  3229. if ((status & HS_FFERM) || (i >= 20)) {
  3230. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3231. "2751 Adapter failed to restart, "
  3232. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3233. status,
  3234. readl(phba->MBslimaddr + 0xa8),
  3235. readl(phba->MBslimaddr + 0xac));
  3236. phba->link_state = LPFC_HBA_ERROR;
  3237. retval = 1;
  3238. }
  3239. return retval;
  3240. }
  3241. /**
  3242. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3243. * @phba: Pointer to HBA context object.
  3244. * @mask: Bit mask to be checked.
  3245. *
  3246. * This function checks the host status register to check if HBA is
  3247. * ready. This function will wait in a loop for the HBA to be ready
  3248. * If the HBA is not ready , the function will will reset the HBA PCI
  3249. * function again. The function returns 1 when HBA fail to be ready
  3250. * otherwise returns zero.
  3251. **/
  3252. static int
  3253. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3254. {
  3255. uint32_t status;
  3256. int retval = 0;
  3257. /* Read the HBA Host Status Register */
  3258. status = lpfc_sli4_post_status_check(phba);
  3259. if (status) {
  3260. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3261. lpfc_sli_brdrestart(phba);
  3262. status = lpfc_sli4_post_status_check(phba);
  3263. }
  3264. /* Check to see if any errors occurred during init */
  3265. if (status) {
  3266. phba->link_state = LPFC_HBA_ERROR;
  3267. retval = 1;
  3268. } else
  3269. phba->sli4_hba.intr_enable = 0;
  3270. return retval;
  3271. }
  3272. /**
  3273. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3274. * @phba: Pointer to HBA context object.
  3275. * @mask: Bit mask to be checked.
  3276. *
  3277. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3278. * from the API jump table function pointer from the lpfc_hba struct.
  3279. **/
  3280. int
  3281. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3282. {
  3283. return phba->lpfc_sli_brdready(phba, mask);
  3284. }
  3285. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3286. /**
  3287. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3288. * @phba: Pointer to HBA context object.
  3289. *
  3290. * This function is called before resetting an HBA. This function is called
  3291. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3292. **/
  3293. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3294. {
  3295. uint32_t __iomem *resp_buf;
  3296. uint32_t __iomem *mbox_buf;
  3297. volatile uint32_t mbox;
  3298. uint32_t hc_copy, ha_copy, resp_data;
  3299. int i;
  3300. uint8_t hdrtype;
  3301. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3302. if (hdrtype != 0x80 ||
  3303. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3304. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3305. return;
  3306. /*
  3307. * Tell the other part of the chip to suspend temporarily all
  3308. * its DMA activity.
  3309. */
  3310. resp_buf = phba->MBslimaddr;
  3311. /* Disable the error attention */
  3312. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3313. return;
  3314. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3315. readl(phba->HCregaddr); /* flush */
  3316. phba->link_flag |= LS_IGNORE_ERATT;
  3317. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3318. return;
  3319. if (ha_copy & HA_ERATT) {
  3320. /* Clear Chip error bit */
  3321. writel(HA_ERATT, phba->HAregaddr);
  3322. phba->pport->stopped = 1;
  3323. }
  3324. mbox = 0;
  3325. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3326. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3327. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3328. mbox_buf = phba->MBslimaddr;
  3329. writel(mbox, mbox_buf);
  3330. for (i = 0; i < 50; i++) {
  3331. if (lpfc_readl((resp_buf + 1), &resp_data))
  3332. return;
  3333. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3334. mdelay(1);
  3335. else
  3336. break;
  3337. }
  3338. resp_data = 0;
  3339. if (lpfc_readl((resp_buf + 1), &resp_data))
  3340. return;
  3341. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3342. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3343. phba->pport->stopped)
  3344. goto restore_hc;
  3345. else
  3346. goto clear_errat;
  3347. }
  3348. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3349. resp_data = 0;
  3350. for (i = 0; i < 500; i++) {
  3351. if (lpfc_readl(resp_buf, &resp_data))
  3352. return;
  3353. if (resp_data != mbox)
  3354. mdelay(1);
  3355. else
  3356. break;
  3357. }
  3358. clear_errat:
  3359. while (++i < 500) {
  3360. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3361. return;
  3362. if (!(ha_copy & HA_ERATT))
  3363. mdelay(1);
  3364. else
  3365. break;
  3366. }
  3367. if (readl(phba->HAregaddr) & HA_ERATT) {
  3368. writel(HA_ERATT, phba->HAregaddr);
  3369. phba->pport->stopped = 1;
  3370. }
  3371. restore_hc:
  3372. phba->link_flag &= ~LS_IGNORE_ERATT;
  3373. writel(hc_copy, phba->HCregaddr);
  3374. readl(phba->HCregaddr); /* flush */
  3375. }
  3376. /**
  3377. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3378. * @phba: Pointer to HBA context object.
  3379. *
  3380. * This function issues a kill_board mailbox command and waits for
  3381. * the error attention interrupt. This function is called for stopping
  3382. * the firmware processing. The caller is not required to hold any
  3383. * locks. This function calls lpfc_hba_down_post function to free
  3384. * any pending commands after the kill. The function will return 1 when it
  3385. * fails to kill the board else will return 0.
  3386. **/
  3387. int
  3388. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3389. {
  3390. struct lpfc_sli *psli;
  3391. LPFC_MBOXQ_t *pmb;
  3392. uint32_t status;
  3393. uint32_t ha_copy;
  3394. int retval;
  3395. int i = 0;
  3396. psli = &phba->sli;
  3397. /* Kill HBA */
  3398. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3399. "0329 Kill HBA Data: x%x x%x\n",
  3400. phba->pport->port_state, psli->sli_flag);
  3401. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3402. if (!pmb)
  3403. return 1;
  3404. /* Disable the error attention */
  3405. spin_lock_irq(&phba->hbalock);
  3406. if (lpfc_readl(phba->HCregaddr, &status)) {
  3407. spin_unlock_irq(&phba->hbalock);
  3408. mempool_free(pmb, phba->mbox_mem_pool);
  3409. return 1;
  3410. }
  3411. status &= ~HC_ERINT_ENA;
  3412. writel(status, phba->HCregaddr);
  3413. readl(phba->HCregaddr); /* flush */
  3414. phba->link_flag |= LS_IGNORE_ERATT;
  3415. spin_unlock_irq(&phba->hbalock);
  3416. lpfc_kill_board(phba, pmb);
  3417. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3418. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3419. if (retval != MBX_SUCCESS) {
  3420. if (retval != MBX_BUSY)
  3421. mempool_free(pmb, phba->mbox_mem_pool);
  3422. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3423. "2752 KILL_BOARD command failed retval %d\n",
  3424. retval);
  3425. spin_lock_irq(&phba->hbalock);
  3426. phba->link_flag &= ~LS_IGNORE_ERATT;
  3427. spin_unlock_irq(&phba->hbalock);
  3428. return 1;
  3429. }
  3430. spin_lock_irq(&phba->hbalock);
  3431. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3432. spin_unlock_irq(&phba->hbalock);
  3433. mempool_free(pmb, phba->mbox_mem_pool);
  3434. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3435. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3436. * 3 seconds we still set HBA_ERROR state because the status of the
  3437. * board is now undefined.
  3438. */
  3439. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3440. return 1;
  3441. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3442. mdelay(100);
  3443. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3444. return 1;
  3445. }
  3446. del_timer_sync(&psli->mbox_tmo);
  3447. if (ha_copy & HA_ERATT) {
  3448. writel(HA_ERATT, phba->HAregaddr);
  3449. phba->pport->stopped = 1;
  3450. }
  3451. spin_lock_irq(&phba->hbalock);
  3452. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3453. psli->mbox_active = NULL;
  3454. phba->link_flag &= ~LS_IGNORE_ERATT;
  3455. spin_unlock_irq(&phba->hbalock);
  3456. lpfc_hba_down_post(phba);
  3457. phba->link_state = LPFC_HBA_ERROR;
  3458. return ha_copy & HA_ERATT ? 0 : 1;
  3459. }
  3460. /**
  3461. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3462. * @phba: Pointer to HBA context object.
  3463. *
  3464. * This function resets the HBA by writing HC_INITFF to the control
  3465. * register. After the HBA resets, this function resets all the iocb ring
  3466. * indices. This function disables PCI layer parity checking during
  3467. * the reset.
  3468. * This function returns 0 always.
  3469. * The caller is not required to hold any locks.
  3470. **/
  3471. int
  3472. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3473. {
  3474. struct lpfc_sli *psli;
  3475. struct lpfc_sli_ring *pring;
  3476. uint16_t cfg_value;
  3477. int i;
  3478. psli = &phba->sli;
  3479. /* Reset HBA */
  3480. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3481. "0325 Reset HBA Data: x%x x%x\n",
  3482. phba->pport->port_state, psli->sli_flag);
  3483. /* perform board reset */
  3484. phba->fc_eventTag = 0;
  3485. phba->link_events = 0;
  3486. phba->pport->fc_myDID = 0;
  3487. phba->pport->fc_prevDID = 0;
  3488. /* Turn off parity checking and serr during the physical reset */
  3489. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3490. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3491. (cfg_value &
  3492. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3493. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3494. /* Now toggle INITFF bit in the Host Control Register */
  3495. writel(HC_INITFF, phba->HCregaddr);
  3496. mdelay(1);
  3497. readl(phba->HCregaddr); /* flush */
  3498. writel(0, phba->HCregaddr);
  3499. readl(phba->HCregaddr); /* flush */
  3500. /* Restore PCI cmd register */
  3501. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3502. /* Initialize relevant SLI info */
  3503. for (i = 0; i < psli->num_rings; i++) {
  3504. pring = &psli->ring[i];
  3505. pring->flag = 0;
  3506. pring->rspidx = 0;
  3507. pring->next_cmdidx = 0;
  3508. pring->local_getidx = 0;
  3509. pring->cmdidx = 0;
  3510. pring->missbufcnt = 0;
  3511. }
  3512. phba->link_state = LPFC_WARM_START;
  3513. return 0;
  3514. }
  3515. /**
  3516. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3517. * @phba: Pointer to HBA context object.
  3518. *
  3519. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3520. * checking during resets the device. The caller is not required to hold
  3521. * any locks.
  3522. *
  3523. * This function returns 0 always.
  3524. **/
  3525. int
  3526. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3527. {
  3528. struct lpfc_sli *psli = &phba->sli;
  3529. uint16_t cfg_value;
  3530. /* Reset HBA */
  3531. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3532. "0295 Reset HBA Data: x%x x%x\n",
  3533. phba->pport->port_state, psli->sli_flag);
  3534. /* perform board reset */
  3535. phba->fc_eventTag = 0;
  3536. phba->link_events = 0;
  3537. phba->pport->fc_myDID = 0;
  3538. phba->pport->fc_prevDID = 0;
  3539. spin_lock_irq(&phba->hbalock);
  3540. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3541. phba->fcf.fcf_flag = 0;
  3542. spin_unlock_irq(&phba->hbalock);
  3543. /* Now physically reset the device */
  3544. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3545. "0389 Performing PCI function reset!\n");
  3546. /* Turn off parity checking and serr during the physical reset */
  3547. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3548. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3549. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3550. /* Perform FCoE PCI function reset */
  3551. lpfc_sli4_queue_destroy(phba);
  3552. lpfc_pci_function_reset(phba);
  3553. /* Restore PCI cmd register */
  3554. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3555. return 0;
  3556. }
  3557. /**
  3558. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3559. * @phba: Pointer to HBA context object.
  3560. *
  3561. * This function is called in the SLI initialization code path to
  3562. * restart the HBA. The caller is not required to hold any lock.
  3563. * This function writes MBX_RESTART mailbox command to the SLIM and
  3564. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3565. * function to free any pending commands. The function enables
  3566. * POST only during the first initialization. The function returns zero.
  3567. * The function does not guarantee completion of MBX_RESTART mailbox
  3568. * command before the return of this function.
  3569. **/
  3570. static int
  3571. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3572. {
  3573. MAILBOX_t *mb;
  3574. struct lpfc_sli *psli;
  3575. volatile uint32_t word0;
  3576. void __iomem *to_slim;
  3577. uint32_t hba_aer_enabled;
  3578. spin_lock_irq(&phba->hbalock);
  3579. /* Take PCIe device Advanced Error Reporting (AER) state */
  3580. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3581. psli = &phba->sli;
  3582. /* Restart HBA */
  3583. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3584. "0337 Restart HBA Data: x%x x%x\n",
  3585. phba->pport->port_state, psli->sli_flag);
  3586. word0 = 0;
  3587. mb = (MAILBOX_t *) &word0;
  3588. mb->mbxCommand = MBX_RESTART;
  3589. mb->mbxHc = 1;
  3590. lpfc_reset_barrier(phba);
  3591. to_slim = phba->MBslimaddr;
  3592. writel(*(uint32_t *) mb, to_slim);
  3593. readl(to_slim); /* flush */
  3594. /* Only skip post after fc_ffinit is completed */
  3595. if (phba->pport->port_state)
  3596. word0 = 1; /* This is really setting up word1 */
  3597. else
  3598. word0 = 0; /* This is really setting up word1 */
  3599. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3600. writel(*(uint32_t *) mb, to_slim);
  3601. readl(to_slim); /* flush */
  3602. lpfc_sli_brdreset(phba);
  3603. phba->pport->stopped = 0;
  3604. phba->link_state = LPFC_INIT_START;
  3605. phba->hba_flag = 0;
  3606. spin_unlock_irq(&phba->hbalock);
  3607. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3608. psli->stats_start = get_seconds();
  3609. /* Give the INITFF and Post time to settle. */
  3610. mdelay(100);
  3611. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3612. if (hba_aer_enabled)
  3613. pci_disable_pcie_error_reporting(phba->pcidev);
  3614. lpfc_hba_down_post(phba);
  3615. return 0;
  3616. }
  3617. /**
  3618. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3619. * @phba: Pointer to HBA context object.
  3620. *
  3621. * This function is called in the SLI initialization code path to restart
  3622. * a SLI4 HBA. The caller is not required to hold any lock.
  3623. * At the end of the function, it calls lpfc_hba_down_post function to
  3624. * free any pending commands.
  3625. **/
  3626. static int
  3627. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3628. {
  3629. struct lpfc_sli *psli = &phba->sli;
  3630. uint32_t hba_aer_enabled;
  3631. /* Restart HBA */
  3632. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3633. "0296 Restart HBA Data: x%x x%x\n",
  3634. phba->pport->port_state, psli->sli_flag);
  3635. /* Take PCIe device Advanced Error Reporting (AER) state */
  3636. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3637. lpfc_sli4_brdreset(phba);
  3638. spin_lock_irq(&phba->hbalock);
  3639. phba->pport->stopped = 0;
  3640. phba->link_state = LPFC_INIT_START;
  3641. phba->hba_flag = 0;
  3642. spin_unlock_irq(&phba->hbalock);
  3643. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3644. psli->stats_start = get_seconds();
  3645. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3646. if (hba_aer_enabled)
  3647. pci_disable_pcie_error_reporting(phba->pcidev);
  3648. lpfc_hba_down_post(phba);
  3649. return 0;
  3650. }
  3651. /**
  3652. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3653. * @phba: Pointer to HBA context object.
  3654. *
  3655. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3656. * API jump table function pointer from the lpfc_hba struct.
  3657. **/
  3658. int
  3659. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3660. {
  3661. return phba->lpfc_sli_brdrestart(phba);
  3662. }
  3663. /**
  3664. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3665. * @phba: Pointer to HBA context object.
  3666. *
  3667. * This function is called after a HBA restart to wait for successful
  3668. * restart of the HBA. Successful restart of the HBA is indicated by
  3669. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3670. * iteration, the function will restart the HBA again. The function returns
  3671. * zero if HBA successfully restarted else returns negative error code.
  3672. **/
  3673. static int
  3674. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3675. {
  3676. uint32_t status, i = 0;
  3677. /* Read the HBA Host Status Register */
  3678. if (lpfc_readl(phba->HSregaddr, &status))
  3679. return -EIO;
  3680. /* Check status register to see what current state is */
  3681. i = 0;
  3682. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3683. /* Check every 10ms for 10 retries, then every 100ms for 90
  3684. * retries, then every 1 sec for 50 retires for a total of
  3685. * ~60 seconds before reset the board again and check every
  3686. * 1 sec for 50 retries. The up to 60 seconds before the
  3687. * board ready is required by the Falcon FIPS zeroization
  3688. * complete, and any reset the board in between shall cause
  3689. * restart of zeroization, further delay the board ready.
  3690. */
  3691. if (i++ >= 200) {
  3692. /* Adapter failed to init, timeout, status reg
  3693. <status> */
  3694. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3695. "0436 Adapter failed to init, "
  3696. "timeout, status reg x%x, "
  3697. "FW Data: A8 x%x AC x%x\n", status,
  3698. readl(phba->MBslimaddr + 0xa8),
  3699. readl(phba->MBslimaddr + 0xac));
  3700. phba->link_state = LPFC_HBA_ERROR;
  3701. return -ETIMEDOUT;
  3702. }
  3703. /* Check to see if any errors occurred during init */
  3704. if (status & HS_FFERM) {
  3705. /* ERROR: During chipset initialization */
  3706. /* Adapter failed to init, chipset, status reg
  3707. <status> */
  3708. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3709. "0437 Adapter failed to init, "
  3710. "chipset, status reg x%x, "
  3711. "FW Data: A8 x%x AC x%x\n", status,
  3712. readl(phba->MBslimaddr + 0xa8),
  3713. readl(phba->MBslimaddr + 0xac));
  3714. phba->link_state = LPFC_HBA_ERROR;
  3715. return -EIO;
  3716. }
  3717. if (i <= 10)
  3718. msleep(10);
  3719. else if (i <= 100)
  3720. msleep(100);
  3721. else
  3722. msleep(1000);
  3723. if (i == 150) {
  3724. /* Do post */
  3725. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3726. lpfc_sli_brdrestart(phba);
  3727. }
  3728. /* Read the HBA Host Status Register */
  3729. if (lpfc_readl(phba->HSregaddr, &status))
  3730. return -EIO;
  3731. }
  3732. /* Check to see if any errors occurred during init */
  3733. if (status & HS_FFERM) {
  3734. /* ERROR: During chipset initialization */
  3735. /* Adapter failed to init, chipset, status reg <status> */
  3736. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3737. "0438 Adapter failed to init, chipset, "
  3738. "status reg x%x, "
  3739. "FW Data: A8 x%x AC x%x\n", status,
  3740. readl(phba->MBslimaddr + 0xa8),
  3741. readl(phba->MBslimaddr + 0xac));
  3742. phba->link_state = LPFC_HBA_ERROR;
  3743. return -EIO;
  3744. }
  3745. /* Clear all interrupt enable conditions */
  3746. writel(0, phba->HCregaddr);
  3747. readl(phba->HCregaddr); /* flush */
  3748. /* setup host attn register */
  3749. writel(0xffffffff, phba->HAregaddr);
  3750. readl(phba->HAregaddr); /* flush */
  3751. return 0;
  3752. }
  3753. /**
  3754. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3755. *
  3756. * This function calculates and returns the number of HBQs required to be
  3757. * configured.
  3758. **/
  3759. int
  3760. lpfc_sli_hbq_count(void)
  3761. {
  3762. return ARRAY_SIZE(lpfc_hbq_defs);
  3763. }
  3764. /**
  3765. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3766. *
  3767. * This function adds the number of hbq entries in every HBQ to get
  3768. * the total number of hbq entries required for the HBA and returns
  3769. * the total count.
  3770. **/
  3771. static int
  3772. lpfc_sli_hbq_entry_count(void)
  3773. {
  3774. int hbq_count = lpfc_sli_hbq_count();
  3775. int count = 0;
  3776. int i;
  3777. for (i = 0; i < hbq_count; ++i)
  3778. count += lpfc_hbq_defs[i]->entry_count;
  3779. return count;
  3780. }
  3781. /**
  3782. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3783. *
  3784. * This function calculates amount of memory required for all hbq entries
  3785. * to be configured and returns the total memory required.
  3786. **/
  3787. int
  3788. lpfc_sli_hbq_size(void)
  3789. {
  3790. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3791. }
  3792. /**
  3793. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3794. * @phba: Pointer to HBA context object.
  3795. *
  3796. * This function is called during the SLI initialization to configure
  3797. * all the HBQs and post buffers to the HBQ. The caller is not
  3798. * required to hold any locks. This function will return zero if successful
  3799. * else it will return negative error code.
  3800. **/
  3801. static int
  3802. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3803. {
  3804. int hbq_count = lpfc_sli_hbq_count();
  3805. LPFC_MBOXQ_t *pmb;
  3806. MAILBOX_t *pmbox;
  3807. uint32_t hbqno;
  3808. uint32_t hbq_entry_index;
  3809. /* Get a Mailbox buffer to setup mailbox
  3810. * commands for HBA initialization
  3811. */
  3812. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3813. if (!pmb)
  3814. return -ENOMEM;
  3815. pmbox = &pmb->u.mb;
  3816. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3817. phba->link_state = LPFC_INIT_MBX_CMDS;
  3818. phba->hbq_in_use = 1;
  3819. hbq_entry_index = 0;
  3820. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3821. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3822. phba->hbqs[hbqno].hbqPutIdx = 0;
  3823. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3824. phba->hbqs[hbqno].entry_count =
  3825. lpfc_hbq_defs[hbqno]->entry_count;
  3826. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3827. hbq_entry_index, pmb);
  3828. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3829. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3830. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3831. mbxStatus <status>, ring <num> */
  3832. lpfc_printf_log(phba, KERN_ERR,
  3833. LOG_SLI | LOG_VPORT,
  3834. "1805 Adapter failed to init. "
  3835. "Data: x%x x%x x%x\n",
  3836. pmbox->mbxCommand,
  3837. pmbox->mbxStatus, hbqno);
  3838. phba->link_state = LPFC_HBA_ERROR;
  3839. mempool_free(pmb, phba->mbox_mem_pool);
  3840. return -ENXIO;
  3841. }
  3842. }
  3843. phba->hbq_count = hbq_count;
  3844. mempool_free(pmb, phba->mbox_mem_pool);
  3845. /* Initially populate or replenish the HBQs */
  3846. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3847. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3848. return 0;
  3849. }
  3850. /**
  3851. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3852. * @phba: Pointer to HBA context object.
  3853. *
  3854. * This function is called during the SLI initialization to configure
  3855. * all the HBQs and post buffers to the HBQ. The caller is not
  3856. * required to hold any locks. This function will return zero if successful
  3857. * else it will return negative error code.
  3858. **/
  3859. static int
  3860. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3861. {
  3862. phba->hbq_in_use = 1;
  3863. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3864. phba->hbq_count = 1;
  3865. /* Initially populate or replenish the HBQs */
  3866. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3867. return 0;
  3868. }
  3869. /**
  3870. * lpfc_sli_config_port - Issue config port mailbox command
  3871. * @phba: Pointer to HBA context object.
  3872. * @sli_mode: sli mode - 2/3
  3873. *
  3874. * This function is called by the sli intialization code path
  3875. * to issue config_port mailbox command. This function restarts the
  3876. * HBA firmware and issues a config_port mailbox command to configure
  3877. * the SLI interface in the sli mode specified by sli_mode
  3878. * variable. The caller is not required to hold any locks.
  3879. * The function returns 0 if successful, else returns negative error
  3880. * code.
  3881. **/
  3882. int
  3883. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3884. {
  3885. LPFC_MBOXQ_t *pmb;
  3886. uint32_t resetcount = 0, rc = 0, done = 0;
  3887. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3888. if (!pmb) {
  3889. phba->link_state = LPFC_HBA_ERROR;
  3890. return -ENOMEM;
  3891. }
  3892. phba->sli_rev = sli_mode;
  3893. while (resetcount < 2 && !done) {
  3894. spin_lock_irq(&phba->hbalock);
  3895. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3896. spin_unlock_irq(&phba->hbalock);
  3897. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3898. lpfc_sli_brdrestart(phba);
  3899. rc = lpfc_sli_chipset_init(phba);
  3900. if (rc)
  3901. break;
  3902. spin_lock_irq(&phba->hbalock);
  3903. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3904. spin_unlock_irq(&phba->hbalock);
  3905. resetcount++;
  3906. /* Call pre CONFIG_PORT mailbox command initialization. A
  3907. * value of 0 means the call was successful. Any other
  3908. * nonzero value is a failure, but if ERESTART is returned,
  3909. * the driver may reset the HBA and try again.
  3910. */
  3911. rc = lpfc_config_port_prep(phba);
  3912. if (rc == -ERESTART) {
  3913. phba->link_state = LPFC_LINK_UNKNOWN;
  3914. continue;
  3915. } else if (rc)
  3916. break;
  3917. phba->link_state = LPFC_INIT_MBX_CMDS;
  3918. lpfc_config_port(phba, pmb);
  3919. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3920. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3921. LPFC_SLI3_HBQ_ENABLED |
  3922. LPFC_SLI3_CRP_ENABLED |
  3923. LPFC_SLI3_BG_ENABLED |
  3924. LPFC_SLI3_DSS_ENABLED);
  3925. if (rc != MBX_SUCCESS) {
  3926. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3927. "0442 Adapter failed to init, mbxCmd x%x "
  3928. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3929. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3930. spin_lock_irq(&phba->hbalock);
  3931. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3932. spin_unlock_irq(&phba->hbalock);
  3933. rc = -ENXIO;
  3934. } else {
  3935. /* Allow asynchronous mailbox command to go through */
  3936. spin_lock_irq(&phba->hbalock);
  3937. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3938. spin_unlock_irq(&phba->hbalock);
  3939. done = 1;
  3940. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  3941. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  3942. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3943. "3110 Port did not grant ASABT\n");
  3944. }
  3945. }
  3946. if (!done) {
  3947. rc = -EINVAL;
  3948. goto do_prep_failed;
  3949. }
  3950. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3951. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3952. rc = -ENXIO;
  3953. goto do_prep_failed;
  3954. }
  3955. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3956. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3957. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3958. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3959. phba->max_vpi : phba->max_vports;
  3960. } else
  3961. phba->max_vpi = 0;
  3962. phba->fips_level = 0;
  3963. phba->fips_spec_rev = 0;
  3964. if (pmb->u.mb.un.varCfgPort.gdss) {
  3965. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3966. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  3967. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  3968. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3969. "2850 Security Crypto Active. FIPS x%d "
  3970. "(Spec Rev: x%d)",
  3971. phba->fips_level, phba->fips_spec_rev);
  3972. }
  3973. if (pmb->u.mb.un.varCfgPort.sec_err) {
  3974. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3975. "2856 Config Port Security Crypto "
  3976. "Error: x%x ",
  3977. pmb->u.mb.un.varCfgPort.sec_err);
  3978. }
  3979. if (pmb->u.mb.un.varCfgPort.gerbm)
  3980. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  3981. if (pmb->u.mb.un.varCfgPort.gcrp)
  3982. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  3983. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  3984. phba->port_gp = phba->mbox->us.s3_pgp.port;
  3985. if (phba->cfg_enable_bg) {
  3986. if (pmb->u.mb.un.varCfgPort.gbg)
  3987. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  3988. else
  3989. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3990. "0443 Adapter did not grant "
  3991. "BlockGuard\n");
  3992. }
  3993. } else {
  3994. phba->hbq_get = NULL;
  3995. phba->port_gp = phba->mbox->us.s2.port;
  3996. phba->max_vpi = 0;
  3997. }
  3998. do_prep_failed:
  3999. mempool_free(pmb, phba->mbox_mem_pool);
  4000. return rc;
  4001. }
  4002. /**
  4003. * lpfc_sli_hba_setup - SLI intialization function
  4004. * @phba: Pointer to HBA context object.
  4005. *
  4006. * This function is the main SLI intialization function. This function
  4007. * is called by the HBA intialization code, HBA reset code and HBA
  4008. * error attention handler code. Caller is not required to hold any
  4009. * locks. This function issues config_port mailbox command to configure
  4010. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4011. * calls the config_port_post function to issue init_link mailbox
  4012. * command and to start the discovery. The function will return zero
  4013. * if successful, else it will return negative error code.
  4014. **/
  4015. int
  4016. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4017. {
  4018. uint32_t rc;
  4019. int mode = 3, i;
  4020. int longs;
  4021. switch (lpfc_sli_mode) {
  4022. case 2:
  4023. if (phba->cfg_enable_npiv) {
  4024. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4025. "1824 NPIV enabled: Override lpfc_sli_mode "
  4026. "parameter (%d) to auto (0).\n",
  4027. lpfc_sli_mode);
  4028. break;
  4029. }
  4030. mode = 2;
  4031. break;
  4032. case 0:
  4033. case 3:
  4034. break;
  4035. default:
  4036. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4037. "1819 Unrecognized lpfc_sli_mode "
  4038. "parameter: %d.\n", lpfc_sli_mode);
  4039. break;
  4040. }
  4041. rc = lpfc_sli_config_port(phba, mode);
  4042. if (rc && lpfc_sli_mode == 3)
  4043. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4044. "1820 Unable to select SLI-3. "
  4045. "Not supported by adapter.\n");
  4046. if (rc && mode != 2)
  4047. rc = lpfc_sli_config_port(phba, 2);
  4048. if (rc)
  4049. goto lpfc_sli_hba_setup_error;
  4050. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4051. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4052. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4053. if (!rc) {
  4054. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4055. "2709 This device supports "
  4056. "Advanced Error Reporting (AER)\n");
  4057. spin_lock_irq(&phba->hbalock);
  4058. phba->hba_flag |= HBA_AER_ENABLED;
  4059. spin_unlock_irq(&phba->hbalock);
  4060. } else {
  4061. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4062. "2708 This device does not support "
  4063. "Advanced Error Reporting (AER)\n");
  4064. phba->cfg_aer_support = 0;
  4065. }
  4066. }
  4067. if (phba->sli_rev == 3) {
  4068. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4069. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4070. } else {
  4071. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4072. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4073. phba->sli3_options = 0;
  4074. }
  4075. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4076. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4077. phba->sli_rev, phba->max_vpi);
  4078. rc = lpfc_sli_ring_map(phba);
  4079. if (rc)
  4080. goto lpfc_sli_hba_setup_error;
  4081. /* Initialize VPIs. */
  4082. if (phba->sli_rev == LPFC_SLI_REV3) {
  4083. /*
  4084. * The VPI bitmask and physical ID array are allocated
  4085. * and initialized once only - at driver load. A port
  4086. * reset doesn't need to reinitialize this memory.
  4087. */
  4088. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4089. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4090. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4091. GFP_KERNEL);
  4092. if (!phba->vpi_bmask) {
  4093. rc = -ENOMEM;
  4094. goto lpfc_sli_hba_setup_error;
  4095. }
  4096. phba->vpi_ids = kzalloc(
  4097. (phba->max_vpi+1) * sizeof(uint16_t),
  4098. GFP_KERNEL);
  4099. if (!phba->vpi_ids) {
  4100. kfree(phba->vpi_bmask);
  4101. rc = -ENOMEM;
  4102. goto lpfc_sli_hba_setup_error;
  4103. }
  4104. for (i = 0; i < phba->max_vpi; i++)
  4105. phba->vpi_ids[i] = i;
  4106. }
  4107. }
  4108. /* Init HBQs */
  4109. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4110. rc = lpfc_sli_hbq_setup(phba);
  4111. if (rc)
  4112. goto lpfc_sli_hba_setup_error;
  4113. }
  4114. spin_lock_irq(&phba->hbalock);
  4115. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4116. spin_unlock_irq(&phba->hbalock);
  4117. rc = lpfc_config_port_post(phba);
  4118. if (rc)
  4119. goto lpfc_sli_hba_setup_error;
  4120. return rc;
  4121. lpfc_sli_hba_setup_error:
  4122. phba->link_state = LPFC_HBA_ERROR;
  4123. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4124. "0445 Firmware initialization failed\n");
  4125. return rc;
  4126. }
  4127. /**
  4128. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4129. * @phba: Pointer to HBA context object.
  4130. * @mboxq: mailbox pointer.
  4131. * This function issue a dump mailbox command to read config region
  4132. * 23 and parse the records in the region and populate driver
  4133. * data structure.
  4134. **/
  4135. static int
  4136. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4137. {
  4138. LPFC_MBOXQ_t *mboxq;
  4139. struct lpfc_dmabuf *mp;
  4140. struct lpfc_mqe *mqe;
  4141. uint32_t data_length;
  4142. int rc;
  4143. /* Program the default value of vlan_id and fc_map */
  4144. phba->valid_vlan = 0;
  4145. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4146. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4147. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4148. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4149. if (!mboxq)
  4150. return -ENOMEM;
  4151. mqe = &mboxq->u.mqe;
  4152. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4153. rc = -ENOMEM;
  4154. goto out_free_mboxq;
  4155. }
  4156. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4157. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4158. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4159. "(%d):2571 Mailbox cmd x%x Status x%x "
  4160. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4161. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4162. "CQ: x%x x%x x%x x%x\n",
  4163. mboxq->vport ? mboxq->vport->vpi : 0,
  4164. bf_get(lpfc_mqe_command, mqe),
  4165. bf_get(lpfc_mqe_status, mqe),
  4166. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4167. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4168. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4169. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4170. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4171. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4172. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4173. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4174. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4175. mboxq->mcqe.word0,
  4176. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4177. mboxq->mcqe.trailer);
  4178. if (rc) {
  4179. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4180. kfree(mp);
  4181. rc = -EIO;
  4182. goto out_free_mboxq;
  4183. }
  4184. data_length = mqe->un.mb_words[5];
  4185. if (data_length > DMP_RGN23_SIZE) {
  4186. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4187. kfree(mp);
  4188. rc = -EIO;
  4189. goto out_free_mboxq;
  4190. }
  4191. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4192. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4193. kfree(mp);
  4194. rc = 0;
  4195. out_free_mboxq:
  4196. mempool_free(mboxq, phba->mbox_mem_pool);
  4197. return rc;
  4198. }
  4199. /**
  4200. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4201. * @phba: pointer to lpfc hba data structure.
  4202. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4203. * @vpd: pointer to the memory to hold resulting port vpd data.
  4204. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4205. * On output, the number of data bytes in @vpd.
  4206. *
  4207. * This routine executes a READ_REV SLI4 mailbox command. In
  4208. * addition, this routine gets the port vpd data.
  4209. *
  4210. * Return codes
  4211. * 0 - successful
  4212. * -ENOMEM - could not allocated memory.
  4213. **/
  4214. static int
  4215. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4216. uint8_t *vpd, uint32_t *vpd_size)
  4217. {
  4218. int rc = 0;
  4219. uint32_t dma_size;
  4220. struct lpfc_dmabuf *dmabuf;
  4221. struct lpfc_mqe *mqe;
  4222. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4223. if (!dmabuf)
  4224. return -ENOMEM;
  4225. /*
  4226. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4227. * mailbox command.
  4228. */
  4229. dma_size = *vpd_size;
  4230. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4231. dma_size,
  4232. &dmabuf->phys,
  4233. GFP_KERNEL);
  4234. if (!dmabuf->virt) {
  4235. kfree(dmabuf);
  4236. return -ENOMEM;
  4237. }
  4238. memset(dmabuf->virt, 0, dma_size);
  4239. /*
  4240. * The SLI4 implementation of READ_REV conflicts at word1,
  4241. * bits 31:16 and SLI4 adds vpd functionality not present
  4242. * in SLI3. This code corrects the conflicts.
  4243. */
  4244. lpfc_read_rev(phba, mboxq);
  4245. mqe = &mboxq->u.mqe;
  4246. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4247. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4248. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4249. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4250. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4251. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4252. if (rc) {
  4253. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4254. dmabuf->virt, dmabuf->phys);
  4255. kfree(dmabuf);
  4256. return -EIO;
  4257. }
  4258. /*
  4259. * The available vpd length cannot be bigger than the
  4260. * DMA buffer passed to the port. Catch the less than
  4261. * case and update the caller's size.
  4262. */
  4263. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4264. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4265. memcpy(vpd, dmabuf->virt, *vpd_size);
  4266. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4267. dmabuf->virt, dmabuf->phys);
  4268. kfree(dmabuf);
  4269. return 0;
  4270. }
  4271. /**
  4272. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4273. * @phba: pointer to lpfc hba data structure.
  4274. *
  4275. * This routine retrieves SLI4 device physical port name this PCI function
  4276. * is attached to.
  4277. *
  4278. * Return codes
  4279. * 0 - sucessful
  4280. * otherwise - failed to retrieve physical port name
  4281. **/
  4282. static int
  4283. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4284. {
  4285. LPFC_MBOXQ_t *mboxq;
  4286. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4287. struct lpfc_controller_attribute *cntl_attr;
  4288. struct lpfc_mbx_get_port_name *get_port_name;
  4289. void *virtaddr = NULL;
  4290. uint32_t alloclen, reqlen;
  4291. uint32_t shdr_status, shdr_add_status;
  4292. union lpfc_sli4_cfg_shdr *shdr;
  4293. char cport_name = 0;
  4294. int rc;
  4295. /* We assume nothing at this point */
  4296. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4297. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4298. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4299. if (!mboxq)
  4300. return -ENOMEM;
  4301. /* obtain link type and link number via READ_CONFIG */
  4302. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4303. lpfc_sli4_read_config(phba);
  4304. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4305. goto retrieve_ppname;
  4306. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4307. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4308. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4309. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4310. LPFC_SLI4_MBX_NEMBED);
  4311. if (alloclen < reqlen) {
  4312. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4313. "3084 Allocated DMA memory size (%d) is "
  4314. "less than the requested DMA memory size "
  4315. "(%d)\n", alloclen, reqlen);
  4316. rc = -ENOMEM;
  4317. goto out_free_mboxq;
  4318. }
  4319. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4320. virtaddr = mboxq->sge_array->addr[0];
  4321. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4322. shdr = &mbx_cntl_attr->cfg_shdr;
  4323. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4324. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4325. if (shdr_status || shdr_add_status || rc) {
  4326. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4327. "3085 Mailbox x%x (x%x/x%x) failed, "
  4328. "rc:x%x, status:x%x, add_status:x%x\n",
  4329. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4330. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4331. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4332. rc, shdr_status, shdr_add_status);
  4333. rc = -ENXIO;
  4334. goto out_free_mboxq;
  4335. }
  4336. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4337. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4338. phba->sli4_hba.lnk_info.lnk_tp =
  4339. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4340. phba->sli4_hba.lnk_info.lnk_no =
  4341. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4342. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4343. "3086 lnk_type:%d, lnk_numb:%d\n",
  4344. phba->sli4_hba.lnk_info.lnk_tp,
  4345. phba->sli4_hba.lnk_info.lnk_no);
  4346. retrieve_ppname:
  4347. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4348. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4349. sizeof(struct lpfc_mbx_get_port_name) -
  4350. sizeof(struct lpfc_sli4_cfg_mhdr),
  4351. LPFC_SLI4_MBX_EMBED);
  4352. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4353. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4354. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4355. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4356. phba->sli4_hba.lnk_info.lnk_tp);
  4357. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4358. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4359. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4360. if (shdr_status || shdr_add_status || rc) {
  4361. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4362. "3087 Mailbox x%x (x%x/x%x) failed: "
  4363. "rc:x%x, status:x%x, add_status:x%x\n",
  4364. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4365. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4366. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4367. rc, shdr_status, shdr_add_status);
  4368. rc = -ENXIO;
  4369. goto out_free_mboxq;
  4370. }
  4371. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4372. case LPFC_LINK_NUMBER_0:
  4373. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4374. &get_port_name->u.response);
  4375. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4376. break;
  4377. case LPFC_LINK_NUMBER_1:
  4378. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4379. &get_port_name->u.response);
  4380. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4381. break;
  4382. case LPFC_LINK_NUMBER_2:
  4383. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4384. &get_port_name->u.response);
  4385. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4386. break;
  4387. case LPFC_LINK_NUMBER_3:
  4388. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4389. &get_port_name->u.response);
  4390. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4391. break;
  4392. default:
  4393. break;
  4394. }
  4395. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4396. phba->Port[0] = cport_name;
  4397. phba->Port[1] = '\0';
  4398. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4399. "3091 SLI get port name: %s\n", phba->Port);
  4400. }
  4401. out_free_mboxq:
  4402. if (rc != MBX_TIMEOUT) {
  4403. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4404. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4405. else
  4406. mempool_free(mboxq, phba->mbox_mem_pool);
  4407. }
  4408. return rc;
  4409. }
  4410. /**
  4411. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4412. * @phba: pointer to lpfc hba data structure.
  4413. *
  4414. * This routine is called to explicitly arm the SLI4 device's completion and
  4415. * event queues
  4416. **/
  4417. static void
  4418. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4419. {
  4420. uint8_t fcp_eqidx;
  4421. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4422. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4423. fcp_eqidx = 0;
  4424. if (phba->sli4_hba.fcp_cq) {
  4425. do
  4426. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4427. LPFC_QUEUE_REARM);
  4428. while (++fcp_eqidx < phba->cfg_fcp_eq_count);
  4429. }
  4430. lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
  4431. if (phba->sli4_hba.fp_eq) {
  4432. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
  4433. fcp_eqidx++)
  4434. lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
  4435. LPFC_QUEUE_REARM);
  4436. }
  4437. }
  4438. /**
  4439. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4440. * @phba: Pointer to HBA context object.
  4441. * @type: The resource extent type.
  4442. * @extnt_count: buffer to hold port available extent count.
  4443. * @extnt_size: buffer to hold element count per extent.
  4444. *
  4445. * This function calls the port and retrievs the number of available
  4446. * extents and their size for a particular extent type.
  4447. *
  4448. * Returns: 0 if successful. Nonzero otherwise.
  4449. **/
  4450. int
  4451. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4452. uint16_t *extnt_count, uint16_t *extnt_size)
  4453. {
  4454. int rc = 0;
  4455. uint32_t length;
  4456. uint32_t mbox_tmo;
  4457. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4458. LPFC_MBOXQ_t *mbox;
  4459. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4460. if (!mbox)
  4461. return -ENOMEM;
  4462. /* Find out how many extents are available for this resource type */
  4463. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4464. sizeof(struct lpfc_sli4_cfg_mhdr));
  4465. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4466. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4467. length, LPFC_SLI4_MBX_EMBED);
  4468. /* Send an extents count of 0 - the GET doesn't use it. */
  4469. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4470. LPFC_SLI4_MBX_EMBED);
  4471. if (unlikely(rc)) {
  4472. rc = -EIO;
  4473. goto err_exit;
  4474. }
  4475. if (!phba->sli4_hba.intr_enable)
  4476. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4477. else {
  4478. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4479. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4480. }
  4481. if (unlikely(rc)) {
  4482. rc = -EIO;
  4483. goto err_exit;
  4484. }
  4485. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4486. if (bf_get(lpfc_mbox_hdr_status,
  4487. &rsrc_info->header.cfg_shdr.response)) {
  4488. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4489. "2930 Failed to get resource extents "
  4490. "Status 0x%x Add'l Status 0x%x\n",
  4491. bf_get(lpfc_mbox_hdr_status,
  4492. &rsrc_info->header.cfg_shdr.response),
  4493. bf_get(lpfc_mbox_hdr_add_status,
  4494. &rsrc_info->header.cfg_shdr.response));
  4495. rc = -EIO;
  4496. goto err_exit;
  4497. }
  4498. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4499. &rsrc_info->u.rsp);
  4500. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4501. &rsrc_info->u.rsp);
  4502. err_exit:
  4503. mempool_free(mbox, phba->mbox_mem_pool);
  4504. return rc;
  4505. }
  4506. /**
  4507. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4508. * @phba: Pointer to HBA context object.
  4509. * @type: The extent type to check.
  4510. *
  4511. * This function reads the current available extents from the port and checks
  4512. * if the extent count or extent size has changed since the last access.
  4513. * Callers use this routine post port reset to understand if there is a
  4514. * extent reprovisioning requirement.
  4515. *
  4516. * Returns:
  4517. * -Error: error indicates problem.
  4518. * 1: Extent count or size has changed.
  4519. * 0: No changes.
  4520. **/
  4521. static int
  4522. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4523. {
  4524. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4525. uint16_t size_diff, rsrc_ext_size;
  4526. int rc = 0;
  4527. struct lpfc_rsrc_blks *rsrc_entry;
  4528. struct list_head *rsrc_blk_list = NULL;
  4529. size_diff = 0;
  4530. curr_ext_cnt = 0;
  4531. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4532. &rsrc_ext_cnt,
  4533. &rsrc_ext_size);
  4534. if (unlikely(rc))
  4535. return -EIO;
  4536. switch (type) {
  4537. case LPFC_RSC_TYPE_FCOE_RPI:
  4538. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4539. break;
  4540. case LPFC_RSC_TYPE_FCOE_VPI:
  4541. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4542. break;
  4543. case LPFC_RSC_TYPE_FCOE_XRI:
  4544. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4545. break;
  4546. case LPFC_RSC_TYPE_FCOE_VFI:
  4547. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4548. break;
  4549. default:
  4550. break;
  4551. }
  4552. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4553. curr_ext_cnt++;
  4554. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4555. size_diff++;
  4556. }
  4557. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4558. rc = 1;
  4559. return rc;
  4560. }
  4561. /**
  4562. * lpfc_sli4_cfg_post_extnts -
  4563. * @phba: Pointer to HBA context object.
  4564. * @extnt_cnt - number of available extents.
  4565. * @type - the extent type (rpi, xri, vfi, vpi).
  4566. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4567. * @mbox - pointer to the caller's allocated mailbox structure.
  4568. *
  4569. * This function executes the extents allocation request. It also
  4570. * takes care of the amount of memory needed to allocate or get the
  4571. * allocated extents. It is the caller's responsibility to evaluate
  4572. * the response.
  4573. *
  4574. * Returns:
  4575. * -Error: Error value describes the condition found.
  4576. * 0: if successful
  4577. **/
  4578. static int
  4579. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
  4580. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4581. {
  4582. int rc = 0;
  4583. uint32_t req_len;
  4584. uint32_t emb_len;
  4585. uint32_t alloc_len, mbox_tmo;
  4586. /* Calculate the total requested length of the dma memory */
  4587. req_len = *extnt_cnt * sizeof(uint16_t);
  4588. /*
  4589. * Calculate the size of an embedded mailbox. The uint32_t
  4590. * accounts for extents-specific word.
  4591. */
  4592. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4593. sizeof(uint32_t);
  4594. /*
  4595. * Presume the allocation and response will fit into an embedded
  4596. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4597. */
  4598. *emb = LPFC_SLI4_MBX_EMBED;
  4599. if (req_len > emb_len) {
  4600. req_len = *extnt_cnt * sizeof(uint16_t) +
  4601. sizeof(union lpfc_sli4_cfg_shdr) +
  4602. sizeof(uint32_t);
  4603. *emb = LPFC_SLI4_MBX_NEMBED;
  4604. }
  4605. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4606. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4607. req_len, *emb);
  4608. if (alloc_len < req_len) {
  4609. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4610. "2982 Allocated DMA memory size (x%x) is "
  4611. "less than the requested DMA memory "
  4612. "size (x%x)\n", alloc_len, req_len);
  4613. return -ENOMEM;
  4614. }
  4615. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
  4616. if (unlikely(rc))
  4617. return -EIO;
  4618. if (!phba->sli4_hba.intr_enable)
  4619. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4620. else {
  4621. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4622. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4623. }
  4624. if (unlikely(rc))
  4625. rc = -EIO;
  4626. return rc;
  4627. }
  4628. /**
  4629. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4630. * @phba: Pointer to HBA context object.
  4631. * @type: The resource extent type to allocate.
  4632. *
  4633. * This function allocates the number of elements for the specified
  4634. * resource type.
  4635. **/
  4636. static int
  4637. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4638. {
  4639. bool emb = false;
  4640. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4641. uint16_t rsrc_id, rsrc_start, j, k;
  4642. uint16_t *ids;
  4643. int i, rc;
  4644. unsigned long longs;
  4645. unsigned long *bmask;
  4646. struct lpfc_rsrc_blks *rsrc_blks;
  4647. LPFC_MBOXQ_t *mbox;
  4648. uint32_t length;
  4649. struct lpfc_id_range *id_array = NULL;
  4650. void *virtaddr = NULL;
  4651. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4652. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4653. struct list_head *ext_blk_list;
  4654. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4655. &rsrc_cnt,
  4656. &rsrc_size);
  4657. if (unlikely(rc))
  4658. return -EIO;
  4659. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4660. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4661. "3009 No available Resource Extents "
  4662. "for resource type 0x%x: Count: 0x%x, "
  4663. "Size 0x%x\n", type, rsrc_cnt,
  4664. rsrc_size);
  4665. return -ENOMEM;
  4666. }
  4667. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
  4668. "2903 Available Resource Extents "
  4669. "for resource type 0x%x: Count: 0x%x, "
  4670. "Size 0x%x\n", type, rsrc_cnt,
  4671. rsrc_size);
  4672. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4673. if (!mbox)
  4674. return -ENOMEM;
  4675. rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
  4676. if (unlikely(rc)) {
  4677. rc = -EIO;
  4678. goto err_exit;
  4679. }
  4680. /*
  4681. * Figure out where the response is located. Then get local pointers
  4682. * to the response data. The port does not guarantee to respond to
  4683. * all extents counts request so update the local variable with the
  4684. * allocated count from the port.
  4685. */
  4686. if (emb == LPFC_SLI4_MBX_EMBED) {
  4687. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4688. id_array = &rsrc_ext->u.rsp.id[0];
  4689. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4690. } else {
  4691. virtaddr = mbox->sge_array->addr[0];
  4692. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4693. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4694. id_array = &n_rsrc->id;
  4695. }
  4696. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4697. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4698. /*
  4699. * Based on the resource size and count, correct the base and max
  4700. * resource values.
  4701. */
  4702. length = sizeof(struct lpfc_rsrc_blks);
  4703. switch (type) {
  4704. case LPFC_RSC_TYPE_FCOE_RPI:
  4705. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4706. sizeof(unsigned long),
  4707. GFP_KERNEL);
  4708. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4709. rc = -ENOMEM;
  4710. goto err_exit;
  4711. }
  4712. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4713. sizeof(uint16_t),
  4714. GFP_KERNEL);
  4715. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4716. kfree(phba->sli4_hba.rpi_bmask);
  4717. rc = -ENOMEM;
  4718. goto err_exit;
  4719. }
  4720. /*
  4721. * The next_rpi was initialized with the maximum available
  4722. * count but the port may allocate a smaller number. Catch
  4723. * that case and update the next_rpi.
  4724. */
  4725. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4726. /* Initialize local ptrs for common extent processing later. */
  4727. bmask = phba->sli4_hba.rpi_bmask;
  4728. ids = phba->sli4_hba.rpi_ids;
  4729. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4730. break;
  4731. case LPFC_RSC_TYPE_FCOE_VPI:
  4732. phba->vpi_bmask = kzalloc(longs *
  4733. sizeof(unsigned long),
  4734. GFP_KERNEL);
  4735. if (unlikely(!phba->vpi_bmask)) {
  4736. rc = -ENOMEM;
  4737. goto err_exit;
  4738. }
  4739. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4740. sizeof(uint16_t),
  4741. GFP_KERNEL);
  4742. if (unlikely(!phba->vpi_ids)) {
  4743. kfree(phba->vpi_bmask);
  4744. rc = -ENOMEM;
  4745. goto err_exit;
  4746. }
  4747. /* Initialize local ptrs for common extent processing later. */
  4748. bmask = phba->vpi_bmask;
  4749. ids = phba->vpi_ids;
  4750. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4751. break;
  4752. case LPFC_RSC_TYPE_FCOE_XRI:
  4753. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4754. sizeof(unsigned long),
  4755. GFP_KERNEL);
  4756. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4757. rc = -ENOMEM;
  4758. goto err_exit;
  4759. }
  4760. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4761. sizeof(uint16_t),
  4762. GFP_KERNEL);
  4763. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4764. kfree(phba->sli4_hba.xri_bmask);
  4765. rc = -ENOMEM;
  4766. goto err_exit;
  4767. }
  4768. /* Initialize local ptrs for common extent processing later. */
  4769. bmask = phba->sli4_hba.xri_bmask;
  4770. ids = phba->sli4_hba.xri_ids;
  4771. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4772. break;
  4773. case LPFC_RSC_TYPE_FCOE_VFI:
  4774. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4775. sizeof(unsigned long),
  4776. GFP_KERNEL);
  4777. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4778. rc = -ENOMEM;
  4779. goto err_exit;
  4780. }
  4781. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4782. sizeof(uint16_t),
  4783. GFP_KERNEL);
  4784. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4785. kfree(phba->sli4_hba.vfi_bmask);
  4786. rc = -ENOMEM;
  4787. goto err_exit;
  4788. }
  4789. /* Initialize local ptrs for common extent processing later. */
  4790. bmask = phba->sli4_hba.vfi_bmask;
  4791. ids = phba->sli4_hba.vfi_ids;
  4792. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4793. break;
  4794. default:
  4795. /* Unsupported Opcode. Fail call. */
  4796. id_array = NULL;
  4797. bmask = NULL;
  4798. ids = NULL;
  4799. ext_blk_list = NULL;
  4800. goto err_exit;
  4801. }
  4802. /*
  4803. * Complete initializing the extent configuration with the
  4804. * allocated ids assigned to this function. The bitmask serves
  4805. * as an index into the array and manages the available ids. The
  4806. * array just stores the ids communicated to the port via the wqes.
  4807. */
  4808. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4809. if ((i % 2) == 0)
  4810. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4811. &id_array[k]);
  4812. else
  4813. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4814. &id_array[k]);
  4815. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4816. if (unlikely(!rsrc_blks)) {
  4817. rc = -ENOMEM;
  4818. kfree(bmask);
  4819. kfree(ids);
  4820. goto err_exit;
  4821. }
  4822. rsrc_blks->rsrc_start = rsrc_id;
  4823. rsrc_blks->rsrc_size = rsrc_size;
  4824. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4825. rsrc_start = rsrc_id;
  4826. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4827. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4828. lpfc_sli4_get_els_iocb_cnt(phba);
  4829. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4830. ids[j] = rsrc_id;
  4831. rsrc_id++;
  4832. j++;
  4833. }
  4834. /* Entire word processed. Get next word.*/
  4835. if ((i % 2) == 1)
  4836. k++;
  4837. }
  4838. err_exit:
  4839. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4840. return rc;
  4841. }
  4842. /**
  4843. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4844. * @phba: Pointer to HBA context object.
  4845. * @type: the extent's type.
  4846. *
  4847. * This function deallocates all extents of a particular resource type.
  4848. * SLI4 does not allow for deallocating a particular extent range. It
  4849. * is the caller's responsibility to release all kernel memory resources.
  4850. **/
  4851. static int
  4852. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4853. {
  4854. int rc;
  4855. uint32_t length, mbox_tmo = 0;
  4856. LPFC_MBOXQ_t *mbox;
  4857. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4858. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4859. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4860. if (!mbox)
  4861. return -ENOMEM;
  4862. /*
  4863. * This function sends an embedded mailbox because it only sends the
  4864. * the resource type. All extents of this type are released by the
  4865. * port.
  4866. */
  4867. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4868. sizeof(struct lpfc_sli4_cfg_mhdr));
  4869. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4870. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4871. length, LPFC_SLI4_MBX_EMBED);
  4872. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4873. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4874. LPFC_SLI4_MBX_EMBED);
  4875. if (unlikely(rc)) {
  4876. rc = -EIO;
  4877. goto out_free_mbox;
  4878. }
  4879. if (!phba->sli4_hba.intr_enable)
  4880. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4881. else {
  4882. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4883. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4884. }
  4885. if (unlikely(rc)) {
  4886. rc = -EIO;
  4887. goto out_free_mbox;
  4888. }
  4889. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4890. if (bf_get(lpfc_mbox_hdr_status,
  4891. &dealloc_rsrc->header.cfg_shdr.response)) {
  4892. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4893. "2919 Failed to release resource extents "
  4894. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4895. "Resource memory not released.\n",
  4896. type,
  4897. bf_get(lpfc_mbox_hdr_status,
  4898. &dealloc_rsrc->header.cfg_shdr.response),
  4899. bf_get(lpfc_mbox_hdr_add_status,
  4900. &dealloc_rsrc->header.cfg_shdr.response));
  4901. rc = -EIO;
  4902. goto out_free_mbox;
  4903. }
  4904. /* Release kernel memory resources for the specific type. */
  4905. switch (type) {
  4906. case LPFC_RSC_TYPE_FCOE_VPI:
  4907. kfree(phba->vpi_bmask);
  4908. kfree(phba->vpi_ids);
  4909. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4910. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4911. &phba->lpfc_vpi_blk_list, list) {
  4912. list_del_init(&rsrc_blk->list);
  4913. kfree(rsrc_blk);
  4914. }
  4915. break;
  4916. case LPFC_RSC_TYPE_FCOE_XRI:
  4917. kfree(phba->sli4_hba.xri_bmask);
  4918. kfree(phba->sli4_hba.xri_ids);
  4919. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4920. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4921. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4922. list_del_init(&rsrc_blk->list);
  4923. kfree(rsrc_blk);
  4924. }
  4925. break;
  4926. case LPFC_RSC_TYPE_FCOE_VFI:
  4927. kfree(phba->sli4_hba.vfi_bmask);
  4928. kfree(phba->sli4_hba.vfi_ids);
  4929. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4930. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4931. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  4932. list_del_init(&rsrc_blk->list);
  4933. kfree(rsrc_blk);
  4934. }
  4935. break;
  4936. case LPFC_RSC_TYPE_FCOE_RPI:
  4937. /* RPI bitmask and physical id array are cleaned up earlier. */
  4938. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4939. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  4940. list_del_init(&rsrc_blk->list);
  4941. kfree(rsrc_blk);
  4942. }
  4943. break;
  4944. default:
  4945. break;
  4946. }
  4947. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4948. out_free_mbox:
  4949. mempool_free(mbox, phba->mbox_mem_pool);
  4950. return rc;
  4951. }
  4952. /**
  4953. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  4954. * @phba: Pointer to HBA context object.
  4955. *
  4956. * This function allocates all SLI4 resource identifiers.
  4957. **/
  4958. int
  4959. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  4960. {
  4961. int i, rc, error = 0;
  4962. uint16_t count, base;
  4963. unsigned long longs;
  4964. if (!phba->sli4_hba.rpi_hdrs_in_use)
  4965. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  4966. if (phba->sli4_hba.extents_in_use) {
  4967. /*
  4968. * The port supports resource extents. The XRI, VPI, VFI, RPI
  4969. * resource extent count must be read and allocated before
  4970. * provisioning the resource id arrays.
  4971. */
  4972. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  4973. LPFC_IDX_RSRC_RDY) {
  4974. /*
  4975. * Extent-based resources are set - the driver could
  4976. * be in a port reset. Figure out if any corrective
  4977. * actions need to be taken.
  4978. */
  4979. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4980. LPFC_RSC_TYPE_FCOE_VFI);
  4981. if (rc != 0)
  4982. error++;
  4983. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4984. LPFC_RSC_TYPE_FCOE_VPI);
  4985. if (rc != 0)
  4986. error++;
  4987. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4988. LPFC_RSC_TYPE_FCOE_XRI);
  4989. if (rc != 0)
  4990. error++;
  4991. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4992. LPFC_RSC_TYPE_FCOE_RPI);
  4993. if (rc != 0)
  4994. error++;
  4995. /*
  4996. * It's possible that the number of resources
  4997. * provided to this port instance changed between
  4998. * resets. Detect this condition and reallocate
  4999. * resources. Otherwise, there is no action.
  5000. */
  5001. if (error) {
  5002. lpfc_printf_log(phba, KERN_INFO,
  5003. LOG_MBOX | LOG_INIT,
  5004. "2931 Detected extent resource "
  5005. "change. Reallocating all "
  5006. "extents.\n");
  5007. rc = lpfc_sli4_dealloc_extent(phba,
  5008. LPFC_RSC_TYPE_FCOE_VFI);
  5009. rc = lpfc_sli4_dealloc_extent(phba,
  5010. LPFC_RSC_TYPE_FCOE_VPI);
  5011. rc = lpfc_sli4_dealloc_extent(phba,
  5012. LPFC_RSC_TYPE_FCOE_XRI);
  5013. rc = lpfc_sli4_dealloc_extent(phba,
  5014. LPFC_RSC_TYPE_FCOE_RPI);
  5015. } else
  5016. return 0;
  5017. }
  5018. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5019. if (unlikely(rc))
  5020. goto err_exit;
  5021. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5022. if (unlikely(rc))
  5023. goto err_exit;
  5024. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5025. if (unlikely(rc))
  5026. goto err_exit;
  5027. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5028. if (unlikely(rc))
  5029. goto err_exit;
  5030. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5031. LPFC_IDX_RSRC_RDY);
  5032. return rc;
  5033. } else {
  5034. /*
  5035. * The port does not support resource extents. The XRI, VPI,
  5036. * VFI, RPI resource ids were determined from READ_CONFIG.
  5037. * Just allocate the bitmasks and provision the resource id
  5038. * arrays. If a port reset is active, the resources don't
  5039. * need any action - just exit.
  5040. */
  5041. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5042. LPFC_IDX_RSRC_RDY) {
  5043. lpfc_sli4_dealloc_resource_identifiers(phba);
  5044. lpfc_sli4_remove_rpis(phba);
  5045. }
  5046. /* RPIs. */
  5047. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5048. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5049. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5050. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5051. sizeof(unsigned long),
  5052. GFP_KERNEL);
  5053. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5054. rc = -ENOMEM;
  5055. goto err_exit;
  5056. }
  5057. phba->sli4_hba.rpi_ids = kzalloc(count *
  5058. sizeof(uint16_t),
  5059. GFP_KERNEL);
  5060. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5061. rc = -ENOMEM;
  5062. goto free_rpi_bmask;
  5063. }
  5064. for (i = 0; i < count; i++)
  5065. phba->sli4_hba.rpi_ids[i] = base + i;
  5066. /* VPIs. */
  5067. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5068. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5069. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5070. phba->vpi_bmask = kzalloc(longs *
  5071. sizeof(unsigned long),
  5072. GFP_KERNEL);
  5073. if (unlikely(!phba->vpi_bmask)) {
  5074. rc = -ENOMEM;
  5075. goto free_rpi_ids;
  5076. }
  5077. phba->vpi_ids = kzalloc(count *
  5078. sizeof(uint16_t),
  5079. GFP_KERNEL);
  5080. if (unlikely(!phba->vpi_ids)) {
  5081. rc = -ENOMEM;
  5082. goto free_vpi_bmask;
  5083. }
  5084. for (i = 0; i < count; i++)
  5085. phba->vpi_ids[i] = base + i;
  5086. /* XRIs. */
  5087. count = phba->sli4_hba.max_cfg_param.max_xri;
  5088. base = phba->sli4_hba.max_cfg_param.xri_base;
  5089. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5090. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5091. sizeof(unsigned long),
  5092. GFP_KERNEL);
  5093. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5094. rc = -ENOMEM;
  5095. goto free_vpi_ids;
  5096. }
  5097. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5098. phba->sli4_hba.xri_count = 0;
  5099. phba->sli4_hba.xri_ids = kzalloc(count *
  5100. sizeof(uint16_t),
  5101. GFP_KERNEL);
  5102. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5103. rc = -ENOMEM;
  5104. goto free_xri_bmask;
  5105. }
  5106. for (i = 0; i < count; i++)
  5107. phba->sli4_hba.xri_ids[i] = base + i;
  5108. /* VFIs. */
  5109. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5110. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5111. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5112. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5113. sizeof(unsigned long),
  5114. GFP_KERNEL);
  5115. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5116. rc = -ENOMEM;
  5117. goto free_xri_ids;
  5118. }
  5119. phba->sli4_hba.vfi_ids = kzalloc(count *
  5120. sizeof(uint16_t),
  5121. GFP_KERNEL);
  5122. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5123. rc = -ENOMEM;
  5124. goto free_vfi_bmask;
  5125. }
  5126. for (i = 0; i < count; i++)
  5127. phba->sli4_hba.vfi_ids[i] = base + i;
  5128. /*
  5129. * Mark all resources ready. An HBA reset doesn't need
  5130. * to reset the initialization.
  5131. */
  5132. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5133. LPFC_IDX_RSRC_RDY);
  5134. return 0;
  5135. }
  5136. free_vfi_bmask:
  5137. kfree(phba->sli4_hba.vfi_bmask);
  5138. free_xri_ids:
  5139. kfree(phba->sli4_hba.xri_ids);
  5140. free_xri_bmask:
  5141. kfree(phba->sli4_hba.xri_bmask);
  5142. free_vpi_ids:
  5143. kfree(phba->vpi_ids);
  5144. free_vpi_bmask:
  5145. kfree(phba->vpi_bmask);
  5146. free_rpi_ids:
  5147. kfree(phba->sli4_hba.rpi_ids);
  5148. free_rpi_bmask:
  5149. kfree(phba->sli4_hba.rpi_bmask);
  5150. err_exit:
  5151. return rc;
  5152. }
  5153. /**
  5154. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5155. * @phba: Pointer to HBA context object.
  5156. *
  5157. * This function allocates the number of elements for the specified
  5158. * resource type.
  5159. **/
  5160. int
  5161. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5162. {
  5163. if (phba->sli4_hba.extents_in_use) {
  5164. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5165. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5166. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5167. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5168. } else {
  5169. kfree(phba->vpi_bmask);
  5170. kfree(phba->vpi_ids);
  5171. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5172. kfree(phba->sli4_hba.xri_bmask);
  5173. kfree(phba->sli4_hba.xri_ids);
  5174. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5175. kfree(phba->sli4_hba.vfi_bmask);
  5176. kfree(phba->sli4_hba.vfi_ids);
  5177. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5178. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5179. }
  5180. return 0;
  5181. }
  5182. /**
  5183. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5184. * @phba: Pointer to HBA context object.
  5185. * @type: The resource extent type.
  5186. * @extnt_count: buffer to hold port extent count response
  5187. * @extnt_size: buffer to hold port extent size response.
  5188. *
  5189. * This function calls the port to read the host allocated extents
  5190. * for a particular type.
  5191. **/
  5192. int
  5193. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5194. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5195. {
  5196. bool emb;
  5197. int rc = 0;
  5198. uint16_t curr_blks = 0;
  5199. uint32_t req_len, emb_len;
  5200. uint32_t alloc_len, mbox_tmo;
  5201. struct list_head *blk_list_head;
  5202. struct lpfc_rsrc_blks *rsrc_blk;
  5203. LPFC_MBOXQ_t *mbox;
  5204. void *virtaddr = NULL;
  5205. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5206. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5207. union lpfc_sli4_cfg_shdr *shdr;
  5208. switch (type) {
  5209. case LPFC_RSC_TYPE_FCOE_VPI:
  5210. blk_list_head = &phba->lpfc_vpi_blk_list;
  5211. break;
  5212. case LPFC_RSC_TYPE_FCOE_XRI:
  5213. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5214. break;
  5215. case LPFC_RSC_TYPE_FCOE_VFI:
  5216. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5217. break;
  5218. case LPFC_RSC_TYPE_FCOE_RPI:
  5219. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5220. break;
  5221. default:
  5222. return -EIO;
  5223. }
  5224. /* Count the number of extents currently allocatd for this type. */
  5225. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5226. if (curr_blks == 0) {
  5227. /*
  5228. * The GET_ALLOCATED mailbox does not return the size,
  5229. * just the count. The size should be just the size
  5230. * stored in the current allocated block and all sizes
  5231. * for an extent type are the same so set the return
  5232. * value now.
  5233. */
  5234. *extnt_size = rsrc_blk->rsrc_size;
  5235. }
  5236. curr_blks++;
  5237. }
  5238. /* Calculate the total requested length of the dma memory. */
  5239. req_len = curr_blks * sizeof(uint16_t);
  5240. /*
  5241. * Calculate the size of an embedded mailbox. The uint32_t
  5242. * accounts for extents-specific word.
  5243. */
  5244. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5245. sizeof(uint32_t);
  5246. /*
  5247. * Presume the allocation and response will fit into an embedded
  5248. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5249. */
  5250. emb = LPFC_SLI4_MBX_EMBED;
  5251. req_len = emb_len;
  5252. if (req_len > emb_len) {
  5253. req_len = curr_blks * sizeof(uint16_t) +
  5254. sizeof(union lpfc_sli4_cfg_shdr) +
  5255. sizeof(uint32_t);
  5256. emb = LPFC_SLI4_MBX_NEMBED;
  5257. }
  5258. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5259. if (!mbox)
  5260. return -ENOMEM;
  5261. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5262. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5263. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5264. req_len, emb);
  5265. if (alloc_len < req_len) {
  5266. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5267. "2983 Allocated DMA memory size (x%x) is "
  5268. "less than the requested DMA memory "
  5269. "size (x%x)\n", alloc_len, req_len);
  5270. rc = -ENOMEM;
  5271. goto err_exit;
  5272. }
  5273. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5274. if (unlikely(rc)) {
  5275. rc = -EIO;
  5276. goto err_exit;
  5277. }
  5278. if (!phba->sli4_hba.intr_enable)
  5279. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5280. else {
  5281. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5282. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5283. }
  5284. if (unlikely(rc)) {
  5285. rc = -EIO;
  5286. goto err_exit;
  5287. }
  5288. /*
  5289. * Figure out where the response is located. Then get local pointers
  5290. * to the response data. The port does not guarantee to respond to
  5291. * all extents counts request so update the local variable with the
  5292. * allocated count from the port.
  5293. */
  5294. if (emb == LPFC_SLI4_MBX_EMBED) {
  5295. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5296. shdr = &rsrc_ext->header.cfg_shdr;
  5297. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5298. } else {
  5299. virtaddr = mbox->sge_array->addr[0];
  5300. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5301. shdr = &n_rsrc->cfg_shdr;
  5302. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5303. }
  5304. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5305. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5306. "2984 Failed to read allocated resources "
  5307. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5308. type,
  5309. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5310. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5311. rc = -EIO;
  5312. goto err_exit;
  5313. }
  5314. err_exit:
  5315. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5316. return rc;
  5317. }
  5318. /**
  5319. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5320. * @phba: Pointer to HBA context object.
  5321. *
  5322. * This function is the main SLI4 device intialization PCI function. This
  5323. * function is called by the HBA intialization code, HBA reset code and
  5324. * HBA error attention handler code. Caller is not required to hold any
  5325. * locks.
  5326. **/
  5327. int
  5328. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5329. {
  5330. int rc;
  5331. LPFC_MBOXQ_t *mboxq;
  5332. struct lpfc_mqe *mqe;
  5333. uint8_t *vpd;
  5334. uint32_t vpd_size;
  5335. uint32_t ftr_rsp = 0;
  5336. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5337. struct lpfc_vport *vport = phba->pport;
  5338. struct lpfc_dmabuf *mp;
  5339. /* Perform a PCI function reset to start from clean */
  5340. rc = lpfc_pci_function_reset(phba);
  5341. if (unlikely(rc))
  5342. return -ENODEV;
  5343. /* Check the HBA Host Status Register for readyness */
  5344. rc = lpfc_sli4_post_status_check(phba);
  5345. if (unlikely(rc))
  5346. return -ENODEV;
  5347. else {
  5348. spin_lock_irq(&phba->hbalock);
  5349. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5350. spin_unlock_irq(&phba->hbalock);
  5351. }
  5352. /*
  5353. * Allocate a single mailbox container for initializing the
  5354. * port.
  5355. */
  5356. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5357. if (!mboxq)
  5358. return -ENOMEM;
  5359. /* Issue READ_REV to collect vpd and FW information. */
  5360. vpd_size = SLI4_PAGE_SIZE;
  5361. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5362. if (!vpd) {
  5363. rc = -ENOMEM;
  5364. goto out_free_mbox;
  5365. }
  5366. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5367. if (unlikely(rc)) {
  5368. kfree(vpd);
  5369. goto out_free_mbox;
  5370. }
  5371. mqe = &mboxq->u.mqe;
  5372. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5373. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5374. phba->hba_flag |= HBA_FCOE_MODE;
  5375. else
  5376. phba->hba_flag &= ~HBA_FCOE_MODE;
  5377. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5378. LPFC_DCBX_CEE_MODE)
  5379. phba->hba_flag |= HBA_FIP_SUPPORT;
  5380. else
  5381. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5382. if (phba->sli_rev != LPFC_SLI_REV4) {
  5383. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5384. "0376 READ_REV Error. SLI Level %d "
  5385. "FCoE enabled %d\n",
  5386. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5387. rc = -EIO;
  5388. kfree(vpd);
  5389. goto out_free_mbox;
  5390. }
  5391. /*
  5392. * Continue initialization with default values even if driver failed
  5393. * to read FCoE param config regions, only read parameters if the
  5394. * board is FCoE
  5395. */
  5396. if (phba->hba_flag & HBA_FCOE_MODE &&
  5397. lpfc_sli4_read_fcoe_params(phba))
  5398. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5399. "2570 Failed to read FCoE parameters\n");
  5400. /*
  5401. * Retrieve sli4 device physical port name, failure of doing it
  5402. * is considered as non-fatal.
  5403. */
  5404. rc = lpfc_sli4_retrieve_pport_name(phba);
  5405. if (!rc)
  5406. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5407. "3080 Successful retrieving SLI4 device "
  5408. "physical port name: %s.\n", phba->Port);
  5409. /*
  5410. * Evaluate the read rev and vpd data. Populate the driver
  5411. * state with the results. If this routine fails, the failure
  5412. * is not fatal as the driver will use generic values.
  5413. */
  5414. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5415. if (unlikely(!rc)) {
  5416. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5417. "0377 Error %d parsing vpd. "
  5418. "Using defaults.\n", rc);
  5419. rc = 0;
  5420. }
  5421. kfree(vpd);
  5422. /* Save information as VPD data */
  5423. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5424. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5425. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5426. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5427. &mqe->un.read_rev);
  5428. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5429. &mqe->un.read_rev);
  5430. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5431. &mqe->un.read_rev);
  5432. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5433. &mqe->un.read_rev);
  5434. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5435. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5436. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5437. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5438. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5439. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5440. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5441. "(%d):0380 READ_REV Status x%x "
  5442. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5443. mboxq->vport ? mboxq->vport->vpi : 0,
  5444. bf_get(lpfc_mqe_status, mqe),
  5445. phba->vpd.rev.opFwName,
  5446. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5447. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5448. /*
  5449. * Discover the port's supported feature set and match it against the
  5450. * hosts requests.
  5451. */
  5452. lpfc_request_features(phba, mboxq);
  5453. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5454. if (unlikely(rc)) {
  5455. rc = -EIO;
  5456. goto out_free_mbox;
  5457. }
  5458. /*
  5459. * The port must support FCP initiator mode as this is the
  5460. * only mode running in the host.
  5461. */
  5462. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5463. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5464. "0378 No support for fcpi mode.\n");
  5465. ftr_rsp++;
  5466. }
  5467. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5468. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5469. else
  5470. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5471. /*
  5472. * If the port cannot support the host's requested features
  5473. * then turn off the global config parameters to disable the
  5474. * feature in the driver. This is not a fatal error.
  5475. */
  5476. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5477. if (phba->cfg_enable_bg) {
  5478. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5479. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5480. else
  5481. ftr_rsp++;
  5482. }
  5483. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5484. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5485. ftr_rsp++;
  5486. if (ftr_rsp) {
  5487. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5488. "0379 Feature Mismatch Data: x%08x %08x "
  5489. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5490. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5491. phba->cfg_enable_npiv, phba->max_vpi);
  5492. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5493. phba->cfg_enable_bg = 0;
  5494. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5495. phba->cfg_enable_npiv = 0;
  5496. }
  5497. /* These SLI3 features are assumed in SLI4 */
  5498. spin_lock_irq(&phba->hbalock);
  5499. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5500. spin_unlock_irq(&phba->hbalock);
  5501. /*
  5502. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5503. * calls depends on these resources to complete port setup.
  5504. */
  5505. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5506. if (rc) {
  5507. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5508. "2920 Failed to alloc Resource IDs "
  5509. "rc = x%x\n", rc);
  5510. goto out_free_mbox;
  5511. }
  5512. /* update physical xri mappings in the scsi buffers */
  5513. lpfc_scsi_buf_update(phba);
  5514. /* Read the port's service parameters. */
  5515. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5516. if (rc) {
  5517. phba->link_state = LPFC_HBA_ERROR;
  5518. rc = -ENOMEM;
  5519. goto out_free_mbox;
  5520. }
  5521. mboxq->vport = vport;
  5522. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5523. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5524. if (rc == MBX_SUCCESS) {
  5525. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5526. rc = 0;
  5527. }
  5528. /*
  5529. * This memory was allocated by the lpfc_read_sparam routine. Release
  5530. * it to the mbuf pool.
  5531. */
  5532. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5533. kfree(mp);
  5534. mboxq->context1 = NULL;
  5535. if (unlikely(rc)) {
  5536. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5537. "0382 READ_SPARAM command failed "
  5538. "status %d, mbxStatus x%x\n",
  5539. rc, bf_get(lpfc_mqe_status, mqe));
  5540. phba->link_state = LPFC_HBA_ERROR;
  5541. rc = -EIO;
  5542. goto out_free_mbox;
  5543. }
  5544. lpfc_update_vport_wwn(vport);
  5545. /* Update the fc_host data structures with new wwn. */
  5546. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5547. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5548. /* Register SGL pool to the device using non-embedded mailbox command */
  5549. if (!phba->sli4_hba.extents_in_use) {
  5550. rc = lpfc_sli4_post_els_sgl_list(phba);
  5551. if (unlikely(rc)) {
  5552. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5553. "0582 Error %d during els sgl post "
  5554. "operation\n", rc);
  5555. rc = -ENODEV;
  5556. goto out_free_mbox;
  5557. }
  5558. } else {
  5559. rc = lpfc_sli4_post_els_sgl_list_ext(phba);
  5560. if (unlikely(rc)) {
  5561. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5562. "2560 Error %d during els sgl post "
  5563. "operation\n", rc);
  5564. rc = -ENODEV;
  5565. goto out_free_mbox;
  5566. }
  5567. }
  5568. /* Register SCSI SGL pool to the device */
  5569. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5570. if (unlikely(rc)) {
  5571. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5572. "0383 Error %d during scsi sgl post "
  5573. "operation\n", rc);
  5574. /* Some Scsi buffers were moved to the abort scsi list */
  5575. /* A pci function reset will repost them */
  5576. rc = -ENODEV;
  5577. goto out_free_mbox;
  5578. }
  5579. /* Post the rpi header region to the device. */
  5580. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5581. if (unlikely(rc)) {
  5582. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5583. "0393 Error %d during rpi post operation\n",
  5584. rc);
  5585. rc = -ENODEV;
  5586. goto out_free_mbox;
  5587. }
  5588. lpfc_sli4_node_prep(phba);
  5589. /* Create all the SLI4 queues */
  5590. rc = lpfc_sli4_queue_create(phba);
  5591. if (rc) {
  5592. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5593. "3089 Failed to allocate queues\n");
  5594. rc = -ENODEV;
  5595. goto out_stop_timers;
  5596. }
  5597. /* Set up all the queues to the device */
  5598. rc = lpfc_sli4_queue_setup(phba);
  5599. if (unlikely(rc)) {
  5600. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5601. "0381 Error %d during queue setup.\n ", rc);
  5602. goto out_destroy_queue;
  5603. }
  5604. /* Arm the CQs and then EQs on device */
  5605. lpfc_sli4_arm_cqeq_intr(phba);
  5606. /* Indicate device interrupt mode */
  5607. phba->sli4_hba.intr_enable = 1;
  5608. /* Allow asynchronous mailbox command to go through */
  5609. spin_lock_irq(&phba->hbalock);
  5610. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5611. spin_unlock_irq(&phba->hbalock);
  5612. /* Post receive buffers to the device */
  5613. lpfc_sli4_rb_setup(phba);
  5614. /* Reset HBA FCF states after HBA reset */
  5615. phba->fcf.fcf_flag = 0;
  5616. phba->fcf.current_rec.flag = 0;
  5617. /* Start the ELS watchdog timer */
  5618. mod_timer(&vport->els_tmofunc,
  5619. jiffies + HZ * (phba->fc_ratov * 2));
  5620. /* Start heart beat timer */
  5621. mod_timer(&phba->hb_tmofunc,
  5622. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5623. phba->hb_outstanding = 0;
  5624. phba->last_completion_time = jiffies;
  5625. /* Start error attention (ERATT) polling timer */
  5626. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5627. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5628. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5629. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5630. if (!rc) {
  5631. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5632. "2829 This device supports "
  5633. "Advanced Error Reporting (AER)\n");
  5634. spin_lock_irq(&phba->hbalock);
  5635. phba->hba_flag |= HBA_AER_ENABLED;
  5636. spin_unlock_irq(&phba->hbalock);
  5637. } else {
  5638. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5639. "2830 This device does not support "
  5640. "Advanced Error Reporting (AER)\n");
  5641. phba->cfg_aer_support = 0;
  5642. }
  5643. rc = 0;
  5644. }
  5645. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5646. /*
  5647. * The FC Port needs to register FCFI (index 0)
  5648. */
  5649. lpfc_reg_fcfi(phba, mboxq);
  5650. mboxq->vport = phba->pport;
  5651. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5652. if (rc != MBX_SUCCESS)
  5653. goto out_unset_queue;
  5654. rc = 0;
  5655. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5656. &mboxq->u.mqe.un.reg_fcfi);
  5657. /* Check if the port is configured to be disabled */
  5658. lpfc_sli_read_link_ste(phba);
  5659. }
  5660. /*
  5661. * The port is ready, set the host's link state to LINK_DOWN
  5662. * in preparation for link interrupts.
  5663. */
  5664. spin_lock_irq(&phba->hbalock);
  5665. phba->link_state = LPFC_LINK_DOWN;
  5666. spin_unlock_irq(&phba->hbalock);
  5667. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5668. (phba->hba_flag & LINK_DISABLED)) {
  5669. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5670. "3103 Adapter Link is disabled.\n");
  5671. lpfc_down_link(phba, mboxq);
  5672. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5673. if (rc != MBX_SUCCESS) {
  5674. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5675. "3104 Adapter failed to issue "
  5676. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5677. goto out_unset_queue;
  5678. }
  5679. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5680. /* don't perform init_link on SLI4 FC port loopback test */
  5681. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5682. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5683. if (rc)
  5684. goto out_unset_queue;
  5685. }
  5686. }
  5687. mempool_free(mboxq, phba->mbox_mem_pool);
  5688. return rc;
  5689. out_unset_queue:
  5690. /* Unset all the queues set up in this routine when error out */
  5691. lpfc_sli4_queue_unset(phba);
  5692. out_destroy_queue:
  5693. lpfc_sli4_queue_destroy(phba);
  5694. out_stop_timers:
  5695. lpfc_stop_hba_timers(phba);
  5696. out_free_mbox:
  5697. mempool_free(mboxq, phba->mbox_mem_pool);
  5698. return rc;
  5699. }
  5700. /**
  5701. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5702. * @ptr: context object - pointer to hba structure.
  5703. *
  5704. * This is the callback function for mailbox timer. The mailbox
  5705. * timer is armed when a new mailbox command is issued and the timer
  5706. * is deleted when the mailbox complete. The function is called by
  5707. * the kernel timer code when a mailbox does not complete within
  5708. * expected time. This function wakes up the worker thread to
  5709. * process the mailbox timeout and returns. All the processing is
  5710. * done by the worker thread function lpfc_mbox_timeout_handler.
  5711. **/
  5712. void
  5713. lpfc_mbox_timeout(unsigned long ptr)
  5714. {
  5715. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5716. unsigned long iflag;
  5717. uint32_t tmo_posted;
  5718. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5719. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5720. if (!tmo_posted)
  5721. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5722. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5723. if (!tmo_posted)
  5724. lpfc_worker_wake_up(phba);
  5725. return;
  5726. }
  5727. /**
  5728. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5729. * @phba: Pointer to HBA context object.
  5730. *
  5731. * This function is called from worker thread when a mailbox command times out.
  5732. * The caller is not required to hold any locks. This function will reset the
  5733. * HBA and recover all the pending commands.
  5734. **/
  5735. void
  5736. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5737. {
  5738. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5739. MAILBOX_t *mb = &pmbox->u.mb;
  5740. struct lpfc_sli *psli = &phba->sli;
  5741. struct lpfc_sli_ring *pring;
  5742. /* Check the pmbox pointer first. There is a race condition
  5743. * between the mbox timeout handler getting executed in the
  5744. * worklist and the mailbox actually completing. When this
  5745. * race condition occurs, the mbox_active will be NULL.
  5746. */
  5747. spin_lock_irq(&phba->hbalock);
  5748. if (pmbox == NULL) {
  5749. lpfc_printf_log(phba, KERN_WARNING,
  5750. LOG_MBOX | LOG_SLI,
  5751. "0353 Active Mailbox cleared - mailbox timeout "
  5752. "exiting\n");
  5753. spin_unlock_irq(&phba->hbalock);
  5754. return;
  5755. }
  5756. /* Mbox cmd <mbxCommand> timeout */
  5757. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5758. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5759. mb->mbxCommand,
  5760. phba->pport->port_state,
  5761. phba->sli.sli_flag,
  5762. phba->sli.mbox_active);
  5763. spin_unlock_irq(&phba->hbalock);
  5764. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5765. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5766. * it to fail all outstanding SCSI IO.
  5767. */
  5768. spin_lock_irq(&phba->pport->work_port_lock);
  5769. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5770. spin_unlock_irq(&phba->pport->work_port_lock);
  5771. spin_lock_irq(&phba->hbalock);
  5772. phba->link_state = LPFC_LINK_UNKNOWN;
  5773. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5774. spin_unlock_irq(&phba->hbalock);
  5775. pring = &psli->ring[psli->fcp_ring];
  5776. lpfc_sli_abort_iocb_ring(phba, pring);
  5777. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5778. "0345 Resetting board due to mailbox timeout\n");
  5779. /* Reset the HBA device */
  5780. lpfc_reset_hba(phba);
  5781. }
  5782. /**
  5783. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  5784. * @phba: Pointer to HBA context object.
  5785. * @pmbox: Pointer to mailbox object.
  5786. * @flag: Flag indicating how the mailbox need to be processed.
  5787. *
  5788. * This function is called by discovery code and HBA management code
  5789. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  5790. * function gets the hbalock to protect the data structures.
  5791. * The mailbox command can be submitted in polling mode, in which case
  5792. * this function will wait in a polling loop for the completion of the
  5793. * mailbox.
  5794. * If the mailbox is submitted in no_wait mode (not polling) the
  5795. * function will submit the command and returns immediately without waiting
  5796. * for the mailbox completion. The no_wait is supported only when HBA
  5797. * is in SLI2/SLI3 mode - interrupts are enabled.
  5798. * The SLI interface allows only one mailbox pending at a time. If the
  5799. * mailbox is issued in polling mode and there is already a mailbox
  5800. * pending, then the function will return an error. If the mailbox is issued
  5801. * in NO_WAIT mode and there is a mailbox pending already, the function
  5802. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  5803. * The sli layer owns the mailbox object until the completion of mailbox
  5804. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  5805. * return codes the caller owns the mailbox command after the return of
  5806. * the function.
  5807. **/
  5808. static int
  5809. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  5810. uint32_t flag)
  5811. {
  5812. MAILBOX_t *mb;
  5813. struct lpfc_sli *psli = &phba->sli;
  5814. uint32_t status, evtctr;
  5815. uint32_t ha_copy, hc_copy;
  5816. int i;
  5817. unsigned long timeout;
  5818. unsigned long drvr_flag = 0;
  5819. uint32_t word0, ldata;
  5820. void __iomem *to_slim;
  5821. int processing_queue = 0;
  5822. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5823. if (!pmbox) {
  5824. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5825. /* processing mbox queue from intr_handler */
  5826. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5827. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5828. return MBX_SUCCESS;
  5829. }
  5830. processing_queue = 1;
  5831. pmbox = lpfc_mbox_get(phba);
  5832. if (!pmbox) {
  5833. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5834. return MBX_SUCCESS;
  5835. }
  5836. }
  5837. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  5838. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  5839. if(!pmbox->vport) {
  5840. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5841. lpfc_printf_log(phba, KERN_ERR,
  5842. LOG_MBOX | LOG_VPORT,
  5843. "1806 Mbox x%x failed. No vport\n",
  5844. pmbox->u.mb.mbxCommand);
  5845. dump_stack();
  5846. goto out_not_finished;
  5847. }
  5848. }
  5849. /* If the PCI channel is in offline state, do not post mbox. */
  5850. if (unlikely(pci_channel_offline(phba->pcidev))) {
  5851. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5852. goto out_not_finished;
  5853. }
  5854. /* If HBA has a deferred error attention, fail the iocb. */
  5855. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  5856. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5857. goto out_not_finished;
  5858. }
  5859. psli = &phba->sli;
  5860. mb = &pmbox->u.mb;
  5861. status = MBX_SUCCESS;
  5862. if (phba->link_state == LPFC_HBA_ERROR) {
  5863. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5864. /* Mbox command <mbxCommand> cannot issue */
  5865. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5866. "(%d):0311 Mailbox command x%x cannot "
  5867. "issue Data: x%x x%x\n",
  5868. pmbox->vport ? pmbox->vport->vpi : 0,
  5869. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  5870. goto out_not_finished;
  5871. }
  5872. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  5873. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  5874. !(hc_copy & HC_MBINT_ENA)) {
  5875. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5876. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5877. "(%d):2528 Mailbox command x%x cannot "
  5878. "issue Data: x%x x%x\n",
  5879. pmbox->vport ? pmbox->vport->vpi : 0,
  5880. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  5881. goto out_not_finished;
  5882. }
  5883. }
  5884. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  5885. /* Polling for a mbox command when another one is already active
  5886. * is not allowed in SLI. Also, the driver must have established
  5887. * SLI2 mode to queue and process multiple mbox commands.
  5888. */
  5889. if (flag & MBX_POLL) {
  5890. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5891. /* Mbox command <mbxCommand> cannot issue */
  5892. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5893. "(%d):2529 Mailbox command x%x "
  5894. "cannot issue Data: x%x x%x\n",
  5895. pmbox->vport ? pmbox->vport->vpi : 0,
  5896. pmbox->u.mb.mbxCommand,
  5897. psli->sli_flag, flag);
  5898. goto out_not_finished;
  5899. }
  5900. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  5901. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5902. /* Mbox command <mbxCommand> cannot issue */
  5903. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5904. "(%d):2530 Mailbox command x%x "
  5905. "cannot issue Data: x%x x%x\n",
  5906. pmbox->vport ? pmbox->vport->vpi : 0,
  5907. pmbox->u.mb.mbxCommand,
  5908. psli->sli_flag, flag);
  5909. goto out_not_finished;
  5910. }
  5911. /* Another mailbox command is still being processed, queue this
  5912. * command to be processed later.
  5913. */
  5914. lpfc_mbox_put(phba, pmbox);
  5915. /* Mbox cmd issue - BUSY */
  5916. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5917. "(%d):0308 Mbox cmd issue - BUSY Data: "
  5918. "x%x x%x x%x x%x\n",
  5919. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  5920. mb->mbxCommand, phba->pport->port_state,
  5921. psli->sli_flag, flag);
  5922. psli->slistat.mbox_busy++;
  5923. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5924. if (pmbox->vport) {
  5925. lpfc_debugfs_disc_trc(pmbox->vport,
  5926. LPFC_DISC_TRC_MBOX_VPORT,
  5927. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  5928. (uint32_t)mb->mbxCommand,
  5929. mb->un.varWords[0], mb->un.varWords[1]);
  5930. }
  5931. else {
  5932. lpfc_debugfs_disc_trc(phba->pport,
  5933. LPFC_DISC_TRC_MBOX,
  5934. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  5935. (uint32_t)mb->mbxCommand,
  5936. mb->un.varWords[0], mb->un.varWords[1]);
  5937. }
  5938. return MBX_BUSY;
  5939. }
  5940. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  5941. /* If we are not polling, we MUST be in SLI2 mode */
  5942. if (flag != MBX_POLL) {
  5943. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  5944. (mb->mbxCommand != MBX_KILL_BOARD)) {
  5945. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5946. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5947. /* Mbox command <mbxCommand> cannot issue */
  5948. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5949. "(%d):2531 Mailbox command x%x "
  5950. "cannot issue Data: x%x x%x\n",
  5951. pmbox->vport ? pmbox->vport->vpi : 0,
  5952. pmbox->u.mb.mbxCommand,
  5953. psli->sli_flag, flag);
  5954. goto out_not_finished;
  5955. }
  5956. /* timeout active mbox command */
  5957. mod_timer(&psli->mbox_tmo, (jiffies +
  5958. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  5959. }
  5960. /* Mailbox cmd <cmd> issue */
  5961. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5962. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  5963. "x%x\n",
  5964. pmbox->vport ? pmbox->vport->vpi : 0,
  5965. mb->mbxCommand, phba->pport->port_state,
  5966. psli->sli_flag, flag);
  5967. if (mb->mbxCommand != MBX_HEARTBEAT) {
  5968. if (pmbox->vport) {
  5969. lpfc_debugfs_disc_trc(pmbox->vport,
  5970. LPFC_DISC_TRC_MBOX_VPORT,
  5971. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  5972. (uint32_t)mb->mbxCommand,
  5973. mb->un.varWords[0], mb->un.varWords[1]);
  5974. }
  5975. else {
  5976. lpfc_debugfs_disc_trc(phba->pport,
  5977. LPFC_DISC_TRC_MBOX,
  5978. "MBOX Send: cmd:x%x mb:x%x x%x",
  5979. (uint32_t)mb->mbxCommand,
  5980. mb->un.varWords[0], mb->un.varWords[1]);
  5981. }
  5982. }
  5983. psli->slistat.mbox_cmd++;
  5984. evtctr = psli->slistat.mbox_event;
  5985. /* next set own bit for the adapter and copy over command word */
  5986. mb->mbxOwner = OWN_CHIP;
  5987. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  5988. /* Populate mbox extension offset word. */
  5989. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  5990. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  5991. = (uint8_t *)phba->mbox_ext
  5992. - (uint8_t *)phba->mbox;
  5993. }
  5994. /* Copy the mailbox extension data */
  5995. if (pmbox->in_ext_byte_len && pmbox->context2) {
  5996. lpfc_sli_pcimem_bcopy(pmbox->context2,
  5997. (uint8_t *)phba->mbox_ext,
  5998. pmbox->in_ext_byte_len);
  5999. }
  6000. /* Copy command data to host SLIM area */
  6001. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6002. } else {
  6003. /* Populate mbox extension offset word. */
  6004. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6005. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6006. = MAILBOX_HBA_EXT_OFFSET;
  6007. /* Copy the mailbox extension data */
  6008. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6009. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6010. MAILBOX_HBA_EXT_OFFSET,
  6011. pmbox->context2, pmbox->in_ext_byte_len);
  6012. }
  6013. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6014. /* copy command data into host mbox for cmpl */
  6015. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6016. }
  6017. /* First copy mbox command data to HBA SLIM, skip past first
  6018. word */
  6019. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6020. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  6021. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6022. /* Next copy over first word, with mbxOwner set */
  6023. ldata = *((uint32_t *)mb);
  6024. to_slim = phba->MBslimaddr;
  6025. writel(ldata, to_slim);
  6026. readl(to_slim); /* flush */
  6027. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6028. /* switch over to host mailbox */
  6029. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6030. }
  6031. }
  6032. wmb();
  6033. switch (flag) {
  6034. case MBX_NOWAIT:
  6035. /* Set up reference to mailbox command */
  6036. psli->mbox_active = pmbox;
  6037. /* Interrupt board to do it */
  6038. writel(CA_MBATT, phba->CAregaddr);
  6039. readl(phba->CAregaddr); /* flush */
  6040. /* Don't wait for it to finish, just return */
  6041. break;
  6042. case MBX_POLL:
  6043. /* Set up null reference to mailbox command */
  6044. psli->mbox_active = NULL;
  6045. /* Interrupt board to do it */
  6046. writel(CA_MBATT, phba->CAregaddr);
  6047. readl(phba->CAregaddr); /* flush */
  6048. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6049. /* First read mbox status word */
  6050. word0 = *((uint32_t *)phba->mbox);
  6051. word0 = le32_to_cpu(word0);
  6052. } else {
  6053. /* First read mbox status word */
  6054. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6055. spin_unlock_irqrestore(&phba->hbalock,
  6056. drvr_flag);
  6057. goto out_not_finished;
  6058. }
  6059. }
  6060. /* Read the HBA Host Attention Register */
  6061. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6062. spin_unlock_irqrestore(&phba->hbalock,
  6063. drvr_flag);
  6064. goto out_not_finished;
  6065. }
  6066. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6067. 1000) + jiffies;
  6068. i = 0;
  6069. /* Wait for command to complete */
  6070. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6071. (!(ha_copy & HA_MBATT) &&
  6072. (phba->link_state > LPFC_WARM_START))) {
  6073. if (time_after(jiffies, timeout)) {
  6074. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6075. spin_unlock_irqrestore(&phba->hbalock,
  6076. drvr_flag);
  6077. goto out_not_finished;
  6078. }
  6079. /* Check if we took a mbox interrupt while we were
  6080. polling */
  6081. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6082. && (evtctr != psli->slistat.mbox_event))
  6083. break;
  6084. if (i++ > 10) {
  6085. spin_unlock_irqrestore(&phba->hbalock,
  6086. drvr_flag);
  6087. msleep(1);
  6088. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6089. }
  6090. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6091. /* First copy command data */
  6092. word0 = *((uint32_t *)phba->mbox);
  6093. word0 = le32_to_cpu(word0);
  6094. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6095. MAILBOX_t *slimmb;
  6096. uint32_t slimword0;
  6097. /* Check real SLIM for any errors */
  6098. slimword0 = readl(phba->MBslimaddr);
  6099. slimmb = (MAILBOX_t *) & slimword0;
  6100. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6101. && slimmb->mbxStatus) {
  6102. psli->sli_flag &=
  6103. ~LPFC_SLI_ACTIVE;
  6104. word0 = slimword0;
  6105. }
  6106. }
  6107. } else {
  6108. /* First copy command data */
  6109. word0 = readl(phba->MBslimaddr);
  6110. }
  6111. /* Read the HBA Host Attention Register */
  6112. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6113. spin_unlock_irqrestore(&phba->hbalock,
  6114. drvr_flag);
  6115. goto out_not_finished;
  6116. }
  6117. }
  6118. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6119. /* copy results back to user */
  6120. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  6121. /* Copy the mailbox extension data */
  6122. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6123. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6124. pmbox->context2,
  6125. pmbox->out_ext_byte_len);
  6126. }
  6127. } else {
  6128. /* First copy command data */
  6129. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  6130. MAILBOX_CMD_SIZE);
  6131. /* Copy the mailbox extension data */
  6132. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6133. lpfc_memcpy_from_slim(pmbox->context2,
  6134. phba->MBslimaddr +
  6135. MAILBOX_HBA_EXT_OFFSET,
  6136. pmbox->out_ext_byte_len);
  6137. }
  6138. }
  6139. writel(HA_MBATT, phba->HAregaddr);
  6140. readl(phba->HAregaddr); /* flush */
  6141. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6142. status = mb->mbxStatus;
  6143. }
  6144. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6145. return status;
  6146. out_not_finished:
  6147. if (processing_queue) {
  6148. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6149. lpfc_mbox_cmpl_put(phba, pmbox);
  6150. }
  6151. return MBX_NOT_FINISHED;
  6152. }
  6153. /**
  6154. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6155. * @phba: Pointer to HBA context object.
  6156. *
  6157. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6158. * the driver internal pending mailbox queue. It will then try to wait out the
  6159. * possible outstanding mailbox command before return.
  6160. *
  6161. * Returns:
  6162. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6163. * the outstanding mailbox command timed out.
  6164. **/
  6165. static int
  6166. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6167. {
  6168. struct lpfc_sli *psli = &phba->sli;
  6169. int rc = 0;
  6170. unsigned long timeout = 0;
  6171. /* Mark the asynchronous mailbox command posting as blocked */
  6172. spin_lock_irq(&phba->hbalock);
  6173. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6174. /* Determine how long we might wait for the active mailbox
  6175. * command to be gracefully completed by firmware.
  6176. */
  6177. if (phba->sli.mbox_active)
  6178. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6179. phba->sli.mbox_active) *
  6180. 1000) + jiffies;
  6181. spin_unlock_irq(&phba->hbalock);
  6182. /* Wait for the outstnading mailbox command to complete */
  6183. while (phba->sli.mbox_active) {
  6184. /* Check active mailbox complete status every 2ms */
  6185. msleep(2);
  6186. if (time_after(jiffies, timeout)) {
  6187. /* Timeout, marked the outstanding cmd not complete */
  6188. rc = 1;
  6189. break;
  6190. }
  6191. }
  6192. /* Can not cleanly block async mailbox command, fails it */
  6193. if (rc) {
  6194. spin_lock_irq(&phba->hbalock);
  6195. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6196. spin_unlock_irq(&phba->hbalock);
  6197. }
  6198. return rc;
  6199. }
  6200. /**
  6201. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6202. * @phba: Pointer to HBA context object.
  6203. *
  6204. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6205. * commands from the driver internal pending mailbox queue. It makes sure
  6206. * that there is no outstanding mailbox command before resuming posting
  6207. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6208. * mailbox command, it will try to wait it out before resuming asynchronous
  6209. * mailbox command posting.
  6210. **/
  6211. static void
  6212. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6213. {
  6214. struct lpfc_sli *psli = &phba->sli;
  6215. spin_lock_irq(&phba->hbalock);
  6216. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6217. /* Asynchronous mailbox posting is not blocked, do nothing */
  6218. spin_unlock_irq(&phba->hbalock);
  6219. return;
  6220. }
  6221. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6222. * successful or timeout, after timing-out the outstanding mailbox
  6223. * command shall always be removed, so just unblock posting async
  6224. * mailbox command and resume
  6225. */
  6226. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6227. spin_unlock_irq(&phba->hbalock);
  6228. /* wake up worker thread to post asynchronlous mailbox command */
  6229. lpfc_worker_wake_up(phba);
  6230. }
  6231. /**
  6232. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6233. * @phba: Pointer to HBA context object.
  6234. * @mboxq: Pointer to mailbox object.
  6235. *
  6236. * The function posts a mailbox to the port. The mailbox is expected
  6237. * to be comletely filled in and ready for the port to operate on it.
  6238. * This routine executes a synchronous completion operation on the
  6239. * mailbox by polling for its completion.
  6240. *
  6241. * The caller must not be holding any locks when calling this routine.
  6242. *
  6243. * Returns:
  6244. * MBX_SUCCESS - mailbox posted successfully
  6245. * Any of the MBX error values.
  6246. **/
  6247. static int
  6248. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6249. {
  6250. int rc = MBX_SUCCESS;
  6251. unsigned long iflag;
  6252. uint32_t db_ready;
  6253. uint32_t mcqe_status;
  6254. uint32_t mbx_cmnd;
  6255. unsigned long timeout;
  6256. struct lpfc_sli *psli = &phba->sli;
  6257. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6258. struct lpfc_bmbx_create *mbox_rgn;
  6259. struct dma_address *dma_address;
  6260. struct lpfc_register bmbx_reg;
  6261. /*
  6262. * Only one mailbox can be active to the bootstrap mailbox region
  6263. * at a time and there is no queueing provided.
  6264. */
  6265. spin_lock_irqsave(&phba->hbalock, iflag);
  6266. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6267. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6268. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6269. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6270. "cannot issue Data: x%x x%x\n",
  6271. mboxq->vport ? mboxq->vport->vpi : 0,
  6272. mboxq->u.mb.mbxCommand,
  6273. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6274. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6275. psli->sli_flag, MBX_POLL);
  6276. return MBXERR_ERROR;
  6277. }
  6278. /* The server grabs the token and owns it until release */
  6279. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6280. phba->sli.mbox_active = mboxq;
  6281. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6282. /*
  6283. * Initialize the bootstrap memory region to avoid stale data areas
  6284. * in the mailbox post. Then copy the caller's mailbox contents to
  6285. * the bmbx mailbox region.
  6286. */
  6287. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6288. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6289. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6290. sizeof(struct lpfc_mqe));
  6291. /* Post the high mailbox dma address to the port and wait for ready. */
  6292. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6293. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6294. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6295. * 1000) + jiffies;
  6296. do {
  6297. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6298. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6299. if (!db_ready)
  6300. msleep(2);
  6301. if (time_after(jiffies, timeout)) {
  6302. rc = MBXERR_ERROR;
  6303. goto exit;
  6304. }
  6305. } while (!db_ready);
  6306. /* Post the low mailbox dma address to the port. */
  6307. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6308. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6309. * 1000) + jiffies;
  6310. do {
  6311. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6312. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6313. if (!db_ready)
  6314. msleep(2);
  6315. if (time_after(jiffies, timeout)) {
  6316. rc = MBXERR_ERROR;
  6317. goto exit;
  6318. }
  6319. } while (!db_ready);
  6320. /*
  6321. * Read the CQ to ensure the mailbox has completed.
  6322. * If so, update the mailbox status so that the upper layers
  6323. * can complete the request normally.
  6324. */
  6325. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6326. sizeof(struct lpfc_mqe));
  6327. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6328. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6329. sizeof(struct lpfc_mcqe));
  6330. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6331. /*
  6332. * When the CQE status indicates a failure and the mailbox status
  6333. * indicates success then copy the CQE status into the mailbox status
  6334. * (and prefix it with x4000).
  6335. */
  6336. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6337. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6338. bf_set(lpfc_mqe_status, mb,
  6339. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6340. rc = MBXERR_ERROR;
  6341. } else
  6342. lpfc_sli4_swap_str(phba, mboxq);
  6343. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6344. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6345. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6346. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6347. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6348. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6349. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6350. bf_get(lpfc_mqe_status, mb),
  6351. mb->un.mb_words[0], mb->un.mb_words[1],
  6352. mb->un.mb_words[2], mb->un.mb_words[3],
  6353. mb->un.mb_words[4], mb->un.mb_words[5],
  6354. mb->un.mb_words[6], mb->un.mb_words[7],
  6355. mb->un.mb_words[8], mb->un.mb_words[9],
  6356. mb->un.mb_words[10], mb->un.mb_words[11],
  6357. mb->un.mb_words[12], mboxq->mcqe.word0,
  6358. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6359. mboxq->mcqe.trailer);
  6360. exit:
  6361. /* We are holding the token, no needed for lock when release */
  6362. spin_lock_irqsave(&phba->hbalock, iflag);
  6363. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6364. phba->sli.mbox_active = NULL;
  6365. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6366. return rc;
  6367. }
  6368. /**
  6369. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6370. * @phba: Pointer to HBA context object.
  6371. * @pmbox: Pointer to mailbox object.
  6372. * @flag: Flag indicating how the mailbox need to be processed.
  6373. *
  6374. * This function is called by discovery code and HBA management code to submit
  6375. * a mailbox command to firmware with SLI-4 interface spec.
  6376. *
  6377. * Return codes the caller owns the mailbox command after the return of the
  6378. * function.
  6379. **/
  6380. static int
  6381. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6382. uint32_t flag)
  6383. {
  6384. struct lpfc_sli *psli = &phba->sli;
  6385. unsigned long iflags;
  6386. int rc;
  6387. /* dump from issue mailbox command if setup */
  6388. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6389. rc = lpfc_mbox_dev_check(phba);
  6390. if (unlikely(rc)) {
  6391. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6392. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6393. "cannot issue Data: x%x x%x\n",
  6394. mboxq->vport ? mboxq->vport->vpi : 0,
  6395. mboxq->u.mb.mbxCommand,
  6396. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6397. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6398. psli->sli_flag, flag);
  6399. goto out_not_finished;
  6400. }
  6401. /* Detect polling mode and jump to a handler */
  6402. if (!phba->sli4_hba.intr_enable) {
  6403. if (flag == MBX_POLL)
  6404. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6405. else
  6406. rc = -EIO;
  6407. if (rc != MBX_SUCCESS)
  6408. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6409. "(%d):2541 Mailbox command x%x "
  6410. "(x%x/x%x) cannot issue Data: "
  6411. "x%x x%x\n",
  6412. mboxq->vport ? mboxq->vport->vpi : 0,
  6413. mboxq->u.mb.mbxCommand,
  6414. lpfc_sli_config_mbox_subsys_get(phba,
  6415. mboxq),
  6416. lpfc_sli_config_mbox_opcode_get(phba,
  6417. mboxq),
  6418. psli->sli_flag, flag);
  6419. return rc;
  6420. } else if (flag == MBX_POLL) {
  6421. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6422. "(%d):2542 Try to issue mailbox command "
  6423. "x%x (x%x/x%x) synchronously ahead of async"
  6424. "mailbox command queue: x%x x%x\n",
  6425. mboxq->vport ? mboxq->vport->vpi : 0,
  6426. mboxq->u.mb.mbxCommand,
  6427. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6428. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6429. psli->sli_flag, flag);
  6430. /* Try to block the asynchronous mailbox posting */
  6431. rc = lpfc_sli4_async_mbox_block(phba);
  6432. if (!rc) {
  6433. /* Successfully blocked, now issue sync mbox cmd */
  6434. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6435. if (rc != MBX_SUCCESS)
  6436. lpfc_printf_log(phba, KERN_ERR,
  6437. LOG_MBOX | LOG_SLI,
  6438. "(%d):2597 Mailbox command "
  6439. "x%x (x%x/x%x) cannot issue "
  6440. "Data: x%x x%x\n",
  6441. mboxq->vport ?
  6442. mboxq->vport->vpi : 0,
  6443. mboxq->u.mb.mbxCommand,
  6444. lpfc_sli_config_mbox_subsys_get(phba,
  6445. mboxq),
  6446. lpfc_sli_config_mbox_opcode_get(phba,
  6447. mboxq),
  6448. psli->sli_flag, flag);
  6449. /* Unblock the async mailbox posting afterward */
  6450. lpfc_sli4_async_mbox_unblock(phba);
  6451. }
  6452. return rc;
  6453. }
  6454. /* Now, interrupt mode asynchrous mailbox command */
  6455. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6456. if (rc) {
  6457. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6458. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6459. "cannot issue Data: x%x x%x\n",
  6460. mboxq->vport ? mboxq->vport->vpi : 0,
  6461. mboxq->u.mb.mbxCommand,
  6462. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6463. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6464. psli->sli_flag, flag);
  6465. goto out_not_finished;
  6466. }
  6467. /* Put the mailbox command to the driver internal FIFO */
  6468. psli->slistat.mbox_busy++;
  6469. spin_lock_irqsave(&phba->hbalock, iflags);
  6470. lpfc_mbox_put(phba, mboxq);
  6471. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6472. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6473. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6474. "x%x (x%x/x%x) x%x x%x x%x\n",
  6475. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6476. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6477. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6478. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6479. phba->pport->port_state,
  6480. psli->sli_flag, MBX_NOWAIT);
  6481. /* Wake up worker thread to transport mailbox command from head */
  6482. lpfc_worker_wake_up(phba);
  6483. return MBX_BUSY;
  6484. out_not_finished:
  6485. return MBX_NOT_FINISHED;
  6486. }
  6487. /**
  6488. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6489. * @phba: Pointer to HBA context object.
  6490. *
  6491. * This function is called by worker thread to send a mailbox command to
  6492. * SLI4 HBA firmware.
  6493. *
  6494. **/
  6495. int
  6496. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6497. {
  6498. struct lpfc_sli *psli = &phba->sli;
  6499. LPFC_MBOXQ_t *mboxq;
  6500. int rc = MBX_SUCCESS;
  6501. unsigned long iflags;
  6502. struct lpfc_mqe *mqe;
  6503. uint32_t mbx_cmnd;
  6504. /* Check interrupt mode before post async mailbox command */
  6505. if (unlikely(!phba->sli4_hba.intr_enable))
  6506. return MBX_NOT_FINISHED;
  6507. /* Check for mailbox command service token */
  6508. spin_lock_irqsave(&phba->hbalock, iflags);
  6509. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6510. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6511. return MBX_NOT_FINISHED;
  6512. }
  6513. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6514. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6515. return MBX_NOT_FINISHED;
  6516. }
  6517. if (unlikely(phba->sli.mbox_active)) {
  6518. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6519. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6520. "0384 There is pending active mailbox cmd\n");
  6521. return MBX_NOT_FINISHED;
  6522. }
  6523. /* Take the mailbox command service token */
  6524. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6525. /* Get the next mailbox command from head of queue */
  6526. mboxq = lpfc_mbox_get(phba);
  6527. /* If no more mailbox command waiting for post, we're done */
  6528. if (!mboxq) {
  6529. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6530. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6531. return MBX_SUCCESS;
  6532. }
  6533. phba->sli.mbox_active = mboxq;
  6534. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6535. /* Check device readiness for posting mailbox command */
  6536. rc = lpfc_mbox_dev_check(phba);
  6537. if (unlikely(rc))
  6538. /* Driver clean routine will clean up pending mailbox */
  6539. goto out_not_finished;
  6540. /* Prepare the mbox command to be posted */
  6541. mqe = &mboxq->u.mqe;
  6542. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6543. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6544. mod_timer(&psli->mbox_tmo, (jiffies +
  6545. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6546. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6547. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6548. "x%x x%x\n",
  6549. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6550. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6551. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6552. phba->pport->port_state, psli->sli_flag);
  6553. if (mbx_cmnd != MBX_HEARTBEAT) {
  6554. if (mboxq->vport) {
  6555. lpfc_debugfs_disc_trc(mboxq->vport,
  6556. LPFC_DISC_TRC_MBOX_VPORT,
  6557. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6558. mbx_cmnd, mqe->un.mb_words[0],
  6559. mqe->un.mb_words[1]);
  6560. } else {
  6561. lpfc_debugfs_disc_trc(phba->pport,
  6562. LPFC_DISC_TRC_MBOX,
  6563. "MBOX Send: cmd:x%x mb:x%x x%x",
  6564. mbx_cmnd, mqe->un.mb_words[0],
  6565. mqe->un.mb_words[1]);
  6566. }
  6567. }
  6568. psli->slistat.mbox_cmd++;
  6569. /* Post the mailbox command to the port */
  6570. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6571. if (rc != MBX_SUCCESS) {
  6572. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6573. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6574. "cannot issue Data: x%x x%x\n",
  6575. mboxq->vport ? mboxq->vport->vpi : 0,
  6576. mboxq->u.mb.mbxCommand,
  6577. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6578. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6579. psli->sli_flag, MBX_NOWAIT);
  6580. goto out_not_finished;
  6581. }
  6582. return rc;
  6583. out_not_finished:
  6584. spin_lock_irqsave(&phba->hbalock, iflags);
  6585. if (phba->sli.mbox_active) {
  6586. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6587. __lpfc_mbox_cmpl_put(phba, mboxq);
  6588. /* Release the token */
  6589. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6590. phba->sli.mbox_active = NULL;
  6591. }
  6592. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6593. return MBX_NOT_FINISHED;
  6594. }
  6595. /**
  6596. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6597. * @phba: Pointer to HBA context object.
  6598. * @pmbox: Pointer to mailbox object.
  6599. * @flag: Flag indicating how the mailbox need to be processed.
  6600. *
  6601. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6602. * the API jump table function pointer from the lpfc_hba struct.
  6603. *
  6604. * Return codes the caller owns the mailbox command after the return of the
  6605. * function.
  6606. **/
  6607. int
  6608. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6609. {
  6610. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6611. }
  6612. /**
  6613. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6614. * @phba: The hba struct for which this call is being executed.
  6615. * @dev_grp: The HBA PCI-Device group number.
  6616. *
  6617. * This routine sets up the mbox interface API function jump table in @phba
  6618. * struct.
  6619. * Returns: 0 - success, -ENODEV - failure.
  6620. **/
  6621. int
  6622. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6623. {
  6624. switch (dev_grp) {
  6625. case LPFC_PCI_DEV_LP:
  6626. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6627. phba->lpfc_sli_handle_slow_ring_event =
  6628. lpfc_sli_handle_slow_ring_event_s3;
  6629. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6630. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6631. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6632. break;
  6633. case LPFC_PCI_DEV_OC:
  6634. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6635. phba->lpfc_sli_handle_slow_ring_event =
  6636. lpfc_sli_handle_slow_ring_event_s4;
  6637. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6638. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6639. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6640. break;
  6641. default:
  6642. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6643. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6644. dev_grp);
  6645. return -ENODEV;
  6646. break;
  6647. }
  6648. return 0;
  6649. }
  6650. /**
  6651. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6652. * @phba: Pointer to HBA context object.
  6653. * @pring: Pointer to driver SLI ring object.
  6654. * @piocb: Pointer to address of newly added command iocb.
  6655. *
  6656. * This function is called with hbalock held to add a command
  6657. * iocb to the txq when SLI layer cannot submit the command iocb
  6658. * to the ring.
  6659. **/
  6660. void
  6661. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6662. struct lpfc_iocbq *piocb)
  6663. {
  6664. /* Insert the caller's iocb in the txq tail for later processing. */
  6665. list_add_tail(&piocb->list, &pring->txq);
  6666. pring->txq_cnt++;
  6667. }
  6668. /**
  6669. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6670. * @phba: Pointer to HBA context object.
  6671. * @pring: Pointer to driver SLI ring object.
  6672. * @piocb: Pointer to address of newly added command iocb.
  6673. *
  6674. * This function is called with hbalock held before a new
  6675. * iocb is submitted to the firmware. This function checks
  6676. * txq to flush the iocbs in txq to Firmware before
  6677. * submitting new iocbs to the Firmware.
  6678. * If there are iocbs in the txq which need to be submitted
  6679. * to firmware, lpfc_sli_next_iocb returns the first element
  6680. * of the txq after dequeuing it from txq.
  6681. * If there is no iocb in the txq then the function will return
  6682. * *piocb and *piocb is set to NULL. Caller needs to check
  6683. * *piocb to find if there are more commands in the txq.
  6684. **/
  6685. static struct lpfc_iocbq *
  6686. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6687. struct lpfc_iocbq **piocb)
  6688. {
  6689. struct lpfc_iocbq * nextiocb;
  6690. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6691. if (!nextiocb) {
  6692. nextiocb = *piocb;
  6693. *piocb = NULL;
  6694. }
  6695. return nextiocb;
  6696. }
  6697. /**
  6698. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6699. * @phba: Pointer to HBA context object.
  6700. * @ring_number: SLI ring number to issue iocb on.
  6701. * @piocb: Pointer to command iocb.
  6702. * @flag: Flag indicating if this command can be put into txq.
  6703. *
  6704. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6705. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6706. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6707. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6708. * this function allows only iocbs for posting buffers. This function finds
  6709. * next available slot in the command ring and posts the command to the
  6710. * available slot and writes the port attention register to request HBA start
  6711. * processing new iocb. If there is no slot available in the ring and
  6712. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6713. * the function returns IOCB_BUSY.
  6714. *
  6715. * This function is called with hbalock held. The function will return success
  6716. * after it successfully submit the iocb to firmware or after adding to the
  6717. * txq.
  6718. **/
  6719. static int
  6720. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6721. struct lpfc_iocbq *piocb, uint32_t flag)
  6722. {
  6723. struct lpfc_iocbq *nextiocb;
  6724. IOCB_t *iocb;
  6725. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6726. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6727. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6728. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6729. lpfc_printf_log(phba, KERN_ERR,
  6730. LOG_SLI | LOG_VPORT,
  6731. "1807 IOCB x%x failed. No vport\n",
  6732. piocb->iocb.ulpCommand);
  6733. dump_stack();
  6734. return IOCB_ERROR;
  6735. }
  6736. /* If the PCI channel is in offline state, do not post iocbs. */
  6737. if (unlikely(pci_channel_offline(phba->pcidev)))
  6738. return IOCB_ERROR;
  6739. /* If HBA has a deferred error attention, fail the iocb. */
  6740. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6741. return IOCB_ERROR;
  6742. /*
  6743. * We should never get an IOCB if we are in a < LINK_DOWN state
  6744. */
  6745. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6746. return IOCB_ERROR;
  6747. /*
  6748. * Check to see if we are blocking IOCB processing because of a
  6749. * outstanding event.
  6750. */
  6751. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  6752. goto iocb_busy;
  6753. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  6754. /*
  6755. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  6756. * can be issued if the link is not up.
  6757. */
  6758. switch (piocb->iocb.ulpCommand) {
  6759. case CMD_GEN_REQUEST64_CR:
  6760. case CMD_GEN_REQUEST64_CX:
  6761. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  6762. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  6763. FC_RCTL_DD_UNSOL_CMD) ||
  6764. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  6765. MENLO_TRANSPORT_TYPE))
  6766. goto iocb_busy;
  6767. break;
  6768. case CMD_QUE_RING_BUF_CN:
  6769. case CMD_QUE_RING_BUF64_CN:
  6770. /*
  6771. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  6772. * completion, iocb_cmpl MUST be 0.
  6773. */
  6774. if (piocb->iocb_cmpl)
  6775. piocb->iocb_cmpl = NULL;
  6776. /*FALLTHROUGH*/
  6777. case CMD_CREATE_XRI_CR:
  6778. case CMD_CLOSE_XRI_CN:
  6779. case CMD_CLOSE_XRI_CX:
  6780. break;
  6781. default:
  6782. goto iocb_busy;
  6783. }
  6784. /*
  6785. * For FCP commands, we must be in a state where we can process link
  6786. * attention events.
  6787. */
  6788. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  6789. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  6790. goto iocb_busy;
  6791. }
  6792. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  6793. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  6794. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  6795. if (iocb)
  6796. lpfc_sli_update_ring(phba, pring);
  6797. else
  6798. lpfc_sli_update_full_ring(phba, pring);
  6799. if (!piocb)
  6800. return IOCB_SUCCESS;
  6801. goto out_busy;
  6802. iocb_busy:
  6803. pring->stats.iocb_cmd_delay++;
  6804. out_busy:
  6805. if (!(flag & SLI_IOCB_RET_IOCB)) {
  6806. __lpfc_sli_ringtx_put(phba, pring, piocb);
  6807. return IOCB_SUCCESS;
  6808. }
  6809. return IOCB_BUSY;
  6810. }
  6811. /**
  6812. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  6813. * @phba: Pointer to HBA context object.
  6814. * @piocb: Pointer to command iocb.
  6815. * @sglq: Pointer to the scatter gather queue object.
  6816. *
  6817. * This routine converts the bpl or bde that is in the IOCB
  6818. * to a sgl list for the sli4 hardware. The physical address
  6819. * of the bpl/bde is converted back to a virtual address.
  6820. * If the IOCB contains a BPL then the list of BDE's is
  6821. * converted to sli4_sge's. If the IOCB contains a single
  6822. * BDE then it is converted to a single sli_sge.
  6823. * The IOCB is still in cpu endianess so the contents of
  6824. * the bpl can be used without byte swapping.
  6825. *
  6826. * Returns valid XRI = Success, NO_XRI = Failure.
  6827. **/
  6828. static uint16_t
  6829. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  6830. struct lpfc_sglq *sglq)
  6831. {
  6832. uint16_t xritag = NO_XRI;
  6833. struct ulp_bde64 *bpl = NULL;
  6834. struct ulp_bde64 bde;
  6835. struct sli4_sge *sgl = NULL;
  6836. struct lpfc_dmabuf *dmabuf;
  6837. IOCB_t *icmd;
  6838. int numBdes = 0;
  6839. int i = 0;
  6840. uint32_t offset = 0; /* accumulated offset in the sg request list */
  6841. int inbound = 0; /* number of sg reply entries inbound from firmware */
  6842. if (!piocbq || !sglq)
  6843. return xritag;
  6844. sgl = (struct sli4_sge *)sglq->sgl;
  6845. icmd = &piocbq->iocb;
  6846. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  6847. return sglq->sli4_xritag;
  6848. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6849. numBdes = icmd->un.genreq64.bdl.bdeSize /
  6850. sizeof(struct ulp_bde64);
  6851. /* The addrHigh and addrLow fields within the IOCB
  6852. * have not been byteswapped yet so there is no
  6853. * need to swap them back.
  6854. */
  6855. if (piocbq->context3)
  6856. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  6857. else
  6858. return xritag;
  6859. bpl = (struct ulp_bde64 *)dmabuf->virt;
  6860. if (!bpl)
  6861. return xritag;
  6862. for (i = 0; i < numBdes; i++) {
  6863. /* Should already be byte swapped. */
  6864. sgl->addr_hi = bpl->addrHigh;
  6865. sgl->addr_lo = bpl->addrLow;
  6866. sgl->word2 = le32_to_cpu(sgl->word2);
  6867. if ((i+1) == numBdes)
  6868. bf_set(lpfc_sli4_sge_last, sgl, 1);
  6869. else
  6870. bf_set(lpfc_sli4_sge_last, sgl, 0);
  6871. /* swap the size field back to the cpu so we
  6872. * can assign it to the sgl.
  6873. */
  6874. bde.tus.w = le32_to_cpu(bpl->tus.w);
  6875. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  6876. /* The offsets in the sgl need to be accumulated
  6877. * separately for the request and reply lists.
  6878. * The request is always first, the reply follows.
  6879. */
  6880. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  6881. /* add up the reply sg entries */
  6882. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  6883. inbound++;
  6884. /* first inbound? reset the offset */
  6885. if (inbound == 1)
  6886. offset = 0;
  6887. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  6888. bf_set(lpfc_sli4_sge_type, sgl,
  6889. LPFC_SGE_TYPE_DATA);
  6890. offset += bde.tus.f.bdeSize;
  6891. }
  6892. sgl->word2 = cpu_to_le32(sgl->word2);
  6893. bpl++;
  6894. sgl++;
  6895. }
  6896. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  6897. /* The addrHigh and addrLow fields of the BDE have not
  6898. * been byteswapped yet so they need to be swapped
  6899. * before putting them in the sgl.
  6900. */
  6901. sgl->addr_hi =
  6902. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  6903. sgl->addr_lo =
  6904. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  6905. sgl->word2 = le32_to_cpu(sgl->word2);
  6906. bf_set(lpfc_sli4_sge_last, sgl, 1);
  6907. sgl->word2 = cpu_to_le32(sgl->word2);
  6908. sgl->sge_len =
  6909. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  6910. }
  6911. return sglq->sli4_xritag;
  6912. }
  6913. /**
  6914. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  6915. * @phba: Pointer to HBA context object.
  6916. *
  6917. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  6918. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  6919. * held.
  6920. *
  6921. * Return: index into SLI4 fast-path FCP queue index.
  6922. **/
  6923. static uint32_t
  6924. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  6925. {
  6926. ++phba->fcp_qidx;
  6927. if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
  6928. phba->fcp_qidx = 0;
  6929. return phba->fcp_qidx;
  6930. }
  6931. /**
  6932. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  6933. * @phba: Pointer to HBA context object.
  6934. * @piocb: Pointer to command iocb.
  6935. * @wqe: Pointer to the work queue entry.
  6936. *
  6937. * This routine converts the iocb command to its Work Queue Entry
  6938. * equivalent. The wqe pointer should not have any fields set when
  6939. * this routine is called because it will memcpy over them.
  6940. * This routine does not set the CQ_ID or the WQEC bits in the
  6941. * wqe.
  6942. *
  6943. * Returns: 0 = Success, IOCB_ERROR = Failure.
  6944. **/
  6945. static int
  6946. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  6947. union lpfc_wqe *wqe)
  6948. {
  6949. uint32_t xmit_len = 0, total_len = 0;
  6950. uint8_t ct = 0;
  6951. uint32_t fip;
  6952. uint32_t abort_tag;
  6953. uint8_t command_type = ELS_COMMAND_NON_FIP;
  6954. uint8_t cmnd;
  6955. uint16_t xritag;
  6956. uint16_t abrt_iotag;
  6957. struct lpfc_iocbq *abrtiocbq;
  6958. struct ulp_bde64 *bpl = NULL;
  6959. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  6960. int numBdes, i;
  6961. struct ulp_bde64 bde;
  6962. struct lpfc_nodelist *ndlp;
  6963. uint32_t *pcmd;
  6964. uint32_t if_type;
  6965. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  6966. /* The fcp commands will set command type */
  6967. if (iocbq->iocb_flag & LPFC_IO_FCP)
  6968. command_type = FCP_COMMAND;
  6969. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  6970. command_type = ELS_COMMAND_FIP;
  6971. else
  6972. command_type = ELS_COMMAND_NON_FIP;
  6973. /* Some of the fields are in the right position already */
  6974. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  6975. abort_tag = (uint32_t) iocbq->iotag;
  6976. xritag = iocbq->sli4_xritag;
  6977. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  6978. /* words0-2 bpl convert bde */
  6979. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6980. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  6981. sizeof(struct ulp_bde64);
  6982. bpl = (struct ulp_bde64 *)
  6983. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  6984. if (!bpl)
  6985. return IOCB_ERROR;
  6986. /* Should already be byte swapped. */
  6987. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  6988. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  6989. /* swap the size field back to the cpu so we
  6990. * can assign it to the sgl.
  6991. */
  6992. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  6993. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  6994. total_len = 0;
  6995. for (i = 0; i < numBdes; i++) {
  6996. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  6997. total_len += bde.tus.f.bdeSize;
  6998. }
  6999. } else
  7000. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7001. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7002. cmnd = iocbq->iocb.ulpCommand;
  7003. switch (iocbq->iocb.ulpCommand) {
  7004. case CMD_ELS_REQUEST64_CR:
  7005. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7006. if (!iocbq->iocb.ulpLe) {
  7007. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7008. "2007 Only Limited Edition cmd Format"
  7009. " supported 0x%x\n",
  7010. iocbq->iocb.ulpCommand);
  7011. return IOCB_ERROR;
  7012. }
  7013. wqe->els_req.payload_len = xmit_len;
  7014. /* Els_reguest64 has a TMO */
  7015. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7016. iocbq->iocb.ulpTimeout);
  7017. /* Need a VF for word 4 set the vf bit*/
  7018. bf_set(els_req64_vf, &wqe->els_req, 0);
  7019. /* And a VFID for word 12 */
  7020. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7021. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7022. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7023. iocbq->iocb.ulpContext);
  7024. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7025. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7026. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7027. if (command_type == ELS_COMMAND_FIP)
  7028. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7029. >> LPFC_FIP_ELS_ID_SHIFT);
  7030. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7031. iocbq->context2)->virt);
  7032. if_type = bf_get(lpfc_sli_intf_if_type,
  7033. &phba->sli4_hba.sli_intf);
  7034. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7035. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7036. *pcmd == ELS_CMD_SCR ||
  7037. *pcmd == ELS_CMD_FDISC ||
  7038. *pcmd == ELS_CMD_LOGO ||
  7039. *pcmd == ELS_CMD_PLOGI)) {
  7040. bf_set(els_req64_sp, &wqe->els_req, 1);
  7041. bf_set(els_req64_sid, &wqe->els_req,
  7042. iocbq->vport->fc_myDID);
  7043. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7044. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7045. phba->vpi_ids[phba->pport->vpi]);
  7046. } else if (pcmd && iocbq->context1) {
  7047. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7048. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7049. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7050. }
  7051. }
  7052. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7053. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7054. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7055. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7056. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7057. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7058. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7059. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7060. break;
  7061. case CMD_XMIT_SEQUENCE64_CX:
  7062. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7063. iocbq->iocb.un.ulpWord[3]);
  7064. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7065. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7066. /* The entire sequence is transmitted for this IOCB */
  7067. xmit_len = total_len;
  7068. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7069. if (phba->link_flag & LS_LOOPBACK_MODE)
  7070. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7071. case CMD_XMIT_SEQUENCE64_CR:
  7072. /* word3 iocb=io_tag32 wqe=reserved */
  7073. wqe->xmit_sequence.rsvd3 = 0;
  7074. /* word4 relative_offset memcpy */
  7075. /* word5 r_ctl/df_ctl memcpy */
  7076. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7077. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7078. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7079. LPFC_WQE_IOD_WRITE);
  7080. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7081. LPFC_WQE_LENLOC_WORD12);
  7082. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7083. wqe->xmit_sequence.xmit_len = xmit_len;
  7084. command_type = OTHER_COMMAND;
  7085. break;
  7086. case CMD_XMIT_BCAST64_CN:
  7087. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7088. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7089. /* word4 iocb=rsvd wqe=rsvd */
  7090. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7091. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7092. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7093. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7094. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7095. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7096. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7097. LPFC_WQE_LENLOC_WORD3);
  7098. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7099. break;
  7100. case CMD_FCP_IWRITE64_CR:
  7101. command_type = FCP_COMMAND_DATA_OUT;
  7102. /* word3 iocb=iotag wqe=payload_offset_len */
  7103. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7104. wqe->fcp_iwrite.payload_offset_len =
  7105. xmit_len + sizeof(struct fcp_rsp);
  7106. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7107. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7108. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7109. iocbq->iocb.ulpFCP2Rcvy);
  7110. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7111. /* Always open the exchange */
  7112. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7113. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7114. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7115. LPFC_WQE_LENLOC_WORD4);
  7116. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7117. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7118. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7119. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7120. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7121. }
  7122. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7123. break;
  7124. case CMD_FCP_IREAD64_CR:
  7125. /* word3 iocb=iotag wqe=payload_offset_len */
  7126. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7127. wqe->fcp_iread.payload_offset_len =
  7128. xmit_len + sizeof(struct fcp_rsp);
  7129. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7130. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7131. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7132. iocbq->iocb.ulpFCP2Rcvy);
  7133. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7134. /* Always open the exchange */
  7135. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7136. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7137. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7138. LPFC_WQE_LENLOC_WORD4);
  7139. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7140. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7141. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7142. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7143. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7144. }
  7145. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7146. break;
  7147. case CMD_FCP_ICMND64_CR:
  7148. /* word3 iocb=IO_TAG wqe=reserved */
  7149. wqe->fcp_icmd.rsrvd3 = 0;
  7150. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7151. /* Always open the exchange */
  7152. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7153. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7154. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7155. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7156. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7157. LPFC_WQE_LENLOC_NONE);
  7158. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7159. break;
  7160. case CMD_GEN_REQUEST64_CR:
  7161. /* For this command calculate the xmit length of the
  7162. * request bde.
  7163. */
  7164. xmit_len = 0;
  7165. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7166. sizeof(struct ulp_bde64);
  7167. for (i = 0; i < numBdes; i++) {
  7168. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7169. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7170. break;
  7171. xmit_len += bde.tus.f.bdeSize;
  7172. }
  7173. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7174. wqe->gen_req.request_payload_len = xmit_len;
  7175. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7176. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7177. /* word6 context tag copied in memcpy */
  7178. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7179. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7180. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7181. "2015 Invalid CT %x command 0x%x\n",
  7182. ct, iocbq->iocb.ulpCommand);
  7183. return IOCB_ERROR;
  7184. }
  7185. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7186. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7187. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7188. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7189. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7190. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7191. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7192. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7193. command_type = OTHER_COMMAND;
  7194. break;
  7195. case CMD_XMIT_ELS_RSP64_CX:
  7196. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7197. /* words0-2 BDE memcpy */
  7198. /* word3 iocb=iotag32 wqe=response_payload_len */
  7199. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7200. /* word4 iocb=did wge=rsvd. */
  7201. wqe->xmit_els_rsp.rsvd4 = 0;
  7202. /* word5 iocb=rsvd wge=did */
  7203. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7204. iocbq->iocb.un.elsreq64.remoteID);
  7205. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7206. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7207. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7208. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7209. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7210. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7211. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7212. phba->vpi_ids[iocbq->vport->vpi]);
  7213. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7214. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7215. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7216. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7217. LPFC_WQE_LENLOC_WORD3);
  7218. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7219. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7220. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7221. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7222. iocbq->context2)->virt);
  7223. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7224. bf_set(els_req64_sp, &wqe->els_req, 1);
  7225. bf_set(els_req64_sid, &wqe->els_req,
  7226. iocbq->vport->fc_myDID);
  7227. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7228. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7229. phba->vpi_ids[phba->pport->vpi]);
  7230. }
  7231. command_type = OTHER_COMMAND;
  7232. break;
  7233. case CMD_CLOSE_XRI_CN:
  7234. case CMD_ABORT_XRI_CN:
  7235. case CMD_ABORT_XRI_CX:
  7236. /* words 0-2 memcpy should be 0 rserved */
  7237. /* port will send abts */
  7238. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7239. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7240. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7241. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7242. } else
  7243. fip = 0;
  7244. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7245. /*
  7246. * The link is down, or the command was ELS_FIP
  7247. * so the fw does not need to send abts
  7248. * on the wire.
  7249. */
  7250. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7251. else
  7252. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7253. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7254. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7255. wqe->abort_cmd.rsrvd5 = 0;
  7256. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7257. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7258. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7259. /*
  7260. * The abort handler will send us CMD_ABORT_XRI_CN or
  7261. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7262. */
  7263. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7264. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7265. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7266. LPFC_WQE_LENLOC_NONE);
  7267. cmnd = CMD_ABORT_XRI_CX;
  7268. command_type = OTHER_COMMAND;
  7269. xritag = 0;
  7270. break;
  7271. case CMD_XMIT_BLS_RSP64_CX:
  7272. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7273. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7274. * we re-construct this WQE here based on information in
  7275. * iocbq from scratch.
  7276. */
  7277. memset(wqe, 0, sizeof(union lpfc_wqe));
  7278. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7279. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7280. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7281. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7282. LPFC_ABTS_UNSOL_INT) {
  7283. /* ABTS sent by initiator to CT exchange, the
  7284. * RX_ID field will be filled with the newly
  7285. * allocated responder XRI.
  7286. */
  7287. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7288. iocbq->sli4_xritag);
  7289. } else {
  7290. /* ABTS sent by responder to CT exchange, the
  7291. * RX_ID field will be filled with the responder
  7292. * RX_ID from ABTS.
  7293. */
  7294. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7295. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7296. }
  7297. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7298. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7299. /* Use CT=VPI */
  7300. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7301. ndlp->nlp_DID);
  7302. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7303. iocbq->iocb.ulpContext);
  7304. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7305. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7306. phba->vpi_ids[phba->pport->vpi]);
  7307. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7308. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7309. LPFC_WQE_LENLOC_NONE);
  7310. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7311. command_type = OTHER_COMMAND;
  7312. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7313. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7314. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7315. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7316. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7317. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7318. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7319. }
  7320. break;
  7321. case CMD_XRI_ABORTED_CX:
  7322. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7323. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7324. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7325. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7326. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7327. default:
  7328. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7329. "2014 Invalid command 0x%x\n",
  7330. iocbq->iocb.ulpCommand);
  7331. return IOCB_ERROR;
  7332. break;
  7333. }
  7334. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7335. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7336. wqe->generic.wqe_com.abort_tag = abort_tag;
  7337. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7338. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7339. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7340. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7341. return 0;
  7342. }
  7343. /**
  7344. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7345. * @phba: Pointer to HBA context object.
  7346. * @ring_number: SLI ring number to issue iocb on.
  7347. * @piocb: Pointer to command iocb.
  7348. * @flag: Flag indicating if this command can be put into txq.
  7349. *
  7350. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7351. * an iocb command to an HBA with SLI-4 interface spec.
  7352. *
  7353. * This function is called with hbalock held. The function will return success
  7354. * after it successfully submit the iocb to firmware or after adding to the
  7355. * txq.
  7356. **/
  7357. static int
  7358. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7359. struct lpfc_iocbq *piocb, uint32_t flag)
  7360. {
  7361. struct lpfc_sglq *sglq;
  7362. union lpfc_wqe wqe;
  7363. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7364. if (piocb->sli4_xritag == NO_XRI) {
  7365. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7366. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7367. sglq = NULL;
  7368. else {
  7369. if (pring->txq_cnt) {
  7370. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7371. __lpfc_sli_ringtx_put(phba,
  7372. pring, piocb);
  7373. return IOCB_SUCCESS;
  7374. } else {
  7375. return IOCB_BUSY;
  7376. }
  7377. } else {
  7378. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7379. if (!sglq) {
  7380. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7381. __lpfc_sli_ringtx_put(phba,
  7382. pring,
  7383. piocb);
  7384. return IOCB_SUCCESS;
  7385. } else
  7386. return IOCB_BUSY;
  7387. }
  7388. }
  7389. }
  7390. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7391. /* These IO's already have an XRI and a mapped sgl. */
  7392. sglq = NULL;
  7393. } else {
  7394. /*
  7395. * This is a continuation of a commandi,(CX) so this
  7396. * sglq is on the active list
  7397. */
  7398. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  7399. if (!sglq)
  7400. return IOCB_ERROR;
  7401. }
  7402. if (sglq) {
  7403. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7404. piocb->sli4_xritag = sglq->sli4_xritag;
  7405. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7406. return IOCB_ERROR;
  7407. }
  7408. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7409. return IOCB_ERROR;
  7410. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7411. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7412. /*
  7413. * For FCP command IOCB, get a new WQ index to distribute
  7414. * WQE across the WQsr. On the other hand, for abort IOCB,
  7415. * it carries the same WQ index to the original command
  7416. * IOCB.
  7417. */
  7418. if (piocb->iocb_flag & LPFC_IO_FCP)
  7419. piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7420. if (unlikely(!phba->sli4_hba.fcp_wq))
  7421. return IOCB_ERROR;
  7422. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7423. &wqe))
  7424. return IOCB_ERROR;
  7425. } else {
  7426. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7427. return IOCB_ERROR;
  7428. }
  7429. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7430. return 0;
  7431. }
  7432. /**
  7433. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7434. *
  7435. * This routine wraps the actual lockless version for issusing IOCB function
  7436. * pointer from the lpfc_hba struct.
  7437. *
  7438. * Return codes:
  7439. * IOCB_ERROR - Error
  7440. * IOCB_SUCCESS - Success
  7441. * IOCB_BUSY - Busy
  7442. **/
  7443. int
  7444. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7445. struct lpfc_iocbq *piocb, uint32_t flag)
  7446. {
  7447. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7448. }
  7449. /**
  7450. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7451. * @phba: The hba struct for which this call is being executed.
  7452. * @dev_grp: The HBA PCI-Device group number.
  7453. *
  7454. * This routine sets up the SLI interface API function jump table in @phba
  7455. * struct.
  7456. * Returns: 0 - success, -ENODEV - failure.
  7457. **/
  7458. int
  7459. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7460. {
  7461. switch (dev_grp) {
  7462. case LPFC_PCI_DEV_LP:
  7463. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7464. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7465. break;
  7466. case LPFC_PCI_DEV_OC:
  7467. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7468. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7469. break;
  7470. default:
  7471. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7472. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7473. dev_grp);
  7474. return -ENODEV;
  7475. break;
  7476. }
  7477. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7478. return 0;
  7479. }
  7480. /**
  7481. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7482. * @phba: Pointer to HBA context object.
  7483. * @pring: Pointer to driver SLI ring object.
  7484. * @piocb: Pointer to command iocb.
  7485. * @flag: Flag indicating if this command can be put into txq.
  7486. *
  7487. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7488. * function. This function gets the hbalock and calls
  7489. * __lpfc_sli_issue_iocb function and will return the error returned
  7490. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7491. * functions which do not hold hbalock.
  7492. **/
  7493. int
  7494. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7495. struct lpfc_iocbq *piocb, uint32_t flag)
  7496. {
  7497. unsigned long iflags;
  7498. int rc;
  7499. spin_lock_irqsave(&phba->hbalock, iflags);
  7500. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7501. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7502. return rc;
  7503. }
  7504. /**
  7505. * lpfc_extra_ring_setup - Extra ring setup function
  7506. * @phba: Pointer to HBA context object.
  7507. *
  7508. * This function is called while driver attaches with the
  7509. * HBA to setup the extra ring. The extra ring is used
  7510. * only when driver needs to support target mode functionality
  7511. * or IP over FC functionalities.
  7512. *
  7513. * This function is called with no lock held.
  7514. **/
  7515. static int
  7516. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7517. {
  7518. struct lpfc_sli *psli;
  7519. struct lpfc_sli_ring *pring;
  7520. psli = &phba->sli;
  7521. /* Adjust cmd/rsp ring iocb entries more evenly */
  7522. /* Take some away from the FCP ring */
  7523. pring = &psli->ring[psli->fcp_ring];
  7524. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7525. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7526. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7527. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7528. /* and give them to the extra ring */
  7529. pring = &psli->ring[psli->extra_ring];
  7530. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7531. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7532. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7533. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7534. /* Setup default profile for this ring */
  7535. pring->iotag_max = 4096;
  7536. pring->num_mask = 1;
  7537. pring->prt[0].profile = 0; /* Mask 0 */
  7538. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7539. pring->prt[0].type = phba->cfg_multi_ring_type;
  7540. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7541. return 0;
  7542. }
  7543. /* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
  7544. * @vport: pointer to virtual port object.
  7545. * @ndlp: nodelist pointer for the impacted rport.
  7546. *
  7547. * The driver calls this routine in response to a XRI ABORT CQE
  7548. * event from the port. In this event, the driver is required to
  7549. * recover its login to the rport even though its login may be valid
  7550. * from the driver's perspective. The failed ABTS notice from the
  7551. * port indicates the rport is not responding.
  7552. */
  7553. static void
  7554. lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
  7555. struct lpfc_nodelist *ndlp)
  7556. {
  7557. struct Scsi_Host *shost;
  7558. struct lpfc_hba *phba;
  7559. unsigned long flags = 0;
  7560. shost = lpfc_shost_from_vport(vport);
  7561. phba = vport->phba;
  7562. if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
  7563. lpfc_printf_log(phba, KERN_INFO,
  7564. LOG_SLI, "3093 No rport recovery needed. "
  7565. "rport in state 0x%x\n",
  7566. ndlp->nlp_state);
  7567. return;
  7568. }
  7569. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7570. "3094 Start rport recovery on shost id 0x%x "
  7571. "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
  7572. "flags 0x%x\n",
  7573. shost->host_no, ndlp->nlp_DID,
  7574. vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
  7575. ndlp->nlp_flag);
  7576. /*
  7577. * The rport is not responding. Don't attempt ADISC recovery.
  7578. * Remove the FCP-2 flag to force a PLOGI.
  7579. */
  7580. spin_lock_irqsave(shost->host_lock, flags);
  7581. ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  7582. spin_unlock_irqrestore(shost->host_lock, flags);
  7583. lpfc_disc_state_machine(vport, ndlp, NULL,
  7584. NLP_EVT_DEVICE_RECOVERY);
  7585. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  7586. spin_lock_irqsave(shost->host_lock, flags);
  7587. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  7588. spin_unlock_irqrestore(shost->host_lock, flags);
  7589. lpfc_disc_start(vport);
  7590. }
  7591. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7592. * @phba: Pointer to HBA context object.
  7593. * @iocbq: Pointer to iocb object.
  7594. *
  7595. * The async_event handler calls this routine when it receives
  7596. * an ASYNC_STATUS_CN event from the port. The port generates
  7597. * this event when an Abort Sequence request to an rport fails
  7598. * twice in succession. The abort could be originated by the
  7599. * driver or by the port. The ABTS could have been for an ELS
  7600. * or FCP IO. The port only generates this event when an ABTS
  7601. * fails to complete after one retry.
  7602. */
  7603. static void
  7604. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7605. struct lpfc_iocbq *iocbq)
  7606. {
  7607. struct lpfc_nodelist *ndlp = NULL;
  7608. uint16_t rpi = 0, vpi = 0;
  7609. struct lpfc_vport *vport = NULL;
  7610. /* The rpi in the ulpContext is vport-sensitive. */
  7611. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7612. rpi = iocbq->iocb.ulpContext;
  7613. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7614. "3092 Port generated ABTS async event "
  7615. "on vpi %d rpi %d status 0x%x\n",
  7616. vpi, rpi, iocbq->iocb.ulpStatus);
  7617. vport = lpfc_find_vport_by_vpid(phba, vpi);
  7618. if (!vport)
  7619. goto err_exit;
  7620. ndlp = lpfc_findnode_rpi(vport, rpi);
  7621. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7622. goto err_exit;
  7623. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  7624. lpfc_sli_abts_recover_port(vport, ndlp);
  7625. return;
  7626. err_exit:
  7627. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7628. "3095 Event Context not found, no "
  7629. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  7630. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  7631. vpi, rpi);
  7632. }
  7633. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  7634. * @phba: pointer to HBA context object.
  7635. * @ndlp: nodelist pointer for the impacted rport.
  7636. * @axri: pointer to the wcqe containing the failed exchange.
  7637. *
  7638. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  7639. * port. The port generates this event when an abort exchange request to an
  7640. * rport fails twice in succession with no reply. The abort could be originated
  7641. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  7642. */
  7643. void
  7644. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  7645. struct lpfc_nodelist *ndlp,
  7646. struct sli4_wcqe_xri_aborted *axri)
  7647. {
  7648. struct lpfc_vport *vport;
  7649. uint32_t ext_status = 0;
  7650. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  7651. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7652. "3115 Node Context not found, driver "
  7653. "ignoring abts err event\n");
  7654. return;
  7655. }
  7656. vport = ndlp->vport;
  7657. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7658. "3116 Port generated FCP XRI ABORT event on "
  7659. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  7660. ndlp->vport->vpi, ndlp->nlp_rpi,
  7661. bf_get(lpfc_wcqe_xa_xri, axri),
  7662. bf_get(lpfc_wcqe_xa_status, axri),
  7663. axri->parameter);
  7664. /*
  7665. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  7666. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  7667. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  7668. */
  7669. ext_status = axri->parameter & WCQE_PARAM_MASK;
  7670. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  7671. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  7672. lpfc_sli_abts_recover_port(vport, ndlp);
  7673. }
  7674. /**
  7675. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7676. * @phba: Pointer to HBA context object.
  7677. * @pring: Pointer to driver SLI ring object.
  7678. * @iocbq: Pointer to iocb object.
  7679. *
  7680. * This function is called by the slow ring event handler
  7681. * function when there is an ASYNC event iocb in the ring.
  7682. * This function is called with no lock held.
  7683. * Currently this function handles only temperature related
  7684. * ASYNC events. The function decodes the temperature sensor
  7685. * event message and posts events for the management applications.
  7686. **/
  7687. static void
  7688. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7689. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7690. {
  7691. IOCB_t *icmd;
  7692. uint16_t evt_code;
  7693. struct temp_event temp_event_data;
  7694. struct Scsi_Host *shost;
  7695. uint32_t *iocb_w;
  7696. icmd = &iocbq->iocb;
  7697. evt_code = icmd->un.asyncstat.evt_code;
  7698. switch (evt_code) {
  7699. case ASYNC_TEMP_WARN:
  7700. case ASYNC_TEMP_SAFE:
  7701. temp_event_data.data = (uint32_t) icmd->ulpContext;
  7702. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7703. if (evt_code == ASYNC_TEMP_WARN) {
  7704. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7705. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7706. "0347 Adapter is very hot, please take "
  7707. "corrective action. temperature : %d Celsius\n",
  7708. (uint32_t) icmd->ulpContext);
  7709. } else {
  7710. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7711. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7712. "0340 Adapter temperature is OK now. "
  7713. "temperature : %d Celsius\n",
  7714. (uint32_t) icmd->ulpContext);
  7715. }
  7716. /* Send temperature change event to applications */
  7717. shost = lpfc_shost_from_vport(phba->pport);
  7718. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7719. sizeof(temp_event_data), (char *) &temp_event_data,
  7720. LPFC_NL_VENDOR_ID);
  7721. break;
  7722. case ASYNC_STATUS_CN:
  7723. lpfc_sli_abts_err_handler(phba, iocbq);
  7724. break;
  7725. default:
  7726. iocb_w = (uint32_t *) icmd;
  7727. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7728. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7729. " evt_code 0x%x\n"
  7730. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7731. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7732. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7733. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7734. pring->ringno, icmd->un.asyncstat.evt_code,
  7735. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7736. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  7737. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  7738. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  7739. break;
  7740. }
  7741. }
  7742. /**
  7743. * lpfc_sli_setup - SLI ring setup function
  7744. * @phba: Pointer to HBA context object.
  7745. *
  7746. * lpfc_sli_setup sets up rings of the SLI interface with
  7747. * number of iocbs per ring and iotags. This function is
  7748. * called while driver attach to the HBA and before the
  7749. * interrupts are enabled. So there is no need for locking.
  7750. *
  7751. * This function always returns 0.
  7752. **/
  7753. int
  7754. lpfc_sli_setup(struct lpfc_hba *phba)
  7755. {
  7756. int i, totiocbsize = 0;
  7757. struct lpfc_sli *psli = &phba->sli;
  7758. struct lpfc_sli_ring *pring;
  7759. psli->num_rings = MAX_CONFIGURED_RINGS;
  7760. psli->sli_flag = 0;
  7761. psli->fcp_ring = LPFC_FCP_RING;
  7762. psli->next_ring = LPFC_FCP_NEXT_RING;
  7763. psli->extra_ring = LPFC_EXTRA_RING;
  7764. psli->iocbq_lookup = NULL;
  7765. psli->iocbq_lookup_len = 0;
  7766. psli->last_iotag = 0;
  7767. for (i = 0; i < psli->num_rings; i++) {
  7768. pring = &psli->ring[i];
  7769. switch (i) {
  7770. case LPFC_FCP_RING: /* ring 0 - FCP */
  7771. /* numCiocb and numRiocb are used in config_port */
  7772. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  7773. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  7774. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7775. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7776. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7777. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7778. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7779. SLI3_IOCB_CMD_SIZE :
  7780. SLI2_IOCB_CMD_SIZE;
  7781. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7782. SLI3_IOCB_RSP_SIZE :
  7783. SLI2_IOCB_RSP_SIZE;
  7784. pring->iotag_ctr = 0;
  7785. pring->iotag_max =
  7786. (phba->cfg_hba_queue_depth * 2);
  7787. pring->fast_iotag = pring->iotag_max;
  7788. pring->num_mask = 0;
  7789. break;
  7790. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  7791. /* numCiocb and numRiocb are used in config_port */
  7792. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  7793. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  7794. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7795. SLI3_IOCB_CMD_SIZE :
  7796. SLI2_IOCB_CMD_SIZE;
  7797. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7798. SLI3_IOCB_RSP_SIZE :
  7799. SLI2_IOCB_RSP_SIZE;
  7800. pring->iotag_max = phba->cfg_hba_queue_depth;
  7801. pring->num_mask = 0;
  7802. break;
  7803. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  7804. /* numCiocb and numRiocb are used in config_port */
  7805. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  7806. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  7807. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7808. SLI3_IOCB_CMD_SIZE :
  7809. SLI2_IOCB_CMD_SIZE;
  7810. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7811. SLI3_IOCB_RSP_SIZE :
  7812. SLI2_IOCB_RSP_SIZE;
  7813. pring->fast_iotag = 0;
  7814. pring->iotag_ctr = 0;
  7815. pring->iotag_max = 4096;
  7816. pring->lpfc_sli_rcv_async_status =
  7817. lpfc_sli_async_event_handler;
  7818. pring->num_mask = LPFC_MAX_RING_MASK;
  7819. pring->prt[0].profile = 0; /* Mask 0 */
  7820. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  7821. pring->prt[0].type = FC_TYPE_ELS;
  7822. pring->prt[0].lpfc_sli_rcv_unsol_event =
  7823. lpfc_els_unsol_event;
  7824. pring->prt[1].profile = 0; /* Mask 1 */
  7825. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  7826. pring->prt[1].type = FC_TYPE_ELS;
  7827. pring->prt[1].lpfc_sli_rcv_unsol_event =
  7828. lpfc_els_unsol_event;
  7829. pring->prt[2].profile = 0; /* Mask 2 */
  7830. /* NameServer Inquiry */
  7831. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  7832. /* NameServer */
  7833. pring->prt[2].type = FC_TYPE_CT;
  7834. pring->prt[2].lpfc_sli_rcv_unsol_event =
  7835. lpfc_ct_unsol_event;
  7836. pring->prt[3].profile = 0; /* Mask 3 */
  7837. /* NameServer response */
  7838. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  7839. /* NameServer */
  7840. pring->prt[3].type = FC_TYPE_CT;
  7841. pring->prt[3].lpfc_sli_rcv_unsol_event =
  7842. lpfc_ct_unsol_event;
  7843. /* abort unsolicited sequence */
  7844. pring->prt[4].profile = 0; /* Mask 4 */
  7845. pring->prt[4].rctl = FC_RCTL_BA_ABTS;
  7846. pring->prt[4].type = FC_TYPE_BLS;
  7847. pring->prt[4].lpfc_sli_rcv_unsol_event =
  7848. lpfc_sli4_ct_abort_unsol_event;
  7849. break;
  7850. }
  7851. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  7852. (pring->numRiocb * pring->sizeRiocb);
  7853. }
  7854. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  7855. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  7856. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  7857. "SLI2 SLIM Data: x%x x%lx\n",
  7858. phba->brd_no, totiocbsize,
  7859. (unsigned long) MAX_SLIM_IOCB_SIZE);
  7860. }
  7861. if (phba->cfg_multi_ring_support == 2)
  7862. lpfc_extra_ring_setup(phba);
  7863. return 0;
  7864. }
  7865. /**
  7866. * lpfc_sli_queue_setup - Queue initialization function
  7867. * @phba: Pointer to HBA context object.
  7868. *
  7869. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  7870. * ring. This function also initializes ring indices of each ring.
  7871. * This function is called during the initialization of the SLI
  7872. * interface of an HBA.
  7873. * This function is called with no lock held and always returns
  7874. * 1.
  7875. **/
  7876. int
  7877. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  7878. {
  7879. struct lpfc_sli *psli;
  7880. struct lpfc_sli_ring *pring;
  7881. int i;
  7882. psli = &phba->sli;
  7883. spin_lock_irq(&phba->hbalock);
  7884. INIT_LIST_HEAD(&psli->mboxq);
  7885. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  7886. /* Initialize list headers for txq and txcmplq as double linked lists */
  7887. for (i = 0; i < psli->num_rings; i++) {
  7888. pring = &psli->ring[i];
  7889. pring->ringno = i;
  7890. pring->next_cmdidx = 0;
  7891. pring->local_getidx = 0;
  7892. pring->cmdidx = 0;
  7893. INIT_LIST_HEAD(&pring->txq);
  7894. INIT_LIST_HEAD(&pring->txcmplq);
  7895. INIT_LIST_HEAD(&pring->iocb_continueq);
  7896. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  7897. INIT_LIST_HEAD(&pring->postbufq);
  7898. }
  7899. spin_unlock_irq(&phba->hbalock);
  7900. return 1;
  7901. }
  7902. /**
  7903. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  7904. * @phba: Pointer to HBA context object.
  7905. *
  7906. * This routine flushes the mailbox command subsystem. It will unconditionally
  7907. * flush all the mailbox commands in the three possible stages in the mailbox
  7908. * command sub-system: pending mailbox command queue; the outstanding mailbox
  7909. * command; and completed mailbox command queue. It is caller's responsibility
  7910. * to make sure that the driver is in the proper state to flush the mailbox
  7911. * command sub-system. Namely, the posting of mailbox commands into the
  7912. * pending mailbox command queue from the various clients must be stopped;
  7913. * either the HBA is in a state that it will never works on the outstanding
  7914. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  7915. * mailbox command has been completed.
  7916. **/
  7917. static void
  7918. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  7919. {
  7920. LIST_HEAD(completions);
  7921. struct lpfc_sli *psli = &phba->sli;
  7922. LPFC_MBOXQ_t *pmb;
  7923. unsigned long iflag;
  7924. /* Flush all the mailbox commands in the mbox system */
  7925. spin_lock_irqsave(&phba->hbalock, iflag);
  7926. /* The pending mailbox command queue */
  7927. list_splice_init(&phba->sli.mboxq, &completions);
  7928. /* The outstanding active mailbox command */
  7929. if (psli->mbox_active) {
  7930. list_add_tail(&psli->mbox_active->list, &completions);
  7931. psli->mbox_active = NULL;
  7932. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7933. }
  7934. /* The completed mailbox command queue */
  7935. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  7936. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7937. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  7938. while (!list_empty(&completions)) {
  7939. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  7940. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  7941. if (pmb->mbox_cmpl)
  7942. pmb->mbox_cmpl(phba, pmb);
  7943. }
  7944. }
  7945. /**
  7946. * lpfc_sli_host_down - Vport cleanup function
  7947. * @vport: Pointer to virtual port object.
  7948. *
  7949. * lpfc_sli_host_down is called to clean up the resources
  7950. * associated with a vport before destroying virtual
  7951. * port data structures.
  7952. * This function does following operations:
  7953. * - Free discovery resources associated with this virtual
  7954. * port.
  7955. * - Free iocbs associated with this virtual port in
  7956. * the txq.
  7957. * - Send abort for all iocb commands associated with this
  7958. * vport in txcmplq.
  7959. *
  7960. * This function is called with no lock held and always returns 1.
  7961. **/
  7962. int
  7963. lpfc_sli_host_down(struct lpfc_vport *vport)
  7964. {
  7965. LIST_HEAD(completions);
  7966. struct lpfc_hba *phba = vport->phba;
  7967. struct lpfc_sli *psli = &phba->sli;
  7968. struct lpfc_sli_ring *pring;
  7969. struct lpfc_iocbq *iocb, *next_iocb;
  7970. int i;
  7971. unsigned long flags = 0;
  7972. uint16_t prev_pring_flag;
  7973. lpfc_cleanup_discovery_resources(vport);
  7974. spin_lock_irqsave(&phba->hbalock, flags);
  7975. for (i = 0; i < psli->num_rings; i++) {
  7976. pring = &psli->ring[i];
  7977. prev_pring_flag = pring->flag;
  7978. /* Only slow rings */
  7979. if (pring->ringno == LPFC_ELS_RING) {
  7980. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  7981. /* Set the lpfc data pending flag */
  7982. set_bit(LPFC_DATA_READY, &phba->data_flags);
  7983. }
  7984. /*
  7985. * Error everything on the txq since these iocbs have not been
  7986. * given to the FW yet.
  7987. */
  7988. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  7989. if (iocb->vport != vport)
  7990. continue;
  7991. list_move_tail(&iocb->list, &completions);
  7992. pring->txq_cnt--;
  7993. }
  7994. /* Next issue ABTS for everything on the txcmplq */
  7995. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  7996. list) {
  7997. if (iocb->vport != vport)
  7998. continue;
  7999. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8000. }
  8001. pring->flag = prev_pring_flag;
  8002. }
  8003. spin_unlock_irqrestore(&phba->hbalock, flags);
  8004. /* Cancel all the IOCBs from the completions list */
  8005. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8006. IOERR_SLI_DOWN);
  8007. return 1;
  8008. }
  8009. /**
  8010. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8011. * @phba: Pointer to HBA context object.
  8012. *
  8013. * This function cleans up all iocb, buffers, mailbox commands
  8014. * while shutting down the HBA. This function is called with no
  8015. * lock held and always returns 1.
  8016. * This function does the following to cleanup driver resources:
  8017. * - Free discovery resources for each virtual port
  8018. * - Cleanup any pending fabric iocbs
  8019. * - Iterate through the iocb txq and free each entry
  8020. * in the list.
  8021. * - Free up any buffer posted to the HBA
  8022. * - Free mailbox commands in the mailbox queue.
  8023. **/
  8024. int
  8025. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8026. {
  8027. LIST_HEAD(completions);
  8028. struct lpfc_sli *psli = &phba->sli;
  8029. struct lpfc_sli_ring *pring;
  8030. struct lpfc_dmabuf *buf_ptr;
  8031. unsigned long flags = 0;
  8032. int i;
  8033. /* Shutdown the mailbox command sub-system */
  8034. lpfc_sli_mbox_sys_shutdown(phba);
  8035. lpfc_hba_down_prep(phba);
  8036. lpfc_fabric_abort_hba(phba);
  8037. spin_lock_irqsave(&phba->hbalock, flags);
  8038. for (i = 0; i < psli->num_rings; i++) {
  8039. pring = &psli->ring[i];
  8040. /* Only slow rings */
  8041. if (pring->ringno == LPFC_ELS_RING) {
  8042. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8043. /* Set the lpfc data pending flag */
  8044. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8045. }
  8046. /*
  8047. * Error everything on the txq since these iocbs have not been
  8048. * given to the FW yet.
  8049. */
  8050. list_splice_init(&pring->txq, &completions);
  8051. pring->txq_cnt = 0;
  8052. }
  8053. spin_unlock_irqrestore(&phba->hbalock, flags);
  8054. /* Cancel all the IOCBs from the completions list */
  8055. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8056. IOERR_SLI_DOWN);
  8057. spin_lock_irqsave(&phba->hbalock, flags);
  8058. list_splice_init(&phba->elsbuf, &completions);
  8059. phba->elsbuf_cnt = 0;
  8060. phba->elsbuf_prev_cnt = 0;
  8061. spin_unlock_irqrestore(&phba->hbalock, flags);
  8062. while (!list_empty(&completions)) {
  8063. list_remove_head(&completions, buf_ptr,
  8064. struct lpfc_dmabuf, list);
  8065. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8066. kfree(buf_ptr);
  8067. }
  8068. /* Return any active mbox cmds */
  8069. del_timer_sync(&psli->mbox_tmo);
  8070. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8071. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8072. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8073. return 1;
  8074. }
  8075. /**
  8076. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8077. * @srcp: Source memory pointer.
  8078. * @destp: Destination memory pointer.
  8079. * @cnt: Number of words required to be copied.
  8080. *
  8081. * This function is used for copying data between driver memory
  8082. * and the SLI memory. This function also changes the endianness
  8083. * of each word if native endianness is different from SLI
  8084. * endianness. This function can be called with or without
  8085. * lock.
  8086. **/
  8087. void
  8088. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8089. {
  8090. uint32_t *src = srcp;
  8091. uint32_t *dest = destp;
  8092. uint32_t ldata;
  8093. int i;
  8094. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8095. ldata = *src;
  8096. ldata = le32_to_cpu(ldata);
  8097. *dest = ldata;
  8098. src++;
  8099. dest++;
  8100. }
  8101. }
  8102. /**
  8103. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8104. * @srcp: Source memory pointer.
  8105. * @destp: Destination memory pointer.
  8106. * @cnt: Number of words required to be copied.
  8107. *
  8108. * This function is used for copying data between a data structure
  8109. * with big endian representation to local endianness.
  8110. * This function can be called with or without lock.
  8111. **/
  8112. void
  8113. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8114. {
  8115. uint32_t *src = srcp;
  8116. uint32_t *dest = destp;
  8117. uint32_t ldata;
  8118. int i;
  8119. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8120. ldata = *src;
  8121. ldata = be32_to_cpu(ldata);
  8122. *dest = ldata;
  8123. src++;
  8124. dest++;
  8125. }
  8126. }
  8127. /**
  8128. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8129. * @phba: Pointer to HBA context object.
  8130. * @pring: Pointer to driver SLI ring object.
  8131. * @mp: Pointer to driver buffer object.
  8132. *
  8133. * This function is called with no lock held.
  8134. * It always return zero after adding the buffer to the postbufq
  8135. * buffer list.
  8136. **/
  8137. int
  8138. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8139. struct lpfc_dmabuf *mp)
  8140. {
  8141. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8142. later */
  8143. spin_lock_irq(&phba->hbalock);
  8144. list_add_tail(&mp->list, &pring->postbufq);
  8145. pring->postbufq_cnt++;
  8146. spin_unlock_irq(&phba->hbalock);
  8147. return 0;
  8148. }
  8149. /**
  8150. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8151. * @phba: Pointer to HBA context object.
  8152. *
  8153. * When HBQ is enabled, buffers are searched based on tags. This function
  8154. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8155. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8156. * does not conflict with tags of buffer posted for unsolicited events.
  8157. * The function returns the allocated tag. The function is called with
  8158. * no locks held.
  8159. **/
  8160. uint32_t
  8161. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8162. {
  8163. spin_lock_irq(&phba->hbalock);
  8164. phba->buffer_tag_count++;
  8165. /*
  8166. * Always set the QUE_BUFTAG_BIT to distiguish between
  8167. * a tag assigned by HBQ.
  8168. */
  8169. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8170. spin_unlock_irq(&phba->hbalock);
  8171. return phba->buffer_tag_count;
  8172. }
  8173. /**
  8174. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8175. * @phba: Pointer to HBA context object.
  8176. * @pring: Pointer to driver SLI ring object.
  8177. * @tag: Buffer tag.
  8178. *
  8179. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8180. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8181. * iocb is posted to the response ring with the tag of the buffer.
  8182. * This function searches the pring->postbufq list using the tag
  8183. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8184. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8185. * buffer is returned to the caller else NULL is returned.
  8186. * This function is called with no lock held.
  8187. **/
  8188. struct lpfc_dmabuf *
  8189. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8190. uint32_t tag)
  8191. {
  8192. struct lpfc_dmabuf *mp, *next_mp;
  8193. struct list_head *slp = &pring->postbufq;
  8194. /* Search postbufq, from the beginning, looking for a match on tag */
  8195. spin_lock_irq(&phba->hbalock);
  8196. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8197. if (mp->buffer_tag == tag) {
  8198. list_del_init(&mp->list);
  8199. pring->postbufq_cnt--;
  8200. spin_unlock_irq(&phba->hbalock);
  8201. return mp;
  8202. }
  8203. }
  8204. spin_unlock_irq(&phba->hbalock);
  8205. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8206. "0402 Cannot find virtual addr for buffer tag on "
  8207. "ring %d Data x%lx x%p x%p x%x\n",
  8208. pring->ringno, (unsigned long) tag,
  8209. slp->next, slp->prev, pring->postbufq_cnt);
  8210. return NULL;
  8211. }
  8212. /**
  8213. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8214. * @phba: Pointer to HBA context object.
  8215. * @pring: Pointer to driver SLI ring object.
  8216. * @phys: DMA address of the buffer.
  8217. *
  8218. * This function searches the buffer list using the dma_address
  8219. * of unsolicited event to find the driver's lpfc_dmabuf object
  8220. * corresponding to the dma_address. The function returns the
  8221. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8222. * This function is called by the ct and els unsolicited event
  8223. * handlers to get the buffer associated with the unsolicited
  8224. * event.
  8225. *
  8226. * This function is called with no lock held.
  8227. **/
  8228. struct lpfc_dmabuf *
  8229. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8230. dma_addr_t phys)
  8231. {
  8232. struct lpfc_dmabuf *mp, *next_mp;
  8233. struct list_head *slp = &pring->postbufq;
  8234. /* Search postbufq, from the beginning, looking for a match on phys */
  8235. spin_lock_irq(&phba->hbalock);
  8236. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8237. if (mp->phys == phys) {
  8238. list_del_init(&mp->list);
  8239. pring->postbufq_cnt--;
  8240. spin_unlock_irq(&phba->hbalock);
  8241. return mp;
  8242. }
  8243. }
  8244. spin_unlock_irq(&phba->hbalock);
  8245. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8246. "0410 Cannot find virtual addr for mapped buf on "
  8247. "ring %d Data x%llx x%p x%p x%x\n",
  8248. pring->ringno, (unsigned long long)phys,
  8249. slp->next, slp->prev, pring->postbufq_cnt);
  8250. return NULL;
  8251. }
  8252. /**
  8253. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8254. * @phba: Pointer to HBA context object.
  8255. * @cmdiocb: Pointer to driver command iocb object.
  8256. * @rspiocb: Pointer to driver response iocb object.
  8257. *
  8258. * This function is the completion handler for the abort iocbs for
  8259. * ELS commands. This function is called from the ELS ring event
  8260. * handler with no lock held. This function frees memory resources
  8261. * associated with the abort iocb.
  8262. **/
  8263. static void
  8264. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8265. struct lpfc_iocbq *rspiocb)
  8266. {
  8267. IOCB_t *irsp = &rspiocb->iocb;
  8268. uint16_t abort_iotag, abort_context;
  8269. struct lpfc_iocbq *abort_iocb = NULL;
  8270. if (irsp->ulpStatus) {
  8271. /*
  8272. * Assume that the port already completed and returned, or
  8273. * will return the iocb. Just Log the message.
  8274. */
  8275. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8276. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8277. spin_lock_irq(&phba->hbalock);
  8278. if (phba->sli_rev < LPFC_SLI_REV4) {
  8279. if (abort_iotag != 0 &&
  8280. abort_iotag <= phba->sli.last_iotag)
  8281. abort_iocb =
  8282. phba->sli.iocbq_lookup[abort_iotag];
  8283. } else
  8284. /* For sli4 the abort_tag is the XRI,
  8285. * so the abort routine puts the iotag of the iocb
  8286. * being aborted in the context field of the abort
  8287. * IOCB.
  8288. */
  8289. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8290. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8291. "0327 Cannot abort els iocb %p "
  8292. "with tag %x context %x, abort status %x, "
  8293. "abort code %x\n",
  8294. abort_iocb, abort_iotag, abort_context,
  8295. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8296. spin_unlock_irq(&phba->hbalock);
  8297. }
  8298. lpfc_sli_release_iocbq(phba, cmdiocb);
  8299. return;
  8300. }
  8301. /**
  8302. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8303. * @phba: Pointer to HBA context object.
  8304. * @cmdiocb: Pointer to driver command iocb object.
  8305. * @rspiocb: Pointer to driver response iocb object.
  8306. *
  8307. * The function is called from SLI ring event handler with no
  8308. * lock held. This function is the completion handler for ELS commands
  8309. * which are aborted. The function frees memory resources used for
  8310. * the aborted ELS commands.
  8311. **/
  8312. static void
  8313. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8314. struct lpfc_iocbq *rspiocb)
  8315. {
  8316. IOCB_t *irsp = &rspiocb->iocb;
  8317. /* ELS cmd tag <ulpIoTag> completes */
  8318. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8319. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8320. "x%x x%x x%x\n",
  8321. irsp->ulpIoTag, irsp->ulpStatus,
  8322. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8323. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8324. lpfc_ct_free_iocb(phba, cmdiocb);
  8325. else
  8326. lpfc_els_free_iocb(phba, cmdiocb);
  8327. return;
  8328. }
  8329. /**
  8330. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8331. * @phba: Pointer to HBA context object.
  8332. * @pring: Pointer to driver SLI ring object.
  8333. * @cmdiocb: Pointer to driver command iocb object.
  8334. *
  8335. * This function issues an abort iocb for the provided command iocb down to
  8336. * the port. Other than the case the outstanding command iocb is an abort
  8337. * request, this function issues abort out unconditionally. This function is
  8338. * called with hbalock held. The function returns 0 when it fails due to
  8339. * memory allocation failure or when the command iocb is an abort request.
  8340. **/
  8341. static int
  8342. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8343. struct lpfc_iocbq *cmdiocb)
  8344. {
  8345. struct lpfc_vport *vport = cmdiocb->vport;
  8346. struct lpfc_iocbq *abtsiocbp;
  8347. IOCB_t *icmd = NULL;
  8348. IOCB_t *iabt = NULL;
  8349. int retval;
  8350. /*
  8351. * There are certain command types we don't want to abort. And we
  8352. * don't want to abort commands that are already in the process of
  8353. * being aborted.
  8354. */
  8355. icmd = &cmdiocb->iocb;
  8356. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8357. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8358. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8359. return 0;
  8360. /* issue ABTS for this IOCB based on iotag */
  8361. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8362. if (abtsiocbp == NULL)
  8363. return 0;
  8364. /* This signals the response to set the correct status
  8365. * before calling the completion handler
  8366. */
  8367. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8368. iabt = &abtsiocbp->iocb;
  8369. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8370. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8371. if (phba->sli_rev == LPFC_SLI_REV4) {
  8372. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8373. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8374. }
  8375. else
  8376. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8377. iabt->ulpLe = 1;
  8378. iabt->ulpClass = icmd->ulpClass;
  8379. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8380. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8381. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8382. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8383. if (phba->link_state >= LPFC_LINK_UP)
  8384. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8385. else
  8386. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8387. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8388. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8389. "0339 Abort xri x%x, original iotag x%x, "
  8390. "abort cmd iotag x%x\n",
  8391. iabt->un.acxri.abortIoTag,
  8392. iabt->un.acxri.abortContextTag,
  8393. abtsiocbp->iotag);
  8394. retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
  8395. if (retval)
  8396. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8397. /*
  8398. * Caller to this routine should check for IOCB_ERROR
  8399. * and handle it properly. This routine no longer removes
  8400. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8401. */
  8402. return retval;
  8403. }
  8404. /**
  8405. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8406. * @phba: Pointer to HBA context object.
  8407. * @pring: Pointer to driver SLI ring object.
  8408. * @cmdiocb: Pointer to driver command iocb object.
  8409. *
  8410. * This function issues an abort iocb for the provided command iocb. In case
  8411. * of unloading, the abort iocb will not be issued to commands on the ELS
  8412. * ring. Instead, the callback function shall be changed to those commands
  8413. * so that nothing happens when them finishes. This function is called with
  8414. * hbalock held. The function returns 0 when the command iocb is an abort
  8415. * request.
  8416. **/
  8417. int
  8418. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8419. struct lpfc_iocbq *cmdiocb)
  8420. {
  8421. struct lpfc_vport *vport = cmdiocb->vport;
  8422. int retval = IOCB_ERROR;
  8423. IOCB_t *icmd = NULL;
  8424. /*
  8425. * There are certain command types we don't want to abort. And we
  8426. * don't want to abort commands that are already in the process of
  8427. * being aborted.
  8428. */
  8429. icmd = &cmdiocb->iocb;
  8430. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8431. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8432. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8433. return 0;
  8434. /*
  8435. * If we're unloading, don't abort iocb on the ELS ring, but change
  8436. * the callback so that nothing happens when it finishes.
  8437. */
  8438. if ((vport->load_flag & FC_UNLOADING) &&
  8439. (pring->ringno == LPFC_ELS_RING)) {
  8440. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8441. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8442. else
  8443. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8444. goto abort_iotag_exit;
  8445. }
  8446. /* Now, we try to issue the abort to the cmdiocb out */
  8447. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8448. abort_iotag_exit:
  8449. /*
  8450. * Caller to this routine should check for IOCB_ERROR
  8451. * and handle it properly. This routine no longer removes
  8452. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8453. */
  8454. return retval;
  8455. }
  8456. /**
  8457. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8458. * @phba: Pointer to HBA context object.
  8459. * @pring: Pointer to driver SLI ring object.
  8460. *
  8461. * This function aborts all iocbs in the given ring and frees all the iocb
  8462. * objects in txq. This function issues abort iocbs unconditionally for all
  8463. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8464. * to complete before the return of this function. The caller is not required
  8465. * to hold any locks.
  8466. **/
  8467. static void
  8468. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8469. {
  8470. LIST_HEAD(completions);
  8471. struct lpfc_iocbq *iocb, *next_iocb;
  8472. if (pring->ringno == LPFC_ELS_RING)
  8473. lpfc_fabric_abort_hba(phba);
  8474. spin_lock_irq(&phba->hbalock);
  8475. /* Take off all the iocbs on txq for cancelling */
  8476. list_splice_init(&pring->txq, &completions);
  8477. pring->txq_cnt = 0;
  8478. /* Next issue ABTS for everything on the txcmplq */
  8479. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8480. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8481. spin_unlock_irq(&phba->hbalock);
  8482. /* Cancel all the IOCBs from the completions list */
  8483. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8484. IOERR_SLI_ABORTED);
  8485. }
  8486. /**
  8487. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8488. * @phba: pointer to lpfc HBA data structure.
  8489. *
  8490. * This routine will abort all pending and outstanding iocbs to an HBA.
  8491. **/
  8492. void
  8493. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8494. {
  8495. struct lpfc_sli *psli = &phba->sli;
  8496. struct lpfc_sli_ring *pring;
  8497. int i;
  8498. for (i = 0; i < psli->num_rings; i++) {
  8499. pring = &psli->ring[i];
  8500. lpfc_sli_iocb_ring_abort(phba, pring);
  8501. }
  8502. }
  8503. /**
  8504. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8505. * @iocbq: Pointer to driver iocb object.
  8506. * @vport: Pointer to driver virtual port object.
  8507. * @tgt_id: SCSI ID of the target.
  8508. * @lun_id: LUN ID of the scsi device.
  8509. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8510. *
  8511. * This function acts as an iocb filter for functions which abort or count
  8512. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8513. * 0 if the filtering criteria is met for the given iocb and will return
  8514. * 1 if the filtering criteria is not met.
  8515. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8516. * given iocb is for the SCSI device specified by vport, tgt_id and
  8517. * lun_id parameter.
  8518. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8519. * given iocb is for the SCSI target specified by vport and tgt_id
  8520. * parameters.
  8521. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8522. * given iocb is for the SCSI host associated with the given vport.
  8523. * This function is called with no locks held.
  8524. **/
  8525. static int
  8526. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8527. uint16_t tgt_id, uint64_t lun_id,
  8528. lpfc_ctx_cmd ctx_cmd)
  8529. {
  8530. struct lpfc_scsi_buf *lpfc_cmd;
  8531. int rc = 1;
  8532. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8533. return rc;
  8534. if (iocbq->vport != vport)
  8535. return rc;
  8536. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8537. if (lpfc_cmd->pCmd == NULL)
  8538. return rc;
  8539. switch (ctx_cmd) {
  8540. case LPFC_CTX_LUN:
  8541. if ((lpfc_cmd->rdata->pnode) &&
  8542. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8543. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8544. rc = 0;
  8545. break;
  8546. case LPFC_CTX_TGT:
  8547. if ((lpfc_cmd->rdata->pnode) &&
  8548. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8549. rc = 0;
  8550. break;
  8551. case LPFC_CTX_HOST:
  8552. rc = 0;
  8553. break;
  8554. default:
  8555. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8556. __func__, ctx_cmd);
  8557. break;
  8558. }
  8559. return rc;
  8560. }
  8561. /**
  8562. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8563. * @vport: Pointer to virtual port.
  8564. * @tgt_id: SCSI ID of the target.
  8565. * @lun_id: LUN ID of the scsi device.
  8566. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8567. *
  8568. * This function returns number of FCP commands pending for the vport.
  8569. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8570. * commands pending on the vport associated with SCSI device specified
  8571. * by tgt_id and lun_id parameters.
  8572. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8573. * commands pending on the vport associated with SCSI target specified
  8574. * by tgt_id parameter.
  8575. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8576. * commands pending on the vport.
  8577. * This function returns the number of iocbs which satisfy the filter.
  8578. * This function is called without any lock held.
  8579. **/
  8580. int
  8581. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8582. lpfc_ctx_cmd ctx_cmd)
  8583. {
  8584. struct lpfc_hba *phba = vport->phba;
  8585. struct lpfc_iocbq *iocbq;
  8586. int sum, i;
  8587. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8588. iocbq = phba->sli.iocbq_lookup[i];
  8589. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8590. ctx_cmd) == 0)
  8591. sum++;
  8592. }
  8593. return sum;
  8594. }
  8595. /**
  8596. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8597. * @phba: Pointer to HBA context object
  8598. * @cmdiocb: Pointer to command iocb object.
  8599. * @rspiocb: Pointer to response iocb object.
  8600. *
  8601. * This function is called when an aborted FCP iocb completes. This
  8602. * function is called by the ring event handler with no lock held.
  8603. * This function frees the iocb.
  8604. **/
  8605. void
  8606. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8607. struct lpfc_iocbq *rspiocb)
  8608. {
  8609. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8610. "3096 ABORT_XRI_CN completing on xri x%x "
  8611. "original iotag x%x, abort cmd iotag x%x "
  8612. "status 0x%x, reason 0x%x\n",
  8613. cmdiocb->iocb.un.acxri.abortContextTag,
  8614. cmdiocb->iocb.un.acxri.abortIoTag,
  8615. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  8616. rspiocb->iocb.un.ulpWord[4]);
  8617. lpfc_sli_release_iocbq(phba, cmdiocb);
  8618. return;
  8619. }
  8620. /**
  8621. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8622. * @vport: Pointer to virtual port.
  8623. * @pring: Pointer to driver SLI ring object.
  8624. * @tgt_id: SCSI ID of the target.
  8625. * @lun_id: LUN ID of the scsi device.
  8626. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8627. *
  8628. * This function sends an abort command for every SCSI command
  8629. * associated with the given virtual port pending on the ring
  8630. * filtered by lpfc_sli_validate_fcp_iocb function.
  8631. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8632. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8633. * parameters
  8634. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8635. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8636. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8637. * FCP iocbs associated with virtual port.
  8638. * This function returns number of iocbs it failed to abort.
  8639. * This function is called with no locks held.
  8640. **/
  8641. int
  8642. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8643. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8644. {
  8645. struct lpfc_hba *phba = vport->phba;
  8646. struct lpfc_iocbq *iocbq;
  8647. struct lpfc_iocbq *abtsiocb;
  8648. IOCB_t *cmd = NULL;
  8649. int errcnt = 0, ret_val = 0;
  8650. int i;
  8651. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8652. iocbq = phba->sli.iocbq_lookup[i];
  8653. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8654. abort_cmd) != 0)
  8655. continue;
  8656. /* issue ABTS for this IOCB based on iotag */
  8657. abtsiocb = lpfc_sli_get_iocbq(phba);
  8658. if (abtsiocb == NULL) {
  8659. errcnt++;
  8660. continue;
  8661. }
  8662. cmd = &iocbq->iocb;
  8663. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8664. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8665. if (phba->sli_rev == LPFC_SLI_REV4)
  8666. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8667. else
  8668. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8669. abtsiocb->iocb.ulpLe = 1;
  8670. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8671. abtsiocb->vport = phba->pport;
  8672. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8673. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8674. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8675. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8676. if (lpfc_is_link_up(phba))
  8677. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8678. else
  8679. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8680. /* Setup callback routine and issue the command. */
  8681. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8682. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8683. abtsiocb, 0);
  8684. if (ret_val == IOCB_ERROR) {
  8685. lpfc_sli_release_iocbq(phba, abtsiocb);
  8686. errcnt++;
  8687. continue;
  8688. }
  8689. }
  8690. return errcnt;
  8691. }
  8692. /**
  8693. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8694. * @phba: Pointer to HBA context object.
  8695. * @cmdiocbq: Pointer to command iocb.
  8696. * @rspiocbq: Pointer to response iocb.
  8697. *
  8698. * This function is the completion handler for iocbs issued using
  8699. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8700. * ring event handler function without any lock held. This function
  8701. * can be called from both worker thread context and interrupt
  8702. * context. This function also can be called from other thread which
  8703. * cleans up the SLI layer objects.
  8704. * This function copy the contents of the response iocb to the
  8705. * response iocb memory object provided by the caller of
  8706. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8707. * sleeps for the iocb completion.
  8708. **/
  8709. static void
  8710. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8711. struct lpfc_iocbq *cmdiocbq,
  8712. struct lpfc_iocbq *rspiocbq)
  8713. {
  8714. wait_queue_head_t *pdone_q;
  8715. unsigned long iflags;
  8716. struct lpfc_scsi_buf *lpfc_cmd;
  8717. spin_lock_irqsave(&phba->hbalock, iflags);
  8718. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8719. if (cmdiocbq->context2 && rspiocbq)
  8720. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8721. &rspiocbq->iocb, sizeof(IOCB_t));
  8722. /* Set the exchange busy flag for task management commands */
  8723. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8724. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  8725. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  8726. cur_iocbq);
  8727. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  8728. }
  8729. pdone_q = cmdiocbq->context_un.wait_queue;
  8730. if (pdone_q)
  8731. wake_up(pdone_q);
  8732. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8733. return;
  8734. }
  8735. /**
  8736. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  8737. * @phba: Pointer to HBA context object..
  8738. * @piocbq: Pointer to command iocb.
  8739. * @flag: Flag to test.
  8740. *
  8741. * This routine grabs the hbalock and then test the iocb_flag to
  8742. * see if the passed in flag is set.
  8743. * Returns:
  8744. * 1 if flag is set.
  8745. * 0 if flag is not set.
  8746. **/
  8747. static int
  8748. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  8749. struct lpfc_iocbq *piocbq, uint32_t flag)
  8750. {
  8751. unsigned long iflags;
  8752. int ret;
  8753. spin_lock_irqsave(&phba->hbalock, iflags);
  8754. ret = piocbq->iocb_flag & flag;
  8755. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8756. return ret;
  8757. }
  8758. /**
  8759. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  8760. * @phba: Pointer to HBA context object..
  8761. * @pring: Pointer to sli ring.
  8762. * @piocb: Pointer to command iocb.
  8763. * @prspiocbq: Pointer to response iocb.
  8764. * @timeout: Timeout in number of seconds.
  8765. *
  8766. * This function issues the iocb to firmware and waits for the
  8767. * iocb to complete. If the iocb command is not
  8768. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  8769. * Caller should not free the iocb resources if this function
  8770. * returns IOCB_TIMEDOUT.
  8771. * The function waits for the iocb completion using an
  8772. * non-interruptible wait.
  8773. * This function will sleep while waiting for iocb completion.
  8774. * So, this function should not be called from any context which
  8775. * does not allow sleeping. Due to the same reason, this function
  8776. * cannot be called with interrupt disabled.
  8777. * This function assumes that the iocb completions occur while
  8778. * this function sleep. So, this function cannot be called from
  8779. * the thread which process iocb completion for this ring.
  8780. * This function clears the iocb_flag of the iocb object before
  8781. * issuing the iocb and the iocb completion handler sets this
  8782. * flag and wakes this thread when the iocb completes.
  8783. * The contents of the response iocb will be copied to prspiocbq
  8784. * by the completion handler when the command completes.
  8785. * This function returns IOCB_SUCCESS when success.
  8786. * This function is called with no lock held.
  8787. **/
  8788. int
  8789. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  8790. uint32_t ring_number,
  8791. struct lpfc_iocbq *piocb,
  8792. struct lpfc_iocbq *prspiocbq,
  8793. uint32_t timeout)
  8794. {
  8795. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8796. long timeleft, timeout_req = 0;
  8797. int retval = IOCB_SUCCESS;
  8798. uint32_t creg_val;
  8799. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  8800. /*
  8801. * If the caller has provided a response iocbq buffer, then context2
  8802. * is NULL or its an error.
  8803. */
  8804. if (prspiocbq) {
  8805. if (piocb->context2)
  8806. return IOCB_ERROR;
  8807. piocb->context2 = prspiocbq;
  8808. }
  8809. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  8810. piocb->context_un.wait_queue = &done_q;
  8811. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  8812. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8813. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8814. return IOCB_ERROR;
  8815. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  8816. writel(creg_val, phba->HCregaddr);
  8817. readl(phba->HCregaddr); /* flush */
  8818. }
  8819. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  8820. SLI_IOCB_RET_IOCB);
  8821. if (retval == IOCB_SUCCESS) {
  8822. timeout_req = timeout * HZ;
  8823. timeleft = wait_event_timeout(done_q,
  8824. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  8825. timeout_req);
  8826. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  8827. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8828. "0331 IOCB wake signaled\n");
  8829. } else if (timeleft == 0) {
  8830. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8831. "0338 IOCB wait timeout error - no "
  8832. "wake response Data x%x\n", timeout);
  8833. retval = IOCB_TIMEDOUT;
  8834. } else {
  8835. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8836. "0330 IOCB wake NOT set, "
  8837. "Data x%x x%lx\n",
  8838. timeout, (timeleft / jiffies));
  8839. retval = IOCB_TIMEDOUT;
  8840. }
  8841. } else if (retval == IOCB_BUSY) {
  8842. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8843. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  8844. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  8845. return retval;
  8846. } else {
  8847. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8848. "0332 IOCB wait issue failed, Data x%x\n",
  8849. retval);
  8850. retval = IOCB_ERROR;
  8851. }
  8852. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8853. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8854. return IOCB_ERROR;
  8855. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  8856. writel(creg_val, phba->HCregaddr);
  8857. readl(phba->HCregaddr); /* flush */
  8858. }
  8859. if (prspiocbq)
  8860. piocb->context2 = NULL;
  8861. piocb->context_un.wait_queue = NULL;
  8862. piocb->iocb_cmpl = NULL;
  8863. return retval;
  8864. }
  8865. /**
  8866. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  8867. * @phba: Pointer to HBA context object.
  8868. * @pmboxq: Pointer to driver mailbox object.
  8869. * @timeout: Timeout in number of seconds.
  8870. *
  8871. * This function issues the mailbox to firmware and waits for the
  8872. * mailbox command to complete. If the mailbox command is not
  8873. * completed within timeout seconds, it returns MBX_TIMEOUT.
  8874. * The function waits for the mailbox completion using an
  8875. * interruptible wait. If the thread is woken up due to a
  8876. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  8877. * should not free the mailbox resources, if this function returns
  8878. * MBX_TIMEOUT.
  8879. * This function will sleep while waiting for mailbox completion.
  8880. * So, this function should not be called from any context which
  8881. * does not allow sleeping. Due to the same reason, this function
  8882. * cannot be called with interrupt disabled.
  8883. * This function assumes that the mailbox completion occurs while
  8884. * this function sleep. So, this function cannot be called from
  8885. * the worker thread which processes mailbox completion.
  8886. * This function is called in the context of HBA management
  8887. * applications.
  8888. * This function returns MBX_SUCCESS when successful.
  8889. * This function is called with no lock held.
  8890. **/
  8891. int
  8892. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  8893. uint32_t timeout)
  8894. {
  8895. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8896. int retval;
  8897. unsigned long flag;
  8898. /* The caller must leave context1 empty. */
  8899. if (pmboxq->context1)
  8900. return MBX_NOT_FINISHED;
  8901. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  8902. /* setup wake call as IOCB callback */
  8903. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  8904. /* setup context field to pass wait_queue pointer to wake function */
  8905. pmboxq->context1 = &done_q;
  8906. /* now issue the command */
  8907. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  8908. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  8909. wait_event_interruptible_timeout(done_q,
  8910. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  8911. timeout * HZ);
  8912. spin_lock_irqsave(&phba->hbalock, flag);
  8913. pmboxq->context1 = NULL;
  8914. /*
  8915. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  8916. * else do not free the resources.
  8917. */
  8918. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  8919. retval = MBX_SUCCESS;
  8920. lpfc_sli4_swap_str(phba, pmboxq);
  8921. } else {
  8922. retval = MBX_TIMEOUT;
  8923. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  8924. }
  8925. spin_unlock_irqrestore(&phba->hbalock, flag);
  8926. }
  8927. return retval;
  8928. }
  8929. /**
  8930. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  8931. * @phba: Pointer to HBA context.
  8932. *
  8933. * This function is called to shutdown the driver's mailbox sub-system.
  8934. * It first marks the mailbox sub-system is in a block state to prevent
  8935. * the asynchronous mailbox command from issued off the pending mailbox
  8936. * command queue. If the mailbox command sub-system shutdown is due to
  8937. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  8938. * the mailbox sub-system flush routine to forcefully bring down the
  8939. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  8940. * as with offline or HBA function reset), this routine will wait for the
  8941. * outstanding mailbox command to complete before invoking the mailbox
  8942. * sub-system flush routine to gracefully bring down mailbox sub-system.
  8943. **/
  8944. void
  8945. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
  8946. {
  8947. struct lpfc_sli *psli = &phba->sli;
  8948. unsigned long timeout;
  8949. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  8950. spin_lock_irq(&phba->hbalock);
  8951. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  8952. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  8953. /* Determine how long we might wait for the active mailbox
  8954. * command to be gracefully completed by firmware.
  8955. */
  8956. if (phba->sli.mbox_active)
  8957. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  8958. phba->sli.mbox_active) *
  8959. 1000) + jiffies;
  8960. spin_unlock_irq(&phba->hbalock);
  8961. while (phba->sli.mbox_active) {
  8962. /* Check active mailbox complete status every 2ms */
  8963. msleep(2);
  8964. if (time_after(jiffies, timeout))
  8965. /* Timeout, let the mailbox flush routine to
  8966. * forcefully release active mailbox command
  8967. */
  8968. break;
  8969. }
  8970. } else
  8971. spin_unlock_irq(&phba->hbalock);
  8972. lpfc_sli_mbox_sys_flush(phba);
  8973. }
  8974. /**
  8975. * lpfc_sli_eratt_read - read sli-3 error attention events
  8976. * @phba: Pointer to HBA context.
  8977. *
  8978. * This function is called to read the SLI3 device error attention registers
  8979. * for possible error attention events. The caller must hold the hostlock
  8980. * with spin_lock_irq().
  8981. *
  8982. * This function returns 1 when there is Error Attention in the Host Attention
  8983. * Register and returns 0 otherwise.
  8984. **/
  8985. static int
  8986. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  8987. {
  8988. uint32_t ha_copy;
  8989. /* Read chip Host Attention (HA) register */
  8990. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  8991. goto unplug_err;
  8992. if (ha_copy & HA_ERATT) {
  8993. /* Read host status register to retrieve error event */
  8994. if (lpfc_sli_read_hs(phba))
  8995. goto unplug_err;
  8996. /* Check if there is a deferred error condition is active */
  8997. if ((HS_FFER1 & phba->work_hs) &&
  8998. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  8999. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9000. phba->hba_flag |= DEFER_ERATT;
  9001. /* Clear all interrupt enable conditions */
  9002. writel(0, phba->HCregaddr);
  9003. readl(phba->HCregaddr);
  9004. }
  9005. /* Set the driver HA work bitmap */
  9006. phba->work_ha |= HA_ERATT;
  9007. /* Indicate polling handles this ERATT */
  9008. phba->hba_flag |= HBA_ERATT_HANDLED;
  9009. return 1;
  9010. }
  9011. return 0;
  9012. unplug_err:
  9013. /* Set the driver HS work bitmap */
  9014. phba->work_hs |= UNPLUG_ERR;
  9015. /* Set the driver HA work bitmap */
  9016. phba->work_ha |= HA_ERATT;
  9017. /* Indicate polling handles this ERATT */
  9018. phba->hba_flag |= HBA_ERATT_HANDLED;
  9019. return 1;
  9020. }
  9021. /**
  9022. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9023. * @phba: Pointer to HBA context.
  9024. *
  9025. * This function is called to read the SLI4 device error attention registers
  9026. * for possible error attention events. The caller must hold the hostlock
  9027. * with spin_lock_irq().
  9028. *
  9029. * This function returns 1 when there is Error Attention in the Host Attention
  9030. * Register and returns 0 otherwise.
  9031. **/
  9032. static int
  9033. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9034. {
  9035. uint32_t uerr_sta_hi, uerr_sta_lo;
  9036. uint32_t if_type, portsmphr;
  9037. struct lpfc_register portstat_reg;
  9038. /*
  9039. * For now, use the SLI4 device internal unrecoverable error
  9040. * registers for error attention. This can be changed later.
  9041. */
  9042. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9043. switch (if_type) {
  9044. case LPFC_SLI_INTF_IF_TYPE_0:
  9045. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9046. &uerr_sta_lo) ||
  9047. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9048. &uerr_sta_hi)) {
  9049. phba->work_hs |= UNPLUG_ERR;
  9050. phba->work_ha |= HA_ERATT;
  9051. phba->hba_flag |= HBA_ERATT_HANDLED;
  9052. return 1;
  9053. }
  9054. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9055. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9056. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9057. "1423 HBA Unrecoverable error: "
  9058. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9059. "ue_mask_lo_reg=0x%x, "
  9060. "ue_mask_hi_reg=0x%x\n",
  9061. uerr_sta_lo, uerr_sta_hi,
  9062. phba->sli4_hba.ue_mask_lo,
  9063. phba->sli4_hba.ue_mask_hi);
  9064. phba->work_status[0] = uerr_sta_lo;
  9065. phba->work_status[1] = uerr_sta_hi;
  9066. phba->work_ha |= HA_ERATT;
  9067. phba->hba_flag |= HBA_ERATT_HANDLED;
  9068. return 1;
  9069. }
  9070. break;
  9071. case LPFC_SLI_INTF_IF_TYPE_2:
  9072. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9073. &portstat_reg.word0) ||
  9074. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9075. &portsmphr)){
  9076. phba->work_hs |= UNPLUG_ERR;
  9077. phba->work_ha |= HA_ERATT;
  9078. phba->hba_flag |= HBA_ERATT_HANDLED;
  9079. return 1;
  9080. }
  9081. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9082. phba->work_status[0] =
  9083. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9084. phba->work_status[1] =
  9085. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9086. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9087. "2885 Port Status Event: "
  9088. "port status reg 0x%x, "
  9089. "port smphr reg 0x%x, "
  9090. "error 1=0x%x, error 2=0x%x\n",
  9091. portstat_reg.word0,
  9092. portsmphr,
  9093. phba->work_status[0],
  9094. phba->work_status[1]);
  9095. phba->work_ha |= HA_ERATT;
  9096. phba->hba_flag |= HBA_ERATT_HANDLED;
  9097. return 1;
  9098. }
  9099. break;
  9100. case LPFC_SLI_INTF_IF_TYPE_1:
  9101. default:
  9102. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9103. "2886 HBA Error Attention on unsupported "
  9104. "if type %d.", if_type);
  9105. return 1;
  9106. }
  9107. return 0;
  9108. }
  9109. /**
  9110. * lpfc_sli_check_eratt - check error attention events
  9111. * @phba: Pointer to HBA context.
  9112. *
  9113. * This function is called from timer soft interrupt context to check HBA's
  9114. * error attention register bit for error attention events.
  9115. *
  9116. * This function returns 1 when there is Error Attention in the Host Attention
  9117. * Register and returns 0 otherwise.
  9118. **/
  9119. int
  9120. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9121. {
  9122. uint32_t ha_copy;
  9123. /* If somebody is waiting to handle an eratt, don't process it
  9124. * here. The brdkill function will do this.
  9125. */
  9126. if (phba->link_flag & LS_IGNORE_ERATT)
  9127. return 0;
  9128. /* Check if interrupt handler handles this ERATT */
  9129. spin_lock_irq(&phba->hbalock);
  9130. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9131. /* Interrupt handler has handled ERATT */
  9132. spin_unlock_irq(&phba->hbalock);
  9133. return 0;
  9134. }
  9135. /*
  9136. * If there is deferred error attention, do not check for error
  9137. * attention
  9138. */
  9139. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9140. spin_unlock_irq(&phba->hbalock);
  9141. return 0;
  9142. }
  9143. /* If PCI channel is offline, don't process it */
  9144. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9145. spin_unlock_irq(&phba->hbalock);
  9146. return 0;
  9147. }
  9148. switch (phba->sli_rev) {
  9149. case LPFC_SLI_REV2:
  9150. case LPFC_SLI_REV3:
  9151. /* Read chip Host Attention (HA) register */
  9152. ha_copy = lpfc_sli_eratt_read(phba);
  9153. break;
  9154. case LPFC_SLI_REV4:
  9155. /* Read device Uncoverable Error (UERR) registers */
  9156. ha_copy = lpfc_sli4_eratt_read(phba);
  9157. break;
  9158. default:
  9159. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9160. "0299 Invalid SLI revision (%d)\n",
  9161. phba->sli_rev);
  9162. ha_copy = 0;
  9163. break;
  9164. }
  9165. spin_unlock_irq(&phba->hbalock);
  9166. return ha_copy;
  9167. }
  9168. /**
  9169. * lpfc_intr_state_check - Check device state for interrupt handling
  9170. * @phba: Pointer to HBA context.
  9171. *
  9172. * This inline routine checks whether a device or its PCI slot is in a state
  9173. * that the interrupt should be handled.
  9174. *
  9175. * This function returns 0 if the device or the PCI slot is in a state that
  9176. * interrupt should be handled, otherwise -EIO.
  9177. */
  9178. static inline int
  9179. lpfc_intr_state_check(struct lpfc_hba *phba)
  9180. {
  9181. /* If the pci channel is offline, ignore all the interrupts */
  9182. if (unlikely(pci_channel_offline(phba->pcidev)))
  9183. return -EIO;
  9184. /* Update device level interrupt statistics */
  9185. phba->sli.slistat.sli_intr++;
  9186. /* Ignore all interrupts during initialization. */
  9187. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9188. return -EIO;
  9189. return 0;
  9190. }
  9191. /**
  9192. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9193. * @irq: Interrupt number.
  9194. * @dev_id: The device context pointer.
  9195. *
  9196. * This function is directly called from the PCI layer as an interrupt
  9197. * service routine when device with SLI-3 interface spec is enabled with
  9198. * MSI-X multi-message interrupt mode and there are slow-path events in
  9199. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9200. * interrupt mode, this function is called as part of the device-level
  9201. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9202. * is undergoing initialization, the interrupt handler will not process
  9203. * the interrupt. The link attention and ELS ring attention events are
  9204. * handled by the worker thread. The interrupt handler signals the worker
  9205. * thread and returns for these events. This function is called without
  9206. * any lock held. It gets the hbalock to access and update SLI data
  9207. * structures.
  9208. *
  9209. * This function returns IRQ_HANDLED when interrupt is handled else it
  9210. * returns IRQ_NONE.
  9211. **/
  9212. irqreturn_t
  9213. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9214. {
  9215. struct lpfc_hba *phba;
  9216. uint32_t ha_copy, hc_copy;
  9217. uint32_t work_ha_copy;
  9218. unsigned long status;
  9219. unsigned long iflag;
  9220. uint32_t control;
  9221. MAILBOX_t *mbox, *pmbox;
  9222. struct lpfc_vport *vport;
  9223. struct lpfc_nodelist *ndlp;
  9224. struct lpfc_dmabuf *mp;
  9225. LPFC_MBOXQ_t *pmb;
  9226. int rc;
  9227. /*
  9228. * Get the driver's phba structure from the dev_id and
  9229. * assume the HBA is not interrupting.
  9230. */
  9231. phba = (struct lpfc_hba *)dev_id;
  9232. if (unlikely(!phba))
  9233. return IRQ_NONE;
  9234. /*
  9235. * Stuff needs to be attented to when this function is invoked as an
  9236. * individual interrupt handler in MSI-X multi-message interrupt mode
  9237. */
  9238. if (phba->intr_type == MSIX) {
  9239. /* Check device state for handling interrupt */
  9240. if (lpfc_intr_state_check(phba))
  9241. return IRQ_NONE;
  9242. /* Need to read HA REG for slow-path events */
  9243. spin_lock_irqsave(&phba->hbalock, iflag);
  9244. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9245. goto unplug_error;
  9246. /* If somebody is waiting to handle an eratt don't process it
  9247. * here. The brdkill function will do this.
  9248. */
  9249. if (phba->link_flag & LS_IGNORE_ERATT)
  9250. ha_copy &= ~HA_ERATT;
  9251. /* Check the need for handling ERATT in interrupt handler */
  9252. if (ha_copy & HA_ERATT) {
  9253. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9254. /* ERATT polling has handled ERATT */
  9255. ha_copy &= ~HA_ERATT;
  9256. else
  9257. /* Indicate interrupt handler handles ERATT */
  9258. phba->hba_flag |= HBA_ERATT_HANDLED;
  9259. }
  9260. /*
  9261. * If there is deferred error attention, do not check for any
  9262. * interrupt.
  9263. */
  9264. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9265. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9266. return IRQ_NONE;
  9267. }
  9268. /* Clear up only attention source related to slow-path */
  9269. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9270. goto unplug_error;
  9271. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9272. HC_LAINT_ENA | HC_ERINT_ENA),
  9273. phba->HCregaddr);
  9274. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9275. phba->HAregaddr);
  9276. writel(hc_copy, phba->HCregaddr);
  9277. readl(phba->HAregaddr); /* flush */
  9278. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9279. } else
  9280. ha_copy = phba->ha_copy;
  9281. work_ha_copy = ha_copy & phba->work_ha_mask;
  9282. if (work_ha_copy) {
  9283. if (work_ha_copy & HA_LATT) {
  9284. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9285. /*
  9286. * Turn off Link Attention interrupts
  9287. * until CLEAR_LA done
  9288. */
  9289. spin_lock_irqsave(&phba->hbalock, iflag);
  9290. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9291. if (lpfc_readl(phba->HCregaddr, &control))
  9292. goto unplug_error;
  9293. control &= ~HC_LAINT_ENA;
  9294. writel(control, phba->HCregaddr);
  9295. readl(phba->HCregaddr); /* flush */
  9296. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9297. }
  9298. else
  9299. work_ha_copy &= ~HA_LATT;
  9300. }
  9301. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9302. /*
  9303. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9304. * the only slow ring.
  9305. */
  9306. status = (work_ha_copy &
  9307. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9308. status >>= (4*LPFC_ELS_RING);
  9309. if (status & HA_RXMASK) {
  9310. spin_lock_irqsave(&phba->hbalock, iflag);
  9311. if (lpfc_readl(phba->HCregaddr, &control))
  9312. goto unplug_error;
  9313. lpfc_debugfs_slow_ring_trc(phba,
  9314. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9315. control, status,
  9316. (uint32_t)phba->sli.slistat.sli_intr);
  9317. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9318. lpfc_debugfs_slow_ring_trc(phba,
  9319. "ISR Disable ring:"
  9320. "pwork:x%x hawork:x%x wait:x%x",
  9321. phba->work_ha, work_ha_copy,
  9322. (uint32_t)((unsigned long)
  9323. &phba->work_waitq));
  9324. control &=
  9325. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9326. writel(control, phba->HCregaddr);
  9327. readl(phba->HCregaddr); /* flush */
  9328. }
  9329. else {
  9330. lpfc_debugfs_slow_ring_trc(phba,
  9331. "ISR slow ring: pwork:"
  9332. "x%x hawork:x%x wait:x%x",
  9333. phba->work_ha, work_ha_copy,
  9334. (uint32_t)((unsigned long)
  9335. &phba->work_waitq));
  9336. }
  9337. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9338. }
  9339. }
  9340. spin_lock_irqsave(&phba->hbalock, iflag);
  9341. if (work_ha_copy & HA_ERATT) {
  9342. if (lpfc_sli_read_hs(phba))
  9343. goto unplug_error;
  9344. /*
  9345. * Check if there is a deferred error condition
  9346. * is active
  9347. */
  9348. if ((HS_FFER1 & phba->work_hs) &&
  9349. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9350. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9351. phba->work_hs)) {
  9352. phba->hba_flag |= DEFER_ERATT;
  9353. /* Clear all interrupt enable conditions */
  9354. writel(0, phba->HCregaddr);
  9355. readl(phba->HCregaddr);
  9356. }
  9357. }
  9358. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9359. pmb = phba->sli.mbox_active;
  9360. pmbox = &pmb->u.mb;
  9361. mbox = phba->mbox;
  9362. vport = pmb->vport;
  9363. /* First check out the status word */
  9364. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9365. if (pmbox->mbxOwner != OWN_HOST) {
  9366. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9367. /*
  9368. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9369. * mbxStatus <status>
  9370. */
  9371. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9372. LOG_SLI,
  9373. "(%d):0304 Stray Mailbox "
  9374. "Interrupt mbxCommand x%x "
  9375. "mbxStatus x%x\n",
  9376. (vport ? vport->vpi : 0),
  9377. pmbox->mbxCommand,
  9378. pmbox->mbxStatus);
  9379. /* clear mailbox attention bit */
  9380. work_ha_copy &= ~HA_MBATT;
  9381. } else {
  9382. phba->sli.mbox_active = NULL;
  9383. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9384. phba->last_completion_time = jiffies;
  9385. del_timer(&phba->sli.mbox_tmo);
  9386. if (pmb->mbox_cmpl) {
  9387. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9388. MAILBOX_CMD_SIZE);
  9389. if (pmb->out_ext_byte_len &&
  9390. pmb->context2)
  9391. lpfc_sli_pcimem_bcopy(
  9392. phba->mbox_ext,
  9393. pmb->context2,
  9394. pmb->out_ext_byte_len);
  9395. }
  9396. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9397. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9398. lpfc_debugfs_disc_trc(vport,
  9399. LPFC_DISC_TRC_MBOX_VPORT,
  9400. "MBOX dflt rpi: : "
  9401. "status:x%x rpi:x%x",
  9402. (uint32_t)pmbox->mbxStatus,
  9403. pmbox->un.varWords[0], 0);
  9404. if (!pmbox->mbxStatus) {
  9405. mp = (struct lpfc_dmabuf *)
  9406. (pmb->context1);
  9407. ndlp = (struct lpfc_nodelist *)
  9408. pmb->context2;
  9409. /* Reg_LOGIN of dflt RPI was
  9410. * successful. new lets get
  9411. * rid of the RPI using the
  9412. * same mbox buffer.
  9413. */
  9414. lpfc_unreg_login(phba,
  9415. vport->vpi,
  9416. pmbox->un.varWords[0],
  9417. pmb);
  9418. pmb->mbox_cmpl =
  9419. lpfc_mbx_cmpl_dflt_rpi;
  9420. pmb->context1 = mp;
  9421. pmb->context2 = ndlp;
  9422. pmb->vport = vport;
  9423. rc = lpfc_sli_issue_mbox(phba,
  9424. pmb,
  9425. MBX_NOWAIT);
  9426. if (rc != MBX_BUSY)
  9427. lpfc_printf_log(phba,
  9428. KERN_ERR,
  9429. LOG_MBOX | LOG_SLI,
  9430. "0350 rc should have"
  9431. "been MBX_BUSY\n");
  9432. if (rc != MBX_NOT_FINISHED)
  9433. goto send_current_mbox;
  9434. }
  9435. }
  9436. spin_lock_irqsave(
  9437. &phba->pport->work_port_lock,
  9438. iflag);
  9439. phba->pport->work_port_events &=
  9440. ~WORKER_MBOX_TMO;
  9441. spin_unlock_irqrestore(
  9442. &phba->pport->work_port_lock,
  9443. iflag);
  9444. lpfc_mbox_cmpl_put(phba, pmb);
  9445. }
  9446. } else
  9447. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9448. if ((work_ha_copy & HA_MBATT) &&
  9449. (phba->sli.mbox_active == NULL)) {
  9450. send_current_mbox:
  9451. /* Process next mailbox command if there is one */
  9452. do {
  9453. rc = lpfc_sli_issue_mbox(phba, NULL,
  9454. MBX_NOWAIT);
  9455. } while (rc == MBX_NOT_FINISHED);
  9456. if (rc != MBX_SUCCESS)
  9457. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9458. LOG_SLI, "0349 rc should be "
  9459. "MBX_SUCCESS\n");
  9460. }
  9461. spin_lock_irqsave(&phba->hbalock, iflag);
  9462. phba->work_ha |= work_ha_copy;
  9463. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9464. lpfc_worker_wake_up(phba);
  9465. }
  9466. return IRQ_HANDLED;
  9467. unplug_error:
  9468. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9469. return IRQ_HANDLED;
  9470. } /* lpfc_sli_sp_intr_handler */
  9471. /**
  9472. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9473. * @irq: Interrupt number.
  9474. * @dev_id: The device context pointer.
  9475. *
  9476. * This function is directly called from the PCI layer as an interrupt
  9477. * service routine when device with SLI-3 interface spec is enabled with
  9478. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9479. * ring event in the HBA. However, when the device is enabled with either
  9480. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9481. * device-level interrupt handler. When the PCI slot is in error recovery
  9482. * or the HBA is undergoing initialization, the interrupt handler will not
  9483. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9484. * the intrrupt context. This function is called without any lock held.
  9485. * It gets the hbalock to access and update SLI data structures.
  9486. *
  9487. * This function returns IRQ_HANDLED when interrupt is handled else it
  9488. * returns IRQ_NONE.
  9489. **/
  9490. irqreturn_t
  9491. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9492. {
  9493. struct lpfc_hba *phba;
  9494. uint32_t ha_copy;
  9495. unsigned long status;
  9496. unsigned long iflag;
  9497. /* Get the driver's phba structure from the dev_id and
  9498. * assume the HBA is not interrupting.
  9499. */
  9500. phba = (struct lpfc_hba *) dev_id;
  9501. if (unlikely(!phba))
  9502. return IRQ_NONE;
  9503. /*
  9504. * Stuff needs to be attented to when this function is invoked as an
  9505. * individual interrupt handler in MSI-X multi-message interrupt mode
  9506. */
  9507. if (phba->intr_type == MSIX) {
  9508. /* Check device state for handling interrupt */
  9509. if (lpfc_intr_state_check(phba))
  9510. return IRQ_NONE;
  9511. /* Need to read HA REG for FCP ring and other ring events */
  9512. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9513. return IRQ_HANDLED;
  9514. /* Clear up only attention source related to fast-path */
  9515. spin_lock_irqsave(&phba->hbalock, iflag);
  9516. /*
  9517. * If there is deferred error attention, do not check for
  9518. * any interrupt.
  9519. */
  9520. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9521. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9522. return IRQ_NONE;
  9523. }
  9524. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9525. phba->HAregaddr);
  9526. readl(phba->HAregaddr); /* flush */
  9527. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9528. } else
  9529. ha_copy = phba->ha_copy;
  9530. /*
  9531. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9532. */
  9533. ha_copy &= ~(phba->work_ha_mask);
  9534. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9535. status >>= (4*LPFC_FCP_RING);
  9536. if (status & HA_RXMASK)
  9537. lpfc_sli_handle_fast_ring_event(phba,
  9538. &phba->sli.ring[LPFC_FCP_RING],
  9539. status);
  9540. if (phba->cfg_multi_ring_support == 2) {
  9541. /*
  9542. * Process all events on extra ring. Take the optimized path
  9543. * for extra ring IO.
  9544. */
  9545. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9546. status >>= (4*LPFC_EXTRA_RING);
  9547. if (status & HA_RXMASK) {
  9548. lpfc_sli_handle_fast_ring_event(phba,
  9549. &phba->sli.ring[LPFC_EXTRA_RING],
  9550. status);
  9551. }
  9552. }
  9553. return IRQ_HANDLED;
  9554. } /* lpfc_sli_fp_intr_handler */
  9555. /**
  9556. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9557. * @irq: Interrupt number.
  9558. * @dev_id: The device context pointer.
  9559. *
  9560. * This function is the HBA device-level interrupt handler to device with
  9561. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9562. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9563. * requires driver attention. This function invokes the slow-path interrupt
  9564. * attention handling function and fast-path interrupt attention handling
  9565. * function in turn to process the relevant HBA attention events. This
  9566. * function is called without any lock held. It gets the hbalock to access
  9567. * and update SLI data structures.
  9568. *
  9569. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9570. * returns IRQ_NONE.
  9571. **/
  9572. irqreturn_t
  9573. lpfc_sli_intr_handler(int irq, void *dev_id)
  9574. {
  9575. struct lpfc_hba *phba;
  9576. irqreturn_t sp_irq_rc, fp_irq_rc;
  9577. unsigned long status1, status2;
  9578. uint32_t hc_copy;
  9579. /*
  9580. * Get the driver's phba structure from the dev_id and
  9581. * assume the HBA is not interrupting.
  9582. */
  9583. phba = (struct lpfc_hba *) dev_id;
  9584. if (unlikely(!phba))
  9585. return IRQ_NONE;
  9586. /* Check device state for handling interrupt */
  9587. if (lpfc_intr_state_check(phba))
  9588. return IRQ_NONE;
  9589. spin_lock(&phba->hbalock);
  9590. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9591. spin_unlock(&phba->hbalock);
  9592. return IRQ_HANDLED;
  9593. }
  9594. if (unlikely(!phba->ha_copy)) {
  9595. spin_unlock(&phba->hbalock);
  9596. return IRQ_NONE;
  9597. } else if (phba->ha_copy & HA_ERATT) {
  9598. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9599. /* ERATT polling has handled ERATT */
  9600. phba->ha_copy &= ~HA_ERATT;
  9601. else
  9602. /* Indicate interrupt handler handles ERATT */
  9603. phba->hba_flag |= HBA_ERATT_HANDLED;
  9604. }
  9605. /*
  9606. * If there is deferred error attention, do not check for any interrupt.
  9607. */
  9608. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9609. spin_unlock(&phba->hbalock);
  9610. return IRQ_NONE;
  9611. }
  9612. /* Clear attention sources except link and error attentions */
  9613. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9614. spin_unlock(&phba->hbalock);
  9615. return IRQ_HANDLED;
  9616. }
  9617. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9618. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9619. phba->HCregaddr);
  9620. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9621. writel(hc_copy, phba->HCregaddr);
  9622. readl(phba->HAregaddr); /* flush */
  9623. spin_unlock(&phba->hbalock);
  9624. /*
  9625. * Invokes slow-path host attention interrupt handling as appropriate.
  9626. */
  9627. /* status of events with mailbox and link attention */
  9628. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9629. /* status of events with ELS ring */
  9630. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9631. status2 >>= (4*LPFC_ELS_RING);
  9632. if (status1 || (status2 & HA_RXMASK))
  9633. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9634. else
  9635. sp_irq_rc = IRQ_NONE;
  9636. /*
  9637. * Invoke fast-path host attention interrupt handling as appropriate.
  9638. */
  9639. /* status of events with FCP ring */
  9640. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9641. status1 >>= (4*LPFC_FCP_RING);
  9642. /* status of events with extra ring */
  9643. if (phba->cfg_multi_ring_support == 2) {
  9644. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9645. status2 >>= (4*LPFC_EXTRA_RING);
  9646. } else
  9647. status2 = 0;
  9648. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9649. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9650. else
  9651. fp_irq_rc = IRQ_NONE;
  9652. /* Return device-level interrupt handling status */
  9653. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9654. } /* lpfc_sli_intr_handler */
  9655. /**
  9656. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9657. * @phba: pointer to lpfc hba data structure.
  9658. *
  9659. * This routine is invoked by the worker thread to process all the pending
  9660. * SLI4 FCP abort XRI events.
  9661. **/
  9662. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9663. {
  9664. struct lpfc_cq_event *cq_event;
  9665. /* First, declare the fcp xri abort event has been handled */
  9666. spin_lock_irq(&phba->hbalock);
  9667. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9668. spin_unlock_irq(&phba->hbalock);
  9669. /* Now, handle all the fcp xri abort events */
  9670. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9671. /* Get the first event from the head of the event queue */
  9672. spin_lock_irq(&phba->hbalock);
  9673. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9674. cq_event, struct lpfc_cq_event, list);
  9675. spin_unlock_irq(&phba->hbalock);
  9676. /* Notify aborted XRI for FCP work queue */
  9677. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9678. /* Free the event processed back to the free pool */
  9679. lpfc_sli4_cq_event_release(phba, cq_event);
  9680. }
  9681. }
  9682. /**
  9683. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9684. * @phba: pointer to lpfc hba data structure.
  9685. *
  9686. * This routine is invoked by the worker thread to process all the pending
  9687. * SLI4 els abort xri events.
  9688. **/
  9689. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9690. {
  9691. struct lpfc_cq_event *cq_event;
  9692. /* First, declare the els xri abort event has been handled */
  9693. spin_lock_irq(&phba->hbalock);
  9694. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9695. spin_unlock_irq(&phba->hbalock);
  9696. /* Now, handle all the els xri abort events */
  9697. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9698. /* Get the first event from the head of the event queue */
  9699. spin_lock_irq(&phba->hbalock);
  9700. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9701. cq_event, struct lpfc_cq_event, list);
  9702. spin_unlock_irq(&phba->hbalock);
  9703. /* Notify aborted XRI for ELS work queue */
  9704. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9705. /* Free the event processed back to the free pool */
  9706. lpfc_sli4_cq_event_release(phba, cq_event);
  9707. }
  9708. }
  9709. /**
  9710. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9711. * @phba: pointer to lpfc hba data structure
  9712. * @pIocbIn: pointer to the rspiocbq
  9713. * @pIocbOut: pointer to the cmdiocbq
  9714. * @wcqe: pointer to the complete wcqe
  9715. *
  9716. * This routine transfers the fields of a command iocbq to a response iocbq
  9717. * by copying all the IOCB fields from command iocbq and transferring the
  9718. * completion status information from the complete wcqe.
  9719. **/
  9720. static void
  9721. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  9722. struct lpfc_iocbq *pIocbIn,
  9723. struct lpfc_iocbq *pIocbOut,
  9724. struct lpfc_wcqe_complete *wcqe)
  9725. {
  9726. unsigned long iflags;
  9727. uint32_t status;
  9728. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  9729. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  9730. sizeof(struct lpfc_iocbq) - offset);
  9731. /* Map WCQE parameters into irspiocb parameters */
  9732. status = bf_get(lpfc_wcqe_c_status, wcqe);
  9733. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  9734. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  9735. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  9736. pIocbIn->iocb.un.fcpi.fcpi_parm =
  9737. pIocbOut->iocb.un.fcpi.fcpi_parm -
  9738. wcqe->total_data_placed;
  9739. else
  9740. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9741. else {
  9742. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9743. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  9744. }
  9745. /* Convert BG errors for completion status */
  9746. if (status == CQE_STATUS_DI_ERROR) {
  9747. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  9748. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  9749. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  9750. else
  9751. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  9752. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  9753. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  9754. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9755. BGS_GUARD_ERR_MASK;
  9756. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  9757. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9758. BGS_APPTAG_ERR_MASK;
  9759. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  9760. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9761. BGS_REFTAG_ERR_MASK;
  9762. /* Check to see if there was any good data before the error */
  9763. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  9764. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9765. BGS_HI_WATER_MARK_PRESENT_MASK;
  9766. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  9767. wcqe->total_data_placed;
  9768. }
  9769. /*
  9770. * Set ALL the error bits to indicate we don't know what
  9771. * type of error it is.
  9772. */
  9773. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  9774. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9775. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  9776. BGS_GUARD_ERR_MASK);
  9777. }
  9778. /* Pick up HBA exchange busy condition */
  9779. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  9780. spin_lock_irqsave(&phba->hbalock, iflags);
  9781. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  9782. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9783. }
  9784. }
  9785. /**
  9786. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  9787. * @phba: Pointer to HBA context object.
  9788. * @wcqe: Pointer to work-queue completion queue entry.
  9789. *
  9790. * This routine handles an ELS work-queue completion event and construct
  9791. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  9792. * discovery engine to handle.
  9793. *
  9794. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  9795. **/
  9796. static struct lpfc_iocbq *
  9797. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  9798. struct lpfc_iocbq *irspiocbq)
  9799. {
  9800. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9801. struct lpfc_iocbq *cmdiocbq;
  9802. struct lpfc_wcqe_complete *wcqe;
  9803. unsigned long iflags;
  9804. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  9805. spin_lock_irqsave(&phba->hbalock, iflags);
  9806. pring->stats.iocb_event++;
  9807. /* Look up the ELS command IOCB and create pseudo response IOCB */
  9808. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  9809. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9810. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9811. if (unlikely(!cmdiocbq)) {
  9812. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9813. "0386 ELS complete with no corresponding "
  9814. "cmdiocb: iotag (%d)\n",
  9815. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9816. lpfc_sli_release_iocbq(phba, irspiocbq);
  9817. return NULL;
  9818. }
  9819. /* Fake the irspiocbq and copy necessary response information */
  9820. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  9821. return irspiocbq;
  9822. }
  9823. /**
  9824. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  9825. * @phba: Pointer to HBA context object.
  9826. * @cqe: Pointer to mailbox completion queue entry.
  9827. *
  9828. * This routine process a mailbox completion queue entry with asynchrous
  9829. * event.
  9830. *
  9831. * Return: true if work posted to worker thread, otherwise false.
  9832. **/
  9833. static bool
  9834. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9835. {
  9836. struct lpfc_cq_event *cq_event;
  9837. unsigned long iflags;
  9838. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9839. "0392 Async Event: word0:x%x, word1:x%x, "
  9840. "word2:x%x, word3:x%x\n", mcqe->word0,
  9841. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  9842. /* Allocate a new internal CQ_EVENT entry */
  9843. cq_event = lpfc_sli4_cq_event_alloc(phba);
  9844. if (!cq_event) {
  9845. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9846. "0394 Failed to allocate CQ_EVENT entry\n");
  9847. return false;
  9848. }
  9849. /* Move the CQE into an asynchronous event entry */
  9850. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  9851. spin_lock_irqsave(&phba->hbalock, iflags);
  9852. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  9853. /* Set the async event flag */
  9854. phba->hba_flag |= ASYNC_EVENT;
  9855. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9856. return true;
  9857. }
  9858. /**
  9859. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  9860. * @phba: Pointer to HBA context object.
  9861. * @cqe: Pointer to mailbox completion queue entry.
  9862. *
  9863. * This routine process a mailbox completion queue entry with mailbox
  9864. * completion event.
  9865. *
  9866. * Return: true if work posted to worker thread, otherwise false.
  9867. **/
  9868. static bool
  9869. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9870. {
  9871. uint32_t mcqe_status;
  9872. MAILBOX_t *mbox, *pmbox;
  9873. struct lpfc_mqe *mqe;
  9874. struct lpfc_vport *vport;
  9875. struct lpfc_nodelist *ndlp;
  9876. struct lpfc_dmabuf *mp;
  9877. unsigned long iflags;
  9878. LPFC_MBOXQ_t *pmb;
  9879. bool workposted = false;
  9880. int rc;
  9881. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  9882. if (!bf_get(lpfc_trailer_completed, mcqe))
  9883. goto out_no_mqe_complete;
  9884. /* Get the reference to the active mbox command */
  9885. spin_lock_irqsave(&phba->hbalock, iflags);
  9886. pmb = phba->sli.mbox_active;
  9887. if (unlikely(!pmb)) {
  9888. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  9889. "1832 No pending MBOX command to handle\n");
  9890. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9891. goto out_no_mqe_complete;
  9892. }
  9893. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9894. mqe = &pmb->u.mqe;
  9895. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  9896. mbox = phba->mbox;
  9897. vport = pmb->vport;
  9898. /* Reset heartbeat timer */
  9899. phba->last_completion_time = jiffies;
  9900. del_timer(&phba->sli.mbox_tmo);
  9901. /* Move mbox data to caller's mailbox region, do endian swapping */
  9902. if (pmb->mbox_cmpl && mbox)
  9903. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  9904. /*
  9905. * For mcqe errors, conditionally move a modified error code to
  9906. * the mbox so that the error will not be missed.
  9907. */
  9908. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  9909. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  9910. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  9911. bf_set(lpfc_mqe_status, mqe,
  9912. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  9913. }
  9914. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9915. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9916. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  9917. "MBOX dflt rpi: status:x%x rpi:x%x",
  9918. mcqe_status,
  9919. pmbox->un.varWords[0], 0);
  9920. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  9921. mp = (struct lpfc_dmabuf *)(pmb->context1);
  9922. ndlp = (struct lpfc_nodelist *)pmb->context2;
  9923. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  9924. * RID of the PPI using the same mbox buffer.
  9925. */
  9926. lpfc_unreg_login(phba, vport->vpi,
  9927. pmbox->un.varWords[0], pmb);
  9928. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  9929. pmb->context1 = mp;
  9930. pmb->context2 = ndlp;
  9931. pmb->vport = vport;
  9932. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  9933. if (rc != MBX_BUSY)
  9934. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9935. LOG_SLI, "0385 rc should "
  9936. "have been MBX_BUSY\n");
  9937. if (rc != MBX_NOT_FINISHED)
  9938. goto send_current_mbox;
  9939. }
  9940. }
  9941. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  9942. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  9943. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  9944. /* There is mailbox completion work to do */
  9945. spin_lock_irqsave(&phba->hbalock, iflags);
  9946. __lpfc_mbox_cmpl_put(phba, pmb);
  9947. phba->work_ha |= HA_MBATT;
  9948. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9949. workposted = true;
  9950. send_current_mbox:
  9951. spin_lock_irqsave(&phba->hbalock, iflags);
  9952. /* Release the mailbox command posting token */
  9953. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  9954. /* Setting active mailbox pointer need to be in sync to flag clear */
  9955. phba->sli.mbox_active = NULL;
  9956. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9957. /* Wake up worker thread to post the next pending mailbox command */
  9958. lpfc_worker_wake_up(phba);
  9959. out_no_mqe_complete:
  9960. if (bf_get(lpfc_trailer_consumed, mcqe))
  9961. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  9962. return workposted;
  9963. }
  9964. /**
  9965. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  9966. * @phba: Pointer to HBA context object.
  9967. * @cqe: Pointer to mailbox completion queue entry.
  9968. *
  9969. * This routine process a mailbox completion queue entry, it invokes the
  9970. * proper mailbox complete handling or asynchrous event handling routine
  9971. * according to the MCQE's async bit.
  9972. *
  9973. * Return: true if work posted to worker thread, otherwise false.
  9974. **/
  9975. static bool
  9976. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  9977. {
  9978. struct lpfc_mcqe mcqe;
  9979. bool workposted;
  9980. /* Copy the mailbox MCQE and convert endian order as needed */
  9981. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  9982. /* Invoke the proper event handling routine */
  9983. if (!bf_get(lpfc_trailer_async, &mcqe))
  9984. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  9985. else
  9986. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  9987. return workposted;
  9988. }
  9989. /**
  9990. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  9991. * @phba: Pointer to HBA context object.
  9992. * @wcqe: Pointer to work-queue completion queue entry.
  9993. *
  9994. * This routine handles an ELS work-queue completion event.
  9995. *
  9996. * Return: true if work posted to worker thread, otherwise false.
  9997. **/
  9998. static bool
  9999. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
  10000. struct lpfc_wcqe_complete *wcqe)
  10001. {
  10002. struct lpfc_iocbq *irspiocbq;
  10003. unsigned long iflags;
  10004. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10005. /* Get an irspiocbq for later ELS response processing use */
  10006. irspiocbq = lpfc_sli_get_iocbq(phba);
  10007. if (!irspiocbq) {
  10008. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10009. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10010. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10011. pring->txq_cnt, phba->iocb_cnt,
  10012. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  10013. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  10014. return false;
  10015. }
  10016. /* Save off the slow-path queue event for work thread to process */
  10017. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10018. spin_lock_irqsave(&phba->hbalock, iflags);
  10019. list_add_tail(&irspiocbq->cq_event.list,
  10020. &phba->sli4_hba.sp_queue_event);
  10021. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10022. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10023. return true;
  10024. }
  10025. /**
  10026. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10027. * @phba: Pointer to HBA context object.
  10028. * @wcqe: Pointer to work-queue completion queue entry.
  10029. *
  10030. * This routine handles slow-path WQ entry comsumed event by invoking the
  10031. * proper WQ release routine to the slow-path WQ.
  10032. **/
  10033. static void
  10034. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10035. struct lpfc_wcqe_release *wcqe)
  10036. {
  10037. /* sanity check on queue memory */
  10038. if (unlikely(!phba->sli4_hba.els_wq))
  10039. return;
  10040. /* Check for the slow-path ELS work queue */
  10041. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10042. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10043. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10044. else
  10045. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10046. "2579 Slow-path wqe consume event carries "
  10047. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10048. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10049. phba->sli4_hba.els_wq->queue_id);
  10050. }
  10051. /**
  10052. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10053. * @phba: Pointer to HBA context object.
  10054. * @cq: Pointer to a WQ completion queue.
  10055. * @wcqe: Pointer to work-queue completion queue entry.
  10056. *
  10057. * This routine handles an XRI abort event.
  10058. *
  10059. * Return: true if work posted to worker thread, otherwise false.
  10060. **/
  10061. static bool
  10062. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10063. struct lpfc_queue *cq,
  10064. struct sli4_wcqe_xri_aborted *wcqe)
  10065. {
  10066. bool workposted = false;
  10067. struct lpfc_cq_event *cq_event;
  10068. unsigned long iflags;
  10069. /* Allocate a new internal CQ_EVENT entry */
  10070. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10071. if (!cq_event) {
  10072. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10073. "0602 Failed to allocate CQ_EVENT entry\n");
  10074. return false;
  10075. }
  10076. /* Move the CQE into the proper xri abort event list */
  10077. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10078. switch (cq->subtype) {
  10079. case LPFC_FCP:
  10080. spin_lock_irqsave(&phba->hbalock, iflags);
  10081. list_add_tail(&cq_event->list,
  10082. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10083. /* Set the fcp xri abort event flag */
  10084. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10085. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10086. workposted = true;
  10087. break;
  10088. case LPFC_ELS:
  10089. spin_lock_irqsave(&phba->hbalock, iflags);
  10090. list_add_tail(&cq_event->list,
  10091. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10092. /* Set the els xri abort event flag */
  10093. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10094. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10095. workposted = true;
  10096. break;
  10097. default:
  10098. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10099. "0603 Invalid work queue CQE subtype (x%x)\n",
  10100. cq->subtype);
  10101. workposted = false;
  10102. break;
  10103. }
  10104. return workposted;
  10105. }
  10106. /**
  10107. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10108. * @phba: Pointer to HBA context object.
  10109. * @rcqe: Pointer to receive-queue completion queue entry.
  10110. *
  10111. * This routine process a receive-queue completion queue entry.
  10112. *
  10113. * Return: true if work posted to worker thread, otherwise false.
  10114. **/
  10115. static bool
  10116. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10117. {
  10118. bool workposted = false;
  10119. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10120. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10121. struct hbq_dmabuf *dma_buf;
  10122. uint32_t status, rq_id;
  10123. unsigned long iflags;
  10124. /* sanity check on queue memory */
  10125. if (unlikely(!hrq) || unlikely(!drq))
  10126. return workposted;
  10127. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10128. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10129. else
  10130. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10131. if (rq_id != hrq->queue_id)
  10132. goto out;
  10133. status = bf_get(lpfc_rcqe_status, rcqe);
  10134. switch (status) {
  10135. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10136. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10137. "2537 Receive Frame Truncated!!\n");
  10138. case FC_STATUS_RQ_SUCCESS:
  10139. lpfc_sli4_rq_release(hrq, drq);
  10140. spin_lock_irqsave(&phba->hbalock, iflags);
  10141. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10142. if (!dma_buf) {
  10143. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10144. goto out;
  10145. }
  10146. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10147. /* save off the frame for the word thread to process */
  10148. list_add_tail(&dma_buf->cq_event.list,
  10149. &phba->sli4_hba.sp_queue_event);
  10150. /* Frame received */
  10151. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10152. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10153. workposted = true;
  10154. break;
  10155. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10156. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10157. /* Post more buffers if possible */
  10158. spin_lock_irqsave(&phba->hbalock, iflags);
  10159. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10160. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10161. workposted = true;
  10162. break;
  10163. }
  10164. out:
  10165. return workposted;
  10166. }
  10167. /**
  10168. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10169. * @phba: Pointer to HBA context object.
  10170. * @cq: Pointer to the completion queue.
  10171. * @wcqe: Pointer to a completion queue entry.
  10172. *
  10173. * This routine process a slow-path work-queue or receive queue completion queue
  10174. * entry.
  10175. *
  10176. * Return: true if work posted to worker thread, otherwise false.
  10177. **/
  10178. static bool
  10179. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10180. struct lpfc_cqe *cqe)
  10181. {
  10182. struct lpfc_cqe cqevt;
  10183. bool workposted = false;
  10184. /* Copy the work queue CQE and convert endian order if needed */
  10185. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10186. /* Check and process for different type of WCQE and dispatch */
  10187. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10188. case CQE_CODE_COMPL_WQE:
  10189. /* Process the WQ/RQ complete event */
  10190. phba->last_completion_time = jiffies;
  10191. workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
  10192. (struct lpfc_wcqe_complete *)&cqevt);
  10193. break;
  10194. case CQE_CODE_RELEASE_WQE:
  10195. /* Process the WQ release event */
  10196. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10197. (struct lpfc_wcqe_release *)&cqevt);
  10198. break;
  10199. case CQE_CODE_XRI_ABORTED:
  10200. /* Process the WQ XRI abort event */
  10201. phba->last_completion_time = jiffies;
  10202. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10203. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10204. break;
  10205. case CQE_CODE_RECEIVE:
  10206. case CQE_CODE_RECEIVE_V1:
  10207. /* Process the RQ event */
  10208. phba->last_completion_time = jiffies;
  10209. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10210. (struct lpfc_rcqe *)&cqevt);
  10211. break;
  10212. default:
  10213. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10214. "0388 Not a valid WCQE code: x%x\n",
  10215. bf_get(lpfc_cqe_code, &cqevt));
  10216. break;
  10217. }
  10218. return workposted;
  10219. }
  10220. /**
  10221. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10222. * @phba: Pointer to HBA context object.
  10223. * @eqe: Pointer to fast-path event queue entry.
  10224. *
  10225. * This routine process a event queue entry from the slow-path event queue.
  10226. * It will check the MajorCode and MinorCode to determine this is for a
  10227. * completion event on a completion queue, if not, an error shall be logged
  10228. * and just return. Otherwise, it will get to the corresponding completion
  10229. * queue and process all the entries on that completion queue, rearm the
  10230. * completion queue, and then return.
  10231. *
  10232. **/
  10233. static void
  10234. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  10235. {
  10236. struct lpfc_queue *cq = NULL, *childq, *speq;
  10237. struct lpfc_cqe *cqe;
  10238. bool workposted = false;
  10239. int ecount = 0;
  10240. uint16_t cqid;
  10241. if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
  10242. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10243. "0359 Not a valid slow-path completion "
  10244. "event: majorcode=x%x, minorcode=x%x\n",
  10245. bf_get_le32(lpfc_eqe_major_code, eqe),
  10246. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10247. return;
  10248. }
  10249. /* Get the reference to the corresponding CQ */
  10250. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10251. /* Search for completion queue pointer matching this cqid */
  10252. speq = phba->sli4_hba.sp_eq;
  10253. /* sanity check on queue memory */
  10254. if (unlikely(!speq))
  10255. return;
  10256. list_for_each_entry(childq, &speq->child_list, list) {
  10257. if (childq->queue_id == cqid) {
  10258. cq = childq;
  10259. break;
  10260. }
  10261. }
  10262. if (unlikely(!cq)) {
  10263. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10264. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10265. "0365 Slow-path CQ identifier "
  10266. "(%d) does not exist\n", cqid);
  10267. return;
  10268. }
  10269. /* Process all the entries to the CQ */
  10270. switch (cq->type) {
  10271. case LPFC_MCQ:
  10272. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10273. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10274. if (!(++ecount % cq->entry_repost))
  10275. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10276. }
  10277. break;
  10278. case LPFC_WCQ:
  10279. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10280. if (cq->subtype == LPFC_FCP)
  10281. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10282. cqe);
  10283. else
  10284. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10285. cqe);
  10286. if (!(++ecount % cq->entry_repost))
  10287. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10288. }
  10289. break;
  10290. default:
  10291. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10292. "0370 Invalid completion queue type (%d)\n",
  10293. cq->type);
  10294. return;
  10295. }
  10296. /* Catch the no cq entry condition, log an error */
  10297. if (unlikely(ecount == 0))
  10298. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10299. "0371 No entry from the CQ: identifier "
  10300. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10301. /* In any case, flash and re-arm the RCQ */
  10302. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10303. /* wake up worker thread if there are works to be done */
  10304. if (workposted)
  10305. lpfc_worker_wake_up(phba);
  10306. }
  10307. /**
  10308. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10309. * @eqe: Pointer to fast-path completion queue entry.
  10310. *
  10311. * This routine process a fast-path work queue completion entry from fast-path
  10312. * event queue for FCP command response completion.
  10313. **/
  10314. static void
  10315. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
  10316. struct lpfc_wcqe_complete *wcqe)
  10317. {
  10318. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10319. struct lpfc_iocbq *cmdiocbq;
  10320. struct lpfc_iocbq irspiocbq;
  10321. unsigned long iflags;
  10322. spin_lock_irqsave(&phba->hbalock, iflags);
  10323. pring->stats.iocb_event++;
  10324. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10325. /* Check for response status */
  10326. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10327. /* If resource errors reported from HBA, reduce queue
  10328. * depth of the SCSI device.
  10329. */
  10330. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10331. IOSTAT_LOCAL_REJECT) &&
  10332. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  10333. phba->lpfc_rampdown_queue_depth(phba);
  10334. }
  10335. /* Log the error status */
  10336. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10337. "0373 FCP complete error: status=x%x, "
  10338. "hw_status=x%x, total_data_specified=%d, "
  10339. "parameter=x%x, word3=x%x\n",
  10340. bf_get(lpfc_wcqe_c_status, wcqe),
  10341. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10342. wcqe->total_data_placed, wcqe->parameter,
  10343. wcqe->word3);
  10344. }
  10345. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10346. spin_lock_irqsave(&phba->hbalock, iflags);
  10347. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10348. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10349. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10350. if (unlikely(!cmdiocbq)) {
  10351. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10352. "0374 FCP complete with no corresponding "
  10353. "cmdiocb: iotag (%d)\n",
  10354. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10355. return;
  10356. }
  10357. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10358. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10359. "0375 FCP cmdiocb not callback function "
  10360. "iotag: (%d)\n",
  10361. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10362. return;
  10363. }
  10364. /* Fake the irspiocb and copy necessary response information */
  10365. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10366. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10367. spin_lock_irqsave(&phba->hbalock, iflags);
  10368. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10369. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10370. }
  10371. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10372. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10373. }
  10374. /**
  10375. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10376. * @phba: Pointer to HBA context object.
  10377. * @cq: Pointer to completion queue.
  10378. * @wcqe: Pointer to work-queue completion queue entry.
  10379. *
  10380. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10381. * proper WQ release routine to the slow-path WQ.
  10382. **/
  10383. static void
  10384. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10385. struct lpfc_wcqe_release *wcqe)
  10386. {
  10387. struct lpfc_queue *childwq;
  10388. bool wqid_matched = false;
  10389. uint16_t fcp_wqid;
  10390. /* Check for fast-path FCP work queue release */
  10391. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10392. list_for_each_entry(childwq, &cq->child_list, list) {
  10393. if (childwq->queue_id == fcp_wqid) {
  10394. lpfc_sli4_wq_release(childwq,
  10395. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10396. wqid_matched = true;
  10397. break;
  10398. }
  10399. }
  10400. /* Report warning log message if no match found */
  10401. if (wqid_matched != true)
  10402. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10403. "2580 Fast-path wqe consume event carries "
  10404. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10405. }
  10406. /**
  10407. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10408. * @cq: Pointer to the completion queue.
  10409. * @eqe: Pointer to fast-path completion queue entry.
  10410. *
  10411. * This routine process a fast-path work queue completion entry from fast-path
  10412. * event queue for FCP command response completion.
  10413. **/
  10414. static int
  10415. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10416. struct lpfc_cqe *cqe)
  10417. {
  10418. struct lpfc_wcqe_release wcqe;
  10419. bool workposted = false;
  10420. /* Copy the work queue CQE and convert endian order if needed */
  10421. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10422. /* Check and process for different type of WCQE and dispatch */
  10423. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10424. case CQE_CODE_COMPL_WQE:
  10425. /* Process the WQ complete event */
  10426. phba->last_completion_time = jiffies;
  10427. lpfc_sli4_fp_handle_fcp_wcqe(phba,
  10428. (struct lpfc_wcqe_complete *)&wcqe);
  10429. break;
  10430. case CQE_CODE_RELEASE_WQE:
  10431. /* Process the WQ release event */
  10432. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10433. (struct lpfc_wcqe_release *)&wcqe);
  10434. break;
  10435. case CQE_CODE_XRI_ABORTED:
  10436. /* Process the WQ XRI abort event */
  10437. phba->last_completion_time = jiffies;
  10438. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10439. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10440. break;
  10441. default:
  10442. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10443. "0144 Not a valid WCQE code: x%x\n",
  10444. bf_get(lpfc_wcqe_c_code, &wcqe));
  10445. break;
  10446. }
  10447. return workposted;
  10448. }
  10449. /**
  10450. * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
  10451. * @phba: Pointer to HBA context object.
  10452. * @eqe: Pointer to fast-path event queue entry.
  10453. *
  10454. * This routine process a event queue entry from the fast-path event queue.
  10455. * It will check the MajorCode and MinorCode to determine this is for a
  10456. * completion event on a completion queue, if not, an error shall be logged
  10457. * and just return. Otherwise, it will get to the corresponding completion
  10458. * queue and process all the entries on the completion queue, rearm the
  10459. * completion queue, and then return.
  10460. **/
  10461. static void
  10462. lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10463. uint32_t fcp_cqidx)
  10464. {
  10465. struct lpfc_queue *cq;
  10466. struct lpfc_cqe *cqe;
  10467. bool workposted = false;
  10468. uint16_t cqid;
  10469. int ecount = 0;
  10470. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10471. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10472. "0366 Not a valid fast-path completion "
  10473. "event: majorcode=x%x, minorcode=x%x\n",
  10474. bf_get_le32(lpfc_eqe_major_code, eqe),
  10475. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10476. return;
  10477. }
  10478. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10479. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10480. "3146 Fast-path completion queues "
  10481. "does not exist\n");
  10482. return;
  10483. }
  10484. cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
  10485. if (unlikely(!cq)) {
  10486. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10487. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10488. "0367 Fast-path completion queue "
  10489. "(%d) does not exist\n", fcp_cqidx);
  10490. return;
  10491. }
  10492. /* Get the reference to the corresponding CQ */
  10493. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10494. if (unlikely(cqid != cq->queue_id)) {
  10495. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10496. "0368 Miss-matched fast-path completion "
  10497. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10498. cqid, cq->queue_id);
  10499. return;
  10500. }
  10501. /* Process all the entries to the CQ */
  10502. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10503. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10504. if (!(++ecount % cq->entry_repost))
  10505. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10506. }
  10507. /* Catch the no cq entry condition */
  10508. if (unlikely(ecount == 0))
  10509. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10510. "0369 No entry from fast-path completion "
  10511. "queue fcpcqid=%d\n", cq->queue_id);
  10512. /* In any case, flash and re-arm the CQ */
  10513. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10514. /* wake up worker thread if there are works to be done */
  10515. if (workposted)
  10516. lpfc_worker_wake_up(phba);
  10517. }
  10518. static void
  10519. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10520. {
  10521. struct lpfc_eqe *eqe;
  10522. /* walk all the EQ entries and drop on the floor */
  10523. while ((eqe = lpfc_sli4_eq_get(eq)))
  10524. ;
  10525. /* Clear and re-arm the EQ */
  10526. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10527. }
  10528. /**
  10529. * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
  10530. * @irq: Interrupt number.
  10531. * @dev_id: The device context pointer.
  10532. *
  10533. * This function is directly called from the PCI layer as an interrupt
  10534. * service routine when device with SLI-4 interface spec is enabled with
  10535. * MSI-X multi-message interrupt mode and there are slow-path events in
  10536. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  10537. * interrupt mode, this function is called as part of the device-level
  10538. * interrupt handler. When the PCI slot is in error recovery or the HBA is
  10539. * undergoing initialization, the interrupt handler will not process the
  10540. * interrupt. The link attention and ELS ring attention events are handled
  10541. * by the worker thread. The interrupt handler signals the worker thread
  10542. * and returns for these events. This function is called without any lock
  10543. * held. It gets the hbalock to access and update SLI data structures.
  10544. *
  10545. * This function returns IRQ_HANDLED when interrupt is handled else it
  10546. * returns IRQ_NONE.
  10547. **/
  10548. irqreturn_t
  10549. lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
  10550. {
  10551. struct lpfc_hba *phba;
  10552. struct lpfc_queue *speq;
  10553. struct lpfc_eqe *eqe;
  10554. unsigned long iflag;
  10555. int ecount = 0;
  10556. /*
  10557. * Get the driver's phba structure from the dev_id
  10558. */
  10559. phba = (struct lpfc_hba *)dev_id;
  10560. if (unlikely(!phba))
  10561. return IRQ_NONE;
  10562. /* Get to the EQ struct associated with this vector */
  10563. speq = phba->sli4_hba.sp_eq;
  10564. if (unlikely(!speq))
  10565. return IRQ_NONE;
  10566. /* Check device state for handling interrupt */
  10567. if (unlikely(lpfc_intr_state_check(phba))) {
  10568. /* Check again for link_state with lock held */
  10569. spin_lock_irqsave(&phba->hbalock, iflag);
  10570. if (phba->link_state < LPFC_LINK_DOWN)
  10571. /* Flush, clear interrupt, and rearm the EQ */
  10572. lpfc_sli4_eq_flush(phba, speq);
  10573. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10574. return IRQ_NONE;
  10575. }
  10576. /*
  10577. * Process all the event on FCP slow-path EQ
  10578. */
  10579. while ((eqe = lpfc_sli4_eq_get(speq))) {
  10580. lpfc_sli4_sp_handle_eqe(phba, eqe);
  10581. if (!(++ecount % speq->entry_repost))
  10582. lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
  10583. }
  10584. /* Always clear and re-arm the slow-path EQ */
  10585. lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
  10586. /* Catch the no cq entry condition */
  10587. if (unlikely(ecount == 0)) {
  10588. if (phba->intr_type == MSIX)
  10589. /* MSI-X treated interrupt served as no EQ share INT */
  10590. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10591. "0357 MSI-X interrupt with no EQE\n");
  10592. else
  10593. /* Non MSI-X treated on interrupt as EQ share INT */
  10594. return IRQ_NONE;
  10595. }
  10596. return IRQ_HANDLED;
  10597. } /* lpfc_sli4_sp_intr_handler */
  10598. /**
  10599. * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
  10600. * @irq: Interrupt number.
  10601. * @dev_id: The device context pointer.
  10602. *
  10603. * This function is directly called from the PCI layer as an interrupt
  10604. * service routine when device with SLI-4 interface spec is enabled with
  10605. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10606. * ring event in the HBA. However, when the device is enabled with either
  10607. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10608. * device-level interrupt handler. When the PCI slot is in error recovery
  10609. * or the HBA is undergoing initialization, the interrupt handler will not
  10610. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10611. * the intrrupt context. This function is called without any lock held.
  10612. * It gets the hbalock to access and update SLI data structures. Note that,
  10613. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10614. * equal to that of FCP CQ index.
  10615. *
  10616. * This function returns IRQ_HANDLED when interrupt is handled else it
  10617. * returns IRQ_NONE.
  10618. **/
  10619. irqreturn_t
  10620. lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
  10621. {
  10622. struct lpfc_hba *phba;
  10623. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10624. struct lpfc_queue *fpeq;
  10625. struct lpfc_eqe *eqe;
  10626. unsigned long iflag;
  10627. int ecount = 0;
  10628. uint32_t fcp_eqidx;
  10629. /* Get the driver's phba structure from the dev_id */
  10630. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10631. phba = fcp_eq_hdl->phba;
  10632. fcp_eqidx = fcp_eq_hdl->idx;
  10633. if (unlikely(!phba))
  10634. return IRQ_NONE;
  10635. if (unlikely(!phba->sli4_hba.fp_eq))
  10636. return IRQ_NONE;
  10637. /* Get to the EQ struct associated with this vector */
  10638. fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
  10639. if (unlikely(!fpeq))
  10640. return IRQ_NONE;
  10641. /* Check device state for handling interrupt */
  10642. if (unlikely(lpfc_intr_state_check(phba))) {
  10643. /* Check again for link_state with lock held */
  10644. spin_lock_irqsave(&phba->hbalock, iflag);
  10645. if (phba->link_state < LPFC_LINK_DOWN)
  10646. /* Flush, clear interrupt, and rearm the EQ */
  10647. lpfc_sli4_eq_flush(phba, fpeq);
  10648. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10649. return IRQ_NONE;
  10650. }
  10651. /*
  10652. * Process all the event on FCP fast-path EQ
  10653. */
  10654. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10655. lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
  10656. if (!(++ecount % fpeq->entry_repost))
  10657. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10658. }
  10659. /* Always clear and re-arm the fast-path EQ */
  10660. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10661. if (unlikely(ecount == 0)) {
  10662. if (phba->intr_type == MSIX)
  10663. /* MSI-X treated interrupt served as no EQ share INT */
  10664. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10665. "0358 MSI-X interrupt with no EQE\n");
  10666. else
  10667. /* Non MSI-X treated on interrupt as EQ share INT */
  10668. return IRQ_NONE;
  10669. }
  10670. return IRQ_HANDLED;
  10671. } /* lpfc_sli4_fp_intr_handler */
  10672. /**
  10673. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10674. * @irq: Interrupt number.
  10675. * @dev_id: The device context pointer.
  10676. *
  10677. * This function is the device-level interrupt handler to device with SLI-4
  10678. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10679. * interrupt mode is enabled and there is an event in the HBA which requires
  10680. * driver attention. This function invokes the slow-path interrupt attention
  10681. * handling function and fast-path interrupt attention handling function in
  10682. * turn to process the relevant HBA attention events. This function is called
  10683. * without any lock held. It gets the hbalock to access and update SLI data
  10684. * structures.
  10685. *
  10686. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10687. * returns IRQ_NONE.
  10688. **/
  10689. irqreturn_t
  10690. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10691. {
  10692. struct lpfc_hba *phba;
  10693. irqreturn_t sp_irq_rc, fp_irq_rc;
  10694. bool fp_handled = false;
  10695. uint32_t fcp_eqidx;
  10696. /* Get the driver's phba structure from the dev_id */
  10697. phba = (struct lpfc_hba *)dev_id;
  10698. if (unlikely(!phba))
  10699. return IRQ_NONE;
  10700. /*
  10701. * Invokes slow-path host attention interrupt handling as appropriate.
  10702. */
  10703. sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
  10704. /*
  10705. * Invoke fast-path host attention interrupt handling as appropriate.
  10706. */
  10707. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
  10708. fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
  10709. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10710. if (fp_irq_rc == IRQ_HANDLED)
  10711. fp_handled |= true;
  10712. }
  10713. return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
  10714. } /* lpfc_sli4_intr_handler */
  10715. /**
  10716. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10717. * @queue: The queue structure to free.
  10718. *
  10719. * This function frees a queue structure and the DMAable memory used for
  10720. * the host resident queue. This function must be called after destroying the
  10721. * queue on the HBA.
  10722. **/
  10723. void
  10724. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10725. {
  10726. struct lpfc_dmabuf *dmabuf;
  10727. if (!queue)
  10728. return;
  10729. while (!list_empty(&queue->page_list)) {
  10730. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10731. list);
  10732. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10733. dmabuf->virt, dmabuf->phys);
  10734. kfree(dmabuf);
  10735. }
  10736. kfree(queue);
  10737. return;
  10738. }
  10739. /**
  10740. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10741. * @phba: The HBA that this queue is being created on.
  10742. * @entry_size: The size of each queue entry for this queue.
  10743. * @entry count: The number of entries that this queue will handle.
  10744. *
  10745. * This function allocates a queue structure and the DMAable memory used for
  10746. * the host resident queue. This function must be called before creating the
  10747. * queue on the HBA.
  10748. **/
  10749. struct lpfc_queue *
  10750. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10751. uint32_t entry_count)
  10752. {
  10753. struct lpfc_queue *queue;
  10754. struct lpfc_dmabuf *dmabuf;
  10755. int x, total_qe_count;
  10756. void *dma_pointer;
  10757. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10758. if (!phba->sli4_hba.pc_sli4_params.supported)
  10759. hw_page_size = SLI4_PAGE_SIZE;
  10760. queue = kzalloc(sizeof(struct lpfc_queue) +
  10761. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  10762. if (!queue)
  10763. return NULL;
  10764. queue->page_count = (ALIGN(entry_size * entry_count,
  10765. hw_page_size))/hw_page_size;
  10766. INIT_LIST_HEAD(&queue->list);
  10767. INIT_LIST_HEAD(&queue->page_list);
  10768. INIT_LIST_HEAD(&queue->child_list);
  10769. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  10770. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  10771. if (!dmabuf)
  10772. goto out_fail;
  10773. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  10774. hw_page_size, &dmabuf->phys,
  10775. GFP_KERNEL);
  10776. if (!dmabuf->virt) {
  10777. kfree(dmabuf);
  10778. goto out_fail;
  10779. }
  10780. memset(dmabuf->virt, 0, hw_page_size);
  10781. dmabuf->buffer_tag = x;
  10782. list_add_tail(&dmabuf->list, &queue->page_list);
  10783. /* initialize queue's entry array */
  10784. dma_pointer = dmabuf->virt;
  10785. for (; total_qe_count < entry_count &&
  10786. dma_pointer < (hw_page_size + dmabuf->virt);
  10787. total_qe_count++, dma_pointer += entry_size) {
  10788. queue->qe[total_qe_count].address = dma_pointer;
  10789. }
  10790. }
  10791. queue->entry_size = entry_size;
  10792. queue->entry_count = entry_count;
  10793. /*
  10794. * entry_repost is calculated based on the number of entries in the
  10795. * queue. This works out except for RQs. If buffers are NOT initially
  10796. * posted for every RQE, entry_repost should be adjusted accordingly.
  10797. */
  10798. queue->entry_repost = (entry_count >> 3);
  10799. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  10800. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  10801. queue->phba = phba;
  10802. return queue;
  10803. out_fail:
  10804. lpfc_sli4_queue_free(queue);
  10805. return NULL;
  10806. }
  10807. /**
  10808. * lpfc_eq_create - Create an Event Queue on the HBA
  10809. * @phba: HBA structure that indicates port to create a queue on.
  10810. * @eq: The queue structure to use to create the event queue.
  10811. * @imax: The maximum interrupt per second limit.
  10812. *
  10813. * This function creates an event queue, as detailed in @eq, on a port,
  10814. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  10815. *
  10816. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  10817. * is used to get the entry count and entry size that are necessary to
  10818. * determine the number of pages to allocate and use for this queue. This
  10819. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  10820. * event queue. This function is asynchronous and will wait for the mailbox
  10821. * command to finish before continuing.
  10822. *
  10823. * On success this function will return a zero. If unable to allocate enough
  10824. * memory this function will return -ENOMEM. If the queue create mailbox command
  10825. * fails this function will return -ENXIO.
  10826. **/
  10827. uint32_t
  10828. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  10829. {
  10830. struct lpfc_mbx_eq_create *eq_create;
  10831. LPFC_MBOXQ_t *mbox;
  10832. int rc, length, status = 0;
  10833. struct lpfc_dmabuf *dmabuf;
  10834. uint32_t shdr_status, shdr_add_status;
  10835. union lpfc_sli4_cfg_shdr *shdr;
  10836. uint16_t dmult;
  10837. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10838. /* sanity check on queue memory */
  10839. if (!eq)
  10840. return -ENODEV;
  10841. if (!phba->sli4_hba.pc_sli4_params.supported)
  10842. hw_page_size = SLI4_PAGE_SIZE;
  10843. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10844. if (!mbox)
  10845. return -ENOMEM;
  10846. length = (sizeof(struct lpfc_mbx_eq_create) -
  10847. sizeof(struct lpfc_sli4_cfg_mhdr));
  10848. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10849. LPFC_MBOX_OPCODE_EQ_CREATE,
  10850. length, LPFC_SLI4_MBX_EMBED);
  10851. eq_create = &mbox->u.mqe.un.eq_create;
  10852. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  10853. eq->page_count);
  10854. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  10855. LPFC_EQE_SIZE);
  10856. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  10857. /* Calculate delay multiper from maximum interrupt per second */
  10858. dmult = LPFC_DMULT_CONST/imax - 1;
  10859. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  10860. dmult);
  10861. switch (eq->entry_count) {
  10862. default:
  10863. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10864. "0360 Unsupported EQ count. (%d)\n",
  10865. eq->entry_count);
  10866. if (eq->entry_count < 256)
  10867. return -EINVAL;
  10868. /* otherwise default to smallest count (drop through) */
  10869. case 256:
  10870. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10871. LPFC_EQ_CNT_256);
  10872. break;
  10873. case 512:
  10874. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10875. LPFC_EQ_CNT_512);
  10876. break;
  10877. case 1024:
  10878. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10879. LPFC_EQ_CNT_1024);
  10880. break;
  10881. case 2048:
  10882. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10883. LPFC_EQ_CNT_2048);
  10884. break;
  10885. case 4096:
  10886. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10887. LPFC_EQ_CNT_4096);
  10888. break;
  10889. }
  10890. list_for_each_entry(dmabuf, &eq->page_list, list) {
  10891. memset(dmabuf->virt, 0, hw_page_size);
  10892. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  10893. putPaddrLow(dmabuf->phys);
  10894. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  10895. putPaddrHigh(dmabuf->phys);
  10896. }
  10897. mbox->vport = phba->pport;
  10898. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  10899. mbox->context1 = NULL;
  10900. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10901. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  10902. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10903. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10904. if (shdr_status || shdr_add_status || rc) {
  10905. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10906. "2500 EQ_CREATE mailbox failed with "
  10907. "status x%x add_status x%x, mbx status x%x\n",
  10908. shdr_status, shdr_add_status, rc);
  10909. status = -ENXIO;
  10910. }
  10911. eq->type = LPFC_EQ;
  10912. eq->subtype = LPFC_NONE;
  10913. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  10914. if (eq->queue_id == 0xFFFF)
  10915. status = -ENXIO;
  10916. eq->host_index = 0;
  10917. eq->hba_index = 0;
  10918. mempool_free(mbox, phba->mbox_mem_pool);
  10919. return status;
  10920. }
  10921. /**
  10922. * lpfc_cq_create - Create a Completion Queue on the HBA
  10923. * @phba: HBA structure that indicates port to create a queue on.
  10924. * @cq: The queue structure to use to create the completion queue.
  10925. * @eq: The event queue to bind this completion queue to.
  10926. *
  10927. * This function creates a completion queue, as detailed in @wq, on a port,
  10928. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  10929. *
  10930. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  10931. * is used to get the entry count and entry size that are necessary to
  10932. * determine the number of pages to allocate and use for this queue. The @eq
  10933. * is used to indicate which event queue to bind this completion queue to. This
  10934. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  10935. * completion queue. This function is asynchronous and will wait for the mailbox
  10936. * command to finish before continuing.
  10937. *
  10938. * On success this function will return a zero. If unable to allocate enough
  10939. * memory this function will return -ENOMEM. If the queue create mailbox command
  10940. * fails this function will return -ENXIO.
  10941. **/
  10942. uint32_t
  10943. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10944. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  10945. {
  10946. struct lpfc_mbx_cq_create *cq_create;
  10947. struct lpfc_dmabuf *dmabuf;
  10948. LPFC_MBOXQ_t *mbox;
  10949. int rc, length, status = 0;
  10950. uint32_t shdr_status, shdr_add_status;
  10951. union lpfc_sli4_cfg_shdr *shdr;
  10952. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10953. /* sanity check on queue memory */
  10954. if (!cq || !eq)
  10955. return -ENODEV;
  10956. if (!phba->sli4_hba.pc_sli4_params.supported)
  10957. hw_page_size = SLI4_PAGE_SIZE;
  10958. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10959. if (!mbox)
  10960. return -ENOMEM;
  10961. length = (sizeof(struct lpfc_mbx_cq_create) -
  10962. sizeof(struct lpfc_sli4_cfg_mhdr));
  10963. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10964. LPFC_MBOX_OPCODE_CQ_CREATE,
  10965. length, LPFC_SLI4_MBX_EMBED);
  10966. cq_create = &mbox->u.mqe.un.cq_create;
  10967. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  10968. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  10969. cq->page_count);
  10970. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  10971. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  10972. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  10973. phba->sli4_hba.pc_sli4_params.cqv);
  10974. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  10975. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  10976. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  10977. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  10978. eq->queue_id);
  10979. } else {
  10980. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  10981. eq->queue_id);
  10982. }
  10983. switch (cq->entry_count) {
  10984. default:
  10985. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10986. "0361 Unsupported CQ count. (%d)\n",
  10987. cq->entry_count);
  10988. if (cq->entry_count < 256)
  10989. return -EINVAL;
  10990. /* otherwise default to smallest count (drop through) */
  10991. case 256:
  10992. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10993. LPFC_CQ_CNT_256);
  10994. break;
  10995. case 512:
  10996. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10997. LPFC_CQ_CNT_512);
  10998. break;
  10999. case 1024:
  11000. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11001. LPFC_CQ_CNT_1024);
  11002. break;
  11003. }
  11004. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11005. memset(dmabuf->virt, 0, hw_page_size);
  11006. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11007. putPaddrLow(dmabuf->phys);
  11008. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11009. putPaddrHigh(dmabuf->phys);
  11010. }
  11011. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11012. /* The IOCTL status is embedded in the mailbox subheader. */
  11013. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11014. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11015. if (shdr_status || shdr_add_status || rc) {
  11016. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11017. "2501 CQ_CREATE mailbox failed with "
  11018. "status x%x add_status x%x, mbx status x%x\n",
  11019. shdr_status, shdr_add_status, rc);
  11020. status = -ENXIO;
  11021. goto out;
  11022. }
  11023. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11024. if (cq->queue_id == 0xFFFF) {
  11025. status = -ENXIO;
  11026. goto out;
  11027. }
  11028. /* link the cq onto the parent eq child list */
  11029. list_add_tail(&cq->list, &eq->child_list);
  11030. /* Set up completion queue's type and subtype */
  11031. cq->type = type;
  11032. cq->subtype = subtype;
  11033. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11034. cq->assoc_qid = eq->queue_id;
  11035. cq->host_index = 0;
  11036. cq->hba_index = 0;
  11037. out:
  11038. mempool_free(mbox, phba->mbox_mem_pool);
  11039. return status;
  11040. }
  11041. /**
  11042. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11043. * @phba: HBA structure that indicates port to create a queue on.
  11044. * @mq: The queue structure to use to create the mailbox queue.
  11045. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11046. * @cq: The completion queue to associate with this cq.
  11047. *
  11048. * This function provides failback (fb) functionality when the
  11049. * mq_create_ext fails on older FW generations. It's purpose is identical
  11050. * to mq_create_ext otherwise.
  11051. *
  11052. * This routine cannot fail as all attributes were previously accessed and
  11053. * initialized in mq_create_ext.
  11054. **/
  11055. static void
  11056. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11057. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11058. {
  11059. struct lpfc_mbx_mq_create *mq_create;
  11060. struct lpfc_dmabuf *dmabuf;
  11061. int length;
  11062. length = (sizeof(struct lpfc_mbx_mq_create) -
  11063. sizeof(struct lpfc_sli4_cfg_mhdr));
  11064. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11065. LPFC_MBOX_OPCODE_MQ_CREATE,
  11066. length, LPFC_SLI4_MBX_EMBED);
  11067. mq_create = &mbox->u.mqe.un.mq_create;
  11068. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11069. mq->page_count);
  11070. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11071. cq->queue_id);
  11072. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11073. switch (mq->entry_count) {
  11074. case 16:
  11075. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11076. LPFC_MQ_RING_SIZE_16);
  11077. break;
  11078. case 32:
  11079. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11080. LPFC_MQ_RING_SIZE_32);
  11081. break;
  11082. case 64:
  11083. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11084. LPFC_MQ_RING_SIZE_64);
  11085. break;
  11086. case 128:
  11087. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11088. LPFC_MQ_RING_SIZE_128);
  11089. break;
  11090. }
  11091. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11092. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11093. putPaddrLow(dmabuf->phys);
  11094. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11095. putPaddrHigh(dmabuf->phys);
  11096. }
  11097. }
  11098. /**
  11099. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11100. * @phba: HBA structure that indicates port to create a queue on.
  11101. * @mq: The queue structure to use to create the mailbox queue.
  11102. * @cq: The completion queue to associate with this cq.
  11103. * @subtype: The queue's subtype.
  11104. *
  11105. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11106. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11107. *
  11108. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11109. * is used to get the entry count and entry size that are necessary to
  11110. * determine the number of pages to allocate and use for this queue. This
  11111. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  11112. * mailbox queue. This function is asynchronous and will wait for the mailbox
  11113. * command to finish before continuing.
  11114. *
  11115. * On success this function will return a zero. If unable to allocate enough
  11116. * memory this function will return -ENOMEM. If the queue create mailbox command
  11117. * fails this function will return -ENXIO.
  11118. **/
  11119. int32_t
  11120. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11121. struct lpfc_queue *cq, uint32_t subtype)
  11122. {
  11123. struct lpfc_mbx_mq_create *mq_create;
  11124. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11125. struct lpfc_dmabuf *dmabuf;
  11126. LPFC_MBOXQ_t *mbox;
  11127. int rc, length, status = 0;
  11128. uint32_t shdr_status, shdr_add_status;
  11129. union lpfc_sli4_cfg_shdr *shdr;
  11130. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11131. /* sanity check on queue memory */
  11132. if (!mq || !cq)
  11133. return -ENODEV;
  11134. if (!phba->sli4_hba.pc_sli4_params.supported)
  11135. hw_page_size = SLI4_PAGE_SIZE;
  11136. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11137. if (!mbox)
  11138. return -ENOMEM;
  11139. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11140. sizeof(struct lpfc_sli4_cfg_mhdr));
  11141. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11142. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11143. length, LPFC_SLI4_MBX_EMBED);
  11144. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11145. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11146. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11147. &mq_create_ext->u.request, mq->page_count);
  11148. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11149. &mq_create_ext->u.request, 1);
  11150. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11151. &mq_create_ext->u.request, 1);
  11152. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11153. &mq_create_ext->u.request, 1);
  11154. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11155. &mq_create_ext->u.request, 1);
  11156. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11157. &mq_create_ext->u.request, 1);
  11158. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11159. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11160. phba->sli4_hba.pc_sli4_params.mqv);
  11161. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11162. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11163. cq->queue_id);
  11164. else
  11165. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11166. cq->queue_id);
  11167. switch (mq->entry_count) {
  11168. default:
  11169. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11170. "0362 Unsupported MQ count. (%d)\n",
  11171. mq->entry_count);
  11172. if (mq->entry_count < 16)
  11173. return -EINVAL;
  11174. /* otherwise default to smallest count (drop through) */
  11175. case 16:
  11176. bf_set(lpfc_mq_context_ring_size,
  11177. &mq_create_ext->u.request.context,
  11178. LPFC_MQ_RING_SIZE_16);
  11179. break;
  11180. case 32:
  11181. bf_set(lpfc_mq_context_ring_size,
  11182. &mq_create_ext->u.request.context,
  11183. LPFC_MQ_RING_SIZE_32);
  11184. break;
  11185. case 64:
  11186. bf_set(lpfc_mq_context_ring_size,
  11187. &mq_create_ext->u.request.context,
  11188. LPFC_MQ_RING_SIZE_64);
  11189. break;
  11190. case 128:
  11191. bf_set(lpfc_mq_context_ring_size,
  11192. &mq_create_ext->u.request.context,
  11193. LPFC_MQ_RING_SIZE_128);
  11194. break;
  11195. }
  11196. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11197. memset(dmabuf->virt, 0, hw_page_size);
  11198. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11199. putPaddrLow(dmabuf->phys);
  11200. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11201. putPaddrHigh(dmabuf->phys);
  11202. }
  11203. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11204. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11205. &mq_create_ext->u.response);
  11206. if (rc != MBX_SUCCESS) {
  11207. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11208. "2795 MQ_CREATE_EXT failed with "
  11209. "status x%x. Failback to MQ_CREATE.\n",
  11210. rc);
  11211. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11212. mq_create = &mbox->u.mqe.un.mq_create;
  11213. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11214. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11215. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11216. &mq_create->u.response);
  11217. }
  11218. /* The IOCTL status is embedded in the mailbox subheader. */
  11219. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11220. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11221. if (shdr_status || shdr_add_status || rc) {
  11222. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11223. "2502 MQ_CREATE mailbox failed with "
  11224. "status x%x add_status x%x, mbx status x%x\n",
  11225. shdr_status, shdr_add_status, rc);
  11226. status = -ENXIO;
  11227. goto out;
  11228. }
  11229. if (mq->queue_id == 0xFFFF) {
  11230. status = -ENXIO;
  11231. goto out;
  11232. }
  11233. mq->type = LPFC_MQ;
  11234. mq->assoc_qid = cq->queue_id;
  11235. mq->subtype = subtype;
  11236. mq->host_index = 0;
  11237. mq->hba_index = 0;
  11238. /* link the mq onto the parent cq child list */
  11239. list_add_tail(&mq->list, &cq->child_list);
  11240. out:
  11241. mempool_free(mbox, phba->mbox_mem_pool);
  11242. return status;
  11243. }
  11244. /**
  11245. * lpfc_wq_create - Create a Work Queue on the HBA
  11246. * @phba: HBA structure that indicates port to create a queue on.
  11247. * @wq: The queue structure to use to create the work queue.
  11248. * @cq: The completion queue to bind this work queue to.
  11249. * @subtype: The subtype of the work queue indicating its functionality.
  11250. *
  11251. * This function creates a work queue, as detailed in @wq, on a port, described
  11252. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11253. *
  11254. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11255. * is used to get the entry count and entry size that are necessary to
  11256. * determine the number of pages to allocate and use for this queue. The @cq
  11257. * is used to indicate which completion queue to bind this work queue to. This
  11258. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11259. * work queue. This function is asynchronous and will wait for the mailbox
  11260. * command to finish before continuing.
  11261. *
  11262. * On success this function will return a zero. If unable to allocate enough
  11263. * memory this function will return -ENOMEM. If the queue create mailbox command
  11264. * fails this function will return -ENXIO.
  11265. **/
  11266. uint32_t
  11267. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11268. struct lpfc_queue *cq, uint32_t subtype)
  11269. {
  11270. struct lpfc_mbx_wq_create *wq_create;
  11271. struct lpfc_dmabuf *dmabuf;
  11272. LPFC_MBOXQ_t *mbox;
  11273. int rc, length, status = 0;
  11274. uint32_t shdr_status, shdr_add_status;
  11275. union lpfc_sli4_cfg_shdr *shdr;
  11276. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11277. struct dma_address *page;
  11278. /* sanity check on queue memory */
  11279. if (!wq || !cq)
  11280. return -ENODEV;
  11281. if (!phba->sli4_hba.pc_sli4_params.supported)
  11282. hw_page_size = SLI4_PAGE_SIZE;
  11283. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11284. if (!mbox)
  11285. return -ENOMEM;
  11286. length = (sizeof(struct lpfc_mbx_wq_create) -
  11287. sizeof(struct lpfc_sli4_cfg_mhdr));
  11288. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11289. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11290. length, LPFC_SLI4_MBX_EMBED);
  11291. wq_create = &mbox->u.mqe.un.wq_create;
  11292. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11293. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11294. wq->page_count);
  11295. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11296. cq->queue_id);
  11297. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11298. phba->sli4_hba.pc_sli4_params.wqv);
  11299. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11300. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11301. wq->entry_count);
  11302. switch (wq->entry_size) {
  11303. default:
  11304. case 64:
  11305. bf_set(lpfc_mbx_wq_create_wqe_size,
  11306. &wq_create->u.request_1,
  11307. LPFC_WQ_WQE_SIZE_64);
  11308. break;
  11309. case 128:
  11310. bf_set(lpfc_mbx_wq_create_wqe_size,
  11311. &wq_create->u.request_1,
  11312. LPFC_WQ_WQE_SIZE_128);
  11313. break;
  11314. }
  11315. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11316. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11317. page = wq_create->u.request_1.page;
  11318. } else {
  11319. page = wq_create->u.request.page;
  11320. }
  11321. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11322. memset(dmabuf->virt, 0, hw_page_size);
  11323. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11324. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11325. }
  11326. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11327. /* The IOCTL status is embedded in the mailbox subheader. */
  11328. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11329. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11330. if (shdr_status || shdr_add_status || rc) {
  11331. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11332. "2503 WQ_CREATE mailbox failed with "
  11333. "status x%x add_status x%x, mbx status x%x\n",
  11334. shdr_status, shdr_add_status, rc);
  11335. status = -ENXIO;
  11336. goto out;
  11337. }
  11338. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11339. if (wq->queue_id == 0xFFFF) {
  11340. status = -ENXIO;
  11341. goto out;
  11342. }
  11343. wq->type = LPFC_WQ;
  11344. wq->assoc_qid = cq->queue_id;
  11345. wq->subtype = subtype;
  11346. wq->host_index = 0;
  11347. wq->hba_index = 0;
  11348. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  11349. /* link the wq onto the parent cq child list */
  11350. list_add_tail(&wq->list, &cq->child_list);
  11351. out:
  11352. mempool_free(mbox, phba->mbox_mem_pool);
  11353. return status;
  11354. }
  11355. /**
  11356. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11357. * @phba: HBA structure that indicates port to create a queue on.
  11358. * @rq: The queue structure to use for the receive queue.
  11359. * @qno: The associated HBQ number
  11360. *
  11361. *
  11362. * For SLI4 we need to adjust the RQ repost value based on
  11363. * the number of buffers that are initially posted to the RQ.
  11364. */
  11365. void
  11366. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11367. {
  11368. uint32_t cnt;
  11369. /* sanity check on queue memory */
  11370. if (!rq)
  11371. return;
  11372. cnt = lpfc_hbq_defs[qno]->entry_count;
  11373. /* Recalc repost for RQs based on buffers initially posted */
  11374. cnt = (cnt >> 3);
  11375. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11376. cnt = LPFC_QUEUE_MIN_REPOST;
  11377. rq->entry_repost = cnt;
  11378. }
  11379. /**
  11380. * lpfc_rq_create - Create a Receive Queue on the HBA
  11381. * @phba: HBA structure that indicates port to create a queue on.
  11382. * @hrq: The queue structure to use to create the header receive queue.
  11383. * @drq: The queue structure to use to create the data receive queue.
  11384. * @cq: The completion queue to bind this work queue to.
  11385. *
  11386. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11387. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11388. * to the HBA.
  11389. *
  11390. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11391. * struct is used to get the entry count that is necessary to determine the
  11392. * number of pages to use for this queue. The @cq is used to indicate which
  11393. * completion queue to bind received buffers that are posted to these queues to.
  11394. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11395. * receive queue pair. This function is asynchronous and will wait for the
  11396. * mailbox command to finish before continuing.
  11397. *
  11398. * On success this function will return a zero. If unable to allocate enough
  11399. * memory this function will return -ENOMEM. If the queue create mailbox command
  11400. * fails this function will return -ENXIO.
  11401. **/
  11402. uint32_t
  11403. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11404. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11405. {
  11406. struct lpfc_mbx_rq_create *rq_create;
  11407. struct lpfc_dmabuf *dmabuf;
  11408. LPFC_MBOXQ_t *mbox;
  11409. int rc, length, status = 0;
  11410. uint32_t shdr_status, shdr_add_status;
  11411. union lpfc_sli4_cfg_shdr *shdr;
  11412. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11413. /* sanity check on queue memory */
  11414. if (!hrq || !drq || !cq)
  11415. return -ENODEV;
  11416. if (!phba->sli4_hba.pc_sli4_params.supported)
  11417. hw_page_size = SLI4_PAGE_SIZE;
  11418. if (hrq->entry_count != drq->entry_count)
  11419. return -EINVAL;
  11420. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11421. if (!mbox)
  11422. return -ENOMEM;
  11423. length = (sizeof(struct lpfc_mbx_rq_create) -
  11424. sizeof(struct lpfc_sli4_cfg_mhdr));
  11425. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11426. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11427. length, LPFC_SLI4_MBX_EMBED);
  11428. rq_create = &mbox->u.mqe.un.rq_create;
  11429. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11430. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11431. phba->sli4_hba.pc_sli4_params.rqv);
  11432. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11433. bf_set(lpfc_rq_context_rqe_count_1,
  11434. &rq_create->u.request.context,
  11435. hrq->entry_count);
  11436. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11437. bf_set(lpfc_rq_context_rqe_size,
  11438. &rq_create->u.request.context,
  11439. LPFC_RQE_SIZE_8);
  11440. bf_set(lpfc_rq_context_page_size,
  11441. &rq_create->u.request.context,
  11442. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11443. } else {
  11444. switch (hrq->entry_count) {
  11445. default:
  11446. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11447. "2535 Unsupported RQ count. (%d)\n",
  11448. hrq->entry_count);
  11449. if (hrq->entry_count < 512)
  11450. return -EINVAL;
  11451. /* otherwise default to smallest count (drop through) */
  11452. case 512:
  11453. bf_set(lpfc_rq_context_rqe_count,
  11454. &rq_create->u.request.context,
  11455. LPFC_RQ_RING_SIZE_512);
  11456. break;
  11457. case 1024:
  11458. bf_set(lpfc_rq_context_rqe_count,
  11459. &rq_create->u.request.context,
  11460. LPFC_RQ_RING_SIZE_1024);
  11461. break;
  11462. case 2048:
  11463. bf_set(lpfc_rq_context_rqe_count,
  11464. &rq_create->u.request.context,
  11465. LPFC_RQ_RING_SIZE_2048);
  11466. break;
  11467. case 4096:
  11468. bf_set(lpfc_rq_context_rqe_count,
  11469. &rq_create->u.request.context,
  11470. LPFC_RQ_RING_SIZE_4096);
  11471. break;
  11472. }
  11473. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11474. LPFC_HDR_BUF_SIZE);
  11475. }
  11476. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11477. cq->queue_id);
  11478. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11479. hrq->page_count);
  11480. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11481. memset(dmabuf->virt, 0, hw_page_size);
  11482. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11483. putPaddrLow(dmabuf->phys);
  11484. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11485. putPaddrHigh(dmabuf->phys);
  11486. }
  11487. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11488. /* The IOCTL status is embedded in the mailbox subheader. */
  11489. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11490. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11491. if (shdr_status || shdr_add_status || rc) {
  11492. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11493. "2504 RQ_CREATE mailbox failed with "
  11494. "status x%x add_status x%x, mbx status x%x\n",
  11495. shdr_status, shdr_add_status, rc);
  11496. status = -ENXIO;
  11497. goto out;
  11498. }
  11499. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11500. if (hrq->queue_id == 0xFFFF) {
  11501. status = -ENXIO;
  11502. goto out;
  11503. }
  11504. hrq->type = LPFC_HRQ;
  11505. hrq->assoc_qid = cq->queue_id;
  11506. hrq->subtype = subtype;
  11507. hrq->host_index = 0;
  11508. hrq->hba_index = 0;
  11509. /* now create the data queue */
  11510. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11511. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11512. length, LPFC_SLI4_MBX_EMBED);
  11513. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11514. phba->sli4_hba.pc_sli4_params.rqv);
  11515. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11516. bf_set(lpfc_rq_context_rqe_count_1,
  11517. &rq_create->u.request.context, hrq->entry_count);
  11518. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11519. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11520. LPFC_RQE_SIZE_8);
  11521. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11522. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11523. } else {
  11524. switch (drq->entry_count) {
  11525. default:
  11526. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11527. "2536 Unsupported RQ count. (%d)\n",
  11528. drq->entry_count);
  11529. if (drq->entry_count < 512)
  11530. return -EINVAL;
  11531. /* otherwise default to smallest count (drop through) */
  11532. case 512:
  11533. bf_set(lpfc_rq_context_rqe_count,
  11534. &rq_create->u.request.context,
  11535. LPFC_RQ_RING_SIZE_512);
  11536. break;
  11537. case 1024:
  11538. bf_set(lpfc_rq_context_rqe_count,
  11539. &rq_create->u.request.context,
  11540. LPFC_RQ_RING_SIZE_1024);
  11541. break;
  11542. case 2048:
  11543. bf_set(lpfc_rq_context_rqe_count,
  11544. &rq_create->u.request.context,
  11545. LPFC_RQ_RING_SIZE_2048);
  11546. break;
  11547. case 4096:
  11548. bf_set(lpfc_rq_context_rqe_count,
  11549. &rq_create->u.request.context,
  11550. LPFC_RQ_RING_SIZE_4096);
  11551. break;
  11552. }
  11553. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11554. LPFC_DATA_BUF_SIZE);
  11555. }
  11556. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11557. cq->queue_id);
  11558. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11559. drq->page_count);
  11560. list_for_each_entry(dmabuf, &drq->page_list, list) {
  11561. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11562. putPaddrLow(dmabuf->phys);
  11563. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11564. putPaddrHigh(dmabuf->phys);
  11565. }
  11566. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11567. /* The IOCTL status is embedded in the mailbox subheader. */
  11568. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11569. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11570. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11571. if (shdr_status || shdr_add_status || rc) {
  11572. status = -ENXIO;
  11573. goto out;
  11574. }
  11575. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11576. if (drq->queue_id == 0xFFFF) {
  11577. status = -ENXIO;
  11578. goto out;
  11579. }
  11580. drq->type = LPFC_DRQ;
  11581. drq->assoc_qid = cq->queue_id;
  11582. drq->subtype = subtype;
  11583. drq->host_index = 0;
  11584. drq->hba_index = 0;
  11585. /* link the header and data RQs onto the parent cq child list */
  11586. list_add_tail(&hrq->list, &cq->child_list);
  11587. list_add_tail(&drq->list, &cq->child_list);
  11588. out:
  11589. mempool_free(mbox, phba->mbox_mem_pool);
  11590. return status;
  11591. }
  11592. /**
  11593. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  11594. * @eq: The queue structure associated with the queue to destroy.
  11595. *
  11596. * This function destroys a queue, as detailed in @eq by sending an mailbox
  11597. * command, specific to the type of queue, to the HBA.
  11598. *
  11599. * The @eq struct is used to get the queue ID of the queue to destroy.
  11600. *
  11601. * On success this function will return a zero. If the queue destroy mailbox
  11602. * command fails this function will return -ENXIO.
  11603. **/
  11604. uint32_t
  11605. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11606. {
  11607. LPFC_MBOXQ_t *mbox;
  11608. int rc, length, status = 0;
  11609. uint32_t shdr_status, shdr_add_status;
  11610. union lpfc_sli4_cfg_shdr *shdr;
  11611. /* sanity check on queue memory */
  11612. if (!eq)
  11613. return -ENODEV;
  11614. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  11615. if (!mbox)
  11616. return -ENOMEM;
  11617. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  11618. sizeof(struct lpfc_sli4_cfg_mhdr));
  11619. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11620. LPFC_MBOX_OPCODE_EQ_DESTROY,
  11621. length, LPFC_SLI4_MBX_EMBED);
  11622. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  11623. eq->queue_id);
  11624. mbox->vport = eq->phba->pport;
  11625. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11626. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  11627. /* The IOCTL status is embedded in the mailbox subheader. */
  11628. shdr = (union lpfc_sli4_cfg_shdr *)
  11629. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  11630. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11631. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11632. if (shdr_status || shdr_add_status || rc) {
  11633. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11634. "2505 EQ_DESTROY mailbox failed with "
  11635. "status x%x add_status x%x, mbx status x%x\n",
  11636. shdr_status, shdr_add_status, rc);
  11637. status = -ENXIO;
  11638. }
  11639. /* Remove eq from any list */
  11640. list_del_init(&eq->list);
  11641. mempool_free(mbox, eq->phba->mbox_mem_pool);
  11642. return status;
  11643. }
  11644. /**
  11645. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  11646. * @cq: The queue structure associated with the queue to destroy.
  11647. *
  11648. * This function destroys a queue, as detailed in @cq by sending an mailbox
  11649. * command, specific to the type of queue, to the HBA.
  11650. *
  11651. * The @cq struct is used to get the queue ID of the queue to destroy.
  11652. *
  11653. * On success this function will return a zero. If the queue destroy mailbox
  11654. * command fails this function will return -ENXIO.
  11655. **/
  11656. uint32_t
  11657. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  11658. {
  11659. LPFC_MBOXQ_t *mbox;
  11660. int rc, length, status = 0;
  11661. uint32_t shdr_status, shdr_add_status;
  11662. union lpfc_sli4_cfg_shdr *shdr;
  11663. /* sanity check on queue memory */
  11664. if (!cq)
  11665. return -ENODEV;
  11666. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  11667. if (!mbox)
  11668. return -ENOMEM;
  11669. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  11670. sizeof(struct lpfc_sli4_cfg_mhdr));
  11671. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11672. LPFC_MBOX_OPCODE_CQ_DESTROY,
  11673. length, LPFC_SLI4_MBX_EMBED);
  11674. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  11675. cq->queue_id);
  11676. mbox->vport = cq->phba->pport;
  11677. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11678. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  11679. /* The IOCTL status is embedded in the mailbox subheader. */
  11680. shdr = (union lpfc_sli4_cfg_shdr *)
  11681. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  11682. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11683. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11684. if (shdr_status || shdr_add_status || rc) {
  11685. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11686. "2506 CQ_DESTROY mailbox failed with "
  11687. "status x%x add_status x%x, mbx status x%x\n",
  11688. shdr_status, shdr_add_status, rc);
  11689. status = -ENXIO;
  11690. }
  11691. /* Remove cq from any list */
  11692. list_del_init(&cq->list);
  11693. mempool_free(mbox, cq->phba->mbox_mem_pool);
  11694. return status;
  11695. }
  11696. /**
  11697. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  11698. * @qm: The queue structure associated with the queue to destroy.
  11699. *
  11700. * This function destroys a queue, as detailed in @mq by sending an mailbox
  11701. * command, specific to the type of queue, to the HBA.
  11702. *
  11703. * The @mq struct is used to get the queue ID of the queue to destroy.
  11704. *
  11705. * On success this function will return a zero. If the queue destroy mailbox
  11706. * command fails this function will return -ENXIO.
  11707. **/
  11708. uint32_t
  11709. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  11710. {
  11711. LPFC_MBOXQ_t *mbox;
  11712. int rc, length, status = 0;
  11713. uint32_t shdr_status, shdr_add_status;
  11714. union lpfc_sli4_cfg_shdr *shdr;
  11715. /* sanity check on queue memory */
  11716. if (!mq)
  11717. return -ENODEV;
  11718. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  11719. if (!mbox)
  11720. return -ENOMEM;
  11721. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  11722. sizeof(struct lpfc_sli4_cfg_mhdr));
  11723. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11724. LPFC_MBOX_OPCODE_MQ_DESTROY,
  11725. length, LPFC_SLI4_MBX_EMBED);
  11726. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  11727. mq->queue_id);
  11728. mbox->vport = mq->phba->pport;
  11729. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11730. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  11731. /* The IOCTL status is embedded in the mailbox subheader. */
  11732. shdr = (union lpfc_sli4_cfg_shdr *)
  11733. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  11734. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11735. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11736. if (shdr_status || shdr_add_status || rc) {
  11737. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11738. "2507 MQ_DESTROY mailbox failed with "
  11739. "status x%x add_status x%x, mbx status x%x\n",
  11740. shdr_status, shdr_add_status, rc);
  11741. status = -ENXIO;
  11742. }
  11743. /* Remove mq from any list */
  11744. list_del_init(&mq->list);
  11745. mempool_free(mbox, mq->phba->mbox_mem_pool);
  11746. return status;
  11747. }
  11748. /**
  11749. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  11750. * @wq: The queue structure associated with the queue to destroy.
  11751. *
  11752. * This function destroys a queue, as detailed in @wq by sending an mailbox
  11753. * command, specific to the type of queue, to the HBA.
  11754. *
  11755. * The @wq struct is used to get the queue ID of the queue to destroy.
  11756. *
  11757. * On success this function will return a zero. If the queue destroy mailbox
  11758. * command fails this function will return -ENXIO.
  11759. **/
  11760. uint32_t
  11761. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  11762. {
  11763. LPFC_MBOXQ_t *mbox;
  11764. int rc, length, status = 0;
  11765. uint32_t shdr_status, shdr_add_status;
  11766. union lpfc_sli4_cfg_shdr *shdr;
  11767. /* sanity check on queue memory */
  11768. if (!wq)
  11769. return -ENODEV;
  11770. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  11771. if (!mbox)
  11772. return -ENOMEM;
  11773. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  11774. sizeof(struct lpfc_sli4_cfg_mhdr));
  11775. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11776. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  11777. length, LPFC_SLI4_MBX_EMBED);
  11778. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  11779. wq->queue_id);
  11780. mbox->vport = wq->phba->pport;
  11781. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11782. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  11783. shdr = (union lpfc_sli4_cfg_shdr *)
  11784. &mbox->u.mqe.un.wq_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. "2508 WQ_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. status = -ENXIO;
  11793. }
  11794. /* Remove wq from any list */
  11795. list_del_init(&wq->list);
  11796. mempool_free(mbox, wq->phba->mbox_mem_pool);
  11797. return status;
  11798. }
  11799. /**
  11800. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  11801. * @rq: The queue structure associated with the queue to destroy.
  11802. *
  11803. * This function destroys a queue, as detailed in @rq by sending an mailbox
  11804. * command, specific to the type of queue, to the HBA.
  11805. *
  11806. * The @rq struct is used to get the queue ID of the queue to destroy.
  11807. *
  11808. * On success this function will return a zero. If the queue destroy mailbox
  11809. * command fails this function will return -ENXIO.
  11810. **/
  11811. uint32_t
  11812. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11813. struct lpfc_queue *drq)
  11814. {
  11815. LPFC_MBOXQ_t *mbox;
  11816. int rc, length, status = 0;
  11817. uint32_t shdr_status, shdr_add_status;
  11818. union lpfc_sli4_cfg_shdr *shdr;
  11819. /* sanity check on queue memory */
  11820. if (!hrq || !drq)
  11821. return -ENODEV;
  11822. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  11823. if (!mbox)
  11824. return -ENOMEM;
  11825. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  11826. sizeof(struct lpfc_sli4_cfg_mhdr));
  11827. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11828. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  11829. length, LPFC_SLI4_MBX_EMBED);
  11830. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11831. hrq->queue_id);
  11832. mbox->vport = hrq->phba->pport;
  11833. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11834. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  11835. /* The IOCTL status is embedded in the mailbox subheader. */
  11836. shdr = (union lpfc_sli4_cfg_shdr *)
  11837. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11838. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11839. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11840. if (shdr_status || shdr_add_status || rc) {
  11841. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11842. "2509 RQ_DESTROY mailbox failed with "
  11843. "status x%x add_status x%x, mbx status x%x\n",
  11844. shdr_status, shdr_add_status, rc);
  11845. if (rc != MBX_TIMEOUT)
  11846. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11847. return -ENXIO;
  11848. }
  11849. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11850. drq->queue_id);
  11851. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  11852. shdr = (union lpfc_sli4_cfg_shdr *)
  11853. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11854. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11855. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11856. if (shdr_status || shdr_add_status || rc) {
  11857. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11858. "2510 RQ_DESTROY mailbox failed with "
  11859. "status x%x add_status x%x, mbx status x%x\n",
  11860. shdr_status, shdr_add_status, rc);
  11861. status = -ENXIO;
  11862. }
  11863. list_del_init(&hrq->list);
  11864. list_del_init(&drq->list);
  11865. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11866. return status;
  11867. }
  11868. /**
  11869. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  11870. * @phba: The virtual port for which this call being executed.
  11871. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  11872. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  11873. * @xritag: the xritag that ties this io to the SGL pages.
  11874. *
  11875. * This routine will post the sgl pages for the IO that has the xritag
  11876. * that is in the iocbq structure. The xritag is assigned during iocbq
  11877. * creation and persists for as long as the driver is loaded.
  11878. * if the caller has fewer than 256 scatter gather segments to map then
  11879. * pdma_phys_addr1 should be 0.
  11880. * If the caller needs to map more than 256 scatter gather segment then
  11881. * pdma_phys_addr1 should be a valid physical address.
  11882. * physical address for SGLs must be 64 byte aligned.
  11883. * If you are going to map 2 SGL's then the first one must have 256 entries
  11884. * the second sgl can have between 1 and 256 entries.
  11885. *
  11886. * Return codes:
  11887. * 0 - Success
  11888. * -ENXIO, -ENOMEM - Failure
  11889. **/
  11890. int
  11891. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  11892. dma_addr_t pdma_phys_addr0,
  11893. dma_addr_t pdma_phys_addr1,
  11894. uint16_t xritag)
  11895. {
  11896. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  11897. LPFC_MBOXQ_t *mbox;
  11898. int rc;
  11899. uint32_t shdr_status, shdr_add_status;
  11900. uint32_t mbox_tmo;
  11901. union lpfc_sli4_cfg_shdr *shdr;
  11902. if (xritag == NO_XRI) {
  11903. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11904. "0364 Invalid param:\n");
  11905. return -EINVAL;
  11906. }
  11907. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11908. if (!mbox)
  11909. return -ENOMEM;
  11910. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11911. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  11912. sizeof(struct lpfc_mbx_post_sgl_pages) -
  11913. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  11914. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  11915. &mbox->u.mqe.un.post_sgl_pages;
  11916. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  11917. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  11918. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  11919. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  11920. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  11921. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  11922. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  11923. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  11924. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  11925. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  11926. if (!phba->sli4_hba.intr_enable)
  11927. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11928. else {
  11929. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  11930. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  11931. }
  11932. /* The IOCTL status is embedded in the mailbox subheader. */
  11933. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  11934. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11935. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11936. if (rc != MBX_TIMEOUT)
  11937. mempool_free(mbox, phba->mbox_mem_pool);
  11938. if (shdr_status || shdr_add_status || rc) {
  11939. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11940. "2511 POST_SGL mailbox failed with "
  11941. "status x%x add_status x%x, mbx status x%x\n",
  11942. shdr_status, shdr_add_status, rc);
  11943. rc = -ENXIO;
  11944. }
  11945. return 0;
  11946. }
  11947. /**
  11948. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  11949. * @phba: pointer to lpfc hba data structure.
  11950. *
  11951. * This routine is invoked to post rpi header templates to the
  11952. * HBA consistent with the SLI-4 interface spec. This routine
  11953. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  11954. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  11955. *
  11956. * Returns
  11957. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  11958. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  11959. **/
  11960. uint16_t
  11961. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  11962. {
  11963. unsigned long xri;
  11964. /*
  11965. * Fetch the next logical xri. Because this index is logical,
  11966. * the driver starts at 0 each time.
  11967. */
  11968. spin_lock_irq(&phba->hbalock);
  11969. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  11970. phba->sli4_hba.max_cfg_param.max_xri, 0);
  11971. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  11972. spin_unlock_irq(&phba->hbalock);
  11973. return NO_XRI;
  11974. } else {
  11975. set_bit(xri, phba->sli4_hba.xri_bmask);
  11976. phba->sli4_hba.max_cfg_param.xri_used++;
  11977. phba->sli4_hba.xri_count++;
  11978. }
  11979. spin_unlock_irq(&phba->hbalock);
  11980. return xri;
  11981. }
  11982. /**
  11983. * lpfc_sli4_free_xri - Release an xri for reuse.
  11984. * @phba: pointer to lpfc hba data structure.
  11985. *
  11986. * This routine is invoked to release an xri to the pool of
  11987. * available rpis maintained by the driver.
  11988. **/
  11989. void
  11990. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  11991. {
  11992. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  11993. phba->sli4_hba.xri_count--;
  11994. phba->sli4_hba.max_cfg_param.xri_used--;
  11995. }
  11996. }
  11997. /**
  11998. * lpfc_sli4_free_xri - Release an xri for reuse.
  11999. * @phba: pointer to lpfc hba data structure.
  12000. *
  12001. * This routine is invoked to release an xri to the pool of
  12002. * available rpis maintained by the driver.
  12003. **/
  12004. void
  12005. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12006. {
  12007. spin_lock_irq(&phba->hbalock);
  12008. __lpfc_sli4_free_xri(phba, xri);
  12009. spin_unlock_irq(&phba->hbalock);
  12010. }
  12011. /**
  12012. * lpfc_sli4_next_xritag - Get an xritag for the io
  12013. * @phba: Pointer to HBA context object.
  12014. *
  12015. * This function gets an xritag for the iocb. If there is no unused xritag
  12016. * it will return 0xffff.
  12017. * The function returns the allocated xritag if successful, else returns zero.
  12018. * Zero is not a valid xritag.
  12019. * The caller is not required to hold any lock.
  12020. **/
  12021. uint16_t
  12022. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  12023. {
  12024. uint16_t xri_index;
  12025. xri_index = lpfc_sli4_alloc_xri(phba);
  12026. if (xri_index != NO_XRI)
  12027. return xri_index;
  12028. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12029. "2004 Failed to allocate XRI.last XRITAG is %d"
  12030. " Max XRI is %d, Used XRI is %d\n",
  12031. xri_index,
  12032. phba->sli4_hba.max_cfg_param.max_xri,
  12033. phba->sli4_hba.max_cfg_param.xri_used);
  12034. return NO_XRI;
  12035. }
  12036. /**
  12037. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  12038. * @phba: pointer to lpfc hba data structure.
  12039. *
  12040. * This routine is invoked to post a block of driver's sgl pages to the
  12041. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12042. * is only called when the driver is loading and after all IO has been
  12043. * stopped.
  12044. **/
  12045. int
  12046. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
  12047. {
  12048. struct lpfc_sglq *sglq_entry;
  12049. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12050. struct sgl_page_pairs *sgl_pg_pairs;
  12051. void *viraddr;
  12052. LPFC_MBOXQ_t *mbox;
  12053. uint32_t reqlen, alloclen, pg_pairs;
  12054. uint32_t mbox_tmo;
  12055. uint16_t xritag_start = 0, lxri = 0;
  12056. int els_xri_cnt, rc = 0;
  12057. uint32_t shdr_status, shdr_add_status;
  12058. union lpfc_sli4_cfg_shdr *shdr;
  12059. /* The number of sgls to be posted */
  12060. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  12061. reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12062. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12063. if (reqlen > SLI4_PAGE_SIZE) {
  12064. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12065. "2559 Block sgl registration required DMA "
  12066. "size (%d) great than a page\n", reqlen);
  12067. return -ENOMEM;
  12068. }
  12069. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12070. if (!mbox)
  12071. return -ENOMEM;
  12072. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12073. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12074. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12075. LPFC_SLI4_MBX_NEMBED);
  12076. if (alloclen < reqlen) {
  12077. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12078. "0285 Allocated DMA memory size (%d) is "
  12079. "less than the requested DMA memory "
  12080. "size (%d)\n", alloclen, reqlen);
  12081. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12082. return -ENOMEM;
  12083. }
  12084. /* Set up the SGL pages in the non-embedded DMA pages */
  12085. viraddr = mbox->sge_array->addr[0];
  12086. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12087. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12088. for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
  12089. sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
  12090. /*
  12091. * Assign the sglq a physical xri only if the driver has not
  12092. * initialized those resources. A port reset only needs
  12093. * the sglq's posted.
  12094. */
  12095. if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  12096. LPFC_XRI_RSRC_RDY) {
  12097. lxri = lpfc_sli4_next_xritag(phba);
  12098. if (lxri == NO_XRI) {
  12099. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12100. return -ENOMEM;
  12101. }
  12102. sglq_entry->sli4_lxritag = lxri;
  12103. sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  12104. }
  12105. /* Set up the sge entry */
  12106. sgl_pg_pairs->sgl_pg0_addr_lo =
  12107. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12108. sgl_pg_pairs->sgl_pg0_addr_hi =
  12109. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12110. sgl_pg_pairs->sgl_pg1_addr_lo =
  12111. cpu_to_le32(putPaddrLow(0));
  12112. sgl_pg_pairs->sgl_pg1_addr_hi =
  12113. cpu_to_le32(putPaddrHigh(0));
  12114. /* Keep the first xritag on the list */
  12115. if (pg_pairs == 0)
  12116. xritag_start = sglq_entry->sli4_xritag;
  12117. sgl_pg_pairs++;
  12118. }
  12119. /* Complete initialization and perform endian conversion. */
  12120. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12121. bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
  12122. sgl->word0 = cpu_to_le32(sgl->word0);
  12123. if (!phba->sli4_hba.intr_enable)
  12124. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12125. else {
  12126. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12127. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12128. }
  12129. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12130. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12131. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12132. if (rc != MBX_TIMEOUT)
  12133. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12134. if (shdr_status || shdr_add_status || rc) {
  12135. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12136. "2513 POST_SGL_BLOCK mailbox command failed "
  12137. "status x%x add_status x%x mbx status x%x\n",
  12138. shdr_status, shdr_add_status, rc);
  12139. rc = -ENXIO;
  12140. }
  12141. if (rc == 0)
  12142. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  12143. LPFC_XRI_RSRC_RDY);
  12144. return rc;
  12145. }
  12146. /**
  12147. * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
  12148. * @phba: pointer to lpfc hba data structure.
  12149. *
  12150. * This routine is invoked to post a block of driver's sgl pages to the
  12151. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12152. * is only called when the driver is loading and after all IO has been
  12153. * stopped.
  12154. **/
  12155. int
  12156. lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
  12157. {
  12158. struct lpfc_sglq *sglq_entry;
  12159. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12160. struct sgl_page_pairs *sgl_pg_pairs;
  12161. void *viraddr;
  12162. LPFC_MBOXQ_t *mbox;
  12163. uint32_t reqlen, alloclen, index;
  12164. uint32_t mbox_tmo;
  12165. uint16_t rsrc_start, rsrc_size, els_xri_cnt, post_els_xri_cnt;
  12166. uint16_t xritag_start = 0, lxri = 0;
  12167. struct lpfc_rsrc_blks *rsrc_blk;
  12168. int cnt, ttl_cnt, rc = 0;
  12169. int loop_cnt;
  12170. uint32_t shdr_status, shdr_add_status;
  12171. union lpfc_sli4_cfg_shdr *shdr;
  12172. /* The number of sgls to be posted */
  12173. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  12174. reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12175. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12176. if (reqlen > SLI4_PAGE_SIZE) {
  12177. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12178. "2989 Block sgl registration required DMA "
  12179. "size (%d) great than a page\n", reqlen);
  12180. return -ENOMEM;
  12181. }
  12182. cnt = 0;
  12183. ttl_cnt = 0;
  12184. post_els_xri_cnt = els_xri_cnt;
  12185. list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
  12186. list) {
  12187. rsrc_start = rsrc_blk->rsrc_start;
  12188. rsrc_size = rsrc_blk->rsrc_size;
  12189. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12190. "3014 Working ELS Extent start %d, cnt %d\n",
  12191. rsrc_start, rsrc_size);
  12192. loop_cnt = min(post_els_xri_cnt, rsrc_size);
  12193. if (loop_cnt < post_els_xri_cnt) {
  12194. post_els_xri_cnt -= loop_cnt;
  12195. ttl_cnt += loop_cnt;
  12196. } else
  12197. ttl_cnt += post_els_xri_cnt;
  12198. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12199. if (!mbox)
  12200. return -ENOMEM;
  12201. /*
  12202. * Allocate DMA memory and set up the non-embedded mailbox
  12203. * command.
  12204. */
  12205. alloclen = lpfc_sli4_config(phba, mbox,
  12206. LPFC_MBOX_SUBSYSTEM_FCOE,
  12207. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12208. reqlen, LPFC_SLI4_MBX_NEMBED);
  12209. if (alloclen < reqlen) {
  12210. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12211. "2987 Allocated DMA memory size (%d) "
  12212. "is less than the requested DMA memory "
  12213. "size (%d)\n", alloclen, reqlen);
  12214. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12215. return -ENOMEM;
  12216. }
  12217. /* Set up the SGL pages in the non-embedded DMA pages */
  12218. viraddr = mbox->sge_array->addr[0];
  12219. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12220. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12221. /*
  12222. * The starting resource may not begin at zero. Control
  12223. * the loop variants via the block resource parameters,
  12224. * but handle the sge pointers with a zero-based index
  12225. * that doesn't get reset per loop pass.
  12226. */
  12227. for (index = rsrc_start;
  12228. index < rsrc_start + loop_cnt;
  12229. index++) {
  12230. sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
  12231. /*
  12232. * Assign the sglq a physical xri only if the driver
  12233. * has not initialized those resources. A port reset
  12234. * only needs the sglq's posted.
  12235. */
  12236. if (bf_get(lpfc_xri_rsrc_rdy,
  12237. &phba->sli4_hba.sli4_flags) !=
  12238. LPFC_XRI_RSRC_RDY) {
  12239. lxri = lpfc_sli4_next_xritag(phba);
  12240. if (lxri == NO_XRI) {
  12241. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12242. rc = -ENOMEM;
  12243. goto err_exit;
  12244. }
  12245. sglq_entry->sli4_lxritag = lxri;
  12246. sglq_entry->sli4_xritag =
  12247. phba->sli4_hba.xri_ids[lxri];
  12248. }
  12249. /* Set up the sge entry */
  12250. sgl_pg_pairs->sgl_pg0_addr_lo =
  12251. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12252. sgl_pg_pairs->sgl_pg0_addr_hi =
  12253. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12254. sgl_pg_pairs->sgl_pg1_addr_lo =
  12255. cpu_to_le32(putPaddrLow(0));
  12256. sgl_pg_pairs->sgl_pg1_addr_hi =
  12257. cpu_to_le32(putPaddrHigh(0));
  12258. /* Track the starting physical XRI for the mailbox. */
  12259. if (index == rsrc_start)
  12260. xritag_start = sglq_entry->sli4_xritag;
  12261. sgl_pg_pairs++;
  12262. cnt++;
  12263. }
  12264. /* Complete initialization and perform endian conversion. */
  12265. rsrc_blk->rsrc_used += loop_cnt;
  12266. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12267. bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
  12268. sgl->word0 = cpu_to_le32(sgl->word0);
  12269. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12270. "3015 Post ELS Extent SGL, start %d, "
  12271. "cnt %d, used %d\n",
  12272. xritag_start, loop_cnt, rsrc_blk->rsrc_used);
  12273. if (!phba->sli4_hba.intr_enable)
  12274. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12275. else {
  12276. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12277. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12278. }
  12279. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12280. shdr_status = bf_get(lpfc_mbox_hdr_status,
  12281. &shdr->response);
  12282. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  12283. &shdr->response);
  12284. if (rc != MBX_TIMEOUT)
  12285. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12286. if (shdr_status || shdr_add_status || rc) {
  12287. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12288. "2988 POST_SGL_BLOCK mailbox "
  12289. "command failed status x%x "
  12290. "add_status x%x mbx status x%x\n",
  12291. shdr_status, shdr_add_status, rc);
  12292. rc = -ENXIO;
  12293. goto err_exit;
  12294. }
  12295. if (ttl_cnt >= els_xri_cnt)
  12296. break;
  12297. }
  12298. err_exit:
  12299. if (rc == 0)
  12300. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  12301. LPFC_XRI_RSRC_RDY);
  12302. return rc;
  12303. }
  12304. /**
  12305. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12306. * @phba: pointer to lpfc hba data structure.
  12307. * @sblist: pointer to scsi buffer list.
  12308. * @count: number of scsi buffers on the list.
  12309. *
  12310. * This routine is invoked to post a block of @count scsi sgl pages from a
  12311. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12312. * No Lock is held.
  12313. *
  12314. **/
  12315. int
  12316. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
  12317. int cnt)
  12318. {
  12319. struct lpfc_scsi_buf *psb;
  12320. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12321. struct sgl_page_pairs *sgl_pg_pairs;
  12322. void *viraddr;
  12323. LPFC_MBOXQ_t *mbox;
  12324. uint32_t reqlen, alloclen, pg_pairs;
  12325. uint32_t mbox_tmo;
  12326. uint16_t xritag_start = 0;
  12327. int rc = 0;
  12328. uint32_t shdr_status, shdr_add_status;
  12329. dma_addr_t pdma_phys_bpl1;
  12330. union lpfc_sli4_cfg_shdr *shdr;
  12331. /* Calculate the requested length of the dma memory */
  12332. reqlen = cnt * sizeof(struct sgl_page_pairs) +
  12333. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12334. if (reqlen > SLI4_PAGE_SIZE) {
  12335. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12336. "0217 Block sgl registration required DMA "
  12337. "size (%d) great than a page\n", reqlen);
  12338. return -ENOMEM;
  12339. }
  12340. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12341. if (!mbox) {
  12342. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12343. "0283 Failed to allocate mbox cmd memory\n");
  12344. return -ENOMEM;
  12345. }
  12346. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12347. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12348. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12349. LPFC_SLI4_MBX_NEMBED);
  12350. if (alloclen < reqlen) {
  12351. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12352. "2561 Allocated DMA memory size (%d) is "
  12353. "less than the requested DMA memory "
  12354. "size (%d)\n", alloclen, reqlen);
  12355. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12356. return -ENOMEM;
  12357. }
  12358. /* Get the first SGE entry from the non-embedded DMA memory */
  12359. viraddr = mbox->sge_array->addr[0];
  12360. /* Set up the SGL pages in the non-embedded DMA pages */
  12361. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12362. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12363. pg_pairs = 0;
  12364. list_for_each_entry(psb, sblist, list) {
  12365. /* Set up the sge entry */
  12366. sgl_pg_pairs->sgl_pg0_addr_lo =
  12367. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12368. sgl_pg_pairs->sgl_pg0_addr_hi =
  12369. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12370. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12371. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12372. else
  12373. pdma_phys_bpl1 = 0;
  12374. sgl_pg_pairs->sgl_pg1_addr_lo =
  12375. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12376. sgl_pg_pairs->sgl_pg1_addr_hi =
  12377. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12378. /* Keep the first xritag on the list */
  12379. if (pg_pairs == 0)
  12380. xritag_start = psb->cur_iocbq.sli4_xritag;
  12381. sgl_pg_pairs++;
  12382. pg_pairs++;
  12383. }
  12384. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12385. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12386. /* Perform endian conversion if necessary */
  12387. sgl->word0 = cpu_to_le32(sgl->word0);
  12388. if (!phba->sli4_hba.intr_enable)
  12389. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12390. else {
  12391. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12392. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12393. }
  12394. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12395. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12396. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12397. if (rc != MBX_TIMEOUT)
  12398. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12399. if (shdr_status || shdr_add_status || rc) {
  12400. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12401. "2564 POST_SGL_BLOCK mailbox command failed "
  12402. "status x%x add_status x%x mbx status x%x\n",
  12403. shdr_status, shdr_add_status, rc);
  12404. rc = -ENXIO;
  12405. }
  12406. return rc;
  12407. }
  12408. /**
  12409. * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
  12410. * @phba: pointer to lpfc hba data structure.
  12411. * @sblist: pointer to scsi buffer list.
  12412. * @count: number of scsi buffers on the list.
  12413. *
  12414. * This routine is invoked to post a block of @count scsi sgl pages from a
  12415. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12416. * No Lock is held.
  12417. *
  12418. **/
  12419. int
  12420. lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
  12421. int cnt)
  12422. {
  12423. struct lpfc_scsi_buf *psb = NULL;
  12424. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12425. struct sgl_page_pairs *sgl_pg_pairs;
  12426. void *viraddr;
  12427. LPFC_MBOXQ_t *mbox;
  12428. uint32_t reqlen, alloclen, pg_pairs;
  12429. uint32_t mbox_tmo;
  12430. uint16_t xri_start = 0, scsi_xri_start;
  12431. uint16_t rsrc_range;
  12432. int rc = 0, avail_cnt;
  12433. uint32_t shdr_status, shdr_add_status;
  12434. dma_addr_t pdma_phys_bpl1;
  12435. union lpfc_sli4_cfg_shdr *shdr;
  12436. struct lpfc_rsrc_blks *rsrc_blk;
  12437. uint32_t xri_cnt = 0;
  12438. /* Calculate the total requested length of the dma memory */
  12439. reqlen = cnt * sizeof(struct sgl_page_pairs) +
  12440. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12441. if (reqlen > SLI4_PAGE_SIZE) {
  12442. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12443. "2932 Block sgl registration required DMA "
  12444. "size (%d) great than a page\n", reqlen);
  12445. return -ENOMEM;
  12446. }
  12447. /*
  12448. * The use of extents requires the driver to post the sgl headers
  12449. * in multiple postings to meet the contiguous resource assignment.
  12450. */
  12451. psb = list_prepare_entry(psb, sblist, list);
  12452. scsi_xri_start = phba->sli4_hba.scsi_xri_start;
  12453. list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
  12454. list) {
  12455. rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
  12456. if (rsrc_range < scsi_xri_start)
  12457. continue;
  12458. else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
  12459. continue;
  12460. else
  12461. avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
  12462. reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
  12463. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12464. /*
  12465. * Allocate DMA memory and set up the non-embedded mailbox
  12466. * command. The mbox is used to post an SGL page per loop
  12467. * but the DMA memory has a use-once semantic so the mailbox
  12468. * is used and freed per loop pass.
  12469. */
  12470. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12471. if (!mbox) {
  12472. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12473. "2933 Failed to allocate mbox cmd "
  12474. "memory\n");
  12475. return -ENOMEM;
  12476. }
  12477. alloclen = lpfc_sli4_config(phba, mbox,
  12478. LPFC_MBOX_SUBSYSTEM_FCOE,
  12479. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12480. reqlen,
  12481. LPFC_SLI4_MBX_NEMBED);
  12482. if (alloclen < reqlen) {
  12483. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12484. "2934 Allocated DMA memory size (%d) "
  12485. "is less than the requested DMA memory "
  12486. "size (%d)\n", alloclen, reqlen);
  12487. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12488. return -ENOMEM;
  12489. }
  12490. /* Get the first SGE entry from the non-embedded DMA memory */
  12491. viraddr = mbox->sge_array->addr[0];
  12492. /* Set up the SGL pages in the non-embedded DMA pages */
  12493. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12494. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12495. /* pg_pairs tracks posted SGEs per loop iteration. */
  12496. pg_pairs = 0;
  12497. list_for_each_entry_continue(psb, sblist, list) {
  12498. /* Set up the sge entry */
  12499. sgl_pg_pairs->sgl_pg0_addr_lo =
  12500. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12501. sgl_pg_pairs->sgl_pg0_addr_hi =
  12502. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12503. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12504. pdma_phys_bpl1 = psb->dma_phys_bpl +
  12505. SGL_PAGE_SIZE;
  12506. else
  12507. pdma_phys_bpl1 = 0;
  12508. sgl_pg_pairs->sgl_pg1_addr_lo =
  12509. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12510. sgl_pg_pairs->sgl_pg1_addr_hi =
  12511. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12512. /* Keep the first xri for this extent. */
  12513. if (pg_pairs == 0)
  12514. xri_start = psb->cur_iocbq.sli4_xritag;
  12515. sgl_pg_pairs++;
  12516. pg_pairs++;
  12517. xri_cnt++;
  12518. /*
  12519. * Track two exit conditions - the loop has constructed
  12520. * all of the caller's SGE pairs or all available
  12521. * resource IDs in this extent are consumed.
  12522. */
  12523. if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
  12524. break;
  12525. }
  12526. rsrc_blk->rsrc_used += pg_pairs;
  12527. bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
  12528. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12529. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12530. "3016 Post SCSI Extent SGL, start %d, cnt %d "
  12531. "blk use %d\n",
  12532. xri_start, pg_pairs, rsrc_blk->rsrc_used);
  12533. /* Perform endian conversion if necessary */
  12534. sgl->word0 = cpu_to_le32(sgl->word0);
  12535. if (!phba->sli4_hba.intr_enable)
  12536. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12537. else {
  12538. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12539. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12540. }
  12541. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12542. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12543. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  12544. &shdr->response);
  12545. if (rc != MBX_TIMEOUT)
  12546. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12547. if (shdr_status || shdr_add_status || rc) {
  12548. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12549. "2935 POST_SGL_BLOCK mailbox command "
  12550. "failed status x%x add_status x%x "
  12551. "mbx status x%x\n",
  12552. shdr_status, shdr_add_status, rc);
  12553. return -ENXIO;
  12554. }
  12555. /* Post only what is requested. */
  12556. if (xri_cnt >= cnt)
  12557. break;
  12558. }
  12559. return rc;
  12560. }
  12561. /**
  12562. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12563. * @phba: pointer to lpfc_hba struct that the frame was received on
  12564. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12565. *
  12566. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12567. * valid type of frame that the LPFC driver will handle. This function will
  12568. * return a zero if the frame is a valid frame or a non zero value when the
  12569. * frame does not pass the check.
  12570. **/
  12571. static int
  12572. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12573. {
  12574. /* make rctl_names static to save stack space */
  12575. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12576. char *type_names[] = FC_TYPE_NAMES_INIT;
  12577. struct fc_vft_header *fc_vft_hdr;
  12578. uint32_t *header = (uint32_t *) fc_hdr;
  12579. switch (fc_hdr->fh_r_ctl) {
  12580. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12581. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12582. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12583. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12584. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12585. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12586. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12587. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12588. case FC_RCTL_ELS_REQ: /* extended link services request */
  12589. case FC_RCTL_ELS_REP: /* extended link services reply */
  12590. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12591. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12592. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12593. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12594. case FC_RCTL_BA_RMC: /* remove connection */
  12595. case FC_RCTL_BA_ACC: /* basic accept */
  12596. case FC_RCTL_BA_RJT: /* basic reject */
  12597. case FC_RCTL_BA_PRMT:
  12598. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12599. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12600. case FC_RCTL_P_RJT: /* port reject */
  12601. case FC_RCTL_F_RJT: /* fabric reject */
  12602. case FC_RCTL_P_BSY: /* port busy */
  12603. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12604. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12605. case FC_RCTL_LCR: /* link credit reset */
  12606. case FC_RCTL_END: /* end */
  12607. break;
  12608. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12609. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12610. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12611. return lpfc_fc_frame_check(phba, fc_hdr);
  12612. default:
  12613. goto drop;
  12614. }
  12615. switch (fc_hdr->fh_type) {
  12616. case FC_TYPE_BLS:
  12617. case FC_TYPE_ELS:
  12618. case FC_TYPE_FCP:
  12619. case FC_TYPE_CT:
  12620. break;
  12621. case FC_TYPE_IP:
  12622. case FC_TYPE_ILS:
  12623. default:
  12624. goto drop;
  12625. }
  12626. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12627. "2538 Received frame rctl:%s type:%s "
  12628. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12629. rctl_names[fc_hdr->fh_r_ctl],
  12630. type_names[fc_hdr->fh_type],
  12631. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12632. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12633. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12634. return 0;
  12635. drop:
  12636. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12637. "2539 Dropped frame rctl:%s type:%s\n",
  12638. rctl_names[fc_hdr->fh_r_ctl],
  12639. type_names[fc_hdr->fh_type]);
  12640. return 1;
  12641. }
  12642. /**
  12643. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12644. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12645. *
  12646. * This function processes the FC header to retrieve the VFI from the VF
  12647. * header, if one exists. This function will return the VFI if one exists
  12648. * or 0 if no VSAN Header exists.
  12649. **/
  12650. static uint32_t
  12651. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12652. {
  12653. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12654. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12655. return 0;
  12656. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12657. }
  12658. /**
  12659. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12660. * @phba: Pointer to the HBA structure to search for the vport on
  12661. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12662. * @fcfi: The FC Fabric ID that the frame came from
  12663. *
  12664. * This function searches the @phba for a vport that matches the content of the
  12665. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12666. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12667. * returns the matching vport pointer or NULL if unable to match frame to a
  12668. * vport.
  12669. **/
  12670. static struct lpfc_vport *
  12671. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12672. uint16_t fcfi)
  12673. {
  12674. struct lpfc_vport **vports;
  12675. struct lpfc_vport *vport = NULL;
  12676. int i;
  12677. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12678. fc_hdr->fh_d_id[1] << 8 |
  12679. fc_hdr->fh_d_id[2]);
  12680. if (did == Fabric_DID)
  12681. return phba->pport;
  12682. vports = lpfc_create_vport_work_array(phba);
  12683. if (vports != NULL)
  12684. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12685. if (phba->fcf.fcfi == fcfi &&
  12686. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12687. vports[i]->fc_myDID == did) {
  12688. vport = vports[i];
  12689. break;
  12690. }
  12691. }
  12692. lpfc_destroy_vport_work_array(phba, vports);
  12693. return vport;
  12694. }
  12695. /**
  12696. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12697. * @vport: The vport to work on.
  12698. *
  12699. * This function updates the receive sequence time stamp for this vport. The
  12700. * receive sequence time stamp indicates the time that the last frame of the
  12701. * the sequence that has been idle for the longest amount of time was received.
  12702. * the driver uses this time stamp to indicate if any received sequences have
  12703. * timed out.
  12704. **/
  12705. void
  12706. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12707. {
  12708. struct lpfc_dmabuf *h_buf;
  12709. struct hbq_dmabuf *dmabuf = NULL;
  12710. /* get the oldest sequence on the rcv list */
  12711. h_buf = list_get_first(&vport->rcv_buffer_list,
  12712. struct lpfc_dmabuf, list);
  12713. if (!h_buf)
  12714. return;
  12715. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12716. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12717. }
  12718. /**
  12719. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12720. * @vport: The vport that the received sequences were sent to.
  12721. *
  12722. * This function cleans up all outstanding received sequences. This is called
  12723. * by the driver when a link event or user action invalidates all the received
  12724. * sequences.
  12725. **/
  12726. void
  12727. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12728. {
  12729. struct lpfc_dmabuf *h_buf, *hnext;
  12730. struct lpfc_dmabuf *d_buf, *dnext;
  12731. struct hbq_dmabuf *dmabuf = NULL;
  12732. /* start with the oldest sequence on the rcv list */
  12733. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12734. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12735. list_del_init(&dmabuf->hbuf.list);
  12736. list_for_each_entry_safe(d_buf, dnext,
  12737. &dmabuf->dbuf.list, list) {
  12738. list_del_init(&d_buf->list);
  12739. lpfc_in_buf_free(vport->phba, d_buf);
  12740. }
  12741. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12742. }
  12743. }
  12744. /**
  12745. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12746. * @vport: The vport that the received sequences were sent to.
  12747. *
  12748. * This function determines whether any received sequences have timed out by
  12749. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12750. * indicates that there is at least one timed out sequence this routine will
  12751. * go through the received sequences one at a time from most inactive to most
  12752. * active to determine which ones need to be cleaned up. Once it has determined
  12753. * that a sequence needs to be cleaned up it will simply free up the resources
  12754. * without sending an abort.
  12755. **/
  12756. void
  12757. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12758. {
  12759. struct lpfc_dmabuf *h_buf, *hnext;
  12760. struct lpfc_dmabuf *d_buf, *dnext;
  12761. struct hbq_dmabuf *dmabuf = NULL;
  12762. unsigned long timeout;
  12763. int abort_count = 0;
  12764. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12765. vport->rcv_buffer_time_stamp);
  12766. if (list_empty(&vport->rcv_buffer_list) ||
  12767. time_before(jiffies, timeout))
  12768. return;
  12769. /* start with the oldest sequence on the rcv list */
  12770. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12771. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12772. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12773. dmabuf->time_stamp);
  12774. if (time_before(jiffies, timeout))
  12775. break;
  12776. abort_count++;
  12777. list_del_init(&dmabuf->hbuf.list);
  12778. list_for_each_entry_safe(d_buf, dnext,
  12779. &dmabuf->dbuf.list, list) {
  12780. list_del_init(&d_buf->list);
  12781. lpfc_in_buf_free(vport->phba, d_buf);
  12782. }
  12783. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12784. }
  12785. if (abort_count)
  12786. lpfc_update_rcv_time_stamp(vport);
  12787. }
  12788. /**
  12789. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12790. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12791. *
  12792. * This function searches through the existing incomplete sequences that have
  12793. * been sent to this @vport. If the frame matches one of the incomplete
  12794. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12795. * make up that sequence. If no sequence is found that matches this frame then
  12796. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12797. * This function returns a pointer to the first dmabuf in the sequence list that
  12798. * the frame was linked to.
  12799. **/
  12800. static struct hbq_dmabuf *
  12801. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12802. {
  12803. struct fc_frame_header *new_hdr;
  12804. struct fc_frame_header *temp_hdr;
  12805. struct lpfc_dmabuf *d_buf;
  12806. struct lpfc_dmabuf *h_buf;
  12807. struct hbq_dmabuf *seq_dmabuf = NULL;
  12808. struct hbq_dmabuf *temp_dmabuf = NULL;
  12809. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12810. dmabuf->time_stamp = jiffies;
  12811. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12812. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12813. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12814. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12815. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12816. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12817. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12818. continue;
  12819. /* found a pending sequence that matches this frame */
  12820. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12821. break;
  12822. }
  12823. if (!seq_dmabuf) {
  12824. /*
  12825. * This indicates first frame received for this sequence.
  12826. * Queue the buffer on the vport's rcv_buffer_list.
  12827. */
  12828. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12829. lpfc_update_rcv_time_stamp(vport);
  12830. return dmabuf;
  12831. }
  12832. temp_hdr = seq_dmabuf->hbuf.virt;
  12833. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12834. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12835. list_del_init(&seq_dmabuf->hbuf.list);
  12836. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12837. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12838. lpfc_update_rcv_time_stamp(vport);
  12839. return dmabuf;
  12840. }
  12841. /* move this sequence to the tail to indicate a young sequence */
  12842. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12843. seq_dmabuf->time_stamp = jiffies;
  12844. lpfc_update_rcv_time_stamp(vport);
  12845. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12846. temp_hdr = dmabuf->hbuf.virt;
  12847. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12848. return seq_dmabuf;
  12849. }
  12850. /* find the correct place in the sequence to insert this frame */
  12851. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12852. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12853. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12854. /*
  12855. * If the frame's sequence count is greater than the frame on
  12856. * the list then insert the frame right after this frame
  12857. */
  12858. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  12859. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12860. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  12861. return seq_dmabuf;
  12862. }
  12863. }
  12864. return NULL;
  12865. }
  12866. /**
  12867. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  12868. * @vport: pointer to a vitural port
  12869. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12870. *
  12871. * This function tries to abort from the partially assembed sequence, described
  12872. * by the information from basic abbort @dmabuf. It checks to see whether such
  12873. * partially assembled sequence held by the driver. If so, it shall free up all
  12874. * the frames from the partially assembled sequence.
  12875. *
  12876. * Return
  12877. * true -- if there is matching partially assembled sequence present and all
  12878. * the frames freed with the sequence;
  12879. * false -- if there is no matching partially assembled sequence present so
  12880. * nothing got aborted in the lower layer driver
  12881. **/
  12882. static bool
  12883. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  12884. struct hbq_dmabuf *dmabuf)
  12885. {
  12886. struct fc_frame_header *new_hdr;
  12887. struct fc_frame_header *temp_hdr;
  12888. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  12889. struct hbq_dmabuf *seq_dmabuf = NULL;
  12890. /* Use the hdr_buf to find the sequence that matches this frame */
  12891. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12892. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  12893. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12894. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12895. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12896. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12897. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12898. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12899. continue;
  12900. /* found a pending sequence that matches this frame */
  12901. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12902. break;
  12903. }
  12904. /* Free up all the frames from the partially assembled sequence */
  12905. if (seq_dmabuf) {
  12906. list_for_each_entry_safe(d_buf, n_buf,
  12907. &seq_dmabuf->dbuf.list, list) {
  12908. list_del_init(&d_buf->list);
  12909. lpfc_in_buf_free(vport->phba, d_buf);
  12910. }
  12911. return true;
  12912. }
  12913. return false;
  12914. }
  12915. /**
  12916. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  12917. * @phba: Pointer to HBA context object.
  12918. * @cmd_iocbq: pointer to the command iocbq structure.
  12919. * @rsp_iocbq: pointer to the response iocbq structure.
  12920. *
  12921. * This function handles the sequence abort response iocb command complete
  12922. * event. It properly releases the memory allocated to the sequence abort
  12923. * accept iocb.
  12924. **/
  12925. static void
  12926. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  12927. struct lpfc_iocbq *cmd_iocbq,
  12928. struct lpfc_iocbq *rsp_iocbq)
  12929. {
  12930. if (cmd_iocbq)
  12931. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  12932. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  12933. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  12934. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12935. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  12936. rsp_iocbq->iocb.ulpStatus,
  12937. rsp_iocbq->iocb.un.ulpWord[4]);
  12938. }
  12939. /**
  12940. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  12941. * @phba: Pointer to HBA context object.
  12942. * @xri: xri id in transaction.
  12943. *
  12944. * This function validates the xri maps to the known range of XRIs allocated an
  12945. * used by the driver.
  12946. **/
  12947. uint16_t
  12948. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  12949. uint16_t xri)
  12950. {
  12951. int i;
  12952. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  12953. if (xri == phba->sli4_hba.xri_ids[i])
  12954. return i;
  12955. }
  12956. return NO_XRI;
  12957. }
  12958. /**
  12959. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  12960. * @phba: Pointer to HBA context object.
  12961. * @fc_hdr: pointer to a FC frame header.
  12962. *
  12963. * This function sends a basic response to a previous unsol sequence abort
  12964. * event after aborting the sequence handling.
  12965. **/
  12966. static void
  12967. lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
  12968. struct fc_frame_header *fc_hdr)
  12969. {
  12970. struct lpfc_iocbq *ctiocb = NULL;
  12971. struct lpfc_nodelist *ndlp;
  12972. uint16_t oxid, rxid;
  12973. uint32_t sid, fctl;
  12974. IOCB_t *icmd;
  12975. int rc;
  12976. if (!lpfc_is_link_up(phba))
  12977. return;
  12978. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12979. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  12980. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  12981. ndlp = lpfc_findnode_did(phba->pport, sid);
  12982. if (!ndlp) {
  12983. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12984. "1268 Find ndlp returned NULL for oxid:x%x "
  12985. "SID:x%x\n", oxid, sid);
  12986. return;
  12987. }
  12988. if (lpfc_sli4_xri_inrange(phba, rxid))
  12989. lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
  12990. /* Allocate buffer for rsp iocb */
  12991. ctiocb = lpfc_sli_get_iocbq(phba);
  12992. if (!ctiocb)
  12993. return;
  12994. /* Extract the F_CTL field from FC_HDR */
  12995. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  12996. icmd = &ctiocb->iocb;
  12997. icmd->un.xseq64.bdl.bdeSize = 0;
  12998. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  12999. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  13000. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  13001. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  13002. /* Fill in the rest of iocb fields */
  13003. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  13004. icmd->ulpBdeCount = 0;
  13005. icmd->ulpLe = 1;
  13006. icmd->ulpClass = CLASS3;
  13007. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  13008. ctiocb->context1 = ndlp;
  13009. ctiocb->iocb_cmpl = NULL;
  13010. ctiocb->vport = phba->pport;
  13011. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  13012. ctiocb->sli4_lxritag = NO_XRI;
  13013. ctiocb->sli4_xritag = NO_XRI;
  13014. /* If the oxid maps to the FCP XRI range or if it is out of range,
  13015. * send a BLS_RJT. The driver no longer has that exchange.
  13016. * Override the IOCB for a BA_RJT.
  13017. */
  13018. if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
  13019. phba->sli4_hba.max_cfg_param.xri_base) ||
  13020. oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
  13021. phba->sli4_hba.max_cfg_param.xri_base)) {
  13022. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  13023. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  13024. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  13025. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  13026. }
  13027. if (fctl & FC_FC_EX_CTX) {
  13028. /* ABTS sent by responder to CT exchange, construction
  13029. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  13030. * field and RX_ID from ABTS for RX_ID field.
  13031. */
  13032. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  13033. } else {
  13034. /* ABTS sent by initiator to CT exchange, construction
  13035. * of BA_ACC will need to allocate a new XRI as for the
  13036. * XRI_TAG field.
  13037. */
  13038. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  13039. }
  13040. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  13041. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  13042. /* Xmit CT abts response on exchange <xid> */
  13043. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  13044. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  13045. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  13046. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  13047. if (rc == IOCB_ERROR) {
  13048. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  13049. "2925 Failed to issue CT ABTS RSP x%x on "
  13050. "xri x%x, Data x%x\n",
  13051. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  13052. phba->link_state);
  13053. lpfc_sli_release_iocbq(phba, ctiocb);
  13054. }
  13055. }
  13056. /**
  13057. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  13058. * @vport: Pointer to the vport on which this sequence was received
  13059. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13060. *
  13061. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  13062. * receive sequence is only partially assembed by the driver, it shall abort
  13063. * the partially assembled frames for the sequence. Otherwise, if the
  13064. * unsolicited receive sequence has been completely assembled and passed to
  13065. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  13066. * unsolicited sequence has been aborted. After that, it will issue a basic
  13067. * accept to accept the abort.
  13068. **/
  13069. void
  13070. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  13071. struct hbq_dmabuf *dmabuf)
  13072. {
  13073. struct lpfc_hba *phba = vport->phba;
  13074. struct fc_frame_header fc_hdr;
  13075. uint32_t fctl;
  13076. bool abts_par;
  13077. /* Make a copy of fc_hdr before the dmabuf being released */
  13078. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  13079. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  13080. if (fctl & FC_FC_EX_CTX) {
  13081. /*
  13082. * ABTS sent by responder to exchange, just free the buffer
  13083. */
  13084. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13085. } else {
  13086. /*
  13087. * ABTS sent by initiator to exchange, need to do cleanup
  13088. */
  13089. /* Try to abort partially assembled seq */
  13090. abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  13091. /* Send abort to ULP if partially seq abort failed */
  13092. if (abts_par == false)
  13093. lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
  13094. else
  13095. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13096. }
  13097. /* Send basic accept (BA_ACC) to the abort requester */
  13098. lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
  13099. }
  13100. /**
  13101. * lpfc_seq_complete - Indicates if a sequence is complete
  13102. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13103. *
  13104. * This function checks the sequence, starting with the frame described by
  13105. * @dmabuf, to see if all the frames associated with this sequence are present.
  13106. * the frames associated with this sequence are linked to the @dmabuf using the
  13107. * dbuf list. This function looks for two major things. 1) That the first frame
  13108. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  13109. * set. 3) That there are no holes in the sequence count. The function will
  13110. * return 1 when the sequence is complete, otherwise it will return 0.
  13111. **/
  13112. static int
  13113. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  13114. {
  13115. struct fc_frame_header *hdr;
  13116. struct lpfc_dmabuf *d_buf;
  13117. struct hbq_dmabuf *seq_dmabuf;
  13118. uint32_t fctl;
  13119. int seq_count = 0;
  13120. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13121. /* make sure first fame of sequence has a sequence count of zero */
  13122. if (hdr->fh_seq_cnt != seq_count)
  13123. return 0;
  13124. fctl = (hdr->fh_f_ctl[0] << 16 |
  13125. hdr->fh_f_ctl[1] << 8 |
  13126. hdr->fh_f_ctl[2]);
  13127. /* If last frame of sequence we can return success. */
  13128. if (fctl & FC_FC_END_SEQ)
  13129. return 1;
  13130. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  13131. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13132. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13133. /* If there is a hole in the sequence count then fail. */
  13134. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  13135. return 0;
  13136. fctl = (hdr->fh_f_ctl[0] << 16 |
  13137. hdr->fh_f_ctl[1] << 8 |
  13138. hdr->fh_f_ctl[2]);
  13139. /* If last frame of sequence we can return success. */
  13140. if (fctl & FC_FC_END_SEQ)
  13141. return 1;
  13142. }
  13143. return 0;
  13144. }
  13145. /**
  13146. * lpfc_prep_seq - Prep sequence for ULP processing
  13147. * @vport: Pointer to the vport on which this sequence was received
  13148. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13149. *
  13150. * This function takes a sequence, described by a list of frames, and creates
  13151. * a list of iocbq structures to describe the sequence. This iocbq list will be
  13152. * used to issue to the generic unsolicited sequence handler. This routine
  13153. * returns a pointer to the first iocbq in the list. If the function is unable
  13154. * to allocate an iocbq then it throw out the received frames that were not
  13155. * able to be described and return a pointer to the first iocbq. If unable to
  13156. * allocate any iocbqs (including the first) this function will return NULL.
  13157. **/
  13158. static struct lpfc_iocbq *
  13159. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  13160. {
  13161. struct hbq_dmabuf *hbq_buf;
  13162. struct lpfc_dmabuf *d_buf, *n_buf;
  13163. struct lpfc_iocbq *first_iocbq, *iocbq;
  13164. struct fc_frame_header *fc_hdr;
  13165. uint32_t sid;
  13166. uint32_t len, tot_len;
  13167. struct ulp_bde64 *pbde;
  13168. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13169. /* remove from receive buffer list */
  13170. list_del_init(&seq_dmabuf->hbuf.list);
  13171. lpfc_update_rcv_time_stamp(vport);
  13172. /* get the Remote Port's SID */
  13173. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13174. tot_len = 0;
  13175. /* Get an iocbq struct to fill in. */
  13176. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  13177. if (first_iocbq) {
  13178. /* Initialize the first IOCB. */
  13179. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  13180. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  13181. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  13182. first_iocbq->iocb.ulpContext = NO_XRI;
  13183. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  13184. be16_to_cpu(fc_hdr->fh_ox_id);
  13185. /* iocbq is prepped for internal consumption. Physical vpi. */
  13186. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  13187. vport->phba->vpi_ids[vport->vpi];
  13188. /* put the first buffer into the first IOCBq */
  13189. first_iocbq->context2 = &seq_dmabuf->dbuf;
  13190. first_iocbq->context3 = NULL;
  13191. first_iocbq->iocb.ulpBdeCount = 1;
  13192. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13193. LPFC_DATA_BUF_SIZE;
  13194. first_iocbq->iocb.un.rcvels.remoteID = sid;
  13195. tot_len = bf_get(lpfc_rcqe_length,
  13196. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  13197. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13198. }
  13199. iocbq = first_iocbq;
  13200. /*
  13201. * Each IOCBq can have two Buffers assigned, so go through the list
  13202. * of buffers for this sequence and save two buffers in each IOCBq
  13203. */
  13204. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13205. if (!iocbq) {
  13206. lpfc_in_buf_free(vport->phba, d_buf);
  13207. continue;
  13208. }
  13209. if (!iocbq->context3) {
  13210. iocbq->context3 = d_buf;
  13211. iocbq->iocb.ulpBdeCount++;
  13212. pbde = (struct ulp_bde64 *)
  13213. &iocbq->iocb.unsli3.sli3Words[4];
  13214. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13215. /* We need to get the size out of the right CQE */
  13216. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13217. len = bf_get(lpfc_rcqe_length,
  13218. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13219. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13220. tot_len += len;
  13221. } else {
  13222. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13223. if (!iocbq) {
  13224. if (first_iocbq) {
  13225. first_iocbq->iocb.ulpStatus =
  13226. IOSTAT_FCP_RSP_ERROR;
  13227. first_iocbq->iocb.un.ulpWord[4] =
  13228. IOERR_NO_RESOURCES;
  13229. }
  13230. lpfc_in_buf_free(vport->phba, d_buf);
  13231. continue;
  13232. }
  13233. iocbq->context2 = d_buf;
  13234. iocbq->context3 = NULL;
  13235. iocbq->iocb.ulpBdeCount = 1;
  13236. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13237. LPFC_DATA_BUF_SIZE;
  13238. /* We need to get the size out of the right CQE */
  13239. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13240. len = bf_get(lpfc_rcqe_length,
  13241. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13242. tot_len += len;
  13243. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13244. iocbq->iocb.un.rcvels.remoteID = sid;
  13245. list_add_tail(&iocbq->list, &first_iocbq->list);
  13246. }
  13247. }
  13248. return first_iocbq;
  13249. }
  13250. static void
  13251. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13252. struct hbq_dmabuf *seq_dmabuf)
  13253. {
  13254. struct fc_frame_header *fc_hdr;
  13255. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13256. struct lpfc_hba *phba = vport->phba;
  13257. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13258. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13259. if (!iocbq) {
  13260. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13261. "2707 Ring %d handler: Failed to allocate "
  13262. "iocb Rctl x%x Type x%x received\n",
  13263. LPFC_ELS_RING,
  13264. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13265. return;
  13266. }
  13267. if (!lpfc_complete_unsol_iocb(phba,
  13268. &phba->sli.ring[LPFC_ELS_RING],
  13269. iocbq, fc_hdr->fh_r_ctl,
  13270. fc_hdr->fh_type))
  13271. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13272. "2540 Ring %d handler: unexpected Rctl "
  13273. "x%x Type x%x received\n",
  13274. LPFC_ELS_RING,
  13275. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13276. /* Free iocb created in lpfc_prep_seq */
  13277. list_for_each_entry_safe(curr_iocb, next_iocb,
  13278. &iocbq->list, list) {
  13279. list_del_init(&curr_iocb->list);
  13280. lpfc_sli_release_iocbq(phba, curr_iocb);
  13281. }
  13282. lpfc_sli_release_iocbq(phba, iocbq);
  13283. }
  13284. /**
  13285. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13286. * @phba: Pointer to HBA context object.
  13287. *
  13288. * This function is called with no lock held. This function processes all
  13289. * the received buffers and gives it to upper layers when a received buffer
  13290. * indicates that it is the final frame in the sequence. The interrupt
  13291. * service routine processes received buffers at interrupt contexts and adds
  13292. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13293. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13294. * appropriate receive function when the final frame in a sequence is received.
  13295. **/
  13296. void
  13297. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13298. struct hbq_dmabuf *dmabuf)
  13299. {
  13300. struct hbq_dmabuf *seq_dmabuf;
  13301. struct fc_frame_header *fc_hdr;
  13302. struct lpfc_vport *vport;
  13303. uint32_t fcfi;
  13304. /* Process each received buffer */
  13305. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13306. /* check to see if this a valid type of frame */
  13307. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13308. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13309. return;
  13310. }
  13311. if ((bf_get(lpfc_cqe_code,
  13312. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13313. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13314. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13315. else
  13316. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13317. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13318. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13319. if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
  13320. /* throw out the frame */
  13321. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13322. return;
  13323. }
  13324. /* Handle the basic abort sequence (BA_ABTS) event */
  13325. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13326. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13327. return;
  13328. }
  13329. /* Link this frame */
  13330. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13331. if (!seq_dmabuf) {
  13332. /* unable to add frame to vport - throw it out */
  13333. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13334. return;
  13335. }
  13336. /* If not last frame in sequence continue processing frames. */
  13337. if (!lpfc_seq_complete(seq_dmabuf))
  13338. return;
  13339. /* Send the complete sequence to the upper layer protocol */
  13340. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13341. }
  13342. /**
  13343. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13344. * @phba: pointer to lpfc hba data structure.
  13345. *
  13346. * This routine is invoked to post rpi header templates to the
  13347. * HBA consistent with the SLI-4 interface spec. This routine
  13348. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13349. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13350. *
  13351. * This routine does not require any locks. It's usage is expected
  13352. * to be driver load or reset recovery when the driver is
  13353. * sequential.
  13354. *
  13355. * Return codes
  13356. * 0 - successful
  13357. * -EIO - The mailbox failed to complete successfully.
  13358. * When this error occurs, the driver is not guaranteed
  13359. * to have any rpi regions posted to the device and
  13360. * must either attempt to repost the regions or take a
  13361. * fatal error.
  13362. **/
  13363. int
  13364. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13365. {
  13366. struct lpfc_rpi_hdr *rpi_page;
  13367. uint32_t rc = 0;
  13368. uint16_t lrpi = 0;
  13369. /* SLI4 ports that support extents do not require RPI headers. */
  13370. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13371. goto exit;
  13372. if (phba->sli4_hba.extents_in_use)
  13373. return -EIO;
  13374. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13375. /*
  13376. * Assign the rpi headers a physical rpi only if the driver
  13377. * has not initialized those resources. A port reset only
  13378. * needs the headers posted.
  13379. */
  13380. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13381. LPFC_RPI_RSRC_RDY)
  13382. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13383. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13384. if (rc != MBX_SUCCESS) {
  13385. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13386. "2008 Error %d posting all rpi "
  13387. "headers\n", rc);
  13388. rc = -EIO;
  13389. break;
  13390. }
  13391. }
  13392. exit:
  13393. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13394. LPFC_RPI_RSRC_RDY);
  13395. return rc;
  13396. }
  13397. /**
  13398. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13399. * @phba: pointer to lpfc hba data structure.
  13400. * @rpi_page: pointer to the rpi memory region.
  13401. *
  13402. * This routine is invoked to post a single rpi header to the
  13403. * HBA consistent with the SLI-4 interface spec. This memory region
  13404. * maps up to 64 rpi context regions.
  13405. *
  13406. * Return codes
  13407. * 0 - successful
  13408. * -ENOMEM - No available memory
  13409. * -EIO - The mailbox failed to complete successfully.
  13410. **/
  13411. int
  13412. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13413. {
  13414. LPFC_MBOXQ_t *mboxq;
  13415. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13416. uint32_t rc = 0;
  13417. uint32_t shdr_status, shdr_add_status;
  13418. union lpfc_sli4_cfg_shdr *shdr;
  13419. /* SLI4 ports that support extents do not require RPI headers. */
  13420. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13421. return rc;
  13422. if (phba->sli4_hba.extents_in_use)
  13423. return -EIO;
  13424. /* The port is notified of the header region via a mailbox command. */
  13425. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13426. if (!mboxq) {
  13427. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13428. "2001 Unable to allocate memory for issuing "
  13429. "SLI_CONFIG_SPECIAL mailbox command\n");
  13430. return -ENOMEM;
  13431. }
  13432. /* Post all rpi memory regions to the port. */
  13433. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13434. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13435. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13436. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13437. sizeof(struct lpfc_sli4_cfg_mhdr),
  13438. LPFC_SLI4_MBX_EMBED);
  13439. /* Post the physical rpi to the port for this rpi header. */
  13440. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13441. rpi_page->start_rpi);
  13442. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13443. hdr_tmpl, rpi_page->page_count);
  13444. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13445. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13446. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13447. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13448. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13449. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13450. if (rc != MBX_TIMEOUT)
  13451. mempool_free(mboxq, phba->mbox_mem_pool);
  13452. if (shdr_status || shdr_add_status || rc) {
  13453. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13454. "2514 POST_RPI_HDR mailbox failed with "
  13455. "status x%x add_status x%x, mbx status x%x\n",
  13456. shdr_status, shdr_add_status, rc);
  13457. rc = -ENXIO;
  13458. }
  13459. return rc;
  13460. }
  13461. /**
  13462. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13463. * @phba: pointer to lpfc hba data structure.
  13464. *
  13465. * This routine is invoked to post rpi header templates to the
  13466. * HBA consistent with the SLI-4 interface spec. This routine
  13467. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13468. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13469. *
  13470. * Returns
  13471. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13472. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13473. **/
  13474. int
  13475. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13476. {
  13477. unsigned long rpi;
  13478. uint16_t max_rpi, rpi_limit;
  13479. uint16_t rpi_remaining, lrpi = 0;
  13480. struct lpfc_rpi_hdr *rpi_hdr;
  13481. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13482. rpi_limit = phba->sli4_hba.next_rpi;
  13483. /*
  13484. * Fetch the next logical rpi. Because this index is logical,
  13485. * the driver starts at 0 each time.
  13486. */
  13487. spin_lock_irq(&phba->hbalock);
  13488. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13489. if (rpi >= rpi_limit)
  13490. rpi = LPFC_RPI_ALLOC_ERROR;
  13491. else {
  13492. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13493. phba->sli4_hba.max_cfg_param.rpi_used++;
  13494. phba->sli4_hba.rpi_count++;
  13495. }
  13496. /*
  13497. * Don't try to allocate more rpi header regions if the device limit
  13498. * has been exhausted.
  13499. */
  13500. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13501. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13502. spin_unlock_irq(&phba->hbalock);
  13503. return rpi;
  13504. }
  13505. /*
  13506. * RPI header postings are not required for SLI4 ports capable of
  13507. * extents.
  13508. */
  13509. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13510. spin_unlock_irq(&phba->hbalock);
  13511. return rpi;
  13512. }
  13513. /*
  13514. * If the driver is running low on rpi resources, allocate another
  13515. * page now. Note that the next_rpi value is used because
  13516. * it represents how many are actually in use whereas max_rpi notes
  13517. * how many are supported max by the device.
  13518. */
  13519. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13520. spin_unlock_irq(&phba->hbalock);
  13521. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13522. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13523. if (!rpi_hdr) {
  13524. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13525. "2002 Error Could not grow rpi "
  13526. "count\n");
  13527. } else {
  13528. lrpi = rpi_hdr->start_rpi;
  13529. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13530. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13531. }
  13532. }
  13533. return rpi;
  13534. }
  13535. /**
  13536. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13537. * @phba: pointer to lpfc hba data structure.
  13538. *
  13539. * This routine is invoked to release an rpi to the pool of
  13540. * available rpis maintained by the driver.
  13541. **/
  13542. void
  13543. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13544. {
  13545. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13546. phba->sli4_hba.rpi_count--;
  13547. phba->sli4_hba.max_cfg_param.rpi_used--;
  13548. }
  13549. }
  13550. /**
  13551. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13552. * @phba: pointer to lpfc hba data structure.
  13553. *
  13554. * This routine is invoked to release an rpi to the pool of
  13555. * available rpis maintained by the driver.
  13556. **/
  13557. void
  13558. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13559. {
  13560. spin_lock_irq(&phba->hbalock);
  13561. __lpfc_sli4_free_rpi(phba, rpi);
  13562. spin_unlock_irq(&phba->hbalock);
  13563. }
  13564. /**
  13565. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13566. * @phba: pointer to lpfc hba data structure.
  13567. *
  13568. * This routine is invoked to remove the memory region that
  13569. * provided rpi via a bitmask.
  13570. **/
  13571. void
  13572. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13573. {
  13574. kfree(phba->sli4_hba.rpi_bmask);
  13575. kfree(phba->sli4_hba.rpi_ids);
  13576. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13577. }
  13578. /**
  13579. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  13580. * @phba: pointer to lpfc hba data structure.
  13581. *
  13582. * This routine is invoked to remove the memory region that
  13583. * provided rpi via a bitmask.
  13584. **/
  13585. int
  13586. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  13587. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  13588. {
  13589. LPFC_MBOXQ_t *mboxq;
  13590. struct lpfc_hba *phba = ndlp->phba;
  13591. int rc;
  13592. /* The port is notified of the header region via a mailbox command. */
  13593. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13594. if (!mboxq)
  13595. return -ENOMEM;
  13596. /* Post all rpi memory regions to the port. */
  13597. lpfc_resume_rpi(mboxq, ndlp);
  13598. if (cmpl) {
  13599. mboxq->mbox_cmpl = cmpl;
  13600. mboxq->context1 = arg;
  13601. mboxq->context2 = ndlp;
  13602. } else
  13603. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13604. mboxq->vport = ndlp->vport;
  13605. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13606. if (rc == MBX_NOT_FINISHED) {
  13607. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13608. "2010 Resume RPI Mailbox failed "
  13609. "status %d, mbxStatus x%x\n", rc,
  13610. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13611. mempool_free(mboxq, phba->mbox_mem_pool);
  13612. return -EIO;
  13613. }
  13614. return 0;
  13615. }
  13616. /**
  13617. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13618. * @vport: Pointer to the vport for which the vpi is being initialized
  13619. *
  13620. * This routine is invoked to activate a vpi with the port.
  13621. *
  13622. * Returns:
  13623. * 0 success
  13624. * -Evalue otherwise
  13625. **/
  13626. int
  13627. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13628. {
  13629. LPFC_MBOXQ_t *mboxq;
  13630. int rc = 0;
  13631. int retval = MBX_SUCCESS;
  13632. uint32_t mbox_tmo;
  13633. struct lpfc_hba *phba = vport->phba;
  13634. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13635. if (!mboxq)
  13636. return -ENOMEM;
  13637. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13638. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13639. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13640. if (rc != MBX_SUCCESS) {
  13641. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13642. "2022 INIT VPI Mailbox failed "
  13643. "status %d, mbxStatus x%x\n", rc,
  13644. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13645. retval = -EIO;
  13646. }
  13647. if (rc != MBX_TIMEOUT)
  13648. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13649. return retval;
  13650. }
  13651. /**
  13652. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13653. * @phba: pointer to lpfc hba data structure.
  13654. * @mboxq: Pointer to mailbox object.
  13655. *
  13656. * This routine is invoked to manually add a single FCF record. The caller
  13657. * must pass a completely initialized FCF_Record. This routine takes
  13658. * care of the nonembedded mailbox operations.
  13659. **/
  13660. static void
  13661. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13662. {
  13663. void *virt_addr;
  13664. union lpfc_sli4_cfg_shdr *shdr;
  13665. uint32_t shdr_status, shdr_add_status;
  13666. virt_addr = mboxq->sge_array->addr[0];
  13667. /* The IOCTL status is embedded in the mailbox subheader. */
  13668. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13669. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13670. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13671. if ((shdr_status || shdr_add_status) &&
  13672. (shdr_status != STATUS_FCF_IN_USE))
  13673. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13674. "2558 ADD_FCF_RECORD mailbox failed with "
  13675. "status x%x add_status x%x\n",
  13676. shdr_status, shdr_add_status);
  13677. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13678. }
  13679. /**
  13680. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13681. * @phba: pointer to lpfc hba data structure.
  13682. * @fcf_record: pointer to the initialized fcf record to add.
  13683. *
  13684. * This routine is invoked to manually add a single FCF record. The caller
  13685. * must pass a completely initialized FCF_Record. This routine takes
  13686. * care of the nonembedded mailbox operations.
  13687. **/
  13688. int
  13689. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13690. {
  13691. int rc = 0;
  13692. LPFC_MBOXQ_t *mboxq;
  13693. uint8_t *bytep;
  13694. void *virt_addr;
  13695. dma_addr_t phys_addr;
  13696. struct lpfc_mbx_sge sge;
  13697. uint32_t alloc_len, req_len;
  13698. uint32_t fcfindex;
  13699. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13700. if (!mboxq) {
  13701. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13702. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13703. return -ENOMEM;
  13704. }
  13705. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13706. sizeof(uint32_t);
  13707. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13708. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13709. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13710. req_len, LPFC_SLI4_MBX_NEMBED);
  13711. if (alloc_len < req_len) {
  13712. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13713. "2523 Allocated DMA memory size (x%x) is "
  13714. "less than the requested DMA memory "
  13715. "size (x%x)\n", alloc_len, req_len);
  13716. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13717. return -ENOMEM;
  13718. }
  13719. /*
  13720. * Get the first SGE entry from the non-embedded DMA memory. This
  13721. * routine only uses a single SGE.
  13722. */
  13723. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13724. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13725. virt_addr = mboxq->sge_array->addr[0];
  13726. /*
  13727. * Configure the FCF record for FCFI 0. This is the driver's
  13728. * hardcoded default and gets used in nonFIP mode.
  13729. */
  13730. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13731. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13732. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13733. /*
  13734. * Copy the fcf_index and the FCF Record Data. The data starts after
  13735. * the FCoE header plus word10. The data copy needs to be endian
  13736. * correct.
  13737. */
  13738. bytep += sizeof(uint32_t);
  13739. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13740. mboxq->vport = phba->pport;
  13741. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13742. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13743. if (rc == MBX_NOT_FINISHED) {
  13744. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13745. "2515 ADD_FCF_RECORD mailbox failed with "
  13746. "status 0x%x\n", rc);
  13747. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13748. rc = -EIO;
  13749. } else
  13750. rc = 0;
  13751. return rc;
  13752. }
  13753. /**
  13754. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13755. * @phba: pointer to lpfc hba data structure.
  13756. * @fcf_record: pointer to the fcf record to write the default data.
  13757. * @fcf_index: FCF table entry index.
  13758. *
  13759. * This routine is invoked to build the driver's default FCF record. The
  13760. * values used are hardcoded. This routine handles memory initialization.
  13761. *
  13762. **/
  13763. void
  13764. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13765. struct fcf_record *fcf_record,
  13766. uint16_t fcf_index)
  13767. {
  13768. memset(fcf_record, 0, sizeof(struct fcf_record));
  13769. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  13770. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  13771. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  13772. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  13773. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  13774. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  13775. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  13776. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  13777. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  13778. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  13779. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  13780. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  13781. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  13782. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  13783. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  13784. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  13785. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  13786. /* Set the VLAN bit map */
  13787. if (phba->valid_vlan) {
  13788. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  13789. = 1 << (phba->vlan_id % 8);
  13790. }
  13791. }
  13792. /**
  13793. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  13794. * @phba: pointer to lpfc hba data structure.
  13795. * @fcf_index: FCF table entry offset.
  13796. *
  13797. * This routine is invoked to scan the entire FCF table by reading FCF
  13798. * record and processing it one at a time starting from the @fcf_index
  13799. * for initial FCF discovery or fast FCF failover rediscovery.
  13800. *
  13801. * Return 0 if the mailbox command is submitted successfully, none 0
  13802. * otherwise.
  13803. **/
  13804. int
  13805. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13806. {
  13807. int rc = 0, error;
  13808. LPFC_MBOXQ_t *mboxq;
  13809. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  13810. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  13811. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13812. if (!mboxq) {
  13813. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13814. "2000 Failed to allocate mbox for "
  13815. "READ_FCF cmd\n");
  13816. error = -ENOMEM;
  13817. goto fail_fcf_scan;
  13818. }
  13819. /* Construct the read FCF record mailbox command */
  13820. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13821. if (rc) {
  13822. error = -EINVAL;
  13823. goto fail_fcf_scan;
  13824. }
  13825. /* Issue the mailbox command asynchronously */
  13826. mboxq->vport = phba->pport;
  13827. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  13828. spin_lock_irq(&phba->hbalock);
  13829. phba->hba_flag |= FCF_TS_INPROG;
  13830. spin_unlock_irq(&phba->hbalock);
  13831. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13832. if (rc == MBX_NOT_FINISHED)
  13833. error = -EIO;
  13834. else {
  13835. /* Reset eligible FCF count for new scan */
  13836. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  13837. phba->fcf.eligible_fcf_cnt = 0;
  13838. error = 0;
  13839. }
  13840. fail_fcf_scan:
  13841. if (error) {
  13842. if (mboxq)
  13843. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13844. /* FCF scan failed, clear FCF_TS_INPROG flag */
  13845. spin_lock_irq(&phba->hbalock);
  13846. phba->hba_flag &= ~FCF_TS_INPROG;
  13847. spin_unlock_irq(&phba->hbalock);
  13848. }
  13849. return error;
  13850. }
  13851. /**
  13852. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  13853. * @phba: pointer to lpfc hba data structure.
  13854. * @fcf_index: FCF table entry offset.
  13855. *
  13856. * This routine is invoked to read an FCF record indicated by @fcf_index
  13857. * and to use it for FLOGI roundrobin FCF failover.
  13858. *
  13859. * Return 0 if the mailbox command is submitted successfully, none 0
  13860. * otherwise.
  13861. **/
  13862. int
  13863. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13864. {
  13865. int rc = 0, error;
  13866. LPFC_MBOXQ_t *mboxq;
  13867. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13868. if (!mboxq) {
  13869. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13870. "2763 Failed to allocate mbox for "
  13871. "READ_FCF cmd\n");
  13872. error = -ENOMEM;
  13873. goto fail_fcf_read;
  13874. }
  13875. /* Construct the read FCF record mailbox command */
  13876. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13877. if (rc) {
  13878. error = -EINVAL;
  13879. goto fail_fcf_read;
  13880. }
  13881. /* Issue the mailbox command asynchronously */
  13882. mboxq->vport = phba->pport;
  13883. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  13884. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13885. if (rc == MBX_NOT_FINISHED)
  13886. error = -EIO;
  13887. else
  13888. error = 0;
  13889. fail_fcf_read:
  13890. if (error && mboxq)
  13891. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13892. return error;
  13893. }
  13894. /**
  13895. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  13896. * @phba: pointer to lpfc hba data structure.
  13897. * @fcf_index: FCF table entry offset.
  13898. *
  13899. * This routine is invoked to read an FCF record indicated by @fcf_index to
  13900. * determine whether it's eligible for FLOGI roundrobin failover list.
  13901. *
  13902. * Return 0 if the mailbox command is submitted successfully, none 0
  13903. * otherwise.
  13904. **/
  13905. int
  13906. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13907. {
  13908. int rc = 0, error;
  13909. LPFC_MBOXQ_t *mboxq;
  13910. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13911. if (!mboxq) {
  13912. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13913. "2758 Failed to allocate mbox for "
  13914. "READ_FCF cmd\n");
  13915. error = -ENOMEM;
  13916. goto fail_fcf_read;
  13917. }
  13918. /* Construct the read FCF record mailbox command */
  13919. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13920. if (rc) {
  13921. error = -EINVAL;
  13922. goto fail_fcf_read;
  13923. }
  13924. /* Issue the mailbox command asynchronously */
  13925. mboxq->vport = phba->pport;
  13926. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  13927. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13928. if (rc == MBX_NOT_FINISHED)
  13929. error = -EIO;
  13930. else
  13931. error = 0;
  13932. fail_fcf_read:
  13933. if (error && mboxq)
  13934. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13935. return error;
  13936. }
  13937. /**
  13938. * lpfc_check_next_fcf_pri
  13939. * phba pointer to the lpfc_hba struct for this port.
  13940. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  13941. * routine when the rr_bmask is empty. The FCF indecies are put into the
  13942. * rr_bmask based on their priority level. Starting from the highest priority
  13943. * to the lowest. The most likely FCF candidate will be in the highest
  13944. * priority group. When this routine is called it searches the fcf_pri list for
  13945. * next lowest priority group and repopulates the rr_bmask with only those
  13946. * fcf_indexes.
  13947. * returns:
  13948. * 1=success 0=failure
  13949. **/
  13950. int
  13951. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  13952. {
  13953. uint16_t next_fcf_pri;
  13954. uint16_t last_index;
  13955. struct lpfc_fcf_pri *fcf_pri;
  13956. int rc;
  13957. int ret = 0;
  13958. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  13959. LPFC_SLI4_FCF_TBL_INDX_MAX);
  13960. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13961. "3060 Last IDX %d\n", last_index);
  13962. if (list_empty(&phba->fcf.fcf_pri_list)) {
  13963. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13964. "3061 Last IDX %d\n", last_index);
  13965. return 0; /* Empty rr list */
  13966. }
  13967. next_fcf_pri = 0;
  13968. /*
  13969. * Clear the rr_bmask and set all of the bits that are at this
  13970. * priority.
  13971. */
  13972. memset(phba->fcf.fcf_rr_bmask, 0,
  13973. sizeof(*phba->fcf.fcf_rr_bmask));
  13974. spin_lock_irq(&phba->hbalock);
  13975. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13976. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  13977. continue;
  13978. /*
  13979. * the 1st priority that has not FLOGI failed
  13980. * will be the highest.
  13981. */
  13982. if (!next_fcf_pri)
  13983. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13984. spin_unlock_irq(&phba->hbalock);
  13985. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13986. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13987. fcf_pri->fcf_rec.fcf_index);
  13988. if (rc)
  13989. return 0;
  13990. }
  13991. spin_lock_irq(&phba->hbalock);
  13992. }
  13993. /*
  13994. * if next_fcf_pri was not set above and the list is not empty then
  13995. * we have failed flogis on all of them. So reset flogi failed
  13996. * and start at the begining.
  13997. */
  13998. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  13999. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14000. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  14001. /*
  14002. * the 1st priority that has not FLOGI failed
  14003. * will be the highest.
  14004. */
  14005. if (!next_fcf_pri)
  14006. next_fcf_pri = fcf_pri->fcf_rec.priority;
  14007. spin_unlock_irq(&phba->hbalock);
  14008. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  14009. rc = lpfc_sli4_fcf_rr_index_set(phba,
  14010. fcf_pri->fcf_rec.fcf_index);
  14011. if (rc)
  14012. return 0;
  14013. }
  14014. spin_lock_irq(&phba->hbalock);
  14015. }
  14016. } else
  14017. ret = 1;
  14018. spin_unlock_irq(&phba->hbalock);
  14019. return ret;
  14020. }
  14021. /**
  14022. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  14023. * @phba: pointer to lpfc hba data structure.
  14024. *
  14025. * This routine is to get the next eligible FCF record index in a round
  14026. * robin fashion. If the next eligible FCF record index equals to the
  14027. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  14028. * shall be returned, otherwise, the next eligible FCF record's index
  14029. * shall be returned.
  14030. **/
  14031. uint16_t
  14032. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  14033. {
  14034. uint16_t next_fcf_index;
  14035. /* Search start from next bit of currently registered FCF index */
  14036. next_priority:
  14037. next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
  14038. LPFC_SLI4_FCF_TBL_INDX_MAX;
  14039. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14040. LPFC_SLI4_FCF_TBL_INDX_MAX,
  14041. next_fcf_index);
  14042. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  14043. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14044. /*
  14045. * If we have wrapped then we need to clear the bits that
  14046. * have been tested so that we can detect when we should
  14047. * change the priority level.
  14048. */
  14049. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14050. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  14051. }
  14052. /* Check roundrobin failover list empty condition */
  14053. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  14054. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  14055. /*
  14056. * If next fcf index is not found check if there are lower
  14057. * Priority level fcf's in the fcf_priority list.
  14058. * Set up the rr_bmask with all of the avaiable fcf bits
  14059. * at that level and continue the selection process.
  14060. */
  14061. if (lpfc_check_next_fcf_pri_level(phba))
  14062. goto next_priority;
  14063. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14064. "2844 No roundrobin failover FCF available\n");
  14065. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  14066. return LPFC_FCOE_FCF_NEXT_NONE;
  14067. else {
  14068. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14069. "3063 Only FCF available idx %d, flag %x\n",
  14070. next_fcf_index,
  14071. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  14072. return next_fcf_index;
  14073. }
  14074. }
  14075. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  14076. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  14077. LPFC_FCF_FLOGI_FAILED)
  14078. goto next_priority;
  14079. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14080. "2845 Get next roundrobin failover FCF (x%x)\n",
  14081. next_fcf_index);
  14082. return next_fcf_index;
  14083. }
  14084. /**
  14085. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  14086. * @phba: pointer to lpfc hba data structure.
  14087. *
  14088. * This routine sets the FCF record index in to the eligible bmask for
  14089. * roundrobin failover search. It checks to make sure that the index
  14090. * does not go beyond the range of the driver allocated bmask dimension
  14091. * before setting the bit.
  14092. *
  14093. * Returns 0 if the index bit successfully set, otherwise, it returns
  14094. * -EINVAL.
  14095. **/
  14096. int
  14097. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  14098. {
  14099. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14100. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14101. "2610 FCF (x%x) reached driver's book "
  14102. "keeping dimension:x%x\n",
  14103. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14104. return -EINVAL;
  14105. }
  14106. /* Set the eligible FCF record index bmask */
  14107. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14108. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14109. "2790 Set FCF (x%x) to roundrobin FCF failover "
  14110. "bmask\n", fcf_index);
  14111. return 0;
  14112. }
  14113. /**
  14114. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  14115. * @phba: pointer to lpfc hba data structure.
  14116. *
  14117. * This routine clears the FCF record index from the eligible bmask for
  14118. * roundrobin failover search. It checks to make sure that the index
  14119. * does not go beyond the range of the driver allocated bmask dimension
  14120. * before clearing the bit.
  14121. **/
  14122. void
  14123. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  14124. {
  14125. struct lpfc_fcf_pri *fcf_pri;
  14126. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14127. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14128. "2762 FCF (x%x) reached driver's book "
  14129. "keeping dimension:x%x\n",
  14130. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14131. return;
  14132. }
  14133. /* Clear the eligible FCF record index bmask */
  14134. spin_lock_irq(&phba->hbalock);
  14135. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14136. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  14137. list_del_init(&fcf_pri->list);
  14138. break;
  14139. }
  14140. }
  14141. spin_unlock_irq(&phba->hbalock);
  14142. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14143. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14144. "2791 Clear FCF (x%x) from roundrobin failover "
  14145. "bmask\n", fcf_index);
  14146. }
  14147. /**
  14148. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  14149. * @phba: pointer to lpfc hba data structure.
  14150. *
  14151. * This routine is the completion routine for the rediscover FCF table mailbox
  14152. * command. If the mailbox command returned failure, it will try to stop the
  14153. * FCF rediscover wait timer.
  14154. **/
  14155. void
  14156. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  14157. {
  14158. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14159. uint32_t shdr_status, shdr_add_status;
  14160. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14161. shdr_status = bf_get(lpfc_mbox_hdr_status,
  14162. &redisc_fcf->header.cfg_shdr.response);
  14163. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  14164. &redisc_fcf->header.cfg_shdr.response);
  14165. if (shdr_status || shdr_add_status) {
  14166. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14167. "2746 Requesting for FCF rediscovery failed "
  14168. "status x%x add_status x%x\n",
  14169. shdr_status, shdr_add_status);
  14170. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  14171. spin_lock_irq(&phba->hbalock);
  14172. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  14173. spin_unlock_irq(&phba->hbalock);
  14174. /*
  14175. * CVL event triggered FCF rediscover request failed,
  14176. * last resort to re-try current registered FCF entry.
  14177. */
  14178. lpfc_retry_pport_discovery(phba);
  14179. } else {
  14180. spin_lock_irq(&phba->hbalock);
  14181. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  14182. spin_unlock_irq(&phba->hbalock);
  14183. /*
  14184. * DEAD FCF event triggered FCF rediscover request
  14185. * failed, last resort to fail over as a link down
  14186. * to FCF registration.
  14187. */
  14188. lpfc_sli4_fcf_dead_failthrough(phba);
  14189. }
  14190. } else {
  14191. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14192. "2775 Start FCF rediscover quiescent timer\n");
  14193. /*
  14194. * Start FCF rediscovery wait timer for pending FCF
  14195. * before rescan FCF record table.
  14196. */
  14197. lpfc_fcf_redisc_wait_start_timer(phba);
  14198. }
  14199. mempool_free(mbox, phba->mbox_mem_pool);
  14200. }
  14201. /**
  14202. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14203. * @phba: pointer to lpfc hba data structure.
  14204. *
  14205. * This routine is invoked to request for rediscovery of the entire FCF table
  14206. * by the port.
  14207. **/
  14208. int
  14209. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14210. {
  14211. LPFC_MBOXQ_t *mbox;
  14212. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14213. int rc, length;
  14214. /* Cancel retry delay timers to all vports before FCF rediscover */
  14215. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14216. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14217. if (!mbox) {
  14218. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14219. "2745 Failed to allocate mbox for "
  14220. "requesting FCF rediscover.\n");
  14221. return -ENOMEM;
  14222. }
  14223. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14224. sizeof(struct lpfc_sli4_cfg_mhdr));
  14225. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14226. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14227. length, LPFC_SLI4_MBX_EMBED);
  14228. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14229. /* Set count to 0 for invalidating the entire FCF database */
  14230. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14231. /* Issue the mailbox command asynchronously */
  14232. mbox->vport = phba->pport;
  14233. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14234. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14235. if (rc == MBX_NOT_FINISHED) {
  14236. mempool_free(mbox, phba->mbox_mem_pool);
  14237. return -EIO;
  14238. }
  14239. return 0;
  14240. }
  14241. /**
  14242. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14243. * @phba: pointer to lpfc hba data structure.
  14244. *
  14245. * This function is the failover routine as a last resort to the FCF DEAD
  14246. * event when driver failed to perform fast FCF failover.
  14247. **/
  14248. void
  14249. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14250. {
  14251. uint32_t link_state;
  14252. /*
  14253. * Last resort as FCF DEAD event failover will treat this as
  14254. * a link down, but save the link state because we don't want
  14255. * it to be changed to Link Down unless it is already down.
  14256. */
  14257. link_state = phba->link_state;
  14258. lpfc_linkdown(phba);
  14259. phba->link_state = link_state;
  14260. /* Unregister FCF if no devices connected to it */
  14261. lpfc_unregister_unused_fcf(phba);
  14262. }
  14263. /**
  14264. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14265. * @phba: pointer to lpfc hba data structure.
  14266. * @rgn23_data: pointer to configure region 23 data.
  14267. *
  14268. * This function gets SLI3 port configure region 23 data through memory dump
  14269. * mailbox command. When it successfully retrieves data, the size of the data
  14270. * will be returned, otherwise, 0 will be returned.
  14271. **/
  14272. static uint32_t
  14273. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14274. {
  14275. LPFC_MBOXQ_t *pmb = NULL;
  14276. MAILBOX_t *mb;
  14277. uint32_t offset = 0;
  14278. int rc;
  14279. if (!rgn23_data)
  14280. return 0;
  14281. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14282. if (!pmb) {
  14283. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14284. "2600 failed to allocate mailbox memory\n");
  14285. return 0;
  14286. }
  14287. mb = &pmb->u.mb;
  14288. do {
  14289. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14290. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14291. if (rc != MBX_SUCCESS) {
  14292. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14293. "2601 failed to read config "
  14294. "region 23, rc 0x%x Status 0x%x\n",
  14295. rc, mb->mbxStatus);
  14296. mb->un.varDmp.word_cnt = 0;
  14297. }
  14298. /*
  14299. * dump mem may return a zero when finished or we got a
  14300. * mailbox error, either way we are done.
  14301. */
  14302. if (mb->un.varDmp.word_cnt == 0)
  14303. break;
  14304. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14305. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14306. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14307. rgn23_data + offset,
  14308. mb->un.varDmp.word_cnt);
  14309. offset += mb->un.varDmp.word_cnt;
  14310. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14311. mempool_free(pmb, phba->mbox_mem_pool);
  14312. return offset;
  14313. }
  14314. /**
  14315. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14316. * @phba: pointer to lpfc hba data structure.
  14317. * @rgn23_data: pointer to configure region 23 data.
  14318. *
  14319. * This function gets SLI4 port configure region 23 data through memory dump
  14320. * mailbox command. When it successfully retrieves data, the size of the data
  14321. * will be returned, otherwise, 0 will be returned.
  14322. **/
  14323. static uint32_t
  14324. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14325. {
  14326. LPFC_MBOXQ_t *mboxq = NULL;
  14327. struct lpfc_dmabuf *mp = NULL;
  14328. struct lpfc_mqe *mqe;
  14329. uint32_t data_length = 0;
  14330. int rc;
  14331. if (!rgn23_data)
  14332. return 0;
  14333. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14334. if (!mboxq) {
  14335. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14336. "3105 failed to allocate mailbox memory\n");
  14337. return 0;
  14338. }
  14339. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14340. goto out;
  14341. mqe = &mboxq->u.mqe;
  14342. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14343. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14344. if (rc)
  14345. goto out;
  14346. data_length = mqe->un.mb_words[5];
  14347. if (data_length == 0)
  14348. goto out;
  14349. if (data_length > DMP_RGN23_SIZE) {
  14350. data_length = 0;
  14351. goto out;
  14352. }
  14353. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14354. out:
  14355. mempool_free(mboxq, phba->mbox_mem_pool);
  14356. if (mp) {
  14357. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14358. kfree(mp);
  14359. }
  14360. return data_length;
  14361. }
  14362. /**
  14363. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14364. * @phba: pointer to lpfc hba data structure.
  14365. *
  14366. * This function read region 23 and parse TLV for port status to
  14367. * decide if the user disaled the port. If the TLV indicates the
  14368. * port is disabled, the hba_flag is set accordingly.
  14369. **/
  14370. void
  14371. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14372. {
  14373. uint8_t *rgn23_data = NULL;
  14374. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14375. uint32_t offset = 0;
  14376. /* Get adapter Region 23 data */
  14377. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14378. if (!rgn23_data)
  14379. goto out;
  14380. if (phba->sli_rev < LPFC_SLI_REV4)
  14381. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14382. else {
  14383. if_type = bf_get(lpfc_sli_intf_if_type,
  14384. &phba->sli4_hba.sli_intf);
  14385. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14386. goto out;
  14387. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14388. }
  14389. if (!data_size)
  14390. goto out;
  14391. /* Check the region signature first */
  14392. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14393. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14394. "2619 Config region 23 has bad signature\n");
  14395. goto out;
  14396. }
  14397. offset += 4;
  14398. /* Check the data structure version */
  14399. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14400. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14401. "2620 Config region 23 has bad version\n");
  14402. goto out;
  14403. }
  14404. offset += 4;
  14405. /* Parse TLV entries in the region */
  14406. while (offset < data_size) {
  14407. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14408. break;
  14409. /*
  14410. * If the TLV is not driver specific TLV or driver id is
  14411. * not linux driver id, skip the record.
  14412. */
  14413. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14414. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14415. (rgn23_data[offset + 3] != 0)) {
  14416. offset += rgn23_data[offset + 1] * 4 + 4;
  14417. continue;
  14418. }
  14419. /* Driver found a driver specific TLV in the config region */
  14420. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14421. offset += 4;
  14422. tlv_offset = 0;
  14423. /*
  14424. * Search for configured port state sub-TLV.
  14425. */
  14426. while ((offset < data_size) &&
  14427. (tlv_offset < sub_tlv_len)) {
  14428. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14429. offset += 4;
  14430. tlv_offset += 4;
  14431. break;
  14432. }
  14433. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14434. offset += rgn23_data[offset + 1] * 4 + 4;
  14435. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14436. continue;
  14437. }
  14438. /* This HBA contains PORT_STE configured */
  14439. if (!rgn23_data[offset + 2])
  14440. phba->hba_flag |= LINK_DISABLED;
  14441. goto out;
  14442. }
  14443. }
  14444. out:
  14445. kfree(rgn23_data);
  14446. return;
  14447. }
  14448. /**
  14449. * lpfc_wr_object - write an object to the firmware
  14450. * @phba: HBA structure that indicates port to create a queue on.
  14451. * @dmabuf_list: list of dmabufs to write to the port.
  14452. * @size: the total byte value of the objects to write to the port.
  14453. * @offset: the current offset to be used to start the transfer.
  14454. *
  14455. * This routine will create a wr_object mailbox command to send to the port.
  14456. * the mailbox command will be constructed using the dma buffers described in
  14457. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14458. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14459. * used to indicate the starting offset of the transfer and will also return
  14460. * the offset after the write object mailbox has completed. @size is used to
  14461. * determine the end of the object and whether the eof bit should be set.
  14462. *
  14463. * Return 0 is successful and offset will contain the the new offset to use
  14464. * for the next write.
  14465. * Return negative value for error cases.
  14466. **/
  14467. int
  14468. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14469. uint32_t size, uint32_t *offset)
  14470. {
  14471. struct lpfc_mbx_wr_object *wr_object;
  14472. LPFC_MBOXQ_t *mbox;
  14473. int rc = 0, i = 0;
  14474. uint32_t shdr_status, shdr_add_status;
  14475. uint32_t mbox_tmo;
  14476. union lpfc_sli4_cfg_shdr *shdr;
  14477. struct lpfc_dmabuf *dmabuf;
  14478. uint32_t written = 0;
  14479. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14480. if (!mbox)
  14481. return -ENOMEM;
  14482. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14483. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14484. sizeof(struct lpfc_mbx_wr_object) -
  14485. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14486. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14487. wr_object->u.request.write_offset = *offset;
  14488. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14489. wr_object->u.request.object_name[0] =
  14490. cpu_to_le32(wr_object->u.request.object_name[0]);
  14491. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14492. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14493. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14494. break;
  14495. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14496. wr_object->u.request.bde[i].addrHigh =
  14497. putPaddrHigh(dmabuf->phys);
  14498. if (written + SLI4_PAGE_SIZE >= size) {
  14499. wr_object->u.request.bde[i].tus.f.bdeSize =
  14500. (size - written);
  14501. written += (size - written);
  14502. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14503. } else {
  14504. wr_object->u.request.bde[i].tus.f.bdeSize =
  14505. SLI4_PAGE_SIZE;
  14506. written += SLI4_PAGE_SIZE;
  14507. }
  14508. i++;
  14509. }
  14510. wr_object->u.request.bde_count = i;
  14511. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14512. if (!phba->sli4_hba.intr_enable)
  14513. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14514. else {
  14515. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14516. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14517. }
  14518. /* The IOCTL status is embedded in the mailbox subheader. */
  14519. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14520. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14521. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14522. if (rc != MBX_TIMEOUT)
  14523. mempool_free(mbox, phba->mbox_mem_pool);
  14524. if (shdr_status || shdr_add_status || rc) {
  14525. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14526. "3025 Write Object mailbox failed with "
  14527. "status x%x add_status x%x, mbx status x%x\n",
  14528. shdr_status, shdr_add_status, rc);
  14529. rc = -ENXIO;
  14530. } else
  14531. *offset += wr_object->u.response.actual_write_length;
  14532. return rc;
  14533. }
  14534. /**
  14535. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14536. * @vport: pointer to vport data structure.
  14537. *
  14538. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14539. * and REG_VPI mailbox commands associated with the vport. This function
  14540. * is called when driver want to restart discovery of the vport due to
  14541. * a Clear Virtual Link event.
  14542. **/
  14543. void
  14544. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14545. {
  14546. struct lpfc_hba *phba = vport->phba;
  14547. LPFC_MBOXQ_t *mb, *nextmb;
  14548. struct lpfc_dmabuf *mp;
  14549. struct lpfc_nodelist *ndlp;
  14550. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14551. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14552. LIST_HEAD(mbox_cmd_list);
  14553. uint8_t restart_loop;
  14554. /* Clean up internally queued mailbox commands with the vport */
  14555. spin_lock_irq(&phba->hbalock);
  14556. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14557. if (mb->vport != vport)
  14558. continue;
  14559. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14560. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14561. continue;
  14562. list_del(&mb->list);
  14563. list_add_tail(&mb->list, &mbox_cmd_list);
  14564. }
  14565. /* Clean up active mailbox command with the vport */
  14566. mb = phba->sli.mbox_active;
  14567. if (mb && (mb->vport == vport)) {
  14568. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14569. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14570. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14571. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14572. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14573. /* Put reference count for delayed processing */
  14574. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14575. /* Unregister the RPI when mailbox complete */
  14576. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14577. }
  14578. }
  14579. /* Cleanup any mailbox completions which are not yet processed */
  14580. do {
  14581. restart_loop = 0;
  14582. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14583. /*
  14584. * If this mailox is already processed or it is
  14585. * for another vport ignore it.
  14586. */
  14587. if ((mb->vport != vport) ||
  14588. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14589. continue;
  14590. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14591. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14592. continue;
  14593. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14594. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14595. ndlp = (struct lpfc_nodelist *)mb->context2;
  14596. /* Unregister the RPI when mailbox complete */
  14597. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14598. restart_loop = 1;
  14599. spin_unlock_irq(&phba->hbalock);
  14600. spin_lock(shost->host_lock);
  14601. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14602. spin_unlock(shost->host_lock);
  14603. spin_lock_irq(&phba->hbalock);
  14604. break;
  14605. }
  14606. }
  14607. } while (restart_loop);
  14608. spin_unlock_irq(&phba->hbalock);
  14609. /* Release the cleaned-up mailbox commands */
  14610. while (!list_empty(&mbox_cmd_list)) {
  14611. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14612. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14613. mp = (struct lpfc_dmabuf *) (mb->context1);
  14614. if (mp) {
  14615. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14616. kfree(mp);
  14617. }
  14618. ndlp = (struct lpfc_nodelist *) mb->context2;
  14619. mb->context2 = NULL;
  14620. if (ndlp) {
  14621. spin_lock(shost->host_lock);
  14622. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14623. spin_unlock(shost->host_lock);
  14624. lpfc_nlp_put(ndlp);
  14625. }
  14626. }
  14627. mempool_free(mb, phba->mbox_mem_pool);
  14628. }
  14629. /* Release the ndlp with the cleaned-up active mailbox command */
  14630. if (act_mbx_ndlp) {
  14631. spin_lock(shost->host_lock);
  14632. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14633. spin_unlock(shost->host_lock);
  14634. lpfc_nlp_put(act_mbx_ndlp);
  14635. }
  14636. }
  14637. /**
  14638. * lpfc_drain_txq - Drain the txq
  14639. * @phba: Pointer to HBA context object.
  14640. *
  14641. * This function attempt to submit IOCBs on the txq
  14642. * to the adapter. For SLI4 adapters, the txq contains
  14643. * ELS IOCBs that have been deferred because the there
  14644. * are no SGLs. This congestion can occur with large
  14645. * vport counts during node discovery.
  14646. **/
  14647. uint32_t
  14648. lpfc_drain_txq(struct lpfc_hba *phba)
  14649. {
  14650. LIST_HEAD(completions);
  14651. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14652. struct lpfc_iocbq *piocbq = 0;
  14653. unsigned long iflags = 0;
  14654. char *fail_msg = NULL;
  14655. struct lpfc_sglq *sglq;
  14656. union lpfc_wqe wqe;
  14657. spin_lock_irqsave(&phba->hbalock, iflags);
  14658. if (pring->txq_cnt > pring->txq_max)
  14659. pring->txq_max = pring->txq_cnt;
  14660. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14661. while (pring->txq_cnt) {
  14662. spin_lock_irqsave(&phba->hbalock, iflags);
  14663. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14664. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14665. if (!sglq) {
  14666. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14667. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14668. break;
  14669. } else {
  14670. if (!piocbq) {
  14671. /* The txq_cnt out of sync. This should
  14672. * never happen
  14673. */
  14674. sglq = __lpfc_clear_active_sglq(phba,
  14675. sglq->sli4_lxritag);
  14676. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14677. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14678. "2823 txq empty and txq_cnt is %d\n ",
  14679. pring->txq_cnt);
  14680. break;
  14681. }
  14682. }
  14683. /* The xri and iocb resources secured,
  14684. * attempt to issue request
  14685. */
  14686. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14687. piocbq->sli4_xritag = sglq->sli4_xritag;
  14688. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14689. fail_msg = "to convert bpl to sgl";
  14690. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14691. fail_msg = "to convert iocb to wqe";
  14692. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14693. fail_msg = " - Wq is full";
  14694. else
  14695. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14696. if (fail_msg) {
  14697. /* Failed means we can't issue and need to cancel */
  14698. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14699. "2822 IOCB failed %s iotag 0x%x "
  14700. "xri 0x%x\n",
  14701. fail_msg,
  14702. piocbq->iotag, piocbq->sli4_xritag);
  14703. list_add_tail(&piocbq->list, &completions);
  14704. }
  14705. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14706. }
  14707. /* Cancel all the IOCBs that cannot be issued */
  14708. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14709. IOERR_SLI_ABORTED);
  14710. return pring->txq_cnt;
  14711. }